rnd-20030807-1-src
[rocksndiamonds.git] / src / game.c
index d837dad2089889148c108b72a294c71476dcc17d..f5b3ab7b883d783c088bd11a20379611c4fc3a75 100644 (file)
@@ -89,6 +89,9 @@
 #define DOUBLE_PLAYER_SPEED(p) (HALVE_MOVE_DELAY((p)->move_delay_value))
 #define HALVE_PLAYER_SPEED(p)  (DOUBLE_MOVE_DELAY((p)->move_delay_value))
 
+/* values for other actions */
+#define MOVE_STEPSIZE_NORMAL   (TILEX / MOVE_DELAY_NORMAL_SPEED)
+
 #define        INIT_GFX_RANDOM()       (SimpleRND(1000000))
 
 #define GET_NEW_PUSH_DELAY(e)  (   (element_info[e].push_delay_fixed) + \
@@ -163,8 +166,11 @@ static void CloseAllOpenTimegates(void);
 static void CheckGravityMovement(struct PlayerInfo *);
 static void KillHeroUnlessProtected(int, int);
 
-static void CheckTriggeredElementChange(int, int);
-static void CheckPlayerElementChange(int, int, int, int);
+static void TestIfPlayerTouchesCustomElement(int, int);
+static void TestIfElementTouchesCustomElement(int, int);
+
+static boolean CheckTriggeredElementChange(int, int, int, int);
+static boolean CheckElementChange(int, int, int, int);
 static void ChangeElementNow(int, int, int);
 
 static void PlaySoundLevel(int, int, int);
@@ -370,6 +376,25 @@ push_delay_list[] =
   { EL_UNDEFINED,              0, 0 },
 };
 
+struct
+{
+  int element;
+  int move_stepsize;
+}
+move_stepsize_list[] =
+{
+  { EL_AMOEBA_DROP,            2 },
+  { EL_AMOEBA_DROPPING,                2 },
+  { EL_QUICKSAND_FILLING,      1 },
+  { EL_QUICKSAND_EMPTYING,     1 },
+  { EL_MAGIC_WALL_FILLING,     2 },
+  { EL_BD_MAGIC_WALL_FILLING,  2 },
+  { EL_MAGIC_WALL_EMPTYING,    2 },
+  { EL_BD_MAGIC_WALL_EMPTYING, 2 },
+
+  { EL_UNDEFINED,              0 },
+};
+
 struct
 {
   int element;
@@ -377,17 +402,17 @@ struct
 }
 gem_count_list[] =
 {
-  { EL_EMERALD,                1 },
-  { EL_BD_DIAMOND,     1 },
-  { EL_EMERALD_YELLOW, 1 },
-  { EL_EMERALD_RED,    1 },
-  { EL_EMERALD_PURPLE, 1 },
-  { EL_DIAMOND,                3 },
-  { EL_SP_INFOTRON,    1 },
-  { EL_PEARL,          5 },
-  { EL_CRYSTAL,                8 },
-
-  { EL_UNDEFINED,      0 },
+  { EL_EMERALD,                        1 },
+  { EL_BD_DIAMOND,             1 },
+  { EL_EMERALD_YELLOW,         1 },
+  { EL_EMERALD_RED,            1 },
+  { EL_EMERALD_PURPLE,         1 },
+  { EL_DIAMOND,                        3 },
+  { EL_SP_INFOTRON,            1 },
+  { EL_PEARL,                  5 },
+  { EL_CRYSTAL,                        8 },
+
+  { EL_UNDEFINED,              0 },
 };
 
 static boolean changing_element[MAX_NUM_ELEMENTS];
@@ -397,7 +422,6 @@ static unsigned long trigger_events[MAX_NUM_ELEMENTS];
 #define IS_JUST_CHANGING(x, y) (ChangeDelay[x][y] != 0)
 #define IS_CHANGING(x, y)      (IS_AUTO_CHANGING(Feld[x][y]) || \
                                 IS_JUST_CHANGING(x, y))
-#define TRIGGERS_BY_COLLECTING(e) (trigger_events[e] & CE_OTHER_COLLECTING)
 
 
 void GetPlayerConfig()
@@ -822,7 +846,7 @@ static void InitGameEngine()
   /* add trigger events from element change event properties */
   for (i=0; i<MAX_NUM_ELEMENTS; i++)
     if (HAS_CHANGE_EVENT(i, CE_BY_OTHER))
-      trigger_events[element_info[i].change.trigger] |=
+      trigger_events[element_info[i].change.trigger_element] |=
        element_info[i].change.events;
 
   /* ---------- initialize push delay -------------------------------------- */
@@ -846,6 +870,21 @@ static void InitGameEngine()
     element_info[e].push_delay_random = push_delay_list[i].push_delay_random;
   }
 
+  /* ---------- initialize move stepsize ----------------------------------- */
+
+  /* initialize move stepsize values to default */
+  for (i=0; i<MAX_NUM_ELEMENTS; i++)
+    if (!IS_CUSTOM_ELEMENT(i))
+      element_info[i].move_stepsize = MOVE_STEPSIZE_NORMAL;
+
+  /* set move stepsize value for certain elements from pre-defined list */
+  for (i=0; move_stepsize_list[i].element != EL_UNDEFINED; i++)
+  {
+    int e = move_stepsize_list[i].element;
+
+    element_info[e].move_stepsize = move_stepsize_list[i].move_stepsize;
+  }
+
   /* ---------- initialize gem count --------------------------------------- */
 
   /* initialize gem count values for each element */
@@ -993,6 +1032,7 @@ void InitGame()
   game.switchgate_pos = 0;
   game.balloon_dir = MV_NO_MOVING;
   game.explosions_delayed = TRUE;
