fixed bug with macro for laser angle for MM engine
[rocksndiamonds.git] / src / game_mm / mm_game.c
index 6162d7a0d16fa9f767d4a2fdd218326ed26a8a9d..a5b34f290936e49ac503786a01760362e785a201 100644 (file)
@@ -97,7 +97,6 @@ static void RaiseScoreElement_MM(int);
 static void RemoveMovingField_MM(int, int);
 static void InitMovingField_MM(int, int, int);
 static void ContinueMoving_MM(int, int);
-static void Moving2Blocked_MM(int, int, int *, int *);
 
 static void AddLaserEdge(int, int);
 static void ScanLaser(void);
@@ -829,10 +828,12 @@ static void GameOver_MM(int game_over_cause)
 
 static void AddLaserEdge(int lx, int ly)
 {
-  int clx = dSX + lx;
-  int cly = dSY + ly;
+  int full_sxsize = MAX(FULL_SXSIZE, lev_fieldx * TILEX);
+  int full_sysize = MAX(FULL_SYSIZE, lev_fieldy * TILEY);
 
-  if (clx < -2 || cly < -2 || clx >= SXSIZE + 2 || cly >= SYSIZE + 2)
+  // check if laser is still inside visible playfield area (or inside level)
+  if (cSX + lx < REAL_SX || cSX + lx >= REAL_SX + full_sxsize ||
+      cSY + ly < REAL_SY || cSY + ly >= REAL_SY + full_sysize)
   {
     Warn("AddLaserEdge: out of bounds: %d, %d", lx, ly);
 
@@ -880,16 +881,28 @@ static boolean StepBehind(void)
 
 static int getMaskFromElement(int element)
 {
-  if (IS_GRID(element))
-    return MM_MASK_GRID_1 + get_element_phase(element);
-  else if (IS_MCDUFFIN(element))
+  if (IS_MCDUFFIN(element))
     return MM_MASK_MCDUFFIN_RIGHT + get_element_phase(element);
-  else if (IS_RECTANGLE(element) || IS_DF_GRID(element))
+  else if (IS_GRID(element))
+    return MM_MASK_GRID_1 + get_element_phase(element);
+  else if (IS_DF_GRID(element))
+    return MM_MASK_RECTANGLE;
+  else if (IS_RECTANGLE(element))
     return MM_MASK_RECTANGLE;
   else
     return MM_MASK_CIRCLE;
 }
 
+static int getLevelFromLaserX(int x)
+{
+  return x / TILEX - (x < 0 ? 1 : 0);          // correct negative values
+}
+
+static int getLevelFromLaserY(int y)
+{
+  return y / TILEY - (y < 0 ? 1 : 0);          // correct negative values
+}
+
 static int ScanPixel(void)
 {
   int hit_mask = 0;
@@ -941,8 +954,8 @@ static int ScanPixel(void)
       int py = LY + (i / 2) * 2;
       int dx = px % TILEX;
       int dy = py % TILEY;
-      int lx = (px + TILEX) / TILEX - 1;  // ...+TILEX...-1 to get correct
-      int ly = (py + TILEY) / TILEY - 1;  // negative values!
+      int lx = getLevelFromLaserX(px);
+      int ly = getLevelFromLaserY(py);
       Pixel pixel;
 
       if (IN_LEV_FIELD(lx, ly))
@@ -968,6 +981,7 @@ static int ScanPixel(void)
       }
       else
       {
+       // check if laser is still inside visible playfield area
        pixel = (cSX + px < REAL_SX || cSX + px >= REAL_SX + FULL_SXSIZE ||
                 cSY + py < REAL_SY || cSY + py >= REAL_SY + FULL_SYSIZE);
       }
@@ -1058,31 +1072,55 @@ static void ScanLaser(void)
          LX, LY, XS, YS);
 #endif
 
-    // hit something -- check out what it was
-    ELX = (LX + XS) / TILEX;
-    ELY = (LY + YS) / TILEY;
+    // check if laser scan has hit two diagonally adjacent element corners
+    boolean diag_1 = ((hit_mask & HIT_MASK_DIAGONAL_1) == HIT_MASK_DIAGONAL_1);
+    boolean diag_2 = ((hit_mask & HIT_MASK_DIAGONAL_2) == HIT_MASK_DIAGONAL_2);
+
+    // check if laser scan has crossed element boundaries (not just mini tiles)
+    boolean cross_x = (getLevelFromLaserX(LX) != getLevelFromLaserX(LX + 2));
+    boolean cross_y = (getLevelFromLaserY(LY) != getLevelFromLaserY(LY + 2));
+
+    if (cross_x || cross_y)
+    {
+      // hit something at next tile -- check out what it was
+      ELX = getLevelFromLaserX(LX + XS);
+      ELY = getLevelFromLaserY(LY + YS);
+    }
+    else
+    {
+      // hit something at same tile -- check out what it was
+      ELX = getLevelFromLaserX(LX);
+      ELY = getLevelFromLaserY(LY);
+    }
 
 #if 0
     Debug("game:mm:ScanLaser", "hit_mask (1) == '%x' (%d, %d) (%d, %d)",
          hit_mask, LX, LY, ELX, ELY);
 #endif
 
-    if (!IN_LEV_FIELD(ELX, ELY) || !IN_PIX_FIELD(LX, LY))
+    if (!IN_LEV_FIELD(ELX, ELY))
     {
+      // laser next step position
+      int x = cSX + LX + XS;
+      int y = cSY + LY + YS;
+
+      // check if next step of laser is still inside visible playfield area
+      if (x >= REAL_SX && x < REAL_SX + FULL_SXSIZE &&
+         y >= REAL_SY && y < REAL_SY + FULL_SYSIZE)
+      {
+       // go on with another step
+       LX += XS;
+       LY += YS;
+
+       continue;
+      }
+
       element = EL_EMPTY;
       laser.dest_element = element;
 
       break;
     }
 
-    // check if laser scan has hit two diagonally adjacent element corners
-    boolean diag_1 = ((hit_mask & HIT_MASK_DIAGONAL_1) == HIT_MASK_DIAGONAL_1);
-    boolean diag_2 = ((hit_mask & HIT_MASK_DIAGONAL_2) == HIT_MASK_DIAGONAL_2);
-
-    // check if laser scan has crossed element boundaries (not just mini tiles)
-    boolean cross_x = (LX / TILEX != (LX + 2) / TILEX);
-    boolean cross_y = (LY / TILEY != (LY + 2) / TILEY);
-
     // handle special case of laser hitting two diagonally adjacent elements
     // (with or without a third corner element behind these two elements)
     if ((diag_1 || diag_2) && cross_x && cross_y)
@@ -1673,6 +1711,7 @@ static boolean HitElement(int element, int hit_mask)
       IS_POLAR_CROSS(element) ||
       IS_DF_MIRROR(element) ||
       IS_DF_MIRROR_AUTO(element) ||
+      IS_DF_MIRROR_FIXED(element) ||
       element == EL_PRISM ||
       element == EL_REFRACTOR)
   {
@@ -1691,7 +1730,8 @@ static boolean HitElement(int element, int hit_mask)
     if (IS_MIRROR(element) ||
        IS_MIRROR_FIXED(element) ||
        IS_DF_MIRROR(element) ||
-       IS_DF_MIRROR_AUTO(element))
+       IS_DF_MIRROR_AUTO(element) ||
+       IS_DF_MIRROR_FIXED(element))
       laser.current_angle = get_mirrored_angle(laser.current_angle,
                                               get_element_angle(element));
 
