using System.Diagnostics; using RailEnterprise.Core.Common; using RailEnterprise.Core.Data; using RailEnterprise.Core.Map; using RailEnterprise.Tests.Harness; namespace RailEnterprise.Tests.Map; public static class MapTests { [Test(Description = "Accettazione F1: mappa da 128 km generata in meno di 3 s", Slow = true, Isolated = true)] public static void Map128GeneratesUnderThreeSeconds() { TestWorlds.Map(1, 64); // riscaldamento JIT var sw = Stopwatch.StartNew(); var map = TestWorlds.Map(3, 128); sw.Stop(); Assert.Equal(1280, map.Terrain.Width); Assert.True(sw.ElapsedMilliseconds < 3000, $"generazione in {sw.ElapsedMilliseconds} ms"); } [Test] public static void SameSeedSameMapDifferentSeedDifferentMap() { var a = TestWorlds.Map(5, 64); var b = TestWorlds.Map(5, 64); var c = TestWorlds.Map(6, 64); Assert.Equal(a.Terrain.ComputeHash(), b.Terrain.ComputeHash()); Assert.SequenceEqual(a.Towns.Select(t => t.Name), b.Towns.Select(t => t.Name)); Assert.NotEqual(a.Terrain.ComputeHash(), c.Terrain.ComputeHash()); } [Test(Description = "Distanze della guida: città ≥ 6 km, industrie entro i vincoli di collocazione")] public static void PlacementDistancesAreRespected() { var data = TestData.Data; foreach (ulong seed in new ulong[] { 11, 12 }) { var map = TestWorlds.Map(seed, 128); double cs = map.Terrain.CellSizeM; int expectedTowns = data.Balance.Map.Towns.CountBySize["128"]; Assert.True(map.Towns.Count >= expectedTowns * 0.8, $"città {map.Towns.Count} su {expectedTowns}"); for (int i = 0; i < map.Towns.Count; i++) { for (int j = i + 1; j < map.Towns.Count; j++) { var a = map.Towns[i]; var b = map.Towns[j]; double km = Math.Sqrt(Sq(a.CellX - b.CellX) + Sq(a.CellY - b.CellY)) * cs / 1000; Assert.True(km >= data.Balance.Map.Towns.MinDistanceKm - 1e-9, $"{a.Name}–{b.Name}: {km:F2} km"); } } foreach (var ind in map.Industries) { var def = data.Industries[ind.TypeIndex]; var p = map.Terrain.CellCenter(ind.CellX, ind.CellY); double nearest = map.Towns.Min(t => MapGenerator.TownEdgeDistanceKm(t, p, cs)); if (def.Placement.MinTownDistanceKm is { } mn) Assert.True(nearest >= mn, $"{ind.Name}: {nearest:F2} km < {mn}"); if (def.Placement.MaxTownDistanceKm is { } mx) Assert.True(nearest <= mx, $"{ind.Name}: {nearest:F2} km > {mx}"); if (def.Placement.MaxInputDistanceKm is { } maxIn) { foreach (var input in def.Recipe.Inputs) { bool near = map.Industries.Any(o => data.Industries[o.TypeIndex].Recipe.Outputs.Any(x => x.Good == input.Good) && Math.Sqrt(Sq(o.CellX - ind.CellX) + Sq(o.CellY - ind.CellY)) * cs / 1000 <= maxIn + 1e-9); Assert.True(near, $"{ind.Name}: nessun produttore di {input.Good} entro {maxIn} km"); } } if (def.Placement.Needs == PlacementNeed.Deposit) { Assert.True(ind.SiteIndex >= 0, $"{ind.Name} senza giacimento"); Assert.Equal(def.Placement.Resource, map.Sites[ind.SiteIndex].Kind); } var (x0, y0) = GeneratedMap.FootprintOrigin(ind.CellX, ind.CellY, ind.FootprintCells); for (int y = y0; y < y0 + ind.FootprintCells; y++) for (int x = x0; x < x0 + ind.FootprintCells; x++) Assert.Equal(CellType.Industry, map.Terrain.TypeAt(x, y)); } Assert.True(map.Hubs.Any(h => h.Kind == HubKind.Port), "almeno un porto"); Assert.True(map.Hubs.Any(h => h.Kind == HubKind.Border), "almeno un valico"); } } private static double Sq(double v) => v * v; [Test] public static void TerrainHasSeaRiversAndCover() { var map = TestWorlds.Map(21, 64); var t = map.Terrain; var counts = new int[11]; foreach (byte b in t.Types) counts[b]++; Assert.True(counts[(int)CellType.Sea] > 0, "mare presente"); Assert.True(counts[(int)CellType.Water] > 0, "fiumi o laghi presenti"); Assert.True(counts[(int)CellType.Forest] > 0, "boschi presenti"); Assert.True(counts[(int)CellType.Plain] > t.Types.Length / 10, "pianure abbondanti"); // il mare tocca il lato scelto ed è a quota zero bool seaOnEdge = false; for (int i = 0; i < t.Width; i++) { seaOnEdge |= t.TypeAt(i, 0) == CellType.Sea || t.TypeAt(i, t.Height - 1) == CellType.Sea || t.TypeAt(0, i) == CellType.Sea || t.TypeAt(t.Width - 1, i) == CellType.Sea; } Assert.True(seaOnEdge); for (int i = 0; i < t.Types.Length; i++) { if (t.Types[i] == (byte)CellType.Sea) Assert.Equal((ushort)0, t.Heights[i]); } Assert.True(t.RiverWidth.Any(w => w >= 2), "almeno un fiume largo più di una cella"); } [Test] public static void SamplerInterpolatesBetweenCellCenters() { var t = new Terrain(4, 4, 100, 64); for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++) t.Heights[t.Index(x, y)] = Terrain.ToDecimeters(x * 10); // pendenza costante del 10 % var s = new TerrainSampler(t); Assert.Approx(0.0, s.HeightAt(50, 150), 1e-9); Assert.Approx(5.0, s.HeightAt(100, 150), 1e-9); Assert.Approx(15.0, s.HeightAt(200, 250), 1e-9); Assert.Approx(0.1, s.Slope(150, 150), 1e-9); Assert.Approx(DMath.Pi, Math.Abs(s.Aspect(150, 150)), 1e-9, "discesa verso ovest"); } }