public void Initialize(LSAgent agent)
 {
     Agent = agent;
     Body = agent.Body;
     LocatedNode = GridManager.GetNode (Body.Position.x, Body.Position.y);
     LocatedNode.Add (this);
 }
        public void ScanAll(int deltaCount, FastList<LSAgent> outputAgents,
		                      bool CheckAllegiance = false,
		                      AllegianceType allegianceType = AllegianceType.Neutral)
        {
            for (i = 0; i < deltaCount; i++) {
                tempNode = GridManager.GetNode (
                    LocatedNode.gridX + DeltaCache.CacheX[i],
                    LocatedNode.gridY + DeltaCache.CacheY[i]);

                if (tempNode != null && tempNode.LocatedAgents != null) {
                    tempBucket = tempNode.LocatedAgents;
                    for (j = 0; j < tempBucket.PeakCount; j++) {
                        if (LSUtility.GetBitTrue (tempBucket.arrayAllocation, j)) {
                            tempAgent = tempBucket.innerArray [j].Agent;
                            if (System.Object.ReferenceEquals (tempAgent, Agent) == false) {
                                if (CheckAllegiance)
                                {
                                    if (Agent.MyAgentController.DiplomacyFlags
                                        [tempAgent.MyAgentController.ControllerID] != allegianceType) continue;
                                }
                                outputAgents.Add (tempAgent);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
 public static void Add(GridNode item)
 {
     item.HeapIndex = Count;
     items [Count++] = item;
     SortUp (item);
     item.HeapContained = true;
 }
Esempio n. 4
0
 public static void Add(GridNode item)
 {
     item.HeapIndex = Count;
     items [Count++] = item;
     SortUp (item);
     item.HeapVersion = _Version;
 }
Esempio n. 5
0
        static void SortDown(GridNode item)
        {
            while (true) {
                childIndexLeft = item.HeapIndex * 2 + 1;
                childIndexRight = item.HeapIndex * 2 + 2;
                swapIndex = 0;

                if (childIndexLeft < Count) {
                    swapIndex = childIndexLeft;

                    if (childIndexRight < Count) {
                        if (items [childIndexLeft].fCost > (items [childIndexRight]).fCost) {
                            swapIndex = childIndexRight;
                        }
                    }

                    swapNode = items[swapIndex];
                    if (item.fCost == swapNode.fCost)
                    {
                        if (item.gCost > swapNode.gCost)
                            Swap (item, swapNode);
                        else
                            return;
                    }
                    else if(item.fCost > swapNode.fCost) {
                        Swap (item, swapNode);
                    } else {
                        return;
                    }

                } else {
                    return;
                }
            }
        }
Esempio n. 6
0
 public static GridNode RemoveFirst()
 {
     curNode = items [0];
     items [0] = items [--Count];
     items [0].HeapIndex = 0;
     SortDown (items [0]);
     curNode.HeapVersion--;
     return curNode;
 }
Esempio n. 7
0
 public static GridNode RemoveFirst()
 {
     curNode = items [0];
     items [0] = items [--Count];
     items [0].HeapIndex = 0;
     SortDown (items [0]);
     curNode.HeapContained = false;
     return curNode;
 }
 public static void Generate()
 {
     Grid = new GridNode[NodeCount * NodeCount];
     for (int i = 0; i < NodeCount; i++)
     {
         for (int j = 0; j < NodeCount; j++)
         {
             Grid[i * NodeCount + j] = new GridNode(i,j);
         }
     }
 }
        public static bool FindPath(Vector2d End, GridNode startNode, GridNode endNode, FastList<Vector2d> outputVectorPath)
        {
            if (startNode.Unwalkable || endNode.Unwalkable) return false;
            if (FindPath (startNode, endNode, OutputPath)) {
                outputVectorPath.FastClear ();
                length = OutputPath.Count - 1;
                for (i = 0; i < length; i++) {
                    outputVectorPath.Add (OutputPath [i].WorldPos);
                }
                outputVectorPath.Add (End);

                return true;
            }
            return false;
        }
 public static void Generate()
 {
     ScanGrid = new ScanNode[ScanNodeCount * ScanNodeCount];
     for (int i = 0; i < NodeCount / ScanResolution; i++) {
         for (int j = 0; j < NodeCount / ScanResolution; j++) {
             ScanGrid [GetScanIndex (i, j)] = new ScanNode (i, j);
         }
     }
     Grid = new GridNode[NodeCount * NodeCount];
     for (int i = 0; i < NodeCount; i++) {
         for (int j = 0; j < NodeCount; j++) {
             Grid [i * NodeCount + j] = new GridNode (i, j);
         }
     }
 }
Esempio n. 11
0
        public static bool GetPathNode(long X, long Y, out GridNode returnNode)
        {
            returnNode = GridManager.GetNode(X,Y);
            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 + ySign);
                if (currentNode == null || currentNode.Unwalkable)
                {
                    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)
                        {
                            return false;
                        }
                    }
                }
                returnNode = currentNode;
            }
            return true;
        }
Esempio n. 12
0
 public static bool Contains(GridNode item)
 {
     return(item.HeapContained);
 }
Esempio n. 13
0
 public static void UpdateItem(GridNode item)
 {
     SortDown (item);
 }
Esempio n. 14
0
 public static bool Contains(GridNode item)
 {
     return item.HeapContained;
 }
Esempio n. 15
0
        static void Swap(GridNode itemA, GridNode itemB)
        {
            itemAIndex = itemA.HeapIndex;

            items [itemAIndex] = itemB;
            items [itemB.HeapIndex] = itemA;

            itemA.HeapIndex = itemB.HeapIndex;
            itemB.HeapIndex = itemAIndex;
        }
Esempio n. 16
0
 public bool DoesEqual(GridNode obj)
 {
     return obj.gridIndex == this.gridIndex;
 }
 public static void Add(GridNode item)
 {
     item.ClosedSetVersion = _Version;
 }
 public void Deactivate()
 {
     LocatedNode.RemoveAt (this.nodeIndex);
     LocatedNode = null;
 }
 public void Initialize()
 {
     LocatedNode = GridManager.GetNode (Body.Position.x, Body.Position.y);
     nodeIndex = LocatedNode.Add (Agent);
 }
Esempio n. 20
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)
        {
            #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);
            }
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            GridHeap.FastClear();
            GridClosedSet.FastClear();
            #endregion

            #region AStar Algorithm
            GridHeap.Add(startNode);
            GridNode.HeuristicTargetX = endNode.gridX;
            GridNode.HeuristicTargetY = endNode.gridY;
            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

                    outputPath.FastClear();
                    TracePath.FastClear();

                    currentNode = endNode;

                    StartNodeIndex = startNode.gridIndex;
                    while (currentNode.gridIndex != StartNodeIndex)
                    {
                        TracePath.Add(currentNode);
                        oldNode     = currentNode;
                        currentNode = currentNode.parent;
                    }

                    oldNode     = startNode;
                    currentNode = TracePath [TracePath.Count - 1];
                    oldX        = currentNode.gridX - oldNode.gridX;
                    oldY        = currentNode.gridY - oldNode.gridY;

                    for (i = TracePath.Count - 2; i >= 0; i--)
                    {
                        oldNode     = currentNode;
                        currentNode = TracePath.innerArray [i];
                        newX        = currentNode.gridX - oldNode.gridX;
                        newY        = currentNode.gridY - oldNode.gridY;

                        if (newX != oldX || newY != oldY)
                        {
                            outputPath.Add(oldNode);
                            oldX = newX;
                            oldY = newY;
                        }
                        //outputPath.Add (currentNode);
                    }
                    outputPath.Add(endNode);

                    return(true);
                }

                for (i = 0; i < 8; i++)
                {
                    neighbor = currentNode.NeighborNodes [i];

                    if (neighbor == null || neighbor.Unwalkable || GridClosedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    newMovementCostToNeighbor = currentNode.gCost + (GridNode.IsNeighborDiagnal [i] ? 141 : 100);

                    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);
        }
