// ************************************************
//      OnPlayerDeath Resurrection Script
// ************************************************
//
// There is no "respawn" in 1st Ed. rules; therefore, if
// the PC is killed, henchmen will seach their own (and then
// the PC's) inventory for a Resurrection spell.  If none can
// be found, the search continues for a Raise Dead spell.  If
// there is no way to raise the PC from the dead, the PopUp GUI
// is triggered so the PC can reload a saved game (note that the
// "Respawn" option is not possible...
//
void main()
{
object oPlayer = GetLastPlayerDied();
int nMaxHP = GetMaxHitPoints(oPlayer);
object oHench1 = GetHenchman(oPlayer, 1);
object oHench2 = GetHenchman(oPlayer, 2);
object oItem;
int nRodCharges;
int nRaisedPC = 0;

effect eRaiseVFX = EffectVisualEffect(VFX_IMP_RAISE_DEAD);
effect eRaise = EffectResurrection();
effect eHeal = EffectHeal(500);

// First determine if PC has 2nd Henchman with resurrection ability
if ((GetIsObjectValid(oHench2))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oHench2);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Rod of Resurrection
  if ((GetTag(oItem) == "NW_WMGMRD002")&&(nRaisedPC != 1))
   {
   nRodCharges = GetItemCharges(oItem);
   SetItemCharges(oItem, (nRodCharges - 5));
   AssignCommand(oHench2, ClearAllActions());
   DelayCommand(0.5, AssignCommand(oHench2, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(1.0, AssignCommand(oHench2, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE2, 1.0, 2.0)));
   DelayCommand(3.0, AssignCommand(oHench2, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE1, 1.0, 2.0)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(5.6, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Rod of Resurrection on-hand...")));
   nRaisedPC = 1;
   }
  // Check to see if oItem is Scroll of Resurrection
  if ((GetTag(oItem) == "NW_IT_SPDVSCR702")&&(nRaisedPC != 1))
   {
   DestroyObject(oItem, 5.0);
   AssignCommand(oHench2, ClearAllActions());
   DelayCommand(0.2, AssignCommand(oHench2, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(0.5, AssignCommand(oHench2, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(2.0, AssignCommand(oHench2, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(5.6, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Scroll of Resurrection on-hand...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oHench2);
  }
 // End resurrection actions for 2nd Henchman
 }

// If 2nd Henchman doesn't have ability to resurrect PC, try 1st Henchman
if ((GetIsObjectValid(oHench1))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oHench1);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Rod of Resurrection
  if ((GetTag(oItem) == "NW_WMGMRD002")&&(nRaisedPC != 1))
   {
   nRodCharges = GetItemCharges(oItem);
   SetItemCharges(oItem, (nRodCharges - 5));
   AssignCommand(oHench1, ClearAllActions());
   DelayCommand(0.5, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(1.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE2, 1.0, 2.0)));
   DelayCommand(3.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE1, 1.0, 2.0)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(5.6, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Rod of Resurrection on-hand...")));
   nRaisedPC = 1;
   }
  // Check to see if oItem is Scroll of Resurrection
  if ((GetTag(oItem) == "NW_IT_SPDVSCR702")&&(nRaisedPC != 1))
   {
   DestroyObject(oItem, 5.0);
   AssignCommand(oHench1, ClearAllActions());
   DelayCommand(0.2, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(0.5, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(2.0, AssignCommand(oHench1, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(5.6, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Scroll of Resurrection on-hand...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oHench1);
  }
 // End resurrection actions for 1st Henchman
 }

// If neither henchman have ability to resurrect PC, check PC's inventory...
if ((GetIsObjectValid(oPlayer))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oPlayer);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Rod of Resurrection
  if ((GetTag(oItem) == "NW_WMGMRD002")&&(nRaisedPC != 1))
   {
   nRodCharges = GetItemCharges(oItem);
   SetItemCharges(oItem, (nRodCharges - 5));
   AssignCommand(oHench1, ActionForceMoveToObject(oPlayer, TRUE));
   AssignCommand(oHench2, ActionForceMoveToObject(oPlayer, TRUE));
   DelayCommand(4.9, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(5.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW, 1.0, 1.0)));
   DelayCommand(6.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE2, 1.0, 2.0)));
   DelayCommand(8.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_CONJURE1, 1.0, 2.0)));
   DelayCommand(10.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(10.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(10.6, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(11.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, your henchman was able to use the Rod of Resurrection you were carrying...")));
   nRaisedPC = 1;
   }
  // Check to see if oItem is Scroll of Resurrection
  if ((GetTag(oItem) == "NW_IT_SPDVSCR702")&&(nRaisedPC != 1))
   {
   AssignCommand(oHench1, ActionForceMoveToObject(oPlayer, TRUE));
   AssignCommand(oHench2, ActionForceMoveToObject(oPlayer, TRUE));
   DestroyObject(oItem, 5.0);
   DelayCommand(4.9, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(5.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW, 1.0, 1.0)));
   DelayCommand(6.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(7.5, AssignCommand(oHench1, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(10.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(11.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(11.1, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oPlayer));
   DelayCommand(11.5, AssignCommand(oPlayer, ActionSpeakString("Luckily, your henchman was able to use the Scroll of Resurrection you were carrying...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oPlayer);
  }
 // End resurrection actions for PC's inventory
 }

// Nobody had the ability to resurrect the PC?!  Too bad...
// Now determine if PC has 2nd Henchman with raise dead ability
if ((GetIsObjectValid(oHench2))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oHench2);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Scroll of Raise Dead
  if ((GetTag(oItem) == "NW_IT_SPDVSCR501")||(GetTag(oItem) == "ScrollRaiseDead")&&(nRaisedPC != 1))
   {
   DestroyObject(oItem, 5.0);
   AssignCommand(oHench2, ClearAllActions());
   DelayCommand(0.2, AssignCommand(oHench2, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(0.5, AssignCommand(oHench2, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(2.0, AssignCommand(oHench2, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Scroll of Raise Dead on-hand...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oHench2);
  }
 // End raise dead actions for 2nd Henchman
 }

// If 2nd Henchman doesn't have ability to raise PC, try 1st Henchman
if ((GetIsObjectValid(oHench1))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oHench1);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Scroll of Raise Dead
  if ((GetTag(oItem) == "NW_IT_SPDVSCR501")||(GetTag(oItem) == "ScrollRaiseDead")&&(nRaisedPC != 1))
   {
   DestroyObject(oItem, 5.0);
   AssignCommand(oHench1, ClearAllActions());
   DelayCommand(0.2, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(0.5, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(2.0, AssignCommand(oHench1, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(5.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(5.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(6.0, AssignCommand(oPlayer, ActionSpeakString("Luckily, one of your henchman had a Scroll of Raise Dead on-hand...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oHench1);
  }
 // End raise dead actions for 1st Henchman
 }

// If neither henchman have ability to raise PC, check PC's inventory...
if ((GetIsObjectValid(oPlayer))&&(nRaisedPC != 1))
 {
 oItem = GetFirstItemInInventory(oPlayer);
 while ((GetIsObjectValid(oItem))&&(nRaisedPC != 1))
  {
  // Check to see if oItem is Scroll of Raise Dead
  if ((GetTag(oItem) == "NW_IT_SPDVSCR501")||(GetTag(oItem) == "ScrollRaiseDead")&&(nRaisedPC != 1))
   {
   AssignCommand(oHench1, ActionForceMoveToObject(oPlayer, TRUE));
   AssignCommand(oHench2, ActionForceMoveToObject(oPlayer, TRUE));
   DestroyObject(oItem, 5.0);
   DelayCommand(4.9, AssignCommand(oHench1, SetFacingPoint(GetPosition(oPlayer))));
   DelayCommand(5.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW, 1.0, 1.0)));
   DelayCommand(6.0, AssignCommand(oHench1, ActionPlayAnimation(ANIMATION_FIREFORGET_READ)));
   DelayCommand(7.5, AssignCommand(oHench1, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oPlayer, PROJECTILE_PATH_TYPE_DEFAULT)));
   DelayCommand(10.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaiseVFX, oPlayer));
   DelayCommand(11.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oPlayer));
   DelayCommand(11.5, AssignCommand(oPlayer, ActionSpeakString("Luckily, your henchman was able to use the Scroll of Raise Dead you were carrying...")));
   nRaisedPC = 1;
   }
  oItem = GetNextItemInInventory(oPlayer);
  }
 // End raise dead actions for PC's inventory
 }

// If NO-ONE has Resurrection OR Raise Dead ability, PC is dead forever - bummer!
// Show the PopUpDeathGUIPanel with the "Respawn" button disabled...
if (nRaisedPC != 1) DelayCommand(12.5, PopUpDeathGUIPanel(oPlayer,FALSE, TRUE, 66487));

return;
}
