Wednesday, 16 January 2013

AI Breakdown - Part 1

In this update I showed the AI I had been working on for the game, I felt it was important to get the AI to a reasonably intelligent level, as a substantial part of the game will be combat. This post is simply going to explain how I went about making the AI.

Disclaimer: This is simply how I made the AI for my game, it is by no means the only way, and it's definitely not the best. This post is hopefully to show people that an expectable level of intelligence for AI can be based on a relatively simple system.

Research
Research is important for AI, look how others have gone about it. there's no point re-inventing such a complicated wheel. With the videos try and look for the specific actions the AI's perform.


Overview
The AI uses a state machine system(article 2) with a little bit of algorithmic AI thrown in. the main tasks are organised into states, but within those states are multiple potential actions. These actions are chosen based on the environment and the AI's interaction with other AI's and the player.

States - overview
Constant - this runs perpetually whilst the AI is "alive", it defines actions that the AI must always be running.
Idle - When idling the AI simply waits for instructions.
Turtle - Similar to idling but occurs when the AI is targeting an enemy.
Attack - the AI acts aggressively towards it's target.
Defend - the AI act's aggresively but stays within a certain area
Stunned - the AI enters this state when they are "killed", this state always results in the AI being destroyed.
An AI in the turtle(left) and attacking(right) state.

Active?
In Article 1 you may have read how the Halo 2 AI only activates 1 enemy at a time, this not only saves processing time but helps to slow the AI down so it doesn't instantly react to everything. My AI uses this system as well, as it negates the need for artificial timers to simulate reaction times as well as the aforementioned performance bonus.

Path-finding
The AI's path-finding is not as precise as implementations such as A* , if you put the AI in a maze it would get lost. But! this leads to what I feel is more realistic movement. instead of instantly finding the shortest possible path to the player, they "guess" their way to the player!

There are 3 Functions used for path-finding.

  • closest node
  • path-find to node
  • furthest node (rarely used)
In order to understand how these functions work it's important to know how the node system works.



When the scene is first loaded a script loops through all the verts on a nav-mesh, at each point a node is added, each node then casts a ray to every other node in the scene, if the ray is unblocked a "link" is made between the two nodes(each node has a dictionary of it's visible nodes).

So, you end up with a system of points with paths to other points. the AI then simply use the 3 functions mentioned previously to select the appropriate node. when the AI reaches a node, it is set as occupied by that AI


  • closest node - This looks for the closest visible node to the AI's target, and is often used when the target is visible
  • furthest node (rarely used) - Works exactly like the closest node, but the furthest visible node is picked.
  • path-find to node - This one is slightly more complicated, it is called when the target is out of site, instead of only looking only at visible nodes, it looks at the nodes stored for the visible nodes. e.g.




Here node A would be chosen, because even though the player isn't visible, node A can see the player, therefore node a is selected. the actual algorithm uses three such links. 

Here even though B can't see the player, node A can, and as A is visible from B, then B is chosen as the best node to take.

The player isn't stored as an object! it is actually stored as co-ordinates. so even if the player was hidden from the view of A, the AI would make it's way towards the last known position.

(currently it treats the last known location as the player so attacks it, this is a bug that needs fixing, either by switching to the defend state, or perhaps searching for the player).

Part 2 will explain how each state works but in more detail, it will also explain the actions associated with each state.