//::///////////////////////////////////////////////
//:: Wild Magic Spell - Glyph of Wild Magic
//:://////////////////////////////////////////////
/*
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.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"



void mainSpell();
void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{
	object oCaster = JXGetCaster();
	int nWMLvl = GetLevelByClass(CLASS_WILDMAGE, oCaster);
	if(nWMLvl >= 5)
	{
/* 
  Spellcast Hook Code 
  Added 2003-06-23 by GeorgZ
  If you want to make changes to all spells,
  check x2_inc_spellhook.nss to find out more
  
*/

    if (!X2PreSpellCastCode())
    {
	// If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        { JXPostSpellCastCode(); return; }
    }
	//:://////////////////////////////////////////
	
	location lTarget = JXGetSpellTargetLocation();
	int nDuration = JXGetCasterLevel(OBJECT_SELF) / 2;
	effect eVis = EffectVisualEffect(VFX_DUR_GLYPH_IDLE);
	effect eID = SetEffectSpellId(eVis,SPELL_GLYPH);
	effect eLink = EffectLinkEffects(eVis,eID);
	effect eAOE = EffectAreaOfEffect(AOE_PER_GLYPH_ENTER, "2d2f_sp_glyph_enter","","");
		
	//::2d2f: another oddity. the vfx doesn't care for creatures.
	object oVisPlace = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", lTarget, FALSE, "jx_ipoint_caster");
	SetLocalObject(oCaster, "oVisPlace1", oVisPlace);
		   
	int nMetaMagic = JXGetMetaMagicFeat();
    //Check Extend metamagic feat.
    if (nMetaMagic & METAMAGIC_EXTEND)
    {
	   nDuration = nDuration *2;//Duration is +100%
    }
	
	ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVis,oVisPlace,RoundsToSeconds(nDuration));
	ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY,eAOE,lTarget,RoundsToSeconds(nDuration));
			
	DestroyObject(oVisPlace, RoundsToSeconds(nDuration));
	DelayCommand(RoundsToSeconds(nDuration), DeleteLocalObject(oCaster, "oVisPlace1"));

	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 5 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}