///::///////////////////////////////////////////////
//:: [Ressurection]
//:: [NW_S0_Ressurec.nss]
//:: Copyright (c) 2000 Bioware Corp.
//:://////////////////////////////////////////////
//:: Brings a character back to life with full
//:: health.
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
////:: Created On: Jan 31, 2001
//:://////////////////////////////////////////////
//:: Last Updated By: Preston Watamaniuk, On: April 11, 2001
//:: VFX Pass By: Preston W, On: June 22, 2001

#include "rpo_inc"
#include "jw_xp_penalty"

#include "x0_i0_spells"
#include "x2_inc_spellhook"

void main()
{

    //--------------------------------------------------------------------------
    /*
      Spellcast Hook Code
      Added 2003-06-20 by Georg
      If you want to make changes to all spells,
      check x2_inc_spellhook.nss to find out more
    */
    //--------------------------------------------------------------------------
    if (!X2PreSpellCastCode())
    {
        return;
    }
    // End of Spell Cast Hook
    if (GetIsInCombat(OBJECT_SELF))
    {
     SendMessageToPC(OBJECT_SELF,"You need peace and quiet to concentrate while casting this spell. You cannot cast it in combat, or while party members are distratcing you");
     return;
    }

    //Get the spell target
    object oTarget = GetSpellTargetObject();
    //Check to make sure the target is dead first
    if (GetIsDead(oTarget))
    {
       effect eVis = EffectVisualEffect(VFX_IMP_RAISE_DEAD);
        if (GetLocalInt(oTarget,"cantres"))
         {
             SendMessageToPC(oTarget,"The ressurection spell fails");
             FloatingTextStringOnCreature("The ressurection spell fails",OBJECT_SELF,TRUE);
             ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVis, GetLocation(oTarget));
             return;
         }


        //Fire cast spell at event for the specified target
        SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_RESURRECTION, FALSE));
        //Declare major variables
        int nHealed = GetMaxHitPoints(oTarget);
        effect eRaise = EffectResurrection();
        effect eHeal = EffectHeal(nHealed + 10);


        //Apply the heal, raise dead and VFX impact effect
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eRaise, oTarget);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oTarget);
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVis, GetLocation(oTarget));


    object oDead=oTarget;
    int nPenalty=30;
    int nXP = GetXP(oDead);
    nPenalty = nPenalty * GetHitDice(oDead);
    int nHD = GetHitDice(oDead);

    // * 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;
       }



    if (nHD==1)
    {
     nNewXP = (nXP-10);


    }

     if (nNewXP < (nXP/10*9))
       {
       nNewXP = (nXP/10*9);
       }


 if (nNewXP < 1)
       {
       nNewXP = 1;
       }


    SetXP(oDead, nNewXP);
  if (GetSpellCastItem() != OBJECT_INVALID)
  {
  effect eStr=EffectAbilityDecrease(ABILITY_STRENGTH,2);
    effect eDex=EffectAbilityDecrease(ABILITY_DEXTERITY,2);
       effect eWis=EffectAbilityDecrease(ABILITY_WISDOM,2);
          effect eInt=EffectAbilityDecrease(ABILITY_INTELLIGENCE,2);
           effect eChar=EffectAbilityDecrease(ABILITY_CHARISMA,2);
 effect eLink=EffectLinkEffects(eStr,eDex);
   eLink=EffectLinkEffects(eWis,eLink);
     eLink=EffectLinkEffects(eInt,eLink);
      eLink=EffectLinkEffects(eChar,eLink);

effect eSpeed=EffectMovementSpeedDecrease(20);
effect eCurse=ExtraordinaryEffect(eLink);
eSpeed=ExtraordinaryEffect(eSpeed);
ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eCurse,oDead,450.0);
ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eSpeed,oDead,200.0);
 }
  RemoveDeathAmulet(oTarget);
    }
}