Finite State Machine for Games

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Knowledge Representation
Advertisements

Analysis of Computer Algorithms
Decision Support and Artificial Intelligence Jack G. Zheng July 11 th 2005 MIS Chapter 4.
So far Binary numbers Logic gates Digital circuits process data using gates – Half and full adder Data storage – Electronic memory – Magnetic memory –
Peer-to-peer and agent-based computing P2P Algorithms.
Automata Theory Part 1: Introduction & NFA November 2002.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Construction process lasts until coding and testing is completed consists of design and implementation reasons for this phase –analysis model is not sufficiently.
Soar and StarCraft By Alex Turner. What is StarCraft: Brood War? A Real-Time Strategy (RTS) computer game released in A sci-fi war simulation Continually.
Modeling Computation Chapter 13.
Chapter 4 Memory Management Basic memory management Swapping
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Executional Architecture
Part IV: Memory Management
Introduction to Recursion and Recursive Algorithms
Modeling Main issues: What do we want to build How do we write this down.
Finite State Machines in Games
Programming Paradigms and languages
Tetris – Genetic Algorithm Presented by, Jeethan & Jun.
Modeling Main issues: What do we want to build How do we write this down ©2008 John Wiley & Sons Ltd. vliet.
Programming Types of Testing.
Michael Zyda Finite State Machines Michael Zyda
Finite State Machine for Games Fall 2012 Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2.
Artificial Intelligence in Game Design Intelligent Decision Making and Decision Trees.
Artificial Intelligence in Game Design Introduction to Learning.
Finite state machines.
Artificial Intelligence in Game Design Hierarchical Finite State Machines.
CS-378: Game Technology Lecture #16: AI Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica Hodgins.
10/30/2001CS 638, Fall 2001 Today AI –Overview –State Machines.
FunState – An Internal Design Representation for Codesign A model that enables representations of different types of system components. Mixture of functional.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
GATE Common AI Architectures (GATE-561) Dr.Çağatay ÜNDEĞER Instructor Middle East Technical University, GameTechnologies Bilkent University, Computer.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Raven Robin Burke GAM 376. Soccer standings Burke, 7 Ingebristen, 6 Buer, 6 Bukk, 6 Krishnaswamy, 4 Lobes, 3 Borys, 2 Rojas, 2 Bieneman, 2.
Artificial Intelligence in Game Design Problems and Goals.
Finite State Machine for Games Spring 2005 Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2.
1 The Software Development Process  Systems analysis  Systems design  Implementation  Testing  Documentation  Evaluation  Maintenance.
Motion Planning in Games Mark Overmars Utrecht University.
Artificial Intelligence for Games Finite State Machines
CS1Q Computer Systems Lecture 11 Simon Gay. Lecture 11CS1Q Computer Systems - Simon Gay 2 The D FlipFlop The RS flipflop stores one bit of information.
Intermediate 2 Computing Unit 2 - Software Development.
Dr Nick Mitchell (Room CM 224)
Finite State Machines (FSM) OR Finite State Automation (FSA) - are models of the behaviors of a system or a complex object, with a limited number of defined.
Digital System Design using VHDL
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Finite State Machines GAM 376 Robin Burke Winter 2006.
1 Game AI Finite State Machine. 2 Finite State Machine (FSM) is the Most Commonly used Game AI Technology Today Finite State Machine (FSM) is the Most.
Finite State Machines GAM 376 Robin Burke Fall 2006.
Finite State Machines Logical and Artificial Intelligence in Games Lecture 3a.
10/23/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Terrain Generation We’re pretty much done with the graphics part of the course.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
The Game Development Process: Artificial Intelligence.
Finite State Machines GAM 376 Robin Burke Winter 2008.
Finite State Machines ENGR 110 #7 2016
Bbm421 – Computer Games Technology
Decision Making: Decision Tree & State Machines Session 07
Advantages of FSM Their simplicity make it easy for inexperienced developers to implement with little to no extra knowledge (low entry level)
Main issues: • What do we want to build • How do we write this down
Artificial Intelligence and Computer Games
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Chapter 9 – Real Memory Organization and Management
Lecture 2 Introduction to Programming
Unit# 9: Computer Program Development
Interaction with artificial intelligence in games
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Chapter 5 Architectural Design.
Presentation transcript:

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

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

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

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

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 AI Module Sensing Game Engine Thinking Acting Fall 2012

AI by Polling Senses: then acts on it AI gets called at a fixed rate 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

Event Driven AI Events triggered by a message Example messages: does everything in response to events in the world 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

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

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

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

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

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

Vending Machine: state diagram

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

FSM Examples Fall 2012

Fall 2012

Fall 2012

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

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

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

Predator (Raptor) Tidle > 5 Tdining>5 Dining Prey captured (stand) Tidle > 5 Tdining>5 Hungry (wander) Dining Prey captured Prey in sight Pursuit (run) Tpursuit > 10 Fall 2012

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

FSM Design

Quake2 Examples Intuitive thinking: model the events and state changes Quake2 uses 9 different states: standing, walking, running, dodging, attacking, melee, seeing the enemy, idle and searching. Incomplete design

Quake: Rocket Fall 2012

Shambler monster Fall 2012

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

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

FSM Control System Implementation

FSM Implementation Fall 2012

Previous Example Fall 2012

Code 1 Ad hoc implementation Fall 2012

Code 1p Fall 2012

Code 2 Structure, Readable, maintainable Fall 2012

Hierarchical … Fall 2012

FSM Extensions

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

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

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

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

Processing Models Polling Event-driven 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 Fall 2012

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

An Example FSM in Football Game

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

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

FSM Secara Global dlm S.Bola

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

Object : Penjaga Gawang Tangkap ~M, K ~M, K Umpan Lambung Umpan Pendek ~M, K M, K M, K ~M,K Tendangan Gawang Mental Bertahan Dekati Bola

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

Object : Pemain Bertahan (Tengah) Umpan Lambung M,~M,K,~K Umpan Pendek Tackling : Keras M Jemput Bola Dapat Bola Marking=Zona Mental : Bertahan M

Object : Pemain Bertahan (SAYAP) Umpan Lambung Takling : Normal Menggiring Bola Maju Jemput Bola Dapat Bola Umpan Pendek Mental : Normal

Object : Gelandang Bertahan Umpan Lambung Takling : Normal Menggiring Bola Maju Tendangan Jrk Jauh Jemput Bola Dapat Bola Umpan Pendek

Object : Gelandang Serang Umpan Lambung Takling : Ringan Menggiring Bola Maju Tendangan Jrk Jauh Jemput Bola Dapat Bola Umpan Pendek

Object : Sayap Kanan / Kiri Umpan Silang Umpan Lambung Umpan Pendek Dapat Bola Jemput Bola Menggiring Bola Maju Tackling : Ringan

Object : Penyerang (Striker) Giring Bola Umpan Pendek Umpan Silang Cetak GOL Tendangan Jrk Jauh Jemput Bola Dapat Bola

References Web references: Game Programming Gems Sections 3.0 & 3.1 www.gamasutra.com/features/19970601/build_brains_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 Fall 2012