@@ -2884,26 +2924,28 @@ static void TurnRound(int x, int y)
     int x, y;
   } move_xy[] =
   {
-    { 0, 0 },
-    {-1, 0 },
-    {+1, 0 },
-    { 0, 0 },
-    { 0, -1 },
-    { 0, 0 }, { 0, 0 }, { 0, 0 },
-    { 0, +1 }
+    {  0,  0 },
+    { -1,  0 },
+    { +1,  0 },
+    {  0,  0 },
+    {  0, -1 },
+    {  0,  0 }, { 0, 0 }, { 0, 0 },
+    {  0, +1 }
   };
   static struct
   {
     int left, right, back;
   } turn[] =
   {
-    { 0,       0,              0 },
+    { 0,       0,              0        },
     { MV_DOWN, MV_UP,          MV_RIGHT },
-    { MV_UP,   MV_DOWN,        MV_LEFT },
-    { 0,       0,              0 },
-    { MV_LEFT, MV_RIGHT,       MV_DOWN },
-    { 0,0,0 }, { 0,0,0 },      { 0,0,0 },
-    { MV_RIGHT,        MV_LEFT,        MV_UP }
+    { MV_UP,   MV_DOWN,        MV_LEFT  },
+    { 0,       0,              0        },
+    { MV_LEFT, MV_RIGHT,       MV_DOWN  },
+    { 0,       0,              0        },
+    { 0,       0,              0        },
+    { 0,       0,              0        },
+    { MV_RIGHT,        MV_LEFT,        MV_UP    }
   };
 
   int element = Tile[x][y];
