1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://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 1 // unused tape header bytes
62 #define TAPE_CHUNK_SCRN_SIZE 2 // size of screen size chunk
64 #define SCORE_CHUNK_VERS_SIZE 8 // size of file version chunk
66 #define LEVEL_CHUNK_CNT3_SIZE(x) (LEVEL_CHUNK_CNT3_HEADER + (x))
67 #define LEVEL_CHUNK_CUS3_SIZE(x) (2 + (x) * LEVEL_CPART_CUS3_SIZE)
68 #define LEVEL_CHUNK_CUS4_SIZE(x) (96 + (x) * 48)
70 // file identifier strings
71 #define LEVEL_COOKIE_TMPL "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_x.x"
72 #define TAPE_COOKIE_TMPL "ROCKSNDIAMONDS_TAPE_FILE_VERSION_x.x"
73 #define SCORE_COOKIE_TMPL "ROCKSNDIAMONDS_SCORE_FILE_VERSION_x.x"
75 // values for deciding when (not) to save configuration data
76 #define SAVE_CONF_NEVER 0
77 #define SAVE_CONF_ALWAYS 1
78 #define SAVE_CONF_WHEN_CHANGED -1
80 // values for chunks using micro chunks
81 #define CONF_MASK_1_BYTE 0x00
82 #define CONF_MASK_2_BYTE 0x40
83 #define CONF_MASK_4_BYTE 0x80
84 #define CONF_MASK_MULTI_BYTES 0xc0
86 #define CONF_MASK_BYTES 0xc0
87 #define CONF_MASK_TOKEN 0x3f
89 #define CONF_VALUE_1_BYTE(x) (CONF_MASK_1_BYTE | (x))
90 #define CONF_VALUE_2_BYTE(x) (CONF_MASK_2_BYTE | (x))
91 #define CONF_VALUE_4_BYTE(x) (CONF_MASK_4_BYTE | (x))
92 #define CONF_VALUE_MULTI_BYTES(x) (CONF_MASK_MULTI_BYTES | (x))
94 // these definitions are just for convenience of use and readability
95 #define CONF_VALUE_8_BIT(x) CONF_VALUE_1_BYTE(x)
96 #define CONF_VALUE_16_BIT(x) CONF_VALUE_2_BYTE(x)
97 #define CONF_VALUE_32_BIT(x) CONF_VALUE_4_BYTE(x)
98 #define CONF_VALUE_BYTES(x) CONF_VALUE_MULTI_BYTES(x)
100 #define CONF_VALUE_NUM_BYTES(x) ((x) == CONF_MASK_1_BYTE ? 1 : \
101 (x) == CONF_MASK_2_BYTE ? 2 : \
102 (x) == CONF_MASK_4_BYTE ? 4 : 0)
104 #define CONF_CONTENT_NUM_ELEMENTS (3 * 3)
105 #define CONF_CONTENT_NUM_BYTES (CONF_CONTENT_NUM_ELEMENTS * 2)
106 #define CONF_ELEMENT_NUM_BYTES (2)
108 #define CONF_ENTITY_NUM_BYTES(t) ((t) == TYPE_ELEMENT || \
109 (t) == TYPE_ELEMENT_LIST ? \
110 CONF_ELEMENT_NUM_BYTES : \
111 (t) == TYPE_CONTENT || \
112 (t) == TYPE_CONTENT_LIST ? \
113 CONF_CONTENT_NUM_BYTES : 1)
115 #define CONF_ELEMENT_BYTE_POS(i) ((i) * CONF_ELEMENT_NUM_BYTES)
116 #define CONF_ELEMENTS_ELEMENT(b,i) ((b[CONF_ELEMENT_BYTE_POS(i)] << 8) | \
117 (b[CONF_ELEMENT_BYTE_POS(i) + 1]))
119 #define CONF_CONTENT_ELEMENT_POS(c,x,y) ((c) * CONF_CONTENT_NUM_ELEMENTS + \
121 #define CONF_CONTENT_BYTE_POS(c,x,y) (CONF_CONTENT_ELEMENT_POS(c,x,y) * \
122 CONF_ELEMENT_NUM_BYTES)
123 #define CONF_CONTENTS_ELEMENT(b,c,x,y) ((b[CONF_CONTENT_BYTE_POS(c,x,y)]<< 8)|\
124 (b[CONF_CONTENT_BYTE_POS(c,x,y) + 1]))
126 // temporary variables used to store pointers to structure members
127 static struct LevelInfo li;
128 static struct ElementInfo xx_ei, yy_ei;
129 static struct ElementChangeInfo xx_change;
130 static struct ElementGroupInfo xx_group;
131 static struct EnvelopeInfo xx_envelope;
132 static unsigned int xx_event_bits[NUM_CE_BITFIELDS];
133 static char xx_default_description[MAX_ELEMENT_NAME_LEN + 1];
134 static int xx_num_contents;
135 static int xx_current_change_page;
136 static char xx_default_string_empty[1] = "";
137 static int xx_string_length_unused;
139 struct LevelFileConfigInfo
141 int element; // element for which data is to be stored
142 int save_type; // save data always, never or when changed
143 int data_type; // data type (used internally, not stored)
144 int conf_type; // micro chunk identifier (stored in file)
147 void *value; // variable that holds the data to be stored
148 int default_value; // initial default value for this variable
151 void *value_copy; // variable that holds the data to be copied
152 void *num_entities; // number of entities for multi-byte data
153 int default_num_entities; // default number of entities for this data
154 int max_num_entities; // maximal number of entities for this data
155 char *default_string; // optional default string for string data
158 static struct LevelFileConfigInfo chunk_config_INFO[] =
160 // ---------- values not related to single elements -------------------------
163 -1, SAVE_CONF_ALWAYS,
164 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
165 &li.game_engine_type, GAME_ENGINE_TYPE_RND
169 -1, SAVE_CONF_ALWAYS,
170 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
171 &li.fieldx, STD_LEV_FIELDX
174 -1, SAVE_CONF_ALWAYS,
175 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
176 &li.fieldy, STD_LEV_FIELDY
180 -1, SAVE_CONF_ALWAYS,
181 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
186 -1, SAVE_CONF_ALWAYS,
187 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
193 TYPE_INTEGER, CONF_VALUE_32_BIT(2),
199 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
200 &li.use_step_counter, FALSE
205 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
206 &li.wind_direction_initial, MV_NONE
211 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
212 &li.em_slippery_gems, FALSE
217 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
218 &li.use_custom_template, FALSE
223 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
224 &li.can_move_into_acid_bits, ~0 // default: everything can
229 TYPE_BITFIELD, CONF_VALUE_8_BIT(7),
230 &li.dont_collide_with_bits, ~0 // default: always deadly
235 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
236 &li.em_explodes_by_fire, FALSE
241 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
242 &li.score[SC_TIME_BONUS], 1
247 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
248 &li.auto_exit_sokoban, FALSE
253 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
254 &li.auto_count_gems, FALSE
259 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
260 &li.solved_by_one_player, FALSE
265 TYPE_INTEGER, CONF_VALUE_8_BIT(12),
266 &li.time_score_base, 1
271 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
272 &li.rate_time_over_score, FALSE
282 static struct LevelFileConfigInfo chunk_config_ELEM[] =
284 // (these values are the same for each player)
287 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
288 &li.block_last_field, FALSE // default case for EM levels
292 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
293 &li.sp_block_last_field, TRUE // default case for SP levels
297 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
298 &li.instant_relocation, FALSE
302 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
303 &li.can_pass_to_walkable, FALSE
307 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
308 &li.block_snap_field, TRUE
312 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
313 &li.continuous_snapping, TRUE
317 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
318 &li.shifted_relocation, FALSE
322 TYPE_BOOLEAN, CONF_VALUE_8_BIT(15),
323 &li.lazy_relocation, FALSE
327 TYPE_BOOLEAN, CONF_VALUE_8_BIT(16),
328 &li.finish_dig_collect, TRUE
332 TYPE_BOOLEAN, CONF_VALUE_8_BIT(17),
333 &li.keep_walkable_ce, FALSE
336 // (these values are different for each player)
339 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
340 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
344 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
345 &li.initial_player_gravity[0], FALSE
349 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
350 &li.use_start_element[0], FALSE
354 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
355 &li.start_element[0], EL_PLAYER_1
359 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
360 &li.use_artwork_element[0], FALSE
364 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
365 &li.artwork_element[0], EL_PLAYER_1
369 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
370 &li.use_explosion_element[0], FALSE
374 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
375 &li.explosion_element[0], EL_PLAYER_1
379 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
380 &li.use_initial_inventory[0], FALSE
384 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
385 &li.initial_inventory_size[0], 1
389 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
390 &li.initial_inventory_content[0][0],EL_EMPTY, NULL,
391 &li.initial_inventory_size[0], 1, MAX_INITIAL_INVENTORY_SIZE
396 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
397 &li.initial_player_stepsize[1], STEPSIZE_NORMAL
401 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
402 &li.initial_player_gravity[1], FALSE
406 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
407 &li.use_start_element[1], FALSE
411 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
412 &li.start_element[1], EL_PLAYER_2
416 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
417 &li.use_artwork_element[1], FALSE
421 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
422 &li.artwork_element[1], EL_PLAYER_2
426 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
427 &li.use_explosion_element[1], FALSE
431 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
432 &li.explosion_element[1], EL_PLAYER_2
436 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
437 &li.use_initial_inventory[1], FALSE
441 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
442 &li.initial_inventory_size[1], 1
446 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
447 &li.initial_inventory_content[1][0],EL_EMPTY, NULL,
448 &li.initial_inventory_size[1], 1, MAX_INITIAL_INVENTORY_SIZE
453 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
454 &li.initial_player_stepsize[2], STEPSIZE_NORMAL
458 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
459 &li.initial_player_gravity[2], FALSE
463 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
464 &li.use_start_element[2], FALSE
468 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
469 &li.start_element[2], EL_PLAYER_3
473 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
474 &li.use_artwork_element[2], FALSE
478 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
479 &li.artwork_element[2], EL_PLAYER_3
483 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
484 &li.use_explosion_element[2], FALSE
488 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
489 &li.explosion_element[2], EL_PLAYER_3
493 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
494 &li.use_initial_inventory[2], FALSE
498 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
499 &li.initial_inventory_size[2], 1
503 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
504 &li.initial_inventory_content[2][0],EL_EMPTY, NULL,
505 &li.initial_inventory_size[2], 1, MAX_INITIAL_INVENTORY_SIZE
510 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
511 &li.initial_player_stepsize[3], STEPSIZE_NORMAL
515 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
516 &li.initial_player_gravity[3], FALSE
520 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
521 &li.use_start_element[3], FALSE
525 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
526 &li.start_element[3], EL_PLAYER_4
530 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
531 &li.use_artwork_element[3], FALSE
535 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
536 &li.artwork_element[3], EL_PLAYER_4
540 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
541 &li.use_explosion_element[3], FALSE
545 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
546 &li.explosion_element[3], EL_PLAYER_4
550 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
551 &li.use_initial_inventory[3], FALSE
555 TYPE_BOOLEAN, CONF_VALUE_8_BIT(14),
556 &li.initial_inventory_size[3], 1
560 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
561 &li.initial_inventory_content[3][0],EL_EMPTY, NULL,
562 &li.initial_inventory_size[3], 1, MAX_INITIAL_INVENTORY_SIZE
567 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
568 &li.score[SC_EMERALD], 10
573 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
574 &li.score[SC_DIAMOND], 10
579 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
580 &li.score[SC_BUG], 10
585 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
586 &li.score[SC_SPACESHIP], 10
591 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
592 &li.score[SC_PACMAN], 10
597 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
598 &li.score[SC_NUT], 10
603 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
604 &li.score[SC_DYNAMITE], 10
609 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
610 &li.score[SC_KEY], 10
615 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
616 &li.score[SC_PEARL], 10
621 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
622 &li.score[SC_CRYSTAL], 10
627 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
628 &li.amoeba_content, EL_DIAMOND
632 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
637 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
638 &li.grow_into_diggable, TRUE
643 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
644 &li.yamyam_content, EL_ROCK, NULL,
645 &li.num_yamyam_contents, 4, MAX_ELEMENT_CONTENTS
649 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
650 &li.score[SC_YAMYAM], 10
655 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
656 &li.score[SC_ROBOT], 10
660 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
666 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
672 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
673 &li.time_magic_wall, 10
678 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
679 &li.game_of_life[0], 2
683 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
684 &li.game_of_life[1], 3
688 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
689 &li.game_of_life[2], 3
693 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
694 &li.game_of_life[3], 3
698 TYPE_BOOLEAN, CONF_VALUE_8_BIT(5),
699 &li.use_life_bugs, FALSE
704 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
709 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
714 TYPE_INTEGER, CONF_VALUE_8_BIT(3),
719 TYPE_INTEGER, CONF_VALUE_8_BIT(4),
724 EL_TIMEGATE_SWITCH, -1,
725 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
726 &li.time_timegate, 10
730 EL_LIGHT_SWITCH_ACTIVE, -1,
731 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
736 EL_SHIELD_NORMAL, -1,
737 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
738 &li.shield_normal_time, 10
741 EL_SHIELD_NORMAL, -1,
742 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
743 &li.score[SC_SHIELD], 10
747 EL_SHIELD_DEADLY, -1,
748 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
749 &li.shield_deadly_time, 10
752 EL_SHIELD_DEADLY, -1,
753 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
754 &li.score[SC_SHIELD], 10
759 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
764 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
765 &li.extra_time_score, 10
769 EL_TIME_ORB_FULL, -1,
770 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
771 &li.time_orb_time, 10
774 EL_TIME_ORB_FULL, -1,
775 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
776 &li.use_time_orb_bug, FALSE
781 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
782 &li.use_spring_bug, FALSE
787 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
788 &li.android_move_time, 10
792 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
793 &li.android_clone_time, 10
796 EL_EMC_ANDROID, SAVE_CONF_NEVER,
797 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(1),
798 &li.android_clone_element[0], EL_EMPTY, NULL,
799 &li.num_android_clone_elements, 1, MAX_ANDROID_ELEMENTS_OLD
803 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(2),
804 &li.android_clone_element[0], EL_EMPTY, NULL,
805 &li.num_android_clone_elements, 1, MAX_ANDROID_ELEMENTS
810 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
815 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
820 EL_EMC_MAGNIFIER, -1,
821 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
822 &li.magnify_score, 10
825 EL_EMC_MAGNIFIER, -1,
826 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
831 EL_EMC_MAGIC_BALL, -1,
832 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
836 EL_EMC_MAGIC_BALL, -1,
837 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
838 &li.ball_random, FALSE
841 EL_EMC_MAGIC_BALL, -1,
842 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
843 &li.ball_active_initial, FALSE
846 EL_EMC_MAGIC_BALL, -1,
847 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
848 &li.ball_content, EL_EMPTY, NULL,
849 &li.num_ball_contents, 4, MAX_ELEMENT_CONTENTS
853 EL_SOKOBAN_FIELD_EMPTY, -1,
854 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
855 &li.sb_fields_needed, TRUE
859 EL_SOKOBAN_OBJECT, -1,
860 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
861 &li.sb_objects_needed, TRUE
866 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
867 &li.mm_laser_red, FALSE
871 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
872 &li.mm_laser_green, FALSE
876 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
877 &li.mm_laser_blue, TRUE
882 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
883 &li.df_laser_red, TRUE
887 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
888 &li.df_laser_green, TRUE
892 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
893 &li.df_laser_blue, FALSE
897 EL_MM_FUSE_ACTIVE, -1,
898 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
903 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
908 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
912 EL_MM_STEEL_BLOCK, -1,
913 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
914 &li.mm_time_block, 75
918 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
919 &li.score[SC_ELEM_BONUS], 10
922 // ---------- unused values -------------------------------------------------
925 EL_UNKNOWN, SAVE_CONF_NEVER,
926 TYPE_INTEGER, CONF_VALUE_16_BIT(1),
927 &li.score[SC_UNKNOWN_15], 10
937 static struct LevelFileConfigInfo chunk_config_NOTE[] =
941 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
942 &xx_envelope.xsize, MAX_ENVELOPE_XSIZE,
946 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
947 &xx_envelope.ysize, MAX_ENVELOPE_YSIZE,
952 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
953 &xx_envelope.autowrap, FALSE
957 TYPE_BOOLEAN, CONF_VALUE_8_BIT(4),
958 &xx_envelope.centered, FALSE
963 TYPE_STRING, CONF_VALUE_BYTES(1),
964 &xx_envelope.text, -1, NULL,
965 &xx_string_length_unused, -1, MAX_ENVELOPE_TEXT_LEN,
966 &xx_default_string_empty[0]
976 static struct LevelFileConfigInfo chunk_config_CUSX_base[] =
980 TYPE_STRING, CONF_VALUE_BYTES(1),
981 &xx_ei.description[0], -1,
982 &yy_ei.description[0],
983 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
984 &xx_default_description[0]
989 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
990 &xx_ei.properties[EP_BITFIELD_BASE_NR], EP_BITMASK_BASE_DEFAULT,
991 &yy_ei.properties[EP_BITFIELD_BASE_NR]
993 #if ENABLE_RESERVED_CODE
994 // (reserved for later use)
997 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
998 &xx_ei.properties[EP_BITFIELD_BASE_NR + 1], EP_BITMASK_DEFAULT,
999 &yy_ei.properties[EP_BITFIELD_BASE_NR + 1]
1005 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
1006 &xx_ei.use_gfx_element, FALSE,
1007 &yy_ei.use_gfx_element
1011 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1012 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE,
1013 &yy_ei.gfx_element_initial
1018 TYPE_BITFIELD, CONF_VALUE_8_BIT(2),
1019 &xx_ei.access_direction, MV_ALL_DIRECTIONS,
1020 &yy_ei.access_direction
1025 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
1026 &xx_ei.collect_score_initial, 10,
1027 &yy_ei.collect_score_initial
1031 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
1032 &xx_ei.collect_count_initial, 1,
1033 &yy_ei.collect_count_initial
1038 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
1039 &xx_ei.ce_value_fixed_initial, 0,
1040 &yy_ei.ce_value_fixed_initial
1044 TYPE_INTEGER, CONF_VALUE_16_BIT(5),
1045 &xx_ei.ce_value_random_initial, 0,
1046 &yy_ei.ce_value_random_initial
1050 TYPE_BOOLEAN, CONF_VALUE_8_BIT(3),
1051 &xx_ei.use_last_ce_value, FALSE,
1052 &yy_ei.use_last_ce_value
1057 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
1058 &xx_ei.push_delay_fixed, 8,
1059 &yy_ei.push_delay_fixed
1063 TYPE_INTEGER, CONF_VALUE_16_BIT(7),
1064 &xx_ei.push_delay_random, 8,
1065 &yy_ei.push_delay_random
1069 TYPE_INTEGER, CONF_VALUE_16_BIT(8),
1070 &xx_ei.drop_delay_fixed, 0,
1071 &yy_ei.drop_delay_fixed
1075 TYPE_INTEGER, CONF_VALUE_16_BIT(9),
1076 &xx_ei.drop_delay_random, 0,
1077 &yy_ei.drop_delay_random
1081 TYPE_INTEGER, CONF_VALUE_16_BIT(10),
1082 &xx_ei.move_delay_fixed, 0,
1083 &yy_ei.move_delay_fixed
1087 TYPE_INTEGER, CONF_VALUE_16_BIT(11),
1088 &xx_ei.move_delay_random, 0,
1089 &yy_ei.move_delay_random
1093 TYPE_INTEGER, CONF_VALUE_16_BIT(16),
1094 &xx_ei.step_delay_fixed, 0,
1095 &yy_ei.step_delay_fixed
1099 TYPE_INTEGER, CONF_VALUE_16_BIT(17),
1100 &xx_ei.step_delay_random, 0,
1101 &yy_ei.step_delay_random
1106 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
1107 &xx_ei.move_pattern, MV_ALL_DIRECTIONS,
1112 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
1113 &xx_ei.move_direction_initial, MV_START_AUTOMATIC,
1114 &yy_ei.move_direction_initial
1118 TYPE_INTEGER, CONF_VALUE_8_BIT(5),
1119 &xx_ei.move_stepsize, TILEX / 8,
1120 &yy_ei.move_stepsize
1125 TYPE_ELEMENT, CONF_VALUE_16_BIT(12),
1126 &xx_ei.move_enter_element, EL_EMPTY_SPACE,
1127 &yy_ei.move_enter_element
1131 TYPE_ELEMENT, CONF_VALUE_16_BIT(13),
1132 &xx_ei.move_leave_element, EL_EMPTY_SPACE,
1133 &yy_ei.move_leave_element
1137 TYPE_INTEGER, CONF_VALUE_8_BIT(6),
1138 &xx_ei.move_leave_type, LEAVE_TYPE_UNLIMITED,
1139 &yy_ei.move_leave_type
1144 TYPE_INTEGER, CONF_VALUE_8_BIT(7),
1145 &xx_ei.slippery_type, SLIPPERY_ANY_RANDOM,
1146 &yy_ei.slippery_type
1151 TYPE_INTEGER, CONF_VALUE_8_BIT(8),
1152 &xx_ei.explosion_type, EXPLODES_3X3,
1153 &yy_ei.explosion_type
1157 TYPE_INTEGER, CONF_VALUE_16_BIT(14),
1158 &xx_ei.explosion_delay, 16,
1159 &yy_ei.explosion_delay
1163 TYPE_INTEGER, CONF_VALUE_16_BIT(15),
1164 &xx_ei.ignition_delay, 8,
1165 &yy_ei.ignition_delay
1170 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(2),
1171 &xx_ei.content, EL_EMPTY_SPACE,
1173 &xx_num_contents, 1, 1
1176 // ---------- "num_change_pages" must be the last entry ---------------------
1179 -1, SAVE_CONF_ALWAYS,
1180 TYPE_INTEGER, CONF_VALUE_8_BIT(9),
1181 &xx_ei.num_change_pages, 1,
1182 &yy_ei.num_change_pages
1193 static struct LevelFileConfigInfo chunk_config_CUSX_change[] =
1195 // ---------- "current_change_page" must be the first entry -----------------
1198 -1, SAVE_CONF_ALWAYS,
1199 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1200 &xx_current_change_page, -1
1203 // ---------- (the remaining entries can be in any order) -------------------
1207 TYPE_BOOLEAN, CONF_VALUE_8_BIT(2),
1208 &xx_change.can_change, FALSE
1213 TYPE_BITFIELD, CONF_VALUE_32_BIT(1),
1214 &xx_event_bits[0], 0
1218 TYPE_BITFIELD, CONF_VALUE_32_BIT(2),
1219 &xx_event_bits[1], 0
1224 TYPE_BITFIELD, CONF_VALUE_8_BIT(3),
1225 &xx_change.trigger_player, CH_PLAYER_ANY
1229 TYPE_BITFIELD, CONF_VALUE_8_BIT(4),
1230 &xx_change.trigger_side, CH_SIDE_ANY
1234 TYPE_BITFIELD, CONF_VALUE_32_BIT(3),
1235 &xx_change.trigger_page, CH_PAGE_ANY
1240 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1241 &xx_change.target_element, EL_EMPTY_SPACE
1246 TYPE_INTEGER, CONF_VALUE_16_BIT(2),
1247 &xx_change.delay_fixed, 0
1251 TYPE_INTEGER, CONF_VALUE_16_BIT(3),
1252 &xx_change.delay_random, 0
1256 TYPE_INTEGER, CONF_VALUE_16_BIT(4),
1257 &xx_change.delay_frames, FRAMES_PER_SECOND
1262 TYPE_ELEMENT, CONF_VALUE_16_BIT(5),
1263 &xx_change.initial_trigger_element, EL_EMPTY_SPACE
1268 TYPE_BOOLEAN, CONF_VALUE_8_BIT(6),
1269 &xx_change.explode, FALSE
1273 TYPE_BOOLEAN, CONF_VALUE_8_BIT(7),
1274 &xx_change.use_target_content, FALSE
1278 TYPE_BOOLEAN, CONF_VALUE_8_BIT(8),
1279 &xx_change.only_if_complete, FALSE
1283 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1284 &xx_change.use_random_replace, FALSE
1288 TYPE_INTEGER, CONF_VALUE_8_BIT(10),
1289 &xx_change.random_percentage, 100
1293 TYPE_INTEGER, CONF_VALUE_8_BIT(11),
1294 &xx_change.replace_when, CP_WHEN_EMPTY
1299 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1300 &xx_change.has_action, FALSE
1304 TYPE_INTEGER, CONF_VALUE_8_BIT(13),
1305 &xx_change.action_type, CA_NO_ACTION
1309 TYPE_INTEGER, CONF_VALUE_8_BIT(14),
1310 &xx_change.action_mode, CA_MODE_UNDEFINED
1314 TYPE_INTEGER, CONF_VALUE_16_BIT(6),
1315 &xx_change.action_arg, CA_ARG_UNDEFINED
1320 TYPE_ELEMENT, CONF_VALUE_16_BIT(7),
1321 &xx_change.action_element, EL_EMPTY_SPACE
1326 TYPE_CONTENT_LIST, CONF_VALUE_BYTES(1),
1327 &xx_change.target_content, EL_EMPTY_SPACE, NULL,
1328 &xx_num_contents, 1, 1
1338 static struct LevelFileConfigInfo chunk_config_GRPX[] =
1342 TYPE_STRING, CONF_VALUE_BYTES(1),
1343 &xx_ei.description[0], -1, NULL,
1344 &xx_string_length_unused, -1, MAX_ELEMENT_NAME_LEN,
1345 &xx_default_description[0]
1350 TYPE_BOOLEAN, CONF_VALUE_8_BIT(1),
1351 &xx_ei.use_gfx_element, FALSE
1355 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1356 &xx_ei.gfx_element_initial, EL_EMPTY_SPACE
1361 TYPE_INTEGER, CONF_VALUE_8_BIT(2),
1362 &xx_group.choice_mode, ANIM_RANDOM
1367 TYPE_ELEMENT_LIST, CONF_VALUE_BYTES(2),
1368 &xx_group.element[0], EL_EMPTY_SPACE, NULL,
1369 &xx_group.num_elements, 1, MAX_ELEMENTS_IN_GROUP
1379 static struct LevelFileConfigInfo chunk_config_CONF[] = // (OBSOLETE)
1383 TYPE_BOOLEAN, CONF_VALUE_8_BIT(9),
1384 &li.block_snap_field, TRUE
1388 TYPE_BOOLEAN, CONF_VALUE_8_BIT(13),
1389 &li.continuous_snapping, TRUE
1393 TYPE_INTEGER, CONF_VALUE_8_BIT(1),
1394 &li.initial_player_stepsize[0], STEPSIZE_NORMAL
1398 TYPE_BOOLEAN, CONF_VALUE_8_BIT(10),
1399 &li.use_start_element[0], FALSE
1403 TYPE_ELEMENT, CONF_VALUE_16_BIT(1),
1404 &li.start_element[0], EL_PLAYER_1
1408 TYPE_BOOLEAN, CONF_VALUE_8_BIT(11),
1409 &li.use_artwork_element[0], FALSE
1413 TYPE_ELEMENT, CONF_VALUE_16_BIT(2),
1414 &li.artwork_element[0], EL_PLAYER_1
1418 TYPE_BOOLEAN, CONF_VALUE_8_BIT(12),
1419 &li.use_explosion_element[0], FALSE
1423 TYPE_ELEMENT, CONF_VALUE_16_BIT(3),
1424 &li.explosion_element[0], EL_PLAYER_1
1439 filetype_id_list[] =
1441 { LEVEL_FILE_TYPE_RND, "RND" },
1442 { LEVEL_FILE_TYPE_BD, "BD" },
1443 { LEVEL_FILE_TYPE_EM, "EM" },
1444 { LEVEL_FILE_TYPE_SP, "SP" },
1445 { LEVEL_FILE_TYPE_DX, "DX" },
1446 { LEVEL_FILE_TYPE_SB, "SB" },
1447 { LEVEL_FILE_TYPE_DC, "DC" },
1448 { LEVEL_FILE_TYPE_MM, "MM" },
1449 { LEVEL_FILE_TYPE_MM, "DF" },
1454 // ============================================================================
1455 // level file functions
1456 // ============================================================================
1458 static boolean check_special_flags(char *flag)
1460 if (strEqual(options.special_flags, flag) ||
1461 strEqual(leveldir_current->special_flags, flag))
1467 static struct DateInfo getCurrentDate(void)
1469 time_t epoch_seconds = time(NULL);
1470 struct tm *now = localtime(&epoch_seconds);
1471 struct DateInfo date;
1473 date.year = now->tm_year + 1900;
1474 date.month = now->tm_mon + 1;
1475 date.day = now->tm_mday;
1477 date.src = DATE_SRC_CLOCK;
1482 static void resetEventFlags(struct ElementChangeInfo *change)
1486 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1487 change->has_event[i] = FALSE;
1490 static void resetEventBits(void)
1494 for (i = 0; i < NUM_CE_BITFIELDS; i++)
1495 xx_event_bits[i] = 0;
1498 static void setEventFlagsFromEventBits(struct ElementChangeInfo *change)
1502 /* important: only change event flag if corresponding event bit is set
1503 (this is because all xx_event_bits[] values are loaded separately,
1504 and all xx_event_bits[] values are set back to zero before loading
1505 another value xx_event_bits[x] (each value representing 32 flags)) */
1507 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1508 if (xx_event_bits[CH_EVENT_BITFIELD_NR(i)] & CH_EVENT_BIT(i))
1509 change->has_event[i] = TRUE;
1512 static void setEventBitsFromEventFlags(struct ElementChangeInfo *change)
1516 /* in contrast to the above function setEventFlagsFromEventBits(), it
1517 would also be possible to set all bits in xx_event_bits[] to 0 or 1
1518 depending on the corresponding change->has_event[i] values here, as
1519 all xx_event_bits[] values are reset in resetEventBits() before */
1521 for (i = 0; i < NUM_CHANGE_EVENTS; i++)
1522 if (change->has_event[i])
1523 xx_event_bits[CH_EVENT_BITFIELD_NR(i)] |= CH_EVENT_BIT(i);
1526 static char *getDefaultElementDescription(struct ElementInfo *ei)
1528 static char description[MAX_ELEMENT_NAME_LEN + 1];
1529 char *default_description = (ei->custom_description != NULL ?
1530 ei->custom_description :
1531 ei->editor_description);
1534 // always start with reliable default values
1535 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1536 description[i] = '\0';
1538 // truncate element description to MAX_ELEMENT_NAME_LEN bytes
1539 strncpy(description, default_description, MAX_ELEMENT_NAME_LEN);
1541 return &description[0];
1544 static void setElementDescriptionToDefault(struct ElementInfo *ei)
1546 char *default_description = getDefaultElementDescription(ei);
1549 for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
1550 ei->description[i] = default_description[i];
1553 static void setConfigToDefaultsFromConfigList(struct LevelFileConfigInfo *conf)
1557 for (i = 0; conf[i].data_type != -1; i++)
1559 int default_value = conf[i].default_value;
1560 int data_type = conf[i].data_type;
1561 int conf_type = conf[i].conf_type;
1562 int byte_mask = conf_type & CONF_MASK_BYTES;
1564 if (byte_mask == CONF_MASK_MULTI_BYTES)
1566 int default_num_entities = conf[i].default_num_entities;
1567 int max_num_entities = conf[i].max_num_entities;
1569 *(int *)(conf[i].num_entities) = default_num_entities;
1571 if (data_type == TYPE_STRING)
1573 char *default_string = conf[i].default_string;
1574 char *string = (char *)(conf[i].value);
1576 strncpy(string, default_string, max_num_entities);
1578 else if (data_type == TYPE_ELEMENT_LIST)
1580 int *element_array = (int *)(conf[i].value);
1583 for (j = 0; j < max_num_entities; j++)
1584 element_array[j] = default_value;
1586 else if (data_type == TYPE_CONTENT_LIST)
1588 struct Content *content = (struct Content *)(conf[i].value);
1591 for (c = 0; c < max_num_entities; c++)
1592 for (y = 0; y < 3; y++)
1593 for (x = 0; x < 3; x++)
1594 content[c].e[x][y] = default_value;
1597 else // constant size configuration data (1, 2 or 4 bytes)
1599 if (data_type == TYPE_BOOLEAN)
1600 *(boolean *)(conf[i].value) = default_value;
1602 *(int *) (conf[i].value) = default_value;
1607 static void copyConfigFromConfigList(struct LevelFileConfigInfo *conf)
1611 for (i = 0; conf[i].data_type != -1; i++)
1613 int data_type = conf[i].data_type;
1614 int conf_type = conf[i].conf_type;
1615 int byte_mask = conf_type & CONF_MASK_BYTES;
1617 if (byte_mask == CONF_MASK_MULTI_BYTES)
1619 int max_num_entities = conf[i].max_num_entities;
1621 if (data_type == TYPE_STRING)
1623 char *string = (char *)(conf[i].value);
1624 char *string_copy = (char *)(conf[i].value_copy);
1626 strncpy(string_copy, string, max_num_entities);
1628 else if (data_type == TYPE_ELEMENT_LIST)
1630 int *element_array = (int *)(conf[i].value);
1631 int *element_array_copy = (int *)(conf[i].value_copy);
1634 for (j = 0; j < max_num_entities; j++)
1635 element_array_copy[j] = element_array[j];
1637 else if (data_type == TYPE_CONTENT_LIST)
1639 struct Content *content = (struct Content *)(conf[i].value);
1640 struct Content *content_copy = (struct Content *)(conf[i].value_copy);
1643 for (c = 0; c < max_num_entities; c++)
1644 for (y = 0; y < 3; y++)
1645 for (x = 0; x < 3; x++)
1646 content_copy[c].e[x][y] = content[c].e[x][y];
1649 else // constant size configuration data (1, 2 or 4 bytes)
1651 if (data_type == TYPE_BOOLEAN)
1652 *(boolean *)(conf[i].value_copy) = *(boolean *)(conf[i].value);
1654 *(int *) (conf[i].value_copy) = *(int *) (conf[i].value);
1659 void copyElementInfo(struct ElementInfo *ei_from, struct ElementInfo *ei_to)
1663 xx_ei = *ei_from; // copy element data into temporary buffer
1664 yy_ei = *ei_to; // copy element data into temporary buffer
1666 copyConfigFromConfigList(chunk_config_CUSX_base);
1671 // ---------- reinitialize and copy change pages ----------
1673 ei_to->num_change_pages = ei_from->num_change_pages;
1674 ei_to->current_change_page = ei_from->current_change_page;
1676 setElementChangePages(ei_to, ei_to->num_change_pages);
1678 for (i = 0; i < ei_to->num_change_pages; i++)
1679 ei_to->change_page[i] = ei_from->change_page[i];
1681 // ---------- copy group element info ----------
1682 if (ei_from->group != NULL && ei_to->group != NULL) // group or internal
1683 *ei_to->group = *ei_from->group;
1685 // mark this custom element as modified
1686 ei_to->modified_settings = TRUE;
1689 void setElementChangePages(struct ElementInfo *ei, int change_pages)
1691 int change_page_size = sizeof(struct ElementChangeInfo);
1693 ei->num_change_pages = MAX(1, change_pages);
1696 checked_realloc(ei->change_page, ei->num_change_pages * change_page_size);
1698 if (ei->current_change_page >= ei->num_change_pages)
1699 ei->current_change_page = ei->num_change_pages - 1;
1701 ei->change = &ei->change_page[ei->current_change_page];
1704 void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
1706 xx_change = *change; // copy change data into temporary buffer
1708 setConfigToDefaultsFromConfigList(chunk_config_CUSX_change);
1710 *change = xx_change;
1712 resetEventFlags(change);
1714 change->direct_action = 0;
1715 change->other_action = 0;
1717 change->pre_change_function = NULL;
1718 change->change_function = NULL;
1719 change->post_change_function = NULL;
1722 static void setLevelInfoToDefaults_Level(struct LevelInfo *level)
1726 li = *level; // copy level data into temporary buffer
1727 setConfigToDefaultsFromConfigList(chunk_config_INFO);
1728 *level = li; // copy temporary buffer back to level data
1730 setLevelInfoToDefaults_EM();
1731 setLevelInfoToDefaults_SP();
1732 setLevelInfoToDefaults_MM();
1734 level->native_em_level = &native_em_level;
1735 level->native_sp_level = &native_sp_level;
1736 level->native_mm_level = &native_mm_level;
1738 level->file_version = FILE_VERSION_ACTUAL;
1739 level->game_version = GAME_VERSION_ACTUAL;
1741 level->creation_date = getCurrentDate();
1743 level->encoding_16bit_field = TRUE;
1744 level->encoding_16bit_yamyam = TRUE;
1745 level->encoding_16bit_amoeba = TRUE;
1747 // clear level name and level author string buffers
1748 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
1749 level->name[i] = '\0';
1750 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
1751 level->author[i] = '\0';
1753 // set level name and level author to default values
1754 strcpy(level->name, NAMELESS_LEVEL_NAME);
1755 strcpy(level->author, ANONYMOUS_NAME);
1757 // set level playfield to playable default level with player and exit
1758 for (x = 0; x < MAX_LEV_FIELDX; x++)
1759 for (y = 0; y < MAX_LEV_FIELDY; y++)
1760 level->field[x][y] = EL_SAND;
1762 level->field[0][0] = EL_PLAYER_1;
1763 level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
1765 BorderElement = EL_STEELWALL;
1767 // detect custom elements when loading them
1768 level->file_has_custom_elements = FALSE;
1770 // set all bug compatibility flags to "false" => do not emulate this bug
1771 level->use_action_after_change_bug = FALSE;
1773 if (leveldir_current)
1775 // try to determine better author name than 'anonymous'
1776 if (!strEqual(leveldir_current->author, ANONYMOUS_NAME))
1778 strncpy(level->author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
1779 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1783 switch (LEVELCLASS(leveldir_current))
1785 case LEVELCLASS_TUTORIAL:
1786 strcpy(level->author, PROGRAM_AUTHOR_STRING);
1789 case LEVELCLASS_CONTRIB:
1790 strncpy(level->author, leveldir_current->name, MAX_LEVEL_AUTHOR_LEN);
1791 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1794 case LEVELCLASS_PRIVATE:
1795 strncpy(level->author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
1796 level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
1800 // keep default value
1807 static void setLevelInfoToDefaults_Elements(struct LevelInfo *level)
1809 static boolean clipboard_elements_initialized = FALSE;
1812 InitElementPropertiesStatic();
1814 li = *level; // copy level data into temporary buffer
1815 setConfigToDefaultsFromConfigList(chunk_config_ELEM);
1816 *level = li; // copy temporary buffer back to level data
1818 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1821 struct ElementInfo *ei = &element_info[element];
1823 // never initialize clipboard elements after the very first time
1824 // (to be able to use clipboard elements between several levels)
1825 if (IS_CLIPBOARD_ELEMENT(element) && clipboard_elements_initialized)
1828 if (IS_ENVELOPE(element))
1830 int envelope_nr = element - EL_ENVELOPE_1;
1832 setConfigToDefaultsFromConfigList(chunk_config_NOTE);
1834 level->envelope[envelope_nr] = xx_envelope;
1837 if (IS_CUSTOM_ELEMENT(element) ||
1838 IS_GROUP_ELEMENT(element) ||
1839 IS_INTERNAL_ELEMENT(element))
1841 xx_ei = *ei; // copy element data into temporary buffer
1843 setConfigToDefaultsFromConfigList(chunk_config_CUSX_base);
1848 setElementChangePages(ei, 1);
1849 setElementChangeInfoToDefaults(ei->change);
1851 if (IS_CUSTOM_ELEMENT(element) ||
1852 IS_GROUP_ELEMENT(element) ||
1853 IS_INTERNAL_ELEMENT(element))
1855 setElementDescriptionToDefault(ei);
1857 ei->modified_settings = FALSE;
1860 if (IS_CUSTOM_ELEMENT(element) ||
1861 IS_INTERNAL_ELEMENT(element))
1863 // internal values used in level editor
1865 ei->access_type = 0;
1866 ei->access_layer = 0;
1867 ei->access_protected = 0;
1868 ei->walk_to_action = 0;
1869 ei->smash_targets = 0;
1872 ei->can_explode_by_fire = FALSE;
1873 ei->can_explode_smashed = FALSE;
1874 ei->can_explode_impact = FALSE;
1876 ei->current_change_page = 0;
1879 if (IS_GROUP_ELEMENT(element) ||
1880 IS_INTERNAL_ELEMENT(element))
1882 struct ElementGroupInfo *group;
1884 // initialize memory for list of elements in group
1885 if (ei->group == NULL)
1886 ei->group = checked_malloc(sizeof(struct ElementGroupInfo));
1890 xx_group = *group; // copy group data into temporary buffer
1892 setConfigToDefaultsFromConfigList(chunk_config_GRPX);
1898 clipboard_elements_initialized = TRUE;
1901 static void setLevelInfoToDefaults(struct LevelInfo *level,
1902 boolean level_info_only,
1903 boolean reset_file_status)
1905 setLevelInfoToDefaults_Level(level);
1907 if (!level_info_only)
1908 setLevelInfoToDefaults_Elements(level);
1910 if (reset_file_status)
1912 level->no_valid_file = FALSE;
1913 level->no_level_file = FALSE;
1916 level->changed = FALSE;
1919 static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
1921 level_file_info->nr = 0;
1922 level_file_info->type = LEVEL_FILE_TYPE_UNKNOWN;
1923 level_file_info->packed = FALSE;
1925 setString(&level_file_info->basename, NULL);
1926 setString(&level_file_info->filename, NULL);
1929 int getMappedElement_SB(int, boolean);
1931 static void ActivateLevelTemplate(void)
1935 if (check_special_flags("load_xsb_to_ces"))
1937 // fill smaller playfields with padding "beyond border wall" elements
1938 if (level.fieldx < level_template.fieldx ||
1939 level.fieldy < level_template.fieldy)
1941 short field[level.fieldx][level.fieldy];
1942 int new_fieldx = MAX(level.fieldx, level_template.fieldx);
1943 int new_fieldy = MAX(level.fieldy, level_template.fieldy);
1944 int pos_fieldx = (new_fieldx - level.fieldx) / 2;
1945 int pos_fieldy = (new_fieldy - level.fieldy) / 2;
1947 // copy old playfield (which is smaller than the visible area)
1948 for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
1949 field[x][y] = level.field[x][y];
1951 // fill new, larger playfield with "beyond border wall" elements
1952 for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++)
1953 level.field[x][y] = getMappedElement_SB('_', TRUE);
1955 // copy the old playfield to the middle of the new playfield
1956 for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
1957 level.field[pos_fieldx + x][pos_fieldy + y] = field[x][y];
1959 level.fieldx = new_fieldx;
1960 level.fieldy = new_fieldy;
1964 // Currently there is no special action needed to activate the template
1965 // data, because 'element_info' property settings overwrite the original
1966 // level data, while all other variables do not change.
1968 // Exception: 'from_level_template' elements in the original level playfield
1969 // are overwritten with the corresponding elements at the same position in
1970 // playfield from the level template.
1972 for (x = 0; x < level.fieldx; x++)
1973 for (y = 0; y < level.fieldy; y++)
1974 if (level.field[x][y] == EL_FROM_LEVEL_TEMPLATE)
1975 level.field[x][y] = level_template.field[x][y];
1977 if (check_special_flags("load_xsb_to_ces"))
1979 struct LevelInfo level_backup = level;
1981 // overwrite all individual level settings from template level settings
1982 level = level_template;
1984 // restore level file info
1985 level.file_info = level_backup.file_info;
1987 // restore playfield size
1988 level.fieldx = level_backup.fieldx;
1989 level.fieldy = level_backup.fieldy;
1991 // restore playfield content
1992 for (x = 0; x < level.fieldx; x++)
1993 for (y = 0; y < level.fieldy; y++)
1994 level.field[x][y] = level_backup.field[x][y];
1996 // restore name and author from individual level
1997 strcpy(level.name, level_backup.name);
1998 strcpy(level.author, level_backup.author);
2000 // restore flag "use_custom_template"
2001 level.use_custom_template = level_backup.use_custom_template;
2005 static char *getLevelFilenameFromBasename(char *basename)
2007 static char *filename = NULL;
2009 checked_free(filename);
2011 filename = getPath2(getCurrentLevelDir(), basename);
2016 static int getFileTypeFromBasename(char *basename)
2018 // !!! ALSO SEE COMMENT IN checkForPackageFromBasename() !!!
2020 static char *filename = NULL;
2021 struct stat file_status;
2023 // ---------- try to determine file type from filename ----------
2025 // check for typical filename of a Supaplex level package file
2026 if (strlen(basename) == 10 && strPrefixLower(basename, "levels.d"))
2027 return LEVEL_FILE_TYPE_SP;
2029 // check for typical filename of a Diamond Caves II level package file
2030 if (strSuffixLower(basename, ".dc") ||
2031 strSuffixLower(basename, ".dc2"))
2032 return LEVEL_FILE_TYPE_DC;
2034 // check for typical filename of a Sokoban level package file
2035 if (strSuffixLower(basename, ".xsb") &&
2036 strchr(basename, '%') == NULL)
2037 return LEVEL_FILE_TYPE_SB;
2039 // ---------- try to determine file type from filesize ----------
2041 checked_free(filename);
2042 filename = getPath2(getCurrentLevelDir(), basename);
2044 if (stat(filename, &file_status) == 0)
2046 // check for typical filesize of a Supaplex level package file
2047 if (file_status.st_size == 170496)
2048 return LEVEL_FILE_TYPE_SP;
2051 return LEVEL_FILE_TYPE_UNKNOWN;
2054 static int getFileTypeFromMagicBytes(char *filename, int type)
2058 if ((file = openFile(filename, MODE_READ)))
2060 char chunk_name[CHUNK_ID_LEN + 1];
2062 getFileChunkBE(file, chunk_name, NULL);
2064 if (strEqual(chunk_name, "MMII") ||
2065 strEqual(chunk_name, "MIRR"))
2066 type = LEVEL_FILE_TYPE_MM;
2074 static boolean checkForPackageFromBasename(char *basename)
2076 // !!! WON'T WORK ANYMORE IF getFileTypeFromBasename() ALSO DETECTS !!!
2077 // !!! SINGLE LEVELS (CURRENTLY ONLY DETECTS LEVEL PACKAGES !!!
2079 return (getFileTypeFromBasename(basename) != LEVEL_FILE_TYPE_UNKNOWN);
2082 static char *getSingleLevelBasenameExt(int nr, char *extension)
2084 static char basename[MAX_FILENAME_LEN];
2087 sprintf(basename, "%s", LEVELTEMPLATE_FILENAME);
2089 sprintf(basename, "%03d.%s", nr, extension);
2094 static char *getSingleLevelBasename(int nr)
2096 return getSingleLevelBasenameExt(nr, LEVELFILE_EXTENSION);
2099 static char *getPackedLevelBasename(int type)
2101 static char basename[MAX_FILENAME_LEN];
2102 char *directory = getCurrentLevelDir();
2104 DirectoryEntry *dir_entry;
2106 strcpy(basename, UNDEFINED_FILENAME); // default: undefined file
2108 if ((dir = openDirectory(directory)) == NULL)
2110 Warn("cannot read current level directory '%s'", directory);
2115 while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries
2117 char *entry_basename = dir_entry->basename;
2118 int entry_type = getFileTypeFromBasename(entry_basename);
2120 if (entry_type != LEVEL_FILE_TYPE_UNKNOWN) // found valid level package
2122 if (type == LEVEL_FILE_TYPE_UNKNOWN ||
2125 strcpy(basename, entry_basename);
2132 closeDirectory(dir);
2137 static char *getSingleLevelFilename(int nr)
2139 return getLevelFilenameFromBasename(getSingleLevelBasename(nr));
2142 #if ENABLE_UNUSED_CODE
2143 static char *getPackedLevelFilename(int type)
2145 return getLevelFilenameFromBasename(getPackedLevelBasename(type));
2149 char *getDefaultLevelFilename(int nr)
2151 return getSingleLevelFilename(nr);
2154 #if ENABLE_UNUSED_CODE
2155 static void setLevelFileInfo_SingleLevelFilename(struct LevelFileInfo *lfi,
2159 lfi->packed = FALSE;
2161 setString(&lfi->basename, getSingleLevelBasename(lfi->nr, lfi->type));
2162 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2166 static void setLevelFileInfo_FormatLevelFilename(struct LevelFileInfo *lfi,
2167 int type, char *format, ...)
2169 static char basename[MAX_FILENAME_LEN];
2172 va_start(ap, format);
2173 vsprintf(basename, format, ap);
2177 lfi->packed = FALSE;
2179 setString(&lfi->basename, basename);
2180 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2183 static void setLevelFileInfo_PackedLevelFilename(struct LevelFileInfo *lfi,
2189 setString(&lfi->basename, getPackedLevelBasename(lfi->type));
2190 setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
2193 static int getFiletypeFromID(char *filetype_id)
2195 char *filetype_id_lower;
2196 int filetype = LEVEL_FILE_TYPE_UNKNOWN;
2199 if (filetype_id == NULL)
2200 return LEVEL_FILE_TYPE_UNKNOWN;
2202 filetype_id_lower = getStringToLower(filetype_id);
2204 for (i = 0; filetype_id_list[i].id != NULL; i++)
2206 char *id_lower = getStringToLower(filetype_id_list[i].id);
2208 if (strEqual(filetype_id_lower, id_lower))
2209 filetype = filetype_id_list[i].filetype;
2213 if (filetype != LEVEL_FILE_TYPE_UNKNOWN)
2217 free(filetype_id_lower);
2222 char *getLocalLevelTemplateFilename(void)
2224 return getDefaultLevelFilename(-1);
2227 char *getGlobalLevelTemplateFilename(void)
2229 // global variable "leveldir_current" must be modified in the loop below
2230 LevelDirTree *leveldir_current_last = leveldir_current;
2231 char *filename = NULL;
2233 // check for template level in path from current to topmost tree node
2235 while (leveldir_current != NULL)
2237 filename = getDefaultLevelFilename(-1);
2239 if (fileExists(filename))
2242 leveldir_current = leveldir_current->node_parent;
2245 // restore global variable "leveldir_current" modified in above loop
2246 leveldir_current = leveldir_current_last;
2251 static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
2255 // special case: level number is negative => check for level template file
2258 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2259 getSingleLevelBasename(-1));
2261 // replace local level template filename with global template filename
2262 setString(&lfi->filename, getGlobalLevelTemplateFilename());
2264 // no fallback if template file not existing
2268 // special case: check for file name/pattern specified in "levelinfo.conf"
2269 if (leveldir_current->level_filename != NULL)
2271 int filetype = getFiletypeFromID(leveldir_current->level_filetype);
2273 setLevelFileInfo_FormatLevelFilename(lfi, filetype,
2274 leveldir_current->level_filename, nr);
2276 lfi->packed = checkForPackageFromBasename(leveldir_current->level_filename);
2278 if (fileExists(lfi->filename))
2281 else if (leveldir_current->level_filetype != NULL)
2283 int filetype = getFiletypeFromID(leveldir_current->level_filetype);
2285 // check for specified native level file with standard file name
2286 setLevelFileInfo_FormatLevelFilename(lfi, filetype,
2287 "%03d.%s", nr, LEVELFILE_EXTENSION);
2288 if (fileExists(lfi->filename))
2292 // check for native Rocks'n'Diamonds level file
2293 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2294 "%03d.%s", nr, LEVELFILE_EXTENSION);
2295 if (fileExists(lfi->filename))
2298 // check for Emerald Mine level file (V1)
2299 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "a%c%c",
2300 'a' + (nr / 10) % 26, '0' + nr % 10);
2301 if (fileExists(lfi->filename))
2303 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "A%c%c",
2304 'A' + (nr / 10) % 26, '0' + nr % 10);
2305 if (fileExists(lfi->filename))
2308 // check for Emerald Mine level file (V2 to V5)
2309 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%d", nr);
2310 if (fileExists(lfi->filename))
2313 // check for Emerald Mine level file (V6 / single mode)
2314 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02ds", nr);
2315 if (fileExists(lfi->filename))
2317 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dS", nr);
2318 if (fileExists(lfi->filename))
2321 // check for Emerald Mine level file (V6 / teamwork mode)
2322 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dt", nr);
2323 if (fileExists(lfi->filename))
2325 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_EM, "%02dT", nr);
2326 if (fileExists(lfi->filename))
2329 // check for various packed level file formats
2330 setLevelFileInfo_PackedLevelFilename(lfi, LEVEL_FILE_TYPE_UNKNOWN);
2331 if (fileExists(lfi->filename))
2334 // no known level file found -- use default values (and fail later)
2335 setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
2336 "%03d.%s", nr, LEVELFILE_EXTENSION);
2339 static void determineLevelFileInfo_Filetype(struct LevelFileInfo *lfi)
2341 if (lfi->type == LEVEL_FILE_TYPE_UNKNOWN)
2342 lfi->type = getFileTypeFromBasename(lfi->basename);
2344 if (lfi->type == LEVEL_FILE_TYPE_RND)
2345 lfi->type = getFileTypeFromMagicBytes(lfi->filename, lfi->type);
2348 static void setLevelFileInfo(struct LevelFileInfo *level_file_info, int nr)
2350 // always start with reliable default values
2351 setFileInfoToDefaults(level_file_info);
2353 level_file_info->nr = nr; // set requested level number
2355 determineLevelFileInfo_Filename(level_file_info);
2356 determineLevelFileInfo_Filetype(level_file_info);
2359 static void copyLevelFileInfo(struct LevelFileInfo *lfi_from,
2360 struct LevelFileInfo *lfi_to)
2362 lfi_to->nr = lfi_from->nr;
2363 lfi_to->type = lfi_from->type;
2364 lfi_to->packed = lfi_from->packed;
2366 setString(&lfi_to->basename, lfi_from->basename);
2367 setString(&lfi_to->filename, lfi_from->filename);
2370 // ----------------------------------------------------------------------------
2371 // functions for loading R'n'D level
2372 // ----------------------------------------------------------------------------
2374 int getMappedElement(int element)
2376 // remap some (historic, now obsolete) elements
2380 case EL_PLAYER_OBSOLETE:
2381 element = EL_PLAYER_1;
2384 case EL_KEY_OBSOLETE:
2388 case EL_EM_KEY_1_FILE_OBSOLETE:
2389 element = EL_EM_KEY_1;
2392 case EL_EM_KEY_2_FILE_OBSOLETE:
2393 element = EL_EM_KEY_2;
2396 case EL_EM_KEY_3_FILE_OBSOLETE:
2397 element = EL_EM_KEY_3;
2400 case EL_EM_KEY_4_FILE_OBSOLETE:
2401 element = EL_EM_KEY_4;
2404 case EL_ENVELOPE_OBSOLETE:
2405 element = EL_ENVELOPE_1;
2413 if (element >= NUM_FILE_ELEMENTS)
2415 Warn("invalid level element %d", element);
2417 element = EL_UNKNOWN;
2425 static int getMappedElementByVersion(int element, int game_version)
2427 // remap some elements due to certain game version
2429 if (game_version <= VERSION_IDENT(2,2,0,0))
2431 // map game font elements
2432 element = (element == EL_CHAR('[') ? EL_CHAR_AUMLAUT :
2433 element == EL_CHAR('\\') ? EL_CHAR_OUMLAUT :
2434 element == EL_CHAR(']') ? EL_CHAR_UUMLAUT :
2435 element == EL_CHAR('^') ? EL_CHAR_COPYRIGHT : element);
2438 if (game_version < VERSION_IDENT(3,0,0,0))
2440 // map Supaplex gravity tube elements
2441 element = (element == EL_SP_GRAVITY_PORT_LEFT ? EL_SP_PORT_LEFT :
2442 element == EL_SP_GRAVITY_PORT_RIGHT ? EL_SP_PORT_RIGHT :
2443 element == EL_SP_GRAVITY_PORT_UP ? EL_SP_PORT_UP :
2444 element == EL_SP_GRAVITY_PORT_DOWN ? EL_SP_PORT_DOWN :
2451 static int LoadLevel_VERS(File *file, int chunk_size, struct LevelInfo *level)
2453 level->file_version = getFileVersion(file);
2454 level->game_version = getFileVersion(file);
2459 static int LoadLevel_DATE(File *file, int chunk_size, struct LevelInfo *level)
2461 level->creation_date.year = getFile16BitBE(file);
2462 level->creation_date.month = getFile8Bit(file);
2463 level->creation_date.day = getFile8Bit(file);
2465 level->creation_date.src = DATE_SRC_LEVELFILE;
2470 static int LoadLevel_HEAD(File *file, int chunk_size, struct LevelInfo *level)
2472 int initial_player_stepsize;
2473 int initial_player_gravity;
2476 level->fieldx = getFile8Bit(file);
2477 level->fieldy = getFile8Bit(file);
2479 level->time = getFile16BitBE(file);
2480 level->gems_needed = getFile16BitBE(file);
2482 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2483 level->name[i] = getFile8Bit(file);
2484 level->name[MAX_LEVEL_NAME_LEN] = 0;
2486 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
2487 level->score[i] = getFile8Bit(file);
2489 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2490 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
2491 for (y = 0; y < 3; y++)
2492 for (x = 0; x < 3; x++)
2493 level->yamyam_content[i].e[x][y] = getMappedElement(getFile8Bit(file));
2495 level->amoeba_speed = getFile8Bit(file);
2496 level->time_magic_wall = getFile8Bit(file);
2497 level->time_wheel = getFile8Bit(file);
2498 level->amoeba_content = getMappedElement(getFile8Bit(file));
2500 initial_player_stepsize = (getFile8Bit(file) == 1 ? STEPSIZE_FAST :
2503 for (i = 0; i < MAX_PLAYERS; i++)
2504 level->initial_player_stepsize[i] = initial_player_stepsize;
2506 initial_player_gravity = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2508 for (i = 0; i < MAX_PLAYERS; i++)
2509 level->initial_player_gravity[i] = initial_player_gravity;
2511 level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2512 level->em_slippery_gems = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2514 level->use_custom_template = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2516 level->block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2517 level->sp_block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2518 level->can_move_into_acid_bits = getFile32BitBE(file);
2519 level->dont_collide_with_bits = getFile8Bit(file);
2521 level->use_spring_bug = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2522 level->use_step_counter = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2524 level->instant_relocation = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2525 level->can_pass_to_walkable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2526 level->grow_into_diggable = (getFile8Bit(file) == 1 ? TRUE : FALSE);
2528 level->game_engine_type = getFile8Bit(file);
2530 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_HEAD_UNUSED);
2535 static int LoadLevel_NAME(File *file, int chunk_size, struct LevelInfo *level)
2539 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
2540 level->name[i] = getFile8Bit(file);
2541 level->name[MAX_LEVEL_NAME_LEN] = 0;
2546 static int LoadLevel_AUTH(File *file, int chunk_size, struct LevelInfo *level)
2550 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
2551 level->author[i] = getFile8Bit(file);
2552 level->author[MAX_LEVEL_AUTHOR_LEN] = 0;
2557 static int LoadLevel_BODY(File *file, int chunk_size, struct LevelInfo *level)
2560 int chunk_size_expected = level->fieldx * level->fieldy;
2562 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2563 stored with 16-bit encoding (and should be twice as big then).
2564 Even worse, playfield data was stored 16-bit when only yamyam content
2565 contained 16-bit elements and vice versa. */
2567 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2568 chunk_size_expected *= 2;
2570 if (chunk_size_expected != chunk_size)
2572 ReadUnusedBytesFromFile(file, chunk_size);
2573 return chunk_size_expected;
2576 for (y = 0; y < level->fieldy; y++)
2577 for (x = 0; x < level->fieldx; x++)
2578 level->field[x][y] =
2579 getMappedElement(level->encoding_16bit_field ? getFile16BitBE(file) :
2584 static int LoadLevel_CONT(File *file, int chunk_size, struct LevelInfo *level)
2587 int header_size = 4;
2588 int content_size = MAX_ELEMENT_CONTENTS * 3 * 3;
2589 int chunk_size_expected = header_size + content_size;
2591 /* Note: "chunk_size" was wrong before version 2.0 when elements are
2592 stored with 16-bit encoding (and should be twice as big then).
2593 Even worse, playfield data was stored 16-bit when only yamyam content
2594 contained 16-bit elements and vice versa. */
2596 if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
2597 chunk_size_expected += content_size;
2599 if (chunk_size_expected != chunk_size)
2601 ReadUnusedBytesFromFile(file, chunk_size);
2602 return chunk_size_expected;
2606 level->num_yamyam_contents = getFile8Bit(file);
2610 // correct invalid number of content fields -- should never happen
2611 if (level->num_yamyam_contents < 1 ||
2612 level->num_yamyam_contents > MAX_ELEMENT_CONTENTS)
2613 level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
2615 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2616 for (y = 0; y < 3; y++)
2617 for (x = 0; x < 3; x++)
2618 level->yamyam_content[i].e[x][y] =
2619 getMappedElement(level->encoding_16bit_field ?
2620 getFile16BitBE(file) : getFile8Bit(file));
2624 static int LoadLevel_CNT2(File *file, int chunk_size, struct LevelInfo *level)
2629 int content_array[MAX_ELEMENT_CONTENTS][3][3];
2631 element = getMappedElement(getFile16BitBE(file));
2632 num_contents = getFile8Bit(file);
2634 getFile8Bit(file); // content x size (unused)
2635 getFile8Bit(file); // content y size (unused)
2637 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT2_UNUSED);
2639 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
2640 for (y = 0; y < 3; y++)
2641 for (x = 0; x < 3; x++)
2642 content_array[i][x][y] = getMappedElement(getFile16BitBE(file));
2644 // correct invalid number of content fields -- should never happen
2645 if (num_contents < 1 || num_contents > MAX_ELEMENT_CONTENTS)
2646 num_contents = STD_ELEMENT_CONTENTS;
2648 if (element == EL_YAMYAM)
2650 level->num_yamyam_contents = num_contents;
2652 for (i = 0; i < num_contents; i++)
2653 for (y = 0; y < 3; y++)
2654 for (x = 0; x < 3; x++)
2655 level->yamyam_content[i].e[x][y] = content_array[i][x][y];
2657 else if (element == EL_BD_AMOEBA)
2659 level->amoeba_content = content_array[0][0][0];
2663 Warn("cannot load content for element '%d'", element);
2669 static int LoadLevel_CNT3(File *file, int chunk_size, struct LevelInfo *level)
2675 int chunk_size_expected;
2677 element = getMappedElement(getFile16BitBE(file));
2678 if (!IS_ENVELOPE(element))
2679 element = EL_ENVELOPE_1;
2681 envelope_nr = element - EL_ENVELOPE_1;
2683 envelope_len = getFile16BitBE(file);
2685 level->envelope[envelope_nr].xsize = getFile8Bit(file);
2686 level->envelope[envelope_nr].ysize = getFile8Bit(file);
2688 ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT3_UNUSED);
2690 chunk_size_expected = LEVEL_CHUNK_CNT3_SIZE(envelope_len);
2691 if (chunk_size_expected != chunk_size)
2693 ReadUnusedBytesFromFile(file, chunk_size - LEVEL_CHUNK_CNT3_HEADER);
2694 return chunk_size_expected;
2697 for (i = 0; i < envelope_len; i++)
2698 level->envelope[envelope_nr].text[i] = getFile8Bit(file);
2703 static int LoadLevel_CUS1(File *file, int chunk_size, struct LevelInfo *level)
2705 int num_changed_custom_elements = getFile16BitBE(file);
2706 int chunk_size_expected = 2 + num_changed_custom_elements * 6;
2709 if (chunk_size_expected != chunk_size)
2711 ReadUnusedBytesFromFile(file, chunk_size - 2);
2712 return chunk_size_expected;
2715 for (i = 0; i < num_changed_custom_elements; i++)
2717 int element = getMappedElement(getFile16BitBE(file));
2718 int properties = getFile32BitBE(file);
2720 if (IS_CUSTOM_ELEMENT(element))
2721 element_info[element].properties[EP_BITFIELD_BASE_NR] = properties;
2723 Warn("invalid custom element number %d", element);
2725 // older game versions that wrote level files with CUS1 chunks used
2726 // different default push delay values (not yet stored in level file)
2727 element_info[element].push_delay_fixed = 2;
2728 element_info[element].push_delay_random = 8;
2731 level->file_has_custom_elements = TRUE;
2736 static int LoadLevel_CUS2(File *file, int chunk_size, struct LevelInfo *level)
2738 int num_changed_custom_elements = getFile16BitBE(file);
2739 int chunk_size_expected = 2 + num_changed_custom_elements * 4;
2742 if (chunk_size_expected != chunk_size)
2744 ReadUnusedBytesFromFile(file, chunk_size - 2);
2745 return chunk_size_expected;
2748 for (i = 0; i < num_changed_custom_elements; i++)
2750 int element = getMappedElement(getFile16BitBE(file));
2751 int custom_target_element = getMappedElement(getFile16BitBE(file));
2753 if (IS_CUSTOM_ELEMENT(element))
2754 element_info[element].change->target_element = custom_target_element;
2756 Warn("invalid custom element number %d", element);
2759 level->file_has_custom_elements = TRUE;
2764 static int LoadLevel_CUS3(File *file, int chunk_size, struct LevelInfo *level)
2766 int num_changed_custom_elements = getFile16BitBE(file);
2767 int chunk_size_expected = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
2770 if (chunk_size_expected != chunk_size)
2772 ReadUnusedBytesFromFile(file, chunk_size - 2);
2773 return chunk_size_expected;
2776 for (i = 0; i < num_changed_custom_elements; i++)
2778 int element = getMappedElement(getFile16BitBE(file));
2779 struct ElementInfo *ei = &element_info[element];
2780 unsigned int event_bits;
2782 if (!IS_CUSTOM_ELEMENT(element))
2784 Warn("invalid custom element number %d", element);
2786 element = EL_INTERNAL_DUMMY;
2789 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
2790 ei->description[j] = getFile8Bit(file);
2791 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2793 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2795 // some free bytes for future properties and padding
2796 ReadUnusedBytesFromFile(file, 7);
2798 ei->use_gfx_element = getFile8Bit(file);
2799 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2801 ei->collect_score_initial = getFile8Bit(file);
2802 ei->collect_count_initial = getFile8Bit(file);
2804 ei->push_delay_fixed = getFile16BitBE(file);
2805 ei->push_delay_random = getFile16BitBE(file);
2806 ei->move_delay_fixed = getFile16BitBE(file);
2807 ei->move_delay_random = getFile16BitBE(file);
2809 ei->move_pattern = getFile16BitBE(file);
2810 ei->move_direction_initial = getFile8Bit(file);
2811 ei->move_stepsize = getFile8Bit(file);
2813 for (y = 0; y < 3; y++)
2814 for (x = 0; x < 3; x++)
2815 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2817 event_bits = getFile32BitBE(file);
2818 for (j = 0; j < NUM_CHANGE_EVENTS; j++)
2819 if (event_bits & (1 << j))
2820 ei->change->has_event[j] = TRUE;
2822 ei->change->target_element = getMappedElement(getFile16BitBE(file));
2824 ei->change->delay_fixed = getFile16BitBE(file);
2825 ei->change->delay_random = getFile16BitBE(file);
2826 ei->change->delay_frames = getFile16BitBE(file);
2828 ei->change->initial_trigger_element= getMappedElement(getFile16BitBE(file));
2830 ei->change->explode = getFile8Bit(file);
2831 ei->change->use_target_content = getFile8Bit(file);
2832 ei->change->only_if_complete = getFile8Bit(file);
2833 ei->change->use_random_replace = getFile8Bit(file);
2835 ei->change->random_percentage = getFile8Bit(file);
2836 ei->change->replace_when = getFile8Bit(file);
2838 for (y = 0; y < 3; y++)
2839 for (x = 0; x < 3; x++)
2840 ei->change->target_content.e[x][y] =
2841 getMappedElement(getFile16BitBE(file));
2843 ei->slippery_type = getFile8Bit(file);
2845 // some free bytes for future properties and padding
2846 ReadUnusedBytesFromFile(file, LEVEL_CPART_CUS3_UNUSED);
2848 // mark that this custom element has been modified
2849 ei->modified_settings = TRUE;
2852 level->file_has_custom_elements = TRUE;
2857 static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level)
2859 struct ElementInfo *ei;
2860 int chunk_size_expected;
2864 // ---------- custom element base property values (96 bytes) ----------------
2866 element = getMappedElement(getFile16BitBE(file));
2868 if (!IS_CUSTOM_ELEMENT(element))
2870 Warn("invalid custom element number %d", element);
2872 ReadUnusedBytesFromFile(file, chunk_size - 2);
2877 ei = &element_info[element];
2879 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
2880 ei->description[i] = getFile8Bit(file);
2881 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
2883 ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file);
2885 ReadUnusedBytesFromFile(file, 4); // reserved for more base properties
2887 ei->num_change_pages = getFile8Bit(file);
2889 chunk_size_expected = LEVEL_CHUNK_CUS4_SIZE(ei->num_change_pages);
2890 if (chunk_size_expected != chunk_size)
2892 ReadUnusedBytesFromFile(file, chunk_size - 43);
2893 return chunk_size_expected;
2896 ei->ce_value_fixed_initial = getFile16BitBE(file);
2897 ei->ce_value_random_initial = getFile16BitBE(file);
2898 ei->use_last_ce_value = getFile8Bit(file);
2900 ei->use_gfx_element = getFile8Bit(file);
2901 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
2903 ei->collect_score_initial = getFile8Bit(file);
2904 ei->collect_count_initial = getFile8Bit(file);
2906 ei->drop_delay_fixed = getFile8Bit(file);
2907 ei->push_delay_fixed = getFile8Bit(file);
2908 ei->drop_delay_random = getFile8Bit(file);
2909 ei->push_delay_random = getFile8Bit(file);
2910 ei->move_delay_fixed = getFile16BitBE(file);
2911 ei->move_delay_random = getFile16BitBE(file);
2913 // bits 0 - 15 of "move_pattern" ...
2914 ei->move_pattern = getFile16BitBE(file);
2915 ei->move_direction_initial = getFile8Bit(file);
2916 ei->move_stepsize = getFile8Bit(file);
2918 ei->slippery_type = getFile8Bit(file);
2920 for (y = 0; y < 3; y++)
2921 for (x = 0; x < 3; x++)
2922 ei->content.e[x][y] = getMappedElement(getFile16BitBE(file));
2924 ei->move_enter_element = getMappedElement(getFile16BitBE(file));
2925 ei->move_leave_element = getMappedElement(getFile16BitBE(file));
2926 ei->move_leave_type = getFile8Bit(file);
2928 // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
2929 ei->move_pattern |= (getFile16BitBE(file) << 16);
2931 ei->access_direction = getFile8Bit(file);
2933 ei->explosion_delay = getFile8Bit(file);
2934 ei->ignition_delay = getFile8Bit(file);
2935 ei->explosion_type = getFile8Bit(file);
2937 // some free bytes for future custom property values and padding
2938 ReadUnusedBytesFromFile(file, 1);
2940 // ---------- change page property values (48 bytes) ------------------------
2942 setElementChangePages(ei, ei->num_change_pages);
2944 for (i = 0; i < ei->num_change_pages; i++)
2946 struct ElementChangeInfo *change = &ei->change_page[i];
2947 unsigned int event_bits;
2949 // always start with reliable default values
2950 setElementChangeInfoToDefaults(change);
2952 // bits 0 - 31 of "has_event[]" ...
2953 event_bits = getFile32BitBE(file);
2954 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
2955 if (event_bits & (1 << j))
2956 change->has_event[j] = TRUE;
2958 change->target_element = getMappedElement(getFile16BitBE(file));
2960 change->delay_fixed = getFile16BitBE(file);
2961 change->delay_random = getFile16BitBE(file);
2962 change->delay_frames = getFile16BitBE(file);
2964 change->initial_trigger_element = getMappedElement(getFile16BitBE(file));
2966 change->explode = getFile8Bit(file);
2967 change->use_target_content = getFile8Bit(file);
2968 change->only_if_complete = getFile8Bit(file);
2969 change->use_random_replace = getFile8Bit(file);
2971 change->random_percentage = getFile8Bit(file);
2972 change->replace_when = getFile8Bit(file);
2974 for (y = 0; y < 3; y++)
2975 for (x = 0; x < 3; x++)
2976 change->target_content.e[x][y]= getMappedElement(getFile16BitBE(file));
2978 change->can_change = getFile8Bit(file);
2980 change->trigger_side = getFile8Bit(file);
2982 change->trigger_player = getFile8Bit(file);
2983 change->trigger_page = getFile8Bit(file);
2985 change->trigger_page = (change->trigger_page == CH_PAGE_ANY_FILE ?
2986 CH_PAGE_ANY : (1 << change->trigger_page));
2988 change->has_action = getFile8Bit(file);
2989 change->action_type = getFile8Bit(file);
2990 change->action_mode = getFile8Bit(file);
2991 change->action_arg = getFile16BitBE(file);
2993 // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
2994 event_bits = getFile8Bit(file);
2995 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
2996 if (event_bits & (1 << (j - 32)))
2997 change->has_event[j] = TRUE;
3000 // mark this custom element as modified
3001 ei->modified_settings = TRUE;
3003 level->file_has_custom_elements = TRUE;
3008 static int LoadLevel_GRP1(File *file, int chunk_size, struct LevelInfo *level)
3010 struct ElementInfo *ei;
3011 struct ElementGroupInfo *group;
3015 element = getMappedElement(getFile16BitBE(file));
3017 if (!IS_GROUP_ELEMENT(element))
3019 Warn("invalid group element number %d", element);
3021 ReadUnusedBytesFromFile(file, chunk_size - 2);
3026 ei = &element_info[element];
3028 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
3029 ei->description[i] = getFile8Bit(file);
3030 ei->description[MAX_ELEMENT_NAME_LEN] = 0;
3032 group = element_info[element].group;
3034 group->num_elements = getFile8Bit(file);
3036 ei->use_gfx_element = getFile8Bit(file);
3037 ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
3039 group->choice_mode = getFile8Bit(file);
3041 // some free bytes for future values and padding
3042 ReadUnusedBytesFromFile(file, 3);
3044 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
3045 group->element[i] = getMappedElement(getFile16BitBE(file));
3047 // mark this group element as modified
3048 element_info[element].modified_settings = TRUE;
3050 level->file_has_custom_elements = TRUE;
3055 static int LoadLevel_MicroChunk(File *file, struct LevelFileConfigInfo *conf,
3056 int element, int real_element)
3058 int micro_chunk_size = 0;
3059 int conf_type = getFile8Bit(file);
3060 int byte_mask = conf_type & CONF_MASK_BYTES;
3061 boolean element_found = FALSE;
3064 micro_chunk_size += 1;
3066 if (byte_mask == CONF_MASK_MULTI_BYTES)
3068 int num_bytes = getFile16BitBE(file);
3069 byte *buffer = checked_malloc(num_bytes);
3071 ReadBytesFromFile(file, buffer, num_bytes);
3073 for (i = 0; conf[i].data_type != -1; i++)
3075 if (conf[i].element == element &&
3076 conf[i].conf_type == conf_type)
3078 int data_type = conf[i].data_type;
3079 int num_entities = num_bytes / CONF_ENTITY_NUM_BYTES(data_type);
3080 int max_num_entities = conf[i].max_num_entities;
3082 if (num_entities > max_num_entities)
3084 Warn("truncating number of entities for element %d from %d to %d",
3085 element, num_entities, max_num_entities);
3087 num_entities = max_num_entities;
3090 if (num_entities == 0 && (data_type == TYPE_ELEMENT_LIST ||
3091 data_type == TYPE_CONTENT_LIST))
3093 // for element and content lists, zero entities are not allowed
3094 Warn("found empty list of entities for element %d", element);
3096 // do not set "num_entities" here to prevent reading behind buffer
3098 *(int *)(conf[i].num_entities) = 1; // at least one is required
3102 *(int *)(conf[i].num_entities) = num_entities;
3105 element_found = TRUE;
3107 if (data_type == TYPE_STRING)
3109 char *string = (char *)(conf[i].value);
3112 for (j = 0; j < max_num_entities; j++)
3113 string[j] = (j < num_entities ? buffer[j] : '\0');
3115 else if (data_type == TYPE_ELEMENT_LIST)
3117 int *element_array = (int *)(conf[i].value);
3120 for (j = 0; j < num_entities; j++)
3122 getMappedElement(CONF_ELEMENTS_ELEMENT(buffer, j));
3124 else if (data_type == TYPE_CONTENT_LIST)
3126 struct Content *content= (struct Content *)(conf[i].value);
3129 for (c = 0; c < num_entities; c++)
3130 for (y = 0; y < 3; y++)
3131 for (x = 0; x < 3; x++)
3132 content[c].e[x][y] =
3133 getMappedElement(CONF_CONTENTS_ELEMENT(buffer, c, x, y));
3136 element_found = FALSE;
3142 checked_free(buffer);
3144 micro_chunk_size += 2 + num_bytes;
3146 else // constant size configuration data (1, 2 or 4 bytes)
3148 int value = (byte_mask == CONF_MASK_1_BYTE ? getFile8Bit (file) :
3149 byte_mask == CONF_MASK_2_BYTE ? getFile16BitBE(file) :
3150 byte_mask == CONF_MASK_4_BYTE ? getFile32BitBE(file) : 0);
3152 for (i = 0; conf[i].data_type != -1; i++)
3154 if (conf[i].element == element &&
3155 conf[i].conf_type == conf_type)
3157 int data_type = conf[i].data_type;
3159 if (data_type == TYPE_ELEMENT)
3160 value = getMappedElement(value);
3162 if (data_type == TYPE_BOOLEAN)
3163 *(boolean *)(conf[i].value) = value;
3165 *(int *) (conf[i].value) = value;
3167 element_found = TRUE;
3173 micro_chunk_size += CONF_VALUE_NUM_BYTES(byte_mask);
3178 char *error_conf_chunk_bytes =
3179 (byte_mask == CONF_MASK_1_BYTE ? "CONF_VALUE_8_BIT" :
3180 byte_mask == CONF_MASK_2_BYTE ? "CONF_VALUE_16_BIT" :
3181 byte_mask == CONF_MASK_4_BYTE ? "CONF_VALUE_32_BIT" :"CONF_VALUE_BYTES");
3182 int error_conf_chunk_token = conf_type & CONF_MASK_TOKEN;
3183 int error_element = real_element;
3185 Warn("cannot load micro chunk '%s(%d)' value for element %d ['%s']",
3186 error_conf_chunk_bytes, error_conf_chunk_token,
3187 error_element, EL_NAME(error_element));
3190 return micro_chunk_size;
3193 static int LoadLevel_INFO(File *file, int chunk_size, struct LevelInfo *level)
3195 int real_chunk_size = 0;
3197 li = *level; // copy level data into temporary buffer
3199 while (!checkEndOfFile(file))
3201 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1);
3203 if (real_chunk_size >= chunk_size)
3207 *level = li; // copy temporary buffer back to level data
3209 return real_chunk_size;
3212 static int LoadLevel_CONF(File *file, int chunk_size, struct LevelInfo *level)
3214 int real_chunk_size = 0;
3216 li = *level; // copy level data into temporary buffer
3218 while (!checkEndOfFile(file))
3220 int element = getMappedElement(getFile16BitBE(file));
3222 real_chunk_size += 2;
3223 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CONF,
3225 if (real_chunk_size >= chunk_size)
3229 *level = li; // copy temporary buffer back to level data
3231 return real_chunk_size;
3234 static int LoadLevel_ELEM(File *file, int chunk_size, struct LevelInfo *level)
3236 int real_chunk_size = 0;
3238 li = *level; // copy level data into temporary buffer
3240 while (!checkEndOfFile(file))
3242 int element = getMappedElement(getFile16BitBE(file));
3244 real_chunk_size += 2;
3245 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_ELEM,
3247 if (real_chunk_size >= chunk_size)
3251 *level = li; // copy temporary buffer back to level data
3253 return real_chunk_size;
3256 static int LoadLevel_NOTE(File *file, int chunk_size, struct LevelInfo *level)
3258 int element = getMappedElement(getFile16BitBE(file));
3259 int envelope_nr = element - EL_ENVELOPE_1;
3260 int real_chunk_size = 2;
3262 xx_envelope = level->envelope[envelope_nr]; // copy into temporary buffer
3264 while (!checkEndOfFile(file))
3266 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE,
3269 if (real_chunk_size >= chunk_size)
3273 level->envelope[envelope_nr] = xx_envelope; // copy from temporary buffer
3275 return real_chunk_size;
3278 static int LoadLevel_CUSX(File *file, int chunk_size, struct LevelInfo *level)
3280 int element = getMappedElement(getFile16BitBE(file));
3281 int real_chunk_size = 2;
3282 struct ElementInfo *ei = &element_info[element];
3285 xx_ei = *ei; // copy element data into temporary buffer
3287 xx_ei.num_change_pages = -1;
3289 while (!checkEndOfFile(file))
3291 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base,
3293 if (xx_ei.num_change_pages != -1)
3296 if (real_chunk_size >= chunk_size)
3302 if (ei->num_change_pages == -1)
3304 Warn("LoadLevel_CUSX(): missing 'num_change_pages' for '%s'",
3307 ei->num_change_pages = 1;
3309 setElementChangePages(ei, 1);
3310 setElementChangeInfoToDefaults(ei->change);
3312 return real_chunk_size;
3315 // initialize number of change pages stored for this custom element
3316 setElementChangePages(ei, ei->num_change_pages);
3317 for (i = 0; i < ei->num_change_pages; i++)
3318 setElementChangeInfoToDefaults(&ei->change_page[i]);
3320 // start with reading properties for the first change page
3321 xx_current_change_page = 0;
3323 while (!checkEndOfFile(file))
3325 struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page];
3327 xx_change = *change; // copy change data into temporary buffer
3329 resetEventBits(); // reset bits; change page might have changed
3331 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_change,
3334 *change = xx_change;
3336 setEventFlagsFromEventBits(change);
3338 if (real_chunk_size >= chunk_size)
3342 level->file_has_custom_elements = TRUE;
3344 return real_chunk_size;
3347 static int LoadLevel_GRPX(File *file, int chunk_size, struct LevelInfo *level)
3349 int element = getMappedElement(getFile16BitBE(file));
3350 int real_chunk_size = 2;
3351 struct ElementInfo *ei = &element_info[element];
3352 struct ElementGroupInfo *group = ei->group;
3354 xx_ei = *ei; // copy element data into temporary buffer
3355 xx_group = *group; // copy group data into temporary buffer
3357 while (!checkEndOfFile(file))
3359 real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX,
3362 if (real_chunk_size >= chunk_size)
3369 level->file_has_custom_elements = TRUE;
3371 return real_chunk_size;
3374 static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
3375 struct LevelFileInfo *level_file_info,
3376 boolean level_info_only)
3378 char *filename = level_file_info->filename;
3379 char cookie[MAX_LINE_LEN];
3380 char chunk_name[CHUNK_ID_LEN + 1];
3384 if (!(file = openFile(filename, MODE_READ)))
3386 level->no_valid_file = TRUE;
3387 level->no_level_file = TRUE;
3389 if (level_info_only)
3392 Warn("cannot read level '%s' -- using empty level", filename);
3394 if (!setup.editor.use_template_for_new_levels)
3397 // if level file not found, try to initialize level data from template
3398 filename = getGlobalLevelTemplateFilename();
3400 if (!(file = openFile(filename, MODE_READ)))
3403 // default: for empty levels, use level template for custom elements
3404 level->use_custom_template = TRUE;
3406 level->no_valid_file = FALSE;
3409 getFileChunkBE(file, chunk_name, NULL);
3410 if (strEqual(chunk_name, "RND1"))
3412 getFile32BitBE(file); // not used
3414 getFileChunkBE(file, chunk_name, NULL);
3415 if (!strEqual(chunk_name, "CAVE"))
3417 level->no_valid_file = TRUE;
3419 Warn("unknown format of level file '%s'", filename);
3426 else // check for pre-2.0 file format with cookie string
3428 strcpy(cookie, chunk_name);
3429 if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
3431 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
3432 cookie[strlen(cookie) - 1] = '\0';
3434 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
3436 level->no_valid_file = TRUE;
3438 Warn("unknown format of level file '%s'", filename);
3445 if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1)
3447 level->no_valid_file = TRUE;
3449 Warn("unsupported version of level file '%s'", filename);
3456 // pre-2.0 level files have no game version, so use file version here
3457 level->game_version = level->file_version;
3460 if (level->file_version < FILE_VERSION_1_2)
3462 // level files from versions before 1.2.0 without chunk structure
3463 LoadLevel_HEAD(file, LEVEL_CHUNK_HEAD_SIZE, level);
3464 LoadLevel_BODY(file, level->fieldx * level->fieldy, level);
3472 int (*loader)(File *, int, struct LevelInfo *);
3476 { "VERS", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS },
3477 { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE },
3478 { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD },
3479 { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME },
3480 { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH },
3481 { "INFO", -1, LoadLevel_INFO },
3482 { "BODY", -1, LoadLevel_BODY },
3483 { "CONT", -1, LoadLevel_CONT },
3484 { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
3485 { "CNT3", -1, LoadLevel_CNT3 },
3486 { "CUS1", -1, LoadLevel_CUS1 },
3487 { "CUS2", -1, LoadLevel_CUS2 },
3488 { "CUS3", -1, LoadLevel_CUS3 },
3489 { "CUS4", -1, LoadLevel_CUS4 },
3490 { "GRP1", -1, LoadLevel_GRP1 },
3491 { "CONF", -1, LoadLevel_CONF },
3492 { "ELEM", -1, LoadLevel_ELEM },
3493 { "NOTE", -1, LoadLevel_NOTE },
3494 { "CUSX", -1, LoadLevel_CUSX },
3495 { "GRPX", -1, LoadLevel_GRPX },
3500 while (getFileChunkBE(file, chunk_name, &chunk_size))
3504 while (chunk_info[i].name != NULL &&
3505 !strEqual(chunk_name, chunk_info[i].name))
3508 if (chunk_info[i].name == NULL)
3510 Warn("unknown chunk '%s' in level file '%s'",
3511 chunk_name, filename);
3513 ReadUnusedBytesFromFile(file, chunk_size);
3515 else if (chunk_info[i].size != -1 &&
3516 chunk_info[i].size != chunk_size)
3518 Warn("wrong size (%d) of chunk '%s' in level file '%s'",
3519 chunk_size, chunk_name, filename);
3521 ReadUnusedBytesFromFile(file, chunk_size);
3525 // call function to load this level chunk
3526 int chunk_size_expected =
3527 (chunk_info[i].loader)(file, chunk_size, level);
3529 // the size of some chunks cannot be checked before reading other
3530 // chunks first (like "HEAD" and "BODY") that contain some header
3531 // information, so check them here
3532 if (chunk_size_expected != chunk_size)
3534 Warn("wrong size (%d) of chunk '%s' in level file '%s'",
3535 chunk_size, chunk_name, filename);
3545 // ----------------------------------------------------------------------------
3546 // functions for loading EM level
3547 // ----------------------------------------------------------------------------
3549 static void CopyNativeLevel_RND_to_EM(struct LevelInfo *level)
3551 static int ball_xy[8][2] =
3562 struct LevelInfo_EM *level_em = level->native_em_level;
3563 struct CAVE *cav = level_em->cav;
3566 cav->width = MIN(level->fieldx, MAX_PLAYFIELD_WIDTH);
3567 cav->height = MIN(level->fieldy, MAX_PLAYFIELD_HEIGHT);
3569 cav->time_seconds = level->time;
3570 cav->gems_needed = level->gems_needed;
3572 cav->emerald_score = level->score[SC_EMERALD];
3573 cav->diamond_score = level->score[SC_DIAMOND];
3574 cav->alien_score = level->score[SC_ROBOT];
3575 cav->tank_score = level->score[SC_SPACESHIP];
3576 cav->bug_score = level->score[SC_BUG];
3577 cav->eater_score = level->score[SC_YAMYAM];
3578 cav->nut_score = level->score[SC_NUT];
3579 cav->dynamite_score = level->score[SC_DYNAMITE];
3580 cav->key_score = level->score[SC_KEY];
3581 cav->exit_score = level->score[SC_TIME_BONUS];
3583 cav->num_eater_arrays = level->num_yamyam_contents;
3585 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3586 for (y = 0; y < 3; y++)
3587 for (x = 0; x < 3; x++)
3588 cav->eater_array[i][y * 3 + x] =
3589 map_element_RND_to_EM_cave(level->yamyam_content[i].e[x][y]);
3591 cav->amoeba_time = level->amoeba_speed;
3592 cav->wonderwall_time = level->time_magic_wall;
3593 cav->wheel_time = level->time_wheel;
3595 cav->android_move_time = level->android_move_time;
3596 cav->android_clone_time = level->android_clone_time;
3597 cav->ball_random = level->ball_random;
3598 cav->ball_active = level->ball_active_initial;
3599 cav->ball_time = level->ball_time;
3600 cav->num_ball_arrays = level->num_ball_contents;
3602 cav->lenses_score = level->lenses_score;
3603 cav->magnify_score = level->magnify_score;
3604 cav->slurp_score = level->slurp_score;
3606 cav->lenses_time = level->lenses_time;
3607 cav->magnify_time = level->magnify_time;
3609 cav->wind_direction =
3610 map_direction_RND_to_EM(level->wind_direction_initial);
3612 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3613 for (j = 0; j < 8; j++)
3614 cav->ball_array[i][j] =
3615 map_element_RND_to_EM_cave(level->ball_content[i].
3616 e[ball_xy[j][0]][ball_xy[j][1]]);
3618 map_android_clone_elements_RND_to_EM(level);
3620 // first fill the complete playfield with the empty space element
3621 for (y = 0; y < EM_MAX_CAVE_HEIGHT; y++)
3622 for (x = 0; x < EM_MAX_CAVE_WIDTH; x++)
3623 cav->cave[x][y] = Cblank;
3625 // then copy the real level contents from level file into the playfield
3626 for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
3628 int new_element = map_element_RND_to_EM_cave(level->field[x][y]);
3630 if (level->field[x][y] == EL_AMOEBA_DEAD)
3631 new_element = map_element_RND_to_EM_cave(EL_AMOEBA_WET);
3633 cav->cave[x][y] = new_element;
3636 for (i = 0; i < MAX_PLAYERS; i++)
3638 cav->player_x[i] = -1;
3639 cav->player_y[i] = -1;
3642 // initialize player positions and delete players from the playfield
3643 for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
3645 if (IS_PLAYER_ELEMENT(level->field[x][y]))
3647 int player_nr = GET_PLAYER_NR(level->field[x][y]);
3649 cav->player_x[player_nr] = x;
3650 cav->player_y[player_nr] = y;
3652 cav->cave[x][y] = map_element_RND_to_EM_cave(EL_EMPTY);
3657 static void CopyNativeLevel_EM_to_RND(struct LevelInfo *level)
3659 static int ball_xy[8][2] =
3670 struct LevelInfo_EM *level_em = level->native_em_level;
3671 struct CAVE *cav = level_em->cav;
3674 level->fieldx = MIN(cav->width, MAX_LEV_FIELDX);
3675 level->fieldy = MIN(cav->height, MAX_LEV_FIELDY);
3677 level->time = cav->time_seconds;
3678 level->gems_needed = cav->gems_needed;
3680 sprintf(level->name, "Level %d", level->file_info.nr);
3682 level->score[SC_EMERALD] = cav->emerald_score;
3683 level->score[SC_DIAMOND] = cav->diamond_score;
3684 level->score[SC_ROBOT] = cav->alien_score;
3685 level->score[SC_SPACESHIP] = cav->tank_score;
3686 level->score[SC_BUG] = cav->bug_score;
3687 level->score[SC_YAMYAM] = cav->eater_score;
3688 level->score[SC_NUT] = cav->nut_score;
3689 level->score[SC_DYNAMITE] = cav->dynamite_score;
3690 level->score[SC_KEY] = cav->key_score;
3691 level->score[SC_TIME_BONUS] = cav->exit_score;
3693 level->num_yamyam_contents = cav->num_eater_arrays;
3695 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3696 for (y = 0; y < 3; y++)
3697 for (x = 0; x < 3; x++)
3698 level->yamyam_content[i].e[x][y] =
3699 map_element_EM_to_RND_cave(cav->eater_array[i][y * 3 + x]);
3701 level->amoeba_speed = cav->amoeba_time;
3702 level->time_magic_wall = cav->wonderwall_time;
3703 level->time_wheel = cav->wheel_time;
3705 level->android_move_time = cav->android_move_time;
3706 level->android_clone_time = cav->android_clone_time;
3707 level->ball_random = cav->ball_random;
3708 level->ball_active_initial = cav->ball_active;
3709 level->ball_time = cav->ball_time;
3710 level->num_ball_contents = cav->num_ball_arrays;
3712 level->lenses_score = cav->lenses_score;
3713 level->magnify_score = cav->magnify_score;
3714 level->slurp_score = cav->slurp_score;
3716 level->lenses_time = cav->lenses_time;
3717 level->magnify_time = cav->magnify_time;
3719 level->wind_direction_initial =
3720 map_direction_EM_to_RND(cav->wind_direction);
3722 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
3723 for (j = 0; j < 8; j++)
3724 level->ball_content[i].e[ball_xy[j][0]][ball_xy[j][1]] =
3725 map_element_EM_to_RND_cave(cav->ball_array[i][j]);
3727 map_android_clone_elements_EM_to_RND(level);
3729 // convert the playfield (some elements need special treatment)
3730 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
3732 int new_element = map_element_EM_to_RND_cave(cav->cave[x][y]);
3734 if (new_element == EL_AMOEBA_WET && level->amoeba_speed == 0)
3735 new_element = EL_AMOEBA_DEAD;
3737 level->field[x][y] = new_element;
3740 for (i = 0; i < MAX_PLAYERS; i++)
3742 // in case of all players set to the same field, use the first player
3743 int nr = MAX_PLAYERS - i - 1;
3744 int jx = cav->player_x[nr];
3745 int jy = cav->player_y[nr];
3747 if (jx != -1 && jy != -1)
3748 level->field[jx][jy] = EL_PLAYER_1 + nr;
3751 // time score is counted for each 10 seconds left in Emerald Mine levels
3752 level->time_score_base = 10;
3756 // ----------------------------------------------------------------------------
3757 // functions for loading SP level
3758 // ----------------------------------------------------------------------------
3760 static void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
3762 struct LevelInfo_SP *level_sp = level->native_sp_level;
3763 LevelInfoType *header = &level_sp->header;
3766 level_sp->width = level->fieldx;
3767 level_sp->height = level->fieldy;
3769 for (x = 0; x < level->fieldx; x++)
3770 for (y = 0; y < level->fieldy; y++)
3771 level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
3773 header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
3775 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
3776 header->LevelTitle[i] = level->name[i];
3777 // !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!!
3779 header->InfotronsNeeded = level->gems_needed;
3781 header->SpecialPortCount = 0;
3783 for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
3785 boolean gravity_port_found = FALSE;
3786 boolean gravity_port_valid = FALSE;
3787 int gravity_port_flag;
3788 int gravity_port_base_element;
3789 int element = level->field[x][y];
3791 if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
3792 element <= EL_SP_GRAVITY_ON_PORT_UP)
3794 gravity_port_found = TRUE;
3795 gravity_port_valid = TRUE;
3796 gravity_port_flag = 1;
3797 gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
3799 else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
3800 element <= EL_SP_GRAVITY_OFF_PORT_UP)
3802 gravity_port_found = TRUE;
3803 gravity_port_valid = TRUE;
3804 gravity_port_flag = 0;
3805 gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
3807 else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
3808 element <= EL_SP_GRAVITY_PORT_UP)
3810 // change R'n'D style gravity inverting special port to normal port
3811 // (there are no gravity inverting ports in native Supaplex engine)
3813 gravity_port_found = TRUE;
3814 gravity_port_valid = FALSE;
3815 gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
3818 if (gravity_port_found)
3820 if (gravity_port_valid &&
3821 header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
3823 SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
3825 port->PortLocation = (y * level->fieldx + x) * 2;
3826 port->Gravity = gravity_port_flag;
3828 element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
3830 header->SpecialPortCount++;
3834 // change special gravity port to normal port
3836 element += EL_SP_PORT_RIGHT - gravity_port_base_element;
3839 level_sp->playfield[x][y] = element - EL_SP_START;
3844 static void CopyNativeLevel_SP_to_RND(struct LevelInfo *level)
3846 struct LevelInfo_SP *level_sp = level->native_sp_level;
3847 LevelInfoType *header = &level_sp->header;
3848 boolean num_invalid_elements = 0;
3851 level->fieldx = level_sp->width;
3852 level->fieldy = level_sp->height;
3854 for (x = 0; x < level->fieldx; x++)
3856 for (y = 0; y < level->fieldy; y++)
3858 int element_old = level_sp->playfield[x][y];
3859 int element_new = getMappedElement(map_element_SP_to_RND(element_old));
3861 if (element_new == EL_UNKNOWN)
3863 num_invalid_elements++;
3865 Debug("level:native:SP", "invalid element %d at position %d, %d",
3869 level->field[x][y] = element_new;
3873 if (num_invalid_elements > 0)
3874 Warn("found %d invalid elements%s", num_invalid_elements,
3875 (!options.debug ? " (use '--debug' for more details)" : ""));
3877 for (i = 0; i < MAX_PLAYERS; i++)
3878 level->initial_player_gravity[i] =
3879 (header->InitialGravity == 1 ? TRUE : FALSE);
3881 // skip leading spaces
3882 for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
3883 if (header->LevelTitle[i] != ' ')
3887 for (j = 0; i < SP_LEVEL_NAME_LEN; i++, j++)
3888 level->name[j] = header->LevelTitle[i];
3889 level->name[j] = '\0';
3891 // cut trailing spaces
3893 if (level->name[j - 1] == ' ' && level->name[j] == '\0')
3894 level->name[j - 1] = '\0';
3896 level->gems_needed = header->InfotronsNeeded;
3898 for (i = 0; i < header->SpecialPortCount; i++)
3900 SpecialPortType *port = &header->SpecialPort[i];
3901 int port_location = port->PortLocation;
3902 int gravity = port->Gravity;
3903 int port_x, port_y, port_element;
3905 port_x = (port_location / 2) % level->fieldx;
3906 port_y = (port_location / 2) / level->fieldx;
3908 if (port_x < 0 || port_x >= level->fieldx ||
3909 port_y < 0 || port_y >= level->fieldy)
3911 Warn("special port position (%d, %d) out of bounds", port_x, port_y);
3916 port_element = level->field[port_x][port_y];
3918 if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
3919 port_element > EL_SP_GRAVITY_PORT_UP)
3921 Warn("no special port at position (%d, %d)", port_x, port_y);
3926 // change previous (wrong) gravity inverting special port to either
3927 // gravity enabling special port or gravity disabling special port
3928 level->field[port_x][port_y] +=
3929 (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
3930 EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
3933 // change special gravity ports without database entries to normal ports
3934 for (x = 0; x < level->fieldx; x++)
3935 for (y = 0; y < level->fieldy; y++)
3936 if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
3937 level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
3938 level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
3940 level->time = 0; // no time limit
3941 level->amoeba_speed = 0;
3942 level->time_magic_wall = 0;
3943 level->time_wheel = 0;
3944 level->amoeba_content = EL_EMPTY;
3946 // original Supaplex does not use score values -- rate by playing time
3947 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
3948 level->score[i] = 0;
3950 level->rate_time_over_score = TRUE;
3952 // there are no yamyams in supaplex levels
3953 for (i = 0; i < level->num_yamyam_contents; i++)
3954 for (x = 0; x < 3; x++)
3955 for (y = 0; y < 3; y++)
3956 level->yamyam_content[i].e[x][y] = EL_EMPTY;
3959 static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
3961 struct LevelInfo_SP *level_sp = level->native_sp_level;
3962 struct DemoInfo_SP *demo = &level_sp->demo;
3965 // always start with reliable default values
3966 demo->is_available = FALSE;
3969 if (TAPE_IS_EMPTY(tape))
3972 demo->level_nr = tape.level_nr; // (currently not used)
3974 level_sp->header.DemoRandomSeed = tape.random_seed;
3978 for (i = 0; i < tape.length; i++)
3980 int demo_action = map_key_RND_to_SP(tape.pos[i].action[0]);
3981 int demo_repeat = tape.pos[i].delay;
3982 int demo_entries = (demo_repeat + 15) / 16;
3984 if (demo->length + demo_entries >= SP_MAX_TAPE_LEN)
3986 Warn("tape truncated: size exceeds maximum SP demo size %d",
3992 for (j = 0; j < demo_repeat / 16; j++)
3993 demo->data[demo->length++] = 0xf0 | demo_action;
3995 if (demo_repeat % 16)
3996 demo->data[demo->length++] = ((demo_repeat % 16 - 1) << 4) | demo_action;
3999 demo->is_available = TRUE;
4002 static void setTapeInfoToDefaults(void);
4004 static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
4006 struct LevelInfo_SP *level_sp = level->native_sp_level;
4007 struct DemoInfo_SP *demo = &level_sp->demo;
4008 char *filename = level->file_info.filename;
4011 // always start with reliable default values
4012 setTapeInfoToDefaults();
4014 if (!demo->is_available)
4017 tape.level_nr = demo->level_nr; // (currently not used)
4018 tape.random_seed = level_sp->header.DemoRandomSeed;
4020 TapeSetDateFromEpochSeconds(getFileTimestampEpochSeconds(filename));
4023 tape.pos[tape.counter].delay = 0;
4025 for (i = 0; i < demo->length; i++)
4027 int demo_action = demo->data[i] & 0x0f;
4028 int demo_repeat = (demo->data[i] & 0xf0) >> 4;
4029 int tape_action = map_key_SP_to_RND(demo_action);
4030 int tape_repeat = demo_repeat + 1;
4031 byte action[MAX_TAPE_ACTIONS] = { tape_action };
4032 boolean success = 0;
4035 for (j = 0; j < tape_repeat; j++)
4036 success = TapeAddAction(action);
4040 Warn("SP demo truncated: size exceeds maximum tape size %d",
4047 TapeHaltRecording();
4051 // ----------------------------------------------------------------------------
4052 // functions for loading MM level
4053 // ----------------------------------------------------------------------------
4055 static void CopyNativeLevel_RND_to_MM(struct LevelInfo *level)
4057 struct LevelInfo_MM *level_mm = level->native_mm_level;
4060 level_mm->fieldx = MIN(level->fieldx, MM_MAX_PLAYFIELD_WIDTH);
4061 level_mm->fieldy = MIN(level->fieldy, MM_MAX_PLAYFIELD_HEIGHT);
4063 level_mm->time = level->time;
4064 level_mm->kettles_needed = level->gems_needed;
4065 level_mm->auto_count_kettles = level->auto_count_gems;
4067 level_mm->laser_red = level->mm_laser_red;
4068 level_mm->laser_green = level->mm_laser_green;
4069 level_mm->laser_blue = level->mm_laser_blue;
4071 strcpy(level_mm->name, level->name);
4072 strcpy(level_mm->author, level->author);
4074 level_mm->score[SC_EMERALD] = level->score[SC_EMERALD];
4075 level_mm->score[SC_PACMAN] = level->score[SC_PACMAN];
4076 level_mm->score[SC_KEY] = level->score[SC_KEY];
4077 level_mm->score[SC_TIME_BONUS] = level->score[SC_TIME_BONUS];
4078 level_mm->score[SC_ELEM_BONUS] = level->score[SC_ELEM_BONUS];
4080 level_mm->amoeba_speed = level->amoeba_speed;
4081 level_mm->time_fuse = level->mm_time_fuse;
4082 level_mm->time_bomb = level->mm_time_bomb;
4083 level_mm->time_ball = level->mm_time_ball;
4084 level_mm->time_block = level->mm_time_block;
4086 for (x = 0; x < level->fieldx; x++)
4087 for (y = 0; y < level->fieldy; y++)
4089 level_mm->field[x][y] = map_element_RND_to_MM(level->field[x][y]);
4092 static void CopyNativeLevel_MM_to_RND(struct LevelInfo *level)
4094 struct LevelInfo_MM *level_mm = level->native_mm_level;
4097 level->fieldx = MIN(level_mm->fieldx, MAX_LEV_FIELDX);
4098 level->fieldy = MIN(level_mm->fieldy, MAX_LEV_FIELDY);
4100 level->time = level_mm->time;
4101 level->gems_needed = level_mm->kettles_needed;
4102 level->auto_count_gems = level_mm->auto_count_kettles;
4104 level->mm_laser_red = level_mm->laser_red;
4105 level->mm_laser_green = level_mm->laser_green;
4106 level->mm_laser_blue = level_mm->laser_blue;
4108 strcpy(level->name, level_mm->name);
4110 // only overwrite author from 'levelinfo.conf' if author defined in level
4111 if (!strEqual(level_mm->author, ANONYMOUS_NAME))
4112 strcpy(level->author, level_mm->author);
4114 level->score[SC_EMERALD] = level_mm->score[SC_EMERALD];
4115 level->score[SC_PACMAN] = level_mm->score[SC_PACMAN];
4116 level->score[SC_KEY] = level_mm->score[SC_KEY];
4117 level->score[SC_TIME_BONUS] = level_mm->score[SC_TIME_BONUS];
4118 level->score[SC_ELEM_BONUS] = level_mm->score[SC_ELEM_BONUS];
4120 level->amoeba_speed = level_mm->amoeba_speed;
4121 level->mm_time_fuse = level_mm->time_fuse;
4122 level->mm_time_bomb = level_mm->time_bomb;
4123 level->mm_time_ball = level_mm->time_ball;
4124 level->mm_time_block = level_mm->time_block;
4126 for (x = 0; x < level->fieldx; x++)
4127 for (y = 0; y < level->fieldy; y++)
4128 level->field[x][y] = map_element_MM_to_RND(level_mm->field[x][y]);
4132 // ----------------------------------------------------------------------------
4133 // functions for loading DC level
4134 // ----------------------------------------------------------------------------
4136 #define DC_LEVEL_HEADER_SIZE 344
4138 static unsigned short getDecodedWord_DC(unsigned short data_encoded,
4141 static int last_data_encoded;
4145 int diff_hi, diff_lo;
4146 int data_hi, data_lo;
4147 unsigned short data_decoded;
4151 last_data_encoded = 0;
4158 diff = data_encoded - last_data_encoded;
4159 diff_hi = diff & ~0xff;
4160 diff_lo = diff & 0xff;
4164 data_hi = diff_hi - (offset1 << 8) + (offset2 & 0xff00);
4165 data_lo = (diff_lo + (data_hi >> 16)) & 0x00ff;
4166 data_hi = data_hi & 0xff00;
4168 data_decoded = data_hi | data_lo;
4170 last_data_encoded = data_encoded;
4172 offset1 = (offset1 + 1) % 31;
4173 offset2 = offset2 & 0xff;
4175 return data_decoded;
4178 static int getMappedElement_DC(int element)
4186 // 0x0117 - 0x036e: (?)
4189 // 0x042d - 0x0684: (?)
4205 element = EL_CRYSTAL;
4208 case 0x0e77: // quicksand (boulder)
4209 element = EL_QUICKSAND_FAST_FULL;
4212 case 0x0e99: // slow quicksand (boulder)
4213 element = EL_QUICKSAND_FULL;
4217 element = EL_EM_EXIT_OPEN;
4221 element = EL_EM_EXIT_CLOSED;
4225 element = EL_EM_STEEL_EXIT_OPEN;
4229 element = EL_EM_STEEL_EXIT_CLOSED;
4232 case 0x0f4f: // dynamite (lit 1)
4233 element = EL_EM_DYNAMITE_ACTIVE;
4236 case 0x0f57: // dynamite (lit 2)
4237 element = EL_EM_DYNAMITE_ACTIVE;
4240 case 0x0f5f: // dynamite (lit 3)
4241 element = EL_EM_DYNAMITE_ACTIVE;
4244 case 0x0f67: // dynamite (lit 4)
4245 element = EL_EM_DYNAMITE_ACTIVE;
4252 element = EL_AMOEBA_WET;
4256 element = EL_AMOEBA_DROP;
4260 element = EL_DC_MAGIC_WALL;
4264 element = EL_SPACESHIP_UP;
4268 element = EL_SPACESHIP_DOWN;
4272 element = EL_SPACESHIP_LEFT;
4276 element = EL_SPACESHIP_RIGHT;
4280 element = EL_BUG_UP;
4284 element = EL_BUG_DOWN;
4288 element = EL_BUG_LEFT;
4292 element = EL_BUG_RIGHT;
4296 element = EL_MOLE_UP;
4300 element = EL_MOLE_DOWN;
4304 element = EL_MOLE_LEFT;
4308 element = EL_MOLE_RIGHT;
4316 element = EL_YAMYAM_UP;
4320 element = EL_SWITCHGATE_OPEN;
4324 element = EL_SWITCHGATE_CLOSED;
4328 element = EL_DC_SWITCHGATE_SWITCH_UP;
4332 element = EL_TIMEGATE_CLOSED;
4335 case 0x144c: // conveyor belt switch (green)
4336 element = EL_CONVEYOR_BELT_3_SWITCH_MIDDLE;
4339 case 0x144f: // conveyor belt switch (red)
4340 element = EL_CONVEYOR_BELT_1_SWITCH_MIDDLE;
4343 case 0x1452: // conveyor belt switch (blue)
4344 element = EL_CONVEYOR_BELT_4_SWITCH_MIDDLE;
4348 element = EL_CONVEYOR_BELT_3_MIDDLE;
4352 element = EL_CONVEYOR_BELT_3_LEFT;
4356 element = EL_CONVEYOR_BELT_3_RIGHT;
4360 element = EL_CONVEYOR_BELT_1_MIDDLE;
4364 element = EL_CONVEYOR_BELT_1_LEFT;
4368 element = EL_CONVEYOR_BELT_1_RIGHT;
4372 element = EL_CONVEYOR_BELT_4_MIDDLE;
4376 element = EL_CONVEYOR_BELT_4_LEFT;
4380 element = EL_CONVEYOR_BELT_4_RIGHT;
4384 element = EL_EXPANDABLE_WALL_HORIZONTAL;
4388 element = EL_EXPANDABLE_WALL_VERTICAL;
4392 element = EL_EXPANDABLE_WALL_ANY;
4395 case 0x14ce: // growing steel wall (left/right)
4396 element = EL_EXPANDABLE_STEELWALL_HORIZONTAL;
4399 case 0x14df: // growing steel wall (up/down)
4400 element = EL_EXPANDABLE_STEELWALL_VERTICAL;
4403 case 0x14e8: // growing steel wall (up/down/left/right)
4404 element = EL_EXPANDABLE_STEELWALL_ANY;
4408 element = EL_SHIELD_DEADLY;
4412 element = EL_EXTRA_TIME;
4420 element = EL_EMPTY_SPACE;
4423 case 0x1578: // quicksand (empty)
4424 element = EL_QUICKSAND_FAST_EMPTY;
4427 case 0x1579: // slow quicksand (empty)
4428 element = EL_QUICKSAND_EMPTY;
4438 element = EL_EM_DYNAMITE;
4441 case 0x15a1: // key (red)
4442 element = EL_EM_KEY_1;
4445 case 0x15a2: // key (yellow)
4446 element = EL_EM_KEY_2;
4449 case 0x15a3: // key (blue)
4450 element = EL_EM_KEY_4;
4453 case 0x15a4: // key (green)
4454 element = EL_EM_KEY_3;
4457 case 0x15a5: // key (white)
4458 element = EL_DC_KEY_WHITE;
4462 element = EL_WALL_SLIPPERY;
4469 case 0x15a8: // wall (not round)
4473 case 0x15a9: // (blue)
4474 element = EL_CHAR_A;
4477 case 0x15aa: // (blue)
4478 element = EL_CHAR_B;
4481 case 0x15ab: // (blue)
4482 element = EL_CHAR_C;
4485 case 0x15ac: // (blue)
4486 element = EL_CHAR_D;
4489 case 0x15ad: // (blue)
4490 element = EL_CHAR_E;
4493 case 0x15ae: // (blue)
4494 element = EL_CHAR_F;
4497 case 0x15af: // (blue)
4498 element = EL_CHAR_G;
4501 case 0x15b0: // (blue)
4502 element = EL_CHAR_H;
4505 case 0x15b1: // (blue)
4506 element = EL_CHAR_I;
4509 case 0x15b2: // (blue)
4510 element = EL_CHAR_J;
4513 case 0x15b3: // (blue)
4514 element = EL_CHAR_K;
4517 case 0x15b4: // (blue)
4518 element = EL_CHAR_L;
4521 case 0x15b5: // (blue)
4522 element = EL_CHAR_M;
4525 case 0x15b6: // (blue)
4526 element = EL_CHAR_N;
4529 case 0x15b7: // (blue)
4530 element = EL_CHAR_O;
4533 case 0x15b8: // (blue)
4534 element = EL_CHAR_P;
4537 case 0x15b9: // (blue)
4538 element = EL_CHAR_Q;
4541 case 0x15ba: // (blue)
4542 element = EL_CHAR_R;
4545 case 0x15bb: // (blue)
4546 element = EL_CHAR_S;
4549 case 0x15bc: // (blue)
4550 element = EL_CHAR_T;
4553 case 0x15bd: // (blue)
4554 element = EL_CHAR_U;
4557 case 0x15be: // (blue)
4558 element = EL_CHAR_V;
4561 case 0x15bf: // (blue)
4562 element = EL_CHAR_W;
4565 case 0x15c0: // (blue)
4566 element = EL_CHAR_X;
4569 case 0x15c1: // (blue)
4570 element = EL_CHAR_Y;
4573 case 0x15c2: // (blue)
4574 element = EL_CHAR_Z;
4577 case 0x15c3: // (blue)
4578 element = EL_CHAR_AUMLAUT;
4581 case 0x15c4: // (blue)
4582 element = EL_CHAR_OUMLAUT;
4585 case 0x15c5: // (blue)
4586 element = EL_CHAR_UUMLAUT;
4589 case 0x15c6: // (blue)
4590 element = EL_CHAR_0;
4593 case 0x15c7: // (blue)
4594 element = EL_CHAR_1;
4597 case 0x15c8: // (blue)
4598 element = EL_CHAR_2;
4601 case 0x15c9: // (blue)
4602 element = EL_CHAR_3;
4605 case 0x15ca: // (blue)
4606 element = EL_CHAR_4;
4609 case 0x15cb: // (blue)
4610 element = EL_CHAR_5;
4613 case 0x15cc: // (blue)
4614 element = EL_CHAR_6;
4617 case 0x15cd: // (blue)
4618 element = EL_CHAR_7;
4621 case 0x15ce: // (blue)
4622 element = EL_CHAR_8;
4625 case 0x15cf: // (blue)
4626 element = EL_CHAR_9;
4629 case 0x15d0: // (blue)
4630 element = EL_CHAR_PERIOD;
4633 case 0x15d1: // (blue)
4634 element = EL_CHAR_EXCLAM;
4637 case 0x15d2: // (blue)
4638 element = EL_CHAR_COLON;
4641 case 0x15d3: // (blue)
4642 element = EL_CHAR_LESS;
4645 case 0x15d4: // (blue)
4646 element = EL_CHAR_GREATER;
4649 case 0x15d5: // (blue)
4650 element = EL_CHAR_QUESTION;
4653 case 0x15d6: // (blue)
4654 element = EL_CHAR_COPYRIGHT;
4657 case 0x15d7: // (blue)
4658 element = EL_CHAR_UP;
4661 case 0x15d8: // (blue)
4662 element = EL_CHAR_DOWN;
4665 case 0x15d9: // (blue)
4666 element = EL_CHAR_BUTTON;
4669 case 0x15da: // (blue)
4670 element = EL_CHAR_PLUS;
4673 case 0x15db: // (blue)
4674 element = EL_CHAR_MINUS;
4677 case 0x15dc: // (blue)
4678 element = EL_CHAR_APOSTROPHE;
4681 case 0x15dd: // (blue)
4682 element = EL_CHAR_PARENLEFT;
4685 case 0x15de: // (blue)
4686 element = EL_CHAR_PARENRIGHT;
4689 case 0x15df: // (green)
4690 element = EL_CHAR_A;
4693 case 0x15e0: // (green)
4694 element = EL_CHAR_B;
4697 case 0x15e1: // (green)
4698 element = EL_CHAR_C;
4701 case 0x15e2: // (green)
4702 element = EL_CHAR_D;
4705 case 0x15e3: // (green)
4706 element = EL_CHAR_E;
4709 case 0x15e4: // (green)
4710 element = EL_CHAR_F;
4713 case 0x15e5: // (green)
4714 element = EL_CHAR_G;
4717 case 0x15e6: // (green)
4718 element = EL_CHAR_H;
4721 case 0x15e7: // (green)
4722 element = EL_CHAR_I;
4725 case 0x15e8: // (green)
4726 element = EL_CHAR_J;
4729 case 0x15e9: // (green)
4730 element = EL_CHAR_K;
4733 case 0x15ea: // (green)
4734 element = EL_CHAR_L;
4737 case 0x15eb: // (green)
4738 element = EL_CHAR_M;
4741 case 0x15ec: // (green)
4742 element = EL_CHAR_N;
4745 case 0x15ed: // (green)
4746 element = EL_CHAR_O;
4749 case 0x15ee: // (green)
4750 element = EL_CHAR_P;
4753 case 0x15ef: // (green)
4754 element = EL_CHAR_Q;
4757 case 0x15f0: // (green)
4758 element = EL_CHAR_R;
4761 case 0x15f1: // (green)
4762 element = EL_CHAR_S;
4765 case 0x15f2: // (green)
4766 element = EL_CHAR_T;
4769 case 0x15f3: // (green)
4770 element = EL_CHAR_U;
4773 case 0x15f4: // (green)
4774 element = EL_CHAR_V;
4777 case 0x15f5: // (green)
4778 element = EL_CHAR_W;
4781 case 0x15f6: // (green)
4782 element = EL_CHAR_X;
4785 case 0x15f7: // (green)
4786 element = EL_CHAR_Y;
4789 case 0x15f8: // (green)
4790 element = EL_CHAR_Z;
4793 case 0x15f9: // (green)
4794 element = EL_CHAR_AUMLAUT;
4797 case 0x15fa: // (green)
4798 element = EL_CHAR_OUMLAUT;
4801 case 0x15fb: // (green)
4802 element = EL_CHAR_UUMLAUT;
4805 case 0x15fc: // (green)
4806 element = EL_CHAR_0;
4809 case 0x15fd: // (green)
4810 element = EL_CHAR_1;
4813 case 0x15fe: // (green)
4814 element = EL_CHAR_2;
4817 case 0x15ff: // (green)
4818 element = EL_CHAR_3;
4821 case 0x1600: // (green)
4822 element = EL_CHAR_4;
4825 case 0x1601: // (green)
4826 element = EL_CHAR_5;
4829 case 0x1602: // (green)
4830 element = EL_CHAR_6;
4833 case 0x1603: // (green)
4834 element = EL_CHAR_7;
4837 case 0x1604: // (green)
4838 element = EL_CHAR_8;
4841 case 0x1605: // (green)
4842 element = EL_CHAR_9;
4845 case 0x1606: // (green)
4846 element = EL_CHAR_PERIOD;
4849 case 0x1607: // (green)
4850 element = EL_CHAR_EXCLAM;
4853 case 0x1608: // (green)
4854 element = EL_CHAR_COLON;
4857 case 0x1609: // (green)
4858 element = EL_CHAR_LESS;
4861 case 0x160a: // (green)
4862 element = EL_CHAR_GREATER;
4865 case 0x160b: // (green)
4866 element = EL_CHAR_QUESTION;
4869 case 0x160c: // (green)
4870 element = EL_CHAR_COPYRIGHT;
4873 case 0x160d: // (green)
4874 element = EL_CHAR_UP;
4877 case 0x160e: // (green)
4878 element = EL_CHAR_DOWN;
4881 case 0x160f: // (green)
4882 element = EL_CHAR_BUTTON;
4885 case 0x1610: // (green)
4886 element = EL_CHAR_PLUS;
4889 case 0x1611: // (green)
4890 element = EL_CHAR_MINUS;
4893 case 0x1612: // (green)
4894 element = EL_CHAR_APOSTROPHE;
4897 case 0x1613: // (green)
4898 element = EL_CHAR_PARENLEFT;
4901 case 0x1614: // (green)
4902 element = EL_CHAR_PARENRIGHT;
4905 case 0x1615: // (blue steel)
4906 element = EL_STEEL_CHAR_A;
4909 case 0x1616: // (blue steel)
4910 element = EL_STEEL_CHAR_B;
4913 case 0x1617: // (blue steel)
4914 element = EL_STEEL_CHAR_C;
4917 case 0x1618: // (blue steel)
4918 element = EL_STEEL_CHAR_D;
4921 case 0x1619: // (blue steel)
4922 element = EL_STEEL_CHAR_E;
4925 case 0x161a: // (blue steel)
4926 element = EL_STEEL_CHAR_F;
4929 case 0x161b: // (blue steel)
4930 element = EL_STEEL_CHAR_G;
4933 case 0x161c: // (blue steel)
4934 element = EL_STEEL_CHAR_H;
4937 case 0x161d: // (blue steel)
4938 element = EL_STEEL_CHAR_I;
4941 case 0x161e: // (blue steel)
4942 element = EL_STEEL_CHAR_J;
4945 case 0x161f: // (blue steel)
4946 element = EL_STEEL_CHAR_K;
4949 case 0x1620: // (blue steel)
4950 element = EL_STEEL_CHAR_L;
4953 case 0x1621: // (blue steel)
4954 element = EL_STEEL_CHAR_M;
4957 case 0x1622: // (blue steel)
4958 element = EL_STEEL_CHAR_N;
4961 case 0x1623: // (blue steel)
4962 element = EL_STEEL_CHAR_O;
4965 case 0x1624: // (blue steel)
4966 element = EL_STEEL_CHAR_P;
4969 case 0x1625: // (blue steel)
4970 element = EL_STEEL_CHAR_Q;
4973 case 0x1626: // (blue steel)
4974 element = EL_STEEL_CHAR_R;
4977 case 0x1627: // (blue steel)
4978 element = EL_STEEL_CHAR_S;
4981 case 0x1628: // (blue steel)
4982 element = EL_STEEL_CHAR_T;
4985 case 0x1629: // (blue steel)
4986 element = EL_STEEL_CHAR_U;
4989 case 0x162a: // (blue steel)
4990 element = EL_STEEL_CHAR_V;
4993 case 0x162b: // (blue steel)
4994 element = EL_STEEL_CHAR_W;
4997 case 0x162c: // (blue steel)
4998 element = EL_STEEL_CHAR_X;
5001 case 0x162d: // (blue steel)
5002 element = EL_STEEL_CHAR_Y;
5005 case 0x162e: // (blue steel)
5006 element = EL_STEEL_CHAR_Z;
5009 case 0x162f: // (blue steel)
5010 element = EL_STEEL_CHAR_AUMLAUT;
5013 case 0x1630: // (blue steel)
5014 element = EL_STEEL_CHAR_OUMLAUT;
5017 case 0x1631: // (blue steel)
5018 element = EL_STEEL_CHAR_UUMLAUT;
5021 case 0x1632: // (blue steel)
5022 element = EL_STEEL_CHAR_0;
5025 case 0x1633: // (blue steel)
5026 element = EL_STEEL_CHAR_1;
5029 case 0x1634: // (blue steel)
5030 element = EL_STEEL_CHAR_2;
5033 case 0x1635: // (blue steel)
5034 element = EL_STEEL_CHAR_3;
5037 case 0x1636: // (blue steel)
5038 element = EL_STEEL_CHAR_4;
5041 case 0x1637: // (blue steel)
5042 element = EL_STEEL_CHAR_5;
5045 case 0x1638: // (blue steel)
5046 element = EL_STEEL_CHAR_6;
5049 case 0x1639: // (blue steel)
5050 element = EL_STEEL_CHAR_7;
5053 case 0x163a: // (blue steel)
5054 element = EL_STEEL_CHAR_8;
5057 case 0x163b: // (blue steel)
5058 element = EL_STEEL_CHAR_9;
5061 case 0x163c: // (blue steel)
5062 element = EL_STEEL_CHAR_PERIOD;
5065 case 0x163d: // (blue steel)
5066 element = EL_STEEL_CHAR_EXCLAM;
5069 case 0x163e: // (blue steel)
5070 element = EL_STEEL_CHAR_COLON;
5073 case 0x163f: // (blue steel)
5074 element = EL_STEEL_CHAR_LESS;
5077 case 0x1640: // (blue steel)
5078 element = EL_STEEL_CHAR_GREATER;
5081 case 0x1641: // (blue steel)
5082 element = EL_STEEL_CHAR_QUESTION;
5085 case 0x1642: // (blue steel)
5086 element = EL_STEEL_CHAR_COPYRIGHT;
5089 case 0x1643: // (blue steel)
5090 element = EL_STEEL_CHAR_UP;
5093 case 0x1644: // (blue steel)
5094 element = EL_STEEL_CHAR_DOWN;
5097 case 0x1645: // (blue steel)
5098 element = EL_STEEL_CHAR_BUTTON;
5101 case 0x1646: // (blue steel)
5102 element = EL_STEEL_CHAR_PLUS;
5105 case 0x1647: // (blue steel)
5106 element = EL_STEEL_CHAR_MINUS;
5109 case 0x1648: // (blue steel)
5110 element = EL_STEEL_CHAR_APOSTROPHE;
5113 case 0x1649: // (blue steel)
5114 element = EL_STEEL_CHAR_PARENLEFT;
5117 case 0x164a: // (blue steel)
5118 element = EL_STEEL_CHAR_PARENRIGHT;
5121 case 0x164b: // (green steel)
5122 element = EL_STEEL_CHAR_A;
5125 case 0x164c: // (green steel)
5126 element = EL_STEEL_CHAR_B;
5129 case 0x164d: // (green steel)
5130 element = EL_STEEL_CHAR_C;
5133 case 0x164e: // (green steel)
5134 element = EL_STEEL_CHAR_D;
5137 case 0x164f: // (green steel)
5138 element = EL_STEEL_CHAR_E;
5141 case 0x1650: // (green steel)
5142 element = EL_STEEL_CHAR_F;
5145 case 0x1651: // (green steel)
5146 element = EL_STEEL_CHAR_G;
5149 case 0x1652: // (green steel)
5150 element = EL_STEEL_CHAR_H;
5153 case 0x1653: // (green steel)
5154 element = EL_STEEL_CHAR_I;
5157 case 0x1654: // (green steel)
5158 element = EL_STEEL_CHAR_J;
5161 case 0x1655: // (green steel)
5162 element = EL_STEEL_CHAR_K;
5165 case 0x1656: // (green steel)
5166 element = EL_STEEL_CHAR_L;
5169 case 0x1657: // (green steel)
5170 element = EL_STEEL_CHAR_M;
5173 case 0x1658: // (green steel)
5174 element = EL_STEEL_CHAR_N;
5177 case 0x1659: // (green steel)
5178 element = EL_STEEL_CHAR_O;
5181 case 0x165a: // (green steel)
5182 element = EL_STEEL_CHAR_P;
5185 case 0x165b: // (green steel)
5186 element = EL_STEEL_CHAR_Q;
5189 case 0x165c: // (green steel)
5190 element = EL_STEEL_CHAR_R;
5193 case 0x165d: // (green steel)
5194 element = EL_STEEL_CHAR_S;
5197 case 0x165e: // (green steel)
5198 element = EL_STEEL_CHAR_T;
5201 case 0x165f: // (green steel)
5202 element = EL_STEEL_CHAR_U;
5205 case 0x1660: // (green steel)
5206 element = EL_STEEL_CHAR_V;
5209 case 0x1661: // (green steel)
5210 element = EL_STEEL_CHAR_W;
5213 case 0x1662: // (green steel)
5214 element = EL_STEEL_CHAR_X;
5217 case 0x1663: // (green steel)
5218 element = EL_STEEL_CHAR_Y;
5221 case 0x1664: // (green steel)
5222 element = EL_STEEL_CHAR_Z;
5225 case 0x1665: // (green steel)
5226 element = EL_STEEL_CHAR_AUMLAUT;
5229 case 0x1666: // (green steel)
5230 element = EL_STEEL_CHAR_OUMLAUT;
5233 case 0x1667: // (green steel)
5234 element = EL_STEEL_CHAR_UUMLAUT;
5237 case 0x1668: // (green steel)
5238 element = EL_STEEL_CHAR_0;
5241 case 0x1669: // (green steel)
5242 element = EL_STEEL_CHAR_1;
5245 case 0x166a: // (green steel)
5246 element = EL_STEEL_CHAR_2;
5249 case 0x166b: // (green steel)
5250 element = EL_STEEL_CHAR_3;
5253 case 0x166c: // (green steel)
5254 element = EL_STEEL_CHAR_4;
5257 case 0x166d: // (green steel)
5258 element = EL_STEEL_CHAR_5;
5261 case 0x166e: // (green steel)
5262 element = EL_STEEL_CHAR_6;
5265 case 0x166f: // (green steel)
5266 element = EL_STEEL_CHAR_7;
5269 case 0x1670: // (green steel)
5270 element = EL_STEEL_CHAR_8;
5273 case 0x1671: // (green steel)
5274 element = EL_STEEL_CHAR_9;
5277 case 0x1672: // (green steel)
5278 element = EL_STEEL_CHAR_PERIOD;
5281 case 0x1673: // (green steel)
5282 element = EL_STEEL_CHAR_EXCLAM;
5285 case 0x1674: // (green steel)
5286 element = EL_STEEL_CHAR_COLON;
5289 case 0x1675: // (green steel)
5290 element = EL_STEEL_CHAR_LESS;
5293 case 0x1676: // (green steel)
5294 element = EL_STEEL_CHAR_GREATER;
5297 case 0x1677: // (green steel)
5298 element = EL_STEEL_CHAR_QUESTION;
5301 case 0x1678: // (green steel)
5302 element = EL_STEEL_CHAR_COPYRIGHT;
5305 case 0x1679: // (green steel)
5306 element = EL_STEEL_CHAR_UP;
5309 case 0x167a: // (green steel)
5310 element = EL_STEEL_CHAR_DOWN;
5313 case 0x167b: // (green steel)
5314 element = EL_STEEL_CHAR_BUTTON;
5317 case 0x167c: // (green steel)
5318 element = EL_STEEL_CHAR_PLUS;
5321 case 0x167d: // (green steel)
5322 element = EL_STEEL_CHAR_MINUS;
5325 case 0x167e: // (green steel)
5326 element = EL_STEEL_CHAR_APOSTROPHE;
5329 case 0x167f: // (green steel)
5330 element = EL_STEEL_CHAR_PARENLEFT;
5333 case 0x1680: // (green steel)
5334 element = EL_STEEL_CHAR_PARENRIGHT;
5337 case 0x1681: // gate (red)
5338 element = EL_EM_GATE_1;
5341 case 0x1682: // secret gate (red)
5342 element = EL_EM_GATE_1_GRAY;
5345 case 0x1683: // gate (yellow)
5346 element = EL_EM_GATE_2;
5349 case 0x1684: // secret gate (yellow)
5350 element = EL_EM_GATE_2_GRAY;
5353 case 0x1685: // gate (blue)
5354 element = EL_EM_GATE_4;
5357 case 0x1686: // secret gate (blue)
5358 element = EL_EM_GATE_4_GRAY;
5361 case 0x1687: // gate (green)
5362 element = EL_EM_GATE_3;
5365 case 0x1688: // secret gate (green)
5366 element = EL_EM_GATE_3_GRAY;
5369 case 0x1689: // gate (white)
5370 element = EL_DC_GATE_WHITE;
5373 case 0x168a: // secret gate (white)
5374 element = EL_DC_GATE_WHITE_GRAY;
5377 case 0x168b: // secret gate (no key)
5378 element = EL_DC_GATE_FAKE_GRAY;
5382 element = EL_ROBOT_WHEEL;
5386 element = EL_DC_TIMEGATE_SWITCH;
5390 element = EL_ACID_POOL_BOTTOM;
5394 element = EL_ACID_POOL_TOPLEFT;
5398 element = EL_ACID_POOL_TOPRIGHT;
5402 element = EL_ACID_POOL_BOTTOMLEFT;
5406 element = EL_ACID_POOL_BOTTOMRIGHT;
5410 element = EL_STEELWALL;
5414 element = EL_STEELWALL_SLIPPERY;
5417 case 0x1695: // steel wall (not round)
5418 element = EL_STEELWALL;
5421 case 0x1696: // steel wall (left)
5422 element = EL_DC_STEELWALL_1_LEFT;
5425 case 0x1697: // steel wall (bottom)
5426 element = EL_DC_STEELWALL_1_BOTTOM;
5429 case 0x1698: // steel wall (right)
5430 element = EL_DC_STEELWALL_1_RIGHT;
5433 case 0x1699: // steel wall (top)
5434 element = EL_DC_STEELWALL_1_TOP;
5437 case 0x169a: // steel wall (left/bottom)
5438 element = EL_DC_STEELWALL_1_BOTTOMLEFT;
5441 case 0x169b: // steel wall (right/bottom)
5442 element = EL_DC_STEELWALL_1_BOTTOMRIGHT;
5445 case 0x169c: // steel wall (right/top)
5446 element = EL_DC_STEELWALL_1_TOPRIGHT;
5449 case 0x169d: // steel wall (left/top)
5450 element = EL_DC_STEELWALL_1_TOPLEFT;
5453 case 0x169e: // steel wall (right/bottom small)
5454 element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2;
5457 case 0x169f: // steel wall (left/bottom small)
5458 element = EL_DC_STEELWALL_1_BOTTOMLEFT_2;
5461 case 0x16a0: // steel wall (right/top small)
5462 element = EL_DC_STEELWALL_1_TOPRIGHT_2;
5465 case 0x16a1: // steel wall (left/top small)
5466 element = EL_DC_STEELWALL_1_TOPLEFT_2;
5469 case 0x16a2: // steel wall (left/right)
5470 element = EL_DC_STEELWALL_1_VERTICAL;
5473 case 0x16a3: // steel wall (top/bottom)
5474 element = EL_DC_STEELWALL_1_HORIZONTAL;
5477 case 0x16a4: // steel wall 2 (left end)
5478 element = EL_DC_STEELWALL_2_LEFT;
5481 case 0x16a5: // steel wall 2 (right end)
5482 element = EL_DC_STEELWALL_2_RIGHT;
5485 case 0x16a6: // steel wall 2 (top end)
5486 element = EL_DC_STEELWALL_2_TOP;
5489 case 0x16a7: // steel wall 2 (bottom end)
5490 element = EL_DC_STEELWALL_2_BOTTOM;
5493 case 0x16a8: // steel wall 2 (left/right)
5494 element = EL_DC_STEELWALL_2_HORIZONTAL;
5497 case 0x16a9: // steel wall 2 (up/down)
5498 element = EL_DC_STEELWALL_2_VERTICAL;
5501 case 0x16aa: // steel wall 2 (mid)
5502 element = EL_DC_STEELWALL_2_MIDDLE;
5506 element = EL_SIGN_EXCLAMATION;
5510 element = EL_SIGN_RADIOACTIVITY;
5514 element = EL_SIGN_STOP;
5518 element = EL_SIGN_WHEELCHAIR;
5522 element = EL_SIGN_PARKING;
5526 element = EL_SIGN_NO_ENTRY;
5530 element = EL_SIGN_HEART;
5534 element = EL_SIGN_GIVE_WAY;
5538 element = EL_SIGN_ENTRY_FORBIDDEN;
5542 element = EL_SIGN_EMERGENCY_EXIT;
5546 element = EL_SIGN_YIN_YANG;
5550 element = EL_WALL_EMERALD;
5554 element = EL_WALL_DIAMOND;
5558 element = EL_WALL_PEARL;
5562 element = EL_WALL_CRYSTAL;
5566 element = EL_INVISIBLE_WALL;
5570 element = EL_INVISIBLE_STEELWALL;
5574 // EL_INVISIBLE_SAND
5577 element = EL_LIGHT_SWITCH;
5581 element = EL_ENVELOPE_1;
5585 if (element >= 0x0117 && element <= 0x036e) // (?)
5586 element = EL_DIAMOND;
5587 else if (element >= 0x042d && element <= 0x0684) // (?)
5588 element = EL_EMERALD;
5589 else if (element >= 0x157c && element <= 0x158b)
5591 else if (element >= 0x1590 && element <= 0x159f)
5592 element = EL_DC_LANDMINE;
5593 else if (element >= 0x16bc && element <= 0x16cb)
5594 element = EL_INVISIBLE_SAND;
5597 Warn("unknown Diamond Caves element 0x%04x", element);
5599 element = EL_UNKNOWN;
5604 return getMappedElement(element);
5607 static void LoadLevelFromFileStream_DC(File *file, struct LevelInfo *level,
5610 byte header[DC_LEVEL_HEADER_SIZE];
5612 int envelope_header_pos = 62;
5613 int envelope_content_pos = 94;
5614 int level_name_pos = 251;
5615 int level_author_pos = 292;
5616 int envelope_header_len;
5617 int envelope_content_len;
5619 int level_author_len;
5621 int num_yamyam_contents;
5624 getDecodedWord_DC(0, TRUE); // initialize DC2 decoding engine
5626 for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
5628 unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5630 header[i * 2 + 0] = header_word >> 8;
5631 header[i * 2 + 1] = header_word & 0xff;
5634 // read some values from level header to check level decoding integrity
5635 fieldx = header[6] | (header[7] << 8);
5636 fieldy = header[8] | (header[9] << 8);
5637 num_yamyam_contents = header[60] | (header[61] << 8);
5639 // do some simple sanity checks to ensure that level was correctly decoded
5640 if (fieldx < 1 || fieldx > 256 ||
5641 fieldy < 1 || fieldy > 256 ||
5642 num_yamyam_contents < 1 || num_yamyam_contents > 8)
5644 level->no_valid_file = TRUE;
5646 Warn("cannot decode level from stream -- using empty level");
5651 // maximum envelope header size is 31 bytes
5652 envelope_header_len = header[envelope_header_pos];
5653 // maximum envelope content size is 110 (156?) bytes
5654 envelope_content_len = header[envelope_content_pos];
5656 // maximum level title size is 40 bytes
5657 level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN);
5658 // maximum level author size is 30 (51?) bytes
5659 level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
5663 for (i = 0; i < envelope_header_len; i++)
5664 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5665 level->envelope[0].text[envelope_size++] =
5666 header[envelope_header_pos + 1 + i];
5668 if (envelope_header_len > 0 && envelope_content_len > 0)
5670 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5671 level->envelope[0].text[envelope_size++] = '\n';
5672 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5673 level->envelope[0].text[envelope_size++] = '\n';
5676 for (i = 0; i < envelope_content_len; i++)
5677 if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
5678 level->envelope[0].text[envelope_size++] =
5679 header[envelope_content_pos + 1 + i];
5681 level->envelope[0].text[envelope_size] = '\0';
5683 level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
5684 level->envelope[0].ysize = 10;
5685 level->envelope[0].autowrap = TRUE;
5686 level->envelope[0].centered = TRUE;
5688 for (i = 0; i < level_name_len; i++)
5689 level->name[i] = header[level_name_pos + 1 + i];
5690 level->name[level_name_len] = '\0';
5692 for (i = 0; i < level_author_len; i++)
5693 level->author[i] = header[level_author_pos + 1 + i];
5694 level->author[level_author_len] = '\0';
5696 num_yamyam_contents = header[60] | (header[61] << 8);
5697 level->num_yamyam_contents =
5698 MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
5700 for (i = 0; i < num_yamyam_contents; i++)
5702 for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
5704 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5705 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
5707 if (i < MAX_ELEMENT_CONTENTS)
5708 level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
5712 fieldx = header[6] | (header[7] << 8);
5713 fieldy = header[8] | (header[9] << 8);
5714 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
5715 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
5717 for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
5719 unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
5720 int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
5722 if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
5723 level->field[x][y] = getMappedElement_DC(element_dc);
5726 x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
5727 y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
5728 level->field[x][y] = EL_PLAYER_1;
5730 x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
5731 y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
5732 level->field[x][y] = EL_PLAYER_2;
5734 level->gems_needed = header[18] | (header[19] << 8);
5736 level->score[SC_EMERALD] = header[20] | (header[21] << 8);
5737 level->score[SC_DIAMOND] = header[22] | (header[23] << 8);
5738 level->score[SC_PEARL] = header[24] | (header[25] << 8);
5739 level->score[SC_CRYSTAL] = header[26] | (header[27] << 8);
5740 level->score[SC_NUT] = header[28] | (header[29] << 8);
5741 level->score[SC_ROBOT] = header[30] | (header[31] << 8);
5742 level->score[SC_SPACESHIP] = header[32] | (header[33] << 8);
5743 level->score[SC_BUG] = header[34] | (header[35] << 8);
5744 level->score[SC_YAMYAM] = header[36] | (header[37] << 8);
5745 level->score[SC_DYNAMITE] = header[38] | (header[39] << 8);
5746 level->score[SC_KEY] = header[40] | (header[41] << 8);
5747 level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8);
5749 level->time = header[44] | (header[45] << 8);
5751 level->amoeba_speed = header[46] | (header[47] << 8);
5752 level->time_light = header[48] | (header[49] << 8);
5753 level->time_timegate = header[50] | (header[51] << 8);
5754 level->time_wheel = header[52] | (header[53] << 8);
5755 level->time_magic_wall = header[54] | (header[55] << 8);
5756 level->extra_time = header[56] | (header[57] << 8);
5757 level->shield_normal_time = header[58] | (header[59] << 8);
5759 // shield and extra time elements do not have a score
5760 level->score[SC_SHIELD] = 0;
5761 level->extra_time_score = 0;
5763 // set time for normal and deadly shields to the same value
5764 level->shield_deadly_time = level->shield_normal_time;
5766 // Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
5767 // can slip down from flat walls, like normal walls and steel walls
5768 level->em_slippery_gems = TRUE;
5770 // time score is counted for each 10 seconds left in Diamond Caves levels
5771 level->time_score_base = 10;
5774 static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
5775 struct LevelFileInfo *level_file_info,
5776 boolean level_info_only)
5778 char *filename = level_file_info->filename;
5780 int num_magic_bytes = 8;
5781 char magic_bytes[num_magic_bytes + 1];
5782 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
5784 if (!(file = openFile(filename, MODE_READ)))
5786 level->no_valid_file = TRUE;
5788 if (!level_info_only)
5789 Warn("cannot read level '%s' -- using empty level", filename);
5794 // fseek(file, 0x0000, SEEK_SET);
5796 if (level_file_info->packed)
5798 // read "magic bytes" from start of file
5799 if (getStringFromFile(file, magic_bytes, num_magic_bytes + 1) == NULL)
5800 magic_bytes[0] = '\0';
5802 // check "magic bytes" for correct file format
5803 if (!strPrefix(magic_bytes, "DC2"))
5805 level->no_valid_file = TRUE;
5807 Warn("unknown DC level file '%s' -- using empty level", filename);
5812 if (strPrefix(magic_bytes, "DC2Win95") ||
5813 strPrefix(magic_bytes, "DC2Win98"))
5815 int position_first_level = 0x00fa;
5816 int extra_bytes = 4;
5819 // advance file stream to first level inside the level package
5820 skip_bytes = position_first_level - num_magic_bytes - extra_bytes;
5822 // each block of level data is followed by block of non-level data
5823 num_levels_to_skip *= 2;
5825 // at least skip header bytes, therefore use ">= 0" instead of "> 0"
5826 while (num_levels_to_skip >= 0)
5828 // advance file stream to next level inside the level package
5829 if (seekFile(file, skip_bytes, SEEK_CUR) != 0)
5831 level->no_valid_file = TRUE;
5833 Warn("cannot fseek in file '%s' -- using empty level", filename);
5838 // skip apparently unused extra bytes following each level
5839 ReadUnusedBytesFromFile(file, extra_bytes);
5841 // read size of next level in level package
5842 skip_bytes = getFile32BitLE(file);
5844 num_levels_to_skip--;
5849 level->no_valid_file = TRUE;
5851 Warn("unknown DC2 level file '%s' -- using empty level", filename);
5857 LoadLevelFromFileStream_DC(file, level, level_file_info->nr);
5863 // ----------------------------------------------------------------------------
5864 // functions for loading SB level
5865 // ----------------------------------------------------------------------------
5867 int getMappedElement_SB(int element_ascii, boolean use_ces)
5875 sb_element_mapping[] =
5877 { ' ', EL_EMPTY, EL_CUSTOM_1 }, // floor (space)
5878 { '#', EL_STEELWALL, EL_CUSTOM_2 }, // wall
5879 { '@', EL_PLAYER_1, EL_CUSTOM_3 }, // player
5880 { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, // box
5881 { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, // goal square
5882 { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, // box on goal square
5883 { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, // player on goal square
5884 { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, // floor beyond border
5891 for (i = 0; sb_element_mapping[i].ascii != 0; i++)
5892 if (element_ascii == sb_element_mapping[i].ascii)
5893 return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb);
5895 return EL_UNDEFINED;
5898 static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
5899 struct LevelFileInfo *level_file_info,
5900 boolean level_info_only)
5902 char *filename = level_file_info->filename;
5903 char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
5904 char last_comment[MAX_LINE_LEN];
5905 char level_name[MAX_LINE_LEN];
5908 int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
5909 boolean read_continued_line = FALSE;
5910 boolean reading_playfield = FALSE;
5911 boolean got_valid_playfield_line = FALSE;
5912 boolean invalid_playfield_char = FALSE;
5913 boolean load_xsb_to_ces = check_special_flags("load_xsb_to_ces");
5914 int file_level_nr = 0;
5916 int x = 0, y = 0; // initialized to make compilers happy
5918 last_comment[0] = '\0';
5919 level_name[0] = '\0';
5921 if (!(file = openFile(filename, MODE_READ)))
5923 level->no_valid_file = TRUE;
5925 if (!level_info_only)
5926 Warn("cannot read level '%s' -- using empty level", filename);
5931 while (!checkEndOfFile(file))
5933 // level successfully read, but next level may follow here
5934 if (!got_valid_playfield_line && reading_playfield)
5936 // read playfield from single level file -- skip remaining file
5937 if (!level_file_info->packed)
5940 if (file_level_nr >= num_levels_to_skip)
5945 last_comment[0] = '\0';
5946 level_name[0] = '\0';
5948 reading_playfield = FALSE;
5951 got_valid_playfield_line = FALSE;
5953 // read next line of input file
5954 if (!getStringFromFile(file, line, MAX_LINE_LEN))
5957 // check if line was completely read and is terminated by line break
5958 if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
5961 // cut trailing line break (this can be newline and/or carriage return)
5962 for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
5963 if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
5966 // copy raw input line for later use (mainly debugging output)
5967 strcpy(line_raw, line);
5969 if (read_continued_line)
5971 // append new line to existing line, if there is enough space
5972 if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
5973 strcat(previous_line, line_ptr);
5975 strcpy(line, previous_line); // copy storage buffer to line
5977 read_continued_line = FALSE;
5980 // if the last character is '\', continue at next line
5981 if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
5983 line[strlen(line) - 1] = '\0'; // cut off trailing backslash
5984 strcpy(previous_line, line); // copy line to storage buffer
5986 read_continued_line = TRUE;
5992 if (line[0] == '\0')
5995 // extract comment text from comment line
5998 for (line_ptr = line; *line_ptr; line_ptr++)
5999 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != ';')
6002 strcpy(last_comment, line_ptr);
6007 // extract level title text from line containing level title
6008 if (line[0] == '\'')
6010 strcpy(level_name, &line[1]);
6012 if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'')
6013 level_name[strlen(level_name) - 1] = '\0';
6018 // skip lines containing only spaces (or empty lines)
6019 for (line_ptr = line; *line_ptr; line_ptr++)
6020 if (*line_ptr != ' ')
6022 if (*line_ptr == '\0')
6025 // at this point, we have found a line containing part of a playfield
6027 got_valid_playfield_line = TRUE;
6029 if (!reading_playfield)
6031 reading_playfield = TRUE;
6032 invalid_playfield_char = FALSE;
6034 for (x = 0; x < MAX_LEV_FIELDX; x++)
6035 for (y = 0; y < MAX_LEV_FIELDY; y++)
6036 level->field[x][y] = getMappedElement_SB(' ', load_xsb_to_ces);
6041 // start with topmost tile row
6045 // skip playfield line if larger row than allowed
6046 if (y >= MAX_LEV_FIELDY)
6049 // start with leftmost tile column
6052 // read playfield elements from line
6053 for (line_ptr = line; *line_ptr; line_ptr++)
6055 int mapped_sb_element = getMappedElement_SB(*line_ptr, load_xsb_to_ces);
6057 // stop parsing playfield line if larger column than allowed
6058 if (x >= MAX_LEV_FIELDX)
6061 if (mapped_sb_element == EL_UNDEFINED)
6063 invalid_playfield_char = TRUE;
6068 level->field[x][y] = mapped_sb_element;
6070 // continue with next tile column
6073 level->fieldx = MAX(x, level->fieldx);
6076 if (invalid_playfield_char)
6078 // if first playfield line, treat invalid lines as comment lines
6080 reading_playfield = FALSE;
6085 // continue with next tile row
6093 level->fieldx = MIN(MAX(MIN_LEV_FIELDX, level->fieldx), MAX_LEV_FIELDX);
6094 level->fieldy = MIN(MAX(MIN_LEV_FIELDY, level->fieldy), MAX_LEV_FIELDY);
6096 if (!reading_playfield)
6098 level->no_valid_file = TRUE;
6100 Warn("cannot read level '%s' -- using empty level", filename);
6105 if (*level_name != '\0')
6107 strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN);
6108 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6110 else if (*last_comment != '\0')
6112 strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN);
6113 level->name[MAX_LEVEL_NAME_LEN] = '\0';
6117 sprintf(level->name, "--> Level %d <--", level_file_info->nr);
6120 // set all empty fields beyond the border walls to invisible steel wall
6121 for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
6123 if ((x == 0 || x == level->fieldx - 1 ||
6124 y == 0 || y == level->fieldy - 1) &&
6125 level->field[x][y] == getMappedElement_SB(' ', load_xsb_to_ces))
6126 FloodFillLevel(x, y, getMappedElement_SB('_', load_xsb_to_ces),
6127 level->field, level->fieldx, level->fieldy);
6130 // set special level settings for Sokoban levels
6133 level->use_step_counter = TRUE;
6134 level->auto_exit_sokoban = TRUE;
6136 level->score[SC_TIME_BONUS] = 0;
6137 level->time_score_base = 1;
6138 level->rate_time_over_score = TRUE;
6140 if (load_xsb_to_ces)
6142 // special global settings can now be set in level template
6144 level->use_custom_template = TRUE;
6149 // -------------------------------------------------------------------------
6150 // functions for handling native levels
6151 // -------------------------------------------------------------------------
6153 static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
6154 struct LevelFileInfo *level_file_info,
6155 boolean level_info_only)
6157 if (!LoadNativeLevel_EM(level_file_info->filename, level_info_only))
6158 level->no_valid_file = TRUE;
6161 static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
6162 struct LevelFileInfo *level_file_info,
6163 boolean level_info_only)
6167 // determine position of requested level inside level package
6168 if (level_file_info->packed)
6169 pos = level_file_info->nr - leveldir_current->first_level;
6171 if (!LoadNativeLevel_SP(level_file_info->filename, pos, level_info_only))
6172 level->no_valid_file = TRUE;
6175 static void LoadLevelFromFileInfo_MM(struct LevelInfo *level,
6176 struct LevelFileInfo *level_file_info,
6177 boolean level_info_only)
6179 if (!LoadNativeLevel_MM(level_file_info->filename, level_info_only))
6180 level->no_valid_file = TRUE;
6183 void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
6185 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6186 CopyNativeLevel_RND_to_EM(level);
6187 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6188 CopyNativeLevel_RND_to_SP(level);
6189 else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
6190 CopyNativeLevel_RND_to_MM(level);
6193 void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
6195 if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
6196 CopyNativeLevel_EM_to_RND(level);
6197 else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6198 CopyNativeLevel_SP_to_RND(level);
6199 else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
6200 CopyNativeLevel_MM_to_RND(level);
6203 void SaveNativeLevel(struct LevelInfo *level)
6205 if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
6207 char *basename = getSingleLevelBasenameExt(level->file_info.nr, "sp");
6208 char *filename = getLevelFilenameFromBasename(basename);
6210 CopyNativeLevel_RND_to_SP(level);
6211 CopyNativeTape_RND_to_SP(level);
6213 SaveNativeLevel_SP(filename);
6218 // ----------------------------------------------------------------------------
6219 // functions for loading generic level
6220 // ----------------------------------------------------------------------------
6222 static void LoadLevelFromFileInfo(struct LevelInfo *level,
6223 struct LevelFileInfo *level_file_info,
6224 boolean level_info_only)
6226 // always start with reliable default values
6227 setLevelInfoToDefaults(level, level_info_only, TRUE);
6229 switch (level_file_info->type)
6231 case LEVEL_FILE_TYPE_RND:
6232 LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
6235 case LEVEL_FILE_TYPE_EM:
6236 LoadLevelFromFileInfo_EM(level, level_file_info, level_info_only);
6237 level->game_engine_type = GAME_ENGINE_TYPE_EM;
6240 case LEVEL_FILE_TYPE_SP:
6241 LoadLevelFromFileInfo_SP(level, level_file_info, level_info_only);
6242 level->game_engine_type = GAME_ENGINE_TYPE_SP;
6245 case LEVEL_FILE_TYPE_MM:
6246 LoadLevelFromFileInfo_MM(level, level_file_info, level_info_only);
6247 level->game_engine_type = GAME_ENGINE_TYPE_MM;
6250 case LEVEL_FILE_TYPE_DC:
6251 LoadLevelFromFileInfo_DC(level, level_file_info, level_info_only);
6254 case LEVEL_FILE_TYPE_SB:
6255 LoadLevelFromFileInfo_SB(level, level_file_info, level_info_only);
6259 LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
6263 // if level file is invalid, restore level structure to default values
6264 if (level->no_valid_file)
6265 setLevelInfoToDefaults(level, level_info_only, FALSE);
6267 if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
6268 level->game_engine_type = GAME_ENGINE_TYPE_RND;
6270 if (level_file_info->type != LEVEL_FILE_TYPE_RND)
6271 CopyNativeLevel_Native_to_RND(level);
6274 void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
6276 static struct LevelFileInfo level_file_info;
6278 // always start with reliable default values
6279 setFileInfoToDefaults(&level_file_info);
6281 level_file_info.nr = 0; // unknown level number
6282 level_file_info.type = LEVEL_FILE_TYPE_RND; // no others supported yet
6284 setString(&level_file_info.filename, filename);
6286 LoadLevelFromFileInfo(level, &level_file_info, FALSE);
6289 static void LoadLevel_InitVersion(struct LevelInfo *level)
6293 if (leveldir_current == NULL) // only when dumping level
6296 // all engine modifications also valid for levels which use latest engine
6297 if (level->game_version < VERSION_IDENT(3,2,0,5))
6299 // time bonus score was given for 10 s instead of 1 s before 3.2.0-5
6300 level->time_score_base = 10;
6303 if (leveldir_current->latest_engine)
6305 // ---------- use latest game engine --------------------------------------
6307 /* For all levels which are forced to use the latest game engine version
6308 (normally all but user contributed, private and undefined levels), set
6309 the game engine version to the actual version; this allows for actual
6310 corrections in the game engine to take effect for existing, converted
6311 levels (from "classic" or other existing games) to make the emulation
6312 of the corresponding game more accurate, while (hopefully) not breaking
6313 existing levels created from other players. */
6315 level->game_version = GAME_VERSION_ACTUAL;
6317 /* Set special EM style gems behaviour: EM style gems slip down from
6318 normal, steel and growing wall. As this is a more fundamental change,
6319 it seems better to set the default behaviour to "off" (as it is more
6320 natural) and make it configurable in the level editor (as a property
6321 of gem style elements). Already existing converted levels (neither
6322 private nor contributed levels) are changed to the new behaviour. */
6324 if (level->file_version < FILE_VERSION_2_0)
6325 level->em_slippery_gems = TRUE;
6330 // ---------- use game engine the level was created with --------------------
6332 /* For all levels which are not forced to use the latest game engine
6333 version (normally user contributed, private and undefined levels),
6334 use the version of the game engine the levels were created for.
6336 Since 2.0.1, the game engine version is now directly stored
6337 in the level file (chunk "VERS"), so there is no need anymore
6338 to set the game version from the file version (except for old,
6339 pre-2.0 levels, where the game version is still taken from the
6340 file format version used to store the level -- see above). */
6342 // player was faster than enemies in 1.0.0 and before
6343 if (level->file_version == FILE_VERSION_1_0)
6344 for (i = 0; i < MAX_PLAYERS; i++)
6345 level->initial_player_stepsize[i] = STEPSIZE_FAST;
6347 // default behaviour for EM style gems was "slippery" only in 2.0.1
6348 if (level->game_version == VERSION_IDENT(2,0,1,0))
6349 level->em_slippery_gems = TRUE;
6351 // springs could be pushed over pits before (pre-release version) 2.2.0
6352 if (level->game_version < VERSION_IDENT(2,2,0,0))
6353 level->use_spring_bug = TRUE;
6355 if (level->game_version < VERSION_IDENT(3,2,0,5))
6357 // time orb caused limited time in endless time levels before 3.2.0-5
6358 level->use_time_orb_bug = TRUE;
6360 // default behaviour for snapping was "no snap delay" before 3.2.0-5
6361 level->block_snap_field = FALSE;
6363 // extra time score was same value as time left score before 3.2.0-5
6364 level->extra_time_score = level->score[SC_TIME_BONUS];
6367 if (level->game_version < VERSION_IDENT(3,2,0,7))
6369 // default behaviour for snapping was "not continuous" before 3.2.0-7
6370 level->continuous_snapping = FALSE;
6373 // only few elements were able to actively move into acid before 3.1.0
6374 // trigger settings did not exist before 3.1.0; set to default "any"
6375 if (level->game_version < VERSION_IDENT(3,1,0,0))
6377 // correct "can move into acid" settings (all zero in old levels)
6379 level->can_move_into_acid_bits = 0; // nothing can move into acid
6380 level->dont_collide_with_bits = 0; // nothing is deadly when colliding
6382 setMoveIntoAcidProperty(level, EL_ROBOT, TRUE);
6383 setMoveIntoAcidProperty(level, EL_SATELLITE, TRUE);
6384 setMoveIntoAcidProperty(level, EL_PENGUIN, TRUE);
6385 setMoveIntoAcidProperty(level, EL_BALLOON, TRUE);
6387 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6388 SET_PROPERTY(EL_CUSTOM_START + i, EP_CAN_MOVE_INTO_ACID, TRUE);
6390 // correct trigger settings (stored as zero == "none" in old levels)
6392 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6394 int element = EL_CUSTOM_START + i;
6395 struct ElementInfo *ei = &element_info[element];
6397 for (j = 0; j < ei->num_change_pages; j++)
6399 struct ElementChangeInfo *change = &ei->change_page[j];
6401 change->trigger_player = CH_PLAYER_ANY;
6402 change->trigger_page = CH_PAGE_ANY;
6407 // try to detect and fix "Snake Bite" levels, which are broken with 3.2.0
6409 int element = EL_CUSTOM_256;
6410 struct ElementInfo *ei = &element_info[element];
6411 struct ElementChangeInfo *change = &ei->change_page[0];
6413 /* This is needed to fix a problem that was caused by a bugfix in function
6414 game.c/CreateFieldExt() introduced with 3.2.0 that corrects the behaviour
6415 when a custom element changes to EL_SOKOBAN_FIELD_PLAYER (before, it did
6416 not replace walkable elements, but instead just placed the player on it,
6417 without placing the Sokoban field under the player). Unfortunately, this
6418 breaks "Snake Bite" style levels when the snake is halfway through a door
6419 that just closes (the snake head is still alive and can be moved in this
6420 case). This can be fixed by replacing the EL_SOKOBAN_FIELD_PLAYER by the
6421 player (without Sokoban element) which then gets killed as designed). */
6423 if ((strncmp(leveldir_current->identifier, "snake_bite", 10) == 0 ||
6424 strncmp(ei->description, "pause b4 death", 14) == 0) &&
6425 change->target_element == EL_SOKOBAN_FIELD_PLAYER)
6426 change->target_element = EL_PLAYER_1;
6429 // try to detect and fix "Zelda" style levels, which are broken with 3.2.5
6430 if (level->game_version < VERSION_IDENT(3,2,5,0))
6432 /* This is needed to fix a problem that was caused by a bugfix in function
6433 game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
6434 corrects the behaviour when a custom element changes to another custom
6435 element with a higher element number that has change actions defined.
6436 Normally, only one change per frame is allowed for custom elements.
6437 Therefore, it is checked if a custom element already changed in the
6438 current frame; if it did, subsequent changes are suppressed.
6439 Unfortunately, this is only checked for element changes, but not for
6440 change actions, which are still executed. As the function above loops
6441 through all custom elements from lower to higher, an element change
6442 resulting in a lower CE number won't be checked again, while a target
6443 element with a higher number will also be checked, and potential change
6444 actions will get executed for this CE, too (which is wrong), while
6445 further changes are ignored (which is correct). As this bugfix breaks
6446 Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
6447 few other levels like Alan Bond's "FMV"), allow the previous, incorrect
6448 behaviour for existing levels and tapes that make use of this bug */
6450 level->use_action_after_change_bug = TRUE;
6453 // not centering level after relocating player was default only in 3.2.3
6454 if (level->game_version == VERSION_IDENT(3,2,3,0)) // (no pre-releases)
6455 level->shifted_relocation = TRUE;
6457 // EM style elements always chain-exploded in R'n'D engine before 3.2.6
6458 if (level->game_version < VERSION_IDENT(3,2,6,0))
6459 level->em_explodes_by_fire = TRUE;
6461 // levels were solved by the first player entering an exit up to 4.1.0.0
6462 if (level->game_version <= VERSION_IDENT(4,1,0,0))
6463 level->solved_by_one_player = TRUE;
6465 // game logic of "game of life" and "biomaze" was buggy before 4.1.1.1
6466 if (level->game_version < VERSION_IDENT(4,1,1,1))
6467 level->use_life_bugs = TRUE;
6469 // only Sokoban fields (but not objects) had to be solved before 4.1.1.1
6470 if (level->game_version < VERSION_IDENT(4,1,1,1))
6471 level->sb_objects_needed = FALSE;
6473 // CE actions were triggered by unfinished digging/collecting up to 4.2.2.0
6474 if (level->game_version <= VERSION_IDENT(4,2,2,0))
6475 level->finish_dig_collect = FALSE;
6477 // CE changing to player was kept under the player if walkable up to 4.2.3.1
6478 if (level->game_version <= VERSION_IDENT(4,2,3,1))
6479 level->keep_walkable_ce = TRUE;
6482 static void LoadLevel_InitSettings_SB(struct LevelInfo *level)
6484 boolean is_sokoban_level = TRUE; // unless non-Sokoban elements found
6487 // check if this level is (not) a Sokoban level
6488 for (y = 0; y < level->fieldy; y++)
6489 for (x = 0; x < level->fieldx; x++)
6490 if (!IS_SB_ELEMENT(Tile[x][y]))
6491 is_sokoban_level = FALSE;
6493 if (is_sokoban_level)
6495 // set special level settings for Sokoban levels
6497 level->use_step_counter = TRUE;
6498 level->auto_exit_sokoban = TRUE;
6500 level->score[SC_TIME_BONUS] = 0;
6501 level->time_score_base = 1;
6502 level->rate_time_over_score = TRUE;
6506 static void LoadLevel_InitSettings(struct LevelInfo *level)
6508 // adjust level settings for (non-native) Sokoban-style levels
6509 LoadLevel_InitSettings_SB(level);
6512 static void LoadLevel_InitStandardElements(struct LevelInfo *level)
6516 // map elements that have changed in newer versions
6517 level->amoeba_content = getMappedElementByVersion(level->amoeba_content,
6518 level->game_version);
6519 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6520 for (x = 0; x < 3; x++)
6521 for (y = 0; y < 3; y++)
6522 level->yamyam_content[i].e[x][y] =
6523 getMappedElementByVersion(level->yamyam_content[i].e[x][y],
6524 level->game_version);
6528 static void LoadLevel_InitCustomElements(struct LevelInfo *level)
6532 // map custom element change events that have changed in newer versions
6533 // (these following values were accidentally changed in version 3.0.1)
6534 // (this seems to be needed only for 'ab_levelset3' and 'ab_levelset4')
6535 if (level->game_version <= VERSION_IDENT(3,0,0,0))
6537 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6539 int element = EL_CUSTOM_START + i;
6541 // order of checking and copying events to be mapped is important
6542 // (do not change the start and end value -- they are constant)
6543 for (j = CE_BY_OTHER_ACTION; j >= CE_VALUE_GETS_ZERO; j--)
6545 if (HAS_CHANGE_EVENT(element, j - 2))
6547 SET_CHANGE_EVENT(element, j - 2, FALSE);
6548 SET_CHANGE_EVENT(element, j, TRUE);
6552 // order of checking and copying events to be mapped is important
6553 // (do not change the start and end value -- they are constant)
6554 for (j = CE_PLAYER_COLLECTS_X; j >= CE_HITTING_SOMETHING; j--)
6556 if (HAS_CHANGE_EVENT(element, j - 1))
6558 SET_CHANGE_EVENT(element, j - 1, FALSE);
6559 SET_CHANGE_EVENT(element, j, TRUE);
6565 // initialize "can_change" field for old levels with only one change page
6566 if (level->game_version <= VERSION_IDENT(3,0,2,0))
6568 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6570 int element = EL_CUSTOM_START + i;
6572 if (CAN_CHANGE(element))
6573 element_info[element].change->can_change = TRUE;
6577 // correct custom element values (for old levels without these options)
6578 if (level->game_version < VERSION_IDENT(3,1,1,0))
6580 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6582 int element = EL_CUSTOM_START + i;
6583 struct ElementInfo *ei = &element_info[element];
6585 if (ei->access_direction == MV_NO_DIRECTION)
6586 ei->access_direction = MV_ALL_DIRECTIONS;
6590 // correct custom element values (fix invalid values for all versions)
6593 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6595 int element = EL_CUSTOM_START + i;
6596 struct ElementInfo *ei = &element_info[element];
6598 for (j = 0; j < ei->num_change_pages; j++)
6600 struct ElementChangeInfo *change = &ei->change_page[j];
6602 if (change->trigger_player == CH_PLAYER_NONE)
6603 change->trigger_player = CH_PLAYER_ANY;
6605 if (change->trigger_side == CH_SIDE_NONE)
6606 change->trigger_side = CH_SIDE_ANY;
6611 // initialize "can_explode" field for old levels which did not store this
6612 // !!! CHECK THIS -- "<= 3,1,0,0" IS PROBABLY WRONG !!!
6613 if (level->game_version <= VERSION_IDENT(3,1,0,0))
6615 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6617 int element = EL_CUSTOM_START + i;
6619 if (EXPLODES_1X1_OLD(element))
6620 element_info[element].explosion_type = EXPLODES_1X1;
6622 SET_PROPERTY(element, EP_CAN_EXPLODE, (EXPLODES_BY_FIRE(element) ||
6623 EXPLODES_SMASHED(element) ||
6624 EXPLODES_IMPACT(element)));
6628 // correct previously hard-coded move delay values for maze runner style
6629 if (level->game_version < VERSION_IDENT(3,1,1,0))
6631 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6633 int element = EL_CUSTOM_START + i;
6635 if (element_info[element].move_pattern & MV_MAZE_RUNNER_STYLE)
6637 // previously hard-coded and therefore ignored
6638 element_info[element].move_delay_fixed = 9;
6639 element_info[element].move_delay_random = 0;
6644 // set some other uninitialized values of custom elements in older levels
6645 if (level->game_version < VERSION_IDENT(3,1,0,0))
6647 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6649 int element = EL_CUSTOM_START + i;
6651 element_info[element].access_direction = MV_ALL_DIRECTIONS;
6653 element_info[element].explosion_delay = 17;
6654 element_info[element].ignition_delay = 8;
6658 // set mouse click change events to work for left/middle/right mouse button
6659 if (level->game_version < VERSION_IDENT(4,2,3,0))
6661 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
6663 int element = EL_CUSTOM_START + i;
6664 struct ElementInfo *ei = &element_info[element];
6666 for (j = 0; j < ei->num_change_pages; j++)
6668 struct ElementChangeInfo *change = &ei->change_page[j];
6670 if (change->has_event[CE_CLICKED_BY_MOUSE] ||
6671 change->has_event[CE_PRESSED_BY_MOUSE] ||
6672 change->has_event[CE_MOUSE_CLICKED_ON_X] ||
6673 change->has_event[CE_MOUSE_PRESSED_ON_X])
6674 change->trigger_side = CH_SIDE_ANY;
6680 static void LoadLevel_InitElements(struct LevelInfo *level)
6682 LoadLevel_InitStandardElements(level);
6684 if (level->file_has_custom_elements)
6685 LoadLevel_InitCustomElements(level);
6687 // initialize element properties for level editor etc.
6688 InitElementPropertiesEngine(level->game_version);
6689 InitElementPropertiesGfxElement();
6692 static void LoadLevel_InitPlayfield(struct LevelInfo *level)
6696 // map elements that have changed in newer versions
6697 for (y = 0; y < level->fieldy; y++)
6698 for (x = 0; x < level->fieldx; x++)
6699 level->field[x][y] = getMappedElementByVersion(level->field[x][y],
6700 level->game_version);
6702 // clear unused playfield data (nicer if level gets resized in editor)
6703 for (x = 0; x < MAX_LEV_FIELDX; x++)
6704 for (y = 0; y < MAX_LEV_FIELDY; y++)
6705 if (x >= level->fieldx || y >= level->fieldy)
6706 level->field[x][y] = EL_EMPTY;
6708 // copy elements to runtime playfield array
6709 for (x = 0; x < MAX_LEV_FIELDX; x++)
6710 for (y = 0; y < MAX_LEV_FIELDY; y++)
6711 Tile[x][y] = level->field[x][y];
6713 // initialize level size variables for faster access
6714 lev_fieldx = level->fieldx;
6715 lev_fieldy = level->fieldy;
6717 // determine border element for this level
6718 if (level->file_info.type == LEVEL_FILE_TYPE_DC)
6719 BorderElement = EL_EMPTY; // (in editor, SetBorderElement() is used)
6724 static void LoadLevel_InitNativeEngines(struct LevelInfo *level)
6726 struct LevelFileInfo *level_file_info = &level->file_info;
6728 if (level_file_info->type == LEVEL_FILE_TYPE_RND)
6729 CopyNativeLevel_RND_to_Native(level);
6732 static void LoadLevelTemplate_LoadAndInit(void)
6734 LoadLevelFromFileInfo(&level_template, &level_template.file_info, FALSE);
6736 LoadLevel_InitVersion(&level_template);
6737 LoadLevel_InitElements(&level_template);
6738 LoadLevel_InitSettings(&level_template);
6740 ActivateLevelTemplate();
6743 void LoadLevelTemplate(int nr)
6745 if (!fileExists(getGlobalLevelTemplateFilename()))
6747 Warn("no level template found for this level");
6752 setLevelFileInfo(&level_template.file_info, nr);
6754 LoadLevelTemplate_LoadAndInit();
6757 static void LoadNetworkLevelTemplate(struct NetworkLevelInfo *network_level)
6759 copyLevelFileInfo(&network_level->tmpl_info, &level_template.file_info);
6761 LoadLevelTemplate_LoadAndInit();
6764 static void LoadLevel_LoadAndInit(struct NetworkLevelInfo *network_level)
6766 LoadLevelFromFileInfo(&level, &level.file_info, FALSE);
6768 if (level.use_custom_template)
6770 if (network_level != NULL)
6771 LoadNetworkLevelTemplate(network_level);
6773 LoadLevelTemplate(-1);
6776 LoadLevel_InitVersion(&level);
6777 LoadLevel_InitElements(&level);
6778 LoadLevel_InitPlayfield(&level);
6779 LoadLevel_InitSettings(&level);
6781 LoadLevel_InitNativeEngines(&level);
6784 void LoadLevel(int nr)
6786 SetLevelSetInfo(leveldir_current->identifier, nr);
6788 setLevelFileInfo(&level.file_info, nr);
6790 LoadLevel_LoadAndInit(NULL);
6793 void LoadLevelInfoOnly(int nr)
6795 setLevelFileInfo(&level.file_info, nr);
6797 LoadLevelFromFileInfo(&level, &level.file_info, TRUE);
6800 void LoadNetworkLevel(struct NetworkLevelInfo *network_level)
6802 SetLevelSetInfo(network_level->leveldir_identifier,
6803 network_level->file_info.nr);
6805 copyLevelFileInfo(&network_level->file_info, &level.file_info);
6807 LoadLevel_LoadAndInit(network_level);
6810 static int SaveLevel_VERS(FILE *file, struct LevelInfo *level)
6814 chunk_size += putFileVersion(file, level->file_version);
6815 chunk_size += putFileVersion(file, level->game_version);
6820 static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
6824 chunk_size += putFile16BitBE(file, level->creation_date.year);
6825 chunk_size += putFile8Bit(file, level->creation_date.month);
6826 chunk_size += putFile8Bit(file, level->creation_date.day);
6831 #if ENABLE_HISTORIC_CHUNKS
6832 static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
6836 putFile8Bit(file, level->fieldx);
6837 putFile8Bit(file, level->fieldy);
6839 putFile16BitBE(file, level->time);
6840 putFile16BitBE(file, level->gems_needed);
6842 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
6843 putFile8Bit(file, level->name[i]);
6845 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
6846 putFile8Bit(file, level->score[i]);
6848 for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
6849 for (y = 0; y < 3; y++)
6850 for (x = 0; x < 3; x++)
6851 putFile8Bit(file, (level->encoding_16bit_yamyam ? EL_EMPTY :
6852 level->yamyam_content[i].e[x][y]));
6853 putFile8Bit(file, level->amoeba_speed);
6854 putFile8Bit(file, level->time_magic_wall);
6855 putFile8Bit(file, level->time_wheel);
6856 putFile8Bit(file, (level->encoding_16bit_amoeba ? EL_EMPTY :
6857 level->amoeba_content));
6858 putFile8Bit(file, (level->initial_player_stepsize == STEPSIZE_FAST ? 1 : 0));
6859 putFile8Bit(file, (level->initial_gravity ? 1 : 0));
6860 putFile8Bit(file, (level->encoding_16bit_field ? 1 : 0));
6861 putFile8Bit(file, (level->em_slippery_gems ? 1 : 0));
6863 putFile8Bit(file, (level->use_custom_template ? 1 : 0));
6865 putFile8Bit(file, (level->block_last_field ? 1 : 0));
6866 putFile8Bit(file, (level->sp_block_last_field ? 1 : 0));
6867 putFile32BitBE(file, level->can_move_into_acid_bits);
6868 putFile8Bit(file, level->dont_collide_with_bits);
6870 putFile8Bit(file, (level->use_spring_bug ? 1 : 0));
6871 putFile8Bit(file, (level->use_step_counter ? 1 : 0));
6873 putFile8Bit(file, (level->instant_relocation ? 1 : 0));
6874 putFile8Bit(file, (level->can_pass_to_walkable ? 1 : 0));
6875 putFile8Bit(file, (level->grow_into_diggable ? 1 : 0));
6877 putFile8Bit(file, level->game_engine_type);
6879 WriteUnusedBytesToFile(file, LEVEL_CHUNK_HEAD_UNUSED);
6883 static int SaveLevel_NAME(FILE *file, struct LevelInfo *level)
6888 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
6889 chunk_size += putFile8Bit(file, level->name[i]);
6894 static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
6899 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
6900 chunk_size += putFile8Bit(file, level->author[i]);
6905 #if ENABLE_HISTORIC_CHUNKS
6906 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
6911 for (y = 0; y < level->fieldy; y++)
6912 for (x = 0; x < level->fieldx; x++)
6913 if (level->encoding_16bit_field)
6914 chunk_size += putFile16BitBE(file, level->field[x][y]);
6916 chunk_size += putFile8Bit(file, level->field[x][y]);
6922 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
6927 for (y = 0; y < level->fieldy; y++)
6928 for (x = 0; x < level->fieldx; x++)
6929 chunk_size += putFile16BitBE(file, level->field[x][y]);
6934 #if ENABLE_HISTORIC_CHUNKS
6935 static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
6939 putFile8Bit(file, EL_YAMYAM);
6940 putFile8Bit(file, level->num_yamyam_contents);
6941 putFile8Bit(file, 0);
6942 putFile8Bit(file, 0);
6944 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6945 for (y = 0; y < 3; y++)
6946 for (x = 0; x < 3; x++)
6947 if (level->encoding_16bit_field)
6948 putFile16BitBE(file, level->yamyam_content[i].e[x][y]);
6950 putFile8Bit(file, level->yamyam_content[i].e[x][y]);
6954 #if ENABLE_HISTORIC_CHUNKS
6955 static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
6958 int num_contents, content_xsize, content_ysize;
6959 int content_array[MAX_ELEMENT_CONTENTS][3][3];
6961 if (element == EL_YAMYAM)
6963 num_contents = level->num_yamyam_contents;
6967 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6968 for (y = 0; y < 3; y++)
6969 for (x = 0; x < 3; x++)
6970 content_array[i][x][y] = level->yamyam_content[i].e[x][y];
6972 else if (element == EL_BD_AMOEBA)
6978 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6979 for (y = 0; y < 3; y++)
6980 for (x = 0; x < 3; x++)
6981 content_array[i][x][y] = EL_EMPTY;
6982 content_array[0][0][0] = level->amoeba_content;
6986 // chunk header already written -- write empty chunk data
6987 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
6989 Warn("cannot save content for element '%d'", element);
6994 putFile16BitBE(file, element);
6995 putFile8Bit(file, num_contents);
6996 putFile8Bit(file, content_xsize);
6997 putFile8Bit(file, content_ysize);
6999 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
7001 for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
7002 for (y = 0; y < 3; y++)
7003 for (x = 0; x < 3; x++)
7004 putFile16BitBE(file, content_array[i][x][y]);
7008 #if ENABLE_HISTORIC_CHUNKS
7009 static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
7011 int envelope_nr = element - EL_ENVELOPE_1;
7012 int envelope_len = strlen(level->envelope_text[envelope_nr]) + 1;
7016 chunk_size += putFile16BitBE(file, element);
7017 chunk_size += putFile16BitBE(file, envelope_len);
7018 chunk_size += putFile8Bit(file, level->envelope_xsize[envelope_nr]);
7019 chunk_size += putFile8Bit(file, level->envelope_ysize[envelope_nr]);
7021 WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT3_UNUSED);
7022 chunk_size += LEVEL_CHUNK_CNT3_UNUSED;
7024 for (i = 0; i < envelope_len; i++)
7025 chunk_size += putFile8Bit(file, level->envelope_text[envelope_nr][i]);
7031 #if ENABLE_HISTORIC_CHUNKS
7032 static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
7033 int num_changed_custom_elements)
7037 putFile16BitBE(file, num_changed_custom_elements);
7039 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7041 int element = EL_CUSTOM_START + i;
7043 struct ElementInfo *ei = &element_info[element];
7045 if (ei->properties[EP_BITFIELD_BASE_NR] != EP_BITMASK_DEFAULT)
7047 if (check < num_changed_custom_elements)
7049 putFile16BitBE(file, element);
7050 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7057 if (check != num_changed_custom_elements) // should not happen
7058 Warn("inconsistent number of custom element properties");
7062 #if ENABLE_HISTORIC_CHUNKS
7063 static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
7064 int num_changed_custom_elements)
7068 putFile16BitBE(file, num_changed_custom_elements);
7070 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7072 int element = EL_CUSTOM_START + i;
7074 if (element_info[element].change->target_element != EL_EMPTY_SPACE)
7076 if (check < num_changed_custom_elements)
7078 putFile16BitBE(file, element);
7079 putFile16BitBE(file, element_info[element].change->target_element);
7086 if (check != num_changed_custom_elements) // should not happen
7087 Warn("inconsistent number of custom target elements");
7091 #if ENABLE_HISTORIC_CHUNKS
7092 static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
7093 int num_changed_custom_elements)
7095 int i, j, x, y, check = 0;
7097 putFile16BitBE(file, num_changed_custom_elements);
7099 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7101 int element = EL_CUSTOM_START + i;
7102 struct ElementInfo *ei = &element_info[element];
7104 if (ei->modified_settings)
7106 if (check < num_changed_custom_elements)
7108 putFile16BitBE(file, element);
7110 for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
7111 putFile8Bit(file, ei->description[j]);
7113 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7115 // some free bytes for future properties and padding
7116 WriteUnusedBytesToFile(file, 7);
7118 putFile8Bit(file, ei->use_gfx_element);
7119 putFile16BitBE(file, ei->gfx_element_initial);
7121 putFile8Bit(file, ei->collect_score_initial);
7122 putFile8Bit(file, ei->collect_count_initial);
7124 putFile16BitBE(file, ei->push_delay_fixed);
7125 putFile16BitBE(file, ei->push_delay_random);
7126 putFile16BitBE(file, ei->move_delay_fixed);
7127 putFile16BitBE(file, ei->move_delay_random);
7129 putFile16BitBE(file, ei->move_pattern);
7130 putFile8Bit(file, ei->move_direction_initial);
7131 putFile8Bit(file, ei->move_stepsize);
7133 for (y = 0; y < 3; y++)
7134 for (x = 0; x < 3; x++)
7135 putFile16BitBE(file, ei->content.e[x][y]);
7137 putFile32BitBE(file, ei->change->events);
7139 putFile16BitBE(file, ei->change->target_element);
7141 putFile16BitBE(file, ei->change->delay_fixed);
7142 putFile16BitBE(file, ei->change->delay_random);
7143 putFile16BitBE(file, ei->change->delay_frames);
7145 putFile16BitBE(file, ei->change->initial_trigger_element);
7147 putFile8Bit(file, ei->change->explode);
7148 putFile8Bit(file, ei->change->use_target_content);
7149 putFile8Bit(file, ei->change->only_if_complete);
7150 putFile8Bit(file, ei->change->use_random_replace);
7152 putFile8Bit(file, ei->change->random_percentage);
7153 putFile8Bit(file, ei->change->replace_when);
7155 for (y = 0; y < 3; y++)
7156 for (x = 0; x < 3; x++)
7157 putFile16BitBE(file, ei->change->content.e[x][y]);
7159 putFile8Bit(file, ei->slippery_type);
7161 // some free bytes for future properties and padding
7162 WriteUnusedBytesToFile(file, LEVEL_CPART_CUS3_UNUSED);
7169 if (check != num_changed_custom_elements) // should not happen
7170 Warn("inconsistent number of custom element properties");
7174 #if ENABLE_HISTORIC_CHUNKS
7175 static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
7177 struct ElementInfo *ei = &element_info[element];
7180 // ---------- custom element base property values (96 bytes) ----------------
7182 putFile16BitBE(file, element);
7184 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7185 putFile8Bit(file, ei->description[i]);
7187 putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
7189 WriteUnusedBytesToFile(file, 4); // reserved for more base properties
7191 putFile8Bit(file, ei->num_change_pages);
7193 putFile16BitBE(file, ei->ce_value_fixed_initial);
7194 putFile16BitBE(file, ei->ce_value_random_initial);
7195 putFile8Bit(file, ei->use_last_ce_value);
7197 putFile8Bit(file, ei->use_gfx_element);
7198 putFile16BitBE(file, ei->gfx_element_initial);
7200 putFile8Bit(file, ei->collect_score_initial);
7201 putFile8Bit(file, ei->collect_count_initial);
7203 putFile8Bit(file, ei->drop_delay_fixed);
7204 putFile8Bit(file, ei->push_delay_fixed);
7205 putFile8Bit(file, ei->drop_delay_random);
7206 putFile8Bit(file, ei->push_delay_random);
7207 putFile16BitBE(file, ei->move_delay_fixed);
7208 putFile16BitBE(file, ei->move_delay_random);
7210 // bits 0 - 15 of "move_pattern" ...
7211 putFile16BitBE(file, ei->move_pattern & 0xffff);
7212 putFile8Bit(file, ei->move_direction_initial);
7213 putFile8Bit(file, ei->move_stepsize);
7215 putFile8Bit(file, ei->slippery_type);
7217 for (y = 0; y < 3; y++)
7218 for (x = 0; x < 3; x++)
7219 putFile16BitBE(file, ei->content.e[x][y]);
7221 putFile16BitBE(file, ei->move_enter_element);
7222 putFile16BitBE(file, ei->move_leave_element);
7223 putFile8Bit(file, ei->move_leave_type);
7225 // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
7226 putFile16BitBE(file, (ei->move_pattern >> 16) & 0xffff);
7228 putFile8Bit(file, ei->access_direction);
7230 putFile8Bit(file, ei->explosion_delay);
7231 putFile8Bit(file, ei->ignition_delay);
7232 putFile8Bit(file, ei->explosion_type);
7234 // some free bytes for future custom property values and padding
7235 WriteUnusedBytesToFile(file, 1);
7237 // ---------- change page property values (48 bytes) ------------------------
7239 for (i = 0; i < ei->num_change_pages; i++)
7241 struct ElementChangeInfo *change = &ei->change_page[i];
7242 unsigned int event_bits;
7244 // bits 0 - 31 of "has_event[]" ...
7246 for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
7247 if (change->has_event[j])
7248 event_bits |= (1 << j);
7249 putFile32BitBE(file, event_bits);
7251 putFile16BitBE(file, change->target_element);
7253 putFile16BitBE(file, change->delay_fixed);
7254 putFile16BitBE(file, change->delay_random);
7255 putFile16BitBE(file, change->delay_frames);
7257 putFile16BitBE(file, change->initial_trigger_element);
7259 putFile8Bit(file, change->explode);
7260 putFile8Bit(file, change->use_target_content);
7261 putFile8Bit(file, change->only_if_complete);
7262 putFile8Bit(file, change->use_random_replace);
7264 putFile8Bit(file, change->random_percentage);
7265 putFile8Bit(file, change->replace_when);
7267 for (y = 0; y < 3; y++)
7268 for (x = 0; x < 3; x++)
7269 putFile16BitBE(file, change->target_content.e[x][y]);
7271 putFile8Bit(file, change->can_change);
7273 putFile8Bit(file, change->trigger_side);
7275 putFile8Bit(file, change->trigger_player);
7276 putFile8Bit(file, (change->trigger_page == CH_PAGE_ANY ? CH_PAGE_ANY_FILE :
7277 log_2(change->trigger_page)));
7279 putFile8Bit(file, change->has_action);
7280 putFile8Bit(file, change->action_type);
7281 putFile8Bit(file, change->action_mode);
7282 putFile16BitBE(file, change->action_arg);
7284 // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
7286 for (j = 32; j < NUM_CHANGE_EVENTS; j++)
7287 if (change->has_event[j])
7288 event_bits |= (1 << (j - 32));
7289 putFile8Bit(file, event_bits);
7294 #if ENABLE_HISTORIC_CHUNKS
7295 static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
7297 struct ElementInfo *ei = &element_info[element];
7298 struct ElementGroupInfo *group = ei->group;
7301 putFile16BitBE(file, element);
7303 for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
7304 putFile8Bit(file, ei->description[i]);
7306 putFile8Bit(file, group->num_elements);
7308 putFile8Bit(file, ei->use_gfx_element);
7309 putFile16BitBE(file, ei->gfx_element_initial);
7311 putFile8Bit(file, group->choice_mode);
7313 // some free bytes for future values and padding
7314 WriteUnusedBytesToFile(file, 3);
7316 for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
7317 putFile16BitBE(file, group->element[i]);
7321 static int SaveLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *entry,
7322 boolean write_element)
7324 int save_type = entry->save_type;
7325 int data_type = entry->data_type;
7326 int conf_type = entry->conf_type;
7327 int byte_mask = conf_type & CONF_MASK_BYTES;
7328 int element = entry->element;
7329 int default_value = entry->default_value;
7331 boolean modified = FALSE;
7333 if (byte_mask != CONF_MASK_MULTI_BYTES)
7335 void *value_ptr = entry->value;
7336 int value = (data_type == TYPE_BOOLEAN ? *(boolean *)value_ptr :
7339 // check if any settings have been modified before saving them
7340 if (value != default_value)
7343 // do not save if explicitly told or if unmodified default settings
7344 if ((save_type == SAVE_CONF_NEVER) ||
7345 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7349 num_bytes += putFile16BitBE(file, element);
7351 num_bytes += putFile8Bit(file, conf_type);
7352 num_bytes += (byte_mask == CONF_MASK_1_BYTE ? putFile8Bit (file, value) :
7353 byte_mask == CONF_MASK_2_BYTE ? putFile16BitBE(file, value) :
7354 byte_mask == CONF_MASK_4_BYTE ? putFile32BitBE(file, value) :
7357 else if (data_type == TYPE_STRING)
7359 char *default_string = entry->default_string;
7360 char *string = (char *)(entry->value);
7361 int string_length = strlen(string);
7364 // check if any settings have been modified before saving them
7365 if (!strEqual(string, default_string))
7368 // do not save if explicitly told or if unmodified default settings
7369 if ((save_type == SAVE_CONF_NEVER) ||
7370 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7374 num_bytes += putFile16BitBE(file, element);
7376 num_bytes += putFile8Bit(file, conf_type);
7377 num_bytes += putFile16BitBE(file, string_length);
7379 for (i = 0; i < string_length; i++)
7380 num_bytes += putFile8Bit(file, string[i]);
7382 else if (data_type == TYPE_ELEMENT_LIST)
7384 int *element_array = (int *)(entry->value);
7385 int num_elements = *(int *)(entry->num_entities);
7388 // check if any settings have been modified before saving them
7389 for (i = 0; i < num_elements; i++)
7390 if (element_array[i] != default_value)
7393 // do not save if explicitly told or if unmodified default settings
7394 if ((save_type == SAVE_CONF_NEVER) ||
7395 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7399 num_bytes += putFile16BitBE(file, element);
7401 num_bytes += putFile8Bit(file, conf_type);
7402 num_bytes += putFile16BitBE(file, num_elements * CONF_ELEMENT_NUM_BYTES);
7404 for (i = 0; i < num_elements; i++)
7405 num_bytes += putFile16BitBE(file, element_array[i]);
7407 else if (data_type == TYPE_CONTENT_LIST)
7409 struct Content *content = (struct Content *)(entry->value);
7410 int num_contents = *(int *)(entry->num_entities);
7413 // check if any settings have been modified before saving them
7414 for (i = 0; i < num_contents; i++)
7415 for (y = 0; y < 3; y++)
7416 for (x = 0; x < 3; x++)
7417 if (content[i].e[x][y] != default_value)
7420 // do not save if explicitly told or if unmodified default settings
7421 if ((save_type == SAVE_CONF_NEVER) ||
7422 (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
7426 num_bytes += putFile16BitBE(file, element);
7428 num_bytes += putFile8Bit(file, conf_type);
7429 num_bytes += putFile16BitBE(file, num_contents * CONF_CONTENT_NUM_BYTES);
7431 for (i = 0; i < num_contents; i++)
7432 for (y = 0; y < 3; y++)
7433 for (x = 0; x < 3; x++)
7434 num_bytes += putFile16BitBE(file, content[i].e[x][y]);
7440 static int SaveLevel_INFO(FILE *file, struct LevelInfo *level)
7445 li = *level; // copy level data into temporary buffer
7447 for (i = 0; chunk_config_INFO[i].data_type != -1; i++)
7448 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_INFO[i], FALSE);
7453 static int SaveLevel_ELEM(FILE *file, struct LevelInfo *level)
7458 li = *level; // copy level data into temporary buffer
7460 for (i = 0; chunk_config_ELEM[i].data_type != -1; i++)
7461 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_ELEM[i], TRUE);
7466 static int SaveLevel_NOTE(FILE *file, struct LevelInfo *level, int element)
7468 int envelope_nr = element - EL_ENVELOPE_1;
7472 chunk_size += putFile16BitBE(file, element);
7474 // copy envelope data into temporary buffer
7475 xx_envelope = level->envelope[envelope_nr];
7477 for (i = 0; chunk_config_NOTE[i].data_type != -1; i++)
7478 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_NOTE[i], FALSE);
7483 static int SaveLevel_CUSX(FILE *file, struct LevelInfo *level, int element)
7485 struct ElementInfo *ei = &element_info[element];
7489 chunk_size += putFile16BitBE(file, element);
7491 xx_ei = *ei; // copy element data into temporary buffer
7493 // set default description string for this specific element
7494 strcpy(xx_default_description, getDefaultElementDescription(ei));
7496 for (i = 0; chunk_config_CUSX_base[i].data_type != -1; i++)
7497 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_base[i], FALSE);
7499 for (i = 0; i < ei->num_change_pages; i++)
7501 struct ElementChangeInfo *change = &ei->change_page[i];
7503 xx_current_change_page = i;
7505 xx_change = *change; // copy change data into temporary buffer
7508 setEventBitsFromEventFlags(change);
7510 for (j = 0; chunk_config_CUSX_change[j].data_type != -1; j++)
7511 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_change[j],
7518 static int SaveLevel_GRPX(FILE *file, struct LevelInfo *level, int element)
7520 struct ElementInfo *ei = &element_info[element];
7521 struct ElementGroupInfo *group = ei->group;
7525 chunk_size += putFile16BitBE(file, element);
7527 xx_ei = *ei; // copy element data into temporary buffer
7528 xx_group = *group; // copy group data into temporary buffer
7530 // set default description string for this specific element
7531 strcpy(xx_default_description, getDefaultElementDescription(ei));
7533 for (i = 0; chunk_config_GRPX[i].data_type != -1; i++)
7534 chunk_size += SaveLevel_MicroChunk(file, &chunk_config_GRPX[i], FALSE);
7539 static void SaveLevelFromFilename(struct LevelInfo *level, char *filename,
7540 boolean save_as_template)
7546 if (!(file = fopen(filename, MODE_WRITE)))
7548 Warn("cannot save level file '%s'", filename);
7553 level->file_version = FILE_VERSION_ACTUAL;
7554 level->game_version = GAME_VERSION_ACTUAL;
7556 level->creation_date = getCurrentDate();
7558 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
7559 putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
7561 chunk_size = SaveLevel_VERS(NULL, level);
7562 putFileChunkBE(file, "VERS", chunk_size);
7563 SaveLevel_VERS(file, level);
7565 chunk_size = SaveLevel_DATE(NULL, level);
7566 putFileChunkBE(file, "DATE", chunk_size);
7567 SaveLevel_DATE(file, level);
7569 chunk_size = SaveLevel_NAME(NULL, level);
7570 putFileChunkBE(file, "NAME", chunk_size);
7571 SaveLevel_NAME(file, level);
7573 chunk_size = SaveLevel_AUTH(NULL, level);
7574 putFileChunkBE(file, "AUTH", chunk_size);
7575 SaveLevel_AUTH(file, level);
7577 chunk_size = SaveLevel_INFO(NULL, level);
7578 putFileChunkBE(file, "INFO", chunk_size);
7579 SaveLevel_INFO(file, level);
7581 chunk_size = SaveLevel_BODY(NULL, level);
7582 putFileChunkBE(file, "BODY", chunk_size);
7583 SaveLevel_BODY(file, level);
7585 chunk_size = SaveLevel_ELEM(NULL, level);
7586 if (chunk_size > LEVEL_CHUNK_ELEM_UNCHANGED) // save if changed
7588 putFileChunkBE(file, "ELEM", chunk_size);
7589 SaveLevel_ELEM(file, level);
7592 for (i = 0; i < NUM_ENVELOPES; i++)
7594 int element = EL_ENVELOPE_1 + i;
7596 chunk_size = SaveLevel_NOTE(NULL, level, element);
7597 if (chunk_size > LEVEL_CHUNK_NOTE_UNCHANGED) // save if changed
7599 putFileChunkBE(file, "NOTE", chunk_size);
7600 SaveLevel_NOTE(file, level, element);
7604 // if not using template level, check for non-default custom/group elements
7605 if (!level->use_custom_template || save_as_template)
7607 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
7609 int element = EL_CUSTOM_START + i;
7611 chunk_size = SaveLevel_CUSX(NULL, level, element);
7612 if (chunk_size > LEVEL_CHUNK_CUSX_UNCHANGED) // save if changed
7614 putFileChunkBE(file, "CUSX", chunk_size);
7615 SaveLevel_CUSX(file, level, element);
7619 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
7621 int element = EL_GROUP_START + i;
7623 chunk_size = SaveLevel_GRPX(NULL, level, element);
7624 if (chunk_size > LEVEL_CHUNK_GRPX_UNCHANGED) // save if changed
7626 putFileChunkBE(file, "GRPX", chunk_size);
7627 SaveLevel_GRPX(file, level, element);
7634 SetFilePermissions(filename, PERMS_PRIVATE);
7637 void SaveLevel(int nr)
7639 char *filename = getDefaultLevelFilename(nr);
7641 SaveLevelFromFilename(&level, filename, FALSE);
7644 void SaveLevelTemplate(void)
7646 char *filename = getLocalLevelTemplateFilename();
7648 SaveLevelFromFilename(&level, filename, TRUE);
7651 boolean SaveLevelChecked(int nr)
7653 char *filename = getDefaultLevelFilename(nr);
7654 boolean new_level = !fileExists(filename);
7655 boolean level_saved = FALSE;
7657 if (new_level || Request("Save this level and kill the old?", REQ_ASK))
7662 Request("Level saved!", REQ_CONFIRM);
7670 void DumpLevel(struct LevelInfo *level)
7672 if (level->no_level_file || level->no_valid_file)
7674 Warn("cannot dump -- no valid level file found");
7680 Print("Level xxx (file version %08d, game version %08d)\n",
7681 level->file_version, level->game_version);
7684 Print("Level author: '%s'\n", level->author);
7685 Print("Level title: '%s'\n", level->name);
7687 Print("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
7689 Print("Level time: %d seconds\n", level->time);
7690 Print("Gems needed: %d\n", level->gems_needed);
7692 Print("Time for magic wall: %d seconds\n", level->time_magic_wall);
7693 Print("Time for wheel: %d seconds\n", level->time_wheel);
7694 Print("Time for light: %d seconds\n", level->time_light);
7695 Print("Time for timegate: %d seconds\n", level->time_timegate);
7697 Print("Amoeba speed: %d\n", level->amoeba_speed);
7700 Print("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
7701 Print("Player blocks last field: %s\n", (level->block_last_field ? "yes" : "no"));
7702 Print("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
7703 Print("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
7704 Print("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
7705 Print("rate time over score: %s\n", (level->rate_time_over_score ? "yes" : "no"));
7710 void DumpLevels(void)
7712 static LevelDirTree *dumplevel_leveldir = NULL;
7714 dumplevel_leveldir = getTreeInfoFromIdentifier(leveldir_first,
7715 global.dumplevel_leveldir);
7717 if (dumplevel_leveldir == NULL)
7718 Fail("no such level identifier: '%s'", global.dumplevel_leveldir);
7720 if (global.dumplevel_level_nr < dumplevel_leveldir->first_level ||
7721 global.dumplevel_level_nr > dumplevel_leveldir->last_level)
7722 Fail("no such level number: %d", global.dumplevel_level_nr);
7724 leveldir_current = dumplevel_leveldir;
7726 LoadLevel(global.dumplevel_level_nr);
7733 // ============================================================================
7734 // tape file functions
7735 // ============================================================================
7737 static void setTapeInfoToDefaults(void)
7741 // always start with reliable default values (empty tape)
7744 // default values (also for pre-1.2 tapes) with only the first player
7745 tape.player_participates[0] = TRUE;
7746 for (i = 1; i < MAX_PLAYERS; i++)
7747 tape.player_participates[i] = FALSE;
7749 // at least one (default: the first) player participates in every tape
7750 tape.num_participating_players = 1;
7752 tape.property_bits = TAPE_PROPERTY_NONE;
7754 tape.level_nr = level_nr;
7756 tape.changed = FALSE;
7758 tape.recording = FALSE;
7759 tape.playing = FALSE;
7760 tape.pausing = FALSE;
7762 tape.scr_fieldx = SCR_FIELDX_DEFAULT;
7763 tape.scr_fieldy = SCR_FIELDY_DEFAULT;
7765 tape.no_valid_file = FALSE;
7768 static int getTapePosSize(struct TapeInfo *tape)
7770 int tape_pos_size = 0;
7772 if (tape->use_key_actions)
7773 tape_pos_size += tape->num_participating_players;
7775 if (tape->use_mouse_actions)
7776 tape_pos_size += 3; // x and y position and mouse button mask
7778 tape_pos_size += 1; // tape action delay value
7780 return tape_pos_size;
7783 static void setTapeActionFlags(struct TapeInfo *tape, int value)
7785 tape->use_key_actions = FALSE;
7786 tape->use_mouse_actions = FALSE;
7788 if (value != TAPE_USE_MOUSE_ACTIONS_ONLY)
7789 tape->use_key_actions = TRUE;
7791 if (value != TAPE_USE_KEY_ACTIONS_ONLY)
7792 tape->use_mouse_actions = TRUE;
7795 static int getTapeActionValue(struct TapeInfo *tape)
7797 return (tape->use_key_actions &&
7798 tape->use_mouse_actions ? TAPE_USE_KEY_AND_MOUSE_ACTIONS :
7799 tape->use_key_actions ? TAPE_USE_KEY_ACTIONS_ONLY :
7800 tape->use_mouse_actions ? TAPE_USE_MOUSE_ACTIONS_ONLY :
7801 TAPE_ACTIONS_DEFAULT);
7804 static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape)
7806 tape->file_version = getFileVersion(file);
7807 tape->game_version = getFileVersion(file);
7812 static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape)
7816 tape->random_seed = getFile32BitBE(file);
7817 tape->date = getFile32BitBE(file);
7818 tape->length = getFile32BitBE(file);
7820 // read header fields that are new since version 1.2
7821 if (tape->file_version >= FILE_VERSION_1_2)
7823 byte store_participating_players = getFile8Bit(file);
7826 // since version 1.2, tapes store which players participate in the tape
7827 tape->num_participating_players = 0;
7828 for (i = 0; i < MAX_PLAYERS; i++)
7830 tape->player_participates[i] = FALSE;
7832 if (store_participating_players & (1 << i))
7834 tape->player_participates[i] = TRUE;
7835 tape->num_participating_players++;
7839 setTapeActionFlags(tape, getFile8Bit(file));
7841 tape->property_bits = getFile8Bit(file);
7843 ReadUnusedBytesFromFile(file, TAPE_CHUNK_HEAD_UNUSED);
7845 engine_version = getFileVersion(file);
7846 if (engine_version > 0)
7847 tape->engine_version = engine_version;
7849 tape->engine_version = tape->game_version;
7855 static int LoadTape_SCRN(File *file, int chunk_size, struct TapeInfo *tape)
7857 tape->scr_fieldx = getFile8Bit(file);
7858 tape->scr_fieldy = getFile8Bit(file);
7863 static int LoadTape_INFO(File *file, int chunk_size, struct TapeInfo *tape)
7865 char *level_identifier = NULL;
7866 int level_identifier_size;
7869 level_identifier_size = getFile16BitBE(file);
7871 level_identifier = checked_malloc(level_identifier_size);
7873 for (i = 0; i < level_identifier_size; i++)
7874 level_identifier[i] = getFile8Bit(file);
7876 strncpy(tape->level_identifier, level_identifier, MAX_FILENAME_LEN);
7877 tape->level_identifier[MAX_FILENAME_LEN] = '\0';
7879 checked_free(level_identifier);
7881 tape->level_nr = getFile16BitBE(file);
7883 chunk_size = 2 + level_identifier_size + 2;
7888 static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
7891 int tape_pos_size = getTapePosSize(tape);
7892 int chunk_size_expected = tape_pos_size * tape->length;
7894 if (chunk_size_expected != chunk_size)
7896 ReadUnusedBytesFromFile(file, chunk_size);
7897 return chunk_size_expected;
7900 for (i = 0; i < tape->length; i++)
7902 if (i >= MAX_TAPE_LEN)
7904 Warn("tape truncated -- size exceeds maximum tape size %d",
7907 // tape too large; read and ignore remaining tape data from this chunk
7908 for (;i < tape->length; i++)
7909 ReadUnusedBytesFromFile(file, tape_pos_size);
7914 if (tape->use_key_actions)
7916 for (j = 0; j < MAX_PLAYERS; j++)
7918 tape->pos[i].action[j] = MV_NONE;
7920 if (tape->player_participates[j])
7921 tape->pos[i].action[j] = getFile8Bit(file);
7925 if (tape->use_mouse_actions)
7927 tape->pos[i].action[TAPE_ACTION_LX] = getFile8Bit(file);
7928 tape->pos[i].action[TAPE_ACTION_LY] = getFile8Bit(file);
7929 tape->pos[i].action[TAPE_ACTION_BUTTON] = getFile8Bit(file);
7932 tape->pos[i].delay = getFile8Bit(file);
7934 if (tape->file_version == FILE_VERSION_1_0)
7936 // eliminate possible diagonal moves in old tapes
7937 // this is only for backward compatibility
7939 byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
7940 byte action = tape->pos[i].action[0];
7941 int k, num_moves = 0;
7943 for (k = 0; k<4; k++)
7945 if (action & joy_dir[k])
7947 tape->pos[i + num_moves].action[0] = joy_dir[k];
7949 tape->pos[i + num_moves].delay = 0;
7958 tape->length += num_moves;
7961 else if (tape->file_version < FILE_VERSION_2_0)
7963 // convert pre-2.0 tapes to new tape format
7965 if (tape->pos[i].delay > 1)
7968 tape->pos[i + 1] = tape->pos[i];
7969 tape->pos[i + 1].delay = 1;
7972 for (j = 0; j < MAX_PLAYERS; j++)
7973 tape->pos[i].action[j] = MV_NONE;
7974 tape->pos[i].delay--;
7981 if (checkEndOfFile(file))
7985 if (i != tape->length)
7986 chunk_size = tape_pos_size * i;
7991 static void LoadTape_SokobanSolution(char *filename)
7994 int move_delay = TILESIZE / level.initial_player_stepsize[0];
7996 if (!(file = openFile(filename, MODE_READ)))
7998 tape.no_valid_file = TRUE;
8003 while (!checkEndOfFile(file))
8005 unsigned char c = getByteFromFile(file);
8007 if (checkEndOfFile(file))
8014 tape.pos[tape.length].action[0] = MV_UP;
8015 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
8021 tape.pos[tape.length].action[0] = MV_DOWN;
8022 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
8028 tape.pos[tape.length].action[0] = MV_LEFT;
8029 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
8035 tape.pos[tape.length].action[0] = MV_RIGHT;
8036 tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
8044 // ignore white-space characters
8048 tape.no_valid_file = TRUE;
8050 Warn("unsupported Sokoban solution file '%s' ['%d']", filename, c);
8058 if (tape.no_valid_file)
8061 tape.length_frames = GetTapeLengthFrames();
8062 tape.length_seconds = GetTapeLengthSeconds();
8065 void LoadTapeFromFilename(char *filename)
8067 char cookie[MAX_LINE_LEN];
8068 char chunk_name[CHUNK_ID_LEN + 1];
8072 // always start with reliable default values
8073 setTapeInfoToDefaults();
8075 if (strSuffix(filename, ".sln"))
8077 LoadTape_SokobanSolution(filename);
8082 if (!(file = openFile(filename, MODE_READ)))
8084 tape.no_valid_file = TRUE;
8089 getFileChunkBE(file, chunk_name, NULL);
8090 if (strEqual(chunk_name, "RND1"))
8092 getFile32BitBE(file); // not used
8094 getFileChunkBE(file, chunk_name, NULL);
8095 if (!strEqual(chunk_name, "TAPE"))
8097 tape.no_valid_file = TRUE;
8099 Warn("unknown format of tape file '%s'", filename);
8106 else // check for pre-2.0 file format with cookie string
8108 strcpy(cookie, chunk_name);
8109 if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
8111 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8112 cookie[strlen(cookie) - 1] = '\0';
8114 if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
8116 tape.no_valid_file = TRUE;
8118 Warn("unknown format of tape file '%s'", filename);
8125 if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
8127 tape.no_valid_file = TRUE;
8129 Warn("unsupported version of tape file '%s'", filename);
8136 // pre-2.0 tape files have no game version, so use file version here
8137 tape.game_version = tape.file_version;
8140 if (tape.file_version < FILE_VERSION_1_2)
8142 // tape files from versions before 1.2.0 without chunk structure
8143 LoadTape_HEAD(file, TAPE_CHUNK_HEAD_SIZE, &tape);
8144 LoadTape_BODY(file, 2 * tape.length, &tape);
8152 int (*loader)(File *, int, struct TapeInfo *);
8156 { "VERS", TAPE_CHUNK_VERS_SIZE, LoadTape_VERS },
8157 { "HEAD", TAPE_CHUNK_HEAD_SIZE, LoadTape_HEAD },
8158 { "SCRN", TAPE_CHUNK_SCRN_SIZE, LoadTape_SCRN },
8159 { "INFO", -1, LoadTape_INFO },
8160 { "BODY", -1, LoadTape_BODY },
8164 while (getFileChunkBE(file, chunk_name, &chunk_size))
8168 while (chunk_info[i].name != NULL &&
8169 !strEqual(chunk_name, chunk_info[i].name))
8172 if (chunk_info[i].name == NULL)
8174 Warn("unknown chunk '%s' in tape file '%s'",
8175 chunk_name, filename);
8177 ReadUnusedBytesFromFile(file, chunk_size);
8179 else if (chunk_info[i].size != -1 &&
8180 chunk_info[i].size != chunk_size)
8182 Warn("wrong size (%d) of chunk '%s' in tape file '%s'",
8183 chunk_size, chunk_name, filename);
8185 ReadUnusedBytesFromFile(file, chunk_size);
8189 // call function to load this tape chunk
8190 int chunk_size_expected =
8191 (chunk_info[i].loader)(file, chunk_size, &tape);
8193 // the size of some chunks cannot be checked before reading other
8194 // chunks first (like "HEAD" and "BODY") that contain some header
8195 // information, so check them here
8196 if (chunk_size_expected != chunk_size)
8198 Warn("wrong size (%d) of chunk '%s' in tape file '%s'",
8199 chunk_size, chunk_name, filename);
8207 tape.length_frames = GetTapeLengthFrames();
8208 tape.length_seconds = GetTapeLengthSeconds();
8211 Debug("files:LoadTapeFromFilename", "tape file version: %d",
8213 Debug("files:LoadTapeFromFilename", "tape game version: %d",
8215 Debug("files:LoadTapeFromFilename", "tape engine version: %d",
8216 tape.engine_version);
8220 void LoadTape(int nr)
8222 char *filename = getTapeFilename(nr);
8224 LoadTapeFromFilename(filename);
8227 void LoadSolutionTape(int nr)
8229 char *filename = getSolutionTapeFilename(nr);
8231 LoadTapeFromFilename(filename);
8233 if (TAPE_IS_EMPTY(tape) &&
8234 level.game_engine_type == GAME_ENGINE_TYPE_SP &&
8235 level.native_sp_level->demo.is_available)
8236 CopyNativeTape_SP_to_RND(&level);
8239 static boolean checkSaveTape_SCRN(struct TapeInfo *tape)
8241 // chunk required for team mode tapes with non-default screen size
8242 return (tape->num_participating_players > 1 &&
8243 (tape->scr_fieldx != SCR_FIELDX_DEFAULT ||
8244 tape->scr_fieldy != SCR_FIELDY_DEFAULT));
8247 static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
8249 putFileVersion(file, tape->file_version);
8250 putFileVersion(file, tape->game_version);
8253 static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
8256 byte store_participating_players = 0;
8258 // set bits for participating players for compact storage
8259 for (i = 0; i < MAX_PLAYERS; i++)
8260 if (tape->player_participates[i])
8261 store_participating_players |= (1 << i);
8263 putFile32BitBE(file, tape->random_seed);
8264 putFile32BitBE(file, tape->date);
8265 putFile32BitBE(file, tape->length);
8267 putFile8Bit(file, store_participating_players);
8269 putFile8Bit(file, getTapeActionValue(tape));
8271 putFile8Bit(file, tape->property_bits);
8273 // unused bytes not at the end here for 4-byte alignment of engine_version
8274 WriteUnusedBytesToFile(file, TAPE_CHUNK_HEAD_UNUSED);
8276 putFileVersion(file, tape->engine_version);
8279 static void SaveTape_SCRN(FILE *file, struct TapeInfo *tape)
8281 putFile8Bit(file, tape->scr_fieldx);
8282 putFile8Bit(file, tape->scr_fieldy);
8285 static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
8287 int level_identifier_size = strlen(tape->level_identifier) + 1;
8290 putFile16BitBE(file, level_identifier_size);
8292 for (i = 0; i < level_identifier_size; i++)
8293 putFile8Bit(file, tape->level_identifier[i]);
8295 putFile16BitBE(file, tape->level_nr);
8298 static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
8302 for (i = 0; i < tape->length; i++)
8304 if (tape->use_key_actions)
8306 for (j = 0; j < MAX_PLAYERS; j++)
8307 if (tape->player_participates[j])
8308 putFile8Bit(file, tape->pos[i].action[j]);
8311 if (tape->use_mouse_actions)
8313 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LX]);
8314 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LY]);
8315 putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_BUTTON]);
8318 putFile8Bit(file, tape->pos[i].delay);
8322 void SaveTapeToFilename(char *filename)
8326 int info_chunk_size;
8327 int body_chunk_size;
8329 if (!(file = fopen(filename, MODE_WRITE)))
8331 Warn("cannot save level recording file '%s'", filename);
8336 tape_pos_size = getTapePosSize(&tape);
8338 info_chunk_size = 2 + (strlen(tape.level_identifier) + 1) + 2;
8339 body_chunk_size = tape_pos_size * tape.length;
8341 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
8342 putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
8344 putFileChunkBE(file, "VERS", TAPE_CHUNK_VERS_SIZE);
8345 SaveTape_VERS(file, &tape);
8347 putFileChunkBE(file, "HEAD", TAPE_CHUNK_HEAD_SIZE);
8348 SaveTape_HEAD(file, &tape);
8350 if (checkSaveTape_SCRN(&tape))
8352 putFileChunkBE(file, "SCRN", TAPE_CHUNK_SCRN_SIZE);
8353 SaveTape_SCRN(file, &tape);
8356 putFileChunkBE(file, "INFO", info_chunk_size);
8357 SaveTape_INFO(file, &tape);
8359 putFileChunkBE(file, "BODY", body_chunk_size);
8360 SaveTape_BODY(file, &tape);
8364 SetFilePermissions(filename, PERMS_PRIVATE);
8367 static void SaveTapeExt(char *filename)
8371 tape.file_version = FILE_VERSION_ACTUAL;
8372 tape.game_version = GAME_VERSION_ACTUAL;
8374 tape.num_participating_players = 0;
8376 // count number of participating players
8377 for (i = 0; i < MAX_PLAYERS; i++)
8378 if (tape.player_participates[i])
8379 tape.num_participating_players++;
8381 SaveTapeToFilename(filename);
8383 tape.changed = FALSE;
8386 void SaveTape(int nr)
8388 char *filename = getTapeFilename(nr);
8390 InitTapeDirectory(leveldir_current->subdir);
8392 SaveTapeExt(filename);
8395 void SaveScoreTape(int nr)
8397 char *filename = getScoreTapeFilename(tape.score_tape_basename, nr);
8399 // used instead of "leveldir_current->subdir" (for network games)
8400 InitScoreTapeDirectory(levelset.identifier, nr);
8402 SaveTapeExt(filename);
8405 static boolean SaveTapeCheckedExt(int nr, char *msg_replace, char *msg_saved,
8406 unsigned int req_state_added)
8408 char *filename = getTapeFilename(nr);
8409 boolean new_tape = !fileExists(filename);
8410 boolean tape_saved = FALSE;
8412 if (new_tape || Request(msg_replace, REQ_ASK | req_state_added))
8417 Request(msg_saved, REQ_CONFIRM | req_state_added);
8425 boolean SaveTapeChecked(int nr)
8427 return SaveTapeCheckedExt(nr, "Replace old tape?", "Tape saved!", 0);
8430 boolean SaveTapeChecked_LevelSolved(int nr)
8432 return SaveTapeCheckedExt(nr, "Level solved! Replace old tape?",
8433 "Level solved! Tape saved!", REQ_STAY_OPEN);
8436 void DumpTape(struct TapeInfo *tape)
8438 int tape_frame_counter;
8441 if (tape->no_valid_file)
8443 Warn("cannot dump -- no valid tape file found");
8449 Print("Tape of Level %03d (file version %08d, game version %08d)\n",
8450 tape->level_nr, tape->file_version, tape->game_version);
8451 Print(" (effective engine version %08d)\n",
8452 tape->engine_version);
8453 Print("Level series identifier: '%s'\n", tape->level_identifier);
8456 tape_frame_counter = 0;
8458 for (i = 0; i < tape->length; i++)
8460 if (i >= MAX_TAPE_LEN)
8465 for (j = 0; j < MAX_PLAYERS; j++)
8467 if (tape->player_participates[j])
8469 int action = tape->pos[i].action[j];
8471 Print("%d:%02x ", j, action);
8472 Print("[%c%c%c%c|%c%c] - ",
8473 (action & JOY_LEFT ? '<' : ' '),
8474 (action & JOY_RIGHT ? '>' : ' '),
8475 (action & JOY_UP ? '^' : ' '),
8476 (action & JOY_DOWN ? 'v' : ' '),
8477 (action & JOY_BUTTON_1 ? '1' : ' '),
8478 (action & JOY_BUTTON_2 ? '2' : ' '));
8482 Print("(%03d) ", tape->pos[i].delay);
8483 Print("[%05d]\n", tape_frame_counter);
8485 tape_frame_counter += tape->pos[i].delay;
8491 void DumpTapes(void)
8493 static LevelDirTree *dumptape_leveldir = NULL;
8495 dumptape_leveldir = getTreeInfoFromIdentifier(leveldir_first,
8496 global.dumptape_leveldir);
8498 if (dumptape_leveldir == NULL)
8499 Fail("no such level identifier: '%s'", global.dumptape_leveldir);
8501 if (global.dumptape_level_nr < dumptape_leveldir->first_level ||
8502 global.dumptape_level_nr > dumptape_leveldir->last_level)
8503 Fail("no such level number: %d", global.dumptape_level_nr);
8505 leveldir_current = dumptape_leveldir;
8507 if (options.mytapes)
8508 LoadTape(global.dumptape_level_nr);
8510 LoadSolutionTape(global.dumptape_level_nr);
8518 // ============================================================================
8519 // score file functions
8520 // ============================================================================
8522 static void setScoreInfoToDefaultsExt(struct ScoreInfo *scores)
8526 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8528 strcpy(scores->entry[i].tape_basename, UNDEFINED_FILENAME);
8529 strcpy(scores->entry[i].name, EMPTY_PLAYER_NAME);
8530 scores->entry[i].score = 0;
8531 scores->entry[i].time = 0;
8534 scores->num_entries = 0;
8535 scores->last_added = -1;
8536 scores->last_added_local = -1;
8538 scores->updated = FALSE;
8539 scores->force_last_added = FALSE;
8542 static void setScoreInfoToDefaults(void)
8544 setScoreInfoToDefaultsExt(&scores);
8547 static void setServerScoreInfoToDefaults(void)
8549 setScoreInfoToDefaultsExt(&server_scores);
8552 static void LoadScore_OLD(int nr)
8555 char *filename = getScoreFilename(nr);
8556 char cookie[MAX_LINE_LEN];
8557 char line[MAX_LINE_LEN];
8561 if (!(file = fopen(filename, MODE_READ)))
8564 // check file identifier
8565 if (fgets(cookie, MAX_LINE_LEN, file) == NULL)
8567 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8568 cookie[strlen(cookie) - 1] = '\0';
8570 if (!checkCookieString(cookie, SCORE_COOKIE_TMPL))
8572 Warn("unknown format of score file '%s'", filename);
8579 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8581 if (fscanf(file, "%d", &scores.entry[i].score) == EOF)
8582 Warn("fscanf() failed; %s", strerror(errno));
8584 if (fgets(line, MAX_LINE_LEN, file) == NULL)
8587 if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
8588 line[strlen(line) - 1] = '\0';
8590 for (line_ptr = line; *line_ptr; line_ptr++)
8592 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
8594 strncpy(scores.entry[i].name, line_ptr, MAX_PLAYER_NAME_LEN);
8595 scores.entry[i].name[MAX_PLAYER_NAME_LEN] = '\0';
8604 static void ConvertScore_OLD(void)
8606 // only convert score to time for levels that rate playing time over score
8607 if (!level.rate_time_over_score)
8610 // convert old score to playing time for score-less levels (like Supaplex)
8611 int time_final_max = 999;
8614 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8616 int score = scores.entry[i].score;
8618 if (score > 0 && score < time_final_max)
8619 scores.entry[i].time = (time_final_max - score - 1) * FRAMES_PER_SECOND;
8623 static int LoadScore_VERS(File *file, int chunk_size, struct ScoreInfo *scores)
8625 scores->file_version = getFileVersion(file);
8626 scores->game_version = getFileVersion(file);
8631 static int LoadScore_INFO(File *file, int chunk_size, struct ScoreInfo *scores)
8633 char *level_identifier = NULL;
8634 int level_identifier_size;
8637 level_identifier_size = getFile16BitBE(file);
8639 level_identifier = checked_malloc(level_identifier_size);
8641 for (i = 0; i < level_identifier_size; i++)
8642 level_identifier[i] = getFile8Bit(file);
8644 strncpy(scores->level_identifier, level_identifier, MAX_FILENAME_LEN);
8645 scores->level_identifier[MAX_FILENAME_LEN] = '\0';
8647 checked_free(level_identifier);
8649 scores->level_nr = getFile16BitBE(file);
8650 scores->num_entries = getFile16BitBE(file);
8652 chunk_size = 2 + level_identifier_size + 2 + 2;
8657 static int LoadScore_NAME(File *file, int chunk_size, struct ScoreInfo *scores)
8661 for (i = 0; i < scores->num_entries; i++)
8663 for (j = 0; j < MAX_PLAYER_NAME_LEN; j++)
8664 scores->entry[i].name[j] = getFile8Bit(file);
8666 scores->entry[i].name[MAX_PLAYER_NAME_LEN] = '\0';
8669 chunk_size = scores->num_entries * MAX_PLAYER_NAME_LEN;
8674 static int LoadScore_SCOR(File *file, int chunk_size, struct ScoreInfo *scores)
8678 for (i = 0; i < scores->num_entries; i++)
8679 scores->entry[i].score = getFile16BitBE(file);
8681 chunk_size = scores->num_entries * 2;
8686 static int LoadScore_TIME(File *file, int chunk_size, struct ScoreInfo *scores)
8690 for (i = 0; i < scores->num_entries; i++)
8691 scores->entry[i].time = getFile32BitBE(file);
8693 chunk_size = scores->num_entries * 4;
8698 static int LoadScore_TAPE(File *file, int chunk_size, struct ScoreInfo *scores)
8702 for (i = 0; i < scores->num_entries; i++)
8704 for (j = 0; j < MAX_SCORE_TAPE_BASENAME_LEN; j++)
8705 scores->entry[i].tape_basename[j] = getFile8Bit(file);
8707 scores->entry[i].tape_basename[MAX_SCORE_TAPE_BASENAME_LEN] = '\0';
8710 chunk_size = scores->num_entries * MAX_SCORE_TAPE_BASENAME_LEN;
8715 void LoadScore(int nr)
8717 char *filename = getScoreFilename(nr);
8718 char cookie[MAX_LINE_LEN];
8719 char chunk_name[CHUNK_ID_LEN + 1];
8721 boolean old_score_file_format = FALSE;
8724 // always start with reliable default values
8725 setScoreInfoToDefaults();
8727 if (!(file = openFile(filename, MODE_READ)))
8730 getFileChunkBE(file, chunk_name, NULL);
8731 if (strEqual(chunk_name, "RND1"))
8733 getFile32BitBE(file); // not used
8735 getFileChunkBE(file, chunk_name, NULL);
8736 if (!strEqual(chunk_name, "SCOR"))
8738 Warn("unknown format of score file '%s'", filename);
8745 else // check for old file format with cookie string
8747 strcpy(cookie, chunk_name);
8748 if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
8750 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
8751 cookie[strlen(cookie) - 1] = '\0';
8753 if (!checkCookieString(cookie, SCORE_COOKIE_TMPL))
8755 Warn("unknown format of score file '%s'", filename);
8762 old_score_file_format = TRUE;
8765 if (old_score_file_format)
8767 // score files from versions before 4.2.4.0 without chunk structure
8770 // convert score to time, if possible (mainly for Supaplex levels)
8779 int (*loader)(File *, int, struct ScoreInfo *);
8783 { "VERS", SCORE_CHUNK_VERS_SIZE, LoadScore_VERS },
8784 { "INFO", -1, LoadScore_INFO },
8785 { "NAME", -1, LoadScore_NAME },
8786 { "SCOR", -1, LoadScore_SCOR },
8787 { "TIME", -1, LoadScore_TIME },
8788 { "TAPE", -1, LoadScore_TAPE },
8793 while (getFileChunkBE(file, chunk_name, &chunk_size))
8797 while (chunk_info[i].name != NULL &&
8798 !strEqual(chunk_name, chunk_info[i].name))
8801 if (chunk_info[i].name == NULL)
8803 Warn("unknown chunk '%s' in score file '%s'",
8804 chunk_name, filename);
8806 ReadUnusedBytesFromFile(file, chunk_size);
8808 else if (chunk_info[i].size != -1 &&
8809 chunk_info[i].size != chunk_size)
8811 Warn("wrong size (%d) of chunk '%s' in score file '%s'",
8812 chunk_size, chunk_name, filename);
8814 ReadUnusedBytesFromFile(file, chunk_size);
8818 // call function to load this score chunk
8819 int chunk_size_expected =
8820 (chunk_info[i].loader)(file, chunk_size, &scores);
8822 // the size of some chunks cannot be checked before reading other
8823 // chunks first (like "HEAD" and "BODY") that contain some header
8824 // information, so check them here
8825 if (chunk_size_expected != chunk_size)
8827 Warn("wrong size (%d) of chunk '%s' in score file '%s'",
8828 chunk_size, chunk_name, filename);
8837 #if ENABLE_HISTORIC_CHUNKS
8838 void SaveScore_OLD(int nr)
8841 char *filename = getScoreFilename(nr);
8844 // used instead of "leveldir_current->subdir" (for network games)
8845 InitScoreDirectory(levelset.identifier);
8847 if (!(file = fopen(filename, MODE_WRITE)))
8849 Warn("cannot save score for level %d", nr);
8854 fprintf(file, "%s\n\n", SCORE_COOKIE);
8856 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8857 fprintf(file, "%d %s\n", scores.entry[i].score, scores.entry[i].name);
8861 SetFilePermissions(filename, PERMS_PRIVATE);
8865 static void SaveScore_VERS(FILE *file, struct ScoreInfo *scores)
8867 putFileVersion(file, scores->file_version);
8868 putFileVersion(file, scores->game_version);
8871 static void SaveScore_INFO(FILE *file, struct ScoreInfo *scores)
8873 int level_identifier_size = strlen(scores->level_identifier) + 1;
8876 putFile16BitBE(file, level_identifier_size);
8878 for (i = 0; i < level_identifier_size; i++)
8879 putFile8Bit(file, scores->level_identifier[i]);
8881 putFile16BitBE(file, scores->level_nr);
8882 putFile16BitBE(file, scores->num_entries);
8885 static void SaveScore_NAME(FILE *file, struct ScoreInfo *scores)
8889 for (i = 0; i < scores->num_entries; i++)
8891 int name_size = strlen(scores->entry[i].name);
8893 for (j = 0; j < MAX_PLAYER_NAME_LEN; j++)
8894 putFile8Bit(file, (j < name_size ? scores->entry[i].name[j] : 0));
8898 static void SaveScore_SCOR(FILE *file, struct ScoreInfo *scores)
8902 for (i = 0; i < scores->num_entries; i++)
8903 putFile16BitBE(file, scores->entry[i].score);
8906 static void SaveScore_TIME(FILE *file, struct ScoreInfo *scores)
8910 for (i = 0; i < scores->num_entries; i++)
8911 putFile32BitBE(file, scores->entry[i].time);
8914 static void SaveScore_TAPE(FILE *file, struct ScoreInfo *scores)
8918 for (i = 0; i < scores->num_entries; i++)
8920 int size = strlen(scores->entry[i].tape_basename);
8922 for (j = 0; j < MAX_SCORE_TAPE_BASENAME_LEN; j++)
8923 putFile8Bit(file, (j < size ? scores->entry[i].tape_basename[j] : 0));
8927 static void SaveScoreToFilename(char *filename)
8930 int info_chunk_size;
8931 int name_chunk_size;
8932 int scor_chunk_size;
8933 int time_chunk_size;
8934 int tape_chunk_size;
8936 if (!(file = fopen(filename, MODE_WRITE)))
8938 Warn("cannot save score file '%s'", filename);
8943 info_chunk_size = 2 + (strlen(scores.level_identifier) + 1) + 2 + 2;
8944 name_chunk_size = scores.num_entries * MAX_PLAYER_NAME_LEN;
8945 scor_chunk_size = scores.num_entries * 2;
8946 time_chunk_size = scores.num_entries * 4;
8947 tape_chunk_size = scores.num_entries * MAX_SCORE_TAPE_BASENAME_LEN;
8949 putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
8950 putFileChunkBE(file, "SCOR", CHUNK_SIZE_NONE);
8952 putFileChunkBE(file, "VERS", SCORE_CHUNK_VERS_SIZE);
8953 SaveScore_VERS(file, &scores);
8955 putFileChunkBE(file, "INFO", info_chunk_size);
8956 SaveScore_INFO(file, &scores);
8958 putFileChunkBE(file, "NAME", name_chunk_size);
8959 SaveScore_NAME(file, &scores);
8961 putFileChunkBE(file, "SCOR", scor_chunk_size);
8962 SaveScore_SCOR(file, &scores);
8964 putFileChunkBE(file, "TIME", time_chunk_size);
8965 SaveScore_TIME(file, &scores);
8967 putFileChunkBE(file, "TAPE", tape_chunk_size);
8968 SaveScore_TAPE(file, &scores);
8972 SetFilePermissions(filename, PERMS_PRIVATE);
8975 void SaveScore(int nr)
8977 char *filename = getScoreFilename(nr);
8980 // used instead of "leveldir_current->subdir" (for network games)
8981 InitScoreDirectory(levelset.identifier);
8983 scores.file_version = FILE_VERSION_ACTUAL;
8984 scores.game_version = GAME_VERSION_ACTUAL;
8986 strncpy(scores.level_identifier, levelset.identifier, MAX_FILENAME_LEN);
8987 scores.level_identifier[MAX_FILENAME_LEN] = '\0';
8988 scores.level_nr = level_nr;
8990 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
8991 if (scores.entry[i].score == 0 &&
8992 scores.entry[i].time == 0 &&
8993 strEqual(scores.entry[i].name, EMPTY_PLAYER_NAME))
8996 scores.num_entries = i;
8998 if (scores.num_entries == 0)
9001 SaveScoreToFilename(filename);
9004 static void ExecuteAsThread(SDL_ThreadFunction function, char *name, int data,
9007 static int data_static;
9011 SDL_Thread *thread = SDL_CreateThread(function, name, &data_static);
9014 SDL_DetachThread(thread);
9016 Error("Cannot create thread to %s!", error);
9018 // nasty kludge to lower probability of intermingled thread error messages
9022 static void DownloadServerScoreToCacheExt(struct HttpRequest *request,
9023 struct HttpResponse *response,
9026 request->hostname = setup.api_server_hostname;
9027 request->port = API_SERVER_PORT;
9028 request->method = API_SERVER_METHOD;
9029 request->uri = API_SERVER_URI_GET;
9031 snprintf(request->body, MAX_HTTP_BODY_SIZE,
9033 " \"levelset_identifier\": \"%s\",\n"
9034 " \"level_nr\": \"%d\",\n"
9035 " \"rate_time_over_score\": \"%d\"\n"
9037 levelset.identifier, nr, level.rate_time_over_score);
9039 if (!DoHttpRequest(request, response))
9041 Error("HTTP request failed: %s", GetHttpError());
9046 if (!HTTP_SUCCESS(response->status_code))
9048 Error("server failed to handle request: %d %s",
9049 response->status_code,
9050 response->status_text);
9055 if (response->body_size == 0)
9057 // no scores available for this level
9062 ConvertHttpResponseBodyToClientEncoding(response);
9064 char *filename = getScoreCacheFilename(nr);
9068 // used instead of "leveldir_current->subdir" (for network games)
9069 InitScoreCacheDirectory(levelset.identifier);
9071 if (!(file = fopen(filename, MODE_WRITE)))
9073 Warn("cannot save score cache file '%s'", filename);
9078 for (i = 0; i < response->body_size; i++)
9079 fputc(response->body[i], file);
9083 SetFilePermissions(filename, PERMS_PRIVATE);
9085 server_scores.updated = TRUE;
9088 static void DownloadServerScoreToCache(int nr)
9090 struct HttpRequest *request = checked_calloc(sizeof(struct HttpRequest));
9091 struct HttpResponse *response = checked_calloc(sizeof(struct HttpResponse));
9093 DownloadServerScoreToCacheExt(request, response, nr);
9095 checked_free(request);
9096 checked_free(response);
9099 static int DownloadServerScoreToCacheThread(void *data)
9101 DownloadServerScoreToCache(*(int *)data);
9106 static void DownloadServerScoreToCacheAsThread(int nr)
9108 ExecuteAsThread(DownloadServerScoreToCacheThread,
9109 "DownloadServerScoreToCache", nr,
9110 "download scores from server");
9113 static void LoadServerScoreFromCache(int nr)
9115 struct ScoreEntry score_entry;
9124 { &score_entry.score, FALSE, 0 },
9125 { &score_entry.time, FALSE, 0 },
9126 { score_entry.name, TRUE, MAX_PLAYER_NAME_LEN },
9127 { score_entry.tape_basename, TRUE, MAX_FILENAME_LEN },
9131 char *filename = getScoreCacheFilename(nr);
9132 SetupFileHash *score_hash = loadSetupFileHash(filename);
9135 server_scores.num_entries = 0;
9137 if (score_hash == NULL)
9140 for (i = 0; i < MAX_SCORE_ENTRIES; i++)
9142 score_entry = server_scores.entry[i];
9144 for (j = 0; score_mapping[j].value != NULL; j++)
9148 sprintf(token, "%02d.%d", i, j);
9150 char *value = getHashEntry(score_hash, token);
9155 if (score_mapping[j].is_string)
9157 char *score_value = (char *)score_mapping[j].value;
9158 int value_size = score_mapping[j].string_size;
9160 strncpy(score_value, value, value_size);
9161 score_value[value_size] = '\0';
9165 int *score_value = (int *)score_mapping[j].value;
9167 *score_value = atoi(value);
9170 server_scores.num_entries = i + 1;
9173 server_scores.entry[i] = score_entry;
9176 freeSetupFileHash(score_hash);
9179 void LoadServerScore(int nr, boolean download_score)
9181 if (!setup.api_server)
9184 // always start with reliable default values
9185 setServerScoreInfoToDefaults();
9187 // 1st step: load server scores from cache file (which may not exist)
9188 // (this should prevent reading it while the thread is writing to it)
9189 LoadServerScoreFromCache(nr);
9191 if (download_score && runtime.api_server)
9193 // 2nd step: download server scores from score server to cache file
9194 // (as thread, as it might time out if the server is not reachable)
9195 DownloadServerScoreToCacheAsThread(nr);
9199 static char *get_file_base64(char *filename)
9201 struct stat file_status;
9203 if (stat(filename, &file_status) != 0)
9205 Error("cannot stat file '%s'\n", filename);
9210 int buffer_size = file_status.st_size;
9211 byte *buffer = checked_malloc(buffer_size);
9215 if (!(file = fopen(filename, MODE_READ)))
9217 Error("cannot open file '%s'\n", filename);
9219 checked_free(buffer);
9224 for (i = 0; i < buffer_size; i++)
9226 int c = fgetc(file);
9230 Error("cannot read from input file '%s'\n", filename);
9233 checked_free(buffer);
9238 buffer[i] = (byte)c;
9243 int buffer_encoded_size = base64_encoded_size(buffer_size);
9244 char *buffer_encoded = checked_malloc(buffer_encoded_size);
9246 base64_encode(buffer_encoded, buffer, buffer_size);
9248 checked_free(buffer);
9250 return buffer_encoded;
9253 static void UploadScoreToServerExt(struct HttpRequest *request,
9254 struct HttpResponse *response,
9257 struct ScoreEntry *score_entry = &scores.entry[scores.last_added];
9259 request->hostname = setup.api_server_hostname;
9260 request->port = API_SERVER_PORT;
9261 request->method = API_SERVER_METHOD;
9262 request->uri = API_SERVER_URI_ADD;
9264 char *tape_filename = getScoreTapeFilename(score_entry->tape_basename, nr);
9265 char *tape_base64 = get_file_base64(tape_filename);
9267 if (tape_base64 == NULL)
9269 Error("loading and base64 encoding score tape file failed");
9274 snprintf(request->body, MAX_HTTP_BODY_SIZE,
9276 " \"game_version\": \"%s\",\n"
9277 " \"levelset_identifier\": \"%s\",\n"
9278 " \"levelset_name\": \"%s\",\n"
9279 " \"levelset_author\": \"%s\",\n"
9280 " \"levelset_num_levels\": \"%d\",\n"
9281 " \"levelset_first_level\": \"%d\",\n"
9282 " \"level_nr\": \"%d\",\n"
9283 " \"player_name\": \"%s\",\n"
9284 " \"score\": \"%d\",\n"
9285 " \"time\": \"%d\",\n"
9286 " \"tape_basename\": \"%s\",\n"
9287 " \"tape\": \"%s\"\n"
9289 getProgramRealVersionString(),
9290 leveldir_current->identifier,
9291 leveldir_current->name,
9292 leveldir_current->author,
9293 leveldir_current->levels,
9294 leveldir_current->first_level,
9299 score_entry->tape_basename,
9302 checked_free(tape_base64);
9304 ConvertHttpRequestBodyToServerEncoding(request);
9306 if (!DoHttpRequest(request, response))
9308 Error("HTTP request failed: %s", GetHttpError());
9313 if (!HTTP_SUCCESS(response->status_code))
9315 Error("server failed to handle request: %d %s",
9316 response->status_code,
9317 response->status_text);
9323 static void UploadScoreToServer(int nr)
9325 struct HttpRequest *request = checked_calloc(sizeof(struct HttpRequest));
9326 struct HttpResponse *response = checked_calloc(sizeof(struct HttpResponse));
9328 UploadScoreToServerExt(request, response, nr);
9330 checked_free(request);
9331 checked_free(response);
9334 static int UploadScoreToServerThread(void *data)
9336 UploadScoreToServer(*(int *)data);
9341 static void UploadScoreToServerAsThread(int nr)
9343 ExecuteAsThread(UploadScoreToServerThread,
9344 "UploadScoreToServer", nr,
9345 "upload score to server");
9348 void SaveServerScore(int nr)
9350 if (!runtime.api_server)
9353 UploadScoreToServerAsThread(nr);
9356 void LoadLocalAndServerScore(int nr, boolean download_score)
9358 int last_added_local = scores.last_added_local;
9362 // restore last added local score entry (before merging server scores)
9363 scores.last_added = scores.last_added_local = last_added_local;
9365 if (setup.api_server && !setup.only_show_local_scores)
9367 // load server scores from cache file and trigger update from server
9368 LoadServerScore(nr, download_score);
9370 // merge local scores with scores from server
9376 // ============================================================================
9377 // setup file functions
9378 // ============================================================================
9380 #define TOKEN_STR_PLAYER_PREFIX "player_"
9383 static struct TokenInfo global_setup_tokens[] =
9387 &setup.player_name, "player_name"
9391 &setup.multiple_users, "multiple_users"
9395 &setup.sound, "sound"
9399 &setup.sound_loops, "repeating_sound_loops"
9403 &setup.sound_music, "background_music"
9407 &setup.sound_simple, "simple_sound_effects"
9411 &setup.toons, "toons"
9415 &setup.scroll_delay, "scroll_delay"
9419 &setup.forced_scroll_delay, "forced_scroll_delay"
9423 &setup.scroll_delay_value, "scroll_delay_value"
9427 &setup.engine_snapshot_mode, "engine_snapshot_mode"
9431 &setup.engine_snapshot_memory, "engine_snapshot_memory"
9435 &setup.fade_screens, "fade_screens"
9439 &setup.autorecord, "automatic_tape_recording"
9443 &setup.show_titlescreen, "show_titlescreen"
9447 &setup.quick_doors, "quick_doors"
9451 &setup.team_mode, "team_mode"
9455 &setup.handicap, "handicap"
9459 &setup.skip_levels, "skip_levels"
9463 &setup.increment_levels, "increment_levels"
9467 &setup.auto_play_next_level, "auto_play_next_level"
9471 &setup.count_score_after_game, "count_score_after_game"
9475 &setup.show_scores_after_game, "show_scores_after_game"
9479 &setup.time_limit, "time_limit"
9483 &setup.fullscreen, "fullscreen"
9487 &setup.window_scaling_percent, "window_scaling_percent"
9491 &setup.window_scaling_quality, "window_scaling_quality"
9495 &setup.screen_rendering_mode, "screen_rendering_mode"
9499 &setup.vsync_mode, "vsync_mode"
9503 &setup.ask_on_escape, "ask_on_escape"
9507 &setup.ask_on_escape_editor, "ask_on_escape_editor"
9511 &setup.ask_on_game_over, "ask_on_game_over"
9515 &setup.ask_on_quit_game, "ask_on_quit_game"
9519 &setup.ask_on_quit_program, "ask_on_quit_program"
9523 &setup.quick_switch, "quick_player_switch"
9527 &setup.input_on_focus, "input_on_focus"
9531 &setup.prefer_aga_graphics, "prefer_aga_graphics"
9535 &setup.prefer_lowpass_sounds, "prefer_lowpass_sounds"
9539 &setup.prefer_extra_panel_items, "prefer_extra_panel_items"
9543 &setup.game_speed_extended, "game_speed_extended"
9547 &setup.game_frame_delay, "game_frame_delay"
9551 &setup.sp_show_border_elements, "sp_show_border_elements"
9555 &setup.small_game_graphics, "small_game_graphics"
9559 &setup.show_snapshot_buttons, "show_snapshot_buttons"
9563 &setup.only_show_local_scores, "only_show_local_scores"
9567 &setup.graphics_set, "graphics_set"
9571 &setup.sounds_set, "sounds_set"
9575 &setup.music_set, "music_set"
9579 &setup.override_level_graphics, "override_level_graphics"
9583 &setup.override_level_sounds, "override_level_sounds"
9587 &setup.override_level_music, "override_level_music"
9591 &setup.volume_simple, "volume_simple"
9595 &setup.volume_loops, "volume_loops"
9599 &setup.volume_music, "volume_music"
9603 &setup.network_mode, "network_mode"
9607 &setup.network_player_nr, "network_player"
9611 &setup.network_server_hostname, "network_server_hostname"
9615 &setup.api_server, "api_server"
9619 &setup.api_server_hostname, "api_server_hostname"
9623 &setup.touch.control_type, "touch.control_type"
9627 &setup.touch.move_distance, "touch.move_distance"
9631 &setup.touch.drop_distance, "touch.drop_distance"
9635 &setup.touch.transparency, "touch.transparency"
9639 &setup.touch.draw_outlined, "touch.draw_outlined"
9643 &setup.touch.draw_pressed, "touch.draw_pressed"
9647 &setup.touch.grid_xsize[0], "touch.virtual_buttons.0.xsize"
9651 &setup.touch.grid_ysize[0], "touch.virtual_buttons.0.ysize"
9655 &setup.touch.grid_xsize[1], "touch.virtual_buttons.1.xsize"
9659 &setup.touch.grid_ysize[1], "touch.virtual_buttons.1.ysize"
9663 static struct TokenInfo auto_setup_tokens[] =
9667 &setup.auto_setup.editor_zoom_tilesize, "editor.zoom_tilesize"
9671 static struct TokenInfo editor_setup_tokens[] =
9675 &setup.editor.el_classic, "editor.el_classic"
9679 &setup.editor.el_custom, "editor.el_custom"
9683 &setup.editor.el_user_defined, "editor.el_user_defined"
9687 &setup.editor.el_dynamic, "editor.el_dynamic"
9691 &setup.editor.el_headlines, "editor.el_headlines"
9695 &setup.editor.show_element_token, "editor.show_element_token"
9699 &setup.editor.show_read_only_warning, "editor.show_read_only_warning"
9703 static struct TokenInfo editor_cascade_setup_tokens[] =
9707 &setup.editor_cascade.el_bd, "editor.cascade.el_bd"
9711 &setup.editor_cascade.el_em, "editor.cascade.el_em"
9715 &setup.editor_cascade.el_emc, "editor.cascade.el_emc"
9719 &setup.editor_cascade.el_rnd, "editor.cascade.el_rnd"
9723 &setup.editor_cascade.el_sb, "editor.cascade.el_sb"
9727 &setup.editor_cascade.el_sp, "editor.cascade.el_sp"
9731 &setup.editor_cascade.el_dc, "editor.cascade.el_dc"
9735 &setup.editor_cascade.el_dx, "editor.cascade.el_dx"
9739 &setup.editor_cascade.el_mm, "editor.cascade.el_mm"
9743 &setup.editor_cascade.el_df, "editor.cascade.el_df"
9747 &setup.editor_cascade.el_chars, "editor.cascade.el_chars"
9751 &setup.editor_cascade.el_steel_chars, "editor.cascade.el_steel_chars"
9755 &setup.editor_cascade.el_ce, "editor.cascade.el_ce"
9759 &setup.editor_cascade.el_ge, "editor.cascade.el_ge"
9763 &setup.editor_cascade.el_ref, "editor.cascade.el_ref"
9767 &setup.editor_cascade.el_user, "editor.cascade.el_user"
9771 &setup.editor_cascade.el_dynamic, "editor.cascade.el_dynamic"
9775 static struct TokenInfo shortcut_setup_tokens[] =
9779 &setup.shortcut.save_game, "shortcut.save_game"
9783 &setup.shortcut.load_game, "shortcut.load_game"
9787 &setup.shortcut.toggle_pause, "shortcut.toggle_pause"
9791 &setup.shortcut.focus_player[0], "shortcut.focus_player_1"
9795 &setup.shortcut.focus_player[1], "shortcut.focus_player_2"
9799 &setup.shortcut.focus_player[2], "shortcut.focus_player_3"
9803 &setup.shortcut.focus_player[3], "shortcut.focus_player_4"
9807 &setup.shortcut.focus_player_all, "shortcut.focus_player_all"
9811 &setup.shortcut.tape_eject, "shortcut.tape_eject"
9815 &setup.shortcut.tape_extra, "shortcut.tape_extra"
9819 &setup.shortcut.tape_stop, "shortcut.tape_stop"
9823 &setup.shortcut.tape_pause, "shortcut.tape_pause"
9827 &setup.shortcut.tape_record, "shortcut.tape_record"
9831 &setup.shortcut.tape_play, "shortcut.tape_play"
9835 &setup.shortcut.sound_simple, "shortcut.sound_simple"
9839 &setup.shortcut.sound_loops, "shortcut.sound_loops"
9843 &setup.shortcut.sound_music, "shortcut.sound_music"
9847 &setup.shortcut.snap_left, "shortcut.snap_left"
9851 &setup.shortcut.snap_right, "shortcut.snap_right"
9855 &setup.shortcut.snap_up, "shortcut.snap_up"
9859 &setup.shortcut.snap_down, "shortcut.snap_down"
9863 static struct SetupInputInfo setup_input;
9864 static struct TokenInfo player_setup_tokens[] =
9868 &setup_input.use_joystick, ".use_joystick"
9872 &setup_input.joy.device_name, ".joy.device_name"
9876 &setup_input.joy.xleft, ".joy.xleft"
9880 &setup_input.joy.xmiddle, ".joy.xmiddle"
9884 &setup_input.joy.xright, ".joy.xright"
9888 &setup_input.joy.yupper, ".joy.yupper"
9892 &setup_input.joy.ymiddle, ".joy.ymiddle"
9896 &setup_input.joy.ylower, ".joy.ylower"
9900 &setup_input.joy.snap, ".joy.snap_field"
9904 &setup_input.joy.drop, ".joy.place_bomb"
9908 &setup_input.key.left, ".key.move_left"
9912 &setup_input.key.right, ".key.move_right"
9916 &setup_input.key.up, ".key.move_up"
9920 &setup_input.key.down, ".key.move_down"
9924 &setup_input.key.snap, ".key.snap_field"
9928 &setup_input.key.drop, ".key.place_bomb"
9932 static struct TokenInfo system_setup_tokens[] =
9936 &setup.system.sdl_renderdriver, "system.sdl_renderdriver"
9940 &setup.system.sdl_videodriver, "system.sdl_videodriver"
9944 &setup.system.sdl_audiodriver, "system.sdl_audiodriver"
9948 &setup.system.audio_fragment_size, "system.audio_fragment_size"
9952 static struct TokenInfo internal_setup_tokens[] =
9956 &setup.internal.program_title, "program_title"
9960 &setup.internal.program_version, "program_version"
9964 &setup.internal.program_author, "program_author"
9968 &setup.internal.program_email, "program_email"
9972 &setup.internal.program_website, "program_website"
9976 &setup.internal.program_copyright, "program_copyright"
9980 &setup.internal.program_company, "program_company"
9984 &setup.internal.program_icon_file, "program_icon_file"
9988 &setup.internal.default_graphics_set, "default_graphics_set"
9992 &setup.internal.default_sounds_set, "default_sounds_set"
9996 &setup.internal.default_music_set, "default_music_set"
10000 &setup.internal.fallback_graphics_file, "fallback_graphics_file"
10004 &setup.internal.fallback_sounds_file, "fallback_sounds_file"
10008 &setup.internal.fallback_music_file, "fallback_music_file"
10012 &setup.internal.default_level_series, "default_level_series"
10016 &setup.internal.default_window_width, "default_window_width"
10020 &setup.internal.default_window_height, "default_window_height"
10024 &setup.internal.choose_from_top_leveldir, "choose_from_top_leveldir"
10028 &setup.internal.show_scaling_in_title, "show_scaling_in_title"
10032 &setup.internal.create_user_levelset, "create_user_levelset"
10036 &setup.internal.menu_game, "menu_game"
10040 &setup.internal.menu_editor, "menu_editor"
10044 &setup.internal.menu_graphics, "menu_graphics"
10048 &setup.internal.menu_sound, "menu_sound"
10052 &setup.internal.menu_artwork, "menu_artwork"
10056 &setup.internal.menu_input, "menu_input"
10060 &setup.internal.menu_touch, "menu_touch"
10064 &setup.internal.menu_shortcuts, "menu_shortcuts"
10068 &setup.internal.menu_exit, "menu_exit"
10072 &setup.internal.menu_save_and_exit, "menu_save_and_exit"
10076 static struct TokenInfo debug_setup_tokens[] =
10080 &setup.debug.frame_delay[0], "debug.frame_delay_0"
10084 &setup.debug.frame_delay[1], "debug.frame_delay_1"
10088 &setup.debug.frame_delay[2], "debug.frame_delay_2"
10092 &setup.debug.frame_delay[3], "debug.frame_delay_3"
10096 &setup.debug.frame_delay[4], "debug.frame_delay_4"
10100 &setup.debug.frame_delay[5], "debug.frame_delay_5"
10104 &setup.debug.frame_delay[6], "debug.frame_delay_6"
10108 &setup.debug.frame_delay[7], "debug.frame_delay_7"
10112 &setup.debug.frame_delay[8], "debug.frame_delay_8"
10116 &setup.debug.frame_delay[9], "debug.frame_delay_9"
10120 &setup.debug.frame_delay_key[0], "debug.key.frame_delay_0"
10124 &setup.debug.frame_delay_key[1], "debug.key.frame_delay_1"
10128 &setup.debug.frame_delay_key[2], "debug.key.frame_delay_2"
10132 &setup.debug.frame_delay_key[3], "debug.key.frame_delay_3"
10136 &setup.debug.frame_delay_key[4], "debug.key.frame_delay_4"
10140 &setup.debug.frame_delay_key[5], "debug.key.frame_delay_5"
10144 &setup.debug.frame_delay_key[6], "debug.key.frame_delay_6"
10148 &setup.debug.frame_delay_key[7], "debug.key.frame_delay_7"
10152 &setup.debug.frame_delay_key[8], "debug.key.frame_delay_8"
10156 &setup.debug.frame_delay_key[9], "debug.key.frame_delay_9"
10160 &setup.debug.frame_delay_use_mod_key, "debug.frame_delay.use_mod_key"},
10163 &setup.debug.frame_delay_game_only, "debug.frame_delay.game_only"
10167 &setup.debug.show_frames_per_second, "debug.show_frames_per_second"
10171 &setup.debug.xsn_mode, "debug.xsn_mode"
10175 &setup.debug.xsn_percent, "debug.xsn_percent"
10179 static struct TokenInfo options_setup_tokens[] =
10183 &setup.options.verbose, "options.verbose"
10187 static void setSetupInfoToDefaults(struct SetupInfo *si)
10191 si->player_name = getStringCopy(getDefaultUserName(user.nr));
10193 si->multiple_users = TRUE;
10196 si->sound_loops = TRUE;
10197 si->sound_music = TRUE;
10198 si->sound_simple = TRUE;
10200 si->scroll_delay = TRUE;
10201 si->forced_scroll_delay = FALSE;
10202 si->scroll_delay_value = STD_SCROLL_DELAY;
10203 si->engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_DEFAULT);
10204 si->engine_snapshot_memory = SNAPSHOT_MEMORY_DEFAULT;
10205 si->fade_screens = TRUE;
10206 si->autorecord = TRUE;
10207 si->show_titlescreen = TRUE;
10208 si->quick_doors = FALSE;
10209 si->team_mode = FALSE;
10210 si->handicap = TRUE;
10211 si->skip_levels = TRUE;
10212 si->increment_levels = TRUE;
10213 si->auto_play_next_level = TRUE;
10214 si->count_score_after_game = TRUE;
10215 si->show_scores_after_game = TRUE;
10216 si->time_limit = TRUE;
10217 si->fullscreen = FALSE;
10218 si->window_scaling_percent = STD_WINDOW_SCALING_PERCENT;
10219 si->window_scaling_quality = getStringCopy(SCALING_QUALITY_DEFAULT);
10220 si->screen_rendering_mode = getStringCopy(STR_SPECIAL_RENDERING_DEFAULT);
10221 si->vsync_mode = getStringCopy(STR_VSYNC_MODE_DEFAULT);
10222 si->ask_on_escape = TRUE;
10223 si->ask_on_escape_editor = TRUE;
10224 si->ask_on_game_over = TRUE;
10225 si->ask_on_quit_game = TRUE;
10226 si->ask_on_quit_program = TRUE;
10227 si->quick_switch = FALSE;
10228 si->input_on_focus = FALSE;
10229 si->prefer_aga_graphics = TRUE;
10230 si->prefer_lowpass_sounds = FALSE;
10231 si->prefer_extra_panel_items = TRUE;
10232 si->game_speed_extended = FALSE;
10233 si->game_frame_delay = GAME_FRAME_DELAY;
10234 si->sp_show_border_elements = FALSE;
10235 si->small_game_graphics = FALSE;
10236 si->show_snapshot_buttons = FALSE;
10237 si->only_show_local_scores = FALSE;
10239 si->graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
10240 si->sounds_set = getStringCopy(SND_CLASSIC_SUBDIR);
10241 si->music_set = getStringCopy(MUS_CLASSIC_SUBDIR);
10243 si->override_level_graphics = FALSE;
10244 si->override_level_sounds = FALSE;
10245 si->override_level_music = FALSE;
10247 si->volume_simple = 100; // percent
10248 si->volume_loops = 100; // percent
10249 si->volume_music = 100; // percent
10251 si->network_mode = FALSE;
10252 si->network_player_nr = 0; // first player
10253 si->network_server_hostname = getStringCopy(STR_NETWORK_AUTO_DETECT);
10255 si->api_server = TRUE;
10256 si->api_server_hostname = getStringCopy(API_SERVER_HOSTNAME);
10258 si->touch.control_type = getStringCopy(TOUCH_CONTROL_DEFAULT);
10259 si->touch.move_distance = TOUCH_MOVE_DISTANCE_DEFAULT; // percent
10260 si->touch.drop_distance = TOUCH_DROP_DISTANCE_DEFAULT; // percent
10261 si->touch.transparency = TOUCH_TRANSPARENCY_DEFAULT; // percent
10262 si->touch.draw_outlined = TRUE;
10263 si->touch.draw_pressed = TRUE;
10265 for (i = 0; i < 2; i++)
10267 char *default_grid_button[6][2] =
10273 { "111222", " vv " },
10274 { "111222", " vv " }
10276 int grid_xsize = DEFAULT_GRID_XSIZE(i);
10277 int grid_ysize = DEFAULT_GRID_YSIZE(i);
10278 int min_xsize = MIN(6, grid_xsize);
10279 int min_ysize = MIN(6, grid_ysize);
10280 int startx = grid_xsize - min_xsize;
10281 int starty = grid_ysize - min_ysize;
10284 // virtual buttons grid can only be set to defaults if video is initialized
10285 // (this will be repeated if virtual buttons are not loaded from setup file)
10286 if (video.initialized)
10288 si->touch.grid_xsize[i] = grid_xsize;
10289 si->touch.grid_ysize[i] = grid_ysize;
10293 si->touch.grid_xsize[i] = -1;
10294 si->touch.grid_ysize[i] = -1;
10297 for (x = 0; x < MAX_GRID_XSIZE; x++)
10298 for (y = 0; y < MAX_GRID_YSIZE; y++)
10299 si->touch.grid_button[i][x][y] = CHAR_GRID_BUTTON_NONE;
10301 for (x = 0; x < min_xsize; x++)
10302 for (y = 0; y < min_ysize; y++)
10303 si->touch.grid_button[i][x][starty + y] =
10304 default_grid_button[y][0][x];
10306 for (x = 0; x < min_xsize; x++)
10307 for (y = 0; y < min_ysize; y++)
10308 si->touch.grid_button[i][startx + x][starty + y] =
10309 default_grid_button[y][1][x];
10312 si->touch.grid_initialized = video.initialized;
10314 si->editor.el_boulderdash = TRUE;
10315 si->editor.el_emerald_mine = TRUE;
10316 si->editor.el_emerald_mine_club = TRUE;
10317 si->editor.el_more = TRUE;
10318 si->editor.el_sokoban = TRUE;
10319 si->editor.el_supaplex = TRUE;
10320 si->editor.el_diamond_caves = TRUE;
10321 si->editor.el_dx_boulderdash = TRUE;
10323 si->editor.el_mirror_magic = TRUE;
10324 si->editor.el_deflektor = TRUE;
10326 si->editor.el_chars = TRUE;
10327 si->editor.el_steel_chars = TRUE;
10329 si->editor.el_classic = TRUE;
10330 si->editor.el_custom = TRUE;
10332 si->editor.el_user_defined = FALSE;
10333 si->editor.el_dynamic = TRUE;
10335 si->editor.el_headlines = TRUE;
10337 si->editor.show_element_token = FALSE;
10339 si->editor.show_read_only_warning = TRUE;
10341 si->editor.use_template_for_new_levels = TRUE;
10343 si->shortcut.save_game = DEFAULT_KEY_SAVE_GAME;
10344 si->shortcut.load_game = DEFAULT_KEY_LOAD_GAME;
10345 si->shortcut.toggle_pause = DEFAULT_KEY_TOGGLE_PAUSE;
10347 si->shortcut.focus_player[0] = DEFAULT_KEY_FOCUS_PLAYER_1;
10348 si->shortcut.focus_player[1] = DEFAULT_KEY_FOCUS_PLAYER_2;
10349 si->shortcut.focus_player[2] = DEFAULT_KEY_FOCUS_PLAYER_3;
10350 si->shortcut.focus_player[3] = DEFAULT_KEY_FOCUS_PLAYER_4;
10351 si->shortcut.focus_player_all = DEFAULT_KEY_FOCUS_PLAYER_ALL;
10353 si->shortcut.tape_eject = DEFAULT_KEY_TAPE_EJECT;
10354 si->shortcut.tape_extra = DEFAULT_KEY_TAPE_EXTRA;
10355 si->shortcut.tape_stop = DEFAULT_KEY_TAPE_STOP;
10356 si->shortcut.tape_pause = DEFAULT_KEY_TAPE_PAUSE;
10357 si->shortcut.tape_record = DEFAULT_KEY_TAPE_RECORD;
10358 si->shortcut.tape_play = DEFAULT_KEY_TAPE_PLAY;
10360 si->shortcut.sound_simple = DEFAULT_KEY_SOUND_SIMPLE;
10361 si->shortcut.sound_loops = DEFAULT_KEY_SOUND_LOOPS;
10362 si->shortcut.sound_music = DEFAULT_KEY_SOUND_MUSIC;
10364 si->shortcut.snap_left = DEFAULT_KEY_SNAP_LEFT;
10365 si->shortcut.snap_right = DEFAULT_KEY_SNAP_RIGHT;
10366 si->shortcut.snap_up = DEFAULT_KEY_SNAP_UP;
10367 si->shortcut.snap_down = DEFAULT_KEY_SNAP_DOWN;
10369 for (i = 0; i < MAX_PLAYERS; i++)
10371 si->input[i].use_joystick = FALSE;
10372 si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i));
10373 si->input[i].joy.xleft = JOYSTICK_XLEFT;
10374 si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE;
10375 si->input[i].joy.xright = JOYSTICK_XRIGHT;
10376 si->input[i].joy.yupper = JOYSTICK_YUPPER;
10377 si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE;
10378 si->input[i].joy.ylower = JOYSTICK_YLOWER;
10379 si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0);
10380 si->input[i].joy.drop = (i == 0 ? JOY_BUTTON_2 : 0);
10381 si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED);
10382 si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED);
10383 si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED);
10384 si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED);
10385 si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED);
10386 si->input[i].key.drop = (i == 0 ? DEFAULT_KEY_DROP : KSYM_UNDEFINED);
10389 si->system.sdl_renderdriver = getStringCopy(ARG_DEFAULT);
10390 si->system.sdl_videodriver = getStringCopy(ARG_DEFAULT);
10391 si->system.sdl_audiodriver = getStringCopy(ARG_DEFAULT);
10392 si->system.audio_fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
10394 si->internal.program_title = getStringCopy(PROGRAM_TITLE_STRING);
10395 si->internal.program_version = getStringCopy(getProgramRealVersionString());
10396 si->internal.program_author = getStringCopy(PROGRAM_AUTHOR_STRING);
10397 si->internal.program_email = getStringCopy(PROGRAM_EMAIL_STRING);
10398 si->internal.program_website = getStringCopy(PROGRAM_WEBSITE_STRING);
10399 si->internal.program_copyright = getStringCopy(PROGRAM_COPYRIGHT_STRING);
10400 si->internal.program_company = getStringCopy(PROGRAM_COMPANY_STRING);
10402 si->internal.program_icon_file = getStringCopy(PROGRAM_ICON_FILENAME);
10404 si->internal.default_graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
10405 si->internal.default_sounds_set = getStringCopy(SND_CLASSIC_SUBDIR);
10406 si->internal.default_music_set = getStringCopy(MUS_CLASSIC_SUBDIR);
10408 si->internal.fallback_graphics_file = getStringCopy(UNDEFINED_FILENAME);
10409 si->internal.fallback_sounds_file = getStringCopy(UNDEFINED_FILENAME);
10410 si->internal.fallback_music_file = getStringCopy(UNDEFINED_FILENAME);
10412 si->internal.default_level_series = getStringCopy(UNDEFINED_LEVELSET);
10413 si->internal.choose_from_top_leveldir = FALSE;
10414 si->internal.show_scaling_in_title = TRUE;
10415 si->internal.create_user_levelset = TRUE;
10417 si->internal.default_window_width = WIN_XSIZE_DEFAULT;
10418 si->internal.default_window_height = WIN_YSIZE_DEFAULT;
10420 si->debug.frame_delay[0] = DEFAULT_FRAME_DELAY_0;
10421 si->debug.frame_delay[1] = DEFAULT_FRAME_DELAY_1;
10422 si->debug.frame_delay[2] = DEFAULT_FRAME_DELAY_2;
10423 si->debug.frame_delay[3] = DEFAULT_FRAME_DELAY_3;
10424 si->debug.frame_delay[4] = DEFAULT_FRAME_DELAY_4;
10425 si->debug.frame_delay[5] = DEFAULT_FRAME_DELAY_5;
10426 si->debug.frame_delay[6] = DEFAULT_FRAME_DELAY_6;
10427 si->debug.frame_delay[7] = DEFAULT_FRAME_DELAY_7;
10428 si->debug.frame_delay[8] = DEFAULT_FRAME_DELAY_8;
10429 si->debug.frame_delay[9] = DEFAULT_FRAME_DELAY_9;
10431 si->debug.frame_delay_key[0] = DEFAULT_KEY_FRAME_DELAY_0;
10432 si->debug.frame_delay_key[1] = DEFAULT_KEY_FRAME_DELAY_1;
10433 si->debug.frame_delay_key[2] = DEFAULT_KEY_FRAME_DELAY_2;
10434 si->debug.frame_delay_key[3] = DEFAULT_KEY_FRAME_DELAY_3;
10435 si->debug.frame_delay_key[4] = DEFAULT_KEY_FRAME_DELAY_4;
10436 si->debug.frame_delay_key[5] = DEFAULT_KEY_FRAME_DELAY_5;
10437 si->debug.frame_delay_key[6] = DEFAULT_KEY_FRAME_DELAY_6;
10438 si->debug.frame_delay_key[7] = DEFAULT_KEY_FRAME_DELAY_7;
10439 si->debug.frame_delay_key[8] = DEFAULT_KEY_FRAME_DELAY_8;
10440 si->debug.frame_delay_key[9] = DEFAULT_KEY_FRAME_DELAY_9;
10442 si->debug.frame_delay_use_mod_key = DEFAULT_FRAME_DELAY_USE_MOD_KEY;
10443 si->debug.frame_delay_game_only = DEFAULT_FRAME_DELAY_GAME_ONLY;
10445 si->debug.show_frames_per_second = FALSE;
10447 si->debug.xsn_mode = AUTO;
10448 si->debug.xsn_percent = 0;
10450 si->options.verbose = FALSE;
10452 #if defined(PLATFORM_ANDROID)
10453 si->fullscreen = TRUE;
10456 setHideSetupEntry(&setup.debug.xsn_mode);
10459 static void setSetupInfoToDefaults_AutoSetup(struct SetupInfo *si)
10461 si->auto_setup.editor_zoom_tilesize = MINI_TILESIZE;
10464 static void setSetupInfoToDefaults_EditorCascade(struct SetupInfo *si)
10466 si->editor_cascade.el_bd = TRUE;
10467 si->editor_cascade.el_em = TRUE;
10468 si->editor_cascade.el_emc = TRUE;
10469 si->editor_cascade.el_rnd = TRUE;
10470 si->editor_cascade.el_sb = TRUE;
10471 si->editor_cascade.el_sp = TRUE;
10472 si->editor_cascade.el_dc = TRUE;
10473 si->editor_cascade.el_dx = TRUE;
10475 si->editor_cascade.el_mm = TRUE;
10476 si->editor_cascade.el_df = TRUE;
10478 si->editor_cascade.el_chars = FALSE;
10479 si->editor_cascade.el_steel_chars = FALSE;
10480 si->editor_cascade.el_ce = FALSE;
10481 si->editor_cascade.el_ge = FALSE;
10482 si->editor_cascade.el_ref = FALSE;
10483 si->editor_cascade.el_user = FALSE;
10484 si->editor_cascade.el_dynamic = FALSE;
10487 #define MAX_HIDE_SETUP_TOKEN_SIZE 20
10489 static char *getHideSetupToken(void *setup_value)
10491 static char hide_setup_token[MAX_HIDE_SETUP_TOKEN_SIZE];
10493 if (setup_value != NULL)
10494 snprintf(hide_setup_token, MAX_HIDE_SETUP_TOKEN_SIZE, "%p", setup_value);
10496 return hide_setup_token;
10499 void setHideSetupEntry(void *setup_value)
10501 char *hide_setup_token = getHideSetupToken(setup_value);
10503 if (hide_setup_hash == NULL)
10504 hide_setup_hash = newSetupFileHash();
10506 if (setup_value != NULL)
10507 setHashEntry(hide_setup_hash, hide_setup_token, "");
10510 void removeHideSetupEntry(void *setup_value)
10512 char *hide_setup_token = getHideSetupToken(setup_value);
10514 if (setup_value != NULL)
10515 removeHashEntry(hide_setup_hash, hide_setup_token);
10518 boolean hideSetupEntry(void *setup_value)
10520 char *hide_setup_token = getHideSetupToken(setup_value);
10522 return (setup_value != NULL &&
10523 getHashEntry(hide_setup_hash, hide_setup_token) != NULL);
10526 static void setSetupInfoFromTokenText(SetupFileHash *setup_file_hash,
10527 struct TokenInfo *token_info,
10528 int token_nr, char *token_text)
10530 char *token_hide_text = getStringCat2(token_text, ".hide");
10531 char *token_hide_value = getHashEntry(setup_file_hash, token_hide_text);
10533 // set the value of this setup option in the setup option structure
10534 setSetupInfo(token_info, token_nr, getHashEntry(setup_file_hash, token_text));
10536 // check if this setup option should be hidden in the setup menu
10537 if (token_hide_value != NULL && get_boolean_from_string(token_hide_value))
10538 setHideSetupEntry(token_info[token_nr].value);
10540 free(token_hide_text);
10543 static void setSetupInfoFromTokenInfo(SetupFileHash *setup_file_hash,
10544 struct TokenInfo *token_info,
10547 setSetupInfoFromTokenText(setup_file_hash, token_info, token_nr,
10548 token_info[token_nr].text);
10551 static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
10555 if (!setup_file_hash)
10558 for (i = 0; i < ARRAY_SIZE(global_setup_tokens); i++)
10559 setSetupInfoFromTokenInfo(setup_file_hash, global_setup_tokens, i);
10561 setup.touch.grid_initialized = TRUE;
10562 for (i = 0; i < 2; i++)
10564 int grid_xsize = setup.touch.grid_xsize[i];
10565 int grid_ysize = setup.touch.grid_ysize[i];
10568 // if virtual buttons are not loaded from setup file, repeat initializing
10569 // virtual buttons grid with default values later when video is initialized
10570 if (grid_xsize == -1 ||
10573 setup.touch.grid_initialized = FALSE;
10578 for (y = 0; y < grid_ysize; y++)
10580 char token_string[MAX_LINE_LEN];
10582 sprintf(token_string, "touch.virtual_buttons.%d.%02d", i, y);
10584 char *value_string = getHashEntry(setup_file_hash, token_string);
10586 if (value_string == NULL)
10589 for (x = 0; x < grid_xsize; x++)
10591 char c = value_string[x];
10593 setup.touch.grid_button[i][x][y] =
10594 (c == '.' ? CHAR_GRID_BUTTON_NONE : c);
10599 for (i = 0; i < ARRAY_SIZE(editor_setup_tokens); i++)
10600 setSetupInfoFromTokenInfo(setup_file_hash, editor_setup_tokens, i);
10602 for (i = 0; i < ARRAY_SIZE(shortcut_setup_tokens); i++)
10603 setSetupInfoFromTokenInfo(setup_file_hash, shortcut_setup_tokens, i);
10605 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
10609 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
10611 setup_input = setup.input[pnr];
10612 for (i = 0; i < ARRAY_SIZE(player_setup_tokens); i++)
10614 char full_token[100];
10616 sprintf(full_token, "%s%s", prefix, player_setup_tokens[i].text);
10617 setSetupInfoFromTokenText(setup_file_hash, player_setup_tokens, i,
10620 setup.input[pnr] = setup_input;
10623 for (i = 0; i < ARRAY_SIZE(system_setup_tokens); i++)
10624 setSetupInfoFromTokenInfo(setup_file_hash, system_setup_tokens, i);
10626 for (i = 0; i < ARRAY_SIZE(internal_setup_tokens); i++)
10627 setSetupInfoFromTokenInfo(setup_file_hash, internal_setup_tokens, i);
10629 for (i = 0; i < ARRAY_SIZE(debug_setup_tokens); i++)
10630 setSetupInfoFromTokenInfo(setup_file_hash, debug_setup_tokens, i);
10632 for (i = 0; i < ARRAY_SIZE(options_setup_tokens); i++)
10633 setSetupInfoFromTokenInfo(setup_file_hash, options_setup_tokens, i);
10635 setHideRelatedSetupEntries();
10638 static void decodeSetupFileHash_AutoSetup(SetupFileHash *setup_file_hash)
10642 if (!setup_file_hash)
10645 for (i = 0; i < ARRAY_SIZE(auto_setup_tokens); i++)
10646 setSetupInfo(auto_setup_tokens, i,
10647 getHashEntry(setup_file_hash,
10648 auto_setup_tokens[i].text));
10651 static void decodeSetupFileHash_EditorCascade(SetupFileHash *setup_file_hash)
10655 if (!setup_file_hash)
10658 for (i = 0; i < ARRAY_SIZE(editor_cascade_setup_tokens); i++)
10659 setSetupInfo(editor_cascade_setup_tokens, i,
10660 getHashEntry(setup_file_hash,
10661 editor_cascade_setup_tokens[i].text));
10664 void LoadUserNames(void)
10666 int last_user_nr = user.nr;
10669 if (global.user_names != NULL)
10671 for (i = 0; i < MAX_PLAYER_NAMES; i++)
10672 checked_free(global.user_names[i]);
10674 checked_free(global.user_names);
10677 global.user_names = checked_calloc(MAX_PLAYER_NAMES * sizeof(char *));
10679 for (i = 0; i < MAX_PLAYER_NAMES; i++)
10683 SetupFileHash *setup_file_hash = loadSetupFileHash(getSetupFilename());
10685 if (setup_file_hash)
10687 char *player_name = getHashEntry(setup_file_hash, "player_name");
10689 global.user_names[i] = getFixedUserName(player_name);
10691 freeSetupFileHash(setup_file_hash);
10694 if (global.user_names[i] == NULL)
10695 global.user_names[i] = getStringCopy(getDefaultUserName(i));
10698 user.nr = last_user_nr;
10701 void LoadSetupFromFilename(char *filename)
10703 SetupFileHash *setup_file_hash = loadSetupFileHash(filename);
10705 if (setup_file_hash)
10707 decodeSetupFileHash(setup_file_hash);
10709 freeSetupFileHash(setup_file_hash);
10713 Debug("setup", "using default setup values");
10717 static void LoadSetup_SpecialPostProcessing(void)
10719 char *player_name_new;
10721 // needed to work around problems with fixed length strings
10722 player_name_new = getFixedUserName(setup.player_name);
10723 free(setup.player_name);
10724 setup.player_name = player_name_new;
10726 // "scroll_delay: on(3) / off(0)" was replaced by scroll delay value
10727 if (setup.scroll_delay == FALSE)
10729 setup.scroll_delay_value = MIN_SCROLL_DELAY;
10730 setup.scroll_delay = TRUE; // now always "on"
10733 // make sure that scroll delay value stays inside valid range
10734 setup.scroll_delay_value =
10735 MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
10738 void LoadSetup(void)
10742 // always start with reliable default values
10743 setSetupInfoToDefaults(&setup);
10745 // try to load setup values from default setup file
10746 filename = getDefaultSetupFilename();
10748 if (fileExists(filename))
10749 LoadSetupFromFilename(filename);
10751 // try to load setup values from user setup file
10752 filename = getSetupFilename();
10754 LoadSetupFromFilename(filename);
10756 LoadSetup_SpecialPostProcessing();
10759 void LoadSetup_AutoSetup(void)
10761 char *filename = getPath2(getSetupDir(), AUTOSETUP_FILENAME);
10762 SetupFileHash *setup_file_hash = NULL;
10764 // always start with reliable default values
10765 setSetupInfoToDefaults_AutoSetup(&setup);
10767 setup_file_hash = loadSetupFileHash(filename);
10769 if (setup_file_hash)
10771 decodeSetupFileHash_AutoSetup(setup_file_hash);
10773 freeSetupFileHash(setup_file_hash);
10779 void LoadSetup_EditorCascade(void)
10781 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
10782 SetupFileHash *setup_file_hash = NULL;
10784 // always start with reliable default values
10785 setSetupInfoToDefaults_EditorCascade(&setup);
10787 setup_file_hash = loadSetupFileHash(filename);
10789 if (setup_file_hash)
10791 decodeSetupFileHash_EditorCascade(setup_file_hash);
10793 freeSetupFileHash(setup_file_hash);
10799 static void addGameControllerMappingToHash(SetupFileHash *mappings_hash,
10800 char *mapping_line)
10802 char mapping_guid[MAX_LINE_LEN];
10803 char *mapping_start, *mapping_end;
10805 // get GUID from game controller mapping line: copy complete line
10806 strncpy(mapping_guid, mapping_line, MAX_LINE_LEN - 1);
10807 mapping_guid[MAX_LINE_LEN - 1] = '\0';
10809 // get GUID from game controller mapping line: cut after GUID part
10810 mapping_start = strchr(mapping_guid, ',');
10811 if (mapping_start != NULL)
10812 *mapping_start = '\0';
10814 // cut newline from game controller mapping line
10815 mapping_end = strchr(mapping_line, '\n');
10816 if (mapping_end != NULL)
10817 *mapping_end = '\0';
10819 // add mapping entry to game controller mappings hash
10820 setHashEntry(mappings_hash, mapping_guid, mapping_line);
10823 static void LoadSetup_ReadGameControllerMappings(SetupFileHash *mappings_hash,
10828 if (!(file = fopen(filename, MODE_READ)))
10830 Warn("cannot read game controller mappings file '%s'", filename);
10835 while (!feof(file))
10837 char line[MAX_LINE_LEN];
10839 if (!fgets(line, MAX_LINE_LEN, file))
10842 addGameControllerMappingToHash(mappings_hash, line);
10848 void SaveSetup(void)
10850 char *filename = getSetupFilename();
10854 InitUserDataDirectory();
10856 if (!(file = fopen(filename, MODE_WRITE)))
10858 Warn("cannot write setup file '%s'", filename);
10863 fprintFileHeader(file, SETUP_FILENAME);
10865 for (i = 0; i < ARRAY_SIZE(global_setup_tokens); i++)
10867 // just to make things nicer :)
10868 if (global_setup_tokens[i].value == &setup.multiple_users ||
10869 global_setup_tokens[i].value == &setup.sound ||
10870 global_setup_tokens[i].value == &setup.graphics_set ||
10871 global_setup_tokens[i].value == &setup.volume_simple ||
10872 global_setup_tokens[i].value == &setup.network_mode ||
10873 global_setup_tokens[i].value == &setup.api_server ||
10874 global_setup_tokens[i].value == &setup.touch.control_type ||
10875 global_setup_tokens[i].value == &setup.touch.grid_xsize[0] ||
10876 global_setup_tokens[i].value == &setup.touch.grid_xsize[1])
10877 fprintf(file, "\n");
10879 fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i));
10882 for (i = 0; i < 2; i++)
10884 int grid_xsize = setup.touch.grid_xsize[i];
10885 int grid_ysize = setup.touch.grid_ysize[i];
10888 fprintf(file, "\n");
10890 for (y = 0; y < grid_ysize; y++)
10892 char token_string[MAX_LINE_LEN];
10893 char value_string[MAX_LINE_LEN];
10895 sprintf(token_string, "touch.virtual_buttons.%d.%02d", i, y);
10897 for (x = 0; x < grid_xsize; x++)
10899 char c = setup.touch.grid_button[i][x][y];
10901 value_string[x] = (c == CHAR_GRID_BUTTON_NONE ? '.' : c);
10904 value_string[grid_xsize] = '\0';
10906 fprintf(file, "%s\n", getFormattedSetupEntry(token_string, value_string));
10910 fprintf(file, "\n");
10911 for (i = 0; i < ARRAY_SIZE(editor_setup_tokens); i++)
10912 fprintf(file, "%s\n", getSetupLine(editor_setup_tokens, "", i));
10914 fprintf(file, "\n");
10915 for (i = 0; i < ARRAY_SIZE(shortcut_setup_tokens); i++)
10916 fprintf(file, "%s\n", getSetupLine(shortcut_setup_tokens, "", i));
10918 for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
10922 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
10923 fprintf(file, "\n");
10925 setup_input = setup.input[pnr];
10926 for (i = 0; i < ARRAY_SIZE(player_setup_tokens); i++)
10927 fprintf(file, "%s\n", getSetupLine(player_setup_tokens, prefix, i));
10930 fprintf(file, "\n");
10931 for (i = 0; i < ARRAY_SIZE(system_setup_tokens); i++)
10932 fprintf(file, "%s\n", getSetupLine(system_setup_tokens, "", i));
10934 // (internal setup values not saved to user setup file)
10936 fprintf(file, "\n");
10937 for (i = 0; i < ARRAY_SIZE(debug_setup_tokens); i++)
10938 if (!strPrefix(debug_setup_tokens[i].text, "debug.xsn_") ||
10939 setup.debug.xsn_mode != AUTO)
10940 fprintf(file, "%s\n", getSetupLine(debug_setup_tokens, "", i));
10942 fprintf(file, "\n");
10943 for (i = 0; i < ARRAY_SIZE(options_setup_tokens); i++)
10944 fprintf(file, "%s\n", getSetupLine(options_setup_tokens, "", i));
10948 SetFilePermissions(filename, PERMS_PRIVATE);
10951 void SaveSetup_AutoSetup(void)
10953 char *filename = getPath2(getSetupDir(), AUTOSETUP_FILENAME);
10957 InitUserDataDirectory();
10959 if (!(file = fopen(filename, MODE_WRITE)))
10961 Warn("cannot write auto setup file '%s'", filename);
10968 fprintFileHeader(file, AUTOSETUP_FILENAME);
10970 for (i = 0; i < ARRAY_SIZE(auto_setup_tokens); i++)
10971 fprintf(file, "%s\n", getSetupLine(auto_setup_tokens, "", i));
10975 SetFilePermissions(filename, PERMS_PRIVATE);
10980 void SaveSetup_EditorCascade(void)
10982 char *filename = getPath2(getSetupDir(), EDITORCASCADE_FILENAME);
10986 InitUserDataDirectory();
10988 if (!(file = fopen(filename, MODE_WRITE)))
10990 Warn("cannot write editor cascade state file '%s'", filename);
10997 fprintFileHeader(file, EDITORCASCADE_FILENAME);
10999 for (i = 0; i < ARRAY_SIZE(editor_cascade_setup_tokens); i++)
11000 fprintf(file, "%s\n", getSetupLine(editor_cascade_setup_tokens, "", i));
11004 SetFilePermissions(filename, PERMS_PRIVATE);
11009 static void SaveSetup_WriteGameControllerMappings(SetupFileHash *mappings_hash,
11014 if (!(file = fopen(filename, MODE_WRITE)))
11016 Warn("cannot write game controller mappings file '%s'", filename);
11021 BEGIN_HASH_ITERATION(mappings_hash, itr)
11023 fprintf(file, "%s\n", HASH_ITERATION_VALUE(itr));
11025 END_HASH_ITERATION(mappings_hash, itr)
11030 void SaveSetup_AddGameControllerMapping(char *mapping)
11032 char *filename = getPath2(getSetupDir(), GAMECONTROLLER_BASENAME);
11033 SetupFileHash *mappings_hash = newSetupFileHash();
11035 InitUserDataDirectory();
11037 // load existing personal game controller mappings
11038 LoadSetup_ReadGameControllerMappings(mappings_hash, filename);
11040 // add new mapping to personal game controller mappings
11041 addGameControllerMappingToHash(mappings_hash, mapping);
11043 // save updated personal game controller mappings
11044 SaveSetup_WriteGameControllerMappings(mappings_hash, filename);
11046 freeSetupFileHash(mappings_hash);
11050 void LoadCustomElementDescriptions(void)
11052 char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
11053 SetupFileHash *setup_file_hash;
11056 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
11058 if (element_info[i].custom_description != NULL)
11060 free(element_info[i].custom_description);
11061 element_info[i].custom_description = NULL;
11065 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
11068 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
11070 char *token = getStringCat2(element_info[i].token_name, ".name");
11071 char *value = getHashEntry(setup_file_hash, token);
11074 element_info[i].custom_description = getStringCopy(value);
11079 freeSetupFileHash(setup_file_hash);
11082 static int getElementFromToken(char *token)
11084 char *value = getHashEntry(element_token_hash, token);
11087 return atoi(value);
11089 Warn("unknown element token '%s'", token);
11091 return EL_UNDEFINED;
11094 void FreeGlobalAnimEventInfo(void)
11096 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
11098 if (gaei->event_list == NULL)
11103 for (i = 0; i < gaei->num_event_lists; i++)
11105 checked_free(gaei->event_list[i]->event_value);
11106 checked_free(gaei->event_list[i]);
11109 checked_free(gaei->event_list);
11111 gaei->event_list = NULL;
11112 gaei->num_event_lists = 0;
11115 static int AddGlobalAnimEventList(void)
11117 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
11118 int list_pos = gaei->num_event_lists++;
11120 gaei->event_list = checked_realloc(gaei->event_list, gaei->num_event_lists *
11121 sizeof(struct GlobalAnimEventListInfo *));
11123 gaei->event_list[list_pos] =
11124 checked_calloc(sizeof(struct GlobalAnimEventListInfo));
11126 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
11128 gaeli->event_value = NULL;
11129 gaeli->num_event_values = 0;
11134 static int AddGlobalAnimEventValue(int list_pos, int event_value)
11136 // do not add empty global animation events
11137 if (event_value == ANIM_EVENT_NONE)
11140 // if list position is undefined, create new list
11141 if (list_pos == ANIM_EVENT_UNDEFINED)
11142 list_pos = AddGlobalAnimEventList();
11144 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
11145 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
11146 int value_pos = gaeli->num_event_values++;
11148 gaeli->event_value = checked_realloc(gaeli->event_value,
11149 gaeli->num_event_values * sizeof(int *));
11151 gaeli->event_value[value_pos] = event_value;
11156 int GetGlobalAnimEventValue(int list_pos, int value_pos)
11158 if (list_pos == ANIM_EVENT_UNDEFINED)
11159 return ANIM_EVENT_NONE;
11161 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
11162 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
11164 return gaeli->event_value[value_pos];
11167 int GetGlobalAnimEventValueCount(int list_pos)
11169 if (list_pos == ANIM_EVENT_UNDEFINED)
11172 struct GlobalAnimEventInfo *gaei = &global_anim_event_info;
11173 struct GlobalAnimEventListInfo *gaeli = gaei->event_list[list_pos];
11175 return gaeli->num_event_values;
11178 // This function checks if a string <s> of the format "string1, string2, ..."
11179 // exactly contains a string <s_contained>.
11181 static boolean string_has_parameter(char *s, char *s_contained)
11185 if (s == NULL || s_contained == NULL)
11188 if (strlen(s_contained) > strlen(s))
11191 if (strncmp(s, s_contained, strlen(s_contained)) == 0)
11193 char next_char = s[strlen(s_contained)];
11195 // check if next character is delimiter or whitespace
11196 return (next_char == ',' || next_char == '\0' ||
11197 next_char == ' ' || next_char == '\t' ? TRUE : FALSE);
11200 // check if string contains another parameter string after a comma
11201 substring = strchr(s, ',');
11202 if (substring == NULL) // string does not contain a comma
11205 // advance string pointer to next character after the comma
11208 // skip potential whitespaces after the comma
11209 while (*substring == ' ' || *substring == '\t')
11212 return string_has_parameter(substring, s_contained);
11215 static int get_anim_parameter_value(char *s)
11217 int event_value[] =
11225 char *pattern_1[] =
11233 char *pattern_2 = ".part_";
11234 char *matching_char = NULL;
11236 int pattern_1_len = 0;
11237 int result = ANIM_EVENT_NONE;
11240 for (i = 0; i < ARRAY_SIZE(event_value); i++)
11242 matching_char = strstr(s_ptr, pattern_1[i]);
11243 pattern_1_len = strlen(pattern_1[i]);
11244 result = event_value[i];
11246 if (matching_char != NULL)
11250 if (matching_char == NULL)
11251 return ANIM_EVENT_NONE;
11253 s_ptr = matching_char + pattern_1_len;
11255 // check for main animation number ("anim_X" or "anim_XX")
11256 if (*s_ptr >= '0' && *s_ptr <= '9')
11258 int gic_anim_nr = (*s_ptr++ - '0');
11260 if (*s_ptr >= '0' && *s_ptr <= '9')
11261 gic_anim_nr = 10 * gic_anim_nr + (*s_ptr++ - '0');
11263 if (gic_anim_nr < 1 || gic_anim_nr > MAX_GLOBAL_ANIMS)
11264 return ANIM_EVENT_NONE;
11266 result |= gic_anim_nr << ANIM_EVENT_ANIM_BIT;
11270 // invalid main animation number specified
11272 return ANIM_EVENT_NONE;
11275 // check for animation part number ("part_X" or "part_XX") (optional)
11276 if (strPrefix(s_ptr, pattern_2))
11278 s_ptr += strlen(pattern_2);
11280 if (*s_ptr >= '0' && *s_ptr <= '9')
11282 int gic_part_nr = (*s_ptr++ - '0');
11284 if (*s_ptr >= '0' && *s_ptr <= '9')
11285 gic_part_nr = 10 * gic_part_nr + (*s_ptr++ - '0');
11287 if (gic_part_nr < 1 || gic_part_nr > MAX_GLOBAL_ANIM_PARTS)
11288 return ANIM_EVENT_NONE;
11290 result |= gic_part_nr << ANIM_EVENT_PART_BIT;
11294 // invalid animation part number specified
11296 return ANIM_EVENT_NONE;
11300 // discard result if next character is neither delimiter nor whitespace
11301 if (!(*s_ptr == ',' || *s_ptr == '\0' ||
11302 *s_ptr == ' ' || *s_ptr == '\t'))
11303 return ANIM_EVENT_NONE;
11308 static int get_anim_parameter_values(char *s)
11310 int list_pos = ANIM_EVENT_UNDEFINED;
11311 int event_value = ANIM_EVENT_DEFAULT;
11313 if (string_has_parameter(s, "any"))
11314 event_value |= ANIM_EVENT_ANY;
11316 if (string_has_parameter(s, "click:self") ||
11317 string_has_parameter(s, "click") ||
11318 string_has_parameter(s, "self"))
11319 event_value |= ANIM_EVENT_SELF;
11321 if (string_has_parameter(s, "unclick:any"))
11322 event_value |= ANIM_EVENT_UNCLICK_ANY;
11324 // if animation event found, add it to global animation event list
11325 if (event_value != ANIM_EVENT_NONE)
11326 list_pos = AddGlobalAnimEventValue(list_pos, event_value);
11330 // add optional "click:anim_X" or "click:anim_X.part_X" parameter
11331 event_value = get_anim_parameter_value(s);
11333 // if animation event found, add it to global animation event list
11334 if (event_value != ANIM_EVENT_NONE)
11335 list_pos = AddGlobalAnimEventValue(list_pos, event_value);
11337 // continue with next part of the string, starting with next comma
11338 s = strchr(s + 1, ',');
11344 static int get_anim_action_parameter_value(char *token)
11346 // check most common default case first to massively speed things up
11347 if (strEqual(token, ARG_UNDEFINED))
11348 return ANIM_EVENT_ACTION_NONE;
11350 int result = getImageIDFromToken(token);
11354 char *gfx_token = getStringCat2("gfx.", token);
11356 result = getImageIDFromToken(gfx_token);
11358 checked_free(gfx_token);
11363 Key key = getKeyFromX11KeyName(token);
11365 if (key != KSYM_UNDEFINED)
11366 result = -(int)key;
11370 result = ANIM_EVENT_ACTION_NONE;
11375 int get_parameter_value(char *value_raw, char *suffix, int type)
11377 char *value = getStringToLower(value_raw);
11378 int result = 0; // probably a save default value
11380 if (strEqual(suffix, ".direction"))
11382 result = (strEqual(value, "left") ? MV_LEFT :
11383 strEqual(value, "right") ? MV_RIGHT :
11384 strEqual(value, "up") ? MV_UP :
11385 strEqual(value, "down") ? MV_DOWN : MV_NONE);
11387 else if (strEqual(suffix, ".position"))
11389 result = (strEqual(value, "left") ? POS_LEFT :
11390 strEqual(value, "right") ? POS_RIGHT :
11391 strEqual(value, "top") ? POS_TOP :
11392 strEqual(value, "upper") ? POS_UPPER :
11393 strEqual(value, "middle") ? POS_MIDDLE :
11394 strEqual(value, "lower") ? POS_LOWER :
11395 strEqual(value, "bottom") ? POS_BOTTOM :
11396 strEqual(value, "any") ? POS_ANY :
11397 strEqual(value, "last") ? POS_LAST : POS_UNDEFINED);
11399 else if (strEqual(suffix, ".align"))
11401 result = (strEqual(value, "left") ? ALIGN_LEFT :
11402 strEqual(value, "right") ? ALIGN_RIGHT :
11403 strEqual(value, "center") ? ALIGN_CENTER :
11404 strEqual(value, "middle") ? ALIGN_CENTER : ALIGN_DEFAULT);
11406 else if (strEqual(suffix, ".valign"))
11408 result = (strEqual(value, "top") ? VALIGN_TOP :
11409 strEqual(value, "bottom") ? VALIGN_BOTTOM :
11410 strEqual(value, "middle") ? VALIGN_MIDDLE :
11411 strEqual(value, "center") ? VALIGN_MIDDLE : VALIGN_DEFAULT);
11413 else if (strEqual(suffix, ".anim_mode"))
11415 result = (string_has_parameter(value, "none") ? ANIM_NONE :
11416 string_has_parameter(value, "loop") ? ANIM_LOOP :
11417 string_has_parameter(value, "linear") ? ANIM_LINEAR :
11418 string_has_parameter(value, "pingpong") ? ANIM_PINGPONG :
11419 string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 :
11420 string_has_parameter(value, "random") ? ANIM_RANDOM :
11421 string_has_parameter(value, "ce_value") ? ANIM_CE_VALUE :
11422 string_has_parameter(value, "ce_score") ? ANIM_CE_SCORE :
11423 string_has_parameter(value, "ce_delay") ? ANIM_CE_DELAY :
11424 string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL :
11425 string_has_parameter(value, "vertical") ? ANIM_VERTICAL :
11426 string_has_parameter(value, "centered") ? ANIM_CENTERED :
11427 string_has_parameter(value, "all") ? ANIM_ALL :
11430 if (string_has_parameter(value, "once"))
11431 result |= ANIM_ONCE;
11433 if (string_has_parameter(value, "reverse"))
11434 result |= ANIM_REVERSE;
11436 if (string_has_parameter(value, "opaque_player"))
11437 result |= ANIM_OPAQUE_PLAYER;
11439 if (string_has_parameter(value, "static_panel"))
11440 result |= ANIM_STATIC_PANEL;
11442 else if (strEqual(suffix, ".init_event") ||
11443 strEqual(suffix, ".anim_event"))
11445 result = get_anim_parameter_values(value);
11447 else if (strEqual(suffix, ".init_delay_action") ||
11448 strEqual(suffix, ".anim_delay_action") ||
11449 strEqual(suffix, ".post_delay_action") ||
11450 strEqual(suffix, ".init_event_action") ||
11451 strEqual(suffix, ".anim_event_action"))
11453 result = get_anim_action_parameter_value(value_raw);
11455 else if (strEqual(suffix, ".class"))
11457 result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
11458 get_hash_from_key(value));
11460 else if (strEqual(suffix, ".style"))
11462 result = STYLE_DEFAULT;
11464 if (string_has_parameter(value, "accurate_borders"))
11465 result |= STYLE_ACCURATE_BORDERS;
11467 if (string_has_parameter(value, "inner_corners"))
11468 result |= STYLE_INNER_CORNERS;
11470 if (string_has_parameter(value, "reverse"))
11471 result |= STYLE_REVERSE;
11473 if (string_has_parameter(value, "leftmost_position"))
11474 result |= STYLE_LEFTMOST_POSITION;
11476 if (string_has_parameter(value, "block_clicks"))
11477 result |= STYLE_BLOCK;
11479 if (string_has_parameter(value, "passthrough_clicks"))
11480 result |= STYLE_PASSTHROUGH;
11482 if (string_has_parameter(value, "multiple_actions"))
11483 result |= STYLE_MULTIPLE_ACTIONS;
11485 else if (strEqual(suffix, ".fade_mode"))
11487 result = (string_has_parameter(value, "none") ? FADE_MODE_NONE :
11488 string_has_parameter(value, "fade") ? FADE_MODE_FADE :
11489 string_has_parameter(value, "crossfade") ? FADE_MODE_CROSSFADE :
11490 string_has_parameter(value, "melt") ? FADE_MODE_MELT :
11491 string_has_parameter(value, "curtain") ? FADE_MODE_CURTAIN :
11492 FADE_MODE_DEFAULT);
11494 else if (strEqual(suffix, ".auto_delay_unit"))
11496 result = (string_has_parameter(value, "ms") ? AUTO_DELAY_UNIT_MS :
11497 string_has_parameter(value, "frames") ? AUTO_DELAY_UNIT_FRAMES :
11498 AUTO_DELAY_UNIT_DEFAULT);
11500 else if (strPrefix(suffix, ".font")) // (may also be ".font_xyz")
11502 result = gfx.get_font_from_token_function(value);
11504 else // generic parameter of type integer or boolean
11506 result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
11507 type == TYPE_INTEGER ? get_integer_from_string(value) :
11508 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
11509 ARG_UNDEFINED_VALUE);
11517 static int get_token_parameter_value(char *token, char *value_raw)
11521 if (token == NULL || value_raw == NULL)
11522 return ARG_UNDEFINED_VALUE;
11524 suffix = strrchr(token, '.');
11525 if (suffix == NULL)
11528 if (strEqual(suffix, ".element"))
11529 return getElementFromToken(value_raw);
11531 // !!! USE CORRECT VALUE TYPE (currently works also for TYPE_BOOLEAN) !!!
11532 return get_parameter_value(value_raw, suffix, TYPE_INTEGER);
11535 void InitMenuDesignSettings_Static(void)
11539 // always start with reliable default values from static default config
11540 for (i = 0; image_config_vars[i].token != NULL; i++)
11542 char *value = getHashEntry(image_config_hash, image_config_vars[i].token);
11545 *image_config_vars[i].value =
11546 get_token_parameter_value(image_config_vars[i].token, value);
11550 static void InitMenuDesignSettings_SpecialPreProcessing(void)
11554 // the following initializes hierarchical values from static configuration
11556 // special case: initialize "ARG_DEFAULT" values in static default config
11557 // (e.g., initialize "[titlemessage].fade_mode" from "[title].fade_mode")
11558 titlescreen_initial_first_default.fade_mode =
11559 title_initial_first_default.fade_mode;
11560 titlescreen_initial_first_default.fade_delay =
11561 title_initial_first_default.fade_delay;
11562 titlescreen_initial_first_default.post_delay =
11563 title_initial_first_default.post_delay;
11564 titlescreen_initial_first_default.auto_delay =
11565 title_initial_first_default.auto_delay;
11566 titlescreen_initial_first_default.auto_delay_unit =
11567 title_initial_first_default.auto_delay_unit;
11568 titlescreen_first_default.fade_mode = title_first_default.fade_mode;
11569 titlescreen_first_default.fade_delay = title_first_default.fade_delay;
11570 titlescreen_first_default.post_delay = title_first_default.post_delay;
11571 titlescreen_first_default.auto_delay = title_first_default.auto_delay;
11572 titlescreen_first_default.auto_delay_unit =
11573 title_first_default.auto_delay_unit;
11574 titlemessage_initial_first_default.fade_mode =
11575 title_initial_first_default.fade_mode;
11576 titlemessage_initial_first_default.fade_delay =
11577 title_initial_first_default.fade_delay;
11578 titlemessage_initial_first_default.post_delay =
11579 title_initial_first_default.post_delay;
11580 titlemessage_initial_first_default.auto_delay =
11581 title_initial_first_default.auto_delay;
11582 titlemessage_initial_first_default.auto_delay_unit =
11583 title_initial_first_default.auto_delay_unit;
11584 titlemessage_first_default.fade_mode = title_first_default.fade_mode;
11585 titlemessage_first_default.fade_delay = title_first_default.fade_delay;
11586 titlemessage_first_default.post_delay = title_first_default.post_delay;
11587 titlemessage_first_default.auto_delay = title_first_default.auto_delay;
11588 titlemessage_first_default.auto_delay_unit =
11589 title_first_default.auto_delay_unit;
11591 titlescreen_initial_default.fade_mode = title_initial_default.fade_mode;
11592 titlescreen_initial_default.fade_delay = title_initial_default.fade_delay;
11593 titlescreen_initial_default.post_delay = title_initial_default.post_delay;
11594 titlescreen_initial_default.auto_delay = title_initial_default.auto_delay;
11595 titlescreen_initial_default.auto_delay_unit =
11596 title_initial_default.auto_delay_unit;
11597 titlescreen_default.fade_mode = title_default.fade_mode;
11598 titlescreen_default.fade_delay = title_default.fade_delay;
11599 titlescreen_default.post_delay = title_default.post_delay;
11600 titlescreen_default.auto_delay = title_default.auto_delay;
11601 titlescreen_default.auto_delay_unit = title_default.auto_delay_unit;
11602 titlemessage_initial_default.fade_mode = title_initial_default.fade_mode;
11603 titlemessage_initial_default.fade_delay = title_initial_default.fade_delay;
11604 titlemessage_initial_default.post_delay = title_initial_default.post_delay;
11605 titlemessage_initial_default.auto_delay_unit =
11606 title_initial_default.auto_delay_unit;
11607 titlemessage_default.fade_mode = title_default.fade_mode;
11608 titlemessage_default.fade_delay = title_default.fade_delay;
11609 titlemessage_default.post_delay = title_default.post_delay;
11610 titlemessage_default.auto_delay = title_default.auto_delay;
11611 titlemessage_default.auto_delay_unit = title_default.auto_delay_unit;
11613 // special case: initialize "ARG_DEFAULT" values in static default config
11614 // (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode")
11615 for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
11617 titlescreen_initial_first[i] = titlescreen_initial_first_default;
11618 titlescreen_first[i] = titlescreen_first_default;
11619 titlemessage_initial_first[i] = titlemessage_initial_first_default;
11620 titlemessage_first[i] = titlemessage_first_default;
11622 titlescreen_initial[i] = titlescreen_initial_default;
11623 titlescreen[i] = titlescreen_default;
11624 titlemessage_initial[i] = titlemessage_initial_default;
11625 titlemessage[i] = titlemessage_default;
11628 // special case: initialize "ARG_DEFAULT" values in static default config
11629 // (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz")
11630 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
11632 if (i == GFX_SPECIAL_ARG_TITLE) // title values already initialized
11635 menu.enter_screen[i] = menu.enter_screen[GFX_SPECIAL_ARG_DEFAULT];
11636 menu.leave_screen[i] = menu.leave_screen[GFX_SPECIAL_ARG_DEFAULT];
11637 menu.next_screen[i] = menu.next_screen[GFX_SPECIAL_ARG_DEFAULT];
11640 // special case: initialize "ARG_DEFAULT" values in static default config
11641 // (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz")
11642 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
11644 viewport.window[i] = viewport.window[GFX_SPECIAL_ARG_DEFAULT];
11645 viewport.playfield[i] = viewport.playfield[GFX_SPECIAL_ARG_DEFAULT];
11646 viewport.door_1[i] = viewport.door_1[GFX_SPECIAL_ARG_DEFAULT];
11648 if (i == GFX_SPECIAL_ARG_EDITOR) // editor values already initialized
11651 viewport.door_2[i] = viewport.door_2[GFX_SPECIAL_ARG_DEFAULT];
11655 static void InitMenuDesignSettings_SpecialPostProcessing(void)
11659 struct XY *dst, *src;
11661 game_buttons_xy[] =
11663 { &game.button.save, &game.button.stop },
11664 { &game.button.pause2, &game.button.pause },
11665 { &game.button.load, &game.button.play },
11666 { &game.button.undo, &game.button.stop },
11667 { &game.button.redo, &game.button.play },
11673 // special case: initialize later added SETUP list size from LEVELS value
11674 if (menu.list_size[GAME_MODE_SETUP] == -1)
11675 menu.list_size[GAME_MODE_SETUP] = menu.list_size[GAME_MODE_LEVELS];
11677 // set default position for snapshot buttons to stop/pause/play buttons
11678 for (i = 0; game_buttons_xy[i].dst != NULL; i++)
11679 if ((*game_buttons_xy[i].dst).x == -1 &&
11680 (*game_buttons_xy[i].dst).y == -1)
11681 *game_buttons_xy[i].dst = *game_buttons_xy[i].src;
11683 // --------------------------------------------------------------------------
11684 // dynamic viewports (including playfield margins, borders and alignments)
11685 // --------------------------------------------------------------------------
11687 // dynamic viewports currently only supported for landscape mode
11688 int display_width = MAX(video.display_width, video.display_height);
11689 int display_height = MIN(video.display_width, video.display_height);
11691 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
11693 struct RectWithBorder *vp_window = &viewport.window[i];
11694 struct RectWithBorder *vp_playfield = &viewport.playfield[i];
11695 struct RectWithBorder *vp_door_1 = &viewport.door_1[i];
11696 struct RectWithBorder *vp_door_2 = &viewport.door_2[i];
11697 boolean dynamic_window_width = (vp_window->min_width != -1);
11698 boolean dynamic_window_height = (vp_window->min_height != -1);
11699 boolean dynamic_playfield_width = (vp_playfield->min_width != -1);
11700 boolean dynamic_playfield_height = (vp_playfield->min_height != -1);
11702 // adjust window size if min/max width/height is specified
11704 if (vp_window->min_width != -1)
11706 int window_width = display_width;
11708 // when using static window height, use aspect ratio of display
11709 if (vp_window->min_height == -1)
11710 window_width = vp_window->height * display_width / display_height;
11712 vp_window->width = MAX(vp_window->min_width, window_width);
11715 if (vp_window->min_height != -1)
11717 int window_height = display_height;
11719 // when using static window width, use aspect ratio of display
11720 if (vp_window->min_width == -1)
11721 window_height = vp_window->width * display_height / display_width;
11723 vp_window->height = MAX(vp_window->min_height, window_height);
11726 if (vp_window->max_width != -1)
11727 vp_window->width = MIN(vp_window->width, vp_window->max_width);
11729 if (vp_window->max_height != -1)
11730 vp_window->height = MIN(vp_window->height, vp_window->max_height);
11732 int playfield_width = vp_window->width;
11733 int playfield_height = vp_window->height;
11735 // adjust playfield size and position according to specified margins
11737 playfield_width -= vp_playfield->margin_left;
11738 playfield_width -= vp_playfield->margin_right;
11740 playfield_height -= vp_playfield->margin_top;
11741 playfield_height -= vp_playfield->margin_bottom;
11743 // adjust playfield size if min/max width/height is specified
11745 if (vp_playfield->min_width != -1)
11746 vp_playfield->width = MAX(vp_playfield->min_width, playfield_width);
11748 if (vp_playfield->min_height != -1)
11749 vp_playfield->height = MAX(vp_playfield->min_height, playfield_height);
11751 if (vp_playfield->max_width != -1)
11752 vp_playfield->width = MIN(vp_playfield->width, vp_playfield->max_width);
11754 if (vp_playfield->max_height != -1)
11755 vp_playfield->height = MIN(vp_playfield->height,vp_playfield->max_height);
11757 // adjust playfield position according to specified alignment
11759 if (vp_playfield->align == ALIGN_LEFT || vp_playfield->x > 0)
11760 vp_playfield->x = ALIGNED_VP_XPOS(vp_playfield);
11761 else if (vp_playfield->align == ALIGN_CENTER)
11762 vp_playfield->x = playfield_width / 2 - vp_playfield->width / 2;
11763 else if (vp_playfield->align == ALIGN_RIGHT)
11764 vp_playfield->x += playfield_width - vp_playfield->width;
11766 if (vp_playfield->valign == VALIGN_TOP || vp_playfield->y > 0)
11767 vp_playfield->y = ALIGNED_VP_YPOS(vp_playfield);
11768 else if (vp_playfield->valign == VALIGN_MIDDLE)
11769 vp_playfield->y = playfield_height / 2 - vp_playfield->height / 2;
11770 else if (vp_playfield->valign == VALIGN_BOTTOM)
11771 vp_playfield->y += playfield_height - vp_playfield->height;
11773 vp_playfield->x += vp_playfield->margin_left;
11774 vp_playfield->y += vp_playfield->margin_top;
11776 // adjust individual playfield borders if only default border is specified
11778 if (vp_playfield->border_left == -1)
11779 vp_playfield->border_left = vp_playfield->border_size;
11780 if (vp_playfield->border_right == -1)
11781 vp_playfield->border_right = vp_playfield->border_size;
11782 if (vp_playfield->border_top == -1)
11783 vp_playfield->border_top = vp_playfield->border_size;
11784 if (vp_playfield->border_bottom == -1)
11785 vp_playfield->border_bottom = vp_playfield->border_size;
11787 // set dynamic playfield borders if borders are specified as undefined
11788 // (but only if window size was dynamic and playfield size was static)
11790 if (dynamic_window_width && !dynamic_playfield_width)
11792 if (vp_playfield->border_left == -1)
11794 vp_playfield->border_left = (vp_playfield->x -
11795 vp_playfield->margin_left);
11796 vp_playfield->x -= vp_playfield->border_left;
11797 vp_playfield->width += vp_playfield->border_left;
11800 if (vp_playfield->border_right == -1)
11802 vp_playfield->border_right = (vp_window->width -
11804 vp_playfield->width -
11805 vp_playfield->margin_right);
11806 vp_playfield->width += vp_playfield->border_right;
11810 if (dynamic_window_height && !dynamic_playfield_height)
11812 if (vp_playfield->border_top == -1)
11814 vp_playfield->border_top = (vp_playfield->y -
11815 vp_playfield->margin_top);
11816 vp_playfield->y -= vp_playfield->border_top;
11817 vp_playfield->height += vp_playfield->border_top;
11820 if (vp_playfield->border_bottom == -1)
11822 vp_playfield->border_bottom = (vp_window->height -
11824 vp_playfield->height -
11825 vp_playfield->margin_bottom);
11826 vp_playfield->height += vp_playfield->border_bottom;
11830 // adjust playfield size to be a multiple of a defined alignment tile size
11832 int align_size = vp_playfield->align_size;
11833 int playfield_xtiles = vp_playfield->width / align_size;
11834 int playfield_ytiles = vp_playfield->height / align_size;
11835 int playfield_width_corrected = playfield_xtiles * align_size;
11836 int playfield_height_corrected = playfield_ytiles * align_size;
11837 boolean is_playfield_mode = (i == GFX_SPECIAL_ARG_PLAYING ||
11838 i == GFX_SPECIAL_ARG_EDITOR);
11840 if (is_playfield_mode &&
11841 dynamic_playfield_width &&
11842 vp_playfield->width != playfield_width_corrected)
11844 int playfield_xdiff = vp_playfield->width - playfield_width_corrected;
11846 vp_playfield->width = playfield_width_corrected;
11848 if (vp_playfield->align == ALIGN_LEFT)
11850 vp_playfield->border_left += playfield_xdiff;
11852 else if (vp_playfield->align == ALIGN_RIGHT)
11854 vp_playfield->border_right += playfield_xdiff;
11856 else if (vp_playfield->align == ALIGN_CENTER)
11858 int border_left_diff = playfield_xdiff / 2;
11859 int border_right_diff = playfield_xdiff - border_left_diff;
11861 vp_playfield->border_left += border_left_diff;
11862 vp_playfield->border_right += border_right_diff;
11866 if (is_playfield_mode &&
11867 dynamic_playfield_height &&
11868 vp_playfield->height != playfield_height_corrected)
11870 int playfield_ydiff = vp_playfield->height - playfield_height_corrected;
11872 vp_playfield->height = playfield_height_corrected;
11874 if (vp_playfield->valign == VALIGN_TOP)
11876 vp_playfield->border_top += playfield_ydiff;
11878 else if (vp_playfield->align == VALIGN_BOTTOM)
11880 vp_playfield->border_right += playfield_ydiff;
11882 else if (vp_playfield->align == VALIGN_MIDDLE)
11884 int border_top_diff = playfield_ydiff / 2;
11885 int border_bottom_diff = playfield_ydiff - border_top_diff;
11887 vp_playfield->border_top += border_top_diff;
11888 vp_playfield->border_bottom += border_bottom_diff;
11892 // adjust door positions according to specified alignment
11894 for (j = 0; j < 2; j++)
11896 struct RectWithBorder *vp_door = (j == 0 ? vp_door_1 : vp_door_2);
11898 if (vp_door->align == ALIGN_LEFT || vp_door->x > 0)
11899 vp_door->x = ALIGNED_VP_XPOS(vp_door);
11900 else if (vp_door->align == ALIGN_CENTER)
11901 vp_door->x = vp_window->width / 2 - vp_door->width / 2;
11902 else if (vp_door->align == ALIGN_RIGHT)
11903 vp_door->x += vp_window->width - vp_door->width;
11905 if (vp_door->valign == VALIGN_TOP || vp_door->y > 0)
11906 vp_door->y = ALIGNED_VP_YPOS(vp_door);
11907 else if (vp_door->valign == VALIGN_MIDDLE)
11908 vp_door->y = vp_window->height / 2 - vp_door->height / 2;
11909 else if (vp_door->valign == VALIGN_BOTTOM)
11910 vp_door->y += vp_window->height - vp_door->height;
11915 static void InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics(void)
11919 struct XYTileSize *dst, *src;
11922 editor_buttons_xy[] =
11925 &editor.button.element_left, &editor.palette.element_left,
11926 IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT
11929 &editor.button.element_middle, &editor.palette.element_middle,
11930 IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE
11933 &editor.button.element_right, &editor.palette.element_right,
11934 IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT
11941 // set default position for element buttons to element graphics
11942 for (i = 0; editor_buttons_xy[i].dst != NULL; i++)
11944 if ((*editor_buttons_xy[i].dst).x == -1 &&
11945 (*editor_buttons_xy[i].dst).y == -1)
11947 struct GraphicInfo *gd = &graphic_info[editor_buttons_xy[i].graphic];
11949 gd->width = gd->height = editor_buttons_xy[i].src->tile_size;
11951 *editor_buttons_xy[i].dst = *editor_buttons_xy[i].src;
11955 // adjust editor palette rows and columns if specified to be dynamic
11957 if (editor.palette.cols == -1)
11959 int vp_width = viewport.playfield[GFX_SPECIAL_ARG_EDITOR].width;
11960 int bt_width = graphic_info[IMG_EDITOR_PALETTE_BUTTON].width;
11961 int sc_width = graphic_info[IMG_EDITOR_PALETTE_SCROLLBAR].width;
11963 editor.palette.cols = (vp_width - sc_width) / bt_width;
11965 if (editor.palette.x == -1)
11967 int palette_width = editor.palette.cols * bt_width + sc_width;
11969 editor.palette.x = (vp_width - palette_width) / 2;
11973 if (editor.palette.rows == -1)
11975 int vp_height = viewport.playfield[GFX_SPECIAL_ARG_EDITOR].height;
11976 int bt_height = graphic_info[IMG_EDITOR_PALETTE_BUTTON].height;
11977 int tx_height = getFontHeight(FONT_TEXT_2);
11979 editor.palette.rows = (vp_height - tx_height) / bt_height;
11981 if (editor.palette.y == -1)
11983 int palette_height = editor.palette.rows * bt_height + tx_height;
11985 editor.palette.y = (vp_height - palette_height) / 2;
11990 static void LoadMenuDesignSettingsFromFilename(char *filename)
11992 static struct TitleFadingInfo tfi;
11993 static struct TitleMessageInfo tmi;
11994 static struct TokenInfo title_tokens[] =
11996 { TYPE_INTEGER, &tfi.fade_mode, ".fade_mode" },
11997 { TYPE_INTEGER, &tfi.fade_delay, ".fade_delay" },
11998 { TYPE_INTEGER, &tfi.post_delay, ".post_delay" },
11999 { TYPE_INTEGER, &tfi.auto_delay, ".auto_delay" },
12000 { TYPE_INTEGER, &tfi.auto_delay_unit, ".auto_delay_unit" },
12004 static struct TokenInfo titlemessage_tokens[] =
12006 { TYPE_INTEGER, &tmi.x, ".x" },
12007 { TYPE_INTEGER, &tmi.y, ".y" },
12008 { TYPE_INTEGER, &tmi.width, ".width" },
12009 { TYPE_INTEGER, &tmi.height, ".height" },
12010 { TYPE_INTEGER, &tmi.chars, ".chars" },
12011 { TYPE_INTEGER, &tmi.lines, ".lines" },
12012 { TYPE_INTEGER, &tmi.align, ".align" },
12013 { TYPE_INTEGER, &tmi.valign, ".valign" },
12014 { TYPE_INTEGER, &tmi.font, ".font" },
12015 { TYPE_BOOLEAN, &tmi.autowrap, ".autowrap" },
12016 { TYPE_BOOLEAN, &tmi.centered, ".centered" },
12017 { TYPE_BOOLEAN, &tmi.parse_comments, ".parse_comments" },
12018 { TYPE_INTEGER, &tmi.sort_priority, ".sort_priority" },
12019 { TYPE_INTEGER, &tmi.fade_mode, ".fade_mode" },
12020 { TYPE_INTEGER, &tmi.fade_delay, ".fade_delay" },
12021 { TYPE_INTEGER, &tmi.post_delay, ".post_delay" },
12022 { TYPE_INTEGER, &tmi.auto_delay, ".auto_delay" },
12023 { TYPE_INTEGER, &tmi.auto_delay_unit, ".auto_delay_unit" },
12029 struct TitleFadingInfo *info;
12034 // initialize first titles from "enter screen" definitions, if defined
12035 { &title_initial_first_default, "menu.enter_screen.TITLE" },
12036 { &title_first_default, "menu.enter_screen.TITLE" },
12038 // initialize title screens from "next screen" definitions, if defined
12039 { &title_initial_default, "menu.next_screen.TITLE" },
12040 { &title_default, "menu.next_screen.TITLE" },
12046 struct TitleMessageInfo *array;
12049 titlemessage_arrays[] =
12051 // initialize first titles from "enter screen" definitions, if defined
12052 { titlescreen_initial_first, "menu.enter_screen.TITLE" },
12053 { titlescreen_first, "menu.enter_screen.TITLE" },
12054 { titlemessage_initial_first, "menu.enter_screen.TITLE" },
12055 { titlemessage_first, "menu.enter_screen.TITLE" },
12057 // initialize titles from "next screen" definitions, if defined
12058 { titlescreen_initial, "menu.next_screen.TITLE" },
12059 { titlescreen, "menu.next_screen.TITLE" },
12060 { titlemessage_initial, "menu.next_screen.TITLE" },
12061 { titlemessage, "menu.next_screen.TITLE" },
12063 // overwrite titles with title definitions, if defined
12064 { titlescreen_initial_first, "[title_initial]" },
12065 { titlescreen_first, "[title]" },
12066 { titlemessage_initial_first, "[title_initial]" },
12067 { titlemessage_first, "[title]" },
12069 { titlescreen_initial, "[title_initial]" },
12070 { titlescreen, "[title]" },
12071 { titlemessage_initial, "[title_initial]" },
12072 { titlemessage, "[title]" },
12074 // overwrite titles with title screen/message definitions, if defined
12075 { titlescreen_initial_first, "[titlescreen_initial]" },
12076 { titlescreen_first, "[titlescreen]" },
12077 { titlemessage_initial_first, "[titlemessage_initial]" },
12078 { titlemessage_first, "[titlemessage]" },
12080 { titlescreen_initial, "[titlescreen_initial]" },
12081 { titlescreen, "[titlescreen]" },
12082 { titlemessage_initial, "[titlemessage_initial]" },
12083 { titlemessage, "[titlemessage]" },
12087 SetupFileHash *setup_file_hash;
12090 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
12093 // the following initializes hierarchical values from dynamic configuration
12095 // special case: initialize with default values that may be overwritten
12096 // (e.g., init "menu.draw_xoffset.INFO" from "menu.draw_xoffset")
12097 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
12099 struct TokenIntPtrInfo menu_config[] =
12101 { "menu.draw_xoffset", &menu.draw_xoffset[i] },
12102 { "menu.draw_yoffset", &menu.draw_yoffset[i] },
12103 { "menu.list_size", &menu.list_size[i] }
12106 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
12108 char *token = menu_config[j].token;
12109 char *value = getHashEntry(setup_file_hash, token);
12112 *menu_config[j].value = get_integer_from_string(value);
12116 // special case: initialize with default values that may be overwritten
12117 // (eg, init "menu.draw_xoffset.INFO[XXX]" from "menu.draw_xoffset.INFO")
12118 for (i = 0; i < NUM_SPECIAL_GFX_INFO_ARGS; i++)
12120 struct TokenIntPtrInfo menu_config[] =
12122 { "menu.draw_xoffset.INFO", &menu.draw_xoffset_info[i] },
12123 { "menu.draw_yoffset.INFO", &menu.draw_yoffset_info[i] },
12124 { "menu.list_size.INFO", &menu.list_size_info[i] }
12127 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
12129 char *token = menu_config[j].token;
12130 char *value = getHashEntry(setup_file_hash, token);
12133 *menu_config[j].value = get_integer_from_string(value);
12137 // special case: initialize with default values that may be overwritten
12138 // (eg, init "menu.draw_xoffset.SETUP[XXX]" from "menu.draw_xoffset.SETUP")
12139 for (i = 0; i < NUM_SPECIAL_GFX_SETUP_ARGS; i++)
12141 struct TokenIntPtrInfo menu_config[] =
12143 { "menu.draw_xoffset.SETUP", &menu.draw_xoffset_setup[i] },
12144 { "menu.draw_yoffset.SETUP", &menu.draw_yoffset_setup[i] }
12147 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
12149 char *token = menu_config[j].token;
12150 char *value = getHashEntry(setup_file_hash, token);
12153 *menu_config[j].value = get_integer_from_string(value);
12157 // special case: initialize with default values that may be overwritten
12158 // (eg, init "menu.line_spacing.INFO[XXX]" from "menu.line_spacing.INFO")
12159 for (i = 0; i < NUM_SPECIAL_GFX_INFO_ARGS; i++)
12161 struct TokenIntPtrInfo menu_config[] =
12163 { "menu.left_spacing.INFO", &menu.left_spacing_info[i] },
12164 { "menu.right_spacing.INFO", &menu.right_spacing_info[i] },
12165 { "menu.top_spacing.INFO", &menu.top_spacing_info[i] },
12166 { "menu.bottom_spacing.INFO", &menu.bottom_spacing_info[i] },
12167 { "menu.paragraph_spacing.INFO", &menu.paragraph_spacing_info[i] },
12168 { "menu.headline1_spacing.INFO", &menu.headline1_spacing_info[i] },
12169 { "menu.headline2_spacing.INFO", &menu.headline2_spacing_info[i] },
12170 { "menu.line_spacing.INFO", &menu.line_spacing_info[i] },
12171 { "menu.extra_spacing.INFO", &menu.extra_spacing_info[i] },
12174 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
12176 char *token = menu_config[j].token;
12177 char *value = getHashEntry(setup_file_hash, token);
12180 *menu_config[j].value = get_integer_from_string(value);
12184 // special case: initialize with default values that may be overwritten
12185 // (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz")
12186 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
12188 struct TokenIntPtrInfo menu_config[] =
12190 { "menu.enter_screen.fade_mode", &menu.enter_screen[i].fade_mode },
12191 { "menu.enter_screen.fade_delay", &menu.enter_screen[i].fade_delay },
12192 { "menu.enter_screen.post_delay", &menu.enter_screen[i].post_delay },
12193 { "menu.leave_screen.fade_mode", &menu.leave_screen[i].fade_mode },
12194 { "menu.leave_screen.fade_delay", &menu.leave_screen[i].fade_delay },
12195 { "menu.leave_screen.post_delay", &menu.leave_screen[i].post_delay },
12196 { "menu.next_screen.fade_mode", &menu.next_screen[i].fade_mode },
12197 { "menu.next_screen.fade_delay", &menu.next_screen[i].fade_delay },
12198 { "menu.next_screen.post_delay", &menu.next_screen[i].post_delay }
12201 for (j = 0; j < ARRAY_SIZE(menu_config); j++)
12203 char *token = menu_config[j].token;
12204 char *value = getHashEntry(setup_file_hash, token);
12207 *menu_config[j].value = get_token_parameter_value(token, value);
12211 // special case: initialize with default values that may be overwritten
12212 // (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz")
12213 for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
12217 char *token_prefix;
12218 struct RectWithBorder *struct_ptr;
12222 { "viewport.window", &viewport.window[i] },
12223 { "viewport.playfield", &viewport.playfield[i] },
12224 { "viewport.door_1", &viewport.door_1[i] },
12225 { "viewport.door_2", &viewport.door_2[i] }
12228 for (j = 0; j < ARRAY_SIZE(vp_struct); j++)
12230 struct TokenIntPtrInfo vp_config[] =
12232 { ".x", &vp_struct[j].struct_ptr->x },
12233 { ".y", &vp_struct[j].struct_ptr->y },
12234 { ".width", &vp_struct[j].struct_ptr->width },
12235 { ".height", &vp_struct[j].struct_ptr->height },
12236 { ".min_width", &vp_struct[j].struct_ptr->min_width },
12237 { ".min_height", &vp_struct[j].struct_ptr->min_height },
12238 { ".max_width", &vp_struct[j].struct_ptr->max_width },
12239 { ".max_height", &vp_struct[j].struct_ptr->max_height },
12240 { ".margin_left", &vp_struct[j].struct_ptr->margin_left },
12241 { ".margin_right", &vp_struct[j].struct_ptr->margin_right },
12242 { ".margin_top", &vp_struct[j].struct_ptr->margin_top },
12243 { ".margin_bottom", &vp_struct[j].struct_ptr->margin_bottom },
12244 { ".border_left", &vp_struct[j].struct_ptr->border_left },
12245 { ".border_right", &vp_struct[j].struct_ptr->border_right },
12246 { ".border_top", &vp_struct[j].struct_ptr->border_top },
12247 { ".border_bottom", &vp_struct[j].struct_ptr->border_bottom },
12248 { ".border_size", &vp_struct[j].struct_ptr->border_size },
12249 { ".align_size", &vp_struct[j].struct_ptr->align_size },
12250 { ".align", &vp_struct[j].struct_ptr->align },
12251 { ".valign", &vp_struct[j].struct_ptr->valign }
12254 for (k = 0; k < ARRAY_SIZE(vp_config); k++)
12256 char *token = getStringCat2(vp_struct[j].token_prefix,
12257 vp_config[k].token);
12258 char *value = getHashEntry(setup_file_hash, token);
12261 *vp_config[k].value = get_token_parameter_value(token, value);
12268 // special case: initialize with default values that may be overwritten
12269 // (e.g., init "[title].fade_mode" from "menu.next_screen.TITLE.fade_mode")
12270 for (i = 0; title_info[i].info != NULL; i++)
12272 struct TitleFadingInfo *info = title_info[i].info;
12273 char *base_token = title_info[i].text;
12275 for (j = 0; title_tokens[j].type != -1; j++)
12277 char *token = getStringCat2(base_token, title_tokens[j].text);
12278 char *value = getHashEntry(setup_file_hash, token);
12282 int parameter_value = get_token_parameter_value(token, value);
12286 *(int *)title_tokens[j].value = (int)parameter_value;
12295 // special case: initialize with default values that may be overwritten
12296 // (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode")
12297 for (i = 0; titlemessage_arrays[i].array != NULL; i++)
12299 struct TitleMessageInfo *array = titlemessage_arrays[i].array;
12300 char *base_token = titlemessage_arrays[i].text;
12302 for (j = 0; titlemessage_tokens[j].type != -1; j++)
12304 char *token = getStringCat2(base_token, titlemessage_tokens[j].text);
12305 char *value = getHashEntry(setup_file_hash, token);
12309 int parameter_value = get_token_parameter_value(token, value);
12311 for (k = 0; k < MAX_NUM_TITLE_MESSAGES; k++)
12315 if (titlemessage_tokens[j].type == TYPE_INTEGER)
12316 *(int *)titlemessage_tokens[j].value = (int)parameter_value;
12318 *(boolean *)titlemessage_tokens[j].value = (boolean)parameter_value;
12328 // special case: check if network and preview player positions are redefined,
12329 // to compare this later against the main menu level preview being redefined
12330 struct TokenIntPtrInfo menu_config_players[] =
12332 { "main.network_players.x", &menu.main.network_players.redefined },
12333 { "main.network_players.y", &menu.main.network_players.redefined },
12334 { "main.preview_players.x", &menu.main.preview_players.redefined },
12335 { "main.preview_players.y", &menu.main.preview_players.redefined },
12336 { "preview.x", &preview.redefined },
12337 { "preview.y", &preview.redefined }
12340 for (i = 0; i < ARRAY_SIZE(menu_config_players); i++)
12341 *menu_config_players[i].value = FALSE;
12343 for (i = 0; i < ARRAY_SIZE(menu_config_players); i++)
12344 if (getHashEntry(setup_file_hash, menu_config_players[i].token) != NULL)
12345 *menu_config_players[i].value = TRUE;
12347 // read (and overwrite with) values that may be specified in config file
12348 for (i = 0; image_config_vars[i].token != NULL; i++)
12350 char *value = getHashEntry(setup_file_hash, image_config_vars[i].token);
12352 // (ignore definitions set to "[DEFAULT]" which are already initialized)
12353 if (value != NULL && !strEqual(value, ARG_DEFAULT))
12354 *image_config_vars[i].value =
12355 get_token_parameter_value(image_config_vars[i].token, value);
12358 freeSetupFileHash(setup_file_hash);
12361 void LoadMenuDesignSettings(void)
12363 char *filename_base = UNDEFINED_FILENAME, *filename_local;
12365 InitMenuDesignSettings_Static();
12366 InitMenuDesignSettings_SpecialPreProcessing();
12368 if (!GFX_OVERRIDE_ARTWORK(ARTWORK_TYPE_GRAPHICS))
12370 // first look for special settings configured in level series config
12371 filename_base = getCustomArtworkLevelConfigFilename(ARTWORK_TYPE_GRAPHICS);
12373 if (fileExists(filename_base))
12374 LoadMenuDesignSettingsFromFilename(filename_base);
12377 filename_local = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
12379 if (filename_local != NULL && !strEqual(filename_base, filename_local))
12380 LoadMenuDesignSettingsFromFilename(filename_local);
12382 InitMenuDesignSettings_SpecialPostProcessing();
12385 void LoadMenuDesignSettings_AfterGraphics(void)
12387 InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics();
12390 void LoadUserDefinedEditorElementList(int **elements, int *num_elements)
12392 char *filename = getEditorSetupFilename();
12393 SetupFileList *setup_file_list, *list;
12394 SetupFileHash *element_hash;
12395 int num_unknown_tokens = 0;
12398 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
12401 element_hash = newSetupFileHash();
12403 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
12404 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
12406 // determined size may be larger than needed (due to unknown elements)
12408 for (list = setup_file_list; list != NULL; list = list->next)
12411 // add space for up to 3 more elements for padding that may be needed
12412 *num_elements += 3;
12414 // free memory for old list of elements, if needed
12415 checked_free(*elements);
12417 // allocate memory for new list of elements
12418 *elements = checked_malloc(*num_elements * sizeof(int));
12421 for (list = setup_file_list; list != NULL; list = list->next)
12423 char *value = getHashEntry(element_hash, list->token);
12425 if (value == NULL) // try to find obsolete token mapping
12427 char *mapped_token = get_mapped_token(list->token);
12429 if (mapped_token != NULL)
12431 value = getHashEntry(element_hash, mapped_token);
12433 free(mapped_token);
12439 (*elements)[(*num_elements)++] = atoi(value);
12443 if (num_unknown_tokens == 0)
12446 Warn("unknown token(s) found in config file:");
12447 Warn("- config file: '%s'", filename);
12449 num_unknown_tokens++;
12452 Warn("- token: '%s'", list->token);
12456 if (num_unknown_tokens > 0)
12459 while (*num_elements % 4) // pad with empty elements, if needed
12460 (*elements)[(*num_elements)++] = EL_EMPTY;
12462 freeSetupFileList(setup_file_list);
12463 freeSetupFileHash(element_hash);
12466 for (i = 0; i < *num_elements; i++)
12467 Debug("editor", "element '%s' [%d]\n",
12468 element_info[(*elements)[i]].token_name, (*elements)[i]);
12472 static struct MusicFileInfo *get_music_file_info_ext(char *basename, int music,
12475 SetupFileHash *setup_file_hash = NULL;
12476 struct MusicFileInfo tmp_music_file_info, *new_music_file_info;
12477 char *filename_music, *filename_prefix, *filename_info;
12483 token_to_value_ptr[] =
12485 { "title_header", &tmp_music_file_info.title_header },
12486 { "artist_header", &tmp_music_file_info.artist_header },
12487 { "album_header", &tmp_music_file_info.album_header },
12488 { "year_header", &tmp_music_file_info.year_header },
12490 { "title", &tmp_music_file_info.title },
12491 { "artist", &tmp_music_file_info.artist },
12492 { "album", &tmp_music_file_info.album },
12493 { "year", &tmp_music_file_info.year },
12499 filename_music = (is_sound ? getCustomSoundFilename(basename) :
12500 getCustomMusicFilename(basename));
12502 if (filename_music == NULL)
12505 // ---------- try to replace file extension ----------
12507 filename_prefix = getStringCopy(filename_music);
12508 if (strrchr(filename_prefix, '.') != NULL)
12509 *strrchr(filename_prefix, '.') = '\0';
12510 filename_info = getStringCat2(filename_prefix, ".txt");
12512 if (fileExists(filename_info))
12513 setup_file_hash = loadSetupFileHash(filename_info);
12515 free(filename_prefix);
12516 free(filename_info);
12518 if (setup_file_hash == NULL)
12520 // ---------- try to add file extension ----------
12522 filename_prefix = getStringCopy(filename_music);
12523 filename_info = getStringCat2(filename_prefix, ".txt");
12525 if (fileExists(filename_info))
12526 setup_file_hash = loadSetupFileHash(filename_info);
12528 free(filename_prefix);
12529 free(filename_info);
12532 if (setup_file_hash == NULL)
12535 // ---------- music file info found ----------
12537 clear_mem(&tmp_music_file_info, sizeof(struct MusicFileInfo));
12539 for (i = 0; token_to_value_ptr[i].token != NULL; i++)
12541 char *value = getHashEntry(setup_file_hash, token_to_value_ptr[i].token);
12543 *token_to_value_ptr[i].value_ptr =
12544 getStringCopy(value != NULL && *value != '\0' ? value : UNKNOWN_NAME);
12547 tmp_music_file_info.basename = getStringCopy(basename);
12548 tmp_music_file_info.music = music;
12549 tmp_music_file_info.is_sound = is_sound;
12551 new_music_file_info = checked_malloc(sizeof(struct MusicFileInfo));
12552 *new_music_file_info = tmp_music_file_info;
12554 return new_music_file_info;
12557 static struct MusicFileInfo *get_music_file_info(char *basename, int music)
12559 return get_music_file_info_ext(basename, music, FALSE);
12562 static struct MusicFileInfo *get_sound_file_info(char *basename, int sound)
12564 return get_music_file_info_ext(basename, sound, TRUE);
12567 static boolean music_info_listed_ext(struct MusicFileInfo *list,
12568 char *basename, boolean is_sound)
12570 for (; list != NULL; list = list->next)
12571 if (list->is_sound == is_sound && strEqual(list->basename, basename))
12577 static boolean music_info_listed(struct MusicFileInfo *list, char *basename)
12579 return music_info_listed_ext(list, basename, FALSE);
12582 static boolean sound_info_listed(struct MusicFileInfo *list, char *basename)
12584 return music_info_listed_ext(list, basename, TRUE);
12587 void LoadMusicInfo(void)
12589 char *music_directory = getCustomMusicDirectory();
12590 int num_music = getMusicListSize();
12591 int num_music_noconf = 0;
12592 int num_sounds = getSoundListSize();
12594 DirectoryEntry *dir_entry;
12595 struct FileInfo *music, *sound;
12596 struct MusicFileInfo *next, **new;
12599 while (music_file_info != NULL)
12601 next = music_file_info->next;
12603 checked_free(music_file_info->basename);
12605 checked_free(music_file_info->title_header);
12606 checked_free(music_file_info->artist_header);
12607 checked_free(music_file_info->album_header);
12608 checked_free(music_file_info->year_header);
12610 checked_free(music_file_info->title);
12611 checked_free(music_file_info->artist);
12612 checked_free(music_file_info->album);
12613 checked_free(music_file_info->year);
12615 free(music_file_info);
12617 music_file_info = next;
12620 new = &music_file_info;
12622 for (i = 0; i < num_music; i++)
12624 music = getMusicListEntry(i);
12626 if (music->filename == NULL)
12629 if (strEqual(music->filename, UNDEFINED_FILENAME))
12632 // a configured file may be not recognized as music
12633 if (!FileIsMusic(music->filename))
12636 if (!music_info_listed(music_file_info, music->filename))
12638 *new = get_music_file_info(music->filename, i);
12641 new = &(*new)->next;
12645 if ((dir = openDirectory(music_directory)) == NULL)
12647 Warn("cannot read music directory '%s'", music_directory);
12652 while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries
12654 char *basename = dir_entry->basename;
12655 boolean music_already_used = FALSE;
12658 // skip all music files that are configured in music config file
12659 for (i = 0; i < num_music; i++)
12661 music = getMusicListEntry(i);
12663 if (music->filename == NULL)
12666 if (strEqual(basename, music->filename))
12668 music_already_used = TRUE;
12673 if (music_already_used)
12676 if (!FileIsMusic(dir_entry->filename))
12679 if (!music_info_listed(music_file_info, basename))
12681 *new = get_music_file_info(basename, MAP_NOCONF_MUSIC(num_music_noconf));
12684 new = &(*new)->next;
12687 num_music_noconf++;
12690 closeDirectory(dir);
12692 for (i = 0; i < num_sounds; i++)
12694 sound = getSoundListEntry(i);
12696 if (sound->filename == NULL)
12699 if (strEqual(sound->filename, UNDEFINED_FILENAME))
12702 // a configured file may be not recognized as sound
12703 if (!FileIsSound(sound->filename))
12706 if (!sound_info_listed(music_file_info, sound->filename))
12708 *new = get_sound_file_info(sound->filename, i);
12710 new = &(*new)->next;
12715 static void add_helpanim_entry(int element, int action, int direction,
12716 int delay, int *num_list_entries)
12718 struct HelpAnimInfo *new_list_entry;
12719 (*num_list_entries)++;
12722 checked_realloc(helpanim_info,
12723 *num_list_entries * sizeof(struct HelpAnimInfo));
12724 new_list_entry = &helpanim_info[*num_list_entries - 1];
12726 new_list_entry->element = element;
12727 new_list_entry->action = action;
12728 new_list_entry->direction = direction;
12729 new_list_entry->delay = delay;
12732 static void print_unknown_token(char *filename, char *token, int token_nr)
12737 Warn("unknown token(s) found in config file:");
12738 Warn("- config file: '%s'", filename);
12741 Warn("- token: '%s'", token);
12744 static void print_unknown_token_end(int token_nr)
12750 void LoadHelpAnimInfo(void)
12752 char *filename = getHelpAnimFilename();
12753 SetupFileList *setup_file_list = NULL, *list;
12754 SetupFileHash *element_hash, *action_hash, *direction_hash;
12755 int num_list_entries = 0;
12756 int num_unknown_tokens = 0;
12759 if (fileExists(filename))
12760 setup_file_list = loadSetupFileList(filename);
12762 if (setup_file_list == NULL)
12764 // use reliable default values from static configuration
12765 SetupFileList *insert_ptr;
12767 insert_ptr = setup_file_list =
12768 newSetupFileList(helpanim_config[0].token,
12769 helpanim_config[0].value);
12771 for (i = 1; helpanim_config[i].token; i++)
12772 insert_ptr = addListEntry(insert_ptr,
12773 helpanim_config[i].token,
12774 helpanim_config[i].value);
12777 element_hash = newSetupFileHash();
12778 action_hash = newSetupFileHash();
12779 direction_hash = newSetupFileHash();
12781 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
12782 setHashEntry(element_hash, element_info[i].token_name, i_to_a(i));
12784 for (i = 0; i < NUM_ACTIONS; i++)
12785 setHashEntry(action_hash, element_action_info[i].suffix,
12786 i_to_a(element_action_info[i].value));
12788 // do not store direction index (bit) here, but direction value!
12789 for (i = 0; i < NUM_DIRECTIONS_FULL; i++)
12790 setHashEntry(direction_hash, element_direction_info[i].suffix,
12791 i_to_a(1 << element_direction_info[i].value));
12793 for (list = setup_file_list; list != NULL; list = list->next)
12795 char *element_token, *action_token, *direction_token;
12796 char *element_value, *action_value, *direction_value;
12797 int delay = atoi(list->value);
12799 if (strEqual(list->token, "end"))
12801 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
12806 /* first try to break element into element/action/direction parts;
12807 if this does not work, also accept combined "element[.act][.dir]"
12808 elements (like "dynamite.active"), which are unique elements */
12810 if (strchr(list->token, '.') == NULL) // token contains no '.'
12812 element_value = getHashEntry(element_hash, list->token);
12813 if (element_value != NULL) // element found
12814 add_helpanim_entry(atoi(element_value), -1, -1, delay,
12815 &num_list_entries);
12818 // no further suffixes found -- this is not an element
12819 print_unknown_token(filename, list->token, num_unknown_tokens++);
12825 // token has format "<prefix>.<something>"
12827 action_token = strchr(list->token, '.'); // suffix may be action ...
12828 direction_token = action_token; // ... or direction
12830 element_token = getStringCopy(list->token);
12831 *strchr(element_token, '.') = '\0';
12833 element_value = getHashEntry(element_hash, element_token);
12835 if (element_value == NULL) // this is no element
12837 element_value = getHashEntry(element_hash, list->token);
12838 if (element_value != NULL) // combined element found
12839 add_helpanim_entry(atoi(element_value), -1, -1, delay,
12840 &num_list_entries);
12842 print_unknown_token(filename, list->token, num_unknown_tokens++);
12844 free(element_token);
12849 action_value = getHashEntry(action_hash, action_token);
12851 if (action_value != NULL) // action found
12853 add_helpanim_entry(atoi(element_value), atoi(action_value), -1, delay,
12854 &num_list_entries);
12856 free(element_token);
12861 direction_value = getHashEntry(direction_hash, direction_token);
12863 if (direction_value != NULL) // direction found
12865 add_helpanim_entry(atoi(element_value), -1, atoi(direction_value), delay,
12866 &num_list_entries);
12868 free(element_token);
12873 if (strchr(action_token + 1, '.') == NULL)
12875 // no further suffixes found -- this is not an action nor direction
12877 element_value = getHashEntry(element_hash, list->token);
12878 if (element_value != NULL) // combined element found
12879 add_helpanim_entry(atoi(element_value), -1, -1, delay,
12880 &num_list_entries);
12882 print_unknown_token(filename, list->token, num_unknown_tokens++);
12884 free(element_token);
12889 // token has format "<prefix>.<suffix>.<something>"
12891 direction_token = strchr(action_token + 1, '.');
12893 action_token = getStringCopy(action_token);
12894 *strchr(action_token + 1, '.') = '\0';
12896 action_value = getHashEntry(action_hash, action_token);
12898 if (action_value == NULL) // this is no action
12900 element_value = getHashEntry(element_hash, list->token);
12901 if (element_value != NULL) // combined element found
12902 add_helpanim_entry(atoi(element_value), -1, -1, delay,
12903 &num_list_entries);
12905 print_unknown_token(filename, list->token, num_unknown_tokens++);
12907 free(element_token);
12908 free(action_token);
12913 direction_value = getHashEntry(direction_hash, direction_token);
12915 if (direction_value != NULL) // direction found
12917 add_helpanim_entry(atoi(element_value), atoi(action_value),
12918 atoi(direction_value), delay, &num_list_entries);
12920 free(element_token);
12921 free(action_token);
12926 // this is no direction
12928 element_value = getHashEntry(element_hash, list->token);
12929 if (element_value != NULL) // combined element found
12930 add_helpanim_entry(atoi(element_value), -1, -1, delay,
12931 &num_list_entries);
12933 print_unknown_token(filename, list->token, num_unknown_tokens++);
12935 free(element_token);
12936 free(action_token);
12939 print_unknown_token_end(num_unknown_tokens);
12941 add_helpanim_entry(HELPANIM_LIST_NEXT, -1, -1, -1, &num_list_entries);
12942 add_helpanim_entry(HELPANIM_LIST_END, -1, -1, -1, &num_list_entries);
12944 freeSetupFileList(setup_file_list);
12945 freeSetupFileHash(element_hash);
12946 freeSetupFileHash(action_hash);
12947 freeSetupFileHash(direction_hash);
12950 for (i = 0; i < num_list_entries; i++)
12951 Debug("files:LoadHelpAnimInfo", "'%s': %d, %d, %d => %d",
12952 EL_NAME(helpanim_info[i].element),
12953 helpanim_info[i].element,
12954 helpanim_info[i].action,
12955 helpanim_info[i].direction,
12956 helpanim_info[i].delay);
12960 void LoadHelpTextInfo(void)
12962 char *filename = getHelpTextFilename();
12965 if (helptext_info != NULL)
12967 freeSetupFileHash(helptext_info);
12968 helptext_info = NULL;
12971 if (fileExists(filename))
12972 helptext_info = loadSetupFileHash(filename);
12974 if (helptext_info == NULL)
12976 // use reliable default values from static configuration
12977 helptext_info = newSetupFileHash();
12979 for (i = 0; helptext_config[i].token; i++)
12980 setHashEntry(helptext_info,
12981 helptext_config[i].token,
12982 helptext_config[i].value);
12986 BEGIN_HASH_ITERATION(helptext_info, itr)
12988 Debug("files:LoadHelpTextInfo", "'%s' => '%s'",
12989 HASH_ITERATION_TOKEN(itr), HASH_ITERATION_VALUE(itr));
12991 END_HASH_ITERATION(hash, itr)
12996 // ----------------------------------------------------------------------------
12998 // ----------------------------------------------------------------------------
13000 #define MAX_NUM_CONVERT_LEVELS 1000
13002 void ConvertLevels(void)
13004 static LevelDirTree *convert_leveldir = NULL;
13005 static int convert_level_nr = -1;
13006 static int num_levels_handled = 0;
13007 static int num_levels_converted = 0;
13008 static boolean levels_failed[MAX_NUM_CONVERT_LEVELS];
13011 convert_leveldir = getTreeInfoFromIdentifier(leveldir_first,
13012 global.convert_leveldir);
13014 if (convert_leveldir == NULL)
13015 Fail("no such level identifier: '%s'", global.convert_leveldir);
13017 leveldir_current = convert_leveldir;
13019 if (global.convert_level_nr != -1)
13021 convert_leveldir->first_level = global.convert_level_nr;
13022 convert_leveldir->last_level = global.convert_level_nr;
13025 convert_level_nr = convert_leveldir->first_level;
13027 PrintLine("=", 79);
13028 Print("Converting levels\n");
13029 PrintLine("-", 79);
13030 Print("Level series identifier: '%s'\n", convert_leveldir->identifier);
13031 Print("Level series name: '%s'\n", convert_leveldir->name);
13032 Print("Level series author: '%s'\n", convert_leveldir->author);
13033 Print("Number of levels: %d\n", convert_leveldir->levels);
13034 PrintLine("=", 79);
13037 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
13038 levels_failed[i] = FALSE;
13040 while (convert_level_nr <= convert_leveldir->last_level)
13042 char *level_filename;
13045 level_nr = convert_level_nr++;
13047 Print("Level %03d: ", level_nr);
13049 LoadLevel(level_nr);
13050 if (level.no_level_file || level.no_valid_file)
13052 Print("(no level)\n");
13056 Print("converting level ... ");
13058 level_filename = getDefaultLevelFilename(level_nr);
13059 new_level = !fileExists(level_filename);
13063 SaveLevel(level_nr);
13065 num_levels_converted++;
13067 Print("converted.\n");
13071 if (level_nr >= 0 && level_nr < MAX_NUM_CONVERT_LEVELS)
13072 levels_failed[level_nr] = TRUE;
13074 Print("NOT CONVERTED -- LEVEL ALREADY EXISTS.\n");
13077 num_levels_handled++;
13081 PrintLine("=", 79);
13082 Print("Number of levels handled: %d\n", num_levels_handled);
13083 Print("Number of levels converted: %d (%d%%)\n", num_levels_converted,
13084 (num_levels_handled ?
13085 num_levels_converted * 100 / num_levels_handled : 0));
13086 PrintLine("-", 79);
13087 Print("Summary (for automatic parsing by scripts):\n");
13088 Print("LEVELDIR '%s', CONVERTED %d/%d (%d%%)",
13089 convert_leveldir->identifier, num_levels_converted,
13090 num_levels_handled,
13091 (num_levels_handled ?
13092 num_levels_converted * 100 / num_levels_handled : 0));
13094 if (num_levels_handled != num_levels_converted)
13096 Print(", FAILED:");
13097 for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
13098 if (levels_failed[i])
13103 PrintLine("=", 79);
13105 CloseAllAndExit(0);
13109 // ----------------------------------------------------------------------------
13110 // create and save images for use in level sketches (raw BMP format)
13111 // ----------------------------------------------------------------------------
13113 void CreateLevelSketchImages(void)
13119 InitElementPropertiesGfxElement();
13121 bitmap1 = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
13122 bitmap2 = CreateBitmap(MINI_TILEX, MINI_TILEY, DEFAULT_DEPTH);
13124 for (i = 0; i < NUM_FILE_ELEMENTS; i++)
13126 int element = getMappedElement(i);
13127 char basename1[16];
13128 char basename2[16];
13132 sprintf(basename1, "%04d.bmp", i);
13133 sprintf(basename2, "%04ds.bmp", i);
13135 filename1 = getPath2(global.create_sketch_images_dir, basename1);
13136 filename2 = getPath2(global.create_sketch_images_dir, basename2);
13138 DrawSizedElement(0, 0, element, TILESIZE);
13139 BlitBitmap(drawto, bitmap1, SX, SY, TILEX, TILEY, 0, 0);
13141 if (SDL_SaveBMP(bitmap1->surface, filename1) != 0)
13142 Fail("cannot save level sketch image file '%s'", filename1);
13144 DrawSizedElement(0, 0, element, MINI_TILESIZE);
13145 BlitBitmap(drawto, bitmap2, SX, SY, MINI_TILEX, MINI_TILEY, 0, 0);
13147 if (SDL_SaveBMP(bitmap2->surface, filename2) != 0)
13148 Fail("cannot save level sketch image file '%s'", filename2);
13153 // create corresponding SQL statements (for normal and small images)
13156 printf("insert into phpbb_words values (NULL, '`%03d', '<IMG class=\"levelsketch\" src=\"/I/%04d.png\"/>');\n", i, i);
13157 printf("insert into phpbb_words values (NULL, '¸%03d', '<IMG class=\"levelsketch\" src=\"/I/%04ds.png\"/>');\n", i, i);
13160 printf("insert into phpbb_words values (NULL, '`%04d', '<IMG class=\"levelsketch\" src=\"/I/%04d.png\"/>');\n", i, i);
13161 printf("insert into phpbb_words values (NULL, '¸%04d', '<IMG class=\"levelsketch\" src=\"/I/%04ds.png\"/>');\n", i, i);
13163 // optional: create content for forum level sketch demonstration post
13165 fprintf(stderr, "%03d `%03d%c", i, i, (i % 10 < 9 ? ' ' : '\n'));
13168 FreeBitmap(bitmap1);
13169 FreeBitmap(bitmap2);
13172 fprintf(stderr, "\n");
13174 Info("%d normal and small images created", NUM_FILE_ELEMENTS);
13176 CloseAllAndExit(0);
13180 // ----------------------------------------------------------------------------
13181 // create and save images for element collecting animations (raw BMP format)
13182 // ----------------------------------------------------------------------------
13184 static boolean createCollectImage(int element)
13186 return (IS_COLLECTIBLE(element) && !IS_SP_ELEMENT(element));
13189 void CreateCollectElementImages(void)
13193 int anim_frames = num_steps - 1;
13194 int tile_size = TILESIZE;
13195 int anim_width = tile_size * anim_frames;
13196 int anim_height = tile_size;
13197 int num_collect_images = 0;
13198 int pos_collect_images = 0;
13200 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
13201 if (createCollectImage(i))
13202 num_collect_images++;
13204 Info("Creating %d element collecting animation images ...",
13205 num_collect_images);
13207 int dst_width = anim_width * 2;
13208 int dst_height = anim_height * num_collect_images / 2;
13209 Bitmap *dst_bitmap = CreateBitmap(dst_width, dst_height, DEFAULT_DEPTH);
13210 char *basename = "RocksCollect.bmp";
13211 char *filename = getPath2(global.create_collect_images_dir, basename);
13213 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
13215 if (!createCollectImage(i))
13218 int dst_x = (pos_collect_images / (num_collect_images / 2)) * anim_width;
13219 int dst_y = (pos_collect_images % (num_collect_images / 2)) * anim_height;
13220 int graphic = el2img(i);
13221 char *token_name = element_info[i].token_name;
13222 Bitmap *tmp_bitmap = CreateBitmap(tile_size, tile_size, DEFAULT_DEPTH);
13223 Bitmap *src_bitmap;
13226 Info("- creating collecting image for '%s' ...", token_name);
13228 getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
13230 BlitBitmap(src_bitmap, tmp_bitmap, src_x, src_y,
13231 tile_size, tile_size, 0, 0);
13233 tmp_bitmap->surface_masked = tmp_bitmap->surface;
13235 for (j = 0; j < anim_frames; j++)
13237 int frame_size_final = tile_size * (anim_frames - j) / num_steps;
13238 int frame_size = frame_size_final * num_steps;
13239 int offset = (tile_size - frame_size_final) / 2;
13240 Bitmap *frame_bitmap = ZoomBitmap(tmp_bitmap, frame_size, frame_size);
13242 while (frame_size > frame_size_final)
13246 Bitmap *half_bitmap = ZoomBitmap(frame_bitmap, frame_size, frame_size);
13248 FreeBitmap(frame_bitmap);
13250 frame_bitmap = half_bitmap;
13253 BlitBitmap(frame_bitmap, dst_bitmap, 0, 0,
13254 frame_size_final, frame_size_final,
13255 dst_x + j * tile_size + offset, dst_y + offset);
13257 FreeBitmap(frame_bitmap);
13260 tmp_bitmap->surface_masked = NULL;
13262 FreeBitmap(tmp_bitmap);
13264 pos_collect_images++;
13267 if (SDL_SaveBMP(dst_bitmap->surface, filename) != 0)
13268 Fail("cannot save element collecting image file '%s'", filename);
13270 FreeBitmap(dst_bitmap);
13274 CloseAllAndExit(0);
13278 // ----------------------------------------------------------------------------
13279 // create and save images for custom and group elements (raw BMP format)
13280 // ----------------------------------------------------------------------------
13282 void CreateCustomElementImages(char *directory)
13284 char *src_basename = "RocksCE-template.ilbm";
13285 char *dst_basename = "RocksCE.bmp";
13286 char *src_filename = getPath2(directory, src_basename);
13287 char *dst_filename = getPath2(directory, dst_basename);
13288 Bitmap *src_bitmap;
13290 int yoffset_ce = 0;
13291 int yoffset_ge = (TILEY * NUM_CUSTOM_ELEMENTS / 16);
13294 InitVideoDefaults();
13296 ReCreateBitmap(&backbuffer, video.width, video.height);
13298 src_bitmap = LoadImage(src_filename);
13300 bitmap = CreateBitmap(TILEX * 16 * 2,
13301 TILEY * (NUM_CUSTOM_ELEMENTS + NUM_GROUP_ELEMENTS) / 16,
13304 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
13311 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
13312 TILEX * x, TILEY * y + yoffset_ce);
13314 BlitBitmap(src_bitmap, bitmap, 0, TILEY,
13316 TILEX * x + TILEX * 16,
13317 TILEY * y + yoffset_ce);
13319 for (j = 2; j >= 0; j--)
13323 BlitBitmap(src_bitmap, bitmap,
13324 TILEX + c * 7, 0, 6, 10,
13325 TILEX * x + 6 + j * 7,
13326 TILEY * y + 11 + yoffset_ce);
13328 BlitBitmap(src_bitmap, bitmap,
13329 TILEX + c * 8, TILEY, 6, 10,
13330 TILEX * 16 + TILEX * x + 6 + j * 8,
13331 TILEY * y + 10 + yoffset_ce);
13337 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
13344 BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
13345 TILEX * x, TILEY * y + yoffset_ge);
13347 BlitBitmap(src_bitmap, bitmap, 0, TILEY,
13349 TILEX * x + TILEX * 16,
13350 TILEY * y + yoffset_ge);
13352 for (j = 1; j >= 0; j--)
13356 BlitBitmap(src_bitmap, bitmap, TILEX + c * 10, 11, 10, 10,
13357 TILEX * x + 6 + j * 10,
13358 TILEY * y + 11 + yoffset_ge);
13360 BlitBitmap(src_bitmap, bitmap,
13361 TILEX + c * 8, TILEY + 12, 6, 10,
13362 TILEX * 16 + TILEX * x + 10 + j * 8,
13363 TILEY * y + 10 + yoffset_ge);
13369 if (SDL_SaveBMP(bitmap->surface, dst_filename) != 0)
13370 Fail("cannot save CE graphics file '%s'", dst_filename);
13372 FreeBitmap(bitmap);
13374 CloseAllAndExit(0);