openmohaa/code/fgame/g_bot.cpp

287 lines
6.3 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
2023-08-19 16:03:42 +02:00
static gentity_t* firstBot = NULL;
2023-08-19 17:58:54 +02:00
static saved_bot_t* saved_bots = NULL;
static unsigned int current_bot_count = 0;
2023-08-19 16:03:42 +02:00
2016-03-27 11:49:47 +02:00
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 );
}
2023-08-19 16:03:42 +02:00
gentity_t* G_GetFirstBot()
{
return firstBot;
}
2023-08-19 17:58:54 +02:00
void G_AddBot(unsigned int num, saved_bot_t* saved)
2023-08-19 16:03:42 +02:00
{
int n;
int i;
int clientNum = -1;
gentity_t *e;
char botName[ MAX_NETNAME ];
char challenge[ MAX_STRING_TOKENS ];
2023-08-19 17:58:54 +02:00
char userinfo[MAX_INFO_STRING]{ 0 };
Event* teamEv;
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
num = Q_min(num, sv_maxbots->integer);
2023-08-19 16:03:42 +02:00
for( n = 0; n < num; n++ )
{
for( i = maxclients->integer; i < game.maxclients; i++ )
{
e = &g_entities[ i ];
if( !e->inuse && e->client )
{
clientNum = i;
break;
}
}
if( clientNum == -1 )
{
gi.Printf( "No free slot for a bot\n" );
return;
}
if( gi.Argc() > 2 )
{
Q_strncpyz( botName, gi.Argv( 2 ), sizeof( botName ) );
}
else
{
2024-09-20 21:53:48 +02:00
Com_sprintf( botName, sizeof( botName ), "bot%d", clientNum - maxclients->integer + 1 );
2023-08-19 16:03:42 +02:00
}
2024-09-20 21:53:48 +02:00
Com_sprintf( challenge, sizeof( challenge ), "%d", clientNum - maxclients->integer + 1 );
2023-08-19 16:03:42 +02:00
e->s.clientNum = clientNum;
e->s.number = clientNum;
2023-08-19 17:58:54 +02:00
if (saved) {
strncpy(userinfo, saved->pers.userinfo, ARRAY_LEN(userinfo));
} else {
Info_SetValueForKey(userinfo, "name", botName);
Info_SetValueForKey(userinfo, "dm_playermodel", "allied_pilot");
Info_SetValueForKey(userinfo, "dm_playergermanmodel", "german_afrika_officer");
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");
}
current_bot_count++;
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
G_BotConnect( clientNum, userinfo );
if (saved) {
e->client->pers = saved->pers;
}
2023-08-19 16:03:42 +02:00
if( !firstBot )
firstBot = e;
G_BotBegin( e );
2023-08-19 17:58:54 +02:00
if (saved) {
/*
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;
}
*/
} else {
teamEv = new Event(EV_Player_AutoJoinDMTeam);
e->entity->PostEvent(teamEv, level.frametime);
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
Event* ev = new Event(EV_Player_PrimaryDMWeapon);
ev->AddString("smg");
2023-08-19 16:03:42 +02:00
2023-08-19 17:58:54 +02:00
e->entity->PostEvent(ev, level.frametime);
}
2023-08-19 16:03:42 +02:00
}
}
void G_RemoveBot(unsigned int num)
{
2024-04-09 21:43:56 +02:00
num = Q_min(num, sv_maxbots->integer);
2023-08-19 16:03:42 +02:00
for( int n = 0; n < num; n++ )
{
2024-04-09 21:43:56 +02:00
gentity_t *e = &g_entities[maxclients->integer + n];
2023-08-19 16:03:42 +02:00
if( e->inuse && e->client )
{
G_ClientDisconnect( e );
2023-08-19 17:58:54 +02:00
current_bot_count--;
2023-08-19 16:03:42 +02:00
}
}
}
2023-08-19 17:58:54 +02:00
void G_SaveBots() {
unsigned int n;
if (saved_bots) {
delete[] saved_bots;
saved_bots = NULL;
}
if (!current_bot_count) {
return;
}
saved_bots = new saved_bot_t[current_bot_count];
for (n = 0; n < current_bot_count; n++) {
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;
}
}
}
void G_RestoreBots() {
unsigned int n;
if (!saved_bots) {
return;
}
for (n = 0; n < sv_numbots->integer; n++) {
saved_bot_t& saved = saved_bots[n];
G_AddBot(1, &saved);
}
delete[] saved_bots;
saved_bots = NULL;
}
2024-04-09 21:43:56 +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++;
}
}
return count;
}
2023-08-19 17:58:54 +02:00
void G_ResetBots() {
G_SaveBots();
current_bot_count = 0;
}
void G_SpawnBots() {
2024-04-09 21:43:56 +02:00
unsigned int numClients;
unsigned int numBotsToSpawn;
2023-08-19 17:58:54 +02:00
if (saved_bots) {
G_RestoreBots();
}
2024-04-09 21:43:56 +02:00
//
// 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
}
}