fixed compiler warning about uninitialized local variable
[rocksndiamonds.git] / src / files.c
index 7237f57722a570efe9f36d227846ae8b24400abf..6c150ea0f86b3ea4be3de7cc8a7ed79b252036c2 100644 (file)
@@ -1,15 +1,13 @@
-/***********************************************************
-* Rocks'n'Diamonds -- McDuffin Strikes Back!               *
-*----------------------------------------------------------*
-* (c) 1995-2006 Artsoft Entertainment                      *
-*               Holger Schemel                             *
-*               Detmolder Strasse 189                      *
-*               33604 Bielefeld                            *
-*               Germany                                    *
-*               e-mail: info@artsoft.org                   *
-*----------------------------------------------------------*
-* files.c                                                  *
-***********************************************************/
+// ============================================================================
+// Rocks'n'Diamonds - McDuffin Strikes Back!
+// ----------------------------------------------------------------------------
+// (c) 1995-2014 by Artsoft Entertainment
+//                         Holger Schemel
+//                 info@artsoft.org
+//                 http://www.artsoft.org/
+// ----------------------------------------------------------------------------
+// files.c
+// ============================================================================
 
 #include <ctype.h>
 #include <sys/stat.h>
 #include "init.h"
 #include "tools.h"
 #include "tape.h"
+#include "config.h"
 
+#define ENABLE_UNUSED_CODE     0       /* currently unused functions */
+#define ENABLE_HISTORIC_CHUNKS 0       /* only for historic reference */
+#define ENABLE_RESERVED_CODE   0       /* reserved for later use */
 
 #define CHUNK_ID_LEN           4       /* IFF style chunk id length  */
 #define CHUNK_SIZE_UNDEFINED   0       /* undefined chunk size == 0  */
@@ -287,6 +289,11 @@ static struct LevelFileConfigInfo chunk_config_ELEM[] =
     TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(12),
     &li.shifted_relocation,            FALSE
   },
+  {
+    EL_PLAYER_1,                       -1,
+    TYPE_BOOLEAN,                      CONF_VALUE_8_BIT(15),
+    &li.lazy_relocation,               FALSE
+  },
 
   /* (these values are different for each player) */
   {
@@ -869,8 +876,8 @@ static struct LevelFileConfigInfo chunk_config_CUSX_base[] =
     &xx_ei.properties[EP_BITFIELD_BASE_NR], EP_BITMASK_BASE_DEFAULT,
     &yy_ei.properties[EP_BITFIELD_BASE_NR]
   },
-#if 0
-  /* (reserved) */
+#if ENABLE_RESERVED_CODE
+  /* (reserved for later use) */
   {
     -1,                                        -1,
     TYPE_BITFIELD,                     CONF_VALUE_32_BIT(2),
@@ -1322,13 +1329,6 @@ filetype_id_list[] =
 
 static boolean check_special_flags(char *flag)
 {
-#if 0
-  printf("::: '%s', '%s', '%s'\n",
-        flag,
-        options.special_flags,
-        leveldir_current->special_flags);
-#endif
-
   if (strEqual(options.special_flags, flag) ||
       strEqual(leveldir_current->special_flags, flag))
     return TRUE;
@@ -1577,11 +1577,6 @@ void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
 {
   xx_change = *change;         /* copy change data into temporary buffer */
 
-#if 0
-  /* (not needed; set by setConfigToDefaultsFromConfigList()) */
-  xx_num_contents = 1;
-#endif
-
   setConfigToDefaultsFromConfigList(chunk_config_CUSX_change);
 
   *change = xx_change;
@@ -1596,8 +1591,6 @@ void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
   change->post_change_function = NULL;
 }
 
-#if 1
-
 static void setLevelInfoToDefaults_Level(struct LevelInfo *level)
 {
   int i, x, y;
@@ -1621,18 +1614,21 @@ static void setLevelInfoToDefaults_Level(struct LevelInfo *level)
   level->encoding_16bit_yamyam = TRUE;
   level->encoding_16bit_amoeba = TRUE;
 
-  for (x = 0; x < MAX_LEV_FIELDX; x++)
-    for (y = 0; y < MAX_LEV_FIELDY; y++)
-      level->field[x][y] = EL_SAND;
-
+  /* clear level name and level author string buffers */
   for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
     level->name[i] = '\0';
   for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
     level->author[i] = '\0';
 
+  /* set level name and level author to default values */
   strcpy(level->name, NAMELESS_LEVEL_NAME);
   strcpy(level->author, ANONYMOUS_NAME);
 
+  /* set level playfield to playable default level with player and exit */
+  for (x = 0; x < MAX_LEV_FIELDX; x++)
+    for (y = 0; y < MAX_LEV_FIELDY; y++)
+      level->field[x][y] = EL_SAND;
+
   level->field[0][0] = EL_PLAYER_1;
   level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
 
@@ -1770,195 +1766,23 @@ static void setLevelInfoToDefaults_Elements(struct LevelInfo *level)
 }
 
 static void setLevelInfoToDefaults(struct LevelInfo *level,
-                                  boolean level_info_only)
+                                  boolean level_info_only,
+                                  boolean reset_file_status)
 {
   setLevelInfoToDefaults_Level(level);
 
   if (!level_info_only)
     setLevelInfoToDefaults_Elements(level);
 
-  level->no_valid_file = FALSE;
-
-  level->changed = FALSE;
-}
-
-#else
-
-static void setLevelInfoToDefaults(struct LevelInfo *level,
-                                  boolean level_info_only)
-{
-  static boolean clipboard_elements_initialized = FALSE;
-  int i, x, y;
-
-  if (level_info_only)
-    return;
-
-  InitElementPropertiesStatic();
-
-  li = *level;         /* copy level data into temporary buffer */
-
-  setConfigToDefaultsFromConfigList(chunk_config_INFO);
-  setConfigToDefaultsFromConfigList(chunk_config_ELEM);
-
-  *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;
-
-  level->creation_date = getCurrentDate();
-
-  level->encoding_16bit_field  = TRUE;
-  level->encoding_16bit_yamyam = TRUE;
-  level->encoding_16bit_amoeba = TRUE;
-
-  for (x = 0; x < MAX_LEV_FIELDX; x++)
-    for (y = 0; y < MAX_LEV_FIELDY; y++)
-      level->field[x][y] = EL_SAND;
-
-  for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
-    level->name[i] = '\0';
-  for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
-    level->author[i] = '\0';
-
-  strcpy(level->name, NAMELESS_LEVEL_NAME);
-  strcpy(level->author, ANONYMOUS_NAME);
-
-  level->field[0][0] = EL_PLAYER_1;
-  level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
-
-  for (i = 0; i < MAX_NUM_ELEMENTS; i++)
+  if (reset_file_status)
   {
-    int element = i;
-    struct ElementInfo *ei = &element_info[element];
-
-    /* never initialize clipboard elements after the very first time */
-    /* (to be able to use clipboard elements between several levels) */
-    if (IS_CLIPBOARD_ELEMENT(element) && clipboard_elements_initialized)
-      continue;
-
-    if (IS_ENVELOPE(element))
-    {
-      int envelope_nr = element - EL_ENVELOPE_1;
-
-      setConfigToDefaultsFromConfigList(chunk_config_NOTE);
-
-      level->envelope[envelope_nr] = xx_envelope;
-    }
-
-    if (IS_CUSTOM_ELEMENT(element) ||
-       IS_GROUP_ELEMENT(element) ||
-       IS_INTERNAL_ELEMENT(element))
-    {
-      xx_ei = *ei;     /* copy element data into temporary buffer */
-
-      setConfigToDefaultsFromConfigList(chunk_config_CUSX_base);
-
-      *ei = xx_ei;
-    }
-
-    setElementChangePages(ei, 1);
-    setElementChangeInfoToDefaults(ei->change);
-
-    if (IS_CUSTOM_ELEMENT(element) ||
-       IS_GROUP_ELEMENT(element) ||
-       IS_INTERNAL_ELEMENT(element))
-    {
-      setElementDescriptionToDefault(ei);
-
-      ei->modified_settings = FALSE;
-    }
-
-    if (IS_CUSTOM_ELEMENT(element) ||
-       IS_INTERNAL_ELEMENT(element))
-    {
-      /* internal values used in level editor */
-
-      ei->access_type = 0;
-      ei->access_layer = 0;
-      ei->access_protected = 0;
-      ei->walk_to_action = 0;
-      ei->smash_targets = 0;
-      ei->deadliness = 0;
-
-      ei->can_explode_by_fire = FALSE;
-      ei->can_explode_smashed = FALSE;
-      ei->can_explode_impact = FALSE;
-
-      ei->current_change_page = 0;
-    }
-
-    if (IS_GROUP_ELEMENT(element) ||
-       IS_INTERNAL_ELEMENT(element))
-    {
-      struct ElementGroupInfo *group;
-
-      /* initialize memory for list of elements in group */
-      if (ei->group == NULL)
-       ei->group = checked_malloc(sizeof(struct ElementGroupInfo));
-
-      group = ei->group;
-
-      xx_group = *group;       /* copy group data into temporary buffer */
-
-      setConfigToDefaultsFromConfigList(chunk_config_GRPX);
-
-      *group = xx_group;
-    }
+    level->no_valid_file = FALSE;
+    level->no_level_file = FALSE;
   }
 
-  clipboard_elements_initialized = TRUE;
-
-  BorderElement = EL_STEELWALL;
-
-  level->no_valid_file = FALSE;
-
   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)
-  {
-    /* try to determine better author name than 'anonymous' */
-    if (!strEqual(leveldir_current->author, ANONYMOUS_NAME))
-    {
-      strncpy(level->author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
-      level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
-    }
-    else
-    {
-      switch (LEVELCLASS(leveldir_current))
-      {
-       case LEVELCLASS_TUTORIAL:
-         strcpy(level->author, PROGRAM_AUTHOR_STRING);
-         break;
-
-        case LEVELCLASS_CONTRIB:
-         strncpy(level->author, leveldir_current->name, MAX_LEVEL_AUTHOR_LEN);
-         level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
-         break;
-
-        case LEVELCLASS_PRIVATE:
-         strncpy(level->author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
-         level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
-         break;
-
-        default:
-         /* keep default value */
-         break;
-      }
-    }
-  }
 }
 
-#endif
-
 static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
 {
   level_file_info->nr = 0;
@@ -1968,10 +1792,41 @@ static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
   level_file_info->filename = NULL;
 }
 
+int getMappedElement_SB(int, boolean);
+
 static void ActivateLevelTemplate()
 {
   int x, y;
 
+  if (check_special_flags("load_xsb_to_ces"))
+  {
+    /* fill smaller playfields with padding "beyond border wall" elements */
+    if (level.fieldx < level_template.fieldx ||
+       level.fieldy < level_template.fieldy)
+    {
+      short field[level.fieldx][level.fieldy];
+      int new_fieldx = MAX(level.fieldx, level_template.fieldx);
+      int new_fieldy = MAX(level.fieldy, level_template.fieldy);
+      int pos_fieldx = (new_fieldx - level.fieldx) / 2;
+      int pos_fieldy = (new_fieldy - level.fieldy) / 2;
+
+      /* copy old playfield (which is smaller than the visible area) */
+      for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
+       field[x][y] = level.field[x][y];
+
+      /* fill new, larger playfield with "beyond border wall" elements */
+      for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++)
+       level.field[x][y] = getMappedElement_SB('_', TRUE);
+
+      /* copy the old playfield to the middle of the new playfield */
+      for (y = 0; y < level.fieldy; y++) for (x = 0; x < level.fieldx; x++)
+       level.field[pos_fieldx + x][pos_fieldy + y] = field[x][y];
+
+      level.fieldx = new_fieldx;
+      level.fieldy = new_fieldy;
+    }
+  }
+
   /* Currently there is no special action needed to activate the template
      data, because 'element_info' property settings overwrite the original
      level data, while all other variables do not change. */
@@ -2012,13 +1867,14 @@ static void ActivateLevelTemplate()
 
 static char *getLevelFilenameFromBasename(char *basename)
 {
-  static char *filename = NULL;
+  static char *filename[2] = { NULL, NULL };
+  int pos = (strEqual(basename, LEVELTEMPLATE_FILENAME) ? 0 : 1);
 
-  checked_free(filename);
+  checked_free(filename[pos]);
 
-  filename = getPath2(getCurrentLevelDir(), basename);
+  filename[pos] = getPath2(getCurrentLevelDir(), basename);
 
-  return filename;
+  return filename[pos];
 }
 
 static int getFileTypeFromBasename(char *basename)
@@ -2031,14 +1887,8 @@ static int getFileTypeFromBasename(char *basename)
   /* ---------- try to determine file type from filename ---------- */
 
   /* check for typical filename of a Supaplex level package file */
-#if 1
   if (strlen(basename) == 10 && strPrefixLower(basename, "levels.d"))
     return LEVEL_FILE_TYPE_SP;
-#else
-  if (strlen(basename) == 10 && (strncmp(basename, "levels.d", 8) == 0 ||
-                                strncmp(basename, "LEVELS.D", 8) == 0))
-    return LEVEL_FILE_TYPE_SP;
-#endif
 
   /* check for typical filename of a Diamond Caves II level package file */
   if (strSuffixLower(basename, ".dc") ||
@@ -2078,7 +1928,7 @@ static char *getSingleLevelBasenameExt(int nr, char *extension)
   static char basename[MAX_FILENAME_LEN];
 
   if (nr < 0)
-    sprintf(basename, "template.%s", extension);
+    sprintf(basename, "%s", LEVELTEMPLATE_FILENAME);
   else
     sprintf(basename, "%03d.%s", nr, extension);
 
@@ -2090,8 +1940,6 @@ static char *getSingleLevelBasename(int nr)
   return getSingleLevelBasenameExt(nr, LEVELFILE_EXTENSION);
 }
 
-#if 1
-
 static char *getPackedLevelBasename(int type)
 {
   static char basename[MAX_FILENAME_LEN];
@@ -2130,54 +1978,12 @@ static char *getPackedLevelBasename(int type)
   return basename;
 }
 
-#else
-
-static char *getPackedLevelBasename(int type)
-{
-  static char basename[MAX_FILENAME_LEN];
-  char *directory = getCurrentLevelDir();
-  DIR *dir;
-  struct dirent *dir_entry;
-
-  strcpy(basename, UNDEFINED_FILENAME);                /* default: undefined file */
-
-  if ((dir = opendir(directory)) == NULL)
-  {
-    Error(ERR_WARN, "cannot read current level directory '%s'", directory);
-
-    return basename;
-  }
-
-  while ((dir_entry = readdir(dir)) != NULL)   /* loop until last dir entry */
-  {
-    char *entry_basename = dir_entry->d_name;
-    int entry_type = getFileTypeFromBasename(entry_basename);
-
-    if (entry_type != LEVEL_FILE_TYPE_UNKNOWN) /* found valid level package */
-    {
-      if (type == LEVEL_FILE_TYPE_UNKNOWN ||
-         type == entry_type)
-      {
-       strcpy(basename, entry_basename);
-
-       break;
-      }
-    }
-  }
-
-  closedir(dir);
-
-  return basename;
-}
-
-#endif
-
 static char *getSingleLevelFilename(int nr)
 {
   return getLevelFilenameFromBasename(getSingleLevelBasename(nr));
 }
 
-#if 0
+#if ENABLE_UNUSED_CODE
 static char *getPackedLevelFilename(int type)
 {
   return getLevelFilenameFromBasename(getPackedLevelBasename(type));
@@ -2189,7 +1995,7 @@ char *getDefaultLevelFilename(int nr)
   return getSingleLevelFilename(nr);
 }
 
-#if 0
+#if ENABLE_UNUSED_CODE
 static void setLevelFileInfo_SingleLevelFilename(struct LevelFileInfo *lfi,
                                                 int type)
 {
@@ -2254,39 +2060,47 @@ static int getFiletypeFromID(char *filetype_id)
   return filetype;
 }
 
-static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
+char *getLocalLevelTemplateFilename()
 {
-  int nr = lfi->nr;
+  return getDefaultLevelFilename(-1);
+}
 
-  /* special case: level number is negative => check for level template file */
-  if (nr < 0)
-  {
-#if 1
-    /* global variable "leveldir_current" must be modified in the loop below */
-    LevelDirTree *leveldir_current_last = leveldir_current;
+char *getGlobalLevelTemplateFilename()
+{
+  /* global variable "leveldir_current" must be modified in the loop below */
+  LevelDirTree *leveldir_current_last = leveldir_current;
+  char *filename = NULL;
 
-    /* check for template level in path from current to topmost tree node */
+  /* check for template level in path from current to topmost tree node */
 
-    while (leveldir_current != NULL)
-    {
-      setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
-                                          "template.%s", LEVELFILE_EXTENSION);
+  while (leveldir_current != NULL)
+  {
+    filename = getDefaultLevelFilename(-1);
 
-      if (fileExists(lfi->filename))
-       break;
+    if (fileExists(filename))
+      break;
 
-      leveldir_current = leveldir_current->node_parent;
-    }
+    leveldir_current = leveldir_current->node_parent;
+  }
 
-    /* restore global variable "leveldir_current" modified in above loop */
-    leveldir_current = leveldir_current_last;
+  /* restore global variable "leveldir_current" modified in above loop */
+  leveldir_current = leveldir_current_last;
 
-#else
+  return filename;
+}
+
+static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
+{
+  int nr = lfi->nr;
 
+  /* special case: level number is negative => check for level template file */
+  if (nr < 0)
+  {
     setLevelFileInfo_FormatLevelFilename(lfi, LEVEL_FILE_TYPE_RND,
-                                        "template.%s", LEVELFILE_EXTENSION);
+                                        getSingleLevelBasename(-1));
 
-#endif
+    /* replace local level template filename with global template filename */
+    lfi->filename = getGlobalLevelTemplateFilename();
 
     /* no fallback if template file not existing */
     return;
@@ -2451,7 +2265,7 @@ int getMappedElementByVersion(int element, int game_version)
   return element;
 }
 
-static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_VERS(File *file, int chunk_size, struct LevelInfo *level)
 {
   level->file_version = getFileVersion(file);
   level->game_version = getFileVersion(file);
@@ -2459,7 +2273,7 @@ static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_DATE(File *file, int chunk_size, struct LevelInfo *level)
 {
   level->creation_date.year  = getFile16BitBE(file);
   level->creation_date.month = getFile8Bit(file);
@@ -2470,7 +2284,7 @@ static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_HEAD(File *file, int chunk_size, struct LevelInfo *level)
 {
   int initial_player_stepsize;
   int initial_player_gravity;
@@ -2535,7 +2349,7 @@ static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_NAME(File *file, int chunk_size, struct LevelInfo *level)
 {
   int i;
 
@@ -2546,7 +2360,7 @@ static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_AUTH(File *file, int chunk_size, struct LevelInfo *level)
 {
   int i;
 
@@ -2557,7 +2371,7 @@ static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_BODY(File *file, int chunk_size, struct LevelInfo *level)
 {
   int x, y;
   int chunk_size_expected = level->fieldx * level->fieldy;
@@ -2584,7 +2398,7 @@ static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CONT(File *file, int chunk_size, struct LevelInfo *level)
 {
   int i, x, y;
   int header_size = 4;
@@ -2624,25 +2438,18 @@ static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CNT2(File *file, int chunk_size, struct LevelInfo *level)
 {
   int i, x, y;
   int element;
   int num_contents;
-#if 0
-  int content_xsize, content_ysize;
-#endif
   int content_array[MAX_ELEMENT_CONTENTS][3][3];
 
   element = getMappedElement(getFile16BitBE(file));
   num_contents = getFile8Bit(file);
-#if 1
+
   getFile8Bit(file);   /* content x size (unused) */
   getFile8Bit(file);   /* content y size (unused) */
-#else
-  content_xsize = getFile8Bit(file);
-  content_ysize = getFile8Bit(file);
-#endif
 
   ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT2_UNUSED);
 
@@ -2676,7 +2483,7 @@ static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CNT3(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CNT3(File *file, int chunk_size, struct LevelInfo *level)
 {
   int i;
   int element;
@@ -2710,7 +2517,7 @@ static int LoadLevel_CNT3(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CUS1(File *file, int chunk_size, struct LevelInfo *level)
 {
   int num_changed_custom_elements = getFile16BitBE(file);
   int chunk_size_expected = 2 + num_changed_custom_elements * 6;
@@ -2741,7 +2548,7 @@ static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CUS2(File *file, int chunk_size, struct LevelInfo *level)
 {
   int num_changed_custom_elements = getFile16BitBE(file);
   int chunk_size_expected = 2 + num_changed_custom_elements * 4;
@@ -2767,7 +2574,7 @@ static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CUS3(File *file, int chunk_size, struct LevelInfo *level)
 {
   int num_changed_custom_elements = getFile16BitBE(file);
   int chunk_size_expected = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
@@ -2858,7 +2665,7 @@ static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level)
 {
   struct ElementInfo *ei;
   int chunk_size_expected;
@@ -3006,7 +2813,7 @@ static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_GRP1(File *file, int chunk_size, struct LevelInfo *level)
 {
   struct ElementInfo *ei;
   struct ElementGroupInfo *group;
@@ -3050,7 +2857,7 @@ static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level)
   return chunk_size;
 }
 
-static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf,
+static int LoadLevel_MicroChunk(File *file, struct LevelFileConfigInfo *conf,
                                int element, int real_element)
 {
   int micro_chunk_size = 0;
@@ -3190,13 +2997,13 @@ static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf,
   return micro_chunk_size;
 }
 
-static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_INFO(File *file, int chunk_size, struct LevelInfo *level)
 {
   int real_chunk_size = 0;
 
   li = *level;         /* copy level data into temporary buffer */
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1);
 
@@ -3209,13 +3016,13 @@ static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level)
   return real_chunk_size;
 }
 
-static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CONF(File *file, int chunk_size, struct LevelInfo *level)
 {
   int real_chunk_size = 0;
 
   li = *level;         /* copy level data into temporary buffer */
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     int element = getMappedElement(getFile16BitBE(file));
 
@@ -3231,13 +3038,13 @@ static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level)
   return real_chunk_size;
 }
 
-static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_ELEM(File *file, int chunk_size, struct LevelInfo *level)
 {
   int real_chunk_size = 0;
 
   li = *level;         /* copy level data into temporary buffer */
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     int element = getMappedElement(getFile16BitBE(file));
 
@@ -3253,13 +3060,15 @@ static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level)
   return real_chunk_size;
 }
 
-static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_NOTE(File *file, int chunk_size, struct LevelInfo *level)
 {
   int element = getMappedElement(getFile16BitBE(file));
   int envelope_nr = element - EL_ENVELOPE_1;
   int real_chunk_size = 2;
 
-  while (!feof(file))
+  xx_envelope = level->envelope[envelope_nr];  /* copy into temporary buffer */
+
+  while (!checkEndOfFile(file))
   {
     real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE,
                                            -1, element);
@@ -3268,12 +3077,12 @@ static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level)
       break;
   }
 
-  level->envelope[envelope_nr] = xx_envelope;
+  level->envelope[envelope_nr] = xx_envelope;  /* copy from temporary buffer */
 
   return real_chunk_size;
 }
 
-static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_CUSX(File *file, int chunk_size, struct LevelInfo *level)
 {
   int element = getMappedElement(getFile16BitBE(file));
   int real_chunk_size = 2;
@@ -3284,7 +3093,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level)
 
   xx_ei.num_change_pages = -1;
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base,
                                            -1, element);
@@ -3318,7 +3127,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level)
   /* start with reading properties for the first change page */
   xx_current_change_page = 0;
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page];
 
@@ -3340,7 +3149,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level)
   return real_chunk_size;
 }
 
-static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level)
+static int LoadLevel_GRPX(File *file, int chunk_size, struct LevelInfo *level)
 {
   int element = getMappedElement(getFile16BitBE(file));
   int real_chunk_size = 2;
@@ -3350,7 +3159,7 @@ static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level)
   xx_ei = *ei;         /* copy element data into temporary buffer */
   xx_group = *group;   /* copy group data into temporary buffer */
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX,
                                            -1, element);
@@ -3373,21 +3182,31 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
   char cookie[MAX_LINE_LEN];
   char chunk_name[CHUNK_ID_LEN + 1];
   int chunk_size;
-  FILE *file;
+  File *file;
 
-  if (!(file = fopen(filename, MODE_READ)))
+  if (!(file = openFile(filename, MODE_READ)))
   {
     level->no_valid_file = TRUE;
+    level->no_level_file = TRUE;
 
-#if 1
-    if (!level_info_only)
-      Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
-#else
-    if (level != &level_template)
-      Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
-#endif
+    if (level_info_only)
+      return;
 
-    return;
+    Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
+
+    if (!setup.editor.use_template_for_new_levels)
+      return;
+
+    /* if level file not found, try to initialize level data from template */
+    filename = getGlobalLevelTemplateFilename();
+
+    if (!(file = openFile(filename, MODE_READ)))
+      return;
+
+    /* default: for empty levels, use level template for custom elements */
+    level->use_custom_template = TRUE;
+
+    level->no_valid_file = FALSE;
   }
 
   getFileChunkBE(file, chunk_name, NULL);
@@ -3401,14 +3220,16 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
       level->no_valid_file = TRUE;
 
       Error(ERR_WARN, "unknown format of level file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
+
       return;
     }
   }
   else /* check for pre-2.0 file format with cookie string */
   {
     strcpy(cookie, chunk_name);
-    if (fgets(&cookie[4], MAX_LINE_LEN - 4, file) == NULL)
+    if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
       cookie[4] = '\0';
     if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
       cookie[strlen(cookie) - 1] = '\0';
@@ -3418,7 +3239,9 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
       level->no_valid_file = TRUE;
 
       Error(ERR_WARN, "unknown format of level file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
+
       return;
     }
 
@@ -3427,7 +3250,9 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
       level->no_valid_file = TRUE;
 
       Error(ERR_WARN, "unsupported version of level file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
+
       return;
     }
 
@@ -3447,7 +3272,7 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
     {
       char *name;
       int size;
-      int (*loader)(FILE *, int, struct LevelInfo *);
+      int (*loader)(File *, int, struct LevelInfo *);
     }
     chunk_info[] =
     {
@@ -3514,435 +3339,14 @@ static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
     }
   }
 
