//::///////////////////////////////////////////////
//:: Mass Inflict Light Wounds (WAS: [Circle of Doom])
//:: [NW_S0_MaInfMod.nss]
//:://////////////////////////////////////////////
//:: All enemies of the caster take 2d8 damage +1
//:: per caster level (max 30).  Undead are healed
//:: for the same amount
//:://////////////////////////////////////////////
//:: Created By: Jesse Reynolds (JLR - OEI)
//:: Created On: August 01, 2005
//:://////////////////////////////////////////////
//:: VFX Pass By: Preston W, On: June 20, 2001
//:: Update Pass By: Preston W, On: July 25, 2001

// JLR - OEI 08/23/05 -- Metamagic changes
//2d2f - 05-08-08 added zombified surge support
#include "nwn2_inc_spells"


#include "X0_I0_SPELLS"
#include "jx_inc_magic"
#include "x2_inc_spellhook" 

void mainSpell();
void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{

/* 
  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())
    {
	// If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        { JXPostSpellCastCode(); return; }
    }

// End of Spell Cast Hook


    //Declare major variables
    object oTarget;
    effect eDam;
    effect eHeal;
    int nCasterLevel = JXGetCasterLevel(OBJECT_SELF);
	effect eVisInflict = EffectVisualEffect(VFX_HIT_SPELL_INFLICT_3);
    //Limit Caster Level
    if(nCasterLevel > 30)
    {
        nCasterLevel = 30;
    }
    int nMetaMagic = JXGetMetaMagicFeat();
    int nDamage;
    float fDelay;
    //Get first target in the specified area
    oTarget =GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, JXGetSpellTargetLocation());
    while (GetIsObjectValid(oTarget))
    {
        fDelay = GetRandomDelay();
        //Roll damage
        nDamage = d8(2) + nCasterLevel;
        //Make metamagic checks
        nDamage = ApplyMetamagicVariableMods(nDamage, 16 + nCasterLevel);

         int nZombified = GetLocalInt(oTarget,"nZombified");	
        //If the target is an allied undead it is healed
        if(GetRacialType(oTarget) == RACIAL_TYPE_UNDEAD || nZombified > 0)
        {
			if(spellsIsTarget(oTarget, SPELL_TARGET_ALLALLIES, OBJECT_SELF))
        	{
            	//Fire cast spell at event for the specified target
            	SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, JXGetSpellId(), FALSE));
            	//Set the heal effect
            	eHeal = EffectHeal(nDamage);
            	//Apply the impact VFX and healing effect
            	DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oTarget));
            	DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVisInflict, oTarget));
        	}
			else
			{
				FloatingTextStrRefOnCreature(184683, OBJECT_SELF, FALSE);
				{ JXPostSpellCastCode(); return; }
			}
        }
        else
        {
           if (spellsIsTarget(oTarget, SPELL_TARGET_STANDARDHOSTILE, OBJECT_SELF))
           {
                //Fire cast spell at event for the specified target
                SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, JXGetSpellId()));
                //Make an SR Check
                if (!MyResistSpell(OBJECT_SELF, oTarget, fDelay))
                {
                    if (MySavingThrow(SAVING_THROW_WILL, oTarget, JXGetSpellSaveDC(), SAVING_THROW_TYPE_NEGATIVE, OBJECT_SELF, fDelay))
                    {
                        nDamage = nDamage/2;
                    }
                    //Set Damage
                    eDam = EffectDamage(nDamage, DAMAGE_TYPE_NEGATIVE);
                    //Apply impact VFX and damage
                    DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oTarget));
                    DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVisInflict, oTarget));
                }
            }
        }
        //Get next target in the specified area
        oTarget = GetNextObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, JXGetSpellTargetLocation());
    }

	JXPostSpellCastCode();
}