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.

Hollow Knight: no more catching on the ground's seams, as the calculator rounds: the Knight walks on after a drop and slides down walls instead of hanging on them (touching within a thousandth is not a hit, a body only stops against what it rests on, the tilemap's faces are straight runs, lips under the skin are ridden over, slopes climbed); a test lands him a few float steps low everywhere

Mason3631c9b2cb09a07
diff --git a/games/hollowknight/src/phys.c b/games/hollowknight/src/phys.c
index af9b7380..ee93d12f 100644
--- a/games/hollowknight/src/phys.c
+++ b/games/hollowknight/src/phys.c
@@ -10,6 +10,10 @@
#define UNIT (1.0f / 128)
#define CELL 8.0f
#define SKIN 0.01f /* how far a body rests from the ground (Box2D's polygon radius) */
+/* how near counts as just touching: a body resting SKIN from the ground is there only as closely as floats round, and
+ * the calculator rounds otherwise than a computer; within this it neither sinks into the ground's seams (where two of
+ * its pieces meet, every tile's edge) and stops against them, nor rests on the end of one beside it */
+#define TOUCH 0.001f
typedef struct {
int16_t x0, y0, x1, y1; /* (1/128 unit) */
@@ -146,14 +150,27 @@ static int gather(float x0, float y0, float x1, float y1, uint8_t mask, Cand *ou
if (ty0 < 0) ty0 = 0;
if (tx1 >= g_tw) tx1 = g_tw - 1;
if (ty1 >= g_th) ty1 = g_th - 1;
+ /* (each straight run of faces one segment, as the tilemap's edge colliders are chains: no corner inside a wall or a
+ * floor for a body sliding along it to catch on; the runs' ends out here, past where the box can reach) */
+ for (int tx = tx0; tx <= tx1; tx++)
+ for (int side = -1; side <= 1; side += 2) { /* (left faces, right faces) */
+ float X = (float)(side < 0 ? tx : tx + 1);
+ for (int ty = ty0; ty <= ty1 && n < MAX_CAND; ty++) {
+ if (!phys_tile_solid(tx, ty) || phys_tile_solid(tx + side, ty)) continue;
+ int t0 = ty;
+ while (ty < ty1 && phys_tile_solid(tx, ty + 1) && !phys_tile_solid(tx + side, ty + 1)) ty++;
+ out[n++] = (Cand){X, (float)t0, X, (float)(ty + 1), 0};
+ }
+ }
for (int ty = ty0; ty <= ty1; ty++)
- for (int tx = tx0; tx <= tx1; tx++) {
- if (!phys_tile_solid(tx, ty) || n > MAX_CAND - 4) continue;
- float X = (float)tx, Y = (float)ty;
- if (!phys_tile_solid(tx - 1, ty)) out[n++] = (Cand){X, Y, X, Y + 1, 0};
- if (!phys_tile_solid(tx + 1, ty)) out[n++] = (Cand){X + 1, Y, X + 1, Y + 1, 0};
- if (!phys_tile_solid(tx, ty - 1)) out[n++] = (Cand){X, Y, X + 1, Y, 0};
- if (!phys_tile_solid(tx, ty + 1)) out[n++] = (Cand){X, Y + 1, X + 1, Y + 1, 0};
+ for (int side = -1; side <= 1; side += 2) { /* (bottom faces, top faces) */
+ float Y = (float)(side < 0 ? ty : ty + 1);
+ for (int tx = tx0; tx <= tx1 && n < MAX_CAND; tx++) {
+ if (!phys_tile_solid(tx, ty) || phys_tile_solid(tx, ty + side)) continue;
+ int t0 = tx;
+ while (tx < tx1 && phys_tile_solid(tx + 1, ty) && !phys_tile_solid(tx + 1, ty + side)) tx++;
+ out[n++] = (Cand){(float)t0, Y, (float)(tx + 1), Y, 0};
+ }
}
return n;
}
@@ -209,8 +226,10 @@ static bool box_seg(float cx, float cy, float hx, float hy, float dx, float dy,
if (out_hi < pen) pen = out_hi, pnx = ux, pny = uy;
} else
inside = false;
- if (fabsf(v) < 1e-12f) {
- if (c <= lo || c >= hi) return false; /* never meet on this axis */
+ bool touching = c <= lo + TOUCH || c >= hi - TOUCH;
+ if (fabsf(v) < 1e-12f || (touching && fabsf(v) < TOUCH)) {
+ /* (still on this axis, or along it by less than TOUCH where it only touches: sliding along it) */
+ if (touching) return false; /* never meet on this axis (touching at most) */
continue;
}
float ta = (lo - c) / v, tb = (hi - c) / v;
@@ -239,7 +258,8 @@ static bool box_seg(float cx, float cy, float hx, float hy, float dx, float dy,
static void body_box(const Body *b, float *cx, float *cy) { *cx = b->x + b->ox, *cy = b->y + b->oy; }
-/* the colliders the box touches now, with a normal each (towards the box) */
+/* the colliders the box touches now, with a normal each (towards the box): those it rests against (no further than
+ * SKIN, as the steps leave it), not those it only passes near */
static int touching(const Body *b, uint8_t mask, uint8_t *cols, float *nxs, float *nys, int max) {
float cx, cy;
body_box(b, &cx, &cy);
@@ -249,7 +269,7 @@ static int touching(const Body *b, uint8_t mask, uint8_t *cols, float *nxs, floa
for (int k = 0; k < nc; k++) {
const Cand *s = &cand[k];
float toi, nx, ny, depth;
- if (!box_seg(cx, cy, hx, hy, 0, 0, s, &toi, &nx, &ny, &depth)) continue;
+ if (!box_seg(cx, cy, hx, hy, 0, 0, s, &toi, &nx, &ny, &depth) || depth < SKIN - TOUCH) continue;
int i = 0;
while (i < n && cols[i] != s->col) i++;
if (i < n) {
@@ -285,7 +305,7 @@ void body_step(Body *b, float dt) {
body_box(b, &cx, &cy);
float hx = b->hx + SKIN, hy = b->hy + SKIN;
float bx0 = fminf(cx, cx + dx) - hx, bx1 = fmaxf(cx, cx + dx) + hx, by0 = fminf(cy, cy + dy) - hy, by1 = fmaxf(cy, cy + dy) + hy;
- float best = 2, bnx = 0, bny = 0, deep = 0, dnx = 0, dny = 0;
+ float best = 2, bnx = 0, bny = 0, btop = 0, deep = 0, dnx = 0, dny = 0;
int dcol = -1;
bool caught = false; /* (inside a collider whose sides push opposite ways: out of it whole) */
Cand cand[MAX_CAND];
@@ -302,7 +322,7 @@ void body_step(Body *b, float dt) {
continue;
}
if (nx * dx + ny * dy >= 0) continue; /* moving away from it */
- if (toi < best) best = toi, bnx = nx, bny = ny;
+ if (toi < best) best = toi, bnx = nx, bny = ny, btop = fmaxf(s->ay, s->by);
}
#ifdef HOST
if (getenv("PHYSDBG")) fprintf(stderr, "it %d d %.4f,%.4f best %.3f n %.2f,%.2f deep %.4f dn %.2f,%.2f nc %d\n", it, dx, dy, best, bnx, bny, deep, dnx, dny, nc);
@@ -342,6 +362,13 @@ void body_step(Body *b, float dt) {
break;
}
b->x += dx * best, b->y += dy * best;
+ /* (a side no higher above its feet than its skin: up onto it, as a box with Box2D's rounded corners rides over it) */
+ float lip = btop - (b->y + b->oy - b->hy);
+ if (fabsf(bnx) > 0.99f && b->gravity_scale > 0 && b->vy <= 0 && lip > -SKIN && lip <= SKIN) {
+ b->y += lip + SKIN;
+ left *= 1 - best;
+ continue;
+ }
float vn = b->vx * bnx + b->vy * bny;
if (vn < 0) b->vx -= vn * bnx, b->vy -= vn * bny;
left *= 1 - best;
diff --git a/games/hollowknight/tests/check.py b/games/hollowknight/tests/check.py
index 3d398dbc..0a1654c9 100644
--- a/games/hollowknight/tests/check.py
+++ b/games/hollowknight/tests/check.py
@@ -3,6 +3,8 @@
- every room's hazard respawn points: ground under them, in the room
- every gate and door (a door taking his control, as its FSM does): the Knight arrives in the next room, in control, on
the ground, and can slash and move; and spikes there send him back to a good place
+- the ground's seams, as the calculator rounds: landed a few float steps low, the Knight walks off both ways; jumping
+ at a wall, he falls back down
- every arena won: its fight over and the room's battle gates open, also as the Knight comes back and after a save and
a load
- a long walk of made up keys from every room: the Knight never out of the room, without control, wedged, undrawn or
@@ -50,6 +52,8 @@ def main():
results.append(report("hazard respawn points", rc, out, ("BAD",)))
rc, out = play("--gates", "all")
results.append(report("gates", rc, out, ("FAIL",)))
+ rc, out = play("--gates", "seams")
+ results.append(report("seams", rc, out, ("FAIL",)))
with tempfile.TemporaryDirectory() as saves:
rc, out = play("--gates", "arenas", env={"HKSAVES": saves})
results.append(report("arenas", rc, out, ("FAIL",)))
diff --git a/games/hollowknight/tests/play.c b/games/hollowknight/tests/play.c
index 125412d1..6fde74fa 100644
--- a/games/hollowknight/tests/play.c
+++ b/games/hollowknight/tests/play.c
@@ -240,7 +240,106 @@ static int arenas_test(void) {
return fails || !total ? 1 : 0;
}
+/* the ground's seams, as the calculator rounds: from many places in every room the Knight lands on the ground, then is
+ * put a few float steps lower (as near as rounding leaves him), and must walk both ways where no wall stops him; and
+ * jumping at a wall, put a few steps nearer it, he must fall back down, not hang on it */
+static bool wall_ahead(int dir) {
+ const Body *b = &g_hero.body;
+ float x = b->x + b->ox + dir * b->hx, y0 = b->y + b->oy - b->hy, y1 = y0 + 2 * b->hy;
+ for (float y = y0 + 0.08f; y < y1; y += 0.1f)
+ if (phys_ray(x - dir * 0.3f, y, dir, 0, 0.4f, CF_SOLID, NULL)) return true;
+ /* (or the room's end; or a gate he is sent back from: to a room the game leaves out) */
+ if (x < 0.3f || x > g_room.h->w - 0.3f) return true;
+ int n;
+ const Ent *es = room_ents(&n);
+ for (int i = 0; i < n; i++)
+ if (es[i].type == ENT_GATE && !(es[i].flags & G_DOOR))
+ for (int j = i + 1; j < n && es[j].type == ENT_BOX; j++)
+ if (es[j].flags == 0 && x + 0.2f > es[j].x0 && x - 0.2f < es[j].x1 && y1 > es[j].y0 && y0 < es[j].y1) return true;
+ return false;
+}
+static float ulps(float v, int n) { /* (n float steps down, or up) */
+ for (int i = 0; i < abs(n); i++) v = nextafterf(v, n < 0 ? -1e9f : 1e9f);
+ return v;
+}
+static int seams_test(void) {
+ int fails = 0, floors = 0, walls = 0;
+ const Hero *h = &g_hero;
+ shielded = true;
+ for (int r = 0; r < NUM_ROOMS; r++) {
+ if (!room_load(r)) continue;
+ char name[64];
+ snprintf(name, sizeof name, "%s", room_name(r));
+ float w = g_room.h->w, hh = g_room.h->h;
+ for (float x = 2.13f; x < w - 2; x += 1.37f) {
+ float lastgy = -1;
+ for (float y = hh - 2; y > 2; y -= 1.3f) {
+ if (!room_load(r)) continue;
+ PhysHit hit;
+ if (!phys_ray(x, y, 0, -1, 30, CF_TERRAIN, &hit) || hit.ny < 0.99f || hit.y == lastgy) continue;
+ float gy = lastgy = hit.y;
+ bool inside = false;
+ for (float yy = gy + 0.3f; yy < gy + 3; yy += 0.5f)
+ for (float xx = x - 0.6f; xx <= x + 0.61f; xx += 0.3f) inside |= phys_tile_solid((int)floorf(xx), (int)floorf(yy));
+ if (inside || phys_ray(x, gy + 0.05f, 0, 1, 3, CF_SOLID, NULL) || phys_ray(x - 0.4f, gy + 0.5f, 1, 0, 0.8f, CF_SOLID, NULL))
+ continue;
+ game_new();
+ game_enter(r, x, gy + 2.4f, true);
+ int t;
+ for (t = 0; t < 100 && !(h->cs.on_ground && h->state != HS_HARD_LANDING); t++) run_ticks(1, 0);
+ if (!h->cs.on_ground || h->state == HS_NO_INPUT || h->control_relinquished || !h->accepting_input || g_room.id != r)
+ continue;
+ floors++;
+ g_hero.body.y = ulps(g_hero.body.y, -3);
+ for (int dir = 1; dir >= -1; dir -= 2) {
+ float lx = h->body.x;
+ int still = 0;
+ for (t = 0; t < 30; t++) {
+ run_ticks(1, dir > 0 ? K_RIGHT : K_LEFT);
+ if (!h->cs.on_ground || h->state == HS_NO_INPUT || g_room.id != r || game_changing_room() || g_game.freeze_phase) break;
+ still = fabsf(h->body.x - lx) < 0.01f && h->state == HS_RUNNING ? still + 1 : 0;
+ if (still == 3) {
+ if (!wall_ahead(dir) && !h->cs.transitioning)
+ printf("FAIL seams %s: landed at %.2f,%.3f, stuck going %s at %.4f,%.5f\n", name, x, gy, dir > 0 ? "right" : "left",
+ h->body.x, h->body.y), fails++;
+ break;
+ }
+ lx = h->body.x;
+ }
+ run_ticks(5, 0);
+ }
+ /* (a wall a little way off: the Knight jumps at it) */
+ for (int dir = -1; dir <= 1; dir += 2) {
+ PhysHit wh;
+ if (!phys_ray(x, gy + 1.0f, dir, 0, 3, CF_TERRAIN, &wh) || fabsf(wh.nx) < 0.99f || !room_load(r)) continue;
+ game_new();
+ game_enter(r, x, gy + 1.42f, true);
+ run_ticks(30, 0);
+ if (!h->cs.on_ground || h->state == HS_NO_INPUT || h->control_relinquished || g_room.id != r) continue;
+ walls++;
+ int held = 0, worst = 0;
+ bool nudged = false;
+ for (t = 0; t < 120; t++) {
+ run_ticks(1, (dir > 0 ? K_RIGHT : K_LEFT) | (t < 15 || (t > 40 && t < 55) ? K_JUMP : 0));
+ if (g_room.id != r || game_changing_room() || h->cs.transitioning || h->state == HS_NO_INPUT || g_game.freeze_phase) break;
+ if (!nudged && h->cs.touching_wall) g_hero.body.x = ulps(g_hero.body.x, 3 * dir), nudged = true;
+ held = !h->cs.on_ground && fabsf(h->body.vy) < 1e-4f && !h->cs.jumping && h->state != HS_HARD_LANDING ? held + 1 : 0;
+ if (held > worst) worst = held;
+ }
+ if (worst >= 40 && h->body.y + h->body.oy - h->body.hy > 0.5f) /* (not the room's bottom: a pit out of it) */
+ printf("FAIL seams %s: from %.2f,%.3f jumping %s, held up in the air %d ticks at %.4f,%.5f\n", name, x, gy,
+ dir > 0 ? "right" : "left", worst, h->body.x, h->body.y), fails++;
+ }
+ }
+ }
+ }
+ shielded = false;
+ printf("seams: %d landings, %d jumps at walls, %d failing\n", floors, walls, fails);
+ return fails ? 1 : 0;
+}
+
static int gates_test(const char *which) {
+ if (!strcmp(which, "seams")) return seams_test();
if (!strcmp(which, "markers")) return markers_test();
if (!strcmp(which, "arenas")) return arenas_test();
if (!strncmp(which, "wander:", 7)) { /* (wander:ROOM|all:seed:ticks) */