1 /* $Id: state.c,v 1.27 2006-07-24 08:20:39 chris Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Functions to save/restore game state.
33 #if !defined(_MSC_VER) && !defined(macintosh)
47 #if defined(__APPLE__) && defined(__MACH__)
48 #include <OpenGL/gl.h>
107 #define STATE_VERSION 22
108 #define STATE_COMPATIBLE_VERSION 20
109 // 0 - Put DGSS (Descent Game State Save) id at tof.
110 // 1 - Added Difficulty level save
111 // 2 - Added Cheats_enabled flag
112 // 3 - Added between levels save.
113 // 4 - Added mission support
114 // 5 - Mike changed ai and object structure.
115 // 6 - Added buggin' cheat save
116 // 7 - Added other cheat saves and game_id.
117 // 8 - Added AI stuff for escort and thief.
118 // 9 - Save palette with screen shot
119 // 12- Saved last_was_super array
120 // 13- Saved palette flash stuff
121 // 14- Save cloaking wall stuff
122 // 15- Save additional ai info
123 // 16- Save Light_subtracted
124 // 17- New marker save
125 // 18- Took out saving of old cheat status
126 // 19- Saved cheats_enabled flag
127 // 20- First_secret_visit
131 #define THUMBNAIL_W 100
132 #define THUMBNAIL_H 50
133 #define DESC_LENGTH 20
135 extern void multi_initiate_save_game();
136 extern void multi_initiate_restore_game();
137 extern void apply_all_changed_light(void);
139 extern int Do_appearance_effect;
140 extern fix Fusion_next_sound_time;
142 extern int Laser_rapid_fire;
143 extern int Physics_cheat_flag;
145 extern void do_lunacy_on(void);
146 extern void do_lunacy_off(void);
147 extern int First_secret_visit;
150 grs_bitmap *sc_bmp[NUM_SAVES+1];
152 char dgss_id[4] = "DGSS";
154 int state_default_item = 0;
158 extern int robot_controlled[MAX_ROBOTS_CONTROLLED];
159 extern int robot_agitation[MAX_ROBOTS_CONTROLLED];
160 extern fix robot_controlled_time[MAX_ROBOTS_CONTROLLED];
161 extern fix robot_last_send_time[MAX_ROBOTS_CONTROLLED];
162 extern fix robot_last_message_time[MAX_ROBOTS_CONTROLLED];
163 extern int robot_send_pending[MAX_ROBOTS_CONTROLLED];
164 extern int robot_fired[MAX_ROBOTS_CONTROLLED];
165 extern sbyte robot_fire_buf[MAX_ROBOTS_CONTROLLED][18+3];
168 #if defined(WINDOWS) || defined(MACINTOSH)
169 extern ubyte Hack_DblClick_MenuMode;
172 void compute_all_static_light(void);
174 //-------------------------------------------------------------------
175 void state_callback(int nitems,newmenu_item * items, int * last_key, int citem)
180 // if ( sc_last_item != citem ) {
181 // sc_last_item = citem;
183 if ( sc_bmp[citem-1] ) {
185 grs_canvas *save_canv = grd_curcanv;
186 grs_canvas *temp_canv = gr_create_canvas(THUMBNAIL_W*2,(THUMBNAIL_H*24/10));
187 grs_point vertbuf[3] = {{0,0}, {0,0}, {i2f(THUMBNAIL_W*2),i2f(THUMBNAIL_H*24/10)} };
188 gr_set_current_canvas(temp_canv);
189 scale_bitmap(sc_bmp[citem-1], vertbuf, 0 );
190 gr_set_current_canvas( save_canv );
191 gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W*2)/2,items[0].y-10, &temp_canv->cv_bitmap);
192 gr_free_canvas(temp_canv);
195 gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W)/2,items[0].y-5, sc_bmp[citem-1] );
202 void rpad_string( char * string, int max_chars )
207 for( i=0; i<max_chars; i++ ) {
214 *string = 0; // NULL terminate
217 int state_get_save_file(char * fname, char * dsc, int multi, int blind_save)
220 int i, choice, version;
221 newmenu_item m[NUM_SAVES+2];
222 char filename[NUM_SAVES+1][30];
223 char desc[NUM_SAVES+1][DESC_LENGTH+16];
227 for (i=0;i<NUM_SAVES; i++ ) {
231 sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
233 sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
237 sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
239 sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
242 fp = PHYSFSX_openReadBuffered(filename[i]);
245 //FIXME: check for swapped file, react accordingly...
246 PHYSFS_read(fp, id, sizeof(char) * 4, 1);
247 if ( !memcmp( id, dgss_id, 4 )) {
249 PHYSFS_read(fp, &version, sizeof(int), 1);
250 if (version >= STATE_COMPATIBLE_VERSION) {
252 PHYSFS_read(fp, desc[i], sizeof(char) * DESC_LENGTH, 1);
253 //rpad_string( desc[i], DESC_LENGTH-1 );
255 //sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
256 //PHYSFS_read(fp, sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
263 strcpy( desc[i], TXT_EMPTY );
264 //rpad_string( desc[i], DESC_LENGTH-1 );
266 m[i].type = NM_TYPE_INPUT_MENU; m[i].text = desc[i]; m[i].text_len = DESC_LENGTH-1;
270 if (blind_save && state_default_item >= 0)
271 choice = state_default_item;
273 choice = newmenu_do1(NULL, "Save Game", NUM_SAVES, m, NULL, state_default_item);
275 for (i=0; i<NUM_SAVES; i++ ) {
277 gr_free_bitmap( sc_bmp[i] );
281 strcpy( fname, filename[choice] );
282 strcpy( dsc, desc[choice] );
283 state_default_item = choice;
290 extern int Current_display_mode;
292 int state_get_restore_file(char * fname, int multi)
295 int i, choice, version, nsaves;
296 newmenu_item m[NUM_SAVES+2];
297 char filename[NUM_SAVES+1][30];
298 char desc[NUM_SAVES+1][DESC_LENGTH + 16];
303 m[0].type = NM_TYPE_TEXT; m[0].text = "\n\n\n\n";
304 for (i=0;i<NUM_SAVES+1; i++ ) {
308 sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
310 sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
314 sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
316 sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
319 fp = PHYSFSX_openReadBuffered(filename[i]);
322 //FIXME: check for swapped file, react accordingly...
323 PHYSFS_read(fp, id, sizeof(char) * 4, 1);
324 if ( !memcmp( id, dgss_id, 4 )) {
326 PHYSFS_read(fp, &version, sizeof(int), 1);
327 if (version >= STATE_COMPATIBLE_VERSION) {
329 PHYSFS_read(fp, desc[i], sizeof(char) * DESC_LENGTH, 1);
330 //rpad_string( desc[i], DESC_LENGTH-1 );
331 m[i+1].type = NM_TYPE_MENU; m[i+1].text = desc[i];
333 sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
334 PHYSFS_read(fp, sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
337 PHYSFS_read(fp, pal, 3, 256);
338 gr_remap_bitmap_good( sc_bmp[i], pal, -1, -1 );
347 strcpy( desc[i], TXT_EMPTY );
348 //rpad_string( desc[i], DESC_LENGTH-1 );
349 m[i+1].type = NM_TYPE_TEXT; m[i+1].text = desc[i];
354 nm_messagebox( NULL, 1, "Ok", "No saved games were found!" );
358 if (Current_display_mode == 3) //restore menu won't fit on 640x400
359 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
363 #if defined(WINDOWS) || defined(MACINTOSH)
364 Hack_DblClick_MenuMode = 1;
368 choice = newmenu_do3( NULL, "Select Game to Restore", NUM_SAVES+2, m, state_callback, state_default_item+1, NULL, 190, -1 );
371 #if defined(WINDOWS) || defined(MACINTOSH)
372 Hack_DblClick_MenuMode = 0;
375 if (Current_display_mode == 3) //set flag back
376 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
379 for (i=0; i<NUM_SAVES+1; i++ ) {
381 gr_free_bitmap( sc_bmp[i] );
385 strcpy( fname, filename[choice-1] );
386 if (choice != NUM_SAVES+1) //no new default when restore from autosave
387 state_default_item = choice - 1;
393 #define DESC_OFFSET 8
395 // -----------------------------------------------------------------------------------
396 // Imagine if C had a function to copy a file...
397 int copy_file(char *old_file, char *new_file)
401 PHYSFS_file *in_file, *out_file;
403 out_file = PHYSFS_openWrite(new_file);
405 if (out_file == NULL)
408 in_file = PHYSFS_openRead(old_file);
413 buf_size = PHYSFS_fileLength(in_file);
414 while (buf_size && !(buf = d_malloc(buf_size)))
417 return -5; // likely to be an empty file
419 while (!PHYSFS_eof(in_file))
423 bytes_read = PHYSFS_read(in_file, buf, 1, buf_size);
425 Error("Cannot read from file <%s>: %s", old_file, PHYSFS_getLastError());
427 Assert(bytes_read == buf_size || PHYSFS_eof(in_file));
429 if (PHYSFS_write(out_file, buf, 1, bytes_read) < bytes_read)
430 Error("Cannot write to file <%s>: %s", new_file, PHYSFS_getLastError());
435 if (!PHYSFS_close(in_file))
437 PHYSFS_close(out_file);
441 if (!PHYSFS_close(out_file))
448 #define SECRETB_FILENAME "secret.sgb"
449 #define SECRETC_FILENAME "secret.sgc"
451 #define SECRETB_FILENAME ":Players:secret.sgb"
452 #define SECRETC_FILENAME ":Players:secret.sgc"
455 extern int Final_boss_is_dead;
457 // -----------------------------------------------------------------------------------
458 // blind_save means don't prompt user for any info.
459 int state_save_all(int between_levels, int secret_save, char *filename_override, int blind_save)
461 int rval, filenum = -1;
463 char filename[128], desc[DESC_LENGTH+1];
465 Assert(between_levels == 0); //between levels save ripped out
468 if ( Game_mode & GM_MULTI ) {
469 multi_initiate_save_game();
474 if ((Current_level_num < 0) && (secret_save == 0)) {
475 HUD_init_message( "Can't save in secret level!" );
479 if (Final_boss_is_dead) //don't allow save while final boss is dying
482 mprintf(( 0, "CL=%d, NL=%d\n", Current_level_num, Next_level_num ));
484 // If this is a secret save and the control center has been destroyed, don't allow
485 // return to the base level.
486 if (secret_save && (Control_center_destroyed)) {
487 mprintf((0, "Deleting secret.sgb so player can't return to base level.\n"));
488 PHYSFS_delete(SECRETB_FILENAME);
494 if (secret_save == 1) {
495 filename_override = filename;
496 sprintf(filename_override, SECRETB_FILENAME);
497 } else if (secret_save == 2) {
498 filename_override = filename;
499 sprintf(filename_override, SECRETC_FILENAME);
501 if (filename_override) {
502 strcpy( filename, filename_override);
503 sprintf(desc, "[autosave backup]");
505 else if (!(filenum = state_get_save_file(filename, desc, 0, blind_save)))
513 // If not in multiplayer, do special secret level stuff.
514 // If secret.sgc exists, then copy it to Nsecret.sgc (where N = filenum).
515 // If it doesn't exist, then delete Nsecret.sgc
516 if (!secret_save && !(Game_mode & GM_MULTI)) {
518 char temp_fname[32], fc;
523 fc = (filenum-10) + 'a';
528 sprintf(temp_fname, "%csecret.sgc", fc);
530 sprintf(temp_fname, ":Players:%csecret.sgc", fc);
533 mprintf((0, "Trying to copy secret.sgc to %s.\n", temp_fname));
535 if (PHYSFS_exists(temp_fname))
537 mprintf((0, "Deleting file %s\n", temp_fname));
538 if (!PHYSFS_delete(temp_fname))
539 Error("Cannot delete file <%s>: %s", temp_fname, PHYSFS_getLastError());
542 if (PHYSFS_exists(SECRETC_FILENAME))
544 mprintf((0, "Copying secret.sgc to %s.\n", temp_fname));
545 rval = copy_file(SECRETC_FILENAME, temp_fname);
546 Assert(rval == 0); // Oops, error copying secret.sgc to temp_fname!
551 // Save file we're going to save over in last slot and call "[autosave backup]"
552 if (!filename_override) {
555 tfp = PHYSFSX_openWriteBuffered(filename);
561 sprintf( newname, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
563 sprintf(newname, "Players/%s.sg%x", Players[Player_num].callsign, NUM_SAVES);
566 PHYSFS_seek(tfp, DESC_OFFSET);
567 PHYSFS_write(tfp, "[autosave backup]", sizeof(char) * DESC_LENGTH, 1);
569 PHYSFS_delete(newname);
570 PHYSFSX_rename(filename, newname);
574 rval = state_save_all_sub(filename, desc, between_levels);
575 if (rval && !secret_save)
576 HUD_init_message("Game saved.");
581 extern fix Flash_effect, Time_flash_last_played;
584 int state_save_all_sub(char *filename, char *desc, int between_levels)
591 Assert(between_levels == 0); //between levels save ripped out
593 /* if ( Game_mode & GM_MULTI ) {
600 #if defined(MACINTOSH) && !defined(NDEBUG)
601 if ( strncmp(filename, ":Players:", 9) )
605 fp = PHYSFSX_openWriteBuffered(filename);
607 if ( !(Game_mode & GM_MULTI) )
608 nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
614 PHYSFS_write(fp, dgss_id, sizeof(char) * 4, 1);
618 PHYSFS_write(fp, &i, sizeof(int), 1);
621 PHYSFS_write(fp, desc, sizeof(char) * DESC_LENGTH, 1);
623 // Save the current screen shot...
625 cnv = gr_create_canvas( THUMBNAIL_W, THUMBNAIL_H );
631 GLint gl_draw_buffer;
633 grs_canvas * cnv_save;
634 cnv_save = grd_curcanv;
636 gr_set_current_canvas( cnv );
642 buf = d_malloc(THUMBNAIL_W * THUMBNAIL_H * 3);
643 glGetIntegerv(GL_DRAW_BUFFER, &gl_draw_buffer);
644 glReadBuffer(gl_draw_buffer);
645 glReadPixels(0, SHEIGHT - THUMBNAIL_H, THUMBNAIL_W, THUMBNAIL_H, GL_RGB, GL_UNSIGNED_BYTE, buf);
647 for (i = 0; i < THUMBNAIL_W * THUMBNAIL_H; i++) {
648 if (!(j = i % THUMBNAIL_W))
650 cnv->cv_bitmap.bm_data[THUMBNAIL_W * k + j] =
651 gr_find_closest_color(buf[3*i]/4, buf[3*i+1]/4, buf[3*i+2]/4);
654 # else // simpler d1x method, not tested yet
655 ogl_ubitblt_tolinear(grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h, 0, 0, 0, 0, &grd_curscreen->sc_canvas.cv_bitmap, &grd_curcanv->cv_bitmap);
661 PHYSFS_write(fp, cnv->cv_bitmap.bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
663 gr_set_current_canvas(cnv_save);
664 gr_free_canvas( cnv );
665 PHYSFS_write(fp, pal, 3, 256);
670 for ( i=0; i<THUMBNAIL_W*THUMBNAIL_H; i++ )
671 PHYSFS_write(fp, &color, sizeof(ubyte), 1);
674 // Save the Between levels flag...
675 PHYSFS_write(fp, &between_levels, sizeof(int), 1);
677 // Save the mission info...
678 mprintf ((0, "HEY! Mission name is %s\n", Current_mission_filename));
679 PHYSFS_write(fp, Current_mission_filename, 9 * sizeof(char), 1);
682 PHYSFS_write(fp, &Current_level_num, sizeof(int), 1);
683 PHYSFS_write(fp, &Next_level_num, sizeof(int), 1);
686 PHYSFS_write(fp, &GameTime, sizeof(fix), 1);
688 // If coop save, save all
690 if (Game_mode & GM_MULTI_COOP)
692 PHYSFS_write(fp, &state_game_id,sizeof(int), 1);
693 PHYSFS_write(fp, &Netgame,sizeof(netgame_info), 1);
694 PHYSFS_write(fp, &NetPlayers,sizeof(AllNetPlayers_info), 1);
695 PHYSFS_write(fp, &N_players,sizeof(int), 1);
696 PHYSFS_write(fp, &Player_num,sizeof(int), 1);
697 for (i=0;i<N_players;i++)
698 PHYSFS_write(fp, &Players[i], sizeof(player), 1);
700 #ifdef RISKY_PROPOSITION
701 PHYSFS_write(fp, &robot_controlled[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
702 PHYSFS_write(fp, &robot_agitation[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
703 PHYSFS_write(fp, &robot_controlled_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
704 PHYSFS_write(fp, &robot_last_send_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
705 PHYSFS_write(fp, &robot_last_message_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
706 PHYSFS_write(fp, &robot_send_pending[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
707 PHYSFS_write(fp, &robot_fired[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
709 for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
710 PHYSFS_write(fp, robot_fire_buf[i][0], 18 + 3, 1);
717 PHYSFS_write(fp, &Players[Player_num], sizeof(player), 1);
719 // Save the current weapon info
720 PHYSFS_write(fp, &Primary_weapon, sizeof(sbyte), 1);
721 PHYSFS_write(fp, &Secondary_weapon, sizeof(sbyte), 1);
723 // Save the difficulty level
724 PHYSFS_write(fp, &Difficulty_level, sizeof(int), 1);
725 // Save cheats enabled
726 PHYSFS_write(fp, &Cheats_enabled,sizeof(int), 1);
728 if ( !between_levels ) {
730 //Finish all morph objects
731 for (i=0; i<=Highest_object_index; i++ ) {
732 if ( (Objects[i].type != OBJ_NONE) && (Objects[i].render_type==RT_MORPH)) {
734 md = find_morph_data(&Objects[i]);
736 md->obj->control_type = md->morph_save_control_type;
737 md->obj->movement_type = md->morph_save_movement_type;
738 md->obj->render_type = RT_POLYOBJ;
739 md->obj->mtype.phys_info = md->morph_save_phys_info;
741 } else { //maybe loaded half-morphed from disk
742 Objects[i].flags |= OF_SHOULD_BE_DEAD;
743 Objects[i].render_type = RT_POLYOBJ;
744 Objects[i].control_type = CT_NONE;
745 Objects[i].movement_type = MT_NONE;
751 i = Highest_object_index+1;
752 PHYSFS_write(fp, &i, sizeof(int), 1);
753 PHYSFS_write(fp, Objects, sizeof(object), i);
757 PHYSFS_write(fp, &i, sizeof(int), 1);
758 PHYSFS_write(fp, Walls, sizeof(wall), i);
760 //Save exploding wall info
761 i = MAX_EXPLODING_WALLS;
762 PHYSFS_write(fp, &i, sizeof(int), 1);
763 PHYSFS_write(fp, expl_wall_list, sizeof(*expl_wall_list), i);
767 PHYSFS_write(fp, &i, sizeof(int), 1);
768 PHYSFS_write(fp, ActiveDoors, sizeof(active_door), i);
770 //Save cloaking wall info
771 i = Num_cloaking_walls;
772 PHYSFS_write(fp, &i, sizeof(int), 1);
773 PHYSFS_write(fp, CloakingWalls, sizeof(cloaking_wall), i);
776 PHYSFS_write(fp, &Num_triggers, sizeof(int), 1);
777 PHYSFS_write(fp, Triggers, sizeof(trigger), Num_triggers);
780 for (i = 0; i <= Highest_segment_index; i++)
782 for (j = 0; j < 6; j++)
784 PHYSFS_write(fp, &Segments[i].sides[j].wall_num, sizeof(short), 1);
785 PHYSFS_write(fp, &Segments[i].sides[j].tmap_num, sizeof(short), 1);
786 PHYSFS_write(fp, &Segments[i].sides[j].tmap_num2, sizeof(short), 1);
790 // Save the fuelcen info
791 PHYSFS_write(fp, &Control_center_destroyed, sizeof(int), 1);
792 PHYSFS_write(fp, &Countdown_timer, sizeof(int), 1);
793 PHYSFS_write(fp, &Num_robot_centers, sizeof(int), 1);
794 PHYSFS_write(fp, RobotCenters, sizeof(matcen_info), Num_robot_centers);
795 PHYSFS_write(fp, &ControlCenterTriggers, sizeof(control_center_triggers), 1);
796 PHYSFS_write(fp, &Num_fuelcenters, sizeof(int), 1);
797 PHYSFS_write(fp, Station, sizeof(FuelCenter), Num_fuelcenters);
799 // Save the control cen info
800 PHYSFS_write(fp, &Control_center_been_hit, sizeof(int), 1);
801 PHYSFS_write(fp, &Control_center_player_been_seen, sizeof(int), 1);
802 PHYSFS_write(fp, &Control_center_next_fire_time, sizeof(int), 1);
803 PHYSFS_write(fp, &Control_center_present, sizeof(int), 1);
804 PHYSFS_write(fp, &Dead_controlcen_object_num, sizeof(int), 1);
809 // Save the automap visited info
810 PHYSFS_write(fp, Automap_visited, sizeof(ubyte), MAX_SEGMENTS);
813 PHYSFS_write(fp, &state_game_id, sizeof(uint), 1);
814 PHYSFS_write(fp, &Laser_rapid_fire, sizeof(int), 1);
815 PHYSFS_write(fp, &Lunacy, sizeof(int), 1); // Yes, writing this twice. Removed the Ugly robot system, but didn't want to change savegame format.
816 PHYSFS_write(fp, &Lunacy, sizeof(int), 1);
818 // Save automap marker info
820 PHYSFS_write(fp, MarkerObject, sizeof(MarkerObject) ,1);
821 PHYSFS_write(fp, MarkerOwner, sizeof(MarkerOwner), 1);
822 PHYSFS_write(fp, MarkerMessage, sizeof(MarkerMessage), 1);
824 PHYSFS_write(fp, &Afterburner_charge, sizeof(fix), 1);
826 //save last was super information
827 PHYSFS_write(fp, &Primary_last_was_super, sizeof(Primary_last_was_super), 1);
828 PHYSFS_write(fp, &Secondary_last_was_super, sizeof(Secondary_last_was_super), 1);
830 // Save flash effect stuff
831 PHYSFS_write(fp, &Flash_effect, sizeof(int), 1);
832 PHYSFS_write(fp, &Time_flash_last_played, sizeof(int), 1);
833 PHYSFS_write(fp, &PaletteRedAdd, sizeof(int), 1);
834 PHYSFS_write(fp, &PaletteGreenAdd, sizeof(int), 1);
835 PHYSFS_write(fp, &PaletteBlueAdd, sizeof(int), 1);
837 PHYSFS_write(fp, Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS);
839 PHYSFS_write(fp, &First_secret_visit, sizeof(First_secret_visit), 1);
841 if (PHYSFS_write(fp, &Omega_charge, sizeof(Omega_charge), 1) < 1)
843 if ( !(Game_mode & GM_MULTI) ) {
844 nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
846 PHYSFS_delete(filename);
851 #ifdef MACINTOSH // set the type and creator of the saved game file
857 strcpy(pfilename, filename);
859 err = HGetFInfo(0, 0, pfilename, &finfo);
860 finfo.fdType = 'SVGM';
861 finfo.fdCreator = 'DCT2';
862 err = HSetFInfo(0, 0, pfilename, &finfo);
872 // -----------------------------------------------------------------------------------
873 // Set the player's position from the globals Secret_return_segment and Secret_return_orient.
874 void set_pos_from_return_segment(void)
876 int plobjnum = Players[Player_num].objnum;
878 compute_segment_center(&Objects[plobjnum].pos, &Segments[Secret_return_segment]);
879 obj_relink(plobjnum, Secret_return_segment);
880 reset_player_object();
881 Objects[plobjnum].orient = Secret_return_orient;
884 // -----------------------------------------------------------------------------------
885 int state_restore_all(int in_game, int secret_restore, char *filename_override)
891 if ( Game_mode & GM_MULTI ) {
893 multi_initiate_restore_game();
899 if (in_game && (Current_level_num < 0) && (secret_restore == 0)) {
900 HUD_init_message( "Can't restore in secret level!" );
904 if ( Newdemo_state == ND_STATE_RECORDING )
905 newdemo_stop_recording();
907 if ( Newdemo_state != ND_STATE_NORMAL )
912 if (filename_override) {
913 strcpy(filename, filename_override);
914 filenum = NUM_SAVES+1; // So we don't trigger autosave
915 } else if (!(filenum = state_get_restore_file(filename, 0))) {
921 // If not in multiplayer, do special secret level stuff.
922 // If Nsecret.sgc (where N = filenum) exists, then copy it to secret.sgc.
923 // If it doesn't exist, then delete secret.sgc
924 if (!secret_restore && !(Game_mode & GM_MULTI)) {
926 char temp_fname[32], fc;
930 fc = (filenum-10) + 'a';
935 sprintf(temp_fname, "%csecret.sgc", fc);
937 sprintf(temp_fname, "Players/%csecret.sgc", fc);
940 mprintf((0, "Trying to copy %s to secret.sgc.\n", temp_fname));
942 if (PHYSFS_exists(temp_fname))
944 mprintf((0, "Copying %s to secret.sgc\n", temp_fname));
945 rval = copy_file(temp_fname, SECRETC_FILENAME);
946 Assert(rval == 0); // Oops, error copying temp_fname to secret.sgc!
948 PHYSFS_delete(SECRETC_FILENAME);
952 // Changed, 11/15/95, MK, don't to autosave if restoring from main menu.
953 if ((filenum != (NUM_SAVES+1)) && in_game) {
954 char temp_filename[128];
955 mprintf((0, "Doing autosave, filenum = %i, != %i!\n", filenum, NUM_SAVES+1));
957 sprintf( temp_filename, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
959 sprintf(temp_filename, "Players/%s.sg%x", Players[Player_num].callsign, NUM_SAVES);
961 state_save_all(!in_game, secret_restore, temp_filename, 0);
964 if ( !secret_restore && in_game ) {
966 choice = nm_messagebox( NULL, 2, "Yes", "No", "Restore Game?" );
975 return state_restore_all_sub(filename, 0, secret_restore);
978 extern void init_player_stats_new_ship(void);
980 void ShowLevelIntro(int level_num);
982 extern void do_cloak_invul_secret_stuff(fix old_gametime);
983 extern void copy_defaults_to_robot(object *objp);
985 int state_restore_all_sub(char *filename, int multi, int secret_restore)
987 int ObjectStartLocation;
988 int version,i, j, segnum;
991 int current_level, next_level;
994 char desc[DESC_LENGTH+1];
996 char org_callsign[CALLSIGN_LEN+16];
999 int nplayers; //,playid[12],mynum;
1000 player restore_players[MAX_PLAYERS];
1002 fix old_gametime = GameTime;
1004 #if defined(MACINTOSH) && !defined(NDEBUG)
1005 if (strncmp(filename, "Players/", 9))
1009 fp = PHYSFSX_openReadBuffered(filename);
1010 if ( !fp ) return 0;
1013 //FIXME: check for swapped file, react accordingly...
1014 PHYSFS_read(fp, id, sizeof(char) * 4, 1);
1015 if ( memcmp( id, dgss_id, 4 )) {
1021 PHYSFS_read(fp, &version, sizeof(int), 1);
1022 if (version < STATE_COMPATIBLE_VERSION) {
1028 PHYSFS_read(fp, desc, sizeof(char) * DESC_LENGTH, 1);
1030 // Skip the current screen shot...
1031 PHYSFS_seek(fp, PHYSFS_tell(fp) + THUMBNAIL_W * THUMBNAIL_H);
1033 // And now...skip the goddamn palette stuff that somebody forgot to add
1034 PHYSFS_seek(fp, PHYSFS_tell(fp) + 768);
1036 // Read the Between levels flag...
1037 PHYSFS_read(fp, &between_levels, sizeof(int), 1);
1039 Assert(between_levels == 0); //between levels save ripped out
1041 // Read the mission info...
1042 PHYSFS_read(fp, mission, sizeof(char) * 9, 1);
1043 mprintf ((0,"Missionname to load = %s\n",mission));
1045 if (!load_mission_by_name( mission )) {
1046 nm_messagebox( NULL, 1, "Ok", "Error!\nUnable to load mission\n'%s'\n", mission );
1052 PHYSFS_read(fp, ¤t_level, sizeof(int), 1);
1053 PHYSFS_read(fp, &next_level, sizeof(int), 1);
1056 PHYSFS_read(fp, &GameTime, sizeof(fix), 1);
1058 // Start new game....
1060 Game_mode = GM_NORMAL;
1061 Function_mode = FMODE_GAME;
1063 change_playernum_to(0);
1065 strcpy( org_callsign, Players[0].callsign );
1067 if (!secret_restore) {
1068 InitPlayerObject(); //make sure player's object set up
1069 init_player_stats_game(); //clear all stats
1072 strcpy( org_callsign, Players[Player_num].callsign );
1076 if (Game_mode & GM_MULTI)
1078 PHYSFS_read(fp, &state_game_id, sizeof(int), 1);
1079 PHYSFS_read(fp, &Netgame, sizeof(netgame_info), 1);
1080 PHYSFS_read(fp, &NetPlayers, sizeof(AllNetPlayers_info), 1);
1081 PHYSFS_read(fp, &nplayers, sizeof(N_players), 1);
1082 PHYSFS_read(fp, &Player_num, sizeof(Player_num), 1);
1083 for (i=0;i<nplayers;i++)
1084 PHYSFS_read(fp, &restore_players[i], sizeof(player), 1);
1085 #ifdef RISKY_PROPOSITION
1086 PHYSFS_read(fp, &robot_controlled[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1087 PHYSFS_read(fp, &robot_agitation[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1088 PHYSFS_read(fp, &robot_controlled_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1089 PHYSFS_read(fp, &robot_last_send_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1090 PHYSFS_read(fp, &robot_last_message_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1091 PHYSFS_read(fp, &robot_send_pending[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1092 PHYSFS_read(fp, &robot_fired[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1094 for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
1095 PHYSFS_read(fp, &robot_fire_buf[i][0],21,1);
1098 for (i=0;i<nplayers;i++)
1101 for (j=0;j<nplayers;j++)
1103 if ((!strcmp (restore_players[i].callsign,Players[j].callsign)) && Players[j].connected==1)
1106 restore_players[i].connected=found;
1108 memcpy (&Players,&restore_players,sizeof(player)*nplayers);
1111 if (network_i_am_master())
1113 for (i=0;i<N_players;i++)
1117 Players[i].connected=0;
1121 //Viewer = ConsoleObject = &Objects[Players[Player_num].objnum];
1129 StartNewLevelSub(current_level, 1, secret_restore);
1131 if (secret_restore) {
1132 player dummy_player;
1134 PHYSFS_read(fp, &dummy_player, sizeof(player), 1);
1135 if (secret_restore == 1) { // This means he didn't die, so he keeps what he got in the secret level.
1136 Players[Player_num].level = dummy_player.level;
1137 Players[Player_num].last_score = dummy_player.last_score;
1138 Players[Player_num].time_level = dummy_player.time_level;
1140 Players[Player_num].num_robots_level = dummy_player.num_robots_level;
1141 Players[Player_num].num_robots_total = dummy_player.num_robots_total;
1142 Players[Player_num].hostages_rescued_total = dummy_player.hostages_rescued_total;
1143 Players[Player_num].hostages_total = dummy_player.hostages_total;
1144 Players[Player_num].hostages_on_board = dummy_player.hostages_on_board;
1145 Players[Player_num].hostages_level = dummy_player.hostages_level;
1146 Players[Player_num].homing_object_dist = dummy_player.homing_object_dist;
1147 Players[Player_num].hours_level = dummy_player.hours_level;
1148 Players[Player_num].hours_total = dummy_player.hours_total;
1149 do_cloak_invul_secret_stuff(old_gametime);
1151 Players[Player_num] = dummy_player;
1154 PHYSFS_read(fp, &Players[Player_num], sizeof(player), 1);
1157 strcpy( Players[Player_num].callsign, org_callsign );
1159 // Set the right level
1160 if ( between_levels )
1161 Players[Player_num].level = next_level;
1163 // Restore the weapon states
1164 PHYSFS_read(fp, &Primary_weapon, sizeof(sbyte), 1);
1165 PHYSFS_read(fp, &Secondary_weapon, sizeof(sbyte), 1);
1167 select_weapon(Primary_weapon, 0, 0, 0);
1168 select_weapon(Secondary_weapon, 1, 0, 0);
1170 // Restore the difficulty level
1171 PHYSFS_read(fp, &Difficulty_level, sizeof(int), 1);
1173 // Restore the cheats enabled flag
1175 PHYSFS_read(fp, &Cheats_enabled, sizeof(int),1);
1177 if ( !between_levels ) {
1178 Do_appearance_effect = 0; // Don't do this for middle o' game stuff.
1180 ObjectStartLocation = PHYSFS_tell(fp);
1181 //Clear out all the objects from the lvl file
1182 for (segnum=0; segnum <= Highest_segment_index; segnum++)
1183 Segments[segnum].objects = -1;
1186 //Read objects, and pop 'em into their respective segments.
1187 PHYSFS_read(fp, &i, sizeof(int), 1);
1188 Highest_object_index = i-1;
1189 PHYSFS_read(fp, Objects, sizeof(object) * i, 1);
1191 Object_next_signature = 0;
1192 for (i=0; i<=Highest_object_index; i++ ) {
1194 obj->rtype.pobj_info.alt_textures = -1;
1195 segnum = obj->segnum;
1196 obj->next = obj->prev = obj->segnum = -1;
1197 if ( obj->type != OBJ_NONE ) {
1199 if ( obj->signature > Object_next_signature )
1200 Object_next_signature = obj->signature;
1203 //look for, and fix, boss with bogus shields
1204 if (obj->type == OBJ_ROBOT && Robot_info[obj->id].boss_flag) {
1205 fix save_shields = obj->shields;
1207 copy_defaults_to_robot(obj); //calculate starting shields
1209 //if in valid range, use loaded shield value
1210 if (save_shields > 0 && save_shields <= obj->shields)
1211 obj->shields = save_shields;
1213 obj->shields /= 2; //give player a break
1217 special_reset_objects();
1218 Object_next_signature++;
1220 // 1 = Didn't die on secret level.
1221 // 2 = Died on secret level.
1222 if (secret_restore && (Current_level_num >= 0)) {
1223 set_pos_from_return_segment();
1224 if (secret_restore == 2)
1225 init_player_stats_new_ship();
1229 PHYSFS_read(fp, &i, sizeof(int), 1);
1231 PHYSFS_read(fp, Walls, sizeof(wall), Num_walls);
1233 //now that we have the walls, check if any sounds are linked to
1234 //walls that are now open
1235 for (i=0;i<Num_walls;i++) {
1236 if (Walls[i].type == WALL_OPEN)
1237 digi_kill_sound_linked_to_segment(Walls[i].segnum,Walls[i].sidenum,-1); //-1 means kill any sound
1240 //Restore exploding wall info
1241 if (version >= 10) {
1242 PHYSFS_read(fp, &i, sizeof(int), 1);
1243 PHYSFS_read(fp, expl_wall_list, sizeof(*expl_wall_list), i);
1247 PHYSFS_read(fp, &i, sizeof(int), 1);
1249 PHYSFS_read(fp, ActiveDoors, sizeof(active_door), Num_open_doors);
1251 if (version >= 14) { //Restore cloaking wall info
1252 PHYSFS_read(fp, &i, sizeof(int), 1);
1253 Num_cloaking_walls = i;
1254 PHYSFS_read(fp, CloakingWalls, sizeof(cloaking_wall), Num_cloaking_walls);
1257 //Restore trigger info
1258 PHYSFS_read(fp, &Num_triggers, sizeof(int), 1);
1259 PHYSFS_read(fp, Triggers, sizeof(trigger), Num_triggers);
1262 for (i=0; i<=Highest_segment_index; i++ ) {
1263 for (j=0; j<6; j++ ) {
1264 PHYSFS_read(fp, &Segments[i].sides[j].wall_num, sizeof(short), 1);
1265 PHYSFS_read(fp, &Segments[i].sides[j].tmap_num, sizeof(short), 1);
1266 PHYSFS_read(fp, &Segments[i].sides[j].tmap_num2, sizeof(short), 1);
1270 //Restore the fuelcen info
1271 PHYSFS_read(fp, &Control_center_destroyed, sizeof(int), 1);
1272 PHYSFS_read(fp, &Countdown_timer, sizeof(int), 1);
1273 PHYSFS_read(fp, &Num_robot_centers, sizeof(int), 1);
1274 PHYSFS_read(fp, RobotCenters, sizeof(matcen_info), Num_robot_centers);
1275 PHYSFS_read(fp, &ControlCenterTriggers, sizeof(control_center_triggers), 1);
1276 PHYSFS_read(fp, &Num_fuelcenters, sizeof(int), 1);
1277 PHYSFS_read(fp, Station, sizeof(FuelCenter), Num_fuelcenters);
1279 // Restore the control cen info
1280 PHYSFS_read(fp, &Control_center_been_hit, sizeof(int), 1);
1281 PHYSFS_read(fp, &Control_center_player_been_seen, sizeof(int), 1);
1282 PHYSFS_read(fp, &Control_center_next_fire_time, sizeof(int), 1);
1283 PHYSFS_read(fp, &Control_center_present, sizeof(int), 1);
1284 PHYSFS_read(fp, &Dead_controlcen_object_num, sizeof(int), 1);
1286 // Restore the AI state
1287 ai_restore_state( fp, version );
1289 // Restore the automap visited info
1290 PHYSFS_read(fp, Automap_visited, sizeof(ubyte), MAX_SEGMENTS);
1292 // Restore hacked up weapon system stuff.
1293 Fusion_next_sound_time = GameTime;
1294 Auto_fire_fusion_cannon_time = 0;
1295 Next_laser_fire_time = GameTime;
1296 Next_missile_fire_time = GameTime;
1297 Last_laser_fired_time = GameTime;
1302 if ( version >= 7 ) {
1303 PHYSFS_read(fp, &state_game_id, sizeof(uint), 1);
1304 PHYSFS_read(fp, &Laser_rapid_fire, sizeof(int), 1);
1305 PHYSFS_read(fp, &Lunacy, sizeof(int), 1); // Yes, writing this twice. Removed the Ugly robot system, but didn't want to change savegame format.
1306 PHYSFS_read(fp, &Lunacy, sizeof(int), 1);
1311 if (version >= 17) {
1312 PHYSFS_read(fp, MarkerObject, sizeof(MarkerObject), 1);
1313 PHYSFS_read(fp, MarkerOwner, sizeof(MarkerOwner), 1);
1314 PHYSFS_read(fp, MarkerMessage, sizeof(MarkerMessage), 1);
1321 PHYSFS_read(fp, &num,sizeof(int), 1); // was NumOfMarkers
1322 PHYSFS_read(fp, &dummy,sizeof(int), 1); // was CurMarker
1324 PHYSFS_seek(fp, PHYSFS_tell(fp) + num * (sizeof(vms_vector) + 40));
1326 for (num=0;num<NUM_MARKERS;num++)
1327 MarkerObject[num] = -1;
1331 if (secret_restore != 1)
1332 PHYSFS_read(fp, &Afterburner_charge, sizeof(fix), 1);
1335 PHYSFS_read(fp, &dummy_fix, sizeof(fix), 1);
1339 //read last was super information
1340 PHYSFS_read(fp, &Primary_last_was_super, sizeof(Primary_last_was_super), 1);
1341 PHYSFS_read(fp, &Secondary_last_was_super, sizeof(Secondary_last_was_super), 1);
1344 if (version >= 12) {
1345 PHYSFS_read(fp, &Flash_effect, sizeof(int), 1);
1346 PHYSFS_read(fp, &Time_flash_last_played, sizeof(int), 1);
1347 PHYSFS_read(fp, &PaletteRedAdd, sizeof(int), 1);
1348 PHYSFS_read(fp, &PaletteGreenAdd, sizeof(int), 1);
1349 PHYSFS_read(fp, &PaletteBlueAdd, sizeof(int), 1);
1352 Time_flash_last_played = 0;
1354 PaletteGreenAdd = 0;
1358 // Load Light_subtracted
1359 if (version >= 16) {
1360 PHYSFS_read(fp, Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS);
1361 apply_all_changed_light();
1362 compute_all_static_light(); // set static_light field in segment struct. See note at that function.
1365 for (i=0; i<=Highest_segment_index; i++)
1366 Light_subtracted[i] = 0;
1369 if (!secret_restore) {
1370 if (version >= 20) {
1371 PHYSFS_read(fp, &First_secret_visit, sizeof(First_secret_visit), 1);
1372 mprintf((0, "File: [%s] Read First_secret_visit: New value = %i\n", filename, First_secret_visit));
1374 First_secret_visit = 1;
1376 First_secret_visit = 0;
1380 if (secret_restore != 1)
1381 PHYSFS_read(fp, &Omega_charge, sizeof(fix), 1);
1384 PHYSFS_read(fp, &dummy_fix, sizeof(fix), 1);
1391 if (Game_mode & GM_MULTI) // Get rid of ships that aren't
1392 { // connected in the restored game
1393 for (i=0;i<nplayers;i++)
1395 mprintf ((0,"Testing %s = %d\n",Players[i].callsign,Players[i].connected));
1396 if (Players[i].connected!=1)
1398 network_disconnect_player (i);
1399 create_player_appearance_effect(&Objects[Players[i].objnum]);
1400 mprintf ((0,"Killing player ship %s!\n",Players[i].callsign));
1407 // Load in bitmaps, etc..
1408 //!! piggy_load_level_data(); //already done by StartNewLevelSub()
1413 // When loading a saved game, segp->static_light is bogus.
1414 // This is because apply_all_changed_light, which is supposed to properly update this value,
1415 // cannot do so because it needs the original light cast from a light which is no longer there.
1416 // That is, a light has been blown out, so the texture remaining casts 0 light, but the static light
1417 // which is present in the static_light field contains the light cast from that light.
1418 void compute_all_static_light(void)
1422 for (i=0; i<=Highest_segment_index; i++) {
1426 segp = &Segments[i];
1429 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++) {
1432 sidep = &segp->sides[j];
1435 total_light += sidep->uvls[k].l;
1438 Segment2s[i].static_light = total_light/(MAX_SIDES_PER_SEGMENT*4);
1444 int state_get_game_id(char *filename)
1450 char desc[DESC_LENGTH+1];
1454 mprintf((0, "Restoring multigame from [%s]\n", filename));
1456 fp = PHYSFS_openRead(filename);
1460 //FIXME: check for swapped file, react accordingly...
1461 PHYSFS_read(fp, id, sizeof(char) * 4, 1);
1462 if ( memcmp( id, dgss_id, 4 )) {
1468 PHYSFS_read(fp, &version, sizeof(int), 1);
1469 if (version < STATE_COMPATIBLE_VERSION) {
1475 PHYSFS_read(fp, desc, sizeof(char) * DESC_LENGTH, 1);
1477 // Skip the current screen shot...
1478 PHYSFS_seek(fp, PHYSFS_tell(fp) + THUMBNAIL_W * THUMBNAIL_H);
1480 // And now...skip the palette stuff that somebody forgot to add
1481 PHYSFS_seek(fp, PHYSFS_tell(fp) + 768);
1483 // Read the Between levels flag...
1484 PHYSFS_read(fp, &between_levels, sizeof(int), 1);
1486 Assert(between_levels == 0); //between levels save ripped out
1488 // Read the mission info...
1489 PHYSFS_read(fp, mission, sizeof(char) * 9, 1);
1491 PHYSFS_read(fp, &dumbint, sizeof(int), 1);
1492 PHYSFS_read(fp, &dumbint, sizeof(int), 1);
1495 PHYSFS_read(fp, &dumbint, sizeof(fix), 1);
1497 PHYSFS_read(fp, &state_game_id, sizeof(int), 1);
1499 return (state_game_id);