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 CE: faster world rows, faces past the view passed by, a render distance option
Mason3633715fa461917
diff --git a/games/numblocks/ce/host/plat_host.c b/games/numblocks/ce/host/plat_host.cindex 166b8810..5afb6c85 100644--- a/games/numblocks/ce/host/plat_host.c+++ b/games/numblocks/ce/host/plat_host.c@@ -110,6 +110,11 @@ void plat_frame(void) {fprintf(stderr, "polys %lu edges %lu rows %lu visited %lu drawn %lu\n", rs_polys, rs_edges, rs_rows, rs_visited, rs_drawn);rs_visited = rs_drawn = rs_rows = rs_edges = rs_polys = 0;extern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;+ extern unsigned long st_rl, st_ru, st_h[8], st_c2, st_c3;+ fprintf(stderr, "clip2 %lu clip3 %lu\n", st_c2, st_c3);+ st_c2 = st_c3 = 0;+ fprintf(stderr, "rows locked %lu unlocked %lu; polys by rows %lu %lu %lu %lu %lu %lu %lu+\n", st_rl, st_ru, st_h[1], st_h[2], st_h[3], st_h[4], st_h[5], st_h[6], st_h[7]);+ st_rl = st_ru = 0; memset(st_h, 0, sizeof st_h);fprintf(stderr, "cols %lu listed %lu in view %lu tested %lu culled %lu\n", st_cols, st_list, st_frus, st_cov, st_culled);fprintf(stderr, "frame %d: %u cells, %u faces, %u spans, %u pixels; at %.2f %.2f %.2f yaw %d pitch %d, win %d %d %d\n",frame_no, stat_cells, stat_faces, stat_spans, stat_px, pl.x / 4096.0, pl.y / 4096.0, pl.z / 4096.0, pl.yaw,diff --git a/games/numblocks/ce/src/gui.c b/games/numblocks/ce/src/gui.cindex 3f8c9b9a..69a5c2e2 100644--- a/games/numblocks/ce/src/gui.c+++ b/games/numblocks/ce/src/gui.c@@ -414,11 +414,11 @@ typedef struct { int16_t x, y, w; uint8_t on, act; const char *label; } Btn;static Btn btns[8];static int n_btns;enum { BA_PLAY, BA_NEW, BA_QUIT, BA_SELECT, BA_CREATE, BA_CANCEL, BA_MODE, BA_DO_CREATE, BA_DELETE, BA_DO_DELETE,- BA_RESUME, BA_SAVE_QUIT, BA_RESPAWN, BA_TITLE, BA_DONE };+ BA_RESUME, BA_SAVE_QUIT, BA_RESPAWN, BA_TITLE, BA_DONE, BA_DIST };static WorldInfo winfo[MAX_WORLDS];static int wslot[MAX_WORLDS], n_worlds, wsel;static int create_mode;-static char mode_label[24];+static char mode_label[24], dist_label[28];static void add_btn(int x, int y, int w, int on, int act, const char *label) {btns[n_btns++] = (Btn){(int16_t)x, (int16_t)y, (int16_t)w, (uint8_t)on, (uint8_t)act, label};@@ -468,6 +468,9 @@ static void menu_layout(void) {break;case GUI_PAUSE:add_btn(w / 2 - 100, h / 4 + 8, 200, 1, BA_RESUME, "Back to Game");+ strcpy(dist_label, view_dist == 8 ? "Render Distance: 8 blocks"+ : view_dist == 10 ? "Render Distance: 10 blocks" : "Render Distance: 13 blocks");+ add_btn(w / 2 - 100, h / 4 + 56, 200, 1, BA_DIST, dist_label);add_btn(w / 2 - 100, h / 4 + 104, 200, 1, BA_SAVE_QUIT, "Save and Quit to Title");break;case GUI_DEATH:@@ -712,6 +715,13 @@ void gui_input(uint32_t keys, uint32_t pressed) {case BA_RESPAWN: menu_action = ACT_RESPAWN; break;case BA_TITLE: menu_action = ACT_TITLE; break;case BA_DONE: gui_close(); break;+ case BA_DIST:+ /* (fewer blocks seen: more pictures a second) */+ view_dist = view_dist == 8 ? 10 : view_dist == 10 ? 13 : 8;+ options_save();+ menu_layout();+ shown = false;+ break;}}diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.cindex e86b5e36..1e77ce79 100644--- a/games/numblocks/ce/src/main.c+++ b/games/numblocks/ce/src/main.c@@ -52,10 +52,17 @@ static void to_title(void) {gui_open(GUI_TITLE);}-static bool seen_controls(void) {+/* the options (NBCEOPT): the controls seen once, the render distance */+static bool controls_seen;+static void options_load(void) {unsigned len;const uint8_t *p = plat_load("NBCEOPT", &len);- return p && len >= 1 && p[0] == 1;+ controls_seen = p && len >= 1 && p[0] == 1;+ if (p && len >= 2 && (p[1] == 8 || p[1] == 10 || p[1] == 13)) view_dist = p[1];+}+void options_save(void) {+ uint8_t o[2] = {controls_seen, (uint8_t)view_dist};+ plat_save("NBCEOPT", o, 2, 0, 0);}static void start_game(void) {@@ -63,9 +70,9 @@ static void start_game(void) {gui = GUI_NONE;render_sky_palette();last_ms = plat_ms(), tick_acc = 0, ticks_since_save = 0;- if (!seen_controls()) {- static const uint8_t one = 1;- plat_save("NBCEOPT", &one, 1, 0, 0);+ if (!controls_seen) {+ controls_seen = true;+ options_save();render_frame();compose(false, false);gui_open(GUI_CONTROLS);@@ -177,6 +184,7 @@ int main(void) {wlight = wblk + WS * WS * WH;hud_layer = wblk + 2 * WS * WS * WH;render_init();+ options_load();canvas_screen();#ifdef NB_BENCH#include <stdio.h>diff --git a/games/numblocks/ce/src/nb.h b/games/numblocks/ce/src/nb.hindex 9dfdea25..32eac2c7 100644--- a/games/numblocks/ce/src/nb.h+++ b/games/numblocks/ce/src/nb.h@@ -178,6 +178,7 @@ enum {uint32_t keys_now(void);int rnd(int n);void game_save_quit(void);+void options_save(void);/* fixed-point helpers */int16_t fsin(uint16_t a); /* 2.14 */diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.cindex a0ecba16..3d0a0de7 100644--- a/games/numblocks/ce/src/render.c+++ b/games/numblocks/ce/src/render.c@@ -24,7 +24,7 @@ uint32_t stat_faces, stat_spans, stat_px, stat_cells;#ifdef HOSTextern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;#endif-int view_dist = 13;+int view_dist = 10;uint32_t world_time;int crack_stage = -1;const uint8_t *rs_recip, *rs_ratio;@@ -39,11 +39,12 @@ static int bx0, by0, bz0; /* the camera-space position of the eye#define F16 1371L /* 60 / tan(35 degrees), in 1/16 pixels: Minecraft's 70 degree view */#define NEAR (Q1 / 20)-static uint8_t lvl_sky[16], lvl_blk[16]; /* shade level of each light level */+static uint8_t lvl_sky[16], lvl_blk[16], lvl_of[256]; /* (lvl_of: a light byte's, the brighter of its two) */ /* shade level of each light level */static const uint8_t face_off[6] = {6, 0, 2, 2, 4, 4}; /* -Y +Y -Z +Z -X +X: 0.5 1.0 0.8 0.8 0.6 0.6 */-static uint8_t fog_cm[8][256]; /* fog rows: 1/9 to 8/9 of the way to the fog's colour */-#define FC_N 8-static uint16_t fc_key[FC_N]; /* the shade and fog rows made into one (shade_of) */+static uint8_t fog_cm[8][256] __attribute__((aligned(256))); /* fog rows: 1/9 to 8/9 of the way to the fog */+#define FC_N 16+static uint8_t fc_key[FC_N]; /* the shade and fog rows made into one (shade_of): level x 8 + fog - 1 */+static uint8_t fc_slot[256]; /* and where each is (255: not made) */static uint8_t fog_rgb[3], sky_rgb[3];static int fog_q; /* the fog's colour as last made into fog_cm */static int crack_x, crack_y, crack_z;@@ -135,6 +136,10 @@ void render_sky_palette(void) {lvl_blk[l] = (uint8_t)level_of(blk);lvl_sky[l] = (uint8_t)level_of(sky);}+ for (int i = 0; i < 256; i++) {+ uint8_t a = lvl_sky[i & 15], b = lvl_blk[i >> 4];+ lvl_of[i] = a > b ? a : b;+ }/* the sky (Minecraft's plains sky: HSB 0.6, 0.54, 1) and the fog (0.75, 0.85, 1), darker at night */int dk = day_k;sky_rgb[0] = (uint8_t)(120 * dk / 256), sky_rgb[1] = (uint8_t)(167 * dk / 256), sky_rgb[2] = (uint8_t)(255 * dk / 256);@@ -157,7 +162,7 @@ void render_sky_palette(void) {uint8_t w = (uint8_t)((f + 1) * 256 / 9);for (int i = 1; i < 256; i++) fog_cm[f][i] = i < FIRST_STATIC ? (uint8_t)i : lerp_pal(i, fog_rgb, w);}- memset(fc_key, 255, sizeof fc_key);+ memset(fc_key, 255, sizeof fc_key), memset(fc_slot, 255, sizeof fc_slot);}}@@ -174,37 +179,55 @@ static uint8_t fc_next;static void caches_reset(void) {memset(tc_slot, 255, sizeof tc_slot);memset(tc_tex, 255, sizeof tc_tex);- memset(fc_key, 255, sizeof fc_key);+ memset(fc_key, 255, sizeof fc_key), memset(fc_slot, 255, sizeof fc_slot);}/* texture t, in RAM */-static const uint8_t *tex_of(int t) {+/* page k of a 256-byte aligned table (on the eZ80 a shift is a call: the address's middle byte instead) */+static const uint8_t *page(const void *base, uint8_t k) {+#ifdef __ez80__+ static union { const void *p; uint8_t b[3]; } u;+ u.p = base;+ uint8_t m = (uint8_t)(u.b[1] + k);+ if (m < k) u.b[2]++;+ u.b[1] = m;+ return u.p;+#else+ return (const uint8_t *)base + k * 256;+#endif+}++static const uint8_t *tex_of(uint8_t t) {uint8_t s = tc_slot[t];if (s == 255) {/* a slot not used in this frame (round and round) */- for (int n = 0; n < TC_N; n++) {+ for (uint8_t n = 0; n < TC_N; n++) {s = (uint8_t)(tc_next++ & (TC_N - 1));if (tc_used[s] != frame_no) break;}if (tc_tex[s] != 255) tc_slot[tc_tex[s]] = 255;- tc_tex[s] = (uint8_t)t, tc_slot[t] = s;- memcpy(tcache[s], tex_px[t], 256);+ tc_tex[s] = t, tc_slot[t] = s;+ memcpy((void *)page(tcache, s), tex_px[t], 256);}tc_used[s] = frame_no;- return tcache[s];+ return page(tcache, s);}/* the shade row of a level (0..31), through fog (1..8 eighths of the way, 0 none) */-static const uint8_t *shade_of(int level, int fog) {- if (!fog) return shade_ram[level];- uint16_t key = (uint16_t)(level << 4 | fog);- for (int i = 0; i < FC_N; i++)- if (fc_key[i] == key) return fogc[i];- uint8_t s = (uint8_t)(fc_next++ & (FC_N - 1));- fc_key[s] = key;- const uint8_t *a = shade_ram[level], *f = fog_cm[fog - 1];- uint8_t *o = fogc[s];- for (int i = 0; i < 256; i++) o[i] = f[a[i]];+static const uint8_t KEY8[32] = {0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120,+ 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248};+static const uint8_t *shade_of(uint8_t level, uint8_t fog) {+ if (!fog) return page(shade_ram, level);+ /* (through fog, light levels two by two: half as many rows to make) */+ level &= 0x1E;+ uint8_t key = (uint8_t)(KEY8[level] | (uint8_t)(fog - 1)), s = fc_slot[key];+ if (s != 255) return page(fogc, s);+ s = (uint8_t)(fc_next++ & (FC_N - 1));+ if (fc_slot[fc_key[s]] == s) fc_slot[fc_key[s]] = 255;+ fc_key[s] = key, fc_slot[key] = s;+ const uint8_t *a = page(shade_ram, level), *f = page(fog_cm, (uint8_t)(fog - 1));+ uint8_t *o = (uint8_t *)page(fogc, s);+ rs_compose(o, a, f);return o;}@@ -230,14 +253,14 @@ static int clip2(SV *in, int n, SV *out, int k) {}if (da >= 0) out[m++] = *a;if ((da >= 0) != (db >= 0)) {+#ifdef HOST+ { extern unsigned long st_c2; st_c2++; }+#endifint t = (int)(((int32_t)da << 12) / (da - db)); /* 0..4096 */SV *o = &out[m++];- o->x = lerp12(a->x, b->x, t), o->y = lerp12(a->y, b->y, t);o->u = lerp12(a->u, b->u, t), o->v = lerp12(a->v, b->v, t);- if (k == 0) o->x = 0;- else if (k == 1) o->x = RW * 16;- else if (k == 2) o->y = 0;- else o->y = RH * 16;+ if (k < 2) o->x = k ? RW * 16 : 0, o->y = lerp12(a->y, b->y, t);+ else o->y = k == 2 ? 0 : RH * 16, o->x = lerp12(a->x, b->x, t);}}return m;@@ -281,6 +304,9 @@ static int clip3(CV *in, int n, CV *out, int k) {}if (da >= 0) out[m++] = *a;if ((da >= 0) != (db >= 0)) {+#ifdef HOST+ { extern unsigned long st_c3; st_c3++; }+#endifint32_t dd = (int32_t)da - db, aa = da;while (dd > 262143 || dd < -262143) dd >>= 1, aa >>= 1;int t = (int)((aa << 12) / dd);@@ -294,13 +320,15 @@ static int clip3(CV *in, int n, CV *out, int k) {}/* (the general way: clipped to the near plane and the guard band, then projected; near faces, edges of the view) */-static void draw_quad(CV *q, int n) {+static const uint8_t BIT[8] = {1, 2, 4, 8, 16, 32, 64, 128};+static void draw_quad(CV *q, int n, uint8_t planes) {static CV ca[16], cb[16];CV *a = q, *b = ca;- for (int k = 0; k < 5 && n >= 3; k++) {- n = clip3(a, n, b, k);- a = b, b = a == ca ? cb : ca;- }+ for (uint8_t k = 0; k < 5 && n >= 3; k++)+ if (planes & BIT[k]) {+ n = clip3(a, n, b, k);+ a = b, b = a == ca ? cb : ca;+ }if (n < 3) return;static SV s[16];int o[2];@@ -316,12 +344,12 @@ static void draw_quad(CV *q, int n) {static int fq[12];static SV fs[4] = {{0, 0, 0, 0}, {0, 0, 65535, 0}, {0, 0, 65535, 65535}, {0, 0, 0, 65535}};-static bool fq_far(int x, int y, int z) {- /* in front, and well inside the guard band (|x|, |y| < 4z) */- if (z < NEAR + NEAR) return false;- int z4 = z + z;- z4 += z4;- return x < z4 && x + z4 > 0 && y < z4 && y + z4 > 0;+/* rs_code's two halves for a point: the planes to cut by, the view's sides it is past */+static uint8_t code_clip, code_side;+static void code(int x, int y, int z) {+ static union { int i; uint8_t b[3]; } u;+ u.i = rs_code(x, y, z);+ code_clip = u.b[0], code_side = u.b[1];}/* four projected corners: drawn, cut to the screen when they are not all on it */@@ -335,20 +363,29 @@ static void poly4(SV *v) {raster(v, 4);}-/* a near face's grid of (split + 1) x (split + 1) points: camera space, projected when far enough */+/* a near face's grid of (split + 1) x (split + 1) points: camera space, projected when no plane cuts them */static CV gp[25];static SV gq[25];-static uint8_t gfar[25];+static uint8_t gclip[25], gside[25];static const uint16_t GUV2[3] = {0, 32768, 65535}, GUV4[5] = {0, 16384, 32768, 49152, 65535};/* the face in fq, split in split x split parts (1: whole; near faces 2 or 4, as the texture is not drawn in- * perspective) */+ * perspective); passed by when all its corners are past one side of the view */static void quad(uint8_t split) {stat_faces++;- rs_setup(s_tex, s_cm, s_alpha);if (split == 1) {- if (fq_far(fq[0], fq[1], fq[2]) && fq_far(fq[3], fq[4], fq[5]) && fq_far(fq[6], fq[7], fq[8]) &&- fq_far(fq[9], fq[10], fq[11])) {+ uint8_t cl, sd;+ code(fq[0], fq[1], fq[2]);+ cl = code_clip, sd = code_side;+ code(fq[3], fq[4], fq[5]);+ cl |= code_clip, sd &= code_side;+ code(fq[6], fq[7], fq[8]);+ cl |= code_clip, sd &= code_side;+ code(fq[9], fq[10], fq[11]);+ cl |= code_clip, sd &= code_side;+ if (sd) return;+ rs_setup(s_tex, s_cm, s_alpha);+ if (!cl) {rs_proj(fq[0], fq[1], fq[2], &fs[0].x);rs_proj(fq[3], fq[4], fq[5], &fs[1].x);rs_proj(fq[6], fq[7], fq[8], &fs[2].x);@@ -359,9 +396,10 @@ static void quad(uint8_t split) {static CV q[4] = {{0, 0, 0, 0, 0}, {0, 0, 0, 65535, 0}, {0, 0, 0, 65535, 65535}, {0, 0, 0, 0, 65535}};q[0].x = fq[0], q[0].y = fq[1], q[0].z = fq[2], q[1].x = fq[3], q[1].y = fq[4], q[1].z = fq[5];q[2].x = fq[6], q[2].y = fq[7], q[2].z = fq[8], q[3].x = fq[9], q[3].y = fq[10], q[3].z = fq[11];- draw_quad(q, 4);+ draw_quad(q, 4, cl);return;}+ rs_setup(s_tex, s_cm, s_alpha);/* the grid: rows from c0 + t e3 to c1 + t e3, the last from c3 to c2 (the face's edges exact: no cracks) */int e1x = fq[3] - fq[0], e1y = fq[4] - fq[1], e1z = fq[5] - fq[2];int e3x = fq[9] - fq[0], e3y = fq[10] - fq[1], e3z = fq[11] - fq[2];@@ -370,7 +408,7 @@ static void quad(uint8_t split) {const uint16_t *uvt = split == 2 ? GUV2 : GUV4;CV *p = gp;SV *q = gq;- uint8_t *fa = gfar;+ uint8_t *gc = gclip, *gs = gside;int rx = fq[0], ry = fq[1], rz = fq[2], ex = fq[3], ey = fq[4], ez = fq[5];for (uint8_t t = 0; t <= split; t++) {if (t == split) rx = fq[9], ry = fq[10], rz = fq[11], ex = fq[6], ey = fq[7], ez = fq[8];@@ -379,11 +417,13 @@ static void quad(uint8_t split) {for (uint8_t s = 0; s <= split; s++) {if (s == split) x = ex, y = ey, z = ez;p->x = x, p->y = y, p->z = z, p->u = uvt[s], p->v = v;- if ((*fa = fq_far(x, y, z))) {+ code(x, y, z);+ *gc = code_clip, *gs = code_side;+ if (!code_clip) {rs_proj(x, y, z, &q->x);q->u = p->u, q->v = v;}- p++, q++, fa++;+ p++, q++, gc++, gs++;x += e1x, y += e1y, z += e1z;}rx += e3x, ry += e3y, rz += e3z, ex += e3x, ey += e3y, ez += e3z;@@ -392,21 +432,24 @@ static void quad(uint8_t split) {uint8_t w = (uint8_t)(split + 1);CV *a = gp, *b = gp + w;SV *qa = gq, *qb = gq + w;- uint8_t *f0 = gfar, *f1 = gfar + w;+ uint8_t *c0 = gclip, *c1 = gclip + w, *s0 = gside, *s1 = gside + w;for (uint8_t t = 0; t < split; t++) {for (uint8_t s = 0; s < split; s++) {- if (f0[0] && f0[1] && f1[0] && f1[1]) {- static SV v4[4];- v4[0] = qa[0], v4[1] = qa[1], v4[2] = qb[1], v4[3] = qb[0];- poly4(v4);- } else if (a[0].z >= NEAR || a[1].z >= NEAR || b[0].z >= NEAR || b[1].z >= NEAR) {- static CV c4[4];- c4[0] = a[0], c4[1] = a[1], c4[2] = b[1], c4[3] = b[0];- draw_quad(c4, 4);+ if (!(s0[0] & s0[1] & s1[0] & s1[1])) {+ uint8_t cl = c0[0] | c0[1] | c1[0] | c1[1];+ if (!cl) {+ static SV v4[4];+ v4[0] = qa[0], v4[1] = qa[1], v4[2] = qb[1], v4[3] = qb[0];+ poly4(v4);+ } else {+ static CV c4[4];+ c4[0] = a[0], c4[1] = a[1], c4[2] = b[1], c4[3] = b[0];+ draw_quad(c4, 4, cl);+ }}- a++, b++, qa++, qb++, f0++, f1++;+ a++, b++, qa++, qb++, c0++, c1++, s0++, s1++;}- a++, b++, qa++, qb++, f0++, f1++;+ a++, b++, qa++, qb++, c0++, c1++, s0++, s1++;}}@@ -440,10 +483,8 @@ static void face_corners(int f) {static const uint8_t FACE_C[6][4] = {{4, 5, 1, 0}, {2, 3, 7, 6}, {3, 2, 0, 1}, {6, 7, 5, 4}, {2, 6, 4, 0}, {7, 3, 1, 5}};static const uint8_t CROSS_C[2][4] = {{2, 7, 5, 0}, {6, 3, 1, 4}};-static int light_level(uint8_t lb, uint8_t side) {- uint8_t l = lvl_sky[lb & 15], b = lvl_blk[lb >> 4];- if (b > l) l = b;- b = face_off[side];+static uint8_t light_level(uint8_t lb, uint8_t side) {+ uint8_t l = lvl_of[lb], b = face_off[side];return l < b ? 0 : l - b;}@@ -478,7 +519,6 @@ static int nb_index(int f) {}}-static const uint8_t BIT[8] = {1, 2, 4, 8, 16, 32, 64, 128};static uint16_t blk_tex_off[B_N]; /* b x 6 */__attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int fog) {@@ -540,13 +580,11 @@ __attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int fstatic uint8_t sky_row[RH];static void sky_rows(void) {/* the height in the sky of the ray through a row's middle (at the screen's middle column): 0 the horizon,- * the sky's top colour from 0.4 up */- int32_t t0 = (int32_t)(RH / 2 * 16 - 8) * Q1 / F16, dt = 16 * Q1 / F16; /* Q14 */- for (int y = 0; y < RH; y++) {- int32_t ry = fxy + ((t0 - dt * y) * uxy >> Q);- int e = (int)(ry * 75 / 2 >> Q);- sky_row[y] = (uint8_t)(e <= 0 ? 1 : e >= 14 ? 15 : e + 1);- }+ * the sky's top colour from 0.4 up; x 75/2, Q14: linear in the row */+ int32_t t0 = (int32_t)(RH / 2 * 16 - 8) * Q1 / F16, dt = 16 * Q1 / F16;+ int32_t e = (fxy + (t0 * uxy >> Q)) * 75 / 2, de = (dt * uxy >> Q) * 75 / 2;+ for (int y = 0; y < RH; y++, e -= de)+ sky_row[y] = e < Q1 ? 1 : e >= 14 * Q1 ? 15 : (uint8_t)((e >> Q) + 1);}/* ---------------------------------------------------------------- a frame */diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.cindex 173a5436..9c3ac5ba 100644--- a/games/numblocks/ce/src/rs.c+++ b/games/numblocks/ce/src/rs.c@@ -18,7 +18,27 @@ void RS(rs_setup)(const uint8_t *tex, const uint8_t *cm, uint8_t alpha);void RS(rs_poly)(const SV *p, uint8_t n);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);void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);+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]];+}++int RS(rs_code)(int x, int y, int z) {+ int c = 0, v = 0;+ if (z < 819) c |= 1, v |= 1;+ if (x > 4 * z) c |= 2;+ if (-x > 4 * z) c |= 4;+ if (y > 4 * z) c |= 8;+ if (-y > 4 * z) c |= 16;+ if (16 * x > 15 * z) v |= 2;+ if (-16 * x > 15 * z) v |= 4;+ if (4 * y > 3 * z) v |= 8;+ if (-4 * y > 3 * z) v |= 16;+ return c | v << 8;+}+/* (a range of pixels around c, r each way: its first, how many; 0: none on the screen) */static int h_range(int c, int r, int last, int *first) {int lo = c - r, hi = c + r;@@ -136,6 +156,9 @@ void RS(rs_poly)(const SV *p, uint8_t n) {}int y = (ymin + 7) >> 4, yend = (ymax + 7) >> 4;if (y >= yend) return;+#ifndef __ez80__+ { extern unsigned long st_h[8]; int hh = yend - y; st_h[hh > 7 ? 7 : hh]++; }+#endifWalker a = {0}, b = {0}, *l, *r;a.idx = b.idx = top;if (!edge(&a, p, n, 1) || !edge(&b, p, n, n - 1)) return;@@ -166,6 +189,9 @@ void RS(rs_poly)(const SV *p, uint8_t n) {}}}+#ifndef __ez80__+ { extern unsigned long st_rl, st_ru; if (lock) st_rl++; else st_ru++; }+#endifif (lock) {int k = (int)((unsigned)(128 - l->x) & 255) >> 4;u0 = W24(l->u + ku[k]), v0 = W24(l->v + kv[k]);@@ -226,7 +252,7 @@ uint16_t rs_jS[128];uint8_t rs_fog[257];int rs_vlim, rs_cy0, rs_cz0, rs_vzk, rs_R, rs_KX;uint8_t rs_eys, rs_up, rs_down, rs_jbase;-unsigned long st_list, st_frus, st_cov, st_culled, st_cols;+unsigned long st_list, st_frus, st_cov, st_culled, st_cols, st_rl, st_ru, st_h[8], st_c2, st_c3;int rs_qXi, rs_qYi, rs_qZi, rs_qVi, rs_qsXx, rs_qsYx, rs_qsZx, rs_qsVx, rs_qsXz, rs_qsYz, rs_qsZz, rs_qsVz;int rs_qx0, rs_qz0, rs_qsxi, rs_qszi, rs_czm, rs_cx0, rs_wx0, rs_wx1, rs_wz0, rs_wz1;const uint16_t *rs_qsqi, *rs_qsqk;diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.hindex c67e2bff..a1599db3 100644--- a/games/numblocks/ce/src/rs.h+++ b/games/numblocks/ce/src/rs.h@@ -17,6 +17,12 @@ void rs_proj(int x, int y, int z, int *out);/* 1 when the pixels around (x, y, z)'s projection (z > 2r), as far as r's projection and a pixel more each way, are* all drawn already (or off the screen) */uint8_t rs_hidden(int x, int y, int z, int r);+/* bits 0-4: the planes a polygon through camera space (x, y, z) is to be cut by (z < 819, x > 4z, -x > 4z, y > 4z,+ * -y > 4z); bits 8-12: the view's sides it is past, a little wider than the screen's (z < 819, 16x > 15z,+ * -16x > 15z, 4y > 3z, -4y > 3z) */+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);/* 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.sindex c9eb13b7..4cc0a6e1 100644--- a/games/numblocks/ce/src/rs.s+++ b/games/numblocks/ce/src/rs.s@@ -587,41 +587,31 @@ _rs_poly:add hl, deld (r_row), hl.Lpo_loop:- ; px0 = (xl + 127) >> 8 (0 if xl < 0), px1 = (xr + 127) >> 8, at most 160- ld ix, (w_l)- ld hl, (ix+W_X)+ ; px1 = (xr + 127) >> 8, at most 160; px0 = (xl + 127) >> 8 (x >= 0 on the screen: the high byte)ld de, 127- add hl, de- ld (m_t), hl- ld a, (m_t+2)- bit 7, a- ld a, (m_t+1)- jr z, .Lpo_x0- xor a, a-.Lpo_x0:- ld (r_px0), ald ix, (w_r)ld hl, (ix+W_X)add hl, de- ld (m_t), hl- ld a, (m_t+2)- or a, a- ld a, (m_t+1)- jr z, .Lpo_x1- ld a, RW ; (past the right: never, on screen)-.Lpo_x1:+ ld a, hcp a, RW + 1jr c, .Lpo_x1okld a, RW.Lpo_x1ok:- ld hl, r_px0- sub a, (hl)+ ld c, a+ ld ix, (w_l)+ ld hl, (ix+W_X)+ add hl, de+ ld a, c+ sub a, hjp z, .Lpo_stepjp c, .Lpo_stepld (r_n), a+ ld a, h+ ld (r_px0), ald a, (r_lock)or a, ajp nz, .Lpo_locked+ ld ix, (w_r); w = (xr - xl) >> 4: du, dv across it (0 under a pixel)ld hl, (ix+W_X)ld ix, (w_l)@@ -732,8 +722,8 @@ _rs_poly:ld (r_dv), hl ; dv >> 4jr .Lpo_span.Lpo_locked:- ; from the edge to the first pixel's middle: the table's step for (128 - xl) >> 4- ld ix, (w_l)+ ; (ix: the left walker) from the edge to the first pixel's middle: the table's step for (128 - xl) >> 4;+ ; u >> 8 (8.8) and v >> 4 (4.12) for the span, kept to the textureld a, 128sub a, (ix+W_X)rrca@@ -750,9 +740,12 @@ _rs_poly:ld de, (hl)ld hl, (ix+W_U)add hl, de- call clamp20ld (m_t), hl+ ld a, (m_t+2)+ cp a, 16+ jr nc, .Lpo_ucld hl, (m_t+1)+.Lpo_us:ld (r_u), hlpop hlld de, r_kv@@ -760,11 +753,31 @@ _rs_poly:ld de, (hl)ld hl, (ix+W_V)add hl, de- call clamp20- call x16+ ld (m_t), hl+ ld a, (m_t+2)+ cp a, 16+ jr nc, .Lpo_vc+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hlld (m_t), hlld hl, (m_t+1)+.Lpo_vs:ld (r_v), hl+ jr .Lpo_span+.Lpo_uc:+ rla ; (past the texture: below its start, or past its end)+ ld hl, 0x0FFF+ jr nc, .Lpo_us+ ld hl, 0+ jr .Lpo_us+.Lpo_vc:+ rla+ ld hl, 0xFFFF+ jr nc, .Lpo_vs+ ld hl, 0+ jr .Lpo_vs.Lpo_span:ld a, (r_px0)ld de, 0@@ -787,8 +800,14 @@ _rs_poly:ld ix, (w_l)call wstepld ix, (w_r)+ ld a, (r_lock)+ or a, a+ jr z, .Lpo_rboth+ call wstep_x ; (locked: the right edge's u and v no longer needed)+ jp .Lpo_loop+.Lpo_rboth:call wstep- jp nz, .Lpo_loop+ jp .Lpo_loop.Lpo_done:pop ixret@@ -815,6 +834,19 @@ wstep:pop hl ; (none left: the polygon is done)jp .Lpo_done+; the same, x alone+wstep_x:+ ld hl, (ix+W_X)+ ld de, (ix+W_SX)+ add hl, de+ ld (ix+W_X), hl+ dec (ix+W_ROWS)+ ret nz+ call edge+ ret nz+ pop hl+ jp .Lpo_done+; hl into 0..0xFFFFFclamp20:ld (m_t), hl@@ -1695,6 +1727,135 @@ _rs_quad:pop ixret+; ------------------------------------------------------------------------------------------------- rs_code+; int rs_code(int x, int y, int z)+; bits 0-4: the planes a polygon through (x, y, z) is cut by (z < NEAR, x > 4z, -x > 4z, y > 4z, -y > 4z); bits+; 8-12: the view's sides it is past, a little wider than the screen's (z < NEAR, 16x > 15z, -16x > 15z, 4y > 3z,+; -4y > 3z)+ .section .text._rs_code+ .global _rs_code+ .type _rs_code, @function+ .equ NEAR, 819+_rs_code:+ ld iy, 0+ add iy, sp+ ld bc, 0+ ld hl, (iy+9)+ ld de, NEAR+ or a, a+ sbc hl, de+ jp p, .Lrc_front+ ld bc, 0x0101+.Lrc_front:+ ld hl, (iy+9)+ add hl, hl+ add hl, hl+ ld (rc_t), hl ; 4z+ ld de, (iy+3)+ or a, a+ sbc hl, de+ jp p, .Lrc1+ set 1, c+.Lrc1:+ ld hl, (rc_t)+ or a, a+ adc hl, de+ jp p, .Lrc2+ set 2, c+.Lrc2:+ ld hl, (rc_t)+ ld de, (iy+6)+ or a, a+ sbc hl, de+ jp p, .Lrc3+ set 3, c+.Lrc3:+ ld hl, (rc_t)+ or a, a+ adc hl, de+ jp p, .Lrc4+ set 4, c+.Lrc4:+ ld hl, (iy+9)+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hl+ ld de, (iy+9)+ or a, a+ sbc hl, de+ ld (rc_t), hl ; 15z+ ld hl, (iy+3)+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hl+ ex de, hl ; 16x+ ld hl, (rc_t)+ or a, a+ sbc hl, de+ jp p, .Lrc5+ set 1, b+.Lrc5:+ ld hl, (rc_t)+ or a, a+ adc hl, de+ jp p, .Lrc6+ set 2, b+.Lrc6:+ ld hl, (iy+9)+ push hl+ pop de+ add hl, hl+ add hl, de+ ld (rc_t), hl ; 3z+ ld hl, (iy+6)+ add hl, hl+ add hl, hl+ ex de, hl ; 4y+ ld hl, (rc_t)+ or a, a+ sbc hl, de+ jp p, .Lrc7+ set 3, b+.Lrc7:+ ld hl, (rc_t)+ or a, a+ adc hl, de+ jp p, .Lrc8+ set 4, b+.Lrc8:+ or a, a+ sbc hl, hl+ ld h, b+ ld l, c+ ret++; ------------------------------------------------------------------------------------------------- rs_compose+; void rs_compose(uint8_t *dst, const uint8_t *a, const uint8_t *f): dst[i] = f[a[i]], all three 256-byte aligned+ .section .text._rs_compose+ .global _rs_compose+ .type _rs_compose, @function+_rs_compose:+ ld iy, 0+ add iy, sp+ ld de, (iy+3)+ ld hl, (iy+6)+ ld bc, (iy+9)+.Lcm_loop:+ ld c, (hl)+ ld a, (bc)+ ld (de), a+ inc l+ inc e+ ld c, (hl)+ ld a, (bc)+ ld (de), a+ inc l+ inc e+ jr nz, .Lcm_loop+ ret+; ------------------------------------------------------------------------------------------------- data.section .data._rs_varsr_tex: .d24 0@@ -1750,6 +1911,7 @@ h_c: .d24 0h_y0: .db 0h_rows: .db 0h_w: .db 0+rc_t: .d24 0c_h2: .d24 0c_Y0: .d24 0c_Z0: .d24 0diff --git a/games/numblocks/ce/src/world.c b/games/numblocks/ce/src/world.cindex 8bb222c0..0603e4f1 100644--- a/games/numblocks/ce/src/world.c+++ b/games/numblocks/ce/src/world.c@@ -96,8 +96,20 @@ static void edit_log(int x, int y, int z, int b) {}/* ---------------------------------------------------------------- light */-/* the window's edges: a step from a cell on one leaves the window */-static inline int wy_of(int slot) { return win_y + ((slot - win_y) & 31); }+/* Cell indices taken apart a byte at a time (on the eZ80 a mask or a shift of an int is a call): lo = (z & 7) << 5+ * | x, hi = y << 2 | z >> 3; and a column's cells a level at a time, 1024 apart, round the 32 slots. */+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 */+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;+}+/* 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); }/* the queues: in the 3D view's picture (light is never spread while it is drawn) */static uint16_t *lq; /* cells to spread light from */@@ -108,117 +120,154 @@ static uint8_t *dql;static unsigned dq_n;#define DQ_N 3000-static void lq_push(unsigned i) {+static void lq_push(uint16_t i) {unsigned t = lq_t + 1 == LQ_N ? 0 : lq_t + 1;if (t == lq_h) return; /* full: (never, at the window's size; it would only leave some light out) */- lq[lq_t] = (uint16_t)i;+ lq[lq_t] = i;lq_t = t;}-/* the six neighbours of cell i inside the window: their indices, -1 outside */-static int nbr[6];-static void neighbours(unsigned i) {- int x = i & 31, z = (i >> 5) & 31, y = i >> 10;- int ex = (win_x + 31) & 31, sx = win_x & 31, ez = (win_z + 31) & 31, sz = win_z & 31, ey = (win_y + 31) & 31,- sy = win_y & 31;- nbr[0] = y == sy ? -1 : (int)((i & 0x3FF) | ((unsigned)((y - 1) & 31) << 10));- nbr[1] = y == ey ? -1 : (int)((i & 0x3FF) | ((unsigned)((y + 1) & 31) << 10));- nbr[2] = z == sz ? -1 : (int)((i & ~0x3E0u) | ((unsigned)((z - 1) & 31) << 5));- nbr[3] = z == ez ? -1 : (int)((i & ~0x3E0u) | ((unsigned)((z + 1) & 31) << 5));- nbr[4] = x == sx ? -1 : (int)((i & ~31u) | ((unsigned)(x - 1) & 31));- nbr[5] = x == ex ? -1 : (int)((i & ~31u) | ((unsigned)(x + 1) & 31));+/* the neighbours of cell i inside the window (edges() first): nbr[0..nbr_n) */+static uint16_t nbr[6];+static uint8_t nbr_n;+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 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 (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 (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;+ nbr_n = n;}/* spreads both lights from the queued cells */static void light_spread(void) {+ edges();while (lq_h != lq_t) {- unsigned i = lq[lq_h];+ uint16_t i = lq[lq_h];lq_h = lq_h + 1 == LQ_N ? 0 : lq_h + 1;- int l = wlight[i], s = l & 15, b = l >> 4;+ uint8_t l = wlight[i], s = l & 15, b = l >> 4;if (s <= 1 && b <= 1) continue;neighbours(i);- for (int k = 0; k < 6; k++) {- int n = nbr[k];- if (n < 0) continue;- int op = blk_opacity[wblk[n]];+ 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;- int nl = wlight[n], ns = nl & 15, nb = nl >> 4, ch = 0;- if (s - op > ns) ns = s - op, ch = 1;- if (b - op > nb) nb = b - op, ch = 1;+ uint8_t nl = wlight[n], ns = nl & 15, nb = nl >> 4, 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 (ch) {wlight[n] = (uint8_t)(ns | nb << 4);- lq_push((unsigned)n);+ lq_push(n);}}}}-/* takes away the light that came through cell i (of channel shift sh: 0 sky, 4 block), from level lv; the cells+/* takes away the light that came through the queued cells (dql: the level they had, 16: block light); the cells* at its edge that keep their own light are queued to spread it back */static void light_darken(void) {+ edges();unsigned h = 0;while (h < dq_n) {- unsigned i = dq[h];- int lv = dql[h], sh = lv >> 4 ? 4 : 0;+ uint16_t i = dq[h];+ uint8_t lv = dql[h], blk = lv & 16;lv &= 15;h++;neighbours(i);- for (int k = 0; k < 6; k++) {- int n = nbr[k];- if (n < 0) continue;- int nl = (wlight[n] >> sh) & 15;+ for (uint8_t k = 0; k < nbr_n; k++) {+ uint16_t n = nbr[k];+ uint8_t w = wlight[n], nl = blk ? w >> 4 : w & 15;if (!nl) continue;- if (nl < lv && !(sh && blk_light[wblk[n]] >= nl) && !(!sh && wtop[n & 0x3FF] <= wy_of(n >> 10))) {- wlight[n] &= (uint8_t)~(15 << sh);- if (dq_n < DQ_N) dq[dq_n] = (uint16_t)n, dql[dq_n] = (uint8_t)(nl | (sh ? 16 : 0)), dq_n++;- } else lq_push((unsigned)n);+ bool keeps;+ if (blk) keeps = blk_light[wblk[n]] >= nl;+ else {+ /* (sky light straight down: kept) */+ static U16 u;+ u.w = n;+ uint8_t slot = u.b[1] >> 2;+ u.b[1] &= 3;+ keeps = wtop[u.w] <= wy_of(slot);+ }+ if (nl < lv && !keeps) {+ wlight[n] = blk ? w & 15 : w & 0xF0;+ if (dq_n < DQ_N) dq[dq_n] = n, dql[dq_n] = (uint8_t)(nl | blk), dq_n++;+ } else lq_push(n);}}dq_n = 0;}-static void dark_push(unsigned i, int sh) {- int lv = (wlight[i] >> sh) & 15;+static void dark_push(uint16_t i, bool blk) {+ uint8_t w = wlight[i], lv = blk ? w >> 4 : w & 15;if (!lv) return;- wlight[i] &= (uint8_t)~(15 << sh);- if (dq_n < DQ_N) dq[dq_n] = (uint16_t)i, dql[dq_n] = (uint8_t)(lv | (sh ? 16 : 0)), dq_n++;+ wlight[i] = blk ? w & 15 : w & 0xF0;+ if (dq_n < DQ_N) dq[dq_n] = i, dql[dq_n] = (uint8_t)(lv | (blk ? 16 : 0)), dq_n++;}/* the sky's straight-down light of a column: wtop is the first level under everything that stops it */static int column_top(int x, int z) {int c = CI(x, z);if (wcol[c].h >= win_y + WH) return win_y + WH;- for (int y = win_y + WH - 1; y >= win_y; y--)- if (blk_opacity[wblk[WI(x, y, z)]]) return y + 1;+ uint8_t ys = (uint8_t)(win_y + WH - 1) & 31;+ 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;+ }return win_y;}/* ---------------------------------------------------------------- faces to draw */-static inline bool hides(int b, int nb) {+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 bool exposed(int x, int y, int z) {- int b = wblk[WI(x, y, z)];- if (b == B_AIR) return false;- int m = blk_model[b];- if (m == M_CROSS || m == M_TORCH) return true;- /* (past the window: below it, as if solid; above, air) */- if (y + 1 >= win_y + WH ? true : !hides(b, wblk[WI(x, y + 1, z)])) return true;- if (y > win_y && !hides(b, wblk[WI(x, y - 1, z)])) return true;- if (z > win_z && !hides(b, wblk[WI(x, y, z - 1)])) return true;- if (z < win_z + WS - 1 && !hides(b, wblk[WI(x, y, z + 1)])) return true;- if (x > win_x && !hides(b, wblk[WI(x - 1, y, z)])) return true;- if (x < win_x + WS - 1 && !hides(b, wblk[WI(x + 1, y, z)])) 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 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;+ 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];+ }+ p = up;+ ys = (uint8_t)(ys + 1) & 31;+ }+ memcpy(&wexp[c], m, 4);}-static void expose(int x, int y, int z) {- if (!in_win(x, y, z)) return;- uint32_t bit = (uint32_t)1 << (y & 31);- if (exposed(x, y, z)) wexp[CI(x, z)] |= bit;- else wexp[CI(x, z)] &= ~bit;+/* the columns around a cell that changed */+static void expose_around(int x, int z) {+ expose_col(x, z);+ expose_col(x - 1, z);+ expose_col(x + 1, z);+ expose_col(x, z - 1);+ expose_col(x, z + 1);}/* ---------------------------------------------------------------- filling the window */@@ -232,6 +281,7 @@ static void put_cb(int x, int y, int z, int b) {/* 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;for (int z = z0; z < z1; z++)for (int x = x0; x < x1; x++) {int c = CI(x, z);@@ -240,11 +290,15 @@ 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);- for (int y = y0; y < y1; y++) wblk[WI(x, y, z)] = (uint8_t)gen_cell(x, y, z, 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;+ }int py = col->h + 1;- if (py >= y0 && py < y1 && wblk[WI(x, py, z)] == B_AIR && wblk[WI(x, py - 1, z)] == col->top) {- int p = gen_plant(x, z, col);- if (p) wblk[WI(x, py, z)] = (uint8_t)p;+ if (py >= y0 && py < y1 && p[YOFF[(uint8_t)py & 31]] == B_AIR && p[YOFF[(uint8_t)(py - 1) & 31]] == col->top) {+ int pl = gen_plant(x, z, col);+ if (pl) p[YOFF[(uint8_t)py & 31]] = (uint8_t)pl;}}gen_decorate(x0, z0, x1, z1, y0, y1, put_cb);@@ -261,13 +315,15 @@ 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(WI(x, y, z), 0);+ if (y >= win_y) dark_push((uint16_t)(YOFF[(uint8_t)y & 31] + c), false);wtop[c] = (uint8_t)top;+ uint8_t ys = ys0;for (int y = y0; y < y1; y++) {- unsigned i = WI(x, y, z);- int bl = blk_light[wblk[i]];+ 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;}}if (dq_n) light_darken();@@ -277,20 +333,23 @@ 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;+ /* (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)];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;- for (int y = ya; y < yb; y++) {- unsigned i = WI(x, y, z);- if (!(wlight[i] & 15) && !(wlight[i] >> 4)) continue;+ 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 && (wlight[i] & 15) == 15 && !(wlight[i] >> 4)) {- bool edge = y == top && y > win_y && blk_opacity[wblk[WI(x, y - 1, z)]] < 15;- if (!edge) {- int n;- edge = (x > win_x && (n = wtop[CI(x - 1, z)]) > y) || (x < win_x + WS - 1 && (n = wtop[CI(x + 1, z)]) > y) ||- (z > win_z && (n = wtop[CI(x, z - 1)]) > y) || (z < win_z + WS - 1 && (n = wtop[CI(x, z + 1)]) > y);- }+ 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);@@ -299,16 +358,13 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {}light_spread();for (int z = z0 - 1; z <= z1; z++)- for (int x = x0 - 1; x <= x1; x++) {- if ((unsigned)(x - win_x) >= WS || (unsigned)(z - win_z) >= WS) continue;- for (int y = y0 - 1; y <= y1; y++) expose(x, y, z);- }+ for (int x = x0 - 1; x <= x1; x++) expose_col(x, z);}/* ---------------------------------------------------------------- changes */void world_set(int x, int y, int z, int b) {if (!in_win(x, y, z)) return;- unsigned i = WI(x, y, z);+ uint16_t i = (uint16_t)WI(x, y, z);int old = wblk[i];if (old == b) return;edit_log(x, y, z, b);@@ -320,14 +376,14 @@ void world_set(int x, int y, int z, int b) {wtop[c] = (uint8_t)ntop;dq_n = 0;lq_h = lq_t = 0;- dark_push(i, 0);- dark_push(i, 4);+ dark_push(i, false);+ dark_push(i, true);if (ntop > top) /* the sky is stopped higher: the column below loses its straight-down light */- for (int k = ntop - 1; k >= top && k >= win_y; k--) dark_push(WI(x, k, z), 0);+ for (int k = ntop - 1; k >= top && k >= win_y; k--) dark_push((uint16_t)WI(x, k, z), false);light_darken();if (ntop < top)for (int k = ntop; k < top; k++) {- unsigned j = WI(x, k, z);+ uint16_t j = (uint16_t)WI(x, k, z);wlight[j] |= 15;lq_push(j);}@@ -337,17 +393,12 @@ void world_set(int x, int y, int z, int b) {}if (y >= ntop) wlight[i] |= 15;/* the neighbours bring their light back in */+ edges();neighbours(i);- for (int k = 0; k < 6; k++)- if (nbr[k] >= 0 && wlight[nbr[k]]) lq_push((unsigned)nbr[k]);+ for (uint8_t k = 0; k < nbr_n; k++)+ if (wlight[nbr[k]]) lq_push(nbr[k]);light_spread();- expose(x, y, z);- expose(x, y - 1, z);- expose(x, y + 1, z);- expose(x - 1, y, z);- expose(x + 1, y, z);- expose(x, y, z - 1);- expose(x, y, z + 1);+ expose_around(x, z);}/* ---------------------------------------------------------------- the window */@@ -407,18 +458,10 @@ void world_step(void) {/* up a level: the new top level, then the sky stops where it may now stop higher */win_y++;int y = win_y + WH - 1;- for (int z = win_z; z < win_z + WS; z++)- for (int x = win_x; x < win_x + WS; x++) wexp[CI(x, z)] &= ~((uint32_t)1 << (y & 31));fill(win_x, win_z, win_x + WS, win_z + WS, y, y + 1, false);- for (int z = win_z; z < win_z + WS; z++)- for (int x = win_x; x < win_x + WS; x++) expose(x, y - 1, z);} else if (win_y - want_y > 2) {win_y--;int y = win_y;- for (int z = win_z; z < win_z + WS; z++)- for (int x = win_x; x < win_x + WS; x++) wexp[CI(x, z)] &= ~((uint32_t)1 << (y & 31));fill(win_x, win_z, win_x + WS, win_z + WS, y, y + 1, false);- for (int z = win_z; z < win_z + WS; z++)- for (int x = win_x; x < win_x + WS; x++) expose(x, y + 1, z);}}