How do I determine the total resources visible?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
KillerAnt
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sat Jan 23, 2021 7:25 am
Contact:

How do I determine the total resources visible?

Post 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?

User avatar
Silari
Filter Inserter
Filter Inserter
Posts: 490
Joined: Sat Jan 27, 2018 10:04 pm
Contact:

Re: How do I determine the total resources visible?

Post 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.

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: How do I determine the total resources visible?

Post 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.

KillerAnt
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sat Jan 23, 2021 7:25 am
Contact:

Re: How do I determine the total resources visible?

Post 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.

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: How do I determine the total resources visible?

Post by quale »

I realized shortly after posting, but I figured it would have that effect. Glad it worked out. :)

Post Reply

Return to “Gameplay Help”