aorecomp

Anarchy Online classic in the browser

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


tools/data/extract_playfields.py (5163B)


      1 import json
      2 import math
      3 import pathlib
      4 import struct
      5 import zlib
      6 
      7 import ruamel.yaml
      8 
      9 # Read the release database's linked resource index.
     10 root = pathlib.Path('local/references/original-11.0/Gamefiles/cd_image/data')
     11 index = (root / 'db/ResourceDatabase.idx').read_bytes()
     12 records = {}
     13 block = struct.unpack_from('<I', index, 72)[0]
     14 seen = set()
     15 while block:
     16     assert block not in seen
     17     seen.add(block)
     18     count = struct.unpack_from('<H', index, block + 8)[0]
     19     for entry in range(count):
     20         offset = block + 18 + entry * 12
     21         position = struct.unpack_from('<I', index, offset)[0]
     22         kind, identity = struct.unpack_from('>II', index, offset + 4)
     23         assert (kind, identity) not in records
     24         records[kind, identity] = position
     25     block = struct.unpack_from('<I', index, block)[0]
     26 
     27 database = open(root / 'db/ResourceDatabase.dat', 'rb')
     28 
     29 
     30 def read_record(kind, identity):
     31     database.seek(records[kind, identity])
     32     header = database.read(34)
     33     assert struct.unpack_from('<II', header, 10) == (kind, identity)
     34     return database.read(struct.unpack_from('<I', header, 18)[0] - 12)
     35 
     36 
     37 # Extract names and arrivals from the original playfield and district records.
     38 catalog = {}
     39 for kind, identity in sorted(records):
     40     if kind != 1000001:
     41         continue
     42     payload = read_record(kind, identity)
     43     assert struct.unpack_from('<I', payload)[0] == 8
     44     name = payload[8:40].split(b'\0', 1)[0].decode('ascii').strip()
     45 
     46     # Decode the center terrain height and validate the compressed block corners.
     47     if (1000009, identity) in records:
     48         ground = read_record(1000009, identity)
     49         assert ground[:4] == b'CHGA'
     50         width, height, tile_size, scale = struct.unpack_from('<HHff', ground, 12)
     51         spawn = [width // 2 * tile_size, 0.5, height // 2 * tile_size]
     52         marker = b'\x08\x09\x00\x00\x00\x00\x00\x00\x00'
     53         if marker in ground:
     54             cursor = ground.index(marker)
     55             blob_size = struct.unpack_from('<I', ground, cursor + 9)[0]
     56             cursor += 13
     57             end = cursor + blob_size
     58             chunks = []
     59             while cursor < end:
     60                 length = struct.unpack_from('<I', ground, cursor)[0]
     61                 cursor += 4
     62                 chunks.append(zlib.decompress(ground[cursor:cursor + length]))
     63                 cursor += length
     64             assert cursor == end
     65             assert ground[cursor:cursor + 9] == b'\x09\x09\x00\x00\x00\x00\x00\x00\x00'
     66             small_size = struct.unpack_from('<I', ground, cursor + 9)[0]
     67             small = ground[cursor + 13:cursor + 13 + small_size]
     68             assert len(small) == len(chunks) * 8
     69             side = math.isqrt(len(chunks[0]))
     70             x, z = width // 2, height // 2
     71             selected = z // (side - 1) * math.ceil(width / (side - 1)) + x // (side - 1)
     72             chunk = chunks[selected]
     73             assert side * side == len(chunk)
     74             values = []
     75             for offset, delta in enumerate(chunk):
     76                 column, row = offset % side, offset // side
     77                 left = values[-1] if column else 0
     78                 above = values[offset - side] if row else 0
     79                 diagonal = values[offset - side - 1] if column and row else 0
     80                 values.append((delta + left + above - diagonal) % 256)
     81             corners = [0, side - 1, side * (side - 1), side * side - 1]
     82             assert bytes(values[i] for i in corners) == small[selected * 8 + 4:selected * 8 + 8]
     83             spawn[1] += values[z % (side - 1) * side + x % (side - 1)] * scale
     84     else:
     85         districts = read_record(1000014, identity)
     86         assert len(districts) >= 19 and districts[6]
     87         spawn = list(struct.unpack_from('<3f', districts, 7))
     88     catalog[identity] = {'id': identity, 'name': name, 'origin': [0, 0],
     89                          'spawn': [round(value, 4) for value in spawn],
     90                          'unavailable': identity in (100, 102, 103, 1410)}
     91 
     92 # Include launcher-only names as disabled choices and keep verified arrivals.
     93 for line in (root / 'launcher/pfnrmap.dat').read_text().splitlines():
     94     identity, name = line.split(';', 1)
     95     identity = int(identity)
     96     if identity not in catalog:
     97         catalog[identity] = {'id': identity, 'name': name, 'origin': [0, 0],
     98                              'spawn': [0, 0, 0], 'unavailable': True}
     99 catalog[800]['origin'] = [37107, 38360]
    100 catalog[800]['spawn'] = [679.625, 74.75, 537.875]
    101 catalog[950]['spawn'] = [52.625, 14.75, 56.875]
    102 database.close()
    103 
    104 # Write a review candidate without overwriting maintained embedded definitions.
    105 pathlib.Path('local/reports').mkdir(parents=True, exist_ok=True)
    106 yaml = ruamel.yaml.YAML()
    107 with pathlib.Path('local/reports/playfields.generated.yaml').open('w') as output:
    108     yaml.dump({identity: {key: value for key, value in catalog[identity].items() if key != 'id'}
    109                for identity in sorted(catalog)}, output)
    110 pathlib.Path('local/reports/original-playfields.json').write_text(json.dumps([catalog[i] for i in sorted(catalog)], indent=2) + '\n')
    111 print(f"Extracted {len(catalog)} zones; {sum(not zone['unavailable'] for zone in catalog.values())} have map assets")