rnd-20070822-1-src
[rocksndiamonds.git] / src / tools.c
index 8b348d0b4e335d484b24296cce775b24bd26bbe0..f593aeda20954a08accc0e65f332ecf1d8ce1be0 100644 (file)
@@ -205,16 +205,16 @@ void DrawMaskedBorder_Rect(int x, int y, int width, int height)
 
 void DrawMaskedBorder_FIELD()
 {
-  if (game_status >= GAME_MODE_TITLE &&
-      game_status <= GAME_MODE_PLAYING &&
-      border.draw_masked[game_status])
+  if (global.border_status >= GAME_MODE_TITLE &&
+      global.border_status <= GAME_MODE_PLAYING &&
+      border.draw_masked[global.border_status])
     DrawMaskedBorder_Rect(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
 }
 
 void DrawMaskedBorder_DOOR_1()
 {
   if (border.draw_masked[GFX_SPECIAL_ARG_DOOR] &&
-      (game_status != GAME_MODE_EDITOR ||
+      (global.border_status != GAME_MODE_EDITOR ||
        border.draw_masked[GFX_SPECIAL_ARG_EDITOR]))
     DrawMaskedBorder_Rect(DX, DY, DXSIZE, DYSIZE);
 }
@@ -222,7 +222,7 @@ void DrawMaskedBorder_DOOR_1()
 void DrawMaskedBorder_DOOR_2()
 {
   if (border.draw_masked[GFX_SPECIAL_ARG_DOOR] &&
-      game_status != GAME_MODE_EDITOR)
+      global.border_status != GAME_MODE_EDITOR)
     DrawMaskedBorder_Rect(VX, VY, VXSIZE, VYSIZE);
 }
 
@@ -241,8 +241,9 @@ void DrawMaskedBorder_ALL()
 
 void DrawMaskedBorder(int redraw_mask)
 {
-  /* do not draw masked screen borders when displaying title screens */
-  if (effectiveGameStatus() == GAME_MODE_TITLE)
+  /* never draw masked screen borders on borderless screens */
+  if (effectiveGameStatus() == GAME_MODE_LOADING ||
+      effectiveGameStatus() == GAME_MODE_TITLE)
     return;
 
   if (redraw_mask & REDRAW_ALL)
@@ -448,111 +449,64 @@ void BackToFront()
   redraw_mask = REDRAW_NONE;
 }
 
-void FadeToFront()
+static void FadeCrossSaveBackbuffer()
 {
-#if 0
-  long fading_delay = 300;
-
-  if (setup.fading && (redraw_mask & REDRAW_FIELD))
-  {
-#endif
-
-#if 0
-    int x,y;
+  BlitBitmap(backbuffer, bitmap_db_cross, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
+}
 
-    ClearRectangle(window, REAL_SX,REAL_SY,FULL_SXSIZE,FULL_SYSIZE);
-    FlushDisplay();
+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;
+  Bitmap *bitmap = (fade_mode & FADE_TYPE_TRANSFORM ? bitmap_db_cross : NULL);
+  int x, y, width, height;
+  int fade_delay, post_delay;
 
-    for (i = 0; i < 2 * FULL_SYSIZE; i++)
+  if (fade_type == FADE_TYPE_FADE_OUT)
+  {
+    if (fade_type_skip != FADE_TYPE_NONE)
     {
-      for (y = 0; y < FULL_SYSIZE; y++)
-      {
-       BlitBitmap(backbuffer, window,
-                  REAL_SX,REAL_SY+i, FULL_SXSIZE,1, REAL_SX,REAL_SY+i);
-      }
-      FlushDisplay();
-      Delay(10);
-    }
-#endif
-
 #if 0
-    for (i = 1; i < FULL_SYSIZE; i+=2)
-      BlitBitmap(backbuffer, window,
-                REAL_SX,REAL_SY+i, FULL_SXSIZE,1, REAL_SX,REAL_SY+i);
-    FlushDisplay();
-    Delay(fading_delay);
+      printf("::: skipping %d ... [%d] (X)\n", fade_mode, fade_type_skip);
 #endif
 
-#if 0
-    SetClipOrigin(clip_gc[PIX_FADEMASK], 0, 0);
-    BlitBitmapMasked(backbuffer, window,
-                    REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
-                    REAL_SX,REAL_SY);
-    FlushDisplay();
-    Delay(fading_delay);
-
-    SetClipOrigin(clip_gc[PIX_FADEMASK], -1, -1);
-    BlitBitmapMasked(backbuffer, window,
-                    REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
-                    REAL_SX,REAL_SY);
-    FlushDisplay();
-    Delay(fading_delay);
-
-    SetClipOrigin(clip_gc[PIX_FADEMASK], 0, -1);
-    BlitBitmapMasked(backbuffer, window,
-                    REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
-                    REAL_SX,REAL_SY);
-    FlushDisplay();
-    Delay(fading_delay);
-
-    SetClipOrigin(clip_gc[PIX_FADEMASK], -1, 0);
-    BlitBitmapMasked(backbuffer, window,
-                    REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
-                    REAL_SX,REAL_SY);
-    FlushDisplay();
-    Delay(fading_delay);
+      /* skip all fade operations until specified fade operation */
+      if (fade_type & fade_type_skip)
+       fade_type_skip = FADE_TYPE_NONE;
 
-    redraw_mask &= ~REDRAW_MAIN;
-  }
-#endif
+      return;
+    }
 
-  BackToFront();
-}
+    if (fading.fade_mode & FADE_TYPE_TRANSFORM)
+    {
+      FadeCrossSaveBackbuffer();
 
-void FadeExt(int fade_mask, int fade_mode)
-{
-  static int fade_mode_skip = FADE_MODE_NONE;
-  void (*draw_border_function)(void) = NULL;
-#if 0
-  Bitmap *bitmap = (fade_mode != FADE_MODE_FADE_IN ? bitmap_db_cross : NULL);
-#else
-  Bitmap *bitmap = (fade_mode & FADE_TYPE_TRANSFORM ? bitmap_db_cross : NULL);
-#endif
-  int x, y, width, height;
-  int fade_delay, post_delay;
+      return;
+    }
+  }
 
   redraw_mask |= fade_mask;
 
-  if (fade_mode & FADE_TYPE_SKIP)
+  if (fade_type == FADE_TYPE_SKIP)
   {
 #if 0
-    printf("::: will skip %d ... [%d]\n", fade_mode, fade_mode_skip);
+    printf("::: will skip %d ... [%d]\n", fade_mode, fade_type_skip);
 #endif
 
-    fade_mode_skip = fade_mode;
+    fade_type_skip = fade_mode;
 
     return;
   }
 
-  if (fade_mode_skip & FADE_TYPE_SKIP)
+  if (fade_type_skip != FADE_TYPE_NONE)
   {
 #if 0
-    printf("::: skipping %d ... [%d]\n", fade_mode, fade_mode_skip);
+    printf("::: skipping %d ... [%d]\n", fade_mode, fade_type_skip);
 #endif
 
     /* skip all fade operations until specified fade operation */
-    if (fade_mode & fade_mode_skip)
-      fade_mode_skip = FADE_MODE_NONE;
+    if (fade_type & fade_type_skip)
+      fade_type_skip = FADE_TYPE_NONE;
 
     return;
   }
@@ -575,6 +529,12 @@ void FadeExt(int fade_mask, int fade_mode)
   }
 #endif
 
