rnd-20091024-1-src
[rocksndiamonds.git] / src / files.c
index 54d75d6ccb4f4be3e2337849ea2def27c4811911..d0caceb5d47f3a1f17cdb50d65098c228c38b6a3 100644 (file)
@@ -182,6 +182,12 @@ static struct LevelFileConfigInfo chunk_config_INFO[] =
     &li.gems_needed,                   0
   },
 
+  {
+    -1,                                        -1,
+    TYPE_INTEGER,                      CONF_VALUE_32_BIT(2),
+    &li.random_seed,                   0
+  },
+
   {
     -1,                                        -1,
     TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(2),
@@ -218,6 +224,12 @@ static struct LevelFileConfigInfo chunk_config_INFO[] =
     &li.dont_collide_with_bits,                ~0      /* default: always deadly */
   },
 
+  {
+    -1,                                        -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(8),
+    &li.em_explodes_by_fire,           FALSE
+  },
+
   {
     -1,                                        -1,
     TYPE_INTEGER,                      CONF_VALUE_16_BIT(5),
@@ -311,6 +323,22 @@ static struct LevelFileConfigInfo chunk_config_ELEM[] =
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(3),
     &li.explosion_element[0],          EL_PLAYER_1
   },
+  {
+    EL_PLAYER_1,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(13),
+    &li.use_initial_inventory[0],      FALSE
+  },
+  {
+    EL_PLAYER_1,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(14),
+    &li.initial_inventory_size[0],     1
+  },
+  {
+    EL_PLAYER_1,                       -1,
+    TYPE_ELEMENT_LIST,                 CONF_VALUE_BYTES(1),
+    &li.initial_inventory_content[0][0],EL_EMPTY, NULL,
+    &li.initial_inventory_size[0],     1, MAX_INITIAL_INVENTORY_SIZE
+  },
 
   {
     EL_PLAYER_2,                       -1,
@@ -352,6 +380,22 @@ static struct LevelFileConfigInfo chunk_config_ELEM[] =
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(3),
     &li.explosion_element[1],          EL_PLAYER_2
   },
+  {
+    EL_PLAYER_2,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(13),
+    &li.use_initial_inventory[1],      FALSE
+  },
+  {
+    EL_PLAYER_2,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(14),
+    &li.initial_inventory_size[1],     1
+  },
+  {
+    EL_PLAYER_2,                       -1,
+    TYPE_ELEMENT_LIST,                 CONF_VALUE_BYTES(1),
+    &li.initial_inventory_content[1][0],EL_EMPTY, NULL,
+    &li.initial_inventory_size[1],     1, MAX_INITIAL_INVENTORY_SIZE
+  },
 
   {
     EL_PLAYER_3,                       -1,
@@ -393,6 +437,22 @@ static struct LevelFileConfigInfo chunk_config_ELEM[] =
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(3),
     &li.explosion_element[2],          EL_PLAYER_3
   },
+  {
+    EL_PLAYER_3,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(13),
+    &li.use_initial_inventory[2],      FALSE
+  },
+  {
+    EL_PLAYER_3,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(14),
+    &li.initial_inventory_size[2],     1
+  },
+  {
+    EL_PLAYER_3,                       -1,
+    TYPE_ELEMENT_LIST,                 CONF_VALUE_BYTES(1),
+    &li.initial_inventory_content[2][0],EL_EMPTY, NULL,
+    &li.initial_inventory_size[2],     1, MAX_INITIAL_INVENTORY_SIZE
+  },
 
   {
     EL_PLAYER_4,                       -1,
@@ -434,6 +494,22 @@ static struct LevelFileConfigInfo chunk_config_ELEM[] =
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(3),
     &li.explosion_element[3],          EL_PLAYER_4
   },
