public void FOVCurrentHash() { var map = BoxResMap(50, 50); var fovMap = new LambdaTranslationMap <double, bool>(map, d => d >= 1.0 ? false : true); var fov = new FOV(fovMap); fov.Calculate(20, 20, 10); // Inefficient copy but fine for testing HashSet <Coord> currentFov = new HashSet <Coord>(fov.CurrentFOV); for (int x = 0; x < map.Width; x++) { for (int y = 0; y < map.Height; y++) { if (fov[x, y] > 0.0) { Assert.AreEqual(true, currentFov.Contains((x, y))); } else { Assert.AreEqual(false, currentFov.Contains((x, y))); } } } }
public static TimeSpan TimeForLambdaTranslationMap1ParamAccess(int mapWidth, int mapHeight, int iterations) { var s = new Stopwatch(); var map = new ArrayMap <int>(mapWidth, mapHeight); foreach (var pos in map.Positions()) { map[pos] = pos.ToIndex(mapWidth); } var lambdaTranslationMap = new LambdaTranslationMap <int, double>(map, i => i / 100.0); // For caching double val; foreach (var pos in lambdaTranslationMap.Positions()) { val = lambdaTranslationMap[pos]; } s.Start(); for (int i = 0; i < iterations; i++) { foreach (var pos in lambdaTranslationMap.Positions()) { val = lambdaTranslationMap[pos]; } } s.Stop(); return(s.Elapsed); }
public void LambdaTranslationMapTest() { ArrayMap <bool> map = new ArrayMap <bool>(10, 10); RectangleMapGenerator.Generate(map); var lambdaMap = new LambdaTranslationMap <bool, double>(map, b => b ? 1.0 : 0.0); checkMaps(map, lambdaMap); }
public void LambdaTranslationMapTest() { ArrayMap <bool> map = new ArrayMap <bool>(10, 10); QuickGenerators.GenerateRectangleMap(map); var lambdaMap = new LambdaTranslationMap <bool, double>(map, b => b ? 1.0 : 0.0); checkMaps(map, lambdaMap); // Check other constructor. Intentaionally "misusing" the position parameter, to make sure we ensure the position // parameter is correct without complicating our test case lambdaMap = new LambdaTranslationMap <bool, double>(map, (pos, b) => map[pos] ? 1.0 : 0.0); checkMaps(map, lambdaMap); }
public void EqualLargeMap() { var testResMap = TestResMap(17, 17); var testFOVMap = new LambdaTranslationMap <double, bool>(testResMap, d => d >= 1.0 ? false : true); testResMap[8, 8] = 0.0; // Make sure start is free var myFov = new FOV(testFOVMap); var myLighting = new SenseMap(testResMap); // Circle at 8, 8; radius 7 myLighting.AddSenseSource(new SenseSource(SourceType.SHADOW, (8, 8), 7, Radius.CIRCLE)); myFov.Calculate(8, 8, 7, Radius.CIRCLE); myLighting.Calculate(); Console.WriteLine("LOS: "); for (int x = 0; x < testResMap.Width; x++) { for (int y = 0; y < testResMap.Height; y++) { Console.Write($"{myFov[x, y].ToString("N2")}\t"); } Console.WriteLine(); } Console.WriteLine("\nLighting:"); for (int x = 0; x < testResMap.Width; x++) { for (int y = 0; y < testResMap.Height; y++) { Console.Write($"{myLighting[x, y].ToString("N2")}\t"); } Console.WriteLine(); } for (int x = 0; x < testResMap.Width; x++) { for (int y = 0; y < testResMap.Height; y++) { System.Console.WriteLine($"We have ({x},{y}) fov {myFov[x, y]}, lighting {myLighting[(x, y)]}"); Assert.AreEqual(myFov[x, y], myLighting[x, y]); // Both got the same results } } }
public void CircleRadius() { var testFOVMap = EmptyFOVMap(17, 17); var testResMap = new LambdaTranslationMap <bool, double>(testFOVMap, b => 0.0); var myFov = new FOV(testFOVMap); var myLighting = new SenseMap(testResMap); // Circle at 8, 8; radius 7 myLighting.AddSenseSource(new SenseSource(SourceType.SHADOW, (8, 8), 7, Radius.CIRCLE)); myFov.Calculate(8, 8, 7, Radius.CIRCLE); myLighting.Calculate(); for (int x = 0; x < testResMap.Width; x++) { for (int y = 0; y < testResMap.Height; y++) { Console.Write(myLighting[x, y].ToString("0.00") + " "); Assert.AreEqual(myFov[x, y], myLighting[x, y]); // Both got the same results } Console.WriteLine(); } }
public void FOVNewlySeenUnseen() { var map = BoxResMap(50, 50); var fovMap = new LambdaTranslationMap <double, bool>(map, d => d >= 1.0 ? false : true); var fov = new FOV(fovMap); fov.Calculate(20, 20, 10, Radius.SQUARE); var prevFov = new HashSet <Coord>(fov.CurrentFOV); fov.Calculate(19, 19, 10, Radius.SQUARE); var curFov = new HashSet <Coord>(fov.CurrentFOV); var newlySeen = new HashSet <Coord>(fov.NewlySeen); var newlyUnseen = new HashSet <Coord>(fov.NewlyUnseen); foreach (var pos in prevFov) { if (!curFov.Contains(pos)) { Assert.AreEqual(true, newlyUnseen.Contains(pos)); } else { Assert.AreEqual(false, newlyUnseen.Contains(pos)); } } foreach (var pos in curFov) { if (!prevFov.Contains(pos)) { Assert.AreEqual(true, newlySeen.Contains(pos)); } else { Assert.AreEqual(false, newlySeen.Contains(pos)); } } }
public void ApplyTerrainOverlay() { var grMap = new ArrayMap <bool>(10, 10); QuickGenerators.GenerateRectangleMap(grMap); var translationMap = new LambdaTranslationMap <bool, IGameObject>(grMap, (pos, val) => val ? new GameObject(pos, 0, null, true, true, true) : new GameObject(pos, 0, null, true, false, false)); var map = new Map(grMap.Width, grMap.Height, 1, Distance.CHEBYSHEV); // Normally you shouldn't need tempMap, could just use translationMap directly. But we want ref equality comparison // capability for testing var tempMap = new ArrayMap <IGameObject>(grMap.Width, grMap.Height); tempMap.ApplyOverlay(translationMap); map.ApplyTerrainOverlay(tempMap); Assert.AreEqual(grMap.Width, map.Width); Assert.AreEqual(grMap.Height, map.Height); foreach (var pos in map.Positions()) { Assert.AreEqual(tempMap[pos], map.GetTerrain(pos)); } }
/// <summary> /// Sets all terrain on the map to the result of running the given translator on the value in <paramref name="overlay"/> at the /// corresponding position. Useful, for example, for applying the map view resulting from map generation to a Map as terrain. /// </summary> /// <typeparam name="T">Type of values exposed by map view to translate. Generally inferred by the compiler.</typeparam> /// <param name="overlay">Map view to translate.</param> /// <param name="translator">Function that translates values of the type that <paramref name="overlay"/> exposes to values of type IGameObject.</param> public void ApplyTerrainOverlay <T>(IMapView <T> overlay, Func <Coord, T, IGameObject> translator) { var terrainOverlay = new LambdaTranslationMap <T, IGameObject>(overlay, translator); ApplyTerrainOverlay(terrainOverlay); }