FIT CTU

Adam Vesecký

NI-APH
Lecture 10

Game AI

AI introduction

Game AI

Classic Game AI has something to do with scripting, a lot to do with state machines, even more to do with map analysis, and not a lot to do with AI field.
It's not that we can't get good behavior out of machine learning techniques. It's that it may not be the behavior we want for the game experience we are looking for.Jacob Schrum

Artificial Intelligence in Games

  • A broad set of principles that generate behaviors, providing an illusion of intelligence
  • Should be sophisticated enough to make the players think they interact with something intelligent
  • Realistic AI isn't necessarily fun for players
AI in games
AI in games
AI in games

AI Techniques

AI techniques

AI in games

Doom 1 (1993)

  • monsters infighting (two AI characters encounter each other)
  • state machines, intelligent map (SPECIAL attribute of map lines)

Age of Empires (1997)

  • AI is smart enough to find and revisit other kingdoms, poor in self-defense

Half-life (1998)

  • monsters can hear and track players, flee when getting defeated etc.
  • soldiers had automated announcement system, ability to cover

Starcraft (1998)

  • particle model for hidden units' position estimation

Unreal Tournament (1999)

  • Bayesian networks

Quake III (1999)

  • HFSM for decisions, AAS (Area Awareness System) for collisions and pathfinding
Gordon Freeman

AI techniques in games

Black and White (2001)

  • scientific-based Belief-Desire-Intention model

Halo 2 (2004)

  • behavior trees and fuzzy logic

F.E.A.R. (2005)

  • GOAP for tactical coordinating with squad members, suppression fire, blind fire

XCOM: Enemy Unknown (2012)

  • utility-based system, measuring usefulness for every possible action

ARMA 3 (2013)

  • efficient issue ordering, situation-based decisions, retreats, ambush
  • HTN (Hierarchical task network) for mission generator

DOTA 2 (2013)

  • OpenAI bot (since 2017) has beaten some of the greatest players

Starcraft II (2013)

  • AlphaStar deep neural network, released in 2019
Starcraft

Challenges for Game AI

Game AI features and limits

  • real-time
  • limited resources
  • incomplete knowledge
  • planning
  • learning
  • acceleration (strategies)

Game AI properties

  • predictability and unpredictability (surprise elements)
  • support - communication between NPC and the player
  • surprise - harassment, ambush, team support,...
  • winning well and losing well
  • cheating - acceptable as long as it doesn't get detected by the player
AI challenges
The AI must be fun to play against, not beat the player easily

Rules for Good Game AI

  • Good AI lets the player cheat
    • e.g., combat chess system in Doom - only a handful of demons is pursuing the player
  • Good AI tells the player what it is thinking
    • e.g., patrolling units saying "Someone is over there!"
  • Good AI is predictable
    • e.g. mobs who are pursuing the player across the map
  • Good AI can interact with the game system
    • picking up weapons, opening doors
  • Good AI has its own goals
    • it doesn't just "spawn" for the sake of the player
  • Good AI isn't just about enemies
    • worst example: Oblivion NPCs

AI for mobs

Scripting

  • IF-THIS-THEN-THAT
  • AI behavior is completely hardcoded
  • simple, easy to debug, easy to extend
  • human player should behave as the developers expected
  • good scripting behavior must cover a large amount of situations
1 // Doom 2: find player to chase
2 void A_Look (mobj_t* actor) {
3 mobj_t* targ;
4 actor->threshold = 0; // any shot will wake up
5 targ = actor->subsector->sector->soundtarget;
6 if (actor->flags & MF_AMBUSH){
7 if (P_CheckSight (actor, actor->target))
8 goto seeyou;
9 } else goto seeyou;
10
11 if (!P_LookForPlayers (actor, false)) return;
12 // go into chase state
13 seeyou:
14 P_ChasePlayer();
15 }
Doom scripting

Finite State Machine

  • the oldest and most commonly used formalism to model game AIs
  • useful for entities with a small set of distinct states
    Dungeon FSM
  • each entity can be in exactly one of a finite number of states at any time
  • Definition
    • quadruple: (Q, \Sigma, \delta, q_0)
    • Q  is a finite, non-empty set of states
    • \Sigma  is a finite set of inputs
    • \delta: Q \times \Sigma \rightarrow Q  is the state-transition function
    • q_0  is an initial state, q_0 \in Q
  • can be implemented via polymorphism or a state transition table
  • unmanageable for large complex systems, leading to transition explosion

