Entities as nodes (keys in extras, brushes as parsed), brushes through codecs/model/Brush, patchDef2 / patchDef3 through codecs/model/Patch, texture sizes from WAD2 / WAD3 / .wal / images; tested against ericw-tools qbsp.
173 lines
8.2 KiB
Python
173 lines
8.2 KiB
Python
# model/map fixtures (tools/fixtures.py): .map files written here in the four dialects and compiled with ericw-tools
|
|
# qbsp -nofill (Quake 1 BSP; it reads all four). Reference: qbsp's faces per model (plane, corners, texinfo vectors,
|
|
# texture and its size from the WAD), so the test checks our CSG'd polygons and texture coordinates per entity.
|
|
# quake: a room (6 wall boxes), a pillar, a func_door, point entities, Quake standard faces, "wad" -> tex.wad (WAD2)
|
|
# valve: the same layout in Valve 220 with a WAD3 named by a Windows path
|
|
# quake2: Quake II faces with contents / flags / value; sizes from textures/<name>.wal
|
|
# quake3: brushDef faces + a patchDef2 (qbsp compiles the brushes of a copy without the patch; the patch reference is
|
|
# evaluated here: biquadratic Bezier, 4 segments per sub-patch)
|
|
# tests/gen/map.luau { [name] = { text, files = { [name] = bytes }, models = { [model] = { faces } }, sizes, patch? } }
|
|
import importlib.util, os
|
|
|
|
INPUTS = ["tools/fixtures/_qbsp.py", "tools/fixtures/brush.py"]
|
|
|
|
TEX = [("brick", 64, 32), ("metal", 32, 128), ("wood", 128, 64), ("+0lava", 64, 64), ("stone", 16, 16)]
|
|
|
|
|
|
def load(ctx, name):
|
|
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
def layout(rng):
|
|
world = []
|
|
# room [-256, 256] x [-256, 256] x [0, 256], walls 16 thick
|
|
lo, hi = (-256, -256, 0), (256, 256, 256)
|
|
t = 16
|
|
world.append(((lo[0] - t, lo[1] - t, lo[2] - t), (hi[0] + t, hi[1] + t, lo[2])))
|
|
world.append(((lo[0] - t, lo[1] - t, hi[2]), (hi[0] + t, hi[1] + t, hi[2] + t)))
|
|
world.append(((lo[0] - t, lo[1] - t, lo[2]), (lo[0], hi[1] + t, hi[2])))
|
|
world.append(((hi[0], lo[1] - t, lo[2]), (hi[0] + t, hi[1] + t, hi[2])))
|
|
world.append(((lo[0], lo[1] - t, lo[2]), (hi[0], lo[1], hi[2])))
|
|
world.append(((lo[0], hi[1], lo[2]), (hi[0], hi[1] + t, hi[2])))
|
|
world.append(((-32, -32, 0), (32, 32, 192))) # pillar standing on the floor
|
|
world.append(((64, 64, -8), (160, 128, 48))) # a box sunk into the floor
|
|
door = [((100, -200, 0), (116, -120, 128))]
|
|
return world, door
|
|
|
|
|
|
def faces_for(B, box, rng, kind):
|
|
out = []
|
|
for pts in B.box_faces(*box):
|
|
tex = rng.choice(TEX)[0]
|
|
out.append((pts, tex, B.projection(rng, "bp" if kind == "quake3" else ("valve" if kind == "valve" else "standard"))))
|
|
return out
|
|
|
|
|
|
def face_text(B, kind, pts, tex, a, flags):
|
|
k = {"quake": "standard", "quake2": "standard", "valve": "valve", "quake3": "bp"}[kind]
|
|
line = B.face_line(k, pts, tex, a)
|
|
if kind == "quake3":
|
|
line = line[: -len(" 0 0 0")] + " " + " ".join(str(x) for x in flags)
|
|
elif kind == "quake2":
|
|
line += " " + " ".join(str(x) for x in flags)
|
|
return line
|
|
|
|
|
|
def write(B, kind, world, door, rng, wadkey, patch=None):
|
|
head = {"quake": "// Game: Quake\n// Format: Standard\n", "valve": "// Game: Half-Life\n// Format: Valve\n",
|
|
"quake2": "// Game: Quake 2\n// Format: Quake2\n", "quake3": "// Game: Quake 3\n// Format: Quake3\n"}[kind]
|
|
lines = [head + "{", '"classname" "worldspawn"', '"message" "fixture room"']
|
|
if wadkey:
|
|
lines.append('"wad" "%s"' % wadkey)
|
|
if kind == "valve":
|
|
lines.append('"mapversion" "220"')
|
|
for box in world:
|
|
lines.append("{")
|
|
if kind == "quake3":
|
|
lines += ["brushDef", "{"]
|
|
for pts, tex, a in faces_for(B, box, rng, kind):
|
|
lines.append(face_text(B, kind, pts, tex, a, [0, rng.randrange(0, 4), 0]))
|
|
if kind == "quake3":
|
|
lines.append("}")
|
|
lines.append("}")
|
|
if patch:
|
|
lines.append(patch)
|
|
lines += ["}", "{", '"classname" "func_door"', '"targetname" "door1"', '"angle" "90"']
|
|
for box in door:
|
|
lines.append("{")
|
|
if kind == "quake3":
|
|
lines += ["brushDef", "{"]
|
|
for pts, tex, a in faces_for(B, box, rng, kind):
|
|
lines.append(face_text(B, kind, pts, tex, a, [1, 0, 5]))
|
|
if kind == "quake3":
|
|
lines.append("}")
|
|
lines.append("}")
|
|
lines += ["}", "{", '"classname" "info_player_start"', '"origin" "-128 -128 24"', '"angle" "45"', "}",
|
|
"{", '"classname" "light"', '"origin" "0 0 200"', '"light" "300"', "}", ""]
|
|
return "\n".join(lines)
|
|
|
|
|
|
def bezier_patch(w, h, ctrl, level):
|
|
# ctrl[(i * w + j)] row-major (row i, column j) of (x, y, z, s, t); quadratic Bezier per 3 x 3 sub-patch
|
|
px, py = (w - 1) // 2, (h - 1) // 2
|
|
gw, gh = px * level + 1, py * level + 1
|
|
out = []
|
|
for gy in range(gh):
|
|
sy = min(gy // level, py - 1)
|
|
tv = (gy - sy * level) / level
|
|
bv = [(1 - tv) ** 2, 2 * tv * (1 - tv), tv * tv]
|
|
for gx in range(gw):
|
|
sx = min(gx // level, px - 1)
|
|
tu = (gx - sx * level) / level
|
|
bu = [(1 - tu) ** 2, 2 * tu * (1 - tu), tu * tu]
|
|
p = [0.0] * 5
|
|
for j in range(3):
|
|
for i in range(3):
|
|
c = ctrl[(sy * 2 + j) * w + sx * 2 + i]
|
|
for k in range(5):
|
|
p[k] += c[k] * bu[i] * bv[j]
|
|
out.append(p)
|
|
return out, gw, gh
|
|
|
|
|
|
def compile_faces(H, ctx, text, files):
|
|
res = H.compile(ctx, text, files, ["-nofill", "-noclip", "-nopercent"])
|
|
b = H.read_bsp(res["bsp"])
|
|
models = {}
|
|
for mi, m in enumerate(b["models"]):
|
|
first, count = m[14], m[15]
|
|
faces = []
|
|
for f in b["faces"][first:first + count]:
|
|
ti = b["texinfo"][f["texinfo"]]
|
|
name, w, h = b["textures"][ti[8]]
|
|
assert name and w and h, "qbsp did not find the texture WAD"
|
|
faces.append({"plane": list(H.face_plane(b, f)), "pts": [c for p in H.face_points(b, f) for c in p],
|
|
"tex": name, "vecs": list(ti[0:8]), "size": [w, h]})
|
|
models[str(mi)] = faces
|
|
return models
|
|
|
|
|
|
def generate(ctx):
|
|
H = load(ctx, "_qbsp")
|
|
B = load(ctx, "brush")
|
|
rng = ctx.rng(4242)
|
|
world, door = layout(rng)
|
|
out = {}
|
|
wad2 = H.wad([(n, 0x44, H.miptex(n, w, h, rng)) for n, w, h in TEX])
|
|
pal = bytes(rng.randrange(256) for _ in range(768))
|
|
wad3 = H.wad([(n, 0x43, H.miptex(n, w, h, rng, pal)) for n, w, h in TEX], b"WAD3")
|
|
sizes = {n: [w, h] for n, w, h in TEX}
|
|
# quake
|
|
text = write(B, "quake", world, door, rng, "gfx/tex.wad")
|
|
out["quake"] = {"text": text, "files": {"gfx/tex.wad": wad2}, "models": compile_faces(H, ctx, text.replace("gfx/tex.wad", "tex.wad"), {"tex.wad": wad2}), "sizes": sizes}
|
|
# valve 220 with a WAD3 under a Windows path
|
|
text = write(B, "valve", world, door, rng, "\\half-life\\valve\\tex3.wad")
|
|
qtext = text.replace("\\half-life\\valve\\tex3.wad", "tex3.wad")
|
|
out["valve"] = {"text": text, "files": {"tex3.wad": wad3}, "models": compile_faces(H, ctx, qtext, {"tex3.wad": wad3}), "sizes": sizes}
|
|
# quake 2: .wal sizes (qbsp is given the same sizes through a WAD of the same names)
|
|
text = write(B, "quake2", world, door, rng, None)
|
|
wals = {}
|
|
for n, w, h in TEX:
|
|
wals["textures/" + n + ".wal"] = (n.encode() + b"\0" * (32 - len(n))) + w.to_bytes(4, "little") + h.to_bytes(4, "little") + bytes(60)
|
|
qtext = text.replace('"message" "fixture room"', '"message" "fixture room"\n"wad" "tex.wad"')
|
|
out["quake2"] = {"text": text, "files": wals, "models": compile_faces(H, ctx, qtext, {"tex.wad": wad2}), "sizes": sizes}
|
|
# quake 3: brushDef + a 5 x 3 patch
|
|
W, Hh = 5, 3
|
|
ctrl = []
|
|
for i in range(Hh):
|
|
for j in range(W):
|
|
ctrl.append([-200 + j * 40, 100 + i * 50, 64 + (48 if i == 1 else 0) + (24 if j % 2 else 0), j / 4, i / 2])
|
|
rows = []
|
|
for j in range(W): # file order: column by column
|
|
rows.append("( " + " ".join("( %s )" % " ".join(B.num(x) for x in ctrl[i * W + j]) for i in range(Hh)) + " )")
|
|
patch = "{\npatchDef2\n{\nbase/curve\n( %d %d 0 0 0 )\n(\n%s\n)\n}\n}" % (W, Hh, "\n".join(rows))
|
|
text = write(B, "quake3", world, door, rng, "tex.wad", patch)
|
|
plain = text.replace(patch + "\n", "")
|
|
grid, gw, gh = bezier_patch(W, Hh, ctrl, 4)
|
|
out["quake3"] = {"text": text, "files": {"tex.wad": wad2}, "models": compile_faces(H, ctx, plain, {"tex.wad": wad2}), "sizes": sizes,
|
|
"patch": {"grid": [c for p in grid for c in p], "w": gw, "h": gh}}
|
|
ctx.emit("map", out)
|