+  {
+    EL_PLAYER_4,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(13),
+    &li.use_initial_inventory[3],      FALSE
+  },
+  {
+    EL_PLAYER_4,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(14),
+    &li.initial_inventory_size[3],     1
+  },
+  {
+    EL_PLAYER_4,                       -1,
+    TYPE_ELEMENT_LIST,                 CONF_VALUE_BYTES(1),
+    &li.initial_inventory_content[3][0],EL_EMPTY, NULL,
+    &li.initial_inventory_size[3],     1, MAX_INITIAL_INVENTORY_SIZE
+  },
 
   {
     EL_EMERALD,                                -1,
@@ -806,8 +882,8 @@ static struct LevelFileConfigInfo chunk_config_CUSX_base[] =
   {
     -1,                                        -1,
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(1),
-    &xx_ei.gfx_element,                        EL_EMPTY_SPACE,
-    &yy_ei.gfx_element
+    &xx_ei.gfx_element_initial,                EL_EMPTY_SPACE,
+    &yy_ei.gfx_element_initial
   },
 
   {
@@ -1045,7 +1121,7 @@ static struct LevelFileConfigInfo chunk_config_CUSX_change[] =
   {
     -1,                                        -1,
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(5),
-    &xx_change.trigger_element,                EL_EMPTY_SPACE
+    &xx_change.initial_trigger_element,        EL_EMPTY_SPACE
   },
 
   {
@@ -1100,6 +1176,12 @@ static struct LevelFileConfigInfo chunk_config_CUSX_change[] =
     &xx_change.action_arg,             CA_ARG_UNDEFINED
   },
 
+  {
+    -1,                                        -1,
+    TYPE_ELEMENT,                      CONF_VALUE_16_BIT(7),
+    &xx_change.action_element,         EL_EMPTY_SPACE
+  },
+
   {
     -1,                                        -1,
     TYPE_CONTENT_LIST,                 CONF_VALUE_BYTES(1),
@@ -1132,7 +1214,7 @@ static struct LevelFileConfigInfo chunk_config_GRPX[] =
   {
     -1,                                        -1,
     TYPE_ELEMENT,                      CONF_VALUE_16_BIT(1),
-    &xx_ei.gfx_element,                        EL_EMPTY_SPACE
+    &xx_ei.gfx_element_initial,                EL_EMPTY_SPACE
   },
 
   {
@@ -1505,8 +1587,10 @@ static void setLevelInfoToDefaults(struct LevelInfo *level)
   *level = li;         /* copy temporary buffer back to level data */
 
   setLevelInfoToDefaults_EM();
+  setLevelInfoToDefaults_SP();
 
   level->native_em_level = &native_em_level;
+  level->native_sp_level = &native_sp_level;
 
   level->file_version = FILE_VERSION_ACTUAL;
   level->game_version = GAME_VERSION_ACTUAL;
@@ -1620,6 +1704,9 @@ static void setLevelInfoToDefaults(struct LevelInfo *level)
 
   level->changed = FALSE;
 
+  /* set all bug compatibility flags to "false" => do not emulate this bug */
+  level->use_action_after_change_bug = FALSE;
+
   if (leveldir_current == NULL)                /* only when dumping level */
     return;
 
@@ -1950,6 +2037,7 @@ int getMappedElement(int element)
 
     case EL_KEY_OBSOLETE:
       element = EL_KEY_1;
+      break;
 
     case EL_EM_KEY_1_FILE_OBSOLETE:
       element = EL_EM_KEY_1;
@@ -2355,7 +2443,7 @@ static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
     ReadUnusedBytesFromFile(file, 7);
 
     ei->use_gfx_element = getFile8Bit(file);
-    ei->gfx_element = getMappedElement(getFile16BitBE(file));
+    ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
 
     ei->collect_score_initial = getFile8Bit(file);
     ei->collect_count_initial = getFile8Bit(file);
@@ -2384,7 +2472,7 @@ static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
     ei->change->delay_random = getFile16BitBE(file);
     ei->change->delay_frames = getFile16BitBE(file);
 
-    ei->change->trigger_element = getMappedElement(getFile16BitBE(file));
+    ei->change->initial_trigger_element= getMappedElement(getFile16BitBE(file));
 
     ei->change->explode = getFile8Bit(file);
     ei->change->use_target_content = getFile8Bit(file);
@@ -2454,7 +2542,7 @@ static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level)
   ei->use_last_ce_value = getFile8Bit(file);
 
   ei->use_gfx_element = getFile8Bit(file);
-  ei->gfx_element = getMappedElement(getFile16BitBE(file));
+  ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
 
   ei->collect_score_initial = getFile8Bit(file);
   ei->collect_count_initial = getFile8Bit(file);
@@ -2517,7 +2605,7 @@ static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level)
     change->delay_random = getFile16BitBE(file);
     change->delay_frames = getFile16BitBE(file);
 
-    change->trigger_element = getMappedElement(getFile16BitBE(file));
+    change->initial_trigger_element = getMappedElement(getFile16BitBE(file));
 
     change->explode = getFile8Bit(file);
     change->use_target_content = getFile8Bit(file);
@@ -2587,7 +2675,7 @@ static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level)
   group->num_elements = getFile8Bit(file);
 
   ei->use_gfx_element = getFile8Bit(file);
-  ei->gfx_element = getMappedElement(getFile16BitBE(file));
+  ei->gfx_element_initial = getMappedElement(getFile16BitBE(file));
 
   group->choice_mode = getFile8Bit(file);
 
@@ -3713,30 +3801,13 @@ void CopyNativeLevel_EM_to_RND(struct LevelInfo *level)
   }
 }
 
