Game ai.fsm.02

28
상태구동형 에이전트의 디자인 By Changhoon Park http://www.wawworld.me Last Update : 2012. 03. 03 게임 인공지능 GameAI 12년 3월 31일 토요일

Transcript of Game ai.fsm.02

Page 1: Game ai.fsm.02

상태구동형 에이전트의 디자인By Changhoon Parkhttp://www.wawworld.me

Last Update : 2012. 03. 03

게임 인공지능GameAI

12년 3월 31일 토요일

Page 2: Game ai.fsm.02

2

By Changhoon Park http://wawworld.me

West World 프로젝트

12년 3월 31일 토요일

Page 3: Game ai.fsm.02

3

By Changhoon Park http://wawworld.me

데모

12년 3월 31일 토요일

Page 4: Game ai.fsm.02

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일 토요일

Page 5: Game ai.fsm.02

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일 토요일

Page 6: Game ai.fsm.02

6

By Changhoon Park http://wawworld.me

Minor 클래스

void Miner::Update() { m_iThirst += 1;

if (m_pCurrentState) { m_pCurrentState->Execute(this); }}

12년 3월 31일 토요일

Page 7: Game ai.fsm.02

7

By Changhoon Park http://wawworld.me

Minor의 상태들

12년 3월 31일 토요일

Page 8: Game ai.fsm.02

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일 토요일

Page 9: Game ai.fsm.02

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일 토요일

Page 10: Game ai.fsm.02

10

By Changhoon Park http://wawworld.me

상태디자인 패턴 자세히보기

12년 3월 31일 토요일

Page 11: Game ai.fsm.02

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일 토요일

Page 12: Game ai.fsm.02

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일 토요일

Page 13: Game ai.fsm.02

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일 토요일

Page 14: Game ai.fsm.02

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일 토요일

Page 15: Game ai.fsm.02

15

By Changhoon Park http://wawworld.me

상태의 기본 클래스 재사용할 수 있게 만들기

12년 3월 31일 토요일

Page 16: Game ai.fsm.02

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일 토요일

Page 17: Game ai.fsm.02

17

By Changhoon Park http://wawworld.me

Template 클래스

class EnterMineAndDigForNugget : public State<Miner>{

public: /* OMITTED */};

12년 3월 31일 토요일

Page 18: Game ai.fsm.02

18

By Changhoon Park http://wawworld.me

전역 상태 및 상태 블립

12년 3월 31일 토요일

Page 19: Game ai.fsm.02

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일 토요일

Page 20: Game ai.fsm.02

20

By Changhoon Park http://wawworld.me

상태기계 클래스 생성하기

12년 3월 31일 토요일

Page 21: Game ai.fsm.02

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일 토요일

Page 22: Game ai.fsm.02

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일 토요일

Page 23: Game ai.fsm.02

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일 토요일

Page 24: Game ai.fsm.02

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일 토요일

Page 25: Game ai.fsm.02

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일 토요일

Page 26: Game ai.fsm.02

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일 토요일

Page 27: Game ai.fsm.02

27

By Changhoon Park http://wawworld.me

12년 3월 31일 토요일

Page 28: Game ai.fsm.02

28

By Changhoon Park http://wawworld.me

12년 3월 31일 토요일