fixed redrawing redefined graphics for steel or wooden grid in MM engine
[rocksndiamonds.git] / src / game_mm / mm_game.c
index bd957ee37952a2a59d22c566956d730bbac12e55..c87bc90a0aa4afc22ab67372d9dd5599fe8c5f93 100644 (file)
@@ -809,6 +809,10 @@ static void GameOver_MM(int game_over_cause)
   game_mm.game_over = TRUE;
   game_mm.game_over_cause = game_over_cause;
 
+  // do not ask to play again if game was never actually played
+  if (!game.GamePlayed)
+    return;
+
   if (setup.ask_on_game_over)
     game.restart_game_message = (game_over_cause == GAME_OVER_BOMB ?
                                 "Bomb killed Mc Duffin! Play it again?" :
@@ -1049,24 +1053,51 @@ static void ScanLaser(void)
       break;
     }
 
-    if (hit_mask == (HIT_MASK_TOPRIGHT | HIT_MASK_BOTTOMLEFT))
-    {
-      /* we have hit the top-right and bottom-left element --
-        choose the bottom-left one */
-      /* !!! THIS CAN BE DONE MORE INTELLIGENTLY, FOR EXAMPLE, IF ONE
-        ELEMENT WAS STEEL AND THE OTHER ONE WAS ICE => ALWAYS CHOOSE
-        THE ICE AND MELT IT AWAY INSTEAD OF OVERLOADING LASER !!! */
-      ELX = (LX - 2) / TILEX;
-      ELY = (LY + 2) / 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);
 
-    if (hit_mask == (HIT_MASK_TOPLEFT | HIT_MASK_BOTTOMRIGHT))
+    // 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)
     {
-      /* we have hit the top-left and bottom-right element --
-        choose the top-left one */
-      // !!! SEE ABOVE !!!
-      ELX = (LX - 2) / TILEX;
-      ELY = (LY - 2) / TILEY;
+      // compare the two diagonally adjacent elements
+      int xoffset = 2;
+      int yoffset = 2 * (diag_1 ? -1 : +1);
+      int elx1 = (LX - xoffset) / TILEX;
+      int ely1 = (LY + yoffset) / TILEY;
+      int elx2 = (LX + xoffset) / TILEX;
+      int ely2 = (LY - yoffset) / TILEY;
+      int e1 = Tile[elx1][ely1];
+      int e2 = Tile[elx2][ely2];
+      boolean use_element_1 = FALSE;
+
+      if (IS_WALL_ICE(e1) || IS_WALL_ICE(e2))
+      {
+       if (IS_WALL_ICE(e1) && IS_WALL_ICE(e2))
+         use_element_1 = (RND(2) ? TRUE : FALSE);
+       else if (IS_WALL_ICE(e1))
+         use_element_1 = TRUE;
+      }
+      else if (IS_WALL_AMOEBA(e1) || IS_WALL_AMOEBA(e2))
+      {
+       // if both tiles match, we can just select the first one
+       if (IS_WALL_AMOEBA(e1))
+         use_element_1 = TRUE;
+      }
+      else if (IS_ABSORBING_BLOCK(e1) || IS_ABSORBING_BLOCK(e2))
+      {
+       // if both tiles match, we can just select the first one
+       if (IS_ABSORBING_BLOCK(e1))
+         use_element_1 = TRUE;
+      }
+
+      ELX = (use_element_1 ? elx1 : elx2);
+      ELY = (use_element_1 ? ely1 : ely2);
     }
 
 #if 0
@@ -1691,6 +1722,8 @@ static boolean HitElement(int element, int hit_mask)
                      element == EL_MINE ? EL_MINE_ACTIVE :
                      EL_GRAY_BALL_ACTIVE);
 
+    GfxFrame[ELX][ELY] = 0;            // restart animation
+
     laser.dest_element_last = Tile[ELX][ELY];
     laser.dest_element_last_x = ELX;
     laser.dest_element_last_y = ELY;
@@ -1973,10 +2006,16 @@ static boolean HitPolarizer(int element, int hit_mask)
   }
   else if (IS_GRID_STEEL(element))
   {
+    // may be required if graphics for steel grid redefined
+    AddDamagedField(ELX, ELY);
+
     return HitReflectingWalls(element, hit_mask);
   }
   else // IS_GRID_WOOD
   {
+    // may be required if graphics for wooden grid redefined
+    AddDamagedField(ELX, ELY);
+
     return HitAbsorbingWalls(element, hit_mask);
   }
 
