//:: Phobia wild surge exit script
//::///////////////////////////////////////////////
/*
	Surge # 25
	
	Caster gains a phobia to random creature. 
	If the creature type is within 30 feet of caster, 
	save vs fear (DC 16) or flee (3-6 rounds) and suffer -4 to hit, ac, skills, 
	50% spell failure untell the creature moves out of range or is destroyed. 
	On save still suffer negative effects except fear. 
	Phobia lasts 1-4 days or untell the phobic object is encountered 3-6 times.
	
	2d2f_surge_phobia_exit.nss

*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 05-22-2008
//:: Last Update: 05-22-2008
//::///////////////////////////////////////////////

#include "2d2f_includes"

void main()
{
	object oTarget = GetExitingObject();
	//SendMessageToPC(GetFirstPC(),"oTarget = " + GetName(oTarget));
	object oPhobicTarget = GetLocalObject(oTarget,"oPhobicTarget");
	object oCaster = GetAreaOfEffectCreator();
    int bValid = FALSE;
    effect eAOE;
    if(GetHasSpellEffect(SURGE_TEMP_SPELLID_PHOBIA_FEAR, oCaster) && oTarget == oPhobicTarget)
    {
		//Search through the valid effects on the target.
        eAOE = GetFirstEffect(oCaster);
        while (GetIsEffectValid(eAOE))
        {
            if (GetEffectCreator(eAOE) == GetAreaOfEffectCreator())
            {
                //If the effect was created by the AOE then remove it
                if(GetEffectSpellId(eAOE) == SURGE_TEMP_SPELLID_PHOBIA_FEAR)
                {
					//SendMessageToPC(GetFirstPC(),"spell id = " + IntToString(GetEffectSpellId(eAOE)));
                    DeleteLocalObject(oTarget,"oPhobicTarget");
					RemoveEffect(oCaster, eAOE);
					//object oPC = GetFirstPC();
				}
            }
            //Get next effect on the target
            eAOE = GetNextEffect(oCaster);
        }
    }
}