model/buildmap: Build engine .map v7-9 (sectors, walls, sprites as raw tables; Scene with sloped floors / ceilings, upper / lower / masked walls, sprites as nodes; v9 room-over-room fields), read + write; tested against an independent Python reader and Ken Silverman's map2stl
This commit is contained in:
1 parent
75984be212
commit
aacc78fac2
7 files changed
+1112
-1
No files matched your search
@@ -0,0 +1,223 @@
|
||||
# Build .map fixtures (tools/fixtures.py). No Duke 3D / Shadow Warrior maps (proprietary): maps are written here from
|
||||
# BUILDINF.TXT (v7 layout; v8 / v9 share it), red walls linked by matching reversed wall segments. References:
|
||||
# - an independent Python reading of each map: field sums per table, every sector's loops, and the floor / ceiling
|
||||
# z of every sector at each of its wall points by the BUILDINF slope formula;
|
||||
# - Ken Silverman's map2stl (eduke32 source, tools/fixtures/_build_ref.py) turns the v7 bytes of each map into an
|
||||
# STL: total area and area-weighted centroid of its floor / ceiling triangles and of its wall triangles.
|
||||
# tests/gen/buildmap.luau { maps = { { name, data, version, counts = { sectors, walls, sprites }, sums = { sector =
|
||||
# { field = sum }, wall, sprite }, loops = { { { wall } } }, z = { { sector, x, y, floor,
|
||||
# ceiling } }, stl = { floorArea, wallArea, floorCentroid = { x, y, z }, wallCentroid } } } }
|
||||
import os, struct, subprocess, tempfile, shutil, importlib.util
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_build_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_ref.py"))
|
||||
B = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(B)
|
||||
|
||||
INPUTS = ["tools/fixtures/_build_ref.py"]
|
||||
|
||||
SECTOR = [("wallptr", "h"), ("wallnum", "h"), ("ceilingz", "i"), ("floorz", "i"), ("ceilingstat", "h"), ("floorstat", "h"),
|
||||
("ceilingpicnum", "h"), ("ceilingheinum", "h"), ("ceilingshade", "b"), ("ceilingpal", "B"), ("ceilingxpanning", "B"),
|
||||
("ceilingypanning", "B"), ("floorpicnum", "h"), ("floorheinum", "h"), ("floorshade", "b"), ("floorpal", "B"),
|
||||
("floorxpanning", "B"), ("floorypanning", "B"), ("visibility", "B"), ("filler", "B"), ("lotag", "h"), ("hitag", "h"),
|
||||
("extra", "h")]
|
||||
WALL = [("x", "i"), ("y", "i"), ("point2", "h"), ("nextwall", "h"), ("nextsector", "h"), ("cstat", "h"), ("picnum", "h"),
|
||||
("overpicnum", "h"), ("shade", "b"), ("pal", "B"), ("xrepeat", "B"), ("yrepeat", "B"), ("xpanning", "B"),
|
||||
("ypanning", "B"), ("lotag", "h"), ("hitag", "h"), ("extra", "h")]
|
||||
SPRITE = [("x", "i"), ("y", "i"), ("z", "i"), ("cstat", "h"), ("picnum", "h"), ("shade", "b"), ("pal", "B"), ("clipdist", "B"),
|
||||
("filler", "B"), ("xrepeat", "B"), ("yrepeat", "B"), ("xoffset", "b"), ("yoffset", "b"), ("sectnum", "h"),
|
||||
("statnum", "h"), ("ang", "h"), ("owner", "h"), ("xvel", "h"), ("yvel", "h"), ("zvel", "h"), ("lotag", "h"),
|
||||
("hitag", "h"), ("extra", "h")]
|
||||
FMT = {name: "<" + "".join(t for _, t in fields) for name, fields in (("sector", SECTOR), ("wall", WALL), ("sprite", SPRITE))}
|
||||
assert [struct.calcsize(FMT[k]) for k in ("sector", "wall", "sprite")] == [40, 32, 44]
|
||||
|
||||
|
||||
class MapBuilder:
|
||||
def __init__(self, rng):
|
||||
self.rng = rng
|
||||
self.sectors, self.walls, self.sprites = [], [], []
|
||||
|
||||
def sector(self, loops, **f):
|
||||
"""loops: outer loop first (positive shoelace in Build's y-down coordinates), then holes (negative)"""
|
||||
start = len(self.walls)
|
||||
for li, loop in enumerate(loops):
|
||||
area = sum(loop[i][0] * loop[(i + 1) % len(loop)][1] - loop[(i + 1) % len(loop)][0] * loop[i][1] for i in range(len(loop)))
|
||||
assert (area > 0) == (li == 0), "loop orientation"
|
||||
base = len(self.walls)
|
||||
for i, (x, y) in enumerate(loop):
|
||||
self.walls.append({"x": x, "y": y, "point2": base + (i + 1) % len(loop), "nextwall": -1, "nextsector": -1,
|
||||
"cstat": 0, "picnum": self.rng.randrange(1, 40), "overpicnum": 0, "shade": self.rng.randrange(-8, 20),
|
||||
"pal": 0, "xrepeat": 8, "yrepeat": 8, "xpanning": self.rng.randrange(256),
|
||||
"ypanning": self.rng.randrange(256), "lotag": 0, "hitag": 0, "extra": -1})
|
||||
s = {k: 0 for k, _ in SECTOR}
|
||||
s.update({"wallptr": start, "wallnum": len(self.walls) - start, "extra": -1, "ceilingpicnum": self.rng.randrange(1, 40),
|
||||
"floorpicnum": self.rng.randrange(1, 40), "visibility": self.rng.randrange(256)})
|
||||
s.update(f)
|
||||
self.sectors.append(s)
|
||||
return len(self.sectors) - 1
|
||||
|
||||
def link(self):
|
||||
"""red walls: a wall a -> b of one sector and b -> a of another"""
|
||||
seg = {}
|
||||
for si, s in enumerate(self.sectors):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
a, b = self.walls[w], self.walls[self.walls[w]["point2"]]
|
||||
seg[(a["x"], a["y"], b["x"], b["y"])] = (si, w)
|
||||
for si, s in enumerate(self.sectors):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
a, b = self.walls[w], self.walls[self.walls[w]["point2"]]
|
||||
o = seg.get((b["x"], b["y"], a["x"], a["y"]))
|
||||
if o:
|
||||
a["nextsector"], a["nextwall"] = o
|
||||
|
||||
def sprite(self, **f):
|
||||
sp = {k: 0 for k, _ in SPRITE}
|
||||
sp.update({"xrepeat": 64, "yrepeat": 64, "clipdist": 32, "owner": -1, "picnum": self.rng.randrange(1000, 2000),
|
||||
"ang": self.rng.randrange(2048), "lotag": self.rng.randrange(100)})
|
||||
sp.update(f)
|
||||
self.sprites.append(sp)
|
||||
|
||||
def write(self, version, pos=(512, 512, -4096), ang=512, cursect=0, trailing=b""):
|
||||
out = bytearray(struct.pack("<iiiihh", version, *pos, ang, cursect))
|
||||
for name, rows, fields in (("sector", self.sectors, SECTOR), ("wall", self.walls, WALL), ("sprite", self.sprites, SPRITE)):
|
||||
out += struct.pack("<H", len(rows))
|
||||
for r in rows:
|
||||
out += struct.pack(FMT[name], *[r[k] for k, _ in fields])
|
||||
return bytes(out) + trailing
|
||||
|
||||
|
||||
def parse(data):
|
||||
"""independent reading: (version, {"sector": [...], "wall": [...], "sprite": [...]})"""
|
||||
version = struct.unpack_from("<i", data, 0)[0]
|
||||
p, tables = 20, {}
|
||||
for name, fields in (("sector", SECTOR), ("wall", WALL), ("sprite", SPRITE)):
|
||||
n = struct.unpack_from("<H", data, p)[0]
|
||||
p += 2
|
||||
size = struct.calcsize(FMT[name])
|
||||
tables[name] = [dict(zip([k for k, _ in fields], struct.unpack_from(FMT[name], data, p + i * size))) for i in range(n)]
|
||||
p += n * size
|
||||
return version, tables
|
||||
|
||||
|
||||
def slope_z(t, si, floor, x, y):
|
||||
s = t["sector"][si]
|
||||
pre = "floor" if floor else "ceiling"
|
||||
z = s[pre + "z"]
|
||||
if not s[pre + "stat"] & 2 or s[pre + "heinum"] == 0:
|
||||
return z
|
||||
w0 = t["wall"][s["wallptr"]]
|
||||
w1 = t["wall"][w0["point2"]]
|
||||
dx, dy = w1["x"] - w0["x"], w1["y"] - w0["y"]
|
||||
ln = (dx * dx + dy * dy) ** 0.5
|
||||
return z + s[pre + "heinum"] * (dx * (y - w0["y"]) - dy * (x - w0["x"])) / (ln * 256)
|
||||
|
||||
|
||||
def loops_of(t, si):
|
||||
s, out, seen = t["sector"][si], [], set()
|
||||
for start in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
if start in seen:
|
||||
continue
|
||||
loop, w = [], start
|
||||
while w not in seen:
|
||||
seen.add(w)
|
||||
loop.append(w)
|
||||
w = t["wall"][w]["point2"]
|
||||
out.append(loop)
|
||||
return out
|
||||
|
||||
|
||||
def stl_stats(stl):
|
||||
n = struct.unpack_from("<I", stl, 80)[0]
|
||||
acc = {"floor": [0.0, 0.0, 0.0, 0.0], "wall": [0.0, 0.0, 0.0, 0.0]}
|
||||
for i in range(n):
|
||||
v = struct.unpack_from("<12f", stl, 84 + i * 50)
|
||||
nz, a, b, c = v[2], v[3:6], v[6:9], v[9:12]
|
||||
ux, uy, uz = b[0] - a[0], b[1] - a[1], b[2] - a[2]
|
||||
vx, vy, vz = c[0] - a[0], c[1] - a[1], c[2] - a[2]
|
||||
cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
area = 0.5 * (cx * cx + cy * cy + cz * cz) ** 0.5
|
||||
k = acc["wall" if abs(nz) < 1e-6 else "floor"]
|
||||
k[0] += area
|
||||
for d in range(3):
|
||||
k[d + 1] += area * (a[d] + b[d] + c[d]) / 3
|
||||
res = {}
|
||||
for k, (area, sx, sy, sz) in acc.items():
|
||||
res[k + "Area"] = area
|
||||
res[k + "Centroid"] = [sx / area, sy / area, sz / area] if area > 0 else [0.0, 0.0, 0.0]
|
||||
return res
|
||||
|
||||
|
||||
def rooms(rng):
|
||||
m = MapBuilder(rng)
|
||||
# a hall with a sloped platform sector inside (the hall's hole) and a red-wall opening to a sloped east room
|
||||
m.sector([[(0, 0), (4096, 0), (4096, 1024), (4096, 3072), (4096, 4096), (0, 4096)],
|
||||
[(1024, 1024), (1024, 2048), (2048, 2048), (2048, 1024)]], floorz=0, ceilingz=-32768)
|
||||
m.sector([[(4096, 1024), (6144, 1024), (6144, 3072), (4096, 3072)]], floorz=-4096, ceilingz=-24576,
|
||||
floorstat=2, floorheinum=-1024, ceilingstat=2 | 64, ceilingheinum=512, floorxpanning=17)
|
||||
m.sector([[(1024, 1024), (2048, 1024), (2048, 2048), (1024, 2048)]], floorz=-8192, ceilingz=-28672,
|
||||
floorstat=2 | 4 | 16, floorheinum=4096, ceilingstat=8 | 32)
|
||||
m.link()
|
||||
for w in m.walls:
|
||||
if (w["x"], w["y"]) == (4096, 1024) and w["nextsector"] == 1:
|
||||
w["cstat"] |= 16 | 4
|
||||
w["overpicnum"] = 77
|
||||
if w["nextsector"] == 2:
|
||||
w["cstat"] |= 2 | 8
|
||||
m.walls[0]["cstat"] |= 256
|
||||
m.sprite(x=300, y=400, z=0, sectnum=0, cstat=1)
|
||||
m.sprite(x=5000, y=2000, z=-4096, sectnum=1, cstat=16, ang=0)
|
||||
m.sprite(x=1500, y=1500, z=-8192, sectnum=2, cstat=32 | 4, ang=1024)
|
||||
return m
|
||||
|
||||
|
||||
def sky(rng):
|
||||
m = MapBuilder(rng)
|
||||
m.sector([[(0, 0), (2048, 0), (2048, 2048), (0, 2048)]], floorz=0, ceilingz=-16384, ceilingstat=1)
|
||||
m.sector([[(2048, 0), (4096, 0), (4096, 2048), (2048, 2048)]], floorz=1024, ceilingz=-20480, ceilingstat=1)
|
||||
m.link()
|
||||
m.sprite(x=1000, y=1000, z=0, sectnum=0)
|
||||
return m
|
||||
|
||||
|
||||
def yax(rng):
|
||||
m = MapBuilder(rng)
|
||||
m.sector([[(0, 0), (1024, 0), (1024, 1024), (0, 1024)]], floorz=0, ceilingz=-8192, ceilingstat=1024, ceilingxpanning=3)
|
||||
m.sector([[(0, 0), (1024, 0), (1024, 1024), (0, 1024)]], floorz=-8192, ceilingz=-16384, floorstat=1024, floorxpanning=3)
|
||||
m.walls[0]["cstat"] |= 1024
|
||||
m.walls[0]["lotag"] = 4
|
||||
m.walls[5]["cstat"] |= 2048
|
||||
m.walls[5]["extra"] = 0
|
||||
return m
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(1996)
|
||||
tools = B.tools(ctx)
|
||||
specs = [("rooms", rooms(rng), 7, b""), ("sky", sky(rng), 8, b""), ("yax", yax(rng), 9, b""),
|
||||
("trailing", rooms(rng), 7, b"EXTRA DATA\x00\x01")]
|
||||
maps = []
|
||||
tmp = tempfile.mkdtemp(prefix="rosetta-buildmap-")
|
||||
try:
|
||||
for name, m, version, trailing in specs:
|
||||
data = m.write(version, trailing=trailing)
|
||||
v, t = parse(data)
|
||||
assert v == version
|
||||
e = {"name": name, "data": data, "version": version,
|
||||
"counts": {"sectors": len(t["sector"]), "walls": len(t["wall"]), "sprites": len(t["sprite"])},
|
||||
"sums": {k: {f: sum(r[f] for r in rows) for f, _ in fields} for k, rows, fields in
|
||||
(("sector", t["sector"], SECTOR), ("wall", t["wall"], WALL), ("sprite", t["sprite"], SPRITE))},
|
||||
"loops": [loops_of(t, si) for si in range(len(t["sector"]))], "z": []}
|
||||
for si, s in enumerate(t["sector"]):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
x, y = t["wall"][w]["x"], t["wall"][w]["y"]
|
||||
e["z"].append({"sector": si, "x": x, "y": y, "floor": float(slope_z(t, si, True, x, y)),
|
||||
"ceiling": float(slope_z(t, si, False, x, y))})
|
||||
if name != "yax":
|
||||
src, dst = os.path.join(tmp, name + ".map"), os.path.join(tmp, name + ".stl")
|
||||
with open(src, "wb") as f:
|
||||
f.write(m.write(7))
|
||||
subprocess.run([tools["map2stl"], src, dst], capture_output=True, check=True)
|
||||
e["stl"] = stl_stats(open(dst, "rb").read())
|
||||
maps.append(e)
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
ctx.emit("buildmap", {"maps": maps})
|
||||
Reference in new issue
Block a user