Game ai.fsm.02
-
Upload
hoseo-university -
Category
Travel
-
view
420 -
download
0
Transcript of Game ai.fsm.02
상태구동형 에이전트의 디자인By Changhoon Parkhttp://www.wawworld.me
Last Update : 2012. 03. 03
게임 인공지능GameAI
12년 3월 31일 토요일
2
By Changhoon Park http://wawworld.me
West World 프로젝트
12년 3월 31일 토요일
4
By Changhoon Park http://wawworld.me
BaseGameEntity 클래스
class BaseGameEntity{private: int m_ID; static int m_iNextValidID; void SetID(int val);public:
BaseGameEntity(int id) { SetID(id); } virtual ~BaseGameEntity(){} virtual void Update()=0; int ID()const{return m_ID;} };
12년 3월 31일 토요일
5
By Changhoon Park http://wawworld.me
Minor 클래스
class Miner : public BaseGameEntity{private: State* m_pCurrentState; location_type m_Location; int m_iGoldCarried; int m_iMoneyInBank; int m_iThirst; int m_iFatigue;public:
Miner(int id); void Update(); void ChangeState(State* new_state);}
12년 3월 31일 토요일
6
By Changhoon Park http://wawworld.me
Minor 클래스
void Miner::Update() { m_iThirst += 1;
if (m_pCurrentState) { m_pCurrentState->Execute(this); }}
12년 3월 31일 토요일
7
By Changhoon Park http://wawworld.me
Minor의 상태들
12년 3월 31일 토요일
8
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
class State{public: virtual ~State(){}
virtual void Enter(Miner*)=0;
virtual void Execute(Miner*)=0;
virtual void Exit(Miner*)=0;};
12년 3월 31일 토요일
9
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
void Miner::ChangeState(State* pNewState) {
assert(m_pCurrentState && pNewState);
m_pCurrentState->Exit(this);
m_pCurrentState = pNewState;
m_pCurrentState->Enter(this);}
12년 3월 31일 토요일
10
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
12년 3월 31일 토요일
11
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기class EnterMineAndDigForNugget : public State{private:
EnterMineAndDigForNugget(){}
public: static EnterMineAndDigForNugget* Instance(); virtual void Enter(Miner* miner); virtual void Execute(Miner* miner); virtual void Exit(Miner* miner);};
12년 3월 31일 토요일
12
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
void EnterMineAndDigForNugget::Enter(Miner* pMiner){ if (pMiner->Location() != goldmine) { SetTextColor(FOREGROUND_RED| FOREGROUND_INTENSITY); cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": " << "Walkin' to the goldmine";
pMiner->ChangeLocation(goldmine); }}
12년 3월 31일 토요일
13
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
void EnterMineAndDigForNugget::Execute(Miner* pMiner){ pMiner->AddToGoldCarried(1); pMiner->IncreaseFatigue();
SetTextColor(FOREGROUND_RED| FOREGROUND_INTENSITY); cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": " << "Pickin' up a nugget";
if (pMiner->PocketsFull()) { pMiner->ChangeState(VisitBankAndDepositGold::Instance()); }
if (pMiner->Thirsty()) { pMiner->ChangeState(QuenchThirst::Instance()); }}
12년 3월 31일 토요일
14
By Changhoon Park http://wawworld.me
상태디자인 패턴 자세히보기
void EnterMineAndDigForNugget::Exit(Miner* pMiner){ SetTextColor(FOREGROUND_RED| FOREGROUND_INTENSITY); cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": " << "Ah'm leavin' the goldmine with mah pockets full o' sweet gold";}
12년 3월 31일 토요일
15
By Changhoon Park http://wawworld.me
상태의 기본 클래스 재사용할 수 있게 만들기
12년 3월 31일 토요일
16
By Changhoon Park http://wawworld.me
Template 클래스
template <class entity_type>class State{public:
virtual void Enter(entity_type*)=0; virtual void Execute(entity_type*)=0; virtual void Exit(entity_type*)=0; virtual ~State(){}
};
12년 3월 31일 토요일
17
By Changhoon Park http://wawworld.me
Template 클래스
class EnterMineAndDigForNugget : public State<Miner>{
public: /* OMITTED */};
12년 3월 31일 토요일
18
By Changhoon Park http://wawworld.me
전역 상태 및 상태 블립
12년 3월 31일 토요일
19
By Changhoon Park http://wawworld.me
State<Miner>* m_pGlobalState;
class Miner : public BaseGameEntity{private: State<Miner>* m_pCurrentState; State<Miner>* m_pPreviousState; State<Miner>* m_pGlobalState; ... public: void ChangeState(State<Miner>* pNewState); void RevertToPreviousState(); ...};
12년 3월 31일 토요일
20
By Changhoon Park http://wawworld.me
상태기계 클래스 생성하기
12년 3월 31일 토요일
21
By Changhoon Park http://wawworld.me
template <class entity_type>class StateMachine{private: entity_type* m_pOwner;
State<entity_type>* m_pCurrentState; State<entity_type>* m_pPreviousState; State<entity_type>* m_pGlobalState; public: StateMachine(entity_type* owner):m_pOwner(owner), m_pCurrentState(NULL), m_pPreviousState(NULL), m_pGlobalState(NULL) {}
12년 3월 31일 토요일
22
By Changhoon Park http://wawworld.me
virtual ~StateMachine(){}
void SetCurrentState(State<entity_type>* s){m_pCurrentState = s;} void SetGlobalState(State<entity_type>* s) {m_pGlobalState = s;} void SetPreviousState(State<entity_type>* s){m_pPreviousState = s;}
void Update()const { if(m_pGlobalState) m_pGlobalState->Execute(m_pOwner); if (m_pCurrentState) m_pCurrentState->Execute(m_pOwner); }
12년 3월 31일 토요일
23
By Changhoon Park http://wawworld.me
void ChangeState(State<entity_type>* pNewState) { assert(pNewState && "<StateMachine::ChangeState>: trying to change to NULL state"); m_pPreviousState = m_pCurrentState; m_pCurrentState->Exit(m_pOwner); m_pCurrentState = pNewState; m_pCurrentState->Enter(m_pOwner); }
void RevertToPreviousState() { ChangeState(m_pPreviousState); }
12년 3월 31일 토요일
24
By Changhoon Park http://wawworld.me
bool isInState(const State<entity_type>& st)const { return typeid(*m_pCurrentState) == typeid(st); }
State<entity_type>* CurrentState() const{return m_pCurrentState;} State<entity_type>* GlobalState() const{return m_pGlobalState;} State<entity_type>* PreviousState() const{return m_pPreviousState;}};
12년 3월 31일 토요일
25
By Changhoon Park http://wawworld.me
class Miner : public BaseGameEntity{private:
StateMachine<Miner>* m_pStateMachine; /* EXTRANEOUS DETAIL OMITTED */
public:
Miner(int id):m_Location(shack), m_iGoldCarried(0), m_iMoneyInBank(0), m_iThirst(0), m_iFatigue(0), BaseGameEntity(id) { m_pStateMachine = new StateMachine<Miner>(this); m_pStateMachine->SetCurrentState(GoHomeAndSleepTilRested::Instance()); m_pStateMachine->SetGlobalState(MinerGloalState::Instance()); }
12년 3월 31일 토요일
26
By Changhoon Park http://wawworld.me
~Miner(){delete m_pStateMachine;}
void Update() { ++m_iThirst; m_pStateMachine->Update(); }
StateMachine<Miner>* GetFSM()const{return m_pStateMachine;} /* EXTRANEOUS DETAIL O<ITTED */};
12년 3월 31일 토요일