- /* filter crap for v5 */
-
- 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15,
- 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,118,28,
- 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128,
- 128,148,148,148, 45,45,45,148, 0,57,58,59, 60,61,62,63,
-
- 64,65,66,67, 68,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,68,68,68,68, 68,68,68,68,118, 118,118,
- 118,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118,
-
- 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
- 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
- 160,153,153,153, 153,153,153,118, 168,169,170,171, 172,173,174,175,
- 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,68,153,
-
- 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207,
- 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
- 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
- 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153
-};
-
-static unsigned char remap_v4[256] =
-{
- /* filter crap for v4 */
-
- 0,0,2,2, 4,4,118,118, 8,9,10,11, 12,13,14,15,
- 16,16,18,18, 20,21,22,23, 24,25,26,27, 28,28,118,28,
- 0,16,2,18, 36,37,37,37, 147,41,42,43, 44,45,128,128,
- 128,148,148,148, 45,45,45,148, 0,153,153,59, 60,61,62,63,
-
- 64,65,66,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153,
- 153,118,114,115, 131,118,118,119, 120,121,122,118, 118,118,118,118,
-
- 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
- 144,145,146,147, 148,149,150,151, 152,68,154,155, 156,157,158,160,
- 160,160,160,160, 160,160,160,160, 160,160,160,160, 160,160,160,175,
- 153,153,153,153, 153,153,153,153, 153,153,153,153, 153,153,68,153,
-
- 153,153,153,153, 153,153,153,153, 200,201,202,203, 204,205,206,207,
- 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
- 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
- 240,241,153,153, 153,153,153,153, 153,153,153,153, 153,153,153,153
-};
-
-static unsigned char remap_v4eater[28] =
-{
- /* filter crap for v4 */
-
- 128,18,2,0,4,8,16,20,28,37,
- 41,45,130,129,131,132,133,134,135,136,
- 146,147,175,65,66,64,2,18
-};
-
-static boolean filename_has_v1_format(char *filename)
-{
- char *basename = getBaseNamePtr(filename);
-
- return (strlen(basename) == 3 &&
- basename[0] == 'a' &&
- basename[1] >= 'a' && basename[1] <= 'k' &&
- basename[2] >= '0' && basename[2] <= '9');
-}
-
-int cleanup_em_level(unsigned char *src, int length, char *filename)
-{
- int file_version = FILE_VERSION_EM_UNKNOWN;
- int i;
-
- if (length >= 2172 &&
- src[2106] == 255 && /* version id: */
- src[2107] == 54 && /* '6' */
- src[2108] == 48 && /* '0' */
- src[2109] == 48) /* '0' */
- {
- /* ---------- this cave has V6 file format ---------- */
- file_version = FILE_VERSION_EM_V6;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v6[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v6[src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = remap_v6[src[i]];
- }
- else if (length >= 2110 &&
- src[2106] == 255 && /* version id: */
- src[2107] == 53 && /* '5' */
- src[2108] == 48 && /* '0' */
- src[2109] == 48) /* '0' */
- {
- /* ---------- this cave has V5 file format ---------- */
- file_version = FILE_VERSION_EM_V5;
-
- /* remap elements to internal EMC level format */
- for (i = 0; i < 2048; i++)
- src[i] = remap_v5[src[i]];
- for (i = 2048; i < 2084; i++)
- src[i] = remap_v5[src[i]];
- for (i = 2112; i < 2148; i++)
- src[i] = src[i - 64];
- }
-
-#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)