Skip to content

Commit

Permalink
Implement GameObjectAI
Browse files Browse the repository at this point in the history
Also on wotlk changed NULL in file to nullptr
  • Loading branch information
killerwife committed Oct 10, 2017
1 parent 8445563 commit fe88336
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/game/AI/BaseAI/GameObjectAI.cpp
@@ -0,0 +1,12 @@
#include "AI/BaseAI/GameObjectAI.h"
#include "Entities/GameObject.h"


GameObjectAI::GameObjectAI(GameObject* go) : m_go(go)
{
}


GameObjectAI::~GameObjectAI()
{
}
21 changes: 21 additions & 0 deletions src/game/AI/BaseAI/GameObjectAI.h
@@ -0,0 +1,21 @@
#ifndef DEF_GAMEOBJECT_AI_H
#define DEF_GAMEOBJECT_AI_H

#include "Platform/Define.h"

class GameObject;

class GameObjectAI
{
public:
explicit GameObjectAI(GameObject* go);
virtual ~GameObjectAI();

virtual void UpdateAI(const uint32 /*diff*/) {}

protected:
GameObject* m_go;
};

#endif

10 changes: 10 additions & 0 deletions src/game/AI/ScriptDevAI/ScriptDevAIMgr.cpp
Expand Up @@ -333,6 +333,16 @@ CreatureAI* ScriptDevAIMgr::GetCreatureAI(Creature* pCreature)
return pTempScript->GetAI(pCreature);
}

GameObjectAI * ScriptDevAIMgr::GetGameObjectAI(GameObject * gameobject)
{
Script* pTempScript = GetScript(gameobject->GetScriptId());

if (!pTempScript || !pTempScript->GetGameObjectAI)
return nullptr;

return pTempScript->GetGameObjectAI(gameobject);
}

bool ScriptDevAIMgr::OnItemUse(Player* pPlayer, Item* pItem, SpellCastTargets const& targets)
{
Script* pTempScript = GetScript(pItem->GetProto()->ScriptId);
Expand Down
19 changes: 11 additions & 8 deletions src/game/AI/ScriptDevAI/ScriptDevAIMgr.h
Expand Up @@ -23,6 +23,7 @@ class WorldObject;
class Aura;
class Object;
class ObjectGuid;
class GameObjectAI;

// *********************************************************
// ************** Some defines used globally ***************
Expand Down Expand Up @@ -63,14 +64,14 @@ enum EscortFaction
struct Script
{
Script() :
pGossipHello(NULL), pGossipHelloGO(NULL), pGossipSelect(NULL), pGossipSelectGO(NULL),
pGossipSelectWithCode(NULL), pGossipSelectGOWithCode(NULL),
pDialogStatusNPC(NULL), pDialogStatusGO(NULL),
pQuestAcceptNPC(NULL), pQuestAcceptGO(NULL), pQuestAcceptItem(NULL),
pQuestRewardedNPC(NULL), pQuestRewardedGO(NULL),
pGOUse(NULL), pItemUse(NULL), pAreaTrigger(NULL), pNpcSpellClick(NULL), pProcessEventId(NULL),
pEffectDummyNPC(NULL), pEffectDummyGO(NULL), pEffectDummyItem(NULL), pEffectScriptEffectNPC(NULL),
pEffectAuraDummy(NULL), GetAI(NULL), GetInstanceData(NULL)
pGossipHello(nullptr), pGossipHelloGO(nullptr), pGossipSelect(nullptr), pGossipSelectGO(nullptr),
pGossipSelectWithCode(nullptr), pGossipSelectGOWithCode(nullptr),
pDialogStatusNPC(nullptr), pDialogStatusGO(nullptr),
pQuestAcceptNPC(nullptr), pQuestAcceptGO(nullptr), pQuestAcceptItem(nullptr),
pQuestRewardedNPC(nullptr), pQuestRewardedGO(nullptr),
pGOUse(nullptr), pItemUse(nullptr), pAreaTrigger(nullptr), pNpcSpellClick(nullptr), pProcessEventId(nullptr),
pEffectDummyNPC(nullptr), pEffectDummyGO(nullptr), pEffectDummyItem(nullptr), pEffectScriptEffectNPC(nullptr),
pEffectAuraDummy(nullptr), GetGameObjectAI(nullptr), GetAI(nullptr), GetInstanceData(nullptr)
{}

std::string Name;
Expand Down Expand Up @@ -99,6 +100,7 @@ struct Script
bool (*pEffectScriptEffectNPC)(Unit*, uint32, SpellEffectIndex, Creature*, ObjectGuid);
bool (*pEffectAuraDummy)(const Aura*, bool);

GameObjectAI* (*GetGameObjectAI)(GameObject*);
CreatureAI* (*GetAI)(Creature*);
InstanceData* (*GetInstanceData)(Map*);

Expand Down Expand Up @@ -145,6 +147,7 @@ class ScriptDevAIMgr
uint32 GetScriptIdsCount() const { return m_scriptNames.size(); }

CreatureAI* GetCreatureAI(Creature* creature);
GameObjectAI* GetGameObjectAI(GameObject* gameobject);

InstanceData* CreateInstanceData(Map* pMap);
uint32 GetAreaTriggerScriptId(uint32 triggerId) const;
Expand Down
16 changes: 15 additions & 1 deletion src/game/Entities/GameObject.cpp
Expand Up @@ -47,7 +47,8 @@ GameObject::GameObject() : WorldObject(),
m_captureSlider(0),
m_captureState(),
m_goInfo(nullptr),
m_displayInfo(nullptr)
m_displayInfo(nullptr),
m_AI(nullptr)
{
m_objectType |= TYPEMASK_GAMEOBJECT;
m_objectTypeId = TYPEID_GAMEOBJECT;
Expand Down Expand Up @@ -543,6 +544,9 @@ void GameObject::Update(uint32 update_diff, uint32 p_time)
break;
}
}

if (m_AI)
m_AI->UpdateAI(update_diff);
}

void GameObject::Refresh()
Expand Down Expand Up @@ -2470,3 +2474,13 @@ void GameObject::SetInUse(bool use)
else
SetGoState(GO_STATE_READY);
}

uint32 GameObject::GetScriptId() const
{
return ObjectMgr::GetGameObjectInfo(GetEntry())->ScriptId;
}

void GameObject::AIM_Initialize()
{
m_AI.reset(sScriptDevAIMgr.GetGameObjectAI(this));
}
6 changes: 6 additions & 0 deletions src/game/Entities/GameObject.h
Expand Up @@ -23,6 +23,7 @@
#include "Globals/SharedDefines.h"
#include "Entities/Object.h"
#include "Util.h"
#include "AI/BaseAI/GameObjectAI.h"

// GCC have alternative #pragma pack(N) syntax and old gcc version not support pack(push,N), also any gcc version not support it at some platform
#if defined( __GNUC__ )
Expand Down Expand Up @@ -796,6 +797,9 @@ class GameObject : public WorldObject

GridReference<GameObject>& GetGridRef() { return m_gridRef; }

uint32 GetScriptId() const;
void AIM_Initialize();

GameObjectModel* m_model;

protected:
Expand Down Expand Up @@ -833,6 +837,8 @@ class GameObject : public WorldObject
time_t m_reStockTimer; // timer to refill the chest
time_t m_despawnTimer; // timer to despawn the chest if something changed in it

std::unique_ptr<GameObjectAI> m_AI;

private:
void SwitchDoorOrButton(bool activate, bool alternative = false);
void TickCapturePoint();
Expand Down

0 comments on commit fe88336

Please sign in to comment.