Jump to content
  • Advertisement
Sign in to follow this  

Basic AI for Enemies

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am developing a basic 2D R-TYPE style game using OpenGL and C++. I was wondering what types of elementary AI would be useful for generating computer opponents that are reasonably challenging. Thanks, Steve

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Most games of that type use highly scripted enemy behaviors. For example, enemy ships tend to mostly follow scripted routes, so they move as if they were on a track. The challenge and fun tends to come from the enemies moving in complicated, hard to avoid patterns, possibly while shooting at the player. Some enemies, especially bosses, might randomly choose from a range of scripted actions to perform. They might follow more elaborate patterns, possibly involving probabilistic choices. So to design the AI, you might want to design the patterns you want the enemies to move in, and then just directly program them to move in that way.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
My guess is that FSM´s (finite state machines) would be the way to go in your case. Have a read at this excellent article (perhaps the best ever written on the subject) by clicking on the following link:



Share this post

Link to post
Share on other sites
I can give you some examples of enemy AI routines I used in Darklaga (a vertical shoot'em up, see my signature), as well as the design behind them. To understand how to make a game difficult, you have to understand how the player reacts to obstacles. In shoot'em up games, difficulty comes from the following causes:

  • Reflex-based obstacles require good hand-eye coordination and the ability to react in a split second. Examples are very fast shots or speeding suicide bombers.

  • Tactics-based obstacles require more concentration, because they are crafted in such a way that blind reflexes will not work. Examples are player position during intricate boss attacks, or choosing the order in which to kill the enemies.

  • Strategy-based obstacles usually involve the choice of weapons, or the use of smart bombs.

The common denominator to all these obstacles is the one and only player resource: concentration time. A given player can only concentrate on a fixed amount of entities and concepts in a given time duration.

An isolated entity (a ship, a bullet), no matter how dangerous, should never require large amounts of skill to overcome, because this is simply not fun for the player: acquiring the required skill can only happen when fighting that entity, which means the player will fail a large amount of times. However, the interaction of several entities creates another kind of difficulty: since the player can only handle a limited amount of entities at a given time, player skill will come from the ability to decide which entites are worth concentrating on, and which are not. Difficulty will then come from interesting ways in which entites can be made to interact in order to surprise or puzzle the player.


The human brain has several innate abilities that allow it to handle large amounts of entites. The most important of these is clustering: if several entities are similar enough in their aspect and (simple) behavior, then the brain will be able to handle all of them at the same time as if there was only one. This has several implications on the difficulty of several situations:

  • Shots that move in the same direction are easier to manage than shots fired in random directions.

  • Shots that come from the same point are easier to manage than shots fired from several different points.

  • Enemies that are far away from each other cannot be easily concentrated on.

  • Differences in movement speed, as well as complex non-rectilinear movement, increase the difficulty of managing entitites. However, if all entitites move exactly the same way (even if it is complex), then the effect is lessened a great deal.

  • Colour-coding helps isolate movements. By having all shots that move in a given direction have a certain colour, the player will dodge them with less difficulties.

The effects in Darklaga were obvious, as different kinds of shots were used. In the high-difficulty modes, enemies used both a slow-moving shot type and a fast-moving one. The slow-moving type was initially used to spin a web, through which the player had to navigate. Because the fast-moving shots were almost the same color as the slow-moving ones, the player had many difficulties spotting them because he was concentrated on navigating through the web.

Delayed threats

The next step is the introduction of delayed threats. Indeed, right now all techniques only serve to increase the difficulty of dodging enemy fire, which is an immediate threat. Once an enemy shot is fired, the only solution is to dodge it. However, if a type of enemy is known for firing dangerous pellets, then that enemy is a delayed thread: if it is killed before it fires, then the game becomes easier. As such, destroying delayed threats is a top priority for the player (second only to the necessity to avoid immediate threats). Here are several pointers related to delayed threats:

  • Delayed threats fall into two categories: diversion and sneaking. The former must attract the concentration of the player onto itself to divert him from immediate threats. The latter will attempt to stay unseen in order to unleash an immediate threat of its own.

  • Diversion threats rely on attracting the player's attention, and to do this they use several properties of the human brain. They blink/are animated/have a distinct color, they appear in a clear area where nothing else can block the view, and are usually quite big. Average differences in movement speed are not noticed (but different movement directions are). The reverse is true for sneaking threats.

  • A delayed threat need not be an enemy. A wave of shots that moves towards the player is an immediate threat. Now, hide among these shots a similar-looking shot that moves in the same direction (so it will not stand out of the crowd), but at a smaller speed (so natural clustering will ignore it), and you have an unseen sneaking threat going straight for the player if he did not spot it fast enough to move away. This technique was used for the Blazing Star final boss, by the way.

  • A delayed threat need not even be hostile! By setting free a valuable power-up at the right time (and having that power-up 'leave' the screen after a short time), the player faces the 'danger' of not picking up that item. This is a very interesting diversion system, by the way.

  • Delayed threats that must be destroyed (essentially enemies) are stronger when the weapon of the player does not have a wide spray or auto-aiming. The harder it is to aim at a threat, the more difficult it will be to counter it.

What would be examples of this? Darklaga featured "warping" enemies which appeared somewhere on the screen, waited a short moment, and unleashed a vicious attack at the player. It was very important to destroy these enemies as soon as possible (and they were weak), or they would transform the playing field into hell in space. Another such "delayed" feature was the introduction of kamikaze enemies. These would slowly move down the screen vertically, stop, and suddenly move horizontally to ram the player's ship from its undefended sides. These enemies were mainly introduced to rebalance the otherwise unbalanced vertical laser (a very powerful weapon that ripped through enemies, but which required one to move fast enough to kill the kamikaze before it rushed in).

More diversions

There are even more ways to apply the constatation that the player can only concentrate on so many things at a given time:

  • Darklaga had a special class of enemy that rotated around the player. The enemy would usually do almost nothing (it did not shoot very often either), but it was safe from any attacks until its giratory movement placed it in front of the player's ship (at which point it died). However, a rotating enemy ship is enough to attract the player's attention, and divert it from more important concerns. Not having those enemies be completely inoffensive also helped keep a portion of the player's attention on them.

  • The player naturally concentrates on its target. This can be used to your advantage in several ways. The most obvious is to have the player keep its eyes on an enemy a few milliseconds after its death. Idea: a way to do this is to have a little slot machine appear where the enemy was (which can roll out an instant bonus for the player).
  • Another is to notice that players watch the enemy until it dies (and not until they fire the final shot that would kill it): if the lethal blow is not different from normal shots, the player will wait until it reaches the enemy. Idea: have the player weapon fire "targeted" missiles at the enemy, with missiles being slower and more powerful as time passes; the lethal missile will be very slow. It works even best with a flamethrower, since missiles can be counted.

  • Have threats appear as much more dangerous than they really are. Darklaga featured horizontal walls of enemies. If the player kept firing constantly, they would die whether or not the player was paying attention at them. However, even though it served no purpose, most testers kept their eyes on the enemy wall until it was destroyed, and were hit by enemy fire in the process.

  • Teleporting enemies can be made to teleport behind the player when he isn't looking (because his attention was diverted by something else). And the time spend on dispatching them can be put to use by other delayed threads coming from the other way.

That's all I could remember off the top of my head. But the basic idea is that psychology is the key.

Share this post

Link to post
Share on other sites
Actually, one of the simpliest AI I would come up for this project is when the enemies are moving towards you if they are a given distance to you. When they are really close, they start attacking. If you want to go deeper, take a look at Finite State Machines, Fuzzy Logic. Hope this helps ...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!