Exemplo n.º 1
0
        private static void CallbackExample()
        {
            Console.WriteLine(nameof(CallbackExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager = new PathfindaxManager();
            var factory           = new DefinitionNodeGridFactory();
            var nodeGrid          = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var nodeNetwork       = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder        = pathfindaxManager.CreateAstarPathfinder(nodeNetwork, new ManhattanDistance());

            //Request a path.
            var pathRequest = pathfinder.RequestPath(nodeNetwork.NodeGrid.ToIndex(0, 0), nodeNetwork.NodeGrid.ToIndex(2, 0));

            Console.WriteLine($"Solving path from {pathRequest.PathStart} to {pathRequest.PathEnd}...");

            //Add a callback to the request that will be called then the pathfinder is done.
            var callbackCalled = false;

            pathRequest.AddCallback(request =>
            {
                Console.WriteLine($"Solved path! {request.CompletedPath}");
                callbackCalled = true;
            });

            //Wait till callback is called.
            while (!callbackCalled)
            {
            }
            Console.WriteLine($"{nameof(CallbackExample)} completed. Press any key to continue with the next example.");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static object[] AstarAlgorithmVisualizationTestCase(int width, int height, Vector2 scale, Vector2 position, Vector2 start, Vector2 target)
        {
            var factory         = new DefinitionNodeGridFactory();
            var definitionNodes = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, width, height);

            return(new object[] { new DefinitionNodeGrid(definitionNodes, scale, position), start, target });
        }
Exemplo n.º 3
0
        private static void PollingExample()
        {
            Console.WriteLine(nameof(PollingExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager = new PathfindaxManager();
            var factory           = new DefinitionNodeGridFactory();
            var nodeGrid          = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var nodeNetwork       = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder        = pathfindaxManager.CreateAstarPathfinder(nodeNetwork, new ManhattanDistance());

            //Request a path.
            var pathRequest = pathfinder.RequestPath(nodeNetwork.NodeGrid.ToIndex(0, 0), nodeNetwork.NodeGrid.ToIndex(2, 0));

            Console.WriteLine($"Solving path from {pathRequest.PathStart} to {pathRequest.PathEnd}...");

            //Poll the status to check when the pathfinder is finished.
            while (pathRequest.Status == PathRequestStatus.Solving)
            {
            }
            switch (pathRequest.Status)
            {
            case PathRequestStatus.Solved:
                Console.WriteLine($"Solved path! {pathRequest.CompletedPath}");
                break;

            case PathRequestStatus.NoPathFound:
                Console.WriteLine("Could not find a path");
                break;
            }
            Console.WriteLine($"{nameof(PollingExample)} completed. Press any key to continue with the next example.");
            Console.ReadKey();
        }
Exemplo n.º 4
0
        public void GeneratePreFilledArray_CrossCorners(int width, int height, Point2[] blockedNodes)
        {
            var factory       = new DefinitionNodeGridFactory();
            var collisionMask = new NodeGridCollisionMask(PathfindaxCollisionCategory.Cat1, width, height);

            foreach (var blockedNode in blockedNodes)
            {
                collisionMask.Layers[0].CollisionDirections[blockedNode.X, blockedNode.Y] = CollisionDirection.Solid;
            }
            var nodeGrid = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, collisionMask, true);

            Assert.AreEqual(width, nodeGrid.Width);
            Assert.AreEqual(height, nodeGrid.Height);

            foreach (var point in blockedNodes)
            {
                var index = nodeGrid.ToIndex(point.X, point.Y);
                foreach (var connection in nodeGrid.Array[index].Connections)
                {
                    Assert.AreEqual(PathfindaxCollisionCategory.Cat1, connection.CollisionCategory);
                    var connectionToThis = nodeGrid.Array[connection.To].Connections.First(x => x.To == index);
                    Assert.AreEqual(PathfindaxCollisionCategory.Cat1, connectionToThis.CollisionCategory);
                }
            }
        }
Exemplo n.º 5
0
        private static object[] GenerateNodeVisualizationTestCase(int width, int height, Vector2 scale, Vector2 position)
        {
            var factory         = new DefinitionNodeGridFactory();
            var definitionNodes = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, width, height).Array;
            var transform       = new Transformer(scale, position);

            return(new object[] { definitionNodes, transform });
        }
Exemplo n.º 6
0
        private static object[] GenerateWaypointNodeVisualizationTestCase(int width, int height, Vector2 scale, Vector2 position, int[] path, ColorRgba startColor, ColorRgba endColor, ColorRgba nodeColor, ColorRgba lineColor)
        {
            var factory         = new DefinitionNodeGridFactory();
            var definitionNodes = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, width, height).Array;
            var transform       = new Transformer(scale, position);

            return(new object[] { transform, path.Select(i => definitionNodes[i].Position).ToArray(), startColor, endColor, nodeColor, lineColor });
        }
Exemplo n.º 7
0
        public void Setup()
        {
            var factory  = new DefinitionNodeGridFactory();
            var nodeGrid = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 320, 200);

            _definitionNodeGrid = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            _dijkstraNodeGrid   = new DijkstraNodeGrid(_definitionNodeGrid, 1);
            _algorithm          = new DijkstraAlgorithm(_definitionNodeGrid.NodeCount);
            _pathRequest        = PathRequest.Create(Substitute.For <IPathfinder <NodePath> >(),
                                                     _definitionNodeGrid.NodeGrid.ToIndex(0, 0), _definitionNodeGrid.NodeGrid.ToIndex(319, 199));
            _nodeNetwork = _dijkstraNodeGrid.GetCollisionLayerNetwork(_pathRequest.CollisionCategory);
        }
