From 37b6e30f4e121360cb331ab259d2c3ca10689ad0 Mon Sep 17 00:00:00 2001 From: Flekz Date: Mon, 22 Jan 2024 16:38:15 +0000 Subject: [PATCH] Make the playerbotai and playerbotmgr unique ptr --- src/game/Entities/CharacterHandler.cpp | 12 ++++---- src/game/Entities/Player.cpp | 41 +++++++++++++++++--------- src/game/Entities/Player.h | 23 +++++++-------- 3 files changed, 43 insertions(+), 33 deletions(-) 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 c8dbeae960..f576cfa644 100644 --- a/src/game/Entities/Player.cpp +++ b/src/game/Entities/Player.cpp @@ -676,8 +676,8 @@ Player::Player(WorldSession* session): Unit(), m_taxiTracker(*this), m_mover(thi m_fishingSteps = 0; #ifdef ENABLE_PLAYERBOTS - m_playerbotAI = NULL; - m_playerbotMgr = NULL; + m_playerbotAI = nullptr; + m_playerbotMgr = nullptr; #endif } @@ -725,18 +725,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 @@ -1681,12 +1671,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 7d6b773f2d..8324b03259 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(); @@ -2281,14 +2277,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 @@ -2604,8 +2603,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