-  fclose(file);
+  closeFile(file);
 }
 
+
 /* ------------------------------------------------------------------------- */
 /* functions for loading EM level                                            */
 /* ------------------------------------------------------------------------- */
 
-#if 0
-
-static int map_em_element_yam(int element)
-{
-  switch (element)
-  {
-    case 0x00: return EL_EMPTY;
-    case 0x01: return EL_EMERALD;
-    case 0x02: return EL_DIAMOND;
-    case 0x03: return EL_ROCK;
-    case 0x04: return EL_ROBOT;
-    case 0x05: return EL_SPACESHIP_UP;
-    case 0x06: return EL_BOMB;
-    case 0x07: return EL_BUG_UP;
-    case 0x08: return EL_AMOEBA_DROP;
-    case 0x09: return EL_NUT;
-    case 0x0a: return EL_YAMYAM;
-    case 0x0b: return EL_QUICKSAND_FULL;
-    case 0x0c: return EL_SAND;
-    case 0x0d: return EL_WALL_SLIPPERY;
-    case 0x0e: return EL_STEELWALL;
-    case 0x0f: return EL_WALL;
-    case 0x10: return EL_EM_KEY_1;
-    case 0x11: return EL_EM_KEY_2;
-    case 0x12: return EL_EM_KEY_4;
-    case 0x13: return EL_EM_KEY_3;
-    case 0x14: return EL_MAGIC_WALL;
-    case 0x15: return EL_ROBOT_WHEEL;
-    case 0x16: return EL_DYNAMITE;
-
-    case 0x17: return EL_EM_KEY_1;                     /* EMC */
-    case 0x18: return EL_BUG_UP;                       /* EMC */
-    case 0x1a: return EL_DIAMOND;                      /* EMC */
-    case 0x1b: return EL_EMERALD;                      /* EMC */
-    case 0x25: return EL_NUT;                          /* EMC */
-    case 0x80: return EL_EMPTY;                        /* EMC */
-    case 0x85: return EL_EM_KEY_1;                     /* EMC */
-    case 0x86: return EL_EM_KEY_2;                     /* EMC */
-    case 0x87: return EL_EM_KEY_4;                     /* EMC */
-    case 0x88: return EL_EM_KEY_3;                     /* EMC */
-    case 0x94: return EL_QUICKSAND_EMPTY;              /* EMC */
-    case 0x9a: return EL_AMOEBA_WET;                   /* EMC */
-    case 0xaf: return EL_DYNAMITE;                     /* EMC */
-    case 0xbd: return EL_SAND;                         /* EMC */
-
-    default:
-      Error(ERR_WARN, "invalid level element %d", element);
-      return EL_UNKNOWN;
-  }
-}
-
-static int map_em_element_field(int element)
-{
-  if (element >= 0xc8 && element <= 0xe1)
-    return EL_CHAR_A + (element - 0xc8);
-  else if (element >= 0xe2 && element <= 0xeb)
-    return EL_CHAR_0 + (element - 0xe2);
-
-  switch (element)
-  {
-    case 0x00: return EL_ROCK;
-    case 0x01: return EL_ROCK;                         /* EMC */
-    case 0x02: return EL_DIAMOND;
-    case 0x03: return EL_DIAMOND;
-    case 0x04: return EL_ROBOT;
-    case 0x05: return EL_ROBOT;                        /* EMC */
-    case 0x06: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x07: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x08: return EL_SPACESHIP_UP;
-    case 0x09: return EL_SPACESHIP_RIGHT;
-    case 0x0a: return EL_SPACESHIP_DOWN;
-    case 0x0b: return EL_SPACESHIP_LEFT;
-    case 0x0c: return EL_SPACESHIP_UP;
-    case 0x0d: return EL_SPACESHIP_RIGHT;
-    case 0x0e: return EL_SPACESHIP_DOWN;
-    case 0x0f: return EL_SPACESHIP_LEFT;
-
-    case 0x10: return EL_BOMB;
-    case 0x11: return EL_BOMB;                         /* EMC */
-    case 0x12: return EL_EMERALD;
-    case 0x13: return EL_EMERALD;
-    case 0x14: return EL_BUG_UP;
-    case 0x15: return EL_BUG_RIGHT;
-    case 0x16: return EL_BUG_DOWN;
-    case 0x17: return EL_BUG_LEFT;
-    case 0x18: return EL_BUG_UP;
-    case 0x19: return EL_BUG_RIGHT;
-    case 0x1a: return EL_BUG_DOWN;
-    case 0x1b: return EL_BUG_LEFT;
-    case 0x1c: return EL_AMOEBA_DROP;
-    case 0x1d: return EL_AMOEBA_DROP;                  /* EMC */
-    case 0x1e: return EL_AMOEBA_DROP;                  /* EMC */
-    case 0x1f: return EL_AMOEBA_DROP;                  /* EMC */
-
-    case 0x20: return EL_ROCK;
-    case 0x21: return EL_BOMB;                         /* EMC */
-    case 0x22: return EL_DIAMOND;                      /* EMC */
-    case 0x23: return EL_EMERALD;                      /* EMC */
-    case 0x24: return EL_MAGIC_WALL;
-    case 0x25: return EL_NUT;
-    case 0x26: return EL_NUT;                          /* EMC */
-    case 0x27: return EL_NUT;                          /* EMC */
-
-      /* looks like magic wheel, but is _always_ activated */
-    case 0x28: return EL_ROBOT_WHEEL;                  /* EMC */
-
-    case 0x29: return EL_YAMYAM;       /* up */
-    case 0x2a: return EL_YAMYAM;       /* down */
-    case 0x2b: return EL_YAMYAM;       /* left */      /* EMC */
-    case 0x2c: return EL_YAMYAM;       /* right */     /* EMC */
-    case 0x2d: return EL_QUICKSAND_FULL;
-    case 0x2e: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x2f: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0x30: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x31: return EL_SAND;                         /* EMC */
-    case 0x32: return EL_SAND;                         /* EMC */
-    case 0x33: return EL_SAND;                         /* EMC */
-    case 0x34: return EL_QUICKSAND_FULL;               /* EMC */
-    case 0x35: return EL_QUICKSAND_FULL;               /* EMC */
-    case 0x36: return EL_QUICKSAND_FULL;               /* EMC */
-    case 0x37: return EL_SAND;                         /* EMC */
-    case 0x38: return EL_ROCK;                         /* EMC */
-    case 0x39: return EL_EXPANDABLE_WALL_HORIZONTAL;   /* EMC */
-    case 0x3a: return EL_EXPANDABLE_WALL_VERTICAL;     /* EMC */
-    case 0x3b: return EL_DYNAMITE_ACTIVE;      /* 1 */
-    case 0x3c: return EL_DYNAMITE_ACTIVE;      /* 2 */
-    case 0x3d: return EL_DYNAMITE_ACTIVE;      /* 3 */
-    case 0x3e: return EL_DYNAMITE_ACTIVE;      /* 4 */
-    case 0x3f: return EL_ACID_POOL_BOTTOM;
-
-    case 0x40: return EL_EXIT_OPEN;    /* 1 */
-    case 0x41: return EL_EXIT_OPEN;    /* 2 */
-    case 0x42: return EL_EXIT_OPEN;    /* 3 */
-    case 0x43: return EL_BALLOON;                      /* EMC */
-    case 0x44: return EL_UNKNOWN;                      /* EMC ("plant") */
-    case 0x45: return EL_SPRING;                       /* EMC */
-    case 0x46: return EL_SPRING;       /* falling */   /* EMC */
-    case 0x47: return EL_SPRING;       /* left */      /* EMC */
-    case 0x48: return EL_SPRING;       /* right */     /* EMC */
-    case 0x49: return EL_UNKNOWN;                      /* EMC ("ball 1") */
-    case 0x4a: return EL_UNKNOWN;                      /* EMC ("ball 2") */
-    case 0x4b: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x4c: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x4d: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x4e: return EL_INVISIBLE_WALL;               /* EMC (? "android") */
-    case 0x4f: return EL_UNKNOWN;                      /* EMC ("android") */
-
-    case 0x50: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x51: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x52: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x53: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x54: return EL_UNKNOWN;                      /* EMC ("android") */
-    case 0x55: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x56: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x57: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x58: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x59: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5a: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5b: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5c: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5d: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5e: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x5f: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0x60: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x61: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x62: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x63: return EL_SPRING;       /* left */      /* EMC */
-    case 0x64: return EL_SPRING;       /* right */     /* EMC */
-    case 0x65: return EL_ACID;         /* 1 */         /* EMC */
-    case 0x66: return EL_ACID;         /* 2 */         /* EMC */
-    case 0x67: return EL_ACID;         /* 3 */         /* EMC */
-    case 0x68: return EL_ACID;         /* 4 */         /* EMC */
-    case 0x69: return EL_ACID;         /* 5 */         /* EMC */
-    case 0x6a: return EL_ACID;         /* 6 */         /* EMC */
-    case 0x6b: return EL_ACID;         /* 7 */         /* EMC */
-    case 0x6c: return EL_ACID;         /* 8 */         /* EMC */
-    case 0x6d: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x6e: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x6f: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0x70: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x71: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x72: return EL_NUT;          /* left */      /* EMC */
-    case 0x73: return EL_SAND;                         /* EMC (? "nut") */
-    case 0x74: return EL_STEELWALL;
-    case 0x75: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x76: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x77: return EL_BOMB;         /* left */      /* EMC */
-    case 0x78: return EL_BOMB;         /* right */     /* EMC */
-    case 0x79: return EL_ROCK;         /* left */      /* EMC */
-    case 0x7a: return EL_ROCK;         /* right */     /* EMC */
-    case 0x7b: return EL_ACID;                         /* (? EMC "blank") */
-    case 0x7c: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x7d: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x7e: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0x7f: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0x80: return EL_EMPTY;
-    case 0x81: return EL_WALL_SLIPPERY;
-    case 0x82: return EL_SAND;
-    case 0x83: return EL_STEELWALL;
-    case 0x84: return EL_WALL;
-    case 0x85: return EL_EM_KEY_1;
-    case 0x86: return EL_EM_KEY_2;
-    case 0x87: return EL_EM_KEY_4;
-    case 0x88: return EL_EM_KEY_3;
-    case 0x89: return EL_EM_GATE_1;
-    case 0x8a: return EL_EM_GATE_2;
-    case 0x8b: return EL_EM_GATE_4;
-    case 0x8c: return EL_EM_GATE_3;
-    case 0x8d: return EL_INVISIBLE_WALL;               /* EMC (? "dripper") */
-    case 0x8e: return EL_EM_GATE_1_GRAY;
-    case 0x8f: return EL_EM_GATE_2_GRAY;
-
-    case 0x90: return EL_EM_GATE_4_GRAY;
-    case 0x91: return EL_EM_GATE_3_GRAY;
-    case 0x92: return EL_MAGIC_WALL;
-    case 0x93: return EL_ROBOT_WHEEL;
-    case 0x94: return EL_QUICKSAND_EMPTY;              /* (? EMC "sand") */
-    case 0x95: return EL_ACID_POOL_TOPLEFT;
-    case 0x96: return EL_ACID_POOL_TOPRIGHT;
-    case 0x97: return EL_ACID_POOL_BOTTOMLEFT;
-    case 0x98: return EL_ACID_POOL_BOTTOMRIGHT;
-    case 0x99: return EL_ACID;                 /* (? EMC "fake blank") */
-    case 0x9a: return EL_AMOEBA_DEAD;          /* 1 */
-    case 0x9b: return EL_AMOEBA_DEAD;          /* 2 */
-    case 0x9c: return EL_AMOEBA_DEAD;          /* 3 */
-    case 0x9d: return EL_AMOEBA_DEAD;          /* 4 */
-    case 0x9e: return EL_EXIT_CLOSED;
-    case 0x9f: return EL_CHAR_LESS;            /* arrow left */
-
-      /* looks like normal sand, but behaves like wall */
-    case 0xa0: return EL_UNKNOWN;              /* EMC ("fake grass") */
-    case 0xa1: return EL_UNKNOWN;              /* EMC ("lenses") */
-    case 0xa2: return EL_UNKNOWN;              /* EMC ("magnify") */
-    case 0xa3: return EL_UNKNOWN;              /* EMC ("fake blank") */
-    case 0xa4: return EL_UNKNOWN;              /* EMC ("fake grass") */
-    case 0xa5: return EL_UNKNOWN;              /* EMC ("switch") */
-    case 0xa6: return EL_UNKNOWN;              /* EMC ("switch") */
-    case 0xa7: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xa8: return EL_EMC_WALL_1;                   /* EMC ("decor 8") */
-    case 0xa9: return EL_EMC_WALL_2;                   /* EMC ("decor 9") */
-    case 0xaa: return EL_EMC_WALL_3;                   /* EMC ("decor 10") */
-    case 0xab: return EL_EMC_WALL_7;                   /* EMC ("decor 5") */
-    case 0xac: return EL_CHAR_COMMA;                   /* EMC */
-    case 0xad: return EL_CHAR_QUOTEDBL;                /* EMC */
-    case 0xae: return EL_CHAR_MINUS;                   /* EMC */
-    case 0xaf: return EL_DYNAMITE;
-
-    case 0xb0: return EL_EMC_STEELWALL_1;              /* EMC ("steel 3") */
-    case 0xb1: return EL_EMC_WALL_8;                   /* EMC ("decor 6") */
-    case 0xb2: return EL_UNKNOWN;                      /* EMC ("decor 7") */
-    case 0xb3: return EL_STEELWALL;            /* 2 */ /* EMC */
-    case 0xb4: return EL_WALL_SLIPPERY;        /* 2 */ /* EMC */
-    case 0xb5: return EL_EMC_WALL_6;                   /* EMC ("decor 2") */
-    case 0xb6: return EL_EMC_WALL_5;                   /* EMC ("decor 4") */
-    case 0xb7: return EL_EMC_WALL_4;                   /* EMC ("decor 3") */
-    case 0xb8: return EL_BALLOON_SWITCH_ANY;           /* EMC */
-    case 0xb9: return EL_BALLOON_SWITCH_RIGHT;         /* EMC */
-    case 0xba: return EL_BALLOON_SWITCH_DOWN;          /* EMC */
-    case 0xbb: return EL_BALLOON_SWITCH_LEFT;          /* EMC */
-    case 0xbc: return EL_BALLOON_SWITCH_UP;            /* EMC */
-    case 0xbd: return EL_SAND;                         /* EMC ("dirt") */
-    case 0xbe: return EL_UNKNOWN;                      /* EMC ("plant") */
-    case 0xbf: return EL_UNKNOWN;                      /* EMC ("key 5") */
-
-    case 0xc0: return EL_UNKNOWN;                      /* EMC ("key 6") */
-    case 0xc1: return EL_UNKNOWN;                      /* EMC ("key 7") */
-    case 0xc2: return EL_UNKNOWN;                      /* EMC ("key 8") */
-    case 0xc3: return EL_UNKNOWN;                      /* EMC ("door 5") */
-    case 0xc4: return EL_UNKNOWN;                      /* EMC ("door 6") */
-    case 0xc5: return EL_UNKNOWN;                      /* EMC ("door 7") */
-    case 0xc6: return EL_UNKNOWN;                      /* EMC ("door 8") */
-    case 0xc7: return EL_UNKNOWN;                      /* EMC ("bumper") */
-
-      /* characters: see above */
-
-    case 0xec: return EL_CHAR_PERIOD;
-    case 0xed: return EL_CHAR_EXCLAM;
-    case 0xee: return EL_CHAR_COLON;
-    case 0xef: return EL_CHAR_QUESTION;
-
-    case 0xf0: return EL_CHAR_GREATER;                 /* arrow right */
-    case 0xf1: return EL_CHAR_COPYRIGHT;               /* EMC: "decor 1" */
-    case 0xf2: return EL_UNKNOWN;              /* EMC ("fake door 5") */
-    case 0xf3: return EL_UNKNOWN;              /* EMC ("fake door 6") */
-    case 0xf4: return EL_UNKNOWN;              /* EMC ("fake door 7") */
-    case 0xf5: return EL_UNKNOWN;              /* EMC ("fake door 8") */
-    case 0xf6: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xf7: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0xf8: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xf9: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xfa: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xfb: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xfc: return EL_EMPTY_SPACE;                  /* EMC */
-    case 0xfd: return EL_EMPTY_SPACE;                  /* EMC */
-
-    case 0xfe: return EL_PLAYER_1;                     /* EMC: "blank" */
-    case 0xff: return EL_PLAYER_2;                     /* EMC: "blank" */
-
-    default:
-      /* should never happen (all 8-bit value cases should be handled) */
-      Error(ERR_WARN, "invalid level element %d", element);
-      return EL_UNKNOWN;
-  }
-}
-
-#define EM_LEVEL_SIZE                  2106
-#define EM_LEVEL_XSIZE                 64
-#define EM_LEVEL_YSIZE                 32
-
-static void OLD_LoadLevelFromFileInfo_EM(struct LevelInfo *level,
-                                        struct LevelFileInfo *level_file_info)
-{
-  char *filename = level_file_info->filename;
-  FILE *file;
-  unsigned char leveldata[EM_LEVEL_SIZE];
-  unsigned char *header = &leveldata[EM_LEVEL_XSIZE * EM_LEVEL_YSIZE];
-  int nr = level_file_info->nr;
-  int i, x, y;
-
-  if (!(file = fopen(filename, MODE_READ)))
-  {
-    level->no_valid_file = TRUE;
-
-    Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
-
-    return;
-  }
-
-  for (i = 0; i < EM_LEVEL_SIZE; i++)
-    leveldata[i] = fgetc(file);
-
-  fclose(file);
-
-  /* check if level data is crypted by testing against known starting bytes
-     of the few existing crypted level files (from Emerald Mine 1 + 2) */
-
-  if ((leveldata[0] == 0xf1 ||
-       leveldata[0] == 0xf5) && leveldata[2] == 0xe7 && leveldata[3] == 0xee)
-  {
-    unsigned char code0 = 0x65;
-    unsigned char code1 = 0x11;
-
-    if (leveldata[0] == 0xf5)  /* error in crypted Emerald Mine 2 levels */
-      leveldata[0] = 0xf1;
-
-    /* decode crypted level data */
-
-    for (i = 0; i < EM_LEVEL_SIZE; i++)
-    {
-      leveldata[i] ^= code0;
-      leveldata[i] -= code1;
-
-      code0 = (code0 + 7) & 0xff;
-    }
-  }
-
-  level->fieldx        = EM_LEVEL_XSIZE;
-  level->fieldy        = EM_LEVEL_YSIZE;
-
-  level->time          = header[46] * 10;
-  level->gems_needed   = header[47];
-
-  /* The original Emerald Mine levels have their level number stored
-     at the second byte of the level file...
-     Do not trust this information at other level files, e.g. EMC,
-     but correct it anyway (normally the first row is completely
-     steel wall, so the correction does not hurt anyway). */
-
-  if (leveldata[1] == nr)
-    leveldata[1] = leveldata[2];       /* correct level number field */
-
-  sprintf(level->name, "Level %d", nr);                /* set level name */
-
-  level->score[SC_EMERALD]     = header[36];
-  level->score[SC_DIAMOND]     = header[37];
-  level->score[SC_ROBOT]       = header[38];
-  level->score[SC_SPACESHIP]   = header[39];
-  level->score[SC_BUG]         = header[40];
-  level->score[SC_YAMYAM]      = header[41];
-  level->score[SC_NUT]         = header[42];
-  level->score[SC_DYNAMITE]    = header[43];
-  level->score[SC_TIME_BONUS]  = header[44];
-
-  level->num_yamyam_contents = 4;
-
-  for (i = 0; i < level->num_yamyam_contents; i++)
-    for (y = 0; y < 3; y++)
-      for (x = 0; x < 3; x++)
-       level->yamyam_content[i].e[x][y] =
-         map_em_element_yam(header[i * 9 + y * 3 + x]);
-
-  level->amoeba_speed          = (header[52] * 256 + header[53]) % 256;
-  level->time_magic_wall       = (header[54] * 256 + header[55]) * 16 / 100;
-  level->time_wheel            = (header[56] * 256 + header[57]) * 16 / 100;
-  level->amoeba_content                = EL_DIAMOND;
-
-  for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
-  {
-    int new_element = map_em_element_field(leveldata[y * EM_LEVEL_XSIZE + x]);
-
-    if (new_element == EL_AMOEBA_DEAD && level->amoeba_speed)
-      new_element = EL_AMOEBA_WET;
-
-    level->field[x][y] = new_element;
-  }
-
-  x = (header[48] * 256 + header[49]) % EM_LEVEL_XSIZE;
-  y = (header[48] * 256 + header[49]) / EM_LEVEL_XSIZE;
-  level->field[x][y] = EL_PLAYER_1;
-
-  x = (header[50] * 256 + header[51]) % EM_LEVEL_XSIZE;
-  y = (header[50] * 256 + header[51]) / EM_LEVEL_XSIZE;
-  level->field[x][y] = EL_PLAYER_2;
-}
-
-#endif
-
 void CopyNativeLevel_RND_to_EM(struct LevelInfo *level)
 {
   static int ball_xy[8][2] =
@@ -4172,464 +3576,113 @@ void CopyNativeLevel_EM_to_RND(struct LevelInfo *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
-#define SP_LEVEL_YSIZE                 24
-#define SP_LEVEL_NAME_LEN              23
-
-static void LoadLevelFromFileStream_SP(FILE *file, struct LevelInfo *level,
-                                      int nr)
+void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
 {
-  int initial_player_gravity;
-  int num_special_ports;
+  struct LevelInfo_SP *level_sp = level->native_sp_level;
+  LevelInfoType *header = &level_sp->header;
   int i, x, y;
 
-  /* for details of the Supaplex level format, see Herman Perk's Supaplex
-     documentation file "SPFIX63.DOC" from his Supaplex "SpeedFix" package */
-
-  /* read level body (width * height == 60 * 24 tiles == 1440 bytes) */
-  for (y = 0; y < SP_LEVEL_YSIZE; y++)
-  {
-    for (x = 0; x < SP_LEVEL_XSIZE; x++)
-    {
-      int element_old = fgetc(file);
-      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, "in level %d, at position %d, %d:", nr, x, y);
-       Error(ERR_WARN, "invalid level element %d", element_old);
-
-       element_new = EL_UNKNOWN;
-      }
-
-      level->field[x][y] = element_new;
-    }
-  }
-
-  ReadUnusedBytesFromFile(file, 4);    /* (not used by Supaplex engine) */
-
-  /* initial gravity: 1 == "on", anything else (0) == "off" */
-  initial_player_gravity = (fgetc(file) == 1 ? TRUE : FALSE);
+  level_sp->width  = level->fieldx;
+  level_sp->height = level->fieldy;
 
-  for (i = 0; i < MAX_PLAYERS; i++)
-    level->initial_player_gravity[i] = initial_player_gravity;
+  for (x = 0; x < level->fieldx; x++)
+    for (y = 0; y < level->fieldy; y++)
+      level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
 
-  ReadUnusedBytesFromFile(file, 1);    /* (not used by Supaplex engine) */
+  header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
 
-  /* level title in uppercase letters, padded with dashes ("-") (23 bytes) */
   for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
-    level->name[i] = fgetc(file);
-  level->name[SP_LEVEL_NAME_LEN] = '\0';
-
-  /* initial "freeze zonks": 2 == "on", anything else (0, 1) == "off" */
-  ReadUnusedBytesFromFile(file, 1);    /* (not used by R'n'D engine) */
+    header->LevelTitle[i] = level->name[i];
+  /* !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!! */
 
-  /* number of infotrons needed; 0 means that Supaplex will count the total
-     amount of infotrons in the level and use the low byte of that number
-     (a multiple of 256 infotrons will result in "0 infotrons needed"!) */
-  level->gems_needed = fgetc(file);
+  header->InfotronsNeeded = level->gems_needed;
 
-  /* number of special ("gravity") port entries below (maximum 10 allowed) */
-  num_special_ports = fgetc(file);
+  header->SpecialPortCount = 0;
 
-  /* database of properties of up to 10 special ports (6 bytes per port) */
-  for (i = 0; i < 10; i++)
+  for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
   {
-    int port_location, port_x, port_y, port_element;
-    int gravity;
-
-    /* high and low byte of the location of a special port; if (x, y) are the
-       coordinates of a port in the field and (0, 0) is the top-left corner,
-       the 16 bit value here calculates as 2 * (x + (y * 60)) (this is twice
-       of what may be expected: Supaplex works with a game field in memory
-       which is 2 bytes per tile) */
-    port_location = getFile16BitBE(file);
+    boolean gravity_port_found = FALSE;
+    boolean gravity_port_valid = FALSE;
+    int gravity_port_flag;
+    int gravity_port_base_element;
+    int element = level->field[x][y];
 
-    /* change gravity: 1 == "turn on", anything else (0) == "turn off" */
-    gravity = fgetc(file);
+    if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
+       element <= EL_SP_GRAVITY_ON_PORT_UP)
+    {
+      gravity_port_found = TRUE;
+      gravity_port_valid = TRUE;
+      gravity_port_flag = 1;
+      gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
+    }
+    else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
+            element <= EL_SP_GRAVITY_OFF_PORT_UP)
+    {
+      gravity_port_found = TRUE;
+      gravity_port_valid = TRUE;
+      gravity_port_flag = 0;
+      gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
+    }
+    else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
+            element <= EL_SP_GRAVITY_PORT_UP)
+    {
+      /* change R'n'D style gravity inverting special port to normal port
+        (there are no gravity inverting ports in native Supaplex engine) */
 
-    /* "freeze zonks": 2 == "turn on", anything else (0, 1) == "turn off" */
-    ReadUnusedBytesFromFile(file, 1);  /* (not used by R'n'D engine) */
+      gravity_port_found = TRUE;
+      gravity_port_valid = FALSE;
+      gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
+    }
 