+  game.current_gravity = level.initial_gravity;
 
   for (i=0; i<4; i++)
   {
@@ -1007,7 +1047,7 @@ void InitGame()
   {
     for (y=0; y<lev_fieldy; y++)
     {
-      Feld[x][y] = Ur[x][y];
+      Feld[x][y] = level.field[x][y];
       MovPos[x][y] = MovDir[x][y] = MovDelay[x][y] = 0;
       ChangeDelay[x][y] = 0;
       Store[x][y] = Store2[x][y] = StorePlayer[x][y] = Back[x][y] = 0;
@@ -1015,6 +1055,7 @@ void InitGame()
       JustStopped[x][y] = 0;
       Stop[x][y] = FALSE;
       Pushed[x][y] = FALSE;
+      Changing[x][y] = FALSE;
       ExplodePhase[x][y] = 0;
       ExplodeField[x][y] = EX_NO_EXPLOSION;
 
@@ -1711,11 +1752,6 @@ static void RemoveField(int x, int y)
   MovDir[x][y] = 0;
   MovDelay[x][y] = 0;
 
-#if 0
-  Store[x][y] = 0;
-  Store2[x][y] = 0;
-#endif
-
   AmoebaNr[x][y] = 0;
   ChangeDelay[x][y] = 0;
   Pushed[x][y] = FALSE;
@@ -1746,7 +1782,6 @@ void RemoveMovingField(int x, int y)
       return;
   }
 
-#if 1
   if (element == EL_BLOCKED &&
       (Feld[oldx][oldy] == EL_QUICKSAND_EMPTYING ||
        Feld[oldx][oldy] == EL_MAGIC_WALL_EMPTYING ||
@@ -1757,31 +1792,10 @@ void RemoveMovingField(int x, int y)
   RemoveField(oldx, oldy);
   RemoveField(newx, newy);
 
-#if 1
   Store[oldx][oldy] = Store2[oldx][oldy] = 0;
-#endif
 
   if (next_element != EL_UNDEFINED)
     Feld[oldx][oldy] = next_element;
-#else
-  if (element == EL_BLOCKED &&
-      (Feld[oldx][oldy] == EL_QUICKSAND_EMPTYING ||
-       Feld[oldx][oldy] == EL_MAGIC_WALL_EMPTYING ||
-       Feld[oldx][oldy] == EL_BD_MAGIC_WALL_EMPTYING ||
-       Feld[oldx][oldy] == EL_AMOEBA_DROPPING))
-    Feld[oldx][oldy] = get_next_element(Feld[oldx][oldy]);
-  else
-    Feld[oldx][oldy] = EL_EMPTY;
-
-  Store[oldx][oldy] = Store2[oldx][oldy] = 0;
-
-  Feld[newx][newy] = EL_EMPTY;
-  MovPos[oldx][oldy] = MovDir[oldx][oldy] = MovDelay[oldx][oldy] = 0;
-  MovPos[newx][newy] = MovDir[newx][newy] = MovDelay[newx][newy] = 0;
-  ChangeDelay[oldx][oldy] = ChangeDelay[newx][newy] = 0;
-  Pushed[oldx][oldy] = Pushed[newx][newy] = FALSE;
-  GfxAction[oldx][oldy] = GfxAction[newx][newy] = ACTION_DEFAULT;
-#endif
 
   DrawLevelField(oldx, oldy);
   DrawLevelField(newx, newy);
@@ -1868,8 +1882,19 @@ void Explode(int ex, int ey, int phase, int mode)
   {
     int center_element = Feld[ex][ey];
 
+#if 0
+    /* --- This is only really needed (and now handled) in "Impact()". --- */
+    /* do not explode moving elements that left the explode field in time */
+    if (game.engine_version >= RELEASE_IDENT(2,2,0,7) &&
+       center_element == EL_EMPTY && (mode == EX_NORMAL || mode == EX_CENTER))
+      return;
+#endif
+
+    if (mode == EX_NORMAL || mode == EX_CENTER)
+      PlaySoundLevelAction(ex, ey, ACTION_EXPLODING);
+
     /* remove things displayed in background while burning dynamite */
-    if (!IS_INDESTRUCTIBLE(Back[ex][ey]))
+    if (Back[ex][ey] != EL_EMPTY && !IS_INDESTRUCTIBLE(Back[ex][ey]))
       Back[ex][ey] = 0;
 
     if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
@@ -1882,6 +1907,8 @@ void Explode(int ex, int ey, int phase, int mode)
 
     for (y = ey - 1; y <= ey + 1; y++) for(x = ex - 1; x <= ex + 1; x++)
     {
+      int xx = x - ex + 1;
+      int yy = y - ey + 1;
       int element;
 
       if (!IN_LEV_FIELD(x, y) ||
@@ -1894,12 +1921,23 @@ void Explode(int ex, int ey, int phase, int mode)
       if (IS_MOVING(x, y) || IS_BLOCKED(x, y))
       {
        element = MovingOrBlocked2Element(x, y);
-       RemoveMovingField(x, y);
+
+       if (!IS_EXPLOSION_PROOF(element))
+         RemoveMovingField(x, y);
       }
 
 #if 1
+
+#if 0
       if (IS_EXPLOSION_PROOF(element))
        continue;
+#else
+      /* indestructible elements can only explode in center (but not flames) */
+      if ((IS_EXPLOSION_PROOF(element) && (x != ex || y != ey)) ||
+         element == EL_FLAMES)
+       continue;
+#endif
+
 #else
       if ((IS_INDESTRUCTIBLE(element) &&
           (game.engine_version < VERSION_IDENT(2,2,0) ||
@@ -1921,8 +1959,13 @@ void Explode(int ex, int ey, int phase, int mode)
       }
 
       /* save walkable background elements while explosion on same tile */
+#if 0
       if (IS_INDESTRUCTIBLE(element))
        Back[x][y] = element;
+#else
+      if (IS_WALKABLE(element) && IS_INDESTRUCTIBLE(element))
+       Back[x][y] = element;
+#endif
 
       /* ignite explodable elements reached by other explosion */
       if (element == EL_EXPLOSION)
@@ -1976,11 +2019,10 @@ void Explode(int ex, int ey, int phase, int mode)
       else if (center_element == EL_AMOEBA_TO_DIAMOND)
        Store[x][y] = level.amoeba_content;
       else if (center_element == EL_YAMYAM)
-       Store[x][y] =
-         level.yamyam_content[game.yamyam_content_nr][x - ex + 1][y - ey + 1];
-      else if (IS_CUSTOM_ELEMENT(center_element))
-       Store[x][y] =
-         element_info[center_element].content[x - ex + 1][y - ey + 1];
+       Store[x][y] = level.yamyam_content[game.yamyam_content_nr][xx][yy];
+      else if (IS_CUSTOM_ELEMENT(center_element) &&
+              element_info[center_element].content[xx][yy] != EL_EMPTY)
+       Store[x][y] = element_info[center_element].content[xx][yy];
       else if (element == EL_WALL_EMERALD)
        Store[x][y] = EL_EMERALD;
       else if (element == EL_WALL_DIAMOND)
@@ -1997,6 +2039,8 @@ void Explode(int ex, int ey, int phase, int mode)
        Store[x][y] = EL_PEARL;
       else if (element == EL_WALL_CRYSTAL)
        Store[x][y] = EL_CRYSTAL;
+      else if (IS_CUSTOM_ELEMENT(element))
+       Store[x][y] = element_info[element].content[1][1];
       else
        Store[x][y] = EL_EMPTY;
 
@@ -2093,6 +2137,9 @@ void Explode(int ex, int ey, int phase, int mode)
       InitMovDir(x, y);
     DrawLevelField(x, y);
 
+    if (CAN_BE_CRUMBLED(element))
+      DrawLevelFieldCrumbledSandNeighbours(x, y);
+
     if (IS_PLAYER(x, y) && !PLAYERINFO(x,y)->present)
       StorePlayer[x][y] = 0;
   }
@@ -2169,6 +2216,7 @@ void DynaExplode(int ex, int ey)
 
       Explode(x, y, EX_PHASE_START, EX_BORDER);
 
+      /* !!! extend EL_SAND to anything diggable (but maybe not SP_BASE) !!! */
       if (element != EL_EMPTY &&
          element != EL_SAND &&
          element != EL_EXPLOSION &&
@@ -2194,6 +2242,7 @@ void Bang(int x, int y)
                            player->element_nr);
   }
 
+#if 0
 #if 1
   PlaySoundLevelAction(x, y, ACTION_EXPLODING);
 #else
@@ -2202,6 +2251,7 @@ void Bang(int x, int y)
   else
     PlaySoundLevel(x, y, SND_ELEMENT_EXPLODING);
 #endif
+#endif
 
 #if 0
   if (IS_PLAYER(x, y)) /* remove objects that might cause smaller explosion */
@@ -2244,7 +2294,7 @@ void Bang(int x, int y)
       break;
   }
 
-  CheckTriggeredElementChange(element, CE_OTHER_EXPLODING);
+  CheckTriggeredElementChange(x, y, element, CE_OTHER_IS_EXPLODING);
 }
 
 void SplashAcid(int x, int y)
@@ -2477,14 +2527,16 @@ static int getInvisibleActiveFromInvisibleElement(int element)
 {
   return (element == EL_INVISIBLE_STEELWALL ? EL_INVISIBLE_STEELWALL_ACTIVE :
          element == EL_INVISIBLE_WALL      ? EL_INVISIBLE_WALL_ACTIVE :
-         EL_INVISIBLE_SAND_ACTIVE);
+         element == EL_INVISIBLE_SAND      ? EL_INVISIBLE_SAND_ACTIVE :
+         element);
 }
 
 static int getInvisibleFromInvisibleActiveElement(int element)
 {
   return (element == EL_INVISIBLE_STEELWALL_ACTIVE ? EL_INVISIBLE_STEELWALL :
          element == EL_INVISIBLE_WALL_ACTIVE      ? EL_INVISIBLE_WALL :
-         EL_INVISIBLE_SAND);
+         element == EL_INVISIBLE_SAND_ACTIVE      ? EL_INVISIBLE_SAND :
+         element);
 }
 
 static void RedrawAllLightSwitchesAndInvisibleElements()
@@ -2575,6 +2627,29 @@ static void ActivateTimegateSwitch(int x, int y)
   Feld[x][y] = EL_TIMEGATE_SWITCH_ACTIVE;
 }
 
+inline static int getElementMoveStepsize(int x, int y)
+{
+  int element = Feld[x][y];
+  int direction = MovDir[x][y];
+  int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
+  int dy = (direction == MV_UP   ? -1 : direction == MV_DOWN  ? +1 : 0);
+  int horiz_move = (dx != 0);
+  int sign = (horiz_move ? dx : dy);
+  int step = sign * element_info[element].move_stepsize;
+
+  /* special values for move stepsize for spring and things on conveyor belt */
+  if (horiz_move)
+  {
+    if (CAN_FALL(element) &&
+       y < lev_fieldy - 1 && IS_BELT_ACTIVE(Feld[x][y + 1]))
+      step = sign * MOVE_STEPSIZE_NORMAL / 2;
+    else if (element == EL_SPRING)
+      step = sign * MOVE_STEPSIZE_NORMAL * 2;
+  }
+
+  return step;
+}
+
 void Impact(int x, int y)
 {
   boolean lastline = (y == lev_fieldy-1);
@@ -2591,6 +2666,12 @@ void Impact(int x, int y)
     object_hit = (!IS_FREE(x, y + 1) && (!IS_MOVING(x, y + 1) ||
                                         MovDir[x][y + 1] != MV_DOWN ||
                                         MovPos[x][y + 1] <= TILEY / 2));
+
+    /* do not smash moving elements that left the smashed field in time */
+    if (game.engine_version >= RELEASE_IDENT(2,2,0,7) && IS_MOVING(x, y + 1) &&
+       ABS(MovPos[x][y + 1] + getElementMoveStepsize(x, y + 1)) >= TILEX)
+      object_hit = FALSE;
+
     if (object_hit)
       smashed = MovingOrBlocked2Element(x, y + 1);
 
@@ -2620,6 +2701,14 @@ void Impact(int x, int y)
     PlaySoundLevel(x, y, SND_PEARL_BREAKING);
     return;
   }
+#if 1
+  else if (impact && CheckElementChange(x, y, element, CE_IMPACT))
+  {
+    PlaySoundLevelElementAction(x, y, element, ACTION_IMPACT);
+
+    return;
+  }
+#else
   else if (impact && CAN_CHANGE(element) &&
           HAS_CHANGE_EVENT(element, CE_IMPACT))
   {
@@ -2629,6 +2718,7 @@ void Impact(int x, int y)
 
     return;
   }
+#endif
 
   if (impact && element == EL_AMOEBA_DROP)
   {
@@ -2704,6 +2794,13 @@ void Impact(int x, int y)
       Bang(x, y + 1);
       return;
     }
+#if 0
+    else if (CAN_SMASH_ENEMIES(element) && IS_CLASSIC_ENEMY(smashed))
+    {
+      Bang(x, y + 1);
+      return;
+    }
+#endif
     else if (CAN_SMASH_EVERYTHING(element))
     {
       if (IS_CLASSIC_ENEMY(smashed) ||
@@ -2753,11 +2850,21 @@ void Impact(int x, int y)
        {
          ToggleLightSwitch(x, y + 1);
        }
-       else if (CAN_CHANGE(smashed) &&
-                HAS_CHANGE_EVENT(smashed, CE_SMASHED))
+#if 1
+       else
+       {
+         CheckElementChange(x, y + 1, smashed, CE_SMASHED);
+       }
+#else
+       else if (CAN_CHANGE(smashed) && HAS_CHANGE_EVENT(smashed, CE_SMASHED))
        {
          ChangeElementNow(x, y + 1, smashed);
        }
+#endif
+      }
+      else
+      {
+       CheckElementChange(x, y + 1, smashed, CE_SMASHED);
       }
     }
   }
@@ -3343,7 +3450,9 @@ void StartMoving(int x, int y)
   if (Stop[x][y])
     return;
 
-  GfxAction[x][y] = ACTION_DEFAULT;
+  /* !!! this should be handled more generic (not only for mole) !!! */
+  if (element != EL_MOLE && GfxAction[x][y] != ACTION_DIGGING)
+    GfxAction[x][y] = ACTION_DEFAULT;
 
   if (CAN_FALL(element) && y < lev_fieldy - 1)
   {
@@ -3547,31 +3656,47 @@ void StartMoving(int x, int y)
             element != EL_DX_SUPABOMB && element != EL_SP_DISK_ORANGE)
 #endif
     {
-      boolean left  = (x>0 && IS_FREE(x-1, y) &&
-                      (IS_FREE(x-1, y + 1) || Feld[x-1][y + 1] == EL_ACID));
-      boolean right = (x<lev_fieldx-1 && IS_FREE(x+1, y) &&
-                      (IS_FREE(x+1, y + 1) || Feld[x+1][y + 1] == EL_ACID));
+      boolean can_fall_left  = (x > 0 && IS_FREE(x - 1, y) &&
+                               (IS_FREE(x - 1, y + 1) ||
+                                Feld[x - 1][y + 1] == EL_ACID));
+      boolean can_fall_right = (x < lev_fieldx - 1 && IS_FREE(x + 1, y) &&
+                               (IS_FREE(x + 1, y + 1) ||
+                                Feld[x + 1][y + 1] == EL_ACID));
+      boolean can_fall_any  = (can_fall_left || can_fall_right);
+      boolean can_fall_both = (can_fall_left && can_fall_right);
 
-      if (left || right)
+      if (can_fall_any && IS_CUSTOM_ELEMENT(Feld[x][y + 1]))
       {
-       if (left && right &&
+       int slippery_type = element_info[Feld[x][y + 1]].slippery_type;
+
+       if (slippery_type == SLIPPERY_ONLY_LEFT)
+         can_fall_right = FALSE;
+       else if (slippery_type == SLIPPERY_ONLY_RIGHT)
+         can_fall_left = FALSE;
+       else if (slippery_type == SLIPPERY_ANY_LEFT_RIGHT && can_fall_both)
+         can_fall_right = FALSE;
+       else if (slippery_type == SLIPPERY_ANY_RIGHT_LEFT && can_fall_both)
+         can_fall_left = FALSE;
+
+       can_fall_any  = (can_fall_left || can_fall_right);
+       can_fall_both = (can_fall_left && can_fall_right);
+      }
+
+      if (can_fall_any)
+      {
+       if (can_fall_both &&
            (game.emulation != EMU_BOULDERDASH &&
             element != EL_BD_ROCK && element != EL_BD_DIAMOND))
-         left = !(right = RND(2));
+         can_fall_left = !(can_fall_right = RND(2));
 
-       InitMovingField(x, y, left ? MV_LEFT : MV_RIGHT);
+       InitMovingField(x, y, can_fall_left ? MV_LEFT : MV_RIGHT);
        started_moving = TRUE;
-
-#if 0
-       if (element == EL_BOMB)
-         printf("::: SLIP DOWN [%d]\n", FrameCounter);
-#endif
       }
     }
     else if (IS_BELT_ACTIVE(Feld[x][y + 1]))
     {
-      boolean left_is_free  = (x>0 && IS_FREE(x-1, y));
-      boolean right_is_free = (x<lev_fieldx-1 && IS_FREE(x+1, y));
+      boolean left_is_free  = (x > 0 && IS_FREE(x - 1, y));
+      boolean right_is_free = (x < lev_fieldx - 1 && IS_FREE(x + 1, y));
       int belt_nr = getBeltNrFromBeltActiveElement(Feld[x][y + 1]);
       int belt_dir = game.belt_dir[belt_nr];
 
@@ -3722,7 +3847,10 @@ void StartMoving(int x, int y)
        return;
       }
 
-      GfxAction[x][y] = ACTION_MOVING;
+      /* special case of "moving" animation of waiting elements (FIX THIS !!!);
+        for all other elements GfxAction will be set by InitMovingField() */
+      if (element == EL_BD_BUTTERFLY || element == EL_BD_FIREFLY)
+       GfxAction[x][y] = ACTION_MOVING;
     }
 
     /* now make next step */
@@ -3906,6 +4034,11 @@ void StartMoving(int x, int y)
       {
        Feld[newx][newy] = EL_AMOEBA_SHRINKING;
        PlaySoundLevel(x, y, SND_MOLE_DIGGING);
+
+       ResetGfxAnimation(x, y);
+       GfxAction[x][y] = ACTION_DIGGING;
+       DrawLevelField(x, y);
+
        MovDelay[newx][newy] = 0;       /* start amoeba shrinking delay */
        return;                         /* wait for shrinking amoeba */
       }
@@ -3930,7 +4063,8 @@ void StartMoving(int x, int y)
       TurnRound(x, y);
 
 #if 1
-      DrawLevelElementAnimation(x, y, element);
+      if (GFX_ELEMENT(element) != EL_SAND)     /* !!! FIX THIS (crumble) !!! */
+       DrawLevelElementAnimation(x, y, element);
 #else
       if (element == EL_BUG ||
          element == EL_SPACESHIP ||
@@ -3972,73 +4106,14 @@ void ContinueMoving(int x, int y)
   int direction = MovDir[x][y];
   int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
   int dy = (direction == MV_UP   ? -1 : direction == MV_DOWN  ? +1 : 0);
-  int horiz_move = (dx != 0);
   int newx = x + dx, newy = y + dy;
-  int step = (horiz_move ? dx : dy) * TILEX / MOVE_DELAY_NORMAL_SPEED;
-#if 1
+  int nextx = newx + dx, nexty = newy + dy;
   boolean pushed = Pushed[x][y];
-#else
-  struct PlayerInfo *player = (IS_PLAYER(x, y) ? PLAYERINFO(x, y) : NULL);
-#if 0
-  boolean pushing = (player != NULL && player->Pushing && player->MovPos != 0);
-#else
-  boolean pushing = (player != NULL && player->Pushing && player->is_moving);
-#endif
-#endif
-
-#if 0
-  if (player && player->is_moving && player->MovPos == 0)
-    printf("::: !!!\n");
-#endif
-
-  if (element == EL_AMOEBA_DROP || element == EL_AMOEBA_DROPPING)
-    step /= 2;
-  else if (element == EL_QUICKSAND_FILLING ||
-          element == EL_QUICKSAND_EMPTYING)
-    step /= 4;
-  else if (element == EL_MAGIC_WALL_FILLING ||
-          element == EL_BD_MAGIC_WALL_FILLING ||
-          element == EL_MAGIC_WALL_EMPTYING ||
-          element == EL_BD_MAGIC_WALL_EMPTYING)
-    step /= 2;
-  else if (CAN_FALL(element) && horiz_move &&
-          y < lev_fieldy-1 && IS_BELT_ACTIVE(Feld[x][y+1]))
-    step /= 2;
-  else if (element == EL_SPRING && horiz_move)
-    step *= 2;
-  else if (IS_CUSTOM_ELEMENT(element))
-    step = SIGN(step) * element_info[element].move_stepsize;
 
-#if OLD_GAME_BEHAVIOUR
-  else if (CAN_FALL(element) && horiz_move && !IS_SP_ELEMENT(element))
-    step*=2;
-#endif
-
-  MovPos[x][y] += step;
+  MovPos[x][y] += getElementMoveStepsize(x, y);
 
-#if 1
-#if 1
   if (pushed)          /* special case: moving object pushed by player */
-#else
-  if (pushing)         /* special case: moving object pushed by player */
-#endif
-#if 1
     MovPos[x][y] = SIGN(MovPos[x][y]) * (TILEX - ABS(PLAYERINFO(x,y)->MovPos));
-#else
-    MovPos[x][y] = SIGN(MovPos[x][y]) * (TILEX - ABS(PLAYERINFO(x,y)->GfxPos));
-#endif
-#endif
-
-#if 0
-  if (element == EL_SPRING)
-    printf("::: spring moves %d [%d: %d, %d, %d/%d]\n",
-          MovPos[x][y],
-          pushing,
-          (player?player->Pushing:-42),
-          (player?player->is_moving:-42),
-          (player?player->MovPos:-42),
-          (player?player->GfxPos:-42));
-#endif
 
   if (ABS(MovPos[x][y]) >= TILEX)      /* object reached its destination */
   {
@@ -4048,31 +4123,11 @@ void ContinueMoving(int x, int y)
 
     if (element == EL_MOLE)
     {
-      int i;
-      static int xy[4][2] =
-      {
-       { 0, -1 },
-       { -1, 0 },
-       { +1, 0 },
-       { 0, +1 }
-      };
-
       Feld[x][y] = EL_SAND;
-      DrawLevelField(x, y);
-
-      for(i=0; i<4; i++)
-      {
-       int xx, yy;
-
-       xx = x + xy[i][0];
-       yy = y + xy[i][1];
 
-       if (IN_LEV_FIELD(xx, yy) && Feld[xx][yy] == EL_SAND)
-         DrawLevelField(xx, yy);       /* for "crumbled sand" */
-      }
+      DrawLevelFieldCrumbledSandNeighbours(x, y);
     }
-
-    if (element == EL_QUICKSAND_FILLING)
+    else if (element == EL_QUICKSAND_FILLING)
     {
       element = Feld[newx][newy] = get_next_element(element);
       Store[newx][newy] = Store[x][y];
@@ -4146,7 +4201,6 @@ void ContinueMoving(int x, int y)
 
     ResetGfxAnimation(x, y);   /* reset animation values for old field */
 
-#if 1
 #if 0
     /* 2.1.1 (does not work correctly for spring) */
     if (!CAN_MOVE(element))
@@ -4168,20 +4222,14 @@ void ContinueMoving(int x, int y)
        (CAN_FALL(element) && MovDir[newx][newy] == MV_DOWN))
       MovDir[newx][newy] = 0;
 #endif
-
-#endif
 #endif
 
     DrawLevelField(x, y);
     DrawLevelField(newx, newy);
 
-#if 0
-    if (game.engine_version >= RELEASE_IDENT(2,2,0,7) || !pushing)
-#endif
-      Stop[newx][newy] = TRUE; /* ignore this element until the next frame */
-#if 1
+    Stop[newx][newy] = TRUE;   /* ignore this element until the next frame */
+
     if (!pushed)       /* special case: moving object pushed by player */
-#endif
       JustStopped[newx][newy] = 3;
 
     if (DONT_TOUCH(element))   /* object may be nasty to player or others */
@@ -4193,15 +4241,15 @@ void ContinueMoving(int x, int y)
     else if (element == EL_PENGUIN)
       TestIfFriendTouchesBadThing(newx, newy);
 
-#if 1
     if (CAN_FALL(element) && direction == MV_DOWN &&
        (newy == lev_fieldy - 1 || !IS_FREE(x, newy + 1)))
       Impact(x, newy);
-#else
-    if (CAN_SMASH(element) && direction == MV_DOWN &&
-       (newy == lev_fieldy - 1 || !IS_FREE(x, newy + 1)))
-      Impact(x, newy);
-#endif
+
+    if (!IN_LEV_FIELD(nextx, nexty) || !IS_FREE(nextx, nexty))
+      CheckElementChange(newx, newy, element, CE_COLLISION);
+
+    TestIfPlayerTouchesCustomElement(newx, newy);
+    TestIfElementTouchesCustomElement(newx, newy);
   }
   else                         /* still moving on */
   {
@@ -4510,6 +4558,7 @@ void AmoebeAbleger(int ax, int ay)
     if (!IN_LEV_FIELD(x, y))
       return;
 
+    /* !!! extend EL_SAND to anything diggable (but maybe not SP_BASE) !!! */
     if (IS_FREE(x, y) ||
        Feld[x][y] == EL_SAND || Feld[x][y] == EL_QUICKSAND_EMPTY)
     {
@@ -4534,6 +4583,7 @@ void AmoebeAbleger(int ax, int ay)
       if (!IN_LEV_FIELD(x, y))
        continue;
 
+      /* !!! extend EL_SAND to anything diggable (but maybe not SP_BASE) !!! */
       if (IS_FREE(x, y) ||
          Feld[x][y] == EL_SAND || Feld[x][y] == EL_QUICKSAND_EMPTY)
       {
@@ -4678,6 +4728,7 @@ void Life(int ax, int ay)
        changed = TRUE;
       }
     }
+    /* !!! extend EL_SAND to anything diggable (but maybe not SP_BASE) !!! */
     else if (IS_FREE(xx, yy) || Feld[xx][yy] == EL_SAND)
     {                                  /* free border field */
       if (nachbarn >= life[2] && nachbarn <= life[3])
@@ -5114,40 +5165,23 @@ static void ChangeElementNowExt(int x, int y, int target_element)
   DrawLevelField(x, y);
 
   if (CAN_BE_CRUMBLED(Feld[x][y]))
-  {
-    int sx = SCREENX(x), sy = SCREENY(y);
-    static int xy[4][2] =
-    {
-      { 0, -1 },
-      { -1, 0 },
-      { +1, 0 },
-      { 0, +1 }
-    };
-    int i;
+    DrawLevelFieldCrumbledSandNeighbours(x, y);
 
-    for(i=0; i<4; i++)
-    {
-      int xx = x + xy[i][0];
-      int yy = y + xy[i][1];
-      int sxx = sx + xy[i][0];
-      int syy = sy + xy[i][1];
-
-      if (!IN_LEV_FIELD(xx, yy) ||
-         !IN_SCR_FIELD(sxx, syy) ||
-         !CAN_BE_CRUMBLED(Feld[xx][yy]) ||
-         IS_MOVING(xx, yy))
-       continue;
-
-      DrawLevelField(xx, yy);
-    }
-  }
+  TestIfBadThingTouchesHero(x, y);
+  TestIfPlayerTouchesCustomElement(x, y);
+  TestIfElementTouchesCustomElement(x, y);
 }
 
 static void ChangeElementNow(int x, int y, int element)
 {
   struct ElementChangeInfo *change = &element_info[element].change;
 
-  CheckTriggeredElementChange(Feld[x][y], CE_OTHER_CHANGING);
+  /* prevent CheckTriggeredElementChange() from looping */
+  Changing[x][y] = TRUE;
+
+  CheckTriggeredElementChange(x, y, Feld[x][y], CE_OTHER_IS_CHANGING);
+
+  Changing[x][y] = FALSE;
 
   if (change->explode)
   {
@@ -5190,6 +5224,9 @@ static void ChangeElementNow(int x, int y, int element)
 
       e = Feld[ex][ey];
 
+      if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
+       e = MovingOrBlocked2Element(ex, ey);
+
       half_destructible = (IS_FREE(ex, ey) || IS_DIGGABLE(e));
 
       if ((change->power <= CP_NON_DESTRUCTIVE  && !IS_FREE(ex, ey)) ||
@@ -5203,31 +5240,52 @@ static void ChangeElementNow(int x, int y, int element)
 
     if (!change->only_complete || complete_change)
     {
+      boolean something_has_changed = FALSE;
+
+      if (change->only_complete && change->use_random_change &&
+         RND(100) < change->random)
+       return;
+
       for (yy = 0; yy < 3; yy++) for(xx = 0; xx < 3 ; xx++)
       {
        int ex = x + xx - 1;
        int ey = y + yy - 1;
 
-       if (can_change[xx][yy])
+       if (can_change[xx][yy] && (!change->use_random_change ||
+                                  RND(100) < change->random))
        {
+         if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
+           RemoveMovingField(ex, ey);
+
          ChangeElementNowExt(ex, ey, change->content[xx][yy]);
 
+         something_has_changed = TRUE;
+
          /* for symmetry reasons, stop newly created border elements */
          if (ex != x || ey != y)
            Stop[ex][ey] = TRUE;
        }
       }
 
-      return;
+      if (something_has_changed)
+       PlaySoundLevelElementAction(x, y, element, ACTION_CHANGING);
     }
   }
+  else
+  {
+    ChangeElementNowExt(x, y, change->target_element);
 
-  ChangeElementNowExt(x, y, change->target_element);
+    PlaySoundLevelElementAction(x, y, element, ACTION_CHANGING);
+  }
 }
 
 static void ChangeElement(int x, int y)
 {
+#if 1
+  int element = MovingOrBlocked2Element(x, y);
+#else
   int element = Feld[x][y];
+#endif
   struct ElementChangeInfo *change = &element_info[element].change;
 
   if (ChangeDelay[x][y] == 0)          /* initialize element change */
@@ -5306,21 +5364,28 @@ static void ChangeElement(int x, int y)
   }
 }
 
-static void CheckTriggeredElementChange(int trigger_element, int trigger_event)
+static boolean CheckTriggeredElementChange(int lx, int ly, int trigger_element,
+                                          int trigger_event)
 {
   int i, x, y;
 
   if (!(trigger_events[trigger_element] & CH_EVENT_BIT(trigger_event)))
-    return;
+    return FALSE;
 
   for (i=0; i<MAX_NUM_ELEMENTS; i++)
   {
     if (!CAN_CHANGE(i) || !HAS_CHANGE_EVENT(i, trigger_event) ||
-       element_info[i].change.trigger != trigger_element)
+       element_info[i].change.trigger_element != trigger_element)
       continue;
 
     for (y=0; y<lev_fieldy; y++) for (x=0; x<lev_fieldx; x++)
     {
+      if (x == lx && y == ly)  /* do not change trigger element itself */
+       continue;
+
+      if (Changing[x][y])      /* do not change just changing elements */
+       continue;
+
       if (Feld[x][y] == i)
       {
        ChangeDelay[x][y] = 1;
@@ -5328,16 +5393,22 @@ static void CheckTriggeredElementChange(int trigger_element, int trigger_event)
       }
     }
   }
+
+  return TRUE;
 }
 
-static void CheckPlayerElementChange(int x, int y, int element,
-                                    int trigger_event)
+static boolean CheckElementChange(int x, int y, int element, int trigger_event)
 {
   if (!CAN_CHANGE(element) || !HAS_CHANGE_EVENT(element, trigger_event))
-    return;
+    return FALSE;
+
+  if (Feld[x][y] == EL_BLOCKED)
+    Blocked2Moving(x, y, &x, &y);
 
   ChangeDelay[x][y] = 1;
   ChangeElement(x, y);
+
+  return TRUE;
 }
 
 static void PlayerActions(struct PlayerInfo *player, byte player_action)
@@ -5538,6 +5609,16 @@ void GameActions()
 
     GfxFrame[x][y]++;
 
+#if 1
+    /* reset finished pushing action (not done in ContinueMoving() to allow
+       continous pushing animation for elements without push delay) */
+    if (GfxAction[x][y] == ACTION_PUSHING && !IS_MOVING(x, y))
+    {
+      ResetGfxAnimation(x, y);
+      DrawLevelField(x, y);
+    }
+#endif
+
 #if DEBUG
     if (IS_BLOCKED(x, y))
     {
@@ -5611,9 +5692,10 @@ void GameActions()
 #if 1
       graphic = el_act_dir2img(element, GfxAction[x][y], MovDir[x][y]);
 #if 0
-      if (element == EL_PACMAN)
-       printf("::: %d, %d, %d\n",
-              IS_ANIMATED(graphic), IS_MOVING(x, y), Stop[x][y]);
+      if (element == EL_MOLE)
+       printf("::: %d, %d, %d [%d]\n",
+              IS_ANIMATED(graphic), IS_MOVING(x, y), Stop[x][y],
+              GfxAction[x][y]);
 #endif
 #if 0
       if (element == EL_YAMYAM)
@@ -5629,7 +5711,7 @@ void GameActions()
        DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
 
 #if 0
-       if (element == EL_YAMYAM)
+       if (element == EL_MOLE)
          printf("::: %d, %d\n", graphic, GfxFrame[x][y]);
 #endif
       }
@@ -5736,6 +5818,7 @@ void GameActions()
 #endif
       element = Feld[x][y];
 
+      /* !!! extend EL_SAND to anything diggable (but maybe not SP_BASE) !!! */
       if (!IS_PLAYER(x,y) &&
          (element == EL_EMPTY ||
           element == EL_SAND ||
@@ -6005,7 +6088,7 @@ void ScrollLevel(int dx, int dy)
 
 static void CheckGravityMovement(struct PlayerInfo *player)
 {
-  if (level.gravity && !player->programmed_action)
+  if (game.current_gravity && !player->programmed_action)
   {
     int move_dir_vertical = player->action & (MV_UP | MV_DOWN);
     int move_dir_horizontal = player->action & (MV_LEFT | MV_RIGHT);
@@ -6023,6 +6106,7 @@ static void CheckGravityMovement(struct PlayerInfo *player)
       (IN_LEV_FIELD(new_jx, new_jy) &&
        (Feld[new_jx][new_jy] == EL_SP_BASE ||
        Feld[new_jx][new_jy] == EL_SAND));
+    /* !!! extend EL_SAND to anything diggable !!! */
 
     if (field_under_player_is_free &&
        !player_is_moving_to_valid_field &&
@@ -6268,6 +6352,7 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
   }
 
   TestIfHeroTouchesBadThing(jx, jy);
+  TestIfPlayerTouchesCustomElement(jx, jy);
 
   if (!player->active)
     RemoveHero(player);
@@ -6292,7 +6377,9 @@ void ScrollFigure(struct PlayerInfo *player, int mode)
     if (Feld[last_jx][last_jy] == EL_EMPTY)
       Feld[last_jx][last_jy] = EL_PLAYER_IS_LEAVING;
 
+#if 0
     DrawPlayer(player);
+#endif
     return;
   }
   else if (!FrameReached(&player->actual_frame_counter, 1))
@@ -6308,7 +6395,9 @@ void ScrollFigure(struct PlayerInfo *player, int mode)
   if (player->MovPos == 0)
     CheckGravityMovement(player);
 
-  DrawPlayer(player);
+#if 0
+  DrawPlayer(player);  /* needed here only to cleanup last field */
+#endif
 
   if (player->MovPos == 0)
   {
@@ -6368,6 +6457,93 @@ void ScrollScreen(struct PlayerInfo *player, int mode)
     ScreenMovDir = MV_NO_MOVING;
 }
 
+void TestIfPlayerTouchesCustomElement(int x, int y)
+{
+  static boolean check_changing = FALSE;
+  static int xy[4][2] =
+  {
+    { 0, -1 },
+    { -1, 0 },
+    { +1, 0 },
+    { 0, +1 }
+  };
+  boolean center_is_player = (IS_PLAYER(x, y));
+  int i;
+
+  /* prevent TestIfPlayerTouchesCustomElement() from looping */
+  if (check_changing)
+    return;
+
+  check_changing = TRUE;
+
+  for (i=0; i<4; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+
+    if (!IN_LEV_FIELD(xx, yy))
+      continue;
+
+    if (center_is_player)
+    {
+      CheckTriggeredElementChange(xx, yy, Feld[xx][yy], CE_OTHER_GETS_TOUCHED);
+      CheckElementChange(xx, yy, Feld[xx][yy], CE_TOUCHED_BY_PLAYER);
+    }
+    else if (IS_PLAYER(xx, yy))
+    {
+      CheckTriggeredElementChange(x, y, Feld[x][y], CE_OTHER_GETS_TOUCHED);
+      CheckElementChange(x, y, Feld[x][y], CE_TOUCHED_BY_PLAYER);
+
+      break;
+    }
+  }
+
+  check_changing = FALSE;
+}
+
+void TestIfElementTouchesCustomElement(int x, int y)
+{
+  static boolean check_changing = FALSE;
+  static int xy[4][2] =
+  {
+    { 0, -1 },
+    { -1, 0 },
+    { +1, 0 },
+    { 0, +1 }
+  };
+  boolean center_is_custom = (IS_CUSTOM_ELEMENT(Feld[x][y]));
+  int i;
+
+  /* prevent TestIfElementTouchesCustomElement() from looping */
+  if (check_changing)
+    return;
+
+  check_changing = TRUE;
+
+  for (i=0; i<4; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+
+    if (!IN_LEV_FIELD(xx, yy))
+      continue;
+
+    if (center_is_custom &&
+       Feld[xx][yy] == element_info[Feld[x][y]].change.trigger_element)
+    {
+      CheckElementChange(x, y, Feld[x][y], CE_OTHER_IS_TOUCHING);
+    }
+
+    if (IS_CUSTOM_ELEMENT(Feld[xx][yy]) &&
+       Feld[x][y] == element_info[Feld[xx][yy]].change.trigger_element)
+    {
+      CheckElementChange(xx, yy, Feld[xx][yy], CE_OTHER_IS_TOUCHING);
+    }
+  }
+
+  check_changing = FALSE;
+}
+
 void TestIfGoodThingHitsBadThing(int good_x, int good_y, int good_move_dir)
 {
   int i, kill_x = -1, kill_y = -1;
@@ -6906,6 +7082,12 @@ int DigField(struct PlayerInfo *player,
          !IS_FREE(nextx, nexty))
        return MF_NO_ACTION;
 
+      if (element == EL_SP_GRAVITY_PORT_LEFT ||
+         element == EL_SP_GRAVITY_PORT_RIGHT ||
+         element == EL_SP_GRAVITY_PORT_UP ||
+         element == EL_SP_GRAVITY_PORT_DOWN)
+       game.current_gravity = !game.current_gravity;
+
       /* automatically move to the next field with double speed */
       player->programmed_action = move_direction;
       DOUBLE_PLAYER_SPEED(player);
@@ -7036,8 +7218,12 @@ int DigField(struct PlayerInfo *player,
 
        if (mode != DF_SNAP)
        {
+#if 1
+         GfxElement[x][y] = GFX_ELEMENT(element);
+#else
          GfxElement[x][y] =
            (CAN_BE_CRUMBLED(element) ? EL_SAND : GFX_ELEMENT(element));
+#endif
          player->is_digging = TRUE;
        }
 
@@ -7115,7 +7301,7 @@ int DigField(struct PlayerInfo *player,
        RaiseScoreElement(element);
        PlaySoundLevelElementAction(x, y, element, ACTION_COLLECTING);
 
-       CheckTriggeredElementChange(element, CE_OTHER_COLLECTING);
+       CheckTriggeredElementChange(x, y, element, CE_OTHER_GETS_COLLECTED);
 
        break;
       }
@@ -7193,6 +7379,7 @@ int DigField(struct PlayerInfo *player,
          PlaySoundLevelElementAction(x, y, element, ACTION_PUSHING);
 
        InitMovingField(x, y, move_direction);
+       GfxAction[x][y] = ACTION_PUSHING;
 
        if (mode == DF_SNAP)
          ContinueMoving(x, y);
@@ -7205,14 +7392,15 @@ int DigField(struct PlayerInfo *player,
        if (game.engine_version < RELEASE_IDENT(2,2,0,7))
          player->push_delay_value = GET_NEW_PUSH_DELAY(element);
 
-       CheckTriggeredElementChange(element, CE_OTHER_PUSHING);
-       CheckPlayerElementChange(x, y, element, CE_PUSHED_BY_PLAYER);
+       CheckTriggeredElementChange(x, y, element, CE_OTHER_GETS_PUSHED);
+       CheckElementChange(x, y, element, CE_PUSHED_BY_PLAYER);
 
        break;
       }
       else
       {
-       CheckPlayerElementChange(x, y, element, CE_PRESSED_BY_PLAYER);
+       CheckTriggeredElementChange(x, y, element, CE_OTHER_GETS_PRESSED);
+       CheckElementChange(x, y, element, CE_PRESSED_BY_PLAYER);
       }
 
       return MF_NO_ACTION;
@@ -7750,6 +7938,8 @@ static void HandleGameButtons(struct GadgetInfo *gi)
       else if (audio.music_available)
       { 
        setup.sound = setup.sound_music = TRUE;
+
+       SetAudioMode(setup.sound);
        PlayMusic(level_nr);
       }
       break;
@@ -7758,14 +7948,20 @@ static void HandleGameButtons(struct GadgetInfo *gi)
       if (setup.sound_loops)
        setup.sound_loops = FALSE;
       else if (audio.loops_available)
+      {
        setup.sound = setup.sound_loops = TRUE;
+       SetAudioMode(setup.sound);
+      }
       break;
 
     case SOUND_CTRL_ID_SIMPLE:
       if (setup.sound_simple)
        setup.sound_simple = FALSE;
       else if (audio.sound_available)
+      {
        setup.sound = setup.sound_simple = TRUE;
+       SetAudioMode(setup.sound);
+      }
       break;
 
     default: