//::///////////////////////////////////////////////
//:: Henchman Death Script
//::
//:: 69_hen_death
//::
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
//:: <description>
//:://////////////////////////////////////////////
//::
//:: Created By: 69MEH69 Jul 2004 Henchman Death Option
//:://////////////////////////////////////////////
#include "69_hench_lib"

void HenchmanBleed(object oHench = OBJECT_SELF);
void HenchHeal(object oHench = OBJECT_SELF);
void HenchmanDeath(object oHench = OBJECT_SELF);
void HenchSalvation(object oHench);
void HenchmanLootBag(object oHench);

void main()
{
    int HENCH_BLEED = GetLocalInt(GetModule(), "HENCH_BLEED");
    int TEST_MODE = GetLocalInt(GetModule(), "TEST_MODE");
    object oMaster = GetMaster(OBJECT_SELF);
    object oPC = GetLastMaster(OBJECT_SELF);
    string sName = GetName(OBJECT_SELF);
    object oArea = GetArea(OBJECT_SELF);
    string sTag = GetTag(OBJECT_SELF);
    string sLastName = GetName(oPC);

    //Test
    if(TEST_MODE == 1)
    {
     SendMessageToPC(GetFirstPC(), "My last master is " +sLastName);
    }
    //Adds up number of dead henchmen under PC's watch
    int nHenchDeath = GetLocalInt(oMaster, "Hench_Death");
    SetLocalInt(oMaster, "Hench_Death", nHenchDeath + 1);

    //Resets henchman familiar/animal companion
    SetLocalInt(OBJECT_SELF, "HasCompanion", FALSE);

    // I have yet to be hired but have died by unforeseen event
    if(!GetIsEnemy(oPC) && !GetIsObjectValid(oMaster))
    {
            ClearAllActions();
            SetHenchmanDying(OBJECT_SELF, FALSE);
            SetPlotFlag(OBJECT_SELF, TRUE);
            SetAssociateState(NW_ASC_IS_BUSY, TRUE);
            SetIsDestroyable(FALSE, TRUE, TRUE);
            DelayCommand(30.0, RespawnHenchman69());

    }
    // I may not be currently hired but am indeed an enemy to my last master
    if(GetIsEnemy(oPC) && !GetIsObjectValid(oMaster))
    {
            ClearAllActions();
            //Give XP
            ExecuteScript("nw_c2_default7", OBJECT_SELF);
            //ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_COMMONER);
            ClearPersonalReputation(oPC);
            ClearPersonalReputation(OBJECT_SELF, oPC);
            //SetFriendly(oPC);
            SetIsDestroyable(FALSE, FALSE, TRUE);
            DelayCommand(59.0, SetIsDestroyable(TRUE, FALSE, TRUE));
            DelayCommand(60.0, DestroyObject(OBJECT_SELF));
    }
    // * I am a familiar, give 1d6 damage to my master
    if(GetAssociate(ASSOCIATE_TYPE_FAMILIAR, oMaster) == OBJECT_SELF)
    {
            // April 2002: Made it so that familiar death can never kill the player
            // only wound them.
            int nDam = d6();
            if (nDam >= GetCurrentHitPoints(oMaster))
            {
                nDam = GetCurrentHitPoints(oMaster) - 1;
            }
            effect eDam = EffectDamage(nDam);
            FloatingTextStrRefOnCreature(63489, oMaster, FALSE);
            ApplyEffectToObject(DURATION_TYPE_PERMANENT, eDam, oMaster);
    }

    if(HENCH_BLEED == 0)
    {
     if(GetIsObjectValid(oMaster))
     {
        if(GetAssociateType(OBJECT_SELF) == ASSOCIATE_TYPE_HENCHMAN)
        {
            ClearAllDialogue(oMaster, OBJECT_SELF);
            ClearAllActions();
            SetLastMaster(oMaster, OBJECT_SELF);
            HenchmanDeath(OBJECT_SELF);
            SetDidDie(TRUE, OBJECT_SELF);
            SetHenchmanDying(OBJECT_SELF, FALSE);
            SetPlotFlag(OBJECT_SELF, FALSE);
            SetAssociateState(NW_ASC_IS_BUSY, TRUE);
            SetIsDestroyable(TRUE, FALSE, TRUE);

            //Spawn henchman to location specified in spawn script
            DelayCommand(60.0, DestroyObject(OBJECT_SELF));
         }
     }
    }//End HENCH_BLEED = 0

    else if(HENCH_BLEED == 1)
    {
        if(GetIsObjectValid(oMaster))
        {
          ClearAllDialogue(oMaster, OBJECT_SELF);
          ClearAllActions();
          SetLastMaster(oMaster, OBJECT_SELF);
          //Inform player
          HenchmanDeath(OBJECT_SELF);

          // Mark us as in the process of dying
          SetHenchmanDying(OBJECT_SELF, TRUE);

          // Mark henchman PLOT & Busy
          //SetPlotFlag(oHench, TRUE);
          SetAssociateState(NW_ASC_IS_BUSY, FALSE, OBJECT_SELF);

          // Make henchman's corpse stick around,
          // be raiseable, and selectable
          SetIsDestroyable(FALSE, TRUE, TRUE);

          SetLocalObject(oArea, "DOA" +sTag, OBJECT_SELF);

          if(GetCurrentHitPoints(OBJECT_SELF) <= -10)
          {
                SetDidDie(TRUE, OBJECT_SELF);
                SetHenchmanDying(OBJECT_SELF, FALSE);
                PlayVoiceChat(VOICE_CHAT_DEATH); // scream one last time
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_DEATH), OBJECT_SELF); // make death dramatic
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDeath(), OBJECT_SELF); // now kill them
                SendMessageToPC(oMaster, sName+ " has died!");
                if(GetLocalInt(OBJECT_SELF, "HenchInv"))
                {
                 HenchmanLootBag(OBJECT_SELF);
                }
                return;
          }
          DelayCommand(0.5, VoiceHealMe(TRUE));
          if(GetCurrentHitPoints(OBJECT_SELF) > 0)
          {
            SetLocalInt(oArea, "nCHP" +sTag, -1);
          }
          else
          {
            SetLocalInt(oArea, "nCHP" +sTag, GetCurrentHitPoints(OBJECT_SELF));
          }

          HenchSalvation(OBJECT_SELF);

          if(GetIsHenchmanDying(OBJECT_SELF) == TRUE && GetDidDie(OBJECT_SELF) == FALSE)
          {
              SendMessageToPC(oMaster, sName+ " is dying");
              DelayCommand(6.0, HenchmanBleed(OBJECT_SELF));
          }
        }
    } //End HENCH_BLEED = 1

    else if(HENCH_BLEED == 2)
    {
         ClearAllDialogue(oMaster, OBJECT_SELF);
         ClearAllActions();
         SetLastMaster(oMaster, OBJECT_SELF);
         HenchmanDeath(OBJECT_SELF);
         SetDidDie(TRUE, OBJECT_SELF);
         SetHenchmanDying(OBJECT_SELF, FALSE);
         DelayCommand(15.0, DestroyObject(OBJECT_SELF));
    } //End HENCH_BLEED = 2

    else if(HENCH_BLEED == 3)
    {
        if(GetAssociateType(OBJECT_SELF) == ASSOCIATE_TYPE_HENCHMAN)
        {
            ClearAllDialogue(oMaster, OBJECT_SELF);
            ClearAllActions();

            HenchmanDeath(OBJECT_SELF);
            SetDidDie(TRUE, OBJECT_SELF);
            SetHenchmanDying(OBJECT_SELF, FALSE);
            SetPlotFlag(OBJECT_SELF, TRUE);
            SetAssociateState(NW_ASC_IS_BUSY, TRUE);
            SetIsDestroyable(FALSE, TRUE, TRUE);

            //Spawn henchman to location specified in spawn script
            DelayCommand(30.0, RespawnHenchman69());
         }

    }//End HENCH_BLEED = 3
}

