1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2006 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
19 #include "libgame/libgame.h"
27 #define CHUNK_ID_LEN 4 /* IFF style chunk id length */
28 #define CHUNK_SIZE_UNDEFINED 0 /* undefined chunk size == 0 */
29 #define CHUNK_SIZE_NONE -1 /* do not write chunk size */
31 #define LEVEL_CHUNK_NAME_SIZE MAX_LEVEL_NAME_LEN
32 #define LEVEL_CHUNK_AUTH_SIZE MAX_LEVEL_AUTHOR_LEN
34 #define LEVEL_CHUNK_VERS_SIZE 8 /* size of file version chunk */
35 #define LEVEL_CHUNK_DATE_SIZE 4 /* size of file date chunk */
36 #define LEVEL_CHUNK_HEAD_SIZE 80 /* size of level file header */
37 #define LEVEL_CHUNK_HEAD_UNUSED 0 /* unused level header bytes */
38 #define LEVEL_CHUNK_CNT2_SIZE 160 /* size of level CNT2 chunk */
39 #define LEVEL_CHUNK_CNT2_UNUSED 11 /* unused CNT2 chunk bytes */
40 #define LEVEL_CHUNK_CNT3_HEADER 16 /* size of level CNT3 header */
41 #define LEVEL_CHUNK_CNT3_UNUSED 10 /* unused CNT3 chunk bytes */
42 #define LEVEL_CPART_CUS3_SIZE 134 /* size of CUS3 chunk part */
43 #define LEVEL_CPART_CUS3_UNUSED 15 /* unused CUS3 bytes / part */
44 #define LEVEL_CHUNK_GRP1_SIZE 74 /* size of level GRP1 chunk */
46 /* (element number, number of change pages, change page number) */
47 #define LEVEL_CHUNK_CUSX_UNCHANGED (2 + (1 + 1) + (1 + 1))
49 /* (element number only) */
50 #define LEVEL_CHUNK_GRPX_UNCHANGED 2
51 #define LEVEL_CHUNK_NOTE_UNCHANGED 2
53 /* (nothing at all if unchanged) */
54 #define LEVEL_CHUNK_ELEM_UNCHANGED 0
56 #define TAPE_CHUNK_VERS_SIZE 8 /* size of file version chunk */
57 #define TAPE_CHUNK_HEAD_SIZE 20 /* size of tape file header */
58 #define TAPE_CHUNK_HEAD_UNUSED 3 /* unused tape header bytes */
60 #define LEVEL_CHUNK_CNT3_SIZE(x) (LEVEL_CHUNK_CNT3_HEADER + (x))
61 #define LEVEL_CHUNK_CUS3_SIZE(x) (2 + (x) * LEVEL_CPART_CUS3_SIZE)
62 #define LEVEL_CHUNK_CUS4_SIZE(x) (96 + (x) * 48)
64 /* file identifier strings */
65 #define LEVEL_COOKIE_TMPL "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_x.x"
66 #define TAPE_COOKIE_TMPL "ROCKSNDIAMONDS_TAPE_FILE_VERSION_x.x"
67 #define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2"
69 /* values for deciding when (not) to save configuration data */
70 #define SAVE_CONF_NEVER 0
71 #define SAVE_CONF_ALWAYS 1
72 #define SAVE_CONF_WHEN_CHANGED -1
74 /* values for chunks using micro chunks */
75 #define CONF_MASK_1_BYTE 0x00
76 #define CONF_MASK_2_BYTE 0x40
77 #define CONF_MASK_4_BYTE 0x80
78 #define CONF_MASK_MULTI_BYTES 0xc0
80 #define CONF_MASK_BYTES 0xc0
81 #define CONF_MASK_TOKEN 0x3f
83 #define CONF_VALUE_1_BYTE(x) (CONF_MASK_1_BYTE | (x))
84 #define CONF_VALUE_2_BYTE(x) (CONF_MASK_2_BYTE | (x))
85 #define CONF_VALUE_4_BYTE(x) (CONF_MASK_4_BYTE | (x))
86 #define CONF_VALUE_MULTI_BYTES(x) (CONF_MASK_MULTI_BYTES | (x))
88 /* these definitions are just for convenience of use and readability */
89 #define CONF_VALUE_8_BIT(x) CONF_VALUE_1_BYTE(x)
90 #define CONF_VALUE_16_BIT(x) CONF_VALUE_2_BYTE(x)
91 #define CONF_VALUE_32_BIT(x) CONF_VALUE_4_BYTE(x)
92 #define CONF_VALUE_BYTES(x) CONF_VALUE_MULTI_BYTES(x)
94 #define CONF_VALUE_NUM_BYTES(x) ((x) == CONF_MASK_1_BYTE ? 1 : \
95 (x) == CONF_MASK_2_BYTE ? 2 : \
96 (x) == CONF_MASK_4_BYTE ? 4 : 0)
98 #define CONF_CONTENT_NUM_ELEMENTS (3 * 3)
99 #define CONF_CONTENT_NUM_BYTES (CONF_CONTENT_NUM_ELEMENTS * 2)
100 #define CONF_ELEMENT_NUM_BYTES (2)
102 #define CONF_ENTITY_NUM_BYTES(t) ((t) == TYPE_ELEMENT || \
103 (t) == TYPE_ELEMENT_LIST ? \
104 CONF_ELEMENT_NUM_BYTES : \
105 (t) == TYPE_CONTENT || \
106 (t) == TYPE_CONTENT_LIST ? \
107 CONF_CONTENT_NUM_BYTES : 1)
109 #define CONF_ELEMENT_BYTE_POS(i) ((i) * CONF_ELEMENT_NUM_BYTES)
110 #define CONF_ELEMENTS_ELEMENT(b,i) ((b[CONF_ELEMENT_BYTE_POS(i)] << 8) | \
111 (b[CONF_ELEMENT_BYTE_POS(i) + 1]))
113 #define CONF_CONTENT_ELEMENT_POS(c,x,y) ((c) * CONF_CONTENT_NUM_ELEMENTS + \
115 #define CONF_CONTENT_BYTE_POS(c,x,y) (CONF_CONTENT_ELEMENT_POS(c,x,y) * \
116 CONF_ELEMENT_NUM_BYTES)
117 #define CONF_CONTENTS_ELEMENT(b,c,x,y) ((b[CONF_CONTENT_BYTE_POS(c,x,y)]<< 8)|\
118 (b[CONF_CONTENT_BYTE_POS(c,x,y) + 1]))
120 /* temporary variables used to store pointers to structure members */
121 static struct LevelInfo li;
122 static struct ElementInfo xx_ei, yy_ei;
123 static struct ElementChangeInfo xx_change;
124 static struct ElementGroupInfo xx_group;
125 static struct EnvelopeInfo xx_envelope;
126 static unsigned int xx_event_bits[NUM_CE_BITFIELDS];
127 static char xx_default_description[MAX_ELEMENT_NAME_LEN + 1];
128 static int xx_num_contents;
129 static int xx_current_change_page;
130 static char xx_default_string_empty[1] = "";
131 static int xx_string_length_unused;
133 struct LevelFileConfigInfo
135 int element; /* element for which data is to be stored */
136 int save_type; /* save data always, never or when changed */
137 int data_type; /* data type (used internally, not stored) */
138 int conf_type; /* micro chunk identifier (stored in file) */
141 void *value; /* variable that holds the data to be stored */
142 int default_value; /* initial default value for this variable */
145 void *value_copy; /* variable that holds the data to be copied */
146 void *num_entities; /* number of entities for multi-byte data */
147 int default_num_entities; /* default number of entities for this data */
148 int max_num_entities; /* maximal number of entities for this data */
149 char *default_string; /* optional default string for string data */
152 static struct LevelFileConfigInfo chunk_config_INFO[] =
154 /* ---------- values not related to single elements ----------------------- */
157 -1, SAVE_CONF_ALWAYS,
158 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
159 &li.game_engine_type, GAME_ENGINE_TYPE_RND
163 -1, SAVE_CONF_ALWAYS,
164 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
165 &li.fieldx, STD_LEV_FIELDX
168 -1, SAVE_CONF_ALWAYS,
169 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
170 &li.fieldy, STD_LEV_FIELDY
174 -1, SAVE_CONF_ALWAYS,
175 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
180 -1, SAVE_CONF_ALWAYS,
181 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
187 TYPE_INTEGER, CONF_VALUE_32_BIT(2),
193 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
194 &li.use_step_counter, FALSE
199 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
200 &li.wind_direction_initial, MV_NONE
205 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
206 &li.em_slippery_gems, FALSE
211 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
212 &li.use_custom_template, FALSE
217 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
218 &li.can_move_into_acid_bits, ~0 /* default: everything can */
223 TYPE_BITFIELD, CONF_VALUE_8_BIT(7),
224 &li.dont_collide_with_bits, ~0 /* default: always deadly */
229 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
230 &li.em_explodes_by_fire, FALSE
235 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
236 &li.score[SC_TIME_BONUS], 1
241 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
242 &li.auto_exit_sokoban, FALSE
252 static struct LevelFileConfigInfo chunk_config_ELEM[] =
254 /* (these values are the same for each player) */
257 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
258 &li.block_last_field, FALSE /* default case for EM levels */
262 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
263 &li.sp_block_last_field, TRUE /* default case for SP levels */
267 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
268 &li.instant_relocation, FALSE
272 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
273 &li.can_pass_to_walkable, FALSE
277 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
278 &li.block_snap_field, TRUE
282 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
283 &li.continuous_snapping, TRUE
287 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
288 &li.shifted_relocation, FALSE
291 /* (these values are different for each player) */
294 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
295 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
299 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
300 &li.initial_player_gravity[0], FALSE
304 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
305 &li.use_start_element[0], FALSE
309 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
310 &li.start_element[0], EL_PLAYER_1
314 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
315 &li.use_artwork_element[0], FALSE
319 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
320 &li.artwork_element[0], EL_PLAYER_1
324 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
325 &li.use_explosion_element[0], FALSE
329 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
330 &li.explosion_element[0], EL_PLAYER_1
334 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
335 &li.use_initial_inventory[0], FALSE
339 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
340 &li.initial_inventory_size[0], 1
344 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
345 &li.initial_inventory_content[0][0],EL_EMPTY, NULL,
346 &li.initial_inventory_size[0], 1, MAX_INITIAL_INVENTORY_SIZE
351 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
352 &li.initial_player_stepsize[1], STEPSIZE_NORMAL
356 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
357 &li.initial_player_gravity[1], FALSE
361 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
362 &li.use_start_element[1], FALSE
366 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
367 &li.start_element[1], EL_PLAYER_2
371 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
372 &li.use_artwork_element[1], FALSE
376 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
377 &li.artwork_element[1], EL_PLAYER_2
381 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
382 &li.use_explosion_element[1], FALSE
386 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
387 &li.explosion_element[1], EL_PLAYER_2
391 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
392 &li.use_initial_inventory[1], FALSE
396 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
397 &li.initial_inventory_size[1], 1
401 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
402 &li.initial_inventory_content[1][0],EL_EMPTY, NULL,
403 &li.initial_inventory_size[1], 1, MAX_INITIAL_INVENTORY_SIZE
408 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
409 &li.initial_player_stepsize[2], STEPSIZE_NORMAL
413 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
414 &li.initial_player_gravity[2], FALSE
418 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
419 &li.use_start_element[2], FALSE
423 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
424 &li.start_element[2], EL_PLAYER_3
428 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
429 &li.use_artwork_element[2], FALSE
433 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
434 &li.artwork_element[2], EL_PLAYER_3
438 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
439 &li.use_explosion_element[2], FALSE
443 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
444 &li.explosion_element[2], EL_PLAYER_3
448 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
449 &li.use_initial_inventory[2], FALSE
453 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
454 &li.initial_inventory_size[2], 1
458 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
459 &li.initial_inventory_content[2][0],EL_EMPTY, NULL,
460 &li.initial_inventory_size[2], 1, MAX_INITIAL_INVENTORY_SIZE
465 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
466 &li.initial_player_stepsize[3], STEPSIZE_NORMAL
470 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
471 &li.initial_player_gravity[3], FALSE
475 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
476 &li.use_start_element[3], FALSE
480 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
481 &li.start_element[3], EL_PLAYER_4
485 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
486 &li.use_artwork_element[3], FALSE
490 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
491 &li.artwork_element[3], EL_PLAYER_4
495 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
496 &li.use_explosion_element[3], FALSE
500 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
501 &li.explosion_element[3], EL_PLAYER_4
505 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
506 &li.use_initial_inventory[3], FALSE
510 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
511 &li.initial_inventory_size[3], 1
515 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
516 &li.initial_inventory_content[3][0],EL_EMPTY, NULL,
517 &li.initial_inventory_size[3], 1, MAX_INITIAL_INVENTORY_SIZE
522 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
523 &li.score[SC_EMERALD], 10
528 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
529 &li.score[SC_DIAMOND], 10
534 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
535 &li.score[SC_BUG], 10
540 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
541 &li.score[SC_SPACESHIP], 10
546 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
547 &li.score[SC_PACMAN], 10
552 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
553 &li.score[SC_NUT], 10
558 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
559 &li.score[SC_DYNAMITE], 10
564 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
565 &li.score[SC_KEY], 10
570 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
571 &li.score[SC_PEARL], 10
576 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
577 &li.score[SC_CRYSTAL], 10
582 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
583 &li.amoeba_content, EL_DIAMOND
587 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
592 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
593 &li.grow_into_diggable, TRUE
598 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
599 &li.yamyam_content, EL_ROCK, NULL,
600 &li.num_yamyam_contents, 4, MAX_ELEMENT_CONTENTS
604 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
605 &li.score[SC_YAMYAM], 10
610 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
611 &li.score[SC_ROBOT], 10
615 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
621 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
627 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
628 &li.time_magic_wall, 10
633 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
634 &li.game_of_life[0], 2
638 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
639 &li.game_of_life[1], 3
643 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
644 &li.game_of_life[2], 3
648 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
649 &li.game_of_life[3], 3
654 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
659 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
664 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
669 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
674 EL_TIMEGATE_SWITCH, -1,
675 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
676 &li.time_timegate, 10
680 EL_LIGHT_SWITCH_ACTIVE, -1,
681 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
686 EL_SHIELD_NORMAL, -1,
687 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
688 &li.shield_normal_time, 10
691 EL_SHIELD_NORMAL, -1,
692 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
693 &li.score[SC_SHIELD], 10
697 EL_SHIELD_DEADLY, -1,
698 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
699 &li.shield_deadly_time, 10
702 EL_SHIELD_DEADLY, -1,
703 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
704 &li.score[SC_SHIELD], 10
709 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
714 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
715 &li.extra_time_score, 10
719 EL_TIME_ORB_FULL, -1,
720 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
721 &li.time_orb_time, 10
724 EL_TIME_ORB_FULL, -1,
725 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
726 &li.use_time_orb_bug, FALSE
731 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
732 &li.use_spring_bug, FALSE
737 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
738 &li.android_move_time, 10
742 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
743 &li.android_clone_time, 10
747 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
748 &li.android_clone_element[0], EL_EMPTY, NULL,
749 &li.num_android_clone_elements, 1, MAX_ANDROID_ELEMENTS
754 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
759 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
764 EL_EMC_MAGNIFIER, -1,
765 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
766 &li.magnify_score, 10
769 EL_EMC_MAGNIFIER, -1,
770 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
775 EL_EMC_MAGIC_BALL, -1,
776 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
780 EL_EMC_MAGIC_BALL, -1,
781 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
782 &li.ball_random, FALSE
785 EL_EMC_MAGIC_BALL, -1,
786 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
787 &li.ball_state_initial, FALSE
790 EL_EMC_MAGIC_BALL, -1,
791 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
792 &li.ball_content, EL_EMPTY, NULL,
793 &li.num_ball_contents, 4, MAX_ELEMENT_CONTENTS
796 /* ---------- unused values ----------------------------------------------- */
799 EL_UNKNOWN, SAVE_CONF_NEVER,
800 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
801 &li.score[SC_UNKNOWN_14], 10
804 EL_UNKNOWN, SAVE_CONF_NEVER,
805 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
806 &li.score[SC_UNKNOWN_15], 10
816 static struct LevelFileConfigInfo chunk_config_NOTE[] =
820 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
821 &xx_envelope.xsize, MAX_ENVELOPE_XSIZE,
825 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
826 &xx_envelope.ysize, MAX_ENVELOPE_YSIZE,
831 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
832 &xx_envelope.autowrap, FALSE
836 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
837 &xx_envelope.centered, FALSE
842 TYPE_STRING, CONF_VALUE_BYTES(1),
843 &xx_envelope.text, -1, NULL,
844 &xx_string_length_unused, -1, MAX_ENVELOPE_TEXT_LEN,
845 &xx_default_string_empty[0]
855 static struct LevelFileConfigInfo chunk_config_CUSX_base[] =
859 TYPE_STRING, CONF_VALUE_BYTES(1),
860 &xx_ei.description[0], -1,
861 &yy_ei.description[0],
862 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
863 &xx_default_description[0]
868 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
869 &xx_ei.properties[EP_BITFIELD_BASE_NR], EP_BITMASK_BASE_DEFAULT,
870 &yy_ei.properties[EP_BITFIELD_BASE_NR]
876 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
877 &xx_ei.properties[EP_BITFIELD_BASE_NR + 1], EP_BITMASK_DEFAULT,
878 &yy_ei.properties[EP_BITFIELD_BASE_NR + 1]
884 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
885 &xx_ei.use_gfx_element, FALSE,
886 &yy_ei.use_gfx_element
890 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
891 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE,
892 &yy_ei.gfx_element_initial
897 TYPE_BITFIELD, CONF_VALUE_8_BIT(2),
898 &xx_ei.access_direction, MV_ALL_DIRECTIONS,
899 &yy_ei.access_direction
904 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
905 &xx_ei.collect_score_initial, 10,
906 &yy_ei.collect_score_initial
910 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
911 &xx_ei.collect_count_initial, 1,
912 &yy_ei.collect_count_initial
917 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
918 &xx_ei.ce_value_fixed_initial, 0,
919 &yy_ei.ce_value_fixed_initial
923 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
924 &xx_ei.ce_value_random_initial, 0,
925 &yy_ei.ce_value_random_initial
929 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
930 &xx_ei.use_last_ce_value, FALSE,
931 &yy_ei.use_last_ce_value
936 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
937 &xx_ei.push_delay_fixed, 8,
938 &yy_ei.push_delay_fixed
942 TYPE_INTEGER, CONF_VALUE_16_BIT(7),
943 &xx_ei.push_delay_random, 8,
944 &yy_ei.push_delay_random
948 TYPE_INTEGER, CONF_VALUE_16_BIT(8),
949 &xx_ei.drop_delay_fixed, 0,
950 &yy_ei.drop_delay_fixed
954 TYPE_INTEGER, CONF_VALUE_16_BIT(9),
955 &xx_ei.drop_delay_random, 0,
956 &yy_ei.drop_delay_random
960 TYPE_INTEGER, CONF_VALUE_16_BIT(10),
961 &xx_ei.move_delay_fixed, 0,
962 &yy_ei.move_delay_fixed
966 TYPE_INTEGER, CONF_VALUE_16_BIT(11),
967 &xx_ei.move_delay_random, 0,
968 &yy_ei.move_delay_random
973 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
974 &xx_ei.move_pattern, MV_ALL_DIRECTIONS,
979 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
980 &xx_ei.move_direction_initial, MV_START_AUTOMATIC,
981 &yy_ei.move_direction_initial
985 TYPE_INTEGER, CONF_VALUE_8_BIT(5),
986 &xx_ei.move_stepsize, TILEX / 8,
992 TYPE_ELEMENT, CONF_VALUE_16_BIT(12),
993 &xx_ei.move_enter_element, EL_EMPTY_SPACE,
994 &yy_ei.move_enter_element
998 TYPE_ELEMENT, CONF_VALUE_16_BIT(13),
999 &xx_ei.move_leave_element, EL_EMPTY_SPACE,
1000 &yy_ei.move_leave_element
1004 TYPE_INTEGER, CONF_VALUE_8_BIT(6),
1005 &xx_ei.move_leave_type, LEAVE_TYPE_UNLIMITED,
1006 &yy_ei.move_leave_type
1011 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
1012 &xx_ei.slippery_type, SLIPPERY_ANY_RANDOM,
1013 &yy_ei.slippery_type
1018 TYPE_INTEGER, CONF_VALUE_8_BIT(8),
1019 &xx_ei.explosion_type, EXPLODES_3X3,
1020 &yy_ei.explosion_type
1024 TYPE_INTEGER, CONF_VALUE_16_BIT(14),
1025 &xx_ei.explosion_delay, 16,
1026 &yy_ei.explosion_delay
1030 TYPE_INTEGER, CONF_VALUE_16_BIT(15),
1031 &xx_ei.ignition_delay, 8,
1032 &yy_ei.ignition_delay
1037 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(2),
1038 &xx_ei.content, EL_EMPTY_SPACE,
1040 &xx_num_contents, 1, 1
1043 /* ---------- "num_change_pages" must be the last entry ------------------- */
1046 -1, SAVE_CONF_ALWAYS,
1047 TYPE_INTEGER, CONF_VALUE_8_BIT(9),
1048 &xx_ei.num_change_pages, 1,
1049 &yy_ei.num_change_pages
1060 static struct LevelFileConfigInfo chunk_config_CUSX_change[] =
1062 /* ---------- "current_change_page" must be the first entry --------------- */
1065 -1, SAVE_CONF_ALWAYS,
1066 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1067 &xx_current_change_page, -1
1070 /* ---------- (the remaining entries can be in any order) ----------------- */
1074 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
1075 &xx_change.can_change, FALSE
1080 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
1081 &xx_event_bits[0], 0
1085 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
1086 &xx_event_bits[1], 0
1091 TYPE_BITFIELD, CONF_VALUE_8_BIT(3),
1092 &xx_change.trigger_player, CH_PLAYER_ANY
1096 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
1097 &xx_change.trigger_side, CH_SIDE_ANY
1101 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
1102 &xx_change.trigger_page, CH_PAGE_ANY
1107 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1108 &xx_change.target_element, EL_EMPTY_SPACE
1113 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
1114 &xx_change.delay_fixed, 0
1118 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
1119 &xx_change.delay_random, 0
1123 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
1124 &xx_change.delay_frames, FRAMES_PER_SECOND
1129 TYPE_ELEMENT, CONF_VALUE_16_BIT(5),
1130 &xx_change.initial_trigger_element, EL_EMPTY_SPACE
1135 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
1136 &xx_change.explode, FALSE
1140 TYPE_BOOLEAN, CONF_VALUE_8_BIT(7),
1141 &xx_change.use_target_content, FALSE
1145 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
1146 &xx_change.only_if_complete, FALSE
1150 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1151 &xx_change.use_random_replace, FALSE
1155 TYPE_INTEGER, CONF_VALUE_8_BIT(10),
1156 &xx_change.random_percentage, 100
1160 TYPE_INTEGER, CONF_VALUE_8_BIT(11),
1161 &xx_change.replace_when, CP_WHEN_EMPTY
1166 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1167 &xx_change.has_action, FALSE
1171 TYPE_INTEGER, CONF_VALUE_8_BIT(13),
1172 &xx_change.action_type, CA_NO_ACTION
1176 TYPE_INTEGER, CONF_VALUE_8_BIT(14),
1177 &xx_change.action_mode, CA_MODE_UNDEFINED
1181 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
1182 &xx_change.action_arg, CA_ARG_UNDEFINED
1187 TYPE_ELEMENT, CONF_VALUE_16_BIT(7),
1188 &xx_change.action_element, EL_EMPTY_SPACE
1193 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
1194 &xx_change.target_content, EL_EMPTY_SPACE, NULL,
1195 &xx_num_contents, 1, 1
1205 static struct LevelFileConfigInfo chunk_config_GRPX[] =
1209 TYPE_STRING, CONF_VALUE_BYTES(1),
1210 &xx_ei.description[0], -1, NULL,
1211 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
1212 &xx_default_description[0]
1217 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
1218 &xx_ei.use_gfx_element, FALSE
1222 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1223 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE
1228 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
1229 &xx_group.choice_mode, ANIM_RANDOM
1234 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(2),
1235 &xx_group.element[0], EL_EMPTY_SPACE, NULL,
1236 &xx_group.num_elements, 1, MAX_ELEMENTS_IN_GROUP
1246 static struct LevelFileConfigInfo chunk_config_CONF[] = /* (OBSOLETE) */
1250 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1251 &li.block_snap_field, TRUE
1255 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
1256 &li.continuous_snapping, TRUE
1260 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1261 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
1265 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
1266 &li.use_start_element[0], FALSE
1270 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1271 &li.start_element[0], EL_PLAYER_1
1275 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
1276 &li.use_artwork_element[0], FALSE
1280 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
1281 &li.artwork_element[0], EL_PLAYER_1
1285 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1286 &li.use_explosion_element[0], FALSE
1290 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
1291 &li.explosion_element[0], EL_PLAYER_1
1306 filetype_id_list[] =
1308 { LEVEL_FILE_TYPE_RND, "RND" },
1309 { LEVEL_FILE_TYPE_BD, "BD" },
1310 { LEVEL_FILE_TYPE_EM, "EM" },
1311 { LEVEL_FILE_TYPE_SP, "SP" },
1312 { LEVEL_FILE_TYPE_DX, "DX" },
1313 { LEVEL_FILE_TYPE_SB, "SB" },
1314 { LEVEL_FILE_TYPE_DC, "DC" },
1319 /* ========================================================================= */
1320 /* level file functions */
1321 /* ========================================================================= */
1323 static boolean check_special_flags(char *flag)
1326 printf("::: '%s', '%s', '%s'\n",
1328 options.special_flags,
1329 leveldir_current->special_flags);
1332 if (strEqual(options.special_flags, flag) ||
1333 strEqual(leveldir_current->special_flags, flag))
1339 static struct DateInfo getCurrentDate()
1341 time_t epoch_seconds = time(NULL);
1342 struct tm *now = localtime(&epoch_seconds);
1343 struct DateInfo date;
1345 date.year = now->tm_year + 1900;
1346 date.month = now->tm_mon + 1;
1347 date.day = now->tm_mday;
1349 date.src = DATE_SRC_CLOCK;
1354 static void resetEventFlags(struct ElementChangeInfo *change)
1358 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1359 change->has_event[i] = FALSE;
1362 static void resetEventBits()
1366 for (i = 0; i < NUM_CE_BITFIELDS; i++)
1367 xx_event_bits[i] = 0;
1370 static void setEventFlagsFromEventBits(struct ElementChangeInfo *change)
1374 /* important: only change event flag if corresponding event bit is set
1375 (this is because all xx_event_bits[] values are loaded separately,
1376 and all xx_event_bits[] values are set back to zero before loading
1377 another value xx_event_bits[x] (each value representing 32 flags)) */
1379 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1380 if (xx_event_bits[CH_EVENT_BITFIELD_NR(i)] & CH_EVENT_BIT(i))
1381 change->has_event[i] = TRUE;
1384 static void setEventBitsFromEventFlags(struct ElementChangeInfo *change)
1388 /* in contrast to the above function setEventFlagsFromEventBits(), it
1389 would also be possible to set all bits in xx_event_bits[] to 0 or 1
1390 depending on the corresponding change->has_event[i] values here, as
1391 all xx_event_bits[] values are reset in resetEventBits() before */
1393 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1394 if (change->has_event[i])
1395 xx_event_bits[CH_EVENT_BITFIELD_NR(i)] |= CH_EVENT_BIT(i);
1398 static char *getDefaultElementDescription(struct ElementInfo *ei)
1400 static char description[MAX_ELEMENT_NAME_LEN + 1];
1401 char *default_description = (ei->custom_description != NULL ?
1402 ei->custom_description :
1403 ei->editor_description);
1406 /* always start with reliable default values */
1407 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1408 description[i] = '\0';
1410 /* truncate element description to MAX_ELEMENT_NAME_LEN bytes */
1411 strncpy(description, default_description, MAX_ELEMENT_NAME_LEN);
1413 return &description[0];
1416 static void setElementDescriptionToDefault(struct ElementInfo *ei)
1418 char *default_description = getDefaultElementDescription(ei);
1421 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1422 ei->description[i] = default_description[i];
1425 static void setConfigToDefaultsFromConfigList(struct LevelFileConfigInfo *conf)
1429 for (i = 0; conf[i].data_type != -1; i++)
1431 int default_value = conf[i].default_value;
1432 int data_type = conf[i].data_type;
1433 int conf_type = conf[i].conf_type;
1434 int byte_mask = conf_type & CONF_MASK_BYTES;
1436 if (byte_mask == CONF_MASK_MULTI_BYTES)
1438 int default_num_entities = conf[i].default_num_entities;
1439 int max_num_entities = conf[i].max_num_entities;
1441 *(int *)(conf[i].num_entities) = default_num_entities;
1443 if (data_type == TYPE_STRING)
1445 char *default_string = conf[i].default_string;
1446 char *string = (char *)(conf[i].value);
1448 strncpy(string, default_string, max_num_entities);
1450 else if (data_type == TYPE_ELEMENT_LIST)
1452 int *element_array = (int *)(conf[i].value);
1455 for (j = 0; j < max_num_entities; j++)
1456 element_array[j] = default_value;
1458 else if (data_type == TYPE_CONTENT_LIST)
1460 struct Content *content = (struct Content *)(conf[i].value);
1463 for (c = 0; c < max_num_entities; c++)
1464 for (y = 0; y < 3; y++)
1465 for (x = 0; x < 3; x++)
1466 content[c].e[x][y] = default_value;
1469 else /* constant size configuration data (1, 2 or 4 bytes) */
1471 if (data_type == TYPE_BOOLEAN)
1472 *(boolean *)(conf[i].value) = default_value;
1474 *(int *) (conf[i].value) = default_value;
1479 static void copyConfigFromConfigList(struct LevelFileConfigInfo *conf)
1483 for (i = 0; conf[i].data_type != -1; i++)
1485 int data_type = conf[i].data_type;
1486 int conf_type = conf[i].conf_type;
1487 int byte_mask = conf_type & CONF_MASK_BYTES;
1489 if (byte_mask == CONF_MASK_MULTI_BYTES)
1491 int max_num_entities = conf[i].max_num_entities;
1493 if (data_type == TYPE_STRING)
1495 char *string = (char *)(conf[i].value);
1496 char *string_copy = (char *)(conf[i].value_copy);
1498 strncpy(string_copy, string, max_num_entities);
1500 else if (data_type == TYPE_ELEMENT_LIST)
1502 int *element_array = (int *)(conf[i].value);
1503 int *element_array_copy = (int *)(conf[i].value_copy);
1506 for (j = 0; j < max_num_entities; j++)
1507 element_array_copy[j] = element_array[j];
1509 else if (data_type == TYPE_CONTENT_LIST)
1511 struct Content *content = (struct Content *)(conf[i].value);
1512 struct Content *content_copy = (struct Content *)(conf[i].value_copy);
1515 for (c = 0; c < max_num_entities; c++)
1516 for (y = 0; y < 3; y++)
1517 for (x = 0; x < 3; x++)
1518 content_copy[c].e[x][y] = content[c].e[x][y];
1521 else /* constant size configuration data (1, 2 or 4 bytes) */
1523 if (data_type == TYPE_BOOLEAN)
1524 *(boolean *)(conf[i].value_copy) = *(boolean *)(conf[i].value);
1526 *(int *) (conf[i].value_copy) = *(int *) (conf[i].value);
1531 void copyElementInfo(struct ElementInfo *ei_from, struct ElementInfo *ei_to)
1535 xx_ei = *ei_from; /* copy element data into temporary buffer */
1536 yy_ei = *ei_to; /* copy element data into temporary buffer */
1538 copyConfigFromConfigList(chunk_config_CUSX_base);
1543 /* ---------- reinitialize and copy change pages ---------- */
1545 ei_to->num_change_pages = ei_from->num_change_pages;
1546 ei_to->current_change_page = ei_from->current_change_page;
1548 setElementChangePages(ei_to, ei_to->num_change_pages);
1550 for (i = 0; i < ei_to->num_change_pages; i++)
1551 ei_to->change_page[i] = ei_from->change_page[i];
1553 /* ---------- copy group element info ---------- */
1554 if (ei_from->group != NULL && ei_to->group != NULL) /* group or internal */
1555 *ei_to->group = *ei_from->group;
1557 /* mark this custom element as modified */
1558 ei_to->modified_settings = TRUE;
1561 void setElementChangePages(struct ElementInfo *ei, int change_pages)
1563 int change_page_size = sizeof(struct ElementChangeInfo);
1565 ei->num_change_pages = MAX(1, change_pages);
1568 checked_realloc(ei->change_page, ei->num_change_pages * change_page_size);
1570 if (ei->current_change_page >= ei->num_change_pages)
1571 ei->current_change_page = ei->num_change_pages - 1;
1573 ei->change = &ei->change_page[ei->current_change_page];
1576 void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
1578 xx_change = *change; /* copy change data into temporary buffer */
1581 /* (not needed; set by setConfigToDefaultsFromConfigList()) */
1582 xx_num_contents = 1;
1585 setConfigToDefaultsFromConfigList(chunk_config_CUSX_change);
1587 *change = xx_change;
1589 resetEventFlags(change);
1591 change->direct_action = 0;
1592 change->other_action = 0;
1594 change->pre_change_function = NULL;
1595 change->change_function = NULL;
1596 change->post_change_function = NULL;
1599 static void setLevelInfoToDefaults(struct LevelInfo *level)
1601 static boolean clipboard_elements_initialized = FALSE;
1604 InitElementPropertiesStatic();
1606 li = *level; /* copy level data into temporary buffer */
1608 setConfigToDefaultsFromConfigList(chunk_config_INFO);
1609 setConfigToDefaultsFromConfigList(chunk_config_ELEM);
1611 *level = li; /* copy temporary buffer back to level data */
1613 setLevelInfoToDefaults_EM();
1614 setLevelInfoToDefaults_SP();
1616 level->native_em_level = &native_em_level;
1617 level->native_sp_level = &native_sp_level;
1619 level->file_version = FILE_VERSION_ACTUAL;
1620 level->game_version = GAME_VERSION_ACTUAL;
1622 level->creation_date = getCurrentDate();
1624 level->encoding_16bit_field = TRUE;
1625 level->encoding_16bit_yamyam = TRUE;
1626 level->encoding_16bit_amoeba = TRUE;
1628 for (x = 0; x < MAX_LEV_FIELDX; x++)
1629 for (y = 0; y < MAX_LEV_FIELDY; y++)
1630 level->field[x][y] = EL_SAND;
1632 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
1633 level->name[i] = '\0';
1634 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
1635 level->author[i] = '\0';
1637 strcpy(level->name, NAMELESS_LEVEL_NAME);
1638 strcpy(level->author, ANONYMOUS_NAME);
1640 level->field[0][0] = EL_PLAYER_1;
1641 level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
1643 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1646 struct ElementInfo *ei = &element_info[element];
1648 /* never initialize clipboard elements after the very first time */
1649 /* (to be able to use clipboard elements between several levels) */
1650 if (IS_CLIPBOARD_ELEMENT(element) && clipboard_elements_initialized)
1653 if (IS_ENVELOPE(element))
1655 int envelope_nr = element - EL_ENVELOPE_1;
1657 setConfigToDefaultsFromConfigList(chunk_config_NOTE);
1659 level->envelope[envelope_nr] = xx_envelope;
1662 if (IS_CUSTOM_ELEMENT(element) ||
1663 IS_GROUP_ELEMENT(element) ||
1664 IS_INTERNAL_ELEMENT(element))
1666 xx_ei = *ei; /* copy element data into temporary buffer */
1668 setConfigToDefaultsFromConfigList(chunk_config_CUSX_base);
1673 setElementChangePages(ei, 1);
1674 setElementChangeInfoToDefaults(ei->change);
1676 if (IS_CUSTOM_ELEMENT(element) ||
1677 IS_GROUP_ELEMENT(element) ||
1678 IS_INTERNAL_ELEMENT(element))
1680 setElementDescriptionToDefault(ei);
1682 ei->modified_settings = FALSE;
1685 if (IS_CUSTOM_ELEMENT(element) ||
1686 IS_INTERNAL_ELEMENT(element))
1688 /* internal values used in level editor */
1690 ei->access_type = 0;
1691 ei->access_layer = 0;
1692 ei->access_protected = 0;
1693 ei->walk_to_action = 0;
1694 ei->smash_targets = 0;
1697 ei->can_explode_by_fire = FALSE;
1698 ei->can_explode_smashed = FALSE;
1699 ei->can_explode_impact = FALSE;
1701 ei->current_change_page = 0;
1704 if (IS_GROUP_ELEMENT(element) ||
1705 IS_INTERNAL_ELEMENT(element))
1707 struct ElementGroupInfo *group;
1709 /* initialize memory for list of elements in group */
1710 if (ei->group == NULL)
1711 ei->group = checked_malloc(sizeof(struct ElementGroupInfo));
1715 xx_group = *group; /* copy group data into temporary buffer */
1717 setConfigToDefaultsFromConfigList(chunk_config_GRPX);
1723 clipboard_elements_initialized = TRUE;
1725 BorderElement = EL_STEELWALL;
1727 level->no_valid_file = FALSE;
1729 level->changed = FALSE;
1731 /* set all bug compatibility flags to "false" => do not emulate this bug */
1732 level->use_action_after_change_bug = FALSE;
1734 if (leveldir_current == NULL) /* only when dumping level */
1737 /* try to determine better author name than 'anonymous' */
1738 if (!strEqual(leveldir_current->author, ANONYMOUS_NAME))
1740 strncpy(level->author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
1741 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1745 switch (LEVELCLASS(leveldir_current))
1747 case LEVELCLASS_TUTORIAL:
1748 strcpy(level->author, PROGRAM_AUTHOR_STRING);
1751 case LEVELCLASS_CONTRIB:
1752 strncpy(level->author, leveldir_current->name, MAX_LEVEL_AUTHOR_LEN);
1753 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1756 case LEVELCLASS_PRIVATE:
1757 strncpy(level->author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
1758 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1762 /* keep default value */
1768 static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
1770 level_file_info->nr = 0;
1771 level_file_info->type = LEVEL_FILE_TYPE_UNKNOWN;
1772 level_file_info->packed = FALSE;
1773 level_file_info->basename = NULL;
1774 level_file_info->filename = NULL;
1777 static void ActivateLevelTemplate()
1781 /* Currently there is no special action needed to activate the template
1782 data, because 'element_info' property settings overwrite the original
1783 level data, while all other variables do not change. */
1785 /* Exception: 'from_level_template' elements in the original level playfield
1786 are overwritten with the corresponding elements at the same position in
1787 playfield from the level template. */
1789 for (x = 0; x < level.fieldx; x++)
1790 for (y = 0; y < level.fieldy; y++)
1791 if (level.field[x][y] == EL_FROM_LEVEL_TEMPLATE)
1792 level.field[x][y] = level_template.field[x][y];
1794 if (check_special_flags("load_xsb_to_ces"))
1796 struct LevelInfo level_backup = level;
1798 /* overwrite all individual level settings from template level settings */
1799 level = level_template;
1801 /* restore playfield size */
1802 level.fieldx = level_backup.fieldx;
1803 level.fieldy = level_backup.fieldy;
1805 /* restore playfield content */
1806 for (x = 0; x < level.fieldx; x++)
1807 for (y = 0; y < level.fieldy; y++)
1808 level.field[x][y] = level_backup.field[x][y];
1810 /* restore name and author from individual level */
1811 strcpy(level.name, level_backup.name);
1812 strcpy(level.author, level_backup.author);
1814 /* restore flag "use_custom_template" */
1815 level.use_custom_template = level_backup.use_custom_template;
1819 static char *getLevelFilenameFromBasename(char *basename)
1821 static char *filename = NULL;
1823 checked_free(filename);
1825 filename = getPath2(getCurrentLevelDir(), basename);
1830 static int getFileTypeFromBasename(char *basename)
1832 /* !!! ALSO SEE COMMENT IN checkForPackageFromBasename() !!! */
1834 static char *filename = NULL;
1835 struct stat file_status;
1837 /* ---------- try to determine file type from filename ---------- */
1839 /* check for typical filename of a Supaplex level package file */
1841 if (strlen(basename) == 10 && strPrefixLower(basename, "levels.d"))
1842 return LEVEL_FILE_TYPE_SP;
1844 if (strlen(basename) == 10 && (strncmp(basename, "levels.d", 8) == 0 ||
1845 strncmp(basename, "LEVELS.D", 8) == 0))
1846 return LEVEL_FILE_TYPE_SP;
1849 /* check for typical filename of a Diamond Caves II level package file */
1850 if (strSuffixLower(basename, ".dc") ||
1851 strSuffixLower(basename, ".dc2"))
1852 return LEVEL_FILE_TYPE_DC;
1854 /* check for typical filename of a Sokoban level package file */
1855 if (strSuffixLower(basename, ".xsb") &&
1856 strchr(basename, '%') == NULL)
1857 return LEVEL_FILE_TYPE_SB;
1859 /* ---------- try to determine file type from filesize ---------- */
1861 checked_free(filename);
1862 filename = getPath2(getCurrentLevelDir(), basename);
1864 if (stat(filename, &file_status) == 0)
1866 /* check for typical filesize of a Supaplex level package file */
1867 if (file_status.st_size == 170496)
1868 return LEVEL_FILE_TYPE_SP;
1871 return LEVEL_FILE_TYPE_UNKNOWN;
1874 static boolean checkForPackageFromBasename(char *basename)
1876 /* !!! WON'T WORK ANYMORE IF getFileTypeFromBasename() ALSO DETECTS !!!
1877 !!! SINGLE LEVELS (CURRENTLY ONLY DETECTS LEVEL PACKAGES !!! */
1879 return (getFileTypeFromBasename(basename) != LEVEL_FILE_TYPE_UNKNOWN);
1882 static char *getSingleLevelBasenameExt(int nr, char *extension)
1884 static char basename[MAX_FILENAME_LEN];
1887 sprintf(basename, "template.%s", extension);
1889 sprintf(basename, "%03d.%s", nr, extension);
1894 static char *getSingleLevelBasename(int nr)
1896 return getSingleLevelBasenameExt(nr, LEVELFILE_EXTENSION);
1899 static char *getPackedLevelBasename(int type)
1901 static char basename[MAX_FILENAME_LEN];
1902 char *directory = getCurrentLevelDir();
1904 struct dirent *dir_entry;
1906 strcpy(basename, UNDEFINED_FILENAME); /* default: undefined file */
1908 if ((dir = opendir(directory)) == NULL)
1910 Error(ERR_WARN, "cannot read current level directory '%s'", directory);
1915 while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */
1917 char *entry_basename = dir_entry->d_name;
1918 int entry_type = getFileTypeFromBasename(entry_basename);
1920 if (entry_type != LEVEL_FILE_TYPE_UNKNOWN) /* found valid level package */
1922 if (type == LEVEL_FILE_TYPE_UNKNOWN ||
1925 strcpy(basename, entry_basename);
1937 static char *getSingleLevelFilename(int nr)
1939 return getLevelFilenameFromBasename(getSingleLevelBasename(nr));
1943 static char *getPackedLevelFilename(int type)
1945 return getLevelFilenameFromBasename(getPackedLevelBasename(type));
1949 char *getDefaultLevelFilename(int nr)
1951 return getSingleLevelFilename(nr);
1955 static void setLevelFileInfo_SingleLevelFilename(struct LevelFileInfo *lfi,
1959 lfi->packed = FALSE;
1960 lfi->basename = getSingleLevelBasename(lfi->nr, lfi->type);
1961 lfi->filename = getLevelFilenameFromBasename(lfi->basename);
1965 static void setLevelFileInfo_FormatLevelFilename(struct LevelFileInfo *lfi,
1966 int type, char *format, ...)
1968 static char basename[MAX_FILENAME_LEN];
1971 va_start(ap, format);
1972 vsprintf(basename, format, ap);
1976 lfi->packed = FALSE;
1977 lfi->basename = basename;
1978 lfi->filename = getLevelFilenameFromBasename(lfi->basename);
1981 static void setLevelFileInfo_PackedLevelFilename(struct LevelFileInfo *lfi,
1986 lfi->basename = getPackedLevelBasename(lfi->type);
1987 lfi->filename = getLevelFilenameFromBasename(lfi->basename);
1990 static int getFiletypeFromID(char *filetype_id)
1992 char *filetype_id_lower;
1993 int filetype = LEVEL_FILE_TYPE_UNKNOWN;
1996 if (filetype_id == NULL)
1997 return LEVEL_FILE_TYPE_UNKNOWN;
1999 filetype_id_lower = getStringToLower(filetype_id);
2001 for (i = 0; filetype_id_list[i].id != NULL; i++)
2003 char *id_lower = getStringToLower(filetype_id_list[i].id);
2005 if (strEqual(filetype_id_lower, id_lower))
2006 filetype = filetype_id_list[i].filetype;
2010 if (filetype != LEVEL_FILE_TYPE_UNKNOWN)
2014 free(filetype_id_lower);
2019 static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
2023 /* special case: level number is negative => check for level template file */
2027 /* global variable "leveldir_current" must be modified in the loop below */
2028 LevelDirTree *leveldir_current_last = leveldir_current;
2030 /* check for template level in path from current to topmost tree node */
2032 while (leveldir_current != NULL)
2034 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2035 "template.%s", LEVELFILE_EXTENSION);
2037 if (fileExists(lfi->filename))
2040 leveldir_current = leveldir_current->node_parent;
2043 /* restore global variable "leveldir_current" modified in above loop */
2044 leveldir_current = leveldir_current_last;
2048 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2049 "template.%s", LEVELFILE_EXTENSION);
2053 /* no fallback if template file not existing */
2057 /* special case: check for file name/pattern specified in "levelinfo.conf" */
2058 if (leveldir_current->level_filename != NULL)
2060 int filetype = getFiletypeFromID(leveldir_current->level_filetype);
2062 setLevelFileInfo_FormatLevelFilename(lfi, filetype,
2063 leveldir_current->level_filename, nr);
2065 lfi->packed = checkForPackageFromBasename(leveldir_current->level_filename);
2067 if (fileExists(lfi->filename))
2071 /* check for native Rocks'n'Diamonds level file */
2072 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2073 "%03d.%s", nr, LEVELFILE_EXTENSION);
2074 if (fileExists(lfi->filename))
2077 /* check for Emerald Mine level file (V1) */
2078 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "a%c%c",
2079 'a' + (nr / 10) % 26, '0' + nr % 10);
2080 if (fileExists(lfi->filename))
2082 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "A%c%c",
2083 'A' + (nr / 10) % 26, '0' + nr % 10);
2084 if (fileExists(lfi->filename))
2087 /* check for Emerald Mine level file (V2 to V5) */
2088 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%d", nr);
2089 if (fileExists(lfi->filename))
2092 /* check for Emerald Mine level file (V6 / single mode) */
2093 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02ds", nr);
2094 if (fileExists(lfi->filename))
2096 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dS", nr);
2097 if (fileExists(lfi->filename))
2100 /* check for Emerald Mine level file (V6 / teamwork mode) */
2101 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dt", nr);
2102 if (fileExists(lfi->filename))
2104 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dT", nr);
2105 if (fileExists(lfi->filename))
2108 /* check for various packed level file formats */
2109 setLevelFileInfo_PackedLevelFilename(lfi, LEVEL_FILE_TYPE_UNKNOWN);
2110 if (fileExists(lfi->filename))
2113 /* no known level file found -- use default values (and fail later) */
2114 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2115 "%03d.%s", nr, LEVELFILE_EXTENSION);
2118 static void determineLevelFileInfo_Filetype(struct LevelFileInfo *lfi)
2120 if (lfi->type == LEVEL_FILE_TYPE_UNKNOWN)
2121 lfi->type = getFileTypeFromBasename(lfi->basename);
2124 static void setLevelFileInfo(struct LevelFileInfo *level_file_info, int nr)
2126 /* always start with reliable default values */
2127 setFileInfoToDefaults(level_file_info);
2129 level_file_info->nr = nr; /* set requested level number */
2131 determineLevelFileInfo_Filename(level_file_info);
2132 determineLevelFileInfo_Filetype(level_file_info);
2135 /* ------------------------------------------------------------------------- */
2136 /* functions for loading R'n'D level */
2137 /* ------------------------------------------------------------------------- */
2139 int getMappedElement(int element)
2141 /* remap some (historic, now obsolete) elements */
2145 case EL_PLAYER_OBSOLETE:
2146 element = EL_PLAYER_1;
2149 case EL_KEY_OBSOLETE:
2153 case EL_EM_KEY_1_FILE_OBSOLETE:
2154 element = EL_EM_KEY_1;
2157 case EL_EM_KEY_2_FILE_OBSOLETE:
2158 element = EL_EM_KEY_2;
2161 case EL_EM_KEY_3_FILE_OBSOLETE:
2162 element = EL_EM_KEY_3;
2165 case EL_EM_KEY_4_FILE_OBSOLETE:
2166 element = EL_EM_KEY_4;
2169 case EL_ENVELOPE_OBSOLETE:
2170 element = EL_ENVELOPE_1;
2178 if (element >= NUM_FILE_ELEMENTS)
2180 Error(ERR_WARN, "invalid level element %d", element);
2182 element = EL_UNKNOWN;
2190 int getMappedElementByVersion(int element, int game_version)
2192 /* remap some elements due to certain game version */
2194 if (game_version <= VERSION_IDENT(2,2,0,0))
2196 /* map game font elements */
2197 element = (element == EL_CHAR('[') ? EL_CHAR_AUMLAUT :
2198 element == EL_CHAR('\\') ? EL_CHAR_OUMLAUT :
2199 element == EL_CHAR(']') ? EL_CHAR_UUMLAUT :
2200 element == EL_CHAR('^') ? EL_CHAR_COPYRIGHT : element);
2203 if (game_version < VERSION_IDENT(3,0,0,0))
2205 /* map Supaplex gravity tube elements */
2206 element = (element == EL_SP_GRAVITY_PORT_LEFT ? EL_SP_PORT_LEFT :
2207 element == EL_SP_GRAVITY_PORT_RIGHT ? EL_SP_PORT_RIGHT :
2208 element == EL_SP_GRAVITY_PORT_UP ? EL_SP_PORT_UP :
2209 element == EL_SP_GRAVITY_PORT_DOWN ? EL_SP_PORT_DOWN :
2216 static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level)
2218 level->file_version = getFileVersion(file);
2219 level->game_version = getFileVersion(file);
2224 static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level)
2226 level->creation_date.year = getFile16BitBE(file);
2227 level->creation_date.month = getFile8Bit(file);
2228 level->creation_date.day = getFile8Bit(file);
2230 level->creation_date.src = DATE_SRC_LEVELFILE;
2235 static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level)
2237 int initial_player_stepsize;
2238 int initial_player_gravity;
2241 level->fieldx = getFile8Bit(file);
2242 level->fieldy = getFile8Bit(file);
2244 level->time = getFile16BitBE(file);
2245 level->gems_needed = getFile16BitBE(file);
2247 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2248 level->name[i] = getFile8Bit(file);
2249 level->name[MAX_LEVEL_NAME_LEN] = 0;
2251 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
2252 level->score[i] = getFile8Bit(file);
2254 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2255 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
2256 for (y = 0; y < 3; y++)
2257 for (x = 0; x < 3; x++)
2258 level->yamyam_content[i].e[x][y] = getMappedElement(getFile8Bit(file));
2260 level->amoeba_speed = getFile8Bit(file);
2261 level->time_magic_wall = getFile8Bit(file);
2262 level->time_wheel = getFile8Bit(file);
2263 level->amoeba_content = getMappedElement(getFile8Bit(file));
2265 initial_player_stepsize = (getFile8Bit(file) == 1 ? STEPSIZE_FAST :
2268 for (i = 0; i < MAX_PLAYERS; i++)
2269 level->initial_player_stepsize[i] = initial_player_stepsize;
2271 initial_player_gravity = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2273 for (i = 0; i < MAX_PLAYERS; i++)
2274 level->initial_player_gravity[i] = initial_player_gravity;
2276 level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2277 level->em_slippery_gems = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2279 level->use_custom_template = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2281 level->block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2282 level->sp_block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2283 level->can_move_into_acid_bits = getFile32BitBE(file);
2284 level->dont_collide_with_bits = getFile8Bit(file);
2286 level->use_spring_bug = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2287 level->use_step_counter = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2289 level->instant_relocation = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2290 level->can_pass_to_walkable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2291 level->grow_into_diggable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2293 level->game_engine_type = getFile8Bit(file);
2295 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_HEAD_UNUSED);
2300 static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level)
2304 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2305 level->name[i] = getFile8Bit(file);
2306 level->name[MAX_LEVEL_NAME_LEN] = 0;
2311 static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level)
2315 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
2316 level->author[i] = getFile8Bit(file);
2317 level->author[MAX_LEVEL_AUTHOR_LEN] = 0;
2322 static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level)
2325 int chunk_size_expected = level->fieldx * level->fieldy;
2327 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2328 stored with 16-bit encoding (and should be twice as big then).
2329 Even worse, playfield data was stored 16-bit when only yamyam content
2330 contained 16-bit elements and vice versa. */
2332 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2333 chunk_size_expected *= 2;
2335 if (chunk_size_expected != chunk_size)
2337 ReadUnusedBytesFromFile(file, chunk_size);
2338 return chunk_size_expected;
2341 for (y = 0; y < level->fieldy; y++)
2342 for (x = 0; x < level->fieldx; x++)
2343 level->field[x][y] =
2344 getMappedElement(level->encoding_16bit_field ? getFile16BitBE(file) :
2349 static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level)
2352 int header_size = 4;
2353 int content_size = MAX_ELEMENT_CONTENTS * 3 * 3;
2354 int chunk_size_expected = header_size + content_size;
2356 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2357 stored with 16-bit encoding (and should be twice as big then).
2358 Even worse, playfield data was stored 16-bit when only yamyam content
2359 contained 16-bit elements and vice versa. */
2361 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2362 chunk_size_expected += content_size;
2364 if (chunk_size_expected != chunk_size)
2366 ReadUnusedBytesFromFile(file, chunk_size);
2367 return chunk_size_expected;
2371 level->num_yamyam_contents = getFile8Bit(file);
2375 /* correct invalid number of content fields -- should never happen */
2376 if (level->num_yamyam_contents < 1 ||
2377 level->num_yamyam_contents > MAX_ELEMENT_CONTENTS)
2378 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2380 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2381 for (y = 0; y < 3; y++)
2382 for (x = 0; x < 3; x++)
2383 level->yamyam_content[i].e[x][y] =
2384 getMappedElement(level->encoding_16bit_field ?
2385 getFile16BitBE(file) : getFile8Bit(file));
2389 static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level)
2393 int num_contents, content_xsize, content_ysize;
2394 int content_array[MAX_ELEMENT_CONTENTS][3][3];
2396 element = getMappedElement(getFile16BitBE(file));
2397 num_contents = getFile8Bit(file);
2398 content_xsize = getFile8Bit(file);
2399 content_ysize = getFile8Bit(file);
2401 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT2_UNUSED);
2403 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2404 for (y = 0; y < 3; y++)
2405 for (x = 0; x < 3; x++)
2406 content_array[i][x][y] = getMappedElement(getFile16BitBE(file));
2408 /* correct invalid number of content fields -- should never happen */
2409 if (num_contents < 1 || num_contents > MAX_ELEMENT_CONTENTS)
2410 num_contents = STD_ELEMENT_CONTENTS;
2412 if (element == EL_YAMYAM)
2414 level->num_yamyam_contents = num_contents;
2416 for (i = 0; i < num_contents; i++)
2417 for (y = 0; y < 3; y++)
2418 for (x = 0; x < 3; x++)
2419 level->yamyam_content[i].e[x][y] = content_array[i][x][y];
2421 else if (element == EL_BD_AMOEBA)
2423 level->amoeba_content = content_array[0][0][0];
2427 Error(ERR_WARN, "cannot load content for element '%d'", element);
2433 static int LoadLevel_CNT3(FILE *file, int chunk_size, struct LevelInfo *level)
2439 int chunk_size_expected;
2441 element = getMappedElement(getFile16BitBE(file));
2442 if (!IS_ENVELOPE(element))
2443 element = EL_ENVELOPE_1;
2445 envelope_nr = element - EL_ENVELOPE_1;
2447 envelope_len = getFile16BitBE(file);
2449 level->envelope[envelope_nr].xsize = getFile8Bit(file);
2450 level->envelope[envelope_nr].ysize = getFile8Bit(file);
2452 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT3_UNUSED);
2454 chunk_size_expected = LEVEL_CHUNK_CNT3_SIZE(envelope_len);
2455 if (chunk_size_expected != chunk_size)
2457 ReadUnusedBytesFromFile(file, chunk_size - LEVEL_CHUNK_CNT3_HEADER);
2458 return chunk_size_expected;
2461 for (i = 0; i < envelope_len; i++)
2462 level->envelope[envelope_nr].text[i] = getFile8Bit(file);
2467 static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level)
2469 int num_changed_custom_elements = getFile16BitBE(file);
2470 int chunk_size_expected = 2 + num_changed_custom_elements * 6;
2473 if (chunk_size_expected != chunk_size)
2475 ReadUnusedBytesFromFile(file, chunk_size - 2);
2476 return chunk_size_expected;
2479 for (i = 0; i < num_changed_custom_elements; i++)
2481 int element = getMappedElement(getFile16BitBE(file));
2482 int properties = getFile32BitBE(file);
2484 if (IS_CUSTOM_ELEMENT(element))
2485 element_info[element].properties[EP_BITFIELD_BASE_NR] = properties;
2487 Error(ERR_WARN, "invalid custom element number %d", element);
2489 /* older game versions that wrote level files with CUS1 chunks used
2490 different default push delay values (not yet stored in level file) */
2491 element_info[element].push_delay_fixed = 2;
2492 element_info[element].push_delay_random = 8;
2498 static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level)
2500 int num_changed_custom_elements = getFile16BitBE(file);
2501 int chunk_size_expected = 2 + num_changed_custom_elements * 4;
2504 if (chunk_size_expected != chunk_size)
2506 ReadUnusedBytesFromFile(file, chunk_size - 2);
2507 return chunk_size_expected;
2510 for (i = 0; i < num_changed_custom_elements; i++)
2512 int element = getMappedElement(getFile16BitBE(file));
2513 int custom_target_element = getMappedElement(getFile16BitBE(file));
2515 if (IS_CUSTOM_ELEMENT(element))
2516 element_info[element].change->target_element = custom_target_element;
2518 Error(ERR_WARN, "invalid custom element number %d", element);
2524 static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
2526 int num_changed_custom_elements = getFile16BitBE(file);
2527 int chunk_size_expected = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
2530 if (chunk_size_expected != chunk_size)
2532 ReadUnusedBytesFromFile(file, chunk_size - 2);
2533 return chunk_size_expected;
2536 for (i = 0; i < num_changed_custom_elements; i++)
2538 int element = getMappedElement(getFile16BitBE(file));
2539 struct ElementInfo *ei = &element_info[element];
2540 unsigned int event_bits;
2542 if (!IS_CUSTOM_ELEMENT(element))
2544 Error(ERR_WARN, "invalid custom element number %d", element);
2546 element = EL_INTERNAL_DUMMY;
2549 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
2550 ei->description[j] = getFile8Bit(file);
2551 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2553 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2555 /* some free bytes for future properties and padding */
2556 ReadUnusedBytesFromFile(file, 7);
2558 ei->use_gfx_element = getFile8Bit(file);
2559 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2561 ei->collect_score_initial = getFile8Bit(file);
2562 ei->collect_count_initial = getFile8Bit(file);
2564 ei->push_delay_fixed = getFile16BitBE(file);
2565 ei->push_delay_random = getFile16BitBE(file);
2566 ei->move_delay_fixed = getFile16BitBE(file);
2567 ei->move_delay_random = getFile16BitBE(file);
2569 ei->move_pattern = getFile16BitBE(file);
2570 ei->move_direction_initial = getFile8Bit(file);
2571 ei->move_stepsize = getFile8Bit(file);
2573 for (y = 0; y < 3; y++)
2574 for (x = 0; x < 3; x++)
2575 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2577 event_bits = getFile32BitBE(file);
2578 for (j = 0; j < NUM_CHANGE_EVENTS; j++)
2579 if (event_bits & (1 << j))
2580 ei->change->has_event[j] = TRUE;
2582 ei->change->target_element = getMappedElement(getFile16BitBE(file));
2584 ei->change->delay_fixed = getFile16BitBE(file);
2585 ei->change->delay_random = getFile16BitBE(file);
2586 ei->change->delay_frames = getFile16BitBE(file);
2588 ei->change->initial_trigger_element= getMappedElement(getFile16BitBE(file));
2590 ei->change->explode = getFile8Bit(file);
2591 ei->change->use_target_content = getFile8Bit(file);
2592 ei->change->only_if_complete = getFile8Bit(file);
2593 ei->change->use_random_replace = getFile8Bit(file);
2595 ei->change->random_percentage = getFile8Bit(file);
2596 ei->change->replace_when = getFile8Bit(file);
2598 for (y = 0; y < 3; y++)
2599 for (x = 0; x < 3; x++)
2600 ei->change->target_content.e[x][y] =
2601 getMappedElement(getFile16BitBE(file));
2603 ei->slippery_type = getFile8Bit(file);
2605 /* some free bytes for future properties and padding */
2606 ReadUnusedBytesFromFile(file, LEVEL_CPART_CUS3_UNUSED);
2608 /* mark that this custom element has been modified */
2609 ei->modified_settings = TRUE;
2615 static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level)
2617 struct ElementInfo *ei;
2618 int chunk_size_expected;
2622 /* ---------- custom element base property values (96 bytes) ------------- */
2624 element = getMappedElement(getFile16BitBE(file));
2626 if (!IS_CUSTOM_ELEMENT(element))
2628 Error(ERR_WARN, "invalid custom element number %d", element);
2630 ReadUnusedBytesFromFile(file, chunk_size - 2);
2634 ei = &element_info[element];
2636 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
2637 ei->description[i] = getFile8Bit(file);
2638 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2640 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2642 ReadUnusedBytesFromFile(file, 4); /* reserved for more base properties */
2644 ei->num_change_pages = getFile8Bit(file);
2646 chunk_size_expected = LEVEL_CHUNK_CUS4_SIZE(ei->num_change_pages);
2647 if (chunk_size_expected != chunk_size)
2649 ReadUnusedBytesFromFile(file, chunk_size - 43);
2650 return chunk_size_expected;
2653 ei->ce_value_fixed_initial = getFile16BitBE(file);
2654 ei->ce_value_random_initial = getFile16BitBE(file);
2655 ei->use_last_ce_value = getFile8Bit(file);
2657 ei->use_gfx_element = getFile8Bit(file);
2658 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2660 ei->collect_score_initial = getFile8Bit(file);
2661 ei->collect_count_initial = getFile8Bit(file);
2663 ei->drop_delay_fixed = getFile8Bit(file);
2664 ei->push_delay_fixed = getFile8Bit(file);
2665 ei->drop_delay_random = getFile8Bit(file);
2666 ei->push_delay_random = getFile8Bit(file);
2667 ei->move_delay_fixed = getFile16BitBE(file);
2668 ei->move_delay_random = getFile16BitBE(file);
2670 /* bits 0 - 15 of "move_pattern" ... */
2671 ei->move_pattern = getFile16BitBE(file);
2672 ei->move_direction_initial = getFile8Bit(file);
2673 ei->move_stepsize = getFile8Bit(file);
2675 ei->slippery_type = getFile8Bit(file);
2677 for (y = 0; y < 3; y++)
2678 for (x = 0; x < 3; x++)
2679 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2681 ei->move_enter_element = getMappedElement(getFile16BitBE(file));
2682 ei->move_leave_element = getMappedElement(getFile16BitBE(file));
2683 ei->move_leave_type = getFile8Bit(file);
2685 /* ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible) */
2686 ei->move_pattern |= (getFile16BitBE(file) << 16);
2688 ei->access_direction = getFile8Bit(file);
2690 ei->explosion_delay = getFile8Bit(file);
2691 ei->ignition_delay = getFile8Bit(file);
2692 ei->explosion_type = getFile8Bit(file);
2694 /* some free bytes for future custom property values and padding */
2695 ReadUnusedBytesFromFile(file, 1);
2697 /* ---------- change page property values (48 bytes) --------------------- */
2699 setElementChangePages(ei, ei->num_change_pages);
2701 for (i = 0; i < ei->num_change_pages; i++)
2703 struct ElementChangeInfo *change = &ei->change_page[i];
2704 unsigned int event_bits;
2706 /* always start with reliable default values */
2707 setElementChangeInfoToDefaults(change);
2709 /* bits 0 - 31 of "has_event[]" ... */
2710 event_bits = getFile32BitBE(file);
2711 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
2712 if (event_bits & (1 << j))
2713 change->has_event[j] = TRUE;
2715 change->target_element = getMappedElement(getFile16BitBE(file));
2717 change->delay_fixed = getFile16BitBE(file);
2718 change->delay_random = getFile16BitBE(file);
2719 change->delay_frames = getFile16BitBE(file);
2721 change->initial_trigger_element = getMappedElement(getFile16BitBE(file));
2723 change->explode = getFile8Bit(file);
2724 change->use_target_content = getFile8Bit(file);
2725 change->only_if_complete = getFile8Bit(file);
2726 change->use_random_replace = getFile8Bit(file);
2728 change->random_percentage = getFile8Bit(file);
2729 change->replace_when = getFile8Bit(file);
2731 for (y = 0; y < 3; y++)
2732 for (x = 0; x < 3; x++)
2733 change->target_content.e[x][y]= getMappedElement(getFile16BitBE(file));
2735 change->can_change = getFile8Bit(file);
2737 change->trigger_side = getFile8Bit(file);
2739 change->trigger_player = getFile8Bit(file);
2740 change->trigger_page = getFile8Bit(file);
2742 change->trigger_page = (change->trigger_page == CH_PAGE_ANY_FILE ?
2743 CH_PAGE_ANY : (1 << change->trigger_page));
2745 change->has_action = getFile8Bit(file);
2746 change->action_type = getFile8Bit(file);
2747 change->action_mode = getFile8Bit(file);
2748 change->action_arg = getFile16BitBE(file);
2750 /* ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible) */
2751 event_bits = getFile8Bit(file);
2752 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
2753 if (event_bits & (1 << (j - 32)))
2754 change->has_event[j] = TRUE;
2757 /* mark this custom element as modified */
2758 ei->modified_settings = TRUE;
2763 static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level)
2765 struct ElementInfo *ei;
2766 struct ElementGroupInfo *group;
2770 element = getMappedElement(getFile16BitBE(file));
2772 if (!IS_GROUP_ELEMENT(element))
2774 Error(ERR_WARN, "invalid group element number %d", element);
2776 ReadUnusedBytesFromFile(file, chunk_size - 2);
2780 ei = &element_info[element];
2782 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
2783 ei->description[i] = getFile8Bit(file);
2784 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2786 group = element_info[element].group;
2788 group->num_elements = getFile8Bit(file);
2790 ei->use_gfx_element = getFile8Bit(file);
2791 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2793 group->choice_mode = getFile8Bit(file);
2795 /* some free bytes for future values and padding */
2796 ReadUnusedBytesFromFile(file, 3);
2798 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
2799 group->element[i] = getMappedElement(getFile16BitBE(file));
2801 /* mark this group element as modified */
2802 element_info[element].modified_settings = TRUE;
2807 static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf,
2808 int element, int real_element)
2810 int micro_chunk_size = 0;
2811 int conf_type = getFile8Bit(file);
2812 int byte_mask = conf_type & CONF_MASK_BYTES;
2813 boolean element_found = FALSE;
2816 micro_chunk_size += 1;
2818 if (byte_mask == CONF_MASK_MULTI_BYTES)
2820 int num_bytes = getFile16BitBE(file);
2821 byte *buffer = checked_malloc(num_bytes);
2823 ReadBytesFromFile(file, buffer, num_bytes);
2825 for (i = 0; conf[i].data_type != -1; i++)
2827 if (conf[i].element == element &&
2828 conf[i].conf_type == conf_type)
2830 int data_type = conf[i].data_type;
2831 int num_entities = num_bytes / CONF_ENTITY_NUM_BYTES(data_type);
2832 int max_num_entities = conf[i].max_num_entities;
2834 if (num_entities > max_num_entities)
2837 "truncating number of entities for element %d from %d to %d",
2838 element, num_entities, max_num_entities);
2840 num_entities = max_num_entities;
2843 if (num_entities == 0 && (data_type == TYPE_ELEMENT_LIST ||
2844 data_type == TYPE_CONTENT_LIST))
2846 /* for element and content lists, zero entities are not allowed */
2847 Error(ERR_WARN, "found empty list of entities for element %d",
2850 /* do not set "num_entities" here to prevent reading behind buffer */
2852 *(int *)(conf[i].num_entities) = 1; /* at least one is required */
2856 *(int *)(conf[i].num_entities) = num_entities;
2859 element_found = TRUE;
2861 if (data_type == TYPE_STRING)
2863 char *string = (char *)(conf[i].value);
2866 for (j = 0; j < max_num_entities; j++)
2867 string[j] = (j < num_entities ? buffer[j] : '\0');
2869 else if (data_type == TYPE_ELEMENT_LIST)
2871 int *element_array = (int *)(conf[i].value);
2874 for (j = 0; j < num_entities; j++)
2876 getMappedElement(CONF_ELEMENTS_ELEMENT(buffer, j));
2878 else if (data_type == TYPE_CONTENT_LIST)
2880 struct Content *content= (struct Content *)(conf[i].value);
2883 for (c = 0; c < num_entities; c++)
2884 for (y = 0; y < 3; y++)
2885 for (x = 0; x < 3; x++)
2886 content[c].e[x][y] =
2887 getMappedElement(CONF_CONTENTS_ELEMENT(buffer, c, x, y));
2890 element_found = FALSE;
2896 checked_free(buffer);
2898 micro_chunk_size += 2 + num_bytes;
2900 else /* constant size configuration data (1, 2 or 4 bytes) */
2902 int value = (byte_mask == CONF_MASK_1_BYTE ? getFile8Bit (file) :
2903 byte_mask == CONF_MASK_2_BYTE ? getFile16BitBE(file) :
2904 byte_mask == CONF_MASK_4_BYTE ? getFile32BitBE(file) : 0);
2906 for (i = 0; conf[i].data_type != -1; i++)
2908 if (conf[i].element == element &&
2909 conf[i].conf_type == conf_type)
2911 int data_type = conf[i].data_type;
2913 if (data_type == TYPE_ELEMENT)
2914 value = getMappedElement(value);
2916 if (data_type == TYPE_BOOLEAN)
2917 *(boolean *)(conf[i].value) = value;
2919 *(int *) (conf[i].value) = value;
2921 element_found = TRUE;
2927 micro_chunk_size += CONF_VALUE_NUM_BYTES(byte_mask);
2932 char *error_conf_chunk_bytes =
2933 (byte_mask == CONF_MASK_1_BYTE ? "CONF_VALUE_8_BIT" :
2934 byte_mask == CONF_MASK_2_BYTE ? "CONF_VALUE_16_BIT" :
2935 byte_mask == CONF_MASK_4_BYTE ? "CONF_VALUE_32_BIT" :"CONF_VALUE_BYTES");
2936 int error_conf_chunk_token = conf_type & CONF_MASK_TOKEN;
2937 int error_element = real_element;
2939 Error(ERR_WARN, "cannot load micro chunk '%s(%d)' value for element %d ['%s']",
2940 error_conf_chunk_bytes, error_conf_chunk_token,
2941 error_element, EL_NAME(error_element));
2944 return micro_chunk_size;
2947 static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level)
2949 int real_chunk_size = 0;
2951 li = *level; /* copy level data into temporary buffer */
2955 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1);
2957 if (real_chunk_size >= chunk_size)
2961 *level = li; /* copy temporary buffer back to level data */
2963 return real_chunk_size;
2966 static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level)
2968 int real_chunk_size = 0;
2970 li = *level; /* copy level data into temporary buffer */
2974 int element = getMappedElement(getFile16BitBE(file));
2976 real_chunk_size += 2;
2977 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CONF,
2979 if (real_chunk_size >= chunk_size)
2983 *level = li; /* copy temporary buffer back to level data */
2985 return real_chunk_size;
2988 static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level)
2990 int real_chunk_size = 0;
2992 li = *level; /* copy level data into temporary buffer */
2996 int element = getMappedElement(getFile16BitBE(file));
2998 real_chunk_size += 2;
2999 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_ELEM,
3001 if (real_chunk_size >= chunk_size)
3005 *level = li; /* copy temporary buffer back to level data */
3007 return real_chunk_size;
3010 static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level)
3012 int element = getMappedElement(getFile16BitBE(file));
3013 int envelope_nr = element - EL_ENVELOPE_1;
3014 int real_chunk_size = 2;
3018 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE,
3021 if (real_chunk_size >= chunk_size)
3025 level->envelope[envelope_nr] = xx_envelope;
3027 return real_chunk_size;
3030 static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level)
3032 int element = getMappedElement(getFile16BitBE(file));
3033 int real_chunk_size = 2;
3034 struct ElementInfo *ei = &element_info[element];
3037 xx_ei = *ei; /* copy element data into temporary buffer */
3039 xx_ei.num_change_pages = -1;
3043 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base,
3045 if (xx_ei.num_change_pages != -1)
3048 if (real_chunk_size >= chunk_size)
3054 if (ei->num_change_pages == -1)
3056 Error(ERR_WARN, "LoadLevel_CUSX(): missing 'num_change_pages' for '%s'",
3059 ei->num_change_pages = 1;
3061 setElementChangePages(ei, 1);
3062 setElementChangeInfoToDefaults(ei->change);
3064 return real_chunk_size;
3067 /* initialize number of change pages stored for this custom element */
3068 setElementChangePages(ei, ei->num_change_pages);
3069 for (i = 0; i < ei->num_change_pages; i++)
3070 setElementChangeInfoToDefaults(&ei->change_page[i]);
3072 /* start with reading properties for the first change page */
3073 xx_current_change_page = 0;
3077 struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page];
3079 xx_change = *change; /* copy change data into temporary buffer */
3081 resetEventBits(); /* reset bits; change page might have changed */
3083 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_change,
3086 *change = xx_change;
3088 setEventFlagsFromEventBits(change);
3090 if (real_chunk_size >= chunk_size)
3094 return real_chunk_size;
3097 static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level)
3099 int element = getMappedElement(getFile16BitBE(file));
3100 int real_chunk_size = 2;
3101 struct ElementInfo *ei = &element_info[element];
3102 struct ElementGroupInfo *group = ei->group;
3104 xx_ei = *ei; /* copy element data into temporary buffer */
3105 xx_group = *group; /* copy group data into temporary buffer */
3109 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX,
3112 if (real_chunk_size >= chunk_size)
3119 return real_chunk_size;
3122 static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
3123 struct LevelFileInfo *level_file_info)
3125 char *filename = level_file_info->filename;
3126 char cookie[MAX_LINE_LEN];
3127 char chunk_name[CHUNK_ID_LEN + 1];
3131 if (!(file = fopen(filename, MODE_READ)))
3133 level->no_valid_file = TRUE;
3136 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
3138 if (level != &level_template)
3139 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
3145 getFileChunkBE(file, chunk_name, NULL);
3146 if (strEqual(chunk_name, "RND1"))
3148 getFile32BitBE(file); /* not used */
3150 getFileChunkBE(file, chunk_name, NULL);
3151 if (!strEqual(chunk_name, "CAVE"))
3153 level->no_valid_file = TRUE;
3155 Error(ERR_WARN, "unknown format of level file '%s'", filename);
3160 else /* check for pre-2.0 file format with cookie string */
3162 strcpy(cookie, chunk_name);
3163 fgets(&cookie[4], MAX_LINE_LEN - 4, file);
3164 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
3165 cookie[strlen(cookie) - 1] = '\0';
3167 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
3169 level->no_valid_file = TRUE;
3171 Error(ERR_WARN, "unknown format of level file '%s'", filename);
3176 if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1)
3178 level->no_valid_file = TRUE;
3180 Error(ERR_WARN, "unsupported version of level file '%s'", filename);
3185 /* pre-2.0 level files have no game version, so use file version here */
3186 level->game_version = level->file_version;
3189 if (level->file_version < FILE_VERSION_1_2)
3191 /* level files from versions before 1.2.0 without chunk structure */
3192 LoadLevel_HEAD(file, LEVEL_CHUNK_HEAD_SIZE, level);
3193 LoadLevel_BODY(file, level->fieldx * level->fieldy, level);
3201 int (*loader)(FILE *, int, struct LevelInfo *);
3205 { "VERS", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS },
3206 { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE },
3207 { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD },
3208 { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME },
3209 { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH },
3210 { "INFO", -1, LoadLevel_INFO },
3211 { "BODY", -1, LoadLevel_BODY },
3212 { "CONT", -1, LoadLevel_CONT },
3213 { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
3214 { "CNT3", -1, LoadLevel_CNT3 },
3215 { "CUS1", -1, LoadLevel_CUS1 },
3216 { "CUS2", -1, LoadLevel_CUS2 },
3217 { "CUS3", -1, LoadLevel_CUS3 },
3218 { "CUS4", -1, LoadLevel_CUS4 },
3219 { "GRP1", -1, LoadLevel_GRP1 },
3220 { "CONF", -1, LoadLevel_CONF },
3221 { "ELEM", -1, LoadLevel_ELEM },
3222 { "NOTE", -1, LoadLevel_NOTE },
3223 { "CUSX", -1, LoadLevel_CUSX },
3224 { "GRPX", -1, LoadLevel_GRPX },
3229 while (getFileChunkBE(file, chunk_name, &chunk_size))
3233 while (chunk_info[i].name != NULL &&
3234 !strEqual(chunk_name, chunk_info[i].name))
3237 if (chunk_info[i].name == NULL)
3239 Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
3240 chunk_name, filename);
3241 ReadUnusedBytesFromFile(file, chunk_size);
3243 else if (chunk_info[i].size != -1 &&
3244 chunk_info[i].size != chunk_size)
3246 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
3247 chunk_size, chunk_name, filename);
3248 ReadUnusedBytesFromFile(file, chunk_size);
3252 /* call function to load this level chunk */
3253 int chunk_size_expected =
3254 (chunk_info[i].loader)(file, chunk_size, level);
3256 /* the size of some chunks cannot be checked before reading other
3257 chunks first (like "HEAD" and "BODY") that contain some header
3258 information, so check them here */
3259 if (chunk_size_expected != chunk_size)
3261 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
3262 chunk_size, chunk_name, filename);
3271 /* ------------------------------------------------------------------------- */
3272 /* functions for loading EM level */
3273 /* ------------------------------------------------------------------------- */
3277 static int map_em_element_yam(int element)
3281 case 0x00: return EL_EMPTY;
3282 case 0x01: return EL_EMERALD;
3283 case 0x02: return EL_DIAMOND;
3284 case 0x03: return EL_ROCK;
3285 case 0x04: return EL_ROBOT;
3286 case 0x05: return EL_SPACESHIP_UP;
3287 case 0x06: return EL_BOMB;
3288 case 0x07: return EL_BUG_UP;
3289 case 0x08: return EL_AMOEBA_DROP;
3290 case 0x09: return EL_NUT;
3291 case 0x0a: return EL_YAMYAM;
3292 case 0x0b: return EL_QUICKSAND_FULL;
3293 case 0x0c: return EL_SAND;
3294 case 0x0d: return EL_WALL_SLIPPERY;
3295 case 0x0e: return EL_STEELWALL;
3296 case 0x0f: return EL_WALL;
3297 case 0x10: return EL_EM_KEY_1;
3298 case 0x11: return EL_EM_KEY_2;
3299 case 0x12: return EL_EM_KEY_4;
3300 case 0x13: return EL_EM_KEY_3;
3301 case 0x14: return EL_MAGIC_WALL;
3302 case 0x15: return EL_ROBOT_WHEEL;
3303 case 0x16: return EL_DYNAMITE;
3305 case 0x17: return EL_EM_KEY_1; /* EMC */
3306 case 0x18: return EL_BUG_UP; /* EMC */
3307 case 0x1a: return EL_DIAMOND; /* EMC */
3308 case 0x1b: return EL_EMERALD; /* EMC */
3309 case 0x25: return EL_NUT; /* EMC */
3310 case 0x80: return EL_EMPTY; /* EMC */
3311 case 0x85: return EL_EM_KEY_1; /* EMC */
3312 case 0x86: return EL_EM_KEY_2; /* EMC */
3313 case 0x87: return EL_EM_KEY_4; /* EMC */
3314 case 0x88: return EL_EM_KEY_3; /* EMC */
3315 case 0x94: return EL_QUICKSAND_EMPTY; /* EMC */
3316 case 0x9a: return EL_AMOEBA_WET; /* EMC */
3317 case 0xaf: return EL_DYNAMITE; /* EMC */
3318 case 0xbd: return EL_SAND; /* EMC */
3321 Error(ERR_WARN, "invalid level element %d", element);
3326 static int map_em_element_field(int element)
3328 if (element >= 0xc8 && element <= 0xe1)
3329 return EL_CHAR_A + (element - 0xc8);
3330 else if (element >= 0xe2 && element <= 0xeb)
3331 return EL_CHAR_0 + (element - 0xe2);
3335 case 0x00: return EL_ROCK;
3336 case 0x01: return EL_ROCK; /* EMC */
3337 case 0x02: return EL_DIAMOND;
3338 case 0x03: return EL_DIAMOND;
3339 case 0x04: return EL_ROBOT;
3340 case 0x05: return EL_ROBOT; /* EMC */
3341 case 0x06: return EL_EMPTY_SPACE; /* EMC */
3342 case 0x07: return EL_EMPTY_SPACE; /* EMC */
3343 case 0x08: return EL_SPACESHIP_UP;
3344 case 0x09: return EL_SPACESHIP_RIGHT;
3345 case 0x0a: return EL_SPACESHIP_DOWN;
3346 case 0x0b: return EL_SPACESHIP_LEFT;
3347 case 0x0c: return EL_SPACESHIP_UP;
3348 case 0x0d: return EL_SPACESHIP_RIGHT;
3349 case 0x0e: return EL_SPACESHIP_DOWN;
3350 case 0x0f: return EL_SPACESHIP_LEFT;
3352 case 0x10: return EL_BOMB;
3353 case 0x11: return EL_BOMB; /* EMC */
3354 case 0x12: return EL_EMERALD;
3355 case 0x13: return EL_EMERALD;
3356 case 0x14: return EL_BUG_UP;
3357 case 0x15: return EL_BUG_RIGHT;
3358 case 0x16: return EL_BUG_DOWN;
3359 case 0x17: return EL_BUG_LEFT;
3360 case 0x18: return EL_BUG_UP;
3361 case 0x19: return EL_BUG_RIGHT;
3362 case 0x1a: return EL_BUG_DOWN;
3363 case 0x1b: return EL_BUG_LEFT;
3364 case 0x1c: return EL_AMOEBA_DROP;
3365 case 0x1d: return EL_AMOEBA_DROP; /* EMC */
3366 case 0x1e: return EL_AMOEBA_DROP; /* EMC */
3367 case 0x1f: return EL_AMOEBA_DROP; /* EMC */
3369 case 0x20: return EL_ROCK;
3370 case 0x21: return EL_BOMB; /* EMC */
3371 case 0x22: return EL_DIAMOND; /* EMC */
3372 case 0x23: return EL_EMERALD; /* EMC */
3373 case 0x24: return EL_MAGIC_WALL;
3374 case 0x25: return EL_NUT;
3375 case 0x26: return EL_NUT; /* EMC */
3376 case 0x27: return EL_NUT; /* EMC */
3378 /* looks like magic wheel, but is _always_ activated */
3379 case 0x28: return EL_ROBOT_WHEEL; /* EMC */
3381 case 0x29: return EL_YAMYAM; /* up */
3382 case 0x2a: return EL_YAMYAM; /* down */
3383 case 0x2b: return EL_YAMYAM; /* left */ /* EMC */
3384 case 0x2c: return EL_YAMYAM; /* right */ /* EMC */
3385 case 0x2d: return EL_QUICKSAND_FULL;
3386 case 0x2e: return EL_EMPTY_SPACE; /* EMC */
3387 case 0x2f: return EL_EMPTY_SPACE; /* EMC */
3389 case 0x30: return EL_EMPTY_SPACE; /* EMC */
3390 case 0x31: return EL_SAND; /* EMC */
3391 case 0x32: return EL_SAND; /* EMC */
3392 case 0x33: return EL_SAND; /* EMC */
3393 case 0x34: return EL_QUICKSAND_FULL; /* EMC */
3394 case 0x35: return EL_QUICKSAND_FULL; /* EMC */
3395 case 0x36: return EL_QUICKSAND_FULL; /* EMC */
3396 case 0x37: return EL_SAND; /* EMC */
3397 case 0x38: return EL_ROCK; /* EMC */
3398 case 0x39: return EL_EXPANDABLE_WALL_HORIZONTAL; /* EMC */
3399 case 0x3a: return EL_EXPANDABLE_WALL_VERTICAL; /* EMC */
3400 case 0x3b: return EL_DYNAMITE_ACTIVE; /* 1 */
3401 case 0x3c: return EL_DYNAMITE_ACTIVE; /* 2 */
3402 case 0x3d: return EL_DYNAMITE_ACTIVE; /* 3 */
3403 case 0x3e: return EL_DYNAMITE_ACTIVE; /* 4 */
3404 case 0x3f: return EL_ACID_POOL_BOTTOM;
3406 case 0x40: return EL_EXIT_OPEN; /* 1 */
3407 case 0x41: return EL_EXIT_OPEN; /* 2 */
3408 case 0x42: return EL_EXIT_OPEN; /* 3 */
3409 case 0x43: return EL_BALLOON; /* EMC */
3410 case 0x44: return EL_UNKNOWN; /* EMC ("plant") */
3411 case 0x45: return EL_SPRING; /* EMC */
3412 case 0x46: return EL_SPRING; /* falling */ /* EMC */
3413 case 0x47: return EL_SPRING; /* left */ /* EMC */
3414 case 0x48: return EL_SPRING; /* right */ /* EMC */
3415 case 0x49: return EL_UNKNOWN; /* EMC ("ball 1") */
3416 case 0x4a: return EL_UNKNOWN; /* EMC ("ball 2") */
3417 case 0x4b: return EL_UNKNOWN; /* EMC ("android") */
3418 case 0x4c: return EL_EMPTY_SPACE; /* EMC */
3419 case 0x4d: return EL_UNKNOWN; /* EMC ("android") */
3420 case 0x4e: return EL_INVISIBLE_WALL; /* EMC (? "android") */
3421 case 0x4f: return EL_UNKNOWN; /* EMC ("android") */
3423 case 0x50: return EL_UNKNOWN; /* EMC ("android") */
3424 case 0x51: return EL_UNKNOWN; /* EMC ("android") */
3425 case 0x52: return EL_UNKNOWN; /* EMC ("android") */
3426 case 0x53: return EL_UNKNOWN; /* EMC ("android") */
3427 case 0x54: return EL_UNKNOWN; /* EMC ("android") */
3428 case 0x55: return EL_EMPTY_SPACE; /* EMC */
3429 case 0x56: return EL_EMPTY_SPACE; /* EMC */
3430 case 0x57: return EL_EMPTY_SPACE; /* EMC */
3431 case 0x58: return EL_EMPTY_SPACE; /* EMC */
3432 case 0x59: return EL_EMPTY_SPACE; /* EMC */
3433 case 0x5a: return EL_EMPTY_SPACE; /* EMC */
3434 case 0x5b: return EL_EMPTY_SPACE; /* EMC */
3435 case 0x5c: return EL_EMPTY_SPACE; /* EMC */
3436 case 0x5d: return EL_EMPTY_SPACE; /* EMC */
3437 case 0x5e: return EL_EMPTY_SPACE; /* EMC */
3438 case 0x5f: return EL_EMPTY_SPACE; /* EMC */
3440 case 0x60: return EL_EMPTY_SPACE; /* EMC */
3441 case 0x61: return EL_EMPTY_SPACE; /* EMC */
3442 case 0x62: return EL_EMPTY_SPACE; /* EMC */
3443 case 0x63: return EL_SPRING; /* left */ /* EMC */
3444 case 0x64: return EL_SPRING; /* right */ /* EMC */
3445 case 0x65: return EL_ACID; /* 1 */ /* EMC */
3446 case 0x66: return EL_ACID; /* 2 */ /* EMC */
3447 case 0x67: return EL_ACID; /* 3 */ /* EMC */
3448 case 0x68: return EL_ACID; /* 4 */ /* EMC */
3449 case 0x69: return EL_ACID; /* 5 */ /* EMC */
3450 case 0x6a: return EL_ACID; /* 6 */ /* EMC */
3451 case 0x6b: return EL_ACID; /* 7 */ /* EMC */
3452 case 0x6c: return EL_ACID; /* 8 */ /* EMC */
3453 case 0x6d: return EL_EMPTY_SPACE; /* EMC */
3454 case 0x6e: return EL_EMPTY_SPACE; /* EMC */
3455 case 0x6f: return EL_EMPTY_SPACE; /* EMC */
3457 case 0x70: return EL_EMPTY_SPACE; /* EMC */
3458 case 0x71: return EL_EMPTY_SPACE; /* EMC */
3459 case 0x72: return EL_NUT; /* left */ /* EMC */
3460 case 0x73: return EL_SAND; /* EMC (? "nut") */
3461 case 0x74: return EL_STEELWALL;
3462 case 0x75: return EL_EMPTY_SPACE; /* EMC */
3463 case 0x76: return EL_EMPTY_SPACE; /* EMC */
3464 case 0x77: return EL_BOMB; /* left */ /* EMC */
3465 case 0x78: return EL_BOMB; /* right */ /* EMC */
3466 case 0x79: return EL_ROCK; /* left */ /* EMC */
3467 case 0x7a: return EL_ROCK; /* right */ /* EMC */
3468 case 0x7b: return EL_ACID; /* (? EMC "blank") */
3469 case 0x7c: return EL_EMPTY_SPACE; /* EMC */
3470 case 0x7d: return EL_EMPTY_SPACE; /* EMC */
3471 case 0x7e: return EL_EMPTY_SPACE; /* EMC */
3472 case 0x7f: return EL_EMPTY_SPACE; /* EMC */
3474 case 0x80: return EL_EMPTY;
3475 case 0x81: return EL_WALL_SLIPPERY;
3476 case 0x82: return EL_SAND;
3477 case 0x83: return EL_STEELWALL;
3478 case 0x84: return EL_WALL;
3479 case 0x85: return EL_EM_KEY_1;
3480 case 0x86: return EL_EM_KEY_2;
3481 case 0x87: return EL_EM_KEY_4;
3482 case 0x88: return EL_EM_KEY_3;
3483 case 0x89: return EL_EM_GATE_1;
3484 case 0x8a: return EL_EM_GATE_2;
3485 case 0x8b: return EL_EM_GATE_4;
3486 case 0x8c: return EL_EM_GATE_3;
3487 case 0x8d: return EL_INVISIBLE_WALL; /* EMC (? "dripper") */
3488 case 0x8e: return EL_EM_GATE_1_GRAY;
3489 case 0x8f: return EL_EM_GATE_2_GRAY;
3491 case 0x90: return EL_EM_GATE_4_GRAY;
3492 case 0x91: return EL_EM_GATE_3_GRAY;
3493 case 0x92: return EL_MAGIC_WALL;
3494 case 0x93: return EL_ROBOT_WHEEL;
3495 case 0x94: return EL_QUICKSAND_EMPTY; /* (? EMC "sand") */
3496 case 0x95: return EL_ACID_POOL_TOPLEFT;
3497 case 0x96: return EL_ACID_POOL_TOPRIGHT;
3498 case 0x97: return EL_ACID_POOL_BOTTOMLEFT;
3499 case 0x98: return EL_ACID_POOL_BOTTOMRIGHT;
3500 case 0x99: return EL_ACID; /* (? EMC "fake blank") */
3501 case 0x9a: return EL_AMOEBA_DEAD; /* 1 */
3502 case 0x9b: return EL_AMOEBA_DEAD; /* 2 */
3503 case 0x9c: return EL_AMOEBA_DEAD; /* 3 */
3504 case 0x9d: return EL_AMOEBA_DEAD; /* 4 */
3505 case 0x9e: return EL_EXIT_CLOSED;
3506 case 0x9f: return EL_CHAR_LESS; /* arrow left */
3508 /* looks like normal sand, but behaves like wall */
3509 case 0xa0: return EL_UNKNOWN; /* EMC ("fake grass") */
3510 case 0xa1: return EL_UNKNOWN; /* EMC ("lenses") */
3511 case 0xa2: return EL_UNKNOWN; /* EMC ("magnify") */
3512 case 0xa3: return EL_UNKNOWN; /* EMC ("fake blank") */
3513 case 0xa4: return EL_UNKNOWN; /* EMC ("fake grass") */
3514 case 0xa5: return EL_UNKNOWN; /* EMC ("switch") */
3515 case 0xa6: return EL_UNKNOWN; /* EMC ("switch") */
3516 case 0xa7: return EL_EMPTY_SPACE; /* EMC */
3517 case 0xa8: return EL_EMC_WALL_1; /* EMC ("decor 8") */
3518 case 0xa9: return EL_EMC_WALL_2; /* EMC ("decor 9") */
3519 case 0xaa: return EL_EMC_WALL_3; /* EMC ("decor 10") */
3520 case 0xab: return EL_EMC_WALL_7; /* EMC ("decor 5") */
3521 case 0xac: return EL_CHAR_COMMA; /* EMC */
3522 case 0xad: return EL_CHAR_QUOTEDBL; /* EMC */
3523 case 0xae: return EL_CHAR_MINUS; /* EMC */
3524 case 0xaf: return EL_DYNAMITE;
3526 case 0xb0: return EL_EMC_STEELWALL_1; /* EMC ("steel 3") */
3527 case 0xb1: return EL_EMC_WALL_8; /* EMC ("decor 6") */
3528 case 0xb2: return EL_UNKNOWN; /* EMC ("decor 7") */
3529 case 0xb3: return EL_STEELWALL; /* 2 */ /* EMC */
3530 case 0xb4: return EL_WALL_SLIPPERY; /* 2 */ /* EMC */
3531 case 0xb5: return EL_EMC_WALL_6; /* EMC ("decor 2") */
3532 case 0xb6: return EL_EMC_WALL_5; /* EMC ("decor 4") */
3533 case 0xb7: return EL_EMC_WALL_4; /* EMC ("decor 3") */
3534 case 0xb8: return EL_BALLOON_SWITCH_ANY; /* EMC */
3535 case 0xb9: return EL_BALLOON_SWITCH_RIGHT; /* EMC */
3536 case 0xba: return EL_BALLOON_SWITCH_DOWN; /* EMC */
3537 case 0xbb: return EL_BALLOON_SWITCH_LEFT; /* EMC */
3538 case 0xbc: return EL_BALLOON_SWITCH_UP; /* EMC */
3539 case 0xbd: return EL_SAND; /* EMC ("dirt") */
3540 case 0xbe: return EL_UNKNOWN; /* EMC ("plant") */
3541 case 0xbf: return EL_UNKNOWN; /* EMC ("key 5") */
3543 case 0xc0: return EL_UNKNOWN; /* EMC ("key 6") */
3544 case 0xc1: return EL_UNKNOWN; /* EMC ("key 7") */
3545 case 0xc2: return EL_UNKNOWN; /* EMC ("key 8") */
3546 case 0xc3: return EL_UNKNOWN; /* EMC ("door 5") */
3547 case 0xc4: return EL_UNKNOWN; /* EMC ("door 6") */
3548 case 0xc5: return EL_UNKNOWN; /* EMC ("door 7") */
3549 case 0xc6: return EL_UNKNOWN; /* EMC ("door 8") */
3550 case 0xc7: return EL_UNKNOWN; /* EMC ("bumper") */
3552 /* characters: see above */
3554 case 0xec: return EL_CHAR_PERIOD;
3555 case 0xed: return EL_CHAR_EXCLAM;
3556 case 0xee: return EL_CHAR_COLON;
3557 case 0xef: return EL_CHAR_QUESTION;
3559 case 0xf0: return EL_CHAR_GREATER; /* arrow right */
3560 case 0xf1: return EL_CHAR_COPYRIGHT; /* EMC: "decor 1" */
3561 case 0xf2: return EL_UNKNOWN; /* EMC ("fake door 5") */
3562 case 0xf3: return EL_UNKNOWN; /* EMC ("fake door 6") */
3563 case 0xf4: return EL_UNKNOWN; /* EMC ("fake door 7") */
3564 case 0xf5: return EL_UNKNOWN; /* EMC ("fake door 8") */
3565 case 0xf6: return EL_EMPTY_SPACE; /* EMC */
3566 case 0xf7: return EL_EMPTY_SPACE; /* EMC */
3568 case 0xf8: return EL_EMPTY_SPACE; /* EMC */
3569 case 0xf9: return EL_EMPTY_SPACE; /* EMC */
3570 case 0xfa: return EL_EMPTY_SPACE; /* EMC */
3571 case 0xfb: return EL_EMPTY_SPACE; /* EMC */
3572 case 0xfc: return EL_EMPTY_SPACE; /* EMC */
3573 case 0xfd: return EL_EMPTY_SPACE; /* EMC */
3575 case 0xfe: return EL_PLAYER_1; /* EMC: "blank" */
3576 case 0xff: return EL_PLAYER_2; /* EMC: "blank" */
3579 /* should never happen (all 8-bit value cases should be handled) */
3580 Error(ERR_WARN, "invalid level element %d", element);
3585 #define EM_LEVEL_SIZE 2106
3586 #define EM_LEVEL_XSIZE 64
3587 #define EM_LEVEL_YSIZE 32
3589 static void OLD_LoadLevelFromFileInfo_EM(struct LevelInfo *level,
3590 struct LevelFileInfo *level_file_info)
3592 char *filename = level_file_info->filename;
3594 unsigned char leveldata[EM_LEVEL_SIZE];
3595 unsigned char *header = &leveldata[EM_LEVEL_XSIZE * EM_LEVEL_YSIZE];
3596 int nr = level_file_info->nr;
3599 if (!(file = fopen(filename, MODE_READ)))
3601 level->no_valid_file = TRUE;
3603 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
3608 for (i = 0; i < EM_LEVEL_SIZE; i++)
3609 leveldata[i] = fgetc(file);
3613 /* check if level data is crypted by testing against known starting bytes
3614 of the few existing crypted level files (from Emerald Mine 1 + 2) */
3616 if ((leveldata[0] == 0xf1 ||
3617 leveldata[0] == 0xf5) && leveldata[2] == 0xe7 && leveldata[3] == 0xee)
3619 unsigned char code0 = 0x65;
3620 unsigned char code1 = 0x11;
3622 if (leveldata[0] == 0xf5) /* error in crypted Emerald Mine 2 levels */
3623 leveldata[0] = 0xf1;
3625 /* decode crypted level data */
3627 for (i = 0; i < EM_LEVEL_SIZE; i++)
3629 leveldata[i] ^= code0;
3630 leveldata[i] -= code1;
3632 code0 = (code0 + 7) & 0xff;
3636 level->fieldx = EM_LEVEL_XSIZE;
3637 level->fieldy = EM_LEVEL_YSIZE;
3639 level->time = header[46] * 10;
3640 level->gems_needed = header[47];
3642 /* The original Emerald Mine levels have their level number stored
3643 at the second byte of the level file...
3644 Do not trust this information at other level files, e.g. EMC,
3645 but correct it anyway (normally the first row is completely
3646 steel wall, so the correction does not hurt anyway). */
3648 if (leveldata[1] == nr)
3649 leveldata[1] = leveldata[2]; /* correct level number field */
3651 sprintf(level->name, "Level %d", nr); /* set level name */
3653 level->score[SC_EMERALD] = header[36];
3654 level->score[SC_DIAMOND] = header[37];
3655 level->score[SC_ROBOT] = header[38];
3656 level->score[SC_SPACESHIP] = header[39];
3657 level->score[SC_BUG] = header[40];
3658 level->score[SC_YAMYAM] = header[41];
3659 level->score[SC_NUT] = header[42];
3660 level->score[SC_DYNAMITE] = header[43];
3661 level->score[SC_TIME_BONUS] = header[44];
3663 level->num_yamyam_contents = 4;
3665 for (i = 0; i < level->num_yamyam_contents; i++)
3666 for (y = 0; y < 3; y++)
3667 for (x = 0; x < 3; x++)
3668 level->yamyam_content[i].e[x][y] =
3669 map_em_element_yam(header[i * 9 + y * 3 + x]);
3671 level->amoeba_speed = (header[52] * 256 + header[53]) % 256;
3672 level->time_magic_wall = (header[54] * 256 + header[55]) * 16 / 100;
3673 level->time_wheel = (header[56] * 256 + header[57]) * 16 / 100;
3674 level->amoeba_content = EL_DIAMOND;
3676 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
3678 int new_element = map_em_element_field(leveldata[y * EM_LEVEL_XSIZE + x]);
3680 if (new_element == EL_AMOEBA_DEAD && level->amoeba_speed)
3681 new_element = EL_AMOEBA_WET;
3683 level->field[x][y] = new_element;
3686 x = (header[48] * 256 + header[49]) % EM_LEVEL_XSIZE;
3687 y = (header[48] * 256 + header[49]) / EM_LEVEL_XSIZE;
3688 level->field[x][y] = EL_PLAYER_1;
3690 x = (header[50] * 256 + header[51]) % EM_LEVEL_XSIZE;
3691 y = (header[50] * 256 + header[51]) / EM_LEVEL_XSIZE;
3692 level->field[x][y] = EL_PLAYER_2;
3697 void CopyNativeLevel_RND_to_EM(struct LevelInfo *level)
3699 static int ball_xy[8][2] =
3710 struct LevelInfo_EM *level_em = level->native_em_level;
3711 struct LEVEL *lev = level_em->lev;
3712 struct PLAYER **ply = level_em->ply;
3715 lev->width = MIN(level->fieldx, EM_MAX_CAVE_WIDTH);
3716 lev->height = MIN(level->fieldy, EM_MAX_CAVE_HEIGHT);
3718 lev->time_seconds = level->time;
3719 lev->required_initial = level->gems_needed;
3721 lev->emerald_score = level->score[SC_EMERALD];
3722 lev->diamond_score = level->score[SC_DIAMOND];
3723 lev->alien_score = level->score[SC_ROBOT];
3724 lev->tank_score = level->score[SC_SPACESHIP];
3725 lev->bug_score = level->score[SC_BUG];
3726 lev->eater_score = level->score[SC_YAMYAM];
3727 lev->nut_score = level->score[SC_NUT];
3728 lev->dynamite_score = level->score[SC_DYNAMITE];
3729 lev->key_score = level->score[SC_KEY];
3730 lev->exit_score = level->score[SC_TIME_BONUS];
3732 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3733 for (y = 0; y < 3; y++)
3734 for (x = 0; x < 3; x++)
3735 lev->eater_array[i][y * 3 + x] =
3736 map_element_RND_to_EM(level->yamyam_content[i].e[x][y]);
3738 lev->amoeba_time = level->amoeba_speed;
3739 lev->wonderwall_time_initial = level->time_magic_wall;
3740 lev->wheel_time = level->time_wheel;
3742 lev->android_move_time = level->android_move_time;
3743 lev->android_clone_time = level->android_clone_time;
3744 lev->ball_random = level->ball_random;
3745 lev->ball_state_initial = level->ball_state_initial;
3746 lev->ball_time = level->ball_time;
3747 lev->num_ball_arrays = level->num_ball_contents;
3749 lev->lenses_score = level->lenses_score;
3750 lev->magnify_score = level->magnify_score;
3751 lev->slurp_score = level->slurp_score;
3753 lev->lenses_time = level->lenses_time;
3754 lev->magnify_time = level->magnify_time;
3756 lev->wind_direction_initial =
3757 map_direction_RND_to_EM(level->wind_direction_initial);
3758 lev->wind_cnt_initial = (level->wind_direction_initial != MV_NONE ?
3759 lev->wind_time : 0);
3761 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3762 for (j = 0; j < 8; j++)
3763 lev->ball_array[i][j] =
3764 map_element_RND_to_EM(level->
3765 ball_content[i].e[ball_xy[j][0]][ball_xy[j][1]]);
3767 map_android_clone_elements_RND_to_EM(level);
3769 /* first fill the complete playfield with the default border element */
3770 for (y = 0; y < EM_MAX_CAVE_HEIGHT; y++)
3771 for (x = 0; x < EM_MAX_CAVE_WIDTH; x++)
3772 level_em->cave[x][y] = ZBORDER;
3774 if (BorderElement == EL_STEELWALL)
3776 for (y = 0; y < lev->height + 2; y++)
3777 for (x = 0; x < lev->width + 2; x++)
3778 level_em->cave[x + 1][y + 1] = map_element_RND_to_EM(EL_STEELWALL);
3781 /* then copy the real level contents from level file into the playfield */
3782 for (y = 0; y < lev->height; y++) for (x = 0; x < lev->width; x++)
3784 int new_element = map_element_RND_to_EM(level->field[x][y]);
3785 int offset = (BorderElement == EL_STEELWALL ? 1 : 0);
3786 int xx = x + 1 + offset;
3787 int yy = y + 1 + offset;
3789 if (level->field[x][y] == EL_AMOEBA_DEAD)
3790 new_element = map_element_RND_to_EM(EL_AMOEBA_WET);
3792 level_em->cave[xx][yy] = new_element;
3795 for (i = 0; i < MAX_PLAYERS; i++)
3797 ply[i]->x_initial = 0;
3798 ply[i]->y_initial = 0;
3801 /* initialize player positions and delete players from the playfield */
3802 for (y = 0; y < lev->height; y++) for (x = 0; x < lev->width; x++)
3804 if (ELEM_IS_PLAYER(level->field[x][y]))
3806 int player_nr = GET_PLAYER_NR(level->field[x][y]);
3807 int offset = (BorderElement == EL_STEELWALL ? 1 : 0);
3808 int xx = x + 1 + offset;
3809 int yy = y + 1 + offset;
3811 ply[player_nr]->x_initial = xx;
3812 ply[player_nr]->y_initial = yy;
3814 level_em->cave[xx][yy] = map_element_RND_to_EM(EL_EMPTY);
3818 if (BorderElement == EL_STEELWALL)
3825 void CopyNativeLevel_EM_to_RND(struct LevelInfo *level)
3827 static int ball_xy[8][2] =
3838 struct LevelInfo_EM *level_em = level->native_em_level;
3839 struct LEVEL *lev = level_em->lev;
3840 struct PLAYER **ply = level_em->ply;
3843 level->fieldx = MIN(lev->width, MAX_LEV_FIELDX);
3844 level->fieldy = MIN(lev->height, MAX_LEV_FIELDY);
3846 level->time = lev->time_seconds;
3847 level->gems_needed = lev->required_initial;
3849 sprintf(level->name, "Level %d", level->file_info.nr);
3851 level->score[SC_EMERALD] = lev->emerald_score;
3852 level->score[SC_DIAMOND] = lev->diamond_score;
3853 level->score[SC_ROBOT] = lev->alien_score;
3854 level->score[SC_SPACESHIP] = lev->tank_score;
3855 level->score[SC_BUG] = lev->bug_score;
3856 level->score[SC_YAMYAM] = lev->eater_score;
3857 level->score[SC_NUT] = lev->nut_score;
3858 level->score[SC_DYNAMITE] = lev->dynamite_score;
3859 level->score[SC_KEY] = lev->key_score;
3860 level->score[SC_TIME_BONUS] = lev->exit_score;
3862 level->num_yamyam_contents = MAX_ELEMENT_CONTENTS;
3864 for (i = 0; i < level->num_yamyam_contents; i++)
3865 for (y = 0; y < 3; y++)
3866 for (x = 0; x < 3; x++)
3867 level->yamyam_content[i].e[x][y] =
3868 map_element_EM_to_RND(lev->eater_array[i][y * 3 + x]);
3870 level->amoeba_speed = lev->amoeba_time;
3871 level->time_magic_wall = lev->wonderwall_time_initial;
3872 level->time_wheel = lev->wheel_time;
3874 level->android_move_time = lev->android_move_time;
3875 level->android_clone_time = lev->android_clone_time;
3876 level->ball_random = lev->ball_random;
3877 level->ball_state_initial = lev->ball_state_initial;
3878 level->ball_time = lev->ball_time;
3879 level->num_ball_contents = lev->num_ball_arrays;
3881 level->lenses_score = lev->lenses_score;
3882 level->magnify_score = lev->magnify_score;
3883 level->slurp_score = lev->slurp_score;
3885 level->lenses_time = lev->lenses_time;
3886 level->magnify_time = lev->magnify_time;
3888 level->wind_direction_initial =
3889 map_direction_EM_to_RND(lev->wind_direction_initial);
3891 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3892 for (j = 0; j < 8; j++)
3893 level->ball_content[i].e[ball_xy[j][0]][ball_xy[j][1]] =
3894 map_element_EM_to_RND(lev->ball_array[i][j]);
3896 map_android_clone_elements_EM_to_RND(level);
3898 /* convert the playfield (some elements need special treatment) */
3899 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
3901 int new_element = map_element_EM_to_RND(level_em->cave[x + 1][y + 1]);
3903 if (new_element == EL_AMOEBA_WET && level->amoeba_speed == 0)
3904 new_element = EL_AMOEBA_DEAD;
3906 level->field[x][y] = new_element;
3909 for (i = 0; i < MAX_PLAYERS; i++)
3911 /* in case of all players set to the same field, use the first player */
3912 int nr = MAX_PLAYERS - i - 1;
3913 int jx = ply[nr]->x_initial - 1;
3914 int jy = ply[nr]->y_initial - 1;
3916 if (jx != -1 && jy != -1)
3917 level->field[jx][jy] = EL_PLAYER_1 + nr;
3922 /* ------------------------------------------------------------------------- */
3923 /* functions for loading SP level */
3924 /* ------------------------------------------------------------------------- */
3928 #define NUM_SUPAPLEX_LEVELS_PER_PACKAGE 111
3929 #define SP_LEVEL_SIZE 1536
3930 #define SP_LEVEL_XSIZE 60
3931 #define SP_LEVEL_YSIZE 24
3932 #define SP_LEVEL_NAME_LEN 23
3934 static void LoadLevelFromFileStream_SP(FILE *file, struct LevelInfo *level,
3937 int initial_player_gravity;
3938 int num_special_ports;
3941 /* for details of the Supaplex level format, see Herman Perk's Supaplex
3942 documentation file "SPFIX63.DOC" from his Supaplex "SpeedFix" package */
3944 /* read level body (width * height == 60 * 24 tiles == 1440 bytes) */
3945 for (y = 0; y < SP_LEVEL_YSIZE; y++)
3947 for (x = 0; x < SP_LEVEL_XSIZE; x++)
3949 int element_old = fgetc(file);
3952 if (element_old <= 0x27)
3953 element_new = getMappedElement(EL_SP_START + element_old);
3954 else if (element_old == 0x28)
3955 element_new = EL_INVISIBLE_WALL;
3958 Error(ERR_WARN, "in level %d, at position %d, %d:", nr, x, y);
3959 Error(ERR_WARN, "invalid level element %d", element_old);
3961 element_new = EL_UNKNOWN;
3964 level->field[x][y] = element_new;
3968 ReadUnusedBytesFromFile(file, 4); /* (not used by Supaplex engine) */
3970 /* initial gravity: 1 == "on", anything else (0) == "off" */
3971 initial_player_gravity = (fgetc(file) == 1 ? TRUE : FALSE);
3973 for (i = 0; i < MAX_PLAYERS; i++)
3974 level->initial_player_gravity[i] = initial_player_gravity;
3976 ReadUnusedBytesFromFile(file, 1); /* (not used by Supaplex engine) */
3978 /* level title in uppercase letters, padded with dashes ("-") (23 bytes) */
3979 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
3980 level->name[i] = fgetc(file);
3981 level->name[SP_LEVEL_NAME_LEN] = '\0';
3983 /* initial "freeze zonks": 2 == "on", anything else (0, 1) == "off" */
3984 ReadUnusedBytesFromFile(file, 1); /* (not used by R'n'D engine) */
3986 /* number of infotrons needed; 0 means that Supaplex will count the total
3987 amount of infotrons in the level and use the low byte of that number
3988 (a multiple of 256 infotrons will result in "0 infotrons needed"!) */
3989 level->gems_needed = fgetc(file);
3991 /* number of special ("gravity") port entries below (maximum 10 allowed) */
3992 num_special_ports = fgetc(file);
3994 /* database of properties of up to 10 special ports (6 bytes per port) */
3995 for (i = 0; i < 10; i++)
3997 int port_location, port_x, port_y, port_element;
4000 /* high and low byte of the location of a special port; if (x, y) are the
4001 coordinates of a port in the field and (0, 0) is the top-left corner,
4002 the 16 bit value here calculates as 2 * (x + (y * 60)) (this is twice
4003 of what may be expected: Supaplex works with a game field in memory
4004 which is 2 bytes per tile) */
4005 port_location = getFile16BitBE(file);
4007 /* change gravity: 1 == "turn on", anything else (0) == "turn off" */
4008 gravity = fgetc(file);
4010 /* "freeze zonks": 2 == "turn on", anything else (0, 1) == "turn off" */
4011 ReadUnusedBytesFromFile(file, 1); /* (not used by R'n'D engine) */
4013 /* "freeze enemies": 1 == "turn on", anything else (0) == "turn off" */
4014 ReadUnusedBytesFromFile(file, 1); /* (not used by R'n'D engine) */
4016 ReadUnusedBytesFromFile(file, 1); /* (not used by Supaplex engine) */
4018 if (i >= num_special_ports)
4021 port_x = (port_location / 2) % SP_LEVEL_XSIZE;
4022 port_y = (port_location / 2) / SP_LEVEL_XSIZE;
4024 if (port_x < 0 || port_x >= SP_LEVEL_XSIZE ||
4025 port_y < 0 || port_y >= SP_LEVEL_YSIZE)
4027 Error(ERR_WARN, "special port position (%d, %d) out of bounds",
4033 port_element = level->field[port_x][port_y];
4035 if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
4036 port_element > EL_SP_GRAVITY_PORT_UP)
4038 Error(ERR_WARN, "no special port at position (%d, %d)", port_x, port_y);
4043 /* change previous (wrong) gravity inverting special port to either
4044 gravity enabling special port or gravity disabling special port */
4045 level->field[port_x][port_y] +=
4046 (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
4047 EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
4050 ReadUnusedBytesFromFile(file, 4); /* (not used by Supaplex engine) */
4052 /* change special gravity ports without database entries to normal ports */
4053 for (y = 0; y < SP_LEVEL_YSIZE; y++)
4054 for (x = 0; x < SP_LEVEL_XSIZE; x++)
4055 if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
4056 level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
4057 level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
4059 /* auto-determine number of infotrons if it was stored as "0" -- see above */
4060 if (level->gems_needed == 0)
4062 for (y = 0; y < SP_LEVEL_YSIZE; y++)
4063 for (x = 0; x < SP_LEVEL_XSIZE; x++)
4064 if (level->field[x][y] == EL_SP_INFOTRON)
4065 level->gems_needed++;
4067 level->gems_needed &= 0xff; /* only use low byte -- see above */
4070 level->fieldx = SP_LEVEL_XSIZE;
4071 level->fieldy = SP_LEVEL_YSIZE;
4073 level->time = 0; /* no time limit */
4074 level->amoeba_speed = 0;
4075 level->time_magic_wall = 0;
4076 level->time_wheel = 0;
4077 level->amoeba_content = EL_EMPTY;
4080 /* original Supaplex does not use score values -- use default values */
4082 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
4083 level->score[i] = 0;
4086 /* there are no yamyams in supaplex levels */
4087 for (i = 0; i < level->num_yamyam_contents; i++)
4088 for (y = 0; y < 3; y++)
4089 for (x = 0; x < 3; x++)
4090 level->yamyam_content[i].e[x][y] = EL_EMPTY;
4093 static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
4094 struct LevelFileInfo *level_file_info)
4096 char *filename = level_file_info->filename;
4098 int nr = level_file_info->nr - leveldir_current->first_level;
4100 char name_first, name_last;
4101 struct LevelInfo multipart_level;
4102 int multipart_xpos, multipart_ypos;
4103 boolean is_multipart_level;
4104 boolean is_first_part;
4105 boolean reading_multipart_level = FALSE;
4106 boolean use_empty_level = FALSE;
4108 if (!(file = fopen(filename, MODE_READ)))
4110 level->no_valid_file = TRUE;
4112 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
4117 /* position file stream to the requested level inside the level package */
4118 if (level_file_info->packed &&
4119 fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
4121 level->no_valid_file = TRUE;
4123 Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename);
4128 /* there exist Supaplex level package files with multi-part levels which
4129 can be detected as follows: instead of leading and trailing dashes ('-')
4130 to pad the level name, they have leading and trailing numbers which are
4131 the x and y coordinations of the current part of the multi-part level;
4132 if there are '?' characters instead of numbers on the left or right side
4133 of the level name, the multi-part level consists of only horizontal or
4136 for (l = nr; l < NUM_SUPAPLEX_LEVELS_PER_PACKAGE; l++)
4138 LoadLevelFromFileStream_SP(file, level, l);
4140 /* check if this level is a part of a bigger multi-part level */
4142 name_first = level->name[0];
4143 name_last = level->name[SP_LEVEL_NAME_LEN - 1];
4145 is_multipart_level =
4146 ((name_first == '?' || (name_first >= '0' && name_first <= '9')) &&
4147 (name_last == '?' || (name_last >= '0' && name_last <= '9')));
4150 ((name_first == '?' || name_first == '1') &&
4151 (name_last == '?' || name_last == '1'));
4153 /* correct leading multipart level meta information in level name */
4154 for (i = 0; i < SP_LEVEL_NAME_LEN && level->name[i] == name_first; i++)
4155 level->name[i] = '-';
4157 /* correct trailing multipart level meta information in level name */
4158 for (i = SP_LEVEL_NAME_LEN - 1; i >= 0 && level->name[i] == name_last; i--)
4159 level->name[i] = '-';
4161 /* ---------- check for normal single level ---------- */
4163 if (!reading_multipart_level && !is_multipart_level)
4165 /* the current level is simply a normal single-part level, and we are
4166 not reading a multi-part level yet, so return the level as it is */
4171 /* ---------- check for empty level (unused multi-part) ---------- */
4173 if (!reading_multipart_level && is_multipart_level && !is_first_part)
4175 /* this is a part of a multi-part level, but not the first part
4176 (and we are not already reading parts of a multi-part level);
4177 in this case, use an empty level instead of the single part */
4179 use_empty_level = TRUE;
4184 /* ---------- check for finished multi-part level ---------- */
4186 if (reading_multipart_level &&
4187 (!is_multipart_level ||
4188 !strEqual(level->name, multipart_level.name)))
4190 /* we are already reading parts of a multi-part level, but this level is
4191 either not a multi-part level, or a part of a different multi-part
4192 level; in both cases, the multi-part level seems to be complete */
4197 /* ---------- here we have one part of a multi-part level ---------- */
4199 reading_multipart_level = TRUE;
4201 if (is_first_part) /* start with first part of new multi-part level */
4203 /* copy level info structure from first part */
4204 multipart_level = *level;
4206 /* clear playfield of new multi-part level */
4207 for (y = 0; y < MAX_LEV_FIELDY; y++)
4208 for (x = 0; x < MAX_LEV_FIELDX; x++)
4209 multipart_level.field[x][y] = EL_EMPTY;
4212 if (name_first == '?')
4214 if (name_last == '?')
4217 multipart_xpos = (int)(name_first - '0');
4218 multipart_ypos = (int)(name_last - '0');
4221 printf("----------> part (%d/%d) of multi-part level '%s'\n",
4222 multipart_xpos, multipart_ypos, multipart_level.name);
4225 if (multipart_xpos * SP_LEVEL_XSIZE > MAX_LEV_FIELDX ||
4226 multipart_ypos * SP_LEVEL_YSIZE > MAX_LEV_FIELDY)
4228 Error(ERR_WARN, "multi-part level is too big -- ignoring part of it");
4233 multipart_level.fieldx = MAX(multipart_level.fieldx,
4234 multipart_xpos * SP_LEVEL_XSIZE);
4235 multipart_level.fieldy = MAX(multipart_level.fieldy,
4236 multipart_ypos * SP_LEVEL_YSIZE);
4238 /* copy level part at the right position of multi-part level */
4239 for (y = 0; y < SP_LEVEL_YSIZE; y++)
4241 for (x = 0; x < SP_LEVEL_XSIZE; x++)
4243 int start_x = (multipart_xpos - 1) * SP_LEVEL_XSIZE;
4244 int start_y = (multipart_ypos - 1) * SP_LEVEL_YSIZE;
4246 multipart_level.field[start_x + x][start_y + y] = level->field[x][y];
4253 if (use_empty_level)
4255 setLevelInfoToDefaults(level);
4257 level->fieldx = SP_LEVEL_XSIZE;
4258 level->fieldy = SP_LEVEL_YSIZE;
4260 for (y = 0; y < SP_LEVEL_YSIZE; y++)
4261 for (x = 0; x < SP_LEVEL_XSIZE; x++)
4262 level->field[x][y] = EL_EMPTY;
4264 strcpy(level->name, "-------- EMPTY --------");
4266 Error(ERR_WARN, "single part of multi-part level -- using empty level");
4269 if (reading_multipart_level)
4270 *level = multipart_level;
4275 void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
4277 struct LevelInfo_SP *level_sp = level->native_sp_level;
4278 LevelInfoType *header = &level_sp->header;
4281 level_sp->width = level->fieldx;
4282 level_sp->height = level->fieldy;
4284 for (x = 0; x < level->fieldx; x++)
4285 for (y = 0; y < level->fieldy; y++)
4286 level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
4288 header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
4290 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
4291 header->LevelTitle[i] = level->name[i];
4292 /* !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!! */
4294 header->InfotronsNeeded = level->gems_needed;
4296 header->SpecialPortCount = 0;
4298 for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
4300 boolean gravity_port_found = FALSE;
4301 boolean gravity_port_valid = FALSE;
4302 int gravity_port_flag;
4303 int gravity_port_base_element;
4304 int element = level->field[x][y];
4306 if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
4307 element <= EL_SP_GRAVITY_ON_PORT_UP)
4309 gravity_port_found = TRUE;
4310 gravity_port_valid = TRUE;
4311 gravity_port_flag = 1;
4312 gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
4314 else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
4315 element <= EL_SP_GRAVITY_OFF_PORT_UP)
4317 gravity_port_found = TRUE;
4318 gravity_port_valid = TRUE;
4319 gravity_port_flag = 0;
4320 gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
4322 else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
4323 element <= EL_SP_GRAVITY_PORT_UP)
4325 /* change R'n'D style gravity inverting special port to normal port
4326 (there are no gravity inverting ports in native Supaplex engine) */
4328 gravity_port_found = TRUE;
4329 gravity_port_valid = FALSE;
4330 gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
4333 if (gravity_port_found)
4335 if (gravity_port_valid &&
4336 header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
4338 SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
4340 port->PortLocation = (y * level->fieldx + x) * 2;
4341 port->Gravity = gravity_port_flag;
4343 element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
4345 header->SpecialPortCount++;
4349 /* change special gravity port to normal port */
4351 element += EL_SP_PORT_RIGHT - gravity_port_base_element;
4354 level_sp->playfield[x][y] = element - EL_SP_START;
4359 void CopyNativeLevel_SP_to_RND(struct LevelInfo *level)
4361 struct LevelInfo_SP *level_sp = level->native_sp_level;
4362 LevelInfoType *header = &level_sp->header;
4365 level->fieldx = level_sp->width;
4366 level->fieldy = level_sp->height;
4368 for (x = 0; x < level->fieldx; x++)
4370 for (y = 0; y < level->fieldy; y++)
4372 int element_old = level_sp->playfield[x][y];
4373 int element_new = getMappedElement(map_element_SP_to_RND(element_old));
4375 if (element_new == EL_UNKNOWN)
4376 Error(ERR_WARN, "invalid element %d at position %d, %d",
4379 level->field[x][y] = element_new;
4383 for (i = 0; i < MAX_PLAYERS; i++)
4384 level->initial_player_gravity[i] =
4385 (header->InitialGravity == 1 ? TRUE : FALSE);
4387 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
4388 level->name[i] = header->LevelTitle[i];
4389 level->name[SP_LEVEL_NAME_LEN] = '\0';
4391 level->gems_needed = header->InfotronsNeeded;
4393 for (i = 0; i < header->SpecialPortCount; i++)
4395 SpecialPortType *port = &header->SpecialPort[i];
4396 int port_location = port->PortLocation;
4397 int gravity = port->Gravity;
4398 int port_x, port_y, port_element;
4400 port_x = (port_location / 2) % level->fieldx;
4401 port_y = (port_location / 2) / level->fieldx;
4403 if (port_x < 0 || port_x >= level->fieldx ||
4404 port_y < 0 || port_y >= level->fieldy)
4406 Error(ERR_WARN, "special port position (%d, %d) out of bounds",
4412 port_element = level->field[port_x][port_y];
4414 if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
4415 port_element > EL_SP_GRAVITY_PORT_UP)
4417 Error(ERR_WARN, "no special port at position (%d, %d)", port_x, port_y);
4422 /* change previous (wrong) gravity inverting special port to either
4423 gravity enabling special port or gravity disabling special port */
4424 level->field[port_x][port_y] +=
4425 (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
4426 EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
4429 /* change special gravity ports without database entries to normal ports */
4430 for (x = 0; x < level->fieldx; x++)
4431 for (y = 0; y < level->fieldy; y++)
4432 if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
4433 level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
4434 level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
4436 level->time = 0; /* no time limit */
4437 level->amoeba_speed = 0;
4438 level->time_magic_wall = 0;
4439 level->time_wheel = 0;
4440 level->amoeba_content = EL_EMPTY;
4443 /* original Supaplex does not use score values -- use default values */
4445 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
4446 level->score[i] = 0;
4449 /* there are no yamyams in supaplex levels */
4450 for (i = 0; i < level->num_yamyam_contents; i++)
4451 for (x = 0; x < 3; x++)
4452 for (y = 0; y < 3; y++)
4453 level->yamyam_content[i].e[x][y] = EL_EMPTY;
4456 static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
4458 struct LevelInfo_SP *level_sp = level->native_sp_level;
4459 struct DemoInfo_SP *demo = &level_sp->demo;
4462 /* always start with reliable default values */
4463 demo->is_available = FALSE;
4466 if (TAPE_IS_EMPTY(tape))
4469 demo->level_nr = tape.level_nr; /* (currently not used) */
4471 level_sp->header.DemoRandomSeed = tape.random_seed;
4474 for (i = 0; i < tape.length; i++)
4476 int demo_action = map_key_RND_to_SP(tape.pos[i].action[0]);
4477 int demo_repeat = tape.pos[i].delay;
4479 for (j = 0; j < demo_repeat / 16; j++)
4480 demo->data[demo->length++] = 0xf0 | demo_action;
4482 if (demo_repeat % 16)
4483 demo->data[demo->length++] = ((demo_repeat % 16 - 1) << 4) | demo_action;
4486 demo->data[demo->length++] = 0xff;
4488 demo->is_available = TRUE;
4491 static void setTapeInfoToDefaults();
4493 static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
4495 struct LevelInfo_SP *level_sp = level->native_sp_level;
4496 struct DemoInfo_SP *demo = &level_sp->demo;
4497 char *filename = level->file_info.filename;
4500 /* always start with reliable default values */
4501 setTapeInfoToDefaults();
4503 if (!demo->is_available)
4506 tape.level_nr = demo->level_nr; /* (currently not used) */
4507 tape.length = demo->length - 1; /* without "end of demo" byte */
4508 tape.random_seed = level_sp->header.DemoRandomSeed;
4510 TapeSetDateFromEpochSeconds(getFileTimestampEpochSeconds(filename));
4512 for (i = 0; i < demo->length - 1; i++)
4514 int demo_action = demo->data[i] & 0x0f;
4515 int demo_repeat = (demo->data[i] & 0xf0) >> 4;
4517 tape.pos[i].action[0] = map_key_SP_to_RND(demo_action);
4518 tape.pos[i].delay = demo_repeat + 1;
4521 tape.length_seconds = GetTapeLength();
4525 /* ------------------------------------------------------------------------- */
4526 /* functions for loading DC level */
4527 /* ------------------------------------------------------------------------- */
4529 #define DC_LEVEL_HEADER_SIZE 344
4531 unsigned short getDecodedWord_DC(unsigned short data_encoded, boolean init)
4533 static int last_data_encoded;
4537 int diff_hi, diff_lo;
4538 int data_hi, data_lo;
4539 unsigned short data_decoded;
4543 last_data_encoded = 0;
4550 diff = data_encoded - last_data_encoded;
4551 diff_hi = diff & ~0xff;
4552 diff_lo = diff & 0xff;
4556 data_hi = diff_hi - (offset1 << 8) + (offset2 & 0xff00);
4557 data_lo = (diff_lo + (data_hi >> 16)) & 0x00ff;
4558 data_hi = data_hi & 0xff00;
4560 data_decoded = data_hi | data_lo;
4562 last_data_encoded = data_encoded;
4564 offset1 = (offset1 + 1) % 31;
4565 offset2 = offset2 & 0xff;
4567 return data_decoded;
4570 int getMappedElement_DC(int element)
4578 /* 0x0117 - 0x036e: (?) */
4581 /* 0x042d - 0x0684: (?) */
4597 element = EL_CRYSTAL;
4600 case 0x0e77: /* quicksand (boulder) */
4601 element = EL_QUICKSAND_FAST_FULL;
4604 case 0x0e99: /* slow quicksand (boulder) */
4605 element = EL_QUICKSAND_FULL;
4609 element = EL_EM_EXIT_OPEN;
4613 element = EL_EM_EXIT_CLOSED;
4617 element = EL_EM_STEEL_EXIT_OPEN;
4621 element = EL_EM_STEEL_EXIT_CLOSED;
4624 case 0x0f4f: /* dynamite (lit 1) */
4625 element = EL_EM_DYNAMITE_ACTIVE;
4628 case 0x0f57: /* dynamite (lit 2) */
4629 element = EL_EM_DYNAMITE_ACTIVE;
4632 case 0x0f5f: /* dynamite (lit 3) */
4633 element = EL_EM_DYNAMITE_ACTIVE;
4636 case 0x0f67: /* dynamite (lit 4) */
4637 element = EL_EM_DYNAMITE_ACTIVE;
4644 element = EL_AMOEBA_WET;
4648 element = EL_AMOEBA_DROP;
4652 element = EL_DC_MAGIC_WALL;
4656 element = EL_SPACESHIP_UP;
4660 element = EL_SPACESHIP_DOWN;
4664 element = EL_SPACESHIP_LEFT;
4668 element = EL_SPACESHIP_RIGHT;
4672 element = EL_BUG_UP;
4676 element = EL_BUG_DOWN;
4680 element = EL_BUG_LEFT;
4684 element = EL_BUG_RIGHT;
4688 element = EL_MOLE_UP;
4692 element = EL_MOLE_DOWN;
4696 element = EL_MOLE_LEFT;
4700 element = EL_MOLE_RIGHT;
4708 element = EL_YAMYAM;
4712 element = EL_SWITCHGATE_OPEN;
4716 element = EL_SWITCHGATE_CLOSED;
4720 element = EL_DC_SWITCHGATE_SWITCH_UP;
4724 element = EL_TIMEGATE_CLOSED;
4727 case 0x144c: /* conveyor belt switch (green) */
4728 element = EL_CONVEYOR_BELT_3_SWITCH_MIDDLE;
4731 case 0x144f: /* conveyor belt switch (red) */
4732 element = EL_CONVEYOR_BELT_1_SWITCH_MIDDLE;
4735 case 0x1452: /* conveyor belt switch (blue) */
4736 element = EL_CONVEYOR_BELT_4_SWITCH_MIDDLE;
4740 element = EL_CONVEYOR_BELT_3_MIDDLE;
4744 element = EL_CONVEYOR_BELT_3_LEFT;
4748 element = EL_CONVEYOR_BELT_3_RIGHT;
4752 element = EL_CONVEYOR_BELT_1_MIDDLE;
4756 element = EL_CONVEYOR_BELT_1_LEFT;
4760 element = EL_CONVEYOR_BELT_1_RIGHT;
4764 element = EL_CONVEYOR_BELT_4_MIDDLE;
4768 element = EL_CONVEYOR_BELT_4_LEFT;
4772 element = EL_CONVEYOR_BELT_4_RIGHT;
4776 element = EL_EXPANDABLE_WALL_HORIZONTAL;
4780 element = EL_EXPANDABLE_WALL_VERTICAL;
4784 element = EL_EXPANDABLE_WALL_ANY;
4787 case 0x14ce: /* growing steel wall (left/right) */
4788 element = EL_EXPANDABLE_STEELWALL_HORIZONTAL;
4791 case 0x14df: /* growing steel wall (up/down) */
4792 element = EL_EXPANDABLE_STEELWALL_VERTICAL;
4795 case 0x14e8: /* growing steel wall (up/down/left/right) */
4796 element = EL_EXPANDABLE_STEELWALL_ANY;
4800 element = EL_SHIELD_DEADLY;
4804 element = EL_EXTRA_TIME;
4812 element = EL_EMPTY_SPACE;
4815 case 0x1578: /* quicksand (empty) */
4816 element = EL_QUICKSAND_FAST_EMPTY;
4819 case 0x1579: /* slow quicksand (empty) */
4820 element = EL_QUICKSAND_EMPTY;
4823 /* 0x157c - 0x158b: */
4826 /* 0x1590 - 0x159f: */
4827 /* EL_DC_LANDMINE */
4830 element = EL_EM_DYNAMITE;
4833 case 0x15a1: /* key (red) */
4834 element = EL_EM_KEY_1;
4837 case 0x15a2: /* key (yellow) */
4838 element = EL_EM_KEY_2;
4841 case 0x15a3: /* key (blue) */
4842 element = EL_EM_KEY_4;
4845 case 0x15a4: /* key (green) */
4846 element = EL_EM_KEY_3;
4849 case 0x15a5: /* key (white) */
4850 element = EL_DC_KEY_WHITE;
4854 element = EL_WALL_SLIPPERY;
4861 case 0x15a8: /* wall (not round) */
4865 case 0x15a9: /* (blue) */
4866 element = EL_CHAR_A;
4869 case 0x15aa: /* (blue) */
4870 element = EL_CHAR_B;
4873 case 0x15ab: /* (blue) */
4874 element = EL_CHAR_C;
4877 case 0x15ac: /* (blue) */
4878 element = EL_CHAR_D;
4881 case 0x15ad: /* (blue) */
4882 element = EL_CHAR_E;
4885 case 0x15ae: /* (blue) */
4886 element = EL_CHAR_F;
4889 case 0x15af: /* (blue) */
4890 element = EL_CHAR_G;
4893 case 0x15b0: /* (blue) */
4894 element = EL_CHAR_H;
4897 case 0x15b1: /* (blue) */
4898 element = EL_CHAR_I;
4901 case 0x15b2: /* (blue) */
4902 element = EL_CHAR_J;
4905 case 0x15b3: /* (blue) */
4906 element = EL_CHAR_K;
4909 case 0x15b4: /* (blue) */
4910 element = EL_CHAR_L;
4913 case 0x15b5: /* (blue) */
4914 element = EL_CHAR_M;
4917 case 0x15b6: /* (blue) */
4918 element = EL_CHAR_N;
4921 case 0x15b7: /* (blue) */
4922 element = EL_CHAR_O;
4925 case 0x15b8: /* (blue) */
4926 element = EL_CHAR_P;
4929 case 0x15b9: /* (blue) */
4930 element = EL_CHAR_Q;
4933 case 0x15ba: /* (blue) */
4934 element = EL_CHAR_R;
4937 case 0x15bb: /* (blue) */
4938 element = EL_CHAR_S;
4941 case 0x15bc: /* (blue) */
4942 element = EL_CHAR_T;
4945 case 0x15bd: /* (blue) */
4946 element = EL_CHAR_U;
4949 case 0x15be: /* (blue) */
4950 element = EL_CHAR_V;
4953 case 0x15bf: /* (blue) */
4954 element = EL_CHAR_W;
4957 case 0x15c0: /* (blue) */
4958 element = EL_CHAR_X;
4961 case 0x15c1: /* (blue) */
4962 element = EL_CHAR_Y;
4965 case 0x15c2: /* (blue) */
4966 element = EL_CHAR_Z;
4969 case 0x15c3: /* (blue) */
4970 element = EL_CHAR_AUMLAUT;
4973 case 0x15c4: /* (blue) */
4974 element = EL_CHAR_OUMLAUT;
4977 case 0x15c5: /* (blue) */
4978 element = EL_CHAR_UUMLAUT;
4981 case 0x15c6: /* (blue) */
4982 element = EL_CHAR_0;
4985 case 0x15c7: /* (blue) */
4986 element = EL_CHAR_1;
4989 case 0x15c8: /* (blue) */
4990 element = EL_CHAR_2;
4993 case 0x15c9: /* (blue) */
4994 element = EL_CHAR_3;
4997 case 0x15ca: /* (blue) */
4998 element = EL_CHAR_4;
5001 case 0x15cb: /* (blue) */
5002 element = EL_CHAR_5;
5005 case 0x15cc: /* (blue) */
5006 element = EL_CHAR_6;
5009 case 0x15cd: /* (blue) */
5010 element = EL_CHAR_7;
5013 case 0x15ce: /* (blue) */
5014 element = EL_CHAR_8;
5017 case 0x15cf: /* (blue) */
5018 element = EL_CHAR_9;
5021 case 0x15d0: /* (blue) */
5022 element = EL_CHAR_PERIOD;
5025 case 0x15d1: /* (blue) */
5026 element = EL_CHAR_EXCLAM;
5029 case 0x15d2: /* (blue) */
5030 element = EL_CHAR_COLON;
5033 case 0x15d3: /* (blue) */
5034 element = EL_CHAR_LESS;
5037 case 0x15d4: /* (blue) */
5038 element = EL_CHAR_GREATER;
5041 case 0x15d5: /* (blue) */
5042 element = EL_CHAR_QUESTION;
5045 case 0x15d6: /* (blue) */
5046 element = EL_CHAR_COPYRIGHT;
5049 case 0x15d7: /* (blue) */
5050 element = EL_CHAR_UP;
5053 case 0x15d8: /* (blue) */
5054 element = EL_CHAR_DOWN;
5057 case 0x15d9: /* (blue) */
5058 element = EL_CHAR_BUTTON;
5061 case 0x15da: /* (blue) */
5062 element = EL_CHAR_PLUS;
5065 case 0x15db: /* (blue) */
5066 element = EL_CHAR_MINUS;
5069 case 0x15dc: /* (blue) */
5070 element = EL_CHAR_APOSTROPHE;
5073 case 0x15dd: /* (blue) */
5074 element = EL_CHAR_PARENLEFT;
5077 case 0x15de: /* (blue) */
5078 element = EL_CHAR_PARENRIGHT;
5081 case 0x15df: /* (green) */
5082 element = EL_CHAR_A;
5085 case 0x15e0: /* (green) */
5086 element = EL_CHAR_B;
5089 case 0x15e1: /* (green) */
5090 element = EL_CHAR_C;
5093 case 0x15e2: /* (green) */
5094 element = EL_CHAR_D;
5097 case 0x15e3: /* (green) */
5098 element = EL_CHAR_E;
5101 case 0x15e4: /* (green) */
5102 element = EL_CHAR_F;
5105 case 0x15e5: /* (green) */
5106 element = EL_CHAR_G;
5109 case 0x15e6: /* (green) */
5110 element = EL_CHAR_H;
5113 case 0x15e7: /* (green) */
5114 element = EL_CHAR_I;
5117 case 0x15e8: /* (green) */
5118 element = EL_CHAR_J;
5121 case 0x15e9: /* (green) */
5122 element = EL_CHAR_K;
5125 case 0x15ea: /* (green) */
5126 element = EL_CHAR_L;
5129 case 0x15eb: /* (green) */
5130 element = EL_CHAR_M;
5133 case 0x15ec: /* (green) */
5134 element = EL_CHAR_N;
5137 case 0x15ed: /* (green) */
5138 element = EL_CHAR_O;
5141 case 0x15ee: /* (green) */
5142 element = EL_CHAR_P;
5145 case 0x15ef: /* (green) */
5146 element = EL_CHAR_Q;
5149 case 0x15f0: /* (green) */
5150 element = EL_CHAR_R;
5153 case 0x15f1: /* (green) */
5154 element = EL_CHAR_S;
5157 case 0x15f2: /* (green) */
5158 element = EL_CHAR_T;
5161 case 0x15f3: /* (green) */
5162 element = EL_CHAR_U;
5165 case 0x15f4: /* (green) */
5166 element = EL_CHAR_V;
5169 case 0x15f5: /* (green) */
5170 element = EL_CHAR_W;
5173 case 0x15f6: /* (green) */
5174 element = EL_CHAR_X;
5177 case 0x15f7: /* (green) */
5178 element = EL_CHAR_Y;
5181 case 0x15f8: /* (green) */
5182 element = EL_CHAR_Z;
5185 case 0x15f9: /* (green) */
5186 element = EL_CHAR_AUMLAUT;
5189 case 0x15fa: /* (green) */
5190 element = EL_CHAR_OUMLAUT;
5193 case 0x15fb: /* (green) */
5194 element = EL_CHAR_UUMLAUT;
5197 case 0x15fc: /* (green) */
5198 element = EL_CHAR_0;
5201 case 0x15fd: /* (green) */
5202 element = EL_CHAR_1;
5205 case 0x15fe: /* (green) */
5206 element = EL_CHAR_2;
5209 case 0x15ff: /* (green) */
5210 element = EL_CHAR_3;
5213 case 0x1600: /* (green) */
5214 element = EL_CHAR_4;
5217 case 0x1601: /* (green) */
5218 element = EL_CHAR_5;
5221 case 0x1602: /* (green) */
5222 element = EL_CHAR_6;
5225 case 0x1603: /* (green) */
5226 element = EL_CHAR_7;
5229 case 0x1604: /* (green) */
5230 element = EL_CHAR_8;
5233 case 0x1605: /* (green) */
5234 element = EL_CHAR_9;
5237 case 0x1606: /* (green) */
5238 element = EL_CHAR_PERIOD;
5241 case 0x1607: /* (green) */
5242 element = EL_CHAR_EXCLAM;
5245 case 0x1608: /* (green) */
5246 element = EL_CHAR_COLON;
5249 case 0x1609: /* (green) */
5250 element = EL_CHAR_LESS;
5253 case 0x160a: /* (green) */
5254 element = EL_CHAR_GREATER;
5257 case 0x160b: /* (green) */
5258 element = EL_CHAR_QUESTION;
5261 case 0x160c: /* (green) */
5262 element = EL_CHAR_COPYRIGHT;
5265 case 0x160d: /* (green) */
5266 element = EL_CHAR_UP;
5269 case 0x160e: /* (green) */
5270 element = EL_CHAR_DOWN;
5273 case 0x160f: /* (green) */
5274 element = EL_CHAR_BUTTON;
5277 case 0x1610: /* (green) */
5278 element = EL_CHAR_PLUS;
5281 case 0x1611: /* (green) */
5282 element = EL_CHAR_MINUS;
5285 case 0x1612: /* (green) */
5286 element = EL_CHAR_APOSTROPHE;
5289 case 0x1613: /* (green) */
5290 element = EL_CHAR_PARENLEFT;
5293 case 0x1614: /* (green) */
5294 element = EL_CHAR_PARENRIGHT;
5297 case 0x1615: /* (blue steel) */
5298 element = EL_STEEL_CHAR_A;
5301 case 0x1616: /* (blue steel) */
5302 element = EL_STEEL_CHAR_B;
5305 case 0x1617: /* (blue steel) */
5306 element = EL_STEEL_CHAR_C;
5309 case 0x1618: /* (blue steel) */
5310 element = EL_STEEL_CHAR_D;
5313 case 0x1619: /* (blue steel) */
5314 element = EL_STEEL_CHAR_E;
5317 case 0x161a: /* (blue steel) */
5318 element = EL_STEEL_CHAR_F;
5321 case 0x161b: /* (blue steel) */
5322 element = EL_STEEL_CHAR_G;
5325 case 0x161c: /* (blue steel) */
5326 element = EL_STEEL_CHAR_H;
5329 case 0x161d: /* (blue steel) */
5330 element = EL_STEEL_CHAR_I;
5333 case 0x161e: /* (blue steel) */
5334 element = EL_STEEL_CHAR_J;
5337 case 0x161f: /* (blue steel) */
5338 element = EL_STEEL_CHAR_K;
5341 case 0x1620: /* (blue steel) */
5342 element = EL_STEEL_CHAR_L;
5345 case 0x1621: /* (blue steel) */
5346 element = EL_STEEL_CHAR_M;
5349 case 0x1622: /* (blue steel) */
5350 element = EL_STEEL_CHAR_N;
5353 case 0x1623: /* (blue steel) */
5354 element = EL_STEEL_CHAR_O;
5357 case 0x1624: /* (blue steel) */
5358 element = EL_STEEL_CHAR_P;
5361 case 0x1625: /* (blue steel) */
5362 element = EL_STEEL_CHAR_Q;
5365 case 0x1626: /* (blue steel) */
5366 element = EL_STEEL_CHAR_R;
5369 case 0x1627: /* (blue steel) */
5370 element = EL_STEEL_CHAR_S;
5373 case 0x1628: /* (blue steel) */
5374 element = EL_STEEL_CHAR_T;
5377 case 0x1629: /* (blue steel) */
5378 element = EL_STEEL_CHAR_U;
5381 case 0x162a: /* (blue steel) */
5382 element = EL_STEEL_CHAR_V;
5385 case 0x162b: /* (blue steel) */
5386 element = EL_STEEL_CHAR_W;
5389 case 0x162c: /* (blue steel) */
5390 element = EL_STEEL_CHAR_X;
5393 case 0x162d: /* (blue steel) */
5394 element = EL_STEEL_CHAR_Y;
5397 case 0x162e: /* (blue steel) */
5398 element = EL_STEEL_CHAR_Z;
5401 case 0x162f: /* (blue steel) */
5402 element = EL_STEEL_CHAR_AUMLAUT;
5405 case 0x1630: /* (blue steel) */
5406 element = EL_STEEL_CHAR_OUMLAUT;
5409 case 0x1631: /* (blue steel) */
5410 element = EL_STEEL_CHAR_UUMLAUT;
5413 case 0x1632: /* (blue steel) */
5414 element = EL_STEEL_CHAR_0;
5417 case 0x1633: /* (blue steel) */
5418 element = EL_STEEL_CHAR_1;
5421 case 0x1634: /* (blue steel) */
5422 element = EL_STEEL_CHAR_2;
5425 case 0x1635: /* (blue steel) */
5426 element = EL_STEEL_CHAR_3;
5429 case 0x1636: /* (blue steel) */
5430 element = EL_STEEL_CHAR_4;
5433 case 0x1637: /* (blue steel) */
5434 element = EL_STEEL_CHAR_5;
5437 case 0x1638: /* (blue steel) */
5438 element = EL_STEEL_CHAR_6;
5441 case 0x1639: /* (blue steel) */
5442 element = EL_STEEL_CHAR_7;
5445 case 0x163a: /* (blue steel) */
5446 element = EL_STEEL_CHAR_8;
5449 case 0x163b: /* (blue steel) */
5450 element = EL_STEEL_CHAR_9;
5453 case 0x163c: /* (blue steel) */
5454 element = EL_STEEL_CHAR_PERIOD;
5457 case 0x163d: /* (blue steel) */
5458 element = EL_STEEL_CHAR_EXCLAM;
5461 case 0x163e: /* (blue steel) */
5462 element = EL_STEEL_CHAR_COLON;
5465 case 0x163f: /* (blue steel) */
5466 element = EL_STEEL_CHAR_LESS;
5469 case 0x1640: /* (blue steel) */
5470 element = EL_STEEL_CHAR_GREATER;
5473 case 0x1641: /* (blue steel) */
5474 element = EL_STEEL_CHAR_QUESTION;
5477 case 0x1642: /* (blue steel) */
5478 element = EL_STEEL_CHAR_COPYRIGHT;
5481 case 0x1643: /* (blue steel) */
5482 element = EL_STEEL_CHAR_UP;
5485 case 0x1644: /* (blue steel) */
5486 element = EL_STEEL_CHAR_DOWN;
5489 case 0x1645: /* (blue steel) */
5490 element = EL_STEEL_CHAR_BUTTON;
5493 case 0x1646: /* (blue steel) */
5494 element = EL_STEEL_CHAR_PLUS;
5497 case 0x1647: /* (blue steel) */
5498 element = EL_STEEL_CHAR_MINUS;
5501 case 0x1648: /* (blue steel) */
5502 element = EL_STEEL_CHAR_APOSTROPHE;
5505 case 0x1649: /* (blue steel) */
5506 element = EL_STEEL_CHAR_PARENLEFT;
5509 case 0x164a: /* (blue steel) */
5510 element = EL_STEEL_CHAR_PARENRIGHT;
5513 case 0x164b: /* (green steel) */
5514 element = EL_STEEL_CHAR_A;
5517 case 0x164c: /* (green steel) */
5518 element = EL_STEEL_CHAR_B;
5521 case 0x164d: /* (green steel) */
5522 element = EL_STEEL_CHAR_C;
5525 case 0x164e: /* (green steel) */
5526 element = EL_STEEL_CHAR_D;
5529 case 0x164f: /* (green steel) */
5530 element = EL_STEEL_CHAR_E;
5533 case 0x1650: /* (green steel) */
5534 element = EL_STEEL_CHAR_F;
5537 case 0x1651: /* (green steel) */
5538 element = EL_STEEL_CHAR_G;
5541 case 0x1652: /* (green steel) */
5542 element = EL_STEEL_CHAR_H;
5545 case 0x1653: /* (green steel) */
5546 element = EL_STEEL_CHAR_I;
5549 case 0x1654: /* (green steel) */
5550 element = EL_STEEL_CHAR_J;
5553 case 0x1655: /* (green steel) */
5554 element = EL_STEEL_CHAR_K;
5557 case 0x1656: /* (green steel) */
5558 element = EL_STEEL_CHAR_L;
5561 case 0x1657: /* (green steel) */
5562 element = EL_STEEL_CHAR_M;
5565 case 0x1658: /* (green steel) */
5566 element = EL_STEEL_CHAR_N;
5569 case 0x1659: /* (green steel) */
5570 element = EL_STEEL_CHAR_O;
5573 case 0x165a: /* (green steel) */
5574 element = EL_STEEL_CHAR_P;
5577 case 0x165b: /* (green steel) */
5578 element = EL_STEEL_CHAR_Q;
5581 case 0x165c: /* (green steel) */
5582 element = EL_STEEL_CHAR_R;
5585 case 0x165d: /* (green steel) */
5586 element = EL_STEEL_CHAR_S;
5589 case 0x165e: /* (green steel) */
5590 element = EL_STEEL_CHAR_T;
5593 case 0x165f: /* (green steel) */
5594 element = EL_STEEL_CHAR_U;
5597 case 0x1660: /* (green steel) */
5598 element = EL_STEEL_CHAR_V;
5601 case 0x1661: /* (green steel) */
5602 element = EL_STEEL_CHAR_W;
5605 case 0x1662: /* (green steel) */
5606 element = EL_STEEL_CHAR_X;
5609 case 0x1663: /* (green steel) */
5610 element = EL_STEEL_CHAR_Y;
5613 case 0x1664: /* (green steel) */
5614 element = EL_STEEL_CHAR_Z;
5617 case 0x1665: /* (green steel) */
5618 element = EL_STEEL_CHAR_AUMLAUT;
5621 case 0x1666: /* (green steel) */
5622 element = EL_STEEL_CHAR_OUMLAUT;
5625 case 0x1667: /* (green steel) */
5626 element = EL_STEEL_CHAR_UUMLAUT;
5629 case 0x1668: /* (green steel) */
5630 element = EL_STEEL_CHAR_0;
5633 case 0x1669: /* (green steel) */
5634 element = EL_STEEL_CHAR_1;
5637 case 0x166a: /* (green steel) */
5638 element = EL_STEEL_CHAR_2;
5641 case 0x166b: /* (green steel) */
5642 element = EL_STEEL_CHAR_3;
5645 case 0x166c: /* (green steel) */
5646 element = EL_STEEL_CHAR_4;
5649 case 0x166d: /* (green steel) */
5650 element = EL_STEEL_CHAR_5;
5653 case 0x166e: /* (green steel) */
5654 element = EL_STEEL_CHAR_6;
5657 case 0x166f: /* (green steel) */
5658 element = EL_STEEL_CHAR_7;
5661 case 0x1670: /* (green steel) */
5662 element = EL_STEEL_CHAR_8;
5665 case 0x1671: /* (green steel) */
5666 element = EL_STEEL_CHAR_9;
5669 case 0x1672: /* (green steel) */
5670 element = EL_STEEL_CHAR_PERIOD;
5673 case 0x1673: /* (green steel) */
5674 element = EL_STEEL_CHAR_EXCLAM;
5677 case 0x1674: /* (green steel) */
5678 element = EL_STEEL_CHAR_COLON;
5681 case 0x1675: /* (green steel) */
5682 element = EL_STEEL_CHAR_LESS;
5685 case 0x1676: /* (green steel) */
5686 element = EL_STEEL_CHAR_GREATER;
5689 case 0x1677: /* (green steel) */
5690 element = EL_STEEL_CHAR_QUESTION;
5693 case 0x1678: /* (green steel) */
5694 element = EL_STEEL_CHAR_COPYRIGHT;
5697 case 0x1679: /* (green steel) */
5698 element = EL_STEEL_CHAR_UP;
5701 case 0x167a: /* (green steel) */
5702 element = EL_STEEL_CHAR_DOWN;
5705 case 0x167b: /* (green steel) */
5706 element = EL_STEEL_CHAR_BUTTON;
5709 case 0x167c: /* (green steel) */
5710 element = EL_STEEL_CHAR_PLUS;
5713 case 0x167d: /* (green steel) */
5714 element = EL_STEEL_CHAR_MINUS;
5717 case 0x167e: /* (green steel) */
5718 element = EL_STEEL_CHAR_APOSTROPHE;
5721 case 0x167f: /* (green steel) */
5722 element = EL_STEEL_CHAR_PARENLEFT;
5725 case 0x1680: /* (green steel) */
5726 element = EL_STEEL_CHAR_PARENRIGHT;
5729 case 0x1681: /* gate (red) */
5730 element = EL_EM_GATE_1;
5733 case 0x1682: /* secret gate (red) */
5734 element = EL_GATE_1_GRAY;
5737 case 0x1683: /* gate (yellow) */
5738 element = EL_EM_GATE_2;
5741 case 0x1684: /* secret gate (yellow) */
5742 element = EL_GATE_2_GRAY;
5745 case 0x1685: /* gate (blue) */
5746 element = EL_EM_GATE_4;
5749 case 0x1686: /* secret gate (blue) */
5750 element = EL_GATE_4_GRAY;
5753 case 0x1687: /* gate (green) */
5754 element = EL_EM_GATE_3;
5757 case 0x1688: /* secret gate (green) */
5758 element = EL_GATE_3_GRAY;
5761 case 0x1689: /* gate (white) */
5762 element = EL_DC_GATE_WHITE;
5765 case 0x168a: /* secret gate (white) */
5766 element = EL_DC_GATE_WHITE_GRAY;
5769 case 0x168b: /* secret gate (no key) */
5770 element = EL_DC_GATE_FAKE_GRAY;
5774 element = EL_ROBOT_WHEEL;
5778 element = EL_DC_TIMEGATE_SWITCH;
5782 element = EL_ACID_POOL_BOTTOM;
5786 element = EL_ACID_POOL_TOPLEFT;
5790 element = EL_ACID_POOL_TOPRIGHT;
5794 element = EL_ACID_POOL_BOTTOMLEFT;
5798 element = EL_ACID_POOL_BOTTOMRIGHT;
5802 element = EL_STEELWALL;
5806 element = EL_STEELWALL_SLIPPERY;
5809 case 0x1695: /* steel wall (not round) */
5810 element = EL_STEELWALL;
5813 case 0x1696: /* steel wall (left) */
5814 element = EL_DC_STEELWALL_1_LEFT;
5817 case 0x1697: /* steel wall (bottom) */
5818 element = EL_DC_STEELWALL_1_BOTTOM;
5821 case 0x1698: /* steel wall (right) */
5822 element = EL_DC_STEELWALL_1_RIGHT;
5825 case 0x1699: /* steel wall (top) */
5826 element = EL_DC_STEELWALL_1_TOP;
5829 case 0x169a: /* steel wall (left/bottom) */
5830 element = EL_DC_STEELWALL_1_BOTTOMLEFT;
5833 case 0x169b: /* steel wall (right/bottom) */
5834 element = EL_DC_STEELWALL_1_BOTTOMRIGHT;
5837 case 0x169c: /* steel wall (right/top) */
5838 element = EL_DC_STEELWALL_1_TOPRIGHT;
5841 case 0x169d: /* steel wall (left/top) */
5842 element = EL_DC_STEELWALL_1_TOPLEFT;
5845 case 0x169e: /* steel wall (right/bottom small) */
5846 element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2;
5849 case 0x169f: /* steel wall (left/bottom small) */
5850 element = EL_DC_STEELWALL_1_BOTTOMLEFT_2;
5853 case 0x16a0: /* steel wall (right/top small) */
5854 element = EL_DC_STEELWALL_1_TOPRIGHT_2;
5857 case 0x16a1: /* steel wall (left/top small) */
5858 element = EL_DC_STEELWALL_1_TOPLEFT_2;
5861 case 0x16a2: /* steel wall (left/right) */
5862 element = EL_DC_STEELWALL_1_VERTICAL;
5865 case 0x16a3: /* steel wall (top/bottom) */
5866 element = EL_DC_STEELWALL_1_HORIZONTAL;
5869 case 0x16a4: /* steel wall 2 (left end) */
5870 element = EL_DC_STEELWALL_2_LEFT;
5873 case 0x16a5: /* steel wall 2 (right end) */
5874 element = EL_DC_STEELWALL_2_RIGHT;
5877 case 0x16a6: /* steel wall 2 (top end) */
5878 element = EL_DC_STEELWALL_2_TOP;
5881 case 0x16a7: /* steel wall 2 (bottom end) */
5882 element = EL_DC_STEELWALL_2_BOTTOM;
5885 case 0x16a8: /* steel wall 2 (left/right) */
5886 element = EL_DC_STEELWALL_2_HORIZONTAL;
5889 case 0x16a9: /* steel wall 2 (up/down) */
5890 element = EL_DC_STEELWALL_2_VERTICAL;
5893 case 0x16aa: /* steel wall 2 (mid) */
5894 element = EL_DC_STEELWALL_2_MIDDLE;
5898 element = EL_SIGN_EXCLAMATION;
5902 element = EL_SIGN_RADIOACTIVITY;
5906 element = EL_SIGN_STOP;
5910 element = EL_SIGN_WHEELCHAIR;
5914 element = EL_SIGN_PARKING;
5918 element = EL_SIGN_NO_ENTRY;
5922 element = EL_SIGN_HEART;
5926 element = EL_SIGN_GIVE_WAY;
5930 element = EL_SIGN_ENTRY_FORBIDDEN;
5934 element = EL_SIGN_EMERGENCY_EXIT;
5938 element = EL_SIGN_YIN_YANG;
5942 element = EL_WALL_EMERALD;
5946 element = EL_WALL_DIAMOND;
5950 element = EL_WALL_PEARL;
5954 element = EL_WALL_CRYSTAL;
5958 element = EL_INVISIBLE_WALL;
5962 element = EL_INVISIBLE_STEELWALL;
5965 /* 0x16bc - 0x16cb: */
5966 /* EL_INVISIBLE_SAND */
5969 element = EL_LIGHT_SWITCH;
5973 element = EL_ENVELOPE_1;
5977 if (element >= 0x0117 && element <= 0x036e) /* (?) */
5978 element = EL_DIAMOND;
5979 else if (element >= 0x042d && element <= 0x0684) /* (?) */
5980 element = EL_EMERALD;
5981 else if (element >= 0x157c && element <= 0x158b)
5983 else if (element >= 0x1590 && element <= 0x159f)
5984 element = EL_DC_LANDMINE;
5985 else if (element >= 0x16bc && element <= 0x16cb)
5986 element = EL_INVISIBLE_SAND;
5989 Error(ERR_WARN, "unknown Diamond Caves element 0x%04x", element);
5990 element = EL_UNKNOWN;
5995 return getMappedElement(element);
6000 static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level,
6003 byte header[DC_LEVEL_HEADER_SIZE];
6005 int envelope_header_pos = 62;
6006 int envelope_content_pos = 94;
6007 int level_name_pos = 251;
6008 int level_author_pos = 292;
6009 int envelope_header_len;
6010 int envelope_content_len;
6012 int level_author_len;
6014 int num_yamyam_contents;
6017 getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */
6019 for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
6021 unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6023 header[i * 2 + 0] = header_word >> 8;
6024 header[i * 2 + 1] = header_word & 0xff;
6027 /* read some values from level header to check level decoding integrity */
6028 fieldx = header[6] | (header[7] << 8);
6029 fieldy = header[8] | (header[9] << 8);
6030 num_yamyam_contents = header[60] | (header[61] << 8);
6032 /* do some simple sanity checks to ensure that level was correctly decoded */
6033 if (fieldx < 1 || fieldx > 256 ||
6034 fieldy < 1 || fieldy > 256 ||
6035 num_yamyam_contents < 1 || num_yamyam_contents > 8)
6037 level->no_valid_file = TRUE;
6039 Error(ERR_WARN, "cannot decode level from stream -- using empty level");
6044 /* maximum envelope header size is 31 bytes */
6045 envelope_header_len = header[envelope_header_pos];
6046 /* maximum envelope content size is 110 (156?) bytes */
6047 envelope_content_len = header[envelope_content_pos];
6049 /* maximum level title size is 40 bytes */
6050 level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN);
6051 /* maximum level author size is 30 (51?) bytes */
6052 level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
6056 for (i = 0; i < envelope_header_len; i++)
6057 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6058 level->envelope[0].text[envelope_size++] =
6059 header[envelope_header_pos + 1 + i];
6061 if (envelope_header_len > 0 && envelope_content_len > 0)
6063 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6064 level->envelope[0].text[envelope_size++] = '\n';
6065 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6066 level->envelope[0].text[envelope_size++] = '\n';
6069 for (i = 0; i < envelope_content_len; i++)
6070 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6071 level->envelope[0].text[envelope_size++] =
6072 header[envelope_content_pos + 1 + i];
6074 level->envelope[0].text[envelope_size] = '\0';
6076 level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
6077 level->envelope[0].ysize = 10;
6078 level->envelope[0].autowrap = TRUE;
6079 level->envelope[0].centered = TRUE;
6081 for (i = 0; i < level_name_len; i++)
6082 level->name[i] = header[level_name_pos + 1 + i];
6083 level->name[level_name_len] = '\0';
6085 for (i = 0; i < level_author_len; i++)
6086 level->author[i] = header[level_author_pos + 1 + i];
6087 level->author[level_author_len] = '\0';
6089 num_yamyam_contents = header[60] | (header[61] << 8);
6090 level->num_yamyam_contents =
6091 MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
6093 for (i = 0; i < num_yamyam_contents; i++)
6095 for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
6097 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6099 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
6101 int element_dc = word;
6104 if (i < MAX_ELEMENT_CONTENTS)
6105 level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
6109 fieldx = header[6] | (header[7] << 8);
6110 fieldy = header[8] | (header[9] << 8);
6111 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
6112 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
6114 for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
6116 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6118 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
6120 int element_dc = word;
6123 if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
6124 level->field[x][y] = getMappedElement_DC(element_dc);
6127 x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
6128 y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
6129 level->field[x][y] = EL_PLAYER_1;
6131 x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
6132 y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
6133 level->field[x][y] = EL_PLAYER_2;
6135 level->gems_needed = header[18] | (header[19] << 8);
6137 level->score[SC_EMERALD] = header[20] | (header[21] << 8);
6138 level->score[SC_DIAMOND] = header[22] | (header[23] << 8);
6139 level->score[SC_PEARL] = header[24] | (header[25] << 8);
6140 level->score[SC_CRYSTAL] = header[26] | (header[27] << 8);
6141 level->score[SC_NUT] = header[28] | (header[29] << 8);
6142 level->score[SC_ROBOT] = header[30] | (header[31] << 8);
6143 level->score[SC_SPACESHIP] = header[32] | (header[33] << 8);
6144 level->score[SC_BUG] = header[34] | (header[35] << 8);
6145 level->score[SC_YAMYAM] = header[36] | (header[37] << 8);
6146 level->score[SC_DYNAMITE] = header[38] | (header[39] << 8);
6147 level->score[SC_KEY] = header[40] | (header[41] << 8);
6148 level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8);
6150 level->time = header[44] | (header[45] << 8);
6152 level->amoeba_speed = header[46] | (header[47] << 8);
6153 level->time_light = header[48] | (header[49] << 8);
6154 level->time_timegate = header[50] | (header[51] << 8);
6155 level->time_wheel = header[52] | (header[53] << 8);
6156 level->time_magic_wall = header[54] | (header[55] << 8);
6157 level->extra_time = header[56] | (header[57] << 8);
6158 level->shield_normal_time = header[58] | (header[59] << 8);
6160 /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
6161 can slip down from flat walls, like normal walls and steel walls */
6162 level->em_slippery_gems = TRUE;
6165 /* Diamond Caves II levels are always surrounded by indestructible wall, but
6166 not necessarily in a rectangular way -- fill with invisible steel wall */
6168 /* !!! not always true !!! keep level and set BorderElement instead !!! */
6170 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6173 if ((x == 0 || x == level->fieldx - 1 ||
6174 y == 0 || y == level->fieldy - 1) &&
6175 level->field[x][y] == EL_EMPTY)
6176 level->field[x][y] = EL_INVISIBLE_STEELWALL;
6178 if ((x == 0 || x == level->fieldx - 1 ||
6179 y == 0 || y == level->fieldy - 1) &&
6180 level->field[x][y] == EL_EMPTY)
6181 FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL,
6182 level->field, level->fieldx, level->fieldy);
6188 static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
6189 struct LevelFileInfo *level_file_info)
6191 char *filename = level_file_info->filename;
6193 int num_magic_bytes = 8;
6194 char magic_bytes[num_magic_bytes + 1];
6195 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
6197 if (!(file = fopen(filename, MODE_READ)))
6199 level->no_valid_file = TRUE;
6201 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
6206 // fseek(file, 0x0000, SEEK_SET);
6208 if (level_file_info->packed)
6210 /* read "magic bytes" from start of file */
6211 fgets(magic_bytes, num_magic_bytes + 1, file);
6213 /* check "magic bytes" for correct file format */
6214 if (!strPrefix(magic_bytes, "DC2"))
6216 level->no_valid_file = TRUE;
6218 Error(ERR_WARN, "unknown DC level file '%s' -- using empty level",
6224 if (strPrefix(magic_bytes, "DC2Win95") ||
6225 strPrefix(magic_bytes, "DC2Win98"))
6227 int position_first_level = 0x00fa;
6228 int extra_bytes = 4;
6231 /* advance file stream to first level inside the level package */
6232 skip_bytes = position_first_level - num_magic_bytes - extra_bytes;
6234 /* each block of level data is followed by block of non-level data */
6235 num_levels_to_skip *= 2;
6237 /* at least skip header bytes, therefore use ">= 0" instead of "> 0" */
6238 while (num_levels_to_skip >= 0)
6240 /* advance file stream to next level inside the level package */
6241 if (fseek(file, skip_bytes, SEEK_CUR) != 0)
6243 level->no_valid_file = TRUE;
6245 Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level",
6251 /* skip apparently unused extra bytes following each level */
6252 ReadUnusedBytesFromFile(file, extra_bytes);
6254 /* read size of next level in level package */
6255 skip_bytes = getFile32BitLE(file);
6257 num_levels_to_skip--;
6262 level->no_valid_file = TRUE;
6264 Error(ERR_WARN, "unknown DC2 level file '%s' -- using empty level",
6271 LoadLevelFromFileStream_DC(file, level, level_file_info->nr);
6278 static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
6279 struct LevelFileInfo *level_file_info)
6281 char *filename = level_file_info->filename;
6284 int nr = level_file_info->nr - leveldir_current->first_level;
6286 byte header[DC_LEVEL_HEADER_SIZE];
6288 int envelope_header_pos = 62;
6289 int envelope_content_pos = 94;
6290 int level_name_pos = 251;
6291 int level_author_pos = 292;
6292 int envelope_header_len;
6293 int envelope_content_len;
6295 int level_author_len;
6297 int num_yamyam_contents;
6300 if (!(file = fopen(filename, MODE_READ)))
6302 level->no_valid_file = TRUE;
6304 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
6310 /* position file stream to the requested level inside the level package */
6311 if (fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
6313 level->no_valid_file = TRUE;
6315 Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename);
6321 getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */
6323 for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
6325 unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6327 header[i * 2 + 0] = header_word >> 8;
6328 header[i * 2 + 1] = header_word & 0xff;
6331 /* read some values from level header to check level decoding integrity */
6332 fieldx = header[6] | (header[7] << 8);
6333 fieldy = header[8] | (header[9] << 8);
6334 num_yamyam_contents = header[60] | (header[61] << 8);
6336 /* do some simple sanity checks to ensure that level was correctly decoded */
6337 if (fieldx < 1 || fieldx > 256 ||
6338 fieldy < 1 || fieldy > 256 ||
6339 num_yamyam_contents < 1 || num_yamyam_contents > 8)
6341 level->no_valid_file = TRUE;
6343 Error(ERR_WARN, "cannot read level from file '%s' -- using empty level",
6349 /* maximum envelope header size is 31 bytes */
6350 envelope_header_len = header[envelope_header_pos];
6351 /* maximum envelope content size is 110 (156?) bytes */
6352 envelope_content_len = header[envelope_content_pos];
6354 /* maximum level title size is 40 bytes */
6355 level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN);
6356 /* maximum level author size is 30 (51?) bytes */
6357 level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
6361 for (i = 0; i < envelope_header_len; i++)
6362 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6363 level->envelope[0].text[envelope_size++] =
6364 header[envelope_header_pos + 1 + i];
6366 if (envelope_header_len > 0 && envelope_content_len > 0)
6368 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6369 level->envelope[0].text[envelope_size++] = '\n';
6370 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6371 level->envelope[0].text[envelope_size++] = '\n';
6374 for (i = 0; i < envelope_content_len; i++)
6375 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
6376 level->envelope[0].text[envelope_size++] =
6377 header[envelope_content_pos + 1 + i];
6379 level->envelope[0].text[envelope_size] = '\0';
6381 level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
6382 level->envelope[0].ysize = 10;
6383 level->envelope[0].autowrap = TRUE;
6384 level->envelope[0].centered = TRUE;
6386 for (i = 0; i < level_name_len; i++)
6387 level->name[i] = header[level_name_pos + 1 + i];
6388 level->name[level_name_len] = '\0';
6390 for (i = 0; i < level_author_len; i++)
6391 level->author[i] = header[level_author_pos + 1 + i];
6392 level->author[level_author_len] = '\0';
6394 num_yamyam_contents = header[60] | (header[61] << 8);
6395 level->num_yamyam_contents =
6396 MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
6398 for (i = 0; i < num_yamyam_contents; i++)
6400 for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
6402 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6404 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
6406 int element_dc = word;
6409 if (i < MAX_ELEMENT_CONTENTS)
6410 level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
6414 fieldx = header[6] | (header[7] << 8);
6415 fieldy = header[8] | (header[9] << 8);
6416 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
6417 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
6419 for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
6421 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
6423 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
6425 int element_dc = word;
6428 if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
6429 level->field[x][y] = getMappedElement_DC(element_dc);
6432 x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
6433 y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
6434 level->field[x][y] = EL_PLAYER_1;
6436 x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
6437 y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
6438 level->field[x][y] = EL_PLAYER_2;
6440 level->gems_needed = header[18] | (header[19] << 8);
6442 level->score[SC_EMERALD] = header[20] | (header[21] << 8);
6443 level->score[SC_DIAMOND] = header[22] | (header[23] << 8);
6444 level->score[SC_PEARL] = header[24] | (header[25] << 8);
6445 level->score[SC_CRYSTAL] = header[26] | (header[27] << 8);
6446 level->score[SC_NUT] = header[28] | (header[29] << 8);
6447 level->score[SC_ROBOT] = header[30] | (header[31] << 8);
6448 level->score[SC_SPACESHIP] = header[32] | (header[33] << 8);
6449 level->score[SC_BUG] = header[34] | (header[35] << 8);
6450 level->score[SC_YAMYAM] = header[36] | (header[37] << 8);
6451 level->score[SC_DYNAMITE] = header[38] | (header[39] << 8);
6452 level->score[SC_KEY] = header[40] | (header[41] << 8);
6453 level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8);
6455 level->time = header[44] | (header[45] << 8);
6457 level->amoeba_speed = header[46] | (header[47] << 8);
6458 level->time_light = header[48] | (header[49] << 8);
6459 level->time_timegate = header[50] | (header[51] << 8);
6460 level->time_wheel = header[52] | (header[53] << 8);
6461 level->time_magic_wall = header[54] | (header[55] << 8);
6462 level->extra_time = header[56] | (header[57] << 8);
6463 level->shield_normal_time = header[58] | (header[59] << 8);
6467 /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
6468 can slip down from flat walls, like normal walls and steel walls */
6469 level->em_slippery_gems = TRUE;
6472 /* Diamond Caves II levels are always surrounded by indestructible wall, but
6473 not necessarily in a rectangular way -- fill with invisible steel wall */
6475 /* !!! not always true !!! keep level and set BorderElement instead !!! */
6477 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6480 if ((x == 0 || x == level->fieldx - 1 ||
6481 y == 0 || y == level->fieldy - 1) &&
6482 level->field[x][y] == EL_EMPTY)
6483 level->field[x][y] = EL_INVISIBLE_STEELWALL;
6485 if ((x == 0 || x == level->fieldx - 1 ||
6486 y == 0 || y == level->fieldy - 1) &&
6487 level->field[x][y] == EL_EMPTY)
6488 FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL,
6489 level->field, level->fieldx, level->fieldy);
6498 /* ------------------------------------------------------------------------- */
6499 /* functions for loading SB level */
6500 /* ------------------------------------------------------------------------- */
6502 int getMappedElement_SB(int element_ascii, boolean use_ces)
6510 sb_element_mapping[] =
6512 { ' ', EL_EMPTY, EL_CUSTOM_1 }, /* floor (space) */
6513 { '#', EL_STEELWALL, EL_CUSTOM_2 }, /* wall */
6514 { '@', EL_PLAYER_1, EL_CUSTOM_3 }, /* player */
6515 { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, /* box */
6516 { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, /* goal square */
6517 { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, /* box on goal square */
6518 { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, /* player on goal square */
6520 { '_', EL_INVISIBLE_STEELWALL, EL_CUSTOM_8 }, /* floor beyond border */
6522 { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, /* floor beyond border */
6530 for (i = 0; sb_element_mapping[i].ascii != 0; i++)
6531 if (element_ascii == sb_element_mapping[i].ascii)
6532 return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb);
6534 return EL_UNDEFINED;
6537 static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
6538 struct LevelFileInfo *level_file_info)
6540 char *filename = level_file_info->filename;
6541 char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
6542 char last_comment[MAX_LINE_LEN];
6543 char level_name[MAX_LINE_LEN];
6546 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
6547 boolean read_continued_line = FALSE;
6548 boolean reading_playfield = FALSE;
6549 boolean got_valid_playfield_line = FALSE;
6550 boolean invalid_playfield_char = FALSE;
6551 boolean load_xsb_to_ces = check_special_flags("load_xsb_to_ces");
6552 int file_level_nr = 0;
6557 printf("::: looking for level number %d [%d]\n",
6558 level_file_info->nr, num_levels_to_skip);
6561 last_comment[0] = '\0';
6562 level_name[0] = '\0';
6564 if (!(file = fopen(filename, MODE_READ)))
6566 level->no_valid_file = TRUE;
6568 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
6575 /* level successfully read, but next level may follow here */
6576 if (!got_valid_playfield_line && reading_playfield)
6579 printf("::: read complete playfield\n");
6582 /* read playfield from single level file -- skip remaining file */
6583 if (!level_file_info->packed)
6586 if (file_level_nr >= num_levels_to_skip)
6591 last_comment[0] = '\0';
6592 level_name[0] = '\0';
6594 reading_playfield = FALSE;
6597 got_valid_playfield_line = FALSE;
6599 /* read next line of input file */
6600 if (!fgets(line, MAX_LINE_LEN, file))
6603 /* check if line was completely read and is terminated by line break */
6604 if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
6607 /* cut trailing line break (this can be newline and/or carriage return) */
6608 for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
6609 if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
6612 /* copy raw input line for later use (mainly debugging output) */
6613 strcpy(line_raw, line);
6615 if (read_continued_line)
6617 /* append new line to existing line, if there is enough space */
6618 if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
6619 strcat(previous_line, line_ptr);
6621 strcpy(line, previous_line); /* copy storage buffer to line */
6623 read_continued_line = FALSE;
6626 /* if the last character is '\', continue at next line */
6627 if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
6629 line[strlen(line) - 1] = '\0'; /* cut off trailing backslash */
6630 strcpy(previous_line, line); /* copy line to storage buffer */
6632 read_continued_line = TRUE;
6637 /* skip empty lines */
6638 if (line[0] == '\0')
6641 /* extract comment text from comment line */
6644 for (line_ptr = line; *line_ptr; line_ptr++)
6645 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != ';')
6648 strcpy(last_comment, line_ptr);
6651 printf("::: found comment '%s' in line %d\n", last_comment, line_nr);
6657 /* extract level title text from line containing level title */
6658 if (line[0] == '\'')
6660 strcpy(level_name, &line[1]);
6662 if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'')
6663 level_name[strlen(level_name) - 1] = '\0';
6666 printf("::: found level name '%s' in line %d\n", level_name, line_nr);
6672 /* skip lines containing only spaces (or empty lines) */
6673 for (line_ptr = line; *line_ptr; line_ptr++)
6674 if (*line_ptr != ' ')
6676 if (*line_ptr == '\0')
6679 /* at this point, we have found a line containing part of a playfield */
6682 printf("::: found playfield row in line %d\n", line_nr);
6685 got_valid_playfield_line = TRUE;
6687 if (!reading_playfield)
6689 reading_playfield = TRUE;
6690 invalid_playfield_char = FALSE;
6692 for (x = 0; x < MAX_LEV_FIELDX; x++)
6693 for (y = 0; y < MAX_LEV_FIELDY; y++)
6694 level->field[x][y] = getMappedElement_SB(' ', load_xsb_to_ces);
6699 /* start with topmost tile row */
6703 /* skip playfield line if larger row than allowed */
6704 if (y >= MAX_LEV_FIELDY)
6707 /* start with leftmost tile column */
6710 /* read playfield elements from line */
6711 for (line_ptr = line; *line_ptr; line_ptr++)
6713 int mapped_sb_element = getMappedElement_SB(*line_ptr, load_xsb_to_ces);
6715 /* stop parsing playfield line if larger column than allowed */
6716 if (x >= MAX_LEV_FIELDX)
6719 if (mapped_sb_element == EL_UNDEFINED)
6721 invalid_playfield_char = TRUE;
6726 level->field[x][y] = mapped_sb_element;
6728 /* continue with next tile column */
6731 level->fieldx = MAX(x, level->fieldx);
6734 if (invalid_playfield_char)
6736 /* if first playfield line, treat invalid lines as comment lines */
6738 reading_playfield = FALSE;
6743 /* continue with next tile row */
6751 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, level->fieldx), MAX_LEV_FIELDX);
6752 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, level->fieldy), MAX_LEV_FIELDY);
6754 if (!reading_playfield)
6756 level->no_valid_file = TRUE;
6758 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
6763 if (*level_name != '\0')
6765 strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN);
6766 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6769 printf(":1: level name: '%s'\n", level->name);
6772 else if (*last_comment != '\0')
6774 strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN);
6775 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6778 printf(":2: level name: '%s'\n", level->name);
6783 sprintf(level->name, "--> Level %d <--", level_file_info->nr);
6786 /* set all empty fields beyond the border walls to invisible steel wall */
6787 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6789 if ((x == 0 || x == level->fieldx - 1 ||
6790 y == 0 || y == level->fieldy - 1) &&
6791 level->field[x][y] == getMappedElement_SB(' ', load_xsb_to_ces))
6792 FloodFillLevel(x, y, getMappedElement_SB('_', load_xsb_to_ces),
6793 level->field, level->fieldx, level->fieldy);
6796 /* set special level settings for Sokoban levels */
6799 level->use_step_counter = TRUE;
6801 if (load_xsb_to_ces)
6804 /* !!! special global settings can now be set in level template !!! */
6806 level->initial_player_stepsize[0] = STEPSIZE_SLOW;
6809 /* fill smaller playfields with padding "beyond border wall" elements */
6810 if (level->fieldx < SCR_FIELDX ||
6811 level->fieldy < SCR_FIELDY)
6813 short field[level->fieldx][level->fieldy];
6814 int new_fieldx = MAX(level->fieldx, SCR_FIELDX);
6815 int new_fieldy = MAX(level->fieldy, SCR_FIELDY);
6816 int pos_fieldx = (new_fieldx - level->fieldx) / 2;
6817 int pos_fieldy = (new_fieldy - level->fieldy) / 2;
6819 /* copy old playfield (which is smaller than the visible area) */
6820 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6821 field[x][y] = level->field[x][y];
6823 /* fill new, larger playfield with "beyond border wall" elements */
6824 for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++)
6825 level->field[x][y] = getMappedElement_SB('_', load_xsb_to_ces);
6827 /* copy the old playfield to the middle of the new playfield */
6828 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6829 level->field[pos_fieldx + x][pos_fieldy + y] = field[x][y];
6831 level->fieldx = new_fieldx;
6832 level->fieldy = new_fieldy;
6835 level->use_custom_template = TRUE;
6840 /* ------------------------------------------------------------------------- */
6841 /* functions for handling native levels */
6842 /* ------------------------------------------------------------------------- */
6844 static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
6845 struct LevelFileInfo *level_file_info)
6847 if (!LoadNativeLevel_EM(level_file_info->filename))
6848 level->no_valid_file = TRUE;
6851 static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
6852 struct LevelFileInfo *level_file_info)
6856 /* determine position of requested level inside level package */
6857 if (level_file_info->packed)
6858 pos = level_file_info->nr - leveldir_current->first_level;
6860 if (!LoadNativeLevel_SP(level_file_info->filename, pos))
6861 level->no_valid_file = TRUE;
6864 void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
6866 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6867 CopyNativeLevel_RND_to_EM(level);
6868 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6869 CopyNativeLevel_RND_to_SP(level);
6872 void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
6874 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6875 CopyNativeLevel_EM_to_RND(level);
6876 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6877 CopyNativeLevel_SP_to_RND(level);
6880 void SaveNativeLevel(struct LevelInfo *level)
6882 if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6884 char *basename = getSingleLevelBasenameExt(level->file_info.nr, "sp");
6885 char *filename = getLevelFilenameFromBasename(basename);
6887 CopyNativeLevel_RND_to_SP(level);
6888 CopyNativeTape_RND_to_SP(level);
6890 SaveNativeLevel_SP(filename);
6895 /* ------------------------------------------------------------------------- */
6896 /* functions for loading generic level */
6897 /* ------------------------------------------------------------------------- */
6899 void LoadLevelFromFileInfo(struct LevelInfo *level,
6900 struct LevelFileInfo *level_file_info)
6902 /* always start with reliable default values */
6903 setLevelInfoToDefaults(level);
6905 switch (level_file_info->type)
6907 case LEVEL_FILE_TYPE_RND:
6908 LoadLevelFromFileInfo_RND(level, level_file_info);
6911 case LEVEL_FILE_TYPE_EM:
6912 LoadLevelFromFileInfo_EM(level, level_file_info);
6913 level->game_engine_type = GAME_ENGINE_TYPE_EM;
6916 case LEVEL_FILE_TYPE_SP:
6917 LoadLevelFromFileInfo_SP(level, level_file_info);
6918 level->game_engine_type = GAME_ENGINE_TYPE_SP;
6921 case LEVEL_FILE_TYPE_DC:
6922 LoadLevelFromFileInfo_DC(level, level_file_info);
6925 case LEVEL_FILE_TYPE_SB:
6926 LoadLevelFromFileInfo_SB(level, level_file_info);
6930 LoadLevelFromFileInfo_RND(level, level_file_info);
6934 /* if level file is invalid, restore level structure to default values */
6935 if (level->no_valid_file)
6936 setLevelInfoToDefaults(level);
6938 if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
6939 level->game_engine_type = GAME_ENGINE_TYPE_RND;
6941 if (level_file_info->type != LEVEL_FILE_TYPE_RND)
6942 CopyNativeLevel_Native_to_RND(level);
6945 void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
6947 static struct LevelFileInfo level_file_info;
6949 /* always start with reliable default values */
6950 setFileInfoToDefaults(&level_file_info);
6952 level_file_info.nr = 0; /* unknown level number */
6953 level_file_info.type = LEVEL_FILE_TYPE_RND; /* no others supported yet */
6954 level_file_info.filename = filename;
6956 LoadLevelFromFileInfo(level, &level_file_info);
6959 static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
6963 if (leveldir_current == NULL) /* only when dumping level */
6966 /* all engine modifications also valid for levels which use latest engine */
6967 if (level->game_version < VERSION_IDENT(3,2,0,5))
6969 /* time bonus score was given for 10 s instead of 1 s before 3.2.0-5 */
6970 level->score[SC_TIME_BONUS] /= 10;
6974 leveldir_current->latest_engine = TRUE; /* !!! TEST ONLY !!! */
6977 if (leveldir_current->latest_engine)
6979 /* ---------- use latest game engine ----------------------------------- */
6981 /* For all levels which are forced to use the latest game engine version
6982 (normally all but user contributed, private and undefined levels), set
6983 the game engine version to the actual version; this allows for actual
6984 corrections in the game engine to take effect for existing, converted
6985 levels (from "classic" or other existing games) to make the emulation
6986 of the corresponding game more accurate, while (hopefully) not breaking
6987 existing levels created from other players. */
6989 level->game_version = GAME_VERSION_ACTUAL;
6991 /* Set special EM style gems behaviour: EM style gems slip down from
6992 normal, steel and growing wall. As this is a more fundamental change,
6993 it seems better to set the default behaviour to "off" (as it is more
6994 natural) and make it configurable in the level editor (as a property
6995 of gem style elements). Already existing converted levels (neither
6996 private nor contributed levels) are changed to the new behaviour. */
6998 if (level->file_version < FILE_VERSION_2_0)
6999 level->em_slippery_gems = TRUE;
7004 /* ---------- use game engine the level was created with ----------------- */
7006 /* For all levels which are not forced to use the latest game engine
7007 version (normally user contributed, private and undefined levels),
7008 use the version of the game engine the levels were created for.
7010 Since 2.0.1, the game engine version is now directly stored
7011 in the level file (chunk "VERS"), so there is no need anymore
7012 to set the game version from the file version (except for old,
7013 pre-2.0 levels, where the game version is still taken from the
7014 file format version used to store the level -- see above). */
7016 /* player was faster than enemies in 1.0.0 and before */
7017 if (level->file_version == FILE_VERSION_1_0)
7018 for (i = 0; i < MAX_PLAYERS; i++)
7019 level->initial_player_stepsize[i] = STEPSIZE_FAST;
7021 /* default behaviour for EM style gems was "slippery" only in 2.0.1 */
7022 if (level->game_version == VERSION_IDENT(2,0,1,0))
7023 level->em_slippery_gems = TRUE;
7025 /* springs could be pushed over pits before (pre-release version) 2.2.0 */
7026 if (level->game_version < VERSION_IDENT(2,2,0,0))
7027 level->use_spring_bug = TRUE;
7029 if (level->game_version < VERSION_IDENT(3,2,0,5))
7031 /* time orb caused limited time in endless time levels before 3.2.0-5 */
7032 level->use_time_orb_bug = TRUE;
7034 /* default behaviour for snapping was "no snap delay" before 3.2.0-5 */
7035 level->block_snap_field = FALSE;
7037 /* extra time score was same value as time left score before 3.2.0-5 */
7038 level->extra_time_score = level->score[SC_TIME_BONUS];
7041 /* time bonus score was given for 10 s instead of 1 s before 3.2.0-5 */
7042 level->score[SC_TIME_BONUS] /= 10;
7046 if (level->game_version < VERSION_IDENT(3,2,0,7))
7048 /* default behaviour for snapping was "not continuous" before 3.2.0-7 */
7049 level->continuous_snapping = FALSE;
7052 /* only few elements were able to actively move into acid before 3.1.0 */
7053 /* trigger settings did not exist before 3.1.0; set to default "any" */
7054 if (level->game_version < VERSION_IDENT(3,1,0,0))
7056 /* correct "can move into acid" settings (all zero in old levels) */
7058 level->can_move_into_acid_bits = 0; /* nothing can move into acid */
7059 level->dont_collide_with_bits = 0; /* nothing is deadly when colliding */
7061 setMoveIntoAcidProperty(level, EL_ROBOT, TRUE);
7062 setMoveIntoAcidProperty(level, EL_SATELLITE, TRUE);
7063 setMoveIntoAcidProperty(level, EL_PENGUIN, TRUE);
7064 setMoveIntoAcidProperty(level, EL_BALLOON, TRUE);
7066 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7067 SET_PROPERTY(EL_CUSTOM_START + i, EP_CAN_MOVE_INTO_ACID, TRUE);
7069 /* correct trigger settings (stored as zero == "none" in old levels) */
7071 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7073 int element = EL_CUSTOM_START + i;
7074 struct ElementInfo *ei = &element_info[element];
7076 for (j = 0; j < ei->num_change_pages; j++)
7078 struct ElementChangeInfo *change = &ei->change_page[j];
7080 change->trigger_player = CH_PLAYER_ANY;
7081 change->trigger_page = CH_PAGE_ANY;
7086 /* try to detect and fix "Snake Bite" levels, which are broken with 3.2.0 */
7088 int element = EL_CUSTOM_256;
7089 struct ElementInfo *ei = &element_info[element];
7090 struct ElementChangeInfo *change = &ei->change_page[0];
7092 /* This is needed to fix a problem that was caused by a bugfix in function
7093 game.c/CreateFieldExt() introduced with 3.2.0 that corrects the behaviour
7094 when a custom element changes to EL_SOKOBAN_FIELD_PLAYER (before, it did
7095 not replace walkable elements, but instead just placed the player on it,
7096 without placing the Sokoban field under the player). Unfortunately, this
7097 breaks "Snake Bite" style levels when the snake is halfway through a door
7098 that just closes (the snake head is still alive and can be moved in this
7099 case). This can be fixed by replacing the EL_SOKOBAN_FIELD_PLAYER by the
7100 player (without Sokoban element) which then gets killed as designed). */
7102 if ((strncmp(leveldir_current->identifier, "snake_bite", 10) == 0 ||
7103 strncmp(ei->description, "pause b4 death", 14) == 0) &&
7104 change->target_element == EL_SOKOBAN_FIELD_PLAYER)
7105 change->target_element = EL_PLAYER_1;
7109 /* try to detect and fix "Zelda" style levels, which are broken with 3.2.5 */
7110 if (level->game_version < VERSION_IDENT(3,2,5,0))
7112 /* This is needed to fix a problem that was caused by a bugfix in function
7113 game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
7114 corrects the behaviour when a custom element changes to another custom
7115 element with a higher element number that has change actions defined.
7116 Normally, only one change per frame is allowed for custom elements.
7117 Therefore, it is checked if a custom element already changed in the
7118 current frame; if it did, subsequent changes are suppressed.
7119 Unfortunately, this is only checked for element changes, but not for
7120 change actions, which are still executed. As the function above loops
7121 through all custom elements from lower to higher, an element change
7122 resulting in a lower CE number won't be checked again, while a target
7123 element with a higher number will also be checked, and potential change
7124 actions will get executed for this CE, too (which is wrong), while
7125 further changes are ignored (which is correct). As this bugfix breaks
7126 Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
7127 few other levels like Alan Bond's "FMV"), allow the previous, incorrect
7128 behaviour for existing levels and tapes that make use of this bug */
7130 level->use_action_after_change_bug = TRUE;
7133 /* !!! THIS DOES NOT FIX "Zelda I" (GRAPHICALLY) AND "Alan's FMV" LEVELS */
7134 /* try to detect and fix "Zelda II" levels, which are broken with 3.2.5 */
7136 int element = EL_CUSTOM_16;
7137 struct ElementInfo *ei = &element_info[element];
7139 /* This is needed to fix a problem that was caused by a bugfix in function
7140 game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
7141 corrects the behaviour when a custom element changes to another custom
7142 element with a higher element number that has change actions defined.
7143 Normally, only one change per frame is allowed for custom elements.
7144 Therefore, it is checked if a custom element already changed in the
7145 current frame; if it did, subsequent changes are suppressed.
7146 Unfortunately, this is only checked for element changes, but not for
7147 change actions, which are still executed. As the function above loops
7148 through all custom elements from lower to higher, an element change
7149 resulting in a lower CE number won't be checked again, while a target
7150 element with a higher number will also be checked, and potential change
7151 actions will get executed for this CE, too (which is wrong), while
7152 further changes are ignored (which is correct). As this bugfix breaks
7153 Zelda II (but no other levels), allow the previous, incorrect behaviour
7154 for this outstanding level set to not break the game or existing tapes */
7156 if (strncmp(leveldir_current->identifier, "zelda2", 6) == 0 ||
7157 strncmp(ei->description, "scanline - row 1", 16) == 0)
7158 level->use_action_after_change_bug = TRUE;
7162 /* not centering level after relocating player was default only in 3.2.3 */
7163 if (level->game_version == VERSION_IDENT(3,2,3,0)) /* (no pre-releases) */
7164 level->shifted_relocation = TRUE;
7166 /* EM style elements always chain-exploded in R'n'D engine before 3.2.6 */
7167 if (level->game_version < VERSION_IDENT(3,2,6,0))
7168 level->em_explodes_by_fire = TRUE;
7171 static void LoadLevel_InitElements(struct LevelInfo *level, char *filename)
7175 /* map custom element change events that have changed in newer versions
7176 (these following values were accidentally changed in version 3.0.1)
7177 (this seems to be needed only for 'ab_levelset3' and 'ab_levelset4') */
7178 if (level->game_version <= VERSION_IDENT(3,0,0,0))
7180 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7182 int element = EL_CUSTOM_START + i;
7184 /* order of checking and copying events to be mapped is important */
7185 /* (do not change the start and end value -- they are constant) */
7186 for (j = CE_BY_OTHER_ACTION; j >= CE_VALUE_GETS_ZERO; j--)
7188 if (HAS_CHANGE_EVENT(element, j - 2))
7190 SET_CHANGE_EVENT(element, j - 2, FALSE);
7191 SET_CHANGE_EVENT(element, j, TRUE);
7195 /* order of checking and copying events to be mapped is important */
7196 /* (do not change the start and end value -- they are constant) */
7197 for (j = CE_PLAYER_COLLECTS_X; j >= CE_HITTING_SOMETHING; j--)
7199 if (HAS_CHANGE_EVENT(element, j - 1))
7201 SET_CHANGE_EVENT(element, j - 1, FALSE);
7202 SET_CHANGE_EVENT(element, j, TRUE);
7208 /* initialize "can_change" field for old levels with only one change page */
7209 if (level->game_version <= VERSION_IDENT(3,0,2,0))
7211 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7213 int element = EL_CUSTOM_START + i;
7215 if (CAN_CHANGE(element))
7216 element_info[element].change->can_change = TRUE;
7220 /* correct custom element values (for old levels without these options) */
7221 if (level->game_version < VERSION_IDENT(3,1,1,0))
7223 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7225 int element = EL_CUSTOM_START + i;
7226 struct ElementInfo *ei = &element_info[element];
7228 if (ei->access_direction == MV_NO_DIRECTION)
7229 ei->access_direction = MV_ALL_DIRECTIONS;
7233 /* correct custom element values (fix invalid values for all versions) */
7236 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7238 int element = EL_CUSTOM_START + i;
7239 struct ElementInfo *ei = &element_info[element];
7241 for (j = 0; j < ei->num_change_pages; j++)
7243 struct ElementChangeInfo *change = &ei->change_page[j];
7245 if (change->trigger_player == CH_PLAYER_NONE)
7246 change->trigger_player = CH_PLAYER_ANY;
7248 if (change->trigger_side == CH_SIDE_NONE)
7249 change->trigger_side = CH_SIDE_ANY;
7254 /* initialize "can_explode" field for old levels which did not store this */
7255 /* !!! CHECK THIS -- "<= 3,1,0,0" IS PROBABLY WRONG !!! */
7256 if (level->game_version <= VERSION_IDENT(3,1,0,0))
7258 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7260 int element = EL_CUSTOM_START + i;
7262 if (EXPLODES_1X1_OLD(element))
7263 element_info[element].explosion_type = EXPLODES_1X1;
7265 SET_PROPERTY(element, EP_CAN_EXPLODE, (EXPLODES_BY_FIRE(element) ||
7266 EXPLODES_SMASHED(element) ||
7267 EXPLODES_IMPACT(element)));
7271 /* correct previously hard-coded move delay values for maze runner style */
7272 if (level->game_version < VERSION_IDENT(3,1,1,0))
7274 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7276 int element = EL_CUSTOM_START + i;
7278 if (element_info[element].move_pattern & MV_MAZE_RUNNER_STYLE)
7280 /* previously hard-coded and therefore ignored */
7281 element_info[element].move_delay_fixed = 9;
7282 element_info[element].move_delay_random = 0;
7287 /* map elements that have changed in newer versions */
7288 level->amoeba_content = getMappedElementByVersion(level->amoeba_content,
7289 level->game_version);
7290 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7291 for (x = 0; x < 3; x++)
7292 for (y = 0; y < 3; y++)
7293 level->yamyam_content[i].e[x][y] =
7294 getMappedElementByVersion(level->yamyam_content[i].e[x][y],
7295 level->game_version);
7297 /* initialize element properties for level editor etc. */
7298 InitElementPropertiesEngine(level->game_version);
7299 InitElementPropertiesAfterLoading(level->game_version);
7300 InitElementPropertiesGfxElement();
7303 static void LoadLevel_InitPlayfield(struct LevelInfo *level, char *filename)
7307 /* map elements that have changed in newer versions */
7308 for (y = 0; y < level->fieldy; y++)
7309 for (x = 0; x < level->fieldx; x++)
7310 level->field[x][y] = getMappedElementByVersion(level->field[x][y],
7311 level->game_version);
7313 /* copy elements to runtime playfield array */
7314 for (x = 0; x < MAX_LEV_FIELDX; x++)
7315 for (y = 0; y < MAX_LEV_FIELDY; y++)
7316 Feld[x][y] = level->field[x][y];
7318 /* initialize level size variables for faster access */
7319 lev_fieldx = level->fieldx;
7320 lev_fieldy = level->fieldy;
7322 /* determine border element for this level */
7323 if (level->file_info.type == LEVEL_FILE_TYPE_DC)
7324 BorderElement = EL_EMPTY; /* (in editor, SetBorderElement() is used) */
7329 static void LoadLevel_InitNativeEngines(struct LevelInfo *level,char *filename)
7331 struct LevelFileInfo *level_file_info = &level->file_info;
7333 if (level_file_info->type == LEVEL_FILE_TYPE_RND)
7334 CopyNativeLevel_RND_to_Native(level);
7337 void LoadLevelTemplate(int nr)
7341 setLevelFileInfo(&level_template.file_info, nr);
7342 filename = level_template.file_info.filename;
7344 LoadLevelFromFileInfo(&level_template, &level_template.file_info);
7346 LoadLevel_InitVersion(&level_template, filename);
7347 LoadLevel_InitElements(&level_template, filename);
7349 ActivateLevelTemplate();
7352 void LoadLevel(int nr)
7356 setLevelFileInfo(&level.file_info, nr);
7357 filename = level.file_info.filename;
7359 LoadLevelFromFileInfo(&level, &level.file_info);
7361 if (level.use_custom_template)
7362 LoadLevelTemplate(-1);
7364 LoadLevel_InitVersion(&level, filename);
7365 LoadLevel_InitElements(&level, filename);
7366 LoadLevel_InitPlayfield(&level, filename);
7368 LoadLevel_InitNativeEngines(&level, filename);
7371 static int SaveLevel_VERS(FILE *file, struct LevelInfo *level)
7375 chunk_size += putFileVersion(file, level->file_version);
7376 chunk_size += putFileVersion(file, level->game_version);
7381 static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
7385 chunk_size += putFile16BitBE(file, level->creation_date.year);
7386 chunk_size += putFile8Bit(file, level->creation_date.month);
7387 chunk_size += putFile8Bit(file, level->creation_date.day);
7393 static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
7397 putFile8Bit(file, level->fieldx);
7398 putFile8Bit(file, level->fieldy);
7400 putFile16BitBE(file, level->time);
7401 putFile16BitBE(file, level->gems_needed);
7403 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
7404 putFile8Bit(file, level->name[i]);
7406 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
7407 putFile8Bit(file, level->score[i]);
7409 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
7410 for (y = 0; y < 3; y++)
7411 for (x = 0; x < 3; x++)
7412 putFile8Bit(file, (level->encoding_16bit_yamyam ? EL_EMPTY :
7413 level->yamyam_content[i].e[x][y]));
7414 putFile8Bit(file, level->amoeba_speed);
7415 putFile8Bit(file, level->time_magic_wall);
7416 putFile8Bit(file, level->time_wheel);
7417 putFile8Bit(file, (level->encoding_16bit_amoeba ? EL_EMPTY :
7418 level->amoeba_content));
7419 putFile8Bit(file, (level->initial_player_stepsize == STEPSIZE_FAST ? 1 : 0));
7420 putFile8Bit(file, (level->initial_gravity ? 1 : 0));
7421 putFile8Bit(file, (level->encoding_16bit_field ? 1 : 0));
7422 putFile8Bit(file, (level->em_slippery_gems ? 1 : 0));
7424 putFile8Bit(file, (level->use_custom_template ? 1 : 0));
7426 putFile8Bit(file, (level->block_last_field ? 1 : 0));
7427 putFile8Bit(file, (level->sp_block_last_field ? 1 : 0));
7428 putFile32BitBE(file, level->can_move_into_acid_bits);
7429 putFile8Bit(file, level->dont_collide_with_bits);
7431 putFile8Bit(file, (level->use_spring_bug ? 1 : 0));
7432 putFile8Bit(file, (level->use_step_counter ? 1 : 0));
7434 putFile8Bit(file, (level->instant_relocation ? 1 : 0));
7435 putFile8Bit(file, (level->can_pass_to_walkable ? 1 : 0));
7436 putFile8Bit(file, (level->grow_into_diggable ? 1 : 0));
7438 putFile8Bit(file, level->game_engine_type);
7440 WriteUnusedBytesToFile(file, LEVEL_CHUNK_HEAD_UNUSED);
7444 static int SaveLevel_NAME(FILE *file, struct LevelInfo *level)
7449 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
7450 chunk_size += putFile8Bit(file, level->name[i]);
7455 static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
7460 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
7461 chunk_size += putFile8Bit(file, level->author[i]);
7467 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
7472 for (y = 0; y < level->fieldy; y++)
7473 for (x = 0; x < level->fieldx; x++)
7474 if (level->encoding_16bit_field)
7475 chunk_size += putFile16BitBE(file, level->field[x][y]);
7477 chunk_size += putFile8Bit(file, level->field[x][y]);
7483 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
7488 for (y = 0; y < level->fieldy; y++)
7489 for (x = 0; x < level->fieldx; x++)
7490 chunk_size += putFile16BitBE(file, level->field[x][y]);
7496 static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
7500 putFile8Bit(file, EL_YAMYAM);
7501 putFile8Bit(file, level->num_yamyam_contents);
7502 putFile8Bit(file, 0);
7503 putFile8Bit(file, 0);
7505 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7506 for (y = 0; y < 3; y++)
7507 for (x = 0; x < 3; x++)
7508 if (level->encoding_16bit_field)
7509 putFile16BitBE(file, level->yamyam_content[i].e[x][y]);
7511 putFile8Bit(file, level->yamyam_content[i].e[x][y]);
7516 static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
7519 int num_contents, content_xsize, content_ysize;
7520 int content_array[MAX_ELEMENT_CONTENTS][3][3];
7522 if (element == EL_YAMYAM)
7524 num_contents = level->num_yamyam_contents;
7528 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7529 for (y = 0; y < 3; y++)
7530 for (x = 0; x < 3; x++)
7531 content_array[i][x][y] = level->yamyam_content[i].e[x][y];
7533 else if (element == EL_BD_AMOEBA)
7539 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7540 for (y = 0; y < 3; y++)
7541 for (x = 0; x < 3; x++)
7542 content_array[i][x][y] = EL_EMPTY;
7543 content_array[0][0][0] = level->amoeba_content;
7547 /* chunk header already written -- write empty chunk data */
7548 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
7550 Error(ERR_WARN, "cannot save content for element '%d'", element);
7554 putFile16BitBE(file, element);
7555 putFile8Bit(file, num_contents);
7556 putFile8Bit(file, content_xsize);
7557 putFile8Bit(file, content_ysize);
7559 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
7561 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7562 for (y = 0; y < 3; y++)
7563 for (x = 0; x < 3; x++)
7564 putFile16BitBE(file, content_array[i][x][y]);
7569 static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
7571 int envelope_nr = element - EL_ENVELOPE_1;
7572 int envelope_len = strlen(level->envelope_text[envelope_nr]) + 1;
7576 chunk_size += putFile16BitBE(file, element);
7577 chunk_size += putFile16BitBE(file, envelope_len);
7578 chunk_size += putFile8Bit(file, level->envelope_xsize[envelope_nr]);
7579 chunk_size += putFile8Bit(file, level->envelope_ysize[envelope_nr]);
7581 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT3_UNUSED);
7582 chunk_size += LEVEL_CHUNK_CNT3_UNUSED;
7584 for (i = 0; i < envelope_len; i++)
7585 chunk_size += putFile8Bit(file, level->envelope_text[envelope_nr][i]);
7592 static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
7593 int num_changed_custom_elements)
7597 putFile16BitBE(file, num_changed_custom_elements);
7599 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7601 int element = EL_CUSTOM_START + i;
7603 struct ElementInfo *ei = &element_info[element];
7605 if (ei->properties[EP_BITFIELD_BASE_NR] != EP_BITMASK_DEFAULT)
7607 if (check < num_changed_custom_elements)
7609 putFile16BitBE(file, element);
7610 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7617 if (check != num_changed_custom_elements) /* should not happen */
7618 Error(ERR_WARN, "inconsistent number of custom element properties");
7623 static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
7624 int num_changed_custom_elements)
7628 putFile16BitBE(file, num_changed_custom_elements);
7630 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7632 int element = EL_CUSTOM_START + i;
7634 if (element_info[element].change->target_element != EL_EMPTY_SPACE)
7636 if (check < num_changed_custom_elements)
7638 putFile16BitBE(file, element);
7639 putFile16BitBE(file, element_info[element].change->target_element);
7646 if (check != num_changed_custom_elements) /* should not happen */
7647 Error(ERR_WARN, "inconsistent number of custom target elements");
7652 static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
7653 int num_changed_custom_elements)
7655 int i, j, x, y, check = 0;
7657 putFile16BitBE(file, num_changed_custom_elements);
7659 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7661 int element = EL_CUSTOM_START + i;
7662 struct ElementInfo *ei = &element_info[element];
7664 if (ei->modified_settings)
7666 if (check < num_changed_custom_elements)
7668 putFile16BitBE(file, element);
7670 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
7671 putFile8Bit(file, ei->description[j]);
7673 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7675 /* some free bytes for future properties and padding */
7676 WriteUnusedBytesToFile(file, 7);
7678 putFile8Bit(file, ei->use_gfx_element);
7679 putFile16BitBE(file, ei->gfx_element_initial);
7681 putFile8Bit(file, ei->collect_score_initial);
7682 putFile8Bit(file, ei->collect_count_initial);
7684 putFile16BitBE(file, ei->push_delay_fixed);
7685 putFile16BitBE(file, ei->push_delay_random);
7686 putFile16BitBE(file, ei->move_delay_fixed);
7687 putFile16BitBE(file, ei->move_delay_random);
7689 putFile16BitBE(file, ei->move_pattern);
7690 putFile8Bit(file, ei->move_direction_initial);
7691 putFile8Bit(file, ei->move_stepsize);
7693 for (y = 0; y < 3; y++)
7694 for (x = 0; x < 3; x++)
7695 putFile16BitBE(file, ei->content.e[x][y]);
7697 putFile32BitBE(file, ei->change->events);
7699 putFile16BitBE(file, ei->change->target_element);
7701 putFile16BitBE(file, ei->change->delay_fixed);
7702 putFile16BitBE(file, ei->change->delay_random);
7703 putFile16BitBE(file, ei->change->delay_frames);
7705 putFile16BitBE(file, ei->change->initial_trigger_element);
7707 putFile8Bit(file, ei->change->explode);
7708 putFile8Bit(file, ei->change->use_target_content);
7709 putFile8Bit(file, ei->change->only_if_complete);
7710 putFile8Bit(file, ei->change->use_random_replace);
7712 putFile8Bit(file, ei->change->random_percentage);
7713 putFile8Bit(file, ei->change->replace_when);
7715 for (y = 0; y < 3; y++)
7716 for (x = 0; x < 3; x++)
7717 putFile16BitBE(file, ei->change->content.e[x][y]);
7719 putFile8Bit(file, ei->slippery_type);
7721 /* some free bytes for future properties and padding */
7722 WriteUnusedBytesToFile(file, LEVEL_CPART_CUS3_UNUSED);
7729 if (check != num_changed_custom_elements) /* should not happen */
7730 Error(ERR_WARN, "inconsistent number of custom element properties");
7735 static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
7737 struct ElementInfo *ei = &element_info[element];
7740 /* ---------- custom element base property values (96 bytes) ------------- */
7742 putFile16BitBE(file, element);
7744 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7745 putFile8Bit(file, ei->description[i]);
7747 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7749 WriteUnusedBytesToFile(file, 4); /* reserved for more base properties */
7751 putFile8Bit(file, ei->num_change_pages);
7753 putFile16BitBE(file, ei->ce_value_fixed_initial);
7754 putFile16BitBE(file, ei->ce_value_random_initial);
7755 putFile8Bit(file, ei->use_last_ce_value);
7757 putFile8Bit(file, ei->use_gfx_element);
7758 putFile16BitBE(file, ei->gfx_element_initial);
7760 putFile8Bit(file, ei->collect_score_initial);
7761 putFile8Bit(file, ei->collect_count_initial);
7763 putFile8Bit(file, ei->drop_delay_fixed);
7764 putFile8Bit(file, ei->push_delay_fixed);
7765 putFile8Bit(file, ei->drop_delay_random);
7766 putFile8Bit(file, ei->push_delay_random);
7767 putFile16BitBE(file, ei->move_delay_fixed);
7768 putFile16BitBE(file, ei->move_delay_random);
7770 /* bits 0 - 15 of "move_pattern" ... */
7771 putFile16BitBE(file, ei->move_pattern & 0xffff);
7772 putFile8Bit(file, ei->move_direction_initial);
7773 putFile8Bit(file, ei->move_stepsize);
7775 putFile8Bit(file, ei->slippery_type);
7777 for (y = 0; y < 3; y++)
7778 for (x = 0; x < 3; x++)
7779 putFile16BitBE(file, ei->content.e[x][y]);
7781 putFile16BitBE(file, ei->move_enter_element);
7782 putFile16BitBE(file, ei->move_leave_element);
7783 putFile8Bit(file, ei->move_leave_type);
7785 /* ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible) */
7786 putFile16BitBE(file, (ei->move_pattern >> 16) & 0xffff);
7788 putFile8Bit(file, ei->access_direction);
7790 putFile8Bit(file, ei->explosion_delay);
7791 putFile8Bit(file, ei->ignition_delay);
7792 putFile8Bit(file, ei->explosion_type);
7794 /* some free bytes for future custom property values and padding */
7795 WriteUnusedBytesToFile(file, 1);
7797 /* ---------- change page property values (48 bytes) --------------------- */
7799 for (i = 0; i < ei->num_change_pages; i++)
7801 struct ElementChangeInfo *change = &ei->change_page[i];
7802 unsigned int event_bits;
7804 /* bits 0 - 31 of "has_event[]" ... */
7806 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
7807 if (change->has_event[j])
7808 event_bits |= (1 << j);
7809 putFile32BitBE(file, event_bits);
7811 putFile16BitBE(file, change->target_element);
7813 putFile16BitBE(file, change->delay_fixed);
7814 putFile16BitBE(file, change->delay_random);
7815 putFile16BitBE(file, change->delay_frames);
7817 putFile16BitBE(file, change->initial_trigger_element);
7819 putFile8Bit(file, change->explode);
7820 putFile8Bit(file, change->use_target_content);
7821 putFile8Bit(file, change->only_if_complete);
7822 putFile8Bit(file, change->use_random_replace);
7824 putFile8Bit(file, change->random_percentage);
7825 putFile8Bit(file, change->replace_when);
7827 for (y = 0; y < 3; y++)
7828 for (x = 0; x < 3; x++)
7829 putFile16BitBE(file, change->target_content.e[x][y]);
7831 putFile8Bit(file, change->can_change);
7833 putFile8Bit(file, change->trigger_side);
7835 putFile8Bit(file, change->trigger_player);
7836 putFile8Bit(file, (change->trigger_page == CH_PAGE_ANY ? CH_PAGE_ANY_FILE :
7837 log_2(change->trigger_page)));
7839 putFile8Bit(file, change->has_action);
7840 putFile8Bit(file, change->action_type);
7841 putFile8Bit(file, change->action_mode);
7842 putFile16BitBE(file, change->action_arg);
7844 /* ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible) */
7846 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
7847 if (change->has_event[j])
7848 event_bits |= (1 << (j - 32));
7849 putFile8Bit(file, event_bits);
7855 static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
7857 struct ElementInfo *ei = &element_info[element];
7858 struct ElementGroupInfo *group = ei->group;
7861 putFile16BitBE(file, element);
7863 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7864 putFile8Bit(file, ei->description[i]);
7866 putFile8Bit(file, group->num_elements);
7868 putFile8Bit(file, ei->use_gfx_element);
7869 putFile16BitBE(file, ei->gfx_element_initial);
7871 putFile8Bit(file, group->choice_mode);
7873 /* some free bytes for future values and padding */
7874 WriteUnusedBytesToFile(file, 3);
7876 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
7877 putFile16BitBE(file, group->element[i]);
7881 static int SaveLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *entry,
7882 boolean write_element)
7884 int save_type = entry->save_type;
7885 int data_type = entry->data_type;
7886 int conf_type = entry->conf_type;
7887 int byte_mask = conf_type & CONF_MASK_BYTES;
7888 int element = entry->element;
7889 int default_value = entry->default_value;
7891 boolean modified = FALSE;
7893 if (byte_mask != CONF_MASK_MULTI_BYTES)
7895 void *value_ptr = entry->value;
7896 int value = (data_type == TYPE_BOOLEAN ? *(boolean *)value_ptr :
7899 /* check if any settings have been modified before saving them */
7900 if (value != default_value)
7903 /* do not save if explicitly told or if unmodified default settings */
7904 if ((save_type == SAVE_CONF_NEVER) ||
7905 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7909 num_bytes += putFile16BitBE(file, element);
7911 num_bytes += putFile8Bit(file, conf_type);
7912 num_bytes += (byte_mask == CONF_MASK_1_BYTE ? putFile8Bit (file, value) :
7913 byte_mask == CONF_MASK_2_BYTE ? putFile16BitBE(file, value) :
7914 byte_mask == CONF_MASK_4_BYTE ? putFile32BitBE(file, value) :
7917 else if (data_type == TYPE_STRING)
7919 char *default_string = entry->default_string;
7920 char *string = (char *)(entry->value);
7921 int string_length = strlen(string);
7924 /* check if any settings have been modified before saving them */
7925 if (!strEqual(string, default_string))
7928 /* do not save if explicitly told or if unmodified default settings */
7929 if ((save_type == SAVE_CONF_NEVER) ||
7930 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7934 num_bytes += putFile16BitBE(file, element);
7936 num_bytes += putFile8Bit(file, conf_type);
7937 num_bytes += putFile16BitBE(file, string_length);
7939 for (i = 0; i < string_length; i++)
7940 num_bytes += putFile8Bit(file, string[i]);
7942 else if (data_type == TYPE_ELEMENT_LIST)
7944 int *element_array = (int *)(entry->value);
7945 int num_elements = *(int *)(entry->num_entities);
7948 /* check if any settings have been modified before saving them */
7949 for (i = 0; i < num_elements; i++)
7950 if (element_array[i] != default_value)
7953 /* do not save if explicitly told or if unmodified default settings */
7954 if ((save_type == SAVE_CONF_NEVER) ||
7955 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7959 num_bytes += putFile16BitBE(file, element);
7961 num_bytes += putFile8Bit(file, conf_type);
7962 num_bytes += putFile16BitBE(file, num_elements * CONF_ELEMENT_NUM_BYTES);
7964 for (i = 0; i < num_elements; i++)
7965 num_bytes += putFile16BitBE(file, element_array[i]);
7967 else if (data_type == TYPE_CONTENT_LIST)
7969 struct Content *content = (struct Content *)(entry->value);
7970 int num_contents = *(int *)(entry->num_entities);
7973 /* check if any settings have been modified before saving them */
7974 for (i = 0; i < num_contents; i++)
7975 for (y = 0; y < 3; y++)
7976 for (x = 0; x < 3; x++)
7977 if (content[i].e[x][y] != default_value)
7980 /* do not save if explicitly told or if unmodified default settings */
7981 if ((save_type == SAVE_CONF_NEVER) ||
7982 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7986 num_bytes += putFile16BitBE(file, element);
7988 num_bytes += putFile8Bit(file, conf_type);
7989 num_bytes += putFile16BitBE(file, num_contents * CONF_CONTENT_NUM_BYTES);
7991 for (i = 0; i < num_contents; i++)
7992 for (y = 0; y < 3; y++)
7993 for (x = 0; x < 3; x++)
7994 num_bytes += putFile16BitBE(file, content[i].e[x][y]);
8000 static int SaveLevel_INFO(FILE *file, struct LevelInfo *level)
8005 li = *level; /* copy level data into temporary buffer */
8007 for (i = 0; chunk_config_INFO[i].data_type != -1; i++)
8008 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_INFO[i], FALSE);
8013 static int SaveLevel_ELEM(FILE *file, struct LevelInfo *level)
8018 li = *level; /* copy level data into temporary buffer */
8020 for (i = 0; chunk_config_ELEM[i].data_type != -1; i++)
8021 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_ELEM[i], TRUE);
8026 static int SaveLevel_NOTE(FILE *file, struct LevelInfo *level, int element)
8028 int envelope_nr = element - EL_ENVELOPE_1;
8032 chunk_size += putFile16BitBE(file, element);
8034 /* copy envelope data into temporary buffer */
8035 xx_envelope = level->envelope[envelope_nr];
8037 for (i = 0; chunk_config_NOTE[i].data_type != -1; i++)
8038 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_NOTE[i], FALSE);
8043 static int SaveLevel_CUSX(FILE *file, struct LevelInfo *level, int element)
8045 struct ElementInfo *ei = &element_info[element];
8049 chunk_size += putFile16BitBE(file, element);
8051 xx_ei = *ei; /* copy element data into temporary buffer */
8053 /* set default description string for this specific element */
8054 strcpy(xx_default_description, getDefaultElementDescription(ei));
8057 /* set (fixed) number of content areas (may be wrong by broken level file) */
8058 /* (this is now directly corrected for broken level files after loading) */
8059 xx_num_contents = 1;
8062 for (i = 0; chunk_config_CUSX_base[i].data_type != -1; i++)
8063 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_base[i], FALSE);
8065 for (i = 0; i < ei->num_change_pages; i++)
8067 struct ElementChangeInfo *change = &ei->change_page[i];
8069 xx_current_change_page = i;
8071 xx_change = *change; /* copy change data into temporary buffer */
8074 setEventBitsFromEventFlags(change);
8076 for (j = 0; chunk_config_CUSX_change[j].data_type != -1; j++)
8077 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_change[j],
8084 static int SaveLevel_GRPX(FILE *file, struct LevelInfo *level, int element)
8086 struct ElementInfo *ei = &element_info[element];
8087 struct ElementGroupInfo *group = ei->group;
8091 chunk_size += putFile16BitBE(file, element);
8093 xx_ei = *ei; /* copy element data into temporary buffer */
8094 xx_group = *group; /* copy group data into temporary buffer */
8096 /* set default description string for this specific element */
8097 strcpy(xx_default_description, getDefaultElementDescription(ei));
8099 for (i = 0; chunk_config_GRPX[i].data_type != -1; i++)
8100 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_GRPX[i], FALSE);
8105 static void SaveLevelFromFilename(struct LevelInfo *level, char *filename)
8111 if (!(file = fopen(filename, MODE_WRITE)))
8113 Error(ERR_WARN, "cannot save level file '%s'", filename);
8117 level->file_version = FILE_VERSION_ACTUAL;
8118 level->game_version = GAME_VERSION_ACTUAL;
8120 level->creation_date = getCurrentDate();
8122 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
8123 putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
8125 chunk_size = SaveLevel_VERS(NULL, level);
8126 putFileChunkBE(file, "VERS", chunk_size);
8127 SaveLevel_VERS(file, level);
8129 chunk_size = SaveLevel_DATE(NULL, level);
8130 putFileChunkBE(file, "DATE", chunk_size);
8131 SaveLevel_DATE(file, level);
8133 chunk_size = SaveLevel_NAME(NULL, level);
8134 putFileChunkBE(file, "NAME", chunk_size);
8135 SaveLevel_NAME(file, level);
8137 chunk_size = SaveLevel_AUTH(NULL, level);
8138 putFileChunkBE(file, "AUTH", chunk_size);
8139 SaveLevel_AUTH(file, level);
8141 chunk_size = SaveLevel_INFO(NULL, level);
8142 putFileChunkBE(file, "INFO", chunk_size);
8143 SaveLevel_INFO(file, level);
8145 chunk_size = SaveLevel_BODY(NULL, level);
8146 putFileChunkBE(file, "BODY", chunk_size);
8147 SaveLevel_BODY(file, level);
8149 chunk_size = SaveLevel_ELEM(NULL, level);
8150 if (chunk_size > LEVEL_CHUNK_ELEM_UNCHANGED) /* save if changed */
8152 putFileChunkBE(file, "ELEM", chunk_size);
8153 SaveLevel_ELEM(file, level);
8156 for (i = 0; i < NUM_ENVELOPES; i++)
8158 int element = EL_ENVELOPE_1 + i;
8160 chunk_size = SaveLevel_NOTE(NULL, level, element);
8161 if (chunk_size > LEVEL_CHUNK_NOTE_UNCHANGED) /* save if changed */
8163 putFileChunkBE(file, "NOTE", chunk_size);
8164 SaveLevel_NOTE(file, level, element);
8168 /* if not using template level, check for non-default custom/group elements */
8169 if (!level->use_custom_template)
8171 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
8173 int element = EL_CUSTOM_START + i;
8175 chunk_size = SaveLevel_CUSX(NULL, level, element);
8176 if (chunk_size > LEVEL_CHUNK_CUSX_UNCHANGED) /* save if changed */
8178 putFileChunkBE(file, "CUSX", chunk_size);
8179 SaveLevel_CUSX(file, level, element);
8183 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
8185 int element = EL_GROUP_START + i;
8187 chunk_size = SaveLevel_GRPX(NULL, level, element);
8188 if (chunk_size > LEVEL_CHUNK_GRPX_UNCHANGED) /* save if changed */
8190 putFileChunkBE(file, "GRPX", chunk_size);
8191 SaveLevel_GRPX(file, level, element);
8198 SetFilePermissions(filename, PERMS_PRIVATE);
8201 void SaveLevel(int nr)
8203 char *filename = getDefaultLevelFilename(nr);
8205 SaveLevelFromFilename(&level, filename);
8208 void SaveLevelTemplate()
8210 char *filename = getDefaultLevelFilename(-1);
8212 SaveLevelFromFilename(&level, filename);
8215 boolean SaveLevelChecked(int nr)
8217 char *filename = getDefaultLevelFilename(nr);
8218 boolean new_level = !fileExists(filename);
8219 boolean level_saved = FALSE;
8221 if (new_level || Request("Save this level and kill the old ?", REQ_ASK))
8226 Request("Level saved !", REQ_CONFIRM);
8234 void DumpLevel(struct LevelInfo *level)
8236 if (level->no_valid_file)
8238 Error(ERR_WARN, "cannot dump -- no valid level file found");
8243 printf_line("-", 79);
8244 printf("Level xxx (file version %08d, game version %08d)\n",
8245 level->file_version, level->game_version);
8246 printf_line("-", 79);
8248 printf("Level author: '%s'\n", level->author);
8249 printf("Level title: '%s'\n", level->name);
8251 printf("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
8253 printf("Level time: %d seconds\n", level->time);
8254 printf("Gems needed: %d\n", level->gems_needed);
8256 printf("Time for magic wall: %d seconds\n", level->time_magic_wall);
8257 printf("Time for wheel: %d seconds\n", level->time_wheel);
8258 printf("Time for light: %d seconds\n", level->time_light);
8259 printf("Time for timegate: %d seconds\n", level->time_timegate);
8261 printf("Amoeba speed: %d\n", level->amoeba_speed);
8264 printf("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
8265 printf("Player blocks last field: %s\n", (level->block_last_field ? "yes" : "no"));
8266 printf("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
8267 printf("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
8268 printf("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
8270 printf_line("-", 79);
8274 /* ========================================================================= */
8275 /* tape file functions */
8276 /* ========================================================================= */
8278 static void setTapeInfoToDefaults()
8282 /* always start with reliable default values (empty tape) */
8285 /* default values (also for pre-1.2 tapes) with only the first player */
8286 tape.player_participates[0] = TRUE;
8287 for (i = 1; i < MAX_PLAYERS; i++)
8288 tape.player_participates[i] = FALSE;
8290 /* at least one (default: the first) player participates in every tape */
8291 tape.num_participating_players = 1;
8293 tape.level_nr = level_nr;
8295 tape.changed = FALSE;
8297 tape.recording = FALSE;
8298 tape.playing = FALSE;
8299 tape.pausing = FALSE;
8301 tape.no_valid_file = FALSE;
8304 static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape)
8306 tape->file_version = getFileVersion(file);
8307 tape->game_version = getFileVersion(file);
8312 static int LoadTape_HEAD(FILE *file, int chunk_size, struct TapeInfo *tape)
8316 tape->random_seed = getFile32BitBE(file);
8317 tape->date = getFile32BitBE(file);
8318 tape->length = getFile32BitBE(file);
8320 /* read header fields that are new since version 1.2 */
8321 if (tape->file_version >= FILE_VERSION_1_2)
8323 byte store_participating_players = getFile8Bit(file);
8326 /* since version 1.2, tapes store which players participate in the tape */
8327 tape->num_participating_players = 0;
8328 for (i = 0; i < MAX_PLAYERS; i++)
8330 tape->player_participates[i] = FALSE;
8332 if (store_participating_players & (1 << i))
8334 tape->player_participates[i] = TRUE;
8335 tape->num_participating_players++;
8339 ReadUnusedBytesFromFile(file, TAPE_CHUNK_HEAD_UNUSED);
8341 engine_version = getFileVersion(file);
8342 if (engine_version > 0)
8343 tape->engine_version = engine_version;
8345 tape->engine_version = tape->game_version;
8351 static int LoadTape_INFO(FILE *file, int chunk_size, struct TapeInfo *tape)
8353 int level_identifier_size;
8356 level_identifier_size = getFile16BitBE(file);
8358 tape->level_identifier =
8359 checked_realloc(tape->level_identifier, level_identifier_size);
8361 for (i = 0; i < level_identifier_size; i++)
8362 tape->level_identifier[i] = getFile8Bit(file);
8364 tape->level_nr = getFile16BitBE(file);
8366 chunk_size = 2 + level_identifier_size + 2;
8371 static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
8374 int chunk_size_expected =
8375 (tape->num_participating_players + 1) * tape->length;
8377 if (chunk_size_expected != chunk_size)
8379 ReadUnusedBytesFromFile(file, chunk_size);
8380 return chunk_size_expected;
8383 for (i = 0; i < tape->length; i++)
8385 if (i >= MAX_TAPE_LEN)
8388 for (j = 0; j < MAX_PLAYERS; j++)
8390 tape->pos[i].action[j] = MV_NONE;
8392 if (tape->player_participates[j])
8393 tape->pos[i].action[j] = getFile8Bit(file);
8396 tape->pos[i].delay = getFile8Bit(file);
8398 if (tape->file_version == FILE_VERSION_1_0)
8400 /* eliminate possible diagonal moves in old tapes */
8401 /* this is only for backward compatibility */
8403 byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
8404 byte action = tape->pos[i].action[0];
8405 int k, num_moves = 0;
8407 for (k = 0; k<4; k++)
8409 if (action & joy_dir[k])
8411 tape->pos[i + num_moves].action[0] = joy_dir[k];
8413 tape->pos[i + num_moves].delay = 0;
8422 tape->length += num_moves;
8425 else if (tape->file_version < FILE_VERSION_2_0)
8427 /* convert pre-2.0 tapes to new tape format */
8429 if (tape->pos[i].delay > 1)
8432 tape->pos[i + 1] = tape->pos[i];
8433 tape->pos[i + 1].delay = 1;
8436 for (j = 0; j < MAX_PLAYERS; j++)
8437 tape->pos[i].action[j] = MV_NONE;
8438 tape->pos[i].delay--;
8449 if (i != tape->length)
8450 chunk_size = (tape->num_participating_players + 1) * i;
8455 void LoadTapeFromFilename(char *filename)
8457 char cookie[MAX_LINE_LEN];
8458 char chunk_name[CHUNK_ID_LEN + 1];
8462 /* always start with reliable default values */
8463 setTapeInfoToDefaults();
8465 if (!(file = fopen(filename, MODE_READ)))
8467 tape.no_valid_file = TRUE;
8472 getFileChunkBE(file, chunk_name, NULL);
8473 if (strEqual(chunk_name, "RND1"))
8475 getFile32BitBE(file); /* not used */
8477 getFileChunkBE(file, chunk_name, NULL);
8478 if (!strEqual(chunk_name, "TAPE"))
8480 tape.no_valid_file = TRUE;
8482 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
8487 else /* check for pre-2.0 file format with cookie string */
8489 strcpy(cookie, chunk_name);
8490 fgets(&cookie[4], MAX_LINE_LEN - 4, file);
8491 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8492 cookie[strlen(cookie) - 1] = '\0';
8494 if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
8496 tape.no_valid_file = TRUE;
8498 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
8503 if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
8505 tape.no_valid_file = TRUE;
8507 Error(ERR_WARN, "unsupported version of tape file '%s'", filename);
8512 /* pre-2.0 tape files have no game version, so use file version here */
8513 tape.game_version = tape.file_version;
8516 if (tape.file_version < FILE_VERSION_1_2)
8518 /* tape files from versions before 1.2.0 without chunk structure */
8519 LoadTape_HEAD(file, TAPE_CHUNK_HEAD_SIZE, &tape);
8520 LoadTape_BODY(file, 2 * tape.length, &tape);
8528 int (*loader)(FILE *, int, struct TapeInfo *);
8532 { "VERS", TAPE_CHUNK_VERS_SIZE, LoadTape_VERS },
8533 { "HEAD", TAPE_CHUNK_HEAD_SIZE, LoadTape_HEAD },
8534 { "INFO", -1, LoadTape_INFO },
8535 { "BODY", -1, LoadTape_BODY },
8539 while (getFileChunkBE(file, chunk_name, &chunk_size))
8543 while (chunk_info[i].name != NULL &&
8544 !strEqual(chunk_name, chunk_info[i].name))
8547 if (chunk_info[i].name == NULL)
8549 Error(ERR_WARN, "unknown chunk '%s' in tape file '%s'",
8550 chunk_name, filename);
8551 ReadUnusedBytesFromFile(file, chunk_size);
8553 else if (chunk_info[i].size != -1 &&
8554 chunk_info[i].size != chunk_size)
8556 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
8557 chunk_size, chunk_name, filename);
8558 ReadUnusedBytesFromFile(file, chunk_size);
8562 /* call function to load this tape chunk */
8563 int chunk_size_expected =
8564 (chunk_info[i].loader)(file, chunk_size, &tape);
8566 /* the size of some chunks cannot be checked before reading other
8567 chunks first (like "HEAD" and "BODY") that contain some header
8568 information, so check them here */
8569 if (chunk_size_expected != chunk_size)
8571 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
8572 chunk_size, chunk_name, filename);
8580 tape.length_seconds = GetTapeLength();
8583 printf("::: tape file version: %d\n", tape.file_version);
8584 printf("::: tape game version: %d\n", tape.game_version);
8585 printf("::: tape engine version: %d\n", tape.engine_version);
8589 void LoadTape(int nr)
8591 char *filename = getTapeFilename(nr);
8593 LoadTapeFromFilename(filename);
8596 void LoadSolutionTape(int nr)
8598 char *filename = getSolutionTapeFilename(nr);
8600 LoadTapeFromFilename(filename);
8603 if (TAPE_IS_EMPTY(tape) &&
8604 level.game_engine_type == GAME_ENGINE_TYPE_SP &&
8605 level.native_sp_level->demo.is_available)
8606 CopyNativeTape_SP_to_RND(&level);
8610 static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
8612 putFileVersion(file, tape->file_version);
8613 putFileVersion(file, tape->game_version);
8616 static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
8619 byte store_participating_players = 0;
8621 /* set bits for participating players for compact storage */
8622 for (i = 0; i < MAX_PLAYERS; i++)
8623 if (tape->player_participates[i])
8624 store_participating_players |= (1 << i);
8626 putFile32BitBE(file, tape->random_seed);
8627 putFile32BitBE(file, tape->date);
8628 putFile32BitBE(file, tape->length);
8630 putFile8Bit(file, store_participating_players);
8632 /* unused bytes not at the end here for 4-byte alignment of engine_version */
8633 WriteUnusedBytesToFile(file, TAPE_CHUNK_HEAD_UNUSED);
8635 putFileVersion(file, tape->engine_version);
8638 static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
8640 int level_identifier_size = strlen(tape->level_identifier) + 1;
8643 putFile16BitBE(file, level_identifier_size);
8645 for (i = 0; i < level_identifier_size; i++)
8646 putFile8Bit(file, tape->level_identifier[i]);
8648 putFile16BitBE(file, tape->level_nr);
8651 static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
8655 for (i = 0; i < tape->length; i++)
8657 for (j = 0; j < MAX_PLAYERS; j++)
8658 if (tape->player_participates[j])
8659 putFile8Bit(file, tape->pos[i].action[j]);
8661 putFile8Bit(file, tape->pos[i].delay);
8665 void SaveTape(int nr)
8667 char *filename = getTapeFilename(nr);
8670 boolean new_tape = TRUE;
8672 int num_participating_players = 0;
8673 int info_chunk_size;
8674 int body_chunk_size;
8677 InitTapeDirectory(leveldir_current->subdir);
8680 /* if a tape still exists, ask to overwrite it */
8681 if (fileExists(filename))
8684 if (!Request("Replace old tape ?", REQ_ASK))
8689 if (!(file = fopen(filename, MODE_WRITE)))
8691 Error(ERR_WARN, "cannot save level recording file '%s'", filename);
8695 tape.file_version = FILE_VERSION_ACTUAL;
8696 tape.game_version = GAME_VERSION_ACTUAL;
8698 /* count number of participating players */
8699 for (i = 0; i < MAX_PLAYERS; i++)
8700 if (tape.player_participates[i])
8701 num_participating_players++;
8703 info_chunk_size = 2 + (strlen(tape.level_identifier) + 1) + 2;
8704 body_chunk_size = (num_participating_players + 1) * tape.length;
8706 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
8707 putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
8709 putFileChunkBE(file, "VERS", TAPE_CHUNK_VERS_SIZE);
8710 SaveTape_VERS(file, &tape);
8712 putFileChunkBE(file, "HEAD", TAPE_CHUNK_HEAD_SIZE);
8713 SaveTape_HEAD(file, &tape);
8715 putFileChunkBE(file, "INFO", info_chunk_size);
8716 SaveTape_INFO(file, &tape);
8718 putFileChunkBE(file, "BODY", body_chunk_size);
8719 SaveTape_BODY(file, &tape);
8723 SetFilePermissions(filename, PERMS_PRIVATE);
8725 tape.changed = FALSE;
8729 Request("Tape saved !", REQ_CONFIRM);
8733 boolean SaveTapeChecked(int nr)
8735 char *filename = getTapeFilename(nr);
8736 boolean new_tape = !fileExists(filename);
8737 boolean tape_saved = FALSE;
8739 if (new_tape || Request("Replace old tape ?", REQ_ASK))
8744 Request("Tape saved !", REQ_CONFIRM);
8752 void DumpTape(struct TapeInfo *tape)
8754 int tape_frame_counter;
8757 if (tape->no_valid_file)
8759 Error(ERR_WARN, "cannot dump -- no valid tape file found");
8764 printf_line("-", 79);
8765 printf("Tape of Level %03d (file version %08d, game version %08d)\n",
8766 tape->level_nr, tape->file_version, tape->game_version);
8767 printf(" (effective engine version %08d)\n",
8768 tape->engine_version);
8769 printf("Level series identifier: '%s'\n", tape->level_identifier);
8770 printf_line("-", 79);
8772 tape_frame_counter = 0;
8774 for (i = 0; i < tape->length; i++)
8776 if (i >= MAX_TAPE_LEN)
8779 printf("%04d: ", i);
8781 for (j = 0; j < MAX_PLAYERS; j++)
8783 if (tape->player_participates[j])
8785 int action = tape->pos[i].action[j];
8787 printf("%d:%02x ", j, action);
8788 printf("[%c%c%c%c|%c%c] - ",
8789 (action & JOY_LEFT ? '<' : ' '),
8790 (action & JOY_RIGHT ? '>' : ' '),
8791 (action & JOY_UP ? '^' : ' '),
8792 (action & JOY_DOWN ? 'v' : ' '),
8793 (action & JOY_BUTTON_1 ? '1' : ' '),
8794 (action & JOY_BUTTON_2 ? '2' : ' '));
8798 printf("(%03d) ", tape->pos[i].delay);
8799 printf("[%05d]\n", tape_frame_counter);
8801 tape_frame_counter += tape->pos[i].delay;
8804 printf_line("-", 79);
8808 /* ========================================================================= */
8809 /* score file functions */
8810 /* ========================================================================= */
8812 void LoadScore(int nr)
8815 char *filename = getScoreFilename(nr);
8816 char cookie[MAX_LINE_LEN];
8817 char line[MAX_LINE_LEN];
8821 /* always start with reliable default values */
8822 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8824 strcpy(highscore[i].Name, EMPTY_PLAYER_NAME);
8825 highscore[i].Score = 0;
8828 if (!(file = fopen(filename, MODE_READ)))
8831 /* check file identifier */
8832 fgets(cookie, MAX_LINE_LEN, file);
8833 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8834 cookie[strlen(cookie) - 1] = '\0';
8836 if (!checkCookieString(cookie, SCORE_COOKIE))
8838 Error(ERR_WARN, "unknown format of score file '%s'", filename);
8843 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8845 fscanf(file, "%d", &highscore[i].Score);
8846 fgets(line, MAX_LINE_LEN, file);
8848 if (line[strlen(line) - 1] == '\n')
8849 line[strlen(line) - 1] = '\0';
8851 for (line_ptr = line; *line_ptr; line_ptr++)
8853 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
8855 strncpy(highscore[i].Name, line_ptr, MAX_PLAYER_NAME_LEN);
8856 highscore[i].Name[MAX_PLAYER_NAME_LEN] = '\0';
8865 void SaveScore(int nr)
8868 char *filename = getScoreFilename(nr);
8871 InitScoreDirectory(leveldir_current->subdir);
8873 if (!(file = fopen(filename, MODE_WRITE)))
8875 Error(ERR_WARN, "cannot save score for level %d", nr);
8879 fprintf(file, "%s\n\n", SCORE_COOKIE);
8881 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8882 fprintf(file, "%d %s\n", highscore[i].Score, highscore[i].Name);
8886 SetFilePermissions(filename, PERMS_PUBLIC);
8890 /* ========================================================================= */
8891 /* setup file functions */
8892 /* ========================================================================= */
8894 #define TOKEN_STR_PLAYER_PREFIX "player_"
8897 #define SETUP_TOKEN_PLAYER_NAME 0
8898 #define SETUP_TOKEN_SOUND 1
8899 #define SETUP_TOKEN_SOUND_LOOPS 2
8900 #define SETUP_TOKEN_SOUND_MUSIC 3
8901 #define SETUP_TOKEN_SOUND_SIMPLE 4
8902 #define SETUP_TOKEN_TOONS 5
8903 #define SETUP_TOKEN_SCROLL_DELAY 6
8904 #define SETUP_TOKEN_SCROLL_DELAY_VALUE 7
8905 #define SETUP_TOKEN_SOFT_SCROLLING 8
8906 #define SETUP_TOKEN_FADE_SCREENS 9
8907 #define SETUP_TOKEN_AUTORECORD 10
8908 #define SETUP_TOKEN_SHOW_TITLESCREEN 11
8909 #define SETUP_TOKEN_QUICK_DOORS 12
8910 #define SETUP_TOKEN_TEAM_MODE 13
8911 #define SETUP_TOKEN_HANDICAP 14
8912 #define SETUP_TOKEN_SKIP_LEVELS 15
8913 #define SETUP_TOKEN_TIME_LIMIT 16
8914 #define SETUP_TOKEN_FULLSCREEN 17
8915 #define SETUP_TOKEN_FULLSCREEN_MODE 18
8916 #define SETUP_TOKEN_ASK_ON_ESCAPE 19
8917 #define SETUP_TOKEN_ASK_ON_ESCAPE_EDITOR 20
8918 #define SETUP_TOKEN_QUICK_SWITCH 21
8919 #define SETUP_TOKEN_INPUT_ON_FOCUS 22
8920 #define SETUP_TOKEN_PREFER_AGA_GRAPHICS 23
8921 #define SETUP_TOKEN_GAME_FRAME_DELAY 24
8922 #define SETUP_TOKEN_SP_SHOW_BORDER_ELEMENTS 25
8923 #define SETUP_TOKEN_GRAPHICS_SET 26
8924 #define SETUP_TOKEN_SOUNDS_SET 27
8925 #define SETUP_TOKEN_MUSIC_SET 28
8926 #define SETUP_TOKEN_OVERRIDE_LEVEL_GRAPHICS 29
8927 #define SETUP_TOKEN_OVERRIDE_LEVEL_SOUNDS 30
8928 #define SETUP_TOKEN_OVERRIDE_LEVEL_MUSIC 31
8930 #define NUM_GLOBAL_SETUP_TOKENS 32
8933 #define SETUP_TOKEN_EDITOR_EL_BOULDERDASH 0
8934 #define SETUP_TOKEN_EDITOR_EL_EMERALD_MINE 1
8935 #define SETUP_TOKEN_EDITOR_EL_EMERALD_MINE_CLUB 2
8936 #define SETUP_TOKEN_EDITOR_EL_MORE 3
8937 #define SETUP_TOKEN_EDITOR_EL_SOKOBAN 4
8938 #define SETUP_TOKEN_EDITOR_EL_SUPAPLEX 5
8939 #define SETUP_TOKEN_EDITOR_EL_DIAMOND_CAVES 6
8940 #define SETUP_TOKEN_EDITOR_EL_DX_BOULDERDASH 7
8941 #define SETUP_TOKEN_EDITOR_EL_CHARS 8
8942 #define SETUP_TOKEN_EDITOR_EL_STEEL_CHARS 9
8943 #define SETUP_TOKEN_EDITOR_EL_CUSTOM 10
8944 #define SETUP_TOKEN_EDITOR_EL_HEADLINES 11
8945 #define SETUP_TOKEN_EDITOR_EL_USER_DEFINED 12
8946 #define SETUP_TOKEN_EDITOR_EL_DYNAMIC 13
8947 #define SETUP_TOKEN_EDITOR_EL_BY_GAME 14
8948 #define SETUP_TOKEN_EDITOR_EL_BY_TYPE 15
8949 #define SETUP_TOKEN_EDITOR_SHOW_ELEMENT_TOKEN 16
8951 #define NUM_EDITOR_SETUP_TOKENS 17
8953 /* editor cascade setup */
8954 #define SETUP_TOKEN_EDITOR_CASCADE_BD 0
8955 #define SETUP_TOKEN_EDITOR_CASCADE_EM 1
8956 #define SETUP_TOKEN_EDITOR_CASCADE_EMC 2
8957 #define SETUP_TOKEN_EDITOR_CASCADE_RND 3
8958 #define SETUP_TOKEN_EDITOR_CASCADE_SB 4
8959 #define SETUP_TOKEN_EDITOR_CASCADE_SP 5
8960 #define SETUP_TOKEN_EDITOR_CASCADE_DC 6
8961 #define SETUP_TOKEN_EDITOR_CASCADE_DX 7
8962 #define SETUP_TOKEN_EDITOR_CASCADE_TEXT 8
8963 #define SETUP_TOKEN_EDITOR_CASCADE_STEELTEXT 9
8964 #define SETUP_TOKEN_EDITOR_CASCADE_CE 10
8965 #define SETUP_TOKEN_EDITOR_CASCADE_GE 11
8966 #define SETUP_TOKEN_EDITOR_CASCADE_REF 12
8967 #define SETUP_TOKEN_EDITOR_CASCADE_USER 13
8968 #define SETUP_TOKEN_EDITOR_CASCADE_DYNAMIC 14
8970 #define NUM_EDITOR_CASCADE_SETUP_TOKENS 15
8972 /* shortcut setup */
8973 #define SETUP_TOKEN_SHORTCUT_SAVE_GAME 0
8974 #define SETUP_TOKEN_SHORTCUT_LOAD_GAME 1
8975 #define SETUP_TOKEN_SHORTCUT_TOGGLE_PAUSE 2
8976 #define SETUP_TOKEN_SHORTCUT_FOCUS_PLAYER_1 3
8977 #define SETUP_TOKEN_SHORTCUT_FOCUS_PLAYER_2 4
8978 #define SETUP_TOKEN_SHORTCUT_FOCUS_PLAYER_3 5
8979 #define SETUP_TOKEN_SHORTCUT_FOCUS_PLAYER_4 6
8980 #define SETUP_TOKEN_SHORTCUT_FOCUS_PLAYER_ALL 7
8981 #define SETUP_TOKEN_SHORTCUT_TAPE_EJECT 8
8982 #define SETUP_TOKEN_SHORTCUT_TAPE_STOP 9
8983 #define SETUP_TOKEN_SHORTCUT_TAPE_PAUSE 10
8984 #define SETUP_TOKEN_SHORTCUT_TAPE_RECORD 11
8985 #define SETUP_TOKEN_SHORTCUT_TAPE_PLAY 12
8986 #define SETUP_TOKEN_SHORTCUT_SOUND_SIMPLE 13
8987 #define SETUP_TOKEN_SHORTCUT_SOUND_LOOPS 14
8988 #define SETUP_TOKEN_SHORTCUT_SOUND_MUSIC 15
8990 #define NUM_SHORTCUT_SETUP_TOKENS 16
8993 #define SETUP_TOKEN_PLAYER_USE_JOYSTICK 0
8994 #define SETUP_TOKEN_PLAYER_JOY_DEVICE_NAME 1
8995 #define SETUP_TOKEN_PLAYER_JOY_XLEFT 2
8996 #define SETUP_TOKEN_PLAYER_JOY_XMIDDLE 3
8997 #define SETUP_TOKEN_PLAYER_JOY_XRIGHT 4
8998 #define SETUP_TOKEN_PLAYER_JOY_YUPPER 5
8999 #define SETUP_TOKEN_PLAYER_JOY_YMIDDLE 6
9000 #define SETUP_TOKEN_PLAYER_JOY_YLOWER 7
9001 #define SETUP_TOKEN_PLAYER_JOY_SNAP 8
9002 #define SETUP_TOKEN_PLAYER_JOY_DROP 9
9003 #define SETUP_TOKEN_PLAYER_KEY_LEFT 10
9004 #define SETUP_TOKEN_PLAYER_KEY_RIGHT 11
9005 #define SETUP_TOKEN_PLAYER_KEY_UP 12
9006 #define SETUP_TOKEN_PLAYER_KEY_DOWN 13
9007 #define SETUP_TOKEN_PLAYER_KEY_SNAP 14
9008 #define SETUP_TOKEN_PLAYER_KEY_DROP 15
9010 #define NUM_PLAYER_SETUP_TOKENS 16
9013 #define SETUP_TOKEN_SYSTEM_SDL_VIDEODRIVER 0
9014 #define SETUP_TOKEN_SYSTEM_SDL_AUDIODRIVER 1
9015 #define SETUP_TOKEN_SYSTEM_AUDIO_FRAGMENT_SIZE 2
9017 #define NUM_SYSTEM_SETUP_TOKENS 3
9020 #define SETUP_TOKEN_OPTIONS_VERBOSE 0
9022 #define NUM_OPTIONS_SETUP_TOKENS 1
9025 static struct SetupInfo si;
9026 static struct SetupEditorInfo sei;
9027 static struct SetupEditorCascadeInfo seci;
9028 static struct SetupShortcutInfo ssi;
9029 static struct SetupInputInfo sii;
9030 static struct SetupSystemInfo syi;
9031 static struct OptionInfo soi;
9033 static struct TokenInfo global_setup_tokens[] =
9035 { TYPE_STRING, &si.player_name, "player_name" },
9036 { TYPE_SWITCH, &si.sound, "sound" },
9037 { TYPE_SWITCH, &si.sound_loops, "repeating_sound_loops" },
9038 { TYPE_SWITCH, &si.sound_music, "background_music" },
9039 { TYPE_SWITCH, &si.sound_simple, "simple_sound_effects" },
9040 { TYPE_SWITCH, &si.toons, "toons" },
9041 { TYPE_SWITCH, &si.scroll_delay, "scroll_delay" },
9042 { TYPE_INTEGER,&si.scroll_delay_value, "scroll_delay_value" },
9043 { TYPE_SWITCH, &si.soft_scrolling, "soft_scrolling" },
9044 { TYPE_SWITCH, &si.fade_screens, "fade_screens" },
9045 { TYPE_SWITCH, &si.autorecord, "automatic_tape_recording"},
9046 { TYPE_SWITCH, &si.show_titlescreen, "show_titlescreen" },
9047 { TYPE_SWITCH, &si.quick_doors, "quick_doors" },
9048 { TYPE_SWITCH, &si.team_mode, "team_mode" },
9049 { TYPE_SWITCH, &si.handicap, "handicap" },
9050 { TYPE_SWITCH, &si.skip_levels, "skip_levels" },
9051 { TYPE_SWITCH, &si.time_limit, "time_limit" },
9052 { TYPE_SWITCH, &si.fullscreen, "fullscreen" },
9053 { TYPE_STRING, &si.fullscreen_mode, "fullscreen_mode" },
9054 { TYPE_SWITCH, &si.ask_on_escape, "ask_on_escape" },
9055 { TYPE_SWITCH, &si.ask_on_escape_editor, "ask_on_escape_editor" },
9056 { TYPE_SWITCH, &si.quick_switch, "quick_player_switch" },
9057 { TYPE_SWITCH, &si.input_on_focus, "input_on_focus" },
9058 { TYPE_SWITCH, &si.prefer_aga_graphics, "prefer_aga_graphics" },
9059 { TYPE_INTEGER,&si.game_frame_delay, "game_frame_delay" },
9060 { TYPE_SWITCH, &si.sp_show_border_elements, "sp_show_border_elements" },
9061 { TYPE_STRING, &si.graphics_set, "graphics_set" },
9062 { TYPE_STRING, &si.sounds_set, "sounds_set" },
9063 { TYPE_STRING, &si.music_set, "music_set" },
9064 { TYPE_SWITCH3,&si.override_level_graphics, "override_level_graphics" },
9065 { TYPE_SWITCH3,&si.override_level_sounds, "override_level_sounds" },
9066 { TYPE_SWITCH3,&si.override_level_music, "override_level_music" },
9069 static boolean not_used = FALSE;
9070 static struct TokenInfo editor_setup_tokens[] =
9073 { TYPE_SWITCH, ¬_used, "editor.el_boulderdash" },
9074 { TYPE_SWITCH, ¬_used, "editor.el_emerald_mine" },
9075 { TYPE_SWITCH, ¬_used, "editor.el_emerald_mine_club" },
9076 { TYPE_SWITCH, ¬_used, "editor.el_more" },
9077 { TYPE_SWITCH, ¬_used, "editor.el_sokoban" },
9078 { TYPE_SWITCH, ¬_used, "editor.el_supaplex" },
9079 { TYPE_SWITCH, ¬_used, "editor.el_diamond_caves" },
9080 { TYPE_SWITCH, ¬_used, "editor.el_dx_boulderdash" },
9082 { TYPE_SWITCH, &sei.el_boulderdash, "editor.el_boulderdash" },
9083 { TYPE_SWITCH, &sei.el_emerald_mine, "editor.el_emerald_mine" },
9084 { TYPE_SWITCH, &sei.el_emerald_mine_club,"editor.el_emerald_mine_club"},
9085 { TYPE_SWITCH, &sei.el_more, "editor.el_more" },
9086 { TYPE_SWITCH, &sei.el_sokoban, "editor.el_sokoban" },
9087 { TYPE_SWITCH, &sei.el_supaplex, "editor.el_supaplex" },
9088 { TYPE_SWITCH, &sei.el_diamond_caves, "editor.el_diamond_caves" },
9089 { TYPE_SWITCH, &sei.el_dx_boulderdash,"editor.el_dx_boulderdash" },
9091 { TYPE_SWITCH, &sei.el_chars, "editor.el_chars" },
9092 { TYPE_SWITCH, &sei.el_steel_chars, "editor.el_steel_chars" },
9093 { TYPE_SWITCH, &sei.el_custom, "editor.el_custom" },
9095 { TYPE_SWITCH, ¬_used, "editor.el_headlines" },
9097 { TYPE_SWITCH, &sei.el_headlines, "editor.el_headlines" },
9099 { TYPE_SWITCH, &sei.el_user_defined, "editor.el_user_defined" },
9100 { TYPE_SWITCH, &sei.el_dynamic, "editor.el_dynamic" },
9101 { TYPE_SWITCH, &sei.el_by_game, "editor.el_by_game" },
9102 { TYPE_SWITCH, &sei.el_by_type, "editor.el_by_type" },
9103 { TYPE_SWITCH, &sei.show_element_token,"editor.show_element_token" },
9106 static struct TokenInfo editor_cascade_setup_tokens[] =
9108 { TYPE_SWITCH, &seci.el_bd, "editor.cascade.el_bd" },
9109 { TYPE_SWITCH, &seci.el_em, "editor.cascade.el_em" },
9110 { TYPE_SWITCH, &seci.el_emc, "editor.cascade.el_emc" },
9111 { TYPE_SWITCH, &seci.el_rnd, "editor.cascade.el_rnd" },
9112 { TYPE_SWITCH, &seci.el_sb, "editor.cascade.el_sb" },
9113 { TYPE_SWITCH, &seci.el_sp, "editor.cascade.el_sp" },
9114 { TYPE_SWITCH, &seci.el_dc, "editor.cascade.el_dc" },
9115 { TYPE_SWITCH, &seci.el_dx, "editor.cascade.el_dx" },
9116 { TYPE_SWITCH, &seci.el_chars, "editor.cascade.el_chars" },
9117 { TYPE_SWITCH, &seci.el_steel_chars, "editor.cascade.el_steel_chars" },
9118 { TYPE_SWITCH, &seci.el_ce, "editor.cascade.el_ce" },
9119 { TYPE_SWITCH, &seci.el_ge, "editor.cascade.el_ge" },
9120 { TYPE_SWITCH, &seci.el_ref, "editor.cascade.el_ref" },
9121 { TYPE_SWITCH, &seci.el_user, "editor.cascade.el_user" },
9122 { TYPE_SWITCH, &seci.el_dynamic, "editor.cascade.el_dynamic" },
9125 static struct TokenInfo shortcut_setup_tokens[] =
9127 { TYPE_KEY_X11, &ssi.save_game, "shortcut.save_game" },
9128 { TYPE_KEY_X11, &ssi.load_game, "shortcut.load_game" },
9129 { TYPE_KEY_X11, &ssi.toggle_pause, "shortcut.toggle_pause" },
9130 { TYPE_KEY_X11, &ssi.focus_player[0], "shortcut.focus_player_1" },
9131 { TYPE_KEY_X11, &ssi.focus_player[1], "shortcut.focus_player_2" },
9132 { TYPE_KEY_X11, &ssi.focus_player[2], "shortcut.focus_player_3" },
9133 { TYPE_KEY_X11, &ssi.focus_player[3], "shortcut.focus_player_4" },
9134 { TYPE_KEY_X11, &ssi.focus_player_all,"shortcut.focus_player_all" },
9135 { TYPE_KEY_X11, &ssi.tape_eject, "shortcut.tape_eject" },
9136 { TYPE_KEY_X11, &ssi.tape_stop, "shortcut.tape_stop" },
9137 { TYPE_KEY_X11, &ssi.tape_pause, "shortcut.tape_pause" },
9138 { TYPE_KEY_X11, &ssi.tape_record, "shortcut.tape_record" },
9139 { TYPE_KEY_X11, &ssi.tape_play, "shortcut.tape_play" },
9140 { TYPE_KEY_X11, &ssi.sound_simple, "shortcut.sound_simple" },
9141 { TYPE_KEY_X11, &ssi.sound_loops, "shortcut.sound_loops" },
9142 { TYPE_KEY_X11, &ssi.sound_music, "shortcut.sound_music" },
9145 static struct TokenInfo player_setup_tokens[] =
9147 { TYPE_BOOLEAN, &sii.use_joystick, ".use_joystick" },
9148 { TYPE_STRING, &sii.joy.device_name, ".joy.device_name" },
9149 { TYPE_INTEGER, &sii.joy.xleft, ".joy.xleft" },
9150 { TYPE_INTEGER, &sii.joy.xmiddle, ".joy.xmiddle" },
9151 { TYPE_INTEGER, &sii.joy.xright, ".joy.xright" },
9152 { TYPE_INTEGER, &sii.joy.yupper, ".joy.yupper" },
9153 { TYPE_INTEGER, &sii.joy.ymiddle, ".joy.ymiddle" },
9154 { TYPE_INTEGER, &sii.joy.ylower, ".joy.ylower" },
9155 { TYPE_INTEGER, &sii.joy.snap, ".joy.snap_field" },
9156 { TYPE_INTEGER, &sii.joy.drop, ".joy.place_bomb" },
9157 { TYPE_KEY_X11, &sii.key.left, ".key.move_left" },
9158 { TYPE_KEY_X11, &sii.key.right, ".key.move_right" },
9159 { TYPE_KEY_X11, &sii.key.up, ".key.move_up" },
9160 { TYPE_KEY_X11, &sii.key.down, ".key.move_down" },
9161 { TYPE_KEY_X11, &sii.key.snap, ".key.snap_field" },
9162 { TYPE_KEY_X11, &sii.key.drop, ".key.place_bomb" },
9165 static struct TokenInfo system_setup_tokens[] =
9167 { TYPE_STRING, &syi.sdl_videodriver, "system.sdl_videodriver" },
9168 { TYPE_STRING, &syi.sdl_audiodriver, "system.sdl_audiodriver" },
9169 { TYPE_INTEGER, &syi.audio_fragment_size,"system.audio_fragment_size" },
9172 static struct TokenInfo options_setup_tokens[] =
9174 { TYPE_BOOLEAN, &soi.verbose, "options.verbose" },
9177 static char *get_corrected_login_name(char *login_name)
9179 /* needed because player name must be a fixed length string */
9180 char *login_name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1);
9182 strncpy(login_name_new, login_name, MAX_PLAYER_NAME_LEN);
9183 login_name_new[MAX_PLAYER_NAME_LEN] = '\0';
9185 if (strlen(login_name) > MAX_PLAYER_NAME_LEN) /* name has been cut */
9186 if (strchr(login_name_new, ' '))
9187 *strchr(login_name_new, ' ') = '\0';
9189 return login_name_new;
9192 static void setSetupInfoToDefaults(struct SetupInfo *si)
9196 si->player_name = get_corrected_login_name(getLoginName());
9199 si->sound_loops = TRUE;
9200 si->sound_music = TRUE;
9201 si->sound_simple = TRUE;
9203 si->scroll_delay = TRUE;
9204 si->scroll_delay_value = STD_SCROLL_DELAY;
9205 si->soft_scrolling = TRUE;
9206 si->fade_screens = TRUE;
9207 si->autorecord = TRUE;
9208 si->show_titlescreen = TRUE;
9209 si->quick_doors = FALSE;
9210 si->team_mode = FALSE;
9211 si->handicap = TRUE;
9212 si->skip_levels = TRUE;
9213 si->time_limit = TRUE;
9214 si->fullscreen = FALSE;
9215 si->fullscreen_mode = getStringCopy(DEFAULT_FULLSCREEN_MODE);
9216 si->ask_on_escape = TRUE;
9217 si->ask_on_escape_editor = TRUE;
9218 si->quick_switch = FALSE;
9219 si->input_on_focus = FALSE;
9220 si->prefer_aga_graphics = TRUE;
9221 si->game_frame_delay = GAME_FRAME_DELAY;
9222 si->sp_show_border_elements = FALSE;
9224 si->graphics_set = getStringCopy(GFX_DEFAULT_SUBDIR);
9225 si->sounds_set = getStringCopy(SND_DEFAULT_SUBDIR);
9226 si->music_set = getStringCopy(MUS_DEFAULT_SUBDIR);
9227 si->override_level_graphics = FALSE;
9228 si->override_level_sounds = FALSE;
9229 si->override_level_music = FALSE;
9231 si->editor.el_boulderdash = TRUE;
9232 si->editor.el_emerald_mine = TRUE;
9233 si->editor.el_emerald_mine_club = TRUE;
9234 si->editor.el_more = TRUE;
9235 si->editor.el_sokoban = TRUE;
9236 si->editor.el_supaplex = TRUE;
9237 si->editor.el_diamond_caves = TRUE;
9238 si->editor.el_dx_boulderdash = TRUE;
9239 si->editor.el_chars = TRUE;
9240 si->editor.el_steel_chars = TRUE;
9241 si->editor.el_custom = TRUE;
9243 si->editor.el_headlines = TRUE;
9244 si->editor.el_user_defined = FALSE;
9245 si->editor.el_dynamic = TRUE;
9247 si->editor.show_element_token = FALSE;
9249 si->shortcut.save_game = DEFAULT_KEY_SAVE_GAME;
9250 si->shortcut.load_game = DEFAULT_KEY_LOAD_GAME;
9251 si->shortcut.toggle_pause = DEFAULT_KEY_TOGGLE_PAUSE;
9253 si->shortcut.focus_player[0] = DEFAULT_KEY_FOCUS_PLAYER_1;
9254 si->shortcut.focus_player[1] = DEFAULT_KEY_FOCUS_PLAYER_2;
9255 si->shortcut.focus_player[2] = DEFAULT_KEY_FOCUS_PLAYER_3;
9256 si->shortcut.focus_player[3] = DEFAULT_KEY_FOCUS_PLAYER_4;
9257 si->shortcut.focus_player_all = DEFAULT_KEY_FOCUS_PLAYER_ALL;
9259 si->shortcut.tape_eject = DEFAULT_KEY_TAPE_EJECT;
9260 si->shortcut.tape_stop = DEFAULT_KEY_TAPE_STOP;
9261 si->shortcut.tape_pause = DEFAULT_KEY_TAPE_PAUSE;
9262 si->shortcut.tape_record = DEFAULT_KEY_TAPE_RECORD;
9263 si->shortcut.tape_play = DEFAULT_KEY_TAPE_PLAY;
9265 si->shortcut.sound_simple = DEFAULT_KEY_SOUND_SIMPLE;
9266 si->shortcut.sound_loops = DEFAULT_KEY_SOUND_LOOPS;
9267 si->shortcut.sound_music = DEFAULT_KEY_SOUND_MUSIC;
9269 for (i = 0; i < MAX_PLAYERS; i++)
9271 si->input[i].use_joystick = FALSE;
9272 si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i));
9273 si->input[i].joy.xleft = JOYSTICK_XLEFT;
9274 si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE;
9275 si->input[i].joy.xright = JOYSTICK_XRIGHT;
9276 si->input[i].joy.yupper = JOYSTICK_YUPPER;
9277 si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE;
9278 si->input[i].joy.ylower = JOYSTICK_YLOWER;
9279 si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0);
9280 si->input[i].joy.drop = (i == 0 ? JOY_BUTTON_2 : 0);
9281 si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED);
9282 si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED);
9283 si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED);
9284 si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED);
9285 si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED);
9286 si->input[i].key.drop = (i == 0 ? DEFAULT_KEY_DROP : KSYM_UNDEFINED);
9289 si->system.sdl_videodriver = getStringCopy(ARG_DEFAULT);
9290 si->system.sdl_audiodriver = getStringCopy(ARG_DEFAULT);
9291 si->system.audio_fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
9293 si->options.verbose = FALSE;
9295 #if defined(CREATE_SPECIAL_EDITION_RND_JUE)
9296 si->handicap = FALSE;
9297 si->fullscreen = TRUE;
9298 si->override_level_graphics = AUTO;
9299 si->override_level_sounds = AUTO;
9300 si->override_level_music = AUTO;
9304 static void setSetupInfoToDefaults_EditorCascade(struct SetupInfo *si)
9306 si->editor_cascade.el_bd = TRUE;
9307 si->editor_cascade.el_em = TRUE;
9308 si->editor_cascade.el_emc = TRUE;
9309 si->editor_cascade.el_rnd = TRUE;
9310 si->editor_cascade.el_sb = TRUE;
9311 si->editor_cascade.el_sp = TRUE;
9312 si->editor_cascade.el_dc = TRUE;
9313 si->editor_cascade.el_dx = TRUE;
9315 si->editor_cascade.el_chars = FALSE;
9316 si->editor_cascade.el_steel_chars = FALSE;
9317 si->editor_cascade.el_ce = FALSE;
9318 si->editor_cascade.el_ge = FALSE;
9319 si->editor_cascade.el_ref = FALSE;
9320 si->editor_cascade.el_user = FALSE;
9321 si->editor_cascade.el_dynamic = FALSE;
9324 static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
9328 if (!setup_file_hash)
9333 for (i = 0; i < NUM_GLOBAL_SETUP_TOKENS; i++)
9334 setSetupInfo(global_setup_tokens, i,
9335 getHashEntry(setup_file_hash, global_setup_tokens[i].text));
9340 for (i = 0; i < NUM_EDITOR_SETUP_TOKENS; i++)
9341 setSetupInfo(editor_setup_tokens, i,
9342 getHashEntry(setup_file_hash,editor_setup_tokens[i].text));
9345 /* shortcut setup */
9346 ssi = setup.shortcut;
9347 for (i = 0; i < NUM_SHORTCUT_SETUP_TOKENS; i++)
9348 setSetupInfo(shortcut_setup_tokens, i,
9349 getHashEntry(setup_file_hash,shortcut_setup_tokens[i].text));
9350 setup.shortcut = ssi;
9353 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
9357 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
9359 sii = setup.input[pnr];
9360 for (i = 0; i < NUM_PLAYER_SETUP_TOKENS; i++)
9362 char full_token[100];
9364 sprintf(full_token, "%s%s", prefix, player_setup_tokens[i].text);
9365 setSetupInfo(player_setup_tokens, i,
9366 getHashEntry(setup_file_hash, full_token));
9368 setup.input[pnr] = sii;
9373 for (i = 0; i < NUM_SYSTEM_SETUP_TOKENS; i++)
9374 setSetupInfo(system_setup_tokens, i,
9375 getHashEntry(setup_file_hash, system_setup_tokens[i].text));
9379 soi = setup.options;
9380 for (i = 0; i < NUM_OPTIONS_SETUP_TOKENS; i++)
9381 setSetupInfo(options_setup_tokens, i,
9382 getHashEntry(setup_file_hash, options_setup_tokens[i].text));
9383 setup.options = soi;
9386 static void decodeSetupFileHash_EditorCascade(SetupFileHash *setup_file_hash)
9390 if (!setup_file_hash)
9393 /* editor cascade setup */
9394 seci = setup.editor_cascade;
9395 for (i = 0; i < NUM_EDITOR_CASCADE_SETUP_TOKENS; i++)
9396 setSetupInfo(editor_cascade_setup_tokens, i,
9397 getHashEntry(setup_file_hash,
9398 editor_cascade_setup_tokens[i].text));
9399 setup.editor_cascade = seci;
9404 char *filename = getSetupFilename();
9405 SetupFileHash *setup_file_hash = NULL;
9407 /* always start with reliable default values */
9408 setSetupInfoToDefaults(&setup);
9410 setup_file_hash = loadSetupFileHash(filename);
9412 if (setup_file_hash)
9414 char *player_name_new;
9416 checkSetupFileHashIdentifier(setup_file_hash, filename,getCookie("SETUP"));
9417 decodeSetupFileHash(setup_file_hash);
9419 freeSetupFileHash(setup_file_hash);
9421 /* needed to work around problems with fixed length strings */
9422 player_name_new = get_corrected_login_name(setup.player_name);
9423 free(setup.player_name);
9424 setup.player_name = player_name_new;
9426 /* "scroll_delay: on(3) / off(0)" was replaced by scroll delay value */
9427 if (setup.scroll_delay == FALSE)
9429 setup.scroll_delay_value = MIN_SCROLL_DELAY;
9430 setup.scroll_delay = TRUE; /* now always "on" */
9433 /* make sure that scroll delay value stays inside valid range */
9434 setup.scroll_delay_value =
9435 MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
9438 Error(ERR_WARN, "using default setup values");
9441 void LoadSetup_EditorCascade()
9443 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
9444 SetupFileHash *setup_file_hash = NULL;
9446 /* always start with reliable default values */
9447 setSetupInfoToDefaults_EditorCascade(&setup);
9449 setup_file_hash = loadSetupFileHash(filename);
9451 if (setup_file_hash)
9453 checkSetupFileHashIdentifier(setup_file_hash, filename,getCookie("SETUP"));
9454 decodeSetupFileHash_EditorCascade(setup_file_hash);
9456 freeSetupFileHash(setup_file_hash);
9464 char *filename = getSetupFilename();
9468 InitUserDataDirectory();
9470 if (!(file = fopen(filename, MODE_WRITE)))
9472 Error(ERR_WARN, "cannot write setup file '%s'", filename);
9476 fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
9477 getCookie("SETUP")));
9478 fprintf(file, "\n");
9482 for (i = 0; i < NUM_GLOBAL_SETUP_TOKENS; i++)
9484 /* just to make things nicer :) */
9485 if (i == SETUP_TOKEN_PLAYER_NAME + 1 ||
9486 i == SETUP_TOKEN_GRAPHICS_SET)
9487 fprintf(file, "\n");
9489 fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i));
9494 fprintf(file, "\n");
9495 for (i = 0; i < NUM_EDITOR_SETUP_TOKENS; i++)
9496 fprintf(file, "%s\n", getSetupLine(editor_setup_tokens, "", i));
9498 /* shortcut setup */
9499 ssi = setup.shortcut;
9500 fprintf(file, "\n");
9501 for (i = 0; i < NUM_SHORTCUT_SETUP_TOKENS; i++)
9502 fprintf(file, "%s\n", getSetupLine(shortcut_setup_tokens, "", i));
9505 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
9509 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
9510 fprintf(file, "\n");
9512 sii = setup.input[pnr];
9513 for (i = 0; i < NUM_PLAYER_SETUP_TOKENS; i++)
9514 fprintf(file, "%s\n", getSetupLine(player_setup_tokens, prefix, i));
9519 fprintf(file, "\n");
9520 for (i = 0; i < NUM_SYSTEM_SETUP_TOKENS; i++)
9521 fprintf(file, "%s\n", getSetupLine(system_setup_tokens, "", i));
9524 soi = setup.options;
9525 fprintf(file, "\n");
9526 for (i = 0; i < NUM_OPTIONS_SETUP_TOKENS; i++)
9527 fprintf(file, "%s\n", getSetupLine(options_setup_tokens, "", i));
9531 SetFilePermissions(filename, PERMS_PRIVATE);
9534 void SaveSetup_EditorCascade()
9536 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
9540 InitUserDataDirectory();
9542 if (!(file = fopen(filename, MODE_WRITE)))
9544 Error(ERR_WARN, "cannot write editor cascade state file '%s'", filename);
9549 fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
9550 getCookie("SETUP")));
9551 fprintf(file, "\n");
9553 seci = setup.editor_cascade;
9554 fprintf(file, "\n");
9555 for (i = 0; i < NUM_EDITOR_CASCADE_SETUP_TOKENS; i++)
9556 fprintf(file, "%s\n", getSetupLine(editor_cascade_setup_tokens, "", i));
9560 SetFilePermissions(filename, PERMS_PRIVATE);
9565 void LoadCustomElementDescriptions()
9567 char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
9568 SetupFileHash *setup_file_hash;
9571 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9573 if (element_info[i].custom_description != NULL)
9575 free(element_info[i].custom_description);
9576 element_info[i].custom_description = NULL;
9580 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
9583 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9585 char *token = getStringCat2(element_info[i].token_name, ".name");
9586 char *value = getHashEntry(setup_file_hash, token);
9589 element_info[i].custom_description = getStringCopy(value);
9594 freeSetupFileHash(setup_file_hash);
9597 static int getElementFromToken(char *token)
9600 char *value = getHashEntry(element_token_hash, token);
9607 /* !!! OPTIMIZE THIS BY USING HASH !!! */
9608 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
9609 if (strEqual(token, element_info[i].token_name))
9613 Error(ERR_WARN, "unknown element token '%s'", token);
9615 return EL_UNDEFINED;
9618 static int get_token_parameter_value(char *token, char *value_raw)
9622 if (token == NULL || value_raw == NULL)
9623 return ARG_UNDEFINED_VALUE;
9625 suffix = strrchr(token, '.');
9630 if (strEqual(suffix, ".element"))
9631 return getElementFromToken(value_raw);
9635 if (strncmp(suffix, ".font", 5) == 0)
9639 /* !!! OPTIMIZE THIS BY USING HASH !!! */
9640 for (i = 0; i < NUM_FONTS; i++)
9641 if (strEqual(value_raw, font_info[i].token_name))
9644 /* if font not found, use reliable default value */
9645 return FONT_INITIAL_1;
9649 /* !!! USE CORRECT VALUE TYPE (currently works also for TYPE_BOOLEAN) !!! */
9650 return get_parameter_value(value_raw, suffix, TYPE_INTEGER);
9653 void InitMenuDesignSettings_Static()
9656 static SetupFileHash *image_config_hash = NULL;
9661 if (image_config_hash == NULL)
9663 image_config_hash = newSetupFileHash();
9665 for (i = 0; image_config[i].token != NULL; i++)
9666 setHashEntry(image_config_hash,
9667 image_config[i].token,
9668 image_config[i].value);
9673 /* always start with reliable default values from static default config */
9674 for (i = 0; image_config_vars[i].token != NULL; i++)
9676 char *value = getHashEntry(image_config_hash, image_config_vars[i].token);
9679 *image_config_vars[i].value =
9680 get_token_parameter_value(image_config_vars[i].token, value);
9687 /* always start with reliable default values from static default config */
9688 for (i = 0; image_config_vars[i].token != NULL; i++)
9689 for (j = 0; image_config[j].token != NULL; j++)
9690 if (strEqual(image_config_vars[i].token, image_config[j].token))
9691 *image_config_vars[i].value =
9692 get_token_parameter_value(image_config_vars[i].token,
9693 image_config[j].value);
9697 static void InitMenuDesignSettings_SpecialPreProcessing()
9701 /* the following initializes hierarchical values from static configuration */
9703 /* special case: initialize "ARG_DEFAULT" values in static default config */
9704 /* (e.g., initialize "[titlemessage].fade_mode" from "[title].fade_mode") */
9705 titlemessage_initial_default.fade_mode = title_initial_default.fade_mode;
9706 titlemessage_initial_default.fade_delay = title_initial_default.fade_delay;
9707 titlemessage_initial_default.post_delay = title_initial_default.post_delay;
9708 titlemessage_initial_default.auto_delay = title_initial_default.auto_delay;
9709 titlemessage_default.fade_mode = title_default.fade_mode;
9710 titlemessage_default.fade_delay = title_default.fade_delay;
9711 titlemessage_default.post_delay = title_default.post_delay;
9712 titlemessage_default.auto_delay = title_default.auto_delay;
9714 /* special case: initialize "ARG_DEFAULT" values in static default config */
9715 /* (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode") */
9716 for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
9718 titlemessage_initial[i] = titlemessage_initial_default;
9719 titlemessage[i] = titlemessage_default;
9722 /* special case: initialize "ARG_DEFAULT" values in static default config */
9723 /* (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz") */
9724 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
9726 menu.enter_screen[i] = menu.enter_screen[GFX_SPECIAL_ARG_DEFAULT];
9727 menu.leave_screen[i] = menu.leave_screen[GFX_SPECIAL_ARG_DEFAULT];
9731 static void InitMenuDesignSettings_SpecialPostProcessing()
9733 /* special case: initialize later added SETUP list size from LEVELS value */
9734 if (menu.list_size[GAME_MODE_SETUP] == -1)
9735 menu.list_size[GAME_MODE_SETUP] = menu.list_size[GAME_MODE_LEVELS];
9738 static void LoadMenuDesignSettingsFromFilename(char *filename)
9740 static struct TitleMessageInfo tmi;
9741 static struct TokenInfo titlemessage_tokens[] =
9743 { TYPE_INTEGER, &tmi.x, ".x" },
9744 { TYPE_INTEGER, &tmi.y, ".y" },
9745 { TYPE_INTEGER, &tmi.width, ".width" },
9746 { TYPE_INTEGER, &tmi.height, ".height" },
9747 { TYPE_INTEGER, &tmi.chars, ".chars" },
9748 { TYPE_INTEGER, &tmi.lines, ".lines" },
9749 { TYPE_INTEGER, &tmi.align, ".align" },
9750 { TYPE_INTEGER, &tmi.valign, ".valign" },
9751 { TYPE_INTEGER, &tmi.font, ".font" },
9752 { TYPE_BOOLEAN, &tmi.autowrap, ".autowrap" },
9753 { TYPE_BOOLEAN, &tmi.centered, ".centered" },
9754 { TYPE_BOOLEAN, &tmi.parse_comments, ".parse_comments" },
9755 { TYPE_INTEGER, &tmi.sort_priority, ".sort_priority" },
9756 { TYPE_INTEGER, &tmi.fade_mode, ".fade_mode" },
9757 { TYPE_INTEGER, &tmi.fade_delay, ".fade_delay" },
9758 { TYPE_INTEGER, &tmi.post_delay, ".post_delay" },
9759 { TYPE_INTEGER, &tmi.auto_delay, ".auto_delay" },
9765 struct TitleMessageInfo *array;
9768 titlemessage_arrays[] =
9770 { titlemessage_initial, "[titlemessage_initial]" },
9771 { titlemessage, "[titlemessage]" },
9775 SetupFileHash *setup_file_hash;
9779 printf("LoadMenuDesignSettings from file '%s' ...\n", filename);
9782 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
9785 /* the following initializes hierarchical values from dynamic configuration */
9787 /* special case: initialize with default values that may be overwritten */
9788 /* (e.g., init "menu.draw_xoffset.INFO" from "menu.draw_xoffset") */
9789 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
9791 char *value_1 = getHashEntry(setup_file_hash, "menu.draw_xoffset");
9792 char *value_2 = getHashEntry(setup_file_hash, "menu.draw_yoffset");
9793 char *value_3 = getHashEntry(setup_file_hash, "menu.list_size");
9795 if (value_1 != NULL)
9796 menu.draw_xoffset[i] = get_integer_from_string(value_1);
9797 if (value_2 != NULL)
9798 menu.draw_yoffset[i] = get_integer_from_string(value_2);
9799 if (value_3 != NULL)
9800 menu.list_size[i] = get_integer_from_string(value_3);
9803 /* special case: initialize with default values that may be overwritten */
9804 /* (eg, init "menu.draw_xoffset.INFO[XXX]" from "menu.draw_xoffset.INFO") */
9805 for (i = 0; i < NUM_SPECIAL_GFX_INFO_ARGS; i++)
9807 char *value_1 = getHashEntry(setup_file_hash, "menu.draw_xoffset.INFO");
9808 char *value_2 = getHashEntry(setup_file_hash, "menu.draw_yoffset.INFO");
9810 if (value_1 != NULL)
9811 menu.draw_xoffset_info[i] = get_integer_from_string(value_1);
9812 if (value_2 != NULL)
9813 menu.draw_yoffset_info[i] = get_integer_from_string(value_2);
9816 /* special case: initialize with default values that may be overwritten */
9817 /* (eg, init "menu.draw_xoffset.SETUP[XXX]" from "menu.draw_xoffset.SETUP") */
9818 for (i = 0; i < NUM_SPECIAL_GFX_SETUP_ARGS; i++)
9820 char *value_1 = getHashEntry(setup_file_hash, "menu.draw_xoffset.SETUP");
9821 char *value_2 = getHashEntry(setup_file_hash, "menu.draw_yoffset.SETUP");
9823 if (value_1 != NULL)
9824 menu.draw_xoffset_setup[i] = get_integer_from_string(value_1);
9825 if (value_2 != NULL)
9826 menu.draw_yoffset_setup[i] = get_integer_from_string(value_2);
9829 /* special case: initialize with default values that may be overwritten */
9830 /* (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz") */
9831 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
9833 char *token_1 = "menu.enter_screen.fade_mode";
9834 char *token_2 = "menu.enter_screen.fade_delay";
9835 char *token_3 = "menu.enter_screen.post_delay";
9836 char *token_4 = "menu.leave_screen.fade_mode";
9837 char *token_5 = "menu.leave_screen.fade_delay";
9838 char *token_6 = "menu.leave_screen.post_delay";
9839 char *value_1 = getHashEntry(setup_file_hash, token_1);
9840 char *value_2 = getHashEntry(setup_file_hash, token_2);
9841 char *value_3 = getHashEntry(setup_file_hash, token_3);
9842 char *value_4 = getHashEntry(setup_file_hash, token_4);
9843 char *value_5 = getHashEntry(setup_file_hash, token_5);
9844 char *value_6 = getHashEntry(setup_file_hash, token_6);
9846 if (value_1 != NULL)
9847 menu.enter_screen[i].fade_mode = get_token_parameter_value(token_1,
9849 if (value_2 != NULL)
9850 menu.enter_screen[i].fade_delay = get_token_parameter_value(token_2,
9852 if (value_3 != NULL)
9853 menu.enter_screen[i].post_delay = get_token_parameter_value(token_3,
9855 if (value_4 != NULL)
9856 menu.leave_screen[i].fade_mode = get_token_parameter_value(token_4,
9858 if (value_5 != NULL)
9859 menu.leave_screen[i].fade_delay = get_token_parameter_value(token_5,
9861 if (value_6 != NULL)
9862 menu.leave_screen[i].post_delay = get_token_parameter_value(token_6,
9866 /* special case: initialize with default values that may be overwritten */
9867 /* (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode") */
9868 for (i = 0; titlemessage_arrays[i].array != NULL; i++)
9870 struct TitleMessageInfo *array = titlemessage_arrays[i].array;
9871 char *base_token = titlemessage_arrays[i].text;
9873 for (j = 0; titlemessage_tokens[j].type != -1; j++)
9875 char *token = getStringCat2(base_token, titlemessage_tokens[j].text);
9876 char *value = getHashEntry(setup_file_hash, token);
9880 int parameter_value = get_token_parameter_value(token, value);
9882 for (k = 0; k < MAX_NUM_TITLE_MESSAGES; k++)
9886 if (titlemessage_tokens[j].type == TYPE_INTEGER)
9887 *(boolean *)titlemessage_tokens[j].value = (boolean)parameter_value;
9889 *(int *)titlemessage_tokens[j].value = (int)parameter_value;
9899 /* read (and overwrite with) values that may be specified in config file */
9900 for (i = 0; image_config_vars[i].token != NULL; i++)
9902 char *value = getHashEntry(setup_file_hash, image_config_vars[i].token);
9904 /* (ignore definitions set to "[DEFAULT]" which are already initialized) */
9905 if (value != NULL && !strEqual(value, ARG_DEFAULT))
9906 *image_config_vars[i].value =
9907 get_token_parameter_value(image_config_vars[i].token, value);
9910 freeSetupFileHash(setup_file_hash);
9913 void LoadMenuDesignSettings()
9915 char *filename_base = UNDEFINED_FILENAME, *filename_local;
9917 InitMenuDesignSettings_Static();
9918 InitMenuDesignSettings_SpecialPreProcessing();
9921 if (!GFX_OVERRIDE_ARTWORK(ARTWORK_TYPE_GRAPHICS))
9923 if (!SETUP_OVERRIDE_ARTWORK(setup, ARTWORK_TYPE_GRAPHICS))
9926 /* first look for special settings configured in level series config */
9927 filename_base = getCustomArtworkLevelConfigFilename(ARTWORK_TYPE_GRAPHICS);
9929 if (fileExists(filename_base))
9930 LoadMenuDesignSettingsFromFilename(filename_base);
9933 filename_local = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
9935 if (filename_local != NULL && !strEqual(filename_base, filename_local))
9936 LoadMenuDesignSettingsFromFilename(filename_local);
9938 InitMenuDesignSettings_SpecialPostProcessing();
9941 void LoadUserDefinedEditorElementList(int **elements, int *num_elements)
9943 char *filename = getEditorSetupFilename();
9944 SetupFileList *setup_file_list, *list;
9945 SetupFileHash *element_hash;
9946 int num_unknown_tokens = 0;
9949 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
9952 element_hash = newSetupFileHash();
9954 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9955 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
9957 /* determined size may be larger than needed (due to unknown elements) */
9959 for (list = setup_file_list; list != NULL; list = list->next)
9962 /* add space for up to 3 more elements for padding that may be needed */
9965 /* free memory for old list of elements, if needed */
9966 checked_free(*elements);
9968 /* allocate memory for new list of elements */
9969 *elements = checked_malloc(*num_elements * sizeof(int));
9972 for (list = setup_file_list; list != NULL; list = list->next)
9974 char *value = getHashEntry(element_hash, list->token);
9976 if (value == NULL) /* try to find obsolete token mapping */
9978 char *mapped_token = get_mapped_token(list->token);
9980 if (mapped_token != NULL)
9982 value = getHashEntry(element_hash, mapped_token);
9990 (*elements)[(*num_elements)++] = atoi(value);
9994 if (num_unknown_tokens == 0)
9996 Error(ERR_INFO_LINE, "-");
9997 Error(ERR_INFO, "warning: unknown token(s) found in config file:");
9998 Error(ERR_INFO, "- config file: '%s'", filename);
10000 num_unknown_tokens++;
10003 Error(ERR_INFO, "- token: '%s'", list->token);
10007 if (num_unknown_tokens > 0)
10008 Error(ERR_INFO_LINE, "-");
10010 while (*num_elements % 4) /* pad with empty elements, if needed */
10011 (*elements)[(*num_elements)++] = EL_EMPTY;
10013 freeSetupFileList(setup_file_list);
10014 freeSetupFileHash(element_hash);
10017 for (i = 0; i < *num_elements; i++)
10018 printf("editor: element '%s' [%d]\n",
10019 element_info[(*elements)[i]].token_name, (*elements)[i]);
10023 static struct MusicFileInfo *get_music_file_info_ext(char *basename, int music,
10026 SetupFileHash *setup_file_hash = NULL;
10027 struct MusicFileInfo tmp_music_file_info, *new_music_file_info;
10028 char *filename_music, *filename_prefix, *filename_info;
10034 token_to_value_ptr[] =
10036 { "title_header", &tmp_music_file_info.title_header },
10037 { "artist_header", &tmp_music_file_info.artist_header },
10038 { "album_header", &tmp_music_file_info.album_header },
10039 { "year_header", &tmp_music_file_info.year_header },
10041 { "title", &tmp_music_file_info.title },
10042 { "artist", &tmp_music_file_info.artist },
10043 { "album", &tmp_music_file_info.album },
10044 { "year", &tmp_music_file_info.year },
10050 filename_music = (is_sound ? getCustomSoundFilename(basename) :
10051 getCustomMusicFilename(basename));
10053 if (filename_music == NULL)
10056 /* ---------- try to replace file extension ---------- */
10058 filename_prefix = getStringCopy(filename_music);
10059 if (strrchr(filename_prefix, '.') != NULL)
10060 *strrchr(filename_prefix, '.') = '\0';
10061 filename_info = getStringCat2(filename_prefix, ".txt");
10064 printf("trying to load file '%s'...\n", filename_info);
10067 if (fileExists(filename_info))
10068 setup_file_hash = loadSetupFileHash(filename_info);
10070 free(filename_prefix);
10071 free(filename_info);
10073 if (setup_file_hash == NULL)
10075 /* ---------- try to add file extension ---------- */
10077 filename_prefix = getStringCopy(filename_music);
10078 filename_info = getStringCat2(filename_prefix, ".txt");
10081 printf("trying to load file '%s'...\n", filename_info);
10084 if (fileExists(filename_info))
10085 setup_file_hash = loadSetupFileHash(filename_info);
10087 free(filename_prefix);
10088 free(filename_info);
10091 if (setup_file_hash == NULL)
10094 /* ---------- music file info found ---------- */
10096 clear_mem(&tmp_music_file_info, sizeof(struct MusicFileInfo));
10098 for (i = 0; token_to_value_ptr[i].token != NULL; i++)
10100 char *value = getHashEntry(setup_file_hash, token_to_value_ptr[i].token);
10102 *token_to_value_ptr[i].value_ptr =
10103 getStringCopy(value != NULL && *value != '\0' ? value : UNKNOWN_NAME);
10106 tmp_music_file_info.basename = getStringCopy(basename);
10107 tmp_music_file_info.music = music;
10108 tmp_music_file_info.is_sound = is_sound;
10110 new_music_file_info = checked_malloc(sizeof(struct MusicFileInfo));
10111 *new_music_file_info = tmp_music_file_info;
10113 return new_music_file_info;
10116 static struct MusicFileInfo *get_music_file_info(char *basename, int music)
10118 return get_music_file_info_ext(basename, music, FALSE);
10121 static struct MusicFileInfo *get_sound_file_info(char *basename, int sound)
10123 return get_music_file_info_ext(basename, sound, TRUE);
10126 static boolean music_info_listed_ext(struct MusicFileInfo *list,
10127 char *basename, boolean is_sound)
10129 for (; list != NULL; list = list->next)
10130 if (list->is_sound == is_sound && strEqual(list->basename, basename))
10136 static boolean music_info_listed(struct MusicFileInfo *list, char *basename)
10138 return music_info_listed_ext(list, basename, FALSE);
10141 static boolean sound_info_listed(struct MusicFileInfo *list, char *basename)
10143 return music_info_listed_ext(list, basename, TRUE);
10146 void LoadMusicInfo()
10148 char *music_directory = getCustomMusicDirectory();
10149 int num_music = getMusicListSize();
10150 int num_music_noconf = 0;
10151 int num_sounds = getSoundListSize();
10153 struct dirent *dir_entry;
10154 struct FileInfo *music, *sound;
10155 struct MusicFileInfo *next, **new;
10158 while (music_file_info != NULL)
10160 next = music_file_info->next;
10162 checked_free(music_file_info->basename);
10164 checked_free(music_file_info->title_header);
10165 checked_free(music_file_info->artist_header);
10166 checked_free(music_file_info->album_header);
10167 checked_free(music_file_info->year_header);
10169 checked_free(music_file_info->title);
10170 checked_free(music_file_info->artist);
10171 checked_free(music_file_info->album);
10172 checked_free(music_file_info->year);
10174 free(music_file_info);
10176 music_file_info = next;
10179 new = &music_file_info;
10181 for (i = 0; i < num_music; i++)
10183 music = getMusicListEntry(i);
10185 if (music->filename == NULL)
10188 if (strEqual(music->filename, UNDEFINED_FILENAME))
10191 /* a configured file may be not recognized as music */
10192 if (!FileIsMusic(music->filename))
10196 printf("::: -> '%s' (configured)\n", music->filename);
10199 if (!music_info_listed(music_file_info, music->filename))
10201 *new = get_music_file_info(music->filename, i);
10203 new = &(*new)->next;
10207 if ((dir = opendir(music_directory)) == NULL)
10209 Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
10213 while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */
10215 char *basename = dir_entry->d_name;
10216 boolean music_already_used = FALSE;
10219 /* skip all music files that are configured in music config file */
10220 for (i = 0; i < num_music; i++)
10222 music = getMusicListEntry(i);
10224 if (music->filename == NULL)
10227 if (strEqual(basename, music->filename))
10229 music_already_used = TRUE;
10234 if (music_already_used)
10237 if (!FileIsMusic(basename))
10241 printf("::: -> '%s' (found in directory)\n", basename);
10244 if (!music_info_listed(music_file_info, basename))
10246 *new = get_music_file_info(basename, MAP_NOCONF_MUSIC(num_music_noconf));
10248 new = &(*new)->next;
10251 num_music_noconf++;
10256 for (i = 0; i < num_sounds; i++)
10258 sound = getSoundListEntry(i);
10260 if (sound->filename == NULL)
10263 if (strEqual(sound->filename, UNDEFINED_FILENAME))
10266 /* a configured file may be not recognized as sound */
10267 if (!FileIsSound(sound->filename))
10271 printf("::: -> '%s' (configured)\n", sound->filename);
10274 if (!sound_info_listed(music_file_info, sound->filename))
10276 *new = get_sound_file_info(sound->filename, i);
10278 new = &(*new)->next;
10283 for (next = music_file_info; next != NULL; next = next->next)
10284 printf("::: title == '%s'\n", next->title);
10288 void add_helpanim_entry(int element, int action, int direction, int delay,
10289 int *num_list_entries)
10291 struct HelpAnimInfo *new_list_entry;
10292 (*num_list_entries)++;
10295 checked_realloc(helpanim_info,
10296 *num_list_entries * sizeof(struct HelpAnimInfo));
10297 new_list_entry = &helpanim_info[*num_list_entries - 1];
10299 new_list_entry->element = element;
10300 new_list_entry->action = action;
10301 new_list_entry->direction = direction;
10302 new_list_entry->delay = delay;
10305 void print_unknown_token(char *filename, char *token, int token_nr)
10309 Error(ERR_INFO_LINE, "-");
10310 Error(ERR_INFO, "warning: unknown token(s) found in config file:");
10311 Error(ERR_INFO, "- config file: '%s'", filename);
10314 Error(ERR_INFO, "- token: '%s'", token);
10317 void print_unknown_token_end(int token_nr)
10320 Error(ERR_INFO_LINE, "-");
10323 void LoadHelpAnimInfo()
10325 char *filename = getHelpAnimFilename();
10326 SetupFileList *setup_file_list = NULL, *list;
10327 SetupFileHash *element_hash, *action_hash, *direction_hash;
10328 int num_list_entries = 0;
10329 int num_unknown_tokens = 0;
10332 if (fileExists(filename))
10333 setup_file_list = loadSetupFileList(filename);
10335 if (setup_file_list == NULL)
10337 /* use reliable default values from static configuration */
10338 SetupFileList *insert_ptr;
10340 insert_ptr = setup_file_list =
10341 newSetupFileList(helpanim_config[0].token,
10342 helpanim_config[0].value);
10344 for (i = 1; helpanim_config[i].token; i++)
10345 insert_ptr = addListEntry(insert_ptr,
10346 helpanim_config[i].token,
10347 helpanim_config[i].value);
10350 element_hash = newSetupFileHash();
10351 action_hash = newSetupFileHash();
10352 direction_hash = newSetupFileHash();
10354 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
10355 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
10357 for (i = 0; i < NUM_ACTIONS; i++)
10358 setHashEntry(action_hash, element_action_info[i].suffix,
10359 i_to_a(element_action_info[i].value));
10361 /* do not store direction index (bit) here, but direction value! */
10362 for (i = 0; i < NUM_DIRECTIONS_FULL; i++)
10363 setHashEntry(direction_hash, element_direction_info[i].suffix,
10364 i_to_a(1 << element_direction_info[i].value));
10366 for (list = setup_file_list; list != NULL; list = list->next)
10368 char *element_token, *action_token, *direction_token;
10369 char *element_value, *action_value, *direction_value;
10370 int delay = atoi(list->value);
10372 if (strEqual(list->token, "end"))
10374 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
10379 /* first try to break element into element/action/direction parts;
10380 if this does not work, also accept combined "element[.act][.dir]"
10381 elements (like "dynamite.active"), which are unique elements */
10383 if (strchr(list->token, '.') == NULL) /* token contains no '.' */
10385 element_value = getHashEntry(element_hash, list->token);
10386 if (element_value != NULL) /* element found */
10387 add_helpanim_entry(atoi(element_value), -1, -1, delay,
10388 &num_list_entries);
10391 /* no further suffixes found -- this is not an element */
10392 print_unknown_token(filename, list->token, num_unknown_tokens++);
10398 /* token has format "<prefix>.<something>" */
10400 action_token = strchr(list->token, '.'); /* suffix may be action ... */
10401 direction_token = action_token; /* ... or direction */
10403 element_token = getStringCopy(list->token);
10404 *strchr(element_token, '.') = '\0';
10406 element_value = getHashEntry(element_hash, element_token);
10408 if (element_value == NULL) /* this is no element */
10410 element_value = getHashEntry(element_hash, list->token);
10411 if (element_value != NULL) /* combined element found */
10412 add_helpanim_entry(atoi(element_value), -1, -1, delay,
10413 &num_list_entries);
10415 print_unknown_token(filename, list->token, num_unknown_tokens++);
10417 free(element_token);
10422 action_value = getHashEntry(action_hash, action_token);
10424 if (action_value != NULL) /* action found */
10426 add_helpanim_entry(atoi(element_value), atoi(action_value), -1, delay,
10427 &num_list_entries);
10429 free(element_token);
10434 direction_value = getHashEntry(direction_hash, direction_token);
10436 if (direction_value != NULL) /* direction found */
10438 add_helpanim_entry(atoi(element_value), -1, atoi(direction_value), delay,
10439 &num_list_entries);
10441 free(element_token);
10446 if (strchr(action_token + 1, '.') == NULL)
10448 /* no further suffixes found -- this is not an action nor direction */
10450 element_value = getHashEntry(element_hash, list->token);
10451 if (element_value != NULL) /* combined element found */
10452 add_helpanim_entry(atoi(element_value), -1, -1, delay,
10453 &num_list_entries);
10455 print_unknown_token(filename, list->token, num_unknown_tokens++);
10457 free(element_token);
10462 /* token has format "<prefix>.<suffix>.<something>" */
10464 direction_token = strchr(action_token + 1, '.');
10466 action_token = getStringCopy(action_token);
10467 *strchr(action_token + 1, '.') = '\0';
10469 action_value = getHashEntry(action_hash, action_token);
10471 if (action_value == NULL) /* this is no action */
10473 element_value = getHashEntry(element_hash, list->token);
10474 if (element_value != NULL) /* combined element found */
10475 add_helpanim_entry(atoi(element_value), -1, -1, delay,
10476 &num_list_entries);
10478 print_unknown_token(filename, list->token, num_unknown_tokens++);
10480 free(element_token);
10481 free(action_token);
10486 direction_value = getHashEntry(direction_hash, direction_token);
10488 if (direction_value != NULL) /* direction found */
10490 add_helpanim_entry(atoi(element_value), atoi(action_value),
10491 atoi(direction_value), delay, &num_list_entries);
10493 free(element_token);
10494 free(action_token);
10499 /* this is no direction */
10501 element_value = getHashEntry(element_hash, list->token);
10502 if (element_value != NULL) /* combined element found */
10503 add_helpanim_entry(atoi(element_value), -1, -1, delay,
10504 &num_list_entries);
10506 print_unknown_token(filename, list->token, num_unknown_tokens++);
10508 free(element_token);
10509 free(action_token);
10512 print_unknown_token_end(num_unknown_tokens);
10514 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
10515 add_helpanim_entry(HELPANIM_LIST_END, -1, -1, -1, &num_list_entries);
10517 freeSetupFileList(setup_file_list);
10518 freeSetupFileHash(element_hash);
10519 freeSetupFileHash(action_hash);
10520 freeSetupFileHash(direction_hash);
10523 for (i = 0; i < num_list_entries; i++)
10524 printf("::: '%s': %d, %d, %d => %d\n",
10525 EL_NAME(helpanim_info[i].element),
10526 helpanim_info[i].element,
10527 helpanim_info[i].action,
10528 helpanim_info[i].direction,
10529 helpanim_info[i].delay);
10533 void LoadHelpTextInfo()
10535 char *filename = getHelpTextFilename();
10538 if (helptext_info != NULL)
10540 freeSetupFileHash(helptext_info);
10541 helptext_info = NULL;
10544 if (fileExists(filename))
10545 helptext_info = loadSetupFileHash(filename);
10547 if (helptext_info == NULL)
10549 /* use reliable default values from static configuration */
10550 helptext_info = newSetupFileHash();
10552 for (i = 0; helptext_config[i].token; i++)
10553 setHashEntry(helptext_info,
10554 helptext_config[i].token,
10555 helptext_config[i].value);
10559 BEGIN_HASH_ITERATION(helptext_info, itr)
10561 printf("::: '%s' => '%s'\n",
10562 HASH_ITERATION_TOKEN(itr), HASH_ITERATION_VALUE(itr));
10564 END_HASH_ITERATION(hash, itr)
10569 /* ------------------------------------------------------------------------- */
10570 /* convert levels */
10571 /* ------------------------------------------------------------------------- */
10573 #define MAX_NUM_CONVERT_LEVELS 1000
10575 void ConvertLevels()
10577 static LevelDirTree *convert_leveldir = NULL;
10578 static int convert_level_nr = -1;
10579 static int num_levels_handled = 0;
10580 static int num_levels_converted = 0;
10581 static boolean levels_failed[MAX_NUM_CONVERT_LEVELS];
10584 convert_leveldir = getTreeInfoFromIdentifier(leveldir_first,
10585 global.convert_leveldir);
10587 if (convert_leveldir == NULL)
10588 Error(ERR_EXIT, "no such level identifier: '%s'",
10589 global.convert_leveldir);
10591 leveldir_current = convert_leveldir;
10593 if (global.convert_level_nr != -1)
10595 convert_leveldir->first_level = global.convert_level_nr;
10596 convert_leveldir->last_level = global.convert_level_nr;
10599 convert_level_nr = convert_leveldir->first_level;
10601 printf_line("=", 79);
10602 printf("Converting levels\n");
10603 printf_line("-", 79);
10604 printf("Level series identifier: '%s'\n", convert_leveldir->identifier);
10605 printf("Level series name: '%s'\n", convert_leveldir->name);
10606 printf("Level series author: '%s'\n", convert_leveldir->author);
10607 printf("Number of levels: %d\n", convert_leveldir->levels);
10608 printf_line("=", 79);
10611 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
10612 levels_failed[i] = FALSE;
10614 while (convert_level_nr <= convert_leveldir->last_level)
10616 char *level_filename;
10619 level_nr = convert_level_nr++;
10621 printf("Level %03d: ", level_nr);
10623 LoadLevel(level_nr);
10624 if (level.no_valid_file)
10626 printf("(no level)\n");
10630 printf("converting level ... ");
10632 level_filename = getDefaultLevelFilename(level_nr);
10633 new_level = !fileExists(level_filename);
10637 SaveLevel(level_nr);
10639 num_levels_converted++;
10641 printf("converted.\n");
10645 if (level_nr >= 0 && level_nr < MAX_NUM_CONVERT_LEVELS)
10646 levels_failed[level_nr] = TRUE;
10648 printf("NOT CONVERTED -- LEVEL ALREADY EXISTS.\n");
10651 num_levels_handled++;
10655 printf_line("=", 79);
10656 printf("Number of levels handled: %d\n", num_levels_handled);
10657 printf("Number of levels converted: %d (%d%%)\n", num_levels_converted,
10658 (num_levels_handled ?
10659 num_levels_converted * 100 / num_levels_handled : 0));
10660 printf_line("-", 79);
10661 printf("Summary (for automatic parsing by scripts):\n");
10662 printf("LEVELDIR '%s', CONVERTED %d/%d (%d%%)",
10663 convert_leveldir->identifier, num_levels_converted,
10664 num_levels_handled,
10665 (num_levels_handled ?
10666 num_levels_converted * 100 / num_levels_handled : 0));
10668 if (num_levels_handled != num_levels_converted)
10670 printf(", FAILED:");
10671 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
10672 if (levels_failed[i])
10673 printf(" %03d", i);
10677 printf_line("=", 79);
10679 CloseAllAndExit(0);
10683 /* ------------------------------------------------------------------------- */
10684 /* create and save images for use in level sketches (raw BMP format) */
10685 /* ------------------------------------------------------------------------- */
10687 void CreateLevelSketchImages()
10689 #if defined(TARGET_SDL)
10694 InitElementPropertiesGfxElement();
10696 bitmap1 = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
10697 bitmap2 = CreateBitmap(MINI_TILEX, MINI_TILEY, DEFAULT_DEPTH);
10699 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
10701 Bitmap *src_bitmap;
10703 int element = getMappedElement(i);
10704 int graphic = el2edimg(element);
10705 char basename1[16];
10706 char basename2[16];
10710 sprintf(basename1, "%03d.bmp", i);
10711 sprintf(basename2, "%03ds.bmp", i);
10713 filename1 = getPath2(global.create_images_dir, basename1);
10714 filename2 = getPath2(global.create_images_dir, basename2);
10716 getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
10717 BlitBitmap(src_bitmap, bitmap1, src_x, src_y, TILEX, TILEY, 0, 0);
10719 if (SDL_SaveBMP(bitmap1->surface, filename1) != 0)
10720 Error(ERR_EXIT, "cannot save level sketch image file '%s'", filename1);
10722 getMiniGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
10723 BlitBitmap(src_bitmap, bitmap2, src_x, src_y, MINI_TILEX, MINI_TILEY, 0, 0);
10725 if (SDL_SaveBMP(bitmap2->surface, filename2) != 0)
10726 Error(ERR_EXIT, "cannot save level sketch image file '%s'", filename2);
10732 printf("%03d `%03d%c", i, i, (i % 10 < 9 ? ' ' : '\n'));
10735 FreeBitmap(bitmap1);
10736 FreeBitmap(bitmap2);
10741 Error(ERR_INFO, "%d normal and small images created", NUM_FILE_ELEMENTS);
10743 CloseAllAndExit(0);
10748 /* ------------------------------------------------------------------------- */
10749 /* create and save images for custom and group elements (raw BMP format) */
10750 /* ------------------------------------------------------------------------- */
10752 void CreateCustomElementImages()
10754 #if defined(TARGET_SDL)
10755 char *filename = "graphics.classic/RocksCE.bmp";
10757 Bitmap *src_bitmap;
10758 int dummy_graphic = IMG_CUSTOM_99;
10759 int yoffset_ce = 0;
10760 int yoffset_ge = (TILEY * NUM_CUSTOM_ELEMENTS / 16);
10764 bitmap = CreateBitmap(TILEX * 16 * 2,
10765 TILEY * (NUM_CUSTOM_ELEMENTS + NUM_GROUP_ELEMENTS) / 16,
10768 getGraphicSource(dummy_graphic, 0, &src_bitmap, &src_x, &src_y);
10770 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
10777 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
10778 TILEX * x, TILEY * y + yoffset_ce);
10780 BlitBitmap(src_bitmap, bitmap, 0, TILEY, TILEX, TILEY,
10781 TILEX * x + TILEX * 16, TILEY * y + yoffset_ce);
10783 for (j = 2; j >= 0; j--)
10787 BlitBitmap(src_bitmap, bitmap, TILEX + c * 7, 0, 6, 10,
10788 TILEX * x + 6 + j * 7,
10789 TILEY * y + 11 + yoffset_ce);
10791 BlitBitmap(src_bitmap, bitmap, TILEX + c * 8, TILEY, 6, 10,
10792 TILEX * 16 + TILEX * x + 6 + j * 8,
10793 TILEY * y + 10 + yoffset_ce);
10799 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
10806 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
10807 TILEX * x, TILEY * y + yoffset_ge);
10809 BlitBitmap(src_bitmap, bitmap, 0, TILEY, TILEX, TILEY,
10810 TILEX * x + TILEX * 16, TILEY * y + yoffset_ge);
10812 for (j = 1; j >= 0; j--)
10816 BlitBitmap(src_bitmap, bitmap, TILEX + c * 10, 11, 10, 10,
10817 TILEX * x + 6 + j * 10,
10818 TILEY * y + 11 + yoffset_ge);
10820 BlitBitmap(src_bitmap, bitmap, TILEX + c * 8, TILEY + 12, 6, 10,
10821 TILEX * 16 + TILEX * x + 10 + j * 8,
10822 TILEY * y + 10 + yoffset_ge);
10828 if (SDL_SaveBMP(bitmap->surface, filename) != 0)
10829 Error(ERR_EXIT, "cannot save CE graphics file '%s'", filename);
10831 FreeBitmap(bitmap);
10833 CloseAllAndExit(0);
10838 void CreateLevelSketchImages_TEST()
10840 void CreateCustomElementImages()