Game AI Finite State Machine 1 Introduction 12

  • Slides: 16
Download presentation
Game AI Finite State Machine 1

Game AI Finite State Machine 1

Introduction (1/2) n Finite State Machine (FSM) is the most commonly used Game AI

Introduction (1/2) n Finite State Machine (FSM) is the most commonly used Game AI technology – – n Simple Efficient Easily extensible Powerful enough to handle a wide variety of situations Theory (Simplified) – – – A set states, S An input vocabulary, I Transition function, T(s, i) » Map a state s and an input i to another state 2

Introduction (2/2) n Practical use – State » Behavior – Transition » Across states

Introduction (2/2) n Practical use – State » Behavior – Transition » Across states » Conditions – It’s all about driving behavior n Flow-chart Diagram – UML (universe modeling language) state chart » Arrow n Transition » Rectangle n State 3

An Example of FSM see. Enemy wander Attack Dea d Win Rot 4

An Example of FSM see. Enemy wander Attack Dea d Win Rot 4

FSM for Games n n n Character AI “Decision-Action” Model Behavior – Mental state

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

Implement FSM n Code-based FSM – Simple Code One Up » Straightforward » Most

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

Coding an FSM – Code Example 1 void Run. Logic(int &state) { switch(state) {

Coding an FSM – Code Example 1 void Run. Logic(int &state) { switch(state) { case 0: // Wander(); if (See. Enemy()) state = 1; if (Dead()) state = 2; break; case 1: // Attack(); if (Win()) state = 0; if (Dead()) state = 2; break; case 2: // Dead Slowly. Rot(); break; } } What is the problem with the above code ? 7

Coding an FSM – Code Example 2 void Run. Logic(FSM *fsm) { // Do

Coding an FSM – Code Example 2 void Run. Logic(FSM *fsm) { // Do action based on the state and determine next input = STATE_NULL; switch(fsm->Get. State. ID()) { case STATE_WANDER: // Wander(); if (See. Enemy()) input = STATE_SEE_ENEMY; if (Dead()) input = STATE_DEAD; break; case STATE_ATTACK: // attack Attack(); if (Win()) input = STATE_WANDER; if (Dead()) input = STATE_DEAD; break; case STATE_DEAD: // Dead Slowly. Rot(); break; } // DO state transition based on computed input 8 fsm->State. Transition(input); }

Mealy & Moore Machines n Mealy Machine – A Mealy machine is an FSM

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

FSM Language Use Macros n Coding a state machine directly causes lack of structure

FSM Language Use Macros n Coding a state machine directly causes lack of structure – Going complex when FSM at their largest n n Use Macro Beneficial Properties – – – n Structure Readability Debugging Simplicity 10

FSM Language Use Macros – An Example #define Begin. State. Machine … #define State(a)

FSM Language Use Macros – An Example #define Begin. State. Machine … #define State(a) … … bool My. State. Machine: : States(State. Machine. Event event, int state) { Begin. State. Machine State(STATE_WANDER) On. Update Wander(); if (See. Enemy()) Set. State(STATE_ATTACK); if (Dead()) Set. State(STATE_DEAD); State(STATE_ATTACK) On. Update Attack(); Set. State(STATE_WANDER); if (Dead()) Set. State(STATE_DEAD); On. Update Rot. Slowly(); End. State. Machine } 11

Data-Driven FSM n Scripting language – Text-based script file – Transformed into » C++

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

Data-Driven FSM Diagram Authoring Artist, Designers, & Developers FSMs AI Editing Tool Condition &

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

AI Editing Tool for FSM n Pure text – Syntax ? n n Visual

AI Editing Tool for FSM n Pure text – Syntax ? n n Visual graph with text Used by designers, artists, or developers – Non-programmers n Conditions & action vocabulary – – See. Enemy Close. To. Enemy Attack … 14

FSM Interface n n n Facilitating the binding between vocabulary and the game world

FSM Interface n n n Facilitating the binding between vocabulary and the game world Gluing layers that Implement the condition & action vocabulary in the game world Native conditions – See. Enemy(), Close. To. Enemy() n Action library – – Attack(…) Evade(…) Flee(…) Wander(…) 15

FSM Script Language Benefits n n Accelerated productivity Contributions from artists & designers Ease

FSM Script Language Benefits n n Accelerated productivity Contributions from artists & designers Ease of Use Extensibility 16