// v2.1.0 -
// v2.0.0 -
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  hark_acb_spell
//::////////////////////////////////////////////////////////////////////////////
/*
   This determines if the spell just cast at the target is harmful or not. If
  it was and the spell was NOT friendly fire then start combat.
*/
//::////////////////////////////////////////////////////////////////////////////
#include "hark_i0_hench_ai"
#include "hark_i0_acb"
//::////////////////////////////////////////////////////////////////////////////
void main()
{
  if (!preEvent()) { return; }

  object oMaster = GetRealMaster();
  object oCaster = GetLastSpellCaster();
  if (!GetLastSpellHarmful())
    {
     int nId = GetLastSpell();
     if (nId == SPELL_CURE_LIGHT_WOUNDS ||
         nId == SPELL_CURE_CRITICAL_WOUNDS ||
         nId == SPELL_CURE_MINOR_WOUNDS ||
         nId == SPELL_CURE_MODERATE_WOUNDS ||
         nId == SPELL_CURE_SERIOUS_WOUNDS ||
         nId == SPELL_HEAL ||
         nId == 506 ||// BW Healing Kits.
         nId == SPELLABILITY_LAY_ON_HANDS ||
         nId == SPELLABILITY_WHOLENESS_OF_BODY ||
         nId == SPELL_HEALING_CIRCLE ||
         nId == SPELL_RAISE_DEAD ||
         nId == SPELL_RESURRECTION ||
         nId == SPELL_MASS_HEAL)
       {
        if (GetAssociateState(NW_ASC_MODE_DYING))
          { SPS(OBJECT_SELF, PWS_PLAYER_STATE_ALIVE); }

        if (GetIsPC(oCaster) && !GetIsDM(oCaster))
          { SetFriendly(oCaster); }
       }
    }
  else
    {
     // Henchman was spotted, stop being stealthy.
     if (GetStealthMode(OBJECT_SELF) == STEALTH_MODE_ACTIVATED)
       { ActionUseSkill(SKILL_HIDE, OBJECT_SELF); }

     // If the caster was a PC, indicate they are not very friendly.
     if (GetIsPC(oCaster) && !GetIsDM(oCaster))
       { SetFriendly(oCaster, FALSE); }

     WrapCommandable(TRUE);

     // Determine the reaction the Henchman will take to the harmful spell.
     if (GetAssociateState(NW_ASC_MODE_DYING))
       {
        SPS(OBJECT_SELF, PWS_PLAYER_STATE_DEAD);

        // If dying, then die. If caster was my master then also quit!
        if (GetIsObjectValid(oMaster))
          {
           if (oCaster == oMaster)
             {
              SendMessageToPC(oMaster, GetName(OBJECT_SELF) + HEN_SPELLDIE);
              QuitHenchman(oMaster);
             }
           else
             { FireHenchman(oMaster); }
          }
       }
     else if (MatchAreaOfEffectSpell(GetLastSpell()))
       {
        // Get out of, dispel or remove an area of effect spell.
        ClearActions(CLEAR_ACB_SPELL_Main);
        if (GetHasSpell(SPELL_GUST_OF_WIND))
          {
           ActionCastSpellAtLocation(SPELL_GUST_OF_WIND, GetLocation(OBJECT_SELF));
           ActionDoCommand(WrapCommandable(TRUE));
           WrapCommandable(FALSE);
          }
        else if (GetHasSpell(SPELL_DISPEL_MAGIC))
          {
           ActionCastSpellAtLocation(SPELL_DISPEL_MAGIC, GetLocation(OBJECT_SELF));
           ActionDoCommand(WrapCommandable(TRUE));
           WrapCommandable(FALSE);
          }
        else
          {
           ActionMoveToObject(oMaster, TRUE, 1.1);
           DelayCommand(1.2, ActionDoCommand(HenchDetermineCombatRound(oCaster)));
          }
       }
     else if (GetCurrentAction() != ACTION_TAUNT &&
             !GetIsObjectValid(GetAttackTarget()) &&
             !GetIsObjectValid(GetAttemptedSpellTarget()) &&
             !GetIsObjectValid(GetAttemptedAttackTarget()) &&
             !GetIsObjectValid(GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN)) &&
             !GetIsFriend(oCaster))
       { HenchDetermineCombatRound(oCaster); }
    }

  if (GetSpawnInCondition(NW_FLAG_EVENT_SPELL))
    { SignalEvent(OBJECT_SELF, EventUserDefined(1011)); }

  postEvent();
}
//::////////////////////////////////////////////////////////////////////////////
