//::///////////////////////////////////////////////
//:: Wild Mage Spell - Hornung's Surge Selector
//::///////////////////////////////////////////////
/*
Description modified from AD&D 2nd Edition - "Tome of Magic"

Hornung’s Surge Selector

Caster Level(s): Wizard / Sorcerer 7
Innate Level: 7
School: Abjuration
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Personal
Area of Effect / Target: Caster
Duration: Special
Save: N/A
Spell Resistance: N/A

!!! This is a Wild Magic Spell and can only be cast by a Wild Mage !!!
Saving Throw: None

     The great Hornung, having been blasted more than once by his own wild surges, devised a 
method of improving the results of wild magic and, not incidentally, his own chances of 
survival. The result was Hornung’s surge selector. By casting this spell, the wild mage gains 
greater control over wild surges. When the caster’s spell creates a wild surge, two separate 
results are determined. The caster can then choose which of the two results will take effect. 
This spell can be used in conjunction with Nahal’s reckless dweomer.

     The spell’s duration is a fixed number of surges or 12 hours, whichever comes first. The 
wild mage is able to shape one wild surge per five levels of his experience; thus, a 
15th-level caster could shape three wild surges within a 12-hour period. At the end of 12 
hours, the spell expires, regardless of the number of surges remaining.

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

#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 >= 11)
	{
/* 
  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; }
    }
	//:://////////////////////////////////////////
	
	int nDur = 12;
	float fDuration = HoursToSeconds(nDur);	

	effect eVis = EffectVisualEffect(VFX_DUR_SURGE_SELECTOR);
	
	effect eVis2 = EffectVisualEffect(VFX_DUR_LIGHT_PURPLE_5);
	effect eVis3 = EffectEffectIcon(EFFECT_ICON_SURGE_SELECTOR);
	effect eScale = EffectSetScale(1.0);
	effect eLink = EffectLinkEffects(eVis2,eVis3);
	eLink = EffectLinkEffects(eLink,eScale);
		
	//Fire cast spell at event for the specified target
    SignalEvent(oCaster, EventSpellCastAt(OBJECT_SELF, SPELL_SURGE_SELECTOR, FALSE));
	
	//Check for metamagic extend
    fDuration = ApplyMetamagicDurationMods(fDuration);
    int nDurType = ApplyMetamagicDurationTypeMods(DURATION_TYPE_TEMPORARY);

	JXApplyEffectToObject(nDurType, eVis, oCaster, fDuration);
	JXApplyEffectToObject(nDurType, eLink, oCaster, fDuration);
	
	SetLocalInt(oCaster,"nSurgeSelectorCount", 0);
	
	DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorCount"));
	DelayCommand(fDuration,DeleteLocalObject(oCaster,"oSurgeSelectorTarget"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorMeta"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorMeta"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorCasterLvl"));
	DelayCommand(fDuration,DeleteLocalLocation(oCaster,"nSurgeSelectorLocation"));
	DelayCommand(fDuration,DeleteLocalInt(oCaster,"SelectedSurgeOkButton"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"SelectedSurgeOkButton"));
	DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorRangeType"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"nSurgeSelectorSpellSaveDC"));
    DelayCommand(fDuration,DeleteLocalInt(oCaster,"nHornungsSurgeSelectionMade"));
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 9 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}