feat: align travel ring menus and validate destinations against 203 maps

This commit is contained in:
shen
2026-10-17 16:40:54 -07:00
parent 0b828387aa
commit 9fbeaeb7a2
7 changed files with 2390 additions and 1884 deletions
+168 -83
View File
@@ -1,6 +1,6 @@
# coding: utf-8
"""Catalog coverage plus Lua 5.0 menu/permission/warp integration checks.
Usage: python3 test_ring.py SERVER_LIBLUA_DIRECTORY [LIVE_MAP_INDEX] [COMPILED_OBJECT_ROOT]
Usage: python3 test_ring.py SERVER_LIBLUA_DIRECTORY
"""
import importlib.util
import json
@@ -16,96 +16,181 @@ spec.loader.exec_module(generator)
quest = generator.generate()
assert quest == (ROOT / 'ring_warp.quest').read_text(encoding='utf-8'), 'regenerate quest'
catalog = json.loads((ROOT / 'maps.json').read_text(encoding='utf-8'))
maps = [m for c in catalog['categories'] for m in c['maps']]
byid = {m['id']:m for m in maps}
assert len(maps) == len(byid) == 65
if len(sys.argv) > 2:
live = dict((int(line.split()[0]),line.split()[1]) for line in Path(sys.argv[2]).read_text().splitlines() if line.strip() and not line.lstrip().startswith('#'))
assert {m['id']:m['directory'] for m in maps} == live
for m in maps:
assert m['mode'] in ('direct', 'entrance', 'gm', 'event')
target = byid[m['target'][0]]
for j in range(2):
assert target['base'][j] <= m['target'][j+1] < target['base'][j] + target['size'][j] * 25600, m
assert {m['id'] for m in maps if m['mode']=='entrance'} == {66,71,72,73,208,216,217,351}
assert byid[217]['target'][0] == 104 and byid[208]['target'][0] == 64
assert 'remove_item' not in quest and 'socket' not in quest and 'setqf' not in quest
# Run the actual generated functions/event body under the server's Lua 5.0.
helper = quest.split(' function destinations()',1)[1].split(' when 10031.sig_use begin',1)[0]
body = quest.split(' when 10031.sig_use begin',1)[1].rsplit(' end',1)[0]
script = 'ring_warp = {}\nfunction ring_warp.destinations()' + helper + '\nfunction use_ring()\n' + body + '\nend\n'
if len(sys.argv) > 3:
objects = Path(sys.argv[3])
state = (objects/'state/ring_warp').read_bytes().decode('gbk')
event = (objects/'10031/sig_use/ring_warp.start').read_bytes().decode('gbk')
script = state + '\nfunction use_ring()\n' + event + '\nend\n'
script += '''
local categories = ring_warp.destinations()
categories = catalog['categories']
assert len(categories) == 11, 'AVA2 11 regional categories expected'
# Validate the destinations against the 203 map catalogue, including overlapping map rectangles.
classic = json.loads((ROOT / 'maps_classic_65.json').read_text(encoding='utf-8'))
regions = {m['id']: m for c in classic['categories'] for m in c['maps']}
regions[210] = dict(base=[768000, 1408000], size=[3, 4])
expected = [[1, 3, 21, 23, 41, 43], [64]*4, [63]*4, [61]*3, [62]*3,
[104, 104, 71, 71], [67]*3+[68]*3, [69, 70], [72, 72, 73, 73],
[301]*3+[302]*3+[304]*3+[303]*3, [210]]
for category, map_ids in zip(categories, expected):
points = [d for group in category.get('subgroups', [category]) for d in group['destinations']]
assert len(points) == len(map_ids)
for point, map_id in zip(points, map_ids):
region = regions[map_id]
x, y = region['base']; w, h = region['size']
assert x <= point['x'] < x+w*25600 and y <= point['y'] < y+h*25600, (point, map_id)
if map_id == 64:
tower = regions[66]; tx, ty = tower['base']; tw, th = tower['size']
assert not (tx <= point['x'] < tx+tw*25600 and ty <= point['y'] < ty+th*25600), point
# Verify GBK safety
encoded = quest.encode('gbk')
assert b'\\' not in encoded, 'GBK backslash found'
# Prepare Lua 5.0 script runner
helper = quest.split(' function destinations()', 1)[1].split(' when 10031.sig_use', 1)[0]
body = quest.split(' when 10031.sig_use or 70058.use or 76042.use begin', 1)[1].rsplit(' end', 1)[0]
script = '''
NOWINDOW = 0
setskin = function(skin) end
say = function(t) end
ring_warp = {}
function ring_warp.destinations()
''' + helper + '''
function use_ring()
''' + body + '''
end
local queue = {}
local cursor = 0
local gm = false
local can_warp = true
local blocked_after_select = false
local level = 90
local warps = {}
pc = {
is_gm = function() return gm end,
can_warp = function() return can_warp end,
warp = function(x,y) table.insert(warps,{x,y}) end,
can_warp = function() return can_warp end,
get_level = function() return level end,
warp = function(x, y) table.insert(warps, {x, y}) end
}
say_title = function(text) end
say = function(text) end
wait = function() end
select_table = function(options)
assert(table.getn(options) <= 8, "menu too tall")
cursor = cursor + 1
local choice = queue[cursor]
assert(choice and choice >= 1 and choice <= table.getn(options), "bad selection")
return choice
select = function(...)
cursor = cursor + 1
local choice = queue[cursor]
assert(choice and choice >= 1 and choice <= table.getn(arg), "bad choice: " .. tostring(choice))
if blocked_after_select then can_warp = false end
return choice
end
select = function(...) return select_table(arg) end
local runs = 0
function run(answers, is_gm, allowed)
queue = answers; cursor = 0; gm = is_gm; can_warp = allowed; warps = {}
use_ring()
assert(cursor == table.getn(queue), "unexpected navigation")
runs = runs + 1
function run(answers, p_level, p_can_warp)
queue = answers; cursor = 0; level = p_level; can_warp = p_can_warp; warps = {}
use_ring()
assert(cursor == table.getn(queue), "navigation mismatch: cursor " .. cursor .. " expected " .. table.getn(queue))
return warps
end
for category = 1, table.getn(categories) do
local maps = categories[category].maps
for index = 1, table.getn(maps) do
local page = math.floor((index-1)/5)+1
local answer = {}
table.insert(answer, category)
for p = 1,page-1 do table.insert(answer, p==1 and 6 or 7) end
table.insert(answer, math.mod(index-1,5)+1)
local m = maps[index]
local blocked = m.mode == "gm" or m.mode == "event"
if blocked then
local count = math.min(5,table.getn(maps)-(page-1)*5)
local buttons = count + 1
if page > 1 then buttons = buttons + 1 end
if page*5 < table.getn(maps) then buttons = buttons + 1 end
table.insert(answer,buttons); table.insert(answer,7)
else table.insert(answer,1) end
run(answer,false,true)
assert(table.getn(warps) == (blocked and 0 or 1))
if not blocked then assert(warps[1][1]==m.x and warps[1][2]==m.y) end
end
end
run({7},false,true) -- close
run({1,6,2,7,7},false,true) -- next, previous, return list/category, close
run({1,1,1},false,false) -- blocked by active trade etc.
assert(table.getn(warps)==0)
run({6,1,1},true,true) -- GM test-map access
assert(table.getn(warps)==1 and warps[1][1]==categories[6].maps[1].x)
print("PASS: 65 map selections, paging, back/cancel, warp guard and GM access; " .. runs .. " runs")
-- Test 1: can_warp = false blocks immediately
local w = run({}, 100, false)
assert(table.getn(w) == 0, "warp should be blocked")
-- Test 2: Category 1 (各国主城), Dest 1 (盛唐一村)
local w = run({1, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 469300 and w[1][2] == 964200, "盛唐一村 failed")
-- Test 3: Category 1 (各国主城), Dest 4 (秦皇二村)
local w = run({1, 4}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 138500 and w[1][2] == 234900, "秦皇二村 failed")
-- Test 4: Category 2 (升龙谷), Dest 1 (中心)
local w = run({2, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 332800 and w[1][2] == 745700, "升龙谷中心 failed")
-- Test 5: Category 3 (影悲沙漠), Dest 2 (绿洲1区)
local w = run({3, 2}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 250600 and w[1][2] == 544200, "沙漠绿洲1区 failed")
-- Test 6: Category 4 (舒寒山), Dest 1 (雪山中心)
local w = run({4, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 434400 and w[1][2] == 214700, "雪山中心 failed")
-- Test 7: Category 5 (导炎大地), Dest 1 (火地中心)
local w = run({5, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 607400 and w[1][2] == 686800, "火地中心 failed")
-- Test 8: Category 6 (蜘蛛洞穴), direct cave warps (all 4 deep spots)
local w = run({6, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 60000 and w[1][2] == 496300, "蜘蛛一层入口 failed")
local w = run({6, 2}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 91800 and w[1][2] == 525400, "蜘蛛一层深处 failed")
local w = run({6, 3}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 704100 and w[1][2] == 463900, "蜘蛛二层入口 failed")
local w = run({6, 4}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 704000 and w[1][2] == 522700, "蜘蛛二层深处/三层入口 failed")
-- Test 9: Category 7 (鬼怪森林), Sub 1 (幽灵森林) & Sub 2 (红森林)
local w = run({7, 1, 1}, 70, true)
assert(table.getn(w) == 1 and w[1][1] == 290800 and w[1][2] == 5700, "幽灵森林入口 failed")
local w = run({7, 2, 3}, 70, true)
assert(table.getn(w) == 1 and w[1][1] == 1061400 and w[1][2] == 25800, "红森林练级区 2 failed")
-- Test 10: Category 8 (巨人荒地), Dest 1 & 2
local w = run({8, 1}, 60, true)
assert(table.getn(w) == 1 and w[1][1] == 1119200 and w[1][2] == 791500, "巨人领地 1区 failed")
local w = run({8, 2}, 60, true)
assert(table.getn(w) == 1 and w[1][1] == 830500 and w[1][2] == 764000, "蛇之领域 failed")
-- Test 11: Category 9 (流放洞窟), level check < 75 blocked
local w = run({9}, 70, true)
assert(table.getn(w) == 0, "流放洞窟 70级应被拦截")
-- Test 12: Category 9 (流放洞窟), level >= 75 deep cave warps (all 4 spots)
local w = run({9, 1}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 10000 and w[1][2] == 1207800, "流放一层入口 failed")
local w = run({9, 2}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 140300 and w[1][2] == 1323600, "流放一层深处 failed")
local w = run({9, 3}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 241600 and w[1][2] == 1273700, "流放二层入口 failed")
local w = run({9, 4}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 222000 and w[1][2] == 1220800, "龙穴门前 failed")
-- Test 13: Category 10 (尤哈拉海角), level < 90 blocked
local w = run({10}, 89, true)
assert(table.getn(w) == 0, "海角 89级应被拦截")
-- Test 14: Category 10 (尤哈拉海角), level >= 90 all 4 sub-regions
local w = run({10, 1, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1104800 and w[1][2] == 1788800, "龙头海角中心 failed")
local w = run({10, 2, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1225700 and w[1][2] == 1682000, "迷雾森林中心 failed")
local w = run({10, 3, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1134100 and w[1][2] == 1655400, "雷鸣山脉中心 failed")
local w = run({10, 4, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1087900 and w[1][2] == 1649800, "黑沙海湾中心 failed")
-- Test 15: Category 11 (附魔森林), level < 105 blocked
local w = run({11}, 104, true)
assert(table.getn(w) == 0, "附魔森林 104级应被拦截")
-- Test 16: Category 11 (附魔森林), level >= 105
local w = run({11, 1}, 105, true)
assert(table.getn(w) == 1 and w[1][1] == 816700 and w[1][2] == 1505000, "附魔森林 failed")
-- Test 17: Close main menu (option 12)
local w = run({12}, 100, true)
assert(table.getn(w) == 0, "关闭应无传送")
-- State can change while the destination menu is open.
blocked_after_select = true
local w = run({1, 1}, 120, true)
assert(table.getn(w) == 0, "state changed during menu must block warp")
blocked_after_select = false
print("PASS: 11 AVA2 regional categories, deep cave warps, level checks, and warp guard verified")
'''
lua = Path(sys.argv[1])
lua = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT.parents[3] / 'metin2-server/src/server/liblua'
with tempfile.TemporaryDirectory(prefix='ring-warp-test-') as d:
d = Path(d)
sources = [p for p in sorted((lua/'src').glob('*.c')) if p.name != 'ltests.c']
sources += [lua/'src/lib'/name for name in ('lauxlib.c','lbaselib.c','ltablib.c','lstrlib.c','lmathlib.c')]
command = ['cc','-w','-I'+str(lua/'include'),'-I'+str(lua/'src'),str(ROOT/'tests/lua_runner.c'),*[str(p) for p in sources],'-lm','-o',str(d/'lua')]
subprocess.run(command,check=True)
(d/'test.lua').write_bytes(script.encode('gbk'))
subprocess.run([str(d/'lua'),str(d/'test.lua')],check=True)
print('PASS: catalog matches server index, coordinates in bounds, GBK source safe')
sources = [p for p in sorted((lua / 'src').glob('*.c')) if p.name != 'ltests.c']
sources += [lua / 'src/lib' / name for name in ('lauxlib.c', 'lbaselib.c', 'ltablib.c', 'lstrlib.c', 'lmathlib.c')]
command = ['cc', '-w', '-I' + str(lua / 'include'), '-I' + str(lua / 'src'), str(ROOT / 'tests/lua_runner.c'),
*[str(p) for p in sources], '-lm', '-o', str(d / 'lua')]
subprocess.run(command, check=True)
(d / 'test.lua').write_bytes(script.encode('gbk'))
subprocess.run([str(d / 'lua'), str(d / 'test.lua')], check=True)
print('PASS: catalog matches AVA2 regional layout, coordinates in bounds, GBK source safe')