From 1ac8a93a32364824baa6dfc155eeb99d577712fd Mon Sep 17 00:00:00 2001 From: Jack Back Date: Mon, 1 Apr 2024 18:01:38 +0200 Subject: [PATCH] maint(bots): update logic --- src/client/component/bots.cpp | 44 ++++++++++++++++++++------------ src/client/component/network.cpp | 2 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/client/component/bots.cpp b/src/client/component/bots.cpp index 6c0a9c3..5c06b7f 100644 --- a/src/client/component/bots.cpp +++ b/src/client/component/bots.cpp @@ -64,7 +64,7 @@ namespace bots // SV_BotGetRandomName const auto* const bot_name = game::SV_BotGetRandomName(); - auto* bot_ent = game::SV_AddBot(bot_name); + const auto* bot_ent = game::SV_AddBot(bot_name); if (bot_ent) { spawn_bot(bot_ent->s.number); @@ -79,22 +79,13 @@ namespace bots volatile bool bot_names_received = false; std::vector bot_names; - const char* get_random_bot_name() - { - if (bot_names.empty()) - { - return get_bot_name_hook.invoke(); - } - - const auto index = std::rand() % bot_names.size(); - const auto& name = bot_names.at(index); - - return utils::string::va("%.*s", static_cast(name.size()), name.data()); - } - - bool should_update_bot_names() + bool should_use_remote_bot_names() { +#ifdef ALLOW_CUSTOM_BOT_NAMES return !filesystem::exists("bots.txt"); +#else + return true; +#endif } void parse_bot_names_from_file() @@ -122,11 +113,30 @@ namespace bots } } + const char* get_random_bot_name() + { + if (!bot_names_received && bot_names.empty()) + { + // last attempt to use custom names if they can be found + parse_bot_names_from_file(); + } + + if (bot_names.empty()) + { + return get_bot_name_hook.invoke(); + } + + const auto index = std::rand() % bot_names.size(); + const auto& name = bot_names.at(index); + + return utils::string::va("%.*s", static_cast(name.size()), name.data()); + } + void update_bot_names() { bot_names_received = false; - game::netadr_s master; + game::netadr_s master{}; if (server_list::get_master_server(master)) { console::info("Getting bots...\n"); @@ -167,7 +177,7 @@ namespace bots } }); - if (should_update_bot_names()) + if (should_use_remote_bot_names()) { scheduler::on_game_initialized([]() -> void { diff --git a/src/client/component/network.cpp b/src/client/component/network.cpp index 3be9886..02cccee 100644 --- a/src/client/component/network.cpp +++ b/src/client/component/network.cpp @@ -167,7 +167,7 @@ namespace network { if (a.port) { - return utils::string::va("%u.%u.%u.%u:%u", a.ip[0], a.ip[1], a.ip[2], a.ip[3], htons(a.port)); + return utils::string::va("%u.%u.%u.%u:%u", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ::htons(a.port)); } return utils::string::va("%u.%u.%u.%u", a.ip[0], a.ip[1], a.ip[2], a.ip[3]);