So, your AI routine is running. Taking into account for the enemy field of view, light levels, camouflage, distance, cover, and general threat awareness (like, are there any suspicious dead bodies lying around?) the program has determined that the bad guy has, in fact, spotted the player. Time to start shooting.
Not so fast.
Just as bad guys will always know where the player is unless you tell them not to know that, they will always hit the player flawlessly unless you make them miss. Left to their own devices, bad guys will be able to ding the player right between the eyes with a Luger at a half mile. Auf wiedersehen, dummkopf!
Like figuring out if you can see the player, figuring out if you can hit them is likewise going to keep your programmer busy for a long time.
You want to shoot the player. But where do you aim? The head seems a likely spot. But remember the I-can’t-see-you-you-can’t-see-me problem we had with detection. If the player is crouching behind cover with their butt sticking out, the AI needs to be smart enough to forgo the headshot and aim for the sticking-out stuff. If not, players will eventually realize they can take cover behind ridiculously small things – anything that can cover their head.
But having enemies operate under the same rules as the player means the AI will need to be aware of how each weapon should be used and how to correct for it. This isn’t as much of a problem for linear indoor tunnel games – just put the shotgun guys in small rooms and the snipers up on catwalks and you’re all good. But once you’re trying to make a freeform game where the player can approach an encounter from many different angles you can no longer dictate how the battle will play out, and thus you can’t just hand your AI a random gun and trust it to Do The Interesting thing.
Some weapons are fine for running around and spraying bullets, but others require that you stabilize yourself. This variety makes the weapons more interesting, and lets players have a lot of freedom in selecting arms to suit their play style. But this depth and variety comes back to bite the developer when it comes time to write the AI. It will look stupid if the AI is using the sniper rifle by shooting from the hip, or trying (and failing) to snipe you from the guard tower with his sidearm. Now the AI needs to understand effective range and be able to weigh all sorts of very hairy tradeoffs. To wit:
Hmm. The shotgun isn’t particularly useful at this range. I should get about twenty meters closer. But that means leaving cover and rushing the player. Is it worth it?
That last question is an incredibly complex value judgment. The AI needs to weight just how much it’s getting out of the weapon versus their chances of survival if they move. I doubt any game has really solved it, which explains why sometimes the bad guys fire away uselessly at long range, or dash out into the open and certain death. When they make the wrong decision they come off as either morons or lemmings.
But this type of decision-making falls more under “behavior” than “targeting”. I’ll come back to it later in the series.
|The bot in the distance is using a gun which shoots green orbs which travel in a perfect line without ever dropping. Note how scattered they are. A human would have to shake their mouse very fast to get this effect.|
Beyond this, how the AI aims depends a lot on what sort of game you’re making. Are you going for a “tactical shooter” type simulation like Operation Flashpoint, STALKER, or WWII game? Or is this a run-and-gun action game like DOOM or Max Payne? (FEAR is an interesting example, since the AI behaves like they’re in a tactical shooter but the player can act like they’re Sgt. Bullet Sponge. You’re Duke Nukem up against the foes of Operation Flashpoint. This might be a big part of the appeal of the AI in that game. They act like like a plausibly realistic squad, but you can charge in using slo-mo and see them at work instead of fighting them from a distance where their teamwork isn’t as visible.) In a more action-styled game, you actually want a bit of unrealistic behavior because you’re trying to keep up the action-movie feeling of the hero rushing through a hail of bullets. The bad guys are supposed to miss, up to a point, but they need to just “nearly” miss. Ideally they should be using lots of tracer rounds, and when they do decide to miss they should be aiming in such a way that their bullets still pass somewhere in front of the camera or strike nearby objects that will do interesting things. Glass bottles shattering, wood splintering, pipes leaking.
|Ignoring the fact that the officers seem to be firing into the crowd, or the fact that they shot up the wall for no apparent purpose, and the fact that one of them seems to have taken cover in FRONT of the squad car… Actually, that’s an awful lot to ignore. Anyway, Officer Dumbass (right) is being pretty relaxed about shooting over the head of Officer Fodder. (center)|
This is one that games have yet to tackle (among games I’ve played, anyway) but it’s a major annoyance of mine. A bad guy pins you down behind some cover and then just plinks away at your hiding spot. Endlessly. Even if you hold your position for five minutes, the AI will just drill away at you without ever worrying about running out of ammo. And when you finally DO get out and kill him, he has nine bullets. Maybe he shot at you for five minutes, or maybe you took him down in a surprise attack. Either way, you always kill him nine bullets short of empty.
It would be nice if suppressing fire started out intense, then fell of to bursts, then eventually tapered off to single rounds. (Depending on the weapon.) You do want the bad guys to try to pin down the player, but you don’t want them to abuse their gift of infinite ammunition. They should at least pretend they don’t have infinite ammo. (And actually running out of ammo is out of the question. It would make the game very exploitable, and programming proper behavior for a weaponless AI would be very difficult. You don’t want to spend ages developing and polishing a situation you don’t want to have happen in the first place.)
And while we’re on the subject of cover fire, AI is usually very bad about cheating when you go into hiding. If you duck behind some crates and slip out of the room, the AI generally won’t continue to douse the crates in suppressing fire. Instead, they move to give chase because they’re clairvoyant and know you have left the room, even if they can’t see you. There was one game (I think it was FEAR, but I’m not positive) where the AI didn’t cheat like this and I was able to flank them. I came around and found a group of guys blasting away at the spot where they had last spotted me. This was a very impressive moment, and blindsiding them was a right and proper reward for my quick thinking.
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.
Here is a 13 part series where I talk about programming games, programming languages, and programming problems.
The No Politics Rule
Here are 6 reasons why I forbid political discussions on this site. #4 will amaze you. Or not.
Gamers Aren’t Toxic
This is a horrible narrative that undermines the hobby through crass stereotypes. The hobby is vast, gamers come from all walks of life, and you shouldn't judge ANY group by its worst members.
Are Lootboxes Gambling?
Obviously they are. Right? Actually, is this another one of those sneaky hard-to-define things?