find_entities_filtered with position, radius and limit

Place to get help with not working mods / modding interface.
Post Reply
PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

find_entities_filtered with position, radius and limit

Post by PFQNiet »

Given a position, radius and limit, which entities are returned?

Does the function scan outward from the position until it finds enough entities, at which point it returns the N closest matching entities? Or does it compute the area to search based on position and radius and return whatever N matching entities it happens to find in there?

Use case: I'd like to search for up to 10 of a type of entity within a radius of the player. If the function scans outwards from the position, then I can just use find_entities_filtered without any worry. But if it's arbitrary, then I would need to find all matching entities, sort them by distance and take the 10 closest manually.

I can absolutely do that, of course, but if the function already guarantees the 10 closest, it's redundant to code that myself.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2244
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: find_entities_filtered with position, radius and limit

Post by boskid »

find_entities_filtered with position and radius goes as follows: it does entity search inside of a box {{x-r, y-r}, {x+r, y+r}}. entity search itself goes row by row starting at left top (north west) top of that box going right(east) and then next line directly below that. Lines are based on advanced tiles which are 2 tiles high. Entities are returned only when their position is within given radius from the given search position which gives a nice circle: (ex-x)^2+(ey-y)^2 <= r^2. Limit is applied live as the scan finds entities so if it finds enough entities starting on the top lines (north) it will abort search and return what it found already.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: find_entities_filtered with position, radius and limit

Post by PFQNiet »

Ah, thank you for the detailed info! It's good to know that position + radius search is based on actual point position, not collision boxes (which position alone and area searches are).

I've gone ahead and implemented my search as a series of searches with increasing radii until enough entities are found (or the max radius is reached). Since this is only done in response to a custom-input, and the numbers involved are still quite small, it should be plenty performant enough.

Thanks again!

Post Reply

Return to “Modding help”