We are not about to code anything yet. This section is more about providing general ideas on how to develop your AI in your games. Most of the tips will fall into the common sense box but keep in mind, some of us are still just starting their game development journey.
It is said that the AI found in games is not real one but more a mix of techniques related to AI. The point is to create the illusions that the NPC is able to act by himself, think, interpret its surrounding and reason only to fool the player. With human knowledge and reasoning, one can create an artificial being that would think and act the way it is wanted to.
Note that it is not advised to create a game where the human is outsmarted as it would then end up in a situation where the player is unable to finish the game.
To fulfill this goal, the NPC is implemented with codes, algorithms that will help it to
“understand” its world. The senses of the NPC are based on vectors and sensors.
According to Van Lent, the NPC constantly executes three steps that are kept simple so that the NPC does not outsmart the player:
- perceive (accept information about the environment – sensor information)
- think (evaluate perceived information & plan according actions)
- act (execute the planned actions)
Those are the main topics related to AI in general.
But "Game AI" used in game theory is not totally similar to this as it is mainly concerned with various approaches to tree search. But because of the large amount of computation and resources it requires, its use is still quite limited.
For games dealing with large numbers of NPCs that need to act simultaneously, this is not always possible. Since NPCs have been used in games, AI of some kind was implemented so that they would act as humanly possible but this is hungry for resources.
As computers became more powerful and games grew bigger, background character and creatures were added to enrich the virtual game world without actively contributing to the plot of the game, they just contribute to the realism of the game.
AI and system requirements
The quality of graphics was for long the main feature for selling video games, the Super Nes put out front the quality of graphics and sounds but both are not enough anymore to call it a good game and this has brought the industry to develop not only how we see and hear but also what is there and what it is doing. If the behavior of NPCs appears natural, they seem to be more life-like and real which is a crucial factor for the success and popular acceptance of a computer game.
Then came along a new problem, better hardware and software are required for those new types of game. Hence some techniques cannot just be implemented as they would require too long time to get in action, making the NPCs too slow to react. Gameplay is affected if the player has to wait for the NPC to engage.
Another problem that is closely related to the real-time requirement for games AI is the fact that the AI has to share the computer’s processing resources with the rest of the game which will include graphics, input processing, sound processing and possibly even networking.
In early times, NPCs were not given a real AI, looking at Super Mario Bros or games like
Double Dragon, the NPCs were merely thinking. They had a goal and would stick to it. The brown toad from SMB were just walking until they hit a block or a wall, same for the ducks and as for Browser...well he was jumping and spitting balls of fire. In Double Dragon, the enemies ran straight at you and start fighting, only if they wouldn't wait in line to fight the HP.
Needless to say that AI was not given a large spot and was therefore allocated only little processor time.
Arrival of graphic card
Hopefully arrived the 90's and with it the graphic accelerators so more elements of the graphics were redirected onto dedicated graphics hardware, AI could then be allowed higher priority and with it additional resources. At first, CPU budgets for AI exploded and a number of games spent up to 30% of their processor time doing AI calculations, but this has now leveled off at about 10% of CPU time.
Problems that an artificial agent has to deal with are numerous and depending on its virtual environment, nevertheless some of them are more common and restricted to:
- pathfinding / path planning
- decision making
- steering / motion control.
The first attempts of game Ai
Thinking of first games back in the 60's such as Pong, there was no trace of AI in thosefirst attempts, only patterns and algorithms.
First basic AI trials came with Pursuit and later on with Qwak both by Atari or Space Invader from Taito in 1978. Then, Star Trek appeared, the game was based on a scripted text base where enemy movement depended on stored patterns.
With the arrival of microprocessors, improved computations allowed faster and more complicated computations.
The first game created with so called AI was a computer controlled opponent chess game. It was using a large database of probabilities, depending on the actual positions, the computer could foresee the different states and choose the most appropriate.
The computer was still limited to the database it was implemented with. Is this real AI could be asked then.
But this was a first step even though it was quite slow and requiring a large amount of computer resources and was nothing compared to the capabilities of today’s computers which can analyze millions of possibilities per second.
The classic arcade game PacMan came second in line.
The big progress was to make the player believe that the ghosts hunting him are intelligent. In fact this perception of group-intelligence is only an illusion.
This misperception was simply created thanks to a slight variation in the algorithms implemented. This is called weighted randomness.
In this case, the ghosts would not all follow the same route through the maze and whenever the ghost reaches a junction, then it decides whether it should change its direction or not - sometimes the ghost does so to move in the direction of the player, sometimes it chooses a random direction.
That is when we come up with weighted variation, a second approach to AI.
One ghost may move in a random direction 75% of the time and in the direction of the player in the other 25%. Another ghost would have the random choice of direction weighted at 50% of the time, etc.
The result of this simple method is a personification of each of the ghosts by the player as he will perceive the ghost's behavior as that of an intelligent character.
A/ Finite State Machine AI's
During the '80s more complex games showed up, featuring streams of new and different genres. At the beginning of the '90s, the gaming scene gradually began to move away from AI basics (basic and heavily scripted movement), which practically defined the Atari/C64/NES era. The next generation of gamers hailed the coming of more advanced gaming platforms, including the Amiga, new-generation PCs, the SNES and so on.
Games were already developed using a new system for establishing in-game AI. This system denoted the utilization of appropriate AI tools such as finite state machines (FSM). FSM’s are the most commonly used type of AI in games.
Logical states are arranged to control the behaviors of the NPCs-one state per possible behavior and one state at a time using Boolean value-true/false, active/inative, on/off. A trigger is needed to switch states, the transition happens when a set of conditions are fulfilled. A FSM can be quite simple and easy to create and implement.
The main drawback of FSMs is that they can become very complex and hard to maintain, while on the other hand the behavior resulting from a too simple FSM can easily become predictable. To overcome this problem sometimes hierarchical FSMs are used.
These are FSMs where each state can itself be a FSM.
B/ Fuzzy State machine
Using fuzzy logic instead of boolean logic resulted in Fuzzy State Machines. .
The advantage sets in the holding of intermediate values. FuSMs are not limited to 0/1 or
on /off so that at any time more than one state may be active and to some degree be on and off. This makes the construction of FuSm slightly harder to create but the possibilities of simultaneous active states reduce the predictability of the NPC's behavior.
It also reduces the complexity of the state machine, as a wider range of different
behaviors can be encoded with fewer states. FuSMs are a relatively new games AI technique that can be used in almost all of the areas in which FSMs are usually found.
C/ Machine learning & machine intelligence
In recent years, it has been more frequent to see the use of AI techniques that involve machine learning in games to achieve emergent behavior. It doesn't take a lot of work to implement such systems but the back draft is the impossibility to know what is coming next making them unsuitable for many games. Also, it could happen that learning new algorithms might create a NPC behavior that instead of looking smarter results in behaving more stupid.
D/ Neural nets
Neural networks are a primitive simulations of animal brains in which the neurons are modeled using nodes that are interconnected which allows the network to learn and improve itself. Using a neural network can enable games to adapt to the way that the player plays by updating itself during gameplay.
Neural networks are used in strategy games but they have also been successfully implemented in adventure games or action games like “Heavy Gear” in which the robots controlled by the player use neural networks to improve its skills.
E/ Decision trees
Decision trees that grow as they learn new information are another machine learning method that is used in computer games. It is a decision support tool that uses a tree-like graph or model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. Decision trees are commonly used for decision analysis, to help identify a strategy most likely to reach a goal. Another use of decision trees is as a descriptive means for calculating conditional probabilities.
They are one of the most reliable and robust learning methods available and usually the preferred choice if a game AI requires predicting future outcomes or classifying situations.
Results are stored, allowing it to “remember” the best cause of action in case a similar situation is encountered in the future.
F/ Evolutionary techniques
Evolutionary techniques are the least often used machine intelligence methods. A basic initial set of problem solving strategies is usually evolved over time using a range of selection methods as well as random mutations, which are then evaluated until an optimal solution is found. These solutions are usually very robust and reliable but they can take time to get to a good result which makes them unsuitable for real-time games.
A/ Parameter tweaking
The simplest way for modifying AI behavior is by modifying the parameters or rules that are used internally by the game AI. There are a number of games that allow to do this – some games have graphical user interfaces to make this as simple as possible.
B/ Plug-in interfaces
Some games like Quake contain software interfaces that allow plug-ins to be written that can change the AI of NPCs in the game.