fixed bug with gray ball creating wrong game elements in MM engine
[rocksndiamonds.git] / src / game_mm / mm_game.c
index e3437a7c64cd88faa0f8eaca3b2c39f47ba58ee8..e822706ff9a00902ee49de8dc9fd5115aef8d05a 100644 (file)
@@ -25,9 +25,8 @@
 
 // values for Explode_MM()
 #define EX_PHASE_START         0
-#define EX_NORMAL              0
-#define EX_KETTLE              1
-#define EX_SHORT               2
+#define EX_TYPE_NONE           0
+#define EX_TYPE_NORMAL         (1 << 0)
 
 // special positions in the game control window (relative to control window)
 #define XX_LEVEL               36
@@ -111,13 +110,27 @@ static int hold_x = -1, hold_y = -1;
 static int pacman_nr = -1;
 
 // various game engine delay counters
-static unsigned int rotate_delay = 0;
-static unsigned int pacman_delay = 0;
-static unsigned int energy_delay = 0;
-static unsigned int overload_delay = 0;
+static DelayCounter rotate_delay = { AUTO_ROTATE_DELAY };
+static DelayCounter pacman_delay = { PACMAN_MOVE_DELAY };
+static DelayCounter energy_delay = { ENERGY_DELAY };
+static DelayCounter overload_delay = { 0 };
+
+// element mask positions for scanning pixels of MM elements
+#define MM_MASK_MCDUFFIN_RIGHT 0
+#define MM_MASK_MCDUFFIN_UP    1
+#define MM_MASK_MCDUFFIN_LEFT  2
+#define MM_MASK_MCDUFFIN_DOWN  3
+#define MM_MASK_GRID_1         4
+#define MM_MASK_GRID_2         5
+#define MM_MASK_GRID_3         6
+#define MM_MASK_GRID_4         7
+#define MM_MASK_RECTANGLE      8
+#define MM_MASK_CIRCLE         9
+
+#define NUM_MM_MASKS           10
 
 // element masks for scanning pixels of MM elements
-static const char mm_masks[10][16][16 + 1] =
+static const char mm_masks[NUM_MM_MASKS][16][16 + 1] =
 {
   {
     "                ",
@@ -411,6 +424,20 @@ static void CheckExitMM(void)
     PlayLevelSound_MM(exit_x, exit_y, exit_element, MM_ACTION_OPENING);
 }
 
+static void SetLaserColor(int brightness)
+{
+  int color_min = 0x00;
+  int color_max = brightness;          // (0x00 <= brightness <= 0xFF)
+  int color_up   = color_max * laser.overload_value / MAX_LASER_OVERLOAD;
+  int color_down = color_max - color_up;
+
+  pen_ray =
+    GetPixelFromRGB(window,
+                   (game_mm.laser_red   ? color_max  : color_up),
+                   (game_mm.laser_green ? color_down : color_min),
+                   (game_mm.laser_blue  ? color_down : color_min));
+}
+
 static void InitMovDir_MM(int x, int y)
 {
   int element = Tile[x][y];
@@ -436,7 +463,7 @@ static void InitMovDir_MM(int x, int y)
   }
 }
 
-static void InitField(int x, int y, boolean init_game)
+static void InitField(int x, int y)
 {
   int element = Tile[x][y];
 
@@ -511,6 +538,19 @@ static void InitField(int x, int y, boolean init_game)
        laser.start_edge.x = x;
        laser.start_edge.y = y;
        laser.start_angle = get_element_angle(element);
+
+        if (IS_MCDUFFIN(element))
+        {
+          game_mm.laser_red   = native_mm_level.mm_laser_red;
+          game_mm.laser_green = native_mm_level.mm_laser_green;
+          game_mm.laser_blue  = native_mm_level.mm_laser_blue;
+        }
+        else
+        {
+          game_mm.laser_red   = native_mm_level.df_laser_red;
+          game_mm.laser_green = native_mm_level.df_laser_green;
+          game_mm.laser_blue  = native_mm_level.df_laser_blue;
+        }
       }
 
       break;
@@ -537,7 +577,6 @@ static void InitCycleElements_RotateSingleStep(void)
 
     Tile[x][y] = next_element;
 
-    DrawField_MM(x, y);
     game_mm.cycle[i].steps -= step;
   }
 }
@@ -574,10 +613,7 @@ static void InitLaser(void)
 
   AddLaserEdge(LX, LY);                // set laser starting edge
 
-  pen_ray = GetPixelFromRGB(window,
-                           native_mm_level.laser_red   * 0xFF,
-                           native_mm_level.laser_green * 0xFF,
-                           native_mm_level.laser_blue  * 0xFF);
+  SetLaserColor(0xFF);
 }
 
 void InitGameEngine_MM(void)
@@ -602,6 +638,11 @@ void InitGameEngine_MM(void)
     (native_mm_level.auto_count_kettles ? 0 : native_mm_level.kettles_needed);
   game_mm.lights_still_needed = 0;
   game_mm.num_keys = 0;
+  game_mm.ball_choice_pos = 0;
+
+  game_mm.laser_red = FALSE;
+  game_mm.laser_green = FALSE;
+  game_mm.laser_blue = TRUE;
 
   game_mm.level_solved = FALSE;
   game_mm.game_over = FALSE;
@@ -624,6 +665,9 @@ void InitGameEngine_MM(void)
   laser.fuse_x = laser.fuse_y = -1;
 
   laser.dest_element = EL_EMPTY;
+  laser.dest_element_last = EL_EMPTY;
+  laser.dest_element_last_x = -1;
+  laser.dest_element_last_y = -1;
   laser.wall_mask = 0;
 
   last_LX = 0;
@@ -637,10 +681,10 @@ void InitGameEngine_MM(void)
 
   CT = Ct = 0;
 
-  rotate_delay = 0;
-  pacman_delay = 0;
-  energy_delay = 0;
-  overload_delay = 0;
+  rotate_delay.count = 0;
+  pacman_delay.count = 0;
+  energy_delay.count = 0;
+  overload_delay.count = 0;
 
   ClickElement(-1, -1, -1);
 
@@ -653,10 +697,9 @@ void InitGameEngine_MM(void)
       Angle[x][y] = 0;
       MovPos[x][y] = MovDir[x][y] = MovDelay[x][y] = 0;
       Store[x][y] = Store2[x][y] = 0;
-      Frame[x][y] = 0;
       Stop[x][y] = FALSE;
 
-      InitField(x, y, TRUE);
+      InitField(x, y);
     }
   }
 
@@ -691,6 +734,11 @@ void InitGameActions_MM(void)
       cycle_steps_done++;
     }
 
+    AdvanceFrameCounter();
+    AdvanceGfxFrame();
+
+    DrawLevel_MM();
+
     BackToFront();
 
     ColorCycling();
@@ -708,6 +756,52 @@ 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)
+{
+  int i;
+
+  for (i = 15; i >= 0; i--)
+  {
+    SetLaserColor(0x11 * i);
+
+    DrawLaser(0, DL_LASER_ENABLED);
+
+    BackToFront();
+    Delay_WithScreenUpdates(50);
+  }
+
+  DrawLaser(0, DL_LASER_DISABLED);
+
+  StopSound_MM(SND_MM_GAME_HEALTH_CHARGING);
+}
+
+static void GameOver_MM(int game_over_cause)
+{
+  // do not handle game over if request dialog is already active
+  if (game.request_active)
+    return;
+
+  game_mm.game_over = TRUE;
+  game_mm.game_over_cause = game_over_cause;
+
+  if (setup.ask_on_game_over)
+    game.restart_game_message = (game_over_cause == GAME_OVER_BOMB ?
+                                "Bomb killed Mc Duffin! Play it again?" :
+                                game_over_cause == GAME_OVER_NO_ENERGY ?
+                                "Out of magic energy! Play it again?" :
+                                game_over_cause == GAME_OVER_OVERLOADED ?
+                                "Magic spell hit Mc Duffin! Play it again?" :
+                                NULL);
+
+  SetTileCursorActive(FALSE);
 }
 
 void AddLaserEdge(int lx, int ly)