Exemplo n.º 8
0
        public void Setup()
        {
            var factory  = new DefinitionNodeGridFactory();
            var nodeGrid = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 320, 200);

            _definitionNodeGrid    = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            _astarNodeNetwork      = new AstarNodeNetwork(_definitionNodeGrid, new BrushfireClearanceGenerator(_definitionNodeGrid, 1));
            _algorithm             = new AStarAlgorithm(_definitionNodeGrid.NodeCount, new ManhattanDistance());
            _longPathRequest       = PathRequest.Create(Substitute.For <IPathfinder <IPath> >(), _definitionNodeGrid.NodeGrid.ToIndex(0, 0), _definitionNodeGrid.NodeGrid.ToIndex(319, 199));
            _shortPathRequest      = PathRequest.Create(Substitute.For <IPathfinder <IPath> >(), _definitionNodeGrid.NodeGrid.ToIndex(0, 0), _definitionNodeGrid.NodeGrid.ToIndex(20, 20));
            _veryShortPathRequest  = PathRequest.Create(Substitute.For <IPathfinder <IPath> >(), _definitionNodeGrid.NodeGrid.ToIndex(0, 0), _definitionNodeGrid.NodeGrid.ToIndex(1, 0));
            _zeroLengthPathRequest = PathRequest.Create(Substitute.For <IPathfinder <IPath> >(), _definitionNodeGrid.NodeGrid.ToIndex(0, 0), _definitionNodeGrid.NodeGrid.ToIndex(0, 0));
        }
Exemplo n.º 9
0
        public void GeneratePreFilledArray(int width, int height, Point2[] blockedNodes)
        {
            var factory       = new DefinitionNodeGridFactory();
            var collisionMask = new NodeGridCollisionMask(PathfindaxCollisionCategory.Cat1, width, height);

            foreach (var blockedNode in blockedNodes)
            {
                collisionMask.Layers[0].CollisionDirections[blockedNode.X, blockedNode.Y] = CollisionDirection.Solid;
            }
            var nodeGrid = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, collisionMask);

            Assert.AreEqual(width, nodeGrid.Width);
            Assert.AreEqual(height, nodeGrid.Height);

            foreach (var point in blockedNodes)
            {
                var index = nodeGrid.ToIndex(point.X, point.Y);
                foreach (var connection in nodeGrid.Array[index].Connections)
                {
                    Assert.AreEqual(PathfindaxCollisionCategory.Cat1, connection.CollisionCategory);
                    var connectionToThis = nodeGrid.Array[connection.To].Connections.First(x => x.To == index);
                    Assert.AreEqual(PathfindaxCollisionCategory.Cat1, connectionToThis.CollisionCategory);
                }

                var topNodeIndex    = GetIndex(nodeGrid, point.X, point.Y + 1);
                var bottomNodeIndex = GetIndex(nodeGrid, point.X, point.Y - 1);
                var leftNodeIndex   = GetIndex(nodeGrid, point.X - 1, point.Y);
                var rightNodeIndex  = GetIndex(nodeGrid, point.X + 1, point.Y);

                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, topNodeIndex, leftNodeIndex));
                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, topNodeIndex, rightNodeIndex));

                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, bottomNodeIndex, leftNodeIndex));
                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, bottomNodeIndex, rightNodeIndex));

                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, leftNodeIndex, topNodeIndex));
                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, leftNodeIndex, bottomNodeIndex));

                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, rightNodeIndex, topNodeIndex));
                Assert.IsTrue(ConnectionIsBlocked(nodeGrid, rightNodeIndex, bottomNodeIndex));
            }
        }
