rnd-20060319-2-src
[rocksndiamonds.git] / src / game_em / synchro_2.c
index 3dff9e62edd2445be9f9df73bdbe96d0e2d2a662..1ab59546bc14d613ef83c50f0916f547bb32707a 100644 (file)
@@ -8,25 +8,48 @@
  * compilers suck.
  */
 
-#include "display.h"
-#include "tile.h"
-#include "level.h"
-#include "sample.h"
+#include "main_em.h"
 
 
 #define RANDOM (random = random << 31 | random >> 1)
 
+static void set_nearest_player_xy(int x, int y, int *dx, int *dy)
+{
+  int distance, distance_shortest = EM_MAX_CAVE_WIDTH + EM_MAX_CAVE_HEIGHT;
+  int i;
+
+  /* default values if no players are alive anymore */
+  *dx = 0;
+  *dy = 0;
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+  {
+    if (!ply[i].alive)
+      continue;
+
+    distance = ABS(ply[i].x - x) + ABS(ply[i].y - y);
+
+    if (distance < distance_shortest)
+    {
+      *dx = ply[i].x;
+      *dy = ply[i].y;
+
+      distance_shortest = distance;
+    }
+  }
+}
+
 void synchro_2(void)
 {
-  register unsigned int x = 0;
-  register unsigned int y = 1;
-  register unsigned long random = RandomEM;
-  register unsigned short *cave_cache = Cave[y]; /* might be a win */
-  unsigned long score = 0;
-
-  unsigned int temp = 0;       /* initialized to make compilers happy */
-  unsigned int dx; /* only needed to find closest player */
-  unsigned int dy;
+  int x = 0;
+  int y = 1;
+  unsigned long random = RandomEM;
+  short *cave_cache = Cave[y]; /* might be a win */
+  int score = 0;
+
+  int temp = 0;                        /* initialized to make compilers happy */
+  int dx;                      /* only needed to find closest player */
+  int dy;
   int element;
 
  loop:
@@ -1879,6 +1902,12 @@ void synchro_2(void)
            Cave[y+1][x+1] == Zplayer)
          goto android_still;
 
+#if 1
+
+       set_nearest_player_xy(x, y, &dx, &dy);
+
+#else
+
        if (ply1.alive && ply2.alive)
        {
          if ((ply1.x > x ? ply1.x - x : x - ply1.x) +
@@ -1911,6 +1940,8 @@ void synchro_2(void)
          dy = 0;
        }
 
+#endif
+
        Next[y][x] = Xblank;    /* assume we will move */
        temp = ((x < dx) + 1 - (x > dx)) + ((y < dy) + 1 - (y > dy)) * 3;
 
@@ -3205,6 +3236,21 @@ void synchro_2(void)
     /* --------------------------------------------------------------------- */
 
     case Xalien:
+
+#if 1
+
+      if (lev.wheel_cnt)
+      {
+       dx = lev.wheel_x;
+       dy = lev.wheel_y;
+      }
+      else
+      {
+       set_nearest_player_xy(x, y, &dx, &dy);
+      }
+
+#else
+
       if (lev.wheel_cnt)
       {
        dx = lev.wheel_x;
@@ -3242,6 +3288,8 @@ void synchro_2(void)
        dy = 0;
       }
 
+#endif
+
       if (RANDOM & 1)
       {
        if (y > dy)
@@ -4517,7 +4565,11 @@ void synchro_2(void)
        }
       }
 
+#if 1
+      lev.ball_pos = (lev.ball_pos + 1) % lev.num_ball_arrays;
+#else
       lev.ball_pos = (lev.ball_pos + 1) & 7;
+#endif
       goto loop;
 
     /* --------------------------------------------------------------------- */
@@ -4884,8 +4936,8 @@ void synchro_2(void)
 
  done:
 
-  if (ply1.alive || ply2.alive)
-    lev.score += score; /* only get a score if someone is alive */
+  if (ply[0].alive || ply[1].alive || ply[2].alive || ply[3].alive)
+    lev.score += score;                /* only add a score if someone is alive */
 
   RandomEM = random;