Page 1 of 1

"Get random chunk" from Surface

Posted: Sat Jun 24, 2017 6:51 pm
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.

Re: "Get random chunk" from Surface

Posted: Sat Jun 24, 2017 10:10 pm
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++ :)

Re: "Get random chunk" from Surface

Posted: Sun Jun 25, 2017 12:45 am
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.

Re: "Get random chunk" from Surface

Posted: Sun Jun 25, 2017 10:13 am
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.

Re: "Get random chunk" from Surface

Posted: Sun Jun 25, 2017 8:05 pm
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.

Re: "Get random chunk" from Surface

Posted: Sun Jun 25, 2017 9:54 pm
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.

Re: "Get random chunk" from Surface

Posted: Sun Jun 25, 2017 11:45 pm
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".

Re: "Get random chunk" from Surface

Posted: Wed Jun 28, 2017 7:56 am
by Reika
Would that be feasible?

Re: "Get random chunk" from Surface

Posted: Tue Jul 18, 2017 2:30 am
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?

Re: "Get random chunk" from Surface

Posted: Fri Jan 26, 2018 7:03 am
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.

Re: "Get random chunk" from Surface

Posted: Wed Dec 02, 2020 5:39 am
by adamwong246
I'd really like to know how you did this. My mod suffers from loading all chunks at once.

Re: "Get random chunk" from Surface

Posted: Wed Dec 02, 2020 5:50 am
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?

Re: "Get random chunk" from Surface

Posted: Wed Dec 02, 2020 6:33 am
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.

Re: "Get random chunk" from Surface

Posted: Wed Dec 02, 2020 6:54 am
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.