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: breeding
Mason Chend020bc40494c
diff --git a/games/numblocks/src/mob.c b/games/numblocks/src/mob.cindex 628790c7..bee91a31 100644--- a/games/numblocks/src/mob.c+++ b/games/numblocks/src/mob.c@@ -27,6 +27,7 @@ float mob_height(int t) { return t >= E_ZOMBIE && t <= E_CHICKEN ? info[t].h : 0static bool hostile(int t) { return t >= E_ZOMBIE && t <= E_SPIDER; }float mob_scale(const Entity *e, float pt) {+ if (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 */float f = (e->delay + pt) / 28.0f;@@ -256,6 +257,7 @@ static void drop(int id, int n, const Entity *e) {if (n > 0) ent_drop(id, n, 0, e->x, e->y + 0.5f, e->z, false);}static void loot(Entity *e) {+ if (e->growth < 0) return; /* babies drop nothing */switch (e->type) {case E_ZOMBIE: drop(I_ROTTEN_FLESH, rnd(3), e); break;case E_SKELETON:@@ -356,8 +358,30 @@ bool mob_attack(const Entity *ce) {return true;}+/* what each animal is fed to breed (EntityAnimal.isBreedingItem) */+static int food_of(int type) {+ switch (type) {+ case E_COW: case E_SHEEP: return I_WHEAT;+ case E_PIG: return I_CARROT;+ case E_CHICKEN: return I_WHEAT_SEEDS;+ }+ return 0;+}+bool mob_use(Entity *e) {Stack *h = held();+ if (h->id && h->id == food_of(e->type) && e->state != 255 && e->growth >= 0 && !e->love && e->timer <= 0) {+ /* fed: in love for 30 seconds */+ e->love = 600;+ if (pl.mode == 0) stack_take(h, 1);+ return true;+ }+ if (h->id && h->id == food_of(e->type) && e->growth < 0) {+ /* a baby fed grows up a tenth faster */+ e->growth = (int16_t)(e->growth / 10 * 9);+ if (pl.mode == 0) stack_take(h, 1);+ return true;+ }if (e->type == E_SHEEP && h->id == I_SHEARS && !e->sheared && e->state != 255) {e->sheared = 1;drop(B_WOOL_WHITE, 1 + rnd(3), e);@@ -587,6 +611,27 @@ void mob_tick(Entity *e) {e->fire--;}if (e->panic > 0) e->panic--;+ if (e->growth < 0) e->growth++;+ if (e->timer > 0 && !hostile(e->type) && e->state != 255) e->timer--; /* (animals: time before breeding again) */+ if (e->love > 0) {+ e->love--;+ /* EntityAIMate: to the nearest other one in love; close together for 3 seconds: a baby */+ for (int i = 0; i < N_ENT; i++) {+ Entity *o = &ents[i];+ if (o == e || o->type != e->type || o->love <= 0 || o->state == 255) continue;+ float mx = o->x - e->x, mz = o->z - e->z;+ if (mx * mx + mz * mz > 64) continue;+ e->gx = o->x, e->gz = o->z;+ if (mx * mx + mz * mz < 2.5f && ++e->item.aux >= 60) {+ Entity *b = spawn(e->type, e->x, e->y, e->z);+ if (b) b->growth = -24000;+ e->love = o->love = 0, e->item.aux = o->item.aux = 0;+ e->timer = o->timer = 6000;+ player_add_xp(1 + rnd(7));+ }+ break;+ }+ }if (e->delay > 0 && e->type != E_CREEPER) e->delay--;/* where to go */bool chase = false, go = false;@@ -616,8 +661,8 @@ void mob_tick(Entity *e) {go = true;speed *= e->type == E_COW ? 2.0f : e->type == E_CHICKEN ? 1.4f : 1.25f;} else {- /* EntityAIWander: now and then, somewhere within 10 blocks */- if (rnd(120) == 0) e->gx = e->x + rnd(21) - 10, e->gz = e->z + rnd(21) - 10;+ /* EntityAIWander: now and then, somewhere within 10 blocks (in love: to the other one) */+ if (rnd(120) == 0 && !e->love) e->gx = e->x + rnd(21) - 10, e->gz = e->z + rnd(21) - 10;float gx = e->gx - e->x, gz = e->gz - e->z;go = gx * gx + gz * gz > 1;}diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex 3dc84e5c..311acbce 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -156,6 +156,7 @@ typedef struct {float limb, limb_amt; /* the walk's swing */float gx, gz; /* where it is going */int16_t panic, fire;+ int16_t love, growth; /* animals: in love (ticks), a baby's growing up (negative: ticks left) */uint8_t invuln, sheared;Stack item;} Entity;diff --git a/games/numblocks/tests/gen/arm/bench.c b/games/numblocks/tests/gen/arm/bench.cindex d26d3be8..5ccab075 100644--- a/games/numblocks/tests/gen/arm/bench.c+++ b/games/numblocks/tests/gen/arm/bench.c@@ -1,34 +1,47 @@/* Bare-metal Cortex-M7 benchmark of the generator, run by bench.py in Unicorn.- * phase is bumped after each step so the runner can count instructions per step. */+ * It follows the game's pattern: 24-high slabs (y0 a multiple of 4), a 4 x 3 chunk area at+ * world load, then moves that add a row of 3 or 4 neighbouring chunks.+ * phase is bumped after each call so the runner can count instructions per call. */#include <stdint.h>#include <string.h>#include "../../../src/gen.h"+#ifndef Y0+#define Y0 56+#endif+#ifndef H+#define H 24+#endif+volatile uint32_t phase;volatile uint32_t sums[64];volatile int32_t spawn[3];-static uint8_t out[128 * 256];+static uint8_t out[H * 256];-#ifndef NSIDE-#define NSIDE 4-#endif+static int k;+static void slab(int cx, int cz) {+ gen_slab(cx, cz, Y0, H, out);+ uint32_t s = 0;+ for (unsigned i = 0; i < sizeof out; i++) s = s * 31 + out[i];+ for (int i = 0; i < 256; i++) s = s * 31 + (uint32_t)gen_top(cx * 16 + (i & 15), cz * 16 + (i >> 4));+ if (k < 64) sums[k] = s;+ k++;+ phase = phase + 1;+}int main(void) {gen_init(12345);phase = 1;- int k = 0;- for (int cz = -2; cz < -2 + NSIDE; cz++)- for (int cx = -2; cx < -2 + NSIDE; cx++) {- gen_slab(cx, cz, 0, 128, out);- uint32_t s = 0;- for (unsigned i = 0; i < sizeof out; i++) s = s * 31 + out[i];- sums[k++] = s;- phase = phase + 1;- }+ /* world load: 4 x 3 chunks */+ for (int cz = -2; cz <= 0; cz++)+ for (int cx = -2; cx <= 1; cx++) slab(cx, cz);+ /* walking +x: rows of 3 chunks */+ for (int cx = 2; cx <= 4; cx++)+ for (int cz = -2; cz <= 0; cz++) slab(cx, cz);+ /* walking +z: rows of 4 chunks */+ for (int cz = 1; cz <= 2; cz++)+ for (int cx = 1; cx <= 4; cx++) slab(cx, cz);#ifndef NO_SPAWN- /* a 32-high slab as the engine asks */- gen_slab(5, 5, 32, 32, out);- phase = phase + 1;int x, y, z;gen_spawn(&x, &y, &z);spawn[0] = x, spawn[1] = y, spawn[2] = z;diff --git a/games/numblocks/tests/gen/arm/bench.py b/games/numblocks/tests/gen/arm/bench.pyindex 6e99126d..15166171 100644--- a/games/numblocks/tests/gen/arm/bench.py+++ b/games/numblocks/tests/gen/arm/bench.py@@ -1,8 +1,9 @@#!/usr/bin/env python3"""Builds bench.c + gen.c for the Cortex-M7 (-Os, hard float) and runs it in Unicorn,counting instructions per gen_slab call (1 instruction ~ 1 cycle at 216 MHz, the same-convention as tools/emu.py). Also reports the deepest stack use (painted stack).-Usage: bench.py [OUTDIR] [--side N] [--no-spawn] [--profile]"""+convention as tools/emu.py) in the game's calling pattern (see bench.c). Also reports the+deepest stack use (painted stack) and gen.c's own static RAM.+Usage: bench.py [OUTDIR] [--no-spawn] [--profile] [--y0 N] [--h N]"""import osimport subprocessimport sys@@ -15,9 +16,17 @@ HERE = os.path.dirname(os.path.abspath(__file__))SRC = os.path.join(HERE, "..", "..", "..", "src")args = [a for a in sys.argv[1:] if not a.startswith("--")]OUT = args[0] if args else "/tmp"-SIDE = int(sys.argv[sys.argv.index("--side") + 1]) if "--side" in sys.argv else 4-if "--side" in sys.argv:- args = [a for a in args if a != str(SIDE)]+def opt(name, default):+ global args+ if name not in sys.argv:+ return default+ v = sys.argv[sys.argv.index(name) + 1]+ args = [a for a in args if a != v]+ return int(v)+++Y0 = opt("--y0", 56)+H = opt("--h", 24)NO_SPAWN = "--no-spawn" in sys.argvPROFILE = "--profile" in sys.argvELF = os.path.join(OUT, "gen_bench.elf")@@ -26,8 +35,11 @@ CFLAGS = ["-mcpu=cortex-m7", "-mfpu=fpv5-sp-d16", "-mfloat-abi=hard", "-mthumb",subprocess.check_call(["arm-none-eabi-gcc", *CFLAGS, "-nostartfiles", "--specs=nano.specs", "--specs=nosys.specs","-T", os.path.join(HERE, "bench.ld"), "-Wl,--gc-sections",os.path.join(HERE, "bench.c"), os.path.join(SRC, "gen.c"), "-lm", "-o", ELF,- f"-DNSIDE={SIDE}"] + (["-DNO_SPAWN"] if NO_SPAWN else []))-subprocess.call(["arm-none-eabi-size", ELF])+ f"-DY0={Y0}", f"-DH={H}"] + (["-DNO_SPAWN"] if NO_SPAWN else []))+OBJ = os.path.join(OUT, "gen_arm.o")+subprocess.check_call(["arm-none-eabi-gcc", *CFLAGS, "-c", os.path.join(SRC, "gen.c"), "-o", OBJ])+print("gen.c alone:")+subprocess.call(["arm-none-eabi-size", OBJ])syms = {}funcs = []@@ -87,17 +99,20 @@ while True:breakprev = 0-names = ["gen_init"] + [f"gen_slab 0..128 #{i}" for i in range(SIDE * SIDE)] + ["gen_slab 32..64", "gen_spawn"]+NCALLS = 12 + 9 + 8+names = ["gen_init"] + [f"gen_slab {Y0}..{Y0 + H} #{i}" for i in range(NCALLS)] + ["gen_spawn"]slabs = []for i, (ph, n) in enumerate(marks):d = n - prevprev = nname = names[i] if i < len(names) else f"phase {ph}"- if name.startswith("gen_slab 0"):+ if name.startswith("gen_slab"):slabs.append(d)print(f"{name:22s} {d / 1e6:8.2f} M insns ~{d / 216e3:7.1f} ms at 216 MHz")if slabs:- print(f"gen_slab 0..128: avg {sum(slabs) / len(slabs) / 216e3:.1f} ms, max {max(slabs) / 216e3:.1f} ms")+ print(f"gen_slab (+ 256 gen_top): avg {sum(slabs) / len(slabs) / 1e6:.2f} M insns = "+ f"{sum(slabs) / len(slabs) / 216e3:.1f} ms; load (12 calls) avg {sum(slabs[:12]) / 12 / 216e3:.1f} ms, "+ f"moves (17 calls) avg {sum(slabs[12:]) / max(1, len(slabs) - 12) / 216e3:.1f} ms, max {max(slabs) / 216e3:.1f} ms")st = uc.mem_read(STACK_TOP - PAINT, PAINT)used = PAINT - next(i for i in range(PAINT) if st[i] != 0xA5)print(f"deepest stack: {used} bytes")diff --git a/games/numblocks/tests/gen/t_conv.c b/games/numblocks/tests/gen/t_conv.cnew file mode 100644index 00000000..8e59ceef--- /dev/null+++ b/games/numblocks/tests/gen/t_conv.c@@ -0,0 +1,40 @@+/* checks gen.c's integer-only float <-> int64 conversions against the C casts */+#include "../../src/gen.c"+#include <stdio.h>++int main(void) {+ JRand r;+ jr_seed(&r, 99);+ long bad = 0, n = 0;+ for (int i = 0; i < 20000000; i++) {+ /* random int64 of random magnitude, both signs, plus near-ties */+ int bits = jr_int(&r, 63) + 1;+ i64 v = jr_long(&r) >> (64 - bits);+ if (i % 7 == 0) v = (v | 1) << jr_int(&r, 8);+ for (int k = 0; k <= 53; k += 53 - 21) {+ float a = (float)v * (k == 32 ? (1.0f / 4294967296.0f) : k == 53 ? (1.0f / 9007199254740992.0f) : 1.0f);+ float b = i64_to_f_scaled(v, k);+ if (a != b) {+ if (bad < 10) printf("i2f %lld k %d: %.9g vs %.9g\n", (long long)v, k, a, b);+ bad++;+ }+ n++;+ }+ float f;+ u32 u = (u32)jr_next(&r, 32);+ memcpy(&f, &u, 4);+ if (!(f == f)) continue;+ for (int k = 0; k <= 32; k += 32) {+ float sc = f * (k ? 4294967296.0f : 1.0f);+ if (!(sc > -4.6e18f && sc < 4.6e18f)) continue;+ i64 a = (i64)sc, b = f2i64_scaled(f, k);+ if (a != b) {+ if (bad < 10) printf("f2i %.9g k %d: %lld vs %lld\n", f, k, (long long)a, (long long)b);+ bad++;+ }+ n++;+ }+ }+ printf("%ld / %ld mismatches\n", bad, n);+ return bad != 0;+}diff --git a/games/numblocks/tests/gen/t_slabs.c b/games/numblocks/tests/gen/t_slabs.cnew file mode 100644index 00000000..c5d9d5fa--- /dev/null+++ b/games/numblocks/tests/gen/t_slabs.c@@ -0,0 +1,32 @@+/* Consistency: gen_slab(0..128) against the same chunk built from four 32-high slabs+ * (the engine asks for 32-high slabs). Prints the differing blocks per kind of difference. */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include "../../src/blocks.h"+#include "../../src/gen.h"++int main(int argc, char **argv) {+ int64_t seed = argc > 1 ? strtoll(argv[1], 0, 10) : 12345;+ int n = argc > 2 ? atoi(argv[2]) : 10;+ static uint8_t full[128 * 256], part[128 * 256];+ gen_init(seed);+ long diff = 0, chunks = 0;+ for (int cz = -n / 2; cz < n - n / 2; cz++)+ for (int cx = -n / 2; cx < n - n / 2; cx++) {+ gen_slab(cx, cz, 0, 128, full);+ for (int s = 0; s < 4; s++) gen_slab(cx, cz, s * 32, 32, part + s * 32 * 256);+ int d = 0;+ for (int i = 0; i < 128 * 256; i++)+ if (full[i] != part[i]) {+ if (diff + d < 12)+ printf("chunk %d %d: x %d y %d z %d full %d slabs %d\n", cx, cz, i & 15, i >> 8, (i >> 4) & 15,+ full[i], part[i]);+ d++;+ }+ diff += d;+ chunks += d > 0;+ }+ printf("seed %lld: %ld blocks differ in %ld of %d chunks\n", (long long)seed, diff, chunks, n * n);+ return 0;+}diff --git a/games/numblocks/tests/gen/t_sum.c b/games/numblocks/tests/gen/t_sum.cnew file mode 100644index 00000000..1a41b406--- /dev/null+++ b/games/numblocks/tests/gen/t_sum.c@@ -0,0 +1,46 @@+/* Checksums of gen_slab outputs (full height and 32-high slabs, in the engine's+ * row order and in a scattered order) and of gen_top / gen_biome, for regression checks:+ * t_sum SEED [N] prints one line per chunk and a final combined hash */+#include <stdio.h>+#include <stdlib.h>+#include "../../src/gen.h"++static uint32_t hash(const uint8_t *p, int n) {+ uint32_t h = 2166136261u;+ for (int i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;+ return h;+}++int main(int argc, char **argv) {+ int64_t seed = argc > 1 ? strtoll(argv[1], 0, 10) : 12345;+ int n = argc > 2 ? atoi(argv[2]) : 6;+ int verbose = argc > 3;+ static uint8_t out[128 * 256];+ gen_init(seed);+ uint32_t all = 0;+ for (int cz = -n / 2; cz < n - n / 2; cz++)+ for (int cx = -n / 2; cx < n - n / 2; cx++) {+ gen_slab(cx, cz, 0, 128, out);+ uint32_t h = hash(out, sizeof out);+ for (int i = 0; i < 256; i++) {+ uint8_t t = (uint8_t)gen_top(cx * 16 + (i & 15), cz * 16 + (i >> 4));+ uint8_t b = (uint8_t)gen_biome(cx * 16 + (i & 15), cz * 16 + (i >> 4));+ h = (h ^ t) * 16777619u;+ h = (h ^ b) * 16777619u;+ }+ if (verbose) printf("%d %d %08x\n", cx, cz, h);+ all = all * 31 + h;+ }+ /* 32-high slabs in a scattered order (cache misses) */+ for (int k = 0; k < n * 2; k++) {+ int cx = (k * 7) % (n + 3) - 4, cz = (k * 5) % (n + 2) - 3, y0 = (k % 4) * 32;+ gen_slab(cx, cz, y0, 32, out);+ uint32_t h = hash(out, 32 * 256);+ if (verbose) printf("slab %d %d %d %08x\n", cx, cz, y0, h);+ all = all * 31 + h;+ }+ int x, y, z;+ gen_spawn(&x, &y, &z);+ printf("seed %lld: hash %08x spawn %d %d %d\n", (long long)seed, all, x, y, z);+ return 0;+}diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.cindex fd0b9173..c57811a2 100644--- a/games/numblocks/tests/unit.c+++ b/games/numblocks/tests/unit.c@@ -125,6 +125,30 @@ int main(void) {CHECK(world_get(5, y, 4) == B_OBSIDIAN);plat_remove_prefix("ut2");}+ /* two cows fed wheat make a calf (EntityAIMate) */+ {+ extern uint32_t game_time;+ world_new(0, "ut3");+ world_follow(8, 70, 8);+ int y = 70;+ for (int z = 0; z <= 16; z++)+ for (int x = 0; x <= 16; x++) {+ world_set(x, y - 1, z, B_STONE);+ for (int k = 0; k < 3; k++) world_set(x, y + k, z, B_AIR);+ }+ memset(ents, 0, sizeof ents);+ opt.difficulty = 0; /* (no monsters) */+ Entity *a = ent_new(E_COW, 6.5f, (float)y, 8.5f), *b = ent_new(E_COW, 9.5f, (float)y, 8.5f);+ a->health = b->health = 10;+ pl.inv[0].id = I_WHEAT, pl.inv[0].aux = 5, pl.slot = 0, pl.mode = 0;+ CHECK(mob_use(a) && mob_use(b) && pl.inv[0].aux == 3);+ int calves = 0;+ for (int t = 0; t < 400; t++) ents_tick(), game_time++;+ for (int i = 0; i < N_ENT; i++)+ if (ents[i].type == E_COW && ents[i].growth < 0) calves++;+ CHECK(calves == 1);+ plat_remove_prefix("ut3");+ }printf("%s (%d recipes)\n", fails ? "FAILED" : "all good", N_RECIPES);return fails != 0;}