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.

Slides:



Advertisements
Similar presentations
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Advertisements

Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Programming Types of Testing.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Debugging Introduction to Computing Science and Programming I.
Software Engineering and Design Principles Chapter 1.
From: From:
Chapter 1 Principles of Programming and Software Engineering.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
JokerStars: Online Card Playing William Sanville Milestone 4.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Adding Automated Functionality to Office Applications.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Dr. Ken Hoganson, © August 2014 Programming in R STAT8030 Programming in R COURSE NOTES 1: Hoganson Programming Languages.
ORNL is managed by UT-Battelle for the US Department of Energy EPICS State Notation Language (SNL), “Sequencer” Kay Kasemir, SNS/ORNL Many slides from.
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Chapter 12: Exception Handling
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Programming Translators.
Java Language and SW Dev’t
The Java Programming Language
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
Exceptions Handling Exceptionally Sticky Problems.
Introduction to Exception Handling and Defensive Programming.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Engineering Computing I Chapter 4 Functions and Program Structure.
C++ Classes and Data Structures Jeffrey S. Childs
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
© 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.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Dr Nick Mitchell (Room CM 224)
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Finite State Machines Logical and Artificial Intelligence in Games Lecture 3a.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
TESTING BASED ON ERROR GUESSING NERINGA SIPAVIČIEN Ė IFM-2/4 It is not actually guessing. Good testers do not guess…
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Principles of Programming & Software Engineering
Collision Theory and Logic
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Working with Java.
Learning to Program D is for Digital.
Collision Theory and Logic
Handling Exceptionally Sticky Problems
Principles of Programming and Software Engineering
Lesson Outcomes Be able to identify differentiate between types of error in programs Be able to interpret error messages and identify, locate.
Finite State Machines in Games
Coding Concepts (Basics)
Units with – James tedder
Handling Exceptionally Sticky Problems
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Presentation transcript:

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 Completely general to any problem Might not always provide the best solution But few can deny that they get the job done with minimal risk to the project

State Machine in game AI Disadvantage of state machine No consistent structure Development cycle churns on Poor structure This article presents Robust way to structure your state machine with a simple language Make programming games much easier

