Esempio n. 1
0
 public Path FindPath(Vector3 source, INode target, INodeDistCalculator nodeDistCalculator)
 {
     try {
         return(actualAlg.FindPath(source, target, nodeDistCalculator));
     }
     catch (Exception e) {
         Urho.IO.Log.Write(LogLevel.Error, $"There was un unexpected exception in {nameof(actualAlg.FindPath)}: {e.Message}");
         return(null);
     }
 }
Esempio n. 2
0
        public List <ITile> GetTileList(Vector3 source,
                                        INode target,
                                        INodeDistCalculator nodeDistCalculator)
        {
            Node targetNode = FindPathInNodes(source,
                                              target,
                                              nodeDistCalculator);
            //If path was not found, return null
            List <ITile> finalList = targetNode == null ? null : MakeTileList(targetNode);

            Reset();
            return(finalList);
        }
Esempio n. 3
0
 bool TryGetNodeDistCalculator(out INodeDistCalculator nodeDistCalculator)
 {
     try {
         nodeDistCalculator = user.GetNodeDistCalculator();
         return(nodeDistCalculator != null);
     }
     catch (Exception e) {
         Urho.IO.Log.Write(LogLevel.Error,
                           $"There was an unexpected exception in {nameof(user.GetNodeDistCalculator)}: {e.Message}");
         nodeDistCalculator = null;
         return(false);
     }
 }
Esempio n. 4
0
        Node FindPathInNodes(Vector3 source,
                             INode target,
                             INodeDistCalculator nodeDistCalculator)
        {
            Node startNode = GetTileNode(source).GetClosestNode(source);

            distCalc = nodeDistCalculator as NodeDistCalculator;
            if (distCalc == null)
            {
                throw new ArgumentException("Cannot calculate node distances with calculator for a different algorithm", nameof(nodeDistCalculator));
            }

            targetNode = target as Node;
            if (targetNode == null)
            {
                throw new ArgumentException("Target node was not created by this pathfinding algorithm", nameof(target));
            }

            // Enque the starting node
            priorityQueue.Enqueue(startNode, 0);
            // Add the starting node to touched nodes, so it does not get enqued again
            touchedNodes.Add(startNode);
            double minDistToTarget = Vector3.Distance(startNode.Position, targetNode.Position);

            //Main loop
            while (priorityQueue.Count != 0)
            {
                Node currentNode = priorityQueue.Dequeue();

                //If we hit the target, finish and return the sourceNode
                if (currentNode == targetNode)
                {
                    if (MHUrhoApp.Instance.Config.PathFindingVisualization == Visualization.TouchedNodes)
                    {
                        VisualizeTouchedNodes(targetNode.Time);
                    }

                    return(currentNode);
                }

                //If not finished, add untouched neighbours to the queue and touched nodes
                currentNode.ProcessNeighbours(currentNode, priorityQueue, touchedNodes, targetNode, distCalc, ref minDistToTarget);
            }

            if (MHUrhoApp.Instance.Config.PathFindingVisualization == Visualization.TouchedNodes)
            {
                VisualizeTouchedNodes(targetNode.Time);
            }
            //Did not find path
            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// Finds the fastest path through the map from units current possition to target
        /// </summary>
        /// <param name="target">Target coordinates</param>
        /// <returns>List of IntVector2s the unit should pass through</returns>
        public Path FindPath(Vector3 source,
                             INode target,
                             INodeDistCalculator nodeDistCalculator)
        {
            Node realTargetNode = FindPathInNodes(source,
                                                  target,
                                                  nodeDistCalculator);

            //If path was not found, return null
            Path finalPath = realTargetNode == null
                                                                ? null
                                                                : MakePath(source, realTargetNode);

            Reset();
            return(finalPath);
        }