[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
- `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

View File

@ -13,11 +13,11 @@ namespace Components
{
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_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::botData> Bots::LoadBotNames()
{
std::random_device rd;
std::mt19937 gen(rd());
std::ranges::shuffle(BotNames, gen);
}
std::vector<botData> 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<botData>
{
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;
}

View File

@ -11,13 +11,13 @@ namespace Components
private:
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_replaceBots;
static void RandomizeBotNames();
static void LoadBotNames();
static std::size_t botDataIndex;
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 void Spawn(unsigned int count);