This is what I have so far:
Code: Select all
local radius = 50
local maxRobotsCharged = 10
local chargeRate = 10
function UpdatePoles(event)
for _,poleTable in pairs(global.robotChargerPoles) do
pole = poleTable[0]
chargingRobots = poleTable[1]
--Remove any robots that are no longer valid
robotsToRemove = {}
for _,robot in pairs(chargingRobots) do
if robot.valid == false then
table.insert(robotsToRemove, robot)
end
end
for _,robot in pairs(robotsToRemove) do
table.remove(chargingRobots,robot)
end
--Only maxRobotsCharged number of robots charging at a time
if #chargingRobots < maxRobotsCharged then
length = #chargingRobots
foundEntities = game.get_surface(1).find_entities{{-radius, -radius}, {radius, radius}}
for _,entity in pairs(foundEntities) do
--Find any sort of robot. Examples of robot names: "logistic-robot" "construction-robot" "logistic-robot-mark2" etc.
if string.find(entity.name, "robot") ~= nil then
if string.find(entity.name, "logistic") ~= nil or string.find(entity.name, "construction") ~= nil then
table.insert(chargingRobots, entity)
length = length + 1
if length >= maxRobotsCharged then
break
end
end
end
end
end
for _,robot in pairs(chargingRobots) do
--TODO Suck energy out of pole and add it to the robots
end
--game.players[1].print("Found " .. #chargingRobots .. " robots.")
end
end
function BuiltEntity(event)
if event.created_entity.name == "robot-charger-pole" then
poleTable = {}
poleTable[0] = event.created_entity
poleTable[1] = {}
--game.players[1].print("Entity created")
table.insert(global.robotChargerPoles, poleTable)
end
end
function DestroyedEntity(event)
poleTableToRemoveIndex = -1
if event.entity.name == "robot-charger-pole" then
for index,poleTable in pairs(global.robotChargerPoles) do
if poleTable[0] == event.entity then
poleTableToRemoveIndex = index
break
end
end
if poleTableToRemoveIndex ~= -1 then
table.remove(global.robotChargerPoles, poleTableToRemoveIndex)
else
game.players[1].print("Couldn't find pole to destroy")
end
end
end
function Load(event)
if global.robotChargerPoles == nil then
global.robotChargerPoles = {}
end
end
script.on_event(defines.events.on_tick, UpdatePoles)
script.on_event(defines.events.on_built_entity, BuiltEntity)
script.on_event(defines.events.on_robot_built_entity, BuiltEntity)
script.on_load(Load)
script.on_event(defines.events.on_preplayer_mined_item, DestroyedEntity)
script.on_event(defines.events.on_robot_pre_mined, DestroyedEntity)
script.on_event(defines.events.on_entity_died, DestroyedEntity)