Entities could simply be kept in a heap structure instead of a hash table as it seems to be now. The nearest entity would naturally float to the top of the heap. Insertion and removal would take O(log n) time instead of O(1) amortized time as for the hash table. For a 64x64 tiles region insertion and removal would be up to 16 times slower. Would that even be noticable? Or would it totaly disapear under the cost of allocating or freeing and initializing a new ghost entity. I don't think the per entity cost would be that huge, esspecially if it is only used for personal roboports, not all roboports.Manron wrote:i guess it is the way it is for performance reasons.
calculating which entity is currently 'nearest' has to be calculated every time a single entity is removed or you move.
i dont think the QOL improvement would justify the performance hit.
But maybe i'm wrong here, who knows but the devs.
When you move the distance of every entity changes. Also a lot of entities can enter or leave the reachable area. If you change the diatance of every entity in the heap separately then that would be O(n * log n) but if you instead just change the distance and then re-heap the whole thing in one go it is O(n). Computing the sqrt(dx*dx + dy*dy) for each entity costs probably more than re-heaping.
Now if the player is running around with 6 exoskeletons, driving a car or riding a train this might be a problem. First off I would love it if roboports would simply stop sending out robots while moving. At least while moving faster than the robots can fly. Secondly you could take advantage of the chunks. For each chunk compute the minimum distance to the player. Then start with the chunk the player is in and find the nearest entity there. If any other chunk has minimum distance less than the nearest entity then add entities from that chunk to the heap. Otherwise handle the nearest entity. Worst case for this algorithm will be when you stand unmoving in a huge (de)construction area near the end. All the chunks lying on a circle around the player will have to be considered. But that's still a lot better than dealing with the whole area under the roboport (linear instead of quadratic).
If that is still too much:
* Does it have to react instantaneous? You could only re-heap every 1-5 seconds instead of on every move.You could devide the chunks into subchunks to get finer groups of entities to sort.
* Does it have to be perfect? Compute the distance of an entity as distance to center of chunk + distance of chunk to player. Handle the chunk (+ neighbours when near a border) the player is in prefectly, the rest by approximation. You can then keep a heap of entities in each chunk and keep a heap of chunks sorted by the approximately nearest entity of each chunk. Moving around only resorts the heap of chunks, not each chunks heap.
That last one would also be useful for using this with roboports. When a construction bot needs work it picks the nearest roboport that needs work done, then the chunk nearest to that roboport that needs work done and last the entity nearest to the center of the chunk..