aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/data/extract_whompahs.py (3655B)


      1 import hashlib
      2 import json
      3 import math
      4 import pathlib
      5 import struct
      6 
      7 import ao_database
      8 
      9 
     10 def destination_lines(payload, playfield):
     11     # The playfield ends with packed identities and two XYZ endpoints per line.
     12     candidates = []
     13     for count in range(min(256, (len(payload) - 4) // 28) + 1):
     14         offset = len(payload) - 4 - count * 28
     15         if struct.unpack_from('<I', payload, offset)[0] != count:
     16             continue
     17         lines = [struct.unpack_from('<I6f', payload, offset + 4 + i * 28) for i in range(count)]
     18         if all(line[0] & 65535 == playfield and line[0] >> 24 == 0 for line in lines):
     19             candidates.append(lines)
     20     assert len(candidates) == 1, (playfield, len(candidates))
     21     return {line[0]: (list(line[1:4]), list(line[4:7])) for line in candidates[0]}
     22 
     23 
     24 def main():
     25     client = pathlib.Path('anarchy-vanilla-14.9.1')
     26     assert (client / 'version.id').read_text().strip() == '14.9.1'
     27     database = ao_database.Database(client)
     28     routes = []
     29     for kind, playfield in sorted(database.records):
     30         if kind != 1000026:
     31             continue
     32         payload = database.read(1000026, playfield)
     33         offset = 4
     34         for _ in range(struct.unpack_from('<I', payload)[0]):
     35             size = struct.unpack_from('<I', payload, offset)[0]
     36             offset += 4
     37             raw = payload[offset:offset + size]
     38             offset += size
     39             assert len(raw) == size and size == 64 + struct.unpack_from('<I', raw, 60)[0]
     40             statel_type, door_id = struct.unpack_from('<II', raw)
     41             descriptor = raw[64:]
     42             template = struct.unpack_from('<I', raw, 56)[0]
     43             if statel_type != 51016:
     44                 continue
     45             if template != 96272 and not (template == 41565 and b'Whom-Pah' in descriptor):
     46                 continue
     47             assert struct.unpack_from('>16I', descriptor) == (102, 103, 1009, 3, 2, 16, 2018, 53059, 0, 4, 0, 1, 0, 2, 0, 100001)
     48             packed, destination, opaque = struct.unpack_from('>3I', descriptor, 64)
     49             assert packed & 65535 == destination and opaque == 0
     50             destination_payload = database.read(1000001, destination)
     51             start, end = destination_lines(destination_payload, destination)[packed]
     52 
     53             # Midpoint, human clearance and facing away are reconstruction policy.
     54             arrival = [(first + second) / 2 for first, second in zip(start, end)]
     55             arrival[1] += 0.5
     56             heading = math.atan2(start[2] - end[2], end[0] - start[0])
     57             routes.append(dict(playfield=playfield, door=door_id, position=list(struct.unpack_from('<3f', raw, 24)),
     58                                rotation=list(reversed(struct.unpack_from('<4f', raw, 36))),
     59                                destination=destination, destination_line=packed >> 16, arrival_start=start, arrival_end=end,
     60                                arrival=dict(position=arrival, rotation=[0, math.sin(heading / 2), 0, math.cos(heading / 2)]),
     61                                record_sha256=hashlib.sha256(raw).hexdigest(), descriptor_hex=descriptor.hex(),
     62                                destination_sha256=hashlib.sha256(destination_payload).hexdigest()))
     63         assert offset == len(payload)
     64     database.close()
     65     assert len(routes) == 60
     66 
     67     # Review these values against the target entries in zone_links.yaml.
     68     output = pathlib.Path('local/reports/whompahs-14.9.1.json')
     69     output.parent.mkdir(parents=True, exist_ok=True)
     70     output.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(), routes=routes), indent=2) + '\n')
     71     print(output)
     72 
     73 
     74 if __name__ == '__main__':
     75     main()