Skip to content

Commit

Permalink
Make the playerbotai and playerbotmgr unique ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
davidonete authored and celguar committed Jan 23, 2024
1 parent cd22cb1 commit 4773c57
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
12 changes: 5 additions & 7 deletions src/game/Entities/CharacterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ void PlayerbotHolder::HandlePlayerBotLoginCallback(QueryResult * dummy, SqlQuery
sLog.outError("Error logging in bot %d, please try to reset all random bots", guid);
return;
}
PlayerbotMgr *mgr = bot->GetPlayerbotMgr();
bot->SetPlayerbotMgr(NULL);
delete mgr;

bot->RemovePlayerbotMgr();
sRandomPlayerbotMgr.OnPlayerLogin(bot);

bool allowed = false;
Expand Down Expand Up @@ -249,7 +248,7 @@ class CharacterHandler
Player* player = session->GetPlayer();
if (player)
{
player->SetPlayerbotMgr(new PlayerbotMgr(player));
player->CreatePlayerbotMgr();
player->GetPlayerbotMgr()->OnPlayerLogin(player);
sRandomPlayerbotMgr.OnPlayerLogin(player);
}
Expand Down Expand Up @@ -623,9 +622,8 @@ void WorldSession::HandlePlayerLoginOpcode(WorldPacket& recv_data)
PlayerbotMgr* mgr = _player->GetPlayerbotMgr();
if (!mgr || mgr->GetMaster() != _player)
{
_player->SetPlayerbotMgr(NULL);
delete mgr;
_player->SetPlayerbotMgr(new PlayerbotMgr(_player));
_player->RemovePlayerbotMgr();
_player->CreatePlayerbotMgr();
_player->GetPlayerbotMgr()->OnPlayerLogin(_player);

if (sRandomPlayerbotMgr.GetPlayerBot(playerGuid))
Expand Down
41 changes: 27 additions & 14 deletions src/game/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ Player::Player(WorldSession* session): Unit(), m_taxiTracker(*this), m_mover(thi
m_lastGameObject = false;

#ifdef ENABLE_PLAYERBOTS
m_playerbotAI = NULL;
m_playerbotMgr = NULL;
m_playerbotAI = nullptr;
m_playerbotMgr = nullptr;
#endif
}

Expand Down Expand Up @@ -728,18 +728,8 @@ Player::~Player()
#endif

#ifdef ENABLE_PLAYERBOTS
if (m_playerbotAI) {
{
delete m_playerbotAI;
}
m_playerbotAI = 0;
}
if (m_playerbotMgr) {
{
delete m_playerbotMgr;
}
m_playerbotMgr = 0;
}
RemovePlayerbotAI();
RemovePlayerbotMgr();
#endif

#ifdef USE_ACHIEVEMENTS
Expand Down Expand Up @@ -1684,12 +1674,35 @@ void Player::Update(const uint32 diff)
}

#ifdef ENABLE_PLAYERBOTS
void Player::CreatePlayerbotAI()
{
assert(!m_playerbotAI);
m_playerbotAI = std::make_unique<PlayerbotAI>(this);
}

void Player::RemovePlayerbotAI()
{
m_playerbotAI = nullptr;
}

void Player::CreatePlayerbotMgr()
{
assert(!m_playerbotMgr);
m_playerbotMgr = std::make_unique<PlayerbotMgr>(this);
}

void Player::RemovePlayerbotMgr()
{
m_playerbotMgr = nullptr;
}

void Player::UpdateAI(const uint32 diff, bool minimal)
{
if (m_playerbotAI)
{
m_playerbotAI->UpdateAI(diff);
}

if (m_playerbotMgr)
{
m_playerbotMgr->UpdateAI(diff);
Expand Down
23 changes: 11 additions & 12 deletions src/game/Entities/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,6 @@ class Player : public Unit
void Update(const uint32 diff) override;
void Heartbeat() override;

#ifdef ENABLE_PLAYERBOTS
void UpdateAI(const uint32 diff, bool minimal = false);
#endif

static bool BuildEnumData(QueryResult* result, WorldPacket& p_data);

void SendInitialPacketsBeforeAddToMap();
Expand Down Expand Up @@ -2286,14 +2282,17 @@ class Player : public Unit
#endif

#ifdef ENABLE_PLAYERBOTS
//EquipmentSets& GetEquipmentSets() { return m_EquipmentSets; }
void SetPlayerbotAI(PlayerbotAI* ai) { assert(!m_playerbotAI && !m_playerbotMgr); m_playerbotAI = ai; }
PlayerbotAI* GetPlayerbotAI() { return m_playerbotAI; }
void SetPlayerbotMgr(PlayerbotMgr* mgr) { assert(!m_playerbotAI && !m_playerbotMgr); m_playerbotMgr = mgr; }
PlayerbotMgr* GetPlayerbotMgr() { return m_playerbotMgr; }
// A Player can either have a playerbotMgr (to manage its bots), or have playerbotAI (if it is a bot), or
// neither. Code that enables bots must create the playerbotMgr and set it using SetPlayerbotMgr.
void UpdateAI(const uint32 diff, bool minimal = false);
void CreatePlayerbotAI();
void RemovePlayerbotAI();
PlayerbotAI* GetPlayerbotAI() { return m_playerbotAI.get(); }
void CreatePlayerbotMgr();
void RemovePlayerbotMgr();
PlayerbotMgr* GetPlayerbotMgr() { return m_playerbotMgr.get(); }
void SetBotDeathTimer() { m_deathTimer = 0; }
bool isRealPlayer() { return m_session && (m_session->GetRemoteAddress() != "disconnected/bot"); }
//PlayerTalentMap& GetTalentMap(uint8 spec) { return m_talents[spec]; }
#endif

#ifdef USE_ACHIEVEMENTS
Expand Down Expand Up @@ -2612,8 +2611,8 @@ class Player : public Unit
#endif

#ifdef ENABLE_PLAYERBOTS
PlayerbotAI* m_playerbotAI;
PlayerbotMgr* m_playerbotMgr;
std::unique_ptr<PlayerbotAI> m_playerbotAI;
std::unique_ptr<PlayerbotMgr> m_playerbotMgr;
#endif

#ifdef USE_ACHIEVEMENTS
Expand Down

0 comments on commit 4773c57

Please sign in to comment.