+  /* !!! what abount fade_mask == REDRAW_FIELD | REDRAW_ALL ??? !!! */
+
+#if 0
+  printf("::: NOW FADING %d ... [%d]\n", fade_mode, fade_type);
+#endif
+
   if (fade_mask & REDRAW_FIELD)
   {
     x = REAL_SX;
@@ -585,7 +545,10 @@ void FadeExt(int fade_mask, int fade_mode)
     fade_delay = fading.fade_delay;
     post_delay = (fade_mode == FADE_MODE_FADE_OUT ? fading.post_delay : 0);
 
-    draw_border_function = DrawMaskedBorder_FIELD;
+    if (border.draw_masked_when_fading)
+      draw_border_function = DrawMaskedBorder_FIELD;   /* update when fading */
+    else
+      DrawMaskedBorder_FIELD();                                /* draw once */
   }
   else         /* REDRAW_ALL */
   {
@@ -620,13 +583,17 @@ void FadeExt(int fade_mask, int fade_mode)
 
 void FadeIn(int fade_mask)
 {
+#if 0
+  global.border_status = game_status;
+#endif
+
 #if 1
   // printf("::: now fading in...\n");
 
   if (fading.fade_mode & FADE_TYPE_TRANSFORM)
-    FadeExt(fade_mask, fading.fade_mode);
+    FadeExt(fade_mask, fading.fade_mode, FADE_TYPE_FADE_IN);
   else
-    FadeExt(fade_mask, FADE_MODE_FADE_IN);
+    FadeExt(fade_mask, FADE_MODE_FADE_IN, FADE_TYPE_FADE_IN);
 #else
 #if 1
   if (fading.fade_mode == FADE_MODE_CROSSFADE)
@@ -644,10 +611,18 @@ void FadeOut(int fade_mask)
 #if 1
   // printf("::: fading.fade_mode == %d\n", fading.fade_mode);
 
+#if 1
+  if (fading.fade_mode & FADE_TYPE_TRANSFORM)
+    FadeExt(fade_mask, fading.fade_mode, FADE_TYPE_FADE_OUT);
+  else
+    FadeExt(fade_mask, FADE_MODE_FADE_OUT, FADE_TYPE_FADE_OUT);
+#else
   if (fading.fade_mode & FADE_TYPE_TRANSFORM)
     FadeCrossSaveBackbuffer();
   else
-    FadeExt(fade_mask, FADE_MODE_FADE_OUT);
+    FadeExt(fade_mask, FADE_MODE_FADE_OUT, FADE_TYPE_FADE_OUT);
+#endif
+
 #else
 #if 1
   if (fading.fade_mode == FADE_MODE_CROSSFADE)
@@ -658,17 +633,18 @@ void FadeOut(int fade_mask)
   FadeExt(fade_mask, FADE_MODE_FADE_OUT);
 #endif
 #endif
+
+#if 1
+  global.border_status = game_status;
+#endif
 }
 
+#if 0
 void FadeCross(int fade_mask)
 {
   FadeExt(fade_mask, FADE_MODE_CROSSFADE);
 }
-
-void FadeCrossSaveBackbuffer()
-{
-  BlitBitmap(backbuffer, bitmap_db_cross, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
-}
+#endif
 
 static void FadeSetLeaveNext(struct TitleFadingInfo fading_leave, boolean set)
 {
@@ -684,6 +660,10 @@ void FadeSetEnterMenu()
 {
   fading = menu.enter_menu;
 
+#if 0
+  printf("::: storing enter_menu\n");
+#endif
+
   FadeSetLeaveNext(fading, TRUE);      /* (keep same fade mode) */
 }
 
@@ -691,6 +671,10 @@ void FadeSetLeaveMenu()
 {
   fading = menu.leave_menu;
 
+#if 0
+  printf("::: storing leave_menu\n");
+#endif
+
   FadeSetLeaveNext(fading, TRUE);      /* (keep same fade mode) */
 }
 
@@ -698,11 +682,31 @@ void FadeSetEnterScreen()
 {
   fading = menu.enter_screen[game_status];
 
+#if 0
+  printf("::: storing leave_screen[%d]\n", game_status);
+#endif
+
   FadeSetLeaveNext(menu.leave_screen[game_status], TRUE);      /* store */
 }
 
+void FadeSetNextScreen()
+{
+  fading = menu.next_screen;
+
+#if 0
+  printf("::: storing next_screen\n");
+#endif
+
+  // (do not overwrite fade mode set by FadeSetEnterScreen)
+  // FadeSetLeaveNext(fading, TRUE);   /* (keep same fade mode) */
+}
+
 void FadeSetLeaveScreen()
 {
+#if 0
+  printf("::: recalling last stored value\n");
+#endif
+
   FadeSetLeaveNext(menu.leave_screen[game_status], FALSE);     /* recall */
 }
 
@@ -725,12 +729,12 @@ void FadeSetDisabled()
 
 void FadeSkipNextFadeIn()
 {
-  FadeExt(0, FADE_MODE_SKIP_FADE_IN);
+  FadeExt(0, FADE_MODE_SKIP_FADE_IN, FADE_TYPE_SKIP);
 }
 
 void FadeSkipNextFadeOut()
 {
-  FadeExt(0, FADE_MODE_SKIP_FADE_OUT);
+  FadeExt(0, FADE_MODE_SKIP_FADE_OUT, FADE_TYPE_SKIP);
 }
 
 void SetWindowBackgroundImageIfDefined(int graphic)
@@ -920,8 +924,8 @@ inline int getGraphicAnimationFrame(int graphic, int sync_frame)
                           sync_frame);
 }
 
-void getSizedGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y,
-                          int tilesize_raw)
+void getSizedGraphicSource(int graphic, int frame, int tilesize_raw,
+                          Bitmap **bitmap, int *x, int *y)
 {
   struct
   {
@@ -937,27 +941,54 @@ void getSizedGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y,
     { 0, 1,    2, 3    },      /* 16 x 16 */
     { 0, 1,    0, 1    },      /* 32 x 32 */
   };
+  struct GraphicInfo *g = &graphic_info[graphic];
+  Bitmap *src_bitmap = g->bitmap;
   int tilesize = MIN(MAX(1, tilesize_raw), TILESIZE);
   int offset_calc_pos = log_2(tilesize);
-  Bitmap *src_bitmap = graphic_info[graphic].bitmap;
   int width_mult  = offset_calc[offset_calc_pos].width_mult;
   int width_div   = offset_calc[offset_calc_pos].width_div;
   int height_mult = offset_calc[offset_calc_pos].height_mult;
   int height_div  = offset_calc[offset_calc_pos].height_div;
   int startx = src_bitmap->width * width_mult / width_div;
   int starty = src_bitmap->height * height_mult / height_div;
-  int src_x = startx + graphic_info[graphic].src_x * tilesize / TILESIZE;
-  int src_y = starty + graphic_info[graphic].src_y * tilesize / TILESIZE;
+  int src_x = g->src_x * tilesize / TILESIZE;
+  int src_y = g->src_y * tilesize / TILESIZE;
+  int width = g->width * tilesize / TILESIZE;
+  int height = g->height * tilesize / TILESIZE;
+  int offset_x = g->offset_x * tilesize / TILESIZE;
+  int offset_y = g->offset_y * tilesize / TILESIZE;
+
+  if (g->offset_y == 0)                /* frames are ordered horizontally */
+  {
+    int max_width = g->anim_frames_per_line * width;
+    int pos = (src_y / height) * max_width + src_x + frame * offset_x;
+
+    src_x = pos % max_width;
+    src_y = src_y % height + pos / max_width * height;
+  }
+  else if (g->offset_x == 0)   /* frames are ordered vertically */
+  {
+    int max_height = g->anim_frames_per_line * height;
+    int pos = (src_x / width) * max_height + src_y + frame * offset_y;
+
+    src_x = src_x % width + pos / max_height * width;
+    src_y = pos % max_height;
+  }
+  else                         /* frames are ordered diagonally */
+  {
+    src_x = src_x + frame * offset_x;
+    src_y = src_y + frame * offset_y;
+  }
 
   *bitmap = src_bitmap;
-  *x = src_x;
-  *y = src_y;
+  *x = startx + src_x;
+  *y = starty + src_y;
 }
 
 void getMiniGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
 {
 #if 1
-  getSizedGraphicSource(graphic, bitmap, x, y, MINI_TILESIZE);
+  getSizedGraphicSource(graphic, 0, MINI_TILESIZE, bitmap, x, y);
 #else
   struct GraphicInfo *g = &graphic_info[graphic];
   int mini_startx = 0;
@@ -1060,19 +1091,20 @@ void DrawGraphicThruMaskExt(DrawBuffer *d, int dst_x, int dst_y, int graphic,
   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dst_x, dst_y);
 }
 
