// v2.0.0 -
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  hark_onply_rest
//::////////////////////////////////////////////////////////////////////////////
/*
   This will need to be added to the On Player Rest Event Hook of your
  module. The easiest way to do this is to add the following block of code to
  the postEvent hook located in the hc_inc_on_rest script.

  It should look something like this.

void postEvent()
{
  object oPC = GetLastPCRested();
  object oHench = GetHenchman(oPC);
  if (GetIsObjectValid(oHench))
    { ExecuteScript("hark_onply_rest", oHench); }
  return;
}

   Note: This MUST be added to the postEvent hook or it will not work!!!
*/
//::////////////////////////////////////////////////////////////////////////////
#include "hark_i0_generic"
//://///////////////////////////////////////////////////////////////////////////
void DoLimitedRestDamage(object oHench)
{
  int nSHP = GetLocalInt(oHench, "HenchmanHealth");
  int nCHP = GetCurrentHitPoints(oHench);
  int nHD = GetHitDice(oHench);
  if (nCHP > (nSHP + nHD))
    {
     int nDam = (nCHP - (nSHP + nHD));
     effect eDam = EffectDamage(nDam, DAMAGE_TYPE_MAGICAL, DAMAGE_POWER_PLUS_FIVE);
     ApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oHench);
    }
  DeleteLocalInt(oHench, "HenchmanHealth");
}
//://///////////////////////////////////////////////////////////////////////////
void main()
{
  object oHench = OBJECT_SELF;
  object oMaster = GetRealMaster(oHench);
  int nImResting = GetLocalInt(oHench, "HS_ASC_IS_RESTING");
  int nRestType = GetLastRestEventType();
  int nCHP = GetCurrentHitPoints(oHench);

  if (GetLocalInt(GetModule(), "RESTSYSTEM"))
    {
     if (!nImResting && GetLocalInt(GetModule(), "LIMITEDRESTHEAL") &&
          nRestType == REST_EVENTTYPE_REST_STARTED)
       { SetLocalInt(oHench, "HenchmanHealth", nCHP); }

     if (!nImResting && nRestType == REST_EVENTTYPE_REST_STARTED &&
         GetCurrentAction(oMaster) == ACTION_REST &&
         GetLocalInt(oMaster, "REST") == TRUE &&
         IsInConversation(oMaster) == FALSE)
       {
        SetLocalInt(oHench, "HS_ASC_IS_RESTING", TRUE);
        if (GetRacialType(oHench) != RACIAL_TYPE_ELF &&
            GetRacialType(oHench) != RACIAL_TYPE_HALFELF)
          {
           effect eSnore = EffectVisualEffect(VFX_IMP_SLEEP);
           ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eSnore, oHench, 7.0);
           DelayCommand(7.0, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eSnore, oHench, 7.0));
          }
        if (GetIsObjectValid(GetHenchman(oHench)))
          { ExecuteScript("hark_onply_rest", GetHenchman(oHench)); }
       }
     else if (nImResting && (nRestType == REST_EVENTTYPE_REST_FINISHED ||
                             nRestType == REST_EVENTTYPE_REST_CANCELLED))
       {
        DeleteLocalInt(oHench, "HS_ASC_IS_RESTING");
        if (GetLocalInt(GetModule(), "LIMITEDRESTHEAL"))
          { DelayCommand(1.0, DoLimitedRestDamage(oHench)); }
        if (GetIsObjectValid(GetHenchman(oHench)))
          { ExecuteScript("hark_onply_rest", GetHenchman(oHench)); }
       }
    }
}
//://///////////////////////////////////////////////////////////////////////////
