]> icculus.org git repositories - btb/d2x.git/blob - main/playsave.c
oops, fix bug in new byteswap code
[btb/d2x.git] / main / playsave.c
1 /* $Id: */
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 load & save player games
18  *
19  * Old Log:
20  * Revision 1.1  1995/12/05  16:05:47  allender
21  * Initial revision
22  *
23  * Revision 1.10  1995/11/03  12:53:24  allender
24  * shareware changes
25  *
26  * Revision 1.9  1995/10/31  10:19:12  allender
27  * shareware stuff
28  *
29  * Revision 1.8  1995/10/23  14:50:11  allender
30  * set control type for new player *before* calling kc_set_controls
31  *
32  * Revision 1.7  1995/10/21  22:25:31  allender
33  * *** empty log message ***
34  *
35  * Revision 1.6  1995/10/17  15:57:42  allender
36  * removed line setting wrong COnfig_control_type
37  *
38  * Revision 1.5  1995/10/17  13:16:44  allender
39  * new controller support
40  *
41  * Revision 1.4  1995/08/24  16:03:38  allender
42  * call joystick code when player file uses joystick
43  *
44  * Revision 1.3  1995/08/03  15:15:39  allender
45  * got player save file working (more to go for shareware)
46  *
47  * Revision 1.2  1995/08/01  13:57:20  allender
48  * macified the player file stuff -- in a seperate folder
49  *
50  * Revision 1.1  1995/05/16  15:30:00  allender
51  * Initial revision
52  *
53  * Revision 2.3  1995/05/26  16:16:23  john
54  * Split SATURN into define's for requiring cd, using cd, etc.
55  * Also started adding all the Rockwell stuff.
56  *
57  * Revision 2.2  1995/03/24  17:48:21  john
58  * Made player files from saturn excrement the highest level for
59  * normal descent levels.
60  *
61  * Revision 2.1  1995/03/21  14:38:49  john
62  * Ifdef'd out the NETWORK code.
63  *
64  * Revision 2.0  1995/02/27  11:27:59  john
65  * New version 2.0, which has no anonymous unions, builds with
66  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
67  *
68  * Revision 1.57  1995/02/13  20:34:55  john
69  * Lintized
70  *
71  * Revision 1.56  1995/02/13  13:23:24  john
72  * Fixed bug with new player joystick selection.
73  *
74  * Revision 1.55  1995/02/13  12:01:19  john
75  * Fixed bug with joystick throttle still asking for
76  * calibration with new pilots.
77  *
78  * Revision 1.54  1995/02/13  10:29:12  john
79  * Fixed bug with creating new player not resetting everything to default.
80  *
81  * Revision 1.53  1995/02/03  10:58:46  john
82  * Added code to save shareware style saved games into new format...
83  * Also, made new player file format not have the saved game array in it.
84  *
85  * Revision 1.52  1995/02/02  21:09:28  matt
86  * Let player start of level 8 if he made it to level 7 in the shareware
87  *
88  * Revision 1.51  1995/02/02  18:50:14  john
89  * Added warning for FCS when new pilot chooses.
90  *
91  * Revision 1.50  1995/02/02  11:21:34  john
92  * Made joystick calibrate when new user selects.
93  *
94  * Revision 1.49  1995/02/01  18:06:38  rob
95  * Put defaults macros into descent.tex
96  *
97  * Revision 1.48  1995/01/25  14:37:53  john
98  * Made joystick only prompt for calibration once...
99  *
100  * Revision 1.47  1995/01/24  19:37:12  matt
101  * Took out incorrect mprintf
102  *
103  * Revision 1.46  1995/01/22  18:57:22  matt
104  * Made player highest level work with missions
105  *
106  * Revision 1.45  1995/01/21  16:36:05  matt
107  * Made starting level system work for now, pending integration with
108  * mission code.
109  *
110  * Revision 1.44  1995/01/20  22:47:32  matt
111  * Mission system implemented, though imcompletely
112  *
113  * Revision 1.43  1995/01/04  14:58:39  rob
114  * Fixed for shareware build.
115  *
116  * Revision 1.42  1995/01/04  11:36:43  rob
117  * Added compatibility with older shareware pilot files.
118  *
119  * Revision 1.41  1995/01/03  11:01:58  rob
120  * fixed a default macro.
121  *
122  * Revision 1.40  1995/01/03  10:44:06  rob
123  * Added default taunt macros.
124  *
125  * Revision 1.39  1994/12/13  10:01:16  allender
126  * pop up message box when unable to correctly save player file
127  *
128  * Revision 1.38  1994/12/12  11:37:14  matt
129  * Fixed auto leveling defaults & saving
130  *
131  * Revision 1.37  1994/12/12  00:26:59  matt
132  * Added support for no-levelling option
133  *
134  * Revision 1.36  1994/12/10  19:09:54  matt
135  * Added assert for valid player number when loading game
136  *
137  * Revision 1.35  1994/12/08  10:53:07  rob
138  * Fixed a bug in highest_level tracking.
139  *
140  * Revision 1.34  1994/12/08  10:01:36  john
141  * Changed the way the player callsign stuff works.
142  *
143  * Revision 1.33  1994/12/07  18:30:38  rob
144  * Load highest level along with player (used to be only if higher)
145  * Capped at LAST_LEVEL in case a person loads a registered player in shareware.
146  *
147  * Revision 1.32  1994/12/03  16:01:12  matt
148  * When player file has bad version, force player to choose another
149  *
150  * Revision 1.31  1994/12/02  19:54:00  yuan
151  * Localization.
152  *
153  * Revision 1.30  1994/12/02  11:01:36  yuan
154  * Localization.
155  *
156  * Revision 1.29  1994/11/29  03:46:28  john
157  * Added joystick sensitivity; Added sound channels to detail menu.  Removed -maxchannels
158  * command line arg.
159  *
160  * Revision 1.28  1994/11/29  01:10:23  john
161  * Took out code that allowed new players to
162  * configure keyboard.
163  *
164  * Revision 1.27  1994/11/25  22:47:10  matt
165  * Made saved game descriptions longer
166  *
167  * Revision 1.26  1994/11/22  12:10:42  rob
168  * Fixed file handle left open if player file versions don't
169  * match.
170  *
171  * Revision 1.25  1994/11/21  19:35:30  john
172  * Replaced calls to joy_init with if (joy_present)
173  *
174  * Revision 1.24  1994/11/21  17:29:34  matt
175  * Cleaned up sequencing & game saving for secret levels
176  *
177  * Revision 1.23  1994/11/21  11:10:01  john
178  * Fixed bug with read-only .plr file making the config file
179  * not update.
180  *
181  * Revision 1.22  1994/11/20  19:03:08  john
182  * Fixed bug with if not having a joystick, default
183  * player input device is cyberman.
184  *
185  * Revision 1.21  1994/11/17  12:24:07  matt
186  * Made an array the right size, to fix error loading games
187  *
188  * Revision 1.20  1994/11/14  17:52:54  allender
189  * add call to WriteConfigFile when player files gets written
190  *
191  * Revision 1.19  1994/11/14  17:19:23  rob
192  * Removed gamma, joystick calibration, and sound settings from player file.
193  * Added default difficulty and multi macros.
194  *
195  * Revision 1.18  1994/11/07  14:01:23  john
196  * Changed the gamma correction sequencing.
197  *
198  * Revision 1.17  1994/11/05  17:22:49  john
199  * Fixed lots of sequencing problems with newdemo stuff.
200  *
201  * Revision 1.16  1994/11/01  16:40:11  john
202  * Added Gamma correction.
203  *
204  * Revision 1.15  1994/10/24  19:56:50  john
205  * Made the new user setup prompt for config options.
206  *
207  * Revision 1.14  1994/10/24  17:44:21  john
208  * Added stereo channel reversing.
209  *
210  * Revision 1.13  1994/10/24  16:05:12  matt
211  * Improved handling of player names that are the names of DOS devices
212  *
213  * Revision 1.12  1994/10/22  00:08:51  matt
214  * Fixed up problems with bonus & game sequencing
215  * Player doesn't get credit for hostages unless he gets them out alive
216  *
217  * Revision 1.11  1994/10/19  19:59:57  john
218  * Added bonus points at the end of level based on skill level.
219  *
220  * Revision 1.10  1994/10/19  15:14:34  john
221  * Took % hits out of player structure, made %kills work properly.
222  *
223  * Revision 1.9  1994/10/19  12:44:26  john
224  * Added hours field to player structure.
225  *
226  * Revision 1.8  1994/10/17  17:24:34  john
227  * Added starting_level to player struct.
228  *
229  * Revision 1.7  1994/10/17  13:07:15  john
230  * Moved the descent.cfg info into the player config file.
231  *
232  * Revision 1.6  1994/10/09  14:54:31  matt
233  * Made player cockpit state & window size save/restore with saved games & automap
234  *
235  * Revision 1.5  1994/10/08  23:08:09  matt
236  * Added error check & handling for game load/save disk io
237  *
238  * Revision 1.4  1994/10/05  17:40:54  rob
239  * Bumped save_file_version to 5 due to change in player.h
240  *
241  * Revision 1.3  1994/10/03  23:00:54  matt
242  * New file version for shorter callsigns
243  *
244  * Revision 1.2  1994/09/28  17:25:05  matt
245  * Added first draft of game save/load system
246  *
247  * Revision 1.1  1994/09/27  14:39:12  matt
248  * Initial revision
249  *
250  */
251
252 #ifdef HAVE_CONFIG_H
253 #include <conf.h>
254 #endif
255
256 #ifdef WINDOWS
257 #include "desw.h"
258 #include <mmsystem.h>
259 #endif
260
261 #include <stdio.h>
262 #include <string.h>
263 #include <unistd.h>
264 #include <errno.h>
265
266 #include "error.h"
267
268 #include "pa_enabl.h"
269 #include "strutil.h"
270 #include "game.h"
271 #include "gameseq.h"
272 #include "player.h"
273 #include "playsave.h"
274 #include "joy.h"
275 #include "kconfig.h"
276 #include "digi.h"
277 #include "newmenu.h"
278 #include "joydefs.h"
279 #include "palette.h"
280 #include "multi.h"
281 #include "menu.h"
282 #include "config.h"
283 #include "text.h"
284 #include "mono.h"
285 #include "state.h"
286 #include "gauges.h"
287 #include "screens.h"
288 #include "powerup.h"
289 #include "makesig.h"
290 #include "byteswap.h"
291 #include "fileutil.h"
292
293 #define SAVE_FILE_ID                    MAKE_SIG('D','P','L','R')
294
295 #ifdef MACINTOSH
296         #include <Files.h>
297         #include <Errors.h>                     // mac doesn't have "normal" error numbers -- must use mac equivs
298         #ifndef ENOENT
299                 #define ENOENT fnfErr
300         #endif
301         #ifdef POLY_ACC
302                 #include "poly_acc.h"
303         #endif
304         #include "isp.h"
305 #endif
306
307 int get_lifetime_checksum (int a,int b);
308
309 typedef struct hli {
310         char    shortname[9];
311         ubyte   level_num;
312 } hli;
313
314 int n_highest_levels;
315
316 hli highest_levels[MAX_MISSIONS];
317
318 #define PLAYER_FILE_VERSION     24                      //increment this every time the player file changes
319
320 //version 5  ->  6: added new highest level information
321 //version 6  ->  7: stripped out the old saved_game array.
322 //version 7  ->  8: added reticle flag, & window size
323 //version 8  ->  9: removed player_struct_version
324 //version 9  -> 10: added default display mode
325 //version 10 -> 11: added all toggles in toggle menu
326 //version 11 -> 12: added weapon ordering
327 //version 12 -> 13: added more keys
328 //version 13 -> 14: took out marker key
329 //version 14 -> 15: added guided in big window
330 //version 15 -> 16: added small windows in cockpit
331 //version 16 -> 17: ??
332 //version 17 -> 18: save guidebot name
333 //version 18 -> 19: added automap-highres flag
334 //version 19 -> 20: added kconfig data for windows joysticks
335 //version 20 -> 21: save seperate config types for DOS & Windows
336 //version 21 -> 22: save lifetime netstats 
337 //version 22 -> 23: ??
338 //version 23 -> 24: add name of joystick for windows version.
339
340 #define COMPATIBLE_PLAYER_FILE_VERSION          17
341
342
343 int Default_leveling_on=1;
344 extern ubyte SecondaryOrder[],PrimaryOrder[];
345 extern void InitWeaponOrdering();
346
347 #ifdef MACINTOSH
348 extern ubyte default_firebird_settings[];
349 extern ubyte default_mousestick_settings[];
350 #endif
351
352 int new_player_config()
353 {
354         int nitems;
355         int i,j,control_choice;
356         newmenu_item m[8];
357    int mct=CONTROL_MAX_TYPES;
358  
359    #ifndef WINDOWS
360          mct--;
361         #endif
362
363    InitWeaponOrdering ();               //setup default weapon priorities 
364
365 #if defined(MACINTOSH) && defined(USE_ISP)
366         if (!ISpEnabled())
367         {
368 #endif
369 RetrySelection:
370                 #if !defined(MACINTOSH) && !defined(WINDOWS)
371                 for (i=0; i<mct; i++ )  {
372                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
373                 }
374                 #elif defined(WINDOWS)
375                         m[0].type = NM_TYPE_MENU; m[0].text = CONTROL_TEXT(0);
376                         m[1].type = NM_TYPE_MENU; m[1].text = CONTROL_TEXT(5);
377                         m[2].type = NM_TYPE_MENU; m[2].text = CONTROL_TEXT(7);
378                         i = 3;
379                 #else
380                 for (i = 0; i < 6; i++) {
381                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
382                 }
383                 m[4].text = "Gravis Firebird/Mousetick II";
384                 m[3].text = "Thrustmaster";
385                 #endif
386                 
387                 nitems = i;
388                 m[0].text = TXT_CONTROL_KEYBOARD;
389         
390                 #ifdef WINDOWS
391                         if (Config_control_type==CONTROL_NONE) control_choice = 0;
392                         else if (Config_control_type == CONTROL_MOUSE) control_choice = 1;
393                         else if (Config_control_type == CONTROL_WINJOYSTICK) control_choice = 2;
394                         else control_choice = 0;
395                 #else
396                         control_choice = Config_control_type;                           // Assume keyboard
397                 #endif
398         
399                 #ifndef APPLE_DEMO
400                         control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
401                 #else
402                         control_choice = 0;
403                 #endif
404                 
405                 if ( control_choice < 0 )
406                         return 0;
407
408 #if defined(MACINTOSH) && defined(USE_ISP)
409         }
410         else    // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
411         {
412                 control_choice = 0;
413         }
414 #endif
415
416         for (i=0;i<CONTROL_MAX_TYPES; i++ )
417                 for (j=0;j<MAX_CONTROLS; j++ )
418                         kconfig_settings[i][j] = default_kconfig_settings[i][j];
419         kc_set_controls();
420
421         Config_control_type = control_choice;
422
423 #ifdef WINDOWS
424         if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
425         else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
426
427 //      if (Config_control_type == CONTROL_WINJOYSTICK) 
428 //              joydefs_calibrate();
429 #else
430         #ifndef MACINTOSH
431         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
432                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
433                 if (i==0) goto RetrySelection;
434         }
435         
436         if ( (Config_control_type>0) &&         (Config_control_type<5))        {
437                 joydefs_calibrate();
438         }
439         #else           // some macintosh only stuff here
440         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
441                 extern char *tm_warning;
442                 
443                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
444                 if (i==0) goto RetrySelection;
445         } else  if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO )     {
446                 extern char *ch_warning;
447
448                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
449                 if (i==0) goto RetrySelection;
450         } else  if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD )      {
451                 extern char *ms_warning;
452
453                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
454                 if (i==0) goto RetrySelection;
455                 // stupid me -- get real default setting for either mousestick or firebird
456                 joydefs_set_type( Config_control_type );
457                 if (joy_have_firebird())
458                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
459                                 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
460                 else
461                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
462                                 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
463                 kc_set_controls();              // reset the joystick control
464         }
465         if ( (Config_control_type>0) && (Config_control_type<5)  ) {
466                 joydefs_set_type( Config_control_type );
467                 joydefs_calibrate();
468         }
469
470         #endif
471 #endif
472         
473         Player_default_difficulty = 1;
474         Auto_leveling_on = Default_leveling_on = 1;
475         n_highest_levels = 1;
476         highest_levels[0].shortname[0] = 0;                     //no name for mission 0
477         highest_levels[0].level_num = 1;                                //was highest level in old struct
478         Config_joystick_sensitivity = 8;
479         Cockpit_3d_view[0]=CV_NONE;
480         Cockpit_3d_view[1]=CV_NONE;
481
482         // Default taunt macros
483         #ifdef NETWORK
484         strcpy(Network_message_macro[0], "Why can't we all just get along?");
485         strcpy(Network_message_macro[1], "Hey, I got a present for ya");
486         strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
487         strcpy(Network_message_macro[3], "This one's headed for Uranus");
488         Netlife_kills=0; Netlife_killed=0;      
489         #endif
490         
491         #ifdef MACINTOSH
492                 #ifdef POLY_ACC
493                         if (PAEnabled)
494                         {
495                                 Scanline_double = 0;            // no pixel doubling for poly_acc
496                         }
497                         else
498                         {
499                                 Scanline_double = 1;            // should be default for new player
500                         }
501                 #else
502                         Scanline_double = 1;                    // should be default for new player
503                 #endif
504         #endif
505
506         return 1;
507 }
508
509 extern int Guided_in_big_window,Automap_always_hires;
510
511 //this length must match the value in escort.c
512 #define GUIDEBOT_NAME_LEN 9
513 extern char guidebot_name[];
514 extern char real_guidebot_name[];
515
516 WIN(extern char win95_current_joyname[]);
517
518 ubyte control_type_dos,control_type_win;
519
520 //read in the player's saved games.  returns errno (0 == no error)
521 int read_player_file()
522 {
523         #ifdef MACINTOSH
524         char filename[FILENAME_LEN+15];
525         #else
526         char filename[FILENAME_LEN];
527         #endif
528         FILE *file;
529         int errno_ret = EZERO;
530         int id,player_file_version,i;
531         int rewrite_it=0;
532         int swap = 0;
533
534         Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
535
536 #ifndef MACINTOSH
537         sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
538 #else
539         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
540 #endif
541         file = fopen(filename,"rb");
542
543 #ifndef MACINTOSH
544         //check filename
545         if (file && isatty(fileno(file))) {
546                 //if the callsign is the name of a tty device, prepend a char
547                 fclose(file);
548                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
549                 file = fopen(filename,"rb");
550         }
551 #endif
552
553         if (!file) {
554                 return errno;
555         }
556
557         id = file_read_int(file);
558
559         // SWAPINT added here because old versions of d2x
560         // used the wrong byte order.
561         if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
562                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
563                 fclose(file);
564                 return -1;
565         }
566
567         player_file_version = file_read_short(file);
568
569         if (player_file_version > 255) // bigendian file?
570                 swap = 1;
571
572         if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
573                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
574                 fclose(file);
575                 return -1;
576         }
577
578         Game_window_w                                   = file_read_short(file);
579         Game_window_h                                   = file_read_short(file);
580
581         if (swap) {
582                 player_file_version = SWAPSHORT(player_file_version);
583                 Game_window_w = SWAPSHORT(Game_window_w);
584                 Game_window_h = SWAPSHORT(Game_window_h);
585         }
586
587         Player_default_difficulty       = file_read_byte(file);
588         Default_leveling_on                     = file_read_byte(file);
589         Reticle_on                                              = file_read_byte(file);
590         Cockpit_mode                                    = file_read_byte(file);
591         #ifdef POLY_ACC
592          #ifdef PA_3DFX_VOODOO
593                 if (Cockpit_mode<2)
594                 {
595                 Cockpit_mode=2;
596                         Game_window_w  = 640;
597                         Game_window_h   = 480;
598                 }
599          #endif
600         #endif
601  
602         Default_display_mode                    = file_read_byte(file);
603         Missile_view_enabled                    = file_read_byte(file);
604         Headlight_active_default        = file_read_byte(file);
605         Guided_in_big_window                    = file_read_byte(file);
606
607         if (player_file_version >= 19)
608                 Automap_always_hires                    = file_read_byte(file);
609           
610         Auto_leveling_on = Default_leveling_on;
611
612         //read new highest level info
613
614         n_highest_levels = file_read_short(file);
615         if (swap)
616                 n_highest_levels = SWAPSHORT(n_highest_levels);
617
618         if (fread(highest_levels,sizeof(hli),n_highest_levels,file) != n_highest_levels) {
619                 errno_ret                       = errno;
620                 fclose(file);
621                 return errno_ret;
622         }
623
624         //read taunt macros
625         {
626 #ifdef NETWORK
627                 int i,len;
628
629                 len                     = MAX_MESSAGE_LEN;
630
631                 for (i                  = 0; i < 4; i++)
632                         if (fread(Network_message_macro[i], len, 1, file) != 1)
633                                 {errno_ret                      = errno; break;}
634 #else
635                 char dummy[4][MAX_MESSAGE_LEN];
636                 fread(dummy, MAX_MESSAGE_LEN, 4, file);
637
638 #endif
639         }
640
641         //read kconfig data
642         {
643                 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
644
645                 if (fread( kconfig_settings, MAX_CONTROLS*n_control_types, 1, file )!=1)
646                         errno_ret=errno;
647                 else if (fread((ubyte *)&control_type_dos, sizeof(ubyte), 1, file )!=1)
648                         errno_ret=errno;
649                 else if (player_file_version >= 21 && fread((ubyte *)&control_type_win, sizeof(ubyte), 1, file )!=1)
650                         errno_ret=errno;
651                 else if (fread(&Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
652                         errno_ret=errno;
653
654                 #ifdef WINDOWS
655                 Config_control_type = control_type_win;
656                 #else
657                 Config_control_type = control_type_dos;
658                 #endif
659                 
660                 #ifdef MACINTOSH
661                 joydefs_set_type(Config_control_type);
662                 #endif
663
664                 for (i=0;i<11;i++)
665                  {
666                         PrimaryOrder[i]=file_read_byte (file);
667                         SecondaryOrder[i]=file_read_byte(file);
668                  }
669                 
670                 if (player_file_version>=16)
671                  {
672                   Cockpit_3d_view[0]=file_read_int(file);
673                   Cockpit_3d_view[1]=file_read_int(file);
674                   if (swap) {
675                           Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
676                           Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
677                   }
678                  }      
679                 
680                   
681                 if (errno_ret==EZERO)   {
682                         kc_set_controls();
683                 }
684
685         }
686
687    if (player_file_version>=22)
688          {
689 #ifdef NETWORK
690                 Netlife_kills=file_read_int (file);
691                 Netlife_killed=file_read_int (file);
692                 if (swap) {
693                         Netlife_kills = SWAPINT(Netlife_kills);
694                         Netlife_killed = SWAPINT(Netlife_killed);
695                 }
696 #else
697                 file_read_int(file); file_read_int(file);
698 #endif
699          }
700 #ifdef NETWORK
701    else
702          {
703                  Netlife_kills=0; Netlife_killed=0;
704          }
705 #endif
706
707         if (player_file_version>=23)
708          {
709           i=file_read_int (file);       
710           if (swap)
711                   i = SWAPINT(i);
712 #ifdef NETWORK
713           mprintf ((0,"Reading: lifetime checksum is %d\n",i));
714           if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
715                 {
716                  Netlife_kills=0; Netlife_killed=0;
717                  nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
718                  rewrite_it=1;
719                 }
720 #endif
721          }
722
723         //read guidebot name
724         if (player_file_version >= 18)
725                 file_read_string(guidebot_name,file);
726         else
727                 strcpy(guidebot_name,"GUIDE-BOT");
728
729         strcpy(real_guidebot_name,guidebot_name);
730
731         {
732                 char buf[128];
733
734         #ifdef WINDOWS
735                 joy95_get_name(JOYSTICKID1, buf, 127);
736                 if (player_file_version >= 24) 
737                         file_read_string(win95_current_joyname, file);
738                 else
739                         strcpy(win95_current_joyname, "Old Player File");
740                 
741                 mprintf((0, "Detected joystick: %s\n", buf));
742                 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
743
744                 if (strcmp(win95_current_joyname, buf)) {
745                         for (i = 0; i < MAX_CONTROLS; i++)
746                                 kconfig_settings[CONTROL_WINJOYSTICK][i] = 
747                                         default_kconfig_settings[CONTROL_WINJOYSTICK][i];
748                 }        
749         #else
750                 if (player_file_version >= 24) 
751                         file_read_string(buf, file);                    // Just read it in fpr DPS.
752         #endif
753         }
754
755         if (fclose(file) && errno_ret==EZERO)
756                 errno_ret                       = errno;
757
758         if (rewrite_it)
759          write_player_file();
760
761         return errno_ret;
762
763 }
764
765
766 //finds entry for this level in table.  if not found, returns ptr to 
767 //empty entry.  If no empty entries, takes over last one 
768 int find_hli_entry()
769 {
770         int i;
771
772         for (i=0;i<n_highest_levels;i++)
773                 if (!stricmp(highest_levels[i].shortname,Mission_list[Current_mission_num].filename))
774                         break;
775
776         if (i==n_highest_levels) {              //not found.  create entry
777
778                 if (i==MAX_MISSIONS)
779                         i--;            //take last entry
780                 else
781                         n_highest_levels++;
782
783                 strcpy(highest_levels[i].shortname,Mission_list[Current_mission_num].filename);
784                 highest_levels[i].level_num                     = 0;
785         }
786
787         return i;
788 }
789
790 //set a new highest level for player for this mission
791 void set_highest_level(int levelnum)
792 {
793         int ret,i;
794
795         if ((ret=read_player_file()) != EZERO)
796                 if (ret != ENOENT)              //if file doesn't exist, that's ok
797                         return;
798
799         i                       = find_hli_entry();
800
801         if (levelnum > highest_levels[i].level_num)
802                 highest_levels[i].level_num                     = levelnum;
803
804         write_player_file();
805 }
806
807 //gets the player's highest level from the file for this mission
808 int get_highest_level(void)
809 {
810         int i;
811         int highest_saturn_level                        = 0;
812         read_player_file();
813 #ifndef SATURN
814         if (strlen(Mission_list[Current_mission_num].filename)==0 )     {
815                 for (i=0;i<n_highest_levels;i++)
816                         if (!stricmp(highest_levels[i].shortname, "DESTSAT"))   //      Destination Saturn.
817                                 highest_saturn_level                    = highest_levels[i].level_num; 
818         }
819 #endif
820    i                    = highest_levels[find_hli_entry()].level_num;
821         if ( highest_saturn_level > i )
822         i                       = highest_saturn_level;
823         return i;
824 }
825
826 extern int Cockpit_mode_save;
827
828 //write out player's saved games.  returns errno (0 == no error)
829 int write_player_file()
830 {
831         #ifdef MACINTOSH
832         char filename[FILENAME_LEN+15];
833         #else
834         char filename[FILENAME_LEN];            // because of ":Players:" path
835         #endif
836         FILE *file;
837         int errno_ret,i;
838
839 //      #ifdef APPLE_DEMO               // no saving of player files in Apple OEM version
840 //      return 0;
841 //      #endif
842
843         errno_ret                       = WriteConfigFile();
844
845 #ifndef MACINTOSH
846         sprintf(filename,"%s.plr",Players[Player_num].callsign);
847 #else
848         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
849 #endif
850         file                    = fopen(filename,"wb");
851
852 #ifndef MACINTOSH
853         //check filename
854         if (file && isatty(fileno(file))) {
855
856                 //if the callsign is the name of a tty device, prepend a char
857
858                 fclose(file);
859                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
860                 file                    = fopen(filename,"wb");
861         }
862 #endif
863
864         if (!file)
865               return errno;
866
867         errno_ret                       = EZERO;
868
869         //Write out player's info
870         file_write_int(SAVE_FILE_ID,file);
871         file_write_short(PLAYER_FILE_VERSION,file);
872
873         file_write_short(Game_window_w,file);
874         file_write_short(Game_window_h,file);
875
876         file_write_byte(Player_default_difficulty,file);
877         file_write_byte(Auto_leveling_on,file);
878         file_write_byte(Reticle_on,file);
879         file_write_byte((Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode,file);   //if have saved mode, write it instead of letterbox/rear view
880         file_write_byte(Default_display_mode,file);
881         file_write_byte(Missile_view_enabled,file);
882         file_write_byte(Headlight_active_default,file);
883         file_write_byte(Guided_in_big_window,file);
884         file_write_byte(Automap_always_hires,file);
885
886         //write higest level info
887         file_write_short(n_highest_levels,file);
888         if ((fwrite(highest_levels, sizeof(hli), n_highest_levels, file) != n_highest_levels)) {
889                 errno_ret                       = errno;
890                 fclose(file);
891                 return errno_ret;
892         }
893
894         #ifdef NETWORK
895         if ((fwrite(Network_message_macro, MAX_MESSAGE_LEN, 4, file) != 4)) {
896                 errno_ret                       = errno;
897                 fclose(file);
898                 return errno_ret;
899         }
900         #else
901         fseek( file, MAX_MESSAGE_LEN * 4, SEEK_CUR );
902         #endif
903
904         //write kconfig info
905         {
906
907                 #ifdef WINDOWS
908                 control_type_win = Config_control_type;
909                 #else
910                 control_type_dos = Config_control_type;
911                 #endif
912
913                 if (fwrite( kconfig_settings, MAX_CONTROLS*CONTROL_MAX_TYPES, 1, file )!=1)
914                         errno_ret=errno;
915                 else if (fwrite( &control_type_dos, sizeof(ubyte), 1, file )!=1)
916                         errno_ret=errno;
917                 else if (fwrite( &control_type_win, sizeof(ubyte), 1, file )!=1)
918                         errno_ret=errno;
919                 else if (fwrite( &Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
920                         errno_ret=errno;
921
922       for (i=0;i<11;i++)
923        {
924         fwrite (&PrimaryOrder[i],sizeof(ubyte),1,file);
925         fwrite (&SecondaryOrder[i],sizeof(ubyte),1,file);
926        }
927                 file_write_int (Cockpit_3d_view[0],file);
928                 file_write_int (Cockpit_3d_view[1],file);
929
930 #ifdef NETWORK
931                 file_write_int (Netlife_kills,file);
932                 file_write_int (Netlife_killed,file);
933                 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
934                 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
935 #else
936            file_write_int(0, file);
937            file_write_int(0, file);
938            i = get_lifetime_checksum (0, 0);
939 #endif
940            file_write_int (i,file);
941         }
942
943         //write guidebot name
944         file_write_string(real_guidebot_name,file);
945
946         {
947                 char buf[128];
948                 #ifdef WINDOWS
949                 joy95_get_name(JOYSTICKID1, buf, 127);
950                 #else
951                 strcpy(buf, "DOS joystick");
952                 #endif
953                 file_write_string(buf, file);           // Write out current joystick for player.
954         }
955
956         if (fclose(file))
957                 errno_ret                       = errno;
958
959         if (errno_ret != EZERO) {
960                 remove(filename);                       //delete bogus file
961                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s",TXT_ERROR_WRITING_PLR, strerror(errno_ret));
962         }
963
964         #ifdef MACINTOSH                // set filetype and creator for playerfile
965         {
966                 FInfo finfo;
967                 Str255 pfilename;
968                 OSErr err;
969
970                 strcpy(pfilename, filename);
971                 c2pstr(pfilename);
972                 err = HGetFInfo(0, 0, pfilename, &finfo);
973                 finfo.fdType = 'PLYR';
974                 finfo.fdCreator = 'DCT2';
975                 err = HSetFInfo(0, 0, pfilename, &finfo);
976         }
977         #endif
978
979         return errno_ret;
980
981 }
982
983 //update the player's highest level.  returns errno (0 == no error)
984 int update_player_file()
985 {
986         int ret;
987
988         if ((ret=read_player_file()) != EZERO)
989                 if (ret != ENOENT)              //if file doesn't exist, that's ok
990                         return ret;
991
992         return write_player_file();
993 }
994
995 int get_lifetime_checksum (int a,int b)
996  {
997   int num;
998
999   // confusing enough to beat amateur disassemblers? Lets hope so
1000
1001   num=(a<<8 ^ b);
1002   num^=(a | b);
1003   num*=num>>2;
1004   return (num);
1005  }
1006   
1007