Enemy AI, or seemingly smart enemies in a nutshell

Megaban Avatar

Hello everyone! Your virtual safe haven is here to accommodate you once again.

Lately I’ve found some interest for different kinds of AI. I’m almost sure you’re familiar with the acronym, but in case you aren’t, it stands for ‘artificial intelligence’. Basically a program or app can process whatever input you give it and spit out what feels like a self-created response. The ‘intelligence’ part is supposed to emphasise that seemingly independent thinking.

Of course it’s not like AI has its own human like brain. Its ‘intelligence” stems from being taught patterns and cause-effect relationships to predict what could be a good response. This is what lets you talk with Chat GPT or fictional characters on various websites.

There is also the kind of AI that can generate images, music, and even short videos. These tend to be controversial. An AI-created image is not the same as something made by a digital artist. The former is free and art commissions usually need to be paid for. This means the artists and their supporters don’t take kindly to this other option.

Even WordPress has some AI implementation. Like this picture above generated from typing “rainy day.”

I am not opening this can of worms. I will just say that any kind of AI needs smart and careful use. No one will mind if you have a casual conversation or brainstorm ideas with them. Even generating simpler pictures for your own use is ok in my book. Just don’t pretend like it’s something you did yourself or even worse – gain profit off it.

But enough about this rabbit hole, let’s talk about gaming. That’s what I do after all.

Busting ghost patterns

When we talk about AI in video games, we think something along the lines of computer-controlled characters that act on their own without your direct input. Today I’m focusing on hostile characters and their ‘enemy AI’. The threats whose only aim is to give you a game over.

Note that I am no programming pro or anything so don’t expect some super technical analysis from me. All I’m looking at are reactions you can see and some basic reasons behind them.

Enemy AI has a bit of similar general premise to traditional AI programs. In the way that it has some data it bases its decisions off. It’s usually just a set of more or less varied conditions to be met. Sometimes with a little random number generating thrown into the mix. It’s not the sheer quantity of what Chat GPT or Gemini have access to. Mostly aforementioned data and some player-related info.

Let’s take a look at Pac-Man.

Looks and plays very simple, but there is some crucial data going on under the surface.

As you likely know, this classic involves the player controlling a yellow slice of pizza and eating pellets. In the way of this quest stand (or float) 4 ghosts. Blinky/red, Pinky/pink, Inky/blue and Clyde/orange. Each one of them is trying to catch Pac-Man, but has a different approach to it.

All ghosts start a level by heading towards one of the corners each. After a couple seconds they go into chase mode. This is where their strategies diverge.

Blinky is always chasing Pac-Man. That’s because the enemy AI tells it to take the shortest route to his current location, updating continually as the target moves. Pinky aims for 4 spaces ahead of Pac, making it seem like it’s trying to get ahead of you. Inky instead determines its target space by analysing both Pac’s and Blinky’s positions and tries to make the yellow dot stuck between the two ghosts.

Clyde is supposed to be the random one, but it in fact isn’t. Just a little more complicated. If Pac-Man is more than 8 spaces away, the ghost will act like Blinky. Once it gets closer than that, it will default to returning to one of the map’s corners. Once the distance is big enough again, Clyde will be in chase mode again. The combination of two is what makes it seem erratic.

These ghosts are a great base example of how this kind of AI works. They pick their target based on Pac-man’s position, and their general behaviour is based on combination of this and their individual characteristics.

That’s pretty much how each ghost is programmed to act.

It’s also a great example of enemy AI giving some personality to characters by itself. In this case Blinky being impatient or Clyde not caring.

I’m smart… in my own way!

This kind of structure aka target and the instructions AI is given to follow based on it is always there. Bad guy needs to be a threat after all. How threatening it is supposed to be can be dictated by giving different enemy AI more or less beneficial mechanisms.

Take your typical turn-based RPG. In a lot of them elemental weaknesses are a thing. If you want to make some foes more threatening without even changing their stats, you can make them specifically target these weaknesses.

In the first generation—Pokemon Red, Blue, and Yellow—there is a pretty primitive version of what we call ‘good AI.’ The way it works ironically makes the opponents dumber, but that’s because they follow these rules to a tee.

Good AI tries to get this message as often as possible.

There are 3 main rules for opponents to follow, and a few more specific ones. Let’s focus on the former.

The first rule is to avoid using a move that gives a negative status like sleep or paralysis on a Pokemon that is already statused. Majority of trainers abide by that one.

