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