-    /* "freeze enemies": 1 == "turn on", anything else (0) == "turn off" */
-    ReadUnusedBytesFromFile(file, 1);  /* (not used by R'n'D engine) */
+    if (gravity_port_found)
+    {
+      if (gravity_port_valid &&
+         header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
+      {
+       SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
 
-    ReadUnusedBytesFromFile(file, 1);  /* (not used by Supaplex engine) */
+       port->PortLocation = (y * level->fieldx + x) * 2;
+       port->Gravity = gravity_port_flag;
 
-    if (i >= num_special_ports)
-      continue;
+       element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
 
-    port_x = (port_location / 2) % SP_LEVEL_XSIZE;
-    port_y = (port_location / 2) / SP_LEVEL_XSIZE;
+       header->SpecialPortCount++;
+      }
+      else
+      {
+       /* change special gravity port to normal port */
 
-    if (port_x < 0 || port_x >= SP_LEVEL_XSIZE ||
-       port_y < 0 || port_y >= SP_LEVEL_YSIZE)
-    {
-      Error(ERR_WARN, "special port position (%d, %d) out of bounds",
-           port_x, port_y);
+       element += EL_SP_PORT_RIGHT - gravity_port_base_element;
+      }
 
-      continue;
+      level_sp->playfield[x][y] = element - EL_SP_START;
     }
+  }
+}
 
-    port_element = level->field[port_x][port_y];
+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;
 
-    if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
-       port_element > EL_SP_GRAVITY_PORT_UP)
+  level->fieldx = level_sp->width;
+  level->fieldy = level_sp->height;
+
+  for (x = 0; x < level->fieldx; x++)
+  {
+    for (y = 0; y < level->fieldy; y++)
     {
-      Error(ERR_WARN, "no special port at position (%d, %d)", port_x, port_y);
+      int element_old = level_sp->playfield[x][y];
+      int element_new = getMappedElement(map_element_SP_to_RND(element_old));
 
-      continue;
+      if (element_new == EL_UNKNOWN)
+       Error(ERR_WARN, "invalid element %d at position %d, %d",
+             element_old, x, y);
+
+      level->field[x][y] = element_new;
     }
-
-    /* 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;
-  }
-
-  ReadUnusedBytesFromFile(file, 4);    /* (not used by Supaplex engine) */
-
-  /* change special gravity ports without database entries to normal ports */
-  for (y = 0; y < SP_LEVEL_YSIZE; y++)
-    for (x = 0; x < SP_LEVEL_XSIZE; x++)
-      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;
-
-  /* auto-determine number of infotrons if it was stored as "0" -- see above */
-  if (level->gems_needed == 0)
-  {
-    for (y = 0; y < SP_LEVEL_YSIZE; y++)
-      for (x = 0; x < SP_LEVEL_XSIZE; x++)
-       if (level->field[x][y] == EL_SP_INFOTRON)
-         level->gems_needed++;
-
-    level->gems_needed &= 0xff;                /* only use low byte -- see above */
-  }
-
-  level->fieldx = SP_LEVEL_XSIZE;
-  level->fieldy = SP_LEVEL_YSIZE;
-
-  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 (y = 0; y < 3; y++)
-      for (x = 0; x < 3; x++)
-       level->yamyam_content[i].e[x][y] = EL_EMPTY;
-}
-
-static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
-                                    struct LevelFileInfo *level_file_info,
-                                    boolean level_info_only)
-{
-  char *filename = level_file_info->filename;
-  FILE *file;
-  int nr = level_file_info->nr - leveldir_current->first_level;
-  int i, l, x, y;
-  char name_first, name_last;
-  struct LevelInfo multipart_level;
-  int multipart_xpos, multipart_ypos;
-  boolean is_multipart_level;
-  boolean is_first_part;
-  boolean reading_multipart_level = FALSE;
-  boolean use_empty_level = FALSE;
-
-  if (!(file = fopen(filename, MODE_READ)))
-  {
-    level->no_valid_file = TRUE;
-
-    if (!level_info_only)
-      Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
-
-    return;
-  }
-
-  /* position file stream to the requested level inside the level package */
-  if (level_file_info->packed &&
-      fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
-  {
-    level->no_valid_file = TRUE;
-
-    Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename);
-
-    return;
-  }
-
-  /* there exist Supaplex level package files with multi-part levels which
-     can be detected as follows: instead of leading and trailing dashes ('-')
-     to pad the level name, they have leading and trailing numbers which are
-     the x and y coordinations of the current part of the multi-part level;
-     if there are '?' characters instead of numbers on the left or right side
-     of the level name, the multi-part level consists of only horizontal or
-     vertical parts */
-
-  for (l = nr; l < NUM_SUPAPLEX_LEVELS_PER_PACKAGE; l++)
-  {
-    LoadLevelFromFileStream_SP(file, level, l);
-
-    /* check if this level is a part of a bigger multi-part level */
-
-    name_first = level->name[0];
-    name_last  = level->name[SP_LEVEL_NAME_LEN - 1];
-
-    is_multipart_level =
-      ((name_first == '?' || (name_first >= '0' && name_first <= '9')) &&
-       (name_last  == '?' || (name_last  >= '0' && name_last  <= '9')));
-
-    is_first_part =
-      ((name_first == '?' || name_first == '1') &&
-       (name_last  == '?' || name_last  == '1'));
-
-    /* correct leading multipart level meta information in level name */
-    for (i = 0; i < SP_LEVEL_NAME_LEN && level->name[i] == name_first; i++)
-      level->name[i] = '-';
-
-    /* correct trailing multipart level meta information in level name */
-    for (i = SP_LEVEL_NAME_LEN - 1; i >= 0 && level->name[i] == name_last; i--)
-      level->name[i] = '-';
-
-    /* ---------- check for normal single level ---------- */
-
-    if (!reading_multipart_level && !is_multipart_level)
-    {
-      /* the current level is simply a normal single-part level, and we are
-        not reading a multi-part level yet, so return the level as it is */
-
-      break;
-    }
-
-    /* ---------- check for empty level (unused multi-part) ---------- */
-
-    if (!reading_multipart_level && is_multipart_level && !is_first_part)
-    {
-      /* this is a part of a multi-part level, but not the first part
-        (and we are not already reading parts of a multi-part level);
-        in this case, use an empty level instead of the single part */
-
-      use_empty_level = TRUE;
-
-      break;
-    }
-
-    /* ---------- check for finished multi-part level ---------- */
-
-    if (reading_multipart_level &&
-       (!is_multipart_level ||
-        !strEqual(level->name, multipart_level.name)))
-    {
-      /* we are already reading parts of a multi-part level, but this level is
-        either not a multi-part level, or a part of a different multi-part
-        level; in both cases, the multi-part level seems to be complete */
-
-      break;
-    }
-
-    /* ---------- here we have one part of a multi-part level ---------- */
-
-    reading_multipart_level = TRUE;
-
-    if (is_first_part) /* start with first part of new multi-part level */
-    {
-      /* copy level info structure from first part */
-      multipart_level = *level;
-
-      /* clear playfield of new multi-part level */
-      for (y = 0; y < MAX_LEV_FIELDY; y++)
-       for (x = 0; x < MAX_LEV_FIELDX; x++)
-         multipart_level.field[x][y] = EL_EMPTY;
-    }
-
-    if (name_first == '?')
-      name_first = '1';
-    if (name_last == '?')
-      name_last = '1';
-
-    multipart_xpos = (int)(name_first - '0');
-    multipart_ypos = (int)(name_last  - '0');
-
-#if 0
-    printf("----------> part (%d/%d) of multi-part level '%s'\n",
-          multipart_xpos, multipart_ypos, multipart_level.name);
-#endif
-
-    if (multipart_xpos * SP_LEVEL_XSIZE > MAX_LEV_FIELDX ||
-       multipart_ypos * SP_LEVEL_YSIZE > MAX_LEV_FIELDY)
-    {
-      Error(ERR_WARN, "multi-part level is too big -- ignoring part of it");
-
-      break;
-    }
-
-    multipart_level.fieldx = MAX(multipart_level.fieldx,
-                                multipart_xpos * SP_LEVEL_XSIZE);
-    multipart_level.fieldy = MAX(multipart_level.fieldy,
-                                multipart_ypos * SP_LEVEL_YSIZE);
-
-    /* copy level part at the right position of multi-part level */
-    for (y = 0; y < SP_LEVEL_YSIZE; y++)
-    {
-      for (x = 0; x < SP_LEVEL_XSIZE; x++)
-      {
-       int start_x = (multipart_xpos - 1) * SP_LEVEL_XSIZE;
-       int start_y = (multipart_ypos - 1) * SP_LEVEL_YSIZE;
-
-       multipart_level.field[start_x + x][start_y + y] = level->field[x][y];
-      }
-    }
-  }
-
-  fclose(file);
-
-  if (use_empty_level)
-  {
-    setLevelInfoToDefaults(level);
-
-    level->fieldx = SP_LEVEL_XSIZE;
-    level->fieldy = SP_LEVEL_YSIZE;
-
-    for (y = 0; y < SP_LEVEL_YSIZE; y++)
-      for (x = 0; x < SP_LEVEL_XSIZE; x++)
-       level->field[x][y] = EL_EMPTY;
-
-    strcpy(level->name, "-------- EMPTY --------");
-
-    Error(ERR_WARN, "single part of multi-part level -- using empty level");
-  }
-
-  if (reading_multipart_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++)
-      level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
-
-  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;
-
-  header->SpecialPortCount = 0;
-
-  for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
-  {
-    boolean gravity_port_found = FALSE;
-    boolean gravity_port_valid = FALSE;
-    int gravity_port_flag;
-    int gravity_port_base_element;
-    int element = level->field[x][y];
-
-    if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
-       element <= EL_SP_GRAVITY_ON_PORT_UP)
-    {
-      gravity_port_found = TRUE;
-      gravity_port_valid = TRUE;
-      gravity_port_flag = 1;
-      gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
-    }
-    else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
-            element <= EL_SP_GRAVITY_OFF_PORT_UP)
-    {
-      gravity_port_found = TRUE;
-      gravity_port_valid = TRUE;
-      gravity_port_flag = 0;
-      gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
-    }
-    else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
-            element <= EL_SP_GRAVITY_PORT_UP)
-    {
-      /* change R'n'D style gravity inverting special port to normal port
-        (there are no gravity inverting ports in native Supaplex engine) */
-
-      gravity_port_found = TRUE;
-      gravity_port_valid = FALSE;
-      gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
-    }
-
-    if (gravity_port_found)
-    {
-      if (gravity_port_valid &&
-         header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
-      {
-       SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
-
-       port->PortLocation = (y * level->fieldx + x) * 2;
-       port->Gravity = gravity_port_flag;
-
-       element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
-
-       header->SpecialPortCount++;
-      }
-      else
-      {
-       /* change special gravity port to normal port */
-
-       element += EL_SP_PORT_RIGHT - gravity_port_base_element;
-      }
-
-      level_sp->playfield[x][y] = element - EL_SP_START;
-    }
-  }
-}
-
-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 = getMappedElement(map_element_SP_to_RND(element_old));
-
-      if (element_new == EL_UNKNOWN)
-       Error(ERR_WARN, "invalid element %d at position %d, %d",
-             element_old, x, y);
-
-      level->field[x][y] = element_new;
-    }
-  }
+  }
 
   for (i = 0; i < MAX_PLAYERS; i++)
     level->initial_player_gravity[i] =
@@ -4722,10 +3775,20 @@ static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
   level_sp->header.DemoRandomSeed = tape.random_seed;
 
   demo->length = 0;
+
   for (i = 0; i < tape.length; i++)
   {
     int demo_action = map_key_RND_to_SP(tape.pos[i].action[0]);
     int demo_repeat = tape.pos[i].delay;
+    int demo_entries = (demo_repeat + 15) / 16;
+
+    if (demo->length + demo_entries >= SP_MAX_TAPE_LEN)
+    {
+      Error(ERR_WARN, "tape truncated: size exceeds maximum SP demo size %d",
+           SP_MAX_TAPE_LEN);
+
+      break;
+    }
 
     for (j = 0; j < demo_repeat / 16; j++)
       demo->data[demo->length++] = 0xf0 | demo_action;
@@ -4734,8 +3797,6 @@ static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
       demo->data[demo->length++] = ((demo_repeat % 16 - 1) << 4) | demo_action;
   }
 
-  demo->data[demo->length++] = 0xff;
-
   demo->is_available = TRUE;
 }
 
@@ -4755,21 +3816,36 @@ static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
     return;
 
   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;
 
   TapeSetDateFromEpochSeconds(getFileTimestampEpochSeconds(filename));
 
-  for (i = 0; i < demo->length - 1; i++)
+  tape.counter = 0;
+  tape.pos[tape.counter].delay = 0;
+
+  for (i = 0; i < demo->length; i++)
   {
     int demo_action = demo->data[i] & 0x0f;
     int demo_repeat = (demo->data[i] & 0xf0) >> 4;
+    int tape_action = map_key_SP_to_RND(demo_action);
+    int tape_repeat = demo_repeat + 1;
+    byte action[MAX_PLAYERS] = { tape_action, 0, 0, 0 };
+    boolean success = 0;
+    int j;
+
+    for (j = 0; j < tape_repeat; j++)
+      success = TapeAddAction(action);
+
+    if (!success)
+    {
+      Error(ERR_WARN, "SP demo truncated: size exceeds maximum tape size %d",
+           MAX_TAPE_LEN);
 
-    tape.pos[i].action[0] = map_key_SP_to_RND(demo_action);
-    tape.pos[i].delay = demo_repeat + 1;
+      break;
+    }
   }
 
-  tape.length_seconds = GetTapeLength();
+  TapeHaltRecording();
 }
 
 
@@ -6246,9 +5322,7 @@ int getMappedElement_DC(int element)
   return getMappedElement(element);
 }
 
