finite state machines gam 376 robin burke winter 2006

45
Finite State Machines GAM 376 Robin Burke Winter 2006

Upload: blaze-griffin

Post on 18-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

AI in the Game Loop AI is updated as part of the game loop, after user input, and before rendering There are issues here: Which AI goes first? Does the AI run on every frame? Is the AI synchronized?

TRANSCRIPT

Page 1: Finite State Machines GAM 376 Robin Burke Winter 2006

Finite State Machines

GAM 376Robin BurkeWinter 2006

Page 2: Finite State Machines GAM 376 Robin Burke Winter 2006

Outline

Finite State MachinesTheoryImplementations

Homework #2 Break Lab

Page 3: Finite State Machines GAM 376 Robin Burke Winter 2006

AI in the Game Loop

AI is updated as part of the game loop, after user input, and before rendering

There are issues here:Which AI goes first? Does the AI run on every frame? Is the AI synchronized?

Page 4: Finite State Machines GAM 376 Robin Burke Winter 2006

AI Module

AI Update Step

The sensing phase determines the state of the world May be very simple - state

changes all come by message

Or complex - figure out what is visible, where your team is, etc

The thinking phase decides what to do given the world The core of AI

The acting phase tells the animation what to do Generally not interesting

Game Engine

Sensing

Thinking

Acting

Page 5: Finite State Machines GAM 376 Robin Burke Winter 2006

AI by Polling

The AI gets called at a fixed rate Senses: It looks to see what has

changed in the world. For instance:Queries what it can seeChecks to see if its animation has

finished running And then acts on it Why is this generally inefficient?

Page 6: Finite State Machines GAM 376 Robin Burke Winter 2006

Event Driven AI

Event driven AI does everything in response to events in the world Events sent by message (basically, a function gets

called when a message arrives, just like a user interface)

Example messages: A certain amount of time has passed, so update

yourself You have heard a sound Someone has entered your field of view

Note that messages can completely replace sensing, but typically do not. Why not? Real systems are a mix - something changes, so you

do some sensing

Page 7: Finite State Machines GAM 376 Robin Burke Winter 2006

Finite State Machines (FSMs) A set of states that the agent can be in Connected by transitions that are triggered

by a change in the world Normally represented as a directed graph,

with the edges labeled with the transition event

Ubiquitous in computer game AI You might have seen them in theory of

computation (or compilers)

Page 8: Finite State Machines GAM 376 Robin Burke Winter 2006

Classic Application: Regular Expressions Any regular expression can be

translated into a finite state machinethis is part of what it means to be a

regular expression Example (Perl)

DePaul course ids• [A..Z]{2,3}([1..6]\d\d)• GAM376

Page 9: Finite State Machines GAM 376 Robin Burke Winter 2006

What would the machine look like?

Page 10: Finite State Machines GAM 376 Robin Burke Winter 2006

Quake Bot Example

Types of behavior to capture: Wander randomly if don’t see or hear an enemy When see enemy, attack When hear an enemy, chase enemy When die, respawn When health is low and see an enemy, retreat

Extensions: When see power-ups during wandering, collect them

Borrowed from John Laird and Mike van Lent’s GDC tutorial

Page 11: Finite State Machines GAM 376 Robin Burke Winter 2006

Example FSM

States: E: enemy in sight S: sound audible D: dead

Events: E: see an enemy S: hear a sound D: die

Action performed: On each transition On each update in some

states (e.g. attack)

SpawnD

Wander~E,~S,~D

~E

D

AttackE,~D~E

E

E

D

~SChase

S,~E,~D

E

S

S

D

Page 12: Finite State Machines GAM 376 Robin Burke Winter 2006

Example FSM Problem

States: E: enemy in sight S: sound audible D: dead

Events: E: see an enemy S: hear a sound D: die

SpawnD

Wander~E,~S,~D

~E

D

AttackE,~D~E

E

E

D

~SChase

S,~E,~D

E

S

S

D

Problem: Can’t go directly from attack to chase. Why not?

Page 13: Finite State Machines GAM 376 Robin Burke Winter 2006

Better Example FSM

States: E: enemy in sight S: sound audible D: dead

Events: E: see an enemy S: hear a sound D: die

Extra state to recall whether or not heard a sound while attacking

SpawnD

Wander~E,~S,~D

~E

D

AttackE,~S,~D~E

E

E

D

~SChase

S,~E,~D

S

S

D

E

Attack-SE,S,~D

~E

~S

S

D

Page 14: Finite State Machines GAM 376 Robin Burke Winter 2006

Example FSM with Retreat

SpawnD

