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.nwa: old worlds keep their look, loot, the desert temple's trap

Mason36370f1f1e94fbb
diff --git a/games/numblocks/Makefile b/games/numblocks/Makefile
index 4941313b..173e9926 100644
--- a/games/numblocks/Makefile
+++ b/games/numblocks/Makefile
@@ -67,12 +67,17 @@ build/dims_test: $(GAME_DIMS) src/plat_host.c tests/dims.c $(HEADERS)
build/np_load: $(GAME) src/plat_host.c tests/np_load.c $(HEADERS)
$(Q) mkdir -p build
$(Q) cc $(HOST_FLAGS) $(GAME) src/plat_host.c tests/np_load.c -lm -o $@
-test: build/unit build/unit_np build/dims_test build/np_load
+# villages and desert temples: where they start, their layouts, their blocks, old worlds (structs.c)
+build/structs_test: $(GAME_DIMS) src/plat_host.c tests/structs.c $(HEADERS)
+ $(Q) mkdir -p build
+ $(Q) cc $(HOST_FLAGS) -DNB_DIMS=1 $(GAME_DIMS) src/plat_host.c tests/structs.c -lm -o $@
+test: build/unit build/unit_np build/dims_test build/np_load build/structs_test
@mkdir -p build/unit-saves
./build/unit
./build/unit_np
./build/dims_test
./build/np_load
+ ./build/structs_test
# every pixel traced: the reference the adaptive picture is checked against
build/play_ref: $(GAME_DIMS) src/plat_host.c tests/play.c $(HEADERS)
$(Q) mkdir -p build
diff --git a/games/numblocks/src/gui.c b/games/numblocks/src/gui.c
index 6b741857..c6f8f4eb 100644
--- a/games/numblocks/src/gui.c
+++ b/games/numblocks/src/gui.c
@@ -669,8 +669,10 @@ static Chest *chest_at(int x, int y, int z, bool make) {
#if NB_DIMS
if (dim == DIM_NETHER && !edits_changed(x, y, z)) chest_loot(&chests[i], 0);
else if (dim == DIM_OVERWORLD && !edits_changed(x, y, z)) {
- int k = gen_stronghold_chest(x, y, z); /* (a stronghold's: 1-3; none: 0) */
- if (k) chest_loot(&chests[i], k);
+ int k = gen_stronghold_chest(x, y, z); /* (a stronghold's: 1-3; else a village's or a temple's: 4, 5; */
+ if (!k) k = gen_struct_chest(x, y, z) + 3; /* else a dungeon's: 6) */
+ if (k == 3) k = 6;
+ chest_loot(&chests[i], k);
}
#endif
return &chests[i];
@@ -750,9 +752,24 @@ static void chest_loot(Chest *c, int kind) {
{I_IRON_INGOT, 1, 5, 10}, {I_GOLD_INGOT, 1, 3, 5}, {I_REDSTONE, 4, 9, 5}, {I_COAL, 3, 8, 10},
{I_BREAD, 1, 3, 15}, {I_APPLE, 1, 3, 15}, {I_IRON_PICKAXE, 1, 1, 1}, {0, 1, 1, 1}};
static const Loot library[] = {{I_BOOK, 1, 3, 20}, {I_PAPER, 2, 7, 20}, {0, 1, 1, 1}, {I_COMPASS, 1, 1, 1}, {0, 1, 5, 2}};
- static const struct { const Loot *l; uint8_t total, n0, n1; } T[4] = {
- {fort, 75, 2, 4}, {corridor, 99, 2, 2}, {room, 62, 1, 4}, {library, 44, 1, 4}};
- if (kind < 0 || kind > 3) return;
+ /* StructureVillagePieces.House2's, ComponentScatteredFeaturePieces.DesertPyramid's (and its book),
+ * WorldGenDungeons' (records and name tags are not in NumBlocks either) */
+ static const Loot smith[] = {
+ {I_DIAMOND, 1, 3, 3}, {I_IRON_INGOT, 1, 5, 10}, {I_GOLD_INGOT, 1, 3, 5}, {I_BREAD, 1, 3, 15}, {I_APPLE, 1, 3, 15},
+ {I_IRON_PICKAXE, 1, 1, 5}, {I_IRON_SWORD, 1, 1, 5}, {I_IRON_CHESTPLATE, 1, 1, 5}, {I_IRON_HELMET, 1, 1, 5},
+ {I_IRON_LEGGINGS, 1, 1, 5}, {I_IRON_BOOTS, 1, 1, 5}, {B_OBSIDIAN, 3, 7, 5}, {B_SAPLING_OAK, 3, 7, 5},
+ {0, 1, 1, 3}, {0, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1, 1}};
+ static const Loot desert[] = {
+ {I_DIAMOND, 1, 3, 3}, {I_IRON_INGOT, 1, 5, 10}, {I_GOLD_INGOT, 2, 7, 15}, {I_EMERALD, 1, 3, 2}, {I_BONE, 4, 6, 20},
+ {I_ROTTEN_FLESH, 3, 7, 16}, {0, 1, 1, 3}, {0, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1, 1}};
+ static const Loot dungeon[] = {
+ {0, 1, 1, 10}, {I_IRON_INGOT, 1, 4, 10}, {I_BREAD, 1, 1, 10}, {I_WHEAT, 1, 4, 10}, {I_GUNPOWDER, 1, 4, 10},
+ {I_STRING, 1, 4, 10}, {I_BUCKET, 1, 1, 10}, {I_GOLDEN_APPLE, 1, 1, 1}, {I_REDSTONE, 1, 4, 10}, {0, 1, 1, 4},
+ {0, 1, 1, 4}, {0, 1, 1, 10}, {0, 1, 1, 2}, {0, 1, 1, 5}, {0, 1, 1, 1}, {0, 1, 1, 1}};
+ static const struct { const Loot *l; uint8_t total, n0, n1; } T[7] = {
+ {fort, 75, 2, 4}, {corridor, 99, 2, 2}, {room, 62, 1, 4}, {library, 44, 1, 4}, {smith, 94, 3, 6},
+ {desert, 73, 2, 5}, {dungeon, 108, 8, 1}};
+ if (kind < 0 || kind > 6) return;
const Loot *L = T[kind].l;
int n = T[kind].n0 + rnd(T[kind].n1);
for (int k = 0; k < n; k++) {
diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.c
index 63760f98..76d008c2 100644
--- a/games/numblocks/src/player.c
+++ b/games/numblocks/src/player.c
@@ -953,6 +953,7 @@ void player_tick(uint32_t keys, uint32_t pressed) {
#if NB_DIMS
dims_player_tick();
buttons_tick();
+ plates_tick();
#endif
effects_tick();
if (pl.mode == 0) food_tick();
diff --git a/games/numblocks/src/player_dims.h b/games/numblocks/src/player_dims.h
index ebed5803..5665be59 100644
--- a/games/numblocks/src/player_dims.h
+++ b/games/numblocks/src/player_dims.h
@@ -106,6 +106,24 @@ static void buttons_tick(void) {
}
}
+/* BlockPressurePlate: stepped on, it powers the block under it and its neighbours; TNT next to either is lit
+ * (BlockTNT.onNeighborBlockChange): the desert temples' trap */
+static void plates_tick(void) {
+ static int16_t last[3] = {0, -1, 0};
+ int x = ifloor(pl.x), y = ifloor(pl.y), z = ifloor(pl.z), b = world_get(x, y, z);
+ if (b != B_WOODEN_PRESSURE_PLATE && b != B_STONE_PRESSURE_PLATE) {
+ last[1] = -1;
+ return;
+ }
+ if (last[0] == x && last[1] == y && last[2] == z) return;
+ last[0] = (int16_t)x, last[1] = (int16_t)y, last[2] = (int16_t)z;
+ for (int p = 0; p < 2; p++)
+ for (int f = 0; f < 6; f++) {
+ int tx = x + NX[f], ty = y - p + NY[f], tz = z + NZ[f];
+ if (world_get(tx, ty, tz) == B_TNT) world_set(tx, ty, tz, B_AIR), tnt_light(tx, ty, tz, 80);
+ }
+}
+
/* using an item of the Nether or the End (true: done) */
static bool dims_use(int k, bool on_block, int x, int y, int z, int b) {
Stack *h = held();
diff --git a/games/numblocks/src/save.c b/games/numblocks/src/save.c
index 23061700..1718e693 100644
--- a/games/numblocks/src/save.c
+++ b/games/numblocks/src/save.c
@@ -137,13 +137,18 @@ void world_unique_name(const char *base, char *out) {
* in, the player as they are, every chest and furnace, the dimensions' state. If NumPlay's NumBlocks
* saved the world after (the record is not the one the copy goes with), the record wins, the player in
* the Overworld, and the Nether's and the End's chests and the dimensions' state come back from the copy */
+#define NOLD 8
typedef struct {
char magic[4];
uint32_t time, played; /* the record's: the copy goes with it */
int32_t dim;
Player pl;
DimsState dst;
+ /* (NBD2) the chunks the world had before NumBlocks.nwa made villages and temples: up to 8 boxes */
+ uint8_t nold, pad[3];
+ int16_t old[NOLD][4];
} Side; /* then the tiles */
+#define SIDE1 offsetof(Side, nold) /* (NBD1: the world's copy made before, without the boxes) */
static void side_of(int s, char *out) {
prefix_of(s, out);
memcpy(out + 3, "rdim.nbx", 9);
@@ -154,7 +159,68 @@ static void known(Stack *s, int n) {
}
void tiles_for_numplay(void *t);
void tiles_merge(const void *t);
-bool gen_old_chunk(int cx, int cz) { return false; }
+
+/* Villages and temples (gen_struct.h) are not made in the chunks a world had before NumBlocks.nwa made them,
+ * so that an old world keeps its look: where it had been, as told when it is first opened since (the
+ * regions of its changes, 3 chunks around, and 6 around the player and their spawn), kept as up to 8 boxes
+ * of chunks (x0, z0, x1, z1) with the world (the side record) */
+static int16_t old_box[NOLD][4];
+static uint8_t n_old;
+bool gen_old_chunk(int cx, int cz) {
+ for (int i = 0; i < n_old; i++)
+ if (cx >= old_box[i][0] && cx <= old_box[i][2] && cz >= old_box[i][1] && cz <= old_box[i][3]) return true;
+ return false;
+}
+static int b_area(const int16_t *b) { return (b[2] - b[0] + 1) * (b[3] - b[1] + 1); }
+static void old_area(int s, float x, float z) {
+ int16_t b[48][4];
+ int n = 0;
+ int ix = (int)x - (x < (int)x), iz = (int)z - (z < (int)z); /* (floored) */
+ int c[2][2] = {{ix >> 4, iz >> 4}, {pl.spawn_x >> 4, pl.spawn_z >> 4}};
+ for (int k = 0; k < 2; k++)
+ b[n][0] = (int16_t)(c[k][0] - 6), b[n][1] = (int16_t)(c[k][1] - 6), b[n][2] = (int16_t)(c[k][0] + 6),
+ b[n][3] = (int16_t)(c[k][1] + 6), n++;
+ /* the Overworld's change records, "nb3r-3_12.nbe": regions of 4 x 4 chunks */
+ char pre[4], name[40];
+ prefix_of(s, pre);
+ for (int i = 0; plat_record(i, name, sizeof name); i++) {
+ if (memcmp(name, pre, 3) || name[3] != 'r') continue;
+ int v[2], k = 0;
+ const char *p = name + 4;
+ for (; k < 2; k++) {
+ int neg = *p == '-', d = 0;
+ p += neg;
+ if (*p < '0' || *p > '9') break;
+ while (*p >= '0' && *p <= '9') d = d * 10 + (*p++ - '0');
+ v[k] = neg ? -d : d;
+ if (*p++ != (k ? '.' : '_')) break;
+ }
+ if (k < 2) continue;
+ if (n == 48) n--; /* (a world changed in more places: the last box grows to hold them) */
+ b[n][0] = (int16_t)(v[0] * 4 - 3), b[n][1] = (int16_t)(v[1] * 4 - 3), b[n][2] = (int16_t)(v[0] * 4 + 6),
+ b[n][3] = (int16_t)(v[1] * 4 + 6);
+ n++;
+ }
+ /* to 8 boxes: the two whose union adds least, joined, until they fit */
+ while (n > NOLD) {
+ int bi = 0, bj = 1, best = 0x7fffffff;
+ for (int i = 0; i < n; i++)
+ for (int j = i + 1; j < n; j++) {
+ int16_t u[4] = {b[i][0] < b[j][0] ? b[i][0] : b[j][0], b[i][1] < b[j][1] ? b[i][1] : b[j][1],
+ b[i][2] > b[j][2] ? b[i][2] : b[j][2], b[i][3] > b[j][3] ? b[i][3] : b[j][3]};
+ int add = b_area(u) - b_area(b[i]) - b_area(b[j]);
+ if (add < best) best = add, bi = i, bj = j;
+ }
+ for (int k = 0; k < 2; k++) {
+ if (b[bj][k] < b[bi][k]) b[bi][k] = b[bj][k];
+ if (b[bj][k + 2] > b[bi][k + 2]) b[bi][k + 2] = b[bj][k + 2];
+ }
+ memmove(b[bj], b[bj + 1], sizeof b[0] * (size_t)(n - bj - 1));
+ n--;
+ }
+ memcpy(old_box, b, sizeof b[0] * (size_t)n);
+ n_old = (uint8_t)n;
+}
#endif
bool save_world(void) {
@@ -196,8 +262,10 @@ bool save_world(void) {
{
Side sd;
memset(&sd, 0, sizeof sd);
- memcpy(sd.magic, "NBD1", 4);
+ memcpy(sd.magic, "NBD2", 4);
sd.time = game_time, sd.played = cur_played, sd.dim = dim, sd.pl = pl, sd.dst = dst;
+ sd.nold = n_old;
+ memcpy(sd.old, old_box, sizeof old_box);
char rn[16];
side_of(slot, rn);
uint8_t *c = (uint8_t *)edits_scratch(sizeof sd + tl);
@@ -248,15 +316,24 @@ bool load_world(int s) {
uint32_t sl;
const uint8_t *c = plat_load(rn, &sl);
Side sd;
- if (c && sl == sizeof sd + tl && !memcmp(c, "NBD1", 4)) {
- memcpy(&sd, c, sizeof sd);
+ memset(&sd, 0, sizeof sd);
+ /* (NBD1, from before the villages: no boxes; the world's are found below) */
+ uint32_t hl2 = c && !memcmp(c, "NBD2", 4) ? sizeof sd : c && !memcmp(c, "NBD1", 4) ? SIDE1 : 0;
+ if (hl2 && sl == hl2 + tl) {
+ memcpy(&sd, c, hl2);
dst = sd.dst;
if (sd.time == h.h2.h.time && sd.played == h.h2.played && sd.dim >= 0 && sd.dim <= DIM_END) {
dim = sd.dim, pl = sd.pl;
- memcpy(t, c + sizeof sd, tl);
+ memcpy(t, c + hl2, tl);
} else
- tiles_merge(c + sizeof sd);
+ tiles_merge(c + hl2);
}
+ /* where the world had been before NumBlocks.nwa made villages: kept, else found now */
+ if (hl2 == sizeof sd) {
+ n_old = sd.nold > NOLD ? NOLD : sd.nold;
+ memcpy(old_box, sd.old, sizeof old_box);
+ } else if (dim == DIM_OVERWORLD) old_area(s, pl.x, pl.z);
+ else old_area(s, dst.ow_x, dst.ow_z);
}
#endif
char pre[4];
@@ -284,6 +361,7 @@ void new_world(int s, int64_t seed, int mode, bool cheats, int type, const char
#if NB_DIMS
dim = DIM_OVERWORLD;
memset(&dst, 0, sizeof dst);
+ n_old = 0; /* (a new world: villages everywhere) */
#endif
world_new(seed, pre);
player_spawn();
diff --git a/games/numblocks/tests/structs.c b/games/numblocks/tests/structs.c
new file mode 100644
index 00000000..07830743
--- /dev/null
+++ b/games/numblocks/tests/structs.c
@@ -0,0 +1,229 @@
+/* NumBlocks.nwa's villages and desert temples, checked on a computer:
+ *
+ * where they start: 1.8.8's grid of regions of 32 x 32 chunks, a start 0 to 23 chunks into each (so at
+ * least 8 apart), from World.setRandomSeed with the salt 10387312 (villages) or 14357617 (temples),
+ * worked out here again with a java.util.Random of this file's own, for a few seeds;
+ * how villages are laid out: no two pieces in each other, all within reach of the well, kept only with
+ * more than two pieces off the roads;
+ * how they are built: a village's well and a desert temple's trap where 1.8.8 puts them, and the same
+ * blocks whichever rows of a chunk are made;
+ * old worlds: a world saved before NumBlocks.nwa made villages keeps its look where it had been (no
+ * village there), and gets them elsewhere; it remembers where that is. */
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "../src/nb.h"
+#include "../src/gen.h"
+
+extern uint32_t host_time;
+extern bool start_in_world;
+extern int64_t start_seed;
+void game_init(void);
+bool game_frame(void);
+void host_save_dir(const char *d);
+
+static int fails;
+#define CHECK(c) do { if (!(c)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); fails++; } } while (0)
+#define SAY(...) do { printf(" "); printf(__VA_ARGS__); printf("\n"); } while (0)
+
+/* java.util.Random */
+static uint64_t jr;
+static void jseed(int64_t s) { jr = ((uint64_t)s ^ 0x5DEECE66DULL) & ((1ULL << 48) - 1); }
+static int jnext(int bits) {
+ jr = (jr * 0x5DEECE66DULL + 0xB) & ((1ULL << 48) - 1);
+ return (int)(int32_t)(uint32_t)(jr >> (48 - bits));
+}
+static int jint(int n) {
+ if ((n & -n) == n) return (int)(((int64_t)n * jnext(31)) >> 31);
+ int bits, val;
+ do bits = jnext(31), val = bits % n;
+ while (bits - val + (n - 1) < 0);
+ return val;
+}
+/* MapGenVillage / MapGenScatteredFeature.canSpawnStructureAtCoords: region (rx, rz)'s start */
+static void start_of(int64_t seed, int rx, int rz, int salt, int *sx, int *sz) {
+ jseed((int64_t)((uint64_t)(int64_t)rx * 341873128712ULL + (uint64_t)(int64_t)rz * 132897987541ULL + (uint64_t)seed +
+ (uint64_t)(int64_t)salt));
+ *sx = rx * 32 + jint(32 - 8);
+ *sz = rz * 32 + jint(32 - 8);
+}
+
+static uint8_t slab[128 * 256], hb[128 * 256 / 8];
+static int block_at(const uint8_t *s, const uint8_t *h, int x, int y, int z) {
+ int c = (y * 16 + (z & 15)) * 16 + (x & 15);
+ return s[c] | (h[c >> 3] >> (c & 7) & 1) << 8;
+}
+static void make(int cx, int cz, int y0, int h, uint8_t *s, uint8_t *hi) {
+ memset(hi, 0, (size_t)h * 256 / 8);
+ gen_hi = hi;
+ gen_slab(cx, cz, y0, h, s);
+ gen_hi = NULL;
+}
+
+/* the water in a village's well (the start chunk's corner + 2) as gen_slab makes it */
+static int well_water(int wx, int wz) {
+ make(wx >> 4, wz >> 4, 0, 128, slab, hb);
+ int n = 0;
+ for (int y = 40; y < 100; y++) n += block_at(slab, hb, wx + 2, y, wz + 2) == B_WATER;
+ return n;
+}
+/* a block only villages make (the generator's own, rare, differences at a slab's edge are plants and springs) */
+static int built(int b) {
+ return b == B_COBBLESTONE || b == B_PLANKS_OAK || b == B_LOG_OAK || b == B_GLASS_PANE || b == B_FENCE_OAK ||
+ b == B_FARMLAND || b == B_GRAVEL || b == B_WOOL_BLACK || b == B_LADDER || b == B_BOOKSHELF ||
+ b == B_CRAFTING_TABLE || b == B_FURNACE || b == B_STONE_SLAB || b == B_STONE_SLAB_DOUBLE || b == B_CHEST ||
+ (b >= B_TORCH && b <= B_TORCH_N) || (b >= B_WHEAT_0 && b <= B_WHEAT_7) || (b >= B_CARROTS_0 && b <= B_POTATOES_3) ||
+ b == B_OAK_STAIRS || b == B_COBBLESTONE_STAIRS || (b >= B_OAK_STAIRS_W && b <= B_DOOR_OAK_LOWER_N_OPEN) ||
+ b == B_DOOR_OAK_LOWER || b == B_DOOR_OAK_UPPER || b > 255;
+}
+static int boxes_meet(const int *a, const int *b) {
+ return a[5] >= b[2] && a[2] <= b[5] && a[7] >= b[4] && a[4] <= b[7] && a[6] >= b[3] && a[3] <= b[6];
+}
+
+int main(void) {
+ static const int64_t seeds[] = {0, 12345, -4172144997902289642LL, 404};
+ printf("where villages and temples start\n");
+ int villages = 0, made = 0;
+ for (unsigned s = 0; s < sizeof seeds / sizeof seeds[0]; s++) {
+ gen_init(seeds[s]);
+ for (int rz = -3; rz <= 3; rz++)
+ for (int rx = -3; rx <= 3; rx++) {
+ int out[10], sx, sz;
+ start_of(seeds[s], rx, rz, 10387312, &sx, &sz);
+ int found = gen_village_find(rx, rz, out);
+ CHECK(out[0] == sx && out[1] == sz);
+ CHECK(sx - rx * 32 >= 0 && sx - rx * 32 < 24 && sz - rz * 32 >= 0 && sz - rz * 32 < 24);
+ int nx, nz; /* the next region's start: at least 8 chunks further */
+ start_of(seeds[s], rx + 1, rz, 10387312, &nx, &nz);
+ CHECK(nx - sx >= 8);
+ if (!found) continue;
+ villages++;
+ /* the layout: the well first, at the start chunk + 2; no piece in another; near the well; more than
+ * two pieces off the roads to be made */
+ int p[128][8], n = 0, houses = 0;
+ while (n < 128 && gen_village_piece(n, p[n])) n++;
+ CHECK(n == out[9] && n > 0);
+ CHECK(p[0][0] == 0 && p[0][2] == out[0] * 16 + 2 && p[0][4] == out[1] * 16 + 2);
+ for (int i = 0; i < n; i++) {
+ houses += p[i][0] != 1;
+ CHECK(abs(p[i][2] - p[0][2]) <= 112 + 35 && abs(p[i][4] - p[0][4]) <= 112 + 35);
+ for (int j = i + 1; j < n; j++) CHECK(!boxes_meet(p[i], p[j]));
+ }
+ CHECK(out[2] == (houses > 2));
+ made += out[2];
+ }
+ for (int rz = -3; rz <= 3; rz++)
+ for (int rx = -3; rx <= 3; rx++) {
+ int sx, sz, t[2];
+ start_of(seeds[s], rx, rz, 14357617, &sx, &sz);
+ gen_temple_find(rx, rz, t);
+ CHECK(t[0] == sx && t[1] == sz);
+ }
+ }
+ SAY("%d villages laid out, %d made, for %d seeds", villages, made, (int)(sizeof seeds / sizeof seeds[0]));
+ CHECK(made > 10);
+
+ printf("a village's well, the same whichever rows are made\n");
+ gen_init(0);
+ int out[10];
+ CHECK(gen_village_find(-2, -1, out) && out[2]); /* (seed 0: a village at chunk -53, -16) */
+ int wx = out[0] * 16 + 2, wz = out[1] * 16 + 2, cx = wx >> 4, cz = wz >> 4;
+ make(cx, cz, 0, 128, slab, hb);
+ int water = 0, cobble = 0, wy = 0;
+ for (int y = 40; y < 100; y++)
+ for (int z = wz + 1; z <= wz + 4; z++)
+ for (int x = wx + 1; x <= wx + 4; x++) {
+ int b = block_at(slab, hb, x, y, z);
+ water += b == B_WATER;
+ cobble += b == B_COBBLESTONE;
+ if (b == B_WATER && y > wy) wy = y;
+ }
+ SAY("the well at %d %d: %d water, %d cobblestone, its water up to y %d", wx, wz, water, cobble, wy);
+ CHECK(water >= 4 * 10 && cobble >= 12 * 10);
+ static uint8_t part[24 * 256], phb[24 * 256 / 8];
+ int differ = 0;
+ for (int k = 0; k < 9; k++) { /* (the well's chunk and the eight around it) */
+ int qx = cx - 1 + k % 3, qz = cz - 1 + k / 3;
+ make(qx, qz, 0, 128, slab, hb);
+ for (int y0 = 0; y0 < 128; y0 += 24) {
+ int h = 128 - y0 < 24 ? 128 - y0 : 24;
+ make(qx, qz, y0, h, part, phb);
+ for (int y = 0; y < h; y++)
+ for (int i = 0; i < 256; i++)
+ if (block_at(part, phb, i & 15, y, i >> 4) != block_at(slab, hb, i & 15, y0 + y, i >> 4) &&
+ (built(block_at(part, phb, i & 15, y, i >> 4)) || built(block_at(slab, hb, i & 15, y0 + y, i >> 4)))) {
+ if (differ++ < 8)
+ SAY("at %d %d %d: %d (rows from %d), %d (all)", qx * 16 + (i & 15), y0 + y, qz * 16 + (i >> 4),
+ block_at(part, phb, i & 15, y, i >> 4), y0, block_at(slab, hb, i & 15, y0 + y, i >> 4));
+ }
+ }
+ }
+ SAY("its nine chunks made in rows of 24: %d blocks of the village differ", differ);
+ CHECK(differ == 0);
+
+ printf("a desert temple's trap\n");
+ gen_init(12345);
+ int t[2];
+ CHECK(gen_temple_find(2, -1, t)); /* (seed 12345: a desert temple at chunk 86, -14) */
+ int tx = t[0] * 16 + 10, tz = t[1] * 16 + 10;
+ make(tx >> 4, tz >> 4, 0, 128, slab, hb);
+ int tnt = 0;
+ for (int z = tz - 1; z <= tz + 1; z++)
+ for (int x = tx - 1; x <= tx + 1; x++) tnt += block_at(slab, hb, x, 51, z) == B_TNT;
+ SAY("the middle at %d %d: %d TNT at y 51, the plate %d", tx, tz, tnt, block_at(slab, hb, tx, 53, tz));
+ CHECK(tnt == 9 && block_at(slab, hb, tx, 53, tz) == B_STONE_PRESSURE_PLATE);
+ CHECK(gen_struct_chest(tx + 2, 53, tz) == 2 && gen_struct_chest(tx, 53, tz - 2) == 2 && !gen_struct_chest(tx, 53, tz));
+
+ printf("an old world keeps its look where it had been\n");
+ mkdir("build/structs-saves", 0755);
+ host_save_dir("build/structs-saves");
+ remove("build/structs-saves/nb1.nbw");
+ remove("build/structs-saves/nb1rdim.nbx");
+ start_in_world = true, start_seed = 0;
+ game_init();
+ pl.mode = 1, pl.flying = true;
+ pl.x = wx + 3.5f, pl.y = 80, pl.z = wz + 3.5f;
+ world_finish(pl.x, pl.y, pl.z);
+ int new_well = well_water(wx, wz);
+ SAY("a new world: the well's water %d deep", new_well);
+ CHECK(new_well >= 10);
+ CHECK(save_world());
+ /* the world as NumBlocks.nwa saved it before: its side record without the boxes ("NBD1") */
+ uint32_t len;
+ const uint8_t *d = plat_load("nb1rdim.nbx", &len);
+ CHECK(d && !memcmp(d, "NBD2", 4));
+ static uint8_t old[8192];
+ uint32_t side2 = 0;
+ if (d) {
+ /* (Side: magic, time, played, dim, the player, the dimensions' state; then (NBD2) 68 bytes of boxes) */
+ side2 = 4 + 4 + 4 + 4 + (uint32_t)sizeof(Player) + (uint32_t)sizeof(DimsState);
+ memcpy(old, d, side2);
+ memcpy(old, "NBD1", 4);
+ memcpy(old + side2, d + side2 + 68, len - side2 - 68);
+ CHECK(plat_save("nb1rdim.nbx", old, len - 68));
+ }
+ CHECK(load_world(1));
+ world_finish(pl.x, pl.y, pl.z);
+ int old_well = well_water(wx, wz);
+ SAY("the same world, saved before there were villages: water %d deep there", old_well);
+ CHECK(old_well < 3 && gen_old_chunk(cx, cz));
+ /* elsewhere: villages (seed 0: one at chunk 67, 40) */
+ CHECK(gen_village_find(2, 1, out) && out[2]);
+ int fx = out[0] * 16 + 2, fz = out[1] * 16 + 2;
+ CHECK(!gen_old_chunk(fx >> 4, fz >> 4));
+ pl.x = fx + 3.5f, pl.z = fz + 3.5f;
+ world_finish(pl.x, pl.y, pl.z);
+ int far_well = well_water(fx, fz);
+ SAY("far away, a village's well: water %d deep", far_well);
+ CHECK(far_well >= 10);
+ /* saved there and loaded again: the old place is still the old one */
+ CHECK(save_world() && load_world(1));
+ CHECK(gen_old_chunk(cx, cz) && !gen_old_chunk(fx >> 4, fz >> 4));
+ d = plat_load("nb1rdim.nbx", &len);
+ CHECK(d && !memcmp(d, "NBD2", 4));
+
+ printf("%s\n", fails ? "FAILED" : "all good (villages and temples)");
+ return fails != 0;
+}