示例#1
0
        public void ReloadPathfinding(int[,] map, Point start, Point end)
        {
            Map = new StarNode[map.GetLength(0), map.GetLength(1)];
            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    Map[x, y]      = new StarNode(new Point(x, y));
                    Map[x, y].type = map[x, y];
                }
            }

            Start = start;
            curr  = new StarNode(start);
            End   = end;

            ValidPath  = false;
            IsComplete = false;

            OpenSet          = new List <StarNode>();
            ClosedSet        = new List <StarNode>();
            CurNodeNeighbors = new List <StarNode>();
            CompleatedPath   = new List <StarNode>();
            //StepFinding();

            RunPathfinding();
        }
示例#2
0
        private void GetNeighborstonode(StarNode curr, List <StarNode> Nnodes)
        {
            // Retrieve 8 in a square
            for (int x = -1; x <= 1; x++)
            {
                for (int y = -1; y <= 1; y++)
                {
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }

                    if (curr.Location.X + x > -1 && curr.Location.X + x < Map.GetLength(0) &&
                        curr.Location.Y + y > -1 && curr.Location.Y + y < Map.GetLength(1))
                    {
                        if ((Map[curr.Location.X + x, curr.Location.Y + y].type != 1))
                        {
                            if (/*!OpenSet.Contains(Map[curr.Location.X + x, curr.Location.Y + y]) &&*/
                                !ClosedSet.Contains(Map[curr.Location.X + x, curr.Location.Y + y]))
                            {
                                if (Map[curr.Location.X + x, curr.Location.Y].type != 1 &&
                                    Map[curr.Location.X, curr.Location.Y + y].type != 1)
                                {
                                    Nnodes.Add(Map[curr.Location.X + x, curr.Location.Y + y]);
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        private void RunPathfinding()
        {
            curr.G = 0;
            curr.H = HcostEstimate(curr.Location, End);


            OpenSet.Add(curr);

            while (OpenSet.Count != 0)
            {
                curr = GetLowestInList(OpenSet);

                if (curr.Location.X == End.X && curr.Location.Y == End.Y)
                {
                    CompleatedPath = ConstructPath(curr);
                    LargestGScore  = MaxGscore(ClosedSet);
                    IsComplete     = true;
                    ValidPath      = true;
                    break;
                }

                OpenSet.Remove(curr);
                ClosedSet.Add(curr);

                GetNeighborstonode(curr, CurNodeNeighbors);

                for (int i = 0; i < CurNodeNeighbors.Count; i++)
                {
                    if (ClosedSet.Contains(CurNodeNeighbors[i]))
                    {
                        continue;
                    }

                    double tentative_g_score = curr.G + DistBetween(curr.Location, CurNodeNeighbors[i].Location);

                    if (!OpenSet.Contains(CurNodeNeighbors[i]))
                    {
                        OpenSet.Add(CurNodeNeighbors[i]);
                    }
                    else if (tentative_g_score >= CurNodeNeighbors[i].G)
                    {
                        continue;
                    }
                    CurNodeNeighbors[i].Previous = curr;
                    CurNodeNeighbors[i].H        = HcostEstimate(CurNodeNeighbors[i].Location, End);
                    CurNodeNeighbors[i].G        = tentative_g_score;
                }

                CurNodeNeighbors.Clear();
            }
            if (OpenSet.Count == 0)
            {
                IsComplete = true;
                ValidPath  = false;
            }
        }
示例#4
0
        private List <StarNode> ConstructPath(StarNode curr)
        {
            List <StarNode> final = new List <StarNode>();

            final.Add(curr);
            while (curr.Previous != null)
            {
                curr = curr.Previous;
                final.Add(curr);
            }
            return(final);
        }
示例#5
0
        private StarNode GetLowestInList(List <StarNode> allnodes)
        {
            double   currvalue = (int)Math.Pow(Map.GetLength(0) * Map.GetLength(1), 2);
            StarNode low       = allnodes[0];

            for (int i = 0; i < allnodes.Count; i++)
            {
                if (allnodes[i].F < currvalue)
                {
                    low       = allnodes[i];
                    currvalue = allnodes[i].F;
                }
            }
            return(low);
        }
示例#6
0
        public void StepFinding(Point start, Point end, int[,] map)
        {
            if (Isrun == false)
            {
                Map = new StarNode[map.GetLength(0), map.GetLength(1)];
                for (int x = 0; x < map.GetLength(0); x++)
                {
                    for (int y = 0; y < map.GetLength(1); y++)
                    {
                        Map[x, y]      = new StarNode(new Point(x, y));
                        Map[x, y].type = map[x, y];
                    }
                }

                Start = start;
                curr  = new StarNode(start);
                End   = end;

                OpenSet.Clear();
                ClosedSet.Clear();
                CurNodeNeighbors.Clear();
                CompleatedPath.Clear();
                curr.G = 0;
                curr.H = HcostEstimate(curr.Location, End);


                OpenSet.Add(curr);

                Isrun = true;
            }

            if (OpenSet.Count != 0)
            {
                curr = GetLowestInList(OpenSet);

                if (curr.Location.X == End.X && curr.Location.Y == End.Y)
                {
                    CompleatedPath = ConstructPath(curr);
                    //LargestGScore = MaxGscore(ClosedSet);
                    IsComplete = true;
                    ValidPath  = true;
                    Isrun      = false;
                    return;
                }

                OpenSet.Remove(curr);
                ClosedSet.Add(curr);

                GetNeighborstonode(curr, CurNodeNeighbors);

                for (int i = 0; i < CurNodeNeighbors.Count; i++)
                {
                    if (ClosedSet.Contains(CurNodeNeighbors[i]))
                    {
                        continue;
                    }

                    double tentative_g_score = curr.G + DistBetween(curr.Location, CurNodeNeighbors[i].Location);

                    if (!OpenSet.Contains(CurNodeNeighbors[i]))
                    {
                        OpenSet.Add(CurNodeNeighbors[i]);
                    }
                    else if (tentative_g_score >= CurNodeNeighbors[i].G)
                    {
                        continue;
                    }
                    CurNodeNeighbors[i].Previous = curr;
                    CurNodeNeighbors[i].H        = HcostEstimate(CurNodeNeighbors[i].Location, End);
                    CurNodeNeighbors[i].G        = tentative_g_score;
                }

                CurNodeNeighbors.Clear();
            }
            if (OpenSet.Count == 0)
            {
                IsComplete = true;
                ValidPath  = false;
            }
        }