tools/data/extract_npc_spawns.py (5242B)
1 import collections 2 import hashlib 3 import io 4 import json 5 import pathlib 6 import struct 7 8 import ao_database 9 10 11 def read(reader, format): 12 return list(struct.unpack('<' + format, reader.read(struct.calcsize('<' + format)))) 13 14 15 def decode_population(payload): 16 # AODB PlayfieldDistrictInfo.ReadDistrict/ReadHashSpawnPoint, commit a772622b. 17 reader = io.BytesIO(payload) 18 version, zone_count, district_count = read(reader, 'HiB') 19 assert version in [5, 6], ('district version', version) 20 districts = [] 21 for _ in range(district_count): 22 district = {'payload_offset': reader.tell(), 'center': read(reader, '3f')} 23 length = 32 if version == 5 else read(reader, 'H')[0] 24 district['name'] = reader.read(length).split(b'\0', 1)[0].decode('ascii') 25 district['stats'] = read(reader, '9H') 26 if version == 5: 27 district['extras'] = reader.read(4).hex() 28 district['npc_min_level'], district['npc_max_level'] = read(reader, '2B') 29 else: 30 district['npc_min_level'], district['npc_max_level'] = read(reader, '2H') 31 district['land_control_levels'] = read(reader, '2H') 32 district['respawn_chance'], district['respawn_time'], district['fight_mode'] = read(reader, 'BiB') 33 spawn_count = read(reader, 'B')[0] 34 extra_count = read(reader, 'B')[0] if version == 5 else 0 35 music_count, point_count, fixed_count = read(reader, '3B') 36 37 # Population codes are packed abbreviations, independent of appearance IDs. 38 district['spawn_groups'] = [] 39 for _ in range(spawn_count): 40 code, count = read(reader, 'IH') 41 district['spawn_groups'].append({ 42 'template_hash': struct.pack('>I', code).decode('latin1'), 'count': count}) 43 district['extra_infos'] = [reader.read(6).hex() for _ in range(extra_count)] 44 district['music_pairs'] = [read(reader, '2H') for _ in range(music_count)] 45 district['spawn_points'] = [read(reader, '4f') for _ in range(point_count)] 46 47 # Preserve optional fixed-spawn flags and routes without interpreting their AI. 48 district['fixed_spawns'] = [] 49 for _ in range(fixed_count): 50 spawn = {'payload_offset': reader.tell(), 'position': read(reader, '3f'), 51 'radius': read(reader, 'f')[0], 'angles': read(reader, '2h')} 52 spawn['template_hash'] = struct.pack('>I', read(reader, 'I')[0]).decode('latin1') 53 spawn['min_level'], spawn['max_level'] = read(reader, '2H') 54 spawn['respawn_chance'], spawn['flags'], spawn['respawn_time'] = read(reader, '2BH') 55 assert spawn['flags'] & ~1 == 0, ('unsupported fixed-spawn flags', spawn) 56 if spawn['flags'] & 1: 57 spawn['optional_flags'] = read(reader, '4B') 58 district['fixed_spawns'].append(spawn) 59 districts.append(district) 60 61 zone_map = list(reader.read(zone_count)) 62 assert len(zone_map) == zone_count 63 assert all(index < len(districts) for index in zone_map) 64 assert reader.tell() == len(payload), ('district trailing bytes', reader.tell(), len(payload)) 65 return {'version': version, 'districts': districts, 'zone_to_district_map': zone_map} 66 67 68 def main(): 69 client = pathlib.Path('anarchy-vanilla-14.9.1') 70 assert (client / 'version.id').read_text().strip() == '14.9.1' 71 database = ao_database.Database(client) 72 catalog = [] 73 hashes = {} 74 for kind, identity in sorted(database.records): 75 if kind != 1000014: 76 continue 77 payload = database.read(kind, identity) 78 playfield = decode_population(payload) 79 playfield['playfield'] = identity 80 playfield['name'] = database.read(1000001, identity)[8:40].split(b'\0', 1)[0].decode('ascii').strip() 81 playfield['record_file_offset'] = database.records[kind, identity] 82 catalog.append(playfield) 83 hashes[identity] = hashlib.sha256(payload).hexdigest() 84 database.close() 85 86 pathlib.Path('local/data').mkdir(parents=True, exist_ok=True) 87 pathlib.Path('local/data/npc-spawns.json').write_text(json.dumps(catalog, indent=2) + '\n') 88 evidence = {'version': '14.9.1', 'client': str(client), 89 'index_sha256': hashlib.sha256(database.index).hexdigest(), 'payload_sha256': hashes, 90 'format_versions': dict(collections.Counter(p['version'] for p in catalog)), 91 'districts': sum(len(p['districts']) for p in catalog), 92 'spawn_groups': sum(len(d['spawn_groups']) for p in catalog for d in p['districts']), 93 'fixed_spawns': sum(len(d['fixed_spawns']) for p in catalog for d in p['districts']), 94 'checks': ['Every resource decoded to exact EOF; all zone-map indices valid.'], 95 'reference': 'AODB.Common/RDBObjects/PlayfieldDistrictInfo_1000014.cs at a772622bf39f7ac07a1761b90765214304d15107'} 96 pathlib.Path('local/reports').mkdir(parents=True, exist_ok=True) 97 pathlib.Path('local/reports/npc-extraction-evidence.json').write_text(json.dumps(evidence, indent=2) + '\n') 98 print('Decoded', len(catalog), '14.9.1 maps;', evidence['districts'], 'districts;', evidence['format_versions']) 99 100 101 if __name__ == '__main__': 102 main()