IEnumerator FindPath(Vector3 startPos, Vector3 targetPos) { grid = GetComponent<Grid>(); Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); if (startNode.walkable && targetNode.walkable) { Heap<Node> openSet = new Heap<Node>(grid.MaxSize); HashSet<Node> closedSet = new HashSet<Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print ("Path found: " + sw.ElapsedMilliseconds + " ms"); pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) openSet.Add(neighbour); } } } } yield return null; if (pathSuccess) { waypoints = RetracePath(startNode,targetNode); } requestManager.FinishedProcessingPath(waypoints,pathSuccess); }
public Node searchNearestNode(Vector3 position, List<Node> goals,Grid grid) { List<Node> visitedNodes = new List<Node>(); List<Node> lastAddedNodes = new List<Node>(); List<Node> nowAddedNodes = new List<Node>(); Node center = grid.NodeFromWorldPoint(position); bool run = true; if(center!=null && goals != null) { if (goals.Count > 0) { visitedNodes.Add(center); lastAddedNodes.Add(center); int addedCount = 0; while (run) { foreach (Node n in lastAddedNodes) { List<Node> neighbours = grid.GetNeighbours(n); foreach(Node node in neighbours) { if(node.danger==0 && !node.visited && !visitedNodes.Contains(node)) { if (goals.Contains(node)) { return node; } visitedNodes.Add(node); nowAddedNodes.Add(node); addedCount++; } } } lastAddedNodes = nowAddedNodes; nowAddedNodes = new List<Node>(); if (addedCount == 0) { run = false; } addedCount = 0; } } } return null; }
IEnumerator FindPath(Vector3 starPosition, Vector3 targetPosition) { Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(starPosition); Node targetNode = grid.NodeFromWorldPoint(targetPosition); if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } pathRequestManager.FinishedProcessingPath(waypoints, pathSuccess); }
//funcao que recebe duas posicoes globais e calcula o A* entre eles private void FindPath(Vector2 startPos, Vector2 targetPos) { Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); //lista dos nos que ja foram checados mas ainda nao se sabe se o caminho ate eles eh o menor mesmo List <Node> openList = new List <Node>(); //lista dos nos que ja possuem o menor caminho ateh o noh inicial HashSet <Node> closedList = new HashSet <Node>(); //adiciona o noh inicial na openList openList.Add(startNode); //loop principal, roda enquano houver nos na openlist while (openList.Count > 0) { //seleciona o noh atual como o noh da openlist com o menor custo Node currentNode = openList[0]; for (int i = 1; i < openList.Count; i++) { if (openList[i].FCost < currentNode.FCost) { currentNode = openList[i]; } } //remove ele da openList e coloca na closedList openList.Remove(currentNode); closedList.Add(currentNode); //verifica se o noh atual eh o noh final if (currentNode == targetNode) { GetFinalPath(startNode, targetNode); } //loop checando os nos vizinhos do noh atual foreach (Node neighborNode in grid.GetNeighboringNodes(currentNode)) { //se o noh vizinho for um obstaculo ou ja esta na closedList entao nao faz nada if (neighborNode.isWall || closedList.Contains(neighborNode)) { continue; } //calcula o custo para se mover do noh atual para o vizinho int moveCost = currentNode.gCost + GetManhattenDistance(currentNode, neighborNode); //se o custo for menor que o custo atual do vizinho ou se o vizinho ainda nao possui um custo if (moveCost < neighborNode.gCost || !openList.Contains(neighborNode)) { //refaz os custos do noh vizinho e adicina o noh atual como pai do noh vizinho neighborNode.gCost = moveCost; neighborNode.hCost = GetManhattenDistance(neighborNode, targetNode); neighborNode.parent = currentNode; //se o noh vizinho nao estiver na openList, adiciona ele if (!openList.Contains(neighborNode)) { openList.Add(neighborNode); } } } } }
IEnumerator FindPath(Vector2 startPos, Vector2 targetPos) { Vector2[] waypoints = new Vector2[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); Debug.Log(startNode.walkable && targetNode.walkable); if (startNode.walkable && targetNode.walkable) { List <Node> openSet = new List <Node>(); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost) { currentNode = openSet[i]; } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { Debug.Log("path successful"); pathSuccess = true; break; } foreach (Node neighbor in grid.GetNeighbors(currentNode)) { if (!neighbor.walkable || closedSet.Contains(neighbor)) { continue; } int newMoveCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor); if (newMoveCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor)) { neighbor.gCost = newMoveCostToNeighbor; neighbor.hCost = GetDistance(neighbor, targetNode); neighbor.parent = currentNode; if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } requestController.FinishedProcessingPath(waypoints, pathSuccess); }
Vector2[] FindPath(Vector2 from, Vector2 to) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector2[] waypoints = new Vector2[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(from); Node targetNode = grid.NodeFromWorldPoint(to); startNode.parent = startNode; if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print("Path found: " + sw.ElapsedMilliseconds + " ms"); pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + TurningCost(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } return(waypoints); }
//Pathfinding function- finds paths and attempts to move between walkable nodes and to add them to the closed list public void FindPath(PathRequest request, Action <PathResult> callback) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(request.pathStart); Node targetNode = grid.NodeFromWorldPoint(request.pathEnd); startNode.parent = startNode; if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); pathSuccess = true; break; } //checking if neighbouring node are walkabe foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } //calculating distance and movement penalties int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.movementPenalty; if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } //if the path is successful retrace the path/callback if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); pathSuccess = waypoints.Length > 0; } callback(new PathResult(waypoints, pathSuccess, request.callback)); }
void Update() { //BFS (transform.position, mousePos); AStarSearch(grid.NodeFromWorldPoint(transform.position), grid.NodeFromWorldPoint(new Vector3(19, 0, 19))); //print (grid.path.Count); }
void FindPath(Vector3 startPos, Vector3 targetPos) { Stopwatch sw = new Stopwatch(); sw.Start(); Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); //List<Node> openSet = new List<Node>(); Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node> (); openSet.Add(startNode); while (openSet.Count > 0) { //Node currentNode = openSet[0]; Node currentNode = openSet.RemoveFirst(); // for(int i = 1; i < openSet.Count; i++) // { // if(openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost // && openSet[i].hCost < currentNode.hCost) // { // currentNode = openSet[i]; // } // } // // openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print("Path found " + sw.ElapsedMilliseconds + " ms"); Retracepath(startNode, targetNode); return; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDisTance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDisTance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } }
public void FindPath(PathRequest request, Action <PathResult> callback) { if (gv.playing) { Node startNode = grid.NodeFromWorldPoint(request.pathStart); Node targetNode = grid.NodeFromWorldPoint(request.pathEnd); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; startNode.parent = startNode; if (startNode.isWalkable && targetNode.isWalkable) { Heap <Node> openSet = new Heap <Node>(grid.BoxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { pathSuccess = true; break; } foreach (Node n in grid.GetNeighbors(currentNode)) { if (!n.isWalkable || closedSet.Contains(n)) { continue; } int newCost = currentNode.GCost + DistanceOfNodes(currentNode, n); if (newCost < n.GCost || !openSet.Contains(n)) { n.GCost = newCost; n.HCost = DistanceOfNodes(n, targetNode); n.parent = currentNode; if (!openSet.Contains(n)) { openSet.Add(n); } else { openSet.UpdateItem(n); } } } } } if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); pathSuccess = waypoints.Length > 0; } callback(new PathResult(waypoints, pathSuccess, request.callback)); } }
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos) { Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; // Get their actual position Nodes startNode = grid.NodeFromWorldPoint(startPos); Nodes targetNode = grid.NodeFromWorldPoint(targetPos); if (!startNode.walkable) { print("Will fail because the start node is on an unwalkable node"); float lowest = 90000000000000; Nodes newStart = startNode; foreach (Nodes neighbour in grid.getNeighbors(startNode, true)) { if (!neighbour.walkable) { continue; } float dist = Vector3.Distance(neighbour.worldPosition, targetPos); if (lowest > dist) { newStart = neighbour; lowest = dist; } } startNode = newStart; if (!startNode.walkable) { Debug.Log("We've failed boiz"); } } if (!targetNode.walkable) { print("Will fail because the target is on an unwalkable node"); float lowest = 90000000000000; Nodes newEnd = targetNode; foreach (Nodes neighbour in grid.getNeighbors(targetNode, true)) { if (!neighbour.walkable) { continue; } float dist = Vector3.Distance(neighbour.worldPosition, startPos); if (lowest > dist) { newEnd = neighbour; lowest = dist; } } targetNode = newEnd; if (!startNode.walkable) { Debug.Log("We've failed (end) boiz"); } } if (startNode.walkable && targetNode.walkable) { // Start the process // We'll be using lists Heap <Nodes> openSet = new Heap <Nodes>(grid.MaxSize); HashSet <Nodes> closedSet = new HashSet <Nodes>(); // Start at the start openSet.Add(startNode); while (openSet.Count > 0) { // Find the node in the open set with the lowest fCost Nodes currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); // Have we found the exit yet? if (currentNode == targetNode) { pathSuccess = true; break; } // Start comparing foreach (Nodes neighbour in grid.getNeighbors(currentNode, true)) { // if the neighbor isn't walkable or is already in the closed set then ignore it if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); // if the new movement cost is less then the cost to actually move towards the exit or if the open set dooesn't already have the neighbor if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { // Add the neighbor neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } requestManager.FinishedProcessingPath(waypoints, pathSuccess); }
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, bool noticeOccupied) { Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); bool abortPF = false; if (!targetNode.walkable) { Node[] neighbours = grid.GetNeighbours(targetNode); for (int i = 0; i < neighbours.Length; ++i) { if (neighbours[i] == null) { continue; } else if (neighbours[i].walkable) { abortPF = false; targetNode = neighbours[i]; break; } abortPF = true; } } Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); int nodeCount = -1; while (openSet.Count > 0) { ++nodeCount; if (nodeCount > maximumNodes) { break; } else if (abortPF) { break; } Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { pathSuccess = true; break; } Node[] neighbours = grid.GetNeighbours(currentNode); for (int i = 0; i < neighbours.Length; ++i) { if (neighbours[i] == null) { continue; } if (!neighbours[i].walkable || closedSet.Contains(neighbours[i])) { continue; } if (noticeOccupied && neighbours[i].occupied) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbours[i]); if (newMovementCostToNeighbour < neighbours[i].gCost || !openSet.Contains(neighbours[i])) { neighbours[i].gCost = newMovementCostToNeighbour; neighbours[i].hCost = GetDistance(neighbours[i], targetNode); neighbours[i].parent = currentNode; if (!openSet.Contains(neighbours[i])) { openSet.Add(neighbours[i]); } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode, noticeOccupied); requestManager.FinishedProcessingPath(waypoints, pathSuccess); } else { requestManager.FinishedProcessingPath(waypoints, pathSuccess); } }
IEnumerator FindPath(Vector3 startPosition, Vector3 targetPosition) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] wayPoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPosition); Node targetNode = grid.NodeFromWorldPoint(targetPosition); if (startNode.walkable && targetNode.walkable) { //A Heap/Hashset is used to help manage the algorithm and optimize performance Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print("Path Found: " + sw.ElapsedMilliseconds + " Millisecond(s)."); pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } } yield return(null); if (pathSuccess) { wayPoints = RetracePath(startNode, targetNode); } requestManager.FinishedProcessingPath(wayPoints, pathSuccess); }
IEnumerator FindPath(Vector3 StartPos, Vector3 TargetPos) { Stopwatch MyStopwatch = new Stopwatch(); MyStopwatch.Start(); Vector3[] Waypoints = new Vector3[0]; bool PathSuccess = false; Node StartNode = grid.NodeFromWorldPoint(StartPos); Node TargetNode = grid.NodeFromWorldPoint(TargetPos); if (StartNode.IsWalkable && TargetNode.IsWalkable) { Heap <Node> OpenSet = new Heap <Node>(grid.MaxSize); HashSet <Node> ClosedSet = new HashSet <Node>(); OpenSet.Add(StartNode); while (OpenSet.Count > 0) { Node CurrentNode = OpenSet.RemoveFirstItem(); ClosedSet.Add(CurrentNode); if (CurrentNode == TargetNode) { MyStopwatch.Stop(); print("Path Found " + MyStopwatch.ElapsedMilliseconds + " Milliseconds"); PathSuccess = true; break; } foreach (Node Neighbour in grid.GetNeighbours(CurrentNode)) { if (!Neighbour.IsWalkable || ClosedSet.Contains(Neighbour)) { continue; } int NewMovementCostToNeighbour = CurrentNode.GCost + GetDistance(CurrentNode, Neighbour) + Neighbour.MovementPenalty; if (NewMovementCostToNeighbour < Neighbour.GCost || !OpenSet.Contains(Neighbour)) { Neighbour.GCost = NewMovementCostToNeighbour; Neighbour.HCost = GetDistance(Neighbour, TargetNode); Neighbour.Parent = CurrentNode; if (!OpenSet.Contains(Neighbour)) { OpenSet.Add(Neighbour); OpenSet.UpdateItem(Neighbour); } else { OpenSet.UpdateItem(Neighbour); } } } } } yield return(null); if (PathSuccess) { Waypoints = RetracePath(StartNode, TargetNode); } RequestManager.FinishedProcessingPath(Waypoints, PathSuccess); }
//ASTAR algorithmus //ones it found a path it needs to call the finishprocessingpath of the pathrequest manager script IEnumerator FindPath(Vector3 startPos, Vector3 targetPos) { //To see performance gain Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; //check if we find a path bool pathSuccess = false; //Convert Worldpos in Nodes, already done in grid method Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); //Openset of nodes to be evaluated //List<Node> openSet = new List<Node>(); //if they are not walkable its impossible to find a path if (startNode.walkable && targetNode.walkable) { //Optimising with Heap Heap <Node> openSet = new Heap <Node>(grid.MaxSize); //Closedset of nodes to be already evaluated HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); //search for node with lowes f_cost is the most expensive part of algorithm while (openSet.Count > 0) { //find Node with the lowest f_cost Node currentNode = openSet.RemoveFirst(); //This Code is optimizid through heap //Node currentNode = openSet[0]; //first element in openset ////loop to all of the nodes in openset //for (int i = 1; i < openSet.Count; i++) //{ // //if the fCost ist equal with the other node, than we see which one is closest to end node by comapring hCost // if (openSet[i].FCost < currentNode.FCost || openSet[i].FCost == currentNode.FCost && openSet[i].hCost < currentNode.hCost) // { // //find node in the openset with the lowest fCost // currentNode = openSet[i]; // } //} ////now we found the node with the lowest fCost in the openset ////remove it from openSet and add to closeSet //openSet.Remove(currentNode); closedSet.Add(currentNode); //we found ouer path if (currentNode == targetNode) { sw.Stop(); print("Path found: " + sw.ElapsedMilliseconds + " ms" + " with a frameWait of " + drawSpeed); //found path pathSuccess = true; break; } //if not found the past, loop through each neighbouring node of current node foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (count >= drawSpeed) { yield return(new WaitForEndOfFrame()); count = 0; } count++; drawNodes.Add(neighbour); //check if the neighbour in the not walkable or in the close list, than skip if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } //Check if the new path to the neighbar is shorter than the old one, or if the neighbour is not in the node list int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { //set the fXost of the neighbour //we calculate the gCost and hCost neighbour.gCost = newMovementCostToNeighbour; //distance from the node to the end node neighbour.hCost = GetDistance(neighbour, targetNode); //set the parent of the neighbour to the current node neighbour.parent = currentNode; //check if the neighbour is not in the open set, if not add to open set if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } //wait for 1 frame befor returning yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } requestManager.FinishedProcessingPath(waypoints, pathSuccess); }
public void FindPath(PathRequest request, Action <PathResult> callback) { Vector2[] waypoints = new Vector2[0]; bool pathSucess = false; Node startNode = grid.NodeFromWorldPoint(request.pathStart); Node targetNode = grid.NodeFromWorldPoint(request.pathEnd); if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.GridSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node curNode = openSet.RemoveFirst(); closedSet.Add(curNode); if (curNode == targetNode) { pathSucess = true; break; } List <Node> neighbours = grid.GetNeighbours(curNode); foreach (Node neighbour in neighbours) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int moveCostToNeighbour = curNode.gCost + getDistance(curNode, neighbour); if (moveCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = moveCostToNeighbour; neighbour.hCost = getDistance(neighbour, targetNode); neighbour.parentNode = curNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } if (pathSucess) { waypoints = getPath(startNode, targetNode); pathSucess = waypoints.Length > 0; } callback(new PathResult(waypoints, pathSucess, request.callback)); }
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, List <Node> unwalkableNodes, List <Node> dynamicExceptions) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); if (targetNode.danger < 1) //if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); //print("Path found: " + sw.ElapsedMilliseconds + " ms"); pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (dynamicExceptions.Contains(neighbour) || unwalkableNodes.Contains(neighbour) || closedSet.Contains(neighbour)) //if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } requestManager.FinishedProcessingPath(waypoints, pathSuccess); }
private void FindPath(Vector3 startPos, Vector3 targetPos) { timer.Reset(); timer.Start(); Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); UnityEngine.Debug.Log("Posisi Awal NPC: " + musuhNPC.transform.position); UnityEngine.Debug.Log("Posisi Player: " + dicari.transform.position); List <Node> openSet = new List <Node>(); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost) { currentNode = openSet[i]; } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { RetracePath(startNode, targetNode); timer.Stop(); UnityEngine.Debug.Log("waktu (milidetik) : " + timer.ElapsedMilliseconds); UnityEngine.Debug.Log("===BATAS=================================================================================================================="); return; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }
public void FindPath(PathRequest request, Action <PathResult> callback) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(request.pathStart); Node targetNode = grid.NodeFromWorldPoint(request.pathEnd); if (startNode.walkable && targetNode.walkable) { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count() > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print("Path found in " + sw.ElapsedMilliseconds + " ms."); pathSuccess = true; break; } foreach (Node neighbor in grid.GetNeighbors(currentNode)) { if (!neighbor.walkable || closedSet.Contains(neighbor)) { continue; } int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor) + neighbor.movementPenalty; if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor)) { neighbor.gCost = newMovementCostToNeighbor; neighbor.hCost = GetDistance(neighbor, targetNode); neighbor.parent = currentNode; if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } else { openSet.UpdateItem(neighbor); } } } } } if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); pathSuccess = waypoints.Length > 0; } callback(new PathResult(waypoints, pathSuccess, request.callback)); }
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos) { Stopwatch sw = new Stopwatch(); sw.Start(); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); if (targetNode.walkable)//startNode.walkable && { //UnityEngine.Debug.Log("wee"); Heap <Node> openSet = new Heap <Node>(grid.MaxSize); //List<Node> openSet = new List<Node>(); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); //Node currentNode = openSet[0]; //for (int i = 1; i < openSet.Count; i++) //{ // if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost) // { // currentNode = openSet[i]; // } //} //openSet.Remove(currentNode); //openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); //print("Path found: " + sw.ElapsedMilliseconds + "ms"); pathSuccess = true; break; } //else //{ // UnityEngine.Debug.Log("pathnotfound"); //} foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } //else if(!startNode.walkable || !targetNode.walkable) //{ // UnityEngine.Debug.Log("not walkable"); //} yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } requestManager.FinishedProcessingPath(waypoints, pathSuccess); }