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 CE: the window's rows twice as fast

Mason36307de77155b2d
diff --git a/games/numblocks/ce/src/gen.c b/games/numblocks/ce/src/gen.c
index 4c98b55c..db859c2f 100644
--- a/games/numblocks/ce/src/gen.c
+++ b/games/numblocks/ce/src/gen.c
@@ -6,10 +6,14 @@
* All of it is integer arithmetic on small numbers (the eZ80 has no floating point): value noise on lattices
* of 8 to 128 blocks, hashed through a permutation of 0-255 shuffled by the seed. */
#include "nb.h"
+#include "rs.h"
static uint8_t perm[256];
static uint8_t salt[8];
-static uint16_t smooth_t[256]; /* smoothstep, 0..256 for 0..255 */
+static uint8_t smooth_t[256]; /* smoothstep, 0..255 for 0..255 */
+static uint8_t HALF[WORLD_H]; /* y >> 1 */
+static uint8_t seed_gen; /* (noise corners kept are of this seed) */
+static int cave_x = -32768, cave_z = -32768;
/* the seed's own shuffle (Java's LCG would need 48-bit arithmetic: this one is 32-bit) */
static uint32_t lcg;
@@ -27,38 +31,61 @@ void gen_seed(uint32_t seed) {
perm[i] = perm[j], perm[j] = t;
}
for (int i = 0; i < 8; i++) salt[i] = (uint8_t)lcg_next();
- for (int t = 0; t < 256; t++) smooth_t[t] = (uint16_t)(((int32_t)t * t * (768 - 2 * t)) >> 16);
+ for (int t = 0; t < 256; t++) smooth_t[t] = (uint8_t)(((int32_t)t * t * (768 - 2 * t)) >> 16);
+ for (int y = 0; y < WORLD_H; y++) HALF[y] = (uint8_t)(y >> 1);
+ seed_gen++, cave_x = -32768;
}
-static inline uint8_t h2(int x, int z, int s) {
- return perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)(x + s)] + z)] + ((x >> 8) ^ (z >> 8) * 3) + s)];
+/* (byte arithmetic: on the eZ80 a shift, a mask or an xor of an int is a call) */
+static inline __attribute__((always_inline)) uint8_t h2(int x, int z, uint8_t s) {
+ uint8_t hz = (uint8_t)(z >> 8);
+ return perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)((uint8_t)x + s)] + (uint8_t)z)] +
+ (uint8_t)((uint8_t)(x >> 8) ^ (uint8_t)(hz + hz + hz)) + s)];
+}
+static uint8_t h3(int x, int y, int z, uint8_t s) {
+ return perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)((uint8_t)x + s)] + (uint8_t)y)] + (uint8_t)z)] +
+ (uint8_t)((uint8_t)(x >> 8) + (uint8_t)(z >> 8)))];
+}
+/* (h3 with a column's x and z taken in once: on the eZ80 a shift or a mask of an int is a call) */
+typedef struct { uint8_t px, z8, hi; } H3C;
+static void h3_col(H3C *h, int x, int z, uint8_t s) {
+ h->px = perm[(uint8_t)(x + s)];
+ h->z8 = (uint8_t)z;
+ h->hi = (uint8_t)((uint8_t)(x >> 8) + (uint8_t)(z >> 8));
}
-static inline uint8_t h3(int x, int y, int z, int s) {
- return perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)(x + s)] + y)] + z)] + (x >> 8) + (z >> 8))];
+static inline __attribute__((always_inline)) uint8_t h3y(const H3C *h, uint8_t y) {
+ return perm[(uint8_t)(perm[(uint8_t)(perm[(uint8_t)(h->px + y)] + h->z8)] + h->hi)];
}
#define smooth(t) smooth_t[t]
-/* value noise in 2D: 0..255, on a lattice of 1 << sh blocks */
-static int noise2(int x, int z, int sh, int s) {
+/* value noise in 2D: 0..255, on a lattice of 1 << sh blocks; the lattice square's corners kept (k) for the next
+ * column, most often in the same square */
+typedef struct { int xi, zi; uint8_t gen, a, b, c, d; } N2C;
+static int noise2(N2C *k, int x, int z, int sh, int s) {
int xi = x >> sh, zi = z >> sh;
+ if (xi != k->xi || zi != k->zi || k->gen != seed_gen) {
+ k->xi = xi, k->zi = zi, k->gen = seed_gen;
+ k->a = h2(xi, zi, (uint8_t)s), k->b = h2(xi + 1, zi, (uint8_t)s), k->c = h2(xi, zi + 1, (uint8_t)s);
+ k->d = h2(xi + 1, zi + 1, (uint8_t)s);
+ }
int fxv = smooth((x - (xi << sh)) << (8 - sh)), fzv = smooth((z - (zi << sh)) << (8 - sh));
- int a = h2(xi, zi, s), b = h2(xi + 1, zi, s), c = h2(xi, zi + 1, s), d = h2(xi + 1, zi + 1, s);
- int ab = a + (((b - a) * fxv) >> 8), cd = c + (((d - c) * fxv) >> 8);
- return ab + (((cd - ab) * fzv) >> 8);
+ return rs_lerp(rs_lerp(k->a, k->b, fxv), rs_lerp(k->c, k->d, fxv), fzv);
}
-/* biomes from temperature, rain and height */
-static int fbm2(int x, int z, int sh, int s) {
- return (noise2(x, z, sh, s) * 2 + noise2(x, z, sh - 1, s + 37) + 1) / 3;
+/* biomes from temperature, rain and height (x 683 >> 11: / 3, exactly, under 2048) */
+static int fbm2(N2C *k, int x, int z, int sh, int s) {
+ return (noise2(k, x, z, sh, s) * 2 + noise2(k + 1, x, z, sh - 1, s + 37) + 1) * 683 >> 11;
}
void gen_column(int x, int z, Column *c) {
- int cont = fbm2(x, z, 7, salt[0]); /* land or sea (128-block lattice) */
- int hill = fbm2(x, z, 6, salt[1]); /* where the extreme hills rise */
- int det = noise2(x, z, 4, salt[2]) * 2 + noise2(x, z, 3, salt[3]) + noise2(x, z, 2, salt[4]) / 2; /* 0..893 */
- int temp = fbm2(x + 1000, z, 7, salt[5]);
- int rain = fbm2(x, z + 1000, 6, salt[6]);
+ static N2C k[11];
+ int cont = fbm2(k, x, z, 7, salt[0]); /* land or sea (128-block lattice) */
+ int hill = fbm2(k + 2, x, z, 6, salt[1]); /* where the extreme hills rise */
+ int det = noise2(k + 4, x, z, 4, salt[2]) * 2 + noise2(k + 5, x, z, 3, salt[3]) +
+ noise2(k + 6, x, z, 2, salt[4]) / 2; /* 0..893 */
+ int temp = fbm2(k + 7, x + 1000, z, 7, salt[5]);
+ int rain = fbm2(k + 9, x, z + 1000, 6, salt[6]);
/* the land: up from the sea where cont is high; the hills: up to 22 blocks more */
int land = cont - 118; /* -118..137: < 0 the ocean */
int base = land < 0 ? SEA + land * 13 / 64 : SEA + 1 + land / 18;
@@ -99,22 +126,33 @@ void gen_column(int x, int z, Column *c) {
/* caves: a cell is cut out where two smooth 3D noises are both near their middle (worm-like tunnels); the
* lattice is 16 x 8 x 16 blocks, the noise read once a column at each lattice level and blended down it */
-static int cave_x = -32768, cave_z = -32768;
static uint8_t cave_a[WORLD_H / 8 + 1], cave_b[WORLD_H / 8 + 1];
-static int noise3_col(int x, int z, int ly, int s) {
+/* (a noise's lattice square around (x, z): its four corners' hashes down the column, kept for the next column in
+ * the same square, and the blend's weights) */
+typedef struct { int xi, zi; uint8_t gen, h[4][WORLD_H / 8 + 10]; int fx, fz; } N3;
+static void n3_col(N3 *n, int x, int z, uint8_t s, uint8_t l0) {
int xi = x >> 4, zi = z >> 4;
- int fxv = smooth((x & 15) << 4), fzv = smooth((z & 15) << 4);
- int a = h3(xi, ly, zi, s), b = h3(xi + 1, ly, zi, s), c = h3(xi, ly, zi + 1, s), d = h3(xi + 1, ly, zi + 1, s);
- int ab = a + (((b - a) * fxv) >> 8), cd = c + (((d - c) * fxv) >> 8);
- return ab + (((cd - ab) * fzv) >> 8);
+ n->fx = smooth((x & 15) << 4), n->fz = smooth((z & 15) << 4);
+ if (xi == n->xi && zi == n->zi && n->gen == seed_gen) return;
+ n->xi = xi, n->zi = zi, n->gen = seed_gen;
+ static H3C c[4];
+ h3_col(&c[0], xi, zi, s), h3_col(&c[1], xi + 1, zi, s);
+ h3_col(&c[2], xi, zi + 1, s), h3_col(&c[3], xi + 1, zi + 1, s);
+ for (uint8_t l = 0; l <= WORLD_H / 8; l++) {
+ uint8_t ly = (uint8_t)(l + l0);
+ n->h[0][l] = h3y(&c[0], ly), n->h[1][l] = h3y(&c[1], ly), n->h[2][l] = h3y(&c[2], ly), n->h[3][l] = h3y(&c[3], ly);
+ }
+}
+static uint8_t n3_at(const N3 *n, uint8_t l) {
+ return (uint8_t)rs_lerp(rs_lerp(n->h[0][l], n->h[1][l], n->fx), rs_lerp(n->h[2][l], n->h[3][l], n->fx), n->fz);
}
static void cave_column(int x, int z) {
if (x == cave_x && z == cave_z) return;
cave_x = x, cave_z = z;
- for (int l = 0; l <= WORLD_H / 8; l++) {
- cave_a[l] = (uint8_t)noise3_col(x, z, l, salt[2] ^ 0x5A);
- cave_b[l] = (uint8_t)noise3_col(x + 7, z + 3, l + 9, salt[4] ^ 0xA5);
- }
+ static N3 na, nb;
+ n3_col(&na, x, z, salt[2] ^ 0x5A, 0);
+ n3_col(&nb, x + 7, z + 3, salt[4] ^ 0xA5, 9);
+ for (uint8_t l = 0; l <= WORLD_H / 8; l++) cave_a[l] = n3_at(&na, l), cave_b[l] = n3_at(&nb, l);
}
static bool cave_at(int x, int y, int z) {
cave_column(x, z);
@@ -160,6 +198,68 @@ int gen_cell(int x, int y, int z, const Column *c) {
return o ? o : B_STONE;
}
+/* ---------------------------------------------------------------- a column's cells at once */
+static const uint8_t SQ13[13] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144};
+
+/* gen_cell for levels [y0, y1) of a column, into out[0..y1 - y0) */
+void gen_cells(int x, int z, const Column *c, int y0, int y1, uint8_t *out) {
+ static H3C hb, ho, hg;
+ static int8_t ca[WORLD_H], cb[WORLD_H];
+ h3_col(&hb, x, z, salt[0]);
+ h3_col(&ho, x, z, salt[1]);
+ h3_col(&hg, x >> 1, z >> 1, salt[5]);
+ /* the caves' two noises down the column, blended between their lattice levels (8 apart) */
+ cave_column(x, z);
+ int va = 0, vb = 0, da = 0, db = 0;
+ for (uint8_t y = (uint8_t)y0; y < (uint8_t)y1; y++) {
+ uint8_t k = y & 7;
+ if (!k || y == (uint8_t)y0) {
+ uint8_t l = HALF[HALF[HALF[y]]];
+ int a0 = cave_a[l], b0 = cave_b[l];
+ da = (cave_a[l + 1] - a0) << 5, db = (cave_b[l + 1] - b0) << 5;
+ va = ((a0 - 128) << 8) + da * k, vb = ((b0 - 128) << 8) + db * k;
+ }
+ ca[y] = (int8_t)(uint8_t)(va >> 8), cb[y] = (int8_t)(uint8_t)(vb >> 8);
+ va += da, vb += db;
+ }
+ uint8_t h = c->h, top = c->top, filler = c->filler, low = (uint8_t)(h - c->depth);
+ bool sea_ok = h >= SEA;
+ for (uint8_t y = (uint8_t)y0; y < (uint8_t)y1; y++) {
+ uint8_t b;
+ if (!y) b = B_BEDROCK;
+ else if (y < 4 && (h3y(&hb, y) & 3) >= y) b = B_BEDROCK;
+ else if (y > h) b = y < SEA ? (c->biome == BIOME_COLD_TAIGA && y == SEA - 1 ? B_ICE : B_WATER) : B_AIR;
+ else {
+ /* caves: not right under the sea floor or a lake, and not through the beaches */
+ int8_t a = ca[y], e = cb[y];
+ if (y > 3 && (sea_ok || y + 3 < h) && a > -13 && a < 13 && e > -13 && e < 13 &&
+ SQ13[a < 0 ? -a : a] + SQ13[e < 0 ? -e : e] < 150)
+ b = y < 8 ? B_LAVA : B_AIR;
+ else if (y == h)
+ b = h < SEA - 1 && top == B_GRASS ? B_DIRT : top == B_GRASS_SNOWED && h < SEA ? B_DIRT : top;
+ else if (y > low) b = filler;
+ else if (filler == B_SAND && y + 3 > low) b = B_SANDSTONE;
+ else {
+ /* ores (ore_at) */
+ b = B_STONE;
+ uint8_t k = h3y(&ho, y);
+ if (k >= 110) {
+ uint8_t g = h3y(&hg, HALF[y]);
+ if (g < 7) b = y < 64 ? B_COAL_ORE : B_STONE;
+ else if (g < 11) b = y < 32 ? B_IRON_ORE : B_STONE;
+ else if (g < 12) b = y < 16 ? B_GOLD_ORE : B_STONE;
+ else if (g < 14) b = y < 8 ? B_REDSTONE_ORE : B_STONE;
+ else if (g == 14) b = y < 8 && (k & 1) ? B_DIAMOND_ORE : B_STONE;
+ else if (g == 15) b = y < 16 && y > 4 && (k & 3) == 0 ? B_LAPIS_ORE : B_STONE;
+ else if (g == 16 && (k & 1)) b = B_GRAVEL;
+ else if (g == 17 && (k & 1)) b = B_DIRT;
+ }
+ }
+ }
+ *out++ = b;
+ }
+}
+
void gen_spawn(int *x, int *z) {
/* near 0, 0: plains or a forest (Minecraft's spawn biomes), else any land that is not a beach */
Column c;
diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.c
index 1e77ce79..46a3d443 100644
--- a/games/numblocks/ce/src/main.c
+++ b/games/numblocks/ce/src/main.c
@@ -148,11 +148,11 @@ static void look(uint32_t keys, uint32_t dt) {
static void play(uint32_t keys, uint32_t pressed) {
uint32_t now = plat_ms(), dt = now - last_ms;
last_ms = now;
- if (dt > 250) dt = 250;
+ if (dt > 500) dt = 500; /* (the game's 20 ticks a second kept down to 2 pictures a second) */
if (gui == GUI_NONE) look(keys, dt);
tick_acc += dt;
int n = 0;
- while (tick_acc >= 50 && n < 4) {
+ while (tick_acc >= 50 && n < 10) {
tick_acc -= 50, n++;
if (gui == GUI_NONE) player_tick(keys, n == 1 ? pressed : 0);
gui_tick();
@@ -162,7 +162,8 @@ static void play(uint32_t keys, uint32_t pressed) {
}
if (tick_acc > 200) tick_acc = 0;
world_center(FXI(pl.x), FXI(pl.y) + 1, FXI(pl.z), false);
- for (int k = 0; k < 2 && world_busy(); k++) world_step();
+ /* a row a picture, more when the view would see past them */
+ for (int k = 0; world_busy() && (!k || world_behind()); k++) world_step();
if (gui == GUI_NONE) {
if (pressed & K_INV) {
gui_open(pl.mode ? GUI_CREATIVE : GUI_INVENTORY);
@@ -238,6 +239,56 @@ int main(void) {
LAP("world");
render_sky_palette();
LAP("sky");
+#if NB_BENCH == 7
+ {
+ /* one picture, as the computer build makes it (to compare) */
+ in_game = true, gui = GUI_NONE;
+ render_frame();
+ hud_draw();
+ compose(true, true);
+ for (uint32_t t = plat_ms(); plat_ms() - t < 3000;) plat_frame();
+ plat_exit();
+ }
+#endif
+#if NB_BENCH >= 5 && NB_BENCH <= 6
+ {
+ /* rows of the window made (5: 16 of them, 6: none) */
+ for (int f = 0; f < (NB_BENCH == 5 ? 16 : 0); f++) {
+ pl.x += FX1;
+ world_center(FXI(pl.x), FXI(pl.y) + 1, FXI(pl.z), false);
+ while (world_busy()) world_step();
+ }
+ LAP("rows");
+ plat_save("NBBENCH", out, (unsigned)n, 0, 0);
+ plat_exit();
+ }
+#endif
+#if NB_BENCH == 4
+ {
+ /* walking: 16 frames of 3 ticks each, each part's average */
+ static const char *const part[5] = {"ticks", "rows", "render", "hud", "compose"};
+ uint32_t sum[5] = {0, 0, 0, 0, 0};
+ in_game = true, gui = GUI_NONE;
+ for (int f = 0; f < 16; f++) {
+ uint32_t t0 = plat_ms();
+ for (int k = 0; k < 3; k++) player_tick(K_FWD | K_SPRINT, 0);
+ uint32_t t1 = plat_ms();
+ world_center(FXI(pl.x), FXI(pl.y) + 1, FXI(pl.z), false);
+ for (int k = 0; k < 2 && world_busy(); k++) world_step();
+ uint32_t t2 = plat_ms();
+ render_frame();
+ uint32_t t3 = plat_ms();
+ hud_draw();
+ uint32_t t4 = plat_ms();
+ compose(true, true);
+ uint32_t t5 = plat_ms();
+ sum[0] += t1 - t0, sum[1] += t2 - t1, sum[2] += t3 - t2, sum[3] += t4 - t3, sum[4] += t5 - t4;
+ }
+ for (int k = 0; k < 5; k++) n += sprintf(out + n, "%s %lu\n", part[k], (unsigned long)(sum[k] / 16));
+ plat_save("NBBENCH", out, (unsigned)n, 0, 0);
+ plat_exit();
+ }
+#endif
for (int v = 0; v < (NB_BENCH > 1 ? 24 : 4); v++) {
pl.yaw = (uint16_t)((v & 3) * 16384);
pl.pitch = (v & 3) == 3 ? 6000 : 0;
diff --git a/games/numblocks/ce/src/nb.h b/games/numblocks/ce/src/nb.h
index 32eac2c7..2833d004 100644
--- a/games/numblocks/ce/src/nb.h
+++ b/games/numblocks/ce/src/nb.h
@@ -44,6 +44,7 @@ int world_light(int x, int y, int z); /* the light byte there */
void world_new(uint32_t seed);
void world_center(int x, int y, int z, bool all); /* keeps the window around (x, y, z) */
bool world_busy(void); /* parts of the window still being made */
+bool world_behind(void); /* so many that the view would see past them */
void world_step(void); /* makes a little more of them */
int world_top(int x, int z); /* the highest block's y + 1 (spawning) */
extern uint32_t world_seed;
@@ -62,7 +63,8 @@ typedef struct {
} Column;
void gen_seed(uint32_t seed);
void gen_column(int x, int z, Column *c);
-int gen_cell(int x, int y, int z, const Column *c); /* the terrain, caves, ores, water */
+int gen_cell(int x, int y, int z, const Column *c);
+void gen_cells(int x, int z, const Column *c, int y0, int y1, uint8_t *out); /* gen_cell for [y0, y1) */ /* the terrain, caves, ores, water */
/* trees and plants whose blocks fall in [x0, x1) x [y0, y1) x [z0, z1): put(x, y, z, block) for each */
void gen_decorate(int x0, int z0, int x1, int z1, int y0, int y1, void (*put)(int, int, int, int));
int gen_plant(int x, int z, const Column *c); /* the plant on top of a column (0: none) */
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index 3d0a0de7..23714c3f 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -168,7 +168,7 @@ void render_sky_palette(void) {
/* ---------------------------------------------------------------- textures and shades in RAM, 256-aligned */
/* (rs_poly's loop reads a texel and its shade by setting a page's low byte: both tables 256-byte aligned) */
-#define TC_N 32
+#define TC_N 28
static uint8_t tcache[TC_N][256] __attribute__((aligned(256)));
static uint8_t shade_ram[N_SHADE][256] __attribute__((aligned(256)));
static uint8_t fogc[FC_N][256] __attribute__((aligned(256)));
@@ -202,7 +202,7 @@ static const uint8_t *tex_of(uint8_t t) {
if (s == 255) {
/* a slot not used in this frame (round and round) */
for (uint8_t n = 0; n < TC_N; n++) {
- s = (uint8_t)(tc_next++ & (TC_N - 1));
+ s = tc_next, tc_next = tc_next + 1 == TC_N ? 0 : (uint8_t)(tc_next + 1);
if (tc_used[s] != frame_no) break;
}
if (tc_tex[s] != 255) tc_slot[tc_tex[s]] = 255;
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index 9c3ac5ba..5e196383 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -20,7 +20,10 @@ void RS(rs_proj)(int x, int y, int z, int *out);
uint8_t RS(rs_hidden)(int x, int y, int z, int r);
int RS(rs_code)(int x, int y, int z);
void RS(rs_compose)(uint8_t *dst, const uint8_t *a, const uint8_t *f);
+int RS(rs_lerp)(int a, int b, int f);
void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);
+int RS(rs_lerp)(int a, int b, int f) { return a + (((b - a) * f) >> 8); }
+
void RS(rs_compose)(uint8_t *dst, const uint8_t *a, const uint8_t *f) {
for (int i = 0; i < 256; i++) dst[i] = f[a[i]];
}
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index a1599db3..a6eae783 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -23,6 +23,8 @@ uint8_t rs_hidden(int x, int y, int z, int r);
int rs_code(int x, int y, int z);
/* dst[i] = f[a[i]] for the 256 bytes, all three 256-byte aligned */
void rs_compose(uint8_t *dst, const uint8_t *a, const uint8_t *f);
+/* a + floor((b - a) f / 256), a and b 0..255, f 0..256 */
+int rs_lerp(int a, int b, int f);
/* the 160 x 120 picture to the screen at 2x, 0s as the row's sky colour (sky[y]); from screen row 180 on, the HUD's
* layer (187 wide at x 67, 0 see-through) over it (hud 0: none) */
void rs_expand(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);
diff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.s
index 4cc0a6e1..9ad08878 100644
--- a/games/numblocks/ce/src/rs.s
+++ b/games/numblocks/ce/src/rs.s
@@ -1856,6 +1856,355 @@ _rs_compose:
jr nz, .Lcm_loop
ret
+; ------------------------------------------------------------------------------------------------- rs_lerp
+; int rs_lerp(int a, int b, int f): a + floor((b - a) f / 256), a and b 0..255, f 0..256
+ .section .text._rs_lerp
+ .global _rs_lerp
+ .type _rs_lerp, @function
+_rs_lerp:
+ ld iy, 0
+ add iy, sp
+ ld a, (iy+10)
+ or a, a
+ jr nz, .Llp_b
+ ld a, (iy+6)
+ sub a, (iy+3)
+ jr c, .Llp_neg
+ ld h, a
+ ld l, (iy+9)
+ mlt hl
+ ld a, h
+ add a, (iy+3)
+ sbc hl, hl
+ ld l, a
+ ret
+.Llp_neg:
+ neg
+ ld h, a
+ ld l, (iy+9)
+ mlt hl
+ ld de, 255
+ add hl, de
+ ld a, (iy+3)
+ sub a, h
+ sbc hl, hl
+ ld l, a
+ ret
+.Llp_b:
+ ld hl, (iy+6)
+ ret
+
+; ------------------------------------------------------------------------------------------------- w_neighbours
+; void w_neighbours(uint16_t i): the window cell i's neighbours inside it (-Y +Y -Z +Z -X +X) into w_nbr, how many
+; into w_nbr_n (world.c; i = y << 10 | z << 5 | x, each 0..31; w_esx.. the window's first and last slots)
+ .section .text._w_neighbours
+ .global _w_neighbours
+ .type _w_neighbours, @function
+_w_neighbours:
+ ld iy, 0
+ add iy, sp
+ ld l, (iy+3)
+ ld h, (iy+4)
+ ld iy, _w_nbr
+ ld c, 0
+ ld a, h
+ rrca
+ rrca
+ and a, 0x1F
+ ld b, a ; y
+ ld a, (_w_esy)
+ cp a, b
+ jr z, .Lnb_y1
+ ld a, h
+ sub a, 4
+ and a, 0x7C
+ ld d, a
+ ld a, h
+ and a, 3
+ or a, d
+ ld (iy+1), a
+ ld (iy+0), l
+ lea iy, iy+2
+ inc c
+.Lnb_y1:
+ ld a, (_w_eey)
+ cp a, b
+ jr z, .Lnb_z0
+ ld a, h
+ add a, 4
+ and a, 0x7C
+ ld d, a
+ ld a, h
+ and a, 3
+ or a, d
+ ld (iy+1), a
+ ld (iy+0), l
+ lea iy, iy+2
+ inc c
+.Lnb_z0:
+ ld a, h
+ and a, 3
+ rlca
+ rlca
+ rlca
+ ld b, a
+ ld a, l
+ rlca
+ rlca
+ rlca
+ and a, 7
+ or a, b
+ ld b, a ; z
+ ld a, (_w_esz)
+ cp a, b
+ jr z, .Lnb_z1
+ ld a, b
+ dec a
+ and a, 31
+ call nb_z
+ inc c
+.Lnb_z1:
+ ld a, (_w_eez)
+ cp a, b
+ jr z, .Lnb_x0
+ ld a, b
+ inc a
+ and a, 31
+ call nb_z
+ inc c
+.Lnb_x0:
+ ld a, l
+ and a, 31
+ ld b, a ; x
+ ld a, (_w_esx)
+ cp a, b
+ jr z, .Lnb_x1
+ ld a, b
+ dec a
+ and a, 31
+ ld d, a
+ ld a, l
+ and a, 0xE0
+ or a, d
+ ld (iy+0), a
+ ld (iy+1), h
+ lea iy, iy+2
+ inc c
+.Lnb_x1:
+ ld a, (_w_eex)
+ cp a, b
+ jr z, .Lnb_done
+ ld a, b
+ inc a
+ and a, 31
+ ld d, a
+ ld a, l
+ and a, 0xE0
+ or a, d
+ ld (iy+0), a
+ ld (iy+1), h
+ inc c
+.Lnb_done:
+ ld a, c
+ ld (_w_nbr_n), a
+ ret
+
+; z' in a: lo = (z' & 7) << 5 | x, hi = y << 2 | z' >> 3, to (iy), iy on (b, c, h, l kept)
+nb_z:
+ ld e, a
+ and a, 7
+ rrca
+ rrca
+ rrca
+ ld d, a
+ ld a, l
+ and a, 31
+ or a, d
+ ld (iy+0), a
+ ld a, e
+ rrca
+ rrca
+ rrca
+ and a, 3
+ ld d, a
+ ld a, h
+ and a, 0x7C
+ or a, d
+ ld (iy+1), a
+ lea iy, iy+2
+ ret
+
+; ------------------------------------------------------------------------------------------------- w_expose
+; void w_expose(void): a window column's cells with a face to draw into xp_out (4 bytes, bit y & 31), from its cell
+; at the window's bottom (xp_p, slot xp_ys) up; a face shows unless what is next to it is opaque, the same block or
+; both are liquids; plants and torches always; past the window: below it as if solid, above air; the columns
+; around it at xp_ox0.. (bits of xp_sides: 1 -x, 2 +x, 4 -z, 8 +z inside the window) (world.c)
+ .section .text._w_expose
+ .global _w_expose
+ .type _w_expose, @function
+ .equ M_CROSS, 2
+ .equ M_TORCH, 6
+ .equ BF_OPAQUE, 1
+ .equ BF_LIQUID, 8
+_w_expose:
+ push ix
+ ld ix, (_xp_p)
+ xor a, a
+ ld (xp_k), a
+ ld hl, xp_m
+ ld (hl), a
+ inc hl
+ ld (hl), a
+ inc hl
+ ld (hl), a
+ inc hl
+ ld (hl), a
+.Lxp_cell:
+ ld a, (_xp_ys)
+ cp a, 31
+ ld de, 1024
+ jr nz, .Lxp_up
+ ld de, -31 * 1024
+.Lxp_up:
+ lea hl, ix+0
+ add hl, de
+ ld (xp_up), hl
+ ld a, (ix+0)
+ or a, a
+ jp z, .Lxp_next
+ ld (xp_b), a
+ ld de, 0
+ ld e, a
+ ld hl, _blk_model
+ add hl, de
+ ld a, (hl)
+ cp a, M_CROSS
+ jp z, .Lxp_show
+ cp a, M_TORCH
+ jp z, .Lxp_show
+ ld a, (xp_k)
+ cp a, 31
+ jp z, .Lxp_show
+ ld hl, _blk_flags
+ add hl, de
+ ld a, (hl)
+ and a, BF_LIQUID
+ ld (xp_liq), a
+ ld hl, (xp_up)
+ call xp_shows
+ jp nz, .Lxp_show
+ ld a, (xp_k)
+ or a, a
+ jr z, .Lxp_sides
+ ld a, (_xp_ys)
+ or a, a
+ ld de, -1024
+ jr nz, .Lxp_dn
+ ld de, 31 * 1024
+.Lxp_dn:
+ lea hl, ix+0
+ add hl, de
+ call xp_shows
+ jp nz, .Lxp_show
+.Lxp_sides:
+ ld a, (_xp_sides)
+ bit 2, a
+ jr z, .Lxp_s1
+ ld de, (_xp_oz0)
+ lea hl, ix+0
+ add hl, de
+ call xp_shows
+ jp nz, .Lxp_show
+.Lxp_s1:
+ ld a, (_xp_sides)
+ bit 3, a
+ jr z, .Lxp_s2
+ ld de, (_xp_oz1)
+ lea hl, ix+0
+ add hl, de
+ call xp_shows
+ jp nz, .Lxp_show
+.Lxp_s2:
+ ld a, (_xp_sides)
+ bit 0, a
+ jr z, .Lxp_s3
+ ld de, (_xp_ox0)
+ lea hl, ix+0
+ add hl, de
+ call xp_shows
+ jp nz, .Lxp_show
+.Lxp_s3:
+ ld a, (_xp_sides)
+ bit 1, a
+ jr z, .Lxp_next
+ ld de, (_xp_ox1)
+ lea hl, ix+0
+ add hl, de
+ call xp_shows
+ jr z, .Lxp_next
+.Lxp_show:
+ ld a, (_xp_ys)
+ ld c, a
+ rrca
+ rrca
+ rrca
+ and a, 3
+ ld hl, xp_m
+ ld de, 0
+ ld e, a
+ add hl, de
+ ld a, c
+ and a, 7
+ ld b, a
+ ld a, 1
+ jr z, .Lxp_bit
+.Lxp_sh:
+ add a, a
+ djnz .Lxp_sh
+.Lxp_bit:
+ or a, (hl)
+ ld (hl), a
+.Lxp_next:
+ ld ix, (xp_up)
+ ld a, (_xp_ys)
+ inc a
+ and a, 31
+ ld (_xp_ys), a
+ ld a, (xp_k)
+ inc a
+ ld (xp_k), a
+ cp a, 32
+ jp nz, .Lxp_cell
+ ld hl, xp_m
+ ld de, (_xp_out)
+ ld bc, 4
+ ldir
+ pop ix
+ ret
+
+; nz when the cell at hl shows xp_b's face next to it
+xp_shows:
+ ld a, (hl)
+ ld hl, xp_b
+ cp a, (hl)
+ ret z
+ ld de, 0
+ ld e, a
+ ld hl, _blk_flags
+ add hl, de
+ ld a, (hl)
+ ld c, a
+ and a, BF_OPAQUE
+ jr nz, .Lxs_hid
+ ld a, (xp_liq)
+ and a, c
+ jr nz, .Lxs_hid
+ or a, 1
+ ret
+.Lxs_hid:
+ xor a, a
+ ret
+
; ------------------------------------------------------------------------------------------------- data
.section .data._rs_vars
r_tex: .d24 0
@@ -2044,6 +2393,27 @@ q_a: .db 0
q_c: .db 0
q_xs: .db 0
q_zs: .db 0
+ .global _xp_p
+ .global _xp_out
+ .global _xp_ox0
+ .global _xp_ox1
+ .global _xp_oz0
+ .global _xp_oz1
+ .global _xp_ys
+ .global _xp_sides
+_xp_p: .d24 0
+_xp_out: .d24 0
+_xp_ox0: .d24 0
+_xp_ox1: .d24 0
+_xp_oz0: .d24 0
+_xp_oz1: .d24 0
+_xp_ys: .db 0
+_xp_sides: .db 0
+xp_up: .d24 0
+xp_k: .db 0
+xp_b: .db 0
+xp_liq: .db 0
+xp_m: .ds 4
.section .rodata._rs_rtab
; 1371 << 12 / (256 + i)
@@ -2055,5 +2425,15 @@ r_rtab:
.extern _rbuf
.extern _rs_cell
.extern _wexp
+ .extern _w_nbr
+ .extern _blk_model
+ .extern _blk_flags
+ .extern _w_nbr_n
+ .extern _w_esx
+ .extern _w_eex
+ .extern _w_esz
+ .extern _w_eez
+ .extern _w_esy
+ .extern _w_eey
.extern _rs_recip
.extern _rs_ratio
diff --git a/games/numblocks/ce/src/world.c b/games/numblocks/ce/src/world.c
index 0603e4f1..669bddc4 100644
--- a/games/numblocks/ce/src/world.c
+++ b/games/numblocks/ce/src/world.c
@@ -102,11 +102,17 @@ typedef union { uint16_t w; uint8_t b[2]; } U16;
static const uint16_t YOFF[32] = {0, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 9216, 10240,
11264, 12288, 13312, 14336, 15360, 16384, 17408, 18432, 19456, 20480, 21504,
22528, 23552, 24576, 25600, 26624, 27648, 28672, 29696, 30720, 31744};
-static uint8_t e_sx, e_ex, e_sz, e_ez, e_sy, e_ey; /* the window's first and last slots */
+uint8_t w_esx, w_eex, w_esz, w_eez, w_esy, w_eey; /* the window's first and last slots (rs.s too) */
+/* y & 31 for y 0..63, and the slot after and before one (masks of bytes are widened to ints, and calls) */
+static const uint8_t MOD32[64] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
+ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
+#define NEXT32(s) ((s) == 31 ? 0 : (uint8_t)((s) + 1))
+#define PREV32(s) ((s) ? (uint8_t)((s) - 1) : 31)
static void edges(void) {
- e_sx = (uint8_t)win_x & 31, e_ex = (uint8_t)(e_sx + 31) & 31;
- e_sz = (uint8_t)win_z & 31, e_ez = (uint8_t)(e_sz + 31) & 31;
- e_sy = (uint8_t)win_y & 31, e_ey = (uint8_t)(e_sy + 31) & 31;
+ w_esx = MOD32[win_x & 31], w_eex = PREV32(w_esx);
+ w_esz = MOD32[win_z & 31], w_eez = PREV32(w_esz);
+ w_esy = MOD32[win_y & 31], w_eey = PREV32(w_esy);
}
/* the y slot's level */
static inline int wy_of(uint8_t slot) { return win_y + (uint8_t)((uint8_t)(slot - (uint8_t)win_y) & 31); }
@@ -127,48 +133,55 @@ static void lq_push(uint16_t i) {
lq_t = t;
}
-/* the neighbours of cell i inside the window (edges() first): nbr[0..nbr_n) */
-static uint16_t nbr[6];
-static uint8_t nbr_n;
+/* the neighbours of cell i inside the window (edges() first): nbr[0..nbr_n), -Y +Y -Z +Z -X +X (rs.s on the CE) */
+uint16_t w_nbr[6];
+uint8_t w_nbr_n;
+#define nbr w_nbr
+#define nbr_n w_nbr_n
+#ifdef __ez80__
+void w_neighbours(uint16_t i);
+#define neighbours w_neighbours
+#else
static void neighbours(uint16_t i) {
- static U16 u;
- u.w = i;
- uint8_t lo = u.b[0], hi = u.b[1], n = 0;
+ uint8_t lo = (uint8_t)i, hi = (uint8_t)(i >> 8), n = 0;
uint8_t x = lo & 31, z = (uint8_t)((hi & 3) << 3 | lo >> 5), y = hi >> 2;
- if (y != e_sy) u.b[0] = lo, u.b[1] = (uint8_t)(((uint8_t)(hi - 4) & 0x7C) | (hi & 3)), nbr[n++] = u.w;
- if (y != e_ey) u.b[0] = lo, u.b[1] = (uint8_t)(((uint8_t)(hi + 4) & 0x7C) | (hi & 3)), nbr[n++] = u.w;
- if (z != e_sz) {
- uint8_t z2 = (uint8_t)(z - 1) & 31;
- u.b[0] = (uint8_t)((z2 & 7) << 5 | x), u.b[1] = (uint8_t)((hi & 0x7C) | z2 >> 3), nbr[n++] = u.w;
+ if (y != w_esy) nbr[n++] = (uint16_t)((((hi - 4) & 0x7C) | (hi & 3)) << 8 | lo);
+ if (y != w_eey) nbr[n++] = (uint16_t)((((hi + 4) & 0x7C) | (hi & 3)) << 8 | lo);
+ if (z != w_esz) {
+ uint8_t z2 = (z - 1) & 31;
+ nbr[n++] = (uint16_t)(((hi & 0x7C) | z2 >> 3) << 8 | (z2 & 7) << 5 | x);
}
- if (z != e_ez) {
- uint8_t z2 = (uint8_t)(z + 1) & 31;
- u.b[0] = (uint8_t)((z2 & 7) << 5 | x), u.b[1] = (uint8_t)((hi & 0x7C) | z2 >> 3), nbr[n++] = u.w;
+ if (z != w_eez) {
+ uint8_t z2 = (z + 1) & 31;
+ nbr[n++] = (uint16_t)(((hi & 0x7C) | z2 >> 3) << 8 | (z2 & 7) << 5 | x);
}
- if (x != e_sx) u.b[0] = (uint8_t)((lo & 0xE0) | ((uint8_t)(x - 1) & 31)), u.b[1] = hi, nbr[n++] = u.w;
- if (x != e_ex) u.b[0] = (uint8_t)((lo & 0xE0) | ((uint8_t)(x + 1) & 31)), u.b[1] = hi, nbr[n++] = u.w;
+ if (x != w_esx) nbr[n++] = (uint16_t)(hi << 8 | (lo & 0xE0) | ((x - 1) & 31));
+ if (x != w_eex) nbr[n++] = (uint16_t)(hi << 8 | (lo & 0xE0) | ((x + 1) & 31));
nbr_n = n;
}
+#endif
/* spreads both lights from the queued cells */
+static const uint8_t OP16[16] = {0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240};
static void light_spread(void) {
edges();
while (lq_h != lq_t) {
uint16_t i = lq[lq_h];
lq_h = lq_h + 1 == LQ_N ? 0 : lq_h + 1;
- uint8_t l = wlight[i], s = l & 15, b = l >> 4;
- if (s <= 1 && b <= 1) continue;
+ /* (block light kept in the high nibble: no shifting, a call on the eZ80) */
+ uint8_t l = wlight[i], s = l & 15, b = l & 0xF0;
+ if (s <= 1 && b <= 0x10) continue;
neighbours(i);
for (uint8_t k = 0; k < nbr_n; k++) {
uint16_t n = nbr[k];
uint8_t op = blk_opacity[wblk[n]];
if (op >= 15) continue;
if (!op) op = 1;
- uint8_t nl = wlight[n], ns = nl & 15, nb = nl >> 4, ch = 0;
+ uint8_t op16 = OP16[op], nl = wlight[n], ns = nl & 15, nb = nl & 0xF0, ch = 0;
if (s > op && (uint8_t)(s - op) > ns) ns = (uint8_t)(s - op), ch = 1;
- if (b > op && (uint8_t)(b - op) > nb) nb = (uint8_t)(b - op), ch = 1;
+ if (b > op16 && (uint8_t)(b - op16) > nb) nb = (uint8_t)(b - op16), ch = 1;
if (ch) {
- wlight[n] = (uint8_t)(ns | nb << 4);
+ wlight[n] = (uint8_t)(ns | nb);
lq_push(n);
}
}
@@ -219,47 +232,70 @@ static void dark_push(uint16_t i, bool blk) {
static int column_top(int x, int z) {
int c = CI(x, z);
if (wcol[c].h >= win_y + WH) return win_y + WH;
- uint8_t ys = (uint8_t)(win_y + WH - 1) & 31;
+ uint8_t ys = MOD32[win_y + WH - 1];
for (int y = win_y + WH - 1; y >= win_y; y--) {
if (blk_opacity[wblk[YOFF[ys] + c]]) return y + 1;
- ys = (uint8_t)(ys - 1) & 31;
+ ys = PREV32(ys);
}
return win_y;
}
/* ---------------------------------------------------------------- faces to draw */
-static inline bool hides(uint8_t b, uint8_t nb) {
- if (blk_flags[nb] & BF_OPAQUE) return true;
- if (nb == b && blk_model[b] != M_CROSS && blk_model[b] != M_TORCH) return true;
- if ((blk_flags[b] & BF_LIQUID) && (blk_flags[nb] & BF_LIQUID)) return true;
- return false;
-}
-static const uint8_t BIT8[8] = {1, 2, 4, 8, 16, 32, 64, 128};
-/* a column's cells with a face to draw (past the window: below it, as if solid; above, air) */
-static void expose_col(int x, int z) {
- if ((unsigned)(x - win_x) >= WS || (unsigned)(z - win_z) >= WS) return;
- int c = CI(x, z);
- uint8_t sx = (uint8_t)x & 31, sz = (uint8_t)z & 31;
- bool hx0 = x > win_x, hx1 = x < win_x + WS - 1, hz0 = z > win_z, hz1 = z < win_z + WS - 1;
- int ox0 = sx ? -1 : 31, ox1 = sx != 31 ? 1 : -31, oz0 = sz ? -32 : 31 * 32, oz1 = sz != 31 ? 32 : -31 * 32;
+static const uint8_t BIT32[32] = {1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128,
+ 1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128};
+static const uint8_t BYTE8[32] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
+ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3};
+/* a column's cells with a face to draw (past the window: below it, as if solid; above, air): a face shows unless
+ * what is next to it is opaque, the same block (not a plant), or both are liquids (rs.s on the CE) */
+#ifdef __ez80__
+extern const uint8_t *xp_p;
+extern uint8_t *xp_out;
+extern int xp_ox0, xp_ox1, xp_oz0, xp_oz1;
+extern uint8_t xp_ys, xp_sides;
+void w_expose(void);
+#else
+static const uint8_t *xp_p;
+static uint8_t *xp_out;
+static int xp_ox0, xp_ox1, xp_oz0, xp_oz1;
+static uint8_t xp_ys, xp_sides;
+static void w_expose(void) {
static uint8_t m[4];
m[0] = m[1] = m[2] = m[3] = 0;
- uint8_t ys = (uint8_t)win_y & 31;
- const uint8_t *p = wblk + YOFF[ys] + c;
+ const uint8_t *p = xp_p;
+ uint8_t ys = xp_ys;
for (uint8_t k = 0; k < 32; k++) {
uint8_t b = *p;
const uint8_t *up = ys == 31 ? p - 31 * 1024 : p + 1024;
if (b != B_AIR) {
uint8_t md = blk_model[b];
- if (md == M_CROSS || md == M_TORCH || k == 31 || !hides(b, *up) ||
- (k && !hides(b, ys ? p[-1024] : p[31 * 1024])) || (hz0 && !hides(b, p[oz0])) ||
- (hz1 && !hides(b, p[oz1])) || (hx0 && !hides(b, p[ox0])) || (hx1 && !hides(b, p[ox1])))
- m[ys >> 3] |= BIT8[ys & 7];
+ bool e = md == M_CROSS || md == M_TORCH || k == 31;
+ if (!e) {
+ uint8_t liq = blk_flags[b] & BF_LIQUID, n, fn;
+#define SHOWS(q) (n = (q), fn = blk_flags[n], !(fn & BF_OPAQUE) && n != b && !(liq && (fn & BF_LIQUID)))
+ e = SHOWS(*up) || (k && SHOWS(ys ? p[-1024] : p[31 * 1024])) || ((xp_sides & 4) && SHOWS(p[xp_oz0])) ||
+ ((xp_sides & 8) && SHOWS(p[xp_oz1])) || ((xp_sides & 1) && SHOWS(p[xp_ox0])) ||
+ ((xp_sides & 2) && SHOWS(p[xp_ox1]));
+#undef SHOWS
+ }
+ if (e) m[BYTE8[ys]] |= BIT32[ys];
}
p = up;
- ys = (uint8_t)(ys + 1) & 31;
+ ys = NEXT32(ys);
}
- memcpy(&wexp[c], m, 4);
+ memcpy(xp_out, m, 4);
+}
+#endif
+static void expose_col(int x, int z) {
+ if ((unsigned)(x - win_x) >= WS || (unsigned)(z - win_z) >= WS) return;
+ int c = CI(x, z);
+ uint8_t sx = MOD32[x - win_x + (win_x & 31)], sz = MOD32[z - win_z + (win_z & 31)];
+ xp_sides = (uint8_t)((x > win_x ? 1 : 0) | (x < win_x + WS - 1 ? 2 : 0) | (z > win_z ? 4 : 0) |
+ (z < win_z + WS - 1 ? 8 : 0));
+ xp_ox0 = sx ? -1 : 31, xp_ox1 = sx != 31 ? 1 : -31, xp_oz0 = sz ? -32 : 31 * 32, xp_oz1 = sz != 31 ? 32 : -31 * 32;
+ xp_ys = MOD32[win_y];
+ xp_p = wblk + YOFF[xp_ys] + c;
+ xp_out = (uint8_t *)&wexp[c];
+ w_expose();
}
/* the columns around a cell that changed */
static void expose_around(int x, int z) {
@@ -278,10 +314,49 @@ static void put_cb(int x, int y, int z, int b) {
wblk[i] = (uint8_t)b;
}
+/* (fill's loops down a column, each on its own: few values at once, kept in registers) */
+__attribute__((noinline)) static void col_copy(uint8_t *p, uint8_t ys, const uint8_t *cells, uint8_t n) {
+ for (; n; n--, cells++) {
+ p[YOFF[ys]] = *cells;
+ ys = NEXT32(ys);
+ }
+}
+__attribute__((noinline)) static void col_light(uint16_t c, uint8_t ys, uint8_t y, uint8_t y1, uint8_t top) {
+ for (; y < y1; y++, ys = NEXT32(ys)) {
+ uint16_t i = (uint16_t)(YOFF[ys] + c);
+ uint8_t bl = blk_light[wblk[i]], v = y >= top ? 15 : 0;
+ if (bl) v |= (uint8_t)(bl << 4), lq_push(i);
+ wlight[i] = v;
+ }
+}
+__attribute__((noinline)) static void col_seed_border(uint16_t c, uint16_t cn, uint8_t ys, uint8_t y, uint8_t y1) {
+ for (; y < y1; y++, ys = NEXT32(ys)) {
+ uint16_t o = YOFF[ys];
+ uint8_t l = wlight[o + c];
+ if (!l) continue;
+ uint8_t nl = wlight[o + cn];
+ if (((l & 15) > (uint8_t)((nl & 15) + 1) || ((nl & 0xF0) < 0xE0 && (l & 0xF0) > (uint8_t)((nl & 0xF0) + 0x10))) &&
+ blk_opacity[wblk[o + cn]] < 15)
+ lq_push((uint16_t)(o + c));
+ }
+}
+__attribute__((noinline)) static void col_seed(uint16_t c, uint8_t ys, uint8_t y, uint8_t y1, uint8_t top, uint8_t nt, bool inside) {
+ uint8_t wy = (uint8_t)win_y;
+ for (; y < y1; y++, ys = NEXT32(ys)) {
+ uint16_t i = (uint16_t)(YOFF[ys] + c);
+ uint8_t l = wlight[i];
+ if (!l) continue;
+ /* a sky-lit cell matters only next to a column whose sky stops higher, or above leaves or water */
+ if (inside && l == 15 && nt <= y && (y != top || y <= wy || blk_opacity[wblk[YOFF[PREV32(ys)] + c]] >= 15))
+ continue;
+ lq_push(i);
+ }
+}
+
/* makes the columns [x0, x1) x [z0, z1) at levels [y0, y1) (cols: their column data too), then their light and
* faces */
static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
- uint8_t ys0 = (uint8_t)y0 & 31;
+ uint8_t ys0 = MOD32[y0];
for (int z = z0; z < z1; z++)
for (int x = x0; x < x1; x++) {
int c = CI(x, z);
@@ -290,15 +365,13 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
gen_column(x, z, col);
col_pack(&wcol[c], col);
} else col_unpack(&wcol[c], col);
- uint8_t *p = wblk + c, ys = ys0;
- for (int y = y0; y < y1; y++) {
- p[YOFF[ys]] = (uint8_t)gen_cell(x, y, z, col);
- ys = (uint8_t)(ys + 1) & 31;
- }
+ uint8_t *p = wblk + c, cells[WH];
+ gen_cells(x, z, col, y0, y1, cells);
+ col_copy(p, ys0, cells, (uint8_t)(y1 - y0));
int py = col->h + 1;
- if (py >= y0 && py < y1 && p[YOFF[(uint8_t)py & 31]] == B_AIR && p[YOFF[(uint8_t)(py - 1) & 31]] == col->top) {
+ if (py >= y0 && py < y1 && p[YOFF[MOD32[py]]] == B_AIR && p[YOFF[MOD32[py - 1]]] == col->top) {
int pl = gen_plant(x, z, col);
- if (pl) p[YOFF[(uint8_t)py & 31]] = (uint8_t)pl;
+ if (pl) p[YOFF[MOD32[py]]] = (uint8_t)pl;
}
}
gen_decorate(x0, z0, x1, z1, y0, y1, put_cb);
@@ -315,16 +388,9 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
int c = CI(x, z), top = column_top(x, z);
if (!cols && top > wtop[c])
for (int y = wtop[c]; y < top && y < y0; y++)
- if (y >= win_y) dark_push((uint16_t)(YOFF[(uint8_t)y & 31] + c), false);
+ if (y >= win_y) dark_push((uint16_t)(YOFF[MOD32[y]] + c), false);
wtop[c] = (uint8_t)top;
- uint8_t ys = ys0;
- for (int y = y0; y < y1; y++) {
- uint16_t i = (uint16_t)(YOFF[ys] + c);
- uint8_t bl = blk_light[wblk[i]];
- wlight[i] = (uint8_t)((y >= top ? 15 : 0) | bl << 4);
- if (bl) lq_push(i);
- ys = (uint8_t)(ys + 1) & 31;
- }
+ col_light((uint16_t)c, ys0, (uint8_t)y0, (uint8_t)y1, (uint8_t)top);
}
if (dq_n) light_darken();
/* spreading it: from the lit cells next to darker ones (the region's and the cells around it) */
@@ -333,27 +399,27 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
if ((unsigned)(x - win_x) >= WS || (unsigned)(z - win_z) >= WS) continue;
int c = CI(x, z), top = wtop[c];
bool inside = x >= x0 && x < x1 && z >= z0 && z < z1;
+ if (!inside) {
+ /* a column around the region: the cells brighter than the region's next to them by more than a step
+ * (none at the corners: not next to it) */
+ bool ox = x < x0 || x >= x1, oz = z < z0 || z >= z1;
+ if (ox && oz) continue;
+ int cn = CI(x < x0 ? x0 : x >= x1 ? x1 - 1 : x, z < z0 ? z0 : z >= z1 ? z1 - 1 : z);
+ col_seed_border((uint16_t)c, (uint16_t)cn, ys0, (uint8_t)y0, (uint8_t)y1);
+ if (x == x1) light_spread();
+ continue;
+ }
/* (the highest top of the columns around: a sky-lit cell under it lights them sideways) */
- int nt = 0;
- if (x > win_x && wtop[CI(x - 1, z)] > nt) nt = wtop[CI(x - 1, z)];
- if (x < win_x + WS - 1 && wtop[CI(x + 1, z)] > nt) nt = wtop[CI(x + 1, z)];
- if (z > win_z && wtop[CI(x, z - 1)] > nt) nt = wtop[CI(x, z - 1)];
- if (z < win_z + WS - 1 && wtop[CI(x, z + 1)] > nt) nt = wtop[CI(x, z + 1)];
+ uint8_t sx = MOD32[x - win_x + (win_x & 31)], sz = MOD32[z - win_z + (win_z & 31)], nt = 0;
+ const uint8_t *tp = wtop + c;
+ if (x > win_x && tp[sx ? -1 : 31] > nt) nt = tp[sx ? -1 : 31];
+ if (x < win_x + WS - 1 && tp[sx != 31 ? 1 : -31] > nt) nt = tp[sx != 31 ? 1 : -31];
+ if (z > win_z && tp[sz ? -32 : 31 * 32] > nt) nt = tp[sz ? -32 : 31 * 32];
+ if (z < win_z + WS - 1 && tp[sz != 31 ? 32 : -31 * 32] > nt) nt = tp[sz != 31 ? 32 : -31 * 32];
int ya = inside ? y0 - 1 : y0, yb = inside ? y1 + 1 : y1;
if (ya < win_y) ya = win_y;
if (yb > win_y + WH) yb = win_y + WH;
- uint8_t ys = (uint8_t)ya & 31;
- for (int y = ya; y < yb; y++, ys = (uint8_t)(ys + 1) & 31) {
- uint16_t i = (uint16_t)(YOFF[ys] + c);
- uint8_t l = wlight[i];
- if (!l) continue;
- /* a sky-lit cell matters only next to a column whose sky stops higher, or above leaves or water */
- if (inside && l == 15) {
- bool edge = nt > y || (y == top && y > win_y && blk_opacity[wblk[YOFF[(uint8_t)(ys - 1) & 31] + c]] < 15);
- if (!edge) continue;
- }
- lq_push(i);
- }
+ col_seed((uint16_t)c, MOD32[ya], (uint8_t)ya, (uint8_t)yb, (uint8_t)top, nt, inside);
if (x == x1) light_spread();
}
light_spread();
@@ -439,6 +505,11 @@ bool world_busy(void) {
want_y - win_y > 2 || win_y - want_y > 2;
}
+bool world_behind(void) {
+ /* (more rows to make than the view's edge can wait for: it sees 13 blocks at most, the window holds 16) */
+ return want_x - win_x > 3 || win_x - want_x > 3 || want_z - win_z > 3 || win_z - want_z > 3;
+}
+
/* one row (or level) of the window made: the side the player went towards */
void world_step(void) {
lq_h = lq_t = 0;