Files
Rail_Enterprise/client/RailEnterprise.Client/scripts/Map/TerrainLayer.cs
T
Alby96andClaude Opus 5.5 c1a0049bcf
CI / Build e test (pull_request) Successful in 1m20s
CI / Immagine Docker (pull_request) Successful in 37s
Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*,
variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per
allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos:
CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in
server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly.
Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e
aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:14:54 +01:00

203 lines
9.0 KiB
C#

using Godot;
using RailEnterprise.Client.App;
using RailEnterprise.Core.Map;
using RailEnterprise.Protocol.Client;
namespace RailEnterprise.Client.Map;
/// <summary>
/// Terreno a chunk (immagine 01): colori per tipo di cella e fascia altimetrica, ombreggiatura
/// con luce da nord-ovest (ombre verso sud-est), sponde chiare lungo l'acqua, campi a strisce;
/// curve di livello ogni 20 m su una texture separata visibile oltre lo zoom 1.
/// </summary>
public partial class TerrainLayer : Node2D
{
private const float ContourStepM = 20f;
private readonly Dictionary<(int, int), (Sprite2D Base, Sprite2D Contours)> _sprites = [];
private readonly Queue<(int, int)> _dirty = new();
private readonly HashSet<(int, int)> _queued = [];
private ClientWorld? _world;
public void Bind(ClientWorld world)
{
_world = world;
foreach (var (b, c) in _sprites.Values)
{
b.QueueFree();
c.QueueFree();
}
_sprites.Clear();
_dirty.Clear();
_queued.Clear();
}
/// <summary>Segna un chunk (e i vicini, per la continuità dell'ombreggiatura) da ridisegnare.</summary>
public void MarkDirty(int cx, int cy)
{
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
var key = (cx + dx, cy + dy);
if (_world is null || !_world.Chunks.Contains(key) || !_queued.Add(key)) continue;
_dirty.Enqueue(key);
}
}
}
public void SetZoom(float zoom)
{
bool contours = zoom >= MapScale.ContourMinZoom;
foreach (var (_, c) in _sprites.Values) c.Visible = contours;
}
public override void _Process(double delta)
{
// pochi chunk per fotogramma: l'ingresso in partita resta fluido
for (int i = 0; i < 6 && _dirty.Count > 0; i++)
{
var key = _dirty.Dequeue();
_queued.Remove(key);
Build(key.Item1, key.Item2);
}
}
private void Build(int cx, int cy)
{
var w = _world;
if (w?.Terrain is null || w.Sampler is null) return;
var t = w.Terrain;
int ppc = t.Width <= 700 ? 4 : t.Width <= 1300 ? 2 : 1;
int x0 = cx * t.ChunkCells, y0 = cy * t.ChunkCells;
int cellsW = Math.Min(t.ChunkCells, t.Width - x0), cellsH = Math.Min(t.ChunkCells, t.Height - y0);
int pw = cellsW * ppc, ph = cellsH * ppc;
var pixels = new byte[pw * ph * 4];
var contour = new byte[pw * ph * 4];
double cs = t.CellSizeM, px = cs / ppc;
// per cella (con un bordo): ombreggiatura dalle differenze centrali e indicatore d'acqua
int gw = cellsW + 3, gh = cellsH + 3;
var shade = new float[gw * gh];
var water = new float[gw * gh];
for (int gy = 0; gy < gh; gy++)
{
for (int gx = 0; gx < gw; gx++)
{
int x = Math.Clamp(x0 + gx - 1, 0, t.Width - 1), y = Math.Clamp(y0 + gy - 1, 0, t.Height - 1);
double hx = (H(t, x + 1, y) - H(t, x - 1, y)) / (2 * cs);
double hy = (H(t, x, y + 1) - H(t, x, y - 1)) / (2 * cs);
// luce da nord-ovest: i pendii rivolti a sud-est si scuriscono
shade[gy * gw + gx] = (float)Math.Clamp(1.0 - (hx + hy) * 1.8, 0.80, 1.12);
water[gy * gw + gx] = IsWater((CellType)t.Types[y * t.Width + x]) ? 1f : 0f;
}
}
var corner = new Color[4];
for (int y = 0; y < ph; y++)
{
for (int x = 0; x < pw; x++)
{
// posizione in celle rispetto ai centri: interpolazione bilineare tra quattro celle
double fx = (x + 0.5) / ppc - 0.5, fy = (y + 0.5) / ppc - 0.5;
int ix = (int)Math.Floor(fx), iy = (int)Math.Floor(fy);
float tx = (float)(fx - ix), ty = (float)(fy - iy);
int g00 = (iy + 1) * gw + ix + 1;
float wv = Bilinear(water[g00], water[g00 + 1], water[g00 + gw], water[g00 + gw + 1], tx, ty);
float sh = Bilinear(shade[g00], shade[g00 + 1], shade[g00 + gw], shade[g00 + gw + 1], tx, ty);
double h = w.Sampler.HeightAt(x0 * cs + (x + 0.5) * px, y0 * cs + (y + 0.5) * px);
for (int k = 0; k < 4; k++)
{
int cxCell = Math.Clamp(x0 + ix + (k & 1), 0, t.Width - 1), cyCell = Math.Clamp(y0 + iy + (k >> 1), 0, t.Height - 1);
corner[k] = BaseColor((CellType)t.Types[cyCell * t.Width + cxCell], h, cxCell, cyCell, x, y, ppc);
}
var c = corner[0].Lerp(corner[1], tx).Lerp(corner[2].Lerp(corner[3], tx), ty);
if (wv < 0.5f) c = new Color(c.R * sh, c.G * sh, c.B * sh);
SetPixel(pixels, pw, x, y, new Color(c.R, c.G, c.B, wv));
if (wv < 0.5f)
{
double hR = w.Sampler.HeightAt(x0 * cs + (x + 1.5) * px, y0 * cs + (y + 0.5) * px);
double hD = w.Sampler.HeightAt(x0 * cs + (x + 0.5) * px, y0 * cs + (y + 1.5) * px);
int band = (int)Math.Floor(h / ContourStepM);
if (band != (int)Math.Floor(hR / ContourStepM) || band != (int)Math.Floor(hD / ContourStepM))
SetPixel(contour, pw, x, y, new Color(0.25f, 0.27f, 0.22f, (band % 5 == 0) ? 0.45f : 0.22f));
}
}
}
var key = (cx, cy);
if (!_sprites.TryGetValue(key, out var pair))
{
pair = (NewSprite(), NewSprite());
pair.Base.Material = TerrainMaterial();
AddChild(pair.Base);
AddChild(pair.Contours);
pair.Contours.ZIndex = 1;
_sprites[key] = pair;
}
foreach (var (sprite, data) in new[] { (pair.Base, pixels), (pair.Contours, contour) })
{
sprite.Texture = ImageTexture.CreateFromImage(Image.CreateFromData(pw, ph, false, Image.Format.Rgba8, data));
sprite.Position = new Vector2((float)(x0 * cs), (float)(y0 * cs));
sprite.Scale = new Vector2((float)px, (float)px);
}
pair.Contours.Visible = GetViewport().GetCamera2D() is { } cam && cam.Zoom.X >= MapScale.ContourMinZoom;
}
private static ShaderMaterial? _material;
private static ShaderMaterial TerrainMaterial()
{
if (_material is not null) return _material;
var shader = ResourceLoader.Load<Shader>("res://assets/shaders/terrain.gdshader");
_material = new ShaderMaterial { Shader = shader };
_material.SetShaderParameter("bank_color", Palette.Banks);
return _material;
}
private static double H(Terrain t, int x, int y) => t.CellHeightM(Math.Clamp(x, 0, t.Width - 1), Math.Clamp(y, 0, t.Height - 1));
private static float Bilinear(float a, float b, float c, float d, float tx, float ty) => (a + (b - a) * tx) + ((c + (d - c) * tx) - (a + (b - a) * tx)) * ty;
private static Sprite2D NewSprite() => new() { Centered = false, TextureFilter = TextureFilterEnum.Linear };
private static bool IsWater(CellType type) => type is CellType.Water or CellType.Sea;
private static Color BaseColor(CellType type, double h, int cellX, int cellY, int x, int y, int ppc)
{
switch (type)
{
case CellType.Sea: return Palette.Sea;
case CellType.Water: return Palette.Water;
case CellType.Marsh: return (x + y) % 6 < 2 ? Palette.Water.Lerp(Palette.Marsh, 0.6f) : Palette.Marsh;
case CellType.Forest: return Palette.Undergrowth;
case CellType.Field:
{
// campi a strisce, con orientamento e colore per cella
uint hash = (uint)(cellX * 73856093 ^ cellY * 19349663);
bool vertical = (hash & 1) == 0;
int stripe = (vertical ? x : y) * 4 / Math.Max(1, ppc);
var baseColor = ((hash >> 1) % 3) switch { 0 => Palette.Grain, 1 => new Color("a9c46a"), _ => new Color("c8a86a") };
return stripe % 2 == 0 ? baseColor : baseColor.Darkened(0.08f);
}
case CellType.Town: return new Color("d6d2bf");
case CellType.Industry: return new Color("c9c4b2");
case CellType.Rock: return Palette.Rock;
case CellType.Mountain: return Palette.Highland.Darkened(0.05f);
default:
{
// fasce altimetriche: pianura, collina, altura
if (h < 120) return Palette.Plain;
if (h < 320) return Palette.Plain.Lerp(Palette.Hill, (float)((h - 120) / 200));
return Palette.Hill.Lerp(Palette.Highland, (float)Math.Min(1, (h - 320) / 300));
}
}
}
private static void SetPixel(byte[] data, int width, int x, int y, Color c)
{
int i = (y * width + x) * 4;
data[i] = (byte)(Math.Clamp(c.R, 0, 1) * 255);
data[i + 1] = (byte)(Math.Clamp(c.G, 0, 1) * 255);
data[i + 2] = (byte)(Math.Clamp(c.B, 0, 1) * 255);
data[i + 3] = (byte)(Math.Clamp(c.A, 0, 1) * 255);
}
}