//::///////////////////////////////////////////////
//:: Hornung's Surge Selector - Cancel button choice script
//::///////////////////////////////////////////////
/*
gui_2d2f_surge_selector_cancel.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 06-14-2008
//:: Last Update: 06-14-2008
//::///////////////////////////////////////////////

#include "2d2f_inc_magic_wild"
//#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()
{
	object oCaster = GetFirstPC();
	
	int nSurgeIndex = GetLocalInt(oCaster,"SelectedSurgeCancelButton");
	//SendMessageToPC(oCaster,"SelectedSurgeCancelButton index = " + IntToString(nSurgeIndex));
	SetGlobalInt("next_surge_index",nSurgeIndex);
	AssignCommand(oCaster, mainSpell());
	//AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{

/* 
  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; }
    }
	//:://////////////////////////////////////////
	
	object oCaster = GetFirstPC();
	
	object oTarget = GetLocalObject(oCaster,"oSurgeSelectorTarget");
    int nSpellId = GetLocalInt(oCaster,"nSurgeSelectorSpellId");
    int nMeta = GetLocalInt(oCaster,"nSurgeSelectorMeta");
    int nCasterLvl = GetLocalInt(oCaster,"nSurgeSelectorCasterLvl");
    location lLoc = GetLocalLocation(oCaster,"nSurgeSelectorLocation");
	int nSpellSaveDC = GetLocalInt(oCaster,"nSurgeSelectorSpellSaveDC");
	
	if(GetIsObjectValid(oTarget) == TRUE)
	{
		JXCastSpellFromObjectAtObject(oCaster, oTarget, nSpellId, nMeta, nCasterLvl, nSpellSaveDC, TRUE);
	}
	else
	{
		JXCastSpellFromObjectAtLocation(oCaster, lLoc, nSpellId, nMeta, nCasterLvl, nSpellSaveDC, TRUE);
	}

    RemoveSurgeSelector(oCaster);	
		
	JXPostSpellCastCode();
	return;
}