Download

Help with Prioritizing a Target with the Least Distance and Health

So I have an AI service that finds all enemies within a maximum distance from the AI. It makes sure each possible target has more than 0 health and that it can be seen. I have three arrays, PossibleTargets(character array), TargetDistances (float array), and TargetHealth (float array). Which I have populated with each target, their distance to the AI, and their current health.

The question is: How do I find a target with the best of both worlds; the enemy that is the closest to the controlled AI and has the least health? (while still prioritizing the target that is closest)

Perhaps I could add the two values together (at the same index) and add them to a fourth array and then find the lowest value of that array?

There are many ways to do it :

  • You can make a secondary range where priority is based on health ( will have an impact on performence , many loops will be executed)
    Or:
  • You can also have the health having an impact on distance (ex. NewDistance = Distance - (1 - HealthRatio) * A_Value )

Okay I put together your second option. But, what exactly is the A_Value?

EDIT: Oh, I get it. the A_Value is how much the health will effect the distance?

Yeah , (1 - HealthRatio) will give a value between 0 & 1 , and there is nothing really logical there A_Value will be based on tests (maybe 50 , 100,…)

For the second way using 2 tests , here is a way to do it :

1- Find the nearest enemy
2- Find the lowest health in 2nd range ( nearest distance + X_Range)

It’s hard to say without knowing how your game works, but I think your implementation should depend on a few more factors than just distance and health. Anyway, I messed around with some values in a spreadsheet and I think I found a formula that works pretty well. The formula I used was distance * (health + a)[SUP]b[/SUP], and the values the seemed to give the best results were as follows:

a = 3 (lowering this value causes the health to be favored)
b = 2 (raising this value causes the health to be favored)

After this calculate is done, you would iterate through each enemy and pick the lowest value. Note that the health has to be normalized to a 0-1 range. However, if all of your enemies have the same max health, then you would simply find this value with health / max health.