//::///////////////////////////////////////////////
//:: Wild Mage Spell - Chaos Shield
//::///////////////////////////////////////////////
/*
Description modified from AD&D 2nd Edition - "Tome of Magic"

Chaos Shield

Caster Level(s): Wizard / Sorcerer 2
Innate Level: 2
School: Abjuration 
Descriptor(s): Wild Magic
Component(s): Verbal, Somatic
Range: Personal
Area of Effect / Target: Caster
Duration: 1d10 rounds + 2 rounds/level
Save: Special
Spell Resistance: Special

Following the discovery of wild magic came the discovery of 
wild surges and the personal danger such surges create. 
After several wild mages destroyed themselves by rather 
spectacular means (or suffered very odd side effects), the 
chaos shield was created as protection from these surges. 
This spell imbues the wild mage with special protection 
against the effects of wild surges. It protects only against 
wild surges caused by the caster’s own spells, not from the 
effects of another mage’s wild surges. When a wild surge 
affects a caster protected by chaos shield, he is allowed a 
will saving throw vs. spells against the original spell that 
triggered the surge. If the saving throw is successful, the 
effect of the surge negated. If the saving throw is failed, 
the caster is affected normally by the surge. 

The chaos shield protects only the caster and does not 
negate the effects of a wild surge for other characters who
might be in the area of effect. The spell does not protect 
against wild surges that might be caused by its own casting. 
The caster cannot voluntarily cancel the protection once he 
has learned the nature of a wild surge; the chaos shield 
protects from both good and harmful effects. Thus, if a wild 
surge resulted in a heal spell for all characters within 10 
feet of the caster, the protected caster might not benefit, 
while all others in the radius would be healed. The spell 
remains in effect until it negates a wild surge or the spell 
duration expires.

-- To difficult to literally implement.  Changing it so that 
it negates all surge effects that effect the caster.


2d2f_sp_chaos_shield.nss
*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 02-17-2008
//:: Last Update: 03-22-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 >= 1) || (GetObjectType(oCaster) == OBJECT_TYPE_PLACEABLE))
	{
/* 
  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 nSpellId = JXGetSpellId();
	int nLvl = JXGetCasterLevel();
	int nDur = d10(1) + (2*nLvl);
	float fDuration = RoundsToSeconds(nDur);

	effect eVis = EffectVisualEffect(VFX_DUR_CHAOS_SHIELD);
	/*
	effect eVis2 = EffectVisualEffect(VFX_DUR_LIGHT_PURPLE_5);
	effect eVis3 = EffectEffectIcon(EFFECT_ICON_CHAOS_SHIELD);
	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_CHAOS_SHIELD, FALSE));
	
	//Check for metamagic extend
    fDuration = ApplyMetamagicDurationMods(fDuration);
    int nDurType = ApplyMetamagicDurationTypeMods(DURATION_TYPE_TEMPORARY);

	//JXApplyEffectToObject(nDurType, eLink, oCaster, fDuration);
	JXApplyEffectToObject(nDurType, eVis, oCaster, fDuration);
	
	}
	else
	{
	SendMessageToPC(oCaster, "You must be at least a level 1 Wild Mage to use this spell");
	}
	
	
	JXPostSpellCastCode();
	return;
}