Beispiel #1
0
        /// <summary>
        /// Gets a path from the given position to the target position. Uses A*
        /// </summary>
        /// <param name="pos">The start tile</param>
        /// <param name="target">The target tile</param>
        /// <returns>A list of vectors to the target</returns>
        public static Path GetPathTo(BaseTile pos, BaseTile target)
        {
            Path p = new Path();

            if (pos.tileVertex == null)
            {
                return(p);
            }

            TileSystem.Prepare(ResourceGatherer.instance.gameWorld.tiles.tiles);

            PriorityQueue <Vertex> q = new PriorityQueue <Vertex>();

            pos.tileVertex.dist    = 0;
            pos.tileVertex.Scratch = true;

            q.Insert(pos.tileVertex, Heuristics(pos.position, target.position));

            Vertex current;

            while (!q.isEmpty)
            {
                current = q.GetHighestPriority();

                if (current == null)
                {
                    continue;
                }

                if (current.parentTile.position == target.position)
                {
                    Vertex icurrent = current;
                    while (icurrent.prev != null)
                    {
                        p.AddWaypointFront(icurrent);
                        icurrent = icurrent.prev;
                    }
                    return(p);
                }

                foreach (Edge e in current.adj)
                {
                    if (e.destination.dist >= current.dist + e.cost)
                    {
                        e.destination.prev = current;
                        e.destination.dist = current.dist + e.cost;
                        if (!e.destination.Scratch)
                        {
                            e.destination.Scratch = true;
                            q.Insert(e.destination, (int)e.destination.dist + Heuristics(e.destination.parentTile.position, target.position));
                        }
                    }
                }
            }

            return(p);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a path from the given position to the nearest target material. Uses Dijkstra
        /// </summary>
        /// <param name="pos">The start tile</param>
        /// <param name="mat">The material looked for</param>
        /// <returns>A path with the list of vectors</returns>
        public static Path GetPathTo(BaseTile pos, Material mat)
        {
            Path p = new Path();

            if (pos.tileVertex == null)
            {
                return(p);
            }

            TileSystem.Prepare(GameWorld.instance.tiles.tiles);

            PriorityQueue <Vertex> q = new PriorityQueue <Vertex>();

            pos.tileVertex.dist    = 0;
            pos.tileVertex.Scratch = true;

            q.Insert(pos.tileVertex, pos.tileVertex.dist);

            Vertex current;

            while (!q.isEmpty)
            {
                current = q.GetHighestPriority();

                if (current == null)
                {
                    continue;
                }

                foreach (MaterialEntity m in current.parentTile.entityList)
                {
                    if (m.material.Equals(mat))
                    {
                        Vertex icurrent = current.prev;
                        p.AddWaypointFront(current);
                        if (icurrent != null)
                        {
                            while (icurrent.prev != null)
                            {
                                p.AddWaypointFront(icurrent);
                                icurrent = icurrent.prev;
                            }
                        }
                        return(p);
                    }
                }

                foreach (Edge e in current.adj)
                {
                    if (e.destination.dist >= current.dist + e.cost)
                    {
                        e.destination.prev = current;
                        e.destination.dist = current.dist + e.cost;
                        if (!e.destination.Scratch)
                        {
                            e.destination.Scratch = true;
                            q.Insert(e.destination, e.destination.dist);
                        }
                    }
                }
            }

            return(p);
        }