34
Principles of Computer Game Design and Implementation Lecture 24

Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Principles of Computer Game Design and Implementation › ~tsakalid › game_materials › lecture24.pdf · •Inspired by a number of techniques –Hierarchical FSMs –Scheduling

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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