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: shorter hitches when the world moves with the player

Mason Chen2c8c4ec22fd0
diff --git a/games/numblocks/src/gen.c b/games/numblocks/src/gen.c
index 8cbe0b69..824d98b2 100644
--- a/games/numblocks/src/gen.c
+++ b/games/numblocks/src/gen.c
@@ -3142,7 +3142,7 @@ static void crec_cover(int cx, int cz) {
/* Summary cache */
/* ======================================================================== */
-#define NSUM 20
+#define NSUM 25
typedef struct {
int cx, cz;
uint32_t age;
@@ -5448,6 +5448,19 @@ static void slab_caves(int cx, int cz, const ColSum *sum, uint8_t *out, int y0,
cave_track_on = 0;
}
+/* Ahead of gen_slab(cx, cz): one of the summaries of the chunks around it that it will need, if
+ * one is not kept (each is a chunk's terrain, so the work of a chunk can be spread over frames).
+ * 0 when they are all there. */
+int gen_prepare(int cx, int cz) {
+ for (int dz = -1; dz <= 1; dz++)
+ for (int dx = -1; dx <= 1; dx++)
+ if ((dx || dz) && !sum_find(cx + dx, cz + dz)) {
+ sum_get(cx + dx, cz + dz);
+ return 1;
+ }
+ return 0;
+}
+
void gen_slab(int cx, int cz, int y0, int h, uint8_t *out) {
if (y0 < 0) {
h += y0;
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.h
index 891ad7ee..089a8ad4 100644
--- a/games/numblocks/src/nb.h
+++ b/games/numblocks/src/nb.h
@@ -56,6 +56,7 @@ static inline bool is_lava(int b) { return b == B_LAVA || b == B_FLOWING_LAVA ||
/* ---------------------------------------------------------------- the generator (gen.c) */
void gen_init(int64_t seed);
void gen_slab(int cx, int cz, int y0, int h, uint8_t *out); /* out[(y - y0) * 256 + z * 16 + x] */
+int gen_prepare(int cx, int cz); /* a slice of the work for gen_slab(cx, cz) ahead; 0: nothing left */
int gen_biome(int x, int z);
int gen_top(int x, int z);
void gen_spawn(int *x, int *y, int *z);
diff --git a/games/numblocks/src/world.c b/games/numblocks/src/world.c
index b16b67a1..2fc4ae8f 100644
--- a/games/numblocks/src/world.c
+++ b/games/numblocks/src/world.c
@@ -291,6 +291,26 @@ void world_new(int64_t seed, const char *name) {
memset(vpend, 0, sizeof vpend);
}
+/* Near a side of the cache the next move is coming: the chunks it will make there (one row of
+ * them, 8 blocks in) need the terrain of the chunks around them, which takes most of making a
+ * chunk. Made ahead, one chunk's a frame, the move's frames making the chunks are shorter. */
+static void prepare_ahead(int lx, int lz) {
+ int dx = lx >= 22 ? 1 : lx < 18 ? -1 : 0, dz = lz >= 22 ? 1 : lz < 18 ? -1 : 0;
+ if (dx && dz) { /* (one side at a time: the nearer) */
+ if ((dx > 0 ? VCX - lx : lx) < (dz > 0 ? VCZ - lz : lz)) dz = 0;
+ else dx = 0;
+ }
+ if (dx) {
+ int cx = floordiv(dx > 0 ? vc_x0 + VCX : vc_x0 - 8, 16);
+ for (int cz = floordiv(vc_z0, 16); cz <= floordiv(vc_z0 + VCZ - 1, 16); cz++)
+ if (gen_prepare(cx, cz)) return;
+ } else if (dz) {
+ int cz = floordiv(dz > 0 ? vc_z0 + VCZ : vc_z0 - 8, 16);
+ for (int cx = floordiv(vc_x0, 16); cx <= floordiv(vc_x0 + VCX - 1, 16); cx++)
+ if (gen_prepare(cx, cz)) return;
+ }
+}
+
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;
@@ -315,7 +335,7 @@ void world_follow(float x, float y, float z) {
fill_pending(best);
light_mark(x0 - 4, 0, z0 - 4, x0 + 20, VCY, z0 + 20);
mac_box(x0 < 0 ? 0 : x0, 0, z0 < 0 ? 0 : z0, x0 + 16 > VCX ? VCX : x0 + 16, VCY, z0 + 16 > VCZ ? VCZ : z0 + 16);
- }
+ } else prepare_ahead(lx, lz);
world_light_flush();
}