Page 1 of 1

Access to a robot's energy

Posted: Sun Mar 20, 2016 3:11 am
by Yulgalminakf
I wanted to create a robot charging pole that charges every robot within a certain radius. In control.lua, I have everything working except the actual charging of robots. How can I get access to and change the robot's energy levels?

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)		
This finds and stores up to 10 robots in a radius of 50 around the pole. This all works fine, but I have no idea how to get access to the robot's energy levels though. The wiki isn't all that helpful as a reference because a lot of it is left blank (including the one for robots). Anyone have some advice?

Re: Access to a robot's energy

Posted: Sun Mar 20, 2016 9:48 am
by prg
Tried entity.energy?

Re: Access to a robot's energy

Posted: Mon Mar 21, 2016 5:44 am
by Yulgalminakf
Yes. Tried that. I get an error saying "Entity doesn't have an energy_source."

Re: Access to a robot's energy

Posted: Mon Mar 21, 2016 8:34 am
by prg
If I point at a robot and run something like print(game.player.selected.energy) or game.player.selected.energy=0 it seems to be working just fine. How are you doing it?