rnd-20040411-1-src
[rocksndiamonds.git] / src / game.c
index adc212db11a05ba46dc5674929f67c4320784271..59ced41619717c82f6fc88b6e1a34d074651ccb7 100644 (file)
@@ -96,6 +96,9 @@
 /* values for other actions */
 #define MOVE_STEPSIZE_NORMAL   (TILEX / MOVE_DELAY_NORMAL_SPEED)
 
+#define GET_DX_FROM_DIR(d)     ((d) == MV_LEFT ? -1 : (d) == MV_RIGHT ? 1 : 0)
+#define GET_DY_FROM_DIR(d)     ((d) == MV_UP   ? -1 : (d) == MV_DOWN  ? 1 : 0)
+
 #define        INIT_GFX_RANDOM()       (SimpleRND(1000000))
 
 #define GET_NEW_PUSH_DELAY(e)  (   (element_info[e].push_delay_fixed) + \
@@ -1633,6 +1636,7 @@ void InitGame()
       AmoebaNr[x][y] = 0;
       WasJustMoving[x][y] = 0;
       WasJustFalling[x][y] = 0;
+      CheckCollision[x][y] = 0;
       Stop[x][y] = FALSE;
       Pushed[x][y] = FALSE;
 
@@ -2518,6 +2522,10 @@ static void RemoveField(int x, int y)
   ChangePage[x][y] = -1;
   Pushed[x][y] = FALSE;
 
+#if 0
+  ExplodeField[x][y] = EX_TYPE_NONE;
+#endif
+
   GfxElement[x][y] = EL_UNDEFINED;
   GfxAction[x][y] = ACTION_DEFAULT;
   GfxDir[x][y] = MV_NO_MOVING;
@@ -3939,6 +3947,7 @@ void Impact(int x, int y)
     return;
   }
 
+  /* !!! not sufficient for all cases -- see EL_PEARL below !!! */
   /* only reset graphic animation if graphic really changes after impact */
   if (impact &&
       el_act_dir2img(element, GfxAction[x][y], MV_DOWN) != el2img(element))
@@ -3954,6 +3963,8 @@ void Impact(int x, int y)
   }
   else if (impact && element == EL_PEARL)
   {
+    ResetGfxAnimation(x, y);
+
     Feld[x][y] = EL_PEARL_BREAKING;
     PlayLevelSound(x, y, SND_PEARL_BREAKING);
     return;
@@ -4073,6 +4084,8 @@ void Impact(int x, int y)
        }
        else if (smashed == EL_PEARL)
        {
+         ResetGfxAnimation(x, y);
+
          Feld[x][y + 1] = EL_PEARL_BREAKING;
          PlayLevelSound(x, y, SND_PEARL_BREAKING);
          return;
@@ -5046,12 +5059,16 @@ void StartMoving(int x, int y)
 #endif
     }
 #if 1
-    else if ((game.engine_version < VERSION_IDENT(2,2,0,7) &&
-             CAN_SMASH(element) && WasJustMoving[x][y] && !Pushed[x][y + 1] &&
-             (Feld[x][y + 1] == EL_BLOCKED)) ||
+    else if ((game.engine_version >= VERSION_IDENT(3,1,0,0) &&
+             CheckCollision[x][y] && !IS_FREE(x, y + 1)) ||
+
             (game.engine_version >= VERSION_IDENT(3,0,7,0) &&
              CAN_SMASH(element) && WasJustFalling[x][y] &&
-             (Feld[x][y + 1] == EL_BLOCKED || IS_PLAYER(x, y + 1))))
+             (Feld[x][y + 1] == EL_BLOCKED || IS_PLAYER(x, y + 1))) ||
+
+            (game.engine_version < VERSION_IDENT(2,2,0,7) &&
+             CAN_SMASH(element) && WasJustMoving[x][y] && !Pushed[x][y + 1] &&
+             (Feld[x][y + 1] == EL_BLOCKED)))
 
 #else
 #if 1