-static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
-                                    struct LevelFileInfo *level_file_info)
-{
-  if (!LoadNativeLevel_EM(level_file_info->filename))
-    level->no_valid_file = TRUE;
-}
-
-void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
-{
-  if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
-    CopyNativeLevel_RND_to_EM(level);
-}
-
-void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
-{
-  if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
-    CopyNativeLevel_EM_to_RND(level);
-}
-
 
 /* ------------------------------------------------------------------------- */
 /* functions for loading SP level                                            */
 /* ------------------------------------------------------------------------- */
 
+#if 0
+
 #define NUM_SUPAPLEX_LEVELS_PER_PACKAGE        111
 #define SP_LEVEL_SIZE                  1536
 #define SP_LEVEL_XSIZE                 60
@@ -3927,7 +3998,8 @@ static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
   }
 
   /* position file stream to the requested level inside the level package */
-  if (fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
+  if (level_file_info->packed &&
+      fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
   {
     level->no_valid_file = TRUE;
 
@@ -4081,6 +4153,185 @@ static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
     *level = multipart_level;
 }
 
+#endif
+
+void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
+{
+  struct LevelInfo_SP *level_sp = level->native_sp_level;
+  LevelInfoType *header = &level_sp->header;
+  int i, x, y;
+
+  level_sp->width = level->fieldx;
+  level_sp->height = level->fieldy;
+
+  for (x = 0; x < level->fieldx; x++)
+  {
+    for (y = 0; y < level->fieldy; y++)
+    {
+      int element_old = level->field[x][y];
+      int element_new;
+
+      if (element_old >= EL_SP_START &&
+         element_old <= EL_SP_END)
+       element_new = element_old - EL_SP_START;
+      else if (element_old == EL_INVISIBLE_WALL)
+       element_new = 0x28;
+      else
+       element_new = 0x20;     /* map unknown elements to yellow "hardware" */
+
+      level_sp->playfield[x][y] = element_new;
+    }
+  }
+
+  header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
+
+  for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
+    header->LevelTitle[i] = level->name[i];
+  /* !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!! */
+
+  header->InfotronsNeeded = level->gems_needed;
+
+  /* !!! ADD SPECIAL PORT DATABASE STUFF !!! */
+}
+
+void CopyNativeLevel_SP_to_RND(struct LevelInfo *level)
+{
+  struct LevelInfo_SP *level_sp = level->native_sp_level;
+  LevelInfoType *header = &level_sp->header;
+  int i, x, y;
+
+  level->fieldx = level_sp->width;
+  level->fieldy = level_sp->height;
+
+  for (x = 0; x < level->fieldx; x++)
+  {
+    for (y = 0; y < level->fieldy; y++)
+    {
+      int element_old = level_sp->playfield[x][y];
+      int element_new;
+
+      if (element_old <= 0x27)
+       element_new = getMappedElement(EL_SP_START + element_old);
+      else if (element_old == 0x28)
+       element_new = EL_INVISIBLE_WALL;
+      else
+      {
+       Error(ERR_WARN, "invalid element %d at position %d, %d",
+             element_old, x, y);
+
+       element_new = EL_UNKNOWN;
+      }
+
+      level->field[x][y] = element_new;
+    }
+  }
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    level->initial_player_gravity[i] =
+      (header->InitialGravity == 1 ? TRUE : FALSE);
+
+  for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
+    level->name[i] = header->LevelTitle[i];
+  level->name[SP_LEVEL_NAME_LEN] = '\0';
+
+  level->gems_needed = header->InfotronsNeeded;
+
+  for (i = 0; i < header->SpecialPortCount; i++)
+  {
+    SpecialPortType *port = &header->SpecialPort[i];
+    int port_location = port->PortLocation;
+    int gravity = port->Gravity;
+    int port_x, port_y, port_element;
+
+    port_x = (port_location / 2) % level->fieldx;
+    port_y = (port_location / 2) / level->fieldx;
+
+    if (port_x < 0 || port_x >= level->fieldx ||
+       port_y < 0 || port_y >= level->fieldy)
+    {
+      Error(ERR_WARN, "special port position (%d, %d) out of bounds",
+           port_x, port_y);
+
+      continue;
+    }
+
+    port_element = level->field[port_x][port_y];
+
+    if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
+       port_element > EL_SP_GRAVITY_PORT_UP)
+    {
+      Error(ERR_WARN, "no special port at position (%d, %d)", port_x, port_y);
+
+      continue;
+    }
+
+    /* change previous (wrong) gravity inverting special port to either
+       gravity enabling special port or gravity disabling special port */
+    level->field[port_x][port_y] +=
+      (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
+       EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
+  }
+
+  /* change special gravity ports without database entries to normal ports */
+  for (x = 0; x < level->fieldx; x++)
+    for (y = 0; y < level->fieldy; y++)
+      if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
+         level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
+       level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
+
+  level->time = 0;                     /* no time limit */
+  level->amoeba_speed = 0;
+  level->time_magic_wall = 0;
+  level->time_wheel = 0;
+  level->amoeba_content = EL_EMPTY;
+
+#if 1
+  /* original Supaplex does not use score values -- use default values */
+#else
+  for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
+    level->score[i] = 0;
+#endif
+
+  /* there are no yamyams in supaplex levels */
+  for (i = 0; i < level->num_yamyam_contents; i++)
+    for (x = 0; x < 3; x++)
+      for (y = 0; y < 3; y++)
+       level->yamyam_content[i].e[x][y] = EL_EMPTY;
+}
+
+static void setTapeInfoToDefaults();
+
+static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
+{
+  struct LevelInfo_SP *level_sp = level->native_sp_level;
+  struct DemoInfo_SP *demo = &level_sp->demo;
+  int i;
+
+  /* always start with reliable default values */
+  setTapeInfoToDefaults();
+
+  tape.level_nr = demo->level_nr;      /* (currently not used) */
+  tape.length = demo->length - 1;      /* without "end of demo" byte */
+  tape.random_seed = level_sp->header.DemoRandomSeed;
+
+  // tape.date = <SET FROM FILE DATE OF *.SP FILE>
+
+  for (i = 0; i < demo->length - 1; i++)
+  {
+    int demo_action = demo->data[i] & 0x0f;
+    int demo_repeat = (demo->data[i] & 0xf0) >> 4;
+
+    tape.pos[i].action[0] = map_key_SP_to_RND(demo_action);
+    tape.pos[i].delay = demo_repeat + 1;
+  }
+
+  tape.length_seconds = GetTapeLength();
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* functions for loading DC level                                            */
+/* ------------------------------------------------------------------------- */
 
 #define DC_LEVEL_HEADER_SIZE           344
 
@@ -6051,6 +6302,47 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
 #endif
 
 
+/* ------------------------------------------------------------------------- */
+/* functions for handling native levels                                      */
+/* ------------------------------------------------------------------------- */
+
+static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
+                                    struct LevelFileInfo *level_file_info)
+{
+  if (!LoadNativeLevel_EM(level_file_info->filename))
+    level->no_valid_file = TRUE;
+}
+
+static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
+                                    struct LevelFileInfo *level_file_info)
+{
+  int pos = 0;
+
+  /* determine position of requested level inside level package */
+  if (level_file_info->packed)
+    pos = level_file_info->nr - leveldir_current->first_level;
+
+  if (!LoadNativeLevel_SP(level_file_info->filename, pos))
+    level->no_valid_file = TRUE;
+}
+
+void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
+{
+  if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
+    CopyNativeLevel_RND_to_EM(level);
+  else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
+    CopyNativeLevel_RND_to_SP(level);
+}
+
+void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
+{
+  if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
+    CopyNativeLevel_EM_to_RND(level);
+  else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
+    CopyNativeLevel_SP_to_RND(level);
+}
+
+
 /* ------------------------------------------------------------------------- */
 /* functions for loading generic level                                       */
 /* ------------------------------------------------------------------------- */
@@ -6074,6 +6366,7 @@ void LoadLevelFromFileInfo(struct LevelInfo *level,
 
     case LEVEL_FILE_TYPE_SP:
       LoadLevelFromFileInfo_SP(level, level_file_info);
+      level->game_engine_type = GAME_ENGINE_TYPE_SP;
       break;
 
     case LEVEL_FILE_TYPE_DC:
@@ -6239,7 +6532,7 @@ static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
 
   /* try to detect and fix "Snake Bite" levels, which are broken with 3.2.0 */
   {
-    int element = EL_CUSTOM_START + 255;
+    int element = EL_CUSTOM_256;
     struct ElementInfo *ei = &element_info[element];
     struct ElementChangeInfo *change = &ei->change_page[0];
 
@@ -6259,9 +6552,67 @@ static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
       change->target_element = EL_PLAYER_1;
   }
 
+#if 1
+  /* try to detect and fix "Zelda" style levels, which are broken with 3.2.5 */
+  if (level->game_version < VERSION_IDENT(3,2,5,0))
+  {
+    /* This is needed to fix a problem that was caused by a bugfix in function
+       game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
+       corrects the behaviour when a custom element changes to another custom
+       element with a higher element number that has change actions defined.
+       Normally, only one change per frame is allowed for custom elements.
+       Therefore, it is checked if a custom element already changed in the
+       current frame; if it did, subsequent changes are suppressed.
+       Unfortunately, this is only checked for element changes, but not for
+       change actions, which are still executed. As the function above loops
+       through all custom elements from lower to higher, an element change
+       resulting in a lower CE number won't be checked again, while a target
+       element with a higher number will also be checked, and potential change
+       actions will get executed for this CE, too (which is wrong), while
+       further changes are ignored (which is correct). As this bugfix breaks
+       Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
+       few other levels like Alan Bond's "FMV"), allow the previous, incorrect
+       behaviour for existing levels and tapes that make use of this bug */
+
+    level->use_action_after_change_bug = TRUE;
+  }
+#else
+  /* !!! THIS DOES NOT FIX "Zelda I" (GRAPHICALLY) AND "Alan's FMV" LEVELS */
+  /* try to detect and fix "Zelda II" levels, which are broken with 3.2.5 */
+  {
+    int element = EL_CUSTOM_16;
+    struct ElementInfo *ei = &element_info[element];
+
+    /* This is needed to fix a problem that was caused by a bugfix in function
+       game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
+       corrects the behaviour when a custom element changes to another custom
+       element with a higher element number that has change actions defined.
+       Normally, only one change per frame is allowed for custom elements.
+       Therefore, it is checked if a custom element already changed in the
+       current frame; if it did, subsequent changes are suppressed.
+       Unfortunately, this is only checked for element changes, but not for
+       change actions, which are still executed. As the function above loops
+       through all custom elements from lower to higher, an element change
+       resulting in a lower CE number won't be checked again, while a target
+       element with a higher number will also be checked, and potential change
+       actions will get executed for this CE, too (which is wrong), while
+       further changes are ignored (which is correct). As this bugfix breaks
+       Zelda II (but no other levels), allow the previous, incorrect behaviour
+       for this outstanding level set to not break the game or existing tapes */
+
+    if (strncmp(leveldir_current->identifier, "zelda2", 6) == 0 ||
+       strncmp(ei->description, "scanline - row 1", 16) == 0)
+      level->use_action_after_change_bug = TRUE;
+  }
+#endif
+
   /* not centering level after relocating player was default only in 3.2.3 */
   if (level->game_version == VERSION_IDENT(3,2,3,0))   /* (no pre-releases) */
     level->shifted_relocation = TRUE;
+
+  /* EM style elements always chain-exploded in R'n'D engine before 3.2.6 */
+  if (level->game_version < VERSION_IDENT(3,2,6,0))
+    level->em_explodes_by_fire = TRUE;
 }
 
 static void LoadLevel_InitElements(struct LevelInfo *level, char *filename)
@@ -6393,6 +6744,7 @@ static void LoadLevel_InitElements(struct LevelInfo *level, char *filename)
   /* initialize element properties for level editor etc. */
   InitElementPropertiesEngine(level->game_version);
   InitElementPropertiesAfterLoading(level->game_version);
+  InitElementPropertiesGfxElement();
 }
 
 static void LoadLevel_InitPlayfield(struct LevelInfo *level, char *filename)
@@ -6771,7 +7123,7 @@ static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
        WriteUnusedBytesToFile(file, 7);
 
        putFile8Bit(file, ei->use_gfx_element);
-       putFile16BitBE(file, ei->gfx_element);
+       putFile16BitBE(file, ei->gfx_element_initial);
 
        putFile8Bit(file, ei->collect_score_initial);
        putFile8Bit(file, ei->collect_count_initial);
@@ -6797,7 +7149,7 @@ static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
        putFile16BitBE(file, ei->change->delay_random);
        putFile16BitBE(file, ei->change->delay_frames);
 
-       putFile16BitBE(file, ei->change->trigger_element);
+       putFile16BitBE(file, ei->change->initial_trigger_element);
 
        putFile8Bit(file, ei->change->explode);
        putFile8Bit(file, ei->change->use_target_content);
@@ -6850,7 +7202,7 @@ static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
   putFile8Bit(file, ei->use_last_ce_value);
 
   putFile8Bit(file, ei->use_gfx_element);
-  putFile16BitBE(file, ei->gfx_element);
+  putFile16BitBE(file, ei->gfx_element_initial);
 
   putFile8Bit(file, ei->collect_score_initial);
   putFile8Bit(file, ei->collect_count_initial);
@@ -6909,7 +7261,7 @@ static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
     putFile16BitBE(file, change->delay_random);
     putFile16BitBE(file, change->delay_frames);
 
-    putFile16BitBE(file, change->trigger_element);
+    putFile16BitBE(file, change->initial_trigger_element);
 
     putFile8Bit(file, change->explode);
     putFile8Bit(file, change->use_target_content);
@@ -6961,7 +7313,7 @@ static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
   putFile8Bit(file, group->num_elements);
 
   putFile8Bit(file, ei->use_gfx_element);
-  putFile16BitBE(file, ei->gfx_element);
+  putFile16BitBE(file, ei->gfx_element_initial);
 
   putFile8Bit(file, group->choice_mode);
 
@@ -7693,6 +8045,13 @@ void LoadSolutionTape(int nr)
   char *filename = getSolutionTapeFilename(nr);
 
   LoadTapeFromFilename(filename);
+
+#if 1
+  if (TAPE_IS_EMPTY(tape) &&
+      level.game_engine_type == GAME_ENGINE_TYPE_SP &&
+      level.native_sp_level->demo.is_available)
+    CopyNativeTape_SP_to_RND(&level);
+#endif
 }
 
 static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
@@ -9740,7 +10099,7 @@ void ConvertLevels()
 
 
 /* ------------------------------------------------------------------------- */
-/* create images for use in level sketches (raw BMP format)                  */
+/* create and save images for use in level sketches (raw BMP format)         */
 /* ------------------------------------------------------------------------- */
 
 void CreateLevelSketchImages()
