added tile selection cursor for playing MM levels with keyboard or joystick
[rocksndiamonds.git] / src / game_mm / mm_game.c
index bb579712588767f19b1536b968c009973a61e1b9..ec206faf8f0452278fffed98d8d9e6c14126992e 100644 (file)
@@ -9,6 +9,8 @@
 // mm_game.c
 // ============================================================================
 
+#include <math.h>
+
 #include "main_mm.h"
 
 #include "mm_main.h"
@@ -742,6 +744,9 @@ void InitGameActions_MM()
 
   if (game_mm.kettles_still_needed == 0)
     CheckExitMM();
+
+  SetTileCursorXY(laser.start_edge.x, laser.start_edge.y);
+  SetTileCursorActive(TRUE);
 }
 
 void AddLaserEdge(int lx, int ly)
@@ -1963,8 +1968,12 @@ boolean HitLaserDestination(int element, int hit_mask)
   AddDamagedField(ELX, ELY);
 
   if (game_mm.lights_still_needed == 0)
+  {
     game_mm.level_solved = TRUE;
 
+    SetTileCursorActive(FALSE);
+  }
+
   return TRUE;
 }
 
@@ -2472,6 +2481,8 @@ static void Explode_MM(int x, int y, int phase, int mode)
       game_mm.game_over = TRUE;
       game_mm.game_over_cause = GAME_OVER_BOMB;
 
+      SetTileCursorActive(FALSE);
+
       laser.overloaded = FALSE;
     }
     else if (IS_MCDUFFIN(Store[x][y]))
@@ -3145,12 +3156,16 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
       }
 
       StopSound_MM(SND_MM_GAME_HEALTH_CHARGING);
+#if 0
       FadeMusic();
+#endif
 
       DrawLaser(0, DL_LASER_DISABLED);
       game_mm.game_over = TRUE;
       game_mm.game_over_cause = GAME_OVER_NO_ENERGY;
 
+      SetTileCursorActive(FALSE);
+
 #if 0
       if (Request("Out of magic energy ! Play it again ?",
                  REQ_ASK | REQ_STAY_CLOSED))
@@ -3281,6 +3296,8 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode)
       game_mm.game_over = TRUE;
       game_mm.game_over_cause = GAME_OVER_OVERLOADED;
 
+      SetTileCursorActive(FALSE);
+
 #if 0
       if (Request("Magic spell hit Mc Duffin ! Play it again ?",
                  REQ_ASK | REQ_STAY_CLOSED))
@@ -4190,3 +4207,179 @@ void RaiseScoreElement_MM(int element)
       break;
   }
 }
