Exemplo n.º 1
0
        public void TestLoadGridFailsIfFileNotExists()
        {
            var want        = SetupZoneMap.SetupMediumGrid();
            var gridFactory = GridFactorySetup.SetupGridFactory();

            Assert.Throws <FileNotFoundException>(() => gridFactory.LoadGrid(Path.GetRandomFileName()));
        }
Exemplo n.º 2
0
        public void NavigatorFindsPathToWaypoint()
        {
            var startPos = new Vector3(-2, 0, -2);
            var endPos   = new Vector3(2, 0, 2);
            // Vector3[] want =
            // {
            //     // startPos, -- Don't think I actually want to have the start if I can swing it, so that warping is easier.
            //     new Vector3(1f, 0f, 1f),
            //     endPos
            // };

            var zone = new Zone("tests");
            var grid = SetupZoneMap.SetupMediumGrid();

            zone.Map = grid;

            var traveler = new Traveler {
                CurrentZone = zone
            };

            traveler.Walker.CurrentPosition = startPos;
            traveler.PathMaker = new GridPathMaker {
                ZoneMap = zone.Map
            };


            traveler.PathfindAndWalkToFarAwayWorldMapPosition(endPos);


            Assert.Equal(endPos, traveler.Walker.CurrentPosition);
        }
Exemplo n.º 3
0
        public void FavorsKnownNodesOverUnknownNodes()
        {
            /*
             * Visualization of the MapGrid.
             * p = path
             * x = obstacle
-------------------------------
|     |  e  |     |     |     |
-------------------------------
|  ?  |  ?  |  p  |     |     |
-------------------------------
|  ?  |  ?  |  p  |     |     |
-------------------------------
|  ?  |  ?  |  p  |     |     |
-------------------------------
|  s  |  p  |     |     |     |
-------------------------------
             */

            var grid = SetupZoneMap.SetupMediumGrid();
            grid.AddKnownNode(new Vector3(0, 0, 0));
            grid.AddKnownNode(new Vector3(0, 0, 1));
            grid.AddKnownNode(new Vector3(0, 0, 2));
            grid.AddKnownNode(new Vector3(0, 0, -1));
            grid.AddKnownNode(new Vector3(0, 0, -2));


            grid.AddKnownNode(new Vector3(-1, 0, -2));
            grid.AddKnownNode(new Vector3(-1, 0, 2));

            // string example = grid.Print();

            var startPos = new Vector3(-2, 0, -2);
            var endPos = new Vector3(-1, 0, 2);
            Vector3[] want =
            {
                // startPos,

                new Vector3(-1, 0, -2),
                new Vector3(0, 0, -1),
                Vector3.Zero,
                new Vector3(0, 0, 1),
                endPos
            };

            // act 
            var got = Pathfinding.FindWaypoints(grid, startPos, endPos);

            // assert
            Assert.Equal(want, got);
        }
Exemplo n.º 4
0
        public void TestFindPathAvoidsObstacles()
        {
            /*
             * Visualization of the MapGrid.
             * p = path
             * x = obstacle
-------------------------------
|     |  p  |     |     |     |
-------------------------------
|  e  |     |     |     |     |
-------------------------------
|  x  |  x  |  x  |  p  |     |
-------------------------------
|     |  p  |  p  |     |     |
-------------------------------
|  s  |     |     |     |     |
-------------------------------
             */

            var grid = SetupZoneMap.SetupMediumGrid();

            grid.AddUnWalkableNode(Vector3.Zero);
            grid.AddUnWalkableNode(new Vector3(-1, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-2, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-3, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-4, 0, 0));


            string example = grid.Print();

            var startPos = new Vector3(-2, 0, -2);
            var endPos = new Vector3(-2, 0, 1);
            Vector3[] want =
            {
                // startPos,
                new Vector3(-1, 0, -1),
                new Vector3(0, 0, -1),
                new Vector3(1, 0, 0),
                new Vector3(0, 0, 1),
                new Vector3(-1, 0, 2),
                endPos
            };

            // act 
            var got = Pathfinding.FindWaypoints(grid, startPos, endPos);

            // assert
            Assert.Equal(want, got);
        }
Exemplo n.º 5
0
        public void LoadGridOrCreateNewCreatesNewIfItDoesntExist()
        {
            var want        = SetupZoneMap.SetupMediumGrid();
            var gridFactory = GridFactorySetup.SetupGridFactory();

            gridFactory.DefaultGridSize = new Vector2(5f, 5f);

            string mapName = Path.GetRandomFileName();

            want.MapName = mapName;

            var got = gridFactory.LoadGridOrCreateNew(mapName);

            SetupZoneMap.AssertGridMapEqual(want.MapGrid, got.MapGrid);
            Assert.Equal(want.MapName, got.MapName);
        }
