Finite State Machines in Games

Slides:



Advertisements
Similar presentations
NP-Completeness.
Advertisements

Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Michael Zyda Finite State Machines Michael Zyda
Artificial Intelligence in Game Design Representing NPCs as Finite State Machines.
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.
Artificial Intelligence in Game Design Hierarchical Finite State Machines.
Planning under Uncertainty
CPSC Compiler Tutorial 8 Code Generator (unoptimized)
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.
Snick  snack A Working Computer Slides based on work by Bob Woodham and others.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
10/30/2001CS 638, Fall 2001 Today AI –Overview –State Machines.
Artificial Intelligence in Game Design Probabilistic Finite State Machines.
Artificial Intelligence in Game Design Representing NPCs as Finite State Machines.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Guide To UNIX Using Linux Third Edition
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Reminder Next class (Tuesday), you should have formed groups and select a topic for presentation among: (1)Real-Time Strategy games (Luis Villegas, Dulmovits,
What is Concurrent Programming? Maram Bani Younes.
1 Chapter-01 Introduction to Computers and C++ Programming.
Game Scripting By: Nicholas Haines. Aurora Neverwinter Toolset.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
1 I-Logix Professional Services Specialist Rhapsody IDF (Interrupt Driven Framework) CPU External Code RTOS OXF Framework Rhapsody Generated.
Finite State Machine for Games Spring 2005 Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Memory Management Page replacement algorithms, segmentation Tanenbaum, ch. 3 p Silberschatz, ch. 8, 9 p
1 A Simple but Realistic Assembly Language for a Course in Computer Organization Eric Larson Moon Ok Kim Seattle University October 25, 2008.
Artificial Intelligence in Game Design
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
Game Scripting by: Nicholas Haines. What is Scripting? Interpreted Language Interpreted Language –As the game runs.
1 Game AI Finite State Machine. Finite State Machine (FSM) is the most commonly used Game AI technology Finite State Machine (FSM) is the most commonly.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 7 OS System Structure.
1 Scheduling The part of the OS that makes the choice of which process to run next is called the scheduler and the algorithm it uses is called the scheduling.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Motion Planning in Games Mark Overmars Utrecht University.
Artificial Intelligence in Game Design N-Grams and Decision Tree Learning.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Jumping, Climbing, and Tactical Reasoning Section 2.5 Tom Schaible CSE 497 – AI & Game Programming.
Artificial Intelligence for Games Finite State Machines
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
6.5 Implementing a State Machine Language. State Machine in game AI The most used software pattern Simple to program Easy to comprehend Easy to debug.
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.
Introduction to OOP CPS235: Introduction.
Finite State Machines GAM 376 Robin Burke Winter 2006.
Artificial Intelligence in Game Design Lecture 20: Hill Climbing and N-Grams.
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.
Hello world !!! ASCII representation of hello.c.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
The Game Development Process: Artificial Intelligence.
Finite State Machines GAM 376 Robin Burke Winter 2008.
Visit for more Learning Resources
Advantages of FSM Their simplicity make it easy for inexperienced developers to implement with little to no extra knowledge (low entry level)
Artificial Intelligence and Computer Games
CO Games Development 2 Week 19 Extensions to Finite State Machines
Code Generation.
Finite State Machines in Games
CIS 488/588 Bruce R. Maxim UM-Dearborn
What is Concurrent Programming?
Introduction to Data Structure
Programming Logic and Design Eighth Edition
Running & Testing Programs :: Translators
Presentation transcript:

Finite State Machines in Games Slides by: Jarret Raim

FSM’s in Theory Simple theoretical construct Set of states (S) Input vocabulary (I) Transitional function T(s,i) A way of denoting how an object can change its state over time.

FSM’s in Practice Each state represents some desired behavior. The transition function T resides across all states. Each state “knows” how to transition to other states. Accepting states (those that require more input) are considered to be the end of execution for an FSM. Input to the FSM continues as long as the game continues.

FSM’s in Games Character AI can be modeled as a sequence of mental states. World events can force a change in state. The mental model is easy to grasp, even for non-programmers. Monster In Sight Gather Treasure Flee No Monster Fight Monster Dead Cornered

FSM Example States Events Action performed E: enemy in sight S: hear a sound D: dead Events E: see an enemy D: die Action performed On each transition On each update in some states (e.g. attack) Attack E,~D ~E S Patrol E ~S D D E Inspect E ~E Spawn D D Problem: Can’t go directly from attack to patrol. We’ll fix this later.

FSM Implementation - Code Simplest method After an action, the state might change. Requires a recompile for changes No pluggable AI Not accessible to non-programmers No set structure Can be a bottleneck. void RunLogic( int *state ) { switch( *state ) { case 0: //Wander Wander(); if( SeeEnemy() ) *state = 1; if( Dead() ) *state = 2; break; case 1: //Attack Attack(); *state = 0; case 3: //Dead SlowlyRot() }

FSM Implementation - Macro Forces structure Shallow learning curve More readable Removes clutter by using macros. Easily debugged Allows focus on important code. bool MyStateMachine::States( StateMachineEvent event, int state ); { BeginStateMachine State(0) OnUpdate Wander(); if( SeeEnemy() ) SetState(1); if( Dead() ) SetState(2); State(1) Attack(); SetState(0); State(2) RotSlowly(); EndStateMachine }

FSM Implementation – Data Driven Developer creates scripting language to control AI. Script is translated to C++ or bytecode. Requires a vocabulary for interacting with the game engine. A ‘glue layer’ must connect scripting vocabulary to game engine internals. Allows pluggable AI modules, even after the game has been released.

FSM Processing Polling Event Driven Model Multithreaded Simple and easy to debug. Inefficient since FSM’s are always evaluated. Event Driven Model FSM registers which events it is interested in. Requires complex Observer model in engine. Hard to balance granularity of event model. Multithreaded Each FSM assigned its own thread. Requires thread-safe communication. Conceptually elegant. Difficult to debug. Can be made more efficient using microthreads.

Game Engine Interfacing Simple hard coded approach Allows arbitrary parameterization Requires full recompile Function pointers Pointers are stored in a singleton or global Implementation in DLL Allows for pluggable AI. Data Driven An interface must provide glue from engine to script engine. Engine AI Engine AI DLL Engine S. Interface AI Compiler Byte Code

Optimization – Time Management Helps manage time spent in processing FSM’s. Scheduled Processing Assigns a priority that decides how often that particular FSM is evaluated. Results in uneven (unpredictable) CPU usage by the AI subsystem. Can be mitigated using a load balancing algorithm. Time Bounded Places a hard time bound on CPU usage. More complex: interruptible FSM’s

Optimization – Level of Detail It’s ok to cut corners if the user won’t notice. Each level of detail will require programmer time. The selection of which level to execute can be difficult. Many decisions cannot be approximated.

FSM Extensions Extending States Stack Based FSM’s Adding onEnter() and onExit() states can help handle state changes gracefully. Stack Based FSM’s Allows an AI to switch states, then return to a previous state. Gives the AI ‘memory’ More realistic behavior Subtype: Hierarchical FSM’s

FSM Example Original version doesn’t remember what the previous state was. One solution is to add another state to remember if you heard a sound before attacking. E Attack-P E,S,~D ~E ~S S D Attack E,~D ~E E D E Inspect ~E D ~S Patrol S D Spawn D S

FSM Example Worst case: Spawn D (-E,-S,-L) Wander -E,-D,-S,-L E -S Attack-E E,-D,-S,-L Chase -E,-D,S,-L S Retreat-E E,-D,-S,L L -E Retreat-S -E,-D,S,L Wander-L -E,-D,-S,L Retreat-ES E,-D,S,L Attack-ES E,-D,S,-L -L Worst case: Each extra state variable can add 2n extra states n = number of existing states Using a stack would allow much of this behavior without the extra states.

Stack FSM – Thief 3 Stack allows AI to move back and forth between states. Leads to more realistic behavior without increasing FSM complexity.

Hierarchical FSMs Expand a state into its own sub-FSM 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 Random choice Depends on the nature of the behavior

Hierarchical FSM Example Attack Wander ~E Chase Pick-up Powerup E ~S S Spawn Start Turn Right D ~E Note: This is not a complete FSM All links between top level states still exist Need more states for wander Go-through Door

Non-Deterministic Hierarchical 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 Approach .3 .4 Aim & Slide Right & Shoot Aim & Slide Left & Shoot Start Aim & Jump & Shoot If two arcs are two, pick according to probability

More FSM Extensions Fuzzy State Machines Multiple FSM’s Degrees of truth allow multiple FSM’s to contribute to character actions. Multiple FSM’s High level FSM coordinates several smaller FSM’s. Polymorphic FSM’s Allows common behavior to be shared. Soldier -> German -> Machine Gunner

Polymorphic FSM Example Soldier Rifleman Machine Gunner Officer British Soviet American German American German American German British Soviet British Soviet

Debugging FSM’s Offline Debugging Online Debugging Logging Verbosity Levels Online Debugging Graphical representation is modified based on AI state Command line to modify AI behavior on the fly.

Case Study: Robocode First determine what states are needed Attack, Evade, Search, etc. Code up the FSM transition function. Include an error state that is noticeable. Setup debugging. Verbosity levels are a must.

Case Study: Robocode Defend Search Implement and test each state separately. A test bot AI might help test single behaviors. (see Target bot) Attack

Defense and Firing Power Enable your bot to dodge incoming fire. Every 20 ‘ticks’ reverse direction. Adds a circle strafe. Selects a bullet power based on our distance away from the target void doMovement() { if (getTime()%20 == 0) direction *= -1; setAhead(direction*300); } setTurnRightRadians( target.bearing + (PI/2)); void doFirePower() { firePower = 400/target.distance; }

Searching Reducing the scanner arc allows you to fire faster. If a target hasn’t been seen recently, spin. Scan where the target is. ‘Wobble’ to make sure to find the target. void doScanner() { double radarOffset; if(getTime() - target.ctime > 4) radarOffset = 360; else radarOffset = getRadarHeadingRadians() - absbearing(getX(),getY(),target.x,target.y); if( radarOffset < 0 ) radarOffset -= PI/8; radarOffset += PI/8; setTurnRadarLeftRadians(NormaliseBearing(radarOffset)); }

References AI Game Programming Wisdom University of Wisconsin presentation Robocode Website