4/3/2016 What’s AI Part of a Game? • Everything that isn’t graphics (sound) or networking ... (says an AI professor ☺ ) Basic Game AI – or physics (though sometimes lumped in) – usually via non-player characters IMGD 4000 – but sometimes operates more broadly, e.g., • Civilization -style games (sophisticated simulations) • interactive storytelling (drama control) With material from: Ian Millington and John Funge. Artificial Intelligence for Games , Morgan Kaufmann, 2009. (Chapter 5) 2 “Levels” of Game AI This Course • Basic • Basic game AI – Decision-making techniques commonly used in almost – Decision-making techniques commonly used in almost all games all games • Basic pathfinding (A*) (IMGD 3000) • Advanced • Decision trees (this deck) • (Hierarchical) state machines (this deck) – Used in practice, but in more sophisticated games • Advanced game AI • Future – Used in practice, but in more sophisticated games • Advanced pathfinding (other deck) – Not yet used, but explored in research • Behavior trees in UE4 (this deck) 3 4 Two Fundamental Types of AI Future Game AI? Algorithms • Non-Search vs. Search • Take IMGD 4100 – Non-Search: amount of computation is predictable – “AI for Interactive Media and Games” • e.g., decision trees, state machines – Search: upper bound depends on size of search space • Fuzzy logic (often large) • More goal-driven agent behavior • e.g., minimax, planning. Sometimes pathfinding • Scary for real-time games (or need ways to “short-circuit”, e.g., pathfind to closer node) • Take CS 4341 • Need to otherwise limit computation (e.g., threshold, time-slice pathfinding) – “Artificial Intelligence” • Where’s the “knowledge”? – Non-Search: in the code logic (or external tables) • Machine learning – Search: in state evaluation and search order functions • Planning – Which one is better? Whichever has better knowledge. ;-) 5 6 1
4/3/2016 How About AI Middleware (“AI AI Coding Theme (for Basic AI) Engines”)? • Recent panel at GDC AI Summit: “Why so wary of • Use object-oriented paradigm AI middleware?” • Only one panelist reported completely positive instead of... experience – Steve Gargolinski, Blue Fang (Zoo Tycoon, etc.) – Used Havok Behavior (with Physics) • A tangle of if-then-else statements • Most industry AI programmers still write their own AI from scratch (or reuse their own code) – Damian Isla, Flame in the Flood , custom procedural content generation • So, we are going to look at coding details 7 8 Outline • Introduction (done) First Basic AI Technique: • Decision Trees (next) • Finite State Machines (FSM) Decision Trees • Hierarchical FSM • Behavior Trees See code at: https://github.com/idmillington/aicore src/dectree.cpp and src/demos/c05-dectree Ian Millington and John Funge. Artificial Intelligence for Games , Morgan Kaufmann, 2009. (Chapter 5) 10 Deciding How to Respond to an Enemy Decision Trees (1 of 2) Leaves are actions • Most basic of the basic AI techniques if visible? { // level 0 Interior nodes are decisions visible? if close? { // level 1 no yes attack ; } else { // level 1 • Easy to implement audible? close? no if flank? { // level 2 no yes move ; yes } else { // level 2 flank? • Fast execution attack ; creep attack } no yes } } else { // level 0 • Simple to understand if audible? { // level 1 attack move creep ; } } Typically binary (if multiple choices, can be converted to binary) 11 12 2
4/3/2016 Deciding How to Respond to an Enemy Modifying (2 of 2) Deciding How to Respond to an Enemy visible? Alternate form. Alternate form. Harder to see “depth”! no yes visible? no yes audible? close? if visible? { no if visible? { // level 0 if visible? { // level 0 if close? { no yes audible? close? if close? { // level 1 if close? { // level 1 yes no attack; attack; attack; ??? no yes } else if flank? { // level 1&2 } else if flank? { // level 1&2 } else { yes move; move; yes if flank? { creep flank? ??? attack } else { } else { move; attack; attack; creep flank? attack no yes } else { } } } else if audible? { // level 0&1 } else if audible? { // level 0&1 attack; no yes creep; creep; } } } } attack move attack move Harder to see “depth”! What if need to modify? ??? e.g., if close , only flank if ally near Modification restructures all below code! Code is brittle . Solution? � Object-Oriented 14 13 Building an O-O Decision Tree O-O Decision Trees (Pseudo-Code) no yes class Boolean : Decision // if yes/no visible = new Boolean... yesNode no audible = new Boolean... no yes yes noNode close = new MinMax... visible? flank = new Boolean... class MinMax : Boolean // if range no yes no yes minValue attack = new Attack... maxValue audible? close? testValue move = new Move... no creep = new Creep... no yes def getBranch() class Node yes if maxValue >= testValue >= minValue def decide() // return action/decision visible.yesNode = close return yesNode flank? else visible.noNode = audible creep return noNode attack no yes class Decision : Node // interior audible.yesNode = creep def getBranch() // return a node def decide() // Define root as start of tree close.yesNode = attack return getBranch().decide() Node *root attack move close.noNode = flank class Action : Node // leaf // Calls recursively until action flank.yesNode = move def decide() return this Action * action = root � decide() action � doAction() flank.noNode = attack ...or a graphical editor ... 15 16 Modifying an O-O Decision Tree Decision Tree Performance visible = new Boolean... audible = new Boolean... visible? close = new MinMax... • Individual node tests ( getBranch ) typically flank = new Boolean... no yes ??? = new Boolean... constant time (and fast ) audible? close? no attack = new Action... no yes • Worst case behavior depends on depth of tree move = new Action... yes ??? creep = new Action... – longest path from root to action creep yes visible.yesNode = close attack visible.noNode = audible • Roughly “balance” tree (when possible) flank? audible.yesNode = creep no yes no yes – not too deep, not too wide no close.yesNode = attack no yes – make commonly used paths shorter yes close.noNode = ??? ???.yesNode = flank attack move no yes – put most expensive decisions late flank.yesNode = move flank.noNode = attack ... 17 18 3
4/3/2016 Outline • Introduction (done) Second Basic AI Technique: • Decision Trees (done) • Finite State Machines (FSM) (next) (Hierarchical) Finite State Machines • Hierarchical FSM • Behavior Trees 20 Finite State Machines Finite State Machines • Often AI as agents: sense , think , then act • But many different rules for agents – Ex: sensing, thinking and acting when fighting , running , exploring … small enemy on guard fight • Acting done states – Can be difficult to keep rules consistent! • Sensing done by • Try Finite State Machine conditionals large enemy – Natural correspondence between states and behaviors • Thinking done by escaped losing fight transitions – Easy: to diagram, program, debug • Formally: – Set of states run away – A starting state – An input vocabulary – A transition function that maps inputs and current state to next state (Game example next slide) 22 Hard-Coded Implementation Hard-Coded State Machines class Soldier def update() if currentState == ON_GUARD { enum State • Easy to write (at the start) if small enemy { ON_GUARD currentState = FIGHT FIGHT start Fighting RUN_AWAY • Very efficient } else if big enemy { currentState = RUN_AWAY currentState start RunningAway } • Notoriously hard to maintain (e.g., modify and } else if currentState == FIGHT { if losing fight { currentState = RUN_AWAY debug) small enemy on guard fight start RunningAway } large enemy } else if currentState == RUN_AWAY { losing fight escaped if escaped { currentState = ON_GUARD run away start Guarding } } 23 24 4
Recommend
More recommend