Files
WT-Offline/tools/navgen/render-preview.py
T
N0VAN0M4LY 4191d59168 feat(navgen): bake road materials and directional cover
Decode LMp2 terrain materials and decals into navigable road areas. Preserve usable cover bearings and verify left and right corner approaches independently on the standard tank mesh.

Verified with Rust and MSVC builds, TypeScript checks, and offline baking on Berlin, Poland, and Hurtgen for 2.61.0.58.
2026-12-04 12:36:07 +08:00

258 lines
9.7 KiB
Python

"""Render top-down PNG inspection maps (`*_nav.png` and `*_cover.png`) from `.preview.bin` + `.pois.json`.
Zero external dependencies (Python stdlib `struct` + `zlib` only).
"""
import argparse
import json
import math
import os
import struct
import zlib
def write_png(path: str, width: int, height: int, rgb: bytearray) -> None:
def chunk(tag: bytes, data: bytes) -> bytes:
crc = zlib.crc32(tag + data) & 0xFFFFFFFF
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", crc)
raw = bytearray()
stride = width * 3
for y in range(height):
raw.append(0)
row = y * stride
raw.extend(rgb[row : row + stride])
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
png = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(bytes(raw), 6)) + chunk(b"IEND", b"")
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
with open(path, "wb") as f:
f.write(png)
def set_px(rgb: bytearray, w: int, h: int, x: int, y: int, r: int, g: int, b: int) -> None:
if 0 <= x < w and 0 <= y < h:
idx = (y * w + x) * 3
rgb[idx] = r
rgb[idx + 1] = g
rgb[idx + 2] = b
def draw_line(rgb: bytearray, w: int, h: int, x0: int, y0: int, x1: int, y1: int, col, thick: int = 1) -> None:
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx - dy
r, g, b = col
while True:
for oy in range(-thick // 2, thick // 2 + 1):
for ox in range(-thick // 2, thick // 2 + 1):
set_px(rgb, w, h, x0 + ox, y0 + oy, r, g, b)
if x0 == x1 and y0 == y1:
break
e2 = 2 * err
if e2 > -dy:
err -= dy
x0 += sx
if e2 < dx:
err += dx
y0 += sy
def draw_circle(rgb: bytearray, w: int, h: int, cx: int, cy: int, rad: int, col, fill: bool = False) -> None:
r, g, b = col
r2 = rad * rad
for dy in range(-rad, rad + 1):
for dx in range(-rad, rad + 1):
d2 = dx * dx + dy * dy
if fill and d2 <= r2:
set_px(rgb, w, h, cx + dx, cy + dy, r, g, b)
elif not fill and abs(d2 - r2) <= max(2, rad * 2):
set_px(rgb, w, h, cx + dx, cy + dy, r, g, b)
def draw_square(rgb: bytearray, w: int, h: int, cx: int, cy: int, half: int, col) -> None:
r, g, b = col
for dy in range(-half, half + 1):
for dx in range(-half, half + 1):
set_px(rgb, w, h, cx + dx, cy + dy, r, g, b)
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--preview", required=True)
ap.add_argument("--pois", required=True)
ap.add_argument("--out-dir", required=True)
ap.add_argument("--name", required=True)
args = ap.parse_args()
with open(args.preview, "rb") as f:
data = f.read()
with open(args.pois, "r", encoding="utf-8") as f:
pois = json.load(f)
magic = data[:8]
if magic not in (b"WTOPREV1", b"WTOPREV2"):
raise ValueError("bad preview magic")
off = 8
pw, ph, min_x, min_z, max_x, max_z, water_y = struct.unpack_from("<IIfffff", data, off)
off += 28
n_px = pw * ph
heights = struct.unpack_from(f"<{n_px}f", data, off)
off += n_px * 4
flag_format = "H" if magic == b"WTOPREV2" else "B"
flags = struct.unpack_from(f"<{n_px}{flag_format}", data, off)
off += n_px * struct.calcsize(flag_format)
cover_cnt = struct.unpack_from("<I", data, off)[0]
off += 4
covers = []
# WtoCoverPoint contains 16 floats followed by four one-byte fields.
cov_fmt = "<3f2f2f3f3f3f4B"
cov_sz = struct.calcsize(cov_fmt)
for _ in range(cover_cnt):
covers.append(struct.unpack_from(cov_fmt, data, off))
off += cov_sz
tac_cols, tac_rows = struct.unpack_from("<II", data, off)
off += 8
tac = data[off : off + tac_cols * tac_rows * 4]
off += tac_cols * tac_rows * 4
path_cnt = struct.unpack_from("<I", data, off)[0]
off += 4
paths = []
for _ in range(path_cnt):
npts = struct.unpack_from("<I", data, off)[0]
off += 4
pts = struct.unpack_from(f"<{npts * 3}f", data, off)
off += npts * 12
paths.append([(pts[i * 3], pts[i * 3 + 2]) for i in range(npts)])
def world_to_px(wx: float, wz: float):
px = int((wx - min_x) / max(1e-3, max_x - min_x) * (pw - 1))
py = int((max_z - wz) / max(1e-3, max_z - min_z) * (ph - 1)) # North (+Z) up
return px, py
h_min = min(heights)
h_max = max(heights)
h_span = max(1.0, h_max - h_min)
nav_rgb = bytearray(pw * ph * 3)
cov_rgb = bytearray(pw * ph * 3)
for rz in range(ph):
py = ph - 1 - rz # Flip Z so North (+Z) is top of image
row_off = rz * pw
for rx in range(pw):
h = heights[row_off + rx]
fl = flags[row_off + rx]
# Base terrain shade with 5m contour lines
t = (h - h_min) / h_span
base = int(38 + t * 52)
if abs((h % 5.0)) < 0.25:
base = max(20, base - 16)
# Nav map color:
# Dark gray = unwalkable obstacle / steep cliff
# Deep blue = deep water, Cyan-blue = shallow water
# Bright green = T2 (wide heavy) + T1 + T0 walkable
# Mid-green = T1 + T0 walkable (standard tank fits, wide heavy doesn't)
# Yellow-green = T0 only (narrow tank alley)
# Tan = road, Orange = chokepoint, Olive = crushable/forest, Teal = bridge
r, g, b = base, base, base
if fl & 16: # deep water
r, g, b = 22, 58, 125
elif fl & 8: # shallow water
r, g, b = 55, 130, 185
elif fl & 128: # bridge
r, g, b = 70, 210, 210
elif fl & 32: # chokepoint
r, g, b = 220, 145, 45
elif fl & 64: # crushable/forest
r, g, b = int(base * 0.6 + 65), int(base * 0.8 + 85), int(base * 0.5 + 35)
elif fl & 256: # road
r, g, b = 174, 149, 108
elif fl & 4: # T2 wide walkable
r, g, b = int(base * 0.5 + 48), int(base * 0.7 + 125), int(base * 0.5 + 52)
elif fl & 2: # T1 standard walkable
r, g, b = int(base * 0.5 + 88), int(base * 0.7 + 115), int(base * 0.4 + 40)
elif fl & 1: # T0 narrow only
r, g, b = 185, 175, 65
else:
# Hard obstacle / cliff
r, g, b = max(18, base - 18), max(18, base - 18), max(22, base - 14)
idx = (py * pw + rx) * 3
nav_rgb[idx] = min(255, r)
nav_rgb[idx + 1] = min(255, g)
nav_rgb[idx + 2] = min(255, b)
# Cover map background: muted terrain + exposure tint
tx = min(tac_cols - 1, int(rx * tac_cols / pw))
tz = min(tac_rows - 1, int(rz * tac_rows / ph))
exp_v = tac[(tz * tac_cols + tx) * 4] if tac_cols > 0 and tac_rows > 0 else 0
if fl & 16:
cr, cg, cb = 20, 48, 95
elif fl & 2:
cr = min(255, int(base * 0.85 + exp_v * 0.18))
cg = min(255, int(base * 0.90 + 20))
cb = min(255, int(base * 0.85))
else:
cr, cg, cb = 26, 26, 30
cov_rgb[idx] = cr
cov_rgb[idx + 1] = cg
cov_rgb[idx + 2] = cb
# Overlay sample Detour paths (bright cyan) on nav map
for path in paths:
for i in range(len(path) - 1):
x0, y0 = world_to_px(*path[i])
x1, y1 = world_to_px(*path[i + 1])
draw_line(nav_rgb, pw, ph, x0, y0, x1, y1, (0, 245, 255), thick=2)
# Overlay spawns & capture zones on BOTH maps
for rgb in (nav_rgb, cov_rgb):
for sp in pois.get("team1Spawns", []):
x, y = world_to_px(sp["x"], sp["z"])
draw_square(rgb, pw, ph, x, y, 5, (40, 130, 255))
draw_square(rgb, pw, ph, x, y, 2, (255, 255, 255))
for sp in pois.get("team2Spawns", []):
x, y = world_to_px(sp["x"], sp["z"])
draw_square(rgb, pw, ph, x, y, 5, (255, 65, 65))
draw_square(rgb, pw, ph, x, y, 2, (255, 255, 255))
for cap in pois.get("captureZones", []):
x, y = world_to_px(cap["x"], cap["z"])
rad_px = max(8, int(cap.get("radius", 25.0) / max(1.0, max_x - min_x) * pw))
draw_circle(rgb, pw, ph, x, y, rad_px, (255, 215, 0), fill=False)
draw_circle(rgb, pw, ph, x, y, 4, (255, 230, 40), fill=True)
# Overlay covers on cover map: Blue=FULL, Green=HULLDOWN, Yellow=PEEK
peeks = []
for c in covers:
px_w, py_w, pz_w = c[0], c[1], c[2]
dx, dz = c[3], c[4]
kind = c[16]
x, y = world_to_px(px_w, pz_w)
col = (60, 165, 255) if kind == 1 else (55, 240, 100)
draw_circle(cov_rgb, pw, ph, x, y, 2, col, fill=True)
# Direction tick pointing toward blocked threat
x2, y2 = world_to_px(px_w + dx * 8.0, pz_w + dz * 8.0)
draw_line(cov_rgb, pw, ph, x, y, x2, y2, col, thick=1)
if c[17]:
peeks.append(world_to_px(c[7], c[9]))
if c[18]:
peeks.append(world_to_px(c[10], c[12]))
# Draw both corners last so neighboring covers cannot paint over their markers.
for x, y in peeks:
draw_circle(cov_rgb, pw, ph, x, y, 1, (255, 240, 60), fill=True)
nav_out = os.path.join(args.out_dir, f"{args.name}_nav.png")
cov_out = os.path.join(args.out_dir, f"{args.name}_cover.png")
write_png(nav_out, pw, ph, nav_rgb)
write_png(cov_out, pw, ph, cov_rgb)
print(f"Rendered {nav_out} and {cov_out} (covers={len(covers)}, paths={len(paths)})")
if __name__ == "__main__":
main()