]> icculus.org git repositories - btb/d2x.git/blob - main/playsave.c
This commit was generated by cvs2svn to compensate for changes in r2,
[btb/d2x.git] / main / playsave.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #include <conf.h>
15
16
17 #ifdef WINDOWS
18 #include "desw.h"
19 #include <mmsystem.h>
20 #endif
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26
27 #include "error.h"
28
29 #include "pa_enabl.h"
30 #include "strutil.h"
31 #include "game.h"
32 #include "gameseq.h"
33 #include "player.h"
34 #include "playsave.h"
35 #include "joy.h"
36 #include "kconfig.h"
37 #include "digi.h"
38 #include "newmenu.h"
39 #include "joydefs.h"
40 #include "palette.h"
41 #include "multi.h"
42 #include "menu.h"
43 #include "config.h"
44 #include "text.h"
45 #include "mono.h"
46 #include "state.h"
47 #include "gauges.h"
48 #include "screens.h"
49 #include "powerup.h"
50
51 #define SAVE_FILE_ID                    "DPLR"
52
53 #ifdef MACINTOSH
54         #include <Files.h>
55         #include <Errors.h>                     // mac doesn't have "normal" error numbers -- must use mac equivs
56         #ifndef ENOENT
57                 #define ENOENT fnfErr
58         #endif
59         #ifdef POLY_ACC
60                 #include "poly_acc.h"
61         #endif
62         #include "isp.h"
63 #endif
64
65 int get_lifetime_checksum (int a,int b);
66
67 typedef struct hli {
68         char    shortname[9];
69         ubyte   level_num;
70 } hli;
71
72 int n_highest_levels;
73
74 hli highest_levels[MAX_MISSIONS];
75
76 #define PLAYER_FILE_VERSION     24                      //increment this every time the player file changes
77
78 //version 5  ->  6: added new highest level information
79 //version 6  ->  7: stripped out the old saved_game array.
80 //version 7  ->  8: added reticle flag, & window size
81 //version 8  ->  9: removed player_struct_version
82 //version 9  -> 10: added default display mode
83 //version 10 -> 11: added all toggles in toggle menu
84 //version 11 -> 12: added weapon ordering
85 //version 12 -> 13: added more keys
86 //version 13 -> 14: took out marker key
87 //version 14 -> 15: added guided in big window
88 //version 15 -> 16: added small windows in cockpit
89 //version 16 -> 17: ??
90 //version 17 -> 18: save guidebot name
91 //version 18 -> 19: added automap-highres flag
92 //version 19 -> 20: added kconfig data for windows joysticks
93 //version 20 -> 21: save seperate config types for DOS & Windows
94 //version 21 -> 22: save lifetime netstats 
95 //version 22 -> 23: ??
96 //version 23 -> 24: add name of joystick for windows version.
97
98 #define COMPATIBLE_PLAYER_FILE_VERSION          17
99
100
101 int Default_leveling_on=1;
102 extern ubyte SecondaryOrder[],PrimaryOrder[];
103 extern void InitWeaponOrdering();
104
105 #ifdef MACINTOSH
106 extern ubyte default_firebird_settings[];
107 extern ubyte default_mousestick_settings[];
108 #endif
109
110 int new_player_config()
111 {
112         int nitems;
113         int i,j,control_choice;
114         newmenu_item m[8];
115    int mct=CONTROL_MAX_TYPES;
116  
117    #ifndef WINDOWS
118          mct--;
119         #endif
120
121    InitWeaponOrdering ();               //setup default weapon priorities 
122
123 #if defined(MACINTOSH) && defined(USE_ISP)
124         if (!ISpEnabled())
125         {
126 #endif
127 RetrySelection:
128                 #if !defined(MACINTOSH) && !defined(WINDOWS)
129                 for (i=0; i<mct; i++ )  {
130                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
131                 }
132                 #elif defined(WINDOWS)
133                         m[0].type = NM_TYPE_MENU; m[0].text = CONTROL_TEXT(0);
134                         m[1].type = NM_TYPE_MENU; m[1].text = CONTROL_TEXT(5);
135                         m[2].type = NM_TYPE_MENU; m[2].text = CONTROL_TEXT(7);
136                         i = 3;
137                 #else
138                 for (i = 0; i < 6; i++) {
139                         m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
140                 }
141                 m[4].text = "Gravis Firebird/Mousetick II";
142                 m[3].text = "Thrustmaster";
143                 #endif
144                 
145                 nitems = i;
146                 m[0].text = TXT_CONTROL_KEYBOARD;
147         
148                 #ifdef WINDOWS
149                         if (Config_control_type==CONTROL_NONE) control_choice = 0;
150                         else if (Config_control_type == CONTROL_MOUSE) control_choice = 1;
151                         else if (Config_control_type == CONTROL_WINJOYSTICK) control_choice = 2;
152                         else control_choice = 0;
153                 #else
154                         control_choice = Config_control_type;                           // Assume keyboard
155                 #endif
156         
157                 #ifndef APPLE_DEMO
158                         control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
159                 #else
160                         control_choice = 0;
161                 #endif
162                 
163                 if ( control_choice < 0 )
164                         return 0;
165
166 #if defined(MACINTOSH) && defined(USE_ISP)
167         }
168         else    // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
169         {
170                 control_choice = 0;
171         }
172 #endif
173
174         for (i=0;i<CONTROL_MAX_TYPES; i++ )
175                 for (j=0;j<MAX_CONTROLS; j++ )
176                         kconfig_settings[i][j] = default_kconfig_settings[i][j];
177         kc_set_controls();
178
179         Config_control_type = control_choice;
180
181 #ifdef WINDOWS
182         if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
183         else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
184
185 //      if (Config_control_type == CONTROL_WINJOYSTICK) 
186 //              joydefs_calibrate();
187 #else
188         #ifndef MACINTOSH
189         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
190                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
191                 if (i==0) goto RetrySelection;
192         }
193         
194         if ( (Config_control_type>0) &&         (Config_control_type<5))        {
195                 joydefs_calibrate();
196         }
197         #else           // some macintosh only stuff here
198         if ( Config_control_type==CONTROL_THRUSTMASTER_FCS)     {
199                 extern char *tm_warning;
200                 
201                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
202                 if (i==0) goto RetrySelection;
203         } else  if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO )     {
204                 extern char *ch_warning;
205
206                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
207                 if (i==0) goto RetrySelection;
208         } else  if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD )      {
209                 extern char *ms_warning;
210
211                 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
212                 if (i==0) goto RetrySelection;
213                 // stupid me -- get real default setting for either mousestick or firebird
214                 joydefs_set_type( Config_control_type );
215                 if (joy_have_firebird())
216                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
217                                 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
218                 else
219                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
220                                 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
221                 kc_set_controls();              // reset the joystick control
222         }
223         if ( (Config_control_type>0) && (Config_control_type<5)  ) {
224                 joydefs_set_type( Config_control_type );
225                 joydefs_calibrate();
226         }
227
228         #endif
229 #endif
230         
231         Player_default_difficulty = 1;
232         Auto_leveling_on = Default_leveling_on = 1;
233         n_highest_levels = 1;
234         highest_levels[0].shortname[0] = 0;                     //no name for mission 0
235         highest_levels[0].level_num = 1;                                //was highest level in old struct
236         Config_joystick_sensitivity = 8;
237         Cockpit_3d_view[0]=CV_NONE;
238         Cockpit_3d_view[1]=CV_NONE;
239
240         // Default taunt macros
241         #ifdef NETWORK
242         strcpy(Network_message_macro[0], "Why can't we all just get along?");
243         strcpy(Network_message_macro[1], "Hey, I got a present for ya");
244         strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
245         strcpy(Network_message_macro[3], "This one's headed for Uranus");
246         Netlife_kills=0; Netlife_killed=0;      
247         #endif
248         
249         #ifdef MACINTOSH
250                 #ifdef POLY_ACC
251                         if (PAEnabled)
252                         {
253                                 Scanline_double = 0;            // no pixel doubling for poly_acc
254                         }
255                         else
256                         {
257                                 Scanline_double = 1;            // should be default for new player
258                         }
259                 #else
260                         Scanline_double = 1;                    // should be default for new player
261                 #endif
262         #endif
263
264         return 1;
265 }
266
267 static int read_int(FILE *file)
268 {
269         int i;
270
271         if (fread( &i, sizeof(i), 1, file) != 1)
272                 Error( "Error reading int in gamesave.c" );
273
274         return i;
275 }
276
277 static short read_short(FILE *file)
278 {
279         short s;
280
281         if (fread( &s, sizeof(s), 1, file) != 1)
282                 Error( "Error reading short in gamesave.c" );
283
284         return s;
285 }
286
287 static byte read_byte(FILE *file)
288 {
289         byte s;
290
291         if (fread( &s, sizeof(s), 1, file) != 1)
292                 Error( "Error reading byte in gamesave.c" );
293
294         return s;
295 }
296
297 static void write_int(int i,FILE *file)
298 {
299         if (fwrite( &i, sizeof(i), 1, file) != 1)
300                 Error( "Error writing int in gamesave.c" );
301
302 }
303
304 static void write_short(short s,FILE *file)
305 {
306         if (fwrite( &s, sizeof(s), 1, file) != 1)
307                 Error( "Error writing short in gamesave.c" );
308
309 }
310
311 static void write_byte(byte i,FILE *file)
312 {
313         if (fwrite( &i, sizeof(i), 1, file) != 1)
314                 Error( "Error writing byte in gamesave.c" );
315
316 }
317
318 extern int Guided_in_big_window,Automap_always_hires;
319
320 //this length must match the value in escort.c
321 #define GUIDEBOT_NAME_LEN 9
322 extern char guidebot_name[];
323 extern char real_guidebot_name[];
324
325 WIN(extern char win95_current_joyname[]);
326
327 void read_string(char *s, FILE *f)
328 {
329         if (feof(f))
330                 *s = 0;
331         else
332                 do
333                         *s = fgetc(f);
334                 while (!feof(f) && *s++!=0);
335 }
336
337 void write_string(char *s, FILE *f)
338 {
339         do
340                 fputc(*s,f);
341         while (*s++!=0);
342 }
343
344 ubyte control_type_dos,control_type_win;
345
346 //read in the player's saved games.  returns errno (0 == no error)
347 int read_player_file()
348 {
349         #ifdef MACINTOSH
350         char filename[FILENAME_LEN+15];
351         #else
352         char filename[FILENAME_LEN];
353         #endif
354         FILE *file;
355         int errno_ret = EZERO;
356         int player_file_version,i;
357         int rewrite_it=0;
358         char id[4];
359
360         Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
361
362 #ifndef MACINTOSH
363         sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
364 #else
365         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
366 #endif
367         file = fopen(filename,"rb");
368
369 #ifndef MACINTOSH
370         //check filename
371         if (file && isatty(fileno(file))) {
372                 //if the callsign is the name of a tty device, prepend a char
373                 fclose(file);
374                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
375                 file = fopen(filename,"rb");
376         }
377 #endif
378
379         if (!file) {
380                 return errno;
381         }
382
383         fread( &id, 1, 4, file);
384
385         if (memcmp(id, SAVE_FILE_ID, 4)) {
386                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
387                 fclose(file);
388                 return -1;
389         }
390
391         player_file_version = read_short(file);
392
393         if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
394                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
395                 fclose(file);
396                 return -1;
397         }
398
399         Game_window_w                                   = read_short(file);
400         Game_window_h                                   = read_short(file);
401
402         Player_default_difficulty       = read_byte(file);
403         Default_leveling_on                     = read_byte(file);
404         Reticle_on                                              = read_byte(file);
405         Cockpit_mode                                    = read_byte(file);
406         #ifdef POLY_ACC
407          #ifdef PA_3DFX_VOODOO
408                 if (Cockpit_mode<2)
409                 {
410                 Cockpit_mode=2;
411                         Game_window_w  = 640;
412                         Game_window_h   = 480;
413                 }
414          #endif
415         #endif
416  
417         Default_display_mode                    = read_byte(file);
418         Missile_view_enabled                    = read_byte(file);
419         Headlight_active_default        = read_byte(file);
420         Guided_in_big_window                    = read_byte(file);
421
422         if (player_file_version >= 19)
423                 Automap_always_hires                    = read_byte(file);
424           
425         Auto_leveling_on = Default_leveling_on;
426
427         //read new highest level info
428
429         n_highest_levels = read_short(file);
430         if (fread(highest_levels,sizeof(hli),n_highest_levels,file) != n_highest_levels) {
431                 errno_ret                       = errno;
432                 fclose(file);
433                 return errno_ret;
434         }
435
436         //read taunt macros
437         {
438                 int i,len;
439 #ifdef NETWORK
440                 len                     = MAX_MESSAGE_LEN;
441
442                 for (i                  = 0; i < 4; i++)
443                         if (fread(Network_message_macro[i], len, 1, file) != 1)
444                                 {errno_ret                      = errno; break;}
445 #else
446                 char dummy[4][MAX_MESSAGE_LEN];
447                 fread(dummy, MAX_MESSAGE_LEN, 4, file);
448
449 #endif
450         }
451
452         //read kconfig data
453         {
454                 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
455
456                 if (fread( kconfig_settings, MAX_CONTROLS*n_control_types, 1, file )!=1)
457                         errno_ret=errno;
458                 else if (fread((ubyte *)&control_type_dos, sizeof(ubyte), 1, file )!=1)
459                         errno_ret=errno;
460                 else if (player_file_version >= 21 && fread((ubyte *)&control_type_win, sizeof(ubyte), 1, file )!=1)
461                         errno_ret=errno;
462                 else if (fread(&Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
463                         errno_ret=errno;
464
465                 #ifdef WINDOWS
466                 Config_control_type = control_type_win;
467                 #else
468                 Config_control_type = control_type_dos;
469                 #endif
470                 
471                 #ifdef MACINTOSH
472                 joydefs_set_type(Config_control_type);
473                 #endif
474
475                 for (i=0;i<11;i++)
476                  {
477                         PrimaryOrder[i]=read_byte (file);
478                         SecondaryOrder[i]=read_byte(file);
479                  }
480                 
481                 if (player_file_version>=16)
482                  {
483                   Cockpit_3d_view[0]=read_int(file);
484                   Cockpit_3d_view[1]=read_int(file);
485                  }      
486                 
487                   
488                 if (errno_ret==EZERO)   {
489                         kc_set_controls();
490                 }
491
492         }
493
494    if (player_file_version>=22)
495          {
496 #ifdef NETWORK
497                 Netlife_kills=read_int (file);
498                 Netlife_killed=read_int (file);
499 #else
500                 read_int(file); read_int(file);
501 #endif
502          }
503 #ifdef NETWORK
504    else
505          {
506                  Netlife_kills=0; Netlife_killed=0;
507          }
508 #endif
509
510         if (player_file_version>=23)
511          {
512           i=read_int (file);    
513 #ifdef NETWORK
514           mprintf ((0,"Reading: lifetime checksum is %d\n",i));
515           if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
516                 {
517                  Netlife_kills=0; Netlife_killed=0;
518                  nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
519                  rewrite_it=1;
520                 }
521 #endif
522          }
523
524         //read guidebot name
525         if (player_file_version >= 18)
526                 read_string(guidebot_name,file);
527         else
528                 strcpy(guidebot_name,"GUIDE-BOT");
529
530         strcpy(real_guidebot_name,guidebot_name);
531
532         {
533                 char buf[128];
534
535         #ifdef WINDOWS
536                 joy95_get_name(JOYSTICKID1, buf, 127);
537                 if (player_file_version >= 24) 
538                         read_string(win95_current_joyname, file);
539                 else
540                         strcpy(win95_current_joyname, "Old Player File");
541                 
542                 mprintf((0, "Detected joystick: %s\n", buf));
543                 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
544
545                 if (strcmp(win95_current_joyname, buf)) {
546                         for (i = 0; i < MAX_CONTROLS; i++)
547                                 kconfig_settings[CONTROL_WINJOYSTICK][i] = 
548                                         default_kconfig_settings[CONTROL_WINJOYSTICK][i];
549                 }        
550         #else
551                 if (player_file_version >= 24) 
552                         read_string(buf, file);                 // Just read it in fpr DPS.
553         #endif
554         }
555
556         if (fclose(file) && errno_ret==EZERO)
557                 errno_ret                       = errno;
558
559         if (rewrite_it)
560          write_player_file();
561
562         return errno_ret;
563
564 }
565
566
567 //finds entry for this level in table.  if not found, returns ptr to 
568 //empty entry.  If no empty entries, takes over last one 
569 int find_hli_entry()
570 {
571         int i;
572
573         for (i=0;i<n_highest_levels;i++)
574                 if (!stricmp(highest_levels[i].shortname,Mission_list[Current_mission_num].filename))
575                         break;
576
577         if (i==n_highest_levels) {              //not found.  create entry
578
579                 if (i==MAX_MISSIONS)
580                         i--;            //take last entry
581                 else
582                         n_highest_levels++;
583
584                 strcpy(highest_levels[i].shortname,Mission_list[Current_mission_num].filename);
585                 highest_levels[i].level_num                     = 0;
586         }
587
588         return i;
589 }
590
591 //set a new highest level for player for this mission
592 void set_highest_level(int levelnum)
593 {
594         int ret,i;
595
596         if ((ret=read_player_file()) != EZERO)
597                 if (ret != ENOENT)              //if file doesn't exist, that's ok
598                         return;
599
600         i                       = find_hli_entry();
601
602         if (levelnum > highest_levels[i].level_num)
603                 highest_levels[i].level_num                     = levelnum;
604
605         write_player_file();
606 }
607
608 //gets the player's highest level from the file for this mission
609 int get_highest_level(void)
610 {
611         int i;
612         int highest_saturn_level                        = 0;
613         read_player_file();
614 #ifndef SATURN
615         if (strlen(Mission_list[Current_mission_num].filename)==0 )     {
616                 for (i=0;i<n_highest_levels;i++)
617                         if (!stricmp(highest_levels[i].shortname, "DESTSAT"))   //      Destination Saturn.
618                                 highest_saturn_level                    = highest_levels[i].level_num; 
619         }
620 #endif
621    i                    = highest_levels[find_hli_entry()].level_num;
622         if ( highest_saturn_level > i )
623         i                       = highest_saturn_level;
624         return i;
625 }
626
627 extern int Cockpit_mode_save;
628
629 //write out player's saved games.  returns errno (0 == no error)
630 int write_player_file()
631 {
632         #ifdef MACINTOSH
633         char filename[FILENAME_LEN+15];
634         #else
635         char filename[FILENAME_LEN];            // because of ":Players:" path
636         #endif
637         FILE *file;
638         int errno_ret,i;
639
640 //      #ifdef APPLE_DEMO               // no saving of player files in Apple OEM version
641 //      return 0;
642 //      #endif
643
644         errno_ret                       = WriteConfigFile();
645
646 #ifndef MACINTOSH
647         sprintf(filename,"%s.plr",Players[Player_num].callsign);
648 #else
649         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
650 #endif
651         file                    = fopen(filename,"wb");
652
653 #ifndef MACINTOSH
654         //check filename
655         if (file && isatty(fileno(file))) {
656
657                 //if the callsign is the name of a tty device, prepend a char
658
659                 fclose(file);
660                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
661                 file                    = fopen(filename,"wb");
662         }
663 #endif
664
665         if (!file)
666               return errno;
667
668         errno_ret                       = EZERO;
669
670         //Write out player's info
671         fwrite(SAVE_FILE_ID, 1, 4, file);
672         write_short(PLAYER_FILE_VERSION,file);
673
674         write_short(Game_window_w,file);
675         write_short(Game_window_h,file);
676
677         write_byte(Player_default_difficulty,file);
678         write_byte(Auto_leveling_on,file);
679         write_byte(Reticle_on,file);
680         write_byte((Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode,file);        //if have saved mode, write it instead of letterbox/rear view
681         write_byte(Default_display_mode,file);
682         write_byte(Missile_view_enabled,file);
683         write_byte(Headlight_active_default,file);
684         write_byte(Guided_in_big_window,file);
685         write_byte(Automap_always_hires,file);
686
687         //write higest level info
688         write_short(n_highest_levels,file);
689         if ((fwrite(highest_levels, sizeof(hli), n_highest_levels, file) != n_highest_levels)) {
690                 errno_ret                       = errno;
691                 fclose(file);
692                 return errno_ret;
693         }
694
695         #ifdef NETWORK
696         if ((fwrite(Network_message_macro, MAX_MESSAGE_LEN, 4, file) != 4)) {
697                 errno_ret                       = errno;
698                 fclose(file);
699                 return errno_ret;
700         }
701         #else
702         fseek( file, MAX_MESSAGE_LEN * 4, SEEK_CUR );
703         #endif
704
705         //write kconfig info
706         {
707
708                 #ifdef WINDOWS
709                 control_type_win = Config_control_type;
710                 #else
711                 control_type_dos = Config_control_type;
712                 #endif
713
714                 if (fwrite( kconfig_settings, MAX_CONTROLS*CONTROL_MAX_TYPES, 1, file )!=1)
715                         errno_ret=errno;
716                 else if (fwrite( &control_type_dos, sizeof(ubyte), 1, file )!=1)
717                         errno_ret=errno;
718                 else if (fwrite( &control_type_win, sizeof(ubyte), 1, file )!=1)
719                         errno_ret=errno;
720                 else if (fwrite( &Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
721                         errno_ret=errno;
722
723       for (i=0;i<11;i++)
724        {
725         fwrite (&PrimaryOrder[i],sizeof(ubyte),1,file);
726         fwrite (&SecondaryOrder[i],sizeof(ubyte),1,file);
727        }
728                 write_int (Cockpit_3d_view[0],file);
729                 write_int (Cockpit_3d_view[1],file);
730
731 #ifdef NETWORK
732                 write_int (Netlife_kills,file);
733            write_int (Netlife_killed,file);
734                 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
735                 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
736 #else
737            write_int(0, file);
738            write_int(0, file);
739            i = get_lifetime_checksum (0, 0);
740 #endif
741            write_int (i,file);
742         }
743
744         //write guidebot name
745         write_string(real_guidebot_name,file);
746
747         {
748                 char buf[128];
749                 #ifdef WINDOWS
750                 joy95_get_name(JOYSTICKID1, buf, 127);
751                 #else
752                 strcpy(buf, "DOS joystick");
753                 #endif
754                 write_string(buf, file);                // Write out current joystick for player.
755         }
756
757         if (fclose(file))
758                 errno_ret                       = errno;
759
760         if (errno_ret != EZERO) {
761                 remove(filename);                       //delete bogus file
762                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s",TXT_ERROR_WRITING_PLR, strerror(errno_ret));
763         }
764
765         #ifdef MACINTOSH                // set filetype and creator for playerfile
766         {
767                 FInfo finfo;
768                 Str255 pfilename;
769                 OSErr err;
770
771                 strcpy(pfilename, filename);
772                 c2pstr(pfilename);
773                 err = HGetFInfo(0, 0, pfilename, &finfo);
774                 finfo.fdType = 'PLYR';
775                 finfo.fdCreator = 'DCT2';
776                 err = HSetFInfo(0, 0, pfilename, &finfo);
777         }
778         #endif
779
780         return errno_ret;
781
782 }
783
784 //update the player's highest level.  returns errno (0 == no error)
785 int update_player_file()
786 {
787         int ret;
788
789         if ((ret=read_player_file()) != EZERO)
790                 if (ret != ENOENT)              //if file doesn't exist, that's ok
791                         return ret;
792
793         return write_player_file();
794 }
795
796 int get_lifetime_checksum (int a,int b)
797  {
798   int num;
799
800   // confusing enough to beat amateur disassemblers? Lets hope so
801
802   num=(a<<8 ^ b);
803   num^=(a | b);
804   num*=num>>2;
805   return (num);
806  }
807   
808