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?
Performant way to pick random positions in the world?
Re: Performant way to pick random positions in the world?
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?
Then in your random function check if the generated position is already generated, if not generate a new one until it is valid?
Re: Performant way to pick random positions in the world?
I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.
Re: Performant way to pick random positions in the world?
No, the only thing given is an iterator across ALL chunks.darkfrei wrote:I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.
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.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?
Re: Performant way to pick random positions in the world?
Looking at LuaChunkIterator It does seem lacking basic functionality of other custom lists.
If it had # and [] you could do something like this
Chances are pretty good if you argue like this that it'll be added to some future version.
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)]
My Mods: mods.factorio.com
Re: Performant way to pick random positions in the world?
Is there a location to request new API hooks and/or ping a developer?Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
Re: Performant way to pick random positions in the world?
There's an entire subforum for itReika wrote:Is there a location to request new API hooks and/or ping a developer?Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
viewforum.php?f=28
My Mods: mods.factorio.com
Re: Performant way to pick random positions in the world?
Requested.Optera wrote:There's an entire subforum for itReika wrote:Is there a location to request new API hooks and/or ping a developer?Optera wrote:Chances are pretty good if you argue like this that it'll be added to some future version.
viewforum.php?f=28
Re: Performant way to pick random positions in the world?
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.Reika wrote:No, the only thing given is an iterator across ALL chunks.darkfrei wrote:I think that you can have the list of chunks, chose one with random and take a ransom coordinate in it.
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.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?