While working with unit AI I've run into a fun problem to solve. How does a unit, left up to its own devices, determine which enemy to attack? Throughout this I'll use the example of an aggressive Archer who sees a Soldier and a Healer. Whom does the Archer attack and does that answer change based on the circumstances of the situation? In setting the ground rules remember this has nothing to do with the user commanding the archer to attack a specific unit target. Commands from users override all other commands.
The easiest way to deal with these questions is to have a "unit value." This value can be based on anything and is assigned to each type of unit. For example a low level soldier may have the value of '3' purely based on how much damage it does. A low level healer may have the value of '5' because its healing can keep other units alive (making it particularly annoying). Finally, a resource cart may have the value of '6' because if it dies the enemy would lose a strategic advantage. When determining whom to attack, a unit can check potential target's value; the unit with the highest value is the target. So in our example, our little archer unit will see a soldier (value of 3) and healer (value of 5) and attack the healer. Very cut and dry.
The problem with this system is when trying to take current situations into account. With our main example, what should our archer do when the healer is far away (in sight range but not attack range) and the enemy soldier is within striking distance? When checking for the target, should there be a condition to look at sight verse attack range?
How does this all change when the soldier is within its own attack range and capable of attacking our archer? Should units have a self preservation condition? This starts to spiral down a larger rabbit hole and gets into how much information any single unit has. Does our little archer know the attack range of the soldier? What about the remaining health of a potential target (and places a higher desire to kill targets with lower health)? Should our archer switch targets every time it gets hit (again, this switch could not occur when the user commands the attacking of a specific target)? Does this self-preservation extend to stopping the fight and running away? The more of these special conditions are factored in the less useful the unit value is. So should the unit value be used at all, is it useful?
A part of the answer may reside with weights and thresholds. If the unit's self-preservation is important, then it may only become important if a certain threshold is reached. For example, our archer may continue to shoot at the healer until its health drops below 50%, then the self-preservation conditions may come into affect. Our archer may do different checks at different radius (sight range, attack range, half the sight range) so it can place more value on targets closer. For the potential targets found in each range units values are checked and multiplied by coefficients to get a weighted value. For example, healer units may have a coefficient of 1, while soldiers have a coefficient of 2. In our example with the enemy soldier being close to our archer we could apply the rules of "For any potential target within our attack range, multiply the unit value by the coefficient." This would make the soldier now have total value of '6' verses the healer (who is outside of the attack range) who has a total value of '5'. Our archer will attack the soldier.
Ultimately these questions will not be answered until the game is balanced. After play testing and demoing players might reject the logic for any number of reasons. To build this system correctly each condition should be as modular as possible. That way changes can be made quick and easy (and hopefully in an optimized way).