#include "inc_misc_tools"
void DoHenchAfterRest(object oPC)
{
    int iHeal = GetHitDice(oPC);
    if (GetLocalInt(oPC, "LongTermCare"))
        iHeal = iHeal *2;
    int iBeforeRest = GetLocalInt(oPC,"BeforeRestHPs");
    int iHP = GetCurrentHitPoints(oPC);
    if (iHP > iBeforeRest)
        ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectDamage(iHP - iBeforeRest),oPC);
    DeleteLocalInt(oPC,"LongTermCare");
}
void main()
    {
    object oPC = GetLastPCRested();
    if (GetLocalInt(GetArea(oPC),"Town"))
        return;// Resting in town is always allowed.
    object oItem;
    if (!GetIsPC(oPC) || GetIsDM(oPC))
        return;
    int iType = GetLastRestEventType();
    int iRest = HasAvailableRest(oPC);
    int iHeal = GetHitDice(oPC);
    int iHP = GetCurrentHitPoints(oPC);
    int iBeforeRest = GetLocalInt(oPC,"BeforeRestHPs");
    int iLongTermCare = GetLocalInt(oPC,"LongTermCare");
    object oHealer = GetHighestPartySkillMember(oPC,SKILL_HEAL);
    if (iType == REST_EVENTTYPE_REST_STARTED)
        {
        if (!HasAvailableRest(oPC))
            {
            if (!GetHasAllPartyRested(oPC))
                {
                FloatingTextStringOnCreature("You must wait for the rest of the party to rest.",oPC,FALSE);
                AssignCommand(oPC,ClearAllActions());
                SetLocalInt(oPC,"BeforeRestHPs",iHP);
                return;
                }
            else
                ResetPartyRests(oPC);
            }
        else if (GetHasFood(OBJECT_SELF)) // Must eat food to rest.
            {
            FloatingTextStringOnCreature("Hungry.",oPC,FALSE);
            AssignCommand(oPC,ClearAllActions());
            return;
            }
        SetLocalInt(oPC,"BeforeRestHPs",iHP);
        if (GetIsSkillSuccessful(oHealer,SKILL_HEAL,15))
            SetLocalInt(oPC,"LongTermCare",TRUE);
        if (!IsAnotherPartyMemberResting(oPC))
            DelayCommand(1.0,ExecuteScript("rdg_wand_monster",oPC));
        int i =1 ;
        object oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        while(GetIsObjectValid(oHench))
        {
            SetLocalInt(oHench,"BeforeRestHPs",GetCurrentHitPoints(oHench));
            if (GetIsSkillSuccessful(oHealer,SKILL_HEAL,15))
                SetLocalInt(oHench,"LongTermCare",TRUE);
            i++;
            oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        }
        }
    else if (iType == REST_EVENTTYPE_REST_FINISHED)
        {
        if (iLongTermCare)
            iHeal = iHeal * 2;
        if (iHP > iBeforeRest + iHeal)
            ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectDamage(iHP - (iBeforeRest + iHeal)),oPC);
        SetPanelButtonFlash(oPC,PANEL_BUTTON_REST,TRUE);
        DeleteLocalInt(oPC,"Rest");
        EatFood(oPC);
        DeleteLocalInt(oPC,"LongTermCare");
        int i =1 ;
        object oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        while(GetIsObjectValid(oHench))
        {
            DoHenchAfterRest(oHench);
            i++;
            oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        }
        }
    else if (iType == REST_EVENTTYPE_REST_CANCELLED)
        {
        if (iHP > iBeforeRest)
            ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectDamage(iHP - iBeforeRest),oPC);
        DeleteLocalInt(oPC,"LongTermCare");
        int i =1 ;
        object oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        while(GetIsObjectValid(oHench))
        {
            DoHenchAfterRest(oHench);
            i++;
            oHench = GetAssociate(ASSOCIATE_TYPE_HENCHMAN ,oPC,i);
        }
        }
    }
