//::///////////////////////////////////////////////
//:: Acid Fog: Heartbeat
//:: NW_S0_AcidFogC.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    All creatures within the AoE take 2d6 acid damage
    per round and upon entering if they fail a Fort Save
    their movement is halved.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: May 17, 2001
//:://////////////////////////////////////////////

#include "X0_I0_SPELLS"

void main()
{

    //Declare major variables
    int nMetaMagic = JXGetMetaMagicFeat();
    int nDamage = d6(2);
    effect eDam;
    effect eVis = EffectVisualEffect(VFX_HIT_SPELL_ACID);
    object oTarget;
    float fDelay;

   //--------------------------------------------------------------------------
    // GZ 2003-Oct-15
    // When the caster is no longer there, all functions calling
    // JXGetAOERealCreator will fail. Its better to remove the barrier then
    //--------------------------------------------------------------------------
    if (!GetIsObjectValid(JXGetAOERealCreator()))
    {
        DestroyObject(OBJECT_SELF);
		JXPostSpellCastCode();
        return;
    }


    //Start cycling through the AOE Object for viable targets including doors and placable objects.
    oTarget = GetFirstInPersistentObject(OBJECT_SELF);
    while(GetIsObjectValid(oTarget))
    {
        if (spellsIsTarget(oTarget, SPELL_TARGET_STANDARDHOSTILE, JXGetAOERealCreator()))
        {
            /*if(!MySavingThrow(SAVING_THROW_FORT, oTarget, JXGetSpellSaveDC(), SAVING_THROW_TYPE_ACID, JXGetAOERealCreator(), fDelay))
            {
                 nDamage = d6();
            }*/

            nDamage = d6(2);

        //Enter Metamagic conditions
        if (nMetaMagic & METAMAGIC_MAXIMIZE)
        {
            nDamage = 12;//Damage is at max
        }
        if (nMetaMagic & METAMAGIC_EMPOWER)
        {
            nDamage = nDamage + (nDamage/2); //Damage/Healing is +50%
        }

        //Set the damage effect
        eDam = EffectDamage(nDamage, DAMAGE_TYPE_ACID);

            fDelay = GetRandomDelay(0.4, 1.2);
            //Fire cast spell at event for the affected target
            SignalEvent(oTarget, EventSpellCastAt(JXGetAOERealCreator(), SPELL_ACID_FOG));
            //Spell resistance check
            if(!MyResistSpell(JXGetAOERealCreator(), oTarget, fDelay))
            {
               //Apply damage and visuals
                DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oTarget));
                DelayCommand(fDelay, JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget));
            }
        }
        //Get next target.
        oTarget = GetNextInPersistentObject(OBJECT_SELF);
    }
}