FSM–有限状态机
今天我们来探讨一下Finite State Machine,有限状态机,简称FSM,有兴趣的同学可以看一下《游戏编程中的人工智能》这本书,写得很经典(无论从代码上来说还是思维上来说),此次的例子基本是在此书的思想结构上来编程的。
FSM的基本思想我不多赘述,我们只关注代码的实现。
首先是State基类
State.h
template<class Type>
class State
{
public:
virtual ~State(){}
virtual void enter(Type*) = 0;
virtual void execute(Type*) = 0;
virtual void exit(Type*) = 0;
virtual bool onMsg(Type*, const Telegram&) = 0;
};
State是一个纯虚类,我们通过继承来使用它的子类,而不用State本身。
这里有一个很好的抽象,State将所有的事件状态分为了enter,execute,exit三个基本事件,这样在游戏中的所有事件,无论是持续性的还是瞬时事件都可以用其概括。
接下来我们看一下StateMachine类(!!!重要提示,文件头不要使用#ifndef宏,会出现宏冲突,请使用#pragma once)
StateMachine.h
template<class Type>
class StateMachine
{
private:
State<Type>* _currentState;
State<Type>* _previousState;
State<Type>* _globalState;
Type* _owner;
public:
StateMachine(Type*);
virtual ~StateMachine();
public:
//ignore getxx & setxx...
void update() const;
bool handleMsg(const Telegram&);
void changeState(State<Type>*);
bool isStateOn(State<Type>& state)const;
void revert2PreviousState();
};
template<class Type>
StateMachine<Type>::StateMachine(Type* owner) :_currentState(NULL),
_previousState(NULL),
_globalState(NULL),
_owner(owner)
{
}
template<class Type>
StateMachine<Type>::~StateMachine()
{
}
template<class Type>
void StateMachine<Type>::update()const
{
if (_globalState)
_globalState->execute(_owner);
if (_currentState)
_currentState->execute(_owner);
}
template<class Type>
bool StateMachine<Type>::handleMsg(const Telegram& tel)
{
if (_currentState&&_currentState->onMsg(_owner, tel))
return true;
if (_globalState&&_globalState->onMsg(_owner, tel))
return true;
return false;
}
template<class Type>
void StateMachine<Type>::changeState(State<Type>* newState)
{
assert(newState&&"a null state,tring to changeState falied");
_previousState = _currentState;
_currentState->exit(_owner);
_currentState = newState;
_currentState->enter(_owner);
}
template<class Type>
bool StateMachine<Type>::isStateOn(State<Type>& state)const
{
return typeid(*_currentState) == typeid(state);
}
template<class Type>
void StateMachine<Type>::revert2PreviousState()
{
changeState(_previousState);
}
我们看一下update函数就可以知道此类的运行方式,每次更新执行globalState,意指随时可能发生的状态,比如你下班回家,突然下雨了,你要找一家咖啡店避雨,这个状态,有别于其他按序或者按规则发生的状态,不受其他状态约束,在避雨状态完了之后,你会revert2PreviousState,返回原本状态(回滚),此处为继续回家。
然后每次更新还要执行currentState,此状态按规则(一般是自身属性规则),接下来描述一个状态切换,一个miner矿工的状态集合
MinerOwnedStates.h
using namespace std;
class Miner;
class EMADFN :public State < Miner >, public Singleton < EMADFN >//enter mine and dig for nugget
{
public:
EMADFN(){}
virtual void enter(Miner*);
virtual void execute(Miner*);
virtual void exit(Miner*);
virtual bool onMsg(Miner*, const Telegram&);
};
class VBADG :public State< Miner >, public Singleton < VBADG >//visit bank and deposit gold
{
public:
VBADG(){}
virtual void enter(Miner*);
virtual void execute(Miner*);
virtual void exit(Miner*);
virtual bool onMsg(Miner*, const Telegram&);
};
class GHASTR :public State < Miner >, public Singleton < GHASTR >//go home and sleep til rested
{
public:
GHASTR(){}
virtual void enter(Miner*);
virtual void execute(Miner*);
virtual void exit(Miner*);
virtual bool onMsg(Miner*, const Telegram&);
};
class QT :public State < Miner >, public Singleton < QT >//quench thirst
{
public:
QT(){}
virtual void enter(Miner*);
virtual void execute(Miner*);
virtual void exit(Miner*);
virtual bool onMsg(Miner*, const Telegram&);
};
class ES :public State < Miner >, public Singleton < ES >//eat stew
{
public:
ES(){}
virtual void enter(Miner*);
virtual void execute(Miner*);
virtual void exit(Miner*);
virtual bool onMsg(Miner*, const Telegram&);
};
MinerOwnedStates.cpp
#include"MinerStates.h"
#include"Miner.h"
//------------------------------------------------------------------------------------------------------------------------
//enter mine and dig for nugget
void EMADFN::enter(Miner* miner)
{
if (miner->location() != goldMine)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": " << "Walkin' to the goldmine";
miner->locationChange(goldMine);
}
}
void EMADFN::execute(Miner* miner)
{
miner->addToGoldCarried(1);
miner->increaseFatigue();
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": " << "Pickin' up a nugget";
//if enough gold mined, go and put it in the bank
if (miner->pocketsFull())
{
miner->getFSM()->changeState(vbadg);
}
if (miner->thirst())
{
miner->getFSM()->changeState(qt);
}
}
void EMADFN::exit(Miner* miner)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": "
<< "Ah'm leavin' the goldmine with mah pockets full o' sweet gold";
}
bool EMADFN::onMsg(Miner* miner, const Telegram& tel)
{
return false;
}
//------------------------------------------------------------------------------------------------------------------------
//visit bank and deposit gold
void VBADG::enter(Miner* miner)
{
if (miner->location() != bank)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": " << "Goin' to the bank. Yes siree";
miner->locationChange(bank);
}
}
void VBADG::execute(Miner* miner)
{
miner->addToWealth(miner->goldCarried());
miner->setGoldCarried(0);
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": "
<< "Depositing gold. Total savings now: " << miner->moneyInBank();
//wealthy enough to have a well earned rest?
if (miner->moneyInBank() >= COMFORT_LEVEL)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": "
<< "WooHoo! Rich enough for now. Back home to mah li'lle lady";
miner->getFSM()->changeState(ghastr);
}
//otherwise get more gold
else
{
miner->getFSM()->changeState(emadfn);
}
}
void VBADG::exit(Miner* miner)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": " << "Leavin' the bank";
}
bool VBADG::onMsg(Miner* miner, const Telegram& tel)
{
return false;
}
//------------------------------------------------------------------------------------------------------------------------
//go home and sleep til rested
void GHASTR::enter(Miner* miner)
{
if (miner->location() != shack)
{
cout << "\n" << EntityManenger::instance()->getNameOfEntity(miner->ID()) << ": " << "Walkin' home";
miner->locationChange(shack);
//let the wife know I'm home
MSGDISPATCHER->addMsg(DEFAULT_DELAY, //time delay
miner->ID(), //ID of sender
EntityManenger::ent_Elsa, //ID of recipient
Bob_home, //the message
NO_EXTRA_INFO);
}
}
void GHASTR::execute(Miner* miner)
{
//if miner is not fatigued start to dig for nuggets again.
if (!miner->fatigued())
{
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": "
<< "All mah fatigue has drained away. Time to find more gold!";
miner->getFSM()->changeState(EMADFN::instance());
}
else
{
//sleep
miner->decreaseFatigue();
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "ZZZZ... ";
}
}
void GHASTR::exit(Miner* miner)
{
return;
}
bool GHASTR::onMsg(Miner* miner, const Telegram& tel)
{
switch (tel.msg_)
{
case Stew_cooked:
cout << "\nMessage handled by " << EMGR->getNameOfEntity(miner->ID())
<< " at time: " << clock();
cout << "\n" << EMGR->getNameOfEntity(miner->ID())
<< ": Okay Hun, ahm a comin'!";
miner->getFSM()->changeState(ES::instance());
return true;
default:
return false;
}//end switch
}
//------------------------------------------------------------------------------------------------------------------------
//quench thirst
void QT::enter(Miner* miner)
{
if (miner->location() != saloon)
{
miner->locationChange(saloon);
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "Boy, ah sure is thusty! Walking to the saloon";
}
}
void QT::execute(Miner* miner)
{
miner->buyAndDrinkAWhiskey();
cout << "\n" <<EMGR->getNameOfEntity(miner->ID()) << ": " << "That's mighty fine sippin' liquer";
miner->getFSM()->changeState(EMADFN::instance());
}
void QT::exit(Miner* miner)
{
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "Leaving the saloon, feelin' good";
}
bool QT::onMsg(Miner* miner, const Telegram& tel)
{
return false;
}
//------------------------------------------------------------------------------------------------------------------------
//eat stew
void ES::enter(Miner* miner)
{
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "Smells Reaaal goood Elsa!";
}
void ES::execute(Miner* miner)
{
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "Tastes real good too!";
miner->getFSM()->revert2PreviousState();
}
void ES::exit(Miner* miner)
{
cout << "\n" << EMGR->getNameOfEntity(miner->ID()) << ": " << "Thankya li'lle lady. Ah better get back to whatever ah wuz doin'";
}
bool ES::onMsg(Miner* miner, const Telegram& tel)
{
return false;
}
此处由于是测试,并未带有完整的消息处理。这个状态集合描述了miner从工作,赚钱,休息,吃饭的完整集合,状态可循环。下次我会放上整个程序给大家看看。
准备写一个有关游戏底层算法,物理算法,以及AI(重点是机器学习在游戏中的应用)的长篇博客,欢迎大家指正交流╰( ̄▽ ̄)╯