Table of all entities everywhere.

This is the place to request new mods or give ideas about what could be done.
Post Reply
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Table of all entities everywhere.

Post by bobingabout »

The only hooks I can seem to find for finding an entity include:

find_entity(entity, position)
find_entities(area)
find_entities_filtered{area=…, name=…, type=…, force=…, limit=…}
find_enemy_units(center, radius, force
find_nearest_enemy{position=…, max_distance=…, force=…}

all in LuaSurface by the way.

now, these are alright if you want to find out what is near you, but what if you're doing some sort of migration, scanning to see what exists so you can hook a script to it...
from what I can tell, you have to scan an area, and see if the entity type is there....

The game obviously knows all entities that exist, since it updates them with the built in functionality, so... why can't we search through a list of all entities that exist everywhere?

game.entities, being a table of all entities everywhere.
LuaSurface.entities being a list of all entities on that surface.

Somehow, I think using a hook like that would be a lot faster than...

Code: Select all

  local entities = game.surfaces[1].find_entities_filtered{area = {{-1000000, -1000000}, {1000000, 1000000}}, type= "inserter"} -- find and save a table of every inserter on the whole possible map
instead, something like this

Code: Select all

  for i, entity in pairs(game.surfaces[1].entities) do -- iterate through entities
    if entity.type = "inserter" then -- it's an inserter, do something to it
or even

Code: Select all

  local entities = game.surfaces[1].find_entities_filtered{type= "inserter"} -- find and save a table of every inserter on the surface
since no area is specified, it should just iterate through all entities, rather than searching in an area. and in this case, create and return a table of all inserters.

since searching a grid of 2 million by 2 million takes... effectively forever, I had to narrow the area down to 100 thousand by 100 thousand, which takes about 10 seconds on my rather fast computer. something at least remotely sane. but if someone built an inserter further than this away from the middle of the map, it would be missed.


Do you see where I'm coming from? Is there any technical limitation to prevent this from being possible? (or faster than an area scan)

I mean, you scan the maximum possible map size, it's obviously checking every single tile position even if nothing is there because of how long it takes. so, on larger areas, iterating through a table of entities and checking if it is within the specified area would be far faster in my opinion, as well as allowing for checking the entire surface, regardless of position.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13219
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Table of all entities everywhere.

Post by Rseding91 »

That sounds reasonable.

Can you think of what all kinds of interfaces you'd like in such a method?

I can have it return a table of all entities on a surface, all entities on chunks, filter them by type, or what ever.
If you want to get ahold of me I'm almost always on Discord.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Table of all entities everywhere.

Post by bobingabout »

Obviously I can't speak for everyone on this, but personally, I'd see it as a function similar to these 2.

Code: Select all

LuaSurface.find_entities(area)
LuaSurface.find_entities_filtered{area=…, name=…, type=…, force=…, limit=…}
Basically, do exactly what they would do, but return all entities on the surface.
Since possible filters on the filtered variant include name, type and force, those sound like good factors to be able to limit the returned results to.

Though, by chunk does sound like a reasonable additional limitation too, return all entities (possibly filtered by name, type, force too) in a specific chunk.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13219
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Table of all entities everywhere.

Post by Rseding91 »

Done for 0.13.10:
changelog wrote:Changed LuaSurface::find_entities/count_entities/filtered to search the entire surface if the area isn't defined.
If you want to get ahold of me I'm almost always on Discord.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Table of all entities everywhere.

Post by bobingabout »

Thank you very much *Bows*

I suppose when the update arrives, I'll have to do a test to see how much faster it is.

Either way, I know it will make certain things in my mods easier to do.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

Post Reply

Return to “Ideas and Requests For Mods”