75 lines
3.4 KiB
Python
75 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""#127 M4: place LOCAL server spawns from an editor placement list (LOCAL only, no DB writes, no new server features).
|
|
|
|
tortoise main registers `.npc add` / `.gobject add` with allowConsole=false (src/game/Chat/Chat.cpp:253,434), so SOAP
|
|
(ops/local_gm.py) cannot spawn. Both place the spawn at the GM's own position, so each placement is
|
|
`.go xyz <x> <y> <z> <map>` (exact editor ground height - never a guessed Z, see memory teleport-valid-ground)
|
|
followed by the add command, typed by an in-game LOCAL GM through the chat harness (chat.py, GWD=<display>).
|
|
|
|
Placement list (JSON): [{"kind": "npc"|"go", "entry": 123, "map": 0, "x": .., "y": .., "z": .., "o": optional}]
|
|
Orientation: `.go xyz` has no orientation argument, so NPCs face the GM's current facing. Game objects can be turned
|
|
afterwards with `.gobject turn <guid> <o>` once the GUID is read back (not automated yet).
|
|
Spawns persist to tw_world: every applied list is appended to the log next to it, so it can be undone.
|
|
|
|
editor_spawns.py plan <list.json> print the command sequence (default, nothing sent)
|
|
editor_spawns.py apply <list.json> --display :2 --chat <chat.py> type it into the LOCAL client on that X display
|
|
"""
|
|
import json, subprocess, sys, time, os, argparse
|
|
from pathlib import Path
|
|
|
|
KINDS = {'npc': '.npc add', 'go': '.gobject add'}
|
|
|
|
|
|
def load(path):
|
|
rows = json.loads(Path(path).read_text())
|
|
if not isinstance(rows, list):
|
|
raise ValueError('placement list must be a JSON array')
|
|
for i, r in enumerate(rows):
|
|
if r.get('kind') not in KINDS:
|
|
raise ValueError(f'row {i}: kind must be npc or go')
|
|
if not isinstance(r.get('entry'), int) or r['entry'] <= 0:
|
|
raise ValueError(f'row {i}: entry must be a positive integer')
|
|
if not isinstance(r.get('map'), int) or r['map'] < 0:
|
|
raise ValueError(f'row {i}: map must be a non-negative integer')
|
|
for k in ('x', 'y', 'z'):
|
|
if not isinstance(r.get(k), (int, float)):
|
|
raise ValueError(f'row {i}: {k} must be a number (exact editor height, never guessed)')
|
|
return rows
|
|
|
|
|
|
def commands(rows):
|
|
out = []
|
|
for r in rows:
|
|
out.append(f".go xyz {r['x']:.3f} {r['y']:.3f} {r['z']:.3f} {r['map']}")
|
|
out.append(f"{KINDS[r['kind']]} {r['entry']}")
|
|
return out
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('mode', choices=('plan', 'apply'))
|
|
ap.add_argument('list')
|
|
ap.add_argument('--display')
|
|
ap.add_argument('--chat', help='LOCAL chat harness script (types one chat line; GWD=<display>)')
|
|
a = ap.parse_args(argv)
|
|
rows = load(a.list)
|
|
cmds = commands(rows)
|
|
if a.mode == 'plan':
|
|
print('\n'.join(cmds))
|
|
return 0
|
|
if not a.display or not a.chat or not Path(a.chat).is_file():
|
|
raise SystemExit('apply needs --display of the LOCAL client (never OCTO) and an existing --chat harness')
|
|
log = Path(a.list).with_suffix('.applied.log')
|
|
with log.open('a') as f:
|
|
f.write(f'# {time.strftime("%Y-%m-%d %H:%M:%S")} display {a.display}\n')
|
|
for c in cmds:
|
|
subprocess.run([sys.executable, a.chat, c], env=dict(os.environ, GWD=a.display), check=True)
|
|
f.write(c + '\n')
|
|
time.sleep(3 if c.startswith('.go ') else 1.5)
|
|
print(f'applied {len(rows)} placements; log {log}')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|