Debian Codegraph @

src/config.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "config.h"
20
21 #include "episodes.h"
22 #include "file.h"
23 #include "joystick.h"
24 #include "loudness.h"
25 #include "mtrand.h"
26 #include "nortsong.h"
27 #include "opentyr.h"
28 #include "player.h"
29 #include "varz.h"
30 #include "vga256d.h"
31 #include "video.h"
32 #include "video_scale.h"
33
34 #include <stdio.h>
35 #include <sys/stat.h>
36
37 #ifdef _MSC_VER
38 #include <direct.h>
39 #define mkdir _mkdir
40 #else
41 #include <unistd.h>
42 #endif
43
44 /* Configuration Load/Save handler */
45
46 const JE_byte cryptKey[10] = /* [1..10] */
47 {
48 15, 50, 89, 240, 147, 34, 86, 9, 32, 208
49 };
50
51 const DosKeySettings defaultDosKeySettings =
52 {
53 72, 80, 75, 77, 57, 28, 29, 56
54 };
55
56 const KeySettings defaultKeySettings =
57 {
58 SDL_SCANCODE_UP,
59 SDL_SCANCODE_DOWN,
60 SDL_SCANCODE_LEFT,
61 SDL_SCANCODE_RIGHT,
62 SDL_SCANCODE_SPACE,
63 SDL_SCANCODE_RETURN,
64 SDL_SCANCODE_LCTRL,
65 SDL_SCANCODE_LALT,
66 };
67
68 static const char *const keySettingNames[] =
69 {
70 "up",
71 "down",
72 "left",
73 "right",
74 "fire",
75 "change fire",
76 "left sidekick",
77 "right sidekick",
78 };
79
80 const char defaultHighScoreNames[34][23] = /* [1..34] of string [22] */
81 {/*1P*/
82 /*TYR*/ "The Prime Chair", /*13*/
83 "Transon Lohk",
84 "Javi Onukala",
85 "Mantori",
86 "Nortaneous",
87 "Dougan",
88 "Reid",
89 "General Zinglon",
90 "Late Gyges Phildren",
91 "Vykromod",
92 "Beppo",
93 "Borogar",
94 "ShipMaster Carlos",
95
96 /*OTHER*/ "Jill", /*5*/
97 "Darcy",
98 "Jake Stone",
99 "Malvineous Havershim",
100 "Marta Louise Velasquez",
101
102 /*JAZZ*/ "Jazz Jackrabbit", /*3*/
103 "Eva Earlong",
104 "Devan Shell",
105
106 /*OMF*/ "Crystal Devroe", /*11*/
107 "Steffan Tommas",
108 "Milano Angston",
109 "Christian",
110 "Shirro",
111 "Jean-Paul",
112 "Ibrahim Hothe",
113 "Angel",
114 "Cossette Akira",
115 "Raven",
116 "Hans Kreissack",
117
118 /*DARE*/ "Tyler", /*2*/
119 "Rennis the Rat Guard"
120 };
121
122 const char defaultTeamNames[22][25] = /* [1..22] of string [24] */
123 {
124 "Jackrabbits",
125 "Team Tyrian",
126 "The Elam Brothers",
127 "Dare to Dream Team",
128 "Pinball Freaks",
129 "Extreme Pinball Freaks",
130 "Team Vykromod",
131 "Epic All-Stars",
132 "Hans Keissack's WARriors",
133 "Team Overkill",
134 "Pied Pipers",
135 "Gencore Growlers",
136 "Microsol Masters",
137 "Beta Warriors",
138 "Team Loco",
139 "The Shellians",
140 "Jungle Jills",
141 "Murderous Malvineous",
142 "The Traffic Department",
143 "Clan Mikal",
144 "Clan Patrok",
145 "Carlos' Crawlers"
146 };
147
148 const JE_EditorItemAvailType initialItemAvail =
149 {
150 1,1,1,0,0,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0, /* Front/Rear Weapons 1-38 */
151 0,0,0,0,0,0,0,0,0,0,1, /* Fill */
152 1,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,0,0, /* Sidekicks 51-68 */
153 0,0,0,0,0,0,0,0,0,0,0, /* Fill */
154 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* Special Weapons 81-93 */
155 0,0,0,0,0 /* Fill */
156 };
157
158 /* Last 2 bytes = Word
159 *
160 * Max Value = 1680
161 * X div 60 = Armor (1-28)
162 * X div 168 = Shield (1-12)
163 * X div 280 = Engine (1-06)
164 */
165
166 JE_boolean smoothies[9] = /* [1..9] */
167 { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
168
169 JE_byte starShowVGASpecialCode;
170
171 /* CubeData */
172 JE_word lastCubeMax, cubeMax;
173 JE_word cubeList[4]; /* [1..4] */
174
175 /* High-Score Stuff */
176 JE_boolean gameHasRepeated; // can only get highscore on first play-through
177
178 /* Difficulty */
179 JE_shortint difficultyLevel, oldDifficultyLevel,
180 initialDifficulty; // can only get highscore on initial episode
181
182 /* Player Stuff */
183 uint power, lastPower, powerAdd;
184 JE_byte shieldWait, shieldT;
185
186 JE_byte shotRepeat[11], shotMultiPos[11];
187 JE_boolean portConfigChange, portConfigDone;
188
189 /* Level Data */
190 char lastLevelName[11], levelName[11]; /* string [10] */
191 JE_byte mainLevel, nextLevel, saveLevel; /*Current Level #*/
192
193 /* Keyboard Junk */
194 DosKeySettings dosKeySettings;
195 KeySettings keySettings;
196
197 /* Configuration */
198 JE_shortint levelFilter, levelFilterNew, levelBrightness, levelBrightnessChg;
199 JE_boolean filtrationAvail, filterActive, filterFade, filterFadeStart;
200
201 JE_boolean gameJustLoaded;
202
203 JE_boolean galagaMode;
204
205 JE_boolean extraGame;
206
207 JE_boolean twoPlayerMode, twoPlayerLinked, onePlayerAction, superTyrian;
208 JE_boolean trentWin = false;
209 JE_byte superArcadeMode;
210
211 JE_byte superArcadePowerUp;
212
213 JE_real linkGunDirec;
214 JE_byte inputDevice[2] = { 1, 2 }; // 0:any 1:keyboard 2:mouse 3+:joystick
215
216 JE_byte secretHint;
217 JE_byte background3over;
218 JE_byte background2over;
219 JE_byte gammaCorrection;
220 JE_boolean superPause = false;
221 JE_boolean explosionTransparent,
222 youAreCheating,
223 displayScore,
224 background2, smoothScroll, wild, superWild, starActive,
225 topEnemyOver,
226 skyEnemyOverAll,
227 background2notTransparent;
228
229 JE_byte soundEffects; // dummy value for config
230 JE_byte versionNum; /* SW 1.0 and SW/Reg 1.1 = 0 or 1
231 * EA 1.2 = 2 */
232
233 JE_byte fastPlay;
234 JE_boolean pentiumMode;
235
236 /* Savegame files */
237 JE_byte gameSpeed;
238 JE_byte processorType; /* 1=386 2=486 3=Pentium Hyper */
239
240 JE_SaveFilesType saveFiles; /*array[1..saveLevelnum] of savefiletype;*/
241 JE_SaveGameTemp saveTemp;
242
243 JE_word editorLevel; /*Initial value 800*/
244
245 Config opentyrian_config; // implicitly initialized
246
247 bool load_opentyrian_config(void)
248 {
249 // defaults
250 fullscreen_display = -1;
251 set_scaler_by_name("Scale2x");
252 memcpy(keySettings, defaultKeySettings, sizeof(keySettings));
253
254 Config *config = &opentyrian_config;
255
256 FILE *file = dir_fopen_warn(get_user_directory(), "opentyrian.cfg", "r");
257 if (file == NULL)
258 return false;
259
260 if (!config_parse(config, file))
261 {
262 fclose(file);
263
264 return false;
265 }
266
267 ConfigSection *section;
268
269 section = config_find_section(config, "video", NULL);
270 if (section != NULL)
271 {
272 config_get_int_option(section, "fullscreen", &fullscreen_display);
273
274 const char *scaler;
275 if (config_get_string_option(section, "scaler", &scaler))
276 set_scaler_by_name(scaler);
277
278 const char *scaling_mode;
279 if (config_get_string_option(section, "scaling_mode", &scaling_mode))
280 set_scaling_mode_by_name(scaling_mode);
281 }
282
283 section = config_find_section(config, "keyboard", NULL);
284 if (section != NULL)
285 {
286 for (size_t i = 0; i < COUNTOF(keySettings); ++i)
287 {
288 const char *keyName;
289 if (config_get_string_option(section, keySettingNames[i], &keyName))
290 {
291 SDL_Scancode scancode = SDL_GetScancodeFromName(keyName);
292 if (scancode != SDL_SCANCODE_UNKNOWN)
293 keySettings[i] = scancode;
294 }
295 }
296 }
297
298 fclose(file);
299
300 return true;
301 }
302
303 bool save_opentyrian_config(void)
304 {
305 Config *config = &opentyrian_config;
306
307 ConfigSection *section;
308
309 section = config_find_or_add_section(config, "video", NULL);
310 if (section == NULL)
311 exit(EXIT_FAILURE); // out of memory
312
313 config_set_int_option(section, "fullscreen", fullscreen_display);
314
315 config_set_string_option(section, "scaler", scalers[scaler].name);
316
317 config_set_string_option(section, "scaling_mode", scaling_mode_names[scaling_mode]);
318
319 section = config_find_or_add_section(config, "keyboard", NULL);
320 if (section == NULL)
321 exit(EXIT_FAILURE); // out of memory
322
323 for (size_t i = 0; i < COUNTOF(keySettings); ++i)
324 {
325 const char *keyName = SDL_GetScancodeName(keySettings[i]);
326 if (keyName[0] == '\0')
327 keyName = NULL;
328 config_set_string_option(section, keySettingNames[i], keyName);
329 }
330
331 #ifndef TARGET_WIN32
332 mkdir(get_user_directory(), 0700);
333 #else
334 mkdir(get_user_directory());
335 #endif
336
337 FILE *file = dir_fopen(get_user_directory(), "opentyrian.cfg", "w");
338 if (file == NULL)
339 return false;
340
341 config_write(config, file);
342
343 #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
344 fsync(fileno(file));
345 #endif
346 fclose(file);
347
348 return true;
349 }
350
351 static void playeritems_to_pitems(JE_PItemsType pItems, PlayerItems *items, JE_byte initial_episode_num)
352 {
353 pItems[0] = items->weapon[FRONT_WEAPON].id;
354 pItems[1] = items->weapon[REAR_WEAPON].id;
355 pItems[2] = items->super_arcade_mode;
356 pItems[3] = items->sidekick[LEFT_SIDEKICK];
357 pItems[4] = items->sidekick[RIGHT_SIDEKICK];
358 pItems[5] = items->generator;
359 pItems[6] = items->sidekick_level;
360 pItems[7] = items->sidekick_series;
361 pItems[8] = initial_episode_num;
362 pItems[9] = items->shield;
363 pItems[10] = items->special;
364 pItems[11] = items->ship;
365 }
366
367 static void pitems_to_playeritems(PlayerItems *items, JE_PItemsType pItems, JE_byte *initial_episode_num)
368 {
369 items->weapon[FRONT_WEAPON].id = pItems[0];
370 items->weapon[REAR_WEAPON].id = pItems[1];
371 items->super_arcade_mode = pItems[2];
372 items->sidekick[LEFT_SIDEKICK] = pItems[3];
373 items->sidekick[RIGHT_SIDEKICK] = pItems[4];
374 items->generator = pItems[5];
375 items->sidekick_level = pItems[6];
376 items->sidekick_series = pItems[7];
377 if (initial_episode_num != NULL)
378 *initial_episode_num = pItems[8];
379 items->shield = pItems[9];
380 items->special = pItems[10];
381 items->ship = pItems[11];
382 }
383
384 void JE_saveGame(JE_byte slot, const char *name)
385 {
386 saveFiles[slot-1].initialDifficulty = initialDifficulty;
387 saveFiles[slot-1].gameHasRepeated = gameHasRepeated;
388 saveFiles[slot-1].level = saveLevel;
389
390 if (superTyrian)
391 player[0].items.super_arcade_mode = SA_SUPERTYRIAN;
392 else if (superArcadeMode == SA_NONE && onePlayerAction)
393 player[0].items.super_arcade_mode = SA_ARCADE;
394 else
395 player[0].items.super_arcade_mode = superArcadeMode;
396
397 playeritems_to_pitems(saveFiles[slot-1].items, &player[0].items, initial_episode_num);
398
399 if (twoPlayerMode)
400 playeritems_to_pitems(saveFiles[slot-1].lastItems, &player[1].items, 0);
401 else
402 playeritems_to_pitems(saveFiles[slot-1].lastItems, &player[0].last_items, 0);
403
404 saveFiles[slot-1].score = player[0].cash;
405 saveFiles[slot-1].score2 = player[1].cash;
406
407 memcpy(&saveFiles[slot-1].levelName, &lastLevelName, sizeof(lastLevelName));
408 saveFiles[slot-1].cubes = lastCubeMax;
409
410 if (strcmp(lastLevelName, "Completed") == 0)
411 {
412 temp = episodeNum - 1;
413 if (temp < 1)
414 {
415 temp = EPISODE_AVAILABLE; /* JE: {Episodemax is 4 for completion purposes} */
416 }
417 saveFiles[slot-1].episode = temp;
418 }
419 else
420 {
421 saveFiles[slot-1].episode = episodeNum;
422 }
423
424 saveFiles[slot-1].difficulty = difficultyLevel;
425 saveFiles[slot-1].secretHint = secretHint;
426 saveFiles[slot-1].input1 = inputDevice[0];
427 saveFiles[slot-1].input2 = inputDevice[1];
428
429 strcpy(saveFiles[slot-1].name, name);
430
431 for (uint port = 0; port < 2; ++port)
432 {
433 // if two-player, use first player's front and second player's rear weapon
434 saveFiles[slot-1].power[port] = player[twoPlayerMode ? port : 0].items.weapon[port].power;
435 }
436
437 JE_saveConfiguration();
438 }
439
440 void JE_loadGame(JE_byte slot)
441 {
442 superTyrian = false;
443 onePlayerAction = false;
444 twoPlayerMode = false;
445 extraGame = false;
446 galagaMode = false;
447
448 initialDifficulty = saveFiles[slot-1].initialDifficulty;
449 gameHasRepeated = saveFiles[slot-1].gameHasRepeated;
450 twoPlayerMode = (slot-1) > 10;
451 difficultyLevel = saveFiles[slot-1].difficulty;
452
453 pitems_to_playeritems(&player[0].items, saveFiles[slot-1].items, &initial_episode_num);
454
455 superArcadeMode = player[0].items.super_arcade_mode;
456
457 if (superArcadeMode == SA_SUPERTYRIAN)
458 superTyrian = true;
459 if (superArcadeMode != SA_NONE)
460 onePlayerAction = true;
461 if (superArcadeMode > SA_NORTSHIPZ)
462 superArcadeMode = SA_NONE;
463
464 if (twoPlayerMode)
465 {
466 onePlayerAction = false;
467
468 pitems_to_playeritems(&player[1].items, saveFiles[slot-1].lastItems, NULL);
469 }
470 else
471 {
472 pitems_to_playeritems(&player[0].last_items, saveFiles[slot-1].lastItems, NULL);
473 }
474
475 /* Compatibility with old version */
476 if (player[1].items.sidekick_level < 101)
477 {
478 player[1].items.sidekick_level = 101;
479 player[1].items.sidekick_series = player[1].items.sidekick[LEFT_SIDEKICK];
480 }
481
482 player[0].cash = saveFiles[slot-1].score;
483 player[1].cash = saveFiles[slot-1].score2;
484
485 mainLevel = saveFiles[slot-1].level;
486 cubeMax = saveFiles[slot-1].cubes;
487 lastCubeMax = cubeMax;
488
489 secretHint = saveFiles[slot-1].secretHint;
490 inputDevice[0] = saveFiles[slot-1].input1;
491 inputDevice[1] = saveFiles[slot-1].input2;
492
493 for (uint port = 0; port < 2; ++port)
494 {
495 // if two-player, use first player's front and second player's rear weapon
496 player[twoPlayerMode ? port : 0].items.weapon[port].power = saveFiles[slot-1].power[port];
497 }
498
499 int episode = saveFiles[slot-1].episode;
500
501 memcpy(&levelName, &saveFiles[slot-1].levelName, sizeof(levelName));
502
503 if (strcmp(levelName, "Completed") == 0)
504 {
505 if (episode == EPISODE_AVAILABLE)
506 episode = 1;
507 else if (episode < EPISODE_AVAILABLE)
508 episode++;
509 /* Increment episode. Episode EPISODE_AVAILABLE goes to 1. */
510 }
511
512 JE_initEpisode(episode);
513 saveLevel = mainLevel;
514 memcpy(&lastLevelName, &levelName, sizeof(levelName));
515 }
516
517 void JE_initProcessorType(void)
518 {
519 /* SYN: Originally this proc looked at your hardware specs and chose appropriate options. We don't care, so I'll just set
520 decent defaults here. */
521
522 wild = false;
523 superWild = false;
524 smoothScroll = true;
525 explosionTransparent = true;
526 filtrationAvail = false;
527 background2 = true;
528 displayScore = true;
529
530 switch (processorType)
531 {
532 case 1: /* 386 */
533 background2 = false;
534 displayScore = false;
535 explosionTransparent = false;
536 break;
537 case 2: /* 486 - Default */
538 break;
539 case 3: /* High Detail */
540 smoothScroll = false;
541 break;
542 case 4: /* Pentium */
543 wild = true;
544 filtrationAvail = true;
545 break;
546 case 5: /* Nonstandard VGA */
547 smoothScroll = false;
548 break;
549 case 6: /* SuperWild */
550 wild = true;
551 superWild = true;
552 filtrationAvail = true;
553 break;
554 }
555
556 switch (gameSpeed)
557 {
558 case 1: /* Slug Mode */
559 fastPlay = 3;
560 break;
561 case 2: /* Slower */
562 fastPlay = 4;
563 break;
564 case 3: /* Slow */
565 fastPlay = 5;
566 break;
567 case 4: /* Normal */
568 fastPlay = 0;
569 break;
570 case 5: /* Pentium Hyper */
571 fastPlay = 1;
572 break;
573 }
574
575 }
576
577 void JE_setNewGameSpeed(void)
578 {
579 pentiumMode = false;
580
581 Uint16 speed;
582 switch (fastPlay)
583 {
584 default:
585 assert(false);
586 // fall through
587 case 0: // Normal
588 speed = 0x4300;
589 smoothScroll = true;
590 frameCountMax = 2;
591 break;
592 case 1: // Pentium Hyper
593 speed = 0x3000;
594 smoothScroll = true;
595 frameCountMax = 2;
596 break;
597 case 2:
598 speed = 0x2000;
599 smoothScroll = false;
600 frameCountMax = 2;
601 break;
602 case 3: // Slug mode
603 speed = 0x5300;
604 smoothScroll = true;
605 frameCountMax = 4;
606 break;
607 case 4: // Slower
608 speed = 0x4300;
609 smoothScroll = true;
610 frameCountMax = 3;
611 break;
612 case 5: // Slow
613 speed = 0x4300;
614 smoothScroll = true;
615 frameCountMax = 2;
616 pentiumMode = true;
617 break;
618 }
619
620 setDelaySpeed(speed);
621 setDelay(frameCountMax);
622 }
623
624 void JE_encryptSaveTemp(void)
625 {
626 JE_SaveGameTemp s3;
627 JE_word x;
628 JE_byte y;
629
630 memcpy(&s3, &saveTemp, sizeof(s3));
631
632 y = 0;
633 for (x = 0; x < SAVE_FILE_SIZE; x++)
634 {
635 y += s3[x];
636 }
637 saveTemp[SAVE_FILE_SIZE] = y;
638
639 y = 0;
640 for (x = 0; x < SAVE_FILE_SIZE; x++)
641 {
642 y -= s3[x];
643 }
644 saveTemp[SAVE_FILE_SIZE+1] = y;
645
646 y = 1;
647 for (x = 0; x < SAVE_FILE_SIZE; x++)
648 {
649 y = (y * s3[x]) + 1;
650 }
651 saveTemp[SAVE_FILE_SIZE+2] = y;
652
653 y = 0;
654 for (x = 0; x < SAVE_FILE_SIZE; x++)
655 {
656 y = y ^ s3[x];
657 }
658 saveTemp[SAVE_FILE_SIZE+3] = y;
659
660 for (x = 0; x < SAVE_FILE_SIZE; x++)
661 {
662 saveTemp[x] = saveTemp[x] ^ cryptKey[(x+1) % 10];
663 if (x > 0)
664 {
665 saveTemp[x] = saveTemp[x] ^ saveTemp[x - 1];
666 }
667 }
668 }
669
670 void JE_decryptSaveTemp(void)
671 {
672 JE_boolean correct = true;
673 JE_SaveGameTemp s2;
674 int x;
675 JE_byte y;
676
677 /* Decrypt save game file */
678 for (x = (SAVE_FILE_SIZE - 1); x >= 0; x--)
679 {
680 s2[x] = (JE_byte)saveTemp[x] ^ (JE_byte)(cryptKey[(x+1) % 10]);
681 if (x > 0)
682 {
683 s2[x] ^= (JE_byte)saveTemp[x - 1];
684 }
685
686 }
687
688 /* for (x = 0; x < SAVE_FILE_SIZE; x++) printf("%c", s2[x]); */
689
690 /* Check save file for correctitude */
691 y = 0;
692 for (x = 0; x < SAVE_FILE_SIZE; x++)
693 {
694 y += s2[x];
695 }
696 if (saveTemp[SAVE_FILE_SIZE] != y)
697 {
698 correct = false;
699 printf("Failed additive checksum: %d vs %d\n", saveTemp[SAVE_FILE_SIZE], y);
700 }
701
702 y = 0;
703 for (x = 0; x < SAVE_FILE_SIZE; x++)
704 {
705 y -= s2[x];
706 }
707 if (saveTemp[SAVE_FILE_SIZE+1] != y)
708 {
709 correct = false;
710 printf("Failed subtractive checksum: %d vs %d\n", saveTemp[SAVE_FILE_SIZE+1], y);
711 }
712
713 y = 1;
714 for (x = 0; x < SAVE_FILE_SIZE; x++)
715 {
716 y = (y * s2[x]) + 1;
717 }
718 if (saveTemp[SAVE_FILE_SIZE+2] != y)
719 {
720 correct = false;
721 printf("Failed multiplicative checksum: %d vs %d\n", saveTemp[SAVE_FILE_SIZE+2], y);
722 }
723
724 y = 0;
725 for (x = 0; x < SAVE_FILE_SIZE; x++)
726 {
727 y = y ^ s2[x];
728 }
729 if (saveTemp[SAVE_FILE_SIZE+3] != y)
730 {
731 correct = false;
732 printf("Failed XOR'd checksum: %d vs %d\n", saveTemp[SAVE_FILE_SIZE+3], y);
733 }
734
735 /* Barf and die if save file doesn't validate */
736 if (!correct)
737 {
738 fprintf(stderr, "Error reading save file!\n");
739 exit(255);
740 }
741
742 /* Keep decrypted version plz */
743 memcpy(&saveTemp, &s2, sizeof(s2));
744 }
745
746 const char *get_user_directory(void)
747 {
748 static char user_dir[500] = "";
749
750 if (strlen(user_dir) == 0)
751 {
752 #ifndef TARGET_WIN32
753 char *xdg_config_home = getenv("XDG_CONFIG_HOME");
754 if (xdg_config_home != NULL)
755 {
756 snprintf(user_dir, sizeof(user_dir), "%s/opentyrian", xdg_config_home);
757 }
758 else
759 {
760 char *home = getenv("HOME");
761 if (home != NULL)
762 {
763 snprintf(user_dir, sizeof(user_dir), "%s/.config/opentyrian", home);
764 }
765 else
766 {
767 strcpy(user_dir, ".");
768 }
769 }
770 #else
771 strcpy(user_dir, ".");
772 #endif
773 }
774
775 return user_dir;
776 }
777
778 // for compatibility
779 Uint8 joyButtonAssign[4] = {1, 4, 5, 5};
780 Uint8 inputDevice_ = 0, jConfigure = 0, midiPort = 1;
781
782 void JE_loadConfiguration(void)
783 {
784 FILE *fi;
785 int z;
786 JE_byte *p;
787 int y;
788
789 fi = dir_fopen_warn(get_user_directory(), "tyrian.cfg", "rb");
790 if (fi && ftell_eof(fi) == 28)
791 {
792 background2 = 0;
793 fread_bool_die(&background2, fi);
794 fread_u8_die(&gameSpeed, 1, fi);
795
796 fread_u8_die(&inputDevice_, 1, fi);
797 fread_u8_die(&jConfigure, 1, fi);
798
799 fread_u8_die(&versionNum, 1, fi);
800
801 fread_u8_die(&processorType, 1, fi);
802 fread_u8_die(&midiPort, 1, fi);
803 fread_u8_die(&soundEffects, 1, fi);
804 fread_u8_die(&gammaCorrection, 1, fi);
805 fread_s8_die(&difficultyLevel, 1, fi);
806
807 fread_u8_die(joyButtonAssign, 4, fi);
808
809 fread_u16_die(&tyrMusicVolume, 1, fi);
810 fread_u16_die(&fxVolume, 1, fi);
811
812 fread_u8_die(inputDevice, 2, fi);
813
814 fread_u8_die(dosKeySettings, 8, fi);
815
816 fclose(fi);
817 }
818 else
819 {
820 printf("\nInvalid or missing TYRIAN.CFG! Continuing using defaults.\n\n");
821
822 soundEffects = 1;
823 memcpy(&dosKeySettings, &defaultDosKeySettings, sizeof(dosKeySettings));
824 background2 = true;
825 tyrMusicVolume = 191;
826 fxVolume = 191;
827 gammaCorrection = 0;
828 processorType = 3;
829 gameSpeed = 4;
830 }
831
832 load_opentyrian_config();
833
834 if (tyrMusicVolume > 255)
835 tyrMusicVolume = 255;
836 if (fxVolume > 255)
837 fxVolume = 255;
838
839 set_volume(tyrMusicVolume, fxVolume);
840
841 fi = dir_fopen_warn(get_user_directory(), "tyrian.sav", "rb");
842 if (fi)
843 {
844
845 fseek(fi, 0, SEEK_SET);
846 fread_die(saveTemp, 1, sizeof(saveTemp), fi);
847 JE_decryptSaveTemp();
848
849 /* SYN: The original mostly blasted the save file into raw memory. However, our lives are not so
850 easy, because the C struct is necessarily a different size. So instead we have to loop
851 through each record and load fields manually. *emo tear* :'( */
852
853 p = saveTemp;
854 for (z = 0; z < SAVE_FILES_NUM; z++)
855 {
856 memcpy(&saveFiles[z].encode, p, sizeof(JE_word)); p += 2;
857 saveFiles[z].encode = SDL_SwapLE16(saveFiles[z].encode);
858
859 memcpy(&saveFiles[z].level, p, sizeof(JE_word)); p += 2;
860 saveFiles[z].level = SDL_SwapLE16(saveFiles[z].level);
861
862 memcpy(&saveFiles[z].items, p, sizeof(JE_PItemsType)); p += sizeof(JE_PItemsType);
863
864 memcpy(&saveFiles[z].score, p, sizeof(JE_longint)); p += 4;
865 saveFiles[z].score = SDL_SwapLE32(saveFiles[z].score);
866
867 memcpy(&saveFiles[z].score2, p, sizeof(JE_longint)); p += 4;
868 saveFiles[z].score2 = SDL_SwapLE32(saveFiles[z].score2);
869
870 /* SYN: Pascal strings are prefixed by a byte holding the length! */
871 memset(&saveFiles[z].levelName, 0, sizeof(saveFiles[z].levelName));
872 memcpy(&saveFiles[z].levelName, &p[1], *p);
873 p += 10;
874
875 /* This was a BYTE array, not a STRING, in the original. Go fig. */
876 memcpy(&saveFiles[z].name, p, 14);
877 p += 14;
878
879 memcpy(&saveFiles[z].cubes, p, sizeof(JE_byte)); p++;
880 memcpy(&saveFiles[z].power, p, sizeof(JE_byte) * 2); p += 2;
881 memcpy(&saveFiles[z].episode, p, sizeof(JE_byte)); p++;
882 memcpy(&saveFiles[z].lastItems, p, sizeof(JE_PItemsType)); p += sizeof(JE_PItemsType);
883 memcpy(&saveFiles[z].difficulty, p, sizeof(JE_byte)); p++;
884 memcpy(&saveFiles[z].secretHint, p, sizeof(JE_byte)); p++;
885 memcpy(&saveFiles[z].input1, p, sizeof(JE_byte)); p++;
886 memcpy(&saveFiles[z].input2, p, sizeof(JE_byte)); p++;
887
888 /* booleans were 1 byte in pascal -- working around it */
889 Uint8 temp;
890 memcpy(&temp, p, 1); p++;
891 saveFiles[z].gameHasRepeated = temp != 0;
892
893 memcpy(&saveFiles[z].initialDifficulty, p, sizeof(JE_byte)); p++;
894
895 memcpy(&saveFiles[z].highScore1, p, sizeof(JE_longint)); p += 4;
896 saveFiles[z].highScore1 = SDL_SwapLE32(saveFiles[z].highScore1);
897
898 memcpy(&saveFiles[z].highScore2, p, sizeof(JE_longint)); p += 4;
899 saveFiles[z].highScore2 = SDL_SwapLE32(saveFiles[z].highScore2);
900
901 memset(&saveFiles[z].highScoreName, 0, sizeof(saveFiles[z].highScoreName));
902 memcpy(&saveFiles[z].highScoreName, &p[1], *p);
903 p += 30;
904
905 memcpy(&saveFiles[z].highScoreDiff, p, sizeof(JE_byte)); p++;
906 }
907
908 /* SYN: This is truncating to bytes. I have no idea what this is doing or why. */
909 /* TODO: Figure out what this is about and make sure it isn't broken. */
910 editorLevel = (saveTemp[SIZEOF_SAVEGAMETEMP - 5] << 8) | saveTemp[SIZEOF_SAVEGAMETEMP - 6];
911
912 fclose(fi);
913 }
914 else
915 {
916 /* We didn't have a save file! Let's make up random stuff! */
917 editorLevel = 800;
918
919 for (z = 0; z < 100; z++)
920 {
921 saveTemp[SAVE_FILES_SIZE + z] = initialItemAvail[z];
922 }
923
924 for (z = 0; z < SAVE_FILES_NUM; z++)
925 {
926 saveFiles[z].level = 0;
927
928 for (y = 0; y < 14; y++)
929 {
930 saveFiles[z].name[y] = ' ';
931 }
932 saveFiles[z].name[14] = 0;
933
934 saveFiles[z].highScore1 = ((mt_rand() % 20) + 1) * 1000;
935
936 if (z % 6 > 2)
937 {
938 saveFiles[z].highScore2 = ((mt_rand() % 20) + 1) * 1000;
939 strcpy(saveFiles[z].highScoreName, defaultTeamNames[mt_rand() % 22]);
940 }
941 else
942 {
943 strcpy(saveFiles[z].highScoreName, defaultHighScoreNames[mt_rand() % 34]);
944 }
945 }
946 }
947
948 JE_initProcessorType();
949 }
950
951 void JE_saveConfiguration(void)
952 {
953 FILE *f;
954 JE_byte *p;
955 int z;
956
957 p = saveTemp;
958 for (z = 0; z < SAVE_FILES_NUM; z++)
959 {
960 JE_SaveFileType tempSaveFile;
961 memcpy(&tempSaveFile, &saveFiles[z], sizeof(tempSaveFile));
962
963 tempSaveFile.encode = SDL_SwapLE16(tempSaveFile.encode);
964 memcpy(p, &tempSaveFile.encode, sizeof(JE_word)); p += 2;
965
966 tempSaveFile.level = SDL_SwapLE16(tempSaveFile.level);
967 memcpy(p, &tempSaveFile.level, sizeof(JE_word)); p += 2;
968
969 memcpy(p, &tempSaveFile.items, sizeof(JE_PItemsType)); p += sizeof(JE_PItemsType);
970
971 tempSaveFile.score = SDL_SwapLE32(tempSaveFile.score);
972 memcpy(p, &tempSaveFile.score, sizeof(JE_longint)); p += 4;
973
974 tempSaveFile.score2 = SDL_SwapLE32(tempSaveFile.score2);
975 memcpy(p, &tempSaveFile.score2, sizeof(JE_longint)); p += 4;
976
977 /* SYN: Pascal strings are prefixed by a byte holding the length! */
978 memset(p, 0, sizeof(tempSaveFile.levelName));
979 *p = strlen(tempSaveFile.levelName);
980 memcpy(&p[1], &tempSaveFile.levelName, *p);
981 p += 10;
982
983 /* This was a BYTE array, not a STRING, in the original. Go fig. */
984 memcpy(p, &tempSaveFile.name, 14);
985 p += 14;
986
987 memcpy(p, &tempSaveFile.cubes, sizeof(JE_byte)); p++;
988 memcpy(p, &tempSaveFile.power, sizeof(JE_byte) * 2); p += 2;
989 memcpy(p, &tempSaveFile.episode, sizeof(JE_byte)); p++;
990 memcpy(p, &tempSaveFile.lastItems, sizeof(JE_PItemsType)); p += sizeof(JE_PItemsType);
991 memcpy(p, &tempSaveFile.difficulty, sizeof(JE_byte)); p++;
992 memcpy(p, &tempSaveFile.secretHint, sizeof(JE_byte)); p++;
993 memcpy(p, &tempSaveFile.input1, sizeof(JE_byte)); p++;
994 memcpy(p, &tempSaveFile.input2, sizeof(JE_byte)); p++;
995
996 /* booleans were 1 byte in pascal -- working around it */
997 Uint8 temp = tempSaveFile.gameHasRepeated != false;
998 memcpy(p, &temp, 1); p++;
999
1000 memcpy(p, &tempSaveFile.initialDifficulty, sizeof(JE_byte)); p++;
1001
1002 tempSaveFile.highScore1 = SDL_SwapLE32(tempSaveFile.highScore1);
1003 memcpy(p, &tempSaveFile.highScore1, sizeof(JE_longint)); p += 4;
1004
1005 tempSaveFile.highScore2 = SDL_SwapLE32(tempSaveFile.highScore2);
1006 memcpy(p, &tempSaveFile.highScore2, sizeof(JE_longint)); p += 4;
1007
1008 memset(p, 0, sizeof(tempSaveFile.highScoreName));
1009 *p = strlen(tempSaveFile.highScoreName);
1010 memcpy(&p[1], &tempSaveFile.highScoreName, *p);
1011 p += 30;
1012
1013 memcpy(p, &tempSaveFile.highScoreDiff, sizeof(JE_byte)); p++;
1014 }
1015
1016 saveTemp[SIZEOF_SAVEGAMETEMP - 6] = editorLevel >> 8;
1017 saveTemp[SIZEOF_SAVEGAMETEMP - 5] = editorLevel;
1018
1019 JE_encryptSaveTemp();
1020
1021 #ifndef TARGET_WIN32
1022 mkdir(get_user_directory(), 0700);
1023 #else
1024 mkdir(get_user_directory());
1025 #endif
1026
1027 f = dir_fopen_warn(get_user_directory(), "tyrian.sav", "wb");
1028 if (f != NULL)
1029 {
1030 fwrite_die(saveTemp, 1, sizeof(saveTemp), f);
1031
1032 #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
1033 fsync(fileno(f));
1034 #endif
1035 fclose(f);
1036 }
1037
1038 JE_decryptSaveTemp();
1039
1040 f = dir_fopen_warn(get_user_directory(), "tyrian.cfg", "wb");
1041 if (f != NULL)
1042 {
1043 fwrite_bool_die(&background2, f);
1044 fwrite_u8_die(&gameSpeed, 1, f);
1045
1046 fwrite_u8_die(&inputDevice_, 1, f);
1047 fwrite_u8_die(&jConfigure, 1, f);
1048
1049 fwrite_u8_die(&versionNum, 1, f);
1050 fwrite_u8_die(&processorType, 1, f);
1051 fwrite_u8_die(&midiPort, 1, f);
1052 fwrite_u8_die(&soundEffects, 1, f);
1053 fwrite_u8_die(&gammaCorrection, 1, f);
1054 fwrite_s8_die(&difficultyLevel, 1, f);
1055 fwrite_u8_die(joyButtonAssign, 4, f);
1056
1057 fwrite_u16_die(&tyrMusicVolume, f);
1058 fwrite_u16_die(&fxVolume, f);
1059
1060 fwrite_u8_die(inputDevice, 2, f);
1061
1062 fwrite_u8_die(dosKeySettings, 8, f);
1063
1064 #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
1065 fsync(fileno(f));
1066 #endif
1067 fclose(f);
1068 }
1069
1070 save_opentyrian_config();
1071 }