@@ -731,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;
@@ -757,13 +858,13 @@ static boolean StepBehind(void)
 static int getMaskFromElement(int element)
 {
   if (IS_GRID(element))
-    return IMG_MM_MASK_GRID_1 + get_element_phase(element);
+    return MM_MASK_GRID_1 + get_element_phase(element);
   else if (IS_MCDUFFIN(element))
-    return IMG_MM_MASK_MCDUFFIN_RIGHT + get_element_phase(element);
+    return MM_MASK_MCDUFFIN_RIGHT + get_element_phase(element);
   else if (IS_RECTANGLE(element) || IS_DF_GRID(element))
-    return IMG_MM_MASK_RECTANGLE;
+    return MM_MASK_RECTANGLE;
   else
-    return IMG_MM_MASK_CIRCLE;
+    return MM_MASK_CIRCLE;
 }
 
 static int ScanPixel(void)
@@ -771,8 +872,8 @@ static int ScanPixel(void)
   int hit_mask = 0;
 
 #if 0
-  printf("ScanPixel: start scanning at (%d, %d) [%d, %d] [%d, %d]\n",
-        LX, LY, LX / TILEX, LY / TILEY, LX % TILEX, LY % TILEY);
+  Debug("game:mm:ScanPixel", "start scanning at (%d, %d) [%d, %d] [%d, %d]",
+       LX, LY, LX / TILEX, LY / TILEY, LX % TILEX, LY % TILEY);
 #endif
 
   // follow laser beam until it hits something (at least the screen border)
@@ -785,7 +886,7 @@ static int ScanPixel(void)
     if (SX + LX < REAL_SX || SX + LX >= REAL_SX + FULL_SXSIZE ||
        SY + LY < REAL_SY || SY + LY >= REAL_SY + FULL_SYSIZE)
     {
-      printf("ScanPixel: touched screen border!\n");
+      Debug("game:mm:ScanPixel", "touched screen border!");
 
       return HIT_MASK_ALL;
     }
@@ -817,7 +918,7 @@ static int ScanPixel(void)
        }
        else
        {
-         int pos = getMaskFromElement(element) - IMG_MM_MASK_MCDUFFIN_RIGHT;
+         int pos = getMaskFromElement(element);
 
          pixel = (mm_masks[pos][dy / 2][dx / 2] == 'X' ? 1 : 0);
        }
@@ -843,23 +944,54 @@ static int ScanPixel(void)
   return hit_mask;
 }
 
+static void DeactivateLaserTargetElement(void)
+{
+  if (laser.dest_element_last == EL_BOMB_ACTIVE ||
+      laser.dest_element_last == EL_MINE_ACTIVE ||
+      laser.dest_element_last == EL_GRAY_BALL_OPENING)
+  {
+    int x = laser.dest_element_last_x;
+    int y = laser.dest_element_last_y;
+    int element = laser.dest_element_last;
+
+    if (Tile[x][y] == element)
+      Tile[x][y] = (element == EL_BOMB_ACTIVE ? EL_BOMB :
+                   element == EL_MINE_ACTIVE ? EL_MINE : EL_BALL_GRAY);
+
+    if (Tile[x][y] == EL_BALL_GRAY)
+      MovDelay[x][y] = 0;
+
+    laser.dest_element_last = EL_EMPTY;
+    laser.dest_element_last_x = -1;
+    laser.dest_element_last_y = -1;
+  }
+}
+
 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
   if (game_mm.game_over)
     return;
 
+  // do not scan laser if fuse is off
+  if (laser.fuse_off)
+    return;
+
+  DeactivateLaserTargetElement();
+
   laser.overloaded = FALSE;
   laser.stops_inside_element = FALSE;
 
   DrawLaser(0, DL_LASER_ENABLED);
 
 #if 0
-  printf("Start scanning with LX == %d, LY == %d, XS == %d, YS == %d\n",
-        LX, LY, XS, YS);
+  Debug("game:mm:ScanLaser",
+       "Start scanning with LX == %d, LY == %d, XS == %d, YS == %d",
+       LX, LY, XS, YS);
 #endif
 
   while (1)
@@ -877,8 +1009,9 @@ void ScanLaser(void)
     hit_mask = ScanPixel();
 
 #if 0
-    printf("Hit something at LX == %d, LY == %d, XS == %d, YS == %d\n",
-          LX, LY, XS, YS);
+    Debug("game:mm:ScanLaser",
+         "Hit something at LX == %d, LY == %d, XS == %d, YS == %d",
+         LX, LY, XS, YS);
 #endif
 
     // hit something -- check out what it was
@@ -886,8 +1019,8 @@ void ScanLaser(void)
     ELY = (LY + YS) / TILEY;
 
 #if 0
-    printf("hit_mask (1) == '%x' (%d, %d) (%d, %d)\n",
-          hit_mask, LX, LY, ELX, ELY);
+    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))
@@ -919,29 +1052,39 @@ void ScanLaser(void)
     }
 
 #if 0
-    printf("hit_mask (2) == '%x' (%d, %d) (%d, %d)\n",
-          hit_mask, LX, LY, ELX, ELY);
+    Debug("game:mm:ScanLaser", "hit_mask (2) == '%x' (%d, %d) (%d, %d)",
+         hit_mask, LX, LY, ELX, ELY);
 #endif
 
+    last_element = element;
+
     element = Tile[ELX][ELY];
     laser.dest_element = element;
 
 #if 0
-    printf("Hit element %d at (%d, %d) [%d, %d] [%d, %d] [%d]\n",
-          element, ELX, ELY,
-          LX, LY,
-          LX % TILEX, LY % TILEY,
-          hit_mask);
+    Debug("game:mm:ScanLaser",
+         "Hit element %d at (%d, %d) [%d, %d] [%d, %d] [%d]",
+         element, ELX, ELY,
+         LX, LY,
+         LX % TILEX, LY % TILEY,
+         hit_mask);
 #endif
 
 #if 0
     if (!IN_LEV_FIELD(ELX, ELY))
-      printf("WARNING! (1) %d, %d (%d)\n", ELX, ELY, element);
+      Debug("game:mm:ScanLaser", "WARNING! (1) %d, %d (%d)",
+           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(element, hit_mask))
+      if (!HitOnlyAnEdge(hit_mask))
        break;
     }
     else if (element == EL_FUSE_ON)
@@ -997,20 +1140,29 @@ void ScanLaser(void)
     if (rf)
       DrawLaser(rf - 1, DL_LASER_ENABLED);
     rf = laser.num_edges;
+
+    if (!IS_DF_WALL_STEEL(element))
+    {
+      // only used for scanning DF steel walls; reset for all other elements
+      last_LX = 0;
+      last_LY = 0;
+      last_hit_mask = 0;
+    }
   }
 
 #if 0
   if (laser.dest_element != Tile[ELX][ELY])
   {
-    printf("ALARM: laser.dest_element == %d, Tile[ELX][ELY] == %d\n",
-          laser.dest_element, Tile[ELX][ELY]);
+    Debug("game:mm:ScanLaser",
+         "ALARM: laser.dest_element == %d, Tile[ELX][ELY] == %d",
+         laser.dest_element, Tile[ELX][ELY]);
   }
 #endif
 
   if (!end && !laser.stops_inside_element && !StepBehind())
   {
 #if 0
-    printf("ScanLaser: Go one step back\n");
+    Debug("game:mm:ScanLaser", "Go one step back");
 #endif
 
     LX -= XS;
@@ -1026,7 +1178,7 @@ void ScanLaser(void)
 
 #if 0
     if (!IN_LEV_FIELD(ELX, ELY))
-      printf("WARNING! (2) %d, %d\n", ELX, ELY);
+      Debug("game:mm:ScanLaser", "WARNING! (2) %d, %d", ELX, ELY);
 #endif
 }
 
