public List <ThreadedPathfindNode> getPath() { List <ThreadedPathfindNode> foundPath = new List <ThreadedPathfindNode> (); List <ThreadedPathfindNode> openSet = new List <ThreadedPathfindNode> (); HashSet <ThreadedPathfindNode> closedSet = new HashSet <ThreadedPathfindNode> (); openSet.Add(nodes [currentJob.sX, currentJob.sY]); while (openSet.Count > 0) { ThreadedPathfindNode currentNode = openSet [0]; for (int i = 0; i < openSet.Count; i++) { if (openSet [i].fCost < currentNode.fCost || (openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)) { if (currentNode.Equals(openSet [i]) == false) { currentNode = openSet [i]; } } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode.Equals(nodes [currentJob.fX, currentJob.fY])) { foundPath = retracePath(nodes [currentJob.sX, currentJob.sY], currentNode); break; } foreach (ThreadedPathfindNode neighbour in currentNode.myNeighbours) { if (neighbour.walkable == false && neighbour != nodes [currentJob.fX, currentJob.fY] || closedSet.Contains(neighbour) || neighbour == null) { continue; } //took out modifier int newMoveCost = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.getModifier(); if (newMoveCost < neighbour.gCost || openSet.Contains(neighbour) == false) { neighbour.gCost = newMoveCost; neighbour.hCost = GetDistance(neighbour, nodes [currentJob.fX, currentJob.fY]); neighbour.parent = currentNode; if (openSet.Contains(neighbour) == false) { openSet.Add(neighbour); } } } } return(foundPath); }
int GetDistance(ThreadedPathfindNode nodeA, ThreadedPathfindNode nodeB) { int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX); int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY); if (dstX > dstY) { return(14 * dstY + 10 * (dstX - dstY)); } return(14 * dstX + 10 * (dstY - dstX)); }
private List <ThreadedPathfindNode> retracePath(ThreadedPathfindNode start, ThreadedPathfindNode end) { List <ThreadedPathfindNode> foundPath = new List <ThreadedPathfindNode> (); ThreadedPathfindNode currentNode = end; while (currentNode != start) { currentNode.gCost = 0; foundPath.Add(currentNode); currentNode = currentNode.parent; } foundPath.Reverse(); return(foundPath); }
/// <summary> /// Creates the threaded pathfinding nodes based on the non threaded nodes taken from the tilemaps /// </summary> void initialiseNodes() { WorldBuilder wb = FindObjectOfType <WorldBuilder> (); nodes = new ThreadedPathfindNode[WorldBuilder.me.worldTiles.GetLength(0), WorldBuilder.me.worldTiles.GetLength(1)]; for (int x = 0; x < nodes.GetLength(0); x++) { for (int y = 0; y < nodes.GetLength(1); y++) { // ////////Debug.Log(nodes.GetLength(0)); if (CreateNodesFromTilemaps.me.nodes [x, y] == null) { } else { WorldTile wt = CreateNodesFromTilemaps.me.nodes [x, y].GetComponent <WorldTile> (); nodes [x, y] = new ThreadedPathfindNode(); nodes [x, y].gridX = x; nodes [x, y].gridY = y; nodes [x, y].walkable = wt.walkable; nodes [x, y].worldPos = wt.gameObject.transform.position; nodes [x, y].modifier = wt.modifier; } } } for (int x = 0; x < nodes.GetLength(0); x++) { for (int y = 0; y < nodes.GetLength(1); y++) { if (CreateNodesFromTilemaps.me.nodes [x, y] == null) { } else { WorldTile wt = CreateNodesFromTilemaps.me.nodes [x, y].GetComponent <WorldTile> (); nodes [x, y].myNeighbours = new List <ThreadedPathfindNode> (); foreach (WorldTile w in wt.myNeighbours) { nodes [x, y].myNeighbours.Add(nodes [w.gridX, w.gridY]); } } } } }