1 /* $Id: playsave.c,v 1.24 2005-07-30 01:50:17 chris 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)
69 #define SAVE_FILE_ID MAKE_SIG('D','P','L','R')
73 #include <Errors.h> // mac doesn't have "normal" error numbers -- must use mac equivs
78 #elif defined(_WIN32_WCE)
81 # define strerror(x) "Unknown Error"
84 int get_lifetime_checksum (int a,int b);
91 short n_highest_levels;
93 hli highest_levels[MAX_MISSIONS];
95 #define PLAYER_FILE_VERSION 25 //increment this every time the player file changes
97 //version 5 -> 6: added new highest level information
98 //version 6 -> 7: stripped out the old saved_game array.
99 //version 7 -> 8: added reticle flag, & window size
100 //version 8 -> 9: removed player_struct_version
101 //version 9 -> 10: added default display mode
102 //version 10 -> 11: added all toggles in toggle menu
103 //version 11 -> 12: added weapon ordering
104 //version 12 -> 13: added more keys
105 //version 13 -> 14: took out marker key
106 //version 14 -> 15: added guided in big window
107 //version 15 -> 16: added small windows in cockpit
108 //version 16 -> 17: ??
109 //version 17 -> 18: save guidebot name
110 //version 18 -> 19: added automap-highres flag
111 //version 19 -> 20: added kconfig data for windows joysticks
112 //version 20 -> 21: save seperate config types for DOS & Windows
113 //version 21 -> 22: save lifetime netstats
114 //version 22 -> 23: ??
115 //version 23 -> 24: add name of joystick for windows version.
116 //version 24 -> 25: add d2x keys array
118 #define COMPATIBLE_PLAYER_FILE_VERSION 17
121 int Default_leveling_on=1;
122 extern ubyte SecondaryOrder[],PrimaryOrder[];
123 extern void InitWeaponOrdering();
126 extern ubyte default_firebird_settings[];
127 extern ubyte default_mousestick_settings[];
130 int new_player_config()
133 int i,j,control_choice;
135 int mct=CONTROL_MAX_TYPES;
141 InitWeaponOrdering (); //setup default weapon priorities
143 #if defined(MACINTOSH) && defined(USE_ISP)
148 #if !defined(MACINTOSH) && !defined(WINDOWS)
149 for (i=0; i<mct; i++ ) {
150 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
152 #elif defined(WINDOWS)
153 m[0].type = NM_TYPE_MENU; m[0].text = CONTROL_TEXT(0);
154 m[1].type = NM_TYPE_MENU; m[1].text = CONTROL_TEXT(5);
155 m[2].type = NM_TYPE_MENU; m[2].text = CONTROL_TEXT(7);
158 for (i = 0; i < 6; i++) {
159 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
161 m[4].text = "Gravis Firebird/Mousetick II";
162 m[3].text = "Thrustmaster";
166 m[0].text = TXT_CONTROL_KEYBOARD;
169 if (Config_control_type==CONTROL_NONE) control_choice = 0;
170 else if (Config_control_type == CONTROL_MOUSE) control_choice = 1;
171 else if (Config_control_type == CONTROL_WINJOYSTICK) control_choice = 2;
172 else control_choice = 0;
174 control_choice = Config_control_type; // Assume keyboard
178 control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
183 if ( control_choice < 0 )
186 #if defined(MACINTOSH) && defined(USE_ISP)
188 else // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
194 for (i=0;i<CONTROL_MAX_TYPES; i++ )
195 for (j=0;j<MAX_CONTROLS; j++ )
196 kconfig_settings[i][j] = default_kconfig_settings[i][j];
197 //added on 2/4/99 by Victor Rachels for new keys
198 for(i=0; i < MAX_D2X_CONTROLS; i++)
199 kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
200 //end this section addition - VR
203 Config_control_type = control_choice;
206 if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
207 else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
209 // if (Config_control_type == CONTROL_WINJOYSTICK)
210 // joydefs_calibrate();
213 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
214 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
215 if (i==0) goto RetrySelection;
218 if ( (Config_control_type>0) && (Config_control_type<5)) {
221 #else // some macintosh only stuff here
222 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
223 extern char *tm_warning;
225 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
226 if (i==0) goto RetrySelection;
227 } else if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO ) {
228 extern char *ch_warning;
230 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
231 if (i==0) goto RetrySelection;
232 } else if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD ) {
233 extern char *ms_warning;
235 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
236 if (i==0) goto RetrySelection;
237 // stupid me -- get real default setting for either mousestick or firebird
238 joydefs_set_type( Config_control_type );
239 if (joy_have_firebird())
240 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
241 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
243 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
244 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
245 kc_set_controls(); // reset the joystick control
247 if ( (Config_control_type>0) && (Config_control_type<5) ) {
248 joydefs_set_type( Config_control_type );
255 Player_default_difficulty = 1;
256 Auto_leveling_on = Default_leveling_on = 1;
257 n_highest_levels = 1;
258 highest_levels[0].shortname[0] = 0; //no name for mission 0
259 highest_levels[0].level_num = 1; //was highest level in old struct
260 Config_joystick_sensitivity = 8;
261 Cockpit_3d_view[0]=CV_NONE;
262 Cockpit_3d_view[1]=CV_NONE;
264 // Default taunt macros
266 strcpy(Network_message_macro[0], "Why can't we all just get along?");
267 strcpy(Network_message_macro[1], "Hey, I got a present for ya");
268 strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
269 strcpy(Network_message_macro[3], "This one's headed for Uranus");
270 Netlife_kills=0; Netlife_killed=0;
276 extern int Guided_in_big_window,Automap_always_hires;
278 //this length must match the value in escort.c
279 #define GUIDEBOT_NAME_LEN 9
280 extern char guidebot_name[];
281 extern char real_guidebot_name[];
283 WIN(extern char win95_current_joyname[]);
285 ubyte control_type_dos,control_type_win;
288 //read in the player's saved games. returns errno (0 == no error)
289 int read_player_file()
292 char filename[FILENAME_LEN+15];
294 char filename[FILENAME_LEN];
298 short player_file_version;
302 Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
305 sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
307 sprintf(filename, "Players/%.8s.plr",Players[Player_num].callsign);
310 if (!PHYSFS_exists(filename))
313 file = PHYSFSX_openReadBuffered(filename);
318 if (file && isatty(fileno(file))) {
319 //if the callsign is the name of a tty device, prepend a char
321 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
322 file = PHYSFSX_openReadBuffered(filename);
328 goto read_player_file_failed;
330 PHYSFS_readSLE32(file, &id);
332 // SWAPINT added here because old versions of d2x
333 // used the wrong byte order.
334 if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
335 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
340 player_file_version = cfile_read_short(file);
342 if (player_file_version > 255) // bigendian file?
346 player_file_version = SWAPSHORT(player_file_version);
348 if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
349 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
354 Game_window_w = cfile_read_short(file);
355 Game_window_h = cfile_read_short(file);
358 Game_window_w = SWAPSHORT(Game_window_w);
359 Game_window_h = SWAPSHORT(Game_window_h);
362 Player_default_difficulty = cfile_read_byte(file);
363 Default_leveling_on = cfile_read_byte(file);
364 Reticle_on = cfile_read_byte(file);
365 Cockpit_mode = cfile_read_byte(file);
366 Default_display_mode = cfile_read_byte(file);
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);
371 if (player_file_version >= 19)
372 Automap_always_hires = cfile_read_byte(file);
374 Auto_leveling_on = Default_leveling_on;
376 //read new highest level info
378 n_highest_levels = cfile_read_short(file);
380 n_highest_levels = SWAPSHORT(n_highest_levels);
381 Assert(n_highest_levels <= MAX_MISSIONS);
383 if (PHYSFS_read(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels)
384 goto read_player_file_failed;
391 len = MAX_MESSAGE_LEN;
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;
397 char dummy[4][MAX_MESSAGE_LEN];
399 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
405 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
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;
417 Config_control_type = control_type_win;
419 Config_control_type = control_type_dos;
423 joydefs_set_type(Config_control_type);
428 PrimaryOrder[i] = cfile_read_byte(file);
429 SecondaryOrder[i] = cfile_read_byte(file);
432 if (player_file_version>=16)
434 PHYSFS_readSLE32(file, &Cockpit_3d_view[0]);
435 PHYSFS_readSLE32(file, &Cockpit_3d_view[1]);
438 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
439 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
447 if (player_file_version>=22)
450 PHYSFS_readSLE32(file, &Netlife_kills);
451 PHYSFS_readSLE32(file, &Netlife_killed);
453 Netlife_kills = SWAPINT(Netlife_kills);
454 Netlife_killed = SWAPINT(Netlife_killed);
459 PHYSFS_readSLE32(file, &dummy);
460 PHYSFS_readSLE32(file, &dummy);
467 Netlife_kills=0; Netlife_killed=0;
471 if (player_file_version>=23)
473 PHYSFS_readSLE32(file, &i);
477 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
478 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
480 Netlife_kills=0; Netlife_killed=0;
481 nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
488 if (player_file_version >= 18)
489 PHYSFSX_readString(file, guidebot_name);
491 strcpy(guidebot_name,"GUIDE-BOT");
493 strcpy(real_guidebot_name,guidebot_name);
499 joy95_get_name(JOYSTICKID1, buf, 127);
500 if (player_file_version >= 24)
501 PHYSFSX_readString(file, win95_current_joyname);
503 strcpy(win95_current_joyname, "Old Player File");
505 mprintf((0, "Detected joystick: %s\n", buf));
506 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
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];
514 if (player_file_version >= 24)
515 PHYSFSX_readString(file, buf); // Just read it in fpr DPS.
519 if (player_file_version >= 25)
520 PHYSFS_read(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
522 for(i=0; i < MAX_D2X_CONTROLS; i++)
523 kconfig_d2x_settings[i] = default_kconfig_d2x_settings[i];
525 if (!PHYSFS_close(file))
526 goto read_player_file_failed;
533 read_player_file_failed:
534 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
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
548 for (i=0;i<n_highest_levels;i++)
549 if (!stricmp(highest_levels[i].shortname, Current_mission_filename))
552 if (i==n_highest_levels) { //not found. create entry
555 i--; //take last entry
559 strcpy(highest_levels[i].shortname, Current_mission_filename);
560 highest_levels[i].level_num = 0;
566 //set a new highest level for player for this mission
567 void set_highest_level(int levelnum)
571 if ((ret=read_player_file()) != EZERO)
572 if (ret != ENOENT) //if file doesn't exist, that's ok
575 i = find_hli_entry();
577 if (levelnum > highest_levels[i].level_num)
578 highest_levels[i].level_num = levelnum;
583 //gets the player's highest level from the file for this mission
584 int get_highest_level(void)
587 int highest_saturn_level = 0;
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;
596 i = highest_levels[find_hli_entry()].level_num;
597 if ( highest_saturn_level > i )
598 i = highest_saturn_level;
602 extern int Cockpit_mode_save;
605 //write out player's saved games. returns errno (0 == no error)
606 int write_player_file()
609 char filename[FILENAME_LEN+15];
611 char filename[FILENAME_LEN]; // because of ":Players:" path
616 // #ifdef APPLE_DEMO // no saving of player files in Apple OEM version
623 sprintf(filename,"%s.plr",Players[Player_num].callsign);
625 sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
627 file = PHYSFSX_openWriteBuffered(filename);
629 #if 0 //ndef MACINTOSH
631 if (file && isatty(fileno(file))) {
633 //if the callsign is the name of a tty device, prepend a char
636 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
637 file = PHYSFSX_openWriteBuffered(filename);
644 //Write out player's info
645 PHYSFS_writeULE32(file, SAVE_FILE_ID);
646 PHYSFS_writeULE16(file, PLAYER_FILE_VERSION);
648 PHYSFS_writeULE16(file, Game_window_w);
649 PHYSFS_writeULE16(file, Game_window_h);
651 PHYSFSX_writeU8(file, Player_default_difficulty);
652 PHYSFSX_writeU8(file, Auto_leveling_on);
653 PHYSFSX_writeU8(file, Reticle_on);
654 PHYSFSX_writeU8(file, (Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode); //if have saved mode, write it instead of letterbox/rear view
655 PHYSFSX_writeU8(file, Default_display_mode);
656 PHYSFSX_writeU8(file, Missile_view_enabled);
657 PHYSFSX_writeU8(file, Headlight_active_default);
658 PHYSFSX_writeU8(file, Guided_in_big_window);
659 PHYSFSX_writeU8(file, Automap_always_hires);
661 //write higest level info
662 PHYSFS_writeULE16(file, n_highest_levels);
663 if ((PHYSFS_write(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels))
664 goto write_player_file_failed;
667 if ((PHYSFS_write(file, Network_message_macro, MAX_MESSAGE_LEN, 4) != 4))
668 goto write_player_file_failed;
671 char dummy[4][MAX_MESSAGE_LEN]; // Pull the messages from a hat! ;-)
673 if ((PHYSFS_write(file, dummy, MAX_MESSAGE_LEN, 4) != 4))
674 goto write_player_file_failed;
682 control_type_win = Config_control_type;
684 control_type_dos = Config_control_type;
687 if (PHYSFS_write(file, kconfig_settings, MAX_CONTROLS*CONTROL_MAX_TYPES, 1) != 1)
688 goto write_player_file_failed;
689 else if (PHYSFS_write(file, &control_type_dos, sizeof(ubyte), 1) != 1)
690 goto write_player_file_failed;
691 else if (PHYSFS_write(file, &control_type_win, sizeof(ubyte), 1) != 1)
692 goto write_player_file_failed;
693 else if (PHYSFS_write(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) != 1)
694 goto write_player_file_failed;
696 for (i = 0; i < 11; i++)
698 PHYSFS_write(file, &PrimaryOrder[i], sizeof(ubyte), 1);
699 PHYSFS_write(file, &SecondaryOrder[i], sizeof(ubyte), 1);
702 PHYSFS_writeULE32(file, Cockpit_3d_view[0]);
703 PHYSFS_writeULE32(file, Cockpit_3d_view[1]);
706 PHYSFS_writeULE32(file, Netlife_kills);
707 PHYSFS_writeULE32(file, Netlife_killed);
708 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
709 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
711 PHYSFS_writeULE32(file, 0);
712 PHYSFS_writeULE32(file, 0);
713 i = get_lifetime_checksum (0, 0);
715 PHYSFS_writeULE32(file, i);
718 //write guidebot name
719 PHYSFSX_writeString(file, real_guidebot_name);
724 joy95_get_name(JOYSTICKID1, buf, 127);
726 strcpy(buf, "DOS joystick");
728 PHYSFSX_writeString(file, buf); // Write out current joystick for player.
731 PHYSFS_write(file, kconfig_d2x_settings, MAX_D2X_CONTROLS, 1);
733 if (!PHYSFS_close(file))
734 goto write_player_file_failed;
736 #ifdef MACINTOSH // set filetype and creator for playerfile
742 strcpy(pfilename, filename);
744 err = HGetFInfo(0, 0, pfilename, &finfo);
745 finfo.fdType = 'PLYR';
746 finfo.fdCreator = 'DCT2';
747 err = HSetFInfo(0, 0, pfilename, &finfo);
753 write_player_file_failed:
754 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
758 PHYSFS_delete(filename); //delete bogus file
764 //update the player's highest level. returns errno (0 == no error)
765 int update_player_file()
769 if ((ret=read_player_file()) != EZERO)
770 if (ret != ENOENT) //if file doesn't exist, that's ok
773 return write_player_file();
776 int get_lifetime_checksum (int a,int b)
780 // confusing enough to beat amateur disassemblers? Lets hope so