// hcr3.1
// removed disabled logic.
// hcr3
// added heal ability token code.
// sr 6.0
// fix for npcs and healkit only post message when no target.
// sr 5.5.1
// added if statement around respawning subrace.
// Fixed healkit to work like 3e.
// Took out long term care too confusing and doesnt work correctly.
// added +2 bonus for using healkit.
// took out destroying heal kit as it now has 10 charges as per phb.
// HCR 5.5 change by Lorinton
// Modified to respawn the subrace after removing effects.
// Modified to reduce (hopefully eliminate) subrace effects being stripped by the game
// and other effects such as level drain being stripped by the subrace system.
// 5.4.1
// fixed some incorrrect if logic..
// note healing kits do not remove disabled state just help stop bleeding
// or provide long term care.
// added disabled logic to heal kits.
// 5.3 changed public cd key to player name
// hcr3.1 no subrace code needed.
// 5.5 Lorinton change
//#include "subraces"

#include "hc_inc"
#include "hc_text_bleed"
#include "hc_text_activate"

void main()
{

    object oUser=OBJECT_SELF;
    object oOther=GetLocalObject(oUser,"OTHER");
    object oItem=GetLocalObject(oUser,"ITEM");
    DeleteLocalObject(oUser,"ITEM");
    DeleteLocalObject(oUser,"OTHER");
    string sName=GetName(oOther);
    string sCDK=GetPCPlayerName(oOther);
    int nHeal = GetSkillRank(SKILL_HEAL, oUser);
    int nHealing;
    if (GetDistanceToObject(oOther) > FeetToMeters(8.0))
    {
        SendMessageToPC(oUser, MOVECLOSER);
        SetItemCharges(oItem, GetItemCharges(oItem)+1);
        return;
    }
    //If target is bleeding, attempt to stop the bleeding.
    // hcr3.1
    if(GPS(oOther) == PWS_PLAYER_STATE_STABLE ||
       GPS(oOther) == PWS_PLAYER_STATE_DYING)
    {
        nHealing = TRUE;
        if  (GPS(oOther) == PWS_PLAYER_STATE_STABLE ||
             GPS(oOther) == PWS_PLAYER_STATE_DYING )
             DelayCommand(1.0,AssignCommand(oUser, ActionPlayAnimation( ANIMATION_LOOPING_GET_LOW, 1.0, 2.0)));
        else
             DelayCommand(1.0,AssignCommand(oUser, ActionPlayAnimation( ANIMATION_LOOPING_GET_MID, 1.0, 2.0)));
        int nRandom = d20(1);
        // hcr3 added heal ability code with no modifier if using without healkit.
        int nResult;
        if (GetResRef(oItem) == "healability")
            nResult = nRandom + nHeal;
        else
            nResult = nRandom + nHeal + 2;
        // hcr3.1 fix for removing disabled.
        if(nResult > 15)
        {
               SendMessageToPC(oOther, STABLERECMESSAGE);
               SPS( oOther, PWS_PLAYER_STATE_STABLEHEAL);
               SendMessageToPC(oUser, STOPBLEEDING);

        }

        else   SendMessageToPC(oUser,NOSTOPBLEED);
        //DestroyObject(oItem);
        ApplyEffectToObject( DURATION_TYPE_TEMPORARY, EffectParalyze(), oOther, 6.0);
        return;
    }

    //Check for poison or disease, and attempt to remove the effect.
    effect eBad= GetFirstEffect(oOther);
    while(GetIsEffectValid(eBad))
    {
    //Check to see if the effect is poison or disease
        if (GetEffectType(eBad) == EFFECT_TYPE_POISON ||
            GetEffectType(eBad) == EFFECT_TYPE_DISEASE)
        {
            nHealing = TRUE;
            //AssignCommand( oUser, ActionMoveToLocation(GetLocation(oOther)));
            DelayCommand(1.0,AssignCommand(oUser,
                ActionPlayAnimation(ANIMATION_LOOPING_GET_MID, 1.0, 2.0)));
            int nHeal = GetSkillRank(SKILL_HEAL, oUser);
            int nRandom = d20(1);
            int nResult = nRandom + nHeal + 2;
             //TEMPORARY: Generate random DC from 12-22.  This will be replaced
            //by poison/disease specific DCs once I figure out how to determine
           //what type of poison or disease is afflicting the PC.
            int nDC = d6(2) + 10;

            if(nResult > nDC)
            {
                effect eVis = EffectVisualEffect(VFX_IMP_REMOVE_CONDITION);
                RemoveEffect(oOther, eBad);
                // 5.5 Lorinton change, respawn the subrace
                // 5.5.1 added check for SUBRACES variable.
                // hcr3.1
                //if (GetLocalInt(GetModule(), "SUBRACES"))
                    //Subraces_RespawnSubrace( oOther );
                ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oOther);
            }
            else SendMessageToPC(oUser, TREATMENTFAIL);

                //Destroy kit and end treatment attempt after first poison/disease effect.
            //DestroyObject(oItem);
            return;
        }
        eBad=GetNextEffect(oOther);
    }
 // 5.5.1 if they get to here theres no target or invalid target.
 // sr6.0
 // hcr3 if invalid target object just restore charges of item.
 if (!nHealing)
     SetItemCharges(oItem, GetItemCharges(oItem)+1);
}