@@ -1036,8 +1188,8 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
   int elx, ely;
 
 #if 0
-  printf("DrawLaserExt: start_edge, num_edges, mode == %d, %d, %d\n",
-        start_edge, num_edges, mode);
+  Debug("game:mm:DrawLaserExt", "start_edge, num_edges, mode == %d, %d, %d",
+       start_edge, num_edges, mode);
 #endif
 
   if (start_edge < 0)
@@ -1057,7 +1209,7 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
 #if 0
   if (mode == DL_LASER_DISABLED)
   {
-    printf("DrawLaser: Delete laser from edge %d\n", start_edge);
+    Debug("game:mm:DrawLaserExt", "Delete laser from edge %d", start_edge);
   }
 #endif
 
@@ -1117,11 +1269,12 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
       int i;
 
       for (i = 0; i < laser.num_beamers; i++)
-       printf("-> %d\n", laser.beamer_edge[i]);
-      printf("DrawLaserExt: IS_BEAMER: [%d]: Hit[%d][%d] == %d [%d]\n",
-            mode, elx, ely, Hit[elx][ely], start_edge);
-      printf("DrawLaserExt: IS_BEAMER: %d / %d\n",
-            get_element_angle(element), laser.damage[damage_start].angle);
+       Debug("game:mm:DrawLaserExt", "-> %d", laser.beamer_edge[i]);
+
+      Debug("game:mm:DrawLaserExt", "IS_BEAMER: [%d]: Hit[%d][%d] == %d [%d]",
+           mode, elx, ely, Hit[elx][ely], start_edge);
+      Debug("game:mm:DrawLaserExt", "IS_BEAMER: %d / %d",
+           get_element_angle(element), laser.damage[damage_start].angle);
     }
 #endif
 
@@ -1161,8 +1314,8 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
   YS = 2 * Step[laser.current_angle].y;
 
 #if 0
-  printf("DrawLaser: Set (LX, LY) to (%d, %d) [%d]\n",
-        LX, LY, element);
+  Debug("game:mm:DrawLaserExt", "Set (LX, LY) to (%d, %d) [%d]",
+       LX, LY, element);
 #endif
 
   if (start_edge > 0)
@@ -1177,7 +1330,7 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
       int step_size;
 
 #if 0
-      printf("element == %d\n", element);
+      Debug("game:mm:DrawLaserExt", "element == %d", element);
 #endif
 
       if (IS_22_5_ANGLE(laser.current_angle))  // neither 90° nor 45° angle
@@ -1197,10 +1350,9 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
 
 #if 0
       if (IS_BEAMER(element))
-      {
-       printf("start_edge == %d, laser.beamer_edge == %d\n",
-              start_edge, laser.beamer_edge);
-      }
+       Debug("game:mm:DrawLaserExt",
+             "start_edge == %d, laser.beamer_edge == %d",
+             start_edge, laser.beamer_edge);
 #endif
 
       LX += step_size * XS;
@@ -1215,13 +1367,20 @@ static void DrawLaserExt(int start_edge, int num_edges, int mode)
   }
 
 #if 0
