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.teamwork = (src[2150] & 128) != 0;
306 cav.emerald_score = src[2084];
307 cav.diamond_score = src[2085];
308 cav.alien_score = src[2086];
309 cav.tank_score = src[2087];
310 cav.bug_score = src[2088];
311 cav.eater_score = src[2089];
312 cav.nut_score = src[2090];
313 cav.dynamite_score = src[2091];
314 cav.key_score = src[2092];
315 cav.exit_score = src[2093];
317 cav.lenses_score = src[2151];
318 cav.magnify_score = src[2152];
319 cav.slurp_score = src[2153];
323 cav.android_move_time = MIN(GET_BE16(src[2164]), 9999);
324 cav.android_clone_time = MIN(GET_BE16(src[2166]), 9999);
325 cav.ball_time = MIN(GET_BE16(src[2160]), 9999);
327 cav.lenses_time = MIN(GET_BE16(src[2154]), 9999);
328 cav.magnify_time = MIN(GET_BE16(src[2156]), 9999);
329 cav.wheel_time = MIN(GET_BE16(src[2104]), 9999);
331 cav.amoeba_time = MIN(GET_BE16(src[2100]) * 28, 9999);
332 cav.wonderwall_time = MIN(GET_BE16(src[2102]), 9999);
334 cav.wind_time = 9999;
336 cav.wind_direction = (temp & 8 ? 0 :
343 cav.ball_random = (src[2162] & 1) != 0;
344 cav.ball_active = (src[2162] & 128) != 0;
346 cav.wonderwall_active = FALSE;
347 cav.wheel_active = FALSE;
348 cav.lenses_active = FALSE;
349 cav.magnify_active = FALSE;
351 for (temp = 1; temp < 2047; temp++)
355 case 36: /* wonderwall */
356 cav.wonderwall_active = TRUE;
357 cav.wonderwall_time = 9999;
361 cav.wheel_active = TRUE;
362 cav.wheel_x = temp % 64;
363 cav.wheel_y = temp / 64;
366 case 163: /* fake blank */
367 cav.lenses_active = TRUE;
370 case 164: /* fake grass */
371 cav.magnify_active = TRUE;
378 temp = GET_BE16(src[2168]);
380 for (i = 0; i < GAME_TILE_MAX; i++)
381 cav.android_array[i] = Cblank;
383 for (i = 0; android_clone_table[i].bit_nr != -1; i++)
384 if (temp & (1 << android_clone_table[i].bit_nr))
385 cav.android_array[android_clone_table[i].clone_from] =
386 android_clone_table[i].clone_to;
390 for (i = 0; i < 8; i++)
391 for (x = 0; x < 9; x++)
392 cav.eater_array[i][x] = map_emc[src[eater_offset[i] + x]];
394 if (file_version < FILE_VERSION_EM_V6)
395 cav.num_eater_arrays = 4;
399 temp = map_emc[src[2159]];
401 for (y = 0; y < 8; y++)
405 for (x = 0; x < 8; x++)
406 cav.ball_array[y][x] = temp;
410 cav.ball_array[y][1] = (src[2163] & 1) ? temp : Cblank; /* north */
411 cav.ball_array[y][6] = (src[2163] & 2) ? temp : Cblank; /* south */
412 cav.ball_array[y][3] = (src[2163] & 4) ? temp : Cblank; /* west */
413 cav.ball_array[y][4] = (src[2163] & 8) ? temp : Cblank; /* east */
414 cav.ball_array[y][7] = (src[2163] & 16) ? temp : Cblank; /* southeast */
415 cav.ball_array[y][5] = (src[2163] & 32) ? temp : Cblank; /* southwest */
416 cav.ball_array[y][2] = (src[2163] & 64) ? temp : Cblank; /* northeast */
417 cav.ball_array[y][0] = (src[2163] & 128)? temp : Cblank; /* northwest */
423 for (i = 0; i < 2; i++)
425 temp = GET_BE16(src[2096 + i * 2]);
427 cav.player_x[i] = (temp & 63);
428 cav.player_y[i] = (temp >> 6 & 31);
433 /* first fill the complete playfield with the empty space element */
434 for (y = 0; y < CAVE_HEIGHT; y++)
435 for (x = 0; x < CAVE_WIDTH; x++)
436 cav.cave[x][y] = Cblank;
438 /* then copy the real level contents from level file into the playfield */
440 for (y = 0; y < cav.height; y++)
441 for (x = 0; x < cav.width; x++)
442 cav.cave[x][y] = map_emc[src[temp++]];
444 native_em_level.file_version = file_version;
448 /* convert all emerald mine caves to emc version 6 cave format.
450 * caves are filtered to get rid of invalid or unsupported tiles.
452 * although the result is a somewhat clean cave, it is meant only
453 * to give a common structure for the binary cave format. it is not
454 * for archiving purposes (it is better to keep the raw cave as-is)
455 * and it is not meant for serializing (the intermediate structure
456 * is better defined).
458 * acid top is added to acid bottom in both the cave and the eaters.
459 * fake acid (only in v4) does not exist because it adds nothing to
460 * the game, and is different even in different versions of the emc
463 * v4/v5 time is converted to 10x old time (as it should be).
464 * the reason the kingsoft player used 5x time was copy protection.
466 * note: emc v6 converter has an error in converting v4 eaters to the
467 * wrong bug(24 instead of 20) and tank(12 instead of 8).
470 /* changes for game engine integration in Rocks'n'Diamonds:
472 * cave versions below V5 used different objects than later versions
473 * - steel/wall/roundwall of type 1 was used instead of type 2
474 * - dirt was used instead of grass
475 * - wall codes 129, 131 and 132 are changed to 180, 179 and 252
476 * - object code 130 (V6 grass) is changed to 189 (V6 dirt)
477 * - object codes are changed in both cave and eater arrays
478 * - only graphical change, as changed objects behave the same
480 * acid with no base beneath it is converted to fake acid
481 * - required for downunder mine 16, level 0 (and others)
484 static const unsigned char map_v6[256] =
488 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
489 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
490 0,16,2,18, 36,37,37,37, 40,41,42,43, 44,45,128,128, // 32
491 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
493 64,65,66,67, 68,69,69,71, 72,73,74,75, 118,75,75,75, // 64
494 75,75,75,75, 75,153,153,153, 153,153,153,153, 153,153,153,153, // 80
495 153,153,153,99, 100,68,68,68, 68,68,68,68, 68,118,118,118, // 96
496 118,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118, // 112
498 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
499 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
500 160,161,162,163, 164,165,165,118, 168,169,170,171, 172,173,174,175, // 160
501 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,191, // 176
503 192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207, // 192
504 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
505 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
506 240,241,242,243, 244,245,153,153, 153,153,153,153, 153,153,153,153 // 240
509 static const unsigned char map_v5[256] =
513 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
514 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
515 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
516 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63, // 48
518 64,65,66,67, 68,153,153,153, 153,153,153,153, 153,153,153,153, // 64
519 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
520 153,153,153,153, 153,68,68,68, 68,68,68,68, 68,118,118,118, // 96
521 118,118,114,115, 131,62,118,119, 120,121,122,118, 118,118,118,118, // 112
523 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143, // 128
524 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159, // 144
525 160,153,153,153, 153,153,153,118, 168,169,170,171, 172,173,174,175, // 160
526 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,153, // 176
528 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
529 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
530 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
531 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
534 static const unsigned char map_v4[256] =
538 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15, // 0
539 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,128,28, // 16
540 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128, // 32
541 128,148,148,148, 45,45,45,148, 0,153,153,59, 60,61,62,63, // 48
543 64,65,66,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 64
544 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 80
545 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153, // 96
546 153,118,114,115, 179,62,118,119, 120,121,122,118, 118,118,118,118, // 112
548 128,180,189,179, 252,133,134,135, 136,137,138,139, 140,141,142,143, // 128
549 144,145,146,147, 148,149,150,151, 152,68,248,249, 250,251,158,160, // 144
550 160,160,160,160, 160,160,160,160, 160,160,160,160, 160,160,160,175, // 160
551 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,68,153, // 176
553 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207, // 192
554 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223, // 208
555 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239, // 224
556 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153 // 240
559 static const unsigned char map_v4_eater[28] =
561 /* filter for v4 eater */
563 128,18,2,0, 4,8,16,20, 28,37,41,45, 189,180,179,252, // 0
564 133,134,135,136, 146,147,175,65, 66,64,2,18 // 16
567 static boolean filename_has_v1_format(char *filename)
569 char *basename = getBaseNamePtr(filename);
571 return (strlen(basename) == 3 &&
572 basename[0] == 'a' &&
573 basename[1] >= 'a' && basename[1] <= 'k' &&
574 basename[2] >= '0' && basename[2] <= '9');
577 int cleanup_em_level(unsigned char *src, int length, char *filename)
579 int file_version = FILE_VERSION_EM_UNKNOWN;
582 if (length >= 2172 &&
583 src[2106] == 255 && /* version id: */
584 src[2107] == 54 && /* '6' */
585 src[2108] == 48 && /* '0' */
586 src[2109] == 48) /* '0' */
588 /* ---------- this cave has V6 file format ---------- */
589 file_version = FILE_VERSION_EM_V6;
591 /* remap elements to internal EMC level format */
592 for (i = 0; i < 2048; i++) /* cave */
593 src[i] = map_v6[src[i]];
594 for (i = 2048; i < 2084; i++) /* eaters */
595 src[i] = map_v6[src[i]];
596 for (i = 2112; i < 2148; i++) /* eaters */
597 src[i] = map_v6[src[i]];
599 else if (length >= 2110 &&
600 src[2106] == 255 && /* version id: */
601 src[2107] == 53 && /* '5' */
602 src[2108] == 48 && /* '0' */
603 src[2109] == 48) /* '0' */
605 /* ---------- this cave has V5 file format ---------- */
606 file_version = FILE_VERSION_EM_V5;
608 /* remap elements to internal EMC level format */
609 for (i = 0; i < 2048; i++) /* cave */
610 src[i] = map_v5[src[i]];
611 for (i = 2048; i < 2084; i++) /* eaters */
612 src[i] = map_v5[src[i]];
613 for (i = 2112; i < 2148; i++) /* eaters */
614 src[i] = src[i - 64];
616 else if (length >= 2106 &&
617 (src[1983] == 27 || /* encrypted (only EM I/II/III) */
618 src[1983] == 116 || /* unencrypted (usual case) */
619 src[1983] == 131)) /* unencrypted (rare case) */
621 /* ---------- this cave has V1, V2 or V3 file format ---------- */
623 boolean fix_copyright = FALSE;
626 byte at position 1983 (0x07bf) is used as "magic byte":
627 - 27 (0x1b) => encrypted level (V3 only / Kingsoft original games)
628 - 116 (0x74) => unencrypted level (byte is corrected to 131 (0x83))
629 - 131 (0x83) => unencrypted level (happens only in very rare cases)
632 if (src[1983] == 27) /* (0x1b) -- after decryption: 116 (0x74) */
634 /* this is original (encrypted) Emerald Mine I, II or III level file */
636 int first_byte = src[0];
637 unsigned char code0 = 0x65;
638 unsigned char code1 = 0x11;
640 /* decode encrypted level data */
641 for (i = 0; i < 2106; i++)
646 code0 = (code0 + 7) & 0xff;
649 src[1] = 131; /* needed for all Emerald Mine levels */
651 /* first byte is either 0xf1 (EM I and III) or 0xf5 (EM II) */
652 if (first_byte == 0xf5)
654 src[0] = 131; /* only needed for Emerald Mine II levels */
656 fix_copyright = TRUE;
659 /* ---------- this cave has V3 file format ---------- */
660 file_version = FILE_VERSION_EM_V3;
662 else if (filename_has_v1_format(filename))
664 /* ---------- this cave has V1 file format ---------- */
665 file_version = FILE_VERSION_EM_V1;
669 /* ---------- this cave has V2 file format ---------- */
670 file_version = FILE_VERSION_EM_V2;
673 /* remap elements to internal EMC level format */
674 for (i = 0; i < 2048; i++) /* cave */
675 src[i] = map_v4[src[i]];
676 for (i = 2048; i < 2084; i++) /* eaters */
677 src[i] = map_v4_eater[src[i] < 28 ? src[i] : 0];
678 for (i = 2112; i < 2148; i++) /* eaters */
679 src[i] = src[i - 64];
681 if (fix_copyright) /* fix "(c)" sign in Emerald Mine II levels */
683 for (i = 0; i < 2048; i++)
685 src[i] = 254; /* replace 'Cdecor_1' with 'Calpha_copyr' */
690 /* ---------- this cave has unknown file format ---------- */
692 /* if file has length of old-style level file, print (wrong) magic byte */
694 Warn("unknown magic byte 0x%02x at position 0x%04x", src[1983], 1983);
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 */
787 Debug("level:native:EM", "EM level file version: %d", file_version);
796 structure of emerald mine level disk files
797 ----------------------------------------------------------------------
799 if(len >= 2172 && (buf[2106] == 255 && buf[2107] == 54 && buf[2108] == 48 && buf[2109] == 48)) // v6
800 if(len >= 2110 && (buf[2106] == 255 && buf[2107] == 53 && buf[2108] == 48 && buf[2109] == 48)) // v5
801 if(len >= 2106 && (buf[1983] == 116 || buf[2047] == 116)) // v4
802 if(len >= 2106 && (buf[1983] == 27 || buf[2047] == 219)) // v3
804 buf[0] = 241; buf[1] = 248; for(i = 0, j = 101; i < 2106; i++, j += 7) buf[i] = (buf[i] ^ j) - 17; // decrypt
806 number of movements (calls to logic) = time * 50 / 8
808 {} reserved (but some broken levels use them)
810 ----------------------------------------------------------------------
830 2095: emeralds needed
834 2102: wonderwall time
836 2106: ID (0xff363030)
842 2148: flags bit#7=NOI #6=RIS
843 2149: wind direction bit#0=right #1=down #2=left #3=up
844 2150: cave number bit#7=teamwork
853 2162: ball info bit#15=switch state #8=random
854 ; bit#0=N #1=S #2=W #3=E #4=SE #5=SW #6=NE #7=NW
855 2164: android move speed
856 2166: android clone speed
858 ; bit#0=emerald #1=diamond #2=stone #3=bomb #4=nut #5=tank #6=eater
859 ; #7=bug #8=alien #9=spring #10=balloon #11=ameuba #12=dynamite
864 1: stone {stone_fall}
866 3: diamond {diamond_fall}
868 5: alien {alien_pause}
882 19: emerald {emerald_fall}
893 30: blank {drip_stretchB}
894 31: drip {drip_stretch}
895 32: stone {stone_pause}
896 33: bomb {bomb_pause}
897 34: diamond {diamond_pause}
898 35: emerald {emerald_pause}
899 36: wonderwall {wonderwallB}
909 46: blank {sand_stonein_2}
910 47: blank {sand_stonein_3}
911 48: blank {sand_stonein_4}
912 49: sand {sand_stonesand_2}
913 50: sand {sand_stonesand_3}
914 51: sand {sand_stonesand_4}
915 52: sand_stone {sand_sandstone_2}
916 53: sand_stone {sand_sandstone_3}
917 54: sand_stone {sand_sandstone_4}
918 55: sand {sand_stonesand_4}
919 56: stone {sand_stoneout_2}
933 70: spring {spring_fall}
934 71: spring {spring_w}
935 72: spring {spring_e}
940 77: android {android_n_1}
941 78: android {android_n_2}
942 79: android {android_s_1}
943 80: android {android_s_2}
944 81: android {android_e_1}
945 82: android {android_e_2}
946 83: android {android_w_1}
947 84: android {android_w_2}
962 99: spring {push_spring_w}
963 100: spring {push_spring_e}
972 109: pause {grass_wB}
973 110: pause {grass_eB}
974 111: pause {grass_nB}
975 112: pause {grass_sB}
976 113: pause {dynamite_blank}
977 114: nut {push_nut_w}
978 115: nut {push_nut_e}
979 116: steel_2 {end of level}
981 118: pause {emerald_blank}
982 119: bomb {push_bomb_w}
983 120: bomb {push_bomb_e}
984 121: stone {push_stone_w}
985 122: stone {push_stone_e}
986 123: pause {diamond_blank}
1027 163: fake_blank {fake_blankB}
1028 164: fake_grass {fake_grassB}
1030 166: switch {switchB}
1121 ----------------------------------------------------------------------
1137 2091: dynamite value
1141 2095: emeralds needed
1145 2102: wonderwall time
1147 2106: ID (0xff353030)
1151 1: stone {stone_fall}
1153 3: diamond {diamond_fall}
1155 5: alien {alien_pause}
1167 17: bomb {bomb_fall}
1169 19: emerald {emerald_fall}
1179 29: drip {drip_fall}
1180 30: blank {drip_stretchB}
1181 31: drip {drip_stretch}
1182 32: stone {stone_pause}
1183 33: bomb {bomb_pause}
1184 34: diamond {diamond_pause}
1185 35: emerald {emerald_pause}
1186 36: wonderwall {wonderwallB}
1196 46: blank {sand_stonein_2}
1197 47: blank {sand_stonein_3}
1198 48: blank {sand_stonein_4}
1199 49: sand {sand_stonesand_2}
1200 50: sand {sand_stonesand_3}
1201 51: sand {sand_stonesand_4}
1202 52: sand_stone {sand_sandstone_2}
1203 53: sand_stone {sand_sandstone_3}
1204 54: sand_stone {sand_sandstone_4}
1205 55: sand {sand_stonesand_4}
1206 56: stone {sand_stoneout_2}
1259 109: pause {grass_wB}
1260 110: pause {grass_eB}
1261 111: pause {grass_nB}
1262 112: pause {grass_sB}
1263 113: pause {dynamite_blank}
1264 114: nut {push_nut_w}
1265 115: nut {push_nut_e}
1266 116: steel_2 {end of level}
1267 117: dynamite_4 {boom_2}
1268 118: pause {emerald_blank}
1269 119: bomb {push_bomb_w}
1270 120: bomb {push_bomb_e}
1271 121: stone {push_stone_w}
1272 122: stone {push_stone_e}
1273 123: pause {diamond_blank}
1274 124: pause {dirt_wB}
1275 125: pause {dirt_eB}
1276 126: pause {dirt_nB}
1277 127: pause {dirt_sB}
1408 ----------------------------------------------------------------------
1424 2091: dynamite value
1428 2095: emeralds needed
1432 2102: wonderwall time
1462 26: diamond {diamond_shine}
1463 27: emerald {emerald_shine}
1466 1: stone {stone_fall}
1468 3: diamond {diamond_fall}
1470 5: alien {alien_pause}
1482 17: bomb {bomb_fall}
1484 19: emerald {emerald_fall}
1494 29: drip {drip_fall}
1495 30: blank {drip_stretchB}
1496 31: drip {drip_stretch}
1497 32: stone {stone_pause}
1498 33: bomb {bomb_pause}
1499 34: diamond {diamond_pause}
1500 35: emerald {emerald_pause}
1501 36: wonderwall {wonderwallB}
1511 46: blank {sand_stonein_2}
1512 47: blank {sand_stonein_3}
1513 48: blank {sand_stonein_4}
1514 49: sand {sand_stonesand_2}
1515 50: sand {sand_stonesand_3}
1516 51: sand {sand_stonesand_4}
1517 52: sand_stone {sand_sandstone_2}
1518 53: sand_stone {sand_sandstone_3}
1519 54: sand_stone {sand_sandstone_4}
1520 55: sand {sand_stonesand_4}
1521 56: stone {sand_stoneout_2}
1578 113: pause {dynamite_blank}
1579 114: nut {push_nut_w}
1580 115: nut {push_nut_e}
1581 116: steel_1 {end of level}
1582 117: dynamite_4 {boom_2}
1583 118: pause {emerald_blank}
1584 119: bomb {push_bomb_w}
1585 120: bomb {push_bomb_e}
1586 121: stone {push_stone_w}
1587 122: stone {push_stone_e}
1588 123: pause {diamond_blank}
1589 124: pause {dirt_wB}
1590 125: pause {dirt_eB}
1591 126: pause {dirt_nB}
1592 127: pause {dirt_sB}
1625 159: fake_grass {dirt}
1626 160: fake_grass {dirt}
1627 161: fake_grass {dirt}
1628 162: fake_grass {dirt}
1629 163: fake_grass {dirt}
1630 164: fake_grass {dirt}
1631 165: fake_grass {dirt}
1632 166: fake_grass {dirt}
1633 167: fake_grass {dirt}
1634 168: fake_grass {dirt}
1635 169: fake_grass {dirt}
1636 170: fake_grass {dirt}
1637 171: fake_grass {dirt}
1638 172: fake_grass {dirt}
1639 173: fake_grass {dirt}
1640 174: fake_grass {dirt}
1706 240: alpha_arrow_e {}
1707 241: decor_1 {alpha_copyr}
1723 ----------------------------------------------------------------------