#!/usr/bin/env python3 """Make data.h for cavern.c: the maze and the sprite shapes. The maze is 28 x 29 tiles of 8 x 8 pixels, mirrored left to right. It is built from a grid of junctions joined by straight corridors: every link is made, then links are taken away at random as long as every junction keeps at least two, the whole cave stays connected and the fixed parts (the crossings at the roost and the start, the side tunnels) stay. So there are no dead ends, which a chase game can't have. The bats' roost sits in the middle. Tiles: 0 open, 1 wall, 2 crystal, 3 lantern, 4 roost door, 5 roost, 6 tunnel. """ import random import sys from collections import deque W, H = 28, 29 NX = [1, 5, 9, 12] # junction columns on the left (12 mirrors to 15) NY = [1, 5, 9, 14, 19, 23, 27] # junction rows ROOST = (10, 11, 17, 16) # cols 10-17, rows 11-16 (x0, y0, x1, y1) TUNNEL_Y = 14 START = (13, 23) # the miner starts between 13 and 14 on row 23 LANTERNS = [(1, 5), (1, 23)] # and their mirror images def mirror(x): return W - 1 - x def build(seed): rnd = random.Random(seed) nodes = {(x, y) for x in NX for y in NY} nodes.discard((12, 14)) # inside the roost edges = set() for y in NY: row = [x for x in NX if (x, y) in nodes] for a, b in zip(row, row[1:]): if not (y == 14 and b == 12): edges.add(((a, y), (b, y))) if (12, y) in nodes: edges.add(((12, y), (15, y))) # across the middle for x in NX: col = [y for y in NY if (x, y) in nodes] for a, b in zip(col, col[1:]): if x == 12 and a < ROOST[3] and b > ROOST[1]: continue # would run through the roost edges.add(((x, a), (x, b))) fixed = {((12, 9), (15, 9)), ((12, 23), (15, 23))} def full(es): g = {} for (a, b) in es: for p, q in ((a, b), (b, a)): g.setdefault(p, set()).add(q) if a[0] == 12 and b[0] == 15: continue ma, mb = (mirror(a[0]), a[1]), (mirror(b[0]), b[1]) g.setdefault(ma, set()).add(mb) g.setdefault(mb, set()).add(ma) return g def ok(es): g = full(es) # the tunnel ends count as a link for (1, 14) deg = {n: len(v) + (1 if n[1] == TUNNEL_Y and n[0] in (1, mirror(1)) else 0) for n, v in g.items()} if any(d < 2 for d in deg.values()): return False start = next(iter(g)) seen, q = {start}, deque([start]) while q: for m in g[q.popleft()]: if m not in seen: seen.add(m); q.append(m) return len(seen) == len(g) order = sorted(edges - fixed) rnd.shuffle(order) for e in order: if rnd.random() < 0.45 and ok(edges - {e}): edges.discard(e) assert ok(edges) grid = [[1] * W for _ in range(H)] def carve(x, y, t=2): for xx in (x, mirror(x)): if grid[y][xx] == 1: grid[y][xx] = t for (a, b) in edges: if a[1] == b[1]: for x in range(min(a[0], b[0]), max(a[0], b[0]) + 1): carve(x, a[1]) else: for y in range(min(a[1], b[1]), max(a[1], b[1]) + 1): carve(a[0], y) # the roost: walls round a room, a door at the top, a way up to row 9 x0, y0, x1, y1 = ROOST for y in range(y0, y1 + 1): for x in range(x0, x1 + 1): grid[y][x] = 1 if y in (y0, y1) or x in (x0, x1) else 5 grid[y0][13] = grid[y0][14] = 4 grid[10][13] = grid[10][14] = 0 # tunnels: from the side junctions out through the wall for x in (0,): carve(x, TUNNEL_Y, 6) for x in range(1, NX[0] + 1): if grid[TUNNEL_Y][x] == 1: carve(x, TUNNEL_Y, 6) # no crystals in the tunnel, by the roost door, or where the miner starts for x in range(0, 6): for xx in (x, mirror(x)): if grid[TUNNEL_Y][xx] == 2: grid[TUNNEL_Y][xx] = 6 for x in (12, 13, 14, 15): if grid[9][x] == 2: grid[9][x] = 0 for x in (13, 14): grid[START[1]][x] = 0 for (x, y) in LANTERNS: assert grid[y][x] == 2, (x, y) grid[y][x] = grid[y][mirror(x)] = 3 return grid def check(grid): open_t = lambda t: t in (0, 2, 3, 6) for y in range(H): for x in range(W): if not open_t(grid[y][x]): continue n = 0 for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): nx, ny = (x + dx) % W, y + dy if 0 <= ny < H and (open_t(grid[ny][nx]) or grid[ny][nx] == 4): n += 1 assert n >= 2, "dead end at %d,%d" % (x, y) for y in range(H): assert grid[y] == grid[y][::-1], "row %d is not mirrored" % y # ---------------------------------------------------------------- shapes Z = "0" * 16 MINER_A = [Z, "00000BBBBBB00000", "0000B3333333F000", # helmet with a lamp "00033333333FF000", "0009999999990000", "0009909909990000", "0009999999990000", "0000999009900000", "000CCCCCCCCC0000", "00CCCCCCCCCC9000", "009CCCCCCCCC0000", "000444444444000", "0004440004440000", "0007700000770000", ] + [Z] * 2 MINER_A[11] = MINER_A[11] + "0" MINER_B = MINER_A[:11] + [ "0004444444440000", "0000444044400000", "0000077077000000", ] + [Z] * 2 def bat(c, frame): if frame == 0: rows = [Z, Z, "0C000000000000C0", "0CC0000000000CC0", "0CCC00CCCC00CCC0", "0CCCCCCCCCCCCCC0", "00CCCFCCCCFCCC00", "000CCCCCCCCCC000", "0000C0CCCC0C0000", "00000C0CC0C00000", "000000000000000" + "0"] + [Z] * 5 else: rows = [Z, Z, Z, Z, "000000CCCC000000", "0C0CCCCCCCCCC0C0", "0CCCCFCCCCFCCCC0", "0CCCCCCCCCCCCCC0", "0CC0CCCCCCCC0CC0", "0C0000CCCC0000C0", "00000C0000C00000"] + [Z] * 5 return [r.replace("C", c) for r in rows] EYES = [Z] * 6 + ["0000FF0000FF0000", "0000F40000F40000"] + [Z] * 8 NUGGET = [Z] * 4 + [ "000000BBB0000000", "0000BBBFBBB00000", "000BBFBBBBBB0000", "000BBBBBB3BB0000", "0000BB3BBBB00000", "00000BBBB0000000", ] + [Z] * 6 BOOM = [Z, "000F00000000F000", "0000F00BB00F0000", "00000F0BB0F00000", "000000BFFB000000", "0BBB0BFFFFB0BBB0", "000000BFFB000000", "00000F0BB0F00000", "0000F00BB00F0000", "000F00000000F000", ] + [Z] * 6 SHAPES = [("S_MINER", MINER_A), ("S_MINER2", MINER_B), ("S_BAT_R", bat("9", 0)), ("S_BAT_R2", bat("9", 1)), ("S_BAT_M", bat("D", 0)), ("S_BAT_M2", bat("D", 1)), ("S_BAT_G", bat("A", 0)), ("S_BAT_G2", bat("A", 1)), ("S_BAT_O", bat("3", 0)), ("S_BAT_O2", bat("3", 1)), ("S_SCARED", bat("4", 0)), ("S_SCARED2", bat("4", 1)), ("S_FLASH", bat("F", 0)), ("S_FLASH2", bat("F", 1)), ("S_EYES", EYES), ("S_NUGGET", NUGGET), ("S_BOOM", BOOM)] def main(out, seed=11): grid = build(seed) check(grid) crystals = sum(r.count(2) + r.count(3) for r in grid) L = ["/* made by gen.py - edit that instead */", ""] L.append("#define MW %d\n#define MH %d" % (W, H)) L.append("#define START_X %d\n#define START_Y %d\n#define TUNNEL_Y %d" % (START[0], START[1], TUNNEL_Y)) L.append("#define CRYSTALS %d" % crystals) L.append("static const u8 maze_start[MH][MW] = {") for row in grid: L.append(" {" + ",".join(str(t) for t in row) + "},") L.append("};") for i, (name, img) in enumerate(SHAPES): assert len(img) == 16 and all(len(r) == 16 for r in img), name L.append("#define %s %d" % (name, i)) L.append("#define N_SHAPES %d" % len(SHAPES)) L.append("static const u8 shape_data[N_SHAPES * 128] = {") for name, img in SHAPES: b = [int(r[x], 16) << 4 | int(r[x + 1], 16) for r in img for x in range(0, 16, 2)] L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name) L.append("};") open(out, "w").write("\n".join(L) + "\n") print("data.h: maze %d x %d, %d crystals, %d shapes" % (W, H, crystals, len(SHAPES))) return grid if __name__ == "__main__": g = main(sys.argv[1] if len(sys.argv) > 1 else "data.h", int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2].isdigit() else 11) if "--show" in sys.argv: for row in g: print("".join(" ##..oo==-- "[t * 2:t * 2 + 2][0] if False else " #.o=- "[t] for t in row))