Title: Game AI
1Game AI Finite State Machine
2Introduction (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
3Introduction (2/2)
- Practical use
- State
- Behavior
- Transition
- Across states
- Conditions
- Its all about driving behavior
- Flow-chart Diagram
- UML (universe modeling language) state chart
- Arrow
- Transition
- Rectangle
- State
4An Example of FSM
seeEnemy
wander
Attack
Win
Dead
Rot
5FSM for Games
- Character AI
- Decision-Action Model
- Behavior
- Mental state
- Transition
- Players action
- The other characters actions
- Some features in the game world
6Implement FSM
- Code-based FSM
- Simple Code One Up
- Straightforward
- Most common
- Macro-assisted FSM language
- Data-Driven FSM
- FSM Script Language
7Coding 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 ?
8Coding an FSM Code Example 2
void RunLogic(FSM fsm) // Do action based
on the state and determine next input input
STATE_NULL switch(fsm-gtGetStateID())
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-gtStateTransition(in
put)
9Mealy Moore Machines
- Mealy Machine
- A Mealy machine is an FSM whose actions are
performed on transitions - Moore Machine
- A Moore machines actions reside in states
- More intuitive for game developers
10FSM 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
11FSM Language Use Macros An Example
define BeginStateMachine define State(a)
bool MyStateMachineStates(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
12Data-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
13Data-Driven FSM Diagram
Authoring
Games
FSMs
bytecode
FSM Script Engine
Compiler
Artist, Designers, Developers
AI Editing Tool
FSM Interface
Condition Action Code
Game Engine
Condition Action Vocabulary
14AI 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
15FSM 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()
16FSM Script Language Benefits
- Accelerated productivity
- Contributions from artists designers
- Ease of Use
- Extensibility