RTS Demo – 04 – Fleeing
The purpose of this document is to provide explanations and descriptions for how fleeing was handled in the Apex Utility AI RTS (Real-Time Strategy) Demo project. It is a part of a series of use-cases showcased through the demo project. If you haven’t already, we recommend reading the High Level Overview Document first. The scene associated with this document is called “04_Fleeing”.
When worker units see an enemy within their scan radius, they need to react. Worker units have low health and non-existent damage, so they have no purpose in combat. Their only solution is to flee in order to attempt to stay alive, and as a bonus they may have enemy units chasing them for a while, possibly keeping a number of enemies busy. Worker units in the demo project use position scoring for fleeing away from the nearest enemy.
Worker units’ fleeing behaviour is facilitated through position scoring of previously sampled positions, see [link: 01 Scanning] for more about how positions are sampled. The position scoring is actually very simple, encompassing just two scorers. The dominant scorer is for scoring positions at further distances from the nearest enemy higher, than those that are closer to the nearest enemy. Thus, units attempt to move away from their nearest enemy. This approach has one weakness: if there are multiple enemies from different directions closing in, the fleeing worker will only seek to move away from the nearest one. The secondary scorer is a nearest position scorer, used as a tiebreaker. Meaning that if two positions score almost or exactly the same, the nearest one is chosen. The scorers and action can be seen in figure 1.
Figure 1 – Shows the composite action which includes the fleeing action, actually a Move to Best Position action. Only two scorers are present in the current implementation of fleeing, one for moving away from the nearest enemy, and one for picking the nearest position.
The fleeing action used is a simple ActionWithOptions evaluating positions, and it makes the unit move to the highest scoring position. Its Execute method can be seen in the below code snippet. The other action, ‘Worker Set Fleeing’, just sets a boolean for visualization purposes.
var c = (UnitContext)context;
var positions = c.sampledPositions;
if (positions.Count == 0)
// no sampled positions available
var best = this.GetBest(c, positions);
c.moveTarget = best;
In the current implementation of the RTS demo, no special handling has been implemented for chasing behaviours. Thus, chasing is extremely simple. Chasing units simply move to the current position of their enemy, not updating that destination while being in movement. This is one of the points that we would like to improve upon in future updates, including breaking off a chase if the chasee is faster than the chaser. The way to improve this would most likely be to use position scoring for the chasing as well, instead of moving to the target directly each time it reaches its last set destination, in addition to introducing a bit more surrounding logic.
Visualization and Debugging
For fleeing units, the prime visualization for debugging purposes is the position score visualizer. It color codes and writes the scores of each scored position, in order to facilitate easy evaluations of whether the scoring is as intended. Additionally, the units’ current move target – where they are currently moving towards – is visualized in blue color with gizmo line and wire sphere. For chasing units the only visualization relevant is the drawing of their attack targets, which shows who they are in fact chasing through gizmo line and wire sphere in red color. Such a scenario can be seen in figure 2.
Figure 2 – This screenshot shows a fleeing unit, in the middle, currently moving towards the blue wire sphere along the blue line. It is fleeing from the chasing melee unit in the bottom left corner, which draws a red line and wire sphere towards the fleeing worker, its current attack target. As can be seen, the position spheres are more red towards the melee unit, and greener away from it, corresponding with the written scores.