"Get random chunk" from Surface

Post Reply
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

"Get random chunk" from Surface

Post by Reika »

Basically a random access into the surface's ChunkIterator. This would need both indexed/named lookup and the '#' operator.

As per
viewtopic.php?f=25&t=50154
I am trying to make an ambient effect (i.e. not tied to any entity) that transforms tiles into other tiles, but the only way I know of to get the valid "in world" positions are using the surface:getchunks, which returns the ChunkIterator. The problem with that is that it provides no information on the number of chunks that it will return, nor does it provide random access into the chunks. As my script has to run in on_tick, even running every 5s creates a large lag spike on worlds with many chunks.
Image

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

Random access into the chunk iterator would be iterating the chunk iterator to get the total count then iterating again to decide when to stop.

What I'm trying to say is: there's no built-in way to "get a random chunk" without just "getting all chunks and pick a random one" which would be roughly the same cost to do in Lua as C++ :)
If you want to get ahold of me I'm almost always on Discord.

User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Get random chunk" from Surface

Post by Reika »

Rseding91 wrote:Random access into the chunk iterator would be iterating the chunk iterator to get the total count then iterating again to decide when to stop.

What I'm trying to say is: there's no built-in way to "get a random chunk" without just "getting all chunks and pick a random one" which would be roughly the same cost to do in Lua as C++ :)
Is there nothing in the internal game engine that is capable of this (that can be exposed to the API) either? I am thinking of a Minecraft-style implementation, where the current chunks are kept in a list, any entry of which can be accessed in O(1).

Ultimately, what I am trying to do just needs a way to pick random positions in the world (think MC block ticks for crop/grass/tree growth etc). I would find it very hard to believe the game was designed in such a way that this could only be done by iterating across the entire world.
Image

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

The game can access any given chunk in O(1) time but doesn't know the total-set of chunks that exist without iterating the container that may or may not hold chunks for the given position in it.

That virtually never happens in the normal game because O(1) access time is far better than iterating.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Get random chunk" from Surface

Post by Reika »

Rseding91 wrote:The game can access any given chunk in O(1) time but doesn't know the total-set of chunks that exist without iterating the container that may or may not hold chunks for the given position in it.

That virtually never happens in the normal game because O(1) access time is far better than iterating.
OK...is there any way at all to give the API the ability to performantly pick random in-world positions?


Alternatively, though it is less generally useful, an event for 'on chunk update pollution' would work for my purposes.
Image

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

Reika wrote:OK...is there any way at all to give the API the ability to performantly pick random in-world positions?
No.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Get random chunk" from Surface

Post by Reika »

Rseding91 wrote:
Reika wrote:OK...is there any way at all to give the API the ability to performantly pick random in-world positions?
No.
In which case let me transform this request into an event for "on chunk updates pollution".
Image

User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Get random chunk" from Surface

Post by Reika »

Would that be feasible?
Image

Veden
Filter Inserter
Filter Inserter
Posts: 294
Joined: Wed Jul 13, 2016 3:54 pm
Contact:

Re: "Get random chunk" from Surface

Post by Veden »

Have you tried recording all of the chunks generated on a surface into an array that is stored into global and randomly pick an index from that array?

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

So, I decided to just implement this in C++ anyway because it might still be faster than doing it in Lua and then anybody can use it if they want.
If you want to get ahold of me I'm almost always on Discord.

User avatar
adamwong246
Fast Inserter
Fast Inserter
Posts: 148
Joined: Tue Dec 01, 2020 5:01 am
Contact:

Re: "Get random chunk" from Surface

Post by adamwong246 »

I'd really like to know how you did this. My mod suffers from loading all chunks at once.

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

adamwong246 wrote:
Wed Dec 02, 2020 5:39 am
I'd really like to know how you did this. My mod suffers from loading all chunks at once.
I'm not quite sure I get what you're saying. "loading all chunks at once" is a very specific thing related to game logic but I suspect you mean something else completely?
If you want to get ahold of me I'm almost always on Discord.

User avatar
adamwong246
Fast Inserter
Fast Inserter
Posts: 148
Joined: Tue Dec 01, 2020 5:01 am
Contact:

Re: "Get random chunk" from Surface

Post by adamwong246 »

I guess I meant "iterating" rather than "loading."

I'm trying to pick a random entity_with_health off the surface every nth tick and I was hoping I could do it with`chunkIterator`but even iterating over each chunk, (and then iterating over the entities within that chunk) was causing a lag.

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

Re: "Get random chunk" from Surface

Post by Rseding91 »

adamwong246 wrote:
Wed Dec 02, 2020 6:33 am
I guess I meant "iterating" rather than "loading."

I'm trying to pick a random entity_with_health off the surface every nth tick and I was hoping I could do it with`chunkIterator`but even iterating over each chunk, (and then iterating over the entities within that chunk) was causing a lag.
Ah, that's always going to lag.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Implemented mod requests”