770 likes | 964 Views
From the Ground Up: A.I. Architecture and Design Patterns. Dr Brett Laming Rockstar Leeds. Contents. Basic Principles Think/Act - The I/O Divide An Algorithmic Approach A Functional Approach Movement Animation Conclusion. Basic Principles. It’s a Competitive Market…
E N D
From the Ground Up: A.I. Architecture and Design Patterns Dr Brett Laming Rockstar Leeds
Contents • Basic Principles • Think/Act - The I/O Divide • An Algorithmic Approach • A Functional Approach • Movement • Animation • Conclusion
It’s a Competitive Market… Higher Expectations Tighter Schedules Multiple Platforms Simultaneous Titles …But Patterns Are Everywhere Identify, generalise, reuse, evolve Reliability Production Speed More Fun Stuff Why Do We Care?
Inspirations Marvin Minsky Different representations for different views Daniel Dennet Behaviour can be viewed at the physical, design and intentional levels David Marr Computational, algorithmic and implementational Damian Isla Cognitive Maps Spatial Relations Semantics Noam Chomsky Hierarchical decomposition Structure vs Meaning “Colorless green ideas sleep furiously” Chris Hecker Style vs Structure [2008] What is the textured triangle of A.I. Craig Reynolds Simple rules Complex behaviour
What Am I Looking For? Algorithms Concepts Hierarchy Components Reusability Best Practice Commutability
Observation How might it work? Introspection What would I do? Generalisation I’ve done this before They do the same Bad Experience Lets not do that again Background I studied this once? Could I apply this? How Do I Find Them?
Best Practice • Prototype new ideas where possible • Get visual and design direction • Mock-ups • Prove (or disprove) the concept • Quick and dirty programming • Play to peoples strengths • Maths Physics Guys • Navigation Mesh Collision Guys
Best Practice • Program Defensively • Assert and Unit Test • Automated Scripts as soak tests • One co-ordinate system and S.I. units • Maximise Workflow • Cater tools to their needs • Put new functionality on bypass • Think of the man-hour cost! • Minimise potential for human error
Best Practice • Build A Debugging Suite • Instant Pause • Flyable Camera • Layered Information • Action Histories • Maintain Player Immersion • A.I. should not be too bad or too good • Limit ourselves to what the player would know • Constrain them to the same actions
Sensory Data Think Controller Act The Think-Act Loop • Key tenants • Use only what we might know • Mimic the player Player A.I. Sensory Receptors Brain Controller Game
Sensory Input Sensory Data Think Controller Act
Sensory Data • Detail level • Depends on genre and perceived communication • Thief vs Medieval Total War II • General Model • Visual component an arc • Auditory component a radius • Auditory targets less official • Occlusion too expensive? • Shoot a weapon to get info • Theorise using ghost images
Blackboards • Used To Share Information • Static blackboard stores defined types of info • Dynamic blackboard stores arbitrary data • Agents write to the board • Generally read it as well Agent A Agent B {10,20,15} {-30,20,13} Scout {3, 17, 10} Cover {17, 11,5} {-10,15,12} {17,11,5}
Control Output cSensorCone Think Controller Act
Purpose Carries control instructions Provides a strict I/O Divide Notice the const correctness Unifies player and A.I. Control mapping for the player A.I. fills it in from Think() Key Properties Never stored in it’s entirety Created on the stack Lifetime of a single Process() Virtual Controller (yoke) Process() cVirtualYoke yoke Think(yoke&) const Controller Act(const yoke&)
Process() cBlackboard cVirtualYokeyoke Think(yoke&) const cSensorCone Act(const yoke&) … So Actually
Composite Collection of smaller yokes Allows selective storage cLocomotiveYoke fp32 mGas; fp32 mSteering; fp32 mPitch; cEntity *mpDontAvoid; Context Based Controller Input Logic Flow Control cWeaponYoke enum eFire { F_PRESSED, F_WHEN_IN_CONE, F_WHEN_LOCKED }; eFire mFireButton; Conditional signals Virtual Yoke cVirtualYoke cLocomotionYoke mLocomotion; cWeaponYoke mWeapons; cAnimYoke mAnimation;
Taking Action Process() cBlackboard cVirtualYokeyoke Think(yoke&) const cSensorCone Act(const yoke&)
Act() yoke iWeaponMgr* iLocomotive* Act • Applies Yoke Commands • Composite yokes Subsystems • Object Models • Supply common interfaces • Could be a turret mounted weapon or my pistol. • Could be driving a car, a plane, a boat or myself!
Self Contained Instructions for Think Actions for Act Commutable Plug and Play Downloadable Content Maybe broadcast use Think Sims 2! Object Model iLocomotive // Fills in yoke ComputeMotion(const cTarget &, cLocomotiveYoke&) // Computes forces ApplyMotion(const cTarget &, const cLocomotiveYoke&, v3 &force, v3 &torque)
Compute Compute Compute Apply Apply Apply More Coolness ApplyMotion(const cTarget& targ, cLocomotiveYoke& yoke) { v3 force(Zero); v3 torque(Zero); if (Drivable()) { Drivable()->ApplyMotion( targ, yoke, force, torque); } if (Floatable()) { Floatable()->ApplyMotion( targ, yoke, force, torque); } ApplyForceTorque(force, torque); } Compute Apply
Frame Time Process() cBlackboard cVirtualYokeyoke dt() Think(yoke&, dt) const cSensorCone Update(dt) Act(const yoke&, dt)
Work into everything Including fixed time steps No more s+=v, s+=v*dt() Benefits Integration Implicit forward Euler Rough Smoothing Closed Feedback Pause dt=0 Level Of Detail dt=2dt Special Effects dt() smoothing feedback control dt()
Problem Domain Needs a problem • Examine the Terminology • Feeling, Knowledge, Goals, Beliefs, Needs • Examine the Concepts • Decisions, Facts, Uncertainty, Exploration, Verb-Noun Actions, Repetition, Sequencing
What Are We Doing? • Goal based reasoning • Working to solve a goal • Thinking about and realising smaller tasks • Taking a hierarchical approach • Using a limited number of short verb-noun pairings to form a plan • We’ve seen this before • An old pattern • Colossal Cave Adventure and MUDs • Verb-noun actions like “get axe, wield axe” separated by movement “n, e, e, s, e” • We use a container object model
Know of “Mine, Smithy” Goto “Mine” Know of “Wall” Get Ore From “Wall” Goto “Smithy” Know of “Door” Use “Door” Play “Open Door” Warp Inside Know of “Owner”, “Forge” Play “Close Door” Goto “Owner” Put 10 gp In “Owner” Get Time From “Owner” Goto “Forge” Put Time In “Forge” Put Iron In “Forge” Use “Forge” Use Enchant Use “Forge” Get Sword From “Anvil” Applying The Pattern Taking the computational to the algorithmic
Easy Questions • Why did I choose to do this again? • Because we were driven to it by personality and need • What happens when I get the treasure? • I’ll probably choose to do something else depending on my mood • Why stop at get iron? • Because its reached the atomic level - there are no more questions, just results
Explicit Order “Script here, Please greet the player” Putting It Together Ambient Controller “Get treasure” Planning “Get magic sword”
Explicit Order “Script here, Please greet the player” Explicit Orders • Script commands • Script on A.I. • Autopilot • Player on Player • Player instructions • Player on A.I. • Squad Commander • A.I. on A.I. Ambient Controller “Get treasure” Planning “Get magic sword”
Scripting Notes • Don’t mix styles • Script has immediate control • Script waits for an opportunity • Keep common properties separate • No sharing memory locations • A script global population density • A code global population density • Maintain a set order of calculation • Generally consistent with style
Explicit Order “Script here, Please greet the player” Ambient Controller • Generates sensible actions autonomously • Maybe Idle • Maybe Full Daily Routine • Daily Routines • Character properties • Needs/Drives • Scheduling • Time of day • nice emergent behaviour Ambient Controller “Get treasure” Planning “Get magic sword”
Hunger Libido dt() Daily Routine Time Of Day Sleep Drive Model Goto Work Work Character Leave Work Relax Schedule Go Home
Planning Goto Position “Goto Forge” Explicit Order “Script here, Please greet the player” Perform Action “Use Forge” Planning Continuous bar dt() • Defines Think() Ambient Controller “Get treasure” Planning “Get magic sword”
Get/Take “Object” From “Container” Primary world manipulation Contents determine state Goto “Location” Waits on dt() Key A.I. output Complex Warrants special attention later Plan Components • Play “Animation” • Waits on dt() • Use “Object” • Object model again • Broadcast actions. • Change world based on state • May wait on dt()
Search Based Planning • Traditional academic approach • See STRIPS, Hierarchical Task Networks, Bratko • The Good • Mimics our introspective reasoning • Seeks to fully realise a plan to the goal • Directed search for optimal solutions • Post processing even more so • The Bad • Knowledge representation • Scalability - difficult for video games • Lots of storage
Procedural Planning • Industry preferred approach • Hierarchical • Easy to comprehend • Limited Language • Goals • Sub-goals • Conditions • Actions • Transitions • Sequential • Decision Based • Powerful Results Get Object Buy Short of money? Make Thief skills? Steal Goto Mine Get Ore
Procedural Planning - Issues • Competing children • We have to make a best guess from the options • A* might help • But we could still end up with case of a basic sword being bought but not affording the forge. • Incomplete plan means no post process • Not good for player supporting A.I. • More action for generic A.I.
The Curve Ball • Task Interruption • I’m returning to my gang hideout • I see an enemy • I engage the enemy • I roll out of the way of a car • I recover to my feet • I re-engage the enemy • I continue to return to my gang hideout • … Is A Key Requirement of our A.I.
Death Examine Kill Death Examine Kill Gain Range Gain Range • HFSM came along • Eased transitions • But history still an issue Look At Attack Look At Attack Finite State Machines • No plan history • No idea of context • No generalised exit. • Hideous state history workarounds • Don’t scale well • Many transitions
Behaviour Trees Get Object Decorators Selection Crime Allowed? Create Buy Low money? Get Material Ok money? Steal Sequencing Thief Class? Goto Source Take Material Preconditions Actions
Modelling Interruption? Ambient Behaviour Return Home Kill Enemy Parallel? Fire Weapon Locate Hideout Goto Location Gain Range
Simple and powerful Limited vocabulary Most situations handled Highly flexible Plug and play Customisable Nice design tools Handy child evaluation Lends itself to directed decision making Issues Interruption handling Where to return Amount of flexibility Trees get complicated Behaviour Trees
MARPO Get Object Thief class? Thieving allowed? Not enough money? Money? Steal Create Buy Get Material Goto Source Take Material
MARPO Long term Reactive Immediate Get Object Create Get Material Take Material
Create Gain Distance Get Material Take Material MARPO Long term Reactive Immediate Get Object Kill Enemy
Gain Distance MARPO Long term Reactive Immediate Get Object Kill Enemy Roll