surface.find_entities_filtered() hangs for large radius
Posted: Mon Mar 20, 2023 8:32 pm
Both the Bluebuild and Autobuild mods hang the game when you start a Sandbox. Both are very similar code so I only looked at Bluebuild to see where it hangs and this is where it hangs:
In Sandbox mode reach_distance = 4294967295 so it searches a basically infinite area.
But if I change that to
then the game searches the whole surface and finishes with no delay as barely any area is generated.
It looks like find_entitites_filtered with an area to search iterates over the whole area while searching the whole surface only goes through the generated chunks. This problem could be avoided by checking the radius against the number of generated chunks on the surface and picking a smarter iterator:
The API for find_entities_filtered does not specify any order for the returned entites so changing the order in which chunks are checked would not violate the API contract.
PS: same problem probably exists when searching for tiles, when counting entities/tiles, find_non_colliding_position, ...
Code: Select all
local areaList = builder.surface.find_entities_filtered{position = pos, radius = builder.reach_distance, type = {"entity-ghost", "tile-ghost"}, force=builder.force, limit=200 }
But if I change that to
Code: Select all
local areaList = builder.surface.find_entities_filtered{type = {"entity-ghost", "tile-ghost"}, force=builder.force, limit=200 }
It looks like find_entitites_filtered with an area to search iterates over the whole area while searching the whole surface only goes through the generated chunks. This problem could be avoided by checking the radius against the number of generated chunks on the surface and picking a smarter iterator:
Code: Select all
if (M_PI * radius * radius > surface.num_generated_chunks) {
iterate_over_generated_chunks(...);
else
iterate_over_area(...);
end
PS: same problem probably exists when searching for tiles, when counting entities/tiles, find_non_colliding_position, ...