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.

NumBlocks: see the ground from high up; no stall falling through the air

Mason Chen94dfb622643f
diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.c
index b286a2d5..3b8f0af6 100644
--- a/games/numblocks/src/main.c
+++ b/games/numblocks/src/main.c
@@ -95,6 +95,9 @@ void game_init(void) {
if (start_in_world) {
new_world(1, start_seed, 0, false, "New World");
game_time = 1000;
+#ifdef BENCH_FALL
+ pl.mode = 1, pl.y += BENCH_FALL; /* (timing a fall from that high, in Creative: no harm) */
+#endif
loading();
return;
}
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index aa4df2bb..0dfbe058 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -122,7 +122,7 @@ static __attribute__((noinline)) uint16_t sky(float dx, float dy, float dz, floa
}
/* RenderGlobal.renderSky: the sun, the moon in its phase and the stars turn with the
* sky; their textures add light (and rain hides them) */
- if (sun_a > 0) {
+ if (sun_a > 0 && dy > 0) {
float sd = dx * sun_x + dy * sun_y + dz * sun_z;
float tx = dx * sun_tx + dy * sun_ty; /* along the sky's turn; dz across it */
int sp = -1;
@@ -531,7 +531,13 @@ static uint16_t trace(float dx, float dy, float dz) {
float idx = fabsf(ivx), idy = fabsf(ivy), idz = fabsf(ivz);
float tx = cam_fr[0][bx] * ivx, ty = cam_fr[1][by] * ivy, tz = cam_fr[2][bz] * ivz;
float tmax = MAX_T / len, t = 0;
- if (cam_i < 0) return hit_kind = 2, sky(dx, dy, dz, len);
+ /* the camera above the cache (high in the air, the cache kept on the ground): rays going down come
+ * in through its top; any other way out of it, only the sky */
+ int top = trace_top;
+ if (cam_i < 0) {
+ if (dy >= 0 || oy < VCY || cam_x < 0 || cam_x >= VCX || cam_z < 0 || cam_z >= VCZ) return hit_kind = 2, sky(dx, dy, dz, len);
+ if (top < 0) top = VCY;
+ }
int i = cam_i;
Ray R; /* (in the block the camera is in: water...) */
R.dx = dx, R.dy = dy, R.dz = dz, R.len = len, R.ivx = ivx, R.ivy = ivy, R.ivz = ivz, R.wa = 0, R.inside = cam_b;
@@ -546,20 +552,22 @@ static uint16_t trace(float dx, float dy, float dz) {
float tj = 0;
int jf = -1;
int in_liq = cam_liq; /* (the liquid the ray is in: cells of it are skipped) */
- if (trace_top >= 0 && dy < 0) {
- /* start where the ray comes down through plane y = trace_top, in through the top of the cell below */
- float t0 = (trace_top - oy) * ivy;
- if (t0 > 0) {
+ if (top >= 0 && dy < 0) {
+ /* start where the ray comes down through plane y = top, in through the top of the cell below */
+ float t0 = (top - oy) * ivy;
+ if (t0 > 0 || cam_i < 0) {
if (t0 > tmax) goto out;
- int x0 = (int)(ox + dx * t0), z0 = (int)(oz + dz * t0);
- if ((unsigned)x0 < VCX && (unsigned)z0 < VCZ && (unsigned)(trace_top - 1) < VCY) {
- x = x0, y = trace_top - 1, z = z0;
+ int x0 = (int)floorf(ox + dx * t0), z0 = (int)floorf(oz + dz * t0);
+ if ((unsigned)x0 < VCX && (unsigned)z0 < VCZ && (unsigned)(top - 1) < VCY) {
+ x = x0, y = top - 1, z = z0;
t = tj = t0, jf = 1;
tx = (x + bx - ox) * ivx, ty = (y - oy) * ivy, tz = (z + bz - oz) * ivz;
i = VC_I(x, y, z), mi = MC_I(x, y, z);
- }
+ } else if (cam_i < 0)
+ goto out;
}
}
+ if (vmac[mi] == 3 && dy < 0) goto unmade;
if (!vmac[mi] || (vmac[mi] == 2 && in_liq == 1)) goto jump;
for (;;) {
ST(st_steps++);
@@ -601,6 +609,7 @@ static uint16_t trace(float dx, float dy, float dz) {
if ((z & 3) != ez0) goto next;
mi += mz;
}
+ if (vmac[mi] == 3 && dy < 0) goto unmade;
if (!vmac[mi] || (vmac[mi] == 2 && in_liq == 1)) {
jump:;
/* an empty 4 x 4 x 4 region (or all water, seen from in the water): on from region to
@@ -622,6 +631,7 @@ static uint16_t trace(float dx, float dy, float dz) {
}
if (te > tmax) goto out;
reg = vmac[mi];
+ if (reg == 3 && dy < 0) goto unmade;
if (reg && !(reg == 2 && wet)) break;
}
t = tj = te;
@@ -641,6 +651,9 @@ static uint16_t trace(float dx, float dy, float dz) {
next:
if (t > tmax) break;
}
+ /* a chunk not made yet, seen from above: the fog, not the sky through the ground */
+unmade:
+ if (vmac[mi] == 3 && dy < 0) return fog565;
out:;
uint16_t c = sky(dx, dy, dz, len);
int wa = R.wa;
diff --git a/games/numblocks/src/world.c b/games/numblocks/src/world.c
index 81030bf8..e28104c4 100644
--- a/games/numblocks/src/world.c
+++ b/games/numblocks/src/world.c
@@ -258,8 +258,9 @@ static void recenter(int nx0, int ny0, int nz0, int px, int pz) {
if (dy > 0) ylo = VCY - dy;
else yhi = -dy;
/* new rows above everything the generator put in these columns: air, at once (going up a
- * hill moves the cache up, and making every chunk again for its top rows was slow) */
- air = dy > 0;
+ * hill, or falling from high up, moves the cache, and making every chunk again for its new
+ * rows was slow) */
+ air = true;
for (int z = 0; z < 16 && air; z++)
for (int x = 0; x < 16; x++) {
int lx = cx * 16 + x - vc_x0, lz = cz * 16 + z - vc_z0;
@@ -292,6 +293,17 @@ static void recenter(int nx0, int ny0, int nz0, int px, int pz) {
dirty[0] = dirty[3] = 0; /* (all of it, now) */
light_box(0, 0, 0, VCX, VCY, VCZ);
mac_box(0, 0, 0, VCX, VCY, VCZ);
+ /* the chunks waiting to be made: their regions drawn as fog until they are (not the sky through the
+ * ground), as high as the ground already made goes */
+ int known = 0;
+ for (int k = 0; k < VCX * VCZ; k++)
+ if (!vpend[k] && vgtop[k] > known) known = vgtop[k];
+ for (int k = 0; k < npend; k++)
+ for (int lz = pend[k].cz * 16 - vc_z0; lz < pend[k].cz * 16 + 16 - vc_z0; lz += 4)
+ for (int lx = pend[k].cx * 16 - vc_x0; lx < pend[k].cx * 16 + 16 - vc_x0; lx += 4)
+ if (lx >= 0 && lx < VCX && lz >= 0 && lz < VCZ)
+ for (int my = pend[k].ylo >> 2; my < (pend[k].yhi + 3) >> 2 && my < MCY && my * 4 + vc_y0 <= known; my++)
+ vmac[(my * MCZ + (lz >> 2)) * MCX + (lx >> 2)] = 3;
}
void world_new(int64_t seed, const char *name) {
@@ -326,9 +338,17 @@ static void prepare_ahead(int lx, int lz) {
void world_follow(float x, float y, float z) {
int px = ifloor(x), py = ifloor(y), pz = ifloor(z);
int lx = px - vc_x0, ly = py - vc_y0, lz = pz - vc_z0;
- if (!(vc_valid && lx >= 12 && lx < VCX - 12 && lz >= 12 && lz < VCZ - 12 && ly >= 8 && ly < VCY - 8)) {
+ /* High above the ground (flying, falling) the cache goes down as far as it can with room above the
+ * player to build: what is in sight below shows, and falling makes nothing for the air it passes */
+ static bool high;
+ int ground = vc_valid && (unsigned)lx < VCX && (unsigned)lz < VCZ ? vgtop[lz * VCX + lx] : py;
+ if (py - ground > 10) high = true;
+ if (py - ground < 6) high = false;
+ int ny_high = (py + 8 - VCY) & ~3;
+ bool ok = high ? ly >= 4 && ly < VCY - 4 && !(vc_y0 > ground - 4 && ny_high < vc_y0) : ly >= 8 && ly < VCY - 8;
+ if (!(vc_valid && lx >= 12 && lx < VCX - 12 && lz >= 12 && lz < VCZ - 12 && ok)) {
int nx0 = (px - VCX / 2) & ~7, nz0 = (pz - VCZ / 2) & ~7;
- int ny0 = (py - VCY / 2 + 2) & ~3;
+ int ny0 = high ? ny_high : (py - VCY / 2 + 2) & ~3;
if (ny0 < 0) ny0 = 0;
if (ny0 > WORLD_H - VCY) ny0 = WORLD_H - VCY;
if (!vc_valid || nx0 != vc_x0 || ny0 != vc_y0 || nz0 != vc_z0) {
diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.c
index 62a0abd5..071aa9de 100644
--- a/games/numblocks/tests/unit.c
+++ b/games/numblocks/tests/unit.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include "nb.h"
#include "edits.h"
+#include "gen.h"
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); fails++; } } while (0)
@@ -310,6 +311,26 @@ int main(void) {
CHECK(ay == vc_y0);
CHECK(!memcmp(a, vc, sizeof a));
CHECK(!memcmp(l, vl, sizeof l));
+ /* and coming down from high up (the new rows in the air made as air): as the generator makes them */
+ world_new(77, "ut9");
+ for (int y = 160; y >= 92; y -= 3)
+ for (int r = 0; r < 3; r++) {
+ world_follow(8, (float)y, 8);
+ while (world_pending()) world_follow(8, (float)y, 8);
+ }
+ static uint8_t g[16 * 16 * VCY];
+ int off = 0;
+ for (int cz = -2; cz <= 1; cz++)
+ for (int cx = -2; cx <= 1; cx++) {
+ gen_slab(cx, cz, vc_y0, VCY, g);
+ for (int y = 0; y < VCY; y++)
+ for (int z = 0; z < 16; z++)
+ for (int x = 0; x < 16; x++) {
+ int lx = cx * 16 + x - vc_x0, lz = cz * 16 + z - vc_z0;
+ if (lx >= 0 && lx < VCX && lz >= 0 && lz < VCZ) off += vc[VC_I(lx, y, lz)] != g[(y * 16 + z) * 16 + x];
+ }
+ }
+ CHECK(off == 0);
plat_remove_prefix("ut9");
}
{