-  printf("DrawLaser: Finally: (LX, LY) to (%d, %d) [%d]\n",
-        LX, LY, element);
+  Debug("game:mm:DrawLaserExt", "Finally: (LX, LY) to (%d, %d) [%d]",
+       LX, LY, element);
 #endif
 }
 
 void DrawLaser(int start_edge, int mode)
 {
+  // do not draw laser if fuse is off
+  if (laser.fuse_off && mode == DL_LASER_ENABLED)
+    return;
+
+  if (mode == DL_LASER_DISABLED)
+    DeactivateLaserTargetElement();
+
   if (laser.num_edges - start_edge < 0)
   {
     Warn("DrawLaser: laser.num_edges - start_edge < 0");
@@ -1247,8 +1406,8 @@ void DrawLaser(int start_edge, int mode)
          continue;
 
 #if 0
-       printf("DrawLaser: DL_LASER_ENABLED: i==%d: %d, %d\n",
-              i, laser.beamer_edge[i], tmp_start_edge);
+       Debug("game:mm:DrawLaser", "DL_LASER_ENABLED: i==%d: %d, %d",
+             i, laser.beamer_edge[i], tmp_start_edge);
 #endif
 
        DrawLaserExt(tmp_start_edge, tmp_num_edges, DL_LASER_ENABLED);
@@ -1282,8 +1441,8 @@ void DrawLaser(int start_edge, int mode)
 
 #if 0
       if (last_num_edges - start_edge <= 0)
-       printf("DrawLaser: DL_LASER_DISABLED: %d, %d\n",
-              last_num_edges, start_edge);
+       Debug("game:mm:DrawLaser", "DL_LASER_DISABLED: %d, %d",
+             last_num_edges, start_edge);
 #endif
 
       // special case when rotating first beamer: delete laser edge on beamer
@@ -1310,21 +1469,23 @@ void DrawLaser_MM(void)
 
 boolean HitElement(int element, int hit_mask)
 {
-  if (HitOnlyAnEdge(element, hit_mask))
+  if (HitOnlyAnEdge(hit_mask))
     return FALSE;
 
   if (IS_MOVING(ELX, ELY) || IS_BLOCKED(ELX, ELY))
     element = MovingOrBlocked2Element_MM(ELX, ELY);
 
 #if 0
-  printf("HitElement (1): element == %d\n", element);
+  Debug("game:mm:HitElement", "(1): element == %d", element);
 #endif
 
 #if 0
   if ((ELX * TILEX + 14 - LX) * YS == (ELY * TILEY + 14 - LY) * XS)
-    printf("HitElement (%d): EXACT MATCH @ (%d, %d)\n", element, ELX, ELY);
+    Debug("game:mm:HitElement", "(%d): EXACT MATCH @ (%d, %d)",
+         element, ELX, ELY);
   else
-    printf("HitElement (%d): FUZZY MATCH @ (%d, %d)\n", element, ELX, ELY);
+    Debug("game:mm:HitElement", "(%d): FUZZY MATCH @ (%d, %d)",
+         element, ELX, ELY);
 #endif
 
   AddDamagedField(ELX, ELY);
@@ -1332,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
     {
@@ -1355,7 +1520,7 @@ boolean HitElement(int element, int hit_mask)
   }
 
 #if 0
-  printf("HitElement (2): element == %d\n", element);
+  Debug("game:mm:HitElement", "(2): element == %d", element);
 #endif
 
   if (LX + 5 * XS < 0 ||
@@ -1368,7 +1533,7 @@ boolean HitElement(int element, int hit_mask)
   }
 
 #if 0
-  printf("HitElement (3): element == %d\n", element);
+  Debug("game:mm:HitElement", "(3): element == %d", element);
 #endif
 
   if (IS_POLAR(element) &&
@@ -1399,9 +1564,9 @@ boolean HitElement(int element, int hit_mask)
   {
 #if 0
     if ((ELX * TILEX + 14 - LX) * YS == (ELY * TILEY + 14 - LY) * XS)
-      printf("EXACT MATCH @ (%d, %d)\n", ELX, ELY);
+      Debug("game:mm:HitElement", "EXACT MATCH @ (%d, %d)", ELX, ELY);
     else
-      printf("FUZZY MATCH @ (%d, %d)\n", ELX, ELY);
+      Debug("game:mm:HitElement", "FUZZY MATCH @ (%d, %d)", ELX, ELY);
 #endif
 
     LX = ELX * TILEX + 14;
@@ -1452,14 +1617,14 @@ boolean HitElement(int element, int hit_mask)
     LX += step_size * XS;
     LY += step_size * YS;
 
-#if 0
     // draw sparkles on mirror
-    if ((IS_MIRROR(element) || IS_MIRROR_FIXED(element)) &&
+    if ((IS_MIRROR(element) ||
+        IS_MIRROR_FIXED(element) ||
+        element == EL_PRISM) &&
        current_angle != laser.current_angle)
     {
-      MoveSprite(vp, &Pfeil[2], 4 + 16 * ELX, 5 + 16 * ELY + 1);
+      MovDelay[ELX][ELY] = 11;         // start animation
     }
-#endif
 
     if ((!IS_POLAR(element) && !IS_POLAR_CROSS(element)) &&
        current_angle != laser.current_angle)
@@ -1483,6 +1648,12 @@ boolean HitElement(int element, int hit_mask)
   {
     PlayLevelSound_MM(ELX, ELY, element, MM_ACTION_HITTING);
 
+    Tile[ELX][ELY] = (element == EL_BOMB ? EL_BOMB_ACTIVE : EL_MINE_ACTIVE);
+
+    laser.dest_element_last = Tile[ELX][ELY];
+    laser.dest_element_last_x = ELX;
+    laser.dest_element_last_y = ELY;
+
     if (element == EL_MINE)
       laser.overloaded = TRUE;
   }
@@ -1492,9 +1663,11 @@ boolean HitElement(int element, int hit_mask)
       element == EL_KEY ||
       element == EL_LIGHTBALL ||
       element == EL_PACMAN ||
-      IS_PACMAN(element))
+      IS_PACMAN(element) ||
+      IS_ENVELOPE(element))
   {
-    if (!IS_PACMAN(element))
+    if (!IS_PACMAN(element) &&
+       !IS_ENVELOPE(element))
       Bang_MM(ELX, ELY);
 
     if (element == EL_PACMAN)
@@ -1522,6 +1695,10 @@ boolean HitElement(int element, int hit_mask)
     {
       DeletePacMan(ELX, ELY);
     }
+    else if (IS_ENVELOPE(element))
+    {
+      Tile[ELX][ELY] = EL_ENVELOPE_1_OPENING + ENVELOPE_NR(Tile[ELX][ELY]);
+    }
 
     RaiseScoreElement_MM(element);
 
@@ -1557,7 +1734,7 @@ boolean HitElement(int element, int hit_mask)
   }
 
 #if 0
-  printf("HitElement (4): element == %d\n", element);
+  Debug("game:mm:HitElement", "(4): element == %d", element);
 #endif
 
   if ((IS_BEAMER(element) || IS_FIBRE_OPTIC(element)) &&
@@ -1569,7 +1746,7 @@ boolean HitElement(int element, int hit_mask)
     int step_size;
 
 #if 0
-  printf("HitElement (BEAMER): element == %d\n", element);
+    Debug("game:mm:HitElement", "(BEAMER): element == %d", element);
 #endif
 
     laser.num_damages--;
@@ -1631,12 +1808,13 @@ boolean HitElement(int element, int hit_mask)
   return TRUE;
 }
 
-boolean HitOnlyAnEdge(int element, int hit_mask)
+boolean HitOnlyAnEdge(int hit_mask)
 {
   // check if the laser hit only the edge of an element and, if so, go on
 
 #if 0
-  printf("LX, LY, hit_mask == %d, %d, %d\n", LX, LY, hit_mask);
+  Debug("game:mm:HitOnlyAnEdge", "LX, LY, hit_mask == %d, %d, %d",
+       LX, LY, hit_mask);
 #endif
 
   if ((hit_mask == HIT_MASK_TOPLEFT ||
@@ -1674,14 +1852,14 @@ boolean HitOnlyAnEdge(int element, int hit_mask)
     LY += YS;
 
 #if 0
-    printf("[HitOnlyAnEdge() == TRUE]\n");
+    Debug("game:mm:HitOnlyAnEdge", "[HitOnlyAnEdge() == TRUE]");
 #endif
 
     return TRUE;
   }
 
 #if 0
-    printf("[HitOnlyAnEdge() == FALSE]\n");
+  Debug("game:mm:HitOnlyAnEdge", "[HitOnlyAnEdge() == FALSE]");
 #endif
 
   return FALSE;
@@ -1689,7 +1867,7 @@ boolean HitOnlyAnEdge(int element, int hit_mask)
 
 boolean HitPolarizer(int element, int hit_mask)
 {
-  if (HitOnlyAnEdge(element, hit_mask))
+  if (HitOnlyAnEdge(hit_mask))
     return FALSE;
 
   if (IS_DF_GRID(element))
@@ -1697,8 +1875,8 @@ boolean HitPolarizer(int element, int hit_mask)
     int grid_angle = get_element_angle(element);
 
 #if 0
-    printf("HitPolarizer: angle: grid == %d, laser == %d\n",
-          grid_angle, laser.current_angle);
+    Debug("game:mm:HitPolarizer", "angle: grid == %d, laser == %d",
+         grid_angle, laser.current_angle);
 #endif
 
     AddLaserEdge(LX, LY);
@@ -1735,10 +1913,10 @@ boolean HitPolarizer(int element, int hit_mask)
       LY += YS;
 
 #if 0
-      printf("HitPolarizer: LX, LY == %d, %d [%d, %d] [%d, %d]\n",
-            LX, LY,
-            LX / TILEX, LY / TILEY,
-            LX % TILEX, LY % TILEY);
+      Debug("game:mm:HitPolarizer", "LX, LY == %d, %d [%d, %d] [%d, %d]",
+           LX, LY,
+           LX / TILEX, LY / TILEY,
+           LX % TILEX, LY % TILEY);
 #endif
 
       return FALSE;
@@ -1887,7 +2065,7 @@ boolean HitBlock(int element, int hit_mask)
 
 boolean HitLaserSource(int element, int hit_mask)
 {
-  if (HitOnlyAnEdge(element, hit_mask))
+  if (HitOnlyAnEdge(hit_mask))
     return FALSE;
 
   PlayLevelSound_MM(ELX, ELY, element, MM_ACTION_HITTING);
@@ -1899,7 +2077,7 @@ boolean HitLaserSource(int element, int hit_mask)
 
 boolean HitLaserDestination(int element, int hit_mask)
 {
-  if (HitOnlyAnEdge(element, hit_mask))
+  if (HitOnlyAnEdge(hit_mask))
     return FALSE;
 
   if (element != EL_EXIT_OPEN &&
@@ -2135,7 +2313,7 @@ boolean HitReflectingWalls(int element, int hit_mask)
     }
   }
 
-  if (!HitOnlyAnEdge(element, hit_mask))
+  if (!HitOnlyAnEdge(hit_mask))
   {
     laser.overloaded = TRUE;
 
@@ -2147,7 +2325,7 @@ boolean HitReflectingWalls(int element, int hit_mask)
 
 boolean HitAbsorbingWalls(int element, int hit_mask)
 {
-  if (HitOnlyAnEdge(element, hit_mask))
+  if (HitOnlyAnEdge(hit_mask))
     return FALSE;
 
   if (ABS(XS) == 4 &&
@@ -2301,7 +2479,8 @@ static void OpenSurpriseBall(int x, int y)
     if (!MovDelay[x][y])
     {
       Tile[x][y] = Store[x][y];
-      Store[x][y] = 0;
+      Store[x][y] = Store2[x][y] = 0;
+
       DrawField_MM(x, y);
 
       ScanLaser();
@@ -2309,6 +2488,42 @@ static void OpenSurpriseBall(int x, int y)
   }
 }
 
+static void OpenEnvelope(int x, int y)
+{
+  int num_frames = 8;          // seven frames plus final empty space
+
+  if (!MovDelay[x][y])         // next animation frame
+    MovDelay[x][y] = num_frames;
+
+  if (MovDelay[x][y])          // wait some time before next frame
+  {
+    int nr = ENVELOPE_OPENING_NR(Tile[x][y]);
+
+    MovDelay[x][y]--;
+
+    if (MovDelay[x][y] > 0 && IN_SCR_FIELD(x, y))
+    {
+      int graphic = el_act2gfx(EL_ENVELOPE_1 + nr, MM_ACTION_COLLECTING);
+      int frame = num_frames - MovDelay[x][y] - 1;
+
+      DrawGraphicAnimation_MM(x, y, graphic, frame);
+
+      laser.redraw = TRUE;
+    }
+
+    if (MovDelay[x][y] == 0)
+    {
+      Tile[x][y] = EL_EMPTY;
+
+      DrawField_MM(x, y);
+
+      ScanLaser();
+
+      ShowEnvelope_MM(nr);
+    }
+  }
+}
+
 static void MeltIce(int x, int y)
 {
   int frames = 5;
@@ -2390,6 +2605,46 @@ 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 (!getGraphicInfo_NewFrame(x, y, graphic))
+    return;
+
+  DrawField_MM(x, y);
+
+  laser.redraw = TRUE;
+}
+
+static void DrawFieldTwinkle(int x, int y)
+{
+  if (MovDelay[x][y] != 0)     // wait some time before next frame
+  {
+    MovDelay[x][y]--;
+
+    DrawField_MM(x, y);
+
+    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;
+  }
+}
+
 static void Explode_MM(int x, int y, int phase, int mode)
 {
   int num_phase = 9, delay = 2;
@@ -2411,20 +2666,27 @@ static void Explode_MM(int x, int y, int phase, int mode)
       Tile[x][y] = center_element;
     }
 
-    if (center_element == EL_BOMB || IS_MCDUFFIN(center_element))
+    if (center_element == EL_BOMB_ACTIVE || IS_MCDUFFIN(center_element))
       Store[x][y] = center_element;
     else
       Store[x][y] = EL_EMPTY;
 
     Store2[x][y] = mode;
+
     Tile[x][y] = EL_EXPLODING_OPAQUE;
+    GfxElement[x][y] = center_element;
+
     MovDir[x][y] = MovPos[x][y] = MovDelay[x][y] = 0;
-    Frame[x][y] = 1;
+
+    ExplodePhase[x][y] = 1;
 
     return;
   }
 
-  Frame[x][y] = (phase < last_phase ? phase + 1 : 0);
+  if (phase == 1)
+    GfxFrame[x][y] = 0;                // restart explosion animation
+
+  ExplodePhase[x][y] = (phase < last_phase ? phase + 1 : 0);
 
   if (phase == half_phase)
   {
@@ -2436,7 +2698,7 @@ static void Explode_MM(int x, int y, int phase, int mode)
 
   if (phase == last_phase)
   {
-    if (Store[x][y] == EL_BOMB)
+    if (Store[x][y] == EL_BOMB_ACTIVE)
     {
       DrawLaser(0, DL_LASER_DISABLED);
       InitLaser();
@@ -2444,10 +2706,7 @@ static void Explode_MM(int x, int y, int phase, int mode)
       Bang_MM(laser.start_edge.x, laser.start_edge.y);
       Store[x][y] = EL_EMPTY;
 
-      game_mm.game_over = TRUE;
-      game_mm.game_over_cause = GAME_OVER_BOMB;
-
-      SetTileCursorActive(FALSE);
+      GameOver_MM(GAME_OVER_DELAYED);
 
       laser.overloaded = FALSE;
     }
@@ -2455,56 +2714,22 @@ static void Explode_MM(int x, int y, int phase, int mode)
     {
       Store[x][y] = EL_EMPTY;
 
-      game.restart_game_message = "Bomb killed Mc Duffin! Play it again?";
+      GameOver_MM(GAME_OVER_BOMB);
     }
 
     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);
+    InitField(x, y);
     DrawField_MM(x, y);
   }
   else if (!(phase % delay) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
   {
-    int graphic = IMG_MM_DEFAULT_EXPLODING;
-    int graphic_phase = (phase / delay - 1);
-    Bitmap *bitmap;
-    int src_x, src_y;
+    int graphic = el_act2gfx(GfxElement[x][y], MM_ACTION_EXPLODING);
+    int frame = getGraphicAnimationFrameXY(graphic, x, y);
 
-    if (Store2[x][y] == EX_KETTLE)
-    {
-      if (graphic_phase < 3)
-      {
-       graphic = IMG_MM_KETTLE_EXPLODING;
-      }
-      else if (graphic_phase < 5)
-      {
-       graphic_phase += 3;
-      }
-      else
-      {
-       graphic = IMG_EMPTY;
-       graphic_phase = 0;
-      }
-    }
-    else if (Store2[x][y] == EX_SHORT)
-    {
-      if (graphic_phase < 4)
-      {
-       graphic_phase += 4;
-      }
-      else
-      {
-       graphic = IMG_EMPTY;
-       graphic_phase = 0;
-      }
-    }
-
-    getGraphicSource(graphic, graphic_phase, &bitmap, &src_x, &src_y);
-
-    BlitBitmap(bitmap, drawto_field, src_x, src_y, TILEX, TILEY,
-              cFX + x * TILEX, cFY + y * TILEY);
+    DrawGraphicAnimation_MM(x, y, graphic, frame);
 
     MarkTileDirty(x, y);
   }
@@ -2513,38 +2738,21 @@ static void Explode_MM(int x, int y, int phase, int mode)
 static void Bang_MM(int x, int y)
 {
   int element = Tile[x][y];
-  int mode = EX_NORMAL;
 
 #if 0
   DrawLaser(0, DL_LASER_ENABLED);
 #endif
 
-  switch (element)
-  {
-    case EL_KETTLE:
-      mode = EX_KETTLE;
-      break;
-
-    case EL_GATE_STONE:
-    case EL_GATE_WOOD:
-      mode = EX_SHORT;
-      break;
-
-    default:
-      mode = EX_NORMAL;
-      break;
-  }
-
   if (IS_PACMAN(element))
     PlayLevelSound_MM(x, y, element, MM_ACTION_EXPLODING);
-  else if (element == EL_BOMB || IS_MCDUFFIN(element))
+  else if (element == EL_BOMB_ACTIVE || IS_MCDUFFIN(element))
     PlayLevelSound_MM(x, y, element, MM_ACTION_EXPLODING);
   else if (element == EL_KEY)
     PlayLevelSound_MM(x, y, element, MM_ACTION_EXPLODING);
   else
     PlayLevelSound_MM(x, y, element, MM_ACTION_EXPLODING);
 
-  Explode_MM(x, y, EX_PHASE_START, mode);
+  Explode_MM(x, y, EX_PHASE_START, EX_TYPE_NORMAL);
 }
 
 void TurnRound(int x, int y)
@@ -2701,8 +2909,7 @@ static void ContinueMoving_MM(int x, int y)
 
 boolean ClickElement(int x, int y, int button)
 {
-  static unsigned int click_delay = 0;
-  static int click_delay_value = CLICK_DELAY;
+  static DelayCounter click_delay = { CLICK_DELAY };
   static boolean new_button = TRUE;
   boolean element_clicked = FALSE;
   int element;
@@ -2710,8 +2917,8 @@ boolean ClickElement(int x, int y, int button)
   if (button == -1)
   {
     // initialize static variables
-    click_delay = 0;
-    click_delay_value = CLICK_DELAY;
+    click_delay.count = 0;
+    click_delay.value = CLICK_DELAY;
     new_button = TRUE;
 
     return FALSE;
@@ -2724,7 +2931,7 @@ boolean ClickElement(int x, int y, int button)
   if (button == MB_RELEASED)
   {
     new_button = TRUE;
-    click_delay_value = CLICK_DELAY;
+    click_delay.value = CLICK_DELAY;
 
     // release eventually hold auto-rotating mirror
     RotateMirror(x, y, MB_RELEASED);
@@ -2732,7 +2939,7 @@ boolean ClickElement(int x, int y, int button)
     return FALSE;
   }
 
-  if (!FrameReached(&click_delay, click_delay_value) && !new_button)
+  if (!FrameReached(&click_delay) && !new_button)
     return FALSE;
 
   if (button == MB_MIDDLEBUTTON)       // middle button has no function
@@ -2818,8 +3025,14 @@ boolean ClickElement(int x, int y, int button)
 
     element_clicked = TRUE;
   }
+  else if (IS_ENVELOPE(element))
+  {
+    Tile[x][y] = EL_ENVELOPE_1_OPENING + ENVELOPE_NR(element);
 
-  click_delay_value = (new_button ? CLICK_DELAY_FIRST : CLICK_DELAY);
+    element_clicked = TRUE;
+  }
+
+  click_delay.value = (new_button ? CLICK_DELAY_FIRST : CLICK_DELAY);
   new_button = FALSE;
 
   return element_clicked;
@@ -2900,20 +3113,20 @@ 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
-       printf("TEST (%d, %d) [%d] [%d]\n",
-              LX, LY,
-              laser.beamer_edge, laser.beamer[1].num);
+       Debug("game:mm:RotateMirror", "TEST (%d, %d) [%d] [%d]",
+             LX, LY, laser.beamer_edge, laser.beamer[1].num);
 #endif
 
-       laser.num_edges--;
+       if (check == 1)
+         laser.num_edges--;
       }
 
       ScanLaser();
+
+      check = 0;
     }
 
     if (check == 2)
@@ -2925,7 +3138,7 @@ static void AutoRotateMirrors(void)
 {
   int x, y;
 
-  if (!FrameReached(&rotate_delay, AUTO_ROTATE_DELAY))
+  if (!FrameReached(&rotate_delay))
     return;
 
   for (x = 0; x < lev_fieldx; x++)
@@ -3050,7 +3263,7 @@ void ColorCycling(void)
   }
 }
 
-static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
+static void GameActions_MM_Ext(void)
 {
   int element;
   int x, y, i;
@@ -3069,15 +3282,27 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     else if (IS_MOVING(x, y))
       ContinueMoving_MM(x, y);
     else if (IS_EXPLODING(element))
-      Explode_MM(x, y, Frame[x][y], EX_NORMAL);
+      Explode_MM(x, y, ExplodePhase[x][y], EX_TYPE_NORMAL);
     else if (element == EL_EXIT_OPENING)
       OpenExit(x, y);
     else if (element == EL_GRAY_BALL_OPENING)
       OpenSurpriseBall(x, y);
+    else if (IS_ENVELOPE_OPENING(element))
+      OpenEnvelope(x, y);
     else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_ICE)
       MeltIce(x, y);
     else if (IS_WALL_CHANGING(element) && Store[x][y] == EL_WALL_AMOEBA)
       GrowAmoeba(x, y);
+    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();
@@ -3093,7 +3318,7 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
   CT = FrameCounter;
 
-  if (game_mm.num_pacman && FrameReached(&pacman_delay, PACMAN_MOVE_DELAY))
+  if (game_mm.num_pacman && FrameReached(&pacman_delay))
   {
     MovePacMen();
 
@@ -3104,61 +3329,25 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     }
   }
 
-  if (FrameReached(&energy_delay, ENERGY_DELAY))
-  {
-    if (game_mm.energy_left > 0)
-    {
-      game_mm.energy_left--;
-
-      redraw_mask |= REDRAW_DOOR_1;
-    }
-    else if (setup.time_limit && !game_mm.game_over)
-    {
-      int i;
-
-      for (i = 15; i >= 0; i--)
-      {
-#if 0
-       SetRGB(pen_ray, 0x0000, 0x0000, i * color_scale);
-#endif
-       pen_ray = GetPixelFromRGB(window,
-                                 native_mm_level.laser_red   * 0x11 * i,
-                                 native_mm_level.laser_green * 0x11 * i,
-                                 native_mm_level.laser_blue  * 0x11 * i);
-
-       DrawLaser(0, DL_LASER_ENABLED);
-       BackToFront();
-       Delay_WithScreenUpdates(50);
-      }
-
-      StopSound_MM(SND_MM_GAME_HEALTH_CHARGING);
-#if 0
-      FadeMusic();
-#endif
+  // skip all following game actions if game is over
+  if (game_mm.game_over)
+    return;
 
-      DrawLaser(0, DL_LASER_DISABLED);
-      game_mm.game_over = TRUE;
-      game_mm.game_over_cause = GAME_OVER_NO_ENERGY;
+  if (game_mm.energy_left == 0 && !game.no_level_time_limit && game.time_limit)
+  {
+    FadeOutLaser();
 
-      SetTileCursorActive(FALSE);
+    GameOver_MM(GAME_OVER_NO_ENERGY);
 
-      game.restart_game_message = "Out of magic energy! Play it again?";
+    return;
+  }
 
-#if 0
-      if (Request("Out of magic energy! Play it again?",
-                 REQ_ASK | REQ_STAY_CLOSED))
-      {
-       InitGame();
-      }
-      else
-      {
-       game_status = MAINMENU;
-       DrawMainMenu();
-      }
-#endif
+  if (FrameReached(&energy_delay))
+  {
+    if (game_mm.energy_left > 0)
+      game_mm.energy_left--;
 
-      return;
-    }
+    // when out of energy, wait another frame to play "out of time" sound
   }
 
   element = laser.dest_element;
@@ -3166,14 +3355,16 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 #if 0
   if (element != Tile[ELX][ELY])
   {
-    printf("element == %d, Tile[ELX][ELY] == %d\n",
-          element, Tile[ELX][ELY]);
+    Debug("game:mm:GameActions_MM_Ext", "element == %d, Tile[ELX][ELY] == %d",
+         element, Tile[ELX][ELY]);
   }
 #endif
 
   if (!laser.overloaded && laser.overload_value == 0 &&
       element != EL_BOMB &&
+      element != EL_BOMB_ACTIVE &&
       element != EL_MINE &&
+      element != EL_MINE_ACTIVE &&
       element != EL_BALL_GRAY &&
       element != EL_BLOCK_STONE &&
       element != EL_BLOCK_WOOD &&
@@ -3183,9 +3374,11 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
       !IS_WALL_AMOEBA(element))
     return;
 
+  overload_delay.value = HEALTH_DELAY(laser.overloaded);
+
   if (((laser.overloaded && laser.overload_value < MAX_LASER_OVERLOAD) ||
        (!laser.overloaded && laser.overload_value > 0)) &&
-      FrameReached(&overload_delay, HEALTH_DELAY(laser.overloaded)))
+      FrameReached(&overload_delay))
   {
     if (laser.overloaded)
       laser.overload_value++;
@@ -3202,18 +3395,7 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
     if (laser.overload_value < MAX_LASER_OVERLOAD - 8)
     {
-      int color_up = 0xFF * laser.overload_value / MAX_LASER_OVERLOAD;
-      int color_down = 0xFF - color_up;
-
-#if 0
-      SetRGB(pen_ray, (laser.overload_value / 6) * color_scale, 0x0000,
-            (15 - (laser.overload_value / 6)) * color_scale);
-#endif
-      pen_ray =
-       GetPixelFromRGB(window,
-                       (native_mm_level.laser_red  ? 0xFF : color_up),
-                       (native_mm_level.laser_green ? color_down : 0x00),
-                       (native_mm_level.laser_blue  ? color_down : 0x00));
+      SetLaserColor(0xFF);
 
       DrawLaser(0, DL_LASER_ENABLED);
     }
@@ -3251,44 +3433,11 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
     if (laser.overload_value == MAX_LASER_OVERLOAD)
     {
-      int i;
-
       UpdateAndDisplayGameControlValues();
 
-      for (i = 15; i >= 0; i--)
-      {
-#if 0
-       SetRGB(pen_ray, i * color_scale, 0x0000, 0x0000);
-#endif
-
-       pen_ray = GetPixelFromRGB(window, 0x11 * i, 0x00, 0x00);
-
-       DrawLaser(0, DL_LASER_ENABLED);
-       BackToFront();
-       Delay_WithScreenUpdates(50);
-      }
+      FadeOutLaser();
 
-      DrawLaser(0, DL_LASER_DISABLED);
-
-      game_mm.game_over = TRUE;
-      game_mm.game_over_cause = GAME_OVER_OVERLOADED;
-
-      SetTileCursorActive(FALSE);
-
-      game.restart_game_message = "Magic spell hit Mc Duffin! Play it again?";
-
-#if 0
-      if (Request("Magic spell hit Mc Duffin! Play it again?",
-                 REQ_ASK | REQ_STAY_CLOSED))
-      {
-       InitGame();
-      }
-      else
-      {
-       game_status = MAINMENU;
-       DrawMainMenu();
-      }
-#endif
+      GameOver_MM(GAME_OVER_OVERLOADED);
 
       return;
     }
@@ -3304,36 +3453,10 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     if (game_mm.cheat_no_explosion)
       return;
 
-#if 0
-    laser.num_damages--;
-    DrawLaser(0, DL_LASER_DISABLED);
-    laser.num_edges = 0;
-#endif
-
     Bang_MM(ELX, ELY);
 
     laser.dest_element = EL_EXPLODING_OPAQUE;
 
-#if 0
-    Bang_MM(ELX, ELY);
-    laser.num_damages--;
-    DrawLaser(0, DL_LASER_DISABLED);
-
-    laser.num_edges = 0;
-    Bang_MM(laser.start_edge.x, laser.start_edge.y);
-
-    if (Request("Bomb killed Mc Duffin! Play it again?",
-               REQ_ASK | REQ_STAY_CLOSED))
-    {
-      InitGame();
-    }
-    else
-    {
-      game_status = MAINMENU;
-      DrawMainMenu();
-    }
-#endif
-
     return;
   }
 
@@ -3349,26 +3472,38 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
   if (element == EL_BALL_GRAY && CT > native_mm_level.time_ball)
   {
-    static int new_elements[] =
+    if (!Store2[ELX][ELY])     // check if content element not yet determined
     {
-      EL_MIRROR_START,
-      EL_MIRROR_FIXED_START,
-      EL_POLAR_START,
-      EL_POLAR_CROSS_START,
-      EL_PACMAN_START,
-      EL_KETTLE,
-      EL_BOMB,
-      EL_PRISM
-    };
-    int num_new_elements = sizeof(new_elements) / sizeof(int);
-    int new_element = new_elements[RND(num_new_elements)];
-
-    Store[ELX][ELY] = new_element + RND(get_num_elements(new_element));
+      int last_anim_random_frame = gfx.anim_random_frame;
+      int element_pos;
+
+      if (native_mm_level.ball_choice_mode == ANIM_RANDOM)
+       gfx.anim_random_frame = RND(native_mm_level.num_ball_contents);
+
+      element_pos = getAnimationFrame(native_mm_level.num_ball_contents, 1,
+                                     native_mm_level.ball_choice_mode, 0,
+                                     game_mm.ball_choice_pos);
+
+      if (native_mm_level.ball_choice_mode == ANIM_RANDOM)
+       gfx.anim_random_frame = last_anim_random_frame;
+
+      game_mm.ball_choice_pos++;
+
+      int new_element = native_mm_level.ball_content[element_pos];
+
+      Store[ELX][ELY] = get_rotated_element(new_element, RND(16));
+      Store2[ELX][ELY] = TRUE;
+    }
+
     Tile[ELX][ELY] = EL_GRAY_BALL_OPENING;
 
     // !!! CHECK AGAIN: Laser on Polarizer !!!
     ScanLaser();
 
+    laser.dest_element_last = Tile[ELX][ELY];
+    laser.dest_element_last_x = ELX;
+    laser.dest_element_last_y = ELY;
+
     return;
 
 #if 0
@@ -3425,7 +3560,7 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
     DrawField_MM(ELX, ELY);
 
 #if 0
-    printf("NEW ELEMENT: (%d, %d)\n", ELX, ELY);
+    Debug("game:mm:GameActions_MM_Ext", "NEW ELEMENT: (%d, %d)", ELX, ELY);
 #endif
 
     // above stuff: GRAY BALL -> PRISM !!!
@@ -3457,10 +3592,6 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 #endif
 
     ScanLaser();
-
-    /*
-    printf("TEST ELEMENT: %d\n", Tile[0][0]);
-    */
 #endif
 
     return;
@@ -3693,24 +3824,27 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
 
   if (element == EL_FUEL_FULL && CT > 10)
   {
-    for (i = game_mm.energy_left; i <= MAX_LASER_ENERGY; i+=2)
+    int num_init_game_frames = INIT_GAME_ACTIONS_DELAY;
+    int start = num_init_game_frames * game_mm.energy_left / native_mm_level.time;
+
+    for (i = start; i <= num_init_game_frames; i++)
     {
-#if 0
-      BlitBitmap(pix[PIX_DOOR], drawto,
-                DOOR_GFX_PAGEX4 + XX_ENERGY,
-                DOOR_GFX_PAGEY1 + YY_ENERGY + ENERGY_YSIZE - i,
-                ENERGY_XSIZE, i, DX_ENERGY,
-                DY_ENERGY + ENERGY_YSIZE - i);
-#endif
+      if (i == num_init_game_frames)
+       StopSound_MM(SND_MM_GAME_LEVELTIME_CHARGING);
+      else if (setup.sound_loops)
+       PlaySoundLoop_MM(SND_MM_GAME_LEVELTIME_CHARGING);
+      else
+       PlaySound_MM(SND_MM_GAME_LEVELTIME_CHARGING);
 
-      redraw_mask |= REDRAW_DOOR_1;
-      BackToFront();
+      game_mm.energy_left = native_mm_level.time * i / num_init_game_frames;
 
-      Delay_WithScreenUpdates(20);
+      UpdateAndDisplayGameControlValues();
+
+      BackToFront();
     }
 
-    game_mm.energy_left = MAX_LASER_ENERGY;
-    Tile[ELX][ELY] = EL_FUEL_EMPTY;
+    Tile[ELX][ELY] = laser.dest_element = EL_FUEL_EMPTY;
+
     DrawField_MM(ELX, ELY);
 
     DrawLaser(0, DL_LASER_ENABLED);
@@ -3721,12 +3855,12 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
   return;
 }
 
-void GameActions_MM(struct MouseActionInfo action, boolean warp_mode)
+void GameActions_MM(struct MouseActionInfo action)
 {
   boolean element_clicked = ClickElement(action.lx, action.ly, action.button);
   boolean button_released = (action.button == MB_RELEASED);
 
-  GameActions_MM_Ext(action, warp_mode);
+  GameActions_MM_Ext();
 
   CheckSingleStepMode_MM(element_clicked, button_released);
 }
@@ -3836,182 +3970,6 @@ void MovePacMen(void)
   }
 }
 
-void GameWon_MM(void)
-{
-  int hi_pos;
-  boolean raise_level = FALSE;
-
-#if 0
-  if (local_player->MovPos)
-    return;
-
-  local_player->LevelSolved = FALSE;
-#endif
-
-  if (game_mm.energy_left)
-  {
-    if (setup.sound_loops)
-      PlaySoundExt(SND_SIRR, SOUND_MAX_VOLUME, SOUND_MAX_RIGHT,
-                  SND_CTRL_PLAY_LOOP);
-
-    while (game_mm.energy_left > 0)
-    {
-      if (!setup.sound_loops)
-       PlaySoundStereo(SND_SIRR, SOUND_MAX_RIGHT);
-
-      /*
-      if (game_mm.energy_left > 0 && !(game_mm.energy_left % 10))
-       RaiseScore_MM(native_mm_level.score[SC_ZEITBONUS]);
-      */
-
-      RaiseScore_MM(5);
-
-      game_mm.energy_left--;
-      if (game_mm.energy_left >= 0)
-      {
-#if 0
-       BlitBitmap(pix[PIX_DOOR], drawto,
-                  DOOR_GFX_PAGEX5 + XX_ENERGY, DOOR_GFX_PAGEY1 + YY_ENERGY,
-                  ENERGY_XSIZE, ENERGY_YSIZE - game_mm.energy_left,
-                  DX_ENERGY, DY_ENERGY);
-#endif
-       redraw_mask |= REDRAW_DOOR_1;
-      }
-
-      BackToFront();
-      Delay_WithScreenUpdates(10);
-    }
-
-    if (setup.sound_loops)
-      StopSound(SND_SIRR);
-  }
-  else if (native_mm_level.time == 0)          // level without time limit
-  {
-    if (setup.sound_loops)
-      PlaySoundExt(SND_SIRR, SOUND_MAX_VOLUME, SOUND_MAX_RIGHT,
-                  SND_CTRL_PLAY_LOOP);
-
-    while (TimePlayed < 999)
-    {
-      if (!setup.sound_loops)
-       PlaySoundStereo(SND_SIRR, SOUND_MAX_RIGHT);
-      if (TimePlayed < 999 && !(TimePlayed % 10))
-       RaiseScore_MM(native_mm_level.score[SC_TIME_BONUS]);
-      if (TimePlayed < 900 && !(TimePlayed % 10))
-       TimePlayed += 10;
-      else
-       TimePlayed++;
-
-      /*
-      DrawText(DX_TIME, DY_TIME, int2str(TimePlayed, 3), FONT_TEXT_2);
-      */
-
-      BackToFront();
-      Delay_WithScreenUpdates(10);
-    }
-
-    if (setup.sound_loops)
-      StopSound(SND_SIRR);
-  }
-
-  CloseDoor(DOOR_CLOSE_1);
-
-  Request("Level solved!", REQ_CONFIRM);
-
-  if (level_nr == leveldir_current->handicap_level)
-  {
-    leveldir_current->handicap_level++;
-    SaveLevelSetup_SeriesInfo();
-  }
-
-  if (level_editor_test_game)
-    game_mm.score = -1;                // no highscore when playing from editor
-  else if (level_nr < leveldir_current->last_level)
-    raise_level = TRUE;                // advance to next level
-
-  if ((hi_pos = NewHiScore_MM()) >= 0)
-  {
-    game_status = HALLOFFAME;
-
-    // DrawHallOfFame(hi_pos);
-
-    if (raise_level)
-      level_nr++;
-  }
-  else
-  {
-    game_status = MAINMENU;
-
-    if (raise_level)
-      level_nr++;
-
-    // DrawMainMenu();
-  }
-
-  BackToFront();
-}
-
-int NewHiScore_MM(void)
-{
-  int k, l;
-  int position = -1;
-
-  // LoadScore(level_nr);
-
-  if (strcmp(setup.player_name, EMPTY_PLAYER_NAME) == 0 ||
-      game_mm.score < highscore[MAX_SCORE_ENTRIES - 1].Score)
-    return -1;
-
-  for (k = 0; k < MAX_SCORE_ENTRIES; k++)
-  {
-    if (game_mm.score > highscore[k].Score)
-    {
-      // player has made it to the hall of fame
-
-      if (k < MAX_SCORE_ENTRIES - 1)
-      {
-       int m = MAX_SCORE_ENTRIES - 1;
-
-#ifdef ONE_PER_NAME
-       for (l = k; l < MAX_SCORE_ENTRIES; l++)
-         if (!strcmp(setup.player_name, highscore[l].Name))
-           m = l;
-       if (m == k)     // player's new highscore overwrites his old one
-         goto put_into_list;
-#endif
-
-       for (l = m; l>k; l--)
-       {
-         strcpy(highscore[l].Name, highscore[l - 1].Name);
-         highscore[l].Score = highscore[l - 1].Score;
-       }
-      }
-
-#ifdef ONE_PER_NAME
-      put_into_list:
-#endif
-      strncpy(highscore[k].Name, setup.player_name, MAX_PLAYER_NAME_LEN);
-      highscore[k].Name[MAX_PLAYER_NAME_LEN] = '\0';
-      highscore[k].Score = game_mm.score;
-      position = k;
-
-      break;
-    }
-
-#ifdef ONE_PER_NAME
-    else if (!strncmp(setup.player_name, highscore[k].Name,
-                     MAX_PLAYER_NAME_LEN))
-      break;   // player already there with a higher score
-#endif
-
-  }
-
-  // if (position >= 0)
-  //   SaveScore(level_nr);
-
-  return position;
-}
-
 static void InitMovingField_MM(int x, int y, int direction)
 {
   int newx = x + (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
@@ -4186,7 +4144,7 @@ void RaiseScoreElement_MM(int element)
 // Mirror Magic game engine snapshot handling functions
 // ----------------------------------------------------------------------------
 
-void SaveEngineSnapshotValues_MM(ListNode **buffers)
+void SaveEngineSnapshotValues_MM(void)
 {
   int x, y;
 
@@ -4201,7 +4159,6 @@ void SaveEngineSnapshotValues_MM(ListNode **buffers)
       engine_snapshot_mm.Hit[x][y]   = Hit[x][y];
       engine_snapshot_mm.Box[x][y]   = Box[x][y];
       engine_snapshot_mm.Angle[x][y] = Angle[x][y];
-      engine_snapshot_mm.Frame[x][y] = Frame[x][y];
     }
   }
 
@@ -4244,7 +4201,6 @@ void LoadEngineSnapshotValues_MM(void)
       Hit[x][y]   = engine_snapshot_mm.Hit[x][y];
       Box[x][y]   = engine_snapshot_mm.Box[x][y];
       Angle[x][y] = engine_snapshot_mm.Angle[x][y];
-      Frame[x][y] = engine_snapshot_mm.Frame[x][y];
     }
   }