# How to make ranged RTS units move to their firing range from an opponent using flow fields?

I am trying to make a simple real-time strategy game (only the battle system of units fighting each other; no building, no economy, only battles and victory). I am using flow fields to make the units move.

I don't know how to make the AI take the right actions when I completing a move.

For example, I have a ranged unit, its behavior should move to its firing distance from the enemy and stop (or orbit/strafe in a circle around the enemy) and attack the enemy. But in my current code, the ranged unit just follows the flow field to move close to the enemy.

I don't know how to make it flee from the enemy until it gets to its attack range.

There is a way is recreate the flow fields to direct the unit to its flee positions from an enemy, but there are many ranged units, and their enemy is same. I don't know if it's right to create many flow fields for the units which have the same target.

I can't find a solution for this condition, I want my AI to behave more freely and not only follow the flow fields.

• "rts game" and "simple" are two phrases that do not belong into the same sentence. Sep 12, 2023 at 11:56
• Yse ,you are right,Rts it not a simple type , my game actually not a rts game , only the units fight part Sep 12, 2023 at 14:48

Instead of a flow field (storing direction to travel at each point), consider using a distance field (storing the distance to the closest enemy at each point).

From a distance field, you can quickly determine the flow direction: it's just the gradient of the field. That means you can check the neighbours of a cell, and move in the direction the distance is decreasing (approach) or increasing (flee) most/fastest.

That lets you get both approach and flee behaviours from one data structure. And it also answers the question "how far", so a unit trying to maintain a set distance can climb up the distance field when it's currently too close, and roll down the distance field when it's currently too far.

• Really Thanks for editing and replying to the post!!! I think that it's not right to use the fields system to move in my game, sorry, I don't have enough experience to design the game. Could I write the game detail request here to ask for help? The system is difficult for me, I couldn't work it out Sep 13, 2023 at 13:49
• You're always welcome to ask questions here. It's not clear to me though what specific problem makes fields unsuitable for your needs, or what specific aspect you need help with. So make sure your question explains this in detail. Sep 13, 2023 at 15:43

A simple solution to this would be to have two flow fields:

• An "Attack" flow field.
• A "Retreat" flow field.

If the ranged unit is too far away it uses the attack flow field to move closer to the target and if it is too close it uses the retreat field to move away. If the unit is "just right" it stops moving.

A very simple retreat field would just be the reverse of the attack field, however this would be problematic, because it would simply repel units - possibly into impassable terrain.

A better retreat field would be a field that targets a position the unit had a while back - so it retreats back the way it came or you could be a bit more intelligent - targeting a friendly base or large group of friendly units.

However this entire design has a fatal flaw - it assumes you are only facing a single enemy unit (or tightly grouped set of enemy units) if you have two groups of enemy units simply moving towards one may bring you under fire from the other.

If initiating new attack you may be able to solve this by careful target selection (only picking off straggler enemy units), however if the AI unit gets enveloped it probably won't pick the optimum strategy.