Exemple #1
0
        [Test] //Partial vertical block
        public void PathFoundIsTheShortest_WhenPathIsPartiallyBlocked()
        {
            var grid = new PathfindingGrid(6, 5, Allocator.Persistent);

            SetArea(grid, 6, 5, 1, 1);
            grid.SetClearance(3, 0, 0);
            grid.SetClearance(3, 1, 0);
            grid.SetClearance(3, 2, 0);
            grid.SetClearance(3, 3, 0);

            NativeList <int> resultPath = new NativeList <int>(Allocator.Persistent);
            var pathFound = AStarSearch.TryGetPath(grid,
                                                   2, 0,
                                                   5, 4,
                                                   1, 1, Allocator.Persistent, resultPath);

            Assert.AreEqual(true, pathFound);
            Assert.AreEqual(2, resultPath[0]);
            Assert.AreEqual(8, resultPath[1]);
            Assert.AreEqual(14, resultPath[2]);
            Assert.AreEqual(20, resultPath[3]);
            Assert.AreEqual(27, resultPath[4]);
            Assert.AreEqual(28, resultPath[5]);

            grid.Dispose();
            resultPath.Dispose();
        }
Exemple #2
0
        private void RunSearch()
        {
            var grid = new PathfindingGrid(GridWidth, GridHeight, Allocator.Persistent);

            for (var i = 0; i < Cells.Length; ++i)
            {
                var cell = Cells[i];
                grid.SetClearance(cell.GridX, cell.GridY, cell.Clearance);
                grid.SetFlag(cell.GridX, cell.GridY, cell.Capabilities);
            }

            NativeList <int> path = new NativeList <int>(Allocator.Persistent);
            var pathFound         = AStarSearch.TryGetPath(grid,
                                                           SelectedCell1.GridX, SelectedCell1.GridY,
                                                           SelectedCell2.GridX, SelectedCell2.GridY,
                                                           UnitSize, UnitCapabilities,
                                                           Allocator.Persistent, path);

            foreach (var index in path)
            {
                Cells[index].SetCellColor(SelectedColor);
            }

            if (path.IsCreated)
            {
                path.Dispose();
            }

            grid.Dispose();
        }
Exemple #3
0
 void Awake()
 {
     Instance = this;
     SetEdgeColliders();
     // backgroundTransform.position = new Vector3((width / 2f) - .5f, (height / 2) - .5f, 0);
     //  backgroundTransform.localScale = new Vector3(width, height, 0);
 }
Exemple #4
0
    private PathfindingGrid BuildGrid()
    {
        float nodeDiam = nodeRadius * 2f;
        int   width    = Mathf.CeilToInt(gridSize.x / nodeDiam);
        int   height   = Mathf.CeilToInt(gridSize.y / nodeDiam);

        PathfindingGrid grid = new PathfindingGrid();

        grid.width    = width;
        grid.height   = height;
        grid.grid     = new Unity.Collections.NativeArray <Node>(width * height, Unity.Collections.Allocator.Persistent);
        grid.nodeSize = nodeDiam;

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                var rayOrigin = grid.GetNodePosition(x, y);
                rayOrigin.y = gridHeight;
                var rayDir = Vector3.down;
                var ray    = new Ray(rayOrigin, rayDir);

                if (Physics.Raycast(ray, out RaycastHit hit, gridHeight))
                {
                    bool obstructed = CheckCube(hit.point, nodeRadius);
                    Node node       = new Node()
                    {
                        yPosition = hit.point.y,
                        walkable  = !obstructed && (walkable == (walkable | (1 << hit.collider.gameObject.layer)))
                    };

                    grid.grid[y * width + x] = node;
                }
    public void Start()
    {
        Vector3 position = transform.position;

        foreach (PathfindingLayer layer in layers)
        {
            PathfindingGrid grid = new PathfindingGrid(width, height, tileSize);

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    Vector2 origin = new Vector2(x * tileSize.x + position.x, (y - height) * tileSize.y + position.y);
                    if (Physics2D.OverlapArea(origin, origin + tileSize, layer.collisionMask) == null)
                    {
                        grid.MarkPassible(x, y);
                    }
                }
            }
            grid.BuildIndex();
            grids[layer.name] = grid;
        }

        if (grids.ContainsKey(PathingTypes.Walking) && grids.ContainsKey(PathingTypes.Flying))
        {
            coverOptions = CoverFinder.FindCover(grids[PathingTypes.Walking], grids[PathingTypes.Flying]);
        }
    }
        public void Size_IsTenByTen_Vec2()
        {
            Vec2            size = new Vec2(10, 10);
            PathfindingGrid grid = new PathfindingGrid(size);

            Assert.AreEqual(size, grid.Size);
        }
