Reader small image

You're reading from  Practical Game AI Programming

Product typeBook
Published inJun 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781787122819
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Micael DaGraça
Micael DaGraça
author image
Micael DaGraça

Micael DaGraça is a professional game designer and interactive creator who works with independent video game studios and creates interactive apps focused on the health and pharmaceutical industries. He studied digital arts at the University of IESA Multimedia, Paris, and ESAD Matosinhos. He started his career as a project manager in a small studio and then gradually started working as a game developer by helping other studios to develop their games. More recently, he has been creating interactive content for the pharmaceutical industry.
Read more about Micael DaGraça

Right arrow

Chapter 2. Possibility and Probability Maps

In this chapter, we'll be talking about possibility and probability maps, understanding how and where they are used. We'll also be learning the best practices for creating an AI that reacts to the player and that also chooses the best options, as we look to create a character that can make decisions as a human would.

As we saw previously, video games used to rely heavily on predetermining the behavior of what the AI could do in different scenarios that were either created by the game itself or by the player's actions. This method has been present since day one and is still being used today, making it an extremely valuable method for creating quality AI characters. Before explaining, in detail, what each of the maps do, and before demonstrating how to create them in order to develop good AI behavior, it's always good to have a general idea of what possibility and probability maps are and where or when they are applied.

As gamers, we tend to enjoy...

Game states


To understand how to create possibility or probability maps we need to first acknowledge the principle aspect necessary to create them, which is called game states, or simply states. We call game states to the actions that are predetermined throughout different occasions in the game, and those actions can be applied to both the player or to the enemy character. Some examples can be simple behavior, such as run, jump, or attack, and those states can be expanded a little more, for example when the character is in the air and cannot attack or if the character has low magical energy and cannot perform a magic attack. In these cases, the character goes from one state to another or can't perform one if it's doing another.

Possibility maps


Now let's take a deeper look at the possibility maps that we encountered in the examples in the first chapter, from the chess machine to the Metal Gear Solid video game. As we can see, it's a technique that is still being used today, and it is almost impossible to create a game AI without it.

As the name suggests, possibility maps allow the programmer to define the possibilities available to the player or the AI character within the game. Everything that is possible inside the game needs to be planned and coded, but what happens when you allow the character to do a lot of things can they do them all at the same time? If played during different stages of the game, can they react in the same way at all of the stages? To allow, and restrain, the possible actions, we also need to think about the possible scenarios that can occur in the game, and when you put all of that together it's called a possibility map.

How to use possibility maps

Let's take a look at a simple example of...

Defining the states


Let's define what triggers each state and how the AI should choose the correct state in different scenarios. The PASSIVE state will be the default state, and the game will start in that position until the player comes across our character. The DEFENSIVE state will be used in two different situations if the player comes from the right side and if he has already confronted the player and has low HP. Finally, the AGGRESSIVE state will be activated if the player comes from the left side or has already arrived at the middle area:

public class Enemy : MonoBehaviour {
private int Health = 100;
private bool statePassive; 
private bool stateAggressive; 
private bool stateDefensive; 
private bool triggerL; 
private bool triggerR; 
private bool triggerM;
// Use this for initialisation 
void Start () { 
 statePassive = true; 
}
// Update is called once per frame 
void Update () {
 // The AI will remain passive until an interaction with the player occurs 
 if(Health == 100 &&...

Probability maps


A probability map is a more complex and detailed version of a possibility map because it relies on probabilities in order to change the behavior of the character, rather than a simple on or off trigger. Its similarity with the possibility map is that it's also required for planning ahead the possible states for our character. This time, however, we add to it a percentage, using which the AI will calculate what behavior he will be using. Imagine the next example using the situation that we previously created for the possibility map our enemy AI could be more aggressive in the daytime than at night. For that, we create a statement that explains to our enemy that if it's night time, there is a lesser chance of seeing the player character, and for that reason it will choose a more defensive approach instead of an aggressive one. Or, simply, we could define that our enemy calculates the probability of killing the player simply based on the distance between the two characters...

Summary


This chapter has described possibility and probability maps and we have learned how to let the AI take decisions for itself according to the player actions. Possibility and probability maps is the foundation of an AI character and now we can explore this technique to create new and unique artificial intelligence for our games. In the next chapter, we'll learn how the AI should behave according to the different options that it has on that moment instead of using a possibility map. We want the character to analyze the situation and think about what to do, taking in consideration many factors, such as health, distance, weapon, bullets, and any other relevant factors.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Practical Game AI Programming
Published in: Jun 2017Publisher: PacktISBN-13: 9781787122819
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Micael DaGraça

Micael DaGraça is a professional game designer and interactive creator who works with independent video game studios and creates interactive apps focused on the health and pharmaceutical industries. He studied digital arts at the University of IESA Multimedia, Paris, and ESAD Matosinhos. He started his career as a project manager in a small studio and then gradually started working as a game developer by helping other studios to develop their games. More recently, he has been creating interactive content for the pharmaceutical industry.
Read more about Micael DaGraça