brain-circuitRTS Opponent AI

Play against the computer

Enemy Masses RTS AI

The EnemyMassesRTSAI is a server-controlled AI opponent designed for RTS games. It provides autonomous control of non-player factions with intelligent tactical decision-making, including exploration, combat engagement, retreat behavior, and advanced baiting/trapping tactics.


Overview

The RTS AI acts as a computer-controlled opponent that manages its own faction of agents, making decisions based on:

  • Force ratios – Comparing army sizes to decide when to engage or retreat

  • Tactical awareness – Using advanced maneuvers like baiting and trapping

  • AoE threat detection – Automatically spreading formations against area-of-effect attacks

  • Difficulty scaling – Adjustable intelligence and reaction times


Quick Start

Adding the RTS AI to Your Scene

  1. Create the AI Controller:

    • GameObject → Enemy Masses → RTS AI Controller

  2. Configure Faction Control:

    • Set Controlled Factions to the factions the AI should command

    • Set Enemy Factions or enable Auto Detect Enemy Factions to identify targets

  3. Assign References:

    • Link the Crowd Controller (auto-detected if not set)

    • Link the Player RTS Controller for automatic enemy faction detection


AI States

State
Description
Behavior

Idle

No active objective

Waits for enemies or transitions to exploration

Exploring

Scouting the map

Moves to random points within exploration radius

Engaging

In combat

Aggressively attacks enemy forces

Retreating

Falling back

Moves away from threats when outnumbered

Regrouping

Consolidating forces

Gathers scattered units before next action

SettingTrap

Positioning ambush

Moving main force to ambush position

Baiting

Luring enemies

Using small bait force to draw enemies into trap


Inspector Settings

References

Property
Description

Crowd Controller

The EnemyMassesCrowdController to command. Auto-detected if null.

Player RTS Controller

Reference to identify player-controlled factions as enemies.


Faction Control

Property
Description

Controlled Factions

Factions that this AI is allowed to control.

Enemy Factions

Factions to attack (used when auto-detect is disabled).

Auto Detect Enemy Factions

Automatically treats player-controlled factions as enemies.

circle-info

When Auto Detect Enemy Factions is enabled, the AI reads faction data from the referenced EnemyMassesRTSController to determine which factions are hostile.


Difficulty Settings

Property
Range
Description

Difficulty

0-5

AI difficulty level (Beginner → Nightmare). Affects tactical decisions and timing.

Decision Interval

0.1-2s

How often the AI makes decisions. Lower = more responsive but higher CPU.

Decision Variance

0-0.5s

Random variance in decision timing for less predictable behavior.

Difficulty Levels

Level
Name
Behavior

0

Beginner

Rarely uses tactics, slow reactions

1

Easy

Occasional tactics, moderate reactions

2

Normal

Balanced tactical use, standard reactions

3

Hard

Frequent tactics, faster reactions

4

Expert

Advanced tactics, quick reactions

5

Nightmare

Maximum tactical intelligence, instant reactions


Exploration Settings

Property
Range
Description

Enable Exploration

bool

Enable map exploration when not in combat.

Exploration Radius

10-200

Radius around current position to explore.

Exploration Duration

5-60s

How long to explore an area before moving on.

Min Exploration Distance

5-50

Minimum distance to travel for a new exploration target.


Combat Tactics

Property
Range
Description

Enable Advanced Tactics

bool

Enable baiting, trapping, and flanking behaviors.

Min Agents For Baiting

5-100

Minimum agents required to attempt baiting tactics.

Bait Percentage

1-20%

Percentage of forces to use as bait.

Bait Retreat Distance

10-100

Distance bait units retreat to lure enemies.

Engagement Range

10-100

Engage enemies when within this range.

Retreat Ratio

1.5-5.0

Retreat when outnumbered by this ratio.

Regroup Distance

20-100

Regroup when forces spread beyond this distance.

circle-exclamation

AoE Avoidance (Formation Spacing)

The AI can automatically detect AoE (Area of Effect) threats from enemy factions and spread out formations to minimize damage.

Property
Range
Description

Enable AoE Avoidance

bool

Enable dynamic formation spacing.

Normal Spacing

0.5-3

Formation spacing when no AoE threat detected.

Spread Spacing

2-6

Spread out spacing when AoE threat detected.

Max Spread Spacing

4-10

Maximum spacing against heavy AoE threats.