Example: Pacman FSM

FSM pacman
pacman

Example: Pacman transition table

StateTransitionCondition
Wander the mazeChase pacmanPacman spotted
Wander the mazeFlee PacmanPowerPellet eaten
Chase PacmanWander the mazePacman lost
Chase PacmanFlee PacmanPowerPellet eaten
Flee PacmanReturn to BaseEaten by Pacman
Flee PacmanWander the mazePowerPellet expired
Return to BaseWander the mazeCentral base reached

Example: Doomguard

FSM doomguard
doomguard

Example: Doomguard

  • let's add an ability to fall asleep
doomguard
FSM doomguard extended

Hierarchical state machine

  • also known as statecharts
  • each state can have a superstate or a set of substates
  • groups of states share transitions
  • usually implemented as a stack
    • push a low-level state on the stack when entered
    • pop and move to the next state when finished
HFSM doomguard
doomguard

Fuzzy logic

  • a complementary asset for state machines and scripting
  • instead of thresholding, we can blurry out state transitions
  • used in Halo 3 for unit control, threat assessment and classification
  • other applications: proximity, mood management
fuzzy logic diagram

Behavior Tree

  • tree of hierarchical nodes that control decision making process
  • originated from gaming industry since Halo 2 (2004)
  • combines elements from both Scripting and HFSMs
  • there is no standardized formalization
  • depth-first traversal, starting with the root node
  • each executed behavior passes back and returns a status
    • SUCCESS, FAILURE, RUNNING, (SUSPENDED)
behavior tree example

Behavior Tree

behavior tree
Node TypeSuccessFailureRunning
SelectorIf one child succeedsIf all children failIf one child is running
SequenceIf all children succeedIf one child failsIf one child is running
DecoratorIt dependsIt dependsIt depends
ParallelIf N children succeedIf M-N children succeedIf all children are running
ActionWhen completedUpon an errorDuring completion
ConditionIf trueIf falseNever

Example: Unreal Engine BT Editor

behavior tree in unrel engine

Example: Doomguard

behavior tree with doomguard

BT Improvements

  • we can define a conditional selector in order to simplify the diagram
behavior tree optimizationbehavior tree optimization

Example: Locked door

doom door opening
behavior tree door

BDI Systems

  • Belief-Desire-Intention model
  • Beliefs - simplified model state (in sharp contrast with knowledge, beliefs may not be true)
  • Desires - motivational states, objectives to accomplish
  • Intention - choices, goals to pursue
  • Features
    • Deliberative - creates a symbolic representation of the game world
    • Logic-based - choices are driven by logic calculus, not by heuristic function
    • Rational - choices made are in the best interest of agent's desires
    • Goal-oriented
  • Implementations
    • PRS (Procedural Reasoning System)
    • GOAP (Goal-oriented action planning)

Goal-Oriented Action Planning

  • centers on the idea of goals as desirable world states -> select a goal and attempt to fulfill it
  • each action has a set of conditions it can satisfy, as well as a set of preconditions that must be true in order to be satisfied
  • implemented for F.E.A.R (2005) and Tomb Raider (2013)
goal-oriented action planning diagram

Example: F.E.A.R

  • a set of goals is assigned to each mob
  • these goals compete for activation, and the AI uses a planner to try to satisfy the highest priority goal
  • the AI figures out the dependencies at run-time, based on the goal state and effects of actions
SoldierAssassinRat
AttackAttackAnimate
AttackCrouchInspectDisturbanceIdle
SuppressionFireLookAtDisturbanceGotoNode
FlushOutWithGrenadeAttackMeleeUncloaked
AttackFromCoverTraverseBlockedDoor
BlindFireFromCoverAttackFromAmbush
ReloadCoveredAttackLungeUncloaked
FEAR soldier
FEAR assassin
FEAR rat

AI for navigation

