rnd-20030825-1-src
[rocksndiamonds.git] / src / game.c
index 2f2b0381cd79d02f3e6f12b28bf248b4e196359f..7b3017e95a7f2864f62b877f913e4bebceccb933 100644 (file)
@@ -1222,47 +1222,60 @@ void InitGame()
   {
     int start_x = 0, start_y = 0;
     int found_rating = 0;
+    int found_element = EL_UNDEFINED;
 
-    for(y=0; y < lev_fieldy; y++)
+    for(y=0; y < lev_fieldy; y++) for(x=0; x < lev_fieldx; x++)
     {
-      for(x=0; x < lev_fieldx; x++)
+      int element = Feld[x][y];
+      int content;
+      int xx, yy;
+      boolean is_player;
+
+      if (!IS_CUSTOM_ELEMENT(element))
+       continue;
+
+      if (CAN_CHANGE(element))
       {
-       int element = Feld[x][y];
+       content = element_info[element].change.target_element;
+       is_player = ELEM_IS_PLAYER(content);
 
-       if (IS_CUSTOM_ELEMENT(element))
+       if (is_player && (found_rating < 3 || element < found_element))
        {
-         int xx, yy;
+         start_x = x;
+         start_y = y;
 
-         for(yy=0; yy < 3; yy++)
-         {
-           for(xx=0; xx < 3; xx++)
-           {
-             int content;
-             boolean is_player;
+         found_rating = 3;
+         found_element = element;
+       }
+      }
+
+      for(yy=0; yy < 3; yy++) for(xx=0; xx < 3; xx++)
+      {
+       content = element_info[element].content[xx][yy];
+       is_player = ELEM_IS_PLAYER(content);
 
-             content = element_info[element].content[xx][yy];
-             is_player = (ELEM_IS_PLAYER(content) || content == EL_SP_MURPHY);
+       if (is_player && (found_rating < 2 || element < found_element))
+       {
+         start_x = x + xx - 1;
+         start_y = y + yy - 1;
 
-             if (is_player && found_rating < 2)
-             {
-               start_x = x + xx - 1;
-               start_y = y + yy - 1;
+         found_rating = 2;
+         found_element = element;
+       }
 
-               found_rating = 2;
-             }
+       if (!CAN_CHANGE(element))
+         continue;
 
-             content = element_info[element].change.content[xx][yy];
-             is_player = (ELEM_IS_PLAYER(content) || content == EL_SP_MURPHY);
+       content = element_info[element].change.content[xx][yy];
+       is_player = ELEM_IS_PLAYER(content);
 
-             if (is_player && found_rating < 1)
-             {
-               start_x = x + xx - 1;
-               start_y = y + yy - 1;
+       if (is_player && (found_rating < 1 || element < found_element))
+       {
+         start_x = x + xx - 1;
+         start_y = y + yy - 1;
 
-               found_rating = 1;
-             }
-           }
-         }
+         found_rating = 1;
+         found_element = element;
        }
       }
     }
@@ -3607,7 +3620,8 @@ void StartMoving(int x, int y)
 
   if (CAN_FALL(element) && y < lev_fieldy - 1)
   {
-    if ((x>0 && IS_PLAYER(x-1, y)) || (x<lev_fieldx-1 && IS_PLAYER(x+1, y)))
+    if ((x > 0 && IS_PLAYER(x - 1, y)) ||
+       (x < lev_fieldx-1 && IS_PLAYER(x + 1, y)))
       if (JustBeingPushed(x, y))
        return;
 
@@ -4008,7 +4022,8 @@ void StartMoving(int x, int y)
 
     Moving2Blocked(x, y, &newx, &newy);        /* get next screen position */
 
-    if (DONT_COLLIDE_WITH(element) && IS_PLAYER(newx, newy) &&
+    if (DONT_COLLIDE_WITH(element) &&
+       IN_LEV_FIELD(newx, newy) && IS_PLAYER(newx, newy) &&
        !PLAYER_PROTECTED(newx, newy))
     {
 #if 1
@@ -5297,13 +5312,14 @@ static void ChangeActiveTrap(int x, int y)
 
 static void ChangeElementNowExt(int x, int y, int target_element)
 {
-#if 0  /* !!! let the player exacpe from a suddenly unaccessible element */
-  if (IS_PLAYER(x, y) && !IS_ACCESSIBLE(target_element))
+  /* check if element under player changes from accessible to unaccessible
+     (needed for special case of dropping element which then changes) */
+  if (IS_PLAYER(x, y) &&
+      IS_ACCESSIBLE(Feld[x][y]) && !IS_ACCESSIBLE(target_element))
   {
     Bang(x, y);
     return;
   }
-#endif
 
   RemoveField(x, y);
   Feld[x][y] = target_element;
@@ -5332,12 +5348,21 @@ static void ChangeElementNow(int x, int y, int element)
 {
   struct ElementChangeInfo *change = &element_info[element].change;
 
+#if 0
+  if (element >= EL_CUSTOM_START + 17 && element <= EL_CUSTOM_START + 39)
+    printf("::: changing... [%d]\n", FrameCounter);
+#endif
+
+#if 0
   /* prevent CheckTriggeredElementChange() from looping */
   Changing[x][y] = TRUE;
+#endif
 
   CheckTriggeredElementChange(x, y, Feld[x][y], CE_OTHER_IS_CHANGING);
 
+#if 0
   Changing[x][y] = FALSE;
+#endif
 
   if (change->explode)
   {
@@ -5528,6 +5553,10 @@ static boolean CheckTriggeredElementChange(int lx, int ly, int trigger_element,
   if (!(trigger_events[trigger_element] & CH_EVENT_BIT(trigger_event)))
     return FALSE;
 
+  /* prevent this function from running into a loop */
+  if (trigger_event == CE_OTHER_IS_CHANGING)
+    Changing[lx][ly] = TRUE;
+
   for (i=0; i<MAX_NUM_ELEMENTS; i++)
   {
     if (!CAN_CHANGE(i) || !HAS_CHANGE_EVENT(i, trigger_event) ||
@@ -5546,10 +5575,16 @@ static boolean CheckTriggeredElementChange(int lx, int ly, int trigger_element,
       {
        ChangeDelay[x][y] = 1;
        ChangeElement(x, y);
+
+       Changing[x][y] = TRUE;  /* do not change just changed elements */
       }
     }
   }
 
+  /* reset change prevention array */
+  for (y=0; y<lev_fieldy; y++) for (x=0; x<lev_fieldx; x++)
+    Changing[x][y] = FALSE;
+
   return TRUE;
 }
 
@@ -6328,6 +6363,10 @@ boolean MoveFigureOneStep(struct PlayerInfo *player,
   if (can_move != MF_MOVING)
     return can_move;
 
+  /* check if DigField() has caused relocation of the player */
+  if (player->jx != jx || player->jy != jy)
+    return MF_NO_ACTION;
+
   StorePlayer[jx][jy] = 0;
   player->last_jx = jx;
   player->last_jy = jy;
@@ -6497,6 +6536,9 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
 
     player->last_move_dir = player->MovDir;
     player->is_moving = TRUE;
+#if 1
+    player->snapped = FALSE;
+#endif
   }
   else
   {
@@ -6573,6 +6615,7 @@ void ScrollFigure(struct PlayerInfo *player, int mode)
     if (Feld[jx][jy] == EL_EXIT_OPEN ||
        Feld[jx][jy] == EL_SP_EXIT_OPEN)
     {
+      DrawPlayer(player);      /* needed here only to cleanup last field */
       RemoveHero(player);
 
       if (local_player->friends_still_needed == 0 ||
@@ -7615,8 +7658,15 @@ boolean SnapField(struct PlayerInfo *player, int dx, int dy)
     {
       player->is_digging = FALSE;
       player->is_collecting = FALSE;
+#if 1
+      player->is_moving = FALSE;
+#endif
     }
 
+#if 0
+    printf("::: trying to snap...\n");
+#endif
+
     return FALSE;
   }
 
@@ -7625,12 +7675,25 @@ boolean SnapField(struct PlayerInfo *player, int dx, int dy)
 
   player->MovDir = snap_direction;
 
+#if 1
+  player->is_digging = FALSE;
+  player->is_collecting = FALSE;
+#if 1
+  player->is_moving = FALSE;
+#endif
+#endif
+
   if (DigField(player, x, y, 0, 0, DF_SNAP) == MF_NO_ACTION)
     return FALSE;
 
   player->snapped = TRUE;
+#if 1
   player->is_digging = FALSE;
   player->is_collecting = FALSE;
+#if 1
+  player->is_moving = FALSE;
+#endif
+#endif
 
   DrawLevelField(x, y);
   BackToFront();