[Bots]: Use modern c++ lambda for loading file once (#957)

This commit is contained in:
Edo 2023-04-20 19:20:00 +02:00 committed by GitHub
parent b3a0c521d2
commit 39b9b9818c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 33 deletions

View File

@ -14,7 +14,7 @@ The format is based on [Keep a Changelog v0.3.0](http://keepachangelog.com/en/0.
### Changed ### 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. - `SetPing` GSC method is now deprecated.
### Fixed ### Fixed

View File

@ -13,11 +13,11 @@ namespace Components
{ {
constexpr std::size_t MAX_NAME_LENGTH = 16; constexpr std::size_t MAX_NAME_LENGTH = 16;
std::vector<Bots::botData> Bots::BotNames;
const Game::dvar_t* Bots::sv_randomBotNames; const Game::dvar_t* Bots::sv_randomBotNames;
const Game::dvar_t* Bots::sv_replaceBots; const Game::dvar_t* Bots::sv_replaceBots;
std::size_t Bots::botDataIndex;
struct BotMovementInfo struct BotMovementInfo
{ {
std::int32_t buttons; // Actions std::int32_t buttons; // Actions
@ -54,27 +54,21 @@ namespace Components
{ "activate", Game::CMD_BUTTON_ACTIVATE }, { "activate", Game::CMD_BUTTON_ACTIVATE },
}; };
void Bots::RandomizeBotNames() std::vector<Bots::botData> Bots::LoadBotNames()
{ {
std::random_device rd; std::vector<botData> result;
std::mt19937 gen(rd());
std::ranges::shuffle(BotNames, gen);
}
void Bots::LoadBotNames()
{
FileSystem::File bots("bots.txt"); FileSystem::File bots("bots.txt");
if (!bots.exists()) if (!bots.exists())
{ {
return; return result;
} }
auto data = Utils::String::Split(bots.getBuffer(), '\n'); auto data = Utils::String::Split(bots.getBuffer(), '\n');
for (auto& entry : data) for (auto& entry : data)
{ {
// Take into account for CR line endings // Take into account CR line endings
Utils::String::Replace(entry, "\r", ""); Utils::String::Replace(entry, "\r", "");
// Remove whitespace // Remove whitespace
Utils::String::Trim(entry); Utils::String::Trim(entry);
@ -100,38 +94,41 @@ namespace Components
entry = entry.substr(0, MAX_NAME_LENGTH - 1); entry = entry.substr(0, MAX_NAME_LENGTH - 1);
BotNames.emplace_back(entry, clanAbbrev); result.emplace_back(entry, clanAbbrev);
} }
if (sv_randomBotNames->current.enabled) return result;
{
RandomizeBotNames();
}
} }
int Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port) 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 botName;
std::string clanName; std::string clanName;
if (!loadedNames) static const auto botNames = []() -> std::vector<botData>
{ {
loadedNames = true; auto names = LoadBotNames();
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(); botDataIndex %= botNames.size();
const auto index = botId++; const auto index = botDataIndex++;
botName = BotNames[index].first; botName = botNames[index].first;
clanName = BotNames[index].second; clanName = botNames[index].second;
} }
else else
{ {
botName = std::format("bot{}", ++botId); botName = std::format("bot{}", port);
clanName = "BOT"s; clanName = "BOT"s;
} }

View File

@ -11,13 +11,13 @@ namespace Components
private: private:
using botData = std::pair< std::string, std::string>; using botData = std::pair< std::string, std::string>;
static std::vector<botData> BotNames;
static const Game::dvar_t* sv_randomBotNames; static const Game::dvar_t* sv_randomBotNames;
static const Game::dvar_t* sv_replaceBots; static const Game::dvar_t* sv_replaceBots;
static void RandomizeBotNames(); static std::size_t botDataIndex;
static void LoadBotNames();
static std::vector<botData> LoadBotNames();
static int BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port); 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); static void Spawn(unsigned int count);