dofile( "data/scripts/lib/utilities.lua" )

local theEntity = GetUpdatedEntityID()
local x, y, r = EntityGetTransform(theEntity)

local components = EntityGetComponent( theEntity, "VariableStorageComponent" )
local projectileComponent = EntityGetFirstComponent( theEntity, "ProjectileComponent" )
if(projectileComponent ~= nil)then
    local is_explosive = false
    local explosion_radius = tonumber(ComponentObjectGetValue(projectileComponent, "config_explosion", "explosion_radius"))
    local explosion_damage = ComponentObjectGetValue(projectileComponent, "config_explosion", "damage")

    local explosion1 = ComponentGetValue(projectileComponent, "on_lifetime_out_explode")

    local explosion2 = ComponentGetValue(projectileComponent, "on_death_explode")


    if(explosion_radius < 3)then
        explosion_radius = 3
    end

    if( components ~= nil and tonumber(explosion1) == 1 or components ~= nil and tonumber(explosion2) == 1) then
        for key,comp_id in pairs(components) do 
            local var_name = ComponentGetValue( comp_id, "name" )
            if( var_name == "projectile_to_launch") then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                local newEntity = EntityLoad(value, x, y)
                local who_shot = ComponentGetValue( projectileComponent, "mWhoShot" )

                GameShootProjectile( who_shot, x, y, 0, 0, newEntity )
                edit_component( newEntity, "ProjectileComponent", function(comp,vars)
                    vars.mWhoShot       = component_get_value_int( theEntity, "ProjectileComponent", "mWhoShot", 0 )
                    vars.mShooterHerdId = component_get_value_int( theEntity, "ProjectileComponent", "mShooterHerdId", 0 )
                    ComponentObjectSetValue(comp, "config_explosion", "explosion_radius", explosion_radius)
                end)
                
            end
        end
    end
end