1. 게임 인공지능
GameAI
Raven : 개관
By Changhoon Park
http://wawworld.me
Last Update : 2011. 08. 28
11년 10월 10일 월요일
2. 2
Raven 게임
개관
AI
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
3. 3
Raven 게임
개관
AI
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
4. class Raven_Game
{
private:
Raven_Map* m_pMap;
std::list<Raven_Bot*> m_Bots;
//the user may select a bot to control manually. This member holds a pointer to that bot
Raven_Bot* m_pSelectedBot;
//this list contains any active projectiles (slugs, rockets, shotgun pellets, etc.)
std::list<Raven_Projectile*> m_Projectiles;
/* EXTRANEOUS DETAIL OMITTED FOR CLARITY */
4
Raven 게임 Raven_Game
개관 맵
AI 무기
탄환
트리거
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
5. public:
void Render();
void Update();
bool LoadMap(const std::string& FileName);
bool isPathObstructed(Vector2D A, Vector2D B, double BoundingRadius = 0)const;
std::vector<Raven_Bot*> GetAllBotsInFOV(const Raven_Bot* pBot)const;
bool isSecondVisibleToFirst(const Raven_Bot* pFirst,
const Raven_Bot* pSecond)const;
/* EXTRANEOUS DETAIL OMITTED FOR CLARITY */
};
5
Raven 게임 Raven_Game
개관 맵
AI 무기
탄환
트리거
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
6. class Raven_Map
{
public:
typedef NavGraphNode<GraphEdge, Trigger<Raven_Bot>*> GraphNode;
typedef SparseGraph<GraphNode> NavGraph;
typedef TriggerSystem<Trigger<Raven_Bot> > Trigger_System;
private:
std::vector<Wall2D*> m_Walls;
Trigger_System m_TriggerSystem;
std::vector<Vector2D> m_SpawnPoints;
NavGraph* m_pNavGraph;
6
Raven 게임 Raven_Game 자각
개관 맵 목표선택
AI 무기 무기 다루기
탄환 AI 갱신
트리거
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
7. public:
Raven_Map();
~Raven_Map();
void Render();
bool LoadMap(const std::string& FileName);
void AddSoundTrigger(Raven_Bot* pSoundSource, double range);
double CalculateCostToTravelBetweenNodes(unsigned int nd1,
unsigned int nd2)const;
void UpdateTriggerSystem(std::list<Raven_Bot*>& bots);
/* EXTRANEOUS DETAIL OMITTED */
};
7
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
8. 8
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
9. class Raven_Weapon
{
public:
Raven_Weapon(unsigned int TypeOfGun
unsigned int DefaultNumRounds,
unsigned int MaxRoundsCarried,
double RateOfFire,
double IdealRange,
double ProjectileSpeed,
Raven_Bot* OwnerOfGun);
virtual ~Raven_Weapon(){}
bool AimAt(Vector2D target)const;
virtual void ShootAt(Vector2D target) = 0;
9
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
10. //this method returns a value representing the desirability of using the
//weapon. This is used by the AI to select the most suitable weapon for
//a bot's current situation. This value is calculated using fuzzy logic.
//(Fuzzy logic is covered in Chapter 10)
virtual double GetDesirability(double DistToTarget)=0;
//returns the maximum speed of the projectile this weapon fires
double GetProjectileSpeed()const;
int NumRoundsRemaining()const;
void DecrementNumRounds();
void IncrementRounds(int num);
//returns an enumerated value representing the gun type
unsigned int GetTypeOfGun()const;
};
10
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
11. 11
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
12. 12
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
13. class TriggerRegion
{
public:
virtual ~TriggerRegion(){}
virtual bool isTouching(Vector2D EntityPos, double EntityRadius)const = 0;
};
13
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
14. class TriggerRegion_Circle : public TriggerRegion
{
private:
Vector2D m_vPos; //the center of the region
double m_dRadius; //the radius of the region
public:
TriggerRegion_Circle(Vector2D pos, double radius)
:m_dRadius(radius), m_vPos(pos){}
bool isTouching(Vector2D pos, double EntityRadius)const
{
//distances calculated in squared-distance space
return Vec2DDistanceSq(m_vPos, pos) <
(EntityRadius + m_dRadius)*(EntityRadius + m_dRadius);
}
};
14
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
15. template <class entity_type>
class Trigger : public BaseGameEntity
{
private:
//Every trigger owns a trigger region. If an entity comes within this region the trigger is activated
TriggerRegion* m_pRegionOfInfluence;
//if this is true the trigger will be removed from the game on the next update
bool m_bRemoveFromGame;
bool m_bActive;
// some types of triggers are twinned with a graph node. This enables the pathfinding component
// of an AI to search a navgraph for a specific type of trigger.
int m_iGraphNodeIndex;
15
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
16. protected:
void SetGraphNodeIndex(int idx){m_iGraphNodeIndex = idx;}
void SetToBeRemovedFromGame(){m_bRemoveFromGame = true;}
void SetInactive(){m_bActive = false;}
void SetActive(){m_bActive = true;}
//returns true if the entity given by a position and bounding radius is
//overlapping the trigger region
bool isTouchingTrigger(Vector2D EntityPos, double EntityRadius)const;
//child classes use one of these methods to add a trigger region
void AddCircularTriggerRegion(Vector2D center, double radius);
void AddRectangularTriggerRegion(Vector2D TopLeft, Vector2D BottomRight);
16
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
17. public:
Trigger(unsigned int id);
virtual ~Trigger();
// when this is called the trigger determines if the entity is within the trigger's region of influence.
// If it is then the trigger will be triggered and the appropriate action will be taken.
virtual void Try(entity_type*) = 0;
//called each update step of the game. This method updates any internal
//state the trigger may have
virtual void Update() = 0;
int GraphNodeIndex()const{return m_iGraphNodeIndex;}
bool isToBeRemoved()const{return m_bRemoveFromGame;}
bool isActive(){return m_bActive;}
};
17
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
18. template <class entity_type>
class Trigger_Respawning : public Trigger<entity_type>
{
protected:
// When a bot comes within this trigger's area of influence it is triggered but then
// becomes inactive for a specified amount of time. These values control
// the amount of time required to pass before the trigger becomes active once more.
int m_iNumUpdatesBetweenRespawns;
int m_iNumUpdatesRemainingUntilRespawn;
void Deactivate()
{
SetInactive();
m_iNumUpdatesRemainingUntilRespawn = m_iNumUpdatesBetweenRespawns;
}
18
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
19. public:
Trigger_Respawning(int id);
virtual ~Trigger_Respawning();
virtual void Try(entity_type*) = 0; //to be implemented by child classes
virtual void Update() //this is called each game-tick to update the trigger's internal state
{
if ( (--m_iNumUpdatesRemainingUntilRespawn <= 0) && !isActive())
{
SetActive();
}
}
void SetRespawnDelay(unsigned int numTicks);
};
19
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
20. class Trigger_WeaponGiver : public Trigger_Respawning<Raven_Bot>
{
private:
/* EXTRANEOUS DETAIL OMITTED */
public:
//this type of trigger is created when reading a map file
Trigger_WeaponGiver(std::ifstream& datafile);
//if triggered, this trigger will call the PickupWeapon method of the
//bot. PickupWeapon will instantiate a weapon of the appropriate type.
void Try(Raven_Bot*);
//draws a symbol representing the weapon type at the trigger’s location
void Render();
};
20
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
21. void Trigger_WeaponGiver::Try(Raven_Bot* pBot)
{
if (isActive() && isTouchingTrigger(pBot->Pos(), pBot->BRadius()))
{
pBot->PickupWeapon( EntityType() );
Deactivate();
}
}
void Trigger_HealthGiver::Try(Raven_Bot* pBot)
{
if (isActive() && isTouchingTrigger(pBot->Pos(), pBot->BRadius()))
{
pBot->IncreaseHealth(m_iHealthGiven);
Deactivate();
}
}
21
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
22. template <class entity_type>
class Trigger_LimitedLifetime : public Trigger<entity_type>
{
protected:
int m_iLifetime; //the lifetime of this trigger in update steps
public:
Trigger_LimitedLifetime(int lifetime);
virtual ~Trigger_LimitedLifetime(){}
//children of this class should always make sure this is called from within their own update method
virtual void Update()
{
//if the lifetime counter expires set this trigger to be removed from the game
if (--m_iLifetime <= 0) {
SetToBeRemovedFromGame();
}
}
virtual void Try(entity_type*) = 0; //to be implemented by child classes
};
22
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
23. class Trigger_SoundNotify : public Trigger_LimitedLifetime<Raven_Bot>
{
private:
//a pointer to the bot that has made this sound
Raven_Bot* m_pSoundSource;
public:
Trigger_SoundNotify(Raven_Bot* source, double range);
void Trigger_SoundNotify::Try(Raven_Bot* pBot)
{
//is this bot within range of this sound
if (isTouchingTrigger(pBot->Pos(), pBot->BRadius()))
{
Dispatcher->DispatchMsg(SEND_MSG_IMMEDIATELY,
SENDER_ID_IRRELEVANT, pBot->ID(),
Msg_GunshotSound,
m_pSoundSource);
}
}
};
23
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
24. template <class trigger_type>
class TriggerSystem
{
public:
typedef std::list<trigger_type*> TriggerList;
private:
//a container of all the triggers
TriggerList m_Triggers;
//this method iterates through all the triggers present in the system and
//calls their Update method in order that their internal state can be
//updated if necessary. It also removes any triggers from the system that
//have their m_bRemoveFromGame field set to true.
24
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
25. void UpdateTriggers()
{
TriggerList::iterator curTrg = m_Triggers.begin();
while (curTrg != m_Triggers.end())
{
//remove trigger if dead
if ((*curTrg)->isToBeRemoved())
{
delete *curTrg;
curTrg = m_Triggers.erase(curTrg);
}
else
{
//update this trigger
(*curTrg)->Update();
++curTrg;
}
}
}
25
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
26. //this method iterates through the container of entities passed as a parameter and passes each one to
// the Try method of each trigger provided the entity is alive and is ready for a trigger update.
template <class ContainerOfEntities>
void TryTriggers(ContainerOfEntities& entities)
{
ContainerOfEntities::iterator curEnt = entities.begin(); //test each entity against the triggers
for (curEnt; curEnt != entities.end(); ++curEnt)
{
//an entity must be ready for its next trigger update and it must be
//alive before it is tested against each trigger.
if ((*curEnt)->isReadyForTriggerUpdate() && (*curEnt)->isAlive())
{
TriggerList::const_iterator curTrg;
for (curTrg = m_Triggers.begin(); curTrg != m_Triggers.end(); ++curTrg) {
(*curTrg)->Try(*curEnt);
}
}
}
}
26
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
27. public:
~TriggerSystem() {
Clear();
}
void Clear(); //this deletes any current triggers and empties the trigger list
// This method should be called each update step of the game. It will first update the internal state of t
// he triggers and then try each entity against each active trigger to test if any should be triggered.
template <class ContainerOfEntities>
void Update(ContainerOfEntities& entities) {
UpdateTriggers();
TryTriggers(entities);
}
// this is used to register triggers with the TriggerSystem (the TriggerSystem /will take care of tidying
// up memory used by a trigger)
void Register(trigger_type* trigger);
//some triggers are required to be rendered (like giver-triggers for example)
void Render();
const TriggerList& GetTriggers()const{return m_Triggers;}
};
27
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
28. 28
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
29. 29
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
30. struct MemoryRecord
{
=
double dTimeLastSensed;
double dTimeBecameVisible;
double dTimeLastVisible;
Vector2D vLastSensedPosition;
bool bWithinFOV;
bool bShootable;
};
30
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
31. public:
Raven_SensoryMemory(Raven_Bot* owner, double MemorySpan);
//this method is used to update the memory map whenever an opponent makes a noise
void UpdateWithSoundSource(Raven_Bot* pNoiseMaker);
//this method iterates through all the opponents in the game world and updates the records of those that are in the owner's FOV
void UpdateVision();
bool isOpponentShootable(Raven_Bot* pOpponent)const;
bool isOpponentWithinFOV(Raven_Bot* pOpponent)const;
Vector2D GetLastRecordedPositionOfOpponent(Raven_Bot* pOpponent)const;
double GetTimeOpponentHasBeenVisible(Raven_Bot* pOpponent)const;
double GetTimeSinceLastSensed(Raven_Bot* pOpponent)const;
double GetTimeOpponentHasBeenOutOfView(Raven_Bot* pOpponent)const;
//this method returns a list of all the opponents that have had their /records updated within the last m_dMemorySpan seconds.
std::list<Raven_Bot*> GetListOfRecentlySensedOpponents()const;
};
31
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
32. class Raven_TargetingSystem
{
private:
//the owner of this system
Raven_Bot* m_pOwner;
//the current target (this will be null if there is no target assigned)
Raven_Bot* m_pCurrentTarget;
public:
Raven_TargetingSystem(Raven_Bot* owner);
void Update();
//returns true if there is a currently assigned target
bool isTargetPresent()const;
32
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
33. bool isTargetWithinFOV()const;
bool isTargetShootable()const;
Vector2D GetLastRecordedPosition()const;
double GetTimeTargetHasBeenVisible()const;
double GetTimeTargetHasBeenOutOfView()const;
Raven_Bot* GetTarget()const;
void ClearTarget();
};
33
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
34. class Regulator
{
private:
//the time period between updates
double m_dUpdatePeriod;
//the next time the regulator allows code flow
DWORD m_dwNextUpdateTime;
public:
Regulator(double NumUpdatesPerSecondRqd);
//returns true if the current time exceeds m_dwNextUpdateTime
bool isReady();
};
34
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
35. void Raven_Bot::Update()
{
m_pBrain->Process();
UpdateMovement(); //Calculate the steering force and update the bot's velocity and position
if (!isPossessed()) //if the bot is under AI control
{
//update the sensory memory with any visual stimulus
if (m_pVisionUpdateRegulator->isReady()) {
m_pSensoryMem->UpdateVision();
}
//examine all the opponents in the bot's sensory memory and select one to be the current target
if (m_pTargetSelectionRegulator->isReady()) {
m_pTargSys->Update();
}
35
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
36. //appraise and arbitrate between all possible high-level goals
if (m_pGoalArbitrationRegulator->isReady())
{
m_pBrain->Arbitrate();
}
//select the appropriate weapon to use from the weapons currently in the inventory
if (m_pWeaponSelectionRegulator->isReady())
{
m_pWeaponSys->SelectWeapon();
}
//this method aims the bot's current weapon at the current target
//and takes a shot if a shot is possible
m_pWeaponSys->TakeAimAndShoot();
}
}
36
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
37. 37
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일
38. void Trigger_WeaponGiver::Try(Raven_Bot* pBot)
{
if (isActive() && isTouchingTrigger(pBot->Pos(), pBot->BRadius()))
{
pBot->PickupWeapon( EntityType() );
Deactivate();
}
}
void Trigger_HealthGiver::Try(Raven_Bot* pBot)
{
if (isActive() && isTouchingTrigger(pBot->Pos(), pBot->BRadius()))
{
pBot->IncreaseHealth(m_iHealthGiven);
Deactivate();
}
}
38
그래프 탐색 알고리즘 무정보 그래프 탐색 에지완화
비용 기반 그래프 탐색 Dijkstra 알고리즘
A*
By Changhoon Park
http://wawworld.me
11년 10월 10일 월요일