Performance update refined.

This commit is contained in:
Dustin Hendrickson
2025-05-27 10:43:57 -07:00
parent f1367a0f00
commit c8ed4f4631

View File

@@ -140,6 +140,63 @@ static void LoadBotLevelBracketsConfig()
ClampAndBalanceBrackets();
}
// -----------------------------------------------------------------------------
// BOT DETECTION HELPERS
// -----------------------------------------------------------------------------
static bool IsPlayerBot(Player* player)
{
if (!player)
{
return false;
}
PlayerbotAI* botAI = sPlayerbotsMgr->GetPlayerbotAI(player);
return botAI && botAI->IsBotAI();
}
static bool IsPlayerRandomBot(Player* player)
{
if (!player)
{
return false;
}
return sRandomPlayerbotMgr->IsRandomBot(player);
}
static bool IsAlliancePlayerBot(Player* bot)
{
return bot && (bot->GetTeamId() == TEAM_ALLIANCE);
}
static bool IsHordePlayerBot(Player* bot)
{
return bot && (bot->GetTeamId() == TEAM_HORDE);
}
static void LogAllBotLevels()
{
if (g_BotDistFullDebugMode)
{
std::map<uint8, uint32> botLevelCount;
for (auto const& itr : ObjectAccessor::GetPlayers())
{
Player* player = itr.second;
if (!player || !player->IsInWorld())
{
continue;
}
if (!IsPlayerBot(player))
{
continue;
}
botLevelCount[player->GetLevel()]++;
}
for (const auto& entry : botLevelCount)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Level {}: {} bots", entry.first, entry.second);
}
}
}
// -----------------------------------------------------------------------------
// Loads the friend guid(s) from character_social into array
// -----------------------------------------------------------------------------
@@ -302,63 +359,6 @@ static void AdjustBotToRange(Player* bot, int targetRangeIndex, const LevelRange
ChatHandler(bot->GetSession()).SendSysMessage("[mod-bot-level-brackets] Your level has been reset.");
}
// -----------------------------------------------------------------------------
// BOT DETECTION HELPERS
// -----------------------------------------------------------------------------
static bool IsPlayerBot(Player* player)
{
if (!player)
{
return false;
}
PlayerbotAI* botAI = sPlayerbotsMgr->GetPlayerbotAI(player);
return botAI && botAI->IsBotAI();
}
static bool IsPlayerRandomBot(Player* player)
{
if (!player)
{
return false;
}
return sRandomPlayerbotMgr->IsRandomBot(player);
}
static bool IsAlliancePlayerBot(Player* bot)
{
return bot && (bot->GetTeamId() == TEAM_ALLIANCE);
}
static bool IsHordePlayerBot(Player* bot)
{
return bot && (bot->GetTeamId() == TEAM_HORDE);
}
static void LogAllBotLevels()
{
if (g_BotDistFullDebugMode)
{
std::map<uint8, uint32> botLevelCount;
for (auto const& itr : ObjectAccessor::GetPlayers())
{
Player* player = itr.second;
if (!player || !player->IsInWorld())
{
continue;
}
if (!IsPlayerBot(player))
{
continue;
}
botLevelCount[player->GetLevel()]++;
}
for (const auto& entry : botLevelCount)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Level {}: {} bots", entry.first, entry.second);
}
}
}
// -----------------------------------------------------------------------------
// HELPER FUNCTION: Check if a bot is in a guild with at least one real player online.
// -----------------------------------------------------------------------------
@@ -956,6 +956,7 @@ public:
LOG_INFO("server.loading", "[BotLevelBrackets] =========================================");
}
// Process Alliance bots.
// Process Alliance bots.
if (totalAllianceBots > 0)
{
@@ -974,78 +975,45 @@ public:
allianceDesiredCounts[i], allianceActualCounts[i]);
}
}
for (int i = 0; i < g_NumRanges; ++i)
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] >>> Processing Alliance bots in range {}.", i + 1);
}
// Collect safe and flagged bots
std::vector<Player*> safeBots;
std::vector<Player*> flaggedBots;
for (Player* bot : allianceBotsByRange[i])
{
if (IsBotSafeForLevelReset(bot))
{
if (IsBotSafeForLevelReset(bot)) {
safeBots.push_back(bot);
}
else
{
} else {
flaggedBots.push_back(bot);
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Alliance bot '{}' is NOT safe for level reset in range {}.",
bot->GetName(), i + 1);
}
}
}
while (allianceActualCounts[i] > allianceDesiredCounts[i] && !safeBots.empty())
{
Player* bot = safeBots.back();
if (!bot || !bot->IsInWorld() || !bot->GetSession() || bot->GetSession()->isLogingOut() || bot->IsDuringRemoveFromWorld())
{
safeBots.pop_back();
continue;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Alliance safe bot '{}' from range {} will be moved.", bot->GetName(), i + 1);
}
int targetRange = -1;
if (bot->getClass() == CLASS_DEATH_KNIGHT)
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (allianceActualCounts[j] < allianceDesiredCounts[j] && g_AllianceLevelRanges[j].upper >= 55)
{
targetRange = j;
break;
}
}
}
else
{
// --------- Efficient surplus redistribution for safeBots ----------
// Build a list of target ranges that need bots
std::vector<int> targetRanges;
for (int j = 0; j < g_NumRanges; ++j)
{
if (allianceActualCounts[j] < allianceDesiredCounts[j])
targetRanges.push_back(j);
}
size_t targetIdx = 0;
while (allianceActualCounts[i] > allianceDesiredCounts[i] && !safeBots.empty() && targetIdx < targetRanges.size())
{
targetRange = j;
break;
}
}
}
if (targetRange == -1)
Player* bot = safeBots.back();
safeBots.pop_back();
int targetRange = targetRanges[targetIdx];
// Skip if no need (already filled by earlier loop)
if (allianceActualCounts[targetRange] >= allianceDesiredCounts[targetRange])
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] No valid target range found for alliance safe bot '{}'.", bot->GetName());
}
break;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] !!!! Adjusting alliance bot '{}' from range {} to range {} ({}-{}).",
bot->GetName(), i + 1, targetRange + 1, g_AllianceLevelRanges[targetRange].lower, g_AllianceLevelRanges[targetRange].upper);
targetIdx++;
continue;
}
// Only flag if not already flagged
bool alreadyFlagged = false;
for (auto& entry : g_PendingLevelResets)
{
@@ -1066,50 +1034,25 @@ public:
}
allianceActualCounts[i]--;
allianceActualCounts[targetRange]++;
if (allianceActualCounts[targetRange] >= allianceDesiredCounts[targetRange])
targetIdx++;
}
while (allianceActualCounts[i] > allianceDesiredCounts[i] && !flaggedBots.empty())
// --------- Efficient surplus redistribution for flaggedBots ----------
// Reset for flagged bots
targetIdx = 0;
while (allianceActualCounts[i] > allianceDesiredCounts[i] && !flaggedBots.empty() && targetIdx < targetRanges.size())
{
Player* bot = flaggedBots.back();
if (!bot || !bot->IsInWorld() || !bot->GetSession() || bot->GetSession()->isLogingOut() || bot->IsDuringRemoveFromWorld())
{
flaggedBots.pop_back();
int targetRange = targetRanges[targetIdx];
if (allianceActualCounts[targetRange] >= allianceDesiredCounts[targetRange])
{
targetIdx++;
continue;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Alliance flagged bot '{}' from range {} will be processed for pending reset.", bot->GetName(), i + 1);
}
int targetRange = -1;
if (bot->getClass() == CLASS_DEATH_KNIGHT)
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (allianceActualCounts[j] < allianceDesiredCounts[j] && g_AllianceLevelRanges[j].upper >= 55)
{
targetRange = j;
break;
}
}
}
else
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (allianceActualCounts[j] < allianceDesiredCounts[j])
{
targetRange = j;
break;
}
}
}
if (targetRange == -1)
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] No valid target range found for flagged alliance bot '{}' for pending reset.", bot->GetName());
}
break;
}
bool alreadyFlagged = false;
for (auto& entry : g_PendingLevelResets)
{
@@ -1124,14 +1067,19 @@ public:
g_PendingLevelResets.push_back({bot, targetRange, g_AllianceLevelRanges.data()});
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Alliance bot '{}' flagged for pending level reset to range {}-{}.",
LOG_INFO("server.loading", "[BotLevelBrackets] Alliance flagged bot '{}' flagged for pending level reset to range {}-{}.",
bot->GetName(), g_AllianceLevelRanges[targetRange].lower, g_AllianceLevelRanges[targetRange].upper);
}
}
allianceActualCounts[i]--;
allianceActualCounts[targetRange]++;
if (allianceActualCounts[targetRange] >= allianceDesiredCounts[targetRange])
targetIdx++;
}
}
}
// Process Horde bots.
if (totalHordeBots > 0)
{
@@ -1150,78 +1098,41 @@ public:
hordeDesiredCounts[i], hordeActualCounts[i]);
}
}
for (int i = 0; i < g_NumRanges; ++i)
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Processing Horde bots in range {}.", i + 1);
}
std::vector<Player*> safeBots;
std::vector<Player*> flaggedBots;
for (Player* bot : hordeBotsByRange[i])
{
if (IsBotSafeForLevelReset(bot))
{
if (IsBotSafeForLevelReset(bot)) {
safeBots.push_back(bot);
}
else
{
} else {
flaggedBots.push_back(bot);
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Horde bot '{}' is NOT safe for level reset in range {}.",
bot->GetName(), i + 1);
}
}
}
while (hordeActualCounts[i] > hordeDesiredCounts[i] && !safeBots.empty())
{
Player* bot = safeBots.back();
if (!bot || !bot->IsInWorld() || !bot->GetSession() || bot->GetSession()->isLogingOut() || bot->IsDuringRemoveFromWorld())
{
safeBots.pop_back();
continue;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Horde safe bot '{}' from range {} will be moved.", bot->GetName(), i + 1);
}
int targetRange = -1;
if (bot->getClass() == CLASS_DEATH_KNIGHT)
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (hordeActualCounts[j] < hordeDesiredCounts[j] && g_HordeLevelRanges[j].upper >= 55)
{
targetRange = j;
break;
}
}
}
else
{
// Efficient surplus redistribution for safeBots
std::vector<int> targetRanges;
for (int j = 0; j < g_NumRanges; ++j)
{
if (hordeActualCounts[j] < hordeDesiredCounts[j])
targetRanges.push_back(j);
}
size_t targetIdx = 0;
while (hordeActualCounts[i] > hordeDesiredCounts[i] && !safeBots.empty() && targetIdx < targetRanges.size())
{
targetRange = j;
break;
}
}
}
if (targetRange == -1)
Player* bot = safeBots.back();
safeBots.pop_back();
int targetRange = targetRanges[targetIdx];
if (hordeActualCounts[targetRange] >= hordeDesiredCounts[targetRange])
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] No valid target range found for safe horde bot '{}'.", bot->GetName());
}
break;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] !!!! Adjusting horde bot '{}' from range {} to range {} ({}-{}).",
bot->GetName(), i + 1, targetRange + 1, g_HordeLevelRanges[targetRange].lower, g_HordeLevelRanges[targetRange].upper);
targetIdx++;
continue;
}
bool alreadyFlagged = false;
for (auto& entry : g_PendingLevelResets)
{
@@ -1242,50 +1153,24 @@ public:
}
hordeActualCounts[i]--;
hordeActualCounts[targetRange]++;
if (hordeActualCounts[targetRange] >= hordeDesiredCounts[targetRange])
targetIdx++;
}
while (hordeActualCounts[i] > hordeDesiredCounts[i] && !flaggedBots.empty())
// Efficient surplus redistribution for flaggedBots
targetIdx = 0;
while (hordeActualCounts[i] > hordeDesiredCounts[i] && !flaggedBots.empty() && targetIdx < targetRanges.size())
{
Player* bot = flaggedBots.back();
if (!bot || !bot->IsInWorld() || !bot->GetSession() || bot->GetSession()->isLogingOut() || bot->IsDuringRemoveFromWorld())
{
flaggedBots.pop_back();
int targetRange = targetRanges[targetIdx];
if (hordeActualCounts[targetRange] >= hordeDesiredCounts[targetRange])
{
targetIdx++;
continue;
}
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Horde flagged bot '{}' from range {} will be processed for pending reset.", bot->GetName(), i + 1);
}
int targetRange = -1;
if (bot->getClass() == CLASS_DEATH_KNIGHT)
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (hordeActualCounts[j] < hordeDesiredCounts[j] && g_HordeLevelRanges[j].upper >= 55)
{
targetRange = j;
break;
}
}
}
else
{
for (int j = 0; j < g_NumRanges; ++j)
{
if (hordeActualCounts[j] < hordeDesiredCounts[j])
{
targetRange = j;
break;
}
}
}
if (targetRange == -1)
{
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] No valid target range found for flagged horde bot '{}' for pending reset.", bot->GetName());
}
break;
}
bool alreadyFlagged = false;
for (auto& entry : g_PendingLevelResets)
{
@@ -1300,14 +1185,19 @@ public:
g_PendingLevelResets.push_back({bot, targetRange, g_HordeLevelRanges.data()});
if (g_BotDistFullDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] Horde bot '{}' flagged for pending level reset to range {}-{}.",
LOG_INFO("server.loading", "[BotLevelBrackets] Horde flagged bot '{}' flagged for pending level reset to range {}-{}.",
bot->GetName(), g_HordeLevelRanges[targetRange].lower, g_HordeLevelRanges[targetRange].upper);
}
}
hordeActualCounts[i]--;
hordeActualCounts[targetRange]++;
if (hordeActualCounts[targetRange] >= hordeDesiredCounts[targetRange])
targetIdx++;
}
}
}
if (g_BotDistFullDebugMode || g_BotDistLiteDebugMode)
{
LOG_INFO("server.loading", "[BotLevelBrackets] ========================================= COMPLETE");