Пример #1
0
        public static List<Vector2> PathToRoute(Path path)
        {
            List<Vector2> route = new List<Vector2>();
            route.Add(new Vector2(path.PathPoints[0].X, path.PathPoints[0].Y));
            if (path.PathPoints.Count == 1)
            {
                return route;
            }
            if (path.PathPoints.Count == 2)
            {
                route.Add(new Vector2(path.PathPoints[1].X, path.PathPoints[1].Y));
                return route;
            }

            Point currentStart = path.PathPoints[0];
            Point currentEnd = path.PathPoints[1];
            int currentStartIndex = 0;
            int currentEndIndex = 1;

            while (true)
            {

                PathDirection pd = PathDirection.Streight;

                if (currentEnd.X < currentStart.X && currentEnd.Y < currentStart.Y)
                    pd = PathDirection.UpLeft;
                if (currentEnd.X < currentStart.X && currentEnd.Y > currentStart.Y)
                    pd = PathDirection.DownLeft;
                if (currentEnd.X > currentStart.X && currentEnd.Y < currentStart.Y)
                    pd = PathDirection.UpRight;
                if (currentEnd.X > currentStart.X && currentEnd.Y > currentStart.Y)
                    pd = PathDirection.DownRight;

                bool hit = false;

                if (pd != PathDirection.Streight)
                {

                    Vector3 p1End = new Vector3(currentEnd.X , currentEnd.Y , 0);
                    Vector3 p1Start = new Vector3(currentStart.X , currentStart.Y , 0);
                    Ray r1 = new Ray(p1End, p1End - p1Start);

                    Vector3 p2End = new Vector3(currentEnd.X + 1 , currentEnd.Y + 1 , 0);
                    Vector3 p2Start = new Vector3(currentStart.X + 1 , currentStart.Y + 1 , 0);
                    Ray r2 = new Ray(p2End, p2End - p2Start);

                    Vector3 p3End = new Vector3(currentEnd.X + 1,  currentEnd.Y , 0);
                    Vector3 p3Start = new Vector3(currentStart.X + 1 , currentStart.Y , 0);
                    Ray r3 = new Ray(p3End, p3End - p3Start);

                    Vector3 p4End = new Vector3(currentEnd.X , currentEnd.Y + 1 , 0);
                    Vector3 p4Start = new Vector3(currentStart.X , currentStart.Y + 1 , 0);
                    Ray r4 = new Ray(p4End, p4End - p4Start);

                    int x = -1;
                    int y = -1;
                    int xx = -1;
                    int yy = -1;

                    if (currentStart.X > currentEnd.X)
                    {
                        x = currentEnd.X;
                        xx = currentStart.X;
                    }
                    else
                    {
                        x = currentStart.X;
                        xx = currentEnd.X;
                    }
                    if (currentStart.Y > currentEnd.Y)
                    {
                        y = currentEnd.Y;
                        yy = currentStart.Y;
                    }
                    else
                    {
                        y = currentStart.Y;
                        yy = currentEnd.Y;
                    }

                    bool breakOut = false;
                    for (int i = x; i <= xx; i++)
                    {
                        if (breakOut)
                            break;
                        for (int j = y; j <= yy; j++)
                        {
                            if (WorldMap.Instance.GetMapElement(i, j, 5) != null && WorldMap.Instance.GetMapElement(i, j, 5).ElementEffect == MapElementEffect.Solid)
                            {
                                BoundingBox b = new BoundingBox(new Vector3(i, j, -0.1f), new Vector3(i + 1, j + 1, 0.1f));
                                if (r1.Intersects(b) != null || r2.Intersects(b) != null || r3.Intersects(b) != null || r4.Intersects(b) != null)
                                {
                                    hit = true;
                                    breakOut = true;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (hit == true)
                {
                    route.Add(new Vector2(path.PathPoints[currentEndIndex - 1].X, path.PathPoints[currentEndIndex - 1].Y));
                    currentStartIndex = currentEndIndex - 1;
                    currentEndIndex = currentStartIndex + 1;

                    currentStart = path.PathPoints[currentStartIndex];
                    currentEnd = path.PathPoints[currentEndIndex];
                }
                else
                {
                    currentEndIndex++;
                    currentEnd = path.PathPoints[currentEndIndex];
                }

                if (currentEndIndex == path.PathPoints.Count - 1)
                {
                    route.Add(new Vector2(path.PathPoints[currentEndIndex].X, path.PathPoints[currentEndIndex].Y));
                    return route;
                }
            }
            throw new Exception("");
        }
Пример #2
0
        public void SetMovePath(Path path)
        {
            _prevMovePath = path;
            MovePath.Clear();

            for (int i = path.PathPoints.Count - 1; i >= 0; i--)
            {
                MovePath.Push(path.PathPoints[i]);
            }
        }
Пример #3
0
 public bool IsShorterThan(Path path)
 {
     if (GoalSameAsStart == true)
         return true;
     if (path.GoalSameAsStart == true)
         return false;
     if (PathPoints.Count <= path.PathPoints.Count)
         return true;
     return false;
 }
Пример #4
0
        public bool start(int Sx, int Sy, int Gx, int Gy)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            bool openUpGoal = false;
            pathBuffer = new List<Point>();

            if ((Math.Abs(Sx - Gx) == 1 && Math.Abs(Sy - Gy) == 0) ||
                (Math.Abs(Sx - Gx) == 0 && Math.Abs(Sy - Gy) == 1) ||
                (Math.Abs(Sx - Gx) == 1 && Math.Abs(Sy - Gy) == 1) ||
                (Sx == Gx && Sy == Gy))
            {
                List<Point> p = new List<Point>();
                p.Add(new Point(Gx, Gy));
                Path = new Path(p);
                if (Sx == Gx && Sy == Gy)
                    Path.GoalSameAsStart = true;
                return true;
            }

            pathAvailable = false;

            goalX = Gx;
            goalY = Gy;

            startX = Sx;
            startY = Sy;

            openList.Clear();

            list.Clear();

            bool noPath = false;

            if ((Gx - 1 < 0 || _colMap[Gx - 1, Gy].tileType == TileInfo.TileType.Wall) &&
               (Gx + 1 > Width || _colMap[Gx + 1, Gy].tileType == TileInfo.TileType.Wall) &&
                (Gy - 1 < 0 || _colMap[Gx, Gy - 1].tileType == TileInfo.TileType.Wall) &&
                (Gy + 1 > Width || _colMap[Gx, Gy + 1].tileType == TileInfo.TileType.Wall))
                noPath = true;

            if (_colMap[Gx, Gy].tileType == TileInfo.TileType.Wall)
            {
                openUpGoal = true;
                _colMap[Gx, Gy].tileType = TileInfo.TileType.Floor;
                //throw new Exception("Destination is a wall");
            }

            if (!(Sx == Gx && Sy == Gy) && _colMap[Gx, Gy].tileType != TileInfo.TileType.Wall && !noPath)
            {
                list.Add(new Point(Sx, Sy), new listitem(Sx, Sy));
                list.Add(new Point(Gx, Gy), new listitem(Gx, Gy));
                openList.Add(new listitem(Sx, Sy));

                setOpenOnLowestF(Sx, Sy, Gx, Gy);

                if (pathAvailable)
                {
                    makePath(Gx, Gy, Sx, Sy);
                }
                else if (_colMap[Gx, Gy].unitOnTile != null && _colMap[Gx, Gy].unitOnTile.isPlayer
                    && Math.Abs(goalX - startX) < 2 && Math.Abs(goalY - startY) <  2)
                {
                    // If the player is on the goal tile then return true so we can attack him
                    pathBuffer.Add(new Point(Gx, Gy));
                    return true;
                }

            }

            if (openUpGoal)
                _colMap[Gx, Gy].tileType = TileInfo.TileType.Wall;

            // TODO: Bug, det er lidt et problem at det her nogen gange sker
            if (pathBuffer.Count == 0)
            {

            }

            Path = new Path(pathBuffer);

            if (s.Elapsed.TotalMilliseconds >= 100)
                DwarfConsole.WriteLine("Path calculation time: " + s.Elapsed.TotalMilliseconds, ConsoleColor.Red);

            //Console.WriteLine(Path.ToString);
            return pathAvailable;
        }