using RailEnterprise.Core.Map;
namespace RailEnterprise.Tests.Rail;
/// Terreni costruiti a mano per i test del pianificatore (celle da 100 m).
public static class SyntheticTerrain
{
public static Terrain Build(int width, int height, Func heightAtMeters, Func? type = null)
{
var t = new Terrain(width, height, 100, 64);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
double hx = (x + 0.5) * 100, hy = (y + 0.5) * 100;
t.Heights[t.Index(x, y)] = Terrain.ToDecimeters(heightAtMeters(hx, hy));
t.Types[t.Index(x, y)] = (byte)(type?.Invoke(x, y) ?? CellType.Plain);
}
}
return t;
}
public static Terrain Flat(int size = 60, double h = 100) => Build(size, size, (_, _) => h);
/// Collina a cupola centrata in (cx, cy) metri.
public static Func Hill(double cx, double cy, double radiusM, double heightM, double baseM = 100) =>
(x, y) =>
{
double r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy)) / radiusM;
return baseM + (r >= 1 ? 0 : heightM * (1 - r * r) * (1 - r * r));
};
/// Pianura con un fiume nord-sud largo «widthCells» celle intorno a x = riverX metri.
public static Terrain River(int size, double riverXm, int widthCells, double baseM = 100, double waterM = 96)
{
int x0 = (int)(riverXm / 100) - widthCells / 2;
var t = Build(size, size, (x, _) => (int)(x / 100) >= x0 && (int)(x / 100) < x0 + widthCells ? waterM : baseM,
(x, _) => x >= x0 && x < x0 + widthCells ? CellType.Water : CellType.Plain);
for (int y = 0; y < size; y++)
for (int x = x0; x < x0 + widthCells; x++)
t.RiverWidth[t.Index(x, y)] = (byte)Math.Min(4, widthCells);
return t;
}
}