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: Nether fortresses, glowstone, quartz, lava; dimensions saved

Mason363f62f1a9528cd
diff --git a/games/numblocks/src/dims.h b/games/numblocks/src/dims.h
index b0fd681f..5113913e 100644
--- a/games/numblocks/src/dims.h
+++ b/games/numblocks/src/dims.h
@@ -16,10 +16,15 @@ extern const uint16_t gen_code_block[GC_N];
/* gen.c, past the Overworld (gen_dims.h): the generator of each dimension */
void gen_dim(int d); /* which one gen_slab, gen_biome and gen_top run (after gen_init) */
+int gen_fortress_at(int x, int y, int z);
+int gen_fortress_find(int x, int z, int *out); /* (tests) the nearest fortress: x, y, z, pieces */ /* 2: in a piece of a fortress, 1: in its box, 0: neither */
/* dims.c: what is kept of the dimensions with the world (save.c) */
typedef struct {
- float ow_x, ow_y, ow_z; /* where the player was last in the Overworld */
+ float ow_x, ow_y, ow_z; /* where the player was last in the Overworld */
+ uint8_t dragon_dead, pad[3];
+ float dragon_hp; /* the dragon's health while it lives (0: not met yet, full) */
+ uint32_t crystals_gone[2]; /* the End's crystals destroyed, by pillar */
} DimsState;
extern DimsState dst;
extern float portal_time; /* 0..1: the portal's swirl over the picture */
diff --git a/games/numblocks/src/edits.c b/games/numblocks/src/edits.c
index 62d0da8a..3dd1fe13 100644
--- a/games/numblocks/src/edits.c
+++ b/games/numblocks/src/edits.c
@@ -291,6 +291,20 @@ int edits_top(int x, int z, int top) {
}
#if NB_DIMS
+/* was the block there changed (else it is as the world was made)? */
+bool edits_changed(int x, int y, int z) {
+ int rx = floordiv(x, 64), rz = floordiv(z, 64), n;
+ uint32_t key = entry_key(x, y, z);
+ const uint8_t *r = region(rx, rz, &n);
+ for (int pass = 0; pass < 2; pass++, key |= HI) {
+ int i = jlower(rx, rz, key);
+ if (i < nj && jcmp(rx, rz, key, &jn[i]) == 0) return true;
+ int k = rlower(r, n, key);
+ if (k < n && KEY(R(k)) == key) return true;
+ }
+ return false;
+}
+
/* Teleporter.placeInExistingPortal: of the blocks b0 and b1 changed within r of (x, z) (portals: only made
* by changes), the nearest to (x, y, z) of those with neither below; false if none. Records only: the
* journal is flushed first */
diff --git a/games/numblocks/src/edits.h b/games/numblocks/src/edits.h
index 8975d1e2..e0df9ca0 100644
--- a/games/numblocks/src/edits.h
+++ b/games/numblocks/src/edits.h
@@ -18,5 +18,6 @@ void *edits_scratch(uint32_t n); /* in the journal */
void edits_apply9(int cx, int cz, int y0, int h, uint8_t *slab, uint8_t *hb); /* NumBlocks.nwa: 9-bit blocks */
void edits_dim(int d); /* the dimension whose records are used */
bool edits_find(int x, int y, int z, int r, int b0, int b1, int *out); /* the nearest changed b0 or b1 */
+bool edits_changed(int x, int y, int z); /* the block there is not as the world was made */
#endif
#endif
diff --git a/games/numblocks/src/gen_dims.h b/games/numblocks/src/gen_dims.h
index 72eee32d..2ef18807 100644
--- a/games/numblocks/src/gen_dims.h
+++ b/games/numblocks/src/gen_dims.h
@@ -77,15 +77,17 @@ static void gd_octaves(const Octave *oc, int n, float *out, i64 bx, i64 by, i64
/* The Nether: ChunkProviderHell */
/* ======================================================================== */
-#define NG 6 /* density grids kept */
+#define NGC 12 /* the decorations' grids kept (rounded to 1/32: they only look) */
typedef struct {
Octave oc[48]; /* netherNoiseGen1 (16), netherNoiseGen2 (16), netherNoiseGen3 (8), slowsandGravel (4),
netherrackExculsivity (4) */
float bias[17]; /* the density's offset by height (initializeNoiseField's adouble) */
- int gx[NG], gz[NG];
- uint32_t gage[NG], gclock;
- uint8_t gok[NG];
- float grid[NG][425]; /* (x * 5 + z) * 17 + y */
+ int fx, fz, fok;
+ float grid[425]; /* the terrain's grid, of chunk (fx, fz): (x * 5 + z) * 17 + y */
+ int gx[NGC], gz[NGC];
+ uint32_t gage[NGC], gclock;
+ uint8_t gok[NGC];
+ int16_t cgrid[NGC][425];
} NetherMem;
_Static_assert(sizeof(NetherMem) <= GEN_CACHE_BYTES, "the Nether's state fits in the summary cache");
#define NM ((NetherMem *)(void *)sumc)
@@ -113,22 +115,10 @@ static void nether_init(void) {
}
}
-/* initializeNoiseField: chunk (cx, cz)'s 5 x 17 x 5 densities */
-static const float *nether_grid(int cx, int cz) {
+static void nether_cgrid_put(int cx, int cz, const float *g);
+/* initializeNoiseField: chunk (cx, cz)'s 5 x 17 x 5 densities, into g */
+static void nether_noise(int cx, int cz, float *g) {
NetherMem *m = NM;
- int k = 0;
- for (int i = 0; i < NG; i++) {
- if (m->gok[i] && m->gx[i] == cx && m->gz[i] == cz) {
- m->gage[i] = ++m->gclock;
- return m->grid[i];
- }
- if (!m->gok[i] || m->gage[i] < m->gage[k]) k = m->gok[k] ? i : k;
- }
- for (int i = 0; i < NG; i++)
- if (!m->gok[i]) {
- k = i;
- break;
- }
float n1[425], n2[425], n3[425];
memset(n1, 0, sizeof n1);
memset(n2, 0, sizeof n2);
@@ -136,7 +126,6 @@ static const float *nether_grid(int cx, int cz) {
gd_octaves(m->oc + 32, 8, n3, cx * 4, 0, cz * 4, 5, 17, 5, SC_N3XZ, SC_N3Y, SC_N3XZ);
gd_octaves(m->oc, 16, n1, cx * 4, 0, cz * 4, 5, 17, 5, SC_N1XZ, SC_N1Y, SC_N1XZ);
gd_octaves(m->oc + 16, 16, n2, cx * 4, 0, cz * 4, 5, 17, 5, SC_N1XZ, SC_N1Y, SC_N1XZ);
- float *g = m->grid[k];
for (int i = 0; i < 425; i++) {
int y = i % 17;
float d6 = n1[i] / 512.0f, d7 = n2[i] / 512.0f, d8 = (n3[i] / 10.0f + 1.0f) / 2.0f, d4;
@@ -150,8 +139,47 @@ static const float *nether_grid(int cx, int cz) {
}
g[i] = d4;
}
+ nether_cgrid_put(cx, cz, g);
+}
+/* the terrain's grid of chunk (cx, cz) (the last one kept) */
+static const float *nether_grid(int cx, int cz) {
+ NetherMem *m = NM;
+ if (!m->fok || m->fx != cx || m->fz != cz) {
+ nether_noise(cx, cz, m->grid);
+ m->fx = cx, m->fz = cz, m->fok = 1;
+ }
+ return m->grid;
+}
+/* the decorations' grids: the latest used kept, rounded */
+static void nether_cgrid_put(int cx, int cz, const float *g) {
+ NetherMem *m = NM;
+ int k = 0;
+ for (int i = 0; i < NGC; i++) {
+ if (m->gok[i] && m->gx[i] == cx && m->gz[i] == cz) return;
+ if (!m->gok[i]) {
+ k = i;
+ break;
+ }
+ if (m->gage[i] < m->gage[k]) k = i;
+ }
+ for (int i = 0; i < 425; i++) {
+ float v = g[i] * 32.0f;
+ m->cgrid[k][i] = (int16_t)(v > 32767 ? 32767 : v < -32768 ? -32768 : v);
+ }
m->gx[k] = cx, m->gz[k] = cz, m->gok[k] = 1, m->gage[k] = ++m->gclock;
- return g;
+}
+static const int16_t *nether_cgrid(int cx, int cz) {
+ NetherMem *m = NM;
+ for (int pass = 0; pass < 2; pass++) {
+ for (int i = 0; i < NGC; i++)
+ if (m->gok[i] && m->gx[i] == cx && m->gz[i] == cz) {
+ m->gage[i] = ++m->gclock;
+ return m->cgrid[i];
+ }
+ float g[425];
+ nether_noise(cx, cz, g);
+ }
+ return m->cgrid[0];
}
/* func_180515_a: the terrain of a column (netherrack where the density is above 0, lava under y 32),
@@ -230,9 +258,11 @@ typedef struct {
static int hc_lava(const HellCaves *c, int x, int y, int z) { return c->lava[(x * 16 + z) * 4 + (y >> 5)] >> (y & 31) & 1; }
-static void hell_tunnel(HellCaves *c, i64 seed, double px, double py, double pz, float w, float yaw, float pitch, int k,
- int len, double vy) {
- double d0 = (double)(c->cx * 16 + 8), d1 = (double)(c->cz * 16 + 8);
+/* MapGenCavesHell.func_180702_a. As gen.c's caves: the place exact in fixed point (Java's doubles only ever
+ * add floats to it), the carving's tests in float */
+static void hell_tunnel(HellCaves *c, i64 seed, Q32 px, Q32 py, Q32 pz, float w, float yaw, float pitch, int k,
+ int len, int half) {
+ Q32 d0 = SHL(c->cx * 16 + 8, 32), d1 = SHL(c->cz * 16 + 8, 32);
float f = 0.0f, f1 = 0.0f;
JRand rnd;
jr_seed(&rnd, seed);
@@ -247,12 +277,12 @@ static void hell_tunnel(HellCaves *c, i64 seed, double px, double py, double pz,
}
int j = jr_int(&rnd, len / 2) + len / 4;
for (int flag = jr_int(&rnd, 6) == 0; k < len; ++k) {
- double d2 = 1.5 + (double)(mh_sin((float)k * 3.1415927f / (float)len) * w * 1.0f);
- double d3 = d2 * vy;
+ float sv = mh_sin((float)k * 3.1415927f / (float)len) * w * 1.0f;
+ Q32 r2 = (Q32)(3LL << 31) + q32_of_float(sv), r3 = half ? r2 / 2 : r2; /* d2 = 1.5 + ..., d3 = d2 x 0.5 or 1 */
float f2 = mh_cos(pitch), f3 = mh_sin(pitch);
- px += (double)(mh_cos(yaw) * f2);
- py += (double)f3;
- pz += (double)(mh_sin(yaw) * f2);
+ px += q32_of_float(mh_cos(yaw) * f2);
+ py += q32_of_float(f3);
+ pz += q32_of_float(mh_sin(yaw) * f2);
pitch = flag ? pitch * 0.92f : pitch * 0.7f;
pitch += f1 * 0.1f;
yaw += f * 0.1f;
@@ -266,20 +296,19 @@ static void hell_tunnel(HellCaves *c, i64 seed, double px, double py, double pz,
if (!flag1 && k == j && w > 1.0f) {
i64 s1 = jr_long(&rnd);
float a = jr_float(&rnd) * 0.5f + 0.5f;
- hell_tunnel(c, s1, px, py, pz, a, yaw - 1.5707964f, pitch / 3.0f, k, len, 1.0);
+ hell_tunnel(c, s1, px, py, pz, a, yaw - 1.5707964f, pitch / 3.0f, k, len, 0);
i64 s2 = jr_long(&rnd);
float b2 = jr_float(&rnd) * 0.5f + 0.5f;
- hell_tunnel(c, s2, px, py, pz, b2, yaw + 1.5707964f, pitch / 3.0f, k, len, 1.0);
+ hell_tunnel(c, s2, px, py, pz, b2, yaw + 1.5707964f, pitch / 3.0f, k, len, 0);
return;
}
if (flag1 || jr_int(&rnd, 4) != 0) {
- double d4 = px - d0, d5 = pz - d1, d6 = (double)(len - k), d7 = (double)(w + 2.0f + 16.0f);
- if (d4 * d4 + d5 * d5 - d6 * d6 > d7 * d7) return;
- if (px >= d0 - 16.0 - d2 * 2.0 && pz >= d1 - 16.0 - d2 * 2.0 && px <= d0 + 16.0 + d2 * 2.0 &&
- pz <= d1 + 16.0 + d2 * 2.0) {
- int k2 = (int)floor(px - d2) - c->cx * 16 - 1, kk = (int)floor(px + d2) - c->cx * 16 + 1;
- int l2 = (int)floor(py - d3) - 1, l = (int)floor(py + d3) + 1;
- int i3 = (int)floor(pz - d2) - c->cz * 16 - 1, i1 = (int)floor(pz + d2) - c->cz * 16 + 1;
+ if (cave_too_far(px - d0, pz - d1, len - k, w + 2.0f + 16.0f)) return;
+ Q32 reach = SHL(16, 32) + r2 * 2;
+ if (px >= d0 - reach && pz >= d1 - reach && px <= d0 + reach && pz <= d1 + reach) {
+ int k2 = q32_floor(px - r2) - c->cx * 16 - 1, kk = q32_floor(px + r2) - c->cx * 16 + 1;
+ int l2 = q32_floor(py - r3) - 1, l = q32_floor(py + r3) + 1;
+ int i3 = q32_floor(pz - r2) - c->cz * 16 - 1, i1 = q32_floor(pz + r2) - c->cz * 16 + 1;
if (k2 < 0) k2 = 0;
if (kk > 16) kk = 16;
if (l2 < 1) l2 = 1;
@@ -295,16 +324,18 @@ static void hell_tunnel(HellCaves *c, i64 seed, double px, double py, double pz,
if (l1 != l2 - 1 && j1 != k2 && j1 != kk - 1 && k1 != i3 && k1 != i1 - 1) l1 = l2;
}
if (!flag2) {
+ float d2 = q32_to_float(r2), d3 = q32_to_float(r3);
+ int ylo = l2 + 1 > c->y0 ? l2 + 1 : c->y0, yhi = l < c->y0 + c->h - 1 ? l : c->y0 + c->h - 1;
for (int j3 = k2; j3 < kk; ++j3) {
- double d10 = ((double)(j3 + c->cx * 16) + 0.5 - px) / d2;
+ float d10 = q32_to_float(SHL(j3 + c->cx * 16, 32) + (Q32_ONE >> 1) - px) / d2;
for (int i2 = i3; i2 < i1; ++i2) {
- double d8 = ((double)(i2 + c->cz * 16) + 0.5 - pz) / d2;
- for (int j2 = l; j2 > l2; --j2) {
- double d9 = ((double)(j2 - 1) + 0.5 - py) / d3;
- if (d9 > -0.7 && d10 * d10 + d9 * d9 + d8 * d8 < 1.0) {
- int y = j2 - c->y0;
- if (y < 0 || y >= c->h) continue;
- uint8_t *b = &c->out[(y * 16 + i2) * 16 + j3];
+ float d8 = q32_to_float(SHL(i2 + c->cz * 16, 32) + (Q32_ONE >> 1) - pz) / d2;
+ float h2 = d10 * d10 + d8 * d8;
+ if (h2 >= 1.0f) continue;
+ for (int y = yhi; y >= ylo; --y) {
+ float d9 = q32_to_float(SHL(y - 1, 32) + (Q32_ONE >> 1) - py) / d3;
+ if (d9 > -0.7f && h2 + d9 * d9 < 1.0f) {
+ uint8_t *b = &c->out[((y - c->y0) * 16 + i2) * 16 + j3];
if (*b == GEN_CODE(GC_NETHERRACK) || *b == B_DIRT || *b == B_GRASS) *b = B_AIR;
}
}
@@ -328,21 +359,21 @@ static void hell_caves(HellCaves *c) {
int i = jr_int(&r, jr_int(&r, jr_int(&r, 10) + 1) + 1);
if (jr_int(&r, 5) != 0) i = 0;
for (int n = 0; n < i; ++n) {
- double d0 = (double)(l * 16 + jr_int(&r, 16)), d1 = (double)jr_int(&r, 128),
- d2 = (double)(i1 * 16 + jr_int(&r, 16));
+ int x0 = l * 16 + jr_int(&r, 16), y0 = jr_int(&r, 128), z0 = i1 * 16 + jr_int(&r, 16);
+ Q32 d0 = SHL(x0, 32), d1 = SHL(y0, 32), d2 = SHL(z0, 32);
int kk = 1;
if (jr_int(&r, 4) == 0) {
i64 s = jr_long(&r);
/* func_180705_a: a room */
float w = 1.0f + jr_float(&r) * 6.0f;
- hell_tunnel(c, s, d0, d1, d2, w, 0.0f, 0.0f, -1, -1, 0.5);
+ hell_tunnel(c, s, d0, d1, d2, w, 0.0f, 0.0f, -1, -1, 1);
kk += jr_int(&r, 4);
}
for (int m = 0; m < kk; ++m) {
float f = jr_float(&r) * 3.1415927f * 2.0f;
float f1 = (jr_float(&r) - 0.5f) * 2.0f / 8.0f;
float f2 = jr_float(&r) * 2.0f + jr_float(&r);
- hell_tunnel(c, jr_long(&r), d0, d1, d2, f2 * 2.0f, f, f1, 0, 0, 0.5);
+ hell_tunnel(c, jr_long(&r), d0, d1, d2, f2 * 2.0f, f, f1, 0, 0, 1);
}
}
}
@@ -380,7 +411,184 @@ static void nether_slab(int cx, int cz, int y0, int h, uint8_t *out) {
nether_decorate(cx, cz, y0, h, out);
}
-static void nether_decorate(int cx, int cz, int y0, int h, uint8_t *out) { (void)cx, (void)cz, (void)y0, (void)h, (void)out; }
+#include "gen_fortress.h"
+
+/* ---- ChunkProviderHell.populate
+ * A chunk is decorated by the populations of the four chunks whose areas (offset by 8) cover it, replayed
+ * each time its rows are made. What their features look at is the terrain without the caves (nq: the
+ * same for every row of the chunk, so the rows agree) and what they placed before (a small set of
+ * blocks); what they place lands in the rows where it fits (glowstone, fire and mushrooms in air, lava in
+ * air or netherrack, quartz in netherrack). */
+static int nq(int x, int y, int z) {
+ if (y <= 0 || y == 127) return B_BEDROCK;
+ if (y > 127) return B_AIR;
+ const int16_t *g = nether_cgrid(x >> 4, z >> 4);
+ int lx = x & 15, lz = z & 15, j1 = lx >> 2, k1 = lz >> 2, l1 = y >> 3;
+#define G(i) ((float)g[i] * (1.0f / 32.0f))
+ float d1 = G(((j1 + 0) * 5 + k1 + 0) * 17 + l1), d2 = G(((j1 + 0) * 5 + k1 + 1) * 17 + l1);
+ float d3 = G(((j1 + 1) * 5 + k1 + 0) * 17 + l1), d4 = G(((j1 + 1) * 5 + k1 + 1) * 17 + l1);
+ float f = (float)(y & 7) * 0.125f;
+ d1 += (G(((j1 + 0) * 5 + k1 + 0) * 17 + l1 + 1) - d1) * f;
+ d2 += (G(((j1 + 0) * 5 + k1 + 1) * 17 + l1 + 1) - d2) * f;
+ d3 += (G(((j1 + 1) * 5 + k1 + 0) * 17 + l1 + 1) - d3) * f;
+ d4 += (G(((j1 + 1) * 5 + k1 + 1) * 17 + l1 + 1) - d4) * f;
+#undef G
+ float fx = (float)(lx & 3) * 0.25f, a = d1 + (d3 - d1) * fx, b = d2 + (d4 - d2) * fx;
+ float d = a + (b - a) * (float)(lz & 3) * 0.25f;
+ return d > 0.0f ? GEN_CODE(GC_NETHERRACK) : y < 32 ? B_LAVA : B_AIR;
+}
+
+/* the blocks placed by the populations replayed, in the rest of U (open addressing on the place) */
+#define WS_N ((int)((sizeof U - sizeof(Fort)) / 4))
+#define WSET ((uint32_t *)(void *)((uint8_t *)&U + sizeof(Fort)))
+static struct {
+ int cx, cz, y0, h;
+ uint8_t *out;
+} nd;
+static uint32_t ws_key(int x, int y, int z) {
+ int rx = x - nd.cx * 16 + 48, rz = z - nd.cz * 16 + 48;
+ if ((unsigned)rx > 127 || (unsigned)rz > 127 || (unsigned)y > 127) return 0;
+ return (uint32_t)(rx << 14 | rz << 7 | y) + 1;
+}
+static uint32_t *ws_find(uint32_t k) {
+ uint32_t *w = WSET;
+ for (uint32_t i = k * 2654435761u % (uint32_t)WS_N, n = 0; n < (uint32_t)WS_N; n++, i = i + 1 == (uint32_t)WS_N ? 0 : i + 1)
+ if (!w[i] || w[i] >> 8 == k) return &w[i];
+ return NULL;
+}
+static int wget(int x, int y, int z) {
+ uint32_t k = ws_key(x, y, z), *e = k ? ws_find(k) : NULL;
+ return e && *e ? (int)(*e & 255) : nq(x, y, z);
+}
+static void wput(int x, int y, int z, int b) {
+ uint32_t k = ws_key(x, y, z), *e = k ? ws_find(k) : NULL;
+ if (e) *e = k << 8 | (uint32_t)b;
+ int lx = x - nd.cx * 16, lz = z - nd.cz * 16, ly = y - nd.y0;
+ if ((unsigned)lx > 15 || (unsigned)lz > 15 || ly < 0 || ly >= nd.h) return;
+ uint8_t *c = &nd.out[(ly * 16 + lz) * 16 + lx];
+ if (*c == B_AIR || (b == B_LAVA && *c == GEN_CODE(GC_NETHERRACK))) *c = (uint8_t)b;
+}
+#define RACK GEN_CODE(GC_NETHERRACK)
+
+/* WorldGenGlowStone1 (and 2, the same): from under netherrack, 1500 tries to hang one by just one other */
+static void feat_glow(JRand *r, int x, int y, int z) {
+ if (wget(x, y, z) != B_AIR || wget(x, y + 1, z) != RACK) return;
+ wput(x, y, z, B_GLOWSTONE);
+ for (int i = 0; i < 1500; i++) {
+ int a = jr_int(r, 8), b = jr_int(r, 8), dy = -jr_int(r, 12), c = jr_int(r, 8), d = jr_int(r, 8);
+ int bx = x + a - b, by = y + dy, bz = z + c - d;
+ if (wget(bx, by, bz) != B_AIR) continue;
+ static const int8_t D[6][3] = {{0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}};
+ int j = 0;
+ for (int f = 0; f < 6 && j <= 1; f++) j += wget(bx + D[f][0], by + D[f][1], bz + D[f][2]) == B_GLOWSTONE;
+ if (j == 1) wput(bx, by, bz, B_GLOWSTONE);
+ }
+}
+/* WorldGenHellLava: under netherrack, in air or netherrack: walled on all five sides, or (not inside the
+ * rock) on four with air on the fifth (it pours out there) */
+static void feat_lava(int x, int y, int z, int inside) {
+ if (wget(x, y + 1, z) != RACK) return;
+ int b = wget(x, y, z);
+ if (b != B_AIR && b != RACK) return;
+ static const int8_t D[5][3] = {{-1, 0, 0}, {1, 0, 0}, {0, 0, -1}, {0, 0, 1}, {0, -1, 0}};
+ int i = 0, j = 0, open = 0;
+ for (int f = 0; f < 5; f++) i += wget(x + D[f][0], y + D[f][1], z + D[f][2]) == RACK;
+ for (int f = 0; f < 5; f++)
+ if (wget(x + D[f][0], y + D[f][1], z + D[f][2]) == B_AIR) j++, open = f;
+ if ((!inside && i == 4 && j == 1) || i == 5) {
+ wput(x, y, z, B_LAVA);
+ if (i == 4) wput(x + D[open][0], y + D[open][1], z + D[open][2], B_FLOWING_LAVA);
+ }
+}
+/* WorldGenFire and WorldGenBush: 64 tries about the place */
+static void feat_scatter(JRand *r, int x, int y, int z, int b) {
+ for (int i = 0; i < 64; i++) {
+ int a = jr_int(r, 8), c = jr_int(r, 8), e = jr_int(r, 4), f = jr_int(r, 4), g = jr_int(r, 8), k = jr_int(r, 8);
+ int bx = x + a - c, by = y + e - f, bz = z + g - k;
+ if (wget(bx, by, bz) != B_AIR) continue;
+ int below = wget(bx, by - 1, bz);
+ if (b == B_FIRE ? below == RACK : (below == RACK || below == B_GLOWSTONE || below == GEN_CODE(GC_SOUL_SAND)))
+ wput(bx, by, bz, b);
+ }
+}
+/* WorldGenMinable(quartz ore, 14), in netherrack: only the rows made matter */
+static void feat_quartz(JRand *r, int x, int y, int z) {
+ const int n = 14;
+ float f = jr_float(r) * 3.1415927f;
+ float d0 = (float)(x + 8) + mh_sin(f) * (float)n / 8.0f, d1 = (float)(x + 8) - mh_sin(f) * (float)n / 8.0f;
+ float d2 = (float)(z + 8) + mh_cos(f) * (float)n / 8.0f, d3 = (float)(z + 8) - mh_cos(f) * (float)n / 8.0f;
+ float d4 = (float)(y + jr_int(r, 3) - 2), d5 = (float)(y + jr_int(r, 3) - 2);
+ for (int i = 0; i < n; i++) {
+ float f1 = (float)i / (float)n;
+ float d6 = d0 + (d1 - d0) * f1, d7 = d4 + (d5 - d4) * f1, d8 = d2 + (d3 - d2) * f1;
+ float d9 = jr_doublef(r) * (float)n / 16.0f;
+ float d10 = (mh_sin(3.1415927f * f1) + 1.0f) * d9 + 1.0f;
+ int j = (int)floorf(d6 - d10 / 2), k = (int)floorf(d7 - d10 / 2), l = (int)floorf(d8 - d10 / 2);
+ int i1 = (int)floorf(d6 + d10 / 2), j1 = (int)floorf(d7 + d10 / 2), k1 = (int)floorf(d8 + d10 / 2);
+ if (i1 < nd.cx * 16 || j > nd.cx * 16 + 15 || k1 < nd.cz * 16 || l > nd.cz * 16 + 15 || j1 < nd.y0 || k >= nd.y0 + nd.h)
+ continue;
+ for (int bx = j; bx <= i1; bx++) {
+ float d12 = ((float)bx + 0.5f - d6) / (d10 / 2);
+ if (d12 * d12 >= 1) continue;
+ for (int by = k; by <= j1; by++) {
+ float d13 = ((float)by + 0.5f - d7) / (d10 / 2);
+ if (d12 * d12 + d13 * d13 >= 1) continue;
+ for (int bz = l; bz <= k1; bz++) {
+ float d14 = ((float)bz + 0.5f - d8) / (d10 / 2);
+ if (d12 * d12 + d13 * d13 + d14 * d14 >= 1) continue;
+ int lx = bx - nd.cx * 16, lz = bz - nd.cz * 16, ly = by - nd.y0;
+ if ((unsigned)lx > 15 || (unsigned)lz > 15 || ly < 0 || ly >= nd.h) continue;
+ uint8_t *c = &nd.out[(ly * 16 + lz) * 16 + lx];
+ if (*c == RACK) *c = GEN_CODE(GC_QUARTZ_ORE);
+ }
+ }
+ }
+ }
+}
+
+static void nether_populate(int px, int pz) {
+ JRand r;
+ jr_seed(&r, ((i64)px * 341873128712LL + (i64)pz * 132897987541LL) ^ g_seed);
+ int bx = px * 16, bz = pz * 16;
+ for (int i = 0; i < 8; i++) {
+ int x = jr_int(&r, 16) + 8, y = jr_int(&r, 120) + 4, z = jr_int(&r, 16) + 8;
+ feat_lava(bx + x, y, bz + z, 0);
+ }
+ for (int j = 0; j < jr_int(&r, jr_int(&r, 10) + 1) + 1; j++) {
+ int x = jr_int(&r, 16) + 8, y = jr_int(&r, 120) + 4, z = jr_int(&r, 16) + 8;
+ feat_scatter(&r, bx + x, y, bz + z, B_FIRE);
+ }
+ for (int k = 0; k < jr_int(&r, jr_int(&r, 10) + 1); k++) {
+ int x = jr_int(&r, 16) + 8, y = jr_int(&r, 120) + 4, z = jr_int(&r, 16) + 8;
+ feat_glow(&r, bx + x, y, bz + z);
+ }
+ for (int l = 0; l < 10; l++) {
+ int x = jr_int(&r, 16) + 8, y = jr_int(&r, 128), z = jr_int(&r, 16) + 8;
+ feat_glow(&r, bx + x, y, bz + z);
+ }
+ for (int m = 0; m < 2; m++)
+ if (jr_bool(&r)) {
+ int x = jr_int(&r, 16) + 8, y = jr_int(&r, 128), z = jr_int(&r, 16) + 8;
+ feat_scatter(&r, bx + x, y, bz + z, m ? B_RED_MUSHROOM : B_BROWN_MUSHROOM);
+ }
+ for (int i = 0; i < 16; i++) {
+ int x = jr_int(&r, 16), y = jr_int(&r, 108) + 10, z = jr_int(&r, 16);
+ feat_quartz(&r, bx + x, y, bz + z);
+ }
+ for (int i = 0; i < 16; i++) {
+ int x = jr_int(&r, 16), y = jr_int(&r, 108) + 10, z = jr_int(&r, 16);
+ feat_lava(bx + x, y, bz + z, 1);
+ }
+}
+
+static void nether_decorate(int cx, int cz, int y0, int h, uint8_t *out) {
+ fort_build(cx, cz, y0, h, out);
+ nd.cx = cx, nd.cz = cz, nd.y0 = y0, nd.h = h, nd.out = out;
+ memset(WSET, 0, (size_t)WS_N * 4);
+ for (int pz = cz - 1; pz <= cz; pz++)
+ for (int px = cx - 1; px <= cx; px++) nether_populate(px, pz);
+}
+#undef RACK
/* ======================================================================== */
/* The End: ChunkProviderEnd */
@@ -489,6 +697,7 @@ static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out) { (void)cx
void gen_dim(int d) {
gd_dim = d;
+ memset(&U, 0, sizeof U); /* (the fortress kept there, the Overworld's flags) */
if (d == DIM_NETHER) nether_init();
else if (d == DIM_END) end_init();
}
diff --git a/games/numblocks/src/gen_fortress.h b/games/numblocks/src/gen_fortress.h
new file mode 100644
index 00000000..66d90830
--- /dev/null
+++ b/games/numblocks/src/gen_fortress.h
@@ -0,0 +1,654 @@
+/* NumBlocks.nwa only (NB_DIMS), included by gen_dims.h: Nether fortresses (MapGenNetherBridge,
+ * StructureNetherBridgePieces), laid out as 1.8.8 lays them, piece by piece from the same random, then
+ * built chunk by chunk.
+ *
+ * A fortress's layout is kept in the scratch union U while the player is in the Nether (gen.c's
+ * Overworld does not run there): the last one laid out, as the chunks around one are made one after
+ * the other. */
+
+enum { FP_STRAIGHT, FP_CROSSING3, FP_CROSSING, FP_STAIRS, FP_THRONE, FP_ENTRANCE, FP_CORRIDOR5, FP_CROSSING2,
+ FP_CORRIDOR2, FP_CORRIDOR, FP_CORRIDOR3, FP_CORRIDOR4, FP_STALK, FP_END };
+enum { F_N, F_E, F_S, F_W }; /* EnumFacing.Plane.HORIZONTAL's order */
+static inline int iabs(int v) { return v < 0 ? -v : v; }
+
+typedef struct {
+ int16_t x0, y0, z0, x1, y1, z1;
+ uint8_t type, face, flag, depth; /* flag: a corridor's chest; depth: getComponentType */
+ i32 seed; /* an End's fillSeed */
+} FPiece;
+
+#define FMAX 232
+typedef struct {
+ int sx, sz, ok; /* the start's chunk */
+ int n, npend, last; /* pieces; waiting to build; the last weight placed (a type, or -1) */
+ FPiece p[FMAX];
+ uint8_t pend[FMAX];
+ uint8_t wl[2][7], wn[2], cnt[FP_END]; /* the weights left (types), how many each placed */
+ int16_t bb[6]; /* all of it */
+} Fort;
+#define FT ((Fort *)(void *)&U)
+
+/* PieceWeight: weight, most placed (0: any), may follow itself */
+static const uint8_t fw_weight[FP_END] = {30, 10, 10, 10, 5, 5, 25, 15, 5, 5, 10, 7, 5};
+static const uint8_t fw_max[FP_END] = {0, 4, 4, 3, 2, 1, 0, 5, 10, 10, 3, 2, 2};
+static const uint8_t fw_row[FP_END] = {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0};
+/* each piece's box: offset and size along its facing (StructureBoundingBox.getComponentToAddBoundingBox) */
+static const int8_t fp_box[FP_END + 1][6] = {
+ {-1, -3, 0, 5, 10, 19}, {-8, -3, 0, 19, 10, 19}, {-2, 0, 0, 7, 9, 7}, {-2, 0, 0, 7, 11, 7},
+ {-2, 0, 0, 7, 8, 9}, {-5, -3, 0, 13, 14, 13}, {-1, 0, 0, 5, 7, 5}, {-1, 0, 0, 5, 7, 5},
+ {-1, 0, 0, 5, 7, 5}, {-1, 0, 0, 5, 7, 5}, {-1, -7, 0, 5, 14, 10}, {-3, 0, 0, 9, 7, 9},
+ {-5, -3, 0, 13, 14, 13}, {-1, -3, 0, 5, 10, 8}};
+
+static void box_for(int x, int y, int z, const int8_t *b, int face, int16_t *o) {
+ int ox = b[0], oy = b[1], oz = b[2], sx = b[3], sy = b[4], sz = b[5];
+ switch (face) {
+ case F_N: o[0] = x + ox, o[1] = y + oy, o[2] = z - sz + 1 + oz, o[3] = x + sx - 1 + ox, o[4] = y + sy - 1 + oy, o[5] = z + oz; break;
+ case F_S: o[0] = x + ox, o[1] = y + oy, o[2] = z + oz, o[3] = x + sx - 1 + ox, o[4] = y + sy - 1 + oy, o[5] = z + sz - 1 + oz; break;
+ case F_W: o[0] = x - sz + 1 + oz, o[1] = y + oy, o[2] = z + ox, o[3] = x + oz, o[4] = y + sy - 1 + oy, o[5] = z + sx - 1 + ox; break;
+ default: o[0] = x + oz, o[1] = y + oy, o[2] = z + ox, o[3] = x + sz - 1 + oz, o[4] = y + sy - 1 + oy, o[5] = z + sx - 1 + ox; break;
+ }
+}
+static int boxes_meet(const int16_t *a, const FPiece *p) {
+ return a[3] >= p->x0 && a[0] <= p->x1 && a[5] >= p->z0 && a[2] <= p->z1 && a[4] >= p->y0 && a[1] <= p->y1;
+}
+
+/* the piece of a type there, if its box is above y 10 and free (each one's func_175xxx_a); -1 */
+static int fp_new(JRand *r, int type, int x, int y, int z, int face, int depth) {
+ Fort *f = FT;
+ int16_t b[6];
+ box_for(x, y, z, fp_box[type], face, b);
+ if (b[1] <= 10) return -1;
+ for (int i = 0; i < f->n; i++)
+ if (boxes_meet(b, &f->p[i])) return -1;
+ if (f->n >= FMAX) return -1;
+ FPiece *p = &f->p[f->n];
+ p->x0 = b[0], p->y0 = b[1], p->z0 = b[2], p->x1 = b[3], p->y1 = b[4], p->z1 = b[5];
+ p->type = (uint8_t)type, p->face = (uint8_t)face, p->depth = (uint8_t)depth, p->flag = 0, p->seed = 0;
+ if (type == FP_END) p->seed = jr_next(r, 32);
+ if (type == FP_CORRIDOR2 || type == FP_CORRIDOR) p->flag = jr_int(r, 3) == 0;
+ return f->n++;
+}
+
+/* Piece.func_175871_a: a piece of the weights left (five tries), else an end */
+static int fp_pick(JRand *r, int sec, int x, int y, int z, int face, int depth) {
+ Fort *f = FT;
+ int total = 0, any = 0;
+ for (int i = 0; i < f->wn[sec]; i++) {
+ int t = f->wl[sec][i];
+ if (fw_max[t] > 0 && f->cnt[t] < fw_max[t]) any = 1;
+ total += fw_weight[t];
+ }
+ if (!any) total = -1;
+ int ok = total > 0 && depth <= 30;
+ for (int j = 0; j < 5 && ok; j++) {
+ int k = jr_int(r, total);
+ for (int i = 0; i < f->wn[sec]; i++) {
+ int t = f->wl[sec][i];
+ k -= fw_weight[t];
+ if (k < 0) {
+ if (!(fw_max[t] == 0 || f->cnt[t] < fw_max[t]) || (t == f->last && !fw_row[t])) break;
+ int p = fp_new(r, t, x, y, z, face, depth);
+ if (p >= 0) {
+ f->cnt[t]++;
+ f->last = t;
+ if (!(fw_max[t] == 0 || f->cnt[t] < fw_max[t])) {
+ memmove(&f->wl[sec][i], &f->wl[sec][i + 1], (size_t)(f->wn[sec] - i - 1));
+ f->wn[sec]--;
+ }
+ return p;
+ }
+ }
+ }
+ }
+ return fp_new(r, FP_END, x, y, z, face, depth);
+}
+
+/* Piece.func_175870_a: the next piece there, within 112 of the start (else an end, left out of the list:
+ * as 1.8.8 does), waiting to be built */
+static void fp_next(JRand *r, int sec, int x, int y, int z, int face, int depth) {
+ Fort *f = FT;
+ if (iabs(x - f->p[0].x0) <= 112 && iabs(z - f->p[0].z0) <= 112) {
+ int p = fp_pick(r, sec, x, y, z, face, depth + 1);
+ if (p >= 0 && f->npend < FMAX) f->pend[f->npend++] = (uint8_t)p;
+ } else {
+ int16_t b[6]; /* (an end made and dropped: only its draw counts) */
+ box_for(x, y, z, fp_box[FP_END], face, b);
+ int free = b[1] > 10;
+ for (int i = 0; i < f->n && free; i++)
+ if (boxes_meet(b, &f->p[i])) free = 0;
+ if (free) jr_next(r, 32);
+ }
+}
+/* getNextComponentNormal, X (to the left) and Z (to the right) */
+static void fp_normal(JRand *r, int pi, int ox, int oy, int sec) {
+ FPiece p = FT->p[pi];
+ switch (p.face) {
+ case F_N: fp_next(r, sec, p.x0 + ox, p.y0 + oy, p.z0 - 1, F_N, p.depth); break;
+ case F_S: fp_next(r, sec, p.x0 + ox, p.y0 + oy, p.z1 + 1, F_S, p.depth); break;
+ case F_W: fp_next(r, sec, p.x0 - 1, p.y0 + oy, p.z0 + ox, F_W, p.depth); break;
+ default: fp_next(r, sec, p.x1 + 1, p.y0 + oy, p.z0 + ox, F_E, p.depth); break;
+ }
+}
+static void fp_left(JRand *r, int pi, int oy, int ox, int sec) {
+ FPiece p = FT->p[pi];
+ if (p.face == F_N || p.face == F_S) fp_next(r, sec, p.x0 - 1, p.y0 + oy, p.z0 + ox, F_W, p.depth);
+ else fp_next(r, sec, p.x0 + ox, p.y0 + oy, p.z0 - 1, F_N, p.depth);
+}
+static void fp_right(JRand *r, int pi, int oy, int ox, int sec) {
+ FPiece p = FT->p[pi];
+ if (p.face == F_N || p.face == F_S) fp_next(r, sec, p.x1 + 1, p.y0 + oy, p.z0 + ox, F_E, p.depth);
+ else fp_next(r, sec, p.x0 + ox, p.y0 + oy, p.z1 + 1, F_S, p.depth);
+}
+/* buildComponent */
+static void fp_build(JRand *r, int pi) {
+ switch (FT->p[pi].type) {
+ case FP_CROSSING3: fp_normal(r, pi, 8, 3, 0), fp_left(r, pi, 3, 8, 0), fp_right(r, pi, 3, 8, 0); break;
+ case FP_STRAIGHT: fp_normal(r, pi, 1, 3, 0); break;
+ case FP_CROSSING: fp_normal(r, pi, 2, 0, 0), fp_left(r, pi, 0, 2, 0), fp_right(r, pi, 0, 2, 0); break;
+ case FP_STAIRS: fp_right(r, pi, 6, 2, 0); break;
+ case FP_ENTRANCE: fp_normal(r, pi, 5, 3, 1); break;
+ case FP_CORRIDOR5: fp_normal(r, pi, 1, 0, 1); break;
+ case FP_CROSSING2: fp_normal(r, pi, 1, 0, 1), fp_left(r, pi, 0, 1, 1), fp_right(r, pi, 0, 1, 1); break;
+ case FP_CORRIDOR2: fp_right(r, pi, 0, 1, 1); break;
+ case FP_CORRIDOR: fp_left(r, pi, 0, 1, 1); break;
+ case FP_CORRIDOR3: fp_normal(r, pi, 1, 0, 1); break;
+ case FP_CORRIDOR4: {
+ int f = FT->p[pi].face, i = f == F_W || f == F_N ? 5 : 1;
+ int s1 = jr_int(r, 8) > 0;
+ fp_left(r, pi, 0, i, s1);
+ int s2 = jr_int(r, 8) > 0;
+ fp_right(r, pi, 0, i, s2);
+ break;
+ }
+ case FP_STALK: fp_normal(r, pi, 5, 3, 1), fp_normal(r, pi, 5, 11, 1); break;
+ }
+}
+
+/* MapGenNetherBridge.canSpawnStructureAtCoords: the start of the region of 16 x 16 chunks (rx, rz), if
+ * it has one (one in three), and the random as it leaves it */
+static int fort_start(int rx, int rz, int *sx, int *sz, JRand *r) {
+ jr_seed(r, (i64)(i32)(rx ^ (i32)((u32)rz << 4)) ^ g_seed);
+ jr_next(r, 32);
+ if (jr_int(r, 3) != 0) return 0;
+ *sx = (rx << 4) + 4 + jr_int(r, 8);
+ *sz = (rz << 4) + 4 + jr_int(r, 8);
+ return 1;
+}
+
+/* MapGenNetherBridge.Start: the layout of the fortress starting in chunk (sx, sz) */
+static void fort_layout(int sx, int sz, JRand *r) {
+ Fort *f = FT;
+ if (f->ok && f->sx == sx && f->sz == sz) return;
+ f->ok = 1, f->sx = sx, f->sz = sz, f->n = 0, f->npend = 0, f->last = -1;
+ static const uint8_t prim[6] = {FP_STRAIGHT, FP_CROSSING3, FP_CROSSING, FP_STAIRS, FP_THRONE, FP_ENTRANCE};
+ static const uint8_t sec[7] = {FP_CORRIDOR5, FP_CROSSING2, FP_CORRIDOR2, FP_CORRIDOR, FP_CORRIDOR3, FP_CORRIDOR4, FP_STALK};
+ memcpy(f->wl[0], prim, 6), memcpy(f->wl[1], sec, 7);
+ f->wn[0] = 6, f->wn[1] = 7;
+ memset(f->cnt, 0, sizeof f->cnt);
+ /* the start: a bridge crossing turned at random */
+ int x = (sx << 4) + 2, z = (sz << 4) + 2;
+ FPiece *s = &f->p[f->n++];
+ s->type = FP_CROSSING3, s->face = (uint8_t)jr_int(r, 4), s->depth = 0, s->flag = 0, s->seed = 0;
+ s->x0 = (int16_t)x, s->y0 = 64, s->z0 = (int16_t)z, s->x1 = (int16_t)(x + 18), s->y1 = 73, s->z1 = (int16_t)(z + 18);
+ fp_build(r, 0);
+ while (f->npend) {
+ int i = jr_int(r, f->npend), p = f->pend[i];
+ memmove(&f->pend[i], &f->pend[i + 1], (size_t)(f->npend - i - 1));
+ f->npend--;
+ fp_build(r, p);
+ }
+ /* updateBoundingBox, then setRandomHeight(48, 70) */
+ int16_t *b = f->bb;
+ b[0] = b[1] = b[2] = 32767, b[3] = b[4] = b[5] = -32768;
+ for (int i = 0; i < f->n; i++) {
+ const FPiece *p = &f->p[i];
+ if (p->x0 < b[0]) b[0] = p->x0;
+ if (p->y0 < b[1]) b[1] = p->y0;
+ if (p->z0 < b[2]) b[2] = p->z0;
+ if (p->x1 > b[3]) b[3] = p->x1;
+ if (p->y1 > b[4]) b[4] = p->y1;
+ if (p->z1 > b[5]) b[5] = p->z1;
+ }
+ int room = 70 - 48 + 1 - (b[4] - b[1] + 1), y = room > 1 ? 48 + jr_int(r, room) : 48, k = y - b[1];
+ b[1] += k, b[4] += k;
+ for (int i = 0; i < f->n; i++) f->p[i].y0 += k, f->p[i].y1 += k;
+}
+
+/* the fortress whose pieces may reach chunk (cx, cz), laid out; 0 if none */
+static int fort_near(int cx, int cz) {
+ for (int rz = (cz - 9) >> 4; rz <= (cz + 9) >> 4; rz++)
+ for (int rx = (cx - 9) >> 4; rx <= (cx + 9) >> 4; rx++) {
+ int sx, sz;
+ JRand r;
+ if (!fort_start(rx, rz, &sx, &sz, &r) || iabs(sx - cx) > 9 || iabs(sz - cz) > 9) continue;
+ fort_layout(sx, sz, &r);
+ const int16_t *b = FT->bb;
+ if (b[3] >= cx * 16 && b[0] <= cx * 16 + 15 && b[5] >= cz * 16 && b[2] <= cz * 16 + 15) return 1;
+ }
+ return 0;
+}
+
+/* ---- building a piece into the slab of chunk (cx, cz), rows [y0, y0 + h) */
+typedef struct {
+ const FPiece *p;
+ int cx, cz, y0, h;
+ uint8_t *out;
+} FBuild;
+static int fb_x(const FPiece *p, int x, int z) { return p->face == F_W ? p->x1 - z : p->face == F_E ? p->x0 + z : p->x0 + x; }
+static int fb_z(const FPiece *p, int x, int z) { return p->face == F_N ? p->z1 - z : p->face == F_S ? p->z0 + z : p->z0 + x; }
+static uint8_t *fb_at(const FBuild *b, int wx, int wy, int wz) {
+ int lx = wx - b->cx * 16, lz = wz - b->cz * 16, ly = wy - b->y0;
+ if ((unsigned)lx > 15 || (unsigned)lz > 15 || ly < 0 || ly >= b->h || wy < 1) return NULL;
+ return &b->out[(ly * 16 + lz) * 16 + lx];
+}
+static void fb_set(const FBuild *b, int x, int y, int z, uint8_t v) {
+ uint8_t *c = fb_at(b, fb_x(b->p, x, z), b->p->y0 + y, fb_z(b->p, x, z));
+ if (c) *c = v;
+}
+/* fillWithBlocks (the same block on the edges and inside) */
+static void fb_fill(const FBuild *b, int x0, int y0, int z0, int x1, int y1, int z1, uint8_t v) {
+ for (int y = y0; y <= y1; y++)
+ for (int x = x0; x <= x1; x++)
+ for (int z = z0; z <= z1; z++) fb_set(b, x, y, z, v);
+}
+#define NB GEN_CODE(GC_NETHER_BRICK)
+#define FENCE GEN_CODE(GC_FENCE)
+static int nq(int x, int y, int z); /* (below: the terrain, without caves) */
+/* replaceAirAndLiquidDownwards: a pillar down to the ground (here, the terrain without its caves, or another
+ * piece) */
+static void fb_down(const FBuild *b, int x, int y, int z) {
+ const FPiece *p = b->p;
+ int wx = fb_x(p, x, z), wy = p->y0 + y, wz = fb_z(p, x, z);
+ if (wx < b->cx * 16 || wx > b->cx * 16 + 15 || wz < b->cz * 16 || wz > b->cz * 16 + 15) return;
+ const Fort *f = FT;
+ int16_t under[8][2]; /* the other pieces over this column: their heights */
+ int nu = 0;
+ for (int i = 0; i < f->n && nu < 8; i++) {
+ const FPiece *o = &f->p[i];
+ if (o != p && wx >= o->x0 && wx <= o->x1 && wz >= o->z0 && wz <= o->z1 && o->y0 < wy)
+ under[nu][0] = o->y0, under[nu++][1] = o->y1;
+ }
+ for (; wy > 1; wy--) {
+ int t = nq(wx, wy, wz);
+ if (t != B_AIR && t != B_LAVA) return;
+ for (int i = 0; i < nu; i++)
+ if (wy >= under[i][0] && wy <= under[i][1]) return;
+ uint8_t *c = fb_at(b, wx, wy, wz);
+ if (c) *c = NB;
+ }
+}
+/* getMetadataWithOffset for stairs (0 east, 1 west, 2 south, 3 north), as the code */
+static uint8_t fb_stairs(const FPiece *p, int m) {
+ static const uint8_t rot[4][4] = {{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 3, 2}, {2, 3, 0, 1}}; /* N, -, S, W */
+ static const uint8_t east[4] = {2, 3, 1, 0};
+ int k = p->face == F_E ? east[m] : rot[p->face][m];
+ return GEN_CODE(GC_STAIRS_E + k);
+}
+/* the castle's rooms with the walls of fences on top (the entrance, the wart room) */
+static void fb_castle(const FBuild *b) {
+ fb_fill(b, 0, 3, 0, 12, 4, 12, NB);
+ fb_fill(b, 0, 5, 0, 12, 13, 12, B_AIR);
+ fb_fill(b, 0, 5, 0, 1, 12, 12, NB);
+ fb_fill(b, 11, 5, 0, 12, 12, 12, NB);
+ fb_fill(b, 2, 5, 11, 4, 12, 12, NB);
+ fb_fill(b, 8, 5, 11, 10, 12, 12, NB);
+ fb_fill(b, 5, 9, 11, 7, 12, 12, NB);
+ fb_fill(b, 2, 5, 0, 4, 12, 1, NB);
+ fb_fill(b, 8, 5, 0, 10, 12, 1, NB);
+ fb_fill(b, 5, 9, 0, 7, 12, 1, NB);
+ fb_fill(b, 2, 11, 2, 10, 12, 10, NB);
+}
+static void fb_castle_top(const FBuild *b) {
+ for (int i = 1; i <= 11; i += 2) {
+ fb_fill(b, i, 10, 0, i, 11, 0, FENCE);
+ fb_fill(b, i, 10, 12, i, 11, 12, FENCE);
+ fb_fill(b, 0, 10, i, 0, 11, i, FENCE);
+ fb_fill(b, 12, 10, i, 12, 11, i, FENCE);
+ fb_set(b, i, 13, 0, NB);
+ fb_set(b, i, 13, 12, NB);
+ fb_set(b, 0, 13, i, NB);
+ fb_set(b, 12, 13, i, NB);
+ fb_set(b, i + 1, 13, 0, FENCE);
+ fb_set(b, i + 1, 13, 12, FENCE);
+ fb_set(b, 0, 13, i + 1, FENCE);
+ fb_set(b, 12, 13, i + 1, FENCE);
+ }
+ fb_set(b, 0, 13, 0, FENCE);
+ fb_set(b, 0, 13, 12, FENCE);
+ fb_set(b, 12, 13, 0, FENCE);
+ for (int j = 3; j <= 9; j += 2) {
+ fb_fill(b, 1, 7, j, 1, 8, j, FENCE);
+ fb_fill(b, 11, 7, j, 11, 8, j, FENCE);
+ }
+}
+static void fb_castle_base(const FBuild *b) {
+ fb_fill(b, 4, 2, 0, 8, 2, 12, NB);
+ fb_fill(b, 0, 2, 4, 12, 2, 8, NB);
+ fb_fill(b, 4, 0, 0, 8, 1, 3, NB);
+ fb_fill(b, 4, 0, 9, 8, 1, 12, NB);
+ fb_fill(b, 0, 0, 4, 3, 1, 8, NB);
+ fb_fill(b, 9, 0, 4, 12, 1, 8, NB);
+ for (int k = 4; k <= 8; k++)
+ for (int l = 0; l <= 2; l++) fb_down(b, k, -1, l), fb_down(b, k, -1, 12 - l);
+ for (int i = 0; i <= 2; i++)
+ for (int j = 4; j <= 8; j++) fb_down(b, i, -1, j), fb_down(b, 12 - i, -1, j);
+}
+/* a small corridor's floor, room and roof (5 x 7 x 5) */
+static void fb_small(const FBuild *b) {
+ fb_fill(b, 0, 0, 0, 4, 1, 4, NB);
+ fb_fill(b, 0, 2, 0, 4, 5, 4, B_AIR);
+}
+static void fb_small_end(const FBuild *b) {
+ fb_fill(b, 0, 6, 0, 4, 6, 4, NB);
+ for (int i = 0; i <= 4; i++)
+ for (int j = 0; j <= 4; j++) fb_down(b, i, -1, j);
+}
+
+/* addComponentParts */
+static void fp_parts(const FBuild *b) {
+ const FPiece *p = b->p;
+ switch (p->type) {
+ case FP_CROSSING3:
+ fb_fill(b, 7, 3, 0, 11, 4, 18, NB);
+ fb_fill(b, 0, 3, 7, 18, 4, 11, NB);
+ fb_fill(b, 8, 5, 0, 10, 7, 18, B_AIR);
+ fb_fill(b, 0, 5, 8, 18, 7, 10, B_AIR);
+ fb_fill(b, 7, 5, 0, 7, 5, 7, NB);
+ fb_fill(b, 7, 5, 11, 7, 5, 18, NB);
+ fb_fill(b, 11, 5, 0, 11, 5, 7, NB);
+ fb_fill(b, 11, 5, 11, 11, 5, 18, NB);
+ fb_fill(b, 0, 5, 7, 7, 5, 7, NB);
+ fb_fill(b, 11, 5, 7, 18, 5, 7, NB);
+ fb_fill(b, 0, 5, 11, 7, 5, 11, NB);
+ fb_fill(b, 11, 5, 11, 18, 5, 11, NB);
+ fb_fill(b, 7, 2, 0, 11, 2, 5, NB);
+ fb_fill(b, 7, 2, 13, 11, 2, 18, NB);
+ fb_fill(b, 7, 0, 0, 11, 1, 3, NB);
+ fb_fill(b, 7, 0, 15, 11, 1, 18, NB);
+ for (int i = 7; i <= 11; i++)
+ for (int j = 0; j <= 2; j++) fb_down(b, i, -1, j), fb_down(b, i, -1, 18 - j);
+ fb_fill(b, 0, 2, 7, 5, 2, 11, NB);
+ fb_fill(b, 13, 2, 7, 18, 2, 11, NB);
+ fb_fill(b, 0, 0, 7, 3, 1, 11, NB);
+ fb_fill(b, 15, 0, 7, 18, 1, 11, NB);
+ for (int k = 0; k <= 2; k++)
+ for (int l = 7; l <= 11; l++) fb_down(b, k, -1, l), fb_down(b, 18 - k, -1, l);
+ break;
+ case FP_STRAIGHT:
+ fb_fill(b, 0, 3, 0, 4, 4, 18, NB);
+ fb_fill(b, 1, 5, 0, 3, 7, 18, B_AIR);
+ fb_fill(b, 0, 5, 0, 0, 5, 18, NB);
+ fb_fill(b, 4, 5, 0, 4, 5, 18, NB);
+ fb_fill(b, 0, 2, 0, 4, 2, 5, NB);
+ fb_fill(b, 0, 2, 13, 4, 2, 18, NB);
+ fb_fill(b, 0, 0, 0, 4, 1, 3, NB);
+ fb_fill(b, 0, 0, 15, 4, 1, 18, NB);
+ for (int i = 0; i <= 4; i++)
+ for (int j = 0; j <= 2; j++) fb_down(b, i, -1, j), fb_down(b, i, -1, 18 - j);
+ fb_fill(b, 0, 1, 1, 0, 4, 1, FENCE);
+ fb_fill(b, 0, 3, 4, 0, 4, 4, FENCE);
+ fb_fill(b, 0, 3, 14, 0, 4, 14, FENCE);
+ fb_fill(b, 0, 1, 17, 0, 4, 17, FENCE);
+ fb_fill(b, 4, 1, 1, 4, 4, 1, FENCE);
+ fb_fill(b, 4, 3, 4, 4, 4, 4, FENCE);
+ fb_fill(b, 4, 3, 14, 4, 4, 14, FENCE);
+ fb_fill(b, 4, 1, 17, 4, 4, 17, FENCE);
+ break;
+ case FP_CROSSING:
+ fb_fill(b, 0, 0, 0, 6, 1, 6, NB);
+ fb_fill(b, 0, 2, 0, 6, 7, 6, B_AIR);
+ fb_fill(b, 0, 2, 0, 1, 6, 0, NB);
+ fb_fill(b, 0, 2, 6, 1, 6, 6, NB);
+ fb_fill(b, 5, 2, 0, 6, 6, 0, NB);
+ fb_fill(b, 5, 2, 6, 6, 6, 6, NB);
+ fb_fill(b, 0, 2, 0, 0, 6, 1, NB);
+ fb_fill(b, 0, 2, 5, 0, 6, 6, NB);
+ fb_fill(b, 6, 2, 0, 6, 6, 1, NB);
+ fb_fill(b, 6, 2, 5, 6, 6, 6, NB);
+ fb_fill(b, 2, 6, 0, 4, 6, 0, NB);
+ fb_fill(b, 2, 5, 0, 4, 5, 0, FENCE);
+ fb_fill(b, 2, 6, 6, 4, 6, 6, NB);
+ fb_fill(b, 2, 5, 6, 4, 5, 6, FENCE);
+ fb_fill(b, 0, 6, 2, 0, 6, 4, NB);
+ fb_fill(b, 0, 5, 2, 0, 5, 4, FENCE);
+ fb_fill(b, 6, 6, 2, 6, 6, 4, NB);
+ fb_fill(b, 6, 5, 2, 6, 5, 4, FENCE);
+ for (int i = 0; i <= 6; i++)
+ for (int j = 0; j <= 6; j++) fb_down(b, i, -1, j);
+ break;
+ case FP_STAIRS:
+ fb_fill(b, 0, 0, 0, 6, 1, 6, NB);
+ fb_fill(b, 0, 2, 0, 6, 10, 6, B_AIR);
+ fb_fill(b, 0, 2, 0, 1, 8, 0, NB);
+ fb_fill(b, 5, 2, 0, 6, 8, 0, NB);
+ fb_fill(b, 0, 2, 1, 0, 8, 6, NB);
+ fb_fill(b, 6, 2, 1, 6, 8, 6, NB);
+ fb_fill(b, 1, 2, 6, 5, 8, 6, NB);
+ fb_fill(b, 0, 3, 2, 0, 5, 4, FENCE);
+ fb_fill(b, 6, 3, 2, 6, 5, 2, FENCE);
+ fb_fill(b, 6, 3, 4, 6, 5, 4, FENCE);
+ fb_set(b, 5, 2, 5, NB);
+ fb_fill(b, 4, 2, 5, 4, 3, 5, NB);
+ fb_fill(b, 3, 2, 5, 3, 4, 5, NB);
+ fb_fill(b, 2, 2, 5, 2, 5, 5, NB);
+ fb_fill(b, 1, 2, 5, 1, 6, 5, NB);
+ fb_fill(b, 1, 7, 1, 5, 7, 4, NB);
+ fb_fill(b, 6, 8, 2, 6, 8, 4, B_AIR);
+ fb_fill(b, 2, 6, 0, 4, 8, 0, NB);
+ fb_fill(b, 2, 5, 0, 4, 5, 0, FENCE);
+ for (int i = 0; i <= 6; i++)
+ for (int j = 0; j <= 6; j++) fb_down(b, i, -1, j);
+ break;
+ case FP_THRONE:
+ fb_fill(b, 0, 2, 0, 6, 7, 7, B_AIR);
+ fb_fill(b, 1, 0, 0, 5, 1, 7, NB);
+ fb_fill(b, 1, 2, 1, 5, 2, 7, NB);
+ fb_fill(b, 1, 3, 2, 5, 3, 7, NB);
+ fb_fill(b, 1, 4, 3, 5, 4, 7, NB);
+ fb_fill(b, 1, 2, 0, 1, 4, 2, NB);
+ fb_fill(b, 5, 2, 0, 5, 4, 2, NB);
+ fb_fill(b, 1, 5, 2, 1, 5, 3, NB);
+ fb_fill(b, 5, 5, 2, 5, 5, 3, NB);
+ fb_fill(b, 0, 5, 3, 0, 5, 8, NB);
+ fb_fill(b, 6, 5, 3, 6, 5, 8, NB);
+ fb_fill(b, 1, 5, 8, 5, 5, 8, NB);
+ fb_set(b, 1, 6, 3, FENCE);
+ fb_set(b, 5, 6, 3, FENCE);
+ fb_fill(b, 0, 6, 3, 0, 6, 8, FENCE);
+ fb_fill(b, 6, 6, 3, 6, 6, 8, FENCE);
+ fb_fill(b, 1, 6, 8, 5, 7, 8, FENCE);
+ fb_fill(b, 2, 8, 8, 4, 8, 8, FENCE);
+ fb_set(b, 3, 5, 5, B_MOB_SPAWNER); /* (blazes) */
+ for (int i = 0; i <= 6; i++)
+ for (int j = 0; j <= 6; j++) fb_down(b, i, -1, j);
+ break;
+ case FP_ENTRANCE:
+ fb_castle(b);
+ fb_fill(b, 5, 8, 0, 7, 8, 0, FENCE);
+ fb_castle_top(b);
+ fb_castle_base(b);
+ fb_fill(b, 5, 5, 5, 7, 5, 7, NB);
+ fb_fill(b, 6, 1, 6, 6, 4, 6, B_AIR);
+ fb_set(b, 6, 0, 6, NB);
+ fb_set(b, 6, 5, 6, B_LAVA);
+ break;
+ case FP_CORRIDOR5:
+ fb_small(b);
+ fb_fill(b, 0, 2, 0, 0, 5, 4, NB);
+ fb_fill(b, 4, 2, 0, 4, 5, 4, NB);
+ fb_fill(b, 0, 3, 1, 0, 4, 1, FENCE);
+ fb_fill(b, 0, 3, 3, 0, 4, 3, FENCE);
+ fb_fill(b, 4, 3, 1, 4, 4, 1, FENCE);
+ fb_fill(b, 4, 3, 3, 4, 4, 3, FENCE);
+ fb_small_end(b);
+ break;
+ case FP_CROSSING2:
+ fb_small(b);
+ fb_fill(b, 0, 2, 0, 0, 5, 0, NB);
+ fb_fill(b, 4, 2, 0, 4, 5, 0, NB);
+ fb_fill(b, 0, 2, 4, 0, 5, 4, NB);
+ fb_fill(b, 4, 2, 4, 4, 5, 4, NB);
+ fb_small_end(b);
+ break;
+ case FP_CORRIDOR2:
+ fb_small(b);
+ fb_fill(b, 0, 2, 0, 0, 5, 4, NB);
+ fb_fill(b, 0, 3, 1, 0, 4, 1, FENCE);
+ fb_fill(b, 0, 3, 3, 0, 4, 3, FENCE);
+ fb_fill(b, 4, 2, 0, 4, 5, 0, NB);
+ fb_fill(b, 1, 2, 4, 4, 5, 4, NB);
+ fb_fill(b, 1, 3, 4, 1, 4, 4, FENCE);
+ fb_fill(b, 3, 3, 4, 3, 4, 4, FENCE);
+ if (p->flag) fb_set(b, 1, 2, 3, B_CHEST);
+ fb_small_end(b);
+ break;
+ case FP_CORRIDOR:
+ fb_small(b);
+ fb_fill(b, 4, 2, 0, 4, 5, 4, NB);
+ fb_fill(b, 4, 3, 1, 4, 4, 1, FENCE);
+ fb_fill(b, 4, 3, 3, 4, 4, 3, FENCE);
+ fb_fill(b, 0, 2, 0, 0, 5, 0, NB);
+ fb_fill(b, 0, 2, 4, 3, 5, 4, NB);
+ fb_fill(b, 1, 3, 4, 1, 4, 4, FENCE);
+ fb_fill(b, 3, 3, 4, 3, 4, 4, FENCE);
+ if (p->flag) fb_set(b, 3, 2, 3, B_CHEST);
+ fb_small_end(b);
+ break;
+ case FP_CORRIDOR3: {
+ uint8_t st = fb_stairs(p, 2);
+ for (int j = 0; j <= 9; j++) {
+ int k = 7 - j > 1 ? 7 - j : 1, l = k + 5 > 14 - j ? k + 5 : 14 - j;
+ if (l > 13) l = 13;
+ fb_fill(b, 0, 0, j, 4, k, j, NB);
+ fb_fill(b, 1, k + 1, j, 3, l - 1, j, B_AIR);
+ if (j <= 6) fb_set(b, 1, k + 1, j, st), fb_set(b, 2, k + 1, j, st), fb_set(b, 3, k + 1, j, st);
+ fb_fill(b, 0, l, j, 4, l, j, NB);
+ fb_fill(b, 0, k + 1, j, 0, l - 1, j, NB);
+ fb_fill(b, 4, k + 1, j, 4, l - 1, j, NB);
+ if (!(j & 1)) fb_fill(b, 0, k + 2, j, 0, k + 3, j, FENCE), fb_fill(b, 4, k + 2, j, 4, k + 3, j, FENCE);
+ for (int i = 0; i <= 4; i++) fb_down(b, i, -1, j);
+ }
+ break;
+ }
+ case FP_CORRIDOR4:
+ fb_fill(b, 0, 0, 0, 8, 1, 8, NB);
+ fb_fill(b, 0, 2, 0, 8, 5, 8, B_AIR);
+ fb_fill(b, 0, 6, 0, 8, 6, 5, NB);
+ fb_fill(b, 0, 2, 0, 2, 5, 0, NB);
+ fb_fill(b, 6, 2, 0, 8, 5, 0, NB);
+ fb_fill(b, 1, 3, 0, 1, 4, 0, FENCE);
+ fb_fill(b, 7, 3, 0, 7, 4, 0, FENCE);
+ fb_fill(b, 0, 2, 4, 8, 2, 8, NB);
+ fb_fill(b, 1, 1, 4, 2, 2, 4, B_AIR);
+ fb_fill(b, 6, 1, 4, 7, 2, 4, B_AIR);
+ fb_fill(b, 0, 3, 8, 8, 3, 8, FENCE);
+ fb_fill(b, 0, 3, 6, 0, 3, 7, FENCE);
+ fb_fill(b, 8, 3, 6, 8, 3, 7, FENCE);
+ fb_fill(b, 0, 3, 4, 0, 5, 5, NB);
+ fb_fill(b, 8, 3, 4, 8, 5, 5, NB);
+ fb_fill(b, 1, 3, 5, 2, 5, 5, NB);
+ fb_fill(b, 6, 3, 5, 7, 5, 5, NB);
+ fb_fill(b, 1, 4, 5, 1, 5, 5, FENCE);
+ fb_fill(b, 7, 4, 5, 7, 5, 5, FENCE);
+ for (int i = 0; i <= 5; i++)
+ for (int j = 0; j <= 8; j++) fb_down(b, j, -1, i);
+ break;
+ case FP_STALK: {
+ fb_castle(b);
+ fb_castle_top(b);
+ uint8_t k1 = fb_stairs(p, 3);
+ for (int j = 0; j <= 6; j++) {
+ int k = j + 4;
+ for (int l = 5; l <= 7; l++) fb_set(b, l, 5 + j, k, k1);
+ if (k >= 5 && k <= 8) fb_fill(b, 5, 5, k, 7, j + 4, k, NB);
+ else if (k >= 9 && k <= 10) fb_fill(b, 5, 8, k, 7, j + 4, k, NB);
+ if (j >= 1) fb_fill(b, 5, 6 + j, k, 7, 9 + j, k, B_AIR);
+ }
+ for (int l = 5; l <= 7; l++) fb_set(b, l, 12, 11, k1);
+ fb_fill(b, 5, 6, 7, 5, 7, 7, FENCE);
+ fb_fill(b, 7, 6, 7, 7, 7, 7, FENCE);
+ fb_fill(b, 5, 13, 12, 7, 13, 12, B_AIR);
+ fb_fill(b, 2, 5, 2, 3, 5, 3, NB);
+ fb_fill(b, 2, 5, 9, 3, 5, 10, NB);
+ fb_fill(b, 9, 5, 2, 10, 5, 3, NB);
+ fb_fill(b, 9, 5, 9, 10, 5, 10, NB);
+ fb_fill(b, 10, 5, 4, 10, 5, 8, NB);
+ uint8_t i2 = fb_stairs(p, 0), j2 = fb_stairs(p, 1);
+ fb_set(b, 4, 5, 2, j2), fb_set(b, 4, 5, 3, j2), fb_set(b, 4, 5, 9, j2), fb_set(b, 4, 5, 10, j2);
+ fb_set(b, 8, 5, 2, i2), fb_set(b, 8, 5, 3, i2), fb_set(b, 8, 5, 9, i2), fb_set(b, 8, 5, 10, i2);
+ fb_fill(b, 3, 4, 4, 4, 4, 8, GEN_CODE(GC_SOUL_SAND));
+ fb_fill(b, 8, 4, 4, 9, 4, 8, GEN_CODE(GC_SOUL_SAND));
+ fb_fill(b, 3, 5, 4, 4, 5, 8, GEN_CODE(GC_WART));
+ fb_fill(b, 8, 5, 4, 9, 5, 8, GEN_CODE(GC_WART));
+ fb_castle_base(b);
+ break;
+ }
+ case FP_END: {
+ JRand r;
+ jr_seed(&r, p->seed);
+ for (int i = 0; i <= 4; i++)
+ for (int j = 3; j <= 4; j++) {
+ int k = jr_int(&r, 8);
+ fb_fill(b, i, j, 0, i, j, k, NB);
+ }
+ int l = jr_int(&r, 8);
+ fb_fill(b, 0, 5, 0, 0, 5, l, NB);
+ l = jr_int(&r, 8);
+ fb_fill(b, 4, 5, 0, 4, 5, l, NB);
+ for (l = 0; l <= 4; l++) {
+ int i1 = jr_int(&r, 5);
+ fb_fill(b, l, 2, 0, l, 2, i1, NB);
+ }
+ for (l = 0; l <= 4; l++)
+ for (int j1 = 0; j1 <= 1; j1++) {
+ int k1 = jr_int(&r, 3);
+ fb_fill(b, l, j1, 0, l, j1, k1, NB);
+ }
+ break;
+ }
+ }
+}
+
+/* the fortress's pieces in chunk (cx, cz), rows [y0, y0 + h) */
+static void fort_build(int cx, int cz, int y0, int h, uint8_t *out) {
+ if (!fort_near(cx, cz)) return;
+ const Fort *f = FT;
+ for (int i = 0; i < f->n; i++) {
+ const FPiece *p = &f->p[i];
+ if (p->x1 < cx * 16 || p->x0 > cx * 16 + 15 || p->z1 < cz * 16 || p->z0 > cz * 16 + 15 || p->y1 + 1 < y0)
+ continue;
+ FBuild b = {p, cx, cz, y0, h, out};
+ fp_parts(&b);
+ }
+}
+
+/* MapGenNetherBridge.func_175795_b, func_175796_a: in one of a fortress's pieces (2), or in its box (1),
+ * of the fortress near (the one last laid out); 0 */
+int gen_fortress_at(int x, int y, int z) {
+ if (gd_dim != DIM_NETHER || !fort_near(x >> 4, z >> 4)) return 0;
+ const Fort *f = FT;
+ const int16_t *b = f->bb;
+ if (x < b[0] || x > b[3] || y < b[1] || y > b[4] || z < b[2] || z > b[5]) return 0;
+ for (int i = 0; i < f->n; i++) {
+ const FPiece *p = &f->p[i];
+ if (x >= p->x0 && x <= p->x1 && y >= p->y0 && y <= p->y1 && z >= p->z0 && z <= p->z1) return 2;
+ }
+ return 1;
+}
+/* (tests) the nearest fortress's start to (x, z), within 4 regions: its middle at the bridges' level */
+int gen_fortress_find(int x, int z, int *out) {
+ int best = -1;
+ for (int rz = (z >> 8) - 2; rz <= (z >> 8) + 2; rz++)
+ for (int rx = (x >> 8) - 2; rx <= (x >> 8) + 2; rx++) {
+ int sx, sz;
+ JRand r;
+ if (!fort_start(rx, rz, &sx, &sz, &r)) continue;
+ fort_layout(sx, sz, &r);
+ const FPiece *p = &FT->p[0];
+ int cx = (p->x0 + p->x1) / 2, cz = (p->z0 + p->z1) / 2, d = iabs(cx - x) + iabs(cz - z);
+ if (best < 0 || d < best) best = d, out[0] = cx, out[1] = p->y0 + 5, out[2] = cz, out[3] = FT->n;
+ }
+ return best >= 0;
+}
+#undef NB
+#undef FENCE
diff --git a/games/numblocks/src/gui.c b/games/numblocks/src/gui.c
index e50c41c2..54abe6e1 100644
--- a/games/numblocks/src/gui.c
+++ b/games/numblocks/src/gui.c
@@ -426,27 +426,41 @@ void *tiles_data(uint32_t *len) {
}
static Chest *chest;
static Furnace *furnace;
+#if NB_DIMS
+/* NumBlocks.nwa: a chest's or furnace's used is 1 + its dimension (NumPlay's NumBlocks only gets the
+ * Overworld's: save.c) */
+#define USED(t) ((t).used == 1 + dim)
+#define USE_TAG ((uint8_t)(1 + dim))
+#include "edits.h"
+static void chest_loot(Chest *c);
+#else
+#define USED(t) ((t).used)
+#define USE_TAG 1
+#endif
static Chest *chest_at(int x, int y, int z, bool make) {
for (int i = 0; i < N_CHESTS; i++)
- if (chests[i].used && chests[i].x == x && chests[i].y == y && chests[i].z == z) return &chests[i];
+ if (USED(chests[i]) && chests[i].x == x && chests[i].y == y && chests[i].z == z) return &chests[i];
if (!make) return NULL;
for (int i = 0; i < N_CHESTS; i++)
if (!chests[i].used) {
memset(&chests[i], 0, sizeof chests[i]);
- chests[i].used = 1, chests[i].x = (int16_t)x, chests[i].y = (uint8_t)y, chests[i].z = (int16_t)z;
+ chests[i].used = USE_TAG, chests[i].x = (int16_t)x, chests[i].y = (uint8_t)y, chests[i].z = (int16_t)z;
+#if NB_DIMS
+ if (dim == DIM_NETHER && !edits_changed(x, y, z)) chest_loot(&chests[i]);
+#endif
return &chests[i];
}
return NULL;
}
static Furnace *furnace_at(int x, int y, int z, bool make) {
for (int i = 0; i < N_FURNACES; i++)
- if (furnaces[i].used && furnaces[i].x == x && furnaces[i].y == y && furnaces[i].z == z) return &furnaces[i];
+ if (USED(furnaces[i]) && furnaces[i].x == x && furnaces[i].y == y && furnaces[i].z == z) return &furnaces[i];
if (!make) return NULL;
for (int i = 0; i < N_FURNACES; i++)
if (!furnaces[i].used) {
memset(&furnaces[i], 0, sizeof furnaces[i]);
- furnaces[i].used = 1, furnaces[i].x = (int16_t)x, furnaces[i].y = (uint8_t)y, furnaces[i].z = (int16_t)z;
+ furnaces[i].used = USE_TAG, furnaces[i].x = (int16_t)x, furnaces[i].y = (uint8_t)y, furnaces[i].z = (int16_t)z;
return &furnaces[i];
}
return NULL;
@@ -463,6 +477,54 @@ void tiles_removed(int x, int y, int z) {
if (s[i].id) ent_drop(s[i].id, item_count(&s[i]), s[i].aux, x + 0.5f, y + 0.5f, z + 0.5f, false);
}
+#if NB_DIMS
+/* the tiles as NumPlay's NumBlocks gets them (in t, a copy): the Overworld's, without what it does not know */
+static void stack_known(Stack *s, int n) {
+ for (int i = 0; i < n; i++)
+ if (s[i].id >= I_END) s[i].id = 0, s[i].aux = 0;
+}
+void tiles_for_numplay(void *t) {
+ Chest *c = (Chest *)t;
+ Furnace *f = (Furnace *)(c + N_CHESTS);
+ for (int i = 0; i < N_CHESTS; i++)
+ if (c[i].used != 1) memset(&c[i], 0, sizeof c[i]);
+ else stack_known(c[i].s, 27);
+ for (int i = 0; i < N_FURNACES; i++)
+ if (f[i].used != 1) memset(&f[i], 0, sizeof f[i]);
+ else stack_known(f[i].s, 3);
+}
+/* the tiles of the Nether and the End (in t, a copy of all) back into free places of these */
+void tiles_merge(const void *t) {
+ const Chest *c = (const Chest *)t;
+ const Furnace *f = (const Furnace *)(c + N_CHESTS);
+ for (int i = 0, j = 0; i < N_CHESTS; i++) {
+ if (c[i].used <= 1) continue;
+ while (j < N_CHESTS && chests[j].used) j++;
+ if (j < N_CHESTS) chests[j] = c[i];
+ }
+ for (int i = 0, j = 0; i < N_FURNACES; i++) {
+ if (f[i].used <= 1) continue;
+ while (j < N_FURNACES && furnaces[j].used) j++;
+ if (j < N_FURNACES) furnaces[j] = f[i];
+ }
+}
+/* a chest the world made (a fortress's): its loot, when first opened (StructureNetherBridgePieces's list;
+ * horse armour and saddles are not in NumBlocks: those draws leave nothing) */
+static void chest_loot(Chest *c) {
+ static const struct { uint16_t id; uint8_t lo, hi, w; } L[] = {
+ {I_DIAMOND, 1, 3, 5}, {I_IRON_INGOT, 1, 5, 5}, {I_GOLD_INGOT, 1, 3, 15}, {I_GOLDEN_SWORD, 1, 1, 5},
+ {I_GOLDEN_CHESTPLATE, 1, 1, 5}, {I_FLINT_AND_STEEL, 1, 1, 5}, {I_NETHER_WART, 3, 7, 5}, {0, 1, 1, 10},
+ {0, 1, 1, 8}, {0, 1, 1, 5}, {0, 1, 1, 3}, {B_OBSIDIAN, 2, 4, 2}};
+ int n = 2 + rnd(4);
+ for (int k = 0; k < n; k++) {
+ int w = rnd(75), i = 0;
+ while (w >= L[i].w) w -= L[i++].w;
+ int count = L[i].lo + rnd(L[i].hi - L[i].lo + 1), slot = rnd(27);
+ if (L[i].id) c->s[slot] = (Stack){L[i].id, (uint16_t)(item_dur(L[i].id) ? 0 : count)};
+ }
+}
+#endif
+
void tiles_forget(int x, int y, int z) {
Chest *c = chest_at(x, y, z, false);
if (c) c->used = 0;
@@ -486,7 +548,7 @@ void gui_tick(void) {
if (chat_age(i) > 1000) chat[i + 1] = (char)((chat_clock - 1000) & 255), chat[i + 2] = (char)((chat_clock - 1000) >> 8);
for (int i = 0; i < N_FURNACES; i++) {
Furnace *f = &furnaces[i];
- if (!f->used) continue;
+ if (!USED(*f)) continue;
bool was = f->burn > 0;
if (f->burn > 0) f->burn--;
if ((f->burn > 0 || f->s[1].id) && f->s[0].id) {
diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.c
index 5e5612e6..8915e3ae 100644
--- a/games/numblocks/src/main.c
+++ b/games/numblocks/src/main.c
@@ -105,6 +105,9 @@ void game_init(void) {
pl.mode = 1, pl.y += BENCH_FALL; /* (timing a fall from that high, in Creative: no harm) */
#endif
loading();
+#if defined(BENCH_DIM) && NB_DIMS
+ dims_go(BENCH_DIM); /* (timing the Nether or the End: through a portal at once) */
+#endif
return;
}
title();
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index 470f3fd6..ba8a64d9 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -1902,9 +1902,13 @@ static void dims_light(void) {
static const float face_shade[4] = {1.0f, 0.8f, 0.6f, 0.5f};
float table[16];
for (int l = 0; l < 16; l++) {
+ /* (the green of the lightmap: no sky light, the sun's brightness 0.2 in the Nether; block light x 1.5) */
float f = 1 - l / 15.0f, t = (1 - f) / (f * 3 + 1);
table[l] = dim == DIM_NETHER ? t * 0.9f + 0.1f : t;
- float bri = dim == DIM_NETHER ? table[l] * 0.96f + 0.03f : 0.25f + 0.75f * t;
+ float b = table[l] * 1.5f, g = b * ((b * 0.6f + 0.4f) * 0.6f + 0.4f), bri;
+ if (dim == DIM_NETHER) bri = table[0] * 0.24f * 0.48f + g, bri = (bri > 1 ? 1 : bri) * 0.96f + 0.03f;
+ else bri = 0.28f + g * 0.75f; /* (the End's, after the first rounding) */
+ bri = (bri > 1 ? 1 : bri) * 0.96f + 0.03f;
for (int s = 0; s < 4; s++) shade[s][l] = shade_blk[s][l] = (int)(bri * face_shade[s] * 32 + 0.5f);
}
int lb = cam_i >= 0 ? vl[cam_i] : 0, l = (lb & 15) > (lb >> 4) ? lb & 15 : lb >> 4;
diff --git a/games/numblocks/src/save.c b/games/numblocks/src/save.c
index d25aea5e..6fd0c7fe 100644
--- a/games/numblocks/src/save.c
+++ b/games/numblocks/src/save.c
@@ -130,6 +130,32 @@ void world_unique_name(const char *base, char *out) {
}
}
+#if NB_DIMS
+/* NumBlocks.nwa: NumPlay's NumBlocks reads the same world record, so the record holds what it knows: the
+ * player in the Overworld (where they last were there), with none of the Nether's and the End's items,
+ * the Overworld's chests and furnaces. The rest is in "nb3rdim.nbx" by it: the dimension the player is
+ * 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 */
+typedef struct {
+ char magic[4];
+ uint32_t time, played; /* the record's: the copy goes with it */
+ int32_t dim;
+ Player pl;
+ DimsState dst;
+} Side; /* then the tiles */
+static void side_of(int s, char *out) {
+ prefix_of(s, out);
+ memcpy(out + 3, "rdim.nbx", 9);
+}
+static void known(Stack *s, int n) {
+ for (int i = 0; i < n; i++)
+ if (s[i].id >= I_END) s[i].id = 0, s[i].aux = 0;
+}
+void tiles_for_numplay(void *t);
+void tiles_merge(const void *t);
+#endif
+
bool save_world(void) {
bool ok = edits_flush();
uint32_t tl;
@@ -145,12 +171,43 @@ bool save_world(void) {
uint8_t *b = (uint8_t *)edits_scratch(n);
if (!b) return false;
memcpy(b, &h, sizeof h);
+#if NB_DIMS
+ {
+ Player p = pl;
+ if (dim != DIM_OVERWORLD) {
+ p.x = dst.ow_x, p.y = dst.ow_y, p.z = dst.ow_z;
+ p.vx = p.vy = p.vz = 0, p.fall = 0, p.fire = 0;
+ }
+ known(p.inv, 36), known(p.armor, 4), known(p.craft, 4), known(&p.cursor, 1);
+ memcpy(b + sizeof h, &p, sizeof p);
+ }
+ memcpy(b + sizeof h + sizeof pl, t, tl);
+ tiles_for_numplay(b + sizeof h + sizeof pl);
+#else
memcpy(b + sizeof h, &pl, sizeof pl);
memcpy(b + sizeof h + sizeof pl, t, tl);
+#endif
char rec[8];
record_of(slot, rec);
ok = plat_save(rec, b, n) && ok;
edits_forget();
+#if NB_DIMS
+ {
+ Side sd;
+ memset(&sd, 0, sizeof sd);
+ memcpy(sd.magic, "NBD1", 4);
+ sd.time = game_time, sd.played = cur_played, sd.dim = dim, sd.pl = pl, sd.dst = dst;
+ char rn[16];
+ side_of(slot, rn);
+ uint8_t *c = (uint8_t *)edits_scratch(sizeof sd + tl);
+ if (c) {
+ memcpy(c, &sd, sizeof sd);
+ memcpy(c + sizeof sd, t, tl);
+ ok = plat_save(rn, c, sizeof sd + tl) && ok;
+ edits_forget();
+ }
+ }
+#endif
copy_write();
return ok;
}
@@ -181,6 +238,26 @@ bool load_world(int s) {
memcpy(&pl, d + hl, sizeof pl);
memcpy(t, d + hl + sizeof pl, tl);
memset(ents, 0, sizeof ents);
+#if NB_DIMS
+ {
+ dim = DIM_OVERWORLD;
+ memset(&dst, 0, sizeof dst);
+ char rn[16];
+ side_of(s, rn);
+ 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);
+ 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);
+ } else
+ tiles_merge(c + sizeof sd);
+ }
+ }
+#endif
char pre[4];
prefix_of(s, pre);
world_new(world_seed, pre);
@@ -203,6 +280,10 @@ void new_world(int s, int64_t seed, int mode, bool cheats, int type, const char
world_cheats = cheats, lan_open = lan_cheats = false, game_rules = GR_DEFAULT, world_type = (uint8_t)type;
memset(&weather, 0, sizeof weather);
rain_str = thunder_str = 0;
+#if NB_DIMS
+ dim = DIM_OVERWORLD;
+ memset(&dst, 0, sizeof dst);
+#endif
world_new(seed, pre);
player_spawn();
pl.mode = (uint8_t)mode;
diff --git a/games/numblocks/tests/play.c b/games/numblocks/tests/play.c
index 8dc1098b..210afb8d 100644
--- a/games/numblocks/tests/play.c
+++ b/games/numblocks/tests/play.c
@@ -38,7 +38,8 @@ int main(int argc, char **argv) {
const char *out = "build/play", *shots = "";
int give = 0, showcase = 0, title_screen = 0, mobs = 0, animals = 0, start_time = -1, torches = 0, water = 0, creative = 0, build = 0, rain = 0;
float hurt = 0;
- int frames = 60, mspf = 50, go_dim = 0, portal = 0;
+ int frames = 60, mspf = 50, go_dim = 0, portal = 0, at = 0, fortress = 0;
+ float at_v[5] = {0};
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--frames") && i + 1 < argc) frames = atoi(argv[++i]);
else if (!strcmp(argv[i], "--ms-per-frame") && i + 1 < argc) mspf = atoi(argv[++i]);
@@ -57,6 +58,9 @@ int main(int argc, char **argv) {
else if (!strcmp(argv[i], "--nether")) go_dim = 1;
else if (!strcmp(argv[i], "--end")) go_dim = 2;
else if (!strcmp(argv[i], "--portal")) portal = 1;
+ else if (!strcmp(argv[i], "--fortress")) go_dim = 1, fortress = 1;
+ else if (!strcmp(argv[i], "--at") && i + 1 < argc) /* x,y,z,yaw,pitch: there, flying (Creative) */
+ at = sscanf(argv[++i], "%f,%f,%f,%f,%f", &at_v[0], &at_v[1], &at_v[2], &at_v[3], &at_v[4]) >= 3;
else if (!strcmp(argv[i], "--storm")) rain = 2;
else if (!strcmp(argv[i], "--time") && i + 1 < argc) start_time = atoi(argv[++i]);
else if (!strcmp(argv[i], "--seed") && i + 1 < argc) {
@@ -125,6 +129,20 @@ int main(int argc, char **argv) {
fire_set(px, fy, pz, 0);
}
if (go_dim) dims_go(go_dim);
+ if (fortress) {
+ /* to the nearest fortress, flying over its first crossing */
+ int f[4];
+ if (gen_fortress_find((int)pl.x, (int)pl.z, f)) {
+ printf("fortress at %d %d %d, %d pieces\n", f[0], f[1], f[2], f[3]);
+ at = 1, at_v[0] = f[0] + 0.5f, at_v[1] = (float)f[1], at_v[2] = f[2] + 0.5f, at_v[3] = pl.yaw, at_v[4] = 20;
+ }
+ }
+ if (at) {
+ pl.x = at_v[0], pl.y = at_v[1], pl.z = at_v[2], pl.yaw = at_v[3], pl.pitch = at_v[4];
+ pl.mode = 1, pl.flying = true;
+ world_finish(pl.x, pl.y, pl.z);
+ camera_reset();
+ }
if (go_dim) printf("at %.2f %.2f %.2f: feet %d head %d below %d (cache y %d)\n", pl.x, pl.y, pl.z, world_get((int)floorf(pl.x), (int)floorf(pl.y), (int)floorf(pl.z)), world_get((int)floorf(pl.x), (int)floorf(pl.y) + 1, (int)floorf(pl.z)), world_get((int)floorf(pl.x), (int)floorf(pl.y) - 1, (int)floorf(pl.z)), vc_y0);
if (rain) {
/* raining (and storming) from the start, for long enough */