Using HTN for Squad-Based Combat AI in Games

Hierarchical Task Networks (HTN) enable squad-based game AI to execute complex, multi-phase tactical assaults by breaking down high-level tactical objectives into smaller, actionable sub-tasks distributed across multiple agents. By maintaining a shared understanding of the battlefield and evaluating preconditions dynamically, an HTN planner allows non-player characters (NPCs) to fulfill complementary roles—such as suppression, flanking, and breaching—while adapting in real time to player actions and environmental changes.

Understanding Hierarchical Task Networks

An HTN planner approaches decision-making by organizing AI logic into a hierarchy. At the top are compound tasks representing abstract goals, such as Assault_Enemy_Position. These compound tasks branch into various methods, which define alternative ways to achieve the goal based on the current world state. Methods are further decomposed until the planner reaches primitive tasks—atomic, executable actions like Move_To_Cover, Fire_Burst, or Throw_Smoke.

In a squad scenario, the HTN operates either through a centralized "squad commander" planner or distributed individual planners bound by a shared squad blackboard.

Decomposing Multi-Phase Assaults

Multi-phase tactical assaults typically follow distinct military stages: preparation, suppression, maneuver, and final assault. HTN models this progression naturally through sequential decomposition:

  1. Phase 1: Pin and Suppress: The compound assault task first decomposes into a suppression phase. The planner evaluates the world state (player position, line of sight, cover density) and assigns the Base_Of_Fire method to agents equipped with high-rate-of-fire weapons. Their tasks break down into primitive actions: taking heavy cover and firing sustained bursts to pin the player.
  2. Phase 2: Tactical Maneuver: While the suppression task is active, the HTN decomposes the maneuver branch for mobile squad members. It selects a method such as Flank_Route_Concealed. Primitive tasks are issued: querying the navigation mesh for low-exposure paths, moving quietly, and maintaining situational awareness.
  3. Phase 3: Breach and Clear: Once the flanking agents reach their designated assault positions, the planner triggers the final compound task. Primitive actions execute in rapid succession: deploying flashbangs, pushing out of cover, and switching to aggressive direct fire.

Squad Synchronization and Role Assignment

Coordination requires synchronization tokens within the HTN domain. Rather than each agent planning independently in a vacuum, tasks declare interdependencies:

  • Preconditions and Effects on Shared State: A flanking task includes the precondition Target_Suppressed == true. If the suppressing unit is interrupted or killed, the condition invalidates the flanking method, preventing the flanking unit from exposing themselves prematurely.
  • Blackboard Communication: The squad coordinator writes assigned sectors, designated target priorities, and cover reservation tags to a shared blackboard. When individual HTN planners evaluate methods, they check these blackboard variables to avoid role overlap, ensuring two units do not attempt to flank via the exact same path or occupy the same cover point.

Real-Time Replanning and Adaptation

Battlefields are dynamic; players move, deploy defensive tools, or eliminate key enemies. HTN planning handles these disruptions efficiently through targeted replanning:

  • Early Termination: If a primitive task fails—for example, an agent is stunned or their path is blocked by fire—the current method fails. The HTN planner steps back up the hierarchy to find an alternative method under the parent compound task, such as falling back or requesting reinforcement.
  • Cost-Efficient Search: Because HTNs search through structured domain trees rather than flat state spaces, finding an alternative plan (e.g., switching from an aggressive push to a defensive retreat) takes negligible CPU time, allowing entire squads to react instantly without noticeable performance drops.