-#if 1
-
-static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level,
+static void LoadLevelFromFileStream_DC(File *file, struct LevelInfo *level,
                                       int nr)
 {
   byte header[DC_LEVEL_HEADER_SIZE];
@@ -6346,11 +5420,7 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level,
     for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
     {
       unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
-#if 1
       int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
-#else
-      int element_dc = word;
-#endif
 
       if (i < MAX_ELEMENT_CONTENTS)
        level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
@@ -6365,11 +5435,7 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level,
   for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
   {
     unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
-#if 1
     int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
-#else
-    int element_dc = word;
-#endif
 
     if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
       level->field[x][y] = getMappedElement_DC(element_dc);
@@ -6411,29 +5477,6 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level,
   /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
      can slip down from flat walls, like normal walls and steel walls */
   level->em_slippery_gems = TRUE;
-
-#if 0
-  /* Diamond Caves II levels are always surrounded by indestructible wall, but
-     not necessarily in a rectangular way -- fill with invisible steel wall */
-
-  /* !!! not always true !!! keep level and set BorderElement instead !!! */
-
-  for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
-  {
-#if 1
-    if ((x == 0 || x == level->fieldx - 1 ||
-        y == 0 || y == level->fieldy - 1) &&
-       level->field[x][y] == EL_EMPTY)
-      level->field[x][y] = EL_INVISIBLE_STEELWALL;
-#else
-    if ((x == 0 || x == level->fieldx - 1 ||
-        y == 0 || y == level->fieldy - 1) &&
-       level->field[x][y] == EL_EMPTY)
-      FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL,
-                    level->field, level->fieldx, level->fieldy);
-#endif
-  }
-#endif
 }
 
 static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
@@ -6441,12 +5484,12 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
                                     boolean level_info_only)
 {
   char *filename = level_file_info->filename;
-  FILE *file;
+  File *file;
   int num_magic_bytes = 8;
   char magic_bytes[num_magic_bytes + 1];
   int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
 
-  if (!(file = fopen(filename, MODE_READ)))
+  if (!(file = openFile(filename, MODE_READ)))
   {
     level->no_valid_file = TRUE;
 
@@ -6461,7 +5504,7 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
   if (level_file_info->packed)
   {
     /* read "magic bytes" from start of file */
-    if (fgets(magic_bytes, num_magic_bytes + 1, file) == NULL)
+    if (getStringFromFile(file, magic_bytes, num_magic_bytes + 1) == NULL)
       magic_bytes[0] = '\0';
 
     /* check "magic bytes" for correct file format */
@@ -6492,7 +5535,7 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
       while (num_levels_to_skip >= 0)
       {
        /* advance file stream to next level inside the level package */
-       if (fseek(file, skip_bytes, SEEK_CUR) != 0)
+       if (seekFile(file, skip_bytes, SEEK_CUR) != 0)
        {
          level->no_valid_file = TRUE;
 
@@ -6524,231 +5567,9 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
 
   LoadLevelFromFileStream_DC(file, level, level_file_info->nr);
 
-  fclose(file);
+  closeFile(file);
 }
 
-#else
-
-static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
-                                    struct LevelFileInfo *level_file_info)
-{
-  char *filename = level_file_info->filename;
-  FILE *file;
-#if 0
-  int nr = level_file_info->nr - leveldir_current->first_level;
-#endif
-  byte header[DC_LEVEL_HEADER_SIZE];
-  int envelope_size;
-  int envelope_header_pos = 62;
-  int envelope_content_pos = 94;
-  int level_name_pos = 251;
-  int level_author_pos = 292;
-  int envelope_header_len;
-  int envelope_content_len;
-  int level_name_len;
-  int level_author_len;
-  int fieldx, fieldy;
-  int num_yamyam_contents;
-  int i, x, y;
-
-  if (!(file = fopen(filename, MODE_READ)))
-  {
-    level->no_valid_file = TRUE;
-
-    if (!level_info_only)
-      Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename);
-
-    return;
-  }
-
-#if 0
-  /* position file stream to the requested level inside the level package */
-  if (fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0)
-  {
-    level->no_valid_file = TRUE;
-
-    Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename);
-
-    return;
-  }
-#endif
-
-  getDecodedWord_DC(0, TRUE);          /* initialize DC2 decoding engine */
-
-  for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
-  {
-    unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
-
-    header[i * 2 + 0] = header_word >> 8;
-    header[i * 2 + 1] = header_word & 0xff;
-  }
-
-  /* read some values from level header to check level decoding integrity */
-  fieldx = header[6] | (header[7] << 8);
-  fieldy = header[8] | (header[9] << 8);
-  num_yamyam_contents = header[60] | (header[61] << 8);
-
-  /* do some simple sanity checks to ensure that level was correctly decoded */
-  if (fieldx < 1 || fieldx > 256 ||
-      fieldy < 1 || fieldy > 256 ||
-      num_yamyam_contents < 1 || num_yamyam_contents > 8)
-  {
-    level->no_valid_file = TRUE;
-
-    Error(ERR_WARN, "cannot read level from file '%s' -- using empty level",
-         filename);
-
-    return;
-  }
-
-  /* maximum envelope header size is 31 bytes */
-  envelope_header_len  = header[envelope_header_pos];
-  /* maximum envelope content size is 110 (156?) bytes */
-  envelope_content_len = header[envelope_content_pos];
-
-  /* maximum level title size is 40 bytes */
-  level_name_len       = MIN(header[level_name_pos],   MAX_LEVEL_NAME_LEN);
-  /* maximum level author size is 30 (51?) bytes */
-  level_author_len     = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
-
-  envelope_size = 0;
-
-  for (i = 0; i < envelope_header_len; i++)
-    if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
-      level->envelope[0].text[envelope_size++] =
-       header[envelope_header_pos + 1 + i];
-
-  if (envelope_header_len > 0 && envelope_content_len > 0)
-  {
-    if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
-      level->envelope[0].text[envelope_size++] = '\n';
-    if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
-      level->envelope[0].text[envelope_size++] = '\n';
-  }
-
-  for (i = 0; i < envelope_content_len; i++)
-    if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
-      level->envelope[0].text[envelope_size++] =
-       header[envelope_content_pos + 1 + i];
-
-  level->envelope[0].text[envelope_size] = '\0';
-
-  level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
-  level->envelope[0].ysize = 10;
-  level->envelope[0].autowrap = TRUE;
-  level->envelope[0].centered = TRUE;
-
-  for (i = 0; i < level_name_len; i++)
-    level->name[i] = header[level_name_pos + 1 + i];
-  level->name[level_name_len] = '\0';
-
-  for (i = 0; i < level_author_len; i++)
-    level->author[i] = header[level_author_pos + 1 + i];
-  level->author[level_author_len] = '\0';
-
-  num_yamyam_contents = header[60] | (header[61] << 8);
-  level->num_yamyam_contents =
-    MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
-
-  for (i = 0; i < num_yamyam_contents; i++)
-  {
-    for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
-    {
-      unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
-#if 1
-      int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
-#else
-      int element_dc = word;
-#endif
-
-      if (i < MAX_ELEMENT_CONTENTS)
-       level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
-    }
-  }
-
-  fieldx = header[6] | (header[7] << 8);
-  fieldy = header[8] | (header[9] << 8);
-  level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
-  level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
-
-  for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
-  {
-    unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
-#if 1
-    int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
-#else
-    int element_dc = word;
-#endif
-
-    if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
-      level->field[x][y] = getMappedElement_DC(element_dc);
-  }
-
-  x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
-  y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
-  level->field[x][y] = EL_PLAYER_1;
-
-  x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
-  y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
-  level->field[x][y] = EL_PLAYER_2;
-
-  level->gems_needed           = header[18] | (header[19] << 8);
-
-  level->score[SC_EMERALD]     = header[20] | (header[21] << 8);
-  level->score[SC_DIAMOND]     = header[22] | (header[23] << 8);
-  level->score[SC_PEARL]       = header[24] | (header[25] << 8);
-  level->score[SC_CRYSTAL]     = header[26] | (header[27] << 8);
-  level->score[SC_NUT]         = header[28] | (header[29] << 8);
-  level->score[SC_ROBOT]       = header[30] | (header[31] << 8);
-  level->score[SC_SPACESHIP]   = header[32] | (header[33] << 8);
-  level->score[SC_BUG]         = header[34] | (header[35] << 8);
-  level->score[SC_YAMYAM]      = header[36] | (header[37] << 8);
-  level->score[SC_DYNAMITE]    = header[38] | (header[39] << 8);
-  level->score[SC_KEY]         = header[40] | (header[41] << 8);
-  level->score[SC_TIME_BONUS]  = header[42] | (header[43] << 8);
-
-  level->time                  = header[44] | (header[45] << 8);
-
-  level->amoeba_speed          = header[46] | (header[47] << 8);
-  level->time_light            = header[48] | (header[49] << 8);
-  level->time_timegate         = header[50] | (header[51] << 8);
-  level->time_wheel            = header[52] | (header[53] << 8);
-  level->time_magic_wall       = header[54] | (header[55] << 8);
-  level->extra_time            = header[56] | (header[57] << 8);
-  level->shield_normal_time    = header[58] | (header[59] << 8);
-
-  fclose(file);
-
-  /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
-     can slip down from flat walls, like normal walls and steel walls */
-  level->em_slippery_gems = TRUE;
-
-#if 0
-  /* Diamond Caves II levels are always surrounded by indestructible wall, but
-     not necessarily in a rectangular way -- fill with invisible steel wall */
-
-  /* !!! not always true !!! keep level and set BorderElement instead !!! */
-
-  for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
-  {
-#if 1
-    if ((x == 0 || x == level->fieldx - 1 ||
-        y == 0 || y == level->fieldy - 1) &&
-       level->field[x][y] == EL_EMPTY)
-      level->field[x][y] = EL_INVISIBLE_STEELWALL;
-#else
-    if ((x == 0 || x == level->fieldx - 1 ||
-        y == 0 || y == level->fieldy - 1) &&
-       level->field[x][y] == EL_EMPTY)
-      FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL,
-                    level->field, level->fieldx, level->fieldy);
-#endif
-  }
-#endif
-}
-
-#endif
-
 
 /* ------------------------------------------------------------------------- */
 /* functions for loading SB level                                            */
@@ -6771,11 +5592,7 @@ int getMappedElement_SB(int element_ascii, boolean use_ces)
     { '.', EL_SOKOBAN_FIELD_EMPTY,  EL_CUSTOM_5 },  /* goal square */
     { '*', EL_SOKOBAN_FIELD_FULL,   EL_CUSTOM_6 },  /* box on goal square */
     { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 },  /* player on goal square */
-#if 0
-    { '_', EL_INVISIBLE_STEELWALL,  EL_CUSTOM_8 },  /* floor beyond border */
-#else
     { '_', EL_INVISIBLE_STEELWALL,  EL_FROM_LEVEL_TEMPLATE },  /* floor beyond border */
-#endif
 
     { 0,   -1,                      -1          },
   };
@@ -6798,7 +5615,7 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
   char last_comment[MAX_LINE_LEN];
   char level_name[MAX_LINE_LEN];
   char *line_ptr;
-  FILE *file;
+  File *file;
   int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
   boolean read_continued_line = FALSE;
   boolean reading_playfield = FALSE;
@@ -6809,15 +5626,10 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
   int line_nr = 0;
   int x = 0, y = 0;            /* initialized to make compilers happy */
 
-#if 0
-  printf("::: looking for level number %d [%d]\n",
-        level_file_info->nr, num_levels_to_skip);
-#endif
-
   last_comment[0] = '\0';
   level_name[0] = '\0';
 
-  if (!(file = fopen(filename, MODE_READ)))
+  if (!(file = openFile(filename, MODE_READ)))
   {
     level->no_valid_file = TRUE;
 
@@ -6827,15 +5639,11 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
     return;
   }
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
     /* level successfully read, but next level may follow here */
     if (!got_valid_playfield_line && reading_playfield)
     {
-#if 0
-      printf("::: read complete playfield\n");
-#endif
-
       /* read playfield from single level file -- skip remaining file */
       if (!level_file_info->packed)
        break;
@@ -6854,7 +5662,7 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
     got_valid_playfield_line = FALSE;
 
     /* read next line of input file */
-    if (!fgets(line, MAX_LINE_LEN, file))
+    if (!getStringFromFile(file, line, MAX_LINE_LEN))
       break;
 
     /* check if line was completely read and is terminated by line break */
@@ -6904,10 +5712,6 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
 
       strcpy(last_comment, line_ptr);
 
-#if 0
-      printf("::: found comment '%s' in line %d\n", last_comment, line_nr);
-#endif
-
       continue;
     }
 
@@ -6919,10 +5723,6 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
       if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'')
        level_name[strlen(level_name) - 1] = '\0';
 
-#if 0
-      printf("::: found level name '%s' in line %d\n", level_name, line_nr);
-#endif
-
       continue;
     }
 
@@ -6935,10 +5735,6 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
 
     /* at this point, we have found a line containing part of a playfield */
 
-#if 0
-    printf("::: found playfield row in line %d\n", line_nr);
-#endif
-
     got_valid_playfield_line = TRUE;
 
     if (!reading_playfield)
@@ -7001,7 +5797,7 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
     y++;
   }
 
-  fclose(file);
+  closeFile(file);
 
   level->fieldy = y;
 
@@ -7021,19 +5817,11 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
   {
     strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN);
     level->name[MAX_LEVEL_NAME_LEN] = '\0';
-
-#if 0
-    printf(":1: level name: '%s'\n", level->name);
-#endif
   }
   else if (*last_comment != '\0')
   {
     strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN);
     level->name[MAX_LEVEL_NAME_LEN] = '\0';
-
-#if 0
-    printf(":2: level name: '%s'\n", level->name);
-#endif
   }
   else
   {
@@ -7057,37 +5845,7 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
 
   if (load_xsb_to_ces)
   {
-#if 1
-    /* !!! special global settings can now be set in level template !!! */
-#else
-    level->initial_player_stepsize[0] = STEPSIZE_SLOW;
-#endif
-
-    /* fill smaller playfields with padding "beyond border wall" elements */
-    if (level->fieldx < SCR_FIELDX ||
-       level->fieldy < SCR_FIELDY)
-    {
-      short field[level->fieldx][level->fieldy];
-      int new_fieldx = MAX(level->fieldx, SCR_FIELDX);
-      int new_fieldy = MAX(level->fieldy, SCR_FIELDY);
-      int pos_fieldx = (new_fieldx - level->fieldx) / 2;
-      int pos_fieldy = (new_fieldy - level->fieldy) / 2;
-
-      /* copy old playfield (which is smaller than the visible area) */
-      for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
-       field[x][y] = level->field[x][y];
-
-      /* fill new, larger playfield with "beyond border wall" elements */
-      for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++)
-       level->field[x][y] = getMappedElement_SB('_', load_xsb_to_ces);
-
-      /* copy the old playfield to the middle of the new playfield */
-      for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
-       level->field[pos_fieldx + x][pos_fieldy + y] = field[x][y];
-
-      level->fieldx = new_fieldx;
-      level->fieldy = new_fieldy;
-    }
+    /* special global settings can now be set in level template */
 
     level->use_custom_template = TRUE;
   }
@@ -7160,7 +5918,7 @@ static void LoadLevelFromFileInfo(struct LevelInfo *level,
                                  boolean level_info_only)
 {
   /* always start with reliable default values */
-  setLevelInfoToDefaults(level, level_info_only);
+  setLevelInfoToDefaults(level, level_info_only, TRUE);
 
   switch (level_file_info->type)
   {
@@ -7193,11 +5951,7 @@ static void LoadLevelFromFileInfo(struct LevelInfo *level,
 
   /* if level file is invalid, restore level structure to default values */
   if (level->no_valid_file)
-  {
-    setLevelInfoToDefaults(level, level_info_only);
-
-    level->no_valid_file = TRUE;       /* but keep "no valid file" flag */
-  }
+    setLevelInfoToDefaults(level, level_info_only, FALSE);
 
   if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
     level->game_engine_type = GAME_ENGINE_TYPE_RND;
@@ -7234,10 +5988,6 @@ static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
     level->score[SC_TIME_BONUS] /= 10;
   }
 
-#if 0
-  leveldir_current->latest_engine = TRUE;      /* !!! TEST ONLY !!! */
-#endif
-
   if (leveldir_current->latest_engine)
   {
     /* ---------- use latest game engine ----------------------------------- */
@@ -7300,11 +6050,6 @@ static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
 
     /* extra time score was same value as time left score before 3.2.0-5 */
     level->extra_time_score = level->score[SC_TIME_BONUS];
-
-#if 0
-    /* time bonus score was given for 10 s instead of 1 s before 3.2.0-5 */
-    level->score[SC_TIME_BONUS] /= 10;
-#endif
   }
 
   if (level->game_version < VERSION_IDENT(3,2,0,7))
@@ -7369,7 +6114,6 @@ 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))
   {
@@ -7391,37 +6135,8 @@ static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
        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;
+    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) */
@@ -7574,6 +6289,12 @@ static void LoadLevel_InitPlayfield(struct LevelInfo *level, char *filename)
       level->field[x][y] = getMappedElementByVersion(level->field[x][y],
                                                     level->game_version);
 
+  /* clear unused playfield data (nicer if level gets resized in editor) */
+  for (x = 0; x < MAX_LEV_FIELDX; x++)
+    for (y = 0; y < MAX_LEV_FIELDY; y++)
+      if (x >= level->fieldx || y >= level->fieldy)
+       level->field[x][y] = EL_EMPTY;
+
   /* copy elements to runtime playfield array */
   for (x = 0; x < MAX_LEV_FIELDX; x++)
     for (y = 0; y < MAX_LEV_FIELDY; y++)
@@ -7634,14 +6355,7 @@ void LoadLevel(int nr)
 
 void LoadLevelInfoOnly(int nr)
 {
-#if 0
-  char *filename;
-#endif
-
   setLevelFileInfo(&level.file_info, nr);
-#if 0
-  filename = level.file_info.filename;
-#endif
 
   LoadLevelFromFileInfo(&level, &level.file_info, TRUE);
 }
