added flags for "level solved" and "game over" to EM engine
[rocksndiamonds.git] / src / game_em / convert.c
index e695f3735497af8c8443d68c97a0c2f010bcd19f..d61e33eec5db472f1f55df60d9bad4b77221fe8b 100644 (file)
@@ -8,11 +8,11 @@
  * inconsequential, but no doubt it will break some caves.
  */
 
-#include "global.h"
-#include "tile.h"
-#include "level.h"
+#include "main_em.h"
 
 
+#define ALLOW_ROLLING_SPRING
+
 static unsigned char remap_v6[256] =
 {
   /* filter crap for v6 */
@@ -22,7 +22,11 @@ static unsigned char remap_v6[256] =
   0,16,2,18,       36,37,37,37,     40,41,42,43,     44,45,128,128,
   128,148,148,     148,45,45,45,    148,0,57,58,     59,60,61,62,63,
 
+#ifdef ALLOW_ROLLING_SPRING
+  64,65,66,67,     68,69,69,71,     72,73,74,75,     118,75,75,75,
+#else
   64,65,66,67,     68,69,69,69,     69,73,74,75,     118,75,75,75,
+#endif
   75,75,75,75,     75,153,153,153,  153,153,153,153, 153,153,153,153,
   153,153,153,99,  100,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,
@@ -97,20 +101,31 @@ static unsigned char remap_v4eater[28] =
   146,147,175,65,66,64,2,18
 };
 
-int cleanup_em_level(unsigned char *src, int length)
+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 = 0;
+  int file_version = FILE_VERSION_EM_UNKNOWN;
   int i;
 
   if (length >= 2172 &&
-      src[2106] == 255 &&
-      src[2107] == 54 &&
-      src[2108] == 48 &&
-      src[2109] == 48)
+      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++)
@@ -119,14 +134,15 @@ int cleanup_em_level(unsigned char *src, int length)
       src[i] = remap_v6[src[i]];
   }
   else if (length >= 2110 &&
-          src[2106] == 255 &&
-          src[2107] == 53 &&
-          src[2108] == 48 &&
-          src[2109] == 48)
+          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++)
@@ -134,83 +150,105 @@ int cleanup_em_level(unsigned char *src, int length)
     for (i = 2112; i < 2148; i++)
       src[i] = src[i - 64];
   }
-#if 0
-  else if (length >= 2106)     /* !!! TEST ONLY: SHOW BROKEN LEVELS !!! */
-#else
   else if (length >= 2106 &&
-          src[1983] == 116)
-#endif
+          (src[1983] == 27 ||          /* encrypted (only EM I/II/III) */
+           src[1983] == 116 ||         /* unencrypted (usual case) */
+           src[1983] == 131))          /* unencrypted (rare case) */
   {
-    /* ---------- this cave has V4 file format ---------- */
-    file_version = FILE_VERSION_EM_V4;
+    /* ---------- this cave has V1, V2 or V3 file 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 &&
-          src[1983] == 27)
-  {
-    unsigned char j = 94;
+    boolean fix_copyright = FALSE;
 
-    /* ---------- this cave has V3 file format ---------- */
-    file_version = FILE_VERSION_EM_V3;
+    /*
+      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)
+    */
 
-    for (i = 0; i < 2106; i++)
-      src[i] = (src[i] ^ (j += 7)) - 0x11;
-    src[1] = 131;
-    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 &&
-          src[1983] == 27)
-  {
-    unsigned char j = 94;
+    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 */
 
-    /* ---------- this cave has V3 file format ---------- */
-    file_version = FILE_VERSION_EM_V3;
+      /* 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;
+    }
 
-    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 (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' */
+    }
   }
