"""第二階段:依手的區域把音符自動配置到 5x6 格子 規則 - L 軌只用左手區、R 軌只用右手區、M 軌只用中間欄;slide 可進中間欄但不跨到另一手 - 每隻手記住自己上一格;間隔越短,下一格只能離上一格越近(同一隻手連打不用大跳) - 同一格在音符顯示期間(判定前 APPEAR_MS ~ 判定後 EXPIRE_MS)不重複使用 - M 軌交給「目前空著、且最久沒按」的那隻手 - locked 的音符保留座標,但仍計入格子與手的佔用 - 固定亂數種子,結果可重現 """ import random from zones import COLS, ROWS, APPEAR_MS, EXPIRE_MS, TAP_COLS, SLIDE_COLS from chart import slide_steps CELL_MARGIN = 50 # 格子釋放後再多留一點緩衝(ms),避免掉幀時同格重疊 class HandState: def __init__(self, start): self.cell = start self.last_time = -10**9 # 上一次按下的時間 self.busy_until = -10**9 # hold / slide 進行中 def assign_hand(note, hands): """回傳負責這顆音符的手('L' / 'R'),M 軌選空著且最久沒按的手""" if note.lane in ("L", "R"): return note.lane free = [h for h in ("L", "R") if hands[h].busy_until < note.t] pool = free or ["L", "R"] return min(pool, key=lambda h: hands[h].last_time) def reach_for(gap): return 1 if gap < 140 else 2 if gap < 300 else max(COLS, ROWS) class ZonePlacer: def __init__(self, seed=1): self.rng = random.Random(seed) self.cell_free_at = {(x, y): -10**9 for x in range(COLS) for y in range(ROWS)} self.hands = {"L": HandState((1, 2)), "R": HandState((3, 2))} def _free(self, cell, t): return self.cell_free_at[cell] <= t - APPEAR_MS def _occupy(self, cell, t_end): self.cell_free_at[cell] = t_end + EXPIRE_MS + CELL_MARGIN def _pick(self, t, cols, hand): h = self.hands[hand] reach = reach_for(t - h.last_time) lx, ly = h.cell cells = [(x, y) for x in cols for y in range(ROWS)] cands = [c for c in cells if c != h.cell and self._free(c, t) and max(abs(c[0] - lx), abs(c[1] - ly)) <= reach] if not cands: cands = [c for c in cells if self._free(c, t)] if not cands: cands = [min(cells, key=lambda c: self.cell_free_at[c])] return self.rng.choice(cands) def _path(self, t0, t1, steps, cols, hand): h = self.hands[hand] interval = (t1 - t0) / max(1, steps - 1) start = self._pick(t0, cols, hand) path = [start] cur = start for i in range(1, steps): t = t0 + interval * i x, y = cur nbrs = [(x + dx, y + dy) for dx in (-1, 0, 1) for dy in (-1, 0, 1) if (dx or dy) and (x + dx) in cols and 0 <= y + dy < ROWS and (x + dx, y + dy) not in path and self._free((x + dx, y + dy), t)] # 傾向直線前進,看起來像一筆畫 if len(path) >= 2: d = (path[-1][0] - path[-2][0], path[-1][1] - path[-2][1]) straight = (x + d[0], y + d[1]) if straight in nbrs and self.rng.random() < 0.6: nbrs = [straight] if not nbrs: break cur = self.rng.choice(nbrs) path.append(cur) return path def _commit(self, note, hand): h = self.hands[hand] if note.type == "slide": k = len(note.path) interval = (note.end_time - note.t) / max(1, k - 1) for i, c in enumerate(note.path): self._occupy(c, note.t + interval * i) h.cell = note.path[-1] else: self._occupy(note.pos, note.end_time) h.cell = note.pos h.last_time = note.end_time if note.type != "tap" else note.t if note.type != "tap": h.busy_until = note.end_time def place(self, chart, only_unplaced=False): """配置 chart 內所有未鎖定的音符(only_unplaced=True 時只配置還沒有座標的)""" chart.sort() for note in chart.notes: hand = assign_hand(note, self.hands) keep = note.locked or (only_unplaced and note.placed()) if not keep: if note.type == "slide": steps = slide_steps(chart, note) note.path = self._path(note.t, note.end_time, steps, SLIDE_COLS[note.lane], hand) note.pos = None else: note.pos = self._pick(note.t, TAP_COLS[note.lane], hand) note.path = None if note.placed(): self._commit(note, hand) return chart def auto_place(chart, seed=1, only_unplaced=False): return ZonePlacer(seed).place(chart, only_unplaced)