Skip to content

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.

circloO: tests, the icon, and a ball that no longer sticks on fast slides

Mason363988f64b2badd
diff --git a/games/circloo/src/game.c b/games/circloo/src/game.c
index 8e2a03c5..143ff8bf 100644
--- a/games/circloo/src/game.c
+++ b/games/circloo/src/game.c
@@ -185,6 +185,9 @@ void game_init(void) {
g.running = true;
g.pick[0] = first_pick(0), g.pick[1] = first_pick(1);
main_menu();
+#ifdef CO_START /* (speed tests: make EXTRA=-DCO_START=9, or 106 for hard level 6) */
+ start(CO_START % 100, CO_START > 100);
+#endif
}
static void act(int item) {
diff --git a/games/circloo/src/icon.png b/games/circloo/src/icon.png
new file mode 100644
index 00000000..83883b19
Binary files /dev/null and b/games/circloo/src/icon.png differ
diff --git a/games/circloo/src/level.c b/games/circloo/src/level.c
index 5a243121..0c5b1cfd 100644
--- a/games/circloo/src/level.c
+++ b/games/circloo/src/level.c
@@ -228,6 +228,10 @@ static void grow(void) {
boundary();
}
+#ifdef HOST
+void level_debug_grow(void) { grow(); } /* (tests: the next ring now) */
+#endif
+
static void take(Coll *c) {
c->taken = 1;
L.taken++;
@@ -235,6 +239,16 @@ static void take(Coll *c) {
else L.complete = true;
}
+#ifdef HOST
+void level_debug_take(void) { /* (tests: the next circle taken) */
+ for (int k = 0; k < L.ncoll; k++)
+ if (!L.coll[k].taken && !L.complete) {
+ take(&L.coll[k]);
+ return;
+ }
+}
+#endif
+
void level_tick(uint32_t keys) {
Body *ball = L.ball >= 0 ? &W.b[L.ball] : NULL;
if (ball && !L.complete) {
diff --git a/games/circloo/src/phys.c b/games/circloo/src/phys.c
index bc9b982c..6e6afaac 100644
--- a/games/circloo/src/phys.c
+++ b/games/circloo/src/phys.c
@@ -764,8 +764,8 @@ static bool joints_positions(void) {
return ok;
}
-/* a fast circle may not jump through a wall: it stops where its sweep first meets one (Box2D sweeps every
- * moving body against the static ones) */
+/* a fast circle may not jump through a wall: where its step would take it through one (or deep into it), it stops
+ * where it first meets it, its speed into the wall gone (Box2D sweeps every moving body against the static ones) */
static float sweep_circle(float x0, float y0, float dx, float dy, float cx, float cy, float R) {
float fx = x0 - cx, fy = y0 - cy;
float a = dx * dx + dy * dy, b = fx * dx + fy * dy, c = fx * fx + fy * fy - R * R;
@@ -779,9 +779,9 @@ static float sweep_circle(float x0, float y0, float dx, float dy, float cx, floa
static void sweep(Body *b) {
float dx = b->x - b->sx, dy = b->y - b->sy;
if (dx * dx + dy * dy < 0.25f * b->r * b->r) return;
- float R = b->r - SLOP, best = 1;
- float x0 = fminf(b->sx, b->x) - b->r, x1 = fmaxf(b->sx, b->x) + b->r;
- float y0 = fminf(b->sy, b->y) - b->r, y1 = fmaxf(b->sy, b->y) + b->r;
+ float r = b->r, best = 1, bnx = 0, bny = 0;
+ float x0 = fminf(b->sx, b->x) - r, x1 = fmaxf(b->sx, b->x) + r;
+ float y0 = fminf(b->sy, b->y) - r, y1 = fmaxf(b->sy, b->y) + r;
if (++W.stamp_now == 0) memset(W.stamp, 0, sizeof W.stamp), W.stamp_now = 1;
for (int cy = cell_of(y0); cy <= cell_of(y1); cy++)
for (int cx = cell_of(x0); cx <= cell_of(x1); cx++) {
@@ -790,31 +790,37 @@ static void sweep(Body *b) {
int s = W.cell_items[t];
if (W.stamp[s] == W.stamp_now) continue;
W.stamp[s] = W.stamp_now;
- float hit = 1;
- if (s < MAX_SCIRCLES) hit = sweep_circle(b->sx, b->sy, dx, dy, W.sc[s].x, W.sc[s].y, W.sc[s].r + R);
- else if (s < MAX_SCIRCLES + MAX_SEDGES) {
+ if (s < MAX_SCIRCLES) {
+ const SCircle *o = &W.sc[s];
+ float ex = b->x - o->x, ey = b->y - o->y;
+ if (ex * ex + ey * ey > (o->r + 0.5f * r) * (o->r + 0.5f * r)) continue; /* (not through, nor deep) */
+ float h = sweep_circle(b->sx, b->sy, dx, dy, o->x, o->y, o->r + r);
+ if (h < best) {
+ best = h;
+ float px = b->sx + dx * h - o->x, py = b->sy + dy * h - o->y, l = sqrtf(px * px + py * py);
+ bnx = px / l, bny = py / l;
+ }
+ } else if (s < MAX_SCIRCLES + MAX_SEDGES) {
const SEdge *e = &W.se[s - MAX_SCIRCLES];
float ex = e->x2 - e->x1, ey = e->y2 - e->y1, el = sqrtf(ex * ex + ey * ey);
if (el < 1e-9f) continue;
float nx = ey / el, ny = -ex / el;
- float d0 = (b->sx - e->x1) * nx + (b->sy - e->y1) * ny, dd = dx * nx + dy * ny;
- if (d0 < 0) d0 = -d0, dd = -dd;
- if (d0 > R && dd < 0) {
- float t = (d0 - R) / -dd;
- if (t < 1) {
- float px = b->sx + dx * t, py = b->sy + dy * t;
- float u = ((px - e->x1) * ex + (py - e->y1) * ey) / (el * el);
- if (u >= 0 && u <= 1) hit = t;
- }
- }
- float h1 = sweep_circle(b->sx, b->sy, dx, dy, e->x1, e->y1, R), h2 = sweep_circle(b->sx, b->sy, dx, dy, e->x2, e->y2, R);
- if (h1 < hit) hit = h1;
- if (h2 < hit) hit = h2;
+ float d0 = (b->sx - e->x1) * nx + (b->sy - e->y1) * ny, d1 = (b->x - e->x1) * nx + (b->y - e->y1) * ny;
+ if (d0 < 0) d0 = -d0, d1 = -d1, nx = -nx, ny = -ny;
+ if (d0 < r || d1 > 0.5f * r) continue; /* touching already, or not through it nor deep in it */
+ float t = (d0 - r) / (d0 - d1);
+ float px = b->sx + dx * t, py = b->sy + dy * t;
+ float u = ((px - e->x1) * ex + (py - e->y1) * ey) / (el * el);
+ if (u < 0 || u > 1 || t >= best) continue;
+ best = t, bnx = nx, bny = ny;
}
- if (hit < best) best = hit;
}
}
- if (best < 1) b->x = b->sx + dx * best, b->y = b->sy + dy * best;
+ if (best < 1) {
+ b->x = b->sx + dx * best, b->y = b->sy + dy * best;
+ float vn = b->vx * bnx + b->vy * bny;
+ if (vn < 0) b->vx -= vn * bnx, b->vy -= vn * bny;
+ }
}
void ph_step(void) {
diff --git a/games/circloo/tests/check.py b/games/circloo/tests/check.py
new file mode 100644
index 00000000..f0c70d6e
--- /dev/null
+++ b/games/circloo/tests/check.py
@@ -0,0 +1,110 @@
+"""circloO's tests on a computer: python3 tests/check.py build/play (built with the sanitizers by make host).
+
+- the menus start level 1;
+- every level, normal and hard, played through in a row (circles taken by the test), into the credits: the
+ unlocks, the next level, hard mode and the save;
+- the save comes back, from its file or from its copy in circloo_saves.py, and a broken one is ignored;
+- the ball rolls up the walls: played by keys, level 1 gets at least five circles, never held still while fast;
+- random keys through menus and levels, with no crash.
+"""
+import base64
+import os
+import re
+import shutil
+import struct
+import subprocess
+import sys
+import tempfile
+
+PLAY = sys.argv[1] if len(sys.argv) > 1 else "build/play"
+fails = 0
+
+
+def run(*args, saves=None):
+ cmd = [PLAY] + [str(a) for a in args]
+ if saves:
+ cmd += ["--saves", saves]
+ r = subprocess.run(cmd, capture_output=True, text=True, timeout=600)
+ if r.returncode:
+ print(r.stdout[-2000:], r.stderr[-4000:])
+ raise SystemExit("FAIL %s exited %d" % (" ".join(cmd), r.returncode))
+ m = re.search(r"END state (\d+) level (\d+) hard (\d+) taken (\d+) complete (\d+) frames (\d+) R (\d+) stuck (\d+)", r.stdout)
+ return dict(zip(["state", "level", "hard", "taken", "complete", "frames", "R", "stuck"], map(int, m.groups())))
+
+
+def check(ok, what):
+ global fails
+ print(("ok " if ok else "FAIL ") + what)
+ if not ok:
+ fails += 1
+
+
+def read_save(d):
+ b = open(os.path.join(d, "circloo.sav"), "rb").read()
+ magic, version, size = struct.unpack_from("<IHH", b)
+ s = b[8:]
+ return dict(magic=magic, version=version, size=size, unlocked=list(s[0:2]), bonus=list(s[2:4]),
+ done=[list(s[4:20]), list(s[20:36])], best=[list(struct.unpack_from("<16H", s, 36)),
+ list(struct.unpack_from("<16H", s, 68))], raw=b)
+
+
+tmp = tempfile.mkdtemp()
+try:
+ d = os.path.join(tmp, "a")
+ os.makedirs(d)
+ r = run("--frames", 120, "--keys", "10:ok", saves=d)
+ check(r["state"] == 1 and r["level"] == 1, "OK on the main menu starts level 1")
+
+ # every level in a row: a circle taken each 25 frames, OK on each "Level complete!" (Next level, then Hard Mode)
+ keys = ",".join("%d:ok" % f for f in range(400, 60000, 100))
+ r = run("--frames", 60000, "--keys", "10:ok," + keys, "--take", 25, saves=d)
+ s = read_save(d)
+ check(r["state"] in (0, 4), "all 14 levels and the 6 hard ones played in a row, through the credits (state %d)" % r["state"])
+ check(s["magic"] == 0x534F4F43 and s["version"] == 1 and s["size"] == 104 and len(s["raw"]) == 112, "the save's header")
+ check(s["done"][0][1:15] == [1] * 14 and s["done"][1][1:7] == [1] * 6, "every level marked done")
+ # the game's unlocking: 2 at first, one a new level, one more at levels 5 and 10
+ check(s["unlocked"] == [2 + 14 + 2, 2 + 6 + 1] and s["bonus"] == [2, 1], "levels unlocked %s, bonus %s" % (s["unlocked"], s["bonus"]))
+ check(all(0 < t < 2000 for t in s["best"][0][1:15] + s["best"][1][1:7]), "best times kept")
+
+ # the copy in circloo_saves.py
+ py = open(os.path.join(d, "circloo_saves.py"), "rb").read()
+ m = re.search(rb"#>circloo\.sav:([A-Za-z0-9+/=]+)", py)
+ check(m and base64.b64decode(m.group(1)) == s["raw"], "circloo_saves.py holds the same save")
+ e = os.path.join(tmp, "b")
+ os.makedirs(e)
+ shutil.copy(os.path.join(d, "circloo_saves.py"), e)
+ run("--frames", 5, saves=e)
+ check(read_save(e)["done"] == s["done"], "the save comes back from circloo_saves.py alone")
+
+ # a broken save: the game starts fresh
+ f = os.path.join(tmp, "c")
+ os.makedirs(f)
+ open(os.path.join(f, "circloo.sav"), "wb").write(s["raw"][:8] + b"\xff" * 104)
+ r = run("--frames", 120, "--keys", "10:ok", saves=f)
+ check(r["state"] == 1 and r["level"] == 1 and read_save(f)["unlocked"] == [2, 2], "a broken save is ignored")
+
+ # by keys: the ball climbs the walls (level 1: five circles by a simple player), and never stops while fast
+ g = os.path.join(tmp, "g")
+ os.makedirs(g)
+ r = run("--level", 1, "--auto", "--frames", 6000, saves=g)
+ check(r["taken"] >= 5 and r["stuck"] == 0, "level 1 by keys: %d circles taken, %d frames held" % (r["taken"], r["stuck"]))
+ for lv in (4, 7, 13):
+ r = run("--level", lv, "--auto", "--frames", 3000, saves=g)
+ check(r["stuck"] == 0, "level %d by keys: never held still while fast" % lv)
+
+ # random keys
+ n = int(os.environ.get("FUZZ", "120"))
+ for seed in range(n):
+ h = os.path.join(tmp, "f%d" % seed)
+ os.makedirs(h)
+ args = ["--fuzz", seed, "--frames", 2500]
+ if seed % 3 == 1:
+ args += ["--level", 1 + seed % 14]
+ if seed % 3 == 2:
+ args += ["--level", 1 + seed % 6, "--hard", "--take", 97]
+ run(*args, saves=h)
+ check(True, "%d random sessions without a crash" % n)
+finally:
+ shutil.rmtree(tmp)
+print("circloO: %s" % ("all good" if not fails else "%d failed" % fails))
+sys.exit(1 if fails else 0)
diff --git a/games/circloo/tests/play.c b/games/circloo/tests/play.c
index bd7e024c..6503e12f 100644
--- a/games/circloo/tests/play.c
+++ b/games/circloo/tests/play.c
@@ -19,6 +19,8 @@
extern uint32_t host_keys, host_time;
void host_shot(const char *path);
void host_save_dir(const char *d);
+void level_debug_grow(void);
+void level_debug_take(void);
typedef struct { int a, b; uint32_t k; } Hold;
static Hold holds[4096];
@@ -55,24 +57,29 @@ static uint32_t rnd(void) {
static uint32_t autoplay(void) {
if (g.state != S_PLAY || L.ball < 0) return 0;
const Body *b = &W.b[L.ball];
- float best = 1e30f, tx = 0;
+ float best = 1e30f, tx = 0, ty = 0;
for (int k = 0; k < L.ncoll; k++) {
const Coll *c = &L.coll[k];
if (c->taken || c->io) continue;
float d = sqrtf((c->x - 1500) * (c->x - 1500) + (c->y - 1500) * (c->y - 1500));
if (d > L.R) continue;
float dx = c->x * PX_M - b->x, dy = c->y * PX_M - b->y, dd = dx * dx + dy * dy;
- if (dd < best) best = dd, tx = dx;
+ if (dd < best) best = dd, tx = dx, ty = dy;
}
if (best > 1e29f) return 0;
- float want = tx * 2.0f; /* a speed towards it, m/s */
- if (want > 6) want = 6;
- if (want < -6) want = -6;
+ /* higher up than the ball: rock to and fro, pushing the way it rolls, until it gets there */
+ if (ty < -1.0f) {
+ if (fabsf(b->vx) < 0.3f) return b->x > 30 ? K_LEFT : K_RIGHT; /* (at the top of a swing: downhill) */
+ return b->vx > 0 ? K_RIGHT : K_LEFT;
+ }
+ float want = tx * 4.0f; /* a speed towards it, m/s */
+ if (want > 20) want = 20;
+ if (want < -20) want = -20;
return b->vx < want - 0.3f ? K_RIGHT : b->vx > want + 0.3f ? K_LEFT : 0;
}
int main(int argc, char **argv) {
- int level = 0, frames = 600, fuzz = -1, draw = 0, trace = 0, hard = 0, autop = 0;
+ int takeevery = 0, rings = 0, level = 0, frames = 600, fuzz = -1, draw = 0, trace = 0, hard = 0, autop = 0;
const char *out = "build/shots";
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--level") && i + 1 < argc) level = atoi(argv[++i]);
@@ -90,6 +97,8 @@ int main(int argc, char **argv) {
} else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i];
else if (!strcmp(argv[i], "--saves") && i + 1 < argc) host_save_dir(argv[++i]);
else if (!strcmp(argv[i], "--auto")) autop = 1;
+ else if (!strcmp(argv[i], "--take") && i + 1 < argc) takeevery = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--rings") && i + 1 < argc) rings = atoi(argv[++i]);
else if (!strcmp(argv[i], "--fuzz") && i + 1 < argc) fuzz = atoi(argv[++i]), rng = (uint32_t)fuzz * 2654435761u + 1;
else if (!strcmp(argv[i], "--draw")) draw = 1;
else if (!strcmp(argv[i], "--trace") && i + 1 < argc) trace = atoi(argv[++i]);
@@ -102,9 +111,11 @@ int main(int argc, char **argv) {
level_load(level, hard);
g.state = S_PLAY;
save.hard = (uint8_t)hard;
+ for (int r = 0; r < rings; r++) level_debug_grow();
}
uint32_t fk = 0;
- int ended = 0;
+ int ended = 0, stuck = 0;
+ float lx = 0, ly = 0;
for (int f = 0; f < frames && g.running; f++) {
uint32_t k = 0;
for (int i = 0; i < nholds; i++)
@@ -121,6 +132,12 @@ int main(int argc, char **argv) {
break;
}
game_tick(k);
+ if (takeevery && g.state == S_PLAY && L.grow == 0 && f % takeevery == takeevery - 1) level_debug_take();
+ if (g.state == S_PLAY && L.ball >= 0) { /* (a ball fast but not moving: something holds it wrongly) */
+ const Body *b = &W.b[L.ball];
+ if (b->vx * b->vx + b->vy * b->vy > 9 && b->x == lx && b->y == ly) stuck++;
+ lx = b->x, ly = b->y;
+ }
int want = every ? f % every == 0 : 0;
for (int i = 0; i < nshots; i++) want |= shots[i] == f;
if (want || (draw && f % 2 == 0)) game_draw();
@@ -135,7 +152,7 @@ int main(int argc, char **argv) {
}
(void)ended;
save_write();
- printf("END state %d level %d hard %d taken %d complete %d frames %d R %.0f\n", g.state, L.level, L.hard, L.taken,
- L.complete, L.frames, L.R);
+ printf("END state %d level %d hard %d taken %d complete %d frames %d R %.0f stuck %d\n", g.state, L.level, L.hard,
+ L.taken, L.complete, L.frames, L.R, stuck);
return 0;
}
diff --git a/games/circloo/tools/levelshots.sh b/games/circloo/tools/levelshots.sh
new file mode 100755
index 00000000..e9150b29
--- /dev/null
+++ b/games/circloo/tools/levelshots.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# Each level with every ring open, one screenshot each, into a sheet: tools/levelshots.sh OUT.png LEVEL... (h5: hard 5)
+set -e
+cd "$(dirname "$0")/.."
+OUT=$1
+shift
+D=build/levelshots
+rm -rf "$D" && mkdir -p "$D"
+for l in "$@"; do
+ case $l in
+ h*) ./build/play --level "${l#h}" --hard --rings 6 --frames 2 --shots 1 --out "$D" --saves "$D" >/dev/null ;;
+ *) ./build/play --level "$l" --rings 6 --frames 2 --shots 1 --out "$D" --saves "$D" >/dev/null ;;
+ esac
+ mv "$D/shot_00001.ppm" "$D/$l.ppm"
+done
+/usr/bin/python3 tools/sheet.py "$OUT" $(for l in "$@"; do echo "$D/$l.ppm"; done)
diff --git a/games/circloo/tools/speed.sh b/games/circloo/tools/speed.sh
new file mode 100755
index 00000000..68f6fe7e
--- /dev/null
+++ b/games/circloo/tools/speed.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+# Speed of a level in the emulator (about an N0110's speed): tools/speed.sh LEVEL [ms] [keys]
+# LEVEL: 1-14, or 101-106 for hard mode. Builds a copy that starts in that level (CO_START).
+set -e
+cd "$(dirname "$0")/.."
+ROOT=$(cd ../.. && pwd)
+L=$1
+MS=${2:-8000}
+KEYS=${3:-"500-1500:right,2000-3000:left,3500-4500:right"}
+OUT=${TMPDIR:-/tmp}/circloo_speed_$L
+rm -rf "$OUT"
+make -s build NWLINK="node $ROOT/games/numdash/node_modules/nwlink/bin/nwlink" EXTRA=-DCO_START=$L BUILD_DIR="$OUT" >/dev/null
+PATH="$ROOT/games/numdash/node_modules/.bin:$PATH" python3 "$ROOT/tools/emu.py" "$OUT/circloo.nwa" --ms "$MS" \
+ --keys "$KEYS" --shots "$((MS - 100))" --out "$OUT" --ram-length 133652 | tail -1