//::///////////////////////////////////////////////////////////////////////////
//::
//::	nw_s0_shadescaster.nss
//::
//::	This is the ImpactScript for spell ID 969. If the caster clicks on 
//::	himself he will cast Premonition, Protection from Spells, and Shield.
//::
//::///////////////////////////////////////////////////////////////////////////
//::
//::	Created by: Brian Fox
//::	Created on: 6/27/06
//::
//::///////////////////////////////////////////////////////////////////////////
#include "nw_i0_spells"
#include "x2_inc_spellhook" 


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

void mainSpell()
{
    if (!X2PreSpellCastCode())
    {
		// If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        JXPostSpellCastCode();
		return;
    }

	int nCasterLevel = JXGetCasterLevel( OBJECT_SELF );
    int nDuration = nCasterLevel; // Duration is 1 hr per level of the caster

    if (JXGetMetaMagicFeat() & METAMAGIC_EXTEND)
    {
		nDuration = nDuration *2;	//Duration is +100%
    }

	object oTarget = JXGetSpellTargetObject();

    if ( oTarget == OBJECT_SELF )	// This may be redundant since the 2DA defines the spell as self-only
    {
		//Fire cast spell at event for the specified target
		SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, JXGetSpellId(), FALSE));
		
		// Get rid of old effects
		RemoveEffectsFromSpell(oTarget, SPELL_PROTECTION_FROM_SPELLS);
		RemoveEffectsFromSpell(oTarget, SPELL_PREMONITION);
		RemoveEffectsFromSpell(oTarget, SPELL_SHIELD);
		RemoveEffectsFromSpell(oTarget, JXGetSpellId());
		
		effect eLink;
		
		// The unified VFX
		effect eVis = EffectVisualEffect( VFX_SPELL_SHADES_BUFF );
		
		// Premonition	
		int nLimit = JXGetCasterLevel(oTarget) * 10;
		//effect eStone = EffectDamageReduction(30, DAMAGE_POWER_PLUS_FIVE, nLimit);	// 3.0 DR rules
		effect eStone = EffectDamageReduction( 30, GMATERIAL_METAL_ADAMANTINE, nLimit, DR_TYPE_GMATERIAL );		// 3.5 DR approximation
		eLink = EffectLinkEffects(eStone, eVis); // Link it up!
		
		// Protection from Spells
		effect ePfS_Save = EffectSavingThrowIncrease(SAVING_THROW_ALL, 8, SAVING_THROW_TYPE_SPELL);
		eLink = EffectLinkEffects(ePfS_Save, eLink);
		
		//Immunity to Magic Missiles
		effect eSpell = EffectSpellImmunity(SPELL_MAGIC_MISSILE);
		eLink = EffectLinkEffects(eLink, eSpell);
		
		//Shield
		effect eMA_AC = EffectACIncrease(4, AC_SHIELD_ENCHANTMENT_BONUS);
		eLink = EffectLinkEffects(eMA_AC, eLink);
		
		//Apply the armor bonuses and the VFX impact
		JXApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oTarget, HoursToSeconds(nDuration));
    }

	JXPostSpellCastCode();
}
