160 likes | 203 Views
Game AI Finite State Machine. Introduction (1/2). 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) A set states, S An input vocabulary, I
E N D
Game AI Finite State Machine
Introduction (1/2) • 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) • A set states, S • An input vocabulary, I • Transition function, T(s, i) • Map a state s and an input i to another state
Introduction (2/2) • Practical use • State • Behavior • Transition • Across states • Conditions • It’s all about driving behavior • Flow-chart Diagram • UML (universe modeling language) state chart • Arrow • Transition • Rectangle • State
An Example of FSM seeEnemy wander Attack Win Dead Rot
FSM for Games • Character AI • “Decision-Action” Model • Behavior • Mental state • Transition • Players’ action • The other characters’ actions • Some features in the game world
Implement FSM • Code-based FSM • Simple Code One Up • Straightforward • Most common • Macro-assisted FSM language • Data-Driven FSM • FSM Script Language
Coding an FSM – Code Example 1 void RunLogic(int &state) { switch(state) { case 0: // Wander Wander(); if (SeeEnemy()) state = 1; if (Dead()) state = 2; break; case 1: // Attack Attack(); if (Win()) state = 0; if (Dead()) state = 2; break; case 2: // Dead SlowlyRot(); break; } } What is the problem with the above code ?
Coding an FSM – Code Example 2 void RunLogic(FSM *fsm) { // Do action based on the state and determine next input input = STATE_NULL; switch(fsm->GetStateID()) { case STATE_WANDER: // Wander Wander(); if (SeeEnemy()) 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 SlowlyRot(); break; } // DO state transition based on computed input fsm->StateTransition(input); }
Mealy & Moore Machines • Mealy Machine • A Mealy machine is an FSM whose actions are performed on transitions • Moore Machine • A Moore machine’s actions reside in states • More intuitive for game developers
FSM Language Use Macros • Coding a state machine directly causes lack of structure • Going complex when FSM at their largest • Use Macro • Beneficial Properties • Structure • Readability • Debugging • Simplicity
FSM Language Use Macros – An Example #define BeginStateMachine … #define State(a) … … bool MyStateMachine::States(StateMachineEvent event, int state) { BeginStateMachine State(STATE_WANDER) OnUpdate Wander(); if (SeeEnemy()) SetState(STATE_ATTACK); if (Dead()) SetState(STATE_DEAD); State(STATE_ATTACK) OnUpdate Attack(); SetState(STATE_WANDER); if (Dead()) SetState(STATE_DEAD); State(STATE_DEAD); OnUpdate RotSlowly(); EndStateMachine }
Data-Driven FSM • Scripting language • Text-based script file • Transformed into • C++ • Integrated into source code • Bytecode • Interpreted by the game • Authoring • Compiler • AI editing tool • Game • FSM script engine • FSM interface
Data-Driven FSM Diagram Authoring Games FSMs FSM Script Engine bytecode Compiler Artist, Designers, & Developers AI Editing Tool FSM Interface Condition & Action Code Game Engine Condition & Action Vocabulary
AI Editing Tool for FSM • Pure text • Syntax ? • Visual graph with text • Used by designers, artists, or developers • Non-programmers • Conditions & action vocabulary • SeeEnemy • CloseToEnemy • Attack • …
FSM Interface • Facilitating the binding between vocabulary and the game world • Gluing layers that Implement the condition & action vocabulary in the game world • Native conditions • SeeEnemy(), CloseToEnemy() • Action library • Attack(…) • Evade(…) • Flee(…) • Wander(…)
FSM Script Language Benefits Accelerated productivity Contributions from artists & designers Ease of Use Extensibility