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.

Slides:



Advertisements
Similar presentations
State Machines An approach to assembler coding. Intro State Machines are an integral part of software programming. State machines make code more efficient,
Advertisements

ICT2191 Topic 4 State Machines Why State Machines? What is a State Machine? Finite State Automata Finite State Machines Using FSMs Use in Game AI Advantages.
Finite State Machines in Games
SM2220 – Class 10 Finite Automata. SM2220 – Class 10 Finite Automata Computation theory Formal language.
Artificial Intelligence in Game Design Representing NPCs as Finite State Machines.
Artificial Intelligence in Game Design Intelligent Decision Making and Decision Trees.
Automated Planning & Computer Games: Perspectives and Applications Hector Munoz-Avila.
Artificial Intelligence in Game Design Hierarchical Finite State Machines.
Scheduling for Embedded Real-Time Systems Amit Mahajan and Haibo.
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Advanced Behavioral Modeling
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
1 CS101 Introduction to Computing Lecture 19 Programming Languages.
02/06/05 “Investigating a Finite–State Machine Notation for Discrete–Event Systems” Nikolay Stoimenov.
PROGRAMMING LANGUAGES The Study of Programming Languages.
CSE 381 – Advanced Game Programming 3D Game Architecture.
Ch.2 Part A: Requirements, State Charts EECE **** Embedded System Design.
- 1 - Embedded Systems—State charts Specifications.
Chapter 11-Multimedia Authoring Tools. Overview Introduction to multimedia authoring tools. Types of authoring tools. Cross-platform authoring notes.
1 COMP541 State Machines Montek Singh Feb 8, 2012.
Finite State Machine for Games Spring 2005 Ref: Chenney, CS679 lectures AI Game Programming Wisdom 2.
Institute of Computer and Communication Network Engineering OFC/NFOEC, 6-10 March 2011, Los Angeles, CA Lessons Learned From Implementing a Path Computation.
Survey of AI for games. AI vs. AI for games Traditional AI: – Made to handle unseen inputs, large state space – Too many options possible to compute an.
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 10Slide 1 Architectural Design l Establishing the overall structure of a software system.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
CHAPTER TEN AUTHORING.
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.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
INTRODUCTION TO COMPUTING CHAPTER NO. 04. Programming Languages Program Algorithms and Pseudo Code Properties and Advantages of Algorithms Flowchart (Symbols.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Framework for MDO Studies Amitay Isaacs Center for Aerospace System Design and Engineering IIT Bombay.
Application of AI techniques for Computer Games BSc Computer Games Programming, 2006 Julien Delezenne GAMES ARTIFICIAL INTELLIGENCE.
Games Development Game Architecture: Entities CO2301 Games Development 1 Week 22.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Artificial Intelligence for Games Finite State Machines
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.
1 Game System. 2 Control system Control system User input User input Mouse or keyboard Mouse or keyboard Keyboard layout Keyboard layout Camera control.
Introduction to Interactive Media Interactive Media Tools: Authoring Applications.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
SM2220 – Class 06 Finite Automata. SM2220 – Class 06 Topic in theoretical computing. A subset of computation machines. Closely related to formal language.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
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
Chapter Three The Surgical Team. The Problem Large Group – 10:1 productivity and 5:1 program speed and space management. – Negative aspect Sheer number.
Source Level Debugging of Parallel Programs Roland Wismüller LRR-TUM, TU München Germany.
1 COMP541 Finite State Machines - 1 Montek Singh Sep 22, 2014.
Andrey Karaulov, Alexander Strabykin Institute for System Programming Russian Academy of Sciences SYRCoSE: Spring Young Researchers Colloquium on Software.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Finite State Machines Logical and Artificial Intelligence in Games Lecture 3a.
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
Two New UML Diagram Types Component Diagram Deployment Diagram.
1 COMP541 Sequential Logic – 2: Finite State Machines Montek Singh Feb 29, 2016.
The Game Development Process: Artificial Intelligence.
Collision Theory and Logic
Advanced Computer Systems
Done By: Ashlee Lizarraga Ricky Usher Jacinto Roches Eli Gomez
Game Architecture Rabin is a good overview of everything to do with Games A lot of these slides come from the 1st edition CS 4455.
Collision Theory and Logic
Enemy and Friendly AIs Richard Gesick.
Finite State Machines Computer theory covers several types of abstract machines, including Finite State Machines.
CO Games Development 2 Week 19 Extensions to Finite State Machines
Finite State Machines in Games
Programming Languages 2nd edition Tucker and Noonan
21 Threads.
Games Development Game Architecture: Entities
Presentation transcript:

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 Commonly used Game AI Technology Today –Simple –Efficient –Easily extensible –Powerful enough to handle a wide variety of situations Theory (Simplified) Theory (Simplified) –A set states, S –An input vocabulary, I –Transition function, T(s, i) »Map a state and an input to another state Introduction (1/2)

3 Practical Use Practical Use –State »Behavior –Transition »Across states »Conditions –It’s all about driving behavior Flow-chart Diagram Flow-chart Diagram –UML State Chart »Arrow Transition Transition »Rectangle State State Introduction (2/2)

4 An Example of FSM As a Diagram Gather Treasure Flee Fight Monster in sight No monster Monster dead Cornered

5 Character AI Character AI “Decision-Action” Model “Decision-Action” Model Behavior Behavior –Mental State Transition Transition –Players’ action –The other characters’ actions –Some features in the game world FSM for Games

6 Code-based FSM Code-based FSM –Simple Code One Up »Straightforward »Most common –Macro-assisted FSM Language Data-Driven FSM Data-Driven FSM –FSM Script Language Implement FSM

7 Coding an FSM – Code Example 1 void RunLogic(int *state) { switch(*state) switch(*state) { case 0: // Wander case 0: // Wander Wander(); Wander(); if (SeeEnemy()) *state = 1; if (SeeEnemy()) *state = 1; if (Dead()) *state = 2; if (Dead()) *state = 2; break; break; case 1: // Attack case 1: // Attack Attack(); Attack(); *state = 0; *state = 0; if (Dead()) *state = 2; if (Dead()) *state = 2; break; break; case 2: // Dead case 2: // Dead SlowlyRot(); SlowlyRot(); break; break; }}

8 Coding an FSM – Code Example 2 void RunLogic(FSM *fsm) { // Do action based on the state and determine next input // Do action based on the state and determine next input input = 0; input = 0; switch(fsm->GetStateID()) switch(fsm->GetStateID()) { case 0: // Wander case 0: // Wander Wander(); Wander(); if (SeeEnemy()) input = SEE_ENEMY; if (SeeEnemy()) input = SEE_ENEMY; if (Dead()) input = DEAD; if (Dead()) input = DEAD; break; break; case 1: // Attack case 1: // Attack Attack(); Attack(); input = WANDER; input = WANDER; if (Dead()) input = DEAD; if (Dead()) input = DEAD; break; break; case 2: // Dead case 2: // Dead SlowlyRot(); SlowlyRot(); break; break; } // DO state transition based on computed input // DO state transition based on computed input fsm->StateTransition(input); fsm->StateTransition(input);}

9 Mealy & Moore Machines Mealy Machine Mealy Machine –A Mealy machine is an FSM whose actions are performed on transitions Moore Machine Moore Machine –A Moore machine’s actions reside in states –More intuitive for game developers

10 FSM Language Use Macros Coding a State Machine Directly Causes Lack of Structure Coding a State Machine Directly Causes Lack of Structure –Going complex when FSM at their largest Use Macro Use Macro Beneficial Properties Beneficial Properties –Structure –Readability –Debugging Simplicity Simplicity

11 FSM Language Use Macros – An Example #define BeginStateMachine … #define State(a) … … bool MyStateMachine::States(StateMachineEvent event, int state) int state){ BeginStateMachine BeginStateMachine State(0) State(0) OnUpdate OnUpdate Wander(); Wander(); if (SeeEnemy()) SetState(1); if (SeeEnemy()) SetState(1); if (Dead()) SetState(2); if (Dead()) SetState(2); State(1) State(1) OnUpdate OnUpdate Attack(); Attack(); SetState(0); SetState(0); if (Dead()) SetState(2); if (Dead()) SetState(2); State(2); State(2); OnUpdate OnUpdate RotSlowly(); RotSlowly(); EndStateMachine EndStateMachine}

12 Data-Driven FSM Scripting Language Scripting Language –Text-based script file –Transformed into »C++ Integrated into source code Integrated into source code »Bytecode Interpreted by the game Interpreted by the game Authoring Authoring –Compiler –AI editing tool Game Game –FSM script engine –FSM interface

13 Data-Driven FSM Diagram Authoring FSMs bytecode Compiler AI Editing Tool Condition & Action Vocabulary Games FSM Script Engine FSM Interface Condition & Action Code Game Engine Artist, Designers, & Developers

14 AI Editing Tool for FSM Pure Text Pure Text –Syntax ? Visual Graph with Text Visual Graph with Text Used by Designers, Artists, or Developers Used by Designers, Artists, or Developers –Non-programmers Conditions & Action Vocabulary Conditions & Action Vocabulary –SeeEnemy –CloseToEnemy –Attack –…–…–…–…

15 FSM Interface Facilitating the Binding between Vocabulary and Game World Facilitating the Binding between Vocabulary and Game World Glue Layer that Implements the Condition & Action Vocabulary in the Game World Glue Layer that Implements the Condition & Action Vocabulary in the Game World Native Conditions Native Conditions –SeeEnemy(), CloseToEnemy() Action Library Action Library –Attack(…)

16 FSM Script Language Benefits Accelerated Productivity Accelerated Productivity Contributions from Artists & Designers Contributions from Artists & Designers Ease of Use Ease of Use Extensibility Extensibility

17 Processing Models for FSMs Processing the FSMs Processing the FSMs –Evaluate the transition conditions for current state –Perform any associated actions When and How ? When and How ? – Depend on the exact need of games Three Common FSM Processing Models Three Common FSM Processing Models –Polling –Event-driven –Multithread

18 Polling Processing Model Processing Each FSM at Regular Time Intervals Processing Each FSM at Regular Time Intervals –Tied to game frame rate –Or some desired FSM update frequency –Limit one state transition in a cycle –Give a FSM a time-bound Pros Pros –Straightforward –Easy to implement –Easy to debug Cons Cons –Inefficiency »Some transition are not necessary to check every time Careful Design of Your FSM Careful Design of Your FSM

19 Event-driven Processing Model Designed to Prevent from Wasted FSM Processing Designed to Prevent from Wasted FSM Processing An FSM is Only Processed When It’s relevant An FSM is Only Processed When It’s relevant Implementation Implementation –A Publish-subscribe messaging system (Observer pattern) –Allows the engine to send events to individual FSMs –An FSM subscribes only to the events that have the potential to change the current state –When an event is generated, the FSMs subscribed to that events are all processed “As-needed” Approach “As-needed” Approach –Should be much more efficient than polling ? Tricky Balance for Fine-grained or Coarse-grained Events Tricky Balance for Fine-grained or Coarse-grained Events

20 Multithread Processing Model Both Polling & Event-Driven are Serially Processed Both Polling & Event-Driven are Serially Processed Multithread Processing Model Multithread Processing Model –Each FSM is assigned to its own thread for processing –Game engine is running in another separate thread –All FSM processing is effectively concurrent and continuous –Communication between threads must be thread-safe »Using standard locking & synchronization mechanisms Pros Pros –FSM as an autonomous agent who can constantly and independently examine and react to his environment Cons Cons –Overhead when many simultaneous characters active –Multithreaded programming is difficult

21 Interfacing with Game Engine (1/2) FSMs Encapsulate Complex Behavior Logic FSMs Encapsulate Complex Behavior Logic –Decision, condition, action, … Game Engine Does Corresponding Game Engine Does Corresponding –Character animation, movements, sounds, … The Interface : The Interface : –Code each action as a function »Need recompile if any code is changed »ie., FleeWolf() –Callbacks »Function pointers »ie., actionFunction[fleeWolf]() –Container method »actionFunctions->FleeWolf(); »DLL

22 Interfacing with Game Engine (2/2) Take TheFly3D as Example: Take TheFly3D as Example: class AArmyUnit : public FnCharacter { … void DoAttack(…); } AArmyUnit *army; army->Object(…); army->MoveForward(dist, …); … army->DoAttack(…);

23 FSM Efficiency & Optimization Two Categories : Two Categories : –Time spent –Computational cost Scheduled Processing Scheduled Processing –Priority for each FSM –Different update frequency Load Balancing Scheme Load Balancing Scheme –Collecting statistics of past performance & extrapolating Time-bound for Each FSM Time-bound for Each FSM Do careful design Do careful design –At the design level Level-of-detail FSMs Level-of-detail FSMs

24 Level-Of-Detail FSMs Simplify the FSM When the Player Won’t Notice the Differences Simplify the FSM When the Player Won’t Notice the Differences –Outside the player’s perceptual range –Just like the LOD technique used in 3D game engine Three design Keys : Three design Keys : –Decide how many LOD levels »How much development time available ? »The approximation extent –LOD selection policy »The distance between the NPC with the player ? »If the NPC can “see” the player ? »Be careful the problem of “visible discontinuous behavior” –What kind of approximations »Cheaper and less accurate solution

25 Extending the Basic FSM Extending States Extending States –Begin-end block Stacks & FSMs Stacks & FSMs –Stack-based “history” of FSMs »“Remember” the sequence of states passed through »“Retrace” its steps at will –Hierarchical FSM Polymorphic FSMs Polymorphic FSMs Fuzzy State Machine Fuzzy State Machine –Combined with fuzzy logic BeginDoAction(); DoActions(); EndDoAction();

26 A Hierarchical FSM Example Gather Treasure Flee Fight Monster in sight No monster Monster dead Cornered Find Treasure Go To Treasure Take Treasure Find Treasure Gather Treasure Live Active FSM Stack

27 Another Hierarchical FSM Example Patrol Investigate Attack Done Noise Saw Enemy Done Go to A Look for Intruders Go to A Look for Intruders Patrol Report Noise Go Over To Noise Look for Intruders False Alerm! Investigate noise