public static Dictionary <IPathfindingNode, double> GetMovementCostArray(IPathfindingNode startNode, PathfindingType pathfindingType, int maxCost) { IPathfindingNode[] neighbourNodes = null; IPathfindingNode neighbourNode; cameFromArray.Clear(); costSoFarArray.Clear(); frontier.Clear(); currentNode = null; frontier.Enqueue(startNode, 0); cameFromArray[startNode] = null; costSoFarArray[startNode] = 0.0; while (frontier.NumItems > 0) { currentNode = frontier.Dequeue(); //float startTime = Time.realtimeSinceStartup; neighbourNodes = currentNode.GetConnectedNodes().ToArray(); for (int i = 0; i < neighbourNodes.Length; i++) { neighbourNode = neighbourNodes[i]; if (neighbourNode != null && neighbourNode != startNode && (neighbourNode.IsEmpty || pathfindingType == PathfindingType.Air)) { //stepCost = GetTransportTimeToNextTileInDirection(currentPathfindingNode, i, isDirect); stepCost = 1; costSoFarArray.TryGetValue(neighbourNode, out currentLowestCost); newCost = currentLowestCost + stepCost; if ((currentLowestCost <= 0 || newCost < currentLowestCost) && newCost <= maxCost) { costSoFarArray[neighbourNode] = newCost; //if (pathfindingType == PathfindingType.Tile) //{ //priority = 1.0 / (newCost + heuristic(endPathfindingNode, neighbourPathfindingNode)); //} else //{ priority = 1.0 / newCost; //} frontier.Enqueue(neighbourNode, priority); cameFromArray[neighbourNode] = currentNode; } } } } return(costSoFarArray); }
public static Path GetPathOfTypeForUnit(IPathfindingNode startNode, IPathfindingNode endNode, PathfindingType pathfindingType, NodeUnit unit) { IPathfindingNode[] neighbourNodes = null; IPathfindingNode neighbourNode; cameFromArray.Clear(); costSoFarArray.Clear(); frontier.Clear(); currentNode = null; frontier.Enqueue(startNode, 0); cameFromArray[startNode] = null; costSoFarArray[startNode] = 0.0; while (frontier.NumItems > 0) { currentNode = frontier.Dequeue(); //float startTime = Time.realtimeSinceStartup; if (currentNode == endNode) { //float endTime = Time.realtimeSinceStartup; //float totalTime = endTime - startTime; path = new Path(startNode, currentNode, cameFromArray, costSoFarArray); return(path); } neighbourNodes = currentNode.GetConnectedNodes().ToArray(); for (int i = 0; i < neighbourNodes.Length; i++) { neighbourNode = neighbourNodes[i]; if (neighbourNode != null && neighbourNode != startNode && (!neighbourNode.BlocksUnit(unit) || pathfindingType == PathfindingType.Air)) { stepCost = currentNode.GetConnectionToNode(neighbourNode)._movementPointCost; bool hasFoundCost = costSoFarArray.TryGetValue(neighbourNode, out currentLowestCost); var costSoFar = costSoFarArray[currentNode]; newCost = costSoFar + stepCost; if (currentLowestCost <= 0 || newCost < currentLowestCost) { costSoFarArray[neighbourNode] = newCost; //if (pathfindingType == PathfindingType.Tile) //{ //priority = 1.0 / (newCost + heuristic(endPathfindingNode, neighbourPathfindingNode)); //} else //{ priority = 1.0 / newCost; //} frontier.Enqueue(neighbourNode, priority); cameFromArray[neighbourNode] = currentNode; } } } } return(null); }