surface.total_pollution read

Post Reply
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

surface.total_pollution read

Post by Boodals »

Currently, to get the total amount of pollution on a surface, you have to iterate over every chunk, and call get_pollution on them all. As surfaces can easily have hundreds if not thousands of chunks, it quickly gets slow.

Bilka said on discord that the game tracks polluted chunks, and thus will be able to do it more efficiently. Also less API calls is always better.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: surface.total_pollution read

Post by Rseding91 »

What's the use-case? I could add it, but it's still an O(N) cost where N is all of the polluted chunks.
If you want to get ahold of me I'm almost always on Discord.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: surface.total_pollution read

Post by darkfrei »

Rseding91 wrote:
Thu Jan 03, 2019 8:48 am
What's the use-case? I could add it, but it's still an O(N) cost where N is all of the polluted chunks.
How it was checked in evolution calculation?

User avatar
Godmave
Long Handed Inserter
Long Handed Inserter
Posts: 89
Joined: Tue Nov 15, 2016 3:52 pm
Contact:

Re: surface.total_pollution read

Post by Godmave »

How it was checked in evolution calculation?
I would guess that that one only sums up the pollution absorbed by the spawners

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: surface.total_pollution read

Post by DaveMcW »

Evolution only adds up pollution, it doesn't subtract when it is absorbed by tiles.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: surface.total_pollution read

Post by Rseding91 »

darkfrei wrote:
Fri Jan 04, 2019 8:08 am
Rseding91 wrote:
Thu Jan 03, 2019 8:48 am
What's the use-case? I could add it, but it's still an O(N) cost where N is all of the polluted chunks.
How it was checked in evolution calculation?
Evolution is modified as pollution is created.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Mylon
Filter Inserter
Filter Inserter
Posts: 513
Joined: Sun Oct 23, 2016 11:42 pm
Contact:

Re: surface.total_pollution read

Post by Mylon »

It's pretty simple to do in lua:

Code: Select all

function getPollution()
	-- Iterate over all chunks and sum pollution.
		local pollution = 0
		-- local surface = game.surfaces[1]
		for chunk in game.surfaces[1].get_chunks() do
			pollution = pollution + game.surfaces[1].get_pollution({chunk.x*32, chunk.y*32})
		end
		return pollution
end

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: surface.total_pollution read

Post by Boodals »

Rseding91 wrote:
Thu Jan 03, 2019 8:48 am
What's the use-case? I could add it, but it's still an O(N) cost where N is all of the polluted chunks.
A simple use case would be just a GUI that monitors the total amount of pollution your factory is producing.
My use case is: a mod which makes pollution affect whether the surface is habitable, or if you need a special suit to breathe, and also causes global warming which can melt ice (alien biomes), and start forest fires.

As an alternative to the O(N), you could keep a running total for each surface that updates when pollution is created/absorbed. It would affect performance of the base game, but perhaps it could be used in a graph. It would be nice to be able to see how much less pollution you're producing by installing efficiency modules, for example.

Mylon wrote:
Fri Jan 04, 2019 3:12 pm
It's pretty simple to do in lua:
Read the OP

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: surface.total_pollution read

Post by Boodals »

Ayy I implemented this myself for 0.17 (I got access to the game's source code).
LuaSurface.get_total_pollution()

Post Reply

Return to “Implemented mod requests”