finite state machine for games mohammad zikky, m.t ref: chenney, cs679 lectures ai game programming...

57
Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Upload: heather-presser

Post on 14-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Finite State Machine for Games

Mohammad Zikky, M.T

Ref: Chenney, CS679 lecturesAI Game Programming Wisdom 2

Page 2: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

2

Outline

AI and GameIntroduction/examplesDesign Intuition State-based

ImplementationExtending Stack-based Fuzzy-state machine

Page 3: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

3

What is AI? (and is NOT)

AI is the control of non-human entities in a game

The other cars in a car game The opponents and monsters in a shooter Your units, your enemy’s units and your enemy in a

RTS game

But, typically does not refer to passive things that just react to the player and never initiate action

That’s physics or game logic For example, the blocks in Tetris are not AI, nor is a

flag blowing in the wind

Page 4: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

4

AI in the Game Loop

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

Page 5: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 5

AI Module

AI Update Step

The sensing phase determines the state of the world

May be very simple - state changes all come by messaging

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

Game Engine

Sensing

Thinking

Acting

Page 6: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

6

AI by Polling

Senses: looks to see what has been changed in the

world

then acts on it

Generally inefficient and complicated Different characters might require different

polling rate

AI gets called at a fixed rate

Page 7: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Event Driven AI

• Events triggered by a message • basically, a function gets called when

a message arrives, just like a user interface)

• Example messages: You have heard a sound Someone has entered your field of

view A certain amount of time has passed,

so update yourself

Fall 2012 7

does everything in response to events in the world

Page 8: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

8

AI Techniques in Games

Basic problem: Given the state of the world, what should I do?A wide range of solutions in games: Finite state machines, Decision trees, Rule

based systems, Neural networks, Fuzzy logic

A wider range of solutions in the academic world: Complex planning systems, logic

programming, genetic algorithms, Bayes-nets Typically, too slow for games

Page 9: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 9

Expressiveness

What behaviors can easily be defined, or defined at all?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 door14Predicate 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 10: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 10

Finite State Machines (FSMs)

A set of states that the agent can be inConnected by transitions that are triggered by a change in the worldNormally represented as a directed graph, with the edges labeled with the transition eventUbiquitous in computer game AI

Page 11: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 11

Formal Definitions (N. Philips)

"An abstract machine consisting of a set of states (including the initial state), a set of input events, a set of output events, and a state transition function. The function takes the current state and an input event and returns the new set of output events and the next state. Some states may be designated as "terminal states".The state machine can also be viewed as a function which maps an ordered sequence of input events into a corresponding sequence of (sets of) output events.Finite State Automaton: the machine with no output

Page 12: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

12

FSM with Output: vending machines

OJ & AJ for 30 centsState table

Page 13: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

13

Vending Machine: state diagram

Page 14: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

14

FSM and Game

Game character behavior can be modeled (in most cases) as a sequence of different “mental state”, where change is driven by the actions of player/other characters, …Natural choice for defining AI in games

Page 15: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 15

FSM Examples

Page 16: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 16

Page 17: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 17

Page 18: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

18

Page 19: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

PacMan State Machine

19

PacMan eats a Power Pill

Timer <= 0

Collision with PacMan

Timer <= 0

Collision with GhostBox

Page 20: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Actions of States

20

Roam;If PacMan gets close, PathTo (PacMan)

Timer--;PathAwayFrom (PacMan)

PathTo (GhostBox)Timer—Move back-and-forth

Page 21: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

21

Ex: predator vs. prey

Prey (laalaa)

Idle(stand,wave,…)

Flee(run)

Sees predator

No more threat

capturedDead

Page 22: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 22

Predator (Raptor)

Idle(stand)

Hungry(wander)

Pursuit(run)

Tidle > 5

Prey in sight

Tpursuit > 10

Dining

Prey captured

Tdining>5

Page 23: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

23

Idling LaaLaa

Stand

Wave

Wander(set random target)

50%

30%20%

Target arrived

Twave>2

RTstand>4

This page illustrates:hierarchical state,Non-deterministic state transition

This page illustrates:hierarchical state,Non-deterministic state transition

Page 24: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

FSM Design

Page 25: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

25

Quake2 ExamplesQuake2 uses 9 different states:

standing, walking, running, dodging, attacking, melee, seeing the enemy, idle and searching.

Incomplete design

Intuitive thinking: model the events and state changes

Intuitive thinking: model the events and state changes

Page 26: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 26

Quake: Rocket

Page 27: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 27

Shambler monster

Page 28: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 28

FSM Advantages

Very fast – one array accessExpressive 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 behaviorNon-deterministic FSM can make behavior unpredictable

Page 29: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 29

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

Propositional representation Difficult to put in “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 30: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

FSM Control System Implementation

Page 31: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 31

