106 lines
5.1 KiB
Python
106 lines
5.1 KiB
Python
# coding: utf-8
|
|
"""Generate AVA2-style teleporter quest from maps.json (Python 3)."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
|
|
def generate():
|
|
catalog = json.loads((ROOT / 'maps.json').read_text(encoding='utf-8'))
|
|
def quote(s):
|
|
return json.dumps(s, ensure_ascii=False)
|
|
|
|
lines = [
|
|
'-- Generated by generate.py from maps.json. Do not edit by hand.',
|
|
'-- UTF-8 source; install.sh compiles GBK for the 203 server.',
|
|
'quest ring_warp begin',
|
|
' state start begin',
|
|
' function destinations()',
|
|
' return {'
|
|
]
|
|
for c in catalog['categories']:
|
|
lines.append(' {name = %s, ' % quote(c['name']))
|
|
if 'min_level' in c:
|
|
lines[-1] += 'min_level = %d, ' % c['min_level']
|
|
if 'destinations' in c:
|
|
lines.append(' destinations = {')
|
|
for d in c['destinations']:
|
|
lines.append(' {name = %s, x = %d, y = %d},' % (quote(d['name']), d['x'], d['y']))
|
|
lines.append(' }},')
|
|
elif 'subgroups' in c:
|
|
lines.append(' subgroups = {')
|
|
for sg in c['subgroups']:
|
|
lines.append(' {name = %s, destinations = {' % quote(sg['name']))
|
|
for d in sg['destinations']:
|
|
lines.append(' {name = %s, x = %d, y = %d},' % (quote(d['name']), d['x'], d['y']))
|
|
lines.append(' }},')
|
|
lines.append(' }},')
|
|
lines.extend([
|
|
' }',
|
|
' end',
|
|
' when 10031.sig_use or 70058.use or 76042.use begin',
|
|
' if not pc.can_warp() then',
|
|
' say("当前状态无法传送,请结束交易、战斗或其他操作后再试。")',
|
|
' return',
|
|
' end',
|
|
' local s = select('
|
|
])
|
|
cat_names = [quote(c['name']) for c in catalog['categories']] + [quote('关闭')]
|
|
lines.append(' ' + ', '.join(cat_names))
|
|
lines.append(' )')
|
|
for idx, c in enumerate(catalog['categories'], 1):
|
|
if_stmt = 'if' if idx == 1 else 'elseif'
|
|
lines.append(' %s s == %d then' % (if_stmt, idx))
|
|
if 'min_level' in c:
|
|
lines.extend([
|
|
' if pc.get_level() < %d then' % c['min_level'],
|
|
' say("")',
|
|
' say(%s)' % quote(c.get('level_message', '等级不足!')),
|
|
' say("")',
|
|
' else'
|
|
])
|
|
indent = ' '
|
|
else:
|
|
indent = ' '
|
|
|
|
var_name = 'sub_%d' % idx
|
|
if 'destinations' in c:
|
|
d_names = [quote(d['name']) for d in c['destinations']] + [quote('关闭')]
|
|
lines.append(indent + 'local %s = select(%s)' % (var_name, ', '.join(d_names)))
|
|
for d_idx, d in enumerate(c['destinations'], 1):
|
|
sub_if = 'if' if d_idx == 1 else 'elseif'
|
|
lines.append(indent + '%s %s == %d then if not pc.can_warp() then say("当前状态无法传送,请稍后重试。") return end pc.warp(%d, %d) setskin(NOWINDOW)' % (sub_if, var_name, d_idx, d['x'], d['y']))
|
|
lines.append(indent + 'end')
|
|
elif 'subgroups' in c:
|
|
sg_names = [quote(sg['name']) for sg in c['subgroups']] + [quote('关闭')]
|
|
lines.append(indent + 'local %s = select(%s)' % (var_name, ', '.join(sg_names)))
|
|
for sg_idx, sg in enumerate(c['subgroups'], 1):
|
|
sg_if = 'if' if sg_idx == 1 else 'elseif'
|
|
lines.append(indent + '%s %s == %d then' % (sg_if, var_name, sg_idx))
|
|
sub_var = 'sub_%d_%d' % (idx, sg_idx)
|
|
d_names = [quote(d['name']) for d in sg['destinations']] + [quote('关闭')]
|
|
lines.append(indent + ' local %s = select(%s)' % (sub_var, ', '.join(d_names)))
|
|
for d_idx, d in enumerate(sg['destinations'], 1):
|
|
d_if = 'if' if d_idx == 1 else 'elseif'
|
|
lines.append(indent + ' %s %s == %d then if not pc.can_warp() then say("当前状态无法传送,请稍后重试。") return end pc.warp(%d, %d) setskin(NOWINDOW)' % (d_if, sub_var, d_idx, d['x'], d['y']))
|
|
lines.append(indent + ' end')
|
|
lines.append(indent + 'end')
|
|
|
|
if 'min_level' in c:
|
|
lines.append(' end')
|
|
lines.extend([
|
|
' end',
|
|
' end',
|
|
' end',
|
|
'end'
|
|
])
|
|
result = '\n'.join(line.rstrip() for line in lines) + '\n'
|
|
encoded = result.encode('gbk')
|
|
if b'\\' in encoded:
|
|
bad = sorted({c for c in result if b'\\' in c.encode('gbk')})
|
|
raise ValueError('qc cannot parse GBK backslash bytes: %r' % bad)
|
|
return result
|
|
|
|
if __name__ == '__main__':
|
|
(ROOT / 'ring_warp.quest').write_text(generate(), encoding='utf-8')
|