Game Developer-Style State Machine void RunLogic (int *state) { switch(*state){ case 0: //Wander Wander(); if(SeeEnemy()){ if(GetRandomChance() <0.8) *state =1; else *state =2; } if(Dead()) *state =3; break; case 1: //Attack Attack(); if(Dead()) *state =3; break; case 2: //RunAway RunAway(); if(Dead()) *state =3; break; case 3: //Dead SlowlyRot(); break; }

Game Developer-Style State Machine Serious weaknesses The state changes are poorly regulated States are of type int and would be more robust and debuggable as enums The omission of a single break keyword would couse hard- to-find bugs Redundant logic appears in multiple states No way to tell that a state has been entered for the first time No way to monitor or log how the state machine has behaved over time What is a poor game programmer to do? Provide some structure

A State Machine Language Be created with the help of macros Have six keyword BeginStateMachine EndStateMachine State OnEnter OnExit OnUpdate

The State Machine Language Example structure of the State Machine Language BeginStateMachine State(STATE_Wander) OnEnter // C or C++ code for state entry OnUpdate // C or C++ code executed every tick OnExit // C or C++ code for state clean-up State(STATE_Attack) OnEnter // C or C++ code for state entry EndStateMachine

Actual Implementation The six macro keywords #define BeginStateMachineif(state < 0){if(0){ #define EndStateMachine return(true);}}else(assert(0); \ return(false);}return(false); #define State(a) return(true);}}else if(a == state){if(0){ #define OnEvent(a) return(true);}else if(a == event){ #define OnEnterOnEvent(EVENT_Enter) #define OnUpdateOnEvent(EVENT_Update) #define OnExitOnEvent(EVENT_Exit)

Using macro Nice properties Expand in a building-block fashion Handled event Provide an easy way to monitor how the state machine is Can't mess up the state machine by forgetting something like a break keyword. Easier to read

Support function void StateMachine::Process(StateMachineEvent event){ States(event, m_currentState); int safetyCount = 10; while (m_stateChange && (-safetyCount >=0)) { assert ( safetyCount > 0 && "States are flip-flopping."); m_stateChange = false; //Let the last state clean-up States(EVENT_Exit, m_currentState); //Set the new state m_currentState = m_nextState; //Let the new state initialize States(EVENT_Entr, m_currentState); }

Integrating this Solution How is it actually integrated within a game? class Robot: public StateMachine{ public: Robot(void){}; ~Robot(void){}; private: virtual bool States(StateMachineEvent event, int state); // Put private robot specific variables here }; bool Robot::States (StateMachineEvent event, int state){ BeginStateMachine // Put any number of states and event responses. // Refer to the code on the CD for more examples. EndStateMachine }

Conclusion State Machine Language provides Simple enforced structure Excellent readability Natural to debug Full power of C/C++ within the state machine Easy to add code for entering or exiting a state State changes are more formal and protected Error checking for flip-flopping state changes No need to spend months developing a custom language

6.6 Enhancing a State Machine Language through Messaging

The Concept of Messages Message A number of enumerated type Communicate with messages between game objects The technique of messages Event-driven behavior enum MSG_Name { MSG_Attacked, MSG_Damaged, MSG_Healed, MSG_Poisoned };

Messages as Letters class MSG_Object { public: MSG_Namem_Name; // Message name(enumeration) float m_Data; // Extra data for the message objectID m_Sender; // Object tht sent the message objectID m_Receiver; // Object that will get the message float m_DeliveryTime; // Time at which to send the message };

Messages as Events Incorporate messages into the State Machine Language A message event BeginStateMachine State(STATE_Wander) OnMsg(MSG_Attacked) SetState(STATE_RunAway); State(STATE_RunAway) OnEnter //run away EndStateMachine

Message Timers When a game object sends a message to itself to create an internal event BeginStateMachine State(STATE_Wander) OnMsg(MSG_Attacked) SendDelayedMsgToMe(0.5, MSG_TimeOut); OnMsg(MSG_TimeOut) SetState(STATE_RunAway); State(STATE_RunAway) OnEnter //run away EndStateMachine

Scoping Messages Scoping Something is only valid within a certain context Scoping messages By defining a scope for messages Won't be misinterpreted in the wrong context The extraneous queued timeout messages are ignored

Scoping Messages BeginStateMachine State(STATE_Wander) OnMsg(MSG_Attacked) SendDelayedMsgToMe(0.5, MSG_TimeOut, SCOPE_TO_THIS_STATE); OnMsg(MSG_TimeOut) SetState(STATE_RunAway); State(STATE_RunAway) OnEnter SendDelayedMsgToMe(7.0, MSG_TimeOut); OnMsg(MSG_TimeOut) SetState(STATE_Wander); EndStateMachine

Redundant Message Policy Redundant Messages Multiple delayed messages All basically the same messages Accumulate in the message router Standard rules for redundant messages 1. Ignore the new message 2. Replace the old message with the new one 3. Store the new message, letting redundant messages accumulate The best policy is #1

Global Event Responses BeginStateMachine OnMsg(MSG_Dead) // Global response -Triggered regardless of state SetState(STATE_Dead); State(STATE_Wander) OnEnter //Wander State(STATE_RunAway) OnEnter //Run away State(STATE_Dead) OnEnter //Die OnMsg(MSG_Dead) //Ignore msg - this overrids the global response EndStateMachine

Recording Behavior for Debugging The need for good debugging of state machines is critical To monitor and record every event That's handled and not handled by each state machine Embed logging function Calls within the macro keywords themselves #define BeginStateMachine if(state < 0){char statename[64]= "STATE_Global"; if(0){ #define State(a) return(true);}}else if(a == state) \ {char statename[64] =#a; if(0){ #define OnMsg(a) return(true);}else if(EVENT_Message == \ event && msg && a == msg->GetMsgName()){ \ g_debuflog.LogStateMachineEvent( \ m_Owner->GetID(),msg,statename,#a,true);

Partial listing of the main function // Create game object GameObject myGameObject (g_database.GetNewObjectID()); g_database.Store(myGameObject); //Give the game object a state machine myGameObject.SetStateMachine (new Robot(&myGameObject)); myGameObject.GetStateMachine()>Initialized(); //Game Loop while(1) { g_time.MarkTimeThisTick(); myGameObject.GetStateMachine()->Update(); g_msgroute.DeliverDelayedMessages(); }

Summary Make games easier and more robust to code State machines Easy to read Easy to debug Easy to build and prototype A way to communicate with other game objects Global event responses to reduce redundant code A way to set event timers Allow transparent monitoring and logging of all events

Summary Allow execution of special code when first entered Allow execution of special code when exited Conclusion For simplicity For maintainability For robustness For ease of debugging