Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumBlocks.nwa: Nether and End mobs, ender crystals, the Ender Dragon
Mason363f6b845c148a6
diff --git a/games/numblocks/Makefile b/games/numblocks/Makefileindex 5cf2be0e..779bdb9c 100644--- a/games/numblocks/Makefile+++ b/games/numblocks/Makefile@@ -15,7 +15,7 @@ GEN ?= src/gen.cGAME = src/main.c src/world.c src/edits.c src/render.c src/player.c src/phys.c src/inv.c src/entity.c src/mob.c src/tick.c src/gui.c src/hand.c src/save.c src/data.c src/names.c src/command.c $(GEN)HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h ../common/np_text.h Makefile# NumBlocks.nwa's sources: the game's, its tables with the Nether and the End, and theirs-DIMS_SRC = src/dims.c+DIMS_SRC = src/dims.c src/end.cGAME_DIMS = $(filter-out src/data.c,$(GAME)) src/data_dims.c $(DIMS_SRC)CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device)diff --git a/games/numblocks/src/dims.c b/games/numblocks/src/dims.cindex 601ee635..827aea36 100644--- a/games/numblocks/src/dims.c+++ b/games/numblocks/src/dims.c@@ -255,11 +255,18 @@ static void travel(int to, float frac, int ax) {/* ---------------------------------------------------------------- the player in portals */static int16_t portal_count, portal_cool;float portal_time; /* EntityPlayerSP.timeInPortal: the overlay */+int wither_ticks;static bool in_portal;/* Entity.onEntityUpdate's portal part, Entity.setPortal (a portal block in the player's cells: their* whole cells, as doBlockCollisions has it), BlockEndPortal.onEntityCollidedWithBlock */void dims_player_tick(void) {+ /* Potion.wither: 1 every 40 ticks (it kills) */+ if (wither_ticks > 0) {+ if (wither_ticks % 40 == 0) player_hurt(1, DMG_MAGIC);+ wither_ticks--;+ }+ end_tick();bool nether = false, end = false;int px = 0, pz = 0, pax = 0;for (int y = ifl(pl.y + 0.001f); y <= ifl(pl.y + 1.799f); y++)@@ -331,8 +338,6 @@ void dims_respawn(void) {memset(ents, 0, sizeof ents);}-/* the dragon fight (dragon.c) and the credits come later */-void dragon_enter(void) {}void dims_credits(void) {loading_screen();if (!world_dim(DIM_OVERWORLD)) return;diff --git a/games/numblocks/src/dims.h b/games/numblocks/src/dims.hindex 5113913e..0f538ad5 100644--- a/games/numblocks/src/dims.h+++ b/games/numblocks/src/dims.h@@ -16,8 +16,12 @@ extern const uint16_t gen_code_block[GC_N];/* gen.c, past the Overworld (gen_dims.h): the generator of each dimension */void gen_dim(int d); /* which one gen_slab, gen_biome and gen_top run (after gen_init) */-int gen_fortress_at(int x, int y, int z);-int gen_fortress_find(int x, int z, int *out); /* (tests) the nearest fortress: x, y, z, pieces */ /* 2: in a piece of a fortress, 1: in its box, 0: neither */+int gen_fortress_at(int x, int y, int z); /* 2: in a piece of a fortress, 1: in its box, 0: neither */+int gen_fortress_find(int x, int z, int *out); /* (tests) the nearest fortress: x, y, z, pieces */++typedef struct { int16_t x, z; uint8_t y, h, r, pad; } EndSpike; /* a pillar: its foot, height, radius */+const EndSpike *gen_spikes(int *n); /* the End's pillars */+int gen_end_block(int x, int y, int z); /* the End's block there, as made *//* dims.c: what is kept of the dimensions with the world (save.c) */typedef struct {@@ -28,12 +32,21 @@ typedef struct {} DimsState;extern DimsState dst;extern float portal_time; /* 0..1: the portal's swirl over the picture */+extern int wither_ticks; /* the wither effect on the player (a wither skeleton's hit) */+bool dims_punch(void); /* (mob.c) the player's swing sends a fireball back */bool dims_portal_light(int x, int y, int z); /* fire there lights a portal? (then it is made) */int dims_can_stay(int b, int x, int y, int z); /* -1: not one of the dimensions' blocks */void dims_player_tick(void); /* portals */void dims_respawn(void); /* (before the player respawns) */void player_to_spawn(void);void dims_go(int to); /* (tests) there, as through a portal */-void dragon_enter(void);+void dragon_enter(void); /* (end.c) */+void end_tick(void);+float dragon_bar(void);+bool dragon_beam(float *from, float *to);+void dragon_offsets(int i, float pt, float *yaw, float *y);+float dragon_anim(float pt);+void throw_eye(int tx, int ty, int tz); /* (mob.c) an eye of ender towards there */+enum { SKIN_CRYSTAL = 16, SKIN_DRAGON = 17 }; /* (the big skins: data.h's skin_crystal_px, skin_dragon_px) */void dims_credits(void);#endifdiff --git a/games/numblocks/src/end.c b/games/numblocks/src/end.cnew file mode 100644index 00000000..e2086b03--- /dev/null+++ b/games/numblocks/src/end.c@@ -0,0 +1,332 @@+/* NumBlocks.nwa only: the End's fight, as Minecraft 1.8.8 has it. The Ender Dragon (EntityDragon) flies+ * about the island and at the player, heals from the ender crystals on the pillars, knocks the player back+ * with its wings and bites; killed, it rises turning for ten seconds, gives its experience and leaves the+ * exit portal with the egg on its pillar of bedrock. */+#include <math.h>+#include "nb.h"+#if NB_DIMS+#include "edits.h"++/* the dragon's own state (its place, health and turn are its entity's) */+static struct {+ float yaw; /* rotationYaw (the entity's yaw is the model's: this one + 180) */+ float tx, ty, tz; /* where it flies to */+ float yaw_v; /* randomYawVelocity */+ float anim, prev_anim; /* the wings' beat */+ int16_t death; /* deathTicks */+ int8_t heal; /* the pillar whose crystal heals it, -1 none */+ uint8_t to_player, slowed, force_new;+ uint8_t ring_i;+ int16_t ring[32][2]; /* its last turns (1/100 degree) and heights (1/16): the neck and tail follow them */+} dg;++static inline int ifl(float v) { return (int)floorf(v); }+static float wrap(float a) {+ while (a >= 180) a -= 360;+ while (a < -180) a += 360;+ return a;+}+static float gauss(void) {+ float u = rndf() + 1e-6f, v = rndf();+ return sqrtf(-2 * logf(u)) * cosf(6.2831853f * v);+}+static Entity *dragon(void) {+ for (int i = 0; i < N_ENT; i++)+ if (ents[i].type == E_DRAGON) return &ents[i];+ return NULL;+}+static bool crystal_alive(int i) { return i < 64 && !(dst.crystals_gone[i >> 5] >> (i & 31) & 1); }++/* a block set where it may be out of the cache (its change kept all the same) */+static void put(int x, int y, int z, int b) {+ if (y < 0 || y >= WORLD_H) return;+ if (world_loaded(x, y, z)) world_set(x, y, z, b);+ else if (b != gen_end_block(x, y, z) || edits_changed(x, y, z)) edits_put(x, y, z, b);+}++/* getMovementOffsets(i): its turn and height i ticks ago, eased by the frame's part of the tick */+void dragon_offsets(int i, float pt, float *yaw, float *y) {+ Entity *e = dragon();+ if (e && e->health <= 0) pt = 0;+ pt = 1 - pt;+ int j = (dg.ring_i - i) & 31, k = (dg.ring_i - i - 1) & 31;+ float y0 = dg.ring[j][0] / 100.0f, y1 = dg.ring[k][0] / 100.0f;+ *yaw = y0 + wrap(y1 - y0) * pt;+ float h0 = dg.ring[j][1] / 16.0f, h1 = dg.ring[k][1] / 16.0f;+ *y = h0 + (h1 - h0) * pt;+}+float dragon_anim(float pt) { return dg.prev_anim + (dg.anim - dg.prev_anim) * pt; }+float dragon_dying(void) { return dg.death; }++/* setNewTarget: half the time the player, else somewhere above the island more than 10 away */+static void new_target(Entity *e) {+ dg.force_new = 0;+ if (rnd(2) == 0 && !pl.dead && pl.mode != 3) {+ dg.to_player = 1;+ return;+ }+ float d;+ do {+ dg.tx = 0, dg.ty = 70 + rndf() * 50, dg.tz = 0;+ dg.tx += rndf() * 120 - 60;+ dg.tz += rndf() * 120 - 60;+ float a = e->x - dg.tx, b = e->y - dg.ty, c = e->z - dg.tz;+ d = a * a + b * b + c * c;+ } while (d <= 100);+ dg.to_player = 0;+}++/* destroyBlocksInAABB: what it flies through goes, but obsidian, end stone and bedrock; true if any of+ * those (it slows) */+static bool through(float x0, float y0, float z0, float x1, float y1, float z1) {+ bool hard = false;+ for (int y = ifl(y0); y <= ifl(y1); y++)+ for (int z = ifl(z0); z <= ifl(z1); z++)+ for (int x = ifl(x0); x <= ifl(x1); x++) {+ if (!world_loaded(x, y, z)) continue;+ int b = world_get(x, y, z);+ if (b == B_AIR) continue;+ if (b == B_OBSIDIAN || b == B_END_STONE || b == B_BEDROCK || !rule(GR_MOB_GRIEFING)) hard = true;+ else {+ if (b == B_CHEST || b == B_FURNACE || b == B_FURNACE_LIT) tiles_removed(x, y, z);+ world_set(x, y, z, B_AIR);+ }+ }+ return hard;+}++/* EntityDragon.generatePortal: the exit portal (bedrock round it and under it), the pillar with its+ * four torches and the egg */+static void exit_portal(int px, int py, int pz) {+ for (int j = -1; j <= 32; j++)+ for (int k = -4; k <= 4; k++)+ for (int l = -4; l <= 4; l++) {+ int d2 = k * k + l * l;+ if (d2 > 12) continue; /* (12.25) */+ int x = px + k, y = py + j, z = pz + l;+ if (j < 0) {+ if (d2 <= 6) put(x, y, z, B_BEDROCK);+ } else if (j > 0)+ put(x, y, z, B_AIR);+ else+ put(x, y, z, d2 > 6 ? B_BEDROCK : B_END_PORTAL);+ }+ put(px, py, pz, B_BEDROCK);+ put(px, py + 1, pz, B_BEDROCK);+ put(px, py + 2, pz, B_BEDROCK);+ put(px - 1, py + 2, pz, B_TORCH_E);+ put(px + 1, py + 2, pz, B_TORCH_W);+ put(px, py + 2, pz - 1, B_TORCH_S);+ put(px, py + 2, pz + 1, B_TORCH_N);+ put(px, py + 3, pz, B_BEDROCK);+ put(px, py + 4, pz, B_DRAGON_EGG);+}++/* onDeathUpdate: ten seconds rising and turning, its light breaking out; experience from 7.5 s on, the+ * portal at the end */+static void dying(Entity *e) {+ dg.death++;+ if (dg.death >= 180 && dg.death <= 200)+ for (int k = 0; k < 3; k++)+ particle_add(e->x + (rndf() - 0.5f) * 8, e->y + 2 + (rndf() - 0.5f) * 4, e->z + (rndf() - 0.5f) * 8, 0, 0.05f, 0,+ 0xFFFF, 20);+ else+ for (int k = 0; k < 2; k++) {+ /* (RenderDragon's beams: light thrown out of it) */+ float a = rndf() * 6.2831853f, b = (rndf() - 0.5f) * 3.1415927f, s = 0.4f + rndf() * 0.6f;+ particle_add(e->x, e->y + 2, e->z, cosf(a) * cosf(b) * s, sinf(b) * s, sinf(a) * cosf(b) * s,+ rnd(2) ? 0xFFFF : 0xF81F, 30);+ }+ if (dg.death > 150 && dg.death % 5 == 0 && rule(GR_MOB_LOOT)) player_add_xp(1000);+ e->y += 0.1f;+ dg.yaw += 20;+ e->yaw = dg.yaw + 180;+ if (dg.death == 200) {+ if (rule(GR_MOB_LOOT)) player_add_xp(2000);+ exit_portal(ifl(e->x), 64, ifl(e->z));+ dst.dragon_dead = 1, dst.dragon_hp = 0;+ e->type = E_NONE;+ }+}++/* EntityDragon.onLivingUpdate */+void dragon_tick(Entity *e) {+ if (e->hurt > 0) e->hurt--;+ if (e->invuln > 0) e->invuln--;+ dg.prev_anim = dg.anim;+ if (dg.death) {+ dying(e);+ return;+ }+ /* updateDragonEnderCrystal: the nearest crystal within 32 heals it 1 every 10 ticks */+ int n;+ const EndSpike *s = gen_spikes(&n);+ if (dg.heal >= 0 && e->age % 10 == 0 && e->health < 200) e->health++;+ if (rnd(10) == 0) {+ float best = 1e30f;+ dg.heal = -1;+ for (int i = 0; i < n; i++) {+ if (!crystal_alive(i)) continue;+ float cx = s[i].x + 0.5f, cy = s[i].y + s[i].h + 1.0f, cz = s[i].z + 0.5f;+ if (fabsf(cx - e->x) > 40 || fabsf(cz - e->z) > 40 || cy < e->y - 32 || cy > e->y + 40) continue;+ float d = (cx - e->x) * (cx - e->x) + (cy - e->y) * (cy - e->y) + (cz - e->z) * (cz - e->z);+ if (d < best) best = d, dg.heal = (int8_t)i;+ }+ }+ float sp = sqrtf(e->vx * e->vx + e->vz * e->vz);+ float f10 = 0.2f / (sp * 10 + 1) * powf(2, e->vy);+ dg.anim += dg.slowed ? f10 * 0.5f : f10;+ dg.yaw = wrap(dg.yaw);+ if (++dg.ring_i == 32) dg.ring_i = 0;+ dg.ring[dg.ring_i][0] = (int16_t)(dg.yaw * 100), dg.ring[dg.ring_i][1] = (int16_t)(e->y * 16);+ float d10 = dg.tx - e->x, d0 = dg.ty - e->y, d1 = dg.tz - e->z, d2 = d10 * d10 + d0 * d0 + d1 * d1;+ if (dg.to_player) {+ dg.tx = pl.x, dg.tz = pl.z;+ float a = dg.tx - e->x, b = dg.tz - e->z, d8 = 0.4f + sqrtf(a * a + b * b) / 80 - 1;+ dg.ty = pl.y + (d8 > 10 ? 10 : d8);+ if (pl.dead || dim != DIM_END) dg.to_player = 0;+ } else+ dg.tx += gauss() * 2, dg.tz += gauss() * 2;+ if (dg.force_new || d2 < 100 || d2 > 22500) new_target(e);+ float hd = sqrtf(d10 * d10 + d1 * d1);+ d0 = hd > 0 ? d0 / hd : 0;+ d0 = d0 > 0.6f ? 0.6f : d0 < -0.6f ? -0.6f : d0;+ e->vy += d0 * 0.1f;+ float d4 = 180 - atan2f(d10, d1) * 57.29578f, d6 = wrap(d4 - dg.yaw);+ d6 = d6 > 50 ? 50 : d6 < -50 ? -50 : d6;+ float tx = dg.tx - e->x, ty = dg.ty - e->y, tz = dg.tz - e->z, tl = sqrtf(tx * tx + ty * ty + tz * tz);+ if (tl > 0) tx /= tl, ty /= tl, tz /= tl;+ float yr = dg.yaw * 0.017453292f, lx = sinf(yr), ly = e->vy, lz = -cosf(yr), ll = sqrtf(lx * lx + ly * ly + lz * lz);+ lx /= ll, ly /= ll, lz /= ll;+ float f5 = ((lx * tx + ly * ty + lz * tz) + 0.5f) / 1.5f;+ if (f5 < 0) f5 = 0;+ dg.yaw_v *= 0.8f;+ float f6 = sp + 1, d11 = sp + 1;+ if (d11 > 40) d11 = 40;+ dg.yaw_v += d6 * (0.7f / d11 / f6);+ dg.yaw += dg.yaw_v * 0.1f;+ float f7 = 2 / (d11 + 1), fr = 0.06f * (f5 * f7 + (1 - f7));+ /* moveFlying(0, -1): head first */+ yr = dg.yaw * 0.017453292f;+ e->vx += fr * sinf(yr), e->vz -= fr * cosf(yr);+ float k = dg.slowed ? 0.8f : 1;+ e->x += e->vx * k, e->y += e->vy * k, e->z += e->vz * k; /* (noClip) */+ float mx = e->vx, my = e->vy, mz = e->vz, ml = sqrtf(mx * mx + my * my + mz * mz);+ if (ml > 0) mx /= ml, my /= ml, mz /= ml;+ float f9 = 0.8f + 0.15f * (((mx * lx + my * ly + mz * lz) + 1) / 2);+ e->vx *= f9, e->vz *= f9, e->vy *= 0.91f;+ e->yaw = dg.yaw + 180;+ /* its parts: the body, the wings out at 4.5, the head 5.5 ahead (as the neck lifts it) */+ float o5y, o10y, o0y, oy;+ dragon_offsets(5, 1, &oy, &o5y);+ dragon_offsets(10, 1, &oy, &o10y);+ dragon_offsets(0, 1, &oy, &o0y);+ float f1 = (o5y - o10y) * 10 / 180 * 3.1415927f, f2 = cosf(f1), f15 = -sinf(f1);+ float f16 = sinf(yr), f4 = cosf(yr);+ float bx = e->x + f16 * 0.5f, bz = e->z - f4 * 0.5f;+ float f17 = sinf(yr - dg.yaw_v * 0.01f), f18 = cosf(yr - dg.yaw_v * 0.01f);+ float hx = e->x + f17 * 5.5f * f2, hy = e->y + (o0y - o5y) + f15 * 5.5f, hz = e->z - f18 * 5.5f * f2;+ if (e->hurt == 0 && !pl.dead && dim == DIM_END) {+ /* the wings knock the player away; the head bites (10) */+ for (int w = -1; w <= 1; w += 2) {+ float wx = e->x + w * f4 * 4.5f, wy = e->y + 2, wz = e->z + w * f16 * 4.5f;+ if (fabsf(pl.x - wx) < 2 + 4 + 0.3f && fabsf(pl.z - wz) < 2 + 4 + 0.3f && pl.y + 1.8f > wy - 2 && pl.y < wy) {+ float a = pl.x - bx, b = pl.z - bz, d = a * a + b * b;+ if (d < 0.01f) d = 0.01f;+ pl.vx += a / d * 4, pl.vy += 0.2f, pl.vz += b / d * 4;+ }+ }+ if (fabsf(pl.x - hx) < 1.5f + 1 + 0.3f && fabsf(pl.z - hz) < 1.5f + 1 + 0.3f && pl.y + 1.8f > hy - 1 && pl.y < hy + 4)+ player_hurt(opt.difficulty == 0 ? 0 : opt.difficulty == 1 ? 6 : opt.difficulty == 3 ? 15 : 10, DMG_MOB);+ }+ dg.slowed = through(hx - 1.5f, hy, hz - 1.5f, hx + 1.5f, hy + 3, hz + 1.5f) |+ through(bx - 2.5f, e->y, bz - 2.5f, bx + 2.5f, e->y + 3, bz + 2.5f);+ dst.dragon_hp = e->health;+}++/* attackEntityFromPart: the head takes it all, the rest a quarter and 1; it turns away; dead, it dies */+void dragon_hurt(Entity *e, float dmg) {+ if (dg.death || e->invuln > 10) return;+ /* the head: if the player's look passes near it */+ float yr = dg.yaw * 0.017453292f, hx = e->x + sinf(yr) * 5.5f, hy = e->y + 1.5f, hz = e->z - cosf(yr) * 5.5f;+ float vx = hx - look_ray[0], vy = hy - look_ray[1], vz = hz - look_ray[2], *d = look_ray + 3;+ float dl = sqrtf(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]), t = (vx * d[0] + vy * d[1] + vz * d[2]) / (dl * dl);+ float cx = vx - d[0] * t, cy = vy - d[1] * t, cz = vz - d[2] * t;+ bool head = cx * cx + cy * cy + cz * cz < 2.5f * 2.5f;+ if (!head) dmg = dmg / 4 + 1;+ dg.tx = e->x + sinf(yr) * 5 + (rndf() - 0.5f) * 2;+ dg.ty = e->y + rndf() * 3 + 1;+ dg.tz = e->z - cosf(yr) * 5 + (rndf() - 0.5f) * 2;+ dg.to_player = 0;+ e->health = (int16_t)(e->health - (int)ceilf(dmg));+ e->invuln = 20, e->hurt = 10;+ if (e->health <= 0) e->health = 0, dg.death = 1;+}++/* an ender crystal hit: it blows up (power 6); the dragon it was healing takes 10 */+void crystal_gone(Entity *e) {+ int i = e->tx;+ if (i >= 0 && i < 64) dst.crystals_gone[i >> 5] |= 1u << (i & 31);+ float x = e->x, y = e->y, z = e->z;+ e->type = E_NONE;+ explode(x, y, z, 6, true);+ Entity *d = dragon();+ if (d && dg.heal == i) {+ dg.heal = -1;+ if (!dg.death && d->invuln <= 10) {+ d->health = (int16_t)(d->health - 10), d->invuln = 20, d->hurt = 10;+ if (d->health <= 0) d->health = 0, dg.death = 1;+ }+ }+}++/* entering the End: the dragon, if it lives (it is met at (0, 128, 0), as it was made) */+void dragon_enter(void) {+ if (dst.dragon_dead) return;+ Entity *e = ent_new(E_DRAGON, 0.5f, 128, 0.5f);+ if (!e) return;+ memset(&dg, 0, sizeof dg);+ dg.heal = -1;+ dg.yaw = rndf() * 360;+ e->yaw = dg.yaw + 180;+ e->health = (int16_t)(dst.dragon_hp > 0 ? dst.dragon_hp : 200);+ for (int i = 0; i < 32; i++) dg.ring[i][0] = (int16_t)(wrap(dg.yaw) * 100), dg.ring[i][1] = 128 * 16;+ new_target(e);+}++/* each tick in the End: the crystals of the pillars near, the dragon back if the entities were lost */+void end_tick(void) {+ if (dim != DIM_END) return;+ int n;+ const EndSpike *s = gen_spikes(&n);+ for (int i = 0; i < n; i++) {+ int x = s[i].x, y = s[i].y + s[i].h + 1, z = s[i].z;+ if (!crystal_alive(i) || !world_loaded(x, vc_y0, z)) continue;+ bool there = false;+ for (int k = 0; k < N_ENT && !there; k++) there = ents[k].type == E_CRYSTAL && ents[k].tx == i;+ if (there) continue;+ Entity *e = ent_new(E_CRYSTAL, x + 0.5f, (float)y, z + 0.5f);+ if (e) e->tx = (int16_t)i, e->tz = 0, e->age = (int16_t)rnd(10000), e->health = 5;+ }+ if (!dst.dragon_dead && !dragon()) dragon_enter();+}++/* the boss bar: its health (0 to 1), or -1 with no dragon near (BossStatus, set as RenderDragon draws it) */+float dragon_bar(void) {+ Entity *e = dragon();+ if (!e || dim != DIM_END) return -1;+ float dx = e->x - pl.x, dz = e->z - pl.z;+ if (dx * dx + dz * dz > 160 * 160) return -1;+ return e->health / 200.0f;+}+/* the beam to the dragon from the crystal healing it: the crystal's place, false if none */+bool dragon_beam(float *from, float *to) {+ Entity *e = dragon();+ if (!e || dg.heal < 0 || dg.death) return false;+ int n;+ const EndSpike *s = gen_spikes(&n);+ from[0] = s[dg.heal].x + 0.5f, from[1] = s[dg.heal].y + s[dg.heal].h + 1.6f, from[2] = s[dg.heal].z + 0.5f;+ to[0] = e->x, to[1] = e->y + 2, to[2] = e->z;+ return true;+}+#endifdiff --git a/games/numblocks/src/entity.c b/games/numblocks/src/entity.cindex 1b355fc5..007c94d2 100644--- a/games/numblocks/src/entity.c+++ b/games/numblocks/src/entity.c@@ -111,7 +111,7 @@ Entity *ent_new(int type, float x, float y, float z) {e->y = e->py = y;e->z = e->pz = z;e->gx = x, e->gz = z;- if (type >= E_ZOMBIE && type <= E_CHICKEN) e->tx = (int16_t)floorf(x), e->tz = (int16_t)floorf(z); /* (going nowhere yet) */+ if (IS_MOB(type)) e->tx = (int16_t)floorf(x), e->tz = (int16_t)floorf(z); /* (going nowhere yet) */return e;}@@ -279,7 +279,7 @@ static void bobber_tick(Entity *e) {if (!e->on_ground)for (int i = 0; i < N_ENT; i++) {Entity *c = &ents[i];- if (c->type < E_ZOMBIE || c->type > E_CHICKEN || c->state == 255) continue;+ if (NOT_MOB(c->type) || c->state == 255) continue;float w = mob_width(c->type) / 2 + 0.125f;if (fabsf(c->x - e->x) < w && fabsf(c->z - e->z) < w && e->y + 0.25f > c->y && e->y < c->y + mob_height(c->type)) {mob_hooked(c);@@ -365,6 +365,9 @@ void ents_tick(void) {e->px = e->x, e->py = e->y, e->pz = e->z;e->age++;/* far outside the loaded blocks: gone */+#if NB_DIMS+ if (e->type != E_DRAGON) /* (the dragon flies far) */+#endifif (!world_loaded((int)floorf(e->x), vc_y0, (int)floorf(e->z))) {e->type = E_NONE;continue;diff --git a/games/numblocks/src/gen_dims.h b/games/numblocks/src/gen_dims.hindex 2ef18807..59577130 100644--- a/games/numblocks/src/gen_dims.h+++ b/games/numblocks/src/gen_dims.h@@ -653,6 +653,80 @@ static void end_grid(int cx, int cz, float *g) {static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out);+/* the density at one place of the grid g of its chunk, summed as func_180520_a sums it: end stone or not */+static int end_point(const float *g, int lx, int lz, int y) {+ int j1 = lx >> 3, k1 = lz >> 3, l1 = y >> 2;+ float d1 = g[((j1 + 0) * 3 + k1 + 0) * 33 + l1], d2 = g[((j1 + 0) * 3 + k1 + 1) * 33 + l1];+ float d3 = g[((j1 + 1) * 3 + k1 + 0) * 33 + l1], d4 = g[((j1 + 1) * 3 + k1 + 1) * 33 + l1];+ float d5 = (g[((j1 + 0) * 3 + k1 + 0) * 33 + l1 + 1] - d1) * 0.25f;+ float d6 = (g[((j1 + 0) * 3 + k1 + 1) * 33 + l1 + 1] - d2) * 0.25f;+ float d7 = (g[((j1 + 1) * 3 + k1 + 0) * 33 + l1 + 1] - d3) * 0.25f;+ float d8 = (g[((j1 + 1) * 3 + k1 + 1) * 33 + l1 + 1] - d4) * 0.25f;+ for (int i2 = 0; i2 < (y & 3); i2++) d1 += d5, d2 += d6, d3 += d7, d4 += d8;+ float d10 = d1, d11 = d2, d12 = (d3 - d1) * 0.125f, d13 = (d4 - d2) * 0.125f;+ for (int j2 = 0; j2 < (lx & 7); j2++) d10 += d12, d11 += d13;+ float d15 = d10, d16 = (d11 - d10) * 0.125f;+ for (int k2 = 0; k2 < (lz & 7); k2++) d15 += d16;+ return d15 > 0.0f;+}+/* the End's terrain at one place (a grid kept for its chunk in g, gx, gz) */+static int end_solid(float *g, int *gc, int x, int y, int z) {+ if (y < 0 || y >= 128) return 0;+ int cx = x >> 4, cz = z >> 4;+ if (cx * cx + cz * cz > 30 * 30) return 0;+ if (gc[2] != 1 || gc[0] != cx || gc[1] != cz) end_grid(cx, cz, g), gc[0] = cx, gc[1] = cz, gc[2] = 1;+ return end_point(g, x & 15, z & 15, y);+}++/* BiomeEndDecorator: a chunk in five has an obsidian pillar (WorldGenSpikes) where its decoration lands on+ * end stone: 6 to 37 high, 1 to 4 across, bedrock on top (its crystal: end.c). 1.8.8 decorates the End with+ * the world's random; here each chunk's is its own, so the pillars are the same every time: listed once,+ * in U (the End has no other use for it) */+typedef struct {+ int n, ok;+ EndSpike s[64];+} SpikeList;+#define SPK ((SpikeList *)(void *)&U)+const EndSpike *gen_spikes(int *n) {+ SpikeList *l = SPK;+ if (!l->ok) {+ l->ok = 1, l->n = 0;+ float g[297];+ int gc[3] = {0, 0, 0};+ for (int cz = -8; cz <= 8; cz++)+ for (int cx = -8; cx <= 8; cx++) {+ JRand r;+ jr_seed(&r, ((i64)cx * 341873128712LL + (i64)cz * 132897987541LL) ^ g_seed);+ if (jr_int(&r, 5) != 0) continue;+ int x = cx * 16 + jr_int(&r, 16) + 8, z = cz * 16 + jr_int(&r, 16) + 8, y = 127;+ while (y >= 0 && !end_solid(g, gc, x, y, z)) y--;+ if (y < 0 || ++y >= 127) continue;+ int h = jr_int(&r, 32) + 6, rad = jr_int(&r, 4) + 1, ok = 1;+ for (int k = x - rad; k <= x + rad && ok; k++)+ for (int l = z - rad; l <= z + rad && ok; l++)+ if ((k - x) * (k - x) + (l - z) * (l - z) <= rad * rad + 1 && !end_solid(g, gc, k, y - 1, l)) ok = 0;+ if (!ok || l->n >= 64) continue;+ if (y + h > 127) h = 127 - y;+ l->s[l->n++] = (EndSpike){(int16_t)x, (int16_t)z, (uint8_t)y, (uint8_t)h, (uint8_t)rad, 0};+ }+ }+ *n = l->n;+ return l->s;+}+/* what the End has there as made (end stone, a pillar, air) */+int gen_end_block(int x, int y, int z) {+ int n;+ const EndSpike *s = gen_spikes(&n);+ for (int i = 0; i < n; i++) {+ int dx = x - s[i].x, dz = z - s[i].z;+ if (dx * dx + dz * dz <= s[i].r * s[i].r + 1 && y >= s[i].y && y <= s[i].y + s[i].h)+ return y == s[i].y + s[i].h ? B_BEDROCK : B_OBSIDIAN;+ }+ float g[297];+ int gc[3] = {0, 0, 0};+ return end_solid(g, gc, x, y, z) ? B_END_STONE : B_AIR;+}+/* func_180520_a: end stone where the density is above 0 (the surface pass changes nothing there) */static void end_slab(int cx, int cz, int y0, int h, uint8_t *out) {float g[297];@@ -689,7 +763,24 @@ static void end_slab(int cx, int cz, int y0, int h, uint8_t *out) {end_decorate(cx, cz, y0, h, out);}-static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out) { (void)cx, (void)cz, (void)y0, (void)h, (void)out; }+static void end_decorate(int cx, int cz, int y0, int h, uint8_t *out) {+ int n;+ const EndSpike *s = gen_spikes(&n);+ for (int i = 0; i < n; i++) {+ const EndSpike *p = &s[i];+ if (p->x + p->r < cx * 16 || p->x - p->r > cx * 16 + 15 || p->z + p->r < cz * 16 || p->z - p->r > cz * 16 + 15) continue;+ for (int y = p->y; y <= p->y + p->h; y++) {+ if (y < y0 || y >= y0 + h) continue;+ for (int z = p->z - p->r; z <= p->z + p->r; z++)+ for (int x = p->x - p->r; x <= p->x + p->r; x++) {+ int lx = x - cx * 16, lz = z - cz * 16;+ if ((unsigned)lx > 15 || (unsigned)lz > 15) continue;+ if ((x - p->x) * (x - p->x) + (z - p->z) * (z - p->z) > p->r * p->r + 1) continue;+ out[((y - y0) * 16 + lz) * 16 + lx] = y == p->y + p->h ? B_BEDROCK : B_OBSIDIAN;+ }+ }+ }+}/* ======================================================================== *//* which generator runs */diff --git a/games/numblocks/src/mob.c b/games/numblocks/src/mob.cindex 5d9f1dfc..ef0daa9a 100644--- a/games/numblocks/src/mob.c+++ b/games/numblocks/src/mob.c@@ -21,12 +21,31 @@ static const MobInfo info[] = {[E_CREEPER] = {0.6f, 1.7f, 0.25f, 20}, [E_SPIDER] = {1.4f, 0.9f, 0.3f, 16},[E_PIG] = {0.9f, 0.9f, 0.25f, 10}, [E_COW] = {0.9f, 1.3f, 0.2f, 10},[E_SHEEP] = {0.9f, 1.3f, 0.23f, 8}, [E_CHICKEN] = {0.4f, 0.7f, 0.25f, 4},+#if NB_DIMS+ [E_PIGMAN] = {0.6f, 1.95f, 0.23f, 20}, [E_GHAST] = {4.0f, 4.0f, 0.0f, 10},+ [E_BLAZE] = {0.6f, 1.8f, 0.23f, 20}, [E_MAGMA] = {0.51f, 0.51f, 0.2f, 1},+ [E_WSKELETON] = {0.72f, 2.535f, 0.25f, 20}, [E_ENDERMAN] = {0.6f, 2.9f, 0.3f, 40},+ [E_CRYSTAL] = {2.0f, 2.0f, 0.0f, 5}, [E_DRAGON] = {16.0f, 8.0f, 0.0f, 200},+#endif};-float mob_width(int t) { return t >= E_ZOMBIE && t <= E_CHICKEN ? info[t].w : 0.25f; }-float mob_height(int t) { return t >= E_ZOMBIE && t <= E_CHICKEN ? info[t].h : 0.25f; }-static bool hostile(int t) { return t >= E_ZOMBIE && t <= E_SPIDER; }+#if NB_DIMS+static bool fire_immune(int t);+static bool neutral(int t);+static float rnd_gauss(void);+#endif+float mob_width(int t) { return IS_MOB(t) ? info[t].w : 0.25f; }+float mob_height(int t) { return IS_MOB(t) ? info[t].h : 0.25f; }+static bool hostile(int t) { return IS_MONSTER(t); }float mob_scale(const Entity *e, float pt) {+#if NB_DIMS+ switch (e->type) {+ case E_WSKELETON: return 1.2f; /* RenderSkeleton */+ case E_GHAST: return 4.5f; /* RenderGhast */+ case E_MAGMA: return e->sheared; /* RenderMagmaCube: its size */+ case E_CRYSTAL: return 2; /* ModelEnderCrystal */+ }+#endifif (e->growth < 0) return 0.5f; /* a baby */if (e->type != E_CREEPER || e->delay <= 0) return 1;/* RenderCreeper: swells by up to 40% (and flickers) over the fuse */@@ -172,6 +191,12 @@ int mob_parts(const Entity *e, float pt, Part *o) {box(-1, 0, -3, 1, 4, 6, 24, 13, 4, 13, 0, SKIN_CHICKEN)->rz = -flap;break;}+#if NB_DIMS+ default: {+ void dims_parts_(const Entity *e, float pt, float f, float a, float pitch, float age);+ dims_parts_(e, pt, f, a, pitch, age);+ }+#endif}return np;}@@ -200,7 +225,7 @@ static bool can_stand(int x, int y, int z) {static int count(bool host) {int n = 0;for (int i = 0; i < N_ENT; i++)- if (ents[i].type >= E_ZOMBIE && ents[i].type <= E_CHICKEN && hostile(ents[i].type) == host) n++;+ if (IS_MOB(ents[i].type) && hostile(ents[i].type) == host) n++;return n;}@@ -215,7 +240,13 @@ static Entity *spawn(int type, float x, float y, float z) {Entity *mob_summon(int type, float x, float y, float z) { return spawn(type, x, y, z); }/* SpawnerAnimals, made small: a try every second */+#if NB_DIMS+static bool dims_spawn(void);+#endifvoid mobs_spawn(void) {+#if NB_DIMS+ if (dims_spawn()) return;+#endifif (pl.dead) return;for (int k = 0; k < 4; k++) {int x = ifl(pl.x) + rnd(41) - 20, z = ifl(pl.z) + rnd(41) - 20;@@ -236,6 +267,13 @@ void mobs_spawn(void) {static const uint8_t kinds[4] = {E_ZOMBIE, E_SKELETON, E_CREEPER, E_SPIDER};int t = kinds[rnd(4)];if (t == E_SPIDER && !(can_stand(x + 1, y, z) && can_stand(x, y, z + 1))) t = E_ZOMBIE;+#if NB_DIMS+ /* an enderman: weight 10 of the monsters' 410 */+ if (rnd(41) == 0) {+ if (!can_stand(x, y + 1, z)) return;+ t = E_ENDERMAN;+ }+#endifspawn(t, x + 0.5f, (float)y, z + 0.5f);return;}@@ -282,12 +320,27 @@ static void loot(Entity *e) {drop(I_FEATHER, rnd(3), e);drop(e->fire > 0 ? I_COOKED_CHICKEN : I_CHICKEN, 1, e);break;+#if NB_DIMS+ default: {+ bool dims_loot_(Entity *e);+ if (dims_loot_(e)) return;+ }+#endif}player_add_xp(hostile(e->type) ? 5 : 1 + rnd(3));}+#if NB_DIMS+void dims_hurt_(Entity *e, float dmg, float kx, float kz);+#endifstatic void hurt(Entity *e, float dmg, float kx, float kz) {if (e->state == 255 || e->invuln > 10) return;+#if NB_DIMS+ if (e->type == E_DRAGON || e->type == E_CRYSTAL) {+ dims_hurt_(e, dmg, kx, kz);+ return;+ }+#endife->health = (int16_t)(e->health - (int)ceilf(dmg));e->invuln = 20;e->hurt = 10;@@ -302,6 +355,9 @@ static void hurt(Entity *e, float dmg, float kx, float kz) {if (!hostile(e->type)) e->panic = 100;else if (kx != 0 || kz != 0) e->panic = e->panic > 100 ? e->panic : 100; /* EntityAIHurtByTarget */if (e->type == E_SPIDER) e->panic = 600; /* a hit spider fights back, even by day */+#if NB_DIMS+ dims_hurt_(e, dmg, kx, kz);+#endifif (e->health <= 0) {e->state = 255;e->timer = 0;@@ -315,7 +371,7 @@ Entity *entity_looked_at(float reach, float block_t) {Entity *hit = NULL;for (int i = 0; i < N_ENT; i++) {Entity *e = &ents[i];- if (e->type < E_ZOMBIE || e->type > E_CHICKEN || e->state == 255) continue;+ if (NOT_MOB(e->type) || e->state == 255) continue;float w = info[e->type].w / 2 + 0.1f, h = info[e->type].h + 0.1f;float lo[3] = {e->x - w, e->y, e->z - w}, hi[3] = {e->x + w, e->y + h, e->z + w}, o[3] = {ex, ey, ez};float t0 = 0, t1 = best;@@ -480,7 +536,7 @@ void explode(float x, float y, float z, float power, bool blocks) {}for (int i = 0; i < N_ENT; i++) {Entity *e = &ents[i];- if (e->type < E_ZOMBIE || e->type > E_CHICKEN) continue;+ if (NOT_MOB(e->type)) continue;float ex = e->x - x, ez = e->z - z, ey = e->y - y;float d = sqrtf(ex * ex + ey * ey + ez * ez) / (power * 2);if (d <= 1) hurt(e, ((1 - d) * (1 - d) + (1 - d)) / 2 * 7 * power * 2 + 1, ex, ez);@@ -540,7 +596,7 @@ static void arrow_tick(Entity *e) {if (e->item.aux)for (int i = 0; i < N_ENT; i++) {Entity *m = &ents[i];- if (m->type < E_ZOMBIE || m->type > E_CHICKEN || m->state == 255) continue;+ if (NOT_MOB(m->type) || m->state == 255) continue;float w = info[m->type].w / 2 + 0.15f, mx = m->x - nx, mz = m->z - nz;if (fabsf(mx) < w && fabsf(mz) < w && ny > m->y - 0.1f && ny < m->y + info[m->type].h) {/* EntityArrow: damage 2 x speed, rounded up (a full draw sometimes more) */@@ -709,6 +765,10 @@ void mob_tick(Entity *e) {tnt_tick(e);return;}+#if NB_DIMS+ bool dims_tick_(Entity *e);+ if (e->type > E_BOBBER && dims_tick_(e)) return;+#endifconst MobInfo *mi = &info[e->type];if (e->hurt > 0) e->hurt--;if (e->invuln > 0) e->invuln--;@@ -729,13 +789,19 @@ void mob_tick(Entity *e) {int bx = ifl(e->x), by = ifl(e->y), bz = ifl(e->z);bool in_water = blk_model[world_get(bx, ifl(e->y + 0.4f), bz)] == M_LIQUID;bool in_lava = is_lava(world_get(bx, ifl(e->y + 0.4f), bz));+#if NB_DIMS+ bool immune = fire_immune(e->type);+ if (immune) in_lava = false, e->fire = 0;+#else+#define immune 0+#endif/* daylight burns zombies and skeletons (EntityZombie.onLivingUpdate) */if ((e->type == E_ZOMBIE || e->type == E_SKELETON) && daylight() && !in_water &&light_there(bx, ifl(e->y + 1.6f), bz) >= 15 && world_get(bx, ifl(e->y + 1.6f), bz) == B_AIR) {if (e->fire < 160) e->fire = 160;}if (in_lava) e->fire = 300, hurt(e, 4, 0, 0);- if (world_get(bx, by, bz) == B_FIRE || world_get(bx, ifl(e->y + 1), bz) == B_FIRE) {+ if (!immune && (world_get(bx, by, bz) == B_FIRE || world_get(bx, ifl(e->y + 1), bz) == B_FIRE)) {hurt(e, 1, 0, 0);if (e->fire < 160) e->fire = 160;}@@ -778,9 +844,23 @@ void mob_tick(Entity *e) {/* EntityAINearestAttackableTarget: seen within 16 (a spider only in the dark); EntityAITarget: kept* while seen in the last 3 seconds, within 16 (zombies 35) */bool dark = e->type != E_SPIDER || light_there(bx, by + 1, bz) < 8 || !daylight();+#if NB_DIMS+ if (!neutral(e->type))+#endifif (can && dist < 16 && (dark || e->panic > 60) && (ticks_run + (unsigned)(e - ents)) % 4 == 0 && sees(e))e->panic = e->panic > 60 ? e->panic : 60;chase = can && e->panic > 0 && dist < (e->type == E_ZOMBIE ? 35 : 16);+#if NB_DIMS+ if (e->type == E_PIGMAN || e->type == E_ENDERMAN || e->type == E_BLAZE) chase = can && e->panic > 0 && dist < 40;+ if (e->type == E_BLAZE) {+ void blaze_tick_(Entity *e, bool chase, float dist);+ blaze_tick_(e, chase, dist);+ }+ if (e->type == E_MAGMA) {+ void magma_tick_(Entity *e, bool chase, float dist);+ magma_tick_(e, chase, dist);+ }+#endifif (!can) e->panic = 0;}if (chase) {@@ -861,6 +941,9 @@ void mob_tick(Entity *e) {e->pitch *= 0.8f;/* moving: EntityLivingBase.moveEntityWithHeading, the AI's speed twice (as forward and as friction) */float fwd = go && e->state != 255 ? speed : 0;+#if NB_DIMS+ if (e->type == E_MAGMA) fwd = 0; /* (it only hops: magma_tick) */+#endiffloat yr = e->yaw * 0.017453292f;if (in_water) {if (rndf() < 0.8f) e->vy += 0.04f; /* EntityAISwimming */@@ -891,6 +974,9 @@ void mob_tick(Entity *e) {}/* against a wall: a jump (EntityJumpHelper), set for the next move as Minecraft sets it before* moving, so the first rise is the whole 0.42 and a block is climbed */+#if NB_DIMS+ if (e->type != E_MAGMA)+#endifif (blocked && go && ground && e->type != E_SPIDER) e->vy = 0.42f;/* the walk's swing (limbSwingAmount follows how fast it moves) */float target = moved * 4;@@ -906,6 +992,16 @@ void mob_tick(Entity *e) {push_player(dx, dz, 0.4f);e->delay = 20;}+#if NB_DIMS+ /* the pigman's golden sword 9, the wither skeleton's stone sword 8 (and the wither), the enderman 7 */+ if ((e->type == E_PIGMAN || e->type == E_WSKELETON || e->type == E_ENDERMAN) && dist * dist < reach + 0.5f &&+ fabsf(dy) < 2.0f && !e->delay) {+ player_hurt(scaled(e->type == E_PIGMAN ? 9 : e->type == E_WSKELETON ? 8 : 7), DMG_MOB);+ push_player(dx, dz, 0.4f);+ if (e->type == E_WSKELETON) wither_ticks = 200;+ e->delay = 20;+ }+#endif/* a spider leaps (EntityAILeapAtTarget) */if (e->type == E_SPIDER && ground && dist > 2 && dist < 4 && rnd(5) == 0) {e->vx += dx / dist * 0.4f;@@ -919,3 +1015,20 @@ void mob_tick(Entity *e) {}if (e->y < -64) e->type = E_NONE;}+#if !NB_DIMS+#undef immune+#endif++#if NB_DIMS+#include "mob_dims.h"+void dims_parts_(const Entity *e, float pt, float f, float a, float pitch, float age) { dims_parts(e, pt, f, a, pitch, age); }+bool dims_loot_(Entity *e) { return dims_loot(e); }+bool dims_tick_(Entity *e) { return dims_tick(e); }+void blaze_tick_(Entity *e, bool chase, float dist) { blaze_tick(e, chase, dist); }+void magma_tick_(Entity *e, bool chase, float dist) { magma_tick(e, chase, dist); }+void dims_hurt_(Entity *e, float dmg, float kx, float kz) {+ if (e->type == E_DRAGON) dragon_hurt(e, dmg);+ else if (e->type == E_CRYSTAL) crystal_gone(e);+ else dims_hurt(e, kx, kz);+}+#endifdiff --git a/games/numblocks/src/mob.h b/games/numblocks/src/mob.hindex 95ca6b32..ae5ccbdd 100644--- a/games/numblocks/src/mob.h+++ b/games/numblocks/src/mob.h@@ -15,7 +15,12 @@ typedef struct {float grow; /* inflated by this much on every side (sheep's wool) */} Part;+#if NB_DIMS+#define MAX_PARTS 14 /* (a blaze's thirteen) */+#define MAX_PARTS_DRAGON 42 /* (drawn in world_scratch: 6144 bytes) */+#else#define MAX_PARTS 12+#endifint mob_parts(const Entity *e, float pt, Part *out); /* this frame's parts */float mob_width(int type), mob_height(int type);float mob_scale(const Entity *e, float pt); /* a creeper swells before it blows */diff --git a/games/numblocks/src/mob_dims.h b/games/numblocks/src/mob_dims.hnew file mode 100644index 00000000..92e15338--- /dev/null+++ b/games/numblocks/src/mob_dims.h@@ -0,0 +1,706 @@+/* NumBlocks.nwa only (NB_DIMS), included at the end of mob.c: the Nether's and the End's creatures as+ * Minecraft 1.8.8 has them: zombie pigmen (EntityPigZombie), ghasts and their fireballs (EntityGhast,+ * EntityLargeFireball), blazes (EntityBlaze, EntitySmallFireball), magma cubes (EntityMagmaCube), wither+ * skeletons (EntitySkeleton of type 1), endermen (EntityEnderman) and ender crystals; their models, how+ * they move and fight, where they spawn (ChunkProviderHell.getPossibleCreatures, the End's endermen, the+ * Overworld's), what they drop. The Ender Dragon is end.c's. */++/* java.util.Random.nextGaussian, near enough */+static float rnd_gauss(void) {+ float u = rndf() + 1e-6f, v = rndf();+ return sqrtf(-2 * logf(u)) * cosf(6.2831853f * v);+}++/* isImmuneToFire */+static bool fire_immune(int t) { return t == E_PIGMAN || t == E_GHAST || t == E_BLAZE || t == E_MAGMA || t == E_WSKELETON || t == E_DRAGON || t == E_CRYSTAL; }+/* angry only when crossed (a pigman hit, an enderman looked at or hit): e->panic counts the anger down */+static bool neutral(int t) { return t == E_PIGMAN || t == E_ENDERMAN; }+++/* ---------------------------------------------------------------- models */+/* a rotation (world axes, as Minecraft's GL calls turn) as the part turns (Z, then Y, then X, in the+ * model's frame) */+static void part_turn(Part *p, const float *R) {+ float m[9];+ for (int i = 0; i < 9; i++) m[i] = R[i];+ /* F R F: the model's frame is the world's turned about x (F = diag(1, -1, -1), the entity facing 0) */+ m[1] = -m[1], m[2] = -m[2], m[3] = -m[3], m[6] = -m[6];+ float sb = -m[6];+ sb = sb > 1 ? 1 : sb < -1 ? -1 : sb;+ p->ry = asinf(sb);+ p->rx = atan2f(m[7], m[8]);+ p->rz = atan2f(m[3], m[0]);+}+static void mat_axis(float *m, float a, float x, float y, float z) { /* glRotate: a radians about (x, y, z) */+ float l = sqrtf(x * x + y * y + z * z);+ x /= l, y /= l, z /= l;+ float c = cosf(a), s = sinf(a), t = 1 - c;+ m[0] = t * x * x + c, m[1] = t * x * y - s * z, m[2] = t * x * z + s * y;+ m[3] = t * x * y + s * z, m[4] = t * y * y + c, m[5] = t * y * z - s * x;+ m[6] = t * x * z - s * y, m[7] = t * y * z + s * x, m[8] = t * z * z + c;+}+static void mat_mul(const float *a, const float *b, float *o) {+ float t[9];+ for (int r = 0; r < 3; r++)+ for (int c = 0; c < 3; c++) t[r * 3 + c] = a[r * 3] * b[c] + a[r * 3 + 1] * b[3 + c] + a[r * 3 + 2] * b[6 + c];+ for (int i = 0; i < 9; i++) o[i] = t[i];+}++/* ModelDragon: its parts, each turned in the frame of the one it hangs from (a frame: the pivot and the turn,+ * in the model's pixels) */+typedef struct {+ float m[9], p[3];+} Xf;+static void rzyx(float rx, float ry, float rz, float *m) {+ float cx = cosf(rx), sx = sinf(rx), cy = cosf(ry), sy = sinf(ry), cz = cosf(rz), sz = sinf(rz);+ m[0] = cz * cy, m[1] = cz * sy * sx - sz * cx, m[2] = cz * sy * cx + sz * sx;+ m[3] = sz * cy, m[4] = sz * sy * sx + cz * cx, m[5] = sz * sy * cx - cz * sx;+ m[6] = -sy, m[7] = cy * sx, m[8] = cy * cx;+}+/* a ModelRenderer at (px, py, pz) in a, turned rz, ry, rx */+static void xf_child(const Xf *a, float px, float py, float pz, float rx, float ry, float rz, Xf *o) {+ float r[9], q[3] = {px, py, pz};+ rzyx(rx, ry, rz, r);+ float p[3];+ for (int i = 0; i < 3; i++) p[i] = a->p[i] + a->m[i * 3] * q[0] + a->m[i * 3 + 1] * q[1] + a->m[i * 3 + 2] * q[2];+ mat_mul(a->m, r, o->m);+ o->p[0] = p[0], o->p[1] = p[1], o->p[2] = p[2];+}+/* a box of the dragon's skin in frame x (mirrored: the left side, GL's scale(-1, 1, 1)) */+static void xbox(const Xf *x, int mir, float bx, float by, float bz, int w, int h, int d, int u, int v) {+ float m[9];+ for (int i = 0; i < 9; i++) m[i] = x->m[i];+ float px = x->p[0];+ if (mir) m[1] = -m[1], m[2] = -m[2], m[3] = -m[3], m[6] = -m[6], px = -px, bx = -bx - w;+ if (np >= MAX_PARTS_DRAGON) return;+ Part *p = box(bx, by, bz, w, h, d, u & 255, v, px, x->p[1], x->p[2], SKIN_DRAGON);+ p->mirror = (uint8_t)mir;+ float sb = -m[6];+ p->ry = asinf(sb > 1 ? 1 : sb < -1 ? -1 : sb);+ p->rx = atan2f(m[7], m[8]);+ p->rz = atan2f(m[3], m[0]);+}+static float dwrap(float a) {+ while (a >= 180) a -= 360;+ while (a < -180) a += 360;+ return a;+}+static void dragon_parts(const Entity *e, float pt) {+ float f = dragon_anim(pt), f8 = f * PI * 2;+ float jaw = (sinf(f8) + 1) * 0.2f, f1 = sinf(f8 - 1) + 1;+ f1 = (f1 * f1 + f1 * 2) * 0.05f;+ /* the whole: 2 blocks up, 3 ahead, nodding with the wings */+ Xf G, B, X, Y, Z;+ rzyx(f1 * 2 * PI / 180, 0, 0, G.m);+ G.p[0] = 0, G.p[1] = (f1 - 2) * 16, G.p[2] = -48;+ float y6, h6, y5, h5, y10, h10, yy, hh;+ dragon_offsets(6, pt, &y6, &h6);+ dragon_offsets(5, pt, &y5, &h5);+ dragon_offsets(10, pt, &y10, &h10);+ float f5 = 1.5f, f6 = dwrap(y5 - y10), f7 = dwrap(y5 + f6 / 2), D = PI / 180;+ /* the neck: five vertebrae, following its last turns */+ float f2 = 20, f3 = -12, f4 = 0;+ for (int i = 0; i < 5; i++) {+ dragon_offsets(5 - i, pt, &yy, &hh);+ float f9 = cosf((float)i * 0.45f + f8) * 0.15f;+ float ry = dwrap(yy - y6) * D * f5, rx = f9 + (hh - h6) * D * f5 * 5, rz = -dwrap(yy - f7) * D * f5;+ xf_child(&G, f4, f2, f3, rx, ry, rz, &X);+ xbox(&X, 0, -5, -5, -5, 10, 10, 10, 192, 104);+ f2 += sinf(rx) * 10, f3 -= cosf(ry) * cosf(rx) * 10, f4 -= sinf(ry) * cosf(rx) * 10;+ }+ /* the head and its jaw */+ dragon_offsets(0, pt, &yy, &hh);+ xf_child(&G, f4, f2, f3, 0, dwrap(yy - y6) * D, -dwrap(yy - f7) * D, &X);+ xbox(&X, 0, -6, -1, -24, 12, 5, 16, 176, 44);+ xbox(&X, 0, -8, -8, -10, 16, 16, 16, 112, 30);+ xf_child(&X, 0, 4, -8, jaw, 0, 0, &Y);+ xbox(&Y, 0, -6, 0, -16, 12, 4, 16, 176, 65);+ /* the body, rolled as it turns; its wings and legs on both sides */+ Xf T;+ xf_child(&G, 0, 16, 0, 0, 0, -f6 * f5 * D, &T);+ xf_child(&T, 0, -16, 0, 0, 0, 0, &B);+ xf_child(&B, 0, 4, 8, 0, 0, 0, &X);+ xbox(&X, 0, -12, 0, -16, 24, 24, 64, 0, 0);+ float f11 = f8;+ for (int side = 0; side < 2; side++) {+ xf_child(&B, -12, 5, 2, 0.125f - cosf(f11) * 0.2f, 0.25f, (sinf(f11) + 0.125f) * 0.8f, &X);+ xbox(&X, side, -56, -4, -4, 56, 8, 8, 112, 88);+ xbox(&X, side, -56, 0, 2, 56, 0, 56, -56, 88);+ xf_child(&X, -56, 0, 0, 0, 0, -(sinf(f11 + 2) + 0.5f) * 0.75f, &Y);+ xbox(&Y, side, -56, -2, -2, 56, 4, 4, 112, 136);+ xbox(&Y, side, -56, 0, 2, 56, 0, 56, -56, 144);+ xf_child(&B, -12, 20, 2, 1.3f + f1 * 0.1f, 0, 0, &X);+ xbox(&X, side, -4, -4, -4, 8, 24, 8, 112, 104);+ xf_child(&X, 0, 20, -1, -0.5f - f1 * 0.1f, 0, 0, &Y);+ xbox(&Y, side, -3, -1, -3, 6, 24, 6, 226, 138);+ xf_child(&Y, 0, 23, 0, 0.75f + f1 * 0.1f, 0, 0, &Z);+ xbox(&Z, side, -4, 0, -12, 8, 4, 16, 144, 104);+ xf_child(&B, -16, 16, 42, 1.0f + f1 * 0.1f, 0, 0, &X);+ xbox(&X, side, -8, -4, -8, 16, 32, 16, 0, 0);+ xf_child(&X, 0, 32, -4, 0.5f + f1 * 0.1f, 0, 0, &Y);+ xbox(&Y, side, -6, -2, 0, 12, 32, 12, 196, 0);+ xf_child(&Y, 0, 31, 4, 0.75f + f1 * 0.1f, 0, 0, &Z);+ xbox(&Z, side, -9, 0, -20, 18, 6, 24, 112, 0);+ }+ /* the tail: twelve, swaying */+ float y11, h11, f10 = 0;+ dragon_offsets(11, pt, &y11, &h11);+ f2 = 10, f3 = 60, f4 = 0;+ for (int k = 0; k < 12; k++) {+ dragon_offsets(12 + k, pt, &yy, &hh);+ f10 += sinf((float)k * 0.45f + f8) * 0.05f;+ float ry = (dwrap(yy - y11) * f5 + 180) * D, rx = f10 + (hh - h11) * D * f5 * 5, rz = dwrap(yy - f7) * D * f5;+ xf_child(&G, f4, f2, f3, rx, ry, rz, &X);+ xbox(&X, 0, -5, -5, -5, 10, 10, 10, 192, 104);+ f2 += sinf(rx) * 10, f3 -= cosf(ry) * cosf(rx) * 10, f4 -= sinf(ry) * cosf(rx) * 10;+ }+ (void)e;+}++static void dims_parts(const Entity *e, float pt, float f, float a, float pitch, float age) {+ float sw = cosf(f * 0.6662f), swo = cosf(f * 0.6662f + PI);+ switch (e->type) {+ case E_PIGMAN:+ case E_WSKELETON: {+ /* ModelZombie (the pigman), ModelSkeleton (its arms held out as a zombie's: no bow) */+ bool pig = e->type == E_PIGMAN;+ int sk = pig ? SKIN_ZOMBIE_PIGMAN : SKIN_WITHER_SKELETON, aw = pig ? 4 : 2, o1 = pig ? 2 : 1;+ box(-4, -8, -4, 8, 8, 8, 0, 0, 0, 0, 0, sk)->rx = pitch;+ box(-4, 0, -2, 8, 12, 4, 16, 16, 0, 0, 0, sk);+ Part *ra = box(pig ? -3 : -1, -2, -o1, aw, 12, aw, 40, 16, -5, 2, 0, sk);+ Part *la = box(-1, -2, -o1, aw, 12, aw, 40, 16, 5, 2, 0, sk);+ Part *rl = box(-o1, 0, -o1, aw, 12, aw, 0, 16, pig ? -1.9f : -2, 12, 0, sk);+ Part *ll = box(-o1, 0, -o1, aw, 12, aw, 0, 16, pig ? 1.9f : 2, 12, 0, sk);+ ra->rx = la->rx = -PI / 2 + sinf(age * 0.067f) * 0.05f;+ ra->rz = cosf(age * 0.09f) * 0.05f + 0.05f;+ la->rz = -ra->rz;+ la->mirror = ll->mirror = 1;+ rl->rx = sw * 1.4f * a;+ ll->rx = swo * 1.4f * a;+ break;+ }+ case E_ENDERMAN: {+ /* ModelEnderman: a biped 14 pixels taller, its limbs long and thin, swinging half as far; the jaw+ * drops (the head rises) when it screams; arms forward carrying a block */+ bool angry = e->panic > 0;+ float hy = -13 - (angry ? 5 : 0);+ box(-4, -8, -4, 8, 8, 8, 0, 0, 0, hy, 0, SKIN_ENDERMAN)->rx = pitch;+ Part *jaw = box(-4, -8, -4, 8, 8, 8, 0, 16, 0, -13, 0, SKIN_ENDERMAN);+ jaw->grow = -0.5f, jaw->rx = pitch;+ box(-4, 0, -2, 8, 12, 4, 32, 16, 0, -14, 0, SKIN_ENDERMAN);+ float ar = swo * a * 0.5f, al = sw * a * 0.5f, lr = sw * 1.4f * a * 0.5f, ll = swo * 1.4f * a * 0.5f;+#define CL(v) ((v) > 0.4f ? 0.4f : (v) < -0.4f ? -0.4f : (v))+ Part *rA = box(-1, -2, -1, 2, 30, 2, 56, 0, -3, -12, 0, SKIN_ENDERMAN);+ Part *lA = box(-1, -2, -1, 2, 30, 2, 56, 0, 5, -12, 0, SKIN_ENDERMAN);+ rA->rx = CL(ar), lA->rx = CL(al), lA->mirror = 1;+ if (e->love) rA->rx = lA->rx = -0.5f, rA->rz = 0.05f, lA->rz = -0.05f; /* (carrying: love holds the block) */+ box(-1, 0, -1, 2, 30, 2, 56, 0, -2, -5, 0, SKIN_ENDERMAN)->rx = CL(lr);+ Part *lL = box(-1, 0, -1, 2, 30, 2, 56, 0, 2, -5, 0, SKIN_ENDERMAN);+ lL->rx = CL(ll), lL->mirror = 1;+#undef CL+ break;+ }+ case E_GHAST: {+ /* ModelGhast: a 16-pixel cube and nine tentacles (their lengths from Random(1660)), 0.6 lower;+ * shooting, its eyes and mouth open */+ static const uint8_t len[9] = {8, 13, 9, 11, 11, 10, 12, 9, 12};+ int sk = e->state == 1 ? SKIN_GHAST_SHOOTING : SKIN_GHAST;+ box(-8, -8, -8, 16, 16, 16, 0, 0, 0, 8 + 9.6f, 0, sk);+ for (int i = 0; i < 9; i++) {+ float fx = (((float)(i % 3) - (float)(i / 3 % 2) * 0.5f + 0.25f) / 2.0f * 2.0f - 1.0f) * 5.0f;+ float fz = ((float)(i / 3) / 2.0f * 2.0f - 1.0f) * 5.0f;+ box(-1, 0, -1, 2, len[i], 2, 0, 0, fx, 15 + 9.6f, fz, sk)->rx = 0.2f * sinf(age * 0.3f + (float)i) + 0.4f;+ }+ break;+ }+ case E_BLAZE: {+ /* ModelBlaze: the head and twelve rods going round in three rings */+ Part *h = box(-4, -4, -4, 8, 8, 8, 0, 0, 0, 0, 0, SKIN_BLAZE);+ h->rx = pitch;+ float g = age * PI * -0.1f;+ for (int i = 0; i < 4; i++, g++)+ box(0, 0, 0, 2, 8, 2, 0, 16, cosf(g) * 9, -2 + cosf(((float)(i * 2) + age) * 0.25f), sinf(g) * 9, SKIN_BLAZE);+ g = PI / 4 + age * PI * 0.03f;+ for (int j = 4; j < 8; j++, g++)+ box(0, 0, 0, 2, 8, 2, 0, 16, cosf(g) * 7, 2 + cosf(((float)(j * 2) + age) * 0.25f), sinf(g) * 7, SKIN_BLAZE);+ g = 0.47123894f + age * PI * -0.05f;+ for (int k = 8; k < 12; k++, g++)+ box(0, 0, 0, 2, 8, 2, 0, 16, cosf(g) * 5, 11 + cosf(((float)k * 1.5f + age) * 0.5f), sinf(g) * 5, SKIN_BLAZE);+ break;+ }+ case E_MAGMA: {+ /* ModelMagmaCube: eight slices of 8 x 1 x 8 pulled apart as it squishes, and its core */+ float sq = e->on_ground ? 0 : 0.6f;+ for (int i = 0; i < 8; i++) {+ int u = 0, v = i;+ if (i == 2) u = 24, v = 10;+ else if (i == 3) u = 24, v = 19;+ box(-4, (float)(16 + i), -4, 8, 1, 8, u, v, 0, (float)(-(4 - i)) * sq * 1.7f, 0, SKIN_MAGMACUBE);+ }+ box(-2, 18, -2, 4, 4, 4, 0, 16, 0, 0, 0, SKIN_MAGMACUBE);+ break;+ }+ case E_CRYSTAL: {+ /* ModelEnderCrystal (scaled 2: mob_scale): its base, then the glass turning on a slant inside+ * glass inside the cube, bobbing */+ float t = e->age + pt, bob = sinf(t * 0.2f) / 2 + 0.5f;+ bob = (bob * bob + bob) * 0.2f;+ float spin = t * 3 * PI / 180;+ box(-6, 28, -6, 12, 4, 12, 0, 16, 0, 0, 0, SKIN_CRYSTAL);+ float R[9], A[9], Y[9], S[9];+ mat_axis(Y, spin, 0, 1, 0);+ mat_axis(A, PI / 3, 0.7071f, 0, 0.7071f);+ mat_mul(Y, A, R);+ float cy = 24.125f - (0.8f + bob - 0.5f) * 16; /* (2 x (-0.5 + 0.8 + bob) blocks up, in the model's pixels at scale 2) */+ static const float grow[3] = {0, -0.5f, -0.94f};+ for (int k = 0; k < 3; k++) {+ Part *p = box(-4, -4, -4, 8, 8, 8, k == 2 ? 0 : 32, 0, 0, cy, 0, SKIN_CRYSTAL);+ p->grow = grow[k];+ part_turn(p, R);+ mat_mul(R, A, S);+ mat_mul(S, Y, R);+ }+ break;+ }+ case E_DRAGON:+ dragon_parts(e, pt);+ break;+ }+}++/* ---------------------------------------------------------------- spawning */+/* ChunkProviderHell.getPossibleCreatures and the End's: a try every second, as for the Overworld; the+ * Overworld's endermen are one in 41 of its monsters (weight 10 of 410) */+static bool room_for(float x, float y, float z, float w, float h) {+ float b[6] = {x - w / 2, y, z - w / 2, x + w / 2, y + h, z + w / 2};+ return phys_free(b);+}+static Entity *dims_mob(int type, float x, float y, float z) {+ Entity *e = spawn(type, x, y, z);+ if (!e) return NULL;+ if (type == E_MAGMA) {+ /* EntitySlime.onInitialSpawn: size 1, 2 or 4; the health its square */+ e->sheared = (uint8_t)(1 << rnd(3));+ e->health = (int16_t)(e->sheared * e->sheared);+ }+ return e;+}+static bool dims_spawn(void) {+ if (dim == DIM_OVERWORLD) return false; /* (mobs_spawn: its endermen) */+ if (pl.dead || opt.difficulty == 0) return true;+ for (int k = 0; k < 4; k++) {+ int x = ifl(pl.x) + rnd(41) - 20, z = ifl(pl.z) + rnd(41) - 20;+ float dx = x + 0.5f - pl.x, dz = z + 0.5f - pl.z;+ if (dx * dx + dz * dz < 14 * 14) continue;+ int y0 = ifl(pl.y) + 8;+ for (int y = y0; y > y0 - 20; y--) {+ if (!world_loaded(x, y + 1, z) || !world_loaded(x, y - 1, z) || !can_stand(x, y, z)) continue;+ if (count(true) >= 4) return true;+ int below = world_get(x, y - 1, z), t;+ if (dim == DIM_END) t = E_ENDERMAN;+ else {+ int fo = gen_fortress_at(x, y, z);+ if (fo == 2 || (fo == 1 && below == B_NETHER_BRICK)) {+ /* the fortress's: blaze 10, pigman 5, magma cube 3, skeleton 2 (in the Nether, four in+ * five of them wither skeletons) */+ int r = rnd(20);+ t = r < 10 ? E_BLAZE : r < 15 ? E_PIGMAN : r < 18 ? E_MAGMA : rnd(5) ? E_WSKELETON : E_SKELETON;+ } else {+ /* BiomeGenHell: ghast 50, pigman 100, magma cube 1; a ghast one try in 20 */+ int r = rnd(151);+ t = r < 50 ? E_GHAST : r < 150 ? E_PIGMAN : E_MAGMA;+ if (t == E_GHAST && rnd(20)) return true;+ }+ }+ /* monsters but these (EntityMob.isValidLightLevel) in the dark */+ if ((t == E_WSKELETON || t == E_SKELETON || t == E_ENDERMAN) && light_there(x, y, z) > rnd(8)) return true;+ const MobInfo *mi = &info[t];+ if (!room_for(x + 0.5f, (float)y, z + 0.5f, mi->w, mi->h)) return true;+ dims_mob(t, x + 0.5f, (float)y, z + 0.5f);+ return true;+ }+ }+ return true;+}++/* ---------------------------------------------------------------- drops */+static bool dims_loot(Entity *e) {+ switch (e->type) {+ case E_PIGMAN:+ drop(I_ROTTEN_FLESH, rnd(2), e);+ drop(I_GOLD_NUGGET, rnd(2), e);+ if (rnd(40) == 0) drop(I_GOLD_INGOT, 1, e); /* (addRandomDrop, one in forty with the player's kill) */+ break;+ case E_GHAST:+ drop(I_GHAST_TEAR, rnd(2), e);+ drop(I_GUNPOWDER, rnd(3), e);+ break;+ case E_BLAZE: drop(I_BLAZE_ROD, rnd(2), e); break;+ case E_MAGMA: {+ int n = e->sheared > 1 ? rnd(4) - 2 : 0;+ drop(I_MAGMA_CREAM, n, e);+ /* EntitySlime.setDead: two to four of half its size */+ if (e->sheared > 1) {+ int k = 2 + rnd(3), s = e->sheared / 2;+ for (int i = 0; i < k; i++) {+ float fx = ((float)(i % 2) - 0.5f) * e->sheared / 4.0f, fz = ((float)(i / 2) - 0.5f) * e->sheared / 4.0f;+ Entity *c = spawn(E_MAGMA, e->x + fx, e->y + 0.5f, e->z + fz);+ if (c) c->sheared = (uint8_t)s, c->health = (int16_t)(s * s);+ }+ }+ player_add_xp(e->sheared);+ return true;+ }+ case E_WSKELETON:+ drop(I_COAL, rnd(3) - 1, e);+ drop(I_BONE, rnd(3), e);+ break;+ case E_ENDERMAN: drop(I_ENDER_PEARL, rnd(2), e); break;+ default: return true;+ }+ player_add_xp(e->type == E_BLAZE ? 10 : 5);+ return true;+}++/* ---------------------------------------------------------------- fireballs, eyes */+/* EntityFireball: from a mob towards (dx, dy, dz) with a little spread, sped up 0.1 a tick that way and+ * slowed to 0.95. item.aux: 0 a ghast's (explodes), 1 a blaze's (sets fire); state 1 once the player+ * sent it back */+static void fireball(const Entity *from, float x, float y, float z, float dx, float dy, float dz, int small) {+ Entity *f = ent_new(E_FIREBALL, x, y, z);+ if (!f) return;+ f->item.id = I_FIRE_CHARGE, f->item.aux = (uint16_t)small;+ float sp = small ? 0.0f : 0.4f; /* (the large one: nextGaussian x 0.4 on each) */+ dx += rnd_gauss() * sp, dy += rnd_gauss() * sp, dz += rnd_gauss() * sp;+ float d = sqrtf(dx * dx + dy * dy + dz * dz);+ if (d < 1e-4f) d = 1;+ f->gx = dx / d * 0.1f, f->gz = dz / d * 0.1f, f->limb = dy / d * 0.1f; /* (the acceleration: y's in limb) */+ (void)from;+}+/* explosion power p, setting fire about it (Explosion with isFlaming) */+static void explode_fire(float x, float y, float z, float p, bool blocks, bool flame) {+ explode(x, y, z, p, blocks);+ if (!flame || !blocks) return;+ int r = (int)ceilf(p) + 1;+ for (int dy = -r; dy <= r; dy++)+ for (int dz = -r; dz <= r; dz++)+ for (int dx = -r; dx <= r; dx++) {+ int bx = ifl(x) + dx, by = ifl(y) + dy, bz = ifl(z) + dz;+ if (dx * dx + dy * dy + dz * dz > r * r || rnd(3)) continue;+ if (world_get(bx, by, bz) == B_AIR && (blk_flags[world_get(bx, by - 1, bz)] & BF_OPAQUE)) fire_set(bx, by, bz, 0);+ }+}+static void fireball_tick(Entity *e) {+ bool small = e->item.aux == 1;+ float ay = e->limb;+ float nx = e->x + e->vx, ny = e->y + e->vy, nz = e->z + e->vz;+ /* what it hits: the player (not its own, the first 25 ticks), a mob (sent back: its ghast), a block */+ float px = pl.x - nx, pz = pl.z - nz, r = small ? 0.5f : 1.0f;+ if (e->state == 0 && !pl.dead && px * px + pz * pz < r * r + 0.3f && ny > pl.y - r && ny < pl.y + 1.8f + r) {+ if (small) {+ player_hurt(scaled(5), DMG_FIRE);+ if (pl.fire < 100) pl.fire = 100;+ } else {+ player_hurt(scaled(6), DMG_MOB);+ explode_fire(nx, ny, nz, 1, rule(GR_MOB_GRIEFING), rule(GR_MOB_GRIEFING));+ }+ e->type = E_NONE;+ return;+ }+ if (e->state == 1)+ for (int i = 0; i < N_ENT; i++) {+ Entity *m = &ents[i];+ if (!IS_MOB(m->type) || m->state == 255) continue;+ float w = info[m->type].w / 2 + 0.5f, mx = m->x - nx, mz = m->z - nz;+ if (fabsf(mx) < w && fabsf(mz) < w && ny > m->y - 0.5f && ny < m->y + info[m->type].h) {+ /* EntityGhast.attackEntityFrom: its own fireball, sent back by the player, kills it */+ hurt(m, m->type == E_GHAST && !small ? 1000 : 6, e->vx, e->vz);+ explode_fire(nx, ny, nz, 1, rule(GR_MOB_GRIEFING), rule(GR_MOB_GRIEFING));+ e->type = E_NONE;+ return;+ }+ }+ int b = world_get(ifl(nx), ifl(ny), ifl(nz));+ if ((blk_flags[b] & BF_SOLID) || ++e->timer > 600) {+ if (small) {+ /* EntitySmallFireball: fire where it came from, against the block */+ int fx = ifl(e->x), fy = ifl(e->y), fz = ifl(e->z);+ if (world_get(fx, fy, fz) == B_AIR && rule(GR_MOB_GRIEFING)) fire_set(fx, fy, fz, 0);+ } else+ explode_fire(e->x, e->y, e->z, 1, rule(GR_MOB_GRIEFING), rule(GR_MOB_GRIEFING));+ e->type = E_NONE;+ return;+ }+ e->x = nx, e->y = ny, e->z = nz;+ bool water = blk_model[b] == M_LIQUID && !is_lava(b);+ float k = water ? 0.8f : 0.95f;+ e->vx = (e->vx + e->gx) * k, e->vy = (e->vy + ay) * k, e->vz = (e->vz + e->gz) * k;+}+/* the player hits a fireball: it goes the way they look (EntityFireball.attackEntityFrom) */+static void fireball_hit(Entity *e);+bool dims_punch(void) {+ float *o = look_ray, *d = look_ray + 3;+ for (int i = 0; i < N_ENT; i++) {+ Entity *e = &ents[i];+ if (e->type != E_FIREBALL) continue;+ float r = e->item.aux ? 0.5f : 1.0f, vx = e->x - o[0], vy = e->y - o[1], vz = e->z - o[2];+ float t = vx * d[0] + vy * d[1] + vz * d[2];+ if (t < 0 || t > (pl.mode ? 5 : 3) + r) continue;+ float cx = vx - d[0] * t, cy = vy - d[1] * t, cz = vz - d[2] * t;+ if (cx * cx + cy * cy + cz * cz > r * r) continue;+ fireball_hit(e);+ return true;+ }+ return false;+}+static void fireball_hit(Entity *e) {+ float yaw = pl.yaw * 0.017453292f, pitch = pl.pitch * 0.017453292f;+ float lx = -sinf(yaw) * cosf(pitch), ly = -sinf(pitch), lz = cosf(yaw) * cosf(pitch);+ e->vx = lx, e->vy = ly, e->vz = lz;+ e->gx = lx * 0.1f, e->gz = lz * 0.1f, e->limb = ly * 0.1f;+ e->state = 1;+}++/* EntityEnderEye: up to 12 blocks towards the stronghold, rising, then down onto it; after 80 ticks it+ * drops (four in five) or shatters */+static void eye_tick(Entity *e) {+ e->x += e->vx, e->y += e->vy, e->z += e->vz;+ float sp = sqrtf(e->vx * e->vx + e->vz * e->vz);+ float d0 = e->gx - e->x, d1 = e->gz - e->z, f1 = sqrtf(d0 * d0 + d1 * d1), f2 = atan2f(d1, d0);+ float d2 = sp + (f1 - sp) * 0.0025f;+ if (f1 < 1) d2 *= 0.8f, e->vy *= 0.8f;+ e->vx = cosf(f2) * d2, e->vz = sinf(f2) * d2;+ e->vy += ((e->y < e->limb ? 1.0f : -1.0f) - e->vy) * 0.015f; /* (limb: the height it makes for) */+ if (++e->timer > 80) {+ e->type = E_NONE;+ if (e->delay) ent_drop(I_ENDER_EYE, 1, 0, e->x, e->y, e->z, false);+ }+}+void throw_eye(int tx, int ty, int tz) {+ Entity *e = ent_new(E_EYE, pl.x, pl.y + 1.62f, pl.z);+ if (!e) return;+ e->item.id = I_ENDER_EYE;+ float d1 = tx - e->x, d2 = tz - e->z, f = sqrtf(d1 * d1 + d2 * d2);+ if (f > 12) e->gx = e->x + d1 / f * 12, e->gz = e->z + d2 / f * 12, e->limb = e->y + 8;+ else e->gx = (float)tx, e->gz = (float)tz, e->limb = (float)ty;+ e->delay = rnd(5) > 0;+}++/* ---------------------------------------------------------------- ghasts, blazes, magma cubes */+/* EntityGhast: flies about (EntityAIRandomFly: somewhere within 16, while the way is clear), faces the player+ * within 64 it can see, warns at 10 and shoots at 20, then waits 40 */+static void ghast_tick(Entity *e) {+ float gx = e->gx - e->x, gy = (float)e->tz - e->y, gz = e->gz - e->z, d = gx * gx + gy * gy + gz * gz;+ if (d < 1 || d > 3600 || e->panic <= 0) {+ e->gx = e->x + (rndf() * 2 - 1) * 16, e->tz = (int16_t)(e->y + (rndf() * 2 - 1) * 16), e->gz = e->z + (rndf() * 2 - 1) * 16;+ if (e->tz < 5) e->tz = 5;+ if (e->tz > 120) e->tz = 120;+ e->panic = 1;+ }+ /* GhastMoveHelper: a push towards it now and then, if the way there is free */+ if (--e->delay <= 0) {+ e->delay = (int16_t)(rnd(5) + 2);+ gx = e->gx - e->x, gy = (float)e->tz - e->y, gz = e->gz - e->z;+ float l = sqrtf(gx * gx + gy * gy + gz * gz);+ bool free = l > 0;+ for (int k = 1; k < l && free; k++) free = room_for(e->x + gx / l * k, e->y + gy / l * k, e->z + gz / l * k, 4, 4);+ if (free) e->vx += gx / l * 0.1f, e->vy += gy / l * 0.1f, e->vz += gz / l * 0.1f;+ else e->panic = 0;+ }+ float dx = pl.x - e->x, dy = pl.y + 0.9f - (e->y + 2), dz = pl.z - e->z, pd = dx * dx + dy * dy + dz * dz;+ bool target = !pl.dead && pl.mode == 0 && pd < 64 * 64;+ if (target) e->yaw = -atan2f(dx, dz) * 57.29578f;+ else e->yaw = -atan2f(e->vx, e->vz) * 57.29578f;+ if (target && sees(e)) {+ if (++e->timer == 20) {+ float yr = e->yaw * 0.017453292f, lx = -sinf(yr), lz = cosf(yr);+ float fx = e->x + lx * 4, fy = e->y + 2 + 0.5f, fz = e->z + lz * 4;+ fireball(e, fx, fy, fz, pl.x - (e->x + lx * 4), pl.y + 0.9f - (0.5f + e->y + 2), pl.z - (e->z + lz * 4), 0);+ e->timer = -40;+ }+ } else if (e->timer > 0)+ e->timer--;+ e->state = e->timer > 10;+ /* EntityFlying: no gravity, the air's friction */+ float p[3] = {e->x, e->y, e->z}, v[3] = {e->vx, e->vy, e->vz};+ phys_move(p, v, 4, 4);+ e->x = p[0], e->y = p[1], e->z = p[2];+ e->vx = v[0] * 0.91f, e->vy = v[1] * 0.91f, e->vz = v[2] * 0.91f;+}++/* EntityBlaze: falls slowly, rises to the player's eyes (and an offset that changes every 5 seconds);+ * close, it hits; within 16, it sets itself alight for 3 seconds then shoots three small fireballs, 6+ * ticks apart, then rests 5 seconds */+static void blaze_tick(Entity *e, bool chase, float dist) {+ if (e->delay > 0) e->delay--;+ if (--e->growth <= 0) e->growth = 100, e->love = (int16_t)((0.5f + rnd_gauss() * 3) * 100);+ if (chase && pl.y + 1.62f > e->y + 1.53f + e->love / 100.0f) e->vy += (0.3f - e->vy) * 0.3f;+ if (!e->on_ground && e->vy < 0) e->vy *= 0.6f;+ if (!chase) return;+ float dx = pl.x - e->x, dy = pl.y + 0.9f - (e->y + 0.9f), dz = pl.z - e->z, d2 = dx * dx + dy * dy + dz * dz;+ if (d2 < 4) {+ if (e->delay <= 0) e->delay = 20, player_hurt(scaled(6), DMG_MOB), push_player(dx, dz, 0.4f);+ } else if (d2 < 256) {+ if (e->delay <= 0) {+ e->timer++;+ if (e->timer == 1) e->delay = 60, e->fire = 60;+ else if (e->timer <= 4) e->delay = 6;+ else e->delay = 100, e->timer = 0;+ if (e->timer > 1) {+ float f = sqrtf(sqrtf(d2)) * 0.5f;+ fireball(e, e->x, e->y + 0.9f + 0.5f, e->z, dx + rnd_gauss() * f, dy, dz + rnd_gauss() * f, 1);+ }+ }+ }+ (void)dist;+}++/* EntityMagmaCube: hops towards the player (every 2 to 6 seconds, a third of it when after them), its+ * jump 0.42 + 0.1 a size; touching them hurts, its size + 2 */+static void magma_tick(Entity *e, bool chase, float dist) {+ int s = e->sheared ? e->sheared : 1;+ float dx = pl.x - e->x, dz = pl.z - e->z;+ if (chase) e->yaw = -atan2f(dx, dz) * 57.29578f;+ else if (rnd(80) == 0) e->yaw = rndf() * 360;+ if (e->on_ground) {+ e->vx *= 0.2f, e->vz *= 0.2f;+ if (--e->delay <= 0) {+ e->delay = (int16_t)((rnd(20) + 10) * 4);+ if (chase) e->delay /= 3;+ e->vy = 0.42f + s * 0.1f;+ float yr = e->yaw * 0.017453292f;+ e->vx = -sinf(yr) * 0.25f, e->vz = cosf(yr) * 0.25f;+ }+ }+ if (chase && dist < 0.6f * s + 0.6f && fabsf(pl.y - e->y) < s && e->timer <= 0) {+ player_hurt(scaled((float)s + 2), DMG_MOB);+ push_player(dx, dz, 0.4f);+ e->timer = 10;+ }+ if (e->timer > 0) e->timer--;+}++/* ---------------------------------------------------------------- endermen */+/* EntityEnderman.teleportTo: there, down onto the ground, if it fits and is dry */+static bool ender_tp(Entity *e, float x, float y, float z) {+ int bx = ifl(x), bz = ifl(z), by = ifl(y);+ if (!world_loaded(bx, by, bz)) return false;+ while (by > 0 && !(blk_flags[world_get(bx, by - 1, bz)] & BF_SOLID)) by--;+ if (by <= 0 || !room_for(x, (float)by, z, 0.6f, 2.9f) || blk_model[world_get(bx, by, bz)] == M_LIQUID) return false;+ for (int k = 0; k < 8; k++)+ particle_add(e->x + (rndf() - 0.5f) * 0.6f, e->y + rndf() * 2.9f, e->z + (rndf() - 0.5f) * 0.6f, 0, 0.05f, 0, 0xA01F, 20);+ e->x = x, e->y = (float)by, e->z = z, e->vx = e->vy = e->vz = 0;+ e->px = e->x, e->py = e->y, e->pz = e->z, e->gx = e->x, e->gz = e->z, e->tx = (int16_t)bx, e->tz = (int16_t)bz;+ return true;+}+static bool ender_tp_random(Entity *e) {+ return ender_tp(e, e->x + (rndf() - 0.5f) * 64, e->y + (float)(rnd(64) - 32), e->z + (rndf() - 0.5f) * 64);+}+/* shouldAttackPlayer: the player looks at its head, and sees it */+static bool stared_at(const Entity *e) {+ float yaw = pl.yaw * 0.017453292f, pitch = pl.pitch * 0.017453292f;+ float lx = -sinf(yaw) * cosf(pitch), ly = -sinf(pitch), lz = cosf(yaw) * cosf(pitch);+ float vx = e->x - pl.x, vy = e->y + 1.45f - (pl.y + 1.62f), vz = e->z - pl.z, d = sqrtf(vx * vx + vy * vy + vz * vz);+ if (d < 1e-3f || d > 64) return false;+ return (lx * vx + ly * vy + lz * vz) / d > 1 - 0.025f / d && sees(e);+}+/* what an enderman picks up (EntityEnderman's carriable blocks) */+static bool carriable(int b) {+ return b == B_GRASS || b == B_DIRT || b == B_SAND || b == B_GRAVEL || b == B_DANDELION || b == B_POPPY ||+ b == B_BROWN_MUSHROOM || b == B_RED_MUSHROOM || b == B_TNT || b == B_CACTUS || b == B_CLAY ||+ b == B_PUMPKIN || b == B_MELON || b == B_MYCELIUM;+}+static void ender_tick(Entity *e) {+ int bx = ifl(e->x), by = ifl(e->y), bz = ifl(e->z);+ /* water and rain hurt it, and it goes */+ if (is_water(world_get(bx, by, bz)) || is_water(world_get(bx, by + 1, bz)) || rain_at(bx, by + 2, bz)) {+ hurt(e, 1, 0, 0);+ e->panic = 0;+ ender_tp_random(e);+ }+ /* by day in the Overworld's sky, now and then it goes */+ if (dim == DIM_OVERWORLD && sky_sub() < 4 && light_there(bx, by + 1, bz) > 7 && rndf() * 30 < 1.2f) {+ e->panic = 0;+ ender_tp_random(e);+ }+ /* EntityAIFindPlayer: looked at for 5 ticks: it screams and comes; close and still looked at, it goes;+ * far and angry, it comes to the player */+ if (!pl.dead && pl.mode == 0 && opt.difficulty > 0) {+ if (stared_at(e)) {+ if (e->panic <= 0 && ++e->timer >= 5) e->panic = 600, e->timer = 0;+ else if (e->panic > 0 && (pl.x - e->x) * (pl.x - e->x) + (pl.z - e->z) * (pl.z - e->z) < 16) ender_tp_random(e);+ } else+ e->timer = 0;+ float d2 = (pl.x - e->x) * (pl.x - e->x) + (pl.z - e->z) * (pl.z - e->z);+ if (e->panic > 0 && d2 > 256 && rnd(30) == 0) {+ float a = rndf() * 6.2831853f;+ ender_tp(e, pl.x + cosf(a) * 4, pl.y + 4, pl.z + sinf(a) * 4);+ }+ }+ /* EntityEnderman.AITakeBlock and AIPlaceBlock (mobGriefing): love holds the block carried */+ if (!rule(GR_MOB_GRIEFING)) return;+ if (!e->love && rnd(20) == 0) {+ int x = ifl(e->x - 1 + rndf() * 2), y = ifl(e->y + rndf() * 2), z = ifl(e->z - 1 + rndf() * 2);+ int b = world_get(x, y, z);+ if (carriable(b) && world_loaded(x, y, z)) {+ world_set(x, y, z, B_AIR);+ neighbours_changed(x, y, z);+ e->love = (int16_t)b;+ }+ } else if (e->love && rnd(2000) == 0) {+ int x = ifl(e->x - 1 + rndf() * 2), y = ifl(e->y + rndf() * 2), z = ifl(e->z - 1 + rndf() * 2);+ int below = world_get(x, y - 1, z);+ if (world_get(x, y, z) == B_AIR && below != B_AIR && (blk_flags[below] & BF_OPAQUE) && world_loaded(x, y, z)) {+ world_set(x, y, z, e->love);+ e->love = 0;+ }+ }+}++/* the dimensions' creatures each tick: the ones that are all theirs (true), or what the others do before+ * the zombie's AI runs */+void dragon_tick(Entity *e); /* (end.c) */+static bool dims_tick(Entity *e) {+ if (e->type == E_FIREBALL) {+ fireball_tick(e);+ return true;+ }+ if (e->type == E_EYE) {+ eye_tick(e);+ return true;+ }+ if (e->type == E_DRAGON) {+ dragon_tick(e);+ return true;+ }+ if (e->type == E_CRYSTAL) {+ /* EntityEnderCrystal: fire where it is, in the End */+ if (e->hurt > 0) e->hurt--;+ if (dim == DIM_END && world_get(ifl(e->x), ifl(e->y), ifl(e->z)) == B_AIR) world_set(ifl(e->x), ifl(e->y), ifl(e->z), B_FIRE);+ return true;+ }+ if (e->type == E_PIGMAN && e->panic > 0) e->panic--;+ if (e->type == E_ENDERMAN && e->state != 255) ender_tick(e);+ if (e->type == E_GHAST && e->state != 255) {+ if (e->hurt > 0) e->hurt--;+ if (e->invuln > 0) e->invuln--;+ ghast_tick(e);+ return true;+ }+ return false;+}++/* the dimensions' creatures hurt (after their health went down) */+void dragon_hurt(Entity *e, float dmg); /* (end.c) */+void crystal_gone(Entity *e); /* (end.c) */+static void dims_hurt(Entity *e, float kx, float kz) {+ if (e->type == E_PIGMAN && (kx != 0 || kz != 0)) {+ /* EntityPigZombie.becomeAngryAt: every pigman within 32 */+ for (int i = 0; i < N_ENT; i++) {+ Entity *o = &ents[i];+ if (o->type == E_PIGMAN && fabsf(o->x - e->x) < 32 && fabsf(o->y - e->y) < 32 && fabsf(o->z - e->z) < 32)+ o->panic = (int16_t)(400 + rnd(400));+ }+ }+ if (e->type == E_ENDERMAN && (kx != 0 || kz != 0)) e->panic = 600;+}diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex 6905e909..4ddb1091 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -200,6 +200,18 @@ void gui_message(const char *s); /* a line at the bottom left, as Minecraft's/* ---------------------------------------------------------------- entities (entity.c) */enum { E_NONE, E_ITEM, E_ZOMBIE, E_SKELETON, E_CREEPER, E_SPIDER, E_PIG, E_COW, E_SHEEP, E_CHICKEN, E_ARROW, E_TNT,E_BOBBER };+#if NB_DIMS+/* NumBlocks.nwa's: the Nether's and the End's creatures (the ender crystal and the dragon count as mobs: they+ * are hit), then what flies (fireballs, eyes of ender: their item in item) */+enum { E_PIGMAN = E_BOBBER + 1, E_GHAST, E_BLAZE, E_MAGMA, E_WSKELETON, E_ENDERMAN, E_CRYSTAL, E_DRAGON, E_FIREBALL, E_EYE };+#define IS_MOB(t) (((t) >= E_ZOMBIE && (t) <= E_CHICKEN) || ((t) >= E_PIGMAN && (t) <= E_DRAGON))+#define NOT_MOB(t) (!IS_MOB(t))+#define IS_MONSTER(t) (((t) >= E_ZOMBIE && (t) <= E_SPIDER) || ((t) >= E_PIGMAN && (t) <= E_ENDERMAN))+#else+#define IS_MOB(t) ((t) >= E_ZOMBIE && (t) <= E_CHICKEN)+#define NOT_MOB(t) ((t) < E_ZOMBIE || (t) > E_CHICKEN)+#define IS_MONSTER(t) ((t) >= E_ZOMBIE && (t) <= E_SPIDER)+#endiftypedef struct {uint8_t type, on_ground, hurt, state; /* hurt: ticks of red; state 255: dying (timer counts) */int16_t age, health, timer, delay; /* delay: an item's pickup delay, a mob's attack wait, a creeper's fuse */diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.cindex 6cadcc90..1679d15e 100644--- a/games/numblocks/src/player.c+++ b/games/numblocks/src/player.c@@ -563,7 +563,7 @@ static void sleep_in(int x, int y, int z) {}for (int i = 0; i < N_ENT; i++) {const Entity *e = &ents[i];- if (e->type >= E_ZOMBIE && e->type <= E_SPIDER && fabsf(e->x - x) < 8 && fabsf(e->y - y) < 5 &&+ if (IS_MONSTER(e->type) && fabsf(e->x - x) < 8 && fabsf(e->y - y) < 5 &&fabsf(e->z - z) < 8) {gui_message(T("You may not rest now, there are monsters nearby"));return;@@ -944,6 +944,9 @@ void player_tick(uint32_t keys, uint32_t pressed) {if (pl.mode == 0) food_tick();if (pl.dead) return;pick();+#if NB_DIMS+ if ((pressed & K_ATTACK) && dims_punch()) player_swing(), pressed &= ~K_ATTACK, keys &= ~K_ATTACK;+#endif/* a mob under the crosshair, nearer than the block: attacking hits it, using uses on it */Entity *target = entity_looked_at(pl.mode ? 5.0f : 3.0f, pl.hit_face >= 0 ? hit_t : 1e9f);if (target) {diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.cindex ba8a64d9..18d27084 100644--- a/games/numblocks/src/render.c+++ b/games/numblocks/src/render.c@@ -1196,13 +1196,22 @@ static void ents_frame(void) {ez = e->pz + (e->z - e->pz) * t - vc_z0;float dx = ex - ox, dy = ey - oy, dz = ez - oz;float dist = sqrtf(dx * dx + dy * dy + dz * dz);+#if NB_DIMS+ if (dist > MAX_T + (e->type == E_DRAGON ? 12 : 1)) continue;+#elseif (dist > MAX_T + 1) continue;+#endiffloat w, h;+#if NB_DIMS+ if (e->type == E_FIREBALL || e->type == E_EYE) w = h = 1.2f, ey -= 0.4f;+ else if (e->type == E_DRAGON) w = 24, h = 14, ey -= 4;+ else+#endifif (e->type == E_ITEM) w = 0.5f, h = 0.6f;else if (e->type == E_TNT) w = 1.2f, h = 1.1f;else if (e->type == E_ARROW || e->type == E_BOBBER) w = 0.6f, h = 0.6f, ey -= 0.3f;else w = mob_width(e->type) * 2.2f, h = mob_height(e->type) * 1.2f;- if (e->type >= E_ZOMBIE && e->type <= E_CHICKEN && e->state == 255) w = h = 2.4f; /* dying: lying down */+ if (IS_MOB(e->type) && e->state == 255) w = h = 2.4f; /* dying: lying down */float c[8][3];for (int k = 0; k < 8; k++)c[k][0] = ex + ((k & 1) ? w : -w) / 2, c[k][1] = ey + ((k & 2) ? h : 0), c[k][2] = ez + ((k & 4) ? w : -w) / 2;@@ -1238,9 +1247,14 @@ static void rot(float *m, int axis, float a) { /* m = rotation about the axis}/* M: model frame to world (Ry (180 - yaw) . death turn . S(-1,-1,1) . scale) */+#if NB_DIMS+static int make_boxes_in(const Draw *dr, Box *bx, Part *parts) {+ const Entity *e = dr->e;+#elsestatic int make_boxes(const Draw *dr, Box *bx) {const Entity *e = dr->e;Part parts[MAX_PARTS];+#endifint n = mob_parts(e, tick_frac, parts);float yaw = e->yaw * 0.017453292f;float M[9], R[9], Z[9];@@ -1310,6 +1324,12 @@ static int make_boxes(const Draw *dr, Box *bx) {(void)light_sh;return n;}+#if NB_DIMS+static int make_boxes(const Draw *dr, Box *bx) {+ Part parts[MAX_PARTS];+ return make_boxes_in(dr, bx, parts);+}+#endif/* the skin texel the ray meets on a box face (ModelBox's layout), -1 if see-through */static int box_texel(const Box *b, int face, float x, float y, float z) {@@ -1335,6 +1355,21 @@ static int box_texel(const Box *b, int face, float x, float y, float z) {case 4: u = U + d + lx, v = V + d + ly; break; /* z0 (the front) */default: u = U + d + w + d + (w - 1 - lx), v = V + d + ly; break; /* z1 (the back) */}+#if NB_DIMS+ if (b->skin == SKIN_DRAGON) { /* (256 x 256; its wings' skin starts at u -56: wrapped) */+ u &= 255;+ if ((unsigned)v >= 256) return -1;+ uint8_t q = skin_dragon_px[(v * 256 + u) >> 1];+ int k = (u & 1) ? q >> 4 : q & 15;+ return k ? skin_dragon_pal[k] : -1;+ }+ if (b->skin == SKIN_CRYSTAL) { /* (its 64 x 32 drawn at 128 x 64) */+ if ((unsigned)u >= 64 || (unsigned)v >= 32) return -1;+ uint8_t q = skin_crystal_px[(v * 2 * 128 + u * 2) >> 1];+ int k = q & 15;+ return k ? skin_crystal_pal[k] : -1;+ }+#endifif ((unsigned)u >= 64 || (unsigned)v >= 32) return -1;uint8_t p = skin_px[b->skin][(v * 64 + u) >> 1];int i = (u & 1) ? p >> 4 : p & 15;@@ -1441,12 +1476,19 @@ static bool item_hit(const Draw *dr, const float *d, float *best_t, uint16_t *co/* a flat picture half a block wide, facing the camera */int ic = e->type == E_BOBBER ? SP_BOBBER : item_icon(id);if (ic < 0) return false;+#if NB_DIMS+ /* RenderFireball: the fire charge, 2 across (a ghast's) or a half (a blaze's); the eye of ender as an item */+ float half = e->type == E_FIREBALL ? (e->item.aux ? 0.125f : 0.5f) : 0.25f;+ if (e->type == E_FIREBALL) cy = dr->ey;+#else+ const float half = 0.25f;+#endiffloat rel[3] = {cx - ox, cy + 0.125f - oy, cz - oz};float den = d[0] * fwx + d[1] * fwy + d[2] * fwz;float t = (rel[0] * fwx + rel[1] * fwy + rel[2] * fwz) / den;if (!(t > 0) || t >= *best_t) return false;float hx = d[0] * t - rel[0], hy = d[1] * t - rel[1], hz = d[2] * t - rel[2];- float u = (hx * rgx + hz * rgz) / 0.5f + 0.5f, v = 0.5f - (hx * upx + hy * upy + hz * upz) / 0.5f;+ float u = (hx * rgx + hz * rgz) / (2 * half) + 0.5f, v = 0.5f - (hx * upx + hy * upy + hz * upz) / (2 * half);if (u < 0 || u >= 1 || v < 0 || v >= 1) return false;int w = spr_w[ic], hh = spr_h[ic], pxu = (int)(u * w), pyv = (int)(v * hh);const uint8_t *px = spr_px + spr_off[ic] + pyv * ((w + 1) / 2);@@ -1851,14 +1893,31 @@ static void select_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW])}static void ents_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {+#if NB_DIMS+ Box bxs[MAX_PARTS];+#elseBox bx[MAX_PARTS];+#endifparticles_strip(py0, rows, cb, zb);for (int k = 0; k < ndraws; k++) {const Draw *dr = &draws[k];if (dr->y1 <= py0 || dr->y0 >= py0 + rows) continue;const Entity *e = dr->e;- bool mob = e->type >= E_ZOMBIE && e->type <= E_CHICKEN;+ bool mob = IS_MOB(e->type);+#if NB_DIMS+ /* the dragon's many parts: in the room the rain has (none falls in the End) */+ Box *bx = bxs;+ int n = 0;+ if (e->type == E_DRAGON) {+ Part *parts = (Part *)world_scratch(MAX_PARTS_DRAGON * (sizeof(Part) + sizeof(Box)));+ if (!parts) continue;+ bx = (Box *)(parts + MAX_PARTS_DRAGON);+ n = make_boxes_in(dr, bx, parts);+ } else if (mob)+ n = make_boxes(dr, bx);+#elseint n = mob ? make_boxes(dr, bx) : 0;+#endif/* hurt (and dying): a red tint; a creeper about to blow flashes white */int red = mob && (e->hurt > 0 || e->state == 255) ? 10 : 0;int white = e->type == E_CREEPER && e->delay > 0 && ((e->delay / 3) & 1) ? 16 : 0;diff --git a/games/numblocks/src/tick.c b/games/numblocks/src/tick.cindex 51480906..bb45a664 100644--- a/games/numblocks/src/tick.c+++ b/games/numblocks/src/tick.c@@ -500,7 +500,7 @@ static void bolt_strikes(void) {}for (int i = 0; i < N_ENT; i++) {Entity *e = &ents[i];- if (e->type >= E_ZOMBIE && e->type <= E_CHICKEN && fabsf(e->x - bolt.x) < 3.3f && fabsf(e->z - bolt.z) < 3.3f &&+ if (IS_MOB(e->type) && fabsf(e->x - bolt.x) < 3.3f && fabsf(e->z - bolt.z) < 3.3f &&e->y > bolt.y - 4 && e->y < bolt.y + 9)mob_struck(e);}diff --git a/games/numblocks/tests/play.c b/games/numblocks/tests/play.cindex 210afb8d..2cde646e 100644--- a/games/numblocks/tests/play.c+++ b/games/numblocks/tests/play.c@@ -192,6 +192,17 @@ int main(int argc, char **argv) {Entity *e = ent_new(E_ZOMBIE + k, x, (float)fy, z);if (e) e->health = 20, e->yaw = 180, e->gx = e->x, e->gz = e->z;}+ if (dim) {+ /* the Nether's and the End's instead */+ memset(ents, 0, sizeof ents);+ static const int8_t t[6] = {E_PIGMAN, E_WSKELETON, E_BLAZE, E_MAGMA, E_ENDERMAN, E_CRYSTAL};+ for (int k = 0; k < 6; k++) {+ Entity *e = ent_new(t[k], pl.x - 5.0f + k * 2.0f, (float)fy, pl.z + 5.5f);+ if (e) e->health = 20, e->yaw = 180, e->gx = e->x, e->gz = e->z, e->sheared = 2;+ }+ Entity *g = ent_new(E_GHAST, pl.x, (float)fy + 2, pl.z + 14);+ if (g) g->health = 10, g->yaw = 180, g->delay = 1000;+ }}if (animals) {/* a few animals ahead of the player (forward f, right r blocks), each on the first solid ground under it, facing@@ -241,6 +252,9 @@ int main(int argc, char **argv) {s = c + 1;}}+ for (int i = 0; i < N_ENT; i++)+ if (ents[i].type > E_BOBBER)+ printf("entity %d at %.1f %.1f %.1f health %d\n", ents[i].type, ents[i].x, ents[i].y, ents[i].z, ents[i].health);extern unsigned long st_steps, st_texels, st_jumps, st_pixels;printf("per frame: %.0f traces, %.0f steps\n", (double)st_pixels / frames, (double)st_steps / frames);extern unsigned long st_dis, st_k0, st_fpfail, st_fpok, st_sky;