@@ -5213,9 +5230,15 @@ void StartMoving(int x, int y)
 #endif
 
 #if 1
+
+#if 1
+    if (game.engine_version >= VERSION_IDENT(3,1,0,0) &&
+       CheckCollision[x][y] && IN_LEV_FIELD_AND_NOT_FREE(newx, newy))
+#else
     if (game.engine_version >= VERSION_IDENT(3,1,0,0) &&
        WasJustMoving[x][y] && IN_LEV_FIELD(newx, newy) &&
        (Feld[newx][newy] == EL_BLOCKED || IS_PLAYER(newx, newy)))
+#endif
     {
 #if 0
       printf("::: element %d '%s' WasJustMoving %d [%d, %d, %d, %d]\n",
@@ -5603,6 +5626,9 @@ void StartMoving(int x, int y)
          DrawLevelField(newx, newy);
        }
 
+       /* if digged element was about to explode, prevent the explosion */
+       ExplodeField[newx][newy] = EX_TYPE_NONE;
+
        PlayLevelSoundAction(x, y, action);
       }
 
@@ -6059,10 +6085,16 @@ void ContinueMoving(int x, int y)
 
   if (!pushed_by_player)
   {
+    int nextx = newx + dx, nexty = newy + dy;
+    boolean check_collision_again = IN_LEV_FIELD_AND_IS_FREE(nextx, nexty);
+
     WasJustMoving[newx][newy] = 3;
 
     if (CAN_FALL(element) && direction == MV_DOWN)
       WasJustFalling[newx][newy] = 3;
+
+    if ((!CAN_FALL(element) || direction == MV_DOWN) && check_collision_again)
+      CheckCollision[newx][newy] = 2;
   }
 
   if (DONT_TOUCH(element))     /* object may be nasty to player or others */
@@ -6080,6 +6112,26 @@ void ContinueMoving(int x, int y)
       (newy == lev_fieldy - 1 || !IS_FREE(x, newy + 1)))
     Impact(x, newy);
 
+#if 1
+  if (pushed_by_player)
+  {
+    static int trigger_sides[4] =
+    {
+      CH_SIDE_RIGHT,   /* moving left  */
+      CH_SIDE_LEFT,    /* moving right */
+      CH_SIDE_BOTTOM,  /* moving up    */
+      CH_SIDE_TOP,     /* moving down  */
+    };
+    int dig_side = trigger_sides[MV_DIR_BIT(direction)];
+    struct PlayerInfo *player = PLAYERINFO(x, y);
+
+    CheckElementChangeByPlayer(newx, newy, element, CE_PUSHED_BY_PLAYER,
+                              player->index_bit, dig_side);
+    CheckTriggeredElementChangeByPlayer(newx,newy,element,CE_OTHER_GETS_PUSHED,
+                                       player->index_bit, dig_side);
+  }
+#endif
+
 #if 1
   TestIfElementTouchesCustomElement(x, y);     /* empty or new element */
 #endif
@@ -7199,6 +7251,8 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
     {
       boolean is_empty;
       boolean is_diggable;
+      boolean is_collectible;
+      boolean is_removable;
       boolean is_destructible;
       int ex = x + xx - 1;
       int ey = y + yy - 1;
@@ -7240,11 +7294,15 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
                                      IS_WALKABLE(content_element)));
 #endif
       is_diggable = (is_empty || IS_DIGGABLE(e));
+      is_collectible = (is_empty || IS_COLLECTIBLE(e));
+      is_removable = (is_diggable || is_collectible);
       is_destructible = (is_empty || !IS_INDESTRUCTIBLE(e));
 
       can_replace[xx][yy] =
