Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Finite State Machine for Games Mohammad Zikky, M.T Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2."— Presentation transcript:

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

2 2 Outline AI and Game Introduction/examples Design Intuition State-based Implementation Extending Stack-based Fuzzy-state machine

3 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

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

5 Fall 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

6 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

7 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 does everything in response to events in the world

8 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

9 Fall 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 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

10 Fall 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

11 Fall 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

12 12 FSM with Output: vending machines OJ & AJ for 30 cents State table

13 13 Vending Machine: state diagram

14 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

15 Fall FSM Examples

16 Fall

17 Fall

18 18

19 PacMan State Machine 19 PacMan eats a Power Pill Timer <= 0 Collision with PacMan Timer <= 0 Collision with GhostBox

20 Actions of States 20 Roam; If PacMan gets close, PathTo (PacMan) Timer--; PathAwayFrom (PacMan) PathTo (GhostBox) Timer— Move back-and-forth

21 21 Ex: predator vs. prey Prey (laalaa) Idle (stand,wave, … ) Flee (run) Sees predator No more threat captured Dead

22 Fall Predator (Raptor) Idle (stand) Hungry (wander) Pursuit (run) T idle > 5 Prey in sight T pursuit > 10 Dining Prey captured T dining >5

23 23 Idling LaaLaa Stand Wave Wander (set random target) 50% 30% 20% Target arrived T wave >2 R T stand >4 This page illustrates: hierarchical state, Non-deterministic state transition This page illustrates: hierarchical state, Non-deterministic state transition

24 FSM Design

25 25 Quake2 Examples Quake2 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

26 Fall Quake: Rocket

27 Fall Shambler monster

28 Fall FSM Advantages Very fast – one array access 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

29 Fall FSM Disadvantages Number of states can grow very fast Exponentially with number of events: s=2 e Number of arcs can grow even faster: a=s 2 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

30 FSM Control System Implementation

31 Fall FSM Implementation

32 Fall Previous Example

33 Fall Code 1 Ad hoc implementation

34 Fall Code 1p

35 Fall Code 2 Structure, Readable, maintainable

36 Fall Hierarchical …

37 FSM Extensions

38 Advanced Issues Hierarchical FSM Non-deterministic FSM Swapping FSM Fall

39 Fall 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 its child in the hierarchy Set a default, and always go to that Or, random choice Depends on the nature of the behavior

40 Fall Stack-based FSM History stack: Remember previous state; create characters with a memory … Pushdown automaton (PDA)

41 Fall 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.

42 Fall 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

43 Fall Efficiency and Optimization In AI, FSM is the most efficient technology available Yet, 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

44 An Example FSM in Football Game

45 Class : Sepakbola Lapangan Jadwal Pertandingan (Liga, Tournament, 2x45 mnt) Skuad Pemain (11 di lapangan + cadangan) Taktik & Strategi (4-4-2 Menyerang / Bertahan) Latihan Staff Pemain (Pelatih, Ass Pelatih, Dokter) Keuangan Transfer Pemain

46 Object : Pemain Bola Technicals Intellegents Physicals Temperaments Cetak Gol, Giring Bola, Tangkap Bola (penjaga Gawang), Control Bola, Tackling Bola, Tendang Jarak Jauh, Marking, dll

47 FSM Secara Global dlm S.Bola

48 Class : Sepakbola Keterangan  CG : Cetak Gol  TB : Tangkap Bola (Penjaga Gawang)  US : Umpan Silang  UP : Umpan Pendek  UL : Umpan Lambung  TJ : Tendangan Jarak Jauh  TC : Tendangan Pojok  TP : Tendangan Pinalti  T : Tackling / Jegal  LMB : Lari Kejar Bola  LB : Lempar Bola  MM : Menjaga Lawan (Man Marking)  MZ : Menjaga Daerah (Zona Marking)  SB : Sundul Bola  GB : Giring Bola  LMD : Lari Maju ke depan  LMBC : Lari Mundur ke Belakang  AM : Mental Menyerang  DM : Mental Bertahan

49 Object : Penjaga Gawang Tangk ap Umpa n Pende k Umpa n Lambu ng Tendan gan Gawan g Mental Bertah an Dekati Bola ~M, K M, K ~M,K

50 Object : Penjaga Gawang M = Musuh Mendekat ~M = Musuh Menjauh K = Teman Mendekat ~K = Teman Menjauh

51 Object : Pemain Bertahan (Tengah) Umpa n Lamb ung Tackli ng : Keras Umpa n Pende k Marki ng=Z ona Menta l : Berta han Jemp ut Bola Dapat Bola M M M,~M,K, ~K

52 Object : Pemain Bertahan (SAYAP) Umpa n Lamb ung Taklin g : Norm al Menggirin g Bola Maju Umpa n Pende k Menta l : Norm al Jemp ut Bola Dapat Bola

53 Object : Gelandang Bertahan Umpa n Lamb ung Taklin g : Norm al Menggirin g Bola Maju Umpa n Pende k Jemp ut Bola Dapat Bola Tendan gan Jrk Jauh

54 Object : Gelandang Serang Umpa n Lamb ung Taklin g : Ringa n Menggirin g Bola Maju Umpa n Pende k Dapat Bola Tendan gan Jrk Jauh Jemp ut Bola

55 Object : Sayap Kanan / Kiri Umpa n Silang Tackli ng : Ringa n Umpa n Pende k Jemp ut Bola Dapat Bola Umpa n Lamb ung Menggirin g Bola Maju

56 Object : Penyerang (Striker) Jemp ut Bola Dapat Bola Umpa n Silang Umpa n Pende k Tendan gan Jrk Jauh Ceta k GOL Giring Bola

57 Fall References Web references: games.htm games.htm csr.uvic.ca/~mmania/machines/intro.htm /doc/design_principles/fsm.html /doc/design_principles/fsm.html Game Programming Gems Sections 3.0 & 3.1 It ’ s very very detailed, but also some cute programming


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

Similar presentations


Ads by Google