iw4x-client/src/Components/Modules/Bots.cpp

367 lines
9.4 KiB
C++
Raw Normal View History

2017-01-19 16:23:59 -05:00
#include "STDInclude.hpp"
#define KEY_MASK_FIRE 1
#define KEY_MASK_SPRINT 2
#define KEY_MASK_MELEE 4
#define KEY_MASK_RELOAD 16
#define KEY_MASK_LEANLEFT 64
#define KEY_MASK_LEANRIGHT 128
#define KEY_MASK_PRONE 256
#define KEY_MASK_CROUCH 512
#define KEY_MASK_JUMP 1024
#define KEY_MASK_ADS_MODE 2048
#define KEY_MASK_TEMP_ACTION 4096
#define KEY_MASK_HOLDBREATH 8192
#define KEY_MASK_FRAG 16384
#define KEY_MASK_SMOKE 32768
#define KEY_MASK_NIGHTVISION 262144
#define KEY_MASK_ADS 524288
#define KEY_MASK_USE 0x28
#define MAX_G_BOTAI_ENTRIES 18
2017-01-19 16:23:59 -05:00
namespace Components
{
std::vector<std::string> Bots::BotNames;
typedef struct BotMovementInfo_t
{
/* Actions */
int buttons;
/* Movement */
2022-01-07 16:00:44 -05:00
int8_t forward;
int8_t right;
/* Weapon */
2022-01-07 16:00:44 -05:00
uint16_t weapon;
} BotMovementInfo_t;
2020-11-14 04:28:58 -05:00
static BotMovementInfo_t g_botai[MAX_G_BOTAI_ENTRIES];
struct BotAction_t
{
const char* action;
int key;
};
2020-11-14 04:28:58 -05:00
static const BotAction_t BotActions[] =
{
{ "gostand", KEY_MASK_JUMP },
{ "gocrouch", KEY_MASK_CROUCH },
{ "goprone", KEY_MASK_PRONE },
{ "fire", KEY_MASK_FIRE },
{ "melee", KEY_MASK_MELEE },
{ "frag", KEY_MASK_FRAG },
{ "smoke", KEY_MASK_SMOKE },
{ "reload", KEY_MASK_RELOAD },
{ "sprint", KEY_MASK_SPRINT },
{ "leanleft", KEY_MASK_LEANLEFT },
{ "leanright", KEY_MASK_LEANRIGHT },
{ "ads", KEY_MASK_ADS_MODE },
{ "holdbreath", KEY_MASK_HOLDBREATH },
{ "use", KEY_MASK_USE },
{ "0", 8 },
{ "1", 32 },
{ "2", 65536 },
{ "3", 131072 },
{ "4", 1048576 },
{ "5", 2097152 },
{ "6", 4194304 },
{ "7", 8388608 },
{ "8", 16777216 },
{ "9", 33554432 },
};
2020-11-14 03:58:05 -05:00
bool Bots::IsValidClientNum(unsigned int cNum)
{
2020-11-14 04:29:53 -05:00
return (cNum >= 0) && (cNum < (unsigned int)*Game::svs_numclients);
2020-11-14 03:58:05 -05:00
}
2017-01-19 16:23:59 -05:00
void Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port)
{
static int botId = 0;
const char* botName;
2017-01-19 16:23:59 -05:00
if (Bots::BotNames.empty())
{
FileSystem::File bots("bots.txt");
if (bots.exists())
{
2022-01-07 16:00:44 -05:00
auto names = Utils::String::Explode(bots.getBuffer(), '\n');
2017-01-19 16:23:59 -05:00
2022-01-07 16:00:44 -05:00
for (auto& name : names)
2017-01-19 16:23:59 -05:00
{
Utils::String::Replace(name, "\r", "");
name = Utils::String::Trim(name);
if (!name.empty())
{
Bots::BotNames.push_back(name);
}
}
}
}
2017-01-19 16:23:59 -05:00
if (!Bots::BotNames.empty())
{
botId %= Bots::BotNames.size();
botName = Bots::BotNames[botId++].data();
}
else
{
botName = Utils::String::VA("bot%d", ++botId);
2017-01-19 16:23:59 -05:00
}
2022-01-07 16:00:44 -05:00
_snprintf_s(buffer, 0x400, _TRUNCATE, connectString, num, botName, protocol, checksum, statVer, statStuff, port);
2017-01-19 16:23:59 -05:00
}
2022-01-07 16:00:44 -05:00
void Bots::Spawn(int count)
2017-04-23 07:31:48 -04:00
{
2022-01-07 16:00:44 -05:00
for (auto i = 0; i < count; ++i)
2017-04-23 07:31:48 -04:00
{
Scheduler::OnDelay([]()
2017-04-23 07:31:48 -04:00
{
2022-01-07 16:00:44 -05:00
auto* ent = Game::SV_AddTestClient();
if (ent == nullptr)
return;
Scheduler::OnDelay([ent]()
2017-04-23 07:31:48 -04:00
{
2022-01-07 16:00:44 -05:00
Game::Scr_AddString("autoassign");
Game::Scr_AddString("team_marinesopfor");
Game::Scr_Notify(ent, Game::SL_GetString("menuresponse", 0), 2);
Scheduler::OnDelay([ent]()
2017-04-24 15:14:08 -04:00
{
2022-01-07 16:00:44 -05:00
Game::Scr_AddString(Utils::String::VA("class%u", Utils::Cryptography::Rand::GenerateInt() % 5u));
Game::Scr_AddString("changeclass");
Game::Scr_Notify(ent, Game::SL_GetString("menuresponse", 0), 2);
}, 1s);
}, 1s);
2017-04-24 15:14:08 -04:00
}, 500ms * (i + 1));
2017-04-23 07:31:48 -04:00
}
}
2020-11-14 03:58:05 -05:00
void Bots::AddMethods()
{
Script::AddFunction("SetPing", [](Game::scr_entref_t entref) // gsc: self SetPing(<int>)
{
2022-01-07 16:00:44 -05:00
const auto ping = Game::Scr_GetInt(0);
if (ping < 0 || ping > 999)
{
Game::Scr_ParamError(0, "^1SetPing: Ping needs to be between 0 and 999!\n");
return;
}
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
auto* client = Script::GetClientFromEnt(gentity);
if (!client->isBot)
{
Game::Scr_Error("^1SetPing: Can only call on a bot!\n");
return;
}
2022-01-07 16:00:44 -05:00
client->ping = static_cast<int16_t>(ping);
});
Script::AddFunction("IsBot", [](Game::scr_entref_t entref) // Usage: <bot> IsBot();
{
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
const auto* client = Script::GetClientFromEnt(gentity);
Game::Scr_AddBool(client->isBot == 1);
});
Script::AddFunction("BotStop", [](Game::scr_entref_t entref) // Usage: <bot> BotStop();
{
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
const auto* client = Script::GetClientFromEnt(gentity);
if (!client->isBot)
{
2022-01-07 16:00:44 -05:00
Game::Scr_Error("^1BotStop: Can only call on a bot!\n");
return;
}
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum] = {0};
g_botai[entref.entnum].weapon = 1;
});
Script::AddFunction("BotWeapon", [](Game::scr_entref_t entref) // Usage: <bot> BotWeapon(<str>);
{
2022-01-07 16:00:44 -05:00
const auto* weapon = Game::Scr_GetString(0);
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
const auto* client = Script::GetClientFromEnt(gentity);
if (!client->isBot)
{
2022-01-07 16:00:44 -05:00
Game::Scr_Error("^1BotWeapon: Can only call on a bot!\n");
return;
}
2022-01-07 16:00:44 -05:00
if (weapon[0] == '\0')
{
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum].weapon = 1;
return;
}
2022-01-07 16:00:44 -05:00
const auto weapId = Game::G_GetWeaponIndexForName(weapon);
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum].weapon = static_cast<uint16_t>(weapId);
});
Script::AddFunction("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> BotAction(<str action>);
{
2022-01-07 16:00:44 -05:00
const auto* action = Game::Scr_GetString(0);
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
const auto* client = Script::GetClientFromEnt(gentity);
if (!client->isBot)
{
2022-01-07 16:00:44 -05:00
Game::Scr_Error("^1BotAction: Can only call on a bot!\n");
return;
}
2022-01-07 16:00:44 -05:00
if (action[0] != '+' && action[0] != '-')
{
Game::Scr_ParamError(0, "^1BotAction: Sign for action must be '+' or '-'.\n");
return;
}
2022-01-07 16:00:44 -05:00
for (auto i = 0u; i < sizeof(BotActions) / sizeof(BotAction_t); ++i)
{
if (strcmp(&action[1], BotActions[i].action))
continue;
if (action[0] == '+')
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum].buttons |= BotActions[i].key;
else
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum].buttons &= ~(BotActions[i].key);
return;
}
Game::Scr_ParamError(0, "^1BotAction: Unknown action.\n");
});
Script::AddFunction("BotMovement", [](Game::scr_entref_t entref) // Usage: <bot> BotMovement(<int>, <int>);
{
auto forwardInt = Game::Scr_GetInt(0);
auto rightInt = Game::Scr_GetInt(1);
const auto* gentity = Script::GetEntFromEntRef(entref);
2022-01-07 16:00:44 -05:00
const auto* client = Script::GetClientFromEnt(gentity);
if (!client->isBot)
{
2022-01-07 16:00:44 -05:00
Game::Scr_Error("^1BotMovement: Can only call on a bot!\n");
return;
}
if (forwardInt > 127)
forwardInt = 127;
2022-01-07 16:00:44 -05:00
if (forwardInt < -127)
forwardInt = -127;
2022-01-07 16:00:44 -05:00
if (rightInt > 127)
rightInt = 127;
2022-01-07 16:00:44 -05:00
if (rightInt < -127)
rightInt = -127;
2020-11-14 03:58:05 -05:00
2022-01-17 19:21:25 -05:00
g_botai[entref.entnum].forward = static_cast<int8_t>(forwardInt);
g_botai[entref.entnum].right = static_cast<int8_t>(rightInt);
});
2020-11-14 03:58:05 -05:00
}
2017-01-19 16:23:59 -05:00
Bots::Bots()
{
// Replace connect string
Utils::Hook::Set<const char*>(0x48ADA6, "connect bot%d \"\\cg_predictItems\\1\\cl_anonymous\\0\\color\\4\\head\\default\\model\\multi\\snaps\\20\\rate\\5000\\name\\%s\\protocol\\%d\\checksum\\%d\\statver\\%d %u\\qport\\%d\"");
// Intercept sprintf for the connect string
Utils::Hook(0x48ADAB, Bots::BuildConnectString, HOOK_CALL).install()->quick();
2017-04-23 07:31:48 -04:00
// Stop default behavour of bots spinning and shooting
Utils::Hook(0x627021, 0x4BB9B0, HOOK_CALL).install()->quick();
Utils::Hook(0x627241, 0x4BB9B0, HOOK_CALL).install()->quick();
2022-01-07 16:00:44 -05:00
// Zero the bot command array
for (auto i = 0; i < MAX_G_BOTAI_ENTRIES; i++)
{
2022-01-07 16:00:44 -05:00
g_botai[i] = {0};
g_botai[i].weapon = 1; // Prevent the bots from defaulting to the 'none' weapon
}
2022-01-07 16:00:44 -05:00
// Have the bots perform the command every server frame
Scheduler::OnFrame([]()
{
if (!Game::SV_Loaded())
return;
2022-01-07 16:00:44 -05:00
for (auto i = 0; i < *Game::svs_numclients; ++i)
{
2022-01-07 16:00:44 -05:00
auto* client = &Game::svs_clients[i];
if (client->state < Game::CS_CONNECTED)
continue;
if (!client->isBot)
continue;
2022-01-07 16:00:44 -05:00
Game::usercmd_s ucmd = {0};
2022-01-07 16:00:44 -05:00
ucmd.serverTime = *Game::svs_time;
ucmd.buttons = g_botai[i].buttons;
ucmd.forwardmove = g_botai[i].forward;
ucmd.rightmove = g_botai[i].right;
ucmd.weapon = g_botai[i].weapon;
2021-09-08 05:19:30 -04:00
client->deltaMessage = client->netchan.outgoingSequence - 1;
Game::SV_ClientThink(client, &ucmd);
}
});
2017-04-23 07:31:48 -04:00
Command::Add("spawnBot", [](Command::Params* params)
{
2022-01-07 16:00:44 -05:00
auto count = 1;
2017-04-23 07:31:48 -04:00
if (params->length() > 1)
{
2022-01-07 16:00:44 -05:00
if (params->get(1) == "all"s)
count = *Game::svs_numclients;
else
count = std::atoi(params->get(1));
2017-04-23 07:31:48 -04:00
}
2022-01-07 16:00:44 -05:00
count = std::min(*Game::svs_numclients, count);
2017-04-24 15:14:08 -04:00
2017-04-23 07:31:48 -04:00
// Check if ingame and host
if (!Game::SV_Loaded())
{
Toast::Show("cardicon_headshot", "^1Error", "You need to be host to spawn bots!", 3000);
Logger::Print("You need to be host to spawn bots!\n");
2017-04-24 15:14:08 -04:00
return;
2017-04-23 07:31:48 -04:00
}
2017-04-24 15:14:08 -04:00
Toast::Show("cardicon_headshot", "^2Success", Utils::String::VA("Spawning %d %s...", count, (count == 1 ? "bot" : "bots")), 3000);
Logger::Print("Spawning %d %s...\n", count, (count == 1 ? "bot" : "bots"));
Bots::Spawn(count);
2017-04-23 07:31:48 -04:00
});
2020-11-14 03:58:05 -05:00
Bots::AddMethods();
2017-01-19 16:23:59 -05:00
}
Bots::~Bots()
{
Bots::BotNames.clear();
}
}