fixed and improved single step mode for all game engines
[rocksndiamonds.git] / src / tools.c
index 76fa6c484a282c8a2170ac80b5a997b7d66d6e46..d15f800e638e3534adc600d1db7b08694efcc5a7 100644 (file)
@@ -192,16 +192,196 @@ static char *print_if_not_empty(int element)
   return s;
 }
 
-void DumpTile(int x, int y)
+int correctLevelPosX_EM(int lx)
 {
-  int sx = SCREENX(x);
-  int sy = SCREENY(y);
+  lx -= 1;
+  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
 
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+  return lx;
+}
+
+int correctLevelPosY_EM(int ly)
+{
+  ly -= 1;
+  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
+
+  return ly;
+}
+
+static int getFieldbufferOffsetX_RND()
+{
+  int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
+  int dx = (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
+  int dx_var = dx * TILESIZE_VAR / TILESIZE;
+  int fx = FX;
+
+  if (EVEN(SCR_FIELDX))
   {
-    x--;
-    y--;
+    int ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
+
+    if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
+      fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
+    else
+      fx += (dx_var > 0 ? TILEX_VAR : 0);
   }
+  else
+  {
+    fx += dx_var;
+  }
+
+  if (full_lev_fieldx <= SCR_FIELDX)
+  {
+    if (EVEN(SCR_FIELDX))
+      fx = 2 * TILEX_VAR - (ODD(lev_fieldx)  ? TILEX_VAR / 2 : 0);
+    else
+      fx = 2 * TILEX_VAR - (EVEN(lev_fieldx) ? TILEX_VAR / 2 : 0);
+  }
+
+  return fx;
+}
+
+static int getFieldbufferOffsetY_RND()
+{
+  int full_lev_fieldy = lev_fieldy + (BorderElement != EL_EMPTY ? 2 : 0);
+  int dy = (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
+  int dy_var = dy * TILESIZE_VAR / TILESIZE;
+  int fy = FY;
+
+  if (EVEN(SCR_FIELDY))
+  {
+    int ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
+
+    if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
+      fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
+    else
+      fy += (dy_var > 0 ? TILEY_VAR : 0);
+  }
+  else
+  {
+    fy += dy_var;
+  }
+
+  if (full_lev_fieldy <= SCR_FIELDY)
+  {
+    if (EVEN(SCR_FIELDY))
+      fy = 2 * TILEY_VAR - (ODD(lev_fieldy)  ? TILEY_VAR / 2 : 0);
+    else
+      fy = 2 * TILEY_VAR - (EVEN(lev_fieldy) ? TILEY_VAR / 2 : 0);
+  }
+
+  return fy;
+}
+
+static int getLevelFromScreenX_RND(int sx)
+{
+  int fx = getFieldbufferOffsetX_RND();
+  int dx = fx - FX;
+  int px = sx - SX;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
+
+  return lx;
+}
+
+static int getLevelFromScreenY_RND(int sy)
+{
+  int fy = getFieldbufferOffsetY_RND();
+  int dy = fy - FY;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  return ly;
+}
+
+static int getLevelFromScreenX_EM(int sx)
+{
+  int level_xsize = level.native_em_level->lev->width;
+  int full_xsize = level_xsize * TILESIZE_VAR;
+
+  sx -= (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
+
+  int fx = getFieldbufferOffsetX_EM();
+  int dx = fx;
+  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 full_ysize = level_ysize * TILESIZE_VAR;
+
+  sy -= (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
+
+  int fy = getFieldbufferOffsetY_EM();
+  int dy = fy;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  ly = correctLevelPosY_EM(ly);
+
+  return ly;
+}
+
+static int getLevelFromScreenX_SP(int sx)
+{
+  int menBorder = setup.sp_show_border_elements;
+  int level_xsize = level.native_sp_level->width;
+  int full_xsize = (level_xsize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
+
+  sx += (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
+
+  int fx = getFieldbufferOffsetX_SP();
+  int dx = fx - FX;
+  int px = sx - SX;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
+
+  return lx;
+}
+
+static int getLevelFromScreenY_SP(int sy)
+{
+  int menBorder = setup.sp_show_border_elements;
+  int level_ysize = level.native_sp_level->height;
+  int full_ysize = (level_ysize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
+
+  sy += (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
+
+  int fy = getFieldbufferOffsetY_SP();
+  int dy = fy - FY;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  return ly;
+}
+
+int getLevelFromScreenX(int x)
+{
+  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+    return getLevelFromScreenX_EM(x);
+  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
+    return getLevelFromScreenX_SP(x);
+  else
+    return getLevelFromScreenX_RND(x);
+}
+
+int getLevelFromScreenY(int y)
+{
+  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+    return getLevelFromScreenY_EM(y);
+  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
+    return getLevelFromScreenY_SP(y);
+  else
+    return getLevelFromScreenY_RND(y);
+}
+
+void DumpTile(int x, int y)
+{
+  int sx = SCREENX(x);
+  int sy = SCREENY(y);
 
   printf_line("-", 79);
   printf("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
@@ -229,9 +409,18 @@ void DumpTile(int x, int 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");
 }
 
+void DumpTileFromScreen(int sx, int sy)
+{
+  int lx = getLevelFromScreenX(sx);
+  int ly = getLevelFromScreenY(sy);
+
+  DumpTile(lx, ly);
+}
+
 void SetDrawtoField(int mode)
 {
   if (mode == DRAW_TO_FIELDBUFFER)
@@ -410,58 +599,8 @@ void DrawMaskedBorderToTarget(int draw_target)
 
 void BlitScreenToBitmap_RND(Bitmap *target_bitmap)
 {
-  int fx = FX, fy = FY;
-  int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
-  int full_lev_fieldy = lev_fieldy + (BorderElement != EL_EMPTY ? 2 : 0);
-
-  int dx = (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
-  int dy = (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
-  int dx_var = dx * TILESIZE_VAR / TILESIZE;
-  int dy_var = dy * TILESIZE_VAR / TILESIZE;
-  int ffx, ffy;
-
-  ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
-  ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
-
-  if (EVEN(SCR_FIELDX))
-  {
-    if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
-      fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
-    else
-      fx += (dx_var > 0 ? TILEX_VAR : 0);
-  }
-  else
-  {
-    fx += dx_var;
-  }
-
-  if (EVEN(SCR_FIELDY))
-  {
-    if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
-      fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
-    else
-      fy += (dy_var > 0 ? TILEY_VAR : 0);
-  }
-  else
-  {
-    fy += dy_var;
-  }
-
-  if (full_lev_fieldx <= SCR_FIELDX)
-  {
-    if (EVEN(SCR_FIELDX))
-      fx = 2 * TILEX_VAR - (ODD(lev_fieldx)  ? TILEX_VAR / 2 : 0);
-    else
-      fx = 2 * TILEX_VAR - (EVEN(lev_fieldx) ? TILEX_VAR / 2 : 0);
-  }
-
-  if (full_lev_fieldy <= SCR_FIELDY)
-  {
-    if (EVEN(SCR_FIELDY))
-      fy = 2 * TILEY_VAR - (ODD(lev_fieldy)  ? TILEY_VAR / 2 : 0);
-    else
-      fy = 2 * TILEY_VAR - (EVEN(lev_fieldy) ? TILEY_VAR / 2 : 0);
-  }
+  int fx = getFieldbufferOffsetX_RND();
+  int fy = getFieldbufferOffsetY_RND();
 
   BlitBitmap(drawto_field, target_bitmap, fx, fy, SXSIZE, SYSIZE, SX, SY);
 }
@@ -528,6 +667,22 @@ static void PrintFrameTimeDebugging()
 }
 #endif
 
+static int unifiedRedrawMask(int mask)
+{
+  if (mask & REDRAW_ALL)
+    return REDRAW_ALL;
+
+  if (mask & REDRAW_FIELD && mask & REDRAW_DOORS)
+    return REDRAW_ALL;
+
+  return mask;
+}
+
+static boolean equalRedrawMasks(int mask_1, int mask_2)
+{
+  return unifiedRedrawMask(mask_1) == unifiedRedrawMask(mask_2);
+}
+
 void BackToFront()
 {
   static int last_redraw_mask = REDRAW_NONE;
@@ -550,6 +705,10 @@ void BackToFront()
   if (redraw_mask & REDRAW_FPS)
     DrawFramesPerSecond();
 
+  // remove playfield redraw before potentially merging with doors redraw
+  if (DrawingDeactivated(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE))
+    redraw_mask &= ~REDRAW_FIELD;
+
   // redraw complete window if both playfield and (some) doors need redraw
   if (redraw_mask & REDRAW_FIELD && redraw_mask & REDRAW_DOORS)
     redraw_mask = REDRAW_ALL;
@@ -570,14 +729,44 @@ void BackToFront()
   }
   else if (redraw_mask & REDRAW_DOORS)
   {
+    // merge door areas to prevent calling screen redraw more than once
+    int x1 = WIN_XSIZE;
+    int y1 = WIN_YSIZE;
+    int x2 = 0;
+    int y2 = 0;
+
     if (redraw_mask & REDRAW_DOOR_1)
-      BlitBitmap(backbuffer, window, DX, DY, DXSIZE, DYSIZE, DX, DY);
+    {
+      x1 = MIN(x1, DX);
+      y1 = MIN(y1, DY);
+      x2 = MAX(x2, DX + DXSIZE);
+      y2 = MAX(y2, DY + DYSIZE);
+    }
 
     if (redraw_mask & REDRAW_DOOR_2)
-      BlitBitmap(backbuffer, window, VX, VY, VXSIZE, VYSIZE, VX, VY);
+    {
+      x1 = MIN(x1, VX);
+      y1 = MIN(y1, VY);
+      x2 = MAX(x2, VX + VXSIZE);
+      y2 = MAX(y2, VY + VYSIZE);
+    }
 
     if (redraw_mask & REDRAW_DOOR_3)
-      BlitBitmap(backbuffer, window, EX, EY, EXSIZE, EYSIZE, EX, EY);
+    {
+      x1 = MIN(x1, EX);
+      y1 = MIN(y1, EY);
+      x2 = MAX(x2, EX + EXSIZE);
+      y2 = MAX(y2, EY + EYSIZE);
+    }
+
+    // make sure that at least one pixel is blitted, and inside the screen
+    // (else nothing is blitted, causing the animations not to be updated)
+    x1 = MIN(MAX(0, x1), WIN_XSIZE - 1);
+    y1 = MIN(MAX(0, y1), WIN_YSIZE - 1);
+    x2 = MIN(MAX(1, x2), WIN_XSIZE);
+    y2 = MIN(MAX(1, y2), WIN_YSIZE);
+
+    BlitBitmap(backbuffer, window, x1, y1, x2 - x1, y2 - y1, x1, y1);
   }
 
   redraw_mask = REDRAW_NONE;
@@ -598,9 +787,10 @@ void BackToFront_WithFrameDelay(unsigned int frame_delay_value)
   SetVideoFrameDelay(frame_delay_value_old);
 }
 
+static int fade_type_skip = FADE_TYPE_NONE;
+
 static void FadeExt(int fade_mask, int fade_mode, int fade_type)
 {
-  static int fade_type_skip = FADE_TYPE_NONE;
   void (*draw_border_function)(void) = NULL;
   int x, y, width, height;
   int fade_delay, post_delay;
@@ -692,7 +882,8 @@ static void SetScreenStates_BeforeFadingIn()
   global.anim_status = global.anim_status_next;
 
   // store backbuffer with all animations that will be started after fading in
-  PrepareFadeBitmap(DRAW_TO_FADE_TARGET);
+  if (fade_type_skip != FADE_MODE_SKIP_FADE_IN)
+    PrepareFadeBitmap(DRAW_TO_FADE_TARGET);
 
   // set screen mode for animations back to fading
   global.anim_status = GAME_MODE_PSEUDO_FADING;
@@ -704,10 +895,6 @@ static void SetScreenStates_AfterFadingIn()
   gfx.fade_border_source_status = global.border_status;
 
   global.anim_status = global.anim_status_next;
-
-  // force update of global animation status in case of rapid screen changes
-  redraw_mask = REDRAW_ALL;
-  BackToFront();
 }
 
 static void SetScreenStates_BeforeFadingOut()
@@ -719,7 +906,8 @@ static void SetScreenStates_BeforeFadingOut()
   global.anim_status = GAME_MODE_PSEUDO_FADING;
 
   // store backbuffer with all animations that will be stopped for fading out
-  PrepareFadeBitmap(DRAW_TO_FADE_SOURCE);
+  if (fade_type_skip != FADE_MODE_SKIP_FADE_OUT)
+    PrepareFadeBitmap(DRAW_TO_FADE_SOURCE);
 }
 
 static void SetScreenStates_AfterFadingOut()
@@ -745,13 +933,27 @@ void FadeIn(int fade_mask)
   FADE_SXSIZE = FULL_SXSIZE;
   FADE_SYSIZE = FULL_SYSIZE;
 
+  if (game_status == GAME_MODE_PLAYING &&
+      strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
+    SetOverlayActive(TRUE);
+
   SetScreenStates_AfterFadingIn();
+
+  // force update of global animation status in case of rapid screen changes
+  redraw_mask = REDRAW_ALL;
+  BackToFront();
 }
 
 void FadeOut(int fade_mask)
 {
+  // update screen if areas covered by "fade_mask" and "redraw_mask" differ
+  if (!equalRedrawMasks(fade_mask, redraw_mask))
+    BackToFront();
+
   SetScreenStates_BeforeFadingOut();
 
+  SetOverlayActive(FALSE);
+
 #if 0
   DrawMaskedBorder(REDRAW_ALL);
 #endif
@@ -1169,52 +1371,64 @@ int getGraphicAnimationFrame(int graphic, int sync_frame)
                           sync_frame);
 }
 
-void getSizedGraphicSourceExt(int graphic, int frame, int tilesize,
-                             Bitmap **bitmap, int *x, int *y,
-                             boolean get_backside)
+void getGraphicSourceBitmap(int graphic, int tilesize, Bitmap **bitmap)
 {
   struct GraphicInfo *g = &graphic_info[graphic];
-  Bitmap *src_bitmap = g->bitmap;
-  int src_x = g->src_x + (get_backside ? g->offset2_x : 0);
-  int src_y = g->src_y + (get_backside ? g->offset2_y : 0);
   int tilesize_capped = MIN(MAX(1, tilesize), TILESIZE);
 
-  // if no in-game graphics defined, always use standard graphic size
-  if (g->bitmaps[IMG_BITMAP_GAME] == NULL)
-    tilesize = TILESIZE;
-
   if (tilesize == gfx.standard_tile_size)
-    src_bitmap = g->bitmaps[IMG_BITMAP_STANDARD];
+    *bitmap = g->bitmaps[IMG_BITMAP_STANDARD];
   else if (tilesize == game.tile_size)
-    src_bitmap = g->bitmaps[IMG_BITMAP_GAME];
+    *bitmap = g->bitmaps[IMG_BITMAP_GAME];
   else
-    src_bitmap = g->bitmaps[IMG_BITMAP_1x1 - log_2(tilesize_capped)];
+    *bitmap = g->bitmaps[IMG_BITMAP_1x1 - log_2(tilesize_capped)];
+}
+
+void getGraphicSourceXY(int graphic, int frame, int *x, int *y,
+                       boolean get_backside)
+{
+  struct GraphicInfo *g = &graphic_info[graphic];
+  int src_x = g->src_x + (get_backside ? g->offset2_x : 0);
+  int src_y = g->src_y + (get_backside ? g->offset2_y : 0);
 
   if (g->offset_y == 0)                /* frames are ordered horizontally */
   {
     int max_width = g->anim_frames_per_line * g->width;
     int pos = (src_y / g->height) * max_width + src_x + frame * g->offset_x;
 
-    src_x = pos % max_width;
-    src_y = src_y % g->height + pos / max_width * g->height;
+    *x = pos % max_width;
+    *y = src_y % g->height + pos / max_width * g->height;
   }
   else if (g->offset_x == 0)   /* frames are ordered vertically */
   {
     int max_height = g->anim_frames_per_line * g->height;
     int pos = (src_x / g->width) * max_height + src_y + frame * g->offset_y;
 
-    src_x = src_x % g->width + pos / max_height * g->width;
-    src_y = pos % max_height;
+    *x = src_x % g->width + pos / max_height * g->width;
+    *y = pos % max_height;
   }
   else                         /* frames are ordered diagonally */
   {
-    src_x = src_x + frame * g->offset_x;
-    src_y = src_y + frame * g->offset_y;
+    *x = src_x + frame * g->offset_x;
+    *y = src_y + frame * g->offset_y;
   }
+}
+
+void getSizedGraphicSourceExt(int graphic, int frame, int tilesize,
+                             Bitmap **bitmap, int *x, int *y,
+                             boolean get_backside)
+{
+  struct GraphicInfo *g = &graphic_info[graphic];
+
+  // if no in-game graphics defined, always use standard graphic size
+  if (g->bitmaps[IMG_BITMAP_GAME] == NULL)
+    tilesize = TILESIZE;
 
-  *bitmap = src_bitmap;
-  *x = src_x * tilesize / g->tile_size;
-  *y = src_y * tilesize / g->tile_size;
+  getGraphicSourceBitmap(graphic, tilesize, bitmap);
+  getGraphicSourceXY(graphic, frame, x, y, get_backside);
+
+  *x = *x * tilesize / g->tile_size;
+  *y = *y * tilesize / g->tile_size;
 }
 
 void getFixedGraphicSourceExt(int graphic, int frame, Bitmap **bitmap,
@@ -1244,40 +1458,8 @@ void getMiniGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
 inline static void getGraphicSourceExt(int graphic, int frame, Bitmap **bitmap,
                                       int *x, int *y, boolean get_backside)
 {
-  struct GraphicInfo *g = &graphic_info[graphic];
-  int src_x = g->src_x + (get_backside ? g->offset2_x : 0);
-  int src_y = g->src_y + (get_backside ? g->offset2_y : 0);
-
-  if (TILESIZE_VAR != TILESIZE)
-    return getSizedGraphicSourceExt(graphic, frame, TILESIZE_VAR, bitmap, x, y,
-                                   get_backside);
-
-  *bitmap = g->bitmap;
-
-  if (g->offset_y == 0)                /* frames are ordered horizontally */
-  {
-    int max_width = g->anim_frames_per_line * g->width;
-    int pos = (src_y / g->height) * max_width + src_x + frame * g->offset_x;
-
-    *x = pos % max_width;
-    *y = src_y % g->height + pos / max_width * g->height;
-  }
-  else if (g->offset_x == 0)   /* frames are ordered vertically */
-  {
-    int max_height = g->anim_frames_per_line * g->height;
-    int pos = (src_x / g->width) * max_height + src_y + frame * g->offset_y;
-
-    *x = src_x % g->width + pos / max_height * g->width;
-    *y = pos % max_height;
-  }
-  else                         /* frames are ordered diagonally */
-  {
-    *x = src_x + frame * g->offset_x;
-    *y = src_y + frame * g->offset_y;
-  }
-
-  *x = *x * TILESIZE_VAR / g->tile_size;
-  *y = *y * TILESIZE_VAR / g->tile_size;
+  getSizedGraphicSourceExt(graphic, frame, TILESIZE_VAR, bitmap, x, y,
+                          get_backside);
 }
 
 void getGraphicSource(int graphic, int frame, Bitmap **bitmap, int *x, int *y)
@@ -1730,6 +1912,9 @@ static void DrawLevelFieldCrumbledInnerCorners(int x, int y, int dx, int dy,
   int width, height, cx, cy;
   int sx = SCREENX(x), sy = SCREENY(y);
   int crumbled_border_size = graphic_info[graphic].border_size;
+  int crumbled_tile_size = graphic_info[graphic].tile_size;
+  int crumbled_border_size_var =
+    crumbled_border_size * TILESIZE_VAR / crumbled_tile_size;
   int i;
 
   getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
@@ -1757,8 +1942,8 @@ static void DrawLevelFieldCrumbledInnerCorners(int x, int y, int dx, int dy,
 
   getGraphicSource(graphic, 1, &src_bitmap, &src_x, &src_y);
 
-  width  = crumbled_border_size * TILESIZE_VAR / TILESIZE;
-  height = crumbled_border_size * TILESIZE_VAR / TILESIZE;
+  width  = crumbled_border_size_var;
+  height = crumbled_border_size_var;
   cx = (dx > 0 ? TILESIZE_VAR - width  : 0);
   cy = (dy > 0 ? TILESIZE_VAR - height : 0);
 
@@ -1774,7 +1959,9 @@ static void DrawLevelFieldCrumbledBorders(int x, int y, int graphic, int frame,
   int width, height, bx, by, cx, cy;
   int sx = SCREENX(x), sy = SCREENY(y);
   int crumbled_border_size = graphic_info[graphic].border_size;
-  int crumbled_border_size_var = crumbled_border_size * TILESIZE_VAR / TILESIZE;
+  int crumbled_tile_size = graphic_info[graphic].tile_size;
+  int crumbled_border_size_var =
+    crumbled_border_size * TILESIZE_VAR / crumbled_tile_size;
   int crumbled_border_pos_var = TILESIZE_VAR - crumbled_border_size_var;
   int i;
 
@@ -1793,7 +1980,7 @@ static void DrawLevelFieldCrumbledBorders(int x, int y, int graphic, int frame,
 
   /* (remaining middle border part must be at least as big as corner part) */
   if (!(graphic_info[graphic].style & STYLE_ACCURATE_BORDERS) ||
-      crumbled_border_size >= TILESIZE / 3)
+      crumbled_border_size_var >= TILESIZE_VAR / 3)
     return;
 
   /* correct corners of crumbled border, if needed */
@@ -1861,12 +2048,12 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
 
   element = TILE_GFX_ELEMENT(x, y);
 
-  /* crumble field itself */
-  if (IS_CRUMBLED_TILE(x, y, element))
+  if (IS_CRUMBLED_TILE(x, y, element))         /* crumble field itself */
   {
     if (!IN_SCR_FIELD(sx, sy))
       return;
 
+    /* crumble field borders towards direct neighbour fields */
     for (i = 0; i < 4; i++)
     {
       int xx = x + xy[i][0];
@@ -1884,6 +2071,7 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
       DrawLevelFieldCrumbledBorders(x, y, graphic, frame, i);
     }
 
+    /* crumble inner field corners towards corner neighbour fields */
     if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
        graphic_info[graphic].anim_frames == 2)
     {
@@ -1898,8 +2086,9 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
 
     MarkTileDirty(sx, sy);
   }
-  else         /* center field not crumbled -- crumble neighbour fields */
+  else         /* center field is not crumbled -- crumble neighbour fields */
   {
+    /* crumble field borders of direct neighbour fields */
     for (i = 0; i < 4; i++)
     {
       int xx = x + xy[i][0];
@@ -1925,6 +2114,37 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
 
       MarkTileDirty(sxx, syy);
     }
+
+    /* crumble inner field corners of corner neighbour fields */
+    for (i = 0; i < 4; i++)
+    {
+      int dx = (i & 1 ? +1 : -1);
+      int dy = (i & 2 ? +1 : -1);
+      int xx = x + dx;
+      int yy = y + dy;
+      int sxx = sx + dx;
+      int syy = sy + dy;
+
+      if (!IN_LEV_FIELD(xx, yy) ||
+         !IN_SCR_FIELD(sxx, syy))
+       continue;
+
+      if (Feld[xx][yy] == EL_ELEMENT_SNAPPING)
+       continue;
+
+      element = TILE_GFX_ELEMENT(xx, yy);
+
+      if (!IS_CRUMBLED_TILE(xx, yy, element))
+       continue;
+
+      graphic = el_act2crm(element, ACTION_DEFAULT);
+
+      if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
+         graphic_info[graphic].anim_frames == 2)
+       DrawLevelFieldCrumbledInnerCorners(xx, yy, -dx, -dy, graphic);
+
+      MarkTileDirty(sxx, syy);
+    }
   }
 }
 
@@ -1974,6 +2194,7 @@ void DrawLevelFieldCrumbledNeighbours(int x, int y)
   };
   int i;
 
+  /* crumble direct neighbour fields (required for field borders) */
   for (i = 0; i < 4; i++)
   {
     int xx = x + xy[i][0];
@@ -1989,6 +2210,30 @@ void DrawLevelFieldCrumbledNeighbours(int x, int y)
 
     DrawLevelField(xx, yy);
   }
+
+  /* crumble corner neighbour fields (required for inner field corners) */
+  for (i = 0; i < 4; i++)
+  {
+    int dx = (i & 1 ? +1 : -1);
+    int dy = (i & 2 ? +1 : -1);
+    int xx = x + dx;
+    int yy = y + dy;
+    int sxx = sx + dx;
+    int syy = sy + dy;
+
+    if (!IN_LEV_FIELD(xx, yy) ||
+       !IN_SCR_FIELD(sxx, syy) ||
+       !GFX_CRUMBLED(Feld[xx][yy]) ||
+       IS_MOVING(xx, yy))
+      continue;
+
+    int element = TILE_GFX_ELEMENT(xx, yy);
+    int graphic = el_act2crm(element, ACTION_DEFAULT);
+
+    if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
+       graphic_info[graphic].anim_frames == 2)
+      DrawLevelField(xx, yy);
+  }
 }
 
 static int getBorderElement(int x, int y)
@@ -2090,6 +2335,10 @@ void DrawScreenField(int x, int y)
       int newly = ly + (dir == MV_UP   ? -1 : dir == MV_DOWN  ? +1 : 0);
 
       DrawLevelElementThruMask(newlx, newly, EL_ACID);
+
+      // prevent target field from being drawn again (but without masking)
+      // (this would happen if target field is scanned after moving element)
+      Stop[newlx][newly] = TRUE;
     }
   }
   else if (IS_BLOCKED(lx, ly))
@@ -3487,6 +3736,9 @@ void WaitForEventToContinue()
 {
   boolean still_wait = TRUE;
 
+  if (program.headless)
+    return;
+
   /* simulate releasing mouse button over last gadget, if still pressed */
   if (button_status)
     HandleGadgets(-1, -1, 0);
@@ -3507,6 +3759,10 @@ void WaitForEventToContinue()
       {
        case EVENT_BUTTONPRESS:
        case EVENT_KEYPRESS:
+#if defined(TARGET_SDL2)
+        case SDL_CONTROLLERBUTTONDOWN:
+#endif
+        case SDL_JOYBUTTONDOWN:
          still_wait = FALSE;
          break;
 
@@ -3634,8 +3890,24 @@ static int RequestHandleEvents(unsigned int req_state)
            break;
          }
 
+#if defined(TARGET_SDL2)
+         case SDL_WINDOWEVENT:
+           HandleWindowEvent((WindowEvent *) &event);
+           break;
+
+         case SDL_APP_WILLENTERBACKGROUND:
+         case SDL_APP_DIDENTERBACKGROUND:
+         case SDL_APP_WILLENTERFOREGROUND:
+         case SDL_APP_DIDENTERFOREGROUND:
+           HandlePauseResumeEvent((PauseResumeEvent *) &event);
+           break;
+#endif
+
          case EVENT_KEYPRESS:
-           switch (GetEventKey((KeyEvent *)&event, TRUE))
+         {
+           Key key = GetEventKey((KeyEvent *)&event, TRUE);
+
+           switch (key)
            {
              case KSYM_space:
                if (req_state & REQ_CONFIRM)
@@ -3644,7 +3916,11 @@ static int RequestHandleEvents(unsigned int req_state)
 
              case KSYM_Return:
 #if defined(TARGET_SDL2)
+             case KSYM_Select:
              case KSYM_Menu:
+#if defined(KSYM_Rewind)
+             case KSYM_Rewind:         /* for Amazon Fire TV remote */
+#endif
 #endif
                result = 1;
                break;
@@ -3652,22 +3928,57 @@ static int RequestHandleEvents(unsigned int req_state)
              case KSYM_Escape:
 #if defined(TARGET_SDL2)
              case KSYM_Back:
+#if defined(KSYM_FastForward)
+             case KSYM_FastForward:    /* for Amazon Fire TV remote */
+#endif
 #endif
                result = 0;
                break;
 
              default:
+               HandleKeysDebug(key);
                break;
            }
 
            if (req_state & REQ_PLAYER)
              result = 0;
+
            break;
+         }
 
          case EVENT_KEYRELEASE:
            ClearPlayerAction();
            break;
 
+#if defined(TARGET_SDL2)
+         case SDL_CONTROLLERBUTTONDOWN:
+           switch (event.cbutton.button)
+           {
+             case SDL_CONTROLLER_BUTTON_A:
+             case SDL_CONTROLLER_BUTTON_X:
+             case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
+               result = 1;
+               break;
+
+             case SDL_CONTROLLER_BUTTON_B:
+             case SDL_CONTROLLER_BUTTON_Y:
+             case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
+             case SDL_CONTROLLER_BUTTON_BACK:
+               result = 0;
+               break;
+           }
+
+           if (req_state & REQ_PLAYER)
+             result = 0;
+
+           break;
+
+         case SDL_CONTROLLERBUTTONUP:
+           HandleJoystickEvent(&event);
+           ClearPlayerAction();
+           break;
+#endif
+
          default:
            HandleOtherEvents(&event);
            break;
@@ -3969,10 +4280,19 @@ static boolean RequestEnvelope(char *text, unsigned int req_state)
 
 boolean Request(char *text, unsigned int req_state)
 {
+  boolean overlay_active = GetOverlayActive();
+  boolean result;
+
+  SetOverlayActive(FALSE);
+
   if (global.use_envelope_request)
-    return RequestEnvelope(text, req_state);
+    result = RequestEnvelope(text, req_state);
   else
-    return RequestDoor(text, req_state);
+    result = RequestDoor(text, req_state);
+
+  SetOverlayActive(overlay_active);
+
+  return result;
 }
 
 static int compareDoorPartOrderInfo(const void *object1, const void *object2)
@@ -4424,7 +4744,8 @@ unsigned int MoveDoor(unsigned int door_state)
          int sync_frame = kk_door * door_delay_value;
          int frame = getGraphicAnimationFrame(dpc->graphic, sync_frame);
 
-         getGraphicSource(dpc->graphic, frame, &bitmap, &g_src_x, &g_src_y);
+         getFixedGraphicSource(dpc->graphic, frame, &bitmap,
+                               &g_src_x, &g_src_y);
        }
 
        // draw door panel
@@ -7228,6 +7549,7 @@ inline static void set_crumbled_graphics_EM(struct GraphicInfo_EM *g_em,
                     &g_em->crumbled_src_x, &g_em->crumbled_src_y);
 
     g_em->crumbled_border_size = graphic_info[crumbled].border_size;
+    g_em->crumbled_tile_size = graphic_info[crumbled].tile_size;
 
     g_em->has_crumbled_graphics = TRUE;
   }
@@ -7237,6 +7559,7 @@ inline static void set_crumbled_graphics_EM(struct GraphicInfo_EM *g_em,
     g_em->crumbled_src_x = 0;
     g_em->crumbled_src_y = 0;
     g_em->crumbled_border_size = 0;
+    g_em->crumbled_tile_size = 0;
 
     g_em->has_crumbled_graphics = FALSE;
   }
@@ -7996,42 +8319,38 @@ void CheckSaveEngineSnapshot_EM(byte action[MAX_PLAYERS], int frame,
                                boolean any_player_snapping,
                                boolean any_player_dropping)
 {
-  static boolean player_was_waiting = TRUE;
-
   if (frame == 0 && !any_player_dropping)
   {
-    if (!player_was_waiting)
+    if (!local_player->was_waiting)
     {
-      if (!SaveEngineSnapshotToList())
+      if (!CheckSaveEngineSnapshotToList())
        return;
 
-      player_was_waiting = TRUE;
+      local_player->was_waiting = TRUE;
     }
   }
   else if (any_player_moving || any_player_snapping || any_player_dropping)
   {
-    player_was_waiting = FALSE;
+    local_player->was_waiting = FALSE;
   }
 }
 
 void CheckSaveEngineSnapshot_SP(boolean murphy_is_waiting,
                                boolean murphy_is_dropping)
 {
-  static boolean player_was_waiting = TRUE;
-
   if (murphy_is_waiting)
   {
-    if (!player_was_waiting)
+    if (!local_player->was_waiting)
     {
-      if (!SaveEngineSnapshotToList())
+      if (!CheckSaveEngineSnapshotToList())
        return;
 
-      player_was_waiting = TRUE;
+      local_player->was_waiting = TRUE;
     }
   }
   else
   {
-    player_was_waiting = FALSE;
+    local_player->was_waiting = FALSE;
   }
 }
 
@@ -8051,8 +8370,15 @@ void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
                            boolean murphy_is_dropping)
 {
+  boolean murphy_starts_dropping = FALSE;
+  int i;
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    if (stored_player[i].force_dropping)
+      murphy_starts_dropping = TRUE;
+
   if (tape.single_step && tape.recording && !tape.pausing)
-    if (murphy_is_waiting)
+    if (murphy_is_waiting && !murphy_starts_dropping)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
 
   CheckSaveEngineSnapshot_SP(murphy_is_waiting, murphy_is_dropping);
@@ -8142,7 +8468,37 @@ void PlayMenuMusicExt(int music)
 
 void PlayMenuMusic()
 {
-  PlayMenuMusicExt(menu.music[game_status]);
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    PlayMenuMusicExt(menu.music[game_status]);
+}
+
+void PlayMenuSoundsAndMusic()
+{
+  PlayMenuSound();
+  PlayMenuMusic();
+}
+
+static void FadeMenuSounds()
+{
+  FadeSounds();
+}
+
+static void FadeMenuMusic()
+{
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    FadeMusic();
+}
+
+void FadeMenuSoundsAndMusic()
+{
+  FadeMenuSounds();
+  FadeMenuMusic();
 }
 
 void PlaySoundActivating()
@@ -8238,6 +8594,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_SCORES)
+    anim_status_new = GAME_MODE_PSEUDO_SCORESOLD;
 
   global.anim_status_next = anim_status_new;
 
@@ -8251,6 +8609,9 @@ void SetAnimStatus(int anim_status_new)
 
 void SetGameStatus(int game_status_new)
 {
+  if (game_status_new != game_status)
+    game_status_last_screen = game_status;
+
   game_status = game_status_new;
 
   SetAnimStatus(game_status_new);