//::///////////////////////////////////////////////
//:: Henchman Death Script
//::
//:: 69_hen_death
//::
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
//:: <description>
//:://////////////////////////////////////////////
//::
//:: Created By:
//:: Modified by:   Brent, April 3 2002
//::                Removed delay in respawning
//::                the henchman - caused bugs
//:: Modified by 69MEH69 21Aug2002 Remove journal entry
//:://////////////////////////////////////////////

//::///////////////////////////////////////////////
//:: Greater Restoration
//:: NW_S0_GrRestore.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    Removes all negative effects of a temporary nature
    and all permanent effects of a supernatural nature
    from the character. Does not remove the effects
    relating to Mind-Affecting spells or movement alteration.
    Heals target for 5d8 + 1 point per caster level.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Jan 7, 2002
//:://////////////////////////////////////////////
//:: VFX Pass By: Preston W, On: June 20, 2001
#include "69_i0_henchman"

/*void RemoveEffects(object oHench)
{
    //Declare major variables
    object oTarget = oHench;
    effect eVisual = EffectVisualEffect(VFX_IMP_RESTORATION_GREATER);

    effect eBad = GetFirstEffect(oTarget);
    //Search for negative effects
    while(GetIsEffectValid(eBad))
    {
        if (GetEffectType(eBad) == EFFECT_TYPE_ABILITY_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_AC_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_ATTACK_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_IMMUNITY_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_SAVING_THROW_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_SPELL_RESISTANCE_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_SKILL_DECREASE ||
            GetEffectType(eBad) == EFFECT_TYPE_BLINDNESS ||
            GetEffectType(eBad) == EFFECT_TYPE_DEAF ||
            GetEffectType(eBad) == EFFECT_TYPE_CURSE ||
            GetEffectType(eBad) == EFFECT_TYPE_DISEASE ||
            GetEffectType(eBad) == EFFECT_TYPE_POISON ||
            GetEffectType(eBad) == EFFECT_TYPE_PARALYZE ||
            GetEffectType(eBad) == EFFECT_TYPE_NEGATIVELEVEL)
        {
            //Remove effect if it is negative.
            RemoveEffect(oTarget, eBad);
        }
        eBad = GetNextEffect(oTarget);
    }
    SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_RESTORATION, FALSE));
    ApplyEffectToObject(DURATION_TYPE_INSTANT, eVisual, oTarget);
}*/


