diff --git a/CHANGELOG.md b/CHANGELOG.md index 7296ac3f..2de00a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog v0.3.0](http://keepachangelog.com/en/0. ### Changed -- `sv_mapRotationCurrent` supports `exec` directive for executing cfg scripts from the `game_settings` folder +- `sv_mapRotationCurrent` supports `exec` directive for executing cfg scripts from the `game_settings` folder (#916) - `SetPing` GSC method is now deprecated. ### Fixed diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index a598c4ef..0daebf16 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -13,11 +13,11 @@ namespace Components { constexpr std::size_t MAX_NAME_LENGTH = 16; - std::vector Bots::BotNames; - const Game::dvar_t* Bots::sv_randomBotNames; const Game::dvar_t* Bots::sv_replaceBots; + std::size_t Bots::botDataIndex; + struct BotMovementInfo { std::int32_t buttons; // Actions @@ -54,27 +54,21 @@ namespace Components { "activate", Game::CMD_BUTTON_ACTIVATE }, }; - void Bots::RandomizeBotNames() + std::vector Bots::LoadBotNames() { - std::random_device rd; - std::mt19937 gen(rd()); - std::ranges::shuffle(BotNames, gen); - } + std::vector result; - void Bots::LoadBotNames() - { FileSystem::File bots("bots.txt"); - if (!bots.exists()) { - return; + return result; } auto data = Utils::String::Split(bots.getBuffer(), '\n'); for (auto& entry : data) { - // Take into account for CR line endings + // Take into account CR line endings Utils::String::Replace(entry, "\r", ""); // Remove whitespace Utils::String::Trim(entry); @@ -100,38 +94,41 @@ namespace Components entry = entry.substr(0, MAX_NAME_LENGTH - 1); - BotNames.emplace_back(entry, clanAbbrev); + result.emplace_back(entry, clanAbbrev); } - if (sv_randomBotNames->current.enabled) - { - RandomizeBotNames(); - } + return result; } int Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port) { - static std::size_t botId = 0; // Loop over the BotNames vector - static bool loadedNames = false; // Load file only once std::string botName; std::string clanName; - if (!loadedNames) + static const auto botNames = []() -> std::vector { - loadedNames = true; - LoadBotNames(); - } + auto names = LoadBotNames(); - if (!BotNames.empty()) + if (sv_randomBotNames->current.enabled) + { + std::random_device rd; + std::mt19937 gen(rd()); + std::ranges::shuffle(names, gen); + } + + return names; + }(); + + if (!botNames.empty()) { - botId %= BotNames.size(); - const auto index = botId++; - botName = BotNames[index].first; - clanName = BotNames[index].second; + botDataIndex %= botNames.size(); + const auto index = botDataIndex++; + botName = botNames[index].first; + clanName = botNames[index].second; } else { - botName = std::format("bot{}", ++botId); + botName = std::format("bot{}", port); clanName = "BOT"s; } diff --git a/src/Components/Modules/Bots.hpp b/src/Components/Modules/Bots.hpp index 9d7526e1..b7cf2315 100644 --- a/src/Components/Modules/Bots.hpp +++ b/src/Components/Modules/Bots.hpp @@ -11,13 +11,13 @@ namespace Components private: using botData = std::pair< std::string, std::string>; - static std::vector BotNames; static const Game::dvar_t* sv_randomBotNames; static const Game::dvar_t* sv_replaceBots; - static void RandomizeBotNames(); - static void LoadBotNames(); + static std::size_t botDataIndex; + + static std::vector LoadBotNames(); static int BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port); static void Spawn(unsigned int count);