(-E,-S,-L)

Wander-E,-D,-S,-L

E

-SAttack-EE,-D,-S,-L

E

Chase-E,-D,S,-L

S

D

S

D

D

Retreat-EE,-D,-S,L

L

-E

Retreat-S-E,-D,S,L

Wander-L-E,-D,-S,L

Retreat-ESE,-D,S,L

Attack-ESE,-D,S,-L

E

E-E

-L

S-S

L

-E E

L-L

-L

-L

L

D

• States:– E: enemy in sight– S: sound audible– D: dead– L: Low health

• Worst case: Each extra state variable can add 2n extra states• n = number of

existing states

Page 15: Finite State Machines GAM 376 Robin Burke Winter 2006

Augmented FSM

Typically, there will be book-keeping to do when transitioning between states For example

• "direction of sound" variable• cleared when sound is absent and changing to "Wander"

state Most FSM implementations allow specification of

update• what to do during each time increment in this state

enter• what to do to start up this activity

exit• what to do to end this activity

Page 16: Finite State Machines GAM 376 Robin Burke Winter 2006

Example

Chase Enter

• Play animation "weapon forward"• Play sound "battle cry"• Set heading "direction of sound"• Set speed "run"

Update• Set heading "direction of sound"• Move

Exit• Play animation "hand to ear"• Play sound "Huh?"• Set speed "walk"

Page 17: Finite State Machines GAM 376 Robin Burke Winter 2006

Exercise

FSM for Blinky

Page 18: Finite State Machines GAM 376 Robin Burke Winter 2006

Hierarchical FSMs

What if there is no simple action for a state? Expand a state into its own FSM, which

explains what to do if in that state Some events move you around the same

level in the hierarchy, some move you up a level

When entering a state, have to choose a state for it’s child in the hierarchy Set a default, and always go to that Or, random choice Depends on the nature of the behavior

Page 19: Finite State Machines GAM 376 Robin Burke Winter 2006

Hierarchical FSM Example

Note: This is not a complete FSM All links between top level states

still exist Need more states for wander

StartTurn Right

Go-throughDoor

Pick-upPowerup

Wander Attack

Chase

Spawn

~E

E~S

SD

~E

Page 20: Finite State Machines GAM 376 Robin Burke Winter 2006

Non-Deterministic FSM (Markov Model)

Adds variety to actions Have multiple transitions

for the same event Label each with a

probability that it will be taken

Randomly choose a transition at run-time

Markov Model: New state only depends on the previous state

Attack

Start

Approach

Aim & Jump &Shoot

Aim & Slide Left& Shoot

Aim & Slide Right

& Shoot .3.3

.4

.3.3

.4

Page 21: Finite State Machines GAM 376 Robin Burke Winter 2006

Push-down State Machines

Suppose we have some repeated behavior common to many states don't want to "forget" what we were doing

Example change weapon if out of ammo

• might want to do this while wandering, chasing, or attacking

if we make this a state• where do we transition after doing it?

Separate global state Transition into this state temporarily

• and pop back to origin

Page 22: Finite State Machines GAM 376 Robin Burke Winter 2006

Efficient Implementation

Compile into an array of state-name, event

state-namei+1 := array[state-namei, event]

Switch on state-name to call execution logic

Hierarchical Create array for every FSM Have stack of states

• Classify events according to stack• Update state which is sensitive to

current event Markov: Have array of possible

transitions for every (state-name,event) pair, and choose one at random

event

state

Page 23: Finite State Machines GAM 376 Robin Burke Winter 2006

OO Implementation

States as objects Actions as methods State machine can be very generic More in a minute

Page 24: Finite State Machines GAM 376 Robin Burke Winter 2006

FSM Advantages

Very fast – one array access think GBA

Expressive enough for simple behaviors or characters that are intended to be “dumb”

Can be compiled into compact data structure Dynamic memory: current state Static memory: state diagram – array implementation

Can create tools so non-programmer can build behavior

Non-deterministic FSM can make behavior unpredictable

Page 25: Finite State Machines GAM 376 Robin Burke Winter 2006

FSM Disadvantages

Number of states can grow very fast Exponentially with number of events:

s=2e

Number of arcs can grow even faster: a=s2

Limited representational power Propositional representation

Page 26: Finite State Machines GAM 376 Robin Burke Winter 2006

Varieties of representation

Propositional logic: Statements about specific objects in the world – no

variables Jim is in room7, Jim has the rocket launcher, the

rocket launcher does splash damage Go to room8 if you are in room7 through door14

Predicate Logic: Allows general statement – using variables All rooms have doors All splash damage weapons can be used around

