// v2.2.0 - Increased Rez Penalty delay.
// v2.0.0 -
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  hark_o0_pct
//::////////////////////////////////////////////////////////////////////////////
/*
   Henchman Activate PCT Script.
*/
//::////////////////////////////////////////////////////////////////////////////
#include "hark_i0_henlvl"
//::////////////////////////////////////////////////////////////////////////////

const int TRUE_RESS = 3;
const int RESS      = 2;
const int RAISE     = 1;
const int NONE      = 0;

//::////////////////////////////////////////////////////////////////////////////
void main()
{
  object oMod = GetModule();
  object oUser = OBJECT_SELF;
  object oRealMaster = GetRealMaster(oUser);
  object oCleric = GetLocalObject(oUser, "CLERIC");
  object oRespawner = GetLocalObject(oUser, "DEADMAN");
  object oItem = GetLocalObject(oUser, "ITEM");
  DeleteLocalObject(oUser, "CLERIC");
  DeleteLocalObject(oUser, "DEADMAN");
  DeleteLocalObject(oUser, "ITEM");
  int nCastType = NONE;
  int nLevel = GetLevelByClass(CLASS_TYPE_CLERIC, oCleric);
  string sVarName = HS_REZ_REFUSED + GetTag(oUser);

  // Make sure the cleric is atleast high enough in levels to cast Raise Dead.
  if (nLevel < 9)
    {
     // If using Feedback, give it.
     if (GetLocalInt(GetObjectByTag(HS_VARS_OBJ), HS_VAR_REZ_FDB))
       { SendMessageToPC(oRealMaster, GetName(oUser) + ": " + GetName(oCleric) + REZ_MSG14); }
     SetLocalInt(oCleric, sVarName, TRUE);
     AssignCommand(oMod, DelayCommand(300.0, DeleteLocalInt(oCleric, sVarName)));
     return;
    }

  // Make an alignment check for rez.
  int nAlign = GetLocalInt(oItem, "Alignment");
  if (nAlign != ALIGNMENT_NEUTRAL && GetAlignmentGoodEvil(oCleric) != ALIGNMENT_NEUTRAL)
    {
     if (GetAlignmentGoodEvil(oCleric) != nAlign)
       {
        // If using Feedback, give it.
        if (GetLocalInt(GetObjectByTag(HS_VARS_OBJ), HS_VAR_REZ_FDB))
          { SendMessageToPC(oRealMaster, GetName(oUser) + ": " + GetName(oCleric) + REZ_MSG15); }
        SetLocalInt(oCleric, sVarName, TRUE);
        AssignCommand(oMod, DelayCommand(300.0, DeleteLocalInt(oCleric, sVarName)));
        return;
       }
    }

  // Determine which type of rez is available.
  int nGold = GetGold(oUser);
  int nCost1 = 50 * nLevel + 500;
  int nCost2 = 70 * nLevel + 500;
  int nCost3 = 90 * nLevel + 5000;
  int nRezAmount;
  if (nLevel >= 17 && nGold >= nCost3)
    {
     nRezAmount = nCost3;
     nCastType = TRUE_RESS;
    }
  else if (nLevel >= 13 && nGold >= nCost2)
    {
     nRezAmount = nCost2;
     nCastType = RESS;
    }
  else if (nLevel >= 9 && nGold >= nCost1)
    {
     nRezAmount = nCost1;
     nCastType = RAISE;
    }

  // If we dont have enough gold.
  if (nCastType == NONE)
    {
     nRezAmount = nCost1 - nGold;
     if (GetLocalInt(GetObjectByTag(HS_VARS_OBJ), HS_VAR_REZ_FDB))
       { SendMessageToPC(oRealMaster, GetName(oUser) + ": " + GetName(oCleric) + REZ_MSG16 + IntToString(nRezAmount) + REZ_MSG18); }
     //SetLocalInt(oCleric, sVarName, TRUE);
     //AssignCommand(oMod, DelayCommand(300.0, DeleteLocalInt(oCleric, sVarName)));
     SetLocalInt(oUser, HS_REZ_NEED_GOLD, TRUE);
     SetLocalInt(oUser, HS_REZ_GOLD_NEED, nRezAmount);
     return;
    }

  // Henchman has the gold so charge them and raise the dead man.
  TakeGoldFromCreature(nRezAmount, oUser, TRUE);

  // If using Feedback, give it.
  if (GetLocalInt(GetObjectByTag(HS_VARS_OBJ), HS_VAR_REZ_FDB))
    { SendMessageToPC(oRealMaster, GetName(oUser) + ": " + GetName(oCleric) + REZ_MSG17 + IntToString(nRezAmount) + REZ_MSG18); }

  // Show some animation.
  AssignCommand(oCleric, ActionCastFakeSpellAtObject(SPELL_RESURRECTION, oCleric));

  if (GetIsObjectValid(oRespawner))
    {
     // Clear the Plot Flag and set the player state.
     SetPlotFlag(oRespawner, FALSE);
     SPS(oRespawner, PWS_PLAYER_STATE_ALIVE);

     // Remove effects, raise and heal as appropriate.
     effect eRez  = EffectResurrection();
     effect eHeal = EffectHeal(GetMaxHitPoints(oRespawner));
     effect eDam  = EffectDamage(GetCurrentHitPoints(oRespawner) - 1);
     DelayCommand(0.1, RemoveEffects(oRespawner));
     DelayCommand(0.2, ApplyEffectToObject(DURATION_TYPE_INSTANT, eRez, oRespawner));
     if (nCastType >= RESS)
       { DelayCommand(0.3, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oRespawner)); }
     else
       { DelayCommand(0.3, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oRespawner)); }
     DelayCommand(0.4, AssignCommand(oRespawner, JumpToObject(oUser)));

     // Apply the Rez Penalty if appropriate.
     if (nCastType != TRUE_RESS && GetLocalInt(oMod, "REZPENALTY"))
       { DelayCommand(3.0, hcRezPenalty(oRespawner)); }

     // Attempt to speak with the PC so we can return to work.
     DelayCommand(5.0, PersistentConversationAttempt(oRespawner));
    }
  else
    {
     if (GetLocalInt(oMod, "PERSIST"))
       {
        string sID = GetLocalString(oItem, "PKey");
        SetCampaignInt("HCRPC" + sID, "PlayerState", 9);
       }

     string sName = GetLocalString(oItem, "Name");
     string sKey  = GetLocalString(oItem, "Key");
     StoreLocation(oMod, "RESLOC" + sName + sKey, GetLocation(oUser));
     if (nCastType == TRUE_RESS)
       { StoreInt(oMod, "PlayerState" + sName + sKey, PWS_PLAYER_STATE_RESTRUE); }
     else if (nCastType == RESS)
       { StoreInt(oMod, "PlayerState" + sName + sKey, PWS_PLAYER_STATE_RESURRECTED); }
     else
       { StoreInt(oMod, "PlayerState" + sName + sKey, PWS_PLAYER_STATE_RAISEDEAD); }
    }

  // Now that our master is back, clear all the variables.
  HenchRezClear(oRespawner, oUser);

  // Remove the PCT and Death Corpse.
  DestroyObject(oItem);
  DestroyCorpse(oRespawner);
}
//::////////////////////////////////////////////////////////////////////////////