-#endif
   else
   {
     /* ---------- this cave has unknown file format ---------- */
 
-#if 1
-    printf("::: %d, %d\n", src[0], src[1983]);
-#endif
+    /* 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 0;
+    return FILE_VERSION_EM_UNKNOWN;
   }
 
   if (file_version < FILE_VERSION_EM_V6)
   {
     /* id */
-    src[2106] = 255;
-    src[2107] = 54;
-    src[2108] = 48;
-    src[2109] = 48;
+    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;
 
@@ -232,11 +270,34 @@ int cleanup_em_level(unsigned char *src, int length)
       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 */
@@ -244,13 +305,25 @@ int cleanup_em_level(unsigned char *src, int length)
   {
     if (src[i] == 63)          /* 'Xacid_s' (acid pool, bottom middle) */
     {
-      if (file_version == FILE_VERSION_EM_V4 &&
+      if (file_version <= FILE_VERSION_EM_V2 &&
          i < 2048 - 64 && src[i + 64] == 63)
-       src[i - 64] = 255;      /* replace element above with 'Xfake_acid_1' */
+      {
+       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 */
@@ -381,9 +454,9 @@ int cleanup_em_level(unsigned char *src, int length)
   /* size of v6 cave */
   length = 2172;
 
-#if 1
+#if 0
   if (options.debug)
-    printf("::: EM level file version: %d\n", file_version);
+    Error(ERR_DEBUG, "EM level file version: %d", file_version);
 #endif
 
   return file_version;
@@ -408,6 +481,9 @@ int cleanup_em_level(unsigned char *src, int length)
  *   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:
  *
@@ -436,6 +512,11 @@ int cleanup_em_level(unsigned char *src, int length)
  * - 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] =
@@ -460,10 +541,17 @@ static unsigned short remap_emerald[256] =
   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,
@@ -538,10 +626,24 @@ static unsigned short remap_emerald[256] =
   Xblank,              Xblank,         Xblank,         Xblank,
 #else
   /* special elements added to solve compatibility problems */
-  Xblank,              Xblank,         Xblank,         Xfake_acid_1
+  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;
+}
+
 void convert_em_level(unsigned char *src, int file_version)
 {
   static int eater_offset[8] =
@@ -611,9 +713,10 @@ void convert_em_level(unsigned char *src, int file_version)
 
   for (i = 0; i < 8; i++)
     for (x = 0; x < 9; x++)
-      lev.eater_array[i][x] = remap_emerald[src[eater_offset[i] + x]];
+      lev.eater_array[i][x] =
+       get_em_element(src[eater_offset[i] + x], file_version);
 
-  temp = remap_emerald[src[0x86F]];
+  temp = get_em_element(src[0x86F], file_version);
   for (y = 0; y < 8; y++)
   {
     if (src[0x872] & 1)
@@ -883,18 +986,13 @@ void convert_em_level(unsigned char *src, int file_version)
   temp = 0;
   for (y = 0; y < lev.height; y++)
     for (x = 0; x < lev.width; x++)
-      native_em_level.cave[x + 1][y + 1] = remap_emerald[src[temp++]];
+      native_em_level.cave[x + 1][y + 1] =
+       get_em_element(src[temp++], file_version);
 
   /* at last, set the two players at their positions in the playfield */
   /* (native EM[C] levels always have exactly two players in a level) */
-#if 1
   for (i = 0; i < 2; i++)
     native_em_level.cave[ply[i].x_initial][ply[i].y_initial] = Zplayer;
-#else
-  for (i = 0; i < 2; i++)
-    if (ply[i].alive_initial)
-      native_em_level.cave[ply[i].x_initial][ply[i].y_initial] = Zplayer;
-#endif
 
   native_em_level.file_version = file_version;
 }
@@ -903,6 +1001,7 @@ void prepare_em_level(void)
 {
   int i, x, y;
   int players_left;
+  boolean team_mode;
 
   /* reset all runtime variables to their initial values */
 
@@ -918,11 +1017,7 @@ void prepare_em_level(void)
     for (x = 0; x < WIDTH; x++)
       Draw[y][x] = Cave[y][x];
 
-#if 1
   lev.time_initial = lev.time_seconds;
-#else
-  lev.time_initial = (lev.time_seconds * 50 + 7) / 8;
-#endif
   lev.time = lev.time_initial;
 
   lev.required = lev.required_initial;
@@ -969,8 +1064,10 @@ void prepare_em_level(void)
     }
   }
 
-  if (!setup.team_mode)
-    lev.home_initial = MIN(lev.home_initial, 1);
+  team_mode = getTeamMode_EM();
+
+  if (!team_mode)
+    lev.home_initial = 1;
 
   lev.home = lev.home_initial;
   players_left = lev.home_initial;
@@ -986,7 +1083,12 @@ void prepare_em_level(void)
       }
       else
       {
-       native_em_level.cave[ply[i].x_initial][ply[i].y_initial] = Xblank;
+       int x = ply[i].x_initial;
+       int y = ply[i].y_initial;
+
+       native_em_level.cave[x][y] = Xblank;
+
+       Cave[y][x] = Next[y][x] = Draw[y][x] = Xblank;
       }
     }
   }
@@ -1005,10 +1107,24 @@ void prepare_em_level(void)
     ply[i].joy_n = ply[i].joy_e = ply[i].joy_s = ply[i].joy_w = 0;
     ply[i].joy_snap  = ply[i].joy_drop = 0;
     ply[i].joy_stick = ply[i].joy_spin = 0;
-
-#if 0
-    printf("player %d: x/y == %d/%d, alive == %d\n",
-          i, ply[i].x_initial, ply[i].y_initial, ply[i].alive);
-#endif
   }
+
+  // the following engine variables are initialized to version-specific values
+  // in function InitGameEngine() (src/game.c):
+  //
+  // - game_em.use_single_button (default: TRUE)
+  // - game_em.use_snap_key_bug (default: FALSE)
+
+  game_em.level_solved = FALSE;
+  game_em.game_over = FALSE;
+
+  game_em.any_player_moving = FALSE;
+  game_em.any_player_snapping = FALSE;
+
+  game_em.last_moving_player = 0;      /* default: first player */
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    game_em.last_player_direction[i] = MV_NONE;
+
+  lev.exit_x = lev.exit_y = -1;        /* kludge for playing player exit sound */
 }