spingle/source/host.c

932 lines
21 KiB
C
Raw Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2007-2008 Kristian Duske
Copyright (C) 2010-2014 QuakeSpasm developers
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// host.c -- coordinates spawning and killing of local servers
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
#include "bgmusic.h"
#include <setjmp.h>
/*
A server can allways be started, even if the system started out as a client
to a remote system.
A client can NOT be started if the system started as a dedicated server.
Memory is cleared / released when a server or client begins, not when they end.
*/
quakeparms_t *host_parms;
2019-11-25 17:40:18 -08:00
bool host_initialized; // true if into command execution
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
double host_frametime;
double realtime; // without any filtering or bounding
double oldrealtime; // last frame run
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
int32_t host_framecount;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
int32_t host_hunklevel;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
client_t *host_client; // current client
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
jmp_buf host_abortserver;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
byte *host_colormap;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t host_framerate = {"host_framerate", "0", CVAR_NONE}; // set for slow motion
cvar_t host_speeds = {"host_speeds", "0", CVAR_NONE}; // set for running times
cvar_t host_maxfps = {"host_maxfps", "72", CVAR_ARCHIVE}; //johnfitz
cvar_t host_timescale = {"host_timescale", "0", CVAR_NONE}; //johnfitz
cvar_t max_edicts = {"max_edicts", "8192", CVAR_NONE}; //johnfitz //ericw -- changed from 2048 to 8192, removed CVAR_ARCHIVE
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t sys_ticrate = {"sys_ticrate", "0.05", CVAR_NONE}; // dedicated server
cvar_t serverprofile = {"serverprofile", "0", CVAR_NONE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t fraglimit = {"fraglimit", "0", CVAR_NOTIFY | CVAR_SERVERINFO};
cvar_t timelimit = {"timelimit", "0", CVAR_NOTIFY | CVAR_SERVERINFO};
cvar_t teamplay = {"teamplay", "0", CVAR_NOTIFY | CVAR_SERVERINFO};
cvar_t samelevel = {"samelevel", "0", CVAR_NONE};
cvar_t noexit = {"noexit", "0", CVAR_NOTIFY | CVAR_SERVERINFO};
cvar_t skill = {"skill", "1", CVAR_NONE}; // 0 - 3
cvar_t deathmatch = {"deathmatch", "0", CVAR_NONE}; // 0, 1, or 2
cvar_t coop = {"coop", "0", CVAR_NONE}; // 0 or 1
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t pausable = {"pausable", "1", CVAR_NONE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t developer = {"developer", "0", CVAR_NONE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t devstats = {"devstats", "0", CVAR_NONE}; //johnfitz -- track developer statistics that vary every frame
2019-11-24 20:45:15 -08:00
devstats_t dev_stats, dev_peakstats;
overflowtimes_t dev_overflows; //this stores the last time overflow messages were displayed, not the last time overflows occured
/*
================
Max_Edicts_f -- johnfitz
================
*/
2019-11-25 17:40:18 -08:00
static void Max_Edicts_f(cvar_t *var)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:20:03 -08:00
(void)var;
2019-11-24 20:45:15 -08:00
//TODO: clamp it here?
2019-11-25 17:40:18 -08:00
if(cls.state == ca_connected || sv.active)
Con_Printf("Changes to max_edicts will not take effect until the next time a map is loaded.\n");
2019-11-24 20:45:15 -08:00
}
/*
================
Max_Fps_f -- ericw
================
*/
2019-11-25 17:40:18 -08:00
static void Max_Fps_f(cvar_t *var)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(var->value > 72)
Con_Warning("host_maxfps above 72 breaks physics.\n");
2019-11-24 20:45:15 -08:00
}
/*
================
Host_EndGame
================
*/
2019-11-25 17:40:18 -08:00
void Host_EndGame(const char *message, ...)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
va_list argptr;
char string[1024];
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
va_start(argptr, message);
q_vsnprintf(string, sizeof(string), message, argptr);
va_end(argptr);
Con_DPrintf("Host_EndGame: %s\n", string);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sv.active)
Host_ShutdownServer(false);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.state == ca_dedicated)
Sys_Error("Host_EndGame: %s\n", string); // dedicated servers exit
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.demonum != -1)
CL_NextDemo();
2019-11-24 20:45:15 -08:00
else
2019-11-25 17:40:18 -08:00
CL_Disconnect();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
longjmp(host_abortserver, 1);
2019-11-24 20:45:15 -08:00
}
/*
================
Host_Error
This shuts down both the client and server
================
*/
2019-11-25 17:40:18 -08:00
void Host_Error(const char *error, ...)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
va_list argptr;
char string[1024];
static bool inerror = false;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(inerror)
Sys_Error("Host_Error: recursively entered");
2019-11-24 20:45:15 -08:00
inerror = true;
2019-11-25 17:40:18 -08:00
SCR_EndLoadingPlaque(); // reenable screen updates
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
va_start(argptr, error);
q_vsnprintf(string, sizeof(string), error, argptr);
va_end(argptr);
Con_Printf("Host_Error: %s\n", string);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sv.active)
Host_ShutdownServer(false);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.state == ca_dedicated)
Sys_Error("Host_Error: %s\n", string); // dedicated servers exit
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
CL_Disconnect();
2019-11-24 20:45:15 -08:00
cls.demonum = -1;
cl.intermission = 0; //johnfitz -- for errors during intermissions (changelevel with no map found, etc.)
inerror = false;
2019-11-25 17:40:18 -08:00
longjmp(host_abortserver, 1);
2019-11-24 20:45:15 -08:00
}
/*
================
Host_FindMaxClients
================
*/
2019-11-25 17:40:18 -08:00
void Host_FindMaxClients(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
2019-11-24 20:45:15 -08:00
svs.maxclients = 1;
2019-11-25 17:40:18 -08:00
i = COM_CheckParm("-dedicated");
if(i)
2019-11-24 20:45:15 -08:00
{
cls.state = ca_dedicated;
2019-11-25 17:40:18 -08:00
if(i != (com_argc - 1))
2019-11-24 20:45:15 -08:00
{
2019-12-07 09:27:26 -08:00
svs.maxclients = atoi(com_argv[i + 1]);
2019-11-24 20:45:15 -08:00
}
else
svs.maxclients = 8;
}
else
cls.state = ca_disconnected;
2019-11-25 17:40:18 -08:00
i = COM_CheckParm("-listen");
if(i)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cls.state == ca_dedicated)
Sys_Error("Only one of -dedicated or -listen can be specified");
if(i != (com_argc - 1))
2019-12-07 09:27:26 -08:00
svs.maxclients = atoi(com_argv[i + 1]);
2019-11-24 20:45:15 -08:00
else
svs.maxclients = 8;
}
2019-11-25 17:40:18 -08:00
if(svs.maxclients < 1)
2019-11-24 20:45:15 -08:00
svs.maxclients = 8;
2019-11-25 17:40:18 -08:00
else if(svs.maxclients > MAX_SCOREBOARD)
2019-11-24 20:45:15 -08:00
svs.maxclients = MAX_SCOREBOARD;
svs.maxclientslimit = svs.maxclients;
2019-11-25 17:40:18 -08:00
if(svs.maxclientslimit < 4)
2019-11-24 20:45:15 -08:00
svs.maxclientslimit = 4;
2019-11-25 17:40:18 -08:00
svs.clients = (struct client_s *) Hunk_AllocName(svs.maxclientslimit * sizeof(client_t), "clients");
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(svs.maxclients > 1)
Cvar_SetQuick(&deathmatch, "1");
2019-11-24 20:45:15 -08:00
else
2019-11-25 17:40:18 -08:00
Cvar_SetQuick(&deathmatch, "0");
2019-11-24 20:45:15 -08:00
}
2019-12-02 07:01:47 -08:00
static void PrintExeTime(void)
{
Con_Printf("Compiled on " __DATE__ " " __TIME__ "\n");
}
2019-11-25 17:40:18 -08:00
void Host_Version_f(void)
2019-11-24 20:45:15 -08:00
{
2019-12-02 07:01:47 -08:00
Con_Printf(ENGINE_NAME " Version " VERSION "\n");
PrintExeTime();
2019-11-24 20:45:15 -08:00
}
/* cvar callback functions : */
2019-11-25 17:40:18 -08:00
void Host_Callback_Notify(cvar_t *var)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(sv.active)
SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
2019-11-24 20:45:15 -08:00
}
/*
=======================
Host_InitLocal
======================
*/
2019-11-25 17:40:18 -08:00
void Host_InitLocal(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cmd_AddCommand("version", Host_Version_f);
Host_InitCommands();
Cvar_RegisterVariable(&host_framerate);
Cvar_RegisterVariable(&host_speeds);
Cvar_RegisterVariable(&host_maxfps); //johnfitz
Cvar_SetCallback(&host_maxfps, Max_Fps_f);
Cvar_RegisterVariable(&host_timescale); //johnfitz
Cvar_RegisterVariable(&max_edicts); //johnfitz
Cvar_SetCallback(&max_edicts, Max_Edicts_f);
Cvar_RegisterVariable(&devstats); //johnfitz
Cvar_RegisterVariable(&sys_ticrate);
Cvar_RegisterVariable(&sys_throttle);
Cvar_RegisterVariable(&serverprofile);
Cvar_RegisterVariable(&fraglimit);
Cvar_RegisterVariable(&timelimit);
Cvar_RegisterVariable(&teamplay);
Cvar_SetCallback(&fraglimit, Host_Callback_Notify);
Cvar_SetCallback(&timelimit, Host_Callback_Notify);
Cvar_SetCallback(&teamplay, Host_Callback_Notify);
Cvar_RegisterVariable(&samelevel);
Cvar_RegisterVariable(&noexit);
Cvar_SetCallback(&noexit, Host_Callback_Notify);
Cvar_RegisterVariable(&skill);
Cvar_RegisterVariable(&developer);
Cvar_RegisterVariable(&coop);
Cvar_RegisterVariable(&deathmatch);
Cvar_RegisterVariable(&pausable);
Host_FindMaxClients();
2019-11-24 20:45:15 -08:00
}
/*
===============
Host_WriteConfiguration
Writes key bindings and archived cvars to config.cfg
===============
*/
2019-11-25 17:40:18 -08:00
void Host_WriteConfiguration(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
FILE *f;
2019-11-24 20:45:15 -08:00
// dedicated servers initialize the host but don't parse and set the
// config.cfg cvars
2019-11-25 17:40:18 -08:00
if(host_initialized && !isDedicated && !host_parms->errstate)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
f = fopen(va("%s/config.cfg", com_gamedir), "w");
if(!f)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("Couldn't write config.cfg.\n");
2019-11-24 20:45:15 -08:00
return;
}
//VID_SyncCvars (); //johnfitz -- write actual current mode to config file, in case cvars were messed with
2019-11-25 17:40:18 -08:00
Key_WriteBindings(f);
Cvar_WriteVariables(f);
2019-11-24 20:45:15 -08:00
//johnfitz -- extra commands to preserve state
2019-11-25 17:40:18 -08:00
fprintf(f, "vid_restart\n");
if(in_mlook.state & 1) fprintf(f, "+mlook\n");
2019-11-24 20:45:15 -08:00
//johnfitz
2019-11-25 17:40:18 -08:00
fclose(f);
2019-11-24 20:45:15 -08:00
}
}
/*
=================
SV_ClientPrintf
Sends text across to be displayed
FIXME: make this just a stuffed echo?
=================
*/
2019-11-25 17:40:18 -08:00
void SV_ClientPrintf(const char *fmt, ...)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
va_list argptr;
char string[1024];
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
va_start(argptr, fmt);
q_vsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&host_client->message, svc_print);
MSG_WriteString(&host_client->message, string);
2019-11-24 20:45:15 -08:00
}
/*
=================
SV_BroadcastPrintf
Sends text to all active clients
=================
*/
2019-11-25 17:40:18 -08:00
void SV_BroadcastPrintf(const char *fmt, ...)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
va_list argptr;
char string[1024];
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
va_start(argptr, fmt);
q_vsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0; i < svs.maxclients; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(svs.clients[i].active && svs.clients[i].spawned)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&svs.clients[i].message, svc_print);
MSG_WriteString(&svs.clients[i].message, string);
2019-11-24 20:45:15 -08:00
}
}
}
/*
=================
Host_ClientCommands
Send text over to the client to be executed
=================
*/
2019-11-25 17:40:18 -08:00
void Host_ClientCommands(const char *fmt, ...)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
va_list argptr;
char string[1024];
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
va_start(argptr, fmt);
q_vsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&host_client->message, svc_stufftext);
MSG_WriteString(&host_client->message, string);
2019-11-24 20:45:15 -08:00
}
/*
=====================
SV_DropClient
Called when the player is getting totally kicked off the host
if (crash = true), don't bother sending signofs
=====================
*/
2019-11-25 17:40:18 -08:00
void SV_DropClient(bool crash)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t saveSelf;
int32_t i;
2019-11-24 20:45:15 -08:00
client_t *client;
2019-11-25 17:40:18 -08:00
if(!crash)
2019-11-24 20:45:15 -08:00
{
// send any final messages (don't check for errors)
2019-11-25 17:40:18 -08:00
if(NET_CanSendMessage(host_client->netconnection))
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&host_client->message, svc_disconnect);
NET_SendMessage(host_client->netconnection, &host_client->message);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
if(host_client->edict && host_client->spawned)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
// call the prog function for removing a client
// this will set the body to a dead frame, among other things
saveSelf = G_PEdict(GBL_self);
G_PEdict(GBL_self) = EdictProg(host_client->edict);
PR_ExecuteProgram(G_Func(GBL_ClientDisconnect));
G_PEdict(GBL_self) = saveSelf;
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
Sys_Printf("Client %s removed\n", host_client->name);
2019-11-24 20:45:15 -08:00
}
// break the net connection
2019-11-25 17:40:18 -08:00
NET_Close(host_client->netconnection);
2019-11-24 20:45:15 -08:00
host_client->netconnection = NULL;
// free the client (the body stays around)
host_client->active = false;
host_client->name[0] = 0;
host_client->old_frags = -999999;
net_activeconnections--;
// send notification to all clients
2019-11-25 17:40:18 -08:00
for(i = 0, client = svs.clients; i < svs.maxclients; i++, client++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!client->active)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&client->message, svc_updatename);
MSG_WriteByte(&client->message, host_client - svs.clients);
MSG_WriteString(&client->message, "");
MSG_WriteByte(&client->message, svc_updatefrags);
MSG_WriteByte(&client->message, host_client - svs.clients);
MSG_WriteShort(&client->message, 0);
MSG_WriteByte(&client->message, svc_updatecolors);
MSG_WriteByte(&client->message, host_client - svs.clients);
MSG_WriteByte(&client->message, 0);
2019-11-24 20:45:15 -08:00
}
}
/*
==================
Host_ShutdownServer
This only happens at the end of a game, not between levels
==================
*/
2019-11-25 15:28:38 -08:00
void Host_ShutdownServer(bool crash)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
int32_t count;
sizebuf_t buf;
byte message[4];
double start;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sv.active)
2019-11-24 20:45:15 -08:00
return;
sv.active = false;
// stop all client sounds immediately
2019-11-25 17:40:18 -08:00
if(cls.state == ca_connected)
CL_Disconnect();
2019-11-24 20:45:15 -08:00
// flush any pending messages - like the score!!!
start = Sys_DoubleTime();
do
{
count = 0;
2019-11-25 17:40:18 -08:00
for(i = 0, host_client = svs.clients ; i < svs.maxclients ; i++, host_client++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(host_client->active && host_client->message.cursize)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(NET_CanSendMessage(host_client->netconnection))
2019-11-24 20:45:15 -08:00
{
NET_SendMessage(host_client->netconnection, &host_client->message);
2019-11-25 17:40:18 -08:00
SZ_Clear(&host_client->message);
2019-11-24 20:45:15 -08:00
}
else
{
NET_GetMessage(host_client->netconnection);
count++;
}
}
}
2019-11-25 17:40:18 -08:00
if((Sys_DoubleTime() - start) > 3.0)
2019-11-24 20:45:15 -08:00
break;
}
2019-11-25 17:40:18 -08:00
while(count);
2019-11-24 20:45:15 -08:00
// make sure all the clients know we're disconnecting
buf.data = message;
buf.maxsize = 4;
buf.cursize = 0;
MSG_WriteByte(&buf, svc_disconnect);
count = NET_SendToAll(&buf, 5.0);
2019-11-25 17:40:18 -08:00
if(count)
2019-11-25 17:29:41 -08:00
Con_Printf("Host_ShutdownServer: NET_SendToAll failed for %" PRIu32 " clients\n", count);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++)
if(host_client->active)
2019-11-24 20:45:15 -08:00
SV_DropClient(crash);
//
// clear structures
//
// memset (&sv, 0, sizeof(sv)); // ServerSpawn already do this by Host_ClearMemory
2019-11-25 17:40:18 -08:00
memset(svs.clients, 0, svs.maxclientslimit * sizeof(client_t));
2019-11-24 20:45:15 -08:00
}
/*
================
Host_ClearMemory
This clears all the memory used by both the client and server, but does
not reinitialize anything.
================
*/
2019-11-25 17:40:18 -08:00
void Host_ClearMemory(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_DPrintf("Clearing memory\n");
D_FlushCaches();
Mod_ClearAll();
/* host_hunklevel MUST be set at this point */
Hunk_FreeToLowMark(host_hunklevel);
2019-11-24 20:45:15 -08:00
cls.signon = 0;
free(sv.edicts); // ericw -- sv.edicts switched to use malloc()
2019-11-25 17:40:18 -08:00
memset(&sv, 0, sizeof(sv));
memset(&cl, 0, sizeof(cl));
2019-11-24 20:45:15 -08:00
}
//==============================================================================
//
// Host Frame
//
//==============================================================================
/*
===================
Host_FilterTime
Returns false if the time is too short to run a frame
===================
*/
2019-11-25 17:40:18 -08:00
bool Host_FilterTime(float time)
2019-11-24 20:45:15 -08:00
{
float maxfps; //johnfitz
realtime += time;
//johnfitz -- max fps cvar
2019-11-25 17:40:18 -08:00
maxfps = CLAMP(10.0, host_maxfps.value, 1000.0);
if(!cls.timedemo && realtime - oldrealtime < 1.0 / maxfps)
2019-11-24 20:45:15 -08:00
return false; // framerate is too high
//johnfitz
host_frametime = realtime - oldrealtime;
oldrealtime = realtime;
//johnfitz -- host_timescale is more intuitive than host_framerate
2019-11-25 17:40:18 -08:00
if(host_timescale.value > 0)
2019-11-24 20:45:15 -08:00
host_frametime *= host_timescale.value;
//johnfitz
2019-11-25 17:40:18 -08:00
else if(host_framerate.value > 0)
2019-11-24 20:45:15 -08:00
host_frametime = host_framerate.value;
else // don't allow really long or short frames
2019-11-25 17:40:18 -08:00
host_frametime = CLAMP(0.001, host_frametime, 0.1); //johnfitz -- use CLAMP
2019-11-24 20:45:15 -08:00
return true;
}
/*
===================
Host_GetConsoleCommands
Add them exactly as if they had been typed at the console
===================
*/
2019-11-25 17:40:18 -08:00
void Host_GetConsoleCommands(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
const char *cmd;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!isDedicated)
return; // no stdin necessary in graphical mode
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
while(1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
cmd = Sys_ConsoleInput();
if(!cmd)
2019-11-24 20:45:15 -08:00
break;
2019-11-25 17:40:18 -08:00
Cbuf_AddText(cmd);
2019-11-24 20:45:15 -08:00
}
}
/*
==================
Host_ServerFrame
==================
*/
2019-11-25 17:40:18 -08:00
void Host_ServerFrame(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i, active; //johnfitz
edict_t *ent; //johnfitz
2019-11-24 20:45:15 -08:00
// run the world state
G_Float(GBL_frametime) = host_frametime;
2019-11-24 20:45:15 -08:00
// set the time and clear the general datagram
2019-11-25 17:40:18 -08:00
SV_ClearDatagram();
2019-11-24 20:45:15 -08:00
// check for new clients
2019-11-25 17:40:18 -08:00
SV_CheckForNewClients();
2019-11-24 20:45:15 -08:00
// read client messages
2019-11-25 17:40:18 -08:00
SV_RunClients();
2019-11-24 20:45:15 -08:00
// move things around and think
// always pause in single player if in console or menus
2019-11-25 17:40:18 -08:00
if(!sv.paused && (svs.maxclients > 1 || key_dest == key_game))
SV_Physics();
2019-11-24 20:45:15 -08:00
//johnfitz -- devstats
2019-11-25 17:40:18 -08:00
if(cls.signon == SIGNONS)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
for(i = 0, active = 0; i < sv.num_edicts; i++)
2019-11-24 20:45:15 -08:00
{
ent = EdictNum(i);
2019-11-25 17:40:18 -08:00
if(!ent->free)
2019-11-24 20:45:15 -08:00
active++;
}
2019-11-25 17:40:18 -08:00
if(active > 600 && dev_peakstats.edicts <= 600)
Con_DWarning("%" PRIi32 " edicts exceeds standard limit of 600 (max = %" PRIi32 ").\n", active, sv.max_edicts);
2019-11-24 20:45:15 -08:00
dev_stats.edicts = active;
dev_peakstats.edicts = q_max(active, dev_peakstats.edicts);
}
//johnfitz
// send all messages to the clients
2019-11-25 17:40:18 -08:00
SV_SendClientMessages();
2019-11-24 20:45:15 -08:00
}
/*
==================
Host_Frame
Runs all active servers
==================
*/
2019-12-07 09:27:26 -08:00
static void Host_FrameT(float time)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
static double time1 = 0;
static double time2 = 0;
static double time3 = 0;
int32_t pass1, pass2, pass3;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(setjmp(host_abortserver))
return; // something bad happened, or the server disconnected
2019-11-24 20:45:15 -08:00
// keep the random time dependent
2019-11-25 17:40:18 -08:00
rand();
2019-11-24 20:45:15 -08:00
// decide the simulation time
2019-11-25 17:40:18 -08:00
if(!Host_FilterTime(time))
return; // don't run too fast, or packets will flood out
2019-11-24 20:45:15 -08:00
// get new key events
2019-11-25 17:40:18 -08:00
Key_UpdateForDest();
IN_UpdateInputMode();
Sys_SendKeyEvents();
2019-11-24 20:45:15 -08:00
// allow mice or other external controllers to add commands
2019-11-25 17:40:18 -08:00
IN_Commands();
2019-11-24 20:45:15 -08:00
// process console commands
2019-11-25 17:40:18 -08:00
Cbuf_Execute();
2019-11-24 20:45:15 -08:00
NET_Poll();
// if running the server locally, make intentions now
2019-11-25 17:40:18 -08:00
if(sv.active)
CL_SendCmd();
2019-11-24 20:45:15 -08:00
//-------------------
//
// server operations
//
//-------------------
// check for commands typed to the host
2019-11-25 17:40:18 -08:00
Host_GetConsoleCommands();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sv.active)
Host_ServerFrame();
2019-11-24 20:45:15 -08:00
//-------------------
//
// client operations
//
//-------------------
// if running the server remotely, send intentions now after
// the incoming messages have been read
2019-11-25 17:40:18 -08:00
if(!sv.active)
CL_SendCmd();
2019-11-24 20:45:15 -08:00
// fetch results from server
2019-11-25 17:40:18 -08:00
if(cls.state == ca_connected)
CL_ReadFromServer();
2019-11-24 20:45:15 -08:00
// update video
2019-11-25 17:40:18 -08:00
if(host_speeds.value)
time1 = Sys_DoubleTime();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
SCR_UpdateScreen();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
CL_RunParticles(); //johnfitz -- seperated from rendering
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(host_speeds.value)
time2 = Sys_DoubleTime();
2019-11-24 20:45:15 -08:00
// update audio
2019-11-25 17:40:18 -08:00
BGM_Update(); // adds music raw samples and/or advances midi driver
if(cls.signon == SIGNONS)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
S_Update(r_origin, vpn, vright, vup);
CL_DecayLights();
2019-11-24 20:45:15 -08:00
}
else
2019-11-25 17:40:18 -08:00
S_Update(vec3_origin, vec3_origin, vec3_origin, vec3_origin);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(host_speeds.value)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
pass1 = (time1 - time3) * 1000;
time3 = Sys_DoubleTime();
pass2 = (time2 - time1) * 1000;
pass3 = (time3 - time2) * 1000;
Con_Printf("%3" PRIi32 " tot %3" PRIi32 " server %3" PRIi32 " gfx %3" PRIi32 " snd\n",
pass1 + pass2 + pass3, pass1, pass2, pass3);
2019-11-24 20:45:15 -08:00
}
host_framecount++;
}
2019-11-25 17:40:18 -08:00
void Host_Frame(float time)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
double time1, time2;
static double timetotal;
static int32_t timecount;
int32_t i, c, m;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!serverprofile.value)
2019-11-24 20:45:15 -08:00
{
2019-12-07 09:27:26 -08:00
Host_FrameT(time);
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
time1 = Sys_DoubleTime();
2019-12-07 09:27:26 -08:00
Host_FrameT(time);
2019-11-25 17:40:18 -08:00
time2 = Sys_DoubleTime();
2019-11-24 20:45:15 -08:00
timetotal += time2 - time1;
timecount++;
2019-11-25 17:40:18 -08:00
if(timecount < 1000)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
m = timetotal * 1000 / timecount;
2019-11-24 20:45:15 -08:00
timecount = 0;
timetotal = 0;
c = 0;
2019-11-25 17:40:18 -08:00
for(i = 0; i < svs.maxclients; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(svs.clients[i].active)
2019-11-24 20:45:15 -08:00
c++;
}
2019-11-25 17:40:18 -08:00
Con_Printf("serverprofile: %2" PRIi32 " clients %2" PRIi32 " msec\n", c, m);
2019-11-24 20:45:15 -08:00
}
/*
====================
Host_Init
====================
*/
2019-11-25 17:40:18 -08:00
void Host_Init(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
int32_t minimum_memory;
2019-11-25 16:41:36 -08:00
2019-11-25 17:40:18 -08:00
if(standard_quake)
2019-11-24 20:45:15 -08:00
minimum_memory = MINIMUM_MEMORY;
2019-11-25 16:41:36 -08:00
else
minimum_memory = MINIMUM_MEMORY_LEVELPAK;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(COM_CheckParm("-minmemory"))
2019-11-24 20:45:15 -08:00
host_parms->memsize = minimum_memory;
2019-11-25 17:40:18 -08:00
if(host_parms->memsize < minimum_memory)
Sys_Error("Only %4.1f MiB of memory available, can't execute game", host_parms->memsize / (float)0x100000);
2019-11-24 20:45:15 -08:00
com_argc = host_parms->argc;
com_argv = host_parms->argv;
2019-11-25 17:40:18 -08:00
Memory_Init(host_parms->membase, host_parms->memsize);
Cbuf_Init();
Cmd_Init();
LOG_Init(host_parms);
Cvar_Init(); //johnfitz
COM_InitFilesystem();
Host_InitLocal();
W_LoadWadFile(); //johnfitz -- filename is now hard-coded for honesty
if(cls.state != ca_dedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Key_Init();
Con_Init();
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
PR_Init();
Mod_Init();
NET_Init();
SV_Init();
2019-11-24 20:45:15 -08:00
2019-12-02 07:01:47 -08:00
PrintExeTime();
2019-11-25 17:40:18 -08:00
Con_Printf("%4.1f megabyte heap\n", host_parms->memsize / (1024 * 1024.0));
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.state != ca_dedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
host_colormap = (byte *)COM_LoadHunkFile("gfx/colormap.lmp", NULL);
if(!host_colormap)
Sys_Error("Couldn't load gfx/colormap.lmp");
V_Init();
Chase_Init();
M_Init();
ExtraMaps_Init(); //johnfitz
Modlist_Init(); //johnfitz
DemoList_Init(); //ericw
VID_Init();
IN_Init();
TexMgr_Init(); //johnfitz
Draw_Init();
SCR_Init();
R_Init();
S_Init();
2019-11-24 20:45:15 -08:00
BGM_Init();
2019-11-25 17:40:18 -08:00
Sbar_Init();
CL_Init();
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
Hunk_AllocName(0, "-HOST_HUNKLEVEL-");
host_hunklevel = Hunk_LowMark();
2019-11-24 20:45:15 -08:00
host_initialized = true;
2019-11-25 17:40:18 -08:00
if(cls.state != ca_dedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cbuf_InsertText("exec quake.rc\n");
// johnfitz -- in case the vid mode was locked during vid_init, we can unlock it now.
2019-11-24 20:45:15 -08:00
// note: two leading newlines because the command buffer swallows one of them.
2019-11-25 17:40:18 -08:00
Cbuf_AddText("\n\nvid_unlock\n");
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
if(cls.state == ca_dedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cbuf_AddText("exec autoexec.cfg\n");
Cbuf_AddText("stuffcmds");
Cbuf_Execute();
if(!sv.active)
Cbuf_AddText("map start\n");
2019-11-24 20:45:15 -08:00
}
}
/*
===============
Host_Shutdown
FIXME: this is a callback from Sys_Quit and Sys_Error. It would be better
to run quit through here before the final handoff to the sys code.
===============
*/
void Host_Shutdown(void)
{
2019-11-25 15:28:38 -08:00
static bool isdown = false;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(isdown)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
printf("recursive shutdown\n");
2019-11-24 20:45:15 -08:00
return;
}
isdown = true;
// keep Con_Printf from trying to update the screen
scr_disabled_for_loading = true;
2019-11-25 17:40:18 -08:00
Host_WriteConfiguration();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
NET_Shutdown();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.state != ca_dedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(con_initialized)
History_Shutdown();
2019-11-24 20:45:15 -08:00
BGM_Shutdown();
2019-11-25 17:40:18 -08:00
S_Shutdown();
IN_Shutdown();
2019-11-24 20:45:15 -08:00
VID_Shutdown();
}
2019-11-25 17:40:18 -08:00
LOG_Close();
2019-11-24 20:45:15 -08:00
}