// Module : OnPlayerDeath by Brian "spilth" Kelly
// For Neverwinter Nights - Bleeding Tutorial

#include "bleeding_config"
#include "x3_inc_horse"

void Raise(object oPlayer)
{
        effect eVisual = EffectVisualEffect(VFX_IMP_RESTORATION);

        effect eBad = GetFirstEffect(oPlayer);
        ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectResurrection(),oPlayer);
        ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectHeal(GetMaxHitPoints(oPlayer)), oPlayer);

        //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_PARALYZE ||
                GetEffectType(eBad) == EFFECT_TYPE_NEGATIVELEVEL)
                {
                    //Remove effect if it is negative.
                    RemoveEffect(oPlayer, eBad);
                }
            eBad = GetNextEffect(oPlayer);
        }
        //Fire cast spell at event for the specified target
        SignalEvent(oPlayer, EventSpellCastAt(OBJECT_SELF, SPELL_RESTORATION, FALSE));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eVisual, oPlayer);
}

void main() {

	object oPC = GetLastPlayerDied();  
    object oPlayer = oPC;
    object oHorse;
    object oInventory;
    string sID;
    int nC;
    string sT;
    string sR;
    int nCH;
    int nST;
    object oItem;
    effect eEffect;
    string sDB="X3SADDLEBAG"+GetTag(GetModule());
    if (GetStringLength(GetLocalString(GetModule(),"X3_SADDLEBAG_DATABASE"))>0) sDB=GetLocalString(GetModule(),"X3_SADDLEBAG_DATABASE");
    if (HorseGetIsMounted(oPlayer))
    { // Dismount and then die
        //SetCommandable(FALSE,oPlayer);
        //ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectResurrection(),oPlayer);
        DelayCommand(0.3,HORSE_SupportResetUnmountedAppearance(oPlayer));
        DelayCommand(3.0,HORSE_SupportCleanVariables(oPlayer));
        DelayCommand(1.0,HORSE_SupportRemoveACBonus(oPlayer));
        DelayCommand(1.0,HORSE_SupportRemoveHPBonus(oPlayer));
        DelayCommand(1.1,HORSE_SupportRemoveMountedSkillDecreases(oPlayer));
        DelayCommand(1.1,HORSE_SupportAdjustMountedArcheryPenalty(oPlayer));
        DelayCommand(1.2,HORSE_SupportOriginalSpeed(oPlayer));
        if (!GetLocalInt(GetModule(),"X3_HORSE_NO_CORPSES"))
        { // okay to create lootable horse corpses
            sR=GetSkinString(oPlayer,"sX3_HorseResRef");
            sT=GetSkinString(oPlayer,"sX3_HorseMountTag");
            nCH=GetSkinInt(oPlayer,"nX3_HorseAppearance");
            nST=GetSkinInt(oPlayer,"nX3_HorseTail");
            nC=GetLocalInt(oPlayer,"nX3_HorsePortrait");
            if (GetStringLength(sR)>0&&GetStringLeft(sR,GetStringLength(HORSE_PALADIN_PREFIX))!=HORSE_PALADIN_PREFIX)
            { // create horse
                oHorse=HorseCreateHorse(sR,GetLocation(oPlayer),oPlayer,sT,nCH,nST);
                SetLootable(oHorse,TRUE);
                SetPortraitId(oHorse,nC);
                SetLocalInt(oHorse,"bDie",TRUE);
                AssignCommand(oHorse,SetIsDestroyable(FALSE,TRUE,TRUE));
            } // create horse
        } // okay to create lootable horse corpses
        oInventory=GetLocalObject(oPlayer,"oX3_Saddlebags");
        sID=GetLocalString(oPlayer,"sDB_Inv");
        if (GetIsObjectValid(oInventory))
        { // drop horse saddlebags
            if (!GetIsObjectValid(oHorse))
            { // no horse created
                HORSE_SupportTransferInventory(oInventory,OBJECT_INVALID,GetLocation(oPlayer),TRUE);
            } // no horse created
            else
            { // transfer to horse
                HORSE_SupportTransferInventory(oInventory,oHorse,GetLocation(oHorse),TRUE);
                //DelayCommand(2.0,PurgeSkinObject(oHorse));
                //DelayCommand(3.0,KillTheHorse(oHorse));
                //DelayCommand(1.8,PurgeSkinObject(oHorse));
            } // transfer to horse
        } // drop horse saddlebags
        else if (GetStringLength(sID)>0)
        { // database based inventory
            nC=GetCampaignInt(sDB,"nCO_"+sID);
            while(nC>0)
            { // restore inventory
                sR=GetCampaignString(sDB,"sR"+sID+IntToString(nC));
                sT=GetCampaignString(sDB,"sT"+sID+IntToString(nC));
                nST=GetCampaignInt(sDB,"nS"+sID+IntToString(nC));
                nCH=GetCampaignInt(sDB,"nC"+sID+IntToString(nC));
                DeleteCampaignVariable(sDB,"sR"+sID+IntToString(nC));
                DeleteCampaignVariable(sDB,"sT"+sID+IntToString(nC));
                DeleteCampaignVariable(sDB,"nS"+sID+IntToString(nC));
                DeleteCampaignVariable(sDB,"nC"+sID+IntToString(nC));
                if (!GetIsObjectValid(oHorse))
                { // no lootable corpse
                    oItem=CreateObject(OBJECT_TYPE_ITEM,sR,GetLocation(oPlayer),FALSE,sT);
                } // no lootable corpse
                else
                { // lootable corpse
                    oItem=CreateItemOnObject(sR,oHorse,nST,sT);
                } // lootable corpse
                if (GetItemStackSize(oItem)!=nST) SetItemStackSize(oItem,nST);
                if (nCH>0) SetItemCharges(oItem,nCH);
                nC--;
            } // restore inventory
            DeleteCampaignVariable(sDB,"nCO_"+sID);
            //DelayCommand(2.0,PurgeSkinObject(oHorse));
            if (GetIsObjectValid(oHorse)&&GetLocalInt(oHorse,"bDie")) DelayCommand(3.0,KillTheHorse(oHorse));
            //DelayCommand(2.5,PurgeSkinObject(oHorse));
        } // database based inventory
        else if (GetIsObjectValid(oHorse))
        { // no inventory
            //DelayCommand(1.0,PurgeSkinObject(oHorse));
            DelayCommand(2.0,KillTheHorse(oHorse));
            //DelayCommand(1.8,PurgeSkinObject(oHorse));
        } // no inventory
        //eEffect=EffectDeath();
        //DelayCommand(1.6,ApplyEffectToObject(DURATION_TYPE_INSTANT,eEffect,oPlayer));
        //DelayCommand(1.7,SetCommandable(TRUE,oPlayer));
        //return;
    } // Dismount and then die

    // * increment global tracking number of times that I died
    SetLocalInt(oPlayer, "NW_L_PLAYER_DIED", GetLocalInt(oPlayer, "NW_L_PLAYER_DIED") + 1);

    // * BK: Automation Control. Autopcs ignore death
    if (GetLocalInt(oPlayer, "NW_L_AUTOMATION") == 10)
    {
        Raise(oPlayer);
        DelayCommand(1.0, ExecuteScript("crawl", OBJECT_SELF));
        return; // Raise and return
    }  

/*************************************************************

   object oPC = GetLastPlayerDied();

   // The DeathGUI automatically re-pops up when you're dead
    // So no need to make OnHeartbeat do any work
    SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_DEAD);

    // Save their hit points in case they disconnect
    SetLocalInt(oPC, "LastHitPoints", GetCurrentHitPoints(oPC));
******************************************************************/


  // *** Added to fix faction bug

    location lPClocation = GetLocation(oPC);
    object oInvItem = GetFirstItemInInventory(oPC);
    object oLootBag;
    int nYes;
    string sItemTag;


    while (GetIsObjectValid(oInvItem) == TRUE)
    {
        nYes = 0;
        sItemTag = "";
        sItemTag = GetTag(oInvItem);
        if(sItemTag == "FalseMark")
          nYes = 1;
        if(sItemTag == "MarkofOron")
          nYes = 1;
        if(sItemTag == "MarkofGamlee")
          nYes = 1;
        if(sItemTag == "MarkofMaro")
          nYes = 1;
        if(sItemTag == "MarkofZool")
          nYes = 1;
        if(nYes == 1) {
          oLootBag = CreateObject(OBJECT_TYPE_PLACEABLE, "pclootbag", lPClocation);
          AssignCommand(oLootBag, ActionTakeItem(oInvItem, oPC));
        }

        oInvItem = GetNextItemInInventory(oPC);
    }



    if (GetStandardFactionReputation(STANDARD_FACTION_COMMONER, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_COMMONER, 80, oPC);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_MERCHANT, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_MERCHANT, 80, oPC);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_DEFENDER, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_DEFENDER, 80, oPC);
    }

    AssignCommand(oPC, ClearAllActions());
    //DelayCommand(2.5, PopUpDeathGUIPanel(oPC, TRUE, TRUE, 0, sDeathGUIMessage));
	DelayCommand(2.5, PopUpGUIPanel(oPC,GUI_PANEL_PLAYER_DEATH));

}