tools/data/extract_grid.py (3525B)
1 import hashlib 2 import json 3 import math 4 import pathlib 5 import struct 6 7 import ao_database 8 import extract_whompahs 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 routes = [] 16 for playfield, instance, template, destination, packed in [ 17 (800, 0xc0040320, 95350, 152, 0x260098), 18 (152, 0xc04c0098, 95351, 800, 0x000e0320)]: 19 # Select the two authored SimpleItems, preserving their complete scripts. 20 payload = database.read(1000026, playfield) 21 offset = 4 22 for _ in range(struct.unpack_from('<I', payload)[0]): 23 size = struct.unpack_from('<I', payload, offset)[0] 24 offset += 4 25 raw = payload[offset:offset + size] 26 offset += size 27 assert len(raw) == size and size == 64 + struct.unpack_from('<I', raw, 60)[0] 28 if struct.unpack_from('<II', raw) != (51005, instance): 29 continue 30 assert struct.unpack_from('<I', raw, 56)[0] == template 31 descriptor = raw[64:] 32 assert struct.unpack_from('>5I', descriptor) == (102, 103, 1009, 1, 2) 33 event, _, function, _, _, count = struct.unpack_from('>6I', descriptor, 20) 34 requirements = [struct.unpack_from('>3I', descriptor, 44 + i * 12) for i in range(count)] 35 assert requirements == ([(410, 0, 0), (161, 90, 2), (0, 0, 4)] if playfield == 800 else [(161, 90, 2)]) 36 argument_offset = 44 + count * 12 37 assert struct.unpack_from('>4I', descriptor, argument_offset) == (1, 0, 2, 0) 38 arguments = struct.unpack_from('>7I' if playfield == 800 else '>4I', descriptor, argument_offset + 16) 39 assert (event, function, arguments) == ((0, 53083, (51102, 152, 0, 0, 100001, 0x260098, 500)) if playfield == 800 else (3, 53059, (100001, 0x000e0320, 800, 0))) 40 41 # Midpoint, human clearance and facing away are reconstruction policy. 42 destination_payload = database.read(1000001, destination) 43 start, end = extract_whompahs.destination_lines(destination_payload, destination)[packed] 44 arrival = [(first + second) / 2 for first, second in zip(start, end)] 45 arrival[1] += 0.5 46 heading = math.atan2(start[2] - end[2], end[0] - start[0]) 47 routes.append(dict(playfield=playfield, instance=instance, template=template, 48 position=list(struct.unpack_from('<3f', raw, 24)), event=event, function=function, 49 requirements=requirements, arguments=arguments, destination=destination, destination_line=packed >> 16, 50 arrival_start=start, arrival_end=end, 51 arrival=dict(position=arrival, rotation=[0, math.sin(heading / 2), 0, math.cos(heading / 2)]), 52 record_sha256=hashlib.sha256(raw).hexdigest(), descriptor_hex=descriptor.hex(), 53 destination_sha256=hashlib.sha256(destination_payload).hexdigest())) 54 assert offset == len(payload) 55 database.close() 56 assert len(routes) == 2 57 output = pathlib.Path('local/reports/grid-14.9.1.json') 58 output.parent.mkdir(parents=True, exist_ok=True) 59 output.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(), routes=routes), indent=2) + '\n') 60 print(output) 61 62 63 if __name__ == '__main__': 64 main()