fixed bug that could cause an endless loop (freeze) in the MM engine
[rocksndiamonds.git] / src / game_mm / mm_game.c
index 267f4c5e106dc3b96ef89e4cedf1a0768910fc3a..2805fb77d3af6f33e4bebc4ce3967b61de5bf956 100644 (file)
@@ -463,7 +463,7 @@ static void InitMovDir_MM(int x, int y)
   }
 }
 
-static void InitField(int x, int y)
+static void InitField(int x, int y, boolean init_game)
 {
   int element = Tile[x][y];
 
@@ -475,7 +475,7 @@ static void InitField(int x, int y)
 
     case EL_KETTLE:
     case EL_CELL:
-      if (native_mm_level.auto_count_kettles)
+      if (init_game && native_mm_level.auto_count_kettles)
        game_mm.kettles_still_needed++;
       break;
 
@@ -535,9 +535,12 @@ static void InitField(int x, int y)
       }
       else if (IS_MCDUFFIN(element) || IS_LASER(element))
       {
-       laser.start_edge.x = x;
-       laser.start_edge.y = y;
-       laser.start_angle = get_element_angle(element);
+       if (init_game)
+       {
+         laser.start_edge.x = x;
+         laser.start_edge.y = y;
+         laser.start_angle = get_element_angle(element);
+       }
 
         if (IS_MCDUFFIN(element))
         {
@@ -699,7 +702,7 @@ void InitGameEngine_MM(void)
       Store[x][y] = Store2[x][y] = 0;
       Stop[x][y] = FALSE;
 
-      InitField(x, y);
+      InitField(x, y, TRUE);
     }
   }
 
@@ -1493,6 +1496,8 @@ boolean HitElement(int element, int hit_mask)
   // this is more precise: check if laser would go through the center
   if ((ELX * TILEX + 14 - LX) * YS != (ELY * TILEY + 14 - LY) * XS)
   {
+    int skip_count = 0;
+
     // prevent cutting through laser emitter with laser beam
     if (IS_LASER(element))
       return TRUE;
@@ -1502,15 +1507,18 @@ boolean HitElement(int element, int hit_mask)
     {
       LX += XS;
       LY += YS;
+
+      skip_count++;
     }
     while (ELX == LX/TILEX && ELY == LY/TILEY && LX > 0 && LY > 0);
 
-    if (LX/TILEX > ELX || LY/TILEY > ELY)
+    if ((LX/TILEX > ELX || LY/TILEY > ELY) && skip_count > 1)
     {
       /* skipping scan positions to the right and down skips one scan
         position too much, because this is only the top left scan position
         of totally four scan positions (plus one to the right, one to the
         bottom and one to the bottom right) */
+      /* ... but only roll back scan position if more than one step done */
 
       LX -= XS;
       LY -= YS;
@@ -2482,7 +2490,9 @@ static void OpenSurpriseBall(int x, int y)
     {
       Tile[x][y] = Store[x][y];
       Store[x][y] = Store2[x][y] = 0;
+      MovDir[x][y] = MovPos[x][y] = MovDelay[x][y] = 0;
 
+      InitField(x, y, FALSE);
       DrawField_MM(x, y);
 
       ScanLaser();
@@ -2668,15 +2678,14 @@ static void Explode_MM(int x, int y, int phase, int mode)
       Tile[x][y] = center_element;
     }
 
-    if (center_element == EL_BOMB_ACTIVE || IS_MCDUFFIN(center_element))
-      Store[x][y] = center_element;
-    else
-      Store[x][y] = EL_EMPTY;
-
+    Store[x][y] = center_element;
     Store2[x][y] = mode;
 
     Tile[x][y] = EL_EXPLODING_OPAQUE;
-    GfxElement[x][y] = center_element;
+
+    GfxElement[x][y] = (center_element == EL_BOMB_ACTIVE ? EL_BOMB :
+                       IS_MCDUFFIN(center_element) ? EL_MCDUFFIN :
+                       center_element);
 
     MovDir[x][y] = MovPos[x][y] = MovDelay[x][y] = 0;
 
@@ -2706,7 +2715,6 @@ static void Explode_MM(int x, int y, int phase, int mode)
       InitLaser();
 
       Bang_MM(laser.start_edge.x, laser.start_edge.y);
-      Store[x][y] = EL_EMPTY;
 
       GameOver_MM(GAME_OVER_DELAYED);
 
@@ -2714,16 +2722,15 @@ static void Explode_MM(int x, int y, int phase, int mode)
     }
     else if (IS_MCDUFFIN(Store[x][y]))
     {
-      Store[x][y] = EL_EMPTY;
-
       GameOver_MM(GAME_OVER_BOMB);
     }
 
-    Tile[x][y] = Store[x][y];
+    Tile[x][y] = EL_EMPTY;
+
     Store[x][y] = Store2[x][y] = 0;
     MovDir[x][y] = MovPos[x][y] = MovDelay[x][y] = 0;
 
-    InitField(x, y);
+    InitField(x, y, FALSE);
     DrawField_MM(x, y);
   }
   else if (!(phase % delay) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))