Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Game AI Finite State Machine

2 Finite State Machine (FSM) is the most commonly used Game AI technology Finite State Machine (FSM) is the most commonly used Game AI technology –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 s and an input i to another state 2 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 (universe modeling language) state chart »Arrow Transition Transition »Rectangle State State 3 Introduction (2/2)

4 4 An Example of FSM wander Attack Rot seeEnemy Win Dead

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 5 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 6 Implement FSM

7 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(); if (Win()) state = 0; if (Win()) state = 0; if (Dead()) state = 2; if (Dead()) state = 2; break; break; case 2: // Dead case 2: // Dead SlowlyRot(); SlowlyRot(); break; break; }} What is the problem with the above code ?

8 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 = STATE_NULL; input = STATE_NULL; switch(fsm->GetStateID()) switch(fsm->GetStateID()) { case STATE_WANDER: // Wander case STATE_WANDER: // Wander Wander(); Wander(); if (SeeEnemy()) input = STATE_SEE_ENEMY; if (SeeEnemy()) input = STATE_SEE_ENEMY; if (Dead()) input = STATE_DEAD; if (Dead()) input = STATE_DEAD; break; break; case STATE_ATTACK: // attack case STATE_ATTACK: // attack Attack(); Attack(); if (Win()) input = STATE_WANDER; if (Win()) input = STATE_WANDER; if (Dead()) input = STATE_DEAD; if (Dead()) input = STATE_DEAD; break; break; case STATE_DEAD: // Dead case STATE_DEAD: // 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 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 9 Mealy & Moore Machines

10 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 10 FSM Language Use Macros

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

12 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 12 Data-Driven FSM

13 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 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 –…–…–…–… 14 AI Editing Tool for FSM

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

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


Download ppt "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."

Similar presentations


Ads by Google