Beispiel #1
0
        /// <summary>
        /// Finds a path on the current thread and returns.
        /// task.CompletedCallBack is ignored.
        /// </summary>
        /// <param name="state"></param>
        public void FindPathSync(AStarState <T> state)
        {
            try
            {
                state.SearchingPath = true;

                FindPathExec(state);
            }
            finally
            {
                state.SearchingPath = false;
            }
        }
Beispiel #2
0
        public async Task FindPath(AStarState <T> state)
        {
            try
            {
                state.SearchingPath = true;
                await Task.Run(() => FindPathExec(state)).ConfigureAwait(false);
            }
            finally
            {
                state.SearchingPath = false;
            }

            state.Complete = true;
        }
Beispiel #3
0
        void FindPathExec(AStarState <T> state)
        {
            var openNodes   = state.openNodes;
            var closedNodes = state.closedNodes;

            openNodes.Clear();
            closedNodes.Clear();
            state.Path.Clear();
            state.AbortOperation = false;

            var node = new AStarNode <T>
            {
                Location  = state.Start,
                Parent    = null,
                Heuristic = mMap.CalculateHeuristic(state.Start, state.EndPoints),
                PaidCost  = 0
            };

            state.openNodes.Add(node);

            bool found = false;
            int  steps = 0;

            do
            {
                SortOpenNodes(openNodes);

                //sMap.ReportProgress(openNodes, closedNodes, end);

                node = openNodes[0];
                openNodes.RemoveAt(0);
                closedNodes.Add(node);

                if (state.EndPoints.Contains(node.Location))
                {
                    found = true;
                    break;
                }

                steps++;
                if (steps > maxSteps)
                {
                    break;
                }

                foreach (T test in mMap.GetAvailableSteps(state, node.Location))
                {
                    if (state.AbortOperation)
                    {
                        return;
                    }

                    if (LocationIn(closedNodes, test))
                    {
                        continue;
                    }

                    int deltaCost = mMap.GetStepCost(test, node.Location);

                    int index = FindPointInOpenNodes(openNodes, test);

                    if (index >= 0)
                    {
                        if (node.PaidCost + deltaCost < openNodes[index].PaidCost)
                        {
                            var target = openNodes[index];

                            target.Parent   = node;
                            target.PaidCost = node.PaidCost + deltaCost;

                            openNodes[index] = target;
                        }
                    }
                    else
                    {
                        var newtarget = new AStarNode <T>
                        {
                            Location  = test,
                            Parent    = node,
                            PaidCost  = node.PaidCost + deltaCost,
                            Heuristic = mMap.CalculateHeuristic(test, state.EndPoints)
                        };

                        if (newtarget.Heuristic < 0)
                        {
                            continue;
                        }

                        openNodes.Add(newtarget);

                        if (newtarget.Heuristic == 0)
                        {
                            node  = newtarget;
                            found = true;
                        }
                    }

                    if (found)
                    {
                        break;
                    }
                }
            } while (openNodes.Count > 0 && found == false);

            if (!found)
            {
                state.FoundPath = false;
                return;
            }

            var path = new List <T>();

            path.Add(node.Location);

            while (node.Parent != null && node.Parent != node)
            {
                node = node.Parent;
                path.Add(node.Location);
            }

            state.Path.AddRange(path);
            state.FoundPath = true;
        }