Page 1 of 1

finding ghosts in LuaLogisticNetwork

Posted: Tue Jul 02, 2019 5:26 am
by Optera
Issue:
Currently finding ghosts within a specific LuaLogisticNetwork is slow and cumbersome in lua.
We have to iterate through every cell in the network to get the area and search on the surface for ghosts.

The main problem with this approach is that overlapping cells scan the same area multiple times resulting in terrible performance.
Now while each mod trying to fetch ghosts could calculate the complete network polygon and break it up into rectangles for find_entities itself, performance would be a lot better if that was a function of LuaLogisticNetwork.
Request:
Extend LuaLogisticNetwork with a find_ghosts function returning all ghosts, tile ghosts and requester proxies within any however shaped logistic network.

Alternatively extend LuaLogisticNetwork with get_coverage returning a polygon and find_entities/find_entities_filtered to consume such a polygon as scan area.
I'm not sure if introducing a polygon data model makes sense, but once introduced it could be used for other things like collision boxes on curved rails.

Re: finding ghosts in LuaLogisticNetwork

Posted: Tue Jul 02, 2019 10:12 am
by Boodals
A better way to do this is to listen to the various entity created events, check if it's a ghost, and put it into a global array. Then loop through them, and use that API function that tells you what logistics cell it's in (if any).
If something doesn't raise an event then you're out of luck though.

Re: finding ghosts in LuaLogisticNetwork

Posted: Tue Jul 02, 2019 11:10 am
by Rseding91
What you describe is literally what the game does to dispatch robots to build ghosts. That's why it's limited to only a few per tick at most that it sends robots to work on.

Re: finding ghosts in LuaLogisticNetwork

Posted: Tue Jul 02, 2019 1:05 pm
by posila
Rseding91 wrote: Tue Jul 02, 2019 11:10 am ... literally ...
That's not what the game does at all. It's the other way around. The game has list(s) of ghosts and searches for logistic network that could service them (since single ghost could be within construction range of multiple networks)

Re: finding ghosts in LuaLogisticNetwork

Posted: Tue Jul 02, 2019 1:09 pm
by Rseding91
posila wrote: Tue Jul 02, 2019 1:05 pm
Rseding91 wrote: Tue Jul 02, 2019 11:10 am ... literally ...
That's not what the game does at all. It's the other way around. The game has list(s) of ghosts and searches for logistic network that could service them (since single ghost could be within construction range of multiple networks)
It does both. When a roboport is built it searches.

Re: finding ghosts in LuaLogisticNetwork

Posted: Wed Jul 03, 2019 6:15 am
by Optera
Boodals wrote: Tue Jul 02, 2019 10:12 am A better way to do this is to listen to the various entity created events, check if it's a ghost, and put it into a global array. Then loop through them, and use that API function that tells you what logistics cell it's in (if any).
If something doesn't raise an event then you're out of luck though.
Not raising events is a big problem with ghosts and logistics networks.
- building or destroying request proxies doesn't raise events
- changes to logistic networks, from roboports running out of power also doesn't raise events

Another problem is lack of connection between entity and covering logistics_network.
While LuaEntity has logistic_cell and logistic_network those properties are nil for almost all entities including ghost and tile-ghost.

If there was a reliable way to get proxies, caching ghosts could improve performance by swapping finding ghosts on a surface to finding networks on ghost positions entity.surface.find_logistic_network_by_position(entity.position, entity.force) for each ghost.