spingle/source/cl_demo.c

499 lines
11 KiB
C
Raw Permalink Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
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.
*/
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static void CL_FinishTimeDemo(void);
2019-11-24 20:45:15 -08:00
/*
==============================================================================
DEMO CODE
When a demo is playing back, all NET_SendMessages are skipped, and
NET_GetMessages are read from the demo file.
Whenever cl.time gets past the last received message, another message is
read from the demo file.
==============================================================================
*/
// from ProQuake: space to fill out the demo header for record at any time
2019-11-25 17:40:18 -08:00
static byte demo_head[3][MAX_MSGLEN];
static int32_t demo_head_size[2];
2019-11-24 20:45:15 -08:00
/*
==============
CL_StopPlayback
Called when a demo file runs out, or the user starts a game
==============
*/
2019-11-25 17:40:18 -08:00
void CL_StopPlayback(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!cls.demoplayback)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
fclose(cls.demofile);
2019-11-24 20:45:15 -08:00
cls.demoplayback = false;
cls.demopaused = false;
cls.demofile = NULL;
cls.state = ca_disconnected;
2019-11-25 17:40:18 -08:00
if(cls.timedemo)
CL_FinishTimeDemo();
2019-11-24 20:45:15 -08:00
}
/*
====================
CL_WriteDemoMessage
Dumps the current net message, prefixed by the length and view angles
====================
*/
2019-11-25 17:40:18 -08:00
static void CL_WriteDemoMessage(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t len;
int32_t i;
float f;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
len = LittleLong(net_message.cursize);
fwrite(&len, 4, 1, cls.demofile);
for(i = 0; i < 3; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
f = LittleFloat(cl.viewangles[i]);
fwrite(&f, 4, 1, cls.demofile);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
fwrite(net_message.data, net_message.cursize, 1, cls.demofile);
fflush(cls.demofile);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
static int32_t CL_GetDemoMessage(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t r, i;
float f;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.demopaused)
2019-11-24 20:45:15 -08:00
return 0;
// decide if it is time to grab the next message
2019-11-25 17:40:18 -08:00
if(cls.signon == SIGNONS) // always grab until fully connected
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cls.timedemo)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(host_framecount == cls.td_lastframe)
return 0; // already read this frame's message
2019-11-24 20:45:15 -08:00
cls.td_lastframe = host_framecount;
2019-11-25 17:40:18 -08:00
// if this is the second frame, grab the real td_starttime
// so the bogus time on the first frame doesn't count
if(host_framecount == cls.td_startframe + 1)
2019-11-24 20:45:15 -08:00
cls.td_starttime = realtime;
}
2019-11-25 17:40:18 -08:00
else if(/* cl.time > 0 && */ cl.time <= cl.mtime[0])
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
return 0; // don't need another message yet
2019-11-24 20:45:15 -08:00
}
}
// get the next message
2019-11-25 17:40:18 -08:00
fread(&net_message.cursize, 4, 1, cls.demofile);
VectorCopy(cl.mviewangles[0], cl.mviewangles[1]);
for(i = 0 ; i < 3 ; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
r = fread(&f, 4, 1, cls.demofile);
cl.mviewangles[0][i] = LittleFloat(f);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
net_message.cursize = LittleLong(net_message.cursize);
if(net_message.cursize > MAX_MSGLEN)
Sys_Error("Demo message > MAX_MSGLEN");
r = fread(net_message.data, net_message.cursize, 1, cls.demofile);
if(r != 1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
CL_StopPlayback();
2019-11-24 20:45:15 -08:00
return 0;
}
return 1;
}
/*
====================
CL_GetMessage
Handles recording and playback of demos, on top of NET_ code
====================
*/
2019-11-25 17:40:18 -08:00
int32_t CL_GetMessage(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t r;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.demoplayback)
return CL_GetDemoMessage();
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
r = NET_GetMessage(cls.netcon);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(r != 1 && r != 2)
2019-11-24 20:45:15 -08:00
return r;
2019-11-25 17:40:18 -08:00
// discard nop keepalive message
if(net_message.cursize == 1 && net_message.data[0] == svc_nop)
Con_Printf("<-- server to client keepalive\n");
2019-11-24 20:45:15 -08:00
else
break;
}
2019-11-25 17:40:18 -08:00
if(cls.demorecording)
CL_WriteDemoMessage();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cls.signon < 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
// record messages before full connection, so that a
// demo record can happen after connection is done
2019-11-24 20:45:15 -08:00
memcpy(demo_head[cls.signon], net_message.data, net_message.cursize);
demo_head_size[cls.signon] = net_message.cursize;
}
return r;
}
/*
====================
CL_Stop_f
stop recording a demo
====================
*/
2019-11-25 17:40:18 -08:00
void CL_Stop_f(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cmd_source != src_command)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(!cls.demorecording)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("Not recording a demo.\n");
2019-11-24 20:45:15 -08:00
return;
}
// write a disconnect message to the demo file
2019-11-25 17:40:18 -08:00
SZ_Clear(&net_message);
MSG_WriteByte(&net_message, svc_disconnect);
CL_WriteDemoMessage();
2019-11-24 20:45:15 -08:00
// finish up
2019-11-25 17:40:18 -08:00
fclose(cls.demofile);
2019-11-24 20:45:15 -08:00
cls.demofile = NULL;
cls.demorecording = false;
2019-11-25 17:40:18 -08:00
Con_Printf("Completed demo\n");
2019-11-24 20:45:15 -08:00
// ericw -- update demo tab-completion list
2019-11-25 17:40:18 -08:00
DemoList_Rebuild();
2019-11-24 20:45:15 -08:00
}
/*
====================
CL_Record_f
record <demoname> <map> [cd track]
====================
*/
2019-11-25 17:40:18 -08:00
void CL_Record_f(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t c;
char name[MAX_OSPATH];
int32_t track;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cmd_source != src_command)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(cls.demoplayback)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("Can't record during demo playback\n");
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
if(cls.demorecording)
2019-11-24 20:45:15 -08:00
CL_Stop_f();
c = Cmd_Argc();
2019-11-25 17:40:18 -08:00
if(c != 2 && c != 3 && c != 4)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("record <demoname> [<map> [cd track]]\n");
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
if(strstr(Cmd_Argv(1), ".."))
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("Relative pathnames are not allowed.\n");
2019-11-24 20:45:15 -08:00
return;
}
2019-12-03 04:48:19 -08:00
if(c == 2 && cls.state == ca_connected && cls.signon < 2)
2019-11-24 20:45:15 -08:00
{
2019-12-03 04:48:19 -08:00
Con_Printf("Can't record - try again when connected\n");
2019-11-24 20:45:15 -08:00
return;
}
// write the forced cd track number, or -1
2019-11-25 17:40:18 -08:00
if(c == 4)
2019-11-24 20:45:15 -08:00
{
track = atoi(Cmd_Argv(3));
2019-11-25 17:40:18 -08:00
Con_Printf("Forcing CD track to %" PRIi32 "\n", cls.forcetrack);
2019-11-24 20:45:15 -08:00
}
else
{
track = -1;
}
2019-11-25 17:40:18 -08:00
q_snprintf(name, sizeof(name), "%s/%s", com_gamedir, Cmd_Argv(1));
2019-11-24 20:45:15 -08:00
// start the map up
2019-11-25 17:40:18 -08:00
if(c > 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cmd_ExecuteString(va("map %s", Cmd_Argv(2)), src_command);
if(cls.state != ca_connected)
2019-11-24 20:45:15 -08:00
return;
}
// open the demo file
2019-11-25 17:40:18 -08:00
COM_AddExtension(name, ".dem", sizeof(name));
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
Con_Printf("recording to %s.\n", name);
cls.demofile = fopen(name, "wb");
if(!cls.demofile)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("ERROR: couldn't create %s\n", name);
2019-11-24 20:45:15 -08:00
return;
}
cls.forcetrack = track;
2019-11-25 17:40:18 -08:00
fprintf(cls.demofile, "%" PRIi32 "\n", cls.forcetrack);
2019-11-24 20:45:15 -08:00
cls.demorecording = true;
// from ProQuake: initialize the demo file if we're already connected
2019-11-25 17:40:18 -08:00
if(c == 2 && cls.state == ca_connected)
2019-11-24 20:45:15 -08:00
{
byte *data = net_message.data;
2019-11-25 16:49:58 -08:00
int32_t cursize = net_message.cursize;
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0; i < 2; i++)
2019-11-24 20:45:15 -08:00
{
net_message.data = demo_head[i];
net_message.cursize = demo_head_size[i];
CL_WriteDemoMessage();
}
net_message.data = demo_head[2];
2019-11-25 17:40:18 -08:00
SZ_Clear(&net_message);
2019-11-24 20:45:15 -08:00
// current names, colors, and frag counts
2019-11-25 17:40:18 -08:00
for(i = 0; i < cl.maxclients; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_updatename);
MSG_WriteByte(&net_message, i);
MSG_WriteString(&net_message, cl.scores[i].name);
MSG_WriteByte(&net_message, svc_updatefrags);
MSG_WriteByte(&net_message, i);
MSG_WriteShort(&net_message, cl.scores[i].frags);
MSG_WriteByte(&net_message, svc_updatecolors);
MSG_WriteByte(&net_message, i);
MSG_WriteByte(&net_message, cl.scores[i].colors);
2019-11-24 20:45:15 -08:00
}
// send all current light styles
2019-11-25 17:40:18 -08:00
for(i = 0; i < MAX_LIGHTSTYLES; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_lightstyle);
MSG_WriteByte(&net_message, i);
MSG_WriteString(&net_message, cl_lightstyle[i].map);
2019-11-24 20:45:15 -08:00
}
// what about the CD track or SVC fog... future consideration.
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_updatestat);
MSG_WriteByte(&net_message, STAT_TOTALSECRETS);
MSG_WriteLong(&net_message, cl.stats[STAT_TOTALSECRETS]);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_updatestat);
MSG_WriteByte(&net_message, STAT_TOTALMONSTERS);
MSG_WriteLong(&net_message, cl.stats[STAT_TOTALMONSTERS]);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_updatestat);
MSG_WriteByte(&net_message, STAT_SECRETS);
MSG_WriteLong(&net_message, cl.stats[STAT_SECRETS]);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_updatestat);
MSG_WriteByte(&net_message, STAT_MONSTERS);
MSG_WriteLong(&net_message, cl.stats[STAT_MONSTERS]);
2019-11-24 20:45:15 -08:00
// view entity
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_setview);
MSG_WriteShort(&net_message, cl.viewentity);
2019-11-24 20:45:15 -08:00
// signon
2019-11-25 17:40:18 -08:00
MSG_WriteByte(&net_message, svc_signonnum);
MSG_WriteByte(&net_message, 3);
2019-11-24 20:45:15 -08:00
CL_WriteDemoMessage();
// restore net_message
net_message.data = data;
net_message.cursize = cursize;
}
}
/*
====================
CL_PlayDemo_f
play [demoname]
====================
*/
2019-11-25 17:40:18 -08:00
void CL_PlayDemo_f(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
char name[MAX_OSPATH];
int32_t i, c;
2019-11-25 15:28:38 -08:00
bool neg;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(cmd_source != src_command)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(Cmd_Argc() != 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("playdemo <demoname> : plays a demo\n");
2019-11-24 20:45:15 -08:00
return;
}
// disconnect from server
2019-11-25 17:40:18 -08:00
CL_Disconnect();
2019-11-24 20:45:15 -08:00
// open the demo file
2019-11-25 17:40:18 -08:00
q_strlcpy(name, Cmd_Argv(1), sizeof(name));
COM_AddExtension(name, ".dem", sizeof(name));
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
Con_Printf("Playing demo from %s.\n", name);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
COM_FOpenFile(name, &cls.demofile, NULL);
if(!cls.demofile)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("ERROR: couldn't open %s\n", name);
cls.demonum = -1; // stop demo loop
2019-11-24 20:45:15 -08:00
return;
}
// ZOID, fscanf is evil
// O.S.: if a space character e.g. 0x20 (' ') follows '\n',
// fscanf skips that byte too and screws up further reads.
2019-11-25 16:56:15 -08:00
// fscanf (cls.demofile, "%" PRIi32 "\n", &cls.forcetrack);
2019-11-24 20:45:15 -08:00
cls.forcetrack = 0;
c = 0; /* silence pesky compiler warnings */
neg = false;
// read a decimal integer possibly with a leading '-',
// followed by a '\n':
2019-11-25 17:40:18 -08:00
for(i = 0; i < 13; i++)
2019-11-24 20:45:15 -08:00
{
c = getc(cls.demofile);
2019-11-25 17:40:18 -08:00
if(c == '\n')
2019-11-24 20:45:15 -08:00
break;
2019-11-25 17:40:18 -08:00
if(c == '-')
{
2019-11-24 20:45:15 -08:00
neg = true;
continue;
}
// check for multiple '-' or legal digits? meh...
cls.forcetrack = cls.forcetrack * 10 + (c - '0');
}
2019-11-25 17:40:18 -08:00
if(c != '\n')
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
fclose(cls.demofile);
2019-11-24 20:45:15 -08:00
cls.demofile = NULL;
2019-11-25 17:40:18 -08:00
cls.demonum = -1; // stop demo loop
Con_Printf("ERROR: demo \"%s\" is invalid\n", name);
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
if(neg)
2019-11-24 20:45:15 -08:00
cls.forcetrack = -cls.forcetrack;
cls.demoplayback = true;
cls.demopaused = false;
cls.state = ca_connected;
// get rid of the menu and/or console
key_dest = key_game;
}
/*
====================
CL_FinishTimeDemo
====================
*/
2019-11-25 17:40:18 -08:00
static void CL_FinishTimeDemo(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t frames;
float time;
2019-11-24 20:45:15 -08:00
cls.timedemo = false;
// the first frame didn't count
frames = (host_framecount - cls.td_startframe) - 1;
time = realtime - cls.td_starttime;
2019-11-25 17:40:18 -08:00
if(!time)
2019-11-24 20:45:15 -08:00
time = 1;
2019-11-25 17:40:18 -08:00
Con_Printf("%" PRIi32 " frames %5.1f seconds %5.1f fps\n", frames, time, frames / time);
2019-11-24 20:45:15 -08:00
}
/*
====================
CL_TimeDemo_f
timedemo [demoname]
====================
*/
2019-11-25 17:40:18 -08:00
void CL_TimeDemo_f(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cmd_source != src_command)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(Cmd_Argc() != 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("timedemo <demoname> : gets demo speeds\n");
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
CL_PlayDemo_f();
if(!cls.demofile)
2019-11-24 20:45:15 -08:00
return;
// cls.td_starttime will be grabbed at the second frame of the demo, so
// all the loading time doesn't get counted
cls.timedemo = true;
cls.td_startframe = host_framecount;
2019-11-25 17:40:18 -08:00
cls.td_lastframe = -1; // get a new message this frame
2019-11-24 20:45:15 -08:00
}