Artificial Intelligence Overview
The game will implement AI entities in the form of an 'Animal NPC' class from which several types of AI will inherit. All AI will use a finite state machine to dictate their behaviour and A* pathfinding algorithm combined with the game's grid to handle movement.
AI States
The states which each AI will use are:
Patrol State
The AI moves to randomly selected positions within a radius of their current position.
Chase State
The AI selects a Character within a detection range and begins to move towards them as quickly as possible
Flee State
The AI selects all Characters within a detection range and begins to move towards a position which is on average directly away from all Characters in range.
Attack State
The AI, once within range of a target Character, performs an animation and attack method unique to their type
Dead State
The AI spawns in a 'corpse' object which acts as a resource node. The AI object then deletes itself leaving behind the corpse.
AI Movement on a Grid
As mentioned the path finding algorithm which the AI uses will be A* but will be restricted to the in-game grid. This will work by having each grid cell be an object held within a list which the AI can access and the AI having a NavAgent Unity component attached to them. Their destination will always be the transform.position of one of the grid cell objects within the list of navigable grid cells. So they will use the NavMesh to move across the terrain but will always complete their movement at the centre of one of the grid cells.
Types of AI
Currently we are thinking of Wolf, Deer and Bear types will different behaviour depending on the class.
The diagram below shows a concept for the Wolf State Machine, which would be similar to the Bear and Deer with some exceptions.
Wolf type AI
The Wolf AI will consist of small 'packs' of 3-5 Wolf NPCs. These AI will reference each other's position in the game and use flocking behaviour (specifically, coherency) to remain within range of each other.
An alternative could be having a seperate 'Wolf Pack Centre' object which utilizes the logic found in the Patrol State movement (moving using randomly selected grid cells). Each individual Wolf would then select its destination grid cell only from available cells within a specified radius of the Wolf Pack Centre while in their Patrol State.
When switching to the Chase State the Wolf would ignore their coherency and simply try to get to the target Character via the shortest path. Once within range they would switch to the Attack State and perform their attack according to the logic detailed in the Combat page.
For Wolf NPCs, the method for switching to the Flee State could be based their 'pack number', as in how many Wolf NPCs are left within a single pack. They could have a 'morale' stat which takes a hit whenever a Wolf dies, scaled by the starting number of Wolf NPCs in their pack, which would cause them to enter the Flee State if it drops low enough. Once far enough away and in the Patrol State the Wolf could then begin recovering 'morale' at a constant rate, capping out at a number determined by the number of Wolf NPCs left in their pack.
The Dead State would be simplest of all and simply instantiate a resource node which would provide Uncooked Food when interacted with. Once the object was instantiated the Wolf would then simply be deleted from the scene.
Bear type AI
The Bear AI would be almost the same as the Wolf AI with only a few exceptions.
Firstly the Bear would not spawn in packs, so any of the logic/extra components required for the Wolf pack functionality would not be required for the Bear.
In order to switch to the Flee State the Bear would have a much higher threshold than the Wolf type. This would either be calculated through the amount of remaining HP or through a morale stat as described earlier for the Wolf. If the morale stat was being used the Bear would not have any pack members to worry about and instead lose morale whenever it took damage.
Deer type AI
The Deer could use the same pack logic as the Wolf, patrolling in small groups then scattering when entering the Flee State before reforming.
Unlike the Wolf and Bear the Deer would have no way to enter (or simply not have) an Attack State, and would instead switch to the Flee State as soon as a Character entered it's detection range. If the morale stat was implemented across all Animal NPC types this could work by simply having the Deer's starting morale low enough to always flee & the regeneration rate at 0 so that they would never have a high enough morale to do anything except flee when they detect a Character.