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.
16 * Functions to load & save player's settings (*.plr file)
31 #if !defined(_MSC_VER) && !defined(macintosh)
68 #define SAVE_FILE_ID MAKE_SIG('D','P','L','R')
72 #include <Errors.h> // mac doesn't have "normal" error numbers -- must use mac equivs
77 #elif defined(_WIN32_WCE)
80 # define strerror(x) "Unknown Error"
83 int get_lifetime_checksum (int a,int b);
90 short n_highest_levels;
92 hli highest_levels[MAX_MISSIONS];
94 #define PLAYER_FILE_VERSION 25 //increment this every time the player file changes
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: removed kconfig data
117 #define COMPATIBLE_PLAYER_FILE_VERSION 17
120 int Default_leveling_on=1;
121 extern ubyte SecondaryOrder[],PrimaryOrder[];
122 extern void InitWeaponOrdering();
125 extern ubyte default_firebird_settings[];
126 extern ubyte default_mousestick_settings[];
129 int new_player_config()
132 int i,j,control_choice;
134 int mct=CONTROL_MAX_TYPES;
140 InitWeaponOrdering (); //setup default weapon priorities
142 #if defined(MACINTOSH) && defined(USE_ISP)
147 #if !defined(MACINTOSH)
148 for (i=0; i<mct; i++ ) {
149 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
152 for (i = 0; i < 6; i++) {
153 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
155 m[4].text = "Gravis Firebird/Mousetick II";
156 m[3].text = "Thrustmaster";
160 m[0].text = TXT_CONTROL_KEYBOARD;
162 control_choice = Config_control_type; // Assume keyboard
165 control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
170 if ( control_choice < 0 )
173 #if defined(MACINTOSH) && defined(USE_ISP)
175 else // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
183 Config_control_type = control_choice;
186 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
187 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
188 if (i==0) goto RetrySelection;
191 if ( (Config_control_type>0) && (Config_control_type<5)) {
194 #else // some macintosh only stuff here
195 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
196 extern char *tm_warning;
198 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
199 if (i==0) goto RetrySelection;
200 } else if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO ) {
201 extern char *ch_warning;
203 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
204 if (i==0) goto RetrySelection;
205 } else if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD ) {
206 extern char *ms_warning;
208 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
209 if (i==0) goto RetrySelection;
210 // stupid me -- get real default setting for either mousestick or firebird
211 joydefs_set_type( Config_control_type );
212 if (joy_have_firebird())
213 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
214 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
216 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
217 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
218 kc_set_controls(); // reset the joystick control
220 if ( (Config_control_type>0) && (Config_control_type<5) ) {
221 joydefs_set_type( Config_control_type );
227 Player_default_difficulty = 1;
228 Auto_leveling_on = Default_leveling_on = 1;
229 n_highest_levels = 1;
230 highest_levels[0].shortname[0] = 0; //no name for mission 0
231 highest_levels[0].level_num = 1; //was highest level in old struct
232 Config_joystick_sensitivity = 8;
233 Cockpit_3d_view[0]=CV_NONE;
234 Cockpit_3d_view[1]=CV_NONE;
236 // Default taunt macros
238 strcpy(Network_message_macro[0], "Why can't we all just get along?");
239 strcpy(Network_message_macro[1], "Hey, I got a present for ya");
240 strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
241 strcpy(Network_message_macro[3], "This one's headed for Uranus");
242 Netlife_kills=0; Netlife_killed=0;
248 extern int Guided_in_big_window,Automap_always_hires;
250 //this length must match the value in escort.c
251 #define GUIDEBOT_NAME_LEN 9
252 extern char guidebot_name[];
253 extern char real_guidebot_name[];
256 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) };
258 //read in the player's saved games. returns errno (0 == no error)
259 int read_player_file()
262 char filename[FILENAME_LEN+15];
264 char filename[FILENAME_LEN];
268 short player_file_version;
272 Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
274 sprintf(filename, PLAYER_DIR "%.8s.plr", Players[Player_num].callsign);
276 if (!PHYSFS_exists(filename))
279 file = PHYSFSX_openReadBuffered(filename);
284 if (file && isatty(fileno(file))) {
285 //if the callsign is the name of a tty device, prepend a char
287 sprintf(filename, PLAYER_DIR "$%.7s.plr",Players[Player_num].callsign);
288 file = PHYSFSX_openReadBuffered(filename);
294 goto read_player_file_failed;
296 PHYSFS_readSLE32(file, &id);
298 // SWAPINT added here because old versions of d2x
299 // used the wrong byte order.
300 if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
301 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
306 player_file_version = cfile_read_short(file);
308 if (player_file_version > 255) // bigendian file?
312 player_file_version = SWAPSHORT(player_file_version);
314 if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
315 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
320 Game_window_w = cfile_read_short(file);
321 Game_window_h = cfile_read_short(file);
324 Game_window_w = SWAPSHORT(Game_window_w);
325 Game_window_h = SWAPSHORT(Game_window_h);
328 Player_default_difficulty = cfile_read_byte(file);
329 Default_leveling_on = cfile_read_byte(file);
330 Reticle_on = cfile_read_byte(file);
331 Cockpit_mode = cfile_read_byte(file);
333 i = cfile_read_byte(file);
334 Default_display_mode = legacy_display_mode[i];
336 Missile_view_enabled = cfile_read_byte(file);
337 Headlight_active_default = cfile_read_byte(file);
338 Guided_in_big_window = cfile_read_byte(file);
340 if (player_file_version >= 19)
341 Automap_always_hires = cfile_read_byte(file);
343 Auto_leveling_on = Default_leveling_on;
345 //read new highest level info
347 n_highest_levels = cfile_read_short(file);
349 n_highest_levels = SWAPSHORT(n_highest_levels);
350 Assert(n_highest_levels <= MAX_MISSIONS);
352 if (PHYSFS_read(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels)
353 goto read_player_file_failed;
360 len = MAX_MESSAGE_LEN;
362 for (i = 0; i < 4; i++)
363 if (PHYSFS_read(file, Network_message_macro[i], len, 1) != 1)
364 goto read_player_file_failed;
366 char dummy[4][MAX_MESSAGE_LEN];
368 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
374 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
375 ubyte kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS], control_type_win;
377 if (player_file_version < 25)
378 if (PHYSFS_read(file, kconfig_settings, MAX_CONTROLS*n_control_types, 1) != 1)
379 goto read_player_file_failed;
380 if (PHYSFS_read(file, (ubyte *)&Config_control_type, sizeof(ubyte), 1) != 1)
381 goto read_player_file_failed;
382 else if (player_file_version >= 21 && player_file_version < 25
383 && PHYSFS_read(file, (ubyte *)&control_type_win, sizeof(ubyte), 1) != 1)
384 goto read_player_file_failed;
385 else if (PHYSFS_read(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) !=1 )
386 goto read_player_file_failed;
389 joydefs_set_type(Config_control_type);
394 PrimaryOrder[i] = cfile_read_byte(file);
395 SecondaryOrder[i] = cfile_read_byte(file);
398 if (player_file_version>=16)
400 PHYSFS_readSLE32(file, &Cockpit_3d_view[0]);
401 PHYSFS_readSLE32(file, &Cockpit_3d_view[1]);
404 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
405 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
413 if (player_file_version>=22)
416 PHYSFS_readSLE32(file, &Netlife_kills);
417 PHYSFS_readSLE32(file, &Netlife_killed);
419 Netlife_kills = SWAPINT(Netlife_kills);
420 Netlife_killed = SWAPINT(Netlife_killed);
425 PHYSFS_readSLE32(file, &dummy);
426 PHYSFS_readSLE32(file, &dummy);
433 Netlife_kills=0; Netlife_killed=0;
437 if (player_file_version>=23)
439 PHYSFS_readSLE32(file, &i);
443 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
444 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
446 Netlife_kills=0; Netlife_killed=0;
447 nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
454 if (player_file_version >= 18)
455 PHYSFSX_readString(file, guidebot_name);
457 strcpy(guidebot_name,"GUIDE-BOT");
459 strcpy(real_guidebot_name,guidebot_name);
464 if (player_file_version >= 24)
465 PHYSFSX_readString(file, buf); // Just read it in fpr DPS.
468 if (!PHYSFS_close(file))
469 goto read_player_file_failed;
476 read_player_file_failed:
477 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
485 //finds entry for this level in table. if not found, returns ptr to
486 //empty entry. If no empty entries, takes over last one
491 for (i=0;i<n_highest_levels;i++)
492 if (!stricmp(highest_levels[i].shortname, Current_mission_filename))
495 if (i==n_highest_levels) { //not found. create entry
498 i--; //take last entry
502 strcpy(highest_levels[i].shortname, Current_mission_filename);
503 highest_levels[i].level_num = 0;
509 //set a new highest level for player for this mission
510 void set_highest_level(int levelnum)
514 if ((ret=read_player_file()) != EZERO)
515 if (ret != ENOENT) //if file doesn't exist, that's ok
518 i = find_hli_entry();
520 if (levelnum > highest_levels[i].level_num)
521 highest_levels[i].level_num = levelnum;
526 //gets the player's highest level from the file for this mission
527 int get_highest_level(void)
530 int highest_saturn_level = 0;
533 if (strlen(Current_mission_filename)==0 ) {
534 for (i=0;i<n_highest_levels;i++)
535 if (!stricmp(highest_levels[i].shortname, "DESTSAT")) // Destination Saturn.
536 highest_saturn_level = highest_levels[i].level_num;
539 i = highest_levels[find_hli_entry()].level_num;
540 if ( highest_saturn_level > i )
541 i = highest_saturn_level;
545 extern int Cockpit_mode_save;
548 //write out player's saved games. returns errno (0 == no error)
549 int write_player_file()
551 char filename[FILENAME_LEN+15];
555 // #ifdef APPLE_DEMO // no saving of player files in Apple OEM version
561 sprintf(filename, PLAYER_DIR "%s.plr", Players[Player_num].callsign);
562 file = PHYSFSX_openWriteBuffered(filename);
564 #if 0 //ndef MACINTOSH
566 if (file && isatty(fileno(file))) {
568 //if the callsign is the name of a tty device, prepend a char
571 sprintf(filename, PLAYER_DIR "$%.7s.plr", Players[Player_num].callsign);
572 file = PHYSFSX_openWriteBuffered(filename);
579 //Write out player's info
580 PHYSFS_writeULE32(file, SAVE_FILE_ID);
581 PHYSFS_writeULE16(file, PLAYER_FILE_VERSION);
583 PHYSFS_writeULE16(file, Game_window_w);
584 PHYSFS_writeULE16(file, Game_window_h);
586 PHYSFSX_writeU8(file, Player_default_difficulty);
587 PHYSFSX_writeU8(file, Auto_leveling_on);
588 PHYSFSX_writeU8(file, Reticle_on);
589 PHYSFSX_writeU8(file, (Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode); //if have saved mode, write it instead of letterbox/rear view
591 for (i = 0; i < (sizeof(legacy_display_mode) / sizeof(uint32_t)); i++) {
592 if (legacy_display_mode[i] == Current_display_mode)
595 PHYSFSX_writeU8(file, i);
597 PHYSFSX_writeU8(file, Missile_view_enabled);
598 PHYSFSX_writeU8(file, Headlight_active_default);
599 PHYSFSX_writeU8(file, Guided_in_big_window);
600 PHYSFSX_writeU8(file, Automap_always_hires);
602 //write higest level info
603 PHYSFS_writeULE16(file, n_highest_levels);
604 if ((PHYSFS_write(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels))
605 goto write_player_file_failed;
608 if ((PHYSFS_write(file, Network_message_macro, MAX_MESSAGE_LEN, 4) != 4))
609 goto write_player_file_failed;
612 char dummy[4][MAX_MESSAGE_LEN]; // Pull the messages from a hat! ;-)
614 if ((PHYSFS_write(file, dummy, MAX_MESSAGE_LEN, 4) != 4))
615 goto write_player_file_failed;
621 if (PHYSFS_write(file, &Config_control_type, sizeof(ubyte), 1) != 1)
622 goto write_player_file_failed;
623 else if (PHYSFS_write(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) != 1)
624 goto write_player_file_failed;
626 for (i = 0; i < 11; i++)
628 PHYSFS_write(file, &PrimaryOrder[i], sizeof(ubyte), 1);
629 PHYSFS_write(file, &SecondaryOrder[i], sizeof(ubyte), 1);
632 PHYSFS_writeULE32(file, Cockpit_3d_view[0]);
633 PHYSFS_writeULE32(file, Cockpit_3d_view[1]);
636 PHYSFS_writeULE32(file, Netlife_kills);
637 PHYSFS_writeULE32(file, Netlife_killed);
638 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
639 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
641 PHYSFS_writeULE32(file, 0);
642 PHYSFS_writeULE32(file, 0);
643 i = get_lifetime_checksum (0, 0);
645 PHYSFS_writeULE32(file, i);
648 //write guidebot name
649 PHYSFSX_writeString(file, real_guidebot_name);
653 strcpy(buf, "DOS joystick");
654 PHYSFSX_writeString(file, buf); // Write out current joystick for player.
657 if (!PHYSFS_close(file))
658 goto write_player_file_failed;
660 #ifdef MACINTOSH // set filetype and creator for playerfile
666 strcpy(pfilename, filename);
668 err = HGetFInfo(0, 0, pfilename, &finfo);
669 finfo.fdType = 'PLYR';
670 finfo.fdCreator = 'DCT2';
671 err = HSetFInfo(0, 0, pfilename, &finfo);
677 write_player_file_failed:
678 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
682 PHYSFS_delete(filename); //delete bogus file
688 //update the player's highest level. returns errno (0 == no error)
689 int update_player_file()
693 if ((ret=read_player_file()) != EZERO)
694 if (ret != ENOENT) //if file doesn't exist, that's ok
697 return write_player_file();
700 int get_lifetime_checksum (int a,int b)
704 // confusing enough to beat amateur disassemblers? Lets hope so