@@ -7667,7 +6381,7 @@ static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
   return chunk_size;
 }
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
 {
   int i, x, y;
@@ -7741,7 +6455,7 @@ static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
   return chunk_size;
 }
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
 {
   int chunk_size = 0;
@@ -7770,7 +6484,7 @@ static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
   return chunk_size;
 }
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
 {
   int i, x, y;
@@ -7790,7 +6504,7 @@ static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
 {
   int i, x, y;
@@ -7843,7 +6557,7 @@ static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
 {
   int envelope_nr = element - EL_ENVELOPE_1;
@@ -7866,7 +6580,7 @@ static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
                           int num_changed_custom_elements)
 {
@@ -7897,7 +6611,7 @@ static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
                           int num_changed_custom_elements)
 {
@@ -7926,7 +6640,7 @@ static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
                           int num_changed_custom_elements)
 {
@@ -8009,7 +6723,7 @@ static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
 {
   struct ElementInfo *ei = &element_info[element];
@@ -8129,7 +6843,7 @@ static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
 }
 #endif
 
-#if 0
+#if ENABLE_HISTORIC_CHUNKS
 static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
 {
   struct ElementInfo *ei = &element_info[element];
@@ -8331,12 +7045,6 @@ static int SaveLevel_CUSX(FILE *file, struct LevelInfo *level, int element)
   /* set default description string for this specific element */
   strcpy(xx_default_description, getDefaultElementDescription(ei));
 
-#if 0
-  /* set (fixed) number of content areas (may be wrong by broken level file) */
-  /* (this is now directly corrected for broken level files after loading) */
-  xx_num_contents = 1;
-#endif
-
   for (i = 0; chunk_config_CUSX_base[i].data_type != -1; i++)
     chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_base[i], FALSE);
 
@@ -8380,7 +7088,8 @@ static int SaveLevel_GRPX(FILE *file, struct LevelInfo *level, int element)
   return chunk_size;
 }
 
-static void SaveLevelFromFilename(struct LevelInfo *level, char *filename)
+static void SaveLevelFromFilename(struct LevelInfo *level, char *filename,
+                                 boolean save_as_template)
 {
   int chunk_size;
   int i;
@@ -8444,7 +7153,7 @@ static void SaveLevelFromFilename(struct LevelInfo *level, char *filename)
   }
 
   /* if not using template level, check for non-default custom/group elements */
-  if (!level->use_custom_template)
+  if (!level->use_custom_template || save_as_template)
   {
     for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
     {
@@ -8480,14 +7189,14 @@ void SaveLevel(int nr)
 {
   char *filename = getDefaultLevelFilename(nr);
 
-  SaveLevelFromFilename(&level, filename);
+  SaveLevelFromFilename(&level, filename, FALSE);
 }
 
 void SaveLevelTemplate()
 {
-  char *filename = getDefaultLevelFilename(-1);
+  char *filename = getLocalLevelTemplateFilename();
 
-  SaveLevelFromFilename(&level, filename);
+  SaveLevelFromFilename(&level, filename, TRUE);
 }
 
 boolean SaveLevelChecked(int nr)
@@ -8496,12 +7205,12 @@ boolean SaveLevelChecked(int nr)
   boolean new_level = !fileExists(filename);
   boolean level_saved = FALSE;
 
-  if (new_level || Request("Save this level and kill the old ?", REQ_ASK))
+  if (new_level || Request("Save this level and kill the old?", REQ_ASK))
   {
     SaveLevel(nr);
 
     if (new_level)
-      Request("Level saved !", REQ_CONFIRM);
+      Request("Level saved!", REQ_CONFIRM);
 
     level_saved = TRUE;
   }
@@ -8511,41 +7220,41 @@ boolean SaveLevelChecked(int nr)
 
 void DumpLevel(struct LevelInfo *level)
 {
-  if (level->no_valid_file)
+  if (level->no_level_file || level->no_valid_file)
   {
     Error(ERR_WARN, "cannot dump -- no valid level file found");
 
     return;
   }
 
-  printf_line("-", 79);
-  printf("Level xxx (file version %08d, game version %08d)\n",
-        level->file_version, level->game_version);
-  printf_line("-", 79);
+  PrintLine("-", 79);
+  Print("Level xxx (file version %08d, game version %08d)\n",
+       level->file_version, level->game_version);
+  PrintLine("-", 79);
 
-  printf("Level author: '%s'\n", level->author);
-  printf("Level title:  '%s'\n", level->name);
-  printf("\n");
-  printf("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
-  printf("\n");
-  printf("Level time:  %d seconds\n", level->time);
-  printf("Gems needed: %d\n", level->gems_needed);
-  printf("\n");
-  printf("Time for magic wall: %d seconds\n", level->time_magic_wall);
-  printf("Time for wheel:      %d seconds\n", level->time_wheel);
-  printf("Time for light:      %d seconds\n", level->time_light);
-  printf("Time for timegate:   %d seconds\n", level->time_timegate);
-  printf("\n");
-  printf("Amoeba speed: %d\n", level->amoeba_speed);
-  printf("\n");
+  Print("Level author: '%s'\n", level->author);
+  Print("Level title:  '%s'\n", level->name);
+  Print("\n");
+  Print("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
+  Print("\n");
+  Print("Level time:  %d seconds\n", level->time);
+  Print("Gems needed: %d\n", level->gems_needed);
+  Print("\n");
+  Print("Time for magic wall: %d seconds\n", level->time_magic_wall);
+  Print("Time for wheel:      %d seconds\n", level->time_wheel);
+  Print("Time for light:      %d seconds\n", level->time_light);
+  Print("Time for timegate:   %d seconds\n", level->time_timegate);
+  Print("\n");
+  Print("Amoeba speed: %d\n", level->amoeba_speed);
+  Print("\n");
 
-  printf("EM style slippery gems:      %s\n", (level->em_slippery_gems ? "yes" : "no"));
-  printf("Player blocks last field:    %s\n", (level->block_last_field ? "yes" : "no"));
-  printf("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
-  printf("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
-  printf("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
+  Print("EM style slippery gems:      %s\n", (level->em_slippery_gems ? "yes" : "no"));
+  Print("Player blocks last field:    %s\n", (level->block_last_field ? "yes" : "no"));
+  Print("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
+  Print("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
+  Print("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
 
-  printf_line("-", 79);
+  PrintLine("-", 79);
 }
 
 
@@ -8579,7 +7288,7 @@ static void setTapeInfoToDefaults()
   tape.no_valid_file = FALSE;
 }
 
-static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape)
+static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape)
 {
   tape->file_version = getFileVersion(file);
   tape->game_version = getFileVersion(file);
@@ -8587,7 +7296,7 @@ static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape)
   return chunk_size;
 }
 
-static int LoadTape_HEAD(FILE *file, int chunk_size, struct TapeInfo *tape)
+static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape)
 {
   int i;
 
@@ -8626,7 +7335,7 @@ static int LoadTape_HEAD(FILE *file, int chunk_size, struct TapeInfo *tape)
   return chunk_size;
 }
 
-static int LoadTape_INFO(FILE *file, int chunk_size, struct TapeInfo *tape)
+static int LoadTape_INFO(File *file, int chunk_size, struct TapeInfo *tape)
 {
   int level_identifier_size;
   int i;
@@ -8646,7 +7355,7 @@ static int LoadTape_INFO(FILE *file, int chunk_size, struct TapeInfo *tape)
   return chunk_size;
 }
 
-static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
+static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
 {
   int i, j;
   int chunk_size_expected =
@@ -8661,7 +7370,16 @@ static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
   for (i = 0; i < tape->length; i++)
   {
     if (i >= MAX_TAPE_LEN)
+    {
+      Error(ERR_WARN, "tape truncated -- size exceeds maximum tape size %d",
+           MAX_TAPE_LEN);
+
+      // tape too large; read and ignore remaining tape data from this chunk
+      for (;i < tape->length; i++)
+       ReadUnusedBytesFromFile(file, tape->num_participating_players + 1);
+
       break;
+    }
 
     for (j = 0; j < MAX_PLAYERS; j++)
     {
@@ -8720,7 +7438,7 @@ static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
       }
     }
 
-    if (feof(file))
+    if (checkEndOfFile(file))
       break;
   }
 
@@ -8732,21 +7450,21 @@ static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
 
 void LoadTape_SokobanSolution(char *filename)
 {
-  FILE *file;
+  File *file;
   int move_delay = TILESIZE / level.initial_player_stepsize[0];
 
-  if (!(file = fopen(filename, MODE_READ)))
+  if (!(file = openFile(filename, MODE_READ)))
   {
     tape.no_valid_file = TRUE;
 
     return;
   }
 
-  while (!feof(file))
+  while (!checkEndOfFile(file))
   {
-    unsigned char c = fgetc(file);
+    unsigned char c = getByteFromFile(file);
 
-    if (feof(file))
+    if (checkEndOfFile(file))
       break;
 
     switch (c)
@@ -8795,19 +7513,20 @@ void LoadTape_SokobanSolution(char *filename)
     }
   }
 
-  fclose(file);
+  closeFile(file);
 
   if (tape.no_valid_file)
     return;
 
-  tape.length_seconds = GetTapeLength();
+  tape.length_frames  = GetTapeLengthFrames();
+  tape.length_seconds = GetTapeLengthSeconds();
 }
 
 void LoadTapeFromFilename(char *filename)
 {
   char cookie[MAX_LINE_LEN];
   char chunk_name[CHUNK_ID_LEN + 1];
-  FILE *file;
+  File *file;
   int chunk_size;
 
   /* always start with reliable default values */
@@ -8820,7 +7539,7 @@ void LoadTapeFromFilename(char *filename)
     return;
   }
 
-  if (!(file = fopen(filename, MODE_READ)))
+  if (!(file = openFile(filename, MODE_READ)))
   {
     tape.no_valid_file = TRUE;
 
@@ -8838,14 +7557,16 @@ void LoadTapeFromFilename(char *filename)
       tape.no_valid_file = TRUE;
 
       Error(ERR_WARN, "unknown format of tape file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
+
       return;
     }
   }
   else /* check for pre-2.0 file format with cookie string */
   {
     strcpy(cookie, chunk_name);
-    if (fgets(&cookie[4], MAX_LINE_LEN - 4, file) == NULL)
+    if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
       cookie[4] = '\0';
     if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
       cookie[strlen(cookie) - 1] = '\0';
@@ -8855,7 +7576,9 @@ void LoadTapeFromFilename(char *filename)
       tape.no_valid_file = TRUE;
 
       Error(ERR_WARN, "unknown format of tape file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
+
       return;
     }
 
@@ -8864,7 +7587,8 @@ void LoadTapeFromFilename(char *filename)
       tape.no_valid_file = TRUE;
 
       Error(ERR_WARN, "unsupported version of tape file '%s'", filename);
-      fclose(file);
+
+      closeFile(file);
 
       return;
     }
@@ -8885,7 +7609,7 @@ void LoadTapeFromFilename(char *filename)
     {
       char *name;
       int size;
-      int (*loader)(FILE *, int, struct TapeInfo *);
+      int (*loader)(File *, int, struct TapeInfo *);
     }
     chunk_info[] =
     {
@@ -8935,13 +7659,14 @@ void LoadTapeFromFilename(char *filename)
     }
   }
 
-  fclose(file);
+  closeFile(file);
 
-  tape.length_seconds = GetTapeLength();
+  tape.length_frames  = GetTapeLengthFrames();
+  tape.length_seconds = GetTapeLengthSeconds();
 
 #if 0
-  printf("::: tape file version: %d\n", tape.file_version);
-  printf("::: tape game version: %d\n", tape.game_version);
+  printf("::: tape file version: %d\n",   tape.file_version);
+  printf("::: tape game version: %d\n",   tape.game_version);
   printf("::: tape engine version: %d\n", tape.engine_version);
 #endif
 }
@@ -8959,12 +7684,10 @@ void LoadSolutionTape(int 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)
@@ -9026,9 +7749,6 @@ void SaveTape(int nr)
 {
   char *filename = getTapeFilename(nr);
   FILE *file;
-#if 0
-  boolean new_tape = TRUE;
-#endif
   int num_participating_players = 0;
   int info_chunk_size;
   int body_chunk_size;
@@ -9036,16 +7756,6 @@ void SaveTape(int nr)
 
   InitTapeDirectory(leveldir_current->subdir);
 
-#if 0
-  /* if a tape still exists, ask to overwrite it */
-  if (fileExists(filename))
-  {
-    new_tape = FALSE;
-    if (!Request("Replace old tape ?", REQ_ASK))
-      return;
-  }
-#endif
-
   if (!(file = fopen(filename, MODE_WRITE)))
   {
     Error(ERR_WARN, "cannot save level recording file '%s'", filename);
@@ -9083,11 +7793,6 @@ void SaveTape(int nr)
   SetFilePermissions(filename, PERMS_PRIVATE);
 
   tape.changed = FALSE;
-
-#if 0
-  if (new_tape)
-    Request("Tape saved !", REQ_CONFIRM);
-#endif
 }
 
 boolean SaveTapeChecked(int nr)
@@ -9096,12 +7801,12 @@ boolean SaveTapeChecked(int nr)
   boolean new_tape = !fileExists(filename);
   boolean tape_saved = FALSE;
 
-  if (new_tape || Request("Replace old tape ?", REQ_ASK))
+  if (new_tape || Request("Replace old tape?", REQ_ASK))
   {
     SaveTape(nr);
 
     if (new_tape)
-      Request("Tape saved !", REQ_CONFIRM);
+      Request("Tape saved!", REQ_CONFIRM);
 
     tape_saved = TRUE;
   }
@@ -9121,13 +7826,13 @@ void DumpTape(struct TapeInfo *tape)
     return;
   }
 
-  printf_line("-", 79);
-  printf("Tape of Level %03d (file version %08d, game version %08d)\n",
-        tape->level_nr, tape->file_version, tape->game_version);
-  printf("                  (effective engine version %08d)\n",
-        tape->engine_version);
-  printf("Level series identifier: '%s'\n", tape->level_identifier);
-  printf_line("-", 79);
+  PrintLine("-", 79);
+  Print("Tape of Level %03d (file version %08d, game version %08d)\n",
+       tape->level_nr, tape->file_version, tape->game_version);
+  Print("                  (effective engine version %08d)\n",
+       tape->engine_version);
+  Print("Level series identifier: '%s'\n", tape->level_identifier);
+  PrintLine("-", 79);
 
   tape_frame_counter = 0;
 
@@ -9136,7 +7841,7 @@ void DumpTape(struct TapeInfo *tape)
     if (i >= MAX_TAPE_LEN)
       break;
 
-    printf("%04d: ", i);
+    Print("%04d: ", i);
 
     for (j = 0; j < MAX_PLAYERS; j++)
     {
@@ -9144,24 +7849,24 @@ void DumpTape(struct TapeInfo *tape)
       {
        int action = tape->pos[i].action[j];
 
-       printf("%d:%02x ", j, action);
-       printf("[%c%c%c%c|%c%c] - ",
-              (action & JOY_LEFT ? '<' : ' '),
-              (action & JOY_RIGHT ? '>' : ' '),
-              (action & JOY_UP ? '^' : ' '),
-              (action & JOY_DOWN ? 'v' : ' '),
-              (action & JOY_BUTTON_1 ? '1' : ' '),
-              (action & JOY_BUTTON_2 ? '2' : ' '));
+       Print("%d:%02x ", j, action);
+       Print("[%c%c%c%c|%c%c] - ",
+             (action & JOY_LEFT ? '<' : ' '),
+             (action & JOY_RIGHT ? '>' : ' '),
+             (action & JOY_UP ? '^' : ' '),
+             (action & JOY_DOWN ? 'v' : ' '),
+             (action & JOY_BUTTON_1 ? '1' : ' '),
+             (action & JOY_BUTTON_2 ? '2' : ' '));
       }
     }
 
-    printf("(%03d) ", tape->pos[i].delay);
-    printf("[%05d]\n", tape_frame_counter);
+    Print("(%03d) ", tape->pos[i].delay);
+    Print("[%05d]\n", tape_frame_counter);
 
     tape_frame_counter += tape->pos[i].delay;
   }
 
-  printf_line("-", 79);
+  PrintLine("-", 79);
 }
 
 
@@ -9228,6 +7933,7 @@ void LoadScore(int nr)
 void SaveScore(int nr)
 {
   int i;
+  int permissions = (program.global_scores ? PERMS_PUBLIC : PERMS_PRIVATE);
   char *filename = getScoreFilename(nr);
   FILE *file;
 
@@ -9246,7 +7952,7 @@ void SaveScore(int nr)
 
   fclose(file);
 
-  SetFilePermissions(filename, PERMS_PUBLIC);
+  SetFilePermissions(filename, permissions);
 }
 
 
@@ -9265,57 +7971,54 @@ void SaveScore(int nr)
 #define SETUP_TOKEN_TOONS                      5
 #define SETUP_TOKEN_SCROLL_DELAY               6
 #define SETUP_TOKEN_SCROLL_DELAY_VALUE         7
-#define SETUP_TOKEN_SOFT_SCROLLING             8
-#define SETUP_TOKEN_FADE_SCREENS               9
-#define SETUP_TOKEN_AUTORECORD                 10
-#define SETUP_TOKEN_SHOW_TITLESCREEN           11
-#define SETUP_TOKEN_QUICK_DOORS                        12
-#define SETUP_TOKEN_TEAM_MODE                  13
-#define SETUP_TOKEN_HANDICAP                   14
-#define SETUP_TOKEN_SKIP_LEVELS                        15
-#define SETUP_TOKEN_TIME_LIMIT                 16
-#define SETUP_TOKEN_FULLSCREEN                 17
-#define SETUP_TOKEN_FULLSCREEN_MODE            18
-#define SETUP_TOKEN_ASK_ON_ESCAPE              19
-#define SETUP_TOKEN_ASK_ON_ESCAPE_EDITOR       20
-#define SETUP_TOKEN_QUICK_SWITCH               21
-#define SETUP_TOKEN_INPUT_ON_FOCUS             22
-#define SETUP_TOKEN_PREFER_AGA_GRAPHICS                23
-#define SETUP_TOKEN_GAME_FRAME_DELAY           24
-#define SETUP_TOKEN_SP_SHOW_BORDER_ELEMENTS    25
-#define SETUP_TOKEN_SMALL_GAME_GRAPHICS                26
-#define SETUP_TOKEN_GRAPHICS_SET               27
-#define SETUP_TOKEN_SOUNDS_SET                 28
-#define SETUP_TOKEN_MUSIC_SET                  29
-#define SETUP_TOKEN_OVERRIDE_LEVEL_GRAPHICS    30
-#define SETUP_TOKEN_OVERRIDE_LEVEL_SOUNDS      31
-#define SETUP_TOKEN_OVERRIDE_LEVEL_MUSIC       32
-#define SETUP_TOKEN_VOLUME_SIMPLE              33
-#define SETUP_TOKEN_VOLUME_LOOPS               34
-#define SETUP_TOKEN_VOLUME_MUSIC               35
-
-#define NUM_GLOBAL_SETUP_TOKENS                        36
+#define SETUP_TOKEN_ENGINE_SNAPSHOT_MODE       8
+#define SETUP_TOKEN_ENGINE_SNAPSHOT_MEMORY     9
+#define SETUP_TOKEN_FADE_SCREENS               10
+#define SETUP_TOKEN_AUTORECORD                 11
+#define SETUP_TOKEN_SHOW_TITLESCREEN           12
+#define SETUP_TOKEN_QUICK_DOORS                        13
+#define SETUP_TOKEN_TEAM_MODE                  14
+#define SETUP_TOKEN_HANDICAP                   15
+#define SETUP_TOKEN_SKIP_LEVELS                        16
+#define SETUP_TOKEN_INCREMENT_LEVELS           17
+#define SETUP_TOKEN_TIME_LIMIT                 18
+#define SETUP_TOKEN_FULLSCREEN                 19
+#define SETUP_TOKEN_WINDOW_SCALING_PERCENT     20
+#define SETUP_TOKEN_WINDOW_SCALING_QUALITY     21
+#define SETUP_TOKEN_SCREEN_RENDERING_MODE      22
+#define SETUP_TOKEN_ASK_ON_ESCAPE              23
+#define SETUP_TOKEN_ASK_ON_ESCAPE_EDITOR       24
+#define SETUP_TOKEN_QUICK_SWITCH               25
+#define SETUP_TOKEN_INPUT_ON_FOCUS             26
+#define SETUP_TOKEN_PREFER_AGA_GRAPHICS                27
+#define SETUP_TOKEN_GAME_FRAME_DELAY           28
+#define SETUP_TOKEN_SP_SHOW_BORDER_ELEMENTS    29
+#define SETUP_TOKEN_SMALL_GAME_GRAPHICS                30
+#define SETUP_TOKEN_SHOW_SNAPSHOT_BUTTONS      31
+#define SETUP_TOKEN_GRAPHICS_SET               32
+#define SETUP_TOKEN_SOUNDS_SET                 33
+#define SETUP_TOKEN_MUSIC_SET                  34
+#define SETUP_TOKEN_OVERRIDE_LEVEL_GRAPHICS    35
+#define SETUP_TOKEN_OVERRIDE_LEVEL_SOUNDS      36
+#define SETUP_TOKEN_OVERRIDE_LEVEL_MUSIC       37
+#define SETUP_TOKEN_VOLUME_SIMPLE              38
+#define SETUP_TOKEN_VOLUME_LOOPS               39
+#define SETUP_TOKEN_VOLUME_MUSIC               40
+#define SETUP_TOKEN_TOUCH_CONTROL_TYPE         41
+#define SETUP_TOKEN_TOUCH_MOVE_DISTANCE                42
+#define SETUP_TOKEN_TOUCH_DROP_DISTANCE                43
+
+#define NUM_GLOBAL_SETUP_TOKENS                        44
 
 /* editor setup */
-#define SETUP_TOKEN_EDITOR_EL_BOULDERDASH      0
-#define SETUP_TOKEN_EDITOR_EL_EMERALD_MINE     1
-#define SETUP_TOKEN_EDITOR_EL_EMERALD_MINE_CLUB        2
-#define SETUP_TOKEN_EDITOR_EL_MORE             3
-#define SETUP_TOKEN_EDITOR_EL_SOKOBAN          4
-#define SETUP_TOKEN_EDITOR_EL_SUPAPLEX         5
-#define SETUP_TOKEN_EDITOR_EL_DIAMOND_CAVES    6
-#define SETUP_TOKEN_EDITOR_EL_DX_BOULDERDASH   7
-#define SETUP_TOKEN_EDITOR_EL_CHARS            8
-#define SETUP_TOKEN_EDITOR_EL_STEEL_CHARS      9
-#define SETUP_TOKEN_EDITOR_EL_CUSTOM           10
-#define SETUP_TOKEN_EDITOR_EL_HEADLINES                11
-#define SETUP_TOKEN_EDITOR_EL_USER_DEFINED     12
-#define SETUP_TOKEN_EDITOR_EL_DYNAMIC          13
-#define SETUP_TOKEN_EDITOR_EL_BY_GAME          14
-#define SETUP_TOKEN_EDITOR_EL_BY_TYPE          15
-#define SETUP_TOKEN_EDITOR_SHOW_ELEMENT_TOKEN  16
-
-#define NUM_EDITOR_SETUP_TOKENS                        17
+#define SETUP_TOKEN_EDITOR_EL_CLASSIC          0
+#define SETUP_TOKEN_EDITOR_EL_CUSTOM           1
+#define SETUP_TOKEN_EDITOR_EL_USER_DEFINED     2
+#define SETUP_TOKEN_EDITOR_EL_DYNAMIC          3
+#define SETUP_TOKEN_EDITOR_EL_HEADLINES                4
+#define SETUP_TOKEN_EDITOR_SHOW_ELEMENT_TOKEN  5
+
+#define NUM_EDITOR_SETUP_TOKENS                        6
 
 /* editor cascade setup */
 #define SETUP_TOKEN_EDITOR_CASCADE_BD          0
@@ -9388,6 +8091,56 @@ void SaveScore(int nr)
 
 #define NUM_SYSTEM_SETUP_TOKENS                        3
 
+/* internal setup */
+#define SETUP_TOKEN_INT_PROGRAM_TITLE          0
+#define SETUP_TOKEN_INT_PROGRAM_VERSION                1
+#define SETUP_TOKEN_INT_PROGRAM_AUTHOR         2
+#define SETUP_TOKEN_INT_PROGRAM_EMAIL          3
+#define SETUP_TOKEN_INT_PROGRAM_WEBSITE                4
+#define SETUP_TOKEN_INT_PROGRAM_COPYRIGHT      5
+#define SETUP_TOKEN_INT_PROGRAM_COMPANY                6
+#define SETUP_TOKEN_INT_PROGRAM_ICON_FILE      7
+#define SETUP_TOKEN_INT_DEFAULT_GRAPHICS_SET   8
+#define SETUP_TOKEN_INT_DEFAULT_SOUNDS_SET     9
+#define SETUP_TOKEN_INT_DEFAULT_MUSIC_SET      10
+#define SETUP_TOKEN_INT_FALLBACK_GRAPHICS_FILE 11
+#define SETUP_TOKEN_INT_FALLBACK_SOUNDS_FILE   12
+#define SETUP_TOKEN_INT_FALLBACK_MUSIC_FILE    13
+#define SETUP_TOKEN_INT_DEFAULT_LEVEL_SERIES   14
+#define SETUP_TOKEN_INT_CHOOSE_FROM_TOP_LEVELDIR 15
+#define SETUP_TOKEN_INT_SHOW_SCALING_IN_TITLE  16
+#define SETUP_TOKEN_INT_DEFAULT_WINDOW_WIDTH   17
+#define SETUP_TOKEN_INT_DEFAULT_WINDOW_HEIGHT  18
+
+#define NUM_INTERNAL_SETUP_TOKENS              19
+
+/* debug setup */
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_0                0
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_1                1
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_2                2
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_3                3
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_4                4
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_5                5
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_6                6
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_7                7
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_8                8
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_9                9
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_0    10
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_1    11
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_2    12
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_3    13
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_4    14
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_5    15
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_6    16
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_7    17
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_8    18
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_KEY_9    19
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_USE_MOD_KEY 20
+#define SETUP_TOKEN_DEBUG_FRAME_DELAY_GAME_ONLY        21
+#define SETUP_TOKEN_DEBUG_SHOW_FRAMES_PER_SECOND 22
+
+#define NUM_DEBUG_SETUP_TOKENS                 23
+
 /* options setup */
 #define SETUP_TOKEN_OPTIONS_VERBOSE            0
 
@@ -9400,6 +8153,8 @@ static struct SetupEditorCascadeInfo seci;
 static struct SetupShortcutInfo ssi;
 static struct SetupInputInfo sii;
 static struct SetupSystemInfo syi;
+static struct SetupInternalInfo sxi;
+static struct SetupDebugInfo sdi;
 static struct OptionInfo soi;
 
 static struct TokenInfo global_setup_tokens[] =
@@ -9412,7 +8167,8 @@ static struct TokenInfo global_setup_tokens[] =
   { TYPE_SWITCH, &si.toons,                   "toons"                  },
   { TYPE_SWITCH, &si.scroll_delay,            "scroll_delay"           },
   { TYPE_INTEGER,&si.scroll_delay_value,      "scroll_delay_value"     },
-  { TYPE_SWITCH, &si.soft_scrolling,          "soft_scrolling"         },
+  { TYPE_STRING, &si.engine_snapshot_mode,    "engine_snapshot_mode"   },
+  { TYPE_INTEGER,&si.engine_snapshot_memory,  "engine_snapshot_memory" },
   { TYPE_SWITCH, &si.fade_screens,            "fade_screens"           },
   { TYPE_SWITCH, &si.autorecord,              "automatic_tape_recording"},
   { TYPE_SWITCH, &si.show_titlescreen,        "show_titlescreen"       },
@@ -9420,9 +8176,12 @@ static struct TokenInfo global_setup_tokens[] =
   { TYPE_SWITCH, &si.team_mode,               "team_mode"              },
   { TYPE_SWITCH, &si.handicap,                "handicap"               },
   { TYPE_SWITCH, &si.skip_levels,             "skip_levels"            },
+  { TYPE_SWITCH, &si.increment_levels,        "increment_levels"       },
   { TYPE_SWITCH, &si.time_limit,              "time_limit"             },
   { TYPE_SWITCH, &si.fullscreen,              "fullscreen"             },
-  { TYPE_STRING, &si.fullscreen_mode,         "fullscreen_mode"                },
+  { TYPE_INTEGER,&si.window_scaling_percent,  "window_scaling_percent" },
+  { TYPE_STRING, &si.window_scaling_quality,  "window_scaling_quality" },
+  { TYPE_STRING, &si.screen_rendering_mode,   "screen_rendering_mode"  },
   { TYPE_SWITCH, &si.ask_on_escape,           "ask_on_escape"          },
   { TYPE_SWITCH, &si.ask_on_escape_editor,    "ask_on_escape_editor"   },
   { TYPE_SWITCH, &si.quick_switch,            "quick_player_switch"    },
@@ -9431,6 +8190,7 @@ static struct TokenInfo global_setup_tokens[] =
   { TYPE_INTEGER,&si.game_frame_delay,        "game_frame_delay"       },
   { TYPE_SWITCH, &si.sp_show_border_elements, "sp_show_border_elements"        },
   { TYPE_SWITCH, &si.small_game_graphics,     "small_game_graphics"    },
+  { TYPE_SWITCH, &si.show_snapshot_buttons,   "show_snapshot_buttons"  },
   { TYPE_STRING, &si.graphics_set,            "graphics_set"           },
   { TYPE_STRING, &si.sounds_set,              "sounds_set"             },
   { TYPE_STRING, &si.music_set,               "music_set"              },
@@ -9440,42 +8200,18 @@ static struct TokenInfo global_setup_tokens[] =
   { TYPE_INTEGER,&si.volume_simple,           "volume_simple"          },
   { TYPE_INTEGER,&si.volume_loops,            "volume_loops"           },
   { TYPE_INTEGER,&si.volume_music,            "volume_music"           },
+  { TYPE_STRING, &si.touch.control_type,      "touch.control_type"     },
+  { TYPE_INTEGER,&si.touch.move_distance,     "touch.move_distance"    },
+  { TYPE_INTEGER,&si.touch.drop_distance,     "touch.drop_distance"    },
 };
 
-static boolean not_used = FALSE;
 static struct TokenInfo editor_setup_tokens[] =
 {
-#if 1
-  { TYPE_SWITCH, &not_used,            "editor.el_boulderdash"         },
-  { TYPE_SWITCH, &not_used,            "editor.el_emerald_mine"        },
-  { TYPE_SWITCH, &not_used,            "editor.el_emerald_mine_club"   },
-  { TYPE_SWITCH, &not_used,            "editor.el_more"                },
-  { TYPE_SWITCH, &not_used,            "editor.el_sokoban"             },
-  { TYPE_SWITCH, &not_used,            "editor.el_supaplex"            },
-  { TYPE_SWITCH, &not_used,            "editor.el_diamond_caves"       },
-  { TYPE_SWITCH, &not_used,            "editor.el_dx_boulderdash"      },
-#else
-  { TYPE_SWITCH, &sei.el_boulderdash,  "editor.el_boulderdash"         },
-  { TYPE_SWITCH, &sei.el_emerald_mine, "editor.el_emerald_mine"        },
-  { TYPE_SWITCH, &sei.el_emerald_mine_club,"editor.el_emerald_mine_club"},
-  { TYPE_SWITCH, &sei.el_more,         "editor.el_more"                },
-  { TYPE_SWITCH, &sei.el_sokoban,      "editor.el_sokoban"             },
-  { TYPE_SWITCH, &sei.el_supaplex,     "editor.el_supaplex"            },
-  { TYPE_SWITCH, &sei.el_diamond_caves,        "editor.el_diamond_caves"       },
-  { TYPE_SWITCH, &sei.el_dx_boulderdash,"editor.el_dx_boulderdash"     },
-#endif
-  { TYPE_SWITCH, &sei.el_chars,                "editor.el_chars"               },
-  { TYPE_SWITCH, &sei.el_steel_chars,  "editor.el_steel_chars"         },
+  { TYPE_SWITCH, &sei.el_classic,      "editor.el_classic"             },
   { TYPE_SWITCH, &sei.el_custom,       "editor.el_custom"              },
-#if 1
-  { TYPE_SWITCH, &not_used,            "editor.el_headlines"           },
-#else
-  { TYPE_SWITCH, &sei.el_headlines,    "editor.el_headlines"           },
-#endif
   { TYPE_SWITCH, &sei.el_user_defined, "editor.el_user_defined"        },
   { TYPE_SWITCH, &sei.el_dynamic,      "editor.el_dynamic"             },
-  { TYPE_SWITCH, &sei.el_by_game,      "editor.el_by_game"             },
-  { TYPE_SWITCH, &sei.el_by_type,      "editor.el_by_type"             },
+  { TYPE_SWITCH, &sei.el_headlines,    "editor.el_headlines"           },
   { TYPE_SWITCH, &sei.show_element_token,"editor.show_element_token"   },
 };
 
@@ -9545,11 +8281,61 @@ static struct TokenInfo player_setup_tokens[] =
 
 static struct TokenInfo system_setup_tokens[] =
 {
-  { TYPE_STRING,  &syi.sdl_videodriver,        "system.sdl_videodriver"        },
-  { TYPE_STRING,  &syi.sdl_audiodriver,        "system.sdl_audiodriver"        },
+  { TYPE_STRING,  &syi.sdl_videodriver,    "system.sdl_videodriver"    },
+  { TYPE_STRING,  &syi.sdl_audiodriver,           "system.sdl_audiodriver"     },
   { TYPE_INTEGER, &syi.audio_fragment_size,"system.audio_fragment_size"        },
 };
 
+static struct TokenInfo internal_setup_tokens[] =
+{
+  { TYPE_STRING, &sxi.program_title,           "program_title"         },
+  { TYPE_STRING, &sxi.program_version,         "program_version"       },
+  { TYPE_STRING, &sxi.program_author,          "program_author"        },
+  { TYPE_STRING, &sxi.program_email,           "program_email"         },
+  { TYPE_STRING, &sxi.program_website,         "program_website"       },
+  { TYPE_STRING, &sxi.program_copyright,       "program_copyright"     },
+  { TYPE_STRING, &sxi.program_company,         "program_company"       },
+  { TYPE_STRING, &sxi.program_icon_file,       "program_icon_file"     },
+  { TYPE_STRING, &sxi.default_graphics_set,    "default_graphics_set"  },
+  { TYPE_STRING, &sxi.default_sounds_set,      "default_sounds_set"    },
+  { TYPE_STRING, &sxi.default_music_set,       "default_music_set"     },
+  { TYPE_STRING, &sxi.fallback_graphics_file,  "fallback_graphics_file"},
+  { TYPE_STRING, &sxi.fallback_sounds_file,    "fallback_sounds_file"  },
+  { TYPE_STRING, &sxi.fallback_music_file,     "fallback_music_file"   },
+  { TYPE_STRING, &sxi.default_level_series,    "default_level_series"  },
+  { TYPE_BOOLEAN,&sxi.choose_from_top_leveldir,        "choose_from_top_leveldir" },
+  { TYPE_BOOLEAN,&sxi.show_scaling_in_title,   "show_scaling_in_title" },
+  { TYPE_INTEGER,&sxi.default_window_width,    "default_window_width"  },
+  { TYPE_INTEGER,&sxi.default_window_height,   "default_window_height" },
+};
+
+static struct TokenInfo debug_setup_tokens[] =
+{
+  { TYPE_INTEGER, &sdi.frame_delay[0],         "debug.frame_delay_0"   },
+  { TYPE_INTEGER, &sdi.frame_delay[1],         "debug.frame_delay_1"   },
+  { TYPE_INTEGER, &sdi.frame_delay[2],         "debug.frame_delay_2"   },
+  { TYPE_INTEGER, &sdi.frame_delay[3],         "debug.frame_delay_3"   },
+  { TYPE_INTEGER, &sdi.frame_delay[4],         "debug.frame_delay_4"   },
+  { TYPE_INTEGER, &sdi.frame_delay[5],         "debug.frame_delay_5"   },
+  { TYPE_INTEGER, &sdi.frame_delay[6],         "debug.frame_delay_6"   },
+  { TYPE_INTEGER, &sdi.frame_delay[7],         "debug.frame_delay_7"   },
+  { TYPE_INTEGER, &sdi.frame_delay[8],         "debug.frame_delay_8"   },
+  { TYPE_INTEGER, &sdi.frame_delay[9],         "debug.frame_delay_9"   },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[0],     "debug.key.frame_delay_0" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[1],     "debug.key.frame_delay_1" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[2],     "debug.key.frame_delay_2" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[3],     "debug.key.frame_delay_3" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[4],     "debug.key.frame_delay_4" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[5],     "debug.key.frame_delay_5" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[6],     "debug.key.frame_delay_6" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[7],     "debug.key.frame_delay_7" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[8],     "debug.key.frame_delay_8" },
+  { TYPE_KEY_X11, &sdi.frame_delay_key[9],     "debug.key.frame_delay_9" },
+  { TYPE_BOOLEAN, &sdi.frame_delay_use_mod_key,"debug.frame_delay.use_mod_key"},
+  { TYPE_BOOLEAN, &sdi.frame_delay_game_only,  "debug.frame_delay.game_only" },
+  { TYPE_BOOLEAN, &sdi.show_frames_per_second, "debug.show_frames_per_second" },
+};
+
 static struct TokenInfo options_setup_tokens[] =
 {
   { TYPE_BOOLEAN, &soi.verbose,                "options.verbose"               },
@@ -9583,7 +8369,8 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->toons = TRUE;
   si->scroll_delay = TRUE;
   si->scroll_delay_value = STD_SCROLL_DELAY;
-  si->soft_scrolling = TRUE;
+  si->engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_DEFAULT);
+  si->engine_snapshot_memory = SNAPSHOT_MEMORY_DEFAULT;
   si->fade_screens = TRUE;
   si->autorecord = TRUE;
   si->show_titlescreen = TRUE;