@@ -2951,7 +2993,7 @@ static void StartMoving_MM(int x, int y)
 
     // now make next step
 
-    Moving2Blocked_MM(x, y, &newx, &newy);     // get next screen position
+    Moving2Blocked(x, y, &newx, &newy);        // get next screen position
 
     if (element == EL_PACMAN &&
        IN_LEV_FIELD(newx, newy) && IS_EATABLE4PACMAN(Tile[newx][newy]) &&
@@ -3273,7 +3315,11 @@ static void AutoRotateMirrors(void)
          IS_GRID_WOOD_AUTO(element) ||
          IS_GRID_STEEL_AUTO(element) ||
          element == EL_REFRACTOR)
+      {
        RotateMirror(x, y, MB_RIGHTBUTTON);
+
+       laser.redraw = TRUE;
+      }
     }
   }
 }
@@ -3886,35 +3932,6 @@ static void InitMovingField_MM(int x, int y, int direction)
     Tile[newx][newy] = EL_BLOCKED;
 }
 
-static void Moving2Blocked_MM(int x, int y, int *goes_to_x, int *goes_to_y)
-{
-  int direction = MovDir[x][y];
-  int newx = x + (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
-  int newy = y + (direction == MV_UP   ? -1 : direction == MV_DOWN  ? +1 : 0);
-
-  *goes_to_x = newx;
-  *goes_to_y = newy;
-}
-
-static void Blocked2Moving_MM(int x, int y,
-                             int *comes_from_x, int *comes_from_y)
-{
-  int oldx = x, oldy = y;
-  int direction = MovDir[x][y];
-
-  if (direction == MV_LEFT)
-    oldx++;
-  else if (direction == MV_RIGHT)
-    oldx--;
-  else if (direction == MV_UP)
-    oldy++;
-  else if (direction == MV_DOWN)
-    oldy--;
-
-  *comes_from_x = oldx;
-  *comes_from_y = oldy;
-}
-
 static int MovingOrBlocked2Element_MM(int x, int y)
 {
   int element = Tile[x][y];
@@ -3923,7 +3940,7 @@ static int MovingOrBlocked2Element_MM(int x, int y)
   {
     int oldx, oldy;
 
-    Blocked2Moving_MM(x, y, &oldx, &oldy);
+    Blocked2Moving(x, y, &oldx, &oldy);
 
     return Tile[oldx][oldy];
   }
@@ -3940,13 +3957,13 @@ static void RemoveMovingField_MM(int x, int y)
 
   if (IS_MOVING(x, y))
   {
-    Moving2Blocked_MM(x, y, &newx, &newy);
+    Moving2Blocked(x, y, &newx, &newy);
     if (Tile[newx][newy] != EL_BLOCKED)
       return;
   }
   else if (Tile[x][y] == EL_BLOCKED)
   {
-    Blocked2Moving_MM(x, y, &oldx, &oldy);
+    Blocked2Moving(x, y, &oldx, &oldy);
     if (!IS_MOVING(oldx, oldy))
       return;
   }