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