Performant way to pick random positions in the world?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Performant way to pick random positions in the world?

Post by Reika »

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.

Is there an easy and performant way to either "get random chunk" or, failing that, "get bounds of world" (i.e. min/max generated X/Y) so I can try picking random coordinates and testing chunk existence?
Image

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Performant way to pick random positions in the world?

Post by Choumiko »

You could use the on_chunk_generated event to keep track of either all generated chunks or just to adjust the max/min x/y coordinates of the world.
Then in your random function check if the generated position is already generated, if not generate a new one until it is valid?

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

Re: Performant way to pick random positions in the world?

Post by darkfrei »

I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.

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

Re: Performant way to pick random positions in the world?

Post by Reika »

darkfrei wrote:I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.
No, the only thing given is an iterator across ALL chunks.
Choumiko wrote:You could use the on_chunk_generated event to keep track of either all generated chunks or just to adjust the max/min x/y coordinates of the world.
Then in your random function check if the generated position is already generated, if not generate a new one until it is valid?
This is vulnerable to becoming incorrect if other mods delete chunks or similar, which they can do. Also, just sticking all the chunks in the list is not going to help performance much.
Image

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Performant way to pick random positions in the world?

Post by Optera »

Looking at LuaChunkIterator It does seem lacking basic functionality of other custom lists.
If it had # and [] you could do something like this

Code: Select all

local chunks = surface.get_chunks()
random chunk = chunks[math.random(1, #chunks)]
Chances are pretty good if you argue like this that it'll be added to some future version.

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

Re: Performant way to pick random positions in the world?

Post by Reika »

Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
Is there a location to request new API hooks and/or ping a developer?
Image

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Performant way to pick random positions in the world?

Post by Optera »

Reika wrote:
Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
Is there a location to request new API hooks and/or ping a developer?
There's an entire subforum for it
viewforum.php?f=28

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

Re: Performant way to pick random positions in the world?

Post by Reika »

Optera wrote:
Reika wrote:
Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
Is there a location to request new API hooks and/or ping a developer?
There's an entire subforum for it
viewforum.php?f=28
Requested.
Image

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

Re: Performant way to pick random positions in the world?

Post by Veden »

Reika wrote:
darkfrei wrote:I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.
No, the only thing given is an iterator across ALL chunks.
Choumiko wrote:You could use the on_chunk_generated event to keep track of either all generated chunks or just to adjust the max/min x/y coordinates of the world.
Then in your random function check if the generated position is already generated, if not generate a new one until it is valid?
This is vulnerable to becoming incorrect if other mods delete chunks or similar, which they can do. Also, just sticking all the chunks in the list is not going to help performance much.
If you are storing all of the chunks on surfaces in a global and have a hook into on_chunk_generated to add the newly created chunks in the same global, which when you want an effect just involves doing a lookup on this table to find a chunk. How often are chunks deleted? You can always test a position for if the chunk exists before you make your changes within the chunk.

Post Reply

Return to “Modding help”