Exemple #1
0
        /// <summary>
        /// Find route from init locations to destination defined by IAstarTarget
        /// </summary>
        public static AStarResult Find(IEnumerable<IntVector3> initLocs, IAStarTarget target,
			int maxNodeCount = 200000, CancellationToken? cancellationToken = null)
        {
            var astar = new AStar(initLocs, target);
            astar.MaxNodeCount = maxNodeCount;
            if (cancellationToken.HasValue)
                astar.CancellationToken = cancellationToken.Value;

            var status = astar.Find();

            return new AStarResult(status, astar.LastNode);
        }
Exemple #2
0
        public static IEnumerable<AStarResult> FindMany(IEnvironmentObject env,
			IntVector3 src, DirectionSet srcPositioning, IAStarTarget target,
			int maxNodeCount = 200000, CancellationToken? cancellationToken = null)
        {
            var initLocs = env.GetPositioningLocations(src, srcPositioning);

            var astar = new AStar(initLocs, target);
            astar.MaxNodeCount = maxNodeCount;
            if (cancellationToken.HasValue)
                astar.CancellationToken = cancellationToken.Value;

            AStarStatus status;
            while ((status = astar.Find()) == AStarStatus.Found)
                yield return new AStarResult(status, astar.LastNode);
        }
Exemple #3
0
        void DoAStar(IntVector3 src, IntVector3 dst)
        {
            long startBytes, stopBytes;
            Stopwatch sw = new Stopwatch();
            GC.Collect();
            startBytes = GC.GetTotalMemory(true);
            int gc0 = GC.CollectionCount(0);
            int gc1 = GC.CollectionCount(1);
            int gc2 = GC.CollectionCount(2);
            sw.Start();

            var initLocs = this.SrcPos.ToDirections().Select(d => src + d)
                .Where(p => m_map.Bounds.Contains(p) && !m_map.GetBlocked(p));

            var astar = new AStar(initLocs, new MyTarget(m_map, dst, this.DstPos));

            if (!this.Step)
            {
                var status = astar.Find();

                sw.Stop();
                gc0 = GC.CollectionCount(0) - gc0;
                gc1 = GC.CollectionCount(1) - gc1;
                gc2 = GC.CollectionCount(2) - gc2;
                stopBytes = GC.GetTotalMemory(true);

                this.MemUsed = stopBytes - startBytes;
                this.TicksUsed = sw.ElapsedTicks;
                this.GCCollections = String.Format("{0}/{1}/{2}", gc0, gc1, gc2);

                this.Status = status;
                m_nodes = astar.NodeMap;

                if (status != AStarStatus.Found)
                {
                    m_path = null;
                    this.PathLength = 0;
                    this.NodeCount = 0;
                    return;
                }

                m_path = new HashSet<IntVector3>(astar.GetPathLocationsReverse());
                var dirs = astar.GetPathReverse().ToArray();

                this.PathLength = dirs.Length;
                this.NodeCount = astar.NodeMap.Count;

                InvalidateTileData();

                Trace.TraceInformation("Ticks {0}, Mem {1}, Len {2}, NodeCount {3}, GC {4}", this.TicksUsed, this.MemUsed,
                    this.PathLength, this.NodeCount, this.GCCollections);
            }
            else
            {
                astar.DebugCallback = AStarDebugCallback;

                m_contEvent.Reset();

                AStarStatus status = AStarStatus.NotFound;

                Task.Factory.StartNew(() =>
                    {
                        status = astar.Find();
                    })
                    .ContinueWith((task) =>
                        {
                            sw.Stop();
                            stopBytes = GC.GetTotalMemory(true);

                            this.MemUsed = stopBytes - startBytes;
                            this.TicksUsed = sw.ElapsedTicks;

                            this.Status = status;
                            m_nodes = astar.NodeMap;

                            if (status != AStarStatus.Found)
                            {
                                m_path = null;
                                this.PathLength = 0;
                                this.NodeCount = 0;
                                return;
                            }

                            m_path = new HashSet<IntVector3>(astar.GetPathLocationsReverse());
                            var dirs = astar.GetPathReverse().ToArray();

                            this.PathLength = dirs.Length;
                            this.NodeCount = astar.NodeMap.Count;

                            InvalidateTileData();
                        }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }