Beispiel #1
0
        /*
         * /// <summary>
         * /// Performs a terrain passability check betwee two points by doing pixel validity checks at interval delta.
         * /// </summary>
         * public List<Creature> RayTracerPassabilityCheckRough(Creature client, Vector v1, Vector v2, double delta)
         * {
         *  Vector difference = v2 - v1;
         *  Vector deltaV = difference;
         *  deltaV.ScaleToLength(delta);
         *
         *  Vector currentPosition = v1;
         *
         *  for (int i = 0; i < difference.Length() / deltaV.Length(); ++i)
         *  {
         *      Coords pixel = new Coords(CoordsType.Pixel, currentPosition);
         *      List<Creature> collision = _myCollider.CreatureClippingCheck(client, pixel, false);
         *      if (collision == null || collision.Count > 0)
         *      {
         *          return collision;
         *      }
         *      currentPosition += deltaV;
         *  }
         *
         *  return new List<Creature>();
         * }
         */

        /*
         * /// <summary>
         * /// Returns the Bresenham line between p0 and p1; Borrowed the code
         * /// from some dude whose name I don't have, who in turn borrowed from Wikipedia.
         * /// </summary>
         * private List<Coords> BresenhamLine(Coords p0, Coords p1)
         * {
         *  List<Coords> returnList = new List<Coords>();
         *
         *  Boolean steep = Math.Abs(p1.Y - p0.Y) > Math.Abs(p1.X - p0.X);
         *
         *  if (steep == true)
         *  {
         *      Coords tmpPoint = new Coords(CoordsType.Tile, p0.X, p0.Y);
         *      p0 = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X);
         *
         *      tmpPoint = p1;
         *      p1 = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X);
         *  }
         *
         *  Int32 deltaX = Math.Abs(p1.X - p0.X);
         *  Int32 deltaY = Math.Abs(p1.Y - p0.Y);
         *  Int32 error = 0;
         *  Int32 deltaError = deltaY;
         *  Int32 yStep = 0;
         *  Int32 xStep = 0;
         *  Int32 y = p0.Y;
         *  Int32 x = p0.X;
         *
         *  if (p0.Y < p1.Y)
         *  {
         *      yStep = 1;
         *  }
         *  else
         *  {
         *      yStep = -1;
         *  }
         *
         *  if (p0.X < p1.X)
         *  {
         *      xStep = 1;
         *  }
         *  else
         *  {
         *      xStep = -1;
         *  }
         *
         *  Int32 tmpX = 0;
         *  Int32 tmpY = 0;
         *
         *  while (x != p1.X)
         *  {
         *
         *      x += xStep;
         *      error += deltaError;
         *
         *      //if the error exceeds the X delta then
         *      //move one along on the Y axis
         *      if ((2 * error) > deltaX)
         *      {
         *          y += yStep;
         *          error -= deltaX;
         *      }
         *
         *      //flip the coords if they're steep
         *      if (steep)
         *      {
         *          tmpX = y;
         *          tmpY = x;
         *      }
         *      else
         *      {
         *          tmpX = x;
         *          tmpY = y;
         *      }
         *
         *      //check the point generated is legal
         *      //and if it is add it to the list
         *      if (_myCollider.CheckInBounds(new Coords(CoordsType.Tile, tmpX, tmpY)) == true)
         *      {
         *          returnList.Add(new Coords(CoordsType.Tile, tmpX, tmpY));
         *      }
         *      else
         *      {   //a bad point has been found, so return the list thus far
         *          return returnList;
         *      }
         *
         *  }
         *
         *  return returnList;
         * }
         */

        /// <summary>
        /// Checks if the Bresenham line between p0 and p1 goes only through visible tiles
        /// !!! Code repetition, should redo.
        /// </summary>
        public bool BresenhamLineCheckVisible(Coords p0, Coords p1)
        {
            if (p0.Equals(p1))
            {
                return(true);
            }

            Boolean steep = Math.Abs(p1.Y - p0.Y) > Math.Abs(p1.X - p0.X);

            // fix this stupidity
            Coords p0original = new Coords(CoordsType.Tile, p0.X, p0.Y);
            Coords p1original = new Coords(CoordsType.Tile, p1.X, p1.Y);

            if (steep == true)
            {
                Coords tmpPoint = new Coords(CoordsType.Tile, p0.X, p0.Y);
                p0 = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X);

                tmpPoint = p1;
                p1       = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X);
            }

            Int32 deltaX     = Math.Abs(p1.X - p0.X);
            Int32 deltaY     = Math.Abs(p1.Y - p0.Y);
            Int32 error      = 0;
            Int32 deltaError = deltaY;
            Int32 yStep      = 0;
            Int32 xStep      = 0;
            Int32 y          = p0.Y;
            Int32 x          = p0.X;

            if (p0.Y < p1.Y)
            {
                yStep = 1;
            }
            else
            {
                yStep = -1;
            }

            if (p0.X < p1.X)
            {
                xStep = 1;
            }
            else
            {
                xStep = -1;
            }

            Int32 tmpX = 0;
            Int32 tmpY = 0;


            float visibilityTotal = 1f;

            while (x != p1.X)
            {
                x     += xStep;
                error += deltaError;

                //if the error exceeds the X delta then
                //move one along on the Y axis
                if ((2 * error) > deltaX)
                {
                    y     += yStep;
                    error -= deltaX;
                }

                //flip the coords if they're steep
                if (steep)
                {
                    tmpX = y;
                    tmpY = x;
                }
                else
                {
                    tmpX = x;
                    tmpY = y;
                }

                // check the point generated is legal
                // using passability check. creatures will leave shadows. should write a visibility
                // check later
                Coords currentCoords = new Coords(CoordsType.Tile, tmpX, tmpY);
                // for this to look good you must make sure it takes account of the eucledean distances over which the coeffcients hold
                // otherwise you get square FOVs.
                visibilityTotal *= this._visibilityMap[currentCoords.X, currentCoords.Y];

                if (
                    (visibilityTotal < Constants.VisibilityTreshold)
                    &
                    (!(currentCoords.Equals(p0original) | currentCoords.Equals(p1original)))
                    )
                {
                    return(false);
                }
            }

            return(true);
        }