AI System Overview March 2012 AI System Basics – Navigation • Default Navigation System – Triangulation • 2D terrain-based navigation – Navigation Modifiers • Waypoints • Flight • Volume etc. • Multi-layer Navigation System • Smart Object System – Allows AIs to move in a special way AI System Basics – Decision Making • Behavior Selection System – Uses Behavior Selection Trees to select behaviors of AIs • Goal System – Executes atomic and composite goals of AIs • Cover System – Provides AIs with static and dynamic covers • Smart Object System (again) – Allows AIs to interact with their environment • Interest System – Allows AIs to perform intelligent actions when not alerted AI System Basics – Tactical AI • Tactical Point System – Allows AIs to ask intelligent questions about their environment (e.g. where to hide or attack) • Faction System – Determines which AIs are hostile to which other AIs • Group Coordination System – Uses Coordination Selection Trees to select group behaviors • Formation System – Allows AIs to move in formations AI System Basics – World Interfacing • Signals – Trigger events and/or change behaviors • Perception System – 2 options: – Perception Handler (legacy, usually per-game) – Target Track System uses configurable ADSR envelopes to represent incoming stimuli • Communication System – Allows AIs to play sound/voice/animation events Environment (1/4) - CryAction • Game Object Model – Entity, Movement Controller, Extensions • Actor & Vehicle System – Health, camera, IK, weapons, animation etc. • Flow Graph – Visual definition of game logic • AI Debug Renderer – HUD, geometric primitives, text labels, graphs etc. Environment (2/4) - Editor • AI Entities (AIs) – Properties, Flow Graphs (FG Debugger available), scripts • Entity Archetypes – Templates for properties of individual AIs • AI Shapes – AI Territories, AI Paths, Forbidden Areas • Navigation – Navigation Modifiers for where Triangulation is not used • Cover Surfaces – CoverSurface anchors for where cover should be • Visual AI Debugger – Recording AI signals, active behaviors, goals, stimuli etc. Environment (3/4) – Lua & XML • Lua scripts • • • • • Entity definitions (including entity Flow Graph nodes) AI behavior definitions Group behavior definitions Library or shared Lua code (game rules, basic entities) Blackboards to share information among groups or globally • XML files • • • • Behavior/Coordination Selection Trees Goalpipes AI Communications Items (e.g. weapons) Environment (4/4) – Entity System • Spatial queries: GetPhysicalEntitiesInBox • AIs and vehicles are entities of the Entity System • To spawn an entity, its Entity Class is required – Defined either using .ent file in directory Game/Entities – Or through a C++ call to RegisterFactory(…) in game code • Entity Pool can be used to limit the number of active AI actors (AIs) per each specified Entity Class AI System Execution Context • AI update is called every frame • That said, AIs are “fully” updated only at ~10Hz • Some AI subsystems use independent time-slicing: – – – – – – Pathfinding Tactical Point System Dynamic waypoints update Smart Object System Interest System Dead bodies removal • Some services can be called synchronously from game code (e.g. Tactical Point System queries) AI Objects Hierarchy • CAIObject: Basic properties (entity ID, position, direction, group ID, faction etc.) • CAIActor: Basic perception & navigation, behavior selection, coordination, blackboard, AI Territory awareness, AI Proxy (of CryAction) • CPipeUser: Goalpipes/goalops, smart objects, pathfinding/pathfollowing/obstacle avoidance, attention target, full-fledged perception • CAIPlayer: AI System’s representation of the actual (real) game player • CPuppet: Aiming, firing, stances, covers – full-fledged AI agent • CAIVehicle: Vehicle-specific code Navigation (1/3) • Default Navigation – Triangulation on terrain • Uses cylindrical objects (e.g. trees) and Forbidden Areas – Human Waypoints • Waypoints need to be placed manually • Connections can be generated automatically – Flight Navigation • Information about navigable volumes for flying entities – Volume Navigation • More general volume navigation, e.g. for ocean Navigation (2/3) • Multi-layer Navigation Navigation (3/3) • In general – A* search is time-sliced to use 0.5 ms per AI frame (configured in console variable “ai_PathfinderUpdateTime”) – Pathfinding options: high priority, straight or partial path – Human Waypoints update is a bit heavy but time-sliced – Navigation graph is optimized but needs some memory • Navigation generation – Navigation data is generated offline, in Editor – Multi-layer Navigation allows the regeneration of navigation mesh as the designer modifies the map Behavior Selection Tree – Overview • Effectively a state machine – Defines conditions to changes Behaviors (“states”) • Behavior variables are used to choose behaviors – They can be set in Lua or as a result of receiving a Signal • Uses tree decision system with condition checking – Conditions are specified as strings but can be more complex than just checking a single variable • E.g. "IsHurt and !InCover and HasTarget" – If a condition fails traversal moves on to the next sibling – If no leaf node is reached then no behavior switch is made • The code can be shared among a number of BSTs Behavior Selection Tree - Example <SelectionTrees> <SelectionTree name="FogOfWar" type="BehaviorSelectionTree"> <Variables> <Variable name="IsFar"/> <Variable name="IsClose"/> <Variable name="AwareOfPlayer"/> </Variables> <SignalVariables> <Signal name="OnEnemySeen" variable="AwareOfPlayer" value="true"/> <Signal name="OnNoTarget" variable="AwareOfPlayer" value="false”/> <Signal name="OnLostSightOfTarget" variable="AwareOfPlayer" value="false"/> </SignalVariables> <Priority name="Root"> <Leaf name="FogOfWarSeekST" condition="IsFar"/> <Leaf name="FogOfWarEscapeST" condition="IsClose"/> <Leaf name="FogOfWarAttackST" condition="AwareOfPlayer"/> <Leaf name="FogOfWarIdleST"/> </Priority> </SelectionTree> </SelectionTrees> Behavior Selection Tree – Definition • XML file which resides in Scripts/AI/SelectionTrees • Use tag <SelectionTree> • Multiple Behavior Selection Trees (BSTs) can be defined in the same file, if really necessary <SelectionTrees> <SelectionTree name="FogOfWar" type="BehaviorSelectionTree"> […] </SelectionTree> </SelectionTrees> Behavior Selection Tree – Tags • To define a BST, use tags <Leaf> and <Priority> • Leaf name should be the same as that of the corresponding behavior (or a Leaf Translation node) • Leaves are evaluated (as defined in attribute “condition”) in order. The behavior of the first leaf evaluated to true is selected. <Priority name="Root"> <Leaf name="FogOfWarSeekST" condition="IsFar"/> <Leaf name="FogOfWarEscapeST" condition="IsClose"/> <Leaf name="FogOfWarAttackST" condition="AwareOfPlayer"/> <Leaf name="FogOfWarIdleST"/> </Priority> Behavior Selection Trees – Variables • Variables can only be Boolean <Variables> <Variable name="IsFar"/> <Variable name="IsClose"/> <Variable name="AwareOfPlayer"/> </Variables> Behavior Selection Trees – Signals • AI signals can be used to change the values of specific BST variables <SignalVariables> <Signal name="OnEnemySeen" variable="AwareOfPlayer" value="true"/> <Signal name="OnNoTarget" variable="AwareOfPlayer" value="false"/> <Signal name="OnLostSightOfTarget" variable="AwareOfPlayer" value="false"/> </SignalVariables> Behavior • Can essentially be thought of as an FSM state • Written in Lua and contains a function table where each row defines the response to a Signal • Scripts must be in “Scripts/AI/Behaviors” (or below) • Each behavior script should contain a call to function CreateAIBehavior(name, parent, def) defined in AIBehavior.lua in order to be added to the table of available behaviors. Behavior – Example Scripts/AI/Behaviors/NewAliens/AlienGrunt/AlienGruntAvoidImmediateThreat.lua: local Behavior = CreateAIBehavior("AlienGruntAvoidImmediateThreat", "GruntBaseBehavior", { Alertness = 2, Constructor = function(behavior, entity) ... end, Destructor = function(behavior, entity) ... end, AvoidThreat = function(behavior, entity) ... end, AvoidThreatCoverNotFound = function(behavior, entity) ... end, ... }) Goalpipes • Goalpipe is a predefined sequence of atomic goals (goalops) to be performed by an AI • In Lua it’s easier to create goalpipes on the fly, if necessary • In XML goalpipe definitions look a lot more intelligible Example of an XML goalpipe: <GoalPipe name="ApproachAndShootObject"> <Speed blocking="false" id="Run"/> <FireCmd mode="Burst"/> <LookAround blocking="false" lookAroundRange="360" scanRange="5" interval="60" breakOnLiveTarget="true"/> <Timeout intervalMin="1.0" intervalMax="4.0"/> </GoalPipe> Goalops – Some Examples • Locate • Locate an AI object/actor for further handling • LookAt • Look at a specified AI object/actor • FireCmd • Attack a specified AI object/actor in a specified manner • Signal • Send a signal to a specified AI actor/group/neighborhood • Speed • Stance • Stick • Approach/follow a specified AI object/actor • TacticalPos • Find a spot that match certain criteria (to hide, attack etc.) Cover Surfaces • Actual cover information is generated offline, in Editor Interest System • If the player is able to secretly approach AIs, merely playing idle animations may not suffice to look cool • Interest System basically triggers predefined AI Actions, as soon as idle AIs of applicable AI classes find themselves near applicable (interesting) entities • Examples: • AI attentively examines a dead body • AI attentively examines an abandoned car • AI “finds” a good spot and uses his binoculars to attentively examine the landscape Tactical Point System (TPS) Bill, on the right, finds a hidespot from a Woodsman Tactical Point System – Overview • Tactical Point System – Allows AIs to ask intelligent questions about their environment – Is a structured query language over sets of points in the world – Is used for finding hidespots or attack points or navigation waypoints – Optimizes queries automatically and processes them in a time-sliced manner • Accessible through Lua, XML, C++ TPS Query Example • Find a cover from referencePoint AI.RegisterTacticalPointQuery( { Name = "Find_Cover", { Generation = { hidespots_from_referencePoint_around_puppet = 20.0 }, Conditions = { reachable = true, coverSuperior = true, }, Weights = { distance_from_referencePoint = -1.0, distance_from_puppet = -1.0, friendlyDistance = 1.0, }, }, Faction System Determines which AIs are hostile to which other AIs: <Factions> <Faction name="Players"> <Reaction faction="Grunts" reaction="hostile"/> <Reaction faction="Civilians" reaction="friendly"/> <Reaction faction="Assassins" reaction="hostile"/> </Faction> <Faction name="Civilians" default="neutral"/> ... </Factions> (Game/Scripts/AI/Factions.xml) Group Behavior – Example (1/2) <SelectionTrees> <SelectionTree name="DefaultGroup" type="CoordinationSelectionTree"> <Variables> <Variable name="AreaSearch" /> <Variable name="Sweep" /> <Variable name="InvestigateMemberDeath" /> </Variables> <Priority name="Root"> <Leaf name="GroupAreaSearch" condition="AreaSearch" /> <Leaf name="GroupSweep" condition="Sweep" /> <Leaf name="GroupInvestigateMemberDeath" condition="InvestigateMemberDeath" /> <Leaf name="DEFAULT" /> </Priority> </SelectionTree> </SelectionTrees> Group Behavior – Example (2/2) local Behavior = CreateAIGroupBehavior("GroupAreaSearch", "DEFAULT", { Constructor = function(self, group) ... end, Destructor = function(self, group) ... end, OnGroupTargetNone = function(self, group) ... OnGroupTargetSound = function(self, group) ... OnGroupTargetMemory = function(self, group) ... OnGroupTargetVisual = function(self, group) ... end, ... }) AI Territories & Waves • Control the number of active AIs at any time through simple Flow Graph logic • All AIs assigned to an AI Territory can be activated, deactivated, and spawned using a single FG node • Optionally, AI Waves are attached to AI Territories and allow independent AI activations • If AIs were marked to be Created Through Pool and assigned to an AI Wave, the AI Wave handles Entity Pool issues – e.g. loading/unloading – automatically AI Debug Draw – (1/2) • ai_DebugDraw 1: Basic info on AIs (selected by ai_DrawAgentStats) 74: All the Navigation Graph (can be slow) 79: Parts of the Navigation Graph around the player • ai_statsTarget Goon35 – Detailed info for specified AI (e.g. Goon35) • ai_DebugTargetTracksAgent Grunt1 – Perception information on AI Grunt1 • ai_Recorder_Auto – Record AI activity in Editor game mode for AI Debugger • ai_DebugTacticalPoints – Debug TPS queries AI Debug Draw – (2/2) • ai_DrawPath Grunt1 – Draw the path of a specific AI (can be “all”, for all AIs) • ai_DrawPathFollower – Draw the actual path following • ai_DrawSmartObjects – Display Smart Objects, their classes and attributes • ai_DebugDrawEnabledActors – List currently enabled AIs AI Debugger • AI Debugger can track a number of AI features: active behavior, active goalpipe, signal received, attention target, comments created with AI.RecComment etc. Scriptbind Functions • Some AI functionality is available in Lua, for example – – – – – – – – – – – AI.CreateGoalPipe AI.Signal AI.FindObjectOfType AI.GetAttentionTargetType – Visual, Memory, Sound, None AI.GetAttentionTargetAIType – Actor, Grenade, Car etc. AI.GetRefPointPosition AI.DistanceToGenericShape AI.SetBehaviorVariable (change behavior) AI.CanMelee AI.RecComment (make comment for Visual AI Debugger) … Questions?