/*
    PROJECT Q IV v4.00
    (c) 2022 by Pstemarie

    Blazing Bones OnUserDefined Event Script

        Explodes on death with the effect of a 5d6 fireball

    Version: 1.0
    Created: 7 Jun 2022
    Author:  Pstemarie

*/

const int EVENT_USER_DEFINED_PRESPAWN = 1510;
const int EVENT_USER_DEFINED_POSTSPAWN = 1511;
#include "prc_inc_spells"
#include "prc_add_spell_dc"
//#include "X0_I0_SPELLS"

void main()
{
    int nUser = GetUserDefinedEventNumber();

    if (nUser == EVENT_DAMAGED)
    {
        int nDamage = GetDamageDealtByType(DAMAGE_TYPE_FIRE);
        if (nDamage > 0)
        {
            nDamage = nDamage *2;
            ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(nDamage), OBJECT_SELF);
            ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(VFX_IMP_FLAME_M), OBJECT_SELF);
        }
    }

    else if (nUser == 1007) // EVENT_DEATH
    {
        //Declar major variables
        float fDelay;
        int nDamage;
        effect eDamage;

        //Apply the blast VFX to the skeleton
        location lTarget = GetLocation(OBJECT_SELF);
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_FIREBALL), lTarget);

        object oTarget = GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_HUGE, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);
        while (GetIsObjectValid(oTarget))
        {
            if (spellsIsTarget(oTarget, SPELL_TARGET_STANDARDHOSTILE, OBJECT_SELF))
            {
                fDelay = GetDistanceBetweenLocations(lTarget, GetLocation(oTarget))/20;
                if (!PRCDoResistSpell(OBJECT_SELF, oTarget, FloatToInt(fDelay)))
                {
                    //Roll damage for each target
                    nDamage = d6(5);

                    //Adjust the damage based on the Reflex Save, Evasion and Improved Evasion
                    nDamage = PRCGetReflexAdjustedDamage(nDamage, oTarget, PRCGetSpellSaveDC(), SAVING_THROW_TYPE_FIRE);

                    //Set the damage effect
                    eDamage = EffectDamage(nDamage, DAMAGE_TYPE_FIRE);
                    if(nDamage > 0)
                    {
                        //Apply effects to the currently selected target
                        DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oTarget));
                        DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_FLAME_M), oTarget));
                    }
                 }

            }
            //Get the next target within the blast radius
            oTarget = GetNextObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_HUGE, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);
        }
    }
}