Spacing Adjustment Speed

0.5-5s

How quickly spacing adjusts to threats.

Auto Detect AoE Threats

bool

Automatically scan enemy EnemyCrowd settings for AoE.

Manual AoE Threat Level

0-1

Manual override when auto-detect is disabled.

AoE Threshold Radius

1-10

Minimum damage radius to consider as AoE threat.

How AoE Detection Works

When Auto Detect AoE Threats is enabled, the AI scans enemy faction EnemyCrowd configurations for:

  1. Projectile AoE – Checks projectileConfig.enableAOE and aoeRadius

  2. Secondary Projectile AoE – Checks secondary attack configs

  3. Arc Projectiles – Grenades/catapults using ProjectileArc mode get extra threat weight

  4. Multiple AoE Factions – Threat level increases with more AoE-capable enemies


Spawning (Optional)

The AI can spawn reinforcements over time to replenish losses.

Property
Range
Description

Enable Spawning

bool

Enable AI-controlled spawning of reinforcements.

Max Agent Count

10-1000

Maximum total agents the AI can have at once.

Spawn Wave Size

1-100

Agents to spawn per wave.

Spawn Interval

5-300s

Minimum seconds between spawn waves.

Spawn Formation

enum

Formation type for spawned agents.

Spawn Location

Transform

Spawn location. If null, spawns near existing forces.


Visual Feedback & Debug

Property
Description

Show Gizmos

Show AI state gizmos in the editor Scene view.

AI Color

Color for AI controlled agents visualization.

Debug Logs

Enable debug logging for AI decisions in the Console.


Public API

Properties

Manual Control Methods

AoE Avoidance Control


Tactical Behaviors

Baiting Tactic

When the AI has sufficient forces and tactical conditions are favorable:

  1. Selection – AI selects a small percentage of agents as "bait"

  2. Trap Setup – Main force moves to an ambush position behind the army center

  3. Luring – Bait units approach enemies aggressively

  4. Springing – When enemies pursue bait to the trap position, main force engages

Retreat Logic

The AI retreats when significantly outnumbered:

Regrouping

Forces are considered "scattered" when more than 30% of agents are beyond regroupDistance from the army center. The AI will pause offensive actions to consolidate before continuing.


Integration Examples

Difficulty Scaling Based on Player Performance

Scripted AoE Warning

Wave-Based Spawning Controller


Performance Considerations

Setting
Impact
Recommendation

Decision Interval

Lower = more CPU

Use 0.5s for most games, 0.2s for competitive

Max Agents Per Decision

Batched processing

Internal limit of 100 agents per frame

AoE Detection

1 second cache

Negligible impact

Debug Logs

Console overhead

Disable in production


Gizmo Visualization

When Show Gizmos is enabled, the Scene view displays:

Gizmo
Color
Meaning

State Sphere

Varies

Current AI state at controller position

Army Center

AI Color (orange)

Center of controlled forces

Exploration Radius

Green

Current exploration area

Trap Position

Red

Ambush location during baiting

State Colors

State
Color

Idle

Gray

Exploring

Green

Engaging

Red

Retreating

Yellow

Regrouping

Cyan

SettingTrap

Orange

Baiting

Magenta


Troubleshooting

AI Not Controlling Any Agents

  1. Verify Controlled Factions includes your AI faction names

  2. Ensure agents exist in the scene with matching faction names

  3. Check that agents are not dead (isDead = false)

AI Not Attacking

  1. Check Enemy Factions configuration or enable Auto Detect Enemy Factions

  2. Verify Player RTS Controller reference is set (for auto-detect)

  3. Ensure enemy agents are within Engagement Range

AI Retreats Too Often

  • Increase Retreat Ratio (higher = less likely to retreat)

  • Ensure your AI faction has enough spawned agents

Formations Not Spreading for AoE

  1. Enable Enable AoE Avoidance

  2. Check that enemy factions have enableAOE configured in their ProjectileConfig

  3. Verify AoE Threshold Radius isn't set too high

  4. Try manually setting threat level with SetAoEThreatLevel()

Performance Issues with Large Armies

  • Increase Decision Interval to 1.0s or higher

  • Reduce Spawn Wave Size

  • Disable Debug Logs


See Also

  • Public API Reference – Complete API documentation

  • RTS World Anchor System – Fog of war and minimap integration

  • Networking Overview – Multiplayer synchronization

Last updated