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.

NumDrive: host checks drive Mobile Home to its finish

Mason3635118127e4923
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index a25ede29..88271d90 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -60,6 +60,7 @@ jobs:
python3 tests/test_upsilon.py build/compiled/NumPlay-Upsilon.nwa
python3 tests/test_small_ram.py build/compiled/NumPlay.nwa
make -C games/numblocks host test
+ make -C games/numdrive host test
make -C games/championisland host test
make -C games/undertale host test
make -C games/doom host test
diff --git a/games/numdrive/.gitignore b/games/numdrive/.gitignore
index 5721931b..9a622d5f 100644
--- a/games/numdrive/.gitignore
+++ b/games/numdrive/.gitignore
@@ -4,3 +4,4 @@ host/nd_*
host/out*/
host/test_inflate
src/pack.c
+host/*.dSYM/
diff --git a/games/numdrive/Makefile b/games/numdrive/Makefile
index 961eda7a..14daf38c 100644
--- a/games/numdrive/Makefile
+++ b/games/numdrive/Makefile
@@ -1,6 +1,7 @@
# NumDrive: a Drive Mad inspired game for the NumWorks calculator
# make -> output/device/numdrive.nwa
# make PLATFORM=simulator -> output/simulator/numdrive.nwb (for the Epsilon simulator)
+# make host test -> host/nd, a build for this computer, and its checks (host/check.py)
Q ?= @
PLATFORM ?= device
NWLINK ?= npx --yes -- nwlink
@@ -55,6 +56,14 @@ $(BUILD_DIR):
clean:
$(Q) rm -rf output
+ $(Q) $(MAKE) -C host clean
+
+# Host checks (make host test): the game built for a computer drives First Gear, and Mobile Home to its finish
+.PHONY: host test
+host:
+ $(Q) $(MAKE) -C host nd
+test: host
+ python3 host/check.py host/nd
# NumPlay launcher: the game as a module (see ../../tools/npmodule.py) for the
# calculator, and as a relocatable object for the simulator build.
diff --git a/games/numdrive/host/check.py b/games/numdrive/host/check.py
new file mode 100644
index 00000000..0e9ff44f
--- /dev/null
+++ b/games/numdrive/host/check.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+"""Checks of NumDrive on a computer (make host test): the host build drives First Gear with the gas held, and Mobile
+Home (level 25) to its finish with the host's driver (ND_BOT), which gives gas up to a speed and eases off or brakes
+when the house leans back too far, as a player would. Mobile Home must be won with at least one of three such
+drivers (a lean of 50 or 55 degrees, braking or not): the house's wobbles are chaotic, and another compiler's
+rounding can tip one run either way.
+
+ python3 host/check.py host/nd"""
+import os
+import re
+import subprocess
+import sys
+import tempfile
+
+ND = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else "host/nd")
+failures = []
+
+
+def run(level, frames, **env):
+ e = dict(os.environ, ND_LEVEL=str(level), ND_FRAMES=str(frames), ND_NORENDER="1", ND_OUTCOME="1", **env)
+ with tempfile.TemporaryDirectory() as d:
+ out = subprocess.run([ND], capture_output=True, text=True, env=e, cwd=d, timeout=600).stdout
+ m = re.search(r"(WIN|LOSE) at frame (\d+)", out)
+ return (m.group(1), int(m.group(2))) if m else (None, frames)
+
+
+def check(name, ok, detail):
+ print(f"{'ok ' if ok else 'FAIL'} {name}: {detail}")
+ if not ok:
+ failures.append(name)
+
+
+r = run(0, 1500, ND_KEYS="RIGHT:0:1500")
+check("First Gear, gas held", r[0] == "WIN", f"{r[0] or 'no end'} at frame {r[1]}")
+
+# lean (degrees), dip, top speed (units/s), brake, look ahead (s)
+drivers = ["55,25,8,1,0.2", "55,25,8,0,0.2", "50,25,8,1,0.2"]
+results = [(d, run(24, 3600, ND_BOT=d)) for d in drivers]
+for d, (o, f) in results:
+ print(f" Mobile Home, driver {d}: {o or 'no end'} at frame {f}")
+check("Mobile Home finished", any(o == "WIN" for _, (o, _f) in results),
+ f"{sum(o == 'WIN' for _, (o, _f) in results)} of {len(drivers)} drivers won")
+
+if failures:
+ print(f"{len(failures)} failed")
+ sys.exit(1)
+print("all passed")
diff --git a/games/numdrive/host/platform_host.c b/games/numdrive/host/platform_host.c
index 17b46c24..d0c89474 100644
--- a/games/numdrive/host/platform_host.c
+++ b/games/numdrive/host/platform_host.c
@@ -1,17 +1,22 @@
/* Headless host platform for automated testing.
* Env vars: ND_FRAMES (stop after N frames), ND_SHOTS ("1,20,50" or "all"), ND_OUT (dir),
- * ND_KEYS ("RIGHT:0:120,LEFT:200:260,OK:5:6") key holds by frame range. */
+ * ND_KEYS ("RIGHT:0:120,LEFT:200:260,OK:5:6") key holds by frame range, ND_BOT (a driver, below),
+ * ND_OUTCOME (print "WIN at frame N" or "LOSE at frame N"). */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <math.h>
#include "../src/platform.h"
#include "../src/render.h"
+#include "../src/world.h"
+#include "../src/physics.h"
#ifndef ARMTEST
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
+#ifdef __linux__
#include <sys/prctl.h>
-#include <math.h>
+#endif
#include <semaphore.h>
#endif
@@ -40,6 +45,9 @@ static int s_seg, s_beam, s_frames, s_id = -1, s_act, s_outcome;
static void s_checkpoint(void);
#endif
void host_outcome(int won) {
+ static int told;
+ if (!told && getenv("ND_OUTCOME")) printf("%s at frame %d\n", won == 1 ? "WIN" : "LOSE", frame), fflush(stdout);
+ told = 1;
#ifndef ARMTEST
if (!s_outcome) s_outcome = won;
#else
@@ -98,8 +106,41 @@ static int keycode(const char *n) {
return -1;
}
+/* ND_BOT="lean,dip,speed,brake,ahead": a driver that keeps a tall car on its wheels the way a player would.
+ It gives gas, up to `speed` units a second, unless the body (object ND_BOTOBJ, 0 by default) leans back
+ more than `lean` degrees from the ground under it, when it lets go, or brakes if `brake` is not 0; leaning
+ forward more than `dip` degrees, it gives gas. The lean is taken `ahead` seconds ahead at its turning speed. */
+static uint64_t bot_keys(void) {
+ static float lean, dip, speed, brake, ahead, pa, px;
+ static int body = -1;
+ if (body < 0) {
+ lean = 45, dip = 25, speed = 6, brake = 1, ahead = 0;
+ sscanf(getenv("ND_BOT"), "%f,%f,%f,%f,%f", &lean, &dip, &speed, &brake, &ahead);
+ body = getenv("ND_BOTOBJ") ? atoi(getenv("ND_BOTOBJ")) : 0;
+ }
+ if (body >= nobj) return 0;
+ const Obj *o = &objs[body];
+ float a = 2 * atan2f(o->rot.z, o->rot.w) * RAD2DEG;
+ while (a > 180) a -= 360;
+ while (a < -180) a += 360;
+ float w = frame ? (a - pa) * 60 : 0, v = frame ? (o->pos.x - px) * 60 : 0;
+ pa = a, px = o->pos.x;
+ /* the ground's slope: rays straight down either side of the body */
+ float slope = 0;
+ vec3 h0, h1;
+ int ho;
+ if (phys_raycast_ex(v3(o->pos.x - 1.6f, o->pos.y, o->pos.z), v3(o->pos.x - 1.6f, o->pos.y - 8, o->pos.z), &h0, &ho, body) &&
+ phys_raycast_ex(v3(o->pos.x + 1.6f, o->pos.y, o->pos.z), v3(o->pos.x + 1.6f, o->pos.y - 8, o->pos.z), &h1, &ho, body))
+ slope = atan2f(h1.y - h0.y, h1.x - h0.x) * RAD2DEG;
+ float l = a - slope + w * ahead;
+ if (l > lean) return brake != 0 ? KEY(K_LEFT) : 0;
+ if (l < -dip) return KEY(K_RIGHT);
+ return v < speed ? KEY(K_RIGHT) : 0;
+}
+
uint64_t plat_keys(void) {
init();
+ if (getenv("ND_BOT")) return bot_keys();
#ifndef ARMTEST
if (sr) {
if (s_id < 0) s_checkpoint(); /* frame 0: become the coordinator and the first worker */
@@ -249,7 +290,9 @@ static void s_checkpoint(void) {
if (s_id < 0) {
/* orphaned workers are adopted and reaped here */
signal(SIGCHLD, SIG_IGN);
+#ifdef __linux__
prctl(PR_SET_CHILD_SUBREAPER, 1);
+#endif
s_id = 0;
if (fork() != 0) s_coordinate();
s_act = 0;
diff --git a/games/numdrive/src/pack_data.S b/games/numdrive/src/pack_data.S
index 0e209587..b8966fa3 100644
--- a/games/numdrive/src/pack_data.S
+++ b/games/numdrive/src/pack_data.S
@@ -1,4 +1,17 @@
/* the game data pack produced by tools/pack.py */
+#ifdef __APPLE__
+ /* (the host harness built on macOS: Mach-O sections, C names with an underscore) */
+ .section __TEXT,__const
+ .p2align 2
+ .globl _pack_data
+_pack_data:
+ .incbin "pack.bin"
+pack_end:
+ .p2align 2
+ .globl _pack_size
+_pack_size:
+ .long pack_end - _pack_data
+#else
.section .rodata.pack_data, "a"
.balign 4
.global pack_data
@@ -9,6 +22,7 @@ pack_end:
.global pack_size
pack_size:
.word pack_end - pack_data
+#endif
#if defined(__linux__) && defined(__ELF__)
.section .note.GNU-stack, "", %progbits
#endif