@@ -9591,9 +8378,12 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->team_mode = FALSE;
   si->handicap = TRUE;
   si->skip_levels = TRUE;
+  si->increment_levels = TRUE;
   si->time_limit = TRUE;
   si->fullscreen = FALSE;
-  si->fullscreen_mode = getStringCopy(DEFAULT_FULLSCREEN_MODE);
+  si->window_scaling_percent = STD_WINDOW_SCALING_PERCENT;
+  si->window_scaling_quality = getStringCopy(SCALING_QUALITY_DEFAULT);
+  si->screen_rendering_mode = getStringCopy(STR_SPECIAL_RENDERING_DEFAULT);
   si->ask_on_escape = TRUE;
   si->ask_on_escape_editor = TRUE;
   si->quick_switch = FALSE;
@@ -9602,17 +8392,23 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->game_frame_delay = GAME_FRAME_DELAY;
   si->sp_show_border_elements = FALSE;
   si->small_game_graphics = FALSE;
+  si->show_snapshot_buttons = FALSE;
+
+  si->graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
+  si->sounds_set   = getStringCopy(SND_CLASSIC_SUBDIR);
+  si->music_set    = getStringCopy(MUS_CLASSIC_SUBDIR);
 
-  si->graphics_set = getStringCopy(GFX_DEFAULT_SUBDIR);
-  si->sounds_set = getStringCopy(SND_DEFAULT_SUBDIR);
-  si->music_set = getStringCopy(MUS_DEFAULT_SUBDIR);
   si->override_level_graphics = FALSE;
   si->override_level_sounds = FALSE;
   si->override_level_music = FALSE;
 
-  si->volume_simple = 100;     /* percent */
-  si->volume_loops = 100;      /* percent */
-  si->volume_music = 100;      /* percent */
+  si->volume_simple = 100;             /* percent */
+  si->volume_loops = 100;              /* percent */
+  si->volume_music = 100;              /* percent */
+
+  si->touch.control_type = getStringCopy(TOUCH_CONTROL_DEFAULT);
+  si->touch.move_distance = TOUCH_MOVE_DISTANCE_DEFAULT;       /* percent */
+  si->touch.drop_distance = TOUCH_DROP_DISTANCE_DEFAULT;       /* percent */
 
   si->editor.el_boulderdash            = TRUE;
   si->editor.el_emerald_mine           = TRUE;
@@ -9624,13 +8420,18 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->editor.el_dx_boulderdash         = TRUE;
   si->editor.el_chars                  = TRUE;
   si->editor.el_steel_chars            = TRUE;
+
+  si->editor.el_classic                        = TRUE;
   si->editor.el_custom                 = TRUE;
 
-  si->editor.el_headlines = TRUE;
-  si->editor.el_user_defined = FALSE;
-  si->editor.el_dynamic = TRUE;
+  si->editor.el_user_defined           = FALSE;
+  si->editor.el_dynamic                        = TRUE;
+
+  si->editor.el_headlines              = TRUE;
+
+  si->editor.show_element_token                = FALSE;
 
-  si->editor.show_element_token = FALSE;
+  si->editor.use_template_for_new_levels = TRUE;
 
   si->shortcut.save_game       = DEFAULT_KEY_SAVE_GAME;
   si->shortcut.load_game       = DEFAULT_KEY_LOAD_GAME;
@@ -9682,15 +8483,62 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->system.sdl_audiodriver = getStringCopy(ARG_DEFAULT);
   si->system.audio_fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
 
+  si->internal.program_title     = getStringCopy(PROGRAM_TITLE_STRING);
+  si->internal.program_version   = getStringCopy(getProgramRealVersionString());
+  si->internal.program_author    = getStringCopy(PROGRAM_AUTHOR_STRING);
+  si->internal.program_email     = getStringCopy(PROGRAM_EMAIL_STRING);
+  si->internal.program_website   = getStringCopy(PROGRAM_WEBSITE_STRING);
+  si->internal.program_copyright = getStringCopy(PROGRAM_COPYRIGHT_STRING);
+  si->internal.program_company   = getStringCopy(PROGRAM_COMPANY_STRING);
+
+  si->internal.program_icon_file = getStringCopy(PROGRAM_ICON_FILENAME);
+
+  si->internal.default_graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
+  si->internal.default_sounds_set   = getStringCopy(SND_CLASSIC_SUBDIR);
+  si->internal.default_music_set    = getStringCopy(MUS_CLASSIC_SUBDIR);
+
+  si->internal.fallback_graphics_file = getStringCopy(UNDEFINED_FILENAME);
+  si->internal.fallback_sounds_file   = getStringCopy(UNDEFINED_FILENAME);
+  si->internal.fallback_music_file    = getStringCopy(UNDEFINED_FILENAME);
+
+  si->internal.default_level_series = getStringCopy(UNDEFINED_LEVELSET);
+  si->internal.choose_from_top_leveldir = FALSE;
+  si->internal.show_scaling_in_title = TRUE;
+
+  si->internal.default_window_width  = WIN_XSIZE_DEFAULT;
+  si->internal.default_window_height = WIN_YSIZE_DEFAULT;
+
+  si->debug.frame_delay[0] = DEFAULT_FRAME_DELAY_0;
+  si->debug.frame_delay[1] = DEFAULT_FRAME_DELAY_1;
+  si->debug.frame_delay[2] = DEFAULT_FRAME_DELAY_2;
+  si->debug.frame_delay[3] = DEFAULT_FRAME_DELAY_3;
+  si->debug.frame_delay[4] = DEFAULT_FRAME_DELAY_4;
+  si->debug.frame_delay[5] = DEFAULT_FRAME_DELAY_5;
+  si->debug.frame_delay[6] = DEFAULT_FRAME_DELAY_6;
+  si->debug.frame_delay[7] = DEFAULT_FRAME_DELAY_7;
+  si->debug.frame_delay[8] = DEFAULT_FRAME_DELAY_8;
+  si->debug.frame_delay[9] = DEFAULT_FRAME_DELAY_9;
+
+  si->debug.frame_delay_key[0] = DEFAULT_KEY_FRAME_DELAY_0;
+  si->debug.frame_delay_key[1] = DEFAULT_KEY_FRAME_DELAY_1;
+  si->debug.frame_delay_key[2] = DEFAULT_KEY_FRAME_DELAY_2;
+  si->debug.frame_delay_key[3] = DEFAULT_KEY_FRAME_DELAY_3;
+  si->debug.frame_delay_key[4] = DEFAULT_KEY_FRAME_DELAY_4;
+  si->debug.frame_delay_key[5] = DEFAULT_KEY_FRAME_DELAY_5;
+  si->debug.frame_delay_key[6] = DEFAULT_KEY_FRAME_DELAY_6;
+  si->debug.frame_delay_key[7] = DEFAULT_KEY_FRAME_DELAY_7;
+  si->debug.frame_delay_key[8] = DEFAULT_KEY_FRAME_DELAY_8;
+  si->debug.frame_delay_key[9] = DEFAULT_KEY_FRAME_DELAY_9;
+
+  si->debug.frame_delay_use_mod_key = DEFAULT_FRAME_DELAY_USE_MOD_KEY;
+  si->debug.frame_delay_game_only   = DEFAULT_FRAME_DELAY_GAME_ONLY;
+
+  si->debug.show_frames_per_second = FALSE;
+
   si->options.verbose = FALSE;
 
-#if defined(CREATE_SPECIAL_EDITION_RND_JUE)
-  si->toons = FALSE;
-  si->handicap = FALSE;
+#if defined(PLATFORM_ANDROID)
   si->fullscreen = TRUE;
-  si->override_level_graphics = AUTO;
-  si->override_level_sounds = AUTO;
-  si->override_level_music = AUTO;
 #endif
 }
 
@@ -9714,6 +8562,60 @@ static void setSetupInfoToDefaults_EditorCascade(struct SetupInfo *si)
   si->editor_cascade.el_dynamic                = FALSE;
 }
 
+#define MAX_HIDE_SETUP_TOKEN_SIZE              20
+
+static char *getHideSetupToken(void *setup_value)
+{
+  static char hide_setup_token[MAX_HIDE_SETUP_TOKEN_SIZE];
+
+  if (setup_value != NULL)
+    snprintf(hide_setup_token, MAX_HIDE_SETUP_TOKEN_SIZE, "%p", setup_value);
+
+  return hide_setup_token;
+}
+
+static void setHideSetupEntry(void *setup_value_raw)
+{
+  /* !!! DIRTY WORKAROUND; TO BE FIXED AFTER THE MM ENGINE RELEASE !!! */
+  void *setup_value = setup_value_raw - (void *)&si + (void *)&setup;
+
+  char *hide_setup_token = getHideSetupToken(setup_value);
+
+  if (setup_value != NULL)
+    setHashEntry(hide_setup_hash, hide_setup_token, "");
+}
+
+boolean hideSetupEntry(void *setup_value)
+{
+  char *hide_setup_token = getHideSetupToken(setup_value);
+
+  return (setup_value != NULL &&
+         getHashEntry(hide_setup_hash, hide_setup_token) != NULL);
+}
+
+static void setSetupInfoFromTokenText(SetupFileHash *setup_file_hash,
+                                     struct TokenInfo *token_info,
+                                     int token_nr, char *token_text)
+{
+  char *token_hide_text = getStringCat2(token_text, ".hide");
+  char *token_hide_value = getHashEntry(setup_file_hash, token_hide_text);
+
+  /* set the value of this setup option in the setup option structure */
+  setSetupInfo(token_info, token_nr, getHashEntry(setup_file_hash, token_text));
+
+  /* check if this setup option should be hidden in the setup menu */
+  if (token_hide_value != NULL && get_boolean_from_string(token_hide_value))
+    setHideSetupEntry(token_info[token_nr].value);
+}
+
+static void setSetupInfoFromTokenInfo(SetupFileHash *setup_file_hash,
+                                     struct TokenInfo *token_info,
+                                     int token_nr)
+{
+  setSetupInfoFromTokenText(setup_file_hash, token_info, token_nr,
+                           token_info[token_nr].text);
+}
+
 static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
 {
   int i, pnr;
@@ -9721,25 +8623,25 @@ static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
   if (!setup_file_hash)
     return;
 
+  if (hide_setup_hash == NULL)
+    hide_setup_hash = newSetupFileHash();
+
   /* global setup */
   si = setup;
   for (i = 0; i < NUM_GLOBAL_SETUP_TOKENS; i++)
-    setSetupInfo(global_setup_tokens, i,
-                getHashEntry(setup_file_hash, global_setup_tokens[i].text));
+    setSetupInfoFromTokenInfo(setup_file_hash, global_setup_tokens, i);
   setup = si;
 
   /* editor setup */
   sei = setup.editor;
   for (i = 0; i < NUM_EDITOR_SETUP_TOKENS; i++)
-    setSetupInfo(editor_setup_tokens, i,
-                getHashEntry(setup_file_hash,editor_setup_tokens[i].text));
+    setSetupInfoFromTokenInfo(setup_file_hash, editor_setup_tokens, i);
   setup.editor = sei;
 
   /* shortcut setup */
   ssi = setup.shortcut;
   for (i = 0; i < NUM_SHORTCUT_SETUP_TOKENS; i++)
-    setSetupInfo(shortcut_setup_tokens, i,
-                getHashEntry(setup_file_hash,shortcut_setup_tokens[i].text));
+    setSetupInfoFromTokenInfo(setup_file_hash, shortcut_setup_tokens, i);
   setup.shortcut = ssi;
 
   /* player setup */
@@ -9755,8 +8657,8 @@ static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
       char full_token[100];
 
       sprintf(full_token, "%s%s", prefix, player_setup_tokens[i].text);
-      setSetupInfo(player_setup_tokens, i,
-                  getHashEntry(setup_file_hash, full_token));
+      setSetupInfoFromTokenText(setup_file_hash, player_setup_tokens, i,
+                               full_token);
     }
     setup.input[pnr] = sii;
   }
@@ -9764,15 +8666,25 @@ static void decodeSetupFileHash(SetupFileHash *setup_file_hash)
   /* system setup */
   syi = setup.system;
   for (i = 0; i < NUM_SYSTEM_SETUP_TOKENS; i++)
-    setSetupInfo(system_setup_tokens, i,
-                getHashEntry(setup_file_hash, system_setup_tokens[i].text));
+    setSetupInfoFromTokenInfo(setup_file_hash, system_setup_tokens, i);
   setup.system = syi;
 
+  /* internal setup */
+  sxi = setup.internal;
+  for (i = 0; i < NUM_INTERNAL_SETUP_TOKENS; i++)
+    setSetupInfoFromTokenInfo(setup_file_hash, internal_setup_tokens, i);
+  setup.internal = sxi;
+
+  /* debug setup */
+  sdi = setup.debug;
+  for (i = 0; i < NUM_DEBUG_SETUP_TOKENS; i++)
+    setSetupInfoFromTokenInfo(setup_file_hash, debug_setup_tokens, i);
+  setup.debug = sdi;
+
   /* options setup */
   soi = setup.options;
   for (i = 0; i < NUM_OPTIONS_SETUP_TOKENS; i++)
-    setSetupInfo(options_setup_tokens, i,
-                getHashEntry(setup_file_hash, options_setup_tokens[i].text));
+    setSetupInfoFromTokenInfo(setup_file_hash, options_setup_tokens, i);
   setup.options = soi;
 }
 
@@ -9792,43 +8704,62 @@ static void decodeSetupFileHash_EditorCascade(SetupFileHash *setup_file_hash)
   setup.editor_cascade = seci;
 }
 
-void LoadSetup()
+void LoadSetupFromFilename(char *filename)
 {
-  char *filename = getSetupFilename();
-  SetupFileHash *setup_file_hash = NULL;
-
-  /* always start with reliable default values */
-  setSetupInfoToDefaults(&setup);
-
-  setup_file_hash = loadSetupFileHash(filename);
+  SetupFileHash *setup_file_hash = loadSetupFileHash(filename);
 
   if (setup_file_hash)
   {
-    char *player_name_new;
-
-    checkSetupFileHashIdentifier(setup_file_hash, filename,getCookie("SETUP"));
     decodeSetupFileHash(setup_file_hash);
 
     freeSetupFileHash(setup_file_hash);
+  }
+  else
+  {
+    Error(ERR_DEBUG, "using default setup values");
+  }
+}
 
