fixed graphical bug with wrong draw buffer when using envelope request
[rocksndiamonds.git] / src / tools.c
index 651fbb164f3ec7a1efa72f87bddeab3ba0424829..099fd59de0c007460883d811c59d2997624745d1 100644 (file)
@@ -4,7 +4,7 @@
 // (c) 1995-2014 by Artsoft Entertainment
 //                         Holger Schemel
 //                 info@artsoft.org
-//                 http://www.artsoft.org/
+//                 https://www.artsoft.org/
 // ----------------------------------------------------------------------------
 // tools.c
 // ============================================================================
@@ -193,22 +193,6 @@ static char *print_if_not_empty(int element)
   return s;
 }
 
-int correctLevelPosX_EM(int lx)
-{
-  lx -= 1;
-  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
-
-  return lx;
-}
-
-int correctLevelPosY_EM(int ly)
-{
-  ly -= 1;
-  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
-
-  return ly;
-}
-
 int getFieldbufferOffsetX_RND(int dir, int pos)
 {
   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
@@ -297,7 +281,7 @@ static int getLevelFromScreenY_RND(int sy)
 
 static int getLevelFromScreenX_EM(int sx)
 {
-  int level_xsize = level.native_em_level->lev->width;
+  int level_xsize = level.native_em_level->cav->width;
   int full_xsize = level_xsize * TILESIZE_VAR;
 
   sx -= (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
@@ -307,14 +291,12 @@ static int getLevelFromScreenX_EM(int sx)
   int px = sx - SX;
   int lx = LEVELX((px + dx) / TILESIZE_VAR);
 
-  lx = correctLevelPosX_EM(lx);
-
   return lx;
 }
 
 static int getLevelFromScreenY_EM(int sy)
 {
-  int level_ysize = level.native_em_level->lev->height;
+  int level_ysize = level.native_em_level->cav->height;
   int full_ysize = level_ysize * TILESIZE_VAR;
 
   sy -= (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
@@ -324,8 +306,6 @@ static int getLevelFromScreenY_EM(int sy)
   int py = sy - SY;
   int ly = LEVELY((py + dy) / TILESIZE_VAR);
 
-  ly = correctLevelPosY_EM(ly);
-
   return ly;
 }
 
@@ -411,41 +391,51 @@ int getLevelFromScreenY(int y)
     return getLevelFromScreenY_RND(y);
 }
 
+int getScreenFieldSizeX(void)
+{
+  return (tape.playing ? tape.scr_fieldx : SCR_FIELDX);
+}
+
+int getScreenFieldSizeY(void)
+{
+  return (tape.playing ? tape.scr_fieldy : SCR_FIELDY);
+}
+
 void DumpTile(int x, int y)
 {
   int sx = SCREENX(x);
   int sy = SCREENY(y);
   char *token_name;
 
-  printf_line("-", 79);
-  printf("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
-  printf_line("-", 79);
+  Info("---");
+  Info("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)", sx, sy, x, y);
+  Info("---");
 
   if (!IN_LEV_FIELD(x, y))
   {
-    printf("(not in level field)\n");
-    printf("\n");
+    Info("(not in level field)");
+    Info("");
 
     return;
   }
 
-  token_name = element_info[Feld[x][y]].token_name;
+  token_name = element_info[Tile[x][y]].token_name;
 
-  printf("  Feld:        %d\t['%s']\n", Feld[x][y], token_name);
-  printf("  Back:        %s\n", print_if_not_empty(Back[x][y]));
-  printf("  Store:       %s\n", print_if_not_empty(Store[x][y]));
-  printf("  Store2:      %s\n", print_if_not_empty(Store2[x][y]));
-  printf("  StorePlayer: %s\n", print_if_not_empty(StorePlayer[x][y]));
-  printf("  MovPos:      %d\n", MovPos[x][y]);
-  printf("  MovDir:      %d\n", MovDir[x][y]);
-  printf("  MovDelay:    %d\n", MovDelay[x][y]);
-  printf("  ChangeDelay: %d\n", ChangeDelay[x][y]);
-  printf("  CustomValue: %d\n", CustomValue[x][y]);
-  printf("  GfxElement:  %d\n", GfxElement[x][y]);
-  printf("  GfxAction:   %d\n", GfxAction[x][y]);
-  printf("  GfxFrame:    %d [%d]\n", GfxFrame[x][y], FrameCounter);
-  printf("  Player x/y:  %d, %d\n", local_player->jx, local_player->jy);
-  printf("\n");
+  Info("Tile:        %d\t['%s']",      Tile[x][y], token_name);
+  Info("Back:        %s",              print_if_not_empty(Back[x][y]));
+  Info("Store:       %s",              print_if_not_empty(Store[x][y]));
+  Info("Store2:      %s",              print_if_not_empty(Store2[x][y]));
+  Info("StorePlayer: %s",              print_if_not_empty(StorePlayer[x][y]));
+  Info("MovPos:      %d",              MovPos[x][y]);
+  Info("MovDir:      %d",              MovDir[x][y]);
+  Info("MovDelay:    %d",              MovDelay[x][y]);
+  Info("ChangeDelay: %d",              ChangeDelay[x][y]);
+  Info("CustomValue: %d",              CustomValue[x][y]);
+  Info("GfxElement:  %d",              GfxElement[x][y]);
+  Info("GfxAction:   %d",              GfxAction[x][y]);
+  Info("GfxFrame:    %d [%d]",         GfxFrame[x][y], FrameCounter);
+  Info("Player x/y:  %d, %d",          local_player->jx, local_player->jy);
+  Info("");
 }
 
 void DumpTileFromScreen(int sx, int sy)
@@ -482,6 +472,11 @@ void SetDrawtoField(int mode)
   }
 }
 
+int GetDrawtoField(void)
+{
+  return (drawto_field == fieldbuffer ? DRAW_TO_FIELDBUFFER : DRAW_TO_BACKBUFFER);
+}
+
 static void RedrawPlayfield_RND(void)
 {
   if (game.envelope_active)
@@ -636,60 +631,7 @@ void DrawMaskedBorderToTarget(int draw_target)
 
 void DrawTileCursor(int draw_target)
 {
-  Bitmap *fade_bitmap;
-  Bitmap *src_bitmap;
-  int src_x, src_y;
-  int dst_x, dst_y;
-  int graphic = IMG_GLOBAL_TILE_CURSOR;
-  int frame = 0;
-  int tilesize = TILESIZE_VAR;
-  int width = tilesize;
-  int height = tilesize;
-
-  if (game_status != GAME_MODE_PLAYING)
-    return;
-
-  if (!tile_cursor.enabled ||
-      !tile_cursor.active)
-    return;
-
-  if (tile_cursor.moving)
-  {
-    int step = TILESIZE_VAR / 4;
-    int dx = tile_cursor.target_x - tile_cursor.x;
-    int dy = tile_cursor.target_y - tile_cursor.y;
-
-    if (ABS(dx) < step)
-      tile_cursor.x = tile_cursor.target_x;
-    else
-      tile_cursor.x += SIGN(dx) * step;
-
-    if (ABS(dy) < step)
-      tile_cursor.y = tile_cursor.target_y;
-    else
-      tile_cursor.y += SIGN(dy) * step;
-
-    if (tile_cursor.x == tile_cursor.target_x &&
-       tile_cursor.y == tile_cursor.target_y)
-      tile_cursor.moving = FALSE;
-  }
-
-  dst_x = tile_cursor.x;
-  dst_y = tile_cursor.y;
-
-  frame = getGraphicAnimationFrame(graphic, -1);
-
-  getSizedGraphicSource(graphic, frame, tilesize, &src_bitmap, &src_x, &src_y);
-
-  fade_bitmap =
-    (draw_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
-     draw_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
-
-  if (draw_target == DRAW_TO_SCREEN)
-    BlitToScreenMasked(src_bitmap, src_x, src_y, width, height, dst_x, dst_y);
-  else
-    BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
-                    dst_x, dst_y);
+  DrawTileCursor_MM(draw_target, game_status == GAME_MODE_PLAYING);
 }
 
 void BlitScreenToBitmapExt_RND(Bitmap *target_bitmap, int fx, int fy)
@@ -772,7 +714,7 @@ static void PrintFrameTimeDebugging(void)
 
   diff_bar[pos++] = '\0';
 
-  Error(ERR_INFO, "%06d [%02d] [%c%02d] %s",
+  Debug("time:frame", "%06d [%02d] [%c%02d] %s",
        counter,
        diff_1,
        (diff_2 < 0 ? '-' : diff_2 > 0 ? '+' : ' '), ABS(diff_2),
@@ -971,19 +913,13 @@ static void FadeExt(int fade_mask, int fade_mode, int fade_type)
     height = WIN_YSIZE;
   }
 
-  if (!setup.fade_screens ||
-      fade_delay == 0 ||
-      fading.fade_mode == FADE_MODE_NONE)
-  {
-    if (fade_mode == FADE_MODE_FADE_OUT)
-      return;
-
-    BlitBitmap(backbuffer, window, x, y, width, height, x, y);
-
-    redraw_mask &= ~fade_mask;
+  // when switching screens without fading, set fade delay to zero
+  if (!setup.fade_screens || fading.fade_mode == FADE_MODE_NONE)
+    fade_delay = 0;
 
+  // do not display black frame when fading out without fade delay
+  if (fade_mode == FADE_MODE_FADE_OUT && fade_delay == 0)
     return;
-  }
 
   FadeRectangle(x, y, width, height, fade_mode, fade_delay, post_delay,
                draw_border_function);
@@ -999,8 +935,7 @@ static void SetScreenStates_BeforeFadingIn(void)
   global.anim_status = global.anim_status_next;
 
   // store backbuffer with all animations that will be started after fading in
-  if (fade_type_skip != FADE_MODE_SKIP_FADE_IN)
-    PrepareFadeBitmap(DRAW_TO_FADE_TARGET);
+  PrepareFadeBitmap(DRAW_TO_FADE_TARGET);
 
   // set screen mode for animations back to fading
   global.anim_status = GAME_MODE_PSEUDO_FADING;
@@ -1023,8 +958,7 @@ static void SetScreenStates_BeforeFadingOut(void)
   global.anim_status = GAME_MODE_PSEUDO_FADING;
 
   // store backbuffer with all animations that will be stopped for fading out
-  if (fade_type_skip != FADE_MODE_SKIP_FADE_OUT)
-    PrepareFadeBitmap(DRAW_TO_FADE_SOURCE);
+  PrepareFadeBitmap(DRAW_TO_FADE_SOURCE);
 }
 
 static void SetScreenStates_AfterFadingOut(void)
@@ -1365,6 +1299,9 @@ static void RedrawGlobalBorderIfNeeded(void)
 
 #if ONLY_REDRAW_GLOBAL_BORDER_IF_NEEDED
   if (CheckIfGlobalBorderRedrawIsNeeded())
+#else
+  // determine and store new global border bitmap for current game status
+  global_border_bitmap = getGlobalBorderBitmapFromStatus(game_status);
 #endif
   {
     // redraw global screen border (or clear, if defined to be empty)
@@ -1462,15 +1399,15 @@ void SetBorderElement(void)
 
   BorderElement = EL_EMPTY;
 
-  // the MM game engine does not use a visible border element
-  if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
+  // only the R'n'D game engine may use an additional steelwall border
+  if (level.game_engine_type != GAME_ENGINE_TYPE_RND)
     return;
 
   for (y = 0; y < lev_fieldy && BorderElement == EL_EMPTY; y++)
   {
     for (x = 0; x < lev_fieldx; x++)
     {
-      if (!IS_INDESTRUCTIBLE(Feld[x][y]))
+      if (!IS_INDESTRUCTIBLE(Tile[x][y]))
        BorderElement = EL_STEELWALL;
 
       if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
@@ -1496,7 +1433,7 @@ void FloodFillLevelExt(int from_x, int from_y, int fill_element,
   safety++;
 
   if (safety > max_fieldx * max_fieldy)
-    Error(ERR_EXIT, "Something went wrong in 'FloodFill()'. Please debug.");
+    Fail("Something went wrong in 'FloodFill()'. Please debug.");
 
   old_element = field[from_x][from_y];
   field[from_x][from_y] = fill_element;
@@ -1639,8 +1576,9 @@ void DrawGraphic(int x, int y, int graphic, int frame)
 #if DEBUG
   if (!IN_SCR_FIELD(x, y))
   {
-    printf("DrawGraphic(): x = %d, y = %d, graphic = %d\n", x, y, graphic);
-    printf("DrawGraphic(): This should never happen!\n");
+    Debug("draw:DrawGraphic", "x = %d, y = %d, graphic = %d", x, y, graphic);
+    Debug("draw:DrawGraphic", "This should never happen!");
+
     return;
   }
 #endif
@@ -1656,8 +1594,10 @@ void DrawFixedGraphic(int x, int y, int graphic, int frame)
 #if DEBUG
   if (!IN_SCR_FIELD(x, y))
   {
-    printf("DrawGraphic(): x = %d, y = %d, graphic = %d\n", x, y, graphic);
-    printf("DrawGraphic(): This should never happen!\n");
+    Debug("draw:DrawFixedGraphic", "x = %d, y = %d, graphic = %d",
+         x, y, graphic);
+    Debug("draw:DrawFixedGraphic", "This should never happen!");
+
     return;
   }
 #endif
@@ -1693,8 +1633,10 @@ void DrawGraphicThruMask(int x, int y, int graphic, int frame)
 #if DEBUG
   if (!IN_SCR_FIELD(x, y))
   {
-    printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
-    printf("DrawGraphicThruMask(): This should never happen!\n");
+    Debug("draw:DrawGraphicThruMask", "x = %d,y = %d, graphic = %d",
+         x, y, graphic);
+    Debug("draw:DrawGraphicThruMask", "This should never happen!");
+
     return;
   }
 #endif
@@ -1710,8 +1652,10 @@ void DrawFixedGraphicThruMask(int x, int y, int graphic, int frame)
 #if DEBUG
   if (!IN_SCR_FIELD(x, y))
   {
-    printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
-    printf("DrawGraphicThruMask(): This should never happen!\n");
+    Debug("draw:DrawFixedGraphicThruMask", "x = %d,y = %d, graphic = %d",
+         x, y, graphic);
+    Debug("draw:DrawFixedGraphicThruMask", "This should never happen!");
+
     return;
   }
 #endif
@@ -1872,8 +1816,10 @@ static void DrawGraphicShiftedNormal(int x, int y, int dx, int dy,
 #if DEBUG
   if (!IN_SCR_FIELD(x, y))
   {
-    printf("DrawGraphicShifted(): x = %d, y = %d, graphic = %d\n",x,y,graphic);
-    printf("DrawGraphicShifted(): This should never happen!\n");
+    Debug("draw:DrawGraphicShiftedNormal", "x = %d, y = %d, graphic = %d",
+         x, y, graphic);
+    Debug("draw:DrawGraphicShiftedNormal", "This should never happen!");
+
     return;
   }
 #endif
@@ -2025,9 +1971,9 @@ void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
   {
     boolean left_stopped = FALSE, right_stopped = FALSE;
 
-    if (!IN_LEV_FIELD(lx - 1, ly) || IS_WALL(Feld[lx - 1][ly]))
+    if (!IN_LEV_FIELD(lx - 1, ly) || IS_WALL(Tile[lx - 1][ly]))
       left_stopped = TRUE;
-    if (!IN_LEV_FIELD(lx + 1, ly) || IS_WALL(Feld[lx + 1][ly]))
+    if (!IN_LEV_FIELD(lx + 1, ly) || IS_WALL(Tile[lx + 1][ly]))
       right_stopped = TRUE;
 
     if (left_stopped && right_stopped)
@@ -2079,7 +2025,7 @@ void DrawLevelElementThruMask(int x, int y, int element)
 
 void DrawLevelFieldThruMask(int x, int y)
 {
-  DrawLevelElementExt(x, y, 0, 0, Feld[x][y], NO_CUTTING, USE_MASKING);
+  DrawLevelElementExt(x, y, 0, 0, Tile[x][y], NO_CUTTING, USE_MASKING);
 }
 
 // !!! implementation of quicksand is totally broken !!!
@@ -2286,8 +2232,8 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
        continue;
 
       // do not crumble fields that are being digged or snapped
-      if (Feld[xx][yy] == EL_EMPTY ||
-         Feld[xx][yy] == EL_ELEMENT_SNAPPING)
+      if (Tile[xx][yy] == EL_EMPTY ||
+         Tile[xx][yy] == EL_ELEMENT_SNAPPING)
        continue;
 
       element = TILE_GFX_ELEMENT(xx, yy);
@@ -2316,7 +2262,7 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
          !IN_SCR_FIELD(sxx, syy))
        continue;
 
-      if (Feld[xx][yy] == EL_ELEMENT_SNAPPING)
+      if (Tile[xx][yy] == EL_ELEMENT_SNAPPING)
        continue;
 
       element = TILE_GFX_ELEMENT(xx, yy);
@@ -2342,7 +2288,7 @@ void DrawLevelFieldCrumbled(int x, int y)
   if (!IN_LEV_FIELD(x, y))
     return;
 
-  if (Feld[x][y] == EL_ELEMENT_SNAPPING &&
+  if (Tile[x][y] == EL_ELEMENT_SNAPPING &&
       GfxElement[x][y] != EL_UNDEFINED &&
       GFX_CRUMBLED(GfxElement[x][y]))
   {
@@ -2391,7 +2337,7 @@ void DrawLevelFieldCrumbledNeighbours(int x, int y)
 
     if (!IN_LEV_FIELD(xx, yy) ||
        !IN_SCR_FIELD(sxx, syy) ||
-       !GFX_CRUMBLED(Feld[xx][yy]) ||
+       !GFX_CRUMBLED(Tile[xx][yy]) ||
        IS_MOVING(xx, yy))
       continue;
 
@@ -2410,7 +2356,7 @@ void DrawLevelFieldCrumbledNeighbours(int x, int y)
 
     if (!IN_LEV_FIELD(xx, yy) ||
        !IN_SCR_FIELD(sxx, syy) ||
-       !GFX_CRUMBLED(Feld[xx][yy]) ||
+       !GFX_CRUMBLED(Tile[xx][yy]) ||
        IS_MOVING(xx, yy))
       continue;
 
@@ -2475,7 +2421,7 @@ void DrawScreenField(int x, int y)
     return;
   }
 
-  element = Feld[lx][ly];
+  element = Tile[lx][ly];
   content = Store[lx][ly];
 
   if (IS_MOVING(lx, ly))
@@ -2542,7 +2488,7 @@ void DrawScreenField(int x, int y)
     horiz_move = (MovDir[oldx][oldy] == MV_LEFT ||
                  MovDir[oldx][oldy] == MV_RIGHT);
 
-    element_old = Feld[oldx][oldy];
+    element_old = Tile[oldx][oldy];
     content_old = Store[oldx][oldy];
 
     if (element_old == EL_QUICKSAND_EMPTYING ||
@@ -2695,7 +2641,7 @@ void DrawSizedElementOrWall(int sx, int sy, int scroll_x, int scroll_y,
   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
     DrawSizedElement(sx, sy, EL_EMPTY, tilesize);
   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
-    DrawSizedElement(sx, sy, Feld[x][y], tilesize);
+    DrawSizedElement(sx, sy, Tile[x][y], tilesize);
   else
     DrawSizedGraphic(sx, sy, el2edimg(getBorderElement(x, y)), 0, tilesize);
 }
@@ -2707,7 +2653,7 @@ void DrawMiniElementOrWall(int sx, int sy, int scroll_x, int scroll_y)
   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
     DrawMiniElement(sx, sy, EL_EMPTY);
   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
-    DrawMiniElement(sx, sy, Feld[x][y]);
+    DrawMiniElement(sx, sy, Tile[x][y]);
   else
     DrawMiniGraphic(sx, sy, el2edimg(getBorderElement(x, y)));
 }
@@ -2836,9 +2782,13 @@ void ShowEnvelope(int envelope_nr)
   int anim_mode = graphic_info[graphic].anim_mode;
   int main_anim_mode = (anim_mode == ANIM_NONE ? ANIM_VERTICAL|ANIM_HORIZONTAL:
                        anim_mode == ANIM_DEFAULT ? ANIM_VERTICAL : anim_mode);
+  boolean overlay_enabled = GetOverlayEnabled();
 
   game.envelope_active = TRUE; // needed for RedrawPlayfield() events
 
+  SetOverlayEnabled(FALSE);
+  UnmapAllGadgets();
+
   PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
 
   if (anim_mode == ANIM_DEFAULT)
@@ -2851,6 +2801,9 @@ void ShowEnvelope(int envelope_nr)
   else
     WaitForEventToContinue();
 
+  RemapAllGadgets();
+  SetOverlayEnabled(overlay_enabled);
+
   PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
 
   if (anim_mode != ANIM_NONE)
@@ -2867,6 +2820,53 @@ void ShowEnvelope(int envelope_nr)
   BackToFront();
 }
 
+static void PrepareEnvelopeRequestToScreen(Bitmap *bitmap, int sx, int sy,
+                                          int xsize, int ysize)
+{
+  if (!global.use_envelope_request ||
+      request.sort_priority <= 0)
+    return;
+
+  if (request.bitmap == NULL ||
+      xsize > request.xsize ||
+      ysize > request.ysize)
+  {
+    if (request.bitmap != NULL)
+      FreeBitmap(request.bitmap);
+
+    request.bitmap = CreateBitmap(xsize, ysize, DEFAULT_DEPTH);
+
+    SDL_Surface *surface = request.bitmap->surface;
+
+    if ((request.bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
+      Fail("SDLGetNativeSurface() failed");
+  }
+
+  BlitBitmap(bitmap, request.bitmap, sx, sy, xsize, ysize, 0, 0);
+
+  SDLFreeBitmapTextures(request.bitmap);
+  SDLCreateBitmapTextures(request.bitmap);
+
+  // set envelope request run-time values
+  request.sx = sx;
+  request.sy = sy;
+  request.xsize = xsize;
+  request.ysize = ysize;
+}
+
+void DrawEnvelopeRequestToScreen(int drawing_target, int drawing_stage)
+{
+  if (global.use_envelope_request &&
+      game.request_active_or_moving &&
+      request.sort_priority > 0 &&
+      drawing_target == DRAW_TO_SCREEN &&
+      drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
+  {
+    BlitToScreen(request.bitmap, 0, 0, request.xsize, request.ysize,
+                request.sx, request.sy);
+  }
+}
+
 static void setRequestBasePosition(int *x, int *y)
 {
   int sx_base, sy_base;
@@ -3017,6 +3017,8 @@ static void DrawEnvelopeRequest(char *text)
   // store readily prepared envelope request for later use when animating
   BlitBitmap(backbuffer, bitmap_db_store_2, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
 
+  PrepareEnvelopeRequestToScreen(bitmap_db_store_2, sx, sy, width, height);
+
   if (text_door_style)
     free(text_door_style);
 }
@@ -3098,6 +3100,8 @@ static void AnimateEnvelopeRequest(int anim_mode, int action)
       }
     }
 
+    PrepareEnvelopeRequestToScreen(backbuffer, dst_x, dst_y, width, height);
+
     redraw_mask |= REDRAW_FIELD;
 
     BackToFront();
@@ -3793,7 +3797,7 @@ void DrawLevelGraphicAnimationIfNeeded(int x, int y, int graphic)
   if (GFX_CRUMBLED(TILE_GFX_ELEMENT(x, y)))
     DrawLevelFieldCrumbled(x, y);
 #else
-  if (GFX_CRUMBLED(Feld[x][y]))
+  if (GFX_CRUMBLED(Tile[x][y]))
     DrawLevelFieldCrumbled(x, y);
 #endif
 }
@@ -3897,8 +3901,8 @@ static void DrawPlayerExt(struct PlayerInfo *player, int drawing_stage)
 #if DEBUG
   if (!IN_LEV_FIELD(player->jx, player->jy))
   {
-    printf("DrawPlayerField(): x = %d, y = %d\n", player->jx, player->jy);
-    printf("DrawPlayerField(): This should never happen!\n");
+    Debug("draw:DrawPlayerExt", "x = %d, y = %d", player->jx, player->jy);
+    Debug("draw:DrawPlayerExt", "This should never happen!");
 
     draw_player[pnr] = FALSE;
 
@@ -3924,8 +3928,8 @@ static void DrawPlayerExt(struct PlayerInfo *player, int drawing_stage)
   int sy = SCREENY(jy);
   int sxx = (move_dir == MV_LEFT || move_dir == MV_RIGHT ? player->GfxPos : 0);
   int syy = (move_dir == MV_UP   || move_dir == MV_DOWN  ? player->GfxPos : 0);
-  int element = Feld[jx][jy];
-  int last_element = Feld[last_jx][last_jy];
+  int element = Tile[jx][jy];
+  int last_element = Tile[last_jx][last_jy];
   int action = (player->is_pushing    ? ACTION_PUSHING         :
                player->is_digging    ? ACTION_DIGGING         :
                player->is_collecting ? ACTION_COLLECTING      :
@@ -4051,7 +4055,7 @@ static void DrawPlayerExt(struct PlayerInfo *player, int drawing_stage)
 
     if (!IS_MOVING(jx, jy))            // push movement already finished
     {
-      element = Feld[next_jx][next_jy];
+      element = Tile[next_jx][next_jy];
       gfx_frame = GfxFrame[next_jx][next_jy];
     }
 
@@ -4085,7 +4089,7 @@ static void DrawPlayerExt(struct PlayerInfo *player, int drawing_stage)
     // do not draw (EM style) pushing animation when pushing is finished
     // (two-tile animations usually do not contain start and end frame)
     if (graphic_info[graphic].double_movement && !IS_MOVING(jx, jy))
-      DrawLevelElement(next_jx, next_jy, Feld[next_jx][next_jy]);
+      DrawLevelElement(next_jx, next_jy, Tile[next_jx][next_jy]);
     else
       DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
 #else
@@ -4212,6 +4216,7 @@ void DrawPlayerField(int x, int y)
 
 void WaitForEventToContinue(void)
 {
+  boolean first_wait = TRUE;
   boolean still_wait = TRUE;
 
   if (program.headless)
@@ -4224,6 +4229,7 @@ void WaitForEventToContinue(void)
   button_status = MB_RELEASED;
 
   ClearEventQueue();
+  ClearPlayerAction();
 
   while (still_wait)
   {
@@ -4233,17 +4239,22 @@ void WaitForEventToContinue(void)
     {
       switch (event.type)
       {
-       case EVENT_BUTTONRELEASE:
+       case EVENT_BUTTONPRESS:
+        case EVENT_FINGERPRESS:
+         first_wait = FALSE;
+         break;
+
+        case EVENT_BUTTONRELEASE:
+        case EVENT_FINGERRELEASE:
+         still_wait = first_wait;
+         break;
+
        case EVENT_KEYPRESS:
         case SDL_CONTROLLERBUTTONDOWN:
         case SDL_JOYBUTTONDOWN:
          still_wait = FALSE;
          break;
 
-       case EVENT_KEYRELEASE:
-         ClearPlayerAction();
-         break;
-
        default:
          HandleOtherEvents(&event);
          break;
@@ -4254,7 +4265,8 @@ void WaitForEventToContinue(void)
       still_wait = FALSE;
     }
 
-    BackToFront();
+    if (!PendingEvent())
+      BackToFront();
   }
 }
 
@@ -4262,10 +4274,11 @@ void WaitForEventToContinue(void)
 #define MAX_REQUEST_LINE_FONT1_LEN     7
 #define MAX_REQUEST_LINE_FONT2_LEN     10
 
-static int RequestHandleEvents(unsigned int req_state)
+static int RequestHandleEvents(unsigned int req_state, int draw_buffer_game)
 {
   boolean game_just_ended = (game_status == GAME_MODE_PLAYING &&
                             checkGameEnded());
+  int draw_buffer_last = GetDrawtoField();
   int width  = request.width;
   int height = request.height;
   int sx, sy;
@@ -4286,11 +4299,11 @@ static int RequestHandleEvents(unsigned int req_state)
 
   while (result < 0)
   {
+    boolean event_handled = FALSE;
+
     if (game_just_ended)
     {
-      // the MM game engine does not use a special (scrollable) field buffer
-      if (level.game_engine_type != GAME_ENGINE_TYPE_MM)
-       SetDrawtoField(DRAW_TO_FIELDBUFFER);
+      SetDrawtoField(draw_buffer_game);
 
       HandleGameActions();
 
@@ -4309,6 +4322,8 @@ static int RequestHandleEvents(unsigned int req_state)
 
       while (NextValidEvent(&event))
       {
+       event_handled = TRUE;
+
        switch (event.type)
        {
          case EVENT_BUTTONPRESS:
@@ -4466,6 +4481,7 @@ static int RequestHandleEvents(unsigned int req_state)
            break;
          }
 
+         case EVENT_FINGERRELEASE:
          case EVENT_KEYRELEASE:
            ClearPlayerAction();
            break;
@@ -4562,18 +4578,25 @@ static int RequestHandleEvents(unsigned int req_state)
       }
     }
 
-    if (game_just_ended)
+    if (event_handled)
     {
-      if (global.use_envelope_request)
+      if (game_just_ended)
       {
-       // copy back current state of pressed buttons inside request area
-       BlitBitmap(drawto, bitmap_db_store_2, sx, sy, width, height, sx, sy);
+       if (global.use_envelope_request)
+       {
+         // copy back current state of pressed buttons inside request area
+         BlitBitmap(drawto, bitmap_db_store_2, sx, sy, width, height, sx, sy);
+       }
       }
+
+      PrepareEnvelopeRequestToScreen(drawto, sx, sy, width, height);
     }
 
     BackToFront();
   }
 
+  SetDrawtoField(draw_buffer_last);
+
   game.request_active = FALSE;
 
   return result;
@@ -4581,6 +4604,7 @@ static int RequestHandleEvents(unsigned int req_state)
 
 static boolean RequestDoor(char *text, unsigned int req_state)
 {
+  int draw_buffer_last = GetDrawtoField();
   unsigned int old_door_state;
   int max_request_line_len = MAX_REQUEST_LINE_FONT1_LEN;
   int font_nr = FONT_TEXT_2;
@@ -4722,7 +4746,7 @@ static boolean RequestDoor(char *text, unsigned int req_state)
   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
 
   // ---------- handle request buttons ----------
-  result = RequestHandleEvents(req_state);
+  result = RequestHandleEvents(req_state, draw_buffer_last);
 
   UnmapToolButtons();
 
@@ -4763,6 +4787,7 @@ static boolean RequestDoor(char *text, unsigned int req_state)
 
 static boolean RequestEnvelope(char *text, unsigned int req_state)
 {
+  int draw_buffer_last = GetDrawtoField();
   int result;
 
   if (game_status == GAME_MODE_PLAYING)
@@ -4814,7 +4839,7 @@ static boolean RequestEnvelope(char *text, unsigned int req_state)
   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
 
   // ---------- handle request buttons ----------
-  result = RequestHandleEvents(req_state);
+  result = RequestHandleEvents(req_state, draw_buffer_last);
 
   UnmapToolButtons();
 
@@ -4851,6 +4876,8 @@ boolean Request(char *text, unsigned int req_state)
   boolean overlay_enabled = GetOverlayEnabled();
   boolean result;
 
+  game.request_active_or_moving = TRUE;
+
   SetOverlayEnabled(FALSE);
 
   if (global.use_envelope_request)
@@ -4860,6 +4887,8 @@ boolean Request(char *text, unsigned int req_state)
 
   SetOverlayEnabled(overlay_enabled);
 
+  game.request_active_or_moving = FALSE;
+
   return result;
 }
 
@@ -5367,7 +5396,7 @@ unsigned int MoveDoor(unsigned int door_state)
          if (width > door_rect->width)
            width = door_rect->width;
 
-         // printf("::: k == %d [%d] \n", k, start_step);
+         // Debug("tools:MoveDoor", "k == %d [%d]", k, start_step);
        }
 
        if (pos->step_yoffset < 0)      // door part on bottom side
@@ -5699,7 +5728,7 @@ void CreateToolButtons(void)
                      GDI_END);
 
     if (gi == NULL)
-      Error(ERR_EXIT, "cannot create gadget");
+      Fail("cannot create gadget");
 
     tool_gadget[id] = gi;
   }
@@ -5736,8 +5765,63 @@ static struct Mapping_EM_to_RND_object
   int action;
   int direction;
 }
-em_object_mapping_list[] =
+em_object_mapping_list[GAME_TILE_MAX + 1] =
 {
+  {
+    Zborder,                           FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+  {
+    Zplayer,                           FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+
+  {
+    Zbug,                              FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+  {
+    Ztank,                             FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+  {
+    Zeater,                            FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+  {
+    Zdynamite,                         FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+  {
+    Zboom,                             FALSE,  FALSE,
+    EL_EMPTY,                          -1, -1
+  },
+
+  {
+    Xchain,                            FALSE,  FALSE,
+    EL_DEFAULT,                                ACTION_EXPLODING, -1
+  },
+  {
+    Xboom_bug,                         FALSE,  FALSE,
+    EL_BUG,                            ACTION_EXPLODING, -1
+  },
+  {
+    Xboom_tank,                                FALSE,  FALSE,
+    EL_SPACESHIP,                      ACTION_EXPLODING, -1
+  },
+  {
+    Xboom_android,                     FALSE,  FALSE,
+    EL_EMC_ANDROID,                    ACTION_OTHER, -1
+  },
+  {
+    Xboom_1,                           FALSE,  FALSE,
+    EL_DEFAULT,                                ACTION_EXPLODING, -1
+  },
+  {
+    Xboom_2,                           FALSE,  FALSE,
+    EL_DEFAULT,                                ACTION_EXPLODING, -1
+  },
+
   {
     Xblank,                            TRUE,   FALSE,
     EL_EMPTY,                          -1, -1
@@ -5827,6 +5911,39 @@ em_object_mapping_list[] =
     EL_EMC_FAKE_ACID,                  -1, -1
   },
 
+  {
+    Xfake_acid_1_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_2_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_3_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_4_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_5_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_6_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_7_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+  {
+    Xfake_acid_8_player,               FALSE,  FALSE,
+    EL_EMC_FAKE_ACID,                  -1, -1
+  },
+
   {
     Xgrass,                            TRUE,   FALSE,
     EL_EMC_GRASS,                      -1, -1
@@ -6508,12 +6625,12 @@ em_object_mapping_list[] =
     EL_SPRING,                         -1, -1
   },
   {
-    Xspring_e,                         FALSE,  FALSE,
-    EL_SPRING,                         -1, -1
+    Xspring_e,                         TRUE,   FALSE,
+    EL_SPRING_RIGHT,                   -1, -1
   },
   {
-    Xspring_w,                         FALSE,  FALSE,
-    EL_SPRING,                         -1, -1
+    Xspring_w,                         TRUE,   FALSE,
+    EL_SPRING_LEFT,                    -1, -1
   },
   {
     Xspring_fall,                      FALSE,  FALSE,
@@ -6602,11 +6719,11 @@ em_object_mapping_list[] =
   },
   {
     Xpush_spring_e,                    FALSE,  FALSE,
-    EL_SPRING,                         -1, MV_BIT_RIGHT
+    EL_SPRING_RIGHT,                   -1, MV_BIT_RIGHT
   },
   {
     Xpush_spring_w,                    FALSE,  FALSE,
-    EL_SPRING,                         -1, MV_BIT_LEFT
+    EL_SPRING_LEFT,                    -1, MV_BIT_LEFT
   },
 
   {
@@ -6825,11 +6942,11 @@ em_object_mapping_list[] =
   },
 
   {
-    Xdrip,                             FALSE,  FALSE,
+    Xdrip,                             TRUE,   FALSE,
     EL_AMOEBA_DROP,                    ACTION_GROWING, -1
   },
   {
-    Xdrip_fall,                                TRUE,   FALSE,
+    Xdrip_fall,                                FALSE,  FALSE,
     EL_AMOEBA_DROP,                    -1, -1
   },
   {
@@ -7233,8 +7350,8 @@ em_object_mapping_list[] =
     EL_CHAR('!'),                      -1, -1
   },
   {
-    Xalpha_quote,                      TRUE,   FALSE,
-    EL_CHAR('"'),                      -1, -1
+    Xalpha_apost,                      TRUE,   FALSE,
+    EL_CHAR('\''),                     -1, -1
   },
   {
     Xalpha_comma,                      TRUE,   FALSE,
@@ -7422,43 +7539,6 @@ em_object_mapping_list[] =
     EL_SAND,                           ACTION_SNAPPING, -1
   },
 
-  {
-    Xboom_bug,                         FALSE,  FALSE,
-    EL_BUG,                            ACTION_EXPLODING, -1
-  },
-  {
-    Xboom_bomb,                                FALSE,  FALSE,
-    EL_BOMB,                           ACTION_EXPLODING, -1
-  },
-  {
-    Xboom_android,                     FALSE,  FALSE,
-    EL_EMC_ANDROID,                    ACTION_OTHER, -1
-  },
-  {
-    Xboom_1,                           FALSE,  FALSE,
-    EL_DEFAULT,                                ACTION_EXPLODING, -1
-  },
-  {
-    Xboom_2,                           FALSE,  FALSE,
-    EL_DEFAULT,                                ACTION_EXPLODING, -1
-  },
-  {
-    Znormal,                           FALSE,  FALSE,
-    EL_EMPTY,                          -1, -1
-  },
-  {
-    Zdynamite,                         FALSE,  FALSE,
-    EL_EMPTY,                          -1, -1
-  },
-  {
-    Zplayer,                           FALSE,  FALSE,
-    EL_EMPTY,                          -1, -1
-  },
-  {
-    Zborder,                           FALSE,  FALSE,
-    EL_EMPTY,                          -1, -1
-  },
-
   {
     -1,                                        FALSE,  FALSE,
     -1,                                        -1, -1
@@ -7474,7 +7554,7 @@ static struct Mapping_EM_to_RND_player
   int action;
   int direction;
 }
-em_player_mapping_list[] =
+em_player_mapping_list[MAX_PLAYERS * PLY_MAX + 1] =
 {
   {
     PLY_walk_n,                                0,
@@ -7691,7 +7771,7 @@ em_player_mapping_list[] =
   }
 };
 
-int map_element_RND_to_EM(int element_rnd)
+int map_element_RND_to_EM_cave(int element_rnd)
 {
   static unsigned short mapping_RND_to_EM[NUM_FILE_ELEMENTS];
   static boolean mapping_initialized = FALSE;
@@ -7712,17 +7792,19 @@ int map_element_RND_to_EM(int element_rnd)
     mapping_initialized = TRUE;
   }
 
-  if (element_rnd >= 0 && element_rnd < NUM_FILE_ELEMENTS)
-    return mapping_RND_to_EM[element_rnd];
+  if (element_rnd < 0 || element_rnd >= NUM_FILE_ELEMENTS)
+  {
+    Warn("invalid RND level element %d", element_rnd);
 
-  Error(ERR_WARN, "invalid RND level element %d", element_rnd);
+    return EL_UNKNOWN;
+  }
 
-  return EL_UNKNOWN;
+  return map_em_element_X_to_C(mapping_RND_to_EM[element_rnd]);
 }
 
-int map_element_EM_to_RND(int element_em)
+int map_element_EM_to_RND_cave(int element_em_cave)
 {
-  static unsigned short mapping_EM_to_RND[TILE_MAX];
+  static unsigned short mapping_EM_to_RND[GAME_TILE_MAX];
   static boolean mapping_initialized = FALSE;
 
   if (!mapping_initialized)
@@ -7730,7 +7812,7 @@ int map_element_EM_to_RND(int element_em)
     int i;
 
     // return "EL_UNKNOWN" for all undefined elements in mapping array
-    for (i = 0; i < TILE_MAX; i++)
+    for (i = 0; i < GAME_TILE_MAX; i++)
       mapping_EM_to_RND[i] = EL_UNKNOWN;
 
     for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
@@ -7740,52 +7822,85 @@ int map_element_EM_to_RND(int element_em)
     mapping_initialized = TRUE;
   }
 
-  if (element_em >= 0 && element_em < TILE_MAX)
-    return mapping_EM_to_RND[element_em];
+  if (element_em_cave < 0 || element_em_cave >= CAVE_TILE_MAX)
+  {
+    Warn("invalid EM cave element %d", element_em_cave);
 
-  Error(ERR_WARN, "invalid EM level element %d", element_em);
+    return EL_UNKNOWN;
+  }
 
-  return EL_UNKNOWN;
+  return mapping_EM_to_RND[map_em_element_C_to_X(element_em_cave)];
+}
+
+int map_element_EM_to_RND_game(int element_em_game)
+{
+  static unsigned short mapping_EM_to_RND[GAME_TILE_MAX];
+  static boolean mapping_initialized = FALSE;
+
+  if (!mapping_initialized)
+  {
+    int i;
+
+    // return "EL_UNKNOWN" for all undefined elements in mapping array
+    for (i = 0; i < GAME_TILE_MAX; i++)
+      mapping_EM_to_RND[i] = EL_UNKNOWN;
+
+    for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
+      mapping_EM_to_RND[em_object_mapping_list[i].element_em] =
+       em_object_mapping_list[i].element_rnd;
+
+    mapping_initialized = TRUE;
+  }
+
+  if (element_em_game < 0 || element_em_game >= GAME_TILE_MAX)
+  {
+    Warn("invalid EM game element %d", element_em_game);
+
+    return EL_UNKNOWN;
+  }
+
+  return mapping_EM_to_RND[element_em_game];
 }
 
 void map_android_clone_elements_RND_to_EM(struct LevelInfo *level)
 {
   struct LevelInfo_EM *level_em = level->native_em_level;
-  struct LEVEL *lev = level_em->lev;
+  struct CAVE *cav = level_em->cav;
   int i, j;
 
-  for (i = 0; i < TILE_MAX; i++)
-    lev->android_array[i] = Xblank;
+  for (i = 0; i < GAME_TILE_MAX; i++)
+    cav->android_array[i] = Cblank;
 
   for (i = 0; i < level->num_android_clone_elements; i++)
   {
     int element_rnd = level->android_clone_element[i];
-    int element_em = map_element_RND_to_EM(element_rnd);
+    int element_em_cave = map_element_RND_to_EM_cave(element_rnd);
 
     for (j = 0; em_object_mapping_list[j].element_em != -1; j++)
       if (em_object_mapping_list[j].element_rnd == element_rnd)
-       lev->android_array[em_object_mapping_list[j].element_em] = element_em;
+       cav->android_array[em_object_mapping_list[j].element_em] =
+         element_em_cave;
   }
 }
 
 void map_android_clone_elements_EM_to_RND(struct LevelInfo *level)
 {
   struct LevelInfo_EM *level_em = level->native_em_level;
-  struct LEVEL *lev = level_em->lev;
+  struct CAVE *cav = level_em->cav;
   int i, j;
 
   level->num_android_clone_elements = 0;
 
-  for (i = 0; i < TILE_MAX; i++)
+  for (i = 0; i < GAME_TILE_MAX; i++)
   {
-    int element_em = lev->android_array[i];
+    int element_em_cave = cav->android_array[i];
     int element_rnd;
     boolean element_found = FALSE;
 
-    if (element_em == Xblank)
+    if (element_em_cave == Cblank)
       continue;
 
-    element_rnd = map_element_EM_to_RND(element_em);
+    element_rnd = map_element_EM_to_RND_cave(element_em_cave);
 
     for (j = 0; j < level->num_android_clone_elements; j++)
       if (level->android_clone_element[j] == element_rnd)
@@ -8214,11 +8329,21 @@ int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
   return getBeltSwitchElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
 }
 
+boolean swapTiles_EM(boolean is_pre_emc_cave)
+{
+  return is_pre_emc_cave && leveldir_current->use_emc_tiles;
+}
+
 boolean getTeamMode_EM(void)
 {
   return game.team_mode || network_playing;
 }
 
+boolean isActivePlayer_EM(int player_nr)
+{
+  return stored_player[player_nr].active;
+}
+
 unsigned int InitRND(int seed)
 {
   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
@@ -8231,7 +8356,7 @@ unsigned int InitRND(int seed)
     return InitEngineRandom_RND(seed);
 }
 
-static struct Mapping_EM_to_RND_object object_mapping[TILE_MAX];
+static struct Mapping_EM_to_RND_object object_mapping[GAME_TILE_MAX];
 static struct Mapping_EM_to_RND_player player_mapping[MAX_PLAYERS][PLY_MAX];
 
 static int get_effective_element_EM(int tile, int frame_em)
@@ -8577,7 +8702,7 @@ void InitGraphicInfo_EM(void)
   int i, j, p;
 
   // always start with reliable default values
-  for (i = 0; i < TILE_MAX; i++)
+  for (i = 0; i < GAME_TILE_MAX; i++)
   {
     object_mapping[i].element_rnd = EL_UNKNOWN;
     object_mapping[i].is_backside = FALSE;
@@ -8626,7 +8751,7 @@ void InitGraphicInfo_EM(void)
        MV_DIR_FROM_BIT(em_player_mapping_list[i].direction);
   }
 
-  for (i = 0; i < TILE_MAX; i++)
+  for (i = 0; i < GAME_TILE_MAX; i++)
   {
     int element = object_mapping[i].element_rnd;
     int action = object_mapping[i].action;
@@ -8683,7 +8808,7 @@ void InitGraphicInfo_EM(void)
       boolean has_action_graphics = (graphic != base_graphic);
       boolean has_crumbled_graphics = (base_crumbled != base_graphic);
       struct GraphicInfo *g = &graphic_info[graphic];
-      struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
+      struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][j];
       Bitmap *src_bitmap;
       int src_x, src_y;
       // ensure to get symmetric 3-frame, 2-delay animations as used in EM
@@ -8711,6 +8836,14 @@ void InitGraphicInfo_EM(void)
                        i == Xfake_acid_6 ? 50 :
                        i == Xfake_acid_7 ? 60 :
                        i == Xfake_acid_8 ? 70 :
+                       i == Xfake_acid_1_player ? 0 :
+                       i == Xfake_acid_2_player ? 10 :
+                       i == Xfake_acid_3_player ? 20 :
+                       i == Xfake_acid_4_player ? 30 :
+                       i == Xfake_acid_5_player ? 40 :
+                       i == Xfake_acid_6_player ? 50 :
+                       i == Xfake_acid_7_player ? 60 :
+                       i == Xfake_acid_8_player ? 70 :
                        i == Xball_2 ? 7 :
                        i == Yball_2 ? j + 8 :
                        i == Yball_blank ? j + 1 :
@@ -8767,13 +8900,13 @@ void InitGraphicInfo_EM(void)
                        i == Xboom_bug && j == 5 ? 2 :
                        i == Xboom_bug && j == 6 ? 2 :
                        i == Xboom_bug && j == 7 ? 0 :
-                       i == Xboom_bomb && j == 1 ? 2 :
-                       i == Xboom_bomb && j == 2 ? 2 :
-                       i == Xboom_bomb && j == 3 ? 4 :
-                       i == Xboom_bomb && j == 4 ? 4 :
-                       i == Xboom_bomb && j == 5 ? 2 :
-                       i == Xboom_bomb && j == 6 ? 2 :
-                       i == Xboom_bomb && j == 7 ? 0 :
+                       i == Xboom_tank && j == 1 ? 2 :
+                       i == Xboom_tank && j == 2 ? 2 :
+                       i == Xboom_tank && j == 3 ? 4 :
+                       i == Xboom_tank && j == 4 ? 4 :
+                       i == Xboom_tank && j == 5 ? 2 :
+                       i == Xboom_tank && j == 6 ? 2 :
+                       i == Xboom_tank && j == 7 ? 0 :
                        i == Xboom_android && j == 7 ? 6 :
                        i == Xboom_1 && j == 1 ? 2 :
                        i == Xboom_1 && j == 2 ? 2 :
@@ -8891,7 +9024,7 @@ void InitGraphicInfo_EM(void)
     }
   }
 
-  for (i = 0; i < TILE_MAX; i++)
+  for (i = 0; i < GAME_TILE_MAX; i++)
   {
     for (j = 0; j < 8; j++)
     {
@@ -8914,8 +9047,8 @@ void InitGraphicInfo_EM(void)
                 Xspring);
 
        // no separate animation for "smashed by rock" -- use rock instead
-       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
-       struct GraphicInfo_EM *g_xx = &graphic_info_em_object[e][7 - j];
+       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][j];
+       struct GraphicInfo_EM *g_xx = &graphic_info_em_object[e][j];
 
        g_em->bitmap            = g_xx->bitmap;
        g_em->src_x             = g_xx->src_x;
@@ -8951,7 +9084,7 @@ void InitGraphicInfo_EM(void)
                       el_act_dir2img(effective_element, effective_action,
                                      direction));
        struct GraphicInfo *g = &graphic_info[graphic];
-       struct GraphicInfo_EM *g_em = &graphic_info_em_player[p][i][7 - j];
+       struct GraphicInfo_EM *g_em = &graphic_info_em_player[p][i][j];
        Bitmap *src_bitmap;
        int src_x, src_y;
        int sync_frame = j;
@@ -8982,7 +9115,7 @@ static void CheckSaveEngineSnapshot_EM(byte action[MAX_PLAYERS], int frame,
                                       boolean any_player_snapping,
                                       boolean any_player_dropping)
 {
-  if (frame == 0 && !any_player_dropping)
+  if (frame == 7 && !any_player_dropping)
   {
     if (!local_player->was_waiting)
     {
@@ -9034,17 +9167,19 @@ static void CheckSaveEngineSnapshot_MM(boolean element_clicked,
   }
 }
 
-void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
-                           boolean any_player_moving,
-                           boolean any_player_snapping,
-                           boolean any_player_dropping)
+boolean CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
+                              boolean any_player_moving,
+                              boolean any_player_snapping,
+                              boolean any_player_dropping)
 {
   if (tape.single_step && tape.recording && !tape.pausing)
-    if (frame == 0 && !any_player_dropping)
+    if (frame == 7 && !any_player_dropping && FrameCounter > 6)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
 
   CheckSaveEngineSnapshot_EM(action, frame, any_player_moving,
                             any_player_snapping, any_player_dropping);
+
+  return tape.pausing;
 }
 
 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
@@ -9208,51 +9343,59 @@ void PlaySoundSelecting(void)
 #endif
 }
 
-void ToggleFullscreenOrChangeWindowScalingIfNeeded(void)
+void ToggleFullscreenIfNeeded(void)
 {
-  boolean change_fullscreen = (setup.fullscreen !=
-                              video.fullscreen_enabled);
-  boolean change_window_scaling_percent = (!video.fullscreen_enabled &&
-                                          setup.window_scaling_percent !=
-                                          video.window_scaling_percent);
-
-  if (change_window_scaling_percent && video.fullscreen_enabled)
+  // if setup and video fullscreen state are already matching, nothing do do
+  if (setup.fullscreen == video.fullscreen_enabled ||
+      !video.fullscreen_available)
     return;
 
-  if (!change_window_scaling_percent && !video.fullscreen_available)
-    return;
+  SDLSetWindowFullscreen(setup.fullscreen);
 
-  if (change_window_scaling_percent)
-  {
-    SDLSetWindowScaling(setup.window_scaling_percent);
+  // set setup value according to successfully changed fullscreen mode
+  setup.fullscreen = video.fullscreen_enabled;
+}
 
+void ChangeWindowScalingIfNeeded(void)
+{
+  // if setup and video window scaling are already matching, nothing do do
+  if (setup.window_scaling_percent == video.window_scaling_percent ||
+      video.fullscreen_enabled)
     return;
-  }
-  else if (change_fullscreen)
-  {
-    SDLSetWindowFullscreen(setup.fullscreen);
 
-    // set setup value according to successfully changed fullscreen mode
-    setup.fullscreen = video.fullscreen_enabled;
+  SDLSetWindowScaling(setup.window_scaling_percent);
+
+  // set setup value according to successfully changed window scaling
+  setup.window_scaling_percent = video.window_scaling_percent;
+}
 
+void ChangeVsyncModeIfNeeded(void)
+{
+  int setup_vsync_mode = VSYNC_MODE_STR_TO_INT(setup.vsync_mode);
+  int video_vsync_mode = video.vsync_mode;
+
+  // if setup and video vsync mode are already matching, nothing do do
+  if (setup_vsync_mode == video_vsync_mode)
     return;
-  }
 
-  if (change_fullscreen ||
-      change_window_scaling_percent)
+  // if renderer is using OpenGL, vsync mode can directly be changed
+  SDLSetScreenVsyncMode(setup.vsync_mode);
+
+  // if vsync mode unchanged, try re-creating renderer to set vsync mode
+  if (video.vsync_mode == video_vsync_mode)
   {
     Bitmap *tmp_backbuffer = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
 
-    // save backbuffer content which gets lost when toggling fullscreen mode
+    // save backbuffer content which gets lost when re-creating screen
     BlitBitmap(backbuffer, tmp_backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
 
-    if (change_window_scaling_percent)
-    {
-      // keep window mode, but change window scaling
-      video.fullscreen_enabled = TRUE;         // force new window scaling
-    }
+    // force re-creating screen and renderer to set new vsync mode
+    video.fullscreen_enabled = !setup.fullscreen;
+
+    // when creating new renderer, destroy textures linked to old renderer
+    FreeAllImageTextures();    // needs old renderer to free the textures
 
-    // toggle fullscreen
+    // re-create screen and renderer (including change of vsync mode)
     ChangeVideoModeIfNeeded(setup.fullscreen);
 
     // set setup value according to successfully changed fullscreen mode
@@ -9260,12 +9403,17 @@ void ToggleFullscreenOrChangeWindowScalingIfNeeded(void)
 
     // restore backbuffer content from temporary backbuffer backup bitmap
     BlitBitmap(tmp_backbuffer, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
-
     FreeBitmap(tmp_backbuffer);
 
     // update visible window/screen
     BlitBitmap(backbuffer, window, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
+
+    // when changing vsync mode, re-create textures for new renderer
+    InitImageTextures();
   }
+
+  // set setup value according to successfully changed vsync mode
+  setup.vsync_mode = VSYNC_MODE_INT_TO_STR(video.vsync_mode);
 }
 
 static void JoinRectangles(int *x, int *y, int *width, int *height,
@@ -9285,6 +9433,8 @@ void SetAnimStatus(int anim_status_new)
 {
   if (anim_status_new == GAME_MODE_MAIN)
     anim_status_new = GAME_MODE_PSEUDO_MAINONLY;
+  else if (anim_status_new == GAME_MODE_NAMES)
+    anim_status_new = GAME_MODE_PSEUDO_NAMESONLY;
   else if (anim_status_new == GAME_MODE_SCORES)
     anim_status_new = GAME_MODE_PSEUDO_SCORESOLD;
 
@@ -9294,7 +9444,11 @@ void SetAnimStatus(int anim_status_new)
   if ((global.anim_status      == GAME_MODE_PSEUDO_MAINONLY &&
        global.anim_status_next == GAME_MODE_PSEUDO_TYPENAME) ||
       (global.anim_status      == GAME_MODE_PSEUDO_TYPENAME &&
-       global.anim_status_next == GAME_MODE_PSEUDO_MAINONLY))
+       global.anim_status_next == GAME_MODE_PSEUDO_MAINONLY) ||
+      (global.anim_status      == GAME_MODE_PSEUDO_NAMESONLY &&
+       global.anim_status_next == GAME_MODE_PSEUDO_TYPENAMES) ||
+      (global.anim_status      == GAME_MODE_PSEUDO_TYPENAMES &&
+       global.anim_status_next == GAME_MODE_PSEUDO_NAMESONLY))
     global.anim_status = global.anim_status_next;
 }
 
@@ -9451,7 +9605,7 @@ void ChangeViewportPropertiesIfNeeded(void)
     init_gfx_buffers = TRUE;
     init_gadgets_and_anims = TRUE;
 
-    // printf("::: video: init_video_buffer, init_gfx_buffers\n");
+    // Debug("tools:viewport", "video: init_video_buffer, init_gfx_buffers");
   }
 
   if (new_scr_fieldx != SCR_FIELDX ||
@@ -9462,7 +9616,7 @@ void ChangeViewportPropertiesIfNeeded(void)
     SCR_FIELDX = new_scr_fieldx;
     SCR_FIELDY = new_scr_fieldy;
 
-    // printf("::: new_scr_fieldx != SCR_FIELDX ...\n");
+    // Debug("tools:viewport", "new_scr_fieldx != SCR_FIELDX ...");
   }
 
   if (new_sx != SX ||
@@ -9532,7 +9686,7 @@ void ChangeViewportPropertiesIfNeeded(void)
 
     if (new_tilesize_var != TILESIZE_VAR)
     {
-      // printf("::: new_tilesize_var != TILESIZE_VAR\n");
+      // Debug("tools:viewport", "new_tilesize_var != TILESIZE_VAR");
 
       // changing tile size invalidates scroll values of engine snapshots
       FreeEngineSnapshotSingle();
@@ -9566,13 +9720,13 @@ void ChangeViewportPropertiesIfNeeded(void)
     init_gfx_buffers = TRUE;
     init_gadgets_and_anims = TRUE;
 
-    // printf("::: viewports: init_gfx_buffers\n");
-    // printf("::: viewports: init_gadgets_and_anims\n");
+    // Debug("tools:viewport", "viewports: init_gfx_buffers");
+    // Debug("tools:viewport", "viewports: init_gadgets_and_anims");
   }
 
   if (init_gfx_buffers)
   {
-    // printf("::: init_gfx_buffers\n");
+    // Debug("tools:viewport", "init_gfx_buffers");
 
     SCR_FIELDX = new_scr_fieldx_buffers;
     SCR_FIELDY = new_scr_fieldy_buffers;
@@ -9588,7 +9742,7 @@ void ChangeViewportPropertiesIfNeeded(void)
 
   if (init_video_buffer)
   {
-    // printf("::: init_video_buffer\n");
+    // Debug("tools:viewport", "init_video_buffer");
 
     FreeAllImageTextures();    // needs old renderer to free the textures
 
@@ -9598,7 +9752,7 @@ void ChangeViewportPropertiesIfNeeded(void)
 
   if (init_gadgets_and_anims)
   {
-    // printf("::: init_gadgets_and_anims\n");
+    // Debug("tools:viewport", "init_gadgets_and_anims");
 
     InitGadgets();
     InitGlobalAnimations();
@@ -9606,6 +9760,6 @@ void ChangeViewportPropertiesIfNeeded(void)
 
   if (init_em_graphics)
   {
-      InitGraphicInfo_EM();
+    InitGraphicInfo_EM();
   }
 }