aorecomp

Anarchy Online classic in the browser

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


tools/data/extract_mission_booths.py (3554B)


      1 import hashlib
      2 import json
      3 import pathlib
      4 import struct
      5 
      6 import ao_database
      7 import extract_backyards
      8 import extract_items
      9 
     10 
     11 def main():
     12     client = pathlib.Path('anarchy-vanilla-14.9.1')
     13     assert (client / 'version.id').read_text().strip() == '14.9.1'
     14     database = ao_database.Database(client)
     15     templates = {}
     16     for kind, identity in sorted(database.records):
     17         if kind == 1000020:
     18             payload = database.read(kind, identity)
     19             if struct.unpack_from('<I', payload)[0] == 56001:
     20                 templates[identity] = extract_items.parse_item(payload, kind, identity)
     21     booths, records, excluded = {}, [], []
     22 
     23     # Keep placement overrides: starter booths lack OriginatorType in the template.
     24     for kind, playfield in sorted(database.records):
     25         if kind != 1000026:
     26             continue
     27         payload = database.read(kind, playfield)
     28         offset = 4
     29         for ordinal in range(struct.unpack_from('<I', payload)[0]):
     30             size = struct.unpack_from('<I', payload, offset)[0]
     31             offset += 4
     32             raw = payload[offset:offset + size]
     33             offset += size
     34             assert len(raw) == size and size == 64 + struct.unpack_from('<I', raw, 60)[0]
     35             identity_type, identity = struct.unpack_from('<II', raw)
     36             template = struct.unpack_from('<I', raw, 56)[0]
     37             if identity_type != 56001 and template not in templates:
     38                 continue
     39             assert identity_type == 56001 and template in templates
     40             attributes = extract_backyards.parse_descriptor(raw[64:])
     41             stats = []
     42             for attribute in attributes:
     43                 assert set(attribute) in ({'stats'}, {'name', 'description'})
     44                 if 'stats' in attribute:
     45                     stats.extend(attribute['stats'])
     46             effective = dict(templates[template]['stats'])
     47             effective.update(stats)
     48             record = dict(identity=identity, playfield=playfield, template=template,
     49                           ordinal=ordinal, sha256=hashlib.sha256(raw).hexdigest(),
     50                           resource_sha256=hashlib.sha256(payload).hexdigest(), raw_hex=raw.hex())
     51             if 490 not in effective:
     52                 excluded.append(record)
     53                 continue
     54             assert 1 <= effective[490] <= 8 and identity not in booths
     55             booths[identity] = dict(template=template, playfield=playfield,
     56                                     position=list(struct.unpack_from('<3f', raw, 24)), stats=stats)
     57             records.append(record)
     58         assert offset == len(payload)
     59     database.close()
     60 
     61     # Store authored admission data without deriving faction from originator numbers.
     62     lines = ['# Generated from original 14.9.1 statels by extract_mission_booths.py.']
     63     for identity, booth in sorted(booths.items()):
     64         lines.append(f'{identity}:')
     65         for field, value in booth.items():
     66             lines.append(f'  {field}: {json.dumps(value)}')
     67     output = pathlib.Path('server/gamedata/mission_booths.yaml')
     68     output.write_text('\n'.join(lines) + '\n')
     69     report = pathlib.Path('local/mission-terminal-research/extracted.json')
     70     report.parent.mkdir(parents=True, exist_ok=True)
     71     report.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(),
     72                                       records=records, excluded=excluded), indent=2) + '\n')
     73     print(f'{output}: {len(booths)} mission booths, {len(excluded)} non-mission displays excluded')
     74 
     75 
     76 if __name__ == '__main__':
     77     main()