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