2 * $Logfile: /Freespace2/code/Demo/Demo.cpp $
9 * Revision 1.1 2002/05/03 03:28:08 root
13 * 6 8/26/99 8:51p Dave
14 * Gave multiplayer TvT messaging a heavy dose of sanity. Cheat codes.
16 * 5 6/16/99 10:20a Dave
17 * Added send-message-list sexpression.
19 * 4 4/16/99 5:54p Dave
20 * Support for on/off style "stream" weapons. Real early support for
21 * target-painting lasers.
23 * 3 3/29/99 6:17p Dave
24 * More work on demo system. Got just about everything in except for
25 * blowing ships up, secondary weapons and player death/warpout.
27 * 2 3/28/99 5:58p Dave
28 * Added early demo code. Make objects move. Nice and framerate
29 * independant, but not much else. Don't use yet unless you're me :)
36 #include "missionload.h"
39 #include "freespace.h"
42 #include "gamesequence.h"
43 #include "systemvars.h"
45 #include "missionmessage.h"
46 #include "missionparse.h"
50 // -----------------------------------------------------------------------------------------------------------------------------
54 CFILE *Demo_file = NULL;
57 #define DEMO_DEFAULT_FPS 15
58 int Demo_fps = 1000 / DEMO_DEFAULT_FPS;
60 // timestamp for frame dumping
64 float Demo_missiontime = 0.0f;
66 // buffer for reading and writing demo stuff
67 #define DEMO_BUF_SIZE 32768
68 char *Demo_buf = NULL;
71 // # of events posted for this frame
72 int Demo_frame_events = 0;
74 // current offset into the demo file - only used for playback
75 int Demo_cur_offset = -1;
78 #define DEMO_VERSION 2
80 // an error reading or writing the demo file
81 int Demo_error = DEMO_ERROR_NONE;
83 // all strings read out of the demo file must be no longer than this
84 #define DEMO_STRING_LEN 255
87 #define DEMO_DATA_FRAME() do { \
88 if(Demo_file == NULL){\
90 DEMO_ERROR(DEMO_ERROR_GENERAL);\
93 if(Game_mode & GM_DEMO_RECORD){\
94 if(Demo_buf_pos == 0) {\
98 if(Demo_frame_events <= 0){\
101 if(!cfwrite_ushort((ushort)Demo_buf_pos, Demo_file)){\
102 DEMO_ERROR(DEMO_ERROR_DISK_SPACE);\
105 if(!cfwrite(Demo_buf, Demo_buf_pos, 1, Demo_file)){\
106 DEMO_ERROR(DEMO_ERROR_DISK_SPACE);\
109 } else if(Game_mode & GM_DEMO_PLAYBACK){\
110 Demo_buf_pos = (int)cfread_ushort(Demo_file);\
111 if(!cfread(Demo_buf, Demo_buf_pos, 1, Demo_file)){\
112 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);\
117 #define DEMO_DATA(vl, vl_size) do { if(Demo_buf == NULL){ DEMO_ERROR(DEMO_ERROR_GENERAL); break; } if((Demo_buf_pos + vl_size) >= DEMO_BUF_SIZE){ Int3(); DEMO_ERROR(DEMO_ERROR_FRAMESIZE); break; } if(Game_mode & GM_DEMO_RECORD){ memcpy(Demo_buf + Demo_buf_pos, &vl, vl_size); } else if(Game_mode & GM_DEMO_PLAYBACK){ memcpy(&vl, Demo_buf + Demo_buf_pos, vl_size); } Demo_buf_pos += vl_size; } while(0)
118 #define DEMO_INT(vl) do { DEMO_DATA(vl, sizeof(int)); } while(0)
119 #define DEMO_UINT(vl) do { DEMO_DATA(vl, sizeof(uint)); } while(0)
120 #define DEMO_SHORT(vl) do { DEMO_DATA(vl, sizeof(short)); } while(0)
121 #define DEMO_USHORT(vl) do { DEMO_DATA(vl, sizeof(ushort)); } while(0)
122 #define DEMO_BYTE(vl) do { DEMO_DATA(vl, sizeof(char)); } while(0)
123 #define DEMO_UBYTE(vl) do { DEMO_DATA(vl, sizeof(ubyte)); } while(0)
124 #define DEMO_FLOAT(vl) do { DEMO_DATA(vl, sizeof(float)); } while(0)
125 #define DEMO_VECTOR(vl) do { DEMO_DATA(vl, sizeof(vector)); } while(0)
126 #define DEMO_MATRIX(vl) do { DEMO_DATA(vl, sizeof(matrix)); } while(0)
127 #define DEMO_STRING(vl) do { int stlen; if(Game_mode & GM_DEMO_RECORD){ stlen = strlen(vl); if(stlen <= 0){ break; } DEMO_DATA(stlen, sizeof(ushort)); DEMO_DATA(*vl, strlen(vl)); } else { ushort len = 0; DEMO_USHORT(len); DEMO_DATA(*vl, len); vl[len] = '\0'; } } while(0)
130 #define DE_DUMP 1 // standard object dump
131 #define DE_TRAILER 2 // end of demo trailer
132 #define DE_PRIMARY 3 // primary weapon fired
133 #define DE_UNIQUE_MESSAGE 4 // unique hud message
134 #define DE_BUILTIN_MESSAGE 5 // builtin hud message
135 #define DE_OBJ_CREATE 6 // object create message
136 #define DE_OBJ_WARPIN 7 // ship warpin
137 #define DE_OBJ_WARPOUT 8 // ship warpout
138 #define DE_OBJ_DEPARTED 9 // ship departed
139 #define DE_SHIP_KILL 10 // ship kill
141 // call this when posting an error
142 #define DEMO_ERROR(er) do { Demo_error = er; Int3(); } while(0)
147 Demo_make = !Demo_make;
149 dc_printf("Demo will be recorded\n");
151 dc_printf("Demo will NOT be recorded\n");
156 // -----------------------------------------------------------------------------------------------------------------------------
157 // DEMO FORWARD DECLARATIONS
161 int demo_write_header();
163 // read the demo header
164 int demo_read_header();
166 // write the demo trailer
167 void demo_write_trailer();
169 // do a recording frame
170 void demo_do_recording_frame_start();
172 // do a recording frame
173 void demo_do_recording_frame_end();
175 // do a playback frame
176 void demo_do_playback_frame();
178 // seek through the demo file to the proper location
179 // return 0 on error, 1 on success/continue, 2 if the demo is done
180 int demo_playback_seek();
182 // scan through a read in frame of data and apply everything necessary. returns true if the trailer (end of demo) was found
183 int demo_playback_seek_sub(int frame_size);
186 // -----------------------------------------------------------------------------------------------------------------------------
190 // do frame for the demo - playback and recording, returns 0 if errors were encountered during frame processing
191 int demo_do_frame_start()
196 // if we're not doing any demo stuff
197 if(!(Game_mode & GM_DEMO)){
202 if(Demo_file == NULL){
203 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
207 // make sure we're not trying to record and playback at the same time
208 Assert( ((Game_mode & GM_DEMO_RECORD) && !(Game_mode & GM_DEMO_PLAYBACK)) || (!(Game_mode & GM_DEMO_RECORD) && (Game_mode & GM_DEMO_PLAYBACK)) );
211 if(Game_mode & GM_DEMO_RECORD){
212 demo_do_recording_frame_start();
214 demo_do_playback_frame();
217 // bad bad bad, get mwa
227 // do frame for the demo - playback and recording, returns 0 if errors were encountered during frame processing
228 int demo_do_frame_end()
233 // if we're not doing any demo stuff
234 if(!(Game_mode & GM_DEMO)){
239 if(Demo_file == NULL){
240 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
244 // make sure we're not trying to record and playback at the same time
245 Assert( ((Game_mode & GM_DEMO_RECORD) && !(Game_mode & GM_DEMO_PLAYBACK)) || (!(Game_mode & GM_DEMO_RECORD) && (Game_mode & GM_DEMO_PLAYBACK)) );
247 // recording. there's nothing to do here for playback
248 if(Game_mode & GM_DEMO_RECORD){
249 demo_do_recording_frame_end();
252 // bad bad bad, get mwa
262 // initialize a demo for recording
263 // NOTE : call this after loading the mission and going through the briefing, but _before_ physically moving into the mission
264 int demo_start_record(char *file)
269 char full_name[MAX_FILENAME_LEN] = "";
271 // try and allocate the buffer
272 Demo_buf = (char*)malloc(DEMO_BUF_SIZE);
273 if(Demo_buf == NULL){
274 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
279 strcpy(full_name, file);
280 cf_add_ext(full_name, ".fsd");
281 Demo_file = cfopen(full_name, "wb", CFILE_NORMAL, CF_TYPE_DEMOS);
282 if(Demo_file == NULL){
284 Demo_error = DEMO_ERROR_DISK_ACCESS;
289 Demo_error = DEMO_ERROR_NONE;
292 if(!demo_write_header()){
297 Game_mode |= GM_DEMO_RECORD;
300 Demo_frame_events = 0;
307 // initialize a demo for playback - calling this will load up the demo file and move the player into the playback state
308 int demo_start_playback(char *file)
313 char full_name[MAX_FILENAME_LEN] = "";
315 // try and allocate the buffer
316 Demo_buf = (char*)malloc(DEMO_BUF_SIZE);
317 if(Demo_buf == NULL){
318 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
323 strcpy(full_name, file);
324 cf_add_ext(full_name, ".fsd");
325 Demo_file = cfopen(full_name, "rb", CFILE_NORMAL, CF_TYPE_DEMOS);
326 if(Demo_file == NULL){
327 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
332 Demo_error = DEMO_ERROR_NONE;
335 Demo_cur_offset = -1;
336 if(!demo_read_header()){
341 Game_mode |= GM_DEMO_PLAYBACK;
343 // everything is cool, so jump into the mission
344 gameseq_post_event(GS_EVENT_ENTER_GAME);
353 // if we're recording, write the trailer
354 if(Game_mode & GM_DEMO_RECORD){
355 demo_write_trailer();
358 // close the demo file
359 if(Demo_file != NULL){
365 if(Demo_buf != NULL){
370 // if we're playing back, go back to the main hall
371 if(Game_mode & GM_DEMO_PLAYBACK){
372 gameseq_post_event(GS_EVENT_MAIN_MENU);
376 Game_mode &= ~(GM_DEMO_RECORD | GM_DEMO_PLAYBACK);
380 // if we should run the simulation for this object, or let the demo system handle it
381 int demo_should_sim(object *objp)
386 // always sim stuff in non-demo mode
387 if(!(Game_mode & GM_DEMO)){
391 // don't sim ships or missiles
392 if((objp->type == OBJ_SHIP) || ((objp->type == OBJ_WEAPON) && (objp->instance >= 0) && (Weapon_info[Weapons[objp->instance].weapon_info_index].subtype == WP_MISSILE))){
396 // sim everything else
402 // -----------------------------------------------------------------------------------------------------------------------------
403 // DEMO RECORDING FUNCTIONS
407 int demo_write_header()
412 // write demo version #
413 if(!cfwrite_int(DEMO_VERSION, Demo_file)){
414 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
418 // write mission filename
419 if(!cfwrite_string_len(Game_current_mission_filename, Demo_file)){
420 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
424 // write mission checksum
425 full_name = cf_add_ext(Game_current_mission_filename, FS_MISSION_FILE_EXT);
426 cf_chksum_long(full_name, &chksum);
427 if(!cfwrite_int(chksum, Demo_file)){
428 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
436 // write the demo trailer
437 void demo_write_trailer()
440 demo_do_recording_frame_start();
443 ubyte frame_type = DE_TRAILER;
444 DEMO_UBYTE(frame_type);
449 // write frame data to disk
453 // start recording frame
454 void demo_do_recording_frame_start()
456 // if we're not physically doing the mission
457 if(gameseq_get_state() != GS_STATE_GAME_PLAY){
462 Demo_missiontime += flFrametime;
464 // clear the buffer, set no events, and write the header
466 Demo_frame_events = 0;
469 float fl_time = f2fl(Missiontime);
473 // end recording frame
474 void demo_do_recording_frame_end()
476 // if we're not physically doing the mission
477 if(gameseq_get_state() != GS_STATE_GAME_PLAY){
481 // if its time to dump objects (the last thing we might dump per frame)
482 if((Demo_stamp == -1) || timestamp_elapsed(Demo_stamp)){
483 // post an object dump event
484 demo_POST_object_dump();
487 Demo_stamp = timestamp(Demo_fps);
490 // write all accumulated frame data to disk if necessary
494 // post an object dump event
495 void demo_POST_object_dump()
503 ubyte event_type = DE_DUMP;
504 DEMO_UBYTE(event_type);
506 // go through the ship list and count
508 for ( sobjp = GET_FIRST(&Ship_obj_list); sobjp !=END_OF_LIST(&Ship_obj_list); sobjp = GET_NEXT(sobjp) ){
510 if(sobjp->objnum < 0){
517 // write out the object count
518 DEMO_USHORT(obj_count);
520 // go through the ship list and dump necessary stuff
521 for ( sobjp = GET_FIRST(&Ship_obj_list); sobjp !=END_OF_LIST(&Ship_obj_list); sobjp = GET_NEXT(sobjp) ){
523 if(sobjp->objnum < 0){
526 objp = &Objects[sobjp->objnum];
528 // just ships for now
529 DEMO_INT(objp->signature);
530 DEMO_VECTOR(objp->pos);
531 DEMO_MATRIX(objp->orient);
532 DEMO_FLOAT(objp->phys_info.forward_thrust);
533 team = (ubyte)Ships[objp->instance].team;
535 DEMO_INT(Ships[objp->instance].flags);
538 // up the event count
542 // post a primary fired event
543 void demo_POST_primary_fired(object *objp, int banks, int linked)
548 ubyte event_type = DE_PRIMARY;
549 DEMO_UBYTE(event_type);
552 DEMO_INT(objp->signature);
555 fire_info = (ubyte)banks;
556 fire_info &= ~(1<<7);
560 DEMO_UBYTE(fire_info);
562 // up the event count
566 // post a unique message
567 void demo_POST_unique_message(char *id, char *who_from, int m_source, int priority)
570 if((id == NULL) || (who_from == NULL) || (strlen(id) <= 0) || (strlen(who_from) <= 0)){
575 ubyte event = DE_UNIQUE_MESSAGE;
578 DEMO_STRING(who_from);
582 // up the event count
586 // post a builtin message
587 void demo_POST_builtin_message(int type, ship *shipp, int priority, int timing)
592 ubyte event = DE_BUILTIN_MESSAGE;
597 } else if(shipp->objnum >= 0){
598 sig = Objects[shipp->objnum].signature;
604 // up the event count
608 // post an object create message
609 void demo_POST_obj_create(char *pobj_name, int signature)
612 ubyte event = DE_OBJ_CREATE;
614 DEMO_STRING(pobj_name);
617 // up the event count
621 // post a warpin event
622 void demo_POST_warpin(int signature, int ship_flags)
625 ubyte event = DE_OBJ_WARPIN;
628 DEMO_INT(ship_flags);
630 // up the event count
634 // post a warpout event
635 void demo_POST_warpout(int signature, int ship_flags)
638 ubyte event = DE_OBJ_WARPOUT;
641 DEMO_INT(ship_flags);
643 // up the event count
647 // post a departed event
648 void demo_POST_departed(int signature, int ship_flags)
651 ubyte event = DE_OBJ_DEPARTED;
654 DEMO_INT(ship_flags);
656 // up the event count
660 // post a ship kill event
661 void demo_POST_ship_kill(object *objp)
664 ubyte event = DE_SHIP_KILL;
666 DEMO_INT(objp->signature);
668 // up the event count
673 // -----------------------------------------------------------------------------------------------------------------------------
674 // DEMO PLAYBACK FUNCTIONS
677 // read the demo header
678 int demo_read_header()
681 uint file_checksum, my_checksum;
684 // read the version #
685 version = cfread_int(Demo_file);
686 if(version != DEMO_VERSION){
687 DEMO_ERROR(DEMO_ERROR_VERSION);
691 // read mission filename
692 cfread_string_len(Game_current_mission_filename, MAX_FILENAME_LEN, Demo_file);
694 // get mission checksum
695 file_checksum = cfread_int(Demo_file);
696 full_name = cf_add_ext(Game_current_mission_filename, FS_MISSION_FILE_EXT);
697 if(!cf_chksum_long(full_name, &my_checksum)){
698 DEMO_ERROR(DEMO_ERROR_DISK_ACCESS);
701 if(file_checksum != my_checksum){
702 DEMO_ERROR(DEMO_ERROR_MISSION);
706 // get the file offset of this first frame
707 Demo_cur_offset = cftell(Demo_file);
713 // do a playback frame
714 void demo_do_playback_frame()
716 // seek to the best location in the demo file
717 switch(demo_playback_seek()){
726 // found the trailer - this demo is done
733 // seek through the demo file to the proper location
734 // return 0 on error, 1 on success/continue, 2 if the demo is done
735 int demo_playback_seek()
737 float this_time = 0.0f;
742 // scan until we find something useful
744 // record the beginning of this chunk
745 this_offset = cftell(Demo_file);
747 // read in the data for the next frame
749 frame_size = Demo_buf_pos;
753 DEMO_FLOAT(this_time);
756 if(this_time > f2fl(Missiontime)){
760 // seek back to the beginning of this chunk
761 Demo_cur_offset = this_offset;
766 // we should scan through this chunk
768 // returns true if it finds the demo trailer
769 if(demo_playback_seek_sub(frame_size)){
775 // seek back to the new frame
776 cfseek(Demo_file, Demo_cur_offset, CF_SEEK_SET);
782 // scan through a read in frame of data and apply everything necessary. returns true if the trailer (end of demo) was found
783 int demo_playback_seek_sub(int frame_size)
789 while(Demo_buf_pos < frame_size){
795 // oops, trailer. we're done
801 ushort obj_count = 0;
804 vector obj_pos = vmd_zero_vector;
805 matrix obj_orient = vmd_identity_matrix;
806 float obj_fthrust = 0;
810 // get the object count
811 DEMO_USHORT(obj_count);
813 // read in all the objects
814 for(idx=0; idx<obj_count; idx++){
816 DEMO_VECTOR(obj_pos);
817 DEMO_MATRIX(obj_orient);
818 DEMO_FLOAT(obj_fthrust);
820 DEMO_INT(ship_flags);
822 // find our ship object
823 ship_index = ship_get_by_signature(obj_sig);
824 // Assert(ship_index >= 0);
826 Objects[Ships[ship_index].objnum].pos = obj_pos;
827 Objects[Ships[ship_index].objnum].orient = obj_orient;
828 Objects[Ships[ship_index].objnum].phys_info.forward_thrust = obj_fthrust;
829 Ships[ship_index].team = team;
830 Ships[ship_index].flags = ship_flags;
842 // get the data and ship
844 DEMO_UBYTE(fire_info);
845 ship_index = ship_get_by_signature(obj_sig);
847 Ships[ship_index].weapons.current_primary_bank = (int)(fire_info & ~(1<<7));
848 if(fire_info & (1<<7)){
849 Ships[ship_index].flags |= SF_PRIMARY_LINKED;
851 Ships[ship_index].flags &= ~(SF_PRIMARY_LINKED);
855 ship_fire_primary(&Objects[Ships[ship_index].objnum], 0, 1);
861 case DE_UNIQUE_MESSAGE: {
863 char who_from[255] = "";
869 DEMO_STRING(who_from);
874 message_send_unique_to_player(id, who_from, m_source, priority, 0, 0);
879 case DE_BUILTIN_MESSAGE:{
892 // message_send_builtin_to_player(type, NULL, priority, timing, 0, 0);
894 ship_index = ship_get_by_signature(sig);
896 // message_send_builtin_to_player(type, &Ships[ship_index], priority, timing, 0, 0);
904 char pobj_name[255] = "";
907 p_object *objp = NULL;
909 // try and create the ship
910 DEMO_STRING(pobj_name);
913 objp = mission_parse_get_arrival_ship( pobj_name );
915 objnum = parse_create_object(objp);
917 Objects[objnum].signature = obj_sig;
929 // get the data and ship
931 DEMO_INT(ship_flags);
932 ship_index = ship_get_by_signature(obj_sig);
934 Ships[ship_index].flags = ship_flags;
935 shipfx_warpin_start(&Objects[Ships[ship_index].objnum]);
941 case DE_OBJ_WARPOUT:{
946 // get the data and ship
948 DEMO_INT(ship_flags);
949 ship_index = ship_get_by_signature(obj_sig);
951 Ships[ship_index].flags = ship_flags;
952 shipfx_warpout_start(&Objects[Ships[ship_index].objnum]);
958 case DE_OBJ_DEPARTED:{
963 // get the data and ship
965 DEMO_INT(ship_flags);
966 ship_index = ship_get_by_signature(obj_sig);
968 Ships[ship_index].flags = ship_flags;
969 ship_departed(ship_index);
979 // get the data and ship
981 ship_index = ship_get_by_signature(obj_sig);
983 Objects[Ships[ship_index].objnum].hull_strength = 0.0f;
984 ship_generic_kill_stuff(&Objects[Ships[ship_index].objnum], 1.0f);