-#if 1 /* ================================================================== */
-
- else if (length >= 2106 &&
- (src[1983] == 27 || /* encrypted (only EM I/II/III) */
- src[1983] == 116 || /* unencrypted (usual case) */
- src[1983] == 131)) /* unencrypted (rare case) */
- {
- /* ---------- this cave has V1, V2 or V3 file format ---------- */
-
- boolean fix_copyright = FALSE;
-
- /*
- byte at position 1983 (0x07bf) is used as "magic byte":
- - 27 (0x1b) => encrypted level (V3 only / Kingsoft original games)
- - 116 (0x74) => unencrypted level (byte is corrected to 131 (0x83))
- - 131 (0x83) => unencrypted level (happens only in very rare cases)
- */
-
- if (src[1983] == 27) /* (0x1b) -- after decryption: 116 (0x74) */
- {
- /* this is original (encrypted) Emerald Mine I, II or III level file */
-
- int first_byte = src[0];
- unsigned char code0 = 0x65;
- unsigned char code1 = 0x11;
-
- /* decode encrypted level data */
- for (i = 0; i < 2106; i++)
- {
- src[i] ^= code0;
- src[i] -= code1;
-
- code0 = (code0 + 7) & 0xff;
- }
-
- src[1] = 131; /* needed for all Emerald Mine levels */
-
- /* first byte is either 0xf1 (EM I and III) or 0xf5 (EM II) */
- if (first_byte == 0xf5)
- {
- src[0] = 131; /* only needed for Emerald Mine II levels */
-
- fix_copyright = TRUE;
- }
-
- /* ---------- this cave has V3 file format ---------- */
- file_version = FILE_VERSION_EM_V3;
- }
- else if (filename_has_v1_format(filename))
- {
- /* ---------- this cave has V1 file format ---------- */
- file_version = FILE_VERSION_EM_V1;
- }
- else
- {
- /* ---------- this cave has V2 file format ---------- */
- file_version = FILE_VERSION_EM_V2;
- }
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v4[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v4eater[src[i] >= 28 ? 0 : src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
-
- if (fix_copyright) /* fix "(c)" sign in Emerald Mine II levels */
- {
- for (i = 0; i < 2048; i++)
- if (src[i] == 241)
- src[i] = 254; /* replace 'Xdecor_1' with 'Xalpha_copyr' */
- }
- }
- else
- {
- /* ---------- this cave has unknown file format ---------- */
-
- /* if file has length of old-style level file, print (wrong) magic byte */
- if (length < 2110)
- Error(ERR_WARN, "unknown magic byte 0x%02x at position 0x%04x",
- src[1983], 1983);
-
- return FILE_VERSION_EM_UNKNOWN;
- }
-
-#else /* ================================================================== */
-
-#if 0
- else if (length >= 2106) /* !!! TEST ONLY: SHOW BROKEN LEVELS !!! */
-#else
- else if (length >= 2106 &&
- src[1983] == 116)
-#endif
- {
- /* ---------- this cave has V4 file format ---------- */
- file_version = FILE_VERSION_EM_V4;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v4[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v4eater[src[i] >= 28 ? 0 : src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
- }
- else if (length >= 2106 &&
- src[0] == 241 && /* <-- Emerald Mine I and III levels */
- src[1983] == 27)
- {
- unsigned char j = 94;
-
- /* ---------- this cave has V3 file format ---------- */
- file_version = FILE_VERSION_EM_V3;
-
- /* decrypt encrypted level file */
- for (i = 0; i < 2106; i++)
- src[i] = (src[i] ^ (j += 7)) - 0x11;
-
- src[1] = 131;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v4[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v4eater[src[i] >= 28 ? 0 : src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
- }
-#if 1
- else if (length >= 2106 &&
- src[0] == 245 && /* <-- Emerald Mine II levels */
- src[1983] == 27)
- {
- unsigned char j = 94;
-
- /* ---------- this cave has V3 file format ---------- */
- file_version = FILE_VERSION_EM_V3;
-
- /* decrypt encrypted level file */
- for (i = 0; i < 2106; i++)
- src[i] = (src[i] ^ (j += 7)) - 0x11;
-
- src[0] = 131; /* needed for Emerald Mine II levels */
- src[1] = 131;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v4[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v4eater[src[i] >= 28 ? 0 : src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
-
- /* fix copyright sign in Emerald Mine II levels */
- for (i = 0; i < 2048; i++)
- if (src[i] == 241)
- src[i] = 254; /* replace 'Xdecor_1' with 'Xalpha_copyr' */
- }
-#endif
- else
- {
- /* ---------- this cave has unknown file format ---------- */
-
-#if 0
- printf("::: %d, %d\n", src[0], src[1983]);
-#endif
-
- return 0;
- }
-
-#endif /* ================================================================== */
-
- if (file_version < FILE_VERSION_EM_V6)
- {
- /* id */
- src[2106] = 255; /* version id: */
- src[2107] = 54; /* '6' */
- src[2108] = 48; /* '0' */
- src[2109] = 48; /* '0' */
-
- /* time */
- i = src[2094] * 10;
- /* stored level time of levels for the V2 player was changed to 50% of the
- time for the V1 player (original V3 levels already considered this) */
- if (file_version != FILE_VERSION_EM_V1 &&
- file_version != FILE_VERSION_EM_V3)
- i /= 2;
- src[2110] = i >> 8;
- src[2111] = i;
-
- for (i = 2148; i < 2172; i++)
- src[i] = 0;
-
- /* ball data */
- src[2159] = 128;
-
-#if 0
- printf("::: STORED TIME (< V6): %d s\n", src[2094] * 10);
-#endif
- }
-#if 0
- else
- {
- printf("::: STORED TIME (>= V6): %d s\n", src[2110] * 256 + src[2111]);
- }
-#endif
-
- /* ---------- at this stage, the cave data always has V6 format ---------- */
-
- /* fix wheel */
- for (i = 0; i < 2048; i++)
- if (src[i] == 40)
- break;
- for (i++; i < 2048; i++)
- if (src[i] == 40)
- src[i] = 147;
-
-#if 0
-
- /* fix acid */
- for (i = 64; i < 2048; i++)
- if (src[i] == 63) /* replace element above 'Xacid_s' ... */
- src[i - 64] = 101; /* ... with 'Xacid_1' */
-
-#else
-
-#if 1
- /* fix acid */
- for (i = 64; i < 2048; i++)
- if (src[i] == 63) /* replace element above 'Xacid_s' ... */
- src[i - 64] = 101; /* ... with 'Xacid_1' */
-
- /* fix acid with no base beneath it (see below for details (*)) */
- for (i = 64; i < 2048 - 1; i++)
- {
- if (file_version <= FILE_VERSION_EM_V2 &&
- src[i - 64] == 101 && src[i] != 63) /* acid without base */
- {
- if (src[i - 1] == 101 || /* remove acid over acid row */
- src[i + 1] == 101)
- src[i - 64] = 6; /* replace element above with 'Xblank' */
- else
- src[i - 64] = 255; /* replace element above with 'Xfake_acid_1' */
- }
- }
-
-#else
-
- /* fix acid */
- for (i = 64; i < 2048; i++)
- {
- if (src[i] == 63) /* 'Xacid_s' (acid pool, bottom middle) */
- {
- if (file_version <= FILE_VERSION_EM_V2 &&
- i < 2048 - 64 && src[i + 64] == 63)
- {
- int obj_left = remap_emerald[src[i - 1]];
- int obj_right = remap_emerald[src[i + 1]];
-
- if (obj_left == Xblank || obj_right == Xblank ||
- obj_left == Xplant || obj_right == Xplant)
- src[i - 64] = 6; /* replace element above with 'Xblank' */
- else
- src[i - 64] = 255; /* replace element above with 'Xfake_acid_1' */
- }
- else
- {
- src[i - 64] = 101; /* replace element above with 'Xacid_1' */
- }
- }
- }
-#endif
-#endif
-
- /* fix acid in eater 1 */
- for (i = 2051; i < 2057; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 2 */
- for (i = 2060; i < 2066; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 3 */
- for (i = 2069; i < 2075; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 4 */
- for (i = 2078; i < 2084; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 5 */
- for (i = 2115; i < 2121; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 6 */
- for (i = 2124; i < 2130; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 7 */
- for (i = 2133; i < 2139; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* fix acid in eater 8 */
- for (i = 2142; i < 2148; i++)
- if (src[i] == 63)
- src[i - 3] = 101;
-
- /* old style time */
- src[2094] = 0;
-
- /* player 1 pos */
- src[2096] &= 7;
- src[src[2096] << 8 | src[2097]] = 128;
-
- /* player 2 pos */
- src[2098] &= 7;
- src[src[2098] << 8 | src[2099]] = 128;
-
- /* amoeba speed */
- if ((src[2100] << 8 | src[2101]) > 9999)
- {
- src[2100] = 39;
- src[2101] = 15;
- }
-
- /* time wonderwall */
- if ((src[2102] << 8 | src[2103]) > 9999)
- {
- src[2102] = 39;
- src[2103] = 15;
- }
-
- /* time */
- if ((src[2110] << 8 | src[2111]) > 9999)
- {
- src[2110] = 39;
- src[2111] = 15;
- }
-
- /* wind direction */
- i = src[2149];
- i &= 15;
- i &= -i;
- src[2149] = i;
-
- /* time lenses */
- if ((src[2154] << 8 | src[2155]) > 9999)
- {
- src[2154] = 39;
- src[2155] = 15;
- }
-
- /* time magnify */
- if ((src[2156] << 8 | src[2157]) > 9999)
- {
- src[2156] = 39;
- src[2157] = 15;
- }
-
- /* ball object */
- src[2158] = 0;
- src[2159] = remap_v6[src[2159]];
-
- /* ball pause */
- if ((src[2160] << 8 | src[2161]) > 9999)
- {
- src[2160] = 39;
- src[2161] = 15;
- }
-
- /* ball data */
- src[2162] &= 129;
- if (src[2162] & 1)
- src[2163] = 0;
-
- /* android move pause */
- if ((src[2164] << 8 | src[2165]) > 9999)
- {
- src[2164] = 39;
- src[2165] = 15;
- }
-
- /* android clone pause */
- if ((src[2166] << 8 | src[2167]) > 9999)
- {
- src[2166] = 39;
- src[2167] = 15;
- }
-
- /* android data */
- src[2168] &= 31;
-
- /* size of v6 cave */
- length = 2172;
-
-#if 1
-#if 1
- if (options.debug)
-#endif
- printf("::: EM level file version: %d\n", file_version);
-#endif
-
- return file_version;
-}
-
-/* 2000-07-30T00:26:00Z
- *
- * Read emerald mine caves version 6
- *
- * v4 and v5 emerald mine caves are converted to v6 (which completely supports
- * older versions)
- *
- * converting to the internal format loses /significant/ information which can
- * break lots of caves.
- *
- * major incompatibilities:
- * - borderless caves behave completely differently, the player no longer
- * "warps" to the other side.
- * - a compile time option for spring can make it behave differently when it
- * rolls.
- * - a compile time option for rolling objects (stone, nut, spring, bomb) only
- * in eater.
- * - acid is always deadly even with no base beneath it (this breaks cave 0 in
- * downunder mine 16)
- * (*) fixed (see above):
- * - downunder mine 16, level 0, works again
- * - downunder mine 11, level 71, corrected (only cosmetically)
- *
- * so far all below have not broken any caves:
- *
- * - active wheel inside an eater will not function, eater explosions will not
- * change settings.
- * - initial collect objects (emerald, diamond, dynamite) don't exist.
- * - initial rolling objects will be moved manually and made into sitting
- * objects.
- * - drips always appear from dots.
- * - more than one thing can fall into acid at the same time.
- * - acid explodes when the player walks into it, rather than splashing.
- * - simultaneous explosions may be in a slightly different order.
- * - quicksand states have been reduced.
- * - acid base is effectively an indestructable wall now which can affect eater
- * explosions.
- * - android can clone forever with a clone pause of 0 (emeralds, diamonds,
- * nuts, stones, bombs, springs).
- *
- * 2001-03-12T02:46:55Z
- * - rolling stuff is now allowed in the cave, i didn't like making this
- * decision.
- * - if BAD_ROLL is not defined, initial rolling objects are moved by hand.
- * - initial collect objects break some cave in elvis mine 5.
- * - different timing for wonderwall break some cave in exception mine 2.
- * - i think i'm pretty locked into always using the bad roll. *sigh*
- * - rolling spring is now turned into regular spring. it appears the emc
- * editor only uses the force code for initially moving spring. i will
- * follow this in my editor.
- *
- * 2006-04-02
- * - introduced ALLOW_ROLLING_SPRING; if defined, do NOT turn rolling spring
- * into regular spring, because this breaks at least E.M.C. Mine 3, level 79
- * (see comment directly above)
- */
-
-static unsigned short remap_emerald[256] =
-{
- Xstone, Xstone, Xdiamond, Xdiamond,
- Xalien, Xalien, Xblank, Xblank,
- Xtank_n, Xtank_e, Xtank_s, Xtank_w,
- Xtank_gon, Xtank_goe, Xtank_gos, Xtank_gow,
-
- Xbomb, Xbomb, Xemerald, Xemerald,
- Xbug_n, Xbug_e, Xbug_s, Xbug_w,
- Xbug_gon, Xbug_goe, Xbug_gos, Xbug_gow,
- Xdrip_eat, Xdrip_eat, Xdrip_eat, Xdrip_eat,
-
- Xstone, Xbomb, Xdiamond, Xemerald,
- Xwonderwall, Xnut, Xnut, Xnut,
- Xwheel, Xeater_n, Xeater_s, Xeater_w,
- Xeater_e, Xsand_stone, Xblank, Xblank,
-
- Xblank, Xsand, Xsand, Xsand,
- Xsand_stone, Xsand_stone, Xsand_stone, Xsand,
- Xstone, Xgrow_ew, Xgrow_ns, Xdynamite_1,
- Xdynamite_2, Xdynamite_3, Xdynamite_4, Xacid_s,
-
-#ifdef ALLOW_ROLLING_SPRING
- Xexit_1, Xexit_2, Xexit_3, Xballoon,
- Xplant, Xspring, Xspring_fall, Xspring_w,
- Xspring_e, Xball_1, Xball_2, Xandroid,
- Xblank, Xandroid, Xandroid, Xandroid,
-#else
- Xexit_1, Xexit_2, Xexit_3, Xballoon,
- Xplant, Xspring, Xspring, Xspring,
- Xspring, Xball_1, Xball_2, Xandroid,
- Xblank, Xandroid, Xandroid, Xandroid,
-#endif
-
- Xandroid, Xandroid, Xandroid, Xandroid,
- Xandroid, Xblank, Xblank, Xblank,
- Xblank, Xblank, Xblank, Xblank,
- Xblank, Xblank, Xblank, Xblank,
-
-#ifdef BAD_ROLL
-
- Xblank, Xblank, Xblank, Xspring_force_w,
- Xspring_force_e, Xacid_1, Xacid_2, Xacid_3,
- Xacid_4, Xacid_5, Xacid_6, Xacid_7,
- Xacid_8, Xblank, Xblank, Xblank,
-
- Xblank, Xblank, Xnut_force_w, Xnut_force_e,
- Xsteel_1, Xblank, Xblank, Xbomb_force_w,
- Xbomb_force_e, Xstone_force_w, Xstone_force_e, Xblank,
- Xblank, Xblank, Xblank, Xblank,
-
-#else
-
- Xblank, Xblank, Xblank, Xspring,
- Xspring, Xacid_1, Xacid_2, Xacid_3,
- Xacid_4, Xacid_5, Xacid_6, Xacid_7,
- Xacid_8, Xblank, Xblank, Xblank,
-
- Xblank, Xblank, Xnut, Xnut,
- Xsteel_1, Xblank, Xblank, Xbomb,
- Xbomb, Xstone, Xstone, Xblank,
- Xblank, Xblank, Xblank, Xblank,
-
-#endif
-
- Xblank, Xround_wall_1, Xgrass, Xsteel_1,
- Xwall_1, Xkey_1, Xkey_2, Xkey_3,
- Xkey_4, Xdoor_1, Xdoor_2, Xdoor_3,
- Xdoor_4, Xdripper, Xfake_door_1, Xfake_door_2,
-
- Xfake_door_3, Xfake_door_4, Xwonderwall, Xwheel,
- Xsand, Xacid_nw, Xacid_ne, Xacid_sw,
- Xacid_se, Xfake_blank, Xamoeba_1, Xamoeba_2,
- Xamoeba_3, Xamoeba_4, Xexit, Xalpha_arrow_w,
-
- Xfake_grass, Xlenses, Xmagnify, Xfake_blank,
- Xfake_grass, Xswitch, Xswitch, Xblank,
- Xdecor_8, Xdecor_9, Xdecor_10, Xdecor_5,
- Xalpha_comma, Xalpha_quote, Xalpha_minus, Xdynamite,
-
- Xsteel_3, Xdecor_6, Xdecor_7, Xsteel_2,
- Xround_wall_2, Xdecor_2, Xdecor_4, Xdecor_3,
- Xwind_nesw, Xwind_e, Xwind_s, Xwind_w,
- Xwind_n, Xdirt, Xplant, Xkey_5,
-
- Xkey_6, Xkey_7, Xkey_8, Xdoor_5,
- Xdoor_6, Xdoor_7, Xdoor_8, Xbumper,
- Xalpha_a, Xalpha_b, Xalpha_c, Xalpha_d,
- Xalpha_e, Xalpha_f, Xalpha_g, Xalpha_h,
-
- Xalpha_i, Xalpha_j, Xalpha_k, Xalpha_l,
- Xalpha_m, Xalpha_n, Xalpha_o, Xalpha_p,
- Xalpha_q, Xalpha_r, Xalpha_s, Xalpha_t,
- Xalpha_u, Xalpha_v, Xalpha_w, Xalpha_x,
-
- Xalpha_y, Xalpha_z, Xalpha_0, Xalpha_1,
- Xalpha_2, Xalpha_3, Xalpha_4, Xalpha_5,
- Xalpha_6, Xalpha_7, Xalpha_8, Xalpha_9,
- Xalpha_perio, Xalpha_excla, Xalpha_colon, Xalpha_quest,
-
- Xalpha_arrow_e, Xdecor_1, Xfake_door_5, Xfake_door_6,
- Xfake_door_7, Xfake_door_8, Xblank, Xblank,
- Xblank, Xblank, Xblank, Xblank,
-#if 0
- Xblank, Xblank, Xblank, Xblank,
-#else
- /* special elements added to solve compatibility problems */
- Xblank, Xblank, Xalpha_copyr, Xfake_acid_1
-#endif
-};
-
-static int get_em_element(unsigned short em_element_raw, int file_version)
-{
- int em_element = remap_emerald[em_element_raw];
-
- if (file_version < FILE_VERSION_EM_V5)
- {
- /* versions below V5 had no grass, but only sand/dirt */
- if (em_element == Xgrass)
- em_element = Xdirt;
- }
-
- return em_element;