|
| 1 | +"""LightCycles replay renderer. |
| 2 | +
|
| 3 | +Parses a per-game ``sim_*.json`` written by the engine and renders the Tron grid: |
| 4 | +each cycle's growing trail plus its bright head, with a side panel of who's alive. |
| 5 | +
|
| 6 | +The JSON format (see engine.py ``write_replay``):: |
| 7 | +
|
| 8 | + {"width":40, "height":30, "num_players":2, "names":["p1","p2"], |
| 9 | + "winner": 0 | null, |
| 10 | + "frames":[{"t":0, "heads":[[x,y,alive], ...]}, ...]} |
| 11 | +
|
| 12 | +Frames store only each cycle's head (``[x, y, alive]``, ``alive`` is 1/0) per tick; |
| 13 | +the trail for a player is the set of every head cell it has occupied so far, which |
| 14 | +the renderer rebuilds by accumulating heads up to the frame being shown. ``winner`` |
| 15 | +is the winning player id, or ``null`` for a draw. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import json |
| 21 | + |
| 22 | +from codeclash.replay.base import ReplayData, ReplayRenderer |
| 23 | + |
| 24 | +DRAW_JS = """ |
| 25 | +const ARENA = (function(){ |
| 26 | + const PAL = ['#e5484d','#4593ff','#46a758','#f5d90a','#8e4ec6','#f76b15','#e93d82','#12a594']; |
| 27 | + const BG = '#0d1117', GRID_LINE = 'rgba(255,255,255,0.05)'; |
| 28 | + let W, H, CELL; |
| 29 | + function col(i){ return PAL[i % PAL.length]; } |
| 30 | + function setup(cv, G){ |
| 31 | + W = G.w; H = G.h; |
| 32 | + CELL = Math.max(6, Math.min(20, Math.floor(640 / W))); |
| 33 | + cv.width = W * CELL; cv.height = H * CELL; |
| 34 | + } |
| 35 | + function draw(ctx, cv, G, i){ |
| 36 | + ctx.fillStyle = BG; ctx.fillRect(0, 0, cv.width, cv.height); |
| 37 | + // faint grid |
| 38 | + ctx.strokeStyle = GRID_LINE; ctx.lineWidth = 1; |
| 39 | + for(let x=0;x<=W;x++){ ctx.beginPath(); ctx.moveTo(x*CELL,0); ctx.lineTo(x*CELL,cv.height); ctx.stroke(); } |
| 40 | + for(let y=0;y<=H;y++){ ctx.beginPath(); ctx.moveTo(0,y*CELL); ctx.lineTo(cv.width,y*CELL); ctx.stroke(); } |
| 41 | + const n = (G.frames[0].heads || []).length; |
| 42 | + // rebuild trails by accumulating every head cell up to frame i |
| 43 | + for(let p=0;p<n;p++){ |
| 44 | + ctx.fillStyle = col(p); ctx.globalAlpha = 0.55; |
| 45 | + for(let f=0;f<=i;f++){ |
| 46 | + const h = G.frames[f].heads[p]; |
| 47 | + ctx.fillRect(h[0]*CELL, h[1]*CELL, CELL, CELL); |
| 48 | + } |
| 49 | + ctx.globalAlpha = 1; |
| 50 | + } |
| 51 | + // heads on top (bright, outlined); crashed cycles get an X |
| 52 | + const fr = G.frames[i]; |
| 53 | + for(let p=0;p<n;p++){ |
| 54 | + const h = fr.heads[p], hx = h[0]*CELL, hy = h[1]*CELL; |
| 55 | + ctx.fillStyle = col(p); |
| 56 | + ctx.fillRect(hx, hy, CELL, CELL); |
| 57 | + ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; |
| 58 | + ctx.strokeRect(hx+1, hy+1, CELL-2, CELL-2); |
| 59 | + if(!h[2]){ // crashed |
| 60 | + ctx.strokeStyle = '#0d1117'; ctx.lineWidth = 2; |
| 61 | + ctx.beginPath(); |
| 62 | + ctx.moveTo(hx+3, hy+3); ctx.lineTo(hx+CELL-3, hy+CELL-3); |
| 63 | + ctx.moveTo(hx+CELL-3, hy+3); ctx.lineTo(hx+3, hy+CELL-3); |
| 64 | + ctx.stroke(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + function side(G, i){ |
| 69 | + const fr = G.frames[i], NM = G.names || [], n = fr.heads.length; |
| 70 | + const done = i === G.frames.length - 1; |
| 71 | + let rows = ''; |
| 72 | + for(let p=0;p<n;p++){ |
| 73 | + const alive = fr.heads[p][2]; |
| 74 | + // territory = distinct cells this cycle has occupied through frame i |
| 75 | + const cells = new Set(); |
| 76 | + for(let f=0;f<=i;f++){ const h=G.frames[f].heads[p]; cells.add(h[0]+','+h[1]); } |
| 77 | + rows += `<div class="team ${alive?'':'tdead'}"><div class="tname">` |
| 78 | + + `<span class="sw" style="background:${col(p)}"></span>` |
| 79 | + + `${NM[p] || ('player'+(p+1))} ${alive?'':'✗'}</div>` |
| 80 | + + `<div class="stat"><span>trail</span><b>${cells.size}</b></div></div>`; |
| 81 | + } |
| 82 | + const res = done ? (G.draw ? 'DRAW' : (G.winner || '\\u2014')) : ''; |
| 83 | + return rows |
| 84 | + + `<div class="stat"><span>tick</span><b>${fr.turn} / ${G.max_ticks||''}</b></div>` |
| 85 | + + (done ? `<div class="stat"><span>result</span><b>${res}</b></div>` : ''); |
| 86 | + } |
| 87 | + return {setup, draw, side}; |
| 88 | +})(); |
| 89 | +""" |
| 90 | + |
| 91 | + |
| 92 | +class LightCyclesReplayer(ReplayRenderer): |
| 93 | + arena = "LightCycles" |
| 94 | + sim_glob = "sim_*.json" |
| 95 | + DRAW_JS = DRAW_JS |
| 96 | + |
| 97 | + def parse(self, raw: bytes, players: list[dict] | None = None) -> ReplayData: |
| 98 | + log = json.loads(raw.decode(errors="replace")) |
| 99 | + w = log.get("width", 40) |
| 100 | + h = log.get("height", 30) |
| 101 | + n = log.get("num_players", 2) |
| 102 | + |
| 103 | + names = list(log.get("names", [])) |
| 104 | + if players: |
| 105 | + names = [p.get("name", names[i] if i < len(names) else f"player{i + 1}") for i, p in enumerate(players)] |
| 106 | + while len(names) < n: |
| 107 | + names.append(f"player{len(names) + 1}") |
| 108 | + |
| 109 | + frames = [{"turn": fr.get("t", idx), "heads": fr["heads"]} for idx, fr in enumerate(log.get("frames", []))] |
| 110 | + |
| 111 | + win = log.get("winner") |
| 112 | + draw = win is None |
| 113 | + winner = None if draw else names[win] if isinstance(win, int) and 0 <= win < len(names) else str(win) |
| 114 | + |
| 115 | + return ReplayData( |
| 116 | + w=w, |
| 117 | + h=h, |
| 118 | + frames=frames, |
| 119 | + winner=winner, |
| 120 | + draw=draw, |
| 121 | + extra={"names": names, "num_players": n, "max_ticks": log.get("max_ticks")}, |
| 122 | + ) |
| 123 | + |
| 124 | + def peek_winner(self, raw: bytes, players: list[dict] | None = None) -> tuple[str | None, bool] | None: |
| 125 | + log = json.loads(raw.decode(errors="replace")) |
| 126 | + win = log.get("winner") |
| 127 | + if win is None: |
| 128 | + return (None, True) |
| 129 | + names = list(log.get("names", [])) |
| 130 | + if players: |
| 131 | + names = [p.get("name", names[i] if i < len(names) else f"player{i + 1}") for i, p in enumerate(players)] |
| 132 | + name = names[win] if isinstance(win, int) and 0 <= win < len(names) else str(win) |
| 133 | + return (name, False) |
0 commit comments