Another is discouraging non-attacking moves on the second turn. Why second turn? Don’t ask me, old games have so many weird quirks.

And then we have the good AI. It’s too straightforward for its own good. It boils down to picking the move with best type effectiveness. Basically prioritising super effective moves, then normal effective, then not very effective over immunities.

Why do I say it’s “so-called good AI” then? Because it ONLY checks type effectiveness. Not only is the check itself wonky because it sometimes ignores the fact a Pokemon can have 2 types, it also doesn’t consider that non-attacking moves don’t do damage.

A famous example of this is one of final battles in Red and Blue – Elite Four Lance. He has THREE Pokemon with Agility or Barrier – two psychic type status moves. If your Pokemon is weak to psychic, they will only pick these moves. Effectively making an objectively terrible decision because good AI sees the condition is met.

A famous example is when thousands of people beat his ace with an underleveled Venomoth just because it was “weak” to Barrier and Agility.

Pokemon Yellow fixed this by giving them better movesets so they can’t waste time with status moves anymore.

There are also some minor AI adjustments usually limited to one trainer such as fellow Elite Four, Agatha, switching her Pokemon a lot. I don’t think we need to dwell on these, just keep in mind they exist.

As you can see, enemy AI can give these trainers some decision making capabilities. For better or worse. At least it leads to hilarious moments sometimes.

Wary fighters

In strategy games enemies are supposed to be a bit proactive.

If we take a general look, it is obvious what the premise is. Once again enemies can act differently depending on their own instructions. Their behaviour is still dictated by who or where player units or other targets are situated compared to them.

I will talk about this within confines of Fire Emblem games as it will be the easiest to explain for me.

In pretty much every game, the most basic kind of enemy AI action is to engage player’s units or their allies whenever they are within their movement range. No matter if this will give them a desirable result or not. You can abuse that by only placing units you want to lure them in safely.

Being aware of where the enemy can reach and get you is a crucial skill. Here it’s the pink area.

Of course just like with the earlier example, there is more than one quirk opposing army has. If there is more than one target within their range, they will pick one that is the most vulnerable. According to them at least.

Let’s say you have a defenseless healer within range of a typical mercenary or soldier as well as an unit that can fight back. They should by all means and purposes target the former. If you have two units similar in strength, but one is at low HP, the enemy AI will likely urge them to attack the weaker one. They will see the kill and go for it.

My favourite instance of this kind of targeting is in the first game. No matter what, most of the time enemies will attack Marth, the main hero, if they can. Even if he will absolutely murder them and there’s a choice to take an easy KO on a cleric instead.

Depending on a game or difficulty, enemies might make some other informed decisions. For instance, if AI sees that they have no chance of doing damage to a unit, be it because of 0% hit rate or dealing 0 damage, they will ignore this unit.

A funny quirk I can think off my head is in Echoes: Shadows of Valentia, and probably a game it’s a remake of as well, being Gaiden. When they are at low health, enemies may retreat to healing spots. Running away like headless chicken even if they could kill someone instead.

One last thing is enemies might have preset directives on how and where they’re to move. A common example is boss units staying in one place no matter what. Effectively having 0 movement. Some others will either actively approach player units or stay in place until they see someone within their range before taking action.

In chapter 8 of Fire Emblem Fates’ Conquest route you have some enemies prioritising going towards nearby villages. If they reach them before the player does, there will be reinforcements. If you reach 3 out of 5, you will be nicely rewarded. There are plenty cases where enemies focus on a particular target like a defenceless ally or a village. All depends on a map.

On the minimap you can see these house icons, denoting villages. This is where some red enemy units are headed.

I could go on and on, maybe even write a separate post about Fire Emblem AI. But I think you get the idea.

You’re within my range…

Moving away from turn-based combat and into more interactive games, it is often the case that enemies have different kinds of reactions to player character’s presence as well.

In a bunch of platformers, adventure games and whatnot it is the case that if an enemy sees you they will attack. This is where something we can call “field of vision” comes in. It’s usually a set area around or in front of them that, when entered, will prompt them to take action.

In Robin Hood: The Legend of Sherwood that dictates characters’ general behaviour. Note that I say ‘characters’ not just ‘enemies.’

Aside from typical soldiers, you have your normal inhabitants, simple people, sometimes rich sometimes poorer. The former relish in running away towards the bad guys. You know, to tell on you and have a bunch of armed soldiers go and try to kill your merry band.

