1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
17 #include "libgame/libgame.h"
24 #define CHUNK_ID_LEN 4 /* IFF style chunk id length */
25 #define CHUNK_SIZE_UNDEFINED 0 /* undefined chunk size == 0 */
26 #define CHUNK_SIZE_NONE -1 /* do not write chunk size */
27 #define FILE_VERS_CHUNK_SIZE 8 /* size of file version chunk */
28 #define LEVEL_HEADER_SIZE 80 /* size of level file header */
29 #define LEVEL_HEADER_UNUSED 14 /* unused level header bytes */
30 #define LEVEL_CHUNK_CNT2_SIZE 160 /* size of level CNT2 chunk */
31 #define LEVEL_CHUNK_CNT2_UNUSED 11 /* unused CNT2 chunk bytes */
32 #define TAPE_HEADER_SIZE 20 /* size of tape file header */
33 #define TAPE_HEADER_UNUSED 3 /* unused tape header bytes */
35 /* file identifier strings */
36 #define LEVEL_COOKIE_TMPL "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_x.x"
37 #define TAPE_COOKIE_TMPL "ROCKSNDIAMONDS_TAPE_FILE_VERSION_x.x"
38 #define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2"
41 /* ========================================================================= */
42 /* level file functions */
43 /* ========================================================================= */
45 static void setLevelInfoToDefaults()
49 level.file_version = FILE_VERSION_ACTUAL;
50 level.game_version = GAME_VERSION_ACTUAL;
52 level.encoding_16bit_field = FALSE; /* default: only 8-bit elements */
53 level.encoding_16bit_yamyam = FALSE; /* default: only 8-bit elements */
54 level.encoding_16bit_amoeba = FALSE; /* default: only 8-bit elements */
56 lev_fieldx = level.fieldx = STD_LEV_FIELDX;
57 lev_fieldy = level.fieldy = STD_LEV_FIELDY;
59 for(x=0; x<MAX_LEV_FIELDX; x++)
60 for(y=0; y<MAX_LEV_FIELDY; y++)
61 Feld[x][y] = Ur[x][y] = EL_SAND;
64 level.gems_needed = 0;
65 level.amoeba_speed = 10;
66 level.time_magic_wall = 10;
67 level.time_wheel = 10;
68 level.time_light = 10;
69 level.time_timegate = 10;
70 level.amoeba_content = EL_DIAMOND;
71 level.double_speed = FALSE;
72 level.gravity = FALSE;
73 level.em_slippery_gems = FALSE;
75 for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
77 for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
78 level.author[i] = '\0';
80 strcpy(level.name, NAMELESS_LEVEL_NAME);
81 strcpy(level.author, ANONYMOUS_NAME);
83 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
86 level.num_yam_contents = STD_ELEMENT_CONTENTS;
87 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
90 level.yam_content[i][x][y] =
91 (i < STD_ELEMENT_CONTENTS ? EL_ROCK : EL_EMPTY);
93 Feld[0][0] = Ur[0][0] = EL_PLAYER1;
94 Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
95 Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_EXIT_CLOSED;
97 for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
98 Properties1[EL_CUSTOM_START + i] = EP_BITMASK_DEFAULT;
100 BorderElement = EL_STEELWALL;
102 level.no_level_file = FALSE;
104 if (leveldir_current == NULL) /* only when dumping level */
107 /* try to determine better author name than 'anonymous' */
108 if (strcmp(leveldir_current->author, ANONYMOUS_NAME) != 0)
110 strncpy(level.author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
111 level.author[MAX_LEVEL_AUTHOR_LEN] = '\0';
115 switch (LEVELCLASS(leveldir_current))
117 case LEVELCLASS_TUTORIAL:
118 strcpy(level.author, PROGRAM_AUTHOR_STRING);
121 case LEVELCLASS_CONTRIBUTION:
122 strncpy(level.author, leveldir_current->name,MAX_LEVEL_AUTHOR_LEN);
123 level.author[MAX_LEVEL_AUTHOR_LEN] = '\0';
126 case LEVELCLASS_USER:
127 strncpy(level.author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
128 level.author[MAX_LEVEL_AUTHOR_LEN] = '\0';
132 /* keep default value */
138 static int checkLevelElement(int element)
140 if (element >= NUM_FILE_ELEMENTS)
142 Error(ERR_WARN, "invalid level element %d", element);
143 element = EL_CHAR_QUESTION;
145 else if (element == EL_PLAYER_OBSOLETE)
146 element = EL_PLAYER1;
147 else if (element == EL_KEY_OBSOLETE)
153 static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level)
155 level->file_version = getFileVersion(file);
156 level->game_version = getFileVersion(file);
161 static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level)
165 lev_fieldx = level->fieldx = fgetc(file);
166 lev_fieldy = level->fieldy = fgetc(file);
168 level->time = getFile16BitBE(file);
169 level->gems_needed = getFile16BitBE(file);
171 for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
172 level->name[i] = fgetc(file);
173 level->name[MAX_LEVEL_NAME_LEN] = 0;
175 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
176 level->score[i] = fgetc(file);
178 level->num_yam_contents = STD_ELEMENT_CONTENTS;
179 for(i=0; i<STD_ELEMENT_CONTENTS; i++)
182 level->yam_content[i][x][y] = checkLevelElement(fgetc(file));
184 level->amoeba_speed = fgetc(file);
185 level->time_magic_wall = fgetc(file);
186 level->time_wheel = fgetc(file);
187 level->amoeba_content = checkLevelElement(fgetc(file));
188 level->double_speed = (fgetc(file) == 1 ? TRUE : FALSE);
189 level->gravity = (fgetc(file) == 1 ? TRUE : FALSE);
190 level->encoding_16bit_field = (fgetc(file) == 1 ? TRUE : FALSE);
191 level->em_slippery_gems = (fgetc(file) == 1 ? TRUE : FALSE);
193 ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
198 static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level)
202 for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
203 level->author[i] = fgetc(file);
204 level->author[MAX_LEVEL_NAME_LEN] = 0;
209 static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level)
212 int chunk_size_expected = level->fieldx * level->fieldy;
214 /* Note: "chunk_size" was wrong before version 2.0 when elements are
215 stored with 16-bit encoding (and should be twice as big then).
216 Even worse, playfield data was stored 16-bit when only yamyam content
217 contained 16-bit elements and vice versa. */
219 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
220 chunk_size_expected *= 2;
222 if (chunk_size_expected != chunk_size)
224 ReadUnusedBytesFromFile(file, chunk_size);
225 return chunk_size_expected;
228 for(y=0; y<level->fieldy; y++)
229 for(x=0; x<level->fieldx; x++)
230 Feld[x][y] = Ur[x][y] =
231 checkLevelElement(level->encoding_16bit_field ?
232 getFile16BitBE(file) : fgetc(file));
236 static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level)
240 int content_size = MAX_ELEMENT_CONTENTS * 3 * 3;
241 int chunk_size_expected = header_size + content_size;
243 /* Note: "chunk_size" was wrong before version 2.0 when elements are
244 stored with 16-bit encoding (and should be twice as big then).
245 Even worse, playfield data was stored 16-bit when only yamyam content
246 contained 16-bit elements and vice versa. */
248 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
249 chunk_size_expected += content_size;
251 if (chunk_size_expected != chunk_size)
253 ReadUnusedBytesFromFile(file, chunk_size);
254 return chunk_size_expected;
258 level->num_yam_contents = fgetc(file);
262 /* correct invalid number of content fields -- should never happen */
263 if (level->num_yam_contents < 1 ||
264 level->num_yam_contents > MAX_ELEMENT_CONTENTS)
265 level->num_yam_contents = STD_ELEMENT_CONTENTS;
267 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
270 level->yam_content[i][x][y] =
271 checkLevelElement(level->encoding_16bit_field ?
272 getFile16BitBE(file) : fgetc(file));
276 static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level)
280 int num_contents, content_xsize, content_ysize;
281 int content_array[MAX_ELEMENT_CONTENTS][3][3];
283 element = checkLevelElement(getFile16BitBE(file));
284 num_contents = fgetc(file);
285 content_xsize = fgetc(file);
286 content_ysize = fgetc(file);
287 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT2_UNUSED);
289 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
292 content_array[i][x][y] = checkLevelElement(getFile16BitBE(file));
294 /* correct invalid number of content fields -- should never happen */
295 if (num_contents < 1 || num_contents > MAX_ELEMENT_CONTENTS)
296 num_contents = STD_ELEMENT_CONTENTS;
298 if (element == EL_YAMYAM)
300 level->num_yam_contents = num_contents;
302 for(i=0; i<num_contents; i++)
305 level->yam_content[i][x][y] = content_array[i][x][y];
307 else if (element == EL_BD_AMOEBA)
309 level->amoeba_content = content_array[0][0][0];
313 Error(ERR_WARN, "cannot load content for element '%d'", element);
319 static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level)
321 int num_changed_custom_elements = getFile16BitBE(file);
322 int chunk_size_expected = 2 + num_changed_custom_elements * 6;
325 if (chunk_size_expected != chunk_size)
327 ReadUnusedBytesFromFile(file, chunk_size - 2);
328 return chunk_size_expected;
331 for (i=0; i < num_changed_custom_elements; i++)
333 int element = getFile16BitBE(file);
334 int properties = getFile32BitBE(file);
336 if (IS_CUSTOM_ELEMENT(element))
337 Properties1[element] = properties;
339 Error(ERR_WARN, "invalid custom element number %d", element);
345 void LoadLevelFromFilename(char *filename)
347 char cookie[MAX_LINE_LEN];
348 char chunk_name[CHUNK_ID_LEN + 1];
352 /* always start with reliable default values */
353 setLevelInfoToDefaults();
355 if (!(file = fopen(filename, MODE_READ)))
357 level.no_level_file = TRUE;
359 Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
363 getFileChunkBE(file, chunk_name, NULL);
364 if (strcmp(chunk_name, "RND1") == 0)
366 getFile32BitBE(file); /* not used */
368 getFileChunkBE(file, chunk_name, NULL);
369 if (strcmp(chunk_name, "CAVE") != 0)
371 Error(ERR_WARN, "unknown format of level file '%s'", filename);
376 else /* check for pre-2.0 file format with cookie string */
378 strcpy(cookie, chunk_name);
379 fgets(&cookie[4], MAX_LINE_LEN - 4, file);
380 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
381 cookie[strlen(cookie) - 1] = '\0';
383 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
385 Error(ERR_WARN, "unknown format of level file '%s'", filename);
390 if ((level.file_version = getFileVersionFromCookieString(cookie)) == -1)
392 Error(ERR_WARN, "unsupported version of level file '%s'", filename);
397 /* pre-2.0 level files have no game version, so use file version here */
398 level.game_version = level.file_version;
401 if (level.file_version < FILE_VERSION_1_2)
403 /* level files from versions before 1.2.0 without chunk structure */
404 LoadLevel_HEAD(file, LEVEL_HEADER_SIZE, &level);
405 LoadLevel_BODY(file, level.fieldx * level.fieldy, &level);
413 int (*loader)(FILE *, int, struct LevelInfo *);
417 { "VERS", FILE_VERS_CHUNK_SIZE, LoadLevel_VERS },
418 { "HEAD", LEVEL_HEADER_SIZE, LoadLevel_HEAD },
419 { "AUTH", MAX_LEVEL_AUTHOR_LEN, LoadLevel_AUTH },
420 { "BODY", -1, LoadLevel_BODY },
421 { "CONT", -1, LoadLevel_CONT },
422 { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
423 { "CUS1", -1, LoadLevel_CUS1 },
427 while (getFileChunkBE(file, chunk_name, &chunk_size))
431 while (chunk_info[i].name != NULL &&
432 strcmp(chunk_name, chunk_info[i].name) != 0)
435 if (chunk_info[i].name == NULL)
437 Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
438 chunk_name, filename);
439 ReadUnusedBytesFromFile(file, chunk_size);
441 else if (chunk_info[i].size != -1 &&
442 chunk_info[i].size != chunk_size)
444 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
445 chunk_size, chunk_name, filename);
446 ReadUnusedBytesFromFile(file, chunk_size);
450 /* call function to load this level chunk */
451 int chunk_size_expected =
452 (chunk_info[i].loader)(file, chunk_size, &level);
454 /* the size of some chunks cannot be checked before reading other
455 chunks first (like "HEAD" and "BODY") that contain some header
456 information, so check them here */
457 if (chunk_size_expected != chunk_size)
459 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
460 chunk_size, chunk_name, filename);
468 if (leveldir_current == NULL) /* only when dumping level */
471 if (IS_LEVELCLASS_CONTRIBUTION(leveldir_current) ||
472 IS_LEVELCLASS_USER(leveldir_current))
474 /* For user contributed and private levels, use the version of
475 the game engine the levels were created for.
476 Since 2.0.1, the game engine version is now directly stored
477 in the level file (chunk "VERS"), so there is no need anymore
478 to set the game version from the file version (except for old,
479 pre-2.0 levels, where the game version is still taken from the
480 file format version used to store the level -- see above). */
482 /* do some special adjustments to support older level versions */
483 if (level.file_version == FILE_VERSION_1_0)
485 Error(ERR_WARN, "level file '%s' has version number 1.0", filename);
486 Error(ERR_WARN, "using high speed movement for player");
488 /* player was faster than monsters in (pre-)1.0 levels */
489 level.double_speed = TRUE;
492 /* Default behaviour for EM style gems was "slippery" only in 2.0.1 */
493 if (level.game_version == VERSION_IDENT(2,0,1))
494 level.em_slippery_gems = TRUE;
498 /* Always use the latest version of the game engine for all but
499 user contributed and private levels; this allows for actual
500 corrections in the game engine to take effect for existing,
501 converted levels (from "classic" or other existing games) to
502 make the game emulation more accurate, while (hopefully) not
503 breaking existing levels created from other players. */
505 level.game_version = GAME_VERSION_ACTUAL;
507 /* Set special EM style gems behaviour: EM style gems slip down from
508 normal, steel and growing wall. As this is a more fundamental change,
509 it seems better to set the default behaviour to "off" (as it is more
510 natural) and make it configurable in the level editor (as a property
511 of gem style elements). Already existing converted levels (neither
512 private nor contributed levels) are changed to the new behaviour. */
514 if (level.file_version < FILE_VERSION_2_0)
515 level.em_slippery_gems = TRUE;
518 /* determine border element for this level */
522 void LoadLevel(int level_nr)
524 char *filename = getLevelFilename(level_nr);
526 LoadLevelFromFilename(filename);
529 static void SaveLevel_VERS(FILE *file, struct LevelInfo *level)
531 putFileVersion(file, level->file_version);
532 putFileVersion(file, level->game_version);
535 static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
539 fputc(level->fieldx, file);
540 fputc(level->fieldy, file);
542 putFile16BitBE(file, level->time);
543 putFile16BitBE(file, level->gems_needed);
545 for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
546 fputc(level->name[i], file);
548 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
549 fputc(level->score[i], file);
551 for(i=0; i<STD_ELEMENT_CONTENTS; i++)
554 fputc((level->encoding_16bit_yamyam ? EL_EMPTY :
555 level->yam_content[i][x][y]),
557 fputc(level->amoeba_speed, file);
558 fputc(level->time_magic_wall, file);
559 fputc(level->time_wheel, file);
560 fputc((level->encoding_16bit_amoeba ? EL_EMPTY : level->amoeba_content),
562 fputc((level->double_speed ? 1 : 0), file);
563 fputc((level->gravity ? 1 : 0), file);
564 fputc((level->encoding_16bit_field ? 1 : 0), file);
565 fputc((level->em_slippery_gems ? 1 : 0), file);
567 WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
570 static void SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
574 for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
575 fputc(level->author[i], file);
578 static void SaveLevel_BODY(FILE *file, struct LevelInfo *level)
582 for(y=0; y<level->fieldy; y++)
583 for(x=0; x<level->fieldx; x++)
584 if (level->encoding_16bit_field)
585 putFile16BitBE(file, Ur[x][y]);
587 fputc(Ur[x][y], file);
591 static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
595 fputc(EL_YAMYAM, file);
596 fputc(level->num_yam_contents, file);
600 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
603 if (level->encoding_16bit_field)
604 putFile16BitBE(file, level->yam_content[i][x][y]);
606 fputc(level->yam_content[i][x][y], file);
610 static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
613 int num_contents, content_xsize, content_ysize;
614 int content_array[MAX_ELEMENT_CONTENTS][3][3];
616 if (element == EL_YAMYAM)
618 num_contents = level->num_yam_contents;
622 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
625 content_array[i][x][y] = level->yam_content[i][x][y];
627 else if (element == EL_BD_AMOEBA)
633 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
636 content_array[i][x][y] = EL_EMPTY;
637 content_array[0][0][0] = level->amoeba_content;
641 /* chunk header already written -- write empty chunk data */
642 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
644 Error(ERR_WARN, "cannot save content for element '%d'", element);
648 putFile16BitBE(file, element);
649 fputc(num_contents, file);
650 fputc(content_xsize, file);
651 fputc(content_ysize, file);
653 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
655 for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
658 putFile16BitBE(file, content_array[i][x][y]);
661 static void SaveLevel_CUS1(FILE *file, int num_changed_custom_elements)
665 putFile16BitBE(file, num_changed_custom_elements);
667 for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
669 int element = EL_CUSTOM_START + i;
671 if (Properties1[element] != EP_BITMASK_DEFAULT)
673 if (check < num_changed_custom_elements)
675 putFile16BitBE(file, element);
676 putFile32BitBE(file, Properties1[element]);
683 if (check != num_changed_custom_elements) /* should not happen */
684 Error(ERR_WARN, "inconsistent number of custom element properties");
687 void SaveLevel(int level_nr)
689 char *filename = getLevelFilename(level_nr);
691 int num_changed_custom_elements = 0;
695 if (!(file = fopen(filename, MODE_WRITE)))
697 Error(ERR_WARN, "cannot save level file '%s'", filename);
701 level.file_version = FILE_VERSION_ACTUAL;
702 level.game_version = GAME_VERSION_ACTUAL;
704 /* check level field for 16-bit elements */
705 level.encoding_16bit_field = FALSE;
706 for(y=0; y<level.fieldy; y++)
707 for(x=0; x<level.fieldx; x++)
709 level.encoding_16bit_field = TRUE;
711 /* check yamyam content for 16-bit elements */
712 level.encoding_16bit_yamyam = FALSE;
713 for(i=0; i<level.num_yam_contents; i++)
716 if (level.yam_content[i][x][y] > 255)
717 level.encoding_16bit_yamyam = TRUE;
719 /* check amoeba content for 16-bit elements */
720 level.encoding_16bit_amoeba = FALSE;
721 if (level.amoeba_content > 255)
722 level.encoding_16bit_amoeba = TRUE;
724 /* calculate size of "BODY" chunk */
726 level.fieldx * level.fieldy * (level.encoding_16bit_field ? 2 : 1);
728 /* check for non-standard custom elements and calculate "CUS1" chunk size */
729 for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
730 if (Properties1[EL_CUSTOM_START + i] != EP_BITMASK_DEFAULT)
731 num_changed_custom_elements++;
733 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
734 putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
736 putFileChunkBE(file, "VERS", FILE_VERS_CHUNK_SIZE);
737 SaveLevel_VERS(file, &level);
739 putFileChunkBE(file, "HEAD", LEVEL_HEADER_SIZE);
740 SaveLevel_HEAD(file, &level);
742 putFileChunkBE(file, "AUTH", MAX_LEVEL_AUTHOR_LEN);
743 SaveLevel_AUTH(file, &level);
745 putFileChunkBE(file, "BODY", body_chunk_size);
746 SaveLevel_BODY(file, &level);
748 if (level.encoding_16bit_yamyam ||
749 level.num_yam_contents != STD_ELEMENT_CONTENTS)
751 putFileChunkBE(file, "CNT2", LEVEL_CHUNK_CNT2_SIZE);
752 SaveLevel_CNT2(file, &level, EL_YAMYAM);
755 if (level.encoding_16bit_amoeba)
757 putFileChunkBE(file, "CNT2", LEVEL_CHUNK_CNT2_SIZE);
758 SaveLevel_CNT2(file, &level, EL_BD_AMOEBA);
761 if (num_changed_custom_elements > 0)
763 putFileChunkBE(file, "CUS1", 2 + num_changed_custom_elements * 6);
764 SaveLevel_CUS1(file, num_changed_custom_elements);
769 SetFilePermissions(filename, PERMS_PRIVATE);
772 void DumpLevel(struct LevelInfo *level)
774 printf_line("-", 79);
775 printf("Level xxx (file version %06d, game version %06d)\n",
776 level->file_version, level->game_version);
777 printf_line("-", 79);
779 printf("Level Author: '%s'\n", level->author);
780 printf("Level Title: '%s'\n", level->name);
782 printf("Playfield Size: %d x %d\n", level->fieldx, level->fieldy);
784 printf("Level Time: %d seconds\n", level->time);
785 printf("Gems needed: %d\n", level->gems_needed);
787 printf("Time for Magic Wall: %d seconds\n", level->time_magic_wall);
788 printf("Time for Wheel: %d seconds\n", level->time_wheel);
789 printf("Time for Light: %d seconds\n", level->time_light);
790 printf("Time for Timegate: %d seconds\n", level->time_timegate);
792 printf("Amoeba Speed: %d\n", level->amoeba_speed);
794 printf("Gravity: %s\n", (level->gravity ? "yes" : "no"));
795 printf("Double Speed Movement: %s\n", (level->double_speed ? "yes" : "no"));
796 printf("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
798 printf_line("-", 79);
802 /* ========================================================================= */
803 /* tape file functions */
804 /* ========================================================================= */
806 static void setTapeInfoToDefaults()
810 /* always start with reliable default values (empty tape) */
813 /* default values (also for pre-1.2 tapes) with only the first player */
814 tape.player_participates[0] = TRUE;
815 for(i=1; i<MAX_PLAYERS; i++)
816 tape.player_participates[i] = FALSE;
818 /* at least one (default: the first) player participates in every tape */
819 tape.num_participating_players = 1;
821 tape.level_nr = level_nr;
823 tape.changed = FALSE;
825 tape.recording = FALSE;
826 tape.playing = FALSE;
827 tape.pausing = FALSE;
830 static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape)
832 tape->file_version = getFileVersion(file);
833 tape->game_version = getFileVersion(file);
838 static int LoadTape_HEAD(FILE *file, int chunk_size, struct TapeInfo *tape)
842 tape->random_seed = getFile32BitBE(file);
843 tape->date = getFile32BitBE(file);
844 tape->length = getFile32BitBE(file);
846 /* read header fields that are new since version 1.2 */
847 if (tape->file_version >= FILE_VERSION_1_2)
849 byte store_participating_players = fgetc(file);
852 /* since version 1.2, tapes store which players participate in the tape */
853 tape->num_participating_players = 0;
854 for(i=0; i<MAX_PLAYERS; i++)
856 tape->player_participates[i] = FALSE;
858 if (store_participating_players & (1 << i))
860 tape->player_participates[i] = TRUE;
861 tape->num_participating_players++;
865 ReadUnusedBytesFromFile(file, TAPE_HEADER_UNUSED);
867 engine_version = getFileVersion(file);
868 if (engine_version > 0)
869 tape->engine_version = engine_version;
875 static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
878 int chunk_size_expected =
879 (tape->num_participating_players + 1) * tape->length;
881 if (chunk_size_expected != chunk_size)
883 ReadUnusedBytesFromFile(file, chunk_size);
884 return chunk_size_expected;
887 for(i=0; i<tape->length; i++)
889 if (i >= MAX_TAPELEN)
892 for(j=0; j<MAX_PLAYERS; j++)
894 tape->pos[i].action[j] = MV_NO_MOVING;
896 if (tape->player_participates[j])
897 tape->pos[i].action[j] = fgetc(file);
900 tape->pos[i].delay = fgetc(file);
902 if (tape->file_version == FILE_VERSION_1_0)
904 /* eliminate possible diagonal moves in old tapes */
905 /* this is only for backward compatibility */
907 byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
908 byte action = tape->pos[i].action[0];
909 int k, num_moves = 0;
913 if (action & joy_dir[k])
915 tape->pos[i + num_moves].action[0] = joy_dir[k];
917 tape->pos[i + num_moves].delay = 0;
926 tape->length += num_moves;
929 else if (tape->file_version < FILE_VERSION_2_0)
931 /* convert pre-2.0 tapes to new tape format */
933 if (tape->pos[i].delay > 1)
936 tape->pos[i + 1] = tape->pos[i];
937 tape->pos[i + 1].delay = 1;
940 for(j=0; j<MAX_PLAYERS; j++)
941 tape->pos[i].action[j] = MV_NO_MOVING;
942 tape->pos[i].delay--;
953 if (i != tape->length)
954 chunk_size = (tape->num_participating_players + 1) * i;
959 void LoadTapeFromFilename(char *filename)
961 char cookie[MAX_LINE_LEN];
962 char chunk_name[CHUNK_ID_LEN + 1];
966 /* always start with reliable default values */
967 setTapeInfoToDefaults();
969 if (!(file = fopen(filename, MODE_READ)))
972 getFileChunkBE(file, chunk_name, NULL);
973 if (strcmp(chunk_name, "RND1") == 0)
975 getFile32BitBE(file); /* not used */
977 getFileChunkBE(file, chunk_name, NULL);
978 if (strcmp(chunk_name, "TAPE") != 0)
980 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
985 else /* check for pre-2.0 file format with cookie string */
987 strcpy(cookie, chunk_name);
988 fgets(&cookie[4], MAX_LINE_LEN - 4, file);
989 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
990 cookie[strlen(cookie) - 1] = '\0';
992 if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
994 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
999 if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
1001 Error(ERR_WARN, "unsupported version of tape file '%s'", filename);
1006 /* pre-2.0 tape files have no game version, so use file version here */
1007 tape.game_version = tape.file_version;
1010 if (tape.file_version < FILE_VERSION_1_2)
1012 /* tape files from versions before 1.2.0 without chunk structure */
1013 LoadTape_HEAD(file, TAPE_HEADER_SIZE, &tape);
1014 LoadTape_BODY(file, 2 * tape.length, &tape);
1022 int (*loader)(FILE *, int, struct TapeInfo *);
1026 { "VERS", FILE_VERS_CHUNK_SIZE, LoadTape_VERS },
1027 { "HEAD", TAPE_HEADER_SIZE, LoadTape_HEAD },
1028 { "BODY", -1, LoadTape_BODY },
1032 while (getFileChunkBE(file, chunk_name, &chunk_size))
1036 while (chunk_info[i].name != NULL &&
1037 strcmp(chunk_name, chunk_info[i].name) != 0)
1040 if (chunk_info[i].name == NULL)
1042 Error(ERR_WARN, "unknown chunk '%s' in tape file '%s'",
1043 chunk_name, filename);
1044 ReadUnusedBytesFromFile(file, chunk_size);
1046 else if (chunk_info[i].size != -1 &&
1047 chunk_info[i].size != chunk_size)
1049 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
1050 chunk_size, chunk_name, filename);
1051 ReadUnusedBytesFromFile(file, chunk_size);
1055 /* call function to load this tape chunk */
1056 int chunk_size_expected =
1057 (chunk_info[i].loader)(file, chunk_size, &tape);
1059 /* the size of some chunks cannot be checked before reading other
1060 chunks first (like "HEAD" and "BODY") that contain some header
1061 information, so check them here */
1062 if (chunk_size_expected != chunk_size)
1064 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
1065 chunk_size, chunk_name, filename);
1073 tape.length_seconds = GetTapeLength();
1076 void LoadTape(int level_nr)
1078 char *filename = getTapeFilename(level_nr);
1080 LoadTapeFromFilename(filename);
1083 static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
1085 putFileVersion(file, tape->file_version);
1086 putFileVersion(file, tape->game_version);
1089 static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
1092 byte store_participating_players = 0;
1094 /* set bits for participating players for compact storage */
1095 for(i=0; i<MAX_PLAYERS; i++)
1096 if (tape->player_participates[i])
1097 store_participating_players |= (1 << i);
1099 putFile32BitBE(file, tape->random_seed);
1100 putFile32BitBE(file, tape->date);
1101 putFile32BitBE(file, tape->length);
1103 fputc(store_participating_players, file);
1105 /* unused bytes not at the end here for 4-byte alignment of engine_version */
1106 WriteUnusedBytesToFile(file, TAPE_HEADER_UNUSED);
1108 putFileVersion(file, tape->engine_version);
1111 static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
1115 for(i=0; i<tape->length; i++)
1117 for(j=0; j<MAX_PLAYERS; j++)
1118 if (tape->player_participates[j])
1119 fputc(tape->pos[i].action[j], file);
1121 fputc(tape->pos[i].delay, file);
1125 void SaveTape(int level_nr)
1128 char *filename = getTapeFilename(level_nr);
1130 boolean new_tape = TRUE;
1131 int num_participating_players = 0;
1132 int body_chunk_size;
1134 InitTapeDirectory(leveldir_current->filename);
1136 /* if a tape still exists, ask to overwrite it */
1137 if (access(filename, F_OK) == 0)
1140 if (!Request("Replace old tape ?", REQ_ASK))
1144 if (!(file = fopen(filename, MODE_WRITE)))
1146 Error(ERR_WARN, "cannot save level recording file '%s'", filename);
1150 tape.file_version = FILE_VERSION_ACTUAL;
1151 tape.game_version = GAME_VERSION_ACTUAL;
1153 /* count number of participating players */
1154 for(i=0; i<MAX_PLAYERS; i++)
1155 if (tape.player_participates[i])
1156 num_participating_players++;
1158 body_chunk_size = (num_participating_players + 1) * tape.length;
1160 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
1161 putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
1163 putFileChunkBE(file, "VERS", FILE_VERS_CHUNK_SIZE);
1164 SaveTape_VERS(file, &tape);
1166 putFileChunkBE(file, "HEAD", TAPE_HEADER_SIZE);
1167 SaveTape_HEAD(file, &tape);
1169 putFileChunkBE(file, "BODY", body_chunk_size);
1170 SaveTape_BODY(file, &tape);
1174 SetFilePermissions(filename, PERMS_PRIVATE);
1176 tape.changed = FALSE;
1179 Request("tape saved !", REQ_CONFIRM);
1182 void DumpTape(struct TapeInfo *tape)
1186 if (TAPE_IS_EMPTY(*tape))
1188 Error(ERR_WARN, "no tape available for level %d", tape->level_nr);
1192 printf_line("-", 79);
1193 printf("Tape of Level %03d (file version %06d, game version %06d)\n",
1194 tape->level_nr, tape->file_version, tape->game_version);
1195 printf_line("-", 79);
1197 for(i=0; i<tape->length; i++)
1199 if (i >= MAX_TAPELEN)
1202 printf("%03d: ", i);
1204 for(j=0; j<MAX_PLAYERS; j++)
1206 if (tape->player_participates[j])
1208 int action = tape->pos[i].action[j];
1210 printf("%d:%02x ", j, action);
1211 printf("[%c%c%c%c|%c%c] - ",
1212 (action & JOY_LEFT ? '<' : ' '),
1213 (action & JOY_RIGHT ? '>' : ' '),
1214 (action & JOY_UP ? '^' : ' '),
1215 (action & JOY_DOWN ? 'v' : ' '),
1216 (action & JOY_BUTTON_1 ? '1' : ' '),
1217 (action & JOY_BUTTON_2 ? '2' : ' '));
1221 printf("(%03d)\n", tape->pos[i].delay);
1224 printf_line("-", 79);
1228 /* ========================================================================= */
1229 /* score file functions */
1230 /* ========================================================================= */
1232 void LoadScore(int level_nr)
1235 char *filename = getScoreFilename(level_nr);
1236 char cookie[MAX_LINE_LEN];
1237 char line[MAX_LINE_LEN];
1241 /* always start with reliable default values */
1242 for(i=0; i<MAX_SCORE_ENTRIES; i++)
1244 strcpy(highscore[i].Name, EMPTY_PLAYER_NAME);
1245 highscore[i].Score = 0;
1248 if (!(file = fopen(filename, MODE_READ)))
1251 /* check file identifier */
1252 fgets(cookie, MAX_LINE_LEN, file);
1253 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
1254 cookie[strlen(cookie) - 1] = '\0';
1256 if (!checkCookieString(cookie, SCORE_COOKIE))
1258 Error(ERR_WARN, "unknown format of score file '%s'", filename);
1263 for(i=0; i<MAX_SCORE_ENTRIES; i++)
1265 fscanf(file, "%d", &highscore[i].Score);
1266 fgets(line, MAX_LINE_LEN, file);
1268 if (line[strlen(line) - 1] == '\n')
1269 line[strlen(line) - 1] = '\0';
1271 for (line_ptr = line; *line_ptr; line_ptr++)
1273 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
1275 strncpy(highscore[i].Name, line_ptr, MAX_PLAYER_NAME_LEN);
1276 highscore[i].Name[MAX_PLAYER_NAME_LEN] = '\0';
1285 void SaveScore(int level_nr)
1288 char *filename = getScoreFilename(level_nr);
1291 InitScoreDirectory(leveldir_current->filename);
1293 if (!(file = fopen(filename, MODE_WRITE)))
1295 Error(ERR_WARN, "cannot save score for level %d", level_nr);
1299 fprintf(file, "%s\n\n", SCORE_COOKIE);
1301 for(i=0; i<MAX_SCORE_ENTRIES; i++)
1302 fprintf(file, "%d %s\n", highscore[i].Score, highscore[i].Name);
1306 SetFilePermissions(filename, PERMS_PUBLIC);
1310 /* ========================================================================= */
1311 /* setup file functions */
1312 /* ========================================================================= */
1314 #define TOKEN_STR_PLAYER_PREFIX "player_"
1317 #define SETUP_TOKEN_PLAYER_NAME 0
1318 #define SETUP_TOKEN_SOUND 1
1319 #define SETUP_TOKEN_SOUND_LOOPS 2
1320 #define SETUP_TOKEN_SOUND_MUSIC 3
1321 #define SETUP_TOKEN_SOUND_SIMPLE 4
1322 #define SETUP_TOKEN_TOONS 5
1323 #define SETUP_TOKEN_SCROLL_DELAY 6
1324 #define SETUP_TOKEN_SOFT_SCROLLING 7
1325 #define SETUP_TOKEN_FADING 8
1326 #define SETUP_TOKEN_AUTORECORD 9
1327 #define SETUP_TOKEN_QUICK_DOORS 10
1328 #define SETUP_TOKEN_TEAM_MODE 11
1329 #define SETUP_TOKEN_HANDICAP 12
1330 #define SETUP_TOKEN_TIME_LIMIT 13
1331 #define SETUP_TOKEN_FULLSCREEN 14
1332 #define SETUP_TOKEN_ASK_ON_ESCAPE 15
1333 #define SETUP_TOKEN_GRAPHICS_SET 16
1334 #define SETUP_TOKEN_SOUNDS_SET 17
1335 #define SETUP_TOKEN_MUSIC_SET 18
1336 #define SETUP_TOKEN_OVERRIDE_LEVEL_GRAPHICS 19
1337 #define SETUP_TOKEN_OVERRIDE_LEVEL_SOUNDS 20
1338 #define SETUP_TOKEN_OVERRIDE_LEVEL_MUSIC 21
1340 #define NUM_GLOBAL_SETUP_TOKENS 22
1343 #define SETUP_TOKEN_EDITOR_EL_BOULDERDASH 0
1344 #define SETUP_TOKEN_EDITOR_EL_EMERALD_MINE 1
1345 #define SETUP_TOKEN_EDITOR_EL_MORE 2
1346 #define SETUP_TOKEN_EDITOR_EL_SOKOBAN 3
1347 #define SETUP_TOKEN_EDITOR_EL_SUPAPLEX 4
1348 #define SETUP_TOKEN_EDITOR_EL_DIAMOND_CAVES 5
1349 #define SETUP_TOKEN_EDITOR_EL_DX_BOULDERDASH 6
1350 #define SETUP_TOKEN_EDITOR_EL_CHARS 7
1351 #define SETUP_TOKEN_EDITOR_EL_CUSTOM 8
1353 #define NUM_EDITOR_SETUP_TOKENS 9
1355 /* shortcut setup */
1356 #define SETUP_TOKEN_SHORTCUT_SAVE_GAME 0
1357 #define SETUP_TOKEN_SHORTCUT_LOAD_GAME 1
1358 #define SETUP_TOKEN_SHORTCUT_TOGGLE_PAUSE 2
1360 #define NUM_SHORTCUT_SETUP_TOKENS 3
1363 #define SETUP_TOKEN_PLAYER_USE_JOYSTICK 0
1364 #define SETUP_TOKEN_PLAYER_JOY_DEVICE_NAME 1
1365 #define SETUP_TOKEN_PLAYER_JOY_XLEFT 2
1366 #define SETUP_TOKEN_PLAYER_JOY_XMIDDLE 3
1367 #define SETUP_TOKEN_PLAYER_JOY_XRIGHT 4
1368 #define SETUP_TOKEN_PLAYER_JOY_YUPPER 5
1369 #define SETUP_TOKEN_PLAYER_JOY_YMIDDLE 6
1370 #define SETUP_TOKEN_PLAYER_JOY_YLOWER 7
1371 #define SETUP_TOKEN_PLAYER_JOY_SNAP 8
1372 #define SETUP_TOKEN_PLAYER_JOY_BOMB 9
1373 #define SETUP_TOKEN_PLAYER_KEY_LEFT 10
1374 #define SETUP_TOKEN_PLAYER_KEY_RIGHT 11
1375 #define SETUP_TOKEN_PLAYER_KEY_UP 12
1376 #define SETUP_TOKEN_PLAYER_KEY_DOWN 13
1377 #define SETUP_TOKEN_PLAYER_KEY_SNAP 14
1378 #define SETUP_TOKEN_PLAYER_KEY_BOMB 15
1380 #define NUM_PLAYER_SETUP_TOKENS 16
1382 static struct SetupInfo si;
1383 static struct SetupEditorInfo sei;
1384 static struct SetupShortcutInfo ssi;
1385 static struct SetupInputInfo sii;
1387 static struct TokenInfo global_setup_tokens[] =
1390 { TYPE_STRING, &si.player_name, "player_name" },
1391 { TYPE_SWITCH, &si.sound, "sound" },
1392 { TYPE_SWITCH, &si.sound_loops, "repeating_sound_loops" },
1393 { TYPE_SWITCH, &si.sound_music, "background_music" },
1394 { TYPE_SWITCH, &si.sound_simple, "simple_sound_effects" },
1395 { TYPE_SWITCH, &si.toons, "toons" },
1396 { TYPE_SWITCH, &si.scroll_delay, "scroll_delay" },
1397 { TYPE_SWITCH, &si.soft_scrolling, "soft_scrolling" },
1398 { TYPE_SWITCH, &si.fading, "screen_fading" },
1399 { TYPE_SWITCH, &si.autorecord, "automatic_tape_recording" },
1400 { TYPE_SWITCH, &si.quick_doors, "quick_doors" },
1401 { TYPE_SWITCH, &si.team_mode, "team_mode" },
1402 { TYPE_SWITCH, &si.handicap, "handicap" },
1403 { TYPE_SWITCH, &si.time_limit, "time_limit" },
1404 { TYPE_SWITCH, &si.fullscreen, "fullscreen" },
1405 { TYPE_SWITCH, &si.ask_on_escape, "ask_on_escape" },
1406 { TYPE_STRING, &si.graphics_set, "graphics_set" },
1407 { TYPE_STRING, &si.sounds_set, "sounds_set" },
1408 { TYPE_STRING, &si.music_set, "music_set" },
1409 { TYPE_SWITCH, &si.override_level_graphics, "override_level_graphics" },
1410 { TYPE_SWITCH, &si.override_level_sounds, "override_level_sounds" },
1411 { TYPE_SWITCH, &si.override_level_music, "override_level_music" },
1414 static struct TokenInfo editor_setup_tokens[] =
1416 /* shortcut setup */
1417 { TYPE_SWITCH, &sei.el_boulderdash, "editor.el_boulderdash" },
1418 { TYPE_SWITCH, &sei.el_emerald_mine, "editor.el_emerald_mine" },
1419 { TYPE_SWITCH, &sei.el_more, "editor.el_more" },
1420 { TYPE_SWITCH, &sei.el_sokoban, "editor.el_sokoban" },
1421 { TYPE_SWITCH, &sei.el_supaplex, "editor.el_supaplex" },
1422 { TYPE_SWITCH, &sei.el_diamond_caves, "editor.el_diamond_caves" },
1423 { TYPE_SWITCH, &sei.el_dx_boulderdash,"editor.el_dx_boulderdash" },
1424 { TYPE_SWITCH, &sei.el_chars, "editor.el_chars" },
1425 { TYPE_SWITCH, &sei.el_custom, "editor.el_custom" },
1428 static struct TokenInfo shortcut_setup_tokens[] =
1430 /* shortcut setup */
1431 { TYPE_KEY_X11, &ssi.save_game, "shortcut.save_game" },
1432 { TYPE_KEY_X11, &ssi.load_game, "shortcut.load_game" },
1433 { TYPE_KEY_X11, &ssi.toggle_pause, "shortcut.toggle_pause" }
1436 static struct TokenInfo player_setup_tokens[] =
1439 { TYPE_BOOLEAN, &sii.use_joystick, ".use_joystick" },
1440 { TYPE_STRING, &sii.joy.device_name, ".joy.device_name" },
1441 { TYPE_INTEGER, &sii.joy.xleft, ".joy.xleft" },
1442 { TYPE_INTEGER, &sii.joy.xmiddle, ".joy.xmiddle" },
1443 { TYPE_INTEGER, &sii.joy.xright, ".joy.xright" },
1444 { TYPE_INTEGER, &sii.joy.yupper, ".joy.yupper" },
1445 { TYPE_INTEGER, &sii.joy.ymiddle, ".joy.ymiddle" },
1446 { TYPE_INTEGER, &sii.joy.ylower, ".joy.ylower" },
1447 { TYPE_INTEGER, &sii.joy.snap, ".joy.snap_field" },
1448 { TYPE_INTEGER, &sii.joy.bomb, ".joy.place_bomb" },
1449 { TYPE_KEY_X11, &sii.key.left, ".key.move_left" },
1450 { TYPE_KEY_X11, &sii.key.right, ".key.move_right" },
1451 { TYPE_KEY_X11, &sii.key.up, ".key.move_up" },
1452 { TYPE_KEY_X11, &sii.key.down, ".key.move_down" },
1453 { TYPE_KEY_X11, &sii.key.snap, ".key.snap_field" },
1454 { TYPE_KEY_X11, &sii.key.bomb, ".key.place_bomb" }
1457 static void setSetupInfoToDefaults(struct SetupInfo *si)
1461 si->player_name = getStringCopy(getLoginName());
1464 si->sound_loops = TRUE;
1465 si->sound_music = TRUE;
1466 si->sound_simple = TRUE;
1468 si->double_buffering = TRUE;
1469 si->direct_draw = !si->double_buffering;
1470 si->scroll_delay = TRUE;
1471 si->soft_scrolling = TRUE;
1473 si->autorecord = TRUE;
1474 si->quick_doors = FALSE;
1475 si->team_mode = FALSE;
1476 si->handicap = TRUE;
1477 si->time_limit = TRUE;
1478 si->fullscreen = FALSE;
1479 si->ask_on_escape = TRUE;
1481 si->graphics_set = getStringCopy(GRAPHICS_SUBDIR);
1482 si->sounds_set = getStringCopy(SOUNDS_SUBDIR);
1483 si->music_set = getStringCopy(MUSIC_SUBDIR);
1484 si->override_level_graphics = FALSE;
1485 si->override_level_sounds = FALSE;
1486 si->override_level_music = FALSE;
1488 si->editor.el_boulderdash = TRUE;
1489 si->editor.el_emerald_mine = TRUE;
1490 si->editor.el_more = TRUE;
1491 si->editor.el_sokoban = TRUE;
1492 si->editor.el_supaplex = TRUE;
1493 si->editor.el_diamond_caves = TRUE;
1494 si->editor.el_dx_boulderdash = TRUE;
1495 si->editor.el_chars = TRUE;
1496 si->editor.el_custom = FALSE;
1498 si->shortcut.save_game = DEFAULT_KEY_SAVE_GAME;
1499 si->shortcut.load_game = DEFAULT_KEY_LOAD_GAME;
1500 si->shortcut.toggle_pause = DEFAULT_KEY_TOGGLE_PAUSE;
1502 for (i=0; i<MAX_PLAYERS; i++)
1504 si->input[i].use_joystick = FALSE;
1505 si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i));
1506 si->input[i].joy.xleft = JOYSTICK_XLEFT;
1507 si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE;
1508 si->input[i].joy.xright = JOYSTICK_XRIGHT;
1509 si->input[i].joy.yupper = JOYSTICK_YUPPER;
1510 si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE;
1511 si->input[i].joy.ylower = JOYSTICK_YLOWER;
1512 si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0);
1513 si->input[i].joy.bomb = (i == 0 ? JOY_BUTTON_2 : 0);
1514 si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED);
1515 si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED);
1516 si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED);
1517 si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED);
1518 si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED);
1519 si->input[i].key.bomb = (i == 0 ? DEFAULT_KEY_BOMB : KSYM_UNDEFINED);
1523 static void decodeSetupFileList(struct SetupFileList *setup_file_list)
1527 if (!setup_file_list)
1532 for (i=0; i<NUM_GLOBAL_SETUP_TOKENS; i++)
1533 setSetupInfo(global_setup_tokens, i,
1534 getTokenValue(setup_file_list, global_setup_tokens[i].text));
1539 for (i=0; i<NUM_EDITOR_SETUP_TOKENS; i++)
1540 setSetupInfo(editor_setup_tokens, i,
1541 getTokenValue(setup_file_list,editor_setup_tokens[i].text));
1544 /* shortcut setup */
1545 ssi = setup.shortcut;
1546 for (i=0; i<NUM_SHORTCUT_SETUP_TOKENS; i++)
1547 setSetupInfo(shortcut_setup_tokens, i,
1548 getTokenValue(setup_file_list,shortcut_setup_tokens[i].text));
1549 setup.shortcut = ssi;
1552 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
1556 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
1558 sii = setup.input[pnr];
1559 for (i=0; i<NUM_PLAYER_SETUP_TOKENS; i++)
1561 char full_token[100];
1563 sprintf(full_token, "%s%s", prefix, player_setup_tokens[i].text);
1564 setSetupInfo(player_setup_tokens, i,
1565 getTokenValue(setup_file_list, full_token));
1567 setup.input[pnr] = sii;
1573 char *filename = getSetupFilename();
1574 struct SetupFileList *setup_file_list = NULL;
1576 /* always start with reliable default values */
1577 setSetupInfoToDefaults(&setup);
1579 setup_file_list = loadSetupFileList(filename);
1581 if (setup_file_list)
1583 checkSetupFileListIdentifier(setup_file_list, getCookie("SETUP"));
1584 decodeSetupFileList(setup_file_list);
1586 setup.direct_draw = !setup.double_buffering;
1588 freeSetupFileList(setup_file_list);
1590 /* needed to work around problems with fixed length strings */
1591 if (strlen(setup.player_name) > MAX_PLAYER_NAME_LEN)
1592 setup.player_name[MAX_PLAYER_NAME_LEN] = '\0';
1593 else if (strlen(setup.player_name) < MAX_PLAYER_NAME_LEN)
1595 char *new_name = checked_malloc(MAX_PLAYER_NAME_LEN + 1);
1597 strcpy(new_name, setup.player_name);
1598 free(setup.player_name);
1599 setup.player_name = new_name;
1603 Error(ERR_WARN, "using default setup values");
1608 char *filename = getSetupFilename();
1612 InitUserDataDirectory();
1614 if (!(file = fopen(filename, MODE_WRITE)))
1616 Error(ERR_WARN, "cannot write setup file '%s'", filename);
1620 fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
1621 getCookie("SETUP")));
1622 fprintf(file, "\n");
1626 for (i=0; i<NUM_GLOBAL_SETUP_TOKENS; i++)
1628 /* just to make things nicer :) */
1629 if (i == SETUP_TOKEN_PLAYER_NAME + 1 ||
1630 i == SETUP_TOKEN_GRAPHICS_SET)
1631 fprintf(file, "\n");
1633 fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i));
1638 fprintf(file, "\n");
1639 for (i=0; i<NUM_EDITOR_SETUP_TOKENS; i++)
1640 fprintf(file, "%s\n", getSetupLine(editor_setup_tokens, "", i));
1642 /* shortcut setup */
1643 ssi = setup.shortcut;
1644 fprintf(file, "\n");
1645 for (i=0; i<NUM_SHORTCUT_SETUP_TOKENS; i++)
1646 fprintf(file, "%s\n", getSetupLine(shortcut_setup_tokens, "", i));
1649 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
1653 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
1654 fprintf(file, "\n");
1656 sii = setup.input[pnr];
1657 for (i=0; i<NUM_PLAYER_SETUP_TOKENS; i++)
1658 fprintf(file, "%s\n", getSetupLine(player_setup_tokens, prefix, i));
1663 SetFilePermissions(filename, PERMS_PRIVATE);
1666 void LoadCustomElementDescriptions()
1668 char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
1669 struct SetupFileList *setup_file_list;
1672 for (i=0; i<NUM_FILE_ELEMENTS; i++)
1674 if (element_info[i].custom_description != NULL)
1676 free(element_info[i].custom_description);
1677 element_info[i].custom_description = NULL;
1681 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1684 for (i=0; i<NUM_FILE_ELEMENTS; i++)
1686 char *token = getStringCat2(element_info[i].token_name, ".name");
1687 char *value = getTokenValue(setup_file_list, token);
1690 element_info[i].custom_description = getStringCopy(value);
1695 freeSetupFileList(setup_file_list);
1698 void LoadSpecialMenuDesignSettings()
1700 char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
1701 struct SetupFileList *setup_file_list;
1704 gfx.menu_main_hide_static_text = FALSE;
1706 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1709 value = getTokenValue(setup_file_list, "menu.main.hide_static_text");
1712 gfx.menu_main_hide_static_text = get_boolean_from_string(value);
1714 freeSetupFileList(setup_file_list);