367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
109 lines
4.6 KiB
Python
109 lines
4.6 KiB
Python
"""Native 70334 ADT obj1 (outdoor object list) from a converted obj0.
|
|
|
|
REA (runtime/continuation-20261112/obj0/rea-obj1*.private.json, native
|
|
Kalimdor): every obj0 has an obj1 with MLDD (the MDDF entries, flags | 0x20,
|
|
300/300 tiles same count), MLDX (per entry world AABB + radius, sorted by radius
|
|
descending 300/300), MLMD (the MODF entries without extents, 300/300) and MLMX
|
|
(world AABB + radius). radius is the largest box dimension (132/132 WMO).
|
|
The WMO box is the MODF extents in world axes (132/132); the M2 box is the
|
|
model header bounding box (MD20 +0xA0) placed with scale, heading rot[1]+180
|
|
about up, then rot[0] about y and rot[2] about x (median error 0.004 yd,
|
|
2098 doodads). Converted tiles pointed at an empty obj1 stub: their buildings
|
|
and doodads collided and rendered through interior portals but were not drawn
|
|
outdoors (Windhorn Canyon 820). MLFD/MLDL (optional LOD ranges, 53/122 and
|
|
31/300 native tiles with objects) are not written.
|
|
"""
|
|
import math
|
|
import struct
|
|
|
|
MAP_HALF = 17066.666
|
|
LOD_FLAG = 0x20
|
|
|
|
|
|
def _chunks(raw):
|
|
at, out = 0, []
|
|
while at + 8 <= len(raw):
|
|
size = struct.unpack_from('<I', raw, at + 4)[0]
|
|
out.append((raw[at:at + 4], raw[at + 8:at + 8 + size]))
|
|
at += 8 + size
|
|
if at != len(raw):
|
|
raise ValueError('Trailing bytes after obj0 chunks')
|
|
return out
|
|
|
|
|
|
def _chunk(tag, data):
|
|
return tag[::-1] + struct.pack('<I', len(data)) + data
|
|
|
|
|
|
def _rotation(rot):
|
|
rx, ry, rz = (math.radians(v) for v in rot)
|
|
ry += math.pi
|
|
cz, sz, cy, sy, cx, sx = math.cos(ry), math.sin(ry), math.cos(rx), math.sin(rx), math.cos(rz), math.sin(rz)
|
|
heading = ((cz, -sz, 0), (sz, cz, 0), (0, 0, 1))
|
|
pitch = ((cy, 0, sy), (0, 1, 0), (-sy, 0, cy))
|
|
roll = ((1, 0, 0), (0, cx, -sx), (0, sx, cx))
|
|
|
|
def mul(a, b):
|
|
return tuple(tuple(sum(a[i][k] * b[k][j] for k in range(3)) for j in range(3)) for i in range(3))
|
|
return mul(mul(heading, pitch), roll)
|
|
|
|
|
|
def doodad_world_box(position, rotation, scale, model_box):
|
|
"""World AABB of a placed M2 (position/rotation in ADT file convention)."""
|
|
m, s = _rotation(rotation), scale / 1024
|
|
origin = (MAP_HALF - position[2], MAP_HALF - position[0], position[1])
|
|
low, high = [math.inf] * 3, [-math.inf] * 3
|
|
for x in (model_box[0], model_box[3]):
|
|
for y in (model_box[1], model_box[4]):
|
|
for z in (model_box[2], model_box[5]):
|
|
for i in range(3):
|
|
v = origin[i] + s * (m[i][0] * x + m[i][1] * y + m[i][2] * z)
|
|
low[i] = min(low[i], v)
|
|
high[i] = max(high[i], v)
|
|
return tuple(low + high)
|
|
|
|
|
|
def extents_world_box(extents):
|
|
"""MODF extents (ADT file axes) as a world AABB."""
|
|
return (MAP_HALF - extents[5], MAP_HALF - extents[3], extents[1],
|
|
MAP_HALF - extents[2], MAP_HALF - extents[0], extents[4])
|
|
|
|
|
|
def _radius(box):
|
|
return max(box[3] - box[0], box[4] - box[1], box[5] - box[2])
|
|
|
|
|
|
def build_obj1(obj0, model_box):
|
|
"""Return (obj1 bytes, audit). model_box(file_data_id) -> 6 floats or None."""
|
|
chunks = dict(_chunks(obj0))
|
|
if struct.unpack('<I', chunks[b'REVM'])[0] != 18:
|
|
raise ValueError('ADT version 18 required')
|
|
mddf, modf = chunks.get(b'FDDM', b''), chunks.get(b'FDOM', b'')
|
|
if len(mddf) % 36 or len(modf) % 64:
|
|
raise ValueError('Placement chunk size')
|
|
doodads, held = [], []
|
|
for i in range(len(mddf) // 36):
|
|
entry = mddf[36 * i:36 * i + 36]
|
|
name, _unique, px, py, pz, rx, ry, rz, scale, flags = struct.unpack('<II3f3fHH', entry)
|
|
if not flags & 0x40:
|
|
raise ValueError('MDDF entry without file-data-id flag')
|
|
box = model_box(name)
|
|
if box is None:
|
|
held.append(name)
|
|
continue
|
|
world = doodad_world_box((px, py, pz), (rx, ry, rz), scale, box)
|
|
doodads.append((_radius(world), i, entry[:34] + struct.pack('<H', flags | LOD_FLAG), world))
|
|
objects = []
|
|
for i in range(len(modf) // 64):
|
|
entry = modf[64 * i:64 * i + 64]
|
|
world = extents_world_box(struct.unpack_from('<6f', entry, 32))
|
|
objects.append((_radius(world), i, entry[:32] + entry[56:64], world))
|
|
doodads.sort(key=lambda d: (-d[0], d[1]))
|
|
objects.sort(key=lambda d: (-d[0], d[1]))
|
|
out = _chunk(b'MVER', struct.pack('<I', 18))
|
|
out += _chunk(b'MLDD', b''.join(d[2] for d in doodads))
|
|
out += _chunk(b'MLDX', b''.join(struct.pack('<7f', *d[3], d[0]) for d in doodads))
|
|
out += _chunk(b'MLMD', b''.join(o[2] for o in objects))
|
|
out += _chunk(b'MLMX', b''.join(struct.pack('<7f', *o[3], o[0]) for o in objects))
|
|
return out, dict(doodads=len(doodads), objects=len(objects), held_models=sorted(set(held)))
|