+
+
+/* ------------------------------------------------------------------------- */
+/* Mirror Magic game engine snapshot handling functions                      */
+/* ------------------------------------------------------------------------- */
+
+void SaveEngineSnapshotValues_MM(ListNode **buffers)
+{
+  int x, y;
+
+  engine_snapshot_mm.game_mm = game_mm;
+  engine_snapshot_mm.laser = laser;
+
+  for (x = 0; x < MAX_PLAYFIELD_WIDTH; x++)
+  {
+    for (y = 0; y < MAX_PLAYFIELD_HEIGHT; y++)
+    {
+      engine_snapshot_mm.Ur[x][y]    = Ur[x][y];
+      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];
+    }
+  }
+
+  engine_snapshot_mm.LX = LX;
+  engine_snapshot_mm.LY = LY;
+  engine_snapshot_mm.XS = XS;
+  engine_snapshot_mm.YS = YS;
+  engine_snapshot_mm.ELX = ELX;
+  engine_snapshot_mm.ELY = ELY;
+  engine_snapshot_mm.CT = CT;
+  engine_snapshot_mm.Ct = Ct;
+
+  engine_snapshot_mm.last_LX = last_LX;
+  engine_snapshot_mm.last_LY = last_LY;
+  engine_snapshot_mm.last_hit_mask = last_hit_mask;
+  engine_snapshot_mm.hold_x = hold_x;
+  engine_snapshot_mm.hold_y = hold_y;
+  engine_snapshot_mm.pacman_nr = pacman_nr;
+
+  engine_snapshot_mm.rotate_delay = rotate_delay;
+  engine_snapshot_mm.pacman_delay = pacman_delay;
+  engine_snapshot_mm.energy_delay = energy_delay;
+  engine_snapshot_mm.overload_delay = overload_delay;
+}
+
+void LoadEngineSnapshotValues_MM()
+{
+  int x, y;
+
+  /* stored engine snapshot buffers already restored at this point */
+
+  game_mm = engine_snapshot_mm.game_mm;
+  laser   = engine_snapshot_mm.laser;
+
+  for (x = 0; x < MAX_PLAYFIELD_WIDTH; x++)
+  {
+    for (y = 0; y < MAX_PLAYFIELD_HEIGHT; y++)
+    {
+      Ur[x][y]    = engine_snapshot_mm.Ur[x][y];
+      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];
+    }
+  }
+
+  LX  = engine_snapshot_mm.LX;
+  LY  = engine_snapshot_mm.LY;
+  XS  = engine_snapshot_mm.XS;
+  YS  = engine_snapshot_mm.YS;
+  ELX = engine_snapshot_mm.ELX;
+  ELY = engine_snapshot_mm.ELY;
+  CT  = engine_snapshot_mm.CT;
+  Ct  = engine_snapshot_mm.Ct;
+
+  last_LX       = engine_snapshot_mm.last_LX;
+  last_LY       = engine_snapshot_mm.last_LY;
+  last_hit_mask = engine_snapshot_mm.last_hit_mask;
+  hold_x        = engine_snapshot_mm.hold_x;
+  hold_y        = engine_snapshot_mm.hold_y;
+  pacman_nr     = engine_snapshot_mm.pacman_nr;
+
+  rotate_delay   = engine_snapshot_mm.rotate_delay;
+  pacman_delay   = engine_snapshot_mm.pacman_delay;
+  energy_delay   = engine_snapshot_mm.energy_delay;
+  overload_delay = engine_snapshot_mm.overload_delay;
+
+  RedrawPlayfield_MM(TRUE);
+}
+
+static int getAngleFromTouchDelta(int dx, int dy,  int base)
+{
+  double pi = 3.141592653;
+  double rad = atan2((double)-dy, (double)dx);
+  double rad2 = (rad < 0 ? rad + 2 * pi : rad);
+  double deg = rad2 * 180.0 / pi;
+
+  return (int)(deg * base / 360.0 + 0.5) % base;
+}
+
+int getButtonFromTouchPosition(int x, int y, int dst_mx, int dst_my)
+{
+  // calculate start (source) position to be at the middle of the tile
+  int src_mx = SX + x * TILESIZE_VAR + TILESIZE_VAR / 2;
+  int src_my = SY + y * TILESIZE_VAR + TILESIZE_VAR / 2;
+  int dx = dst_mx - src_mx;
+  int dy = dst_my - src_my;
+  int element;
+  int base = 16;
+  int phases = 16;
+  int angle_old = -1;
+  int angle_new = -1;
+  int button = 0;
+  int i;
+
+  if (!IN_LEV_FIELD(x, y))
+    return 0;
+
+  element = Feld[x][y];
+
+  if (!IS_MCDUFFIN(element) &&
+      !IS_MIRROR(element) &&
+      !IS_BEAMER(element) &&
+      !IS_POLAR(element) &&
+      !IS_POLAR_CROSS(element) &&
+      !IS_DF_MIRROR(element))
+    return 0;
+
+  angle_old = get_element_angle(element);
+
+  if (IS_MCDUFFIN(element))
+  {
+    angle_new = (dx > 0 && ABS(dy) < ABS(dx) ? ANG_RAY_RIGHT :
+                dy < 0 && ABS(dx) < ABS(dy) ? ANG_RAY_UP :
+                dx < 0 && ABS(dy) < ABS(dx) ? ANG_RAY_LEFT :
+                dy > 0 && ABS(dx) < ABS(dy) ? ANG_RAY_DOWN :
+                -1);
+  }
+  else if (IS_MIRROR(element) ||
+          IS_DF_MIRROR(element))
+  {
+    for (i = 0; i < laser.num_damages; i++)
+    {
+      if (laser.damage[i].x == x &&
+         laser.damage[i].y == y &&
+         ObjHit(x, y, HIT_POS_CENTER))
+      {
+       angle_old = get_mirrored_angle(laser.damage[i].angle, angle_old);
+       angle_new = getAngleFromTouchDelta(dx, dy, base) % phases;
+
+       break;
+      }
+    }
+  }
+
+  if (angle_new == -1)
+  {
+    if (IS_MIRROR(element) ||
+       IS_DF_MIRROR(element) ||
+       IS_POLAR(element))
+      base = 32;
+
+    if (IS_POLAR_CROSS(element))
+      phases = 4;
+
+    angle_new = getAngleFromTouchDelta(dx, dy, base) % phases;
+  }
+
+  button = (angle_new == angle_old ? 0 :
+           (angle_new - angle_old + phases) % phases < (phases / 2) ?
+           MB_LEFTBUTTON : MB_RIGHTBUTTON);
+
+  return button;
+}