]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_demo.c
added a note about an id bug with triggers and ignoring .owner (the fact they don't)
[divverent/darkplaces.git] / cl_demo.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22
23 void CL_FinishTimeDemo (void);
24
25 /*
26 ==============================================================================
27
28 DEMO CODE
29
30 When a demo is playing back, all NET_SendMessages are skipped, and
31 NET_GetMessages are read from the demo file.
32
33 Whenever cl.time gets past the last received message, another message is
34 read from the demo file.
35 ==============================================================================
36 */
37
38 /*
39 =====================
40 CL_NextDemo
41
42 Called to play the next demo in the demo loop
43 =====================
44 */
45 void CL_NextDemo (void)
46 {
47         char    str[1024];
48
49         if (cls.demonum == -1)
50                 return;         // don't play demos
51
52         if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
53         {
54                 cls.demonum = 0;
55                 if (!cls.demos[cls.demonum][0])
56                 {
57                         Con_Printf ("No demos listed with startdemos\n");
58                         cls.demonum = -1;
59                         return;
60                 }
61         }
62
63         sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
64         Cbuf_InsertText (str);
65         cls.demonum++;
66 }
67
68 /*
69 ==============
70 CL_StopPlayback
71
72 Called when a demo file runs out, or the user starts a game
73 ==============
74 */
75 // LordHavoc: now called only by CL_Disconnect
76 void CL_StopPlayback (void)
77 {
78         if (!cls.demoplayback)
79                 return;
80
81         FS_Close (cls.demofile);
82         cls.demoplayback = false;
83         cls.demofile = NULL;
84
85         if (cls.timedemo)
86                 CL_FinishTimeDemo ();
87 }
88
89 /*
90 ====================
91 CL_WriteDemoMessage
92
93 Dumps the current net message, prefixed by the length and view angles
94 ====================
95 */
96 void CL_WriteDemoMessage (void)
97 {
98         int             len;
99         int             i;
100         float   f;
101
102         if (cls.demopaused) // LordHavoc: pausedemo
103                 return;
104
105         len = LittleLong (net_message.cursize);
106         FS_Write (cls.demofile, &len, 4);
107         for (i=0 ; i<3 ; i++)
108         {
109                 f = LittleFloat (cl.viewangles[i]);
110                 FS_Write (cls.demofile, &f, 4);
111         }
112         FS_Write (cls.demofile, net_message.data, net_message.cursize);
113         FS_Flush (cls.demofile);
114 }
115
116 /*
117 ====================
118 CL_GetMessage
119
120 Handles recording and playback of demos, on top of NET_ code
121 ====================
122 */
123 int CL_GetMessage (void)
124 {
125         int             r, i;
126         float   f;
127
128         if      (cls.demoplayback)
129         {
130                 if (cls.demopaused) // LordHavoc: pausedemo
131                         return 0;
132
133         // decide if it is time to grab the next message
134                 if (cls.signon == SIGNONS)      // always grab until fully connected
135                 {
136                         if (cls.timedemo)
137                         {
138                                 if (host_framecount == cls.td_lastframe)
139                                         return 0;               // already read this frame's message
140                                 cls.td_lastframe = host_framecount;
141                         // if this is the second frame, grab the real td_starttime
142                         // so the bogus time on the first frame doesn't count
143                                 if (host_framecount == cls.td_startframe + 1)
144                                         cls.td_starttime = realtime;
145                         }
146                         else if (cl.time <= cl.mtime[0])
147                         {
148                                         return 0;               // don't need another message yet
149                         }
150                 }
151
152         // get the next message
153                 FS_Read (cls.demofile, &net_message.cursize, 4);
154                 VectorCopy (cl.mviewangles[0], cl.mviewangles[1]);
155                 for (i=0 ; i<3 ; i++)
156                 {
157                         r = FS_Read (cls.demofile, &f, 4);
158                         cl.mviewangles[0][i] = LittleFloat (f);
159                 }
160
161                 net_message.cursize = LittleLong (net_message.cursize);
162                 if (net_message.cursize > MAX_DATAGRAM)
163                         Host_Error ("Demo message > MAX_DATAGRAM");
164                 r = FS_Read (cls.demofile, net_message.data, net_message.cursize);
165                 if (r != net_message.cursize)
166                 {
167                         CL_Disconnect ();
168                         return 0;
169                 }
170
171                 return 1;
172         }
173
174         while (1)
175         {
176                 r = NET_GetMessage (cls.netcon);
177
178                 if (r != 1 && r != 2)
179                         return r;
180
181         // discard nop keepalive message
182                 if (net_message.cursize == 1 && net_message.data[0] == svc_nop)
183                         Con_Printf ("<-- server to client keepalive\n");
184                 else
185                         break;
186         }
187
188         if (cls.demorecording)
189                 CL_WriteDemoMessage ();
190
191         return r;
192 }
193
194
195 /*
196 ====================
197 CL_Stop_f
198
199 stop recording a demo
200 ====================
201 */
202 void CL_Stop_f (void)
203 {
204         if (cmd_source != src_command)
205                 return;
206
207         if (!cls.demorecording)
208         {
209                 Con_Printf ("Not recording a demo.\n");
210                 return;
211         }
212
213 // write a disconnect message to the demo file
214         SZ_Clear (&net_message);
215         MSG_WriteByte (&net_message, svc_disconnect);
216         CL_WriteDemoMessage ();
217
218 // finish up
219         FS_Close (cls.demofile);
220         cls.demofile = NULL;
221         cls.demorecording = false;
222         Con_Printf ("Completed demo\n");
223 }
224
225 /*
226 ====================
227 CL_Record_f
228
229 record <demoname> <map> [cd track]
230 ====================
231 */
232 void CL_Record_f (void)
233 {
234         int c, track;
235         char name[MAX_OSPATH];
236
237         if (cmd_source != src_command)
238                 return;
239
240         c = Cmd_Argc();
241         if (c != 2 && c != 3 && c != 4)
242         {
243                 Con_Printf ("record <demoname> [<map> [cd track]]\n");
244                 return;
245         }
246
247         if (strstr(Cmd_Argv(1), ".."))
248         {
249                 Con_Printf ("Relative pathnames are not allowed.\n");
250                 return;
251         }
252
253         if (c == 2 && cls.state == ca_connected)
254         {
255                 Con_Printf("Can not record - already connected to server\nClient demo recording must be started before connecting\n");
256                 return;
257         }
258
259         // write the forced cd track number, or -1
260         if (c == 4)
261         {
262                 track = atoi(Cmd_Argv(3));
263                 Con_Printf ("Forcing CD track to %i\n", cls.forcetrack);
264         }
265         else
266                 track = -1;
267
268         // get the demo name
269         strncpy (name, Cmd_Argv(1), sizeof (name) - 1);
270         name[sizeof (name) - 1] = '\0';
271         FS_DefaultExtension (name, ".dem");
272
273         // start the map up
274         if (c > 2)
275                 Cmd_ExecuteString ( va("map %s", Cmd_Argv(2)), src_command);
276
277         // open the demo file
278         Con_Printf ("recording to %s.\n", name);
279         cls.demofile = FS_Open (name, "wb", false);
280         if (!cls.demofile)
281         {
282                 Con_Printf ("ERROR: couldn't open.\n");
283                 return;
284         }
285
286         cls.forcetrack = track;
287         FS_Printf (cls.demofile, "%i\n", cls.forcetrack);
288
289         cls.demorecording = true;
290 }
291
292
293 /*
294 ====================
295 CL_PlayDemo_f
296
297 play [demoname]
298 ====================
299 */
300 void CL_PlayDemo_f (void)
301 {
302         char    name[256];
303         int c;
304         qboolean neg = false;
305
306         if (cmd_source != src_command)
307                 return;
308
309         if (Cmd_Argc() != 2)
310         {
311                 Con_Printf ("play <demoname> : plays a demo\n");
312                 return;
313         }
314
315         // disconnect from server
316         CL_Disconnect ();
317
318         // open the demo file
319         strcpy (name, Cmd_Argv(1));
320         FS_DefaultExtension (name, ".dem");
321
322         Con_Printf ("Playing demo from %s.\n", name);
323         cls.demofile = FS_Open (name, "rb", false);
324         if (!cls.demofile)
325         {
326                 Con_Printf ("ERROR: couldn't open.\n");
327                 cls.demonum = -1;               // stop demo loop
328                 return;
329         }
330
331         SCR_BeginLoadingPlaque ();
332
333         cls.demoplayback = true;
334         cls.state = ca_connected;
335         cls.forcetrack = 0;
336
337         while ((c = FS_Getc (cls.demofile)) != '\n')
338                 if (c == '-')
339                         neg = true;
340                 else
341                         cls.forcetrack = cls.forcetrack * 10 + (c - '0');
342
343         if (neg)
344                 cls.forcetrack = -cls.forcetrack;
345 }
346
347 /*
348 ====================
349 CL_FinishTimeDemo
350
351 ====================
352 */
353 void CL_FinishTimeDemo (void)
354 {
355         int             frames;
356         double  time; // LordHavoc: changed timedemo accuracy to double
357
358         cls.timedemo = false;
359
360 // the first frame didn't count
361         frames = (host_framecount - cls.td_startframe) - 1;
362         time = realtime - cls.td_starttime;
363         if (!time)
364                 time = 1;
365         // LordHavoc: timedemo now prints out 7 digits of fraction
366         Con_Printf ("%i frames %5.7f seconds %5.7f fps\n", frames, time, frames/time);
367 }
368
369 /*
370 ====================
371 CL_TimeDemo_f
372
373 timedemo [demoname]
374 ====================
375 */
376 void CL_TimeDemo_f (void)
377 {
378         if (cmd_source != src_command)
379                 return;
380
381         if (Cmd_Argc() != 2)
382         {
383                 Con_Printf ("timedemo <demoname> : gets demo speeds\n");
384                 return;
385         }
386
387         CL_PlayDemo_f ();
388
389 // cls.td_starttime will be grabbed at the second frame of the demo, so
390 // all the loading time doesn't get counted
391
392         // instantly hide console and deactivate it
393         key_dest = key_game;
394         key_consoleactive = 0;
395         scr_conlines = 0;
396         scr_con_current = 0;
397
398         cls.timedemo = true;
399         cls.td_startframe = host_framecount;
400         cls.td_lastframe = -1;          // get a new message this frame
401 }
402