fixed bug when rotating beamer using new laser bitmap (MM engine)
[rocksndiamonds.git] / src / game_mm / mm_game.c
index 84242f0be78ce1a6ff95bc6f8ee7d84f6658378d..55dc30bdc637212042210606c7ee91c98c2e7e55 100644 (file)
 #define HEALTH_INC_DELAY       9
 #define HEALTH_DELAY(x)                ((x) ? HEALTH_DEC_DELAY : HEALTH_INC_DELAY)
 
+#define BEGIN_NO_HEADLESS                      \
+  {                                            \
+    boolean last_headless = program.headless;  \
+                                               \
+    program.headless = FALSE;                  \
+
+#define END_NO_HEADLESS                                \
+    program.headless = last_headless;          \
+  }                                            \
+
 /* forward declaration for internal use */
 static int MovingOrBlocked2Element_MM(int, int);
 static void Bang_MM(int, int);
@@ -87,55 +97,10 @@ static void InitMovingField_MM(int, int, int);
 static void ContinueMoving_MM(int, int);
 static void Moving2Blocked_MM(int, int, int *, int *);
 
+/* bitmap for laser beam detection */
+static Bitmap *laser_bitmap = NULL;
 
-#if 0
-// =============================================================================
-// TMP_dump_masks_MM (code)
-// =============================================================================
-
-void TMP_dump_masks_MM()
-{
-  int i;
-
-  printf("static const char mm_masks[10][16][16] =\n");
-  printf("{\n");
-
-  for (i = EL_MM_MASK_MCDUFFIN_RIGHT; i <= EL_MM_MASK_CIRCLE; i++)
-  {
-    int graphic_mask = el2gfx(i);
-    Bitmap *bitmap;
-    int src_x, src_y;
-    int x, y;
-
-    getGraphicSource(graphic_mask, 0, &bitmap, &src_x, &src_y);
-
-    printf("  {\n");
-
-    for (y = 0; y < TILEY; y += 2)
-    {
-      printf("    \"");
-
-      for (x = 0; x < TILEX; x += 2)
-      {
-       Pixel pixel = GetPixel(bitmap, src_x + x, src_y + y);
-
-       printf("%c", (pixel ? 'X' : ' '));
-      }
-
-      printf("\",\n");
-    }
-
-    printf("  },\n");
-  }
-
-  printf("};\n");
-}
-#endif
-
-// =============================================================================
-// TMP_dump_masks_MM (result)
-// =============================================================================
-
+/* element masks for scanning pixels of MM elements */
 static const char mm_masks[10][16][16 + 1] =
 {
   {
@@ -349,6 +314,33 @@ static int get_mirrored_angle(int laser_angle, int mirror_angle)
   return (reflected_angle + 16) % 16;
 }
 
+static void DrawLaserLines(struct XY *points, int num_points, int mode)
+{
+  Pixel pixel_drawto = (mode == DL_LASER_ENABLED ? pen_ray     : pen_bg);
+  Pixel pixel_buffer = (mode == DL_LASER_ENABLED ? WHITE_PIXEL : BLACK_PIXEL);
+
+  DrawLines(drawto, points, num_points, pixel_drawto);
+
+  BEGIN_NO_HEADLESS
+  {
+    DrawLines(laser_bitmap, points, num_points, pixel_buffer);
+  }
+  END_NO_HEADLESS
+}
+
+static boolean CheckLaserPixel(int x, int y)
+{
+  Pixel pixel;
+
+  BEGIN_NO_HEADLESS
+  {
+    pixel = ReadPixel(laser_bitmap, x, y);
+  }
+  END_NO_HEADLESS
+
+  return (pixel == WHITE_PIXEL);
+}
+
 static void InitMovDir_MM(int x, int y)
 {
   int element = Feld[x][y];
@@ -522,9 +514,13 @@ void InitGameEngine_MM()
 {
   int i, x, y;
 
-#if 0
-  TMP_dump_masks_MM();
-#endif
+  BEGIN_NO_HEADLESS
+  {
+    /* initialize laser bitmap to current playfield (screen) size */
+    ReCreateBitmap(&laser_bitmap, drawto->width, drawto->height);
+    ClearRectangle(laser_bitmap, 0, 0, drawto->width, drawto->height);
+  }
+  END_NO_HEADLESS
 
   /* set global game control values */
   game_mm.num_cycle = 0;
@@ -1009,8 +1005,7 @@ void DrawLaserExt(int start_edge, int num_edges, int mode)
 #endif
 
   /* now draw the laser to the backbuffer and (if enabled) to the screen */
-  DrawLines(drawto, &laser.edge[start_edge], num_edges,
-           (mode == DL_LASER_ENABLED ? pen_ray : pen_bg));
+  DrawLaserLines(&laser.edge[start_edge], num_edges, mode);
 
   redraw_mask |= REDRAW_FIELD;
 
@@ -1234,6 +1229,11 @@ void DrawLaser(int start_edge, int mode)
               last_num_edges, start_edge);
 #endif
 
+      // special case when rotating first beamer: delete laser edge on beamer
+      // (but do not start scanning on previous edge to prevent mirror sound)
+      if (last_num_edges - start_edge == 1 && start_edge > 0)
+       DrawLaserLines(&laser.edge[start_edge - 1], 2, DL_LASER_DISABLED);
+
       /* delete first segment from start to the first beamer */
       DrawLaserExt(start_edge, last_num_edges - start_edge, DL_LASER_DISABLED);
     }
@@ -2905,25 +2905,24 @@ boolean ObjHit(int obx, int oby, int bits)
 
   if (bits & HIT_POS_CENTER)
   {
-    if (ReadPixel(drawto, SX + obx + 15, SY + oby + 15) == pen_ray)
+    if (CheckLaserPixel(SX + obx + 15,
+                       SY + oby + 15))
       return TRUE;
   }
 
   if (bits & HIT_POS_EDGE)
   {
     for (i = 0; i < 4; i++)
-      if (ReadPixel(drawto,
-                   SX + obx + 31 * (i % 2),
-                   SY + oby + 31 * (i / 2)) == pen_ray)
+      if (CheckLaserPixel(SX + obx + 31 * (i % 2),
+                         SY + oby + 31 * (i / 2)))
        return TRUE;
   }
 
   if (bits & HIT_POS_BETWEEN)
   {
     for (i = 0; i < 4; i++)
-      if (ReadPixel(drawto,
-                   SX + 4 + obx + 22 * (i % 2),
-                   SY + 4 + oby + 22 * (i / 2)) == pen_ray)
+      if (CheckLaserPixel(SX + 4 + obx + 22 * (i % 2),
+                         SY + 4 + oby + 22 * (i / 2)))
        return TRUE;
   }
 
@@ -3503,13 +3502,12 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     {
       if (laser.wall_mask & (1 << i))
       {
-       if (ReadPixel(drawto,
-                     SX + ELX * TILEX + 14 + (i % 2) * 2,
-                     SY + ELY * TILEY + 31 * (i / 2)) == pen_ray)
+       if (CheckLaserPixel(SX + ELX * TILEX + 14 + (i % 2) * 2,
+                           SY + ELY * TILEY + 31 * (i / 2)))
          break;
-       if (ReadPixel(drawto,
-                     SX + ELX * TILEX + 31 * (i % 2),
-                     SY + ELY * TILEY + 14 + (i / 2) * 2) == pen_ray)
+
+       if (CheckLaserPixel(SX + ELX * TILEX + 31 * (i % 2),
+                           SY + ELY * TILEY + 14 + (i / 2) * 2))
          break;
       }
     }
@@ -3520,9 +3518,8 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     {
       if (laser.wall_mask & (1 << i))
       {
-       if (ReadPixel(drawto,
-                     SX + ELX * TILEX + 31 * (i % 2),
-                     SY + ELY * TILEY + 31 * (i / 2)) == pen_ray)
+       if (CheckLaserPixel(SX + ELX * TILEX + 31 * (i % 2),
+                           SY + ELY * TILEY + 31 * (i / 2)))
          break;
       }
     }
@@ -3531,8 +3528,8 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
     if (laser.num_beamers > 0 ||
        k1 < 1 || k2 < 4 || k3 < 4 ||
-       ReadPixel(drawto, SX + ELX * TILEX + 14, SY + ELY * TILEY + 14)
-       == pen_ray)
+       CheckLaserPixel(SX + ELX * TILEX + 14,
+                       SY + ELY * TILEY + 14))
     {
       laser.num_edges = r;
       laser.num_damages = d;