Pathfinding
In my previous blog post I looked at Dijkstra’s algorithm and A* search algorithm. Below I will explain how each of these could be applied to my game idea.
Dijkstra’s Algorithm
In a gaming environment, Dijkstra’s algorithm is rarely suitable. This is because it will search every node that is connected to the starting point until it reaches the end point. This is extremely inefficient and expensive on the hardware. There are all sorts of problem that can occur without heuristic-driven algorithms, an example of this is the way that Dijkstra’s algorithm will not consider the direction or heading of the agent, and could therefore draw a route which takes the agent away from the player. With that being said, Dijkstra’s algorithm can be suited to scenarios where there are very few routes it can take. For example, it would be a more suitable application to games that feature specific paths, where the developer already knows the correct path. An example of this is for tower defence games, where the AI must follow a pre-set path. I would not choose Dijkstra’s algorithm for my game as I will need a heuristic-driven algorithm.
A* Search Algorithm
A* combines cost-driven and heuristic-driven algorithms to produce an algorithm that considers both cost and distance / direction. This has the added advantage that it eliminates one of the main problems of Dijkstra’s algorithm: it will consider how far away it is from the target. A* will consider the end goal, find all the paths which the AI traverse, and uses this information to plot the most optimal route. If something along the route should change, for example a new path opens that is closer to the player, then A* will be more efficient in determining that this is the faster route.
This is the algorithm that I will be using for my game as it’s the more optimal choice. Not only will it find the route faster, but it will also provide a more suitable route. It will be used in conjunction with the ‘navigation mesh’ (NavMesh) in Unity. This NavMesh defines and calculates all the routes that are possible to traverse. It will block AI from walking through anything that is defined as a ‘static’ object such as walls, rocks, trees etc.
State Machines
State machines are designed as a thought process for AI allowing them to transition between different states based on the current conditions. State machines can be used for a variety of situations, not just for AI but also for the player. Typically, they are used to determine the action of an AI based on many parameters. An example of this can be seen in the game League of Legends, where AI will attack the minions to gather gold. However, if the number of AI outnumber the player in one of the lanes, they will become overly aggressive. Should either of them become low health, they will begin to retreat.
I plan to implement state machines into my game. One instance that I could use them is when the zombies are low on health, or whereabouts they have been shot could determine the speed at which they move.
Decision trees
Decision trees empower an AI’s ability to decide depending on factors such as the environment, health, the amount of ammo they have etc. As suggested by the name, they will make many decisions, traversing through the branches until the end of the tree is reached. Decision trees often check one conditions against another, or depend on a trigger. The fundamentality of a decision would, in basic terms read as follows: IF A > B then do Y, Else do Z. This would then branch of to another part of the tree, to which some additional conditions would be checked; this continues to run until it reaches the end of the tree.
Decision trees are seen across multiple games, from First Person Shooters to Real Time Strategy games. An example of this could be seen in the game Warframe, where enemies that have sight on the player will take cover behind barriers, even erecting there own barriers to help them close down the distance to the player.
Decision trees are something that I would like to implement in my game. For example, I could implement it in a scenario such as when the zombies are running towards the player, they could swarm between cover based on the distance between them, the player and the cover around them.