-void DrawSizedGraphic(int x, int y, int graphic, int tilesize)
+void DrawSizedGraphic(int x, int y, int graphic, int frame, int tilesize)
 {
   DrawSizedGraphicExt(drawto, SX + x * tilesize, SY + y * tilesize, graphic,
-                     tilesize);
+                     frame, tilesize);
   MarkTileDirty(x / tilesize, y / tilesize);
 }
 
-void DrawSizedGraphicExt(DrawBuffer *d, int x, int y, int graphic, int tilesize)
+void DrawSizedGraphicExt(DrawBuffer *d, int x, int y, int graphic, int frame,
+                        int tilesize)
 {
   Bitmap *src_bitmap;
   int src_x, src_y;
 
-  getSizedGraphicSource(graphic, &src_bitmap, &src_x, &src_y, tilesize);
+  getSizedGraphicSource(graphic, frame, tilesize, &src_bitmap, &src_x, &src_y);
   BlitBitmap(src_bitmap, d, src_x, src_y, tilesize, tilesize, x, y);
 }
 
@@ -1894,7 +1926,7 @@ void DrawPreviewElement(int dst_x, int dst_y, int element, int tilesize)
   int src_x, src_y;
   int graphic = el2preimg(element);
 
-  getSizedGraphicSource(graphic, &src_bitmap, &src_x, &src_y, tilesize);
+  getSizedGraphicSource(graphic, 0, tilesize, &src_bitmap, &src_x, &src_y);
   BlitBitmap(src_bitmap, drawto, src_x, src_y, tilesize, tilesize, dst_x,dst_y);
 }
 