void HenchmanBleed(object oHench = OBJECT_SELF)
{
  int HENCH_SALVATION = GetLocalInt(GetModule(), "HENCH_SALVATION");
  int TEST_MODE = GetLocalInt(GetModule(), "TEST_MODE");
  object oPC = GetLastMaster(oHench);
  string sTag = GetTag(oHench);
  object oArea = GetArea(oHench);
  string sName = GetName(oHench);

  if(HENCH_SALVATION == 1)
  {
   HenchSalvation(oHench);
  }
  /*switch (d6(1))
  {
     case 1: PlayVoiceChat(VOICE_CHAT_PAIN1); break;
     case 2: PlayVoiceChat(VOICE_CHAT_PAIN2); break;
     case 3: PlayVoiceChat(VOICE_CHAT_PAIN3); break;
     case 4: PlayVoiceChat(VOICE_CHAT_HEALME); break;
     case 5: PlayVoiceChat(VOICE_CHAT_NEARDEATH); break;
     case 6: PlayVoiceChat(VOICE_CHAT_HELP); break;
  }*/
  if((GetIsHenchmanDying(oHench) == TRUE) && (GetDidDie(oHench) == FALSE))
  {
   int nCHP = GetLocalInt(oArea, "nCHP" +sTag);
   SetLocalInt(oArea, "nCHP" +sTag, nCHP - 1);
   nCHP = GetLocalInt(oArea, "nCHP" +sTag);
   string sCHP = IntToString(nCHP);
   SendMessageToPC(oPC, sName+ " has " +sCHP+ " hit points!");
   //Test
   if(TEST_MODE == 1)
   {
    int nHP = GetCurrentHitPoints(OBJECT_SELF);
    string sHP = IntToString(nHP);
    SendMessageToPC(oPC, sName+ " has " +sHP+ " hit points");
   }
   if(GetLocalInt(oArea, "nCHP" +sTag) >= 1)
   {
    ClearAllActions();
    SendMessageToPC(oPC, sName+ " is healed!");
    PostRespawnCleanup69(oHench);
    PartialRes(oHench);
    HireHenchman69(oPC, oHench);

    if(GetLocalInt(oArea, "nCHP" +sTag) >= 20)
    {
     ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(GetMaxHitPoints(oHench)), oHench);
    }
    return;
   }
   else if(GetLocalInt(oArea, "nCHP" +sTag) <= -20)
   {
    SetDidDie(TRUE, oHench);
    SetHenchmanDying(oHench, FALSE);
    SendMessageToPC(oPC, sName+ " is beyond healing!");
    PlayVoiceChat(VOICE_CHAT_DEATH);  //scream one last time
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_DEATH), oHench); // make death dramatic
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDeath(), oHench); // now kill them
    if(GetLocalInt(oHench, "HenchInv"))
    {
     HenchmanLootBag(oHench);
    }
     return;
   }
   else
   {
    SendMessageToPC(oPC, sName+ " is dying");
    DelayCommand(6.0, HenchmanBleed(oHench));
   }
  }
}