Esempio n. 21
0
 public static bool GetPathNodes(long StartX, long StartY, long EndX, long EndY, out GridNode startNode, out GridNode endNode)
 {
     startNode = GridManager.GetNode(StartX, StartY);
     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.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);
         }
     }
     return(true);
 }
Esempio n. 22
0
        public static bool NeedsPath(GridNode startNode, GridNode endNode)
        {
            //return true;
            //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;
            for (x = x0; x <= x1; x++)
            {
                retX = (steep ? y : x);
                retY = (steep ? x : y);

                if (GridManager.Grid [retX * GridManager.NodeCount + retY].Unwalkable)
                {
                    break;
                }
                else if (x == x1)
                {
                    return(false);
                }

                error = error - dy;
                if (error < 0)
                {
                    y     += ystep;
                    error += dx;
                }
            }
            return(true);
        }
Esempio n. 23
0
 public static bool GetPathNodes(long StartX, long StartY, long EndX, long EndY, out GridNode startNode, out GridNode endNode)
 {
     startNode = GridManager.GetNode (StartX, StartY);
     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.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;
     }
     return true;
 }
 public static bool Contains(GridNode item)
 {
     return item.ClosedSetVersion == _Version;
 }
Esempio n. 25
0
        public static bool NeedsPath(GridNode startNode, GridNode endNode)
        {
            //return true;
            //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;
            for (x = x0; x <= x1; x++) {
                retX = (steep ? y : x);
                retY = (steep ? x : y);

                if (GridManager.Grid [retX * GridManager.NodeCount + retY].Unwalkable) {
                    break;
                } else if (x == x1) {
                    return false;
                }

                error = error - dy;
                if (error < 0) {
                    y += ystep;
                    error += dx;
                }
            }
            return true;
        }
