Director AI system: A director AI organizes aggressive NPCs by utility scores, assigning actions like attacking or seeking cover. Cover points are validated for safety and availability.
Improving percived realism of NPC: Problem-solving included adding animations and reaction time variability to enhance gameplay. Including an artificial delay between state transitions simulates decision-making, improving accessibility and perceived realism while enabling dynamic difficulty adjustment.
Enhanced Parent State Responsibilities: The FSM begins with a patrol state where the AI moves between designated points, with a chance to idle. If the player is detected or a weapon is fired nearby, the AI transitions to an aggression state.
Intro:
This project is meant to get me to work on NPC systems and learn how to properly work in architecture systems so they could be attacked between different parts of the game. The main plan for the project was to create an interesting and complicated NPC system using a Finite State Machine (FSM) and then upgrade it into a Hierarchical Finite State Machine (HFSM). The FSM included 5 states; Patrol, Idle, Aggression, Attacking and Cover states.
Documentation:
One of the first things I worked on for the project was the documentation for the NPC system and the basic surrounding game design. This mostly included deciding how each state would transition into each other and what would be tracked in each state as well as any managers I would need for the NPC as a whole. I also worked on a trait system that would give each AI a bit more individuality with traits ranging from more aggressive NPC (And attack the player) or scared (Going to cover or staying away from the player).
Implementation:
One of the cornerstones of the NPC system is the NPC Director. The system helps determine how many NPCs get spawned, possible patrol locations, as well as different traits and audio. The Director is also in charge of removing any eliminated NPCs and communicates to the proper channels about the removed NPC. Another unique idea added to the Director is that whenever the player gets discovered by an enemy, the enemy will play an audio clip calling them out, and the Director along with said NPC will have nearby NPCs change their state to a more alerted version as a way to signal that they are now aware of the player and moving into position. Along those lines, a token system has been set up where only a set number of NPCs can actually shoot at the player at one time, which ensures that players don't feel overwhelmed and prevents the risk of the player being hit by too many bullets at once and dying without being able to do anything about it.
Meanwhile, each NPC has an FSM manager that controls transitioning between states and running the start and update methods, as well as controlling the senses of the NPC. This system lets each NPC manage state changes from a central point instead of having multiple interconnected state changes spread across all of the states themselves. It makes debugging state changes, and debugging what the AI is doing versus what it is meant to be doing in relation to its current state, much easier.
Execution of FSM:
When loading into a level, all of the NPCs that get spawned have their current state set to patrolling, and a random list of walking locations is set up for them to visit. Once they reach the end of the list, a new random list of nearby walking points is created and traveling between them begins again. During this state, the NPCs can switch to two other states: Idle and Aggression.
The Idle state occurs at random intervals — every 25 seconds there is a 15% chance to enter the state. When entering the state, a coroutine starts that has the NPC stop for a given amount of time depending on their trait. During the coroutine, every time a third of that time has passed, the NPC will rotate a random amount. This not only makes it harder for players to sneak past NPCs by following directly behind them, but it also makes the NPCs appear more active, as though they are real humans standing around and idling. After the timer runs out, if the NPC has not spotted a player they return to the patrol state; otherwise they transition to the Aggression state.
The Aggression state works as a transitional state where NPCs determine whether to transition toward the Attack or Cover state. This is done by taking into account the distance from the player, whether their current weapon can reach the player, comparing the closest cover points, and finally considering the NPC's trait and whether they are a more aggressive NPC or one that prefers to hang back and take cover.
When the NPC transitions to the Attack state, it will find a position that keeps them in range but is not blocked by a cover position or too close to another NPC. Once the NPC is en route to their location, they will play an audio clip to signal to the player that the NPC is preparing to attack. At the same time, the NPC will keep looking at the player in order to monitor them and be able to reassess if anything needs to change. Once they arrive at their destination, they take less than 2 seconds to aim at the player before firing. After that point, the NPC will check whether they still have ammo and reload if necessary, or determine if they can no longer find the player. If the player is not found, they will return to the patrol state; otherwise they will have a chance to shoot at the player again and/or return to the Aggression state to decide their next move.
On the other hand, if the NPC decides to switch to the Cover state, they will find the closest cover position and check that it has not been taken by another NPC and that it faces away from the player to provide adequate cover. They will then run toward the cover location, where they will request an attack token in order to stand up and fire at the player. If at any point the player moves more than 30 paces from their original location, the NPC will reconsider whether they need to leave their cover position or if it is still viable to stay where they are.