Esempio n. 1
0
        public List <CPos> FindPath(PathSearch search)
        {
            using (new PerfSample("Pathfinder"))
            {
                using (search)
                {
                    List <CPos> path = null;

                    while (!search.Queue.Empty)
                    {
                        var p = search.Expand(world);
                        if (search.Heuristic(p) == 0)
                        {
                            path = MakePath(search.CellInfo, p);
                            break;
                        }
                    }

                    var dbg = world.WorldActor.TraitOrDefault <PathfinderDebugOverlay>();
                    if (dbg != null)
                    {
                        dbg.AddLayer(search.Considered.Select(p => new Pair <CPos, int>(p, search.CellInfo[p].MinCost)), search.MaxCost, search.Owner);
                    }

                    if (path != null)
                    {
                        return(path);
                    }
                }

                // no path exists
                return(emptyPath);
            }
        }
Esempio n. 2
0
        public List <CPos> FindBidiPath(                        /* searches from both ends toward each other */
            PathSearch fromSrc,
            PathSearch fromDest)
        {
            using (new PerfSample("Pathfinder"))
            {
                using (fromSrc)
                    using (fromDest)
                        while (!fromSrc.queue.Empty && !fromDest.queue.Empty)
                        {
                            /* make some progress on the first search */
                            var p = fromSrc.Expand(world);

                            if (fromDest.cellInfo[p.X, p.Y].Seen && fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity)
                            {
                                return(MakeBidiPath(fromSrc, fromDest, p));
                            }

                            /* make some progress on the second search */
                            var q = fromDest.Expand(world);

                            if (fromSrc.cellInfo[q.X, q.Y].Seen && fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity)
                            {
                                return(MakeBidiPath(fromSrc, fromDest, q));
                            }
                        }

                return(new List <CPos>(0));
            }
        }
Esempio n. 3
0
        public List <CPos> FindBidiPath(                        /* searches from both ends toward each other */
            PathSearch fromSrc,
            PathSearch fromDest)
        {
            using (new PerfSample("Pathfinder"))
            {
                using (fromSrc)
                    using (fromDest)
                    {
                        List <CPos> path = null;

                        while (!fromSrc.queue.Empty && !fromDest.queue.Empty)
                        {
                            /* make some progress on the first search */
                            var p = fromSrc.Expand(world);

                            if (fromDest.cellInfo[p.X, p.Y].Seen &&
                                fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity)
                            {
                                path = MakeBidiPath(fromSrc, fromDest, p);
                                break;
                            }

                            /* make some progress on the second search */
                            var q = fromDest.Expand(world);

                            if (fromSrc.cellInfo[q.X, q.Y].Seen &&
                                fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity)
                            {
                                path = MakeBidiPath(fromSrc, fromDest, q);
                                break;
                            }
                        }

                        var dbg = world.WorldActor.TraitOrDefault <DebugOverlay>();
                        if (dbg != null)
                        {
                            dbg.AddLayer(fromSrc.considered.Select(p => new Pair <CPos, int>(p, fromSrc.cellInfo[p.X, p.Y].MinCost)), fromSrc.maxCost, fromSrc.owner);
                            dbg.AddLayer(fromDest.considered.Select(p => new Pair <CPos, int>(p, fromDest.cellInfo[p.X, p.Y].MinCost)), fromDest.maxCost, fromDest.owner);
                        }

                        if (path != null)
                        {
                            return(path);
                        }
                    }

                return(emptyPath);
            }
        }
Esempio n. 4
0
        public List <CPos> FindPath(PathSearch search)
        {
            using (new PerfSample("Pathfinder"))
            {
                using (search)
                    while (!search.queue.Empty)
                    {
                        var p = search.Expand(world);
                        if (search.heuristic(p) == 0)
                        {
                            return(MakePath(search.cellInfo, p));
                        }
                    }

                // no path exists
                return(new List <CPos>(0));
            }
        }