1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
17 #include "libgame/libgame.h"
26 #define ENABLE_UNUSED_CODE 0 // currently unused functions
27 #define ENABLE_HISTORIC_CHUNKS 0 // only for historic reference
28 #define ENABLE_RESERVED_CODE 0 // reserved for later use
30 #define CHUNK_ID_LEN 4 // IFF style chunk id length
31 #define CHUNK_SIZE_UNDEFINED 0 // undefined chunk size == 0
32 #define CHUNK_SIZE_NONE -1 // do not write chunk size
34 #define LEVEL_CHUNK_NAME_SIZE MAX_LEVEL_NAME_LEN
35 #define LEVEL_CHUNK_AUTH_SIZE MAX_LEVEL_AUTHOR_LEN
37 #define LEVEL_CHUNK_VERS_SIZE 8 // size of file version chunk
38 #define LEVEL_CHUNK_DATE_SIZE 4 // size of file date chunk
39 #define LEVEL_CHUNK_HEAD_SIZE 80 // size of level file header
40 #define LEVEL_CHUNK_HEAD_UNUSED 0 // unused level header bytes
41 #define LEVEL_CHUNK_CNT2_SIZE 160 // size of level CNT2 chunk
42 #define LEVEL_CHUNK_CNT2_UNUSED 11 // unused CNT2 chunk bytes
43 #define LEVEL_CHUNK_CNT3_HEADER 16 // size of level CNT3 header
44 #define LEVEL_CHUNK_CNT3_UNUSED 10 // unused CNT3 chunk bytes
45 #define LEVEL_CPART_CUS3_SIZE 134 // size of CUS3 chunk part
46 #define LEVEL_CPART_CUS3_UNUSED 15 // unused CUS3 bytes / part
47 #define LEVEL_CHUNK_GRP1_SIZE 74 // size of level GRP1 chunk
49 // (element number, number of change pages, change page number)
50 #define LEVEL_CHUNK_CUSX_UNCHANGED (2 + (1 + 1) + (1 + 1))
52 // (element number only)
53 #define LEVEL_CHUNK_GRPX_UNCHANGED 2
54 #define LEVEL_CHUNK_NOTE_UNCHANGED 2
56 // (nothing at all if unchanged)
57 #define LEVEL_CHUNK_ELEM_UNCHANGED 0
59 #define TAPE_CHUNK_VERS_SIZE 8 // size of file version chunk
60 #define TAPE_CHUNK_HEAD_SIZE 20 // size of tape file header
61 #define TAPE_CHUNK_HEAD_UNUSED 2 // unused tape header bytes
63 #define LEVEL_CHUNK_CNT3_SIZE(x) (LEVEL_CHUNK_CNT3_HEADER + (x))
64 #define LEVEL_CHUNK_CUS3_SIZE(x) (2 + (x) * LEVEL_CPART_CUS3_SIZE)
65 #define LEVEL_CHUNK_CUS4_SIZE(x) (96 + (x) * 48)
67 // file identifier strings
68 #define LEVEL_COOKIE_TMPL "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_x.x"
69 #define TAPE_COOKIE_TMPL "ROCKSNDIAMONDS_TAPE_FILE_VERSION_x.x"
70 #define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2"
72 // values for deciding when (not) to save configuration data
73 #define SAVE_CONF_NEVER 0
74 #define SAVE_CONF_ALWAYS 1
75 #define SAVE_CONF_WHEN_CHANGED -1
77 // values for chunks using micro chunks
78 #define CONF_MASK_1_BYTE 0x00
79 #define CONF_MASK_2_BYTE 0x40
80 #define CONF_MASK_4_BYTE 0x80
81 #define CONF_MASK_MULTI_BYTES 0xc0
83 #define CONF_MASK_BYTES 0xc0
84 #define CONF_MASK_TOKEN 0x3f
86 #define CONF_VALUE_1_BYTE(x) (CONF_MASK_1_BYTE | (x))
87 #define CONF_VALUE_2_BYTE(x) (CONF_MASK_2_BYTE | (x))
88 #define CONF_VALUE_4_BYTE(x) (CONF_MASK_4_BYTE | (x))
89 #define CONF_VALUE_MULTI_BYTES(x) (CONF_MASK_MULTI_BYTES | (x))
91 // these definitions are just for convenience of use and readability
92 #define CONF_VALUE_8_BIT(x) CONF_VALUE_1_BYTE(x)
93 #define CONF_VALUE_16_BIT(x) CONF_VALUE_2_BYTE(x)
94 #define CONF_VALUE_32_BIT(x) CONF_VALUE_4_BYTE(x)
95 #define CONF_VALUE_BYTES(x) CONF_VALUE_MULTI_BYTES(x)
97 #define CONF_VALUE_NUM_BYTES(x) ((x) == CONF_MASK_1_BYTE ? 1 : \
98 (x) == CONF_MASK_2_BYTE ? 2 : \
99 (x) == CONF_MASK_4_BYTE ? 4 : 0)
101 #define CONF_CONTENT_NUM_ELEMENTS (3 * 3)
102 #define CONF_CONTENT_NUM_BYTES (CONF_CONTENT_NUM_ELEMENTS * 2)
103 #define CONF_ELEMENT_NUM_BYTES (2)
105 #define CONF_ENTITY_NUM_BYTES(t) ((t) == TYPE_ELEMENT || \
106 (t) == TYPE_ELEMENT_LIST ? \
107 CONF_ELEMENT_NUM_BYTES : \
108 (t) == TYPE_CONTENT || \
109 (t) == TYPE_CONTENT_LIST ? \
110 CONF_CONTENT_NUM_BYTES : 1)
112 #define CONF_ELEMENT_BYTE_POS(i) ((i) * CONF_ELEMENT_NUM_BYTES)
113 #define CONF_ELEMENTS_ELEMENT(b,i) ((b[CONF_ELEMENT_BYTE_POS(i)] << 8) | \
114 (b[CONF_ELEMENT_BYTE_POS(i) + 1]))
116 #define CONF_CONTENT_ELEMENT_POS(c,x,y) ((c) * CONF_CONTENT_NUM_ELEMENTS + \
118 #define CONF_CONTENT_BYTE_POS(c,x,y) (CONF_CONTENT_ELEMENT_POS(c,x,y) * \
119 CONF_ELEMENT_NUM_BYTES)
120 #define CONF_CONTENTS_ELEMENT(b,c,x,y) ((b[CONF_CONTENT_BYTE_POS(c,x,y)]<< 8)|\
121 (b[CONF_CONTENT_BYTE_POS(c,x,y) + 1]))
123 // temporary variables used to store pointers to structure members
124 static struct LevelInfo li;
125 static struct ElementInfo xx_ei, yy_ei;
126 static struct ElementChangeInfo xx_change;
127 static struct ElementGroupInfo xx_group;
128 static struct EnvelopeInfo xx_envelope;
129 static unsigned int xx_event_bits[NUM_CE_BITFIELDS];
130 static char xx_default_description[MAX_ELEMENT_NAME_LEN + 1];
131 static int xx_num_contents;
132 static int xx_current_change_page;
133 static char xx_default_string_empty[1] = "";
134 static int xx_string_length_unused;
136 struct LevelFileConfigInfo
138 int element; // element for which data is to be stored
139 int save_type; // save data always, never or when changed
140 int data_type; // data type (used internally, not stored)
141 int conf_type; // micro chunk identifier (stored in file)
144 void *value; // variable that holds the data to be stored
145 int default_value; // initial default value for this variable
148 void *value_copy; // variable that holds the data to be copied
149 void *num_entities; // number of entities for multi-byte data
150 int default_num_entities; // default number of entities for this data
151 int max_num_entities; // maximal number of entities for this data
152 char *default_string; // optional default string for string data
155 static struct LevelFileConfigInfo chunk_config_INFO[] =
157 // ---------- values not related to single elements -------------------------
160 -1, SAVE_CONF_ALWAYS,
161 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
162 &li.game_engine_type, GAME_ENGINE_TYPE_RND
166 -1, SAVE_CONF_ALWAYS,
167 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
168 &li.fieldx, STD_LEV_FIELDX
171 -1, SAVE_CONF_ALWAYS,
172 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
173 &li.fieldy, STD_LEV_FIELDY
177 -1, SAVE_CONF_ALWAYS,
178 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
183 -1, SAVE_CONF_ALWAYS,
184 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
190 TYPE_INTEGER, CONF_VALUE_32_BIT(2),
196 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
197 &li.use_step_counter, FALSE
202 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
203 &li.wind_direction_initial, MV_NONE
208 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
209 &li.em_slippery_gems, FALSE
214 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
215 &li.use_custom_template, FALSE
220 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
221 &li.can_move_into_acid_bits, ~0 // default: everything can
226 TYPE_BITFIELD, CONF_VALUE_8_BIT(7),
227 &li.dont_collide_with_bits, ~0 // default: always deadly
232 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
233 &li.em_explodes_by_fire, FALSE
238 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
239 &li.score[SC_TIME_BONUS], 1
244 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
245 &li.auto_exit_sokoban, FALSE
250 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
251 &li.auto_count_gems, FALSE
256 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
257 &li.solved_by_one_player, FALSE
267 static struct LevelFileConfigInfo chunk_config_ELEM[] =
269 // (these values are the same for each player)
272 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
273 &li.block_last_field, FALSE // default case for EM levels
277 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
278 &li.sp_block_last_field, TRUE // default case for SP levels
282 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
283 &li.instant_relocation, FALSE
287 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
288 &li.can_pass_to_walkable, FALSE
292 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
293 &li.block_snap_field, TRUE
297 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
298 &li.continuous_snapping, TRUE
302 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
303 &li.shifted_relocation, FALSE
307 TYPE_BOOLEAN, CONF_VALUE_8_BIT(15),
308 &li.lazy_relocation, FALSE
311 // (these values are different for each player)
314 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
315 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
319 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
320 &li.initial_player_gravity[0], FALSE
324 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
325 &li.use_start_element[0], FALSE
329 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
330 &li.start_element[0], EL_PLAYER_1
334 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
335 &li.use_artwork_element[0], FALSE
339 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
340 &li.artwork_element[0], EL_PLAYER_1
344 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
345 &li.use_explosion_element[0], FALSE
349 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
350 &li.explosion_element[0], EL_PLAYER_1
354 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
355 &li.use_initial_inventory[0], FALSE
359 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
360 &li.initial_inventory_size[0], 1
364 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
365 &li.initial_inventory_content[0][0],EL_EMPTY, NULL,
366 &li.initial_inventory_size[0], 1, MAX_INITIAL_INVENTORY_SIZE
371 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
372 &li.initial_player_stepsize[1], STEPSIZE_NORMAL
376 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
377 &li.initial_player_gravity[1], FALSE
381 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
382 &li.use_start_element[1], FALSE
386 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
387 &li.start_element[1], EL_PLAYER_2
391 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
392 &li.use_artwork_element[1], FALSE
396 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
397 &li.artwork_element[1], EL_PLAYER_2
401 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
402 &li.use_explosion_element[1], FALSE
406 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
407 &li.explosion_element[1], EL_PLAYER_2
411 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
412 &li.use_initial_inventory[1], FALSE
416 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
417 &li.initial_inventory_size[1], 1
421 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
422 &li.initial_inventory_content[1][0],EL_EMPTY, NULL,
423 &li.initial_inventory_size[1], 1, MAX_INITIAL_INVENTORY_SIZE
428 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
429 &li.initial_player_stepsize[2], STEPSIZE_NORMAL
433 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
434 &li.initial_player_gravity[2], FALSE
438 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
439 &li.use_start_element[2], FALSE
443 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
444 &li.start_element[2], EL_PLAYER_3
448 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
449 &li.use_artwork_element[2], FALSE
453 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
454 &li.artwork_element[2], EL_PLAYER_3
458 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
459 &li.use_explosion_element[2], FALSE
463 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
464 &li.explosion_element[2], EL_PLAYER_3
468 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
469 &li.use_initial_inventory[2], FALSE
473 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
474 &li.initial_inventory_size[2], 1
478 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
479 &li.initial_inventory_content[2][0],EL_EMPTY, NULL,
480 &li.initial_inventory_size[2], 1, MAX_INITIAL_INVENTORY_SIZE
485 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
486 &li.initial_player_stepsize[3], STEPSIZE_NORMAL
490 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
491 &li.initial_player_gravity[3], FALSE
495 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
496 &li.use_start_element[3], FALSE
500 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
501 &li.start_element[3], EL_PLAYER_4
505 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
506 &li.use_artwork_element[3], FALSE
510 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
511 &li.artwork_element[3], EL_PLAYER_4
515 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
516 &li.use_explosion_element[3], FALSE
520 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
521 &li.explosion_element[3], EL_PLAYER_4
525 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
526 &li.use_initial_inventory[3], FALSE
530 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
531 &li.initial_inventory_size[3], 1
535 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
536 &li.initial_inventory_content[3][0],EL_EMPTY, NULL,
537 &li.initial_inventory_size[3], 1, MAX_INITIAL_INVENTORY_SIZE
542 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
543 &li.score[SC_EMERALD], 10
548 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
549 &li.score[SC_DIAMOND], 10
554 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
555 &li.score[SC_BUG], 10
560 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
561 &li.score[SC_SPACESHIP], 10
566 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
567 &li.score[SC_PACMAN], 10
572 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
573 &li.score[SC_NUT], 10
578 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
579 &li.score[SC_DYNAMITE], 10
584 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
585 &li.score[SC_KEY], 10
590 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
591 &li.score[SC_PEARL], 10
596 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
597 &li.score[SC_CRYSTAL], 10
602 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
603 &li.amoeba_content, EL_DIAMOND
607 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
612 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
613 &li.grow_into_diggable, TRUE
618 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
619 &li.yamyam_content, EL_ROCK, NULL,
620 &li.num_yamyam_contents, 4, MAX_ELEMENT_CONTENTS
624 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
625 &li.score[SC_YAMYAM], 10
630 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
631 &li.score[SC_ROBOT], 10
635 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
641 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
647 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
648 &li.time_magic_wall, 10
653 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
654 &li.game_of_life[0], 2
658 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
659 &li.game_of_life[1], 3
663 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
664 &li.game_of_life[2], 3
668 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
669 &li.game_of_life[3], 3
673 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
674 &li.use_life_bugs, FALSE
679 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
684 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
689 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
694 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
699 EL_TIMEGATE_SWITCH, -1,
700 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
701 &li.time_timegate, 10
705 EL_LIGHT_SWITCH_ACTIVE, -1,
706 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
711 EL_SHIELD_NORMAL, -1,
712 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
713 &li.shield_normal_time, 10
716 EL_SHIELD_NORMAL, -1,
717 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
718 &li.score[SC_SHIELD], 10
722 EL_SHIELD_DEADLY, -1,
723 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
724 &li.shield_deadly_time, 10
727 EL_SHIELD_DEADLY, -1,
728 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
729 &li.score[SC_SHIELD], 10
734 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
739 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
740 &li.extra_time_score, 10
744 EL_TIME_ORB_FULL, -1,
745 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
746 &li.time_orb_time, 10
749 EL_TIME_ORB_FULL, -1,
750 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
751 &li.use_time_orb_bug, FALSE
756 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
757 &li.use_spring_bug, FALSE
762 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
763 &li.android_move_time, 10
767 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
768 &li.android_clone_time, 10
771 EL_EMC_ANDROID, SAVE_CONF_NEVER,
772 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
773 &li.android_clone_element[0], EL_EMPTY, NULL,
774 &li.num_android_clone_elements, 1, MAX_ANDROID_ELEMENTS_OLD
778 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(2),
779 &li.android_clone_element[0], EL_EMPTY, NULL,
780 &li.num_android_clone_elements, 1, MAX_ANDROID_ELEMENTS
785 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
790 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
795 EL_EMC_MAGNIFIER, -1,
796 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
797 &li.magnify_score, 10
800 EL_EMC_MAGNIFIER, -1,
801 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
806 EL_EMC_MAGIC_BALL, -1,
807 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
811 EL_EMC_MAGIC_BALL, -1,
812 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
813 &li.ball_random, FALSE
816 EL_EMC_MAGIC_BALL, -1,
817 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
818 &li.ball_active_initial, FALSE
821 EL_EMC_MAGIC_BALL, -1,
822 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
823 &li.ball_content, EL_EMPTY, NULL,
824 &li.num_ball_contents, 4, MAX_ELEMENT_CONTENTS
828 EL_SOKOBAN_FIELD_EMPTY, -1,
829 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
830 &li.sb_fields_needed, TRUE
834 EL_SOKOBAN_OBJECT, -1,
835 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
836 &li.sb_objects_needed, TRUE
841 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
842 &li.mm_laser_red, FALSE
846 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
847 &li.mm_laser_green, FALSE
851 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
852 &li.mm_laser_blue, TRUE
857 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
858 &li.df_laser_red, TRUE
862 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
863 &li.df_laser_green, TRUE
867 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
868 &li.df_laser_blue, FALSE
872 EL_MM_FUSE_ACTIVE, -1,
873 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
878 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
883 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
887 EL_MM_STEEL_BLOCK, -1,
888 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
889 &li.mm_time_block, 75
893 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
894 &li.score[SC_ELEM_BONUS], 10
897 // ---------- unused values -------------------------------------------------
900 EL_UNKNOWN, SAVE_CONF_NEVER,
901 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
902 &li.score[SC_UNKNOWN_15], 10
912 static struct LevelFileConfigInfo chunk_config_NOTE[] =
916 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
917 &xx_envelope.xsize, MAX_ENVELOPE_XSIZE,
921 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
922 &xx_envelope.ysize, MAX_ENVELOPE_YSIZE,
927 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
928 &xx_envelope.autowrap, FALSE
932 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
933 &xx_envelope.centered, FALSE
938 TYPE_STRING, CONF_VALUE_BYTES(1),
939 &xx_envelope.text, -1, NULL,
940 &xx_string_length_unused, -1, MAX_ENVELOPE_TEXT_LEN,
941 &xx_default_string_empty[0]
951 static struct LevelFileConfigInfo chunk_config_CUSX_base[] =
955 TYPE_STRING, CONF_VALUE_BYTES(1),
956 &xx_ei.description[0], -1,
957 &yy_ei.description[0],
958 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
959 &xx_default_description[0]
964 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
965 &xx_ei.properties[EP_BITFIELD_BASE_NR], EP_BITMASK_BASE_DEFAULT,
966 &yy_ei.properties[EP_BITFIELD_BASE_NR]
968 #if ENABLE_RESERVED_CODE
969 // (reserved for later use)
972 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
973 &xx_ei.properties[EP_BITFIELD_BASE_NR + 1], EP_BITMASK_DEFAULT,
974 &yy_ei.properties[EP_BITFIELD_BASE_NR + 1]
980 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
981 &xx_ei.use_gfx_element, FALSE,
982 &yy_ei.use_gfx_element
986 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
987 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE,
988 &yy_ei.gfx_element_initial
993 TYPE_BITFIELD, CONF_VALUE_8_BIT(2),
994 &xx_ei.access_direction, MV_ALL_DIRECTIONS,
995 &yy_ei.access_direction
1000 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
1001 &xx_ei.collect_score_initial, 10,
1002 &yy_ei.collect_score_initial
1006 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
1007 &xx_ei.collect_count_initial, 1,
1008 &yy_ei.collect_count_initial
1013 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
1014 &xx_ei.ce_value_fixed_initial, 0,
1015 &yy_ei.ce_value_fixed_initial
1019 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
1020 &xx_ei.ce_value_random_initial, 0,
1021 &yy_ei.ce_value_random_initial
1025 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
1026 &xx_ei.use_last_ce_value, FALSE,
1027 &yy_ei.use_last_ce_value
1032 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
1033 &xx_ei.push_delay_fixed, 8,
1034 &yy_ei.push_delay_fixed
1038 TYPE_INTEGER, CONF_VALUE_16_BIT(7),
1039 &xx_ei.push_delay_random, 8,
1040 &yy_ei.push_delay_random
1044 TYPE_INTEGER, CONF_VALUE_16_BIT(8),
1045 &xx_ei.drop_delay_fixed, 0,
1046 &yy_ei.drop_delay_fixed
1050 TYPE_INTEGER, CONF_VALUE_16_BIT(9),
1051 &xx_ei.drop_delay_random, 0,
1052 &yy_ei.drop_delay_random
1056 TYPE_INTEGER, CONF_VALUE_16_BIT(10),
1057 &xx_ei.move_delay_fixed, 0,
1058 &yy_ei.move_delay_fixed
1062 TYPE_INTEGER, CONF_VALUE_16_BIT(11),
1063 &xx_ei.move_delay_random, 0,
1064 &yy_ei.move_delay_random
1069 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
1070 &xx_ei.move_pattern, MV_ALL_DIRECTIONS,
1075 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
1076 &xx_ei.move_direction_initial, MV_START_AUTOMATIC,
1077 &yy_ei.move_direction_initial
1081 TYPE_INTEGER, CONF_VALUE_8_BIT(5),
1082 &xx_ei.move_stepsize, TILEX / 8,
1083 &yy_ei.move_stepsize
1088 TYPE_ELEMENT, CONF_VALUE_16_BIT(12),
1089 &xx_ei.move_enter_element, EL_EMPTY_SPACE,
1090 &yy_ei.move_enter_element
1094 TYPE_ELEMENT, CONF_VALUE_16_BIT(13),
1095 &xx_ei.move_leave_element, EL_EMPTY_SPACE,
1096 &yy_ei.move_leave_element
1100 TYPE_INTEGER, CONF_VALUE_8_BIT(6),
1101 &xx_ei.move_leave_type, LEAVE_TYPE_UNLIMITED,
1102 &yy_ei.move_leave_type
1107 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
1108 &xx_ei.slippery_type, SLIPPERY_ANY_RANDOM,
1109 &yy_ei.slippery_type
1114 TYPE_INTEGER, CONF_VALUE_8_BIT(8),
1115 &xx_ei.explosion_type, EXPLODES_3X3,
1116 &yy_ei.explosion_type
1120 TYPE_INTEGER, CONF_VALUE_16_BIT(14),
1121 &xx_ei.explosion_delay, 16,
1122 &yy_ei.explosion_delay
1126 TYPE_INTEGER, CONF_VALUE_16_BIT(15),
1127 &xx_ei.ignition_delay, 8,
1128 &yy_ei.ignition_delay
1133 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(2),
1134 &xx_ei.content, EL_EMPTY_SPACE,
1136 &xx_num_contents, 1, 1
1139 // ---------- "num_change_pages" must be the last entry ---------------------
1142 -1, SAVE_CONF_ALWAYS,
1143 TYPE_INTEGER, CONF_VALUE_8_BIT(9),
1144 &xx_ei.num_change_pages, 1,
1145 &yy_ei.num_change_pages
1156 static struct LevelFileConfigInfo chunk_config_CUSX_change[] =
1158 // ---------- "current_change_page" must be the first entry -----------------
1161 -1, SAVE_CONF_ALWAYS,
1162 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1163 &xx_current_change_page, -1
1166 // ---------- (the remaining entries can be in any order) -------------------
1170 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
1171 &xx_change.can_change, FALSE
1176 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
1177 &xx_event_bits[0], 0
1181 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
1182 &xx_event_bits[1], 0
1187 TYPE_BITFIELD, CONF_VALUE_8_BIT(3),
1188 &xx_change.trigger_player, CH_PLAYER_ANY
1192 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
1193 &xx_change.trigger_side, CH_SIDE_ANY
1197 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
1198 &xx_change.trigger_page, CH_PAGE_ANY
1203 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1204 &xx_change.target_element, EL_EMPTY_SPACE
1209 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
1210 &xx_change.delay_fixed, 0
1214 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
1215 &xx_change.delay_random, 0
1219 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
1220 &xx_change.delay_frames, FRAMES_PER_SECOND
1225 TYPE_ELEMENT, CONF_VALUE_16_BIT(5),
1226 &xx_change.initial_trigger_element, EL_EMPTY_SPACE
1231 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
1232 &xx_change.explode, FALSE
1236 TYPE_BOOLEAN, CONF_VALUE_8_BIT(7),
1237 &xx_change.use_target_content, FALSE
1241 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
1242 &xx_change.only_if_complete, FALSE
1246 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1247 &xx_change.use_random_replace, FALSE
1251 TYPE_INTEGER, CONF_VALUE_8_BIT(10),
1252 &xx_change.random_percentage, 100
1256 TYPE_INTEGER, CONF_VALUE_8_BIT(11),
1257 &xx_change.replace_when, CP_WHEN_EMPTY
1262 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1263 &xx_change.has_action, FALSE
1267 TYPE_INTEGER, CONF_VALUE_8_BIT(13),
1268 &xx_change.action_type, CA_NO_ACTION
1272 TYPE_INTEGER, CONF_VALUE_8_BIT(14),
1273 &xx_change.action_mode, CA_MODE_UNDEFINED
1277 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
1278 &xx_change.action_arg, CA_ARG_UNDEFINED
1283 TYPE_ELEMENT, CONF_VALUE_16_BIT(7),
1284 &xx_change.action_element, EL_EMPTY_SPACE
1289 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
1290 &xx_change.target_content, EL_EMPTY_SPACE, NULL,
1291 &xx_num_contents, 1, 1
1301 static struct LevelFileConfigInfo chunk_config_GRPX[] =
1305 TYPE_STRING, CONF_VALUE_BYTES(1),
1306 &xx_ei.description[0], -1, NULL,
1307 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
1308 &xx_default_description[0]
1313 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
1314 &xx_ei.use_gfx_element, FALSE
1318 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1319 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE
1324 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
1325 &xx_group.choice_mode, ANIM_RANDOM
1330 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(2),
1331 &xx_group.element[0], EL_EMPTY_SPACE, NULL,
1332 &xx_group.num_elements, 1, MAX_ELEMENTS_IN_GROUP
1342 static struct LevelFileConfigInfo chunk_config_CONF[] = // (OBSOLETE)
1346 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1347 &li.block_snap_field, TRUE
1351 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
1352 &li.continuous_snapping, TRUE
1356 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1357 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
1361 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
1362 &li.use_start_element[0], FALSE
1366 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1367 &li.start_element[0], EL_PLAYER_1
1371 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
1372 &li.use_artwork_element[0], FALSE
1376 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
1377 &li.artwork_element[0], EL_PLAYER_1
1381 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1382 &li.use_explosion_element[0], FALSE
1386 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
1387 &li.explosion_element[0], EL_PLAYER_1
1402 filetype_id_list[] =
1404 { LEVEL_FILE_TYPE_RND, "RND" },
1405 { LEVEL_FILE_TYPE_BD, "BD" },
1406 { LEVEL_FILE_TYPE_EM, "EM" },
1407 { LEVEL_FILE_TYPE_SP, "SP" },
1408 { LEVEL_FILE_TYPE_DX, "DX" },
1409 { LEVEL_FILE_TYPE_SB, "SB" },
1410 { LEVEL_FILE_TYPE_DC, "DC" },
1411 { LEVEL_FILE_TYPE_MM, "MM" },
1412 { LEVEL_FILE_TYPE_MM, "DF" },
1417 // ============================================================================
1418 // level file functions
1419 // ============================================================================
1421 static boolean check_special_flags(char *flag)
1423 if (strEqual(options.special_flags, flag) ||
1424 strEqual(leveldir_current->special_flags, flag))
1430 static struct DateInfo getCurrentDate(void)
1432 time_t epoch_seconds = time(NULL);
1433 struct tm *now = localtime(&epoch_seconds);
1434 struct DateInfo date;
1436 date.year = now->tm_year + 1900;
1437 date.month = now->tm_mon + 1;
1438 date.day = now->tm_mday;
1440 date.src = DATE_SRC_CLOCK;
1445 static void resetEventFlags(struct ElementChangeInfo *change)
1449 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1450 change->has_event[i] = FALSE;
1453 static void resetEventBits(void)
1457 for (i = 0; i < NUM_CE_BITFIELDS; i++)
1458 xx_event_bits[i] = 0;
1461 static void setEventFlagsFromEventBits(struct ElementChangeInfo *change)
1465 /* important: only change event flag if corresponding event bit is set
1466 (this is because all xx_event_bits[] values are loaded separately,
1467 and all xx_event_bits[] values are set back to zero before loading
1468 another value xx_event_bits[x] (each value representing 32 flags)) */
1470 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1471 if (xx_event_bits[CH_EVENT_BITFIELD_NR(i)] & CH_EVENT_BIT(i))
1472 change->has_event[i] = TRUE;
1475 static void setEventBitsFromEventFlags(struct ElementChangeInfo *change)
1479 /* in contrast to the above function setEventFlagsFromEventBits(), it
1480 would also be possible to set all bits in xx_event_bits[] to 0 or 1
1481 depending on the corresponding change->has_event[i] values here, as
1482 all xx_event_bits[] values are reset in resetEventBits() before */
1484 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1485 if (change->has_event[i])
1486 xx_event_bits[CH_EVENT_BITFIELD_NR(i)] |= CH_EVENT_BIT(i);
1489 static char *getDefaultElementDescription(struct ElementInfo *ei)
1491 static char description[MAX_ELEMENT_NAME_LEN + 1];
1492 char *default_description = (ei->custom_description != NULL ?
1493 ei->custom_description :
1494 ei->editor_description);
1497 // always start with reliable default values
1498 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1499 description[i] = '\0';
1501 // truncate element description to MAX_ELEMENT_NAME_LEN bytes
1502 strncpy(description, default_description, MAX_ELEMENT_NAME_LEN);
1504 return &description[0];
1507 static void setElementDescriptionToDefault(struct ElementInfo *ei)
1509 char *default_description = getDefaultElementDescription(ei);
1512 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1513 ei->description[i] = default_description[i];
1516 static void setConfigToDefaultsFromConfigList(struct LevelFileConfigInfo *conf)
1520 for (i = 0; conf[i].data_type != -1; i++)
1522 int default_value = conf[i].default_value;
1523 int data_type = conf[i].data_type;
1524 int conf_type = conf[i].conf_type;
1525 int byte_mask = conf_type & CONF_MASK_BYTES;
1527 if (byte_mask == CONF_MASK_MULTI_BYTES)
1529 int default_num_entities = conf[i].default_num_entities;
1530 int max_num_entities = conf[i].max_num_entities;
1532 *(int *)(conf[i].num_entities) = default_num_entities;
1534 if (data_type == TYPE_STRING)
1536 char *default_string = conf[i].default_string;
1537 char *string = (char *)(conf[i].value);
1539 strncpy(string, default_string, max_num_entities);
1541 else if (data_type == TYPE_ELEMENT_LIST)
1543 int *element_array = (int *)(conf[i].value);
1546 for (j = 0; j < max_num_entities; j++)
1547 element_array[j] = default_value;
1549 else if (data_type == TYPE_CONTENT_LIST)
1551 struct Content *content = (struct Content *)(conf[i].value);
1554 for (c = 0; c < max_num_entities; c++)
1555 for (y = 0; y < 3; y++)
1556 for (x = 0; x < 3; x++)
1557 content[c].e[x][y] = default_value;
1560 else // constant size configuration data (1, 2 or 4 bytes)
1562 if (data_type == TYPE_BOOLEAN)
1563 *(boolean *)(conf[i].value) = default_value;
1565 *(int *) (conf[i].value) = default_value;
1570 static void copyConfigFromConfigList(struct LevelFileConfigInfo *conf)
1574 for (i = 0; conf[i].data_type != -1; i++)
1576 int data_type = conf[i].data_type;
1577 int conf_type = conf[i].conf_type;
1578 int byte_mask = conf_type & CONF_MASK_BYTES;
1580 if (byte_mask == CONF_MASK_MULTI_BYTES)
1582 int max_num_entities = conf[i].max_num_entities;
1584 if (data_type == TYPE_STRING)
1586 char *string = (char *)(conf[i].value);
1587 char *string_copy = (char *)(conf[i].value_copy);
1589 strncpy(string_copy, string, max_num_entities);
1591 else if (data_type == TYPE_ELEMENT_LIST)
1593 int *element_array = (int *)(conf[i].value);
1594 int *element_array_copy = (int *)(conf[i].value_copy);
1597 for (j = 0; j < max_num_entities; j++)
1598 element_array_copy[j] = element_array[j];
1600 else if (data_type == TYPE_CONTENT_LIST)
1602 struct Content *content = (struct Content *)(conf[i].value);
1603 struct Content *content_copy = (struct Content *)(conf[i].value_copy);
1606 for (c = 0; c < max_num_entities; c++)
1607 for (y = 0; y < 3; y++)
1608 for (x = 0; x < 3; x++)
1609 content_copy[c].e[x][y] = content[c].e[x][y];
1612 else // constant size configuration data (1, 2 or 4 bytes)
1614 if (data_type == TYPE_BOOLEAN)
1615 *(boolean *)(conf[i].value_copy) = *(boolean *)(conf[i].value);
1617 *(int *) (conf[i].value_copy) = *(int *) (conf[i].value);
1622 void copyElementInfo(struct ElementInfo *ei_from, struct ElementInfo *ei_to)
1626 xx_ei = *ei_from; // copy element data into temporary buffer
1627 yy_ei = *ei_to; // copy element data into temporary buffer
1629 copyConfigFromConfigList(chunk_config_CUSX_base);
1634 // ---------- reinitialize and copy change pages ----------
1636 ei_to->num_change_pages = ei_from->num_change_pages;
1637 ei_to->current_change_page = ei_from->current_change_page;
1639 setElementChangePages(ei_to, ei_to->num_change_pages);
1641 for (i = 0; i < ei_to->num_change_pages; i++)
1642 ei_to->change_page[i] = ei_from->change_page[i];
1644 // ---------- copy group element info ----------
1645 if (ei_from->group != NULL && ei_to->group != NULL) // group or internal
1646 *ei_to->group = *ei_from->group;
1648 // mark this custom element as modified
1649 ei_to->modified_settings = TRUE;
1652 void setElementChangePages(struct ElementInfo *ei, int change_pages)
1654 int change_page_size = sizeof(struct ElementChangeInfo);
1656 ei->num_change_pages = MAX(1, change_pages);
1659 checked_realloc(ei->change_page, ei->num_change_pages * change_page_size);
1661 if (ei->current_change_page >= ei->num_change_pages)
1662 ei->current_change_page = ei->num_change_pages - 1;
1664 ei->change = &ei->change_page[ei->current_change_page];
1667 void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
1669 xx_change = *change; // copy change data into temporary buffer
1671 setConfigToDefaultsFromConfigList(chunk_config_CUSX_change);
1673 *change = xx_change;
1675 resetEventFlags(change);
1677 change->direct_action = 0;
1678 change->other_action = 0;
1680 change->pre_change_function = NULL;
1681 change->change_function = NULL;
1682 change->post_change_function = NULL;
1685 static void setLevelInfoToDefaults_Level(struct LevelInfo *level)
1689 li = *level; // copy level data into temporary buffer
1690 setConfigToDefaultsFromConfigList(chunk_config_INFO);
1691 *level = li; // copy temporary buffer back to level data
1693 setLevelInfoToDefaults_EM();
1694 setLevelInfoToDefaults_SP();
1695 setLevelInfoToDefaults_MM();
1697 level->native_em_level = &native_em_level;
1698 level->native_sp_level = &native_sp_level;
1699 level->native_mm_level = &native_mm_level;
1701 level->file_version = FILE_VERSION_ACTUAL;
1702 level->game_version = GAME_VERSION_ACTUAL;
1704 level->creation_date = getCurrentDate();
1706 level->encoding_16bit_field = TRUE;
1707 level->encoding_16bit_yamyam = TRUE;
1708 level->encoding_16bit_amoeba = TRUE;
1710 // clear level name and level author string buffers
1711 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
1712 level->name[i] = '\0';
1713 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
1714 level->author[i] = '\0';
1716 // set level name and level author to default values
1717 strcpy(level->name, NAMELESS_LEVEL_NAME);
1718 strcpy(level->author, ANONYMOUS_NAME);
1720 // set level playfield to playable default level with player and exit
1721 for (x = 0; x < MAX_LEV_FIELDX; x++)
1722 for (y = 0; y < MAX_LEV_FIELDY; y++)
1723 level->field[x][y] = EL_SAND;
1725 level->field[0][0] = EL_PLAYER_1;
1726 level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
1728 BorderElement = EL_STEELWALL;
1730 // detect custom elements when loading them
1731 level->file_has_custom_elements = FALSE;
1733 // set all bug compatibility flags to "false" => do not emulate this bug
1734 level->use_action_after_change_bug = FALSE;
1736 if (leveldir_current)
1738 // try to determine better author name than 'anonymous'
1739 if (!strEqual(leveldir_current->author, ANONYMOUS_NAME))
1741 strncpy(level->author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
1742 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1746 switch (LEVELCLASS(leveldir_current))
1748 case LEVELCLASS_TUTORIAL:
1749 strcpy(level->author, PROGRAM_AUTHOR_STRING);
1752 case LEVELCLASS_CONTRIB:
1753 strncpy(level->author, leveldir_current->name, MAX_LEVEL_AUTHOR_LEN);
1754 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1757 case LEVELCLASS_PRIVATE:
1758 strncpy(level->author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
1759 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1763 // keep default value
1770 static void setLevelInfoToDefaults_Elements(struct LevelInfo *level)
1772 static boolean clipboard_elements_initialized = FALSE;
1775 InitElementPropertiesStatic();
1777 li = *level; // copy level data into temporary buffer
1778 setConfigToDefaultsFromConfigList(chunk_config_ELEM);
1779 *level = li; // copy temporary buffer back to level data
1781 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1784 struct ElementInfo *ei = &element_info[element];
1786 // never initialize clipboard elements after the very first time
1787 // (to be able to use clipboard elements between several levels)
1788 if (IS_CLIPBOARD_ELEMENT(element) && clipboard_elements_initialized)
1791 if (IS_ENVELOPE(element))
1793 int envelope_nr = element - EL_ENVELOPE_1;
1795 setConfigToDefaultsFromConfigList(chunk_config_NOTE);
1797 level->envelope[envelope_nr] = xx_envelope;
1800 if (IS_CUSTOM_ELEMENT(element) ||
1801 IS_GROUP_ELEMENT(element) ||
1802 IS_INTERNAL_ELEMENT(element))
1804 xx_ei = *ei; // copy element data into temporary buffer
1806 setConfigToDefaultsFromConfigList(chunk_config_CUSX_base);
1811 setElementChangePages(ei, 1);
1812 setElementChangeInfoToDefaults(ei->change);
1814 if (IS_CUSTOM_ELEMENT(element) ||
1815 IS_GROUP_ELEMENT(element) ||
1816 IS_INTERNAL_ELEMENT(element))
1818 setElementDescriptionToDefault(ei);
1820 ei->modified_settings = FALSE;
1823 if (IS_CUSTOM_ELEMENT(element) ||
1824 IS_INTERNAL_ELEMENT(element))
1826 // internal values used in level editor
1828 ei->access_type = 0;
1829 ei->access_layer = 0;
1830 ei->access_protected = 0;
1831 ei->walk_to_action = 0;
1832 ei->smash_targets = 0;
1835 ei->can_explode_by_fire = FALSE;
1836 ei->can_explode_smashed = FALSE;
1837 ei->can_explode_impact = FALSE;
1839 ei->current_change_page = 0;
1842 if (IS_GROUP_ELEMENT(element) ||
1843 IS_INTERNAL_ELEMENT(element))
1845 struct ElementGroupInfo *group;
1847 // initialize memory for list of elements in group
1848 if (ei->group == NULL)
1849 ei->group = checked_malloc(sizeof(struct ElementGroupInfo));
1853 xx_group = *group; // copy group data into temporary buffer
1855 setConfigToDefaultsFromConfigList(chunk_config_GRPX);
1861 clipboard_elements_initialized = TRUE;
1864 static void setLevelInfoToDefaults(struct LevelInfo *level,
1865 boolean level_info_only,
1866 boolean reset_file_status)
1868 setLevelInfoToDefaults_Level(level);
1870 if (!level_info_only)
1871 setLevelInfoToDefaults_Elements(level);
1873 if (reset_file_status)
1875 level->no_valid_file = FALSE;
1876 level->no_level_file = FALSE;
1879 level->changed = FALSE;
1882 static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
1884 level_file_info->nr = 0;
1885 level_file_info->type = LEVEL_FILE_TYPE_UNKNOWN;
1886 level_file_info->packed = FALSE;
1888 setString(&level_file_info->basename, NULL);
1889 setString(&level_file_info->filename, NULL);
1892 int getMappedElement_SB(int, boolean);
1894 static void ActivateLevelTemplate(void)
1898 if (check_special_flags("load_xsb_to_ces"))
1900 // fill smaller playfields with padding "beyond border wall" elements
1901 if (level.fieldx < level_template.fieldx ||
1902 level.fieldy < level_template.fieldy)
1904 short field[level.fieldx][level.fieldy];
1905 int new_fieldx = MAX(level.fieldx, level_template.fieldx);
1906 int new_fieldy = MAX(level.fieldy, level_template.fieldy);
1907 int pos_fieldx = (new_fieldx - level.fieldx) / 2;
1908 int pos_fieldy = (new_fieldy - level.fieldy) / 2;
1910 // copy old playfield (which is smaller than the visible area)
1911 for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
1912 field[x][y] = level.field[x][y];
1914 // fill new, larger playfield with "beyond border wall" elements
1915 for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++)
1916 level.field[x][y] = getMappedElement_SB('_', TRUE);
1918 // copy the old playfield to the middle of the new playfield
1919 for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
1920 level.field[pos_fieldx + x][pos_fieldy + y] = field[x][y];
1922 level.fieldx = new_fieldx;
1923 level.fieldy = new_fieldy;
1927 // Currently there is no special action needed to activate the template
1928 // data, because 'element_info' property settings overwrite the original
1929 // level data, while all other variables do not change.
1931 // Exception: 'from_level_template' elements in the original level playfield
1932 // are overwritten with the corresponding elements at the same position in
1933 // playfield from the level template.
1935 for (x = 0; x < level.fieldx; x++)
1936 for (y = 0; y < level.fieldy; y++)
1937 if (level.field[x][y] == EL_FROM_LEVEL_TEMPLATE)
1938 level.field[x][y] = level_template.field[x][y];
1940 if (check_special_flags("load_xsb_to_ces"))
1942 struct LevelInfo level_backup = level;
1944 // overwrite all individual level settings from template level settings
1945 level = level_template;
1947 // restore level file info
1948 level.file_info = level_backup.file_info;
1950 // restore playfield size
1951 level.fieldx = level_backup.fieldx;
1952 level.fieldy = level_backup.fieldy;
1954 // restore playfield content
1955 for (x = 0; x < level.fieldx; x++)
1956 for (y = 0; y < level.fieldy; y++)
1957 level.field[x][y] = level_backup.field[x][y];
1959 // restore name and author from individual level
1960 strcpy(level.name, level_backup.name);
1961 strcpy(level.author, level_backup.author);
1963 // restore flag "use_custom_template"
1964 level.use_custom_template = level_backup.use_custom_template;
1968 static char *getLevelFilenameFromBasename(char *basename)
1970 static char *filename = NULL;
1972 checked_free(filename);
1974 filename = getPath2(getCurrentLevelDir(), basename);
1979 static int getFileTypeFromBasename(char *basename)
1981 // !!! ALSO SEE COMMENT IN checkForPackageFromBasename() !!!
1983 static char *filename = NULL;
1984 struct stat file_status;
1986 // ---------- try to determine file type from filename ----------
1988 // check for typical filename of a Supaplex level package file
1989 if (strlen(basename) == 10 && strPrefixLower(basename, "levels.d"))
1990 return LEVEL_FILE_TYPE_SP;
1992 // check for typical filename of a Diamond Caves II level package file
1993 if (strSuffixLower(basename, ".dc") ||
1994 strSuffixLower(basename, ".dc2"))
1995 return LEVEL_FILE_TYPE_DC;
1997 // check for typical filename of a Sokoban level package file
1998 if (strSuffixLower(basename, ".xsb") &&
1999 strchr(basename, '%') == NULL)
2000 return LEVEL_FILE_TYPE_SB;
2002 // ---------- try to determine file type from filesize ----------
2004 checked_free(filename);
2005 filename = getPath2(getCurrentLevelDir(), basename);
2007 if (stat(filename, &file_status) == 0)
2009 // check for typical filesize of a Supaplex level package file
2010 if (file_status.st_size == 170496)
2011 return LEVEL_FILE_TYPE_SP;
2014 return LEVEL_FILE_TYPE_UNKNOWN;
2017 static int getFileTypeFromMagicBytes(char *filename, int type)
2021 if ((file = openFile(filename, MODE_READ)))
2023 char chunk_name[CHUNK_ID_LEN + 1];
2025 getFileChunkBE(file, chunk_name, NULL);
2027 if (strEqual(chunk_name, "MMII") ||
2028 strEqual(chunk_name, "MIRR"))
2029 type = LEVEL_FILE_TYPE_MM;
2037 static boolean checkForPackageFromBasename(char *basename)
2039 // !!! WON'T WORK ANYMORE IF getFileTypeFromBasename() ALSO DETECTS !!!
2040 // !!! SINGLE LEVELS (CURRENTLY ONLY DETECTS LEVEL PACKAGES !!!
2042 return (getFileTypeFromBasename(basename) != LEVEL_FILE_TYPE_UNKNOWN);
2045 static char *getSingleLevelBasenameExt(int nr, char *extension)
2047 static char basename[MAX_FILENAME_LEN];
2050 sprintf(basename, "%s", LEVELTEMPLATE_FILENAME);
2052 sprintf(basename, "%03d.%s", nr, extension);
2057 static char *getSingleLevelBasename(int nr)
2059 return getSingleLevelBasenameExt(nr, LEVELFILE_EXTENSION);
2062 static char *getPackedLevelBasename(int type)
2064 static char basename[MAX_FILENAME_LEN];
2065 char *directory = getCurrentLevelDir();
2067 DirectoryEntry *dir_entry;
2069 strcpy(basename, UNDEFINED_FILENAME); // default: undefined file
2071 if ((dir = openDirectory(directory)) == NULL)
2073 Error(ERR_WARN, "cannot read current level directory '%s'", directory);
2078 while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries
2080 char *entry_basename = dir_entry->basename;
2081 int entry_type = getFileTypeFromBasename(entry_basename);
2083 if (entry_type != LEVEL_FILE_TYPE_UNKNOWN) // found valid level package
2085 if (type == LEVEL_FILE_TYPE_UNKNOWN ||
2088 strcpy(basename, entry_basename);
2095 closeDirectory(dir);
2100 static char *getSingleLevelFilename(int nr)
2102 return getLevelFilenameFromBasename(getSingleLevelBasename(nr));
2105 #if ENABLE_UNUSED_CODE
2106 static char *getPackedLevelFilename(int type)
2108 return getLevelFilenameFromBasename(getPackedLevelBasename(type));
2112 char *getDefaultLevelFilename(int nr)
2114 return getSingleLevelFilename(nr);
2117 #if ENABLE_UNUSED_CODE
2118 static void setLevelFileInfo_SingleLevelFilename(struct LevelFileInfo *lfi,
2122 lfi->packed = FALSE;
2124 setString(&lfi->basename, getSingleLevelBasename(lfi->nr, lfi->type));
2125 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2129 static void setLevelFileInfo_FormatLevelFilename(struct LevelFileInfo *lfi,
2130 int type, char *format, ...)
2132 static char basename[MAX_FILENAME_LEN];
2135 va_start(ap, format);
2136 vsprintf(basename, format, ap);
2140 lfi->packed = FALSE;
2142 setString(&lfi->basename, basename);
2143 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2146 static void setLevelFileInfo_PackedLevelFilename(struct LevelFileInfo *lfi,
2152 setString(&lfi->basename, getPackedLevelBasename(lfi->type));
2153 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2156 static int getFiletypeFromID(char *filetype_id)
2158 char *filetype_id_lower;
2159 int filetype = LEVEL_FILE_TYPE_UNKNOWN;
2162 if (filetype_id == NULL)
2163 return LEVEL_FILE_TYPE_UNKNOWN;
2165 filetype_id_lower = getStringToLower(filetype_id);
2167 for (i = 0; filetype_id_list[i].id != NULL; i++)
2169 char *id_lower = getStringToLower(filetype_id_list[i].id);
2171 if (strEqual(filetype_id_lower, id_lower))
2172 filetype = filetype_id_list[i].filetype;
2176 if (filetype != LEVEL_FILE_TYPE_UNKNOWN)
2180 free(filetype_id_lower);
2185 char *getLocalLevelTemplateFilename(void)
2187 return getDefaultLevelFilename(-1);
2190 char *getGlobalLevelTemplateFilename(void)
2192 // global variable "leveldir_current" must be modified in the loop below
2193 LevelDirTree *leveldir_current_last = leveldir_current;
2194 char *filename = NULL;
2196 // check for template level in path from current to topmost tree node
2198 while (leveldir_current != NULL)
2200 filename = getDefaultLevelFilename(-1);
2202 if (fileExists(filename))
2205 leveldir_current = leveldir_current->node_parent;
2208 // restore global variable "leveldir_current" modified in above loop
2209 leveldir_current = leveldir_current_last;
2214 static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
2218 // special case: level number is negative => check for level template file
2221 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2222 getSingleLevelBasename(-1));
2224 // replace local level template filename with global template filename
2225 setString(&lfi->filename, getGlobalLevelTemplateFilename());
2227 // no fallback if template file not existing
2231 // special case: check for file name/pattern specified in "levelinfo.conf"
2232 if (leveldir_current->level_filename != NULL)
2234 int filetype = getFiletypeFromID(leveldir_current->level_filetype);
2236 setLevelFileInfo_FormatLevelFilename(lfi, filetype,
2237 leveldir_current->level_filename, nr);
2239 lfi->packed = checkForPackageFromBasename(leveldir_current->level_filename);
2241 if (fileExists(lfi->filename))
2244 else if (leveldir_current->level_filetype != NULL)
2246 int filetype = getFiletypeFromID(leveldir_current->level_filetype);
2248 // check for specified native level file with standard file name
2249 setLevelFileInfo_FormatLevelFilename(lfi, filetype,
2250 "%03d.%s", nr, LEVELFILE_EXTENSION);
2251 if (fileExists(lfi->filename))
2255 // check for native Rocks'n'Diamonds level file
2256 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2257 "%03d.%s", nr, LEVELFILE_EXTENSION);
2258 if (fileExists(lfi->filename))
2261 // check for Emerald Mine level file (V1)
2262 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "a%c%c",
2263 'a' + (nr / 10) % 26, '0' + nr % 10);
2264 if (fileExists(lfi->filename))
2266 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "A%c%c",
2267 'A' + (nr / 10) % 26, '0' + nr % 10);
2268 if (fileExists(lfi->filename))
2271 // check for Emerald Mine level file (V2 to V5)
2272 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%d", nr);
2273 if (fileExists(lfi->filename))
2276 // check for Emerald Mine level file (V6 / single mode)
2277 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02ds", nr);
2278 if (fileExists(lfi->filename))
2280 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dS", nr);
2281 if (fileExists(lfi->filename))
2284 // check for Emerald Mine level file (V6 / teamwork mode)
2285 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dt", nr);
2286 if (fileExists(lfi->filename))
2288 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dT", nr);
2289 if (fileExists(lfi->filename))
2292 // check for various packed level file formats
2293 setLevelFileInfo_PackedLevelFilename(lfi, LEVEL_FILE_TYPE_UNKNOWN);
2294 if (fileExists(lfi->filename))
2297 // no known level file found -- use default values (and fail later)
2298 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2299 "%03d.%s", nr, LEVELFILE_EXTENSION);
2302 static void determineLevelFileInfo_Filetype(struct LevelFileInfo *lfi)
2304 if (lfi->type == LEVEL_FILE_TYPE_UNKNOWN)
2305 lfi->type = getFileTypeFromBasename(lfi->basename);
2307 if (lfi->type == LEVEL_FILE_TYPE_RND)
2308 lfi->type = getFileTypeFromMagicBytes(lfi->filename, lfi->type);
2311 static void setLevelFileInfo(struct LevelFileInfo *level_file_info, int nr)
2313 // always start with reliable default values
2314 setFileInfoToDefaults(level_file_info);
2316 level_file_info->nr = nr; // set requested level number
2318 determineLevelFileInfo_Filename(level_file_info);
2319 determineLevelFileInfo_Filetype(level_file_info);
2322 static void copyLevelFileInfo(struct LevelFileInfo *lfi_from,
2323 struct LevelFileInfo *lfi_to)
2325 lfi_to->nr = lfi_from->nr;
2326 lfi_to->type = lfi_from->type;
2327 lfi_to->packed = lfi_from->packed;
2329 setString(&lfi_to->basename, lfi_from->basename);
2330 setString(&lfi_to->filename, lfi_from->filename);
2333 // ----------------------------------------------------------------------------
2334 // functions for loading R'n'D level
2335 // ----------------------------------------------------------------------------
2337 static int getMappedElement(int element)
2339 // remap some (historic, now obsolete) elements
2343 case EL_PLAYER_OBSOLETE:
2344 element = EL_PLAYER_1;
2347 case EL_KEY_OBSOLETE:
2351 case EL_EM_KEY_1_FILE_OBSOLETE:
2352 element = EL_EM_KEY_1;
2355 case EL_EM_KEY_2_FILE_OBSOLETE:
2356 element = EL_EM_KEY_2;
2359 case EL_EM_KEY_3_FILE_OBSOLETE:
2360 element = EL_EM_KEY_3;
2363 case EL_EM_KEY_4_FILE_OBSOLETE:
2364 element = EL_EM_KEY_4;
2367 case EL_ENVELOPE_OBSOLETE:
2368 element = EL_ENVELOPE_1;
2376 if (element >= NUM_FILE_ELEMENTS)
2378 Error(ERR_WARN, "invalid level element %d", element);
2380 element = EL_UNKNOWN;
2388 static int getMappedElementByVersion(int element, int game_version)
2390 // remap some elements due to certain game version
2392 if (game_version <= VERSION_IDENT(2,2,0,0))
2394 // map game font elements
2395 element = (element == EL_CHAR('[') ? EL_CHAR_AUMLAUT :
2396 element == EL_CHAR('\\') ? EL_CHAR_OUMLAUT :
2397 element == EL_CHAR(']') ? EL_CHAR_UUMLAUT :
2398 element == EL_CHAR('^') ? EL_CHAR_COPYRIGHT : element);
2401 if (game_version < VERSION_IDENT(3,0,0,0))
2403 // map Supaplex gravity tube elements
2404 element = (element == EL_SP_GRAVITY_PORT_LEFT ? EL_SP_PORT_LEFT :
2405 element == EL_SP_GRAVITY_PORT_RIGHT ? EL_SP_PORT_RIGHT :
2406 element == EL_SP_GRAVITY_PORT_UP ? EL_SP_PORT_UP :
2407 element == EL_SP_GRAVITY_PORT_DOWN ? EL_SP_PORT_DOWN :
2414 static int LoadLevel_VERS(File *file, int chunk_size, struct LevelInfo *level)
2416 level->file_version = getFileVersion(file);
2417 level->game_version = getFileVersion(file);
2422 static int LoadLevel_DATE(File *file, int chunk_size, struct LevelInfo *level)
2424 level->creation_date.year = getFile16BitBE(file);
2425 level->creation_date.month = getFile8Bit(file);
2426 level->creation_date.day = getFile8Bit(file);
2428 level->creation_date.src = DATE_SRC_LEVELFILE;
2433 static int LoadLevel_HEAD(File *file, int chunk_size, struct LevelInfo *level)
2435 int initial_player_stepsize;
2436 int initial_player_gravity;
2439 level->fieldx = getFile8Bit(file);
2440 level->fieldy = getFile8Bit(file);
2442 level->time = getFile16BitBE(file);
2443 level->gems_needed = getFile16BitBE(file);
2445 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2446 level->name[i] = getFile8Bit(file);
2447 level->name[MAX_LEVEL_NAME_LEN] = 0;
2449 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
2450 level->score[i] = getFile8Bit(file);
2452 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2453 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
2454 for (y = 0; y < 3; y++)
2455 for (x = 0; x < 3; x++)
2456 level->yamyam_content[i].e[x][y] = getMappedElement(getFile8Bit(file));
2458 level->amoeba_speed = getFile8Bit(file);
2459 level->time_magic_wall = getFile8Bit(file);
2460 level->time_wheel = getFile8Bit(file);
2461 level->amoeba_content = getMappedElement(getFile8Bit(file));
2463 initial_player_stepsize = (getFile8Bit(file) == 1 ? STEPSIZE_FAST :
2466 for (i = 0; i < MAX_PLAYERS; i++)
2467 level->initial_player_stepsize[i] = initial_player_stepsize;
2469 initial_player_gravity = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2471 for (i = 0; i < MAX_PLAYERS; i++)
2472 level->initial_player_gravity[i] = initial_player_gravity;
2474 level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2475 level->em_slippery_gems = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2477 level->use_custom_template = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2479 level->block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2480 level->sp_block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2481 level->can_move_into_acid_bits = getFile32BitBE(file);
2482 level->dont_collide_with_bits = getFile8Bit(file);
2484 level->use_spring_bug = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2485 level->use_step_counter = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2487 level->instant_relocation = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2488 level->can_pass_to_walkable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2489 level->grow_into_diggable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2491 level->game_engine_type = getFile8Bit(file);
2493 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_HEAD_UNUSED);
2498 static int LoadLevel_NAME(File *file, int chunk_size, struct LevelInfo *level)
2502 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2503 level->name[i] = getFile8Bit(file);
2504 level->name[MAX_LEVEL_NAME_LEN] = 0;
2509 static int LoadLevel_AUTH(File *file, int chunk_size, struct LevelInfo *level)
2513 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
2514 level->author[i] = getFile8Bit(file);
2515 level->author[MAX_LEVEL_AUTHOR_LEN] = 0;
2520 static int LoadLevel_BODY(File *file, int chunk_size, struct LevelInfo *level)
2523 int chunk_size_expected = level->fieldx * level->fieldy;
2525 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2526 stored with 16-bit encoding (and should be twice as big then).
2527 Even worse, playfield data was stored 16-bit when only yamyam content
2528 contained 16-bit elements and vice versa. */
2530 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2531 chunk_size_expected *= 2;
2533 if (chunk_size_expected != chunk_size)
2535 ReadUnusedBytesFromFile(file, chunk_size);
2536 return chunk_size_expected;
2539 for (y = 0; y < level->fieldy; y++)
2540 for (x = 0; x < level->fieldx; x++)
2541 level->field[x][y] =
2542 getMappedElement(level->encoding_16bit_field ? getFile16BitBE(file) :
2547 static int LoadLevel_CONT(File *file, int chunk_size, struct LevelInfo *level)
2550 int header_size = 4;
2551 int content_size = MAX_ELEMENT_CONTENTS * 3 * 3;
2552 int chunk_size_expected = header_size + content_size;
2554 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2555 stored with 16-bit encoding (and should be twice as big then).
2556 Even worse, playfield data was stored 16-bit when only yamyam content
2557 contained 16-bit elements and vice versa. */
2559 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2560 chunk_size_expected += content_size;
2562 if (chunk_size_expected != chunk_size)
2564 ReadUnusedBytesFromFile(file, chunk_size);
2565 return chunk_size_expected;
2569 level->num_yamyam_contents = getFile8Bit(file);
2573 // correct invalid number of content fields -- should never happen
2574 if (level->num_yamyam_contents < 1 ||
2575 level->num_yamyam_contents > MAX_ELEMENT_CONTENTS)
2576 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2578 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2579 for (y = 0; y < 3; y++)
2580 for (x = 0; x < 3; x++)
2581 level->yamyam_content[i].e[x][y] =
2582 getMappedElement(level->encoding_16bit_field ?
2583 getFile16BitBE(file) : getFile8Bit(file));
2587 static int LoadLevel_CNT2(File *file, int chunk_size, struct LevelInfo *level)
2592 int content_array[MAX_ELEMENT_CONTENTS][3][3];
2594 element = getMappedElement(getFile16BitBE(file));
2595 num_contents = getFile8Bit(file);
2597 getFile8Bit(file); // content x size (unused)
2598 getFile8Bit(file); // content y size (unused)
2600 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT2_UNUSED);
2602 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2603 for (y = 0; y < 3; y++)
2604 for (x = 0; x < 3; x++)
2605 content_array[i][x][y] = getMappedElement(getFile16BitBE(file));
2607 // correct invalid number of content fields -- should never happen
2608 if (num_contents < 1 || num_contents > MAX_ELEMENT_CONTENTS)
2609 num_contents = STD_ELEMENT_CONTENTS;
2611 if (element == EL_YAMYAM)
2613 level->num_yamyam_contents = num_contents;
2615 for (i = 0; i < num_contents; i++)
2616 for (y = 0; y < 3; y++)
2617 for (x = 0; x < 3; x++)
2618 level->yamyam_content[i].e[x][y] = content_array[i][x][y];
2620 else if (element == EL_BD_AMOEBA)
2622 level->amoeba_content = content_array[0][0][0];
2626 Error(ERR_WARN, "cannot load content for element '%d'", element);
2632 static int LoadLevel_CNT3(File *file, int chunk_size, struct LevelInfo *level)
2638 int chunk_size_expected;
2640 element = getMappedElement(getFile16BitBE(file));
2641 if (!IS_ENVELOPE(element))
2642 element = EL_ENVELOPE_1;
2644 envelope_nr = element - EL_ENVELOPE_1;
2646 envelope_len = getFile16BitBE(file);
2648 level->envelope[envelope_nr].xsize = getFile8Bit(file);
2649 level->envelope[envelope_nr].ysize = getFile8Bit(file);
2651 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT3_UNUSED);
2653 chunk_size_expected = LEVEL_CHUNK_CNT3_SIZE(envelope_len);
2654 if (chunk_size_expected != chunk_size)
2656 ReadUnusedBytesFromFile(file, chunk_size - LEVEL_CHUNK_CNT3_HEADER);
2657 return chunk_size_expected;
2660 for (i = 0; i < envelope_len; i++)
2661 level->envelope[envelope_nr].text[i] = getFile8Bit(file);
2666 static int LoadLevel_CUS1(File *file, int chunk_size, struct LevelInfo *level)
2668 int num_changed_custom_elements = getFile16BitBE(file);
2669 int chunk_size_expected = 2 + num_changed_custom_elements * 6;
2672 if (chunk_size_expected != chunk_size)
2674 ReadUnusedBytesFromFile(file, chunk_size - 2);
2675 return chunk_size_expected;
2678 for (i = 0; i < num_changed_custom_elements; i++)
2680 int element = getMappedElement(getFile16BitBE(file));
2681 int properties = getFile32BitBE(file);
2683 if (IS_CUSTOM_ELEMENT(element))
2684 element_info[element].properties[EP_BITFIELD_BASE_NR] = properties;
2686 Error(ERR_WARN, "invalid custom element number %d", element);
2688 // older game versions that wrote level files with CUS1 chunks used
2689 // different default push delay values (not yet stored in level file)
2690 element_info[element].push_delay_fixed = 2;
2691 element_info[element].push_delay_random = 8;
2694 level->file_has_custom_elements = TRUE;
2699 static int LoadLevel_CUS2(File *file, int chunk_size, struct LevelInfo *level)
2701 int num_changed_custom_elements = getFile16BitBE(file);
2702 int chunk_size_expected = 2 + num_changed_custom_elements * 4;
2705 if (chunk_size_expected != chunk_size)
2707 ReadUnusedBytesFromFile(file, chunk_size - 2);
2708 return chunk_size_expected;
2711 for (i = 0; i < num_changed_custom_elements; i++)
2713 int element = getMappedElement(getFile16BitBE(file));
2714 int custom_target_element = getMappedElement(getFile16BitBE(file));
2716 if (IS_CUSTOM_ELEMENT(element))
2717 element_info[element].change->target_element = custom_target_element;
2719 Error(ERR_WARN, "invalid custom element number %d", element);
2722 level->file_has_custom_elements = TRUE;
2727 static int LoadLevel_CUS3(File *file, int chunk_size, struct LevelInfo *level)
2729 int num_changed_custom_elements = getFile16BitBE(file);
2730 int chunk_size_expected = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
2733 if (chunk_size_expected != chunk_size)
2735 ReadUnusedBytesFromFile(file, chunk_size - 2);
2736 return chunk_size_expected;
2739 for (i = 0; i < num_changed_custom_elements; i++)
2741 int element = getMappedElement(getFile16BitBE(file));
2742 struct ElementInfo *ei = &element_info[element];
2743 unsigned int event_bits;
2745 if (!IS_CUSTOM_ELEMENT(element))
2747 Error(ERR_WARN, "invalid custom element number %d", element);
2749 element = EL_INTERNAL_DUMMY;
2752 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
2753 ei->description[j] = getFile8Bit(file);
2754 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2756 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2758 // some free bytes for future properties and padding
2759 ReadUnusedBytesFromFile(file, 7);
2761 ei->use_gfx_element = getFile8Bit(file);
2762 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2764 ei->collect_score_initial = getFile8Bit(file);
2765 ei->collect_count_initial = getFile8Bit(file);
2767 ei->push_delay_fixed = getFile16BitBE(file);
2768 ei->push_delay_random = getFile16BitBE(file);
2769 ei->move_delay_fixed = getFile16BitBE(file);
2770 ei->move_delay_random = getFile16BitBE(file);
2772 ei->move_pattern = getFile16BitBE(file);
2773 ei->move_direction_initial = getFile8Bit(file);
2774 ei->move_stepsize = getFile8Bit(file);
2776 for (y = 0; y < 3; y++)
2777 for (x = 0; x < 3; x++)
2778 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2780 event_bits = getFile32BitBE(file);
2781 for (j = 0; j < NUM_CHANGE_EVENTS; j++)
2782 if (event_bits & (1 << j))
2783 ei->change->has_event[j] = TRUE;
2785 ei->change->target_element = getMappedElement(getFile16BitBE(file));
2787 ei->change->delay_fixed = getFile16BitBE(file);
2788 ei->change->delay_random = getFile16BitBE(file);
2789 ei->change->delay_frames = getFile16BitBE(file);
2791 ei->change->initial_trigger_element= getMappedElement(getFile16BitBE(file));
2793 ei->change->explode = getFile8Bit(file);
2794 ei->change->use_target_content = getFile8Bit(file);
2795 ei->change->only_if_complete = getFile8Bit(file);
2796 ei->change->use_random_replace = getFile8Bit(file);
2798 ei->change->random_percentage = getFile8Bit(file);
2799 ei->change->replace_when = getFile8Bit(file);
2801 for (y = 0; y < 3; y++)
2802 for (x = 0; x < 3; x++)
2803 ei->change->target_content.e[x][y] =
2804 getMappedElement(getFile16BitBE(file));
2806 ei->slippery_type = getFile8Bit(file);
2808 // some free bytes for future properties and padding
2809 ReadUnusedBytesFromFile(file, LEVEL_CPART_CUS3_UNUSED);
2811 // mark that this custom element has been modified
2812 ei->modified_settings = TRUE;
2815 level->file_has_custom_elements = TRUE;
2820 static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level)
2822 struct ElementInfo *ei;
2823 int chunk_size_expected;
2827 // ---------- custom element base property values (96 bytes) ----------------
2829 element = getMappedElement(getFile16BitBE(file));
2831 if (!IS_CUSTOM_ELEMENT(element))
2833 Error(ERR_WARN, "invalid custom element number %d", element);
2835 ReadUnusedBytesFromFile(file, chunk_size - 2);
2839 ei = &element_info[element];
2841 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
2842 ei->description[i] = getFile8Bit(file);
2843 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2845 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2847 ReadUnusedBytesFromFile(file, 4); // reserved for more base properties
2849 ei->num_change_pages = getFile8Bit(file);
2851 chunk_size_expected = LEVEL_CHUNK_CUS4_SIZE(ei->num_change_pages);
2852 if (chunk_size_expected != chunk_size)
2854 ReadUnusedBytesFromFile(file, chunk_size - 43);
2855 return chunk_size_expected;
2858 ei->ce_value_fixed_initial = getFile16BitBE(file);
2859 ei->ce_value_random_initial = getFile16BitBE(file);
2860 ei->use_last_ce_value = getFile8Bit(file);
2862 ei->use_gfx_element = getFile8Bit(file);
2863 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2865 ei->collect_score_initial = getFile8Bit(file);
2866 ei->collect_count_initial = getFile8Bit(file);
2868 ei->drop_delay_fixed = getFile8Bit(file);
2869 ei->push_delay_fixed = getFile8Bit(file);
2870 ei->drop_delay_random = getFile8Bit(file);
2871 ei->push_delay_random = getFile8Bit(file);
2872 ei->move_delay_fixed = getFile16BitBE(file);
2873 ei->move_delay_random = getFile16BitBE(file);
2875 // bits 0 - 15 of "move_pattern" ...
2876 ei->move_pattern = getFile16BitBE(file);
2877 ei->move_direction_initial = getFile8Bit(file);
2878 ei->move_stepsize = getFile8Bit(file);
2880 ei->slippery_type = getFile8Bit(file);
2882 for (y = 0; y < 3; y++)
2883 for (x = 0; x < 3; x++)
2884 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2886 ei->move_enter_element = getMappedElement(getFile16BitBE(file));
2887 ei->move_leave_element = getMappedElement(getFile16BitBE(file));
2888 ei->move_leave_type = getFile8Bit(file);
2890 // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
2891 ei->move_pattern |= (getFile16BitBE(file) << 16);
2893 ei->access_direction = getFile8Bit(file);
2895 ei->explosion_delay = getFile8Bit(file);
2896 ei->ignition_delay = getFile8Bit(file);
2897 ei->explosion_type = getFile8Bit(file);
2899 // some free bytes for future custom property values and padding
2900 ReadUnusedBytesFromFile(file, 1);
2902 // ---------- change page property values (48 bytes) ------------------------
2904 setElementChangePages(ei, ei->num_change_pages);
2906 for (i = 0; i < ei->num_change_pages; i++)
2908 struct ElementChangeInfo *change = &ei->change_page[i];
2909 unsigned int event_bits;
2911 // always start with reliable default values
2912 setElementChangeInfoToDefaults(change);
2914 // bits 0 - 31 of "has_event[]" ...
2915 event_bits = getFile32BitBE(file);
2916 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
2917 if (event_bits & (1 << j))
2918 change->has_event[j] = TRUE;
2920 change->target_element = getMappedElement(getFile16BitBE(file));
2922 change->delay_fixed = getFile16BitBE(file);
2923 change->delay_random = getFile16BitBE(file);
2924 change->delay_frames = getFile16BitBE(file);
2926 change->initial_trigger_element = getMappedElement(getFile16BitBE(file));
2928 change->explode = getFile8Bit(file);
2929 change->use_target_content = getFile8Bit(file);
2930 change->only_if_complete = getFile8Bit(file);
2931 change->use_random_replace = getFile8Bit(file);
2933 change->random_percentage = getFile8Bit(file);
2934 change->replace_when = getFile8Bit(file);
2936 for (y = 0; y < 3; y++)
2937 for (x = 0; x < 3; x++)
2938 change->target_content.e[x][y]= getMappedElement(getFile16BitBE(file));
2940 change->can_change = getFile8Bit(file);
2942 change->trigger_side = getFile8Bit(file);
2944 change->trigger_player = getFile8Bit(file);
2945 change->trigger_page = getFile8Bit(file);
2947 change->trigger_page = (change->trigger_page == CH_PAGE_ANY_FILE ?
2948 CH_PAGE_ANY : (1 << change->trigger_page));
2950 change->has_action = getFile8Bit(file);
2951 change->action_type = getFile8Bit(file);
2952 change->action_mode = getFile8Bit(file);
2953 change->action_arg = getFile16BitBE(file);
2955 // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
2956 event_bits = getFile8Bit(file);
2957 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
2958 if (event_bits & (1 << (j - 32)))
2959 change->has_event[j] = TRUE;
2962 // mark this custom element as modified
2963 ei->modified_settings = TRUE;
2965 level->file_has_custom_elements = TRUE;
2970 static int LoadLevel_GRP1(File *file, int chunk_size, struct LevelInfo *level)
2972 struct ElementInfo *ei;
2973 struct ElementGroupInfo *group;
2977 element = getMappedElement(getFile16BitBE(file));
2979 if (!IS_GROUP_ELEMENT(element))
2981 Error(ERR_WARN, "invalid group element number %d", element);
2983 ReadUnusedBytesFromFile(file, chunk_size - 2);
2987 ei = &element_info[element];
2989 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
2990 ei->description[i] = getFile8Bit(file);
2991 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2993 group = element_info[element].group;
2995 group->num_elements = getFile8Bit(file);
2997 ei->use_gfx_element = getFile8Bit(file);
2998 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
3000 group->choice_mode = getFile8Bit(file);
3002 // some free bytes for future values and padding
3003 ReadUnusedBytesFromFile(file, 3);
3005 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
3006 group->element[i] = getMappedElement(getFile16BitBE(file));
3008 // mark this group element as modified
3009 element_info[element].modified_settings = TRUE;
3011 level->file_has_custom_elements = TRUE;
3016 static int LoadLevel_MicroChunk(File *file, struct LevelFileConfigInfo *conf,
3017 int element, int real_element)
3019 int micro_chunk_size = 0;
3020 int conf_type = getFile8Bit(file);
3021 int byte_mask = conf_type & CONF_MASK_BYTES;
3022 boolean element_found = FALSE;
3025 micro_chunk_size += 1;
3027 if (byte_mask == CONF_MASK_MULTI_BYTES)
3029 int num_bytes = getFile16BitBE(file);
3030 byte *buffer = checked_malloc(num_bytes);
3032 ReadBytesFromFile(file, buffer, num_bytes);
3034 for (i = 0; conf[i].data_type != -1; i++)
3036 if (conf[i].element == element &&
3037 conf[i].conf_type == conf_type)
3039 int data_type = conf[i].data_type;
3040 int num_entities = num_bytes / CONF_ENTITY_NUM_BYTES(data_type);
3041 int max_num_entities = conf[i].max_num_entities;
3043 if (num_entities > max_num_entities)
3046 "truncating number of entities for element %d from %d to %d",
3047 element, num_entities, max_num_entities);
3049 num_entities = max_num_entities;
3052 if (num_entities == 0 && (data_type == TYPE_ELEMENT_LIST ||
3053 data_type == TYPE_CONTENT_LIST))
3055 // for element and content lists, zero entities are not allowed
3056 Error(ERR_WARN, "found empty list of entities for element %d",
3059 // do not set "num_entities" here to prevent reading behind buffer
3061 *(int *)(conf[i].num_entities) = 1; // at least one is required
3065 *(int *)(conf[i].num_entities) = num_entities;
3068 element_found = TRUE;
3070 if (data_type == TYPE_STRING)
3072 char *string = (char *)(conf[i].value);
3075 for (j = 0; j < max_num_entities; j++)
3076 string[j] = (j < num_entities ? buffer[j] : '\0');
3078 else if (data_type == TYPE_ELEMENT_LIST)
3080 int *element_array = (int *)(conf[i].value);
3083 for (j = 0; j < num_entities; j++)
3085 getMappedElement(CONF_ELEMENTS_ELEMENT(buffer, j));
3087 else if (data_type == TYPE_CONTENT_LIST)
3089 struct Content *content= (struct Content *)(conf[i].value);
3092 for (c = 0; c < num_entities; c++)
3093 for (y = 0; y < 3; y++)
3094 for (x = 0; x < 3; x++)
3095 content[c].e[x][y] =
3096 getMappedElement(CONF_CONTENTS_ELEMENT(buffer, c, x, y));
3099 element_found = FALSE;
3105 checked_free(buffer);
3107 micro_chunk_size += 2 + num_bytes;
3109 else // constant size configuration data (1, 2 or 4 bytes)
3111 int value = (byte_mask == CONF_MASK_1_BYTE ? getFile8Bit (file) :
3112 byte_mask == CONF_MASK_2_BYTE ? getFile16BitBE(file) :
3113 byte_mask == CONF_MASK_4_BYTE ? getFile32BitBE(file) : 0);
3115 for (i = 0; conf[i].data_type != -1; i++)
3117 if (conf[i].element == element &&
3118 conf[i].conf_type == conf_type)
3120 int data_type = conf[i].data_type;
3122 if (data_type == TYPE_ELEMENT)
3123 value = getMappedElement(value);
3125 if (data_type == TYPE_BOOLEAN)
3126 *(boolean *)(conf[i].value) = value;
3128 *(int *) (conf[i].value) = value;
3130 element_found = TRUE;
3136 micro_chunk_size += CONF_VALUE_NUM_BYTES(byte_mask);
3141 char *error_conf_chunk_bytes =
3142 (byte_mask == CONF_MASK_1_BYTE ? "CONF_VALUE_8_BIT" :
3143 byte_mask == CONF_MASK_2_BYTE ? "CONF_VALUE_16_BIT" :
3144 byte_mask == CONF_MASK_4_BYTE ? "CONF_VALUE_32_BIT" :"CONF_VALUE_BYTES");
3145 int error_conf_chunk_token = conf_type & CONF_MASK_TOKEN;
3146 int error_element = real_element;
3148 Error(ERR_WARN, "cannot load micro chunk '%s(%d)' value for element %d ['%s']",
3149 error_conf_chunk_bytes, error_conf_chunk_token,
3150 error_element, EL_NAME(error_element));
3153 return micro_chunk_size;
3156 static int LoadLevel_INFO(File *file, int chunk_size, struct LevelInfo *level)
3158 int real_chunk_size = 0;
3160 li = *level; // copy level data into temporary buffer
3162 while (!checkEndOfFile(file))
3164 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1);
3166 if (real_chunk_size >= chunk_size)
3170 *level = li; // copy temporary buffer back to level data
3172 return real_chunk_size;
3175 static int LoadLevel_CONF(File *file, int chunk_size, struct LevelInfo *level)
3177 int real_chunk_size = 0;
3179 li = *level; // copy level data into temporary buffer
3181 while (!checkEndOfFile(file))
3183 int element = getMappedElement(getFile16BitBE(file));
3185 real_chunk_size += 2;
3186 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CONF,
3188 if (real_chunk_size >= chunk_size)
3192 *level = li; // copy temporary buffer back to level data
3194 return real_chunk_size;
3197 static int LoadLevel_ELEM(File *file, int chunk_size, struct LevelInfo *level)
3199 int real_chunk_size = 0;
3201 li = *level; // copy level data into temporary buffer
3203 while (!checkEndOfFile(file))
3205 int element = getMappedElement(getFile16BitBE(file));
3207 real_chunk_size += 2;
3208 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_ELEM,
3210 if (real_chunk_size >= chunk_size)
3214 *level = li; // copy temporary buffer back to level data
3216 return real_chunk_size;
3219 static int LoadLevel_NOTE(File *file, int chunk_size, struct LevelInfo *level)
3221 int element = getMappedElement(getFile16BitBE(file));
3222 int envelope_nr = element - EL_ENVELOPE_1;
3223 int real_chunk_size = 2;
3225 xx_envelope = level->envelope[envelope_nr]; // copy into temporary buffer
3227 while (!checkEndOfFile(file))
3229 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE,
3232 if (real_chunk_size >= chunk_size)
3236 level->envelope[envelope_nr] = xx_envelope; // copy from temporary buffer
3238 return real_chunk_size;
3241 static int LoadLevel_CUSX(File *file, int chunk_size, struct LevelInfo *level)
3243 int element = getMappedElement(getFile16BitBE(file));
3244 int real_chunk_size = 2;
3245 struct ElementInfo *ei = &element_info[element];
3248 xx_ei = *ei; // copy element data into temporary buffer
3250 xx_ei.num_change_pages = -1;
3252 while (!checkEndOfFile(file))
3254 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base,
3256 if (xx_ei.num_change_pages != -1)
3259 if (real_chunk_size >= chunk_size)
3265 if (ei->num_change_pages == -1)
3267 Error(ERR_WARN, "LoadLevel_CUSX(): missing 'num_change_pages' for '%s'",
3270 ei->num_change_pages = 1;
3272 setElementChangePages(ei, 1);
3273 setElementChangeInfoToDefaults(ei->change);
3275 return real_chunk_size;
3278 // initialize number of change pages stored for this custom element
3279 setElementChangePages(ei, ei->num_change_pages);
3280 for (i = 0; i < ei->num_change_pages; i++)
3281 setElementChangeInfoToDefaults(&ei->change_page[i]);
3283 // start with reading properties for the first change page
3284 xx_current_change_page = 0;
3286 while (!checkEndOfFile(file))
3288 struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page];
3290 xx_change = *change; // copy change data into temporary buffer
3292 resetEventBits(); // reset bits; change page might have changed
3294 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_change,
3297 *change = xx_change;
3299 setEventFlagsFromEventBits(change);
3301 if (real_chunk_size >= chunk_size)
3305 level->file_has_custom_elements = TRUE;
3307 return real_chunk_size;
3310 static int LoadLevel_GRPX(File *file, int chunk_size, struct LevelInfo *level)
3312 int element = getMappedElement(getFile16BitBE(file));
3313 int real_chunk_size = 2;
3314 struct ElementInfo *ei = &element_info[element];
3315 struct ElementGroupInfo *group = ei->group;
3317 xx_ei = *ei; // copy element data into temporary buffer
3318 xx_group = *group; // copy group data into temporary buffer
3320 while (!checkEndOfFile(file))
3322 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX,
3325 if (real_chunk_size >= chunk_size)
3332 level->file_has_custom_elements = TRUE;
3334 return real_chunk_size;
3337 static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
3338 struct LevelFileInfo *level_file_info,
3339 boolean level_info_only)
3341 char *filename = level_file_info->filename;
3342 char cookie[MAX_LINE_LEN];
3343 char chunk_name[CHUNK_ID_LEN + 1];
3347 if (!(file = openFile(filename, MODE_READ)))
3349 level->no_valid_file = TRUE;
3350 level->no_level_file = TRUE;
3352 if (level_info_only)
3355 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
3357 if (!setup.editor.use_template_for_new_levels)
3360 // if level file not found, try to initialize level data from template
3361 filename = getGlobalLevelTemplateFilename();
3363 if (!(file = openFile(filename, MODE_READ)))
3366 // default: for empty levels, use level template for custom elements
3367 level->use_custom_template = TRUE;
3369 level->no_valid_file = FALSE;
3372 getFileChunkBE(file, chunk_name, NULL);
3373 if (strEqual(chunk_name, "RND1"))
3375 getFile32BitBE(file); // not used
3377 getFileChunkBE(file, chunk_name, NULL);
3378 if (!strEqual(chunk_name, "CAVE"))
3380 level->no_valid_file = TRUE;
3382 Error(ERR_WARN, "unknown format of level file '%s'", filename);
3389 else // check for pre-2.0 file format with cookie string
3391 strcpy(cookie, chunk_name);
3392 if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
3394 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
3395 cookie[strlen(cookie) - 1] = '\0';
3397 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
3399 level->no_valid_file = TRUE;
3401 Error(ERR_WARN, "unknown format of level file '%s'", filename);
3408 if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1)
3410 level->no_valid_file = TRUE;
3412 Error(ERR_WARN, "unsupported version of level file '%s'", filename);
3419 // pre-2.0 level files have no game version, so use file version here
3420 level->game_version = level->file_version;
3423 if (level->file_version < FILE_VERSION_1_2)
3425 // level files from versions before 1.2.0 without chunk structure
3426 LoadLevel_HEAD(file, LEVEL_CHUNK_HEAD_SIZE, level);
3427 LoadLevel_BODY(file, level->fieldx * level->fieldy, level);
3435 int (*loader)(File *, int, struct LevelInfo *);
3439 { "VERS", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS },
3440 { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE },
3441 { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD },
3442 { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME },
3443 { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH },
3444 { "INFO", -1, LoadLevel_INFO },
3445 { "BODY", -1, LoadLevel_BODY },
3446 { "CONT", -1, LoadLevel_CONT },
3447 { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
3448 { "CNT3", -1, LoadLevel_CNT3 },
3449 { "CUS1", -1, LoadLevel_CUS1 },
3450 { "CUS2", -1, LoadLevel_CUS2 },
3451 { "CUS3", -1, LoadLevel_CUS3 },
3452 { "CUS4", -1, LoadLevel_CUS4 },
3453 { "GRP1", -1, LoadLevel_GRP1 },
3454 { "CONF", -1, LoadLevel_CONF },
3455 { "ELEM", -1, LoadLevel_ELEM },
3456 { "NOTE", -1, LoadLevel_NOTE },
3457 { "CUSX", -1, LoadLevel_CUSX },
3458 { "GRPX", -1, LoadLevel_GRPX },
3463 while (getFileChunkBE(file, chunk_name, &chunk_size))
3467 while (chunk_info[i].name != NULL &&
3468 !strEqual(chunk_name, chunk_info[i].name))
3471 if (chunk_info[i].name == NULL)
3473 Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
3474 chunk_name, filename);
3475 ReadUnusedBytesFromFile(file, chunk_size);
3477 else if (chunk_info[i].size != -1 &&
3478 chunk_info[i].size != chunk_size)
3480 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
3481 chunk_size, chunk_name, filename);
3482 ReadUnusedBytesFromFile(file, chunk_size);
3486 // call function to load this level chunk
3487 int chunk_size_expected =
3488 (chunk_info[i].loader)(file, chunk_size, level);
3490 // the size of some chunks cannot be checked before reading other
3491 // chunks first (like "HEAD" and "BODY") that contain some header
3492 // information, so check them here
3493 if (chunk_size_expected != chunk_size)
3495 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
3496 chunk_size, chunk_name, filename);
3506 // ----------------------------------------------------------------------------
3507 // functions for loading EM level
3508 // ----------------------------------------------------------------------------
3510 static void CopyNativeLevel_RND_to_EM(struct LevelInfo *level)
3512 static int ball_xy[8][2] =
3523 struct LevelInfo_EM *level_em = level->native_em_level;
3524 struct CAVE *cav = level_em->cav;
3527 cav->width = MIN(level->fieldx, MAX_PLAYFIELD_WIDTH);
3528 cav->height = MIN(level->fieldy, MAX_PLAYFIELD_HEIGHT);
3530 cav->time_seconds = level->time;
3531 cav->gems_needed = level->gems_needed;
3533 cav->emerald_score = level->score[SC_EMERALD];
3534 cav->diamond_score = level->score[SC_DIAMOND];
3535 cav->alien_score = level->score[SC_ROBOT];
3536 cav->tank_score = level->score[SC_SPACESHIP];
3537 cav->bug_score = level->score[SC_BUG];
3538 cav->eater_score = level->score[SC_YAMYAM];
3539 cav->nut_score = level->score[SC_NUT];
3540 cav->dynamite_score = level->score[SC_DYNAMITE];
3541 cav->key_score = level->score[SC_KEY];
3542 cav->exit_score = level->score[SC_TIME_BONUS];
3544 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3545 for (y = 0; y < 3; y++)
3546 for (x = 0; x < 3; x++)
3547 cav->eater_array[i][y * 3 + x] =
3548 map_element_RND_to_EM_cave(level->yamyam_content[i].e[x][y]);
3550 cav->amoeba_time = level->amoeba_speed;
3551 cav->wonderwall_time = level->time_magic_wall;
3552 cav->wheel_time = level->time_wheel;
3554 cav->android_move_time = level->android_move_time;
3555 cav->android_clone_time = level->android_clone_time;
3556 cav->ball_random = level->ball_random;
3557 cav->ball_active = level->ball_active_initial;
3558 cav->ball_time = level->ball_time;
3559 cav->num_ball_arrays = level->num_ball_contents;
3561 cav->lenses_score = level->lenses_score;
3562 cav->magnify_score = level->magnify_score;
3563 cav->slurp_score = level->slurp_score;
3565 cav->lenses_time = level->lenses_time;
3566 cav->magnify_time = level->magnify_time;
3568 cav->wind_direction =
3569 map_direction_RND_to_EM(level->wind_direction_initial);
3571 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3572 for (j = 0; j < 8; j++)
3573 cav->ball_array[i][j] =
3574 map_element_RND_to_EM_cave(level->ball_content[i].
3575 e[ball_xy[j][0]][ball_xy[j][1]]);
3577 map_android_clone_elements_RND_to_EM(level);
3579 // first fill the complete playfield with the empty space element
3580 for (y = 0; y < EM_MAX_CAVE_HEIGHT; y++)
3581 for (x = 0; x < EM_MAX_CAVE_WIDTH; x++)
3582 cav->cave[x][y] = Cblank;
3584 // then copy the real level contents from level file into the playfield
3585 for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
3587 int new_element = map_element_RND_to_EM_cave(level->field[x][y]);
3589 if (level->field[x][y] == EL_AMOEBA_DEAD)
3590 new_element = map_element_RND_to_EM_cave(EL_AMOEBA_WET);
3592 cav->cave[x][y] = new_element;
3595 for (i = 0; i < MAX_PLAYERS; i++)
3597 cav->player_x[i] = -1;
3598 cav->player_y[i] = -1;
3601 // initialize player positions and delete players from the playfield
3602 for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
3604 if (ELEM_IS_PLAYER(level->field[x][y]))
3606 int player_nr = GET_PLAYER_NR(level->field[x][y]);
3608 cav->player_x[player_nr] = x;
3609 cav->player_y[player_nr] = y;
3611 cav->cave[x][y] = map_element_RND_to_EM_cave(EL_EMPTY);
3616 static void CopyNativeLevel_EM_to_RND(struct LevelInfo *level)
3618 static int ball_xy[8][2] =
3629 struct LevelInfo_EM *level_em = level->native_em_level;
3630 struct CAVE *cav = level_em->cav;
3633 level->fieldx = MIN(cav->width, MAX_LEV_FIELDX);
3634 level->fieldy = MIN(cav->height, MAX_LEV_FIELDY);
3636 level->time = cav->time_seconds;
3637 level->gems_needed = cav->gems_needed;
3639 sprintf(level->name, "Level %d", level->file_info.nr);
3641 level->score[SC_EMERALD] = cav->emerald_score;
3642 level->score[SC_DIAMOND] = cav->diamond_score;
3643 level->score[SC_ROBOT] = cav->alien_score;
3644 level->score[SC_SPACESHIP] = cav->tank_score;
3645 level->score[SC_BUG] = cav->bug_score;
3646 level->score[SC_YAMYAM] = cav->eater_score;
3647 level->score[SC_NUT] = cav->nut_score;
3648 level->score[SC_DYNAMITE] = cav->dynamite_score;
3649 level->score[SC_KEY] = cav->key_score;
3650 level->score[SC_TIME_BONUS] = cav->exit_score;
3652 level->num_yamyam_contents = MAX_ELEMENT_CONTENTS;
3654 for (i = 0; i < level->num_yamyam_contents; i++)
3655 for (y = 0; y < 3; y++)
3656 for (x = 0; x < 3; x++)
3657 level->yamyam_content[i].e[x][y] =
3658 map_element_EM_to_RND_cave(cav->eater_array[i][y * 3 + x]);
3660 level->amoeba_speed = cav->amoeba_time;
3661 level->time_magic_wall = cav->wonderwall_time;
3662 level->time_wheel = cav->wheel_time;
3664 level->android_move_time = cav->android_move_time;
3665 level->android_clone_time = cav->android_clone_time;
3666 level->ball_random = cav->ball_random;
3667 level->ball_active_initial = cav->ball_active;
3668 level->ball_time = cav->ball_time;
3669 level->num_ball_contents = cav->num_ball_arrays;
3671 level->lenses_score = cav->lenses_score;
3672 level->magnify_score = cav->magnify_score;
3673 level->slurp_score = cav->slurp_score;
3675 level->lenses_time = cav->lenses_time;
3676 level->magnify_time = cav->magnify_time;
3678 level->wind_direction_initial =
3679 map_direction_EM_to_RND(cav->wind_direction);
3681 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3682 for (j = 0; j < 8; j++)
3683 level->ball_content[i].e[ball_xy[j][0]][ball_xy[j][1]] =
3684 map_element_EM_to_RND_cave(cav->ball_array[i][j]);
3686 map_android_clone_elements_EM_to_RND(level);
3688 // convert the playfield (some elements need special treatment)
3689 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
3691 int new_element = map_element_EM_to_RND_cave(cav->cave[x][y]);
3693 if (new_element == EL_AMOEBA_WET && level->amoeba_speed == 0)
3694 new_element = EL_AMOEBA_DEAD;
3696 level->field[x][y] = new_element;
3699 for (i = 0; i < MAX_PLAYERS; i++)
3701 // in case of all players set to the same field, use the first player
3702 int nr = MAX_PLAYERS - i - 1;
3703 int jx = cav->player_x[nr];
3704 int jy = cav->player_y[nr];
3706 if (jx != -1 && jy != -1)
3707 level->field[jx][jy] = EL_PLAYER_1 + nr;
3712 // ----------------------------------------------------------------------------
3713 // functions for loading SP level
3714 // ----------------------------------------------------------------------------
3716 static void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
3718 struct LevelInfo_SP *level_sp = level->native_sp_level;
3719 LevelInfoType *header = &level_sp->header;
3722 level_sp->width = level->fieldx;
3723 level_sp->height = level->fieldy;
3725 for (x = 0; x < level->fieldx; x++)
3726 for (y = 0; y < level->fieldy; y++)
3727 level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
3729 header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
3731 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
3732 header->LevelTitle[i] = level->name[i];
3733 // !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!!
3735 header->InfotronsNeeded = level->gems_needed;
3737 header->SpecialPortCount = 0;
3739 for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
3741 boolean gravity_port_found = FALSE;
3742 boolean gravity_port_valid = FALSE;
3743 int gravity_port_flag;
3744 int gravity_port_base_element;
3745 int element = level->field[x][y];
3747 if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
3748 element <= EL_SP_GRAVITY_ON_PORT_UP)
3750 gravity_port_found = TRUE;
3751 gravity_port_valid = TRUE;
3752 gravity_port_flag = 1;
3753 gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
3755 else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
3756 element <= EL_SP_GRAVITY_OFF_PORT_UP)
3758 gravity_port_found = TRUE;
3759 gravity_port_valid = TRUE;
3760 gravity_port_flag = 0;
3761 gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
3763 else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
3764 element <= EL_SP_GRAVITY_PORT_UP)
3766 // change R'n'D style gravity inverting special port to normal port
3767 // (there are no gravity inverting ports in native Supaplex engine)
3769 gravity_port_found = TRUE;
3770 gravity_port_valid = FALSE;
3771 gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
3774 if (gravity_port_found)
3776 if (gravity_port_valid &&
3777 header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
3779 SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
3781 port->PortLocation = (y * level->fieldx + x) * 2;
3782 port->Gravity = gravity_port_flag;
3784 element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
3786 header->SpecialPortCount++;
3790 // change special gravity port to normal port
3792 element += EL_SP_PORT_RIGHT - gravity_port_base_element;
3795 level_sp->playfield[x][y] = element - EL_SP_START;
3800 static void CopyNativeLevel_SP_to_RND(struct LevelInfo *level)
3802 struct LevelInfo_SP *level_sp = level->native_sp_level;
3803 LevelInfoType *header = &level_sp->header;
3804 boolean num_invalid_elements = 0;
3807 level->fieldx = level_sp->width;
3808 level->fieldy = level_sp->height;
3810 for (x = 0; x < level->fieldx; x++)
3812 for (y = 0; y < level->fieldy; y++)
3814 int element_old = level_sp->playfield[x][y];
3815 int element_new = getMappedElement(map_element_SP_to_RND(element_old));
3817 if (element_new == EL_UNKNOWN)
3819 num_invalid_elements++;
3821 Error(ERR_DEBUG, "invalid element %d at position %d, %d",
3825 level->field[x][y] = element_new;
3829 if (num_invalid_elements > 0)
3830 Error(ERR_WARN, "found %d invalid elements%s", num_invalid_elements,
3831 (!options.debug ? " (use '--debug' for more details)" : ""));
3833 for (i = 0; i < MAX_PLAYERS; i++)
3834 level->initial_player_gravity[i] =
3835 (header->InitialGravity == 1 ? TRUE : FALSE);
3837 // skip leading spaces
3838 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
3839 if (header->LevelTitle[i] != ' ')
3843 for (j = 0; i < SP_LEVEL_NAME_LEN; i++, j++)
3844 level->name[j] = header->LevelTitle[i];
3845 level->name[j] = '\0';
3847 // cut trailing spaces
3849 if (level->name[j - 1] == ' ' && level->name[j] == '\0')
3850 level->name[j - 1] = '\0';
3852 level->gems_needed = header->InfotronsNeeded;
3854 for (i = 0; i < header->SpecialPortCount; i++)
3856 SpecialPortType *port = &header->SpecialPort[i];
3857 int port_location = port->PortLocation;
3858 int gravity = port->Gravity;
3859 int port_x, port_y, port_element;
3861 port_x = (port_location / 2) % level->fieldx;
3862 port_y = (port_location / 2) / level->fieldx;
3864 if (port_x < 0 || port_x >= level->fieldx ||
3865 port_y < 0 || port_y >= level->fieldy)
3867 Error(ERR_WARN, "special port position (%d, %d) out of bounds",
3873 port_element = level->field[port_x][port_y];
3875 if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
3876 port_element > EL_SP_GRAVITY_PORT_UP)
3878 Error(ERR_WARN, "no special port at position (%d, %d)", port_x, port_y);
3883 // change previous (wrong) gravity inverting special port to either
3884 // gravity enabling special port or gravity disabling special port
3885 level->field[port_x][port_y] +=
3886 (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
3887 EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
3890 // change special gravity ports without database entries to normal ports
3891 for (x = 0; x < level->fieldx; x++)
3892 for (y = 0; y < level->fieldy; y++)
3893 if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
3894 level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
3895 level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
3897 level->time = 0; // no time limit
3898 level->amoeba_speed = 0;
3899 level->time_magic_wall = 0;
3900 level->time_wheel = 0;
3901 level->amoeba_content = EL_EMPTY;
3904 // original Supaplex does not use score values -- use default values
3906 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
3907 level->score[i] = 0;
3910 // there are no yamyams in supaplex levels
3911 for (i = 0; i < level->num_yamyam_contents; i++)
3912 for (x = 0; x < 3; x++)
3913 for (y = 0; y < 3; y++)
3914 level->yamyam_content[i].e[x][y] = EL_EMPTY;
3917 static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
3919 struct LevelInfo_SP *level_sp = level->native_sp_level;
3920 struct DemoInfo_SP *demo = &level_sp->demo;
3923 // always start with reliable default values
3924 demo->is_available = FALSE;
3927 if (TAPE_IS_EMPTY(tape))
3930 demo->level_nr = tape.level_nr; // (currently not used)
3932 level_sp->header.DemoRandomSeed = tape.random_seed;
3936 for (i = 0; i < tape.length; i++)
3938 int demo_action = map_key_RND_to_SP(tape.pos[i].action[0]);
3939 int demo_repeat = tape.pos[i].delay;
3940 int demo_entries = (demo_repeat + 15) / 16;
3942 if (demo->length + demo_entries >= SP_MAX_TAPE_LEN)
3944 Error(ERR_WARN, "tape truncated: size exceeds maximum SP demo size %d",
3950 for (j = 0; j < demo_repeat / 16; j++)
3951 demo->data[demo->length++] = 0xf0 | demo_action;
3953 if (demo_repeat % 16)
3954 demo->data[demo->length++] = ((demo_repeat % 16 - 1) << 4) | demo_action;
3957 demo->is_available = TRUE;
3960 static void setTapeInfoToDefaults(void);
3962 static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
3964 struct LevelInfo_SP *level_sp = level->native_sp_level;
3965 struct DemoInfo_SP *demo = &level_sp->demo;
3966 char *filename = level->file_info.filename;
3969 // always start with reliable default values
3970 setTapeInfoToDefaults();
3972 if (!demo->is_available)
3975 tape.level_nr = demo->level_nr; // (currently not used)
3976 tape.random_seed = level_sp->header.DemoRandomSeed;
3978 TapeSetDateFromEpochSeconds(getFileTimestampEpochSeconds(filename));
3981 tape.pos[tape.counter].delay = 0;
3983 for (i = 0; i < demo->length; i++)
3985 int demo_action = demo->data[i] & 0x0f;
3986 int demo_repeat = (demo->data[i] & 0xf0) >> 4;
3987 int tape_action = map_key_SP_to_RND(demo_action);
3988 int tape_repeat = demo_repeat + 1;
3989 byte action[MAX_TAPE_ACTIONS] = { tape_action };
3990 boolean success = 0;
3993 for (j = 0; j < tape_repeat; j++)
3994 success = TapeAddAction(action);
3998 Error(ERR_WARN, "SP demo truncated: size exceeds maximum tape size %d",
4005 TapeHaltRecording();
4009 // ----------------------------------------------------------------------------
4010 // functions for loading MM level
4011 // ----------------------------------------------------------------------------
4013 static void CopyNativeLevel_RND_to_MM(struct LevelInfo *level)
4015 struct LevelInfo_MM *level_mm = level->native_mm_level;
4018 level_mm->fieldx = MIN(level->fieldx, MM_MAX_PLAYFIELD_WIDTH);
4019 level_mm->fieldy = MIN(level->fieldy, MM_MAX_PLAYFIELD_HEIGHT);
4021 level_mm->time = level->time;
4022 level_mm->kettles_needed = level->gems_needed;
4023 level_mm->auto_count_kettles = level->auto_count_gems;
4025 level_mm->laser_red = level->mm_laser_red;
4026 level_mm->laser_green = level->mm_laser_green;
4027 level_mm->laser_blue = level->mm_laser_blue;
4029 strcpy(level_mm->name, level->name);
4030 strcpy(level_mm->author, level->author);
4032 level_mm->score[SC_EMERALD] = level->score[SC_EMERALD];
4033 level_mm->score[SC_PACMAN] = level->score[SC_PACMAN];
4034 level_mm->score[SC_KEY] = level->score[SC_KEY];
4035 level_mm->score[SC_TIME_BONUS] = level->score[SC_TIME_BONUS];
4036 level_mm->score[SC_ELEM_BONUS] = level->score[SC_ELEM_BONUS];
4038 level_mm->amoeba_speed = level->amoeba_speed;
4039 level_mm->time_fuse = level->mm_time_fuse;
4040 level_mm->time_bomb = level->mm_time_bomb;
4041 level_mm->time_ball = level->mm_time_ball;
4042 level_mm->time_block = level->mm_time_block;
4044 for (x = 0; x < level->fieldx; x++)
4045 for (y = 0; y < level->fieldy; y++)
4047 level_mm->field[x][y] = map_element_RND_to_MM(level->field[x][y]);
4050 static void CopyNativeLevel_MM_to_RND(struct LevelInfo *level)
4052 struct LevelInfo_MM *level_mm = level->native_mm_level;
4055 level->fieldx = MIN(level_mm->fieldx, MAX_LEV_FIELDX);
4056 level->fieldy = MIN(level_mm->fieldy, MAX_LEV_FIELDY);
4058 level->time = level_mm->time;
4059 level->gems_needed = level_mm->kettles_needed;
4060 level->auto_count_gems = level_mm->auto_count_kettles;
4062 level->mm_laser_red = level_mm->laser_red;
4063 level->mm_laser_green = level_mm->laser_green;
4064 level->mm_laser_blue = level_mm->laser_blue;
4066 strcpy(level->name, level_mm->name);
4068 // only overwrite author from 'levelinfo.conf' if author defined in level
4069 if (!strEqual(level_mm->author, ANONYMOUS_NAME))
4070 strcpy(level->author, level_mm->author);
4072 level->score[SC_EMERALD] = level_mm->score[SC_EMERALD];
4073 level->score[SC_PACMAN] = level_mm->score[SC_PACMAN];
4074 level->score[SC_KEY] = level_mm->score[SC_KEY];
4075 level->score[SC_TIME_BONUS] = level_mm->score[SC_TIME_BONUS];
4076 level->score[SC_ELEM_BONUS] = level_mm->score[SC_ELEM_BONUS];
4078 level->amoeba_speed = level_mm->amoeba_speed;
4079 level->mm_time_fuse = level_mm->time_fuse;
4080 level->mm_time_bomb = level_mm->time_bomb;
4081 level->mm_time_ball = level_mm->time_ball;
4082 level->mm_time_block = level_mm->time_block;
4084 for (x = 0; x < level->fieldx; x++)
4085 for (y = 0; y < level->fieldy; y++)
4086 level->field[x][y] = map_element_MM_to_RND(level_mm->field[x][y]);
4090 // ----------------------------------------------------------------------------
4091 // functions for loading DC level
4092 // ----------------------------------------------------------------------------
4094 #define DC_LEVEL_HEADER_SIZE 344
4096 static unsigned short getDecodedWord_DC(unsigned short data_encoded,
4099 static int last_data_encoded;
4103 int diff_hi, diff_lo;
4104 int data_hi, data_lo;
4105 unsigned short data_decoded;
4109 last_data_encoded = 0;
4116 diff = data_encoded - last_data_encoded;
4117 diff_hi = diff & ~0xff;
4118 diff_lo = diff & 0xff;
4122 data_hi = diff_hi - (offset1 << 8) + (offset2 & 0xff00);
4123 data_lo = (diff_lo + (data_hi >> 16)) & 0x00ff;
4124 data_hi = data_hi & 0xff00;
4126 data_decoded = data_hi | data_lo;
4128 last_data_encoded = data_encoded;
4130 offset1 = (offset1 + 1) % 31;
4131 offset2 = offset2 & 0xff;
4133 return data_decoded;
4136 static int getMappedElement_DC(int element)
4144 // 0x0117 - 0x036e: (?)
4147 // 0x042d - 0x0684: (?)
4163 element = EL_CRYSTAL;
4166 case 0x0e77: // quicksand (boulder)
4167 element = EL_QUICKSAND_FAST_FULL;
4170 case 0x0e99: // slow quicksand (boulder)
4171 element = EL_QUICKSAND_FULL;
4175 element = EL_EM_EXIT_OPEN;
4179 element = EL_EM_EXIT_CLOSED;
4183 element = EL_EM_STEEL_EXIT_OPEN;
4187 element = EL_EM_STEEL_EXIT_CLOSED;
4190 case 0x0f4f: // dynamite (lit 1)
4191 element = EL_EM_DYNAMITE_ACTIVE;
4194 case 0x0f57: // dynamite (lit 2)
4195 element = EL_EM_DYNAMITE_ACTIVE;
4198 case 0x0f5f: // dynamite (lit 3)
4199 element = EL_EM_DYNAMITE_ACTIVE;
4202 case 0x0f67: // dynamite (lit 4)
4203 element = EL_EM_DYNAMITE_ACTIVE;
4210 element = EL_AMOEBA_WET;
4214 element = EL_AMOEBA_DROP;
4218 element = EL_DC_MAGIC_WALL;
4222 element = EL_SPACESHIP_UP;
4226 element = EL_SPACESHIP_DOWN;
4230 element = EL_SPACESHIP_LEFT;
4234 element = EL_SPACESHIP_RIGHT;
4238 element = EL_BUG_UP;
4242 element = EL_BUG_DOWN;
4246 element = EL_BUG_LEFT;
4250 element = EL_BUG_RIGHT;
4254 element = EL_MOLE_UP;
4258 element = EL_MOLE_DOWN;
4262 element = EL_MOLE_LEFT;
4266 element = EL_MOLE_RIGHT;
4274 element = EL_YAMYAM;
4278 element = EL_SWITCHGATE_OPEN;
4282 element = EL_SWITCHGATE_CLOSED;
4286 element = EL_DC_SWITCHGATE_SWITCH_UP;
4290 element = EL_TIMEGATE_CLOSED;
4293 case 0x144c: // conveyor belt switch (green)
4294 element = EL_CONVEYOR_BELT_3_SWITCH_MIDDLE;
4297 case 0x144f: // conveyor belt switch (red)
4298 element = EL_CONVEYOR_BELT_1_SWITCH_MIDDLE;
4301 case 0x1452: // conveyor belt switch (blue)
4302 element = EL_CONVEYOR_BELT_4_SWITCH_MIDDLE;
4306 element = EL_CONVEYOR_BELT_3_MIDDLE;
4310 element = EL_CONVEYOR_BELT_3_LEFT;
4314 element = EL_CONVEYOR_BELT_3_RIGHT;
4318 element = EL_CONVEYOR_BELT_1_MIDDLE;
4322 element = EL_CONVEYOR_BELT_1_LEFT;
4326 element = EL_CONVEYOR_BELT_1_RIGHT;
4330 element = EL_CONVEYOR_BELT_4_MIDDLE;
4334 element = EL_CONVEYOR_BELT_4_LEFT;
4338 element = EL_CONVEYOR_BELT_4_RIGHT;
4342 element = EL_EXPANDABLE_WALL_HORIZONTAL;
4346 element = EL_EXPANDABLE_WALL_VERTICAL;
4350 element = EL_EXPANDABLE_WALL_ANY;
4353 case 0x14ce: // growing steel wall (left/right)
4354 element = EL_EXPANDABLE_STEELWALL_HORIZONTAL;
4357 case 0x14df: // growing steel wall (up/down)
4358 element = EL_EXPANDABLE_STEELWALL_VERTICAL;
4361 case 0x14e8: // growing steel wall (up/down/left/right)
4362 element = EL_EXPANDABLE_STEELWALL_ANY;
4366 element = EL_SHIELD_DEADLY;
4370 element = EL_EXTRA_TIME;
4378 element = EL_EMPTY_SPACE;
4381 case 0x1578: // quicksand (empty)
4382 element = EL_QUICKSAND_FAST_EMPTY;
4385 case 0x1579: // slow quicksand (empty)
4386 element = EL_QUICKSAND_EMPTY;
4396 element = EL_EM_DYNAMITE;
4399 case 0x15a1: // key (red)
4400 element = EL_EM_KEY_1;
4403 case 0x15a2: // key (yellow)
4404 element = EL_EM_KEY_2;
4407 case 0x15a3: // key (blue)
4408 element = EL_EM_KEY_4;
4411 case 0x15a4: // key (green)
4412 element = EL_EM_KEY_3;
4415 case 0x15a5: // key (white)
4416 element = EL_DC_KEY_WHITE;
4420 element = EL_WALL_SLIPPERY;
4427 case 0x15a8: // wall (not round)
4431 case 0x15a9: // (blue)
4432 element = EL_CHAR_A;
4435 case 0x15aa: // (blue)
4436 element = EL_CHAR_B;
4439 case 0x15ab: // (blue)
4440 element = EL_CHAR_C;
4443 case 0x15ac: // (blue)
4444 element = EL_CHAR_D;
4447 case 0x15ad: // (blue)
4448 element = EL_CHAR_E;
4451 case 0x15ae: // (blue)
4452 element = EL_CHAR_F;
4455 case 0x15af: // (blue)
4456 element = EL_CHAR_G;
4459 case 0x15b0: // (blue)
4460 element = EL_CHAR_H;
4463 case 0x15b1: // (blue)
4464 element = EL_CHAR_I;
4467 case 0x15b2: // (blue)
4468 element = EL_CHAR_J;
4471 case 0x15b3: // (blue)
4472 element = EL_CHAR_K;
4475 case 0x15b4: // (blue)
4476 element = EL_CHAR_L;
4479 case 0x15b5: // (blue)
4480 element = EL_CHAR_M;
4483 case 0x15b6: // (blue)
4484 element = EL_CHAR_N;
4487 case 0x15b7: // (blue)
4488 element = EL_CHAR_O;
4491 case 0x15b8: // (blue)
4492 element = EL_CHAR_P;
4495 case 0x15b9: // (blue)
4496 element = EL_CHAR_Q;
4499 case 0x15ba: // (blue)
4500 element = EL_CHAR_R;
4503 case 0x15bb: // (blue)
4504 element = EL_CHAR_S;
4507 case 0x15bc: // (blue)
4508 element = EL_CHAR_T;
4511 case 0x15bd: // (blue)
4512 element = EL_CHAR_U;
4515 case 0x15be: // (blue)
4516 element = EL_CHAR_V;
4519 case 0x15bf: // (blue)
4520 element = EL_CHAR_W;
4523 case 0x15c0: // (blue)
4524 element = EL_CHAR_X;
4527 case 0x15c1: // (blue)
4528 element = EL_CHAR_Y;
4531 case 0x15c2: // (blue)
4532 element = EL_CHAR_Z;
4535 case 0x15c3: // (blue)
4536 element = EL_CHAR_AUMLAUT;
4539 case 0x15c4: // (blue)
4540 element = EL_CHAR_OUMLAUT;
4543 case 0x15c5: // (blue)
4544 element = EL_CHAR_UUMLAUT;
4547 case 0x15c6: // (blue)
4548 element = EL_CHAR_0;
4551 case 0x15c7: // (blue)
4552 element = EL_CHAR_1;
4555 case 0x15c8: // (blue)
4556 element = EL_CHAR_2;
4559 case 0x15c9: // (blue)
4560 element = EL_CHAR_3;
4563 case 0x15ca: // (blue)
4564 element = EL_CHAR_4;
4567 case 0x15cb: // (blue)
4568 element = EL_CHAR_5;
4571 case 0x15cc: // (blue)
4572 element = EL_CHAR_6;
4575 case 0x15cd: // (blue)
4576 element = EL_CHAR_7;
4579 case 0x15ce: // (blue)
4580 element = EL_CHAR_8;
4583 case 0x15cf: // (blue)
4584 element = EL_CHAR_9;
4587 case 0x15d0: // (blue)
4588 element = EL_CHAR_PERIOD;
4591 case 0x15d1: // (blue)
4592 element = EL_CHAR_EXCLAM;
4595 case 0x15d2: // (blue)
4596 element = EL_CHAR_COLON;
4599 case 0x15d3: // (blue)
4600 element = EL_CHAR_LESS;
4603 case 0x15d4: // (blue)
4604 element = EL_CHAR_GREATER;
4607 case 0x15d5: // (blue)
4608 element = EL_CHAR_QUESTION;
4611 case 0x15d6: // (blue)
4612 element = EL_CHAR_COPYRIGHT;
4615 case 0x15d7: // (blue)
4616 element = EL_CHAR_UP;
4619 case 0x15d8: // (blue)
4620 element = EL_CHAR_DOWN;
4623 case 0x15d9: // (blue)
4624 element = EL_CHAR_BUTTON;
4627 case 0x15da: // (blue)
4628 element = EL_CHAR_PLUS;
4631 case 0x15db: // (blue)
4632 element = EL_CHAR_MINUS;
4635 case 0x15dc: // (blue)
4636 element = EL_CHAR_APOSTROPHE;
4639 case 0x15dd: // (blue)
4640 element = EL_CHAR_PARENLEFT;
4643 case 0x15de: // (blue)
4644 element = EL_CHAR_PARENRIGHT;
4647 case 0x15df: // (green)
4648 element = EL_CHAR_A;
4651 case 0x15e0: // (green)
4652 element = EL_CHAR_B;
4655 case 0x15e1: // (green)
4656 element = EL_CHAR_C;
4659 case 0x15e2: // (green)
4660 element = EL_CHAR_D;
4663 case 0x15e3: // (green)
4664 element = EL_CHAR_E;
4667 case 0x15e4: // (green)
4668 element = EL_CHAR_F;
4671 case 0x15e5: // (green)
4672 element = EL_CHAR_G;
4675 case 0x15e6: // (green)
4676 element = EL_CHAR_H;
4679 case 0x15e7: // (green)
4680 element = EL_CHAR_I;
4683 case 0x15e8: // (green)
4684 element = EL_CHAR_J;
4687 case 0x15e9: // (green)
4688 element = EL_CHAR_K;
4691 case 0x15ea: // (green)
4692 element = EL_CHAR_L;
4695 case 0x15eb: // (green)
4696 element = EL_CHAR_M;
4699 case 0x15ec: // (green)
4700 element = EL_CHAR_N;
4703 case 0x15ed: // (green)
4704 element = EL_CHAR_O;
4707 case 0x15ee: // (green)
4708 element = EL_CHAR_P;
4711 case 0x15ef: // (green)
4712 element = EL_CHAR_Q;
4715 case 0x15f0: // (green)
4716 element = EL_CHAR_R;
4719 case 0x15f1: // (green)
4720 element = EL_CHAR_S;
4723 case 0x15f2: // (green)
4724 element = EL_CHAR_T;
4727 case 0x15f3: // (green)
4728 element = EL_CHAR_U;
4731 case 0x15f4: // (green)
4732 element = EL_CHAR_V;
4735 case 0x15f5: // (green)
4736 element = EL_CHAR_W;
4739 case 0x15f6: // (green)
4740 element = EL_CHAR_X;
4743 case 0x15f7: // (green)
4744 element = EL_CHAR_Y;
4747 case 0x15f8: // (green)
4748 element = EL_CHAR_Z;
4751 case 0x15f9: // (green)
4752 element = EL_CHAR_AUMLAUT;
4755 case 0x15fa: // (green)
4756 element = EL_CHAR_OUMLAUT;
4759 case 0x15fb: // (green)
4760 element = EL_CHAR_UUMLAUT;
4763 case 0x15fc: // (green)
4764 element = EL_CHAR_0;
4767 case 0x15fd: // (green)
4768 element = EL_CHAR_1;
4771 case 0x15fe: // (green)
4772 element = EL_CHAR_2;
4775 case 0x15ff: // (green)
4776 element = EL_CHAR_3;
4779 case 0x1600: // (green)
4780 element = EL_CHAR_4;
4783 case 0x1601: // (green)
4784 element = EL_CHAR_5;
4787 case 0x1602: // (green)
4788 element = EL_CHAR_6;
4791 case 0x1603: // (green)
4792 element = EL_CHAR_7;
4795 case 0x1604: // (green)
4796 element = EL_CHAR_8;
4799 case 0x1605: // (green)
4800 element = EL_CHAR_9;
4803 case 0x1606: // (green)
4804 element = EL_CHAR_PERIOD;
4807 case 0x1607: // (green)
4808 element = EL_CHAR_EXCLAM;
4811 case 0x1608: // (green)
4812 element = EL_CHAR_COLON;
4815 case 0x1609: // (green)
4816 element = EL_CHAR_LESS;
4819 case 0x160a: // (green)
4820 element = EL_CHAR_GREATER;
4823 case 0x160b: // (green)
4824 element = EL_CHAR_QUESTION;
4827 case 0x160c: // (green)
4828 element = EL_CHAR_COPYRIGHT;
4831 case 0x160d: // (green)
4832 element = EL_CHAR_UP;
4835 case 0x160e: // (green)
4836 element = EL_CHAR_DOWN;
4839 case 0x160f: // (green)
4840 element = EL_CHAR_BUTTON;
4843 case 0x1610: // (green)
4844 element = EL_CHAR_PLUS;
4847 case 0x1611: // (green)
4848 element = EL_CHAR_MINUS;
4851 case 0x1612: // (green)
4852 element = EL_CHAR_APOSTROPHE;
4855 case 0x1613: // (green)
4856 element = EL_CHAR_PARENLEFT;
4859 case 0x1614: // (green)
4860 element = EL_CHAR_PARENRIGHT;
4863 case 0x1615: // (blue steel)
4864 element = EL_STEEL_CHAR_A;
4867 case 0x1616: // (blue steel)
4868 element = EL_STEEL_CHAR_B;
4871 case 0x1617: // (blue steel)
4872 element = EL_STEEL_CHAR_C;
4875 case 0x1618: // (blue steel)
4876 element = EL_STEEL_CHAR_D;
4879 case 0x1619: // (blue steel)
4880 element = EL_STEEL_CHAR_E;
4883 case 0x161a: // (blue steel)
4884 element = EL_STEEL_CHAR_F;
4887 case 0x161b: // (blue steel)
4888 element = EL_STEEL_CHAR_G;
4891 case 0x161c: // (blue steel)
4892 element = EL_STEEL_CHAR_H;
4895 case 0x161d: // (blue steel)
4896 element = EL_STEEL_CHAR_I;
4899 case 0x161e: // (blue steel)
4900 element = EL_STEEL_CHAR_J;
4903 case 0x161f: // (blue steel)
4904 element = EL_STEEL_CHAR_K;
4907 case 0x1620: // (blue steel)
4908 element = EL_STEEL_CHAR_L;
4911 case 0x1621: // (blue steel)
4912 element = EL_STEEL_CHAR_M;
4915 case 0x1622: // (blue steel)
4916 element = EL_STEEL_CHAR_N;
4919 case 0x1623: // (blue steel)
4920 element = EL_STEEL_CHAR_O;
4923 case 0x1624: // (blue steel)
4924 element = EL_STEEL_CHAR_P;
4927 case 0x1625: // (blue steel)
4928 element = EL_STEEL_CHAR_Q;
4931 case 0x1626: // (blue steel)
4932 element = EL_STEEL_CHAR_R;
4935 case 0x1627: // (blue steel)
4936 element = EL_STEEL_CHAR_S;
4939 case 0x1628: // (blue steel)
4940 element = EL_STEEL_CHAR_T;
4943 case 0x1629: // (blue steel)
4944 element = EL_STEEL_CHAR_U;
4947 case 0x162a: // (blue steel)
4948 element = EL_STEEL_CHAR_V;
4951 case 0x162b: // (blue steel)
4952 element = EL_STEEL_CHAR_W;
4955 case 0x162c: // (blue steel)
4956 element = EL_STEEL_CHAR_X;
4959 case 0x162d: // (blue steel)
4960 element = EL_STEEL_CHAR_Y;
4963 case 0x162e: // (blue steel)
4964 element = EL_STEEL_CHAR_Z;
4967 case 0x162f: // (blue steel)
4968 element = EL_STEEL_CHAR_AUMLAUT;
4971 case 0x1630: // (blue steel)
4972 element = EL_STEEL_CHAR_OUMLAUT;
4975 case 0x1631: // (blue steel)
4976 element = EL_STEEL_CHAR_UUMLAUT;
4979 case 0x1632: // (blue steel)
4980 element = EL_STEEL_CHAR_0;
4983 case 0x1633: // (blue steel)
4984 element = EL_STEEL_CHAR_1;
4987 case 0x1634: // (blue steel)
4988 element = EL_STEEL_CHAR_2;
4991 case 0x1635: // (blue steel)
4992 element = EL_STEEL_CHAR_3;
4995 case 0x1636: // (blue steel)
4996 element = EL_STEEL_CHAR_4;
4999 case 0x1637: // (blue steel)
5000 element = EL_STEEL_CHAR_5;
5003 case 0x1638: // (blue steel)
5004 element = EL_STEEL_CHAR_6;
5007 case 0x1639: // (blue steel)
5008 element = EL_STEEL_CHAR_7;
5011 case 0x163a: // (blue steel)
5012 element = EL_STEEL_CHAR_8;
5015 case 0x163b: // (blue steel)
5016 element = EL_STEEL_CHAR_9;
5019 case 0x163c: // (blue steel)
5020 element = EL_STEEL_CHAR_PERIOD;
5023 case 0x163d: // (blue steel)
5024 element = EL_STEEL_CHAR_EXCLAM;
5027 case 0x163e: // (blue steel)
5028 element = EL_STEEL_CHAR_COLON;
5031 case 0x163f: // (blue steel)
5032 element = EL_STEEL_CHAR_LESS;
5035 case 0x1640: // (blue steel)
5036 element = EL_STEEL_CHAR_GREATER;
5039 case 0x1641: // (blue steel)
5040 element = EL_STEEL_CHAR_QUESTION;
5043 case 0x1642: // (blue steel)
5044 element = EL_STEEL_CHAR_COPYRIGHT;
5047 case 0x1643: // (blue steel)
5048 element = EL_STEEL_CHAR_UP;
5051 case 0x1644: // (blue steel)
5052 element = EL_STEEL_CHAR_DOWN;
5055 case 0x1645: // (blue steel)
5056 element = EL_STEEL_CHAR_BUTTON;
5059 case 0x1646: // (blue steel)
5060 element = EL_STEEL_CHAR_PLUS;
5063 case 0x1647: // (blue steel)
5064 element = EL_STEEL_CHAR_MINUS;
5067 case 0x1648: // (blue steel)
5068 element = EL_STEEL_CHAR_APOSTROPHE;
5071 case 0x1649: // (blue steel)
5072 element = EL_STEEL_CHAR_PARENLEFT;
5075 case 0x164a: // (blue steel)
5076 element = EL_STEEL_CHAR_PARENRIGHT;
5079 case 0x164b: // (green steel)
5080 element = EL_STEEL_CHAR_A;
5083 case 0x164c: // (green steel)
5084 element = EL_STEEL_CHAR_B;
5087 case 0x164d: // (green steel)
5088 element = EL_STEEL_CHAR_C;
5091 case 0x164e: // (green steel)
5092 element = EL_STEEL_CHAR_D;
5095 case 0x164f: // (green steel)
5096 element = EL_STEEL_CHAR_E;
5099 case 0x1650: // (green steel)
5100 element = EL_STEEL_CHAR_F;
5103 case 0x1651: // (green steel)
5104 element = EL_STEEL_CHAR_G;
5107 case 0x1652: // (green steel)
5108 element = EL_STEEL_CHAR_H;
5111 case 0x1653: // (green steel)
5112 element = EL_STEEL_CHAR_I;
5115 case 0x1654: // (green steel)
5116 element = EL_STEEL_CHAR_J;
5119 case 0x1655: // (green steel)
5120 element = EL_STEEL_CHAR_K;
5123 case 0x1656: // (green steel)
5124 element = EL_STEEL_CHAR_L;
5127 case 0x1657: // (green steel)
5128 element = EL_STEEL_CHAR_M;
5131 case 0x1658: // (green steel)
5132 element = EL_STEEL_CHAR_N;
5135 case 0x1659: // (green steel)
5136 element = EL_STEEL_CHAR_O;
5139 case 0x165a: // (green steel)
5140 element = EL_STEEL_CHAR_P;
5143 case 0x165b: // (green steel)
5144 element = EL_STEEL_CHAR_Q;
5147 case 0x165c: // (green steel)
5148 element = EL_STEEL_CHAR_R;
5151 case 0x165d: // (green steel)
5152 element = EL_STEEL_CHAR_S;
5155 case 0x165e: // (green steel)
5156 element = EL_STEEL_CHAR_T;
5159 case 0x165f: // (green steel)
5160 element = EL_STEEL_CHAR_U;
5163 case 0x1660: // (green steel)
5164 element = EL_STEEL_CHAR_V;
5167 case 0x1661: // (green steel)
5168 element = EL_STEEL_CHAR_W;
5171 case 0x1662: // (green steel)
5172 element = EL_STEEL_CHAR_X;
5175 case 0x1663: // (green steel)
5176 element = EL_STEEL_CHAR_Y;
5179 case 0x1664: // (green steel)
5180 element = EL_STEEL_CHAR_Z;
5183 case 0x1665: // (green steel)
5184 element = EL_STEEL_CHAR_AUMLAUT;
5187 case 0x1666: // (green steel)
5188 element = EL_STEEL_CHAR_OUMLAUT;
5191 case 0x1667: // (green steel)
5192 element = EL_STEEL_CHAR_UUMLAUT;
5195 case 0x1668: // (green steel)
5196 element = EL_STEEL_CHAR_0;
5199 case 0x1669: // (green steel)
5200 element = EL_STEEL_CHAR_1;
5203 case 0x166a: // (green steel)
5204 element = EL_STEEL_CHAR_2;
5207 case 0x166b: // (green steel)
5208 element = EL_STEEL_CHAR_3;
5211 case 0x166c: // (green steel)
5212 element = EL_STEEL_CHAR_4;
5215 case 0x166d: // (green steel)
5216 element = EL_STEEL_CHAR_5;
5219 case 0x166e: // (green steel)
5220 element = EL_STEEL_CHAR_6;
5223 case 0x166f: // (green steel)
5224 element = EL_STEEL_CHAR_7;
5227 case 0x1670: // (green steel)
5228 element = EL_STEEL_CHAR_8;
5231 case 0x1671: // (green steel)
5232 element = EL_STEEL_CHAR_9;
5235 case 0x1672: // (green steel)
5236 element = EL_STEEL_CHAR_PERIOD;
5239 case 0x1673: // (green steel)
5240 element = EL_STEEL_CHAR_EXCLAM;
5243 case 0x1674: // (green steel)
5244 element = EL_STEEL_CHAR_COLON;
5247 case 0x1675: // (green steel)
5248 element = EL_STEEL_CHAR_LESS;
5251 case 0x1676: // (green steel)
5252 element = EL_STEEL_CHAR_GREATER;
5255 case 0x1677: // (green steel)
5256 element = EL_STEEL_CHAR_QUESTION;
5259 case 0x1678: // (green steel)
5260 element = EL_STEEL_CHAR_COPYRIGHT;
5263 case 0x1679: // (green steel)
5264 element = EL_STEEL_CHAR_UP;
5267 case 0x167a: // (green steel)
5268 element = EL_STEEL_CHAR_DOWN;
5271 case 0x167b: // (green steel)
5272 element = EL_STEEL_CHAR_BUTTON;
5275 case 0x167c: // (green steel)
5276 element = EL_STEEL_CHAR_PLUS;
5279 case 0x167d: // (green steel)
5280 element = EL_STEEL_CHAR_MINUS;
5283 case 0x167e: // (green steel)
5284 element = EL_STEEL_CHAR_APOSTROPHE;
5287 case 0x167f: // (green steel)
5288 element = EL_STEEL_CHAR_PARENLEFT;
5291 case 0x1680: // (green steel)
5292 element = EL_STEEL_CHAR_PARENRIGHT;
5295 case 0x1681: // gate (red)
5296 element = EL_EM_GATE_1;
5299 case 0x1682: // secret gate (red)
5300 element = EL_GATE_1_GRAY;
5303 case 0x1683: // gate (yellow)
5304 element = EL_EM_GATE_2;
5307 case 0x1684: // secret gate (yellow)
5308 element = EL_GATE_2_GRAY;
5311 case 0x1685: // gate (blue)
5312 element = EL_EM_GATE_4;
5315 case 0x1686: // secret gate (blue)
5316 element = EL_GATE_4_GRAY;
5319 case 0x1687: // gate (green)
5320 element = EL_EM_GATE_3;
5323 case 0x1688: // secret gate (green)
5324 element = EL_GATE_3_GRAY;
5327 case 0x1689: // gate (white)
5328 element = EL_DC_GATE_WHITE;
5331 case 0x168a: // secret gate (white)
5332 element = EL_DC_GATE_WHITE_GRAY;
5335 case 0x168b: // secret gate (no key)
5336 element = EL_DC_GATE_FAKE_GRAY;
5340 element = EL_ROBOT_WHEEL;
5344 element = EL_DC_TIMEGATE_SWITCH;
5348 element = EL_ACID_POOL_BOTTOM;
5352 element = EL_ACID_POOL_TOPLEFT;
5356 element = EL_ACID_POOL_TOPRIGHT;
5360 element = EL_ACID_POOL_BOTTOMLEFT;
5364 element = EL_ACID_POOL_BOTTOMRIGHT;
5368 element = EL_STEELWALL;
5372 element = EL_STEELWALL_SLIPPERY;
5375 case 0x1695: // steel wall (not round)
5376 element = EL_STEELWALL;
5379 case 0x1696: // steel wall (left)
5380 element = EL_DC_STEELWALL_1_LEFT;
5383 case 0x1697: // steel wall (bottom)
5384 element = EL_DC_STEELWALL_1_BOTTOM;
5387 case 0x1698: // steel wall (right)
5388 element = EL_DC_STEELWALL_1_RIGHT;
5391 case 0x1699: // steel wall (top)
5392 element = EL_DC_STEELWALL_1_TOP;
5395 case 0x169a: // steel wall (left/bottom)
5396 element = EL_DC_STEELWALL_1_BOTTOMLEFT;
5399 case 0x169b: // steel wall (right/bottom)
5400 element = EL_DC_STEELWALL_1_BOTTOMRIGHT;
5403 case 0x169c: // steel wall (right/top)
5404 element = EL_DC_STEELWALL_1_TOPRIGHT;
5407 case 0x169d: // steel wall (left/top)
5408 element = EL_DC_STEELWALL_1_TOPLEFT;
5411 case 0x169e: // steel wall (right/bottom small)
5412 element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2;
5415 case 0x169f: // steel wall (left/bottom small)
5416 element = EL_DC_STEELWALL_1_BOTTOMLEFT_2;
5419 case 0x16a0: // steel wall (right/top small)
5420 element = EL_DC_STEELWALL_1_TOPRIGHT_2;
5423 case 0x16a1: // steel wall (left/top small)
5424 element = EL_DC_STEELWALL_1_TOPLEFT_2;
5427 case 0x16a2: // steel wall (left/right)
5428 element = EL_DC_STEELWALL_1_VERTICAL;
5431 case 0x16a3: // steel wall (top/bottom)
5432 element = EL_DC_STEELWALL_1_HORIZONTAL;
5435 case 0x16a4: // steel wall 2 (left end)
5436 element = EL_DC_STEELWALL_2_LEFT;
5439 case 0x16a5: // steel wall 2 (right end)
5440 element = EL_DC_STEELWALL_2_RIGHT;
5443 case 0x16a6: // steel wall 2 (top end)
5444 element = EL_DC_STEELWALL_2_TOP;
5447 case 0x16a7: // steel wall 2 (bottom end)
5448 element = EL_DC_STEELWALL_2_BOTTOM;
5451 case 0x16a8: // steel wall 2 (left/right)
5452 element = EL_DC_STEELWALL_2_HORIZONTAL;
5455 case 0x16a9: // steel wall 2 (up/down)
5456 element = EL_DC_STEELWALL_2_VERTICAL;
5459 case 0x16aa: // steel wall 2 (mid)
5460 element = EL_DC_STEELWALL_2_MIDDLE;
5464 element = EL_SIGN_EXCLAMATION;
5468 element = EL_SIGN_RADIOACTIVITY;
5472 element = EL_SIGN_STOP;
5476 element = EL_SIGN_WHEELCHAIR;
5480 element = EL_SIGN_PARKING;
5484 element = EL_SIGN_NO_ENTRY;
5488 element = EL_SIGN_HEART;
5492 element = EL_SIGN_GIVE_WAY;
5496 element = EL_SIGN_ENTRY_FORBIDDEN;
5500 element = EL_SIGN_EMERGENCY_EXIT;
5504 element = EL_SIGN_YIN_YANG;
5508 element = EL_WALL_EMERALD;
5512 element = EL_WALL_DIAMOND;
5516 element = EL_WALL_PEARL;
5520 element = EL_WALL_CRYSTAL;
5524 element = EL_INVISIBLE_WALL;
5528 element = EL_INVISIBLE_STEELWALL;
5532 // EL_INVISIBLE_SAND
5535 element = EL_LIGHT_SWITCH;
5539 element = EL_ENVELOPE_1;
5543 if (element >= 0x0117 && element <= 0x036e) // (?)
5544 element = EL_DIAMOND;
5545 else if (element >= 0x042d && element <= 0x0684) // (?)
5546 element = EL_EMERALD;
5547 else if (element >= 0x157c && element <= 0x158b)
5549 else if (element >= 0x1590 && element <= 0x159f)
5550 element = EL_DC_LANDMINE;
5551 else if (element >= 0x16bc && element <= 0x16cb)
5552 element = EL_INVISIBLE_SAND;
5555 Error(ERR_WARN, "unknown Diamond Caves element 0x%04x", element);
5556 element = EL_UNKNOWN;
5561 return getMappedElement(element);
5564 static void LoadLevelFromFileStream_DC(File *file, struct LevelInfo *level,
5567 byte header[DC_LEVEL_HEADER_SIZE];
5569 int envelope_header_pos = 62;
5570 int envelope_content_pos = 94;
5571 int level_name_pos = 251;
5572 int level_author_pos = 292;
5573 int envelope_header_len;
5574 int envelope_content_len;
5576 int level_author_len;
5578 int num_yamyam_contents;
5581 getDecodedWord_DC(0, TRUE); // initialize DC2 decoding engine
5583 for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
5585 unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5587 header[i * 2 + 0] = header_word >> 8;
5588 header[i * 2 + 1] = header_word & 0xff;
5591 // read some values from level header to check level decoding integrity
5592 fieldx = header[6] | (header[7] << 8);
5593 fieldy = header[8] | (header[9] << 8);
5594 num_yamyam_contents = header[60] | (header[61] << 8);
5596 // do some simple sanity checks to ensure that level was correctly decoded
5597 if (fieldx < 1 || fieldx > 256 ||
5598 fieldy < 1 || fieldy > 256 ||
5599 num_yamyam_contents < 1 || num_yamyam_contents > 8)
5601 level->no_valid_file = TRUE;
5603 Error(ERR_WARN, "cannot decode level from stream -- using empty level");
5608 // maximum envelope header size is 31 bytes
5609 envelope_header_len = header[envelope_header_pos];
5610 // maximum envelope content size is 110 (156?) bytes
5611 envelope_content_len = header[envelope_content_pos];
5613 // maximum level title size is 40 bytes
5614 level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN);
5615 // maximum level author size is 30 (51?) bytes
5616 level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
5620 for (i = 0; i < envelope_header_len; i++)
5621 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5622 level->envelope[0].text[envelope_size++] =
5623 header[envelope_header_pos + 1 + i];
5625 if (envelope_header_len > 0 && envelope_content_len > 0)
5627 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5628 level->envelope[0].text[envelope_size++] = '\n';
5629 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5630 level->envelope[0].text[envelope_size++] = '\n';
5633 for (i = 0; i < envelope_content_len; i++)
5634 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5635 level->envelope[0].text[envelope_size++] =
5636 header[envelope_content_pos + 1 + i];
5638 level->envelope[0].text[envelope_size] = '\0';
5640 level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
5641 level->envelope[0].ysize = 10;
5642 level->envelope[0].autowrap = TRUE;
5643 level->envelope[0].centered = TRUE;
5645 for (i = 0; i < level_name_len; i++)
5646 level->name[i] = header[level_name_pos + 1 + i];
5647 level->name[level_name_len] = '\0';
5649 for (i = 0; i < level_author_len; i++)
5650 level->author[i] = header[level_author_pos + 1 + i];
5651 level->author[level_author_len] = '\0';
5653 num_yamyam_contents = header[60] | (header[61] << 8);
5654 level->num_yamyam_contents =
5655 MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
5657 for (i = 0; i < num_yamyam_contents; i++)
5659 for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
5661 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5662 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
5664 if (i < MAX_ELEMENT_CONTENTS)
5665 level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
5669 fieldx = header[6] | (header[7] << 8);
5670 fieldy = header[8] | (header[9] << 8);
5671 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
5672 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
5674 for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
5676 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5677 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
5679 if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
5680 level->field[x][y] = getMappedElement_DC(element_dc);
5683 x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
5684 y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
5685 level->field[x][y] = EL_PLAYER_1;
5687 x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
5688 y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
5689 level->field[x][y] = EL_PLAYER_2;
5691 level->gems_needed = header[18] | (header[19] << 8);
5693 level->score[SC_EMERALD] = header[20] | (header[21] << 8);
5694 level->score[SC_DIAMOND] = header[22] | (header[23] << 8);
5695 level->score[SC_PEARL] = header[24] | (header[25] << 8);
5696 level->score[SC_CRYSTAL] = header[26] | (header[27] << 8);
5697 level->score[SC_NUT] = header[28] | (header[29] << 8);
5698 level->score[SC_ROBOT] = header[30] | (header[31] << 8);
5699 level->score[SC_SPACESHIP] = header[32] | (header[33] << 8);
5700 level->score[SC_BUG] = header[34] | (header[35] << 8);
5701 level->score[SC_YAMYAM] = header[36] | (header[37] << 8);
5702 level->score[SC_DYNAMITE] = header[38] | (header[39] << 8);
5703 level->score[SC_KEY] = header[40] | (header[41] << 8);
5704 level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8);
5706 level->time = header[44] | (header[45] << 8);
5708 level->amoeba_speed = header[46] | (header[47] << 8);
5709 level->time_light = header[48] | (header[49] << 8);
5710 level->time_timegate = header[50] | (header[51] << 8);
5711 level->time_wheel = header[52] | (header[53] << 8);
5712 level->time_magic_wall = header[54] | (header[55] << 8);
5713 level->extra_time = header[56] | (header[57] << 8);
5714 level->shield_normal_time = header[58] | (header[59] << 8);
5716 // Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
5717 // can slip down from flat walls, like normal walls and steel walls
5718 level->em_slippery_gems = TRUE;
5721 static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
5722 struct LevelFileInfo *level_file_info,
5723 boolean level_info_only)
5725 char *filename = level_file_info->filename;
5727 int num_magic_bytes = 8;
5728 char magic_bytes[num_magic_bytes + 1];
5729 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
5731 if (!(file = openFile(filename, MODE_READ)))
5733 level->no_valid_file = TRUE;
5735 if (!level_info_only)
5736 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
5741 // fseek(file, 0x0000, SEEK_SET);
5743 if (level_file_info->packed)
5745 // read "magic bytes" from start of file
5746 if (getStringFromFile(file, magic_bytes, num_magic_bytes + 1) == NULL)
5747 magic_bytes[0] = '\0';
5749 // check "magic bytes" for correct file format
5750 if (!strPrefix(magic_bytes, "DC2"))
5752 level->no_valid_file = TRUE;
5754 Error(ERR_WARN, "unknown DC level file '%s' -- using empty level",
5760 if (strPrefix(magic_bytes, "DC2Win95") ||
5761 strPrefix(magic_bytes, "DC2Win98"))
5763 int position_first_level = 0x00fa;
5764 int extra_bytes = 4;
5767 // advance file stream to first level inside the level package
5768 skip_bytes = position_first_level - num_magic_bytes - extra_bytes;
5770 // each block of level data is followed by block of non-level data
5771 num_levels_to_skip *= 2;
5773 // at least skip header bytes, therefore use ">= 0" instead of "> 0"
5774 while (num_levels_to_skip >= 0)
5776 // advance file stream to next level inside the level package
5777 if (seekFile(file, skip_bytes, SEEK_CUR) != 0)
5779 level->no_valid_file = TRUE;
5781 Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level",
5787 // skip apparently unused extra bytes following each level
5788 ReadUnusedBytesFromFile(file, extra_bytes);
5790 // read size of next level in level package
5791 skip_bytes = getFile32BitLE(file);
5793 num_levels_to_skip--;
5798 level->no_valid_file = TRUE;
5800 Error(ERR_WARN, "unknown DC2 level file '%s' -- using empty level",
5807 LoadLevelFromFileStream_DC(file, level, level_file_info->nr);
5813 // ----------------------------------------------------------------------------
5814 // functions for loading SB level
5815 // ----------------------------------------------------------------------------
5817 int getMappedElement_SB(int element_ascii, boolean use_ces)
5825 sb_element_mapping[] =
5827 { ' ', EL_EMPTY, EL_CUSTOM_1 }, // floor (space)
5828 { '#', EL_STEELWALL, EL_CUSTOM_2 }, // wall
5829 { '@', EL_PLAYER_1, EL_CUSTOM_3 }, // player
5830 { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, // box
5831 { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, // goal square
5832 { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, // box on goal square
5833 { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, // player on goal square
5834 { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, // floor beyond border
5841 for (i = 0; sb_element_mapping[i].ascii != 0; i++)
5842 if (element_ascii == sb_element_mapping[i].ascii)
5843 return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb);
5845 return EL_UNDEFINED;
5848 static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
5849 struct LevelFileInfo *level_file_info,
5850 boolean level_info_only)
5852 char *filename = level_file_info->filename;
5853 char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
5854 char last_comment[MAX_LINE_LEN];
5855 char level_name[MAX_LINE_LEN];
5858 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
5859 boolean read_continued_line = FALSE;
5860 boolean reading_playfield = FALSE;
5861 boolean got_valid_playfield_line = FALSE;
5862 boolean invalid_playfield_char = FALSE;
5863 boolean load_xsb_to_ces = check_special_flags("load_xsb_to_ces");
5864 int file_level_nr = 0;
5866 int x = 0, y = 0; // initialized to make compilers happy
5868 last_comment[0] = '\0';
5869 level_name[0] = '\0';
5871 if (!(file = openFile(filename, MODE_READ)))
5873 level->no_valid_file = TRUE;
5875 if (!level_info_only)
5876 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
5881 while (!checkEndOfFile(file))
5883 // level successfully read, but next level may follow here
5884 if (!got_valid_playfield_line && reading_playfield)
5886 // read playfield from single level file -- skip remaining file
5887 if (!level_file_info->packed)
5890 if (file_level_nr >= num_levels_to_skip)
5895 last_comment[0] = '\0';
5896 level_name[0] = '\0';
5898 reading_playfield = FALSE;
5901 got_valid_playfield_line = FALSE;
5903 // read next line of input file
5904 if (!getStringFromFile(file, line, MAX_LINE_LEN))
5907 // check if line was completely read and is terminated by line break
5908 if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
5911 // cut trailing line break (this can be newline and/or carriage return)
5912 for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
5913 if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
5916 // copy raw input line for later use (mainly debugging output)
5917 strcpy(line_raw, line);
5919 if (read_continued_line)
5921 // append new line to existing line, if there is enough space
5922 if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
5923 strcat(previous_line, line_ptr);
5925 strcpy(line, previous_line); // copy storage buffer to line
5927 read_continued_line = FALSE;
5930 // if the last character is '\', continue at next line
5931 if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
5933 line[strlen(line) - 1] = '\0'; // cut off trailing backslash
5934 strcpy(previous_line, line); // copy line to storage buffer
5936 read_continued_line = TRUE;
5942 if (line[0] == '\0')
5945 // extract comment text from comment line
5948 for (line_ptr = line; *line_ptr; line_ptr++)
5949 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != ';')
5952 strcpy(last_comment, line_ptr);
5957 // extract level title text from line containing level title
5958 if (line[0] == '\'')
5960 strcpy(level_name, &line[1]);
5962 if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'')
5963 level_name[strlen(level_name) - 1] = '\0';
5968 // skip lines containing only spaces (or empty lines)
5969 for (line_ptr = line; *line_ptr; line_ptr++)
5970 if (*line_ptr != ' ')
5972 if (*line_ptr == '\0')
5975 // at this point, we have found a line containing part of a playfield
5977 got_valid_playfield_line = TRUE;
5979 if (!reading_playfield)
5981 reading_playfield = TRUE;
5982 invalid_playfield_char = FALSE;
5984 for (x = 0; x < MAX_LEV_FIELDX; x++)
5985 for (y = 0; y < MAX_LEV_FIELDY; y++)
5986 level->field[x][y] = getMappedElement_SB(' ', load_xsb_to_ces);
5991 // start with topmost tile row
5995 // skip playfield line if larger row than allowed
5996 if (y >= MAX_LEV_FIELDY)
5999 // start with leftmost tile column
6002 // read playfield elements from line
6003 for (line_ptr = line; *line_ptr; line_ptr++)
6005 int mapped_sb_element = getMappedElement_SB(*line_ptr, load_xsb_to_ces);
6007 // stop parsing playfield line if larger column than allowed
6008 if (x >= MAX_LEV_FIELDX)
6011 if (mapped_sb_element == EL_UNDEFINED)
6013 invalid_playfield_char = TRUE;
6018 level->field[x][y] = mapped_sb_element;
6020 // continue with next tile column
6023 level->fieldx = MAX(x, level->fieldx);
6026 if (invalid_playfield_char)
6028 // if first playfield line, treat invalid lines as comment lines
6030 reading_playfield = FALSE;
6035 // continue with next tile row
6043 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, level->fieldx), MAX_LEV_FIELDX);
6044 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, level->fieldy), MAX_LEV_FIELDY);
6046 if (!reading_playfield)
6048 level->no_valid_file = TRUE;
6050 Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
6055 if (*level_name != '\0')
6057 strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN);
6058 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6060 else if (*last_comment != '\0')
6062 strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN);
6063 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6067 sprintf(level->name, "--> Level %d <--", level_file_info->nr);
6070 // set all empty fields beyond the border walls to invisible steel wall
6071 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6073 if ((x == 0 || x == level->fieldx - 1 ||
6074 y == 0 || y == level->fieldy - 1) &&
6075 level->field[x][y] == getMappedElement_SB(' ', load_xsb_to_ces))
6076 FloodFillLevel(x, y, getMappedElement_SB('_', load_xsb_to_ces),
6077 level->field, level->fieldx, level->fieldy);
6080 // set special level settings for Sokoban levels
6083 level->use_step_counter = TRUE;
6085 if (load_xsb_to_ces)
6087 // special global settings can now be set in level template
6089 level->use_custom_template = TRUE;
6094 // -------------------------------------------------------------------------
6095 // functions for handling native levels
6096 // -------------------------------------------------------------------------
6098 static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
6099 struct LevelFileInfo *level_file_info,
6100 boolean level_info_only)
6102 if (!LoadNativeLevel_EM(level_file_info->filename, level_info_only))
6103 level->no_valid_file = TRUE;
6106 static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
6107 struct LevelFileInfo *level_file_info,
6108 boolean level_info_only)
6112 // determine position of requested level inside level package
6113 if (level_file_info->packed)
6114 pos = level_file_info->nr - leveldir_current->first_level;
6116 if (!LoadNativeLevel_SP(level_file_info->filename, pos, level_info_only))
6117 level->no_valid_file = TRUE;
6120 static void LoadLevelFromFileInfo_MM(struct LevelInfo *level,
6121 struct LevelFileInfo *level_file_info,
6122 boolean level_info_only)
6124 if (!LoadNativeLevel_MM(level_file_info->filename, level_info_only))
6125 level->no_valid_file = TRUE;
6128 void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
6130 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6131 CopyNativeLevel_RND_to_EM(level);
6132 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6133 CopyNativeLevel_RND_to_SP(level);
6134 else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
6135 CopyNativeLevel_RND_to_MM(level);
6138 void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
6140 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6141 CopyNativeLevel_EM_to_RND(level);
6142 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6143 CopyNativeLevel_SP_to_RND(level);
6144 else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
6145 CopyNativeLevel_MM_to_RND(level);
6148 void SaveNativeLevel(struct LevelInfo *level)
6150 if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6152 char *basename = getSingleLevelBasenameExt(level->file_info.nr, "sp");
6153 char *filename = getLevelFilenameFromBasename(basename);
6155 CopyNativeLevel_RND_to_SP(level);
6156 CopyNativeTape_RND_to_SP(level);
6158 SaveNativeLevel_SP(filename);
6163 // ----------------------------------------------------------------------------
6164 // functions for loading generic level
6165 // ----------------------------------------------------------------------------
6167 static void LoadLevelFromFileInfo(struct LevelInfo *level,
6168 struct LevelFileInfo *level_file_info,
6169 boolean level_info_only)
6171 // always start with reliable default values
6172 setLevelInfoToDefaults(level, level_info_only, TRUE);
6174 switch (level_file_info->type)
6176 case LEVEL_FILE_TYPE_RND:
6177 LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
6180 case LEVEL_FILE_TYPE_EM:
6181 LoadLevelFromFileInfo_EM(level, level_file_info, level_info_only);
6182 level->game_engine_type = GAME_ENGINE_TYPE_EM;
6185 case LEVEL_FILE_TYPE_SP:
6186 LoadLevelFromFileInfo_SP(level, level_file_info, level_info_only);
6187 level->game_engine_type = GAME_ENGINE_TYPE_SP;
6190 case LEVEL_FILE_TYPE_MM:
6191 LoadLevelFromFileInfo_MM(level, level_file_info, level_info_only);
6192 level->game_engine_type = GAME_ENGINE_TYPE_MM;
6195 case LEVEL_FILE_TYPE_DC:
6196 LoadLevelFromFileInfo_DC(level, level_file_info, level_info_only);
6199 case LEVEL_FILE_TYPE_SB:
6200 LoadLevelFromFileInfo_SB(level, level_file_info, level_info_only);
6204 LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
6208 // if level file is invalid, restore level structure to default values
6209 if (level->no_valid_file)
6210 setLevelInfoToDefaults(level, level_info_only, FALSE);
6212 if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
6213 level->game_engine_type = GAME_ENGINE_TYPE_RND;
6215 if (level_file_info->type != LEVEL_FILE_TYPE_RND)
6216 CopyNativeLevel_Native_to_RND(level);
6219 void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
6221 static struct LevelFileInfo level_file_info;
6223 // always start with reliable default values
6224 setFileInfoToDefaults(&level_file_info);
6226 level_file_info.nr = 0; // unknown level number
6227 level_file_info.type = LEVEL_FILE_TYPE_RND; // no others supported yet
6229 setString(&level_file_info.filename, filename);
6231 LoadLevelFromFileInfo(level, &level_file_info, FALSE);
6234 static void LoadLevel_InitVersion(struct LevelInfo *level)
6238 if (leveldir_current == NULL) // only when dumping level
6241 // all engine modifications also valid for levels which use latest engine
6242 if (level->game_version < VERSION_IDENT(3,2,0,5))
6244 // time bonus score was given for 10 s instead of 1 s before 3.2.0-5
6245 level->score[SC_TIME_BONUS] /= 10;
6248 if (leveldir_current->latest_engine)
6250 // ---------- use latest game engine --------------------------------------
6252 /* For all levels which are forced to use the latest game engine version
6253 (normally all but user contributed, private and undefined levels), set
6254 the game engine version to the actual version; this allows for actual
6255 corrections in the game engine to take effect for existing, converted
6256 levels (from "classic" or other existing games) to make the emulation
6257 of the corresponding game more accurate, while (hopefully) not breaking
6258 existing levels created from other players. */
6260 level->game_version = GAME_VERSION_ACTUAL;
6262 /* Set special EM style gems behaviour: EM style gems slip down from
6263 normal, steel and growing wall. As this is a more fundamental change,
6264 it seems better to set the default behaviour to "off" (as it is more
6265 natural) and make it configurable in the level editor (as a property
6266 of gem style elements). Already existing converted levels (neither
6267 private nor contributed levels) are changed to the new behaviour. */
6269 if (level->file_version < FILE_VERSION_2_0)
6270 level->em_slippery_gems = TRUE;
6275 // ---------- use game engine the level was created with --------------------
6277 /* For all levels which are not forced to use the latest game engine
6278 version (normally user contributed, private and undefined levels),
6279 use the version of the game engine the levels were created for.
6281 Since 2.0.1, the game engine version is now directly stored
6282 in the level file (chunk "VERS"), so there is no need anymore
6283 to set the game version from the file version (except for old,
6284 pre-2.0 levels, where the game version is still taken from the
6285 file format version used to store the level -- see above). */
6287 // player was faster than enemies in 1.0.0 and before
6288 if (level->file_version == FILE_VERSION_1_0)
6289 for (i = 0; i < MAX_PLAYERS; i++)
6290 level->initial_player_stepsize[i] = STEPSIZE_FAST;
6292 // default behaviour for EM style gems was "slippery" only in 2.0.1
6293 if (level->game_version == VERSION_IDENT(2,0,1,0))
6294 level->em_slippery_gems = TRUE;
6296 // springs could be pushed over pits before (pre-release version) 2.2.0
6297 if (level->game_version < VERSION_IDENT(2,2,0,0))
6298 level->use_spring_bug = TRUE;
6300 if (level->game_version < VERSION_IDENT(3,2,0,5))
6302 // time orb caused limited time in endless time levels before 3.2.0-5
6303 level->use_time_orb_bug = TRUE;
6305 // default behaviour for snapping was "no snap delay" before 3.2.0-5
6306 level->block_snap_field = FALSE;
6308 // extra time score was same value as time left score before 3.2.0-5
6309 level->extra_time_score = level->score[SC_TIME_BONUS];
6312 if (level->game_version < VERSION_IDENT(3,2,0,7))
6314 // default behaviour for snapping was "not continuous" before 3.2.0-7
6315 level->continuous_snapping = FALSE;
6318 // only few elements were able to actively move into acid before 3.1.0
6319 // trigger settings did not exist before 3.1.0; set to default "any"
6320 if (level->game_version < VERSION_IDENT(3,1,0,0))
6322 // correct "can move into acid" settings (all zero in old levels)
6324 level->can_move_into_acid_bits = 0; // nothing can move into acid
6325 level->dont_collide_with_bits = 0; // nothing is deadly when colliding
6327 setMoveIntoAcidProperty(level, EL_ROBOT, TRUE);
6328 setMoveIntoAcidProperty(level, EL_SATELLITE, TRUE);
6329 setMoveIntoAcidProperty(level, EL_PENGUIN, TRUE);
6330 setMoveIntoAcidProperty(level, EL_BALLOON, TRUE);
6332 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6333 SET_PROPERTY(EL_CUSTOM_START + i, EP_CAN_MOVE_INTO_ACID, TRUE);
6335 // correct trigger settings (stored as zero == "none" in old levels)
6337 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6339 int element = EL_CUSTOM_START + i;
6340 struct ElementInfo *ei = &element_info[element];
6342 for (j = 0; j < ei->num_change_pages; j++)
6344 struct ElementChangeInfo *change = &ei->change_page[j];
6346 change->trigger_player = CH_PLAYER_ANY;
6347 change->trigger_page = CH_PAGE_ANY;
6352 // try to detect and fix "Snake Bite" levels, which are broken with 3.2.0
6354 int element = EL_CUSTOM_256;
6355 struct ElementInfo *ei = &element_info[element];
6356 struct ElementChangeInfo *change = &ei->change_page[0];
6358 /* This is needed to fix a problem that was caused by a bugfix in function
6359 game.c/CreateFieldExt() introduced with 3.2.0 that corrects the behaviour
6360 when a custom element changes to EL_SOKOBAN_FIELD_PLAYER (before, it did
6361 not replace walkable elements, but instead just placed the player on it,
6362 without placing the Sokoban field under the player). Unfortunately, this
6363 breaks "Snake Bite" style levels when the snake is halfway through a door
6364 that just closes (the snake head is still alive and can be moved in this
6365 case). This can be fixed by replacing the EL_SOKOBAN_FIELD_PLAYER by the
6366 player (without Sokoban element) which then gets killed as designed). */
6368 if ((strncmp(leveldir_current->identifier, "snake_bite", 10) == 0 ||
6369 strncmp(ei->description, "pause b4 death", 14) == 0) &&
6370 change->target_element == EL_SOKOBAN_FIELD_PLAYER)
6371 change->target_element = EL_PLAYER_1;
6374 // try to detect and fix "Zelda" style levels, which are broken with 3.2.5
6375 if (level->game_version < VERSION_IDENT(3,2,5,0))
6377 /* This is needed to fix a problem that was caused by a bugfix in function
6378 game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
6379 corrects the behaviour when a custom element changes to another custom
6380 element with a higher element number that has change actions defined.
6381 Normally, only one change per frame is allowed for custom elements.
6382 Therefore, it is checked if a custom element already changed in the
6383 current frame; if it did, subsequent changes are suppressed.
6384 Unfortunately, this is only checked for element changes, but not for
6385 change actions, which are still executed. As the function above loops
6386 through all custom elements from lower to higher, an element change
6387 resulting in a lower CE number won't be checked again, while a target
6388 element with a higher number will also be checked, and potential change
6389 actions will get executed for this CE, too (which is wrong), while
6390 further changes are ignored (which is correct). As this bugfix breaks
6391 Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
6392 few other levels like Alan Bond's "FMV"), allow the previous, incorrect
6393 behaviour for existing levels and tapes that make use of this bug */
6395 level->use_action_after_change_bug = TRUE;
6398 // not centering level after relocating player was default only in 3.2.3
6399 if (level->game_version == VERSION_IDENT(3,2,3,0)) // (no pre-releases)
6400 level->shifted_relocation = TRUE;
6402 // EM style elements always chain-exploded in R'n'D engine before 3.2.6
6403 if (level->game_version < VERSION_IDENT(3,2,6,0))
6404 level->em_explodes_by_fire = TRUE;
6406 // levels were solved by the first player entering an exit up to 4.1.0.0
6407 if (level->game_version <= VERSION_IDENT(4,1,0,0))
6408 level->solved_by_one_player = TRUE;
6410 // game logic of "game of life" and "biomaze" was buggy before 4.1.1.1
6411 if (level->game_version < VERSION_IDENT(4,1,1,1))
6412 level->use_life_bugs = TRUE;
6414 // only Sokoban fields (but not objects) had to be solved before 4.1.1.1
6415 if (level->game_version < VERSION_IDENT(4,1,1,1))
6416 level->sb_objects_needed = FALSE;
6419 static void LoadLevel_InitStandardElements(struct LevelInfo *level)
6423 // map elements that have changed in newer versions
6424 level->amoeba_content = getMappedElementByVersion(level->amoeba_content,
6425 level->game_version);
6426 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6427 for (x = 0; x < 3; x++)
6428 for (y = 0; y < 3; y++)
6429 level->yamyam_content[i].e[x][y] =
6430 getMappedElementByVersion(level->yamyam_content[i].e[x][y],
6431 level->game_version);
6435 static void LoadLevel_InitCustomElements(struct LevelInfo *level)
6439 // map custom element change events that have changed in newer versions
6440 // (these following values were accidentally changed in version 3.0.1)
6441 // (this seems to be needed only for 'ab_levelset3' and 'ab_levelset4')
6442 if (level->game_version <= VERSION_IDENT(3,0,0,0))
6444 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6446 int element = EL_CUSTOM_START + i;
6448 // order of checking and copying events to be mapped is important
6449 // (do not change the start and end value -- they are constant)
6450 for (j = CE_BY_OTHER_ACTION; j >= CE_VALUE_GETS_ZERO; j--)
6452 if (HAS_CHANGE_EVENT(element, j - 2))
6454 SET_CHANGE_EVENT(element, j - 2, FALSE);
6455 SET_CHANGE_EVENT(element, j, TRUE);
6459 // order of checking and copying events to be mapped is important
6460 // (do not change the start and end value -- they are constant)
6461 for (j = CE_PLAYER_COLLECTS_X; j >= CE_HITTING_SOMETHING; j--)
6463 if (HAS_CHANGE_EVENT(element, j - 1))
6465 SET_CHANGE_EVENT(element, j - 1, FALSE);
6466 SET_CHANGE_EVENT(element, j, TRUE);
6472 // initialize "can_change" field for old levels with only one change page
6473 if (level->game_version <= VERSION_IDENT(3,0,2,0))
6475 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6477 int element = EL_CUSTOM_START + i;
6479 if (CAN_CHANGE(element))
6480 element_info[element].change->can_change = TRUE;
6484 // correct custom element values (for old levels without these options)
6485 if (level->game_version < VERSION_IDENT(3,1,1,0))
6487 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6489 int element = EL_CUSTOM_START + i;
6490 struct ElementInfo *ei = &element_info[element];
6492 if (ei->access_direction == MV_NO_DIRECTION)
6493 ei->access_direction = MV_ALL_DIRECTIONS;
6497 // correct custom element values (fix invalid values for all versions)
6500 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6502 int element = EL_CUSTOM_START + i;
6503 struct ElementInfo *ei = &element_info[element];
6505 for (j = 0; j < ei->num_change_pages; j++)
6507 struct ElementChangeInfo *change = &ei->change_page[j];
6509 if (change->trigger_player == CH_PLAYER_NONE)
6510 change->trigger_player = CH_PLAYER_ANY;
6512 if (change->trigger_side == CH_SIDE_NONE)
6513 change->trigger_side = CH_SIDE_ANY;
6518 // initialize "can_explode" field for old levels which did not store this
6519 // !!! CHECK THIS -- "<= 3,1,0,0" IS PROBABLY WRONG !!!
6520 if (level->game_version <= VERSION_IDENT(3,1,0,0))
6522 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6524 int element = EL_CUSTOM_START + i;
6526 if (EXPLODES_1X1_OLD(element))
6527 element_info[element].explosion_type = EXPLODES_1X1;
6529 SET_PROPERTY(element, EP_CAN_EXPLODE, (EXPLODES_BY_FIRE(element) ||
6530 EXPLODES_SMASHED(element) ||
6531 EXPLODES_IMPACT(element)));
6535 // correct previously hard-coded move delay values for maze runner style
6536 if (level->game_version < VERSION_IDENT(3,1,1,0))
6538 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6540 int element = EL_CUSTOM_START + i;
6542 if (element_info[element].move_pattern & MV_MAZE_RUNNER_STYLE)
6544 // previously hard-coded and therefore ignored
6545 element_info[element].move_delay_fixed = 9;
6546 element_info[element].move_delay_random = 0;
6551 // set some other uninitialized values of custom elements in older levels
6552 if (level->game_version < VERSION_IDENT(3,1,0,0))
6554 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6556 int element = EL_CUSTOM_START + i;
6558 element_info[element].access_direction = MV_ALL_DIRECTIONS;
6560 element_info[element].explosion_delay = 17;
6561 element_info[element].ignition_delay = 8;
6566 static void LoadLevel_InitElements(struct LevelInfo *level)
6568 LoadLevel_InitStandardElements(level);
6570 if (level->file_has_custom_elements)
6571 LoadLevel_InitCustomElements(level);
6573 // initialize element properties for level editor etc.
6574 InitElementPropertiesEngine(level->game_version);
6575 InitElementPropertiesGfxElement();
6578 static void LoadLevel_InitPlayfield(struct LevelInfo *level)
6582 // map elements that have changed in newer versions
6583 for (y = 0; y < level->fieldy; y++)
6584 for (x = 0; x < level->fieldx; x++)
6585 level->field[x][y] = getMappedElementByVersion(level->field[x][y],
6586 level->game_version);
6588 // clear unused playfield data (nicer if level gets resized in editor)
6589 for (x = 0; x < MAX_LEV_FIELDX; x++)
6590 for (y = 0; y < MAX_LEV_FIELDY; y++)
6591 if (x >= level->fieldx || y >= level->fieldy)
6592 level->field[x][y] = EL_EMPTY;
6594 // copy elements to runtime playfield array
6595 for (x = 0; x < MAX_LEV_FIELDX; x++)
6596 for (y = 0; y < MAX_LEV_FIELDY; y++)
6597 Feld[x][y] = level->field[x][y];
6599 // initialize level size variables for faster access
6600 lev_fieldx = level->fieldx;
6601 lev_fieldy = level->fieldy;
6603 // determine border element for this level
6604 if (level->file_info.type == LEVEL_FILE_TYPE_DC)
6605 BorderElement = EL_EMPTY; // (in editor, SetBorderElement() is used)
6610 static void LoadLevel_InitNativeEngines(struct LevelInfo *level)
6612 struct LevelFileInfo *level_file_info = &level->file_info;
6614 if (level_file_info->type == LEVEL_FILE_TYPE_RND)
6615 CopyNativeLevel_RND_to_Native(level);
6618 static void LoadLevelTemplate_LoadAndInit(void)
6620 LoadLevelFromFileInfo(&level_template, &level_template.file_info, FALSE);
6622 LoadLevel_InitVersion(&level_template);
6623 LoadLevel_InitElements(&level_template);
6625 ActivateLevelTemplate();
6628 void LoadLevelTemplate(int nr)
6630 if (!fileExists(getGlobalLevelTemplateFilename()))
6632 Error(ERR_WARN, "no level template found for this level");
6637 setLevelFileInfo(&level_template.file_info, nr);
6639 LoadLevelTemplate_LoadAndInit();
6642 static void LoadNetworkLevelTemplate(struct NetworkLevelInfo *network_level)
6644 copyLevelFileInfo(&network_level->tmpl_info, &level_template.file_info);
6646 LoadLevelTemplate_LoadAndInit();
6649 static void LoadLevel_LoadAndInit(struct NetworkLevelInfo *network_level)
6651 LoadLevelFromFileInfo(&level, &level.file_info, FALSE);
6653 if (level.use_custom_template)
6655 if (network_level != NULL)
6656 LoadNetworkLevelTemplate(network_level);
6658 LoadLevelTemplate(-1);
6661 LoadLevel_InitVersion(&level);
6662 LoadLevel_InitElements(&level);
6663 LoadLevel_InitPlayfield(&level);
6665 LoadLevel_InitNativeEngines(&level);
6668 void LoadLevel(int nr)
6670 SetLevelSetInfo(leveldir_current->identifier, nr);
6672 setLevelFileInfo(&level.file_info, nr);
6674 LoadLevel_LoadAndInit(NULL);
6677 void LoadLevelInfoOnly(int nr)
6679 setLevelFileInfo(&level.file_info, nr);
6681 LoadLevelFromFileInfo(&level, &level.file_info, TRUE);
6684 void LoadNetworkLevel(struct NetworkLevelInfo *network_level)
6686 SetLevelSetInfo(network_level->leveldir_identifier,
6687 network_level->file_info.nr);
6689 copyLevelFileInfo(&network_level->file_info, &level.file_info);
6691 LoadLevel_LoadAndInit(network_level);
6694 static int SaveLevel_VERS(FILE *file, struct LevelInfo *level)
6698 chunk_size += putFileVersion(file, level->file_version);
6699 chunk_size += putFileVersion(file, level->game_version);
6704 static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
6708 chunk_size += putFile16BitBE(file, level->creation_date.year);
6709 chunk_size += putFile8Bit(file, level->creation_date.month);
6710 chunk_size += putFile8Bit(file, level->creation_date.day);
6715 #if ENABLE_HISTORIC_CHUNKS
6716 static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
6720 putFile8Bit(file, level->fieldx);
6721 putFile8Bit(file, level->fieldy);
6723 putFile16BitBE(file, level->time);
6724 putFile16BitBE(file, level->gems_needed);
6726 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
6727 putFile8Bit(file, level->name[i]);
6729 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
6730 putFile8Bit(file, level->score[i]);
6732 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
6733 for (y = 0; y < 3; y++)
6734 for (x = 0; x < 3; x++)
6735 putFile8Bit(file, (level->encoding_16bit_yamyam ? EL_EMPTY :
6736 level->yamyam_content[i].e[x][y]));
6737 putFile8Bit(file, level->amoeba_speed);
6738 putFile8Bit(file, level->time_magic_wall);
6739 putFile8Bit(file, level->time_wheel);
6740 putFile8Bit(file, (level->encoding_16bit_amoeba ? EL_EMPTY :
6741 level->amoeba_content));
6742 putFile8Bit(file, (level->initial_player_stepsize == STEPSIZE_FAST ? 1 : 0));
6743 putFile8Bit(file, (level->initial_gravity ? 1 : 0));
6744 putFile8Bit(file, (level->encoding_16bit_field ? 1 : 0));
6745 putFile8Bit(file, (level->em_slippery_gems ? 1 : 0));
6747 putFile8Bit(file, (level->use_custom_template ? 1 : 0));
6749 putFile8Bit(file, (level->block_last_field ? 1 : 0));
6750 putFile8Bit(file, (level->sp_block_last_field ? 1 : 0));
6751 putFile32BitBE(file, level->can_move_into_acid_bits);
6752 putFile8Bit(file, level->dont_collide_with_bits);
6754 putFile8Bit(file, (level->use_spring_bug ? 1 : 0));
6755 putFile8Bit(file, (level->use_step_counter ? 1 : 0));
6757 putFile8Bit(file, (level->instant_relocation ? 1 : 0));
6758 putFile8Bit(file, (level->can_pass_to_walkable ? 1 : 0));
6759 putFile8Bit(file, (level->grow_into_diggable ? 1 : 0));
6761 putFile8Bit(file, level->game_engine_type);
6763 WriteUnusedBytesToFile(file, LEVEL_CHUNK_HEAD_UNUSED);
6767 static int SaveLevel_NAME(FILE *file, struct LevelInfo *level)
6772 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
6773 chunk_size += putFile8Bit(file, level->name[i]);
6778 static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
6783 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
6784 chunk_size += putFile8Bit(file, level->author[i]);
6789 #if ENABLE_HISTORIC_CHUNKS
6790 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
6795 for (y = 0; y < level->fieldy; y++)
6796 for (x = 0; x < level->fieldx; x++)
6797 if (level->encoding_16bit_field)
6798 chunk_size += putFile16BitBE(file, level->field[x][y]);
6800 chunk_size += putFile8Bit(file, level->field[x][y]);
6806 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
6811 for (y = 0; y < level->fieldy; y++)
6812 for (x = 0; x < level->fieldx; x++)
6813 chunk_size += putFile16BitBE(file, level->field[x][y]);
6818 #if ENABLE_HISTORIC_CHUNKS
6819 static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
6823 putFile8Bit(file, EL_YAMYAM);
6824 putFile8Bit(file, level->num_yamyam_contents);
6825 putFile8Bit(file, 0);
6826 putFile8Bit(file, 0);
6828 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6829 for (y = 0; y < 3; y++)
6830 for (x = 0; x < 3; x++)
6831 if (level->encoding_16bit_field)
6832 putFile16BitBE(file, level->yamyam_content[i].e[x][y]);
6834 putFile8Bit(file, level->yamyam_content[i].e[x][y]);
6838 #if ENABLE_HISTORIC_CHUNKS
6839 static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
6842 int num_contents, content_xsize, content_ysize;
6843 int content_array[MAX_ELEMENT_CONTENTS][3][3];
6845 if (element == EL_YAMYAM)
6847 num_contents = level->num_yamyam_contents;
6851 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6852 for (y = 0; y < 3; y++)
6853 for (x = 0; x < 3; x++)
6854 content_array[i][x][y] = level->yamyam_content[i].e[x][y];
6856 else if (element == EL_BD_AMOEBA)
6862 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6863 for (y = 0; y < 3; y++)
6864 for (x = 0; x < 3; x++)
6865 content_array[i][x][y] = EL_EMPTY;
6866 content_array[0][0][0] = level->amoeba_content;
6870 // chunk header already written -- write empty chunk data
6871 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
6873 Error(ERR_WARN, "cannot save content for element '%d'", element);
6877 putFile16BitBE(file, element);
6878 putFile8Bit(file, num_contents);
6879 putFile8Bit(file, content_xsize);
6880 putFile8Bit(file, content_ysize);
6882 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
6884 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6885 for (y = 0; y < 3; y++)
6886 for (x = 0; x < 3; x++)
6887 putFile16BitBE(file, content_array[i][x][y]);
6891 #if ENABLE_HISTORIC_CHUNKS
6892 static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
6894 int envelope_nr = element - EL_ENVELOPE_1;
6895 int envelope_len = strlen(level->envelope_text[envelope_nr]) + 1;
6899 chunk_size += putFile16BitBE(file, element);
6900 chunk_size += putFile16BitBE(file, envelope_len);
6901 chunk_size += putFile8Bit(file, level->envelope_xsize[envelope_nr]);
6902 chunk_size += putFile8Bit(file, level->envelope_ysize[envelope_nr]);
6904 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT3_UNUSED);
6905 chunk_size += LEVEL_CHUNK_CNT3_UNUSED;
6907 for (i = 0; i < envelope_len; i++)
6908 chunk_size += putFile8Bit(file, level->envelope_text[envelope_nr][i]);
6914 #if ENABLE_HISTORIC_CHUNKS
6915 static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
6916 int num_changed_custom_elements)
6920 putFile16BitBE(file, num_changed_custom_elements);
6922 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6924 int element = EL_CUSTOM_START + i;
6926 struct ElementInfo *ei = &element_info[element];
6928 if (ei->properties[EP_BITFIELD_BASE_NR] != EP_BITMASK_DEFAULT)
6930 if (check < num_changed_custom_elements)
6932 putFile16BitBE(file, element);
6933 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
6940 if (check != num_changed_custom_elements) // should not happen
6941 Error(ERR_WARN, "inconsistent number of custom element properties");
6945 #if ENABLE_HISTORIC_CHUNKS
6946 static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
6947 int num_changed_custom_elements)
6951 putFile16BitBE(file, num_changed_custom_elements);
6953 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6955 int element = EL_CUSTOM_START + i;
6957 if (element_info[element].change->target_element != EL_EMPTY_SPACE)
6959 if (check < num_changed_custom_elements)
6961 putFile16BitBE(file, element);
6962 putFile16BitBE(file, element_info[element].change->target_element);
6969 if (check != num_changed_custom_elements) // should not happen
6970 Error(ERR_WARN, "inconsistent number of custom target elements");
6974 #if ENABLE_HISTORIC_CHUNKS
6975 static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
6976 int num_changed_custom_elements)
6978 int i, j, x, y, check = 0;
6980 putFile16BitBE(file, num_changed_custom_elements);
6982 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6984 int element = EL_CUSTOM_START + i;
6985 struct ElementInfo *ei = &element_info[element];
6987 if (ei->modified_settings)
6989 if (check < num_changed_custom_elements)
6991 putFile16BitBE(file, element);
6993 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
6994 putFile8Bit(file, ei->description[j]);
6996 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
6998 // some free bytes for future properties and padding
6999 WriteUnusedBytesToFile(file, 7);
7001 putFile8Bit(file, ei->use_gfx_element);
7002 putFile16BitBE(file, ei->gfx_element_initial);
7004 putFile8Bit(file, ei->collect_score_initial);
7005 putFile8Bit(file, ei->collect_count_initial);
7007 putFile16BitBE(file, ei->push_delay_fixed);
7008 putFile16BitBE(file, ei->push_delay_random);
7009 putFile16BitBE(file, ei->move_delay_fixed);
7010 putFile16BitBE(file, ei->move_delay_random);
7012 putFile16BitBE(file, ei->move_pattern);
7013 putFile8Bit(file, ei->move_direction_initial);
7014 putFile8Bit(file, ei->move_stepsize);
7016 for (y = 0; y < 3; y++)
7017 for (x = 0; x < 3; x++)
7018 putFile16BitBE(file, ei->content.e[x][y]);
7020 putFile32BitBE(file, ei->change->events);
7022 putFile16BitBE(file, ei->change->target_element);
7024 putFile16BitBE(file, ei->change->delay_fixed);
7025 putFile16BitBE(file, ei->change->delay_random);
7026 putFile16BitBE(file, ei->change->delay_frames);
7028 putFile16BitBE(file, ei->change->initial_trigger_element);
7030 putFile8Bit(file, ei->change->explode);
7031 putFile8Bit(file, ei->change->use_target_content);
7032 putFile8Bit(file, ei->change->only_if_complete);
7033 putFile8Bit(file, ei->change->use_random_replace);
7035 putFile8Bit(file, ei->change->random_percentage);
7036 putFile8Bit(file, ei->change->replace_when);
7038 for (y = 0; y < 3; y++)
7039 for (x = 0; x < 3; x++)
7040 putFile16BitBE(file, ei->change->content.e[x][y]);
7042 putFile8Bit(file, ei->slippery_type);
7044 // some free bytes for future properties and padding
7045 WriteUnusedBytesToFile(file, LEVEL_CPART_CUS3_UNUSED);
7052 if (check != num_changed_custom_elements) // should not happen
7053 Error(ERR_WARN, "inconsistent number of custom element properties");
7057 #if ENABLE_HISTORIC_CHUNKS
7058 static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
7060 struct ElementInfo *ei = &element_info[element];
7063 // ---------- custom element base property values (96 bytes) ----------------
7065 putFile16BitBE(file, element);
7067 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7068 putFile8Bit(file, ei->description[i]);
7070 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7072 WriteUnusedBytesToFile(file, 4); // reserved for more base properties
7074 putFile8Bit(file, ei->num_change_pages);
7076 putFile16BitBE(file, ei->ce_value_fixed_initial);
7077 putFile16BitBE(file, ei->ce_value_random_initial);
7078 putFile8Bit(file, ei->use_last_ce_value);
7080 putFile8Bit(file, ei->use_gfx_element);
7081 putFile16BitBE(file, ei->gfx_element_initial);
7083 putFile8Bit(file, ei->collect_score_initial);
7084 putFile8Bit(file, ei->collect_count_initial);
7086 putFile8Bit(file, ei->drop_delay_fixed);
7087 putFile8Bit(file, ei->push_delay_fixed);
7088 putFile8Bit(file, ei->drop_delay_random);
7089 putFile8Bit(file, ei->push_delay_random);
7090 putFile16BitBE(file, ei->move_delay_fixed);
7091 putFile16BitBE(file, ei->move_delay_random);
7093 // bits 0 - 15 of "move_pattern" ...
7094 putFile16BitBE(file, ei->move_pattern & 0xffff);
7095 putFile8Bit(file, ei->move_direction_initial);
7096 putFile8Bit(file, ei->move_stepsize);
7098 putFile8Bit(file, ei->slippery_type);
7100 for (y = 0; y < 3; y++)
7101 for (x = 0; x < 3; x++)
7102 putFile16BitBE(file, ei->content.e[x][y]);
7104 putFile16BitBE(file, ei->move_enter_element);
7105 putFile16BitBE(file, ei->move_leave_element);
7106 putFile8Bit(file, ei->move_leave_type);
7108 // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
7109 putFile16BitBE(file, (ei->move_pattern >> 16) & 0xffff);
7111 putFile8Bit(file, ei->access_direction);
7113 putFile8Bit(file, ei->explosion_delay);
7114 putFile8Bit(file, ei->ignition_delay);
7115 putFile8Bit(file, ei->explosion_type);
7117 // some free bytes for future custom property values and padding
7118 WriteUnusedBytesToFile(file, 1);
7120 // ---------- change page property values (48 bytes) ------------------------
7122 for (i = 0; i < ei->num_change_pages; i++)
7124 struct ElementChangeInfo *change = &ei->change_page[i];
7125 unsigned int event_bits;
7127 // bits 0 - 31 of "has_event[]" ...
7129 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
7130 if (change->has_event[j])
7131 event_bits |= (1 << j);
7132 putFile32BitBE(file, event_bits);
7134 putFile16BitBE(file, change->target_element);
7136 putFile16BitBE(file, change->delay_fixed);
7137 putFile16BitBE(file, change->delay_random);
7138 putFile16BitBE(file, change->delay_frames);
7140 putFile16BitBE(file, change->initial_trigger_element);
7142 putFile8Bit(file, change->explode);
7143 putFile8Bit(file, change->use_target_content);
7144 putFile8Bit(file, change->only_if_complete);
7145 putFile8Bit(file, change->use_random_replace);
7147 putFile8Bit(file, change->random_percentage);
7148 putFile8Bit(file, change->replace_when);
7150 for (y = 0; y < 3; y++)
7151 for (x = 0; x < 3; x++)
7152 putFile16BitBE(file, change->target_content.e[x][y]);
7154 putFile8Bit(file, change->can_change);
7156 putFile8Bit(file, change->trigger_side);
7158 putFile8Bit(file, change->trigger_player);
7159 putFile8Bit(file, (change->trigger_page == CH_PAGE_ANY ? CH_PAGE_ANY_FILE :
7160 log_2(change->trigger_page)));
7162 putFile8Bit(file, change->has_action);
7163 putFile8Bit(file, change->action_type);
7164 putFile8Bit(file, change->action_mode);
7165 putFile16BitBE(file, change->action_arg);
7167 // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
7169 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
7170 if (change->has_event[j])
7171 event_bits |= (1 << (j - 32));
7172 putFile8Bit(file, event_bits);
7177 #if ENABLE_HISTORIC_CHUNKS
7178 static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
7180 struct ElementInfo *ei = &element_info[element];
7181 struct ElementGroupInfo *group = ei->group;
7184 putFile16BitBE(file, element);
7186 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7187 putFile8Bit(file, ei->description[i]);
7189 putFile8Bit(file, group->num_elements);
7191 putFile8Bit(file, ei->use_gfx_element);
7192 putFile16BitBE(file, ei->gfx_element_initial);
7194 putFile8Bit(file, group->choice_mode);
7196 // some free bytes for future values and padding
7197 WriteUnusedBytesToFile(file, 3);
7199 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
7200 putFile16BitBE(file, group->element[i]);
7204 static int SaveLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *entry,
7205 boolean write_element)
7207 int save_type = entry->save_type;
7208 int data_type = entry->data_type;
7209 int conf_type = entry->conf_type;
7210 int byte_mask = conf_type & CONF_MASK_BYTES;
7211 int element = entry->element;
7212 int default_value = entry->default_value;
7214 boolean modified = FALSE;
7216 if (byte_mask != CONF_MASK_MULTI_BYTES)
7218 void *value_ptr = entry->value;
7219 int value = (data_type == TYPE_BOOLEAN ? *(boolean *)value_ptr :
7222 // check if any settings have been modified before saving them
7223 if (value != default_value)
7226 // do not save if explicitly told or if unmodified default settings
7227 if ((save_type == SAVE_CONF_NEVER) ||
7228 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7232 num_bytes += putFile16BitBE(file, element);
7234 num_bytes += putFile8Bit(file, conf_type);
7235 num_bytes += (byte_mask == CONF_MASK_1_BYTE ? putFile8Bit (file, value) :
7236 byte_mask == CONF_MASK_2_BYTE ? putFile16BitBE(file, value) :
7237 byte_mask == CONF_MASK_4_BYTE ? putFile32BitBE(file, value) :
7240 else if (data_type == TYPE_STRING)
7242 char *default_string = entry->default_string;
7243 char *string = (char *)(entry->value);
7244 int string_length = strlen(string);
7247 // check if any settings have been modified before saving them
7248 if (!strEqual(string, default_string))
7251 // do not save if explicitly told or if unmodified default settings
7252 if ((save_type == SAVE_CONF_NEVER) ||
7253 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7257 num_bytes += putFile16BitBE(file, element);
7259 num_bytes += putFile8Bit(file, conf_type);
7260 num_bytes += putFile16BitBE(file, string_length);
7262 for (i = 0; i < string_length; i++)
7263 num_bytes += putFile8Bit(file, string[i]);
7265 else if (data_type == TYPE_ELEMENT_LIST)
7267 int *element_array = (int *)(entry->value);
7268 int num_elements = *(int *)(entry->num_entities);
7271 // check if any settings have been modified before saving them
7272 for (i = 0; i < num_elements; i++)
7273 if (element_array[i] != default_value)
7276 // do not save if explicitly told or if unmodified default settings
7277 if ((save_type == SAVE_CONF_NEVER) ||
7278 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7282 num_bytes += putFile16BitBE(file, element);
7284 num_bytes += putFile8Bit(file, conf_type);
7285 num_bytes += putFile16BitBE(file, num_elements * CONF_ELEMENT_NUM_BYTES);
7287 for (i = 0; i < num_elements; i++)
7288 num_bytes += putFile16BitBE(file, element_array[i]);
7290 else if (data_type == TYPE_CONTENT_LIST)
7292 struct Content *content = (struct Content *)(entry->value);
7293 int num_contents = *(int *)(entry->num_entities);
7296 // check if any settings have been modified before saving them
7297 for (i = 0; i < num_contents; i++)
7298 for (y = 0; y < 3; y++)
7299 for (x = 0; x < 3; x++)
7300 if (content[i].e[x][y] != default_value)
7303 // do not save if explicitly told or if unmodified default settings
7304 if ((save_type == SAVE_CONF_NEVER) ||
7305 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7309 num_bytes += putFile16BitBE(file, element);
7311 num_bytes += putFile8Bit(file, conf_type);
7312 num_bytes += putFile16BitBE(file, num_contents * CONF_CONTENT_NUM_BYTES);
7314 for (i = 0; i < num_contents; i++)
7315 for (y = 0; y < 3; y++)
7316 for (x = 0; x < 3; x++)
7317 num_bytes += putFile16BitBE(file, content[i].e[x][y]);
7323 static int SaveLevel_INFO(FILE *file, struct LevelInfo *level)
7328 li = *level; // copy level data into temporary buffer
7330 for (i = 0; chunk_config_INFO[i].data_type != -1; i++)
7331 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_INFO[i], FALSE);
7336 static int SaveLevel_ELEM(FILE *file, struct LevelInfo *level)
7341 li = *level; // copy level data into temporary buffer
7343 for (i = 0; chunk_config_ELEM[i].data_type != -1; i++)
7344 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_ELEM[i], TRUE);
7349 static int SaveLevel_NOTE(FILE *file, struct LevelInfo *level, int element)
7351 int envelope_nr = element - EL_ENVELOPE_1;
7355 chunk_size += putFile16BitBE(file, element);
7357 // copy envelope data into temporary buffer
7358 xx_envelope = level->envelope[envelope_nr];
7360 for (i = 0; chunk_config_NOTE[i].data_type != -1; i++)
7361 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_NOTE[i], FALSE);
7366 static int SaveLevel_CUSX(FILE *file, struct LevelInfo *level, int element)
7368 struct ElementInfo *ei = &element_info[element];
7372 chunk_size += putFile16BitBE(file, element);
7374 xx_ei = *ei; // copy element data into temporary buffer
7376 // set default description string for this specific element
7377 strcpy(xx_default_description, getDefaultElementDescription(ei));
7379 for (i = 0; chunk_config_CUSX_base[i].data_type != -1; i++)
7380 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_base[i], FALSE);
7382 for (i = 0; i < ei->num_change_pages; i++)
7384 struct ElementChangeInfo *change = &ei->change_page[i];
7386 xx_current_change_page = i;
7388 xx_change = *change; // copy change data into temporary buffer
7391 setEventBitsFromEventFlags(change);
7393 for (j = 0; chunk_config_CUSX_change[j].data_type != -1; j++)
7394 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_change[j],
7401 static int SaveLevel_GRPX(FILE *file, struct LevelInfo *level, int element)
7403 struct ElementInfo *ei = &element_info[element];
7404 struct ElementGroupInfo *group = ei->group;
7408 chunk_size += putFile16BitBE(file, element);
7410 xx_ei = *ei; // copy element data into temporary buffer
7411 xx_group = *group; // copy group data into temporary buffer
7413 // set default description string for this specific element
7414 strcpy(xx_default_description, getDefaultElementDescription(ei));
7416 for (i = 0; chunk_config_GRPX[i].data_type != -1; i++)
7417 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_GRPX[i], FALSE);
7422 static void SaveLevelFromFilename(struct LevelInfo *level, char *filename,
7423 boolean save_as_template)
7429 if (!(file = fopen(filename, MODE_WRITE)))
7431 Error(ERR_WARN, "cannot save level file '%s'", filename);
7435 level->file_version = FILE_VERSION_ACTUAL;
7436 level->game_version = GAME_VERSION_ACTUAL;
7438 level->creation_date = getCurrentDate();
7440 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
7441 putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
7443 chunk_size = SaveLevel_VERS(NULL, level);
7444 putFileChunkBE(file, "VERS", chunk_size);
7445 SaveLevel_VERS(file, level);
7447 chunk_size = SaveLevel_DATE(NULL, level);
7448 putFileChunkBE(file, "DATE", chunk_size);
7449 SaveLevel_DATE(file, level);
7451 chunk_size = SaveLevel_NAME(NULL, level);
7452 putFileChunkBE(file, "NAME", chunk_size);
7453 SaveLevel_NAME(file, level);
7455 chunk_size = SaveLevel_AUTH(NULL, level);
7456 putFileChunkBE(file, "AUTH", chunk_size);
7457 SaveLevel_AUTH(file, level);
7459 chunk_size = SaveLevel_INFO(NULL, level);
7460 putFileChunkBE(file, "INFO", chunk_size);
7461 SaveLevel_INFO(file, level);
7463 chunk_size = SaveLevel_BODY(NULL, level);
7464 putFileChunkBE(file, "BODY", chunk_size);
7465 SaveLevel_BODY(file, level);
7467 chunk_size = SaveLevel_ELEM(NULL, level);
7468 if (chunk_size > LEVEL_CHUNK_ELEM_UNCHANGED) // save if changed
7470 putFileChunkBE(file, "ELEM", chunk_size);
7471 SaveLevel_ELEM(file, level);
7474 for (i = 0; i < NUM_ENVELOPES; i++)
7476 int element = EL_ENVELOPE_1 + i;
7478 chunk_size = SaveLevel_NOTE(NULL, level, element);
7479 if (chunk_size > LEVEL_CHUNK_NOTE_UNCHANGED) // save if changed
7481 putFileChunkBE(file, "NOTE", chunk_size);
7482 SaveLevel_NOTE(file, level, element);
7486 // if not using template level, check for non-default custom/group elements
7487 if (!level->use_custom_template || save_as_template)
7489 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7491 int element = EL_CUSTOM_START + i;
7493 chunk_size = SaveLevel_CUSX(NULL, level, element);
7494 if (chunk_size > LEVEL_CHUNK_CUSX_UNCHANGED) // save if changed
7496 putFileChunkBE(file, "CUSX", chunk_size);
7497 SaveLevel_CUSX(file, level, element);
7501 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
7503 int element = EL_GROUP_START + i;
7505 chunk_size = SaveLevel_GRPX(NULL, level, element);
7506 if (chunk_size > LEVEL_CHUNK_GRPX_UNCHANGED) // save if changed
7508 putFileChunkBE(file, "GRPX", chunk_size);
7509 SaveLevel_GRPX(file, level, element);
7516 SetFilePermissions(filename, PERMS_PRIVATE);
7519 void SaveLevel(int nr)
7521 char *filename = getDefaultLevelFilename(nr);
7523 SaveLevelFromFilename(&level, filename, FALSE);
7526 void SaveLevelTemplate(void)
7528 char *filename = getLocalLevelTemplateFilename();
7530 SaveLevelFromFilename(&level, filename, TRUE);
7533 boolean SaveLevelChecked(int nr)
7535 char *filename = getDefaultLevelFilename(nr);
7536 boolean new_level = !fileExists(filename);
7537 boolean level_saved = FALSE;
7539 if (new_level || Request("Save this level and kill the old?", REQ_ASK))
7544 Request("Level saved!", REQ_CONFIRM);
7552 void DumpLevel(struct LevelInfo *level)
7554 if (level->no_level_file || level->no_valid_file)
7556 Error(ERR_WARN, "cannot dump -- no valid level file found");
7562 Print("Level xxx (file version %08d, game version %08d)\n",
7563 level->file_version, level->game_version);
7566 Print("Level author: '%s'\n", level->author);
7567 Print("Level title: '%s'\n", level->name);
7569 Print("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
7571 Print("Level time: %d seconds\n", level->time);
7572 Print("Gems needed: %d\n", level->gems_needed);
7574 Print("Time for magic wall: %d seconds\n", level->time_magic_wall);
7575 Print("Time for wheel: %d seconds\n", level->time_wheel);
7576 Print("Time for light: %d seconds\n", level->time_light);
7577 Print("Time for timegate: %d seconds\n", level->time_timegate);
7579 Print("Amoeba speed: %d\n", level->amoeba_speed);
7582 Print("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
7583 Print("Player blocks last field: %s\n", (level->block_last_field ? "yes" : "no"));
7584 Print("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
7585 Print("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
7586 Print("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
7592 // ============================================================================
7593 // tape file functions
7594 // ============================================================================
7596 static void setTapeInfoToDefaults(void)
7600 // always start with reliable default values (empty tape)
7603 // default values (also for pre-1.2 tapes) with only the first player
7604 tape.player_participates[0] = TRUE;
7605 for (i = 1; i < MAX_PLAYERS; i++)
7606 tape.player_participates[i] = FALSE;
7608 // at least one (default: the first) player participates in every tape
7609 tape.num_participating_players = 1;
7611 tape.level_nr = level_nr;
7613 tape.changed = FALSE;
7615 tape.recording = FALSE;
7616 tape.playing = FALSE;
7617 tape.pausing = FALSE;
7619 tape.no_valid_file = FALSE;
7622 static int getTapePosSize(struct TapeInfo *tape)
7624 int tape_pos_size = 0;
7626 if (tape->use_key_actions)
7627 tape_pos_size += tape->num_participating_players;
7629 if (tape->use_mouse_actions)
7630 tape_pos_size += 3; // x and y position and mouse button mask
7632 tape_pos_size += 1; // tape action delay value
7634 return tape_pos_size;
7637 static void setTapeActionFlags(struct TapeInfo *tape, int value)
7639 tape->use_key_actions = FALSE;
7640 tape->use_mouse_actions = FALSE;
7642 if (value != TAPE_USE_MOUSE_ACTIONS_ONLY)
7643 tape->use_key_actions = TRUE;
7645 if (value != TAPE_USE_KEY_ACTIONS_ONLY)
7646 tape->use_mouse_actions = TRUE;
7649 static int getTapeActionValue(struct TapeInfo *tape)
7651 return (tape->use_key_actions &&
7652 tape->use_mouse_actions ? TAPE_USE_KEY_AND_MOUSE_ACTIONS :
7653 tape->use_key_actions ? TAPE_USE_KEY_ACTIONS_ONLY :
7654 tape->use_mouse_actions ? TAPE_USE_MOUSE_ACTIONS_ONLY :
7655 TAPE_ACTIONS_DEFAULT);
7658 static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape)
7660 tape->file_version = getFileVersion(file);
7661 tape->game_version = getFileVersion(file);
7666 static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape)
7670 tape->random_seed = getFile32BitBE(file);
7671 tape->date = getFile32BitBE(file);
7672 tape->length = getFile32BitBE(file);
7674 // read header fields that are new since version 1.2
7675 if (tape->file_version >= FILE_VERSION_1_2)
7677 byte store_participating_players = getFile8Bit(file);
7680 // since version 1.2, tapes store which players participate in the tape
7681 tape->num_participating_players = 0;
7682 for (i = 0; i < MAX_PLAYERS; i++)
7684 tape->player_participates[i] = FALSE;
7686 if (store_participating_players & (1 << i))
7688 tape->player_participates[i] = TRUE;
7689 tape->num_participating_players++;
7693 setTapeActionFlags(tape, getFile8Bit(file));
7695 ReadUnusedBytesFromFile(file, TAPE_CHUNK_HEAD_UNUSED);
7697 engine_version = getFileVersion(file);
7698 if (engine_version > 0)
7699 tape->engine_version = engine_version;
7701 tape->engine_version = tape->game_version;
7707 static int LoadTape_INFO(File *file, int chunk_size, struct TapeInfo *tape)
7709 int level_identifier_size;
7712 level_identifier_size = getFile16BitBE(file);
7714 tape->level_identifier =
7715 checked_realloc(tape->level_identifier, level_identifier_size);
7717 for (i = 0; i < level_identifier_size; i++)
7718 tape->level_identifier[i] = getFile8Bit(file);
7720 tape->level_nr = getFile16BitBE(file);
7722 chunk_size = 2 + level_identifier_size + 2;
7727 static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
7730 int tape_pos_size = getTapePosSize(tape);
7731 int chunk_size_expected = tape_pos_size * tape->length;
7733 if (chunk_size_expected != chunk_size)
7735 ReadUnusedBytesFromFile(file, chunk_size);
7736 return chunk_size_expected;
7739 for (i = 0; i < tape->length; i++)
7741 if (i >= MAX_TAPE_LEN)
7743 Error(ERR_WARN, "tape truncated -- size exceeds maximum tape size %d",
7746 // tape too large; read and ignore remaining tape data from this chunk
7747 for (;i < tape->length; i++)
7748 ReadUnusedBytesFromFile(file, tape_pos_size);
7753 if (tape->use_key_actions)
7755 for (j = 0; j < MAX_PLAYERS; j++)
7757 tape->pos[i].action[j] = MV_NONE;
7759 if (tape->player_participates[j])
7760 tape->pos[i].action[j] = getFile8Bit(file);
7764 if (tape->use_mouse_actions)
7766 tape->pos[i].action[TAPE_ACTION_LX] = getFile8Bit(file);
7767 tape->pos[i].action[TAPE_ACTION_LY] = getFile8Bit(file);
7768 tape->pos[i].action[TAPE_ACTION_BUTTON] = getFile8Bit(file);
7771 tape->pos[i].delay = getFile8Bit(file);
7773 if (tape->file_version == FILE_VERSION_1_0)
7775 // eliminate possible diagonal moves in old tapes
7776 // this is only for backward compatibility
7778 byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
7779 byte action = tape->pos[i].action[0];
7780 int k, num_moves = 0;
7782 for (k = 0; k<4; k++)
7784 if (action & joy_dir[k])
7786 tape->pos[i + num_moves].action[0] = joy_dir[k];
7788 tape->pos[i + num_moves].delay = 0;
7797 tape->length += num_moves;
7800 else if (tape->file_version < FILE_VERSION_2_0)
7802 // convert pre-2.0 tapes to new tape format
7804 if (tape->pos[i].delay > 1)
7807 tape->pos[i + 1] = tape->pos[i];
7808 tape->pos[i + 1].delay = 1;
7811 for (j = 0; j < MAX_PLAYERS; j++)
7812 tape->pos[i].action[j] = MV_NONE;
7813 tape->pos[i].delay--;
7820 if (checkEndOfFile(file))
7824 if (i != tape->length)
7825 chunk_size = tape_pos_size * i;
7830 static void LoadTape_SokobanSolution(char *filename)
7833 int move_delay = TILESIZE / level.initial_player_stepsize[0];
7835 if (!(file = openFile(filename, MODE_READ)))
7837 tape.no_valid_file = TRUE;
7842 while (!checkEndOfFile(file))
7844 unsigned char c = getByteFromFile(file);
7846 if (checkEndOfFile(file))
7853 tape.pos[tape.length].action[0] = MV_UP;
7854 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
7860 tape.pos[tape.length].action[0] = MV_DOWN;
7861 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
7867 tape.pos[tape.length].action[0] = MV_LEFT;
7868 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
7874 tape.pos[tape.length].action[0] = MV_RIGHT;
7875 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
7883 // ignore white-space characters
7887 tape.no_valid_file = TRUE;
7889 Error(ERR_WARN, "unsupported Sokoban solution file '%s' ['%d']", filename, c);
7897 if (tape.no_valid_file)
7900 tape.length_frames = GetTapeLengthFrames();
7901 tape.length_seconds = GetTapeLengthSeconds();
7904 void LoadTapeFromFilename(char *filename)
7906 char cookie[MAX_LINE_LEN];
7907 char chunk_name[CHUNK_ID_LEN + 1];
7911 // always start with reliable default values
7912 setTapeInfoToDefaults();
7914 if (strSuffix(filename, ".sln"))
7916 LoadTape_SokobanSolution(filename);
7921 if (!(file = openFile(filename, MODE_READ)))
7923 tape.no_valid_file = TRUE;
7928 getFileChunkBE(file, chunk_name, NULL);
7929 if (strEqual(chunk_name, "RND1"))
7931 getFile32BitBE(file); // not used
7933 getFileChunkBE(file, chunk_name, NULL);
7934 if (!strEqual(chunk_name, "TAPE"))
7936 tape.no_valid_file = TRUE;
7938 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
7945 else // check for pre-2.0 file format with cookie string
7947 strcpy(cookie, chunk_name);
7948 if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
7950 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
7951 cookie[strlen(cookie) - 1] = '\0';
7953 if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
7955 tape.no_valid_file = TRUE;
7957 Error(ERR_WARN, "unknown format of tape file '%s'", filename);
7964 if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
7966 tape.no_valid_file = TRUE;
7968 Error(ERR_WARN, "unsupported version of tape file '%s'", filename);
7975 // pre-2.0 tape files have no game version, so use file version here
7976 tape.game_version = tape.file_version;
7979 if (tape.file_version < FILE_VERSION_1_2)
7981 // tape files from versions before 1.2.0 without chunk structure
7982 LoadTape_HEAD(file, TAPE_CHUNK_HEAD_SIZE, &tape);
7983 LoadTape_BODY(file, 2 * tape.length, &tape);
7991 int (*loader)(File *, int, struct TapeInfo *);
7995 { "VERS", TAPE_CHUNK_VERS_SIZE, LoadTape_VERS },
7996 { "HEAD", TAPE_CHUNK_HEAD_SIZE, LoadTape_HEAD },
7997 { "INFO", -1, LoadTape_INFO },
7998 { "BODY", -1, LoadTape_BODY },
8002 while (getFileChunkBE(file, chunk_name, &chunk_size))
8006 while (chunk_info[i].name != NULL &&
8007 !strEqual(chunk_name, chunk_info[i].name))
8010 if (chunk_info[i].name == NULL)
8012 Error(ERR_WARN, "unknown chunk '%s' in tape file '%s'",
8013 chunk_name, filename);
8014 ReadUnusedBytesFromFile(file, chunk_size);
8016 else if (chunk_info[i].size != -1 &&
8017 chunk_info[i].size != chunk_size)
8019 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
8020 chunk_size, chunk_name, filename);
8021 ReadUnusedBytesFromFile(file, chunk_size);
8025 // call function to load this tape chunk
8026 int chunk_size_expected =
8027 (chunk_info[i].loader)(file, chunk_size, &tape);
8029 // the size of some chunks cannot be checked before reading other
8030 // chunks first (like "HEAD" and "BODY") that contain some header
8031 // information, so check them here
8032 if (chunk_size_expected != chunk_size)
8034 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
8035 chunk_size, chunk_name, filename);
8043 tape.length_frames = GetTapeLengthFrames();
8044 tape.length_seconds = GetTapeLengthSeconds();
8047 printf("::: tape file version: %d\n", tape.file_version);
8048 printf("::: tape game version: %d\n", tape.game_version);
8049 printf("::: tape engine version: %d\n", tape.engine_version);
8053 void LoadTape(int nr)
8055 char *filename = getTapeFilename(nr);
8057 LoadTapeFromFilename(filename);
8060 void LoadSolutionTape(int nr)
8062 char *filename = getSolutionTapeFilename(nr);
8064 LoadTapeFromFilename(filename);
8066 if (TAPE_IS_EMPTY(tape) &&
8067 level.game_engine_type == GAME_ENGINE_TYPE_SP &&
8068 level.native_sp_level->demo.is_available)
8069 CopyNativeTape_SP_to_RND(&level);
8072 static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
8074 putFileVersion(file, tape->file_version);
8075 putFileVersion(file, tape->game_version);
8078 static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
8081 byte store_participating_players = 0;
8083 // set bits for participating players for compact storage
8084 for (i = 0; i < MAX_PLAYERS; i++)
8085 if (tape->player_participates[i])
8086 store_participating_players |= (1 << i);
8088 putFile32BitBE(file, tape->random_seed);
8089 putFile32BitBE(file, tape->date);
8090 putFile32BitBE(file, tape->length);
8092 putFile8Bit(file, store_participating_players);
8094 putFile8Bit(file, getTapeActionValue(tape));
8096 // unused bytes not at the end here for 4-byte alignment of engine_version
8097 WriteUnusedBytesToFile(file, TAPE_CHUNK_HEAD_UNUSED);
8099 putFileVersion(file, tape->engine_version);
8102 static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
8104 int level_identifier_size = strlen(tape->level_identifier) + 1;
8107 putFile16BitBE(file, level_identifier_size);
8109 for (i = 0; i < level_identifier_size; i++)
8110 putFile8Bit(file, tape->level_identifier[i]);
8112 putFile16BitBE(file, tape->level_nr);
8115 static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
8119 for (i = 0; i < tape->length; i++)
8121 if (tape->use_key_actions)
8123 for (j = 0; j < MAX_PLAYERS; j++)
8124 if (tape->player_participates[j])
8125 putFile8Bit(file, tape->pos[i].action[j]);
8128 if (tape->use_mouse_actions)
8130 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LX]);
8131 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LY]);
8132 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_BUTTON]);
8135 putFile8Bit(file, tape->pos[i].delay);
8139 void SaveTape(int nr)
8141 char *filename = getTapeFilename(nr);
8144 int info_chunk_size;
8145 int body_chunk_size;
8148 InitTapeDirectory(leveldir_current->subdir);
8150 if (!(file = fopen(filename, MODE_WRITE)))
8152 Error(ERR_WARN, "cannot save level recording file '%s'", filename);
8156 tape.file_version = FILE_VERSION_ACTUAL;
8157 tape.game_version = GAME_VERSION_ACTUAL;
8159 tape.num_participating_players = 0;
8161 // count number of participating players
8162 for (i = 0; i < MAX_PLAYERS; i++)
8163 if (tape.player_participates[i])
8164 tape.num_participating_players++;
8166 tape_pos_size = getTapePosSize(&tape);
8168 info_chunk_size = 2 + (strlen(tape.level_identifier) + 1) + 2;
8169 body_chunk_size = tape_pos_size * tape.length;
8171 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
8172 putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
8174 putFileChunkBE(file, "VERS", TAPE_CHUNK_VERS_SIZE);
8175 SaveTape_VERS(file, &tape);
8177 putFileChunkBE(file, "HEAD", TAPE_CHUNK_HEAD_SIZE);
8178 SaveTape_HEAD(file, &tape);
8180 putFileChunkBE(file, "INFO", info_chunk_size);
8181 SaveTape_INFO(file, &tape);
8183 putFileChunkBE(file, "BODY", body_chunk_size);
8184 SaveTape_BODY(file, &tape);
8188 SetFilePermissions(filename, PERMS_PRIVATE);
8190 tape.changed = FALSE;
8193 static boolean SaveTapeCheckedExt(int nr, char *msg_replace, char *msg_saved,
8194 unsigned int req_state_added)
8196 char *filename = getTapeFilename(nr);
8197 boolean new_tape = !fileExists(filename);
8198 boolean tape_saved = FALSE;
8200 if (new_tape || Request(msg_replace, REQ_ASK | req_state_added))
8205 Request(msg_saved, REQ_CONFIRM | req_state_added);
8213 boolean SaveTapeChecked(int nr)
8215 return SaveTapeCheckedExt(nr, "Replace old tape?", "Tape saved!", 0);
8218 boolean SaveTapeChecked_LevelSolved(int nr)
8220 return SaveTapeCheckedExt(nr, "Level solved! Replace old tape?",
8221 "Level solved! Tape saved!", REQ_STAY_OPEN);
8224 void DumpTape(struct TapeInfo *tape)
8226 int tape_frame_counter;
8229 if (tape->no_valid_file)
8231 Error(ERR_WARN, "cannot dump -- no valid tape file found");
8237 Print("Tape of Level %03d (file version %08d, game version %08d)\n",
8238 tape->level_nr, tape->file_version, tape->game_version);
8239 Print(" (effective engine version %08d)\n",
8240 tape->engine_version);
8241 Print("Level series identifier: '%s'\n", tape->level_identifier);
8244 tape_frame_counter = 0;
8246 for (i = 0; i < tape->length; i++)
8248 if (i >= MAX_TAPE_LEN)
8253 for (j = 0; j < MAX_PLAYERS; j++)
8255 if (tape->player_participates[j])
8257 int action = tape->pos[i].action[j];
8259 Print("%d:%02x ", j, action);
8260 Print("[%c%c%c%c|%c%c] - ",
8261 (action & JOY_LEFT ? '<' : ' '),
8262 (action & JOY_RIGHT ? '>' : ' '),
8263 (action & JOY_UP ? '^' : ' '),
8264 (action & JOY_DOWN ? 'v' : ' '),
8265 (action & JOY_BUTTON_1 ? '1' : ' '),
8266 (action & JOY_BUTTON_2 ? '2' : ' '));
8270 Print("(%03d) ", tape->pos[i].delay);
8271 Print("[%05d]\n", tape_frame_counter);
8273 tape_frame_counter += tape->pos[i].delay;
8280 // ============================================================================
8281 // score file functions
8282 // ============================================================================
8284 void LoadScore(int nr)
8287 char *filename = getScoreFilename(nr);
8288 char cookie[MAX_LINE_LEN];
8289 char line[MAX_LINE_LEN];
8293 // always start with reliable default values
8294 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8296 strcpy(highscore[i].Name, EMPTY_PLAYER_NAME);
8297 highscore[i].Score = 0;
8300 if (!(file = fopen(filename, MODE_READ)))
8303 // check file identifier
8304 if (fgets(cookie, MAX_LINE_LEN, file) == NULL)
8306 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8307 cookie[strlen(cookie) - 1] = '\0';
8309 if (!checkCookieString(cookie, SCORE_COOKIE))
8311 Error(ERR_WARN, "unknown format of score file '%s'", filename);
8316 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8318 if (fscanf(file, "%d", &highscore[i].Score) == EOF)
8319 Error(ERR_WARN, "fscanf() failed; %s", strerror(errno));
8320 if (fgets(line, MAX_LINE_LEN, file) == NULL)
8323 if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
8324 line[strlen(line) - 1] = '\0';
8326 for (line_ptr = line; *line_ptr; line_ptr++)
8328 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
8330 strncpy(highscore[i].Name, line_ptr, MAX_PLAYER_NAME_LEN);
8331 highscore[i].Name[MAX_PLAYER_NAME_LEN] = '\0';
8340 void SaveScore(int nr)
8343 int permissions = (program.global_scores ? PERMS_PUBLIC : PERMS_PRIVATE);
8344 char *filename = getScoreFilename(nr);
8347 // used instead of "leveldir_current->subdir" (for network games)
8348 InitScoreDirectory(levelset.identifier);
8350 if (!(file = fopen(filename, MODE_WRITE)))
8352 Error(ERR_WARN, "cannot save score for level %d", nr);
8356 fprintf(file, "%s\n\n", SCORE_COOKIE);
8358 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8359 fprintf(file, "%d %s\n", highscore[i].Score, highscore[i].Name);
8363 SetFilePermissions(filename, permissions);
8367 // ============================================================================
8368 // setup file functions
8369 // ============================================================================
8371 #define TOKEN_STR_PLAYER_PREFIX "player_"
8374 static struct TokenInfo global_setup_tokens[] =
8378 &setup.player_name, "player_name"
8382 &setup.sound, "sound"
8386 &setup.sound_loops, "repeating_sound_loops"
8390 &setup.sound_music, "background_music"
8394 &setup.sound_simple, "simple_sound_effects"
8398 &setup.toons, "toons"
8402 &setup.scroll_delay, "scroll_delay"
8406 &setup.forced_scroll_delay, "forced_scroll_delay"
8410 &setup.scroll_delay_value, "scroll_delay_value"
8414 &setup.engine_snapshot_mode, "engine_snapshot_mode"
8418 &setup.engine_snapshot_memory, "engine_snapshot_memory"
8422 &setup.fade_screens, "fade_screens"
8426 &setup.autorecord, "automatic_tape_recording"
8430 &setup.show_titlescreen, "show_titlescreen"
8434 &setup.quick_doors, "quick_doors"
8438 &setup.team_mode, "team_mode"
8442 &setup.handicap, "handicap"
8446 &setup.skip_levels, "skip_levels"
8450 &setup.increment_levels, "increment_levels"
8454 &setup.auto_play_next_level, "auto_play_next_level"
8458 &setup.skip_scores_after_game, "skip_scores_after_game"
8462 &setup.time_limit, "time_limit"
8466 &setup.fullscreen, "fullscreen"
8470 &setup.window_scaling_percent, "window_scaling_percent"
8474 &setup.window_scaling_quality, "window_scaling_quality"
8478 &setup.screen_rendering_mode, "screen_rendering_mode"
8482 &setup.vsync_mode, "vsync_mode"
8486 &setup.ask_on_escape, "ask_on_escape"
8490 &setup.ask_on_escape_editor, "ask_on_escape_editor"
8494 &setup.ask_on_game_over, "ask_on_game_over"
8498 &setup.quick_switch, "quick_player_switch"
8502 &setup.input_on_focus, "input_on_focus"
8506 &setup.prefer_aga_graphics, "prefer_aga_graphics"
8510 &setup.prefer_lowpass_sounds, "prefer_lowpass_sounds"
8514 &setup.game_speed_extended, "game_speed_extended"
8518 &setup.game_frame_delay, "game_frame_delay"
8522 &setup.sp_show_border_elements, "sp_show_border_elements"
8526 &setup.small_game_graphics, "small_game_graphics"
8530 &setup.show_snapshot_buttons, "show_snapshot_buttons"
8534 &setup.graphics_set, "graphics_set"
8538 &setup.sounds_set, "sounds_set"
8542 &setup.music_set, "music_set"
8546 &setup.override_level_graphics, "override_level_graphics"
8550 &setup.override_level_sounds, "override_level_sounds"
8554 &setup.override_level_music, "override_level_music"
8558 &setup.volume_simple, "volume_simple"
8562 &setup.volume_loops, "volume_loops"
8566 &setup.volume_music, "volume_music"
8570 &setup.network_mode, "network_mode"
8574 &setup.network_player_nr, "network_player"
8578 &setup.network_server_hostname, "network_server_hostname"
8582 &setup.touch.control_type, "touch.control_type"
8586 &setup.touch.move_distance, "touch.move_distance"
8590 &setup.touch.drop_distance, "touch.drop_distance"
8594 &setup.touch.transparency, "touch.transparency"
8598 &setup.touch.draw_outlined, "touch.draw_outlined"
8602 &setup.touch.draw_pressed, "touch.draw_pressed"
8606 &setup.touch.grid_xsize[0], "touch.virtual_buttons.0.xsize"
8610 &setup.touch.grid_ysize[0], "touch.virtual_buttons.0.ysize"
8614 &setup.touch.grid_xsize[1], "touch.virtual_buttons.1.xsize"
8618 &setup.touch.grid_ysize[1], "touch.virtual_buttons.1.ysize"
8622 static struct TokenInfo auto_setup_tokens[] =
8626 &setup.auto_setup.editor_zoom_tilesize, "editor.zoom_tilesize"
8630 static struct TokenInfo editor_setup_tokens[] =
8634 &setup.editor.el_classic, "editor.el_classic"
8638 &setup.editor.el_custom, "editor.el_custom"
8642 &setup.editor.el_user_defined, "editor.el_user_defined"
8646 &setup.editor.el_dynamic, "editor.el_dynamic"
8650 &setup.editor.el_headlines, "editor.el_headlines"
8654 &setup.editor.show_element_token, "editor.show_element_token"
8658 static struct TokenInfo editor_cascade_setup_tokens[] =
8662 &setup.editor_cascade.el_bd, "editor.cascade.el_bd"
8666 &setup.editor_cascade.el_em, "editor.cascade.el_em"
8670 &setup.editor_cascade.el_emc, "editor.cascade.el_emc"
8674 &setup.editor_cascade.el_rnd, "editor.cascade.el_rnd"
8678 &setup.editor_cascade.el_sb, "editor.cascade.el_sb"
8682 &setup.editor_cascade.el_sp, "editor.cascade.el_sp"
8686 &setup.editor_cascade.el_dc, "editor.cascade.el_dc"
8690 &setup.editor_cascade.el_dx, "editor.cascade.el_dx"
8694 &setup.editor_cascade.el_mm, "editor.cascade.el_mm"
8698 &setup.editor_cascade.el_df, "editor.cascade.el_df"
8702 &setup.editor_cascade.el_chars, "editor.cascade.el_chars"
8706 &setup.editor_cascade.el_steel_chars, "editor.cascade.el_steel_chars"
8710 &setup.editor_cascade.el_ce, "editor.cascade.el_ce"
8714 &setup.editor_cascade.el_ge, "editor.cascade.el_ge"
8718 &setup.editor_cascade.el_ref, "editor.cascade.el_ref"
8722 &setup.editor_cascade.el_user, "editor.cascade.el_user"
8726 &setup.editor_cascade.el_dynamic, "editor.cascade.el_dynamic"
8730 static struct TokenInfo shortcut_setup_tokens[] =
8734 &setup.shortcut.save_game, "shortcut.save_game"
8738 &setup.shortcut.load_game, "shortcut.load_game"
8742 &setup.shortcut.toggle_pause, "shortcut.toggle_pause"
8746 &setup.shortcut.focus_player[0], "shortcut.focus_player_1"
8750 &setup.shortcut.focus_player[1], "shortcut.focus_player_2"
8754 &setup.shortcut.focus_player[2], "shortcut.focus_player_3"
8758 &setup.shortcut.focus_player[3], "shortcut.focus_player_4"
8762 &setup.shortcut.focus_player_all, "shortcut.focus_player_all"
8766 &setup.shortcut.tape_eject, "shortcut.tape_eject"
8770 &setup.shortcut.tape_extra, "shortcut.tape_extra"
8774 &setup.shortcut.tape_stop, "shortcut.tape_stop"
8778 &setup.shortcut.tape_pause, "shortcut.tape_pause"
8782 &setup.shortcut.tape_record, "shortcut.tape_record"
8786 &setup.shortcut.tape_play, "shortcut.tape_play"
8790 &setup.shortcut.sound_simple, "shortcut.sound_simple"
8794 &setup.shortcut.sound_loops, "shortcut.sound_loops"
8798 &setup.shortcut.sound_music, "shortcut.sound_music"
8802 &setup.shortcut.snap_left, "shortcut.snap_left"
8806 &setup.shortcut.snap_right, "shortcut.snap_right"
8810 &setup.shortcut.snap_up, "shortcut.snap_up"
8814 &setup.shortcut.snap_down, "shortcut.snap_down"
8818 static struct SetupInputInfo setup_input;
8819 static struct TokenInfo player_setup_tokens[] =
8823 &setup_input.use_joystick, ".use_joystick"
8827 &setup_input.joy.device_name, ".joy.device_name"
8831 &setup_input.joy.xleft, ".joy.xleft"
8835 &setup_input.joy.xmiddle, ".joy.xmiddle"
8839 &setup_input.joy.xright, ".joy.xright"
8843 &setup_input.joy.yupper, ".joy.yupper"
8847 &setup_input.joy.ymiddle, ".joy.ymiddle"
8851 &setup_input.joy.ylower, ".joy.ylower"
8855 &setup_input.joy.snap, ".joy.snap_field"
8859 &setup_input.joy.drop, ".joy.place_bomb"
8863 &setup_input.key.left, ".key.move_left"
8867 &setup_input.key.right, ".key.move_right"
8871 &setup_input.key.up, ".key.move_up"
8875 &setup_input.key.down, ".key.move_down"
8879 &setup_input.key.snap, ".key.snap_field"
8883 &setup_input.key.drop, ".key.place_bomb"
8887 static struct TokenInfo system_setup_tokens[] =
8891 &setup.system.sdl_videodriver, "system.sdl_videodriver"
8895 &setup.system.sdl_audiodriver, "system.sdl_audiodriver"
8899 &setup.system.audio_fragment_size, "system.audio_fragment_size"
8903 static struct TokenInfo internal_setup_tokens[] =
8907 &setup.internal.program_title, "program_title"
8911 &setup.internal.program_version, "program_version"
8915 &setup.internal.program_author, "program_author"
8919 &setup.internal.program_email, "program_email"
8923 &setup.internal.program_website, "program_website"
8927 &setup.internal.program_copyright, "program_copyright"
8931 &setup.internal.program_company, "program_company"
8935 &setup.internal.program_icon_file, "program_icon_file"
8939 &setup.internal.default_graphics_set, "default_graphics_set"
8943 &setup.internal.default_sounds_set, "default_sounds_set"
8947 &setup.internal.default_music_set, "default_music_set"
8951 &setup.internal.fallback_graphics_file, "fallback_graphics_file"
8955 &setup.internal.fallback_sounds_file, "fallback_sounds_file"
8959 &setup.internal.fallback_music_file, "fallback_music_file"
8963 &setup.internal.default_level_series, "default_level_series"
8967 &setup.internal.default_window_width, "default_window_width"
8971 &setup.internal.default_window_height, "default_window_height"
8975 &setup.internal.choose_from_top_leveldir, "choose_from_top_leveldir"
8979 &setup.internal.show_scaling_in_title, "show_scaling_in_title"
8983 &setup.internal.create_user_levelset, "create_user_levelset"
8987 &setup.internal.menu_game, "menu_game"
8991 &setup.internal.menu_editor, "menu_editor"
8995 &setup.internal.menu_graphics, "menu_graphics"
8999 &setup.internal.menu_sound, "menu_sound"
9003 &setup.internal.menu_artwork, "menu_artwork"
9007 &setup.internal.menu_input, "menu_input"
9011 &setup.internal.menu_touch, "menu_touch"
9015 &setup.internal.menu_shortcuts, "menu_shortcuts"
9019 &setup.internal.menu_exit, "menu_exit"
9023 &setup.internal.menu_save_and_exit, "menu_save_and_exit"
9027 static struct TokenInfo debug_setup_tokens[] =
9031 &setup.debug.frame_delay[0], "debug.frame_delay_0"
9035 &setup.debug.frame_delay[1], "debug.frame_delay_1"
9039 &setup.debug.frame_delay[2], "debug.frame_delay_2"
9043 &setup.debug.frame_delay[3], "debug.frame_delay_3"
9047 &setup.debug.frame_delay[4], "debug.frame_delay_4"
9051 &setup.debug.frame_delay[5], "debug.frame_delay_5"
9055 &setup.debug.frame_delay[6], "debug.frame_delay_6"
9059 &setup.debug.frame_delay[7], "debug.frame_delay_7"
9063 &setup.debug.frame_delay[8], "debug.frame_delay_8"
9067 &setup.debug.frame_delay[9], "debug.frame_delay_9"
9071 &setup.debug.frame_delay_key[0], "debug.key.frame_delay_0"
9075 &setup.debug.frame_delay_key[1], "debug.key.frame_delay_1"
9079 &setup.debug.frame_delay_key[2], "debug.key.frame_delay_2"
9083 &setup.debug.frame_delay_key[3], "debug.key.frame_delay_3"
9087 &setup.debug.frame_delay_key[4], "debug.key.frame_delay_4"
9091 &setup.debug.frame_delay_key[5], "debug.key.frame_delay_5"
9095 &setup.debug.frame_delay_key[6], "debug.key.frame_delay_6"
9099 &setup.debug.frame_delay_key[7], "debug.key.frame_delay_7"
9103 &setup.debug.frame_delay_key[8], "debug.key.frame_delay_8"
9107 &setup.debug.frame_delay_key[9], "debug.key.frame_delay_9"
9111 &setup.debug.frame_delay_use_mod_key, "debug.frame_delay.use_mod_key"},
9114 &setup.debug.frame_delay_game_only, "debug.frame_delay.game_only"
9118 &setup.debug.show_frames_per_second, "debug.show_frames_per_second"
9122 static struct TokenInfo options_setup_tokens[] =
9126 &setup.options.verbose, "options.verbose"
9130 static char *get_corrected_login_name(char *login_name)
9132 // needed because player name must be a fixed length string
9133 char *login_name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1);
9135 strncpy(login_name_new, login_name, MAX_PLAYER_NAME_LEN);
9136 login_name_new[MAX_PLAYER_NAME_LEN] = '\0';
9138 if (strlen(login_name) > MAX_PLAYER_NAME_LEN) // name has been cut
9139 if (strchr(login_name_new, ' '))
9140 *strchr(login_name_new, ' ') = '\0';
9142 return login_name_new;
9145 static void setSetupInfoToDefaults(struct SetupInfo *si)
9149 si->player_name = get_corrected_login_name(getLoginName());
9152 si->sound_loops = TRUE;
9153 si->sound_music = TRUE;
9154 si->sound_simple = TRUE;
9156 si->scroll_delay = TRUE;
9157 si->forced_scroll_delay = FALSE;
9158 si->scroll_delay_value = STD_SCROLL_DELAY;
9159 si->engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_DEFAULT);
9160 si->engine_snapshot_memory = SNAPSHOT_MEMORY_DEFAULT;
9161 si->fade_screens = TRUE;
9162 si->autorecord = TRUE;
9163 si->show_titlescreen = TRUE;
9164 si->quick_doors = FALSE;
9165 si->team_mode = FALSE;
9166 si->handicap = TRUE;
9167 si->skip_levels = TRUE;
9168 si->increment_levels = TRUE;
9169 si->auto_play_next_level = TRUE;
9170 si->skip_scores_after_game = FALSE;
9171 si->time_limit = TRUE;
9172 si->fullscreen = FALSE;
9173 si->window_scaling_percent = STD_WINDOW_SCALING_PERCENT;
9174 si->window_scaling_quality = getStringCopy(SCALING_QUALITY_DEFAULT);
9175 si->screen_rendering_mode = getStringCopy(STR_SPECIAL_RENDERING_DEFAULT);
9176 si->vsync_mode = getStringCopy(STR_VSYNC_MODE_DEFAULT);
9177 si->ask_on_escape = TRUE;
9178 si->ask_on_escape_editor = TRUE;
9179 si->ask_on_game_over = TRUE;
9180 si->quick_switch = FALSE;
9181 si->input_on_focus = FALSE;
9182 si->prefer_aga_graphics = TRUE;
9183 si->prefer_lowpass_sounds = FALSE;
9184 si->game_speed_extended = FALSE;
9185 si->game_frame_delay = GAME_FRAME_DELAY;
9186 si->sp_show_border_elements = FALSE;
9187 si->small_game_graphics = FALSE;
9188 si->show_snapshot_buttons = FALSE;
9190 si->graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
9191 si->sounds_set = getStringCopy(SND_CLASSIC_SUBDIR);
9192 si->music_set = getStringCopy(MUS_CLASSIC_SUBDIR);
9194 si->override_level_graphics = FALSE;
9195 si->override_level_sounds = FALSE;
9196 si->override_level_music = FALSE;
9198 si->volume_simple = 100; // percent
9199 si->volume_loops = 100; // percent
9200 si->volume_music = 100; // percent
9202 si->network_mode = FALSE;
9203 si->network_player_nr = 0; // first player
9204 si->network_server_hostname = getStringCopy(STR_NETWORK_AUTO_DETECT);
9206 si->touch.control_type = getStringCopy(TOUCH_CONTROL_DEFAULT);
9207 si->touch.move_distance = TOUCH_MOVE_DISTANCE_DEFAULT; // percent
9208 si->touch.drop_distance = TOUCH_DROP_DISTANCE_DEFAULT; // percent
9209 si->touch.transparency = TOUCH_TRANSPARENCY_DEFAULT; // percent
9210 si->touch.draw_outlined = TRUE;
9211 si->touch.draw_pressed = TRUE;
9213 for (i = 0; i < 2; i++)
9215 char *default_grid_button[6][2] =
9221 { "111222", " vv " },
9222 { "111222", " vv " }
9224 int grid_xsize = DEFAULT_GRID_XSIZE(i);
9225 int grid_ysize = DEFAULT_GRID_YSIZE(i);
9226 int min_xsize = MIN(6, grid_xsize);
9227 int min_ysize = MIN(6, grid_ysize);
9228 int startx = grid_xsize - min_xsize;
9229 int starty = grid_ysize - min_ysize;
9232 // virtual buttons grid can only be set to defaults if video is initialized
9233 // (this will be repeated if virtual buttons are not loaded from setup file)
9234 if (video.initialized)
9236 si->touch.grid_xsize[i] = grid_xsize;
9237 si->touch.grid_ysize[i] = grid_ysize;
9241 si->touch.grid_xsize[i] = -1;
9242 si->touch.grid_ysize[i] = -1;
9245 for (x = 0; x < MAX_GRID_XSIZE; x++)
9246 for (y = 0; y < MAX_GRID_YSIZE; y++)
9247 si->touch.grid_button[i][x][y] = CHAR_GRID_BUTTON_NONE;
9249 for (x = 0; x < min_xsize; x++)
9250 for (y = 0; y < min_ysize; y++)
9251 si->touch.grid_button[i][x][starty + y] =
9252 default_grid_button[y][0][x];
9254 for (x = 0; x < min_xsize; x++)
9255 for (y = 0; y < min_ysize; y++)
9256 si->touch.grid_button[i][startx + x][starty + y] =
9257 default_grid_button[y][1][x];
9260 si->touch.grid_initialized = video.initialized;
9262 si->editor.el_boulderdash = TRUE;
9263 si->editor.el_emerald_mine = TRUE;
9264 si->editor.el_emerald_mine_club = TRUE;
9265 si->editor.el_more = TRUE;
9266 si->editor.el_sokoban = TRUE;
9267 si->editor.el_supaplex = TRUE;
9268 si->editor.el_diamond_caves = TRUE;
9269 si->editor.el_dx_boulderdash = TRUE;
9271 si->editor.el_mirror_magic = TRUE;
9272 si->editor.el_deflektor = TRUE;
9274 si->editor.el_chars = TRUE;
9275 si->editor.el_steel_chars = TRUE;
9277 si->editor.el_classic = TRUE;
9278 si->editor.el_custom = TRUE;
9280 si->editor.el_user_defined = FALSE;
9281 si->editor.el_dynamic = TRUE;
9283 si->editor.el_headlines = TRUE;
9285 si->editor.show_element_token = FALSE;
9287 si->editor.use_template_for_new_levels = TRUE;
9289 si->shortcut.save_game = DEFAULT_KEY_SAVE_GAME;
9290 si->shortcut.load_game = DEFAULT_KEY_LOAD_GAME;
9291 si->shortcut.toggle_pause = DEFAULT_KEY_TOGGLE_PAUSE;
9293 si->shortcut.focus_player[0] = DEFAULT_KEY_FOCUS_PLAYER_1;
9294 si->shortcut.focus_player[1] = DEFAULT_KEY_FOCUS_PLAYER_2;
9295 si->shortcut.focus_player[2] = DEFAULT_KEY_FOCUS_PLAYER_3;
9296 si->shortcut.focus_player[3] = DEFAULT_KEY_FOCUS_PLAYER_4;
9297 si->shortcut.focus_player_all = DEFAULT_KEY_FOCUS_PLAYER_ALL;
9299 si->shortcut.tape_eject = DEFAULT_KEY_TAPE_EJECT;
9300 si->shortcut.tape_extra = DEFAULT_KEY_TAPE_EXTRA;
9301 si->shortcut.tape_stop = DEFAULT_KEY_TAPE_STOP;
9302 si->shortcut.tape_pause = DEFAULT_KEY_TAPE_PAUSE;
9303 si->shortcut.tape_record = DEFAULT_KEY_TAPE_RECORD;
9304 si->shortcut.tape_play = DEFAULT_KEY_TAPE_PLAY;
9306 si->shortcut.sound_simple = DEFAULT_KEY_SOUND_SIMPLE;
9307 si->shortcut.sound_loops = DEFAULT_KEY_SOUND_LOOPS;
9308 si->shortcut.sound_music = DEFAULT_KEY_SOUND_MUSIC;
9310 si->shortcut.snap_left = DEFAULT_KEY_SNAP_LEFT;
9311 si->shortcut.snap_right = DEFAULT_KEY_SNAP_RIGHT;
9312 si->shortcut.snap_up = DEFAULT_KEY_SNAP_UP;
9313 si->shortcut.snap_down = DEFAULT_KEY_SNAP_DOWN;
9315 for (i = 0; i < MAX_PLAYERS; i++)
9317 si->input[i].use_joystick = FALSE;
9318 si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i));
9319 si->input[i].joy.xleft = JOYSTICK_XLEFT;
9320 si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE;
9321 si->input[i].joy.xright = JOYSTICK_XRIGHT;
9322 si->input[i].joy.yupper = JOYSTICK_YUPPER;
9323 si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE;
9324 si->input[i].joy.ylower = JOYSTICK_YLOWER;
9325 si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0);
9326 si->input[i].joy.drop = (i == 0 ? JOY_BUTTON_2 : 0);
9327 si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED);
9328 si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED);
9329 si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED);
9330 si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED);
9331 si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED);
9332 si->input[i].key.drop = (i == 0 ? DEFAULT_KEY_DROP : KSYM_UNDEFINED);
9335 si->system.sdl_videodriver = getStringCopy(ARG_DEFAULT);
9336 si->system.sdl_audiodriver = getStringCopy(ARG_DEFAULT);
9337 si->system.audio_fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
9339 si->internal.program_title = getStringCopy(PROGRAM_TITLE_STRING);
9340 si->internal.program_version = getStringCopy(getProgramRealVersionString());
9341 si->internal.program_author = getStringCopy(PROGRAM_AUTHOR_STRING);
9342 si->internal.program_email = getStringCopy(PROGRAM_EMAIL_STRING);
9343 si->internal.program_website = getStringCopy(PROGRAM_WEBSITE_STRING);
9344 si->internal.program_copyright = getStringCopy(PROGRAM_COPYRIGHT_STRING);
9345 si->internal.program_company = getStringCopy(PROGRAM_COMPANY_STRING);
9347 si->internal.program_icon_file = getStringCopy(PROGRAM_ICON_FILENAME);
9349 si->internal.default_graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
9350 si->internal.default_sounds_set = getStringCopy(SND_CLASSIC_SUBDIR);
9351 si->internal.default_music_set = getStringCopy(MUS_CLASSIC_SUBDIR);
9353 si->internal.fallback_graphics_file = getStringCopy(UNDEFINED_FILENAME);
9354 si->internal.fallback_sounds_file = getStringCopy(UNDEFINED_FILENAME);
9355 si->internal.fallback_music_file = getStringCopy(UNDEFINED_FILENAME);
9357 si->internal.default_level_series = getStringCopy(UNDEFINED_LEVELSET);
9358 si->internal.choose_from_top_leveldir = FALSE;
9359 si->internal.show_scaling_in_title = TRUE;
9360 si->internal.create_user_levelset = TRUE;
9362 si->internal.default_window_width = WIN_XSIZE_DEFAULT;
9363 si->internal.default_window_height = WIN_YSIZE_DEFAULT;
9365 si->debug.frame_delay[0] = DEFAULT_FRAME_DELAY_0;
9366 si->debug.frame_delay[1] = DEFAULT_FRAME_DELAY_1;
9367 si->debug.frame_delay[2] = DEFAULT_FRAME_DELAY_2;
9368 si->debug.frame_delay[3] = DEFAULT_FRAME_DELAY_3;
9369 si->debug.frame_delay[4] = DEFAULT_FRAME_DELAY_4;
9370 si->debug.frame_delay[5] = DEFAULT_FRAME_DELAY_5;
9371 si->debug.frame_delay[6] = DEFAULT_FRAME_DELAY_6;
9372 si->debug.frame_delay[7] = DEFAULT_FRAME_DELAY_7;
9373 si->debug.frame_delay[8] = DEFAULT_FRAME_DELAY_8;
9374 si->debug.frame_delay[9] = DEFAULT_FRAME_DELAY_9;
9376 si->debug.frame_delay_key[0] = DEFAULT_KEY_FRAME_DELAY_0;
9377 si->debug.frame_delay_key[1] = DEFAULT_KEY_FRAME_DELAY_1;
9378 si->debug.frame_delay_key[2] = DEFAULT_KEY_FRAME_DELAY_2;
9379 si->debug.frame_delay_key[3] = DEFAULT_KEY_FRAME_DELAY_3;
9380 si->debug.frame_delay_key[4] = DEFAULT_KEY_FRAME_DELAY_4;
9381 si->debug.frame_delay_key[5] = DEFAULT_KEY_FRAME_DELAY_5;
9382 si->debug.frame_delay_key[6] = DEFAULT_KEY_FRAME_DELAY_6;
9383 si->debug.frame_delay_key[7] = DEFAULT_KEY_FRAME_DELAY_7;
9384 si->debug.frame_delay_key[8] = DEFAULT_KEY_FRAME_DELAY_8;
9385 si->debug.frame_delay_key[9] = DEFAULT_KEY_FRAME_DELAY_9;
9387 si->debug.frame_delay_use_mod_key = DEFAULT_FRAME_DELAY_USE_MOD_KEY;
9388 si->debug.frame_delay_game_only = DEFAULT_FRAME_DELAY_GAME_ONLY;
9390 si->debug.show_frames_per_second = FALSE;
9392 si->options.verbose = FALSE;
9394 #if defined(PLATFORM_ANDROID)
9395 si->fullscreen = TRUE;
9399 static void setSetupInfoToDefaults_AutoSetup(struct SetupInfo *si)
9401 si->auto_setup.editor_zoom_tilesize = MINI_TILESIZE;
9404 static void setSetupInfoToDefaults_EditorCascade(struct SetupInfo *si)
9406 si->editor_cascade.el_bd = TRUE;
9407 si->editor_cascade.el_em = TRUE;
9408 si->editor_cascade.el_emc = TRUE;
9409 si->editor_cascade.el_rnd = TRUE;
9410 si->editor_cascade.el_sb = TRUE;
9411 si->editor_cascade.el_sp = TRUE;
9412 si->editor_cascade.el_dc = TRUE;
9413 si->editor_cascade.el_dx = TRUE;
9415 si->editor_cascade.el_mm = TRUE;
9416 si->editor_cascade.el_df = TRUE;
9418 si->editor_cascade.el_chars = FALSE;
9419 si->editor_cascade.el_steel_chars = FALSE;
9420 si->editor_cascade.el_ce = FALSE;
9421 si->editor_cascade.el_ge = FALSE;
9422 si->editor_cascade.el_ref = FALSE;
9423 si->editor_cascade.el_user = FALSE;
9424 si->editor_cascade.el_dynamic = FALSE;
9427 #define MAX_HIDE_SETUP_TOKEN_SIZE 20
9429 static char *getHideSetupToken(void *setup_value)
9431 static char hide_setup_token[MAX_HIDE_SETUP_TOKEN_SIZE];
9433 if (setup_value != NULL)
9434 snprintf(hide_setup_token, MAX_HIDE_SETUP_TOKEN_SIZE, "%p", setup_value);
9436 return hide_setup_token;
9439 void setHideSetupEntry(void *setup_value)
9441 char *hide_setup_token = getHideSetupToken(setup_value);
9443 if (setup_value != NULL)
9444 setHashEntry(hide_setup_hash, hide_setup_token, "");
9447 boolean hideSetupEntry(void *setup_value)
9449 char *hide_setup_token = getHideSetupToken(setup_value);
9451 return (setup_value != NULL &&
9452 getHashEntry(hide_setup_hash, hide_setup_token) != NULL);
9455 static void setSetupInfoFromTokenText(SetupFileHash *setup_file_hash,
9456 struct TokenInfo *token_info,
9457 int token_nr, char *token_text)
9459 char *token_hide_text = getStringCat2(token_text, ".hide");
9460 char *token_hide_value = getHashEntry(setup_file_hash, token_hide_text);
9462 // set the value of this setup option in the setup option structure
9463 setSetupInfo(token_info, token_nr, getHashEntry(setup_file_hash, token_text));
9465 // check if this setup option should be hidden in the setup menu
9466 if (token_hide_value != NULL && get_boolean_from_string(token_hide_value))
9467 setHideSetupEntry(token_info[token_nr].value);
9470 static void setSetupInfoFromTokenInfo(SetupFileHash *setup_file_hash,
9471 struct TokenInfo *token_info,
9474 setSetupInfoFromTokenText(setup_file_hash, token_info, token_nr,
9475 token_info[token_nr].text);
9478 static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
9482 if (!setup_file_hash)
9485 if (hide_setup_hash == NULL)
9486 hide_setup_hash = newSetupFileHash();
9488 for (i = 0; i < ARRAY_SIZE(global_setup_tokens); i++)
9489 setSetupInfoFromTokenInfo(setup_file_hash, global_setup_tokens, i);
9491 setup.touch.grid_initialized = TRUE;
9492 for (i = 0; i < 2; i++)
9494 int grid_xsize = setup.touch.grid_xsize[i];
9495 int grid_ysize = setup.touch.grid_ysize[i];
9498 // if virtual buttons are not loaded from setup file, repeat initializing
9499 // virtual buttons grid with default values later when video is initialized
9500 if (grid_xsize == -1 ||
9503 setup.touch.grid_initialized = FALSE;
9508 for (y = 0; y < grid_ysize; y++)
9510 char token_string[MAX_LINE_LEN];
9512 sprintf(token_string, "touch.virtual_buttons.%d.%02d", i, y);
9514 char *value_string = getHashEntry(setup_file_hash, token_string);
9516 if (value_string == NULL)
9519 for (x = 0; x < grid_xsize; x++)
9521 char c = value_string[x];
9523 setup.touch.grid_button[i][x][y] =
9524 (c == '.' ? CHAR_GRID_BUTTON_NONE : c);
9529 for (i = 0; i < ARRAY_SIZE(editor_setup_tokens); i++)
9530 setSetupInfoFromTokenInfo(setup_file_hash, editor_setup_tokens, i);
9532 for (i = 0; i < ARRAY_SIZE(shortcut_setup_tokens); i++)
9533 setSetupInfoFromTokenInfo(setup_file_hash, shortcut_setup_tokens, i);
9535 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
9539 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
9541 setup_input = setup.input[pnr];
9542 for (i = 0; i < ARRAY_SIZE(player_setup_tokens); i++)
9544 char full_token[100];
9546 sprintf(full_token, "%s%s", prefix, player_setup_tokens[i].text);
9547 setSetupInfoFromTokenText(setup_file_hash, player_setup_tokens, i,
9550 setup.input[pnr] = setup_input;
9553 for (i = 0; i < ARRAY_SIZE(system_setup_tokens); i++)
9554 setSetupInfoFromTokenInfo(setup_file_hash, system_setup_tokens, i);
9556 for (i = 0; i < ARRAY_SIZE(internal_setup_tokens); i++)
9557 setSetupInfoFromTokenInfo(setup_file_hash, internal_setup_tokens, i);
9559 for (i = 0; i < ARRAY_SIZE(debug_setup_tokens); i++)
9560 setSetupInfoFromTokenInfo(setup_file_hash, debug_setup_tokens, i);
9562 for (i = 0; i < ARRAY_SIZE(options_setup_tokens); i++)
9563 setSetupInfoFromTokenInfo(setup_file_hash, options_setup_tokens, i);
9565 setHideRelatedSetupEntries();
9568 static void decodeSetupFileHash_AutoSetup(SetupFileHash *setup_file_hash)
9572 if (!setup_file_hash)
9575 for (i = 0; i < ARRAY_SIZE(auto_setup_tokens); i++)
9576 setSetupInfo(auto_setup_tokens, i,
9577 getHashEntry(setup_file_hash,
9578 auto_setup_tokens[i].text));
9581 static void decodeSetupFileHash_EditorCascade(SetupFileHash *setup_file_hash)
9585 if (!setup_file_hash)
9588 for (i = 0; i < ARRAY_SIZE(editor_cascade_setup_tokens); i++)
9589 setSetupInfo(editor_cascade_setup_tokens, i,
9590 getHashEntry(setup_file_hash,
9591 editor_cascade_setup_tokens[i].text));
9594 void LoadSetupFromFilename(char *filename)
9596 SetupFileHash *setup_file_hash = loadSetupFileHash(filename);
9598 if (setup_file_hash)
9600 decodeSetupFileHash(setup_file_hash);
9602 freeSetupFileHash(setup_file_hash);
9606 Error(ERR_DEBUG, "using default setup values");
9610 static void LoadSetup_SpecialPostProcessing(void)
9612 char *player_name_new;
9614 // needed to work around problems with fixed length strings
9615 player_name_new = get_corrected_login_name(setup.player_name);
9616 free(setup.player_name);
9617 setup.player_name = player_name_new;
9619 // "scroll_delay: on(3) / off(0)" was replaced by scroll delay value
9620 if (setup.scroll_delay == FALSE)
9622 setup.scroll_delay_value = MIN_SCROLL_DELAY;
9623 setup.scroll_delay = TRUE; // now always "on"
9626 // make sure that scroll delay value stays inside valid range
9627 setup.scroll_delay_value =
9628 MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
9631 void LoadSetup(void)
9635 // always start with reliable default values
9636 setSetupInfoToDefaults(&setup);
9638 // try to load setup values from default setup file
9639 filename = getDefaultSetupFilename();
9641 if (fileExists(filename))
9642 LoadSetupFromFilename(filename);
9644 // try to load setup values from user setup file
9645 filename = getSetupFilename();
9647 LoadSetupFromFilename(filename);
9649 LoadSetup_SpecialPostProcessing();
9652 void LoadSetup_AutoSetup(void)
9654 char *filename = getPath2(getSetupDir(), AUTOSETUP_FILENAME);
9655 SetupFileHash *setup_file_hash = NULL;
9657 // always start with reliable default values
9658 setSetupInfoToDefaults_AutoSetup(&setup);
9660 setup_file_hash = loadSetupFileHash(filename);
9662 if (setup_file_hash)
9664 decodeSetupFileHash_AutoSetup(setup_file_hash);
9666 freeSetupFileHash(setup_file_hash);
9672 void LoadSetup_EditorCascade(void)
9674 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
9675 SetupFileHash *setup_file_hash = NULL;
9677 // always start with reliable default values
9678 setSetupInfoToDefaults_EditorCascade(&setup);
9680 setup_file_hash = loadSetupFileHash(filename);
9682 if (setup_file_hash)
9684 decodeSetupFileHash_EditorCascade(setup_file_hash);
9686 freeSetupFileHash(setup_file_hash);
9692 static void addGameControllerMappingToHash(SetupFileHash *mappings_hash,
9695 char mapping_guid[MAX_LINE_LEN];
9696 char *mapping_start, *mapping_end;
9698 // get GUID from game controller mapping line: copy complete line
9699 strncpy(mapping_guid, mapping_line, MAX_LINE_LEN - 1);
9700 mapping_guid[MAX_LINE_LEN - 1] = '\0';
9702 // get GUID from game controller mapping line: cut after GUID part
9703 mapping_start = strchr(mapping_guid, ',');
9704 if (mapping_start != NULL)
9705 *mapping_start = '\0';
9707 // cut newline from game controller mapping line
9708 mapping_end = strchr(mapping_line, '\n');
9709 if (mapping_end != NULL)
9710 *mapping_end = '\0';
9712 // add mapping entry to game controller mappings hash
9713 setHashEntry(mappings_hash, mapping_guid, mapping_line);
9716 static void LoadSetup_ReadGameControllerMappings(SetupFileHash *mappings_hash,
9721 if (!(file = fopen(filename, MODE_READ)))
9723 Error(ERR_WARN, "cannot read game controller mappings file '%s'", filename);
9730 char line[MAX_LINE_LEN];
9732 if (!fgets(line, MAX_LINE_LEN, file))
9735 addGameControllerMappingToHash(mappings_hash, line);
9741 void SaveSetup(void)
9743 char *filename = getSetupFilename();
9747 InitUserDataDirectory();
9749 if (!(file = fopen(filename, MODE_WRITE)))
9751 Error(ERR_WARN, "cannot write setup file '%s'", filename);
9755 fprintFileHeader(file, SETUP_FILENAME);
9757 for (i = 0; i < ARRAY_SIZE(global_setup_tokens); i++)
9759 // just to make things nicer :)
9760 if (global_setup_tokens[i].value == &setup.sound ||
9761 global_setup_tokens[i].value == &setup.graphics_set ||
9762 global_setup_tokens[i].value == &setup.volume_simple ||
9763 global_setup_tokens[i].value == &setup.network_mode ||
9764 global_setup_tokens[i].value == &setup.touch.control_type ||
9765 global_setup_tokens[i].value == &setup.touch.grid_xsize[0] ||
9766 global_setup_tokens[i].value == &setup.touch.grid_xsize[1])
9767 fprintf(file, "\n");
9769 fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i));
9772 for (i = 0; i < 2; i++)
9774 int grid_xsize = setup.touch.grid_xsize[i];
9775 int grid_ysize = setup.touch.grid_ysize[i];
9778 fprintf(file, "\n");
9780 for (y = 0; y < grid_ysize; y++)
9782 char token_string[MAX_LINE_LEN];
9783 char value_string[MAX_LINE_LEN];
9785 sprintf(token_string, "touch.virtual_buttons.%d.%02d", i, y);
9787 for (x = 0; x < grid_xsize; x++)
9789 char c = setup.touch.grid_button[i][x][y];
9791 value_string[x] = (c == CHAR_GRID_BUTTON_NONE ? '.' : c);
9794 value_string[grid_xsize] = '\0';
9796 fprintf(file, "%s\n", getFormattedSetupEntry(token_string, value_string));
9800 fprintf(file, "\n");
9801 for (i = 0; i < ARRAY_SIZE(editor_setup_tokens); i++)
9802 fprintf(file, "%s\n", getSetupLine(editor_setup_tokens, "", i));
9804 fprintf(file, "\n");
9805 for (i = 0; i < ARRAY_SIZE(shortcut_setup_tokens); i++)
9806 fprintf(file, "%s\n", getSetupLine(shortcut_setup_tokens, "", i));
9808 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
9812 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
9813 fprintf(file, "\n");
9815 setup_input = setup.input[pnr];
9816 for (i = 0; i < ARRAY_SIZE(player_setup_tokens); i++)
9817 fprintf(file, "%s\n", getSetupLine(player_setup_tokens, prefix, i));
9820 fprintf(file, "\n");
9821 for (i = 0; i < ARRAY_SIZE(system_setup_tokens); i++)
9822 fprintf(file, "%s\n", getSetupLine(system_setup_tokens, "", i));
9824 // (internal setup values not saved to user setup file)
9826 fprintf(file, "\n");
9827 for (i = 0; i < ARRAY_SIZE(debug_setup_tokens); i++)
9828 fprintf(file, "%s\n", getSetupLine(debug_setup_tokens, "", i));
9830 fprintf(file, "\n");
9831 for (i = 0; i < ARRAY_SIZE(options_setup_tokens); i++)
9832 fprintf(file, "%s\n", getSetupLine(options_setup_tokens, "", i));
9836 SetFilePermissions(filename, PERMS_PRIVATE);
9839 void SaveSetup_AutoSetup(void)
9841 char *filename = getPath2(getSetupDir(), AUTOSETUP_FILENAME);
9845 InitUserDataDirectory();
9847 if (!(file = fopen(filename, MODE_WRITE)))
9849 Error(ERR_WARN, "cannot write auto setup file '%s'", filename);
9854 fprintFileHeader(file, AUTOSETUP_FILENAME);
9856 for (i = 0; i < ARRAY_SIZE(auto_setup_tokens); i++)
9857 fprintf(file, "%s\n", getSetupLine(auto_setup_tokens, "", i));
9861 SetFilePermissions(filename, PERMS_PRIVATE);
9866 void SaveSetup_EditorCascade(void)
9868 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
9872 InitUserDataDirectory();
9874 if (!(file = fopen(filename, MODE_WRITE)))
9876 Error(ERR_WARN, "cannot write editor cascade state file '%s'", filename);
9881 fprintFileHeader(file, EDITORCASCADE_FILENAME);
9883 for (i = 0; i < ARRAY_SIZE(editor_cascade_setup_tokens); i++)
9884 fprintf(file, "%s\n", getSetupLine(editor_cascade_setup_tokens, "", i));
9888 SetFilePermissions(filename, PERMS_PRIVATE);
9893 static void SaveSetup_WriteGameControllerMappings(SetupFileHash *mappings_hash,
9898 if (!(file = fopen(filename, MODE_WRITE)))
9900 Error(ERR_WARN, "cannot write game controller mappings file '%s'",filename);
9905 BEGIN_HASH_ITERATION(mappings_hash, itr)
9907 fprintf(file, "%s\n", HASH_ITERATION_VALUE(itr));
9909 END_HASH_ITERATION(mappings_hash, itr)
9914 void SaveSetup_AddGameControllerMapping(char *mapping)
9916 char *filename = getPath2(getSetupDir(), GAMECONTROLLER_BASENAME);
9917 SetupFileHash *mappings_hash = newSetupFileHash();
9919 InitUserDataDirectory();
9921 // load existing personal game controller mappings
9922 LoadSetup_ReadGameControllerMappings(mappings_hash, filename);
9924 // add new mapping to personal game controller mappings
9925 addGameControllerMappingToHash(mappings_hash, mapping);
9927 // save updated personal game controller mappings
9928 SaveSetup_WriteGameControllerMappings(mappings_hash, filename);
9930 freeSetupFileHash(mappings_hash);
9934 void LoadCustomElementDescriptions(void)
9936 char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
9937 SetupFileHash *setup_file_hash;
9940 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9942 if (element_info[i].custom_description != NULL)
9944 free(element_info[i].custom_description);
9945 element_info[i].custom_description = NULL;
9949 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
9952 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9954 char *token = getStringCat2(element_info[i].token_name, ".name");
9955 char *value = getHashEntry(setup_file_hash, token);
9958 element_info[i].custom_description = getStringCopy(value);
9963 freeSetupFileHash(setup_file_hash);
9966 static int getElementFromToken(char *token)
9968 char *value = getHashEntry(element_token_hash, token);
9973 Error(ERR_WARN, "unknown element token '%s'", token);
9975 return EL_UNDEFINED;
9978 void FreeGlobalAnimEventInfo(void)
9980 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
9982 if (gaei->event_list == NULL)
9987 for (i = 0; i < gaei->num_event_lists; i++)
9989 checked_free(gaei->event_list[i]->event_value);
9990 checked_free(gaei->event_list[i]);
9993 checked_free(gaei->event_list);
9995 gaei->event_list = NULL;
9996 gaei->num_event_lists = 0;
9999 static int AddGlobalAnimEventList(void)
10001 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
10002 int list_pos = gaei->num_event_lists++;
10004 gaei->event_list = checked_realloc(gaei->event_list, gaei->num_event_lists *
10005 sizeof(struct GlobalAnimEventListInfo *));
10007 gaei->event_list[list_pos] =
10008 checked_calloc(sizeof(struct GlobalAnimEventListInfo));
10010 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
10012 gaeli->event_value = NULL;
10013 gaeli->num_event_values = 0;
10018 static int AddGlobalAnimEventValue(int list_pos, int event_value)
10020 // do not add empty global animation events
10021 if (event_value == ANIM_EVENT_NONE)
10024 // if list position is undefined, create new list
10025 if (list_pos == ANIM_EVENT_UNDEFINED)
10026 list_pos = AddGlobalAnimEventList();
10028 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
10029 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
10030 int value_pos = gaeli->num_event_values++;
10032 gaeli->event_value = checked_realloc(gaeli->event_value,
10033 gaeli->num_event_values * sizeof(int *));
10035 gaeli->event_value[value_pos] = event_value;
10040 int GetGlobalAnimEventValue(int list_pos, int value_pos)
10042 if (list_pos == ANIM_EVENT_UNDEFINED)
10043 return ANIM_EVENT_NONE;
10045 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
10046 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
10048 return gaeli->event_value[value_pos];
10051 int GetGlobalAnimEventValueCount(int list_pos)
10053 if (list_pos == ANIM_EVENT_UNDEFINED)
10056 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
10057 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
10059 return gaeli->num_event_values;
10062 // This function checks if a string <s> of the format "string1, string2, ..."
10063 // exactly contains a string <s_contained>.
10065 static boolean string_has_parameter(char *s, char *s_contained)
10069 if (s == NULL || s_contained == NULL)
10072 if (strlen(s_contained) > strlen(s))
10075 if (strncmp(s, s_contained, strlen(s_contained)) == 0)
10077 char next_char = s[strlen(s_contained)];
10079 // check if next character is delimiter or whitespace
10080 return (next_char == ',' || next_char == '\0' ||
10081 next_char == ' ' || next_char == '\t' ? TRUE : FALSE);
10084 // check if string contains another parameter string after a comma
10085 substring = strchr(s, ',');
10086 if (substring == NULL) // string does not contain a comma
10089 // advance string pointer to next character after the comma
10092 // skip potential whitespaces after the comma
10093 while (*substring == ' ' || *substring == '\t')
10096 return string_has_parameter(substring, s_contained);
10099 static int get_anim_parameter_value(char *s)
10101 int event_value[] =
10109 char *pattern_1[] =
10117 char *pattern_2 = ".part_";
10118 char *matching_char = NULL;
10120 int pattern_1_len = 0;
10121 int result = ANIM_EVENT_NONE;
10124 for (i = 0; i < ARRAY_SIZE(event_value); i++)
10126 matching_char = strstr(s_ptr, pattern_1[i]);
10127 pattern_1_len = strlen(pattern_1[i]);
10128 result = event_value[i];
10130 if (matching_char != NULL)
10134 if (matching_char == NULL)
10135 return ANIM_EVENT_NONE;
10137 s_ptr = matching_char + pattern_1_len;
10139 // check for main animation number ("anim_X" or "anim_XX")
10140 if (*s_ptr >= '0' && *s_ptr <= '9')
10142 int gic_anim_nr = (*s_ptr++ - '0');
10144 if (*s_ptr >= '0' && *s_ptr <= '9')
10145 gic_anim_nr = 10 * gic_anim_nr + (*s_ptr++ - '0');
10147 if (gic_anim_nr < 1 || gic_anim_nr > MAX_GLOBAL_ANIMS)
10148 return ANIM_EVENT_NONE;
10150 result |= gic_anim_nr << ANIM_EVENT_ANIM_BIT;
10154 // invalid main animation number specified
10156 return ANIM_EVENT_NONE;
10159 // check for animation part number ("part_X" or "part_XX") (optional)
10160 if (strPrefix(s_ptr, pattern_2))
10162 s_ptr += strlen(pattern_2);
10164 if (*s_ptr >= '0' && *s_ptr <= '9')
10166 int gic_part_nr = (*s_ptr++ - '0');
10168 if (*s_ptr >= '0' && *s_ptr <= '9')
10169 gic_part_nr = 10 * gic_part_nr + (*s_ptr++ - '0');
10171 if (gic_part_nr < 1 || gic_part_nr > MAX_GLOBAL_ANIM_PARTS)
10172 return ANIM_EVENT_NONE;
10174 result |= gic_part_nr << ANIM_EVENT_PART_BIT;
10178 // invalid animation part number specified
10180 return ANIM_EVENT_NONE;
10184 // discard result if next character is neither delimiter nor whitespace
10185 if (!(*s_ptr == ',' || *s_ptr == '\0' ||
10186 *s_ptr == ' ' || *s_ptr == '\t'))
10187 return ANIM_EVENT_NONE;
10192 static int get_anim_parameter_values(char *s)
10194 int list_pos = ANIM_EVENT_UNDEFINED;
10195 int event_value = ANIM_EVENT_DEFAULT;
10197 if (string_has_parameter(s, "any"))
10198 event_value |= ANIM_EVENT_ANY;
10200 if (string_has_parameter(s, "click:self") ||
10201 string_has_parameter(s, "click") ||
10202 string_has_parameter(s, "self"))
10203 event_value |= ANIM_EVENT_SELF;
10205 if (string_has_parameter(s, "unclick:any"))
10206 event_value |= ANIM_EVENT_UNCLICK_ANY;
10208 // if animation event found, add it to global animation event list
10209 if (event_value != ANIM_EVENT_NONE)
10210 list_pos = AddGlobalAnimEventValue(list_pos, event_value);
10214 // add optional "click:anim_X" or "click:anim_X.part_X" parameter
10215 event_value = get_anim_parameter_value(s);
10217 // if animation event found, add it to global animation event list
10218 if (event_value != ANIM_EVENT_NONE)
10219 list_pos = AddGlobalAnimEventValue(list_pos, event_value);
10221 // continue with next part of the string, starting with next comma
10222 s = strchr(s + 1, ',');
10228 static int get_anim_action_parameter_value(char *token)
10230 int result = getImageIDFromToken(token);
10234 char *gfx_token = getStringCat2("gfx.", token);
10236 result = getImageIDFromToken(gfx_token);
10238 checked_free(gfx_token);
10243 Key key = getKeyFromX11KeyName(token);
10245 if (key != KSYM_UNDEFINED)
10246 result = -(int)key;
10250 result = ANIM_EVENT_ACTION_NONE;
10255 int get_parameter_value(char *value_raw, char *suffix, int type)
10257 char *value = getStringToLower(value_raw);
10258 int result = 0; // probably a save default value
10260 if (strEqual(suffix, ".direction"))
10262 result = (strEqual(value, "left") ? MV_LEFT :
10263 strEqual(value, "right") ? MV_RIGHT :
10264 strEqual(value, "up") ? MV_UP :
10265 strEqual(value, "down") ? MV_DOWN : MV_NONE);
10267 else if (strEqual(suffix, ".position"))
10269 result = (strEqual(value, "left") ? POS_LEFT :
10270 strEqual(value, "right") ? POS_RIGHT :
10271 strEqual(value, "top") ? POS_TOP :
10272 strEqual(value, "upper") ? POS_UPPER :
10273 strEqual(value, "middle") ? POS_MIDDLE :
10274 strEqual(value, "lower") ? POS_LOWER :
10275 strEqual(value, "bottom") ? POS_BOTTOM :
10276 strEqual(value, "any") ? POS_ANY :
10277 strEqual(value, "last") ? POS_LAST : POS_UNDEFINED);
10279 else if (strEqual(suffix, ".align"))
10281 result = (strEqual(value, "left") ? ALIGN_LEFT :
10282 strEqual(value, "right") ? ALIGN_RIGHT :
10283 strEqual(value, "center") ? ALIGN_CENTER :
10284 strEqual(value, "middle") ? ALIGN_CENTER : ALIGN_DEFAULT);
10286 else if (strEqual(suffix, ".valign"))
10288 result = (strEqual(value, "top") ? VALIGN_TOP :
10289 strEqual(value, "bottom") ? VALIGN_BOTTOM :
10290 strEqual(value, "middle") ? VALIGN_MIDDLE :
10291 strEqual(value, "center") ? VALIGN_MIDDLE : VALIGN_DEFAULT);
10293 else if (strEqual(suffix, ".anim_mode"))
10295 result = (string_has_parameter(value, "none") ? ANIM_NONE :
10296 string_has_parameter(value, "loop") ? ANIM_LOOP :
10297 string_has_parameter(value, "linear") ? ANIM_LINEAR :
10298 string_has_parameter(value, "pingpong") ? ANIM_PINGPONG :
10299 string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 :
10300 string_has_parameter(value, "random") ? ANIM_RANDOM :
10301 string_has_parameter(value, "ce_value") ? ANIM_CE_VALUE :
10302 string_has_parameter(value, "ce_score") ? ANIM_CE_SCORE :
10303 string_has_parameter(value, "ce_delay") ? ANIM_CE_DELAY :
10304 string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL :
10305 string_has_parameter(value, "vertical") ? ANIM_VERTICAL :
10306 string_has_parameter(value, "centered") ? ANIM_CENTERED :
10307 string_has_parameter(value, "all") ? ANIM_ALL :
10310 if (string_has_parameter(value, "once"))
10311 result |= ANIM_ONCE;
10313 if (string_has_parameter(value, "reverse"))
10314 result |= ANIM_REVERSE;
10316 if (string_has_parameter(value, "opaque_player"))
10317 result |= ANIM_OPAQUE_PLAYER;
10319 if (string_has_parameter(value, "static_panel"))
10320 result |= ANIM_STATIC_PANEL;
10322 else if (strEqual(suffix, ".init_event") ||
10323 strEqual(suffix, ".anim_event"))
10325 result = get_anim_parameter_values(value);
10327 else if (strEqual(suffix, ".init_delay_action") ||
10328 strEqual(suffix, ".anim_delay_action") ||
10329 strEqual(suffix, ".post_delay_action") ||
10330 strEqual(suffix, ".init_event_action") ||
10331 strEqual(suffix, ".anim_event_action"))
10333 result = get_anim_action_parameter_value(value_raw);
10335 else if (strEqual(suffix, ".class"))
10337 result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
10338 get_hash_from_key(value));
10340 else if (strEqual(suffix, ".style"))
10342 result = STYLE_DEFAULT;
10344 if (string_has_parameter(value, "accurate_borders"))
10345 result |= STYLE_ACCURATE_BORDERS;
10347 if (string_has_parameter(value, "inner_corners"))
10348 result |= STYLE_INNER_CORNERS;
10350 if (string_has_parameter(value, "reverse"))
10351 result |= STYLE_REVERSE;
10353 if (string_has_parameter(value, "block_clicks"))
10354 result |= STYLE_BLOCK;
10356 if (string_has_parameter(value, "passthrough_clicks"))
10357 result |= STYLE_PASSTHROUGH;
10359 if (string_has_parameter(value, "multiple_actions"))
10360 result |= STYLE_MULTIPLE_ACTIONS;
10362 else if (strEqual(suffix, ".fade_mode"))
10364 result = (string_has_parameter(value, "none") ? FADE_MODE_NONE :
10365 string_has_parameter(value, "fade") ? FADE_MODE_FADE :
10366 string_has_parameter(value, "crossfade") ? FADE_MODE_CROSSFADE :
10367 string_has_parameter(value, "melt") ? FADE_MODE_MELT :
10368 string_has_parameter(value, "curtain") ? FADE_MODE_CURTAIN :
10369 FADE_MODE_DEFAULT);
10371 else if (strEqual(suffix, ".auto_delay_unit"))
10373 result = (string_has_parameter(value, "ms") ? AUTO_DELAY_UNIT_MS :
10374 string_has_parameter(value, "frames") ? AUTO_DELAY_UNIT_FRAMES :
10375 AUTO_DELAY_UNIT_DEFAULT);
10377 else if (strPrefix(suffix, ".font")) // (may also be ".font_xyz")
10379 result = gfx.get_font_from_token_function(value);
10381 else // generic parameter of type integer or boolean
10383 result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
10384 type == TYPE_INTEGER ? get_integer_from_string(value) :
10385 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
10386 ARG_UNDEFINED_VALUE);
10394 static int get_token_parameter_value(char *token, char *value_raw)
10398 if (token == NULL || value_raw == NULL)
10399 return ARG_UNDEFINED_VALUE;
10401 suffix = strrchr(token, '.');
10402 if (suffix == NULL)
10405 if (strEqual(suffix, ".element"))
10406 return getElementFromToken(value_raw);
10408 // !!! USE CORRECT VALUE TYPE (currently works also for TYPE_BOOLEAN) !!!
10409 return get_parameter_value(value_raw, suffix, TYPE_INTEGER);
10412 void InitMenuDesignSettings_Static(void)
10416 // always start with reliable default values from static default config
10417 for (i = 0; image_config_vars[i].token != NULL; i++)
10419 char *value = getHashEntry(image_config_hash, image_config_vars[i].token);
10422 *image_config_vars[i].value =
10423 get_token_parameter_value(image_config_vars[i].token, value);
10427 static void InitMenuDesignSettings_SpecialPreProcessing(void)
10431 // the following initializes hierarchical values from static configuration
10433 // special case: initialize "ARG_DEFAULT" values in static default config
10434 // (e.g., initialize "[titlemessage].fade_mode" from "[title].fade_mode")
10435 titlescreen_initial_first_default.fade_mode =
10436 title_initial_first_default.fade_mode;
10437 titlescreen_initial_first_default.fade_delay =
10438 title_initial_first_default.fade_delay;
10439 titlescreen_initial_first_default.post_delay =
10440 title_initial_first_default.post_delay;
10441 titlescreen_initial_first_default.auto_delay =
10442 title_initial_first_default.auto_delay;
10443 titlescreen_initial_first_default.auto_delay_unit =
10444 title_initial_first_default.auto_delay_unit;
10445 titlescreen_first_default.fade_mode = title_first_default.fade_mode;
10446 titlescreen_first_default.fade_delay = title_first_default.fade_delay;
10447 titlescreen_first_default.post_delay = title_first_default.post_delay;
10448 titlescreen_first_default.auto_delay = title_first_default.auto_delay;
10449 titlescreen_first_default.auto_delay_unit =
10450 title_first_default.auto_delay_unit;
10451 titlemessage_initial_first_default.fade_mode =
10452 title_initial_first_default.fade_mode;
10453 titlemessage_initial_first_default.fade_delay =
10454 title_initial_first_default.fade_delay;
10455 titlemessage_initial_first_default.post_delay =
10456 title_initial_first_default.post_delay;
10457 titlemessage_initial_first_default.auto_delay =
10458 title_initial_first_default.auto_delay;
10459 titlemessage_initial_first_default.auto_delay_unit =
10460 title_initial_first_default.auto_delay_unit;
10461 titlemessage_first_default.fade_mode = title_first_default.fade_mode;
10462 titlemessage_first_default.fade_delay = title_first_default.fade_delay;
10463 titlemessage_first_default.post_delay = title_first_default.post_delay;
10464 titlemessage_first_default.auto_delay = title_first_default.auto_delay;
10465 titlemessage_first_default.auto_delay_unit =
10466 title_first_default.auto_delay_unit;
10468 titlescreen_initial_default.fade_mode = title_initial_default.fade_mode;
10469 titlescreen_initial_default.fade_delay = title_initial_default.fade_delay;
10470 titlescreen_initial_default.post_delay = title_initial_default.post_delay;
10471 titlescreen_initial_default.auto_delay = title_initial_default.auto_delay;
10472 titlescreen_initial_default.auto_delay_unit =
10473 title_initial_default.auto_delay_unit;
10474 titlescreen_default.fade_mode = title_default.fade_mode;
10475 titlescreen_default.fade_delay = title_default.fade_delay;
10476 titlescreen_default.post_delay = title_default.post_delay;
10477 titlescreen_default.auto_delay = title_default.auto_delay;
10478 titlescreen_default.auto_delay_unit = title_default.auto_delay_unit;
10479 titlemessage_initial_default.fade_mode = title_initial_default.fade_mode;
10480 titlemessage_initial_default.fade_delay = title_initial_default.fade_delay;
10481 titlemessage_initial_default.post_delay = title_initial_default.post_delay;
10482 titlemessage_initial_default.auto_delay_unit =
10483 title_initial_default.auto_delay_unit;
10484 titlemessage_default.fade_mode = title_default.fade_mode;
10485 titlemessage_default.fade_delay = title_default.fade_delay;
10486 titlemessage_default.post_delay = title_default.post_delay;
10487 titlemessage_default.auto_delay = title_default.auto_delay;
10488 titlemessage_default.auto_delay_unit = title_default.auto_delay_unit;
10490 // special case: initialize "ARG_DEFAULT" values in static default config
10491 // (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode")
10492 for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
10494 titlescreen_initial_first[i] = titlescreen_initial_first_default;
10495 titlescreen_first[i] = titlescreen_first_default;
10496 titlemessage_initial_first[i] = titlemessage_initial_first_default;
10497 titlemessage_first[i] = titlemessage_first_default;
10499 titlescreen_initial[i] = titlescreen_initial_default;
10500 titlescreen[i] = titlescreen_default;
10501 titlemessage_initial[i] = titlemessage_initial_default;
10502 titlemessage[i] = titlemessage_default;
10505 // special case: initialize "ARG_DEFAULT" values in static default config
10506 // (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz")
10507 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
10509 if (i == GFX_SPECIAL_ARG_TITLE) // title values already initialized
10512 menu.enter_screen[i] = menu.enter_screen[GFX_SPECIAL_ARG_DEFAULT];
10513 menu.leave_screen[i] = menu.leave_screen[GFX_SPECIAL_ARG_DEFAULT];
10514 menu.next_screen[i] = menu.next_screen[GFX_SPECIAL_ARG_DEFAULT];
10517 // special case: initialize "ARG_DEFAULT" values in static default config
10518 // (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz")
10519 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
10521 viewport.window[i] = viewport.window[GFX_SPECIAL_ARG_DEFAULT];
10522 viewport.playfield[i] = viewport.playfield[GFX_SPECIAL_ARG_DEFAULT];
10523 viewport.door_1[i] = viewport.door_1[GFX_SPECIAL_ARG_DEFAULT];
10525 if (i == GFX_SPECIAL_ARG_EDITOR) // editor values already initialized
10528 viewport.door_2[i] = viewport.door_2[GFX_SPECIAL_ARG_DEFAULT];
10532 static void InitMenuDesignSettings_SpecialPostProcessing(void)
10536 struct XY *dst, *src;
10538 game_buttons_xy[] =
10540 { &game.button.save, &game.button.stop },
10541 { &game.button.pause2, &game.button.pause },
10542 { &game.button.load, &game.button.play },
10543 { &game.button.undo, &game.button.stop },
10544 { &game.button.redo, &game.button.play },
10550 // special case: initialize later added SETUP list size from LEVELS value
10551 if (menu.list_size[GAME_MODE_SETUP] == -1)
10552 menu.list_size[GAME_MODE_SETUP] = menu.list_size[GAME_MODE_LEVELS];
10554 // set default position for snapshot buttons to stop/pause/play buttons
10555 for (i = 0; game_buttons_xy[i].dst != NULL; i++)
10556 if ((*game_buttons_xy[i].dst).x == -1 &&
10557 (*game_buttons_xy[i].dst).y == -1)
10558 *game_buttons_xy[i].dst = *game_buttons_xy[i].src;
10560 // --------------------------------------------------------------------------
10561 // dynamic viewports (including playfield margins, borders and alignments)
10562 // --------------------------------------------------------------------------
10564 // dynamic viewports currently only supported for landscape mode
10565 int display_width = MAX(video.display_width, video.display_height);
10566 int display_height = MIN(video.display_width, video.display_height);
10568 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
10570 struct RectWithBorder *vp_window = &viewport.window[i];
10571 struct RectWithBorder *vp_playfield = &viewport.playfield[i];
10572 struct RectWithBorder *vp_door_1 = &viewport.door_1[i];
10573 struct RectWithBorder *vp_door_2 = &viewport.door_2[i];
10574 boolean dynamic_window_width = (vp_window->min_width != -1);
10575 boolean dynamic_window_height = (vp_window->min_height != -1);
10576 boolean dynamic_playfield_width = (vp_playfield->min_width != -1);
10577 boolean dynamic_playfield_height = (vp_playfield->min_height != -1);
10579 // adjust window size if min/max width/height is specified
10581 if (vp_window->min_width != -1)
10583 int window_width = display_width;
10585 // when using static window height, use aspect ratio of display
10586 if (vp_window->min_height == -1)
10587 window_width = vp_window->height * display_width / display_height;
10589 vp_window->width = MAX(vp_window->min_width, window_width);
10592 if (vp_window->min_height != -1)
10594 int window_height = display_height;
10596 // when using static window width, use aspect ratio of display
10597 if (vp_window->min_width == -1)
10598 window_height = vp_window->width * display_height / display_width;
10600 vp_window->height = MAX(vp_window->min_height, window_height);
10603 if (vp_window->max_width != -1)
10604 vp_window->width = MIN(vp_window->width, vp_window->max_width);
10606 if (vp_window->max_height != -1)
10607 vp_window->height = MIN(vp_window->height, vp_window->max_height);
10609 int playfield_width = vp_window->width;
10610 int playfield_height = vp_window->height;
10612 // adjust playfield size and position according to specified margins
10614 playfield_width -= vp_playfield->margin_left;
10615 playfield_width -= vp_playfield->margin_right;
10617 playfield_height -= vp_playfield->margin_top;
10618 playfield_height -= vp_playfield->margin_bottom;
10620 // adjust playfield size if min/max width/height is specified
10622 if (vp_playfield->min_width != -1)
10623 vp_playfield->width = MAX(vp_playfield->min_width, playfield_width);
10625 if (vp_playfield->min_height != -1)
10626 vp_playfield->height = MAX(vp_playfield->min_height, playfield_height);
10628 if (vp_playfield->max_width != -1)
10629 vp_playfield->width = MIN(vp_playfield->width, vp_playfield->max_width);
10631 if (vp_playfield->max_height != -1)
10632 vp_playfield->height = MIN(vp_playfield->height,vp_playfield->max_height);
10634 // adjust playfield position according to specified alignment
10636 if (vp_playfield->align == ALIGN_LEFT || vp_playfield->x > 0)
10637 vp_playfield->x = ALIGNED_VP_XPOS(vp_playfield);
10638 else if (vp_playfield->align == ALIGN_CENTER)
10639 vp_playfield->x = playfield_width / 2 - vp_playfield->width / 2;
10640 else if (vp_playfield->align == ALIGN_RIGHT)
10641 vp_playfield->x += playfield_width - vp_playfield->width;
10643 if (vp_playfield->valign == VALIGN_TOP || vp_playfield->y > 0)
10644 vp_playfield->y = ALIGNED_VP_YPOS(vp_playfield);
10645 else if (vp_playfield->valign == VALIGN_MIDDLE)
10646 vp_playfield->y = playfield_height / 2 - vp_playfield->height / 2;
10647 else if (vp_playfield->valign == VALIGN_BOTTOM)
10648 vp_playfield->y += playfield_height - vp_playfield->height;
10650 vp_playfield->x += vp_playfield->margin_left;
10651 vp_playfield->y += vp_playfield->margin_top;
10653 // adjust individual playfield borders if only default border is specified
10655 if (vp_playfield->border_left == -1)
10656 vp_playfield->border_left = vp_playfield->border_size;
10657 if (vp_playfield->border_right == -1)
10658 vp_playfield->border_right = vp_playfield->border_size;
10659 if (vp_playfield->border_top == -1)
10660 vp_playfield->border_top = vp_playfield->border_size;
10661 if (vp_playfield->border_bottom == -1)
10662 vp_playfield->border_bottom = vp_playfield->border_size;
10664 // set dynamic playfield borders if borders are specified as undefined
10665 // (but only if window size was dynamic and playfield size was static)
10667 if (dynamic_window_width && !dynamic_playfield_width)
10669 if (vp_playfield->border_left == -1)
10671 vp_playfield->border_left = (vp_playfield->x -
10672 vp_playfield->margin_left);
10673 vp_playfield->x -= vp_playfield->border_left;
10674 vp_playfield->width += vp_playfield->border_left;
10677 if (vp_playfield->border_right == -1)
10679 vp_playfield->border_right = (vp_window->width -
10681 vp_playfield->width -
10682 vp_playfield->margin_right);
10683 vp_playfield->width += vp_playfield->border_right;
10687 if (dynamic_window_height && !dynamic_playfield_height)
10689 if (vp_playfield->border_top == -1)
10691 vp_playfield->border_top = (vp_playfield->y -
10692 vp_playfield->margin_top);
10693 vp_playfield->y -= vp_playfield->border_top;
10694 vp_playfield->height += vp_playfield->border_top;
10697 if (vp_playfield->border_bottom == -1)
10699 vp_playfield->border_bottom = (vp_window->height -
10701 vp_playfield->height -
10702 vp_playfield->margin_bottom);
10703 vp_playfield->height += vp_playfield->border_bottom;
10707 // adjust playfield size to be a multiple of a defined alignment tile size
10709 int align_size = vp_playfield->align_size;
10710 int playfield_xtiles = vp_playfield->width / align_size;
10711 int playfield_ytiles = vp_playfield->height / align_size;
10712 int playfield_width_corrected = playfield_xtiles * align_size;
10713 int playfield_height_corrected = playfield_ytiles * align_size;
10714 boolean is_playfield_mode = (i == GFX_SPECIAL_ARG_PLAYING ||
10715 i == GFX_SPECIAL_ARG_EDITOR);
10717 if (is_playfield_mode &&
10718 dynamic_playfield_width &&
10719 vp_playfield->width != playfield_width_corrected)
10721 int playfield_xdiff = vp_playfield->width - playfield_width_corrected;
10723 vp_playfield->width = playfield_width_corrected;
10725 if (vp_playfield->align == ALIGN_LEFT)
10727 vp_playfield->border_left += playfield_xdiff;
10729 else if (vp_playfield->align == ALIGN_RIGHT)
10731 vp_playfield->border_right += playfield_xdiff;
10733 else if (vp_playfield->align == ALIGN_CENTER)
10735 int border_left_diff = playfield_xdiff / 2;
10736 int border_right_diff = playfield_xdiff - border_left_diff;
10738 vp_playfield->border_left += border_left_diff;
10739 vp_playfield->border_right += border_right_diff;
10743 if (is_playfield_mode &&
10744 dynamic_playfield_height &&
10745 vp_playfield->height != playfield_height_corrected)
10747 int playfield_ydiff = vp_playfield->height - playfield_height_corrected;
10749 vp_playfield->height = playfield_height_corrected;
10751 if (vp_playfield->valign == VALIGN_TOP)
10753 vp_playfield->border_top += playfield_ydiff;
10755 else if (vp_playfield->align == VALIGN_BOTTOM)
10757 vp_playfield->border_right += playfield_ydiff;
10759 else if (vp_playfield->align == VALIGN_MIDDLE)
10761 int border_top_diff = playfield_ydiff / 2;
10762 int border_bottom_diff = playfield_ydiff - border_top_diff;
10764 vp_playfield->border_top += border_top_diff;
10765 vp_playfield->border_bottom += border_bottom_diff;
10769 // adjust door positions according to specified alignment
10771 for (j = 0; j < 2; j++)
10773 struct RectWithBorder *vp_door = (j == 0 ? vp_door_1 : vp_door_2);
10775 if (vp_door->align == ALIGN_LEFT || vp_door->x > 0)
10776 vp_door->x = ALIGNED_VP_XPOS(vp_door);
10777 else if (vp_door->align == ALIGN_CENTER)
10778 vp_door->x = vp_window->width / 2 - vp_door->width / 2;
10779 else if (vp_door->align == ALIGN_RIGHT)
10780 vp_door->x += vp_window->width - vp_door->width;
10782 if (vp_door->valign == VALIGN_TOP || vp_door->y > 0)
10783 vp_door->y = ALIGNED_VP_YPOS(vp_door);
10784 else if (vp_door->valign == VALIGN_MIDDLE)
10785 vp_door->y = vp_window->height / 2 - vp_door->height / 2;
10786 else if (vp_door->valign == VALIGN_BOTTOM)
10787 vp_door->y += vp_window->height - vp_door->height;
10792 static void InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics(void)
10796 struct XYTileSize *dst, *src;
10799 editor_buttons_xy[] =
10802 &editor.button.element_left, &editor.palette.element_left,
10803 IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT
10806 &editor.button.element_middle, &editor.palette.element_middle,
10807 IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE
10810 &editor.button.element_right, &editor.palette.element_right,
10811 IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT
10818 // set default position for element buttons to element graphics
10819 for (i = 0; editor_buttons_xy[i].dst != NULL; i++)
10821 if ((*editor_buttons_xy[i].dst).x == -1 &&
10822 (*editor_buttons_xy[i].dst).y == -1)
10824 struct GraphicInfo *gd = &graphic_info[editor_buttons_xy[i].graphic];
10826 gd->width = gd->height = editor_buttons_xy[i].src->tile_size;
10828 *editor_buttons_xy[i].dst = *editor_buttons_xy[i].src;
10832 // adjust editor palette rows and columns if specified to be dynamic
10834 if (editor.palette.cols == -1)
10836 int vp_width = viewport.playfield[GFX_SPECIAL_ARG_EDITOR].width;
10837 int bt_width = graphic_info[IMG_EDITOR_PALETTE_BUTTON].width;
10838 int sc_width = graphic_info[IMG_EDITOR_PALETTE_SCROLLBAR].width;
10840 editor.palette.cols = (vp_width - sc_width) / bt_width;
10842 if (editor.palette.x == -1)
10844 int palette_width = editor.palette.cols * bt_width + sc_width;
10846 editor.palette.x = (vp_width - palette_width) / 2;
10850 if (editor.palette.rows == -1)
10852 int vp_height = viewport.playfield[GFX_SPECIAL_ARG_EDITOR].height;
10853 int bt_height = graphic_info[IMG_EDITOR_PALETTE_BUTTON].height;
10854 int tx_height = getFontHeight(FONT_TEXT_2);
10856 editor.palette.rows = (vp_height - tx_height) / bt_height;
10858 if (editor.palette.y == -1)
10860 int palette_height = editor.palette.rows * bt_height + tx_height;
10862 editor.palette.y = (vp_height - palette_height) / 2;
10867 static void LoadMenuDesignSettingsFromFilename(char *filename)
10869 static struct TitleFadingInfo tfi;
10870 static struct TitleMessageInfo tmi;
10871 static struct TokenInfo title_tokens[] =
10873 { TYPE_INTEGER, &tfi.fade_mode, ".fade_mode" },
10874 { TYPE_INTEGER, &tfi.fade_delay, ".fade_delay" },
10875 { TYPE_INTEGER, &tfi.post_delay, ".post_delay" },
10876 { TYPE_INTEGER, &tfi.auto_delay, ".auto_delay" },
10877 { TYPE_INTEGER, &tfi.auto_delay_unit, ".auto_delay_unit" },
10881 static struct TokenInfo titlemessage_tokens[] =
10883 { TYPE_INTEGER, &tmi.x, ".x" },
10884 { TYPE_INTEGER, &tmi.y, ".y" },
10885 { TYPE_INTEGER, &tmi.width, ".width" },
10886 { TYPE_INTEGER, &tmi.height, ".height" },
10887 { TYPE_INTEGER, &tmi.chars, ".chars" },
10888 { TYPE_INTEGER, &tmi.lines, ".lines" },
10889 { TYPE_INTEGER, &tmi.align, ".align" },
10890 { TYPE_INTEGER, &tmi.valign, ".valign" },
10891 { TYPE_INTEGER, &tmi.font, ".font" },
10892 { TYPE_BOOLEAN, &tmi.autowrap, ".autowrap" },
10893 { TYPE_BOOLEAN, &tmi.centered, ".centered" },
10894 { TYPE_BOOLEAN, &tmi.parse_comments, ".parse_comments" },
10895 { TYPE_INTEGER, &tmi.sort_priority, ".sort_priority" },
10896 { TYPE_INTEGER, &tmi.fade_mode, ".fade_mode" },
10897 { TYPE_INTEGER, &tmi.fade_delay, ".fade_delay" },
10898 { TYPE_INTEGER, &tmi.post_delay, ".post_delay" },
10899 { TYPE_INTEGER, &tmi.auto_delay, ".auto_delay" },
10900 { TYPE_INTEGER, &tmi.auto_delay_unit, ".auto_delay_unit" },
10906 struct TitleFadingInfo *info;
10911 // initialize first titles from "enter screen" definitions, if defined
10912 { &title_initial_first_default, "menu.enter_screen.TITLE" },
10913 { &title_first_default, "menu.enter_screen.TITLE" },
10915 // initialize title screens from "next screen" definitions, if defined
10916 { &title_initial_default, "menu.next_screen.TITLE" },
10917 { &title_default, "menu.next_screen.TITLE" },
10923 struct TitleMessageInfo *array;
10926 titlemessage_arrays[] =
10928 // initialize first titles from "enter screen" definitions, if defined
10929 { titlescreen_initial_first, "menu.enter_screen.TITLE" },
10930 { titlescreen_first, "menu.enter_screen.TITLE" },
10931 { titlemessage_initial_first, "menu.enter_screen.TITLE" },
10932 { titlemessage_first, "menu.enter_screen.TITLE" },
10934 // initialize titles from "next screen" definitions, if defined
10935 { titlescreen_initial, "menu.next_screen.TITLE" },
10936 { titlescreen, "menu.next_screen.TITLE" },
10937 { titlemessage_initial, "menu.next_screen.TITLE" },
10938 { titlemessage, "menu.next_screen.TITLE" },
10940 // overwrite titles with title definitions, if defined
10941 { titlescreen_initial_first, "[title_initial]" },
10942 { titlescreen_first, "[title]" },
10943 { titlemessage_initial_first, "[title_initial]" },
10944 { titlemessage_first, "[title]" },
10946 { titlescreen_initial, "[title_initial]" },
10947 { titlescreen, "[title]" },
10948 { titlemessage_initial, "[title_initial]" },
10949 { titlemessage, "[title]" },
10951 // overwrite titles with title screen/message definitions, if defined
10952 { titlescreen_initial_first, "[titlescreen_initial]" },
10953 { titlescreen_first, "[titlescreen]" },
10954 { titlemessage_initial_first, "[titlemessage_initial]" },
10955 { titlemessage_first, "[titlemessage]" },
10957 { titlescreen_initial, "[titlescreen_initial]" },
10958 { titlescreen, "[titlescreen]" },
10959 { titlemessage_initial, "[titlemessage_initial]" },
10960 { titlemessage, "[titlemessage]" },
10964 SetupFileHash *setup_file_hash;
10967 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
10970 // the following initializes hierarchical values from dynamic configuration
10972 // special case: initialize with default values that may be overwritten
10973 // (e.g., init "menu.draw_xoffset.INFO" from "menu.draw_xoffset")
10974 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
10976 struct TokenIntPtrInfo menu_config[] =
10978 { "menu.draw_xoffset", &menu.draw_xoffset[i] },
10979 { "menu.draw_yoffset", &menu.draw_yoffset[i] },
10980 { "menu.list_size", &menu.list_size[i] }
10983 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
10985 char *token = menu_config[j].token;
10986 char *value = getHashEntry(setup_file_hash, token);
10989 *menu_config[j].value = get_integer_from_string(value);
10993 // special case: initialize with default values that may be overwritten
10994 // (eg, init "menu.draw_xoffset.INFO[XXX]" from "menu.draw_xoffset.INFO")
10995 for (i = 0; i < NUM_SPECIAL_GFX_INFO_ARGS; i++)
10997 struct TokenIntPtrInfo menu_config[] =
10999 { "menu.draw_xoffset.INFO", &menu.draw_xoffset_info[i] },
11000 { "menu.draw_yoffset.INFO", &menu.draw_yoffset_info[i] },
11001 { "menu.list_size.INFO", &menu.list_size_info[i] }
11004 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
11006 char *token = menu_config[j].token;
11007 char *value = getHashEntry(setup_file_hash, token);
11010 *menu_config[j].value = get_integer_from_string(value);
11014 // special case: initialize with default values that may be overwritten
11015 // (eg, init "menu.draw_xoffset.SETUP[XXX]" from "menu.draw_xoffset.SETUP")
11016 for (i = 0; i < NUM_SPECIAL_GFX_SETUP_ARGS; i++)
11018 struct TokenIntPtrInfo menu_config[] =
11020 { "menu.draw_xoffset.SETUP", &menu.draw_xoffset_setup[i] },
11021 { "menu.draw_yoffset.SETUP", &menu.draw_yoffset_setup[i] }
11024 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
11026 char *token = menu_config[j].token;
11027 char *value = getHashEntry(setup_file_hash, token);
11030 *menu_config[j].value = get_integer_from_string(value);
11034 // special case: initialize with default values that may be overwritten
11035 // (eg, init "menu.line_spacing.INFO[XXX]" from "menu.line_spacing.INFO")
11036 for (i = 0; i < NUM_SPECIAL_GFX_INFO_ARGS; i++)
11038 struct TokenIntPtrInfo menu_config[] =
11040 { "menu.left_spacing.INFO", &menu.left_spacing_info[i] },
11041 { "menu.right_spacing.INFO", &menu.right_spacing_info[i] },
11042 { "menu.top_spacing.INFO", &menu.top_spacing_info[i] },
11043 { "menu.bottom_spacing.INFO", &menu.bottom_spacing_info[i] },
11044 { "menu.paragraph_spacing.INFO", &menu.paragraph_spacing_info[i] },
11045 { "menu.headline1_spacing.INFO", &menu.headline1_spacing_info[i] },
11046 { "menu.headline2_spacing.INFO", &menu.headline2_spacing_info[i] },
11047 { "menu.line_spacing.INFO", &menu.line_spacing_info[i] },
11048 { "menu.extra_spacing.INFO", &menu.extra_spacing_info[i] },
11051 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
11053 char *token = menu_config[j].token;
11054 char *value = getHashEntry(setup_file_hash, token);
11057 *menu_config[j].value = get_integer_from_string(value);
11061 // special case: initialize with default values that may be overwritten
11062 // (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz")
11063 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
11065 struct TokenIntPtrInfo menu_config[] =
11067 { "menu.enter_screen.fade_mode", &menu.enter_screen[i].fade_mode },
11068 { "menu.enter_screen.fade_delay", &menu.enter_screen[i].fade_delay },
11069 { "menu.enter_screen.post_delay", &menu.enter_screen[i].post_delay },
11070 { "menu.leave_screen.fade_mode", &menu.leave_screen[i].fade_mode },
11071 { "menu.leave_screen.fade_delay", &menu.leave_screen[i].fade_delay },
11072 { "menu.leave_screen.post_delay", &menu.leave_screen[i].post_delay },
11073 { "menu.next_screen.fade_mode", &menu.next_screen[i].fade_mode },
11074 { "menu.next_screen.fade_delay", &menu.next_screen[i].fade_delay },
11075 { "menu.next_screen.post_delay", &menu.next_screen[i].post_delay }
11078 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
11080 char *token = menu_config[j].token;
11081 char *value = getHashEntry(setup_file_hash, token);
11084 *menu_config[j].value = get_token_parameter_value(token, value);
11088 // special case: initialize with default values that may be overwritten
11089 // (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz")
11090 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
11094 char *token_prefix;
11095 struct RectWithBorder *struct_ptr;
11099 { "viewport.window", &viewport.window[i] },
11100 { "viewport.playfield", &viewport.playfield[i] },
11101 { "viewport.door_1", &viewport.door_1[i] },
11102 { "viewport.door_2", &viewport.door_2[i] }
11105 for (j = 0; j < ARRAY_SIZE(vp_struct); j++)
11107 struct TokenIntPtrInfo vp_config[] =
11109 { ".x", &vp_struct[j].struct_ptr->x },
11110 { ".y", &vp_struct[j].struct_ptr->y },
11111 { ".width", &vp_struct[j].struct_ptr->width },
11112 { ".height", &vp_struct[j].struct_ptr->height },
11113 { ".min_width", &vp_struct[j].struct_ptr->min_width },
11114 { ".min_height", &vp_struct[j].struct_ptr->min_height },
11115 { ".max_width", &vp_struct[j].struct_ptr->max_width },
11116 { ".max_height", &vp_struct[j].struct_ptr->max_height },
11117 { ".margin_left", &vp_struct[j].struct_ptr->margin_left },
11118 { ".margin_right", &vp_struct[j].struct_ptr->margin_right },
11119 { ".margin_top", &vp_struct[j].struct_ptr->margin_top },
11120 { ".margin_bottom", &vp_struct[j].struct_ptr->margin_bottom },
11121 { ".border_left", &vp_struct[j].struct_ptr->border_left },
11122 { ".border_right", &vp_struct[j].struct_ptr->border_right },
11123 { ".border_top", &vp_struct[j].struct_ptr->border_top },
11124 { ".border_bottom", &vp_struct[j].struct_ptr->border_bottom },
11125 { ".border_size", &vp_struct[j].struct_ptr->border_size },
11126 { ".align_size", &vp_struct[j].struct_ptr->align_size },
11127 { ".align", &vp_struct[j].struct_ptr->align },
11128 { ".valign", &vp_struct[j].struct_ptr->valign }
11131 for (k = 0; k < ARRAY_SIZE(vp_config); k++)
11133 char *token = getStringCat2(vp_struct[j].token_prefix,
11134 vp_config[k].token);
11135 char *value = getHashEntry(setup_file_hash, token);
11138 *vp_config[k].value = get_token_parameter_value(token, value);
11145 // special case: initialize with default values that may be overwritten
11146 // (e.g., init "[title].fade_mode" from "menu.next_screen.TITLE.fade_mode")
11147 for (i = 0; title_info[i].info != NULL; i++)
11149 struct TitleFadingInfo *info = title_info[i].info;
11150 char *base_token = title_info[i].text;
11152 for (j = 0; title_tokens[j].type != -1; j++)
11154 char *token = getStringCat2(base_token, title_tokens[j].text);
11155 char *value = getHashEntry(setup_file_hash, token);
11159 int parameter_value = get_token_parameter_value(token, value);
11163 *(int *)title_tokens[j].value = (int)parameter_value;
11172 // special case: initialize with default values that may be overwritten
11173 // (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode")
11174 for (i = 0; titlemessage_arrays[i].array != NULL; i++)
11176 struct TitleMessageInfo *array = titlemessage_arrays[i].array;
11177 char *base_token = titlemessage_arrays[i].text;
11179 for (j = 0; titlemessage_tokens[j].type != -1; j++)
11181 char *token = getStringCat2(base_token, titlemessage_tokens[j].text);
11182 char *value = getHashEntry(setup_file_hash, token);
11186 int parameter_value = get_token_parameter_value(token, value);
11188 for (k = 0; k < MAX_NUM_TITLE_MESSAGES; k++)
11192 if (titlemessage_tokens[j].type == TYPE_INTEGER)
11193 *(int *)titlemessage_tokens[j].value = (int)parameter_value;
11195 *(boolean *)titlemessage_tokens[j].value = (boolean)parameter_value;
11205 // special case: check if network and preview player positions are redefined,
11206 // to compare this later against the main menu level preview being redefined
11207 struct TokenIntPtrInfo menu_config_players[] =
11209 { "main.network_players.x", &menu.main.network_players.redefined },
11210 { "main.network_players.y", &menu.main.network_players.redefined },
11211 { "main.preview_players.x", &menu.main.preview_players.redefined },
11212 { "main.preview_players.y", &menu.main.preview_players.redefined },
11213 { "preview.x", &preview.redefined },
11214 { "preview.y", &preview.redefined }
11217 for (i = 0; i < ARRAY_SIZE(menu_config_players); i++)
11218 *menu_config_players[i].value = FALSE;
11220 for (i = 0; i < ARRAY_SIZE(menu_config_players); i++)
11221 if (getHashEntry(setup_file_hash, menu_config_players[i].token) != NULL)
11222 *menu_config_players[i].value = TRUE;
11224 // read (and overwrite with) values that may be specified in config file
11225 for (i = 0; image_config_vars[i].token != NULL; i++)
11227 char *value = getHashEntry(setup_file_hash, image_config_vars[i].token);
11229 // (ignore definitions set to "[DEFAULT]" which are already initialized)
11230 if (value != NULL && !strEqual(value, ARG_DEFAULT))
11231 *image_config_vars[i].value =
11232 get_token_parameter_value(image_config_vars[i].token, value);
11235 freeSetupFileHash(setup_file_hash);
11238 void LoadMenuDesignSettings(void)
11240 char *filename_base = UNDEFINED_FILENAME, *filename_local;
11242 InitMenuDesignSettings_Static();
11243 InitMenuDesignSettings_SpecialPreProcessing();
11245 if (!GFX_OVERRIDE_ARTWORK(ARTWORK_TYPE_GRAPHICS))
11247 // first look for special settings configured in level series config
11248 filename_base = getCustomArtworkLevelConfigFilename(ARTWORK_TYPE_GRAPHICS);
11250 if (fileExists(filename_base))
11251 LoadMenuDesignSettingsFromFilename(filename_base);
11254 filename_local = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
11256 if (filename_local != NULL && !strEqual(filename_base, filename_local))
11257 LoadMenuDesignSettingsFromFilename(filename_local);
11259 InitMenuDesignSettings_SpecialPostProcessing();
11262 void LoadMenuDesignSettings_AfterGraphics(void)
11264 InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics();
11267 void LoadUserDefinedEditorElementList(int **elements, int *num_elements)
11269 char *filename = getEditorSetupFilename();
11270 SetupFileList *setup_file_list, *list;
11271 SetupFileHash *element_hash;
11272 int num_unknown_tokens = 0;
11275 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
11278 element_hash = newSetupFileHash();
11280 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
11281 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
11283 // determined size may be larger than needed (due to unknown elements)
11285 for (list = setup_file_list; list != NULL; list = list->next)
11288 // add space for up to 3 more elements for padding that may be needed
11289 *num_elements += 3;
11291 // free memory for old list of elements, if needed
11292 checked_free(*elements);
11294 // allocate memory for new list of elements
11295 *elements = checked_malloc(*num_elements * sizeof(int));
11298 for (list = setup_file_list; list != NULL; list = list->next)
11300 char *value = getHashEntry(element_hash, list->token);
11302 if (value == NULL) // try to find obsolete token mapping
11304 char *mapped_token = get_mapped_token(list->token);
11306 if (mapped_token != NULL)
11308 value = getHashEntry(element_hash, mapped_token);
11310 free(mapped_token);
11316 (*elements)[(*num_elements)++] = atoi(value);
11320 if (num_unknown_tokens == 0)
11322 Error(ERR_INFO_LINE, "-");
11323 Error(ERR_INFO, "warning: unknown token(s) found in config file:");
11324 Error(ERR_INFO, "- config file: '%s'", filename);
11326 num_unknown_tokens++;
11329 Error(ERR_INFO, "- token: '%s'", list->token);
11333 if (num_unknown_tokens > 0)
11334 Error(ERR_INFO_LINE, "-");
11336 while (*num_elements % 4) // pad with empty elements, if needed
11337 (*elements)[(*num_elements)++] = EL_EMPTY;
11339 freeSetupFileList(setup_file_list);
11340 freeSetupFileHash(element_hash);
11343 for (i = 0; i < *num_elements; i++)
11344 printf("editor: element '%s' [%d]\n",
11345 element_info[(*elements)[i]].token_name, (*elements)[i]);
11349 static struct MusicFileInfo *get_music_file_info_ext(char *basename, int music,
11352 SetupFileHash *setup_file_hash = NULL;
11353 struct MusicFileInfo tmp_music_file_info, *new_music_file_info;
11354 char *filename_music, *filename_prefix, *filename_info;
11360 token_to_value_ptr[] =
11362 { "title_header", &tmp_music_file_info.title_header },
11363 { "artist_header", &tmp_music_file_info.artist_header },
11364 { "album_header", &tmp_music_file_info.album_header },
11365 { "year_header", &tmp_music_file_info.year_header },
11367 { "title", &tmp_music_file_info.title },
11368 { "artist", &tmp_music_file_info.artist },
11369 { "album", &tmp_music_file_info.album },
11370 { "year", &tmp_music_file_info.year },
11376 filename_music = (is_sound ? getCustomSoundFilename(basename) :
11377 getCustomMusicFilename(basename));
11379 if (filename_music == NULL)
11382 // ---------- try to replace file extension ----------
11384 filename_prefix = getStringCopy(filename_music);
11385 if (strrchr(filename_prefix, '.') != NULL)
11386 *strrchr(filename_prefix, '.') = '\0';
11387 filename_info = getStringCat2(filename_prefix, ".txt");
11389 if (fileExists(filename_info))
11390 setup_file_hash = loadSetupFileHash(filename_info);
11392 free(filename_prefix);
11393 free(filename_info);
11395 if (setup_file_hash == NULL)
11397 // ---------- try to add file extension ----------
11399 filename_prefix = getStringCopy(filename_music);
11400 filename_info = getStringCat2(filename_prefix, ".txt");
11402 if (fileExists(filename_info))
11403 setup_file_hash = loadSetupFileHash(filename_info);
11405 free(filename_prefix);
11406 free(filename_info);
11409 if (setup_file_hash == NULL)
11412 // ---------- music file info found ----------
11414 clear_mem(&tmp_music_file_info, sizeof(struct MusicFileInfo));
11416 for (i = 0; token_to_value_ptr[i].token != NULL; i++)
11418 char *value = getHashEntry(setup_file_hash, token_to_value_ptr[i].token);
11420 *token_to_value_ptr[i].value_ptr =
11421 getStringCopy(value != NULL && *value != '\0' ? value : UNKNOWN_NAME);
11424 tmp_music_file_info.basename = getStringCopy(basename);
11425 tmp_music_file_info.music = music;
11426 tmp_music_file_info.is_sound = is_sound;
11428 new_music_file_info = checked_malloc(sizeof(struct MusicFileInfo));
11429 *new_music_file_info = tmp_music_file_info;
11431 return new_music_file_info;
11434 static struct MusicFileInfo *get_music_file_info(char *basename, int music)
11436 return get_music_file_info_ext(basename, music, FALSE);
11439 static struct MusicFileInfo *get_sound_file_info(char *basename, int sound)
11441 return get_music_file_info_ext(basename, sound, TRUE);
11444 static boolean music_info_listed_ext(struct MusicFileInfo *list,
11445 char *basename, boolean is_sound)
11447 for (; list != NULL; list = list->next)
11448 if (list->is_sound == is_sound && strEqual(list->basename, basename))
11454 static boolean music_info_listed(struct MusicFileInfo *list, char *basename)
11456 return music_info_listed_ext(list, basename, FALSE);
11459 static boolean sound_info_listed(struct MusicFileInfo *list, char *basename)
11461 return music_info_listed_ext(list, basename, TRUE);
11464 void LoadMusicInfo(void)
11466 char *music_directory = getCustomMusicDirectory();
11467 int num_music = getMusicListSize();
11468 int num_music_noconf = 0;
11469 int num_sounds = getSoundListSize();
11471 DirectoryEntry *dir_entry;
11472 struct FileInfo *music, *sound;
11473 struct MusicFileInfo *next, **new;
11476 while (music_file_info != NULL)
11478 next = music_file_info->next;
11480 checked_free(music_file_info->basename);
11482 checked_free(music_file_info->title_header);
11483 checked_free(music_file_info->artist_header);
11484 checked_free(music_file_info->album_header);
11485 checked_free(music_file_info->year_header);
11487 checked_free(music_file_info->title);
11488 checked_free(music_file_info->artist);
11489 checked_free(music_file_info->album);
11490 checked_free(music_file_info->year);
11492 free(music_file_info);
11494 music_file_info = next;
11497 new = &music_file_info;
11499 for (i = 0; i < num_music; i++)
11501 music = getMusicListEntry(i);
11503 if (music->filename == NULL)
11506 if (strEqual(music->filename, UNDEFINED_FILENAME))
11509 // a configured file may be not recognized as music
11510 if (!FileIsMusic(music->filename))
11513 if (!music_info_listed(music_file_info, music->filename))
11515 *new = get_music_file_info(music->filename, i);
11518 new = &(*new)->next;
11522 if ((dir = openDirectory(music_directory)) == NULL)
11524 Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
11528 while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries
11530 char *basename = dir_entry->basename;
11531 boolean music_already_used = FALSE;
11534 // skip all music files that are configured in music config file
11535 for (i = 0; i < num_music; i++)
11537 music = getMusicListEntry(i);
11539 if (music->filename == NULL)
11542 if (strEqual(basename, music->filename))
11544 music_already_used = TRUE;
11549 if (music_already_used)
11552 if (!FileIsMusic(dir_entry->filename))
11555 if (!music_info_listed(music_file_info, basename))
11557 *new = get_music_file_info(basename, MAP_NOCONF_MUSIC(num_music_noconf));
11560 new = &(*new)->next;
11563 num_music_noconf++;
11566 closeDirectory(dir);
11568 for (i = 0; i < num_sounds; i++)
11570 sound = getSoundListEntry(i);
11572 if (sound->filename == NULL)
11575 if (strEqual(sound->filename, UNDEFINED_FILENAME))
11578 // a configured file may be not recognized as sound
11579 if (!FileIsSound(sound->filename))
11582 if (!sound_info_listed(music_file_info, sound->filename))
11584 *new = get_sound_file_info(sound->filename, i);
11586 new = &(*new)->next;
11591 static void add_helpanim_entry(int element, int action, int direction,
11592 int delay, int *num_list_entries)
11594 struct HelpAnimInfo *new_list_entry;
11595 (*num_list_entries)++;
11598 checked_realloc(helpanim_info,
11599 *num_list_entries * sizeof(struct HelpAnimInfo));
11600 new_list_entry = &helpanim_info[*num_list_entries - 1];
11602 new_list_entry->element = element;
11603 new_list_entry->action = action;
11604 new_list_entry->direction = direction;
11605 new_list_entry->delay = delay;
11608 static void print_unknown_token(char *filename, char *token, int token_nr)
11612 Error(ERR_INFO_LINE, "-");
11613 Error(ERR_INFO, "warning: unknown token(s) found in config file:");
11614 Error(ERR_INFO, "- config file: '%s'", filename);
11617 Error(ERR_INFO, "- token: '%s'", token);
11620 static void print_unknown_token_end(int token_nr)
11623 Error(ERR_INFO_LINE, "-");
11626 void LoadHelpAnimInfo(void)
11628 char *filename = getHelpAnimFilename();
11629 SetupFileList *setup_file_list = NULL, *list;
11630 SetupFileHash *element_hash, *action_hash, *direction_hash;
11631 int num_list_entries = 0;
11632 int num_unknown_tokens = 0;
11635 if (fileExists(filename))
11636 setup_file_list = loadSetupFileList(filename);
11638 if (setup_file_list == NULL)
11640 // use reliable default values from static configuration
11641 SetupFileList *insert_ptr;
11643 insert_ptr = setup_file_list =
11644 newSetupFileList(helpanim_config[0].token,
11645 helpanim_config[0].value);
11647 for (i = 1; helpanim_config[i].token; i++)
11648 insert_ptr = addListEntry(insert_ptr,
11649 helpanim_config[i].token,
11650 helpanim_config[i].value);
11653 element_hash = newSetupFileHash();
11654 action_hash = newSetupFileHash();
11655 direction_hash = newSetupFileHash();
11657 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
11658 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
11660 for (i = 0; i < NUM_ACTIONS; i++)
11661 setHashEntry(action_hash, element_action_info[i].suffix,
11662 i_to_a(element_action_info[i].value));
11664 // do not store direction index (bit) here, but direction value!
11665 for (i = 0; i < NUM_DIRECTIONS_FULL; i++)
11666 setHashEntry(direction_hash, element_direction_info[i].suffix,
11667 i_to_a(1 << element_direction_info[i].value));
11669 for (list = setup_file_list; list != NULL; list = list->next)
11671 char *element_token, *action_token, *direction_token;
11672 char *element_value, *action_value, *direction_value;
11673 int delay = atoi(list->value);
11675 if (strEqual(list->token, "end"))
11677 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
11682 /* first try to break element into element/action/direction parts;
11683 if this does not work, also accept combined "element[.act][.dir]"
11684 elements (like "dynamite.active"), which are unique elements */
11686 if (strchr(list->token, '.') == NULL) // token contains no '.'
11688 element_value = getHashEntry(element_hash, list->token);
11689 if (element_value != NULL) // element found
11690 add_helpanim_entry(atoi(element_value), -1, -1, delay,
11691 &num_list_entries);
11694 // no further suffixes found -- this is not an element
11695 print_unknown_token(filename, list->token, num_unknown_tokens++);
11701 // token has format "<prefix>.<something>"
11703 action_token = strchr(list->token, '.'); // suffix may be action ...
11704 direction_token = action_token; // ... or direction
11706 element_token = getStringCopy(list->token);
11707 *strchr(element_token, '.') = '\0';
11709 element_value = getHashEntry(element_hash, element_token);
11711 if (element_value == NULL) // this is no element
11713 element_value = getHashEntry(element_hash, list->token);
11714 if (element_value != NULL) // combined element found
11715 add_helpanim_entry(atoi(element_value), -1, -1, delay,
11716 &num_list_entries);
11718 print_unknown_token(filename, list->token, num_unknown_tokens++);
11720 free(element_token);
11725 action_value = getHashEntry(action_hash, action_token);
11727 if (action_value != NULL) // action found
11729 add_helpanim_entry(atoi(element_value), atoi(action_value), -1, delay,
11730 &num_list_entries);
11732 free(element_token);
11737 direction_value = getHashEntry(direction_hash, direction_token);
11739 if (direction_value != NULL) // direction found
11741 add_helpanim_entry(atoi(element_value), -1, atoi(direction_value), delay,
11742 &num_list_entries);
11744 free(element_token);
11749 if (strchr(action_token + 1, '.') == NULL)
11751 // no further suffixes found -- this is not an action nor direction
11753 element_value = getHashEntry(element_hash, list->token);
11754 if (element_value != NULL) // combined element found
11755 add_helpanim_entry(atoi(element_value), -1, -1, delay,
11756 &num_list_entries);
11758 print_unknown_token(filename, list->token, num_unknown_tokens++);
11760 free(element_token);
11765 // token has format "<prefix>.<suffix>.<something>"
11767 direction_token = strchr(action_token + 1, '.');
11769 action_token = getStringCopy(action_token);
11770 *strchr(action_token + 1, '.') = '\0';
11772 action_value = getHashEntry(action_hash, action_token);
11774 if (action_value == NULL) // this is no action
11776 element_value = getHashEntry(element_hash, list->token);
11777 if (element_value != NULL) // combined element found
11778 add_helpanim_entry(atoi(element_value), -1, -1, delay,
11779 &num_list_entries);
11781 print_unknown_token(filename, list->token, num_unknown_tokens++);
11783 free(element_token);
11784 free(action_token);
11789 direction_value = getHashEntry(direction_hash, direction_token);
11791 if (direction_value != NULL) // direction found
11793 add_helpanim_entry(atoi(element_value), atoi(action_value),
11794 atoi(direction_value), delay, &num_list_entries);
11796 free(element_token);
11797 free(action_token);
11802 // this is no direction
11804 element_value = getHashEntry(element_hash, list->token);
11805 if (element_value != NULL) // combined element found
11806 add_helpanim_entry(atoi(element_value), -1, -1, delay,
11807 &num_list_entries);
11809 print_unknown_token(filename, list->token, num_unknown_tokens++);
11811 free(element_token);
11812 free(action_token);
11815 print_unknown_token_end(num_unknown_tokens);
11817 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
11818 add_helpanim_entry(HELPANIM_LIST_END, -1, -1, -1, &num_list_entries);
11820 freeSetupFileList(setup_file_list);
11821 freeSetupFileHash(element_hash);
11822 freeSetupFileHash(action_hash);
11823 freeSetupFileHash(direction_hash);
11826 for (i = 0; i < num_list_entries; i++)
11827 printf("::: '%s': %d, %d, %d => %d\n",
11828 EL_NAME(helpanim_info[i].element),
11829 helpanim_info[i].element,
11830 helpanim_info[i].action,
11831 helpanim_info[i].direction,
11832 helpanim_info[i].delay);
11836 void LoadHelpTextInfo(void)
11838 char *filename = getHelpTextFilename();
11841 if (helptext_info != NULL)
11843 freeSetupFileHash(helptext_info);
11844 helptext_info = NULL;
11847 if (fileExists(filename))
11848 helptext_info = loadSetupFileHash(filename);
11850 if (helptext_info == NULL)
11852 // use reliable default values from static configuration
11853 helptext_info = newSetupFileHash();
11855 for (i = 0; helptext_config[i].token; i++)
11856 setHashEntry(helptext_info,
11857 helptext_config[i].token,
11858 helptext_config[i].value);
11862 BEGIN_HASH_ITERATION(helptext_info, itr)
11864 printf("::: '%s' => '%s'\n",
11865 HASH_ITERATION_TOKEN(itr), HASH_ITERATION_VALUE(itr));
11867 END_HASH_ITERATION(hash, itr)
11872 // ----------------------------------------------------------------------------
11874 // ----------------------------------------------------------------------------
11876 #define MAX_NUM_CONVERT_LEVELS 1000
11878 void ConvertLevels(void)
11880 static LevelDirTree *convert_leveldir = NULL;
11881 static int convert_level_nr = -1;
11882 static int num_levels_handled = 0;
11883 static int num_levels_converted = 0;
11884 static boolean levels_failed[MAX_NUM_CONVERT_LEVELS];
11887 convert_leveldir = getTreeInfoFromIdentifier(leveldir_first,
11888 global.convert_leveldir);
11890 if (convert_leveldir == NULL)
11891 Error(ERR_EXIT, "no such level identifier: '%s'",
11892 global.convert_leveldir);
11894 leveldir_current = convert_leveldir;
11896 if (global.convert_level_nr != -1)
11898 convert_leveldir->first_level = global.convert_level_nr;
11899 convert_leveldir->last_level = global.convert_level_nr;
11902 convert_level_nr = convert_leveldir->first_level;
11904 PrintLine("=", 79);
11905 Print("Converting levels\n");
11906 PrintLine("-", 79);
11907 Print("Level series identifier: '%s'\n", convert_leveldir->identifier);
11908 Print("Level series name: '%s'\n", convert_leveldir->name);
11909 Print("Level series author: '%s'\n", convert_leveldir->author);
11910 Print("Number of levels: %d\n", convert_leveldir->levels);
11911 PrintLine("=", 79);
11914 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
11915 levels_failed[i] = FALSE;
11917 while (convert_level_nr <= convert_leveldir->last_level)
11919 char *level_filename;
11922 level_nr = convert_level_nr++;
11924 Print("Level %03d: ", level_nr);
11926 LoadLevel(level_nr);
11927 if (level.no_level_file || level.no_valid_file)
11929 Print("(no level)\n");
11933 Print("converting level ... ");
11935 level_filename = getDefaultLevelFilename(level_nr);
11936 new_level = !fileExists(level_filename);
11940 SaveLevel(level_nr);
11942 num_levels_converted++;
11944 Print("converted.\n");
11948 if (level_nr >= 0 && level_nr < MAX_NUM_CONVERT_LEVELS)
11949 levels_failed[level_nr] = TRUE;
11951 Print("NOT CONVERTED -- LEVEL ALREADY EXISTS.\n");
11954 num_levels_handled++;
11958 PrintLine("=", 79);
11959 Print("Number of levels handled: %d\n", num_levels_handled);
11960 Print("Number of levels converted: %d (%d%%)\n", num_levels_converted,
11961 (num_levels_handled ?
11962 num_levels_converted * 100 / num_levels_handled : 0));
11963 PrintLine("-", 79);
11964 Print("Summary (for automatic parsing by scripts):\n");
11965 Print("LEVELDIR '%s', CONVERTED %d/%d (%d%%)",
11966 convert_leveldir->identifier, num_levels_converted,
11967 num_levels_handled,
11968 (num_levels_handled ?
11969 num_levels_converted * 100 / num_levels_handled : 0));
11971 if (num_levels_handled != num_levels_converted)
11973 Print(", FAILED:");
11974 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
11975 if (levels_failed[i])
11980 PrintLine("=", 79);
11982 CloseAllAndExit(0);
11986 // ----------------------------------------------------------------------------
11987 // create and save images for use in level sketches (raw BMP format)
11988 // ----------------------------------------------------------------------------
11990 void CreateLevelSketchImages(void)
11996 InitElementPropertiesGfxElement();
11998 bitmap1 = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
11999 bitmap2 = CreateBitmap(MINI_TILEX, MINI_TILEY, DEFAULT_DEPTH);
12001 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
12003 int element = getMappedElement(i);
12004 char basename1[16];
12005 char basename2[16];
12009 sprintf(basename1, "%04d.bmp", i);
12010 sprintf(basename2, "%04ds.bmp", i);
12012 filename1 = getPath2(global.create_images_dir, basename1);
12013 filename2 = getPath2(global.create_images_dir, basename2);
12015 DrawSizedElement(0, 0, element, TILESIZE);
12016 BlitBitmap(drawto, bitmap1, SX, SY, TILEX, TILEY, 0, 0);
12018 if (SDL_SaveBMP(bitmap1->surface, filename1) != 0)
12019 Error(ERR_EXIT, "cannot save level sketch image file '%s'", filename1);
12021 DrawSizedElement(0, 0, element, MINI_TILESIZE);
12022 BlitBitmap(drawto, bitmap2, SX, SY, MINI_TILEX, MINI_TILEY, 0, 0);
12024 if (SDL_SaveBMP(bitmap2->surface, filename2) != 0)
12025 Error(ERR_EXIT, "cannot save level sketch image file '%s'", filename2);
12030 // create corresponding SQL statements (for normal and small images)
12033 printf("insert into phpbb_words values (NULL, '`%03d', '<IMG class=\"levelsketch\" src=\"/I/%04d.png\"/>');\n", i, i);
12034 printf("insert into phpbb_words values (NULL, '¸%03d', '<IMG class=\"levelsketch\" src=\"/I/%04ds.png\"/>');\n", i, i);
12037 printf("insert into phpbb_words values (NULL, '`%04d', '<IMG class=\"levelsketch\" src=\"/I/%04d.png\"/>');\n", i, i);
12038 printf("insert into phpbb_words values (NULL, '¸%04d', '<IMG class=\"levelsketch\" src=\"/I/%04ds.png\"/>');\n", i, i);
12040 // optional: create content for forum level sketch demonstration post
12042 fprintf(stderr, "%03d `%03d%c", i, i, (i % 10 < 9 ? ' ' : '\n'));
12045 FreeBitmap(bitmap1);
12046 FreeBitmap(bitmap2);
12049 fprintf(stderr, "\n");
12051 Error(ERR_INFO, "%d normal and small images created", NUM_FILE_ELEMENTS);
12053 CloseAllAndExit(0);
12057 // ----------------------------------------------------------------------------
12058 // create and save images for custom and group elements (raw BMP format)
12059 // ----------------------------------------------------------------------------
12061 void CreateCustomElementImages(char *directory)
12063 char *src_basename = "RocksCE-template.ilbm";
12064 char *dst_basename = "RocksCE.bmp";
12065 char *src_filename = getPath2(directory, src_basename);
12066 char *dst_filename = getPath2(directory, dst_basename);
12067 Bitmap *src_bitmap;
12069 int yoffset_ce = 0;
12070 int yoffset_ge = (TILEY * NUM_CUSTOM_ELEMENTS / 16);
12073 InitVideoDefaults();
12075 ReCreateBitmap(&backbuffer, video.width, video.height);
12077 src_bitmap = LoadImage(src_filename);
12079 bitmap = CreateBitmap(TILEX * 16 * 2,
12080 TILEY * (NUM_CUSTOM_ELEMENTS + NUM_GROUP_ELEMENTS) / 16,
12083 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
12090 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
12091 TILEX * x, TILEY * y + yoffset_ce);
12093 BlitBitmap(src_bitmap, bitmap, 0, TILEY,
12095 TILEX * x + TILEX * 16,
12096 TILEY * y + yoffset_ce);
12098 for (j = 2; j >= 0; j--)
12102 BlitBitmap(src_bitmap, bitmap,
12103 TILEX + c * 7, 0, 6, 10,
12104 TILEX * x + 6 + j * 7,
12105 TILEY * y + 11 + yoffset_ce);
12107 BlitBitmap(src_bitmap, bitmap,
12108 TILEX + c * 8, TILEY, 6, 10,
12109 TILEX * 16 + TILEX * x + 6 + j * 8,
12110 TILEY * y + 10 + yoffset_ce);
12116 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
12123 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
12124 TILEX * x, TILEY * y + yoffset_ge);
12126 BlitBitmap(src_bitmap, bitmap, 0, TILEY,
12128 TILEX * x + TILEX * 16,
12129 TILEY * y + yoffset_ge);
12131 for (j = 1; j >= 0; j--)
12135 BlitBitmap(src_bitmap, bitmap, TILEX + c * 10, 11, 10, 10,
12136 TILEX * x + 6 + j * 10,
12137 TILEY * y + 11 + yoffset_ge);
12139 BlitBitmap(src_bitmap, bitmap,
12140 TILEX + c * 8, TILEY + 12, 6, 10,
12141 TILEX * 16 + TILEX * x + 10 + j * 8,
12142 TILEY * y + 10 + yoffset_ge);
12148 if (SDL_SaveBMP(bitmap->surface, dst_filename) != 0)
12149 Error(ERR_EXIT, "cannot save CE graphics file '%s'", dst_filename);
12151 FreeBitmap(bitmap);
12153 CloseAllAndExit(0);