The RTS AI system is designed for scalability, performance, and strategic behavior in large-scale battles. It uses a multi-threaded, behavior-based architecture with centralized decision-making and configurable AI strategies that allow different AI personalities and behaviors.
Location: game/systems/ai_system.cpp
The main system that manages all AI players and coordinates the AI pipeline.
Key Responsibilities:
Performance Features:
set_update_interval()Location: game/systems/ai_system/ai_types.h
Core data structures for AI reasoning.
AISnapshot - Immutable world state snapshot AIContext - Persistent AI state with deadlock tracking
Strategic phases: Idle, Gathering, Attacking, Defending, Retreating, Expanding
Deadlock recovery triggers after 60s in same state OR 10 cycles with no progress.
Priority-based modular behaviors:
Location: game/systems/ai_system/ai_strategy.h
AI strategies allow different behaviors and personalities for AI opponents:
Available Strategies:
Strategy Configuration: Each strategy has modifiers that affect AI behavior:
aggression_modifier: Affects attack timing and frequencydefense_modifier: Affects defensive stance and retreat thresholdsexpansion_priority: Priority for capturing neutral buildingsproduction_rate_modifier: Speed of unit productionmin_attack_force: Minimum force required before attackingretreat_threshold: Health percentage when AI retreatsPersonality System: Strategies are further customized by personality values (0.0-1.0):
aggression: Additional aggression factordefense: Additional defensive stanceharassment: Enables harassing behavior at rangeUpdate interval can be adjusted:
ai_system.set_update_interval(0.5f); // Reduce to 2 Hz for performance
Set AI strategy for a specific player:
// Set aggressive strategy with high aggression personality
ai_system.set_ai_strategy(player_id, AIStrategy::Aggressive,
0.9f, // aggression
0.3f, // defense
0.2f); // harassment
// Set defensive strategy
ai_system.set_ai_strategy(player_id, AIStrategy::Defensive,
0.4f, 0.9f, 0.2f);
In mission JSON files, specify strategy for AI opponents:
{
"ai_setups": [
{
"id": "aggressive_enemy",
"nation": "roman_republic",
"strategy": "aggressive",
"personality": {
"aggression": 0.9,
"defense": 0.3,
"harassment": 0.2
}
}
]
}
Strategy values: "balanced", "aggressive", "defensive", "expansionist", "economic", "harasser", "rusher"
Note: Skirmish mode uses the default "balanced" strategy if no strategy is specified.
Access debug metrics via AIContext::debug_info:
total_decisions_made: Count of decision cyclestotal_commands_issued: Count of commands generatedstate_transitions: Count of state changesdeadlock_recoveries: Count of forced recoveriessnake_case (e.g., total_units, update_context)PascalCase (e.g., AIContext, BehaviorPriority)Potential improvements:
Strategies modify the thresholds used in the state machine transitions:
// Example: Aggressive AI attacks with fewer units
int MIN_UNITS_FOR_ATTACK = static_cast<int>(4.0F * strategy.min_attack_force);
// With aggressive strategy (min_attack_force = 0.6), needs only ~2 units
// With defensive strategy (min_attack_force = 1.5), needs ~6 units
// Example: Retreat threshold
if (ctx.average_health < ctx.strategy_config.retreat_threshold) {
ctx.state = AIState::Retreating;
}
// Aggressive: retreats at 15% health
// Defensive: retreats at 40% health
Economic strategies produce units faster:
float production_interval = 1.5F / strategy_config.production_rate_modifier;
// Economic strategy (1.5x): produces every 1.0s
// Balanced (1.0x): produces every 1.5s
// Rusher (0.9x): produces every 1.67s
See the full implementation in game/systems/ai_system/ directory.