openmohaa/code/fgame/g_bot.cpp

372 lines
9.1 KiB
C++
Raw Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
2023-08-19 17:58:54 +02:00
Copyright (C) 2023 the OpenMoHAA team
2016-03-27 11:49:47 +02:00
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// g_bot.cpp
#include "g_local.h"
#include "entity.h"
#include "playerbot.h"
2023-08-19 17:58:54 +02:00
#include "g_bot.h"
2016-03-27 11:49:47 +02:00
2024-10-02 19:42:03 +02:00
static gentity_t *firstBot = NULL;
static saved_bot_t *saved_bots = NULL;
2023-08-19 17:58:54 +02:00
static unsigned int current_bot_count = 0;
2024-10-02 19:42:03 +02:00
static char **modelList = NULL;
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
Container<str> alliedModelList;
Container<str> germanModelList;
bool IsAlliedPlayerModel(const char *filename)
2016-03-27 11:49:47 +02:00
{
2024-10-02 19:42:03 +02:00
return !Q_stricmpn(filename, "/allied_", 8) || !Q_stricmpn(filename, "/american_", 10);
2016-03-27 11:49:47 +02:00
}
2024-10-02 19:42:03 +02:00
bool IsGermanPlayerModel(const char *filename)
2016-03-27 11:49:47 +02:00
{
2024-10-02 19:42:03 +02:00
return !Q_stricmpn(filename, "/german_", 8) || !Q_stricmpn(filename, "/IT_", 4) || !Q_stricmpn(filename, "/SC_", 4);
}
2016-03-27 11:49:47 +02:00
2024-10-02 19:42:03 +02:00
bool IsPlayerModel(const char *filename)
{
size_t len = strlen(filename);
2016-03-27 11:49:47 +02:00
2024-10-02 19:42:03 +02:00
if (len >= 8 && !Q_stricmp(&filename[len - 8], "_fps.tik")) {
return false;
}
2016-03-27 11:49:47 +02:00
2024-10-02 19:42:03 +02:00
if (!IsAlliedPlayerModel(filename) && !IsGermanPlayerModel(filename)) {
return false;
}
2016-03-27 11:49:47 +02:00
2024-10-02 19:42:03 +02:00
return true;
2016-03-27 11:49:47 +02:00
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
void ClearModelList()
2023-08-19 16:03:42 +02:00
{
2024-10-02 19:42:03 +02:00
alliedModelList.FreeObjectList();
germanModelList.FreeObjectList();
2023-08-19 16:03:42 +02:00
}
2024-10-02 19:42:03 +02:00
void InitModelList()
2023-08-19 16:03:42 +02:00
{
2024-10-02 19:42:03 +02:00
char **fileList;
int numFiles;
int i;
size_t numAlliedModels = 0, numGermanModels = 0;
byte *p;
ClearModelList();
fileList = gi.FS_ListFiles("models/player", ".tik", qfalse, &numFiles);
for (i = 0; i < numFiles; i++) {
const char *filename = fileList[i];
if (!IsPlayerModel(filename)) {
continue;
}
if (IsAlliedPlayerModel(filename)) {
numAlliedModels++;
} else {
numGermanModels++;
}
}
alliedModelList.Resize(numAlliedModels);
germanModelList.Resize(numGermanModels);
for (i = 0; i < numFiles; i++) {
const char *filename = fileList[i];
size_t len = strlen(filename);
if (!IsPlayerModel(filename)) {
continue;
}
if (IsAlliedPlayerModel(filename)) {
alliedModelList.AddObject(str(filename + 1, 0, len - 5));
} else {
germanModelList.AddObject(str(filename + 1, 0, len - 5));
}
}
gi.FS_FreeFileList(fileList);
}
void G_BotBegin(gentity_t *ent)
{
level.m_bSpawnBot = true;
G_ClientBegin(ent, NULL);
}
void G_BotThink(gentity_t *ent, int msec)
{
usercmd_t ucmd;
usereyes_t eyeinfo;
PlayerBot *bot;
assert(ent);
assert(ent->entity);
assert(ent->entity->IsSubclassOfBot());
bot = (PlayerBot *)ent->entity;
bot->UpdateBotStates();
bot->GetUsercmd(&ucmd);
bot->GetEyeInfo(&eyeinfo);
G_ClientThink(ent, &ucmd, &eyeinfo);
}
gentity_t *G_GetFirstBot()
{
return firstBot;
}
void G_AddBot(unsigned int num, saved_bot_t *saved)
{
int n;
int i;
int clientNum = -1;
gentity_t *e;
char botName[MAX_NETNAME];
char challenge[MAX_STRING_TOKENS];
Event *teamEv;
num = Q_min(num, sv_maxbots->integer);
for (n = 0; n < num; n++) {
char userinfo[MAX_INFO_STRING] {0};
for (i = maxclients->integer; i < game.maxclients; i++) {
e = &g_entities[i];
if (!e->inuse && e->client) {
clientNum = i;
break;
}
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
if (clientNum == -1) {
gi.Printf("No free slot for a bot\n");
return;
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
if (gi.Argc() > 2) {
Q_strncpyz(botName, gi.Argv(2), sizeof(botName));
} else {
Com_sprintf(botName, sizeof(botName), "bot%d", clientNum - maxclients->integer + 1);
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
Com_sprintf(challenge, sizeof(challenge), "%d", clientNum - maxclients->integer + 1);
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
e->s.clientNum = clientNum;
e->s.number = clientNum;
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
if (saved) {
2024-10-02 19:42:03 +02:00
strncpy(userinfo, saved->pers.userinfo, ARRAY_LEN(userinfo));
} else {
2023-08-19 17:58:54 +02:00
Info_SetValueForKey(userinfo, "name", botName);
2024-10-02 19:42:03 +02:00
//
// Choose a random model
//
if (alliedModelList.NumObjects()) {
Info_SetValueForKey(userinfo, "dm_playermodel", alliedModelList[rand() % alliedModelList.NumObjects()]);
}
if (germanModelList.NumObjects()) {
Info_SetValueForKey(
userinfo, "dm_playergermanmodel", germanModelList[rand() % germanModelList.NumObjects()]
);
}
2023-08-19 17:58:54 +02:00
Info_SetValueForKey(userinfo, "fov", "80");
Info_SetValueForKey(userinfo, "protocol", "8");
Info_SetValueForKey(userinfo, "ip", "0.0.0.0");
Info_SetValueForKey(userinfo, "qport", "0");
Info_SetValueForKey(userinfo, "challenge", challenge);
Info_SetValueForKey(userinfo, "snaps", "1");
Info_SetValueForKey(userinfo, "rate", "1");
Info_SetValueForKey(userinfo, "dmprimary", "smg");
2024-10-02 19:42:03 +02:00
}
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
current_bot_count++;
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
G_BotConnect(clientNum, userinfo);
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
if (saved) {
e->client->pers = saved->pers;
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
if (!firstBot) {
firstBot = e;
}
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
G_BotBegin(e);
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
if (saved) {
/*
2023-08-19 17:58:54 +02:00
switch (saved->team)
{
case TEAM_ALLIES:
teamEv = new Event(EV_Player_JoinDMTeam);
teamEv->AddString("allies");
break;
case TEAM_AXIS:
teamEv = new Event(EV_Player_JoinDMTeam);
teamEv->AddString("axis");
break;
default:
teamEv = new Event(EV_Player_AutoJoinDMTeam);
break;
}
*/
2024-10-02 19:42:03 +02:00
} else {
2023-08-19 17:58:54 +02:00
teamEv = new Event(EV_Player_AutoJoinDMTeam);
e->entity->PostEvent(teamEv, level.frametime);
2023-08-19 16:03:42 +02:00
2024-10-02 19:42:03 +02:00
Event *ev = new Event(EV_Player_PrimaryDMWeapon);
ev->AddString("auto");
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
e->entity->PostEvent(ev, level.frametime);
}
2024-10-02 19:42:03 +02:00
}
2023-08-19 16:03:42 +02:00
}
void G_RemoveBot(unsigned int num)
{
2024-10-02 19:42:03 +02:00
num = Q_min(num, sv_maxbots->integer);
for (int n = 0; n < num; n++) {
gentity_t *e = &g_entities[maxclients->integer + n];
if (e->inuse && e->client) {
G_ClientDisconnect(e);
current_bot_count--;
}
}
2023-08-19 16:03:42 +02:00
}
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
void G_SaveBots()
{
unsigned int n;
2023-08-19 17:58:54 +02:00
if (saved_bots) {
delete[] saved_bots;
2024-10-02 19:42:03 +02:00
saved_bots = NULL;
2023-08-19 17:58:54 +02:00
}
2024-10-02 19:42:03 +02:00
if (!current_bot_count) {
return;
}
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
saved_bots = new saved_bot_t[current_bot_count];
2023-08-19 17:58:54 +02:00
for (n = 0; n < current_bot_count; n++) {
2024-10-02 19:42:03 +02:00
gentity_t *e = &g_entities[game.maxclients - sv_maxbots->integer + n];
saved_bot_t& saved = saved_bots[n];
if (e->inuse && e->client) {
Player *player = static_cast<Player *>(e->entity);
saved.bValid = true;
//saved.team = player->GetTeam();
saved.pers = player->client->pers;
}
}
2023-08-19 17:58:54 +02:00
}
2024-10-02 19:42:03 +02:00
void G_RestoreBots()
{
unsigned int n;
2023-08-19 17:58:54 +02:00
if (!saved_bots) {
2024-10-02 19:42:03 +02:00
return;
2023-08-19 17:58:54 +02:00
}
for (n = 0; n < sv_numbots->integer; n++) {
saved_bot_t& saved = saved_bots[n];
2024-10-02 19:42:03 +02:00
G_AddBot(1, &saved);
}
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
delete[] saved_bots;
saved_bots = NULL;
2023-08-19 17:58:54 +02:00
}
2024-10-02 19:42:03 +02:00
int G_CountClients()
{
gentity_t *other;
unsigned int n;
unsigned int count = 0;
for (n = 0; n < maxclients->integer; n++) {
other = &g_entities[n];
if (other->inuse && other->client) {
Player *p = static_cast<Player *>(other->entity);
if (p->GetTeam() == teamtype_t::TEAM_NONE || p->GetTeam() == teamtype_t::TEAM_SPECTATOR) {
// ignore spectators
continue;
}
count++;
}
}
2024-04-09 21:43:56 +02:00
2024-10-02 19:42:03 +02:00
return count;
2024-04-09 21:43:56 +02:00
}
2024-10-02 19:42:03 +02:00
void G_ResetBots()
{
G_SaveBots();
2023-08-19 17:58:54 +02:00
2024-10-02 19:42:03 +02:00
current_bot_count = 0;
2023-08-19 17:58:54 +02:00
}
2024-10-02 19:42:03 +02:00
void G_SpawnBots()
{
unsigned int numClients;
unsigned int numBotsToSpawn;
InitModelList();
if (saved_bots) {
G_RestoreBots();
}
//
// Check the minimum bot count
//
numClients = G_CountClients() + sv_numbots->integer;
if (numClients < sv_minPlayers->integer) {
numBotsToSpawn = sv_minPlayers->integer - numClients;
} else {
numBotsToSpawn = sv_numbots->integer;
}
//
// Spawn bots
//
if (numBotsToSpawn > current_bot_count) {
G_AddBot(numBotsToSpawn - current_bot_count);
} else if (numBotsToSpawn < current_bot_count) {
G_RemoveBot(current_bot_count - numBotsToSpawn);
}
2023-08-19 17:58:54 +02:00
}