aorecomp

Anarchy Online classic in the browser

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


tools/data/extract_monsters.py (2808B)


      1 import json
      2 import pathlib
      3 import re
      4 import struct
      5 
      6 import ao_database
      7 
      8 # Read appearance definitions from the selected client.
      9 assert pathlib.Path('anarchy-vanilla-14.9.1/version.id').read_text().strip() == '14.9.1'
     10 database = ao_database.Database('anarchy-vanilla-14.9.1')
     11 
     12 
     13 # Resolve model IDs through the selected client's filename registry.
     14 registry = database.read(1000010, 1)
     15 filenames = {}
     16 for match in re.finditer(rb'[ -~]{5,}', registry):
     17     identity, length = struct.unpack_from('<II', registry, match.start() - 8)
     18     if length == (len(match.group()) // 4 + 1) * 4:
     19         filenames[identity] = match.group().decode('ascii')
     20 
     21 catalog = []
     22 for kind, identity in sorted(database.records):
     23     if kind != 1040023:
     24         continue
     25     payload = database.read(kind, identity)
     26     version, section, encoded_count = struct.unpack_from('<III', payload)
     27     assert (version, section) == (15, 23)
     28     assert encoded_count % 1009 == 0
     29     count = encoded_count // 1009 - 1
     30     stats = dict(struct.unpack_from('<II', payload, 12 + entry * 8) for entry in range(count))
     31     offset = 12 + count * 8
     32     assert struct.unpack_from('<II', payload, offset) == (14, 19)
     33 
     34     # Corpse creation needs the resources behind the creature's death key 6000.
     35     animation_count = struct.unpack_from('<I', payload, offset + 8)[0] // 1009 - 1
     36     offset += 12
     37     death_animations = []
     38     for _ in range(animation_count):
     39         key, encoded = struct.unpack_from('<II', payload, offset)
     40         assert encoded % 1009 == 0
     41         variants = encoded // 1009 - 1
     42         offset += 8
     43         if key == 6000:
     44             death_animations = list(struct.unpack_from('<' + 'I' * variants, payload, offset))
     45         offset += variants * 4
     46 
     47     # The final name section has an explicit length and a terminating zero.
     48     offset = payload.rfind(b'\x06\x00\x00\x00\x1c\x00\x00\x00\xf1\x03\x00\x00')
     49     assert offset >= 0, identity
     50     length = struct.unpack_from('<I', payload, offset + 12)[0]
     51     assert offset + 16 + length + 1 == len(payload), identity
     52     assert payload[-1] == 0
     53     name = payload[offset + 16:-1].decode('ascii')
     54     catalog.append({'monster_data': identity, 'name': name, 'mesh': stats[12],
     55                     'mesh_filename': filenames.get(stats[12]), 'death_animations': death_animations, 'stats': stats,
     56                     'record_file_offset': database.records[kind, identity]})
     57 database.close()
     58 
     59 # Preserve appearance definitions independently of population template hashes.
     60 pathlib.Path('local/data').mkdir(parents=True, exist_ok=True)
     61 pathlib.Path('local/data/monsters.json').write_text(json.dumps(catalog, indent=2) + '\n')
     62 print('Extracted', len(catalog), '14.9.1 monster appearance definitions')
     63 print('Mesh filenames unresolved:', sum(monster['mesh_filename'] is None for monster in catalog))