]> icculus.org git repositories - btb/d2x.git/blob - main/playsave.c
get rid of dependencies on predefined display modes
[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 #ifndef MACINTOSH
307         sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
308 #else
309         sprintf(filename, "Players/%.8s.plr",Players[Player_num].callsign);
310 #endif
311
312         if (!PHYSFS_exists(filename))
313                 return ENOENT;
314
315         file = PHYSFSX_openReadBuffered(filename);
316
317 #if 0
318 #ifndef MACINTOSH
319         //check filename
320         if (file && isatty(fileno(file))) {
321                 //if the callsign is the name of a tty device, prepend a char
322                 PHYSFS_close(file);
323                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
324                 file = PHYSFSX_openReadBuffered(filename);
325         }
326 #endif
327 #endif
328
329         if (!file)
330                 goto read_player_file_failed;
331
332         PHYSFS_readSLE32(file, &id);
333
334         // SWAPINT added here because old versions of d2x
335         // used the wrong byte order.
336         if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
337                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
338                 PHYSFS_close(file);
339                 return -1;
340         }
341
342         player_file_version = cfile_read_short(file);
343
344         if (player_file_version > 255) // bigendian file?
345                 swap = 1;
346
347         if (swap)
348                 player_file_version = SWAPSHORT(player_file_version);
349
350         if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
351                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
352                 PHYSFS_close(file);
353                 return -1;
354         }
355
356         Game_window_w = cfile_read_short(file);
357         Game_window_h = cfile_read_short(file);
358
359         if (swap) {
360                 Game_window_w = SWAPSHORT(Game_window_w);
361                 Game_window_h = SWAPSHORT(Game_window_h);
362         }
363
364         Player_default_difficulty = cfile_read_byte(file);
365         Default_leveling_on       = cfile_read_byte(file);
366         Reticle_on                = cfile_read_byte(file);
367         Cockpit_mode              = cfile_read_byte(file);
368
369         i                         = cfile_read_byte(file);
370         Default_display_mode = legacy_display_mode[i];
371
372         Missile_view_enabled      = cfile_read_byte(file);
373         Headlight_active_default  = cfile_read_byte(file);
374         Guided_in_big_window      = cfile_read_byte(file);
375                 
376         if (player_file_version >= 19)
377                 Automap_always_hires = cfile_read_byte(file);
378
379         Auto_leveling_on = Default_leveling_on;
380
381         //read new highest level info
382
383         n_highest_levels = cfile_read_short(file);
384         if (swap)
385                 n_highest_levels = SWAPSHORT(n_highest_levels);
386         Assert(n_highest_levels <= MAX_MISSIONS);
387
388         if (PHYSFS_read(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels)
389                 goto read_player_file_failed;
390
391         //read taunt macros
392         {
393 #ifdef NETWORK
394                 int i,len;
395
396                 len = MAX_MESSAGE_LEN;
397
398                 for (i = 0; i < 4; i++)
399                         if (PHYSFS_read(file, Network_message_macro[i], len, 1) != 1)
400                                 goto read_player_file_failed;
401 #else
402                 char dummy[4][MAX_MESSAGE_LEN];
403
404                 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
405 #endif
406         }
407
408         //read kconfig data
409         {
410                 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
411
412                 if (PHYSFS_read(file, kconfig_settings, MAX_CONTROLS*n_control_types, 1) != 1)
413                         goto read_player_file_failed;
414                 else if (PHYSFS_read(file, (ubyte *)&control_type_dos, sizeof(ubyte), 1) != 1)
415                         goto read_player_file_failed;
416                 else if (player_file_version >= 21 && PHYSFS_read(file, (ubyte *)&control_type_win, sizeof(ubyte), 1) != 1)
417                         goto read_player_file_failed;
418                 else if (PHYSFS_read(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) !=1 )
419                         goto read_player_file_failed;
420
421                 #ifdef WINDOWS
422                 Config_control_type = control_type_win;
423                 #else
424                 Config_control_type = control_type_dos;
425                 #endif
426                 
427                 #ifdef MACINTOSH
428                 joydefs_set_type(Config_control_type);
429                 #endif
430
431                 for (i=0;i<11;i++)
432                 {
433                         PrimaryOrder[i] = cfile_read_byte(file);
434                         SecondaryOrder[i] = cfile_read_byte(file);
435                 }
436
437                 if (player_file_version>=16)
438                 {
439                         PHYSFS_readSLE32(file, &Cockpit_3d_view[0]);
440                         PHYSFS_readSLE32(file, &Cockpit_3d_view[1]);
441                         if (swap)
442                         {
443                                 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
444                                 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
445                         }
446                 }
447
448                 kc_set_controls();
449
450         }
451
452         if (player_file_version>=22)
453         {
454 #ifdef NETWORK
455                 PHYSFS_readSLE32(file, &Netlife_kills);
456                 PHYSFS_readSLE32(file, &Netlife_killed);
457                 if (swap) {
458                         Netlife_kills = SWAPINT(Netlife_kills);
459                         Netlife_killed = SWAPINT(Netlife_killed);
460                 }
461 #else
462                 {
463                         int dummy;
464                         PHYSFS_readSLE32(file, &dummy);
465                         PHYSFS_readSLE32(file, &dummy);
466                 }
467 #endif
468         }
469 #ifdef NETWORK
470         else
471         {
472                 Netlife_kills=0; Netlife_killed=0;
473         }
474 #endif
475
476         if (player_file_version>=23)
477         {
478                 PHYSFS_readSLE32(file, &i);
479                 if (swap)
480                         i = SWAPINT(i);
481 #ifdef NETWORK
482                 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
483                 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
484                 {
485                         Netlife_kills=0; Netlife_killed=0;
486                         nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
487                         rewrite_it=1;
488                 }
489 #endif
490         }
491
492         //read guidebot name
493         if (player_file_version >= 18)
494                 PHYSFSX_readString(file, guidebot_name);
495         else
496                 strcpy(guidebot_name,"GUIDE-BOT");
497
498         strcpy(real_guidebot_name,guidebot_name);
499
500         {
501                 char buf[128];
502
503         #ifdef WINDOWS
504                 joy95_get_name(JOYSTICKID1, buf, 127);
505                 if (player_file_version >= 24) 
506                         PHYSFSX_readString(file, win95_current_joyname);
507                 else
508                         strcpy(win95_current_joyname, "Old Player File");
509                 
510                 mprintf((0, "Detected joystick: %s\n", buf));
511                 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
512
513                 if (strcmp(win95_current_joyname, buf)) {
514                         for (i = 0; i < MAX_CONTROLS; i++)
515                                 kconfig_settings[CONTROL_WINJOYSTICK][i] = 
516                                         default_kconfig_settings[CONTROL_WINJOYSTICK][i];
517                 }        
518         #else
519                 if (player_file_version >= 24) 
520                         PHYSFSX_readString(file, buf);                  // Just read it in fpr DPS.
521         #endif
522         }
523
524         if (player_file_version >= 25)
525                 PHYSFS_read(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
526         else
527                 for(i=0; i < MAX_D2X_CONTROLS; i++)
528                         kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
529
530         if (!PHYSFS_close(file))
531                 goto read_player_file_failed;
532
533         if (rewrite_it)
534                 write_player_file();
535
536         return EZERO;
537
538  read_player_file_failed:
539         nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
540         if (file)
541                 PHYSFS_close(file);
542
543         return -1;
544 }
545
546
547 //finds entry for this level in table.  if not found, returns ptr to 
548 //empty entry.  If no empty entries, takes over last one 
549 int find_hli_entry()
550 {
551         int i;
552
553         for (i=0;i<n_highest_levels;i++)
554                 if (!stricmp(highest_levels[i].shortname, Current_mission_filename))
555                         break;
556
557         if (i==n_highest_levels) {              //not found.  create entry
558
559                 if (i==MAX_MISSIONS)
560                         i--;            //take last entry
561                 else
562                         n_highest_levels++;
563
564                 strcpy(highest_levels[i].shortname, Current_mission_filename);
565                 highest_levels[i].level_num                     = 0;
566         }
567
568         return i;
569 }
570
571 //set a new highest level for player for this mission
572 void set_highest_level(int levelnum)
573 {
574         int ret,i;
575
576         if ((ret=read_player_file()) != EZERO)
577                 if (ret != ENOENT)              //if file doesn't exist, that's ok
578                         return;
579
580         i                       = find_hli_entry();
581
582         if (levelnum > highest_levels[i].level_num)
583                 highest_levels[i].level_num                     = levelnum;
584
585         write_player_file();
586 }
587
588 //gets the player's highest level from the file for this mission
589 int get_highest_level(void)
590 {
591         int i;
592         int highest_saturn_level                        = 0;
593         read_player_file();
594 #ifndef SATURN
595         if (strlen(Current_mission_filename)==0 )       {
596                 for (i=0;i<n_highest_levels;i++)
597                         if (!stricmp(highest_levels[i].shortname, "DESTSAT"))   //      Destination Saturn.
598                                 highest_saturn_level                    = highest_levels[i].level_num; 
599         }
600 #endif
601    i                    = highest_levels[find_hli_entry()].level_num;
602         if ( highest_saturn_level > i )
603         i                       = highest_saturn_level;
604         return i;
605 }
606
607 extern int Cockpit_mode_save;
608
609
610 //write out player's saved games.  returns errno (0 == no error)
611 int write_player_file()
612 {
613         #ifdef MACINTOSH
614         char filename[FILENAME_LEN+15];
615         #else
616         char filename[FILENAME_LEN];            // because of ":Players:" path
617         #endif
618         PHYSFS_file *file;
619         int i;
620
621 //      #ifdef APPLE_DEMO               // no saving of player files in Apple OEM version
622 //      return 0;
623 //      #endif
624
625         WriteConfigFile();
626
627 #ifndef MACINTOSH
628         sprintf(filename,"%s.plr",Players[Player_num].callsign);
629 #else
630         sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
631 #endif
632         file = PHYSFSX_openWriteBuffered(filename);
633
634 #if 0 //ndef MACINTOSH
635         //check filename
636         if (file && isatty(fileno(file))) {
637
638                 //if the callsign is the name of a tty device, prepend a char
639
640                 PHYSFS_close(file);
641                 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
642                 file = PHYSFSX_openWriteBuffered(filename);
643         }
644 #endif
645
646         if (!file)
647                 return -1;
648
649         //Write out player's info
650         PHYSFS_writeULE32(file, SAVE_FILE_ID);
651         PHYSFS_writeULE16(file, PLAYER_FILE_VERSION);
652
653         PHYSFS_writeULE16(file, Game_window_w);
654         PHYSFS_writeULE16(file, Game_window_h);
655
656         PHYSFSX_writeU8(file, Player_default_difficulty);
657         PHYSFSX_writeU8(file, Auto_leveling_on);
658         PHYSFSX_writeU8(file, Reticle_on);
659         PHYSFSX_writeU8(file, (Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode);  //if have saved mode, write it instead of letterbox/rear view
660
661         for (i = 0; i < (sizeof(legacy_display_mode) / sizeof(uint32_t)); i++) {
662                 if (legacy_display_mode[i] == Current_display_mode)
663                         break;
664         }
665         PHYSFSX_writeU8(file, i);
666
667         PHYSFSX_writeU8(file, Missile_view_enabled);
668         PHYSFSX_writeU8(file, Headlight_active_default);
669         PHYSFSX_writeU8(file, Guided_in_big_window);
670         PHYSFSX_writeU8(file, Automap_always_hires);
671
672         //write higest level info
673         PHYSFS_writeULE16(file, n_highest_levels);
674         if ((PHYSFS_write(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels))
675                 goto write_player_file_failed;
676
677 #ifdef NETWORK
678         if ((PHYSFS_write(file, Network_message_macro, MAX_MESSAGE_LEN, 4) != 4))
679                 goto write_player_file_failed;
680 #else
681         {
682                 char dummy[4][MAX_MESSAGE_LEN]; // Pull the messages from a hat! ;-)
683
684                 if ((PHYSFS_write(file, dummy, MAX_MESSAGE_LEN, 4) != 4))
685                         goto write_player_file_failed;
686         }
687 #endif
688
689         //write kconfig info
690         {
691
692                 #ifdef WINDOWS
693                 control_type_win = Config_control_type;
694                 #else
695                 control_type_dos = Config_control_type;
696                 #endif
697
698                 if (PHYSFS_write(file, kconfig_settings, MAX_CONTROLS*CONTROL_MAX_TYPES, 1) != 1)
699                         goto write_player_file_failed;
700                 else if (PHYSFS_write(file, &control_type_dos, sizeof(ubyte), 1) != 1)
701                         goto write_player_file_failed;
702                 else if (PHYSFS_write(file, &control_type_win, sizeof(ubyte), 1) != 1)
703                         goto write_player_file_failed;
704                 else if (PHYSFS_write(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) != 1)
705                         goto write_player_file_failed;
706
707                 for (i = 0; i < 11; i++)
708                 {
709                         PHYSFS_write(file, &PrimaryOrder[i], sizeof(ubyte), 1);
710                         PHYSFS_write(file, &SecondaryOrder[i], sizeof(ubyte), 1);
711                 }
712
713                 PHYSFS_writeULE32(file, Cockpit_3d_view[0]);
714                 PHYSFS_writeULE32(file, Cockpit_3d_view[1]);
715
716 #ifdef NETWORK
717                 PHYSFS_writeULE32(file, Netlife_kills);
718                 PHYSFS_writeULE32(file, Netlife_killed);
719                 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
720                 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
721 #else
722                 PHYSFS_writeULE32(file, 0);
723                 PHYSFS_writeULE32(file, 0);
724                 i = get_lifetime_checksum (0, 0);
725 #endif
726                 PHYSFS_writeULE32(file, i);
727         }
728
729         //write guidebot name
730         PHYSFSX_writeString(file, real_guidebot_name);
731
732         {
733                 char buf[128];
734                 #ifdef WINDOWS
735                 joy95_get_name(JOYSTICKID1, buf, 127);
736                 #else
737                 strcpy(buf, "DOS joystick");
738                 #endif
739                 PHYSFSX_writeString(file, buf);         // Write out current joystick for player.
740         }
741
742         PHYSFS_write(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
743
744         if (!PHYSFS_close(file))
745                 goto write_player_file_failed;
746
747         #ifdef MACINTOSH                // set filetype and creator for playerfile
748         {
749                 FInfo finfo;
750                 Str255 pfilename;
751                 OSErr err;
752
753                 strcpy(pfilename, filename);
754                 c2pstr(pfilename);
755                 err = HGetFInfo(0, 0, pfilename, &finfo);
756                 finfo.fdType = 'PLYR';
757                 finfo.fdCreator = 'DCT2';
758                 err = HSetFInfo(0, 0, pfilename, &finfo);
759         }
760         #endif
761
762         return EZERO;
763
764  write_player_file_failed:
765         nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
766         if (file)
767         {
768                 PHYSFS_close(file);
769                 PHYSFS_delete(filename);        //delete bogus file
770         }
771
772         return -1;
773 }
774
775 //update the player's highest level.  returns errno (0 == no error)
776 int update_player_file()
777 {
778         int ret;
779
780         if ((ret=read_player_file()) != EZERO)
781                 if (ret != ENOENT)              //if file doesn't exist, that's ok
782                         return ret;
783
784         return write_player_file();
785 }
786
787 int get_lifetime_checksum (int a,int b)
788  {
789   int num;
790
791   // confusing enough to beat amateur disassemblers? Lets hope so
792
793   num=(a<<8 ^ b);
794   num^=(a | b);
795   num*=num>>2;
796   return (num);
797  }
798   
799