configuration fixes
[btb/d2x.git] / main / state.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #ifdef RCS
16 char state_rcsid[] = "$Id: state.c,v 1.2 2001-01-20 13:49:17 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20
21 #ifdef WINDOWS
22 #include "desw.h"
23 #endif
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #ifdef MACINTOSH
33 #include <Files.h>
34 #endif
35
36 #include "pstypes.h"
37 #include "pa_enabl.h"                   //$$POLY_ACC
38 #include "mono.h"
39 #include "inferno.h"
40 #include "segment.h"
41 #include "textures.h"
42 #include "wall.h"
43 #include "object.h"
44 #include "digi.h"
45 #include "gamemine.h"
46 #include "error.h"
47 #include "gameseg.h"
48 #include "menu.h"
49 #include "switch.h"
50 #include "game.h"
51 #include "screens.h"
52 #include "newmenu.h"
53 #include "cfile.h"              
54 #include "fuelcen.h"
55 #include "hash.h"
56 #include "key.h"
57 #include "piggy.h"
58 #include "player.h"
59 #include "cntrlcen.h"
60 #include "morph.h"
61 #include "weapon.h"
62 #include "render.h"
63 #include "gameseq.h"
64 #include "gauges.h"
65 #include "newdemo.h"
66 #include "automap.h"
67 #include "piggy.h"
68 #include "paging.h"
69 #include "titles.h"
70 #include "text.h"
71 #include "mission.h"
72 #include "pcx.h"
73 #include "u_mem.h"
74 #include "network.h"
75 #include "args.h"
76 #include "ai.h"
77 #include "fireball.h"
78 #include "controls.h"
79 #include "laser.h"
80 #include "multibot.h"
81 #include "state.h"
82
83 #if defined(POLY_ACC)
84 #include "poly_acc.h"
85 #endif
86
87 #define STATE_VERSION 22
88 #define STATE_COMPATIBLE_VERSION 20
89 // 0 - Put DGSS (Descent Game State Save) id at tof.
90 // 1 - Added Difficulty level save
91 // 2 - Added Cheats_enabled flag
92 // 3 - Added between levels save.
93 // 4 - Added mission support
94 // 5 - Mike changed ai and object structure.
95 // 6 - Added buggin' cheat save
96 // 7 - Added other cheat saves and game_id.
97 // 8 - Added AI stuff for escort and thief.
98 // 9 - Save palette with screen shot
99 // 12- Saved last_was_super array
100 // 13- Saved palette flash stuff
101 // 14- Save cloaking wall stuff
102 // 15- Save additional ai info
103 // 16- Save Light_subtracted
104 // 17- New marker save
105 // 18- Took out saving of old cheat status
106 // 19- Saved cheats_enabled flag
107 // 20- First_secret_visit
108 // 22- Omega_charge
109
110 #define NUM_SAVES 9
111 #define THUMBNAIL_W 100
112 #define THUMBNAIL_H 50
113 #define DESC_LENGTH 20
114
115 extern void multi_initiate_save_game();
116 extern void multi_initiate_restore_game();
117 extern void apply_all_changed_light(void);
118
119 extern int Do_appearance_effect;
120 extern fix Fusion_next_sound_time;
121
122 extern int Laser_rapid_fire;
123 extern int Physics_cheat_flag;
124 extern int      Lunacy;
125 extern void do_lunacy_on(void);
126 extern void do_lunacy_off(void);
127 extern int First_secret_visit;
128
129 int sc_last_item= 0;
130 grs_bitmap *sc_bmp[NUM_SAVES+1];
131
132 char dgss_id[4] = "DGSS";
133
134 int state_default_item = 0;
135
136 uint state_game_id;
137
138 extern int robot_controlled[MAX_ROBOTS_CONTROLLED];
139 extern int robot_agitation[MAX_ROBOTS_CONTROLLED];
140 extern fix robot_controlled_time[MAX_ROBOTS_CONTROLLED];
141 extern fix robot_last_send_time[MAX_ROBOTS_CONTROLLED];
142 extern fix robot_last_message_time[MAX_ROBOTS_CONTROLLED];
143 extern int robot_send_pending[MAX_ROBOTS_CONTROLLED];
144 extern int robot_fired[MAX_ROBOTS_CONTROLLED];
145 extern byte robot_fire_buf[MAX_ROBOTS_CONTROLLED][18+3];
146
147
148 #if defined(WINDOWS) || defined(MACINTOSH)
149 extern ubyte Hack_DblClick_MenuMode;
150 #endif
151
152 void compute_all_static_light(void);
153
154 //-------------------------------------------------------------------
155 void state_callback(int nitems,newmenu_item * items, int * last_key, int citem)
156 {
157         nitems = nitems;
158         last_key = last_key;
159         
160 //      if ( sc_last_item != citem )    {
161 //              sc_last_item = citem;
162                 if ( citem > 0 )        {
163                         if ( sc_bmp[citem-1] )  {
164                                 if (MenuHires) {
165                                 WINDOS(
166                                         dd_grs_canvas *save_canv = dd_grd_curcanv,
167                                         grs_canvas *save_canv = grd_curcanv
168                                 );
169                                         grs_canvas *temp_canv = gr_create_canvas(THUMBNAIL_W*2,(THUMBNAIL_H*24/10));
170                                         grs_point vertbuf[3] = {{0,0}, {0,0}, {i2f(THUMBNAIL_W*2),i2f(THUMBNAIL_H*24/10)} };
171                                         gr_set_current_canvas(temp_canv);
172                                         scale_bitmap(sc_bmp[citem-1], vertbuf, 0 );
173                                 WINDOS(
174                                         dd_gr_set_current_canvas(save_canv),
175                                         gr_set_current_canvas( save_canv )
176                                 );
177                                 WIN(DDGRLOCK(dd_grd_curcanv));
178                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W*2)/2,items[0].y-10, &temp_canv->cv_bitmap);
179                                 WIN(DDGRUNLOCK(dd_grd_curcanv));
180                                         gr_free_canvas(temp_canv);
181                                 }
182                                 else    {
183                                 #ifdef WINDOWS
184                                         Int3();
185                                 #else
186                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W)/2,items[0].y-5, sc_bmp[citem-1] );
187                                 #endif
188                                 }
189                         }
190                 }
191 //      }       
192 }
193
194 void rpad_string( char * string, int max_chars )
195 {
196         int i, end_found;
197
198         end_found = 0;
199         for( i=0; i<max_chars; i++ )    {
200                 if ( *string == 0 )
201                         end_found = 1;
202                 if ( end_found )
203                         *string = ' ';
204                 string++;
205         }
206         *string = 0;            // NULL terminate
207 }
208
209 int state_get_save_file(char * fname, char * dsc, int multi )
210 {
211         FILE * fp;
212         int i, choice, version;
213         newmenu_item m[NUM_SAVES+2];
214         char filename[NUM_SAVES+1][30];
215         char desc[NUM_SAVES+1][DESC_LENGTH+16];
216         char id[5];
217         int valid=0;
218         
219         for (i=0;i<NUM_SAVES; i++ )     {
220                 sc_bmp[i] = NULL;
221                 if ( !multi )
222                         #ifndef MACINTOSH
223                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
224                         #else
225                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
226                         #endif
227                 else
228                         #ifndef MACINTOSH
229                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
230                         #else
231                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
232                         #endif
233                 valid = 0;
234                 fp = fopen( filename[i], "rb" );
235                 if ( fp ) {
236                         //Read id
237                         fread( id, sizeof(char)*4, 1, fp );
238                         if ( !memcmp( id, dgss_id, 4 )) {
239                                 //Read version
240                                 fread( &version, sizeof(int), 1, fp );
241                                 if (version >= STATE_COMPATIBLE_VERSION)        {
242                                         // Read description
243                                         fread( desc[i], sizeof(char)*DESC_LENGTH, 1, fp );
244                                         //rpad_string( desc[i], DESC_LENGTH-1 );
245                                         // Read thumbnail
246                                         //sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
247                                         //fread( sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp );
248                                         valid = 1;
249                                 }
250                         } 
251                         fclose(fp);
252                 }
253                 if (!valid) {
254                         strcpy( desc[i], TXT_EMPTY );
255                         //rpad_string( desc[i], DESC_LENGTH-1 );
256                 }
257                 m[i].type = NM_TYPE_INPUT_MENU; m[i].text = desc[i]; m[i].text_len = DESC_LENGTH-1;
258         }
259
260         sc_last_item = -1;
261         choice = newmenu_do1( NULL, "Save Game", NUM_SAVES, m, NULL, state_default_item );
262
263         for (i=0; i<NUM_SAVES; i++ )    {
264                 if ( sc_bmp[i] )
265                         gr_free_bitmap( sc_bmp[i] );
266         }
267
268         if (choice > -1) {
269                 strcpy( fname, filename[choice] );
270                 strcpy( dsc, desc[choice] );
271                 state_default_item = choice;
272                 return choice+1;
273         }
274         return 0;
275 }
276
277 int RestoringMenu=0;
278 extern int Current_display_mode;
279
280 int state_get_restore_file(char * fname, int multi)
281 {
282         FILE * fp;
283         int i, choice, version, nsaves;
284         newmenu_item m[NUM_SAVES+2];
285         char filename[NUM_SAVES+1][30];
286         char desc[NUM_SAVES+1][DESC_LENGTH + 16];
287         char id[5];
288         int valid;
289
290         nsaves=0;
291         m[0].type = NM_TYPE_TEXT; m[0].text = "\n\n\n\n";       
292         for (i=0;i<NUM_SAVES+1; i++ )   {
293                 sc_bmp[i] = NULL;
294                 if (!multi)
295                         #ifndef MACINTOSH
296                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
297                         #else
298                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
299                         #endif
300                 else
301                         #ifndef MACINTOSH
302                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
303                         #else
304                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
305                         #endif
306                 valid = 0;
307                 fp = fopen( filename[i], "rb" );
308                 if ( fp ) {
309                         //Read id
310                         fread( id, sizeof(char)*4, 1, fp );
311                         if ( !memcmp( id, dgss_id, 4 )) {
312                                 //Read version
313                                 fread( &version, sizeof(int), 1, fp );
314                                 if (version >= STATE_COMPATIBLE_VERSION)        {
315                                         // Read description
316                                         fread( desc[i], sizeof(char)*DESC_LENGTH, 1, fp );
317                                         //rpad_string( desc[i], DESC_LENGTH-1 );
318                                         m[i+1].type = NM_TYPE_MENU; m[i+1].text = desc[i];
319                                         // Read thumbnail
320                                         sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
321                                         fread( sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp );
322                                         if (version >= 9) {
323                                                 ubyte pal[256*3];
324                                                 fread( pal, 3, 256, fp);
325                                                 gr_remap_bitmap_good( sc_bmp[i], pal, -1, -1 );
326                                         }
327                                         nsaves++;
328                                         valid = 1;
329                                 } 
330                         }
331                         fclose(fp);
332                 } 
333                 if (!valid) {
334                         strcpy( desc[i], TXT_EMPTY );
335                         //rpad_string( desc[i], DESC_LENGTH-1 );
336                         m[i+1].type = NM_TYPE_TEXT; m[i+1].text = desc[i];
337                 }
338         }
339
340         if ( nsaves < 1 )       {
341                 nm_messagebox( NULL, 1, "Ok", "No saved games were found!" );
342                 return 0;
343         }
344
345         if (Current_display_mode == 3)  //restore menu won't fit on 640x400
346                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
347
348         sc_last_item = -1;
349
350 #if defined(WINDOWS) || defined(MACINTOSH)
351         Hack_DblClick_MenuMode = 1;
352 #endif
353
354    RestoringMenu=1;
355         choice = newmenu_do3( NULL, "Select Game to Restore", NUM_SAVES+2, m, state_callback, state_default_item+1, NULL, 190, -1 );
356    RestoringMenu=0;
357
358 #if defined(WINDOWS) || defined(MACINTOSH)
359         Hack_DblClick_MenuMode = 0;
360 #endif
361
362         if (Current_display_mode == 3)  //set flag back
363                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
364
365
366         for (i=0; i<NUM_SAVES+1; i++ )  {
367                 if ( sc_bmp[i] )
368                         gr_free_bitmap( sc_bmp[i] );
369         }
370
371         if (choice > 0) {
372                 strcpy( fname, filename[choice-1] );
373                 if (choice != NUM_SAVES+1)              //no new default when restore from autosave
374                         state_default_item = choice - 1;
375                 return choice;
376         }
377         return 0;
378 }
379
380 #define DESC_OFFSET     8
381
382 //      -----------------------------------------------------------------------------------
383 //      Return true if the file named *filename exists, else return false.
384 int file_exists(char *filename)
385 {
386         FILE    *fp;
387
388         if ((fp = fopen(filename, "rb")) != NULL) {
389                 fclose(fp);
390                 return 1;
391         }
392
393         return 0;
394 }
395
396 #define CF_BUF_SIZE     1024
397
398 //      -----------------------------------------------------------------------------------
399 //      Imagine if C had a function to copy a file...
400 int copy_file(char *old_file, char *new_file)
401 {
402         byte    buf[CF_BUF_SIZE];
403         FILE    *in_file, *out_file;
404
405         out_file = fopen(new_file, "wb");
406
407         if (out_file == NULL)
408                 return -1;
409
410         in_file = fopen(old_file, "rb");
411
412         if (in_file == NULL)
413                 return -2;
414
415         while (!feof(in_file)) {
416                 int bytes_read;
417
418                 bytes_read = fread(buf, 1, CF_BUF_SIZE, in_file);
419                 if (ferror(in_file))
420                         Error("Cannot read from file <%s>: %s", old_file, strerror(errno));
421
422                 Assert(bytes_read == CF_BUF_SIZE || feof(in_file));
423
424                 fwrite(buf, 1, bytes_read, out_file);
425
426                 if (ferror(out_file))
427                         Error("Cannot write to file <%s>: %s", new_file, strerror(errno));
428         }
429
430         if (fclose(in_file)) {
431                 fclose(out_file);
432                 return -3;
433         }
434
435         if (fclose(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)
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                 unlink(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                 } else if (!(filenum = state_get_save_file(filename,desc,0))) {
499                         start_time();
500                         return 0;
501                 }
502         }
503                 
504         //      MK, 1/1/96
505         //      If not in multiplayer, do special secret level stuff.
506         //      If secret.sgc exists, then copy it to Nsecret.sgc (where N = filenum).
507         //      If it doesn't exist, then delete Nsecret.sgc
508         if (!secret_save && !(Game_mode & GM_MULTI)) {
509                 int     rval;
510                 char    temp_fname[32], fc;
511
512                 if (filenum != -1) {
513
514                         if (filenum >= 10)
515                                 fc = (filenum-10) + 'a';
516                         else
517                                 fc = '0' + filenum;
518
519                         #ifndef MACINTOSH
520                         sprintf(temp_fname, "%csecret.sgc", fc);
521                         #else
522                         sprintf(temp_fname, ":Players:%csecret.sgc", fc);
523                         #endif
524
525                         mprintf((0, "Trying to copy secret.sgc to %s.\n", temp_fname));
526
527                         if (file_exists(temp_fname)) {
528                                 mprintf((0, "Deleting file %s\n", temp_fname));
529                                 rval = unlink(temp_fname);
530                                 Assert(rval == 0);      //      Oops, error deleting file in temp_fname.
531                         }
532
533                         if (file_exists(SECRETC_FILENAME)) {
534                                 mprintf((0, "Copying secret.sgc to %s.\n", temp_fname));
535                                 rval = copy_file(SECRETC_FILENAME, temp_fname);
536                                 Assert(rval == 0);      //      Oops, error copying temp_fname to secret.sgc!
537                         }
538                 }
539         }
540
541         //      Save file we're going to save over in last slot and call "[autosave backup]"
542         if (!filename_override) {
543                 FILE    *tfp;
544         
545                 tfp = fopen( filename, "r+b" );
546
547                 if ( tfp ) {
548                         char    newname[128];
549
550                         #ifndef MACINTOSH
551                         sprintf( newname, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
552                         #else
553                         sprintf( newname, ":Players:%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
554                         #endif
555                         
556                         fseek( tfp, DESC_OFFSET, SEEK_SET);
557                         fwrite( "[autosave backup]", sizeof(char)*DESC_LENGTH, 1, tfp );
558                         fclose(tfp);
559                         unlink(newname);
560                         rename(filename, newname);
561                 }
562         }
563         
564         rval = state_save_all_sub(filename, desc, between_levels);
565
566         return rval;
567 }
568
569 extern  fix     Flash_effect, Time_flash_last_played;
570
571
572 int state_save_all_sub(char *filename, char *desc, int between_levels)
573 {
574         int i,j;
575         FILE * fp;
576         grs_canvas * cnv;
577         #ifdef POLY_ACC
578         grs_canvas cnv2,*save_cnv2;
579         #endif
580         ubyte *pal;
581
582         Assert(between_levels == 0);    //between levels save ripped out
583
584 /*      if ( Game_mode & GM_MULTI )     {
585                 {
586                 start_time();
587                 return 0;
588                 }
589         }*/
590
591         #if defined(MACINTOSH) && !defined(NDEBUG) 
592         if ( strncmp(filename, ":Players:", 9) )
593                 Int3();
594         #endif
595
596         fp = fopen( filename, "wb" );
597         if ( !fp ) {
598                 if ( !(Game_mode & GM_MULTI) )
599                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
600                 start_time();
601                 return 0;
602         }
603
604 //Save id
605         fwrite( dgss_id, sizeof(char)*4, 1, fp );
606
607 //Save version
608         i = STATE_VERSION;
609         fwrite( &i, sizeof(int), 1, fp );
610
611 //Save description
612         fwrite( desc, sizeof(char)*DESC_LENGTH, 1, fp );
613         
614 // Save the current screen shot...
615
616         cnv = gr_create_canvas( THUMBNAIL_W, THUMBNAIL_H );
617         if ( cnv )
618         {
619                 #ifdef WINDOWS
620                         dd_grs_canvas *cnv_save;
621                         cnv_save = dd_grd_curcanv;
622                 #else
623                         grs_canvas * cnv_save;
624                         cnv_save = grd_curcanv;
625                 #endif
626
627                 #ifndef MACINTOSH
628                 
629                         #if defined(POLY_ACC)
630                         
631                                         PA_DFX (pa_fool_to_backbuffer());
632                         
633                                         //for poly_acc, we render the frame to the normal render buffer
634                                         //so that this doesn't show, we create yet another canvas to save
635                                         //and restore what's on the render buffer
636                                         PA_DFX (pa_alpha_always());     
637                                         PA_DFX (pa_set_front_to_read());
638                                         gr_init_sub_canvas( &cnv2, &VR_render_buffer[0], 0, 0, THUMBNAIL_W, THUMBNAIL_H );
639                                         save_cnv2 = gr_create_canvas2(THUMBNAIL_W, THUMBNAIL_H, cnv2.cv_bitmap.bm_type);
640                                         gr_set_current_canvas( save_cnv2 );
641                                         PA_DFX (pa_set_front_to_read());
642                                         gr_bitmap(0,0,&cnv2.cv_bitmap);
643                                         gr_set_current_canvas( &cnv2 );
644                         #else
645                                         gr_set_current_canvas( cnv );
646                         #endif
647                         
648                                         PA_DFX (pa_set_backbuffer_current());
649                                         render_frame(0, 0);
650                                         PA_DFX (pa_alpha_always());  
651                         
652                         #if defined(POLY_ACC)
653                                         #ifndef MACINTOSH
654                                         screen_shot_pa(cnv,&cnv2);
655                                         #else
656                                         if ( PAEnabled )
657                                                 screen_shot_pa(cnv,&cnv2);
658                                         #endif
659                         #endif
660                         
661                                         pal = gr_palette;
662                         
663                                         fwrite( cnv->cv_bitmap.bm_data, THUMBNAIL_W*THUMBNAIL_H, 1, fp );
664                         
665                         #if defined(POLY_ACC)
666                                         PA_DFX (pa_alpha_always());     
667                                         PA_DFX (pa_set_frontbuffer_current());
668                                         PA_DFX(gr_bitmap(0,0,&save_cnv2->cv_bitmap));
669                                         PA_DFX (pa_set_backbuffer_current());
670                                         gr_bitmap(0,0,&save_cnv2->cv_bitmap);
671                                         gr_free_canvas(save_cnv2);
672                                         PA_DFX (pa_fool_to_offscreen());
673                         
674                         #endif
675                 
676                 #else   // macintosh stuff below
677                 {
678                         #if defined(POLY_ACC)
679                                 int     savePAEnabled = PAEnabled;
680                                 PAEnabled = false;
681                         #endif
682
683                         gr_set_current_canvas( cnv );
684                         render_frame(0, 0);
685                         pal = gr_palette;
686                         fwrite( cnv->cv_bitmap.bm_data, THUMBNAIL_W*THUMBNAIL_H, 1, fp );
687                         
688                         #if defined(POLY_ACC)
689                                 PAEnabled = savePAEnabled;
690                         #endif
691                 }       
692                 #endif  // end of ifndef macintosh
693                 
694                 
695                 WINDOS(
696                         dd_gr_set_current_canvas(cnv_save),
697                         gr_set_current_canvas(cnv_save)
698                 );
699                 gr_free_canvas( cnv );
700                 fwrite( pal, 3, 256, fp);
701         }
702         else
703         {
704                 ubyte color = 0;
705                 for ( i=0; i<THUMBNAIL_W*THUMBNAIL_H; i++ )
706                         fwrite( &color, sizeof(ubyte), 1, fp );         
707         } 
708
709 // Save the Between levels flag...
710         fwrite( &between_levels, sizeof(int), 1, fp );
711
712 // Save the mission info...
713         mprintf ((0,"HEY! Mission name is %s\n",Mission_list[Current_mission_num].filename));
714         fwrite( &Mission_list[Current_mission_num], sizeof(char)*9, 1, fp );
715
716 //Save level info
717         fwrite( &Current_level_num, sizeof(int), 1, fp );
718         fwrite( &Next_level_num, sizeof(int), 1, fp );
719
720 //Save GameTime
721         fwrite( &GameTime, sizeof(fix), 1, fp );
722
723 // If coop save, save all
724 #ifdef NETWORK
725    if (Game_mode & GM_MULTI_COOP)
726          {
727                 fwrite (&state_game_id,sizeof(int),1,fp);
728                 fwrite (&Netgame,sizeof(netgame_info),1,fp);            
729                 fwrite (&NetPlayers,sizeof(AllNetPlayers_info),1,fp);
730                 fwrite (&N_players,sizeof(int),1,fp);
731                 fwrite (&Player_num,sizeof(int),1,fp);
732                 for (i=0;i<N_players;i++)
733                  fwrite (&Players[i],sizeof(player),1,fp);
734
735 #ifdef RISKY_PROPOSITION
736            fwrite (&robot_controlled[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
737            fwrite (&robot_agitation[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
738            fwrite (&robot_controlled_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
739            fwrite (&robot_last_send_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
740            fwrite (&robot_last_message_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
741            fwrite (&robot_send_pending[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
742            fwrite (&robot_fired[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
743  
744       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
745                    fwrite (robot_fire_buf[i][0],18+3,1,fp);
746 #endif
747
748          }
749 #endif
750
751 //Save player info
752         fwrite( &Players[Player_num], sizeof(player), 1, fp );
753
754 // Save the current weapon info
755         fwrite( &Primary_weapon, sizeof(byte), 1, fp );
756         fwrite( &Secondary_weapon, sizeof(byte), 1, fp );
757
758 // Save the difficulty level
759         fwrite( &Difficulty_level, sizeof(int), 1, fp );
760 // Save cheats enabled
761         fwrite (&Cheats_enabled,sizeof(int),1,fp);
762
763         if ( !between_levels )  {
764
765         //Finish all morph objects
766                 for (i=0; i<=Highest_object_index; i++ )        {
767                         if ( (Objects[i].type != OBJ_NONE) && (Objects[i].render_type==RT_MORPH))       {
768                                 morph_data *md;
769                                 md = find_morph_data(&Objects[i]);
770                                 if (md) {                                       
771                                         md->obj->control_type = md->morph_save_control_type;
772                                         md->obj->movement_type = md->morph_save_movement_type;
773                                         md->obj->render_type = RT_POLYOBJ;
774                                         md->obj->mtype.phys_info = md->morph_save_phys_info;
775                                         md->obj = NULL;
776                                 } else {                                                //maybe loaded half-morphed from disk
777                                         Objects[i].flags |= OF_SHOULD_BE_DEAD;
778                                         Objects[i].render_type = RT_POLYOBJ;
779                                         Objects[i].control_type = CT_NONE;
780                                         Objects[i].movement_type = MT_NONE;
781                                 }
782                         }
783                 }
784         
785         //Save object info
786                 i = Highest_object_index+1;
787                 fwrite( &i, sizeof(int), 1, fp );
788                 fwrite( Objects, sizeof(object)*i, 1, fp );
789                 
790         //Save wall info
791                 i = Num_walls;
792                 fwrite( &i, sizeof(int), 1, fp );
793                 fwrite( Walls, sizeof(wall)*i, 1, fp );
794
795         //Save exploding wall info
796                 i = MAX_EXPLODING_WALLS;
797                 fwrite( &i, sizeof(int), 1, fp);
798                 fwrite( expl_wall_list, sizeof(*expl_wall_list), i, fp );
799         
800         //Save door info
801                 i = Num_open_doors;
802                 fwrite( &i, sizeof(int), 1, fp );
803                 fwrite( ActiveDoors, sizeof(active_door)*i, 1, fp );
804         
805         //Save cloaking wall info
806                 i = Num_cloaking_walls;
807                 fwrite( &i, sizeof(int), 1, fp );
808                 fwrite( CloakingWalls, sizeof(cloaking_wall), i, fp );
809         
810         //Save trigger info
811                 fwrite( &Num_triggers, sizeof(int), 1, fp );
812                 fwrite( Triggers, sizeof(trigger)*Num_triggers, 1, fp );
813         
814         //Save tmap info
815                 for (i=0; i<=Highest_segment_index; i++ )       {
816                         for (j=0; j<6; j++ )    {
817                                 fwrite( &Segments[i].sides[j].wall_num, sizeof(short), 1, fp );
818                                 fwrite( &Segments[i].sides[j].tmap_num, sizeof(short), 1, fp );
819                                 fwrite( &Segments[i].sides[j].tmap_num2, sizeof(short), 1, fp );
820                         }
821                 }
822         
823         // Save the fuelcen info
824                 fwrite( &Control_center_destroyed, sizeof(int), 1, fp );
825                 fwrite( &Countdown_timer, sizeof(int), 1, fp );
826                 fwrite( &Num_robot_centers, sizeof(int), 1, fp );
827                 fwrite( RobotCenters, sizeof(matcen_info)*Num_robot_centers, 1, fp );
828                 fwrite( &ControlCenterTriggers, sizeof(control_center_triggers), 1, fp );
829                 fwrite( &Num_fuelcenters, sizeof(int), 1, fp );
830                 fwrite( Station, sizeof(FuelCenter)*Num_fuelcenters, 1, fp );
831         
832         // Save the control cen info
833                 fwrite( &Control_center_been_hit, sizeof(int), 1, fp );
834                 fwrite( &Control_center_player_been_seen, sizeof(int), 1, fp );
835                 fwrite( &Control_center_next_fire_time, sizeof(int), 1, fp );
836                 fwrite( &Control_center_present, sizeof(int), 1, fp );
837                 fwrite( &Dead_controlcen_object_num, sizeof(int), 1, fp );
838         
839         // Save the AI state
840                 ai_save_state( fp );
841         
842         // Save the automap visited info
843                 fwrite( Automap_visited, sizeof(ubyte)*MAX_SEGMENTS, 1, fp );
844
845         }
846         fwrite( &state_game_id, sizeof(uint), 1, fp );
847         fwrite( &Laser_rapid_fire, sizeof(int), 1, fp );
848         fwrite( &Lunacy, sizeof(int), 1, fp );          //      Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
849         fwrite( &Lunacy, sizeof(int), 1, fp );
850
851         // Save automap marker info
852
853         fwrite(MarkerObject,sizeof(MarkerObject),1,fp);
854         fwrite(MarkerOwner,sizeof(MarkerOwner),1,fp);
855         fwrite(MarkerMessage,sizeof(MarkerMessage),1,fp);
856
857         fwrite (&Afterburner_charge,sizeof(fix),1,fp);
858
859         //save last was super information
860         fwrite(&Primary_last_was_super,sizeof(Primary_last_was_super),1,fp);
861         fwrite(&Secondary_last_was_super,sizeof(Secondary_last_was_super),1,fp);
862
863         //      Save flash effect stuff
864         fwrite( &Flash_effect, sizeof(int), 1, fp );
865         fwrite( &Time_flash_last_played, sizeof(int), 1, fp );
866         fwrite( &PaletteRedAdd, sizeof(int), 1, fp);
867         fwrite( &PaletteGreenAdd, sizeof(int), 1, fp);
868         fwrite( &PaletteBlueAdd, sizeof(int), 1, fp);
869
870         fwrite(Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS, fp);
871
872         fwrite(&First_secret_visit, sizeof(First_secret_visit), 1, fp);
873
874         fwrite(&Omega_charge, sizeof(Omega_charge), 1, fp);
875         
876         if ( ferror(fp) ) {
877                 if ( !(Game_mode & GM_MULTI) ) {
878                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
879                         fclose(fp);
880                         unlink(filename);
881                 }
882         } else  {
883                 fclose(fp);
884
885                 #ifdef MACINTOSH                // set the type and creator of the saved game file
886                 {
887                         FInfo finfo;
888                         OSErr err;
889                         Str255 pfilename;
890         
891                         strcpy(pfilename, filename);
892                         c2pstr(pfilename);
893                         err = HGetFInfo(0, 0, pfilename, &finfo);
894                         finfo.fdType = 'SVGM';
895                         finfo.fdCreator = 'DCT2';
896                         err = HSetFInfo(0, 0, pfilename, &finfo);
897                 }
898                 #endif
899         }
900         
901         start_time();
902
903         return 1;
904 }
905
906 //      -----------------------------------------------------------------------------------
907 //      Set the player's position from the globals Secret_return_segment and Secret_return_orient.
908 void set_pos_from_return_segment(void)
909 {
910         int     plobjnum = Players[Player_num].objnum;
911
912         compute_segment_center(&Objects[plobjnum].pos, &Segments[Secret_return_segment]);
913         obj_relink(plobjnum, Secret_return_segment);
914         reset_player_object();
915         Objects[plobjnum].orient = Secret_return_orient;
916 }
917
918 //      -----------------------------------------------------------------------------------
919 int state_restore_all(int in_game, int secret_restore, char *filename_override)
920 {
921         char filename[128];
922         int     filenum = -1;
923
924 #ifdef NETWORK
925         if ( Game_mode & GM_MULTI )     {
926 #ifdef MULTI_SAVE
927                         multi_initiate_restore_game();
928 #endif
929                 return 0;
930         }
931 #endif
932
933         if (in_game && (Current_level_num < 0) && (secret_restore == 0)) {
934                 HUD_init_message( "Can't restore in secret level!" );
935                 return 0;
936         }
937
938         if ( Newdemo_state == ND_STATE_RECORDING )
939                 newdemo_stop_recording();
940
941         if ( Newdemo_state != ND_STATE_NORMAL )
942                 return 0;
943
944         stop_time();
945
946         if (filename_override) {
947                 strcpy(filename, filename_override);
948                 filenum = NUM_SAVES+1;          //      So we don't trigger autosave
949         } else if (!(filenum = state_get_restore_file(filename, 0)))    {
950                 start_time();
951                 return 0;
952         }
953         
954         //      MK, 1/1/96
955         //      If not in multiplayer, do special secret level stuff.
956         //      If Nsecret.sgc (where N = filenum) exists, then copy it to secret.sgc.
957         //      If it doesn't exist, then delete secret.sgc
958         if (!secret_restore && !(Game_mode & GM_MULTI)) {
959                 int     rval;
960                 char    temp_fname[32], fc;
961
962                 if (filenum != -1) {
963                         if (filenum >= 10)
964                                 fc = (filenum-10) + 'a';
965                         else
966                                 fc = '0' + filenum;
967                         
968                         #ifndef MACINTOSH
969                         sprintf(temp_fname, "%csecret.sgc", fc);
970                         #else
971                         sprintf(temp_fname, ":Players:%csecret.sgc", fc);
972                         #endif
973
974                         mprintf((0, "Trying to copy %s to secret.sgc.\n", temp_fname));
975
976                         if (file_exists(temp_fname)) {
977                                 mprintf((0, "Copying %s to secret.sgc\n", temp_fname));
978                                 rval = copy_file(temp_fname, SECRETC_FILENAME);
979                                 Assert(rval == 0);      //      Oops, error copying temp_fname to secret.sgc!
980                         } else
981                                 unlink(SECRETC_FILENAME);
982                 }
983         }
984
985         //      Changed, 11/15/95, MK, don't to autosave if restoring from main menu.
986         if ((filenum != (NUM_SAVES+1)) && in_game) {
987                 char    temp_filename[128];
988                 mprintf((0, "Doing autosave, filenum = %i, != %i!\n", filenum, NUM_SAVES+1));
989                 #ifndef MACINTOSH
990                 sprintf( temp_filename, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
991                 #else
992                 sprintf( temp_filename, ":Players:%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
993                 #endif          
994                 state_save_all(!in_game, secret_restore, temp_filename);
995         }
996
997         if ( !secret_restore && in_game ) {
998                 int choice;
999                 choice =  nm_messagebox( NULL, 2, "Yes", "No", "Restore Game?" );
1000                 if ( choice != 0 )      {
1001                         start_time();
1002                         return 0;
1003                 }
1004         }
1005
1006         start_time();
1007
1008         return state_restore_all_sub(filename, 0, secret_restore);
1009 }
1010
1011 extern void init_player_stats_new_ship(void);
1012
1013 void ShowLevelIntro(int level_num);
1014
1015 extern void do_cloak_invul_secret_stuff(fix old_gametime);
1016 extern void copy_defaults_to_robot(object *objp);
1017
1018 int state_restore_all_sub(char *filename, int multi, int secret_restore)
1019 {
1020         int ObjectStartLocation;
1021         int version,i, j, segnum,found;
1022         object * obj;
1023         FILE *fp;
1024         int current_level, next_level;
1025         int between_levels;
1026         char mission[16];
1027         char desc[DESC_LENGTH+1];
1028         char id[5];
1029         char org_callsign[CALLSIGN_LEN+16];
1030         int nplayers;   //,playid[12],mynum;
1031         player restore_players[MAX_PLAYERS];
1032         fix     old_gametime = GameTime;
1033         
1034         #if defined(MACINTOSH) && !defined(NDEBUG) 
1035         if ( strncmp(filename, ":Players:", 9) )
1036                 Int3();
1037         #endif
1038
1039         fp = fopen( filename, "rb" );
1040         if ( !fp ) return 0;
1041
1042 //Read id
1043         fread( id, sizeof(char)*4, 1, fp );
1044         if ( memcmp( id, dgss_id, 4 )) {
1045                 fclose(fp);
1046                 return 0;
1047         }
1048
1049 //Read version
1050         fread( &version, sizeof(int), 1, fp );
1051         if (version < STATE_COMPATIBLE_VERSION) {
1052                 fclose(fp);
1053                 return 0;
1054         }
1055
1056 // Read description
1057         fread( desc, sizeof(char)*DESC_LENGTH, 1, fp );
1058
1059 // Skip the current screen shot...
1060         fseek( fp, THUMBNAIL_W*THUMBNAIL_H, SEEK_CUR );
1061
1062 // And now...skip the goddamn palette stuff that somebody forgot to add
1063         fseek( fp, 768, SEEK_CUR );
1064
1065 // Read the Between levels flag...
1066         fread( &between_levels, sizeof(int), 1, fp );
1067
1068         Assert(between_levels == 0);    //between levels save ripped out
1069
1070 // Read the mission info...
1071         fread( mission, sizeof(char)*9, 1, fp );
1072         mprintf ((0,"Missionname to load = %s\n",mission));
1073
1074         if (!load_mission_by_name( mission ))   {
1075                 nm_messagebox( NULL, 1, "Ok", "Error!\nUnable to load mission\n'%s'\n", mission );
1076                 fclose(fp);
1077                 return 0;
1078         }
1079
1080 //Read level info
1081         fread( &current_level, sizeof(int), 1, fp );
1082         fread( &next_level, sizeof(int), 1, fp );
1083
1084 //Restore GameTime
1085         fread( &GameTime, sizeof(fix), 1, fp );
1086
1087 // Start new game....
1088         if (!multi)     {
1089                 Game_mode = GM_NORMAL;
1090                 Function_mode = FMODE_GAME;
1091 #ifdef NETWORK
1092                 change_playernum_to(0);
1093 #endif
1094                 strcpy( org_callsign, Players[0].callsign );
1095                 N_players = 1;
1096                 if (!secret_restore) {
1097                         InitPlayerObject();                             //make sure player's object set up
1098                         init_player_stats_game();               //clear all stats
1099                 }
1100         } else {
1101                 strcpy( org_callsign, Players[Player_num].callsign );
1102         }
1103
1104 #ifdef NETWORK
1105    if (Game_mode & GM_MULTI)
1106          {
1107                 fread (&state_game_id,sizeof(int),1,fp);
1108                 fread (&Netgame,sizeof(netgame_info),1,fp);             
1109                 fread (&NetPlayers,sizeof(AllNetPlayers_info),1,fp);
1110                 fread (&nplayers,sizeof(N_players),1,fp);
1111                 fread (&Player_num,sizeof(Player_num),1,fp);
1112                 for (i=0;i<nplayers;i++)
1113                  fread (&restore_players[i],sizeof(player),1,fp);
1114 #ifdef RISKY_PROPOSITION
1115            fread (&robot_controlled[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1116            fread (&robot_agitation[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1117            fread (&robot_controlled_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1118            fread (&robot_last_send_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1119            fread (&robot_last_message_time[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1120            fread (&robot_send_pending[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1121            fread (&robot_fired[0],4*MAX_ROBOTS_CONTROLLED,1,fp);
1122  
1123       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
1124                    fread (&robot_fire_buf[i][0],21,1,fp);
1125 #endif
1126
1127            for (i=0;i<nplayers;i++)
1128                  {
1129                   found=0;
1130                   for (j=0;j<nplayers;j++)
1131                          {
1132            if ((!strcmp (restore_players[i].callsign,Players[j].callsign)) && Players[j].connected==1)
1133                                  found=1;
1134                          }
1135                   restore_players[i].connected=found;
1136             }
1137                 memcpy (&Players,&restore_players,sizeof(player)*nplayers);
1138                 N_players=nplayers;
1139                
1140       if (network_i_am_master())
1141                  {
1142                   for (i=0;i<N_players;i++)
1143                         {
1144                          if (i==Player_num)
1145                                 continue;
1146                  Players[i].connected=0;        
1147                         }
1148                  }
1149                  
1150                 //Viewer = ConsoleObject = &Objects[Players[Player_num].objnum];
1151          }
1152
1153 #endif
1154
1155 //Read player info
1156
1157         {
1158                 StartNewLevelSub(current_level, 1, secret_restore);
1159
1160                 if (secret_restore) {
1161                         player  dummy_player;
1162
1163                         fread( &dummy_player, sizeof(player), 1, fp );
1164                         if (secret_restore == 1) {              //      This means he didn't die, so he keeps what he got in the secret level.
1165                                 Players[Player_num].level = dummy_player.level;
1166                                 Players[Player_num].last_score = dummy_player.last_score;
1167                                 Players[Player_num].time_level = dummy_player.time_level;
1168
1169                                 Players[Player_num].num_robots_level = dummy_player.num_robots_level;
1170                                 Players[Player_num].num_robots_total = dummy_player.num_robots_total;
1171                                 Players[Player_num].hostages_rescued_total = dummy_player.hostages_rescued_total;
1172                                 Players[Player_num].hostages_total = dummy_player.hostages_total;
1173                                 Players[Player_num].hostages_on_board = dummy_player.hostages_on_board;
1174                                 Players[Player_num].hostages_level = dummy_player.hostages_level;
1175                                 Players[Player_num].homing_object_dist = dummy_player.homing_object_dist;
1176                                 Players[Player_num].hours_level = dummy_player.hours_level;
1177                                 Players[Player_num].hours_total = dummy_player.hours_total;
1178                                 do_cloak_invul_secret_stuff(old_gametime);
1179                         } else {
1180                                 Players[Player_num] = dummy_player;
1181                         }
1182                 } else {
1183                         fread( &Players[Player_num], sizeof(player), 1, fp );
1184                 }
1185         }
1186         strcpy( Players[Player_num].callsign, org_callsign );
1187
1188 // Set the right level
1189         if ( between_levels )
1190                 Players[Player_num].level = next_level;
1191
1192 // Restore the weapon states
1193         fread( &Primary_weapon, sizeof(byte), 1, fp );
1194         fread( &Secondary_weapon, sizeof(byte), 1, fp );
1195
1196         select_weapon(Primary_weapon, 0, 0, 0);
1197         select_weapon(Secondary_weapon, 1, 0, 0);
1198
1199 // Restore the difficulty level
1200         fread( &Difficulty_level, sizeof(int), 1, fp );
1201
1202 // Restore the cheats enabled flag
1203  
1204    fread (&Cheats_enabled,sizeof(int),1,fp);
1205
1206         if ( !between_levels )  {
1207                 Do_appearance_effect = 0;                       // Don't do this for middle o' game stuff.
1208
1209                 ObjectStartLocation = ftell( fp );
1210                 //Clear out all the objects from the lvl file
1211                 for (segnum=0; segnum <= Highest_segment_index; segnum++)
1212                         Segments[segnum].objects = -1;
1213                 reset_objects(1);
1214         
1215                 //Read objects, and pop 'em into their respective segments.
1216                 fread( &i, sizeof(int), 1, fp );
1217                 Highest_object_index = i-1;
1218                 fread( Objects, sizeof(object)*i, 1, fp );
1219         
1220                 Object_next_signature = 0;
1221                 for (i=0; i<=Highest_object_index; i++ )        {
1222                         obj = &Objects[i];
1223                         obj->rtype.pobj_info.alt_textures = -1;
1224                         segnum = obj->segnum;
1225                         obj->next = obj->prev = obj->segnum = -1;
1226                         if ( obj->type != OBJ_NONE )    {
1227                                 obj_link(i,segnum);
1228                                 if ( obj->signature > Object_next_signature )
1229                                         Object_next_signature = obj->signature;
1230                         }
1231
1232                         //look for, and fix, boss with bogus shields
1233                         if (obj->type == OBJ_ROBOT && Robot_info[obj->id].boss_flag) {
1234                                 fix save_shields = obj->shields;
1235
1236                                 copy_defaults_to_robot(obj);            //calculate starting shields
1237
1238                                 //if in valid range, use loaded shield value
1239                                 if (save_shields > 0 && save_shields <= obj->shields)
1240                                         obj->shields = save_shields;
1241                                 else
1242                                         obj->shields /= 2;  //give player a break
1243                         }
1244
1245                 }       
1246                 special_reset_objects();
1247                 Object_next_signature++;
1248         
1249                 //      1 = Didn't die on secret level.
1250                 //      2 = Died on secret level.
1251                 if (secret_restore && (Current_level_num >= 0)) {
1252                         set_pos_from_return_segment();
1253                         if (secret_restore == 2)
1254                                 init_player_stats_new_ship();
1255                 }
1256
1257                 //Restore wall info
1258                 fread( &i, sizeof(int), 1, fp );
1259                 Num_walls = i;
1260                 fread( Walls, sizeof(wall)*Num_walls, 1, fp );
1261
1262                 //now that we have the walls, check if any sounds are linked to
1263                 //walls that are now open
1264                 for (i=0;i<Num_walls;i++) {
1265                         if (Walls[i].type == WALL_OPEN)
1266                                 digi_kill_sound_linked_to_segment(Walls[i].segnum,Walls[i].sidenum,-1); //-1 means kill any sound
1267                 }
1268
1269                 //Restore exploding wall info
1270                 if (version >= 10) {
1271                         fread( &i, sizeof(int), 1, fp );
1272                         fread( expl_wall_list, sizeof(*expl_wall_list), i, fp );
1273                 }
1274
1275                 //Restore door info
1276                 fread( &i, sizeof(int), 1, fp );
1277                 Num_open_doors = i;
1278                 fread( ActiveDoors, sizeof(active_door)*Num_open_doors, 1, fp );
1279         
1280                 if (version >= 14) {            //Restore cloaking wall info
1281                         fread( &i, sizeof(int), 1, fp );
1282                         Num_cloaking_walls = i;
1283                         fread( CloakingWalls, sizeof(cloaking_wall), Num_cloaking_walls, fp );
1284                 }
1285         
1286                 //Restore trigger info
1287                 fread( &Num_triggers, sizeof(int), 1, fp );
1288                 fread( Triggers, sizeof(trigger)*Num_triggers, 1, fp );
1289         
1290                 //Restore tmap info
1291                 for (i=0; i<=Highest_segment_index; i++ )       {
1292                         for (j=0; j<6; j++ )    {
1293                                 fread( &Segments[i].sides[j].wall_num, sizeof(short), 1, fp );
1294                                 fread( &Segments[i].sides[j].tmap_num, sizeof(short), 1, fp );
1295                                 fread( &Segments[i].sides[j].tmap_num2, sizeof(short), 1, fp );
1296                         }
1297                 }
1298         
1299                 //Restore the fuelcen info
1300                 fread( &Control_center_destroyed, sizeof(int), 1, fp );
1301                 fread( &Countdown_timer, sizeof(int), 1, fp );
1302                 fread( &Num_robot_centers, sizeof(int), 1, fp );
1303                 fread( RobotCenters, sizeof(matcen_info)*Num_robot_centers, 1, fp );
1304                 fread( &ControlCenterTriggers, sizeof(control_center_triggers), 1, fp );
1305                 fread( &Num_fuelcenters, sizeof(int), 1, fp );
1306                 fread( Station, sizeof(FuelCenter)*Num_fuelcenters, 1, fp );
1307         
1308                 // Restore the control cen info
1309                 fread( &Control_center_been_hit, sizeof(int), 1, fp );
1310                 fread( &Control_center_player_been_seen, sizeof(int), 1, fp );
1311                 fread( &Control_center_next_fire_time, sizeof(int), 1, fp );
1312                 fread( &Control_center_present, sizeof(int), 1, fp );
1313                 fread( &Dead_controlcen_object_num, sizeof(int), 1, fp );
1314         
1315                 // Restore the AI state
1316                 ai_restore_state( fp, version );
1317         
1318                 // Restore the automap visited info
1319                 fread( Automap_visited, sizeof(ubyte)*MAX_SEGMENTS, 1, fp );
1320
1321                 //      Restore hacked up weapon system stuff.
1322                 Fusion_next_sound_time = GameTime;
1323                 Auto_fire_fusion_cannon_time = 0;
1324                 Next_laser_fire_time = GameTime;
1325                 Next_missile_fire_time = GameTime;
1326                 Last_laser_fired_time = GameTime;
1327
1328         }
1329         state_game_id = 0;
1330
1331         if ( version >= 7 )     {
1332                 fread( &state_game_id, sizeof(uint), 1, fp );
1333                 fread( &Laser_rapid_fire, sizeof(int), 1, fp );
1334                 fread( &Lunacy, sizeof(int), 1, fp );           //      Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
1335                 fread( &Lunacy, sizeof(int), 1, fp );
1336                 if ( Lunacy )
1337                         do_lunacy_on();
1338         }
1339
1340         if (version >= 17) {
1341                 fread(MarkerObject,sizeof(MarkerObject),1,fp);
1342                 fread(MarkerOwner,sizeof(MarkerOwner),1,fp);
1343                 fread(MarkerMessage,sizeof(MarkerMessage),1,fp);
1344         }
1345         else {
1346                 int num,dummy;
1347
1348                 // skip dummy info
1349
1350                 fread (&num,sizeof(int),1,fp);                  //was NumOfMarkers
1351                 fread (&dummy,sizeof(int),1,fp);                        //was CurMarker
1352
1353                 fseek( fp, num * (sizeof(vms_vector) + 40), SEEK_CUR );
1354
1355                 for (num=0;num<NUM_MARKERS;num++)
1356                         MarkerObject[num] = -1;
1357         }
1358
1359         if (version>=11) {
1360                 if (secret_restore != 1)
1361                         fread (&Afterburner_charge,sizeof(fix),1,fp);
1362                 else {
1363                         fix     dummy_fix;
1364                         fread (&dummy_fix,sizeof(fix),1,fp);
1365                 }
1366         }
1367         if (version>=12) {
1368                 //read last was super information
1369                 fread(&Primary_last_was_super,sizeof(Primary_last_was_super),1,fp);
1370                 fread(&Secondary_last_was_super,sizeof(Secondary_last_was_super),1,fp);
1371         }
1372
1373         if (version >= 12) {
1374                 fread( &Flash_effect, sizeof(int), 1, fp );
1375                 fread( &Time_flash_last_played, sizeof(int), 1, fp );
1376                 fread( &PaletteRedAdd, sizeof(int), 1, fp);
1377                 fread( &PaletteGreenAdd, sizeof(int), 1, fp);
1378                 fread( &PaletteBlueAdd, sizeof(int), 1, fp);
1379         } else {
1380                 Flash_effect = 0;
1381                 Time_flash_last_played = 0;
1382                 PaletteRedAdd = 0;
1383                 PaletteGreenAdd = 0;
1384                 PaletteBlueAdd = 0;
1385         }
1386
1387         //      Load Light_subtracted
1388         if (version >= 16) {
1389                 fread(Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS, fp);
1390                 apply_all_changed_light();
1391                 compute_all_static_light();     //      set static_light field in segment struct.  See note at that function.
1392         } else {
1393                 int     i;
1394                 for (i=0; i<=Highest_segment_index; i++)
1395                         Light_subtracted[i] = 0;
1396         }
1397    
1398         if (!secret_restore) {
1399                 if (version >= 20) {
1400                         fread(&First_secret_visit, sizeof(First_secret_visit), 1, fp);
1401                         mprintf((0, "File: [%s] Read First_secret_visit: New value = %i\n", filename, First_secret_visit));
1402                 } else
1403                         First_secret_visit = 1;
1404         } else
1405                 First_secret_visit = 0;
1406
1407         if (version >= 22)
1408         {
1409                 if (secret_restore != 1)
1410                         fread (&Omega_charge,sizeof(fix),1,fp);
1411                 else {
1412                         fix     dummy_fix;
1413                         fread (&dummy_fix,sizeof(fix),1,fp);
1414                 }
1415         }
1416
1417         fclose(fp);
1418  
1419 #ifdef NETWORK
1420    if (Game_mode & GM_MULTI)   // Get rid of ships that aren't 
1421          {                                                                       // connected in the restored game
1422                 for (i=0;i<nplayers;i++)
1423                  {
1424                   mprintf ((0,"Testing %s = %d\n",Players[i].callsign,Players[i].connected));
1425                   if (Players[i].connected!=1)
1426                    {
1427                     network_disconnect_player (i);
1428                create_player_appearance_effect(&Objects[Players[i].objnum]);
1429                          mprintf ((0,"Killing player ship %s!\n",Players[i].callsign));
1430               }
1431                  }
1432                         
1433          }
1434 #endif
1435
1436 // Load in bitmaps, etc..
1437 //!!    piggy_load_level_data();        //already done by StartNewLevelSub()
1438
1439         return 1;
1440 }
1441
1442 //      When loading a saved game, segp->static_light is bogus.
1443 //      This is because apply_all_changed_light, which is supposed to properly update this value,
1444 //      cannot do so because it needs the original light cast from a light which is no longer there.
1445 //      That is, a light has been blown out, so the texture remaining casts 0 light, but the static light
1446 //      which is present in the static_light field contains the light cast from that light.
1447 void compute_all_static_light(void)
1448 {
1449         int     i, j, k;
1450
1451         for (i=0; i<=Highest_segment_index; i++) {
1452                 fix     total_light;
1453                 segment *segp;
1454
1455                 segp = &Segments[i];
1456                 total_light = 0;
1457
1458                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++) {
1459                         side    *sidep;
1460
1461                         sidep = &segp->sides[j];
1462
1463                         for (k=0; k<4; k++)
1464                                 total_light += sidep->uvls[k].l;
1465                 }
1466
1467                 Segment2s[i].static_light = total_light/(MAX_SIDES_PER_SEGMENT*4);
1468         }
1469
1470 }
1471
1472
1473 int state_get_game_id(char *filename)
1474 {
1475         int version;
1476         FILE *fp;
1477         int between_levels;
1478         char mission[16];
1479         char desc[DESC_LENGTH+1];
1480         char id[5];
1481         int dumbint;
1482
1483 mprintf((0, "Restoring multigame from [%s]\n", filename));
1484
1485         fp = fopen( filename, "rb" );
1486         if ( !fp ) return 0;
1487
1488 //Read id
1489         fread( id, sizeof(char)*4, 1, fp );
1490         if ( memcmp( id, dgss_id, 4 )) {
1491                 fclose(fp);
1492                 return 0;
1493         }
1494
1495 //Read version
1496         fread( &version, sizeof(int), 1, fp );
1497         if (version < STATE_COMPATIBLE_VERSION) {
1498                 fclose(fp);
1499                 return 0;
1500         }
1501
1502 // Read description
1503         fread( desc, sizeof(char)*DESC_LENGTH, 1, fp );
1504
1505 // Skip the current screen shot...
1506         fseek( fp, THUMBNAIL_W*THUMBNAIL_H, SEEK_CUR );
1507
1508 // And now...skip the palette stuff that somebody forgot to add
1509         fseek( fp, 768, SEEK_CUR );
1510
1511 // Read the Between levels flag...
1512         fread( &between_levels, sizeof(int), 1, fp );
1513
1514         Assert(between_levels == 0);    //between levels save ripped out
1515
1516 // Read the mission info...
1517         fread( mission, sizeof(char)*9, 1, fp );
1518 //Read level info
1519         fread( &dumbint, sizeof(int), 1, fp );
1520         fread( &dumbint, sizeof(int), 1, fp );
1521
1522 //Restore GameTime
1523         fread( &dumbint, sizeof(fix), 1, fp );
1524
1525         fread (&state_game_id,sizeof(int),1,fp);
1526
1527         return (state_game_id);
1528  }
1529
1530 #if defined(POLY_ACC)
1531 //void screen_shot_pa(ubyte *dst,ushort *src)
1532 //{
1533 //    //ushort *src = pa_get_buffer_address(0),
1534 //    ushort *s;
1535 //    fix u, v, du, dv;
1536 //    int ui, w, h;
1537 //
1538 //    pa_flush();
1539 //
1540 //    du = (640.0 / (float)THUMBNAIL_W) * 65536.0;
1541 //    dv = (480.0 / (float)THUMBNAIL_H) * 65536.0;
1542 //
1543 //    for(v = h = 0; h != THUMBNAIL_H; ++h)
1544 //    {
1545 //        s = src + f2i(v) * 640;
1546 //        v += dv;
1547 //        for(u = w = 0; w != THUMBNAIL_W; ++w)
1548 //        {
1549 //            ui = f2i(u);
1550 //            *dst++ = gr_find_closest_color((s[ui] >> 9) & 0x3e, (s[ui] >> 4) & 0x3e, (s[ui] << 1) & 0x3e);
1551 //            u += du;
1552 //        }
1553 //    }
1554 //}
1555
1556 void screen_shot_pa(grs_canvas *dcanv,grs_canvas *scanv)
1557 {
1558 #if !defined(MACINTOSH)
1559         ubyte *dst;
1560         ushort *src;
1561         int x,y;
1562
1563         Assert(scanv->cv_w == dcanv->cv_w && scanv->cv_h == dcanv->cv_h);
1564
1565         pa_flush();
1566
1567         src = (ushort *) scanv->cv_bitmap.bm_data;
1568         dst = dcanv->cv_bitmap.bm_data;
1569
1570         #ifdef PA_3DFX_VOODOO
1571    src=(ushort *)pa_set_back_to_read();
1572         #endif
1573
1574         for (y=0; y<scanv->cv_h; y++) {
1575                 for (x=0; x<scanv->cv_w; x++) {
1576                         #ifdef PA_3DFX_VOODOO
1577                         *dst++ = gr_find_closest_color((*src >> 10) & 0x3e, (*src >> 5) & 0x3f, (*src << 1) & 0x3e);
1578                         #else
1579                         *dst++ = gr_find_closest_color((*src >> 9) & 0x3e, (*src >> 4) & 0x3e, (*src << 1) & 0x3e);
1580                         #endif
1581
1582                         src++;
1583                 }
1584                 src = (ushort *) (((ubyte *) src) + (scanv->cv_bitmap.bm_rowsize - (scanv->cv_bitmap.bm_w*PA_BPP)));
1585                 dst += dcanv->cv_bitmap.bm_rowsize - dcanv->cv_bitmap.bm_w;
1586         }
1587         #ifdef PA_3DFX_VOODOO
1588         pa_set_front_to_read();
1589         #endif
1590 #endif
1591 }
1592 #endif
1593