public void BenchmarkSerializeThenPersistZoneMap() { var want = SetupZoneMap.SetupBigGrid(); var serializer = new ZoneMapSerializer(); var persister = SetupPersister.SetupTestFileTextPersister(); string mapName = SetupPersister.GetCurrentMethodName(); persister.FileName = mapName; int time = Benchmark.Run("StreamReader.ReadToEnd", 5, () => { string json = serializer.Serialize(want); persister.Save(json); string got = persister.Load <string>(); persister.Delete(); var map = serializer.DeSerialize(got); }); int benchTime = 180; Assert.True(time < benchTime, "time per iteration was: " + time + "ms which is greater than expected time of: " + benchTime + "ms"); }
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); }
public void KnownNodeActorCanUpdateKnownNodes() { var map = SetupZoneMap.SetupSmallGrid(); string mapName = SetupPersister.GetCurrentMethodName(); var persister = new FilePersister(mapName); var actor = new KnownNodeActor(persister, map); var traveler = new Traveler(); var watcher = new Watcher(traveler, actor); var newPos = new Vector3(1, 0, 1); traveler.Position = newPos; var wantGrid = SetupZoneMap.SetupSmallGrid(); wantGrid.AddKnownNode(newPos); var want = wantGrid.UnknownNodes; var got = persister.Load <ZoneMap>(); Assert.Equal(want.Count, got.UnknownNodes.Count); for (var i = 0; i < want.Count; i++) { Assert.Equal(want[i], got.UnknownNodes[i]); } }
public void NavigatorDiscoversAllUnknownNodes() { const string want = @" ------------------- | | | | ------------------- | | | | ------------------- | | | | ------------------- "; var grid = SetupZoneMap.SetupSmallGrid(); var traveler = new Traveler(); traveler.CurrentZone = new Zone("test") { Map = grid }; traveler.Position = Vector3.One; traveler.DiscoverAllNodes(); Assert.Empty(traveler.CurrentZone.Map.UnknownNodes); string got = grid.PrintKnown(); Assert.Equal(want, got); }
public void TestPrintSmallGridWithObstacles() { const string want = @" ------------------- | x | x | | ------------------- | x | x | | ------------------- | | | | ------------------- "; // var MapGrid = SetupSmallGrid(); var grid = SetupZoneMap.SetupSmallGrid(); var pos1 = new Vector3(0, 0, 1); var pos2 = new Vector3(-1, 0, 1); var pos3 = new Vector3(-1, 0, 0); grid.AddUnWalkableNode(Vector3.Zero); grid.AddUnWalkableNode(pos1); grid.AddUnWalkableNode(pos2); grid.AddUnWalkableNode(pos3); string got = grid.Print(); SetupZoneMap.AssertPointNotWalkable(grid, pos1); SetupZoneMap.AssertPointNotWalkable(grid, pos2); SetupZoneMap.AssertPointNotWalkable(grid, pos3); Assert.Equal(want, got); }
public void TestLoadGridFailsIfFileNotExists() { var want = SetupZoneMap.SetupMediumGrid(); var gridFactory = GridFactorySetup.SetupGridFactory(); Assert.Throws <FileNotFoundException>(() => gridFactory.LoadGrid(Path.GetRandomFileName())); }
public void BenchmarkZoneMapFfxi() { string mapName = SetupPersister.GetCurrentMethodName(); var persister = new FilePersister(mapName); var want = SetupZoneMap.SetupFfxiSizeGrid(); // Path assumes to start from ./debug/ so we want to set it to the test fixtures dir. string grandParentDirectory = Directory.GetParent(persister.FilePath).FullName; string parentDirectory = Directory.GetParent(grandParentDirectory).FullName; persister.FilePath = Path.Combine(parentDirectory, "fixtures"); int time = Benchmark.Run("StreamReader.ReadToEnd", 5, () => { persister.Save(want); var got = persister.Load <ZoneMap>(); persister.Delete(); }); int benchTime = 1200; // Best time so far: 23548ms // New record!: 10214ms Assert.True(time < benchTime, "time per iteration was: " + time + "ms which is greater than expected time of: " + benchTime + "ms"); }
public void GetUnknownNodes() { var want = new List <Node> { new Node(new Vector3(-1, 0, -1)), new Node(new Vector3(-1, 0, 0)), new Node(new Vector3(-1, 0, 1)), new Node(new Vector3(0, 0, -1)), new Node(new Vector3(0, 0, 1)), new Node(new Vector3(1, 0, -1)), new Node(new Vector3(1, 0, 0)), new Node(new Vector3(1, 0, 1)) }; var grid = SetupZoneMap.SetupSmallGrid(); grid.MapGrid[1, 1].Unknown = false; var got = grid.UnknownNodes; Assert.Equal(want.Count, got.Count); for (var i = 0; i < want.Count; i++) { Assert.Equal(want[i].WorldPosition, got[i].WorldPosition); Assert.Equal(want[i].Unknown, got[i].Unknown); } }
public void NodeFromWorldPointCanGetANodeFromBigGrid() { var grid = SetupZoneMap.SetupBigGrid(); var want = grid.MapGrid[30, 5]; var got = grid.GetNodeFromWorldPoint(new Vector3(5, 0, -20)); Assert.Equal(want.WorldPosition, got.WorldPosition); }
public void NodeFromWorldPointCanGetANodeFromPositiveVector() { var grid = SetupZoneMap.SetupSmallGrid(); var want = grid.MapGrid[2, 2]; var got = grid.GetNodeFromWorldPoint(new Vector3(1, 0, 1)); Assert.Equal(want.WorldPosition, got.WorldPosition); }
public void TestSerializeZoneMapToJson() { var want = SetupZoneMap.SetupSmallGrid(); var serializer = new ZoneMapSerializer(); string json = serializer.Serialize(want); var got = serializer.DeSerialize(json); SetupZoneMap.AssertGridMapEqual(want.MapGrid, got.MapGrid); }
public void TestCanSetUnknownState() { const bool want = false; var grid = SetupZoneMap.SetupSmallGrid(); grid.MapGrid[0, 0].Unknown = want; bool got = grid.MapGrid[0, 0].Unknown; Assert.Equal(want, got); }
public void AddUnWalkableNodeUpdatesTheNode(float x, float y, int gridX, int gridY) { var grid = SetupZoneMap.SetupSmallGrid(); var position = new Vector3(x, 0, y); var want = new Node(position, false); grid.AddUnWalkableNode(position); var got = grid.MapGrid[gridX, gridY]; Assert.Equal(want.Walkable, got.Walkable); }
public void TestAddKnownNodes(float x, float y, int gridX, int gridY) { var grid = SetupZoneMap.SetupSmallGrid(); var position = new Vector3(x, 0, y); const bool want = false; grid.AddKnownNode(position); bool got = grid.MapGrid[gridX, gridY].Unknown; Assert.Equal(want, got); }
public void AddZoneBorder() { var want = Vector3.One; var zone = new Zone("tests"); zone.Map = SetupZoneMap.SetupSmallGrid(); zone.AddBoundary("A", want, "B", want * -1); var got = zone.Boundaries.Find(b => b.ToZone == "B").FromPosition; Assert.Equal(want, got); }
public void CreateGridCanGenerateGridFromCorner() { var want = new Node[1, 1]; want[0, 0] = new Node(Vector3.Zero); var zoneMap = ZoneMap.NewGridFromVector2(Vector2.One); var got = zoneMap.MapGrid; Assert.Equal(want.Length, got.Length); SetupZoneMap.AssertGridMapEqual(want, got); }
public void GetNeighboursCanGetNeighborsFromEdgeNode() { //Arrange var want = SetupZoneMap.GetNeighborsListForEdgeNode(); //Act var grid = ZoneMap.NewGridFromVector2(new Vector2(3f, 3f)); var got = grid.GetNeighbours(grid.MapGrid[2, 2]); //Assert SetupZoneMap.AssertListGridNodesEqual(want, got); }
public void CanCreateGridWithMultipleNodes() { // Arrange var want = SetupZoneMap.SetupThreeByThreeGrid(); // Act var grid = ZoneMap.NewGridFromVector2(new Vector2(3f, 3f)); var got = grid.MapGrid; // Assert Assert.Equal(want.Length, got.Length); SetupZoneMap.AssertGridMapEqual(want, got); }
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); }
public void TestCanSaveToFile() { var want = SetupZoneMap.SetupSmallGrid(); var mapName = SetupPersister.GetCurrentMethodName(); var persister = new FilePersister(mapName); persister.Save(want); var got = persister.Load <ZoneMap>(); persister.Delete(); SetupZoneMap.AssertGridMapEqual(want.MapGrid, got.MapGrid); }
public void TestGridFactoryCanGetGridFromFile() { var want = SetupZoneMap.SetupSmallGrid(); var gridFactory = GridFactorySetup.SetupGridFactory(); string mapName = SetupPersister.GetCurrentMethodName(); // Uncomment to make golden file if zoneMap changes. gridFactory.Persister.MapName = mapName; gridFactory.Persister.Save(want); var got = gridFactory.LoadGrid(mapName); SetupZoneMap.AssertGridMapEqual(want.MapGrid, got.MapGrid); }
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); }
public void CanCreateAZoneWithConstructor() { var name = "TestMap"; var map = SetupZoneMap.SetupSmallGrid(); var boundaries = new List <ZoneBoundary>(); var got = new Zone(name) { Map = map, Boundaries = boundaries }; Assert.Equal(name, got.Name); Assert.Equal(map, got.Map); Assert.Equal(boundaries, got.Boundaries); }
public void TestCanPrintUnknownGrid() { const string want = @" ------------------- | ? | ? | ? | ------------------- | ? | ? | ? | ------------------- | ? | ? | ? | ------------------- "; var grid = SetupZoneMap.SetupSmallGrid(); string got = grid.PrintKnown(); Assert.Equal(want, got); }
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); }
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); }
public void BenchmarkSerializeZoneMap() { var want = SetupZoneMap.SetupSmallGrid(); var serializer = new ZoneMapSerializer(); int time = Benchmark.Run("StreamReader.ReadToEnd", 5, () => { string json = serializer.Serialize(want); var got = serializer.DeSerialize(json); }); int benchTime = 1; Assert.True(time < benchTime, "time per iteration was: " + time + "ms which is greater than expected time of: " + benchTime + "ms"); }
public void LoadGridOrCreateNewLoadsGridIfExists() { var want = SetupZoneMap.SetupSmallGrid(); var gridFactory = GridFactorySetup.SetupGridFactory(); string mapName = SetupPersister.GetCurrentMethodName(); want.MapName = mapName; // Uncomment to make golden file if zoneMap changes. gridFactory.Persister.MapName = mapName; gridFactory.Persister.Save(want); var got = gridFactory.LoadGridOrCreateNew(mapName); SetupZoneMap.AssertGridMapEqual(want.MapGrid, got.MapGrid); Assert.Equal(want.MapName, got.MapName); }
public void TestPrintWithCoordsSmallGrid() { const string want = @" ------------------------- | -1,1 | 0,1 | 1,1 | ------------------------- | -1,0 | 0,0 | 1,0 | ------------------------- | -1,-1 | 0,-1 | 1,-1 | ------------------------- "; var grid = SetupZoneMap.SetupSmallGrid(); string got = grid.PrintWithCoords(); Assert.Equal(want, got); }
public void TestPrintSmallGrid() { const string want = @" ------------------- | | | | ------------------- | | | | ------------------- | | | | ------------------- "; var grid = SetupZoneMap.SetupSmallGrid(); string got = grid.Print(); Assert.Equal(want, got); }