Beispiel #1
0
 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 (tempNode.IsNull())
                {
                    return;
                }

                if (System.Object.ReferenceEquals(tempNode, LocatedNode) == false)
                {
                    LocatedNode.Remove(this);
                    tempNode.Add(this);
                    LocatedNode = tempNode;
                }
            }
        }
        public static bool GetScanCoordinates(long xPos, long yPos, out int xGrid, out int yGrid)
        {
            //xGrid = (int)((((xPos + FixedMath.Half - 1 - OffsetX) >> FixedMath.SHIFT_AMOUNT) + ScanResolution / 2) / ScanResolution);
            //yGrid = (int)((((yPos + FixedMath.Half - 1 - OffsetY) >> FixedMath.SHIFT_AMOUNT) + ScanResolution / 2) / ScanResolution);

            GridNode gridNode = GetNode(xPos, yPos);

            if (gridNode.IsNull())
            {
                xGrid = 0;
                yGrid = 0;
                return(false);
            }

            ScanNode scanNode = gridNode.LinkedScanNode;

            xGrid = scanNode.X;
            yGrid = scanNode.Y;

            return(true);
        }
 static bool CheckNeighborInvalid()
 {
     return(neighbor.IsNull() || neighbor.Unpassable() || GridClosedSet.Contains(neighbor));
 }
 static bool CheckInvalid(GridNode gridNode)
 {
     return(gridNode.IsNull() || gridNode.Unpassable() || GridClosedSet.Contains(gridNode));
 }
Beispiel #6
0
        /// <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() || currentNode.Unpassable() || GridClosedSet.Contains(neighbor))
                    {
                        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);
        }