#include "nw_i0_plot"

// Applies an XP and GP penalty
// to the player respawning
void ApplyPenalty(object oDead)
{
    int nXP = GetXP(oDead);
    int nPenalty;
    int nHD = GetHitDice(oDead);
    // 50 XP/level for levels 1-5, then 200 XP/level
    if (nHD < 6)
      nPenalty = 50 * nHD;
    else
      nPenalty = 200 * nHD;
    // You can not lose a level with this respawning
    int nMin = ((nHD * (nHD - 1)) / 2) * 1000;

    int nNewXP = nXP - nPenalty;
    if (nNewXP < nMin)
       nNewXP = nMin;
    SetXP(oDead, nNewXP);
    int nGoldToTake =    FloatToInt(0.10 * GetGold(oDead));
    // a cap of 10,000gp taken from you
    if (nGoldToTake > 10000)
    {
        nGoldToTake = 10000;
    }
    AssignCommand(oDead, TakeGoldFromCreature(nGoldToTake, oDead, TRUE));
    DelayCommand(4.0, FloatingTextStrRefOnCreature(58299, oDead, FALSE));
    DelayCommand(4.8, FloatingTextStrRefOnCreature(58300, oDead, FALSE));

}


void main()
{
    object oRespawner = GetLastRespawnButtonPresser();
    ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectResurrection(),oRespawner);
    ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectHeal(GetMaxHitPoints(oRespawner)), oRespawner);
    RemoveEffects(oRespawner);

    // Return PC to temple
    string sDestTag =  "NW_DEATH_TEMPLE";
    string sArea = GetTag(GetArea(oRespawner));

    // Dungeons of the Slave Lords special case
    if ((sArea == "DungeonBridge") || (sArea == "DungeonCenter") ||
        (sArea == "DungeonChasm") || (sArea == "DungeonFungus") ||
        (sArea == "DungeonMushrooms") || (sArea == "DungeonSmoothCaves"))
      sDestTag = "WP_Dungeon";

    // Slaver Island Destroyed special case
    if (sArea == "SlaverIslandDestroyed")
      sDestTag = "WP_DungeonEntrance";

    if (GetIsObjectValid(GetObjectByTag(sDestTag)))
    {

        if (sDestTag == "NW_DEATH_TEMPLE")
        {
            object oPriest = GetObjectByTag("NW_DEATH_CLERIC");
            //SetLocalInt(oPriest, "NW_L_SAYONELINER", 10);

            //AssignCommand(oPriest, DelayCommand(3.0,ActionStartConversation(oRespawner)));
            AssignCommand(oPriest, DelayCommand(2.1, PlayVoiceChat(VOICE_CHAT_TALKTOME, oPriest)));

            SetLocalLocation(oRespawner, "NW_L_I_DIED_HERE", GetLocation(GetLastRespawnButtonPresser()));
            SetLocalInt(oRespawner, "NW_L_I_DIED", 1);
            SetLocalObject(oPriest, "NW_L_LASTDIED", GetLastRespawnButtonPresser());
        }
        ApplyPenalty(oRespawner);
        object oSpawnPoint = GetObjectByTag(sDestTag);
        AssignCommand(oRespawner,JumpToLocation(GetLocation(oSpawnPoint)));
    }
    else
    {
        // do nothing, just 'res where you are.
    }
 }
