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: place and break exactly where the crosshair points

Mason Chen02959f949f02
diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.c
index 323d656b..f0494e0b 100644
--- a/games/numblocks/src/main.c
+++ b/games/numblocks/src/main.c
@@ -20,7 +20,10 @@ static float px, py, pz;
static bool in_world; /* playing; else the title screens, the world turning behind */
static float title_yaw;
-static void camera_reset(void) { px = pl.x, py = pl.y, pz = pl.z; }
+static void camera_reset(void) {
+ px = pl.x, py = pl.y, pz = pl.z;
+ player_look(pl.x, pl.y + 1.62f, pl.z, pl.yaw, pl.pitch);
+}
/* the title's backdrop: the saved world where the player is, else a world of seed 0 */
static void title(void) {
@@ -183,10 +186,13 @@ bool game_frame(void) {
}
bool paused = gui == GUI_PAUSE || gui == GUI_OPTIONS || gui == GUI_CONTROLS;
if (!paused) acc += dt;
- uint32_t game_keys = in_game ? k : 0, game_pressed = in_game ? pressed : 0;
+ /* a key pressed on a frame between ticks waits for the next tick (else that press is lost) */
+ static uint32_t game_pressed;
+ uint32_t game_keys = in_game ? k : 0;
+ game_pressed = in_game ? game_pressed | pressed : 0;
while (acc >= 50) {
px = pl.x, py = pl.y, pz = pl.z;
- player_tick(game_keys, game_pressed);
+ player_tick(game_keys | game_pressed, game_pressed);
game_pressed = 0;
ents_tick();
world_tick();
@@ -218,6 +224,8 @@ bool game_frame(void) {
c.x += -cosf(yr) * side, c.z += -sinf(yr) * side, c.y += dip;
c.pitch += fabsf(cosf(w - 0.2f) * b) * 5;
}
+ /* what the crosshair is on: the ray through the middle of this picture */
+ if (!pl.dead) player_look(c.x, c.y, c.z, c.yaw, c.pitch);
if (pl.dead) c.y = pl.y + 0.3f;
if (pl.sleep_timer) c.y = pl.y + 0.3f;
render_frame(&c, game_time);
diff --git a/games/numblocks/src/mob.c b/games/numblocks/src/mob.c
index c02845f7..f00b4ab2 100644
--- a/games/numblocks/src/mob.c
+++ b/games/numblocks/src/mob.c
@@ -308,9 +308,7 @@ static void hurt(Entity *e, float dmg, float kx, float kz) {
/* ---------------------------------------------------------------- the player and mobs */
Entity *entity_looked_at(float reach, float block_t) {
- float ex = pl.x, ey = pl.y + (pl.sneaking ? 1.54f : 1.62f), ez = pl.z;
- float yaw = pl.yaw * 0.017453292f, pitch = pl.pitch * 0.017453292f;
- float d[3] = {-sinf(yaw) * cosf(pitch), -sinf(pitch), cosf(yaw) * cosf(pitch)};
+ float ex = look_ray[0], ey = look_ray[1], ez = look_ray[2], *d = look_ray + 3;
float best = reach < block_t ? reach : block_t;
Entity *hit = NULL;
for (int i = 0; i < N_ENT; i++) {
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.h
index 10651c6e..6fa494a8 100644
--- a/games/numblocks/src/nb.h
+++ b/games/numblocks/src/nb.h
@@ -120,6 +120,7 @@ float rndf(void);
/* ---------------------------------------------------------------- collisions (phys.c) */
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 */
+void select_box(int b, int x, int y, int z, float *o); /* its outline: x0 y0 z0 x1 y1 z1, in blocks */
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} */
@@ -151,6 +152,8 @@ typedef struct {
enum { EF_POISON, EF_HUNGER, EF_REGEN };
extern Player pl;
void player_spawn(void);
+extern float look_ray[6]; /* the eyes' ray as last drawn: from, direction */
+void player_look(float ex, float ey, float ez, float yaw, float pitch);
void player_tick(uint32_t keys, uint32_t pressed); /* 20 a second */
static inline Stack *held(void) { return &pl.inv[pl.slot]; }
void player_hurt(float amount, int kind); /* kind: DMG_* */
diff --git a/games/numblocks/src/phys.c b/games/numblocks/src/phys.c
index 12344fd5..8f22e06e 100644
--- a/games/numblocks/src/phys.c
+++ b/games/numblocks/src/phys.c
@@ -55,6 +55,32 @@ int block_boxes(int b, int x, int y, int z, int8_t (*o)[6]) {
return n;
}
+/* the selection box of block b (Block.getSelectedBoundingBox, from its corner): its shape's bounds;
+ * plants as BlockBush and its kin */
+void select_box(int b, int x, int y, int z, float *o) {
+ int8_t bx[5][6];
+ int n = block_boxes(b, x, y, z, bx);
+ float lo[3] = {0, 0, 0}, hi[3] = {16, 16, 16};
+ if (n) {
+ for (int i = 0; i < 3; i++) lo[i] = 16, hi[i] = 0;
+ for (int k = 0; k < n; k++)
+ for (int i = 0; i < 3; i++) {
+ if (bx[k][i] < lo[i]) lo[i] = bx[k][i];
+ if (bx[k][i + 3] > hi[i]) hi[i] = bx[k][i + 3];
+ }
+ } else if (blk_model[b] == M_CROSS) {
+ float r = 6.4f, h = 12.8f; /* tall grass, ferns, saplings, dead bushes: 0.4 around, 0.8 high */
+ if (b >= B_DANDELION && b <= B_OXEYE_DAISY) r = 3.2f, h = 9.6f; /* flowers */
+ else if (b == B_BROWN_MUSHROOM || b == B_RED_MUSHROOM) r = 3.2f, h = 6.4f; /* mushrooms */
+ else if (b == B_SUGAR_CANE) r = 6, h = 16;
+ else if ((b >= B_WHEAT_0 && b <= B_WHEAT_7) || (b >= B_CARROTS_0 && b <= B_CARROTS_3) ||
+ (b >= B_POTATOES_0 && b <= B_POTATOES_3))
+ r = 8, h = 4; /* crops */
+ lo[0] = lo[2] = 8 - r, hi[0] = hi[2] = 8 + r, hi[1] = h;
+ }
+ for (int i = 0; i < 3; i++) o[i] = lo[i] / 16, o[i + 3] = hi[i] / 16;
+}
+
/* the collision boxes of the block at (x, y, z), in world coordinates (b: up to 5 x 6) */
static int block_cboxes(int x, int y, int z, float *b) {
int s = world_get(x, y, z);
diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.c
index da5f18fa..53d6496e 100644
--- a/games/numblocks/src/player.c
+++ b/games/numblocks/src/player.c
@@ -325,11 +325,57 @@ static void set_block(int x, int y, int z, int b) {
}
/* ---------------------------------------------------------------- looking */
+/* the eyes' ray, as the last frame drew it (EntityRenderer.getMouseOver runs each frame, and the
+ * clicks of the next ticks use what it found): the crosshair is where it points */
+float look_ray[6];
+float hit_t; /* how far along it the block looked at is */
+
+/* the ray against a box: where it comes in, and through which face */
+static bool ray_box(const float *o, const float *d, const float *lo, const float *hi, float *t, int *face) {
+ float t0 = -1e9f, t1 = 1e9f;
+ int f = -1;
+ for (int a = 0; a < 3; a++) {
+ if (fabsf(d[a]) < 1e-7f) {
+ if (o[a] < lo[a] || o[a] > hi[a]) return false;
+ continue;
+ }
+ float ta = (lo[a] - o[a]) / d[a], tb = (hi[a] - o[a]) / d[a];
+ int fa = a == 0 ? 4 : a == 1 ? 0 : 2; /* (coming in through the low side) */
+ if (ta > tb) {
+ float s = ta;
+ ta = tb, tb = s, fa++;
+ }
+ if (ta > t0) t0 = ta, f = fa;
+ if (tb < t1) t1 = tb;
+ }
+ if (t0 > t1 || t1 < 0) return false;
+ *t = t0, *face = f;
+ return true;
+}
+
+/* where the ray meets block b at (x, y, z): its selection box (Block.collisionRayTrace), the steps
+ * of stairs one by one (BlockStairs); a ray passing beside a flower or over a slab goes on */
+static bool ray_block(int b, int x, int y, int z, float *t, int *face) {
+ float o[3] = {look_ray[0] - x, look_ray[1] - y, look_ray[2] - z}, s[6];
+ if (blk_model[b] == M_STAIRS) {
+ int8_t bx[5][6];
+ int n = block_boxes(b, x, y, z, bx);
+ bool hit = false;
+ for (int k = 0; k < n; k++) {
+ float lo[3] = {bx[k][0] / 16.0f, bx[k][1] / 16.0f, bx[k][2] / 16.0f}, hi[3] = {bx[k][3] / 16.0f, bx[k][4] / 16.0f, bx[k][5] / 16.0f};
+ float kt;
+ int kf;
+ if (ray_box(o, look_ray + 3, lo, hi, &kt, &kf) && (!hit || kt < *t)) *t = kt, *face = kf, hit = true;
+ }
+ return hit;
+ }
+ select_box(b, x, y, z, s);
+ return ray_box(o, look_ray + 3, s, s + 3, t, face);
+}
+
/* the first block the eyes' ray meets within reach (4.5, 5 in creative) */
static void pick(void) {
- float ex = pl.x, ey = pl.y + (pl.sneaking ? 1.54f : 1.62f), ez = pl.z;
- float yaw = pl.yaw * 0.017453292f, pitch = pl.pitch * 0.017453292f;
- float dx = -sinf(yaw) * cosf(pitch), dy = -sinf(pitch), dz = cosf(yaw) * cosf(pitch);
+ float ex = look_ray[0], ey = look_ray[1], ez = look_ray[2], dx = look_ray[3], dy = look_ray[4], dz = look_ray[5];
int x = ifloor(ex), y = ifloor(ey), z = ifloor(ez);
int sx = dx > 0 ? 1 : -1, sy = dy > 0 ? 1 : -1, sz = dz > 0 ? 1 : -1;
float idx = dx != 0 ? fabsf(1 / dx) : 1e9f, idy = dy != 0 ? fabsf(1 / dy) : 1e9f, idz = dz != 0 ? fabsf(1 / dz) : 1e9f;
@@ -341,8 +387,14 @@ static void pick(void) {
while (t <= reach) {
int b = world_get(x, y, z);
if (b != B_AIR && blk_model[b] != M_LIQUID) {
- pl.hit_x = x, pl.hit_y = y, pl.hit_z = z, pl.hit_face = face < 0 ? 1 : face;
- return;
+ float bt;
+ int bf;
+ if (ray_block(b, x, y, z, &bt, &bf)) {
+ if (bt > reach) return;
+ if (bt <= t) bt = t, bf = face; /* (the eyes inside it: the side they came in by) */
+ pl.hit_x = x, pl.hit_y = y, pl.hit_z = z, pl.hit_face = bf < 0 ? 1 : bf, hit_t = bt;
+ return;
+ }
}
if (tx < ty && tx < tz) t = tx, tx += idx, x += sx, face = sx > 0 ? 4 : 5;
else if (ty < tz) t = ty, ty += idy, y += sy, face = sy > 0 ? 0 : 1;
@@ -350,11 +402,16 @@ static void pick(void) {
}
}
+void player_look(float ex, float ey, float ez, float yaw, float pitch) {
+ float yr = yaw * 0.017453292f, pr = pitch * 0.017453292f;
+ look_ray[0] = ex, look_ray[1] = ey, look_ray[2] = ez;
+ look_ray[3] = -sinf(yr) * cosf(pr), look_ray[4] = -sinf(pr), look_ray[5] = cosf(yr) * cosf(pr);
+ pick();
+}
+
/* the liquid source the eyes look at (for buckets), or false */
static bool pick_liquid(int *ox, int *oy, int *oz) {
- float ex = pl.x, ey = pl.y + 1.62f, ez = pl.z;
- float yaw = pl.yaw * 0.017453292f, pitch = pl.pitch * 0.017453292f;
- float dx = -sinf(yaw) * cosf(pitch), dy = -sinf(pitch), dz = cosf(yaw) * cosf(pitch);
+ float ex = look_ray[0], ey = look_ray[1], ez = look_ray[2], dx = look_ray[3], dy = look_ray[4], dz = look_ray[5];
for (float t = 0; t < 4.5f; t += 0.1f) {
int x = ifloor(ex + dx * t), y = ifloor(ey + dy * t), z = ifloor(ez + dz * t);
int b = world_get(x, y, z);
@@ -821,12 +878,7 @@ void player_tick(uint32_t keys, uint32_t pressed) {
if (pl.dead) return;
pick();
/* a mob under the crosshair, nearer than the block: Back hits it, OK uses on it */
- float bt = 1e9f;
- if (pl.hit_face >= 0) {
- float bx = pl.hit_x + 0.5f - pl.x, by = pl.hit_y + 0.5f - (pl.y + 1.62f), bz = pl.hit_z + 0.5f - pl.z;
- bt = sqrtf(bx * bx + by * by + bz * bz) - 0.5f;
- }
- Entity *target = entity_looked_at(pl.mode ? 5.0f : 3.0f, bt);
+ Entity *target = entity_looked_at(pl.mode ? 5.0f : 3.0f, pl.hit_face >= 0 ? hit_t : 1e9f);
if (target) {
if (pressed & K_ATTACK) mob_attack(target), player_swing();
if ((pressed & K_USE) && mob_use(target)) pressed &= ~K_USE;
@@ -836,6 +888,9 @@ void player_tick(uint32_t keys, uint32_t pressed) {
/* mining (Back held) */
if (break_cooldown) break_cooldown--;
if ((pressed & K_ATTACK) && pl.hit_face < 0 && !target) player_swing();
+ /* PlayerControllerMP.onPlayerDamageBlock: looking at another block starts over */
+ static int bx_ = -1, by_, bz_;
+ if (pl.hit_x != bx_ || pl.hit_y != by_ || pl.hit_z != bz_) pl.breaking = 0, bx_ = pl.hit_x, by_ = pl.hit_y, bz_ = pl.hit_z;
if ((keys & K_ATTACK) && pl.hit_face >= 0 && !break_cooldown) {
player_swing();
int b = world_get(pl.hit_x, pl.hit_y, pl.hit_z);
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index 3527f7dd..373a550c 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -1736,31 +1736,6 @@ static float sel_pt[8][3]; /* the corners on the picture: x, y, 1 / depth (0:
static float sel_w[8][3]; /* and in the cache */
static bool sel_on;
-/* the selection box (sixteenths) of block b: its shape's bounds; plants as BlockBush and its kin */
-static void select_box(int b, int x, int y, int z, float *o) {
- int8_t bx[5][6];
- int n = block_boxes(b, x, y, z, bx);
- float lo[3] = {0, 0, 0}, hi[3] = {16, 16, 16};
- if (n) {
- for (int i = 0; i < 3; i++) lo[i] = 16, hi[i] = 0;
- for (int k = 0; k < n; k++)
- for (int i = 0; i < 3; i++) {
- if (bx[k][i] < lo[i]) lo[i] = bx[k][i];
- if (bx[k][i + 3] > hi[i]) hi[i] = bx[k][i + 3];
- }
- } else if (blk_model[b] == M_CROSS) {
- float r = 6.4f, h = 12.8f; /* tall grass, ferns, saplings, dead bushes: 0.4 around, 0.8 high */
- if (b >= B_DANDELION && b <= B_OXEYE_DAISY) r = 3.2f, h = 9.6f; /* flowers */
- else if (b == B_BROWN_MUSHROOM || b == B_RED_MUSHROOM) r = 3.2f, h = 6.4f; /* mushrooms */
- else if (b == B_SUGAR_CANE) r = 6, h = 16;
- else if ((b >= B_WHEAT_0 && b <= B_WHEAT_7) || (b >= B_CARROTS_0 && b <= B_CARROTS_3) ||
- (b >= B_POTATOES_0 && b <= B_POTATOES_3))
- r = 8, h = 4; /* crops */
- lo[0] = lo[2] = 8 - r, hi[0] = hi[2] = 8 + r, hi[1] = h;
- }
- for (int i = 0; i < 3; i++) o[i] = lo[i] / 16 - 0.002f, o[i + 3] = hi[i] / 16 + 0.002f;
-}
-
static void select_frame(void) {
sel_on = false;
if (pl.hit_face < 0 || gui != GUI_NONE || !world_loaded(pl.hit_x, pl.hit_y, pl.hit_z)) return;
@@ -1768,6 +1743,7 @@ static void select_frame(void) {
if (b == B_AIR || blk_model[b] == M_LIQUID) return;
float s[6];
select_box(b, pl.hit_x, pl.hit_y, pl.hit_z, s);
+ for (int i = 0; i < 3; i++) s[i] -= 0.002f, s[i + 3] += 0.002f;
float bx = (float)(pl.hit_x - vc_x0), by = (float)(pl.hit_y - vc_y0), bz = (float)(pl.hit_z - vc_z0);
for (int k = 0; k < 8; k++) {
float *w = sel_w[k], *q = sel_pt[k];
diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.c
index d7dc4001..c6f28bd5 100644
--- a/games/numblocks/tests/unit.c
+++ b/games/numblocks/tests/unit.c
@@ -1,5 +1,6 @@
/* Checks of the item rules: crafting, stacking, smelting, drops. */
#include <stdio.h>
+#include <math.h>
#include <unistd.h>
#include "nb.h"
#include "edits.h"
@@ -336,6 +337,31 @@ int main(void) {
CHECK(load_world(2) && pl.mode == 1 && world_seed == 6);
for (int s = 1; s <= MAX_WORLDS; s++) delete_world(s);
}
+ {
+ /* the block looked at: a ray over a slab or beside a flower goes on to what is behind */
+ extern float hit_t;
+ world_new(5, "ut10");
+ world_follow(8, 100, 8);
+ while (world_pending()) world_follow(8, 100, 8);
+ for (int z = 4; z < 16; z++)
+ for (int x = 4; x < 12; x++)
+ for (int y = 99; y < 104; y++) world_set(x, y, z, y == 99 ? B_STONE : B_AIR);
+ world_set(8, 100, 12, B_STONE);
+ world_set(8, 100, 10, B_STONE_SLAB);
+ pl.mode = 0;
+ player_look(8.5f, 100.75f, 8.5f, 0, 0);
+ CHECK(pl.hit_face == 2 && pl.hit_x == 8 && pl.hit_y == 100 && pl.hit_z == 12);
+ player_look(8.5f, 100.25f, 8.5f, 0, 0);
+ CHECK(pl.hit_face == 2 && pl.hit_z == 10 && fabsf(hit_t - 1.5f) < 0.01f);
+ player_look(8.5f, 102, 10.5f, 0, 90);
+ CHECK(pl.hit_face == 1 && pl.hit_y == 100 && pl.hit_z == 10 && fabsf(hit_t - 1.5f) < 0.01f);
+ world_set(8, 100, 10, B_POPPY);
+ player_look(8.9f, 100.25f, 8.5f, 0, 0);
+ CHECK(pl.hit_z == 12);
+ player_look(8.5f, 100.25f, 8.5f, 0, 0);
+ CHECK(pl.hit_face == 2 && pl.hit_z == 10);
+ plat_remove_prefix("ut10");
+ }
printf("%s (%d recipes)\n", fails ? "FAILED" : "all good", N_RECIPES);
return fails != 0;
}