"""Draw a sprite as a 2 x 2 block of tiles: for an icon that must match a sprite (the reserve ships at the bottom of the screen are tiles 4a 4b / 4c 4d in tile colour 3, and match the fighter's upright sprite 0x06). Each pixel keeps its colour: the sprite colour code's three colours are found among the tile colour code's. Writes `tile CODE HEX` lines for a mod. Usage: python3 tools/sprite_to_tiles.py --sprite-rom target/sprites-remix.bin \\ --sprite 06 --sprite-colour 9 --tiles 4a,4b,4c,4d --tile-colour 3 --check counts the pixels that differ from target/tiles.bin. The original icon is its own drawing, a near copy of sprite 0x06 (the same ship one pixel lower and a little shorter: on the original ROMs 44 of 256 pixels differ), so a match is close, not exact. target/tiles.bin and target/palette.json come from galaga-sprite-rom. """ import argparse, json import numpy as np ap = argparse.ArgumentParser() ap.add_argument("--sprite-rom", default="target/sprites.bin") ap.add_argument("--sprite", required=True, help="sprite code, hex") ap.add_argument("--sprite-colour", type=int, required=True) ap.add_argument("--tiles", required=True, help="four tile codes, hex: top left, top right, bottom left, bottom right") ap.add_argument("--tile-colour", type=int, required=True) ap.add_argument("--check", action="store_true") args = ap.parse_args() pal = json.load(open("target/palette.json")) srgb, trgb = pal["sprite_rgb"], pal["tile_rgb"] sl = pal["sprite_lookup"][args.sprite_colour * 4: args.sprite_colour * 4 + 4] tl = pal["tile_lookup"][args.tile_colour * 4: args.tile_colour * 4 + 4] value = {0: 0} for p in (1, 2, 3): want = srgb[sl[p]] found = [q for q in (1, 2, 3) if tl[q] != 15 and trgb[tl[q]] == want] if not found: raise SystemExit(f"sprite colour {want} is not in tile colour code {args.tile_colour}") value[p] = found[0] def pixel(byte, k): return 2 * ((byte >> (7 - k)) & 1) + ((byte >> (3 - k)) & 1) rom = open(args.sprite_rom, "rb").read() code = int(args.sprite, 16) # Work in the raster (the hardware's own frame, before the cabinet's # rotation): sprites and tiles share it, so no turning is needed. img = np.zeros((16, 16), np.uint8) # img[raster y][raster x] for y in range(16): for x in range(16): img[y, x] = value[pixel(rom[64 * code + 32 * (y // 8) + 8 * (x // 4) + (y % 8)], x % 4)] # The screen shows raster (x, y) at (223 - y, x): the icon's screen top-left # tile holds raster y 8-15, x 0-7; top right y 0-7, x 0-7; bottom left # y 8-15, x 8-15; bottom right y 0-7, x 8-15 (measured: the reserve ships). tiles = [int(t, 16) for t in args.tiles.split(",")] corner = [(8, 0), (0, 0), (8, 8), (0, 8)] out = {} for t, (y0, x0) in zip(tiles, corner): b = bytearray(16) for y in range(8): for x in range(8): p = int(img[y0 + y, x0 + x]) k = x % 4 b[y + 8 * (1 - x // 4)] |= ((p >> 1) & 1) << (7 - k) | (p & 1) << (3 - k) out[t] = bytes(b) if args.check: chars = open("target/tiles.bin", "rb").read() for t, b in out.items(): old = chars[16 * t:16 * t + 16] n = sum(pixel(old[i], k) != pixel(b[i], k) for i in range(16) for k in range(4)) print(f"tile {t:02x}: {n} of 64 pixels differ from the tile ROM") else: for t, b in out.items(): print(f"tile {t:02x} {b.hex()}")