Files
2026-10-22 21:07:47 +08:00

102 lines
3.6 KiB
Python

"""譜面檢查:未配置、區域違規、越界、同格顯示重疊、同一隻手同時按壓
用法(CLI):python validate.py chart.lanes.json
"""
import sys
from zones import COLS, ROWS, APPEAR_MS, EXPIRE_MS, cell_allowed
def _events(chart):
"""與遊戲 MusicPlayer 相同的展開方式:(hit_time, x, y, 結束時間, 音符 index)"""
ev = []
for i, n in enumerate(chart.notes):
if n.type == "slide" and n.path:
k = len(n.path)
iv = (n.end_time - n.t) / max(1, k - 1)
ev += [(n.t + iv * j, x, y, n.t + iv * j, i) for j, (x, y) in enumerate(n.path)]
elif n.pos is not None:
ev.append((n.t, n.pos[0], n.pos[1], n.end_time, i))
ev.sort()
return ev
def validate(chart):
"""回傳 [(音符 index, 訊息)],空串列表示沒有問題"""
issues = []
chart.sort()
for i, n in enumerate(chart.notes):
if not n.placed():
issues.append((i, "尚未配置座標"))
continue
cells = n.path if n.type == "slide" else [n.pos]
for x, y in cells:
if not (0 <= x < COLS and 0 <= y < ROWS):
issues.append((i, f"座標超出範圍 ({x},{y})"))
elif not cell_allowed(n.lane, n.type, x, y):
issues.append((i, f"({x},{y}) 不在 {n.lane} 軌可用的區域"))
if n.type in ("hold", "slide") and (n.end is None or n.end <= n.t):
issues.append((i, "結束時間必須晚於開始時間"))
if n.type == "slide" and n.path:
for (ax, ay), (bx, by) in zip(n.path, n.path[1:]):
if max(abs(ax - bx), abs(ay - by)) != 1:
issues.append((i, "slide 路徑必須走相鄰格"))
break
# 同格在顯示期間重疊
free_at = {}
for t, x, y, end, i in _events(chart):
if (x, y) in free_at and t - APPEAR_MS < free_at[(x, y)]:
issues.append((i, f"({x},{y}) 與前一顆音符顯示重疊"))
free_at[(x, y)] = end + EXPIRE_MS
# 同一隻手同時按壓(M 交給空著的手)
busy = {"L": [], "R": []} # 每隻手的 (start, end)
def overlaps(hand, s, e):
return any(not (e < bs or s > be) for bs, be in busy[hand])
for i, n in enumerate(chart.notes):
s, e = n.t, n.end_time
if n.lane in ("L", "R"):
if overlaps(n.lane, s, e):
issues.append((i, f"{'左' if n.lane == 'L' else '右'}手同時有兩個按壓"))
busy[n.lane].append((s, e))
else:
free = [h for h in ("L", "R") if not overlaps(h, s, e)]
if not free:
issues.append((i, "中間欄的音符沒有空著的手"))
else:
busy[free[0]].append((s, e))
issues.sort()
return issues
def _report(label, c):
found = validate(c)
print(f"{label}: {len(c.notes)} notes, {len(found)} issues")
for i, msg in found[:50]:
print(f" #{i} t={c.notes[i].t} {c.notes[i].lane} {c.notes[i].type}: {msg}")
return len(found)
if __name__ == "__main__":
# 用法:python validate.py 曲包.zip | 舊版.lanes.json ...
from chart import Chart
total = 0
for p in sys.argv[1:]:
if p.lower().endswith(".zip"):
from pack import open_pack, validate_pack
pack = open_pack(p)
for msg in validate_pack(pack):
print(f"{p}: {msg}")
total += 1
for d, c in pack.charts.items():
total += _report(f"{p} [{d}]", c)
pack.close()
else:
total += _report(p, Chart.load(p))
sys.exit(1 if total else 0)