-       ((change->replace_when == CP_WHEN_EMPTY && is_empty) ||
-        (change->replace_when == CP_WHEN_DIGGABLE && is_diggable) ||
+       ((change->replace_when == CP_WHEN_EMPTY        && is_empty) ||
+        (change->replace_when == CP_WHEN_DIGGABLE     && is_diggable) ||
+        (change->replace_when == CP_WHEN_COLLECTIBLE  && is_collectible) ||
+        (change->replace_when == CP_WHEN_REMOVABLE    && is_removable) ||
         (change->replace_when == CP_WHEN_DESTRUCTIBLE && is_destructible));
 
       if (!can_replace[xx][yy])
@@ -8159,6 +8217,8 @@ void GameActions()
       WasJustMoving[x][y]--;
     if (WasJustFalling[x][y] > 0)
       WasJustFalling[x][y]--;
+    if (CheckCollision[x][y] > 0)
+      CheckCollision[x][y]--;
 
     GfxFrame[x][y]++;
 
@@ -10504,12 +10564,18 @@ int DigField(struct PlayerInfo *player,
 
     default:
 
+#if 1
+      if (IS_WALKABLE(element) && ACCESS_FROM(element, opposite_direction))
+#else
       if (IS_WALKABLE(element))
+#endif
       {
        int sound_action = ACTION_WALKING;
 
+#if 0
        if (!ACCESS_FROM(element, opposite_direction))
          return MF_NO_ACTION;  /* field not accessible from this direction */
+#endif
 
 #if 1
        if (element == EL_EMPTY_SPACE &&
@@ -10547,13 +10613,18 @@ int DigField(struct PlayerInfo *player,
 
        break;
       }
+#if 1
+      else if (IS_PASSABLE(element) && canPassField(x, y, move_direction))
+#else
       else if (IS_PASSABLE(element))
+#endif
       {
-#if 1
+#if 0
        if (!canPassField(x, y, move_direction))
          return MF_NO_ACTION;
 #else
 
+#if 0
 #if 1
        if (!IN_LEV_FIELD(nextx, nexty) || IS_PLAYER(nextx, nexty) ||
            !IS_WALKABLE_FROM(Feld[nextx][nexty], move_direction) ||
@@ -10563,6 +10634,7 @@ int DigField(struct PlayerInfo *player,
        if (!IN_LEV_FIELD(nextx, nexty) || !IS_FREE(nextx, nexty))
          return MF_NO_ACTION;
 #endif
+#endif
 
 #if 1
        if (!ACCESS_FROM(element, opposite_direction))
@@ -10708,7 +10780,8 @@ int DigField(struct PlayerInfo *player,
          ShowEnvelope(element - EL_ENVELOPE_1);
 #endif
        }
-       else if (IS_DROPPABLE(element)) /* can be collected and dropped */
+       else if (IS_DROPPABLE(element) ||
+                IS_THROWABLE(element)) /* can be collected and dropped */
        {
          int i;
 
@@ -10892,6 +10965,10 @@ int DigField(struct PlayerInfo *player,
        else
          player->push_delay_value = -1;        /* get new value later */
 
+#if 1
+       /* check for element change _after_ element has been pushed! */
+#else
+
 #if 1
       /* !!! TEST ONLY !!! */
        CheckElementChangeByPlayer(x, y, element, CE_PUSHED_BY_PLAYER,
@@ -10903,6 +10980,7 @@ int DigField(struct PlayerInfo *player,
                                            player->index_bit, dig_side);
        CheckElementChangeByPlayer(x, y, element, CE_PUSHED_BY_PLAYER,
                                   player->index_bit, dig_side);
+#endif
 #endif
 
        break;
@@ -11133,10 +11211,10 @@ boolean DropElement(struct PlayerInfo *player)
     CH_SIDE_TOP,       /* dropping up    */
     CH_SIDE_BOTTOM,    /* dropping down  */
   };
-  int jx = player->jx, jy = player->jy;
+  int old_element, new_element;
+  int dropx = player->jx, dropy = player->jy;
   int drop_direction = player->MovDir;
   int drop_side = trigger_sides[MV_DIR_BIT(drop_direction)];
-  int old_element = Feld[jx][jy];
   int drop_element = (player->inventory_size > 0 ?
                      player->inventory_element[player->inventory_size - 1] :
                      player->inventory_infinite_element != EL_UNDEFINED ?
@@ -11144,7 +11222,18 @@ boolean DropElement(struct PlayerInfo *player)
                      player->dynabombs_left > 0 ?
                      EL_DYNABOMB_PLAYER_1_ACTIVE + player->index_nr :
                      EL_UNDEFINED);
-  int new_element = drop_element;      /* default: element does not change */
+
+  if (IS_THROWABLE(drop_element))
+  {
+    dropx += GET_DX_FROM_DIR(drop_direction);
+    dropy += GET_DY_FROM_DIR(drop_direction);
+
+    if (!IN_LEV_FIELD(dropx, dropy))
+      return FALSE;
+  }
+
+  old_element = Feld[dropx][dropy];    /* old element at dropping position */
+  new_element = drop_element;          /* default: no change when dropping */
 
   /* check if player is active, not moving and ready to drop */
   if (!player->active || player->MovPos || player->drop_delay > 0)
@@ -11177,10 +11266,10 @@ boolean DropElement(struct PlayerInfo *player)
 #endif
 
   if (old_element != EL_EMPTY)
-    Back[jx][jy] = old_element;                /* store old element on this field */
+    Back[dropx][dropy] = old_element;  /* store old element on this field */
 
-  ResetGfxAnimation(jx, jy);
-  ResetRandomAnimationValue(jx, jy);
+  ResetGfxAnimation(dropx, dropy);
+  ResetRandomAnimationValue(dropx, dropy);
 
   if (player->inventory_size > 0 ||
       player->inventory_infinite_element != EL_UNDEFINED)
@@ -11201,34 +11290,35 @@ boolean DropElement(struct PlayerInfo *player)
        new_element = EL_SP_DISK_RED_ACTIVE;
     }
 
-    Feld[jx][jy] = new_element;
+    Feld[dropx][dropy] = new_element;
 
-    if (IN_SCR_FIELD(SCREENX(jx), SCREENY(jy)))
-      DrawGraphicThruMask(SCREENX(jx), SCREENY(jy), el2img(Feld[jx][jy]), 0);
+    if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
+      DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
+                         el2img(Feld[dropx][dropy]), 0);
 
-    PlayLevelSoundAction(jx, jy, ACTION_DROPPING);
+    PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
 
 #if 1
     /* needed if previous element just changed to "empty" in the last frame */
-    Changed[jx][jy] = 0;               /* allow another change */
+    Changed[dropx][dropy] = 0;         /* allow another change */
 #endif
 
 #if 1
     /* !!! TEST ONLY !!! */
-    CheckElementChangeByPlayer(jx, jy, new_element, CE_DROPPED_BY_PLAYER,
+    CheckElementChangeByPlayer(dropx, dropy, new_element, CE_DROPPED_BY_PLAYER,
                               player->index_bit, drop_side);
-    CheckTriggeredElementChangeByPlayer(jx, jy, new_element,
+    CheckTriggeredElementChangeByPlayer(dropx, dropy, new_element,
                                        CE_OTHER_GETS_DROPPED,
                                        player->index_bit, drop_side);
 #else
-    CheckTriggeredElementChangeByPlayer(jx, jy, new_element,
+    CheckTriggeredElementChangeByPlayer(dropx, dropy, new_element,
                                        CE_OTHER_GETS_DROPPED,
                                        player->index_bit, drop_side);
-    CheckElementChangeByPlayer(jx, jy, new_element, CE_DROPPED_BY_PLAYER,
+    CheckElementChangeByPlayer(dropx, dropy, new_element, CE_DROPPED_BY_PLAYER,
                               player->index_bit, drop_side);
 #endif
 
-    TestIfElementTouchesCustomElement(jx, jy);
+    TestIfElementTouchesCustomElement(dropx, dropy);
   }
   else         /* player is dropping a dyna bomb */
   {
@@ -11238,30 +11328,31 @@ boolean DropElement(struct PlayerInfo *player)
     new_element = EL_DYNABOMB_PLAYER_1_ACTIVE + player->index_nr;
 #endif
 
-    Feld[jx][jy] = new_element;
+    Feld[dropx][dropy] = new_element;
 
-    if (IN_SCR_FIELD(SCREENX(jx), SCREENY(jy)))
-      DrawGraphicThruMask(SCREENX(jx), SCREENY(jy), el2img(Feld[jx][jy]), 0);
+    if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
+      DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
+                         el2img(Feld[dropx][dropy]), 0);
 
-    PlayLevelSoundAction(jx, jy, ACTION_DROPPING);
+    PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
   }
 
 
 
 #if 1
 
-  if (Feld[jx][jy] == new_element)     /* uninitialized unless CE change */
+  if (Feld[dropx][dropy] == new_element) /* uninitialized unless CE change */
   {
 #if 1
-    InitField_WithBug1(jx, jy, FALSE);
+    InitField_WithBug1(dropx, dropy, FALSE);
 #else
-    InitField(jx, jy, FALSE);
-    if (CAN_MOVE(Feld[jx][jy]))
-      InitMovDir(jx, jy);
+    InitField(dropx, dropy, FALSE);
+    if (CAN_MOVE(Feld[dropx][dropy]))
+      InitMovDir(dropx, dropy);
 #endif
   }
 
-  new_element = Feld[jx][jy];          /* element might have changed */
+  new_element = Feld[dropx][dropy];    /* element might have changed */
 
   if (IS_CUSTOM_ELEMENT(new_element) && CAN_MOVE(new_element) &&
       element_info[new_element].move_pattern == MV_WHEN_DROPPED)
@@ -11269,37 +11360,46 @@ boolean DropElement(struct PlayerInfo *player)
 #if 0
     int move_stepsize = element_info[new_element].move_stepsize;
 #endif
-    int direction, dx, dy, nextx, nexty;
+    int move_direction, nextx, nexty;
 
     if (element_info[new_element].move_direction_initial == MV_START_AUTOMATIC)
-      MovDir[jx][jy] = player->MovDir;
+      MovDir[dropx][dropy] = drop_direction;
+
+    move_direction = MovDir[dropx][dropy];
+    nextx = dropx + GET_DX_FROM_DIR(move_direction);
+    nexty = dropy + GET_DY_FROM_DIR(move_direction);
 
-    direction = MovDir[jx][jy];
-    dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
-    dy = (direction == MV_UP   ? -1 : direction == MV_DOWN  ? +1 : 0);
-    nextx = jx + dx;
-    nexty = jy + dy;
+#if 1
+      Changed[dropx][dropy] = 0;       /* allow another change */
+      CheckCollision[dropx][dropy] = 2;
+#else
 
     if (IN_LEV_FIELD(nextx, nexty) && IS_FREE(nextx, nexty))
     {
 #if 0
-      WasJustMoving[jx][jy] = 3;
+      WasJustMoving[dropx][dropy] = 3;
 #else
-      InitMovingField(jx, jy, direction);
-      ContinueMoving(jx, jy);
+#if 1
+      InitMovingField(dropx, dropy, move_direction);
+      ContinueMoving(dropx, dropy);
+#endif
 #endif
     }
+#if 1
     else
     {
-      Changed[jx][jy] = 0;             /* allow another change */
+      Changed[dropx][dropy] = 0;       /* allow another change */
 
 #if 1
-      TestIfElementHitsCustomElement(jx, jy, direction);
+      TestIfElementHitsCustomElement(dropx, dropy, move_direction);
 #else
-      CheckElementChangeBySide(jx, jy, new_element, touched_element,
-                              CE_HITTING_SOMETHING, direction);
+      CheckElementChangeBySide(dropx, dropy, new_element, touched_element,
+                              CE_HITTING_SOMETHING, move_direction);
 #endif
     }
+#endif
+
+#endif
 
 #if 0
     player->drop_delay = 2 * TILEX / move_stepsize + 1;