"""Night Shift's levels (9 November 2026). Each level is laid by hand-tuned rules per chapter, then checked: the stored solution lights every mast with no cloud in the way at some cloud step, and the scrambled start isn't already solved. Writes NightLevels.kt. Run: python3 make_levels.py """ import random, sys W, H = 7, 5 N, E, S, Wd = 1, 2, 4, 8 DIRS = {N: (0, -1), E: (1, 0), S: (0, 1), Wd: (-1, 0)} OPP = {N: S, S: N, E: Wd, Wd: E} BASE = {'I': N | S, 'L': N | E, 'T': N | E | S, 'X': 15, 'S': N} def rot(mask, r): for _ in range(r % 4): mask = ((mask << 1) | (mask >> 3)) & 15 return mask def conns(t, r): return 15 if t == 'M' else (0 if t == '.' else rot(BASE[t], r)) def rot_for(t, want): return [r for r in range(4) if rot(BASE[t], r) == want] def lit(grid, clouds_at): """Tiles the light reaches: from the star along its beam, through pieces whose sides match; clouds block.""" sx, sy = next((x, y) for y in range(H) for x in range(W) if grid[y][x][0] == 'S') seen = {(sx, sy)}; todo = [(sx, sy)] while todo: x, y = todo.pop(); t, r = grid[y][x] for d in (N, E, S, Wd): if not conns(t, r) & d: continue nx, ny = x + DIRS[d][0], y + DIRS[d][1] if not (0 <= nx < W and 0 <= ny < H) or (nx, ny) in seen or (nx, ny) in clouds_at: continue nt, nr = grid[ny][nx] if nt == '.' or not conns(nt, nr) & OPP[d]: continue seen.add((nx, ny)); todo.append((nx, ny)) return seen def cloud_phases(lanes): n = 1 for l in lanes: n = n * (2 * len(l) - 2) if len(l) > 1 else n out = [] for k in range(max(n, 1)): at = set() for l in lanes: p = 2 * len(l) - 2; i = k % p if p else 0 at.add(l[i] if i < len(l) else l[p - i]) out.append(at) return out def solved(grid, lanes): masts = [(x, y) for y in range(H) for x in range(W) if grid[y][x][0] == 'M'] return any(all(m in lit(grid, c) for m in masts) for c in cloud_phases(lanes)) def walk(rng, start, goal, min_len, avoid, max_extra=4): """A path from start to goal at least min_len tiles between them and not much longer: it leans towards the goal, wandering more as the chapters go on.""" for _ in range(4000): path = [start]; seen = {start} while path[-1] != goal and len(path) < min_len + max_extra + 2: x, y = path[-1] opts = [(x + dx, y + dy) for dx, dy in DIRS.values() if 0 <= x + dx < W and 0 <= y + dy < H and (x + dx, y + dy) not in seen and (x + dx, y + dy) not in avoid] if not opts: break if len(path) - 1 >= min_len and goal in opts: nxt = goal else: opts = [o for o in opts if o != goal] or opts closer = [o for o in opts if abs(o[0] - goal[0]) + abs(o[1] - goal[1]) < abs(x - goal[0]) + abs(y - goal[1])] nxt = rng.choice(closer) if closer and rng.random() < .55 else rng.choice(opts) path.append(nxt); seen.add(nxt) if path[-1] == goal and min_len <= len(path) - 2 <= min_len + max_extra: return path return None def dir_of(a, b): for d, (dx, dy) in DIRS.items(): if (a[0] + dx, a[1] + dy) == b: return d def make(chapter, level, rng): for attempt in range(500): grid = [[('.', 0) for _ in range(W)] for _ in range(H)] # Star on the left, mast on the right (two masts in the Northern Relay and after). star = (0, rng.randrange(H)); mast = (W - 1, rng.randrange(H)) min_len = [5, 6, 7, 7, 8][chapter] + level // 4 path = walk(rng, star, mast, min_len, set()) if not path: continue sol = {} sol[star] = ('S', rot_for('S', dir_of(star, path[1]))[0]) for i in range(1, len(path) - 1): want = OPP[dir_of(path[i - 1], path[i])] | dir_of(path[i], path[i + 1]) t = 'I' if want in (N | S, E | Wd) else 'L' sol[path[i]] = (t, rot_for(t, want)[0]) sol[mast] = ('M', 0) masts = [mast] if chapter >= 3: # A second mast, fed by a tee on the path. ok = False for _ in range(60): i = rng.randrange(2, len(path) - 2); p = path[i] for d in (N, S, E, Wd): q = (p[0] + DIRS[d][0], p[1] + DIRS[d][1]) if not (0 <= q[0] < W and 0 <= q[1] < H) or q in sol: continue branch = walk(rng, q, (q[0], q[1]), 0, set()) # the second mast right beside the tee, or two steps on in a straight line q2 = (q[0] + DIRS[d][0], q[1] + DIRS[d][1]) t_want = OPP[dir_of(path[i - 1], p)] | dir_of(p, path[i + 1]) | d rs = rot_for('T', t_want) if not rs: continue if 0 <= q2[0] < W and 0 <= q2[1] < H and q2 not in sol and rng.random() < .6: sol[p] = ('T', rs[0]); sol[q] = ('I', rot_for('I', d | OPP[d])[0]); sol[q2] = ('M', 0); masts.append(q2) else: sol[p] = ('T', rs[0]); sol[q] = ('M', 0); masts.append(q) ok = True; break if ok: break if not ok: continue for (x, y), v in sol.items(): grid[y][x] = v # Decoys: more pieces on empty tiles as the chapters go on. empties = [(x, y) for y in range(H) for x in range(W) if grid[y][x][0] == '.'] rng.shuffle(empties) for (x, y) in empties[: [2, 5, 6, 7, 9][chapter] + level // 3]: grid[y][x] = (rng.choice('IILLT' if chapter >= 1 else 'IL'), rng.randrange(4)) # Clouds from Cloudbank on: one or two lanes across the path, ping-ponging a tile a hop. lanes = [] if chapter >= 2: for k in range(1 if chapter == 2 and level < 6 else 2): for _ in range(50): p = rng.choice(path[1:-1]) if rng.random() < .5: x0 = max(0, p[0] - rng.randrange(1, 3)); lane = [(x, p[1]) for x in range(x0, min(W, x0 + 3 + rng.randrange(2)))] else: y0 = max(0, p[1] - rng.randrange(1, 3)); lane = [(p[0], y) for y in range(y0, min(H, y0 + 3))] if len(lane) >= 3 and not any(c in masts or grid[c[1]][c[0]][0] == 'S' for c in lane) and not any(set(lane) & set(l) for l in lanes): lanes.append(lane); break solution = [row[:] for row in grid] if not solved(solution, lanes): continue # Scramble every turnable piece; the start mustn't already be solved. # The first levels turn only some of the path's pieces out of place, to learn on. keep = .6 if chapter == 0 and level < 4 else 0 start = [[(t, r if (t in 'ILT' and (x, y) in sol and rng.random() < keep) else ((r + rng.randrange(1, 4)) % 4 if t in 'ILT' else r)) for x, (t, r) in enumerate(row)] for y, row in enumerate(grid)] if solved(start, lanes): continue # Owl's par: the fewest turns that reach the stored solution (a straight is the same both ways round). turns = 0 lit_solution = lit(solution, set()) for y in range(H): for x in range(W): t, r0 = start[y][x]; t2, r1 = solution[y][x] if (x, y) in lit_solution and t in 'ILT': period = 2 if t == 'I' else 4 turns += (r1 - r0) % period par = turns + [2, 2, 3, 3, 4][chapter] return start, solution, lanes, par raise SystemExit(f"couldn't make chapter {chapter + 1} level {level + 1}") def enc(grid): return ['"' + ''.join(t + str(r) for t, r in row) + '"' for row in grid] rng = random.Random(9_112_026) chapters = ["First Light", "The Quiet Mast", "Cloudbank", "Northern Relay", "Last Light Before Dawn"] out = ['package tv.orbit.classic.arcade.nightshift', '', '// Generated by design/night-shift/make_levels.py (9 November 2026): five chapters of twelve, each checked solvable there', '// and again by NightShiftLevelsTest. Each row is 7 tiles of two characters: the piece (. empty, S star, M mast, I straight,', '// L corner, T tee) and its quarter turns. Clouds: each lane\'s tiles, a cloud moving one along it (and back) a hop.', '', 'internal val NightChapters: List = listOf('] for c, name in enumerate(chapters): out.append(f' NightChapter("{name}", listOf(') for l in range(12): start, solution, lanes, par = make(c, l, rng) lane_txt = 'listOf(' + ', '.join('listOf(' + ', '.join(f'{x} to {y}' for x, y in lane) + ')' for lane in lanes) + ')' out.append(f' NightLevel(listOf({", ".join(enc(start))}),') out.append(f' solution = listOf({", ".join(enc(solution))}),') out.append(f' par = {par}, clouds = {lane_txt}),') out.append(' )),') out.append(')') open(sys.argv[1], 'w').write('\n'.join(out) + '\n') print('ok')