I've tries several different syntax, but can't figure it out...
Control.lua code:
Code: Select all
require "defines"
require "util"
---------------------------------------------
script.on_event(defines.events.on_robot_built_entity, function(event) On_Built(event) end)
script.on_event(defines.events.on_built_entity, function(event) On_Built(event) end)
script.on_event({defines.events.on_entity_died,defines.events.on_robot_pre_mined_item,defines.events.on_preplayer_mined_item,},function(event) On_Remove(event) end)
---------------------------------------------
function On_Built(event)
--- Bio Farm has been built
if event.created_entity.name == "bf_bio_farm" then
local surface = game.surfaces['nauvis'] -- Old Code, need to fix
event.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
--game.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
--game.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
--event.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
--event.created_entity.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
end
end
---------------------------------------------
function On_Remove(event)
--- Bio Farm has been removed
if event.created_entity.name == "bf_bio_farm" then
res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm_light", area=GetArea(event.created_entity.position, 0.5)}
if #res then
-- If we've found it, destroy it.
res[1].destroy()
end
end
--- Bio Farm's Light Source has been removed
if event.created_entity.name == "bf_bio_farm_light" then
res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm", area=GetArea(event.created_entity.position, 0.5)}
if #res then
-- If we've found it, destroy it.
res[1].destroy()
end
end
end
function GetArea(pos, radius)
-- This calculates a box of the given radius around the given position.
return {{x = pos.x - radius, y = pos.y - radius}, {x = pos.x + radius, y = pos.y + radius}}
end