void HenchHeal(object oHench = OBJECT_SELF)
{
 object oPC = GetLastMaster(oHench);
 string sName = GetName(oHench);
 object oArea = GetArea(oHench);
 string sTag = GetTag(oHench);
 int nCHP = GetLocalInt(oArea, "nCHP" +sTag);
 SetLocalInt(oArea, "nCHP" +sTag, nCHP + 1);
 nCHP = GetLocalInt(oArea, "nCHP" +sTag);
 string sCHP = IntToString(nCHP);
 SendMessageToPC(oPC, sName+ " has " +sCHP+ " hit points!");
 if(nCHP >= 1)
 {
  PostRespawnCleanup69(oHench);
  PartialRes(oHench);
  if((GetResurrected(oPC) == TRUE) && (GetDidDie(oHench) == TRUE))
  {
   HireHenchman69(oPC);
  }
  else if(GetDidDie(oHench) == FALSE)
  {
   HireHenchman69(oPC);
  }
   return;
 }
 else
 {
  SendMessageToPC(oPC, sName+ " is healing");
  DelayCommand(6.0, HenchHeal(oHench));
 }

}

void HenchmanDeath(object oHench = OBJECT_SELF)
{
            object oPC = GetLastMaster(oHench);
            object oBlood = CreateObject(OBJECT_TYPE_PLACEABLE,"plc_bloodstain", GetLocation(oHench));
            //Remove blood after a while
            DestroyObject(oBlood,60.0f);
            // Henchman death notification
            string sHenchName = GetName(oHench);
            SendMessageToPC(oPC, sHenchName+ " has fallen!");
            //RemoveHenchman(oPC, oHench);

}


void HenchSalvation(object oHench = OBJECT_SELF)
{
 object oPC = GetLastMaster(oHench);
 string sTag = GetTag(oHench);
 object oArea = GetArea(oHench);
 string sName = GetName(oHench);
 int nSalvation = d10(1);
 if(nSalvation == 1 && GetDidDie(oHench) == FALSE)
 {
   int nGender = GetGender(oHench);
   string sGender;
   if(nGender == GENDER_FEMALE)
   {
     sGender = "her";
   }
   else if(nGender == GENDER_MALE)
   {
     sGender = "his";
   }
   else
   {
     sGender = "it's";
   }
   SetHenchmanDying(oHench, FALSE);
   PlayVoiceChat(VOICE_CHAT_LAUGH);
   SendMessageToPC(oPC, sName+ " has avoided death and is recovering from " +sGender+ " wounds!");
   DelayCommand(6.0, HenchHeal(oHench));

  }
}

void HenchmanLootBag(object oHench = OBJECT_SELF)
{
 object oPC = GetLastMaster(oHench);
 location lLoc = GetStepRightLocation(oHench);
 string sTag = GetTag(oHench);
 object oLootBag = CreateObject(OBJECT_TYPE_ITEM, "henchmanbackpack", lLoc, FALSE, sTag+ "BAG");
 MoveHenchmanItems69(oLootBag, oHench);
}