Ejemplo n.º 1
0
        float GetChebyshevH(Node node)
        {
            Vector2 altPos = WorldHelp.CorrectWorldOverflow(FromOff + node.position);
            float   Hx     = Math.Abs(CorrectedGoal.X - altPos.X);
            float   Hy     = Math.Abs(CorrectedGoal.Y - altPos.Y);

            return((float)(Math.Max(Hx, Hy) * ((Hx == 0 || Hy == 0) ? 1 : Sqrt2)));
        }
Ejemplo n.º 2
0
        float GetEuclideanH(Node node)
        {
            Vector2 altPos = WorldHelp.CorrectWorldOverflow(FromOff + node.position);
            float   Hx     = CorrectedGoal.X - altPos.X;
            float   Hy     = CorrectedGoal.Y - altPos.Y;

            return((float)(Math.Sqrt(Math.Pow(Hx, 2) + Math.Pow(Hy, 2)) * ((Hx == 0 || Hy == 0) ? 1 : Sqrt2)));
        }
Ejemplo n.º 3
0
        float GetManhattanH(Node node)
        {
            Vector2 altPos = WorldHelp.CorrectWorldOverflow(FromOff + node.position);
            float   Hx     = Math.Abs(CorrectedGoal.X - altPos.X);
            float   Hy     = Math.Abs(CorrectedGoal.Y - altPos.Y);

            return((Hx + Hy) * 1);
        }
Ejemplo n.º 4
0
        public List <Vector2> GetPath(Vector2 Start, Vector2 Goal, Vector2 ElementSize)
        {
            Visited = new bool[WorldInformation.mapWidth, WorldInformation.mapHeight];

            InitVisitMap();
            _elementSize  = ElementSize / EngineSettings.TileSize;
            _goal         = Goal;
            FromOff       = new Vector2((WorldInformation.mapWidth / 2) - Start.X, (WorldInformation.mapHeight / 2) - Start.Y);
            CorrectedGoal = WorldHelp.CorrectWorldOverflow(_goal + FromOff);
            return(FindPath(Start));
        }
Ejemplo n.º 5
0
        public List <Vector2> getPath(Vector2 Start, Vector2 Goal, Vector2 ElementSize)
        {
            Visited = new bool[GSS.world.mapWidth, GSS.world.mapHeight];

            initVisitMap();
            _elementSize  = ElementSize / GSS.TileSize;
            _goal         = Goal;
            FromOff       = new Vector2((GSS.world.mapWidth / 2) - Start.X, (GSS.world.mapHeight / 2) - Start.Y);
            CorrectedGoal = WorldHelp.correctWorldOverflow(_goal + FromOff);
            return(FindPath(Start));
        }
Ejemplo n.º 6
0
        List <Vector2> FindPath(Vector2 From)
        {
            Node startNode = InitPF(From);

            while (OpenList.Count != 0)
            {
                Node current = FindLowestF(OpenList);
                if (current.position == _goal)
                {
                    //Return goal
                    PathFound = true;
                    PathNode  = current;
                    return(ExtractPath());
                }

                OpenList.Remove(current);
                ClosedList.Add(current.position);
                Visited[(int)current.position.X, (int)current.position.Y] = true;

                for (int X = -1; X <= 1; X++)
                {
                    for (int Y = -1; Y <= 1; Y++)
                    {
                        if (X != 0 || Y != 0)
                        {
                            Vector2 newPos          = new Vector2(current.position.X + X, current.position.Y + Y);
                            Vector2 correctedNewPos = WorldHelp.correctWorldOverflow(newPos);

                            if (Visited[(int)correctedNewPos.X, (int)correctedNewPos.Y])
                            {
                                continue;
                            }
                            else if (OpenList.Any(a => a.position == correctedNewPos))
                            {
                                continue;
                            }
                            else if (GSS.world.IsTileSailable((int)correctedNewPos.X, (int)correctedNewPos.Y, _elementSize))
                            {
                                Node newNode = new Node()
                                {
                                    parent   = current,
                                    position = correctedNewPos,
                                    G        = current.G + ((X == 0 || Y == 0) ? 1 : _sqrt2)
                                };
                                OpenList.Add(newNode);
                            }
                        }
                    }
                }
            }
            return(new List <Vector2>());
        }