1 /* $Id: playsave.c,v 1.21 2004-12-01 12:48:13 btb Exp $ */
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.
17 * Functions to load & save player's settings (*.plr file)
32 #if !defined(_MSC_VER) && !defined(macintosh)
70 #define SAVE_FILE_ID MAKE_SIG('D','P','L','R')
74 #include <Errors.h> // mac doesn't have "normal" error numbers -- must use mac equivs
82 #elif defined(_WIN32_WCE)
85 # define strerror(x) "Unknown Error"
88 int get_lifetime_checksum (int a,int b);
95 short n_highest_levels;
97 hli highest_levels[MAX_MISSIONS];
99 #define PLAYER_FILE_VERSION 25 //increment this every time the player file changes
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
122 #define COMPATIBLE_PLAYER_FILE_VERSION 17
125 int Default_leveling_on=1;
126 extern ubyte SecondaryOrder[],PrimaryOrder[];
127 extern void InitWeaponOrdering();
130 extern ubyte default_firebird_settings[];
131 extern ubyte default_mousestick_settings[];
134 int new_player_config()
137 int i,j,control_choice;
139 int mct=CONTROL_MAX_TYPES;
145 InitWeaponOrdering (); //setup default weapon priorities
147 #if defined(MACINTOSH) && defined(USE_ISP)
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);
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);
162 for (i = 0; i < 6; i++) {
163 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
165 m[4].text = "Gravis Firebird/Mousetick II";
166 m[3].text = "Thrustmaster";
170 m[0].text = TXT_CONTROL_KEYBOARD;
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;
178 control_choice = Config_control_type; // Assume keyboard
182 control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
187 if ( control_choice < 0 )
190 #if defined(MACINTOSH) && defined(USE_ISP)
192 else // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
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
207 Config_control_type = control_choice;
210 if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
211 else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
213 // if (Config_control_type == CONTROL_WINJOYSTICK)
214 // joydefs_calibrate();
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;
222 if ( (Config_control_type>0) && (Config_control_type<5)) {
225 #else // some macintosh only stuff here
226 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
227 extern char *tm_warning;
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;
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;
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];
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
251 if ( (Config_control_type>0) && (Config_control_type<5) ) {
252 joydefs_set_type( Config_control_type );
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;
268 // Default taunt macros
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;
281 Scanline_double = 0; // no pixel doubling for poly_acc
285 Scanline_double = 1; // should be default for new player
288 Scanline_double = 1; // should be default for new player
295 extern int Guided_in_big_window,Automap_always_hires;
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[];
302 WIN(extern char win95_current_joyname[]);
304 ubyte control_type_dos,control_type_win;
307 //read in the player's saved games. returns errno (0 == no error)
308 int read_player_file()
311 char filename[FILENAME_LEN+15];
313 char filename[FILENAME_LEN];
317 short player_file_version;
321 Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
324 sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
326 sprintf(filename, "Players/%.8s.plr",Players[Player_num].callsign);
329 if (!PHYSFS_exists(filename))
332 file = PHYSFS_openRead(filename);
337 if (file && isatty(fileno(file))) {
338 //if the callsign is the name of a tty device, prepend a char
340 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
341 file = PHYSFS_openRead(filename);
347 goto read_player_file_failed;
349 PHYSFS_readSLE32(file, &id);
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");
359 player_file_version = cfile_read_short(file);
361 if (player_file_version > 255) // bigendian file?
365 player_file_version = SWAPSHORT(player_file_version);
367 if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
368 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
373 Game_window_w = cfile_read_short(file);
374 Game_window_h = cfile_read_short(file);
377 Game_window_w = SWAPSHORT(Game_window_w);
378 Game_window_h = SWAPSHORT(Game_window_h);
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);
386 #ifdef PA_3DFX_VOODOO
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);
401 if (player_file_version >= 19)
402 Automap_always_hires = cfile_read_byte(file);
404 Auto_leveling_on = Default_leveling_on;
406 //read new highest level info
408 n_highest_levels = cfile_read_short(file);
410 n_highest_levels = SWAPSHORT(n_highest_levels);
411 Assert(n_highest_levels <= MAX_MISSIONS);
413 if (PHYSFS_read(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels)
414 goto read_player_file_failed;
421 len = MAX_MESSAGE_LEN;
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;
427 char dummy[4][MAX_MESSAGE_LEN];
429 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
435 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
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;
447 Config_control_type = control_type_win;
449 Config_control_type = control_type_dos;
453 joydefs_set_type(Config_control_type);
458 PrimaryOrder[i] = cfile_read_byte(file);
459 SecondaryOrder[i] = cfile_read_byte(file);
462 if (player_file_version>=16)
464 PHYSFS_readSLE32(file, &Cockpit_3d_view[0]);
465 PHYSFS_readSLE32(file, &Cockpit_3d_view[1]);
468 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
469 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
477 if (player_file_version>=22)
480 PHYSFS_readSLE32(file, &Netlife_kills);
481 PHYSFS_readSLE32(file, &Netlife_killed);
483 Netlife_kills = SWAPINT(Netlife_kills);
484 Netlife_killed = SWAPINT(Netlife_killed);
489 PHYSFS_readSLE32(file, &dummy);
490 PHYSFS_readSLE32(file, &dummy);
497 Netlife_kills=0; Netlife_killed=0;
501 if (player_file_version>=23)
503 PHYSFS_readSLE32(file, &i);
507 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
508 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
510 Netlife_kills=0; Netlife_killed=0;
511 nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
518 if (player_file_version >= 18)
519 PHYSFSX_readString(file, guidebot_name);
521 strcpy(guidebot_name,"GUIDE-BOT");
523 strcpy(real_guidebot_name,guidebot_name);
529 joy95_get_name(JOYSTICKID1, buf, 127);
530 if (player_file_version >= 24)
531 PHYSFSX_readString(file, win95_current_joyname);
533 strcpy(win95_current_joyname, "Old Player File");
535 mprintf((0, "Detected joystick: %s\n", buf));
536 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
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];
544 if (player_file_version >= 24)
545 PHYSFSX_readString(file, buf); // Just read it in fpr DPS.
549 if (player_file_version >= 25)
550 PHYSFS_read(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
552 for(i=0; i < MAX_D2X_CONTROLS; i++)
553 kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
555 if (!PHYSFS_close(file))
556 goto read_player_file_failed;
563 read_player_file_failed:
564 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
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
578 for (i=0;i<n_highest_levels;i++)
579 if (!stricmp(highest_levels[i].shortname, Current_mission_filename))
582 if (i==n_highest_levels) { //not found. create entry
585 i--; //take last entry
589 strcpy(highest_levels[i].shortname, Current_mission_filename);
590 highest_levels[i].level_num = 0;
596 //set a new highest level for player for this mission
597 void set_highest_level(int levelnum)
601 if ((ret=read_player_file()) != EZERO)
602 if (ret != ENOENT) //if file doesn't exist, that's ok
605 i = find_hli_entry();
607 if (levelnum > highest_levels[i].level_num)
608 highest_levels[i].level_num = levelnum;
613 //gets the player's highest level from the file for this mission
614 int get_highest_level(void)
617 int highest_saturn_level = 0;
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;
626 i = highest_levels[find_hli_entry()].level_num;
627 if ( highest_saturn_level > i )
628 i = highest_saturn_level;
632 extern int Cockpit_mode_save;
635 //write out player's saved games. returns errno (0 == no error)
636 int write_player_file()
639 char filename[FILENAME_LEN+15];
641 char filename[FILENAME_LEN]; // because of ":Players:" path
646 // #ifdef APPLE_DEMO // no saving of player files in Apple OEM version
653 sprintf(filename,"%s.plr",Players[Player_num].callsign);
655 sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
657 file = PHYSFS_openWrite(filename);
659 #if 0 //ndef MACINTOSH
661 if (file && isatty(fileno(file))) {
663 //if the callsign is the name of a tty device, prepend a char
666 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
667 file = PHYSFS_openWrite(filename);
674 //Write out player's info
675 PHYSFS_writeULE32(file, SAVE_FILE_ID);
676 PHYSFS_writeULE16(file, PLAYER_FILE_VERSION);
678 PHYSFS_writeULE16(file, Game_window_w);
679 PHYSFS_writeULE16(file, Game_window_h);
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);
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;
697 if ((PHYSFS_write(file, Network_message_macro, MAX_MESSAGE_LEN, 4) != 4))
698 goto write_player_file_failed;
701 char dummy[4][MAX_MESSAGE_LEN]; // Pull the messages from a hat! ;-)
703 if ((PHYSFS_write(file, dummy, MAX_MESSAGE_LEN, 4) != 4))
704 goto write_player_file_failed;
712 control_type_win = Config_control_type;
714 control_type_dos = Config_control_type;
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;
726 for (i = 0; i < 11; i++)
728 PHYSFS_write(file, &PrimaryOrder[i], sizeof(ubyte), 1);
729 PHYSFS_write(file, &SecondaryOrder[i], sizeof(ubyte), 1);
732 PHYSFS_writeULE32(file, Cockpit_3d_view[0]);
733 PHYSFS_writeULE32(file, Cockpit_3d_view[1]);
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));
741 PHYSFS_writeULE32(file, 0);
742 PHYSFS_writeULE32(file, 0);
743 i = get_lifetime_checksum (0, 0);
745 PHYSFS_writeULE32(file, i);
748 //write guidebot name
749 PHYSFSX_writeString(file, real_guidebot_name);
754 joy95_get_name(JOYSTICKID1, buf, 127);
756 strcpy(buf, "DOS joystick");
758 PHYSFSX_writeString(file, buf); // Write out current joystick for player.
761 PHYSFS_write(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
763 if (!PHYSFS_close(file))
764 goto write_player_file_failed;
766 #ifdef MACINTOSH // set filetype and creator for playerfile
772 strcpy(pfilename, filename);
774 err = HGetFInfo(0, 0, pfilename, &finfo);
775 finfo.fdType = 'PLYR';
776 finfo.fdCreator = 'DCT2';
777 err = HSetFInfo(0, 0, pfilename, &finfo);
783 write_player_file_failed:
784 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
788 PHYSFS_delete(filename); //delete bogus file
794 //update the player's highest level. returns errno (0 == no error)
795 int update_player_file()
799 if ((ret=read_player_file()) != EZERO)
800 if (ret != ENOENT) //if file doesn't exist, that's ok
803 return write_player_file();
806 int get_lifetime_checksum (int a,int b)
810 // confusing enough to beat amateur disassemblers? Lets hope so