//::///////////////////////////////////////////////
//:: Throw Rotten Fruit
//:: throw_rotten_fruit
//:: Script to throw rotten fruit, creating a visual effect without causing damage.
//:://////////////////////////////////////////////

#include "X2_I0_SPELLS"

const int VFX_IMPACT_EFFECT = VFX_COM_CHUNK_GREEN_MEDIUM;  // The impact VFX
const int VFX_LINGER_EFFECT = VFX_DUR_FLIES;               // The lingering flies VFX
const float LINGER_DURATION = 10.0;                         // Duration in seconds

void main()
{
    // Create the VFX for the impact
    effect eImpact = EffectVisualEffect(VFX_IMPACT_EFFECT);
    
    // Create the VFX for the lingering flies
    effect eLinger = EffectVisualEffect(VFX_LINGER_EFFECT);

    // Get the target of the throw action
    object oTarget = GetSpellTargetObject();
    int nTarget = GetObjectType(oTarget);

    // Check if the target is a creature, door, or placeable
    if (nTarget == OBJECT_TYPE_CREATURE || nTarget == OBJECT_TYPE_DOOR || nTarget == OBJECT_TYPE_PLACEABLE)
    {
        // Valid target, apply the impact VFX
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eImpact, oTarget);

        // Apply the lingering VFX for the flies
        ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLinger, oTarget, LINGER_DURATION);
    }
    else
    {
        // No valid target, apply the VFX to the ground
        location lTargetLocation = GetSpellTargetLocation();
        
        // Apply the impact VFX at the location
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eImpact, lTargetLocation);
        
        // Apply the lingering VFX for the flies at the location
        ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eLinger, lTargetLocation, LINGER_DURATION);
    }
}
