//::///////////////////////////////////////////////
//:: Phobia wild surge enter 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_enter.nss

*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 05-22-2008
//:: Last Update: 05-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"
//#include "X0_I0_SPELLS"
//#include "x0_i0_spells"
#include "2d2f_inc_magic_wild"

void DeathCheck(object oTarget, object oCaster);

void main()
{
		object oPC = GetFirstPC();
		object oCaster = GetAreaOfEffectCreator();//JXGetAOERealCreator();
		object oTarget = GetEnteringObject();
		string sPhobicName = GetLocalString(oCaster,"sPhobicName"); // used for messages
		int nPhobiaSubRace = GetLocalInt(oCaster,"nPhobiaSubRace"); // used in enterscript
		int nPhobiaEncountersMax = GetLocalInt(oCaster,"nPhobiaEncountersMax");
		int nPhobiaEncountersTotal = GetLocalInt(oCaster,"nPhobiaEncountersTotal");
		int nSpellId = GetLocalInt(oCaster,"nPhobiaSpellId");
		
		if(GetSubRace(oTarget) == nPhobiaSubRace && GetSubRace(oCaster) != nPhobiaSubRace)
		{
			nPhobiaEncountersTotal++;
			SetLocalInt(oCaster,"nPhobiaEncountersTotal",nPhobiaEncountersTotal);
			if(nPhobiaEncountersTotal >= nPhobiaEncountersMax)
			{
				RemoveSupernaturalEffectWithSpellId(oCaster,nSpellId) ;
				DeleteLocalString(oCaster,"sPhobicName");
				DeleteLocalInt(oCaster,"nPhobiaSubRace");
				DeleteLocalInt(oCaster,"nPhobiaEncountersMax" );
				DeleteLocalInt(oCaster,"nPhobiaEncountersTotal" );
				DeleteLocalInt(oCaster,"nPhobiaSpellId");
				DestroyObject(OBJECT_SELF);
				FloatingTextStringOnCreature("You are no longer afraid of " + sPhobicName + ".",oCaster);
				return;			
			}		
		
			FloatingTextStringOnCreature("You crippling fear of " + sPhobicName + " takes hold as one of them gets to close.",oCaster);
			DeathCheck(oTarget,oCaster);
			SetLocalObject(oTarget,"oPhobicTarget",oTarget);
			
			effect eAB = EffectAttackDecrease(4);
			effect eAC = EffectACDecrease(4);
			effect eSkill = EffectSkillDecrease(SKILL_ALL_SKILLS,4);
			effect eSpell = EffectSpellFailure(50);
			effect eLink = EffectLinkEffects(eAB,eAC);
			effect eVis = EffectVisualEffect( VFX_DUR_SPELL_FEAR );
			eLink = EffectLinkEffects(eLink,eSkill);
			eLink = EffectLinkEffects(eLink,eSpell);
			eLink = EffectLinkEffects(eLink,eVis);
			//SetEffectSpellId(eLink,SURGE_TEMP_SPELLID_PHOBIA_FEAR);
			
			ApplyEffectToObject(DURATION_TYPE_PERMANENT,SetEffectSpellId(eLink,SURGE_TEMP_SPELLID_PHOBIA_FEAR),oCaster);
			
			// make save DC 12
			if (!/*Will Save*/ MySavingThrow(SAVING_THROW_WILL, oTarget, 12,SAVING_THROW_TYPE_FEAR))
			{
				int nDur = d4(1) + 2;
				float fDur = RoundsToSeconds(nDur);
				effect eFear = EffectFrightened();
				//SetEffectSpellId(eLink,SURGE_TEMP_SPELLID_PHOBIA_FEAR);
				
				ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eFear,oCaster,fDur);
				
			}
		
		}
		
	
}

void DeathCheck(object oTarget, object oCaster)
{
	if(GetIsDead(oTarget) == TRUE)
	{
		effect eAOE;
		object oPhobicTarget = GetLocalObject(oTarget,"oPhobicTarget");
	    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);
	        }    
		}
	}
	else if(GetDistanceBetween(oTarget,oCaster) < 9.9)
	{
		DelayCommand(RoundsToSeconds(1),DeathCheck(oTarget,oCaster));
	}
	else
	{
		return;
	}
}