-    /* needed to work around problems with fixed length strings */
-    player_name_new = get_corrected_login_name(setup.player_name);
-    free(setup.player_name);
-    setup.player_name = player_name_new;
+static void LoadSetup_SpecialPostProcessing()
+{
+  char *player_name_new;
 
-    /* "scroll_delay: on(3) / off(0)" was replaced by scroll delay value */
-    if (setup.scroll_delay == FALSE)
-    {
-      setup.scroll_delay_value = MIN_SCROLL_DELAY;
-      setup.scroll_delay = TRUE;                       /* now always "on" */
-    }
+  /* needed to work around problems with fixed length strings */
+  player_name_new = get_corrected_login_name(setup.player_name);
+  free(setup.player_name);
+  setup.player_name = player_name_new;
 
-    /* make sure that scroll delay value stays inside valid range */
-    setup.scroll_delay_value =
-      MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
+  /* "scroll_delay: on(3) / off(0)" was replaced by scroll delay value */
+  if (setup.scroll_delay == FALSE)
+  {
+    setup.scroll_delay_value = MIN_SCROLL_DELAY;
+    setup.scroll_delay = TRUE;                 /* now always "on" */
   }
-  else
-    Error(ERR_WARN, "using default setup values");
+
+  /* make sure that scroll delay value stays inside valid range */
+  setup.scroll_delay_value =
+    MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
+}
+
+void LoadSetup()
+{
+  char *filename;
+
+  /* always start with reliable default values */
+  setSetupInfoToDefaults(&setup);
+
+  /* try to load setup values from default setup file */
+  filename = getDefaultSetupFilename();
+
+  if (fileExists(filename))
+    LoadSetupFromFilename(filename);
+
+  /* try to load setup values from user setup file */
+  filename = getSetupFilename();
+
+  LoadSetupFromFilename(filename);
+
+  LoadSetup_SpecialPostProcessing();
 }
 
 void LoadSetup_EditorCascade()
@@ -9843,7 +8774,6 @@ void LoadSetup_EditorCascade()
 
   if (setup_file_hash)
   {
-    checkSetupFileHashIdentifier(setup_file_hash, filename,getCookie("SETUP"));
     decodeSetupFileHash_EditorCascade(setup_file_hash);
 
     freeSetupFileHash(setup_file_hash);
@@ -9852,6 +8782,55 @@ void LoadSetup_EditorCascade()
   free(filename);
 }
 
+static void addGameControllerMappingToHash(SetupFileHash *mappings_hash,
+                                          char *mapping_line)
+{
+  char mapping_guid[MAX_LINE_LEN];
+  char *mapping_start, *mapping_end;
+
+  // get GUID from game controller mapping line: copy complete line
+  strncpy(mapping_guid, mapping_line, MAX_LINE_LEN - 1);
+  mapping_guid[MAX_LINE_LEN - 1] = '\0';
+
+  // get GUID from game controller mapping line: cut after GUID part
+  mapping_start = strchr(mapping_guid, ',');
+  if (mapping_start != NULL)
+    *mapping_start = '\0';
+
+  // cut newline from game controller mapping line
+  mapping_end = strchr(mapping_line, '\n');
+  if (mapping_end != NULL)
+    *mapping_end = '\0';
+
+  // add mapping entry to game controller mappings hash
+  setHashEntry(mappings_hash, mapping_guid, mapping_line);
+}
+
+static void LoadSetup_ReadGameControllerMappings(SetupFileHash *mappings_hash,
+                                                char *filename)
+{
+  FILE *file;
+
+  if (!(file = fopen(filename, MODE_READ)))
+  {
+    Error(ERR_WARN, "cannot read game controller mappings file '%s'", filename);
+
+    return;
+  }
+
+  while (!feof(file))
+  {
+    char line[MAX_LINE_LEN];
+
+    if (!fgets(line, MAX_LINE_LEN, file))
+      break;
+
+    addGameControllerMappingToHash(mappings_hash, line);
+  }
+
+  fclose(file);
+}
+
 void SaveSetup()
 {
   char *filename = getSetupFilename();
@@ -9866,9 +8845,7 @@ void SaveSetup()
     return;
   }
 
-  fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
-                                              getCookie("SETUP")));
-  fprintf(file, "\n");
+  fprintFileHeader(file, SETUP_FILENAME);
 
   /* global setup */
   si = setup;
@@ -9877,7 +8854,8 @@ void SaveSetup()
     /* just to make things nicer :) */
     if (i == SETUP_TOKEN_PLAYER_NAME + 1 ||
        i == SETUP_TOKEN_GRAPHICS_SET ||
-       i == SETUP_TOKEN_VOLUME_SIMPLE)
+       i == SETUP_TOKEN_VOLUME_SIMPLE ||
+       i == SETUP_TOKEN_TOUCH_CONTROL_TYPE)
       fprintf(file, "\n");
 
     fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i));
@@ -9914,6 +8892,15 @@ void SaveSetup()
   for (i = 0; i < NUM_SYSTEM_SETUP_TOKENS; i++)
     fprintf(file, "%s\n", getSetupLine(system_setup_tokens, "", i));
 
+  /* internal setup */
+  /* (internal setup values not saved to user setup file) */
+
+  /* debug setup */
+  sdi = setup.debug;
+  fprintf(file, "\n");
+  for (i = 0; i < NUM_DEBUG_SETUP_TOKENS; i++)
+    fprintf(file, "%s\n", getSetupLine(debug_setup_tokens, "", i));
+
   /* options setup */
   soi = setup.options;
   fprintf(file, "\n");
@@ -9940,12 +8927,9 @@ void SaveSetup_EditorCascade()
     return;
   }
 
-  fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
-                                              getCookie("SETUP")));
-  fprintf(file, "\n");
+  fprintFileHeader(file, EDITORCASCADE_FILENAME);
 
   seci = setup.editor_cascade;
-  fprintf(file, "\n");
   for (i = 0; i < NUM_EDITOR_CASCADE_SETUP_TOKENS; i++)
     fprintf(file, "%s\n", getSetupLine(editor_cascade_setup_tokens, "", i));
 
@@ -9956,6 +8940,47 @@ void SaveSetup_EditorCascade()
   free(filename);
 }
 
