// C Header File
// Created 10/12/2002; 9:57:49 PM
/*---------------------------------------------------------------------------*/
/* useful macros                                                             */
/*---------------------------------------------------------------------------*/
#define ABS(a)          (((a)<0) ? -(a): (a))
#define SPECIAL_SGN(a)  (((a)<0) ? 0 : 1)


unsigned long ReturnSpriteToSpriteDistance(short x1,short y1,short x2, short y2)
{
							register short px = x1;
  						register short py = y1;
							register short ex = x2;
							register short ey = y2;
							register short dx = px - ex; //Must be this order
							register short dy = py - ey; //Must be this order
							long distance_squared = (long)dx*dx + (long)dy*dy;
							unsigned long distance = FAT_FastSqrt(distance_squared);
							
			return distance;
}

//Returns 1 if a ray is blocked by a wall, otherwise returns 0
short
RayBlocked (FATCONFIG * fc, short x1, short y1, short x2, short y2)
{
  register short d, x, y, ax, ay, sx, sy, dx, dy;

//accuracy [0...6]
#define RAY_ACCURACY 0
#define RAY_ADJ (6-RAY_ACCURACY)

  x1 >>= RAY_ADJ;
  y1 >>= RAY_ADJ;
  x2 >>= RAY_ADJ;
  y2 >>= RAY_ADJ;

  dx = x2 - x1;
  ax = ABS (dx) << 1;
  sx = SPECIAL_SGN (dx);

  dy = y2 - y1;
  ay = ABS (dy) << 1;
  sy = SPECIAL_SGN (dy);

  x = x1;
  y = y1;



  if (ax > ay)
    {				/* x dominant */
      d = ay - (ax >> 1);
      do
	{
	  if (fc->
	      map_data[(y >> RAY_ACCURACY) * fc->map_width +
		       (x >> RAY_ACCURACY)] > 0)
	    return 1;
	  if (x == x2)
	    return 0;
	  if (d >= 0)
	    {
	      if (sy)
		y += 1;
	      else
		y -= 1;
	      d -= ax;
	    }
	  if (sx)
	    {
	      x += 1;
	    }
	  else
	    {
	      x -= 1;
	    }
	  d += ay;
	}
      while (1);
    }
  else
    {				/* y dominant */
      d = ax - (ay >> 1);
      do
	{
	  if (fc->
	      map_data[(y >> RAY_ACCURACY) * fc->map_width +
		       (x >> RAY_ACCURACY)] > 0)
	    return 1;
	  if (y == y2)
	    return 0;
	  if (d >= 0)
	    {
	      if (sx)
		x += 1;
	      else
		x -= 1;
	      d -= ay;
	    }
	  if (sy)
	    {
	      y += 1;
	    }
	  else
	    {
	      y -= 1;
	    }
	  d += ax;
	}
      while (1);
    }

  return 0;
}
