//::///////////////////////////////////////////////
//:: Wild Magic Spell - Glyph of Wild Magic - Enter script
//:://////////////////////////////////////////////
/*
Glyph of Wild Magic

Caster Level(s): Wizard / Sorcerer 4
Innate Level: 4
School: Evocation
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Long
Area of Effect / Target: 20’ circle
Duration: 1 round / 2 lvl
Save: Special
Spell Resistance: Special

     A glyph of wild magic is a powerful inscription 
magically drawn on the ground or any other surface. Once 
placed the glyph will remain until an unfortunate creature 
passes across it or the duration expires.  If someone steps 
on the runes will explode in a fierce torrent of raw magical 
energy and fire a random spell between level 1 and 6 that is 
guaranteed to result in a wild surge. The probability of 
each level being cast is as follows.
Level 1 = 10%
Level 2 = 20%
Level 3 = 25%
Level 4 = 20%
Level 5 = 15%
Level 6 = 10%
One spell/surge is created for each creature within a 40' 
area.  The actual recipients of the spells/surges are chosen 
at random from within the blast area.

     Naturally, the spell's outcome is completely random. 
Victims can be strengthened by the wild surges, or they can 
be completely annihilated. The larger the number of 
creatures within the area of effect, the more chaotic the 
outcome, and the higher the probability that the glyph will 
cause a lot of damage.   

	2d2f_sp_glyph_enter.nss
*/
//:://////////////////////////////////////////////
//:: Created By: 2DruNk2FraG
//:: Created On: 03-18-08
//:: Last Update: 03-21-08
//:://////////////////////////////////////////////

//#include "2d2f_includes"
//#include "jx_inc_magic"
#include "x2_inc_spellhook" 
//#include "jx_inc_magic_info"
//#include "nwn2_inc_spells"
//#include "nw_i0_spells"
#include "2d2f_inc_magic_wild"


void main()
{
	object oPC = GetFirstPC();
	object oCaster = GetAreaOfEffectCreator();
	object oVisPlace = GetLocalObject(oPC,"oVisPlace1");
	float fRadius = FeetToMeters(40.0); // 20ft radius i think
	float fDelay;
	object oTarget;
	location lTarget = GetLocation(oVisPlace);
	int nEnterOrder = GetLocalInt(OBJECT_SELF,"nEnterOrder");
		
	oTarget = GetEnteringObject();
	
	//SendMessageToPC(oPC,"Glyph pre-enter target = " + GetName(oTarget));
	
	if(nEnterOrder == 0 && oTarget != OBJECT_SELF && oTarget != oVisPlace)
	{
	 	int nCount = 0;
		SetLocalInt(OBJECT_SELF,"nEnterOrder", 1);
		
		oTarget = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lTarget, FALSE, OBJECT_TYPE_CREATURE);
   				
		while(GetIsObjectValid(oTarget))
	        
        {
			//SendMessageToPC(oPC,"Glyph enter target = " + GetName(oTarget));
			SignalEvent(oTarget, EventSpellCastAt(oCaster, SPELL_GLYPH));
	                    
			DelayCommand(IntToFloat(nCount),doSurgeNoSpell(oTarget,fRadius));
						
			nCount++;
			
		oTarget = GetNextObjectInShape(SHAPE_SPHERE, fRadius, lTarget, FALSE, OBJECT_TYPE_CREATURE);
	   	}
		
		fDelay = IntToFloat(nCount);
		
		
	effect eVis = EffectVisualEffect(VFX_FNF_GLYPH_IMPACT);   
		
	RemoveSpellEffects(SPELL_GLYPH,oPC,oVisPlace);
	JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oVisPlace, fDelay);			
		
	DestroyObject(oVisPlace,fDelay);
	DestroyObject(OBJECT_SELF, fDelay);
	DelayCommand(fDelay, DeleteLocalObject(oCaster, "oVisPlace1"));

	}
}	