@@ -9750,6 +10109,8 @@ void CreateLevelSketchImages()
   Bitmap *bitmap2;
   int i;
 
+  InitElementPropertiesGfxElement();
+
   bitmap1 = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
   bitmap2 = CreateBitmap(MINI_TILEX, MINI_TILEY, DEFAULT_DEPTH);
 
@@ -9757,7 +10118,8 @@ void CreateLevelSketchImages()
   {
     Bitmap *src_bitmap;
     int src_x, src_y;
-    int graphic = el2edimg(i);
+    int element = getMappedElement(i);
+    int graphic = el2edimg(element);
     char basename1[16];
     char basename2[16];
     char *filename1;
@@ -9799,3 +10161,100 @@ void CreateLevelSketchImages()
   CloseAllAndExit(0);
 #endif
 }
+
+
+/* ------------------------------------------------------------------------- */
+/* create and save images for custom and group elements (raw BMP format)     */
+/* ------------------------------------------------------------------------- */
+
+void CreateCustomElementImages()
+{
+#if defined(TARGET_SDL)
+  char *filename = "graphics.classic/RocksCE.bmp";
+  Bitmap *bitmap;
+  Bitmap *src_bitmap;
+  int dummy_graphic = IMG_CUSTOM_99;
+  int yoffset_ce = 0;
+  int yoffset_ge = (TILEY * NUM_CUSTOM_ELEMENTS / 16);
+  int src_x, src_y;
+  int i;
+
+  bitmap = CreateBitmap(TILEX * 16 * 2,
+                       TILEY * (NUM_CUSTOM_ELEMENTS + NUM_GROUP_ELEMENTS) / 16,
+                       DEFAULT_DEPTH);
+
+  getGraphicSource(dummy_graphic, 0, &src_bitmap, &src_x, &src_y);
+
+  for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+  {
+    int x = i % 16;
+    int y = i / 16;
+    int ii = i + 1;
+    int j;
+
+    BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
+              TILEX * x, TILEY * y + yoffset_ce);
+
+    BlitBitmap(src_bitmap, bitmap, 0, TILEY, TILEX, TILEY,
+              TILEX * x + TILEX * 16, TILEY * y + yoffset_ce);
+
+    for (j = 2; j >= 0; j--)
+    {
+      int c = ii % 10;
+
+      BlitBitmap(src_bitmap, bitmap, TILEX + c * 7, 0, 6, 10,
+                TILEX * x + 6 + j * 7,
+                TILEY * y + 11 + yoffset_ce);
+
+      BlitBitmap(src_bitmap, bitmap, TILEX + c * 8, TILEY, 6, 10,
+                TILEX * 16 + TILEX * x + 6 + j * 8,
+                TILEY * y + 10 + yoffset_ce);
+
+      ii /= 10;
+    }
+  }
+
+  for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
+  {
+    int x = i % 16;
+    int y = i / 16;
+    int ii = i + 1;
+    int j;
+
+    BlitBitmap(src_bitmap, bitmap, 0, 0, TILEX, TILEY,
+              TILEX * x, TILEY * y + yoffset_ge);
+
+    BlitBitmap(src_bitmap, bitmap, 0, TILEY, TILEX, TILEY,
+              TILEX * x + TILEX * 16, TILEY * y + yoffset_ge);
+
+    for (j = 1; j >= 0; j--)
+    {
+      int c = ii % 10;
+
+      BlitBitmap(src_bitmap, bitmap, TILEX + c * 10, 11, 10, 10,
+                TILEX * x + 6 + j * 10,
+                TILEY * y + 11 + yoffset_ge);
+
+      BlitBitmap(src_bitmap, bitmap, TILEX + c * 8, TILEY + 12, 6, 10,
+                TILEX * 16 + TILEX * x + 10 + j * 8,
+                TILEY * y + 10 + yoffset_ge);
+
+      ii /= 10;
+    }
+  }
+
+  if (SDL_SaveBMP(bitmap->surface, filename) != 0)
+    Error(ERR_EXIT, "cannot save CE graphics file '%s'", filename);
+
+  FreeBitmap(bitmap);
+
+  CloseAllAndExit(0);
+#endif
+}
+
+#if 0
+void CreateLevelSketchImages_TEST()
+{
+  void CreateCustomElementImages()
+}
+#endif