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

    Generic Flaming Placeables OnDeath Event Script

       The Fire Box explodes in a cloud of red_hot coals dealing 3d6 damage to
       any creature, placeable, or door within 3.3 meters (10 feet).

       This script can easily be used for other placeables you wish to explode
       when they die - pyres, etc.

    Version: 1.0
    Created: 16 Jun 2022
    Author:  Pstemarie

*/
#include "prc_inc_spells"
#include "prc_add_spell_dc"
void main()
{
    //Check the "activated" state and abort if not active
    if (!GetLocalInt (OBJECT_SELF,"X2_L_PLC_ACTIVATED_STATE") ||
        !GetLocalInt(OBJECT_SELF,"NW_L_AMION") )
    {
        return;
    }

    //Declare major variables
    int nDamage;
    float fDelay;
    effect eDam;

    //Get the source location
    location lTarget = GetLocation(OBJECT_SELF);

    //Apply the fireball explosion at the location captured above.
    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_FIREBALL), lTarget);

    //Declare the spell shape, size and the location.  Capture the first target object in the shape.
    object oTarget = GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);

    //Cycle through the targets within the spell shape until an invalid object is captured.
    while (GetIsObjectValid(oTarget))
    {
        //Get the distance between the explosion and the target to calculate delay
        fDelay = GetDistanceBetweenLocations(lTarget, GetLocation(oTarget))/20;

        //Roll damage for each target
        nDamage = d6(3);

        //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
        eDam = EffectDamage(nDamage, DAMAGE_TYPE_FIRE);
        if(nDamage > 0)
        {
            // Apply effects to the currently selected target.
            DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oTarget));
            DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_FLAME_M), oTarget));
        }
        //Select the next target within the spell shape.
        oTarget = GetNextObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_MEDIUM, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);
    }
}
