1 / 11

Game AI: Finite State Machines and Variants

Game AI: Finite State Machines and Variants. Soon Tee Teoh CS 134. Finite State Machines (FSM). FSM is one of the simplest and most basic AI models. You may have seen FSM in your Computer Architecture and your Formal Languages classes.

casta
Download Presentation

Game AI: Finite State Machines and Variants

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Game AI: Finite State Machines and Variants Soon Tee Teoh CS 134

  2. Finite State Machines (FSM) • FSM is one of the simplest and most basic AI models. • You may have seen FSM in your Computer Architecture and your Formal Languages classes. • It is useful, for example, in first-person shooter games. • Basically, FSM consists of • States • State transitions • An object (a non-player character) is in one of the states. • When certain conditions are met, the object changes to another state.

  3. FSM Basics • A FSM consists of the following 4 components: • States which define behavior and may produce actions • State transitions which are movement from one state to another • Rules or conditions which must be met to allow a state transition • Input events which are either externally or internally generated, which may possibly trigger rules and lead to state transitions

  4. Example of States • For example, Quake2 uses 9 states for the monsters: • Standing, walking, running, dodging, attacking, melee, seeing the enemy, idle and searching • In other words, at any one time, a monster can be in one of the above 9 states. • Each state has its own distinct behavior and actions. For example, a monster in the running state would behave differently from a monster in the standing state. • The behavior and actions for each state has to be defined by the programmer.

  5. State Transitions • A finite state machine must have: • an initial state which provides a starting point, and • a current state which remembers the product of the last state transition. • An input event act as a trigger • This causes an evaluation of the rules that govern the transitions from the current state to other states. A state change then occurs according to the rules. • The best way to visualize a FSM is to think of it as a directed graph of the states.

  6. Example of State Transitions state Here, a monster starts in the Idle state. In the Idle state, it just walks around. At every iteration of the game loop, check if enemy is in sight. If yes, transition to the attack state. In the attack state, chase after the enemy and fire at the enemy. In the Idle state, if the monster gets the input “Get Shot”, it will transition to the Die state. state transition input event Die Get Shot Melee Get Shot Get Shot Close range Start state Close range Idle Attack See enemy Enemy Fires Dodge Lose Sight See enemy Search Time Out

  7. Implementation Monster::Monster() : state(0) { } // initialize to start state void Monster::Iterate() { // called on every game cycle if (state==0) { Move(rand()); // move in random direction } else if (state==1) { Chase(); Shoot(); } else if (state==2) { Melee(); } else if (state==3) { Dodge(); } else if (state==4) { FindEnemy(); } else if (state==5) { } } void Monster::HandleInput(int eventID) { if (eventID==0) { // monster got shot state = 5; } else if (eventID==1) { // see enemy if ((state==0) || (state==4)) { state = 1; } } … } class Monster { int state; // 0: Idle 1: Attack 2: Melee 3: Dodge 4: Search 5: Die Monster(); void Iterate(); void HandleInput(int eventID); void Shoot(); void Melee(); void Dodge(); void Chase(); void FindEnemy(); };

  8. Implementation Tip • Implementing FSM is pretty straight-forward. • However, you are strongly suggested to keep a paper version of the state diagram (or a PowerPoint picture) for reference. • The preceding implementation does not separate the FSM code from the rest of the code. (Bad design! Not modular)

  9. Another Example:FSM for Ghost in Pac-Man Player re-spawns Move Randomly Time to exit Center Room Chase Player Player dies Player eats Pellet Pellet wears off Player eats Ghost Run from Player Rise Die Eyes reach Center Room

  10. Disadvantages of FSM • May be too predictable • Large FSM with many states and transitions can be difficult to manage and maintain. The graph may start to look like “spaghetti.” • State oscillation. States may be too rigid. Conditions are too crisp. • For example, there are two states: Flee and Idle. • The condition for being in the Flee state is to be within a distance 5.0 from the enemy. The condition for being in the Idle state is to be greater than 5.0 from the enemy. • Say, the object is 4.9 from the enemy. It is in Flee state, so it runs away. Now it is 5.1, so it is in Idle state. It randomly moves around, and goes to 4.9 and gets into the Flee state again etc.

  11. Variations of FSM • Hierarchical FSM • As a FSM increases in complexity (number of states and transitions), this is a technique to manage it and prevent it from getting out of control (becoming spaghetti). • Each state consists of several sub-states. Each state has some “border sub-states” that connects outside of the state. • Polling vs. Event-driven • Polling: There is a central list of all conditions and status. In each game cycle, each object checks all the conditions to see if it needs to transition to another state. • Event-driven: In each game cycle, if an event is generated, send a message to all objects that can be affected by the event, and the objects change state if necessary. • Event-driven saves time if transitions and events are infrequent. • Fuzzy Transitions Between two states, there should be at most two transitions, one in each direction.

More Related