Top Banner
03/21/22 1 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn
26

4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

Dec 14, 2015

Download

Documents

Sandy Linch
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 1

State Driven Agents

CIS 479/579

Bruce R. Maxim

UM-Dearborn

Page 2: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 2

Buckland Software

• The executables and source code availablehttp://www.wordware.com/files/ai

The Boost library is available from

http://www.boost.org/

• The Visual C++ environment need to be configured to compile the source code by setting the appropriate paths

Page 3: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 3

Paths• Include file paths

C:\boostC:\AI_Source\CommonC:\AI_Source\Common\lua-5.0\includeC:\AI_Source\Common\laubind

• Source file pathsC:\AI_Source\CommonC:\AI_Source\Common\lua-5.0C:\AI_Source\Common\laubind

• Library file pathsC:\AI_Source\Common\lua-5.0\lib

Page 4: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 4

FSM Agent Advantages

• Quick design and simple to code• Easy to debug• Low computational overhead• Intuitive so they make sense to domain

experts• Flexible, easy for programmers to tweak to

modify behaviors

Page 5: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 5

FSM State Examples

• Pacman ghosts– evade, chase

• Quake bots– find armor, find health, seek cover, run away

• FIFA soccer players– strike, dribble, chase ball, mark player

• NPC’s in real time strategy games– move to position, patrol, follow path

Page 6: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 6

Switch Implementation - 1

• Define an enumerated type with the state namesenum StateType(RunAway, Patrol, Attack);

• Then define method that manages the transitionsvoid Agent::Update(StateType CurrentState){

switch (CurrentState) { case Runaway:

EvadeEnemy( );if (Safe( )) ChangeState(Patrol);

break;…

Page 7: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 7

Switch Implementation - 2

• This works OK for simple FSM’s• Becomes overly complicated for FSM’s with

lots of states and transitions• Requires recompilation if states are changed• Not easy to have simultaneous behaviors

implemented (run and shoot) without defining lots of states

Page 8: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 8

State Transition Table -1

Current State Condition State Transition

Runaway Safe Patrol

Attack Weaker than enemy Runaway

Patrol Threatened and stronger than enemy

Attack

Patrol Threatened and

weaker than enemy

Runaway

Page 9: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 9

State Transition Table - 2

• Agent queries table as needed• Table can stored in file and edited without

recompiling the program• Each state can be modeled as a separate

objects or functions called by the agent• Fairly easy to extend and modify

Page 10: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 10

State Design Pattern - 1

• The state transition rules can be embedded in the states themselves

• All states share a common interface using a pure virtual class

class State

{

public:

virtual void Execute(Troll* troll) = 0;

}

Page 11: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 11

State Design Pattern - 2class Troll{ State* m_pCurrentState; public: void Update() { m_pCurrentState->Execute(this); } void ChangeState(const State* pNewState) { delete m_pCurrentState; m_pCurrentState = pNewState; }}

Page 12: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 12

State Design Pattern - 3

class State_Runaway : public State

{

public:

void Execute(Troll* troll)

{

if (troll->isSafe())

troll->ChangeState(new State_Sleep());

else

troll->MoveAwayFromEnemy();

}

}

Page 13: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 13

State Design Pattern - 4

class State_Sleep : public State

{

public:

void Execute(Troll* troll)

{

if (troll->isThreatned())

troll->ChangeState(new State_RunAway());

else

troll->Snore();

}

}

Page 14: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 14

West World Miner States

EnterMineDigForGold

QuenchThirstSaloonVisitBankDepositGold

GoHomeSleepwealthy enough

not thirstynot wealthy

rested

thirstypockets full

Page 15: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 15

Classes

• All West World inhabitants are derived from the base class BaseGameEntity

• Each entity will be assigned a unique identifier

• The Miner class is an example of one such class

• The State class is the base abstract class and each of the four Miner states needs its own class definition

Page 16: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 16

Reusable Classes

• The State base class and the Miner owned classes would be more easily reused if were templates

• Creating a StateMachine class with ways of tracking the Miner’s current, previous, and global states gives the FSM a crude memory to allow more complex (context sensitive) behaviors

Page 17: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 17

Adding New NPC

• Elsa only visits two states VisitBathroom (1 chance in 10) or DoHouseWork

• This makes use of the template version of the State class and required the definition of a new global state WifeGlobalState to deal with the bathroom visits

• This requires adding classes MinersWife and MinersWifeOwnedStates

Page 18: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 18

Adding NPC Communication

• Good games make use of event driven architectures

• When events occur they are broadcast to other game objects who are programmed to react to the event or ignore it

• Intelligent game agents can send messages to each other to allow cooperative behaviors

Page 19: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 19

Telegram Structurestruct Telegram{ //the entity that sent this telegram int Sender; //the entity that is to receive this telegram int Receiver; //the message itself. These are all enumerated in the file //"MessageTypes.h" int Msg; //If a delay is necessary this field is stamped with the time //the message should be dispatched. double DispatchTime; //any additional information that may accompany the message void* ExtraInfo; // constructors not shown here}

Page 20: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 20

Message Types#include <string>

enum message_type{ Msg_HiHoneyImHome, Msg_StewReady,};

inline std::string MsgToStr(int msg){ switch (msg) { case 1: return "HiHoneyImHome"; case 2: return "StewReady"; default: return "Not recognized!"; }}

Page 21: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 21

Elsa’s New States

CookStew

VisitBathroom

DoHouseWorkprevious state

Previous state

Receive “honey I’m home”

Delayed message to self“Stew Ready”

1 chance in 10

1 chance in 10

Page 22: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 22

Message Management

• Creation, dispatch, and management of telegrams is handled by the class MessageDispatcher

• The method DispatchMessage is used by every agent to send a message to entity with a known ID

• The database of ID’s are managed by the class EntityManager

Page 23: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 23

Use Priority Queue

• Telegrams with delays are store in a priority queue ordered by their Time Stamp data

• Telegrams without delays (e.g. “I just shot you with my crossbow”) are sent right away

• The front queue message is removed long with any unprocessed expired telegrams

Page 24: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 24

FSM Changes

• The BaseEntity class needs to be modified (add HandleMessage) so that any derived class can receive messages

• The State class needs to be modified so that BaseEntity states can choose to accept and handle messages (add OnMessage method)

• The StateMachine class needs its own version of HandleMessage

Page 25: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 25

Elsa Cooks Dinner

• Miner enters shack and sends “honey I’m home” message

• Elsa receives “honey I’m home” and changes state to CookStew

• Elsa puts Stew in oven and sends delayed message “Stew ready”

• Elsa receives “Stew ready”, takes out stew, and sends Miner “Dinner ready” message

• Miner receives “Dinner ready” and changes state to EatStew

Page 26: 4/10/20151 State Driven Agents CIS 479/579 Bruce R. Maxim UM-Dearborn.

04/18/23 26

What happens?

• In between messages the NPC’s are free to respond to all state appropriate inputs

• This may means that the Miner has left the building before receiving the “Dinner ready” message

• Sometimes you need two FSM’s working in parallel (one for character movement and one for character’s weapon)

• Sometimes you need nested state machines (aka hierarchical FSM’s)