@@ -2026,11 +2065,9 @@ static boolean HitBlock(int element, int hit_mask)
   if (element == EL_GATE_STONE || element == EL_GATE_WOOD)
   {
     int xs = XS / 2, ys = YS / 2;
-    int hit_mask_diagonal1 = HIT_MASK_TOPRIGHT | HIT_MASK_BOTTOMLEFT;
-    int hit_mask_diagonal2 = HIT_MASK_TOPLEFT | HIT_MASK_BOTTOMRIGHT;
 
-    if ((hit_mask & hit_mask_diagonal1) == hit_mask_diagonal1 ||
-       (hit_mask & hit_mask_diagonal2) == hit_mask_diagonal2)
+    if ((hit_mask & HIT_MASK_DIAGONAL_1) == HIT_MASK_DIAGONAL_1 ||
+       (hit_mask & HIT_MASK_DIAGONAL_2) == HIT_MASK_DIAGONAL_2)
     {
       laser.overloaded = (element == EL_GATE_STONE);
 
@@ -2069,11 +2106,9 @@ static boolean HitBlock(int element, int hit_mask)
   if (element == EL_BLOCK_STONE || element == EL_BLOCK_WOOD)
   {
     int xs = XS / 2, ys = YS / 2;
-    int hit_mask_diagonal1 = HIT_MASK_TOPRIGHT | HIT_MASK_BOTTOMLEFT;
-    int hit_mask_diagonal2 = HIT_MASK_TOPLEFT | HIT_MASK_BOTTOMRIGHT;
 
-    if ((hit_mask & hit_mask_diagonal1) == hit_mask_diagonal1 ||
-       (hit_mask & hit_mask_diagonal2) == hit_mask_diagonal2)
+    if ((hit_mask & HIT_MASK_DIAGONAL_1) == HIT_MASK_DIAGONAL_1 ||
+       (hit_mask & HIT_MASK_DIAGONAL_2) == HIT_MASK_DIAGONAL_2)
     {
       laser.overloaded = (element == EL_BLOCK_STONE);
 
@@ -2403,10 +2438,18 @@ static boolean HitAbsorbingWalls(int element, int hit_mask)
 
   if (IS_WALL_ICE(element))
   {
+    int lx = LX + XS;
+    int ly = LY + YS;
     int mask;
 
-    mask = (LX + XS) / MINI_TILEX - ELX * 2 + 1;    // Quadrant (horizontal)
-    mask <<= (((LY + YS) / MINI_TILEY - ELY * 2) > 0) * 2;  // || (vertical)
+    // check if laser hit adjacent edges of two diagonal tiles
+    if (ELX != lx / TILEX)
+      lx = LX - XS;
+    if (ELY != ly / TILEY)
+      ly = LY - YS;
+
+    mask =     lx / MINI_TILEX - ELX * 2 + 1;    // Quadrant (horizontal)
+    mask <<= ((ly / MINI_TILEY - ELY * 2) > 0 ? 2 : 0);  // || (vertical)
 
     // check if laser hits wall with an angle of 90°
     if (IS_90_ANGLE(laser.current_angle))
@@ -2458,7 +2501,7 @@ static boolean HitAbsorbingWalls(int element, int hit_mask)
     if (IS_90_ANGLE(laser.current_angle))
       mask += mask * (2 + IS_HORIZ_ANGLE(laser.current_angle) * 2);
 
-    laser.dest_element = element2 | EL_WALL_AMOEBA;
+    laser.dest_element = element2 | EL_WALL_AMOEBA_BASE;
 
     laser.wall_mask = mask;
   }
@@ -2605,7 +2648,7 @@ static void MeltIce(int x, int y)
   {
     int phase;
     int wall_mask = Store2[x][y];
-    int real_element = Tile[x][y] - EL_WALL_CHANGING + EL_WALL_ICE;
+    int real_element = Tile[x][y] - EL_WALL_CHANGING_BASE + EL_WALL_ICE_BASE;
 
     MovDelay[x][y]--;
     phase = frames - MovDelay[x][y] / delay - 1;
@@ -2617,7 +2660,7 @@ static void MeltIce(int x, int y)
 
       DrawWalls_MM(x, y, Tile[x][y]);
 
-      if (Tile[x][y] == EL_WALL_ICE)
+      if (Tile[x][y] == EL_WALL_ICE_BASE)
        Tile[x][y] = EL_EMPTY;
 
       ScanLaser_FromLastMirror();
@@ -2643,7 +2686,7 @@ static void GrowAmoeba(int x, int y)
   {
     int phase;
     int wall_mask = Store2[x][y];
-    int real_element = Tile[x][y] - EL_WALL_CHANGING + EL_WALL_AMOEBA;
+    int real_element = Tile[x][y] - EL_WALL_CHANGING_BASE + EL_WALL_AMOEBA_BASE;
 
     MovDelay[x][y]--;
     phase = MovDelay[x][y] / delay;
@@ -2708,12 +2751,13 @@ static void Explode_MM(int x, int y, int phase, int mode)
   int num_phase = 9, delay = 2;
   int last_phase = num_phase * delay;
   int half_phase = (num_phase / 2) * delay;
+  int center_element;
 
   laser.redraw = TRUE;
 
   if (phase == EX_PHASE_START)         // initialize 'Store[][]' field
   {
-    int center_element = Tile[x][y];
+    center_element = Tile[x][y];
 
     if (IS_MOVING(x, y) || IS_BLOCKED(x, y))
     {
@@ -2724,12 +2768,14 @@ static void Explode_MM(int x, int y, int phase, int mode)
       Tile[x][y] = center_element;
     }
 
-    Store[x][y] = center_element;
-    Store2[x][y] = mode;
+    if (center_element != EL_GRAY_BALL_ACTIVE)
+      Store[x][y] = EL_EMPTY;
+    Store2[x][y] = center_element;
 
     Tile[x][y] = EL_EXPLODING_OPAQUE;
 
     GfxElement[x][y] = (center_element == EL_BOMB_ACTIVE ? EL_BOMB :
+                       center_element == EL_GRAY_BALL_ACTIVE ? EL_GRAY_BALL :
                        IS_MCDUFFIN(center_element) ? EL_MCDUFFIN :
                        center_element);
 
@@ -2745,7 +2791,9 @@ static void Explode_MM(int x, int y, int phase, int mode)
 
   ExplodePhase[x][y] = (phase < last_phase ? phase + 1 : 0);
 
-  if (phase == half_phase)
+  center_element = Store2[x][y];
+
+  if (phase == half_phase && Store[x][y] == EL_EMPTY)
   {
     Tile[x][y] = EL_EXPLODING_TRANSP;
 
@@ -2755,7 +2803,7 @@ static void Explode_MM(int x, int y, int phase, int mode)
 
   if (phase == last_phase)
   {
-    if (Store[x][y] == EL_BOMB_ACTIVE)
+    if (center_element == EL_BOMB_ACTIVE)
     {
       DrawLaser(0, DL_LASER_DISABLED);
       InitLaser();
@@ -2766,18 +2814,21 @@ static void Explode_MM(int x, int y, int phase, int mode)
 
       laser.overloaded = FALSE;
     }
-    else if (IS_MCDUFFIN(Store[x][y]))
+    else if (IS_MCDUFFIN(center_element))
     {
       GameOver_MM(GAME_OVER_BOMB);
     }
 
-    Tile[x][y] = EL_EMPTY;
+    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);
+
+    if (center_element == EL_GRAY_BALL_ACTIVE)
+      ScanLaser_FromLastMirror();
   }
   else if (!(phase % delay) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
   {
@@ -3296,9 +3347,9 @@ static void GameActions_MM_Ext(void)
       OpenGrayBall(x, y);
     else if (IS_ENVELOPE_OPENING(element))
       OpenEnvelope(x, y);
-    else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_ICE)
+    else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_ICE_BASE)
       MeltIce(x, y);
-    else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_AMOEBA)
+    else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_AMOEBA_BASE)
       GrowAmoeba(x, y);
     else if (IS_MIRROR(element) ||
             IS_MIRROR_FIXED(element) ||
@@ -3474,12 +3525,12 @@ static void GameActions_MM_Ext(void)
       game_mm.ball_choice_pos++;
 
       int new_element = native_mm_level.ball_content[element_pos];
-      int new_element_unmapped = unmap_element(new_element);
+      int new_element_base = map_wall_to_base_element(new_element);
 
-      if (IS_WALL(new_element_unmapped))
+      if (IS_WALL(new_element_base))
       {
        // always use completely filled wall element
-       new_element = new_element_unmapped | 0x000f;
+       new_element = new_element_base | 0x000f;
       }
       else if (native_mm_level.rotate_ball_content &&
               get_num_elements(new_element) > 1)
@@ -3492,9 +3543,12 @@ static void GameActions_MM_Ext(void)
       Store2[ELX][ELY] = TRUE;
     }
 
-    Tile[ELX][ELY] = EL_GRAY_BALL_OPENING;
+    if (native_mm_level.explode_ball)
+      Bang_MM(ELX, ELY);
+    else
+      Tile[ELX][ELY] = EL_GRAY_BALL_OPENING;
 
-    laser.dest_element_last = Tile[ELX][ELY];
+    laser.dest_element = laser.dest_element_last = Tile[ELX][ELY];
 
     return;
   }
@@ -3503,8 +3557,8 @@ static void GameActions_MM_Ext(void)
   {
     PlayLevelSound_MM(ELX, ELY, element, MM_ACTION_SHRINKING);
 
-    Tile[ELX][ELY] = Tile[ELX][ELY] - EL_WALL_ICE + EL_WALL_CHANGING;
-    Store[ELX][ELY] = EL_WALL_ICE;
+    Tile[ELX][ELY] = Tile[ELX][ELY] - EL_WALL_ICE_BASE + EL_WALL_CHANGING_BASE;
+    Store[ELX][ELY] = EL_WALL_ICE_BASE;
     Store2[ELX][ELY] = laser.wall_mask;
 
     laser.dest_element = Tile[ELX][ELY];
@@ -3593,8 +3647,8 @@ static void GameActions_MM_Ext(void)
 
     PlayLevelSound_MM(x, y, element, MM_ACTION_GROWING);
 
-    Tile[x][y] = Tile[x][y] - EL_WALL_AMOEBA + EL_WALL_CHANGING;
-    Store[x][y] = EL_WALL_AMOEBA;
+    Tile[x][y] = Tile[x][y] - EL_WALL_AMOEBA_BASE + EL_WALL_CHANGING_BASE;
+    Store[x][y] = EL_WALL_AMOEBA_BASE;
     Store2[x][y] = wall_mask;
 
     return;