Whenever you travel through Nottingham, Lincoln or even within confines of Sherwood, there will be danger lurking everywhere. Usually if they see you it will result in them running your way to engage in combat. If they’re archers they will come close enough to shoot. You know, they aren’t exactly fit for direct combat.

The fact these enemies generally see within a range in front of them means you can carefully ambush from behind.

Some might however decide it’s not worth it, especially if your whole squad is here and there’s only one of them. They would just mosey out of here and, if possible, do something similar to stupid rich NPCs. Go to their superior and tell them where you are. This would prompt a bunch of soldiers being called to investigate this one spot.

A soldier sending a small army towards you does not have to mean combat is imminent though. You can hide somewhere before they come back or take a vantage point from which you could take some sneaky bow kill. Or distract them with a bunch of other methods.

Some kinds of soldiers have a specific kind of AI as well. The cavalry units I hated with a passion for instance. They can decide to charge at your units from a rather large distance. Makes sense I guess, they’re on a horse and have a larger line of sight. They are also much faster.

When I say charge I don’t mean start combat. I mean literally running at you and dealing direct damage without letting you counterattack. If they knock your units unconscious and they don’t see anyone else nearby, they decide to kill off whoever they have knocked out beforehand.

And here we have bane of my existence. These horse menaces.

This is pretty much how it works in a bunch of games. Enemy sees you, attacks or runs away, might have some extra actions or decisions they can make. Just like with this accursed horseman mentioned above.

Enemy AI can learn???

Before we wrap up, one last curious instance I want to take note of. Amiibo in Super Smash Bros.

If you buy one of the amiibo figurines (sold separately) you can use them to create a so called “figure player” or FP for short. At the first glance this is your typical CPU character. You know, using general enemy AI and stuff. They’re not that simple though.

FPs start off at level 1, and they’re designed to basically do almost nothing. Maybe move left and right, attack once every minute. No I am not kidding. As they get stronger and level up though, they will become more proactive and aggressive. As if a toddler started learning new skills, you know?

In Smash Bros CPUs can have varying levels of “intelligence”. From 1 to 9 usually. 1 is acting like that newborn FP, level 9 is better at combos, has frame-perfect reactions and is generally rather hard to fight. Though, being enemy AI controlled, they can be way more predictable than real players. That’s the nature of non-human though.

Back to FPs, they pretty much rise in these AI levels. Not only that though, as they’ve been advertised to learn from their battles. Which is actually true!… kinda.

It isn’t going to feel like coaching someone else. They’re still CPUs by nature. Yet, they might start developing some new tendencies based on what’s going on during their battles.

You can create your own minisoldiers with some semblance of acquired intelligence.

It isn’t anything groundbreaking. If you start using one move a lot, they may be likely to keep using the same one themselves. f you for instance have a Pikachu and you use down special, Thunder, a lot, the Pikachu FP will be acting like a normal CPU while also incorporating more of this move itself.

In other words, there is some degree of machine learning in here. You won’t get rid of all the stupid habits these FPs have, but it’s still cool to see you have some influence over their playstyle.

Enemy AI is limited and that’s okay

At the end of the day, enemy AI is nowhere near the level of programs like Chat GPT or Gemini. With that said, it’s still a fascinating subject and one your average player might not understand at a glance.

In games like Pac-Man or old Pokemon you might have an idea that ghosts or trainers have certain tendencies. But that doesn’t mean a 10 year old child will know that Pinky constantly aims for 4 spaces ahead of the player or understand why Lance spams Agility, right?

Enemy AI dictates a lot of major and minor tendencies. Whether making them skittish and running on the sight or starting to charge. Even within the same franchise you can have some curious differences, such as being programmed to run to healing spots at low HP or aim for nearby villages instead of player units.

I touched upon the amiibo-borne FPs being able to learn to some extent. That’s more reminiscent of what you typically think of as AI. And it’s not the only instance where this might be the case. Even if it still is in limited capacity.

Enemy AI is meant to be predictable once you get the hang of it. There is a difference between training on thousands of sample data and having to program desirable behaviour for a video game. It’s way simpler, but it works, and makes sure all sorts of players can have fun with their Pac-mans, Fire Emblems and all of that.

You either are blissfully unaware of how enemy AI works, or can have fun exploiting its weaknesses.


Leave a comment

Design a site like this with WordPress.com
Get started