Blog

Article

Posted may,27th - 2015

Enemies' A.I.

Each time we find an enemy in a videogame, we think (or simply act) the most suitable way to defeat it, kill it or even dodge it. Depending on the kind of game we are playing, we will make one decision or another, and once inside the game itself, we will have to change our tactic. But have you ever given thought to what this enemy is actually thinking when he sees you?

In relation to AI terms (artificial intelligence), before being part of Canvaleon I had just had one brief contact with two typical algorithms, which were supposed to make automatic decisions on simple games. Such as a match-4 and a space invader’s one, the second one being easier to take into practice than the first one. In fact, with these two examples I saw the two most common types of enemies (in programming). But I realized that not long ago.

Of course not all enemies have a brain… but they do not need it. This can sound cruel, or so technical, but that is what being a mathematician means: in a specific context or environment, nothing sounds unduly technical.

Therefore, I would make a big distinction between the two main types of enemies: the ones that have a brain and the ones who haven’t. For example, one brainless enemy would be a Goomba, from Mario Bros. and on the other hand, a Pokémon trainer would be a clear example of an enemy with a brain in order to take decisions. We not only have to bear in mind what we see the enemy is doing, but equally as important is what he is actually not doing, as it will be what determines if it is using a brain or not. It can be seen on some of the most popular RPG’s as they are nothing but a machine which follows a predetermined sequence of attacks, not minding what the player is actually doing.

That said, I would like to explain further my experience creating the majority of the enemies that are actually part of the game, and explain one of them more thoroughly.

In "Canvaleon" almost all the enemies have a brain (some are very complex), and we also have brainless enemies (more than one boss can be included here). Obviously an enemy has to react when it sees you, and it has to have more than one possible reaction, adapting itself to each situation and to its own environment and act consequently. Of course everything is restricted on purpose, even if in some situations the enemies may seem foolish or make the player question why it is not doing a specific action that looks the most logical thing to do. Well, maybe it was not possible to take into practice, or maybe we did not want to do it. To decide what an enemy can or can’t do has been present during all the enemies’ creation process. It has to be remembered that it is a game, and its ultimate goal is to amuse, not to be real.

Now I’m going to elaborate on an example of one of Canvas’ enemies, from its “birth” until its final version: the Shooter (the one that always shoots you while you are jumping)

WATCH OUT, SPOILERS! (Maybe this can be considered a guide)

At the beginning, what the Shooter was going to do was clear: It sees you then it shoots you. The way to program the enemies’ brain has been the one from a state machine. To clarify, the enemy has one state (and only one) that determines what it is doing and/or what it is going to do. Consequently, initially the state machine of the shooter, was very simple.

This can be considered a brain, and although it can seem simplistic, the condition “I see you” is a world in its own. But it is truly simple, because here the enemy does not move, it does not follow you and it does not even take into consideration those circumstances where he can see you but cannot hit you when shooting.

Then the next step was pretty clear: to give him movement. Subsequently with that, one of the major problems appears: when do I know when I can move? How do I know if I can reach you when I see you? Will I be able to go back? In fact, working with a 2D space helps us to simplify all of these possibilities. It was clear that we had to delve more deeply into it, but in a way, being able to empathize with the enemy and ask myself: if I was on that particular 2D world, how would I analyze the situation in order to decide if I have to move or not?

Then the capability to detect an obstacle appeared. As a brief summary:

  • If I am moving, I have to look how fast I am and predict where I have to place my next step.
  • If there is no floor under my feet, then I’m going to fall, so I stop.
  •  If there is floor, I look in front of me to see if there is something there. If there is something that means that I am going to hit a wall or any other kind of obstacle.
  • If I do not have anything in front of me, I would look down. If there isn’t anything there I can continue walking, but, if there is something...
  • Then I would have to look up to see if I can jump or asses my options in order to act.

With all these and establishing distances, speeds and making it enjoyable and funny and a challenge at the same time, the shooter’s mind would be something like this:

Foto 1Enemies' A.I.

While I was adding external elements and additional behaviors, the complexity grew because I had to decide between more and more possibilities and it could be possible that something totally external made it change of state unexpectedly. Being “look up and down looking for Canvas” the last change made to its brain, the final “brain” of the shooter would be something similar to this:

Foto 2Enemies' A.I.

Now it is up to the player to memorize all this information in order to know what the enemy is thinking in each and every circumstance.

Now you know what the Shooter is thinking!