Getting list of all generated chunks

Place to get help with not working mods / modding interface.
Post Reply
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2635
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Getting list of all generated chunks

Post by FuryoftheStars »

To help make some operations of a mod I'm working on more performant, I'm wanting to switch from surface.find_entities_filtered of the entire surface to checking a few chunks at a time. I'm looking for a way to get a list of all currently generated chunks, store them away in global, then parse through them a few at a time each tick. Would the LuaSurface::get_chunks do this for me? It's not clear from the description if it's all possible, generated, or charted chunks, though my assumption is generated. Also being an iterator, I'm unsure if I can actually just store its results somewhere and do them a few at a time vs needing to loop through them all in one shot (even if that loop is going through to store them away one by one).
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics

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

Re: Getting list of all generated chunks

Post by DaveMcW »

Yes, the iterator can be stored.

The stored iterator contains the current chunks, even if they were generated after it was created. It might not catch some newly created chunks, if they are inserted before the current position.

When the iterator reaches the end of the list it stops iterating, even if new chunks are generated later.

Example, run it on a new map for best effect:

Code: Select all

/c
global.count = 0
script.on_nth_tick(1, function()
  chunk = global.iterator and global.iterator()
  if chunk then 
    global.count = global.count + 1
  else 
    game.print("end at " .. global.count .. " chunks")
    global.count = 0
    local n = 0
    for chunk in game.surfaces[1].get_chunks() do n = n + 1 end
    game.print("start at " .. n .. " chunks")
    global.iterator = game.surfaces[1].get_chunks()
  end
end)

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2635
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Getting list of all generated chunks

Post by FuryoftheStars »

Ok, great, thank you. That's fine if it doesn't catch newly generated chunks after its creation. I'm already listening to on_chunk_generated. I just need this iterator for already existing maps that this mod is added to, or if one of its startup settings are changed.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics

Post Reply

Return to “Modding help”