Exemple #7
0
    public PathfindingGraph()
    {
        grid              = PathfindingGrid.instance;
        marker            = grid.GetWaypointMarkerPrefab();
        numCellsAcross    = grid.GetCellsAcross();
        numClustersAcross = grid.GetClustersAcross();
        cellSize          = grid.GetCellSize();
        clusterSize       = grid.GetClusterSize();
        gridCapacity      = numCellsAcross * numCellsAcross;

        neighbourOffsetArray    = new NativeArray <int2>(8, Allocator.Persistent);
        neighbourOffsetArray[0] = new int2(-1, 0);
        neighbourOffsetArray[1] = new int2(+1, 0);
        neighbourOffsetArray[2] = new int2(0, -1);
        neighbourOffsetArray[3] = new int2(0, +1);
        neighbourOffsetArray[4] = new int2(-1, -1);
        neighbourOffsetArray[5] = new int2(+1, -1);
        neighbourOffsetArray[6] = new int2(-1, +1);
        neighbourOffsetArray[7] = new int2(+1, +1);

        CreateGraph();

        //for ( int i = 0; i < 500; i++ )
        //Object.Instantiate( marker , new Vector3( edgePathNodePositions[ i ].x * cellSize , 2 , edgePathNodePositions[ i ].y * cellSize ) , Quaternion.identity );
    }
        public void Neighbors_DefaultDirections_IncludeAllClockwiseDirectionsHardcoded()
        {
            Vec2            vec2            = new Vec2(5, 5);
            PathfindingGrid grid            = new PathfindingGrid(vec2);
            Vec2            tileCoordinates = new Vec2(3, 3);

            grid.Fill(position => new GridNode(Tiles.Stone, position, grid));

            List <GridNode> nodes = new List <GridNode>();

            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 2), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(3, 2), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 2), grid));

            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 3), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 3), grid));

            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 4), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(3, 4), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 4), grid));

            foreach (GridNode node in nodes)
            {
                Assert.Contains(node, grid.Neighbors(grid[tileCoordinates]));
            }
        }
        public void Height_IsTen_int()
        {
            int             dimension = 10;
            PathfindingGrid grid      = new PathfindingGrid(dimension, dimension);

            Assert.AreEqual(dimension, grid.Height);
        }
Exemple #10
0
 public void Init()
 {
     _grid = new PathfindingGrid(5, 5);
     _grid.Fill(position => new GridNode(Tiles.Stone, position, _grid));
     _grid.FillEdges((position, annotation) => new GridEdge(position, annotation));
     _grid.FillVertices((position) => new GridVertex(position));
 }
        public void Height_IsTen_Vec2()
        {
            Vec2            size = new Vec2(10, 10);
            PathfindingGrid grid = new PathfindingGrid(size);

            Assert.AreEqual(size.y, grid.Height);
        }
        public void FilteredNeighbors_Unconstrained_EightWayNeighbors()
        {
            Vec2            vec2            = new Vec2(5, 5);
            PathfindingGrid grid            = new PathfindingGrid(vec2);
            Vec2            tileCoordinates = new Vec2(3, 3);

            Motility        agentMotility = Motility.Unconstrained | Motility.EightWayNeighbors;
            List <GridNode> nodes         = new List <GridNode>();

            grid.Fill(position => new GridNode(Tiles.Stone, position, grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 2), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(3, 2), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 2), grid));

            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 3), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 3), grid));

            nodes.Add(new GridNode(Tiles.Stone, new Vec2(2, 4), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(3, 4), grid));
            nodes.Add(new GridNode(Tiles.Stone, new Vec2(4, 4), grid));

            IReadOnlyCollection <GridNode> filteredNeighbors = grid.FilteredNeighbors(grid[tileCoordinates], agentMotility);

            CollectionAssert.AreEquivalent(nodes.AsReadOnly(), filteredNeighbors);
        }
        public void Width_IsTen_int()
        {
            int             dimension = 10;
            PathfindingGrid grid      = new PathfindingGrid(dimension, dimension);

            Assert.AreEqual(dimension, grid.Width);
        }
        public void Width_IsTen_Vec2()
        {
            Vec2            size = new Vec2(10, 10);
            PathfindingGrid grid = new PathfindingGrid(size);

            Assert.AreEqual(size.x, grid.Width);
        }
Exemple #15
0
 public Pathfinding(int width, int height, int cellSize)
 {
     Instance = this;
     grid     = new PathfindingGrid <PathNode>(width, height, cellSize, Vector3.zero, (PathfindingGrid <PathNode> g, int x, int y) => new PathNode(g, x, y));
     SetNodesIsWalkable();
     SetNeighbourLists();
 }
        public void Size_Vec2sNegative_ThrowsArgumentOutOfRangeException()
        {
            Vec2            size = new Vec2(-1, 1);
            PathfindingGrid grid;

            Assert.Throws <ArgumentOutOfRangeException>(
                () => grid = new PathfindingGrid(size));
        }
        public void Size_IsTenByTen_int()
        {
            int             dimension = 10;
            PathfindingGrid grid      = new PathfindingGrid(dimension, dimension);
            Vec2            size      = new Vec2(10, 10);

            Assert.AreEqual(size, grid.Size);
        }
