void ApplyEffectAfterDelay(float fDelay, effect eEffect, object oTarget)
{
    DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eEffect, oTarget, fDelay + 0.5));
}

void main()
{
    // The creature that is dying
    object oCreature = OBJECT_SELF;

    // Define the radius and damage parameters
    float fRadius = RADIUS_SIZE_LARGE;
    int nDC = 50; // Difficulty Class for the saving throw
    int nDamageType = DAMAGE_TYPE_ACID;

    // Get the location of the dying creature
    location lOrigin = GetLocation(oCreature);

    // Find the nearest PC in the vicinity
    object oNearestPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, oCreature);

    // If there's a PC nearby, display the floating text above them
    if (GetIsObjectValid(oNearestPC))
    {
        FloatingTextStringOnCreature("[The creature explodes in a mass of gore and acid!]", oNearestPC, TRUE);
    }
    else
    {
        // If no PC is nearby, fallback to the creature's location
        object oFallbackTarget = GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_SMALL, lOrigin, TRUE, OBJECT_TYPE_CREATURE);
        if (GetIsObjectValid(oFallbackTarget))
        {
            FloatingTextStringOnCreature("[The creature explodes in a mass of gore and acid!]", oFallbackTarget, TRUE);
        }
    }

    // Apply damage to each hostile creature within the radius
    object oTarget = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lOrigin, TRUE, OBJECT_TYPE_CREATURE);

    while (GetIsObjectValid(oTarget))
    {
        // Check if the target is hostile to the dying creature
        if (GetIsReactionTypeHostile(oTarget, oCreature))
        {
            // Check for evasion or improved evasion
            int nEvasion = GetHasFeat(FEAT_EVASION, oTarget) ? 1 : GetHasFeat(FEAT_IMPROVED_EVASION, oTarget) ? 2 : 0;
            int nSave = ReflexSave(oTarget, nDC, SAVING_THROW_TYPE_ACID);

            int nDamage = 0;
            if (nEvasion == 0 || (nEvasion == 1 && nSave != 0) || (nEvasion == 2 && nSave == 0))
            {
                nDamage = d8(20); // Rolls 20d8 for damage
            }
            else if (nEvasion == 2)
            {
                nDamage = d8(20) / 2; // Half damage for improved evasion
            }

            if (nDamage > 0)
            {
                effect eDamage = EffectDamage(nDamage, nDamageType);
                ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oTarget);
            }
        }

        oTarget = GetNextObjectInShape(SHAPE_SPHERE, fRadius, l
