Beispiel #1
0
        public void DrawOverlayGraph(int level)
        {
            for (var i = 0; i < overlayVerticesCellMapping[level].Length; i++)
            {
                for (var j = 0; j < overlayVerticesCellMapping[level][i].Count; j++)
                {
                    var overlayVertex = overlayVertices[overlayVerticesCellMapping[level][i][j]];
                    var vertex        = vertices[overlayVertex.OriginalVertex];
                    var x             = vertex.GridPosition / sizeY;
                    var y             = vertex.GridPosition % sizeY;
                    DebugDrawer.DrawCube(new UnityEngine.Vector2Int(x, y), Vector2Int.one, Color.red);

                    for (var n = 0; n < overlayVertex.OverlayEdges[level - 1].Count; n++)
                    {
                        var target = overlayVertex.OverlayEdges[level - 1][n].NeighborOverlayVertex;
                        if (target == -1)
                        {
                            continue;
                        }

                        var tx = vertices[overlayVertices[target].OriginalVertex].GridPosition / sizeY;
                        var ty = vertices[overlayVertices[target].OriginalVertex].GridPosition % sizeY;
                        DebugDrawer.Draw(new Vector2Int(x, y), new Vector2Int(tx, ty), new Color(1, 1, 1));
                    }
                }
            }
        }
Beispiel #2
0
        public void DrawGraph()
        {
            for (var i = 0; i < vertices.Length; i++)
            {
                var vertex = vertices[i];
                var x      = vertex.GridPosition / sizeY;
                var y      = vertex.GridPosition % sizeY;

                DebugDrawer.DrawCube(new UnityEngine.Vector2Int(x, y), Vector2Int.one, Color.yellow);
                var edgeEnd = (i + 1 == vertices.Length) ? edges.Length : vertices[i + 1].EdgeOffset;
                if (edgeEnd == -1)
                {
                    continue;
                }

                for (var j = vertex.EdgeOffset; j < edgeEnd; j++)
                {
                    var edge   = edges[j];
                    var target = edge.ToVertex;
                    if (target == -1)
                    {
                        var tx = edge.ToVertexGridPosition / sizeY;
                        var ty = edge.ToVertexGridPosition % sizeY;
                        DebugDrawer.Draw(new Vector2Int(x, y), new Vector2Int(tx, ty), Color.yellow);
                    }
                    else
                    {
                        var tx = vertices[target].GridPosition / sizeY;
                        var ty = vertices[target].GridPosition % sizeY;
                        DebugDrawer.Draw(new Vector2Int(x, y), new Vector2Int(tx, ty), Color.yellow);
                    }
                }
            }
        }
