79 lines
3.5 KiB
Python
79 lines
3.5 KiB
Python
"""Generate or check the nine map-0 Undercity transport spawns from the server catalogue."""
|
|
import argparse
|
|
from decimal import Decimal
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
HEADER = Path(__file__).resolve().parents[1] / 'include/gateway/undercity_transport_spawns.hpp'
|
|
|
|
|
|
def float_literal(value):
|
|
if type(value) not in (Decimal, int) or not Decimal(value).is_finite():
|
|
raise ValueError(f'Expected a finite coordinate, got {value!r}')
|
|
text = format(value, 'f') if isinstance(value, Decimal) else str(value)
|
|
return (text if '.' in text else text + '.0') + 'f'
|
|
|
|
|
|
def render(source):
|
|
rows = sorted(source['undercity_map0'], key=lambda row: row['entry'])
|
|
if [row['entry'] for row in rows] != list(range(20649, 20658)):
|
|
raise ValueError('Expected one Undercity spawn for each entry 20649 through 20657')
|
|
if [row['name'] for row in rows] != ['Undervator', 'upperLdoor', 'lowerLdoor'] * 3:
|
|
raise ValueError('Undercity transport names differ from the source templates')
|
|
if len({row['spawn_guid'] for row in rows}) != len(rows):
|
|
raise ValueError('Duplicate Undercity spawn GUID counter')
|
|
|
|
lines = [
|
|
'#pragma once',
|
|
'',
|
|
'#include <array>',
|
|
'#include <cstdint>',
|
|
'',
|
|
'// Generated by ops/undercity_transport_spawns.py from server_omitted_transports.json.',
|
|
'namespace gateway {',
|
|
'',
|
|
'struct UndercityTransportSpawn {',
|
|
' std::uint32_t entry, spawn_guid, display;',
|
|
' float x, y, z, o;',
|
|
' std::array<float, 4> rot;',
|
|
' const char* name;',
|
|
'};',
|
|
'',
|
|
'inline constexpr std::array<UndercityTransportSpawn, 9> UndercityTransportSpawns{{',
|
|
]
|
|
for row in rows:
|
|
entry, counter, display = (row[field] for field in ('entry', 'spawn_guid', 'display'))
|
|
if not all(type(n) is int for n in (entry, counter, display)):
|
|
raise ValueError(f'Entry {entry}: expected integer identifiers')
|
|
if row['type'] != 11 or not 0 < counter < (1 << 19) or not 0 < display < (1 << 32):
|
|
raise ValueError(f'Entry {entry}: invalid transport type, spawn GUID counter or display')
|
|
if len(row['rot']) != 4:
|
|
raise ValueError(f'Entry {entry}: expected four quaternion components')
|
|
position = ', '.join(float_literal(row[field]) for field in ('x', 'y', 'z', 'o'))
|
|
rotation = ', '.join(map(float_literal, row['rot']))
|
|
name = json.dumps(row['name'], ensure_ascii=True)
|
|
lines.append(f' {{{entry}, {counter}, {display}, {position}, {{{rotation}}}, {name}}},')
|
|
lines.extend(['}};', '', '} // namespace gateway', ''])
|
|
return '\n'.join(lines).encode('ascii')
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('source', type=Path, help='server_omitted_transports.json')
|
|
parser.add_argument('--output', type=Path, default=HEADER, help='generated C++ header')
|
|
parser.add_argument('--check', action='store_true', help='verify that the header is byte-identical')
|
|
args = parser.parse_args()
|
|
source = json.loads(args.source.read_bytes(), parse_float=Decimal)
|
|
generated = render(source)
|
|
if args.check:
|
|
if args.output.read_bytes() != generated:
|
|
raise SystemExit(f'Generated Undercity spawn catalogue differs from {args.output}')
|
|
print(f'9 Undercity transport spawns match {args.output}')
|
|
else:
|
|
args.output.write_bytes(generated)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|