Inasmuch as the bad guys are a part of the program you’re running, the bad guys always know where you are. The game knows where you are in the same way that a puppeteer knows where the puppet is. Your position in the game is just another variable it’s constantly tracking as part of running the show.
But unless you want every bad guy to begin homing in on the player the moment the loading screen vanishes, you need to pretend you don’t know where they are. Detection is the system of deciding when to stop pretending you can’t see them. By default the bad guys can draw a bead on the player and ding them between the eyes at an unlimited distance the instant a clear shot becomes available. AI is used here not to make the foes more potent in combat, but to make them less so.
This usually begins with line-of-sight checking. Calculate a line from the AI in question to the player. If that line hits (intersects) anything before it gets there, then something is in the way and obstructing the AI’s view of the player. A LOS check used to be kind of a big deal, but these days they’re generally considered to be an inexpensive operation. Still, there is a cost to doing the check and thus an upper limit on how many you’ll want to do a second.
Sounds easy, right?
That’s the state of detection as it existed around Wolfenstein, but once you’re dealing with complex 3D environments with varying light levels and trying to make believable enemies things get really hairy. The goal of your AI is to not make a fool of itself by being too perceptive or not perceptive enough. Nothing will distance a player from the game and break immersion like having a bad guy perfectly track them while they are completely hidden behind cover, or see them a half mile away. In the initial post in this series I mentioned AI bad guys that had “super-senses”. Counter-intuitively, super senses are the result of exceedingly stupid AI. In effect, it’s too dumb to know it shouldn’t be aware of you yet.
That LOS calculation we talked about a second ago is fine for simple scenarios, but it falls apart quickly in anything less than a barren cube-shaped room. What part of the player is the bad guy trying to look at? Sure, you project the line from the bad guy’s eyes, but where does it go to? The player’s eyes? If so, then you end up with the hilarious “I can’t see you so you can’t see me” logic. If you’re standing in the open but you have something hiding your eyes, then you’ll be invisible to the bad guys. Picture Chris Farley hiding behind a lamp.
So, the AI needs to do several LOS checks to various parts of your body. It needs to be able to see the top of your head poking up from behind the couch, your legs sticking out from behind saloon doors, and your shoulder poking out from around a corner.
Field of View
Obviously AI should not have eyes in the back of their head, and there needs to be some limit on how far they’re allowed to see. Generally, you see quite far and in great detail directly ahead of you, but this high level of perception falls off quickly at the corners of your vision. (The edges of our vision see mostly motion and colors and changes in brightness, and aren’t very good at picking out shapes.) People are also generally very bad at looking up or down, and their vertical vision can often be even worse than their peripheral vision.
Now, most people intuit all of this, even if they don’t know it. We usually have a sense of when other people should be able to see us, and deviations from these expectations are jarring. AI that can detect an unmoving player out of the corner of his eye at fifty meters feels like a rotten cheater. An AI that only sees a 90 degree cone in front of him feels like a bumbling moron. (Although the latter is often forgiven for the sake of being sporting. Because they’re viewing the world through a monitor, the player can only see a ninety degree cone themselves.)
This has become more important since the introduction of HDR lighting in games. It’s now possible for the player to be blinded to areas with vastly different light levels. If you’re standing in the sun, you can’t see into the dark train tunnels. If you move to the tunnels, the area outside becomes too bright to make out. Players expect these restrictions to apply to AI.
But this one can get really tricky, and I don’t think a game has done it right yet. I first raan into the problem in Thief, when I’d crouch in a doorway. I’d be in darkness, but the room behind me would be well-lit. The guard in front of me should have spotted me instantly. In real life, it would be safer to stand in the lit room than to stand in the dark with the light directly behind me, forming a perfect back-lit silhouette. Eventually players learned to rely on the visibility gem instead of their common sense to judge how visible they were.
I suppose it would be sensible to extend any LOS check that succeeds. That is, if we draw a line from the bad guy’s eyes to (say) the player’s shoulder and it actually hits, we then continue to follow the line until it hits something. We can then compare the light level of the player to the light level of the thing stuck, and have any variance in those values count against the stealth rating of the player. This sounds reasonable, but I suspect that we’d see the failure modes move to somewhere else if we did this. Definitely one that requires more thought.
Just because the LOS check passes doesn’t mean the AI should truly “see” the player. If I’m wearing camo and crouching in the underbrush, it feels very unfair to be spotted by the AI. However, if I’m just behind some furniture it feels perfectly reasonable to be spotted.
So the LOS check needs to do more than just figure out if I’m visible. It needs to examine what sort of things are obscuring me, how much of me is visible, how much I should blend with those things, and if I’m moving. This is complex and it requires that your artists get involved with the AI system by tagging objects according to how camo-friendly they are. A fern? Very camouflaging. A beach umbrella? Not so much. I’ve never run into a game where I thought this was done well. In Crysis, the enemies sometimes had foliage-penetrating X-ray vision, and being spotted while sitting still in a thicket is no fun.
This is the most complex one, and the most annoying to players. Generally soldiers don’t immediately begin shooting to kill the moment they see someone milling about in the distance. There is nothing more tedious than the army of clairvoyant soldiers who can tell friend from foe at two hundred meters with enough certainty that they are willing to shoot to kill.
However, if I’ve already gunned down half the guys in their base then I do expect this level of paranoia from the AI. Uh, unless there is no way for this particular bad guy to have heard about the attack, in which case he should go back to be more curious than trigger happy. Unless he’s heard shooting. Or he’s spotted me in a forbidden area where only an enemy would go. (I think it’s fair to assume that anyone coming out of the sewers or air ducts is a troublemaker. (Unless there are civilians around.))
Aren’t you done with the AI yet? We’re going gold in a few weeks.
Having these systems fail is often worse than not having them.
In one of the Cat in the Hat books, the story features a red mess that the kids keep trying to clean up. Each attempt to do so just ends up moving the mess from one place to another. I’m sure this was an environmental message of some sort, but it’s a nice metaphor for the problems that AI programmers are up against. Every time you try to fix a hole in the AI you just move the failure mode to a new situation. Adding better complex occlusion just makes the field of view problems stand out. Fixing those reveals the problems with camo. Fixing that (if you can) will expose the shortcomings in subjective perception. And so on.
Silent Hill Origins
Here is a long look at a game that tries to live up to a big legacy and fails hilariously.
Game at the Bottom
Why spend millions on visuals that are just a distraction from the REAL game of hotbar-watching?
The Plot-Driven Door
You know how videogames sometimes do that thing where it's preposterously hard to go through a simple door? This one is really bad.
Batman: Arkham City
A look back at one of my favorite games. The gameplay was stellar, but the underlying story was clumsy and oddly constructed.
Video Compression Gone Wrong
How does image compression work, and why does it create those ugly spots all over some videos and not others?