73 lines
3.8 KiB
Python
73 lines
3.8 KiB
Python
import sys,json,struct,collections; sys.path.insert(0,'.'); sys.path.insert(0,'/mnt/Games/@project/gateway/ops')
|
|
import octo_mpq as O, map_asset_preflight as terrain, source_map_wdt as smw
|
|
def chunks(raw):
|
|
at=0
|
|
while at+8<=len(raw):
|
|
size=struct.unpack_from('<I',raw,at+4)[0]
|
|
yield raw[at:at+4][::-1].decode('latin1'),raw[at+8:at+8+size]
|
|
at+=8+size
|
|
def pairs(root):
|
|
c=dict(chunks(root)).get('MH2O',b''); out=collections.Counter()
|
|
if not c: return out
|
|
for k in range(256):
|
|
oi,lc,oa=struct.unpack_from('<3I',c,12*k)
|
|
for l in range(lc):
|
|
lt,lo_,lo,hi=struct.unpack_from('<HHff',c,oi+24*l); out[(lt,lo_,'sea' if abs(lo)<.05 and abs(hi)<.05 else 'off')]+=1
|
|
return out
|
|
def convert(d,xy):
|
|
src=O.read('World\\Maps\\%s\\%s_%d_%d.adt'%(d,d,*xy))
|
|
out=terrain.adt_split_candidate(src,xy,file_data_ids={},roles=('root',))[0]['root']
|
|
try: n=smw.normalize_flat_terrain_ocean_material(out)
|
|
except Exception as e: return out,None,str(e)[:80]
|
|
return out,n,None
|
|
if __name__=='__main__':
|
|
import client_read as R
|
|
B=json.load(open('/mnt/Games/@project/gateway/runtime/continuation-20261026/all57-terrain-integrated-batch/all57-terrain-component-and-WDT-bindings.json'))
|
|
CM=json.load(open('client-maps.json')); from recover_bindings import maid
|
|
stats=collections.Counter(); shown=0
|
|
for mid in ('808','816','818','819','820','822','0','1'):
|
|
d=CM[mid]['dir']; t=maid(R.read_fdid(CM[mid]['wdt']))
|
|
for xy,f in list(t.items())[:: (1 if int(mid)>2 else 15)]:
|
|
src=O.read('World\\Maps\\%s\\%s_%d_%d.adt'%(d,d,*xy))
|
|
if src is None or not dict(chunks(src)).get('MH2O'): continue
|
|
inst=R.read_fdid(f[0]); raw,n,err=convert(d,xy)
|
|
nb=n[0] if isinstance(n,tuple) else n
|
|
stats[(mid, 'raw==' if raw==inst else ('norm==' if nb==inst else 'diff'))]+=1
|
|
if nb!=inst and raw!=inst and shown<6: shown+=1; print(mid,xy,'inst',dict(pairs(inst)),'norm',dict(pairs(nb)) if nb else err)
|
|
print(dict(stats))
|
|
|
|
def strip_mclq(root):
|
|
"""Native form once MH2O carries the water: no legacy MCLQ, ofs/sizeMCLQ 0, MCNK liquid flag bits (0x4-0x20) clear."""
|
|
out = []
|
|
for t, c in chunks(root):
|
|
if t == 'MCNK':
|
|
hdr = bytearray(c[:128]); sub = [(st, sc) for st, sc in chunks(c[128:])]
|
|
if any(st == 'MCLQ' for st, _ in sub):
|
|
assert sub[-1][0] == 'MCLQ', [s for s, _ in sub]
|
|
sub = sub[:-1]
|
|
struct.pack_into('<2I', hdr, 96, 0, 0); struct.pack_into('<I', hdr, 0, struct.unpack_from('<I', hdr, 0)[0] & ~0x3C)
|
|
c = bytes(hdr) + b''.join(st[::-1].encode() + struct.pack('<I', len(sc)) + sc for st, sc in sub)
|
|
out.append(t[::-1].encode() + struct.pack('<I', len(c)) + c)
|
|
return b''.join(out)
|
|
|
|
def retype_off_sea(root):
|
|
"""fix-still/fix-lake final form (census of continents): any water instance not at height 0 -> native lake pair (1325, 19034)."""
|
|
ch = list(chunks(root)); n = 0; out = []
|
|
for t, c in ch:
|
|
if t == 'MH2O':
|
|
m = bytearray(c)
|
|
for k in range(256):
|
|
oi, lc, oa = struct.unpack_from('<3I', m, 12 * k)
|
|
for l in range(lc):
|
|
lt, lo_, lo, hi = struct.unpack_from('<HHff', m, oi + 24 * l)
|
|
if lt in (1, 2, 5, 1240, 1250) and (abs(lo) > .05 or abs(hi) > .05): struct.pack_into('<HH', m, oi + 24 * l, 1325, 19034); n += 1
|
|
c = bytes(m)
|
|
out.append(t[::-1].encode() + struct.pack('<I', len(c)) + c)
|
|
return b''.join(out), n
|
|
def native_root(d, xy):
|
|
"""Full chain: structural split -> ocean material -> off-sea lake retype -> strip legacy MCLQ."""
|
|
raw, n, err = convert(d, xy)
|
|
r = raw if n is None else (n[0] if isinstance(n, tuple) else n)
|
|
r, _ = retype_off_sea(r)
|
|
return strip_mclq(r), err
|