Predicting opponents

  • bots shouldn't know where the player is - partial observations of the game state
    predicting opponents
  • General methods:
    • Hidden semi-markov model
    • Particle filters
  • Methods for FPS:
    • Occupancy map
    • SOAR cognitive architecture (used in Quake II)
    • Vision Cone (used in Bioshock)
    • Influence Spheres (distance-based acknowledgment)
  • Methods for strategies:
    • Threat map
    • Influence map
    • Bayesian networks
    • Delaunay triangulation
    • K-Means clustering

Occupancy map

  • a grid over the game environment
  • maintains probability for each grid cell
  • when the opponent is not visible, the probabilities from the previous occupancy cells are propagated along the edges to neighboring cells
occupancy map animation

Threat map

  • the value at any coordinate is the damage the enemy nearby can inflict
  • can be easily integrated into pathfinding
  • Example: combat units bypass enemy defensive structure in order to attack their infrastructure
threat map diagram

AI in strategies

Real-time strategy

  • Real-time strategy is a Bayesian, zero-sum game (Rubinstein, 1994)
    Total War
  • a game where the player is in control of certain, usually military, assets, with which the player can manipulate in order to achieve victory
  • goal: build up a base, gather resources, produce army, destroy the enemy
  • methods: layer-based AI, rule-based AI

Main elements

  • map, mini-map
  • resources
  • units and their attributes
  • buildings

Other features

  • real-time aspect (no turns)
  • fog of war
  • tech tree

RTS games

Dune II
1992
C&C Red Alert
1995
Total Annihilation
1997
Warcraft 3: Reign of Chaos
2002
Starcraft II
2010
Ashes of the Singularity
2016

RTS Features

Resource Control

  • controlling more resources increases the players' constsruction capabilities

Tech tree

  • a directed acyclic graph that contains the whole technological development of a faction

Build order (opening)

  • the timing at which the first buildings are constructed
RTS build order

Fog of war

  • fog that covers the parts of the map the player has not yet explored
  • requires to scout unexplored areas to find enemy sources

Micromanagement

  • way of controlling units in detail while they are in combat

Example: Starcraft 2 Tech-tree

Starcraft II Tech-tree

RTS AI Layers

  • micromanagement
  • tactics (army positions)
  • strategy (tech tree)
RTS AI layers

Layer-based AI

  • each layer handles specific task
  • decision components consider available information, retrieved by lower components, to make decisions; executors are triggered afterwards
  • Sensor - reads the game state
  • Analyzer - combines sensing data to form a coherent picture
  • Memorizer - stores various types of data (terrain analysis, past decisions,...)
  • Decider - a strategic decider, determines goals
  • Executor - translates goals into actions
  • Coordinator - synchronizes groups of units
  • Actuator - executes actions by modifying the game state
Layer-based AI in games

Example: Layer-based AI

Layer Based AI diagram
Cossacks 3

Example: Megaglest

  • Open-source 3D RTS (LINK)
  • seven factions: tech, magic, egypt, indians, norsemen, persian, romans
  • rule-based AI
Megaglest

Megaglest architecture

Megaglest architecture

Megaglest rules

RuleConditionCommandFreq [ms]
AiRuleWorkerHarvestWorker stoppedOrder worker to harvest2 000
AiRuleRefreshHarvesterWorker reassigned20 000
AiRuleScoutPatrolBase is stableSend scout patrol10 000
AiRuleRepairBuilding DamagedRepair10 000
AiRuleReturnBaseStopped unitOrder return to base5 000
AiRuleMassiveAttackEnough soldiersOrder massive attack1 000
AiRuleAddTasksTasks emptyAdd tasks5 000
AiRuleBuildOneFarmNot enough farmsBuild farm10 000
AiRuleResourceProducerNot enough resourcesBuild resource producer5 000
AiRuleProducePerforming prod. task2 000
AiRuleBuildPerforming build task2 000
AiRuleUpgradePerforming upg. task30 000
AiRuleUnBlockBlocked unitsMove surrounding units3 000

Lecture Summary

  • I know what challenges in terms of game AI the developers face
  • I know basic AI techniques, such as scripting, FSM, and HFSM
  • I know something about behavior trees
  • I know the main element and features of RTS

Goodbye Quote

All you had to do was follow the damn train!GTA, San Andreas