16
1 Game AI Game AI Finite State Machine Finite State Machine

Game AI Finite State Machine

  • Upload
    vachel

  • View
    57

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Game AI Finite State Machine

1

Game AIGame AIFinite State MachineFinite State Machine

Page 2: Game AI Finite State Machine

Finite State Machine (FSM) is the most Finite State Machine (FSM) is the most commonly used Game AI technologycommonly used Game AI technology– SimpleSimple– EfficientEfficient– Easily extensibleEasily extensible– Powerful enough to handle a wide variety of Powerful enough to handle a wide variety of

situationssituations Theory (Simplified)Theory (Simplified)

– A set states, SA set states, S– An input vocabulary, IAn input vocabulary, I– Transition function, T(s, i)Transition function, T(s, i)

» Map a state s and an input i to another stateMap a state s and an input i to another state

2

Introduction (1/2)Introduction (1/2)

Page 3: Game AI Finite State Machine

Practical usePractical use– StateState

» BehaviorBehavior

– TransitionTransition» Across statesAcross states» ConditionsConditions

– It’s all about driving behaviorIt’s all about driving behavior Flow-chart DiagramFlow-chart Diagram

– UML (universe modeling language) state chartUML (universe modeling language) state chart» ArrowArrow

TransitionTransition

» RectangleRectangle StateState

3

Introduction (2/2)Introduction (2/2)

Page 4: Game AI Finite State Machine

4

An Example of FSMAn Example of FSM

wander

Attack

Rot

seeEnemy

Win

Dea

d

Page 5: Game AI Finite State Machine

Character AICharacter AI ““Decision-Action” ModelDecision-Action” Model BehaviorBehavior

– Mental stateMental state TransitionTransition

– Players’ actionPlayers’ action– The other characters’ actionsThe other characters’ actions– Some features in the game worldSome features in the game world

5

FSM for GamesFSM for Games

Page 6: Game AI Finite State Machine

Code-based FSMCode-based FSM– Simple Code One UpSimple Code One Up

» StraightforwardStraightforward» Most commonMost common

– Macro-assisted FSM languageMacro-assisted FSM language Data-Driven FSMData-Driven FSM

– FSM Script LanguageFSM Script Language

6

Implement FSMImplement FSM

Page 7: Game AI Finite State Machine

7

Coding an FSM – Code Example 1Coding an FSM – Code Example 1

void RunLogic(int &state)void RunLogic(int &state){{ switch(state)switch(state) {{ case case 00: : // Wander// Wander Wander();Wander(); if (SeeEnemy()) state = 1;if (SeeEnemy()) state = 1; if (Dead()) state = 2;if (Dead()) state = 2; break;break; case case 11: : // Attack// Attack Attack();Attack(); if (Win()) state = 0;if (Win()) state = 0; if (Dead()) state = 2;if (Dead()) state = 2; break;break; case case 22: : // Dead// Dead SlowlyRot();SlowlyRot(); break;break; }}}}

What is the problem with the above code ?

Page 8: Game AI Finite State Machine

8

Coding an FSM – Code Example 2Coding an FSM – Code Example 2void RunLogic(FSM *fsm)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: case STATE_WANDER: // 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: case STATE_ATTACK: // 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: case STATE_DEAD: // 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);}}

Page 9: Game AI Finite State Machine

Mealy MachineMealy Machine– A Mealy machine is an FSM whose actions are A Mealy machine is an FSM whose actions are

performed on transitionsperformed on transitions Moore MachineMoore Machine

– A Moore machine’s actions reside in statesA Moore machine’s actions reside in states– More intuitive for game developersMore intuitive for game developers

9

Mealy & Moore MachinesMealy & Moore Machines

Page 10: Game AI Finite State Machine

Coding a state machine directly causes lack of Coding a state machine directly causes lack of structurestructure– Going complex when FSM at their largestGoing complex when FSM at their largest

Use MacroUse Macro Beneficial PropertiesBeneficial Properties

– StructureStructure– ReadabilityReadability– DebuggingDebugging

SimplicitySimplicity

10

FSM Language Use MacrosFSM Language Use Macros

Page 11: Game AI Finite State Machine

11

FSM Language Use Macros – An ExampleFSM Language Use Macros – An Example#define BeginStateMachine …#define BeginStateMachine …#define State(a) …#define State(a) ………bool MyStateMachine::States(StateMachineEvent event,bool MyStateMachine::States(StateMachineEvent event, int state)int state){{ BeginStateMachineBeginStateMachine State(STATE_WANDER)State(STATE_WANDER) OnUpdateOnUpdate 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) OnUpdateOnUpdate Attack();Attack(); SetState(STATE_WANDER);SetState(STATE_WANDER); if (Dead()) SetState(STATE_DEAD);if (Dead()) SetState(STATE_DEAD); State(STATE_DEAD);State(STATE_DEAD); OnUpdateOnUpdate RotSlowly();RotSlowly(); EndStateMachineEndStateMachine}}

Page 12: Game AI Finite State Machine

Scripting languageScripting language– Text-based script fileText-based script file– Transformed intoTransformed into

» C++C++ Integrated into source codeIntegrated into source code

» BytecodeBytecode Interpreted by the gameInterpreted by the game

AuthoringAuthoring– CompilerCompiler– AI editing toolAI editing tool

GameGame– FSM script engineFSM script engine– FSM interfaceFSM interface

12

Data-Driven FSMData-Driven FSM

Page 13: Game AI Finite State Machine

13

Data-Driven FSM DiagramData-Driven FSM Diagram

Authoring

FSMs bytecodeCompiler

AI EditingTool

Condition &Action

Vocabulary

Games

FSM ScriptEngine

FSM Interface

Condition &Action Code

Game Engine

Artist,Designers, &Developers

Page 14: Game AI Finite State Machine

Pure textPure text– Syntax ?Syntax ?

Visual graph with textVisual graph with text Used by designers, artists, or developersUsed by designers, artists, or developers

– Non-programmersNon-programmers Conditions & action vocabularyConditions & action vocabulary

– SeeEnemySeeEnemy– CloseToEnemyCloseToEnemy– AttackAttack– ……

14

AI Editing Tool for FSMAI Editing Tool for FSM

Page 15: Game AI Finite State Machine

Facilitating the binding between vocabulary Facilitating the binding between vocabulary and the game worldand the game world

Gluing layers that Implement the condition & Gluing layers that Implement the condition & action vocabulary in the game worldaction vocabulary in the game world

Native conditionsNative conditions– SeeEnemy(), CloseToEnemy()SeeEnemy(), CloseToEnemy()

Action libraryAction library– Attack(…) Attack(…) – Evade(…)Evade(…)– Flee(…)Flee(…)– Wander(…)Wander(…)

15

FSM InterfaceFSM Interface

Page 16: Game AI Finite State Machine

Accelerated productivityAccelerated productivity Contributions from artists & designersContributions from artists & designers Ease of UseEase of Use ExtensibilityExtensibility

16

FSM Script Language BenefitsFSM Script Language Benefits