427 lines
24 KiB
Python
427 lines
24 KiB
Python
"""Source-only terrain WDT composer and absent-doodad WMO root policy (synthetic)."""
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'ops'))
|
|
import source_map_wdt as smw # noqa: E402
|
|
|
|
|
|
def tile(base):
|
|
return dict(ROOT=base, OBJ0=base + 1, OBJ1=smw.EMPTY_OBJ1, TEX0=base + 2)
|
|
|
|
|
|
class SourceMapWdtTest(unittest.TestCase):
|
|
def test_native_terrain_profile_roundtrip(self):
|
|
tiles = {(37, 45): tile(100), (38, 46): dict(tile(200), MINIMAP=7)}
|
|
raw, audit = smw.compose_terrain_wdt(tiles)
|
|
self.assertEqual(len(raw), 8 + 4 + 8 + 32 + 8 + 32768 + 8 + 131072)
|
|
self.assertEqual(raw[:4], b'REVM')
|
|
back = smw.parse_wdt(raw)
|
|
self.assertEqual((back['version'], back['flags']), (18, 0x248))
|
|
self.assertEqual(set(back['globals'].values()), {0})
|
|
self.assertEqual(set(back['tiles']), set(tiles))
|
|
row = back['tiles'][(38, 46)]
|
|
self.assertEqual((row['rootADT'], row['obj0ADT'], row['obj1ADT'], row['tex0ADT'],
|
|
row['lodADT'], row['mapTexture'], row['mapTextureN'], row['minimapTexture']),
|
|
(200, 201, smw.EMPTY_OBJ1, 202, 0, 0, 0, 7))
|
|
main = raw.index(b'NIAM') + 8
|
|
states = {struct.unpack_from('<II', raw, main + i * 8) for i in range(4096)}
|
|
self.assertEqual(states, {(1, 0), (2, 0)})
|
|
self.assertEqual(audit['active_tiles'], [[37, 45], [38, 46]])
|
|
|
|
def test_globals_and_refusals(self):
|
|
raw, _ = smw.compose_terrain_wdt({(1, 2): tile(5)}, flags=0x24a,
|
|
global_file_data_ids={'wdl': 9, 'tex': 8})
|
|
back = smw.parse_wdt(raw)
|
|
self.assertEqual((back['flags'], back['globals']['wdl'], back['globals']['tex'],
|
|
back['globals']['lgt']), (0x24a, 9, 8, 0))
|
|
bad = [dict(tiles={(1, 2): dict(ROOT=1, OBJ0=2, TEX0=3)}), # OBJ1 missing
|
|
dict(tiles={(1, 2): dict(tile(5), LOD=4)}), # unreviewed role
|
|
dict(tiles={(1, 2): dict(tile(5), ROOT=0)}), # zero ID
|
|
dict(tiles={(64, 0): tile(5)}), # coordinate
|
|
dict(tiles={(1, 2): tile(5)}, flags=0x241), # WMO-only profile
|
|
dict(tiles={(1, 2): tile(5)}, global_file_data_ids={'x': 1}),
|
|
dict(tiles={})]
|
|
for kw in bad:
|
|
with self.assertRaises(ValueError):
|
|
smw.compose_terrain_wdt(kw.pop('tiles'), **kw)
|
|
|
|
def test_absent_doodads_written_as_zero(self):
|
|
modn = b'a.mdx\0b.mdx\0'
|
|
raw = b'NDOM' + struct.pack('<I', len(modn)) + modn
|
|
seen = {}
|
|
|
|
def fake(raw_, path, *, texture_bindings, group_bindings, doodad_bindings, referenced_doodads_only):
|
|
seen.update(doodad_bindings)
|
|
ids = [doodad_bindings['a.mdx'], doodad_bindings['b.mdx']]
|
|
return _root(MOHD=_mohd(nDoodadNames=2), MODI=struct.pack('<2I', *ids), MODD=bytes(40)), dict(doodad_resources=2)
|
|
original = smw.terrain.classic_wmo_numeric_candidate
|
|
smw.terrain.classic_wmo_numeric_candidate = fake
|
|
try:
|
|
out, audit = smw.numeric_wmo_root(raw, 'x.wmo', texture_bindings={}, group_bindings=[1],
|
|
doodad_bindings={'a.mdx': 4242})
|
|
finally:
|
|
smw.terrain.classic_wmo_numeric_candidate = original
|
|
self.assertEqual(struct.unpack_from('<2I', out, 8 + 64 + 8), (4242, 0))
|
|
self.assertTrue(audit['MOHD_counts_from_emitted_chunks'])
|
|
self.assertEqual(audit['absent_doodads'], ['b.mdx'])
|
|
self.assertEqual(audit['zero_scale_doodad_rows'], [0])
|
|
self.assertFalse(audit['source_doodad_transform_and_upper_flags_preserved'])
|
|
self.assertGreaterEqual(seen['b.mdx'], 0xfffff000)
|
|
|
|
def test_mohd_counts_derived_from_emitted_chunks(self):
|
|
# Uldaman shape: MOHD declares more doodad defs/sets than MODD/MODS hold.
|
|
raw = _root(MOHD=_mohd(nTextures=1, nGroups=2, nPortals=1, nLights=1, nDoodadNames=2,
|
|
nDoodadDefs=5, nDoodadSets=6),
|
|
MOMT=bytes(64), MOGI=bytes(64), GFID=struct.pack('<2I', 7, 8),
|
|
MOPT=bytes(20), MOPR=bytes(8), MOLT=bytes(48),
|
|
MODI=struct.pack('<2I', 11, 12), MODS=bytes(20) + struct.pack('<II', 0, 3) + bytes(4),
|
|
MODD=bytes(120))
|
|
before = smw.wmo_root_count_audit(raw)['mismatches']
|
|
self.assertEqual(before, {'nDoodadDefs': {'MOHD': 5, 'chunk': 3}, 'nDoodadSets': {'MOHD': 6, 'chunk': 1}})
|
|
fixed = smw.normalize_wmo_root_counts(raw)
|
|
self.assertEqual(smw.wmo_root_count_audit(fixed)['mismatches'], {})
|
|
self.assertEqual(struct.unpack_from('<7I', fixed, 8),
|
|
(1, 2, 1, 1, 2, 3, 1))
|
|
self.assertEqual([i for i in range(len(raw)) if raw[i] != fixed[i]], [8 + 20, 8 + 24])
|
|
# A doodad set reaching past MODD is not a header problem: refuse instead of hiding it.
|
|
bad = raw.replace(struct.pack('<II', 0, 3), struct.pack('<II', 1, 3))
|
|
with self.assertRaises(ValueError):
|
|
smw.normalize_wmo_root_counts(bad)
|
|
|
|
def test_legacy_group_liquid_root_clears_only_liquid_id_flag(self):
|
|
mohd = bytearray(_mohd(nGroups=1))
|
|
struct.pack_into('<H', mohd, 60, 0x4 | 0x1 | 0x8)
|
|
raw = _root(MOHD=bytes(mohd), MOGI=bytes(32), GFID=struct.pack('<I', 7))
|
|
fixed, changed = smw.legacy_group_liquid_root(raw)
|
|
self.assertTrue(changed)
|
|
self.assertEqual(struct.unpack_from('<H', fixed, 8 + 60)[0], 0x9)
|
|
self.assertEqual([i for i in range(len(raw)) if raw[i] != fixed[i]], [8 + 60])
|
|
again, changed = smw.legacy_group_liquid_root(fixed)
|
|
self.assertFalse(changed)
|
|
self.assertIs(again, fixed)
|
|
|
|
def test_native_mogi_flags_drop_group_data_bits(self):
|
|
# Source MOGI copies MOGP (0x1049 exterior+BSP+MLIQ, 0x2005 indoor); native keeps 0x48/0x2000.
|
|
mogi = b''.join(struct.pack('<I', f) + bytes(28) for f in (0x1849, 0x2005, 0x80000408))
|
|
raw = _root(MOHD=_mohd(nGroups=3), MOGI=mogi, GFID=struct.pack('<3I', 7, 8, 9))
|
|
fixed, changed = smw.native_mogi_flags(raw)
|
|
self.assertEqual(changed, 2)
|
|
at = fixed.index(b'IGOM') + 8
|
|
self.assertEqual([struct.unpack_from('<I', fixed, at + 32 * i)[0] for i in range(3)],
|
|
[0x48, 0x2000, 0x80000408])
|
|
self.assertEqual(len(fixed), len(raw))
|
|
again, changed = smw.native_mogi_flags(fixed)
|
|
self.assertEqual(changed, 0)
|
|
self.assertIs(again, fixed)
|
|
|
|
def test_native_single_lod_root_clears_numlod_only(self):
|
|
mohd = bytearray(_mohd(nGroups=2))
|
|
struct.pack_into('<HH', mohd, 60, 0, 1)
|
|
raw = _root(MOHD=bytes(mohd), MOGI=bytes(64), GFID=struct.pack('<2I', 7, 8))
|
|
fixed, changed = smw.native_single_lod_root(raw)
|
|
self.assertTrue(changed)
|
|
self.assertEqual(struct.unpack_from('<HH', fixed, 8 + 60), (0, 0))
|
|
self.assertEqual([i for i in range(len(raw)) if raw[i] != fixed[i]], [8 + 62])
|
|
# Native forms are kept: LOD-flagged multi-set roots and 0x5/numLod 1 roots.
|
|
for flags, lods, gfid in ((0x1f, 3, 6), (0x5, 1, 2)):
|
|
struct.pack_into('<HH', mohd, 60, flags, lods)
|
|
keep = _root(MOHD=bytes(mohd), MOGI=bytes(64), GFID=bytes(4 * gfid))
|
|
self.assertIs(smw.native_single_lod_root(keep)[0], keep)
|
|
|
|
def test_native_mogi_bounds_copy_group_boxes(self):
|
|
whole = struct.pack('<6f', -966, -141, 50, 88, 315, 291)
|
|
own = struct.pack('<6f', -830, -140, 63, -685, 137, 277)
|
|
mogi = struct.pack('<I', 0x48) + whole + struct.pack('<i', 2) + struct.pack('<I', 0x48) + own + struct.pack('<i', 9)
|
|
raw = _root(MOHD=_mohd(nGroups=2), MOGI=mogi, GFID=struct.pack('<2I', 7, 8))
|
|
group = _root(MVER=struct.pack('<I', 17), MOGP=bytes(12) + own + bytes(32))
|
|
fixed, changed = smw.native_mogi_bounds(raw, {0: group, 1: group})
|
|
self.assertEqual(changed, 1)
|
|
at = fixed.index(b'IGOM') + 8
|
|
self.assertEqual(fixed[at + 4:at + 28], own)
|
|
self.assertEqual(fixed[at + 32:], raw[at + 32:])
|
|
self.assertEqual(fixed[:at + 4], raw[:at + 4])
|
|
self.assertIs(smw.native_mogi_bounds(fixed, {0: group})[0], fixed)
|
|
|
|
def test_mogp_batch_counts_rewrite_only_cumulative_exterior(self):
|
|
# Converted 8660732 group: Source wrote total MOBA entries into ext.
|
|
for trans, interior, total in ((4, 18, 22), (0, 19, 19), (3, 0, 3)):
|
|
header = bytearray(68)
|
|
struct.pack_into('<3H', header, 0x28, trans, interior, total)
|
|
raw = _root(MVER=struct.pack('<I', 17),
|
|
MOGP=bytes(header) + _root(MOBA=bytes(24 * total)))
|
|
fixed, changed = smw.normalize_mogp_batch_counts(raw)
|
|
self.assertTrue(changed)
|
|
at = fixed.index(b'PGOM') + 8
|
|
self.assertEqual(struct.unpack_from('<3H', fixed, at + 0x28),
|
|
(trans, interior, total - trans - interior))
|
|
self.assertEqual([i for i in range(len(raw)) if raw[i] != fixed[i]],
|
|
[i for i in (at + 0x2c, at + 0x2d) if raw[i] != fixed[i]])
|
|
self.assertEqual(smw.normalize_mogp_batch_counts(fixed), (fixed, False))
|
|
header = bytearray(68)
|
|
struct.pack_into('<3H', header, 0x28, 1, 1, 1)
|
|
with self.assertRaises(ValueError):
|
|
smw.normalize_mogp_batch_counts(_root(MOGP=bytes(header) + _root(MOBA=bytes(24 * 5))))
|
|
no_batches = _root(MOGP=bytes(68) + _root(MOVI=bytes(6)))
|
|
self.assertEqual(smw.normalize_mogp_batch_counts(no_batches), (no_batches, False))
|
|
|
|
def test_mogp_inner_collision_gap_moves_to_tail_and_remaps_bsp(self):
|
|
triangles = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]
|
|
movi = b''.join(struct.pack('<3H', *triangle) for triangle in triangles)
|
|
mopy = bytes((0x20, 1, 0x48, 255, 0x20, 2, 0x48, 255, 0x48, 255))
|
|
batch0, batch1 = bytearray(range(24)), bytearray(range(24, 48))
|
|
struct.pack_into('<IH', batch0, 12, 0, 3)
|
|
struct.pack_into('<IH', batch1, 12, 6, 3)
|
|
mobr = struct.pack('<4H', 0, 1, 2, 4)
|
|
nested = (_root(MOVI=movi, MOPY=mopy, MOBA=bytes(batch0 + batch1),
|
|
MOBN=bytes(range(16)), MOBR=mobr) +
|
|
_root(MOTV=b'first') + _root(MOTV=b'second'))
|
|
raw = _root(MVER=struct.pack('<I', 17), MOGP=bytes(68) + nested)
|
|
fixed, audit = smw.normalize_mogp_batch_gaps(raw)
|
|
self.assertEqual(audit, dict(changed=True, triangles=5, batches=2,
|
|
moved_triangles=1, mobr_references=4))
|
|
self.assertEqual(len(fixed), len(raw))
|
|
group = dict(smw._chunk_list(fixed))['MOGP']
|
|
parts = dict(smw._chunk_list(group[68:]))
|
|
self.assertEqual(parts['MOVI'], b''.join(struct.pack('<3H', *triangles[i])
|
|
for i in (0, 2, 1, 3, 4)))
|
|
self.assertEqual(parts['MOPY'], bytes((0x20, 1, 0x20, 2, 0x48, 255,
|
|
0x48, 255, 0x48, 255)))
|
|
self.assertEqual(struct.unpack('<4H', parts['MOBR']), (0, 2, 1, 4))
|
|
self.assertEqual(parts['MOBN'], bytes(range(16)))
|
|
self.assertEqual([body for tag, body in smw._chunk_list(group[68:])
|
|
if tag == 'MOTV'], [b'first', b'second'])
|
|
self.assertEqual([struct.unpack_from('<IH', parts['MOBA'], 24 * i + 12)
|
|
for i in range(2)], [(0, 3), (3, 3)])
|
|
self.assertEqual(smw.normalize_mogp_batch_gaps(fixed)[0], fixed)
|
|
no_batches = _root(MOGP=bytes(68) + _root(MOVI=movi, MOPY=mopy))
|
|
self.assertEqual(smw.normalize_mogp_batch_gaps(no_batches)[0], no_batches)
|
|
bad = bytearray(raw)
|
|
at = bad.index(b'YPOM') + 8 + 2 + 1 # material byte of inner unbatched triangle
|
|
bad[at] = 4
|
|
with self.assertRaises(ValueError):
|
|
smw.normalize_mogp_batch_gaps(bytes(bad))
|
|
|
|
def test_native_wmo_liquid_group_preserves_pool_and_sets_id_mode_fields(self):
|
|
header = bytearray(68)
|
|
struct.pack_into('<I', header, 8, 0x1000)
|
|
struct.pack_into('<I', header, 0x34, 15)
|
|
uv = struct.pack('<2h', 278, 436)
|
|
heights = (-24.5,) * 8 + (0.0,)
|
|
vertices = b''.join(uv + struct.pack('<f', z) for z in heights)
|
|
liquid = struct.pack('<4I3fH', 3, 3, 2, 2, 10.0, 20.0, 0.0, 1) + vertices + bytes((0x43, 0x53, 0x6F, 0xAF))
|
|
group = _root(MVER=struct.pack('<I', 17), MOGP=bytes(header) + _root(MLIQ=liquid))
|
|
|
|
fixed, audit = smw.emit_native_wmo_liquid_group(group, liquid_type_id=4, align_surface_z=True)
|
|
self.assertEqual(audit['liquid_type_id'], 4)
|
|
self.assertEqual(audit['wet_tiles'], 2)
|
|
self.assertEqual(audit['changed_tile_flags'], 4)
|
|
self.assertEqual(audit['changed_wet_tile_flags'], 2)
|
|
self.assertEqual(audit['changed_dry_tile_flags'], 2)
|
|
mogp = dict(smw._chunk_list(fixed))['MOGP']
|
|
self.assertEqual(struct.unpack_from('<I', mogp, 0x34)[0], 4)
|
|
result = dict(smw._chunk_list(mogp[68:]))['MLIQ']
|
|
self.assertEqual(struct.unpack_from('<f', result, 24)[0], -24.5)
|
|
self.assertEqual(result[30:30 + len(vertices)], vertices)
|
|
self.assertEqual(result[-4:], bytes((0x40, 0x40, 0x0F, 0x0F)))
|
|
self.assertEqual(struct.unpack_from('<4I3fH', result, 0)[:4], (3, 3, 2, 2))
|
|
self.assertEqual(smw.emit_native_wmo_liquid_group(fixed, liquid_type_id=4)[0], fixed)
|
|
with self.assertRaises(ValueError):
|
|
smw.emit_native_wmo_liquid_group(group, liquid_type_id=3)
|
|
|
|
root = _root(MOHD=_mohd(nGroups=1), GFID=struct.pack('<I', 7))
|
|
native_root, changed = smw.enable_native_wmo_liquid_ids(root)
|
|
self.assertTrue(changed)
|
|
self.assertEqual(struct.unpack_from('<H', dict(smw._chunk_list(native_root))['MOHD'], 60)[0], 4)
|
|
self.assertEqual(smw.enable_native_wmo_liquid_ids(native_root), (native_root, False))
|
|
|
|
def test_native_wmo_liquid_component_closes_shared_roots(self):
|
|
# 10 -> 20 -> 30 -> 50 is one component; 40 is disjoint.
|
|
groups = {10: (1, 2), 20: (2, 3), 30: (3, 4),
|
|
40: (5,), 50: (4, 6)}
|
|
selected, children = smw.native_wmo_liquid_component(groups, (10,))
|
|
self.assertEqual(selected, (10, 20, 30, 50))
|
|
self.assertEqual(children, (1, 2, 3, 4, 6))
|
|
flags = {root: (4 if root in selected else 0) for root in groups}
|
|
self.assertEqual(smw.validate_native_wmo_liquid_owners(groups, flags, children), 5)
|
|
flags[20] = 0
|
|
with self.assertRaises(ValueError):
|
|
smw.validate_native_wmo_liquid_owners(groups, flags, children)
|
|
with self.assertRaises(ValueError):
|
|
smw.validate_native_wmo_liquid_owners(groups, flags, (999,))
|
|
with self.assertRaises(ValueError):
|
|
smw.native_wmo_liquid_component(groups, (999,))
|
|
with self.assertRaises(ValueError):
|
|
smw.native_wmo_liquid_component({10: (1, 1)}, (10,))
|
|
|
|
def test_native_wmo_liquid_water_variants_and_shared_magma_seam(self):
|
|
def legacy_group(flags, xt, yt):
|
|
header = bytearray(68)
|
|
struct.pack_into('<I', header, 8, 0x1000)
|
|
struct.pack_into('<I', header, 0x34, 15)
|
|
xv, yv = xt + 1, yt + 1
|
|
vertices = bytes(xv * yv * 8)
|
|
liquid = struct.pack('<4I3fH', xv, yv, xt, yt, 0.0, 0.0, 0.0, 0) + vertices + bytes(flags)
|
|
return _root(MOGP=bytes(header) + _root(MLIQ=liquid)), vertices
|
|
|
|
water, vertices = legacy_group((0x44, 0x54, 0x64, 0x74, 0xC4, 0xD4, 0xE4, 0xF4), 4, 2)
|
|
fixed, audit = smw.emit_native_wmo_liquid_group(water, liquid_type_id=5)
|
|
liquid = dict(smw._chunk_list(dict(smw._chunk_list(fixed))['MOGP'][68:]))['MLIQ']
|
|
self.assertEqual(liquid[-8:], bytes((0x40, 0x50, 0x60, 0x70, 0xC0, 0xC0, 0xC0, 0xC0)))
|
|
self.assertEqual(liquid[30:30 + len(vertices)], vertices)
|
|
self.assertEqual(audit['wet_tiles'], 8)
|
|
|
|
magma, vertices = legacy_group((0x46, 0xC6, 0x76, 0xFF), 2, 2)
|
|
fixed, audit = smw.emit_native_wmo_liquid_group(magma, liquid_type_id=7)
|
|
liquid = dict(smw._chunk_list(dict(smw._chunk_list(fixed))['MOGP'][68:]))['MLIQ']
|
|
self.assertEqual(liquid[-4:], bytes((0x40, 0xC0, 0x40, 0x0F)))
|
|
self.assertEqual(liquid[30:30 + len(vertices)], vertices)
|
|
self.assertEqual((audit['wet_tiles'], audit['changed_dry_tile_flags']), (3, 1))
|
|
invalid, _ = legacy_group((0x04, 0x44, 0x44, 0x44), 2, 2)
|
|
with self.assertRaises(ValueError):
|
|
smw.emit_native_wmo_liquid_group(invalid, liquid_type_id=5)
|
|
|
|
def test_native_wmo_liquid_group_dry_and_malformed_rejection(self):
|
|
dry = bytearray(68)
|
|
struct.pack_into('<I', dry, 0x34, 15)
|
|
group = _root(MVER=struct.pack('<I', 17), MOGP=bytes(dry))
|
|
fixed, audit = smw.emit_native_wmo_liquid_group(group)
|
|
self.assertEqual(audit['liquid_type_id'], 0)
|
|
self.assertEqual(struct.unpack_from('<I', dict(smw._chunk_list(fixed))['MOGP'], 0x34)[0], 0)
|
|
native_flooded = bytearray(dry)
|
|
struct.pack_into('<I', native_flooded, 0x34, 1)
|
|
flooded = _root(MVER=struct.pack('<I', 17), MOGP=bytes(native_flooded))
|
|
self.assertEqual(smw.emit_native_wmo_liquid_group(flooded)[0], flooded)
|
|
bad = bytearray(dry)
|
|
struct.pack_into('<I', bad, 8, 0x1000)
|
|
with self.assertRaises(ValueError):
|
|
smw.emit_native_wmo_liquid_group(_root(MOGP=bytes(bad)))
|
|
|
|
def test_malp_overlap_carving_keeps_wet_cells_and_patch_attributes(self):
|
|
def patch(rect, typ):
|
|
return struct.pack('<4BHH', *rect, typ, 7) + bytes([typ]) * 24
|
|
small = patch((1, 1, 3, 3), 1)
|
|
large = patch((0, 0, 5, 5), 2)
|
|
wet = bytearray(8 * 8)
|
|
for x, y in ((1, 1), (2, 2), (4, 4)):
|
|
wet[y * 8 + x] = 1
|
|
fixed, audit = smw.normalize_malp_rectangles(small + large, wet, grid=8)
|
|
self.assertEqual(audit, dict(patches_before=2, overlap_pairs_before=1,
|
|
patches_after=2, wet_cells_preserved=True))
|
|
self.assertEqual(fixed, small + patch((4, 4, 5, 5), 2))
|
|
self.assertEqual(smw.normalize_malp_rectangles(fixed, wet, grid=8)[0], fixed)
|
|
with self.assertRaises(ValueError):
|
|
smw.normalize_malp_rectangles(small + b'!', wet, grid=8)
|
|
|
|
def test_native_modf_scale_sets_unit_scale_only_where_zero(self):
|
|
a = bytearray(64); b = bytearray(64); struct.pack_into('<HHHH', b, 56, 0xc, 0, 0, 2048)
|
|
raw = b'REVM' + struct.pack('<I', 4) + struct.pack('<I', 18) + b'FDOM' + struct.pack('<I', 128) + bytes(a) + bytes(b)
|
|
fixed, changed = smw.native_modf_scale(raw)
|
|
self.assertEqual(changed, 1)
|
|
self.assertEqual(struct.unpack_from('<H', fixed, 12 + 8 + 62)[0], 1024)
|
|
self.assertEqual(struct.unpack_from('<H', fixed, 12 + 8 + 64 + 62)[0], 2048)
|
|
self.assertEqual(struct.unpack_from('<H', fixed, 12 + 8 + 56)[0], 0x4)
|
|
self.assertEqual([i for i in range(len(raw)) if raw[i] != fixed[i]], [12 + 8 + 56, 12 + 8 + 63])
|
|
self.assertEqual(smw.native_modf_scale(fixed), (fixed, 0))
|
|
|
|
def test_zero_scale_wmo_doodads_keep_all_other_placement_bytes(self):
|
|
rows = bytearray(range(120))
|
|
for i, scale in enumerate((1.25, -0.0, 0.0)):
|
|
struct.pack_into('<f', rows, 40 * i + 32, scale)
|
|
raw = _root(MOHD=_mohd(nDoodadDefs=3), MODD=rows)
|
|
fixed, changed = smw.native_wmo_doodad_scales(raw)
|
|
expected = bytearray(rows)
|
|
for i in (1, 2):
|
|
struct.pack_into('<f', expected, 40 * i + 32, 0.001)
|
|
self.assertEqual(changed, [1, 2])
|
|
self.assertEqual(fixed, _root(MOHD=_mohd(nDoodadDefs=3), MODD=expected))
|
|
self.assertEqual(smw.native_wmo_doodad_scales(fixed), (fixed, []))
|
|
|
|
def test_native_observed_tuple_admission_is_scoped(self):
|
|
allowed = smw.terrain._WMO_STRUCTURAL_MATERIAL_TUPLES
|
|
seen = {}
|
|
|
|
def fake(raw_, path, **kw):
|
|
seen['allowed'] = smw.terrain._WMO_STRUCTURAL_MATERIAL_TUPLES
|
|
seen['profile'] = kw.get('legacy_material_profile')
|
|
return _root(MOHD=_mohd()), {}
|
|
original = smw.terrain.classic_wmo_numeric_candidate
|
|
smw.terrain.classic_wmo_numeric_candidate = fake
|
|
try:
|
|
smw.numeric_wmo_root(b'', 'x.wmo', texture_bindings={}, group_bindings=[], doodad_bindings={},
|
|
native_observed_tuples={(2, 0, 0)}, legacy_material_profile='p')
|
|
with self.assertRaises(ValueError):
|
|
smw.numeric_wmo_root(b'', 'x.wmo', texture_bindings={}, group_bindings=[], doodad_bindings={},
|
|
native_observed_tuples={(9, 9, 9)})
|
|
finally:
|
|
smw.terrain.classic_wmo_numeric_candidate = original
|
|
self.assertIn((2, 0, 0), seen['allowed'])
|
|
self.assertEqual(seen['profile'], 'p')
|
|
self.assertIs(smw.terrain._WMO_STRUCTURAL_MATERIAL_TUPLES, allowed)
|
|
|
|
def test_absent_doodad_placements_omitted_with_group_refs(self):
|
|
modd = b''.join(struct.pack('<I', i) + bytes(36) for i in (0, 1, 0, 1)) # MODI[1] is 0
|
|
root = _root(MOHD=_mohd(nDoodadNames=2, nDoodadDefs=4, nDoodadSets=1), MODI=struct.pack('<2I', 77, 0),
|
|
MODS=bytes(20) + struct.pack('<II', 0, 4) + bytes(4), MODD=modd)
|
|
group = _root(MVER=struct.pack('<I', 17),
|
|
MOGP=bytes(68) + _root(MOPY=b'', MODR=struct.pack('<4H', 0, 1, 2, 3)))
|
|
new_root, groups, audit = smw.omit_absent_doodad_placements(root, {9: group})
|
|
self.assertEqual(audit['omitted_placements'], 2)
|
|
self.assertEqual(smw.wmo_root_count_audit(new_root)['actual']['nDoodadDefs'], 2)
|
|
self.assertEqual(smw.wmo_root_count_audit(new_root)['mismatches'], {})
|
|
modr = dict(smw._chunk_list(dict(smw._chunk_list(groups[9]))['MOGP'][68:]))['MODR']
|
|
self.assertEqual(struct.unpack('<2H', modr), (0, 1))
|
|
self.assertEqual(struct.unpack_from('<II', dict(smw._chunk_list(new_root))['MODS'], 20), (0, 2))
|
|
|
|
def test_empty_companions_match_native_bytes(self):
|
|
c = smw.empty_map_companions()
|
|
self.assertEqual(c['lgt'].hex(), '5245564d0400000014000000')
|
|
self.assertEqual(c['occ'].hex(), '5245564d0400000012000000494f414d00000000484f414d00000000')
|
|
self.assertEqual(c['fogs'].hex(), '5245564d0400000002000000')
|
|
self.assertEqual(c['mpv'].hex(), '5245564d0400000003000000')
|
|
self.assertEqual(len(c['wdl']), 16436)
|
|
self.assertTrue(c['wdl'].startswith(bytes.fromhex('5245564d040000001200000044444c4d00000000')))
|
|
self.assertEqual(c['tex'][:12], bytes.fromhex('525658540400000001000000'))
|
|
|
|
def test_wdl_from_root_adt_heights(self):
|
|
def mcnk(x, y):
|
|
hdr = bytearray(0x80); struct.pack_into('<II', hdr, 4, x, y)
|
|
struct.pack_into('<H', hdr, 0x3C, 0xFFFF if (x, y) == (2, 1) else 0)
|
|
struct.pack_into('<3f', hdr, 0x68, 0.0, 0.0, 100.0 + x)
|
|
heights = [float(i % 17) for i in range(145)]
|
|
return _root(MCNK=bytes(hdr) + _root(MCVT=struct.pack('<145f', *heights)))
|
|
adt = _root(MVER=struct.pack('<I', 18)) + b''.join(mcnk(x, y) for y in range(16) for x in range(16))
|
|
mare, maho = smw.wdl_cell_from_root_adt(adt)
|
|
h = struct.unpack('<545h', mare)
|
|
self.assertEqual((h[0], h[16], h[289]), (100, 123, int(100 + sum(i % 17 for i in range(145)) / 145)))
|
|
self.assertEqual(struct.unpack('<16H', maho)[1], 1 << 2)
|
|
wdl = smw.compose_wdl({(38, 47): (mare, maho)})
|
|
off = struct.unpack_from('<4096I', wdl, wdl.find(b'FOAM') + 8)
|
|
self.assertEqual(wdl[off[47 * 64 + 38]:off[47 * 64 + 38] + 4], b'ERAM')
|
|
self.assertEqual(sum(1 for o in off if o), 1)
|
|
|
|
def test_tex_roundtrip(self):
|
|
entries = {7: (0x62020, b'\x01' * 696), 3: (0x82051010, b'\x02' * 368)}
|
|
raw = smw.compose_tex(entries)
|
|
self.assertEqual(smw.parse_tex(raw), entries)
|
|
self.assertEqual(struct.unpack_from('<III', raw, 20), (3, 0, 0x82051010))
|
|
|
|
|
|
def _mohd(**counts):
|
|
words = [counts.get(n, 0) for n, *_ in smw._MOHD_COUNTS]
|
|
return struct.pack('<7I', *words) + bytes(36)
|
|
|
|
|
|
def _root(**chunks):
|
|
return b''.join(t.encode()[::-1] + struct.pack('<I', len(b)) + b for t, b in chunks.items())
|
|
|
|
|
|
# Keep the shared Source/native environment regression in the existing registered suite.
|
|
from wmo_outdoor_semantics_test import WMOOutdoorTest
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|