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: climbing out of water onto a bank
Mason Chen3335e7108921
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex c196c4cf..36570e9f 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -114,6 +114,7 @@ bool block_box(int x, int y, int z, float *b);int block_boxes(int b, int x, int y, int z, int8_t (*o)[6]); /* its shape, up to 5 boxes; 0: a whole cube */float phys_clip(const float *box, int axis, float d);bool phys_move(float *p, float *v, float w, float h);+bool phys_free(const float *a); /* nothing solid overlaps the box {x0 y0 z0 x1 y1 z1} *//* ---------------------------------------------------------------- the player (player.c) */typedef struct {diff --git a/games/numblocks/src/phys.c b/games/numblocks/src/phys.cindex 42c17361..12344fd5 100644--- a/games/numblocks/src/phys.c+++ b/games/numblocks/src/phys.c@@ -117,6 +117,21 @@ float phys_clip(const float *a, int axis, float d) {return d;}+/* nothing solid overlaps the box a (World.getCollidingBoundingBoxes is empty) */+bool phys_free(const float *a) {+ for (int y = ifloor(a[1]) - 1; y <= ifloor(a[4]); y++)+ for (int z = ifloor(a[2]); z <= ifloor(a[5]); z++)+ for (int x = ifloor(a[0]); x <= ifloor(a[3]); x++) {+ float bs[30];+ int nb = block_cboxes(x, y, z, bs);+ for (int q = 0; q < nb; q++) {+ const float *b = bs + q * 6;+ if (a[0] < b[3] && a[3] > b[0] && a[1] < b[4] && a[4] > b[1] && a[2] < b[5] && a[5] > b[2]) return false;+ }+ }+ return true;+}+/* moves a box (w wide, h high, feet at p) by v; stops on blocks, zeroes the* velocity along blocked axes; true if it landed on something */bool phys_move(float *p, float *v, float w, float h) {diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.cindex 785a20ea..961c82de 100644--- a/games/numblocks/src/player.c+++ b/games/numblocks/src/player.c@@ -86,6 +86,15 @@ static void move_flying(float strafe, float fwd, float f) {pl.vz += fwd * c + strafe * s;}+/* World.isAnyLiquid: a liquid in any block the box touches */+static bool liquid_in(const float *a) {+ for (int y = ifloor(a[1]); y <= ifloor(a[4]); y++)+ for (int z = ifloor(a[2]); z <= ifloor(a[5]); z++)+ for (int x = ifloor(a[0]); x <= ifloor(a[3]); x++)+ if (blk_model[world_get(x, y, z)] == M_LIQUID) return true;+ return false;+}+static bool in_liquid(void) {int b = world_get(ifloor(pl.x), ifloor(pl.y + 0.4f), ifloor(pl.z));return blk_model[b] == M_LIQUID;@@ -749,9 +758,18 @@ void player_tick(uint32_t keys, uint32_t pressed) {int at = world_get(ifloor(pl.x), ifloor(pl.y), ifloor(pl.z));if (pl.in_water) {move_flying(strafe, fwd, 0.02f);+ float bvx = pl.vx, bvz = pl.vz;move(pl.vx, pl.vy, pl.vz);+ bool wall = bvx != pl.vx || bvz != pl.vz;pl.vx *= 0.8f, pl.vy *= 0.8f, pl.vz *= 0.8f;pl.vy -= 0.02f;+ /* EntityLivingBase.moveEntityWithHeading: swimming into a wall with room above it+ * (nothing solid, no water) lifts you out, onto the bank */+ if (wall) {+ float a[6], dy = pl.vy + 0.6f - pl.y + y0;+ box_of(pl.x + pl.vx, pl.y + dy, pl.z + pl.vz, a);+ if (phys_free(a) && !liquid_in(a)) pl.vy = 0.3f;+ }} else {float fr = pl.on_ground ? 0.6f * 0.91f : 0.91f;float speed = 0.1f * (pl.sprinting ? 1.3f : 1.0f);diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.cindex 51079821..fe09c3a3 100644--- a/games/numblocks/tests/unit.c+++ b/games/numblocks/tests/unit.c@@ -238,6 +238,24 @@ int main(void) {alarm(0);plat_remove_prefix("ut6");}+ {+ /* out of a pool one block deep: swim into the bank holding jump (the 0.3 lift) */+ world_new(0, "ut7");+ world_follow(8, 110, 8);+ int g = 110;+ for (int z = 2; z <= 14; z++)+ for (int x = 2; x <= 14; x++)+ for (int y = g - 3; y < g + 4; y++)+ world_set(x, y, z, y >= g ? B_AIR : (x >= 7 && x <= 9 && z >= 7 && z <= 9 && y == g - 1) ? B_WATER : B_DIRT);+ memset(&pl, 0, sizeof pl);+ pl.x = 8.5f, pl.z = 8.5f, pl.y = (float)(g - 1), pl.health = 20, pl.food = 20, pl.mode = 0;+ for (int t = 0; t < 10; t++) player_tick(0, 0);+ CHECK(pl.in_water);+ int t = 0;+ for (; t < 60 && !(pl.on_ground && pl.y >= g); t++) player_tick(K_FWD | K_JUMP, t == 0 ? K_JUMP : 0);+ CHECK(pl.on_ground && pl.y >= g && pl.z > 10);+ plat_remove_prefix("ut7");+ }printf("%s (%d recipes)\n", fails ? "FAILED" : "all good", N_RECIPES);return fails != 0;}