Beispiel #3
0
        private bool CalculateShortestPath()
        {
            Node currentNode;

            Position[] neighbors = new Position[8];

            heap.Add(startNode, 0);
            while (heap.Count > 0)
            {
                currentNode = heap.Remove();
                if (currentNode == targetNode)
                {
                    return(true);
                }

                currentNode.setClosed();
                var count = GetNeighbors(currentNode, ref neighbors);
                for (var i = 0; i < count; i++)
                {
                    var neighbor     = neighbors[i];
                    var neighborNode = GetNeighborNode(currentNode, neighbor);
                    if (neighborNode == null || neighborNode.isClosed())
                    {
                        continue;
                    }

                    int newGCost = NewGCost(currentNode, neighborNode);
                    if (newGCost < neighborNode.gCost || !neighborNode.isOpen())
                    {
#if DEBUG_PATHFINDING
                        if (showDebug)
                        {
                            DebugDrawer.Draw(new Vector2Int(currentNode.x, currentNode.y), new Vector2Int(neighborNode.x, neighborNode.y), Color.white);
                            DebugDrawer.DrawCube(new Vector2Int(neighborNode.x, neighborNode.y), Vector2Int.one, Color.white);
                        }
#endif

                        neighborNode.gCost  = newGCost;
                        neighborNode.hCost  = Heuristic(neighborNode, targetNode);
                        neighborNode.parent = currentNode;

                        if (!neighborNode.isOpen())
                        {
                            heap.Add(neighborNode, neighborNode.gCost + neighborNode.hCost);
                            neighborNode.setOpen();
                        }
                        else
                        {
                            heap.Update(neighborNode, neighborNode.gCost + neighborNode.hCost);
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #4
0
        public void DrawSubGoals()
        {
            foreach (var subgoal in subGoals.Values)
            {
                DebugDrawer.DrawCube(new UnityEngine.Vector2Int(subgoal.x, subgoal.y), Vector2Int.one, Color.yellow);

                foreach (var edge in subgoal.edges)
                {
                    DebugDrawer.Draw(new Vector2Int(subgoal.x, subgoal.y), new Vector2Int(edge.toX, edge.toY), Color.yellow);
                }
            }
        }
Beispiel #5
0
        public override void Draw(GameTime gameTime)
        {
            if (ActiveScene != null && CameraManager.ActiveCamera != null)
            {
                ActiveScene.Draw(CameraManager.ActiveCamera);
                ActiveScene.DrawUI();

                debug.Draw(CameraManager.ActiveCamera);
            }

            base.Draw(gameTime);
        }
Beispiel #6
0
        private void drawEdgesVertex(int vertexID, int chunkID)
        {
            var targetNeighborChunk = graph.GetChunk(chunkID);
            var targetVertex        = targetNeighborChunk.vertices[vertexID];

            var(tx, ty) = fromGridPosition(targetVertex.GridPosition);
            for (var k = targetVertex.EdgeOffset; k < targetNeighborChunk.vertices[vertexID + 1].EdgeOffset; k++)
            {
                var edge       = targetNeighborChunk.edges[k];
                var edgeTarget = edge.ToVertex;
                var(hx, hy) = fromGridPosition(edgeTarget == -1 ? edge.ToVertexGridPosition : targetNeighborChunk.vertices[edgeTarget].GridPosition);
                DebugDrawer.Draw(new Vector2Int(tx, ty), new Vector2Int(hx, hy), Color.white);
            }
        }
Beispiel #7
0
        private void updateForwardFrontier(Node node, float nodeScore)
        {
#if DEBUG_PATHFINDING
            if (showDebug)
            {
                DebugDrawer.Draw(new Vector2Int(node.parentA.x, node.parentA.y), new Vector2Int(node.x, node.y), Color.white);
                DebugDrawer.DrawCube(new Vector2Int(node.x, node.y), Vector2Int.one, Color.white);
            }
#endif

            if (node.isClosedB())
            {
                var pathLength = node.costB + nodeScore;
                if (bestPathLength > pathLength)
                {
                    bestPathLength = pathLength;
                    middleNode     = node;
                }
            }
        }
    public void FindPath(bool astar)
    {
        DebugDrawer.Clear();

        // Convert visible grid to memory grid
        var grid     = GridController.Ground;
        var pathGrid = GridController.Path;

        var sizeX = GridController.active.size.x;
        var sizeY = GridController.active.size.y;

        var memoryGrid = createGraph();
        // Set grid weights
        var start = new Vector2Int(-1, -1);
        var end   = new Vector2Int(-1, -1);

        for (var x = 0; x < sizeX; x++)
        {
            for (var y = 0; y < sizeY; y++)
            {
                var pathTile = pathGrid.GetTile(new Vector3Int(x, y, 0));
                if (pathTile != null)
                {
                    if (pathTile == GridController.active.start)
                    {
                        start = new Vector2Int(x, y);
                    }
                    else if (pathTile == GridController.active.end)
                    {
                        end = new Vector2Int(x, y);
                    }
                }
            }
        }

        if (start.x == -1 || end.x == -1)
        {
            Debug.Log("Couldn't find any start or end position");
            return;
        }

        List <Node> path;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        if (astar)
        {
            var asearch = new AStarSearch();
            sw.Start();
            path = asearch.GetPath(memoryGrid, start, end);
            UnityEngine.Debug.Log("A* Path - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");
        }
        else
        {
            var jpsearch = new JumpPointSearch();
            sw.Start();
            path = jpsearch.GetPath(memoryGrid, start, end);
            UnityEngine.Debug.Log("JPS Path - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");
        }

        if (path != null)
        {
            foreach (var pathTile in path)
            {
                if (pathTile.x == start.x && pathTile.y == start.y)
                {
                    continue;
                }
                if (pathTile.x == end.x && pathTile.y == end.y)
                {
                    continue;
                }

                if (pathTile.parent != null)
                {
                    DebugDrawer.Draw(new Vector2Int(pathTile.parent.x, pathTile.parent.y), new Vector2Int(pathTile.x, pathTile.y), Color.blue);
                }
                DebugDrawer.DrawCube(new Vector2Int(pathTile.x, pathTile.y), Vector2Int.one, Color.blue);
            }
        }
    }
    public void FindSubGoalPath()
    {
        DebugDrawer.Clear();

        // Convert visible grid to memory grid
        var grid     = GridController.Ground;
        var pathGrid = GridController.Path;

        var sizeX = GridController.active.size.x;
        var sizeY = GridController.active.size.y;

        var memoryGrid = createGraph();
        // Set grid weights
        var start = new Vector2Int(-1, -1);
        var end   = new Vector2Int(-1, -1);

        for (var x = 0; x < sizeX; x++)
        {
            for (var y = 0; y < sizeY; y++)
            {
                var pathTile = pathGrid.GetTile(new Vector3Int(x, y, 0));
                if (pathTile != null)
                {
                    if (pathTile == GridController.active.start)
                    {
                        start = new Vector2Int(x, y);
                    }
                    else if (pathTile == GridController.active.end)
                    {
                        end = new Vector2Int(x, y);
                    }
                }
            }
        }

        if (start.x == -1 || end.x == -1)
        {
            Debug.Log("Couldn't find any start or end position");
            return;
        }

        // List<Node> path;
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        SubGoalGrid sgg = new SubGoalGrid(memoryGrid);

        sgg.ConstructSubgoals(0, 0, sizeX, sizeY);
        sgg.ConstructEdges(0, 0, sizeX, sizeY);

        Debug.Log(sgg.subGoals.Count);

        sw.Stop();
        Debug.Log("Preprocessing took " + sw.ElapsedMilliseconds + "ms");
        sgg.DrawSubGoals();

        List <Node> path;
        var         subGoalSearch = new SubGoalAStarSearch(sgg);

        sw.Restart();
        path = subGoalSearch.GetPath(sgg, start, end);
        UnityEngine.Debug.Log("SubGoalSearch Path - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");

        if (path != null)
        {
            foreach (var pathTile in path)
            {
                if (pathTile.x == start.x && pathTile.y == start.y)
                {
                    continue;
                }
                if (pathTile.x == end.x && pathTile.y == end.y)
                {
                    continue;
                }

                if (pathTile.parent != null)
                {
                    DebugDrawer.Draw(new Vector2Int(pathTile.parent.x, pathTile.parent.y), new Vector2Int(pathTile.x, pathTile.y), Color.blue);
                }
                DebugDrawer.DrawCube(new Vector2Int(pathTile.x, pathTile.y), Vector2Int.one, Color.blue);
            }
        }
    }