AI agents with FSM: a question regarding this

Posted by Prog on Game Development See other posts from Game Development or by Prog
Published on 2014-06-02T22:38:45Z Indexed on 2014/06/03 3:44 UTC
Read the original article Hit count: 204

Filed under:
|

Finite State Machines implemented with the State design pattern are a common way to design AI agents.

I am familiar with the State design pattern and know how to implement it.

However I have a question regarding how this is used in games to design AI agents.

Please consider a class Monster that represents an AI agent. Simplified it looks like this:

class Monster{
    State state;
    // other fields omitted 

    public void update(){ // called every game-loop cycle
        state.execute(this);
    }

    public void setState(State state){
        this.state = state;
    }

    // irrelevant stuff omitted       
}

There are several State subclasses that implement execute() differently. So far classic State pattern.

Here's my question:

AI agents are subject to environmental effects and other objects communicating with them.

For example an AI agent might tell another AI agent to attack (i.e. agent.attack()). Or a fireball might tell an AI agent to fall down. This means that the agent must have methods such as attack() and fallDown(), or commonly some message receiving mechanism to understand such messages.

My question is divided to two parts:

1- Please say if this is correct: With an FSM, the current State of the agent should be the one taking care of such method calls - i.e. the agent delegates to the current state upon every event. Correct? Or wrong?

2- If correct, than how is this done? Are all states obligated by their superclass) to implement methods such as attack(), fallDown() etc., so the agent can always delegate to them on almost every event? Or is it done in some other way?

© Game Development or respective owner

Related posts about ai

Related posts about fsm