184 lines
7.0 KiB
Python
184 lines
7.0 KiB
Python
# Wolfenstein 3D fixtures (tools/fixtures.py). id's data and source are not redistributable / GPL, so everything is
|
|
# written here by this script's own writers from the ModdingWiki format descriptions: a random 6-bit palette; a VSWAP
|
|
# with walls, sprites (t_compshape posts, with gaps), sound pages and the sound info page; a GAMEMAPS + MAPHEAD pair
|
|
# with two maps (slots 1 and 3) whose planes are RLEW then Carmack compressed (near and far copies, literal 0xA7xx /
|
|
# 0xA8xx words, RLEW runs and a literal tag value). The references are the source pixels / tiles: CRC-32 of the RGBA
|
|
# (6-bit values scaled as (v << 2) | (v >> 4), sprite holes 0) and of the tiles as u16 LE.
|
|
# tests/gen/wolf3d.luau { palette, vswap = { data, walls = { crc }, sprites = { crc }, sounds = { { bytes, crc } } },
|
|
# gamemaps, maphead, maps = { { slot, name, width, height, planes = { crc } } } }
|
|
import struct, zlib
|
|
|
|
TAG = 0xABCD
|
|
|
|
|
|
def rgba(pal, idx, holes=None):
|
|
out = bytearray()
|
|
for k, i in enumerate(idx):
|
|
if holes is not None and holes[k]:
|
|
out += b"\0\0\0\0"
|
|
else:
|
|
r, g, b = (pal[i * 3 + c] for c in range(3))
|
|
out += bytes([(r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4), 255])
|
|
return zlib.crc32(bytes(out))
|
|
|
|
|
|
def sprite(rng):
|
|
# columns left..right, each with 1-2 posts; pixel data first, then the column post lists
|
|
left, right = rng.randint(0, 10), rng.randint(40, 63)
|
|
img = [None] * 4096 # row-major y * 64 + x: palette index, None = hole
|
|
pix = bytearray()
|
|
cols = []
|
|
hdr = 4 + (right - left + 1) * 2
|
|
for x in range(left, right + 1):
|
|
posts = []
|
|
y = rng.randint(0, 8)
|
|
for _ in range(rng.randint(1, 2)):
|
|
y0 = y
|
|
y1 = min(64, y0 + rng.randint(4, 30))
|
|
if y0 >= y1:
|
|
break
|
|
base = hdr + len(pix)
|
|
for yy in range(y0, y1):
|
|
v = rng.randrange(256)
|
|
img[yy * 64 + x] = v
|
|
pix.append(v)
|
|
posts.append((y1 * 2, base - y0, y0 * 2))
|
|
y = y1 + rng.randint(2, 10)
|
|
if y >= 60:
|
|
break
|
|
cols.append(posts)
|
|
body = bytearray(pix)
|
|
ofs = []
|
|
for posts in cols:
|
|
ofs.append(hdr + len(body))
|
|
for e, s, st in posts:
|
|
body += struct.pack("<HhH", e, s, st)
|
|
body += struct.pack("<H", 0)
|
|
data = struct.pack("<HH", left, right) + b"".join(struct.pack("<H", o) for o in ofs) + bytes(body)
|
|
return data, img
|
|
|
|
|
|
def vswap(rng, pal):
|
|
walls, sprites, sounds = [], [], []
|
|
for _ in range(3):
|
|
cm = bytes(rng.randrange(256) for _ in range(4096)) # column-major x * 64 + y
|
|
walls.append((cm, rgba(pal, [cm[x * 64 + y] for y in range(64) for x in range(64)])))
|
|
for _ in range(3):
|
|
data, img = sprite(rng)
|
|
sprites.append((data, rgba(pal, [v or 0 for v in img], [v is None for v in img])))
|
|
pages = [w for w, _ in walls] + [s for s, _ in sprites]
|
|
ss, so = len(walls), len(walls) + len(sprites)
|
|
# sound 1: one page; sound 2: two pages (4096 + 1000)
|
|
snd = [bytes(rng.randrange(256) for _ in range(700)), bytes(rng.randrange(256) for _ in range(5096))]
|
|
spages, info = [], b""
|
|
for s in snd:
|
|
info += struct.pack("<HH", len(spages), len(s))
|
|
for k in range(0, len(s), 4096):
|
|
spages.append(s[k:k + 4096])
|
|
pages += spages + [info]
|
|
n = len(pages)
|
|
# a sparse page (offset 0) after the walls is legal: the second wall page stays, an extra sprite slot is sparse
|
|
head = struct.pack("<HHH", n, ss, so)
|
|
at = 6 + n * 6
|
|
at = (at + 511) // 512 * 512
|
|
offs, body = [], bytearray()
|
|
for p in pages:
|
|
offs.append(at + len(body))
|
|
body += p
|
|
body += b"\0" * ((-len(body)) % 512)
|
|
out = head + b"".join(struct.pack("<I", o) for o in offs) + b"".join(struct.pack("<H", len(p)) for p in pages)
|
|
out += b"\0" * (at - len(out)) + bytes(body)
|
|
return out, {"walls": [c for _, c in walls], "sprites": [c for _, c in sprites],
|
|
"sounds": [{"bytes": len(s), "first": s[0], "last": s[-1]} for s in snd]}
|
|
|
|
|
|
def rlew(words):
|
|
out = []
|
|
i = 0
|
|
while i < len(words):
|
|
j = i
|
|
while j < len(words) and words[j] == words[i] and j - i < 0xFFFF:
|
|
j += 1
|
|
if j - i > 3 or words[i] == TAG:
|
|
out += [TAG, j - i, words[i]]
|
|
else:
|
|
out += words[i:j]
|
|
i = j
|
|
return out
|
|
|
|
|
|
def carmack(words):
|
|
out = bytearray()
|
|
pos = {}
|
|
i = 0
|
|
while i < len(words):
|
|
best, bo = 0, 0
|
|
for cand in reversed(pos.get((words[i], words[i + 1]) if i + 1 < len(words) else None, [])[-32:]):
|
|
k = 0
|
|
while i + k < len(words) and words[cand + k] == words[i + k] and k < 255 and cand + k < i:
|
|
k += 1
|
|
if k > best:
|
|
best, bo = k, cand
|
|
if best >= 2:
|
|
if i - bo <= 255:
|
|
out += struct.pack("<HB", 0xA700 | best, i - bo)
|
|
else:
|
|
out += struct.pack("<HH", 0xA800 | best, bo)
|
|
step = best
|
|
else:
|
|
w = words[i]
|
|
if w >> 8 in (0xA7, 0xA8):
|
|
out += struct.pack("<HB", w & 0xFF00, w & 0xFF)
|
|
else:
|
|
out += struct.pack("<H", w)
|
|
step = 1
|
|
for k in range(i, i + step):
|
|
if k + 1 < len(words):
|
|
pos.setdefault((words[k], words[k + 1]), []).append(k)
|
|
i += step
|
|
return bytes(out)
|
|
|
|
|
|
def plane(words):
|
|
r = [len(words) * 2] + rlew(words)
|
|
return struct.pack("<H", len(r) * 2) + carmack(r)
|
|
|
|
|
|
def grid(rng, w, h, k):
|
|
g = []
|
|
for y in range(h):
|
|
for x in range(w):
|
|
if k == 0:
|
|
v = 1 if x in (0, w - 1) or y in (0, h - 1) else (rng.choice([108, 108, 108, 2, 90]) if rng.random() < 0.2 else 108)
|
|
elif k == 1:
|
|
v = rng.choice([0] * 20 + [19, 23, 0xA7FF, 0xA801, TAG])
|
|
else:
|
|
v = 0
|
|
g.append(v)
|
|
return g
|
|
|
|
|
|
def generate(ctx):
|
|
rng = ctx.rng(64)
|
|
pal = bytes(rng.randrange(64) for _ in range(768))
|
|
vs, vref = vswap(rng, pal)
|
|
maps, body = [], bytearray(b"TED5v1.0")
|
|
heads = [0] * 100
|
|
for slot, (name, w, h) in [(1, ("Wolf1 Map1", 64, 64)), (3, ("Test map", 20, 12))]:
|
|
planes, crcs = [], []
|
|
for k in range(3):
|
|
g = grid(rng, w, h, k)
|
|
crcs.append(zlib.crc32(struct.pack("<%dH" % len(g), *g)))
|
|
planes.append(plane(g))
|
|
starts = []
|
|
for p in planes:
|
|
starts.append(len(body))
|
|
body += p
|
|
heads[slot - 1] = len(body)
|
|
body += struct.pack("<iiiHHHHH16s", *starts, *(len(p) for p in planes), w, h, name.encode())
|
|
body += b"!ID!"
|
|
maps.append({"slot": slot, "name": name, "width": w, "height": h, "planes": crcs})
|
|
heads[1] = -1
|
|
maphead = struct.pack("<H100i", TAG, *heads)
|
|
ctx.emit("wolf3d", {"palette": pal, "vswap": dict(vref, data=vs), "gamemaps": bytes(body), "maphead": maphead, "maps": maps})
|