c48a93590023ed44bbf3bb1989840a99df103e22
[btb/d2x.git] / main / state.c
1 /* $Id: state.c,v 1.13 2004-05-19 02:47:49 btb 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  * Old Log:
20  * Revision 1.7  1995/10/31  10:18:25  allender
21  * shareware stuff
22  *
23  * Revision 1.6  1995/10/21  22:25:45  allender
24  * put in creator code and file type for saved games.
25  * put save games in players folder
26  *
27  * Revision 1.5  1995/10/20  00:51:21  allender
28  * close boxes and proper mouse support on save game stuff
29  *
30  * Revision 1.4  1995/10/17  13:19:02  allender
31  * close boxes for load and save game
32  *
33  * Revision 1.3  1995/09/18  08:09:15  allender
34  * made larger thumbnail and handled NULL gr_bitmap pointers
35  * better
36  *
37  * Revision 1.2  1995/08/14  14:36:12  allender
38  * change transparency to 0
39  *
40  * Revision 1.1  1995/05/16  15:31:12  allender
41  * Initial revision
42  *
43  * Revision 2.14  1995/05/26  16:16:10  john
44  * Split SATURN into define's for requiring cd, using cd, etc.
45  * Also started adding all the Rockwell stuff.
46  *
47  * Revision 2.13  1995/04/06  15:12:20  john
48  * Fixed bug with lunacy not working.
49  *
50  * Revision 2.12  1995/04/04  13:33:05  john
51  * Removed multiplayer save.
52  *
53  * Revision 2.11  1995/03/31  13:42:10  john
54  * Made saved games from the bogus saturn version read in
55  * correctly.
56  *
57  * Revision 2.10  1995/03/31  12:45:28  john
58  * Fixed bug with previous.
59  *
60  * Revision 2.9  1995/03/31  12:24:40  john
61  * I had changed alt_textures from a pointer to a byte. This hosed old
62  * saved games, so I restored it to an int.
63  *
64  * Revision 2.8  1995/03/28  11:22:47  john
65  * Added cheats to save file. Changed lunacy text.
66  *
67  * Revision 2.7  1995/03/27  21:41:03  john
68  * Added code to verify that the proper multi save file
69  * is used when restoring a network game.
70  *
71  * Revision 2.6  1995/03/27  18:04:18  john
72  * Made multi save/restore require the -multisave command line arg.
73  *
74  * Revision 2.5  1995/03/27  17:01:52  john
75  * Made deafult choice work better.
76  *
77  * Revision 2.4  1995/03/27  15:49:44  john
78  * Added slots to save games.
79  *
80  * Revision 2.3  1995/03/27  12:59:19  john
81  * Initial version of multiplayer save games.
82  *
83  * Revision 2.2  1995/03/24  13:11:35  john
84  * Added save game during briefing screens.
85  *
86  * Revision 2.1  1995/03/21  14:38:36  john
87  * Ifdef'd out the NETWORK code.
88  *
89  * Revision 2.0  1995/02/27  11:27:00  john
90  * New version 2.0, which has no anonymous unions, builds with
91  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
92  *
93  * Revision 1.43  1995/02/22  14:32:41  allender
94  * remove anonymous unions from object structure
95  *
96  * Revision 1.42  1995/02/13  20:34:33  john
97  * Lintized
98  *
99  * Revision 1.41  1995/02/13  10:37:30  john
100  * Saved Buggin' cheat mode to save file.
101  *
102  * Revision 1.40  1995/02/09  10:24:25  john
103  * *** empty log message ***
104  *
105  * Revision 1.39  1995/02/09  10:22:20  john
106  * Fixed bug with callsign getting trashed if you copy somebody else
107  * 's save game file into your directory.
108  *
109  * Revision 1.38  1995/02/08  21:01:27  john
110  * Closed state file around the code that shows briefing screens so that the
111  * code works on machines with clean boot with only 5 file handles.
112  *
113  * Revision 1.37  1995/02/07  14:02:33  john
114  * Added code to verify game restore.
115  *
116  * Revision 1.36  1995/02/07  11:07:43  john
117  * Added hooks for confirm on game state restore.
118  *
119  * Revision 1.35  1995/02/03  11:27:36  john
120  * Made inbetween level save's thumbnail's have correct aspect.
121  *
122  * Revision 1.34  1995/02/03  10:58:43  john
123  * Added code to save shareware style saved games into new format...
124  * Also, made new player file format not have the saved game array in it.
125  *
126  * Revision 1.33  1995/02/02  19:40:52  john
127  * Added 10 save game slots.
128  *
129  * Revision 1.32  1995/02/02  12:23:20  john
130  * Made between level saves have picture.
131  *
132  * Revision 1.31  1995/01/31  11:21:43  john
133  * Added code for fixed with menus.
134  *
135  * Revision 1.30  1995/01/29  21:37:29  mike
136  * initialize variables on game load so you don't drain your energy when you fire.
137  *
138  * Revision 1.29  1995/01/29  13:47:58  mike
139  * Restore some variables on game load (in game).
140  *
141  * Revision 1.28  1995/01/26  10:46:57  john
142  * Fixed bug with state names getting hosed.
143  *
144  * Revision 1.27  1995/01/26  09:51:23  john
145  * Fixed bug with game descriptions getting hosed.
146  *
147  * Revision 1.26  1995/01/25  16:35:49  john
148  * Made so that when you hit enter during
149  * game save, -empty- goes away.
150  *
151  * Revision 1.25  1995/01/25  15:01:39  john
152  * Upped the save file version.
153  *
154  * Revision 1.24  1995/01/24  20:35:35  john
155  * *** empty log message ***
156  *
157  * Revision 1.23  1995/01/24  20:34:24  john
158  * Fixed bug with player stats not being set right for in
159  * between level saves.
160  *
161  * Revision 1.22  1995/01/23  10:39:03  john
162  * Added mission stuff to game saves.
163  *
164  * Revision 1.21  1995/01/22  16:07:12  mike
165  * localization.
166  *
167  * Revision 1.20  1995/01/22  15:58:32  mike
168  * localization
169  *
170  * Revision 1.19  1995/01/20  11:04:40  john
171  * Upped state save version.
172  *
173  * Revision 1.18  1995/01/19  17:00:44  john
174  * Made save game work between levels.
175  *
176  * Revision 1.17  1995/01/17  14:27:33  john
177  * *** empty log message ***
178  *
179  * Revision 1.16  1995/01/17  13:36:37  john
180  * Moved pig loading into StartNewLevelSub.
181  *
182  * Revision 1.15  1995/01/16  16:53:38  john
183  * Added code to save cheat state during save game.
184  *
185  * Revision 1.14  1995/01/15  16:55:22  john
186  * Improved mine texture parsing.
187  *
188  * Revision 1.13  1995/01/12  10:45:15  john
189  * Added difficulty level to save/restore game.
190  *
191  * Revision 1.12  1995/01/05  15:46:55  john
192  * Made weapons not rearm when starting a saved game.
193  *
194  * Revision 1.11  1995/01/05  11:51:45  john
195  * Added better Abort game menu.
196  * Made save state return success or nopt.
197  *
198  * Revision 1.10  1995/01/05  11:34:51  john
199  * Took out endlevel save stuff for registered.
200  *
201  * Revision 1.9  1995/01/04  18:19:52  john
202  * Added automap visited list saving.
203  *
204  * Revision 1.8  1995/01/04  17:29:56  john
205  * Made save/restore ALT+F?. Also made them not work
206  * in network mode, and if recording a demo, will
207  * quit recording.
208  *
209  * Revision 1.7  1995/01/04  13:18:31  john
210  * Added cool 6 game save.
211  *
212  * Revision 1.6  1995/01/03  20:38:46  john
213  * Saved morph objects.
214  *
215  * Revision 1.5  1995/01/03  20:19:29  john
216  * Pretty good working version of game save.
217  *
218  * Revision 1.4  1995/01/03  14:18:18  matt
219  * ifdefs added to compile code add.  Added by Mike, I think.
220  *
221  * Revision 1.3  1994/12/29  18:40:19  john
222  * Initial version.
223  *
224  * Revision 1.2  1994/12/29  15:26:40  john
225  * Put in hooks for saving/restoring game state.
226  *
227  * Revision 1.1  1994/12/29  15:16:02  john
228  * Initial revision
229  *
230  *
231  */
232
233 #ifdef HAVE_CONFIG_H
234 #include <conf.h>
235 #endif
236
237 #ifdef WINDOWS
238 #include "desw.h"
239 #endif
240
241 #include <stdio.h>
242 #include <stdlib.h>
243 #include <math.h>
244 #include <string.h>
245 #ifndef _MSC_VER
246 #include <unistd.h>
247 #endif
248 #ifndef _WIN32_WCE
249 #include <errno.h>
250 #endif
251 #ifdef MACINTOSH
252 #include <Files.h>
253 #endif
254
255 #ifdef OGL
256 # ifdef _MSC_VER
257 #  include <windows.h>
258 # endif
259 #if defined(__APPLE__) && defined(__MACH__)
260 #include <OpenGL/gl.h>
261 #else
262 #include <GL/gl.h>
263 #endif
264 #endif
265
266 #include "pstypes.h"
267 #include "pa_enabl.h"                   //$$POLY_ACC
268 #include "mono.h"
269 #include "inferno.h"
270 #include "segment.h"
271 #include "textures.h"
272 #include "wall.h"
273 #include "object.h"
274 #include "digi.h"
275 #include "gamemine.h"
276 #include "error.h"
277 #include "gameseg.h"
278 #include "menu.h"
279 #include "switch.h"
280 #include "game.h"
281 #include "screens.h"
282 #include "newmenu.h"
283 #include "cfile.h"
284 #include "fuelcen.h"
285 #include "hash.h"
286 #include "key.h"
287 #include "piggy.h"
288 #include "player.h"
289 #include "cntrlcen.h"
290 #include "morph.h"
291 #include "weapon.h"
292 #include "render.h"
293 #include "gameseq.h"
294 #include "gauges.h"
295 #include "newdemo.h"
296 #include "automap.h"
297 #include "piggy.h"
298 #include "paging.h"
299 #include "titles.h"
300 #include "text.h"
301 #include "mission.h"
302 #include "pcx.h"
303 #include "u_mem.h"
304 #ifdef NETWORK
305 #include "network.h"
306 #endif
307 #include "args.h"
308 #include "ai.h"
309 #include "fireball.h"
310 #include "controls.h"
311 #include "laser.h"
312 #include "multibot.h"
313 #include "state.h"
314
315 #if defined(POLY_ACC)
316 #include "poly_acc.h"
317 #endif
318
319 #ifdef OGL
320 #include "gr.h"
321 #endif
322
323 #define STATE_VERSION 22
324 #define STATE_COMPATIBLE_VERSION 20
325 // 0 - Put DGSS (Descent Game State Save) id at tof.
326 // 1 - Added Difficulty level save
327 // 2 - Added Cheats_enabled flag
328 // 3 - Added between levels save.
329 // 4 - Added mission support
330 // 5 - Mike changed ai and object structure.
331 // 6 - Added buggin' cheat save
332 // 7 - Added other cheat saves and game_id.
333 // 8 - Added AI stuff for escort and thief.
334 // 9 - Save palette with screen shot
335 // 12- Saved last_was_super array
336 // 13- Saved palette flash stuff
337 // 14- Save cloaking wall stuff
338 // 15- Save additional ai info
339 // 16- Save Light_subtracted
340 // 17- New marker save
341 // 18- Took out saving of old cheat status
342 // 19- Saved cheats_enabled flag
343 // 20- First_secret_visit
344 // 22- Omega_charge
345
346 #define NUM_SAVES 9
347 #define THUMBNAIL_W 100
348 #define THUMBNAIL_H 50
349 #define DESC_LENGTH 20
350
351 extern void multi_initiate_save_game();
352 extern void multi_initiate_restore_game();
353 extern void apply_all_changed_light(void);
354
355 extern int Do_appearance_effect;
356 extern fix Fusion_next_sound_time;
357
358 extern int Laser_rapid_fire;
359 extern int Physics_cheat_flag;
360 extern int Lunacy;
361 extern void do_lunacy_on(void);
362 extern void do_lunacy_off(void);
363 extern int First_secret_visit;
364
365 int sc_last_item= 0;
366 grs_bitmap *sc_bmp[NUM_SAVES+1];
367
368 char dgss_id[4] = "DGSS";
369
370 int state_default_item = 0;
371
372 uint state_game_id;
373
374 extern int robot_controlled[MAX_ROBOTS_CONTROLLED];
375 extern int robot_agitation[MAX_ROBOTS_CONTROLLED];
376 extern fix robot_controlled_time[MAX_ROBOTS_CONTROLLED];
377 extern fix robot_last_send_time[MAX_ROBOTS_CONTROLLED];
378 extern fix robot_last_message_time[MAX_ROBOTS_CONTROLLED];
379 extern int robot_send_pending[MAX_ROBOTS_CONTROLLED];
380 extern int robot_fired[MAX_ROBOTS_CONTROLLED];
381 extern sbyte robot_fire_buf[MAX_ROBOTS_CONTROLLED][18+3];
382
383
384 #if defined(WINDOWS) || defined(MACINTOSH)
385 extern ubyte Hack_DblClick_MenuMode;
386 #endif
387
388 void compute_all_static_light(void);
389
390 //-------------------------------------------------------------------
391 void state_callback(int nitems,newmenu_item * items, int * last_key, int citem)
392 {
393         nitems = nitems;
394         last_key = last_key;
395         
396 //      if ( sc_last_item != citem )    {
397 //              sc_last_item = citem;
398                 if ( citem > 0 )        {
399                         if ( sc_bmp[citem-1] )  {
400                                 if (MenuHires) {
401                                 WINDOS(
402                                         dd_grs_canvas *save_canv = dd_grd_curcanv,
403                                         grs_canvas *save_canv = grd_curcanv
404                                 );
405                                         grs_canvas *temp_canv = gr_create_canvas(THUMBNAIL_W*2,(THUMBNAIL_H*24/10));
406                                         grs_point vertbuf[3] = {{0,0}, {0,0}, {i2f(THUMBNAIL_W*2),i2f(THUMBNAIL_H*24/10)} };
407                                         gr_set_current_canvas(temp_canv);
408                                         scale_bitmap(sc_bmp[citem-1], vertbuf, 0 );
409                                 WINDOS(
410                                         dd_gr_set_current_canvas(save_canv),
411                                         gr_set_current_canvas( save_canv )
412                                 );
413                                 WIN(DDGRLOCK(dd_grd_curcanv));
414                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W*2)/2,items[0].y-10, &temp_canv->cv_bitmap);
415                                 WIN(DDGRUNLOCK(dd_grd_curcanv));
416                                         gr_free_canvas(temp_canv);
417                                 }
418                                 else    {
419                                 #ifdef WINDOWS
420                                         Int3();
421                                 #else
422                                         gr_bitmap( (grd_curcanv->cv_bitmap.bm_w-THUMBNAIL_W)/2,items[0].y-5, sc_bmp[citem-1] );
423                                 #endif
424                                 }
425                         }
426                 }
427 //      }       
428 }
429
430 void rpad_string( char * string, int max_chars )
431 {
432         int i, end_found;
433
434         end_found = 0;
435         for( i=0; i<max_chars; i++ )    {
436                 if ( *string == 0 )
437                         end_found = 1;
438                 if ( end_found )
439                         *string = ' ';
440                 string++;
441         }
442         *string = 0;            // NULL terminate
443 }
444
445 int state_get_save_file(char * fname, char * dsc, int multi )
446 {
447         CFILE *fp;
448         int i, choice, version;
449         newmenu_item m[NUM_SAVES+2];
450         char filename[NUM_SAVES+1][30];
451         char desc[NUM_SAVES+1][DESC_LENGTH+16];
452         char id[5];
453         int valid=0;
454         
455         for (i=0;i<NUM_SAVES; i++ )     {
456                 sc_bmp[i] = NULL;
457                 if ( !multi )
458                         #ifndef MACINTOSH
459                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
460                         #else
461                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
462                         #endif
463                 else
464                         #ifndef MACINTOSH
465                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
466                         #else
467                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
468                         #endif
469                 valid = 0;
470                 fp = cfopen(filename[i], "rb");
471                 if ( fp ) {
472                         //Read id
473                         cfread(id, sizeof(char)*4, 1, fp);
474                         if ( !memcmp( id, dgss_id, 4 )) {
475                                 //Read version
476                                 cfread(&version, sizeof(int), 1, fp);
477                                 if (version >= STATE_COMPATIBLE_VERSION)        {
478                                         // Read description
479                                         cfread(desc[i], sizeof(char)*DESC_LENGTH, 1, fp);
480                                         //rpad_string( desc[i], DESC_LENGTH-1 );
481                                         // Read thumbnail
482                                         //sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
483                                         //cfread(sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp);
484                                         valid = 1;
485                                 }
486                         }
487                         cfclose(fp);
488                 }
489                 if (!valid) {
490                         strcpy( desc[i], TXT_EMPTY );
491                         //rpad_string( desc[i], DESC_LENGTH-1 );
492                 }
493                 m[i].type = NM_TYPE_INPUT_MENU; m[i].text = desc[i]; m[i].text_len = DESC_LENGTH-1;
494         }
495
496         sc_last_item = -1;
497         choice = newmenu_do1( NULL, "Save Game", NUM_SAVES, m, NULL, state_default_item );
498
499         for (i=0; i<NUM_SAVES; i++ )    {
500                 if ( sc_bmp[i] )
501                         gr_free_bitmap( sc_bmp[i] );
502         }
503
504         if (choice > -1) {
505                 strcpy( fname, filename[choice] );
506                 strcpy( dsc, desc[choice] );
507                 state_default_item = choice;
508                 return choice+1;
509         }
510         return 0;
511 }
512
513 int RestoringMenu=0;
514 extern int Current_display_mode;
515
516 int state_get_restore_file(char * fname, int multi)
517 {
518         CFILE *fp;
519         int i, choice, version, nsaves;
520         newmenu_item m[NUM_SAVES+2];
521         char filename[NUM_SAVES+1][30];
522         char desc[NUM_SAVES+1][DESC_LENGTH + 16];
523         char id[5];
524         int valid;
525
526         nsaves=0;
527         m[0].type = NM_TYPE_TEXT; m[0].text = "\n\n\n\n";       
528         for (i=0;i<NUM_SAVES+1; i++ )   {
529                 sc_bmp[i] = NULL;
530                 if (!multi)
531                         #ifndef MACINTOSH
532                         sprintf( filename[i], "%s.sg%x", Players[Player_num].callsign, i );
533                         #else
534                         sprintf( filename[i], ":Players:%s.sg%x", Players[Player_num].callsign, i );
535                         #endif
536                 else
537                         #ifndef MACINTOSH
538                         sprintf( filename[i], "%s.mg%x", Players[Player_num].callsign, i );
539                         #else
540                         sprintf( filename[i], ":Players:%s.mg%x", Players[Player_num].callsign, i );
541                         #endif
542                 valid = 0;
543                 fp = cfopen(filename[i], "rb");
544                 if ( fp ) {
545                         //Read id
546                         cfread(id, sizeof(char)*4, 1, fp);
547                         if ( !memcmp( id, dgss_id, 4 )) {
548                                 //Read version
549                                 cfread(&version, sizeof(int), 1, fp);
550                                 if (version >= STATE_COMPATIBLE_VERSION)        {
551                                         // Read description
552                                         cfread(desc[i], sizeof(char)*DESC_LENGTH, 1, fp);
553                                         //rpad_string( desc[i], DESC_LENGTH-1 );
554                                         m[i+1].type = NM_TYPE_MENU; m[i+1].text = desc[i];
555                                         // Read thumbnail
556                                         sc_bmp[i] = gr_create_bitmap(THUMBNAIL_W,THUMBNAIL_H );
557                                         cfread(sc_bmp[i]->bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp);
558                                         if (version >= 9) {
559                                                 ubyte pal[256*3];
560                                                 cfread(pal, 3, 256, fp);
561                                                 gr_remap_bitmap_good( sc_bmp[i], pal, -1, -1 );
562                                         }
563                                         nsaves++;
564                                         valid = 1;
565                                 }
566                         }
567                         cfclose(fp);
568                 }
569                 if (!valid) {
570                         strcpy( desc[i], TXT_EMPTY );
571                         //rpad_string( desc[i], DESC_LENGTH-1 );
572                         m[i+1].type = NM_TYPE_TEXT; m[i+1].text = desc[i];
573                 }
574         }
575
576         if ( nsaves < 1 )       {
577                 nm_messagebox( NULL, 1, "Ok", "No saved games were found!" );
578                 return 0;
579         }
580
581         if (Current_display_mode == 3)  //restore menu won't fit on 640x400
582                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
583
584         sc_last_item = -1;
585
586 #if defined(WINDOWS) || defined(MACINTOSH)
587         Hack_DblClick_MenuMode = 1;
588 #endif
589
590    RestoringMenu=1;
591         choice = newmenu_do3( NULL, "Select Game to Restore", NUM_SAVES+2, m, state_callback, state_default_item+1, NULL, 190, -1 );
592    RestoringMenu=0;
593
594 #if defined(WINDOWS) || defined(MACINTOSH)
595         Hack_DblClick_MenuMode = 0;
596 #endif
597
598         if (Current_display_mode == 3)  //set flag back
599                 VR_screen_flags ^= VRF_COMPATIBLE_MENUS;
600
601
602         for (i=0; i<NUM_SAVES+1; i++ )  {
603                 if ( sc_bmp[i] )
604                         gr_free_bitmap( sc_bmp[i] );
605         }
606
607         if (choice > 0) {
608                 strcpy( fname, filename[choice-1] );
609                 if (choice != NUM_SAVES+1)              //no new default when restore from autosave
610                         state_default_item = choice - 1;
611                 return choice;
612         }
613         return 0;
614 }
615
616 #define DESC_OFFSET     8
617
618 #define CF_BUF_SIZE     1024
619
620 #ifdef _WIN32_WCE
621 # define errno -1
622 # define strerror(x) "Unknown Error"
623 #endif
624
625
626 //      -----------------------------------------------------------------------------------
627 //      Imagine if C had a function to copy a file...
628 int copy_file(char *old_file, char *new_file)
629 {
630         sbyte   buf[CF_BUF_SIZE];
631         CFILE   *in_file, *out_file;
632
633         out_file = cfopen(new_file, "wb");
634
635         if (out_file == NULL)
636                 return -1;
637
638         in_file = cfopen(old_file, "rb");
639
640         if (in_file == NULL)
641                 return -2;
642
643         while (!cfeof(in_file))
644         {
645                 int bytes_read;
646
647                 bytes_read = cfread(buf, 1, CF_BUF_SIZE, in_file);
648                 if (cferror(in_file))
649                         Error("Cannot read from file <%s>: %s", old_file, strerror(errno));
650
651                 Assert(bytes_read == CF_BUF_SIZE || cfeof(in_file));
652
653                 cfwrite(buf, 1, bytes_read, out_file);
654
655                 if (cferror(out_file))
656                         Error("Cannot write to file <%s>: %s", new_file, strerror(errno));
657         }
658
659         if (cfclose(in_file)) {
660                 cfclose(out_file);
661                 return -3;
662         }
663
664         if (cfclose(out_file))
665                 return -4;
666
667         return 0;
668 }
669
670 #ifndef MACINTOSH
671 #define SECRETB_FILENAME        "secret.sgb"
672 #define SECRETC_FILENAME        "secret.sgc"
673 #else
674 #define SECRETB_FILENAME        ":Players:secret.sgb"
675 #define SECRETC_FILENAME        ":Players:secret.sgc"
676 #endif
677
678 extern int Final_boss_is_dead;
679
680 //      -----------------------------------------------------------------------------------
681 //      blind_save means don't prompt user for any info.
682 int state_save_all(int between_levels, int secret_save, char *filename_override)
683 {
684         int     rval, filenum = -1;
685
686         char    filename[128], desc[DESC_LENGTH+1];
687
688         Assert(between_levels == 0);    //between levels save ripped out
689
690 #ifdef NETWORK
691         if ( Game_mode & GM_MULTI )     {
692                         multi_initiate_save_game();
693                 return 0;
694         }
695 #endif
696
697         if ((Current_level_num < 0) && (secret_save == 0)) {
698                 HUD_init_message( "Can't save in secret level!" );
699                 return 0;
700         }
701
702         if (Final_boss_is_dead)         //don't allow save while final boss is dying
703                 return 0;
704
705         mprintf(( 0, "CL=%d, NL=%d\n", Current_level_num, Next_level_num ));
706         
707         //      If this is a secret save and the control center has been destroyed, don't allow
708         //      return to the base level.
709         if (secret_save && (Control_center_destroyed)) {
710                 mprintf((0, "Deleting secret.sgb so player can't return to base level.\n"));
711                 cfile_delete(SECRETB_FILENAME);
712                 return 0;
713         }
714
715         stop_time();
716
717         if (secret_save == 1) {
718                 filename_override = filename;
719                 sprintf(filename_override, SECRETB_FILENAME);
720         } else if (secret_save == 2) {
721                 filename_override = filename;
722                 sprintf(filename_override, SECRETC_FILENAME);
723         } else {
724                 if (filename_override) {
725                         strcpy( filename, filename_override);
726                         sprintf(desc, "[autosave backup]");
727                 } else if (!(filenum = state_get_save_file(filename,desc,0))) {
728                         start_time();
729                         return 0;
730                 }
731         }
732                 
733         //      MK, 1/1/96
734         //      If not in multiplayer, do special secret level stuff.
735         //      If secret.sgc exists, then copy it to Nsecret.sgc (where N = filenum).
736         //      If it doesn't exist, then delete Nsecret.sgc
737         if (!secret_save && !(Game_mode & GM_MULTI)) {
738                 int     rval;
739                 char    temp_fname[32], fc;
740
741                 if (filenum != -1) {
742
743                         if (filenum >= 10)
744                                 fc = (filenum-10) + 'a';
745                         else
746                                 fc = '0' + filenum;
747
748                         #ifndef MACINTOSH
749                         sprintf(temp_fname, "%csecret.sgc", fc);
750                         #else
751                         sprintf(temp_fname, ":Players:%csecret.sgc", fc);
752                         #endif
753
754                         mprintf((0, "Trying to copy secret.sgc to %s.\n", temp_fname));
755
756                         if (cfexist(temp_fname))
757                         {
758                                 mprintf((0, "Deleting file %s\n", temp_fname));
759                                 rval = cfile_delete(temp_fname);
760                                 Assert(rval == 0);      //      Oops, error deleting file in temp_fname.
761                         }
762
763                         if (cfexist(SECRETC_FILENAME))
764                         {
765                                 mprintf((0, "Copying secret.sgc to %s.\n", temp_fname));
766                                 rval = copy_file(SECRETC_FILENAME, temp_fname);
767                                 Assert(rval == 0);      //      Oops, error copying temp_fname to secret.sgc!
768                         }
769                 }
770         }
771
772         //      Save file we're going to save over in last slot and call "[autosave backup]"
773         if (!filename_override) {
774                 CFILE *tfp;
775         
776                 tfp = cfopen(filename, "rb");
777
778                 if ( tfp ) {
779                         char    newname[128];
780
781                         #ifndef MACINTOSH
782                         sprintf( newname, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
783                         #else
784                         sprintf( newname, ":Players:%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
785                         #endif
786                         
787                         cfseek(tfp, DESC_OFFSET, SEEK_SET);
788                         cfwrite("[autosave backup]", sizeof(char)*DESC_LENGTH, 1, tfp);
789                         cfclose(tfp);
790                         cfile_delete(newname);
791                         cfile_rename(filename, newname);
792                 }
793         }
794         
795         rval = state_save_all_sub(filename, desc, between_levels);
796
797         return rval;
798 }
799
800 extern  fix     Flash_effect, Time_flash_last_played;
801
802
803 int state_save_all_sub(char *filename, char *desc, int between_levels)
804 {
805         int i,j;
806         CFILE *fp;
807         grs_canvas * cnv;
808         #ifdef POLY_ACC
809         grs_canvas cnv2,*save_cnv2;
810         #endif
811         ubyte *pal;
812
813         Assert(between_levels == 0);    //between levels save ripped out
814
815 /*      if ( Game_mode & GM_MULTI )     {
816                 {
817                 start_time();
818                 return 0;
819                 }
820         }*/
821
822         #if defined(MACINTOSH) && !defined(NDEBUG)
823         if ( strncmp(filename, ":Players:", 9) )
824                 Int3();
825         #endif
826
827         fp = cfopen(filename, "wb");
828         if ( !fp ) {
829                 if ( !(Game_mode & GM_MULTI) )
830                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
831                 start_time();
832                 return 0;
833         }
834
835 //Save id
836         cfwrite(dgss_id, sizeof(char)*4, 1, fp);
837
838 //Save version
839         i = STATE_VERSION;
840         cfwrite(&i, sizeof(int), 1, fp);
841
842 //Save description
843         cfwrite(desc, sizeof(char) * DESC_LENGTH, 1, fp);
844         
845 // Save the current screen shot...
846
847         cnv = gr_create_canvas( THUMBNAIL_W, THUMBNAIL_H );
848         if ( cnv )
849         {
850 #ifdef OGL
851                 ubyte *buf;
852                 int k;
853 #endif
854                 #ifdef WINDOWS
855                         dd_grs_canvas *cnv_save;
856                         cnv_save = dd_grd_curcanv;
857                 #else
858                         grs_canvas * cnv_save;
859                         cnv_save = grd_curcanv;
860                 #endif
861
862                 #ifndef MACINTOSH
863                 
864                         #if defined(POLY_ACC)
865                         
866                                         PA_DFX (pa_fool_to_backbuffer());
867                         
868                                         //for poly_acc, we render the frame to the normal render buffer
869                                         //so that this doesn't show, we create yet another canvas to save
870                                         //and restore what's on the render buffer
871                                         PA_DFX (pa_alpha_always());     
872                                         PA_DFX (pa_set_front_to_read());
873                                         gr_init_sub_canvas( &cnv2, &VR_render_buffer[0], 0, 0, THUMBNAIL_W, THUMBNAIL_H );
874                                         save_cnv2 = gr_create_canvas2(THUMBNAIL_W, THUMBNAIL_H, cnv2.cv_bitmap.bm_type);
875                                         gr_set_current_canvas( save_cnv2 );
876                                         PA_DFX (pa_set_front_to_read());
877                                         gr_bitmap(0,0,&cnv2.cv_bitmap);
878                                         gr_set_current_canvas( &cnv2 );
879                         #else
880                                         gr_set_current_canvas( cnv );
881                         #endif
882                         
883                                         PA_DFX (pa_set_backbuffer_current());
884                                         render_frame(0, 0);
885                                         PA_DFX (pa_alpha_always());
886                         
887 #if defined(POLY_ACC)
888                                         #ifndef MACINTOSH
889                                         screen_shot_pa(cnv,&cnv2);
890                                         #else
891                                         if ( PAEnabled )
892                                                 screen_shot_pa(cnv,&cnv2);
893                                         #endif
894 #elif defined(OGL)
895 # if 1
896                 buf = d_malloc(THUMBNAIL_W * THUMBNAIL_H * 3);
897                 glReadBuffer(GL_FRONT);
898                 glReadPixels(0, SHEIGHT - THUMBNAIL_H, THUMBNAIL_W, THUMBNAIL_H, GL_RGB, GL_UNSIGNED_BYTE, buf);
899                 k = THUMBNAIL_H;
900                 for (i = 0; i < THUMBNAIL_W * THUMBNAIL_H; i++) {
901                         if (!(j = i % THUMBNAIL_W))
902                                 k--;
903                         cnv->cv_bitmap.bm_data[THUMBNAIL_W * k + j] =
904                                 gr_find_closest_color(buf[3*i]/4, buf[3*i+1]/4, buf[3*i+2]/4);
905                 }
906                 d_free(buf);
907 # else // simpler d1x method, not tested yet
908                 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);
909 # endif
910 #endif
911
912                                         pal = gr_palette;
913
914                                         cfwrite(cnv->cv_bitmap.bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp);
915
916                         #if defined(POLY_ACC)
917                                         PA_DFX (pa_alpha_always());     
918                                         PA_DFX (pa_set_frontbuffer_current());
919                                         PA_DFX(gr_bitmap(0,0,&save_cnv2->cv_bitmap));
920                                         PA_DFX (pa_set_backbuffer_current());
921                                         gr_bitmap(0,0,&save_cnv2->cv_bitmap);
922                                         gr_free_canvas(save_cnv2);
923                                         PA_DFX (pa_fool_to_offscreen());
924                         
925                         #endif
926                 
927                 #else   // macintosh stuff below
928                 {
929                         #if defined(POLY_ACC)
930                                 int     savePAEnabled = PAEnabled;
931                                 PAEnabled = false;
932                         #endif
933
934                         gr_set_current_canvas( cnv );
935                         render_frame(0, 0);
936                         pal = gr_palette;
937                         cfwrite(cnv->cv_bitmap.bm_data, THUMBNAIL_W * THUMBNAIL_H, 1, fp);
938                         
939                         #if defined(POLY_ACC)
940                                 PAEnabled = savePAEnabled;
941                         #endif
942                 }       
943                 #endif  // end of ifndef macintosh
944                 
945                 
946                 WINDOS(
947                         dd_gr_set_current_canvas(cnv_save),
948                         gr_set_current_canvas(cnv_save)
949                 );
950                 gr_free_canvas( cnv );
951                 cfwrite(pal, 3, 256, fp);
952         }
953         else
954         {
955                 ubyte color = 0;
956                 for ( i=0; i<THUMBNAIL_W*THUMBNAIL_H; i++ )
957                         cfwrite(&color, sizeof(ubyte), 1, fp);
958         }
959
960 // Save the Between levels flag...
961         cfwrite(&between_levels, sizeof(int), 1, fp);
962
963 // Save the mission info...
964         mprintf ((0,"HEY! Mission name is %s\n",Mission_list[Current_mission_num].filename));
965         cfwrite(&Mission_list[Current_mission_num], sizeof(char), 9, fp);
966
967 //Save level info
968         cfwrite(&Current_level_num, sizeof(int), 1, fp);
969         cfwrite(&Next_level_num, sizeof(int), 1, fp);
970
971 //Save GameTime
972         cfwrite(&GameTime, sizeof(fix), 1, fp);
973
974 // If coop save, save all
975 #ifdef NETWORK
976    if (Game_mode & GM_MULTI_COOP)
977          {
978                 cfwrite(&state_game_id, sizeof(int), 1, fp);
979                 cfwrite(&Netgame, sizeof(netgame_info), 1, fp);
980                 cfwrite(&NetPlayers, sizeof(AllNetPlayers_info), 1, fp);
981                 cfwrite(&N_players, sizeof(int), 1, fp);
982                 cfwrite(&Player_num, sizeof(int), 1, fp);
983                 for (i=0;i<N_players;i++)
984                         cfwrite(&Players[i], sizeof(player), 1, fp);
985
986 #ifdef RISKY_PROPOSITION
987                 cfwrite(&robot_controlled[0], 4, MAX_ROBOTS_CONTROLLED, fp);
988                 cfwrite(&robot_agitation[0], 4, MAX_ROBOTS_CONTROLLED, fp);
989                 cfwrite(&robot_controlled_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
990                 cfwrite(&robot_last_send_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
991                 cfwrite(&robot_last_message_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
992                 cfwrite(&robot_send_pending[0], 4, MAX_ROBOTS_CONTROLLED, fp);
993                 cfwrite(&robot_fired[0], 4, MAX_ROBOTS_CONTROLLED, fp);
994
995       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
996                         cfwrite(robot_fire_buf[i][0], 18 + 3, 1, fp);
997 #endif
998
999          }
1000 #endif
1001
1002 //Save player info
1003         cfwrite(&Players[Player_num], sizeof(player), 1, fp);
1004
1005 // Save the current weapon info
1006         cfwrite(&Primary_weapon, sizeof(sbyte), 1, fp);
1007         cfwrite(&Secondary_weapon, sizeof(sbyte), 1, fp);
1008
1009 // Save the difficulty level
1010         cfwrite(&Difficulty_level, sizeof(int), 1, fp);
1011 // Save cheats enabled
1012         cfwrite(&Cheats_enabled, sizeof(int), 1, fp);
1013
1014         if ( !between_levels )  {
1015
1016         //Finish all morph objects
1017                 for (i=0; i<=Highest_object_index; i++ )        {
1018                         if ( (Objects[i].type != OBJ_NONE) && (Objects[i].render_type==RT_MORPH))       {
1019                                 morph_data *md;
1020                                 md = find_morph_data(&Objects[i]);
1021                                 if (md) {                                       
1022                                         md->obj->control_type = md->morph_save_control_type;
1023                                         md->obj->movement_type = md->morph_save_movement_type;
1024                                         md->obj->render_type = RT_POLYOBJ;
1025                                         md->obj->mtype.phys_info = md->morph_save_phys_info;
1026                                         md->obj = NULL;
1027                                 } else {                                                //maybe loaded half-morphed from disk
1028                                         Objects[i].flags |= OF_SHOULD_BE_DEAD;
1029                                         Objects[i].render_type = RT_POLYOBJ;
1030                                         Objects[i].control_type = CT_NONE;
1031                                         Objects[i].movement_type = MT_NONE;
1032                                 }
1033                         }
1034                 }
1035         
1036         //Save object info
1037                 i = Highest_object_index+1;
1038                 cfwrite(&i, sizeof(int), 1, fp);
1039                 cfwrite(Objects, sizeof(object), i, fp);
1040                 
1041         //Save wall info
1042                 i = Num_walls;
1043                 cfwrite(&i, sizeof(int), 1, fp);
1044                 cfwrite(Walls, sizeof(wall), i, fp);
1045
1046         //Save exploding wall info
1047                 i = MAX_EXPLODING_WALLS;
1048                 cfwrite(&i, sizeof(int), 1, fp);
1049                 cfwrite(expl_wall_list, sizeof(*expl_wall_list), i, fp);
1050         
1051         //Save door info
1052                 i = Num_open_doors;
1053                 cfwrite(&i, sizeof(int), 1, fp );
1054                 cfwrite(ActiveDoors, sizeof(active_door), i, fp);
1055         
1056         //Save cloaking wall info
1057                 i = Num_cloaking_walls;
1058                 cfwrite(&i, sizeof(int), 1, fp );
1059                 cfwrite(CloakingWalls, sizeof(cloaking_wall), i, fp);
1060         
1061         //Save trigger info
1062                 cfwrite(&Num_triggers, sizeof(int), 1, fp);
1063                 cfwrite(Triggers, sizeof(trigger), Num_triggers, fp);
1064         
1065         //Save tmap info
1066                 for (i=0; i<=Highest_segment_index; i++ )       {
1067                         for (j=0; j<6; j++ )    {
1068                                 cfwrite(&Segments[i].sides[j].wall_num, sizeof(short), 1, fp);
1069                                 cfwrite(&Segments[i].sides[j].tmap_num, sizeof(short), 1, fp);
1070                                 cfwrite(&Segments[i].sides[j].tmap_num2, sizeof(short), 1, fp);
1071                         }
1072                 }
1073         
1074         // Save the fuelcen info
1075                 cfwrite(&Control_center_destroyed, sizeof(int), 1, fp);
1076                 cfwrite(&Countdown_timer, sizeof(int), 1, fp);
1077                 cfwrite(&Num_robot_centers, sizeof(int), 1, fp);
1078                 cfwrite(RobotCenters, sizeof(matcen_info), Num_robot_centers, fp);
1079                 cfwrite(&ControlCenterTriggers, sizeof(control_center_triggers), 1, fp);
1080                 cfwrite(&Num_fuelcenters, sizeof(int), 1, fp);
1081                 cfwrite(Station, sizeof(FuelCenter), Num_fuelcenters, fp);
1082         
1083         // Save the control cen info
1084                 cfwrite(&Control_center_been_hit, sizeof(int), 1, fp);
1085                 cfwrite(&Control_center_player_been_seen, sizeof(int), 1, fp);
1086                 cfwrite(&Control_center_next_fire_time, sizeof(int), 1, fp);
1087                 cfwrite(&Control_center_present, sizeof(int), 1, fp);
1088                 cfwrite(&Dead_controlcen_object_num, sizeof(int), 1, fp);
1089         
1090         // Save the AI state
1091                 ai_save_state( fp );
1092         
1093         // Save the automap visited info
1094                 cfwrite(Automap_visited, sizeof(ubyte) * MAX_SEGMENTS, 1, fp);
1095
1096         }
1097         cfwrite(&state_game_id, sizeof(uint), 1, fp);
1098         cfwrite(&Laser_rapid_fire, sizeof(int), 1, fp);
1099         cfwrite(&Lunacy, sizeof(int), 1, fp);           //      Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
1100         cfwrite(&Lunacy, sizeof(int), 1, fp);
1101
1102         // Save automap marker info
1103
1104         cfwrite(MarkerObject, sizeof(MarkerObject), 1, fp);
1105         cfwrite(MarkerOwner, sizeof(MarkerOwner), 1, fp);
1106         cfwrite(MarkerMessage, sizeof(MarkerMessage), 1, fp);
1107
1108         cfwrite(&Afterburner_charge, sizeof(fix), 1, fp);
1109
1110         //save last was super information
1111         cfwrite(&Primary_last_was_super, sizeof(Primary_last_was_super), 1, fp);
1112         cfwrite(&Secondary_last_was_super, sizeof(Secondary_last_was_super), 1, fp);
1113
1114         //      Save flash effect stuff
1115         cfwrite(&Flash_effect, sizeof(int), 1, fp);
1116         cfwrite(&Time_flash_last_played, sizeof(int), 1, fp);
1117         cfwrite(&PaletteRedAdd, sizeof(int), 1, fp);
1118         cfwrite(&PaletteGreenAdd, sizeof(int), 1, fp);
1119         cfwrite(&PaletteBlueAdd, sizeof(int), 1, fp);
1120
1121         cfwrite(Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS, fp);
1122
1123         cfwrite(&First_secret_visit, sizeof(First_secret_visit), 1, fp);
1124
1125         cfwrite(&Omega_charge, sizeof(Omega_charge), 1, fp);
1126         
1127         if (cferror(fp))
1128         {
1129                 if ( !(Game_mode & GM_MULTI) ) {
1130                         nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
1131                         cfclose(fp);
1132                         cfile_delete(filename);
1133                 }
1134         } else  {
1135                 cfclose(fp);
1136
1137                 #ifdef MACINTOSH                // set the type and creator of the saved game file
1138                 {
1139                         FInfo finfo;
1140                         OSErr err;
1141                         Str255 pfilename;
1142         
1143                         strcpy(pfilename, filename);
1144                         c2pstr(pfilename);
1145                         err = HGetFInfo(0, 0, pfilename, &finfo);
1146                         finfo.fdType = 'SVGM';
1147                         finfo.fdCreator = 'DCT2';
1148                         err = HSetFInfo(0, 0, pfilename, &finfo);
1149                 }
1150                 #endif
1151         }
1152         
1153         start_time();
1154
1155         return 1;
1156 }
1157
1158 //      -----------------------------------------------------------------------------------
1159 //      Set the player's position from the globals Secret_return_segment and Secret_return_orient.
1160 void set_pos_from_return_segment(void)
1161 {
1162         int     plobjnum = Players[Player_num].objnum;
1163
1164         compute_segment_center(&Objects[plobjnum].pos, &Segments[Secret_return_segment]);
1165         obj_relink(plobjnum, Secret_return_segment);
1166         reset_player_object();
1167         Objects[plobjnum].orient = Secret_return_orient;
1168 }
1169
1170 //      -----------------------------------------------------------------------------------
1171 int state_restore_all(int in_game, int secret_restore, char *filename_override)
1172 {
1173         char filename[128];
1174         int     filenum = -1;
1175
1176 #ifdef NETWORK
1177         if ( Game_mode & GM_MULTI )     {
1178 #ifdef MULTI_SAVE
1179                         multi_initiate_restore_game();
1180 #endif
1181                 return 0;
1182         }
1183 #endif
1184
1185         if (in_game && (Current_level_num < 0) && (secret_restore == 0)) {
1186                 HUD_init_message( "Can't restore in secret level!" );
1187                 return 0;
1188         }
1189
1190         if ( Newdemo_state == ND_STATE_RECORDING )
1191                 newdemo_stop_recording();
1192
1193         if ( Newdemo_state != ND_STATE_NORMAL )
1194                 return 0;
1195
1196         stop_time();
1197
1198         if (filename_override) {
1199                 strcpy(filename, filename_override);
1200                 filenum = NUM_SAVES+1;          //      So we don't trigger autosave
1201         } else if (!(filenum = state_get_restore_file(filename, 0)))    {
1202                 start_time();
1203                 return 0;
1204         }
1205         
1206         //      MK, 1/1/96
1207         //      If not in multiplayer, do special secret level stuff.
1208         //      If Nsecret.sgc (where N = filenum) exists, then copy it to secret.sgc.
1209         //      If it doesn't exist, then delete secret.sgc
1210         if (!secret_restore && !(Game_mode & GM_MULTI)) {
1211                 int     rval;
1212                 char    temp_fname[32], fc;
1213
1214                 if (filenum != -1) {
1215                         if (filenum >= 10)
1216                                 fc = (filenum-10) + 'a';
1217                         else
1218                                 fc = '0' + filenum;
1219                         
1220                         #ifndef MACINTOSH
1221                         sprintf(temp_fname, "%csecret.sgc", fc);
1222                         #else
1223                         sprintf(temp_fname, ":Players:%csecret.sgc", fc);
1224                         #endif
1225
1226                         mprintf((0, "Trying to copy %s to secret.sgc.\n", temp_fname));
1227
1228                         if (cfexist(temp_fname))
1229                         {
1230                                 mprintf((0, "Copying %s to secret.sgc\n", temp_fname));
1231                                 rval = copy_file(temp_fname, SECRETC_FILENAME);
1232                                 Assert(rval == 0);      //      Oops, error copying temp_fname to secret.sgc!
1233                         } else
1234                                 cfile_delete(SECRETC_FILENAME);
1235                 }
1236         }
1237
1238         //      Changed, 11/15/95, MK, don't to autosave if restoring from main menu.
1239         if ((filenum != (NUM_SAVES+1)) && in_game) {
1240                 char    temp_filename[128];
1241                 mprintf((0, "Doing autosave, filenum = %i, != %i!\n", filenum, NUM_SAVES+1));
1242                 #ifndef MACINTOSH
1243                 sprintf( temp_filename, "%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
1244                 #else
1245                 sprintf( temp_filename, ":Players:%s.sg%x", Players[Player_num].callsign, NUM_SAVES );
1246                 #endif          
1247                 state_save_all(!in_game, secret_restore, temp_filename);
1248         }
1249
1250         if ( !secret_restore && in_game ) {
1251                 int choice;
1252                 choice =  nm_messagebox( NULL, 2, "Yes", "No", "Restore Game?" );
1253                 if ( choice != 0 )      {
1254                         start_time();
1255                         return 0;
1256                 }
1257         }
1258
1259         start_time();
1260
1261         return state_restore_all_sub(filename, 0, secret_restore);
1262 }
1263
1264 extern void init_player_stats_new_ship(void);
1265
1266 void ShowLevelIntro(int level_num);
1267
1268 extern void do_cloak_invul_secret_stuff(fix old_gametime);
1269 extern void copy_defaults_to_robot(object *objp);
1270
1271 int state_restore_all_sub(char *filename, int multi, int secret_restore)
1272 {
1273         int ObjectStartLocation;
1274         int version,i, j, segnum;
1275         object * obj;
1276         CFILE *fp;
1277         int current_level, next_level;
1278         int between_levels;
1279         char mission[16];
1280         char desc[DESC_LENGTH+1];
1281         char id[5];
1282         char org_callsign[CALLSIGN_LEN+16];
1283 #ifdef NETWORK
1284         int found;
1285         int nplayers;   //,playid[12],mynum;
1286         player restore_players[MAX_PLAYERS];
1287 #endif
1288         fix     old_gametime = GameTime;
1289
1290         #if defined(MACINTOSH) && !defined(NDEBUG)
1291         if ( strncmp(filename, ":Players:", 9) )
1292                 Int3();
1293         #endif
1294
1295         fp = cfopen(filename, "rb");
1296         if ( !fp ) return 0;
1297
1298 //Read id
1299         cfread(id, sizeof(char)*4, 1, fp);
1300         if ( memcmp( id, dgss_id, 4 )) {
1301                 cfclose(fp);
1302                 return 0;
1303         }
1304
1305 //Read version
1306         cfread(&version, sizeof(int), 1, fp);
1307         if (version < STATE_COMPATIBLE_VERSION) {
1308                 cfclose(fp);
1309                 return 0;
1310         }
1311
1312 // Read description
1313         cfread(desc, sizeof(char) * DESC_LENGTH, 1, fp);
1314
1315 // Skip the current screen shot...
1316         cfseek(fp, THUMBNAIL_W*THUMBNAIL_H, SEEK_CUR);
1317
1318 // And now...skip the goddamn palette stuff that somebody forgot to add
1319         cfseek(fp, 768, SEEK_CUR);
1320
1321 // Read the Between levels flag...
1322         cfread(&between_levels, sizeof(int), 1, fp);
1323
1324         Assert(between_levels == 0);    //between levels save ripped out
1325
1326 // Read the mission info...
1327         cfread(mission, sizeof(char), 9, fp);
1328         mprintf ((0,"Missionname to load = %s\n",mission));
1329
1330         if (!load_mission_by_name( mission ))   {
1331                 nm_messagebox( NULL, 1, "Ok", "Error!\nUnable to load mission\n'%s'\n", mission );
1332                 cfclose(fp);
1333                 return 0;
1334         }
1335
1336 //Read level info
1337         cfread(&current_level, sizeof(int), 1, fp);
1338         cfread(&next_level, sizeof(int), 1, fp);
1339
1340 //Restore GameTime
1341         cfread(&GameTime, sizeof(fix), 1, fp);
1342
1343 // Start new game....
1344         if (!multi)     {
1345                 Game_mode = GM_NORMAL;
1346                 Function_mode = FMODE_GAME;
1347 #ifdef NETWORK
1348                 change_playernum_to(0);
1349 #endif
1350                 strcpy( org_callsign, Players[0].callsign );
1351                 N_players = 1;
1352                 if (!secret_restore) {
1353                         InitPlayerObject();                             //make sure player's object set up
1354                         init_player_stats_game();               //clear all stats
1355                 }
1356         } else {
1357                 strcpy( org_callsign, Players[Player_num].callsign );
1358         }
1359
1360 #ifdef NETWORK
1361    if (Game_mode & GM_MULTI)
1362          {
1363                 cfread(&state_game_id,sizeof(int), 1, fp);
1364                 cfread(&Netgame,sizeof(netgame_info), 1, fp);
1365                 cfread(&NetPlayers,sizeof(AllNetPlayers_info), 1, fp);
1366                 cfread(&nplayers,sizeof(N_players), 1, fp);
1367                 cfread(&Player_num,sizeof(Player_num), 1, fp);
1368                 for (i=0;i<nplayers;i++)
1369                         cfread(&restore_players[i], sizeof(player), 1, fp);
1370 #ifdef RISKY_PROPOSITION
1371                 cfread(&robot_controlled[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1372                 cfread(&robot_agitation[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1373                 cfread(&robot_controlled_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1374                 cfread(&robot_last_send_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1375                 cfread(&robot_last_message_time[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1376                 cfread(&robot_send_pending[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1377                 cfread(&robot_fired[0], 4, MAX_ROBOTS_CONTROLLED, fp);
1378
1379       for (i=0;i<MAX_ROBOTS_CONTROLLED;i++)
1380                         cfread(&robot_fire_buf[i][0], 21, 1, fp);
1381 #endif
1382
1383            for (i=0;i<nplayers;i++)
1384                  {
1385                   found=0;
1386                   for (j=0;j<nplayers;j++)
1387                          {
1388            if ((!strcmp (restore_players[i].callsign,Players[j].callsign)) && Players[j].connected==1)
1389                                  found=1;
1390                          }
1391                   restore_players[i].connected=found;
1392             }
1393                 memcpy (&Players,&restore_players,sizeof(player)*nplayers);
1394                 N_players=nplayers;
1395
1396       if (network_i_am_master())
1397                  {
1398                   for (i=0;i<N_players;i++)
1399                         {
1400                          if (i==Player_num)
1401                                 continue;
1402                  Players[i].connected=0;
1403                         }
1404                  }
1405
1406                 //Viewer = ConsoleObject = &Objects[Players[Player_num].objnum];
1407          }
1408
1409 #endif
1410
1411 //Read player info
1412
1413         {
1414                 StartNewLevelSub(current_level, 1, secret_restore);
1415
1416                 if (secret_restore) {
1417                         player  dummy_player;
1418
1419                         cfread(&dummy_player, sizeof(player), 1, fp);
1420                         if (secret_restore == 1) {              //      This means he didn't die, so he keeps what he got in the secret level.
1421                                 Players[Player_num].level = dummy_player.level;
1422                                 Players[Player_num].last_score = dummy_player.last_score;
1423                                 Players[Player_num].time_level = dummy_player.time_level;
1424
1425                                 Players[Player_num].num_robots_level = dummy_player.num_robots_level;
1426                                 Players[Player_num].num_robots_total = dummy_player.num_robots_total;
1427                                 Players[Player_num].hostages_rescued_total = dummy_player.hostages_rescued_total;
1428                                 Players[Player_num].hostages_total = dummy_player.hostages_total;
1429                                 Players[Player_num].hostages_on_board = dummy_player.hostages_on_board;
1430                                 Players[Player_num].hostages_level = dummy_player.hostages_level;
1431                                 Players[Player_num].homing_object_dist = dummy_player.homing_object_dist;
1432                                 Players[Player_num].hours_level = dummy_player.hours_level;
1433                                 Players[Player_num].hours_total = dummy_player.hours_total;
1434                                 do_cloak_invul_secret_stuff(old_gametime);
1435                         } else {
1436                                 Players[Player_num] = dummy_player;
1437                         }
1438                 } else {
1439                         cfread(&Players[Player_num], sizeof(player), 1, fp);
1440                 }
1441         }
1442         strcpy( Players[Player_num].callsign, org_callsign );
1443
1444 // Set the right level
1445         if ( between_levels )
1446                 Players[Player_num].level = next_level;
1447
1448 // Restore the weapon states
1449         cfread(&Primary_weapon, sizeof(sbyte), 1, fp);
1450         cfread(&Secondary_weapon, sizeof(sbyte), 1, fp);
1451
1452         select_weapon(Primary_weapon, 0, 0, 0);
1453         select_weapon(Secondary_weapon, 1, 0, 0);
1454
1455 // Restore the difficulty level
1456         cfread(&Difficulty_level, sizeof(int), 1, fp);
1457
1458 // Restore the cheats enabled flag
1459
1460         cfread(&Cheats_enabled, sizeof(int), 1, fp);
1461
1462         if ( !between_levels )  {
1463                 Do_appearance_effect = 0;                       // Don't do this for middle o' game stuff.
1464
1465                 ObjectStartLocation = cftell(fp);
1466                 //Clear out all the objects from the lvl file
1467                 for (segnum=0; segnum <= Highest_segment_index; segnum++)
1468                         Segments[segnum].objects = -1;
1469                 reset_objects(1);
1470         
1471                 //Read objects, and pop 'em into their respective segments.
1472                 cfread(&i, sizeof(int), 1, fp);
1473                 Highest_object_index = i-1;
1474                 cfread(Objects, sizeof(object), i, fp);
1475         
1476                 Object_next_signature = 0;
1477                 for (i=0; i<=Highest_object_index; i++ )        {
1478                         obj = &Objects[i];
1479                         obj->rtype.pobj_info.alt_textures = -1;
1480                         segnum = obj->segnum;
1481                         obj->next = obj->prev = obj->segnum = -1;
1482                         if ( obj->type != OBJ_NONE )    {
1483                                 obj_link(i,segnum);
1484                                 if ( obj->signature > Object_next_signature )
1485                                         Object_next_signature = obj->signature;
1486                         }
1487
1488                         //look for, and fix, boss with bogus shields
1489                         if (obj->type == OBJ_ROBOT && Robot_info[obj->id].boss_flag) {
1490                                 fix save_shields = obj->shields;
1491
1492                                 copy_defaults_to_robot(obj);            //calculate starting shields
1493
1494                                 //if in valid range, use loaded shield value
1495                                 if (save_shields > 0 && save_shields <= obj->shields)
1496                                         obj->shields = save_shields;
1497                                 else
1498                                         obj->shields /= 2;  //give player a break
1499                         }
1500
1501                 }       
1502                 special_reset_objects();
1503                 Object_next_signature++;
1504         
1505                 //      1 = Didn't die on secret level.
1506                 //      2 = Died on secret level.
1507                 if (secret_restore && (Current_level_num >= 0)) {
1508                         set_pos_from_return_segment();
1509                         if (secret_restore == 2)
1510                                 init_player_stats_new_ship();
1511                 }
1512
1513                 //Restore wall info
1514                 cfread(&i, sizeof(int), 1, fp);
1515                 Num_walls = i;
1516                 cfread(Walls, sizeof(wall), Num_walls, fp);
1517
1518                 //now that we have the walls, check if any sounds are linked to
1519                 //walls that are now open
1520                 for (i=0;i<Num_walls;i++) {
1521                         if (Walls[i].type == WALL_OPEN)
1522                                 digi_kill_sound_linked_to_segment(Walls[i].segnum,Walls[i].sidenum,-1); //-1 means kill any sound
1523                 }
1524
1525                 //Restore exploding wall info
1526                 if (version >= 10) {
1527                         cfread(&i, sizeof(int), 1, fp);
1528                         cfread(expl_wall_list, sizeof(*expl_wall_list), i, fp);
1529                 }
1530
1531                 //Restore door info
1532                 cfread(&i, sizeof(int), 1, fp);
1533                 Num_open_doors = i;
1534                 cfread(ActiveDoors, sizeof(active_door), Num_open_doors, fp);
1535         
1536                 if (version >= 14) {            //Restore cloaking wall info
1537                         cfread(&i, sizeof(int), 1, fp);
1538                         Num_cloaking_walls = i;
1539                         cfread(CloakingWalls, sizeof(cloaking_wall), Num_cloaking_walls, fp);
1540                 }
1541         
1542                 //Restore trigger info
1543                 cfread(&Num_triggers, sizeof(int), 1, fp);
1544                 cfread(Triggers, sizeof(trigger), Num_triggers, fp);
1545         
1546                 //Restore tmap info
1547                 for (i=0; i<=Highest_segment_index; i++ )       {
1548                         for (j=0; j<6; j++ )    {
1549                                 cfread(&Segments[i].sides[j].wall_num, sizeof(short), 1, fp);
1550                                 cfread(&Segments[i].sides[j].tmap_num, sizeof(short), 1, fp);
1551                                 cfread(&Segments[i].sides[j].tmap_num2, sizeof(short), 1, fp);
1552                         }
1553                 }
1554         
1555                 //Restore the fuelcen info
1556                 cfread(&Control_center_destroyed, sizeof(int), 1, fp);
1557                 cfread(&Countdown_timer, sizeof(int), 1, fp);
1558                 cfread(&Num_robot_centers, sizeof(int), 1, fp);
1559                 cfread(RobotCenters, sizeof(matcen_info), Num_robot_centers, fp);
1560                 cfread(&ControlCenterTriggers, sizeof(control_center_triggers), 1, fp);
1561                 cfread(&Num_fuelcenters, sizeof(int), 1, fp);
1562                 cfread(Station, sizeof(FuelCenter), Num_fuelcenters, fp);
1563         
1564                 // Restore the control cen info
1565                 cfread(&Control_center_been_hit, sizeof(int), 1, fp);
1566                 cfread(&Control_center_player_been_seen, sizeof(int), 1, fp);
1567                 cfread(&Control_center_next_fire_time, sizeof(int), 1, fp);
1568                 cfread(&Control_center_present, sizeof(int), 1, fp);
1569                 cfread(&Dead_controlcen_object_num, sizeof(int), 1, fp);
1570         
1571                 // Restore the AI state
1572                 ai_restore_state( fp, version );
1573         
1574                 // Restore the automap visited info
1575                 cfread( Automap_visited, sizeof(ubyte), MAX_SEGMENTS, fp);
1576
1577                 //      Restore hacked up weapon system stuff.
1578                 Fusion_next_sound_time = GameTime;
1579                 Auto_fire_fusion_cannon_time = 0;
1580                 Next_laser_fire_time = GameTime;
1581                 Next_missile_fire_time = GameTime;
1582                 Last_laser_fired_time = GameTime;
1583
1584         }
1585         state_game_id = 0;
1586
1587         if ( version >= 7 )     {
1588                 cfread(&state_game_id, sizeof(uint), 1, fp);
1589                 cfread(&Laser_rapid_fire, sizeof(int), 1, fp);
1590                 cfread(&Lunacy, sizeof(int), 1, fp);            //      Yes, writing this twice.  Removed the Ugly robot system, but didn't want to change savegame format.
1591                 cfread(&Lunacy, sizeof(int), 1, fp);
1592                 if ( Lunacy )
1593                         do_lunacy_on();
1594         }
1595
1596         if (version >= 17) {
1597                 cfread(MarkerObject, sizeof(MarkerObject), 1, fp);
1598                 cfread(MarkerOwner, sizeof(MarkerOwner), 1, fp);
1599                 cfread(MarkerMessage, sizeof(MarkerMessage), 1, fp);
1600         }
1601         else {
1602                 int num,dummy;
1603
1604                 // skip dummy info
1605
1606                 cfread(&num, sizeof(int), 1, fp);       //was NumOfMarkers
1607                 cfread(&dummy, sizeof(int), 1, fp);     //was CurMarker
1608
1609                 cfseek(fp, num * (sizeof(vms_vector) + 40), SEEK_CUR);
1610
1611                 for (num=0;num<NUM_MARKERS;num++)
1612                         MarkerObject[num] = -1;
1613         }
1614
1615         if (version>=11) {
1616                 if (secret_restore != 1)
1617                         cfread(&Afterburner_charge, sizeof(fix), 1, fp);
1618                 else {
1619                         fix     dummy_fix;
1620                         cfread(&dummy_fix,sizeof(fix), 1, fp);
1621                 }
1622         }
1623         if (version>=12) {
1624                 //read last was super information
1625                 cfread(&Primary_last_was_super, sizeof(Primary_last_was_super), 1, fp);
1626                 cfread(&Secondary_last_was_super, sizeof(Secondary_last_was_super), 1, fp);
1627         }
1628
1629         if (version >= 12) {
1630                 cfread(&Flash_effect, sizeof(int), 1, fp);
1631                 cfread(&Time_flash_last_played, sizeof(int), 1, fp);
1632                 cfread(&PaletteRedAdd, sizeof(int), 1, fp);
1633                 cfread(&PaletteGreenAdd, sizeof(int), 1, fp);
1634                 cfread(&PaletteBlueAdd, sizeof(int), 1, fp);
1635         } else {
1636                 Flash_effect = 0;
1637                 Time_flash_last_played = 0;
1638                 PaletteRedAdd = 0;
1639                 PaletteGreenAdd = 0;
1640                 PaletteBlueAdd = 0;
1641         }
1642
1643         //      Load Light_subtracted
1644         if (version >= 16) {
1645                 cfread(Light_subtracted, sizeof(Light_subtracted[0]), MAX_SEGMENTS, fp);
1646                 apply_all_changed_light();
1647                 compute_all_static_light();     //      set static_light field in segment struct.  See note at that function.
1648         } else {
1649                 int     i;
1650                 for (i=0; i<=Highest_segment_index; i++)
1651                         Light_subtracted[i] = 0;
1652         }
1653
1654         if (!secret_restore) {
1655                 if (version >= 20) {
1656                         cfread(&First_secret_visit, sizeof(First_secret_visit), 1, fp);
1657                         mprintf((0, "File: [%s] Read First_secret_visit: New value = %i\n", filename, First_secret_visit));
1658                 } else
1659                         First_secret_visit = 1;
1660         } else
1661                 First_secret_visit = 0;
1662
1663         if (version >= 22)
1664         {
1665                 if (secret_restore != 1)
1666                         cfread(&Omega_charge,sizeof(fix), 1, fp);
1667                 else {
1668                         fix     dummy_fix;
1669                         cfread(&dummy_fix,sizeof(fix), 1, fp);
1670                 }
1671         }
1672
1673         cfclose(fp);
1674
1675 #ifdef NETWORK
1676    if (Game_mode & GM_MULTI)   // Get rid of ships that aren't
1677          {                                                                       // connected in the restored game
1678                 for (i=0;i<nplayers;i++)
1679                  {
1680                   mprintf ((0,"Testing %s = %d\n",Players[i].callsign,Players[i].connected));
1681                   if (Players[i].connected!=1)
1682                    {
1683                     network_disconnect_player (i);
1684                create_player_appearance_effect(&Objects[Players[i].objnum]);
1685                          mprintf ((0,"Killing player ship %s!\n",Players[i].callsign));
1686               }
1687                  }
1688                         
1689          }
1690 #endif
1691
1692 // Load in bitmaps, etc..
1693 //!!    piggy_load_level_data();        //already done by StartNewLevelSub()
1694
1695         return 1;
1696 }
1697
1698 //      When loading a saved game, segp->static_light is bogus.
1699 //      This is because apply_all_changed_light, which is supposed to properly update this value,
1700 //      cannot do so because it needs the original light cast from a light which is no longer there.
1701 //      That is, a light has been blown out, so the texture remaining casts 0 light, but the static light
1702 //      which is present in the static_light field contains the light cast from that light.
1703 void compute_all_static_light(void)
1704 {
1705         int     i, j, k;
1706
1707         for (i=0; i<=Highest_segment_index; i++) {
1708                 fix     total_light;
1709                 segment *segp;
1710
1711                 segp = &Segments[i];
1712                 total_light = 0;
1713
1714                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++) {
1715                         side    *sidep;
1716
1717                         sidep = &segp->sides[j];
1718
1719                         for (k=0; k<4; k++)
1720                                 total_light += sidep->uvls[k].l;
1721                 }
1722
1723                 Segment2s[i].static_light = total_light/(MAX_SIDES_PER_SEGMENT*4);
1724         }
1725
1726 }
1727
1728
1729 int state_get_game_id(char *filename)
1730 {
1731         int version;
1732         CFILE *fp;
1733         int between_levels;
1734         char mission[16];
1735         char desc[DESC_LENGTH+1];
1736         char id[5];
1737         int dumbint;
1738
1739 mprintf((0, "Restoring multigame from [%s]\n", filename));
1740
1741         fp = cfopen(filename, "rb");
1742         if ( !fp ) return 0;
1743
1744 //Read id
1745         cfread(id, sizeof(char)*4, 1, fp);
1746         if ( memcmp( id, dgss_id, 4 )) {
1747                 cfclose(fp);
1748                 return 0;
1749         }
1750
1751 //Read version
1752         cfread(&version, sizeof(int), 1, fp);
1753         if (version < STATE_COMPATIBLE_VERSION) {
1754                 cfclose(fp);
1755                 return 0;
1756         }
1757
1758 // Read description
1759         cfread(desc, sizeof(char)*DESC_LENGTH, 1, fp);
1760
1761 // Skip the current screen shot...
1762         cfseek(fp, THUMBNAIL_W*THUMBNAIL_H, SEEK_CUR);
1763
1764 // And now...skip the palette stuff that somebody forgot to add
1765         cfseek(fp, 768, SEEK_CUR);
1766
1767 // Read the Between levels flag...
1768         cfread(&between_levels, sizeof(int), 1, fp);
1769
1770         Assert(between_levels == 0);    //between levels save ripped out
1771
1772 // Read the mission info...
1773         cfread(mission, sizeof(char), 9, fp);
1774 //Read level info
1775         cfread(&dumbint, sizeof(int), 1, fp);
1776         cfread(&dumbint, sizeof(int), 1, fp);
1777
1778 //Restore GameTime
1779         cfread(&dumbint, sizeof(fix), 1, fp);
1780
1781         cfread(&state_game_id, sizeof(int), 1, fp);
1782
1783         return (state_game_id);
1784  }
1785
1786 #if defined(POLY_ACC)
1787 //void screen_shot_pa(ubyte *dst,ushort *src)
1788 //{
1789 //    //ushort *src = pa_get_buffer_address(0),
1790 //    ushort *s;
1791 //    fix u, v, du, dv;
1792 //    int ui, w, h;
1793 //
1794 //    pa_flush();
1795 //
1796 //    du = (640.0 / (float)THUMBNAIL_W) * 65536.0;
1797 //    dv = (480.0 / (float)THUMBNAIL_H) * 65536.0;
1798 //
1799 //    for(v = h = 0; h != THUMBNAIL_H; ++h)
1800 //    {
1801 //        s = src + f2i(v) * 640;
1802 //        v += dv;
1803 //        for(u = w = 0; w != THUMBNAIL_W; ++w)
1804 //        {
1805 //            ui = f2i(u);
1806 //            *dst++ = gr_find_closest_color((s[ui] >> 9) & 0x3e, (s[ui] >> 4) & 0x3e, (s[ui] << 1) & 0x3e);
1807 //            u += du;
1808 //        }
1809 //    }
1810 //}
1811
1812 void screen_shot_pa(grs_canvas *dcanv,grs_canvas *scanv)
1813 {
1814 #if !defined(MACINTOSH)
1815         ubyte *dst;
1816         ushort *src;
1817         int x,y;
1818
1819         Assert(scanv->cv_w == dcanv->cv_w && scanv->cv_h == dcanv->cv_h);
1820
1821         pa_flush();
1822
1823         src = (ushort *) scanv->cv_bitmap.bm_data;
1824         dst = dcanv->cv_bitmap.bm_data;
1825
1826         #ifdef PA_3DFX_VOODOO
1827    src=(ushort *)pa_set_back_to_read();
1828         #endif
1829
1830         for (y=0; y<scanv->cv_h; y++) {
1831                 for (x=0; x<scanv->cv_w; x++) {
1832                         #ifdef PA_3DFX_VOODOO
1833                         *dst++ = gr_find_closest_color((*src >> 10) & 0x3e, (*src >> 5) & 0x3f, (*src << 1) & 0x3e);
1834                         #else
1835                         *dst++ = gr_find_closest_color((*src >> 9) & 0x3e, (*src >> 4) & 0x3e, (*src << 1) & 0x3e);
1836                         #endif
1837
1838                         src++;
1839                 }
1840                 src = (ushort *) (((ubyte *) src) + (scanv->cv_bitmap.bm_rowsize - (scanv->cv_bitmap.bm_w*PA_BPP)));
1841                 dst += dcanv->cv_bitmap.bm_rowsize - dcanv->cv_bitmap.bm_w;
1842         }
1843         #ifdef PA_3DFX_VOODOO
1844         pa_set_front_to_read();
1845         #endif
1846 #endif
1847 }
1848 #endif
1849