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: TNT craters save, and lava that has flowed lights fires

Mason3631cf23232380f
diff --git a/games/common/epsilon_files.h b/games/common/epsilon_files.h
index ae81760b..c99efe9f 100644
--- a/games/common/epsilon_files.h
+++ b/games/common/epsilon_files.h
@@ -184,4 +184,50 @@ __attribute__((unused)) static const uint8_t *ef_read(const char *name, uint32_t
*len = ef_rd16(fs.buf + at) - 2 - nlen;
return fs.buf + at + 2 + nlen;
}
+static void ef_reverse(uint8_t *a, uint8_t *b) {
+ while (a < --b) {
+ uint8_t t = *a;
+ *a++ = *b, *b = t;
+ }
+}
+/* Makes a record `len` bytes long, keeping what it holds (cut, or with zeros after), and returns its content
+ * (unaligned!), or NULL if there is no room (then nothing changed). A record that has to grow is first turned
+ * to the end of the list, the ones after it sliding down, so it can grow where it is. */
+__attribute__((unused)) static uint8_t *ef_resize(const char *name, uint32_t len) {
+ ef_fs_t fs;
+ if (!ef_open(&fs)) return NULL;
+ int end = ef_end(&fs);
+ if (end < 0) return NULL;
+ uint32_t nlen = ef_strlen(name) + 1, total = 2 + nlen + len;
+ if (total > 0xFFFF) return NULL;
+ int at = ef_find(&fs, name, end);
+ if (at < 0) return ef_write(name, NULL, len) ? fs.buf + end + 2 + nlen : NULL;
+ uint32_t size = ef_rd16(fs.buf + at);
+ if (total == size) return fs.buf + at + 2 + nlen;
+ if ((uint32_t)end - size + total + 2 > fs.size) return NULL;
+ if (at + (int)size != end && total > size) {
+ uint32_t *cache = ef_cache(&fs, end);
+ if (!cache) return NULL;
+ ef_reverse(fs.buf + at, fs.buf + at + size);
+ ef_reverse(fs.buf + at + size, fs.buf + end);
+ ef_reverse(fs.buf + at, fs.buf + end);
+ cache[1] = cache[2] = 0;
+ at = end - (int)size;
+ }
+ if (at + (int)size == end) {
+ for (uint32_t i = size; i < total; i++) fs.buf[at + i] = 0;
+ for (uint32_t i = total + 2; i < size + 2; i++) fs.buf[at + i] = 0;
+ ef_wr16(fs.buf + at + total, 0);
+ } else { /* (smaller, in the middle: the ones after slide down) */
+ uint32_t *cache = ef_cache(&fs, end);
+ if (!cache) return NULL;
+ uint8_t *d = fs.buf + at + total;
+ const uint8_t *s = fs.buf + at + size;
+ for (int n = end + 2 - (at + (int)size); n > 0; n--) *d++ = *s++;
+ for (uint32_t n = size - total; n > 0; n--) *d++ = 0;
+ cache[1] = cache[2] = 0;
+ }
+ ef_wr16(fs.buf + at, total);
+ return fs.buf + at + 2 + nlen;
+}
#endif
diff --git a/games/numblocks/src/edits.c b/games/numblocks/src/edits.c
index 3dd1fe13..016716d9 100644
--- a/games/numblocks/src/edits.c
+++ b/games/numblocks/src/edits.c
@@ -144,7 +144,10 @@ static bool journal_has(int rx, int rz, uint32_t key, int i, int j) {
}
#endif
-/* the journal's entries go into their regions' records */
+static inline void wr32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); }
+
+/* the journal's entries go into their regions' records: each record grows in place in storage and the two
+ * are merged from the end, so a record can be as big as storage lets it be */
bool edits_flush(void) {
bool ok = true;
int i = 0;
@@ -152,33 +155,36 @@ bool edits_flush(void) {
int rx = jn[i].rx, rz = jn[i].rz, j = i;
while (j < nj && jn[j].rx == rx && jn[j].rz == rz) j++;
int n;
- const uint8_t *r = region(rx, rz, &n);
- /* merge the record and the journal's entries for it into the scratch buffer */
- uint32_t m = 0;
- int a = 0, b = i;
- while ((a < n || b < j) && m < scratch_n) {
-#if NB_DIMS
- if (a < n && (b >= j || KEY(R(a)) < KEY(jn[b].e)) && journal_has(rx, rz, KEY(R(a)) ^ HI, i, j)) {
- a++; /* (the journal has its spot, as the other kind) */
- continue;
- }
-#endif
- if (b >= j || (a < n && KEY(R(a)) < KEY(jn[b].e))) scratch[m++] = R(a), a++;
- else {
- if (a < n && KEY(R(a)) == KEY(jn[b].e)) a++;
- scratch[m++] = jn[b++].e;
- }
- }
+ region(rx, rz, &n);
char name[32];
region_name(name, rx, rz);
cache_ok = false;
- if (a < n || b < j || !plat_save(name, scratch, m * 4)) {
+ int top = n + (j - i);
+ uint8_t *r = plat_save_grow(name, (uint32_t)top * 4);
+ if (!r) {
/* no room: these changes stay in the journal */
ok = false;
full = true;
i = j;
continue;
}
+ int a = n - 1, b = j - 1, o = top;
+ while (a >= 0 || b >= i) {
+ if (a >= 0 && (b < i || KEY(R(a)) > KEY(jn[b].e))) {
+#if NB_DIMS
+ if (journal_has(rx, rz, KEY(R(a)) ^ HI, i, j)) {
+ a--; /* (the journal has its spot, as the other kind) */
+ continue;
+ }
+#endif
+ wr32(r + 4 * --o, R(a)), a--; /* (never past what is still to read: o > a) */
+ } else {
+ if (a >= 0 && KEY(R(a)) == KEY(jn[b].e)) a--;
+ wr32(r + 4 * --o, jn[b--].e);
+ }
+ }
+ if (o) memmove(r, r + 4 * o, (size_t)(top - o) * 4);
+ plat_save_done(name, (uint32_t)(top - o) * 4);
memmove(&jn[i], &jn[j], (size_t)(nj - j) * sizeof jn[0]);
nj -= j - i;
}
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.h
index 0444c76b..b2ed23f9 100644
--- a/games/numblocks/src/nb.h
+++ b/games/numblocks/src/nb.h
@@ -47,6 +47,8 @@ uint32_t plat_storage_free(void);
bool plat_record(int i, char *name, int max); /* the i-th record's name; false past the last */
uint8_t *plat_save_open(const char *name, uint32_t len); /* a record of len bytes to fill (NULL: no room, nothing changed) */
void plat_save_close(const char *name); /* (done filling it) */
+uint8_t *plat_save_grow(const char *name, uint32_t len); /* the record made len bytes long, keeping what it holds */
+void plat_save_done(const char *name, uint32_t len); /* (done with it: len bytes long, at most as grown) */
bool plat_in_launcher(void); /* running inside NumPlay (which keeps its own copy of the saves) */
void plat_begin(void);
int plat_end(void);
diff --git a/games/numblocks/src/plat_eadk.c b/games/numblocks/src/plat_eadk.c
index b993d347..618468f7 100644
--- a/games/numblocks/src/plat_eadk.c
+++ b/games/numblocks/src/plat_eadk.c
@@ -29,6 +29,14 @@ uint8_t *plat_save_open(const char *name, uint32_t len) {
return ef_write(name, NULL, len) ? (uint8_t *)(uintptr_t)ef_read(name, &n) : NULL;
}
void plat_save_close(const char *name) { (void)name; }
+uint8_t *plat_save_grow(const char *name, uint32_t len) {
+ uint8_t *p = ef_resize(name, len);
+ if (p) return p;
+ ef_remove("numblocks_saves.py");
+ ef_remove("numplay_saves.py");
+ return ef_resize(name, len);
+}
+void plat_save_done(const char *name, uint32_t len) { ef_resize(name, len); }
bool plat_record(int i, char *name, int max) {
ef_fs_t fs;
if (!ef_open(&fs)) return false;
diff --git a/games/numblocks/src/plat_host.c b/games/numblocks/src/plat_host.c
index 9d230354..7a71e461 100644
--- a/games/numblocks/src/plat_host.c
+++ b/games/numblocks/src/plat_host.c
@@ -57,6 +57,15 @@ uint8_t *plat_save_open(const char *name, uint32_t len) {
return open_buf;
}
void plat_save_close(const char *name) { plat_save(name, open_buf, open_len); }
+uint8_t *plat_save_grow(const char *name, uint32_t len) {
+ uint32_t n = 0;
+ const uint8_t *d = plat_load(name, &n);
+ free(open_buf);
+ open_buf = calloc(1, len ? len : 1), open_len = len;
+ if (d) memcpy(open_buf, d, n < len ? n : len);
+ return open_buf;
+}
+void plat_save_done(const char *name, uint32_t len) { plat_save(name, open_buf, len < open_len ? len : open_len); }
bool plat_record(int i, char *name, int max) {
DIR *d = opendir(save_dir);
if (!d) return false;
diff --git a/games/numblocks/src/tick.c b/games/numblocks/src/tick.c
index c3e32730..375aab61 100644
--- a/games/numblocks/src/tick.c
+++ b/games/numblocks/src/tick.c
@@ -384,7 +384,7 @@ static void random_tick(int x, int y, int z) {
fire_schedule(x, y, z, 8);
return;
}
- if (b == B_LAVA && rule(GR_FIRE_TICK)) {
+ if (is_lava(b) && rule(GR_FIRE_TICK)) { /* (a flow that has settled is still lava: BlockStaticLiquid) */
lava_fire(x, y, z);
return;
}
diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.c
index 2a2d6b9a..8496f1c8 100644
--- a/games/numblocks/tests/unit.c
+++ b/games/numblocks/tests/unit.c
@@ -98,6 +98,25 @@ int main(void) {
world_follow(8, 70, 8);
CHECK(world_get(5, 62, 5) == base);
}
+ /* a region with thousands of changes (TNT craters) still saves: its record grows where it is */
+ {
+ remove("build/unit-saves/ut4r0_0.nbe");
+ world_new(0, "ut4");
+ world_follow(8, 70, 8);
+ for (int y = 66; y < 74; y++)
+ for (int z = 0; z < 20; z++)
+ for (int x = 0; x < 20; x++) world_set(x, y, z, (x + z + y) & 1 ? B_GLASS : B_GOLD_BLOCK);
+ CHECK(edits_flush());
+ world_set(3, 70, 4, B_DIAMOND_BLOCK);
+ CHECK(edits_flush());
+ world_new(0, "ut4");
+ world_follow(8, 70, 8);
+ CHECK(world_get(0, 66, 0) == B_GOLD_BLOCK && world_get(19, 73, 19) == B_GLASS);
+ CHECK(world_get(3, 70, 4) == B_DIAMOND_BLOCK && world_get(4, 70, 4) == B_GOLD_BLOCK);
+ uint32_t len = 0;
+ CHECK(plat_load("ut4r0_0.nbe", &len) && len == 20 * 20 * 8 * 4);
+ plat_remove_prefix("ut4");
+ }
/* water: 7 blocks out on a flat floor, then dry when the source is gone; lava meets water */
{
world_new(0, "ut2");
@@ -127,6 +146,23 @@ int main(void) {
CHECK(world_get(5, y, 4) == B_OBSIDIAN);
plat_remove_prefix("ut2");
}
+ /* lava that has flowed sets fire by it, and the fire lights TNT (BlockStaticLiquid, BlockFire) */
+ {
+ world_new(0, "ut5");
+ world_follow(8, 70, 8);
+ int y = 70;
+ for (int z = 4; z <= 12; z++)
+ for (int x = 4; x <= 12; x++) {
+ world_set(x, y - 1, z, B_STONE);
+ for (int k = 0; k < 3; k++) world_set(x, y + k, z, B_AIR);
+ }
+ world_set(8, y, 8, B_FLOWING_LAVA);
+ world_set(9, y, 8, B_TNT);
+ int t = 0;
+ while (t < 400000 && world_get(9, y, 8) == B_TNT) ticks_run++, world_tick(), t++;
+ CHECK(world_get(9, y, 8) != B_TNT);
+ plat_remove_prefix("ut5");
+ }
/* two cows fed wheat make a calf (EntityAIMate) */
{
extern uint32_t game_time;