//::///////////////////////////////////////////////
//:: Destruction
//:: NW_S0_Destruc
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    The target creature is destroyed if it fails a
    Fort save, otherwise it takes 10d6 damage.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Aug 13, 2001
//:://////////////////////////////////////////////

#include "x0_I0_SPELLS"    
#include "x2_inc_spellhook" 

void main()
{

/* 
  Spellcast Hook Code 
  Added 2003-06-23 by GeorgZ
  If you want to make changes to all spells,
  check x2_inc_spellhook.nss to find out more
  
*/

    if (!X2PreSpellCastCode())
    {
	// If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        return;
    }

// End of Spell Cast Hook


    //Declare major variables
    object oTarget = GetSpellTargetObject();
    int nMetaMagic = GetMetaMagicFeat();
    int nDamage;
    effect eDeath = EffectDeath();
    effect eDam;
    effect eVis = EffectVisualEffect(VFX_IMP_DESTRUCTION);
	if (spellsIsTarget(oTarget, SPELL_TARGET_STANDARDHOSTILE, OBJECT_SELF))
	{
        //Fire cast spell at event for the specified target
        SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_DESTRUCTION));
		
		//Make sure the target is not immune to death effects
		if (GetIsImmune(oTarget, IMMUNITY_TYPE_DEATH))
		{   
			FloatingTextStrRefOnCreature(184683, OBJECT_SELF, FALSE);
			return;
		}
		
		//Make SR check
        if(!MyResistSpell(OBJECT_SELF, oTarget))
        {
            //Make a saving throw check
            if(!/*Fort Save*/ MySavingThrow(SAVING_THROW_FORT, oTarget, GetSpellSaveDC(), SAVING_THROW_TYPE_DEATH))
            {
                //Apply the VFX impact and effects
                ApplyEffectToObject(DURATION_TYPE_INSTANT, eDeath, oTarget);
            }
            else
            {
				if (GetHasFeat(6818, oTarget)) //Mettle
				{
					return;
				}
				else
				{
	                nDamage = d6(10);
	                //Enter Metamagic conditions
	                if (nMetaMagic == METAMAGIC_MAXIMIZE)
	                {
	                	nDamage = 60;//Damage is at max
	                }
	                else if (nMetaMagic == METAMAGIC_EMPOWER)
	                {
	                	nDamage = nDamage + (nDamage/2); //Damage/Healing is +50%
	                }
	                //Set damage effect
	                eDam = EffectDamage(nDamage, DAMAGE_TYPE_DIVINE);
	                ApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oTarget);
				}
            }
            //Apply VFX impact
            ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget);
        }
    }
}