Exemplo n.º 10
0
        private static void AsyncExample()
        {
            Console.WriteLine(nameof(AsyncExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager  = new PathfindaxManager();
            var factory            = new DefinitionNodeGridFactory();
            var nodeGrid           = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var definitionNodeGrid = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder         = pathfindaxManager.CreateAstarPathfinder(definitionNodeGrid, new ManhattanDistance());

            var exampleGameObject = new ExampleAsyncGameObject(pathfinder);

            //Wait till callback is called.
            while (!exampleGameObject.CallBackCalled)
            {
                exampleGameObject.Update();
            }
            Console.ReadKey();
        }
Exemplo n.º 11
0
        public void CalculateGridNodeClearances_FilledNodeGrid_Passes()
        {
            const int width            = 4;
            const int height           = 4;
            var       factory          = new DefinitionNodeGridFactory();
            var       nodeGrid         = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, width, height);
            var       sourceNodeGrid   = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var       astarNodeNetwork = new AstarNodeNetwork(sourceNodeGrid, new BrushfireClearanceGenerator(sourceNodeGrid, 5));

            var sourceNodeNetworkCat1 = new Array2D <AstarNode>(astarNodeNetwork.GetCollisionLayerNetwork(PathfindaxCollisionCategory.Cat1), width, height);

            Assert.AreEqual(1, sourceNodeNetworkCat1[0, 0].Clearance);
            Assert.AreEqual(1, sourceNodeNetworkCat1[1, 0].Clearance);
            Assert.AreEqual(1, sourceNodeNetworkCat1[2, 0].Clearance);
            Assert.AreEqual(1, sourceNodeNetworkCat1[3, 0].Clearance);

            Assert.AreEqual(1, sourceNodeNetworkCat1[0, 1].Clearance);
            Assert.AreEqual(3, sourceNodeNetworkCat1[1, 1].Clearance);
            Assert.AreEqual(3, sourceNodeNetworkCat1[2, 1].Clearance);
            Assert.AreEqual(1, sourceNodeNetworkCat1[3, 1].Clearance);
        }
Exemplo n.º 12
0
        private static TestCaseData GeneratePathTestCase(int width, int height, Point2 start, Point2 end, Point2[] blockedNodes = null)
        {
            var factory       = new DefinitionNodeGridFactory();
            var collisionMask = new NodeGridCollisionMask(PathfindaxCollisionCategory.Cat1, width, height);

            if (blockedNodes != null)
            {
                foreach (var blockedNode in blockedNodes)
                {
                    collisionMask.Layers[0].CollisionDirections[blockedNode.X, blockedNode.Y] = CollisionDirection.Solid;
                }
            }
            var nodeGrid = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, collisionMask, true);
            var grid     = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));


            var description = blockedNodes != null ?
                              $"Path from {start} to {end} on a {width} by {height} grid with blocked nodes {string.Join(", ", blockedNodes)}" :
                              $"Path from {start} to {end} on a {width} by {height} grid";

            return(new TestCaseData(grid, start, end).SetName(description));
        }
Exemplo n.º 13
0
        public IDefinitionNodeGrid GenerateGrid2D()
        {
            if (_definitionNodeGrid == null)
            {
                var factory            = new DefinitionNodeGridFactory();
                var nodeGrid           = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 320, 200);
                var definitionNodeGrid = new DefinitionNodeGrid(nodeGrid, new Vector2(32, 32));

                /*definitionNodeGrid.PotentialArray[5, 4].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[5, 5].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[5, 6].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[5, 7].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[5, 8].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 *
                 * definitionNodeGrid.PotentialArray[5, 10].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[6, 10].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[7, 10].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[8, 10].CollisionCategory = PathfindaxCollisionCategory.Cat1;
                 * definitionNodeGrid.PotentialArray[9, 10].CollisionCategory = PathfindaxCollisionCategory.Cat1;*/
                _definitionNodeGrid = definitionNodeGrid;
            }
            return(_definitionNodeGrid);
        }
Exemplo n.º 14
0
 public DefinitionNodeGrid(GenerateNodeGridConnections generateNodeGridConnections, int width, int height, Vector2 scale, Vector2 offset = default(Vector2))
 {
     Transformer = new GridTransformer(new Point2(width, height), scale, offset);
     NodeGrid    = DefinitionNodeGridFactory.GeneratePreFilledArray(generateNodeGridConnections, width, height);
 }