public void Initialize() { LocatedNode = GridManager.GetNode(Body._position.x, Body._position.y); LocatedNode.Add(this); }
public void Initialize() { LocatedNode = GridManager.GetNode(Body.Position.x, Body.Position.y); nodeIndex = LocatedNode.Add(Agent); }
public static bool NeedsPath(GridNode startNode, GridNode endNode, int unitSize) { //Tests if there is a direct path. If there is, no need to run AStar. x0 = startNode.gridX; y0 = startNode.gridY; x1 = endNode.gridX; y1 = endNode.gridY; if (y1 > y0) { compare1 = y1 - y0; } else { compare1 = y0 - y1; } if (x1 > x0) { compare2 = x1 - x0; } else { compare2 = x0 - x1; } steep = compare1 > compare2; if (steep) { t = x0; // swap x0 and y0 x0 = y0; y0 = t; t = x1; // swap x1 and y1 x1 = y1; y1 = t; } if (x0 > x1) { t = x0; // swap x0 and x1 x0 = x1; x1 = t; t = y0; // swap y0 and y1 y0 = y1; y1 = t; } dx = x1 - x0; dy = (y1 - y0); if (dy < 0) { dy = -dy; } error = dx / 2; ystep = (y0 < y1) ? 1 : -1; y = y0; GridNode.PrepareUnpassableCheck(unitSize); for (x = x0; x <= x1; x++) { retX = (steep ? y : x); retY = (steep ? x : y); currentNode = GridManager.Grid[GridManager.GetGridIndex(retX, retY)]; if (currentNode != null && currentNode.Unpassable()) { break; } else if (x == x1) { return(false); } error = error - dy; if (error < 0) { y += ystep; error += dx; } } return(true); }
/// <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); }
public void Initialize() { GenerateNeighbors(); LinkedScanNode = GridManager.GetScanNode(gridX / GridManager.ScanResolution, gridY / GridManager.ScanResolution); }
public static void ScanAll(Vector2d position, long radius, Func <LSAgent, bool> agentConditional, Func <byte, bool> bucketConditional, FastList <LSAgent> output) { //If radius is too big and we scan too many tiles, performance will be bad const long circleCastRadius = FixedMath.One * 16; output.FastClear(); if (radius >= circleCastRadius) { bufferBodies.FastClear(); PhysicsTool.CircleCast(position, radius, bufferBodies); for (int i = 0; i < bufferBodies.Count; i++) { var body = bufferBodies[i]; var agent = body.Agent; //we have to check agent's controller since we did not filter it through buckets if (agent.IsNotNull() && bucketConditional(agent.Controller.ControllerID)) { if (agentConditional(agent)) { output.Add(agent); } } } return; } int xMin = ((position.x - radius - GridManager.OffsetX) / (long)GridManager.ScanResolution).ToInt(); int xMax = ((position.x + radius - GridManager.OffsetX) / (long)GridManager.ScanResolution).CeilToInt(); int yMin = ((position.y - radius - GridManager.OffsetY) / (long)GridManager.ScanResolution).ToInt(); int yMax = ((position.y + radius - GridManager.OffsetY) / (long)GridManager.ScanResolution).CeilToInt(); long fastRadius = radius * radius; for (int x = xMin; x <= xMax; x++) { for (int y = yMin; y <= yMax; y++) { ScanNode tempNode = GridManager.GetScanNode( x, y); if (tempNode.IsNotNull()) { if (tempNode.AgentCount > 0) { bufferBuckets.FastClear(); tempNode.GetBucketsWithAllegiance(bucketConditional, bufferBuckets); for (int i = 0; i < bufferBuckets.Count; i++) { FastBucket <LSInfluencer> tempBucket = bufferBuckets[i]; BitArray arrayAllocation = tempBucket.arrayAllocation; for (int j = 0; j < tempBucket.PeakCount; j++) { if (arrayAllocation.Get(j)) { LSAgent tempAgent = tempBucket[j].Agent; long distance = (tempAgent.Body.Position - position).FastMagnitude(); if (distance < fastRadius) { if (agentConditional(tempAgent)) { output.Add(tempAgent); } } else { } } } } } } } } }