3 * handle binary emc cave file format
9 /* convert emc caves to intermediate cave format.
11 * the intermediate format supports most emc caves, but some internal
12 * objects are not supported.
15 * - active objects - use global flags instead of individual on/off
16 * - falling or paused objects - become still objects
17 * - sand states - become either sand with/without stone
18 * - collected objects - become a common pause object
19 * - drip states - become a normal drip
22 * - exploding objects (not in emc player either)
24 * pushed objects are supported in the cave, eaters and magic ball.
25 * they behave almost the same as in the emc player, pushing over
26 * steel wall, androids, players etc. a compile time option chooses
27 * if pushed objects go into the acid.
29 * acid is handled completely different in my player. acid top is a
30 * separate object, which can be put anywhere and the beasts will
31 * be gobbled, even if they go in from below. in the emc player, an
32 * acid top without a bottom becomes plant.
34 * borderless caves are supported, almost the same as in the emc
35 * player. going off the left edge of the screen resulted in the
36 * beast/player moving up 1 square (and the player disappeared).
37 * going off the right edge of the screen, the beast/player would
38 * go down 1 square. in my player, everything stays on the same y
39 * coord, which is sensible, but breaks caves which expect the old
43 #define GET_BE16(x) ((&x)[0] << 8 | (&x)[1])
45 static const short map_emc[256] =
47 Cstone, Cstone, Cdiamond, Cdiamond, // 0
48 Calien, Calien, Cpause, Cpause, // 4
49 Ctank_1_n, Ctank_1_e, Ctank_1_s, Ctank_1_w, // 8
50 Ctank_2_n, Ctank_2_e, Ctank_2_s, Ctank_2_w, // 12
51 Cbomb, Cbomb, Cemerald, Cemerald, // 16
52 Cbug_1_n, Cbug_1_e, Cbug_1_s, Cbug_1_w, // 20
53 Cbug_2_n, Cbug_2_e, Cbug_2_s, Cbug_2_w, // 24
54 Cdrip, Cdrip, Cblank, Cdrip, // 28
55 Cstone, Cbomb, Cdiamond, Cemerald, // 32
56 Cwonderwall, Cnut, Cnut, Cnut, // 36
57 Cwheel, Ceater_n, Ceater_s, Ceater_w, // 40
58 Ceater_e, Csand_stone, Cblank, Cblank, // 44
59 Cblank, Csand, Csand, Csand, // 48
60 Csand_stone, Csand_stone, Csand_stone, Csand, // 52
61 Cstone, Cslide_ew, Cslide_ns, Cdynamite_1, // 56
62 Cdynamite_2, Cdynamite_3, Cdynamite_4, Cacid_s, // 60
64 Cexit_1, Cexit_2, Cexit_3, Cballoon, // 64
65 Cplant, Cspring, Cspring, Cpush_spring_w, // 68
66 Cpush_spring_e, Cball_1, Cball_2, Candroid, // 72
67 Cpause, Candroid, Candroid, Candroid, // 76
68 Candroid, Candroid, Candroid, Candroid, // 80
69 Candroid, Cblank, Cblank, Cblank, // 84
70 Cblank, Cblank, Cblank, Cblank, // 88
71 Cblank, Cblank, Cblank, Cblank, // 92
72 Cblank, Cblank, Cblank, Cpush_spring_w, // 96
73 Cpush_spring_e, Cacid_1, Cacid_2, Cacid_3, // 100
74 Cacid_4, Cacid_5, Cacid_6, Cacid_7, // 104
75 Cacid_8, Cpause, Cpause, Cpause, // 108
76 Cpause, Cpause, Cpush_nut_w, Cpush_nut_e, // 112
77 Csteel_1, Cdynamite_4, Cblank, Cpush_bomb_w, // 116
78 Cpush_bomb_e, Cpush_stone_w, Cpush_stone_e, Cpause, // 120
79 Cpause, Cpause, Cpause, Cpause, // 124
81 Cblank, Croundwall_2, Cgrass, Csteel_2, // 128
82 Cwall_2, Ckey_1, Ckey_2, Ckey_3, // 132
83 Ckey_4, Cdoor_1, Cdoor_2, Cdoor_3, // 136
84 Cdoor_4, Cfake_amoeba, Cfake_door_1, Cfake_door_2, // 140
85 Cfake_door_3, Cfake_door_4, Cwonderwall, Cwheel, // 144
86 Csand, Cacid_nw, Cacid_ne, Cacid_sw, // 148
87 Cacid_se, Cfake_blank, Camoeba_1, Camoeba_2, // 152
88 Camoeba_3, Camoeba_4, Cexit, Calpha_arrow_w, // 156
89 Cfake_grass, Clenses, Cmagnify, Cfake_blank, // 160
90 Cfake_grass, Cswitch, Cswitch, Cpause, // 164
91 Cdecor_8, Cdecor_9, Cdecor_10, Cdecor_5, // 168
92 Calpha_comma, Calpha_apost, Calpha_minus, Cdynamite, // 172
93 Csteel_3, Cdecor_6, Cdecor_7, Csteel_1, // 176
94 Croundwall_1, Cdecor_2, Cdecor_4, Cdecor_3, // 180
95 Cwind_any, Cwind_e, Cwind_s, Cwind_w, // 184
96 Cwind_n, Cdirt, Cplant, Ckey_5, // 188
98 Ckey_6, Ckey_7, Ckey_8, Cdoor_5, // 192
99 Cdoor_6, Cdoor_7, Cdoor_8, Cbumper, // 196
100 Calpha_a, Calpha_b, Calpha_c, Calpha_d, // 200
101 Calpha_e, Calpha_f, Calpha_g, Calpha_h, // 204
102 Calpha_i, Calpha_j, Calpha_k, Calpha_l, // 208
103 Calpha_m, Calpha_n, Calpha_o, Calpha_p, // 212
104 Calpha_q, Calpha_r, Calpha_s, Calpha_t, // 216
105 Calpha_u, Calpha_v, Calpha_w, Calpha_x, // 220
106 Calpha_y, Calpha_z, Calpha_0, Calpha_1, // 224
107 Calpha_2, Calpha_3, Calpha_4, Calpha_5, // 228
108 Calpha_6, Calpha_7, Calpha_8, Calpha_9, // 232
109 Calpha_perio, Calpha_excla, Calpha_colon, Calpha_quest, // 236
110 Calpha_arrow_e, Cdecor_1, Cfake_door_5, Cfake_door_6, // 240
111 Cfake_door_7, Cfake_door_8, Cblank, Cblank, // 244
112 Camoeba_5, Camoeba_6, Camoeba_7, Camoeba_8, // 248
113 Cwall_1, Cblank, Calpha_copyr, Cfake_acid_1 // 252
122 android_clone_table[] =
124 { 0, Xemerald, Cemerald },
125 { 0, Xemerald_pause, Cemerald },
126 { 0, Xemerald_fall, Cemerald },
127 { 0, Yemerald_sB, Cemerald },
128 { 0, Yemerald_eB, Cemerald },
129 { 0, Yemerald_wB, Cemerald },
131 { 1, Xdiamond, Cdiamond },
132 { 1, Xdiamond_pause, Cdiamond },
133 { 1, Xdiamond_fall, Cdiamond },
134 { 1, Ydiamond_sB, Cdiamond },
135 { 1, Ydiamond_eB, Cdiamond },
136 { 1, Ydiamond_wB, Cdiamond },
138 { 2, Xstone, Cstone },
139 { 2, Xstone_pause, Cstone },
140 { 2, Xstone_fall, Cstone },
141 { 2, Ystone_sB, Cstone },
142 { 2, Ystone_eB, Cstone },
143 { 2, Ystone_wB, Cstone },
146 { 3, Xbomb_pause, Cbomb },
147 { 3, Xbomb_fall, Cbomb },
148 { 3, Ybomb_sB, Cbomb },
149 { 3, Ybomb_eB, Cbomb },
150 { 3, Ybomb_wB, Cbomb },
153 { 4, Xnut_pause, Cnut },
154 { 4, Xnut_fall, Cnut },
155 { 4, Ynut_sB, Cnut },
156 { 4, Ynut_eB, Cnut },
157 { 4, Ynut_wB, Cnut },
159 { 5, Xtank_1_n, Ctank_1_n },
160 { 5, Xtank_2_n, Ctank_1_n },
161 { 5, Ytank_nB, Ctank_1_n },
162 { 5, Ytank_n_e, Ctank_1_n },
163 { 5, Ytank_n_w, Ctank_1_n },
164 { 5, Xtank_1_e, Ctank_1_e },
165 { 5, Xtank_2_e, Ctank_1_e },
166 { 5, Ytank_eB, Ctank_1_e },
167 { 5, Ytank_e_s, Ctank_1_e },
168 { 5, Ytank_e_n, Ctank_1_e },
169 { 5, Xtank_1_s, Ctank_1_s },
170 { 5, Xtank_2_s, Ctank_1_s },
171 { 5, Ytank_sB, Ctank_1_s },
172 { 5, Ytank_s_w, Ctank_1_s },
173 { 5, Ytank_s_e, Ctank_1_s },
174 { 5, Xtank_1_w, Ctank_1_w },
175 { 5, Xtank_2_w, Ctank_1_w },
176 { 5, Ytank_wB, Ctank_1_w },
177 { 5, Ytank_w_n, Ctank_1_w },
178 { 5, Ytank_w_s, Ctank_1_w },
180 { 6, Xeater_n, Ceater_n },
181 { 6, Yeater_nB, Ceater_n },
182 { 6, Xeater_e, Ceater_e },
183 { 6, Yeater_eB, Ceater_e },
184 { 6, Xeater_s, Ceater_s },
185 { 6, Yeater_sB, Ceater_s },
186 { 6, Xeater_w, Ceater_w },
187 { 6, Yeater_wB, Ceater_w },
189 { 7, Xbug_1_n, Cbug_2_n },
190 { 7, Xbug_2_n, Cbug_2_n },
191 { 7, Ybug_nB, Cbug_2_n },
192 { 7, Ybug_n_e, Cbug_2_n },
193 { 7, Ybug_n_w, Cbug_2_n },
194 { 7, Xbug_1_e, Cbug_2_e },
195 { 7, Xbug_2_e, Cbug_2_e },
196 { 7, Ybug_eB, Cbug_2_e },
197 { 7, Ybug_e_s, Cbug_2_e },
198 { 7, Ybug_e_n, Cbug_2_e },
199 { 7, Xbug_1_s, Cbug_2_s },
200 { 7, Xbug_2_s, Cbug_2_s },
201 { 7, Ybug_sB, Cbug_2_s },
202 { 7, Ybug_s_w, Cbug_2_s },
203 { 7, Ybug_s_e, Cbug_2_s },
204 { 7, Xbug_1_w, Cbug_2_w },
205 { 7, Xbug_2_w, Cbug_2_w },
206 { 7, Ybug_wB, Cbug_2_w },
207 { 7, Ybug_w_n, Cbug_2_w },
208 { 7, Ybug_w_s, Cbug_2_w },
210 { 8, Xalien, Calien },
211 { 8, Xalien_pause, Calien },
212 { 8, Yalien_nB, Calien },
213 { 8, Yalien_eB, Calien },
214 { 8, Yalien_sB, Calien },
215 { 8, Yalien_wB, Calien },
217 { 9, Xspring, Cspring },
218 { 9, Xspring_pause, Cspring },
219 { 9, Xspring_fall, Cspring },
220 { 9, Xspring_e, Cspring },
221 { 9, Xspring_w, Cspring },
222 { 9, Yspring_sB, Cspring },
223 { 9, Yspring_eB, Cspring },
224 { 9, Yspring_wB, Cspring },
225 { 9, Yspring_alien_eB, Cspring },
226 { 9, Yspring_alien_wB, Cspring },
228 { 10, Xballoon, Cballoon },
229 { 10, Yballoon_nB, Cballoon },
230 { 10, Yballoon_eB, Cballoon },
231 { 10, Yballoon_sB, Cballoon },
232 { 10, Yballoon_wB, Cballoon },
234 { 11, Xfake_amoeba, Cdrip },
235 { 11, Yfake_amoeba, Cdrip },
236 { 11, Xamoeba_1, Cdrip },
237 { 11, Xamoeba_2, Cdrip },
238 { 11, Xamoeba_3, Cdrip },
239 { 11, Xamoeba_4, Cdrip },
240 { 11, Xamoeba_5, Cdrip },
241 { 11, Xamoeba_6, Cdrip },
242 { 11, Xamoeba_7, Cdrip },
243 { 11, Xamoeba_8, Cdrip },
245 { 12, Xdynamite, Cdynamite },
250 static int eater_offset[8] =
252 2048, 2057, 2066, 2075,
253 2112, 2121, 2130, 2139
256 void convert_em_level(unsigned char *src, int file_version)
260 /* common to all emc caves */
265 cav.time_seconds = MIN(GET_BE16(src[2110]), 9999);
266 cav.gems_needed = src[2095];
269 cav.testmode = FALSE;
270 cav.teamwork = (src[2150] & 128) != 0;
274 cav.emerald_score = src[2084];
275 cav.diamond_score = src[2085];
276 cav.alien_score = src[2086];
277 cav.tank_score = src[2087];
278 cav.bug_score = src[2088];
279 cav.eater_score = src[2089];
280 cav.nut_score = src[2090];
281 cav.dynamite_score = src[2091];
282 cav.key_score = src[2092];
283 cav.exit_score = src[2093] * 8 / 5;
285 cav.lenses_score = src[2151];
286 cav.magnify_score = src[2152];
287 cav.slurp_score = src[2153];
291 cav.android_move_time = MIN(GET_BE16(src[2164]), 9999);
292 cav.android_clone_time = MIN(GET_BE16(src[2166]), 9999);
293 cav.ball_time = MIN(GET_BE16(src[2160]), 9999);
295 cav.lenses_time = MIN(GET_BE16(src[2154]), 9999);
296 cav.magnify_time = MIN(GET_BE16(src[2156]), 9999);
297 cav.wheel_time = MIN(GET_BE16(src[2104]), 9999);
299 cav.amoeba_time = MIN(GET_BE16(src[2100]) * 28, 9999);
300 cav.wonderwall_time = MIN(GET_BE16(src[2102]), 9999);
302 cav.wind_time = 9999;
304 cav.wind_direction = (temp & 8 ? 0 :
311 cav.ball_random = (src[2162] & 1) != 0;
312 cav.ball_active = (src[2162] & 128) != 0;
314 cav.wonderwall_active = FALSE;
315 cav.wheel_active = FALSE;
316 cav.lenses_active = FALSE;
317 cav.magnify_active = FALSE;
319 for (temp = 1; temp < 2047; temp++)
323 case 36: /* wonderwall */
324 cav.wonderwall_active = TRUE;
325 cav.wonderwall_time = 9999;
329 cav.wheel_active = TRUE;
330 cav.wheel_x = temp % 64;
331 cav.wheel_y = temp / 64;
334 case 163: /* fake blank */
335 cav.lenses_active = TRUE;
338 case 164: /* fake grass */
339 cav.magnify_active = TRUE;
346 temp = GET_BE16(src[2168]);
348 for (i = 0; i < GAME_TILE_MAX; i++)
349 cav.android_array[i] = Cblank;
351 for (i = 0; android_clone_table[i].bit_nr != -1; i++)
352 if (temp & (1 << android_clone_table[i].bit_nr))
353 cav.android_array[android_clone_table[i].clone_from] =
354 android_clone_table[i].clone_to;
358 for (i = 0; i < 8; i++)
359 for (x = 0; x < 9; x++)
360 cav.eater_array[i][x] = map_emc[src[eater_offset[i] + x]];
364 temp = map_emc[src[2159]];
366 for (y = 0; y < 8; y++)
370 for (x = 0; x < 8; x++)
371 cav.ball_array[y][x] = temp;
375 cav.ball_array[y][1] = (src[2163] & 1) ? temp : Cblank; /* north */
376 cav.ball_array[y][6] = (src[2163] & 2) ? temp : Cblank; /* south */
377 cav.ball_array[y][3] = (src[2163] & 4) ? temp : Cblank; /* west */
378 cav.ball_array[y][4] = (src[2163] & 8) ? temp : Cblank; /* east */
379 cav.ball_array[y][7] = (src[2163] & 16) ? temp : Cblank; /* southeast */
380 cav.ball_array[y][5] = (src[2163] & 32) ? temp : Cblank; /* southwest */
381 cav.ball_array[y][2] = (src[2163] & 64) ? temp : Cblank; /* northeast */
382 cav.ball_array[y][0] = (src[2163] & 128)? temp : Cblank; /* northwest */
388 for (i = 0; i < 2; i++)
390 temp = GET_BE16(src[2096 + i * 2]);
392 cav.player_x[i] = (temp & 63);
393 cav.player_y[i] = (temp >> 6 & 31);
398 /* first fill the complete playfield with the empty space element */
399 for (y = 0; y < CAVE_HEIGHT; y++)
400 for (x = 0; x < CAVE_WIDTH; x++)
401 cav.cave[x][y] = Cblank;
403 /* then copy the real level contents from level file into the playfield */
405 for (y = 0; y < cav.height; y++)
406 for (x = 0; x < cav.width; x++)
407 cav.cave[x][y] = map_emc[src[temp++]];
409 native_em_level.file_version = file_version;
413 /* convert all emerald mine caves to emc version 6 cave format.
415 * caves are filtered to get rid of invalid or unsupported tiles.
417 * although the result is a somewhat clean cave, it is meant only
418 * to give a common structure for the binary cave format. it is not
419 * for archiving purposes (it is better to keep the raw cave as-is)
420 * and it is not meant for serializing (the intermediate structure
421 * is better defined).
423 * acid top is added to acid bottom in both the cave and the eaters.
424 * fake acid (only in v4) does not exist because it adds nothing to
425 * the game, and is different even in different versions of the emc
428 * v4/v5 time is converted to 10x old time (as it should be).
429 * the reason the kingsoft player used 5x time was copy protection.
431 * note: emc v6 converter has an error in converting v4 eaters to the
432 * wrong bug(24 instead of 20) and tank(12 instead of 8).
435 /* changes for game engine integration in Rocks'n'Diamonds:
437 * cave versions below V5 used different objects than later versions
438 * - steel/wall/roundwall of type 1 was used instead of type 2
439 * - dirt was used instead of grass
440 * - wall codes 129, 131 and 132 are changed to 180, 179 and 252
441 * - object code 130 (V6 grass) is changed to 189 (V6 dirt)
442 * - object codes are changed in both cave and eater arrays
443 * - only graphical change, as changed objects behave the same
445 * acid with no base beneath it is converted to fake acid
446 * - required for downunder mine 16, level 0 (and others)
449 static const unsigned char map_v6[256] =
453 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
454 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
455 0,16,2,18, 36,37,37,37, 40,41,42,43, 44,45,128,128, // 32
456 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
458 64,65,66,67, 68,69,69,71, 72,73,74,75, 118,75,75,75, // 64
459 75,75,75,75, 75,153,153,153, 153,153,153,153, 153,153,153,153, // 80
460 153,153,153,99, 100,68,68,68, 68,68,68,68, 68,118,118,118, // 96
461 118,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118, // 112
463 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
464 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
465 160,161,162,163, 164,165,165,118, 168,169,170,171, 172,173,174,175, // 160
466 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,191, // 176
468 192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207, // 192
469 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
470 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
471 240,241,242,243, 244,245,153,153, 153,153,153,153, 153,153,153,153 // 240
474 static const unsigned char map_v5[256] =
478 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
479 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
480 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
481 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
483 64,65,66,67, 68,153,153,153, 153,153,153,153, 153,153,153,153, // 64
484 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
485 153,153,153,153, 153,68,68,68, 68,68,68,68, 68,118,118,118, // 96
486 118,118,114,115, 131,62,118,119, 120,121,122,118, 118,118,118,118, // 112
488 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
489 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
490 160,153,153,153, 153,153,153,118, 168,169,170,171, 172,173,174,175, // 160
491 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,153, // 176
493 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
494 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
495 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
496 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
499 static const unsigned char map_v4[256] =
503 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
504 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
505 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
506 128,148,148,148, 45,45,45,148, 0,153,153,59, 60,61,62,63, // 48
508 64,65,66,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 64
509 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
510 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 96
511 153,118,114,115, 179,62,118,119, 120,121,122,118, 118,118,118,118, // 112
513 128,180,189,179, 252,133,134,135, 136,137,138,139, 140,141,142,143, // 128
514 144,145,146,147, 148,149,150,151, 152,68,248,249, 250,251,158,160, // 144
515 160,160,160,160, 160,160,160,160, 160,160,160,160, 160,160,160,175, // 160
516 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,68,153, // 176
518 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
519 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
520 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
521 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
524 static const unsigned char map_v4_eater[32] =
526 /* filter for v4 eater */
528 128,18,2,0, 4,8,16,20, 28,37,41,45, 189,180,179,252, // 0
529 133,134,135,136, 146,147,175,65, 66,64,2,18, 128,128,128,128 // 16
532 static boolean filename_has_v1_format(char *filename)
534 char *basename = getBaseNamePtr(filename);
536 return (strlen(basename) == 3 &&
537 basename[0] == 'a' &&
538 basename[1] >= 'a' && basename[1] <= 'k' &&
539 basename[2] >= '0' && basename[2] <= '9');
542 int cleanup_em_level(unsigned char *src, int length, char *filename)
544 int file_version = FILE_VERSION_EM_UNKNOWN;
547 if (length >= 2172 &&
548 src[2106] == 255 && /* version id: */
549 src[2107] == 54 && /* '6' */
550 src[2108] == 48 && /* '0' */
551 src[2109] == 48) /* '0' */
553 /* ---------- this cave has V6 file format ---------- */
554 file_version = FILE_VERSION_EM_V6;
556 /* remap elements to internal EMC level format */
557 for (i = 0; i < 2048; i++) /* cave */
558 src[i] = map_v6[src[i]];
559 for (i = 2048; i < 2084; i++) /* eaters */
560 src[i] = map_v6[src[i]];
561 for (i = 2112; i < 2148; i++) /* eaters */
562 src[i] = map_v6[src[i]];
564 else if (length >= 2110 &&
565 src[2106] == 255 && /* version id: */
566 src[2107] == 53 && /* '5' */
567 src[2108] == 48 && /* '0' */
568 src[2109] == 48) /* '0' */
570 /* ---------- this cave has V5 file format ---------- */
571 file_version = FILE_VERSION_EM_V5;
573 /* remap elements to internal EMC level format */
574 for (i = 0; i < 2048; i++) /* cave */
575 src[i] = map_v5[src[i]];
576 for (i = 2048; i < 2084; i++) /* eaters */
577 src[i] = map_v5[src[i]];
578 for (i = 2112; i < 2148; i++) /* eaters */
579 src[i] = src[i - 64];
581 else if (length >= 2106 &&
582 (src[1983] == 27 || /* encrypted (only EM I/II/III) */
583 src[1983] == 116 || /* unencrypted (usual case) */
584 src[1983] == 131)) /* unencrypted (rare case) */
586 /* ---------- this cave has V1, V2 or V3 file format ---------- */
588 boolean fix_copyright = FALSE;
591 byte at position 1983 (0x07bf) is used as "magic byte":
592 - 27 (0x1b) => encrypted level (V3 only / Kingsoft original games)
593 - 116 (0x74) => unencrypted level (byte is corrected to 131 (0x83))
594 - 131 (0x83) => unencrypted level (happens only in very rare cases)
597 if (src[1983] == 27) /* (0x1b) -- after decryption: 116 (0x74) */
599 /* this is original (encrypted) Emerald Mine I, II or III level file */
601 int first_byte = src[0];
602 unsigned char code0 = 0x65;
603 unsigned char code1 = 0x11;
605 /* decode encrypted level data */
606 for (i = 0; i < 2106; i++)
611 code0 = (code0 + 7) & 0xff;
614 src[1] = 131; /* needed for all Emerald Mine levels */
616 /* first byte is either 0xf1 (EM I and III) or 0xf5 (EM II) */
617 if (first_byte == 0xf5)
619 src[0] = 131; /* only needed for Emerald Mine II levels */
621 fix_copyright = TRUE;
624 /* ---------- this cave has V3 file format ---------- */
625 file_version = FILE_VERSION_EM_V3;
627 else if (filename_has_v1_format(filename))
629 /* ---------- this cave has V1 file format ---------- */
630 file_version = FILE_VERSION_EM_V1;
634 /* ---------- this cave has V2 file format ---------- */
635 file_version = FILE_VERSION_EM_V2;
638 /* remap elements to internal EMC level format */
639 for (i = 0; i < 2048; i++) /* cave */
640 src[i] = map_v4[src[i]];
641 for (i = 2048; i < 2084; i++) /* eaters */
642 src[i] = map_v4_eater[src[i] % 32];
643 for (i = 2112; i < 2148; i++) /* eaters */
644 src[i] = src[i - 64];
646 if (fix_copyright) /* fix "(c)" sign in Emerald Mine II levels */
648 for (i = 0; i < 2048; i++)
650 src[i] = 254; /* replace 'Cdecor_1' with 'Calpha_copyr' */
655 /* ---------- this cave has unknown file format ---------- */
657 /* if file has length of old-style level file, print (wrong) magic byte */
659 Error(ERR_WARN, "unknown magic byte 0x%02x at position 0x%04x",
662 return FILE_VERSION_EM_UNKNOWN;
665 if (file_version < FILE_VERSION_EM_V6)
668 src[2106] = 255; /* version id: */
669 src[2107] = 54; /* '6' */
670 src[2108] = 48; /* '0' */
671 src[2109] = 48; /* '0' */
675 /* stored level time of levels for the V2 player was changed to 50% of the
676 time for the V1 player (original V3 levels already considered this) */
677 if (file_version != FILE_VERSION_EM_V1 &&
678 file_version != FILE_VERSION_EM_V3)
683 for (i = 2148; i < 2172; i++)
690 /* ---------- at this stage, the cave data always has V6 format ---------- */
693 for (i = 0; i < 2048; i++)
696 for (i++; i < 2048; i++)
701 for (i = 64; i < 2048; i++)
702 if (src[i] == 63) /* replace element above 'Cacid_s' ... */
703 src[i - 64] = 101; /* ... with 'Cacid_1' */
705 /* fix acid with no base beneath it (see comment above for details) */
706 for (i = 64; i < 2048 - 1; i++)
708 if (file_version <= FILE_VERSION_EM_V2 &&
709 src[i - 64] == 101 && src[i] != 63) /* acid without base */
711 if (src[i - 1] == 101 || /* remove acid over acid row */
713 src[i - 64] = 6; /* replace element above with 'Cblank' */
715 src[i - 64] = 255; /* replace element above with 'Cfake_acid_1' */
719 /* fix acid in eaters */
720 for (i = 0; i < 8; i++)
721 for (j = 0; j < 6; j++)
722 if (src[eater_offset[i] + j + 3] == 63)
723 src[eater_offset[i] + j] = 101;
728 /* set cave tile at player position to blank */
729 for (i = 0; i < 2; i++)
730 src[GET_BE16(src[2096 + i * 2]) % 2048] = 128;
740 src[2159] = map_v6[src[2159]];
750 /* size of v6 cave */
754 Error(ERR_DEBUG, "EM level file version: %d", file_version);
763 structure of emerald mine level disk files
764 ----------------------------------------------------------------------
766 if(len >= 2172 && (buf[2106] == 255 && buf[2107] == 54 && buf[2108] == 48 && buf[2109] == 48)) // v6
767 if(len >= 2110 && (buf[2106] == 255 && buf[2107] == 53 && buf[2108] == 48 && buf[2109] == 48)) // v5
768 if(len >= 2106 && (buf[1983] == 116 || buf[2047] == 116)) // v4
769 if(len >= 2106 && (buf[1983] == 27 || buf[2047] == 219)) // v3
771 buf[0]=241;buf[1]=248;for(i=0,j=101;i<2106;i++,j+=7)buf[i]=(buf[i]^j)-17; // decrypt
773 number of movements (calls to logic) = time * 50 / 8
775 {} reserved (but some broken levels use them)
777 ----------------------------------------------------------------------
797 2095: emeralds needed
801 2102: wonderwall time
803 2106: ID (0xff363030)
809 2148: flags bit#7=NOI #6=RIS
810 2149: wind direction bit#0=right #1=down #2=left #3=up
811 2150: cave number bit#7=teamwork
820 2162: ball info bit#15=switch state #8=random
821 ; bit#0=N #1=S #2=W #3=E #4=SE #5=SW #6=NE #7=NW
822 2164: android move speed
823 2166: android clone speed
825 ; bit#0=emerald #1=diamond #2=stone #3=bomb #4=nut #5=tank #6=eater
826 ; #7=bug #8=alien #9=spring #10=balloon #11=ameuba #12=dynamite
831 1: stone {stone_fall}
833 3: diamond {diamond_fall}
835 5: alien {alien_pause}
849 19: emerald {emerald_fall}
860 30: blank {drip_stretchB}
861 31: drip {drip_stretch}
862 32: stone {stone_pause}
863 33: bomb {bomb_pause}
864 34: diamond {diamond_pause}
865 35: emerald {emerald_pause}
866 36: wonderwall {wonderwallB}
876 46: blank {sand_stonein_2}
877 47: blank {sand_stonein_3}
878 48: blank {sand_stonein_4}
879 49: sand {sand_stonesand_2}
880 50: sand {sand_stonesand_3}
881 51: sand {sand_stonesand_4}
882 52: sand_stone {sand_sandstone_2}
883 53: sand_stone {sand_sandstone_3}
884 54: sand_stone {sand_sandstone_4}
885 55: sand {sand_stonesand_4}
886 56: stone {sand_stoneout_2}
900 70: spring {spring_fall}
901 71: spring {spring_w}
902 72: spring {spring_e}
907 77: android {android_n_1}
908 78: android {android_n_2}
909 79: android {android_s_1}
910 80: android {android_s_2}
911 81: android {android_e_1}
912 82: android {android_e_2}
913 83: android {android_w_1}
914 84: android {android_w_2}
929 99: spring {push_spring_w}
930 100: spring {push_spring_e}
939 109: pause {grass_wB}
940 110: pause {grass_eB}
941 111: pause {grass_nB}
942 112: pause {grass_sB}
943 113: pause {dynamite_blank}
944 114: nut {push_nut_w}
945 115: nut {push_nut_e}
946 116: steel_2 {end of level}
948 118: pause {emerald_blank}
949 119: bomb {push_bomb_w}
950 120: bomb {push_bomb_e}
951 121: stone {push_stone_w}
952 122: stone {push_stone_e}
953 123: pause {diamond_blank}
994 163: fake_blank {fake_blankB}
995 164: fake_grass {fake_grassB}
997 166: switch {switchB}
1088 ----------------------------------------------------------------------
1104 2091: dynamite value
1108 2095: emeralds needed
1112 2102: wonderwall time
1114 2106: ID (0xff353030)
1118 1: stone {stone_fall}
1120 3: diamond {diamond_fall}
1122 5: alien {alien_pause}
1134 17: bomb {bomb_fall}
1136 19: emerald {emerald_fall}
1146 29: drip {drip_fall}
1147 30: blank {drip_stretchB}
1148 31: drip {drip_stretch}
1149 32: stone {stone_pause}
1150 33: bomb {bomb_pause}
1151 34: diamond {diamond_pause}
1152 35: emerald {emerald_pause}
1153 36: wonderwall {wonderwallB}
1163 46: blank {sand_stonein_2}
1164 47: blank {sand_stonein_3}
1165 48: blank {sand_stonein_4}
1166 49: sand {sand_stonesand_2}
1167 50: sand {sand_stonesand_3}
1168 51: sand {sand_stonesand_4}
1169 52: sand_stone {sand_sandstone_2}
1170 53: sand_stone {sand_sandstone_3}
1171 54: sand_stone {sand_sandstone_4}
1172 55: sand {sand_stonesand_4}
1173 56: stone {sand_stoneout_2}
1226 109: pause {grass_wB}
1227 110: pause {grass_eB}
1228 111: pause {grass_nB}
1229 112: pause {grass_sB}
1230 113: pause {dynamite_blank}
1231 114: nut {push_nut_w}
1232 115: nut {push_nut_e}
1233 116: steel_2 {end of level}
1234 117: dynamite_4 {boom_2}
1235 118: pause {emerald_blank}
1236 119: bomb {push_bomb_w}
1237 120: bomb {push_bomb_e}
1238 121: stone {push_stone_w}
1239 122: stone {push_stone_e}
1240 123: pause {diamond_blank}
1241 124: pause {dirt_wB}
1242 125: pause {dirt_eB}
1243 126: pause {dirt_nB}
1244 127: pause {dirt_sB}
1375 ----------------------------------------------------------------------
1391 2091: dynamite value
1395 2095: emeralds needed
1399 2102: wonderwall time
1429 26: diamond {diamond_shine}
1430 27: emerald {emerald_shine}
1433 1: stone {stone_fall}
1435 3: diamond {diamond_fall}
1437 5: alien {alien_pause}
1449 17: bomb {bomb_fall}
1451 19: emerald {emerald_fall}
1461 29: drip {drip_fall}
1462 30: blank {drip_stretchB}
1463 31: drip {drip_stretch}
1464 32: stone {stone_pause}
1465 33: bomb {bomb_pause}
1466 34: diamond {diamond_pause}
1467 35: emerald {emerald_pause}
1468 36: wonderwall {wonderwallB}
1478 46: blank {sand_stonein_2}
1479 47: blank {sand_stonein_3}
1480 48: blank {sand_stonein_4}
1481 49: sand {sand_stonesand_2}
1482 50: sand {sand_stonesand_3}
1483 51: sand {sand_stonesand_4}
1484 52: sand_stone {sand_sandstone_2}
1485 53: sand_stone {sand_sandstone_3}
1486 54: sand_stone {sand_sandstone_4}
1487 55: sand {sand_stonesand_4}
1488 56: stone {sand_stoneout_2}
1545 113: pause {dynamite_blank}
1546 114: nut {push_nut_w}
1547 115: nut {push_nut_e}
1548 116: steel_1 {end of level}
1549 117: dynamite_4 {boom_2}
1550 118: pause {emerald_blank}
1551 119: bomb {push_bomb_w}
1552 120: bomb {push_bomb_e}
1553 121: stone {push_stone_w}
1554 122: stone {push_stone_e}
1555 123: pause {diamond_blank}
1556 124: pause {dirt_wB}
1557 125: pause {dirt_eB}
1558 126: pause {dirt_nB}
1559 127: pause {dirt_sB}
1592 159: fake_grass {dirt}
1593 160: fake_grass {dirt}
1594 161: fake_grass {dirt}
1595 162: fake_grass {dirt}
1596 163: fake_grass {dirt}
1597 164: fake_grass {dirt}
1598 165: fake_grass {dirt}
1599 166: fake_grass {dirt}
1600 167: fake_grass {dirt}
1601 168: fake_grass {dirt}
1602 169: fake_grass {dirt}
1603 170: fake_grass {dirt}
1604 171: fake_grass {dirt}
1605 172: fake_grass {dirt}
1606 173: fake_grass {dirt}
1607 174: fake_grass {dirt}
1673 240: alpha_arrow_e {}
1674 241: decor_1 {alpha_copyr}
1690 ----------------------------------------------------------------------