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.
15 * $Source: /cvs/cvsroot/d2x/main/playsave.c,v $
18 * $Date: 2001-11-12 00:59:07 $
20 * Functions to load & save player games
22 * $Log: not supported by cvs2svn $
23 * Revision 1.3 2001/11/11 23:39:22 bradleyb
24 * Created header for MAKE_SIG macro
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
83 int get_lifetime_checksum (int a,int b);
92 hli highest_levels[MAX_MISSIONS];
94 #define PLAYER_FILE_VERSION 24 //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.
116 #define COMPATIBLE_PLAYER_FILE_VERSION 17
119 int Default_leveling_on=1;
120 extern ubyte SecondaryOrder[],PrimaryOrder[];
121 extern void InitWeaponOrdering();
124 extern ubyte default_firebird_settings[];
125 extern ubyte default_mousestick_settings[];
128 int new_player_config()
131 int i,j,control_choice;
133 int mct=CONTROL_MAX_TYPES;
139 InitWeaponOrdering (); //setup default weapon priorities
141 #if defined(MACINTOSH) && defined(USE_ISP)
146 #if !defined(MACINTOSH) && !defined(WINDOWS)
147 for (i=0; i<mct; i++ ) {
148 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
150 #elif defined(WINDOWS)
151 m[0].type = NM_TYPE_MENU; m[0].text = CONTROL_TEXT(0);
152 m[1].type = NM_TYPE_MENU; m[1].text = CONTROL_TEXT(5);
153 m[2].type = NM_TYPE_MENU; m[2].text = CONTROL_TEXT(7);
156 for (i = 0; i < 6; i++) {
157 m[i].type = NM_TYPE_MENU; m[i].text = CONTROL_TEXT(i);
159 m[4].text = "Gravis Firebird/Mousetick II";
160 m[3].text = "Thrustmaster";
164 m[0].text = TXT_CONTROL_KEYBOARD;
167 if (Config_control_type==CONTROL_NONE) control_choice = 0;
168 else if (Config_control_type == CONTROL_MOUSE) control_choice = 1;
169 else if (Config_control_type == CONTROL_WINJOYSTICK) control_choice = 2;
170 else control_choice = 0;
172 control_choice = Config_control_type; // Assume keyboard
176 control_choice = newmenu_do1( NULL, TXT_CHOOSE_INPUT, i, m, NULL, control_choice );
181 if ( control_choice < 0 )
184 #if defined(MACINTOSH) && defined(USE_ISP)
186 else // !!!!NOTE ... link to above if (!ISpEnabled()), this is a really crappy function
192 for (i=0;i<CONTROL_MAX_TYPES; i++ )
193 for (j=0;j<MAX_CONTROLS; j++ )
194 kconfig_settings[i][j] = default_kconfig_settings[i][j];
197 Config_control_type = control_choice;
200 if (control_choice == 1) Config_control_type = CONTROL_MOUSE;
201 else if (control_choice == 2) Config_control_type = CONTROL_WINJOYSTICK;
203 // if (Config_control_type == CONTROL_WINJOYSTICK)
204 // joydefs_calibrate();
207 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
208 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, TXT_FCS );
209 if (i==0) goto RetrySelection;
212 if ( (Config_control_type>0) && (Config_control_type<5)) {
215 #else // some macintosh only stuff here
216 if ( Config_control_type==CONTROL_THRUSTMASTER_FCS) {
217 extern char *tm_warning;
219 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, tm_warning );
220 if (i==0) goto RetrySelection;
221 } else if ( Config_control_type==CONTROL_FLIGHTSTICK_PRO ) {
222 extern char *ch_warning;
224 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ch_warning );
225 if (i==0) goto RetrySelection;
226 } else if ( Config_control_type==CONTROL_GRAVIS_GAMEPAD ) {
227 extern char *ms_warning;
229 i = nm_messagebox( TXT_IMPORTANT_NOTE, 2, "Choose another", TXT_OK, ms_warning );
230 if (i==0) goto RetrySelection;
231 // stupid me -- get real default setting for either mousestick or firebird
232 joydefs_set_type( Config_control_type );
233 if (joy_have_firebird())
234 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
235 kconfig_settings[Config_control_type][i] = default_firebird_settings[i];
237 for (i=0; i<NUM_OTHER_CONTROLS; i++ )
238 kconfig_settings[Config_control_type][i] = default_mousestick_settings[i];
239 kc_set_controls(); // reset the joystick control
241 if ( (Config_control_type>0) && (Config_control_type<5) ) {
242 joydefs_set_type( Config_control_type );
249 Player_default_difficulty = 1;
250 Auto_leveling_on = Default_leveling_on = 1;
251 n_highest_levels = 1;
252 highest_levels[0].shortname[0] = 0; //no name for mission 0
253 highest_levels[0].level_num = 1; //was highest level in old struct
254 Config_joystick_sensitivity = 8;
255 Cockpit_3d_view[0]=CV_NONE;
256 Cockpit_3d_view[1]=CV_NONE;
258 // Default taunt macros
260 strcpy(Network_message_macro[0], "Why can't we all just get along?");
261 strcpy(Network_message_macro[1], "Hey, I got a present for ya");
262 strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
263 strcpy(Network_message_macro[3], "This one's headed for Uranus");
264 Netlife_kills=0; Netlife_killed=0;
271 Scanline_double = 0; // no pixel doubling for poly_acc
275 Scanline_double = 1; // should be default for new player
278 Scanline_double = 1; // should be default for new player
285 static int read_int(FILE *file)
289 if (fread( &i, sizeof(i), 1, file) != 1)
290 Error( "Error reading int in gamesave.c" );
295 static short read_short(FILE *file)
299 if (fread( &s, sizeof(s), 1, file) != 1)
300 Error( "Error reading short in gamesave.c" );
305 static byte read_byte(FILE *file)
309 if (fread( &s, sizeof(s), 1, file) != 1)
310 Error( "Error reading byte in gamesave.c" );
315 static void write_int(int i,FILE *file)
317 if (fwrite( &i, sizeof(i), 1, file) != 1)
318 Error( "Error writing int in gamesave.c" );
322 static void write_short(short s,FILE *file)
324 if (fwrite( &s, sizeof(s), 1, file) != 1)
325 Error( "Error writing short in gamesave.c" );
329 static void write_byte(byte i,FILE *file)
331 if (fwrite( &i, sizeof(i), 1, file) != 1)
332 Error( "Error writing byte in gamesave.c" );
336 extern int Guided_in_big_window,Automap_always_hires;
338 //this length must match the value in escort.c
339 #define GUIDEBOT_NAME_LEN 9
340 extern char guidebot_name[];
341 extern char real_guidebot_name[];
343 WIN(extern char win95_current_joyname[]);
345 void read_string(char *s, FILE *f)
352 while (!feof(f) && *s++!=0);
355 void write_string(char *s, FILE *f)
362 ubyte control_type_dos,control_type_win;
364 //read in the player's saved games. returns errno (0 == no error)
365 int read_player_file()
368 char filename[FILENAME_LEN+15];
370 char filename[FILENAME_LEN];
373 int errno_ret = EZERO;
374 int id,player_file_version,i;
377 Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
380 sprintf(filename,"%.8s.plr",Players[Player_num].callsign);
382 sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
384 file = fopen(filename,"rb");
388 if (file && isatty(fileno(file))) {
389 //if the callsign is the name of a tty device, prepend a char
391 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
392 file = fopen(filename,"rb");
402 // SWAPINT added here because old versions of d2x
403 // used the wrong byte order.
404 if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
405 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
410 player_file_version = read_short(file);
412 if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
413 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
418 Game_window_w = read_short(file);
419 Game_window_h = read_short(file);
421 Player_default_difficulty = read_byte(file);
422 Default_leveling_on = read_byte(file);
423 Reticle_on = read_byte(file);
424 Cockpit_mode = read_byte(file);
426 #ifdef PA_3DFX_VOODOO
436 Default_display_mode = read_byte(file);
437 Missile_view_enabled = read_byte(file);
438 Headlight_active_default = read_byte(file);
439 Guided_in_big_window = read_byte(file);
441 if (player_file_version >= 19)
442 Automap_always_hires = read_byte(file);
444 Auto_leveling_on = Default_leveling_on;
446 //read new highest level info
448 n_highest_levels = read_short(file);
449 if (fread(highest_levels,sizeof(hli),n_highest_levels,file) != n_highest_levels) {
460 len = MAX_MESSAGE_LEN;
462 for (i = 0; i < 4; i++)
463 if (fread(Network_message_macro[i], len, 1, file) != 1)
464 {errno_ret = errno; break;}
466 char dummy[4][MAX_MESSAGE_LEN];
467 fread(dummy, MAX_MESSAGE_LEN, 4, file);
474 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
476 if (fread( kconfig_settings, MAX_CONTROLS*n_control_types, 1, file )!=1)
478 else if (fread((ubyte *)&control_type_dos, sizeof(ubyte), 1, file )!=1)
480 else if (player_file_version >= 21 && fread((ubyte *)&control_type_win, sizeof(ubyte), 1, file )!=1)
482 else if (fread(&Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
486 Config_control_type = control_type_win;
488 Config_control_type = control_type_dos;
492 joydefs_set_type(Config_control_type);
497 PrimaryOrder[i]=read_byte (file);
498 SecondaryOrder[i]=read_byte(file);
501 if (player_file_version>=16)
503 Cockpit_3d_view[0]=read_int(file);
504 Cockpit_3d_view[1]=read_int(file);
508 if (errno_ret==EZERO) {
514 if (player_file_version>=22)
517 Netlife_kills=read_int (file);
518 Netlife_killed=read_int (file);
520 read_int(file); read_int(file);
526 Netlife_kills=0; Netlife_killed=0;
530 if (player_file_version>=23)
534 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
535 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
537 Netlife_kills=0; Netlife_killed=0;
538 nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
545 if (player_file_version >= 18)
546 read_string(guidebot_name,file);
548 strcpy(guidebot_name,"GUIDE-BOT");
550 strcpy(real_guidebot_name,guidebot_name);
556 joy95_get_name(JOYSTICKID1, buf, 127);
557 if (player_file_version >= 24)
558 read_string(win95_current_joyname, file);
560 strcpy(win95_current_joyname, "Old Player File");
562 mprintf((0, "Detected joystick: %s\n", buf));
563 mprintf((0, "Player's joystick: %s\n", win95_current_joyname));
565 if (strcmp(win95_current_joyname, buf)) {
566 for (i = 0; i < MAX_CONTROLS; i++)
567 kconfig_settings[CONTROL_WINJOYSTICK][i] =
568 default_kconfig_settings[CONTROL_WINJOYSTICK][i];
571 if (player_file_version >= 24)
572 read_string(buf, file); // Just read it in fpr DPS.
576 if (fclose(file) && errno_ret==EZERO)
587 //finds entry for this level in table. if not found, returns ptr to
588 //empty entry. If no empty entries, takes over last one
593 for (i=0;i<n_highest_levels;i++)
594 if (!stricmp(highest_levels[i].shortname,Mission_list[Current_mission_num].filename))
597 if (i==n_highest_levels) { //not found. create entry
600 i--; //take last entry
604 strcpy(highest_levels[i].shortname,Mission_list[Current_mission_num].filename);
605 highest_levels[i].level_num = 0;
611 //set a new highest level for player for this mission
612 void set_highest_level(int levelnum)
616 if ((ret=read_player_file()) != EZERO)
617 if (ret != ENOENT) //if file doesn't exist, that's ok
620 i = find_hli_entry();
622 if (levelnum > highest_levels[i].level_num)
623 highest_levels[i].level_num = levelnum;
628 //gets the player's highest level from the file for this mission
629 int get_highest_level(void)
632 int highest_saturn_level = 0;
635 if (strlen(Mission_list[Current_mission_num].filename)==0 ) {
636 for (i=0;i<n_highest_levels;i++)
637 if (!stricmp(highest_levels[i].shortname, "DESTSAT")) // Destination Saturn.
638 highest_saturn_level = highest_levels[i].level_num;
641 i = highest_levels[find_hli_entry()].level_num;
642 if ( highest_saturn_level > i )
643 i = highest_saturn_level;
647 extern int Cockpit_mode_save;
649 //write out player's saved games. returns errno (0 == no error)
650 int write_player_file()
653 char filename[FILENAME_LEN+15];
655 char filename[FILENAME_LEN]; // because of ":Players:" path
660 // #ifdef APPLE_DEMO // no saving of player files in Apple OEM version
664 errno_ret = WriteConfigFile();
667 sprintf(filename,"%s.plr",Players[Player_num].callsign);
669 sprintf(filename, ":Players:%.8s.plr",Players[Player_num].callsign);
671 file = fopen(filename,"wb");
675 if (file && isatty(fileno(file))) {
677 //if the callsign is the name of a tty device, prepend a char
680 sprintf(filename,"$%.7s.plr",Players[Player_num].callsign);
681 file = fopen(filename,"wb");
690 //Write out player's info
691 write_int(SAVE_FILE_ID,file);
692 write_short(PLAYER_FILE_VERSION,file);
694 write_short(Game_window_w,file);
695 write_short(Game_window_h,file);
697 write_byte(Player_default_difficulty,file);
698 write_byte(Auto_leveling_on,file);
699 write_byte(Reticle_on,file);
700 write_byte((Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode,file); //if have saved mode, write it instead of letterbox/rear view
701 write_byte(Default_display_mode,file);
702 write_byte(Missile_view_enabled,file);
703 write_byte(Headlight_active_default,file);
704 write_byte(Guided_in_big_window,file);
705 write_byte(Automap_always_hires,file);
707 //write higest level info
708 write_short(n_highest_levels,file);
709 if ((fwrite(highest_levels, sizeof(hli), n_highest_levels, file) != n_highest_levels)) {
716 if ((fwrite(Network_message_macro, MAX_MESSAGE_LEN, 4, file) != 4)) {
722 fseek( file, MAX_MESSAGE_LEN * 4, SEEK_CUR );
729 control_type_win = Config_control_type;
731 control_type_dos = Config_control_type;
734 if (fwrite( kconfig_settings, MAX_CONTROLS*CONTROL_MAX_TYPES, 1, file )!=1)
736 else if (fwrite( &control_type_dos, sizeof(ubyte), 1, file )!=1)
738 else if (fwrite( &control_type_win, sizeof(ubyte), 1, file )!=1)
740 else if (fwrite( &Config_joystick_sensitivity, sizeof(ubyte), 1, file )!=1)
745 fwrite (&PrimaryOrder[i],sizeof(ubyte),1,file);
746 fwrite (&SecondaryOrder[i],sizeof(ubyte),1,file);
748 write_int (Cockpit_3d_view[0],file);
749 write_int (Cockpit_3d_view[1],file);
752 write_int (Netlife_kills,file);
753 write_int (Netlife_killed,file);
754 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
755 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
759 i = get_lifetime_checksum (0, 0);
764 //write guidebot name
765 write_string(real_guidebot_name,file);
770 joy95_get_name(JOYSTICKID1, buf, 127);
772 strcpy(buf, "DOS joystick");
774 write_string(buf, file); // Write out current joystick for player.
780 if (errno_ret != EZERO) {
781 remove(filename); //delete bogus file
782 nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s",TXT_ERROR_WRITING_PLR, strerror(errno_ret));
785 #ifdef MACINTOSH // set filetype and creator for playerfile
791 strcpy(pfilename, filename);
793 err = HGetFInfo(0, 0, pfilename, &finfo);
794 finfo.fdType = 'PLYR';
795 finfo.fdCreator = 'DCT2';
796 err = HSetFInfo(0, 0, pfilename, &finfo);
804 //update the player's highest level. returns errno (0 == no error)
805 int update_player_file()
809 if ((ret=read_player_file()) != EZERO)
810 if (ret != ENOENT) //if file doesn't exist, that's ok
813 return write_player_file();
816 int get_lifetime_checksum (int a,int b)
820 // confusing enough to beat amateur disassemblers? Lets hope so