]> icculus.org git repositories - btb/d2x.git/blob - main/playsave.c
cvars for weapon ordering
[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: removed kconfig data, joy name, guidebot name, control type, joy sensitivity
116
117 #define COMPATIBLE_PLAYER_FILE_VERSION          17
118
119
120 int Default_leveling_on=1;
121
122
123 int new_player_config()
124 {
125         Player_default_difficulty = 1;
126         Auto_leveling_on = Default_leveling_on = 1;
127         n_highest_levels = 1;
128         highest_levels[0].shortname[0] = 0;                     //no name for mission 0
129         highest_levels[0].level_num = 1;                                //was highest level in old struct
130         Cockpit_3d_view[0]=CV_NONE;
131         Cockpit_3d_view[1]=CV_NONE;
132
133         // Default taunt macros
134         #ifdef NETWORK
135         strcpy(Network_message_macro[0], "Why can't we all just get along?");
136         strcpy(Network_message_macro[1], "Hey, I got a present for ya");
137         strcpy(Network_message_macro[2], "I got a hankerin' for a spankerin'");
138         strcpy(Network_message_macro[3], "This one's headed for Uranus");
139         Netlife_kills=0; Netlife_killed=0;      
140         #endif
141         
142         return 1;
143 }
144
145 extern int Guided_in_big_window,Automap_always_hires;
146
147 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) };
148
149 //read in the player's saved games.  returns errno (0 == no error)
150 int read_player_file()
151 {
152         #ifdef MACINTOSH
153         char filename[FILENAME_LEN+15];
154         #else
155         char filename[FILENAME_LEN];
156         #endif
157         PHYSFS_file *file;
158         int id, i;
159         short player_file_version;
160         int rewrite_it=0;
161         int swap = 0;
162
163         Assert(Player_num>=0 && Player_num<MAX_PLAYERS);
164
165         sprintf(filename, PLAYER_DIR "%.8s.plr", Players[Player_num].callsign);
166
167         if (!PHYSFS_exists(filename))
168                 return ENOENT;
169
170         file = PHYSFSX_openReadBuffered(filename);
171
172 #if 0
173 #ifndef MACINTOSH
174         //check filename
175         if (file && isatty(fileno(file))) {
176                 //if the callsign is the name of a tty device, prepend a char
177                 PHYSFS_close(file);
178                 sprintf(filename, PLAYER_DIR "$%.7s.plr",Players[Player_num].callsign);
179                 file = PHYSFSX_openReadBuffered(filename);
180         }
181 #endif
182 #endif
183
184         if (!file)
185                 goto read_player_file_failed;
186
187         PHYSFS_readSLE32(file, &id);
188
189         // SWAPINT added here because old versions of d2x
190         // used the wrong byte order.
191         if (id!=SAVE_FILE_ID && id!=SWAPINT(SAVE_FILE_ID)) {
192                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
193                 PHYSFS_close(file);
194                 return -1;
195         }
196
197         player_file_version = cfile_read_short(file);
198
199         if (player_file_version > 255) // bigendian file?
200                 swap = 1;
201
202         if (swap)
203                 player_file_version = SWAPSHORT(player_file_version);
204
205         if (player_file_version<COMPATIBLE_PLAYER_FILE_VERSION) {
206                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
207                 PHYSFS_close(file);
208                 return -1;
209         }
210
211         Game_window_w = cfile_read_short(file);
212         Game_window_h = cfile_read_short(file);
213
214         if (swap) {
215                 Game_window_w = SWAPSHORT(Game_window_w);
216                 Game_window_h = SWAPSHORT(Game_window_h);
217         }
218
219         Player_default_difficulty = cfile_read_byte(file);
220         Default_leveling_on       = cfile_read_byte(file);
221         Reticle_on                = cfile_read_byte(file);
222         Cockpit_mode              = cfile_read_byte(file);
223
224         i                         = cfile_read_byte(file);
225         Default_display_mode = legacy_display_mode[i];
226
227         Missile_view_enabled      = cfile_read_byte(file);
228         Headlight_active_default  = cfile_read_byte(file);
229         Guided_in_big_window      = cfile_read_byte(file);
230                 
231         if (player_file_version >= 19)
232                 Automap_always_hires = cfile_read_byte(file);
233
234         Auto_leveling_on = Default_leveling_on;
235
236         //read new highest level info
237
238         n_highest_levels = cfile_read_short(file);
239         if (swap)
240                 n_highest_levels = SWAPSHORT(n_highest_levels);
241         Assert(n_highest_levels <= MAX_MISSIONS);
242
243         if (PHYSFS_read(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels)
244                 goto read_player_file_failed;
245
246         //read taunt macros
247         {
248 #ifdef NETWORK
249                 int i,len;
250
251                 len = MAX_MESSAGE_LEN;
252
253                 for (i = 0; i < 4; i++)
254                         if (PHYSFS_read(file, Network_message_macro[i], len, 1) != 1)
255                                 goto read_player_file_failed;
256 #else
257                 char dummy[4][MAX_MESSAGE_LEN];
258
259                 cfread(dummy, MAX_MESSAGE_LEN, 4, file);
260 #endif
261         }
262
263         //read kconfig data
264         {
265                 int n_control_types = (player_file_version<20)?7:CONTROL_MAX_TYPES;
266                 ubyte kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS], control_type_win;
267                 ubyte Config_control_type, Config_joystick_sensitivity;
268                 ubyte SecondaryOrder[11], PrimaryOrder[11];
269
270                 if (player_file_version < 25)
271                         if (PHYSFS_read(file, kconfig_settings, MAX_CONTROLS*n_control_types, 1) != 1)
272                                 goto read_player_file_failed;
273                 if (PHYSFS_read(file, (ubyte *)&Config_control_type, sizeof(ubyte), 1) != 1)
274                         goto read_player_file_failed;
275                 else if (player_file_version >= 21 && player_file_version < 25
276                         && PHYSFS_read(file, (ubyte *)&control_type_win, sizeof(ubyte), 1) != 1)
277                         goto read_player_file_failed;
278                 else if (PHYSFS_read(file, &Config_joystick_sensitivity, sizeof(ubyte), 1) !=1 )
279                         goto read_player_file_failed;
280                 
281                 for (i=0;i<11;i++)
282                 {
283                         PrimaryOrder[i] = cfile_read_byte(file);
284                         SecondaryOrder[i] = cfile_read_byte(file);
285                 }
286
287                 if (player_file_version>=16)
288                 {
289                         PHYSFS_readSLE32(file, &Cockpit_3d_view[0]);
290                         PHYSFS_readSLE32(file, &Cockpit_3d_view[1]);
291                         if (swap)
292                         {
293                                 Cockpit_3d_view[0] = SWAPINT(Cockpit_3d_view[0]);
294                                 Cockpit_3d_view[1] = SWAPINT(Cockpit_3d_view[1]);
295                         }
296                 }
297
298                 kc_set_controls();
299
300         }
301
302         if (player_file_version>=22)
303         {
304 #ifdef NETWORK
305                 PHYSFS_readSLE32(file, &Netlife_kills);
306                 PHYSFS_readSLE32(file, &Netlife_killed);
307                 if (swap) {
308                         Netlife_kills = SWAPINT(Netlife_kills);
309                         Netlife_killed = SWAPINT(Netlife_killed);
310                 }
311 #else
312                 {
313                         int dummy;
314                         PHYSFS_readSLE32(file, &dummy);
315                         PHYSFS_readSLE32(file, &dummy);
316                 }
317 #endif
318         }
319 #ifdef NETWORK
320         else
321         {
322                 Netlife_kills=0; Netlife_killed=0;
323         }
324 #endif
325
326         if (player_file_version>=23)
327         {
328                 PHYSFS_readSLE32(file, &i);
329                 if (swap)
330                         i = SWAPINT(i);
331 #ifdef NETWORK
332                 mprintf ((0,"Reading: lifetime checksum is %d\n",i));
333                 if (i!=get_lifetime_checksum (Netlife_kills,Netlife_killed))
334                 {
335                         Netlife_kills=0; Netlife_killed=0;
336                         nm_messagebox(NULL, 1, "Shame on me", "Trying to cheat eh?");
337                         rewrite_it=1;
338                 }
339 #endif
340         }
341
342         if (!PHYSFS_close(file))
343                 goto read_player_file_failed;
344
345         if (rewrite_it)
346                 write_player_file();
347
348         return EZERO;
349
350  read_player_file_failed:
351         nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
352         if (file)
353                 PHYSFS_close(file);
354
355         return -1;
356 }
357
358
359 //finds entry for this level in table.  if not found, returns ptr to 
360 //empty entry.  If no empty entries, takes over last one 
361 int find_hli_entry()
362 {
363         int i;
364
365         for (i=0;i<n_highest_levels;i++)
366                 if (!stricmp(highest_levels[i].shortname, Current_mission_filename))
367                         break;
368
369         if (i==n_highest_levels) {              //not found.  create entry
370
371                 if (i==MAX_MISSIONS)
372                         i--;            //take last entry
373                 else
374                         n_highest_levels++;
375
376                 strcpy(highest_levels[i].shortname, Current_mission_filename);
377                 highest_levels[i].level_num                     = 0;
378         }
379
380         return i;
381 }
382
383 //set a new highest level for player for this mission
384 void set_highest_level(int levelnum)
385 {
386         int ret,i;
387
388         if ((ret=read_player_file()) != EZERO)
389                 if (ret != ENOENT)              //if file doesn't exist, that's ok
390                         return;
391
392         i                       = find_hli_entry();
393
394         if (levelnum > highest_levels[i].level_num)
395                 highest_levels[i].level_num                     = levelnum;
396
397         write_player_file();
398 }
399
400 //gets the player's highest level from the file for this mission
401 int get_highest_level(void)
402 {
403         int i;
404         int highest_saturn_level                        = 0;
405         read_player_file();
406 #ifndef SATURN
407         if (strlen(Current_mission_filename)==0 )       {
408                 for (i=0;i<n_highest_levels;i++)
409                         if (!stricmp(highest_levels[i].shortname, "DESTSAT"))   //      Destination Saturn.
410                                 highest_saturn_level                    = highest_levels[i].level_num; 
411         }
412 #endif
413    i                    = highest_levels[find_hli_entry()].level_num;
414         if ( highest_saturn_level > i )
415         i                       = highest_saturn_level;
416         return i;
417 }
418
419 extern int Cockpit_mode_save;
420
421
422 //write out player's saved games.  returns errno (0 == no error)
423 int write_player_file()
424 {
425         char filename[FILENAME_LEN+15];
426         PHYSFS_file *file;
427         int i;
428
429 //      #ifdef APPLE_DEMO               // no saving of player files in Apple OEM version
430 //      return 0;
431 //      #endif
432
433         WriteConfigFile();
434
435         sprintf(filename, PLAYER_DIR "%s.plr", Players[Player_num].callsign);
436         file = PHYSFSX_openWriteBuffered(filename);
437
438 #if 0 //ndef MACINTOSH
439         //check filename
440         if (file && isatty(fileno(file))) {
441
442                 //if the callsign is the name of a tty device, prepend a char
443
444                 PHYSFS_close(file);
445                 sprintf(filename, PLAYER_DIR "$%.7s.plr", Players[Player_num].callsign);
446                 file = PHYSFSX_openWriteBuffered(filename);
447         }
448 #endif
449
450         if (!file)
451                 return -1;
452
453         //Write out player's info
454         PHYSFS_writeULE32(file, SAVE_FILE_ID);
455         PHYSFS_writeULE16(file, PLAYER_FILE_VERSION);
456
457         PHYSFS_writeULE16(file, Game_window_w);
458         PHYSFS_writeULE16(file, Game_window_h);
459
460         PHYSFSX_writeU8(file, Player_default_difficulty);
461         PHYSFSX_writeU8(file, Auto_leveling_on);
462         PHYSFSX_writeU8(file, Reticle_on);
463         PHYSFSX_writeU8(file, (Cockpit_mode_save!=-1)?Cockpit_mode_save:Cockpit_mode);  //if have saved mode, write it instead of letterbox/rear view
464
465         for (i = 0; i < (sizeof(legacy_display_mode) / sizeof(uint32_t)); i++) {
466                 if (legacy_display_mode[i] == Current_display_mode)
467                         break;
468         }
469         PHYSFSX_writeU8(file, i);
470
471         PHYSFSX_writeU8(file, Missile_view_enabled);
472         PHYSFSX_writeU8(file, Headlight_active_default);
473         PHYSFSX_writeU8(file, Guided_in_big_window);
474         PHYSFSX_writeU8(file, Automap_always_hires);
475
476         //write higest level info
477         PHYSFS_writeULE16(file, n_highest_levels);
478         if ((PHYSFS_write(file, highest_levels, sizeof(hli), n_highest_levels) != n_highest_levels))
479                 goto write_player_file_failed;
480
481 #ifdef NETWORK
482         if ((PHYSFS_write(file, Network_message_macro, MAX_MESSAGE_LEN, 4) != 4))
483                 goto write_player_file_failed;
484 #else
485         {
486                 char dummy[4][MAX_MESSAGE_LEN]; // Pull the messages from a hat! ;-)
487
488                 if ((PHYSFS_write(file, dummy, MAX_MESSAGE_LEN, 4) != 4))
489                         goto write_player_file_failed;
490         }
491 #endif
492
493         //write kconfig info
494         {
495                 if (PHYSFSX_writeU8(file, Config_control_type.intval) != 1)
496                         goto write_player_file_failed;
497                 else if (PHYSFSX_writeU8(file, Config_joystick_sensitivity.intval) != 1)
498                         goto write_player_file_failed;
499
500                 for (i = 0; i < 11; i++)
501                 {
502                         PHYSFS_write(file, &PrimaryOrder[i], sizeof(ubyte), 1);
503                         PHYSFS_write(file, &SecondaryOrder[i], sizeof(ubyte), 1);
504                 }
505
506                 PHYSFS_writeULE32(file, Cockpit_3d_view[0]);
507                 PHYSFS_writeULE32(file, Cockpit_3d_view[1]);
508
509 #ifdef NETWORK
510                 PHYSFS_writeULE32(file, Netlife_kills);
511                 PHYSFS_writeULE32(file, Netlife_killed);
512                 i=get_lifetime_checksum (Netlife_kills,Netlife_killed);
513                 mprintf ((0,"Writing: Lifetime checksum is %d\n",i));
514 #else
515                 PHYSFS_writeULE32(file, 0);
516                 PHYSFS_writeULE32(file, 0);
517                 i = get_lifetime_checksum (0, 0);
518 #endif
519                 PHYSFS_writeULE32(file, i);
520         }
521
522         if (!PHYSFS_close(file))
523                 goto write_player_file_failed;
524
525         #ifdef MACINTOSH                // set filetype and creator for playerfile
526         {
527                 FInfo finfo;
528                 Str255 pfilename;
529                 OSErr err;
530
531                 strcpy(pfilename, filename);
532                 c2pstr(pfilename);
533                 err = HGetFInfo(0, 0, pfilename, &finfo);
534                 finfo.fdType = 'PLYR';
535                 finfo.fdCreator = 'DCT2';
536                 err = HSetFInfo(0, 0, pfilename, &finfo);
537         }
538         #endif
539
540         return EZERO;
541
542  write_player_file_failed:
543         nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
544         if (file)
545         {
546                 PHYSFS_close(file);
547                 PHYSFS_delete(filename);        //delete bogus file
548         }
549
550         return -1;
551 }
552
553 //update the player's highest level.  returns errno (0 == no error)
554 int update_player_file()
555 {
556         int ret;
557
558         if ((ret=read_player_file()) != EZERO)
559                 if (ret != ENOENT)              //if file doesn't exist, that's ok
560                         return ret;
561
562         return write_player_file();
563 }
564
565 int get_lifetime_checksum (int a,int b)
566  {
567   int num;
568
569   // confusing enough to beat amateur disassemblers? Lets hope so
570
571   num=(a<<8 ^ b);
572   num^=(a | b);
573   num*=num>>2;
574   return (num);
575  }
576   
577