Exemplo n.º 6
0
        public void TestPrintPathShowsAllWithLegend()
        {
            const string want = @"
Visualization of the path
s = start
e = end
w = waypoint
x = obstacle
-------------------------------
|     |     |     |     |     |
-------------------------------
|  e  |  w  |     |     |     |
-------------------------------
|  x  |  x  |  x  |  w  |     |
-------------------------------
|     |  w  |  w  |     |     |
-------------------------------
|  s  |     |     |     |     |
-------------------------------
";

            var grid = SetupZoneMap.SetupMediumGrid();

            grid.AddUnWalkableNode(Vector3.Zero);
            grid.AddUnWalkableNode(new Vector3(-1, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-2, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-3, 0, 0));
            grid.AddUnWalkableNode(new Vector3(-4, 0, 0));


            var path = new[]
            {
                new Vector3(-2, 0, -2),
                new Vector3(-1, 0, -1),
                new Vector3(0, 0, -1),
                new Vector3(1, 0, 0),
                new Vector3(-1, 0, 1),
                new Vector3(-2, 0, 1)
            };

            // act
            string got = grid.PrintPath(path);


            // assert
            Assert.Equal(want, got);
        }
Exemplo n.º 7
0
        public void TestPrintMediumGrid()
        {
            const string want = @"
-------------------------------
|     |     |     |     |     |
-------------------------------
|     |     |     |     |     |
-------------------------------
|     |     |     |     |     |
-------------------------------
|     |     |     |     |     |
-------------------------------
|     |     |     |     |     |
-------------------------------
";

            var    grid = SetupZoneMap.SetupMediumGrid();
            string got  = grid.Print();

            Assert.Equal(want, got);
        }
Exemplo n.º 8
0
        public void TestFindPath()
        {
            /*
             * Visualization of the MapGrid.
             * p = path
             * --------------------------
             * |    |    |    | end|    |
             * --------------------------
             * |    |    |    |  p |    |
             * --------------------------
             * |    |    |0,0 |    |    |
             * --------------------------
             * |    |  p |    |    |    |
             * --------------------------
             * |strt|    |    |    |    |
             * --------------------------
             */

            // arrange
            var grid = SetupZoneMap.SetupMediumGrid();

            var startPos = new Vector3(-2, 0, -2);
            var endPos = new Vector3(1, 0, 2);
            Vector3[] want =
            {
                // startPos,
                new Vector3(-1, 0, -1),
                // new Vector3(-1, 0, 0),
                Vector3.Zero,
                new Vector3(1, 0, 1),
                endPos
            };

            // act 
            var got = Pathfinding.FindWaypoints(grid, startPos, endPos);

            // assert
            Assert.Equal(want, got);
        }
Exemplo n.º 9
0
        public void MappingThreadOpens()
        {
            const string expectedUnknowns = @"
-------------------------------
|  ?  |  ?  |  ?  |  ?  |     |
-------------------------------
|  ?  |  ?  |  ?  |     |  ?  |
-------------------------------
|  ?  |  ?  |     |  ?  |  ?  |
-------------------------------
|  ?  |     |  ?  |  ?  |  ?  |
-------------------------------
|     |  ?  |  ?  |  ?  |  ?  |
-------------------------------
";
            // start the watcher and map
            var zone = new Zone("tests");
            var grid = SetupZoneMap.SetupMediumGrid();

            zone.Map = grid;
            var stubPersister = new StubPersister();
            var actor         = new KnownNodeActor(stubPersister, grid);

            // Walk a Path
            var startPos = new Vector3(-2, 0, -2);
            var endPos   = new Vector3(2, 0, 2);

            Vector3[] path =
            {
                startPos,
                new Vector3(-1f, 0f, -1f),
                new Vector3(0),
                new Vector3(1f,  0f,  1f),
                endPos
            };


            var traveler = new Traveler();
            // Watcher must be watching traveler before it gets it's current position otherwise it won't record the starting
            // position
            var watcher = new Watcher(traveler, actor);

            traveler.CurrentZone = zone;
            traveler.Position    = new Vector3(-2, 0, -2);
            traveler.PathMaker   = new GridPathMaker {
                ZoneMap = zone.Map
            };

            // Walked path
            traveler.PathfindAndWalkToFarAwayWorldMapPosition(endPos);
            var got = traveler.Walker.PositionHistory.ToArray();

            AssertVectorArrayEqual(path, got);

            // Visual Representation
            string actualUnknonws = grid.PrintKnown();

            Assert.Equal(expectedUnknowns, actualUnknonws);

            // Unknown Nodes correct
            var expectedGrid = SetupZoneMap.SetupMediumGrid();

            foreach (var position in path)
            {
                expectedGrid.AddKnownNode(position);
            }

            Assert.Equal(expectedGrid.UnknownNodes.Count, grid.UnknownNodes.Count);
            Assert.Equal(expectedGrid.UnknownNodes, grid.UnknownNodes);
        }