//::///////////////////////////////////////////////
//:: Default: On Spell Cast At
//:: shaz_onspell_dispeldestroy
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    This determines if the spell just cast at the
    target is harmful or not.

    GZ 2003-Oct-02 : - New AoE Behavior AI. Will use
                       Dispel Magic against AOES 
                     - Flying Creatures will ignore
                       Grease

*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Dec 6, 2001
//:: Last Modified On: 2003-Oct-13
//:://////////////////////////////////////////////
// ChazM - 4/21/05 modified so NPC's can stay focused
// DBR 5/30/06 Added check to GetIsValidRetaliationTarget() as a fix for force attacking quest-giving NPC's
// ChazM - 1/26/07 - moved some shared code to ginc_behavior
// SHAZBOTIAN: 06/03/08: new script!!! same as nw_c2_defaultb excet if a dispel hits us, destroy ourselves
	// this is for the "spell elemntals" : being physical incarnations of spells

//#include "nw_i0_generic"
//#include "x2_i0_spells"
#include "ginc_behavior"


void main()
{
    object oCaster = GetLastSpellCaster();
	int nSpellID = GetLastSpell();

    int iFocused = GetIsFocused();

	// spell cast at me so no longer partially focused
	if (iFocused == FOCUSED_PARTIAL)
		SetLocalInt(OBJECT_SELF, VAR_FOCUSED, FOCUSED_STANDARD); // no longer focused

    if (iFocused == 2) {
        // remain focused
    }
    else if(GetLastSpellHarmful())
    {
		ReactToHarmfulSpell(oCaster, nSpellID, FALSE, FALSE); // starndard creature aren't player interruptible and don't use henchman AI
    }
    else // spell was not harmful
    {
        // ---------------------------------------------------------------------
        // July 14, 2003 BK
        // If there is a valid enemy nearby and a NON HARMFUL spell has been
        // cast on me  I should call DetermineCombatRound
        // I may be invisible and casting spells on myself to buff myself up
        // ---------------------------------------------------------------------
        // Fix: JE - let's only do this if I'm currently in combat. If I'm not
        // in combat, and something casts a spell on me, it'll make me search
        // out the nearest enemy, no matter where they are on the level, which
        // is kinda dumb.
        object oEnemy =GetNearestEnemy();
        if ((GetIsObjectValid(oEnemy) == TRUE) && (GetIsInCombat() == TRUE))
        {
           // SpeakString("keep me in combat");
            DetermineCombatRound(oEnemy);
        }
    }
	
	// Shazbotian: Here is the new code to destroy this (creature) when it gets hit by a dispel
	//  should we give the thing a will save or something?
	effect eVis = EffectVisualEffect(VFX_FNF_SUMMON_MONSTER_1);
	switch(nSpellID) {
		case SPELL_DISPEL_MAGIC:
		case SPELL_GREATER_DISPELLING:
		case SPELL_GREATER_WALL_DISPEL_MAGIC:
		case SPELL_LESSER_DISPEL:
		case SPELL_WALL_DISPEL_MAGIC:
		case SPELL_I_VORACIOUS_DISPELLING:
		case SPELL_MORDENKAINENS_DISJUNCTION:
			DestroyObject(OBJECT_SELF, 0.2f);
			ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVis, GetLocation(OBJECT_SELF));
			break;
		default:
			break;
	}

    // Send the user-defined event as appropriate
    if(GetSpawnInCondition(NW_FLAG_SPELL_CAST_AT_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_SPELL_CAST_AT));
    }


}