basic game ai imgd 4000 with material from: ian millington and john funge. artificial intelligence...
TRANSCRIPT
![Page 1: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/1.jpg)
Basic Game AI
IMGD 4000
With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)
![Page 2: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/2.jpg)
2
What’s the AI Part of a Game?
• Everything that isn’t graphics (sound) or networking... (says an AI professor )
– or physics (though sometimes lumped in)
– usually via non-player characters
– but sometimes operates more broadly, e.g.,
• Civilization-style games (sophisticated simulations)
• interactive storytelling (drama control)
![Page 3: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/3.jpg)
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
![Page 4: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/4.jpg)
4
This course
• Basic game AI– Decision-making techniques commonly used in almost
all games• Basic pathfinding (A*) (IMGD 3000)• Decision trees (this deck) • (Hierarchical) state machines (this deck)
• Advanced game AI – Used in practice, but in more sophisticated games
• Advanced pathfinding (later deck)• Behavior trees in Halo 3 (maybe after that)
![Page 5: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/5.jpg)
5
Future Game AI?
• Take IMGD 4100 in 2016– “AI for Interactive Media and Games”• Fuzzy logic• More goal-driven agent behavior
• Take CS 4341 “Artificial Intelligence”– Machine learning– Planning
![Page 6: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/6.jpg)
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 (or need ways to “short-circuit”)• 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– Which one is better? Whichever has better knowledge. ;-)
6
![Page 7: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/7.jpg)
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
7
![Page 8: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/8.jpg)
8
AI Coding Theme (for Basic AI)
• Use object-oriented paradigm
instead of...
• A tangle of if-then-else statements
![Page 9: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/9.jpg)
Outline
• Introduction (done)• Decision Trees (next)• Finite State Machines (FSM)• Hierarchical FSM
![Page 10: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/10.jpg)
First Basic AI Technique:
Decision Trees
10
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)
![Page 11: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/11.jpg)
11
Decision Trees
• Most basic of the basic AI techniques
• Easy to implement
• Fast execution
• Simple to understand
![Page 12: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/12.jpg)
12
Deciding How to Respond to an Enemy
if visible? { if close? { attack; } else { if flank? { move; } else { attack; } } } else { if audible? { creep; }}
attack
moveattack
creep
yes
visible?
flank?
close?audible?no
yesyes
yes
no
no
no
Leaves are actionsInterior nodes are decisions
![Page 13: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/13.jpg)
13
if visible? { if close? { attack; } else { if flank? { move; } else { attack; } }
if visible? { if close? { attack; } else if flank? { move; } else { attack; }} else if audible? { creep;}
Modifying Deciding How to Respond to an Enemy
attackcreep
yes
visible?
close?audible?no
yesyes
no
no
moveattack
flank?
yesno
???
??? yes
Alternate form. Harder to see “depth”!Modification in either form tough!
![Page 14: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/14.jpg)
14
O-O Decision Trees (Pseudo-Code)
class Node def decide() // return action
class Boolean : Decision // if yes/no yesNode noNode
class MinMax : Boolean // if range minValue maxValue testValue
def getBranch() if maxValue >= testValue >= minValue return yesNode else return noNode
yes
noyesyes
yes
no
no
no
class Decision : Node // interior
def getBranch() // return node
def decide() return getBranch().decide()
class Action : Node // leaf def decide() return this
![Page 15: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/15.jpg)
15
Building an O-O Decision Treevisible = new Boolean...audible = new Boolean...close = new MinMax...flank = new Boolean...
attack = new Attack...move = new Move...creep = new Creep...
visible.yesNode = closevisible.noNode = audible
audible.yesNode = creep
close.yesNode = attackclose.noNode = flank
flank.yesNode = moveflank.noNode = attack...
attack
moveattack
creep
yes
visible?
flank?
close?audible?no
yesyes
yes
no
no
no
...or a graphical editor
![Page 16: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/16.jpg)
16
Modifying an O-O Decision Treevisible = new Boolean...audible = new Boolean...close = new MinMax...flank = new Boolean...??? = new Boolean...
attack = new Action...move = new Action...creep = new Action...
visible.yesNode = closevisible.noNode = audible
audible.yesNode = creep
close.yesNode = attackclose.noNode = ??????.yesNode = flank
flank.yesNode = moveflank.noNode = attack...
attackcreep
yes
visible?
close?audible?no
yesyes
no
no
moveattack
flank?
yesno
???
yes
![Page 17: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/17.jpg)
17
Decision Tree Performance
• 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– put most expensive decisions late
yes
noyes
yes
yes
no
no
no
![Page 18: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/18.jpg)
Outline
• Introduction (done)• Decision Trees (done)• Finite State Machines (FSM) (next)• Hierarchical FSM
![Page 19: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/19.jpg)
19
Second Basic AI Technique:
(Hierarchical) Finite State Machines
![Page 20: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/20.jpg)
20
Finite State Machines
on guard
run away
fightsmall enemy
large enemylosing fightescaped
![Page 21: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/21.jpg)
21
Hard-Coded Implementationclass Soldier
enum State ON_GUARD FIGHT RUN_AWAY
currentState
def update() if currentState == ON_GUARD { if small enemy { currentState = FIGHT start Fighting } else if big enemy { currentState = RUN_AWAY start RunningAway } } else if currentState == FIGHT { if losing fight { currentState = RUN_AWAY start RunningAway } } else if currentState == RUN_AWAY { if escaped { currentState = ON_GUARD start Guarding } }
on guard
run away
fightsmall enemy
large enemylosing fight
escaped
![Page 22: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/22.jpg)
22
Hard-Coded State Machines
• Easy to write (at the start)
• Very efficient
• Notoriously hard to maintain (e.g., modify and
debug)
![Page 23: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/23.jpg)
23
Cleaner & More Flexible O-O Implementation
class State def getAction() def getEntryAction() def getExitAction() def getTransitions()
class Transition def isTriggered() def getTargetState() def getAction()
class StateMachine
states initialState currentState = initialState
def update() // returns all actions needed this update
triggeredTransition = null
for transition in currentState.getTransitions() { if transition.isTriggered() { triggeredTransition = transition break } } if triggeredTransition != null { targetState = triggeredTransition.getTargetState() actions = currentState.getExitAction() actions += triggeredTransition.getAction() actions += targetState.getEntryAction() currentState = targetState return actions // list of actions for transitions } else return currentState.getAction() // action this state
on guard
run away
fightsmall enemy
large enemylosing fight
escaped
![Page 24: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/24.jpg)
24
Combining Decision Trees & State Machines (1 of 2)
• Why?– to avoid duplicating expensive tests in state
machine. e.g., assuming “player in sight” is expensive
alert
defend
alarmplayer in sight AND far
player in sight AND near
![Page 25: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/25.jpg)
25
Combining Decision Trees & State Machines (2 of 2)
alert
defend
alarm
player in sight?
far?
yes
yes
no
no
![Page 26: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/26.jpg)
Outline
• Introduction (done)• Decision Trees (done)• Finite State Machines (FSM) (done)• Hierarchical FSM (next)
![Page 27: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/27.jpg)
27
Hierarchical State Machines
• Why? Could be interruptions
search
goto disposal
goto trash
see trash
trash disposed
have trash
E.g., robot can run out of power in any state.Needs to recharge when out of power.When charged, needs to return to previous state.
![Page 28: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/28.jpg)
28
Interruptions (e.g., Recharging)
search
goto disposal
goto trash
see trash
trash disposed
have trash
recharge
low powerrecharged
recharge
low powerrecharged
recharge
low powerrecharged
(search) (trash)
(disposal)6 states needed doubled!
![Page 29: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/29.jpg)
29
Add Another Interruption (e.g., Baddies)
12 states needed doubled!
hide
battleall clear
(search/recharge)
hide
hide
hide
hide
hide
![Page 30: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/30.jpg)
30
Hierarchical State Machine
search
goto disposal
goto trash
see trash
trash disposed
have trash
clean
rechargelow power
recharged
• Leave any state in (composite) “clean” state when “low power”
• “clean” remembers internal state and continues when back from “recharge’’
![Page 31: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/31.jpg)
31
Add Another Interruption (e.g., Baddies)
search
goto disposal
goto trash
see trash
trash disposed
have trash
clean
rechargelow power
recharged
hidebattle
all clear
7 states needed (including composite) vs. 12
battle all clear
hide(recharge)
(clean)
![Page 32: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/32.jpg)
32
Cross-Hierarchy Transitions
• Why?– Suppose we want robot to “top off” battery (even
if it isn’t low) when it doesn’t see any trash
search
goto disposal
goto trash
see trash
trash disposed
have trash
clean
rechargelow power
recharged
![Page 33: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/33.jpg)
33
Cross-Hierarchy Transitions
search
goto disposal
goto trash
see trash
trash disposed
have trash
clean
rechargelow power
recharged
no trash and less than 75% power
![Page 34: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/34.jpg)
34
HFSM Implementation Sketchclass State
// stack of return states def getStates() return [this]
// recursive update def update()
// rest same as flat machine
class Transition
// how deep this transition is def getLevel()
// rest same as flat machine
struct UpdateResult // returned from update transition level actions // same as flat machine
class HierarchicalStateMachine
// same state variables as flat machine
// complicated recursive algorithm* def update () class SubMachine : HierarchicalStateMachine, State
def getStates() push this onto currentState.getStates()
*See full pseudo-code at http://web.cs.wpi.edu/~imgd4000/d15/slides/millington-hsm.pdf
![Page 35: Basic Game AI IMGD 4000 With material from: Ian Millington and John Funge. Artificial Intelligence for Games, Morgan Kaufmann, 2009. (Chapter 5)](https://reader036.vdocument.in/reader036/viewer/2022062423/5697bfd21a28abf838cabbe5/html5/thumbnails/35.jpg)
Resource Links
• HFSM from Millington and Fungehttp://web.cs.wpi.edu/~imgd4000/d15/slides/millington-hsm.pdf
• FSM from IMGD 3000– Slideshttp://www.cs.wpi.edu/~imgd4000/d15/slides/imgd3000-fsm.pdf
– Header fileshttp://dragonfly.wpi.edu/include/classStateMachine.html
• UE4 Behavior Tree– Decision tree + HSM = Behavior treehttps://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/QuickStart/
– Cannot use for your state-based AI!