cl_demo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "quakedef.h"
  16. void CL_FinishTimeDemo (void);
  17. /*
  18. ==============================================================================
  19. DEMO CODE
  20. When a demo is playing back, all NET_SendMessages are skipped, and
  21. NET_GetMessages are read from the demo file.
  22. Whenever cl.time gets past the last received message, another message is
  23. read from the demo file.
  24. ==============================================================================
  25. */
  26. /*
  27. ==============
  28. CL_StopPlayback
  29. Called when a demo file runs out, or the user starts a game
  30. ==============
  31. */
  32. void CL_StopPlayback (void)
  33. {
  34. if (!cls.demoplayback)
  35. return;
  36. fclose (cls.demofile);
  37. cls.demoplayback = false;
  38. cls.demofile = NULL;
  39. cls.state = ca_disconnected;
  40. if (cls.timedemo)
  41. CL_FinishTimeDemo ();
  42. }
  43. /*
  44. ====================
  45. CL_WriteDemoMessage
  46. Dumps the current net message, prefixed by the length and view angles
  47. ====================
  48. */
  49. void CL_WriteDemoMessage (void)
  50. {
  51. int len;
  52. int i;
  53. float f;
  54. len = LittleLong (net_message.cursize);
  55. fwrite (&len, 4, 1, cls.demofile);
  56. for (i=0 ; i<3 ; i++)
  57. {
  58. f = LittleFloat (cl.viewangles[i]);
  59. fwrite (&f, 4, 1, cls.demofile);
  60. }
  61. fwrite (net_message.data, net_message.cursize, 1, cls.demofile);
  62. fflush (cls.demofile);
  63. }
  64. /*
  65. ====================
  66. CL_GetMessage
  67. Handles recording and playback of demos, on top of NET_ code
  68. ====================
  69. */
  70. int CL_GetMessage (void)
  71. {
  72. int r, i;
  73. float f;
  74. if (cls.demoplayback)
  75. {
  76. // decide if it is time to grab the next message
  77. if (cls.signon == SIGNONS) // allways grab until fully connected
  78. {
  79. if (cls.timedemo)
  80. {
  81. if (host_framecount == cls.td_lastframe)
  82. return 0; // allready read this frame's message
  83. cls.td_lastframe = host_framecount;
  84. // if this is the second frame, grab the real td_starttime
  85. // so the bogus time on the first frame doesn't count
  86. if (host_framecount == cls.td_startframe + 1)
  87. cls.td_starttime = realtime;
  88. }
  89. else if ( /* cl.time > 0 && */ cl.time <= cl.mtime[0])
  90. {
  91. return 0; // don't need another message yet
  92. }
  93. }
  94. // get the next message
  95. fread (&net_message.cursize, 4, 1, cls.demofile);
  96. VectorCopy (cl.mviewangles[0], cl.mviewangles[1]);
  97. for (i=0 ; i<3 ; i++)
  98. {
  99. r = fread (&f, 4, 1, cls.demofile);
  100. cl.mviewangles[0][i] = LittleFloat (f);
  101. }
  102. net_message.cursize = LittleLong (net_message.cursize);
  103. if (net_message.cursize > MAX_MSGLEN)
  104. Sys_Error ("Demo message > MAX_MSGLEN");
  105. r = fread (net_message.data, net_message.cursize, 1, cls.demofile);
  106. if (r != 1)
  107. {
  108. CL_StopPlayback ();
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. while (1)
  114. {
  115. r = NET_GetMessage (cls.netcon);
  116. if (r != 1 && r != 2)
  117. return r;
  118. // discard nop keepalive message
  119. if (net_message.cursize == 1 && net_message.data[0] == svc_nop)
  120. Con_Printf ("<-- server to client keepalive\n");
  121. else
  122. break;
  123. }
  124. if (cls.demorecording)
  125. CL_WriteDemoMessage ();
  126. return r;
  127. }
  128. /*
  129. ====================
  130. CL_Stop_f
  131. stop recording a demo
  132. ====================
  133. */
  134. void CL_Stop_f (void)
  135. {
  136. if (cmd_source != src_command)
  137. return;
  138. if (!cls.demorecording)
  139. {
  140. Con_Printf ("Not recording a demo.\n");
  141. return;
  142. }
  143. // write a disconnect message to the demo file
  144. SZ_Clear (&net_message);
  145. MSG_WriteByte (&net_message, svc_disconnect);
  146. CL_WriteDemoMessage ();
  147. // finish up
  148. fclose (cls.demofile);
  149. cls.demofile = NULL;
  150. cls.demorecording = false;
  151. Con_Printf ("Completed demo\n");
  152. }
  153. /*
  154. ====================
  155. CL_Record_f
  156. record <demoname> <map> [cd track]
  157. ====================
  158. */
  159. void CL_Record_f (void)
  160. {
  161. int c;
  162. char name[MAX_OSPATH];
  163. int track;
  164. if (cmd_source != src_command)
  165. return;
  166. c = Cmd_Argc();
  167. if (c != 2 && c != 3 && c != 4)
  168. {
  169. Con_Printf ("record <demoname> [<map> [cd track]]\n");
  170. return;
  171. }
  172. if (strstr(Cmd_Argv(1), ".."))
  173. {
  174. Con_Printf ("Relative pathnames are not allowed.\n");
  175. return;
  176. }
  177. if (c == 2 && cls.state == ca_connected)
  178. {
  179. Con_Printf("Can not record - already connected to server\nClient demo recording must be started before connecting\n");
  180. return;
  181. }
  182. // write the forced cd track number, or -1
  183. if (c == 4)
  184. {
  185. track = atoi(Cmd_Argv(3));
  186. Con_Printf ("Forcing CD track to %i\n", cls.forcetrack);
  187. }
  188. else
  189. track = -1;
  190. sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1));
  191. //
  192. // start the map up
  193. //
  194. if (c > 2)
  195. Cmd_ExecuteString ( va("map %s", Cmd_Argv(2)), src_command);
  196. //
  197. // open the demo file
  198. //
  199. COM_DefaultExtension (name, ".dem");
  200. Con_Printf ("recording to %s.\n", name);
  201. cls.demofile = fopen (name, "wb");
  202. if (!cls.demofile)
  203. {
  204. Con_Printf ("ERROR: couldn't open.\n");
  205. return;
  206. }
  207. cls.forcetrack = track;
  208. fprintf (cls.demofile, "%i\n", cls.forcetrack);
  209. cls.demorecording = true;
  210. }
  211. /*
  212. ====================
  213. CL_PlayDemo_f
  214. play [demoname]
  215. ====================
  216. */
  217. void CL_PlayDemo_f (void)
  218. {
  219. char name[256];
  220. int c;
  221. qboolean neg = false;
  222. if (cmd_source != src_command)
  223. return;
  224. if (Cmd_Argc() != 2)
  225. {
  226. Con_Printf ("play <demoname> : plays a demo\n");
  227. return;
  228. }
  229. //
  230. // disconnect from server
  231. //
  232. CL_Disconnect ();
  233. //
  234. // open the demo file
  235. //
  236. strcpy (name, Cmd_Argv(1));
  237. COM_DefaultExtension (name, ".dem");
  238. Con_Printf ("Playing demo from %s.\n", name);
  239. COM_FOpenFile (name, &cls.demofile);
  240. if (!cls.demofile)
  241. {
  242. Con_Printf ("ERROR: couldn't open.\n");
  243. cls.demonum = -1; // stop demo loop
  244. return;
  245. }
  246. cls.demoplayback = true;
  247. cls.state = ca_connected;
  248. cls.forcetrack = 0;
  249. while ((c = getc(cls.demofile)) != '\n')
  250. if (c == '-')
  251. neg = true;
  252. else
  253. cls.forcetrack = cls.forcetrack * 10 + (c - '0');
  254. if (neg)
  255. cls.forcetrack = -cls.forcetrack;
  256. // ZOID, fscanf is evil
  257. // fscanf (cls.demofile, "%i\n", &cls.forcetrack);
  258. }
  259. /*
  260. ====================
  261. CL_FinishTimeDemo
  262. ====================
  263. */
  264. void CL_FinishTimeDemo (void)
  265. {
  266. int frames;
  267. float time;
  268. cls.timedemo = false;
  269. // the first frame didn't count
  270. frames = (host_framecount - cls.td_startframe) - 1;
  271. time = realtime - cls.td_starttime;
  272. if (!time)
  273. time = 1;
  274. Con_Printf ("%i frames %5.1f seconds %5.1f fps\n", frames, time, frames/time);
  275. }
  276. /*
  277. ====================
  278. CL_TimeDemo_f
  279. timedemo [demoname]
  280. ====================
  281. */
  282. void CL_TimeDemo_f (void)
  283. {
  284. if (cmd_source != src_command)
  285. return;
  286. if (Cmd_Argc() != 2)
  287. {
  288. Con_Printf ("timedemo <demoname> : gets demo speeds\n");
  289. return;
  290. }
  291. CL_PlayDemo_f ();
  292. // cls.td_starttime will be grabbed at the second frame of the demo, so
  293. // all the loading time doesn't get counted
  294. cls.timedemo = true;
  295. cls.td_startframe = host_framecount;
  296. cls.td_lastframe = -1; // get a new message this frame
  297. }