1. 게임 인공지능
GameAI
상태구동형 에이전트의 디자인
By Changhoon Park
http://www.wawworld.me
Last Update : 2012. 03. 03
12년 3월 31일 토요일
2. West World 프로젝트
2
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
3. 데모
3
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
4. 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;
4
int ID()const{return m_ID;}
};
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
5. 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);
5 }
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
6. Minor 클래스
void Miner::Update()
{
m_iThirst += 1;
if (m_pCurrentState)
{
m_pCurrentState->Execute(this);
}
}
6
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
7. Minor의 상태들
7
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
8. 상태디자인 패턴 자세히보기
class State
{
public:
virtual ~State(){}
virtual void Enter(Miner*)=0;
virtual void Execute(Miner*)=0;
virtual void Exit(Miner*)=0;
};
8
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
9. 상태디자인 패턴 자세히보기
void Miner::ChangeState(State* pNewState) {
assert(m_pCurrentState && pNewState);
m_pCurrentState->Exit(this);
m_pCurrentState = pNewState;
m_pCurrentState->Enter(this);
}
9
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
10. 상태디자인 패턴 자세히보기
10
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
11. 상태디자인 패턴 자세히보기
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);
};
11
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
12. 상태디자인 패턴 자세히보기
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
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
13. 상태디자인 패턴 자세히보기
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()) {
13 pMiner->ChangeState(QuenchThirst::Instance());
}
}
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
14. 상태디자인 패턴 자세히보기
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";
}
14
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
15. 상태의 기본 클래스 재사용할 수
있게 만들기
15
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
16. 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(){}
};
16
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
17. Template 클래스
class EnterMineAndDigForNugget : public State<Miner>
{
public:
/* OMITTED */
};
17
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
18. 전역 상태 및 상태 블립
18
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
19. 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();
...
};
19
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
20. 상태기계 클래스 생성하기
20
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
21. 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)
{}
21
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
22. 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);
}
22
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
23. 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);
}
23
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
24. 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;}
};
24
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
25. 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());
}
25
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
26. ~Miner(){delete m_pStateMachine;}
void Update()
{
++m_iThirst;
m_pStateMachine->Update();
}
StateMachine<Miner>* GetFSM()const{return m_pStateMachine;}
/* EXTRANEOUS DETAIL O<ITTED */
};
26
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
27. 27
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일
28. 28
By Changhoon Park
http://wawworld.me
12년 3월 31일 토요일