Debian Codegraph @

Descent3/demofile.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/demofile.cpp $
21 * $Revision: 74 $
22 * $Date: 10/21/01 7:15p $
23 * $Author: Matt $
24 *
25 *
26 *
27 * $Log: /DescentIII/Main/demofile.cpp $
28 *
29 * 74 10/21/01 7:15p Matt
30 * Hack to set the ship_index field for each Player[] so the LOD models
31 * work right.
32 *
33 * 73 10/04/01 9:49a Matt
34 * Added missing semicolon.
35 *
36 * 72 10/03/01 11:39p Kevin
37 * init hud on demo playback
38 *
39 * 71 10/03/01 11:35p Kevin
40 * multiplayer stripes appear in demo playback
41 *
42 * 70 9/11/01 11:25a Matt
43 * Fixed rear-view camera problem when a demo using a player other than 0
44 * started and the rear-view was already on.
45 *
46 * 69 9/10/01 2:30p Matt
47 * Added code to clear the 'press space to continue...' message after
48 * player respawn.
49 *
50 * 68 9/10/01 2:07p Matt
51 * Made add-on data load before level is loaded so texture are mapped
52 * right.
53 *
54 * 67 4/21/00 2:50p Matt
55 * Added check for killing an invalid object type.
56 *
57 * 66 4/19/00 5:07p Matt
58 * From Duane for 1.4
59 * Added checks, asserts, and fixes for bad return values
60 *
61 * 65 3/23/00 2:35p Matt
62 * Added generic object id translastion when creating objects during demo
63 * playback.
64 *
65 * 64 10/22/99 2:38p Jay
66 * Update for the FinalBoss Laser Effect
67 *
68 * 63 7/28/99 9:39a Kevin
69 *
70 * 62 7/27/99 12:58p Kevin
71 * fixed napalm, omega & vauss rendering for other players in a
72 * multiplayer game
73 *
74 * 61 7/21/99 7:17p Chris
75 * Fixed an attach related crash in the load/save game code
76 *
77 * 60 7/21/99 2:32p Kevin
78 * currupt demo will no longer let you keep playing when the demo ends...
79 *
80 * 59 7/21/99 12:03p Kevin
81 * Changed version numbers around
82 *
83 * 58 7/14/99 4:59p Kevin
84 * fixed multiplayer weapons sitting still bug
85 *
86 * 57 6/10/99 9:07p Samir
87 * fixed weapon string name localization issues.
88 *
89 * 56 5/24/99 1:26a Matt
90 * Correctly deal with hit_info == NULL being passed to collision
91 * routines.
92 *
93 * 55 5/12/99 6:01p Jeff
94 * flip order of saving/loading objects and players
95 *
96 * 54 5/06/99 1:19a Samir
97 * shortened filename length to 128 because of problems with fopen and
98 * huge filenames.
99 *
100 * 53 5/06/99 12:34a Samir
101 * made the editbox take up to _MAX_FNAME characters. The cfile code has
102 * been changed to handle larger paths too, but just to keep some
103 * consistancy.
104 *
105 * 52 4/27/99 9:59p Kevin
106 * CD not requred for dedicated server. Also improved demo playback
107 *
108 * 51 4/25/99 10:19p Matt
109 * Fixed multiplayer and demo problems will killing an object from script,
110 * and cleaned up the death code a bit in the process.
111 *
112 * 50 4/23/99 11:39p Kevin
113 * doh!
114 *
115 * 49 4/23/99 10:28p Kevin
116 * fixed problems with minimal install and demo/savegames
117 *
118 * 48 4/21/99 11:05a Kevin
119 * new ps_rand and ps_srand to replace rand & srand
120 *
121 * 47 4/16/99 6:00p Kevin
122 * Bunches of Demo stuff
123 *
124 * 46 4/15/99 1:38a Jeff
125 * changes for linux compile
126 *
127 * 45 4/14/99 2:50a Jeff
128 * fixed some case mismatched #includes
129 *
130 * 44 3/16/99 6:43p Kevin
131 * OEM Split fixes
132 *
133 * 43 3/12/99 7:53p Jeff
134 * save player rotating balls info, handle obj_observer with obj_player,
135 * handle objects that use life left, handle 2d sound playing
136 *
137 * 42 3/11/99 11:40a Kevin
138 * Added SetObjDead code for multiplayer stuff
139 *
140 * 41 3/10/99 6:20p Jeff
141 * many fixes to demo system. Fixed IGC so cameras move...fixed osiris to
142 * be restored correctly, and it handles errors on restore
143 *
144 * 40 3/10/99 2:25p Kevin
145 * Save/Load and Demo file fixes
146 *
147 * 39 2/25/99 10:58a Matt
148 * Added new explosion system.
149 *
150 * 38 2/23/99 10:45a Kevin
151 *
152 * 37 2/23/99 10:37a Kevin
153 * Added camera to list of objects that are saved when changed
154 *
155 * 36 2/23/99 12:44a Jeff
156 * added support for in-game-cinematics in demo system
157 *
158 * 35 2/22/99 9:22p Kevin
159 * Added DT_CINEMATICS
160 *
161 * 34 2/17/99 2:44p Kevin
162 * Adde movie making capabilities to the demo playback system
163 *
164 * 33 2/15/99 11:48p Matt
165 * Fixed compile warnings
166 *
167 * 32 2/15/99 1:22p Kevin
168 * Changes for GameGauge
169 *
170 * 31 2/08/99 8:59p Kevin
171 * Fixed powerups
172 *
173 * 30 2/08/99 7:05p Kevin
174 * Trying to get demo system working with powerup scripts
175 *
176 * 29 2/07/99 1:17a Jeff
177 * peppered UI dialogs that were missing NEWUIRES_FORCEQUIT to handle it
178 *
179 * 28 2/02/99 1:20p Kevin
180 * Added script saving
181 *
182 * 27 2/02/99 12:43p Kevin
183 * Fixes to the Save/Load game system, and the demo system to work with
184 * the new OSIRIS
185 *
186 * 26 1/31/99 7:25p Matt
187 * Renamed a bunch of functions to have HUD capitalized
188 *
189 * 25 1/31/99 3:44p Matt
190 * Streamlined game sequencing
191 *
192 * 24 1/29/99 2:08p Jeff
193 * localization
194 *
195 * 23 1/29/99 12:47p Matt
196 * Rewrote the doorway system
197 *
198 * 22 1/23/99 10:10p Kevin
199 * Added the start of osiris support into the demo system
200 *
201 * 21 1/21/99 11:15p Jeff
202 * pulled out some structs and defines from header files and moved them
203 * into seperate header files so that multiplayer dlls don't require major
204 * game headers, just those new headers. Side effect is a shorter build
205 * time. Also cleaned up some header file #includes that weren't needed.
206 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
207 * manage.h and multi.h
208 *
209 * 20 1/15/99 7:52p Chris
210 * Updated ObjSetPos() to include a f_update_attach_children flag
211 *
212 * 19 1/08/99 2:55p Samir
213 * Ripped out OSIRIS1.
214 *
215 * 18 11/24/98 3:57p Kevin
216 * Demo system immprovements
217 *
218 * 17 11/24/98 12:08p Kevin
219 *
220 * 16 11/24/98 10:41a Kevin
221 * Demo system
222 *
223 * 15 11/23/98 4:52p Kevin
224 * Demo system enhancments
225 *
226 * 14 11/23/98 3:11p Kevin
227 * Demo system
228 *
229 * 13 11/19/98 5:53p Kevin
230 *
231 * 12 11/19/98 5:40p Kevin
232 * Demo system
233 *
234 * 11 11/17/98 4:16p Kevin
235 * Demo recording system
236 *
237 * 10 11/11/98 2:46p Kevin
238 * Demo recording system work
239 *
240 * 9 11/09/98 4:12p Kevin
241 *
242 * 8 11/05/98 5:54p Kevin
243 * Demo system work
244 *
245 * 7 10/15/98 12:22a Matt
246 * Fixed compile warnings
247 *
248 * 6 10/12/98 1:46p Samir
249 * new Program_version parameters.
250 *
251 * 5 10/08/98 12:00p Kevin
252 * Demo system work
253 *
254 * 4 10/06/98 5:45p Kevin
255 * Added new configuration for demo
256 *
257 * 3 10/05/98 12:09p Kevin
258 * Converted projects to VC6 and demo file stuff added
259 *
260 * 2 10/05/98 10:32a Kevin
261 * Initial version
262 *
263 * 1 10/05/98 10:22a Kevin
264 *
265 * $NoKeywords: $
266 */
267
268 #include <stdio.h>
269 #include "cfile.h"
270 #include "objinfo.h"
271 #include "ship.h"
272 #include "ui.h"
273 #include "newui.h"
274 #include "hud.h"
275 #include "stringtable.h"
276 #include "program.h"
277 #include "Mission.h"
278 #include "game.h"
279 #include "gamesequence.h"
280 #include "weapon.h"
281 #include "damage.h"
282 #include "mem.h"
283 #include "ObjScript.h"
284 #include "hlsoundlib.h"
285 #include "viseffect.h"
286 #include "collide.h"
287 #include "sounds.h"
288 #include "fireball.h"
289 #include "attach.h"
290 #include "gameloop.h"
291 #include "multi.h"
292 #include "multisafe.h"
293 #include "osiris_dll.h"
294 #include "args.h"
295 #include "gamecinematics.h"
296 #include "psrand.h"
297 #include "cockpit.h"
298 // We borrow a lot of code from the savegame system
299 #include "gamesave.h"
300 #include "demofile.h"
301
302 extern bool is_multi_demo;
303 static CFILE *Demo_cfp = NULL;
304 char Demo_fname[_MAX_PATH * 2];
305 static char Old_demo_fname[_MAX_PATH * 2];
306 static float Demo_next_frame = 0;
307 static float Demo_frame_time = 0;
308 static float Demo_last_pinfo;
309 float Demo_frame_ofs;
310 static int Demo_auto_idx = 0;
311 uint32_t Demo_flags = 0;
312 uint16_t Demo_obj_map[MAX_OBJECTS];
313 static bool Demo_turretchanged[MAX_OBJECTS];
314 bool Demo_looping = false;
315 bool Demo_paused = false;
316 bool Demo_do_one_frame = false;
317 bool Demo_restart = false;
318 bool Demo_auto_play = false;
319 static bool Demo_first_frame = true;
320 bool Demo_make_movie = false;
321
322 #define DEMO_PINFO_UPDATE .1
323 #define MAX_COOP_TURRETS 400
324 extern float turret_holder[MAX_COOP_TURRETS];
325
326 extern gs_tables *gs_Xlates;
327
328 extern float Min_frametime;
329 extern float Max_frametime;
330 extern float Avg_frametime;
331 extern uint32_t Frames_counted;
332 extern bool Game_paused;
333
334 static bool Demo_play_fast = false;
335
336 extern void PageInAllData(void);
337
338 // Prompts user for filename and starts recording if successfull
339 void DemoToggleRecording() {
340 char szfile[_MAX_PATH * 2];
341 szfile[0] = '\0';
342 if (Demo_flags == DF_RECORDING) {
343 // Stop recording and close the file
344 cfclose(Demo_cfp);
345 Demo_flags = DF_NONE;
346 AddBlinkingHUDMessage(TXT_DEMOSAVED);
347
348 Demo_fname[0] = '\0';
349 return;
350 } else if (Demo_flags == DF_PLAYBACK) {
351 // We can't record a demo while we are playing back a demo
352 return;
353 }
354
355 // hand coded 128 because long filenames were failing in cfopen(which called fopen, which failed on very
356 // long filenames. instead of printing a message out to the user, just don't allow filenames that long)
357 if (DoEditDialog(TXT_DEMOFILENAME, szfile, 128)) {
358 if (stricmp(szfile + (strlen(szfile) - 4), ".dem") != 0) {
359 strcat(szfile, ".dem");
360 }
361 ddio_MakePath(Demo_fname, Base_directory_write, "demo", szfile, NULL);
362 mprintf(0, "Saving demo to file: %s\n", Demo_fname);
363 // Try to create the file
364 Demo_cfp = cfopen(Demo_fname, "wb");
365 if (Demo_cfp) {
366 // Setup the demo variables
367 if (!(Game_mode & GM_MULTI)) {
368 MultiBuildMatchTables();
369 }
370 // Male sure we write the player info the first frame
371 Demo_last_pinfo = timer_GetTime() - (DEMO_PINFO_UPDATE * 2);
372 Demo_flags = DF_RECORDING;
373 // Write the header
374 DemoWriteHeader();
375 DemoStartNewFrame();
376 } else {
377 // cfopen failed
378 AddBlinkingHUDMessage(TXT_DEMOCANTCREATE);
379 Demo_fname[0] = '\0';
380 return;
381 }
382 }
383 }
384
385 void DemoWriteHeader() {
386 char szsig[10];
387 strcpy(szsig, D3_DEMO_SIG_NEW);
388 ASSERT(Demo_flags == DF_RECORDING);
389
390 // Start off with the signature
391 cf_WriteString(Demo_cfp, (const char *)szsig);
392 // Next is the version
393 cf_WriteShort(Demo_cfp, GAMESAVE_VERSION);
394 // Write the mission filename
395 if (Current_mission.filename && (stricmp("d3_2.mn3", Current_mission.filename) == 0)) {
396 cf_WriteString(Demo_cfp, "d3.mn3");
397 } else {
398 cf_WriteString(Demo_cfp, Current_mission.filename ? Current_mission.filename : "");
399 }
400
401 // Level number
402 cf_WriteInt(Demo_cfp, Current_mission.cur_level);
403 // Gametime
404 cf_WriteFloat(Demo_cfp, Gametime);
405
406 // Frame count
407 cf_WriteInt(Demo_cfp, FrameCount);
408
409 // Now store the world state (borrowing save game code)
410
411 // Load translation tables
412 SGSXlateTables(Demo_cfp);
413
414 // save out room information.
415 SGSRooms(Demo_cfp);
416
417 // save out triggers
418 SGSTriggers(Demo_cfp);
419
420 // save out object information.
421 SGSObjects(Demo_cfp);
422
423 // players
424 SGSPlayers(Demo_cfp);
425
426 // save out viseffects
427 SGSVisEffects(Demo_cfp);
428
429 // save out spew
430 SGSSpew(Demo_cfp);
431
432 Osiris_SaveSystemState(Demo_cfp);
433
434 cf_WriteShort(Demo_cfp, Player_num);
435 }
436
437 void DemoStartNewFrame() {
438 if (Demo_flags != DF_RECORDING) {
439 return;
440 }
441
442 // Start with the gametime of this frame
443 cf_WriteByte(Demo_cfp, DT_NEW_FRAME);
444 cf_WriteFloat(Demo_cfp, Gametime);
445 cf_WriteFloat(Demo_cfp, Frametime);
446
447 if ((timer_GetTime() - Demo_last_pinfo) >= DEMO_PINFO_UPDATE) {
448 DemoWritePlayerInfo();
449 Demo_last_pinfo = timer_GetTime();
450 }
451 }
452
453 // Store object num, position, orientation
454 // WB info, Anim states
455 void DemoWriteChangedObj(object *op) {
456 ASSERT(op);
457 if (Demo_flags == DF_RECORDING) {
458 gs_WriteByte(Demo_cfp, DT_OBJ);
459 cf_WriteShort(Demo_cfp, OBJNUM(op));
460 // positional information
461 gs_WriteInt(Demo_cfp, op->roomnum);
462 gs_WriteVector(Demo_cfp, op->pos);
463 gs_WriteMatrix(Demo_cfp, op->orient);
464 if (op->type == OBJ_PLAYER || op->type == OBJ_OBSERVER) {
465 gs_WriteInt(Demo_cfp, Players[op->id].flags);
466 }
467 }
468 }
469
470 void DemoWriteWeaponFire(uint16_t objectnum, vector *pos, vector *dir, uint16_t weaponnum,
471 uint16_t weapobjnum, int16_t gunnum) {
472 uint32_t uniqueid = MultiGetMatchChecksum(OBJ_WEAPON, weaponnum);
473 if (weapobjnum == -1)
474 return;
475 if (!Weapons[weaponnum].used) {
476 Int3();
477 }
478 if (Demo_flags == DF_RECORDING) {
479 gs_WriteByte(Demo_cfp, DT_WEAPON_FIRE);
480 cf_WriteFloat(Demo_cfp, Gametime);
481 cf_WriteShort(Demo_cfp, objectnum);
482 cf_WriteInt(Demo_cfp, uniqueid);
483 gs_WriteVector(Demo_cfp, *pos);
484 gs_WriteVector(Demo_cfp, *dir);
485 gs_WriteShort(Demo_cfp, weapobjnum);
486 cf_WriteShort(Demo_cfp, gunnum);
487 }
488 }
489
490 void DemoWriteHudMessage(uint32_t color, bool blink, char *msg) {
491 if (Demo_flags == DF_RECORDING) {
492 cf_WriteByte(Demo_cfp, DT_HUD_MESSAGE);
493 cf_WriteInt(Demo_cfp, color);
494 cf_WriteByte(Demo_cfp, blink ? 1 : 0);
495 cf_WriteString(Demo_cfp, msg);
496 }
497 }
498
499 void DemoWriteChangedObjects() {
500 int i;
501 // int num_changed = 0;
502 if (Demo_flags == DF_RECORDING) {
503 for (i = 0; i <= Highest_object_index; i++) {
504
505 if ((Objects[i].type == OBJ_PLAYER) || (Objects[i].type == OBJ_OBSERVER) || (Objects[i].type == OBJ_ROBOT) ||
506 (Objects[i].type == OBJ_POWERUP) || (Objects[i].type == OBJ_CLUTTER) || (Objects[i].type == OBJ_BUILDING) ||
507 (Objects[i].type == OBJ_CAMERA)) {
508 if ((Objects[i].flags & OF_MOVED_THIS_FRAME)) //||(Objects[i].mtype.phys_info.velocity!=Zero_vector))
509 {
510 // mprintf(0,"Object moved this frame! type = %d\n",Objects[i].type);
511 DemoWriteChangedObj(&Objects[i]);
512 // num_changed++;
513 }
514 }
515 }
516 // if(num_changed)
517 // mprintf(0,"%d Objects moved this frame!\n",num_changed);
518 }
519 }
520
521 void DemoWriteObjCreate(uint8_t type, uint16_t id, int roomnum, vector *pos, const matrix *orient, int parent_handle,
522 object *obj) {
523
524 if (Demo_flags == DF_RECORDING) {
525
526 if ((type == OBJ_ROBOT) || (type == OBJ_POWERUP) || (type == OBJ_CLUTTER) || (type == OBJ_BUILDING) ||
527 (type == OBJ_CAMERA)) {
528 cf_WriteByte(Demo_cfp, DT_OBJ_CREATE);
529 // cf_WriteFloat(Demo_cfp,Gametime);
530 cf_WriteByte(Demo_cfp, type);
531 cf_WriteByte(Demo_cfp, orient ? 1 : 0);
532 cf_WriteShort(Demo_cfp, id);
533 cf_WriteInt(Demo_cfp, roomnum);
534 gs_WriteVector(Demo_cfp, *pos);
535 cf_WriteInt(Demo_cfp, parent_handle);
536 if (orient)
537 gs_WriteMatrix(Demo_cfp, *orient);
538
539 // Add the script name if there is one
540 // Start with the name len (minus the null)
541
542 cf_WriteShort(Demo_cfp, OBJNUM(obj));
543 }
544 }
545 }
546
547 #define MAX_COOP_TURRETS 400
548 extern float turret_holder[MAX_COOP_TURRETS];
549
550 void DemoWriteTurretChanged(uint16_t objnum) { Demo_turretchanged[objnum] = true; }
551
552 void DemoReadTurretChanged(void) {
553 multi_turret multi_turret_info;
554 int objnum;
555 uint16_t num_turrets;
556 float turr_time;
557 int count = 0;
558 float do_time;
559
560 do_time = cf_ReadFloat(Demo_cfp);
561 int16_t old_objnum = cf_ReadShort(Demo_cfp);
562 objnum = Demo_obj_map[old_objnum];
563
564 turr_time = cf_ReadFloat(Demo_cfp);
565 num_turrets = cf_ReadShort(Demo_cfp);
566 multi_turret_info.keyframes = (float *)&turret_holder;
567 multi_turret_info.num_turrets = num_turrets;
568 for (int i = 0; i < num_turrets; i++) {
569 if (MAX_COOP_TURRETS > i)
570 multi_turret_info.keyframes[i] = cf_ReadFloat(Demo_cfp);
571 }
572
573 if (Objects[objnum].type != OBJ_NONE) {
574 // mprintf(0,"Updating turret %d!\n",objnum);
575 ObjSetTurretUpdate(objnum, &multi_turret_info);
576 }
577 }
578
579 void DemoWriteObjAnimChanged(uint16_t objnum) {
580
581 custom_anim multi_anim_info;
582 if (ObjGetAnimUpdate(objnum, &multi_anim_info)) // Checks if obj is still alive and all
583 {
584 cf_WriteByte(Demo_cfp, DT_OBJ_ANIM);
585 cf_WriteFloat(Demo_cfp, Gametime);
586 cf_WriteShort(Demo_cfp, objnum);
587
588 cf_WriteFloat(Demo_cfp, multi_anim_info.server_time);
589 cf_WriteShort(Demo_cfp, multi_anim_info.server_anim_frame);
590 cf_WriteByte(Demo_cfp, multi_anim_info.anim_start_frame);
591 cf_WriteByte(Demo_cfp, multi_anim_info.anim_end_frame);
592
593 cf_WriteFloat(Demo_cfp, multi_anim_info.anim_time);
594 cf_WriteFloat(Demo_cfp, multi_anim_info.max_speed);
595
596 cf_WriteByte(Demo_cfp, multi_anim_info.flags);
597 cf_WriteShort(Demo_cfp, multi_anim_info.anim_sound_index);
598 }
599 }
600
601 void DemoReadObjAnimChanged(void) {
602 custom_anim multi_anim_info;
603 int objnum;
604 int count = 0;
605 float changetime;
606 changetime = cf_ReadFloat(Demo_cfp);
607 objnum = cf_ReadShort(Demo_cfp);
608
609 multi_anim_info.server_time = cf_ReadFloat(Demo_cfp);
610 multi_anim_info.server_anim_frame = cf_ReadShort(Demo_cfp);
611 multi_anim_info.anim_start_frame = cf_ReadByte(Demo_cfp);
612 multi_anim_info.anim_end_frame = cf_ReadByte(Demo_cfp);
613 multi_anim_info.anim_time = cf_ReadFloat(Demo_cfp);
614 multi_anim_info.max_speed = cf_ReadFloat(Demo_cfp);
615 multi_anim_info.flags = cf_ReadByte(Demo_cfp);
616 multi_anim_info.anim_sound_index = cf_ReadShort(Demo_cfp);
617
618 if (Objects[objnum].type != OBJ_NONE) {
619 ObjSetAnimUpdate(objnum, &multi_anim_info);
620 }
621 }
622
623 void DemoWriteKillObject(object *hit_obj, object *killer, float damage, int death_flags, float delay, int seed) {
624 cf_WriteByte(Demo_cfp, DT_OBJ_EXPLODE);
625 cf_WriteShort(Demo_cfp, OBJNUM(hit_obj));
626 cf_WriteShort(Demo_cfp, killer ? OBJNUM(killer) : 65535);
627 cf_WriteFloat(Demo_cfp, damage);
628 cf_WriteInt(Demo_cfp, death_flags);
629 cf_WriteFloat(Demo_cfp, delay);
630 cf_WriteShort(Demo_cfp, seed);
631 }
632
633 void DemoReadKillObject(void) {
634 int16_t hit_objnum = cf_ReadShort(Demo_cfp);
635 int16_t killer = cf_ReadShort(Demo_cfp);
636 float damage = cf_ReadFloat(Demo_cfp);
637 int death_flags = cf_ReadInt(Demo_cfp);
638 float delay = cf_ReadFloat(Demo_cfp);
639 int16_t seed = cf_ReadShort(Demo_cfp);
640
641 if (!(IS_GENERIC(Objects[hit_objnum].type) || (Objects[hit_objnum].type == OBJ_DOOR)))
642 return; // bail if invalid object type
643
644 ps_srand(seed);
645 KillObject(&Objects[hit_objnum], (killer != 65535) ? &Objects[killer] : NULL, damage, death_flags, delay);
646 }
647
648 void DemoWritePlayerDeath(object *player, bool melee, int fate) {
649 cf_WriteByte(Demo_cfp, DT_PLAYER_DEATH);
650 cf_WriteShort(Demo_cfp, OBJNUM(player));
651 cf_WriteByte(Demo_cfp, melee ? 1 : 0);
652 cf_WriteInt(Demo_cfp, fate);
653 }
654
655 void DemoReadPlayerDeath(void) {
656 int16_t playernum = cf_ReadShort(Demo_cfp);
657 uint8_t melee = cf_ReadByte(Demo_cfp);
658 int fate = cf_ReadInt(Demo_cfp);
659 InitiatePlayerDeath(&Objects[playernum], melee ? true : false, fate);
660 }
661
662 void DemoWrite2DSound(int16_t soundidx, float volume) {
663 cf_WriteByte(Demo_cfp, DT_2D_SOUND);
664 cf_WriteShort(Demo_cfp, soundidx);
665 cf_WriteFloat(Demo_cfp, volume);
666 }
667
668 void DemoRead2DSound(void) {
669 int soundidx = cf_ReadShort(Demo_cfp);
670 float volume = cf_ReadFloat(Demo_cfp);
671
672 Sound_system.Play2dSound(soundidx, volume);
673 }
674
675 void DemoWrite3DSound(int16_t soundidx, uint16_t objnum, int priority, float volume) {
676 cf_WriteByte(Demo_cfp, DT_3D_SOUND);
677 cf_WriteShort(Demo_cfp, objnum);
678 cf_WriteShort(Demo_cfp, soundidx);
679 cf_WriteFloat(Demo_cfp, volume);
680 }
681
682 void DemoRead3DSound(void) {
683 int objnum;
684 int16_t soundidx;
685 float volume;
686
687 objnum = cf_ReadShort(Demo_cfp);
688
689 soundidx = cf_ReadShort(Demo_cfp);
690 volume = cf_ReadFloat(Demo_cfp);
691
692 Sound_system.Play3dSound(soundidx, &Objects[objnum], volume);
693 }
694
695 void DemoWriteScriptEvent() {}
696
697 int FrameDemoDelta = 0;
698 int DemoFrameCount = 0;
699
700 int DemoPlaybackFile(char *filename) {
701 is_multi_demo = false;
702 mprintf(0, "Playing back demo file!\n");
703 MultiBuildMatchTables();
704 if (FindArg("-makemovie")) {
705 Demo_make_movie = true;
706 }
707
708 if (FindArg("-fastdemo")) {
709 Demo_play_fast = true;
710 }
711 Demo_first_frame = true;
712 for (int i = 0; i < MAX_OBJECTS; i++) {
713 Demo_obj_map[i] = i;
714 }
715
716 if (Demo_flags == DF_RECORDING) {
717 DemoToggleRecording();
718 }
719 Demo_cfp = cfopen(filename, "rb");
720
721 if (Demo_cfp == NULL) {
722 DoMessageBox(TXT_ERROR, TXT_CANTLOAD, MSGBOX_OK, UICOL_WINDOW_TITLE, UICOL_TEXT_NORMAL);
723 return 0;
724 }
725
726 Demo_flags = DF_PLAYBACK;
727 FrameDemoDelta = FrameCount;
728 if (!DemoReadHeader()) {
729 Demo_flags = DF_NONE;
730 DoMessageBox(TXT_ERROR, TXT_BADDEMOFILE, MSGBOX_OK, UICOL_WINDOW_TITLE, UICOL_TEXT_NORMAL);
731 return 0;
732 }
733
734 return 1;
735 }
736
737 extern bool IsRestoredGame;
738 int DemoReadHeader() {
739 char szsig[10];
740 int16_t ver;
741 char demo_mission[_MAX_PATH];
742 int level_num;
743 int frame_count;
744 float demo_gametime;
745 mprintf(0, "Reading demo header...\n");
746 ASSERT(Demo_flags == DF_PLAYBACK);
747 cf_ReadString((char *)szsig, 10, Demo_cfp);
748 ver = cf_ReadShort(Demo_cfp);
749
750 if (strcmp(szsig, D3_DEMO_SIG) != 0) {
751 if (strcmp(szsig, D3_DEMO_SIG_NEW) != 0) {
752 mprintf(0, "Bad demo header signature!\n");
753 return 0;
754 }
755 }
756
757 if (ver > GAMESAVE_VERSION) {
758 // This file version is too new!
759 // return 0;
760 }
761
762 cf_ReadString(demo_mission, _MAX_PATH, Demo_cfp);
763 level_num = cf_ReadInt(Demo_cfp);
764 demo_gametime = cf_ReadFloat(Demo_cfp);
765 frame_count = cf_ReadInt(Demo_cfp);
766
767 // Now load the mission
768 Osiris_DisableCreateEvents();
769 IsRestoredGame = true;
770 if (LoadMission((const char *)demo_mission)) {
771 mng_LoadAddonPages();
772
773 SetCurrentLevel(level_num);
774 if (!LoadAndStartCurrentLevel()) {
775 mprintf(0, "Couldn't start new level to play back the demo!\n");
776 Osiris_EnableCreateEvents();
777 return 0;
778 }
779 } else {
780 mprintf(0, "Couldn't load the level to play back the demo!\n");
781 Osiris_EnableCreateEvents();
782 return 0;
783 }
784 Osiris_EnableCreateEvents();
785
786 FrameCount = frame_count;
787 Demo_next_frame = demo_gametime;
788
789 if (gs_Xlates)
790 mem_free(gs_Xlates);
791 gs_Xlates = (struct gs_tables *)mem_malloc(sizeof(gs_tables));
792
793 try {
794 LGSXlateTables(Demo_cfp);
795
796 LGSRooms(Demo_cfp);
797
798 LGSTriggers(Demo_cfp);
799
800 for (int j = 0; j <= Highest_object_index; j++) {
801 if ((Objects[j].type == OBJ_PLAYER) && (Objects[j].id != Player_num)) {
802 object *objp = &Objects[j];
803 objp->type = OBJ_GHOST;
804 objp->movement_type = MT_NONE;
805 objp->render_type = RT_NONE;
806 SetObjectControlType(objp, CT_NONE);
807 }
808 }
809
810 LGSObjects(Demo_cfp, ver);
811 // Fix up the object list for use in MSAFE
812 for (int a = 0; a < MAX_OBJECTS; a++) {
813 Server_object_list[a] = a;
814 Objects[a].flags |= OF_SERVER_OBJECT;
815 }
816
817 LGSPlayers(Demo_cfp);
818
819 LGSVisEffects(Demo_cfp);
820
821 LGSSpew(Demo_cfp);
822
823 if (!Osiris_RestoreSystemState(Demo_cfp)) {
824 mprintf(0, "Error restoring Osiris\n");
825 return 0;
826 }
827
828 Player_num = cf_ReadShort(Demo_cfp);
829
830 Viewer_object = &Objects[Players[Player_num].objnum];
831 Player_object = Viewer_object;
832
833 InitShipHUD(Players[Player_num].ship_index);
834 InitCockpit(Players[Player_num].ship_index);
835
836 if (GetHUDMode() == HUD_COCKPIT)
837 SetHUDMode(HUD_COCKPIT);
838 else if (GetHUDMode() == HUD_FULLSCREEN)
839 SetHUDMode(HUD_FULLSCREEN);
840
841 // Reset rearview cameras since Player_object may have changed
842 extern void RestoreCameraRearviews();
843 extern int Camera_view_mode[];
844 Camera_view_mode[0] = Camera_view_mode[2] = 0; //(0==CV_NONE) Force reinitialization
845 RestoreCameraRearviews();
846 } catch (...) {
847 mprintf(0, "Someone threw an exception while reading the demo header!\n");
848 return 0;
849 }
850
851 PageInAllData();
852
853 // Don't free this variable here, because we need to xlate ids when creating objects during playback
854 // mem_free(gs_Xlates);
855 // gs_Xlates = NULL;
856
857 // Turn sounds back on
858 Sound_system.ResumeSounds();
859 Min_frametime = 500;
860 Max_frametime = 0;
861 Avg_frametime = 0;
862 Frames_counted = 0;
863
864 int a;
865 for (a = 0; a < MAX_PLAYERS; a++) {
866 Players[a].weapon_speed_scalar = 1;
867 Players[a].movement_scalar = 1;
868 Players[a].damage_scalar = 1;
869 Players[a].armor_scalar = 1;
870 Players[a].turn_scalar = 1;
871 Players[a].weapon_recharge_scalar = 1;
872
873 // Set the ship number for this player, since this value is not saved with demo header
874 for (int objnum = 0; objnum <= Highest_object_index; objnum++) {
875 // Look for a player object that is this player
876 if ((Objects[objnum].type == OBJ_PLAYER) && (Objects[objnum].id == a)) {
877 // We found an object for this player. Now find a ship that matches this object's model
878 for (int s = 0; s < MAX_SHIPS; s++) {
879 if (Ships[s].used && (Ships[s].model_handle == Objects[objnum].rtype.pobj_info.model_num)) {
880 // Found a matching ship, so set the player to use that ship
881 Players[a].ship_index = s;
882 break; // found a ship for this player, so stop looking
883 }
884 }
885
886 break; // found an object for this player, so stop looking
887 }
888 }
889 }
890
891 return 1;
892 }
893
894 void DemoReadObj() {
895 int16_t objnum;
896 object *obj;
897 vector pos;
898 matrix orient;
899 ASSERT(Demo_flags == DF_PLAYBACK);
900 objnum = cf_ReadShort(Demo_cfp);
901
902 obj = &Objects[Demo_obj_map[objnum]];
903 // ASSERT(obj->type!=OBJ_NONE);
904
905 int roomnum = cf_ReadInt(Demo_cfp);
906 gs_ReadVector(Demo_cfp, pos);
907 gs_ReadMatrix(Demo_cfp, orient);
908 if (obj->type == OBJ_PLAYER || obj->type == OBJ_OBSERVER) {
909 if (obj->id != Player_num)
910 is_multi_demo = true;
911 Players[obj->id].flags = cf_ReadInt(Demo_cfp);
912
913 if (!(Players[obj->id].flags & PLAYER_FLAGS_DEAD))
914 ResetPersistentHUDMessage();
915 }
916 // if((!((obj->flags&OF_DYING)||(obj->flags&OF_EXPLODING)||(obj->flags&OF_DEAD)))&&obj->type!=255)
917
918 if (obj->type != OBJ_NONE) {
919 obj->mtype.phys_info.velocity = Zero_vector;
920 obj->mtype.phys_info.rotvel = Zero_vector;
921 ObjSetPos(obj, &pos, roomnum, &orient, true);
922 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
923 if ((!(obj->flags & OF_ATTACHED)) && (pm->n_attach))
924 AttachUpdateSubObjects(obj);
925 }
926 }
927
928 void DemoReadHudMessage() {
929 char msg[HUD_MESSAGE_LENGTH];
930 int color = cf_ReadInt(Demo_cfp);
931 uint8_t blink = cf_ReadByte(Demo_cfp);
932 cf_ReadString(msg, HUD_MESSAGE_LENGTH, Demo_cfp);
933
934 if (color) {
935 AddColoredHUDMessage(color, msg);
936 } else if (blink) {
937 AddBlinkingHUDMessage(msg);
938 } else {
939 AddHUDMessage(msg);
940 }
941 }
942
943 void DemoReadWeaponFire() {
944 vector pos, dir;
945 float gametime;
946 uint32_t uniqueid;
947 object *obj;
948 int16_t weaponnum, objnum, weapobjnum;
949 vector laser_pos, laser_dir;
950
951 // Mass driver is hack
952 static int mass_driver_id = -2;
953 static int mass_driver_ring = -2;
954 static int omega_id = -2;
955 static int vauss_id = -2;
956 static int napalm_id = -2;
957
958 if (mass_driver_id == -2) {
959 mass_driver_id = FindWeaponName("Mass Driver");
960 ASSERT(mass_driver_id != -1); // DAJ -1FIX
961 mass_driver_ring = FindTextureName("CloakRing.tga1");
962 ASSERT(mass_driver_ring != -1); // DAJ -1FIX
963 omega_id = FindWeaponName("Omega");
964 ASSERT(omega_id != -1); // DAJ -1FIX
965 vauss_id = FindWeaponName("Vauss");
966 ASSERT(vauss_id != -1); // DAJ -1FIX
967 napalm_id = FindWeaponName("Napalm");
968 ASSERT(napalm_id != -1); // DAJ -1FIX
969 }
970
971 gametime = cf_ReadFloat(Demo_cfp);
972 int16_t old_objnum = cf_ReadShort(Demo_cfp);
973 objnum = Demo_obj_map[old_objnum];
974 obj = &Objects[objnum];
975 ASSERT(Objects[objnum].type != OBJ_NONE);
976
977 uniqueid = cf_ReadInt(Demo_cfp);
978 gs_ReadVector(Demo_cfp, pos);
979 gs_ReadVector(Demo_cfp, dir);
980 laser_pos = pos;
981 laser_dir = dir;
982 weaponnum = MultiMatchWeapon(uniqueid);
983 weapobjnum = cf_ReadShort(Demo_cfp);
984 int16_t gunnum = cf_ReadShort(Demo_cfp);
985 ASSERT(uniqueid != 0xffffffff);
986 ASSERT(dir != Zero_vector);
987
988 // This is a hack for the napalm, omega & vauss to prevent making files incompatible
989 if ((obj->type == OBJ_PLAYER) && (obj->id != Player_num)) {
990 if (omega_id == weaponnum) {
991 Players[obj->id].weapon[PW_PRIMARY].index = OMEGA_INDEX;
992 } else if (vauss_id == weaponnum) {
993 Players[obj->id].weapon[PW_PRIMARY].index = VAUSS_INDEX;
994 } else if (napalm_id == weaponnum) {
995 Players[obj->id].weapon[PW_PRIMARY].index = NAPALM_INDEX;
996 }
997 }
998
999 uint16_t new_weap_objnum = CreateAndFireWeapon(&pos, &dir, &Objects[objnum], weaponnum);
1000 if (0xffff == new_weap_objnum) {
1001 Int3();
1002 return;
1003 }
1004 Objects[new_weap_objnum].ctype.laser_info.src_gun_num = gunnum;
1005
1006 mprintf(0, "Player %d Firing weapon (%d) -- using objnum %d (old num = %d)\n",
1007 obj->id, weaponnum, new_weap_objnum, weapobjnum);
1008
1009 Demo_obj_map[weapobjnum] = new_weap_objnum;
1010 int16_t weapon_num = weaponnum;
1011
1012 if (Weapons[weapon_num].sounds[WSI_FLYING] != SOUND_NONE_INDEX)
1013 Sound_system.Play3dSound(Weapons[weapon_num].sounds[WSI_FLYING], &Objects[objnum]);
1014
1015 // Do a muzzle flash from this gun
1016 if (Weapons[weapon_num].flags & WF_MUZZLE) {
1017 int visnum;
1018 vector newpos;
1019
1020 if (obj == &Objects[Players[Player_num].objnum]) {
1021 newpos = laser_pos + (laser_dir);
1022 visnum = VisEffectCreate(VIS_FIREBALL, MUZZLE_FLASH_INDEX, obj->roomnum, &newpos);
1023 } else {
1024 newpos = laser_pos + (laser_dir / 2);
1025 visnum = VisEffectCreate(VIS_FIREBALL, MUZZLE_FLASH_INDEX, obj->roomnum, &newpos);
1026 }
1027
1028 // Make this guy live for a very short time
1029 if (visnum >= 0) {
1030 vis_effect *vis = &VisEffects[visnum];
1031
1032 vis->lifetime = .08f;
1033 vis->lifeleft = .08f;
1034 vis->size = 1.0f;
1035
1036 vis->movement_type = MT_PHYSICS;
1037 vis->velocity = obj->mtype.phys_info.velocity;
1038
1039 // Make some smoke!
1040 visnum = VisEffectCreate(VIS_FIREBALL, MED_SMOKE_INDEX, obj->roomnum, &newpos);
1041 if (visnum >= 0) {
1042 vis_effect *vis = &VisEffects[visnum];
1043
1044 vis->lifetime = .3f;
1045 vis->lifeleft = .3f;
1046
1047 vis->movement_type = MT_PHYSICS;
1048 vis->velocity = obj->mtype.phys_info.velocity;
1049 vis->velocity.y += 10;
1050 }
1051 }
1052 }
1053
1054 // Check for mass driver
1055 if (weaponnum == mass_driver_id) {
1056 // Create a fire trail for this weapon
1057 // find out where this weapon hits
1058 fvi_query fq;
1059 fvi_info hit_data;
1060
1061 vector dest_vec = laser_pos + (laser_dir * 5000);
1062
1063 fq.p0 = &laser_pos;
1064 fq.startroom = obj->roomnum;
1065 fq.p1 = &dest_vec;
1066 fq.rad = 0;
1067 fq.thisobjnum = -1;
1068 fq.ignore_obj_list = NULL;
1069 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS;
1070
1071 fvi_FindIntersection(&fq, &hit_data);
1072
1073 float mag = vm_VectorDistanceQuick(&hit_data.hit_pnt, &obj->pos);
1074
1075 int visnum;
1076
1077 if (obj == NULL || obj->type != OBJ_BUILDING || stricmp(Object_info[obj->id].name, "FinalbossLITTLETIT") != 0)
1078 visnum = VisEffectCreate(VIS_FIREBALL, MASSDRIVER_EFFECT_INDEX, obj->roomnum, &laser_pos);
1079 else
1080 visnum = VisEffectCreate(VIS_FIREBALL, MERCBOSS_MASSDRIVER_EFFECT_INDEX, obj->roomnum, &laser_pos);
1081
1082 if (visnum >= 0) {
1083 vis_effect *vis = &VisEffects[visnum];
1084 vis->custom_handle = mass_driver_ring;
1085 vis->lifeleft = 1.5;
1086 vis->lifetime = 1.5;
1087
1088 vis->end_pos = hit_data.hit_pnt;
1089 vis->billboard_info.width = 1;
1090 if (obj == NULL || obj->type != OBJ_BUILDING || stricmp(Object_info[obj->id].name, "FinalbossLITTLETIT") != 0)
1091 vis->lighting_color = GR_RGB16(100, 100, 170);
1092 else
1093 vis->lighting_color = GR_RGB16(170, 100, 100);
1094 vis->flags |= VF_LINK_TO_VIEWER | VF_EXPAND;
1095 vis->size = mag;
1096 }
1097 }
1098 }
1099
1100 void DemoReadObjCreate() {
1101 // float gametime;
1102 uint8_t type;
1103 uint8_t use_orient;
1104 matrix orient;
1105 vector pos;
1106 int roomnum;
1107 int16_t id;
1108 int parent_handle;
1109 object *obj;
1110
1111 type = cf_ReadByte(Demo_cfp);
1112 use_orient = cf_ReadByte(Demo_cfp);
1113 id = cf_ReadShort(Demo_cfp);
1114 roomnum = cf_ReadInt(Demo_cfp);
1115 gs_ReadVector(Demo_cfp, pos);
1116 parent_handle = cf_ReadInt(Demo_cfp);
1117 if (use_orient)
1118 gs_ReadMatrix(Demo_cfp, orient);
1119 // mprintf(0,"Creating object ype: %d id: %d room: %d\n",type,id,roomnum);
1120
1121 // xlate id to new id.
1122 switch (type) {
1123 case OBJ_ROBOT:
1124 case OBJ_POWERUP:
1125 case OBJ_BUILDING:
1126 case OBJ_CLUTTER:
1127 id = gs_Xlates->obji_indices[id];
1128 break;
1129 case OBJ_CAMERA: // Don't xlate camera IDs
1130 break;
1131 case OBJ_DOOR:
1132 case OBJ_WEAPON:
1133 case OBJ_PLAYER:
1134 Int3(); // Shouldn't be creating these things
1135 break;
1136 default:
1137 Int3(); // What is this?
1138 }
1139
1140 int16_t new_objnum = ObjCreate(type, id, roomnum, &pos, use_orient ? &orient : NULL, parent_handle);
1141 if (new_objnum > -1) { // DAJ -1FIX
1142 obj = &Objects[new_objnum];
1143
1144 int16_t oldobjnum = cf_ReadShort(Demo_cfp);
1145 Demo_obj_map[oldobjnum] = new_objnum;
1146 // MSAFE needs this list too
1147 Server_object_list[oldobjnum] = new_objnum;
1148 InitObjectScripts(obj);
1149 obj->flags |= OF_SERVER_OBJECT;
1150 }
1151 }
1152
1153 // FrameDemoDelta = FrameCount
1154 // DemoFrameCount = 0;
1155
1156 uint8_t DemoLastOpcode = 0;
1157
1158 void DemoFrame() {
1159 uint8_t opcode;
1160 if (Demo_flags != DF_PLAYBACK)
1161 return;
1162 if (!Demo_first_frame) {
1163 Demo_frame_ofs = 0;
1164
1165 if (Demo_make_movie) {
1166 DoScreenshot();
1167 }
1168 // This code slows down demo playback
1169 if (!Demo_play_fast) {
1170
1171 float tdelta = timer_GetTime();
1172 // if(Gametime<Demo_next_frame)
1173 //{
1174 // mprintf(0,"!%.3f!",Demo_next_frame-Gametime);
1175 //}
1176
1177 while ((Gametime + Demo_frame_ofs) < Demo_next_frame) {
1178 Demo_frame_ofs = timer_GetTime() - tdelta;
1179 }
1180 }
1181 } else {
1182 // mprintf(0,"This is the first frame in the demo playback!(%f)\n",Demo_next_frame);
1183 Gametime = Demo_next_frame;
1184 Demo_frame_time = 0;
1185 Demo_first_frame = false;
1186 }
1187 int exit_loop = 0;
1188 do {
1189 // Read one opcode at a time, and dispatch the proper function to read the rest of the data
1190 // Keep going until we hit a new frame
1191 try {
1192 opcode = cf_ReadByte(Demo_cfp);
1193 // mprintf(0,"Demo ocode: %d\n",opcode);
1194 } catch (...) {
1195 // End of file, so we're done playing the demo
1196 mprintf(0, "End of demo file!");
1197 strcpy(Old_demo_fname, Demo_fname);
1198 DemoAbort();
1199 // Do some cool stuff here, like end of demo stats or exit to the main menu
1200 if (Demo_looping) {
1201 Game_interface_mode = GAME_DEMO_LOOP;
1202 strcpy(Demo_fname, Old_demo_fname);
1203 } else {
1204 Game_interface_mode = GAME_POST_DEMO;
1205 }
1206 return;
1207 }
1208 switch (opcode) {
1209 case DT_NEW_FRAME:
1210 DemoReadNewFrame();
1211 exit_loop = 1;
1212 break;
1213 case DT_OBJ:
1214 DemoReadObj();
1215 break;
1216 case DT_WEAPON_FIRE:
1217 DemoReadWeaponFire();
1218 break;
1219 case DT_HUD_MESSAGE:
1220 DemoReadHudMessage();
1221 break;
1222 case DT_3D_SOUND:
1223 DemoRead3DSound();
1224 break;
1225 case DT_2D_SOUND:
1226 DemoRead2DSound();
1227 break;
1228 case DT_OBJ_CREATE:
1229 DemoReadObjCreate();
1230 break;
1231 case DT_OBJ_ANIM:
1232 DemoReadObjAnimChanged();
1233 break;
1234 case DT_OBJ_TURRET:
1235 DemoReadTurretChanged();
1236 break;
1237 case DT_OBJ_EXPLODE:
1238 DemoReadKillObject();
1239 break;
1240 case DT_PLAYER_DEATH:
1241 DemoReadPlayerDeath();
1242 break;
1243 case DT_COLLIDE_PLR:
1244 DemoReadCollidePlayerWeapon();
1245 break;
1246 case DT_COLLIDE_GEN:
1247 DemoReadCollideGenericWeapon();
1248 break;
1249 case DT_ATTACH:
1250 DemoReadAttachObj();
1251 break;
1252 case DT_ATTACH_RAD:
1253 DemoReadAttachObjRad();
1254 break;
1255 case DT_UNATTACH:
1256 DemoReadUnattachObj();
1257 break;
1258 case DT_WEAP_FIRE_FLAG:
1259 DemoReadObjWeapFireFlagChanged();
1260 break;
1261 case DT_PLAYER_INFO:
1262 DemoReadPlayerInfo();
1263 break;
1264 case DT_MSAFE:
1265 DemoReadMSafe();
1266 break;
1267 case DT_POWERUP:
1268 DemoReadPowerups();
1269 break;
1270 case DT_CINEMATICS:
1271 DemoReadCinematics();
1272 break;
1273 case DT_PERSISTANT_HUD:
1274 DemoReadPersistantHUDMessage();
1275 break;
1276 case DT_SETOBJDEAD:
1277 DemoReadSetObjDead();
1278 break;
1279 case DT_PLAYERBALLS:
1280 DemoReadPlayerBalls();
1281 break;
1282 case DT_PLAYERTYPECHNG:
1283 DemoReadPlayerTypeChange();
1284 break;
1285 case DT_SETOBJLIFELEFT:
1286 DemoReadObjLifeLeft();
1287 break;
1288 default:
1289 mprintf(0, "ERROR! Unknown opcode in demo file!(%d) last code: %d\n", opcode, DemoLastOpcode);
1290 // Int3();
1291 DemoAbort();
1292 if (Demo_looping) {
1293 Game_interface_mode = GAME_DEMO_LOOP;
1294 strcpy(Demo_fname, Old_demo_fname);
1295 } else {
1296 Game_interface_mode = GAME_POST_DEMO;
1297 }
1298 return;
1299 }
1300 DemoLastOpcode = opcode;
1301 } while (!exit_loop);
1302 }
1303
1304 void DemoWriteCinematics(uint8_t *data, uint16_t len) {
1305 cf_WriteByte(Demo_cfp, DT_CINEMATICS);
1306 // Write a bunch of data
1307 mprintf(0, "Writing Cinematic data (%d bytes) to demo file.\n", len);
1308 cf_WriteShort(Demo_cfp, len);
1309 cf_WriteBytes(data, len, Demo_cfp);
1310 }
1311
1312 void DemoReadCinematics() {
1313 uint8_t buffer[1500];
1314 uint16_t len = cf_ReadShort(Demo_cfp);
1315 cf_ReadBytes(buffer, len, Demo_cfp);
1316 mprintf(0, "Reading Cinematic data from demo file.\n");
1317 Cinematic_DoDemoFileData(buffer);
1318 }
1319
1320 void DemoWriteMSafe(uint8_t *data, uint16_t len) {
1321 cf_WriteByte(Demo_cfp, DT_MSAFE);
1322 cf_WriteShort(Demo_cfp, len);
1323 cf_WriteBytes(data, len, Demo_cfp);
1324 // mprintf(0,"Writing MSAFE data to demo file.\n");
1325 }
1326
1327 void DemoWritePowerup(uint8_t *data, uint16_t len) {
1328 cf_WriteByte(Demo_cfp, DT_POWERUP);
1329 cf_WriteShort(Demo_cfp, len);
1330 cf_WriteBytes(data, len, Demo_cfp);
1331 }
1332
1333 extern void MultiDoMSafeFunction(uint8_t *data);
1334
1335 void DemoReadMSafe() {
1336 uint8_t buffer[1500];
1337 uint16_t len = cf_ReadShort(Demo_cfp);
1338 cf_ReadBytes(buffer, len, Demo_cfp);
1339 // mprintf(0,"Reading MSAFE data from demo file.\n");
1340 MultiDoMSafeFunction(buffer);
1341 }
1342
1343 extern void MultiDoMSafePowerup(uint8_t *data);
1344
1345 void DemoReadPowerups() {
1346 uint8_t buffer[1500];
1347 uint16_t len = cf_ReadShort(Demo_cfp);
1348 cf_ReadBytes(buffer, len, Demo_cfp);
1349 MultiDoMSafePowerup(buffer);
1350 }
1351 extern void collide_player_and_weapon(object *playerobj, object *weapon, vector *collision_point,
1352 vector *collision_normal, bool f_reverse_normal, fvi_info *hit_info);
1353 extern void collide_generic_and_weapon(object *robotobj, object *weapon, vector *collision_point,
1354 vector *collision_normal, bool f_reverse_normal, fvi_info *hit_info);
1355
1356 void DemoWriteCollidePlayerWeapon(object *playerobj, object *weapon, vector *collision_point, vector *collision_normal,
1357 bool f_reverse_normal, void *hit_info) {
1358 cf_WriteByte(Demo_cfp, DT_COLLIDE_PLR);
1359 cf_WriteShort(Demo_cfp, OBJNUM(playerobj));
1360 cf_WriteShort(Demo_cfp, OBJNUM(weapon));
1361 gs_WriteVector(Demo_cfp, *collision_point);
1362 gs_WriteVector(Demo_cfp, *collision_normal);
1363 cf_WriteByte(Demo_cfp, f_reverse_normal ? 1 : 0);
1364 }
1365
1366 void DemoReadCollidePlayerWeapon(void) {
1367 vector collision_p;
1368 vector collision_n;
1369 bool f_reverse_normal;
1370 uint16_t real_weapnum;
1371 int16_t plr_objnum = cf_ReadShort(Demo_cfp);
1372 int16_t wep_objnum = cf_ReadShort(Demo_cfp);
1373 gs_ReadVector(Demo_cfp, collision_p);
1374 gs_ReadVector(Demo_cfp, collision_n);
1375 uint8_t b = cf_ReadByte(Demo_cfp);
1376 f_reverse_normal = b ? true : false;
1377
1378 real_weapnum = Demo_obj_map[wep_objnum];
1379 if (real_weapnum != 65535) {
1380 collide_player_and_weapon(&Objects[plr_objnum], &Objects[real_weapnum], &collision_p, &collision_n,
1381 f_reverse_normal, NULL);
1382 } else {
1383 mprintf(0, "Unable to map weapon number for collision in demo playback!\n");
1384 }
1385 }
1386
1387 void DemoWriteCollideGenericWeapon(object *robotobj, object *weapon, vector *collision_point, vector *collision_normal,
1388 bool f_reverse_normal, void *hit_info) {
1389 cf_WriteByte(Demo_cfp, DT_COLLIDE_GEN);
1390 cf_WriteShort(Demo_cfp, OBJNUM(robotobj));
1391 cf_WriteShort(Demo_cfp, OBJNUM(weapon));
1392 gs_WriteVector(Demo_cfp, *collision_point);
1393 gs_WriteVector(Demo_cfp, *collision_normal);
1394 cf_WriteByte(Demo_cfp, f_reverse_normal ? 1 : 0);
1395 }
1396
1397 void DemoReadCollideGenericWeapon(void) {
1398 vector collision_p;
1399 vector collision_n;
1400 bool f_reverse_normal;
1401 uint16_t real_weapnum;
1402 int16_t gen_objnum = cf_ReadShort(Demo_cfp);
1403 int16_t wep_objnum = cf_ReadShort(Demo_cfp);
1404 gs_ReadVector(Demo_cfp, collision_p);
1405 gs_ReadVector(Demo_cfp, collision_n);
1406 uint8_t b = cf_ReadByte(Demo_cfp);
1407 f_reverse_normal = b ? true : false;
1408
1409 real_weapnum = Demo_obj_map[wep_objnum];
1410 if (real_weapnum != 65535) {
1411 collide_generic_and_weapon(&Objects[gen_objnum], &Objects[real_weapnum], &collision_p, &collision_n,
1412 f_reverse_normal, NULL);
1413 } else {
1414 mprintf(0, "Unable to map weapon number for collision in demo playback!\n");
1415 }
1416 }
1417
1418 bool LoadDemoDialog() {
1419 // #ifdef DEMO
1420 // DoMessageBox(TXT_ERROR, TXT_WRONGVERSION, MSGBOX_OK);
1421 // return false;
1422 // #else
1423
1424 char file[_MAX_PATH * 2];
1425
1426 ddio_MakePath(file, Base_directory_write, "demo", NULL);
1427
1428 if (DoPathFileDialog(false, file, TXT_VIEWDEMO, "*.dem", PFDF_FILEMUSTEXIST)) {
1429 strcpy(Demo_fname, file);
1430 return true;
1431 }
1432 return false;
1433 // #endif
1434 }
1435
1436 // This function aborts the demo recording or playback, regardless of the state
1437 void DemoAbort(bool deletefile) {
1438 is_multi_demo = false;
1439 if (Demo_flags != DF_NONE) {
1440 // We're done with the xlate table, so free it
1441 mem_free(gs_Xlates);
1442 gs_Xlates = NULL;
1443
1444 cfclose(Demo_cfp);
1445 Demo_flags = DF_NONE;
1446 if (deletefile)
1447 ddio_DeleteFile(Demo_fname);
1448 Demo_fname[0] = '\0';
1449 return;
1450 }
1451 }
1452 int Debugme = 1;
1453
1454 void DemoReadNewFrame(void) {
1455 // float our_frametime;
1456 Gametime = Demo_next_frame;
1457 Frametime = Demo_frame_time;
1458 Demo_next_frame = cf_ReadFloat(Demo_cfp);
1459 Demo_frame_time = cf_ReadFloat(Demo_cfp);
1460
1461 DemoFrameCount++;
1462 }
1463
1464 void DemoWriteAttachObjRad(object *parent, char parent_ap, object *child, float rad) {
1465 cf_WriteByte(Demo_cfp, DT_ATTACH);
1466 cf_WriteShort(Demo_cfp, OBJNUM(parent));
1467 cf_WriteByte(Demo_cfp, parent_ap);
1468 cf_WriteShort(Demo_cfp, OBJNUM(child));
1469 cf_WriteFloat(Demo_cfp, rad);
1470 }
1471
1472 void DemoReadAttachObjRad(void) {
1473 int16_t old_objnum;
1474 int16_t parent_num;
1475 int16_t child_num;
1476 char parent_ap;
1477 float rad;
1478 old_objnum = cf_ReadShort(Demo_cfp);
1479 parent_num = Demo_obj_map[old_objnum];
1480 parent_ap = cf_ReadByte(Demo_cfp);
1481 old_objnum = cf_ReadShort(Demo_cfp);
1482 child_num = Demo_obj_map[old_objnum];
1483 rad = cf_ReadFloat(Demo_cfp);
1484 AttachObject(&Objects[parent_num], parent_ap, &Objects[child_num], rad);
1485 }
1486
1487 void DemoWriteAttachObj(object *parent, char parent_ap, object *child, char child_ap, bool f_aligned) {
1488 cf_WriteByte(Demo_cfp, DT_ATTACH);
1489 cf_WriteShort(Demo_cfp, OBJNUM(parent));
1490 cf_WriteByte(Demo_cfp, parent_ap);
1491 cf_WriteShort(Demo_cfp, OBJNUM(child));
1492 cf_WriteByte(Demo_cfp, child_ap);
1493 cf_WriteByte(Demo_cfp, f_aligned ? 1 : 0);
1494 }
1495
1496 void DemoReadAttachObj(void) {
1497 int16_t old_objnum;
1498 int16_t parent_num;
1499 int16_t child_num;
1500 char parent_ap;
1501 char child_ap;
1502 bool f_aligned;
1503 old_objnum = cf_ReadShort(Demo_cfp);
1504 parent_num = Demo_obj_map[old_objnum];
1505 parent_ap = cf_ReadByte(Demo_cfp);
1506 old_objnum = cf_ReadShort(Demo_cfp);
1507 child_num = Demo_obj_map[old_objnum];
1508 child_ap = cf_ReadByte(Demo_cfp);
1509 f_aligned = cf_ReadByte(Demo_cfp) ? true : false;
1510 AttachObject(&Objects[parent_num], parent_ap, &Objects[child_num], child_ap, f_aligned);
1511 }
1512
1513 void DemoWriteUnattachObj(object *child) {
1514 cf_WriteByte(Demo_cfp, DT_UNATTACH);
1515 cf_WriteShort(Demo_cfp, OBJNUM(child));
1516 }
1517
1518 void DemoReadUnattachObj(void) {
1519 int16_t old_objnum = cf_ReadShort(Demo_cfp);
1520 int16_t unattach_objnum = Demo_obj_map[old_objnum];
1521 UnattachFromParent(&Objects[unattach_objnum]);
1522 }
1523
1524 // Display options and statistics after displaying a demofile
1525 void DemoPostPlaybackMenu(void) {
1526 #define DEMO_MENU_WIDTH 320
1527 #define DEMO_MENU_HEIGHT 192
1528 #define BTN_WIDTH 96
1529 #define UID_LOOPING 200
1530 NewUIGameWindow window;
1531 NewUIButton ok_btn;
1532 NewUIButton cancel_btn;
1533 NewUIButton loop_btn;
1534 UIText wndtitle, fps_min, fps_avg, fps_max;
1535 bool exit_menu = false, ret = true;
1536 int cury = 9;
1537 char sztemp[200] = "";
1538
1539 window.Create(0, 0, DEMO_MENU_WIDTH, DEMO_MENU_HEIGHT, UIF_PROCESS_MENU | UIF_CENTER | NUWF_TITLELARGE);
1540 UITextItem itemTitle{TXT_DEMO_PLAY_OPTIONS};
1541 wndtitle.Create(&window, &itemTitle, 0, cury, UIF_CENTER | UIF_FIT);
1542 cury += 30;
1543 if (Demo_play_fast) {
1544 snprintf(sztemp, sizeof(sztemp), TXT_MINFPS, (float)1 / Max_frametime);
1545 UITextItem itemFpsMin{sztemp};
1546 fps_min.Create(&window, &itemFpsMin, 0, cury, UIF_CENTER | UIF_FIT);
1547 cury += 20;
1548 snprintf(sztemp, sizeof(sztemp), TXT_MAXFPS, (float)1 / Min_frametime);
1549 UITextItem itemFpsMax{sztemp};
1550 fps_max.Create(&window, &itemFpsMax, 0, cury, UIF_CENTER | UIF_FIT);
1551 cury += 20;
1552 snprintf(sztemp, sizeof(sztemp), TXT_AVGFSP, (float)1 / (Avg_frametime / Frames_counted));
1553 UITextItem itemFpsAvg{sztemp};
1554 fps_avg.Create(&window, &itemFpsAvg, 0, cury, UIF_CENTER | UIF_FIT);
1555 cury += 20;
1556 }
1557 // cury+=20;
1558 UITextItem itemLooping{TXT_PLAYLOOPING};
1559 loop_btn.Create(&window, UID_LOOPING, &itemLooping, 0, cury, BTN_WIDTH * 1.5, 32, UIF_CENTER | UIF_FIT);
1560 cury += 35;
1561
1562 UITextItem itemDone{TXT_DONE};
1563 ok_btn.Create(&window, UID_OK, &itemDone, 0, DEMO_MENU_HEIGHT - 70, BTN_WIDTH, 32, UIF_CENTER | UIF_FIT);
1564
1565 window.Open();
1566
1567 while (!exit_menu) {
1568 int res = DoUI();
1569
1570 switch (res) {
1571 case UID_LOOPING:
1572 Game_interface_mode = GAME_DEMO_LOOP;
1573 strcpy(Demo_fname, Old_demo_fname);
1574 Demo_looping = true;
1575 exit_menu = true;
1576 ret = true;
1577 break;
1578 case NEWUIRES_FORCEQUIT:
1579 case UID_OK:
1580 exit_menu = true;
1581 ret = true;
1582 if ((Players[Player_num].flags & PLAYER_FLAGS_DYING) || (Players[Player_num].flags & PLAYER_FLAGS_DEAD)) {
1583 EndPlayerDeath(Player_num);
1584 SetFunctionMode(MENU_MODE);
1585 } else {
1586 SetFunctionMode(MENU_MODE);
1587 }
1588 break;
1589 }
1590 }
1591 window.Close();
1592 window.Destroy();
1593 // Game_paused = false;
1594 }
1595
1596 void DemoWriteObjWeapFireFlagChanged(int16_t objnum) {
1597 cf_WriteByte(Demo_cfp, DT_WEAP_FIRE_FLAG);
1598 cf_WriteShort(Demo_cfp, objnum);
1599 cf_WriteByte(Demo_cfp, Objects[objnum].weapon_fire_flags);
1600 }
1601
1602 void DemoReadObjWeapFireFlagChanged(void) {
1603 int16_t oldobjnum = cf_ReadShort(Demo_cfp);
1604 int16_t newobjnum = Demo_obj_map[oldobjnum];
1605 Objects[newobjnum].weapon_fire_flags = cf_ReadByte(Demo_cfp);
1606 }
1607
1608 void DemoWritePlayerInfo(void) {
1609 int i;
1610 for (int a = 0; a < MAX_OBJECTS; a++) {
1611 if (Demo_turretchanged[a]) {
1612 object *obj = &Objects[a];
1613 multi_turret multi_turret_info;
1614 polyobj_info *p_info = &obj->rtype.pobj_info;
1615 int i;
1616
1617 if ((a >= 0) && (obj->type != OBJ_NONE) && (obj->type != OBJ_WEAPON) && (obj->flags & OF_POLYGON_OBJECT) &&
1618 (p_info->multi_turret_info.num_turrets)) {
1619 mprintf(0, "Turret %d updated!\n", a);
1620 multi_turret_info.keyframes = (float *)&turret_holder;
1621 ObjGetTurretUpdate(a, &multi_turret_info);
1622
1623 cf_WriteByte(Demo_cfp, DT_OBJ_TURRET);
1624 cf_WriteFloat(Demo_cfp, Gametime);
1625 cf_WriteShort(Demo_cfp, a); // objnum
1626 cf_WriteFloat(Demo_cfp, multi_turret_info.time);
1627 cf_WriteShort(Demo_cfp, multi_turret_info.num_turrets);
1628
1629 for (i = 0; i < multi_turret_info.num_turrets; i++) {
1630 cf_WriteFloat(Demo_cfp, multi_turret_info.keyframes[i]);
1631 }
1632 }
1633 }
1634 }
1635 for (i = 0; i < MAX_OBJECTS; i++) {
1636 Demo_turretchanged[i] = false;
1637 }
1638 cf_WriteByte(Demo_cfp, DT_PLAYER_INFO);
1639 // cf_WriteFloat(Demo_cfp,Players[Player_num].energy);
1640 cf_WriteShort(Demo_cfp, Players[Player_num].energy);
1641 // cf_WriteFloat(Demo_cfp,Objects[Players[Player_num].objnum].shields);
1642 cf_WriteShort(Demo_cfp, Objects[Players[Player_num].objnum].shields);
1643 for (i = 0; i < MAX_PLAYER_WEAPONS; i++) {
1644 cf_WriteShort(Demo_cfp, Players[Player_num].weapon_ammo[i]);
1645 }
1646 // cf_WriteFloat(Demo_cfp,Players[Player_num].weapon[0].firing_time);
1647 cf_WriteInt(Demo_cfp, Players[Player_num].weapon[0].index);
1648 // cf_WriteInt(Demo_cfp,Players[Player_num].weapon[0].sound_handle);
1649 // cf_WriteFloat(Demo_cfp,Players[Player_num].weapon[1].firing_time);
1650 cf_WriteInt(Demo_cfp, Players[Player_num].weapon[1].index);
1651 // cf_WriteInt(Demo_cfp,Players[Player_num].weapon[1].sound_handle);
1652 cf_WriteInt(Demo_cfp, Players[Player_num].weapon_flags);
1653 cf_WriteFloat(Demo_cfp, Players[Player_num].afterburn_time_left);
1654 cf_WriteFloat(Demo_cfp, Render_FOV);
1655 }
1656
1657 void DemoReadPlayerInfo(void) {
1658 int i;
1659 Players[Player_num].energy = cf_ReadShort(Demo_cfp); // cf_ReadFloat(Demo_cfp);
1660 Objects[Players[Player_num].objnum].shields = cf_ReadShort(Demo_cfp); // cf_ReadFloat(Demo_cfp);
1661 for (i = 0; i < MAX_PLAYER_WEAPONS; i++) {
1662 Players[Player_num].weapon_ammo[i] = cf_ReadShort(Demo_cfp);
1663 }
1664 // Players[Player_num].weapon[0].firing_time = cf_ReadFloat(Demo_cfp);
1665 Players[Player_num].weapon[0].index = cf_ReadInt(Demo_cfp);
1666 // Players[Player_num].weapon[0].sound_handle = cf_ReadInt(Demo_cfp);
1667 // Players[Player_num].weapon[1].firing_time = cf_ReadFloat(Demo_cfp);
1668 Players[Player_num].weapon[1].index = cf_ReadInt(Demo_cfp);
1669 // Players[Player_num].weapon[1].sound_handle = cf_ReadInt(Demo_cfp);
1670 Players[Player_num].weapon_flags = cf_ReadInt(Demo_cfp);
1671 Players[Player_num].afterburn_time_left = cf_ReadFloat(Demo_cfp);
1672
1673 if (Viewer_object == Player_object) {
1674 Render_FOV = cf_ReadFloat(Demo_cfp);
1675 } else {
1676 cf_ReadFloat(Demo_cfp);
1677 }
1678 }
1679
1680 void DemoPlayAutoDemo(void) {
1681 char wcard_fname[_MAX_PATH * 2];
1682 char auto_fname[_MAX_PATH * 2];
1683 int idx = 0;
1684 ddio_MakePath(wcard_fname, Base_directory_write, "demo", "*.dem", NULL);
1685 if (ddio_FindFileStart(wcard_fname, auto_fname)) {
1686 // Get to the next demo file we are scheduled to do.
1687 while (idx < Demo_auto_idx) {
1688 if (!ddio_FindNextFile(auto_fname)) {
1689 Demo_auto_idx = 0;
1690 ddio_FindFileClose();
1691 // A little recursion to go back to the start
1692 DemoPlayAutoDemo();
1693 return;
1694 }
1695 idx++;
1696 }
1697 // Now we should have the proper filename
1698 if (DemoPlaybackFile(auto_fname)) {
1699 Demo_auto_play = true;
1700 } else {
1701 Demo_auto_play = false;
1702 }
1703 } else {
1704 // No demo files are available, sorry!
1705 Demo_auto_play = false;
1706 }
1707 }
1708
1709 void DemoWritePersistantHUDMessage(ddgr_color color, int x, int y, float time, int flags, int sound_index, char *msg) {
1710 cf_WriteByte(Demo_cfp, DT_PERSISTANT_HUD);
1711 cf_WriteInt(Demo_cfp, (int)color);
1712 cf_WriteInt(Demo_cfp, (int)x);
1713 cf_WriteInt(Demo_cfp, (int)y);
1714 cf_WriteFloat(Demo_cfp, time);
1715 cf_WriteInt(Demo_cfp, flags);
1716 cf_WriteInt(Demo_cfp, sound_index);
1717 cf_WriteShort(Demo_cfp, strlen(msg) + 1);
1718 cf_WriteBytes((const uint8_t *)msg, strlen(msg) + 1, Demo_cfp);
1719 }
1720
1721 void DemoReadPersistantHUDMessage() {
1722 ddgr_color color;
1723 int x;
1724 int y;
1725 float time;
1726 int flags;
1727 int sound_index;
1728 char *fmt;
1729
1730 color = (uint32_t)cf_ReadInt(Demo_cfp);
1731 x = cf_ReadInt(Demo_cfp);
1732 y = cf_ReadInt(Demo_cfp);
1733 time = cf_ReadFloat(Demo_cfp);
1734 flags = cf_ReadInt(Demo_cfp);
1735 sound_index = cf_ReadInt(Demo_cfp);
1736 int msglen = cf_ReadShort(Demo_cfp);
1737 fmt = (char *)mem_malloc(msglen);
1738 cf_ReadBytes((uint8_t *)fmt, msglen, Demo_cfp);
1739 AddPersistentHUDMessage(color, x, y, time, flags, sound_index, fmt);
1740 mem_free(fmt);
1741 }
1742
1743 void DemoWriteSetObjDead(object *obj) {
1744 cf_WriteByte(Demo_cfp, DT_SETOBJDEAD);
1745 cf_WriteShort(Demo_cfp, OBJNUM(obj));
1746 }
1747
1748 void DemoReadSetObjDead() {
1749 int16_t oldobjnum = cf_ReadShort(Demo_cfp);
1750 int16_t local_objnum = Demo_obj_map[oldobjnum];
1751
1752 Objects[local_objnum].flags |= OF_SERVER_SAYS_DELETE;
1753 SetObjectDeadFlag(&Objects[local_objnum]);
1754
1755 // Clear our lists
1756 Demo_obj_map[oldobjnum] = 0xffff;
1757 Server_object_list[oldobjnum] = 0xffff;
1758 Local_object_list[local_objnum] = 0xffff;
1759 }
1760
1761 void DemoWritePlayerBalls(int pnum) {
1762 cf_WriteByte(Demo_cfp, DT_PLAYERBALLS);
1763
1764 cf_WriteShort(Demo_cfp, pnum);
1765 cf_WriteByte(Demo_cfp, Players[pnum].num_balls);
1766
1767 if (Players[pnum].num_balls > 0) {
1768 cf_WriteFloat(Demo_cfp, Players[pnum].ballspeed);
1769
1770 for (int i = 0; i < Players[pnum].num_balls; i++) {
1771 cf_WriteFloat(Demo_cfp, Players[pnum].ball_r[i]);
1772 cf_WriteFloat(Demo_cfp, Players[pnum].ball_g[i]);
1773 cf_WriteFloat(Demo_cfp, Players[pnum].ball_b[i]);
1774 }
1775 }
1776 }
1777
1778 void DemoReadPlayerBalls(void) {
1779 int slot = cf_ReadShort(Demo_cfp);
1780 int num_balls = cf_ReadByte(Demo_cfp);
1781 float speed;
1782 float r[4], g[4], b[4];
1783
1784 ASSERT(num_balls >= 0 && num_balls <= 4);
1785
1786 if (num_balls > 0) {
1787 speed = cf_ReadFloat(Demo_cfp);
1788
1789 for (int i = 0; i < num_balls; i++) {
1790 r[i] = cf_ReadFloat(Demo_cfp);
1791 g[i] = cf_ReadFloat(Demo_cfp);
1792 b[i] = cf_ReadFloat(Demo_cfp);
1793 }
1794 } else {
1795 speed = 0;
1796 memset(r, 0, sizeof(float) * 4);
1797 memset(g, 0, sizeof(float) * 4);
1798 memset(b, 0, sizeof(float) * 4);
1799 }
1800
1801 PlayerSetRotatingBall(slot, num_balls, speed, r, g, b);
1802 }
1803
1804 void DemoWritePlayerTypeChange(int slot, bool stop_observing, int observer_mode, int piggy_objnum) {
1805 cf_WriteByte(Demo_cfp, DT_PLAYERTYPECHNG);
1806 cf_WriteByte(Demo_cfp, slot);
1807 cf_WriteByte(Demo_cfp, Objects[Players[slot].objnum].type);
1808
1809 switch (Objects[Players[slot].objnum].type) {
1810 case OBJ_PLAYER:
1811 cf_WriteByte(Demo_cfp, (stop_observing) ? 1 : 0);
1812 break;
1813 case OBJ_GHOST:
1814 break;
1815 case OBJ_OBSERVER:
1816 cf_WriteInt(Demo_cfp, observer_mode);
1817
1818 if (observer_mode == OBSERVER_MODE_PIGGYBACK) {
1819 cf_WriteInt(Demo_cfp, piggy_objnum);
1820 }
1821 break;
1822 };
1823 }
1824
1825 void DemoReadPlayerTypeChange(void) {
1826 int slot;
1827 int type;
1828
1829 slot = cf_ReadByte(Demo_cfp);
1830 type = cf_ReadByte(Demo_cfp);
1831
1832 switch (type) {
1833 case OBJ_PLAYER:
1834 if (cf_ReadByte(Demo_cfp))
1835 PlayerStopObserving(slot);
1836 else
1837 MultiMakePlayerReal(slot);
1838 break;
1839 case OBJ_GHOST:
1840 MultiMakePlayerGhost(slot);
1841 break;
1842 case OBJ_OBSERVER: {
1843 int obs_mode = cf_ReadInt(Demo_cfp);
1844 int objnum = -1;
1845
1846 if (obs_mode == OBSERVER_MODE_PIGGYBACK) {
1847 objnum = Demo_obj_map[cf_ReadInt(Demo_cfp)];
1848 }
1849
1850 PlayerSwitchToObserver(slot, obs_mode, objnum);
1851
1852 } break;
1853 };
1854 }
1855
1856 void DemoWriteObjLifeLeft(object *obj) {
1857 if (Demo_flags == DF_RECORDING) {
1858 cf_WriteByte(Demo_cfp, DT_SETOBJLIFELEFT);
1859 cf_WriteShort(Demo_cfp, OBJNUM(obj));
1860
1861 if (obj->flags & OF_USES_LIFELEFT) {
1862 cf_WriteByte(Demo_cfp, 1);
1863 cf_WriteFloat(Demo_cfp, obj->lifeleft);
1864 } else {
1865 cf_WriteByte(Demo_cfp, 0);
1866 }
1867 }
1868 }
1869
1870 void DemoReadObjLifeLeft(void) {
1871 int16_t oldobjnum = cf_ReadShort(Demo_cfp);
1872 int16_t local_objnum = Demo_obj_map[oldobjnum];
1873
1874 if (cf_ReadByte(Demo_cfp)) {
1875 Objects[local_objnum].flags |= OF_USES_LIFELEFT;
1876 Objects[local_objnum].lifeleft = cf_ReadFloat(Demo_cfp);
1877 } else {
1878 Objects[local_objnum].flags &= ~OF_USES_LIFELEFT;
1879 Objects[local_objnum].lifeleft = 0;
1880 }
1881 }