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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumBlocks.nwa: Nether and End terrain, Nether portals
Mason3639049014584f9
diff --git a/games/numblocks/src/dims.c b/games/numblocks/src/dims.cindex 18b44ee3..601ee635 100644--- a/games/numblocks/src/dims.c+++ b/games/numblocks/src/dims.c@@ -2,19 +2,342 @@** The player is in one dimension at a time (dim); the block cache, the generator and the edit log* follow it. Each dimension's changes have their own records (edits.c). */+#include <math.h>#include "nb.h"#if NB_DIMS+#include "edits.h"#include "gen.h"int dim;+DimsState dst;+extern uint32_t game_time;-/* what the generators' codes stand for (nb.h) */+/* what the generators' codes stand for (dims.h) */const uint16_t gen_code_block[GC_N] = {B_NETHERRACK, B_SOUL_SAND, B_QUARTZ_ORE, B_NETHER_BRICK, B_NETHER_BRICK_FENCE, B_NETHER_BRICK_STAIRS_E,B_NETHER_BRICK_STAIRS_W, B_NETHER_BRICK_STAIRS_S, B_NETHER_BRICK_STAIRS_N, B_NETHER_WART_0, B_END_STONE,B_END_PORTAL_FRAME, B_END_PORTAL_FRAME_EYE};-void dims_slab(int cx, int cz, int y0, int h, uint8_t *out) { gen_slab(cx, cz, y0, h, out); }-int dims_biome(int x, int z) { return gen_biome(x, z); }-int dims_top(int x, int z) { return gen_top(x, z); }+static inline int ifl(float v) { return (int)floorf(v); }+static inline bool is_portal(int b) { return b == B_PORTAL_X || b == B_PORTAL_Z; }++/* ---------------------------------------------------------------- Nether portals (BlockPortal) */+/* BlockPortal.Size: the frame around a spot, along an axis (0: x, its left east and its right west;+ * 1: z, north and south) */+typedef struct {+ int ax, lx, lz, rx, rz; /* the axis; its left and right steps */+ int x, y, z; /* the bottom left corner inside (none: w 0) */+ int w, h, n; /* width, height, portal blocks inside */+} PSize;+static bool inside(int b) { return b == B_AIR || b == B_FIRE || is_portal(b); } /* func_150857_a */+/* func_180120_a: how far inside goes that way on obsidian, to obsidian (0: not to obsidian) */+static int span(int x, int y, int z, int dx, int dz) {+ int i = 0;+ for (; i < 22; i++)+ if (!inside(world_get(x + dx * i, y, z + dz * i)) || world_get(x + dx * i, y - 1, z + dz * i) != B_OBSIDIAN) break;+ return world_get(x + dx * i, y, z + dz * i) == B_OBSIDIAN ? i : 0;+}+static void psize(PSize *s, int x, int y, int z, int ax) {+ s->ax = ax;+ s->lx = ax ? 0 : 1, s->lz = ax ? -1 : 0;+ s->rx = -s->lx, s->rz = -s->lz;+ s->w = s->h = s->n = 0;+ for (int y0 = y; y > y0 - 21 && y > 0 && inside(world_get(x, y - 1, z));) y--;+ int i = span(x, y, z, s->lx, s->lz) - 1;+ if (i < 0) return;+ s->x = x + s->lx * i, s->y = y, s->z = z + s->lz * i;+ s->w = span(s->x, s->y, s->z, s->rx, s->rz);+ if (s->w < 2 || s->w > 21) {+ s->w = 0;+ return;+ }+ /* func_150858_a: up while each row is inside, obsidian at both ends; then a row of obsidian on top */+ for (s->h = 0; s->h < 21; s->h++)+ for (int k = 0; k < s->w; k++) {+ int bx = s->x + s->rx * k, by = s->y + s->h, bz = s->z + s->rz * k, b = world_get(bx, by, bz);+ if (!inside(b)) goto top;+ if (is_portal(b)) s->n++;+ if (k == 0 && world_get(bx + s->lx, by, bz + s->lz) != B_OBSIDIAN) goto top;+ if (k == s->w - 1 && world_get(bx + s->rx, by, bz + s->rz) != B_OBSIDIAN) goto top;+ }+top:+ for (int k = 0; k < s->w; k++)+ if (world_get(s->x + s->rx * k, s->y + s->h, s->z + s->rz * k) != B_OBSIDIAN) {+ s->h = 0;+ break;+ }+ if (s->h < 3 || s->h > 21) s->w = s->h = 0;+}+static bool psize_ok(const PSize *s) { return s->w >= 2 && s->w <= 21 && s->h >= 3 && s->h <= 21; }++/* BlockPortal.func_176548_d, from BlockFire.onBlockAdded: fire lit inside an empty frame makes the portal+ * (not in the End) */+bool dims_portal_light(int x, int y, int z) {+ if (dim == DIM_END) return false;+ PSize s;+ for (int ax = 0; ax < 2; ax++) {+ psize(&s, x, y, z, ax);+ if (!psize_ok(&s) || s.n) continue;+ for (int k = 0; k < s.w; k++)+ for (int j = 0; j < s.h; j++) world_set(s.x + s.rx * k, s.y + j, s.z + s.rz * k, ax ? B_PORTAL_Z : B_PORTAL_X);+ return true;+ }+ return false;+}++/* BlockPortal.onNeighborBlockChange: a portal block stays while its frame is whole and full; else it+ * goes, and the rest with it (in Minecraft each one's neighbours go in turn) */+static void portal_break(int x, int y, int z, int b) {+ uint16_t st[448];+ int n = 0, ax = b == B_PORTAL_Z;+ world_set(x, y, z, B_AIR);+ st[n++] = (uint16_t)(32 << 8 | 32);+ while (n) {+ int p = st[--n], u = (p >> 8) - 32, v = (p & 255) - 32;+ static const int8_t du[4] = {1, -1, 0, 0}, dv[4] = {0, 0, 1, -1};+ for (int k = 0; k < 4; k++) {+ int nu = u + du[k], nv = v + dv[k];+ int bx = x + (ax ? 0 : nu), by = y + nv, bz = z + (ax ? nu : 0);+ if (nu < -31 || nu > 31 || nv < -31 || nv > 31 || world_get(bx, by, bz) != b) continue;+ world_set(bx, by, bz, B_AIR);+ if (n < 448) st[n++] = (uint16_t)((nu + 32) << 8 | (nv + 32));+ }+ }+}++/* Block.canBlockStay for the dimensions' blocks: -1 for the others */+int dims_can_stay(int b, int x, int y, int z) {+ if (is_portal(b)) {+ PSize s;+ psize(&s, x, y, z, b == B_PORTAL_Z);+ if (!psize_ok(&s) || s.n < s.w * s.h) portal_break(x, y, z, b);+ return 1; /* (gone already, or staying) */+ }+ if (b >= B_NETHER_WART_0 && b <= B_NETHER_WART_3) return world_get(x, y - 1, z) == B_SOUL_SAND;+ return -1;+}++/* ---------------------------------------------------------------- going through */+/* Teleporter.makePortal: a 2 x 3 portal in its frame, on the nearest spot within 16 that has room (ground+ * under 4 x 3, air above, 4 x 4 x 3 (or else 4 x 1) with the frame), else floating on an obsidian ledge at+ * y 70 or more. (The spots are looked for in the rows the cache holds.) */+static int made[3]; /* (where: the search for it reads the records, which a full storage may not have) */+static void make_portal(void) {+ int j = ifl(pl.x), k = ifl(pl.y), l = ifl(pl.z);+ float best = -1;+ int i1 = j, j1 = k, k1 = l, l1 = 0, i2 = rnd(4);+ int ylo = vc_y0 + 1, yhi = vc_y0 + VCY - 5;+ for (int pass = 0; pass < 2 && best < 0; pass++)+ for (int j2 = j - 16; j2 <= j + 16; j2++) {+ float d1 = j2 + 0.5f - pl.x;+ for (int l2 = l - 16; l2 <= l + 16; l2++) {+ float d2 = l2 + 0.5f - pl.z;+ for (int j3 = yhi; j3 >= ylo; j3--) {+ if (world_get(j2, j3, l2) != B_AIR) continue;+ while (j3 > ylo && world_get(j2, j3 - 1, l2) == B_AIR) j3--;+ for (int k3 = i2; k3 < i2 + (pass ? 2 : 4); k3++) {+ int l3 = k3 % 2, i4 = 1 - l3;+ if (!pass && k3 % 4 >= 2) l3 = -l3, i4 = -i4;+ bool ok = true;+ for (int j4 = 0; j4 < (pass ? 1 : 3) && ok; j4++)+ for (int k4 = 0; k4 < 4 && ok; k4++)+ for (int l4 = -1; l4 < 4 && ok; l4++) {+ int bx = j2 + (k4 - 1) * l3 + j4 * i4, by = j3 + l4, bz = l2 + (k4 - 1) * i4 - j4 * l3;+ int b = world_get(bx, by, bz);+ if (l4 < 0 ? !(blk_flags[b] & BF_SOLID) : b != B_AIR) ok = false;+ }+ if (!ok) continue;+ float d5 = j3 + 0.5f - pl.y, d7 = d1 * d1 + d5 * d5 + d2 * d2;+ if (best < 0 || d7 < best) best = d7, i1 = j2, j1 = j3, k1 = l2, l1 = pass ? k3 % 2 : k3 % 4;+ }+ }+ }+ }+ int l6 = l1 % 2, i3 = 1 - l6;+ if (l1 % 4 >= 2) l6 = -l6, i3 = -i3;+ if (best < 0) {+ /* nowhere: a ledge of obsidian, air about it */+ j1 = j1 < 70 ? 70 : j1 > WORLD_H - 10 ? WORLD_H - 10 : j1;+ pl.y = (float)j1;+ world_finish(pl.x, pl.y, pl.z);+ for (int j7 = -1; j7 <= 1; j7++)+ for (int l7 = 1; l7 < 3; l7++)+ for (int k8 = -1; k8 < 3; k8++)+ world_set(i1 + (l7 - 1) * l6 + j7 * i3, j1 + k8, k1 + (l7 - 1) * i3 - j7 * l6, k8 < 0 ? B_OBSIDIAN : B_AIR);+ }+ for (int l8 = 0; l8 < 4; l8++)+ for (int l9 = -1; l9 < 4; l9++) {+ bool frame = l8 == 0 || l8 == 3 || l9 == -1 || l9 == 3;+ world_set(i1 + (l8 - 1) * l6, j1 + l9, k1 + (l8 - 1) * i3, frame ? B_OBSIDIAN : l6 ? B_PORTAL_X : B_PORTAL_Z);+ }+ made[0] = i1, made[1] = j1, made[2] = k1;+}++/* Teleporter.placeInExistingPortal: to the nearest portal within 128 (its lowest block), at the place+ * across it the player went in at; false: none */+static bool place_in_portal(float frac, int from_ax) {+ int at[3];+ if (!edits_find(ifl(pl.x), ifl(pl.y), ifl(pl.z), 128, B_PORTAL_X, B_PORTAL_Z, at)) return false;+ pl.x = at[0] + 0.5f, pl.y = (float)at[1], pl.z = at[2] + 0.5f;+ world_finish(pl.x, pl.y, pl.z);+ int b = world_get(at[0], at[1], at[2]), ax = b == B_PORTAL_Z;+ if (!is_portal(b)) return true;+ /* across: from its one side to the other */+ int dx = ax ? 0 : 1, dz = ax ? 1 : 0, lo = 0, hi = 0;+ while (lo > -21 && world_get(at[0] + dx * (lo - 1), at[1], at[2] + dz * (lo - 1)) == b) lo--;+ while (hi < 21 && world_get(at[0] + dx * (hi + 1), at[1], at[2] + dz * (hi + 1)) == b) hi++;+ float a = lo + 0.3f + frac * (hi - lo + 0.4f);+ if (ax) pl.z = at[2] + a;+ else pl.x = at[0] + a;+ if (ax != from_ax) pl.yaw += 90;+ return true;+}++/* the player's last place in the Overworld: what NumPlay's NumBlocks loads when the world is saved+ * elsewhere (save.c) */+static void left_overworld(void) {+ dst.ow_x = pl.x, dst.ow_y = pl.y, dst.ow_z = pl.z;+}++/* the loading screen while the dimension is made */+static void loading_screen(void) {+ int g = gui;+ gui = GUI_LOADING;+ render_frame(NULL, game_time);+ gui = g;+}++/* Entity.travelToDimension and ServerConfigurationManager.transferEntityToWorld */+static void travel(int to, float frac, int ax) {+ int from = dim;+ float x = pl.x, y = pl.y, z = pl.z;+ if (to == DIM_NETHER) x /= 8, z /= 8;+ else if (from == DIM_NETHER) x *= 8, z *= 8;+ /* (the world border, then whole blocks) */+ if (x < -29999872) x = -29999872;+ if (x > 29999872) x = 29999872;+ if (z < -29999872) z = -29999872;+ if (z > 29999872) z = 29999872;+ x = (float)(int)x, z = (float)(int)z;+ if (from == DIM_OVERWORLD) left_overworld();+ loading_screen();+ if (!world_dim(to)) {+ gui_message(T("Storage is full: the portal does nothing"));+ return;+ }+ pl.x = x, pl.y = y, pl.z = z;+ pl.vx = pl.vy = pl.vz = 0;+ pl.fall = 0;+ memset(ents, 0, sizeof ents);+ if (to == DIM_END) {+ /* WorldProviderEnd.getSpawnCoordinate, Teleporter.placeInPortal: on an obsidian ledge at 100, 49, 0 */+ pl.x = 100, pl.y = 49, pl.z = 0, pl.yaw = 90, pl.pitch = 0;+ world_finish(pl.x, pl.y, pl.z);+ for (int j1 = -2; j1 <= 2; j1++)+ for (int k1 = -2; k1 <= 2; k1++)+ for (int l1 = -1; l1 < 3; l1++) world_set(100 + k1, 49 + l1, 0 - j1, l1 < 0 ? B_OBSIDIAN : B_AIR);+ dragon_enter();+ } else if (from == DIM_END) {+ /* the End left: back where the player respawns (with all they have) */+ player_to_spawn();+ } else {+ world_finish(pl.x, pl.y, pl.z);+ if (!place_in_portal(frac, ax)) {+ make_portal();+ edits_flush();+ if (!place_in_portal(frac, ax)) pl.x = made[0] + 0.5f, pl.y = (float)made[1], pl.z = made[2] + 0.5f;+ }+ }+ world_finish(pl.x, pl.y, pl.z);+ camera_reset();+}++/* ---------------------------------------------------------------- the player in portals */+static int16_t portal_count, portal_cool;+float portal_time; /* EntityPlayerSP.timeInPortal: the overlay */+static bool in_portal;++/* Entity.onEntityUpdate's portal part, Entity.setPortal (a portal block in the player's cells: their+ * whole cells, as doBlockCollisions has it), BlockEndPortal.onEntityCollidedWithBlock */+void dims_player_tick(void) {+ bool nether = false, end = false;+ int px = 0, pz = 0, pax = 0;+ for (int y = ifl(pl.y + 0.001f); y <= ifl(pl.y + 1.799f); y++)+ for (int z = ifl(pl.z - 0.299f); z <= ifl(pl.z + 0.299f); z++)+ for (int x = ifl(pl.x - 0.299f); x <= ifl(pl.x + 0.299f); x++) {+ int b = world_get(x, y, z);+ if (is_portal(b)) nether = true, px = x, pz = z, pax = b == B_PORTAL_Z;+ if (b == B_END_PORTAL) end = true;+ }+ if (end && !pl.dead) {+ if (dim == DIM_END) dims_credits();+ else if (dim == DIM_OVERWORLD) travel(DIM_END, 0, 0);+ return;+ }+ if (nether) {+ if (portal_cool > 0) portal_cool = 10;+ else in_portal = true;+ }+ /* EntityPlayerSP.onLivingUpdate */+ if (in_portal) {+ portal_time += 0.0125f;+ if (portal_time > 1) portal_time = 1;+ } else if (portal_time > 0) {+ portal_time -= 0.05f;+ if (portal_time < 0) portal_time = 0;+ }+ int wait = pl.mode == 1 ? 0 : 80; /* EntityPlayer.getMaxInPortalTime */+ if (in_portal) {+ if (portal_count++ >= wait) {+ portal_count = (int16_t)wait;+ portal_cool = 10; /* getPortalCooldown */+ /* where across the portal the player went in */+ int lo = 0, hi = 0, b = pax ? B_PORTAL_Z : B_PORTAL_X, dx = pax ? 0 : 1, dz = pax ? 1 : 0, y = ifl(pl.y + 0.5f);+ while (lo > -21 && world_get(px + dx * (lo - 1), y, pz + dz * (lo - 1)) == b) lo--;+ while (hi < 21 && world_get(px + dx * (hi + 1), y, pz + dz * (hi + 1)) == b) hi++;+ float at = pax ? pl.z - pz : pl.x - px, frac = (at - lo) / (hi - lo + 1);+ frac = frac < 0 ? 0 : frac > 1 ? 1 : frac;+ travel(dim == DIM_NETHER ? DIM_OVERWORLD : DIM_NETHER, frac, pax);+ }+ in_portal = false;+ } else {+ if (portal_count > 0) portal_count -= 4;+ if (portal_count < 0) portal_count = 0;+ }+ if (portal_cool > 0) portal_cool--;+}++/* ServerConfigurationManager.recreatePlayerEntity's place: the spawn point, up out of blocks */+void player_to_spawn(void) {+ pl.x = pl.spawn_x + 0.5f, pl.y = (float)pl.spawn_y, pl.z = pl.spawn_z + 0.5f;+ pl.vx = pl.vy = pl.vz = 0, pl.fall = 0;+ world_finish(pl.x, pl.y, pl.z);+ for (int k = 0; k < WORLD_H; k++) {+ int x = ifl(pl.x), y = ifl(pl.y), z = ifl(pl.z);+ if (!(blk_flags[world_get(x, y, z)] & BF_SOLID) && !(blk_flags[world_get(x, y + 1, z)] & BF_SOLID)) break;+ pl.y = (float)(y + 1);+ world_finish(pl.x, pl.y, pl.z);+ }+}++/* (tests) straight there, as through a portal */+void dims_go(int to) { travel(to, 0.5f, 0); }++/* death: the respawn is in the Overworld (WorldProvider.canRespawnHere) */+void dims_respawn(void) {+ if (dim == DIM_OVERWORLD) return;+ loading_screen();+ world_dim(DIM_OVERWORLD);+ memset(ents, 0, sizeof ents);+}++/* the dragon fight (dragon.c) and the credits come later */+void dragon_enter(void) {}+void dims_credits(void) {+ loading_screen();+ if (!world_dim(DIM_OVERWORLD)) return;+ memset(ents, 0, sizeof ents);+ player_to_spawn();+ camera_reset();+}#endifdiff --git a/games/numblocks/src/dims.h b/games/numblocks/src/dims.hnew file mode 100644index 00000000..b0fd681f--- /dev/null+++ b/games/numblocks/src/dims.h@@ -0,0 +1,34 @@+/* NumBlocks.nwa only (NB_DIMS): what the generators of the Nether and the End share with the game. */+#ifndef NB_DIMS_H+#define NB_DIMS_H+#include <stdbool.h>+#include <stdint.h>+#include "blocks.h"++enum { DIM_OVERWORLD = 0, DIM_NETHER = 1, DIM_END = 2 };++/* The generators write a byte a block: codes 67..82 (wool, which no generator makes) stand for the+ * blocks past 255 (world.c turns them back) */+#define GEN_CODE(k) (B_WOOL_WHITE + (k))+enum { GC_NETHERRACK, GC_SOUL_SAND, GC_QUARTZ_ORE, GC_NETHER_BRICK, GC_FENCE, GC_STAIRS_E, GC_STAIRS_W, GC_STAIRS_S,+ GC_STAIRS_N, GC_WART, GC_END_STONE, GC_FRAME, GC_FRAME_EYE, GC_N };+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) */++/* 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 */+} DimsState;+extern DimsState dst;+extern float portal_time; /* 0..1: the portal's swirl over the picture */+bool dims_portal_light(int x, int y, int z); /* fire there lights a portal? (then it is made) */+int dims_can_stay(int b, int x, int y, int z); /* -1: not one of the dimensions' blocks */+void dims_player_tick(void); /* portals */+void dims_respawn(void); /* (before the player respawns) */+void player_to_spawn(void);+void dims_go(int to); /* (tests) there, as through a portal */+void dragon_enter(void);+void dims_credits(void);+#endifdiff --git a/games/numblocks/src/edits.c b/games/numblocks/src/edits.cindex 454e1d07..62d0da8a 100644--- a/games/numblocks/src/edits.c+++ b/games/numblocks/src/edits.c@@ -290,6 +290,41 @@ int edits_top(int x, int z, int top) {return top;}+#if NB_DIMS+/* 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 */+bool edits_find(int x, int y, int z, int rad, int b0, int b1, int *out) {+ int64_t best = -1;+ for (int rz = floordiv(z - rad, 64); rz <= floordiv(z + rad, 64); rz++)+ for (int rx = floordiv(x - rad, 64); rx <= floordiv(x + rad, 64); rx++) {+ int n;+ const uint8_t *rec = region(rx, rz, &n);+ const uint8_t *r_ = rec;+#define RR(i) rd32(r_ + 4 * (i))+ for (int i = n; i-- > 0 && (RR(i) & HI);) {+ uint32_t e = RR(i);+ int b = BLOCK_OF(e);+ if (b != b0 && b != b1) continue;+ uint32_t k = KEY(e) & ~HI;+ /* (a plain entry of the spot wins over it: NumPlay's NumBlocks changed it since) */+ const uint8_t *r = rec;+ int j = rlower(r, n, k);+ if (j < n && KEY(R(j)) == k) continue;+ if ((e & 127) && i > 0 && KEY(RR(i - 1)) == KEY(e) - 1 && (BLOCK_OF(RR(i - 1)) == b0 || BLOCK_OF(RR(i - 1)) == b1))+ continue; /* (one below: the lowest of a column counts) */+ int chunk = (int)(e >> 15) & 15, xz = (int)(e >> 7) & 255;+ int bx = (rx * 4 + (chunk & 3)) * 16 + (xz & 15), bz = (rz * 4 + (chunk >> 2)) * 16 + (xz >> 4), by = (int)(e & 127);+ if (bx < x - rad || bx > x + rad || bz < z - rad || bz > z + rad) continue;+ int64_t d = (int64_t)(bx - x) * (bx - x) + (int64_t)(by - y) * (by - y) + (int64_t)(bz - z) * (bz - z);+ if (best < 0 || d < best) best = d, out[0] = bx, out[1] = by, out[2] = bz;+ }+#undef RR+ }+ return best >= 0;+}+#endif+int edits_count(void) { return nj; }/* the merge buffer, lent out between merges (n bytes at most) */diff --git a/games/numblocks/src/edits.h b/games/numblocks/src/edits.hindex 26324deb..8975d1e2 100644--- a/games/numblocks/src/edits.h+++ b/games/numblocks/src/edits.h@@ -17,5 +17,6 @@ void *edits_scratch(uint32_t n); /* in the journal */#if NB_DIMSvoid 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 */#endif#endifdiff --git a/games/numblocks/src/gen.c b/games/numblocks/src/gen.cindex a31e81cd..780b8db4 100644--- a/games/numblocks/src/gen.c+++ b/games/numblocks/src/gen.c@@ -99,6 +99,11 @@#include <string.h>#include "blocks.h"+#if NB_DIMS+#include "dims.h"+static int dims_slab(int cx, int cz, int y0, int h, uint8_t *out); /* (gen_dims.h: the Nether, the End) */+static int gd_dim;+#endif/* The float arithmetic here must be done as written (IEEE single precision, each operation* rounded): the game builds with -ffast-math, which would fuse multiply-adds, reassociate sums@@ -5411,6 +5416,9 @@ void gen_init(int64_t seed) {jr_seed(&r, seed);cave_mul_x = jr_long(&r);cave_mul_z = jr_long(&r);+#if NB_DIMS+ gd_dim = DIM_OVERWORLD;+#endif/* caches */memset(sumc, 0, NSUM * sizeof *sumc);memset(pmemo, 0, sizeof pmemo);@@ -5465,6 +5473,9 @@ static int flat_block(int y) { return y == 0 ? B_BEDROCK : y < 3 ? B_DIRT : y ==int gen_prepare(int cx, int cz) {if (g_flat) return 0;+#if NB_DIMS+ if (gd_dim) return 0;+#endiffor (int dz = -1; dz <= 1; dz++)for (int dx = -1; dx <= 1; dx++)if ((dx || dz) && !sum_find(cx + dx, cz + dz)) {@@ -5481,6 +5492,9 @@ void gen_slab(int cx, int cz, int y0, int h, uint8_t *out) {}if (y0 + h > 128) h = 128 - y0;if (h <= 0) return;+#if NB_DIMS+ if (dims_slab(cx, cz, y0, h, out)) return;+#endifif (g_flat) {for (int y = 0; y < h; y++) memset(out + y * 256, flat_block(y0 + y), 256);return;@@ -5528,6 +5542,9 @@ void gen_slab(int cx, int cz, int y0, int h, uint8_t *out) {}int gen_biome(int x, int z) {+#if NB_DIMS+ if (gd_dim) return gd_dim == DIM_NETHER ? 8 : 9; /* (BiomeGenHell, BiomeGenEnd) */+#endifif (g_flat) return BI_PLAINS;int cx = x >> 4, cz = z >> 4, i = (x & 15) + (z & 15) * 16;if (cx == last_cx && cz == last_cz) return c_b16[i];@@ -5536,6 +5553,9 @@ int gen_biome(int x, int z) {}int gen_top(int x, int z) {+#if NB_DIMS+ if (gd_dim) return 127;+#endifif (g_flat) return 3;int cx = x >> 4, cz = z >> 4, i = (x & 15) + (z & 15) * 16;if (cx != last_cx || cz != last_cz) gen_slab(cx, cz, 127, 1, col_tmp);@@ -5625,3 +5645,7 @@ unsigned gen_ram_bytes(void) {sizeof bgrid + sizeof lay_wseed + sizeof bflags + sizeof tov + sizeof crec + sizeof U +NSUM * sizeof *sumc);}++#if NB_DIMS+#include "gen_dims.h" /* NumBlocks.nwa: the Nether and the End */+#endifdiff --git a/games/numblocks/src/gen_dims.h b/games/numblocks/src/gen_dims.hnew file mode 100644index 00000000..72eee32d--- /dev/null+++ b/games/numblocks/src/gen_dims.h@@ -0,0 +1,501 @@+/* NumBlocks.nwa only (NB_DIMS): the generators of the Nether (ChunkProviderHell, MapGenCavesHell) and the+ * End (ChunkProviderEnd), included at the end of gen.c to share its java.util.Random, its noise and its+ * memory: while the player is in one of them, the Overworld's caches (the summary cache on main's stack,+ * the scratch union U) are theirs, and gen_init makes them the Overworld's again.+ *+ * As in gen.c, the noise's sample positions are exact (fixed point) and its sums run in float; the+ * terrain, its surface and the caves follow 1.8.8 block for block. Minecraft 1.8.8 decorates the Nether+ * and the End with a random that is not the world's (it depends on the order chunks were made in): here+ * each decoration draws from a random seeded by its chunk, so it is the same every time. */+#include "dims.h"++/* gd_dim (gen.c): the generator gen_slab runs, DIM_* */++/* ---------------------------------------------------------------- noise: NoiseGeneratorOctaves, any size */+/* n octaves of a NoiseGeneratorOctaves (their states st) at grid origin (bx, by, bz), xs x ys x zs samples+ * (ys 1: the 2D noise) spaced sx, sy, sz (Q32), added into out[(x * zs + z) * ys + y] */+static void gd_octaves(const Octave *oc, int n, float *out, i64 bx, i64 by, i64 bz, int xs, int ys, int zs, u64 sx,+ u64 sy, u64 sz) {+ uint8_t P[256];+ int xc[16], zc[16], yc[17];+ float xf[16], zf[16], yf[17];+ for (int o = 0; o < n; o++) {+ i64 off[3];+ perm_build(oc[o].st, P, off);+ float amp = (float)(1 << o);+ for (int k = 0; k < xs; k++) coord_split(coord_q32(bx, sx, o, k, off[0]), &xc[k], &xf[k]);+ for (int k = 0; k < zs; k++) coord_split(coord_q32(bz, sz, o, k, off[2]), &zc[k], &zf[k]);+ if (ys == 1) {+ /* NoiseGeneratorImproved's 2D path (no y) */+ int idx = 0;+ for (int x = 0; x < xs; x++) {+ float fx = xf[x], ux = fade(fx);+ int i3 = xc[x];+ for (int z = 0; z < zs; z++, idx++) {+ float fz = zf[z], uz = fade(fz);+ int l1 = zc[z];+ int j3 = P[P[i3] & 255] + l1, i1 = P[P[(i3 + 1) & 255] & 255] + l1;+ float a = lerp(ux, grad3(P[j3 & 255], fx, 0, fz), grad3(P[i1 & 255], fx - 1, 0, fz));+ float b = lerp(ux, grad3(P[(j3 + 1) & 255], fx, 0, fz - 1), grad3(P[(i1 + 1) & 255], fx - 1, 0, fz - 1));+ out[idx] += lerp(uz, a, b) * amp;+ }+ }+ continue;+ }+ for (int k = 0; k < ys; k++) coord_split(coord_q32(by, sy, o, k, off[1]), &yc[k], &yf[k]);+ int idx = 0;+ for (int x = 0; x < xs; x++) {+ float fx = xf[x], ux = fade(fx), gx1 = fx - 1;+ int pa = P[xc[x]], pb = P[(xc[x] + 1) & 255];+ for (int z = 0; z < zs; z++) {+ float fz = zf[z], uz = fade(fz), gz1 = fz - 1;+ int j4 = zc[z], cur = -1;+ float d16 = 0, d7 = 0, d17 = 0, d8 = 0;+ for (int y = 0; y < ys; y++, idx++) {+ /* Java's stale gradients: recomputed only when the y cell changes (and at a column's start),+ * with the y fraction of the first sample in the cell */+ if (y == 0 || yc[y] != cur) {+ cur = yc[y];+ float fy = yf[y], gy1 = fy - 1;+ int j5 = pa + cur, i6 = pb + cur;+ int k5 = P[j5 & 255] + j4, l5 = P[(j5 + 1) & 255] + j4;+ int i2 = P[i6 & 255] + j4, j6 = P[(i6 + 1) & 255] + j4;+ d16 = lerp(ux, grad3(P[k5 & 255], fx, fy, fz), grad3(P[i2 & 255], gx1, fy, fz));+ d7 = lerp(ux, grad3(P[l5 & 255], fx, gy1, fz), grad3(P[j6 & 255], gx1, gy1, fz));+ d17 = lerp(ux, grad3(P[(k5 + 1) & 255], fx, fy, gz1), grad3(P[(i2 + 1) & 255], gx1, fy, gz1));+ d8 = lerp(ux, grad3(P[(l5 + 1) & 255], fx, gy1, gz1), grad3(P[(j6 + 1) & 255], gx1, gy1, gz1));+ }+ float uy = fade(yf[y]);+ out[idx] += lerp(uz, lerp(uy, d16, d7), lerp(uy, d17, d8)) * amp;+ }+ }+ }+ }+}++/* ======================================================================== */+/* The Nether: ChunkProviderHell */+/* ======================================================================== */++#define NG 6 /* density grids kept */+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 */+} NetherMem;+_Static_assert(sizeof(NetherMem) <= GEN_CACHE_BYTES, "the Nether's state fits in the summary cache");+#define NM ((NetherMem *)(void *)sumc)+static u64 SC_N1XZ, SC_N1Y, SC_N3XZ, SC_N3Y, SC_SLOW, SC_EXCL;++static void nether_init(void) {+ NetherMem *m = NM;+ memset(m, 0, sizeof *m);+ JRand r;+ jr_seed(&r, g_seed);+ for (int i = 0; i < 48; i++) m->oc[i].st = r.s, perm_skip(&r);+ SC_N1XZ = FX(684.412);+ SC_N1Y = FX(2053.236);+ SC_N3XZ = FX(684.412 / 80.0);+ SC_N3Y = FX(2053.236 / 60.0);+ SC_SLOW = FX(0.03125);+ SC_EXCL = FX(0.0625);+ for (int j = 0; j < 17; j++) {+ double a = cos((double)j * 3.141592653589793 * 6.0 / 17.0) * 2.0, d2 = j > 8 ? 16 - j : j;+ if (d2 < 4.0) {+ d2 = 4.0 - d2;+ a -= d2 * d2 * d2 * 10.0;+ }+ m->bias[j] = (float)a;+ }+}++/* initializeNoiseField: chunk (cx, cz)'s 5 x 17 x 5 densities */+static const float *nether_grid(int cx, int cz) {+ 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);+ memset(n3, 0, sizeof n3);+ 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;+ if (d8 < 0.0f) d4 = d6;+ else if (d8 > 1.0f) d4 = d7;+ else d4 = d6 + (d7 - d6) * d8;+ d4 -= m->bias[y];+ if (y > 13) {+ float d9 = (float)(y - 13) / 3.0f;+ d4 = d4 * (1.0f - d9) + -10.0f * d9;+ }+ g[i] = d4;+ }+ m->gx[k] = cx, m->gz[k] = cz, m->gok[k] = 1, m->gage[k] = ++m->gclock;+ return g;+}++/* func_180515_a: the terrain of a column (netherrack where the density is above 0, lava under y 32),+ * into col[0..127] */+static void nether_column(const float *g, int x, int z, uint8_t *col) {+ int j1 = x >> 2, k1 = z >> 2, j2 = x & 3, k2 = z & 3;+ for (int l1 = 0; l1 < 16; l1++) {+ /* the grid's corners, interpolated as Java does: along y in steps of 1/8, x and z in steps of 1/4 */+ 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 d5 = (g[((j1 + 0) * 5 + k1 + 0) * 17 + l1 + 1] - d1) * 0.125f;+ float d6 = (g[((j1 + 0) * 5 + k1 + 1) * 17 + l1 + 1] - d2) * 0.125f;+ float d7 = (g[((j1 + 1) * 5 + k1 + 0) * 17 + l1 + 1] - d3) * 0.125f;+ float d8 = (g[((j1 + 1) * 5 + k1 + 1) * 17 + l1 + 1] - d4) * 0.125f;+ for (int i2 = 0; i2 < 8; i2++) {+ /* (the steps added j2 times, k2 times, as Java's loops add them) */+ float d10 = d1, d11 = d2, d12 = (d3 - d1) * 0.25f, d13 = (d4 - d2) * 0.25f;+ for (int a = 0; a < j2; a++) d10 += d12, d11 += d13;+ float d15 = d10, d16 = (d11 - d10) * 0.25f;+ for (int b = 0; b < k2; b++) d15 += d16;+ int y = l1 * 8 + i2;+ col[y] = d15 > 0.0f ? GEN_CODE(GC_NETHERRACK) : y < 32 ? B_LAVA : B_AIR;+ d1 += d5, d2 += d6, d3 += d7, d4 += d8;+ }+ }+}++/* a column's surface (func_180516_b), its draws from r (the chunk's hellRNG) */+static void nether_surface(uint8_t *col, JRand *r, float slow, float grav, float excl) {+ int flag = slow + jr_doublef(r) * 0.2f > 0.0f;+ int flag1 = grav + jr_doublef(r) * 0.2f > 0.0f;+ int l = (int)(excl / 3.0f + 3.0f + jr_doublef(r) * 0.25f);+ int i1 = -1;+ const int top = 64;+ uint8_t s = GEN_CODE(GC_NETHERRACK), s1 = GEN_CODE(GC_NETHERRACK);+ for (int j1 = 127; j1 >= 0; --j1) {+ int a = j1 < 127 - jr_int(r, 5);+ if (a && j1 > jr_int(r, 5)) {+ uint8_t b = col[j1];+ if (b != B_AIR) {+ if (b == GEN_CODE(GC_NETHERRACK)) {+ if (i1 == -1) {+ if (l <= 0) {+ s = B_AIR;+ s1 = GEN_CODE(GC_NETHERRACK);+ } else if (j1 >= top - 4 && j1 <= top + 1) {+ s = GEN_CODE(GC_NETHERRACK);+ s1 = GEN_CODE(GC_NETHERRACK);+ if (flag1) s = B_GRAVEL, s1 = GEN_CODE(GC_NETHERRACK);+ if (flag) s = GEN_CODE(GC_SOUL_SAND), s1 = GEN_CODE(GC_SOUL_SAND);+ }+ if (j1 < top && s == B_AIR) s = B_LAVA;+ i1 = l;+ col[j1] = j1 >= top - 1 ? s : s1;+ } else if (i1 > 0) {+ --i1;+ col[j1] = s1;+ }+ }+ } else {+ i1 = -1;+ }+ } else {+ col[j1] = B_BEDROCK;+ }+ }+}++/* ---- MapGenCavesHell: tunnels carved where the box they cut holds no lava */+typedef struct {+ int cx, cz; /* the chunk carved */+ const uint32_t *lava; /* its lava, a bit a block: [(x * 16 + z) * 4 + (y >> 5)] bit y & 31 */+ uint8_t *out; /* the slab carved */+ int y0, h;+} HellCaves;++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);+ float f = 0.0f, f1 = 0.0f;+ JRand rnd;+ jr_seed(&rnd, seed);+ if (len <= 0) {+ int i = 8 * 16 - 16;+ len = i - jr_int(&rnd, i / 4);+ }+ int flag1 = 0;+ if (k == -1) {+ k = len / 2;+ flag1 = 1;+ }+ 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 f2 = mh_cos(pitch), f3 = mh_sin(pitch);+ px += (double)(mh_cos(yaw) * f2);+ py += (double)f3;+ pz += (double)(mh_sin(yaw) * f2);+ pitch = flag ? pitch * 0.92f : pitch * 0.7f;+ pitch += f1 * 0.1f;+ yaw += f * 0.1f;+ f1 *= 0.9f;+ f *= 0.75f;+ /* (Java's order of the draws: left to right) */+ float ra = jr_float(&rnd), rb = jr_float(&rnd), rc = jr_float(&rnd);+ f1 += (ra - rb) * rc * 2.0f;+ ra = jr_float(&rnd), rb = jr_float(&rnd), rc = jr_float(&rnd);+ f += (ra - rb) * rc * 4.0f;+ 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);+ 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);+ 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 (k2 < 0) k2 = 0;+ if (kk > 16) kk = 16;+ if (l2 < 1) l2 = 1;+ if (l > 120) l = 120;+ if (i3 < 0) i3 = 0;+ if (i1 > 16) i1 = 16;+ int flag2 = 0;+ for (int j1 = k2; !flag2 && j1 < kk; ++j1)+ for (int k1 = i3; !flag2 && k1 < i1; ++k1)+ for (int l1 = l + 1; !flag2 && l1 >= l2 - 1; --l1)+ if (l1 >= 0 && l1 < 128) {+ if (hc_lava(c, j1, l1, k1)) flag2 = 1;+ if (l1 != l2 - 1 && j1 != k2 && j1 != kk - 1 && k1 != i3 && k1 != i1 - 1) l1 = l2;+ }+ if (!flag2) {+ for (int j3 = k2; j3 < kk; ++j3) {+ double d10 = ((double)(j3 + c->cx * 16) + 0.5 - 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];+ if (*b == GEN_CODE(GC_NETHERRACK) || *b == B_DIRT || *b == B_GRASS) *b = B_AIR;+ }+ }+ }+ }+ if (flag1) break;+ }+ }+ }+ }+}++static void hell_caves(HellCaves *c) {+ JRand r;+ jr_seed(&r, g_seed);+ i64 j = jr_long(&r), k = jr_long(&r);+ for (int l = c->cx - 8; l <= c->cx + 8; ++l)+ for (int i1 = c->cz - 8; i1 <= c->cz + 8; ++i1) {+ jr_seed(&r, ((i64)l * j) ^ ((i64)i1 * k) ^ g_seed);+ /* recursiveGenerate */+ 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 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);+ 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);+ }+ }+ }+}++static void nether_decorate(int cx, int cz, int y0, int h, uint8_t *out); /* (below: fortresses, glowstone...) */++/* chunk (cx, cz), rows [y0, y0 + h): terrain, surface, caves, then what decorates it */+static void nether_slab(int cx, int cz, int y0, int h, uint8_t *out) {+ const float *g = nether_grid(cx, cz);+ NetherMem *m = NM;+ float slow[256], grav[256], excl[256];+ memset(slow, 0, sizeof slow);+ memset(grav, 0, sizeof grav);+ memset(excl, 0, sizeof excl);+ gd_octaves(m->oc + 40, 4, slow, cx * 16, cz * 16, 0, 16, 16, 1, SC_SLOW, SC_SLOW, FX(1.0));+ gd_octaves(m->oc + 40, 4, grav, cx * 16, 109, cz * 16, 16, 1, 16, SC_SLOW, FX(1.0), SC_SLOW);+ gd_octaves(m->oc + 44, 4, excl, cx * 16, cz * 16, 0, 16, 16, 1, SC_EXCL, SC_EXCL, SC_EXCL);+ JRand r;+ jr_seed(&r, (i64)cx * 341873128712LL + (i64)cz * 132897987541LL);+ uint32_t lava[16 * 16 * 4];+ for (int z = 0; z < 16; z++)+ for (int x = 0; x < 16; x++) {+ uint8_t col[128];+ nether_column(g, x, z, col);+ nether_surface(col, &r, slow[z + x * 16], grav[z + x * 16], excl[z + x * 16]);+ uint32_t *lv = &lava[(x * 16 + z) * 4];+ lv[0] = lv[1] = lv[2] = lv[3] = 0;+ for (int y = 0; y < 128; y++)+ if (col[y] == B_LAVA) lv[y >> 5] |= 1u << (y & 31);+ for (int y = 0; y < h; y++) out[(y * 16 + z) * 16 + x] = col[y0 + y];+ }+ HellCaves c = {cx, cz, lava, out, y0, h};+ hell_caves(&c);+ 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; }++/* ======================================================================== */+/* The End: ChunkProviderEnd */+/* ======================================================================== */++typedef struct {+ Octave oc[40]; /* noiseGen1 (16), noiseGen2 (16), noiseGen3 (8) */+} EndMem;+_Static_assert(sizeof(EndMem) <= GEN_CACHE_BYTES, "the End's state fits in the summary cache");+#define EM ((EndMem *)(void *)sumc)+static u64 SC_E1XZ, SC_E3XZ, SC_E3Y;++static void end_init(void) {+ EndMem *m = EM;+ memset(m, 0, sizeof *m);+ JRand r;+ jr_seed(&r, g_seed);+ for (int i = 0; i < 40; i++) m->oc[i].st = r.s, perm_skip(&r);+ SC_E1XZ = FX(684.412 * 2.0);+ SC_E3XZ = FX(684.412 * 2.0 / 80.0);+ SC_E3Y = FX(684.412 / 160.0);+}++/* initializeNoiseField: chunk (cx, cz)'s 3 x 33 x 3 densities (cells of 8 blocks across, 4 up), with the+ * island's falloff from (0, 0) */+static void end_grid(int cx, int cz, float *g) {+ EndMem *m = EM;+ float n1[297], n2[297], n3[297];+ memset(n1, 0, sizeof n1);+ memset(n2, 0, sizeof n2);+ memset(n3, 0, sizeof n3);+ gd_octaves(m->oc + 32, 8, n3, cx * 2, 0, cz * 2, 3, 33, 3, SC_E3XZ, SC_E3Y, SC_E3XZ);+ gd_octaves(m->oc, 16, n1, cx * 2, 0, cz * 2, 3, 33, 3, SC_E1XZ, FX(684.412), SC_E1XZ);+ gd_octaves(m->oc + 16, 16, n2, cx * 2, 0, cz * 2, 3, 33, 3, SC_E1XZ, FX(684.412), SC_E1XZ);+ int i = 0;+ for (int j = 0; j < 3; j++)+ for (int k = 0; k < 3; k++) {+ float f = (float)(j + cx * 2), f1 = (float)(k + cz * 2);+ float f2 = 100.0f - sqrtf(f * f + f1 * f1) * 8.0f;+ if (f2 > 80.0f) f2 = 80.0f;+ if (f2 < -100.0f) f2 = -100.0f;+ for (int l = 0; l < 33; l++, i++) {+ float d3 = n1[i] / 512.0f, d4 = n2[i] / 512.0f, d5 = (n3[i] / 10.0f + 1.0f) / 2.0f, d2;+ if (d5 < 0.0f) d2 = d3;+ else if (d5 > 1.0f) d2 = d4;+ else d2 = d3 + (d4 - d3) * d5;+ d2 -= 8.0f;+ d2 += f2;+ if (l > 33 / 2 - 2) {+ float d6 = (float)(l - (33 / 2 - 2)) / 64.0f;+ d6 = d6 < 0 ? 0 : d6 > 1 ? 1 : d6;+ d2 = d2 * (1.0f - d6) + -3000.0f * d6;+ }+ if (l < 8) {+ float d7 = (float)(8 - l) / 7.0f;+ d2 = d2 * (1.0f - d7) + -30.0f * d7;+ }+ g[i] = d2;+ }+ }+}++static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out);++/* func_180520_a: end stone where the density is above 0 (the surface pass changes nothing there) */+static void end_slab(int cx, int cz, int y0, int h, uint8_t *out) {+ float g[297];+ if (cx * cx + cz * cz > 30 * 30) { /* (far out, the falloff leaves the void: no noise needed) */+ memset(out, B_AIR, (size_t)h * 256);+ return;+ }+ end_grid(cx, cz, g);+ for (int j1 = 0; j1 < 2; j1++)+ for (int k1 = 0; k1 < 2; k1++)+ for (int l1 = 0; l1 < 32; l1++) {+ float d1 = g[((j1 + 0) * 3 + k1 + 0) * 33 + l1], d2 = g[((j1 + 0) * 3 + k1 + 1) * 33 + l1];+ float d3 = g[((j1 + 1) * 3 + k1 + 0) * 33 + l1], d4 = g[((j1 + 1) * 3 + k1 + 1) * 33 + l1];+ float d5 = (g[((j1 + 0) * 3 + k1 + 0) * 33 + l1 + 1] - d1) * 0.25f;+ float d6 = (g[((j1 + 0) * 3 + k1 + 1) * 33 + l1 + 1] - d2) * 0.25f;+ float d7 = (g[((j1 + 1) * 3 + k1 + 0) * 33 + l1 + 1] - d3) * 0.25f;+ float d8 = (g[((j1 + 1) * 3 + k1 + 1) * 33 + l1 + 1] - d4) * 0.25f;+ for (int i2 = 0; i2 < 4; i2++) {+ float d10 = d1, d11 = d2, d12 = (d3 - d1) * 0.125f, d13 = (d4 - d2) * 0.125f;+ int y = l1 * 4 + i2 - y0;+ for (int j2 = 0; j2 < 8; j2++) {+ float d15 = d10, d16 = (d11 - d10) * 0.125f;+ for (int k2 = 0; k2 < 8; k2++) {+ if (y >= 0 && y < h)+ out[(y * 16 + k2 + k1 * 8) * 16 + j2 + j1 * 8] =+ d15 > 0.0f ? GEN_CODE(GC_END_STONE) : B_AIR;+ d15 += d16;+ }+ d10 += d12, d11 += d13;+ }+ d1 += d5, d2 += d6, d3 += d7, d4 += d8;+ }+ }+ end_decorate(cx, cz, y0, h, out);+}++static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out) { (void)cx, (void)cz, (void)y0, (void)h, (void)out; }++/* ======================================================================== */+/* which generator runs */+/* ======================================================================== */++void gen_dim(int d) {+ gd_dim = d;+ if (d == DIM_NETHER) nether_init();+ else if (d == DIM_END) end_init();+}++static int dims_slab(int cx, int cz, int y0, int h, uint8_t *out) {+ if (gd_dim == DIM_NETHER) nether_slab(cx, cz, y0, h, out);+ else if (gd_dim == DIM_END) end_slab(cx, cz, y0, h, out);+ else return 0;+ return 1;+}diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex 959d2d29..6905e909 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -117,16 +117,14 @@ int world_rain_top(int x, int z); /* the first y above what rain lvoid *world_scratch(uint32_t n); /* n bytes free while a frame is drawn, or NULL */#if NB_DIMS/* ---------------------------------------------------------------- the dimensions (dims.c): NumBlocks.nwa only */-enum { DIM_OVERWORLD = 0, DIM_NETHER = 1, DIM_END = 2 };-extern int dim; /* where the player is */-/* the generators' bytes: codes 67..82 (wool, which no generator makes) stand for the blocks past 255 */-#define GEN_CODE(k) (B_WOOL_WHITE + (k))-enum { GC_NETHERRACK, GC_SOUL_SAND, GC_QUARTZ_ORE, GC_NETHER_BRICK, GC_FENCE, GC_STAIRS_E, GC_STAIRS_W, GC_STAIRS_S,- GC_STAIRS_N, GC_WART, GC_END_STONE, GC_FRAME, GC_FRAME_EYE, GC_N };-extern const uint16_t gen_code_block[GC_N];-void dims_slab(int cx, int cz, int y0, int h, uint8_t *out); /* gen_slab, for the dimension the player is in */-int dims_biome(int x, int z);-int dims_top(int x, int z);+#include "dims.h"+extern int dim; /* where the player is (DIM_*) */+bool world_dim(int d); /* (world.c) there: false if the changes made here could not be kept */+void world_finish(float x, float y, float z); /* the cache around there, all of it made now */+void tick_forget(void); /* (tick.c) the liquids' and fires' waiting updates dropped */+#define dims_slab gen_slab /* (gen.c runs the dimension's generator: gen_dim) */+#define dims_biome gen_biome+#define dims_top gen_top#endif/* ---------------------------------------------------------------- items (inv.c) */diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.cindex 7f612d02..6cadcc90 100644--- a/games/numblocks/src/player.c+++ b/games/numblocks/src/player.c@@ -234,6 +234,10 @@ static bool near_water(int x, int y, int z) {static bool can_stay(int b, int x, int y, int z) {int below = world_get(x, y - 1, z);int m = blk_model[b];+#if NB_DIMS+ int d = dims_can_stay(b, x, y, z);+ if (d >= 0) return d;+#endifif (b >= B_WHEAT_0 && b <= B_WHEAT_7) return below == B_FARMLAND || below == B_FARMLAND_WET;if ((b >= B_CARROTS_0 && b <= B_CARROTS_3) || (b >= B_POTATOES_0 && b <= B_POTATOES_3))return below == B_FARMLAND || below == B_FARMLAND_WET;@@ -758,6 +762,9 @@ void player_spawn(void) {}void player_respawn(void) {+#if NB_DIMS+ dims_respawn();+#endifpl.x = pl.spawn_x + 0.5f, pl.y = (float)pl.spawn_y, pl.z = pl.spawn_z + 0.5f;pl.vx = pl.vy = pl.vz = 0;pl.health = 20, pl.food = 20, pl.sat = 5, pl.exhaustion = 0, pl.air = 300, pl.fall = 0, pl.fire = 0;@@ -930,6 +937,9 @@ void player_tick(uint32_t keys, uint32_t pressed) {pl.bob += (want - pl.bob) * 0.4f;if (pl.mode == 0) exhaust(moved * (pl.in_water ? 0.015f : pl.sprinting ? 0.1f : 0.0f));hazards(y0);+#if NB_DIMS+ dims_player_tick();+#endifeffects_tick();if (pl.mode == 0) food_tick();if (pl.dead) return;diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.cindex 601afc43..470f3fd6 100644--- a/games/numblocks/src/render.c+++ b/games/numblocks/src/render.c@@ -106,7 +106,26 @@ static inline int texel_rgb(int b, int tex, int u, int v, int col, int *r, int *}/* ---------------------------------------------------------------- the sky */+#if NB_DIMS+#define CLOUDS (opt.clouds && dim == DIM_OVERWORLD)+/* RenderGlobal.renderSkyEnd: the End's sky, end_sky on the six sides of a box (16 times across each),+ * dimmed to 0x28 */+static uint16_t end_sky(float dx, float dy, float dz) {+ float ax = fabsf(dx), ay = fabsf(dy), az = fabsf(dz), u, v, m;+ if (ax >= ay && ax >= az) m = ax, u = dz, v = dy;+ else if (ay >= az) m = ay, u = dx, v = dz;+ else m = az, u = dx, v = dy;+ int tu = (int)((u / m + 1) * 128) & 15, tv = (int)((v / m + 1) * 128) & 15;+ uint16_t c = tex_pal[TX_END_SKY][texel(TX_END_SKY, tu, tv)];+ return pack(r5(c) * 40 >> 8, g6(c) * 40 >> 8, b5(c) * 40 >> 8);+}+#else+#define CLOUDS opt.clouds+#endifstatic __attribute__((noinline)) uint16_t sky(float dx, float dy, float dz, float len) {+#if NB_DIMS+ if (dim == DIM_END) return end_sky(dx, dy, dz);+#endiffloat e = dy / len; /* elevation, -1..1 *//* horizon (fog colour) to the sky's colour above; below the horizon, fog */uint32_t gc = sky_grad[e <= 0 ? 0 : e >= 0.4f ? 128 : (int)(e * 320)];@@ -154,7 +173,7 @@ static __attribute__((noinline)) uint16_t sky(float dx, float dy, float dz, floa}}/* clouds: Minecraft's 12-block cells at y = 128.33 */- if (dy > 0.01f && opt.clouds) {+ if (dy > 0.01f && CLOUDS) {float t = (128.33f - (oy + vc_y0)) / dy;float cx = (ox + vc_x0) + dx * t + cloud_off, cz = (oz + vc_z0) + dz * t;float dist = t * len;@@ -178,7 +197,11 @@ static __attribute__((noinline)) uint16_t sky(float dx, float dy, float dz, floatypedef struct {float dy, len2, sd0, sds, tx0, txs, cx0, cxs, cz0, czs, ct;} SkyRow;+#if NB_DIMS+static bool sky_fast(void) { return ss_a <= 0 && star_k <= 0 && dim != DIM_END; }+#elsestatic bool sky_fast(void) { return ss_a <= 0 && star_k <= 0; }+#endifstatic void sky_row(SkyRow *k, const float *row) {k->dy = row[1];k->len2 = row[0] * row[0] + row[1] * row[1] + row[2] * row[2]; /* (+ su^2: rg is square to the row) */@@ -214,7 +237,7 @@ static inline __attribute__((always_inline)) uint16_t sky_px(const SkyRow *k, fl}}}- if (dy > 0.01f && k->ct > 0 && opt.clouds && k->ct * k->ct * l2 < 200 * 200) {+ if (dy > 0.01f && k->ct > 0 && CLOUDS && k->ct * k->ct * l2 < 200 * 200) {int ix = (int)floorf((k->cx0 + k->cxs * su) / 12) & 255, iz = (int)floorf((k->cz0 + k->czs * su) / 12) & 255;extern const uint8_t clouds[256 * 256 / 8];if (clouds[(iz * 256 + ix) >> 3] & (1 << (ix & 7))) {@@ -1869,6 +1892,35 @@ static void ents_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {}/* ---------------------------------------------------------------- the frame */+#if NB_DIMS+/* The Nether and the End: WorldProviderHell's light table (never under 0.1), the End's light (0.22 and+ * more, EntityRenderer.updateLightmap); the fog's colour (WorldProvider*.getFogColor, mixed with the black+ * of their sky as updateFogColor does) dimmed where the eyes are dark (fogColor1, eased); no sun, moon,+ * stars or clouds; the Nether's fog close (doesXZShowFog: from 5% to half of 32 blocks) */+static float eye_bri = 1;+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++) {+ 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;+ 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;+ float want = table[l] * (1 - 2 / 32.0f) + 2 / 32.0f;+ eye_bri += (want - eye_bri) * 0.1f;+ float fr, fg, fb;+ if (dim == DIM_NETHER) fr = 0.2f, fg = 0.03f, fb = 0.03f;+ else fr = 160 / 255.0f * 0.15f, fg = 128 / 255.0f * 0.15f, fb = 160 / 255.0f * 0.15f;+ float k = (1 - 0.26f) * eye_bri;+ fog_r = (int)(fr * k * 255), fog_g = (int)(fg * k * 255), fog_b = (int)(fb * k * 255);+ sky_r = fog_r, sky_g = fog_g, sky_b = fog_b;+ sun_a = 0, star_k = 0, ss_a = 0;+ if (dim == DIM_NETHER) fog0 = 1.6f, fog1 = 16;+}+#endif+static void setup_light(float sun) {/* Minecraft's light table (gamma "Moody"): (1 - f) / (3f + 1), f = 1 - level / 15,* sky light scaled by the sun, then brightened to at least 0.03 */@@ -2084,6 +2136,9 @@ void render_frame(const Camera *c, uint32_t tod) {cam_fr[0][0] = cam_x - ox, cam_fr[0][1] = cam_x + 1 - ox;cam_fr[1][0] = cam_y - oy, cam_fr[1][1] = cam_y + 1 - oy;cam_fr[2][0] = cam_z - oz, cam_fr[2][1] = cam_z + 1 - oz;+#if NB_DIMS+ if (dim != DIM_OVERWORLD) dims_light();+#endifunder_water = is_water(cam_b);if (under_water) {fog_r = 10, fog_g = 30, fog_b = 110;diff --git a/games/numblocks/src/tick.c b/games/numblocks/src/tick.cindex e0606bcf..51480906 100644--- a/games/numblocks/src/tick.c+++ b/games/numblocks/src/tick.c@@ -20,6 +20,9 @@ typedef struct {#define N_PENDING 160static Pending pend[N_PENDING];static int npend;+#if NB_DIMS+void tick_forget(void) { npend = 0; }+#endifvoid fluid_schedule(int x, int y, int z) {int b = world_get(x, y, z);@@ -228,6 +231,9 @@ static void fire_schedule(int x, int y, int z, int age) {pend[npend++] = (Pending){(int16_t)x, (int16_t)z, (uint8_t)y, (uint8_t)age, (uint16_t)(ticks_run + 30 + rnd(10))};}void fire_set(int x, int y, int z, int age) {+#if NB_DIMS+ if (dims_portal_light(x, y, z)) return; /* (BlockFire.onBlockAdded) */+#endifworld_set(x, y, z, B_FIRE);fire_schedule(x, y, z, age);}diff --git a/games/numblocks/src/world.c b/games/numblocks/src/world.cindex 76d46dcd..1df4cc4f 100644--- a/games/numblocks/src/world.c+++ b/games/numblocks/src/world.c@@ -386,7 +386,42 @@ void world_new(int64_t seed, const char *name) {vc_valid = false;npend = 0;PEND_CLEAR();+#if NB_DIMS+ gen_dim(dim);+ edits_dim(dim);+#endif+}++#if NB_DIMS+/* the player into another dimension: its generator and records; the cache is made again where the+ * player is next followed (all of it at once). false: the changes made here did not fit in storage */+bool world_dim(int d) {+ if (!edits_flush()) return false;+ dim = d;+ gen_init(world_seed);+ gen_dim(d);+ edits_dim(d);+ edits_clear();+ tick_forget();+ vc_valid = false;+ npend = 0;+ PEND_CLEAR();+ return true;+}++/* the cache around (x, y, z), every chunk of it made now (a dimension's first frame, or blocks to set+ * there) */+void world_finish(float x, float y, float z) {+ world_follow(x, y, z);+ while (npend) {+ int x0 = pend[0].cx * 16 - vc_x0, z0 = pend[0].cz * 16 - vc_z0;+ fill_pending(0);+ 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);+ }+ world_light_flush();}+#endif/* 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 adiff --git a/games/numblocks/tests/play.c b/games/numblocks/tests/play.cindex e1ccda17..8dc1098b 100644--- a/games/numblocks/tests/play.c+++ b/games/numblocks/tests/play.c@@ -38,7 +38,7 @@ 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;+ int frames = 60, mspf = 50, go_dim = 0, portal = 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]);@@ -54,6 +54,9 @@ int main(int argc, char **argv) {else if (!strcmp(argv[i], "--creative")) creative = 1;else if (!strcmp(argv[i], "--build")) build = 1;else if (!strcmp(argv[i], "--rain")) rain = 1;+ 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], "--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) {@@ -72,6 +75,14 @@ int main(int argc, char **argv) {}}mkdir(out, 0755);+ {+ /* the saves (the edit records portals are looked for in) next to the pictures */+ extern void host_save_dir(const char *d);+ static char sd[600];+ snprintf(sd, sizeof sd, "%s/saves", out);+ mkdir(sd, 0755);+ host_save_dir(sd);+ }clock_t c0 = clock();extern bool start_in_world;start_in_world = !title_screen;@@ -102,6 +113,19 @@ int main(int argc, char **argv) {game_time = (uint32_t)start_time;}if (creative) pl.mode = 1;+ if (portal) {+ /* an obsidian frame ahead (along x), lit with flint and steel; the player walks in with --keys */+ int fy = (int)pl.y, px = (int)floorf(pl.x), pz = (int)floorf(pl.z) + 3;+ for (int z = pz - 2; z <= pz + 2; z++)+ for (int x = px - 3; x <= px + 3; x++)+ for (int y = fy - 1; y < fy + 6; y++) world_set(x, y, z, y == fy - 1 ? B_STONE : B_AIR);+ for (int x = px - 1; x <= px + 2; x++)+ for (int y = fy - 1; y <= fy + 3; y++)+ if (x == px - 1 || x == px + 2 || y == fy - 1 || y == fy + 3) world_set(x, y, pz, B_OBSIDIAN);+ fire_set(px, fy, pz, 0);+ }+ if (go_dim) dims_go(go_dim);+ 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 */weather.raining = 1, weather.rain_time = 24000, rain_str = 1;