Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Principles of Computer Game Design and Implementation
Lecture 24
We already learned
• Decision Tree
• Finite State Machine
2
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?
Outline for today
• Behaviour tree
4
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
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
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
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
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
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
… … …
… … …
Sequence of Actions
• Sequence of tasks to achieve a goal
– Get ready for Uni task
11
Wake up Get DressedWash up
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
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
Conditions and Actions
• More than one child
• But what if socks are not clean?
14
Socks clean? Put right onPut left on
Selectors
Terminate immediately with success if any of the child tasks succeed
15
Socks clean? Put on Get new socks Put on
?
More Complicated Behaviour
16
Socks clean? Put onMove to
chest
Get socks
?
?
Drawer open?
Put on
Get socksOpen drawer
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
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
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
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
Put socks on
Decorators Example (1)
21
Socks found?
ignore Put shoes on
Dressing up
Decorators Example (2)
22
Guard AI
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
Implementation
public class Task {
Boolean run()
}
public class Composite extends Task {
Composite (Vector<Task> subtasks)
}
…
24
Quite straightforward but…
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
Tick-based model
•Ti
ck-b
ased
mo
del
fro
m
htt
p:/
/jb
t.so
urc
efo
rge.
net
/
26
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
Event Handling
BT event supportis poor
28
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
Blackboard for Inter-Task Communication
30
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
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
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
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