Skip to content

Commit

Permalink
Merge pull request #39 from diamante0018/bot
Browse files Browse the repository at this point in the history
feature(bots): if bots.txt is empty use AW names
  • Loading branch information
Rackover authored Aug 28, 2023
2 parents 65d8a79 + a7b5a57 commit 762c32d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
46 changes: 42 additions & 4 deletions src/Components/Modules/Bots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Components

std::size_t Bots::BotDataIndex;

std::vector<Bots::botData> Bots::RemoteBotNames;

struct BotMovementInfo
{
std::int32_t buttons; // Actions
Expand Down Expand Up @@ -55,6 +57,17 @@ namespace Components
{ "activate", Game::CMD_BUTTON_ACTIVATE },
};

void Bots::UpdateBotNames()
{
const auto masterPort = (*Game::com_masterPort)->current.integer;
const auto* masterServerName = (*Game::com_masterServerName)->current.string;

Network::Address master(Utils::String::VA("%s:%u", masterServerName, masterPort));

Logger::Print("Getting bots...\n");
Network::Send(master, "getbots");
}

std::vector<Bots::botData> Bots::LoadBotNames()
{
std::vector<botData> result;
Expand Down Expand Up @@ -101,14 +114,19 @@ namespace Components
return result;
}

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 stats, int port)
{
std::string botName;
std::string clanName;

static const auto botNames = []() -> std::vector<botData>
{
auto names = LoadBotNames();
if (names.empty())
{
Logger::Print("bots.txt was empty. Using the names from the master server\n");
names = RemoteBotNames;
}

if (sv_randomBotNames->current.enabled)
{
Expand All @@ -133,7 +151,7 @@ namespace Components
clanName = "BOT"s;
}

return _snprintf_s(buffer, 0x400, _TRUNCATE, connectString, num, botName.data(), clanName.data(), protocol, checksum, statVer, statStuff, port);
return _snprintf_s(buffer, 0x400, _TRUNCATE, connectString, num, botName.data(), clanName.data(), protocol, checksum, statVer, stats, port);
}

void Bots::Spawn(unsigned int count)
Expand Down Expand Up @@ -446,9 +464,9 @@ namespace Components
}
}

count = std::min(Game::MAX_CLIENTS, count);
count = std::clamp<std::size_t>(count, 1, Game::MAX_CLIENTS);

Logger::Print("Spawning {} {}", count, (count == 1 ? "bot" : "bots"));
Logger::Print("Spawning {} {}\n", count, (count == 1 ? "bot" : "bots"));

Spawn(count);
});
Expand Down Expand Up @@ -476,6 +494,26 @@ namespace Components
sv_randomBotNames = Game::Dvar_RegisterBool("sv_randomBotNames", false, Game::DVAR_NONE, "Randomize the bots' names");
sv_replaceBots = Game::Dvar_RegisterBool("sv_replaceBots", false, Game::DVAR_NONE, "Test clients will be replaced by connecting players when the server is full.");

Scheduler::OnGameInitialized(UpdateBotNames, Scheduler::Pipeline::MAIN);

Network::OnClientPacket("getbotsResponse", [](const Network::Address& address, const std::string& data)
{
const auto masterPort = (*Game::com_masterPort)->current.integer;
const auto* masterServerName = (*Game::com_masterServerName)->current.string;

Network::Address master(Utils::String::VA("%s:%u", masterServerName, masterPort));
if (master == address)
{
auto botNames = Utils::String::Split(data, '\n');
Logger::Print("Got {} names from the master server\n", botNames.size());

for (const auto& entry : botNames)
{
RemoteBotNames.emplace_back(entry, "BOT");
}
}
});

// Reset BotMovementInfo.active when client is dropped
Events::OnClientDisconnect([](const int clientNum) -> void
{
Expand Down
6 changes: 5 additions & 1 deletion src/Components/Modules/Bots.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Components
static void SV_DirectConnect_Full_Check();

private:
using botData = std::pair< std::string, std::string>;
using botData = std::pair<std::string, std::string>;

static const Game::dvar_t* sv_randomBotNames;
static const Game::dvar_t* sv_replaceBots;

static std::size_t BotDataIndex;

static std::vector<botData> RemoteBotNames;

static void UpdateBotNames();

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);

Expand Down

0 comments on commit 762c32d

Please sign in to comment.