Esempio n. 26
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)
        {
            #region Broadphase and Preperation
            if (endNode.Unwalkable) {
                return false;
            }

            if (startNode.Unwalkable) {
                return false;
            }

            if (true) {
                #region Obstruction Test
                //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;
                for (x = x0; x <= x1; x++) {
                    retX = (steep ? y : x);
                    retY = (steep ? x : y);

                    if (GridManager.Grid [retX * GridManager.NodeCount + retY].Unwalkable) {
                        break;
                    } else if (x == x1) {
                        OutputPath.FastClear ();
                        OutputPath.Add (startNode);
                        OutputPath.Add (endNode);
                        return true;
                    }

                    error = error - dy;
                    if (error < 0) {
                        y += ystep;
                        error += dx;
                    }
                }
                #endregion
            }

            GridHeap.FastClear ();
            GridClosedSet.FastClear ();
            #endregion

            #region AStar Algorithm
            GridHeap.Add (startNode);
            GridNode.HeuristicTargetX = endNode.gridX;
            GridNode.HeuristicTargetY = endNode.gridY;
            while (GridHeap.Count > 0) {
                currentNode = GridHeap.RemoveFirst ();

                GridClosedSet.Add (currentNode);

                if (currentNode.gridIndex == endNode.gridIndex) {
                    OutputPath.FastClear ();

                    //Retraces the path then outputs it into OutputPath
                    //Also Simplifies the path

                    oldNode = endNode;
                    currentNode = endNode.parent;

                    oldX = int.MaxValue;
                    oldY = int.MaxValue;

                    StartNodeIndex = startNode.gridIndex;

                    //if (!endNode.Obstructed) OutputPath.Add (endNode);

                    while (oldNode.gridIndex != StartNodeIndex) {
                        newX = currentNode.gridX - oldNode.gridX;
                        newY = currentNode.gridY - oldNode.gridY;
                        if ((newX != oldX || newY != oldY))
                        {
                            OutputPath.Add (oldNode);
                            oldX = newX;
                            oldY = newY;
                        }

                        oldNode = currentNode;
                        currentNode = currentNode.parent;
                    }

                    OutputPath.Add (startNode);
                    OutputPath.Reverse ();
                    return true;
                }

                for (i = 0; i < 8; i++) {
                    neighbor = currentNode.NeighborNodes [i];

                    if (neighbor == null|| neighbor.Unwalkable || GridClosedSet.Contains (neighbor)) {
                        continue;
                    }

                    newMovementCostToNeighbor = currentNode.gCost + (currentNode.NeighborDiagnal [i] ? 141 : 100);

                    if (!GridHeap.Contains (neighbor)) {
                        neighbor.gCost = newMovementCostToNeighbor;

                        //Optimized heuristic calculation
                        neighbor.CalculateHeurustic ();
                        neighbor.parent = currentNode;

                        GridHeap.Add (neighbor);
                    } else if (newMovementCostToNeighbor < neighbor.gCost) {
                        neighbor.gCost = newMovementCostToNeighbor;

                        //Optimized heuristic calculation
                        neighbor.CalculateHeurustic ();
                        neighbor.parent = currentNode;

                        GridHeap.UpdateItem (neighbor);
                    }
                }
            }
            #endregion
            return false;
        }
