Finite State Machines in Games Jarret Raim 2004 Based on AI Game Programming Wisdom 2
Problem: Can’t remember what we were doing FSM Example States Attack Chase Spawn Wander Events E: see an enemy S: hear a sound D: die Attack ~E ~S Chase E S D D E Wander E ~E Spawn D Problem: Can’t remember what we were doing
FSM Implementation - Code Simplest method After an action, the state might change. Requires a recompile for changes No pluggable AI Not accessible to non-programmers No set structure Can be a bottleneck. void RunLogic( int *state ) { switch( *state ) { case 0: //Wander Wander(); if( SeeEnemy() ) *state = 1; if( Dead() ) *state = 2; break; case 1: //Attack Attack(); *state = 0; case 3: //Dead SlowlyRot() }
FSM Example Problem: Can’t remember what we were doing Attack Problem: Can’t remember what we were doing ~E ~S Chase E S D D E Wander E ~E Spawn D Solution: Add an stack to keep track of the states I have visited
Stack FSM – Thief 3 Stack allows AI to move back and forth between states. Leads to more realistic behavior without increasing FSM complexity.