@@ -3000,7 +3032,6 @@ boolean Request(char *text, unsigned int req_state)
     if (game_status == GAME_MODE_PLAYING && local_player->LevelSolved_GameEnd)
     {
       HandleGameActions();
-      BackToFront();
     }
     else
     {
@@ -3010,6 +3041,8 @@ boolean Request(char *text, unsigned int req_state)
        Delay(10);
     }
 
+    BackToFront();
+
 #else
 
     DoAnimation();
@@ -5856,7 +5889,7 @@ int getBeltDirFromBeltSwitchElement(int element)
   return belt_move_dir[belt_dir_nr];
 }
 
-int getBeltElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
+int getBeltElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
 {
   static int belt_base_element[4] =
   {
@@ -5865,12 +5898,18 @@ int getBeltElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
     EL_CONVEYOR_BELT_3_LEFT,
     EL_CONVEYOR_BELT_4_LEFT
   };
-  int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
 
   return belt_base_element[belt_nr] + belt_dir_nr;
 }
 
-int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
+int getBeltElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
+{
+  int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
+
+  return getBeltElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
+}
+
+int getBeltSwitchElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
 {
   static int belt_base_element[4] =
   {
@@ -5879,11 +5918,17 @@ int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
     EL_CONVEYOR_BELT_3_SWITCH_LEFT,
     EL_CONVEYOR_BELT_4_SWITCH_LEFT
   };
-  int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
 
   return belt_base_element[belt_nr] + belt_dir_nr;
 }
 
