aorecomp

Anarchy Online classic in the browser

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


tools/data/extract_shops.py (3394B)


      1 import hashlib
      2 import json
      3 import pathlib
      4 import struct
      5 
      6 import ao_database
      7 
      8 
      9 def main():
     10     client = pathlib.Path('anarchy-vanilla-14.9.1')
     11     assert (client / 'version.id').read_text().strip() == '14.9.1'
     12     database = ao_database.Database(client)
     13     templates = set()
     14     for kind, identity in database.records:
     15         if kind == 1000020 and struct.unpack_from('<I', database.read(kind, identity))[0] == 51035:
     16             templates.add(identity)
     17     machines = {}
     18     records = []
     19 
     20     # Original statels retain the complete world transform and HighLow stat overrides.
     21     for kind, playfield in sorted(database.records):
     22         if kind != 1000026:
     23             continue
     24         payload = database.read(kind, playfield)
     25         offset = 4
     26         for ordinal in range(struct.unpack_from('<I', payload)[0]):
     27             size = struct.unpack_from('<I', payload, offset)[0]
     28             offset += 4
     29             raw = payload[offset:offset + size]
     30             offset += size
     31             assert len(raw) == size and size == 64 + struct.unpack_from('<I', raw, 60)[0]
     32             identity_type, identity = struct.unpack_from('<II', raw)
     33             template = struct.unpack_from('<I', raw, 56)[0]
     34             if identity_type != 51035 and template not in templates:
     35                 continue
     36             assert identity_type == 51035 and template in templates and identity not in machines
     37             words = struct.unpack('>' + 'I' * ((size - 64) // 4), raw[64:])
     38             assert words[:3] == (102, 103, 1009)
     39             stats = []
     40             if words[3] == 0:
     41                 assert words == (102, 103, 1009, 0, 0)
     42             else:
     43                 assert words[3:6] == (1, 15, 24) and words[-1] == 0
     44                 assert words[6] % 1009 == 0
     45                 count = words[6] // 1009 - 1
     46                 assert len(words) == 8 + count * 2
     47                 stats = [list(words[index:index + 2]) for index in range(7, len(words) - 1, 2)]
     48             machine = dict(template=template, playfield=playfield,
     49                            position=list(struct.unpack_from('<3f', raw, 24)),
     50                            rotation=list(reversed(struct.unpack_from('<4f', raw, 36))))
     51             if stats:
     52                 machine['stats'] = stats
     53             machines[identity] = machine
     54             records.append(dict(identity=identity, ordinal=ordinal, sha256=hashlib.sha256(raw).hexdigest(),
     55                                 resource_sha256=hashlib.sha256(payload).hexdigest(), raw_hex=raw.hex()))
     56         assert offset == len(payload)
     57     database.close()
     58 
     59     # Keep only the authored data needed to reconstruct the native world objects.
     60     lines = ['# Generated from original 14.9.1 statels by extract_shops.py.']
     61     for identity, machine in sorted(machines.items()):
     62         lines.append(f'{identity}:')
     63         for field, value in machine.items():
     64             lines.append(f'  {field}: {json.dumps(value)}')
     65     output = pathlib.Path('server/gamedata/shops.yaml')
     66     output.write_text('\n'.join(lines) + '\n')
     67     report = pathlib.Path('local/shop-room-research/extracted.json')
     68     report.parent.mkdir(parents=True, exist_ok=True)
     69     report.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(),
     70                                       records=records), indent=2) + '\n')
     71     print(f'{output}: {len(machines)} machines')
     72 
     73 
     74 if __name__ == '__main__':
     75     main()