Page 1 of 1

How do I determine the total resources visible?

Posted: Fri Oct 01, 2021 3:38 pm
by KillerAnt
I would like to query my current game (through console) how many ores per type are in the visible area.

I already found some lua commands in the wiki which are probably helpful:

print
game.player.surface.find_entities_filtered{type="resource"})

but I seem unable to wire them up correctly - I don't get something printed, nor do I see error messages. For example

/c print(game.player.surface.find_entities_filtered({type="resource"})[0])

does not output anything. Any ideas?

Re: How do I determine the total resources visible?

Posted: Fri Oct 01, 2021 4:34 pm
by Silari
Two isses:

One: LUA uses 1 not 0 as the start of an array.

Two: game.print can only print strings. Find entities returns a table of the found entities (or nil). If you want to print a table you need to run it through serpent.block(table) first, which converts it to a string formatted for easier reading.

Code: Select all

/c game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1]))
Note that attempting to print a LUAEntity isn't very useful - it just has a __self attribute, though it can be useful to check if it's nil. This is normal and you can still access the .name or .amount attributes for what you're trying to do.

Code: Select all

/c game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1].name))
game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1].amount))
will show the first found resource's name and amount of resources.

Re: How do I determine the total resources visible?

Posted: Fri Oct 01, 2021 4:38 pm
by quale
Just kind of poking around in the API, I came up with this:

Code: Select all

local function viewable_area(player, zoom)
	local p = player.position
	local r = player.display_resolution
	zoom = (zoom or 1) * 64
	return {left_top = {x = p.x - r.width / zoom, y = p.y - r.height / zoom},
	    right_bottom = {x = p.x + r.width / zoom, y = p.y + r.height / zoom}}
end

local function count_visible_resources(player, zoom)
	local entities = player.surface.find_entities_filtered{area = viewable_area(player, zoom), type = "resource"}
	local resources = {}
	for _, entity in ipairs(entities) do
		resources[entity.name] = (resources[entity.name] or 0) + entity.amount
	end
	return resources
end

for name, amount in pairs(count_visible_resources(game.player)) do
	game.player.print(name..": "..amount)
end
I’m not sure if the player’s zoom level is available anywhere. The property I found is write-only for some reason.

Re: How do I determine the total resources visible?

Posted: Sun Oct 03, 2021 9:25 am
by KillerAnt
Many thanks for your replies, was very helpful. Especially the working code. I meant the whole rendered map when I said "visible area", but I managed to remove the area calculation and constraint. Sorry if you did more work than actually needed, just because I am just too tired this week to clearly state my problem.

Re: How do I determine the total resources visible?

Posted: Sun Oct 03, 2021 11:08 am
by quale
I realized shortly after posting, but I figured it would have that effect. Glad it worked out. :)