Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
#!/usr/bin/env python3
"""Packs levels/microban.txt into output/levels.inc for the game.
The levels stay as Microban has them, in the usual letters (. $ * @ +, '-' for the floor), but for the
walls: the game puts one on every cell next to the floor, so only the few others are written ('#'),
and '_' where there is none next to the floor. The rows end with a newline, a level with '|'. The text
is compressed with raw DEFLATE (zopfli when it is there); the game inflates it when it opens a level.
python3 tools/levels.py OUTDIR [LEVELS] (the first LEVELS levels; all of them by default)
Each level is checked as it is packed: one keeper, as many goals as boxes, the floor walled in and
small enough for the game's grid (tools/solve.py solves them all).
"""
import os
import sys
import zlib
HERE = os.path.dirname(os.path.abspath(__file__))
SOURCE = os.path.join(HERE, "..", "levels", "microban.txt")
MAXW, MAXH = 32, 18 # with the walls around: what the game's grid holds (a column to spare)
def read(path=SOURCE):
levels, rows = [], []
for line in open(path).read().split("\n") + [""]:
if line.startswith(";"):
continue
if line.strip():
rows.append(line.rstrip())
elif rows:
levels.append(rows)
rows = []
return levels
def reach(rows):
"""The cells the keeper can reach, boxes or not, from the keeper."""
h, w = len(rows), max(map(len, rows))
g = [r.ljust(w) for r in rows]
start = [(y, x) for y in range(h) for x in range(w) if g[y][x] in "@+"]
assert len(start) == 1, "one keeper"
seen, todo = {start[0]}, [start[0]]
while todo:
y, x = todo.pop()
for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = (y + dy, x + dx)
if 0 <= n[0] < h and 0 <= n[1] < w and g[n[0]][n[1]] != "#" and n not in seen:
assert 0 < n[0] < h - 1 and 0 < n[1] < w - 1, "the floor is walled in"
seen.add(n)
todo.append(n)
return g, seen
def pack(rows):
"""The level's rows as the game reads them: the floor ('-'), the goals, the boxes and the keeper, and
the walls only where they aren't the cells around the floor: '#' a wall there too, '_' none."""
g, inside = reach(rows)
h, w = len(g), len(g[0])
things = {(y, x) for y in range(h) for x in range(w) if g[y][x] in ".$*@+"}
floor = inside | things # (boxes on goals walled in out of reach are drawn as Microban has them)
walls = {(y, x) for y in range(h) for x in range(w) if g[y][x] == "#"}
around = {(y + dy, x + dx) for y, x in floor for dy in (-1, 0, 1) for dx in (-1, 0, 1)} - floor
cells = floor | walls | around
ys, xs = [y for y, _ in cells], [x for _, x in cells]
out = []
for y in range(min(ys), max(ys) + 1):
r = ""
for x in range(min(xs), max(xs) + 1):
p, c = (y, x), g[y][x] if 0 <= y < h and 0 <= x < w else " "
r += ("-" if c == " " else c) if p in floor else "#" if p in walls - around else \
"_" if p in around - walls else " "
out.append(r.rstrip())
text = "".join(out)
assert text.count("$") + text.count("*") == text.count(".") + text.count("+") + text.count("*") > 0
assert text.count("$") > 0, "something to push"
assert max(map(len, out)) + 1 <= MAXW and len(out) <= MAXH, "fits the grid"
return out
def deflate(data):
try:
import zopfli.zlib
return zopfli.zlib.compress(data, numiterations=100)[2:-4]
except ImportError:
c = zlib.compressobj(9, zlib.DEFLATED, -15, 9)
return c.compress(data) + c.flush()
def main():
outdir = sys.argv[1]
levels = [pack(r) for r in read()]
every = len(levels)
if len(sys.argv) > 2:
levels = levels[:int(sys.argv[2])]
text = "".join("\n".join(r) + "|" for r in levels).encode()
assert len(text) <= 320 * 16 * 2, "fits the strip buffer the game unpacks it into"
z = deflate(text)
lines = ["/* Generated by tools/levels.py from levels/microban.txt. */",
f"#define LEVELS {len(levels)}", f"#define ALL_LEVELS {every}", f"#define TEXT_BYTES {len(text)}",
f"#define MAXW {MAXW} /* the grid, walls around included */", f"#define MAXH {MAXH}",
f"static const uint8_t levels_z[{len(z)}] = {{"]
for i in range(0, len(z), 24):
lines.append(" " + ",".join(str(b) for b in z[i:i + 24]) + ",")
lines.append("};")
os.makedirs(outdir, exist_ok=True)
open(os.path.join(outdir, "levels.inc"), "w").write("\n".join(lines) + "\n")
print(f"levels: {len(levels)}, {len(text)} bytes of text, {len(z)} packed")
if __name__ == "__main__":
main()