void BringBack()
{
    SetFormerMaster(GetRealMaster(), OBJECT_SELF);
    //SetLocalObject(OBJECT_SELF,"NW_L_FORMERMASTER", GetRealMaster());
    // : REMINDER: The delay is here   for a reason
    DelayCommand(0.1, RemoveEffects(OBJECT_SELF));
    DelayCommand(0.2, ApplyEffectToObject(DURATION_TYPE_PERMANENT, EffectResurrection(), OBJECT_SELF));
    DelayCommand(0.3, ApplyEffectToObject(DURATION_TYPE_PERMANENT, EffectHeal(GetMaxHitPoints(OBJECT_SELF)), OBJECT_SELF));
    DelayCommand(5.1, SetIsDestroyable(TRUE, TRUE, TRUE));
    object oWay = GetObjectByTag("NW_DEATH_TEMPLE");
    int iQuest = GetLocalInt(oWay,"DEATH");
    if (GetIsObjectValid(oWay) == TRUE)
    {
        // * if in Source stone area, respawn at opening to area
        if (iQuest == 1)
        {
            DelayCommand(0.2, JumpToObject(GetObjectByTag("WP_DEATH"), FALSE));
            SendMessageToPC(GetMaster(), "Your henchman has died. You can find him back in the bards cell.");
        }
        else
            DelayCommand(0.2, JumpToObject(oWay, FALSE));
            SendMessageToPC(GetMaster(), "Your henchman has died. You can find him back in Hobarts Quarters.");
    }
    else
        DelayCommand(0.3, ActionSpeakString("UT: No place to go"));


}
void main()
{
    // * This is used by the advanced henchmen
    // * Let Brent know if it interferes with animal
    // * companions et cetera
    if (GetIsObjectValid(GetMaster()) == TRUE)
    {
        object oMe = GetAssociate(ASSOCIATE_TYPE_HENCHMAN, GetMaster());
        if (oMe == OBJECT_SELF
            // * this is to prevent 'double hits' from stopping
            // * the henchmen from moving to the temple of tyr
            // * I.e., henchmen dies 'twice', once after leaving  your party
            || GetDidDie(OBJECT_SELF))//GetLocalInt(OBJECT_SELF, "NW_L_HEN_I_DIED") == TRUE
        {


            object myMaster = GetMaster();
            object myHenchman = GetHenchman();
            object oPC = GetRealMaster();
            // Henchman death notification
            string sHenchName = GetName(OBJECT_SELF);
            SendMessageToPC(oPC, sHenchName+ " has died!");
            // Decrement number of henchmen
            int nHench = GetLocalInt(oPC, "nHench");
            string sHench = IntToString(nHench);
            SendMessageToPC(oPC, "Number of henchman was " +sHench);
            SetLocalInt(oPC, "nHench", nHench - 1);
            int nNHench = GetLocalInt(oPC, "nHench");
            string sNHench = IntToString(nNHench);
            SendMessageToPC(oPC, "Number of henchman is now " +sNHench);
            RemoveHenchman(myMaster);
            if (GetIsObjectValid(myHenchman))
            {
                RemoveHenchman(OBJECT_SELF, myHenchman);
                if (GetIsObjectValid(myMaster))
                {
                    AddHenchman(myMaster, myHenchman);
                }
            }




           SetPlotFlag(oMe, TRUE);
           SetAssociateState(NW_ASC_IS_BUSY, TRUE);
           SetIsDestroyable(FALSE, TRUE, TRUE);
           SetDidDie();
           //SetLocalInt(OBJECT_SELF, "NW_L_HEN_I_DIED", TRUE);
           //RemoveHenchman(GetMaster());
           //effect eRaise = EffectResurrection();
            ClearAllDialogue(oPC, OBJECT_SELF);
            ClearAllActions();
            DelayCommand(0.5, ActionDoCommand(SetCommandable(TRUE)));
            DelayCommand(5.0, ActionDoCommand(SetAssociateState(NW_ASC_IS_BUSY, FALSE)));
            if(GetStealthMode(OBJECT_SELF) == STEALTH_MODE_DISABLED)
            {
             DelayCommand(5.0, ActionDoCommand(SetAssociateState(NW_ASC_AGGRESSIVE_STEALTH, FALSE)));
            }
            else
            {
              DelayCommand(5.0, ActionDoCommand(SetAssociateState(NW_ASC_AGGRESSIVE_STEALTH, FALSE)));
              DelayCommand(5.0, ActionDoCommand(ActionUseSkill(SKILL_HIDE, OBJECT_SELF)));
            }
            if(GetLocalInt(OBJECT_SELF,"Scouting"))
            {
             SetLocalInt(OBJECT_SELF, "Scouting", FALSE);
            }
            DelayCommand(5.0, SetPlotFlag(oMe, FALSE));

            DelayCommand(10.0, BringBack());
            DelayCommand(10.5, SetCommandable(FALSE));


        }
        else
        // * I am a familiar, give 1d6 damage to my master
        if (GetAssociate(ASSOCIATE_TYPE_FAMILIAR, GetMaster()) == OBJECT_SELF)
        {
            // April 2002: Made it so that familiar death can never kill the player
            // only wound them.
            int nDam =d6();
            if (nDam >= GetCurrentHitPoints(GetMaster()))
            {
                nDam = GetCurrentHitPoints(GetMaster()) - 1;
            }
            effect eDam = EffectDamage(nDam);
            FloatingTextStrRefOnCreature(63489, GetMaster(), FALSE);
            ApplyEffectToObject(DURATION_TYPE_PERMANENT, eDam, GetMaster());
        }
    }
}

