]> icculus.org git repositories - btb/d2x.git/blob - main/state.c
get rid of dependencies on predefined display modes
[btb/d2x.git] / main / state.c
1 /* $Id: state.c,v 1.27 2006-07-24 08:20:39 chris Exp $ */
2 /*
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.
13 */
14
15 /*
16  *
17  * Functions to save/restore game state.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef WINDOWS
26 #include "desw.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33 #if !defined(_MSC_VER) && !defined(macintosh)
34 #include <unistd.h>
35 #endif
36 #ifndef _WIN32_WCE
37 #include <errno.h>
38 #endif
39 #ifdef MACINTOSH
40 #include <Files.h>
41 #endif
42
43 #ifdef OGL
44 # ifdef _MSC_VER
45 #  include <windows.h>
46 # endif
47 #if defined(__APPLE__) && defined(__MACH__)
48 #include <OpenGL/gl.h>
49 #else
50 #include <GL/gl.h>
51 #endif
52 #endif
53
54 #include "pstypes.h"
55 #include "mono.h"
56 #include "inferno.h"
57 #include "segment.h"
58 #include "textures.h"
59 #include "wall.h"
60 #include "object.h"
61 #include "digi.h"
62 #include "gamemine.h"
63 #include "error.h"
64 #include "gameseg.h"
65 #include "menu.h"
66 #include "switch.h"
67 #include "game.h"
68 #include "screens.h"
69 #include "newmenu.h"
70 #include "cfile.h"
71 #include "fuelcen.h"
72 #include "hash.h"
73 #include "key.h"
74 #include "piggy.h"
75 #include "player.h"
76 #include "cntrlcen.h"
77 #include "morph.h"
78 #include "weapon.h"
79 #include "render.h"
80 #include "gameseq.h"
81 #include "gauges.h"
82 #include "newdemo.h"
83 #include "automap.h"
84 #include "piggy.h"
85 #include "paging.h"
86 #include "titles.h"
87 #include "text.h"
88 #include "mission.h"
89 #include "pcx.h"
90 #include "u_mem.h"
91 #ifdef NETWORK
92 #include "network.h"
93 #endif
94 #include "args.h"
95 #include "ai.h"
96 #include "fireball.h"
97 #include "controls.h"
98 #include "laser.h"
99 #include "multibot.h"
100 #include "state.h"
101
102 #ifdef OGL
103 #include "gr.h"
104 #endif
105 #include "physfsx.h"
106
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
128 // 22- Omega_charge
129
130 #define NUM_SAVES 9
131 #define THUMBNAIL_W 100
132 #define THUMBNAIL_H 50
133 #define DESC_LENGTH 20
134
135 extern void multi_initiate_save_game();
136 extern void multi_initiate_restore_game();
137 extern void apply_all_changed_light(void);
138
139 extern int Do_appearance_effect;
140 extern fix Fusion_next_sound_time;
141
142 extern int Laser_rapid_fire;
143 extern int Physics_cheat_flag;
144 extern int Lunacy;
145 extern void do_lunacy_on(void);
146 extern void do_lunacy_off(void);
147 extern int First_secret_visit;
148
149 int sc_last_item= 0;
150 grs_bitmap *sc_bmp[NUM_SAVES+1];
151
152 char dgss_id[4] = "DGSS";
153
154 int state_default_item = 0;
155
156 uint state_game_id;
157
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];
166
167
168 #if defined(WINDOWS) || defined(MACINTOSH)
169 extern ubyte Hack_DblClick_MenuMode;
170 #endif
171
172 //-------------------------------------------------------------------
173 void state_callback(int nitems,newmenu_item * items, int * last_key, int citem)
174 {
175 //      if ( sc_last_item != citem )    {
176 //              sc_last_item = citem;
177                 if ( citem > 0 )        {
178                         if ( sc_bmp[citem-1] )  {
179                                 if (MenuHires) {
180                                         grs_canvas *save_canv = grd_curcanv;
181                                         grs_canvas *temp_canv = gr_create_canvas(THUMBNAIL_W*2,(THUMBNAIL_H*24/10));
182                                         grs_point vertbuf[3] = {{0,0}, {0,0}, {i2f(THUMBNAIL_W*2),i2f(THUMBNAIL_H*24/10)} };
183                                         gr_set_current_canvas(temp_canv);
184                                         scale_bitmap(sc_bmp[citem-1], vertbuf, 0 );
185                                         gr_set_current_canvas( save_canv );
186                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W*2)/2,items[0].y-10, &temp_canv->cv_bitmap);
187                                         gr_free_canvas(temp_canv);
188                                 }
189                                 else    {
190                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W)/2,items[0].y-5, sc_bmp[citem-1] );
191                                 }
192                         }
193                 }
194 //      }       
195 }
196
197 void rpad_string( char * string, int max_chars )
198 {
199         int i, end_found;
200
201         end_found = 0;
202         for( i=0; i<max_chars; i++ )    {
203                 if ( *string == 0 )
204                         end_found = 1;
205                 if ( end_found )
206                         *string = ' ';
207                 string++;
208         }
209         *string = 0;            // NULL terminate
210 }
211
212 int state_get_save_file(char * fname, char * dsc, int multi, int blind_save)
213 {
214         PHYSFS_file *fp;
215         int i, choice, version;
216         newmenu_item m[NUM_SAVES+2];
217         char filename[NUM_SAVES+1][30];
218         char desc[NUM_SAVES+1][DESC_LENGTH+16];
219         char id[5];
220         int valid=0;
221         
222         for (i=0;i<NUM_SAVES; i++ )     {
223                 sc_bmp[i] = NULL;
224                 if ( !multi )
225                         #ifndef MACINTOSH
226                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
227                         #else
228                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
229                         #endif
230                 else
231                         #ifndef MACINTOSH
232                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
233                         #else
234                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
235                         #endif
236                 valid = 0;
237                 fp = PHYSFSX_openReadBuffered(filename[i]);
238                 if ( fp ) {
239                         //Read id
240                         //FIXME: check for swapped file, react accordingly...
241                         PHYSFS_read(fp, id, sizeof(char) * 4, 1);
242                         if ( !memcmp( id, dgss_id, 4 )) {
243                                 //Read version
244                                 PHYSFS_read(fp, &version, sizeof(int), 1);
245                                 if (version >= STATE_COMPATIBLE_VERSION)        {
246                                         // Read description
247                                         PHYSFS_read(fp, desc[i], sizeof(char) * DESC_LENGTH, 1);
248                                         //rpad_string( desc[i], DESC_LENGTH-1 );
249                                         // Read thumbnail
250                                         //sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
251                                         //PHYSFS_read(fp, sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
252                                         valid = 1;
253                                 }
254                         } 
255                         PHYSFS_close(fp);
256                 }
257                 if (!valid) {
258                         strcpy( desc[i], TXT_EMPTY );
259                         //rpad_string( desc[i], DESC_LENGTH-1 );
260                 }
261                 m[i].type = NM_TYPE_INPUT_MENU; m[i].text = desc[i]; m[i].text_len = DESC_LENGTH-1;
262         }
263
264         sc_last_item = -1;
265         if (blind_save && state_default_item >= 0)
266                 choice = state_default_item;
267         else
268                 choice = newmenu_do1(NULL, "Save Game", NUM_SAVES, m, NULL, state_default_item);
269
270         for (i=0; i<NUM_SAVES; i++ )    {
271                 if ( sc_bmp[i] )
272                         gr_free_bitmap( sc_bmp[i] );
273         }
274
275         if (choice > -1) {
276                 strcpy( fname, filename[choice] );
277                 strcpy( dsc, desc[choice] );
278                 state_default_item = choice;
279                 return choice+1;
280         }
281         return 0;
282 }
283
284 int RestoringMenu=0;
285
286 int state_get_restore_file(char * fname, int multi)
287 {
288         PHYSFS_file *fp;
289         int i, choice, version, nsaves;
290         newmenu_item m[NUM_SAVES+2];
291         char filename[NUM_SAVES+1][30];
292         char desc[NUM_SAVES+1][DESC_LENGTH + 16];
293         char id[5];
294         int valid;
295
296         nsaves=0;
297         m[0].type = NM_TYPE_TEXT; m[0].text = "\n\n\n\n";       
298         for (i=0;i<NUM_SAVES+1; i++ )   {
299                 sc_bmp[i] = NULL;
300                 if (!multi)
301                         #ifndef MACINTOSH
302                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
303                         #else
304                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
305                         #endif
306                 else
307                         #ifndef MACINTOSH
308                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
309                         #else
310                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
311                         #endif
312                 valid = 0;
313                 fp = PHYSFSX_openReadBuffered(filename[i]);
314                 if ( fp ) {
315                         //Read id
316                         //FIXME: check for swapped file, react accordingly...
317                         PHYSFS_read(fp, id, sizeof(char) * 4, 1);
318                         if ( !memcmp( id, dgss_id, 4 )) {
319                                 //Read version
320                                 PHYSFS_read(fp, &version, sizeof(int), 1);
321                                 if (version >= STATE_COMPATIBLE_VERSION)        {
322                                         // Read description
323                                         PHYSFS_read(fp, desc[i], sizeof(char) * DESC_LENGTH, 1);
324                                         //rpad_string( desc[i], DESC_LENGTH-1 );
325                                         m[i+1].type = NM_TYPE_MENU; m[i+1].text = desc[i];
326                                         // Read thumbnail
327                                         sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
328                                         PHYSFS_read(fp, sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
329                                         if (version >= 9) {
330                                                 ubyte pal[256*3];
331                                                 PHYSFS_read(fp, pal, 3, 256);
332                                                 gr_remap_bitmap_good( sc_bmp[i], pal, -1, -1 );
333                                         }
334                                         nsaves++;
335                                         valid = 1;
336                                 }
337                         }
338                         PHYSFS_close(fp);
339                 }
340                 if (!valid) {
341                         strcpy( desc[i], TXT_EMPTY );
342                         //rpad_string( desc[i], DESC_LENGTH-1 );
343                         m[i+1].type = NM_TYPE_TEXT; m[i+1].text = desc[i];
344                 }
345         }
346
347         if ( nsaves < 1 )       {
348                 nm_messagebox( NULL, 1, "Ok", "No saved games were found!" );
349                 return 0;
350         }
351
352         if (Current_display_mode == SM(640,400)) //restore menu won't fit on 640x400
353                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
354
355         sc_last_item = -1;
356
357 #if defined(WINDOWS) || defined(MACINTOSH)
358         Hack_DblClick_MenuMode = 1;
359 #endif
360
361    RestoringMenu=1;
362         choice = newmenu_do3( NULL, "Select Game to Restore", NUM_SAVES+2, m, state_callback, state_default_item+1, NULL, 190, -1 );
363    RestoringMenu=0;
364
365 #if defined(WINDOWS) || defined(MACINTOSH)
366         Hack_DblClick_MenuMode = 0;
367 #endif
368
369         if (Current_display_mode == SM(640,400)) //set flag back
370                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
371
372
373         for (i=0; i<NUM_SAVES+1; i++ )  {
374                 if ( sc_bmp[i] )
375                         gr_free_bitmap( sc_bmp[i] );
376         }
377
378         if (choice > 0) {
379                 strcpy( fname, filename[choice-1] );
380                 if (choice != NUM_SAVES+1)              //no new default when restore from autosave
381                         state_default_item = choice - 1;
382                 return choice;
383         }
384         return 0;
385 }
386
387 #define DESC_OFFSET     8
388
389 //      -----------------------------------------------------------------------------------
390 //      Imagine if C had a function to copy a file...
391 int copy_file(char *old_file, char *new_file)
392 {
393         sbyte   *buf;
394         int             buf_size;
395         PHYSFS_file *in_file, *out_file;
396
397         out_file = PHYSFS_openWrite(new_file);
398
399         if (out_file == NULL)
400                 return -1;
401
402         in_file = PHYSFS_openRead(old_file);
403
404         if (in_file == NULL)
405                 return -2;
406
407         buf_size = PHYSFS_fileLength(in_file);
408         while (buf_size && !(buf = d_malloc(buf_size)))
409                 buf_size /= 2;
410         if (buf_size == 0)
411                 return -5;      // likely to be an empty file
412
413         while (!PHYSFS_eof(in_file))
414         {
415                 int bytes_read;
416
417                 bytes_read = PHYSFS_read(in_file, buf, 1, buf_size);
418                 if (bytes_read < 0)
419                         Error("Cannot read from file <%s>: %s", old_file, PHYSFS_getLastError());
420
421                 Assert(bytes_read == buf_size || PHYSFS_eof(in_file));
422
423                 if (PHYSFS_write(out_file, buf, 1, bytes_read) < bytes_read)
424                         Error("Cannot write to file <%s>: %s", new_file, PHYSFS_getLastError());
425         }
426
427         d_free(buf);
428
429         if (!PHYSFS_close(in_file))
430         {
431                 PHYSFS_close(out_file);
432                 return -3;
433         }
434
435         if (!PHYSFS_close(out_file))
436                 return -4;
437
438         return 0;
439 }
440
441 #ifndef MACINTOSH
442 #define SECRETB_FILENAME        "secret.sgb"
443 #define SECRETC_FILENAME        "secret.sgc"
444 #else
445 #define SECRETB_FILENAME        ":Players:secret.sgb"
446 #define SECRETC_FILENAME        ":Players:secret.sgc"
447 #endif
448
449 extern int Final_boss_is_dead;
450
451 //      -----------------------------------------------------------------------------------
452 //      blind_save means don't prompt user for any info.
453 int state_save_all(int between_levels, int secret_save, char *filename_override, int blind_save)
454 {
455         int     rval, filenum = -1;
456
457         char    filename[128], desc[DESC_LENGTH+1];
458
459         Assert(between_levels == 0);    //between levels save ripped out
460
461 #ifdef NETWORK
462         if ( Game_mode & GM_MULTI )     {
463                         multi_initiate_save_game();
464                 return 0;
465         }
466 #endif
467
468         if ((Current_level_num < 0) && (secret_save == 0)) {
469                 HUD_init_message( "Can't save in secret level!" );
470                 return 0;
471         }
472
473         if (Final_boss_is_dead)         //don't allow save while final boss is dying
474                 return 0;
475
476         mprintf(( 0, "CL=%d, NL=%d\n", Current_level_num, Next_level_num ));
477         
478         //      If this is a secret save and the control center has been destroyed, don't allow
479         //      return to the base level.
480         if (secret_save && (Control_center_destroyed)) {
481                 mprintf((0, "Deleting secret.sgb so player can't return to base level.\n"));
482                 PHYSFS_delete(SECRETB_FILENAME);
483                 return 0;
484         }
485
486         stop_time();
487
488         if (secret_save == 1) {
489                 filename_override = filename;
490                 sprintf(filename_override, SECRETB_FILENAME);
491         } else if (secret_save == 2) {
492                 filename_override = filename;
493                 sprintf(filename_override, SECRETC_FILENAME);
494         } else {
495                 if (filename_override) {
496                         strcpy( filename, filename_override);
497                         sprintf(desc, "[autosave backup]");
498                 }
499                 else if (!(filenum = state_get_save_file(filename, desc, 0, blind_save)))
500                 {
501                         start_time();
502                         return 0;
503                 }
504         }
505                 
506         //      MK, 1/1/96
507         //      If not in multiplayer, do special secret level stuff.
508         //      If secret.sgc exists, then copy it to Nsecret.sgc (where N = filenum).
509         //      If it doesn't exist, then delete Nsecret.sgc
510         if (!secret_save && !(Game_mode & GM_MULTI)) {
511                 int     rval;
512                 char    temp_fname[32], fc;
513
514                 if (filenum != -1) {
515
516                         if (filenum >= 10)
517                                 fc = (filenum-10) + 'a';
518                         else
519                                 fc = '0' + filenum;
520
521                         #ifndef MACINTOSH
522                         sprintf(temp_fname, "%csecret.sgc", fc);
523                         #else
524                         sprintf(temp_fname, ":Players:%csecret.sgc", fc);
525                         #endif
526
527                         mprintf((0, "Trying to copy secret.sgc to %s.\n", temp_fname));
528
529                         if (PHYSFS_exists(temp_fname))
530                         {
531                                 mprintf((0, "Deleting file %s\n", temp_fname));
532                                 if (!PHYSFS_delete(temp_fname))
533                                         Error("Cannot delete file <%s>: %s", temp_fname, PHYSFS_getLastError());
534                         }
535
536                         if (PHYSFS_exists(SECRETC_FILENAME))
537                         {
538                                 mprintf((0, "Copying secret.sgc to %s.\n", temp_fname));
539                                 rval = copy_file(SECRETC_FILENAME, temp_fname);
540                                 Assert(rval == 0);      //      Oops, error copying secret.sgc to temp_fname!
541                         }
542                 }
543         }
544
545         //      Save file we're going to save over in last slot and call "[autosave backup]"
546         if (!filename_override) {
547                 PHYSFS_file *tfp;
548
549                 tfp = PHYSFSX_openWriteBuffered(filename);
550
551                 if ( tfp ) {
552                         char    newname[128];
553
554                         #ifndef MACINTOSH
555                         sprintf( newname, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
556                         #else
557                         sprintf(newname, "Players/%s.sg%x", Players[Player_num].callsign, NUM_SAVES);
558                         #endif
559
560                         PHYSFS_seek(tfp, DESC_OFFSET);
561                         PHYSFS_write(tfp, "[autosave backup]", sizeof(char) * DESC_LENGTH, 1);
562                         PHYSFS_close(tfp);
563                         PHYSFS_delete(newname);
564                         PHYSFSX_rename(filename, newname);
565                 }
566         }
567         
568         rval = state_save_all_sub(filename, desc, between_levels);
569         if (rval && !secret_save)
570                 HUD_init_message("Game saved.");
571
572         return rval;
573 }
574
575 extern  fix     Flash_effect, Time_flash_last_played;
576
577
578 int state_save_all_sub(char *filename, char *desc, int between_levels)
579 {
580         int i,j;
581         PHYSFS_file *fp;
582         grs_canvas * cnv;
583         ubyte *pal;
584
585         Assert(between_levels == 0);    //between levels save ripped out
586
587 /*      if ( Game_mode & GM_MULTI )     {
588                 {
589                 start_time();
590                 return 0;
591                 }
592         }*/
593
594         #if defined(MACINTOSH) && !defined(NDEBUG)
595         if ( strncmp(filename, ":Players:", 9) )
596                 Int3();
597         #endif
598
599         fp = PHYSFSX_openWriteBuffered(filename);
600         if ( !fp ) {
601                 if ( !(Game_mode & GM_MULTI) )
602                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
603                 start_time();
604                 return 0;
605         }
606
607 //Save id
608         PHYSFS_write(fp, dgss_id, sizeof(char) * 4, 1);
609
610 //Save version
611         i = STATE_VERSION;
612         PHYSFS_write(fp, &i, sizeof(int), 1);
613
614 //Save description
615         PHYSFS_write(fp, desc, sizeof(char) * DESC_LENGTH, 1);
616         
617 // Save the current screen shot...
618
619         cnv = gr_create_canvas( THUMBNAIL_W, THUMBNAIL_H );
620         if ( cnv )
621         {
622 #ifdef OGL
623                 ubyte *buf;
624                 int k;
625                 GLint gl_draw_buffer;
626 #endif
627                 grs_canvas * cnv_save;
628                 cnv_save = grd_curcanv;
629
630                 gr_set_current_canvas( cnv );
631
632                 render_frame(0, 0);
633
634 #if defined(OGL)
635 # if 1
636                 buf = d_malloc(THUMBNAIL_W * THUMBNAIL_H * 3);
637                 glGetIntegerv(GL_DRAW_BUFFER, &gl_draw_buffer);
638                 glReadBuffer(gl_draw_buffer);
639                 glReadPixels(0, SHEIGHT - THUMBNAIL_H, THUMBNAIL_W, THUMBNAIL_H, GL_RGB, GL_UNSIGNED_BYTE, buf);
640                 k = THUMBNAIL_H;
641                 for (i = 0; i < THUMBNAIL_W * THUMBNAIL_H; i++) {
642                         if (!(j = i % THUMBNAIL_W))
643                                 k--;
644                         cnv->cv_bitmap.bm_data[THUMBNAIL_W * k + j] =
645                                 gr_find_closest_color(buf[3*i]/4, buf[3*i+1]/4, buf[3*i+2]/4);
646                 }
647                 d_free(buf);
648 # else // simpler d1x method, not tested yet
649                 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);
650 # endif
651 #endif
652
653                 pal = gr_palette;
654
655                 PHYSFS_write(fp, cnv->cv_bitmap.bm_data, THUMBNAIL_W * THUMBNAIL_H, 1);
656
657                 gr_set_current_canvas(cnv_save);
658                 gr_free_canvas( cnv );
659                 PHYSFS_write(fp, pal, 3, 256);
660         }
661         else
662         {
663                 ubyte color = 0;
664                 for ( i=0; i<THUMBNAIL_W*THUMBNAIL_H; i++ )
665                         PHYSFS_write(fp, &color, sizeof(ubyte), 1);             
666         } 
667
668 // Save the Between levels flag...
669         PHYSFS_write(fp, &between_levels, sizeof(int), 1);
670
671 // Save the mission info...
672         mprintf ((0, "HEY! Mission name is %s\n", Current_mission_filename));
673         PHYSFS_write(fp, Current_mission_filename, 9 * sizeof(char), 1);
674
675 //Save level info
676         PHYSFS_write(fp, &Current_level_num, sizeof(int), 1);
677         PHYSFS_write(fp, &Next_level_num, sizeof(int), 1);
678
679 //Save GameTime
680         PHYSFS_write(fp, &GameTime, sizeof(fix), 1);
681
682 // If coop save, save all
683 #ifdef NETWORK
684    if (Game_mode & GM_MULTI_COOP)
685          {
686                 PHYSFS_write(fp, &state_game_id,sizeof(int), 1);
687                 PHYSFS_write(fp, &Netgame,sizeof(netgame_info), 1);
688                 PHYSFS_write(fp, &NetPlayers,sizeof(AllNetPlayers_info), 1);
689                 PHYSFS_write(fp, &N_players,sizeof(int), 1);
690                 PHYSFS_write(fp, &Player_num,sizeof(int), 1);
691                 for (i=0;i<N_players;i++)
692                         PHYSFS_write(fp, &Players[i], sizeof(player), 1);
693
694 #ifdef RISKY_PROPOSITION
695                 PHYSFS_write(fp, &robot_controlled[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
696                 PHYSFS_write(fp, &robot_agitation[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
697                 PHYSFS_write(fp, &robot_controlled_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
698                 PHYSFS_write(fp, &robot_last_send_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
699                 PHYSFS_write(fp, &robot_last_message_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
700                 PHYSFS_write(fp, &robot_send_pending[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
701                 PHYSFS_write(fp, &robot_fired[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
702  
703       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
704                         PHYSFS_write(fp, robot_fire_buf[i][0], 18 + 3, 1);
705 #endif
706
707          }
708 #endif
709
710 //Save player info
711         PHYSFS_write(fp, &Players[Player_num], sizeof(player), 1);
712
713 // Save the current weapon info
714         PHYSFS_write(fp, &Primary_weapon, sizeof(sbyte), 1);
715         PHYSFS_write(fp, &Secondary_weapon, sizeof(sbyte), 1);
716
717 // Save the difficulty level
718         PHYSFS_write(fp, &Difficulty_level, sizeof(int), 1);
719 // Save cheats enabled
720         PHYSFS_write(fp, &Cheats_enabled,sizeof(int), 1);
721
722         if ( !between_levels )  {
723
724         //Finish all morph objects
725                 for (i=0; i<=Highest_object_index; i++ )        {
726                         if ( (Objects[i].type != OBJ_NONE) && (Objects[i].render_type==RT_MORPH))       {
727                                 morph_data *md;
728                                 md = find_morph_data(&Objects[i]);
729                                 if (md) {                                       
730                                         md->obj->control_type = md->morph_save_control_type;
731                                         md->obj->movement_type = md->morph_save_movement_type;
732                                         md->obj->render_type = RT_POLYOBJ;
733                                         md->obj->mtype.phys_info = md->morph_save_phys_info;
734                                         md->obj = NULL;
735                                 } else {                                                //maybe loaded half-morphed from disk
736                                         Objects[i].flags |= OF_SHOULD_BE_DEAD;
737                                         Objects[i].render_type = RT_POLYOBJ;
738                                         Objects[i].control_type = CT_NONE;
739                                         Objects[i].movement_type = MT_NONE;
740                                 }
741                         }
742                 }
743         
744         //Save object info
745                 i = Highest_object_index+1;
746                 PHYSFS_write(fp, &i, sizeof(int), 1);
747                 PHYSFS_write(fp, Objects, sizeof(object), i);
748                 
749         //Save wall info
750                 i = Num_walls;
751                 PHYSFS_write(fp, &i, sizeof(int), 1);
752                 PHYSFS_write(fp, Walls, sizeof(wall), i);
753
754         //Save exploding wall info
755                 i = MAX_EXPLODING_WALLS;
756                 PHYSFS_write(fp, &i, sizeof(int), 1);
757                 PHYSFS_write(fp, expl_wall_list, sizeof(*expl_wall_list), i);
758         
759         //Save door info
760                 i = Num_open_doors;
761                 PHYSFS_write(fp, &i, sizeof(int), 1);
762                 PHYSFS_write(fp, ActiveDoors, sizeof(active_door), i);
763         
764         //Save cloaking wall info
765                 i = Num_cloaking_walls;
766                 PHYSFS_write(fp, &i, sizeof(int), 1);
767                 PHYSFS_write(fp, CloakingWalls, sizeof(cloaking_wall), i);
768         
769         //Save trigger info
770                 PHYSFS_write(fp, &Num_triggers, sizeof(int), 1);
771                 PHYSFS_write(fp, Triggers, sizeof(trigger), Num_triggers);
772         
773         //Save tmap info
774                 for (i = 0; i <= Highest_segment_index; i++)
775                 {
776                         for (j = 0; j < 6; j++)
777                         {
778                                 PHYSFS_write(fp, &Segments[i].sides[j].wall_num, sizeof(short), 1);
779                                 PHYSFS_write(fp, &Segments[i].sides[j].tmap_num, sizeof(short), 1);
780                                 PHYSFS_write(fp, &Segments[i].sides[j].tmap_num2, sizeof(short), 1);
781                         }
782                 }
783         
784         // Save the fuelcen info
785                 PHYSFS_write(fp, &Control_center_destroyed, sizeof(int), 1);
786                 PHYSFS_write(fp, &Countdown_timer, sizeof(int), 1);
787                 PHYSFS_write(fp, &Num_robot_centers, sizeof(int), 1);
788                 PHYSFS_write(fp, RobotCenters, sizeof(matcen_info), Num_robot_centers);
789                 PHYSFS_write(fp, &ControlCenterTriggers, sizeof(control_center_triggers), 1);
790                 PHYSFS_write(fp, &Num_fuelcenters, sizeof(int), 1);
791                 PHYSFS_write(fp, Station, sizeof(FuelCenter), Num_fuelcenters);
792         
793         // Save the control cen info
794                 PHYSFS_write(fp, &Control_center_been_hit, sizeof(int), 1);
795                 PHYSFS_write(fp, &Control_center_player_been_seen, sizeof(int), 1);
796                 PHYSFS_write(fp, &Control_center_next_fire_time, sizeof(int), 1);
797                 PHYSFS_write(fp, &Control_center_present, sizeof(int), 1);
798                 PHYSFS_write(fp, &Dead_controlcen_object_num, sizeof(int), 1);
799         
800         // Save the AI state
801                 ai_save_state( fp );
802         
803         // Save the automap visited info
804                 PHYSFS_write(fp, Automap_visited, sizeof(ubyte), MAX_SEGMENTS);
805
806         }
807         PHYSFS_write(fp, &state_game_id, sizeof(uint), 1);
808         PHYSFS_write(fp, &Laser_rapid_fire, sizeof(int), 1);
809         PHYSFS_write(fp, &Lunacy, sizeof(int), 1);  //  Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
810         PHYSFS_write(fp, &Lunacy, sizeof(int), 1);
811
812         // Save automap marker info
813
814         PHYSFS_write(fp, MarkerObject, sizeof(MarkerObject) ,1);
815         PHYSFS_write(fp, MarkerOwner, sizeof(MarkerOwner), 1);
816         PHYSFS_write(fp, MarkerMessage, sizeof(MarkerMessage), 1);
817
818         PHYSFS_write(fp, &Afterburner_charge, sizeof(fix), 1);
819
820         //save last was super information
821         PHYSFS_write(fp, &Primary_last_was_super, sizeof(Primary_last_was_super), 1);
822         PHYSFS_write(fp, &Secondary_last_was_super, sizeof(Secondary_last_was_super), 1);
823
824         //      Save flash effect stuff
825         PHYSFS_write(fp, &Flash_effect, sizeof(int), 1);
826         PHYSFS_write(fp, &Time_flash_last_played, sizeof(int), 1);
827         PHYSFS_write(fp, &PaletteRedAdd, sizeof(int), 1);
828         PHYSFS_write(fp, &PaletteGreenAdd, sizeof(int), 1);
829         PHYSFS_write(fp, &PaletteBlueAdd, sizeof(int), 1);
830
831         PHYSFS_write(fp, Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS);
832
833         PHYSFS_write(fp, &First_secret_visit, sizeof(First_secret_visit), 1);
834
835         if (PHYSFS_write(fp, &Omega_charge, sizeof(Omega_charge), 1) < 1)
836         {
837                 if ( !(Game_mode & GM_MULTI) ) {
838                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
839                         PHYSFS_close(fp);
840                         PHYSFS_delete(filename);
841                 }
842         } else  {
843                 PHYSFS_close(fp);
844
845                 #ifdef MACINTOSH                // set the type and creator of the saved game file
846                 {
847                         FInfo finfo;
848                         OSErr err;
849                         Str255 pfilename;
850         
851                         strcpy(pfilename, filename);
852                         c2pstr(pfilename);
853                         err = HGetFInfo(0, 0, pfilename, &finfo);
854                         finfo.fdType = 'SVGM';
855                         finfo.fdCreator = 'DCT2';
856                         err = HSetFInfo(0, 0, pfilename, &finfo);
857                 }
858                 #endif
859         }
860         
861         start_time();
862
863         return 1;
864 }
865
866 //      -----------------------------------------------------------------------------------
867 //      Set the player's position from the globals Secret_return_segment and Secret_return_orient.
868 void set_pos_from_return_segment(void)
869 {
870         int     plobjnum = Players[Player_num].objnum;
871
872         compute_segment_center(&Objects[plobjnum].pos, &Segments[Secret_return_segment]);
873         obj_relink(plobjnum, Secret_return_segment);
874         reset_player_object();
875         Objects[plobjnum].orient = Secret_return_orient;
876 }
877
878 //      -----------------------------------------------------------------------------------
879 int state_restore_all(int in_game, int secret_restore, char *filename_override)
880 {
881         char filename[128];
882         int     filenum = -1;
883
884 #ifdef NETWORK
885         if ( Game_mode & GM_MULTI )     {
886 #ifdef MULTI_SAVE
887                         multi_initiate_restore_game();
888 #endif
889                 return 0;
890         }
891 #endif
892
893         if (in_game && (Current_level_num < 0) && (secret_restore == 0)) {
894                 HUD_init_message( "Can't restore in secret level!" );
895                 return 0;
896         }
897
898         if ( Newdemo_state == ND_STATE_RECORDING )
899                 newdemo_stop_recording();
900
901         if ( Newdemo_state != ND_STATE_NORMAL )
902                 return 0;
903
904         stop_time();
905
906         if (filename_override) {
907                 strcpy(filename, filename_override);
908                 filenum = NUM_SAVES+1;          //      So we don't trigger autosave
909         } else if (!(filenum = state_get_restore_file(filename, 0)))    {
910                 start_time();
911                 return 0;
912         }
913         
914         //      MK, 1/1/96
915         //      If not in multiplayer, do special secret level stuff.
916         //      If Nsecret.sgc (where N = filenum) exists, then copy it to secret.sgc.
917         //      If it doesn't exist, then delete secret.sgc
918         if (!secret_restore && !(Game_mode & GM_MULTI)) {
919                 int     rval;
920                 char    temp_fname[32], fc;
921
922                 if (filenum != -1) {
923                         if (filenum >= 10)
924                                 fc = (filenum-10) + 'a';
925                         else
926                                 fc = '0' + filenum;
927                         
928                         #ifndef MACINTOSH
929                         sprintf(temp_fname, "%csecret.sgc", fc);
930                         #else
931                         sprintf(temp_fname, "Players/%csecret.sgc", fc);
932                         #endif
933
934                         mprintf((0, "Trying to copy %s to secret.sgc.\n", temp_fname));
935
936                         if (PHYSFS_exists(temp_fname))
937                         {
938                                 mprintf((0, "Copying %s to secret.sgc\n", temp_fname));
939                                 rval = copy_file(temp_fname, SECRETC_FILENAME);
940                                 Assert(rval == 0);      //      Oops, error copying temp_fname to secret.sgc!
941                         } else
942                                 PHYSFS_delete(SECRETC_FILENAME);
943                 }
944         }
945
946         //      Changed, 11/15/95, MK, don't to autosave if restoring from main menu.
947         if ((filenum != (NUM_SAVES+1)) && in_game) {
948                 char    temp_filename[128];
949                 mprintf((0, "Doing autosave, filenum = %i, != %i!\n", filenum, NUM_SAVES+1));
950                 #ifndef MACINTOSH
951                 sprintf( temp_filename, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
952                 #else
953                 sprintf(temp_filename, "Players/%s.sg%x", Players[Player_num].callsign, NUM_SAVES);
954                 #endif
955                 state_save_all(!in_game, secret_restore, temp_filename, 0);
956         }
957
958         if ( !secret_restore && in_game ) {
959                 int choice;
960                 choice =  nm_messagebox( NULL, 2, "Yes", "No", "Restore Game?" );
961                 if ( choice != 0 )      {
962                         start_time();
963                         return 0;
964                 }
965         }
966
967         start_time();
968
969         return state_restore_all_sub(filename, 0, secret_restore);
970 }
971
972 extern void init_player_stats_new_ship(void);
973
974 void ShowLevelIntro(int level_num);
975
976 extern void do_cloak_invul_secret_stuff(fix old_gametime);
977 extern void copy_defaults_to_robot(object *objp);
978
979 int state_restore_all_sub(char *filename, int multi, int secret_restore)
980 {
981         int ObjectStartLocation;
982         int version,i, j, segnum;
983         object * obj;
984         PHYSFS_file *fp;
985         int current_level, next_level;
986         int between_levels;
987         char mission[16];
988         char desc[DESC_LENGTH+1];
989         char id[5];
990         char org_callsign[CALLSIGN_LEN+16];
991 #ifdef NETWORK
992         int found;
993         int nplayers;   //,playid[12],mynum;
994         player restore_players[MAX_PLAYERS];
995 #endif
996         fix     old_gametime = GameTime;
997         short TempTmapNum[MAX_SEGMENTS][MAX_SIDES_PER_SEGMENT];
998         short TempTmapNum2[MAX_SEGMENTS][MAX_SIDES_PER_SEGMENT];
999
1000         #if defined(MACINTOSH) && !defined(NDEBUG)
1001         if (strncmp(filename, "Players/", 9))
1002                 Int3();
1003         #endif
1004
1005         fp = PHYSFSX_openReadBuffered(filename);
1006         if ( !fp ) return 0;
1007
1008 //Read id
1009         //FIXME: check for swapped file, react accordingly...
1010         PHYSFS_read(fp, id, sizeof(char) * 4, 1);
1011         if ( memcmp( id, dgss_id, 4 )) {
1012                 PHYSFS_close(fp);
1013                 return 0;
1014         }
1015
1016 //Read version
1017         PHYSFS_read(fp, &version, sizeof(int), 1);
1018         if (version < STATE_COMPATIBLE_VERSION) {
1019                 PHYSFS_close(fp);
1020                 return 0;
1021         }
1022
1023 // Read description
1024         PHYSFS_read(fp, desc, sizeof(char) * DESC_LENGTH, 1);
1025
1026 // Skip the current screen shot...
1027         PHYSFS_seek(fp, PHYSFS_tell(fp) + THUMBNAIL_W * THUMBNAIL_H);
1028
1029 // And now...skip the goddamn palette stuff that somebody forgot to add
1030         PHYSFS_seek(fp, PHYSFS_tell(fp) + 768);
1031
1032 // Read the Between levels flag...
1033         PHYSFS_read(fp, &between_levels, sizeof(int), 1);
1034
1035         Assert(between_levels == 0);    //between levels save ripped out
1036
1037 // Read the mission info...
1038         PHYSFS_read(fp, mission, sizeof(char) * 9, 1);
1039         mprintf ((0,"Missionname to load = %s\n",mission));
1040
1041         if (!load_mission_by_name( mission ))   {
1042                 nm_messagebox( NULL, 1, "Ok", "Error!\nUnable to load mission\n'%s'\n", mission );
1043                 PHYSFS_close(fp);
1044                 return 0;
1045         }
1046
1047 //Read level info
1048         PHYSFS_read(fp, &current_level, sizeof(int), 1);
1049         PHYSFS_read(fp, &next_level, sizeof(int), 1);
1050
1051 //Restore GameTime
1052         PHYSFS_read(fp, &GameTime, sizeof(fix), 1);
1053
1054 // Start new game....
1055         if (!multi)     {
1056                 Game_mode = GM_NORMAL;
1057                 Function_mode = FMODE_GAME;
1058 #ifdef NETWORK
1059                 change_playernum_to(0);
1060 #endif
1061                 strcpy( org_callsign, Players[0].callsign );
1062                 N_players = 1;
1063                 if (!secret_restore) {
1064                         InitPlayerObject();                             //make sure player's object set up
1065                         init_player_stats_game();               //clear all stats
1066                 }
1067         } else {
1068                 strcpy( org_callsign, Players[Player_num].callsign );
1069         }
1070
1071 #ifdef NETWORK
1072    if (Game_mode & GM_MULTI)
1073          {
1074                 PHYSFS_read(fp, &state_game_id, sizeof(int), 1);
1075                 PHYSFS_read(fp, &Netgame, sizeof(netgame_info), 1);
1076                 PHYSFS_read(fp, &NetPlayers, sizeof(AllNetPlayers_info), 1);
1077                 PHYSFS_read(fp, &nplayers, sizeof(N_players), 1);
1078                 PHYSFS_read(fp, &Player_num, sizeof(Player_num), 1);
1079                 for (i=0;i<nplayers;i++)
1080                         PHYSFS_read(fp, &restore_players[i], sizeof(player), 1);
1081 #ifdef RISKY_PROPOSITION
1082                 PHYSFS_read(fp, &robot_controlled[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1083                 PHYSFS_read(fp, &robot_agitation[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1084                 PHYSFS_read(fp, &robot_controlled_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1085                 PHYSFS_read(fp, &robot_last_send_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1086                 PHYSFS_read(fp, &robot_last_message_time[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1087                 PHYSFS_read(fp, &robot_send_pending[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1088                 PHYSFS_read(fp, &robot_fired[0], 4 * MAX_ROBOTS_CONTROLLED, 1);
1089
1090       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
1091                         PHYSFS_read(fp, &robot_fire_buf[i][0],21,1);
1092 #endif
1093
1094            for (i=0;i<nplayers;i++)
1095                  {
1096                   found=0;
1097                   for (j=0;j<nplayers;j++)
1098                          {
1099            if ((!strcmp (restore_players[i].callsign,Players[j].callsign)) && Players[j].connected==1)
1100                                  found=1;
1101                          }
1102                   restore_players[i].connected=found;
1103             }
1104                 memcpy (&Players,&restore_players,sizeof(player)*nplayers);
1105                 N_players=nplayers;
1106
1107       if (network_i_am_master())
1108                  {
1109                   for (i=0;i<N_players;i++)
1110                         {
1111                          if (i==Player_num)
1112                                 continue;
1113                  Players[i].connected=0;
1114                         }
1115                  }
1116
1117                 //Viewer = ConsoleObject = &Objects[Players[Player_num].objnum];
1118          }
1119
1120 #endif
1121
1122 //Read player info
1123
1124         {
1125                 StartNewLevelSub(current_level, 1, secret_restore);
1126
1127                 if (secret_restore) {
1128                         player  dummy_player;
1129
1130                         PHYSFS_read(fp, &dummy_player, sizeof(player), 1);
1131                         if (secret_restore == 1) {              //      This means he didn't die, so he keeps what he got in the secret level.
1132                                 Players[Player_num].level = dummy_player.level;
1133                                 Players[Player_num].last_score = dummy_player.last_score;
1134                                 Players[Player_num].time_level = dummy_player.time_level;
1135
1136                                 Players[Player_num].num_robots_level = dummy_player.num_robots_level;
1137                                 Players[Player_num].num_robots_total = dummy_player.num_robots_total;
1138                                 Players[Player_num].hostages_rescued_total = dummy_player.hostages_rescued_total;
1139                                 Players[Player_num].hostages_total = dummy_player.hostages_total;
1140                                 Players[Player_num].hostages_on_board = dummy_player.hostages_on_board;
1141                                 Players[Player_num].hostages_level = dummy_player.hostages_level;
1142                                 Players[Player_num].homing_object_dist = dummy_player.homing_object_dist;
1143                                 Players[Player_num].hours_level = dummy_player.hours_level;
1144                                 Players[Player_num].hours_total = dummy_player.hours_total;
1145                                 do_cloak_invul_secret_stuff(old_gametime);
1146                         } else {
1147                                 Players[Player_num] = dummy_player;
1148                         }
1149                 } else {
1150                         PHYSFS_read(fp, &Players[Player_num], sizeof(player), 1);
1151                 }
1152         }
1153         strcpy( Players[Player_num].callsign, org_callsign );
1154
1155 // Set the right level
1156         if ( between_levels )
1157                 Players[Player_num].level = next_level;
1158
1159 // Restore the weapon states
1160         PHYSFS_read(fp, &Primary_weapon, sizeof(sbyte), 1);
1161         PHYSFS_read(fp, &Secondary_weapon, sizeof(sbyte), 1);
1162
1163         select_weapon(Primary_weapon, 0, 0, 0);
1164         select_weapon(Secondary_weapon, 1, 0, 0);
1165
1166 // Restore the difficulty level
1167         PHYSFS_read(fp, &Difficulty_level, sizeof(int), 1);
1168
1169 // Restore the cheats enabled flag
1170
1171         PHYSFS_read(fp, &Cheats_enabled, sizeof(int),1);
1172
1173         if ( !between_levels )  {
1174                 Do_appearance_effect = 0;                       // Don't do this for middle o' game stuff.
1175
1176                 ObjectStartLocation = PHYSFS_tell(fp);
1177                 //Clear out all the objects from the lvl file
1178                 for (segnum=0; segnum <= Highest_segment_index; segnum++)
1179                         Segments[segnum].objects = -1;
1180                 reset_objects(1);
1181         
1182                 //Read objects, and pop 'em into their respective segments.
1183                 PHYSFS_read(fp, &i, sizeof(int), 1);
1184                 Highest_object_index = i-1;
1185                 PHYSFS_read(fp, Objects, sizeof(object) * i, 1);
1186         
1187                 Object_next_signature = 0;
1188                 for (i=0; i<=Highest_object_index; i++ )        {
1189                         obj = &Objects[i];
1190                         obj->rtype.pobj_info.alt_textures = -1;
1191                         segnum = obj->segnum;
1192                         obj->next = obj->prev = obj->segnum = -1;
1193                         if ( obj->type != OBJ_NONE )    {
1194                                 obj_link(i,segnum);
1195                                 if ( obj->signature > Object_next_signature )
1196                                         Object_next_signature = obj->signature;
1197                         }
1198
1199                         //look for, and fix, boss with bogus shields
1200                         if (obj->type == OBJ_ROBOT && Robot_info[obj->id].boss_flag) {
1201                                 fix save_shields = obj->shields;
1202
1203                                 copy_defaults_to_robot(obj);            //calculate starting shields
1204
1205                                 //if in valid range, use loaded shield value
1206                                 if (save_shields > 0 && save_shields <= obj->shields)
1207                                         obj->shields = save_shields;
1208                                 else
1209                                         obj->shields /= 2;  //give player a break
1210                         }
1211
1212                 }       
1213                 special_reset_objects();
1214                 Object_next_signature++;
1215         
1216                 //      1 = Didn't die on secret level.
1217                 //      2 = Died on secret level.
1218                 if (secret_restore && (Current_level_num >= 0)) {
1219                         set_pos_from_return_segment();
1220                         if (secret_restore == 2)
1221                                 init_player_stats_new_ship();
1222                 }
1223
1224                 //Restore wall info
1225                 PHYSFS_read(fp, &i, sizeof(int), 1);
1226                 Num_walls = i;
1227                 PHYSFS_read(fp, Walls, sizeof(wall), Num_walls);
1228
1229                 //now that we have the walls, check if any sounds are linked to
1230                 //walls that are now open
1231                 for (i=0;i<Num_walls;i++) {
1232                         if (Walls[i].type == WALL_OPEN)
1233                                 digi_kill_sound_linked_to_segment(Walls[i].segnum,Walls[i].sidenum,-1); //-1 means kill any sound
1234                 }
1235
1236                 //Restore exploding wall info
1237                 if (version >= 10) {
1238                         PHYSFS_read(fp, &i, sizeof(int), 1);
1239                         PHYSFS_read(fp, expl_wall_list, sizeof(*expl_wall_list), i);
1240                 }
1241
1242                 //Restore door info
1243                 PHYSFS_read(fp, &i, sizeof(int), 1);
1244                 Num_open_doors = i;
1245                 PHYSFS_read(fp, ActiveDoors, sizeof(active_door), Num_open_doors);
1246         
1247                 if (version >= 14) {            //Restore cloaking wall info
1248                         PHYSFS_read(fp, &i, sizeof(int), 1);
1249                         Num_cloaking_walls = i;
1250                         PHYSFS_read(fp, CloakingWalls, sizeof(cloaking_wall), Num_cloaking_walls);
1251                 }
1252         
1253                 //Restore trigger info
1254                 PHYSFS_read(fp, &Num_triggers, sizeof(int), 1);
1255                 PHYSFS_read(fp, Triggers, sizeof(trigger), Num_triggers);
1256         
1257                 //Restore tmap info (to temp values so we can use compiled-in tmap info to compute static_light)
1258                 for (i=0; i<=Highest_segment_index; i++ )       {
1259                         for (j=0; j<6; j++ )    {
1260                                 PHYSFS_read(fp, &Segments[i].sides[j].wall_num, sizeof(short), 1);
1261                                 PHYSFS_read(fp, &TempTmapNum[i][j], sizeof(short), 1);
1262                                 PHYSFS_read(fp, &TempTmapNum2[i][j], sizeof(short), 1);
1263                         }
1264                 }
1265         
1266                 //Restore the fuelcen info
1267                 PHYSFS_read(fp, &Control_center_destroyed, sizeof(int), 1);
1268                 PHYSFS_read(fp, &Countdown_timer, sizeof(int), 1);
1269                 PHYSFS_read(fp, &Num_robot_centers, sizeof(int), 1);
1270                 PHYSFS_read(fp, RobotCenters, sizeof(matcen_info), Num_robot_centers);
1271                 PHYSFS_read(fp, &ControlCenterTriggers, sizeof(control_center_triggers), 1);
1272                 PHYSFS_read(fp, &Num_fuelcenters, sizeof(int), 1);
1273                 PHYSFS_read(fp, Station, sizeof(FuelCenter), Num_fuelcenters);
1274         
1275                 // Restore the control cen info
1276                 PHYSFS_read(fp, &Control_center_been_hit, sizeof(int), 1);
1277                 PHYSFS_read(fp, &Control_center_player_been_seen, sizeof(int), 1);
1278                 PHYSFS_read(fp, &Control_center_next_fire_time, sizeof(int), 1);
1279                 PHYSFS_read(fp, &Control_center_present, sizeof(int), 1);
1280                 PHYSFS_read(fp, &Dead_controlcen_object_num, sizeof(int), 1);
1281         
1282                 // Restore the AI state
1283                 ai_restore_state( fp, version );
1284         
1285                 // Restore the automap visited info
1286                 PHYSFS_read(fp, Automap_visited, sizeof(ubyte), MAX_SEGMENTS);
1287
1288                 //      Restore hacked up weapon system stuff.
1289                 Fusion_next_sound_time = GameTime;
1290                 Auto_fire_fusion_cannon_time = 0;
1291                 Next_laser_fire_time = GameTime;
1292                 Next_missile_fire_time = GameTime;
1293                 Last_laser_fired_time = GameTime;
1294
1295         }
1296         state_game_id = 0;
1297
1298         if ( version >= 7 )     {
1299                 PHYSFS_read(fp, &state_game_id, sizeof(uint), 1);
1300                 PHYSFS_read(fp, &Laser_rapid_fire, sizeof(int), 1);
1301                 PHYSFS_read(fp, &Lunacy, sizeof(int), 1);               //      Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
1302                 PHYSFS_read(fp, &Lunacy, sizeof(int), 1);
1303                 if ( Lunacy )
1304                         do_lunacy_on();
1305         }
1306
1307         if (version >= 17) {
1308                 PHYSFS_read(fp, MarkerObject, sizeof(MarkerObject), 1);
1309                 PHYSFS_read(fp, MarkerOwner, sizeof(MarkerOwner), 1);
1310                 PHYSFS_read(fp, MarkerMessage, sizeof(MarkerMessage), 1);
1311         }
1312         else {
1313                 int num,dummy;
1314
1315                 // skip dummy info
1316
1317                 PHYSFS_read(fp, &num,sizeof(int), 1);           // was NumOfMarkers
1318                 PHYSFS_read(fp, &dummy,sizeof(int), 1);         // was CurMarker
1319
1320                 PHYSFS_seek(fp, PHYSFS_tell(fp) + num * (sizeof(vms_vector) + 40));
1321
1322                 for (num=0;num<NUM_MARKERS;num++)
1323                         MarkerObject[num] = -1;
1324         }
1325
1326         if (version>=11) {
1327                 if (secret_restore != 1)
1328                         PHYSFS_read(fp, &Afterburner_charge, sizeof(fix), 1);
1329                 else {
1330                         fix     dummy_fix;
1331                         PHYSFS_read(fp, &dummy_fix, sizeof(fix), 1);
1332                 }
1333         }
1334         if (version>=12) {
1335                 //read last was super information
1336                 PHYSFS_read(fp, &Primary_last_was_super, sizeof(Primary_last_was_super), 1);
1337                 PHYSFS_read(fp, &Secondary_last_was_super, sizeof(Secondary_last_was_super), 1);
1338         }
1339
1340         if (version >= 12) {
1341                 PHYSFS_read(fp, &Flash_effect, sizeof(int), 1);
1342                 PHYSFS_read(fp, &Time_flash_last_played, sizeof(int), 1);
1343                 PHYSFS_read(fp, &PaletteRedAdd, sizeof(int), 1);
1344                 PHYSFS_read(fp, &PaletteGreenAdd, sizeof(int), 1);
1345                 PHYSFS_read(fp, &PaletteBlueAdd, sizeof(int), 1);
1346         } else {
1347                 Flash_effect = 0;
1348                 Time_flash_last_played = 0;
1349                 PaletteRedAdd = 0;
1350                 PaletteGreenAdd = 0;
1351                 PaletteBlueAdd = 0;
1352         }
1353
1354         //      Load Light_subtracted
1355         if (version >= 16) {
1356                 PHYSFS_read(fp, Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS);
1357                 apply_all_changed_light();
1358         } else {
1359                 int     i;
1360                 for (i=0; i<=Highest_segment_index; i++)
1361                         Light_subtracted[i] = 0;
1362         }
1363
1364         // static_light should now be computed - now actually set tmap info
1365         for (i=0; i<=Highest_segment_index; i++ )       {
1366                 for (j=0; j<6; j++ )    {
1367                         Segments[i].sides[j].tmap_num=TempTmapNum[i][j];
1368                         Segments[i].sides[j].tmap_num2=TempTmapNum2[i][j];
1369                 }
1370         }
1371
1372         if (!secret_restore) {
1373                 if (version >= 20) {
1374                         PHYSFS_read(fp, &First_secret_visit, sizeof(First_secret_visit), 1);
1375                         mprintf((0, "File: [%s] Read First_secret_visit: New value = %i\n", filename, First_secret_visit));
1376                 } else
1377                         First_secret_visit = 1;
1378         } else
1379                 First_secret_visit = 0;
1380
1381         if (version >= 22)
1382         {
1383                 if (secret_restore != 1)
1384                         PHYSFS_read(fp, &Omega_charge, sizeof(fix), 1);
1385                 else {
1386                         fix     dummy_fix;
1387                         PHYSFS_read(fp, &dummy_fix, sizeof(fix), 1);
1388                 }
1389         }
1390
1391         PHYSFS_close(fp);
1392
1393 #ifdef NETWORK
1394    if (Game_mode & GM_MULTI)   // Get rid of ships that aren't
1395          {                                                                       // connected in the restored game
1396                 for (i=0;i<nplayers;i++)
1397                  {
1398                   mprintf ((0,"Testing %s = %d\n",Players[i].callsign,Players[i].connected));
1399                   if (Players[i].connected!=1)
1400                    {
1401                     network_disconnect_player (i);
1402                create_player_appearance_effect(&Objects[Players[i].objnum]);
1403                          mprintf ((0,"Killing player ship %s!\n",Players[i].callsign));
1404               }
1405                  }
1406                         
1407          }
1408 #endif
1409
1410 // Load in bitmaps, etc..
1411 //!!    piggy_load_level_data();        //already done by StartNewLevelSub()
1412
1413         return 1;
1414 }
1415
1416
1417 int state_get_game_id(char *filename)
1418 {
1419         int version;
1420         PHYSFS_file *fp;
1421         int between_levels;
1422         char mission[16];
1423         char desc[DESC_LENGTH+1];
1424         char id[5];
1425         int dumbint;
1426
1427 mprintf((0, "Restoring multigame from [%s]\n", filename));
1428
1429         fp = PHYSFS_openRead(filename);
1430         if (!fp) return 0;
1431
1432 //Read id
1433         //FIXME: check for swapped file, react accordingly...
1434         PHYSFS_read(fp, id, sizeof(char) * 4, 1);
1435         if ( memcmp( id, dgss_id, 4 )) {
1436                 PHYSFS_close(fp);
1437                 return 0;
1438         }
1439
1440 //Read version
1441         PHYSFS_read(fp, &version, sizeof(int), 1);
1442         if (version < STATE_COMPATIBLE_VERSION) {
1443                 PHYSFS_close(fp);
1444                 return 0;
1445         }
1446
1447 // Read description
1448         PHYSFS_read(fp, desc, sizeof(char) * DESC_LENGTH, 1);
1449
1450 // Skip the current screen shot...
1451         PHYSFS_seek(fp, PHYSFS_tell(fp) + THUMBNAIL_W * THUMBNAIL_H);
1452
1453 // And now...skip the palette stuff that somebody forgot to add
1454         PHYSFS_seek(fp, PHYSFS_tell(fp) + 768);
1455
1456 // Read the Between levels flag...
1457         PHYSFS_read(fp, &between_levels, sizeof(int), 1);
1458
1459         Assert(between_levels == 0);    //between levels save ripped out
1460
1461 // Read the mission info...
1462         PHYSFS_read(fp, mission, sizeof(char) * 9, 1);
1463 //Read level info
1464         PHYSFS_read(fp, &dumbint, sizeof(int), 1);
1465         PHYSFS_read(fp, &dumbint, sizeof(int), 1);
1466
1467 //Restore GameTime
1468         PHYSFS_read(fp, &dumbint, sizeof(fix), 1);
1469
1470         PHYSFS_read(fp, &state_game_id, sizeof(int), 1);
1471
1472         return (state_game_id);
1473  }