LSCS: AI Commander

This asset includes a basic AI system designed to simulate strategic behavior in a Total War-style environment. While it provides essential functionality—such as unit movement, target selection, and simple decision-making—it is intentionally lightweight and easy to understand. The AI is not intended to replicate complex real-time tactics or adaptive learning, but rather to offer a straightforward framework that can be customized or expanded based on your project’s needs. It is planned for the future to greatly improve the AI!


Structure

The AI Commander is the brain of an entire army, designed to provide dynamic and tactical battlefield behavior. It utilizes a powerful and modular Scriptable Object-based architecture, allowing you to create unique AI „personalities“ and behaviors without writing a single line of code.

This guide will walk you through the components of the AI system and how to set them up for your battles.

The AI’s intelligence is broken down into three distinct layers, just like a real chain of command:

  • AICommander: The „General“ on the battlefield. This component lives in your scene and oversees one army. Every few seconds, it evaluates the battlefield and chooses the best overall Tactic to execute.
  • AIStrategySO: The „Playbook“ for the Commander. This Scriptable Object defines the AI’s personality by holding a prioritized list of all the Tactics it is allowed to use. You could have a „Defensive“ strategy that prefers holding ground, and an „Aggressive“ strategy that prefers flanking.
  • AITacticSO: A single „Maneuver“ or building block of the strategy. Each Tactic is a Scriptable Object that defines a specific goal, like „Establish a Frontline,“ „Flank with Cavalry,“ or „Focus Fire with Archers.“

This modular design allows you to easily create new tactics and strategies to give your AI’s endless variety.


AiCommander

Setting up an AI Commander in your scene is straightforward.

  • Locate the AI Commander script: It is located in the previously created Scene Prefab and attached to its child object Manager
  • Think Interval: How often the Commander should overthink its strategy
  • Strategy: The actual Strategy the AiCommander uses

AIStrategy

The Strategy holds all the Tactics the AiCommander can use

Reserve Percentage is the percentage of Enemy Formations the AiCommander holds back for later use


Tactics

Tactics are the individual actions your AI can perform. Currently there are three Tactics avialable:

  • Tactic Establish Frontline: The Tactic tries to establish a Frontline with all avialable Formations that have the Melee state
    • Direct Opponent Bonus: Bonus the Formation gets if it chooses the direct opponent. Useful to prevent path crossing
    • Assignment Penalty: Penalty for assigning the same enemy to different Formations. Useful to prevent blobs.
  • Tactic Archer target selection: The tactic assigned the best valued opponeds to archers and sends them in shooting range. Takes blocking / moving enemys into account
    • Enemy Blocking Layer: Enemy Layer that block the Archers path
  • Tactic Flank with Cavalry: The Cavalry tries to flank the Players Army to attack vulnerable targets
    • Chance for early attack: Chance that the cavalry break out and attack early
    • Cavalry Types: Which Unit types should be considered for flanking
    • Vulnerable Unit Types:Which Unit Types should be flanked
    • Flank Distance: How far the cavalry should stay away form the army

Creating an AI Personality: The Scriptable Objects

The true power of the system lies in creating your own AI behaviors in the Project window.

3.1 Creating Tactics

Tactics are the individual actions your AI can perform. To create one:

  1. In the Project window, right-click and go to Create > AI > Tactic.
  2. Choose one of the available tactics, for example, Establish Front Line.
  3. Select the newly created asset to configure its properties in the Inspector.

Example: Tactic_EstablishFrontLine

  • Direct Opponent Bonus: A high score bonus for attacking the enemy directly opposite in the battle line. This value is crucial for preventing formations from crossing over the battlefield.
  • Assignment Penalty: A score penalty applied to an enemy formation for each friendly formation already targeting it. This is the key to preventing „blobbing“ and encourages the AI to spread its forces along the enemy line.

3.2 Creating a Strategy

The Strategy combines your individual tactics into a complete „personality.“

  1. In the Project window, right-click and go to Create > TopsonGames -> AI > Strategy.
  2. Name the asset (e.g., Aggressive_AI_Strategy).
  3. Select the asset. In the Inspector, you will see a list called Tactics.
  4. Drag and drop the Tactic assets you created (e.g., Tactic_EstablishFrontLine, Tactic_FlankWithCavalry) into this list.

How it works: In each „think“ cycle, the AICommander will go through this list of tactics, evaluate each one based on the current battlefield situation, and execute the one with the highest score. You can influence the AI’s priorities by the order of this list.


4. How It Works: The „Think“ Cycle

The AICommander’s decision-making process is a simple but powerful loop:

  1. Refresh: The Commander gets an updated list of all its available (idle) formations and all visible enemy formations.
  2. Evaluate: It asks every Tactic in its Strategy list: „How good of an idea are you right now?“ Each Tactic returns a score based on the current situation (e.g., the Flank Tactic will return a high score if it has available cavalry and the enemy has vulnerable archers).
  3. Select: The Commander chooses the Tactic that returned the highest score.
  4. Execute: The Commander calls the Execute method on the chosen Tactic, which then issues the commands (SetCustomTarget, etc.) to the relevant formations.
  5. Wait: The Commander waits for the thinkInterval to pass before starting the cycle again.

This creates a dynamic AI that constantly re-evaluates the battle and adapts its strategy accordingly.