//::///////////////////////////////////////////////
//:: Default On Attacked
//:: NW_C2_DEFAULT5
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    If already fighting then ignore, else determine
    combat round
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Oct 16, 2001
//:://////////////////////////////////////////////
#include "cai_inc_hos"
#include "nw_i0_generic"

void main()
{
    //ExecuteScript("prc_npc_physatt", OBJECT_SELF);
	
	object oMe=OBJECT_SELF;
    string sType;
    if(GetFleeToExit()) {
        // Run away!
        ActivateFleeToExit();
    } else if (GetSpawnInCondition(NW_FLAG_SET_WARNINGS)) {
        // We give an attacker one warning before we attack
        // This is not fully implemented yet
        SetSpawnInCondition(NW_FLAG_SET_WARNINGS, FALSE);

        //Put a check in to see if this attacker was the last attacker
        //Possibly change the GetNPCWarning function to make the check
    } else {
        object oAttacker = GetLastAttacker();
        if (!GetIsObjectValid(oAttacker)) {
            // Don't do anything, invalid attacker

        } else if (!GetIsFighting(OBJECT_SELF)) {
            // We're not fighting anyone else, so
            // start fighting the attacker
            if(GetBehaviorState(NW_FLAG_BEHAVIOR_SPECIAL)) {
                SetSummonHelpIfAttacked();
                DetermineSpecialBehavior(oAttacker);
            } else if (GetArea(oAttacker) == GetArea(OBJECT_SELF)) {
                SetSummonHelpIfAttacked();
                if (GetLocalInt(GetModule(),"bCOMBAT_AI_OFF")!=TRUE&&GetLocalInt(oMe,"bCOMBAT_AI_ON")==TRUE)
        { // Combat AI is turned on for the module and NPC
          sType=GetLocalString(oMe,"sCOMBAT_AI_TYPE");
          sType=GetStringLowerCase(sType);
          if (sType=="melee")
          { // melee combat
            ExecuteScript("cai_melee",oMe);
          } // melee combat
          else if (sType=="ranged")
          { // ranged combat
            ExecuteScript("cai_ranged",oMe);
          } // ranged combat
          else if (sType=="stealth")
          { // stealth combat
            ExecuteScript("cai_stealth",oMe);
          } // stealth combat
          else if (sType=="support")
          { // support role such as healing and buffing
            ExecuteScript("cai_support",oMe);
          } // support role such as healing and buffing
          else if (sType=="summon")
          { // summoner type caster
            ExecuteScript("cai_summon",oMe);
          } // summoner type caster
          else if (sType=="caster")
          { // spell caster
            ExecuteScript("cai_caster",oMe);
          } // spell caster
          else if (sType=="flee")
          { // try not to get hurt under any circumstance
            ExecuteScript("cai_flee",oMe);
          } // try not to get hurt under any circumstance
          else if (sType=="hold")
          { // hold where you are
            ExecuteScript("cai_hold",oMe);
          } // hold where you are
          else
          { // based on weapon holding and skill
            if (GetSkillRank(SKILL_HIDE,oMe)>10)
            { // stealth
              ExecuteScript("cai_stealth",oMe);
            } // stealth
            else if (caiGetEquippedWeaponType(oMe,INVENTORY_SLOT_RIGHTHAND)==CAI_WEAPON_RANGED)
            { // ranged
              ExecuteScript("cai_ranged",oMe);
            } // ranged
            else if (caiGetIsArcane(oMe))
            { // caster - wizard/sorcerer
              ExecuteScript("cai_caster",oMe);
            } // caster - wizard/sorcerer
            else if (caiGetIsCleric(oMe))
            { // support - cleric
              ExecuteScript("cai_support",oMe);
            } // support - cleric
            else
            { // melee
              ExecuteScript("cai_melee",oMe);
            } // melee
          } // based on weapon holding and skill
        } // Combat AI is turned on for the module and NPC
        else
        { // standard combat
          DetermineCombatRound(oAttacker);
        } // standard combat
            }

            //Shout Attack my target, only works with the On Spawn In setup
            SpeakString("NW_ATTACK_MY_TARGET", TALKVOLUME_SILENT_TALK);

            //Shout that I was attacked
            SpeakString("NW_I_WAS_ATTACKED", TALKVOLUME_SILENT_TALK);
        }
    }


    if(GetSpawnInCondition(NW_FLAG_ATTACK_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_ATTACKED));
    }
}