From e571694a880c79ef9fa906125161ab56dbb322b4 Mon Sep 17 00:00:00 2001 From: Yunfan Li <56597220+liyunfan1223@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:06:29 +0800 Subject: [PATCH] Reset added rndbots on server restart to automatically apply configuration changes (#913) * Refactor RandomPlayerbotMgr and delete add value on restart server * Remove reset tips in conf file --- conf/playerbots.conf.dist | 3 --- src/PlayerbotAIConfig.cpp | 9 +++++---- src/RandomPlayerbotMgr.cpp | 17 ++++++++++++++++- src/RandomPlayerbotMgr.h | 3 ++- src/factory/PlayerbotFactory.cpp | 7 ++++--- src/factory/PlayerbotFactory.h | 2 +- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/conf/playerbots.conf.dist b/conf/playerbots.conf.dist index 7811986f..b02b50c1 100644 --- a/conf/playerbots.conf.dist +++ b/conf/playerbots.conf.dist @@ -507,7 +507,6 @@ AiPlayerbot.RandomBotRandomPassword = 0 AiPlayerbot.RandomBotAccountPrefix = "rndbot" # Enable/Disable rotation of bots (randomly select a bot from the bots pool to go online and rotate them periodically) -# Need to reset rndbot after changing the setting (.playerbot rndbot reset) # default: 0 (disable, the online bots are fixed) AiPlayerbot.EnableRotation = 0 @@ -528,12 +527,10 @@ AiPlayerbot.PreQuests = 0 AiPlayerbot.RandomBotJoinLfg = 1 # Percentage ratio of alliance and horde -# Need to reset rndbot after changing the setting (.playerbot rndbot reset) AiPlayerbot.RandomBotAllianceRatio = 50 AiPlayerbot.RandomBotHordeRatio = 50 # Disable death knight for bots login -# Need to reset rndbot after changing the setting (.playerbot rndbot reset) AiPlayerbot.DisableDeathKnightLogin = 0 # diff --git a/src/PlayerbotAIConfig.cpp b/src/PlayerbotAIConfig.cpp index ea03f95c..eb237b9f 100644 --- a/src/PlayerbotAIConfig.cpp +++ b/src/PlayerbotAIConfig.cpp @@ -548,9 +548,12 @@ bool PlayerbotAIConfig::Initialize() { return true; } - if (sPlayerbotAIConfig->addClassCommand) - sRandomPlayerbotMgr->PrepareAddclassCache(); + if (sPlayerbotAIConfig->enabled) + { + sRandomPlayerbotMgr->Init(); + } + sRandomItemMgr->Init(); sRandomItemMgr->InitAfterAhBot(); sPlayerbotTextMgr->LoadBotTexts(); @@ -563,8 +566,6 @@ bool PlayerbotAIConfig::Initialize() sTravelMgr->LoadQuestTravelTable(); } - if (sPlayerbotAIConfig->randomBotJoinBG) - sRandomPlayerbotMgr->LoadBattleMastersCache(); if (sPlayerbotAIConfig->randomBotSuggestDungeons) { diff --git a/src/RandomPlayerbotMgr.cpp b/src/RandomPlayerbotMgr.cpp index 456e91f3..81b5ce20 100644 --- a/src/RandomPlayerbotMgr.cpp +++ b/src/RandomPlayerbotMgr.cpp @@ -168,7 +168,6 @@ RandomPlayerbotMgr::RandomPlayerbotMgr() : PlayerbotHolder(), processTicks(0) if (sPlayerbotAIConfig->enabled || sPlayerbotAIConfig->randomBotAutologin) { sPlayerbotCommandServer->Start(); - PrepareTeleportCache(); } BattlegroundData.clear(); // Clear here and here only. @@ -1748,6 +1747,22 @@ void RandomPlayerbotMgr::PrepareAddclassCache() LOG_INFO("playerbots", ">> {} characters collected for addclass command.", collected); } +void RandomPlayerbotMgr::Init() +{ + if (sPlayerbotAIConfig->addClassCommand) + sRandomPlayerbotMgr->PrepareAddclassCache(); + + if (sPlayerbotAIConfig->enabled) + { + sRandomPlayerbotMgr->PrepareTeleportCache(); + } + + if (sPlayerbotAIConfig->randomBotJoinBG) + sRandomPlayerbotMgr->LoadBattleMastersCache(); + + PlayerbotsDatabase.Execute("DELETE FROM playerbots_random_bots WHERE event = 'add'"); +} + void RandomPlayerbotMgr::RandomTeleportForLevel(Player* bot) { if (bot->InBattleground()) diff --git a/src/RandomPlayerbotMgr.h b/src/RandomPlayerbotMgr.h index 8d8b9f56..8fa53fc9 100644 --- a/src/RandomPlayerbotMgr.h +++ b/src/RandomPlayerbotMgr.h @@ -170,6 +170,8 @@ public: static uint8 GetTeamClassIdx(bool isAlliance, uint8 claz) { return isAlliance * 20 + claz; } void PrepareAddclassCache(); + void PrepareTeleportCache(); + void Init(); std::map> addclassCache; std::map> locsPerLevelCache; std::map> allianceStarterPerLevelCache; @@ -199,7 +201,6 @@ private: void RandomTeleport(Player* bot); void RandomTeleport(Player* bot, std::vector& locs, bool hearth = false); uint32 GetZoneLevel(uint16 mapId, float teleX, float teleY, float teleZ); - void PrepareTeleportCache(); typedef void (RandomPlayerbotMgr::*ConsoleCommandHandler)(Player*); std::vector players; diff --git a/src/factory/PlayerbotFactory.cpp b/src/factory/PlayerbotFactory.cpp index c4ecd4a2..ac4d907f 100644 --- a/src/factory/PlayerbotFactory.cpp +++ b/src/factory/PlayerbotFactory.cpp @@ -49,6 +49,7 @@ std::list PlayerbotFactory::classQuestIds; std::list PlayerbotFactory::specialQuestIds; std::vector PlayerbotFactory::enchantSpellIdCache; std::vector PlayerbotFactory::enchantGemIdCache; +std::unordered_map> PlayerbotFactory::trainerIdCache; PlayerbotFactory::PlayerbotFactory(Player* bot, uint32 level, uint32 itemQuality, uint32 gearScoreLimit) : level(level), itemQuality(itemQuality), gearScoreLimit(gearScoreLimit), bot(bot) @@ -2327,7 +2328,7 @@ void PlayerbotFactory::SetRandomSkill(uint16 id) void PlayerbotFactory::InitAvailableSpells() { - if (trainerIdCache.empty()) + if (trainerIdCache[bot->getClass()].empty()) { CreatureTemplateContainer const* creatureTemplateContainer = sObjectMgr->GetCreatureTemplates(); for (CreatureTemplateContainer::const_iterator i = creatureTemplateContainer->begin(); @@ -2341,10 +2342,10 @@ void PlayerbotFactory::InitAvailableSpells() continue; uint32 trainerId = co.Entry; - trainerIdCache.push_back(trainerId); + trainerIdCache[bot->getClass()].push_back(trainerId); } } - for (uint32 trainerId : trainerIdCache) + for (uint32 trainerId : trainerIdCache[bot->getClass()]) { TrainerSpellData const* trainer_spells = sObjectMgr->GetNpcTrainerSpells(trainerId); if (!trainer_spells) diff --git a/src/factory/PlayerbotFactory.h b/src/factory/PlayerbotFactory.h index e77947e5..26e75811 100644 --- a/src/factory/PlayerbotFactory.h +++ b/src/factory/PlayerbotFactory.h @@ -190,7 +190,7 @@ private: uint32 itemQuality; uint32 gearScoreLimit; static std::list specialQuestIds; - std::vector trainerIdCache; + static std::unordered_map> trainerIdCache; static std::vector enchantSpellIdCache; static std::vector enchantGemIdCache;