public List<MapItem> getPath(MapItem[,] map, MapItem s, MapItem d)
        {
            if (s.Equals(this.source) && d.Equals(this.destination) && this.preList != null)
                return preList;

            this.source = s;
            this.destination = d;
            preList = new List<MapItem>();

            if(s.Name.Equals(d.Name))
                return preList;

            this.dijkstra(map, s, d);

            preList.Add(d);
            MapItem pre = d.Pre;

            while (true)
            {
                preList.Add(pre);
                if (pre.Name.Equals(s.Name))
                    break;
                else
                    pre = pre.Pre;
            }

            preList.Reverse();
            return preList;
        }
        public List<MapItem> getPathTo(MapItem[,] map, MapItem s, String whatToFind)
        {
            if (s.Equals(this.source)&&this.whatToFind==whatToFind&& this.preList != null)
                return preList;

            this.source = s;
            this.whatToFind = whatToFind;
            preList = new List<MapItem>();

            if (s.Contain==whatToFind)
                return preList;

            MapItem d = this.bfs(map, s, whatToFind);

            if (d == null)
                return null;
            Console.WriteLine("SSSSSSS");
            preList.Add(d);
            MapItem pre = d.Pre;

            while (true)
            {
                preList.Add(pre);
                if (pre.Name.Equals(s.Name))
                    break;
                else
                    pre = pre.Pre;
            }

            preList.Reverse();
            return preList;
        }
Exemple #3
0
        public static Stack <Vector2> GetAStarPath(List <List <LevelMap.MapItem> > graph, Vector2 start, Vector2 end)
        {
            // Clear old data
            _visited.Clear();
            _unvisited.Clear();

            _predecessorDict.Clear();
            _fDistanceDict.Clear();
            _gDistanceDict.Clear();

            Stack <Vector2> result = new Stack <Vector2>();

            MapItem mapNodeStart = graph[(int)start.y][(int)start.x];
            MapItem mapNodeEnd   = graph[(int)end.y][(int)end.x];

            // Add all nodes
            for (int y = 0; y < graph.Count; y++)
            {
                for (int x = 0; x < graph[y].Count; x++)
                {
                    MapItem node = graph[y][x];

                    if (node.Type == MapItemType.Path)
                    {
                        _fDistanceDict[node] = float.MaxValue;
                        _gDistanceDict[node] = float.MaxValue;
                        _unvisited.Add(node);
                    }
                }
            }

            // Set start to 0
            _fDistanceDict[mapNodeStart] = 0;
            _gDistanceDict[mapNodeStart] = 0;

            int debugCount = 0;

            // Search loop
            while (_unvisited.Count > 0)
            {
                if (debugCount > 5000)
                {
                    Debug.LogError("AStar error!");
                    return(result);
                }

                MapItem u = GetClosestFromUnvisited();

                // Break if reaching goal
                if (u.Equals(mapNodeEnd))
                {
                    break;
                }

                // Add to visited
                _visited.Add(u);

                foreach (MapItem v in GetNeighbors(u, graph))
                {
                    if (_visited.Contains(v))
                    {
                        continue;
                    }

                    if (_fDistanceDict[v] > _gDistanceDict[u] + GetEstimatedDistance(v, mapNodeEnd))
                    {
                        _fDistanceDict[v] = _gDistanceDict[u] + GetEstimatedDistance(v, mapNodeEnd);
                        _gDistanceDict[v] = _gDistanceDict[u];

                        _predecessorDict[v] = u;
                    }
                }

                debugCount++;
            }

            // Add result
            MapItem p     = _predecessorDict[mapNodeEnd];
            int     count = 200;

            while (!p.Equals(mapNodeStart))
            {
                p = _predecessorDict[p];
                result.Push(p.Position);

                if (count < 0)
                {
                    break;
                }

                count--;
            }

            return(result);
        }