//::///////////////////////////////////////////////
//:: Remove Fear
//:: NW_S0_RmvFear.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    All non-enemies within a 10ft radius have their fear
    effects removed and are granted a +4 Save versus
    future fear effects.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: April 13, 2001
//:://////////////////////////////////////////////
// ChazM 1/18/07 - EvenFlw modifications

#include "NW_I0_SPELLS"

#include "jx_inc_magic"
#include "x2_inc_spellhook"

void mainSpell();
void main()
{
	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; }
    }

// End of Spell Cast Hook


    //Declare major variables
    object oTarget;
    effect eFear;
    int nMetaMagic = JXGetMetaMagicFeat();
    int nDuration = 100;
    effect eSave = EffectSavingThrowIncrease(SAVING_THROW_WILL, 4, SAVING_THROW_TYPE_FEAR);
    //effect eMind = EffectVisualEffect(VFX_DUR_MIND_AFFECTING_POSITIVE);
    //effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_POSITIVE
    effect eImpact = EffectVisualEffect(VFX_HIT_CURE_AOE);

    //effect eLink = EffectLinkEffects(eMind, eSave);
    //eLink = EffectLinkEffects(eLink, eDur);
    float fDelay;
    JXApplyEffectAtLocation(DURATION_TYPE_INSTANT, eImpact, JXGetSpellTargetLocation());
    effect eVis = EffectVisualEffect( VFX_HIT_SPELL_ABJURATION );

    if(nMetaMagic & METAMAGIC_EXTEND)
    {
       nDuration = nDuration*2;
    }

    //Get first target in the spell area
    oTarget = GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, JXGetSpellTargetLocation());
    while (GetIsObjectValid(oTarget))
    {
        //Only remove the fear effect from the people who are friends.
        if(!GetIsEnemy(oTarget))
        {
            fDelay = GetRandomDelay();
            //Fire cast spell at event for the specified target
            SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_REMOVE_FEAR, FALSE));
            eFear = GetFirstEffect(oTarget);
            //Get the first effect on the current target
            while(GetIsEffectValid(eFear))
            {
                if (GetEffectType(eFear) == EFFECT_TYPE_FRIGHTENED)
                {
                    //Remove any fear effects and apply the VFX impact
                    RemoveEffect(oTarget, eFear);
                    JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget);
                }
                //Get the next effect on the target
                eFear = GetNextEffect(oTarget);
            }
            //Apply the linked effects
            //DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oTarget, RoundsToSeconds(nDuration)));
			DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_TEMPORARY, eSave, oTarget, RoundsToSeconds(nDuration)));
        }
        //Get the next target in the spell area.
        oTarget = GetNextObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, JXGetSpellTargetLocation());
    }

	JXPostSpellCastCode();
}
