From 0c1700c1176222ea422b99270e636baf8c9ad10a Mon Sep 17 00:00:00 2001 From: Alex Dcnh <140754794+Wishmaster117@users.noreply.github.com> Date: Fri, 21 Nov 2025 15:56:03 +0100 Subject: [PATCH] CORE - Improved language detection for bots (#1784) I've had this problem for a long time, my bots only speak English even though I'm playing on a French client. I suppose this must be the case for some other people who do not have a large number of players with the same local client. If we use French DBCs, the bots bug because they only recognize US DBCs. From what I understand, the language is chosen as follows: On load, the module reads the entire `ai_playerbot_texts` table and stores each text variant in a dictionary indexed by the locale ID: the `text` column remains the default value (English), and the `text_loc1` to `text_loc8` columns fill slots 1 through 8. Whenever a real player connects, the module increments a counter for that player's DBC locale using `AddLocalePriority(player->GetSession()->GetSessionDbcLocale())`. When a bot needs a text, `GetLocalePriority()` returns the most frequently used locale index among currently connected players. The corresponding string is then retrieved. if the box is empty, we fall back to the English version (text[0]). ### This PR improve language detection. **Summary** - log both the client DBC locale and the account database locale when a player logs in - fall back to the account locale when the client reports enUS but the account is configured for another locale - keep the existing vote-based selection so bots always speak the majority language among connected players **Therefore, the original behavior is maintained. Bots still choose the most represented language among connected players (the counter is simply more efficient by prioritizing the account's locale when it differs from the client's English). For example, if more English-speaking players are connected, the language will revert to English, as the bots always share the majority locale.** --- src/PlayerbotMgr.cpp | 22 ++++++++++++++++++++-- src/PlayerbotTextMgr.cpp | 13 ++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/PlayerbotMgr.cpp b/src/PlayerbotMgr.cpp index 6bdbf9b6..fe4a714f 100644 --- a/src/PlayerbotMgr.cpp +++ b/src/PlayerbotMgr.cpp @@ -1602,8 +1602,26 @@ void PlayerbotMgr::OnBotLoginInternal(Player* const bot) void PlayerbotMgr::OnPlayerLogin(Player* player) { + if (!player) + return; + + WorldSession* session = player->GetSession(); + if (!session) + { + LOG_WARN("playerbots", "Unable to register locale priority for player {} because the session is missing", player->GetName()); + return; + } + + // DB locale (source of bot text translation) + LocaleConstant const databaseLocale = session->GetSessionDbLocaleIndex(); + + // For bot texts (DB-driven), prefer the database locale with a safe fallback. + LocaleConstant usedLocale = databaseLocale; + if (usedLocale >= MAX_LOCALES) + usedLocale = LOCALE_enUS; // fallback + // set locale priority for bot texts - sPlayerbotTextMgr->AddLocalePriority(player->GetSession()->GetSessionDbcLocale()); + sPlayerbotTextMgr->AddLocalePriority(usedLocale); if (sPlayerbotAIConfig->selfBotLevel > 2) HandlePlayerbotCommand("self", player); @@ -1611,7 +1629,7 @@ void PlayerbotMgr::OnPlayerLogin(Player* player) if (!sPlayerbotAIConfig->botAutologin) return; - uint32 accountId = player->GetSession()->GetAccountId(); + uint32 accountId = session->GetAccountId(); QueryResult results = CharacterDatabase.Query("SELECT name FROM characters WHERE account = {}", accountId); if (results) { diff --git a/src/PlayerbotTextMgr.cpp b/src/PlayerbotTextMgr.cpp index 3caa96a7..1dce9a29 100644 --- a/src/PlayerbotTextMgr.cpp +++ b/src/PlayerbotTextMgr.cpp @@ -190,26 +190,29 @@ bool PlayerbotTextMgr::GetBotText(std::string name, std::string& text, std::map< void PlayerbotTextMgr::AddLocalePriority(uint32 locale) { - if (!locale) + if (locale >= MAX_LOCALES) + { + LOG_WARN("playerbots", "Ignoring locale {} for bot texts because it exceeds MAX_LOCALES ({})", locale, MAX_LOCALES - 1); return; + } botTextLocalePriority[locale]++; } uint32 PlayerbotTextMgr::GetLocalePriority() { - uint32 topLocale = 0; - // if no real players online, reset top locale - if (!sWorldSessionMgr->GetActiveSessionCount()) + uint32 const activeSessions = sWorldSessionMgr->GetActiveSessionCount(); + if (!activeSessions) { ResetLocalePriority(); return 0; } + uint32 topLocale = 0; for (uint8 i = 0; i < MAX_LOCALES; ++i) { - if (botTextLocalePriority[i] > topLocale) + if (botTextLocalePriority[i] > botTextLocalePriority[topLocale]) topLocale = i; }