fixed overloading laser due to duplicated tiles in damage list in MM engine
[rocksndiamonds.git] / src / game_mm / mm_game.c
index 48bc9a5b875724b6d3cd4e2190b32c17e5fb222f..641aa4fde5c8fdf0759e408bb686fdf80fddc4d6 100644 (file)
@@ -757,7 +757,11 @@ void InitGameActions_MM(void)
   SetTileCursorXY(laser.start_edge.x, laser.start_edge.y);
   SetTileCursorActive(TRUE);
 
+  // restart all delay counters after initially cycling game elements
+  ResetFrameCounter(&rotate_delay);
+  ResetFrameCounter(&pacman_delay);
   ResetFrameCounter(&energy_delay);
+  ResetFrameCounter(&overload_delay);
 }
 
 static void FadeOutLaser(void)
@@ -821,6 +825,13 @@ void AddLaserEdge(int lx, int ly)
 
 void AddDamagedField(int ex, int ey)
 {
+  // prevent adding the same field position again
+  if (laser.num_damages > 0 &&
+      laser.damage[laser.num_damages - 1].x == ex &&
+      laser.damage[laser.num_damages - 1].y == ey &&
+      laser.damage[laser.num_damages - 1].edge == laser.num_edges)
+    return;
+
   laser.damage[laser.num_damages].is_mirror = FALSE;
   laser.damage[laser.num_damages].angle = laser.current_angle;
   laser.damage[laser.num_damages].edge = laser.num_edges;
@@ -958,7 +969,8 @@ static void DeactivateLaserTargetElement(void)
 
 void ScanLaser(void)
 {
-  int element;
+  int element = EL_EMPTY;
+  int last_element = EL_EMPTY;
   int end = 0, rf = laser.num_edges;
 
   // do not scan laser again after the game was lost for whatever reason
@@ -1044,6 +1056,8 @@ void ScanLaser(void)
          hit_mask, LX, LY, ELX, ELY);
 #endif
 
+    last_element = element;
+
     element = Tile[ELX][ELY];
     laser.dest_element = element;
 
@@ -1062,6 +1076,12 @@ void ScanLaser(void)
            ELX, ELY, element);
 #endif
 
+    // special case: leaving fixed MM steel grid (upwards) with non-90° angle
+    if (element == EL_EMPTY &&
+       IS_GRID_STEEL(last_element) &&
+       laser.current_angle % 4)                // angle is not 90°
+      element = last_element;
+
     if (element == EL_EMPTY)
     {
       if (!HitOnlyAnEdge(hit_mask))
@@ -1473,6 +1493,10 @@ 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)
   {
+    // prevent cutting through laser emitter with laser beam
+    if (IS_LASER(element))
+      return TRUE;
+
     // skip the whole element before continuing the scan
     do
     {
@@ -2540,33 +2564,43 @@ static void GrowAmoeba(int x, int y)
 }
 
 static void DrawFieldAnimated_MM(int x, int y)
+{
+  DrawField_MM(x, y);
+
+  laser.redraw = TRUE;
+}
+
+static void DrawFieldAnimatedIfNeeded_MM(int x, int y)
 {
   int element = Tile[x][y];
+  int graphic = el2gfx(element);
 
-  if (IS_BLOCKED(x, y))
+  if (!getGraphicInfo_NewFrame(x, y, graphic))
     return;
 
   DrawField_MM(x, y);
 
-  if (IS_MIRROR(element) ||
-      IS_MIRROR_FIXED(element) ||
-      element == EL_PRISM)
+  laser.redraw = TRUE;
+}
+
+static void DrawFieldTwinkle(int x, int y)
+{
+  if (MovDelay[x][y] != 0)     // wait some time before next frame
   {
-    if (MovDelay[x][y] != 0)   // wait some time before next frame
-    {
-      MovDelay[x][y]--;
+    MovDelay[x][y]--;
 
-      if (MovDelay[x][y] != 0)
-      {
-       int graphic = IMG_TWINKLE_WHITE;
-       int frame = getGraphicAnimationFrame(graphic, 10 - MovDelay[x][y]);
+    DrawField_MM(x, y);
 
-       DrawGraphicThruMask_MM(SCREENX(x), SCREENY(y), graphic, frame);
-      }
+    if (MovDelay[x][y] != 0)
+    {
+      int graphic = IMG_TWINKLE_WHITE;
+      int frame = getGraphicAnimationFrame(graphic, 10 - MovDelay[x][y]);
+
+      DrawGraphicThruMask_MM(SCREENX(x), SCREENY(y), graphic, frame);
     }
-  }
 
-  laser.redraw = TRUE;
+    laser.redraw = TRUE;
+  }
 }
 
 static void Explode_MM(int x, int y, int phase, int mode)
@@ -3031,8 +3065,6 @@ void RotateMirror(int x, int y, int button)
         IS_POLAR(Tile[x][y]) ||
         IS_POLAR_CROSS(Tile[x][y])) && x == ELX && y == ELY)
     {
-      check = 0;
-
       if (IS_BEAMER(Tile[x][y]))
       {
 #if 0
@@ -3040,12 +3072,13 @@ void RotateMirror(int x, int y, int button)
              LX, LY, laser.beamer_edge, laser.beamer[1].num);
 #endif
 
-#if 0
-       laser.num_edges--;
-#endif
+       if (check == 1)
+         laser.num_edges--;
       }
 
       ScanLaser();
+
+      check = 0;
     }
 
     if (check == 2)
@@ -3210,8 +3243,16 @@ static void GameActions_MM_Ext(void)
       MeltIce(x, y);
     else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_AMOEBA)
       GrowAmoeba(x, y);
-    else
+    else if (IS_MIRROR(element) ||
+            IS_MIRROR_FIXED(element) ||
+            element == EL_PRISM)
+      DrawFieldTwinkle(x, y);
+    else if (element == EL_GRAY_BALL_OPENING ||
+            element == EL_BOMB_ACTIVE ||
+            element == EL_MINE_ACTIVE)
       DrawFieldAnimated_MM(x, y);
+    else if (!IS_BLOCKED(x, y))
+      DrawFieldAnimatedIfNeeded_MM(x, y);
   }
 
   AutoRotateMirrors();