local event = require 'utils.event' local type_blacklist = { ["ammo-turret"] = true, ["artillery-turret"] = true, ["electric-turret"] = true, ["fluid-turret"] = true } local function on_built_entity(event) local entity = event.created_entity if not entity.valid then return end if not type_blacklist[event.created_entity.type] then return end local surface = event.created_entity.surface local spawners = surface.find_entities_filtered({type = "unit-spawner", area = {{entity.position.x - 70, entity.position.y - 70}, {entity.position.x + 70, entity.position.y + 70}}}) if #spawners == 0 then return end local allowed_to_build = true for _, e in pairs(spawners) do if (e.position.x - entity.position.x)^2 + (e.position.y - entity.position.y)^2 < 4096 then allowed_to_build = false break end end if allowed_to_build then return end if event.player_index then game.players[event.player_index].insert({name = entity.name, count = 1}) else local inventory = event.robot.get_inventory(defines.inventory.robot_cargo) inventory.insert({name = entity.name, count = 1}) end surface.create_entity({ name = "flying-text", position = entity.position, text = "Turret too close to spawner!", color = {r=0.98, g=0.66, b=0.22} }) entity.destroy() end local function on_robot_built_entity(event) on_built_entity(event) end event.add(defines.events.on_robot_built_entity, on_robot_built_entity) event.add(defines.events.on_built_entity, on_built_entity)