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.

PolyTrack: a route finder for the official tracks, and the tests

Mason363d350ae4932cb
diff --git a/games/polytrack/src/car.c b/games/polytrack/src/car.c
index e6f14820..82baaa84 100644
--- a/games/polytrack/src/car.c
+++ b/games/polytrack/src/car.c
@@ -153,6 +153,13 @@ static void cache_refresh(car_t *c) {
c->cache_ok = 1;
}
+void car_cache_rebuild(car_t *c) {
+ /* the same triangles, in the same order, as when the cache was last filled (for a state put back) */
+ if (!c->cache_ok) return;
+ v3 h = V3(5.0f, 5.0f, 5.0f);
+ c->ncache = world_gather(vsub(c->cache_at, h), vadd(c->cache_at, h), cache, PT_CACHE);
+}
+
/* Bullet's btTriangleRaycastCallback, closest hit: fraction, normal facing the ray's start */
static bool ray_tri(const wtri *t, v3 from, v3 to, float *frac, v3 *normal) {
v3 v10 = vsub(t->b, t->a), v20 = vsub(t->c, t->a);
diff --git a/games/polytrack/src/car.h b/games/polytrack/src/car.h
index 6e4f85b6..cb1f9f6d 100644
--- a/games/polytrack/src/car.h
+++ b/games/polytrack/src/car.h
@@ -59,5 +59,6 @@ void car_reset(car_t *c); /* at the start, as before the first frame *
void car_step(car_t *c, unsigned input);
bool car_finished(const car_t *c);
uint32_t car_hash(const car_t *c); /* the state's bits, for the tests */
+void car_cache_rebuild(car_t *c); /* after putting back a saved state */
int car_checkpoints(void); /* checkpoints on the track (orders) */
#endif
diff --git a/games/polytrack/tests/play.c b/games/polytrack/tests/play.c
index df83e6cc..69dd64d4 100644
--- a/games/polytrack/tests/play.c
+++ b/games/polytrack/tests/play.c
@@ -1,20 +1,22 @@
/* PolyTrack on a computer, for tests and pictures (make host, make test).
*
- * play test DIR every official track driven to the finish by its recorded run, to the same frame and hash
- * on every computer; the checkpoints in order, respawns, restarts; the whole app through its
- * keys; saves written and read back, damaged saves, a full storage, the ghosts; a few hundred
- * sessions of random keys
- * play shots DIR pictures of the screens (DIR/<name>.ppm)
- * play view DIR T MS a picture of track T after MS ms of its recorded run (DIR/view.ppm)
- * play film DIR T MS STEP MS ms of track T's recorded run, a frame every STEP ms (DIR/<frame>_<ms>.raw, for
- * tools/record.py --frames)
- */
+ * play test DIR every official track driven to the finish by its recorded run, to the same frame and
+ * hash on every computer; the checkpoints in order, respawns, restarts; the whole app
+ * through its keys, all 17 tracks one after the other; saves written and read back,
+ * damaged saves, a full storage, the ghosts; 300 sessions of random keys
+ * play shots DIR pictures of the screens (DIR/<name>.ppm)
+ * play view DIR T MS a picture of track T after MS ms of its recorded run (DIR/view.ppm)
+ * play film DIR T MS STEP MS ms of track T's recorded run, a frame every STEP ms (DIR/<frame>_<ms>.raw, for
+ * tools/record.py --frames)
+ * play probe DIR T MS [K] the car's state after MS ms of the run, then a second of keys K (IN_ bits)
+ * A run found by tests/route.c can be looked at before it goes into routes.h: ROUTE_FILE=its output. */
#define main pt_app_main
#include "../src/main.c"
#undef main
#include <stdio.h>
#include <stdlib.h>
#include "routes.h"
+#include "../../../launcher/src/gfx.h"
/* ---------------------------------------------------------------- the calculator, on a computer */
static uint16_t fb[240][320];
@@ -24,19 +26,17 @@ void plat_push(int x, int y, int w, int h, const uint16_t *px) {
for (int i = 0; i < w; i++) fb[y + j][x + i] = px[j * w + i];
}
void *np_alloc(uint32_t n) { (void)n; return 0; }
-static double clock_ms;
-static uint32_t (*script)(uint32_t frame);
-static uint32_t frame_no, quit_at;
-static void (*watch)(void);
+static uint32_t clock_ms, tick = 5, nap = 1; /* ms a look at the keys takes; a sleep's share of what it asks */
+static uint32_t (*script)(uint32_t n);
+static uint32_t calls, quit_at;
uint32_t plat_keys(void) {
- clock_ms += 5;
- frame_no++;
- if (watch) watch();
- if (frame_no >= quit_at) return PK_HOME;
- return script ? script(frame_no) : 0;
-}
-uint32_t plat_millis(void) { return (uint32_t)clock_ms; }
-void plat_sleep(uint32_t ms) { clock_ms += ms; }
+ clock_ms += tick;
+ calls++;
+ if (quit_at && calls >= quit_at) return PK_HOME;
+ return script ? script(calls) : 0;
+}
+uint32_t plat_millis(void) { return clock_ms; }
+void plat_sleep(uint32_t ms) { clock_ms += ms * nap; }
void plat_vblank(void) {}
bool plat_slow(void) { return false; }
void plat_begin(void) {}
@@ -45,6 +45,7 @@ int plat_end(void) { return 0; }
#define NFILES 6
static struct { char name[32]; uint8_t data[16384]; uint32_t len; bool on; } files[NFILES];
static bool storage_full;
+static int writes;
bool plat_save(const char *name, const void *data, uint32_t len) {
if (storage_full) return false;
int k = -1;
@@ -54,6 +55,7 @@ bool plat_save(const char *name, const void *data, uint32_t len) {
if (!files[i].on) k = i;
if (k < 0 || len > sizeof files[k].data) return false;
strcpy(files[k].name, name), memcpy(files[k].data, data, len), files[k].len = len, files[k].on = true;
+ writes++;
return true;
}
const uint8_t *plat_load(const char *name, uint32_t *len) {
@@ -61,15 +63,18 @@ const uint8_t *plat_load(const char *name, uint32_t *len) {
if (files[i].on && !strcmp(files[i].name, name)) return *len = files[i].len, files[i].data;
return 0;
}
-static void forget(const char *name) {
- for (int i = 0; i < NFILES; i++)
- if (files[i].on && !strcmp(files[i].name, name)) files[i].on = false;
-}
static uint8_t *file_data(const char *name, uint32_t *len) {
for (int i = 0; i < NFILES; i++)
if (files[i].on && !strcmp(files[i].name, name)) return *len = files[i].len, files[i].data;
return 0;
}
+static void forget(const char *name) {
+ for (int i = 0; i < NFILES; i++)
+ if (files[i].on && !strcmp(files[i].name, name)) files[i].on = false;
+}
+static void forget_all(void) {
+ for (int i = 0; i < NFILES; i++) files[i].on = false;
+}
static void shot(const char *dir, const char *name) {
char path[512];
@@ -85,27 +90,55 @@ static void shot(const char *dir, const char *name) {
fclose(f);
}
-static int fails;
-#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
+static int fails, checks;
+#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
-/* ---------------------------------------------------------------- a recorded run: the keys held from a frame on */
-static unsigned route_input(const route_t *r, uint32_t frame) {
- unsigned in = 0;
- for (int i = 0; i < r->n; i++) {
- if (r->ev[i].frame > frame) break;
- in = r->ev[i].keys;
+/* ---------------------------------------------------------------- the recorded runs (routes.h) */
+static uint8_t slots[60000]; /* the keys, 10 ms each */
+static int nslots;
+static void route_decode(const char *s) {
+ nslots = 0;
+ while (*s && nslots < (int)sizeof slots) {
+ char c = *s++;
+ uint8_t k;
+ if (c >= 'a' && c <= 'p') k = (uint8_t)(c - 'a');
+ else if (c >= 'A' && c <= 'P') k = (uint8_t)(c - 'A' + IN_RESET);
+ else continue;
+ int r = 0;
+ while (*s >= '0' && *s <= '9') r = r * 10 + (*s++ - '0');
+ if (!r) r = 1;
+ while (r-- && nslots < (int)sizeof slots) slots[nslots++] = k;
}
- return in;
}
+static char file_route[400000];
+static void route_load(int t) {
+ const char *path = getenv("ROUTE_FILE");
+ FILE *f = path ? fopen(path, "r") : NULL;
+ if (!f) {
+ route_decode(pt_routes[t]);
+ return;
+ }
+ /* (the letters between quotes) */
+ int n = 0, ch, in = 0;
+ while ((ch = fgetc(f)) != EOF && n < (int)sizeof file_route - 1) {
+ if (ch == '"') in = !in;
+ else if (in) file_route[n++] = (char)ch;
+ }
+ file_route[n] = 0;
+ fclose(f);
+ route_decode(file_route);
+}
+static unsigned route_keys(uint32_t frame) { return frame / 10 < (uint32_t)nslots ? slots[frame / 10] : 0; }
-/* drives track t with its route; returns the finish frame (0: not finished by max) */
-static uint32_t drive(int t, const route_t *r, uint32_t max, uint32_t *hash) {
+/* drives track t with its run; the finish frame (0: not finished by max) */
+static car_t c1;
+static uint32_t drive(int t, uint32_t max, uint32_t *hash) {
world_load(t);
- static car_t c;
- car_reset(&c);
- for (uint32_t f = 0; f < max && !c.finish; f++) car_step(&c, route_input(r, f));
- if (hash) *hash = car_hash(&c);
- return c.finish;
+ route_load(t);
+ car_reset(&c1);
+ for (uint32_t f = 0; f < max && !c1.finish; f++) car_step(&c1, route_keys(f));
+ if (hash) *hash = car_hash(&c1);
+ return c1.finish;
}
static void render_race(const car_t *c, chase_t *ch, float dt) {
@@ -116,9 +149,484 @@ static void render_race(const car_t *c, chase_t *ch, float dt) {
pt_render(&sv, c, &cam, &look, NULL, &ui);
}
+/* ---------------------------------------------------------------- the tests */
+/* the runs: to the finish, at the frame and with the hash recorded; the checkpoints in order on the way */
+static void test_runs(void) {
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ world_load(t);
+ route_load(t);
+ car_reset(&c1);
+ int n = car_checkpoints();
+ uint16_t last = 0;
+ bool order_ok = true;
+ for (uint32_t f = 0; f < 600000 && !c1.finish; f++) {
+ car_step(&c1, route_keys(f));
+ if (c1.next_cp != last) {
+ if (c1.next_cp != last + 1 || (last < 64 && c1.cp_frames[last] != c1.frames)) order_ok = false;
+ last = c1.next_cp;
+ }
+ }
+ uint32_t h = car_hash(&c1);
+ printf("%-9s %s %6u ms, hash %08x\n", pt_tracks[t].name, c1.finish ? "finished in" : "NOT FINISHED", c1.finish, h);
+ CHECK(c1.finish, "%s: not finished", pt_tracks[t].name);
+ CHECK(c1.next_cp == n, "%s: finished with %d of %d checkpoints", pt_tracks[t].name, c1.next_cp, n);
+ CHECK(order_ok, "%s: checkpoints out of order", pt_tracks[t].name);
+ CHECK(c1.finish == pt_route_ms[t], "%s: finished in %u ms, recorded %u", pt_tracks[t].name, c1.finish, pt_route_ms[t]);
+ CHECK(h == pt_route_hash[t], "%s: hash %08x, recorded %08x (the physics isn't the same here)", pt_tracks[t].name, h,
+ pt_route_hash[t]);
+ }
+}
+
+/* puts the car at rest in a detector, and takes a frame */
+static void put_in(car_t *c, int k) {
+ detector_t d;
+ world_detector(k, &d);
+ c->pos = vmul(vadd(d.lo, d.hi), 0.5f);
+ c->vel = c->ang = V3(0.0f, 0.0f, 0.0f);
+ c->cache_ok = 0;
+ car_step(c, 0);
+}
+
+/* the detector of the o-th checkpoint order (smallest first) */
+static int det_of_order(int o) {
+ const pt_track *tr = W.t;
+ uint16_t got[64];
+ int ng = 0;
+ detector_t d;
+ for (int j = 0; j < tr->ncp; j++) {
+ world_detector(j, &d);
+ bool dup = false;
+ for (int m = 0; m < ng; m++) dup |= got[m] == d.order;
+ if (!dup && ng < 64) got[ng++] = d.order;
+ }
+ for (int i = 0; i < tr->ncp; i++) {
+ world_detector(i, &d);
+ int below = 0;
+ for (int m = 0; m < ng; m++) below += got[m] < d.order;
+ if (below == o) return i;
+ }
+ return -1;
+}
+
+/* checkpoints count in their order only, the finish after all of them */
+static void test_checkpoints(void) {
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ world_load(t);
+ car_reset(&c1);
+ const pt_track *tr = W.t;
+ int n = car_checkpoints();
+ put_in(&c1, tr->ncp);
+ CHECK(!c1.finish && c1.next_cp == 0, "%s: the finish counted before the checkpoints", tr->name);
+ if (n >= 2) {
+ put_in(&c1, det_of_order(n - 1));
+ CHECK(c1.next_cp == 0, "%s: the last checkpoint counted first", tr->name);
+ }
+ for (int o = 0; o < n; o++) {
+ int k = det_of_order(o);
+ CHECK(k >= 0, "%s: no checkpoint of order %d", tr->name, o);
+ if (k < 0) break;
+ put_in(&c1, k);
+ CHECK(c1.next_cp == o + 1, "%s: checkpoint %d not taken", tr->name, o);
+ CHECK(!c1.finish, "%s: finished early", tr->name);
+ }
+ put_in(&c1, tr->ncp);
+ CHECK(c1.finish, "%s: the finish didn't count after every checkpoint", tr->name);
+ }
+}
+
+/* respawns: back to the last flat checkpoint, standing, its count kept; a held key is one respawn; none before */
+static void test_respawn(void) {
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ world_load(t);
+ route_load(t);
+ car_reset(&c1);
+ car_step(&c1, IN_RESET | IN_UP);
+ v3 s0;
+ qt q0;
+ world_start(&s0, &q0);
+ CHECK(!c1.has_respawn && vlen(vsub(c1.pos, s0)) < 0.5f, "%s: a respawn before any checkpoint", pt_tracks[t].name);
+ uint32_t f = 1;
+ while (f < 600000 && !c1.has_respawn && !c1.finish) car_step(&c1, route_keys(f++));
+ if (!c1.has_respawn) continue; /* (only checkpoints on walls: none to go back to) */
+ for (int i = 0; i < 1500 && !c1.finish; i++) car_step(&c1, route_keys(f++));
+ if (c1.finish) continue;
+ uint16_t cp = c1.next_cp;
+ uint32_t frames = c1.frames;
+ car_step(&c1, IN_RESET);
+ CHECK(vlen(vsub(c1.pos, c1.respawn.pos)) < 0.05f, "%s: not back at the checkpoint", pt_tracks[t].name);
+ CHECK(c1.next_cp == c1.respawn.next && c1.next_cp <= cp, "%s: the checkpoints' count changed", pt_tracks[t].name);
+ CHECK(c1.frames == frames + 1, "%s: the clock stopped", pt_tracks[t].name);
+ CHECK(vlen(c1.vel) < 0.1f, "%s: still moving after a respawn", pt_tracks[t].name);
+ v3 at = c1.pos;
+ for (int i = 0; i < 300; i++) car_step(&c1, IN_RESET);
+ CHECK(vlen(vsub(c1.pos, at)) < 1.0f, "%s: a held key respawned again", pt_tracks[t].name);
+ /* (and it drives off) */
+ for (int i = 0; i < 2000; i++) car_step(&c1, IN_UP);
+ CHECK(c1.speed > 30.0f || c1.next_cp > cp || c1.finish, "%s: stuck after a respawn (%.0f km/h)", pt_tracks[t].name,
+ (double)c1.speed);
+ }
+}
+
+/* ---------------------------------------------------------------- the whole app, through its keys */
+static uint32_t app_track_runs; /* races finished */
+static int app_phase;
+static uint32_t to_pk(unsigned in) {
+ uint32_t k = 0;
+ if (in & IN_UP) k |= PK_UP;
+ if (in & IN_DOWN) k |= PK_DOWN;
+ if (in & IN_LEFT) k |= PK_LEFT;
+ if (in & IN_RIGHT) k |= PK_RIGHT;
+ if (in & IN_RESET) k |= PK_OK;
+ return k;
+}
+static int loaded = -1;
+/* Title: Play; Tracks: OK; the guide: OK three times; each track with its run, then Next track; after the last,
+ Tracks, Back, Back, the quit question: Down, OK */
+static uint32_t script_all(uint32_t n) {
+ if (n & 1) return 0; /* (keys go up between presses) */
+ switch (ui.screen) {
+ case V_TITLE: return app_phase ? PK_BACK : PK_OK;
+ case V_TRACKS: return app_phase ? PK_BACK : PK_OK;
+ case V_GUIDE: return PK_OK;
+ case V_QUIT: return ui.sel ? PK_OK : PK_DOWN;
+ case V_RACE:
+ if (loaded != sv.p.track) route_load(sv.p.track), loaded = sv.p.track;
+ return to_pk(route_keys(car.frames));
+ case V_RESULT:
+ if (sv.p.track == PT_NTRACKS - 1) {
+ app_phase = 1;
+ return ui.sel == RES_TRACKS ? PK_OK : PK_DOWN;
+ }
+ return ui.sel == RES_NEXT ? PK_OK : PK_DOWN;
+ default: return PK_BACK;
+ }
+}
+static void reset_app(void) {
+ memset(&ui, 0, sizeof ui);
+ memset(&sv, 0, sizeof sv);
+ memset(&car, 0, sizeof car);
+ rd_detail = 3;
+ calls = 0, quit_at = 0;
+ app_phase = 0, loaded = -1;
+}
+
+static void test_app(void) {
+ forget_all();
+ storage_full = false;
+ reset_app();
+ tick = 1, nap = 0, frame_gap = 2000; /* (a physics frame each time round: the runs' keys land as recorded) */
+ script = script_all;
+ quit_at = 30000000;
+ pt_app_main();
+ CHECK(calls < quit_at, "the app didn't quit through its menus (screen %d)", ui.screen);
+ pt_file f;
+ bool ok = save_load(&f);
+ CHECK(ok, "no save after the races");
+ for (int t = 0; t < PT_NTRACKS && ok; t++)
+ CHECK(f.p.best[t] == pt_route_ms[t], "app: %s's best %u, its run %u", pt_tracks[t].name, f.p.best[t], pt_route_ms[t]);
+ CHECK(ok && f.p.finishes == PT_NTRACKS, "app: %u finishes", ok ? f.p.finishes : 0);
+ CHECK(ok && f.p.guide == 1, "app: the guide wasn't marked read");
+ /* ghosts: the last ones raced, as many as fit */
+ ghosts_load();
+ int kept = 0;
+ ghost_entry g;
+ for (int t = 0; t < PT_NTRACKS; t++)
+ if (ghosts_find(t, &g)) {
+ kept++;
+ CHECK(g.ms == pt_route_ms[t], "app: %s's ghost has %u ms", pt_tracks[t].name, g.ms);
+ }
+ uint32_t glen;
+ CHECK(file_data(PT_GHOST_NAME, &glen) && glen <= PT_GHOST_BYTES, "app: the ghosts' file");
+ CHECK(ghosts_find(PT_NTRACKS - 1, &g) || pt_route_ms[PT_NTRACKS - 1] > 130000, "app: the last track's ghost is missing");
+ printf("app: 17 tracks through the menus, %d ghosts kept (%u bytes)\n", kept, glen);
+}
+
+/* the ghost follows the run it was made from */
+static void test_ghost(void) {
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ world_load(t);
+ route_load(t);
+ car_reset(&c1);
+ ghost_rec_start(&rec);
+ ghost_rec_add(&rec, c1.pos, c1.rot);
+ static v3 path[6000];
+ int np = 0;
+ path[np++] = c1.pos;
+ for (uint32_t f = 0; f < 600000 && !c1.finish; f++) {
+ car_step(&c1, route_keys(f));
+ if (!c1.finish && c1.frames % GHOST_STEP == 0) {
+ ghost_rec_add(&rec, c1.pos, c1.rot);
+ if (np < 6000) path[np++] = c1.pos;
+ }
+ }
+ if (rec.full) {
+ printf("%-9s ghost: %u ms is too long for a ghost\n", pt_tracks[t].name, c1.finish);
+ CHECK(c1.finish > 100000, "%s: a ghost of %u ms didn't fit", pt_tracks[t].name, c1.finish);
+ continue;
+ }
+ ghost_play_start(&gplay, rec.buf, rec.len, c1.finish);
+ float worst = 0.0f;
+ for (int i = 0; i + 1 < np; i++) {
+ v3 p;
+ qt q;
+ bool on = ghost_at(&gplay, (uint32_t)i * GHOST_STEP, &p, &q);
+ CHECK(on, "%s: no ghost at %d ms", pt_tracks[t].name, i * GHOST_STEP);
+ if (!on) break;
+ float e = vlen(vsub(p, path[i]));
+ if (e > worst) worst = e;
+ }
+ CHECK(worst < 0.1f, "%s: the ghost is %.2f m off", pt_tracks[t].name, (double)worst);
+ }
+}
+
+/* keys pressed one at a time (O OK, U D L R arrows, B Back, X Backspace, G gas for a second), then Home once
+ the screen is reached */
+static const char *seq;
+static uint8_t target;
+static uint32_t gas_until;
+static uint32_t script_seq(uint32_t n) {
+ if (n < gas_until) return PK_UP;
+ if (n & 1) return 0;
+ if (!*seq && ui.screen == target) return PK_HOME;
+ char c = *seq ? *seq++ : 0;
+ switch (c) {
+ case 'O': return PK_OK;
+ case 'U': return PK_UP;
+ case 'D': return PK_DOWN;
+ case 'L': return PK_LEFT;
+ case 'R': return PK_RIGHT;
+ case 'B': return PK_BACK;
+ case 'X': return PK_RESTART;
+ case 'G': gas_until = n + 200; return PK_UP;
+ default: return 0;
+ }
+}
+static void run_seq(const char *keys, uint8_t screen) {
+ reset_app();
+ tick = 5, nap = 1, frame_gap = 200;
+ seq = keys, target = screen, gas_until = 0;
+ script = script_seq;
+ quit_at = 5000;
+ pt_app_main();
+}
+
+static void test_restart_home(void) {
+ /* Backspace in a race: the start again, the clock not running */
+ forget_all();
+ run_seq("OOOOOGX", V_RACE);
+ v3 s0;
+ qt q0;
+ world_start(&s0, &q0);
+ CHECK(ui.screen == V_RACE && car.frames == 0 && !ui.started, "restart: the race didn't start over (screen %d, %u frames)",
+ ui.screen, car.frames);
+ CHECK(vlen(vsub(car.pos, s0)) < 0.5f, "restart: the car isn't at the start");
+ /* OK before the first checkpoint: the start again too */
+ forget_all();
+ run_seq("OOOOOGO", V_RACE);
+ CHECK(ui.screen == V_RACE && car.frames == 0, "OK before a checkpoint didn't start over");
+ /* Home: saved and out, from each screen */
+ static const struct { const char *keys; uint8_t screen; } H[] = {
+ {"", V_TITLE}, {"O", V_TRACKS}, {"DOR", V_CAR}, {"DDO", V_HELP}, {"OO", V_GUIDE}, {"OOOOOG", V_RACE},
+ {"OOOOOGB", V_PAUSE}, {"OOOOOGBUO", V_LEAVE}, {"B", V_QUIT}, {"OOOOOGBDO", V_RACE},
+ };
+ for (unsigned i = 0; i < sizeof H / sizeof H[0]; i++) {
+ forget_all();
+ run_seq(H[i].keys, H[i].screen);
+ pt_file f;
+ CHECK(calls < quit_at && ui.screen == H[i].screen, "Home: \"%s\" didn't reach screen %d (%d)", H[i].keys, H[i].screen,
+ ui.screen);
+ CHECK(save_load(&f), "Home on screen %d didn't save", H[i].screen);
+ if (H[i].screen == V_CAR) CHECK(f.p.paint == 1, "Home: the car's colour wasn't kept");
+ }
+ /* Quit game asks first: Back, Up (Quit game), OK, then Keep racing goes back */
+ forget_all();
+ run_seq("OOOOOGBUOO", V_RACE);
+ CHECK(ui.screen == V_RACE, "Keep racing didn't go back to the race");
+}
+
+/* the saves: kept and read back, damaged files, the copy in the script, a full storage */
+static void test_saves(void) {
+ forget_all();
+ storage_full = false;
+ pt_file a, b;
+ CHECK(!save_load(&a), "a save without files");
+ pt_fresh(&a.p);
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ a.p.best[t] = 30000u + (uint32_t)t * 1111u;
+ for (int k = 0; k < 3; k++) a.p.split[t][k] = 5000u * (uint32_t)(k + 1) + (uint32_t)t;
+ }
+ a.p.finishes = 1234, a.p.track = 7, a.p.paint = 3, a.p.view = 1, a.p.guide = 1;
+ CHECK(save_write(&a), "the save wasn't written");
+ CHECK(save_load(&b) && !memcmp(&a.p, &b.p, sizeof a.p), "the save didn't read back");
+ uint32_t n;
+ uint8_t *d = file_data(PT_SAVE_NAME, &n);
+ CHECK(d && n == sizeof(pt_file) && n == 768, "polytrack.sav is %u bytes", n);
+ static uint8_t good[1024], copy[2048];
+ memcpy(good, d, n);
+ uint32_t cn;
+ uint8_t *cp = file_data(PT_COPY_NAME, &cn);
+ CHECK(cp && cn < sizeof copy, "no copy in polytrack_saves.py");
+ memcpy(copy, cp, cn);
+ /* every byte damaged in turn: the copy brings it back */
+ int bad = 0;
+ for (uint32_t i = 0; i < n; i++) {
+ for (int bit = 0; bit < 8; bit += 3) {
+ d = file_data(PT_SAVE_NAME, &n);
+ memcpy(d, good, n);
+ d[i] ^= (uint8_t)(1 << bit);
+ memset(&b, 0, sizeof b);
+ bool ok = save_load(&b);
+ if (!ok || memcmp(&a.p, &b.p, sizeof a.p)) bad++;
+ d = file_data(PT_SAVE_NAME, &n);
+ if (memcmp(d, good, n)) bad++; /* (put back) */
+ }
+ }
+ CHECK(!bad, "%d damaged saves weren't mended from the copy", bad);
+ /* both damaged: a fresh start, never wrong numbers */
+ int wrong = 0;
+ for (uint32_t i = 0; i < cn; i += 3) {
+ d = file_data(PT_SAVE_NAME, &n);
+ memcpy(d, good, n);
+ d[100] ^= 0x10;
+ uint8_t *c = file_data(PT_COPY_NAME, &cn);
+ memcpy(c, copy, cn);
+ c[i] ^= 0x04;
+ memset(&b, 0, sizeof b);
+ bool ok = save_load(&b);
+ if (ok && memcmp(&a.p, &b.p, sizeof a.p)) wrong++;
+ pt_profile z;
+ pt_fresh(&z);
+ if (!ok && memcmp(&b.p, &z, sizeof z)) wrong++;
+ }
+ CHECK(!wrong, "%d damaged saves read as something else", wrong);
+ /* cut short, too long, another version, numbers out of range */
+ forget_all();
+ save_write(&a);
+ forget(PT_COPY_NAME);
+ d = file_data(PT_SAVE_NAME, &n);
+ files[0].len = n - 1;
+ CHECK(!save_load(&b), "a save cut short was read");
+ forget_all();
+ pt_file e = a;
+ e.p.best[3] = PT_MAX_TIME + 1;
+ save_write(&e);
+ CHECK(!save_load(&b), "a time out of range was read");
+ e = a;
+ e.p.split[2][1] = 1; /* (before the one before it) */
+ save_write(&e);
+ CHECK(!save_load(&b), "checkpoint times out of order were read");
+ e = a;
+ e.p.paint = PT_PAINTS;
+ save_write(&e);
+ CHECK(!save_load(&b), "a paint out of range was read");
+ /* only the copy: the save comes back from it */
+ forget_all();
+ save_write(&a);
+ forget(PT_SAVE_NAME);
+ CHECK(save_load(&b) && !memcmp(&a.p, &b.p, sizeof a.p), "the copy didn't bring the save back");
+ CHECK(file_data(PT_SAVE_NAME, &n), "the save wasn't put back from its copy");
+ /* a full storage: the write says so, nothing breaks */
+ forget_all();
+ storage_full = true;
+ CHECK(!save_write(&a), "a write to a full storage said it worked");
+ CHECK(!ghosts_put(0, 1000, good, 100), "a ghost written to a full storage said it worked");
+ storage_full = false;
+ /* the app with a full storage: it races, finishes, says it couldn't save */
+ forget_all();
+ reset_app();
+ storage_full = true;
+ tick = 1, nap = 0, frame_gap = 2000;
+ script = script_all;
+ sv.p.track = 0;
+ quit_at = 120000;
+ pt_app_main();
+ CHECK(ui.save_failed, "the app didn't see the full storage");
+ storage_full = false;
+}
+
+/* the ghosts' file: the last raced first, within 12 KB; damaged: gone */
+static void test_ghost_file(void) {
+ forget_all();
+ ghosts_load();
+ ghost_entry g;
+ CHECK(!ghosts_find(0, &g), "a ghost without a file");
+ static uint8_t data[PT_GHOST_MAX];
+ for (int i = 0; i < PT_GHOST_MAX; i++) data[i] = (uint8_t)(i * 7 + 3);
+ for (int t = 0; t < PT_NTRACKS; t++) {
+ uint16_t len = (uint16_t)(1500 + t * 300);
+ CHECK(ghosts_put(t, 40000u + (uint32_t)t, data, len), "ghost %d not written", t);
+ CHECK(ghosts_find(t, &g) && g.len == len && g.ms == 40000u + (uint32_t)t && !memcmp(g.data, data, len),
+ "ghost %d didn't read back", t);
+ }
+ uint32_t n;
+ uint8_t *d = file_data(PT_GHOST_NAME, &n);
+ CHECK(d && n <= PT_GHOST_BYTES, "the ghosts' file is %u bytes", n);
+ ghosts_load();
+ CHECK(ghosts_find(PT_NTRACKS - 1, &g) && !ghosts_find(0, &g), "the ghosts kept aren't the last ones raced");
+ /* a long ghost pushes the others out, a short one keeps them */
+ CHECK(ghosts_put(5, 99999, data, PT_GHOST_MAX), "the longest ghost wasn't kept");
+ CHECK(ghosts_find(5, &g) && g.len == PT_GHOST_MAX, "the longest ghost didn't read back");
+ /* damaged: every byte in turn, no ghost read wrong */
+ d = file_data(PT_GHOST_NAME, &n);
+ static uint8_t good[PT_GHOST_BYTES];
+ memcpy(good, d, n);
+ int wrong = 0;
+ for (uint32_t i = 0; i < n; i += 37) {
+ d = file_data(PT_GHOST_NAME, &n);
+ memcpy(d, good, n);
+ d[i] ^= 0x21;
+ ghosts_load();
+ if (ghosts_find(5, &g)) wrong++;
+ }
+ CHECK(!wrong, "%d damaged ghost files were read", wrong);
+ d = file_data(PT_GHOST_NAME, &n);
+ memcpy(d, good, n);
+ ghosts_load();
+ CHECK(ghosts_find(5, &g), "the ghosts' file wasn't read back");
+}
+
+/* random keys: anything goes, nothing breaks, the save stays good */
+static uint32_t rng = 12345;
+static uint32_t rnd(void) { return rng = rng * 1103515245u + 12345u, rng >> 8; }
+static uint32_t held;
+static uint32_t script_random(uint32_t n) {
+ if (n % 20 == 0) {
+ static const uint32_t K[] = {PK_OK, PK_UP, PK_DOWN, PK_LEFT, PK_RIGHT, PK_BACK, PK_RESTART, PK_VIEW, PK_UP | PK_LEFT,
+ PK_UP | PK_RIGHT, PK_UP, PK_UP, 0, 0};
+ held = K[rnd() % (sizeof K / sizeof K[0])];
+ }
+ return held;
+}
+static void test_random(int sessions) {
+ int bad = 0;
+ for (int s = 0; s < sessions; s++) {
+ if (s % 50 == 0) forget_all();
+ reset_app();
+ rng = 777u + (uint32_t)s * 7919u;
+ tick = 5, nap = 1, frame_gap = 400;
+ script = script_random;
+ quit_at = 1500 + rnd() % 3000;
+ pt_app_main();
+ pt_file f;
+ uint32_t n;
+ if (file_data(PT_SAVE_NAME, &n) && !save_load(&f)) bad++;
+ }
+ CHECK(!bad, "%d random sessions left a bad save", bad);
+ printf("random keys: %d sessions\n", sessions);
+}
+
+/* ONLY="saves ghost": the parts named, else all */
+static bool want(const char *only, const char *name) {
+ if (!only) return true;
+ size_t n = strlen(name);
+ for (const char *p = only; (p = strstr(p, name)); p += n)
+ if ((p == only || p[-1] == ' ' || p[-1] == ',') && (p[n] == 0 || p[n] == ' ' || p[n] == ',')) return true;
+ return false;
+}
+
int main(int argc, char **argv) {
if (argc < 3) {
- printf("play test|shots|view|film DIR ...\n");
+ printf("play test|shots|view|film|probe DIR ...\n");
return 2;
}
const char *dir = argv[2];
@@ -126,13 +634,15 @@ int main(int argc, char **argv) {
int t = argc > 3 ? atoi(argv[3]) : 0;
uint32_t ms = argc > 4 ? (uint32_t)atoi(argv[4]) : 0;
world_load(t);
+ route_load(t);
car_reset(&car);
chase_reset(&chase, car.pos, car.rot);
for (uint32_t f = 0; f < ms; f++) {
- car_step(&car, route_input(&pt_routes[t], f));
+ car_step(&car, route_keys(f));
if (f % 33 == 0) chase_update(&chase, &cam, 0.033f, car.pos, car.rot, car.speed);
}
sv.p.track = (uint8_t)t;
+ rd_detail = argc > 5 ? atoi(argv[5]) : 3;
render_race(&car, &chase, 0.0f);
printf("track %s frame %u pos %.2f %.2f %.2f speed %.1f cp %d tris %d%s\n", pt_tracks[t].name, car.frames,
(double)car.pos.x, (double)car.pos.y, (double)car.pos.z, (double)car.speed, car.next_cp, rd_tris,
@@ -140,16 +650,66 @@ int main(int argc, char **argv) {
shot(dir, "view");
return 0;
}
+ if (!strcmp(argv[1], "probe")) {
+ int t = argc > 3 ? atoi(argv[3]) : 0;
+ uint32_t ms = argc > 4 ? (uint32_t)atoi(argv[4]) : 0;
+ unsigned keys = argc > 5 ? (unsigned)atoi(argv[5]) : IN_UP;
+ world_load(t);
+ route_load(t);
+ car_reset(&car);
+ for (uint32_t f = 0; f < ms; f++) car_step(&car, route_keys(f));
+ for (int f = 0; f <= 1000; f++) {
+ if (f % 100 == 0) {
+ printf("f %u pos %.2f %.2f %.2f v %.2f %.2f %.2f ang %.2f %.2f %.2f spd %.1f cp %d cache %d up %.2f %.2f %.2f\n",
+ car.frames, (double)car.pos.x, (double)car.pos.y, (double)car.pos.z, (double)car.vel.x, (double)car.vel.y,
+ (double)car.vel.z, (double)car.ang.x, (double)car.ang.y, (double)car.ang.z, (double)car.speed, car.next_cp,
+ car.ncache, (double)car.basis.r[0].y, (double)car.basis.r[1].y, (double)car.basis.r[2].y);
+ for (int i = 0; i < 4; i++)
+ printf(" w%d contact %d susp %.3f force %.0f skid %.2f\n", i, car.w[i].contact, (double)car.w[i].susp,
+ (double)car.w[i].force, (double)car.w[i].skid);
+ }
+ car_step(&car, keys);
+ }
+ return 0;
+ }
+ if (!strcmp(argv[1], "cam")) {
+ /* a picture from anywhere: play cam DIR T X Y Z TX TY TZ */
+ int t = atoi(argv[3]);
+ v3 p = V3((float)atof(argv[4]), (float)atof(argv[5]), (float)atof(argv[6]));
+ v3 at = V3((float)atof(argv[7]), (float)atof(argv[8]), (float)atof(argv[9]));
+ world_load(t);
+ camera_t c;
+ c.pos = p, c.fwd = vnorm(vsub(at, p));
+ c.right = vnorm(vcross(c.fwd, V3(0.0f, 1.0f, 0.0f)));
+ c.up = vcross(c.right, c.fwd);
+ c.fov = 70.0f;
+ rd_detail = 3;
+ rd_begin(&c, W.t->env, W.t->sun, false);
+ rd_track();
+ color_t *old = gfx_buf;
+ static color_t st[320 * 24];
+ gfx_buf = st;
+ for (int y = 0; y < 240; y += 24) {
+ gfx_y0 = y, gfx_y1 = y + 24;
+ rd_draw();
+ plat_push(0, y, 320, 24, st);
+ }
+ gfx_buf = old;
+ shot(dir, "cam");
+ return 0;
+ }
if (!strcmp(argv[1], "film")) {
int t = argc > 3 ? atoi(argv[3]) : 0;
uint32_t ms = argc > 4 ? (uint32_t)atoi(argv[4]) : 10000, step = argc > 5 ? (uint32_t)atoi(argv[5]) : 40;
world_load(t);
+ route_load(t);
car_reset(&car);
chase_reset(&chase, car.pos, car.rot);
sv.p.track = (uint8_t)t;
+ rd_detail = 3;
int n = 0;
for (uint32_t f = 0; f < ms; f++) {
- car_step(&car, route_input(&pt_routes[t], f));
+ car_step(&car, route_keys(f));
if (f % step == 0) {
render_race(&car, &chase, (float)step / 1000.0f);
char path[512];
@@ -163,15 +723,20 @@ int main(int argc, char **argv) {
return 0;
}
if (!strcmp(argv[1], "test")) {
- for (int t = 0; t < PT_NTRACKS; t++) {
- uint32_t h;
- uint32_t fin = drive(t, &pt_routes[t], 600000, &h);
- printf("%-9s %s %u ms, hash %08x\n", pt_tracks[t].name, fin ? "finished in" : "NOT FINISHED", fin, h);
- CHECK(fin, "%s not finished", pt_tracks[t].name);
- }
- printf(fails ? "%d FAILED\n" : "all good\n", fails);
+ const char *only = getenv("ONLY");
+#define PART(name, call) if (want(only, name)) { call; fflush(stdout); }
+ PART("runs", test_runs());
+ PART("checkpoints", test_checkpoints());
+ PART("respawn", test_respawn());
+ PART("ghost", test_ghost());
+ PART("saves", test_saves());
+ PART("ghostfile", test_ghost_file());
+ PART("restart", test_restart_home());
+ PART("app", test_app());
+ PART("random", test_random(300));
+ if (fails) printf("%d of %d checks FAILED\n", fails, checks);
+ else printf("all good (%d checks)\n", checks);
return fails ? 1 : 0;
}
- (void)forget, (void)file_data, (void)storage_full, (void)quit_at, (void)script, (void)watch;
return 2;
}
diff --git a/games/polytrack/tests/route.c b/games/polytrack/tests/route.c
new file mode 100644
index 00000000..0ed66f96
--- /dev/null
+++ b/games/polytrack/tests/route.c
@@ -0,0 +1,659 @@
+/* Finds a run to the finish of each official track with the game's own physics, for tests/routes.h:
+ *
+ * route [TRACK...] prints the routes (C) of the tracks given, or of all
+ *
+ * How: a map of how far each place is from the next checkpoint along the track (2 m cubes near the track's
+ * surfaces, walked from the checkpoint's box), and a beam search: every 100 ms each of the best few cars tries the
+ * keys, looks half a second ahead, and the ones nearest the next checkpoint go on. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../src/car.h"
+#include "../src/data.h"
+#include "../src/world.h"
+
+#define VOX 2.0f
+#define INF 0xFFFF
+
+typedef struct {
+ int nx, ny, nz;
+ float x0, y0, z0;
+ uint8_t *walk; /* 1: a car can be there */
+ uint8_t *wall; /* 1: a barrier; 2: next to one or to a drop, 3: one further in */
+ uint8_t *cut; /* bits 1, 2, 4: a surface between the cube and the next one in x, y, z */
+ uint16_t *dist[16]; /* to each target: the checkpoint orders, then the finish */
+ int ntarget;
+} field_t;
+
+static field_t F;
+
+static int vidx(int x, int y, int z) { return (z * F.ny + y) * F.nx + x; }
+static bool vox_of(v3 p, int *x, int *y, int *z) {
+ *x = (int)((p.x - F.x0) / VOX), *y = (int)((p.y - F.y0) / VOX), *z = (int)((p.z - F.z0) / VOX);
+ return *x >= 0 && *y >= 0 && *z >= 0 && *x < F.nx && *y < F.ny && *z < F.nz;
+}
+
+/* the cubes a car's centre goes through on a triangle: up to 2.5 m off its face, on its front side only (so the
+ map doesn't go through a thin wall) */
+static void mark_tri(uint8_t *g, v3 a, v3 b, v3 c, v3 n, uint8_t kind) {
+ float l = pt_maxf(vlen(vsub(b, a)), pt_maxf(vlen(vsub(c, a)), vlen(vsub(c, b))));
+ int m = (int)(l / (VOX * 0.5f)) + 1;
+ for (int i = 0; i <= m; i++)
+ for (int j = 0; j <= m - i; j++) {
+ float u = (float)i / (float)m, v = (float)j / (float)m;
+ v3 p = vadd(a, vadd(vmul(vsub(b, a), u), vmul(vsub(c, a), v)));
+ for (int k = 0; k < 3; k++) {
+ int x, y, z;
+ if (vox_of(vadd(p, vmul(n, 0.5f + (float)k)), &x, &y, &z) && g[vidx(x, y, z)] < kind) g[vidx(x, y, z)] = kind;
+ }
+ }
+}
+
+/* parts a car drives on whatever their slope: the wall tracks, loops, tilted roads, slopes to vertical */
+static bool drive_part(int id) {
+ static const uint8_t L[] = {30, 31, 32, 50, 51, 66, 67, 79, 80, 81, 87, 88, 90, 98, 134, 135, 136, 137, 138, 139, 142,
+ 143, 144, 156, 157, 158, 159, 160, 161, 162, 163, 166, 167};
+ for (unsigned i = 0; i < sizeof L; i++)
+ if (L[i] == id) return true;
+ return false;
+}
+
+static void floor_tri(float *g, v3 a, v3 b, v3 c) {
+ float l = pt_maxf(vlen(vsub(b, a)), pt_maxf(vlen(vsub(c, a)), vlen(vsub(c, b))));
+ int n = (int)(l / (VOX * 0.5f)) + 1;
+ for (int i = 0; i <= n; i++)
+ for (int j = 0; j <= n - i; j++) {
+ float u = (float)i / (float)n, v = (float)j / (float)n;
+ v3 p = vadd(a, vadd(vmul(vsub(b, a), u), vmul(vsub(c, a), v)));
+ int x, y, z;
+ if (vox_of(p, &x, &y, &z) && p.y < g[vidx(x, y, z)]) g[vidx(x, y, z)] = p.y;
+ }
+}
+
+static void build_field(void) {
+ const pt_track *t = W.t;
+ v3 lo = V3(1e9f, 1e9f, 1e9f), hi = V3(-1e9f, -1e9f, -1e9f);
+ for (int i = 0; i < t->ninst; i++) {
+ v3 a, b;
+ world_box(world_inst(i), &a, &b);
+ lo = V3(pt_minf(lo.x, a.x), pt_minf(lo.y, a.y), pt_minf(lo.z, a.z));
+ hi = V3(pt_maxf(hi.x, b.x), pt_maxf(hi.y, b.y), pt_maxf(hi.z, b.z));
+ }
+ F.x0 = lo.x - 40.0f, F.y0 = -2.0f, F.z0 = lo.z - 40.0f;
+ F.nx = (int)((hi.x - lo.x + 80.0f) / VOX) + 1;
+ F.ny = (int)((hi.y + 30.0f) / VOX) + 1;
+ F.nz = (int)((hi.z - lo.z + 80.0f) / VOX) + 1;
+ size_t n = (size_t)F.nx * F.ny * F.nz;
+ uint8_t *surf = calloc(n, 1);
+ F.walk = calloc(n, 1);
+ float *floor = malloc(n * sizeof(float));
+ for (size_t i = 0; i < n; i++) floor[i] = 1e9f;
+ v3 *walls = NULL;
+ int nwall = 0, cap = 0;
+ /* every physics triangle */
+ for (int i = 0; i < t->ninst; i++) {
+ const pt_inst *in = world_inst(i);
+ const pt_part *p = &pt_parts[in->part];
+ uint32_t mask = p->simple ? pt_masks[t->mask + in->mask] : 0xFFFFFFFFu;
+ for (int k = 0; k < p->ncol; k++) {
+ if (k < 32 && !(mask & (1u << k))) continue;
+ const uint16_t *tr = &pt_ctri[(p->col + k) * 3];
+ v3 v[3];
+ for (int j = 0; j < 3; j++) {
+ const int16_t *q = &pt_pool[(p->vbase + tr[j]) * 3];
+ v[j] = world_apply(in, V3((float)q[0] / PT_FIX, (float)q[1] / PT_FIX, (float)q[2] / PT_FIX));
+ }
+ v3 nn = vnorm(vcross(vsub(v[1], v[0]), vsub(v[2], v[0])));
+ bool dp = drive_part(pt_part_id[in->part]);
+ /* flat floors (1), and what can be climbed: slopes, walls, loops (2) */
+ if (nn.y > 0.5f || dp) {
+ mark_tri(surf, v[0], v[1], v[2], nn, nn.y >= 0.96f && !dp ? 1 : 2);
+ floor_tri(floor, v[0], v[1], v[2]);
+ } else if (nn.y > -0.5f) {
+ if (nwall + 3 > cap) cap = cap ? cap * 2 : 65536, walls = realloc(walls, sizeof(v3) * (size_t)cap);
+ walls[nwall++] = v[0], walls[nwall++] = v[1], walls[nwall++] = v[2];
+ }
+ }
+ }
+ /* near a surface (and the ground everywhere) */
+ for (int z = 0; z < F.nz; z++)
+ for (int y = 0; y < F.ny; y++)
+ for (int x = 0; x < F.nx; x++) {
+ F.walk[vidx(x, y, z)] = (uint8_t)(surf[vidx(x, y, z)] + 1); /* 1 air, 2 over a floor, 3 by a slope or a wall */
+ }
+ /* not inside blocks */
+ for (int i = 0; i < t->ninst; i++) {
+ const pt_inst *in = world_inst(i);
+ int id = pt_part_id[in->part];
+ if (id != 29 && id != 53 && id != 54) continue; /* Block, HalfBlock, QuarterBlock */
+ v3 a, b;
+ world_box(in, &a, &b);
+ for (float zz = a.z + 1.0f; zz < b.z - 0.5f; zz += VOX)
+ for (float yy = a.y + 1.0f; yy < b.y - 0.5f; yy += VOX)
+ for (float xx = a.x + 1.0f; xx < b.x - 0.5f; xx += VOX) {
+ int x, y, z;
+ if (vox_of(V3(xx, yy, zz), &x, &y, &z)) F.walk[vidx(x, y, z)] = 0;
+ }
+ }
+ /* barriers: walls standing on a floor, where a car can't go; next to them a cube costs more */
+ F.wall = calloc(n, 1);
+ for (int w = 0; w < nwall; w += 3) {
+ v3 a = walls[w], b = walls[w + 1], c = walls[w + 2];
+ float l = pt_maxf(vlen(vsub(b, a)), pt_maxf(vlen(vsub(c, a)), vlen(vsub(c, b))));
+ int m = (int)(l / (VOX * 0.25f)) + 1;
+ for (int i = 0; i <= m; i++)
+ for (int j = 0; j <= m - i; j++) {
+ float u = (float)i / (float)m, v = (float)j / (float)m;
+ v3 p = vadd(a, vadd(vmul(vsub(b, a), u), vmul(vsub(c, a), v)));
+ int x, y, z;
+ if (!vox_of(p, &x, &y, &z)) continue;
+ int k = vidx(x, y, z);
+ bool on = floor[k] < p.y - 0.25f || (y > 0 && floor[vidx(x, y - 1, z)] < p.y - 0.25f);
+ if (!on && p.y > 0.25f && p.y < VOX) on = true; /* (the ground) */
+ if (on) F.wall[k] = 1;
+ }
+ }
+ for (size_t i = 0; i < n; i++)
+ if (F.wall[i] == 1) F.walk[i] = 0;
+ /* edges: next to a barrier or a drop (2), and one further in (3); the way keeps off them */
+ for (size_t i = 0; i < n; i++) F.wall[i] = F.wall[i] == 1;
+ for (int pass = 2; pass <= 3; pass++)
+ for (int z = 1; z < F.nz - 1; z++)
+ for (int y = 0; y < F.ny; y++)
+ for (int x = 1; x < F.nx - 1; x++) {
+ int i = vidx(x, y, z);
+ if (F.walk[i] < 2 || F.wall[i]) continue;
+ int nb[4] = {i - 1, i + 1, vidx(x, y, z - 1), vidx(x, y, z + 1)};
+ for (int k = 0; k < 4; k++) {
+ int j = nb[k];
+ bool edge = pass == 2 ? F.walk[j] < 2 : F.wall[j] == 2;
+ if (edge) F.wall[i] = (uint8_t)pass;
+ }
+ }
+ /* surfaces between neighbours (so the way doesn't pass through a thin wall or floor) */
+ F.cut = calloc(n, 1);
+ static wtri near[PT_CACHE * 8];
+ for (int z = 0; z < F.nz - 1; z++)
+ for (int y = 0; y < F.ny - 1; y++)
+ for (int x = 0; x < F.nx - 1; x++) {
+ int i = vidx(x, y, z);
+ int nb[3] = {i + 1, vidx(x, y + 1, z), vidx(x, y, z + 1)};
+ bool any = false;
+ for (int k = 0; k < 3; k++) any |= F.walk[i] >= 2 || F.walk[nb[k]] >= 2;
+ if (!any || !F.walk[i]) continue;
+ v3 a = V3(F.x0 + ((float)x + 0.5f) * VOX, F.y0 + ((float)y + 0.5f) * VOX, F.z0 + ((float)z + 0.5f) * VOX);
+ int m = world_gather(vsub(a, V3(0.1f, 0.1f, 0.1f)), vadd(a, V3(VOX + 0.1f, VOX + 0.1f, VOX + 0.1f)), near,
+ PT_CACHE * 8);
+ for (int k = 0; k < 3; k++) {
+ if (!F.walk[nb[k]] || (F.walk[i] < 2 && F.walk[nb[k]] < 2)) continue;
+ v3 b = vadd(a, V3(k == 0 ? VOX : 0.0f, k == 1 ? VOX : 0.0f, k == 2 ? VOX : 0.0f));
+ for (int q = 0; q < m; q++) {
+ const wtri *w = &near[q];
+ float da = vdot(w->n, a) - w->d, db = vdot(w->n, b) - w->d;
+ if ((da > 0.0f) == (db > 0.0f)) continue;
+ float u = da / (da - db);
+ v3 p = vadd(a, vmul(vsub(b, a), u));
+ if (vdot(vcross(vsub(w->b, w->a), vsub(p, w->a)), w->n) < 0.0f) continue;
+ if (vdot(vcross(vsub(w->c, w->b), vsub(p, w->b)), w->n) < 0.0f) continue;
+ if (vdot(vcross(vsub(w->a, w->c), vsub(p, w->c)), w->n) < 0.0f) continue;
+ F.cut[i] |= (uint8_t)(1 << k);
+ break;
+ }
+ }
+ }
+ free(walls);
+ free(floor);
+ free(surf);
+}
+
+/* Dijkstra with a bucket queue: near a drivable surface a cube costs 2; in the air 6 across, 3 down and 24 up
+ (a jump goes across or down, not up) */
+#define NB 32
+#define VCOST 1 /* up or down a slope: about free, the way across it counts */
+static int *bucket[NB], bcount[NB], bcap[NB];
+static void push(int b, int i) {
+ if (bcount[b] == bcap[b]) bcap[b] = bcap[b] ? bcap[b] * 2 : 4096, bucket[b] = realloc(bucket[b], sizeof(int) * (size_t)bcap[b]);
+ bucket[b][bcount[b]++] = i;
+}
+static void bfs(uint16_t *d, v3 lo, v3 hi) {
+ size_t n = (size_t)F.nx * F.ny * F.nz;
+ for (size_t i = 0; i < n; i++) d[i] = INF;
+ for (int b = 0; b < NB; b++) bcount[b] = 0;
+ for (float zz = lo.z; zz <= hi.z + 0.01f; zz += 1.0f)
+ for (float yy = lo.y; yy <= hi.y + 0.01f; yy += 1.0f)
+ for (float xx = lo.x; xx <= hi.x + 0.01f; xx += 1.0f) {
+ int x, y, z;
+ if (!vox_of(V3(xx, yy, zz), &x, &y, &z)) continue;
+ int i = vidx(x, y, z);
+ if (d[i] == INF) d[i] = 0, push(0, i);
+ }
+ /* (walked backwards from the target: a step from j to i costs what going from i to j would) */
+ for (uint32_t cur = 0, left = 1; left && cur < 60000; cur++) {
+ int b = (int)(cur % NB);
+ left = 0;
+ for (int k = 0; k < NB; k++) left += (uint32_t)bcount[k];
+ while (bcount[b]) {
+ int i = bucket[b][--bcount[b]];
+ if (d[i] != cur) continue;
+ int x = i % F.nx, y = (i / F.nx) % F.ny, z = i / (F.nx * F.ny);
+ static const int D[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
+ for (int k = 0; k < 6; k++) {
+ int a = x + D[k][0], bb = y + D[k][1], c = z + D[k][2];
+ if (a < 0 || bb < 0 || c < 0 || a >= F.nx || bb >= F.ny || c >= F.nz) continue;
+ int j = vidx(a, bb, c);
+ int w = F.walk[j];
+ if (!w) continue;
+ /* (through a surface: no) */
+ int lo = j < i ? j : i, axis = k / 2;
+ if (F.cut[lo] & (1 << axis)) continue;
+ /* the car would go from j to i: up when i is above j */
+ int cost;
+ int wi = F.walk[i];
+ if (D[k][1] == -1) {
+ /* i above j: up only along a slope or a wall */
+ if (wi != 3 && w != 3) continue;
+ cost = wi == 3 && w == 3 ? VCOST : 2;
+ } else if (D[k][1] == 1 && wi == 3 && w == 3) cost = VCOST;
+ else if (w >= 2 && wi >= 2) cost = 2;
+ else if (D[k][1] == 1) cost = 3; /* down */
+ else cost = 6;
+ if (F.wall[j] == 2) cost += 4;
+ else if (F.wall[j] == 3) cost += 2;
+ uint32_t nd = cur + (uint32_t)cost;
+ if (nd < d[j]) d[j] = (uint16_t)nd, push((int)(nd % NB), j);
+ }
+ }
+ }
+}
+
+static void build_targets(void) {
+ const pt_track *t = W.t;
+ size_t n = (size_t)F.nx * F.ny * F.nz;
+ int no = car_checkpoints();
+ F.ntarget = no + 1;
+ /* the checkpoint detectors, by order */
+ detector_t d;
+ int k = 0;
+ uint16_t last = 0xFFFF;
+ int target = -1;
+ for (int i = 0; i < t->ncp; i++) {
+ world_detector(i, &d);
+ if (d.order != last) target++, last = d.order, F.dist[target] = malloc(n * 2), k = 0;
+ /* (alternatives share a map: the nearest box) */
+ if (k == 0) bfs(F.dist[target], d.lo, d.hi);
+ else {
+ uint16_t *tmp = malloc(n * 2);
+ bfs(tmp, d.lo, d.hi);
+ for (size_t j = 0; j < n; j++)
+ if (tmp[j] < F.dist[target][j]) F.dist[target][j] = tmp[j];
+ free(tmp);
+ }
+ k++;
+ }
+ F.dist[no] = malloc(n * 2);
+ for (int i = 0; i < t->nfin; i++) {
+ world_detector(t->ncp + i, &d);
+ if (i == 0) bfs(F.dist[no], d.lo, d.hi);
+ else {
+ uint16_t *tmp = malloc(n * 2);
+ bfs(tmp, d.lo, d.hi);
+ for (size_t j = 0; j < n; j++)
+ if (tmp[j] < F.dist[no][j]) F.dist[no][j] = tmp[j];
+ free(tmp);
+ }
+ }
+}
+
+static void free_field(void) {
+ free(F.walk);
+ free(F.cut);
+ free(F.wall);
+ for (int i = 0; i < F.ntarget; i++) free(F.dist[i]);
+}
+
+/* the car's cube on the map: a metre up from it (its own up), else two, else just above it; -1 when off the map */
+static int car_vox(const car_t *c, int k) {
+ static const float UP[3] = {1.0f, 2.0f, 0.4f};
+ v3 up = mcolumn(&c->basis, 1);
+ for (int i = 0; i < 3; i++) {
+ int x, y, z;
+ if (!vox_of(vadd(c->pos, vmul(up, UP[i])), &x, &y, &z)) continue;
+ int v = vidx(x, y, z);
+ if (F.dist[k][v] != INF) return v;
+ }
+ return -1;
+}
+
+/* how far the car is from its next target (in map units, about metres; 1e5 when lost) */
+static float dist_of(const car_t *c) {
+ int k = c->next_cp;
+ if (k >= F.ntarget) return 0.0f;
+ int v = car_vox(c, k);
+ return v < 0 ? 1e5f : (float)F.dist[k][v];
+}
+
+static double score(const car_t *c) {
+ if (c->finish) return 1e12 - (double)c->finish;
+ double s = (double)c->next_cp * 1e7 - (double)dist_of(c) * 1000.0;
+ s += (double)pt_absf(c->speed) * 2.0;
+ if (c->pos.y < -3.0f) s -= 1e6;
+ return s;
+}
+
+/* ------------------------------------------------------------------ a driver that follows the map */
+/* the place L cubes further down the map from the car (towards target k) */
+static v3 ahead(int k, const car_t *c, int L) {
+ int v = car_vox(c, k);
+ if (v < 0) return c->pos;
+ int x = v % F.nx, y = (v / F.nx) % F.ny, z = v / (F.nx * F.ny);
+ uint16_t best = F.dist[k][v];
+ for (int s = 0; s < L && best > 0; s++) {
+ int nx = x, ny = y, nz = z;
+ for (int dz = -1; dz <= 1; dz++)
+ for (int dy = -1; dy <= 1; dy++)
+ for (int dx = -1; dx <= 1; dx++) {
+ int a = x + dx, b = y + dy, cc = z + dz;
+ if (a < 0 || b < 0 || cc < 0 || a >= F.nx || b >= F.ny || cc >= F.nz) continue;
+ uint16_t d = F.dist[k][vidx(a, b, cc)];
+ if (d < best) best = d, nx = a, ny = b, nz = cc;
+ }
+ if (nx == x && ny == y && nz == z) break;
+ x = nx, y = ny, z = nz;
+ }
+ return V3(F.x0 + ((float)x + 0.5f) * VOX, F.y0 + ((float)y + 0.5f) * VOX, F.z0 + ((float)z + 0.5f) * VOX);
+}
+
+/* the keys for the next 10 ms: steer towards the place L cubes ahead, gas up to vmax km/h */
+static unsigned pilot(const car_t *c, int L, float vmax) {
+ int k = c->next_cp < F.ntarget ? c->next_cp : F.ntarget - 1;
+ v3 d = mcol(&c->basis, vsub(ahead(k, c, L), c->pos));
+ float ang = pt_atan2f(d.x, d.z);
+ float want = pt_clampf(ang * 2.5f, -1.0f, 1.0f);
+ unsigned keys = 0;
+ if (want > c->steer_in + 0.05f) keys |= IN_LEFT;
+ else if (want < c->steer_in - 0.05f) keys |= IN_RIGHT;
+ else if (want > 0.3f) keys |= IN_LEFT;
+ else if (want < -0.3f) keys |= IN_RIGHT;
+ if (c->speed < vmax) keys |= IN_UP;
+ else if (c->speed > vmax + 20.0f) keys |= IN_DOWN;
+ return keys;
+}
+
+/* ------------------------------------------------------------------ the search */
+#define SLOT 10 /* ms a key is held, at least */
+#define STEP 10 /* slots a try lasts */
+#define MAXN 4000000
+typedef struct {
+ int32_t parent;
+ uint8_t keys[STEP];
+} node_t;
+static node_t *nodes;
+static int nnodes;
+typedef struct {
+ car_t car;
+ int32_t node;
+} beam_t;
+static beam_t beam[1];
+
+/* the tries: the driver looking near, far or between, at full speed or slower; and keys held */
+typedef struct { int L; float vmax; uint8_t keys; } action_t;
+static const action_t ACTIONS[] = {
+ {4, 999.0f, 0}, {7, 999.0f, 0}, {11, 999.0f, 0}, {16, 999.0f, 0}, {22, 999.0f, 0}, {8, 60.0f, 0}, {10, 110.0f, 0},
+ {0, 0.0f, IN_UP}, {0, 0.0f, IN_UP | IN_LEFT}, {0, 0.0f, IN_UP | IN_RIGHT}, {0, 0.0f, 0}, {0, 0.0f, IN_DOWN},
+ {0, 0.0f, IN_DOWN | IN_LEFT}, {0, 0.0f, IN_DOWN | IN_RIGHT},
+};
+#define NACT ((int)(sizeof ACTIONS / sizeof ACTIONS[0]))
+
+/* a try: STEP slots; writes the keys used */
+static void act(car_t *c, const action_t *a, uint8_t *keys) {
+ for (int s = 0; s < STEP; s++) {
+ unsigned k = a->L ? pilot(c, a->L, a->vmax) : a->keys;
+ if (keys) keys[s] = (uint8_t)k;
+ for (int i = 0; i < SLOT && !c->finish; i++) car_step(c, k);
+ }
+}
+
+/* the archive */
+#define MAXC 8000
+typedef struct {
+ car_t car;
+ int32_t node;
+ int32_t tries;
+ uint32_t key;
+ uint32_t frames;
+} cell_t;
+static uint32_t cell_key(const car_t *c) {
+ int d = (int)(dist_of(c) / 4.0f);
+ return (uint32_t)c->next_cp << 16 | (uint32_t)(d < 65535 ? d : 65535);
+}
+static double progress(const cell_t *e) { return (double)(e->key >> 16) * 1e6 - (double)(e->key & 0xFFFF); }
+static uint32_t lcg(uint32_t *s) { return *s = *s * 1103515245u + 12345u, *s >> 8; }
+
+static bool route(int t, FILE *out) {
+ world_load(t);
+ static car_t start;
+ car_reset(&start);
+ build_field();
+ build_targets();
+ if (getenv("TRACE")) {
+ /* the way down the map from a point: TRACE="target x y z" */
+ int k;
+ float px, py, pz;
+ sscanf(getenv("TRACE"), "%d %f %f %f", &k, &px, &py, &pz);
+ int x, y, z;
+ vox_of(V3(px, py, pz), &x, &y, &z);
+ for (int step = 0; step < 400; step++) {
+ int i = vidx(x, y, z);
+ uint16_t best = F.dist[k][i];
+ int bx = x, by = y, bz = z;
+ for (int dz = -1; dz <= 1; dz++)
+ for (int dy = -1; dy <= 1; dy++)
+ for (int dx = -1; dx <= 1; dx++) {
+ int a = x + dx, b = y + dy, c = z + dz;
+ if (a < 0 || b < 0 || c < 0 || a >= F.nx || b >= F.ny || c >= F.nz) continue;
+ uint16_t v = F.dist[k][vidx(a, b, c)];
+ if (v < best) best = v, bx = a, by = b, bz = c;
+ }
+ if (step % 5 == 0 || best == 0)
+ fprintf(stderr, " %3d: %.0f %.0f %.0f dist %u walk %d\n", step, (double)(F.x0 + x * VOX), (double)(F.y0 + y * VOX),
+ (double)(F.z0 + z * VOX), F.dist[k][i], F.walk[i]);
+ if (best == 0 || (bx == x && by == y && bz == z)) break;
+ x = bx, y = by, z = bz;
+ }
+ return false;
+ }
+ if (getenv("FIELD_DUMP")) {
+ int k = atoi(getenv("FIELD_DUMP"));
+ FILE *f = fopen("field.ppm", "wb");
+ fprintf(f, "P6 %d %d 255\n", F.nx, F.nz);
+ for (int z = 0; z < F.nz; z++)
+ for (int x = 0; x < F.nx; x++) {
+ uint16_t m = INF;
+ int wy = 0;
+ for (int y = 0; y < F.ny; y++) {
+ uint16_t v = F.dist[k][vidx(x, y, z)];
+ if (v < m) m = v, wy = y;
+ }
+ int walkable = 0;
+ for (int y = 0; y < F.ny; y++) walkable |= F.walk[vidx(x, y, z)] == 2 && F.y0 + y * VOX > 4.0f;
+ int c = m == INF ? 0 : 255 - (m / 8) % 200;
+ fputc(m == INF ? 80 : c, f), fputc(m == INF ? 0 : (walkable ? 255 : c / 2), f), fputc(m == INF ? 0 : wy * 4 % 256, f);
+ }
+ fclose(f);
+ fprintf(stderr, "field %d: origin %.0f %.0f size %d x %d\n", k, (double)F.x0, (double)F.z0, F.nx, F.nz);
+ return false;
+ }
+ if (getenv("PILOT")) {
+ /* the driver alone: PILOT="L vmax" */
+ int L = 12;
+ float vmax = 999.0f;
+ sscanf(getenv("PILOT"), "%d %f", &L, &vmax);
+ static car_t c;
+ c = start;
+ int until = getenv("UNTIL") ? atoi(getenv("UNTIL")) : 120000;
+ for (int f = 0; f < until && !c.finish; f += SLOT) {
+ unsigned k = pilot(&c, L, vmax);
+ for (int i = 0; i < SLOT; i++) car_step(&c, k);
+ if (getenv("PV2") && f >= atoi(getenv("PV2")) && f < atoi(getenv("PV2")) + 500)
+ fprintf(stderr, " f %d pos %.2f %.2f %.2f vel %.1f %.1f %.1f cache %d imp %.0f wheels %d%d%d%d up %.2f %.2f %.2f\n", f,
+ (double)c.pos.x, (double)c.pos.y, (double)c.pos.z, (double)c.vel.x, (double)c.vel.y, (double)c.vel.z, c.ncache,
+ (double)c.impulse, c.w[0].contact, c.w[1].contact, c.w[2].contact, c.w[3].contact, (double)c.basis.r[0].y,
+ (double)c.basis.r[1].y, (double)c.basis.r[2].y);
+ if (getenv("PV") && f < atoi(getenv("PV")) && f % 50 == 0) {
+ v3 tg = ahead(c.next_cp, &c, L);
+ v3 d = mcol(&c.basis, vsub(tg, c.pos));
+ v3 fw = mcolumn(&c.basis, 2);
+ fprintf(stderr, " f %d pos %.1f %.1f fwd %.2f %.2f tgt %.1f %.1f local %.1f %.1f keys %u steer %.2f spd %.0f\n", f,
+ (double)c.pos.x, (double)c.pos.z, (double)fw.x, (double)fw.z, (double)tg.x, (double)tg.z, (double)d.x,
+ (double)d.z, k, (double)c.steer_in, (double)c.speed);
+ }
+ if (f % 1000 == 0)
+ fprintf(stderr, " %3d s: %7.1f %5.1f %7.1f cp %d dist %.0f speed %.0f\n", f / 1000, (double)c.pos.x, (double)c.pos.y,
+ (double)c.pos.z, c.next_cp, (double)dist_of(&c), (double)c.speed);
+ }
+ fprintf(stderr, " finish %u; up %.2f %.2f %.2f fwd %.2f %.2f %.2f wheels %d%d%d%d\n", c.finish, (double)c.basis.r[0].y,
+ (double)c.basis.r[1].y, (double)c.basis.r[2].y, (double)c.basis.r[0].z, (double)c.basis.r[1].z,
+ (double)c.basis.r[2].z, c.w[0].contact, c.w[1].contact, c.w[2].contact, c.w[3].contact);
+ free_field();
+ return c.finish != 0;
+ }
+ /* Go-Explore: an archive of cars, one per stretch of the way (the next checkpoint, the distance left in 4 m
+ steps), the quickest there kept; again and again a car is taken from near the front, the less tried first, and
+ driven on a while with the driver or keys held; new stretches go in the archive */
+ nodes = malloc(sizeof(node_t) * MAXN);
+ nnodes = 1;
+ nodes[0].parent = -1;
+ int ncell = 0;
+ static cell_t cells[MAXC];
+ cells[ncell++] = (cell_t){start, 0, 0, cell_key(&start), 0};
+ int found = -1;
+ uint32_t seed = 12345u + (uint32_t)t * 7919u;
+ int tries = getenv("TRIES") ? atoi(getenv("TRIES")) : 200000;
+ int best = 0;
+ for (int it = 0; it < tries && found < 0 && nnodes + 64 < MAXN; it++) {
+ /* the car to go on from: one of the 24 furthest, weighted by how little it was tried; now and then any */
+ int pick;
+ if (lcg(&seed) % 10 == 0) pick = (int)(lcg(&seed) % (uint32_t)ncell);
+ else {
+ static int top[24];
+ int nt = 0;
+ for (int i = 0; i < ncell; i++) {
+ int j;
+ if (nt < 24) j = nt++;
+ else if (progress(&cells[i]) > progress(&cells[top[23]])) j = 23;
+ else continue;
+ top[j] = i;
+ while (j > 0 && progress(&cells[top[j]]) > progress(&cells[top[j - 1]])) {
+ int tmp = top[j];
+ top[j] = top[j - 1], top[j - 1] = tmp;
+ j--;
+ }
+ }
+ double wsum = 0.0;
+ for (int k = 0; k < nt; k++) wsum += 1.0 / (1.0 + cells[top[k]].tries);
+ double r = (double)(lcg(&seed) % 1000000u) / 1000000.0 * wsum;
+ pick = top[nt - 1];
+ for (int k = 0; k < nt; k++) {
+ r -= 1.0 / (1.0 + cells[top[k]].tries);
+ if (r <= 0.0) {
+ pick = top[k];
+ break;
+ }
+ }
+ }
+ cells[pick].tries++;
+ static car_t c;
+ c = cells[pick].car;
+ car_cache_rebuild(&c);
+ int node = cells[pick].node;
+ int steps = 5 + (int)(lcg(&seed) % 26u), a = (int)(lcg(&seed) % (uint32_t)NACT);
+ for (int s = 0; s < steps && !c.finish; s++) {
+ if (lcg(&seed) % 10 >= 6) {
+ /* (the driver more often than held keys) */
+ a = (int)(lcg(&seed) % (uint32_t)(NACT + 7));
+ if (a >= NACT) a = a - NACT;
+ }
+ nodes[nnodes].parent = node;
+ act(&c, &ACTIONS[a], nodes[nnodes].keys);
+ node = nnodes++;
+ if (node == 1 && !(nodes[1].keys[0] & (IN_UP | IN_DOWN | IN_LEFT | IN_RIGHT))) break; /* (the first keys start the clock) */
+ if (c.finish) {
+ found = node;
+ cells[0].car = c, cells[0].node = node;
+ best = 0;
+ break;
+ }
+ float d = dist_of(&c);
+ if (d >= 1e5f || c.pos.y < -3.0f) break; /* lost */
+ uint32_t key = cell_key(&c);
+ int k = -1;
+ for (int i = 0; i < ncell; i++)
+ if (cells[i].key == key) {
+ k = i;
+ break;
+ }
+ if (k < 0 && ncell < MAXC) k = ncell++, cells[k].tries = 0, cells[k].key = key, cells[k].frames = 0xFFFFFFFFu;
+ if (k >= 0 && c.frames < cells[k].frames) {
+ cells[k].car = c, cells[k].node = node, cells[k].frames = c.frames;
+ if (progress(&cells[k]) > progress(&cells[best])) best = k;
+ }
+ }
+ if (it % 10000 == 0)
+ fprintf(stderr, " %s try %5d: %d stretches, furthest cp %d/%d dist %.0f at %.1f s\n", pt_tracks[t].name, it, ncell,
+ cells[best].car.next_cp, F.ntarget - 1, (double)dist_of(&cells[best].car),
+ (double)cells[best].car.frames / 1000.0);
+ }
+ beam[0].car = found >= 0 ? cells[0].car : cells[best].car;
+ beam[0].node = found >= 0 ? found : cells[best].node;
+ bool ok = found >= 0;
+ if (ok || getenv("PARTIAL")) {
+ /* the keys, from the start, a letter per slot */
+ int n = 0;
+ for (int i = beam[0].node; i > 0; i = nodes[i].parent) n++;
+ int ns = n * STEP;
+ uint8_t *k = malloc((size_t)ns);
+ int j = n;
+ for (int i = beam[0].node; i > 0; i = nodes[i].parent) memcpy(&k[--j * STEP], nodes[i].keys, STEP);
+ /* (the same keys from the start must come to the same car) */
+ static car_t chk;
+ chk = start;
+ chk.cache_ok = 0;
+ for (int i = 0; i < ns && !chk.finish; i++)
+ for (int f = 0; f < SLOT && !chk.finish; f++) car_step(&chk, k[i]);
+ fprintf(stderr, " replay: %08x %.3f %.3f %.3f, search: %08x %.3f %.3f %.3f\n", car_hash(&chk), (double)chk.pos.x,
+ (double)chk.pos.y, (double)chk.pos.z, car_hash(&beam[0].car), (double)beam[0].car.pos.x,
+ (double)beam[0].car.pos.y, (double)beam[0].car.pos.z);
+ /* (to the finish only) */
+ if (ok) ns = (int)((beam[0].car.finish + SLOT - 1) / SLOT);
+ fprintf(out, " /* %s: %u ms%s */\n \"", pt_tracks[t].name, beam[0].car.finish, ok ? "" : " (not finished)");
+ int col = 3;
+ for (int i = 0; i < ns;) {
+ int r = 1;
+ while (i + r < ns && k[i + r] == k[i]) r++;
+ char buf[16];
+ int len = r > 1 ? snprintf(buf, sizeof buf, "%c%d", (k[i] & IN_RESET ? 'A' : 'a') + (k[i] & 15), r)
+ : snprintf(buf, sizeof buf, "%c", (k[i] & IN_RESET ? 'A' : 'a') + (k[i] & 15));
+ if (col + len > 116) fprintf(out, "\"\n \""), col = 3;
+ fputs(buf, out), col += len;
+ i += r;
+ }
+ fprintf(out, "\",\n");
+ fflush(out);
+ free(k);
+ }
+ free(nodes);
+ free_field();
+ return ok;
+}
+
+int main(int argc, char **argv) {
+ int list[PT_NTRACKS], n = 0;
+ for (int i = 1; i < argc; i++) list[n++] = atoi(argv[i]);
+ if (!n)
+ for (int i = 0; i < PT_NTRACKS; i++) list[n++] = i;
+ for (int i = 0; i < n; i++) {
+ bool ok = route(list[i], stdout);
+ fprintf(stderr, "%s: %s\n", pt_tracks[list[i]].name, ok ? "finished" : "NOT FOUND");
+ }
+ return 0;
+}
diff --git a/games/polytrack/tests/routes.h b/games/polytrack/tests/routes.h
index b471836f..7a748123 100644
--- a/games/polytrack/tests/routes.h
+++ b/games/polytrack/tests/routes.h
@@ -1,14 +1,8 @@
-/* A run to the finish for each official track: the keys held from a frame on (IN_ bits of car.h). Found by
- * tests/route.c, a driver that tries keys ahead with the game's own physics. */
+/* (placeholder until the runs are found) */
#ifndef PT_ROUTES_H
#define PT_ROUTES_H
#include <stdint.h>
-typedef struct { uint32_t frame; uint8_t keys; } route_ev;
-typedef struct { const route_ev *ev; int n; } route_t;
-static const route_ev r_default[] = {{0, 1}};
-static const route_t pt_routes[17] = {
- {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1},
- {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1}, {r_default, 1},
- {r_default, 1}, {r_default, 1}, {r_default, 1},
-};
+static const char *const pt_routes[17] = {"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"};
+static const uint32_t pt_route_ms[17] = {0};
+static const uint32_t pt_route_hash[17] = {0};
#endif