+int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
+{
+  int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
+
+  return getBeltSwitchElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
+}
+
 int getNumActivePlayers_EM()
 {
   int num_players = 0;
@@ -5932,8 +5977,73 @@ void ResetGfxAnimation_EM(int x, int y, int tile)
   GfxFrame[x][y] = 0;
 }
 
+void SetGfxAnimation_EM(int tile, int frame_em, int x, int y)
+{
+#if 0
+  int element         = object_mapping[tile].element_rnd;
+#endif
+  int action          = object_mapping[tile].action;
+  int direction       = object_mapping[tile].direction;
+  boolean is_backside = object_mapping[tile].is_backside;
+  boolean action_removing = (action == ACTION_DIGGING ||
+                            action == ACTION_SNAPPING ||
+                            action == ACTION_COLLECTING);
+
+#if 0
+  printf("::: SET: %d, %d: '%s'\n", x, y, EL_NAME(element));
+#endif
+
+#if 1
+  if (action_removing)
+  {
+#if 0
+    printf("::: %d, %d: action_removing [%s]\n", x, y, EL_NAME(element));
+#endif
+
+    GfxFrame[x][y] = 7 - frame_em;
+  }
+  else if (action == ACTION_FALLING ||
+          action == ACTION_MOVING ||
+          action == ACTION_PUSHING ||
+          action == ACTION_EATING ||
+          action == ACTION_FILLING ||
+          action == ACTION_EMPTYING)
+  {
+    int move_dir =
+      (action == ACTION_FALLING ||
+       action == ACTION_FILLING ||
+       action == ACTION_EMPTYING ? MV_DOWN : direction);
+
+    if (is_backside)
+    {
+      GfxFrame[x][y]++;
+
+      if (move_dir == MV_LEFT)
+       GfxFrame[x - 1][y] = GfxFrame[x][y];
+      else if (move_dir == MV_RIGHT)
+       GfxFrame[x + 1][y] = GfxFrame[x][y];
+      else if (move_dir == MV_UP)
+       GfxFrame[x][y - 1] = GfxFrame[x][y];
+      else if (move_dir == MV_DOWN)
+       GfxFrame[x][y + 1] = GfxFrame[x][y];
+    }
+
+#if 0
+    printf("::: %d, %d: %s, %d, %d [%d]\n", x, y, EL_NAME(element), is_backside,
+          move_dir, GfxFrame[x][y]);
+#endif
+  }
+  else
+    GfxFrame[x][y]++;
+#else
+  GfxFrame[x][y] = 7 - frame_em;
+#endif
+}
+
 void getGraphicSourceObjectExt_EM(int tile, int frame_em,
                                  Bitmap **src_bitmap, int *src_x, int *src_y,
+                                 Bitmap **crumbled_src_bitmap,
+                                 int *crumbled_src_x, int *crumbled_src_y,
                                  int x, int y)
 {
   int element         = object_mapping[tile].element_rnd;
@@ -5950,13 +6060,37 @@ void getGraphicSourceObjectExt_EM(int tile, int frame_em,
   int graphic = (direction == MV_NONE ?
                 el_act2img(effective_element, action) :
                 el_act_dir2img(effective_element, action, direction));
+  int crumbled = (direction == MV_NONE ?
+                 el_act2crm(effective_element, action) :
+                 el_act_dir2crm(effective_element, action, direction));
+  int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
+  int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
+  boolean has_crumbled_graphics = (base_crumbled != base_graphic);
   struct GraphicInfo *g = &graphic_info[graphic];
   int sync_frame;
 
+#if 0
+  printf("::: GET: %d, %d: '%s'\n", x, y, EL_NAME(element));
+#endif
+
+#if 0
+  if (GfxFrame[x][y] < 8)
+    printf("::: %d, %d: %d [%s]\n", x, y, GfxFrame[x][y], EL_NAME(element));
+#endif
+
+#if 1
+  if (graphic_info[graphic].anim_global_sync)
+    sync_frame = FrameCounter;
+  else if (IN_FIELD(x, y, MAX_LEV_FIELDX, MAX_LEV_FIELDY))
+    sync_frame = GfxFrame[x][y];
+  else
+    sync_frame = 0;    /* steel border */
+#else
   if (graphic_info[graphic].anim_global_sync)
     sync_frame = FrameCounter;
   else
     sync_frame = 7 - frame_em;
+#endif
 
   SetRandomAnimationValue(x, y);
 
@@ -5967,6 +6101,28 @@ void getGraphicSourceObjectExt_EM(int tile, int frame_em,
                                sync_frame);
 
   getGraphicSourceExt(graphic, frame, src_bitmap, src_x, src_y, FALSE);
+
+#if 0
+  if (x == 1 && y == 1 && frame == 0)
+    printf("--> %d, %d, %d\n", *crumbled_src_x, *crumbled_src_y, tile);
+#endif
+
+#if 0
+  getGraphicSource(crumbled, frame, crumbled_src_bitmap,
+                  crumbled_src_x, crumbled_src_y);
+#endif
+
+#if 0
+  *crumbled_src_bitmap = NULL;
+  *crumbled_src_x = 0;
+  *crumbled_src_y = 0;
+
+  if (has_crumbled_graphics && crumbled != IMG_EMPTY_SPACE)
+  {
+    getGraphicSource(crumbled, frame, crumbled_src_bitmap,
+                    crumbled_src_x, crumbled_src_y);
+  }
+#endif
 }
 
 void getGraphicSourcePlayerExt_EM(int player_nr, int anim, int frame_em,
@@ -6166,6 +6322,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 *g_crumbled = &graphic_info[crumbled];
       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
       Bitmap *src_bitmap;
       int src_x, src_y;
@@ -6318,17 +6475,43 @@ void InitGraphicInfo_EM(void)
 #endif
 
       /* if element can be crumbled, but certain action graphics are just empty
-        space (like snapping sand with the original R'n'D graphics), do not
+        space (like instantly snapping sand to empty space in 1 frame), do not
         treat these empty space graphics as crumbled graphics in EMC engine */
       if (has_crumbled_graphics && crumbled != IMG_EMPTY_SPACE)
       {
-       getGraphicSource(crumbled, frame, &src_bitmap, &src_x, &src_y);
+       int frame_crumbled = getAnimationFrame(g_crumbled->anim_frames,
+                                              g_crumbled->anim_delay,
+                                              g_crumbled->anim_mode,
+                                              g_crumbled->anim_start_frame,
+                                              sync_frame);
+
+       getGraphicSource(crumbled, frame_crumbled, &src_bitmap, &src_x, &src_y);
 
        g_em->has_crumbled_graphics = TRUE;
        g_em->crumbled_bitmap = src_bitmap;
        g_em->crumbled_src_x = src_x;
        g_em->crumbled_src_y = src_y;
        g_em->crumbled_border_size = graphic_info[crumbled].border_size;
+
+
+#if 0
+       if (g_em == &graphic_info_em_object[207][0])
+         printf("... %d, %d [%d, %d, %d, %d] [%d, %d, %d, %d, %d, %d => %d]\n",
+                graphic_info_em_object[207][0].crumbled_src_x,
+                graphic_info_em_object[207][0].crumbled_src_y,
+
+                crumbled, frame, src_x, src_y,
+
+                g->anim_frames,
+                g->anim_delay,
+                g->anim_mode,
+                g->anim_start_frame,
+                sync_frame,
+                gfx.anim_random_frame,
+                frame);
+#endif
+
+
       }
 
 #if 0