diff --git a/src/game/Entities/CharacterHandler.cpp b/src/game/Entities/CharacterHandler.cpp index 23d022b71c..f7a801961e 100644 --- a/src/game/Entities/CharacterHandler.cpp +++ b/src/game/Entities/CharacterHandler.cpp @@ -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; @@ -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); } @@ -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)) diff --git a/src/game/Entities/Player.cpp b/src/game/Entities/Player.cpp index 72b706cebe..b08826c82e 100644 --- a/src/game/Entities/Player.cpp +++ b/src/game/Entities/Player.cpp @@ -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 } @@ -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 @@ -1684,12 +1674,35 @@ void Player::Update(const uint32 diff) } #ifdef ENABLE_PLAYERBOTS +void Player::CreatePlayerbotAI() +{ + assert(!m_playerbotAI); + m_playerbotAI = std::make_unique(this); +} + +void Player::RemovePlayerbotAI() +{ + m_playerbotAI = nullptr; +} + +void Player::CreatePlayerbotMgr() +{ + assert(!m_playerbotMgr); + m_playerbotMgr = std::make_unique(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); diff --git a/src/game/Entities/Player.h b/src/game/Entities/Player.h index 6ea8a51644..bcec9b9b9b 100644 --- a/src/game/Entities/Player.h +++ b/src/game/Entities/Player.h @@ -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(); @@ -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 @@ -2612,8 +2611,8 @@ class Player : public Unit #endif #ifdef ENABLE_PLAYERBOTS - PlayerbotAI* m_playerbotAI; - PlayerbotMgr* m_playerbotMgr; + std::unique_ptr m_playerbotAI; + std::unique_ptr m_playerbotMgr; #endif #ifdef USE_ACHIEVEMENTS