public static bool GetPathNode(long X, long Y, out GridNode returnNode) { returnNode = GridManager.GetNode(X, Y); if (returnNode == null) { return(false); } if (returnNode.Unwalkable) { xSign = X > returnNode.WorldPos.x ? 1 : -1; ySign = Y > returnNode.WorldPos.y ? 1 : -1; currentNode = GridManager.GetNode(returnNode.gridX + xSign, returnNode.gridY); if (currentNode == null || currentNode.Unwalkable) { currentNode = GridManager.GetNode(returnNode.gridX, returnNode.gridY + ySign); if (currentNode == null || currentNode.Unwalkable) { currentNode = GridManager.GetNode(returnNode.gridX + xSign, returnNode.gridY + ySign); if (currentNode == null || currentNode.Unwalkable) { return(false); } } } returnNode = currentNode; } return(true); }
void BlockNode(long x, long y) { var node = GridManager.GetNode(x, y); if (node.IsNotNull()) { node.AddObstacle(); } }
public bool UnpassableLarge() { for (_i = 1; _i < CachedLargeDeltaCount; _i++) { GridNode node = GridManager.GetNode(DeltaCache.CacheX [_i] + this.gridX, DeltaCache.CacheY [_i] + this.gridY); if (node.Unwalkable) { return(true); } } return(false); }
public static bool GetPathNodes(long StartX, long StartY, long EndX, long EndY, out GridNode startNode, out GridNode endNode, out bool endWalkable) { endWalkable = false; startNode = GridManager.GetNode(StartX, StartY); if (startNode.IsNull()) { endNode = null; return(false); } if (startNode.Unwalkable) { for (i = 0; i < 8; i++) { currentNode = startNode.NeighborNodes[i]; if (System.Object.ReferenceEquals(currentNode, null) == false && currentNode.Unwalkable == false) { startNode = currentNode; break; } } if (startNode.Unwalkable) { endNode = null; return(false); } } endNode = GridManager.GetNode(EndX, EndY); if (endNode.IsNull()) { return(false); } if (endNode.Unwalkable) { for (i = 0; i < 8; i++) { currentNode = endNode.NeighborNodes[i]; if (System.Object.ReferenceEquals(currentNode, null) == false && currentNode.Unwalkable == false) { endNode = currentNode; break; } } if (endNode.Unwalkable) { return(false); } } else { endWalkable = true; } return(true); }
public void Simulate() { if (Body.PositionChangedBuffer) { tempNode = GridManager.GetNode(Body._position.x, Body._position.y); if (System.Object.ReferenceEquals(tempNode, LocatedNode) == false) { LocatedNode.Remove(this); tempNode.Add(this); LocatedNode = tempNode; } } }
public void Initialize() { var body = GetComponent <LSBody>(); body.Setup(null); body.InitializeSerialized(); if (MapToGrid) { for (long i = body.XMin; i <= body.XMax; i += FixedMath.One) { for (long j = body.YMin; j <= body.YMax; j += FixedMath.One) { GridManager.GetNode(i, j).Unwalkable = true; } } } }
public static void BlockArea(Area block) { long xMin = block.XMin; long xMax = block.XMax; long yMin = block.YMin; long yMax = block.YMax; for (long x = xMin; x <= xMax; x += FixedMath.One) { for (long y = yMin; y <= yMax; y += FixedMath.One) { var node = GridManager.GetNode(x, y); if (node.IsNotNull()) { node.AddObstacle(); } } } }
void UpdateCoordinates() { const long gridSpacing = FixedMath.One; bufferCoordinates.FastClear(); CachedBody.GetCoveredSnappedPositions(gridSpacing, bufferCoordinates); foreach (Vector2d vec in bufferCoordinates) { GridNode node = GridManager.GetNode(vec.x, vec.y); if (node == null) { continue; } node.AddObstacle(); LastCoordinates.Add(node); } }
public void Initialize() { LocatedNode = GridManager.GetNode(Body._position.x, Body._position.y); LocatedNode.Add(this); }
public static bool FindPath(Vector2d Start, Vector2d End, FastList <GridNode> outputPath) { currentNode = GridManager.GetNode(Start.x, Start.y); if (currentNode.Unwalkable) { //If the start node is unwalkable, attempt to locate the nearest walkable node if (Start.x > currentNode.WorldPos.x) { IndexX = 1; } else { IndexX = -1; } if (Start.y > currentNode.WorldPos.y) { IndexY = 1; } else { IndexY = -1; } node1 = currentNode.NeighborNodes [GridNode.GetNeighborIndex(IndexX, IndexY)]; if (node1 == null || node1.Unwalkable) { node1 = currentNode.NeighborNodes[GridNode.GetNeighborIndex(IndexX, 0)]; if (node1 == null || node1.Unwalkable) { node1 = currentNode.NeighborNodes[GridNode.GetNeighborIndex(0, IndexY)]; if (node1 == null || node1.Unwalkable) { return(false); } } } } else { node1 = currentNode; } currentNode = GridManager.GetNode(End.x, End.y); if (currentNode.Unwalkable) { //If the start node is unwalkable, attempt to locate the nearest walkable node if (End.x > currentNode.WorldPos.x) { IndexX = 1; } else { IndexX = -1; } if (End.y > currentNode.WorldPos.y) { IndexY = 1; } else { IndexY = -1; } node2 = currentNode.NeighborNodes [GridNode.GetNeighborIndex(IndexX, IndexY)]; if (node2 == null || node2.Unwalkable) { node2 = currentNode.NeighborNodes[GridNode.GetNeighborIndex(IndexX, 0)]; if (node2 == null || node2.Unwalkable) { node2 = currentNode.NeighborNodes[GridNode.GetNeighborIndex(0, IndexY)]; if (node2 == null || node2.Unwalkable) { return(false); } } } } else { node2 = currentNode; } OutputPath = outputPath; return(FindPath(node1, node2, OutputPath)); }
public void Initialize() { LocatedNode = GridManager.GetNode(Body.Position.x, Body.Position.y); nodeIndex = LocatedNode.Add(Agent); }
/// <summary> /// Finds a path and outputs it to <c>outputPath</c>. Note: outputPath is unpredictably changed. /// </summary> /// <returns> /// Returns <c>true</c> if path was found and necessary, <c>false</c> if path to End is impossible or not found. /// </returns> /// <param name="startNode">Start node.</param> /// <param name="endNode">End node.</param> /// <param name="outputPath">Return path.</param> public static bool FindPath(GridNode _startNode, GridNode _endNode, FastList <GridNode> _outputPath, int _unitSize = 1) { startNode = _startNode; endNode = _endNode; outputPath = _outputPath; unitSize = _unitSize; #region Broadphase and Preperation if (endNode.Unwalkable) { return(false); } if (startNode.Unwalkable) { return(false); } outputPath.FastClear(); if (System.Object.ReferenceEquals(startNode, endNode)) { outputPath.Add(endNode); return(true); } GridHeap.FastClear(); GridClosedSet.FastClear(); #endregion #region AStar Algorithm GridHeap.Add(startNode); GridNode.HeuristicTargetX = endNode.gridX; GridNode.HeuristicTargetY = endNode.gridY; GridNode.PrepareUnpassableCheck(unitSize); //Prepare Unpassable check optimizations while (GridHeap.Count > 0) { currentNode = GridHeap.RemoveFirst(); GridClosedSet.Add(currentNode); if (currentNode.gridIndex == endNode.gridIndex) { //Retraces the path then outputs it into outputPath //Also Simplifies the path DestinationReached(); return(true); } for (i = 0; i < 8; i++) { neighbor = currentNode.NeighborNodes[i]; if (neighbor.IsNull() || neighbor.Unpassable() || GridClosedSet.Contains(neighbor)) { continue; } if (GridManager.GetNode(currentNode.gridX, neighbor.gridY).Unpassable() || GridManager.GetNode(neighbor.gridX, currentNode.gridY).Unpassable()) { continue; } //0-3 = sides, 4-7 = diagonals if (i < 4) { newMovementCostToNeighbor = currentNode.gCost + 100; } else { if (i == 4) { if (!GridManager.UseDiagonalConnections) { break; } } newMovementCostToNeighbor = currentNode.gCost + 141; } if (!GridHeap.Contains(neighbor)) { neighbor.gCost = newMovementCostToNeighbor; //Optimized heuristic calculation neighbor.CalculateHeuristic(); neighbor.parent = currentNode; GridHeap.Add(neighbor); } else if (newMovementCostToNeighbor < neighbor.gCost) { neighbor.gCost = newMovementCostToNeighbor; //Optimized heuristic calculation neighbor.CalculateHeuristic(); neighbor.parent = currentNode; GridHeap.UpdateItem(neighbor); } } } #endregion return(false); }