]> icculus.org git repositories - btb/d2x.git/blob - main/playsave.c
bigendian networking fixes
[btb/d2x.git] / main / playsave.c
1 /* $Id: playsave.c,v 1.15 2003-08-02 06:45:42 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 load & save player's settings (*.plr file)
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 "escort.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 short n_highest_levels;
315
316 hli highest_levels[MAX_MISSIONS];
317
318 #define PLAYER_FILE_VERSION     25                      //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 //version 24 -> 25: add d2x keys array
340
341 #define COMPATIBLE_PLAYER_FILE_VERSION          17
342
343
344 int Default_leveling_on=1;
345 extern ubyte SecondaryOrder[],PrimaryOrder[];
346 extern void InitWeaponOrdering();
347
348 #ifdef MACINTOSH
349 extern ubyte default_firebird_settings[];
350 extern ubyte default_mousestick_settings[];
351 #endif
352
353 int new_player_config()
354 {
355         int nitems;
356         int i,j,control_choice;
357         newmenu_item m[8];
358    int mct=CONTROL_MAX_TYPES;
359  
360    #ifndef WINDOWS
361          mct--;
362         #endif
363
364    InitWeaponOrdering ();               //setup default weapon priorities 
365
366 #if defined(MACINTOSH) && defined(USE_ISP)
367         if (!ISpEnabled())
368         {
369 #endif
370 RetrySelection:
371                 #if !defined(MACINTOSH) && !defined(WINDOWS)
372                 for (i=0; i<mct; i++ )  {
373                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
374                 }
375                 #elif defined(WINDOWS)
376                         m[0].type = NM_TYPE_MENU; m[0].text = CONTROL_TEXT(0);
377                         m[1].type = NM_TYPE_MENU; m[1].text = CONTROL_TEXT(5);
378                         m[2].type = NM_TYPE_MENU; m[2].text = CONTROL_TEXT(7);
379                         i = 3;
380                 #else
381                 for (i = 0; i < 6; i++) {
382                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
383                 }
384                 m[4].text = "Gravis Firebird/Mousetick II";
385                 m[3].text = "Thrustmaster";
386                 #endif
387                 
388                 nitems = i;
389                 m[0].text = TXT_CONTROL_KEYBOARD;
390         
391                 #ifdef WINDOWS
392                         if (Config_control_type==CONTROL_NONE) control_choice = 0;
393                         else if (Config_control_type == CONTROL_MOUSE) control_choice = 1;
394                         else if (Config_control_type == CONTROL_WINJOYSTICK) control_choice = 2;
395                         else control_choice = 0;
396                 #else
397                         control_choice = Config_control_type;                           // Assume keyboard
398                 #endif
399         
400                 #ifndef APPLE_DEMO
401                         control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
402                 #else
403                         control_choice = 0;
404                 #endif
405                 
406                 if ( control_choice < 0 )
407                         return 0;
408
409 #if defined(MACINTOSH) && defined(USE_ISP)
410         }
411         else    // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
412         {
413                 control_choice = 0;
414         }
415 #endif
416
417         for (i=0;i<CONTROL_MAX_TYPES; i++ )
418                 for (j=0;j<MAX_CONTROLS; j++ )
419                         kconfig_settings[i][j] = default_kconfig_settings[i][j];
420         //added on 2/4/99 by Victor Rachels for new keys
421         for(i=0; i < MAX_D2X_CONTROLS; i++)
422                 kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
423         //end this section addition - VR
424         kc_set_controls();
425
426         Config_control_type = control_choice;
427
428 #ifdef WINDOWS
429         if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
430         else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
431
432 //      if (Config_control_type == CONTROL_WINJOYSTICK) 
433 //              joydefs_calibrate();
434 #else
435         #ifndef MACINTOSH
436         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
437                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
438                 if (i==0) goto RetrySelection;
439         }
440         
441         if ( (Config_control_type>0) &&         (Config_control_type<5))        {
442                 joydefs_calibrate();
443         }
444         #else           // some macintosh only stuff here
445         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
446                 extern char *tm_warning;
447                 
448                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
449                 if (i==0) goto RetrySelection;
450         } else  if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO )     {
451                 extern char *ch_warning;
452
453                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
454                 if (i==0) goto RetrySelection;
455         } else  if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD )      {
456                 extern char *ms_warning;
457
458                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
459                 if (i==0) goto RetrySelection;
460                 // stupid me -- get real default setting for either mousestick or firebird
461                 joydefs_set_type( Config_control_type );
462                 if (joy_have_firebird())
463                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
464                                 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
465                 else
466                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
467                                 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
468                 kc_set_controls();              // reset the joystick control
469         }
470         if ( (Config_control_type>0) && (Config_control_type<5)  ) {
471                 joydefs_set_type( Config_control_type );
472                 joydefs_calibrate();
473         }
474
475         #endif
476 #endif
477         
478         Player_default_difficulty = 1;
479         Auto_leveling_on = Default_leveling_on = 1;
480         n_highest_levels = 1;
481         highest_levels[0].shortname[0] = 0;                     //no name for mission 0
482         highest_levels[0].level_num = 1;                                //was highest level in old struct
483         Config_joystick_sensitivity = 8;
484         Cockpit_3d_view[0]=CV_NONE;
485         Cockpit_3d_view[1]=CV_NONE;
486
487         // Default taunt macros
488         #ifdef NETWORK
489         strcpy(Network_message_macro[0], "Why can't we all just get along?");
490         strcpy(Network_message_macro[1], "Hey, I got a present for ya");
491         strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
492         strcpy(Network_message_macro[3], "This one's headed for Uranus");
493         Netlife_kills=0; Netlife_killed=0;      
494         #endif
495         
496         #ifdef MACINTOSH
497                 #ifdef POLY_ACC
498                         if (PAEnabled)
499                         {
500                                 Scanline_double = 0;            // no pixel doubling for poly_acc
501                         }
502                         else
503                         {
504                                 Scanline_double = 1;            // should be default for new player
505                         }
506                 #else
507                         Scanline_double = 1;                    // should be default for new player
508                 #endif
509         #endif
510
511         return 1;
512 }
513
514 extern int Guided_in_big_window,Automap_always_hires;
515
516 //this length must match the value in escort.c
517 #define GUIDEBOT_NAME_LEN 9
518 extern char guidebot_name[];
519 extern char real_guidebot_name[];
520
521 WIN(extern char win95_current_joyname[]);
522
523 ubyte control_type_dos,control_type_win;
524
525 //read in the player's saved games.  returns errno (0 == no error)
526 int read_player_file()
527 {
528         #ifdef MACINTOSH
529         char filename[FILENAME_LEN+15];
530         #else
531         char filename[FILENAME_LEN];
532         #endif
533         CFILE *file;
534         int errno_ret = EZERO;
535         int id, i;
536         short player_file_version;
537         int rewrite_it=0;
538         int swap = 0;
539
540         Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
541
542 #ifndef MACINTOSH
543         sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
544 #else
545         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
546 #endif
547         file = cfopen(filename, "rb");
548
549 #if 0 //ndef MACINTOSH
550         //check filename
551         if (file && isatty(fileno(file))) {
552                 //if the callsign is the name of a tty device, prepend a char
553                 fclose(file);
554                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
555                 file = fopen(filename,"rb");
556         }
557 #endif
558
559         if (!file) {
560                 return errno;
561         }
562
563         id = cfile_read_int(file);
564
565         // SWAPINT added here because old versions of d2x
566         // used the wrong byte order.
567         if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
568                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
569                 cfclose(file);
570                 return -1;
571         }
572
573         player_file_version = cfile_read_short(file);
574
575         if (player_file_version > 255) // bigendian file?
576                 swap = 1;
577
578         if (swap)
579                 player_file_version = SWAPSHORT(player_file_version);
580
581         if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
582                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
583                 cfclose(file);
584                 return -1;
585         }
586
587         Game_window_w = cfile_read_short(file);
588         Game_window_h = cfile_read_short(file);
589
590         if (swap) {
591                 Game_window_w = SWAPSHORT(Game_window_w);
592                 Game_window_h = SWAPSHORT(Game_window_h);
593         }
594
595         Player_default_difficulty = cfile_read_byte(file);
596         Default_leveling_on       = cfile_read_byte(file);
597         Reticle_on                = cfile_read_byte(file);
598         Cockpit_mode              = cfile_read_byte(file);
599         #ifdef POLY_ACC
600          #ifdef PA_3DFX_VOODOO
601                 if (Cockpit_mode<2)
602                 {
603                 Cockpit_mode=2;
604                         Game_window_w  = 640;
605                         Game_window_h   = 480;
606                 }
607          #endif
608         #endif
609  
610         Default_display_mode     = cfile_read_byte(file);
611         Missile_view_enabled     = cfile_read_byte(file);
612         Headlight_active_default = cfile_read_byte(file);
613         Guided_in_big_window     = cfile_read_byte(file);
614
615         if (player_file_version >= 19)
616                 Automap_always_hires = cfile_read_byte(file);
617           
618         Auto_leveling_on = Default_leveling_on;
619
620         //read new highest level info
621
622         n_highest_levels = cfile_read_short(file);
623         if (swap)
624                 n_highest_levels = SWAPSHORT(n_highest_levels);
625         Assert(n_highest_levels <= MAX_MISSIONS);
626
627         if (cfread(highest_levels, sizeof(hli), n_highest_levels, file) != n_highest_levels)
628         {
629                 errno_ret                       = errno;
630                 cfclose(file);
631                 return errno_ret;
632         }
633
634         //read taunt macros
635         {
636 #ifdef NETWORK
637                 int i,len;
638
639                 len                     = MAX_MESSAGE_LEN;
640
641                 for (i                  = 0; i < 4; i++)
642                         if (cfread(Network_message_macro[i], len, 1, file) != 1)
643                                 {errno_ret                      = errno; break;}
644 #else
645                 char dummy[4][MAX_MESSAGE_LEN];
646
647                 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
648 #endif
649         }
650
651         //read kconfig data
652         {
653                 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
654
655                 if (cfread( kconfig_settings, MAX_CONTROLS*n_control_types, 1, file ) != 1)
656                         errno_ret=errno;
657                 else if (cfread((ubyte *)&control_type_dos, sizeof(ubyte), 1, file ) != 1)
658                         errno_ret=errno;
659                 else if (player_file_version >= 21 && cfread((ubyte *)&control_type_win, sizeof(ubyte), 1, file ) != 1)
660                         errno_ret=errno;
661                 else if (cfread(&Config_joystick_sensitivity, sizeof(ubyte), 1, file ) != 1)
662                         errno_ret=errno;
663
664                 #ifdef WINDOWS
665                 Config_control_type = control_type_win;
666                 #else
667                 Config_control_type = control_type_dos;
668                 #endif
669                 
670                 #ifdef MACINTOSH
671                 joydefs_set_type(Config_control_type);
672                 #endif
673
674                 for (i=0;i<11;i++)
675                 {
676                         PrimaryOrder[i]   = cfile_read_byte(file);
677                         SecondaryOrder[i] = cfile_read_byte(file);
678                 }
679
680                 if (player_file_version>=16)
681                 {
682                         Cockpit_3d_view[0] = cfile_read_int(file);
683                         Cockpit_3d_view[1] = cfile_read_int(file);
684                         if (swap) {
685                                 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
686                                 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
687                         }
688                 }
689                 
690                   
691                 if (errno_ret==EZERO)   {
692                         kc_set_controls();
693                 }
694
695         }
696
697    if (player_file_version>=22)
698          {
699 #ifdef NETWORK
700                 Netlife_kills = cfile_read_int(file);
701                 Netlife_killed = cfile_read_int(file);
702                 if (swap) {
703                         Netlife_kills = SWAPINT(Netlife_kills);
704                         Netlife_killed = SWAPINT(Netlife_killed);
705                 }
706 #else
707                 cfile_read_int(file);
708                 cfile_read_int(file);
709 #endif
710          }
711 #ifdef NETWORK
712    else
713          {
714                  Netlife_kills=0; Netlife_killed=0;
715          }
716 #endif
717
718         if (player_file_version>=23)
719          {
720           i = cfile_read_int(file);
721           if (swap)
722                   i = SWAPINT(i);
723 #ifdef NETWORK
724           mprintf ((0,"Reading: lifetime checksum is %d\n",i));
725           if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
726                 {
727                  Netlife_kills=0; Netlife_killed=0;
728                  nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
729                  rewrite_it=1;
730                 }
731 #endif
732          }
733
734         //read guidebot name
735         if (player_file_version >= 18)
736                 cfile_read_string(guidebot_name, GUIDEBOT_NAME_LEN, file);
737         else
738                 strcpy(guidebot_name,"GUIDE-BOT");
739
740         strcpy(real_guidebot_name,guidebot_name);
741
742         {
743                 char buf[128];
744
745         #ifdef WINDOWS
746                 joy95_get_name(JOYSTICKID1, buf, 127);
747                 if (player_file_version >= 24) 
748                         cfile_read_string(win95_current_joyname, file);
749                 else
750                         strcpy(win95_current_joyname, "Old Player File");
751                 
752                 mprintf((0, "Detected joystick: %s\n", buf));
753                 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
754
755                 if (strcmp(win95_current_joyname, buf)) {
756                         for (i = 0; i < MAX_CONTROLS; i++)
757                                 kconfig_settings[CONTROL_WINJOYSTICK][i] = 
758                                         default_kconfig_settings[CONTROL_WINJOYSTICK][i];
759                 }        
760         #else
761                 if (player_file_version >= 24) 
762                         cfile_read_string(buf, 127, file);      // Just read it in fpr DPS.
763         #endif
764         }
765
766         if (player_file_version >= 25)
767                 cfread(kconfig_d2x_settings, MAX_D2X_CONTROLS, 1, file);
768         else
769                 for(i=0; i < MAX_D2X_CONTROLS; i++)
770                         kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
771
772         if (cfclose(file) && errno_ret == EZERO)
773                 errno_ret                       = errno;
774
775         if (rewrite_it)
776          write_player_file();
777
778         return errno_ret;
779
780 }
781
782
783 //finds entry for this level in table.  if not found, returns ptr to 
784 //empty entry.  If no empty entries, takes over last one 
785 int find_hli_entry()
786 {
787         int i;
788
789         for (i=0;i<n_highest_levels;i++)
790                 if (!stricmp(highest_levels[i].shortname,Mission_list[Current_mission_num].filename))
791                         break;
792
793         if (i==n_highest_levels) {              //not found.  create entry
794
795                 if (i==MAX_MISSIONS)
796                         i--;            //take last entry
797                 else
798                         n_highest_levels++;
799
800                 strcpy(highest_levels[i].shortname,Mission_list[Current_mission_num].filename);
801                 highest_levels[i].level_num                     = 0;
802         }
803
804         return i;
805 }
806
807 //set a new highest level for player for this mission
808 void set_highest_level(int levelnum)
809 {
810         int ret,i;
811
812         if ((ret=read_player_file()) != EZERO)
813                 if (ret != ENOENT)              //if file doesn't exist, that's ok
814                         return;
815
816         i                       = find_hli_entry();
817
818         if (levelnum > highest_levels[i].level_num)
819                 highest_levels[i].level_num                     = levelnum;
820
821         write_player_file();
822 }
823
824 //gets the player's highest level from the file for this mission
825 int get_highest_level(void)
826 {
827         int i;
828         int highest_saturn_level                        = 0;
829         read_player_file();
830 #ifndef SATURN
831         if (strlen(Mission_list[Current_mission_num].filename)==0 )     {
832                 for (i=0;i<n_highest_levels;i++)
833                         if (!stricmp(highest_levels[i].shortname, "DESTSAT"))   //      Destination Saturn.
834                                 highest_saturn_level                    = highest_levels[i].level_num; 
835         }
836 #endif
837    i                    = highest_levels[find_hli_entry()].level_num;
838         if ( highest_saturn_level > i )
839         i                       = highest_saturn_level;
840         return i;
841 }
842
843 extern int Cockpit_mode_save;
844
845 //write out player's saved games.  returns errno (0 == no error)
846 int write_player_file()
847 {
848         #ifdef MACINTOSH
849         char filename[FILENAME_LEN+15];
850         #else
851         char filename[FILENAME_LEN];            // because of ":Players:" path
852         #endif
853         CFILE *file;
854         int errno_ret,i;
855
856 //      #ifdef APPLE_DEMO               // no saving of player files in Apple OEM version
857 //      return 0;
858 //      #endif
859
860         errno_ret                       = WriteConfigFile();
861
862 #ifndef MACINTOSH
863         sprintf(filename,"%s.plr",Players[Player_num].callsign);
864 #else
865         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
866 #endif
867         file = cfopen(filename, "wb");
868
869 #if 0 //ndef MACINTOSH
870         //check filename
871         if (file && isatty(fileno(file))) {
872
873                 //if the callsign is the name of a tty device, prepend a char
874
875                 fclose(file);
876                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
877                 file                    = fopen(filename,"wb");
878         }
879 #endif
880
881         if (!file)
882               return errno;
883
884         errno_ret                       = EZERO;
885
886         //Write out player's info
887         cfile_write_int(SAVE_FILE_ID, file);
888         cfile_write_short(PLAYER_FILE_VERSION, file);
889
890         cfile_write_short(Game_window_w, file);
891         cfile_write_short(Game_window_h, file);
892
893         cfile_write_byte(Player_default_difficulty, file);
894         cfile_write_byte(Auto_leveling_on, file);
895         cfile_write_byte(Reticle_on, file);
896         cfile_write_byte((Cockpit_mode_save != -1)?Cockpit_mode_save:Cockpit_mode, file);   //if have saved mode, write it instead of letterbox/rear view
897         cfile_write_byte(Default_display_mode, file);
898         cfile_write_byte(Missile_view_enabled, file);
899         cfile_write_byte(Headlight_active_default, file);
900         cfile_write_byte(Guided_in_big_window, file);
901         cfile_write_byte(Automap_always_hires, file);
902
903         //write higest level info
904         Assert(n_highest_levels <= MAX_MISSIONS);
905         cfile_write_short(n_highest_levels, file);
906         if ((cfwrite(highest_levels, sizeof(hli), n_highest_levels, file) != n_highest_levels))
907         {
908                 errno_ret                       = errno;
909                 cfclose(file);
910                 return errno_ret;
911         }
912
913 #ifdef NETWORK
914         if ((cfwrite(Network_message_macro, MAX_MESSAGE_LEN, 4, file) != 4))
915         {
916                 errno_ret                       = errno;
917                 cfclose(file);
918                 return errno_ret;
919         }
920 #else
921         cfseek(file, MAX_MESSAGE_LEN * 4, SEEK_CUR);
922 #endif
923
924         //write kconfig info
925         {
926
927                 #ifdef WINDOWS
928                 control_type_win = Config_control_type;
929                 #else
930                 control_type_dos = Config_control_type;
931                 #endif
932
933                 if (cfwrite(kconfig_settings, MAX_CONTROLS * CONTROL_MAX_TYPES, 1, file ) != 1)
934                         errno_ret=errno;
935                 else if (cfwrite(&control_type_dos, sizeof(ubyte), 1, file) != 1)
936                         errno_ret=errno;
937                 else if (cfwrite(&control_type_win, sizeof(ubyte), 1, file ) != 1)
938                         errno_ret=errno;
939                 else if (cfwrite(&Config_joystick_sensitivity, sizeof(ubyte), 1, file) != 1)
940                         errno_ret=errno;
941
942                 for (i=0;i<11;i++)
943                 {
944                         cfwrite(&PrimaryOrder[i], sizeof(ubyte), 1, file);
945                         cfwrite(&SecondaryOrder[i], sizeof(ubyte), 1, file);
946                 }
947
948                 cfile_write_int(Cockpit_3d_view[0], file);
949                 cfile_write_int(Cockpit_3d_view[1], file);
950
951 #ifdef NETWORK
952                 cfile_write_int(Netlife_kills, file);
953                 cfile_write_int(Netlife_killed, file);
954                 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
955                 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
956 #else
957                 cfile_write_int(0, file);
958                 cfile_write_int(0, file);
959                 i = get_lifetime_checksum(0, 0);
960 #endif
961                 cfile_write_int(i,file);
962         }
963
964         //write guidebot name
965         cfile_write_string(real_guidebot_name, file);
966         {
967                 char buf[128];
968                 #ifdef WINDOWS
969                 joy95_get_name(JOYSTICKID1, buf, 127);
970                 #else
971                 strcpy(buf, "DOS joystick");
972                 #endif
973                 cfile_write_string(buf, file);  // Write out current joystick for player.
974         }
975
976         cfwrite(kconfig_d2x_settings, MAX_D2X_CONTROLS, 1, file);
977
978         if (cfclose(file))
979                 errno_ret                       = errno;
980
981         if (errno_ret != EZERO) {
982                 cfile_delete(filename);         //delete bogus file
983                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s",TXT_ERROR_WRITING_PLR, strerror(errno_ret));
984         }
985
986         #ifdef MACINTOSH                // set filetype and creator for playerfile
987         {
988                 FInfo finfo;
989                 Str255 pfilename;
990                 OSErr err;
991
992                 strcpy(pfilename, filename);
993                 c2pstr(pfilename);
994                 err = HGetFInfo(0, 0, pfilename, &finfo);
995                 finfo.fdType = 'PLYR';
996                 finfo.fdCreator = 'DCT2';
997                 err = HSetFInfo(0, 0, pfilename, &finfo);
998         }
999         #endif
1000
1001         return errno_ret;
1002
1003 }
1004
1005 //update the player's highest level.  returns errno (0 == no error)
1006 int update_player_file()
1007 {
1008         int ret;
1009
1010         if ((ret=read_player_file()) != EZERO)
1011                 if (ret != ENOENT)              //if file doesn't exist, that's ok
1012                         return ret;
1013
1014         return write_player_file();
1015 }
1016
1017 int get_lifetime_checksum (int a,int b)
1018  {
1019   int num;
1020
1021   // confusing enough to beat amateur disassemblers? Lets hope so
1022
1023   num=(a<<8 ^ b);
1024   num^=(a | b);
1025   num*=num>>2;
1026   return (num);
1027  }
1028   
1029