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
Among Us: tests for the rules, every minigame, whole games, saves and random keys
Mason363a393cd451fcb
diff --git a/games/amongus/Makefile b/games/amongus/Makefileindex a8d5e14d..de9c12bc 100644--- a/games/amongus/Makefile+++ b/games/amongus/Makefile@@ -68,9 +68,9 @@ HOST_FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Isrc -DHOSTHOST_SAN = -fsanitize=address,undefined -fno-sanitize-recover=undefinedHOST_SRC = $(filter-out src/plat_eadk.c src/main.c,$(wildcard src/*.c))host: build/play-build/play: $(HOST_SRC) tests/play.c src/data.S src/data.bin $(HEADERS)+build/play: $(HOST_SRC) tests/play.c tests/rules.c src/data.S src/data.bin $(HEADERS)$(Q) mkdir -p $(dir $@)- $(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(HOST_SRC) tests/play.c src/data.S -lm -o $@+ $(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(HOST_SRC) tests/play.c tests/rules.c src/data.S -lm -o $@@echo "HOST $@"test: build/playdiff --git a/games/amongus/src/game.c b/games/amongus/src/game.cindex b6f9256b..b36cba06 100644--- a/games/amongus/src/game.c+++ b/games/amongus/src/game.c@@ -10,6 +10,7 @@Game g;bool autopilot; /* (tests: the bots' brain plays the player too) */+bool bots_frozen; /* (tests: the bots stand still) */uint16_t color_pal[N_COLORS][256];const uint32_t BODY_COLORS[N_COLORS] = {0xC61111, 0x132ED1, 0x117F2D, 0xED54BA, 0xEF7D0D, 0xF5F557,diff --git a/games/amongus/src/game.h b/games/amongus/src/game.hindex 9c7b1c8c..3182c7b3 100644--- a/games/amongus/src/game.h+++ b/games/amongus/src/game.h@@ -120,7 +120,7 @@ typedef struct {} Game;extern Game g;-extern bool autopilot;+extern bool autopilot, bots_frozen;/* is player i played by the bots' brain (the others, and in tests the player too) */static inline bool is_bot(int i) { return i != g.me || autopilot; }diff --git a/games/amongus/src/hud.c b/games/amongus/src/hud.cindex 7ede7c34..14017ee4 100644--- a/games/amongus/src/hud.c+++ b/games/amongus/src/hud.c@@ -123,8 +123,27 @@ static void draw_keys(const char *s) {void hud_keys(const char *s) { draw_keys(s); }+/* in a vent: an arrow toward each vent it leads to (Vent.Buttons) */+static void vent_arrows(const Player *M) {+ const VentRec *V = &VENTS[M->vent];+ float px = M->pos.x * PX_PER_UNIT + SHIP_OX - view.cx, py = -M->pos.y * PX_PER_UNIT + SHIP_OY - view.cy;+ for (int k = 0; k < 3; k++) {+ if (V->link[k] < 0) continue;+ float dx = VENTS[V->link[k]].x - V->x, dy = -(VENTS[V->link[k]].y - V->y), l = sqrtf(dx * dx + dy * dy);+ dx /= l, dy /= l;+ float cx = px + dx * 38, cy = py + dy * 38;+ for (int o = 1; o >= 0; o--) {+ float tip = 9 + 2.5f * o, back = 5 + 2.5f * o, side = 7 + 2.5f * o;+ float t[6] = {cx + dx * tip, cy + dy * tip, cx - dx * back - dy * side, cy - dy * back + dx * side,+ cx - dx * back + dy * side, cy - dy * back - dx * side};+ gfx_poly(t, 3, o ? 0x0000 : 0xFFFF, 256);+ }+ }+}+void hud_draw(void) {const Player *M = &g.p[g.me];+ if (M->vent != NONE && !M->vent_anim) vent_arrows(M);draw_tasks();/* the room's name (RoomTracker), for a while after entering */if (g.room_t > 0 && g.room_shown != R_HALLWAY) {diff --git a/games/amongus/src/mg2.c b/games/amongus/src/mg2.cindex 6f90d3cb..72fd4b18 100644--- a/games/amongus/src/mg2.c+++ b/games/amongus/src/mg2.c@@ -64,9 +64,10 @@ static void leaves_tick(uint32_t keys, uint32_t pressed) {/* sucked out */a->x -= 300 * DT, a->a += DT * 8;if (a->x < 40) {+ /* the task is done with the last leaf out */a->on = 0;- task_step_done(g.me, M->task);- if (task_done(&g.p[g.me].tasks[M->task])) M->done = 1, M->done_t = 0;+ if (M->n) M->n--;+ if (!M->n) mg_finish();}continue;}@@ -254,7 +255,7 @@ static void divert_draw(void) {}static void accept_tick(uint32_t pressed) {- if (pressed & K_OK) M->phase = 1, M->b = 0;+ if ((pressed & K_OK) && !M->phase) M->phase = 1, M->b = 0;if (M->phase) {M->b += DT;if (M->b > 0.3f) mg_finish();diff --git a/games/amongus/src/play.c b/games/amongus/src/play.cindex 352d166d..37c06aaa 100644--- a/games/amongus/src/play.c+++ b/games/amongus/src/play.c@@ -284,10 +284,10 @@ void play_tick(uint32_t keys, uint32_t pressed) {else map_tick(pressed, g.overlay);}if (g.screen != screen || (screen != S_PLAY && screen != S_KILLED)) return;- for (int i = 0; i < g.nplayers && g.screen == screen; i++)+ for (int i = 0; i < g.nplayers && g.screen == screen && !bots_frozen; i++)if (i != g.me) bot_tick(i);if (g.screen != screen) return;- bots_dispatch();+ if (!bots_frozen) bots_dispatch();world_tick();check_win();}diff --git a/games/amongus/tests/check.py b/games/amongus/tests/check.pynew file mode 100644index 00000000..e653fbb4--- /dev/null+++ b/games/amongus/tests/check.py@@ -0,0 +1,146 @@+#!/usr/bin/env python3+"""Among Us played on a computer (build/play, with the sanitizers): the rules, every minigame solved with the keys,+whole games to a crew win and an impostor win, saves that come back and survive a broken file, random keys.++ python3 tests/check.py build/play [--fuzz N]"""+import os+import random+import re+import shutil+import subprocess+import sys+import tempfile++PLAY = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else "build/play")+FUZZ = int(sys.argv[sys.argv.index("--fuzz") + 1]) if "--fuzz" in sys.argv else 300+WORK = tempfile.mkdtemp(prefix="amongus-check-")+failed = []+++def play(*args, saves=None, fresh=True):+ saves = saves or os.path.join(WORK, "saves")+ if fresh:+ shutil.rmtree(saves, ignore_errors=True)+ os.makedirs(saves, exist_ok=True)+ out = os.path.join(WORK, "shots")+ os.makedirs(out, exist_ok=True)+ r = subprocess.run([PLAY, "--saves", saves, "--out", out] + [str(a) for a in args], capture_output=True, text=True)+ if r.returncode not in (0, 1) or "ERROR" in r.stderr or "runtime error" in r.stderr:+ failed.append("crashed: %s\n%s" % (" ".join(map(str, args)), r.stderr[-2000:]))+ return r.stdout + r.stderr+++def check(ok, what):+ print(("ok " if ok else "FAIL ") + what)+ if not ok:+ failed.append(what)+++def end(out):+ m = re.search(r"END screen (\d+) winner (\d+) reason (\d+) t ([\d.]+)", out)+ return tuple(int(m.group(k)) for k in (1, 2, 3)) if m else None+++# the rules+out = play("--rules")+m = re.search(r"RULES (\d+) checks, (\d+) failed", out)+check(m and m.group(2) == "0", "rules: %s" % (m.group(0) if m else out[-500:]))+for line in out.splitlines():+ if line.startswith("FAIL"):+ print(" " + line)++# every minigame, solved with the keys (24 is the cameras: nothing to solve)+NAMES = ["swipe card", "wires", "asteroids", "align engine", "scan", "sample", "fuel", "start reactor", "garbage",+ "upload", "calibrate", "chart course", "O2 filter", "manifolds", "steering", "shields", "divert power",+ "accept power", "reactor hands", "lights", "comms", "O2 keypad", "cameras", "emergency button"]+for k, name in enumerate(NAMES):+ if name == "cameras":+ continue+ out = play("--mg", k, "--solve", "--frames", 3000, "--seed", 5 + k)+ m = re.search(r"SOLVED mg \d+ at frame (\d+) step (\d+) sab (\d+)", out)+ if name in ("reactor hands", "lights", "comms"):+ ok = m and m.group(3) == "0"+ elif name == "O2 keypad":+ ok = m is not None+ elif name == "emergency button":+ ok = m is not None and "screen 4" in out+ else:+ ok = m and int(m.group(2)) >= 1+ check(bool(ok), "minigame %s%s" % (name, " (frame %s)" % m.group(1) if m else ""))++# whole games: through the menus to a crew win, an impostor's game to an impostor win+out = play("--auto", "--keys", "10:ok,20:ok", "--frames", 90000, "--seed", 3, "--walled")+check(end(out) and end(out)[:2] == (7, 0) and "WALLED" not in out, "a crew game from the title to a crew win: %s" % (end(out),))+out = play("--start", "imp", "--auto", "--bots", 9, "--imps", 2, "--frames", 90000, "--seed", 2, "--walled")+check(end(out) and end(out)[:2] == (7, 1) and "WALLED" not in out, "an impostor's game to an impostor win: %s" % (end(out),))+wins = {0: 0, 1: 0}+for s in range(1, 9):+ out = play("--start", "crew" if s & 1 else "imp", "--auto", "--frames", 90000, "--seed", 100 + s, "--walled",+ *(["--bots", 9, "--imps", 2] if s % 4 == 0 else []))+ e = end(out)+ check(e is not None and e[0] == 7 and "WALLED" not in out, "game %d ends: %s" % (s, e))+ if e:+ wins[e[1]] += 1+print(" wins: crew %d, impostors %d" % (wins[0], wins[1]))++# saves: Home in the middle of a round, then back where it was, paused+saves = os.path.join(WORK, "s1")+out = play("--start", "crew", "--frames", 400, "--keys", "100-200:left,250-300:down,399:home", "--seed", 7, saves=saves)+check(os.path.exists(os.path.join(saves, "amongus.sav")) and os.path.exists(os.path.join(saves, "amongus_saves.py")),+ "Home saves the game and its copy")+out = play("--frames", 5, saves=saves, fresh=False)+m = re.search(r"LOADED screen (\d+) back (\d+) t ([\d.]+)", out)+check(m and m.group(1) == "9" and m.group(2) == "3" and float(m.group(3)) > 10, "the game comes back, paused: %s" %+ (m.group(0) if m else None))+# its copy alone (installing apps from the website keeps only scripts)+for f in ("amongus.sav", "amongus.set"):+ os.remove(os.path.join(saves, f))+out = play("--frames", 5, saves=saves, fresh=False)+m2 = re.search(r"LOADED screen (\d+) back (\d+) t ([\d.]+)", out)+check(m2 and m2.group(1) == "9" and m2.group(3) == m.group(3), "and from the Python copy alone")+# a broken file: a clean start, no crash+data = open(os.path.join(saves, "amongus_saves.py"), "rb").read()+rnd = random.Random(5)+for k in range(40):+ case = os.path.join(WORK, "broken%d" % k)+ shutil.rmtree(case, ignore_errors=True)+ os.makedirs(case)+ if k < 20:+ b = bytearray(data)+ for _ in range(1 + k):+ b[rnd.randrange(len(b))] = rnd.randrange(256)+ open(os.path.join(case, "amongus_saves.py"), "wb").write(bytes(b[:rnd.randrange(len(b) + 1)] if k & 1 else b))+ else:+ open(os.path.join(case, "amongus.sav"), "wb").write(bytes(rnd.randrange(256) for _ in range(rnd.randrange(4000))))+ open(os.path.join(case, "amongus.set"), "wb").write(bytes(rnd.randrange(256) for _ in range(rnd.randrange(64))))+ out = play("--frames", 60, "--keys", "10:ok,20:ok,40:ok", saves=case, fresh=False)+ if "LOADED" not in out:+ check(False, "broken save %d" % k)+ break+else:+ check(True, "40 broken saves: no crash")+# the settings kept+saves = os.path.join(WORK, "s2")+play("--frames", 60, "--keys", "10:ok,20:down,25:right,30:back", saves=saves)+out = play("--frames", 5, saves=saves, fresh=False)+check("bots 8" in out, "a setting kept: %s" % re.search(r"bots \d+", out).group(0))++# random keys, the app started again when it quits+crashes = 0+for k in range(FUZZ):+ args = ["--fuzz", k, "--frames", 2500, "--seed", k]+ if k % 3 == 1:+ args += ["--start", "crew"]+ elif k % 3 == 2:+ args += ["--start", "imp"]+ out = play(*args)+ if "END screen" not in out:+ crashes += 1+ failed.append("fuzz %d" % k)+check(crashes == 0, "%d random-key runs" % FUZZ)++shutil.rmtree(WORK, ignore_errors=True)+print("\n%d failed" % len(failed) if failed else "\nall passed")+for f in failed:+ print(" - " + f[:3000])+sys.exit(1 if failed else 0)diff --git a/games/amongus/tests/play.c b/games/amongus/tests/play.cindex 8104146d..48ffeb90 100644--- a/games/amongus/tests/play.c+++ b/games/amongus/tests/play.c@@ -14,6 +14,7 @@#include <sys/stat.h>#include "../src/game.h"#include "../src/gfx.h"+#include "../src/mg.h"#include "../src/plat.h"#include "../src/world.h"@@ -137,6 +138,113 @@ static void scene(const char *name) {(void)P;}+/* ---- a player for the minigames: the keys that solve the open one, as a person would press them ---- */++static uint32_t tap(int f, uint32_t k) { return f & 1 ? 0 : k; } /* a press every other frame */+static uint32_t toward(float dx, float dy, float slack) {+ uint32_t k = 0;+ if (dx > slack) k |= K_RIGHT;+ if (dx < -slack) k |= K_LEFT;+ if (dy > slack) k |= K_DOWN;+ if (dy < -slack) k |= K_UP;+ return k;+}+static uint32_t digit(int d) { return (uint32_t)K_D0 << d; }++static uint32_t solve(int f) {+ if (g.overlay != O_TASK || M->done) return 0;+ switch (M->kind) {+ case MG_CARD: return M->phase == 0 ? tap(f, K_OK) : M->phase == 3 ? 0 : K_RIGHT;+ case MG_WIRES:+ if (M->phase == 0) {+ int want = 0;+ while (want < 4 && M->state[want] != NONE) want++;+ return want == M->n ? tap(f, K_OK) : tap(f, K_DOWN);+ } else {+ int want = 0;+ while (want < 4 && M->order[4 + want] != M->order[M->sel]) want++;+ return want == M->n ? tap(f, K_OK) : tap(f, K_DOWN);+ }+ case MG_ASTEROIDS: {+ int best = -1;+ float bd = 1e9f;+ for (int k = 0; k < 10; k++) {+ const Thing *a = &M->th[k];+ if (a->on != 1 || a->x > 255 || a->x < 70) continue;+ float d = (a->x - M->a) * (a->x - M->a) + (a->y - M->b) * (a->y - M->b);+ if (d < bd) bd = d, best = k;+ }+ if (best < 0) return 0;+ if (bd < 12 * 12) return tap(f, K_OK);+ return toward(M->th[best].x - M->a, M->th[best].y - M->b, 3);+ }+ case MG_ALIGN: return M->a > 0.04f ? K_DOWN : M->a < -0.04f ? K_UP : 0;+ case MG_SAMPLE:+ if (M->phase == 0) return tap(f, K_OK);+ if (M->phase == 3) return M->sel == M->n ? tap(f, K_OK) : tap(f, K_RIGHT);+ return 0;+ case MG_FUEL: return K_OK;+ case MG_SIMON:+ if (M->phase == 1) {+ static const int CELL_DIGIT[9] = {7, 8, 9, 4, 5, 6, 1, 2, 3};+ return tap(f, digit(CELL_DIGIT[M->order[M->n]]));+ }+ return 0;+ case MG_GARBAGE: return K_DOWN;+ case MG_UPLOAD: return M->phase == 0 ? tap(f, K_OK) : 0;+ case MG_CALIBRATE: {+ float a = M->v[M->n < 3 ? M->n : 0];+ if (a > 3.14159f) a -= 6.2831853f;+ return fabsf(a) < 0.15f ? tap(f, K_OK) : 0;+ }+ case MG_COURSE: return K_RIGHT;+ case MG_LEAVES: {+ if (M->sel != NONE) return K_OK | K_LEFT;+ int best = -1;+ float bd = 1e9f;+ for (int k = 0; k < 6; k++) {+ const Thing *a = &M->th[k];+ if (a->on != 1) continue;+ float d = (a->x - M->a) * (a->x - M->a) + (a->y - M->b) * (a->y - M->b);+ if (d < bd) bd = d, best = k;+ }+ if (best < 0) return 0;+ if (bd < 8 * 8) return tap(f, K_OK);+ return toward(M->th[best].x - M->a, M->th[best].y - M->b, 2);+ }+ case MG_MANIFOLDS: {+ if (M->c > 0) return 0;+ int want = 0;+ while (want < 10 && M->order[want] != M->n) want++;+ if (want == M->sel) return tap(f, K_OK);+ if (want / 5 != M->sel / 5) return tap(f, K_DOWN);+ return tap(f, K_RIGHT);+ }+ case MG_STEERING: return toward(-M->a, -M->b, 1.5f);+ case MG_SHIELDS:+ for (int k = 0; k < 7; k++)+ if (!(M->n >> k & 1)) return M->sel == k ? tap(f, K_OK) : tap(f, digit(k + 1));+ return 0;+ case MG_DIVERT: return K_UP;+ case MG_ACCEPT: return tap(f, K_OK);+ case MG_HANDS: return K_OK;+ case MG_LIGHTS:+ for (int k = 0; k < 5; k++)+ if (!(g.switches >> k & 1)) return tap(f, digit(k + 1));+ return 0;+ case MG_COMMS: return M->a < M->c - 0.03f ? K_RIGHT : M->a > M->c + 0.03f ? K_LEFT : 0;+ case MG_KEYPAD: {+ if (M->c > 0 || M->phase) return 0;+ char code[8];+ snprintf(code, sizeof code, "%05u", (unsigned)g.o2_code);+ if (M->n < 5) return (f & 1) ? 0 : digit(code[M->n] - '0');+ return tap(f, K_OK);+ }+ case MG_EMERGENCY: return M->b > 0.4f ? tap(f, K_OK) : 0;+ default: return 0;+ }+}+static uint32_t frng = 1;static uint32_t frnd(void) {frng ^= frng << 13, frng ^= frng >> 17, frng ^= frng << 5;@@ -148,7 +256,7 @@ int main(int argc, char **argv) {const char *scene_name = NULL;float at_x = 1e9f, at_y = 0;uint32_t seed = 0;- bool fuzz = false, draw = false, shot_end = false, walled = false;+ bool fuzz = false, draw = false, shot_end = false, walled = false, solving = false, do_rules = false;const char *out = "build/shots";for (int i = 1; i < argc; i++) {if (!strcmp(argv[i], "--frames")) frames = atoi(argv[++i]);@@ -169,6 +277,8 @@ int main(int argc, char **argv) {else if (!strcmp(argv[i], "--at")) at_x = (float)atof(argv[++i]), at_y = (float)atof(argv[++i]);else if (!strcmp(argv[i], "--walk")) walk_frames = atoi(argv[++i]);else if (!strcmp(argv[i], "--shot-end")) shot_end = true;+ else if (!strcmp(argv[i], "--solve")) solving = true;+ else if (!strcmp(argv[i], "--rules")) do_rules = true;else if (!strcmp(argv[i], "--fuzz")) fuzz = true, frng = (uint32_t)atoi(argv[++i]) * 2654435761u + 1;else if (!strcmp(argv[i], "--shots")) {const char *s = argv[++i];@@ -187,6 +297,12 @@ int main(int argc, char **argv) {gfx_init(band);host_time = 1000 + seed * 7919;game_init();+ if (do_rules) {+ int rules(void);+ return rules() ? 1 : 0;+ }+ printf("LOADED screen %d back %d t %.3f me (%.3f,%.3f) players %d color %d bots %d\n", g.screen, g.back_screen, g.t,+ g.p[g.me].pos.x, g.p[g.me].pos.y, g.nplayers, g.opt.color, g.opt.bots);if (start) {if (bots >= 0) g.opt.bots = (uint8_t)bots;if (imps >= 0) g.opt.impostors = (uint8_t)imps;@@ -204,7 +320,15 @@ int main(int argc, char **argv) {uint32_t fk = 0;int meetings = 0, ejects = 0, wrong = 0, prev_scr = -1;static int hist[16];- for (int f = 0; f < frames && game_running; f++) {+ int restarts = 0;+ for (int f = 0; f < frames; f++) {+ if (!game_running) {+ /* quit: the fuzz starts the app again (the saves read back), the others stop */+ if (!fuzz) break;+ game_running = true;+ restarts++;+ game_init();+ }uint32_t k = 0;for (int i = 0; i < nholds; i++)if (f >= holds[i].a && f <= holds[i].b) k |= holds[i].k;@@ -214,10 +338,25 @@ int main(int argc, char **argv) {fk = r & (K_LEFT | K_RIGHT | K_UP | K_DOWN | K_OK | K_EXE | K_TOOL);if (frnd() % 12 == 0) fk |= K_BACK;if (frnd() % 10 == 0) fk |= (uint32_t)K_D0 << (frnd() % 10);+ if (frnd() % 300 == 0) fk = K_HOME;}k = fk;}if (autopilot && (g.screen == S_INTRO || g.screen == S_EJECT) && f % 20 == 0) k |= K_OK;+ if (solving) {+ /* (nobody kills, sabotages or calls a meeting meanwhile) */+ for (int i = 0; i < g.nplayers; i++)+ if (i != g.me) g.p[i].kill_cd = 99, g.p[i].emergencies = 0, g.p[i].bot.found_body = NONE;+ if (g.sab == SAB_NONE) g.sab_cd = 99;+ /* (the reactor's other hand: someone else holds it) */+ if (g.sab == SAB_REACTOR && g.overlay == O_TASK && M->kind == MG_HANDS)+ g.reactor_hold[(CONSOLES[M->cons].id & 1) ^ 1] = (uint8_t)(g.me == 1 ? 2 : 1);+ k |= solve(f);+ if (mgkind >= 0 && g.overlay == O_NONE) {+ printf("SOLVED mg %d at frame %d step %d sab %d\n", mgkind, f, g.p[g.me].tasks[0].step, g.sab);+ break;+ }+ }host_keys = k;hist[g.screen & 15]++;if (g.screen == S_REPORT && prev_scr != S_REPORT) meetings++;@@ -235,7 +374,7 @@ int main(int argc, char **argv) {host_time += 33;bool shot = every ? f % every == 0 : false;for (int i = 0; i < nshots; i++) shot |= shots[i] == f;- if (shot || draw) game_draw();+ if (shot || draw || (fuzz && f % 4 == 0)) game_draw();if (shot) {char p[512];snprintf(p, sizeof p, "%s/shot_%04d.ppm", out, f);@@ -255,6 +394,7 @@ int main(int argc, char **argv) {}}if (fuzz)+ printf("RESTARTS %d\n", restarts),printf("SCREENS title %d lobby %d intro %d play %d report %d meeting %d eject %d end %d howto %d pause %d killed %d\n",hist[0], hist[1], hist[2], hist[3], hist[4], hist[5], hist[6], hist[7], hist[8], hist[9], hist[10]);int alive = 0;diff --git a/games/amongus/tests/rules.c b/games/amongus/tests/rules.cnew file mode 100644index 00000000..c184e812--- /dev/null+++ b/games/amongus/tests/rules.c@@ -0,0 +1,258 @@+/* Among Us: the rules against the game's numbers (gamesrc/amongus/spec/rules.md), called by play --rules. */+#include <math.h>+#include <stdio.h>+#include <string.h>+#include "../src/game.h"+#include "../src/world.h"++int console_use(int i, int c);+int hud_action(int *what);+int kill_target(int i);+void game_forget(void);++static int fails, checks;+#define CHECK(c, ...) \+ do { \+ checks++; \+ if (!(c)) { \+ fails++; \+ printf("FAIL line %d: ", __LINE__); \+ printf(__VA_ARGS__); \+ printf("\n"); \+ } \+ } while (0)+static bool near(float a, float b) { return fabsf(a - b) < 1e-3f; }++/* a round with these many bots and impostors, everybody frozen where they spawn */+static void round_of(int bots, int imps, int role) {+ g.opt.bots = (uint8_t)bots, g.opt.impostors = (uint8_t)imps, g.opt.role = (uint8_t)role;+ game_new();+ g.screen = S_PLAY;+}+static void run(float seconds) {+ for (int k = 0; k < (int)(seconds * 30 + 0.5f) && g.screen == S_PLAY; k++) game_tick(0);+}+static int count_imps(void) {+ int n = 0;+ for (int i = 0; i < g.nplayers; i++) n += g.p[i].imp;+ return n;+}+static int first(bool imp, int except) {+ for (int i = 0; i < g.nplayers; i++)+ if (g.p[i].alive && (g.p[i].imp != 0) == imp && i != except) return i;+ return -1;+}+static int console_of(int kind_or_type, bool is_type) {+ for (int c = 0; c < N_CONSOLES; c++)+ if (is_type ? (!CONSOLES[c].kind && (CONSOLES[c].types >> kind_or_type & 1)) : CONSOLES[c].kind == kind_or_type) return c;+ return -1;+}++int rules(void) {+ bots_frozen = true;+ /* the lobby's defaults (GameOptionsData) */+ const Options *o = &g.opt;+ CHECK(o->kill_cd == 90 && o->kill_dist == 1, "kill cooldown 45 s, distance medium");+ CHECK(o->crew_vision == 4 && o->imp_vision == 6 && o->speed == 4, "vision 1x / 1.5x, speed 1x");+ CHECK(o->common == 1 && o->longt == 1 && o->shortt == 2, "tasks 1 common, 1 long, 2 short");+ CHECK(o->meetings == 1 && o->meet_cd == 15 && o->discuss == 15 && o->vote * 5 == 120, "meetings 1, 15 s, 15 s, 120 s");+ CHECK(o->confirm && !o->anon && o->visual && o->taskbar == 0 && o->impostors == 1 && o->bots == 7, "the rest");++ /* players */+ round_of(7, 1, 1);+ CHECK(g.nplayers == 8 && count_imps() == 1 && !g.p[0].imp, "8 players, 1 impostor, the player a crewmate");+ CHECK(near(player_speed(0), 2.5f), "speed 2.5");+ CHECK(near(vision_radius(0), 5.0f), "crew vision 5");+ int imp = first(true, -1);+ CHECK(near(vision_radius(imp), 7.5f), "impostor vision 7.5");+ CHECK(near(kill_distance(), 1.8f) && near(kill_cooldown(), 45), "kill distance 1.8, cooldown 45");+ for (int i = 0; i < g.nplayers; i++) CHECK(near(g.p[i].kill_cd, 10), "the first kill after 10 s");+ /* tasks: the same common task for all, then long and short ones, one of each type */+ int common = g.p[0].tasks[0].def;+ for (int i = 0; i < g.nplayers; i++) {+ const Player *P = &g.p[i];+ CHECK(P->ntasks == 4, "4 tasks (%d)", P->ntasks);+ CHECK(P->tasks[0].def == common && TASK_DEFS[common].kind == 0, "the common task shared");+ CHECK(TASK_DEFS[P->tasks[1].def].kind == 1 && TASK_DEFS[P->tasks[2].def].kind == 2 && TASK_DEFS[P->tasks[3].def].kind == 2,+ "long then short");+ for (int a = 0; a < P->ntasks; a++)+ for (int b = a + 1; b < P->ntasks; b++)+ CHECK(TASK_DEFS[P->tasks[a].def].type != TASK_DEFS[P->tasks[b].def].type, "one task of each type");+ CHECK(P->tasks[0].data[0] < P->tasks[0].data[1] && P->tasks[0].data[1] < P->tasks[0].data[2], "wiring panels in order");+ for (int k = 0; k < P->ntasks; k++) CHECK(task_console(P, &P->tasks[k]) >= 0, "every task has a console");+ }+ /* impostors by players (GameOptionsData.MaxImpostors) */+ round_of(5, 3, 0);+ CHECK(count_imps() == 1, "6 players: 1 impostor");+ round_of(7, 3, 0);+ CHECK(count_imps() == 2, "8 players: 2 impostors");+ round_of(9, 3, 2);+ CHECK(count_imps() == 3 && g.p[0].imp, "10 players: 3, the player one of them");++ /* kills */+ round_of(7, 1, 2);+ int victim = first(false, -1);+ g.p[0].pos = (V2){g.p[victim].pos.x + 1.0f, g.p[victim].pos.y};+ g.p[0].kill_cd = 0;+ CHECK(kill_target(0) == victim, "a crewmate a unit away in reach");+ g.p[0].pos.x += 1.0f;+ CHECK(kill_target(0) != victim, "two units: out of reach");+ g.p[0].pos.x -= 1.0f;+ V2 at = g.p[victim].pos;+ kill_player(0, victim);+ CHECK(!g.p[victim].alive && g.bodies[0].on && g.bodies[0].who == victim, "a body where it stood");+ CHECK(near(g.p[0].pos.x, at.x) && near(g.p[0].kill_cd, 45), "the killer on the spot, 45 s to wait");+ /* the ghost: faster, no light, tasks still count */+ CHECK(near(player_speed(victim), 3.0f), "ghosts at 3.0");++ /* reports: within 3 units, not through a wall */+ round_of(7, 1, 1);+ victim = 1;+ imp = first(true, -1);+ kill_player(imp, victim);+ int what = -1;+ g.p[0].pos = (V2){g.bodies[0].pos.x + 2.9f, g.bodies[0].pos.y};+ CHECK(hud_action(&what) == 2, "a body 2.9 away: report");+ g.p[0].pos = (V2){g.bodies[0].pos.x + 3.2f, g.bodies[0].pos.y};+ CHECK(hud_action(&what) != 2, "3.2 away: no report");++ /* winning: impostors as many as the crew */+ round_of(3, 1, 1);+ imp = first(true, -1);+ for (int k = 0; k < 2; k++) kill_player(imp, first(false, -1));+ CHECK(g.screen == S_END && g.winner == 1 && g.end_reason == 1, "1 impostor, 1 crewmate: impostors win");+ /* the tasks */+ round_of(7, 1, 1);+ for (int i = 0; i < g.nplayers; i++)+ for (int k = 0; k < g.p[i].ntasks && !g.p[i].imp; k++)+ while (!task_done(&g.p[i].tasks[k])) task_step_done(i, k);+ CHECK(g.screen == S_END && g.winner == 0 && g.end_reason == 2, "every task done: crewmates win");+ /* dead crewmates' tasks count */+ round_of(7, 1, 1);+ imp = first(true, -1);+ kill_player(imp, 0);+ for (int i = 0; i < g.nplayers; i++)+ for (int k = 0; k < g.p[i].ntasks && !g.p[i].imp; k++)+ while (!task_done(&g.p[i].tasks[k])) task_step_done(i, k);+ CHECK(g.screen == S_END && g.winner == 0 && g.end_reason == 2, "ghosts' tasks finish it too");++ /* sabotage: 30 s to fix the reactor; two hands at once fix it */+ round_of(7, 1, 1);+ g.sab_cd = 0;+ CHECK(sabotage_ready(), "sabotage ready");+ sabotage(SAB_REACTOR);+ CHECK(g.sab == SAB_REACTOR && !sabotage_ready(), "one sabotage at a time");+ run(29.5f);+ CHECK(g.screen == S_PLAY, "29.5 s: still on");+ run(1);+ CHECK(g.screen == S_END && g.winner == 1 && g.end_reason == 3, "30 s: meltdown, impostors win");+ round_of(7, 1, 1);+ g.sab_cd = 0;+ sabotage(SAB_REACTOR);+ run(5);+ g.reactor_hold[0] = 1, g.reactor_hold[1] = 2;+ run(1.0f / 30);+ CHECK(g.sab == SAB_NONE && near(g.sab_cd, 30), "both hands: fixed, 30 s before the next");+ g.sab_cd = 0;+ sabotage(SAB_REACTOR);+ g.reactor_hold[0] = g.reactor_hold[1] = 1;+ run(1.0f / 30);+ CHECK(g.sab == SAB_REACTOR, "one player can't hold both");+ /* a meeting ends it */+ start_meeting(1, NONE);+ CHECK(g.sab == SAB_NONE, "a meeting ends the meltdown");+ /* oxygen: both keypads */+ round_of(7, 1, 1);+ g.sab_cd = 0;+ sabotage(SAB_O2);+ CHECK(g.o2_code < 100000, "a five-digit code");+ g.o2_done = 1;+ run(1);+ CHECK(g.sab == SAB_O2, "one keypad isn't enough");+ g.o2_done = 3;+ run(1.0f / 30);+ CHECK(g.sab == SAB_NONE, "both: oxygen restored");+ /* lights: crew vision down to 1, impostors keep theirs; all switches up fixes it */+ g.sab_cd = 0;+ sabotage(SAB_LIGHTS);+ CHECK(g.switches != 0x1F, "some switches down");+ run(1.5f);+ CHECK(near(vision_radius(0), 1.0f) && near(vision_radius(first(true, -1)), 7.5f), "lights out: 1 for the crew");+ g.switches = 0x1F;+ run(2);+ CHECK(g.sab == SAB_NONE && near(vision_radius(0), 5.0f), "switches up: light back");+ /* doors: 10 s closed, the room's 30 s cooldown */+ round_of(7, 1, 1);+ close_doors(R_STORAGE);+ int d = 0;+ while (d < N_DOORS && DOORS[d].room != R_STORAGE) d++;+ CHECK(door_closed(d), "storage doors closed");+ run(9.8f);+ CHECK(door_closed(d), "still closed at 9.8 s");+ run(0.4f);+ CHECK(!door_closed(d), "open after 10 s");+ close_doors(R_STORAGE);+ CHECK(!door_closed(d), "the room's cooldown");+ run(21);+ close_doors(R_STORAGE);+ CHECK(door_closed(d), "again after 30 s");++ /* the emergency button: one each, 15 s after the start, not during a crisis */+ round_of(7, 1, 1);+ int b = console_of(2, false);+ g.p[0].pos = (V2){CONSOLES[b].x, CONSOLES[b].y - 1.0f};+ CHECK(console_use(0, b) < 0, "the button waits 15 s");+ run(15.1f);+ CHECK(console_use(0, b) >= 0, "then it's ready");+ g.sab_cd = 0;+ sabotage(SAB_O2);+ CHECK(console_use(0, b) < 0, "not during a crisis");+ g.o2_done = 3;+ run(0.1f);+ start_meeting(0, NONE);+ CHECK(g.p[0].emergencies == 0 && g.screen == S_REPORT, "the meeting called, none left");++ /* votes: the most votes out, a tie or skips nobody */+ round_of(7, 1, 1);+ start_meeting(0, NONE);+ run(0);+ g.screen = S_MEETING, g.meet_stage = 1, g.meet_t = 0;+ imp = first(true, -1);+ memset(g.votes, NONE, sizeof g.votes);+ for (int i = 0; i < g.nplayers; i++) g.votes[i] = (uint8_t)(i < 5 ? imp : SKIP);+ game_tick(0);+ CHECK(g.meet_stage == 2 && g.ejected == imp, "5 against 3 skips: out");+ for (int k = 0; k < 6 * 30 && g.screen != S_EJECT; k++) game_tick(0);+ CHECK(g.screen == S_EJECT && !g.p[imp].alive, "ejected");+ for (int k = 0; k < 8 * 30 && g.screen == S_EJECT; k++) game_tick(0);+ CHECK(g.screen == S_END && g.winner == 0 && g.end_reason == 0, "the impostor out: crewmates win");+ round_of(7, 1, 1);+ start_meeting(0, NONE);+ g.screen = S_MEETING, g.meet_stage = 1, g.meet_t = 0;+ for (int i = 0; i < g.nplayers; i++) g.votes[i] = (uint8_t)(i < 4 ? 1 : 2);+ game_tick(0);+ CHECK(g.ejected == NONE && g.tie, "4 to 4: a tie, nobody out");+ round_of(7, 1, 1);+ start_meeting(0, NONE);+ g.screen = S_MEETING, g.meet_stage = 1, g.meet_t = 0;+ for (int i = 0; i < g.nplayers; i++) g.votes[i] = (uint8_t)(i < 5 ? SKIP : 1);+ game_tick(0);+ CHECK(g.ejected == NONE && !g.tie, "most skipped: nobody out");++ /* vents: impostors only, within 0.75 */+ round_of(7, 1, 2);+ g.p[0].pos = (V2){VENTS[0].x + 0.5f, VENTS[0].y};+ what = -1;+ CHECK(hud_action(&what) == 3 && what == 0, "the vent 0.5 away");+ g.p[0].imp = 0;+ CHECK(hud_action(&what) != 3, "not for crewmates");++ /* the tasks' consoles: every task step has one */+ for (int d2 = 0; d2 < N_TASK_DEFS; d2++) {+ Task t = {(uint8_t)d2, 0, {0, 2, 4}, 0, 0};+ for (; t.step < TASK_DEFS[d2].steps; t.step++) CHECK(task_console(0, &t) >= 0, "%s step %d", TASK_DEFS[d2].name, t.step);+ }+ bots_frozen = false;+ printf("RULES %d checks, %d failed\n", checks, fails);+ return fails;+}