using System.Globalization; using System.Numerics; using System.Text; using System.Text.Json.Nodes; namespace RynthNav.TownNet; /// /// A floor plan of the network as SVG, for looking the walks over: floors (grey, one shade per /// height), walls and solid objects (dark), portals (blue rings: the reach that uses them), arrival /// points (green), and the walks from one arrival (or all). /// internal static class MapSvg { public static string Draw(Indoor w, IEnumerable cells, JsonObject doc, string? onlyFrom, float level) { var ci = CultureInfo.InvariantCulture; var cellList = cells.Select(id => w.Cells[id]).Where(c => c.Floors.Any(f => MathF.Abs(f[0].Z - level) < 3f)).ToList(); var all = cellList.SelectMany(c => c.Floors).SelectMany(f => f).ToList(); float minX = all.Min(v => v.X) - 3, maxX = all.Max(v => v.X) + 3, minY = all.Min(v => v.Y) - 3, maxY = all.Max(v => v.Y) + 3; const float S = 6f; // pixels per metre string P(float x, float y) => ((x - minX) * S).ToString("F1", ci) + "," + ((maxY - y) * S).ToString("F1", ci); var sb = new StringBuilder(); sb.Append(ci, $"\n"); foreach (var c in cellList) foreach (var f in c.Floors.Where(f => MathF.Abs(f[0].Z - level) < 3f)) sb.Append($" P(v.X, v.Y)))}\" fill=\"{(MathF.Abs(f[0].Z - level) < 0.5f ? "#ddd" : "#eee")}\" stroke=\"#ccc\" stroke-width=\"0.5\"/>\n"); foreach (var s in w.Solids.Where(s => s.Blocks(level))) { if (s.Poly.Length == 0) sb.Append(ci, $"\n"); else sb.Append($" P(v.X, v.Y)))}\" fill=\"{(s.Filled ? "#a33" : "none")}\" stroke=\"{(s.Source.StartsWith("wall") ? "#222" : "#a33")}\" stroke-width=\"1.5\"/>\n"); } foreach (var p in w.Portals) { sb.Append(ci, $"\n"); sb.Append(ci, $"{System.Security.SecurityElement.Escape(p.Name.Replace("Portal to ", "").Replace(" Portal", ""))}\n"); } foreach (var wk in doc["walks"]?.AsArray() ?? new JsonArray()) { if (wk!["points"] is not JsonArray pts || (onlyFrom != null && (string?)wk["from"] != onlyFrom)) continue; var xy = pts.Select(p => P((float)(double)p![1]!, (float)(double)p[2]!)); sb.Append($"\n"); } foreach (var a in doc["arrivals"]!.AsArray()) { float x = (float)(double)a!["x"]!, y = (float)(double)a["y"]!; sb.Append(ci, $"\n"); sb.Append(ci, $"{(string)a["id"]!}\n"); } sb.Append("\n"); return sb.ToString(); } }