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 /* changes for game engine integration in Rocks'n'Diamonds:
45 * added support for rolling spring (not mapped to pushed spring)
46 * - required for Elvis Mine 8, levels 3, 23, 48 and 73
49 #define GET_BE16(x) ((&x)[0] << 8 | (&x)[1])
51 static const short map_emc_raw[256] =
53 Cstone, Cstone, Cdiamond, Cdiamond, // 0
54 Calien, Calien, Cpause, Cpause, // 4
55 Ctank_1_n, Ctank_1_e, Ctank_1_s, Ctank_1_w, // 8
56 Ctank_2_n, Ctank_2_e, Ctank_2_s, Ctank_2_w, // 12
57 Cbomb, Cbomb, Cemerald, Cemerald, // 16
58 Cbug_1_n, Cbug_1_e, Cbug_1_s, Cbug_1_w, // 20
59 Cbug_2_n, Cbug_2_e, Cbug_2_s, Cbug_2_w, // 24
60 Cdrip, Cdrip, Cblank, Cdrip, // 28
61 Cstone, Cbomb, Cdiamond, Cemerald, // 32
62 Cwonderwall, Cnut, Cnut, Cnut, // 36
63 Cwheel, Ceater_n, Ceater_s, Ceater_w, // 40
64 Ceater_e, Csand_stone, Cblank, Cblank, // 44
65 Cblank, Csand, Csand, Csand, // 48
66 Csand_stone, Csand_stone, Csand_stone, Csand, // 52
67 Cstone, Cslide_ew, Cslide_ns, Cdynamite_1, // 56
68 Cdynamite_2, Cdynamite_3, Cdynamite_4, Cacid_s, // 60
70 Cexit_1, Cexit_2, Cexit_3, Cballoon, // 64
71 Cplant, Cspring, Cspring, Cspring_w, // 68
72 Cspring_e, Cball_1, Cball_2, Candroid, // 72
73 Cpause, Candroid, Candroid, Candroid, // 76
74 Candroid, Candroid, Candroid, Candroid, // 80
75 Candroid, Cblank, Cblank, Cblank, // 84
76 Cblank, Cblank, Cblank, Cblank, // 88
77 Cblank, Cblank, Cblank, Cblank, // 92
78 Cblank, Cblank, Cblank, Cpush_spring_w, // 96
79 Cpush_spring_e, Cacid_1, Cacid_2, Cacid_3, // 100
80 Cacid_4, Cacid_5, Cacid_6, Cacid_7, // 104
81 Cacid_8, Cpause, Cpause, Cpause, // 108
82 Cpause, Cpause, Cpush_nut_w, Cpush_nut_e, // 112
83 Csteel_1, Cdynamite_4, Cblank, Cpush_bomb_w, // 116
84 Cpush_bomb_e, Cpush_stone_w, Cpush_stone_e, Cpause, // 120
85 Cpause, Cpause, Cpause, Cpause, // 124
87 Cblank, Croundwall_2, Cgrass, Csteel_2, // 128
88 Cwall_2, Ckey_1, Ckey_2, Ckey_3, // 132
89 Ckey_4, Cdoor_1, Cdoor_2, Cdoor_3, // 136
90 Cdoor_4, Cfake_amoeba, Cfake_door_1, Cfake_door_2, // 140
91 Cfake_door_3, Cfake_door_4, Cwonderwall, Cwheel, // 144
92 Csand, Cacid_nw, Cacid_ne, Cacid_sw, // 148
93 Cacid_se, Cfake_blank, Camoeba_1, Camoeba_2, // 152
94 Camoeba_3, Camoeba_4, Cexit, Calpha_arrow_w, // 156
95 Cfake_grass, Clenses, Cmagnify, Cfake_blank, // 160
96 Cfake_grass, Cswitch, Cswitch, Cpause, // 164
97 Cdecor_8, Cdecor_9, Cdecor_10, Cdecor_5, // 168
98 Calpha_comma, Calpha_apost, Calpha_minus, Cdynamite, // 172
99 Csteel_3, Cdecor_6, Cdecor_7, Csteel_1, // 176
100 Croundwall_1, Cdecor_2, Cdecor_4, Cdecor_3, // 180
101 Cwind_any, Cwind_e, Cwind_s, Cwind_w, // 184
102 Cwind_n, Cdirt, Cplant, Ckey_5, // 188
104 Ckey_6, Ckey_7, Ckey_8, Cdoor_5, // 192
105 Cdoor_6, Cdoor_7, Cdoor_8, Cbumper, // 196
106 Calpha_a, Calpha_b, Calpha_c, Calpha_d, // 200
107 Calpha_e, Calpha_f, Calpha_g, Calpha_h, // 204
108 Calpha_i, Calpha_j, Calpha_k, Calpha_l, // 208
109 Calpha_m, Calpha_n, Calpha_o, Calpha_p, // 212
110 Calpha_q, Calpha_r, Calpha_s, Calpha_t, // 216
111 Calpha_u, Calpha_v, Calpha_w, Calpha_x, // 220
112 Calpha_y, Calpha_z, Calpha_0, Calpha_1, // 224
113 Calpha_2, Calpha_3, Calpha_4, Calpha_5, // 228
114 Calpha_6, Calpha_7, Calpha_8, Calpha_9, // 232
115 Calpha_perio, Calpha_excla, Calpha_colon, Calpha_quest, // 236
116 Calpha_arrow_e, Cdecor_1, Cfake_door_5, Cfake_door_6, // 240
117 Cfake_door_7, Cfake_door_8, Cblank, Cblank, // 244
118 Camoeba_5, Camoeba_6, Camoeba_7, Camoeba_8, // 248
119 Cwall_1, Cblank, Calpha_copyr, Cfake_acid_1 // 252
122 static const short swap_emc[CAVE_TILE_MAX] =
127 [Csteel_1] = Csteel_2,
128 [Csteel_2] = Csteel_1,
133 [Croundwall_1] = Croundwall_2,
134 [Croundwall_2] = Croundwall_1
143 android_clone_table[] =
145 { 0, Xemerald, Cemerald },
146 { 0, Xemerald_pause, Cemerald },
147 { 0, Xemerald_fall, Cemerald },
148 { 0, Yemerald_sB, Cemerald },
149 { 0, Yemerald_eB, Cemerald },
150 { 0, Yemerald_wB, Cemerald },
152 { 1, Xdiamond, Cdiamond },
153 { 1, Xdiamond_pause, Cdiamond },
154 { 1, Xdiamond_fall, Cdiamond },
155 { 1, Ydiamond_sB, Cdiamond },
156 { 1, Ydiamond_eB, Cdiamond },
157 { 1, Ydiamond_wB, Cdiamond },
159 { 2, Xstone, Cstone },
160 { 2, Xstone_pause, Cstone },
161 { 2, Xstone_fall, Cstone },
162 { 2, Ystone_sB, Cstone },
163 { 2, Ystone_eB, Cstone },
164 { 2, Ystone_wB, Cstone },
167 { 3, Xbomb_pause, Cbomb },
168 { 3, Xbomb_fall, Cbomb },
169 { 3, Ybomb_sB, Cbomb },
170 { 3, Ybomb_eB, Cbomb },
171 { 3, Ybomb_wB, Cbomb },
174 { 4, Xnut_pause, Cnut },
175 { 4, Xnut_fall, Cnut },
176 { 4, Ynut_sB, Cnut },
177 { 4, Ynut_eB, Cnut },
178 { 4, Ynut_wB, Cnut },
180 { 5, Xtank_1_n, Ctank_1_n },
181 { 5, Xtank_2_n, Ctank_1_n },
182 { 5, Ytank_nB, Ctank_1_n },
183 { 5, Ytank_n_e, Ctank_1_n },
184 { 5, Ytank_n_w, Ctank_1_n },
185 { 5, Xtank_1_e, Ctank_1_e },
186 { 5, Xtank_2_e, Ctank_1_e },
187 { 5, Ytank_eB, Ctank_1_e },
188 { 5, Ytank_e_s, Ctank_1_e },
189 { 5, Ytank_e_n, Ctank_1_e },
190 { 5, Xtank_1_s, Ctank_1_s },
191 { 5, Xtank_2_s, Ctank_1_s },
192 { 5, Ytank_sB, Ctank_1_s },
193 { 5, Ytank_s_w, Ctank_1_s },
194 { 5, Ytank_s_e, Ctank_1_s },
195 { 5, Xtank_1_w, Ctank_1_w },
196 { 5, Xtank_2_w, Ctank_1_w },
197 { 5, Ytank_wB, Ctank_1_w },
198 { 5, Ytank_w_n, Ctank_1_w },
199 { 5, Ytank_w_s, Ctank_1_w },
201 { 6, Xeater_n, Ceater_n },
202 { 6, Yeater_nB, Ceater_n },
203 { 6, Xeater_e, Ceater_e },
204 { 6, Yeater_eB, Ceater_e },
205 { 6, Xeater_s, Ceater_s },
206 { 6, Yeater_sB, Ceater_s },
207 { 6, Xeater_w, Ceater_w },
208 { 6, Yeater_wB, Ceater_w },
210 { 7, Xbug_1_n, Cbug_2_n },
211 { 7, Xbug_2_n, Cbug_2_n },
212 { 7, Ybug_nB, Cbug_2_n },
213 { 7, Ybug_n_e, Cbug_2_n },
214 { 7, Ybug_n_w, Cbug_2_n },
215 { 7, Xbug_1_e, Cbug_2_e },
216 { 7, Xbug_2_e, Cbug_2_e },
217 { 7, Ybug_eB, Cbug_2_e },
218 { 7, Ybug_e_s, Cbug_2_e },
219 { 7, Ybug_e_n, Cbug_2_e },
220 { 7, Xbug_1_s, Cbug_2_s },
221 { 7, Xbug_2_s, Cbug_2_s },
222 { 7, Ybug_sB, Cbug_2_s },
223 { 7, Ybug_s_w, Cbug_2_s },
224 { 7, Ybug_s_e, Cbug_2_s },
225 { 7, Xbug_1_w, Cbug_2_w },
226 { 7, Xbug_2_w, Cbug_2_w },
227 { 7, Ybug_wB, Cbug_2_w },
228 { 7, Ybug_w_n, Cbug_2_w },
229 { 7, Ybug_w_s, Cbug_2_w },
231 { 8, Xalien, Calien },
232 { 8, Xalien_pause, Calien },
233 { 8, Yalien_nB, Calien },
234 { 8, Yalien_eB, Calien },
235 { 8, Yalien_sB, Calien },
236 { 8, Yalien_wB, Calien },
238 { 9, Xspring, Cspring },
239 { 9, Xspring_pause, Cspring },
240 { 9, Xspring_fall, Cspring },
241 { 9, Xspring_e, Cspring },
242 { 9, Xspring_w, Cspring },
243 { 9, Yspring_sB, Cspring },
244 { 9, Yspring_eB, Cspring },
245 { 9, Yspring_wB, Cspring },
246 { 9, Yspring_alien_eB, Cspring },
247 { 9, Yspring_alien_wB, Cspring },
249 { 10, Xballoon, Cballoon },
250 { 10, Yballoon_nB, Cballoon },
251 { 10, Yballoon_eB, Cballoon },
252 { 10, Yballoon_sB, Cballoon },
253 { 10, Yballoon_wB, Cballoon },
255 { 11, Xfake_amoeba, Cdrip },
256 { 11, Yfake_amoeba, Cdrip },
257 { 11, Xamoeba_1, Cdrip },
258 { 11, Xamoeba_2, Cdrip },
259 { 11, Xamoeba_3, Cdrip },
260 { 11, Xamoeba_4, Cdrip },
261 { 11, Xamoeba_5, Cdrip },
262 { 11, Xamoeba_6, Cdrip },
263 { 11, Xamoeba_7, Cdrip },
264 { 11, Xamoeba_8, Cdrip },
266 { 12, Xdynamite, Cdynamite },
271 static int eater_offset[8] =
273 2048, 2057, 2066, 2075,
274 2112, 2121, 2130, 2139
277 void convert_em_level(unsigned char *src, int file_version)
282 /* initialize element mapping */
284 for (i = 0; i < 256; i++)
285 map_emc[i] = map_emc_raw[i];
287 /* swap tiles for pre-EMC caves (older than V5/V6), if needed */
289 if (swapTiles_EM(file_version < FILE_VERSION_EM_V5))
290 for (i = 0; i < 256; i++)
291 if (swap_emc[map_emc[i]] != 0)
292 map_emc[i] = swap_emc[map_emc[i]];
294 /* common to all emc caves */
299 cav.time_seconds = MIN(GET_BE16(src[2110]), 9999);
300 cav.gems_needed = src[2095];
302 cav.infinite = game_em.use_wrap_around;
303 cav.testmode = FALSE;
304 cav.teamwork = (src[2150] & 128) != 0;
308 cav.emerald_score = src[2084];
309 cav.diamond_score = src[2085];
310 cav.alien_score = src[2086];
311 cav.tank_score = src[2087];
312 cav.bug_score = src[2088];
313 cav.eater_score = src[2089];
314 cav.nut_score = src[2090];
315 cav.dynamite_score = src[2091];
316 cav.key_score = src[2092];
317 cav.exit_score = src[2093] * 8 / 5;
319 cav.lenses_score = src[2151];
320 cav.magnify_score = src[2152];
321 cav.slurp_score = src[2153];
325 cav.android_move_time = MIN(GET_BE16(src[2164]), 9999);
326 cav.android_clone_time = MIN(GET_BE16(src[2166]), 9999);
327 cav.ball_time = MIN(GET_BE16(src[2160]), 9999);
329 cav.lenses_time = MIN(GET_BE16(src[2154]), 9999);
330 cav.magnify_time = MIN(GET_BE16(src[2156]), 9999);
331 cav.wheel_time = MIN(GET_BE16(src[2104]), 9999);
333 cav.amoeba_time = MIN(GET_BE16(src[2100]) * 28, 9999);
334 cav.wonderwall_time = MIN(GET_BE16(src[2102]), 9999);
336 cav.wind_time = 9999;
338 cav.wind_direction = (temp & 8 ? 0 :
345 cav.ball_random = (src[2162] & 1) != 0;
346 cav.ball_active = (src[2162] & 128) != 0;
348 cav.wonderwall_active = FALSE;
349 cav.wheel_active = FALSE;
350 cav.lenses_active = FALSE;
351 cav.magnify_active = FALSE;
353 for (temp = 1; temp < 2047; temp++)
357 case 36: /* wonderwall */
358 cav.wonderwall_active = TRUE;
359 cav.wonderwall_time = 9999;
363 cav.wheel_active = TRUE;
364 cav.wheel_x = temp % 64;
365 cav.wheel_y = temp / 64;
368 case 163: /* fake blank */
369 cav.lenses_active = TRUE;
372 case 164: /* fake grass */
373 cav.magnify_active = TRUE;
380 temp = GET_BE16(src[2168]);
382 for (i = 0; i < GAME_TILE_MAX; i++)
383 cav.android_array[i] = Cblank;
385 for (i = 0; android_clone_table[i].bit_nr != -1; i++)
386 if (temp & (1 << android_clone_table[i].bit_nr))
387 cav.android_array[android_clone_table[i].clone_from] =
388 android_clone_table[i].clone_to;
392 for (i = 0; i < 8; i++)
393 for (x = 0; x < 9; x++)
394 cav.eater_array[i][x] = map_emc[src[eater_offset[i] + x]];
398 temp = map_emc[src[2159]];
400 for (y = 0; y < 8; y++)
404 for (x = 0; x < 8; x++)
405 cav.ball_array[y][x] = temp;
409 cav.ball_array[y][1] = (src[2163] & 1) ? temp : Cblank; /* north */
410 cav.ball_array[y][6] = (src[2163] & 2) ? temp : Cblank; /* south */
411 cav.ball_array[y][3] = (src[2163] & 4) ? temp : Cblank; /* west */
412 cav.ball_array[y][4] = (src[2163] & 8) ? temp : Cblank; /* east */
413 cav.ball_array[y][7] = (src[2163] & 16) ? temp : Cblank; /* southeast */
414 cav.ball_array[y][5] = (src[2163] & 32) ? temp : Cblank; /* southwest */
415 cav.ball_array[y][2] = (src[2163] & 64) ? temp : Cblank; /* northeast */
416 cav.ball_array[y][0] = (src[2163] & 128)? temp : Cblank; /* northwest */
422 for (i = 0; i < 2; i++)
424 temp = GET_BE16(src[2096 + i * 2]);
426 cav.player_x[i] = (temp & 63);
427 cav.player_y[i] = (temp >> 6 & 31);
432 /* first fill the complete playfield with the empty space element */
433 for (y = 0; y < CAVE_HEIGHT; y++)
434 for (x = 0; x < CAVE_WIDTH; x++)
435 cav.cave[x][y] = Cblank;
437 /* then copy the real level contents from level file into the playfield */
439 for (y = 0; y < cav.height; y++)
440 for (x = 0; x < cav.width; x++)
441 cav.cave[x][y] = map_emc[src[temp++]];
443 native_em_level.file_version = file_version;
447 /* convert all emerald mine caves to emc version 6 cave format.
449 * caves are filtered to get rid of invalid or unsupported tiles.
451 * although the result is a somewhat clean cave, it is meant only
452 * to give a common structure for the binary cave format. it is not
453 * for archiving purposes (it is better to keep the raw cave as-is)
454 * and it is not meant for serializing (the intermediate structure
455 * is better defined).
457 * acid top is added to acid bottom in both the cave and the eaters.
458 * fake acid (only in v4) does not exist because it adds nothing to
459 * the game, and is different even in different versions of the emc
462 * v4/v5 time is converted to 10x old time (as it should be).
463 * the reason the kingsoft player used 5x time was copy protection.
465 * note: emc v6 converter has an error in converting v4 eaters to the
466 * wrong bug(24 instead of 20) and tank(12 instead of 8).
469 /* changes for game engine integration in Rocks'n'Diamonds:
471 * cave versions below V5 used different objects than later versions
472 * - steel/wall/roundwall of type 1 was used instead of type 2
473 * - dirt was used instead of grass
474 * - wall codes 129, 131 and 132 are changed to 180, 179 and 252
475 * - object code 130 (V6 grass) is changed to 189 (V6 dirt)
476 * - object codes are changed in both cave and eater arrays
477 * - only graphical change, as changed objects behave the same
479 * acid with no base beneath it is converted to fake acid
480 * - required for downunder mine 16, level 0 (and others)
483 static const unsigned char map_v6[256] =
487 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
488 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
489 0,16,2,18, 36,37,37,37, 40,41,42,43, 44,45,128,128, // 32
490 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
492 64,65,66,67, 68,69,69,71, 72,73,74,75, 118,75,75,75, // 64
493 75,75,75,75, 75,153,153,153, 153,153,153,153, 153,153,153,153, // 80
494 153,153,153,99, 100,68,68,68, 68,68,68,68, 68,118,118,118, // 96
495 118,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118, // 112
497 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
498 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
499 160,161,162,163, 164,165,165,118, 168,169,170,171, 172,173,174,175, // 160
500 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,191, // 176
502 192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207, // 192
503 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
504 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
505 240,241,242,243, 244,245,153,153, 153,153,153,153, 153,153,153,153 // 240
508 static const unsigned char map_v5[256] =
512 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
513 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
514 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
515 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
517 64,65,66,67, 68,153,153,153, 153,153,153,153, 153,153,153,153, // 64
518 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
519 153,153,153,153, 153,68,68,68, 68,68,68,68, 68,118,118,118, // 96
520 118,118,114,115, 131,62,118,119, 120,121,122,118, 118,118,118,118, // 112
522 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
523 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
524 160,153,153,153, 153,153,153,118, 168,169,170,171, 172,173,174,175, // 160
525 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,153, // 176
527 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
528 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
529 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
530 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
533 static const unsigned char map_v4[256] =
537 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
538 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
539 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
540 128,148,148,148, 45,45,45,148, 0,153,153,59, 60,61,62,63, // 48
542 64,65,66,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 64
543 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
544 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 96
545 153,118,114,115, 179,62,118,119, 120,121,122,118, 118,118,118,118, // 112
547 128,180,189,179, 252,133,134,135, 136,137,138,139, 140,141,142,143, // 128
548 144,145,146,147, 148,149,150,151, 152,68,248,249, 250,251,158,160, // 144
549 160,160,160,160, 160,160,160,160, 160,160,160,160, 160,160,160,175, // 160
550 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,68,153, // 176
552 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
553 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
554 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
555 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
558 static const unsigned char map_v4_eater[32] =
560 /* filter for v4 eater */
562 128,18,2,0, 4,8,16,20, 28,37,41,45, 189,180,179,252, // 0
563 133,134,135,136, 146,147,175,65, 66,64,2,18, 128,128,128,128 // 16
566 static boolean filename_has_v1_format(char *filename)
568 char *basename = getBaseNamePtr(filename);
570 return (strlen(basename) == 3 &&
571 basename[0] == 'a' &&
572 basename[1] >= 'a' && basename[1] <= 'k' &&
573 basename[2] >= '0' && basename[2] <= '9');
576 int cleanup_em_level(unsigned char *src, int length, char *filename)
578 int file_version = FILE_VERSION_EM_UNKNOWN;
581 if (length >= 2172 &&
582 src[2106] == 255 && /* version id: */
583 src[2107] == 54 && /* '6' */
584 src[2108] == 48 && /* '0' */
585 src[2109] == 48) /* '0' */
587 /* ---------- this cave has V6 file format ---------- */
588 file_version = FILE_VERSION_EM_V6;
590 /* remap elements to internal EMC level format */
591 for (i = 0; i < 2048; i++) /* cave */
592 src[i] = map_v6[src[i]];
593 for (i = 2048; i < 2084; i++) /* eaters */
594 src[i] = map_v6[src[i]];
595 for (i = 2112; i < 2148; i++) /* eaters */
596 src[i] = map_v6[src[i]];
598 else if (length >= 2110 &&
599 src[2106] == 255 && /* version id: */
600 src[2107] == 53 && /* '5' */
601 src[2108] == 48 && /* '0' */
602 src[2109] == 48) /* '0' */
604 /* ---------- this cave has V5 file format ---------- */
605 file_version = FILE_VERSION_EM_V5;
607 /* remap elements to internal EMC level format */
608 for (i = 0; i < 2048; i++) /* cave */
609 src[i] = map_v5[src[i]];
610 for (i = 2048; i < 2084; i++) /* eaters */
611 src[i] = map_v5[src[i]];
612 for (i = 2112; i < 2148; i++) /* eaters */
613 src[i] = src[i - 64];
615 else if (length >= 2106 &&
616 (src[1983] == 27 || /* encrypted (only EM I/II/III) */
617 src[1983] == 116 || /* unencrypted (usual case) */
618 src[1983] == 131)) /* unencrypted (rare case) */
620 /* ---------- this cave has V1, V2 or V3 file format ---------- */
622 boolean fix_copyright = FALSE;
625 byte at position 1983 (0x07bf) is used as "magic byte":
626 - 27 (0x1b) => encrypted level (V3 only / Kingsoft original games)
627 - 116 (0x74) => unencrypted level (byte is corrected to 131 (0x83))
628 - 131 (0x83) => unencrypted level (happens only in very rare cases)
631 if (src[1983] == 27) /* (0x1b) -- after decryption: 116 (0x74) */
633 /* this is original (encrypted) Emerald Mine I, II or III level file */
635 int first_byte = src[0];
636 unsigned char code0 = 0x65;
637 unsigned char code1 = 0x11;
639 /* decode encrypted level data */
640 for (i = 0; i < 2106; i++)
645 code0 = (code0 + 7) & 0xff;
648 src[1] = 131; /* needed for all Emerald Mine levels */
650 /* first byte is either 0xf1 (EM I and III) or 0xf5 (EM II) */
651 if (first_byte == 0xf5)
653 src[0] = 131; /* only needed for Emerald Mine II levels */
655 fix_copyright = TRUE;
658 /* ---------- this cave has V3 file format ---------- */
659 file_version = FILE_VERSION_EM_V3;
661 else if (filename_has_v1_format(filename))
663 /* ---------- this cave has V1 file format ---------- */
664 file_version = FILE_VERSION_EM_V1;
668 /* ---------- this cave has V2 file format ---------- */
669 file_version = FILE_VERSION_EM_V2;
672 /* remap elements to internal EMC level format */
673 for (i = 0; i < 2048; i++) /* cave */
674 src[i] = map_v4[src[i]];
675 for (i = 2048; i < 2084; i++) /* eaters */
676 src[i] = map_v4_eater[src[i] % 32];
677 for (i = 2112; i < 2148; i++) /* eaters */
678 src[i] = src[i - 64];
680 if (fix_copyright) /* fix "(c)" sign in Emerald Mine II levels */
682 for (i = 0; i < 2048; i++)
684 src[i] = 254; /* replace 'Cdecor_1' with 'Calpha_copyr' */
689 /* ---------- this cave has unknown file format ---------- */
691 /* if file has length of old-style level file, print (wrong) magic byte */
693 Error(ERR_WARN, "unknown magic byte 0x%02x at position 0x%04x",
696 return FILE_VERSION_EM_UNKNOWN;
699 if (file_version < FILE_VERSION_EM_V6)
702 src[2106] = 255; /* version id: */
703 src[2107] = 54; /* '6' */
704 src[2108] = 48; /* '0' */
705 src[2109] = 48; /* '0' */
709 /* stored level time of levels for the V2 player was changed to 50% of the
710 time for the V1 player (original V3 levels already considered this) */
711 if (file_version != FILE_VERSION_EM_V1 &&
712 file_version != FILE_VERSION_EM_V3)
717 for (i = 2148; i < 2172; i++)
724 /* ---------- at this stage, the cave data always has V6 format ---------- */
727 for (i = 0; i < 2048; i++)
730 for (i++; i < 2048; i++)
735 for (i = 64; i < 2048; i++)
736 if (src[i] == 63) /* replace element above 'Cacid_s' ... */
737 src[i - 64] = 101; /* ... with 'Cacid_1' */
739 /* fix acid with no base beneath it (see comment above for details) */
740 for (i = 64; i < 2048 - 1; i++)
742 if (file_version <= FILE_VERSION_EM_V2 &&
743 src[i - 64] == 101 && src[i] != 63) /* acid without base */
745 if (src[i - 1] == 101 || /* remove acid over acid row */
747 src[i - 64] = 6; /* replace element above with 'Cblank' */
749 src[i - 64] = 255; /* replace element above with 'Cfake_acid_1' */
753 /* fix acid in eaters */
754 for (i = 0; i < 8; i++)
755 for (j = 0; j < 6; j++)
756 if (src[eater_offset[i] + j + 3] == 63)
757 src[eater_offset[i] + j] = 101;
762 /* set cave tile at player position to blank */
763 for (i = 0; i < 2; i++)
764 src[GET_BE16(src[2096 + i * 2]) % 2048] = 128;
774 src[2159] = map_v6[src[2159]];
784 /* size of v6 cave */
788 Error(ERR_DEBUG, "EM level file version: %d", file_version);
797 structure of emerald mine level disk files
798 ----------------------------------------------------------------------
800 if(len >= 2172 && (buf[2106] == 255 && buf[2107] == 54 && buf[2108] == 48 && buf[2109] == 48)) // v6
801 if(len >= 2110 && (buf[2106] == 255 && buf[2107] == 53 && buf[2108] == 48 && buf[2109] == 48)) // v5
802 if(len >= 2106 && (buf[1983] == 116 || buf[2047] == 116)) // v4
803 if(len >= 2106 && (buf[1983] == 27 || buf[2047] == 219)) // v3
805 buf[0]=241;buf[1]=248;for(i=0,j=101;i<2106;i++,j+=7)buf[i]=(buf[i]^j)-17; // decrypt
807 number of movements (calls to logic) = time * 50 / 8
809 {} reserved (but some broken levels use them)
811 ----------------------------------------------------------------------
831 2095: emeralds needed
835 2102: wonderwall time
837 2106: ID (0xff363030)
843 2148: flags bit#7=NOI #6=RIS
844 2149: wind direction bit#0=right #1=down #2=left #3=up
845 2150: cave number bit#7=teamwork
854 2162: ball info bit#15=switch state #8=random
855 ; bit#0=N #1=S #2=W #3=E #4=SE #5=SW #6=NE #7=NW
856 2164: android move speed
857 2166: android clone speed
859 ; bit#0=emerald #1=diamond #2=stone #3=bomb #4=nut #5=tank #6=eater
860 ; #7=bug #8=alien #9=spring #10=balloon #11=ameuba #12=dynamite
865 1: stone {stone_fall}
867 3: diamond {diamond_fall}
869 5: alien {alien_pause}
883 19: emerald {emerald_fall}
894 30: blank {drip_stretchB}
895 31: drip {drip_stretch}
896 32: stone {stone_pause}
897 33: bomb {bomb_pause}
898 34: diamond {diamond_pause}
899 35: emerald {emerald_pause}
900 36: wonderwall {wonderwallB}
910 46: blank {sand_stonein_2}
911 47: blank {sand_stonein_3}
912 48: blank {sand_stonein_4}
913 49: sand {sand_stonesand_2}
914 50: sand {sand_stonesand_3}
915 51: sand {sand_stonesand_4}
916 52: sand_stone {sand_sandstone_2}
917 53: sand_stone {sand_sandstone_3}
918 54: sand_stone {sand_sandstone_4}
919 55: sand {sand_stonesand_4}
920 56: stone {sand_stoneout_2}
934 70: spring {spring_fall}
935 71: spring {spring_w}
936 72: spring {spring_e}
941 77: android {android_n_1}
942 78: android {android_n_2}
943 79: android {android_s_1}
944 80: android {android_s_2}
945 81: android {android_e_1}
946 82: android {android_e_2}
947 83: android {android_w_1}
948 84: android {android_w_2}
963 99: spring {push_spring_w}
964 100: spring {push_spring_e}
973 109: pause {grass_wB}
974 110: pause {grass_eB}
975 111: pause {grass_nB}
976 112: pause {grass_sB}
977 113: pause {dynamite_blank}
978 114: nut {push_nut_w}
979 115: nut {push_nut_e}
980 116: steel_2 {end of level}
982 118: pause {emerald_blank}
983 119: bomb {push_bomb_w}
984 120: bomb {push_bomb_e}
985 121: stone {push_stone_w}
986 122: stone {push_stone_e}
987 123: pause {diamond_blank}
1028 163: fake_blank {fake_blankB}
1029 164: fake_grass {fake_grassB}
1031 166: switch {switchB}
1122 ----------------------------------------------------------------------
1138 2091: dynamite value
1142 2095: emeralds needed
1146 2102: wonderwall time
1148 2106: ID (0xff353030)
1152 1: stone {stone_fall}
1154 3: diamond {diamond_fall}
1156 5: alien {alien_pause}
1168 17: bomb {bomb_fall}
1170 19: emerald {emerald_fall}
1180 29: drip {drip_fall}
1181 30: blank {drip_stretchB}
1182 31: drip {drip_stretch}
1183 32: stone {stone_pause}
1184 33: bomb {bomb_pause}
1185 34: diamond {diamond_pause}
1186 35: emerald {emerald_pause}
1187 36: wonderwall {wonderwallB}
1197 46: blank {sand_stonein_2}
1198 47: blank {sand_stonein_3}
1199 48: blank {sand_stonein_4}
1200 49: sand {sand_stonesand_2}
1201 50: sand {sand_stonesand_3}
1202 51: sand {sand_stonesand_4}
1203 52: sand_stone {sand_sandstone_2}
1204 53: sand_stone {sand_sandstone_3}
1205 54: sand_stone {sand_sandstone_4}
1206 55: sand {sand_stonesand_4}
1207 56: stone {sand_stoneout_2}
1260 109: pause {grass_wB}
1261 110: pause {grass_eB}
1262 111: pause {grass_nB}
1263 112: pause {grass_sB}
1264 113: pause {dynamite_blank}
1265 114: nut {push_nut_w}
1266 115: nut {push_nut_e}
1267 116: steel_2 {end of level}
1268 117: dynamite_4 {boom_2}
1269 118: pause {emerald_blank}
1270 119: bomb {push_bomb_w}
1271 120: bomb {push_bomb_e}
1272 121: stone {push_stone_w}
1273 122: stone {push_stone_e}
1274 123: pause {diamond_blank}
1275 124: pause {dirt_wB}
1276 125: pause {dirt_eB}
1277 126: pause {dirt_nB}
1278 127: pause {dirt_sB}
1409 ----------------------------------------------------------------------
1425 2091: dynamite value
1429 2095: emeralds needed
1433 2102: wonderwall time
1463 26: diamond {diamond_shine}
1464 27: emerald {emerald_shine}
1467 1: stone {stone_fall}
1469 3: diamond {diamond_fall}
1471 5: alien {alien_pause}
1483 17: bomb {bomb_fall}
1485 19: emerald {emerald_fall}
1495 29: drip {drip_fall}
1496 30: blank {drip_stretchB}
1497 31: drip {drip_stretch}
1498 32: stone {stone_pause}
1499 33: bomb {bomb_pause}
1500 34: diamond {diamond_pause}
1501 35: emerald {emerald_pause}
1502 36: wonderwall {wonderwallB}
1512 46: blank {sand_stonein_2}
1513 47: blank {sand_stonein_3}
1514 48: blank {sand_stonein_4}
1515 49: sand {sand_stonesand_2}
1516 50: sand {sand_stonesand_3}
1517 51: sand {sand_stonesand_4}
1518 52: sand_stone {sand_sandstone_2}
1519 53: sand_stone {sand_sandstone_3}
1520 54: sand_stone {sand_sandstone_4}
1521 55: sand {sand_stonesand_4}
1522 56: stone {sand_stoneout_2}
1579 113: pause {dynamite_blank}
1580 114: nut {push_nut_w}
1581 115: nut {push_nut_e}
1582 116: steel_1 {end of level}
1583 117: dynamite_4 {boom_2}
1584 118: pause {emerald_blank}
1585 119: bomb {push_bomb_w}
1586 120: bomb {push_bomb_e}
1587 121: stone {push_stone_w}
1588 122: stone {push_stone_e}
1589 123: pause {diamond_blank}
1590 124: pause {dirt_wB}
1591 125: pause {dirt_eB}
1592 126: pause {dirt_nB}
1593 127: pause {dirt_sB}
1626 159: fake_grass {dirt}
1627 160: fake_grass {dirt}
1628 161: fake_grass {dirt}
1629 162: fake_grass {dirt}
1630 163: fake_grass {dirt}
1631 164: fake_grass {dirt}
1632 165: fake_grass {dirt}
1633 166: fake_grass {dirt}
1634 167: fake_grass {dirt}
1635 168: fake_grass {dirt}
1636 169: fake_grass {dirt}
1637 170: fake_grass {dirt}
1638 171: fake_grass {dirt}
1639 172: fake_grass {dirt}
1640 173: fake_grass {dirt}
1641 174: fake_grass {dirt}
1707 240: alpha_arrow_e {}
1708 241: decor_1 {alpha_copyr}
1724 ----------------------------------------------------------------------