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