Exemple #18
0
 public PathNode(PathfindingGrid <PathNode> grid, int x, int y)
 {
     this.grid     = grid;
     this.x        = x;
     this.y        = y;
     isWalkable    = true;
     neighbourList = new List <PathNode>();
 }
        public void Size_IntIsNegative_ThrowsArgumentOutOfRangeException()
        {
            int             dimension = -1;
            PathfindingGrid grid;

            Assert.Throws <ArgumentOutOfRangeException>(
                () => grid = new PathfindingGrid(dimension, dimension));
        }
Exemple #20
0
 public void SetHorizontaLine(PathfindingGrid grid, int y, int flag, int clearance)
 {
     for (var i = 0; i < grid.Width; ++i)
     {
         grid.SetFlag(i, y, flag);
         grid.SetClearance(i, y, clearance);
     }
 }
Exemple #21
0
 public void SetVerticalLine(PathfindingGrid grid, int x, int flag, int clearance)
 {
     for (var i = 0; i < grid.Height; ++i)
     {
         grid.SetFlag(x, i, flag);
         grid.SetClearance(x, i, clearance);
     }
 }
Exemple #22
0
        public void PathIsDeterminedCorrectly(int x, int y, int x2, int y2)
        {
            var grid = new PathfindingGrid(GRID_SIZE, GRID_SIZE, Allocator.Persistent);

            SetArea(grid, GRID_SIZE, GRID_SIZE, 1, 1);
            var pathfound = ExecuteSearch(grid, x, y, x2, y2, 0, 1);

            Assert.IsTrue(pathfound);
        }
 void Awake()
 {
     if (instance)
     {
         Debug.LogWarning("Another instance of pathfinding grid already exists");
         return;
     }
     instance = this;
 }
        public void Neighbors_DefaultDirectionOverload_CountMatches()
        {
            Vec2            vec2            = new Vec2(5, 5);
            PathfindingGrid grid            = new PathfindingGrid(vec2);
            Vec2            tileCoordinates = new Vec2(3, 3);

            grid.Fill(position => new GridNode(Tiles.Stone, position, grid));

            Assert.AreEqual(8, grid.Neighbors(grid[tileCoordinates]).Count);
        }
Exemple #25
0
        public void PathIsDetermined_Correctly_WithHorizontalObstacle_InTheMiddle(int x, int y, int x2, int y2, bool isPathValid)
        {
            var grid = new PathfindingGrid(GRID_SIZE, GRID_SIZE, Allocator.Persistent);

            SetArea(grid, GRID_SIZE, GRID_SIZE, 1, 1);
            SetHorizontaLine(grid, GRID_SIZE / 2, 0, 1);
            var pathfound = ExecuteSearch(grid, x, y, x2, y2, 0, 1);

            Assert.AreEqual(isPathValid, pathfound);
        }
        public void IndexingVec2_SetNode_IsType()
        {
            int             int10 = 10;
            PathfindingGrid grid  = new PathfindingGrid(new Vec2(int10, int10));

            grid.Fill(position => new GridNode(Tiles.Stone, position, grid));
            int int1 = 1;

            Assert.AreEqual(typeof(GridNode), grid[int1, int1].GetType());
        }
Exemple #27
0
 private void ReplaceWith(PathfindingNode with, PathfindingGrid grid)
 {
     for (float y = area.yMin + cellSize.y * 0.5f; y < area.yMax; y += cellSize.y)
     {
         for (float x = area.xMin + cellSize.x * 0.5f; x < area.xMax; x += cellSize.x)
         {
             grid.WriteCell(new Vector2(x, y), with);
         }
     }
 }
Exemple #28
0
        public void GridIsPassableMethodWorksCorrectly(int x, int y, LandscapeType cellType, int cellClearance,
                                                       LandscapeType unitType, int unitySize, bool result)
        {
            var grid = new PathfindingGrid(20, 20, Allocator.Persistent);

            grid.SetFlag(x, y, (long)cellType);
            grid.SetClearance(x, y, cellClearance);
            Assert.AreEqual(result, grid.IsPassable(x, y, (long)unitType, unitySize));
            grid.Dispose();
        }
        public void IndexingInt_ReturnsGridNode_IsType()
        {
            Vec2            vec2 = new Vec2(10, 10);
            PathfindingGrid grid = new PathfindingGrid(vec2);

            grid.Fill(position => new GridNode(Tiles.Stone, position, grid));
            int int1 = 1;

            Assert.AreEqual(typeof(GridNode), grid[1, 1].GetType());
        }
Exemple #30
0
    public List <Vector3> FindPath(Transform target)
    {
        pathfindingGrid = ResetPathfindingGrid();

        gridGraph = SetDestinationInGridGraph(target);

        path = algorithm.FindPath(gridGraph, pathfindingGrid.GetIsGridPointObstructedArray());

        return(IntToPositionList(path));
    }