corners All rocket launchers do splash damage Go to a room connected to the current room

Page 27: Finite State Machines GAM 376 Robin Burke Winter 2006

Representation in FSMs

Can only handle propositions each transition predicated on a proposition

being true or false Difficult to add comparative tests

“pick up the better powerup” “attack the closest enemy”

Expensive to count Wait until the third time I see enemy, then

attack• Need extra events: First time seen, second time

seen, and extra states to take care of counting

Page 28: Finite State Machines GAM 376 Robin Burke Winter 2006

Implementations

switch statement we can implement a FSM with a switch statement

Each state is a branchswitch

case CHASE:if ENEMY state = ATTACKelse if ~SOUND state = WANDERelse DoChase()

case WANDER:etc.

Concise but a maintenance nightmare

Page 29: Finite State Machines GAM 376 Robin Burke Winter 2006

Transition table

For each state and eventrecord next

State Event NewState UpdateFn

Chase Enemy Attack DoChase()

Chase ~Sound Wander DoChase()

Page 30: Finite State Machines GAM 376 Robin Burke Winter 2006

Buckland

Uses the "State" design pattern all information relative to an object's state is

encapsulated changing state is a matter of changing this

object Calling sequence

Game -> Object "update yourself" Object -> State "update me" many advantages

• state objects can be shared without duplicating code

• easy to store "previous state" information

Page 31: Finite State Machines GAM 376 Robin Burke Winter 2006

Overall design

Game-wide state machine When each object is updated,

the state is loaded into the machine and processed

new state is recorded with object Assumes that everything in the game

uses a state machine

Page 32: Finite State Machines GAM 376 Robin Burke Winter 2006

Structure view

Page 33: Finite State Machines GAM 376 Robin Burke Winter 2006

Singleton pattern

All states are unique instances only a single copy of the "Quench Thirst" state

Benefit no dynamic allocation and destruction of state objects

Drawback no object-specific information can be stored in the

state Technique

use static allocation use a static member function that always returns this

instance make constructor private

Page 34: Finite State Machines GAM 376 Robin Burke Winter 2006

Example

class QuenchThirst : public State<Miner>{private: QuenchThirst(){} QuenchThirst(const QuenchThirst&); QuenchThirst& operator=(const QuenchThirst&);public: static QuenchThirst* Instance(); virtual void Enter(Miner* miner); virtual void Execute(Miner* miner); virtual void Exit(Miner* miner);};

QuenchThirst* QuenchThirst::Instance(){ static QuenchThirst instance; return &instance;}

Page 35: Finite State Machines GAM 376 Robin Burke Winter 2006

Improved design

Each object may have slightly different requirements for how its state should be handledsome may not need a state machinesome may have something more

complex• hierarchy, Markov, etc.

Give each object its own state machine

Page 36: Finite State Machines GAM 376 Robin Burke Winter 2006

Nextversion

Page 37: Finite State Machines GAM 376 Robin Burke Winter 2006

Note

That this state machine has a global state and previous statevery limited memoryto handle state "blips"

Page 38: Finite State Machines GAM 376 Robin Burke Winter 2006

Messaging

Perception by polling is inefficient never want to be "busy waiting"

Example if guard does nothing until player enters guard should not be constantly checking "did player

enter"• in order to transition to next state

Better method publish / subscribe have player send a message to each item in a room

when he enters state transition can be predicated on this message

Page 39: Finite State Machines GAM 376 Robin Burke Winter 2006
Page 40: Finite State Machines GAM 376 Robin Burke Winter 2006

Note

Messages sent to specific recipientsnot hard to imagine an extension for

localized messagesglobal messages very expensive

Modern games use messaging extensively

Need for unique entity ids and entity registry

Page 41: Finite State Machines GAM 376 Robin Burke Winter 2006

Homework #2

We are not using WestWorldtoo weird

InsteadAIsteroidsan asteroids gameyou will be working with the AI

controlling the ship

Page 42: Finite State Machines GAM 376 Robin Burke Winter 2006

Part I

Add a new statewhen there are few asteroidsgo into powering-up statetry to avoid asteroids and just get lots

of power-upssort of a specialized idle mode

Page 43: Finite State Machines GAM 376 Robin Burke Winter 2006

Part II

Add toroidal geometrythe AI does not measure distances

rightoff-screen wrap-around not used

Change the code to do this right

Page 44: Finite State Machines GAM 376 Robin Burke Winter 2006

Major hint

screen

wrap regionpretend asteroid is at position (x, y-screenH)

Page 45: Finite State Machines GAM 376 Robin Burke Winter 2006

Break