What?
Both LuaSurface.find_nearest_enemy and LuaSurface.find_nearest_enemy_entity_with_owner take max_distance as argument. I'd like to have the option to pass on min_distance as well.Why?
Currently, these functions find just one enemy in the radius given by max_distance around a position. While this may find an enemy, it isn't guaranteed that this enemy can be attacked because there are weapons that have a min_range (e.g. flame thrower). These weapons will only start shooting if min_range <= distance_to_target <= max_range.Of course, I could use LuaSurface.find_entities_filtered to find all entities that do not belong to a certain force and then check if they are at least min_distance away from the shooter position. However, this isn't practical for two reasons:
- While find_nearest_enemy* is looking for entities that are considered military units or structures out of the box, we must construct filters for find_entities* that will catch all entities with a prototype that has the is_military_target flag.
- Even with such filters in place, find_entities* will find all entities based on prototypes with is_military_target. We'd still need to make sure that they are of a different force than the shooter, and that target and shooter force really are enemies.
Alternative
The beauty of find_nearest_enemy* is that it already filters for enemies of the shooter. The problem is that it will find one enemy only, and searching again from the same position will find the same enemy again, or another that would also be less than min_distance away from the shooter's position.If it isn't possible to add min_distance to find_nearest_enemy and find_nearest_enemy_entity_with_owner, it also would be useful to get versions of these that return all enemies/all enemy entities with owner within the circular area around position. With an array of guaranteed enemies available, it should be easy to find a target that's more than min_distance away.