TExcelTAD
Home › Game AI › Cover Points

Cover Points

Shooter AI that stands in the open and trades bullets with the player looks dumb and dies fast. AI that runs to a crate, crouches behind it and pops out to shoot looks like it has training, even though the "intelligence" is mostly a list of points and a scoring function. This lesson builds both.

Step 1: find cover points

A cover point is a spot where a wall or object stands between the agent and some directions of fire. Two common ways to get them:

Approach How Trade-off
Hand-placed designers drop cover markers in the editor (with flags like "low", "can lean left") full control, more work per level
Generated a tool walks the edges of the navmesh or grid and places a point wherever there is an obstacle next to walkable floor free for every level, needs filtering
Queried at run time an environment query samples points around the agent and tests each one (Unreal's EQS does this, see Unreal: EQS, StateTree & Perception) adapts to dynamic levels, costs CPU per query

The program below uses the generated approach on a grid: every floor cell that touches a crate (# inside the room) is a candidate.

Step 2: score each point against the current threat

Whether a point is good depends on the situation: where the threat is and where the agent is now. A typical score combines:

  • Protected: the line from the threat to the point is blocked. Without this, it isn't cover at all.
  • Can shoot from it: a neighbouring cell has line of sight to the threat, so the agent can lean or step out to fire.
  • Close to the agent: less time running in the open.
  • Preferred range: not hugging the enemy, not out of the fight.
csharp · try it yourself
using System;
using System.Collections.Generic;
using System.Linq;

string[] map =
{
    "####################",
    "#..................#",
    "#...##.......#.....#",
    "#............#.....#",
    "#..A.....##........#",
    "#.....#..........T.#",
    "#.....#.....##.....#",
    "#..................#",
    "####################",
};
var agent = Find('A');
var threat = Find('T');

var candidates = new List<(int X, int Y)>();
for (int y = 1; y < map.Length - 1; y++)
    for (int x = 1; x < map[0].Length - 1; x++)
        if (map[y][x] != '#' && IsInteriorCrateNear(x, y)) candidates.Add((x, y));

var scored = candidates.Select(c =>
{
    bool protectedFromThreat = !Los(c.X, c.Y, threat.X, threat.Y);
    bool canPeek = protectedFromThreat && Neighbours(c).Any(n => map[n.Y][n.X] != '#' && Los(n.X, n.Y, threat.X, threat.Y));
    double fromAgent = Dist(c, agent), toThreat = Dist(c, threat);
    double score = (protectedFromThreat ? 50 : -100) + (canPeek ? 20 : 0) - 2 * fromAgent - 3 * Math.Abs(toThreat - 7);
    return (c, protectedFromThreat, canPeek, fromAgent, toThreat, score);
}).OrderByDescending(s => s.score).ThenBy(s => s.c.Y).ThenBy(s => s.c.X).ToList();

Console.WriteLine("point    protected  can peek  from A  to T   score");
foreach (var s in scored.Take(6))
    Console.WriteLine(
quot;({s.c.X,2},{s.c.Y}) {(s.protectedFromThreat ? "yes" : "no"),-9} {(s.canPeek ? "yes" : "no"),-8} {s.fromAgent,6:F1} {s.toThreat,4:F1} {s.score,6:F1}"); Console.WriteLine(
quot;... {scored.Count} candidates in total, {scored.Count(s => s.protectedFromThreat)} protected"); var best = scored[0].c; Console.WriteLine(); for (int y = 0; y < map.Length; y++) { var row = map[y].ToCharArray(); foreach (var s in scored) if (s.c.Y == y) row[s.c.X] = s.protectedFromThreat ? 'o' : 'x'; if (best.Y == y) row[best.X] = '@'; Console.WriteLine(new string(row)); } Console.WriteLine("@ = chosen cover, o = other protected points, x = exposed points"); (int X, int Y) Find(char ch) { for (int y = 0; y < map.Length; y++) { int x = map[y].IndexOf(ch); if (x >= 0) return (x, y); } throw new InvalidOperationException(); } bool IsInteriorCrateNear(int x, int y) => Neighbours((x, y)).Any(n => map[n.Y][n.X] == '#' && n.X > 0 && n.Y > 0 && n.X < map[0].Length - 1 && n.Y < map.Length - 1); static IEnumerable<(int X, int Y)> Neighbours((int X, int Y) c) => new[] { (c.X + 1, c.Y), (c.X - 1, c.Y), (c.X, c.Y + 1), (c.X, c.Y - 1) }; static double Dist((int X, int Y) a, (int X, int Y) b) => Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y)); bool Los(int ax, int ay, int bx, int by) // sample the segment between cell centres { double len = Math.Sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay)); int steps = Math.Max(1, (int)Math.Ceiling(len / 0.1)); for (int i = 1; i < steps; i++) { double px = ax + 0.5 + (bx - ax) * (double)i / steps, py = ay + 0.5 + (by - ay) * (double)i / steps; if (map[(int)py][(int)px] == '#') return false; } return true; }
point    protected  can peek  from A  to T   score
( 8,4)  yes        yes          5.0   9.1    53.8
( 7,6)  yes        yes          4.5  10.0    51.9
(11,6)  yes        yes          8.2   6.1    50.8
(12,3)  yes        yes          9.1   5.4    47.0
(12,7)  yes        yes          9.5   5.4    46.2
( 6,4)  yes        no           3.0  11.0    31.9
... 30 candidates in total, 13 protected

####################
#...xx.......x.....#
#..o##x.....o#x....#
#...oo...xx.o#x....#
#..A..o.@##x.x.....#
#....o#x.xx.xx...T.#
#....o#o...o##x....#
#.....o.....ox.....#
####################
@ = chosen cover, o = other protected points, x = exposed points

The winner is protected, lets the agent step out to shoot, and isn't far to run. Exposed points (the xs, on the threat's side of a crate) score badly no matter how close they are: the -100 makes "protected" a near-requirement rather than one factor among equals.

Tip: Re-score cover when the threat moves, not every frame, and add a bonus for the point the agent already holds. Without that bonus two points with almost equal scores make the agent sprint back and forth between them, the cover-AI version of flickering.

Step 3: use it

Scoring only picks a point. The behavior around it is what players see:

  1. Claim the point so two agents don't pick the same one (a reservation map, or a flag on the point stored in the squad blackboard).
  2. Move there with pathfinding; prefer paths that stay out of the threat's line of sight if you can afford a weighted path cost (see Influence Maps).
  3. Hold: crouch, peek out, fire a burst, duck back. A small behavior tree or state machine per agent handles this loop.
  4. Abandon the point when it stops being cover: the player flanked, a grenade landed, or the crate was destroyed.

Test yourself

Why does the scoring give an exposed point a large negative value instead of a small penalty?

  1. To save CPU
  2. Because a point that doesn't block the threat isn't cover at all, so closeness must never outweigh it
  3. Because exposed points are always far away
  4. It's required by EQS

How does the program decide whether an agent can shoot from a cover point?

  1. It checks whether the point itself has line of sight
  2. It checks whether a neighbouring floor cell has line of sight to the threat
  3. It measures the distance to the threat
  4. It can't decide that

What stops an agent from sprinting back and forth between two nearly equal cover points?

  1. Scoring every frame
  2. A bonus for the point it already holds and re-scoring only when the situation changes
  3. Using more candidates
  4. Lowering its speed