using Godot; using RailEnterprise.Client.App; using RailEnterprise.Core.Rail; using RailEnterprise.Protocol.Client; using RailEnterprise.Protocol.Messages; namespace RailEnterprise.Client.Map; /// /// Binari e opere (immagini 01 e 02): massicciata #bfb79f, traversine #5b4a39 ogni 5,3 unità, /// rotaie #3b3f42 a ±2,4; rilevati come banda chiara con ombra, trincee come banda scura, ponti /// con pile ogni 30 m e spallette, gallerie con portali e binario tratteggiato; cantieri con /// tratteggio #8a7a52 e barriera ambra. LOD: sotto 0,4 niente traversine fitte, sotto 0,15 solo /// massicciata e rotaia. /// public partial class TrackLayer : Node2D { private ClientWorld? _world; private int _drawnVersion = -1; private float _drawnScale = -1; private float _zoom = 1; public void Bind(ClientWorld world) { _world = world; _drawnVersion = -1; QueueRedraw(); } public void SetZoom(float zoom) { _zoom = zoom; float s = Quantize(MapScale.Symbol(zoom)); if (Math.Abs(s - _drawnScale) > 1e-3) QueueRedraw(); } private static float Quantize(float s) => s <= 1 ? 1 : MathF.Pow(1.25f, MathF.Round(MathF.Log(s) / MathF.Log(1.25f))); public override void _Process(double delta) { if (_world is not null && _world.TrackVersion != _drawnVersion) QueueRedraw(); } public override void _Draw() { if (_world is null) return; _drawnVersion = _world.TrackVersion; float s = Quantize(MapScale.Symbol(_zoom)); _drawnScale = s; var edges = _world.Edges.Values.OrderBy(e => e.Id).ToList(); // prima le opere sotto il binario, poi i binari, infine i dettagli foreach (var e in edges) DrawWorks(e, s); foreach (var e in edges) DrawTrack(e, s); } private static Vector2[] Points(EdgeInfo e, int from, int to) { var pts = new Vector2[to - from + 1]; for (int i = from; i <= to; i++) pts[i - from] = MapScale.V(e.X[i], e.Y[i]); return pts; } /// Tratti consecutivi con la stessa opera. private static IEnumerable<(WorkKind Kind, int From, int To)> Runs(EdgeInfo e) { int start = 0; for (int i = 1; i <= e.Kinds.Length; i++) { if (i < e.Kinds.Length && e.Kinds[i] == e.Kinds[start]) continue; yield return ((WorkKind)e.Kinds[start], Math.Max(0, start - (start > 0 ? 1 : 0)), Math.Min(e.Kinds.Length - 1, i)); start = i; } } private void DrawWorks(EdgeInfo e, float s) { float width = (e.DoubleTrack ? 19f : 9f) * s; foreach (var (kind, from, to) in Runs(e)) { if (to <= from) continue; var pts = Points(e, from, to); switch (kind) { case WorkKind.Embankment: DrawPolyline(Shift(pts, 2.5f * s), new Color(0.35f, 0.36f, 0.3f, 0.35f), width * 2.2f); DrawPolyline(pts, new Color("ddd6bf"), width * 2.0f); break; case WorkKind.Cutting: DrawPolyline(pts, new Color("8f8a73"), width * 2.2f); DrawPolyline(pts, new Color("a8a38a"), width * 1.5f); break; case WorkKind.Bridge: DrawBridge(pts, width, s); break; case WorkKind.Tunnel: DrawPortal(pts[0], pts[1] - pts[0], width, s); DrawPortal(pts[^1], pts[^2] - pts[^1], width, s); break; } } } private void DrawBridge(Vector2[] pts, float width, float s) { // pile ogni 30 m, impalcato e spallette alle estremità foreach (var (p, dir) in MapScale.Every(pts, 30f * Math.Max(1, s * 0.5f), 15f)) { var n = new Vector2(-dir.Y, dir.X); DrawLine(p - n * width * 0.85f, p + n * width * 0.85f, new Color("6f6a5e"), 3f * s); } DrawPolyline(Shift(pts, 2f * s), new Color(0.2f, 0.25f, 0.28f, 0.35f), width * 1.4f); DrawPolyline(pts, new Color("b9b3a3"), width * 1.35f); DrawPolyline(Offset(pts, width * 0.68f), new Color("7a7468"), 1.6f * s); DrawPolyline(Offset(pts, -width * 0.68f), new Color("7a7468"), 1.6f * s); foreach (var (end, inner) in new[] { (pts[0], pts[1]), (pts[^1], pts[^2]) }) { var dir = (inner - end).Normalized(); var n = new Vector2(-dir.Y, dir.X); DrawLine(end - n * width, end + n * width, new Color("8d877a"), 4f * s); } } private void DrawPortal(Vector2 at, Vector2 inward, float width, float s) { if (inward.LengthSquared() <= 0) return; var dir = inward.Normalized(); var n = new Vector2(-dir.Y, dir.X); var c = new Color("4a4740"); DrawLine(at - n * width, at + n * width, c, 5f * s); DrawLine(at - n * width, at - n * width + dir * 8f * s, c, 3f * s); DrawLine(at + n * width, at + n * width + dir * 8f * s, c, 3f * s); } private void DrawTrack(EdgeInfo e, float s) { var all = Points(e, 0, e.S.Length - 1); bool building = e.State == (byte)EdgeState.UnderConstruction; int built = all.Length - 1; if (building) { // parte costruita in proporzione all'avanzamento, il resto tratteggiato double target = e.Progress * e.S[^1]; built = 0; while (built < e.S.Length - 1 && e.S[built + 1] <= target) built++; } float[] offsets = e.DoubleTrack ? [-5f * s, 5f * s] : [0f]; foreach (float off in offsets) { var line = off == 0 ? all : Offset(all, off); foreach (var (kind, from, to) in Runs(e)) { int a = from, b = Math.Min(to, built); if (b <= a) continue; var seg = line[a..(b + 1)]; if (kind == WorkKind.Tunnel) { for (int i = 0; i < seg.Length - 1; i++) DrawDashedLine(seg[i], seg[i + 1], Palette.Rails, 1.5f * s, 6f * s); continue; } DrawSingleTrack(seg, s, e.State == (byte)EdgeState.Closed); } if (building && built < all.Length - 1) { var rest = line[built..]; for (int i = 0; i < rest.Length - 1; i++) DrawDashedLine(rest[i], rest[i + 1], Palette.Construction, 2.5f * s, 7f * s); // barriera ambra al fronte del cantiere var front = rest[0]; var dir = (rest[Math.Min(1, rest.Length - 1)] - front).Normalized(); var n = new Vector2(-dir.Y, dir.X); for (int k = -2; k <= 1; k++) { var c = k % 2 == 0 ? Palette.Amber : Palette.AppBackground; DrawLine(front + n * k * 2.5f * s, front + n * (k + 1) * 2.5f * s, c, 2.2f * s); } } } // segnali: piccoli dischi accanto al binario foreach (double sig in e.Signals) { int i = Array.BinarySearch(e.S, sig); if (i < 0) i = Math.Max(0, ~i - 1); i = Math.Min(i, e.S.Length - 2); var p = all[i]; var dir = (all[i + 1] - p).Normalized(); var n = new Vector2(-dir.Y, dir.X); DrawCircle(p + n * 7f * s, 1.8f * s, Palette.Rails); DrawCircle(p + n * 7f * s, 1.1f * s, Palette.Ok); } } private void DrawSingleTrack(Vector2[] pts, float s, bool closed) { float zoomDetail = _zoom; DrawPolyline(pts, closed ? Palette.Ballast.Darkened(0.2f) : Palette.Ballast, 9f * s); if (zoomDetail >= MapScale.OverviewZoom || s > 1) { float spacing = 5.3f * s * (zoomDetail >= MapScale.DetailZoom ? 1 : 1.6f); var sleepers = new List(); foreach (var (p, dir) in MapScale.Every(pts, spacing, spacing / 2)) { var n = new Vector2(-dir.Y, dir.X); sleepers.Add(p - n * 3.6f * s); sleepers.Add(p + n * 3.6f * s); } if (sleepers.Count > 0) DrawMultiline(sleepers.ToArray(), Palette.Sleepers, 1.3f * s); DrawPolyline(Offset(pts, 2.4f * s), Palette.Rails, 0.9f * s); DrawPolyline(Offset(pts, -2.4f * s), Palette.Rails, 0.9f * s); } else { DrawPolyline(pts, Palette.Rails, 1.4f * s); } } private static Vector2[] Offset(Vector2[] pts, float off) => MapScale.Offset(pts, off); private static Vector2[] Shift(Vector2[] pts, float d) => pts.Select(p => p + new Vector2(d, d)).ToArray(); }