![Page 1: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/1.jpg)
Principles of Computer Game Design and Implementation
Lecture 24
![Page 2: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/2.jpg)
We already learned
• Decision Tree
• Finite State Machine
2
![Page 3: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/3.jpg)
3
FSM Problems: Reminder
• Explosion of states• Too predictable• Often created with ad hoc structure• Mixture of different level concepts:
– Game engine developer• “Atomic” actions and tests linking AI to the game world
– AI developer• Complex behaviours
– FSM States combine both• What to do with more than one action per state?
![Page 4: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/4.jpg)
Outline for today
• Behaviour tree
4
![Page 5: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/5.jpg)
Behaviour Trees
• Inspired by a number of techniques
– Hierarchical FSMs
– Scheduling / planning
– Planning
• First (famously) used in Halo 2
– Picked up by other developers
• Clear separation between AI and Game Engine
5
![Page 6: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/6.jpg)
Tasks
AI agent runs a task. A task can succeed or fail
• Simple tasks
– Conditions
– Actions
• Complex tasks
– Built hierarchically from other tasks using
• Composites
• Decorators
6
Game engine developers
AI developers
![Page 7: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/7.jpg)
Conditions
• Test some properties of the game.
– Proximity
– Line of sight
– Character properties (has ammo etc)
• Succeed or fail
– Like if-then test
• Typically execute fast
7
![Page 8: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/8.jpg)
Actions
• Alter the state of the game
– Animation, audio
– Play a dialog
– Movements
– Change the character internal state (cure)
• Can take time
• Typically succeed
– Failing is like an exception
8
![Page 9: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/9.jpg)
Task Interface
• Actions and tests are used in other AI techniques
but…
• In behaviour trees, all tasks have the same interface
– Simple case: return a Boolean value
• Succeed / fail
– Can be easily combined together
9
![Page 10: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/10.jpg)
Composites
Composites run their child tasks in turn
• Sequence– Terminates immediately with failure
if any of child tasks fail
– Succeeds if all child tasks succeed
• Selector– Terminates immediately with success
if any of the child tasks succeed
– Fails if all child tasks fail
10
… … …
… … …
![Page 11: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/11.jpg)
Sequence of Actions
• Sequence of tasks to achieve a goal
– Get ready for Uni task
11
Wake up Get DressedWash up
![Page 12: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/12.jpg)
Sequences of Sequences
• Logically, there is no need to have sequences as children of sequences, but…
12
Put socksPut shirt on EatCook meal
Dress up Eat
![Page 13: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/13.jpg)
Sequence As Conditions
• Sequence terminates immediately with failureif any of child tasks fail
– The second task is run only when first succeeds
13
Socks clean? Put on
![Page 14: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/14.jpg)
Conditions and Actions
• More than one child
• But what if socks are not clean?
14
Socks clean? Put right onPut left on
![Page 15: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/15.jpg)
Selectors
Terminate immediately with success if any of the child tasks succeed
15
Socks clean? Put on Get new socks Put on
?
![Page 16: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/16.jpg)
More Complicated Behaviour
16
Socks clean? Put onMove to
chest
Get socks
?
?
Drawer open?
Put on
Get socksOpen drawer
![Page 17: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/17.jpg)
Conditions Actions and Composites
• Conditions and actions combined together with composites allow to express complex behaviours
• Goal-driven scripting
• Reactive plans: what if…
– But not a planner!
17
![Page 18: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/18.jpg)
Halo 2 Decision-Making
From Demián Isla’sGDC’05 presentation
Root
Self-preservation
Engage
Search
Charge
Fight
Guard
Cover
Presearch
Uncover
Guard
Grenade
Investigate
Idle Guard
Retreat Flee
Vehicle fight
Vehicle strafe
MeleeRoot
Engage
Search Uncover
![Page 19: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/19.jpg)
Bug Fixes as a Hack
• Behaviour trees are highly adaptable
– Suppose you discovered a very rare condition under which AI fails
– You know what should happen
– But time is pressing
19
?
Correct behaviour
![Page 20: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/20.jpg)
Decorators
• Decorators modify the behaviour of a task
– Limit (Loop)
• Time limit / Attempts
– UntilFail
• Repeat the task until it fails
– Inverter
– Ignorer
• Runs the task and always reports success
20
![Page 21: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/21.jpg)
Put socks on
Decorators Example (1)
21
Socks found?
ignore Put shoes on
Dressing up
![Page 22: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/22.jpg)
Decorators Example (2)
22
Guard AI
![Page 23: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/23.jpg)
Guarding Resources with Decorators
• Semaphore decorator
– Every instance refers to the same flag
– Whenever an AI entity tries to access resource, checks for the flag
• If available, set the flag, run the task, unset the flag
23
Sound engine semaphore
Play sound
Ignore
![Page 24: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/24.jpg)
Implementation
public class Task {
Boolean run()
}
public class Composite extends Task {
Composite (Vector<Task> subtasks)
}
…
24
Quite straightforward but…
![Page 25: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/25.jpg)
BTs and Multitasking
• So far we did not consider multitasking
– Decision trees execute fast
– FSMs state determines what to do
• In behaviour trees, tasks may span over time
– Either use multithreading
• Every tree is being run by a thread
– Or use scheduling
25
![Page 26: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/26.jpg)
Tick-based model
•Ti
ck-b
ased
mo
del
fro
m
htt
p:/
/jb
t.so
urc
efo
rge.
net
/
26
![Page 27: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/27.jpg)
Parallel Composites
• In presence of multitasking, one can run tasks in parallel
– E.g. for group behaviours
27
Soldier 1: attack
Soldier 1: Has ammo?
Soldier 2: attack
Soldier 2: Has ammo?
Group attack
![Page 28: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/28.jpg)
Event Handling
BT event supportis poor
28
![Page 29: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/29.jpg)
Data in BTs
• One of strong points of BT model is that all tasks have same interface
• Tasks cannot take parameters as input
• Use blackboard AKA notice board for communication (see your COMP213 notes)
29
![Page 30: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/30.jpg)
Blackboard for Inter-Task Communication
30
![Page 31: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/31.jpg)
Extensions
• Priority of sub tasks for composites– Dynamic priority
• Low health -> “take cover” gets higher priority
– kicking out of lower priority behaviour
• Probabilistic
• One-off tasks (random choice but do not repeat)
• Interrupting tasks
31
![Page 32: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/32.jpg)
Halo 2: Impulses (1)Problem: What happens (with a prioritized list) when the priority
is not constant?
Unless the
player is in
vehicle, in
which case...
Charge
Fight
Vehicle entry
Engage
Charge
Fight
Vehicle entryEngage
From Demián Isla’s GDC’05 presentation
![Page 33: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/33.jpg)
Halo 2: Impulses (2)
Solution: Separate alternative trigger conditions out into separate impulse
Two execution options
• In-place
• Redirect
Charge
Fight
Vehicle entry
Player vehicle
entry impulseEngage
Charge
Fight
Vehicle entry
Vehicle entry
Engage
Charge
Fight
Vehicle entry
Player vehicle
entry impulseEngage
Charge
Fight
Vehicle entry
Self-preserve on
damage impulseEngage
Root
Self-preserve
Charge
Fight
Vehicle entry
Self-preserve on
damage impulseEngage
Root
Self-preserve
Charge
Fight
Vehicle entry
Self-preserve on
damage impulseEngage
Root
Self-preserve
Charge
Fight
Vehicle entry
Player vehicle
entry impulseEngage
From Demián Isla’s GDC’05 presentation
![Page 34: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling](https://reader033.vdocuments.us/reader033/viewer/2022060408/5f0ff4387e708231d446b65c/html5/thumbnails/34.jpg)
Behaviour Trees: Summary
• Advantages
– Easy to understand
– Builds on past experience
– Executable system specification
– Support parallelism
• Disadvantages:
– Reactive and state-based behaviour may be awkward to describe
34