//Created by Guile  12/31/07
//Goes OnPlayerRespawn of the module properties
effect eEffect;
location lTarget;
int nInt;
object oTarget;

#include "nw_i0_plot"

//Void function to tell the functions below how to appropriately remove xp.
void RemoveXPFromParty(int nXP, object oPC, int bAllParty=TRUE)
{
if (!bAllParty)
   {
   nXP=(GetXP(oPC)-nXP)>=0 ? GetXP(oPC)-nXP : 0;
   SetXP(oPC, nXP);
   }
else
   {
   object oMember=GetFirstFactionMember(oPC, TRUE);

   while (GetIsObjectValid(oMember))
      {
      nXP=(GetXP(oMember)-nXP)>=0 ? GetXP(oMember)-nXP : 0;
      SetXP(oMember, nXP);
      oMember=GetNextFactionMember(oPC, TRUE);
      }
   }
}
//Main Script
void main()
{
//This script may seem very complex, but it's not, each function is noted
//so there is no confusion on what each thing does.
//You can simply void a function by typing // before the line, all functions
//<<<like so are uncommented and highlighted in green, as they don't work at all.
object oPC = GetLastRespawnButtonPresser();
if (!GetIsPC(oPC)) return;
//On Player Respawn, they cannot die for 15 seconds, though they still take damage.
SetPlotFlag(oPC, TRUE);
//After 15 Seconds make them killable.
DelayCommand(15.0, SetPlotFlag(oPC,FALSE));
//Resurrect the player
ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectResurrection(),oPC);
ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectHeal(GetMaxHitPoints(oPC)), oPC);
//Commented out because a bugg was found removing all effects from PC.
//RemoveEffects(oPC);

//Play a cool effect when the player respawns
object oTarget;
oTarget = oPC;
int nInt;
nInt = GetObjectType(oTarget);

if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_TORNADO), oTarget);
else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_TORNADO), GetLocation(oTarget));

//If the player is in the arena and has this token they respawn in arena
//With no death penalty what soever, and you don't gain xp as a reward either.
if (GetItemPossessedBy(oPC, "arenatoken")!= OBJECT_INVALID)
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), GetLocation(oTarget));
//Here you will want to create a Way Point and give it the tagname below.
//This is where the player who died in the arena only will respawn to.
   oTarget = GetWaypointByTag("arenaway1");

   lTarget = GetLocation(oTarget);

   if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

   AssignCommand(oPC, ClearAllActions());

   AssignCommand(oPC, ActionJumpToLocation(lTarget));

   oTarget = oPC;

   eEffect = EffectCutsceneImmobilize();

   eEffect = SupernaturalEffect(eEffect);

   ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eEffect, oTarget, 4.0f);

   FloatingTextStringOnCreature("The Gods have restored you to the arena.", oPC);


    object oItem;
    oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }

   }
//You will want to give your low level players a plot item, which will
//allow them to respawn where they died, with a cool effect telling them
//why they were ressurected magically.
//Note if you don't hand out this item, they will respawn normally
//This could also be used as a reward for immortality as well!
else if (GetItemPossessedBy(oPC, "plotitemtagname")!= OBJECT_INVALID)
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_NATURES_BALANCE), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_NATURES_BALANCE), GetLocation(oTarget));

   FloatingTextStringOnCreature("The magic of the lands has restored you to life!", oPC);

   RemoveXPFromParty(50, oPC, FALSE);

   AssignCommand(oPC, TakeGoldFromCreature(1000, oPC, TRUE));

   object oItem;
    oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }

   }
//No matter what happens, they are going to lose xp/gold, unless in arena.
else if (GetHitDice(oPC) >= 40)
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), GetLocation(oTarget));

   oTarget = GetWaypointByTag("home");

   lTarget = GetLocation(oTarget);

   if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

   DelayCommand(3.0, AssignCommand(oPC, ClearAllActions()));

   DelayCommand(3.3, AssignCommand(oPC, ActionJumpToLocation(lTarget)));

   AssignCommand(oPC, TakeGoldFromCreature(50000, oPC, TRUE));

   object oItem;
    oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }


   }
else if (GetHitDice(oPC) >= 30)
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), GetLocation(oTarget));

   RemoveXPFromParty(5000, oPC, FALSE);

   AssignCommand(oPC, TakeGoldFromCreature(50000, oPC, TRUE));

   oTarget = GetWaypointByTag("home");

   lTarget = GetLocation(oTarget);

   if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

   DelayCommand(2.0, AssignCommand(oPC, ClearAllActions()));

   DelayCommand(2.2, AssignCommand(oPC, ActionJumpToLocation(lTarget)));

   object oItem;
oItem = GetFirstItemInInventory(oPC);

while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }

   }
else if (GetHitDice(oPC) >= 20)
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), GetLocation(oTarget));

   RemoveXPFromParty(3000, oPC, FALSE);

   AssignCommand(oPC, TakeGoldFromCreature(30000, oPC, TRUE));

   oTarget = GetWaypointByTag("home");

   lTarget = GetLocation(oTarget);

   if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

   DelayCommand(2.0, AssignCommand(oPC, ClearAllActions()));

   DelayCommand(2.2, AssignCommand(oPC, ActionJumpToLocation(lTarget)));

   object oItem;
oItem = GetFirstItemInInventory(oPC);

while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }

   }
else
   {
   oTarget = oPC;

   nInt = GetObjectType(oTarget);

   if (nInt != OBJECT_TYPE_WAYPOINT) ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), oTarget);
   else ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY), GetLocation(oTarget));

   RemoveXPFromParty(1000, oPC, FALSE);

   AssignCommand(oPC, TakeGoldFromCreature(5000, oPC, TRUE));

   oTarget = GetWaypointByTag("home");

   lTarget = GetLocation(oTarget);

   if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

   DelayCommand(2.0, AssignCommand(oPC, ClearAllActions()));

   DelayCommand(2.2, AssignCommand(oPC, ActionJumpToLocation(lTarget)));

   object oItem;
oItem = GetFirstItemInInventory(oPC);

while (GetIsObjectValid(oItem))
   {
   if (GetTag(oItem)=="death") DestroyObject(oItem);

   oItem = GetNextItemInInventory(oPC);
   }

   }
}