//::///////////////////////////////////////////////
//:: Rod of Wonder - GUI Cancel button script
//::///////////////////////////////////////////////
/*

Only used if caster has the Feat Chaotic Mind.

gui_2d2f_rod_wonder_cancel.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 06-15-2008
//:: Last Update: 06-15-2008
//::///////////////////////////////////////////////

#include "2d2f_inc_magic_wild"

void doSurgeNoSpellWonder(object oCaster, object oTarget);

void main()
{
	object oCaster = GetFirstPC();
	object oTarget = GetLocalObject(oCaster,"RodOfWonderTarget");
	
	int nSurgeIndex = GetLocalInt(oCaster,"RodOfWonderCancelButton");
    
	SetGlobalInt("next_surge_index",nSurgeIndex);
	doSurgeNoSpellWonder(oCaster,oTarget);

}

void doSurgeNoSpellWonder(object oCaster, object oTarget)
{
	object oPC = GetFirstPC(TRUE);
	
	int nSpellId;
	
	SetLocalInt(oCaster,"nDoSurgeNoSpell", 1);
	
	DelayCommand(1.0,SetLocalInt(oCaster,"nDoSurgeNoSpell", 0));
	DelayCommand(1.0,DeleteLocalInt(oCaster,"RodOfWonderOkButton"));
    DelayCommand(1.0,DeleteLocalInt(oCaster,"RodOfWonderCancelButton"));
	DelayCommand(1.0,DeleteLocalObject(oCaster,"RodOfWonderTarget"));
		
	// hostile										
	if(spellsIsTarget(oTarget,SPELL_TARGET_ALLALLIES,oCaster) == FALSE)
		{
		nSpellId = SPELL_RANDOM_SPELL_UTILITY_HOSTILE;
	}
	else // friendly
	{
		nSpellId = SPELL_RANDOM_SPELL_UTILITY_FRIENDLY;
	}
	
	//:debug
	//SendMessageToPC(oPC,"doSurge nospell spell id = " + IntToString(nSpellId));
	//SendMessageToPC(oPC,"doSurge nospell caster is = " + GetName(oCaster));
	//SendMessageToPC(oPC,"doSurge nospell target is = " + GetName(oTarget));
	
	// Shazbotian: added this here to fix a bug where glyph of wild magic would modifiy your dweomer selection without your knowledge
	//int iOriginalSpellId = GetLocalInt(oCaster,"nSpellId");

	SetLocalInt(oCaster,"nDoSurgeNoSpellSpellId",nSpellId);
	
	// Shazbotian: the idea here is to restore the original selected dweomer spell after the surge insanity is over
	//DelayCommand(2.0f, SetLocalInt(oCaster,"nSpellId",iOriginalSpellId));
		
	//:2d2f this code is to fix bug, apparently objects can't cast when walking... go fig
	// ... it took me 3 days to figure that out... ouch!
	effect eImmob = EffectCutsceneImmobilize();
	JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eImmob,oCaster,0.5);
	AssignCommand(oCaster, ClearAllActions());
			
	JXCastSpellFromObjectAtObject(oCaster,
								oTarget,
								nSpellId,
								METAMAGIC_NONE,
								JXGetCasterLevel(oCaster),
								JXGetSpellSaveDC(oCaster),
								TRUE);
}