Basic Game AI Technical Game Development II Professor Charles Rich Computer Science Department rich@wpi.edu With material from: Millington and Funge, Artificial Intelligence for Games , Morgan Kaufmann 2009. (Chapter 5) IMGD 4000 (D 17) 1 Definitions? § What is artificial intelligence (AI) ? • subfield of computer science ? • subfield of cognitive science ? § What is “AI for Games” ? • versus “academic AI” ? In games, everything (including the AI) is in service of the player’s experience (“fun”) • What does it mean for a game AI to “cheat”? Resources: introduction to Buckland, www.gameai.com, aigamedev.com, www.aiwisdom.com, www.ai4games.org IMGD 4000 (D 17) 2 1
What’s the AI part of a game? § Everything that isn’t graphics (sound) or networking... J • or physics (though sometimes lumped in) • usually via the non-player characters • but sometimes operates more broadly, e.g., – Civilization-style games (sophisticated simulations) – interactive storytelling (drama control) IMGD 4000 (D 17) 3 “Levels” of Game AI § Basic • decision-making techniques commonly used in almost all games § Advanced • used in practice, but in more sophisticated games § Future • not yet used, but explored in research IMGD 4000 (D 17) 4 2
This course § Basic game AI • decision-making techniques commonly used in almost all games – basic pathfinding (A*) (IMGD 3000) – decision trees (today) – (hierarchical) state machines (today) – behavior trees (becoming basic ) (today) § Advanced game AI • used in practice, but in more sophisticated games – advanced pathfinding (last Thursday) – real-time camera control (last Monday) IMGD 4000 (D 17) 5 Future Game AI ? § Take IMGD 4100 “AI for Interactive Media and Games” • fuzzy logic • other special topics § Take CS 4341 “Artificial Intelligence” • machine learning • planning IMGD 4000 (D 17) 6 3
Two Fundamental Types of AI Algorithms § Non-Search vs. Search • Non-Search: amount of computation is predictable – e.g., decision trees, state machines • Search: upper bound depends on size of search space (often large) – e.g., minimax, planning – scary for real-time games – need to otherwise limit computation (e.g., threshold) § Where’s the “knowledge”? • Non-Search: in the code logic (or external tables) • Search: in state evaluation and search order functions IMGD 4000 (D 17) 7 How about AI Middleware (“AI Engines”)? § Recent panel at GDC AI Summit: “Why so wary of AI middleware?” § Only one panelist reported completely positive experience • Steve Gargolinski, Blue Fang (Zoo Tycoon, etc.) • Used Havok Behavior (with Physics) § Most industry AI programmers still mostly write their own AI from scratch (or reuse their own code) § So we are going to look at coding details IMGD 4000 (D 17) 8 4
AI Coding Theme (for Basic AI) instead of... § a tangle of if-then-else statements use... § object-oriented paradigm IMGD 4000 (D 17) 9 First Basic AI Technique: Decision Trees See code at: https://github.com/idmillington/aicore src/dectree.cpp and src/demos/c05-dectree IMGD 4000 (D 17) 10 5
Decision Trees § The most basic of the basic AI techniques § Easy to implement § Fast execution § Simple to understand IMGD 4000 (D 17) 11 Deciding how to respond to an enemy visible? if visible? { if close? { no yes attack; } else { audible? close? no if flank? { no yes move; yes } else { flank? attack; } creep attack no yes } } else { if audible? { creep; attack move } } IMGD 4000 (D 17) 12 6
Which would you rather modify? visible? if visible? { if visible? { no yes if close? { if close? { attack; attack; audible? close? } else { } else if flank? { no move; if flank? { ??? no yes yes } else { move; ??? attack; } else { } attack; yes creep attack } else if audible? { } creep; } flank? } } else { if audible? { no yes creep; } attack move IMGD 4000 (D 17) 13 O-O Decision Trees (Pseudo-Code) no yes no no yes yes no yes class Boolean : Decision class Node yesNode def decide() //return action noNode class Decision : Node class MinMax : Boolean def getBranch() //return node minValue def decide() maxValue return getBranch().decide() testValue def getBranch() class Action : Node if maxValue >= testValue >= minValue def decide() return this return yesNode else return noNode IMGD 4000 (D 17) 14 7
Building an O-O Decision Tree visible = new Boolean... audible = new Boolean... close = new MinMax... flank = new Boolean... visible? no yes attack = new Move... move = new Move... audible? close? creep = new Move... no no yes visible.yesNode = close yes visible.noNode = audible flank? creep audible.yesNode = creep attack no yes close.yesNode = attack close.noNode = flank attack move flank.yesNode = move flank.noNode = attack ... ...or a graphical editor IMGD 4000 (D 17) 15 Modifying an O-O Decision Tree visible = new Boolean... audible = new Boolean... close = new MinMax... visible? flank = new Boolean... ??? = new Boolean... no yes audible? close? attack = new Move... no move = new Move... no yes creep = new Creep... yes ??? visible.yesNode = close yes creep visible.noNode = audible attack flank? audible.yesNode = creep no yes close.yesNode = attack close.noNode = ??? ???.yesNode = flank attack move flank.yesNode = move flank.noNode = attack ... IMGD 4000 (D 17) 16 8
Decision Tree Performance Issues § individual node tests ( getBranch ) typically constant time (and fast ) § worst case behavior depends on depth of tree • longest path from root to action § roughly “balance” tree (when possible) • not too deep, not too wide • make commonly used paths shorter no yes no • put most expensive decisions late no yes yes no yes IMGD 4000 (D 17) 17 Second Basic AI Technique: (Hierarchical) State Machines IMGD 4000 (D 17) 18 9
Finite State Machines § Consider AI’s as “agents” that sense , think , then act § But many different rules for behaviors • Ex: sensing, thinking and acting when fighting vs. running , vs. exploring … • Can be difficult to keep rules consistent! § Try Finite State Machine • Natural correspondence between states and behaviors • Easy: to diagram, program, debug § Formally: • Set of states • A starting state • An input vocabulary • A transition function that maps inputs and current state to next state Game Example: Soldier Agent small enemy on guard fight large enemy escaped losing fight run away IMGD 4000 (D 17) 20 10
Hard-Coded Implementation def update() class Soldier if currentState == ON_GUARD { if small enemy { enum State currentState = FIGHT ON_GUARD start Fighting FIGHT } else if big enemy { RUN_AWAY currentState = RUN_AWAY start RunningAway currentState } } else if currentState == FIGHT { if losing fight { currentState = RUN_AWAY 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 } } IMGD 4000 (D 17) 21 Hard-Coded State Machines § Easy to write (at the start) § Very efficient § Notoriously hard to maintain (e.g., debug) IMGD 4000 (D 17) 22 11
Cleaner & More Flexible O-O Implementation class State class StateMachine def getAction() small enemy on guard fight def getEntryAction() states def getExitAction() initialState large losing def getTransitions() enemy escaped fight currentState = initialState class Transition def update() run away def isTriggered() def getTargetState() triggeredTransition = null def getAction() for transition in currentState.getTransitions() { if transition.isTriggered() { triggeredTransition = transition break } } if triggeredTransition != null { ...add tracing targetState = triggeredTransition.getTargetState() actions = currentState.getExitAction() actions += triggeredTransition.getAction() actions += targetState.getEntryAction() currentState = targetState return actions } else return currentState.getAction() IMGD 4000 (D 17) 23 Combining Decision Trees & State Machines § Why? • to avoid duplicating expensive tests in state machine: player in sight AND far alarm alert player in sight AND near defend IMGD 4000 (D 17) 24 12
Combining Decision Trees & State Machines yes alarm player in sight? alert far? yes no no defend IMGD 4000 (D 17) 25 Hierarchical State Machines § Why? goto see trash search trash have trash trash disposed goto disposal IMGD 4000 (D 17) 26 13
Interruptions (Alarms), e.g., Recharging? recharge recharge -search -trash recharged low power recharged low power see trash goto search trash have trash trash disposed goto disposal recharged low power 6 - doubled the number of states! recharge -disposal IMGD 4000 (D 17) 27 Add Another Interruption Type? hide -search-recharge all clear battle hide hide hide hide hide 12 - doubled the number of states again! IMGD 4000 (D 17) 28 14
Recommend
More recommend