#include "../../config/events.lua"
#include "../helpers/entity.lua"

pCond = GetStringParam("cond", "")
pEvent = GetStringParam("event", "")
pIncludeVehicleParent = GetBoolParam("include_veh_parent", false)

function init()
    if pCond == "" then return end

    local q = pIncludeVehicleParent and "all vehicleparent" or "all"
    entities = cEntity:GetEntities(q)
    if pEvent ~= "" then
        RegisterListenerTo(EVENTS.SPAWN_SET, "Deleter")
    else
        Deleter()
        cEntity:EnqueueDeletion()
    end
end

function Deleter(data)
    if pEvent ~= "" and data ~= pEvent then return end

    local is_completed = loadstring("return "..pCond)()
    if not is_completed then return end

    for i = 1, #entities do
        Delete(entities[i])
    end
    cEntity:EnqueueDeletion()
end
