Easy AI?
I've spent the weekend trying to put together an AI for my game. Having only 3 days to do AI for a RTS-style game is not an easy task and the result is a little unimpressive but it works.

I'm sure that most of you won't want to spend too much time on complicated AI, especially if it's only for a simple platformer, so here's a guide to making simple AI. Be warned that your AI would only be about as smart as a millipede, but often that's all you really need. You'll note that you could make something close to Half-Life 1 quality AI with these methods. My AI is currently smart enough to beat most decent players at my game, which is a lot to say for a RTS game!


Only one thought at a time
The first, simplest thing you should do is to set up a counter to write what the computer is currently thinking. This is how a human thinks. Let's take a shooter for example. When the player sees an enemy, he'll have these options:
1. Shoot
2. Run for cover/hide
3. Call backup
4. Throw a grenade

But he won't do all of them at once! His brain will pick one according to experience or at random. Or he'll do a routine, e.g. 2 -> 3 -> 4 -> 2

Same thing goes for AI. Cut it down into as many small pieces as possible. If you want a simple AI, randomize it. If you want an AI with personality, lean the randomization to favor a certain tactic (an aggressive AI would do Shoot 50%-70% of the time, a scout would do (3) more often). If you really want a smart AI, give them tactics. More often than not, there are better tactics than the ones you designed for your game. Your players will find them. Ask your best players to design tactics for you. If you're lucky enough to have someone write a walkthrough/guide to your game, use it!

Also, remember to clear the "thought counter" after a certain period of time has passed. If you leave him running for cover but a bug prevents him from stopping, he'd be well off the battlefield after a while


Priorities
Give them priorities. In my game, Chaos Wave, I've made them focus on economy, then defenses, before moving on to offense. It's a RTS, you always need economy! But for the shooter above, you might want the rocket launcher guy to attack tanks first, then other soldiers, but you still want him to attack soldiers. This is where priorities come in.

In my case, the computer was so focused on attacking, she neglected to save some extra resources for upgrades and defenses. So, I made it check to see if the economy and defenses are done. I used 3 flags:
3 - Economy done
4 - Defenses done
5 - Clearance for attack

Flag 5 is there because sometimes I don't want BOTH flag 3 and 4 to be on. If under attack, I'd want them to focus on making defenses and mounting a counter-attack, rather than replacing peasants. So, sometimes, it's perfectly fine to prioritize an attack when the economy hasn't been finished.

This same concept should apply for many other games too


Knowledge
In Chaos Wave, there's a 5% chance that some resources won't be generated at all. That's a poor chance, but what if I tell the AI to keep resource collectors a high priority? A similar case would be if you told the AI to guard the bridges, but it was destroyed.

The AI will just be sitting there and scanning for the non-existent bridge or resources. This is where AI knowledge is vital. Humans learn apparently without thinking, if you look out the window, you can automatically assess whether it's going to be hot or cold, sunny or rainy. If you look at the shadows, you'll even automatically estimate the time.

The AI knows nothing. Nothing. So I set aside a few AI knowledge flags to record any bits of knowledge. This works best with knowledge about the map and terrain, rather than changing ones, such as the location of enemy outposts or the amount of soldiers the enemy has.


Use your senses
Here's a trick many people forget. A lot of people tend to give the AI unfair knowledge of the land to give them an advantage. But sometimes that gives the player an unfair advantage instead ("Hmm.. if the AI is going there, there must be gold hidden there!").

Try to only use the same things the players can sense. If there's a fizzling sound before an explosion, use that, instead of some explosion counter. Also, don't be ashamed of use newbie-ish conditions like, "Check for Active1 in (zone)". If the player can see it at once, why shouldn't the AI?

In Chaos Wave, the yellow AI has to avoid building things in the rain which dissolves them. I could make it check the rain timer, but instead I decided to code it in that the clouds would become darker when it's raining/about to rain. The AI builds only when it's not cloudy. It made the game effects much nicer and made the AI feel a bit more real.


Use reactions, not pre-determined code
One trick people usually do with AI is to give them an unfair advantage, such as shooting through walls, jumping higher than usual, etc. It may be a retro tradition, but it's still lazy coding!

Let's face it, an AI with an unfair advantage is not always fun. It may be the tradition, but back then, people had to work really hard to squeeze in advanced AI with limited processing capabilities.

Beating an AI which uses the same rules you do is always a lot more satisfying. The human always has the advantage of cunning and the AI always has the advantage of faster processing power. Faster processing power is a huge advantage in computer games. Examples:

RTS - The computer can do all the actions at once, decide which resources to collect, what units to build, what tactics to take in 1/100ths of a second. It's disadvantage is the lack of ability to think. But heck, it's an RTS.. when has a RTS ever had strategy?

Platformer - The AI can detect your moves as soon as you press the trigger. It can dodge and avoid sudden moves in the blink of an eye. The only thing keeping it moving damn fast is the physical limitations that you've given it like animation speeds.

Action - Now this can be brutal. Lacking thinking skills save for pathfinding, the AI is able to perfectly calculate headshots before you even blink. A little smarter and they're capable of dodging gunshots before you pull the trigger. AI in Counter-Strike has always been blessed and cursed with both accuracy and pathfinding.

Note that there is no mention of turn-based AI here. The only advantage a turn-based AI has is that they can keep a large database of human moves (especially in chess). This also applies to all of the above.

Getting a challenging AI is simple. Just let it do the same things the human can do (a little more, a little less). And be sure to use its superior calculating speed to its advantage!


Learning AI
The most difficult trick in the basic AI book is the learning AI. I rather like this method because it simulates artificial stupidity rather than intelligence, which is fun. Interestingly, it's not difficult to make learning AI. The challenge is in making the AI make use of its new knowledge.

To make learning AI, you just give it knowledge to record. The most obvious choice for this are first-person shooters and strategy games. Basically, you record the outcome of what happens when the AI chooses a decision.

For FPS games, you could have the AI choose between going left and right at a particular junction. He could choose to camp or to storm in. He could choose between sniping, using a big gun, or a small & cheap gun and a few grenades.

Then you compare the record with how successful he's doing. This varies widely and is up to you. With action games, it's usually the amount of kills or the team success (Also, note that you can also pick two AI personalities here - the teamwork type and the solo Rambo type)

After a few successes with one type of tactic, just keep using it until it starts to fail. That is what a real human does. Once the tactic starts failing, reset the learning AI all over again.

If you want to push it just a little further, give profiles for each player when they log into your game. Then record the AI's success against each profile seperately.



Why not AI?
Now there are times when you don't want AI. In fact, it depends on your type of game. Aside from strategy games, you'll want 90% of your enemies to be stupid, while the other 9% are of equal strength to the player with human-like AI, and the final boss being stronger and faster and the human. Having a lot of dumb enemies makes the average ones look smarter.

Making a lot of smart enemies becomes annoying more often than not. But I'd encourage you to put in a proper AI system - with artificial stupidity. Use the senses and reactions as mentioned above.. but don't make them think. Make the dumb AI just button mash or whatever like a newbie who doesn't know the controls.

Then use your full AI coding skills on the bosses and the best of your AI code on the final boss. Also spruce up the final boss a little to make in unfair



Have fun making your game!