#!/usr/bin/env python3 """Make data.h for raid.c: the sprite shapes (drawn here from polygons and ellipses, in the isometric directions the game uses) and the level. The world is a 2:1 isometric grid of tiles 32 pixels wide and 16 high. The ship flies along the rows, towards the top right of the screen; a level is a list of rows, numbered from where it starts. Each row is space or fortress floor, and a fortress row can end in a wall across the flight path: a low wall to fly over, or a tall one with a doorway to fly through. Ground objects stand on tiles: turrets that fire at you, fuel tanks to shoot for fuel, radar dishes for points, and the reactor core at the end of the level. """ import math import random import sys # ------------------------------------------------------------- drawing def blank(): return [[0] * 16 for _ in range(16)] def poly(img, pts, c): """Fill a polygon given in pixel coordinates (centres at +0.5).""" n = len(pts) for y in range(16): for x in range(16): px, py = x + 0.5, y + 0.5 inside = False for k in range(n): (x1, y1), (x2, y2) = pts[k], pts[(k + 1) % n] if (y1 > py) != (y2 > py): if px < x1 + (py - y1) * (x2 - x1) / (y2 - y1): inside = not inside if inside: img[y][x] = c def ellipse(img, cx, cy, rx, ry, c): for y in range(16): for x in range(16): if ((x + 0.5 - cx) / rx) ** 2 + ((y + 0.5 - cy) / ry) ** 2 <= 1: img[y][x] = c def rect(img, x0, y0, x1, y1, c): for y in range(y0, y1): for x in range(x0, x1): img[y][x] = c # the flight direction on screen (2, -1), and across the flight path, which in # the 2:1 isometric view is (2, 1) - the direction the tiles' other edges run F = (2 / math.sqrt(5), -1 / math.sqrt(5)) G = (2 / math.sqrt(5), 1 / math.sqrt(5)) def iso(pts, cx=8, cy=8, fwd=F): """Points given as (along, across) in the flight frame -> pixels.""" side = G return [(cx + a * fwd[0] + b * side[0], cy + a * fwd[1] + b * side[1]) for a, b in pts] def ship(body, wing, canopy, fwd=F): img = blank() poly(img, iso([(-2, -7.5), (1.5, -1.2), (1.5, 1.2), (-2, 7.5), (-4, 6.5), (-3, 0), (-4, -6.5)], fwd=fwd), wing) poly(img, iso([(7.5, 0), (2, -2.2), (-6.5, -1.8), (-6.5, 1.8), (2, 2.2)], fwd=fwd), body) poly(img, iso([(-6.5, -3.5), (-4, -0.5), (-4, 0.5), (-6.5, 3.5), (-7.5, 3), (-7.5, -3)], fwd=fwd), wing) ellipse(img, *iso([(2.5, 0)], fwd=fwd)[0], 1.6, 1.3, canopy) return img SHIP = ship(15, 12, 6) SHADOW = [[4 if c else 0 for c in row] for row in ship(1, 1, 1)] FIGHTER = ship(13, 1, 11, fwd=(-F[0], -F[1])) TURRET = blank() ellipse(TURRET, 8, 11.5, 6.5, 3.5, 8) ellipse(TURRET, 8, 9.5, 5, 3.5, 2) ellipse(TURRET, 7, 8.5, 2, 1.3, 10) poly(TURRET, [(6.5, 10), (8.5, 11), (3.5, 14.5), (2, 13.5)], 7) TANK = blank() rect(TANK, 3, 4, 13, 12, 9) ellipse(TANK, 8, 12, 5, 2.5, 1) rect(TANK, 3, 4, 13, 12, 9) ellipse(TANK, 8, 4, 5, 2.5, 11) for y in range(6, 11): TANK[y][5] = 15 rect(TANK, 7, 6, 9, 11, 1) rect(TANK, 7, 6, 11, 7, 1) rect(TANK, 7, 8, 10, 9, 1) RADAR = blank() rect(RADAR, 7, 7, 9, 14, 7) ellipse(RADAR, 8, 14, 4, 1.6, 8) poly(RADAR, [(2, 2), (13, 5), (12, 8), (3, 6)], 14) poly(RADAR, [(4, 3.5), (11, 5.5), (10.5, 6.8), (4.5, 5)], 6) CORE = blank() poly(CORE, [(8, 0.5), (15, 7), (8, 14.5), (1, 7)], 5) poly(CORE, [(8, 3), (12.5, 7), (8, 11.5), (3.5, 7)], 13) ellipse(CORE, 8, 7, 2.2, 2.2, 15) def burst(seed, cols): r = random.Random(seed) img = blank() for k in range(70): a = r.uniform(0, 2 * math.pi) d = r.uniform(0, 7) ** 0.9 x, y = int(8 + d * math.cos(a)), int(8 + d * math.sin(a) * 0.8) if 0 <= x < 16 and 0 <= y < 16: img[y][x] = cols[0] if d < 2.5 else cols[1] if d < 5 else cols[2] return img BOOM1 = burst(3, (15, 11, 9)) BOOM2 = burst(5, (11, 9, 1)) SHOT = blank() ellipse(SHOT, 8, 8, 1.6, 1.2, 15) SHOT[8][8] = 11 SHELL = blank() ellipse(SHELL, 8, 8, 1.8, 1.8, 9) SHELL[8][8] = 15 SHAPES = [("S_SHIP", SHIP), ("S_SHADOW", SHADOW), ("S_FIGHTER", FIGHTER), ("S_TURRET", TURRET), ("S_TANK", TANK), ("S_RADAR", RADAR), ("S_CORE", CORE), ("S_BOOM1", BOOM1), ("S_BOOM2", BOOM2), ("S_SHOT", SHOT), ("S_SHELL", SHELL)] # --------------------------------------------------------------- level BW = 10 # fortress width in tiles LOW_H, TALL_H, DOOR_H = 20, 56, 30 # row byte: bit 0 fortress floor; bits 2:1 wall at the far edge of the row # (0 none, 1 low, 2 tall with a door); bits 6:3 the door's first tile; # bit 7 fighters attack (space) FORT, WALL_LOW, WALL_DOOR, FIGHTERS = 1, 2, 4, 128 O_TURRET, O_TANK, O_RADAR, O_CORE = 1, 2, 3, 4 def level(): rnd = random.Random(1983) rows = [] objs = [] def space(n, fighters): rows.extend([FIGHTERS if fighters else 0] * n) def fortress(n, walls, turrets, tanks, radars, core=False): start = len(rows) rows.extend([FORT] * n) wall_rows = [] r = start + 10 k = 0 while r < start + n - 6 and k < len(walls): if walls[k] == "L": rows[r] |= WALL_LOW else: rows[r] |= WALL_DOOR | (rnd.randrange(0, BW - 1) << 3) wall_rows.append(r) r += rnd.randrange(9, 14) k += 1 # objects: not on the rows just behind a wall, where its face hides them hidden = {w + d for w in wall_rows for d in range(0, 4)} free = [(rr, i) for rr in range(start + 4, start + n - 3) for i in range(1, BW - 1) if rr not in hidden] rnd.shuffle(free) used = set() def place(t, count): nonlocal free for _ in range(count): while free: rr, i = free.pop() if all(abs(rr - a) > 1 or abs(i - b) > 1 for a, b in used): used.add((rr, i)); objs.append((rr, t, i)); break place(O_TURRET, turrets) place(O_TANK, tanks) place(O_RADAR, radars) if core: objs.append((start + n - 3, O_CORE, BW // 2)) space(14, False) fortress(120, "LDLDDLDD", 10, 7, 3) space(60, True) fortress(140, "DLDDLDDLD", 13, 7, 3, core=True) space(16, False) objs.sort() return rows, objs def main(out): rows, objs = level() L = ["/* made by gen.py - edit that instead */", ""] for i, (n, _) in enumerate(SHAPES): L.append("#define %s %d" % (n, i)) L.append("#define N_SHAPES %d" % len(SHAPES)) L.append("#define BW %d" % BW) L.append("#define LOW_H %d\n#define TALL_H %d\n#define DOOR_H %d" % (LOW_H, TALL_H, DOOR_H)) L.append("#define R_FORT 1\n#define R_WALL 6\n#define R_LOW 2\n#define R_DOOR 4\n#define R_FIGHTERS 128") L.append("#define O_TURRET %d\n#define O_TANK %d\n#define O_RADAR %d\n#define O_CORE %d" % (O_TURRET, O_TANK, O_RADAR, O_CORE)) L.append("static const u8 shape_data[N_SHAPES * 128] = {") for n, img in SHAPES: b = [img[y][x] << 4 | img[y][x + 1] for y in range(16) for x in range(0, 16, 2)] L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % n) L.append("};") L.append("#define LEVEL_ROWS %d" % len(rows)) L.append("static const u8 level_rows[LEVEL_ROWS] = {") for k in range(0, len(rows), 20): L.append(" " + ",".join(str(v) for v in rows[k:k + 20]) + ",") L.append("};") L.append("#define N_OBJS %d" % len(objs)) L.append("/* row (2 bytes), type, tile across */") L.append("static const u8 level_objs[N_OBJS * 4] = {") for r, t, i in objs: L.append(" %d,%d,%d,%d," % (r & 255, r >> 8, t, i)) L.append("};") open(out, "w").write("\n".join(L) + "\n") walls = sum(1 for r in rows if r & 6) print("data.h: %d shapes, %d rows, %d walls, %d objects" % (len(SHAPES), len(rows), walls, len(objs))) if __name__ == "__main__": if len(sys.argv) > 2 and sys.argv[2] == "--show": for n, img in SHAPES: print(n) for row in img: print("".join(" .:-=+*#%@ABCDEF"[c] if c else " " for c in row)) main(sys.argv[1] if len(sys.argv) > 1 else "data.h")