FSM Implementation

Page 32: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 32

Previous Example

Page 33: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 33

Code 1 Ad hoc implementation

Page 34: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 34

Code 1p

Page 35: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 35

Code 2Structure, Readable, maintainable

Page 36: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 36

Hierarchical …

Page 37: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

FSM Extensions

Page 38: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Advanced Issues

Hierarchical FSMNon-deterministic FSMSwapping FSM

Fall 2012 38

Page 39: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 39

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 stateSome events move you around the same level in the hierarchy, some move you up a levelWhen entering a state, have to choose a state for its child in the hierarchy

Set a default, and always go to that Or, random choice Depends on the nature of the behavior

Page 40: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 40

Stack-based FSM

History stack: Remember previous state; create characters with a memory …

Pushdown automaton(PDA)

Page 41: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 41

Goal-based vs. State-based

There is also a slight derivative to the state-based engine, but it used in more complicated games like flight simulators and games like MechWarrior. They use goal -based engines - each entity within the game is assigned a certain goal, be it 'protect base', 'attack bridge', 'fly in circles'. As the game world changes, so do the goals of the various entities.

Page 42: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 42

Processing Models

Polling FSM update

frequency Easy to implement

and debug Inefficiency (Little

Red example)

Event-driven Publish-subscribe

messaging system Game engine sends

event messages to individual FSMs

An FSM subscribes only to the events that have the potential to change the current state

Higher efficiency, non-trivial implementation

Page 43: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 43

Efficiency and Optimization

In AI, FSM is the most efficient technology availableYet, there is always room for improvement Level of Detail: depending on the

condition (e.g., distance with player), use different FSM, or different update frequency

Page 44: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

An Example FSM in Football Game

Page 45: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Class : Sepakbola

LapanganJadwal Pertandingan (Liga, Tournament, 2x45 mnt)Skuad Pemain (11 di lapangan + cadangan)Taktik & Strategi (4-4-2 Menyerang / Bertahan)LatihanStaff Pemain (Pelatih , Ass Pelatih, Dokter)Keuangan Transfer Pemain

Page 46: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Pemain Bola

TechnicalsIntellegentsPhysicalsTemperaments

-----------------------------------------------------------------

Cetak Gol, Giring Bola, Tangkap Bola (penjaga Gawang), Control Bola, Tackling Bola, Tendang Jarak Jauh, Marking, dll

Page 47: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

FSM Secara Global dlm S.Bola

Page 48: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Class : Sepakbola

Page 49: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Penjaga Gawang

Tangkap

Umpan

Pendek

Umpan

Lambung

Tendangan Gawa

ng

Mental

Bertahan

Dekati Bola

~M, K~M, K

~M, K

M, K M, K~M,K

Page 50: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Penjaga Gawang

M = Musuh Mendekat~M = Musuh MenjauhK = Teman Mendekat~K = Teman Menjauh

Page 51: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Pemain Bertahan (Tengah)

Umpan

Lambung

Tackling : Kera

s

Umpan

Pendek

Marking=Zona

Mental :

Bertahan

Jemput

Bola

Dapat

Bola

M

M

M,~M,K,~K

Page 52: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Pemain Bertahan (SAYAP)

Umpan

LambungTakli

ng : Norm

al

Menggiring Bola

Maju

Umpan

Pendek

Mental :

Normal

Jemput

Bola

Dapat

Bola

Page 53: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Gelandang Bertahan

Umpan

Lambung

Takling :

Normal Menggiri

ng Bola Maju

Umpan

Pendek

Jemput

Bola

Dapat

Bola

Tendangan Jrk

Jauh

Page 54: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Gelandang SerangUmpan

Lambung

Takling : Ringan Menggiri

ng Bola Maju

Umpan

Pendek

Dapat

Bola

Tendangan Jrk

Jauh

Jemput

Bola

Page 55: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Sayap Kanan / KiriUmpan

Silang

Tackling : Ringan

Umpan

Pendek

Jemput

Bola

Dapat

Bola

Umpan

Lambung

Menggiring Bola

Maju

Page 56: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Object : Penyerang (Striker)

Jemput

BolaDapa

t Bola

Umpan

Silang

Umpan

Pendek

Tendangan Jrk

Jauh

Cetak GOL

Giring

Bola

Page 57: Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2

Fall 2012 57

References

Web references: www.gamasutra.com/features/19970601/build_brain

s_into_games.htm csr.uvic.ca/~mmania/machines/intro.htm www.erlang/se/documentation/doc-4.7.3/doc/design_

principles/fsm.html www.microconsultants.com/tips/fsm/fsmartcl.htm http://www.angelfire.com/dragon/letstry/tutorials/dfa/

Game Programming Gems Sections 3.0 & 3.1

It’s very very detailed, but also some cute programming