Lecture_11_-_Decision_Trees_-_State_Machines_ppt
CSD1130 GAME Implementation Techniques
Lecture 11: Decision Trees & State Machines
Overview
Decision Making
Decision Trees
State Machines
Finite State Machines (FSM)
Hierarchical State Machines
FSM Scripting Language
AI Behavior in Platformer Outline
Decision Making
Knowledge Types
Internal Knowledge: Knowledge possessed by a character.
External Knowledge: Information external to the character's perception.
Process Model
Decision Maker: Absorbs knowledge.
Input Changes: Internal or external changes can alter the decision context.
Action Request: Translates knowledge and changes into specific actions.
Decision Trees
Basic Concept
A Decision Tree is a conditional structure consisting of a series of yes/no questions leading to a decision based on the current situation.
Each path in the tree represents potential actions (e.g., attack, hide, patrol).
Structure Example
Questions guiding decision making:
Is enemy visible?
If Yes: Check if enemy < 10 m.
If Yes: Attack.
If No: Move.
If No: Stand still.
Implementation of Decision Trees
Node Structure in C++
struct NodeTree {
void * data; // or int, or object
NodeTree * left;
NodeTree * right;
virtual NodeTree * Execute() = 0;
void MakeDecision();
};Binary Decision Tree
Structure resembles a doubly linked list for managing states.
Example of Decision Logic
C++-Style Decision Logic
struct DecisionTreeNode {
void * data;
DecisionTreeNode * left;
DecisionTreeNode * right;
DecisionTreeNode * Execute();
void MakeDecision();
};Execution Logic: Decision-making function performs actions based on the object state.
Combinations of Decisions
Using logical operations in decision-making:f
Example:
If A AND B: Action 1
Otherwise: Action 2
If A OR B: Action 1
Otherwise: Action 2
Balancing Decision Trees
Unbalanced Tree vs. Balanced Tree:
Unbalanced trees can lead to inefficient decision paths.
Balancing improves performance and responsiveness.
Random Decision Trees
Decision nodes can introduce randomness:
Example:
If under attack: Defend
Else: Patrol or Stand Still.
Finite State Machines (FSM)
Concept
Visual representation of different states a character can be in during gameplay.
States include:
Climbing
Running
Swimming
Hitting
State Transitioning
State Manager: Tracks current states and transitions triggered by conditions.
Each update checks for eligible transitions.
Implementation of FSM
Generic Structure
Basic FSM class structure managing states and transitions:
class FSM {
m_states; // Holds states
m_initialState;
m_currentState;
Update(); // Checks transitions
};Hard-Coded Examples
Example illustrating hard-coded state transitions based on character conditions:
class FSM {
enum State { PATROL, DEFEND, SLEEP };
Update() {
if (myState == PATROL) { ... }
}
};Pros and Cons of FSM
Pros
Easy to implement for simple behaviors.
Cons
Difficult to maintain as complexity increases.
FSM with Macros
Structure
FSMs can leverage macros for readability and maintainability:
bool FSM::States(...) {
BeginStateMachine
State(...) { ... }
EndStateMachine
};Hierarchical State Machines
Concept
Enhances FSM with layered states, managing states within other states for complex behaviors.
Conclusion and References
Essential Reading:
"Artificial Intelligence for Games" by Ian Millington
"AI Game Programming Wisdom 2" by Steve Rabin
Decision Diagrams
Overview: Decision diagrams are graphical representations used to illustrate decision-making processes. They consist of nodes and arcs that connect the nodes, helping to visualize the flow of decisions.
Components of Decision Diagrams:
Nodes: Represent decisions, conditions, or actions.
Arcs: Connect nodes, indicating the direction and flow of decision-making.
Leaves: Endpoints of the diagram, representing final actions or outcomes.
Types:
Decision Trees: A type of decision diagram where each node is a decision point leading to various outcomes based on yes/no questions.
Influence Diagrams: Show the influence relationships among decisions, outcomes, and uncertainties, usually incorporating probabilities.
Applications:
In AI, decision diagrams can be employed for modeling complex decision-making scenarios, ensuring clarity and methodical structuring of choices and their consequences.
Example: In gaming, decision diagrams can guide a character’s behavior based on player inputs or environment changes, effectively managing state transitions.