Files

72 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""#127 map editor, M4: turn an editor placement list into LOCAL GM chat commands.
The LOCAL mangosd console has no spawn-at-coordinates command; `.npc add` / `.gobject add` spawn at the GM character's
position. So each placement becomes `.go xyz X Y Z MAP` + `.npc add ENTRY` (creatures) or `.gobject add ENTRY` (objects),
followed by an orientation step. The harness (chat.py) types the lines in-game on a GM character. LOCAL only.
Input JSON (exported by the editor):
[{"kind": "npc"|"gobject", "entry": 1234, "map": 0, "x": -9449.0, "y": 64.0, "z": 56.3, "o": 3.14}, ...]
z must come from the edited terrain (never a guessed height - see memory teleport-valid-ground).
ops/spawn_export.py placements.json # print the command script (dry run)
ops/spawn_export.py placements.json -o out.txt # write it for the harness
"""
import argparse, json, math, sys
from pathlib import Path
KINDS = {'npc': '.npc add {entry}', 'gobject': '.gobject add {entry}'}
MAX_COORD = 17066.67 # 32 ADT tiles * 533.33 yards, the world bound on every continent
def validate(row, index):
where = f'placement {index}'
if row.get('kind') not in KINDS:
raise ValueError(f'{where}: kind must be npc or gobject')
for key in ('entry', 'map'):
if not isinstance(row.get(key), int) or row[key] < 0:
raise ValueError(f'{where}: {key} must be a non-negative integer')
if row['entry'] == 0:
raise ValueError(f'{where}: entry 0 is not a template')
for key in ('x', 'y', 'z'):
value = row.get(key)
if not isinstance(value, (int, float)) or not math.isfinite(value) or abs(value) > MAX_COORD:
raise ValueError(f'{where}: {key} out of world bounds')
o = row.get('o', 0.0)
if not isinstance(o, (int, float)) or not math.isfinite(o):
raise ValueError(f'{where}: o must be a finite angle')
return dict(row, o=o % (2 * math.pi))
def commands(rows):
out = []
for i, raw in enumerate(rows):
row = validate(raw, i)
out.append(f".go xyz {row['x']:.3f} {row['y']:.3f} {row['z']:.3f} {row['map']}")
out.append(KINDS[row['kind']].format(entry=row['entry']))
# Facing: `.go xyz` takes no orientation and `.gobject turn` needs the new spawn's GUID, so the spawn keeps the
# GM's current facing. `o` is validated and kept in the input for a later GUID-aware pass (verified against
# tortoise-wow Chat.cpp command table 2026-11-30); no invented commands here.
return out
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument('placements', type=Path)
p.add_argument('-o', '--out', type=Path)
a = p.parse_args()
rows = json.loads(a.placements.read_text())
if not isinstance(rows, list):
raise SystemExit('placement file must be a JSON list')
lines = commands(rows)
text = '\n'.join(lines) + '\n'
if a.out:
a.out.write_text(text)
print(f'{len(rows)} placements -> {len(lines)} commands in {a.out}')
else:
sys.stdout.write(text)
if __name__ == '__main__':
main()