public PathRequest(DispensaryGrid _grid, Vector3 _start, Vector3 _end, Func <Vector3[], bool, Vector3[]> _callback) { grid = _grid; pathStart = _start; pathEnd = _end; callback = _callback; }
public static void RequestPath(DispensaryGrid grid, Vector3 pathStart, Vector3 pathEnd, Func <Vector3[], bool, Vector3[]> callback) { PathRequest newRequest = new PathRequest(grid, pathStart, pathEnd, callback); instance.pathRequestQueue.Enqueue(newRequest); instance.TryProcessNext(); }
public void GetClosest(DispensaryGrid grid, Vector3 startPos, ComponentNode originalTargetNode, List <ComponentNode> closed) { float currentDist = 1000; ComponentNode currentClosestNode = new ComponentNode(); foreach (ComponentNode node in closed) { if (node.walkable) { float newdist = Vector3.Distance(node.worldPosition, originalTargetNode.worldPosition); if (newdist < currentDist) { currentDist = newdist; currentClosestNode = node; } } } if (!currentClosestNode.isNull) { if (grid != null) { StartFindPath(grid, startPos, currentClosestNode.worldPosition); } else { print("Grid was null"); } } }
IEnumerator FindPath(DispensaryGrid grid, Vector3 startPos, Vector3 targetPos) { Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; ComponentNode startNode = grid.NodeFromWorldPoint(startPos); ComponentNode targetNode = grid.NodeFromWorldPoint(targetPos); List <ComponentNode> closedList = new List <ComponentNode>(); HashSet <ComponentNode> closedSet = new HashSet <ComponentNode>(); if (true) { Heap <ComponentNode> openSet = new Heap <ComponentNode>(grid.MaxSize); openSet.Add(startNode); while (openSet.Count > 0) { ComponentNode currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); closedList.Add(currentNode); if (currentNode == targetNode) { pathSuccess = true; break; } foreach (ComponentNode neighbour in grid.GetNeighbours(currentNode)) { int extraMovementPenalty = 0; foreach (ComponentNode neighbour_ in grid.GetNeighbours(neighbour)) { if (!neighbour_.walkable) { extraMovementPenalty = 15; } } if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + extraMovementPenalty; if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (neighbour.hCost < lowestHValue) { lowestHNode = neighbour; lowestHValue = neighbour.hCost; } if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } else { if (lowestHNode != null) { if (grid != null) { StartFindPath(grid, startPos, lowestHNode.worldPosition); } else { print("Grid was null"); } yield break; } else { GetClosest(grid, startPos, targetNode, closedList); yield break; } } requestManager.FinishedProcessingPath(waypoints, targetPos, pathSuccess); }
public void StartFindPath(DispensaryGrid grid, Vector3 startPos, Vector3 targetPos) { lowestHNode = null; lowestHValue = 1000; StartCoroutine(FindPath(grid, startPos, targetPos)); }