2022-02-27 23:54:27 -05:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "loader/component_loader.hpp"
|
|
|
|
|
|
|
|
#include "command.hpp"
|
|
|
|
#include "scheduler.hpp"
|
|
|
|
#include "network.hpp"
|
|
|
|
#include "party.hpp"
|
|
|
|
|
|
|
|
#include "game/game.hpp"
|
2022-03-04 14:46:09 -05:00
|
|
|
#include "game/scripting/execution.hpp"
|
2022-02-27 23:54:27 -05:00
|
|
|
|
|
|
|
#include <utils/hook.hpp>
|
|
|
|
#include <utils/string.hpp>
|
|
|
|
#include <utils/cryptography.hpp>
|
|
|
|
|
|
|
|
namespace bots
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool can_add()
|
|
|
|
{
|
2022-03-13 18:12:34 -04:00
|
|
|
return party::get_client_count() < *game::mp::svs_numclients
|
|
|
|
&& game::SV_Loaded() && !game::VirtualLobby_Loaded();
|
2022-02-27 23:54:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void bot_team_join(const int entity_num)
|
|
|
|
{
|
2022-03-04 14:46:09 -05:00
|
|
|
const game::scr_entref_t entref{static_cast<uint16_t>(entity_num), 0};
|
|
|
|
scheduler::once([entref]()
|
2022-02-27 23:54:27 -05:00
|
|
|
{
|
2022-03-04 14:46:09 -05:00
|
|
|
scripting::notify(entref, "luinotifyserver", {"team_select", 2});
|
|
|
|
scheduler::once([entref]()
|
2022-02-27 23:54:27 -05:00
|
|
|
{
|
2022-03-04 14:46:09 -05:00
|
|
|
auto* _class = utils::string::va("class%d", utils::cryptography::random::get_integer() % 5);
|
|
|
|
scripting::notify(entref, "luinotifyserver", {"class_select", _class});
|
|
|
|
}, scheduler::pipeline::server, 2s);
|
|
|
|
}, scheduler::pipeline::server, 2s);
|
2022-02-27 23:54:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void spawn_bot(const int entity_num)
|
|
|
|
{
|
|
|
|
game::SV_SpawnTestClient(&game::mp::g_entities[entity_num]);
|
|
|
|
if (game::Com_GetCurrentCoDPlayMode() == game::CODPLAYMODE_CORE)
|
|
|
|
{
|
|
|
|
bot_team_join(entity_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_bot()
|
|
|
|
{
|
|
|
|
if (!can_add())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-13 18:12:34 -04:00
|
|
|
static auto first_bot = true;
|
|
|
|
|
|
|
|
const auto bot_name = game::SV_BotGetRandomName();
|
|
|
|
const auto bot_ent = game::SV_AddBot(bot_name);
|
|
|
|
|
2022-02-27 23:54:27 -05:00
|
|
|
if (bot_ent)
|
|
|
|
{
|
|
|
|
spawn_bot(bot_ent->s.entityNum);
|
|
|
|
}
|
2022-03-13 18:12:34 -04:00
|
|
|
// can cause a stack overflow
|
|
|
|
// else if (can_add()) // workaround since first bot won't ever spawn
|
|
|
|
// {
|
|
|
|
// add_bot();
|
|
|
|
// }
|
2022-02-27 23:54:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class component final : public component_interface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void post_unpack() override
|
|
|
|
{
|
|
|
|
if (game::environment::is_sp())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
command::add("spawnBot", [](const command::params& params)
|
|
|
|
{
|
2022-03-13 18:12:34 -04:00
|
|
|
if (!can_add())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-02-27 23:54:27 -05:00
|
|
|
|
|
|
|
auto num_bots = 1;
|
|
|
|
if (params.size() == 2)
|
|
|
|
{
|
|
|
|
num_bots = atoi(params.get(1));
|
|
|
|
}
|
|
|
|
|
2022-03-05 20:37:08 -05:00
|
|
|
num_bots = std::min(num_bots, *game::mp::svs_numclients);;
|
|
|
|
|
|
|
|
for (auto i = 0; i < num_bots; i++)
|
2022-02-27 23:54:27 -05:00
|
|
|
{
|
|
|
|
scheduler::once(add_bot, scheduler::pipeline::server, 100ms * i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_COMPONENT(bots::component)
|