+static void SaveSetup_WriteGameControllerMappings(SetupFileHash *mappings_hash,
+                                                 char *filename)
+{
+  FILE *file;
+
+  if (!(file = fopen(filename, MODE_WRITE)))
+  {
+    Error(ERR_WARN, "cannot write game controller mappings file '%s'",filename);
+
+    return;
+  }
+
+  BEGIN_HASH_ITERATION(mappings_hash, itr)
+  {
+    fprintf(file, "%s\n", HASH_ITERATION_VALUE(itr));
+  }
+  END_HASH_ITERATION(mappings_hash, itr)
+
+  fclose(file);
+}
+
+void SaveSetup_AddGameControllerMapping(char *mapping)
+{
+  char *filename = getPath2(getSetupDir(), GAMECONTROLLER_BASENAME);
+  SetupFileHash *mappings_hash = newSetupFileHash();
+
+  InitUserDataDirectory();
+
+  // load existing personal game controller mappings
+  LoadSetup_ReadGameControllerMappings(mappings_hash, filename);
+
+  // add new mapping to personal game controller mappings
+  addGameControllerMappingToHash(mappings_hash, mapping);
+
+  // save updated personal game controller mappings
+  SaveSetup_WriteGameControllerMappings(mappings_hash, filename);
+
+  freeSetupFileHash(mappings_hash);
+  free(filename);
+}
+
 void LoadCustomElementDescriptions()
 {
   char *filename = getCustomArtworkConfigFilename(ARTWORK_TYPE_GRAPHICS);
@@ -9990,19 +9015,10 @@ void LoadCustomElementDescriptions()
 
 static int getElementFromToken(char *token)
 {
-#if 1
   char *value = getHashEntry(element_token_hash, token);
 
   if (value != NULL)
     return atoi(value);
-#else
-  int i;
-
-  /* !!! OPTIMIZE THIS BY USING HASH !!! */
-  for (i = 0; i < MAX_NUM_ELEMENTS; i++)
-    if (strEqual(token, element_info[i].token_name))
-      return i;
-#endif
 
   Error(ERR_WARN, "unknown element token '%s'", token);
 
@@ -10020,25 +9036,8 @@ static int get_token_parameter_value(char *token, char *value_raw)
   if (suffix == NULL)
     suffix = token;
 
-#if 1
   if (strEqual(suffix, ".element"))
     return getElementFromToken(value_raw);
-#endif
-
-#if 0
-  if (strncmp(suffix, ".font", 5) == 0)
-  {
-    int i;
-
-    /* !!! OPTIMIZE THIS BY USING HASH !!! */
-    for (i = 0; i < NUM_FONTS; i++)
-      if (strEqual(value_raw, font_info[i].token_name))
-       return i;
-
-    /* if font not found, use reliable default value */
-    return FONT_INITIAL_1;
-  }
-#endif
 
   /* !!! USE CORRECT VALUE TYPE (currently works also for TYPE_BOOLEAN) !!! */
   return get_parameter_value(value_raw, suffix, TYPE_INTEGER);
@@ -10046,24 +9045,8 @@ static int get_token_parameter_value(char *token, char *value_raw)
 
 void InitMenuDesignSettings_Static()
 {
-#if 0
-  static SetupFileHash *image_config_hash = NULL;
-#endif
   int i;
 
-#if 0
-  if (image_config_hash == NULL)
-  {
-    image_config_hash = newSetupFileHash();
-
-    for (i = 0; image_config[i].token != NULL; i++)
-      setHashEntry(image_config_hash,
-                  image_config[i].token,
-                  image_config[i].value);
-  }
-#endif
-
-#if 1
   /* always start with reliable default values from static default config */
   for (i = 0; image_config_vars[i].token != NULL; i++)
   {
@@ -10073,19 +9056,6 @@ void InitMenuDesignSettings_Static()
       *image_config_vars[i].value =
        get_token_parameter_value(image_config_vars[i].token, value);
   }
-
-#else
-
-  int j;
-
-  /* always start with reliable default values from static default config */
-  for (i = 0; image_config_vars[i].token != NULL; i++)
-    for (j = 0; image_config[j].token != NULL; j++)
-      if (strEqual(image_config_vars[i].token, image_config[j].token))
-       *image_config_vars[i].value =
-         get_token_parameter_value(image_config_vars[i].token,
-                                   image_config[j].value);
-#endif
 }
 
 static void InitMenuDesignSettings_SpecialPreProcessing()
@@ -10096,6 +9066,39 @@ static void InitMenuDesignSettings_SpecialPreProcessing()
 
   /* special case: initialize "ARG_DEFAULT" values in static default config */
   /* (e.g., initialize "[titlemessage].fade_mode" from "[title].fade_mode") */
+  titlescreen_initial_first_default.fade_mode  =
+    title_initial_first_default.fade_mode;
+  titlescreen_initial_first_default.fade_delay =
+    title_initial_first_default.fade_delay;
+  titlescreen_initial_first_default.post_delay =
+    title_initial_first_default.post_delay;
+  titlescreen_initial_first_default.auto_delay =
+    title_initial_first_default.auto_delay;
+  titlescreen_first_default.fade_mode  = title_first_default.fade_mode;
+  titlescreen_first_default.fade_delay = title_first_default.fade_delay;
+  titlescreen_first_default.post_delay = title_first_default.post_delay;
+  titlescreen_first_default.auto_delay = title_first_default.auto_delay;
+  titlemessage_initial_first_default.fade_mode  =
+    title_initial_first_default.fade_mode;
+  titlemessage_initial_first_default.fade_delay =
+    title_initial_first_default.fade_delay;
+  titlemessage_initial_first_default.post_delay =
+    title_initial_first_default.post_delay;
+  titlemessage_initial_first_default.auto_delay =
+    title_initial_first_default.auto_delay;
+  titlemessage_first_default.fade_mode  = title_first_default.fade_mode;
+  titlemessage_first_default.fade_delay = title_first_default.fade_delay;
+  titlemessage_first_default.post_delay = title_first_default.post_delay;
+  titlemessage_first_default.auto_delay = title_first_default.auto_delay;
+
+  titlescreen_initial_default.fade_mode  = title_initial_default.fade_mode;
+  titlescreen_initial_default.fade_delay = title_initial_default.fade_delay;
+  titlescreen_initial_default.post_delay = title_initial_default.post_delay;
+  titlescreen_initial_default.auto_delay = title_initial_default.auto_delay;
+  titlescreen_default.fade_mode  = title_default.fade_mode;
+  titlescreen_default.fade_delay = title_default.fade_delay;
+  titlescreen_default.post_delay = title_default.post_delay;
+  titlescreen_default.auto_delay = title_default.auto_delay;
   titlemessage_initial_default.fade_mode  = title_initial_default.fade_mode;
   titlemessage_initial_default.fade_delay = title_initial_default.fade_delay;
   titlemessage_initial_default.post_delay = title_initial_default.post_delay;
@@ -10109,6 +9112,13 @@ static void InitMenuDesignSettings_SpecialPreProcessing()
   /* (e.g., init "titlemessage_1.fade_mode" from "[titlemessage].fade_mode") */
   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
   {
+    titlescreen_initial_first[i] = titlescreen_initial_first_default;
+    titlescreen_first[i] = titlescreen_first_default;
+    titlemessage_initial_first[i] = titlemessage_initial_first_default;
+    titlemessage_first[i] = titlemessage_first_default;
+
+    titlescreen_initial[i] = titlescreen_initial_default;
+    titlescreen[i] = titlescreen_default;
     titlemessage_initial[i] = titlemessage_initial_default;
     titlemessage[i] = titlemessage_default;
   }
@@ -10117,31 +9127,112 @@ static void InitMenuDesignSettings_SpecialPreProcessing()
   /* (eg, init "menu.enter_screen.SCORES.xyz" from "menu.enter_screen.xyz") */
   for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
   {
+    if (i == GFX_SPECIAL_ARG_TITLE)    /* title values already initialized */
+      continue;
+
     menu.enter_screen[i] = menu.enter_screen[GFX_SPECIAL_ARG_DEFAULT];
     menu.leave_screen[i] = menu.leave_screen[GFX_SPECIAL_ARG_DEFAULT];
+    menu.next_screen[i]  = menu.next_screen[GFX_SPECIAL_ARG_DEFAULT];
   }
 
   /* special case: initialize "ARG_DEFAULT" values in static default config */
   /* (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz") */
   for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
   {
+    viewport.window[i]    = viewport.window[GFX_SPECIAL_ARG_DEFAULT];
     viewport.playfield[i] = viewport.playfield[GFX_SPECIAL_ARG_DEFAULT];
-    viewport.door_1[i] = viewport.door_1[GFX_SPECIAL_ARG_DEFAULT];
-    if (i != GFX_SPECIAL_ARG_EDITOR)   /* editor value already initialized */
-      viewport.door_2[i] = viewport.door_2[GFX_SPECIAL_ARG_DEFAULT];
+    viewport.door_1[i]    = viewport.door_1[GFX_SPECIAL_ARG_DEFAULT];
+
+    if (i == GFX_SPECIAL_ARG_EDITOR)   /* editor values already initialized */
+      continue;
+
+    viewport.door_2[i] = viewport.door_2[GFX_SPECIAL_ARG_DEFAULT];
   }
 }
 
 static void InitMenuDesignSettings_SpecialPostProcessing()
 {
+  static struct
+  {
+    struct XY *dst, *src;
+  }
+  game_buttons_xy[] =
+  {
+    { &game.button.save,       &game.button.stop       },
+    { &game.button.pause2,     &game.button.pause      },
+    { &game.button.load,       &game.button.play       },
+    { &game.button.undo,       &game.button.stop       },
+    { &game.button.redo,       &game.button.play       },
+
+    { NULL,                    NULL                    }
+  };
+  int i;
+
   /* special case: initialize later added SETUP list size from LEVELS value */
   if (menu.list_size[GAME_MODE_SETUP] == -1)
     menu.list_size[GAME_MODE_SETUP] = menu.list_size[GAME_MODE_LEVELS];
+
+  /* set default position for snapshot buttons to stop/pause/play buttons */
+  for (i = 0; game_buttons_xy[i].dst != NULL; i++)
+    if ((*game_buttons_xy[i].dst).x == -1 &&
+       (*game_buttons_xy[i].dst).y == -1)
+      *game_buttons_xy[i].dst = *game_buttons_xy[i].src;
+}
+
+static void InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics()
+{
+  static struct
+  {
+    struct XYTileSize *dst, *src;
+    int graphic;
+  }
+  editor_buttons_xy[] =
+  {
+    {
+      &editor.button.element_left,     &editor.palette.element_left,
+      IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT
+    },
+    {
+      &editor.button.element_middle,   &editor.palette.element_middle,
+      IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE
+    },
+    {
+      &editor.button.element_right,    &editor.palette.element_right,
+      IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT
+    },
+
+    { NULL,                    NULL                    }
+  };
+  int i;
+
+  /* set default position for element buttons to element graphics */
+  for (i = 0; editor_buttons_xy[i].dst != NULL; i++)
+  {
+    if ((*editor_buttons_xy[i].dst).x == -1 &&
+       (*editor_buttons_xy[i].dst).y == -1)
+    {
+      struct GraphicInfo *gd = &graphic_info[editor_buttons_xy[i].graphic];
+
+      gd->width = gd->height = editor_buttons_xy[i].src->tile_size;
+
+      *editor_buttons_xy[i].dst = *editor_buttons_xy[i].src;
+    }
+  }
 }
 
 static void LoadMenuDesignSettingsFromFilename(char *filename)
 {
+  static struct TitleFadingInfo tfi;
   static struct TitleMessageInfo tmi;
+  static struct TokenInfo title_tokens[] =
+  {
+    { TYPE_INTEGER,    &tfi.fade_mode,         ".fade_mode"            },
+    { TYPE_INTEGER,    &tfi.fade_delay,        ".fade_delay"           },
+    { TYPE_INTEGER,    &tfi.post_delay,        ".post_delay"           },
+    { TYPE_INTEGER,    &tfi.auto_delay,        ".auto_delay"           },
+
+    { -1,              NULL,                   NULL                    }
+  };
   static struct TokenInfo titlemessage_tokens[] =
   {
     { TYPE_INTEGER,    &tmi.x,                 ".x"                    },
@@ -10165,12 +9256,60 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
     { -1,              NULL,                   NULL                    }
   };
   static struct
+  {
+    struct TitleFadingInfo *info;
+    char *text;
+  }
+  title_info[] =
+  {
+    /* initialize first titles from "enter screen" definitions, if defined */
+    { &title_initial_first_default,    "menu.enter_screen.TITLE"       },
+    { &title_first_default,            "menu.enter_screen.TITLE"       },
+
+    /* initialize title screens from "next screen" definitions, if defined */
+    { &title_initial_default,          "menu.next_screen.TITLE"        },
+    { &title_default,                  "menu.next_screen.TITLE"        },
+
+    { NULL,                            NULL                            }
+  };
+  static struct
   {
     struct TitleMessageInfo *array;
     char *text;
   }
   titlemessage_arrays[] =
   {
+    /* initialize first titles from "enter screen" definitions, if defined */
+    { titlescreen_initial_first,       "menu.enter_screen.TITLE"       },
+    { titlescreen_first,               "menu.enter_screen.TITLE"       },
+    { titlemessage_initial_first,      "menu.enter_screen.TITLE"       },
+    { titlemessage_first,              "menu.enter_screen.TITLE"       },
+
+    /* initialize titles from "next screen" definitions, if defined */
+    { titlescreen_initial,             "menu.next_screen.TITLE"        },
+    { titlescreen,                     "menu.next_screen.TITLE"        },
+    { titlemessage_initial,            "menu.next_screen.TITLE"        },
+    { titlemessage,                    "menu.next_screen.TITLE"        },
+
+    /* overwrite titles with title definitions, if defined */
+    { titlescreen_initial_first,       "[title_initial]"               },
+    { titlescreen_first,               "[title]"                       },
+    { titlemessage_initial_first,      "[title_initial]"               },
+    { titlemessage_first,              "[title]"                       },
+
+    { titlescreen_initial,             "[title_initial]"               },
+    { titlescreen,                     "[title]"                       },
+    { titlemessage_initial,            "[title_initial]"               },
+    { titlemessage,                    "[title]"                       },
+
+    /* overwrite titles with title screen/message definitions, if defined */
+    { titlescreen_initial_first,       "[titlescreen_initial]"         },
+    { titlescreen_first,               "[titlescreen]"                 },
+    { titlemessage_initial_first,      "[titlemessage_initial]"        },
+    { titlemessage_first,              "[titlemessage]"                },
+
+    { titlescreen_initial,             "[titlescreen_initial]"         },
+    { titlescreen,                     "[titlescreen]"                 },
     { titlemessage_initial,            "[titlemessage_initial]"        },
     { titlemessage,                    "[titlemessage]"                },
 
@@ -10179,10 +9318,6 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
   SetupFileHash *setup_file_hash;
   int i, j, k;
 
-#if 0
-  printf("LoadMenuDesignSettings from file '%s' ...\n", filename);
-#endif
-
   if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
     return;
 
@@ -10215,6 +9350,14 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
       menu.draw_xoffset_info[i] = get_integer_from_string(value_1);
     if (value_2 != NULL)
       menu.draw_yoffset_info[i] = get_integer_from_string(value_2);
+
+    if (i == GFX_SPECIAL_ARG_INFO_ELEMENTS)
+    {
+      char *value_1 = getHashEntry(setup_file_hash, "menu.list_size.INFO");
+
+      if (value_1 != NULL)
+       menu.list_size_info[i] = get_integer_from_string(value_1);
+    }
   }
 
   /* special case: initialize with default values that may be overwritten */
@@ -10240,12 +9383,18 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
     char *token_4 = "menu.leave_screen.fade_mode";
     char *token_5 = "menu.leave_screen.fade_delay";
     char *token_6 = "menu.leave_screen.post_delay";
+    char *token_7 = "menu.next_screen.fade_mode";
+    char *token_8 = "menu.next_screen.fade_delay";
+    char *token_9 = "menu.next_screen.post_delay";
     char *value_1 = getHashEntry(setup_file_hash, token_1);
     char *value_2 = getHashEntry(setup_file_hash, token_2);
     char *value_3 = getHashEntry(setup_file_hash, token_3);
     char *value_4 = getHashEntry(setup_file_hash, token_4);
     char *value_5 = getHashEntry(setup_file_hash, token_5);
     char *value_6 = getHashEntry(setup_file_hash, token_6);
+    char *value_7 = getHashEntry(setup_file_hash, token_7);
+    char *value_8 = getHashEntry(setup_file_hash, token_8);
+    char *value_9 = getHashEntry(setup_file_hash, token_9);
 
     if (value_1 != NULL)
       menu.enter_screen[i].fade_mode = get_token_parameter_value(token_1,
@@ -10265,50 +9414,122 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
     if (value_6 != NULL)
       menu.leave_screen[i].post_delay = get_token_parameter_value(token_6,
                                                                  value_6);
+    if (value_7 != NULL)
+      menu.next_screen[i].fade_mode = get_token_parameter_value(token_7,
+                                                               value_7);
+    if (value_8 != NULL)
+      menu.next_screen[i].fade_delay = get_token_parameter_value(token_8,
+                                                                value_8);
+    if (value_9 != NULL)
+      menu.next_screen[i].post_delay = get_token_parameter_value(token_9,
+                                                                value_9);
   }
 
   /* special case: initialize with default values that may be overwritten */
   /* (eg, init "viewport.door_1.MAIN.xyz" from "viewport.door_1.xyz") */
   for (i = 0; i < NUM_SPECIAL_GFX_ARGS; i++)
   {
-    char *token_1 = "viewport.playfield.x";
-    char *token_2 = "viewport.playfield.y";
-    char *token_3 = "viewport.playfield.width";
-    char *token_4 = "viewport.playfield.height";
-    char *token_5 = "viewport.playfield.border_size";
-    char *token_6 = "viewport.door_1.x";
-    char *token_7 = "viewport.door_1.y";
-    char *token_8 = "viewport.door_2.x";
-    char *token_9 = "viewport.door_2.y";
-    char *value_1 = getHashEntry(setup_file_hash, token_1);
-    char *value_2 = getHashEntry(setup_file_hash, token_2);
-    char *value_3 = getHashEntry(setup_file_hash, token_3);
-    char *value_4 = getHashEntry(setup_file_hash, token_4);
-    char *value_5 = getHashEntry(setup_file_hash, token_5);
-    char *value_6 = getHashEntry(setup_file_hash, token_6);
-    char *value_7 = getHashEntry(setup_file_hash, token_7);
-    char *value_8 = getHashEntry(setup_file_hash, token_8);
-    char *value_9 = getHashEntry(setup_file_hash, token_9);
+    char *token_w1 = "viewport.window.width";
+    char *token_w2 = "viewport.window.height";
+    char *token_01 = "viewport.playfield.x";
+    char *token_02 = "viewport.playfield.y";
+    char *token_03 = "viewport.playfield.width";
+    char *token_04 = "viewport.playfield.height";
+    char *token_05 = "viewport.playfield.border_size";
+    char *token_06 = "viewport.door_1.x";
+    char *token_07 = "viewport.door_1.y";
+    char *token_08 = "viewport.door_1.width";
+    char *token_09 = "viewport.door_1.height";
+    char *token_10 = "viewport.door_1.border_size";
+    char *token_11 = "viewport.door_2.x";
+    char *token_12 = "viewport.door_2.y";
+    char *token_13 = "viewport.door_2.width";
+    char *token_14 = "viewport.door_2.height";
+    char *token_15 = "viewport.door_2.border_size";
+    char *value_w1 = getHashEntry(setup_file_hash, token_w1);
+    char *value_w2 = getHashEntry(setup_file_hash, token_w2);
+    char *value_01 = getHashEntry(setup_file_hash, token_01);
+    char *value_02 = getHashEntry(setup_file_hash, token_02);
+    char *value_03 = getHashEntry(setup_file_hash, token_03);
+    char *value_04 = getHashEntry(setup_file_hash, token_04);
+    char *value_05 = getHashEntry(setup_file_hash, token_05);
+    char *value_06 = getHashEntry(setup_file_hash, token_06);
+    char *value_07 = getHashEntry(setup_file_hash, token_07);
+    char *value_08 = getHashEntry(setup_file_hash, token_08);
+    char *value_09 = getHashEntry(setup_file_hash, token_09);
+    char *value_10 = getHashEntry(setup_file_hash, token_10);
+    char *value_11 = getHashEntry(setup_file_hash, token_11);
+    char *value_12 = getHashEntry(setup_file_hash, token_12);
+    char *value_13 = getHashEntry(setup_file_hash, token_13);
+    char *value_14 = getHashEntry(setup_file_hash, token_14);
+    char *value_15 = getHashEntry(setup_file_hash, token_15);
+
+    if (value_w1 != NULL)
+      viewport.window[i].width = get_token_parameter_value(token_w1, value_w1);
+    if (value_w2 != NULL)
+      viewport.window[i].height = get_token_parameter_value(token_w2, value_w2);
+    if (value_01 != NULL)
+      viewport.playfield[i].x = get_token_parameter_value(token_01, value_01);
+    if (value_02 != NULL)
+      viewport.playfield[i].y = get_token_parameter_value(token_02, value_02);
+    if (value_03 != NULL)
+      viewport.playfield[i].width = get_token_parameter_value(token_03,
+                                                             value_03);
+    if (value_04 != NULL)
+      viewport.playfield[i].height = get_token_parameter_value(token_04,
+                                                              value_04);
+    if (value_05 != NULL)
+      viewport.playfield[i].border_size = get_token_parameter_value(token_05,
+                                                                   value_05);
+    if (value_06 != NULL)
+      viewport.door_1[i].x = get_token_parameter_value(token_06, value_06);
+    if (value_07 != NULL)
+      viewport.door_1[i].y = get_token_parameter_value(token_07, value_07);
+    if (value_08 != NULL)
+      viewport.door_1[i].width = get_token_parameter_value(token_08, value_08);
+    if (value_09 != NULL)
+      viewport.door_1[i].height = get_token_parameter_value(token_09, value_09);
+    if (value_10 != NULL)
+      viewport.door_1[i].border_size = get_token_parameter_value(token_10,
+                                                                value_10);
+    if (value_11 != NULL)
+      viewport.door_2[i].x = get_token_parameter_value(token_11, value_11);
+    if (value_12 != NULL)
+      viewport.door_2[i].y = get_token_parameter_value(token_12, value_12);
+    if (value_13 != NULL)
+      viewport.door_2[i].width = get_token_parameter_value(token_13, value_13);
+    if (value_14 != NULL)
+      viewport.door_2[i].height = get_token_parameter_value(token_14, value_14);
+    if (value_15 != NULL)
+      viewport.door_1[i].border_size = get_token_parameter_value(token_15,
+                                                                value_15);
+  }
 
-    if (value_1 != NULL)
-      viewport.playfield[i].x = get_token_parameter_value(token_1, value_1);
-    if (value_2 != NULL)
-      viewport.playfield[i].y = get_token_parameter_value(token_2, value_2);
-    if (value_3 != NULL)
-      viewport.playfield[i].width = get_token_parameter_value(token_3, value_3);
-    if (value_4 != NULL)
-      viewport.playfield[i].height = get_token_parameter_value(token_4,value_4);
-    if (value_5 != NULL)
-      viewport.playfield[i].border_size = get_token_parameter_value(token_5,
-                                                                   value_5);
-    if (value_6 != NULL)
-      viewport.door_1[i].x = get_token_parameter_value(token_6, value_6);
-    if (value_7 != NULL)
-      viewport.door_1[i].y = get_token_parameter_value(token_7, value_7);
-    if (value_8 != NULL)
-      viewport.door_2[i].x = get_token_parameter_value(token_8, value_8);
-    if (value_9 != NULL)
-      viewport.door_2[i].y = get_token_parameter_value(token_9, value_9);
+  /* special case: initialize with default values that may be overwritten */
+  /* (e.g., init "[title].fade_mode" from "menu.next_screen.TITLE.fade_mode") */
+  for (i = 0; title_info[i].info != NULL; i++)
+  {
+    struct TitleFadingInfo *info = title_info[i].info;
+    char *base_token = title_info[i].text;
+
+    for (j = 0; title_tokens[j].type != -1; j++)
+    {
+      char *token = getStringCat2(base_token, title_tokens[j].text);
+      char *value = getHashEntry(setup_file_hash, token);
+
+      if (value != NULL)
+      {
+       int parameter_value = get_token_parameter_value(token, value);
+
+       tfi = *info;
+
+       *(int *)title_tokens[j].value = (int)parameter_value;
+
+       *info = tfi;
+      }
+
+      free(token);
+    }
   }
 
   /* special case: initialize with default values that may be overwritten */
@@ -10332,9 +9553,9 @@ static void LoadMenuDesignSettingsFromFilename(char *filename)
          tmi = array[k];
 
          if (titlemessage_tokens[j].type == TYPE_INTEGER)
-           *(boolean *)titlemessage_tokens[j].value = (boolean)parameter_value;
-         else
            *(int     *)titlemessage_tokens[j].value = (int)parameter_value;
+         else
+           *(boolean *)titlemessage_tokens[j].value = (boolean)parameter_value;
 
          array[k] = tmi;
        }
@@ -10365,11 +9586,7 @@ void LoadMenuDesignSettings()
   InitMenuDesignSettings_Static();
   InitMenuDesignSettings_SpecialPreProcessing();
 
-#if 1
   if (!GFX_OVERRIDE_ARTWORK(ARTWORK_TYPE_GRAPHICS))
-#else
-  if (!SETUP_OVERRIDE_ARTWORK(setup, ARTWORK_TYPE_GRAPHICS))
-#endif
   {
     /* first look for special settings configured in level series config */
     filename_base = getCustomArtworkLevelConfigFilename(ARTWORK_TYPE_GRAPHICS);
@@ -10386,6 +9603,11 @@ void LoadMenuDesignSettings()
   InitMenuDesignSettings_SpecialPostProcessing();
 }
 
+void LoadMenuDesignSettings_AfterGraphics()
+{
+  InitMenuDesignSettings_SpecialPostProcessing_AfterGraphics();
+}
+
 void LoadUserDefinedEditorElementList(int **elements, int *num_elements)
 {
   char *filename = getEditorSetupFilename();
@@ -10508,10 +9730,6 @@ static struct MusicFileInfo *get_music_file_info_ext(char *basename, int music,
     *strrchr(filename_prefix, '.') = '\0';
   filename_info = getStringCat2(filename_prefix, ".txt");
 
-#if 0
-  printf("trying to load file '%s'...\n", filename_info);
-#endif
-
   if (fileExists(filename_info))
     setup_file_hash = loadSetupFileHash(filename_info);
 
@@ -10525,10 +9743,6 @@ static struct MusicFileInfo *get_music_file_info_ext(char *basename, int music,
     filename_prefix = getStringCopy(filename_music);
     filename_info = getStringCat2(filename_prefix, ".txt");
 
-#if 0
-    printf("trying to load file '%s'...\n", filename_info);
-#endif
-
     if (fileExists(filename_info))
       setup_file_hash = loadSetupFileHash(filename_info);
 
@@ -10591,8 +9805,6 @@ static boolean sound_info_listed(struct MusicFileInfo *list, char *basename)
   return music_info_listed_ext(list, basename, TRUE);
 }
 
-#if 1
-
 void LoadMusicInfo()
 {
   char *music_directory = getCustomMusicDirectory();
@@ -10642,17 +9854,10 @@ void LoadMusicInfo()
     if (!FileIsMusic(music->filename))
       continue;
 
-#if 0
-    printf("::: -> '%s' (configured)\n", music->filename);
-#endif
-
     if (!music_info_listed(music_file_info, music->filename))
     {
       *new = get_music_file_info(music->filename, i);
-#if 0
-      if (*new != NULL)
-       printf(":1: adding '%s' ['%s'] ...\n", (*new)->title, music->filename);
-#endif
+
       if (*new != NULL)
        new = &(*new)->next;
     }
@@ -10688,172 +9893,13 @@ void LoadMusicInfo()
     if (music_already_used)
       continue;
 
-    if (!FileIsMusic(basename))
+    if (!FileIsMusic(dir_entry->filename))
       continue;
 
-#if 0
-    printf("::: -> '%s' (found in directory)\n", basename);
-#endif
-
     if (!music_info_listed(music_file_info, basename))
     {
       *new = get_music_file_info(basename, MAP_NOCONF_MUSIC(num_music_noconf));
-#if 0
-      if (*new != NULL)
-       printf(":2: adding '%s' ['%s'] ...\n", (*new)->title, basename);
-#endif
-      if (*new != NULL)
-       new = &(*new)->next;
-    }
-
-    num_music_noconf++;
-  }
-
-  closeDirectory(dir);
-
-  for (i = 0; i < num_sounds; i++)
-  {
-    sound = getSoundListEntry(i);
-
-    if (sound->filename == NULL)
-      continue;
-
-    if (strEqual(sound->filename, UNDEFINED_FILENAME))
-      continue;
-
-    /* a configured file may be not recognized as sound */
-    if (!FileIsSound(sound->filename))
-      continue;
-
-#if 0
-    printf("::: -> '%s' (configured)\n", sound->filename);
-#endif
-
-    if (!sound_info_listed(music_file_info, sound->filename))
-    {
-      *new = get_sound_file_info(sound->filename, i);
-      if (*new != NULL)
-       new = &(*new)->next;
-    }
-  }
-
-#if 0
-  for (next = music_file_info; next != NULL; next = next->next)
-    printf("::: title == '%s'\n", next->title);
-#endif
-}
-
-#else
-
-void LoadMusicInfo()
-{
-  char *music_directory = getCustomMusicDirectory();
-  int num_music = getMusicListSize();
-  int num_music_noconf = 0;
-  int num_sounds = getSoundListSize();
-  DIR *dir;
-  struct dirent *dir_entry;
-  struct FileInfo *music, *sound;
-  struct MusicFileInfo *next, **new;
-  int i;
-
-  while (music_file_info != NULL)
-  {
-    next = music_file_info->next;
-
-    checked_free(music_file_info->basename);
-
-    checked_free(music_file_info->title_header);
-    checked_free(music_file_info->artist_header);
-    checked_free(music_file_info->album_header);
-    checked_free(music_file_info->year_header);
-
-    checked_free(music_file_info->title);
-    checked_free(music_file_info->artist);
-    checked_free(music_file_info->album);
-    checked_free(music_file_info->year);
-
-    free(music_file_info);
-
-    music_file_info = next;
-  }
-
-  new = &music_file_info;
-
-  for (i = 0; i < num_music; i++)
-  {
-    music = getMusicListEntry(i);
-
-    if (music->filename == NULL)
-      continue;
-
-    if (strEqual(music->filename, UNDEFINED_FILENAME))
-      continue;
-
-    /* a configured file may be not recognized as music */
-    if (!FileIsMusic(music->filename))
-      continue;
-
-#if 0
-    printf("::: -> '%s' (configured)\n", music->filename);
-#endif
-
-    if (!music_info_listed(music_file_info, music->filename))
-    {
-      *new = get_music_file_info(music->filename, i);
-#if 0
-      if (*new != NULL)
-       printf(":1: adding '%s' ['%s'] ...\n", (*new)->title, music->filename);
-#endif
-      if (*new != NULL)
-       new = &(*new)->next;
-    }
-  }
-
-  if ((dir = opendir(music_directory)) == NULL)
-  {
-    Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
-    return;
-  }
-
-  while ((dir_entry = readdir(dir)) != NULL)   /* loop until last dir entry */
-  {
-    char *basename = dir_entry->d_name;
-    boolean music_already_used = FALSE;
-    int i;
-
-    /* skip all music files that are configured in music config file */
-    for (i = 0; i < num_music; i++)
-    {
-      music = getMusicListEntry(i);
-
-      if (music->filename == NULL)
-       continue;
-
-      if (strEqual(basename, music->filename))
-      {
-       music_already_used = TRUE;
-       break;
-      }
-    }
-
-    if (music_already_used)
-      continue;
-
-    if (!FileIsMusic(basename))
-      continue;
-
-#if 0
-    printf("::: -> '%s' (found in directory)\n", basename);
-#endif
 
-    if (!music_info_listed(music_file_info, basename))
-    {
-      *new = get_music_file_info(basename, MAP_NOCONF_MUSIC(num_music_noconf));
-#if 0
-      if (*new != NULL)
-       printf(":2: adding '%s' ['%s'] ...\n", (*new)->title, basename);
-#endif
       if (*new != NULL)
        new = &(*new)->next;
     }
@@ -10861,7 +9907,7 @@ void LoadMusicInfo()
     num_music_noconf++;
   }
 
-  closedir(dir);
+  closeDirectory(dir);
 
   for (i = 0; i < num_sounds; i++)
   {
@@ -10877,10 +9923,6 @@ void LoadMusicInfo()
     if (!FileIsSound(sound->filename))
       continue;
 
-#if 0
-    printf("::: -> '%s' (configured)\n", sound->filename);
-#endif
-
     if (!sound_info_listed(music_file_info, sound->filename))
     {
       *new = get_sound_file_info(sound->filename, i);
@@ -10888,15 +9930,8 @@ void LoadMusicInfo()
        new = &(*new)->next;
     }
   }
-
-#if 0
-  for (next = music_file_info; next != NULL; next = next->next)
-    printf("::: title == '%s'\n", next->title);
-#endif
 }
 
-#endif
-
 void add_helpanim_entry(int element, int action, int direction, int delay,
                        int *num_list_entries)
 {
@@ -11210,15 +10245,15 @@ void ConvertLevels()
 
   convert_level_nr = convert_leveldir->first_level;
 
-  printf_line("=", 79);
-  printf("Converting levels\n");
-  printf_line("-", 79);
-  printf("Level series identifier: '%s'\n", convert_leveldir->identifier);
-  printf("Level series name:       '%s'\n", convert_leveldir->name);
-  printf("Level series author:     '%s'\n", convert_leveldir->author);
-  printf("Number of levels:        %d\n",   convert_leveldir->levels);
-  printf_line("=", 79);
-  printf("\n");
+  PrintLine("=", 79);
+  Print("Converting levels\n");
+  PrintLine("-", 79);
+  Print("Level series identifier: '%s'\n", convert_leveldir->identifier);
+  Print("Level series name:       '%s'\n", convert_leveldir->name);
+  Print("Level series author:     '%s'\n", convert_leveldir->author);
+  Print("Number of levels:        %d\n",   convert_leveldir->levels);
+  PrintLine("=", 79);
+  Print("\n");
 
   for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
     levels_failed[i] = FALSE;
@@ -11230,16 +10265,16 @@ void ConvertLevels()
 
     level_nr = convert_level_nr++;
 
-    printf("Level %03d: ", level_nr);
+    Print("Level %03d: ", level_nr);
 
     LoadLevel(level_nr);
-    if (level.no_valid_file)
+    if (level.no_level_file || level.no_valid_file)
     {
-      printf("(no level)\n");
+      Print("(no level)\n");
       continue;
     }
 
-    printf("converting level ... ");
+    Print("converting level ... ");
 
     level_filename = getDefaultLevelFilename(level_nr);
     new_level = !fileExists(level_filename);
@@ -11250,28 +10285,28 @@ void ConvertLevels()
 
       num_levels_converted++;
 
-      printf("converted.\n");
+      Print("converted.\n");
     }
     else
     {
       if (level_nr >= 0 && level_nr < MAX_NUM_CONVERT_LEVELS)
        levels_failed[level_nr] = TRUE;
 
-      printf("NOT CONVERTED -- LEVEL ALREADY EXISTS.\n");
+      Print("NOT CONVERTED -- LEVEL ALREADY EXISTS.\n");
     }
 
     num_levels_handled++;
   }
 
-  printf("\n");
-  printf_line("=", 79);
-  printf("Number of levels handled: %d\n", num_levels_handled);
-  printf("Number of levels converted: %d (%d%%)\n", num_levels_converted,
+  Print("\n");
+  PrintLine("=", 79);
+  Print("Number of levels handled: %d\n", num_levels_handled);
+  Print("Number of levels converted: %d (%d%%)\n", num_levels_converted,
         (num_levels_handled ?
          num_levels_converted * 100 / num_levels_handled : 0));
-  printf_line("-", 79);
-  printf("Summary (for automatic parsing by scripts):\n");
-  printf("LEVELDIR '%s', CONVERTED %d/%d (%d%%)",
+  PrintLine("-", 79);
+  Print("Summary (for automatic parsing by scripts):\n");
+  Print("LEVELDIR '%s', CONVERTED %d/%d (%d%%)",
         convert_leveldir->identifier, num_levels_converted,
         num_levels_handled,
         (num_levels_handled ?
@@ -11279,14 +10314,14 @@ void ConvertLevels()
 
   if (num_levels_handled != num_levels_converted)
   {
-    printf(", FAILED:");
+    Print(", FAILED:");
     for (i = 0; i < MAX_NUM_CONVERT_LEVELS; i++)
       if (levels_failed[i])
-       printf(" %03d", i);
+       Print(" %03d", i);
   }
 
-  printf("\n");
-  printf_line("=", 79);
+  Print("\n");
+  PrintLine("=", 79);
 
   CloseAllAndExit(0);
 }
@@ -11362,24 +10397,29 @@ void CreateLevelSketchImages()
 /* create and save images for custom and group elements (raw BMP format)     */
 /* ------------------------------------------------------------------------- */
 
-void CreateCustomElementImages()
+void CreateCustomElementImages(char *directory)
 {
 #if defined(TARGET_SDL)
-  char *filename = "graphics.classic/RocksCE.bmp";
-  Bitmap *bitmap;
+  char *src_basename = "RocksCE-template.ilbm";
+  char *dst_basename = "RocksCE.bmp";
+  char *src_filename = getPath2(directory, src_basename);
+  char *dst_filename = getPath2(directory, dst_basename);
   Bitmap *src_bitmap;
-  int dummy_graphic = IMG_CUSTOM_99;
+  Bitmap *bitmap;
   int yoffset_ce = 0;
   int yoffset_ge = (TILEY * NUM_CUSTOM_ELEMENTS / 16);
-  int src_x, src_y;
   int i;
 
+  SDLInitVideoDisplay();
+
+  ReCreateBitmap(&backbuffer, video.width, video.height);
+
+  src_bitmap = LoadImage(src_filename);
+
   bitmap = CreateBitmap(TILEX * 16 * 2,
                        TILEY * (NUM_CUSTOM_ELEMENTS + NUM_GROUP_ELEMENTS) / 16,
                        DEFAULT_DEPTH);
 
-  getFixedGraphicSource(dummy_graphic, 0, &src_bitmap, &src_x, &src_y);
-
   for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
   {
     int x = i % 16;
@@ -11445,18 +10485,11 @@ void CreateCustomElementImages()
     }
   }
 
-  if (SDL_SaveBMP(bitmap->surface, filename) != 0)
-    Error(ERR_EXIT, "cannot save CE graphics file '%s'", filename);
+  if (SDL_SaveBMP(bitmap->surface, dst_filename) != 0)
+    Error(ERR_EXIT, "cannot save CE graphics file '%s'", dst_filename);
 
   FreeBitmap(bitmap);
 
   CloseAllAndExit(0);
 #endif
 }
-
-#if 0
-void CreateLevelSketchImages_TEST()
-{
-  void CreateCustomElementImages()
-}
-#endif