Esempio n. 27
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)
        {
            #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;
            }
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start ();

            GridHeap.FastClear ();
            GridClosedSet.FastClear ();
            #endregion

            #region AStar Algorithm
            GridHeap.Add (startNode);
            GridNode.HeuristicTargetX = endNode.gridX;
            GridNode.HeuristicTargetY = endNode.gridY;
            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

                    outputPath.FastClear ();
                    TracePath.FastClear ();

                    currentNode = endNode;

                    StartNodeIndex = startNode.gridIndex;
                    while (currentNode.gridIndex != StartNodeIndex) {
                        TracePath.Add (currentNode);
                        oldNode = currentNode;
                        currentNode = currentNode.parent;
                    }

                    oldNode = startNode;
                    currentNode = TracePath [TracePath.Count - 1];
                    oldX = currentNode.gridX - oldNode.gridX;
                    oldY = currentNode.gridY - oldNode.gridY;

                    for (i = TracePath.Count - 2; i >= 0; i--) {
                        oldNode = currentNode;
                        currentNode = TracePath.innerArray [i];
                        newX = currentNode.gridX - oldNode.gridX;
                        newY = currentNode.gridY - oldNode.gridY;

                        if (newX != oldX || newY != oldY) {

                            outputPath.Add (oldNode);
                            oldX = newX;
                            oldY = newY;
                        }
                        //outputPath.Add (currentNode);
                    }
                    outputPath.Add (endNode);

                    return true;
                }

                for (i = 0; i < 8; i++) {
                    neighbor = currentNode.NeighborNodes [i];

                    if (neighbor == null || neighbor.Unwalkable || GridClosedSet.Contains (neighbor)) {
                        continue;
                    }

                    newMovementCostToNeighbor = currentNode.gCost + (GridNode.IsNeighborDiagnal [i] ? 141 : 100);

                    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 static void Add(GridNode item)
 {
     item.ClosedSetVersion = _Version;
 }
Esempio n. 29
0
        static void SortUp(GridNode item)
        {
            if (parentIndex != 0)
            parentIndex = (item.HeapIndex-1) / 2;

            while (true) {
                curNode = items [parentIndex];
                if (item.fCost == curNode.fCost)
                {
                    if (item.gCost < curNode.gCost)
                        Swap (item,curNode);
                    else
                        return;
                }
                else if(item.fCost < curNode.fCost) {
                    Swap (item, curNode);
                } else {
                    break;
                }
                if (parentIndex != 0)
                parentIndex = (item.HeapIndex - 1) / 2;
            }
        }
Esempio n. 30
0
 public static void UpdateItem(GridNode item)
 {
     SortDown(item);
 }
Esempio n. 31
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);
        }
Esempio n. 32
0
        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);
        }
Esempio n. 33
0
 public bool DoesEqual(GridNode obj)
 {
     return(obj.gridIndex == this.gridIndex);
 }
        public void Simulate()
        {
            tempNode = GridManager.GetNode (Body.Position.x, Body.Position.y);

            if (Body.PositionChangedBuffer) {
                if (System.Object.ReferenceEquals (tempNode, LocatedNode) == false) {
                    LocatedNode.LocatedAgents.Remove (this);
                    LocatedNode = tempNode;
                    LocatedNode.Add (this);
                }
            }
        }
 public void Deactivate()
 {
     LocatedNode.Remove (this);
     LocatedNode = null;
 }
Esempio n. 36
0
 public static bool Contains(GridNode item)
 {
     return item.HeapVersion == _Version;
 }
Esempio n. 37
0
 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 static bool Contains(GridNode item)
 {
     return(item.ClosedSetVersion == _Version);
 }