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
The Binding of Isaac: engine, floors, monsters, bosses and items
Mason36324e2704d59de
diff --git a/games/isaac/.gitignore b/games/isaac/.gitignorenew file mode 100644index 00000000..342d9dda--- /dev/null+++ b/games/isaac/.gitignore@@ -0,0 +1,2 @@+output/+build/diff --git a/games/isaac/Makefile b/games/isaac/Makefilenew file mode 100644index 00000000..5dd8e6b4--- /dev/null+++ b/games/isaac/Makefile@@ -0,0 +1,79 @@+# The Binding of Isaac on the NumWorks calculator.+# make output/isaac.nwa+# make check link it like the calculator does (output/isaac.bin)+# make host build/play: the game on a computer, for tests and screenshots+# make test the game played on a computer (tests/check.py, with the sanitizers)+# make data src/data.bin and src/data.h from the public material tools/fetch.py gathers+# (REF=its folder, Python + Pillow)+Q ?= @+# the ARM toolchain on the PATH, or in TOOLCHAIN=<its bin folder>+TOOLCHAIN ?=+BIN = $(if $(TOOLCHAIN),$(TOOLCHAIN)/)+CC = $(BIN)arm-none-eabi-gcc+NWLINK ?= npx --yes -- [email protected]+STRIP ?= $(BIN)arm-none-eabi-strip --strip-unneeded+SIZE = $(BIN)arm-none-eabi-size+BUILD_DIR = output+REF ?=++# the drawing runs every frame at -O2; the rest is small at -Os+HOT = src/gfx.c src/res.c+COLD = $(filter-out $(HOT) src/plat_host.c src/plat_eadk.c,$(wildcard src/*.c))+COLD_OBJ = $(patsubst src/%.c,$(BUILD_DIR)/cold/%.o,$(COLD))+HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h++CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device) -DDATA_BIN='"src/data.bin"'+CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter -fno-math-errno -fsingle-precision-constant+CFLAGS += -fno-tree-loop-distribute-patterns -flto -fno-fat-lto-objects -fwhole-program -fvisibility=internal+CFLAGS += -ffunction-sections -fdata-sections $(EXTRA)+LDFLAGS = -Wl,--relocatable -nostartfiles --specs=nano.specs+LDFLAGS += -Wl,-e,main -Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level+LDFLAGS += -Wl,--gc-sections -flinker-output=nolto-rel++.PHONY: build check clean host test data+build: $(BUILD_DIR)/isaac.nwa++# apps get 133652 bytes of RAM on custom builds of Epsilon 25.2 (148928 or more on official ones)+check: $(BUILD_DIR)/isaac.nwa+ $(Q) $(NWLINK) nwa-bin --ram-length 133652 $< $(BUILD_DIR)/isaac.bin+ @echo "BIN $(BUILD_DIR)/isaac.bin: $$(wc -c < $(BUILD_DIR)/isaac.bin) bytes"++$(BUILD_DIR)/data.o: src/data.S src/data.bin | $(BUILD_DIR)+ $(Q) $(CC) -c -DDATA_BIN='"src/data.bin"' $< -o $@++$(BUILD_DIR)/cold/%.o: src/%.c $(HEADERS)+ @mkdir -p $(@D)+ $(Q) $(CC) $(CFLAGS) -Os -c $< -o $@++$(BUILD_DIR)/isaac.nwa: $(HOT) src/plat_eadk.c $(COLD_OBJ) $(HEADERS) $(BUILD_DIR)/icon.o $(BUILD_DIR)/data.o+ @echo "LD $@"+ $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(HOT) src/plat_eadk.c $(COLD_OBJ) $(BUILD_DIR)/data.o $(BUILD_DIR)/icon.o -lm -o $@+ $(Q) $(STRIP) $@+ $(Q) $(SIZE) $@++$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)+ @echo "ICON $<"+ $(Q) $(NWLINK) png-icon-o $< $@++$(BUILD_DIR):+ $(Q) mkdir -p $@++# on a computer: tests/play.c plays it with scripted keys and saves screenshots+HOST_FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Isrc -DHOST -DDATA_BIN='"src/data.bin"'+HOST_SAN = -fsanitize=address,undefined -fno-sanitize-recover=undefined+HOST_SRC = $(filter-out src/plat_eadk.c src/main.c,$(wildcard src/*.c))+host: build/play+build/play: $(HOST_SRC) tests/play.c src/data.S src/data.bin $(HEADERS)+ $(Q) mkdir -p $(dir $@)+ $(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(HOST_SRC) tests/play.c src/data.S -lm -o $@+ @echo "HOST $@"++test: build/play+ python3 tests/check.py build/play++data:+ @test -n "$(REF)" || { echo "make data REF=<the folder tools/fetch.py filled>"; exit 1; }+ python3 -I tools/pack.py $(REF)++clean:+ $(Q) rm -rf $(BUILD_DIR) builddiff --git a/games/isaac/src/bosses.c b/games/isaac/src/bosses.cnew file mode 100644index 00000000..68420939--- /dev/null+++ b/games/isaac/src/bosses.c@@ -0,0 +1,853 @@+/* The bosses: each floor's, Mom, Mom's Heart, Satan and Isaac, and the seven+ * deadly sins. Each moves as in Rebirth: Monstro's hops, leap and spray of+ * blood, Larry Jr.'s segments, the Duke's flies, the horsemen's charges...+ * Their health is Rebirth's, near enough. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++typedef struct {+ int16_t spr;+ int16_t hp;+ uint8_t r, flying;+ const char *name;+} BossInfo;++#define B(m, s, hp, r, fly, n) [m - MON_MONSTRO] = {s, hp, r, fly, n}+static const BossInfo bosses[MON_COUNT - MON_MONSTRO] = {+ B(MON_MONSTRO, SPR_MONSTRO, 250, 18, 0, "MONSTRO"),+ B(MON_LARRY, SPR_LARRY, 30, 8, 0, "LARRY JR."),+ B(MON_THE_HOLLOW, SPR_THE_HOLLOW, 40, 8, 0, "THE HOLLOW"),+ B(MON_DUKE, SPR_DUKE, 110, 18, 1, "THE DUKE OF FLIES"),+ B(MON_HUSK, SPR_HUSK, 350, 18, 1, "THE HUSK"),+ B(MON_GEMINI, SPR_GEMINI, 140, 10, 0, "GEMINI"),+ B(MON_GEMINI_BABY, SPR_GEMINI_BABY, 60, 7, 1, "SUTURE"),+ B(MON_STEVEN, SPR_STEVEN, 180, 10, 0, "STEVEN"),+ B(MON_STEVEN_BABY, SPR_STEVEN_BABY, 60, 7, 1, "STEVEN"),+ B(MON_BLIGHTED_OVUM, SPR_BLIGHTED_OVUM, 200, 10, 0, "THE BLIGHTED OVUM"),+ B(MON_GURDY, SPR_GURDY, 350, 30, 0, "GURDY"),+ B(MON_GURDY_JR, SPR_GURDY_JR, 150, 18, 0, "GURDY JR."),+ B(MON_PEEP, SPR_PEEP, 250, 16, 0, "PEEP"),+ B(MON_BLOAT, SPR_BLOAT, 500, 16, 0, "THE BLOAT"),+ B(MON_CHUB, SPR_CHUB, 140, 16, 0, "CHUB"),+ B(MON_CARRION_QUEEN, SPR_CARRION_QUEEN, 120, 16, 0, "CARRION QUEEN"),+ B(MON_LOKI, SPR_LOKI, 450, 14, 0, "LOKI"),+ B(MON_LOKII, SPR_LOKII, 350, 14, 0, "LOKII"),+ B(MON_MOM, SPR_MOM_EYE, 645, 16, 0, "MOM"),+ B(MON_MOMS_HEART, SPR_MOMS_HEART, 1200, 26, 0, "MOM'S HEART"),+ B(MON_IT_LIVES, SPR_IT_LIVES, 1500, 26, 0, "IT LIVES"),+ B(MON_FAMINE, SPR_FAMINE, 300, 18, 1, "FAMINE"),+ B(MON_PESTILENCE, SPR_PESTILENCE, 400, 18, 1, "PESTILENCE"),+ B(MON_WAR, SPR_WAR, 550, 18, 0, "WAR"),+ B(MON_CONQUEST, SPR_CONQUEST, 700, 18, 0, "CONQUEST"),+ B(MON_DEATH, SPR_DEATH, 700, 18, 1, "DEATH"),+ B(MON_PIN, SPR_PIN, 200, 10, 0, "PIN"),+ B(MON_SCOLEX, SPR_SCOLEX, 600, 10, 0, "SCOLEX"),+ B(MON_FRAIL, SPR_FRAIL, 500, 10, 0, "THE FRAIL"),+ B(MON_MONSTRO2, SPR_MONSTRO2, 600, 18, 0, "MONSTRO II"),+ B(MON_GISH, SPR_GISH, 650, 18, 0, "GISH"),+ B(MON_MASK_OF_INFAMY, SPR_MASK_OF_INFAMY, 9999, 18, 0, "MASK OF INFAMY"),+ B(MON_HEART_OF_INFAMY, SPR_HEART_OF_INFAMY, 600, 14, 0, "HEART OF INFAMY"),+ B(MON_WIDOW, SPR_WIDOW, 300, 20, 0, "THE WIDOW"),+ B(MON_WRETCHED, SPR_WRETCHED, 450, 20, 0, "THE WRETCHED"),+ B(MON_DADDY_LONG_LEGS, SPR_DADDY_LONG_LEGS, 700, 22, 1, "DADDY LONG LEGS"),+ B(MON_TRIACHNID, SPR_TRIACHNID, 700, 22, 1, "TRIACHNID"),+ B(MON_FISTULA, SPR_FISTULA_BIG, 160, 22, 0, "FISTULA"),+ B(MON_TERATOMA, SPR_TERATOMA_BIG, 180, 22, 0, "TERATOMA"),+ B(MON_BLASTOCYST, SPR_BLASTOCYST_BIG, 300, 22, 0, "BLASTOCYST"),+ B(MON_SATAN, SPR_FALLEN, 700, 22, 1, "SATAN"),+ B(MON_FALLEN, SPR_FALLEN, 700, 20, 1, "THE FALLEN"),+ B(MON_KRAMPUS, SPR_KRAMPUS, 700, 18, 0, "KRAMPUS"),+ B(MON_ISAAC, SPR_ISAAC_BOSS, 1500, 12, 0, "ISAAC"),+ B(MON_HEADLESS_HORSEMAN, SPR_HEADLESS_HORSEMAN, 500, 18, 0, "HEADLESS HORSEMAN"),+ B(MON_DINGLE, SPR_DINGLE, 250, 16, 0, "DINGLE"),+ B(MON_HAUNT, SPR_HAUNT, 300, 18, 1, "THE HAUNT"),+ B(MON_LIL_HAUNT, SPR_LIL_HAUNT, 20, 7, 1, "LIL HAUNT"),+ B(MON_MEGA_FATTY, SPR_MEGA_FATTY, 350, 20, 0, "MEGA FATTY"),+ B(MON_MAMA_GURDY, SPR_MAMA_GURDY, 700, 30, 0, "MAMA GURDY"),+ B(MON_DARK_ONE, SPR_DARK_ONE, 550, 18, 0, "THE DARK ONE"),+ B(MON_POLYCEPHALUS, SPR_POLYCEPHALUS, 350, 18, 0, "POLYCEPHALUS"),+ B(MON_MR_FRED, SPR_MR_FRED, 600, 18, 0, "MR. FRED"),+ B(MON_GURGLING, SPR_GURGLING, 120, 12, 0, "GURGLING"),+ B(MON_SLOTH, SPR_SLOTH, 100, 12, 0, "SLOTH"),+ B(MON_LUST, SPR_LUST, 100, 10, 0, "LUST"),+ B(MON_WRATH, SPR_WRATH, 120, 12, 0, "WRATH"),+ B(MON_GLUTTONY, SPR_GLUTTONY, 140, 14, 0, "GLUTTONY"),+ B(MON_GREED, SPR_GREED, 120, 12, 0, "GREED"),+ B(MON_ENVY, SPR_ENVY, 90, 12, 0, "ENVY"),+ B(MON_PRIDE, SPR_PRIDE, 160, 12, 0, "PRIDE"),+ B(MON_MOM_HAND, SPR_MOMS_HAND, 0, 14, 0, "MOM"),+ B(MON_MOM_FOOT, SPR_MOM_FOOT, 0, 24, 0, "MOM"),+ B(MON_SATAN_STOMP, SPR_SATAN_STOMP, 0, 24, 0, "SATAN"),+ B(MON_DEATH_SCYTHE, SPR_BONE_SHOT, 0, 5, 1, "DEATH"),+};++static const BossInfo *info(int type) { return type >= MON_MONSTRO && type < MON_COUNT ? &bosses[type - MON_MONSTRO] : NULL; }++static Ent *boss_new(int type, float x, float y, int hp_scale_pct) {+ const BossInfo *b = info(type);+ if (!b || !b->spr) return NULL;+ Ent *e = ent_new(EK_MON, x, y);+ if (!e) return NULL;+ e->type = (uint8_t)type, e->spr = b->spr, e->r = b->r;+ e->hp = e->maxhp = b->hp * hp_scale_pct / 100.0f;+ e->flags = EF_BOSS | EF_NOKNOCK | (b->flying ? EF_FLYING : 0);+ e->t = 40, e->a = rndf() * 6.2831853f;+ return e;+}++static int sins_count;+void boss_spawn(int type, float x, float y) {+ Ent *e;+ switch (type) {+ case MON_LARRY: case MON_THE_HOLLOW: { /* a worm of segments, each following the one before */+ int n = type == MON_LARRY ? 6 : 10, prev = -1;+ for (int i = 0; i < n; i++) {+ e = boss_new(type, x - i * 10, y, 100);+ if (!e) break;+ e->flags |= EF_SEGMENT;+ e->parent = (int8_t)prev, e->sub = (uint8_t)i, e->c = DIR_RIGHT;+ if (prev >= 0) ents[prev].child = (int8_t)(e - ents);+ prev = (int)(e - ents);+ }+ return;+ }+ case MON_CHUB: case MON_CARRION_QUEEN: {+ int prev = -1;+ for (int i = 0; i < 3; i++) {+ e = boss_new(type, x - i * 18, y, 100);+ if (!e) break;+ e->flags |= EF_SEGMENT, e->parent = (int8_t)prev, e->sub = (uint8_t)i;+ if (prev >= 0) ents[prev].child = (int8_t)(e - ents);+ prev = (int)(e - ents);+ }+ return;+ }+ case MON_GEMINI: case MON_STEVEN: case MON_BLIGHTED_OVUM: {+ e = boss_new(type, x - 20, y, 100);+ Ent *b = boss_new(type == MON_STEVEN ? MON_STEVEN_BABY : MON_GEMINI_BABY, x + 20, y, 100);+ if (e && b) b->parent = (int8_t)(e - ents), e->child = (int8_t)(b - ents);+ if (b && type == MON_BLIGHTED_OVUM) b->spr = SPR_GEMINI_BABY;+ return;+ }+ case MON_LOKII:+ boss_new(MON_LOKII, x - 40, y, 100);+ boss_new(MON_LOKII, x + 40, y, 100);+ return;+ case MON_MASK_OF_INFAMY: {+ e = boss_new(MON_MASK_OF_INFAMY, x - 30, y, 100);+ if (e) e->flags |= EF_INVULN | EF_NOCOUNT;+ Ent *h = boss_new(MON_HEART_OF_INFAMY, x + 30, y, 100);+ if (e && h) e->parent = (int8_t)(h - ents);+ return;+ }+ case MON_HAUNT:+ e = boss_new(MON_HAUNT, x, y - 10, 100);+ for (int i = 0; i < 3; i++) {+ Ent *l = boss_new(MON_LIL_HAUNT, x, y, 100);+ if (l) l->sub = (uint8_t)i, l->flags &= (uint16_t)~EF_BOSS;+ }+ return;+ case MON_MOM: /* Mom herself is the room: her eye, hands and foot come from the doors and the sky */+ e = boss_new(MON_MOM, 160, 120, 100);+ if (e) e->flags |= EF_HIDDEN | EF_NOHIT, e->t = 50;+ return;+ case MON_FISTULA: case MON_TERATOMA: case MON_BLASTOCYST:+ e = boss_new(type, x, y, 100);+ if (e) e->sub = 0; /* (its size: 0 big, 1 medium, 2 small) */+ return;+ case MON_SATAN:+ e = boss_new(MON_SATAN, x, y - 20, 100);+ if (e) e->spr = SPR_SATAN, e->state = 0;+ return;+ case MON_ENVY: case MON_GLUTTONY: case MON_SLOTH: case MON_LUST: case MON_WRATH: case MON_GREED: case MON_PRIDE:+ sins_count++;+ e = boss_new(type, x, y, 100);+ if (e) e->flags &= (uint16_t)~EF_BOSS, e->flags |= EF_NOKNOCK;+ return;+ default:+ e = boss_new(type, x, y, 100);+ return;+ }+}++float bosses_hp(float *max) {+ float h = 0, m = 0;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || !(e->flags & EF_BOSS) || (e->flags & EF_NOCOUNT)) continue;+ if (e->type == MON_MOM_HAND || e->type == MON_MOM_FOOT || e->type == MON_SATAN_STOMP || e->type == MON_DEATH_SCYTHE) continue;+ h += e->hp > 0 ? e->hp : 0, m += e->maxhp;+ }+ if (max) *max = m;+ return h;+}++/* ---------------------------------------------------------------- helpers */+static float toward(Ent *e, float *dx, float *dy) {+ float x = pl.x - e->x, y = pl.y - e->y, d = fsqrt(x * x + y * y);+ if (d < 0.01f) d = 0.01f;+ *dx = x / d, *dy = y / d;+ return d;+}+static void glide(Ent *e, float dx, float dy, float s, float acc, bool fly) {+ e->vx += (dx * s - e->vx) * acc, e->vy += (dy * s - e->vy) * acc;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, fly, fly);+}+static void bounce(Ent *e, float s) {+ if (!e->vx && !e->vy) e->vx = s * (rnd(2) ? 1 : -1), e->vy = s * 0.8f * (rnd(2) ? 1 : -1);+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, true, true);+ if (e->vx == 0) e->vx = -ox;+ if (e->vy == 0) e->vy = -oy;+}+/* a spray of shots at him, the way Monstro coughs them up */+static void spray(Ent *e, int n, float speed, float spread) {+ float dx, dy;+ toward(e, &dx, &dy);+ float a0 = fatan2(dy, dx);+ for (int i = 0; i < n; i++) {+ float a = a0 + (rndf() - 0.5f) * spread, s = speed * (0.7f + rndf() * 0.5f);+ Tear *t = enemy_shot(e->x, e->y - 12, fcos(a) * s, fsin(a) * s, 0);+ if (t) t->range = 90 + rnd(80);+ }+}+static int count_type(int type) {+ int n = 0;+ for (int i = 0; i < MAX_ENTS; i++) n += ents[i].kind == EK_MON && ents[i].type == type;+ return n;+}+static void random_spot(float *x, float *y, float away) {+ for (int k = 0; k < 30; k++) {+ float a = ROOM_X + 30 + rnd(200), b = ROOM_Y + 25 + rnd(90);+ if (!solid_at(a, b, false) && dist2(a, b, pl.x, pl.y) > away * away) {+ *x = a, *y = b;+ return;+ }+ }+ *x = 160, *y = 100;+}++/* the hop of Monstro and his kind: a short hop at him, a leap that lands on him, or a cough of blood */+static void jumper(Ent *e, int big_n, float leap_h, bool creep, bool laser) {+ float dx, dy, d = toward(e, &dx, &dy);+ switch (e->state) {+ case 0: /* waiting */+ e->z = 0;+ if (--e->t > 0) break;+ {+ int r = rnd(10);+ if (r < 5) e->state = 1, e->t = e->t3 = 20, e->a = fatan2(dy, dx), e->b = d < 50 ? d : 50;+ else if (r < 7) e->state = 2, e->t = e->t3 = 50, e->flags |= EF_NOHIT;+ else e->state = 3, e->t = 30;+ }+ break;+ case 1: { /* a hop */+ float k = 1 - (float)e->t / e->t3;+ e->z = -16 * 4 * k * (1 - k);+ e->vx = fcos(e->a) * e->b / e->t3, e->vy = fsin(e->a) * e->b / e->t3;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, true, false);+ if (--e->t <= 0) e->state = 0, e->t = 15 + rnd(20), e->z = 0;+ break;+ }+ case 2: { /* the leap: up out of sight, his shadow follows Isaac, down on him */+ float k = 1 - (float)e->t / e->t3;+ e->z = k < 0.5f ? -leap_h * k * 2 : -leap_h * (1 - k) * 2;+ if (k > 0.3f && k < 0.75f) e->x += (pl.x - e->x) * 0.1f, e->y += (pl.y - e->y) * 0.1f;+ if (--e->t <= 0) {+ e->state = 0, e->t = 25, e->z = 0, e->flags &= (uint16_t)~EF_NOHIT;+ g_shake = 8;+ enemy_shots_ring(e->x, e->y - 6, big_n, 3.2f, rndf());+ if (creep) creep_spawn(e->x, e->y, 22, 200, true, 0);+ }+ break;+ }+ case 3: /* the cough */+ if (e->t == 12) {+ if (laser) laser_fire(e->x, e->y - 12, absf(pl.x - e->x) > absf(pl.y - e->y) ? (pl.x < e->x ? DIR_LEFT : DIR_RIGHT) : (pl.y < e->y ? DIR_UP : DIR_DOWN), 11, 0, 18, 0, true);+ else spray(e, 12, 3.8f, 0.9f);+ }+ if (--e->t <= 0) e->state = 0, e->t = 20 + rnd(20);+ break;+ }+}++/* the horsemen's charge across the room */+static void horseman(Ent *e) {+ float dx, dy;+ toward(e, &dx, &dy);+ switch (e->state) {+ case 0: /* floats about; then a charge or its own attack */+ glide(e, fcos(e->a), fsin(e->a) * 0.5f, 1.2f, 0.05f, true);+ e->a += 0.03f;+ if (--e->t <= 0) {+ e->state = rnd(2) ? 1 : 2, e->t = 20;+ e->c = pl.y;+ }+ break;+ case 1: /* to the side, at his height, then across */+ e->x += ((pl.x < 160 ? ROOM_R - 20 : ROOM_X + 20) - e->x) * 0.15f;+ e->y += (e->c - e->y) * 0.15f;+ if (--e->t <= 0) e->state = 3, e->vx = e->x < 160 ? 7 : -7, e->vy = 0, e->t = 50;+ break;+ case 2: /* its own attack */+ if (e->t == 10) {+ switch (e->type) {+ case MON_FAMINE: for (int i = 0; i < 2; i++) mon_spawn(MON_ATTACK_FLY, e->x + i * 8, e->y); break;+ case MON_PESTILENCE: enemy_shots_ring(e->x, e->y - 10, 8, 3, 0); creep_spawn(e->x, e->y, 18, 200, true, 1); break;+ case MON_WAR: for (int i = 0; i < 2; i++) pickup_spawn(PK_TROLL_BOMB, 0, e->x + i * 20 - 10, e->y + 10, true); break;+ case MON_CONQUEST: for (int i = 0; i < 3; i++) {+ Laser *l = laser_fire(pl.x + (i - 1) * 40, pl.y, -1, 12, 0, 20, 2, true);+ (void)l;+ }+ break;+ case MON_DEATH: if (count_type(MON_DEATH_SCYTHE) < 4) for (int i = 0; i < 2; i++) boss_spawn(MON_DEATH_SCYTHE, e->x, e->y);+ break;+ default: spray(e, 8, 3.5f, 1.2f); break;+ }+ }+ if (--e->t <= 0) e->state = 0, e->t = 60 + rnd(40);+ break;+ case 3: /* the charge */+ e->x += e->vx;+ if (e->x < ROOM_X - 30 || e->x > ROOM_R + 30 || --e->t <= 0) {+ e->x = e->x < 160 ? ROOM_X + 10 : ROOM_R - 10;+ e->state = 0, e->t = 50, e->vx = 0;+ if (e->type == MON_DEATH && e->hp < e->maxhp / 2) e->spr = SPR_DEATH_HORSE;+ }+ break;+ }+}++/* a worm of segments (Larry Jr., the Hollow): the head turns on the grid, the others follow its trail */+static void worm(Ent *e) {+ if (e->parent >= 0 && ents[e->parent].kind == EK_MON && ents[e->parent].type == e->type) {+ Ent *p = &ents[e->parent];+ float dx = p->x - e->x, dy = p->y - e->y, d = fsqrt(dx * dx + dy * dy);+ if (d > 11) e->x += dx / d * (d - 11), e->y += dy / d * (d - 11);+ return;+ }+ /* the head */+ e->parent = -1;+ int dd = (int)e->c;+ float s = e->type == MON_LARRY ? 2.2f : 2.6f;+ e->vx = DIR_X[dd] * s, e->vy = DIR_Y[dd] * s;+ float ox = e->x, oy = e->y;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, 7, false, false);+ bool stuck = absf(e->x - ox) + absf(e->y - oy) < 0.5f;+ if (stuck || --e->t <= 0) {+ int nd = rnd(4);+ if (nd == (dd + 2) % 4) nd = (nd + 1) % 4;+ float pdx = pl.x - e->x, pdy = pl.y - e->y;+ if (rnd(2)) nd = absf(pdx) > absf(pdy) ? (pdx < 0 ? DIR_LEFT : DIR_RIGHT) : (pdy < 0 ? DIR_UP : DIR_DOWN);+ e->c = (float)nd, e->t = 20 + rnd(30);+ }+ if (rnd(60) == 0 && e->type == MON_LARRY) enemy_shot_at(e->x, e->y - 6, 3.5f);+}++/* burrowing worms (Pin, Scolex, the Frail, Polycephalus, Mr. Fred): under the floor, up by him, a spit, down */+static void burrower(Ent *e) {+ switch (e->state) {+ case 0:+ e->flags |= EF_HIDDEN | EF_NOHIT | EF_INVULN;+ if (--e->t <= 0) {+ random_spot(&e->x, &e->y, 40);+ if (rnd(2)) e->x = pl.x + (rnd(2) ? 50 : -50), e->y = pl.y;+ e->x = clampf(e->x, ROOM_X + 20, ROOM_R - 20), e->y = clampf(e->y, ROOM_Y + 20, ROOM_B - 15);+ e->state = 1, e->t = 50;+ e->flags &= (uint16_t)~(EF_HIDDEN | EF_NOHIT | EF_INVULN);+ g_shake = 4;+ }+ break;+ case 1:+ e->z = e->t > 40 ? -(50 - e->t) * 2.0f : e->t < 10 ? -e->t * 2.0f : -20;+ if (e->t == 30) {+ if (e->type == MON_POLYCEPHALUS || e->type == MON_MR_FRED) enemy_shots_ring(e->x, e->y - 14, 10, 3, rndf());+ else if (e->type == MON_FRAIL) laser_fire(e->x, e->y - 14, pl.x < e->x ? DIR_LEFT : DIR_RIGHT, 9, 0, 16, 0, true);+ else spray(e, 6, 3.5f, 0.6f);+ }+ if (--e->t <= 0) e->state = 0, e->t = 25 + rnd(30), e->z = 0;+ break;+ }+}++/* ---------------------------------------------------------------- Mom */+static void mom_update(Ent *e) {+ e->flags |= EF_HIDDEN | EF_NOHIT;+ if (--e->t > 0) return;+ int r = rnd(10);+ /* what comes next: her eye in a door, her hand reaching, her foot, or her friends */+ static const float wx[4] = {160, ROOM_X - 2, 160, ROOM_R + 2}, wy[4] = {ROOM_B + 4, 120, ROOM_Y - 2, 120};+ int w = rnd(4);+ if (r < 4) {+ Ent *f = boss_new(MON_MOM_FOOT, pl.x, pl.y, 100);+ if (f) f->parent = (int8_t)(e - ents), f->state = 0, f->t = 30, f->z = -200, f->flags |= EF_NOHIT | EF_NOCOUNT, f->hp = f->maxhp = 1000;+ e->t = 70;+ } else if (r < 7) {+ Ent *f = boss_new(MON_MOM, wx[w], wy[w], 100);+ if (f) f->parent = (int8_t)(e - ents), f->sub = (uint8_t)(1 + w), f->t = 70, f->flags |= EF_NOCOUNT, f->hp = f->maxhp = 1000;+ e->t = 80;+ } else if (r < 9) {+ Ent *f = boss_new(MON_MOM_HAND, wx[w], wy[w], 100);+ if (f) f->parent = (int8_t)(e - ents), f->sub = (uint8_t)w, f->t = 60, f->flags |= EF_NOCOUNT, f->hp = f->maxhp = 1000;+ e->t = 70;+ } else {+ static const uint8_t friends[] = {MON_GAPER, MON_HOPPER, MON_ATTACK_FLY, MON_POOTER, MON_SPITTY, MON_BONY};+ for (int i = 0; i < 2; i++) mon_spawn(friends[rnd(sizeof friends)], wx[w] + (w & 1 ? (w == 1 ? 14 : -14) : 0), wy[w] + (w & 1 ? 0 : (w == 0 ? -14 : 14)));+ e->t = 60;+ }+}++/* a part of Mom: hurting it hurts her */+static Ent *mom_of(Ent *e) {+ if (e->parent >= 0 && ents[e->parent].kind == EK_MON && ents[e->parent].type == MON_MOM && ents[e->parent].sub == 0) return &ents[e->parent];+ return NULL;+}++/* ---------------------------------------------------------------- each boss */+bool boss_update(Ent *e) {+ if (e->type < MON_MONSTRO) return false;+ float dx, dy, d = toward(e, &dx, &dy);+ /* parts pass their hurt on */+ Ent *mom = (e->type == MON_MOM && e->sub) || e->type == MON_MOM_HAND || e->type == MON_MOM_FOOT ? mom_of(e) : NULL;+ switch (e->type) {+ case MON_MONSTRO: jumper(e, 8, 90, false, false); break;+ case MON_MONSTRO2: jumper(e, 10, 100, false, rnd(3) == 0 && e->state == 0); break;+ case MON_GISH: jumper(e, 10, 90, true, false); break;+ case MON_MEGA_FATTY:+ glide(e, dx, dy, 0.7f, 0.05f, false);+ if (--e->t <= 0) {+ e->t = 70;+ if (rnd(2)) enemy_shots_ring(e->x, e->y - 14, 10, 2.8f, 0);+ else mon_spawn(MON_ATTACK_FLY, e->x, e->y - 10), mon_spawn(MON_ATTACK_FLY, e->x + 6, e->y - 10);+ }+ break;+ case MON_LARRY: case MON_THE_HOLLOW: worm(e); break;+ case MON_CHUB: case MON_CARRION_QUEEN: {+ if (e->parent >= 0 && ents[e->parent].kind == EK_MON && ents[e->parent].type == e->type) {+ Ent *p = &ents[e->parent];+ float px = p->x - e->x, py = p->y - e->y, pd = fsqrt(px * px + py * py);+ if (pd > 16) e->x += px / pd * (pd - 16), e->y += py / pd * (pd - 16);+ break;+ }+ e->parent = -1;+ if (e->state == 0) {+ glide(e, fcos(e->a), fsin(e->a), 1.4f, 0.1f, false);+ if (e->vx == 0 || e->vy == 0) e->a += 1.4f;+ if (--e->t <= 0 && (absf(pl.x - e->x) < 12 || absf(pl.y - e->y) < 12)) {+ e->state = 1, e->t = 40;+ e->a = absf(pl.x - e->x) > absf(pl.y - e->y) ? (pl.x < e->x ? 3.14159f : 0) : (pl.y < e->y ? -1.5708f : 1.5708f);+ } else if (e->t <= -60) e->t = 0;+ } else {+ e->vx = fcos(e->a) * 6, e->vy = fsin(e->a) * 6;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, false, false);+ if (e->type == MON_CARRION_QUEEN && (e->t & 3) == 0) creep_spawn(e->x, e->y, 8, 90, true, 0);+ if (--e->t <= 0 || (e->vx == 0 && e->vy == 0)) e->state = 0, e->t = 50, e->a = rndf() * 6.28f;+ }+ break;+ }+ case MON_DUKE: case MON_HUSK: /* bounces about; flies out of it, or blown at him */+ bounce(e, 0.9f);+ if (--e->t <= 0) {+ e->t = 70 + rnd(40);+ int flies = count_type(MON_ATTACK_FLY) + count_type(MON_FLY);+ if (flies < 6) for (int i = 0; i < (e->type == MON_HUSK ? 3 : 2); i++) mon_spawn(rnd(2) ? MON_ATTACK_FLY : MON_FLY, e->x + rnd(16) - 8, e->y + 6);+ else for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && (ents[i].type == MON_FLY || ents[i].type == MON_ATTACK_FLY)) ents[i].vx = dx * 5, ents[i].vy = dy * 5;+ if (e->type == MON_HUSK) spray(e, 6, 3.5f, 0.8f);+ }+ break;+ case MON_GEMINI: case MON_STEVEN: case MON_BLIGHTED_OVUM: { /* the big one chases; it goes wild when alone */+ bool alone = e->child < 0 || ents[e->child].kind != EK_MON;+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ glide(e, px, py, alone ? 2.6f : 1.5f, 0.12f, false);+ if (e->type == MON_STEVEN && --e->t <= 0) e->t = 60, enemy_shot_at(e->x, e->y - 10, 4);+ break;+ }+ case MON_GEMINI_BABY: case MON_STEVEN_BABY: { /* the little one, on its cord, shoots */+ Ent *p = e->parent >= 0 && ents[e->parent].kind == EK_MON ? &ents[e->parent] : NULL;+ if (p) {+ float ox = e->x - p->x, oy = e->y - p->y, od = fsqrt(ox * ox + oy * oy);+ glide(e, -dx * 0.3f + fcos(e->a), -dy * 0.3f + fsin(e->a), 1.6f, 0.06f, true);+ e->a += 0.05f;+ if (od > 60) e->x = p->x + ox / od * 60, e->y = p->y + oy / od * 60;+ } else+ glide(e, dx, dy, 2.2f, 0.08f, true);+ if (--e->t <= 0) e->t = 45, enemy_shot_at(e->x, e->y - 8, 3.8f);+ break;+ }+ case MON_GURDY: case MON_MAMA_GURDY: /* sits; spits spreads of blood, calls Pooters and flies */+ if (--e->t <= 0) {+ e->t = 55 + rnd(30);+ int r = rnd(3);+ if (r == 0) spray(e, 6, 3.6f, 0.6f);+ else if (r == 1) enemy_shots_ring(e->x, e->y - 20, 8, 3, rndf());+ else if (count_type(MON_POOTER) + count_type(MON_ATTACK_FLY) < 4) mon_spawn(rnd(2) ? MON_POOTER : MON_ATTACK_FLY, e->x + rnd(30) - 15, e->y + 16);+ }+ break;+ case MON_GURDY_JR: case MON_DINGLE: case MON_DARK_ONE: /* charges at him; spits */+ if (e->state == 0) {+ glide(e, fcos(e->a), fsin(e->a), 0.8f, 0.1f, false);+ e->a += 0.04f;+ if (--e->t <= 0) {+ e->state = 1, e->t = 25;+ e->vx = dx * 5.5f, e->vy = dy * 5.5f;+ }+ } else {+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, false, false);+ if (--e->t <= 0 || (e->vx == 0 && e->vy == 0)) {+ e->state = 0, e->t = 40 + rnd(30);+ if (e->type == MON_DINGLE && count_type(MON_DIP) < 4) mon_spawn(MON_DIP, e->x, e->y + 8);+ else if (e->type == MON_DARK_ONE) enemy_shots_ring(e->x, e->y - 10, 8, 3, 0);+ else spray(e, 5, 3.5f, 0.5f);+ }+ }+ break;+ case MON_PEEP: case MON_BLOAT: /* bounces; pees creep; shoots */+ bounce(e, 1.1f);+ if ((g_frame & 15) == 0) creep_spawn(e->x, e->y, 10, 150, true, 2);+ if (--e->t <= 0) {+ e->t = 50 + rnd(30);+ if (e->type == MON_BLOAT && rnd(2)) laser_fire(e->x, e->y - 12, DIR_DOWN, 9, 0, 18, 0, true);+ else enemy_shots_ring(e->x, e->y - 12, 6, 3, rndf());+ }+ break;+ case MON_LOKI: case MON_LOKII: /* gone, back elsewhere, shots four or eight ways */+ if (e->state == 0) {+ if (--e->t <= 0) e->state = 1, e->t = 14;+ } else if (e->state == 1) {+ e->alpha = (uint8_t)(e->t * 2);+ if (--e->t <= 0) random_spot(&e->x, &e->y, 50), e->state = 2, e->t = 14;+ } else {+ e->alpha = (uint8_t)(32 - e->t * 2);+ if (--e->t <= 0) {+ e->alpha = 32, e->state = 0, e->t = 40 + rnd(30);+ int r = rnd(3);+ if (r == 0) enemy_shots_ring(e->x, e->y - 10, 4, 3.5f, 0);+ else if (r == 1) enemy_shots_ring(e->x, e->y - 10, 8, 3, 0.39f);+ else if (count_type(MON_ATTACK_FLY) < 3) mon_spawn(MON_ATTACK_FLY, e->x, e->y), mon_spawn(MON_ATTACK_FLY, e->x + 6, e->y);+ }+ }+ break;+ case MON_FAMINE: case MON_PESTILENCE: case MON_WAR: case MON_CONQUEST: case MON_DEATH: case MON_HEADLESS_HORSEMAN:+ horseman(e);+ break;+ case MON_DEATH_SCYTHE: /* Death's scythes: they fly at him */+ e->flags |= EF_NOCOUNT | EF_INVULN;+ glide(e, dx, dy, 2.6f, 0.05f, true);+ if (count_type(MON_DEATH) == 0 || ++e->t3 > 400) e->kind = EK_NONE;+ break;+ case MON_PIN: case MON_SCOLEX: case MON_FRAIL: case MON_POLYCEPHALUS: case MON_MR_FRED:+ burrower(e);+ break;+ case MON_MASK_OF_INFAMY: /* the mask charges, the heart behind it shoots; the heart is what dies */+ e->flags |= EF_INVULN | EF_NOCOUNT;+ if (e->parent < 0 || ents[e->parent].kind != EK_MON) {+ e->kind = EK_NONE;+ break;+ }+ if (e->state == 0) {+ glide(e, dx, dy, 1.2f, 0.08f, false);+ if (--e->t <= 0 && (absf(pl.x - e->x) < 10 || absf(pl.y - e->y) < 10)) e->state = 1, e->t = 30, e->vx = dx * 6, e->vy = dy * 6;+ if (e->t < -60) e->t = 0;+ } else {+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.7f, false, false);+ if (--e->t <= 0 || (!e->vx && !e->vy)) e->state = 0, e->t = 40;+ }+ break;+ case MON_HEART_OF_INFAMY:+ glide(e, -dx + fcos(e->a), -dy + fsin(e->a), 1.0f, 0.06f, false);+ e->a += 0.05f;+ if (--e->t <= 0) e->t = 60, spray(e, 4, 3.4f, 0.5f);+ break;+ case MON_WIDOW: case MON_WRETCHED: /* jumps at him; spiders; a web of creep */+ if (e->state == 0) {+ glide(e, dx, dy, 0.8f, 0.05f, false);+ if (--e->t <= 0) {+ int r = rnd(3);+ if (r == 0) e->state = 1, e->t = e->t3 = 30, e->a = fatan2(dy, dx), e->b = d < 80 ? d : 80;+ else if (r == 1 && count_type(MON_SPIDER) < 4) mon_spawn(MON_SPIDER, e->x, e->y + 8), mon_spawn(MON_SPIDER, e->x + 6, e->y + 8), e->t = 50;+ else spray(e, 6, 3.4f, 0.7f), creep_spawn(e->x, e->y, 20, 200, true, 3), e->t = 50;+ }+ } else {+ float k = 1 - (float)e->t / e->t3;+ e->z = -40 * 4 * k * (1 - k);+ e->vx = fcos(e->a) * e->b / e->t3, e->vy = fsin(e->a) * e->b / e->t3;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.6f, true, false);+ if (--e->t <= 0) e->state = 0, e->t = 40, e->z = 0;+ }+ break;+ case MON_DADDY_LONG_LEGS: case MON_TRIACHNID: /* stomps where he stands, again and again */+ if (e->state == 0) {+ e->z = -200, e->flags |= EF_NOHIT;+ e->x += (pl.x - e->x) * 0.08f, e->y += (pl.y - e->y) * 0.08f;+ if (--e->t <= 0) e->state = 1, e->t = 12;+ } else if (e->state == 1) {+ e->z += 18;+ if (e->z >= 0) {+ e->z = 0, e->state = 2, e->t = 40, g_shake = 6, e->flags &= (uint16_t)~EF_NOHIT;+ if (e->type == MON_TRIACHNID) enemy_shots_ring(e->x, e->y - 10, 8, 3, 0);+ else if (rnd(2)) mon_spawn(MON_SPIDER, e->x, e->y + 10);+ }+ } else {+ if (--e->t <= 0) e->state = 3, e->t = 15;+ }+ if (e->state == 3) {+ e->z -= 14;+ if (--e->t <= 0) e->state = 0, e->t = 30 + rnd(20);+ }+ break;+ case MON_FISTULA: case MON_TERATOMA: case MON_BLASTOCYST: /* bounce about; split when beaten */+ if (e->type == MON_BLASTOCYST) {+ if (e->state == 0) {+ e->z = 0;+ if (--e->t <= 0) e->state = 1, e->t = e->t3 = 24, e->a = fatan2(dy, dx), e->b = d < 60 ? d : 60;+ } else {+ float k = 1 - (float)e->t / e->t3;+ e->z = -30 * 4 * k * (1 - k);+ e->vx = fcos(e->a) * e->b / e->t3, e->vy = fsin(e->a) * e->b / e->t3;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.6f, true, false);+ if (--e->t <= 0) e->state = 0, e->t = 20 + rnd(20), e->z = 0, enemy_shots_ring(e->x, e->y - 8, 6 - e->sub * 2, 3, 0);+ }+ } else+ bounce(e, 1.2f + e->sub * 0.5f);+ break;+ case MON_HAUNT: /* drifts; its Lil Haunts first; then its own beam */+ glide(e, fcos(e->a), 0.3f * fsin(e->a * 2), 1.0f, 0.05f, true);+ e->a += 0.02f;+ e->flags = (uint16_t)((e->flags & ~EF_INVULN) | (count_type(MON_LIL_HAUNT) ? EF_INVULN : 0));+ if (!count_type(MON_LIL_HAUNT) && --e->t <= 0) {+ e->t = 70;+ if (rnd(2)) laser_fire(e->x, e->y - 10, DIR_DOWN, 10, 0, 20, 0, true);+ else spray(e, 6, 3.4f, 0.8f);+ }+ break;+ case MON_LIL_HAUNT: {+ Ent *h = NULL;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && ents[i].type == MON_HAUNT) h = &ents[i];+ if (h) {+ float a = g_frame * 0.05f + e->sub * 2.094f;+ e->x = h->x + fcos(a) * 30, e->y = h->y + fsin(a) * 18;+ } else+ glide(e, dx, dy, 1.4f, 0.08f, true);+ if (--e->t <= 0) e->t = 60 + rnd(40), enemy_shot_at(e->x, e->y - 8, 3.4f);+ break;+ }+ case MON_MOM:+ if (e->sub == 0) {+ mom_update(e);+ break;+ }+ /* her eye in a door: it looks at him and shoots */+ e->flags &= (uint16_t)~(EF_HIDDEN | EF_NOHIT);+ if (e->t == 40 || e->t == 20) spray(e, 3, 3.6f, 0.4f);+ if (--e->t <= 0) e->kind = EK_NONE;+ break;+ case MON_MOM_HAND: /* her hand comes in from a door at him */+ if (e->t > 20) glide(e, dx, dy, 1.6f, 0.1f, true);+ if (--e->t <= 0) e->kind = EK_NONE;+ break;+ case MON_MOM_FOOT: case MON_SATAN_STOMP: /* a foot comes down where he stands */+ if (e->state == 0) {+ e->x += (pl.x - e->x) * 0.12f, e->y += (pl.y - e->y) * 0.12f;+ if (--e->t <= 0) e->state = 1;+ } else if (e->state == 1) {+ e->z += 25;+ if (e->z >= 0) {+ e->z = 0, e->state = 2, e->t = 22, g_shake = 10, e->flags &= (uint16_t)~EF_NOHIT;+ fx_spawn(FX_SHOCKWAVE, e->x, e->y, 14);+ }+ } else if (e->state == 2) {+ if (--e->t <= 0) e->state = 3, e->flags |= EF_NOHIT;+ } else {+ e->z -= 25;+ if (e->z < -220) e->kind = EK_NONE;+ }+ break;+ case MON_MOMS_HEART: case MON_IT_LIVES: /* beats; showers of blood; her womb's friends */+ if (--e->t <= 0) {+ e->t = 50 + rnd(20);+ int r = rnd(4);+ if (r == 0) enemy_shots_ring(e->x, e->y - 20, 12, 3, rndf());+ else if (r == 1) spray(e, 10, 3.8f, 1.0f);+ else if (r == 2 && count_type(MON_FISTULA) + count_type(MON_ATTACK_FLY) < 3) mon_spawn(MON_ATTACK_FLY, e->x - 20, e->y), mon_spawn(MON_ATTACK_FLY, e->x + 20, e->y);+ else for (int i = 0; i < 3; i++) laser_fire(pl.x + (i - 1) * 30 + rnd(10), pl.y, -1, 10, 0, 20, 0, true);+ }+ e->flags = (uint16_t)((e->flags & ~EF_INVULN) | (e->t > 40 ? EF_INVULN * 0 : 0));+ break;+ case MON_SATAN: /* the Fallen first; then Satan's own hands and hoof */+ if (e->state == 0) {+ glide(e, fcos(e->a), fsin(e->a) * 0.4f, 1.2f, 0.04f, true);+ e->a += 0.02f;+ if (--e->t <= 0) {+ e->t = 60;+ if (rnd(2)) laser_fire(e->x, e->y - 14, dx * dx > dy * dy ? (dx < 0 ? DIR_LEFT : DIR_RIGHT) : (dy < 0 ? DIR_UP : DIR_DOWN), 12, 0, 22, 0, true);+ else enemy_shots_ring(e->x, e->y - 14, 12, 3, rndf());+ }+ if (e->hp < e->maxhp / 2) e->state = 1, e->spr = SPR_SATAN, e->t = 60, g_shake = 15;+ } else {+ e->x += (160 - e->x) * 0.05f, e->y += (85 - e->y) * 0.05f;+ if (--e->t <= 0) {+ e->t = 70;+ int r = rnd(3);+ if (r == 0) { Ent *f = boss_new(MON_SATAN_STOMP, pl.x, pl.y, 100); if (f) f->t = 30, f->z = -200, f->flags |= EF_NOHIT | EF_NOCOUNT | EF_INVULN; }+ else if (r == 1) for (int i = 0; i < 2; i++) laser_fire(e->x + (i ? 40 : -40), e->y, DIR_DOWN, 12, 0, 22, 0, true);+ else spray(e, 14, 3.8f, 1.4f);+ }+ }+ break;+ case MON_ISAAC: /* Isaac, in the Cathedral: shots, then light from the sky, then wings */+ glide(e, fcos(e->a) * 0.4f, fsin(e->a) * 0.3f, 0.6f, 0.05f, false);+ e->a += 0.02f;+ if (--e->t <= 0) {+ e->t = e->hp > e->maxhp * 2 / 3 ? 50 : 40;+ int phase = e->hp > e->maxhp * 2 / 3 ? 0 : e->hp > e->maxhp / 3 ? 1 : 2;+ if (phase == 0) spray(e, 8, 3.5f, 1.0f);+ else if (phase == 1) for (int i = 0; i < 4; i++) laser_fire(ROOM_X + 30 + rnd(220), ROOM_Y + 20 + rnd(110), -1, 12, 0, 22, 2, true);+ else enemy_shots_ring(e->x, e->y - 12, 14, 3.2f, rndf()), mon_spawn(MON_ANGELIC_BABY, e->x, e->y);+ }+ break;+ case MON_KRAMPUS: case MON_FALLEN:+ glide(e, dx, dy, 1.0f, 0.05f, e->type == MON_FALLEN);+ if (--e->t <= 0) {+ e->t = 60;+ if (rnd(2)) laser_fire(e->x, e->y - 12, dx * dx > dy * dy ? (dx < 0 ? DIR_LEFT : DIR_RIGHT) : (dy < 0 ? DIR_UP : DIR_DOWN), 11, 0, 20, 0, true);+ else spray(e, 8, 3.6f, 1.0f);+ }+ break;+ case MON_GURGLING:+ glide(e, dx, dy, 1.6f, 0.08f, false);+ if ((g_frame & 7) == 0) creep_spawn(e->x, e->y, 8, 120, true, 0);+ break;+ /* the seven deadly sins */+ case MON_SLOTH:+ glide(e, dx, dy, 0.6f, 0.05f, false);+ if (--e->t <= 0) e->t = 70, spray(e, 3, 2.6f, 0.4f), creep_spawn(e->x, e->y, 12, 150, true, 1);+ break;+ case MON_LUST: {+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ glide(e, px, py, 2.6f, 0.12f, false);+ break;+ }+ case MON_WRATH:+ glide(e, dx, dy, 1.3f, 0.08f, false);+ if (--e->t <= 0) e->t = 60, bomb_place(e->x, e->y + 6, 1);+ break;+ case MON_GLUTTONY:+ glide(e, dx, dy, 0.8f, 0.06f, false);+ if (--e->t <= 0) {+ e->t = 60;+ if (absf(pl.x - e->x) < 12 || absf(pl.y - e->y) < 12)+ laser_fire(e->x, e->y - 10, absf(pl.x - e->x) > absf(pl.y - e->y) ? (pl.x < e->x ? DIR_LEFT : DIR_RIGHT) : (pl.y < e->y ? DIR_UP : DIR_DOWN), 9, 0, 18, 0, true);+ else spray(e, 5, 3, 0.5f), creep_spawn(e->x, e->y, 12, 150, true, 0);+ }+ break;+ case MON_GREED:+ glide(e, -dx * 0.3f + fcos(e->a), -dy * 0.3f + fsin(e->a), 1.2f, 0.05f, false);+ e->a += 0.03f;+ if (--e->t <= 0) e->t = 50, spray(e, 3, 3.8f, 0.3f);+ break;+ case MON_ENVY:+ bounce(e, 1.4f + e->sub * 0.6f);+ break;+ case MON_PRIDE:+ glide(e, dx, dy, 1.1f, 0.06f, false);+ if (--e->t <= 0) {+ e->t = 60;+ if (rnd(2)) laser_fire(e->x, e->y - 10, rnd(4), 7, 0, 18, 0, true);+ else enemy_shots_ring(e->x, e->y - 10, 6, 3, rndf());+ }+ break;+ default: break;+ }+ /* the parts of Mom take the hurt for her */+ if (mom && e->hp < e->maxhp) {+ mom->hp -= e->maxhp - e->hp, e->hp = e->maxhp;+ mom->flash = 3;+ if (mom->hp <= 0) ent_kill(mom);+ }+ (void)d;+ return true;+}++bool boss_on_death(Ent *e) {+ if (e->type < MON_MONSTRO) return false;+ switch (e->type) {+ case MON_FISTULA: case MON_TERATOMA: case MON_BLASTOCYST: case MON_ENVY:+ if (e->sub < 2) { /* in two, smaller */+ static const int16_t spr_fis[3] = {SPR_FISTULA_BIG, SPR_FISTULA_MED, SPR_FISTULA_SMALL};+ static const int16_t spr_bla[3] = {SPR_BLASTOCYST_BIG, SPR_BLASTOCYST_MED, SPR_BLASTOCYST_SMALL};+ static const int16_t spr_env[3] = {SPR_ENVY, SPR_ENVY_HALF, SPR_ENVY_QUARTER};+ for (int i = 0; i < 2; i++) {+ Ent *c = ent_new(EK_MON, e->x + (i ? 8 : -8), e->y);+ if (!c) break;+ *c = *e;+ c->x = e->x + (i ? 8 : -8);+ c->sub = (uint8_t)(e->sub + 1);+ c->maxhp = c->hp = e->maxhp * 0.45f;+ c->r = e->r * 0.65f;+ c->spr = e->type == MON_BLASTOCYST ? spr_bla[c->sub] : e->type == MON_ENVY ? spr_env[c->sub] : spr_fis[c->sub];+ c->vx = i ? 2 : -2, c->vy = (rnd(2) ? 1 : -1) * 1.5f, c->state = 0, c->t = 20, c->z = 0;+ }+ }+ break;+ case MON_HEART_OF_INFAMY:+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && ents[i].type == MON_MASK_OF_INFAMY) ents[i].kind = EK_NONE;+ break;+ case MON_LARRY: case MON_THE_HOLLOW: case MON_CHUB: case MON_CARRION_QUEEN:+ /* (the segment after it leads now) */+ if (e->child >= 0 && ents[e->child].kind == EK_MON) ents[e->child].parent = -1;+ if (e->parent >= 0 && ents[e->parent].kind == EK_MON) ents[e->parent].child = -1;+ break;+ case MON_MOM:+ if (e->sub == 0) /* Mom dead: her parts go with her */+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && (ents[i].type == MON_MOM_FOOT || ents[i].type == MON_MOM_HAND || ents[i].type == MON_MOM)) ents[i].kind = EK_NONE;+ break;+ default: break;+ }+ decal_blood(e->x, e->y, e->r * 1.3f);+ for (int i = 0; i < 6; i++) fx_spawn(FX_GIB, e->x, e->y - 6, 16 + rnd(12));+ e->kind = EK_NONE;+ g_shake = 10;+ return true;+}++void boss_draw(Ent *e) {+ int x = (int)e->x, y = (int)(e->y + e->z), flags = 0;+ if (e->flags & EF_HIDDEN) {+ if (e->type >= MON_PIN && e->type <= MON_FRAIL) gfx_ellipse(x, (int)e->y, 8, 3, rgb565(0x3A2010), 18);+ if (e->type == MON_POLYCEPHALUS || e->type == MON_MR_FRED) gfx_ellipse(x, (int)e->y, 10, 4, rgb565(0x3A2010), 18);+ return;+ }+ if (e->vx < -0.1f || (e->type >= MON_FAMINE && e->type <= MON_DEATH && e->vx < 0)) flags = GFX_FLIPX;+ uint16_t tint = 0;+ int amount = 0;+ if (e->flash) tint = C_WHITE, amount = 18;+ else if (e->freeze) tint = rgb565(0xC0C0C0), amount = 24;+ else if (e->poison) tint = rgb565(0x40C040), amount = 12;+ int sx = 256, sy = 256;+ if (e->type == MON_MOMS_HEART || e->type == MON_IT_LIVES) { /* a heartbeat */+ int k = (int)(g_frame % 20);+ sy = sx = 256 + (k < 4 ? k * 6 : 0);+ }+ if (e->type == MON_MOM_FOOT || e->type == MON_SATAN_STOMP || e->type == MON_DADDY_LONG_LEGS || e->type == MON_TRIACHNID) {+ gfx_ellipse(x, (int)e->y, (int)e->r, (int)e->r / 3, C_BLACK, e->z < -100 ? 10 : 16); /* its shadow, coming */+ }+ if (e->type == MON_MOM && e->sub) { /* her eye peers in from the wall */+ static const uint8_t fl_of[5] = {0, GFX_FLIPY, 0, 0, GFX_FLIPX};+ (void)fl_of;+ gfx_sprite_tint(e->spr, 0, x, y + 16, 0, tint, amount, 32);+ return;+ }+ if (e->type == MON_DEATH_SCYTHE) {+ gfx_sprite_scaled(SPR_BONE_SHOT, 0, x, y - 8, 384, 384, (g_frame & 4) ? GFX_FLIPX : 0, rgb565(0x606070), 16, 32);+ return;+ }+ gfx_sprite_scaled(e->spr, 0, x, y + 3, sx, sy, flags, tint, amount, e->alpha);+}diff --git a/games/isaac/src/data.S b/games/isaac/src/data.Snew file mode 100644index 00000000..e4810897--- /dev/null+++ b/games/isaac/src/data.S@@ -0,0 +1,19 @@+/* data.bin (tools/pack.py) in the app's read-only data */+#ifdef __APPLE__+ .const_data+ .p2align 2+ .globl _is_data+_is_data:+ .incbin DATA_BIN+ .p2align 2+#else+ .section .rodata.is_data,"a"+ .balign 4+ .global is_data+is_data:+ .incbin DATA_BIN+ .balign 4+#endif+#ifdef __ELF__+ .section .note.GNU-stack,"",%progbits+#endifdiff --git a/games/isaac/src/data.bin b/games/isaac/src/data.binnew file mode 100644index 00000000..9d55379dBinary files /dev/null and b/games/isaac/src/data.bin differdiff --git a/games/isaac/src/data.h b/games/isaac/src/data.hnew file mode 100644index 00000000..ce9dcf13--- /dev/null+++ b/games/isaac/src/data.h@@ -0,0 +1,690 @@+/* Generated by tools/pack.py: what data.bin holds. */+#ifndef IS_DATA_H+#define IS_DATA_H++#define SEC_FRAMES 0+#define SEC_SPRITES 1+#define SEC_BACKDROPS 2+#define SEC_FONTS 3+#define SEC_ROOMS 4++enum {+ SPR_ISAAC_HEAD,+ SPR_ISAAC_BODY,+ SPR_TEAR,+ SPR_BLOOD_TEAR,+ SPR_ENEMY_SHOT,+ SPR_BONE_SHOT,+ SPR_KNIFE,+ SPR_GAPER,+ SPR_FROWNING_GAPER,+ SPR_GUSHER,+ SPR_PACER,+ SPR_HORF,+ SPR_FLY,+ SPR_POOTER,+ SPR_SUPER_POOTER,+ SPR_CLOTTY,+ SPR_CLOT,+ SPR_IBLOB,+ SPR_MULLIGAN,+ SPR_ATTACK_FLY,+ SPR_MAGGOT,+ SPR_HIVE,+ SPR_CHARGER,+ SPR_GLOBIN,+ SPR_BOOM_FLY,+ SPR_RED_BOOM_FLY,+ SPR_MAW,+ SPR_RED_MAW,+ SPR_HOST,+ SPR_RED_HOST,+ SPR_HOPPER,+ SPR_TRITE,+ SPR_BOIL,+ SPR_SPITTY,+ SPR_BRAIN,+ SPR_LEAPER,+ SPR_MR_MAW,+ SPR_BABY,+ SPR_ANGELIC_BABY,+ SPR_VIS,+ SPR_GUTS,+ SPR_KNIGHT,+ SPR_SELFLESS_KNIGHT,+ SPR_LEECH,+ SPR_KAMIKAZE_LEECH,+ SPR_PARA_BITE,+ SPR_EYE,+ SPR_BLOODSHOT_EYE,+ SPR_SUCKER,+ SPR_SPIT,+ SPR_EMBRYO,+ SPR_MOTER,+ SPR_SPIDER,+ SPR_KEEPER,+ SPR_GURGLE,+ SPR_WALKING_BOIL,+ SPR_BUTTLICKER,+ SPR_HANGER,+ SPR_SWARMER,+ SPR_HEART,+ SPR_MASK,+ SPR_BIG_SPIDER,+ SPR_NEST,+ SPR_BABY_LONG_LEGS,+ SPR_FATTY,+ SPR_PALE_FATTY,+ SPR_FAT_SACK,+ SPR_DEATHS_HEAD,+ SPR_MOMS_HAND,+ SPR_DIP,+ SPR_SQUIRT,+ SPR_SKINNY,+ SPR_BONY,+ SPR_ONE_TOOTH,+ SPR_GAPING_MAW,+ SPR_RAGLING,+ SPR_NIGHT_CRAWLER,+ SPR_DART_FLY,+ SPR_FAT_BAT,+ SPR_IMP,+ SPR_TUMOR,+ SPR_POKY,+ SPR_STONE_GRIMACE,+ SPR_CONJOINED_FATTY,+ SPR_ROUND_WORM,+ SPR_WALL_CREEP,+ SPR_MEMBRAIN,+ SPR_LUMP,+ SPR_FRED,+ SPR_HOMUNCULUS,+ SPR_BEGOTTEN,+ SPR_SQUIRT_DANK,+ SPR_CHUBBER,+ SPR_FLAMING_HOPPER,+ SPR_MONSTRO,+ SPR_LARRY,+ SPR_THE_HOLLOW,+ SPR_DUKE,+ SPR_HUSK,+ SPR_GEMINI,+ SPR_GEMINI_BABY,+ SPR_STEVEN,+ SPR_STEVEN_BABY,+ SPR_BLIGHTED_OVUM,+ SPR_UMBILICAL,+ SPR_GURDY,+ SPR_GURDY_JR,+ SPR_PEEP,+ SPR_BLOAT,+ SPR_CHUB,+ SPR_CARRION_QUEEN,+ SPR_LOKI,+ SPR_LOKII,+ SPR_MOM_EYE,+ SPR_MOMS_HEART,+ SPR_IT_LIVES,+ SPR_FAMINE,+ SPR_PESTILENCE,+ SPR_WAR,+ SPR_CONQUEST,+ SPR_DEATH,+ SPR_DEATH_HORSE,+ SPR_PIN,+ SPR_SCOLEX,+ SPR_FRAIL,+ SPR_MONSTRO2,+ SPR_GISH,+ SPR_MASK_OF_INFAMY,+ SPR_HEART_OF_INFAMY,+ SPR_WIDOW,+ SPR_WRETCHED,+ SPR_DADDY_LONG_LEGS,+ SPR_TRIACHNID,+ SPR_FISTULA_BIG,+ SPR_FISTULA_MED,+ SPR_FISTULA_SMALL,+ SPR_TERATOMA_BIG,+ SPR_BLASTOCYST_BIG,+ SPR_BLASTOCYST_MED,+ SPR_BLASTOCYST_SMALL,+ SPR_SATAN,+ SPR_SATAN_STOMP,+ SPR_FALLEN,+ SPR_KRAMPUS,+ SPR_ISAAC_BOSS,+ SPR_BLUE_BABY,+ SPR_HEADLESS_HORSEMAN,+ SPR_HORSEMAN_HEAD,+ SPR_DINGLE,+ SPR_HAUNT,+ SPR_LIL_HAUNT,+ SPR_MEGA_FATTY,+ SPR_CAGE,+ SPR_MAMA_GURDY,+ SPR_DARK_ONE,+ SPR_ADVERSARY,+ SPR_POLYCEPHALUS,+ SPR_MR_FRED,+ SPR_LAMB,+ SPR_URIEL,+ SPR_GABRIEL,+ SPR_GURGLING,+ SPR_MEGA_MAW,+ SPR_GATE,+ SPR_SLOTH,+ SPR_LUST,+ SPR_WRATH,+ SPR_GLUTTONY,+ SPR_GREED,+ SPR_ENVY,+ SPR_ENVY_HALF,+ SPR_ENVY_QUARTER,+ SPR_PRIDE,+ SPR_SUPER_SLOTH,+ SPR_POOP,+ SPR_RED_POOP,+ SPR_GOLDEN_POOP,+ SPR_CORNY_POOP,+ SPR_TNT,+ SPR_BLOCK,+ SPR_KEY_BLOCK,+ SPR_SPIKES,+ SPR_COBWEB,+ SPR_PIT,+ SPR_TRAPDOOR,+ SPR_ROCKS_GENERIC,+ SPR_ROCKS_CELLAR,+ SPR_ROCKS_CAVES,+ SPR_ROCKS_CATACOMBS,+ SPR_ROCKS_DEPTHS,+ SPR_ROCKS_WOMB,+ SPR_ROCKS_SHEOL,+ SPR_ROCKS_CATHEDRAL,+ SPR_ROCKS_SECRETROOM,+ SPR_ROCKS_SCARREDWOMB,+ SPR_MOM_FOOT,+ SPR_FIREPLACE,+ SPR_RED_FIREPLACE,+ SPR_BLUE_FIREPLACE,+ SPR_PURPLE_FIREPLACE,+ SPR_HEART_FULL,+ SPR_HEART_HALF,+ SPR_HEART_SOUL,+ SPR_HEART_ETERNAL,+ SPR_HEART_DOUBLE,+ SPR_HEART_BLACK,+ SPR_HEART_HALFSOUL,+ SPR_PENNY,+ SPR_NICKEL,+ SPR_DIME,+ SPR_LUCKY_PENNY,+ SPR_KEY,+ SPR_GOLDEN_KEY,+ SPR_KEY_RING,+ SPR_BOMB_PICKUP,+ SPR_DOUBLE_BOMB,+ SPR_TROLL_BOMB,+ SPR_GOLDEN_BOMB,+ SPR_CHEST,+ SPR_BOMB_CHEST,+ SPR_SPIKED_CHEST,+ SPR_ETERNAL_CHEST,+ SPR_LOCKED_CHEST,+ SPR_RED_CHEST,+ SPR_GRAB_BAG,+ SPR_BATTERY,+ SPR_TROPHY,+ SPR_BIG_CHEST,+ SPR_PRICE,+ SPR_PILL,+ SPR_CARD,+ SPR_RUNE,+ SPR_PEDESTAL,+ SPR_ITEM_1,+ SPR_ITEM_2,+ SPR_ITEM_3,+ SPR_ITEM_4,+ SPR_ITEM_5,+ SPR_ITEM_6,+ SPR_ITEM_7,+ SPR_ITEM_8,+ SPR_ITEM_10,+ SPR_ITEM_11,+ SPR_ITEM_12,+ SPR_ITEM_13,+ SPR_ITEM_14,+ SPR_ITEM_15,+ SPR_ITEM_16,+ SPR_ITEM_17,+ SPR_ITEM_18,+ SPR_ITEM_19,+ SPR_ITEM_20,+ SPR_ITEM_21,+ SPR_ITEM_22,+ SPR_ITEM_23,+ SPR_ITEM_24,+ SPR_ITEM_25,+ SPR_ITEM_26,+ SPR_ITEM_27,+ SPR_ITEM_28,+ SPR_ITEM_29,+ SPR_ITEM_30,+ SPR_ITEM_31,+ SPR_ITEM_32,+ SPR_ITEM_33,+ SPR_ITEM_34,+ SPR_ITEM_35,+ SPR_ITEM_36,+ SPR_ITEM_37,+ SPR_ITEM_38,+ SPR_ITEM_39,+ SPR_ITEM_41,+ SPR_ITEM_42,+ SPR_ITEM_44,+ SPR_ITEM_45,+ SPR_ITEM_46,+ SPR_ITEM_47,+ SPR_ITEM_48,+ SPR_ITEM_49,+ SPR_ITEM_50,+ SPR_ITEM_51,+ SPR_ITEM_52,+ SPR_ITEM_53,+ SPR_ITEM_54,+ SPR_ITEM_55,+ SPR_ITEM_56,+ SPR_ITEM_57,+ SPR_ITEM_58,+ SPR_ITEM_60,+ SPR_ITEM_62,+ SPR_ITEM_63,+ SPR_ITEM_64,+ SPR_ITEM_65,+ SPR_ITEM_66,+ SPR_ITEM_67,+ SPR_ITEM_68,+ SPR_ITEM_69,+ SPR_ITEM_70,+ SPR_ITEM_71,+ SPR_ITEM_72,+ SPR_ITEM_73,+ SPR_ITEM_74,+ SPR_ITEM_75,+ SPR_ITEM_76,+ SPR_ITEM_77,+ SPR_ITEM_78,+ SPR_ITEM_79,+ SPR_ITEM_80,+ SPR_ITEM_81,+ SPR_ITEM_82,+ SPR_ITEM_83,+ SPR_ITEM_84,+ SPR_ITEM_85,+ SPR_ITEM_86,+ SPR_ITEM_87,+ SPR_ITEM_88,+ SPR_ITEM_89,+ SPR_ITEM_90,+ SPR_ITEM_91,+ SPR_ITEM_92,+ SPR_ITEM_94,+ SPR_ITEM_95,+ SPR_ITEM_96,+ SPR_ITEM_97,+ SPR_ITEM_98,+ SPR_ITEM_99,+ SPR_ITEM_100,+ SPR_ITEM_101,+ SPR_ITEM_102,+ SPR_ITEM_103,+ SPR_ITEM_104,+ SPR_ITEM_105,+ SPR_ITEM_106,+ SPR_ITEM_108,+ SPR_ITEM_109,+ SPR_ITEM_110,+ SPR_ITEM_111,+ SPR_ITEM_112,+ SPR_ITEM_113,+ SPR_ITEM_114,+ SPR_ITEM_115,+ SPR_ITEM_117,+ SPR_ITEM_118,+ SPR_ITEM_119,+ SPR_ITEM_120,+ SPR_ITEM_121,+ SPR_ITEM_122,+ SPR_ITEM_123,+ SPR_ITEM_124,+ SPR_ITEM_126,+ SPR_ITEM_127,+ SPR_ITEM_128,+ SPR_ITEM_129,+ SPR_ITEM_130,+ SPR_ITEM_131,+ SPR_ITEM_132,+ SPR_ITEM_133,+ SPR_ITEM_134,+ SPR_ITEM_135,+ SPR_ITEM_136,+ SPR_ITEM_137,+ SPR_ITEM_138,+ SPR_ITEM_140,+ SPR_ITEM_142,+ SPR_ITEM_143,+ SPR_ITEM_144,+ SPR_ITEM_145,+ SPR_ITEM_146,+ SPR_ITEM_147,+ SPR_ITEM_149,+ SPR_ITEM_150,+ SPR_ITEM_152,+ SPR_ITEM_153,+ SPR_ITEM_154,+ SPR_ITEM_155,+ SPR_ITEM_157,+ SPR_ITEM_158,+ SPR_ITEM_159,+ SPR_ITEM_160,+ SPR_ITEM_161,+ SPR_ITEM_162,+ SPR_ITEM_163,+ SPR_ITEM_164,+ SPR_ITEM_165,+ SPR_ITEM_166,+ SPR_ITEM_167,+ SPR_ITEM_169,+ SPR_ITEM_170,+ SPR_ITEM_171,+ SPR_ITEM_173,+ SPR_ITEM_174,+ SPR_ITEM_175,+ SPR_ITEM_176,+ SPR_ITEM_177,+ SPR_ITEM_178,+ SPR_ITEM_179,+ SPR_ITEM_182,+ SPR_ITEM_183,+ SPR_ITEM_184,+ SPR_ITEM_185,+ SPR_ITEM_186,+ SPR_ITEM_188,+ SPR_ITEM_189,+ SPR_ITEM_190,+ SPR_ITEM_192,+ SPR_ITEM_193,+ SPR_ITEM_194,+ SPR_ITEM_196,+ SPR_ITEM_197,+ SPR_ITEM_199,+ SPR_ITEM_201,+ SPR_ITEM_245,+ SPR_ITEM_261,+ SPR_SLOT_MACHINE,+ SPR_BLOOD_MACHINE,+ SPR_FORTUNE_MACHINE,+ SPR_BEGGAR,+ SPR_DEVIL_BEGGAR,+ SPR_KEY_MASTER,+ SPR_BOMB_BUM,+ SPR_SHOPKEEPER,+ SPR_HANGING_KEEPER,+ SPR_BOMB,+ SPR_BIG_BOMB,+ SPR_TROLL_BOMB_LIT,+ SPR_POISON_BOMB,+ SPR_SMOKE,+ SPR_HUD_HEART,+ SPR_HUD_ICON,+ SPR_MAP_ICON,+ SPR_DOOR,+ SPR_DOOR_SIDE_NORMAL,+ SPR_DOOR_SIDE_TREASURE,+ SPR_DOOR_SIDE_BOSS,+ SPR_DOOR_SIDE_SECRET,+ SPR_DOOR_SIDE_SHOP,+ SPR_DOOR_SIDE_CURSE,+ SPR_DOOR_SIDE_DEVIL,+ SPR_DOOR_SIDE_ANGEL,+ SPR_DOOR_SIDE_ARCADE,+ SPR_COUNT+};++enum {+ BD_BASEMENT,+ BD_CELLAR,+ BD_CAVES,+ BD_CATACOMBS,+ BD_DEPTHS,+ BD_NECROPOLIS,+ BD_WOMB,+ BD_UTERO,+ BD_SHEOL,+ BD_CATHEDRAL,+ BD_CHEST,+ BD_DARKROOM,+ BD_SCARRED,+ BD_LIBRARY,+ BD_SHOP,+ BD_ISAACS_ROOM,+ BD_BARREN,+ BD_ARCADE,+ BD_SECRET,+ BD_SACRIFICE,+ BD_COUNT+};++#define FONT_UPHEAVAL 0++enum {+ CELL_EMPTY,+ CELL_ROCK,+ CELL_BLOCK,+ CELL_TINTED,+ CELL_BOMB_ROCK,+ CELL_POOP,+ CELL_CORNY_POOP,+ CELL_PIT,+ CELL_SPIKES,+ CELL_TNT,+ CELL_KEY_BLOCK,+ CELL_COBWEB,+ CELL_FIRE,+ CELL_RED_FIRE,+ CELL_SLOT1,+ CELL_SLOT2,+ CELL_SLOT3,+ CELL_SLOT4,+ CELL_PICKUP,+ CELL_CHEST,+ CELL_ITEM,+ CELL_RED_POOP,+ CELL_GOLDEN_POOP,+ CELL_COUNT+};+#define CELL_MON 64++enum {+ MON_GAPER,+ MON_FROWNING_GAPER,+ MON_GUSHER,+ MON_PACER,+ MON_HORF,+ MON_FLY,+ MON_POOTER,+ MON_SUPER_POOTER,+ MON_CLOTTY,+ MON_CLOT,+ MON_IBLOB,+ MON_MULLIGAN,+ MON_ATTACK_FLY,+ MON_MAGGOT,+ MON_HIVE,+ MON_CHARGER,+ MON_GLOBIN,+ MON_BOOM_FLY,+ MON_RED_BOOM_FLY,+ MON_MAW,+ MON_RED_MAW,+ MON_HOST,+ MON_RED_HOST,+ MON_HOPPER,+ MON_TRITE,+ MON_BOIL,+ MON_SPITTY,+ MON_BRAIN,+ MON_LEAPER,+ MON_MR_MAW,+ MON_BABY,+ MON_ANGELIC_BABY,+ MON_VIS,+ MON_GUTS,+ MON_KNIGHT,+ MON_SELFLESS_KNIGHT,+ MON_LEECH,+ MON_KAMIKAZE_LEECH,+ MON_PARA_BITE,+ MON_EYE,+ MON_BLOODSHOT_EYE,+ MON_SUCKER,+ MON_SPIT,+ MON_EMBRYO,+ MON_MOTER,+ MON_SPIDER,+ MON_KEEPER,+ MON_GURGLE,+ MON_WALKING_BOIL,+ MON_BUTTLICKER,+ MON_HANGER,+ MON_SWARMER,+ MON_HEART,+ MON_MASK,+ MON_BIG_SPIDER,+ MON_NEST,+ MON_BABY_LONG_LEGS,+ MON_FATTY,+ MON_PALE_FATTY,+ MON_FAT_SACK,+ MON_DEATHS_HEAD,+ MON_MOMS_HAND,+ MON_DIP,+ MON_SQUIRT,+ MON_SKINNY,+ MON_BONY,+ MON_ONE_TOOTH,+ MON_GAPING_MAW,+ MON_RAGLING,+ MON_NIGHT_CRAWLER,+ MON_DART_FLY,+ MON_FAT_BAT,+ MON_IMP,+ MON_TUMOR,+ MON_POKY,+ MON_STONE_GRIMACE,+ MON_CONJOINED_FATTY,+ MON_ROUND_WORM,+ MON_WALL_CREEP,+ MON_MEMBRAIN,+ MON_LUMP,+ MON_FRED,+ MON_HOMUNCULUS,+ MON_BEGOTTEN,+ MON_CHUBBER,+ MON_FLAMING_HOPPER,+ MON_BLUE_FLY,+ MON_MONSTRO,+ MON_LARRY,+ MON_THE_HOLLOW,+ MON_DUKE,+ MON_HUSK,+ MON_GEMINI,+ MON_GEMINI_BABY,+ MON_STEVEN,+ MON_STEVEN_BABY,+ MON_BLIGHTED_OVUM,+ MON_GURDY,+ MON_GURDY_JR,+ MON_PEEP,+ MON_BLOAT,+ MON_CHUB,+ MON_CARRION_QUEEN,+ MON_LOKI,+ MON_LOKII,+ MON_MOM,+ MON_MOMS_HEART,+ MON_IT_LIVES,+ MON_FAMINE,+ MON_PESTILENCE,+ MON_WAR,+ MON_CONQUEST,+ MON_DEATH,+ MON_PIN,+ MON_SCOLEX,+ MON_FRAIL,+ MON_MONSTRO2,+ MON_GISH,+ MON_MASK_OF_INFAMY,+ MON_HEART_OF_INFAMY,+ MON_WIDOW,+ MON_WRETCHED,+ MON_DADDY_LONG_LEGS,+ MON_TRIACHNID,+ MON_FISTULA,+ MON_TERATOMA,+ MON_BLASTOCYST,+ MON_SATAN,+ MON_FALLEN,+ MON_KRAMPUS,+ MON_ISAAC,+ MON_HEADLESS_HORSEMAN,+ MON_DINGLE,+ MON_HAUNT,+ MON_LIL_HAUNT,+ MON_MEGA_FATTY,+ MON_MAMA_GURDY,+ MON_DARK_ONE,+ MON_POLYCEPHALUS,+ MON_MR_FRED,+ MON_GURGLING,+ MON_SLOTH,+ MON_LUST,+ MON_WRATH,+ MON_GLUTTONY,+ MON_GREED,+ MON_ENVY,+ MON_PRIDE,+ MON_MOM_HAND,+ MON_MOM_FOOT,+ MON_SATAN_STOMP,+ MON_DEATH_SCYTHE,+ MON_COUNT+};++enum {+ ROOM_NORMAL,+ ROOM_START,+ ROOM_TREASURE,+ ROOM_SHOP,+ ROOM_BOSS,+ ROOM_SECRET,+ ROOM_SUPERSECRET,+ ROOM_CURSE,+ ROOM_ARCADE,+ ROOM_SACRIFICE,+ ROOM_LIBRARY,+ ROOM_MINIBOSS,+ ROOM_CHALLENGE,+ ROOM_DEVIL,+ ROOM_ANGEL,+ ROOM_KIND_COUNT+};++/* the monster groups: chapters, then the monster of each slot (MON_COUNT: none) */+#define GROUP_COUNT 49+#define GROUP_TABLE {{1,MON_GAPER,MON_FLY,MON_HORF,MON_GLOBIN},{1,MON_FROWNING_GAPER,MON_ATTACK_FLY,MON_POOTER,MON_MULLIGAN},{1,MON_HOPPER,MON_FLY,MON_CLOTTY,MON_BOOM_FLY},{1,MON_GAPER,MON_POOTER,MON_HOST,MON_GUSHER},{1,MON_SPIDER,MON_ATTACK_FLY,MON_HORF,MON_BIG_SPIDER},{1,MON_MAGGOT,MON_FLY,MON_CLOT,MON_CHARGER},{1,MON_DIP,MON_POOTER,MON_SQUIRT,MON_SQUIRT},{1,MON_PACER,MON_MOTER,MON_MAW,MON_GLOBIN},{1,MON_GUSHER,MON_SUCKER,MON_HOST,MON_HIVE},{1,MON_GAPER,MON_ATTACK_FLY,MON_CLOTTY,MON_MULLIGAN},{1,MON_HOPPER,MON_POOTER,MON_HORF,MON_BIG_SPIDER},{1,MON_SPIDER,MON_FLY,MON_HOST,MON_BOOM_FLY},{1,MON_FROWNING_GAPER,MON_FLY,MON_MAW,MON_FATTY},{2,MON_HOPPER,MON_SUCKER,MON_HOST,MON_KNIGHT},{2,MON_TRITE,MON_MOTER,MON_MAW,MON_LEAPER},{2,MON_CHARGER,MON_BOOM_FLY,MON_CLOTTY,MON_FATTY},{2,MON_GLOBIN,MON_ATTACK_FLY,MON_BOIL,MON_MR_MAW},{2,MON_SPIDER,MON_DART_FLY,MON_SPITTY,MON_BIG_SPIDER},{2,MON_SKINNY,MON_POOTER,MON_HORF,MON_WALKING_BOIL},{2,MON_GURGLE,MON_SWARMER,MON_IBLOB,MON_BUTTLICKER},{2,MON_BONY,MON_FAT_BAT,MON_RED_HOST,MON_KNIGHT},{2,MON_NIGHT_CRAWLER,MON_SUCKER,MON_TUMOR,MON_HIVE},{2,MON_GAPER,MON_SUPER_POOTER,MON_CLOT,MON_FAT_SACK},{2,MON_TRITE,MON_BOOM_FLY,MON_SPITTY,MON_PALE_FATTY},{2,MON_RAGLING,MON_MOTER,MON_HOST,MON_CHUBBER},{4,MON_KNIGHT,MON_RED_BOOM_FLY,MON_RED_MAW,MON_SELFLESS_KNIGHT},{4,MON_BONY,MON_FAT_BAT,MON_RED_HOST,MON_VIS},{4,MON_BABY,MON_MOTER,MON_EYE,MON_FRED},{4,MON_LEAPER,MON_SWARMER,MON_BRAIN,MON_HANGER},{4,MON_IMP,MON_SUCKER,MON_BLOODSHOT_EYE,MON_GAPING_MAW},{4,MON_DEATHS_HEAD,MON_RED_BOOM_FLY,MON_STONE_GRIMACE,MON_HOMUNCULUS},{4,MON_GUTS,MON_ATTACK_FLY,MON_MR_MAW,MON_BEGOTTEN},{4,MON_GLOBIN,MON_DART_FLY,MON_ONE_TOOTH,MON_KNIGHT},{4,MON_SKINNY,MON_MOTER,MON_RED_HOST,MON_PALE_FATTY},{4,MON_RAGLING,MON_SWARMER,MON_SPITTY,MON_CONJOINED_FATTY},{8,MON_GUTS,MON_SWARMER,MON_EYE,MON_COUNT},{8,MON_PARA_BITE,MON_LEECH,MON_BLOODSHOT_EYE,MON_FRED},{8,MON_KAMIKAZE_LEECH,MON_EMBRYO,MON_LUMP,MON_MEMBRAIN},{8,MON_BABY,MON_HEART,MON_VIS,MON_WALL_CREEP},{8,MON_GURGLE,MON_LEECH,MON_NEST,MON_CONJOINED_FATTY},{8,MON_ROUND_WORM,MON_SUCKER,MON_EYE,MON_VIS},{8,MON_GUTS,MON_BOOM_FLY,MON_BLOODSHOT_EYE,MON_BEGOTTEN},{8,MON_PARA_BITE,MON_SWARMER,MON_LUMP,MON_FAT_SACK},{8,MON_BABY,MON_LEECH,MON_TUMOR,MON_HOMUNCULUS},{16,MON_IMP,MON_RED_BOOM_FLY,MON_ONE_TOOTH,MON_BEGOTTEN},{16,MON_BABY,MON_FAT_BAT,MON_BLOODSHOT_EYE,MON_KNIGHT},{16,MON_ANGELIC_BABY,MON_SWARMER,MON_EYE,MON_SELFLESS_KNIGHT},{16,MON_DEATHS_HEAD,MON_ATTACK_FLY,MON_STONE_GRIMACE,MON_VIS},{16,MON_BONY,MON_LEECH,MON_RED_HOST,MON_FRED},}+#define ITEM_SPRITE_COUNT 180+#define ITEM_ID_TABLE {1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,41,42,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,60,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,94,95,96,97,98,99,100,101,102,103,104,105,106,108,109,110,111,112,113,114,115,117,118,119,120,121,122,123,124,126,127,128,129,130,131,132,133,134,135,136,137,138,140,142,143,144,145,146,147,149,150,152,153,154,155,157,158,159,160,161,162,163,164,165,166,167,169,170,171,173,174,175,176,177,178,179,182,183,184,185,186,188,189,190,192,193,194,196,197,199,201,245,261}++#endifdiff --git a/games/isaac/src/ents.c b/games/isaac/src/ents.cnew file mode 100644index 00000000..131bac2a--- /dev/null+++ b/games/isaac/src/ents.c@@ -0,0 +1,207 @@+/* The room's things: making them, updating them, hurting monsters, and+ * drawing all that stands in the room by its depth, Isaac among it. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++Ent ents[MAX_ENTS];++void grid_draw_row(int r);+void pedestal_update(Ent *e);+void pedestal_draw(Ent *e);+void pickup_draw(Ent *e);+void machine_draw(Ent *e);+void fx_update(Ent *e);+void fx_draw(Ent *e);+void bomb_update(Ent *e);+void creep_update(Ent *e);+void trapdoor_update(Ent *e);++Ent *ent_new(int kind, float x, float y) {+ for (int i = 0; i < MAX_ENTS; i++)+ if (!ents[i].kind) {+ Ent *e = &ents[i];+ memset(e, 0, sizeof *e);+ e->kind = (uint8_t)kind, e->x = x, e->y = y, e->alpha = 32, e->parent = e->child = -1;+ return e;+ }+ return NULL;+}++void ents_clear(void) { memset(ents, 0, sizeof ents); }++/* monsters that keep the doors shut (stone turrets, Poky and Death's Heads don't) */+int ents_alive_monsters(void) {+ int n = 0;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind == EK_MON && !(e->flags & (EF_FRIEND | EF_NOCOUNT))) n++;+ }+ return n;+}++Ent *ent_nearest_monster(float x, float y, float maxd) {+ Ent *best = NULL;+ float bd = maxd * maxd;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || (e->flags & (EF_FRIEND | EF_HIDDEN | EF_INVULN))) continue;+ float d = dist2(x, y, e->x, e->y);+ if (d < bd) bd = d, best = e;+ }+ return best;+}++void ent_kill(Ent *e) {+ if (e->kind != EK_MON) return;+ if (boss_on_death(e)) return;+ if (!mon_on_death(e)) return;+ bool flying = e->flags & EF_FLYING;+ decal_blood(e->x, e->y, e->r * (flying ? 0.8f : 1.2f));+ for (int i = 0; i < 3 + (int)(e->r / 4); i++) fx_spawn(FX_GIB, e->x, e->y - 4, 16 + rnd(10));+ if (e->flags & EF_CHAMPION) pickup_drop_random(e->x, e->y);+ /* Charm of the Vampire: half a heart every thirteen kills */+ if (pl.has[62] && ++pl.vampire >= 13) pl.vampire = 0, player_heal(1);+ e->kind = EK_NONE;+}++void ent_damage(Ent *e, float dmg, uint32_t flags, float kx, float ky) {+ (void)flags;+ if (e->kind != EK_MON || (e->flags & EF_INVULN) || e->hp <= 0) return;+ e->hp -= dmg;+ e->flash = 3;+ if (!(e->flags & (EF_BOSS | EF_NOKNOCK))) e->vx += kx, e->vy += ky;+ if (e->hp <= 0) ent_kill(e);+}++/* ---------------------------------------------------------------- updating */+static void touch_player(Ent *e) {+ if (e->flags & (EF_FRIEND | EF_HIDDEN | EF_NOHIT) || e->z < -12) return;+ float rr = e->r + 5, dx = pl.x - e->x, dy = pl.y - e->y;+ if (dx * dx + dy * dy < rr * rr) {+ if (pl.unicorn > 0 || pl.nail > 0) { /* (Unicorn, the Nail: he hurts them instead) */+ ent_damage(e, 40.0f / FPS * 3, 0, -dx * 0.2f, -dy * 0.2f);+ return;+ }+ /* half a heart, a whole one from the Womb (and from the Depths' bosses) */+ player_hurt(fl.stage >= 7 || ((e->flags & EF_BOSS) && fl.stage >= 5) ? 2 : 1, e->type);+ }+}++void ents_update(void) {+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ switch (e->kind) {+ case EK_MON:+ if (e->flash) e->flash--;+ /* poison, slowness, freezing: what tears did */+ if (e->poison) {+ e->poison--;+ if (e->poison % 15 == 0) ent_damage(e, e->poison_dmg > 0 ? e->poison_dmg : 2, 0, 0, 0);+ if (e->kind != EK_MON) break;+ }+ if (e->freeze) {+ e->freeze--;+ touch_player(e);+ break;+ }+ if (e->slow) e->slow--;+ if (e->confuse) e->confuse--;+ if (e->fear) e->fear--;+ if (pl.hourglass > 0 && (g_frame & 1)) break;+ if (e->slow && (g_frame & 1)) break;+ if (!boss_update(e)) mon_update(e);+ if (e->kind == EK_MON) touch_player(e);+ break;+ case EK_PICKUP: pickup_update(e); break;+ case EK_PEDESTAL: pedestal_update(e); break;+ case EK_MACHINE: machine_update(e); break;+ case EK_BOMB: bomb_update(e); break;+ case EK_FX: fx_update(e); break;+ case EK_FAMILIAR: familiar_update(e); break;+ case EK_CREEP: creep_update(e); break;+ case EK_TRAPDOOR: trapdoor_update(e); break;+ default: break;+ }+ }+ /* creep under Isaac's feet (Lemon Mishap's is his own) */+}++/* ---------------------------------------------------------------- drawing by depth */+typedef struct { int16_t y, what; } Draw; /* what: -1 Isaac, -2 - r a grid row, else an ent */++void ents_draw(void) {+ /* first what lies flat: decals, creep, the trapdoor */+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind == EK_DECAL) {+ if (e->a > 0) {+ gfx_ellipse((int)e->x, (int)e->y, (int)e->a, (int)(e->a * 0.55f), rgb565(0x7A0A0A), 20);+ gfx_ellipse((int)e->x + 2, (int)e->y - 1, (int)(e->a * 0.5f), (int)(e->a * 0.3f), rgb565(0x9A1010), 22);+ } else+ gfx_ellipse((int)e->x, (int)e->y, (int)-e->a, (int)(-e->a * 0.55f), C_BLACK, 14);+ } else if (e->kind == EK_CREEP) {+ static const uint32_t col[4] = {0xB01818, 0x40B020, 0xE0D040, 0xF0F0F0};+ int a = e->t < 20 ? e->t : 20;+ gfx_ellipse((int)e->x, (int)e->y, (int)e->r, (int)(e->r * 0.6f), rgb565(col[e->type & 3]), a);+ } else if (e->kind == EK_TRAPDOOR) {+ if (e->type == 1) { /* the beam of light to the Cathedral */+ gfx_ellipse((int)e->x, (int)e->y, 14, 6, C_WHITE, 20);+ } else+ gfx_sprite(SPR_TRAPDOOR, 0, (int)e->x, (int)e->y, 0, 32);+ }+ }+ /* shadows */+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if ((e->kind == EK_MON && !(e->flags & EF_HIDDEN)) || e->kind == EK_PICKUP || e->kind == EK_FAMILIAR || e->kind == EK_BOMB) {+ int rx = (int)(e->r * 0.9f) + 1;+ if (e->z < -30) rx = rx * 2 / 3;+ gfx_ellipse((int)e->x, (int)e->y, rx, rx / 3 + 1, C_BLACK, 11);+ }+ }+ player_draw_shadow();+ /* then everything standing, the deepest first */+ static Draw list[MAX_ENTS + ROWS + 2];+ int n = 0;+ for (int r = 0; r < ROWS; r++) list[n].y = (int16_t)(ROOM_Y + r * TILE + TILE / 2 + 2), list[n++].what = (int16_t)(-2 - r);+ list[n].y = (int16_t)pl.y, list[n++].what = -1;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (!e->kind || e->kind == EK_DECAL || e->kind == EK_CREEP || e->kind == EK_TRAPDOOR) continue;+ if (e->kind == EK_FX && e->type == FX_EXPLOSION) continue; /* (drawn over all) */+ int y = (int)e->y;+ if (e->kind == EK_MON && (e->flags & EF_FLYING)) y += 6;+ list[n].y = (int16_t)y, list[n++].what = (int16_t)i;+ }+ for (int i = 1; i < n; i++) { /* (insertion sort: nearly sorted from one frame to the next) */+ Draw d = list[i];+ int j = i - 1;+ while (j >= 0 && list[j].y > d.y) list[j + 1] = list[j], j--;+ list[j + 1] = d;+ }+ for (int i = 0; i < n; i++) {+ int w = list[i].what;+ if (w == -1) {+ player_draw();+ continue;+ }+ if (w <= -2) {+ grid_draw_row(-2 - w);+ continue;+ }+ Ent *e = &ents[w];+ switch (e->kind) {+ case EK_MON: mon_draw(e); break;+ case EK_PICKUP: case EK_BOMB: pickup_draw(e); break;+ case EK_PEDESTAL: pedestal_draw(e); break;+ case EK_MACHINE: machine_draw(e); break;+ case EK_FAMILIAR: familiar_draw(e); break;+ case EK_FX: fx_draw(e); break;+ default: break;+ }+ }+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_FX && ents[i].type == FX_EXPLOSION) fx_draw(&ents[i]);+}diff --git a/games/isaac/src/floor.c b/games/isaac/src/floor.cnew file mode 100644index 00000000..61307986--- /dev/null+++ b/games/isaac/src/floor.c@@ -0,0 +1,595 @@+/* A floor: its rooms laid out as the game lays them, and going from room to room.+ *+ * The layout is the original's. From the starting room, each room just+ * added tries each of its four sides, one time in two; a new room may touch+ * no room but the one it grows from, so the floor is a tree, and the floor+ * stops at min(20, 5 + 0 or 1 + 2.6 x the floor's number) rooms. Rooms that+ * grew nothing are its dead ends: the last one is the boss's, others become+ * the treasure room, the shop and the other special rooms. The secret room+ * goes in an empty cell next to at least three rooms (or two), none of them+ * special; the super secret room next to a single one. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++Floor fl;++int chapter_of(int stage) { return stage <= 8 ? (stage + 1) / 2 : stage <= 10 ? 5 : 6; }++const char *stage_name(int stage, bool alt) {+ static const char *const names[][2] = {+ {"BASEMENT I", "CELLAR I"}, {"BASEMENT II", "CELLAR II"}, {"THE CAVES I", "CATACOMBS I"},+ {"THE CAVES II", "CATACOMBS II"}, {"THE DEPTHS I", "NECROPOLIS I"}, {"THE DEPTHS II", "NECROPOLIS II"},+ {"THE WOMB I", "UTERO I"}, {"THE WOMB II", "UTERO II"}, {"SHEOL", "CATHEDRAL"}};+ if (stage < 1) stage = 1;+ if (stage > 9) stage = 9;+ return names[stage - 1][alt ? 1 : 0];+}++int room_backdrop(void) {+ Room *r = room_cur();+ switch (r->kind) {+ case ROOM_SHOP: return BD_SHOP;+ case ROOM_SECRET: case ROOM_SUPERSECRET: return BD_SECRET;+ case ROOM_ARCADE: return BD_ARCADE;+ case ROOM_LIBRARY: return BD_LIBRARY;+ case ROOM_SACRIFICE: return BD_SACRIFICE;+ case ROOM_CURSE: return BD_BARREN;+ case ROOM_DEVIL: return BD_SHEOL;+ case ROOM_ANGEL: return BD_CATHEDRAL;+ default: break;+ }+ static const uint8_t bd[5][2] = {{BD_BASEMENT, BD_CELLAR}, {BD_CAVES, BD_CATACOMBS}, {BD_DEPTHS, BD_NECROPOLIS},+ {BD_WOMB, BD_UTERO}, {BD_SHEOL, BD_CATHEDRAL}};+ int c = chapter_of(fl.stage);+ if (c > 5) c = 5;+ return bd[c - 1][fl.alt ? 1 : 0];+}++Room *room_cur(void) { return &fl.rooms[fl.cur]; }++/* ---------------------------------------------------------------- the layout (the original's, on 10 x 8 cells) */+#define GW 10+#define GH 8+#define START 35+static int8_t cell[GW * GH];+static int ends[GW * GH], nends;+static int act[GW * GH], nact, act2[GW * GH], nact2;+static int added, target;++static int cellv(int i) { return i >= 0 && i < GW * GH ? cell[i] : 0; }+static void try_add(int i) {+ if (i < 0 || i >= GW * GH) return;+ int around = cellv(i + 1) + cellv(i - 1) + cellv(i + GW) + cellv(i - GW);+ if ((cell[i] == 0 && around < 2 && added < target) || i == START) {+ if (cell[i]) return;+ cell[i] = 1;+ act2[nact2++] = i;+ if (i != START) added++;+ }+}+static int neighbours(int i) {+ return (i % GW < GW - 1 && cellv(i + 1) > 0) + (i % GW > 0 && cellv(i - 1) > 0) + (cellv(i + GW) > 0) + (cellv(i - GW) > 0);+}+static int take_end(void) {+ if (!nends) return -1;+ int k = rnd(nends), v = ends[k];+ ends[k] = ends[--nends];+ return v;+}++static bool layout(int stage) {+ target = 5 + rnd(2) + (int)(stage * 2.6f);+ if (target > 20) target = 20;+ for (int tries = 0; tries < 1000; tries++) {+ memset(cell, 0, sizeof cell);+ nends = 0, added = 0, nact2 = 0;+ try_add(START);+ for (float e = 0; e < 300; e++) {+ nact = nact2;+ memcpy(act, act2, sizeof(int) * (size_t)nact);+ nact2 = 0;+ for (int a = 0; a < nact; a++) {+ int v = act[a], before = nact2;+ if (rnd(2) == 0 && v % GW < 9) try_add(v + 1);+ if (rnd(2) == 0 && v % GW > 1) try_add(v - 1);+ if (rnd(2) == 0 && v < 70) try_add(v + GW);+ if (rnd(2) == 0 && v > 10) try_add(v - GW);+ if (nact2 == before && v != START) ends[nends++] = v;+ }+ if (nact2 < 2) e -= 0.5f;+ if (!nact2) break;+ }+ if (added < target || nends < 3) continue;+ /* the boss: the last dead end found, and never right by the start */+ int boss = ends[nends - 1];+ if (boss == START + 1 || boss == START - 1 || boss == START + GW || boss == START - GW) continue;+ return true;+ }+ return false;+}++/* ---------------------------------------------------------------- the rooms */+static int pick_template(int kind, int chapter_bit) {+ int total = 0;+ for (int i = 0; i < room_count(); i++) {+ const RoomTemplate *t = room_get(i);+ if (t->kind == kind && (t->chapters & chapter_bit)) total += t->weight;+ }+ if (!total) return -1;+ int k = rnd(total);+ for (int i = 0; i < room_count(); i++) {+ const RoomTemplate *t = room_get(i);+ if (t->kind != kind || !(t->chapters & chapter_bit)) continue;+ if ((k -= t->weight) < 0) return i;+ }+ return -1;+}++static const uint8_t groups[GROUP_COUNT][5] = GROUP_TABLE;+static int pick_group(int tmpl, int chapter_bit) {+ const RoomTemplate *t = room_get(tmpl);+ bool slot[4] = {false};+ for (int r = 0; r < ROWS; r++)+ for (int c = 0; c < COLS; c++)+ if (t->cell[r][c] >= CELL_SLOT1 && t->cell[r][c] <= CELL_SLOT4) slot[t->cell[r][c] - CELL_SLOT1] = true;+ int ok[GROUP_COUNT], n = 0;+ for (int g = 0; g < GROUP_COUNT; g++) {+ if (!(groups[g][0] & chapter_bit)) continue;+ bool any = false;+ for (int s = 0; s < 4; s++) any |= slot[s] && groups[g][1 + s] != MON_COUNT;+ if (any) ok[n++] = g;+ }+ return n ? ok[rnd(n)] : 0;+}++static int add_room(int mx, int my, int kind) {+ if (fl.nrooms >= MAX_ROOMS) return -1;+ int i = fl.nrooms++;+ Room *r = &fl.rooms[i];+ memset(r, 0, sizeof *r);+ r->kind = (uint8_t)kind, r->mx = (uint8_t)mx, r->my = (uint8_t)my;+ if (mx >= 0 && my >= 0 && mx < MAP_W && my < MAP_H) fl.map[my][mx] = (int8_t)i;+ int cb = 1 << (chapter_of(fl.stage) - 1);+ if (cb > 16) cb = 16;+ int t = pick_template(kind, cb);+ if (t < 0) t = pick_template(ROOM_NORMAL, cb);+ r->tmpl = (uint8_t)(t < 0 ? 0 : t);+ if (kind == ROOM_NORMAL || kind == ROOM_CURSE) r->group = (uint8_t)pick_group(r->tmpl, cb);+ return i;+}++/* the floor's boss, from its chapter's (Rebirth's) */+static int pick_boss(int stage) {+ static const uint8_t ch1[] = {MON_MONSTRO, MON_LARRY, MON_DUKE, MON_GEMINI, MON_GURDY_JR, MON_FAMINE, MON_STEVEN,+ MON_WIDOW, MON_PIN, MON_HAUNT, MON_DINGLE, MON_BLIGHTED_OVUM};+ static const uint8_t ch2[] = {MON_CHUB, MON_GURDY, MON_PEEP, MON_PESTILENCE, MON_CARRION_QUEEN, MON_MEGA_FATTY,+ MON_POLYCEPHALUS, MON_HUSK};+ static const uint8_t ch3[] = {MON_MONSTRO2, MON_LOKI, MON_WAR, MON_MASK_OF_INFAMY, MON_GISH, MON_BLOAT, MON_WRETCHED,+ MON_DARK_ONE};+ static const uint8_t ch4[] = {MON_BLASTOCYST, MON_DADDY_LONG_LEGS, MON_SCOLEX, MON_DEATH, MON_LOKII, MON_CONQUEST,+ MON_MAMA_GURDY, MON_MR_FRED, MON_TRIACHNID, MON_TERATOMA};+ if (stage == 6) return MON_MOM;+ if (stage == 8) return MON_MOMS_HEART;+ if (stage == 9) return fl.alt ? MON_ISAAC : MON_SATAN;+ const uint8_t *list;+ int n;+ switch (chapter_of(stage)) {+ case 1: list = ch1, n = sizeof ch1; break;+ case 2: list = ch2, n = sizeof ch2; break;+ case 3: list = ch3, n = sizeof ch3; break;+ default: list = ch4, n = sizeof ch4; break;+ }+ return list[rnd(n)];+}++void floor_new(int stage) {+ memset(&fl, 0, sizeof fl);+ memset(fl.map, -1, sizeof fl.map);+ fl.stage = (uint8_t)stage;+ fl.seed = g_rng;+ fl.alt = stage <= 8 ? (uint8_t)rnd(2) : fl.alt;+ fl.devil_room = -1;+ if (stage > 1 && stage < 9 && rnd(4) == 0) {+ static const uint8_t curses[] = {CURSE_DARKNESS, CURSE_LOST, CURSE_UNKNOWN, CURSE_BLIND, CURSE_MAZE};+ fl.curse = curses[rnd(sizeof curses)];+ }+ if (!layout(stage)) { /* (never, in practice): a short line of rooms */+ memset(cell, 0, sizeof cell);+ for (int i = 0; i < 6; i++) cell[START + i - 2] = 1;+ nends = 0;+ ends[nends++] = START - 2, ends[nends++] = START + 3;+ }+ /* special rooms on the dead ends (the original's order and chances) */+ int kind[GW * GH];+ for (int i = 0; i < GW * GH; i++) kind[i] = cell[i] ? ROOM_NORMAL : -1;+ kind[START] = ROOM_START;+ int boss = ends[--nends];+ kind[boss] = ROOM_BOSS;+ bool deep = stage >= 9;+ int e;+ if (!deep && (e = take_end()) >= 0) kind[e] = ROOM_TREASURE;+ if (!deep && (e = take_end()) >= 0) kind[e] = ROOM_SHOP;+ if (nends && (rnd(7) == 0 || (pl.red >= pl.containers * 2 && rnd(4) == 0)) && (e = take_end()) >= 0) kind[e] = ROOM_SACRIFICE;+ if (nends && rnd(20) == 0 && (e = take_end()) >= 0) kind[e] = ROOM_LIBRARY;+ if (nends && rnd(2) == 0 && stage > 1 && (e = take_end()) >= 0) kind[e] = ROOM_CURSE;+ if (nends && (rnd(4) == 0 || (stage == 1 && rnd(3) == 0)) && stage > 1 && (e = take_end()) >= 0) kind[e] = ROOM_MINIBOSS;+ if (nends && pl.red >= pl.containers * 2 && stage > 1 && rnd(2) == 0 && (e = take_end()) >= 0) kind[e] = ROOM_CHALLENGE;+ if (nends && pl.coins >= 5 && stage % 2 == 0 && (e = take_end()) >= 0) kind[e] = ROOM_ARCADE;+ /* the map: the original's 10 x 8 on our 13 x 9, one cell in */+ for (int i = 0; i < GW * GH; i++) {+ if (kind[i] < 0) continue;+ int idx = add_room(i % GW + 1, i / GW + 1, kind[i]);+ if (i == START) fl.start = (int8_t)idx;+ if (i == boss) fl.boss_room = (int8_t)idx;+ }+ /* the secret room: the empty cell with the most rooms around (three, else two), none special */+ int best = -1, bestn = 1;+ for (int i = 0; i < GW * GH; i++) {+ if (cell[i] || i % GW < 1 || i % GW > 8) continue;+ int n = neighbours(i);+ bool ok = true;+ const int d[4] = {1, -1, GW, -GW};+ for (int k = 0; k < 4; k++) {+ int j = i + d[k];+ if (j < 0 || j >= GW * GH || (k < 2 && (j / GW != i / GW))) continue;+ if (cell[j] && kind[j] != ROOM_NORMAL && kind[j] != ROOM_START) ok = false;+ }+ if (ok && (n > bestn || (n == bestn && rnd(3) == 0))) best = i, bestn = n;+ }+ if (best >= 0) {+ cell[best] = 2;+ add_room(best % GW + 1, best / GW + 1, ROOM_SECRET);+ }+ /* the super secret room: next to a single normal room */+ for (int tries = 0; tries < 200; tries++) {+ int i = rnd(GW * GH);+ if (cell[i] || i % GW < 1 || i % GW > 8 || neighbours(i) != 1) continue;+ const int d[4] = {1, -1, GW, -GW};+ bool ok = true;+ for (int k = 0; k < 4; k++) {+ int j = i + d[k];+ if (j >= 0 && j < GW * GH && cell[j] && kind[j] != ROOM_NORMAL) ok = false;+ }+ if (!ok) continue;+ cell[i] = 3;+ add_room(i % GW + 1, i / GW + 1, ROOM_SUPERSECRET);+ break;+ }+ fl.boss = (uint8_t)pick_boss(stage);+ fl.rooms[fl.boss_room].boss = fl.boss;+ fl.cur = fl.start;+}++/* ---------------------------------------------------------------- doors */+static int room_at(int mx, int my) {+ if (mx < 0 || my < 0 || mx >= MAP_W || my >= MAP_H) return -1;+ return fl.map[my][mx];+}++bool room_door_exists(int d, int *kind) {+ Room *r = room_cur();+ if (fl.cur == fl.devil_room) { /* the Devil's (or an angel's) room: its one door, down */+ if (kind) *kind = ROOM_BOSS;+ return d == DIR_DOWN;+ }+ if (r->mx == 0 && r->my == 0) return false;+ int j = room_at(r->mx + DIR_X[d], r->my + DIR_Y[d]);+ if (fl.cur == fl.boss_room && d == DIR_UP && fl.devil_open && j < 0) {+ if (kind) *kind = fl.devil_angel ? ROOM_ANGEL : ROOM_DEVIL;+ return true;+ }+ if (j < 0) return false;+ int k = fl.rooms[j].kind;+ if (kind) *kind = k;+ /* a secret room's walls hide their doors until a bomb opens them; from inside they show */+ if ((k == ROOM_SECRET || k == ROOM_SUPERSECRET) && !(r->doors_open & (1 << d))) return false;+ if ((r->kind == ROOM_SECRET || r->kind == ROOM_SUPERSECRET) && k != ROOM_SECRET) {+ if (kind) *kind = ROOM_SECRET;+ }+ return true;+}++/* the kind of lock a door has: 0 none, 1 a key (treasure rooms and shops from the second floor), 2 two coins+ * (the arcade), 3 full health (a challenge room) */+static int door_lock(int d) {+ int k;+ if (!room_door_exists(d, &k)) return 0;+ if (room_cur()->doors_open & (1 << d)) return 0;+ if ((k == ROOM_TREASURE || k == ROOM_SHOP) && fl.stage > 1) return 1;+ if (k == ROOM_LIBRARY) return 1;+ return 0;+}++bool room_door_open(int d) {+ if (ents_alive_monsters()) return false;+ return door_lock(d) == 0;+}++void room_open_doors(void) {}++static void open_both(int d) {+ Room *r = room_cur();+ r->doors_open |= (uint8_t)(1 << d);+ int j = room_at(r->mx + DIR_X[d], r->my + DIR_Y[d]);+ if (j >= 0) fl.rooms[j].doors_open |= (uint8_t)(1 << ((d + 2) & 3));+}++/* he walks into a locked door: a key opens it */+bool door_try_unlock(int d) {+ if (door_lock(d) != 1) return false;+ if (pl.has[175]) { /* (Dad's Key opens them all) */+ open_both(d);+ return true;+ }+ if (pl.keys == 0) return false;+ pl.keys--;+ open_both(d);+ return true;+}++/* a bomb by a wall: a secret room's door, if there's one behind */+void door_bombed(float x, float y) {+ Room *r = room_cur();+ static const float dx[4] = {160, ROOM_X, 160, ROOM_R}, dy[4] = {ROOM_B, 120, ROOM_Y, 120};+ for (int d = 0; d < 4; d++) {+ float ex = x - dx[d], ey = y - dy[d];+ if (ex * ex + ey * ey > 34 * 34) continue;+ int j = room_at(r->mx + DIR_X[d], r->my + DIR_Y[d]);+ if (j < 0) continue;+ int k = fl.rooms[j].kind;+ if ((k == ROOM_SECRET || k == ROOM_SUPERSECRET) && !(r->doors_open & (1 << d))) {+ open_both(d);+ fl.rooms[j].state |= RS_SEEN;+ fx_spawn(FX_POOF, dx[d], dy[d], 12);+ }+ }+}++/* ---------------------------------------------------------------- what a room holds */+static const uint8_t cell_grid[CELL_COUNT] = {+ [CELL_EMPTY] = CELL_EMPTY, [CELL_ROCK] = CELL_ROCK, [CELL_BLOCK] = CELL_BLOCK, [CELL_TINTED] = CELL_TINTED,+ [CELL_BOMB_ROCK] = CELL_BOMB_ROCK, [CELL_POOP] = CELL_POOP, [CELL_CORNY_POOP] = CELL_CORNY_POOP, [CELL_PIT] = CELL_PIT,+ [CELL_SPIKES] = CELL_SPIKES, [CELL_TNT] = CELL_TNT, [CELL_KEY_BLOCK] = CELL_KEY_BLOCK, [CELL_COBWEB] = CELL_COBWEB,+ [CELL_FIRE] = CELL_FIRE, [CELL_RED_FIRE] = CELL_RED_FIRE,+};++static float cell_x(int c) { return ROOM_X + c * TILE + TILE / 2; }+static float cell_y(int r) { return ROOM_Y + r * TILE + TILE / 2; }++static void room_build_grid(Room *r) {+ const RoomTemplate *t = room_get(r->tmpl);+ for (int y = 0; y < ROWS; y++)+ for (int x = 0; x < COLS; x++) {+ int c = t->cell[y][x];+ int g = c < CELL_COUNT ? cell_grid[c] : CELL_EMPTY;+ if (g == CELL_ROCK && rnd(30) == 0) g = CELL_TINTED; /* a marked rock, now and then */+ if (g == CELL_POOP && rnd(12) == 0 && fl.stage > 2) g = CELL_RED_POOP;+ if (g == CELL_POOP && rnd(60) == 0) g = CELL_GOLDEN_POOP;+ r->grid[y][x] = (uint8_t)g;+ r->gridhp[y][x] = (g == CELL_POOP || g == CELL_CORNY_POOP || g == CELL_RED_POOP || g == CELL_GOLDEN_POOP) ? 4+ : (g == CELL_FIRE || g == CELL_RED_FIRE) ? 4 : 0;+ }+}++/* the monsters of a room's layout, in its group */+static void room_spawn_monsters(Room *r) {+ const RoomTemplate *t = room_get(r->tmpl);+ for (int y = 0; y < ROWS; y++)+ for (int x = 0; x < COLS; x++) {+ int c = t->cell[y][x], m = -1;+ if (c >= CELL_SLOT1 && c <= CELL_SLOT4) m = groups[r->group][1 + c - CELL_SLOT1];+ else if (c >= CELL_MON) m = c - CELL_MON;+ if (m < 0 || m >= MON_COUNT) continue;+ Ent *e = mon_spawn(m, cell_x(x), cell_y(y));+ /* a champion, one time in twenty (Champion Belt: one in five) */+ if (e && !(e->flags & EF_BOSS) && rnd(pl.has[208] ? 5 : 20) == 0) {+ e->flags |= EF_CHAMPION;+ e->sub = (uint8_t)rnd(4);+ e->hp = e->maxhp = e->maxhp * (e->sub == 0 ? 2.0f : 1.5f);+ }+ }+}++static void room_spawn_first(Room *r) {+ const RoomTemplate *t = room_get(r->tmpl);+ for (int y = 0; y < ROWS; y++)+ for (int x = 0; x < COLS; x++) {+ int c = t->cell[y][x];+ if (c == CELL_PICKUP) pickup_drop_random(cell_x(x), cell_y(y));+ else if (c == CELL_CHEST) pickup_spawn(r->kind == ROOM_CURSE ? PK_RED_CHEST : rnd(5) == 0 ? PK_LOCKED_CHEST : PK_CHEST,+ 0, cell_x(x), cell_y(y), false);+ else if (c == CELL_ITEM) {+ int pool = r->kind == ROOM_SECRET ? POOL_SECRET : r->kind == ROOM_SUPERSECRET ? (rnd(2) ? POOL_DEVIL : POOL_ANGEL)+ : r->kind == ROOM_CURSE ? POOL_CURSE : POOL_TREASURE;+ pedestal_spawn(item_from_pool(pool), cell_x(x), cell_y(y), 0, false);+ }+ }+ float mx = 160;+ switch (r->kind) {+ case ROOM_SHOP: {+ machine_spawn(MA_SHOPKEEPER, mx, ROOM_Y + 24);+ /* Rebirth's shop: an item, then pickups at their prices */+ static const float sx[5] = {80, 120, 160, 200, 240};+ int n = fl.stage <= 2 ? 3 : 5;+ int first = n == 3 ? 1 : 0;+ for (int i = 0; i < n; i++) {+ float x = sx[first + i], y = 135;+ if (i == 0 || (n == 5 && i == 4 && rnd(3) == 0)) {+ int it = item_from_pool(POOL_SHOP);+ pedestal_spawn(it, x, y, pl.has[64] ? 8 : 15, false);+ } else {+ static const uint8_t wares[] = {PK_HEART, PK_SOUL, PK_BOMB, PK_KEY, PK_PILL, PK_CARD, PK_DOUBLE_BOMB, PK_GRAB_BAG};+ int k = wares[rnd(sizeof wares)];+ int price = k == PK_HEART ? 3 : k == PK_GRAB_BAG ? 7 : 5;+ Ent *p = pickup_spawn(k, 0, x, y, false);+ if (p) p->price = (int16_t)(pl.has[64] ? (price + 1) / 2 : price), p->flags |= EF_SHOP;+ }+ }+ break;+ }+ case ROOM_ARCADE:+ machine_spawn(MA_SLOT, 100, 90);+ machine_spawn(MA_SLOT, 220, 90);+ machine_spawn(rnd(2) ? MA_BLOOD : MA_FORTUNE, 160, 80);+ machine_spawn(rnd(3) ? MA_BEGGAR : MA_BOMB_BUM, 100, 160);+ if (rnd(2)) machine_spawn(MA_SLOT, 220, 160);+ break;+ case ROOM_SACRIFICE:+ r->grid[3][6] = CELL_SPIKES;+ break;+ case ROOM_LIBRARY:+ pedestal_spawn(item_from_pool(POOL_LIBRARY), 120, 120, 0, false);+ pedestal_spawn(item_from_pool(POOL_LIBRARY), 200, 120, 0, false);+ break;+ case ROOM_DEVIL: {+ machine_spawn(MA_DEVIL_STATUE, mx, ROOM_Y + 22);+ int n = 1 + rnd(3);+ static const float px[3] = {110, 160, 210};+ for (int i = 0; i < n; i++) {+ int it = item_from_pool(POOL_DEVIL);+ pedestal_spawn(it, n == 1 ? 160 : n == 2 ? (i ? 190 : 130) : px[i], 125, 0, true);+ }+ break;+ }+ case ROOM_ANGEL:+ machine_spawn(MA_ANGEL_STATUE, mx, ROOM_Y + 22);+ pedestal_spawn(item_from_pool(POOL_ANGEL), 160, 125, 0, false);+ break;+ default: break;+ }+}++static void room_spawn_specials(Room *r) {+ if (r->kind == ROOM_BOSS && !(r->state & RS_CLEARED)) boss_spawn(fl.boss, 160, 105);+ if (r->kind == ROOM_MINIBOSS && !(r->state & RS_CLEARED)) {+ static const uint8_t sins[] = {MON_SLOTH, MON_LUST, MON_WRATH, MON_GLUTTONY, MON_GREED, MON_ENVY, MON_PRIDE};+ if (!r->boss) r->boss = sins[rnd(sizeof sins)];+ boss_spawn(r->boss, 160, 110);+ }+ if (r->kind == ROOM_CHALLENGE && !(r->state & RS_CLEARED)) {+ /* two waves of the floor's monsters, for the chest */+ int cb = 1 << (chapter_of(fl.stage) - 1);+ int g = pick_group(r->tmpl, cb > 16 ? 16 : cb);+ for (int i = 0; i < 4; i++) {+ int m = groups[g][1 + i];+ if (m != MON_COUNT) mon_spawn(m, i < 2 ? 70 + i * 180 : 120 + (i - 2) * 80, i < 2 ? 80 : 160);+ }+ r->variant = 1;+ }+}++/* ---------------------------------------------------------------- leaving and coming back */+void room_save_things(void) {+ Room *r = room_cur();+ r->nsaved = 0;+ for (int i = 0; i < MAX_ENTS && r->nsaved < ROOM_SAVED; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_PICKUP && e->kind != EK_PEDESTAL && e->kind != EK_MACHINE && e->kind != EK_TRAPDOOR) continue;+ if (e->kind == EK_MACHINE && e->state == 255) continue; /* (blown up) */+ Saved *s = &r->saved[r->nsaved++];+ s->kind = e->kind, s->type = e->type, s->sub = e->sub, s->flags = 0;+ s->x = (int16_t)e->x, s->y = (int16_t)e->y;+ if (e->kind == EK_PICKUP) s->val = (uint16_t)e->price, s->flags = (e->flags & EF_SHOP) ? 1 : 0;+ if (e->kind == EK_PEDESTAL) s->val = (uint16_t)e->t3, s->sub = (uint8_t)e->price, s->flags = (e->flags & EF_DEVIL) ? 1 : 0;+ if (e->kind == EK_MACHINE) s->flags = e->state;+ }+}++static void room_restore_things(Room *r) {+ for (int i = 0; i < r->nsaved; i++) {+ Saved *s = &r->saved[i];+ if (s->kind == EK_PICKUP) {+ Ent *e = pickup_spawn(s->type, s->sub, s->x, s->y, false);+ if (e && (s->flags & 1)) e->price = (int16_t)s->val, e->flags |= EF_SHOP;+ } else if (s->kind == EK_PEDESTAL) {+ pedestal_spawn(s->val, s->x, s->y, s->sub, s->flags & 1);+ } else if (s->kind == EK_MACHINE) {+ Ent *e = machine_spawn(s->type, s->x, s->y);+ if (e) e->sub = s->sub, e->state = s->flags;+ } else if (s->kind == EK_TRAPDOOR) {+ Ent *e = ent_new(EK_TRAPDOOR, s->x, s->y);+ if (e) e->type = s->type, e->spr = SPR_TRAPDOOR;+ }+ }+}++void floor_enter_room(int index, int from_dir) {+ if (index < 0 || index >= fl.nrooms) return;+ Room *old = room_cur();+ if (g_mode == MODE_PLAY && index != fl.cur) room_save_things();+ (void)old;+ fl.cur = (int8_t)index;+ Room *r = room_cur();+ ents_clear();+ tears_clear();+ bool first = !(r->state & RS_VISITED);+ r->state |= RS_VISITED | RS_SEEN;+ /* the rooms next door show on the map */+ for (int d = 0; d < 4; d++) {+ int j = room_at(r->mx + DIR_X[d], r->my + DIR_Y[d]);+ if (j >= 0 && fl.rooms[j].kind != ROOM_SECRET && fl.rooms[j].kind != ROOM_SUPERSECRET) fl.rooms[j].state |= RS_SEEN;+ }+ if (first) {+ room_build_grid(r);+ room_spawn_first(r);+ } else+ room_restore_things(r);+ if (!(r->state & RS_CLEARED)) {+ room_spawn_monsters(r);+ room_spawn_specials(r);+ if (!ents_alive_monsters()) r->state |= RS_CLEARED;+ }+ g_room_was_cleared = r->state & RS_CLEARED;+ /* where he comes in */+ static const float ex[5] = {160, ROOM_X + 14, 160, ROOM_R - 14, 160}, ey[5] = {ROOM_B - 12, 120, ROOM_Y + 14, 120, 140};+ int d = from_dir < 0 || from_dir > 4 ? 4 : from_dir;+ pl.x = ex[d], pl.y = ey[d], pl.vx = pl.vy = 0;+ if (r->kind == ROOM_START && d == 4) pl.x = 160, pl.y = 120;+ familiars_spawn();+ g_room_frames = 0;+ /* per-room effects end */+ pl.belial = pl.bible = pl.shadows = pl.unicorn = pl.hourglass = pl.telepathy = pl.nail = pl.pony = 0;+ pl.room_damage = 0, pl.lust = 0;+ player_stats();+ /* the Curse of the Lost hides the map; Darkness, Unknown and Blind are drawn */+ if (fl.curse & CURSE_LOST)+ for (int i = 0; i < fl.nrooms; i++)+ if (i != fl.cur) fl.rooms[i].state &= (uint8_t)~RS_SEEN;+ /* a curse room hurts as he comes in (unless he flies) */+ if (r->kind == ROOM_CURSE && !pl.flight && d != 4) player_hurt(1, -3);+}++/* Teleport!, the Joker...: into another room, its middle */+void room_teleport(int index) {+ room_save_things();+ floor_enter_room(index, DIR_NONE);+}++void floor_open_devil(void) {+ if (fl.devil_room >= 0) return;+ int i = add_room(-1, -1, fl.devil_angel ? ROOM_ANGEL : ROOM_DEVIL);+ fl.devil_room = (int8_t)i;+}++void floor_reveal(int what) {+ for (int i = 0; i < fl.nrooms; i++) {+ Room *r = &fl.rooms[i];+ bool secret = r->kind == ROOM_SECRET || r->kind == ROOM_SUPERSECRET;+ if ((what & 1) && !secret) r->state |= RS_SEEN | RS_FOUND;+ if ((what & 2) && r->kind != ROOM_NORMAL && !secret) r->state |= RS_SEEN | RS_FOUND;+ if ((what & 4) && secret) r->state |= RS_SEEN | RS_FOUND;+ }+}++/* entering a door: through to the next room */+int floor_neighbour(int d) {+ Room *r = room_cur();+ if (fl.cur == fl.devil_room) return fl.boss_room;+ if (fl.cur == fl.boss_room && d == DIR_UP && fl.devil_open && room_at(r->mx, r->my - 1) < 0) {+ floor_open_devil();+ return fl.devil_room;+ }+ return room_at(r->mx + DIR_X[d], r->my + DIR_Y[d]);+}diff --git a/games/isaac/src/game.c b/games/isaac/src/game.cnew file mode 100644index 00000000..ecad88cb--- /dev/null+++ b/games/isaac/src/game.c@@ -0,0 +1,523 @@+/* The Binding of Isaac: the title, the menus, a run from the Basement down,+ * pausing, dying, the endings, and going from room to room. */+#include <math.h>+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++int g_mode;+bool g_running = true;+uint32_t g_frame, g_keys, g_pressed;+int g_room_frames, g_shake, g_killer, g_transition;+bool g_room_was_cleared;+uint32_t g_run_seed, g_rng = 0x2545F491u;+uint8_t g_wins, g_best_stage;+uint16_t g_deaths, g_runs;+static int menu_sel, pause_sel, confirm_sel, mode_t, ending;+static int trans_dir, trans_room;+static bool boss_hit; /* he was hurt in the boss's room (the Devil's deal) */+void pills_shuffle(void);+int floor_neighbour(int d);+bool door_try_unlock(int d);++/* ---------------------------------------------------------------- numbers */+uint32_t rng_next(uint32_t *s) {+ uint32_t x = *s;+ x ^= x << 13, x ^= x >> 17, x ^= x << 5;+ return *s = x ? x : 0x9E3779B9u;+}+int rnd(int n) { return n > 0 ? (int)(rng_next(&g_rng) % (uint32_t)n) : 0; }+float rndf(void) { return (rng_next(&g_rng) >> 8) * (1.0f / 16777216.0f); }+bool chance(float p) { return rndf() < p; }+float fsqrt(float v) { return v > 0 ? __builtin_sqrtf(v) : 0; }+float fatan2(float y, float x) { return atan2f(y, x); }+float fsin(float a) { return sinf(a); }+float fcos(float a) { return cosf(a); }+float dist2(float ax, float ay, float bx, float by) { return (ax - bx) * (ax - bx) + (ay - by) * (ay - by); }++/* ---------------------------------------------------------------- a run */+void game_new_run(void) {+ g_run_seed = g_rng ^ (plat_millis() * 2654435761u);+ if (!g_run_seed) g_run_seed = 1;+ g_rng = g_run_seed;+ g_frame = 0;+ player_reset();+ items_new_run();+ pills_shuffle();+ floor_new(1);+ floor_enter_room(fl.start, DIR_NONE);+ g_runs++;+ save_progress();+ g_mode = MODE_STAGE, mode_t = 0;+}++static uint8_t next_alt;+void game_next_floor(void) {+ int stage = fl.stage + 1;+ if (stage > 9) return;+ next_alt = fl.alt;+ if (stage >= 9) {+ floor_new(stage);+ fl.alt = next_alt;+ fl.boss = (uint8_t)(fl.alt ? MON_ISAAC : MON_SATAN);+ fl.rooms[fl.boss_room].boss = fl.boss;+ } else+ floor_new(stage);+ pl.damage_taken_floor = 0;+ boss_hit = false;+ if (pl.has[76]) floor_reveal(4);+ if (pl.has[54]) floor_reveal(1);+ if (pl.has[21]) floor_reveal(2);+ floor_enter_room(fl.start, DIR_NONE);+ if (stage > g_best_stage) g_best_stage = (uint8_t)stage;+ g_mode = MODE_STAGE, mode_t = 0;+ save_run();+ save_progress();+}++void game_won(int how) {+ ending = how;+ g_mode = MODE_WIN, mode_t = 0;+ if (how >= 2) {+ g_wins |= (uint8_t)(1 << how);+ save_remove_run();+ } else+ g_wins |= (uint8_t)(1 << how);+ save_progress();+}++void game_over(void) {+ g_deaths++;+ g_mode = MODE_DEAD, mode_t = 0;+ save_remove_run();+ save_progress();+}++void game_init(void) {+ save_restore_copy();+ load_progress();+ g_mode = MODE_TITLE;+ menu_sel = run_saved() ? 1 : 0;+}++/* ---------------------------------------------------------------- the boss's reward */+static void boss_reward(void) {+ Room *r = room_cur();+ if (r->kind != ROOM_BOSS) return;+ fl.boss_beaten = true;+ if (fl.stage == 9) { /* Satan or Isaac: the end */+ game_won(fl.alt ? 3 : 2);+ return;+ }+ if (fl.stage != 8 || true) pedestal_spawn(item_from_pool(POOL_BOSS), 160, 85, 0, false);+ pickup_spawn(rnd(3) ? PK_HEART : PK_SOUL, 0, 130, 140, true);+ if (fl.stage == 8) { /* Mom's Heart: the ending; then down to Sheol, or up to the Cathedral */+ Ent *t = ent_new(EK_TRAPDOOR, 120, 140);+ if (t) t->spr = SPR_TRAPDOOR, t->type = 0;+ t = ent_new(EK_TRAPDOOR, 200, 140);+ if (t) t->type = 1;+ game_won(1);+ } else {+ Ent *t = ent_new(EK_TRAPDOOR, 160, 130);+ if (t) t->spr = SPR_TRAPDOOR;+ if (fl.stage == 6) game_won(0);+ }+ /* the Devil's room, or an angel's, behind a door in the boss's room */+ if (fl.stage >= 2 && fl.stage <= 8) {+ float p = 0.15f + (boss_hit ? 0 : 0.35f) + (pl.damage_taken_floor ? 0 : 0.35f);+ if (pl.has[51]) p += 0.1f;+ if (pl.has[122]) p += 0.1f;+ if (chance(p)) {+ fl.devil_open = true;+ fl.devil_angel = !pl.has[79] && !pl.has[80] && rnd(2) == 0 && g_frame > 0 && !pl.has[118] && !pl.has[51];+ }+ }+}++/* ---------------------------------------------------------------- playing */+static void room_cleared(void) {+ Room *r = room_cur();+ r->state |= RS_CLEARED;+ if (r->kind == ROOM_CHALLENGE && r->variant == 1) { /* the second wave */+ r->state &= (uint8_t)~RS_CLEARED;+ r->variant = 2;+ int cb = 1 << (chapter_of(fl.stage) - 1);+ (void)cb;+ for (int i = 0; i < 3; i++) mon_spawn(fl.stage > 4 ? MON_KNIGHT : MON_GAPER, 80 + i * 80, 90);+ mon_spawn(fl.stage > 4 ? MON_BONY : MON_HOPPER, 160, 160);+ return;+ }+ if (r->kind == ROOM_CHALLENGE) pickup_spawn(PK_CHEST, 0, 160, 120, true), pedestal_spawn(item_from_pool(POOL_TREASURE), 160, 100, 0, false);+ if (r->kind == ROOM_MINIBOSS && r->boss == MON_GREED) for (int i = 0; i < 4; i++) pickup_spawn(PK_PENNY, 0, 160, 120, true);+ else if (r->kind == ROOM_MINIBOSS) pickup_drop_random(160, 120);+ room_clear_reward();+ item_room_cleared();+ boss_reward();+}++static void enter_door(int d) {+ int j = floor_neighbour(d);+ if (j < 0) return;+ trans_dir = d, trans_room = j;+ g_transition = 10;+}++static void play_update(void) {+ uint32_t k = g_keys;+ g_frame++;+ g_room_frames++;+ if (g_shake) g_shake--;+ if (banner_t > 0) banner_t--;+ if (g_transition > 0) { /* the dip to black between rooms */+ g_transition--;+ if (g_transition == 5) {+ static const int8_t opposite[4] = {DIR_UP, DIR_RIGHT, DIR_DOWN, DIR_LEFT};+ floor_enter_room(trans_room, opposite[trans_dir]);+ }+ return;+ }+ if (pl.dead) {+ player_update(0);+ ents_update();+ if (pl.hurt_anim <= 0) game_over();+ return;+ }+ /* the keys of a room */+ if (g_pressed & K_ITEM || (g_pressed & K_OK)) item_use_active();+ if ((g_pressed & K_BOMB) && pl.bombs > 0 && !pl.has[52]) {+ pl.bombs--;+ bomb_place(pl.x, pl.y + 1, 0);+ }+ if ((g_pressed & K_CARD) && pl.pocket_kind) {+ int kind = pl.pocket_kind, what = pl.pocket;+ pl.pocket_kind = 0, pl.pocket = 0;+ if (kind == 1) pill_use(what);+ else card_use(what);+ }+ int hp_before = pl.red + pl.soul;+ player_update(k);+ ents_update();+ tears_update();+ lasers_update();+ if (room_cur()->kind == ROOM_BOSS && pl.red + pl.soul < hp_before) boss_hit = true;+ if (pl.dead) return;+ /* the room is cleared when its last monster dies */+ if (!(room_cur()->state & RS_CLEARED) && !ents_alive_monsters() && g_room_frames > 3) room_cleared();+ /* the doors: walking into one (open) goes through; a locked one takes a key */+ static const uint32_t way[4] = {K_DOWN, K_LEFT, K_UP, K_RIGHT};+ for (int d = 0; d < 4; d++) {+ if (!(k & way[d]) || !room_door_exists(d, NULL)) continue;+ bool at = d == DIR_UP ? pl.y <= ROOM_Y + 6.5f && absf(pl.x - 160) < 9+ : d == DIR_DOWN ? pl.y >= ROOM_B - 6.5f && absf(pl.x - 160) < 9+ : d == DIR_LEFT ? pl.x <= ROOM_X + 6.5f && absf(pl.y - 120) < 9+ : pl.x >= ROOM_R - 6.5f && absf(pl.y - 120) < 9;+ if (!at) continue;+ if (!room_door_open(d) && !ents_alive_monsters()) door_try_unlock(d);+ if (room_door_open(d)) enter_door(d);+ }+ if (g_pressed & K_BACK) g_mode = MODE_PAUSE, pause_sel = 0;+}++/* ---------------------------------------------------------------- drawing */+static void draw_room(void) {+ room_draw_floor();+ doors_draw();+ ents_draw();+ tears_draw();+ lasers_draw();+ if (fl.curse & CURSE_DARKNESS) gfx_dark((int)pl.x, (int)pl.y - 8, 46, 26);+ hud_draw();+ if (g_keys & K_MAP) map_draw_full();+ if (g_transition > 0) {+ int a = g_transition > 5 ? (10 - g_transition) * 7 : g_transition * 7;+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, a > 32 ? 32 : a);+ }+}++static void paper(int x, int y, int w, int h) {+ gfx_rect(x + 3, y + 3, w, h, C_BLACK, 14);+ gfx_rect(x, y, w, h, rgb565(0xE6D6B4), 32);+ gfx_rect(x, y, w, 2, rgb565(0xC8B48C), 32);+ gfx_rect(x, y + h - 2, w, 2, rgb565(0xB49C74), 32);+}+#define INK rgb565(0x3A2A20)+#define INK2 rgb565(0x7A6048)++static void menu_list(const char *const *items, int n, int sel, int x, int y) {+ for (int i = 0; i < n; i++) {+ uint16_t c = i == sel ? INK : INK2;+ gfx_text_c(FONT_UPHEAVAL, x, y + i * 24, items[i], c, 2, false);+ if (i == sel) {+ int w = gfx_text_width(FONT_UPHEAVAL, items[i], 2);+ gfx_sprite_scaled(SPR_TEAR, 0, x - w / 2 - 10, y + i * 24 + 8, 150, 150, 0, 0, 0, 32);+ }+ }+}++static void draw_title(void) {+ gfx_backdrop(BD_ISAACS_ROOM);+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 10);+ gfx_text_c(FONT_UPHEAVAL, 162, 30, "THE BINDING OF", C_BLACK, 2, false);+ gfx_text_c(FONT_UPHEAVAL, 160, 28, "THE BINDING OF", rgb565(0xE8DCC8), 2, false);+ gfx_text_c(FONT_UPHEAVAL, 163, 55, "ISAAC", C_BLACK, 5, false);+ gfx_text_c(FONT_UPHEAVAL, 160, 52, "ISAAC", rgb565(0xB01010), 5, false);+ /* Isaac, crying on the floor of his room */+ int x = 160, y = 172;+ gfx_ellipse(x, y, 9, 3, C_BLACK, 12);+ gfx_sprite(SPR_ISAAC_BODY, 0, x, y, 0, 32);+ gfx_sprite(SPR_ISAAC_HEAD, (g_frame / 12) % 2, x, y - 7, 0, 32);+ if (g_frame % 50 < 34) gfx_text_c(FONT_UPHEAVAL, 160, 206, "PRESS OK", C_WHITE, 1, true);+}++static const char *const menu_new[] = {"NEW RUN", "HOW TO PLAY"};+static const char *const menu_cont[] = {"NEW RUN", "CONTINUE", "HOW TO PLAY"};++static void draw_menu(void) {+ gfx_backdrop(BD_ISAACS_ROOM);+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 12);+ paper(70, 40, 180, 150);+ bool cont = run_saved();+ menu_list(cont ? menu_cont : menu_new, cont ? 3 : 2, menu_sel, 160, 64);+ char s[48];+ int n = 0;+ const char *p = "RUNS ";+ while (*p) s[n++] = *p++;+ int v = g_runs;+ char digits[8];+ int nd = 0;+ do digits[nd++] = (char)('0' + v % 10), v /= 10; while (v && nd < 6);+ while (nd) s[n++] = digits[--nd];+ s[n] = 0;+ gfx_text_c(FONT_UPHEAVAL, 160, 162, s, INK2, 1, false);+ /* the bosses beaten: Mom, Mom's Heart, Satan, Isaac */+ static const int16_t marks[4] = {SPR_MOM_EYE, SPR_MOMS_HEART, SPR_SATAN, SPR_ISAAC_BOSS};+ for (int i = 0; i < 4; i++)+ if (g_wins & (1 << i)) gfx_sprite_scaled(marks[i], 0, 112 + i * 32, 186, 90, 90, 0, 0, 0, 32);+}++static void draw_help(void) {+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, rgb565(0x1A1210), 32);+ paper(14, 10, 292, 220);+ gfx_text_c(FONT_UPHEAVAL, 160, 18, "HOW TO PLAY", INK, 2, false);+ static const char *const rows[][2] = {+ {"ARROWS", "WALK"}, {"8 4 6 2 (5)", "CRY TEARS UP, LEFT, RIGHT, DOWN"}, {"9 OR SHIFT", "DROP A BOMB"},+ {"7 OR OK", "USE THE ACTIVE ITEM"}, {"1 OR 3", "TAKE THE PILL OR CARD"}, {"TOOLBOX", "HOLD FOR THE MAP"},+ {"BACK", "PAUSE"}, {"HOME", "KEEP THE RUN AND QUIT"},+ };+ for (int i = 0; i < 8; i++) {+ gfx_text(FONT_UPHEAVAL, 28, 48 + i * 17, rows[i][0], INK, 1, false);+ gfx_text(FONT_UPHEAVAL, 122, 48 + i * 17, rows[i][1], INK2, 1, false);+ }+ gfx_text_c(FONT_UPHEAVAL, 160, 190, "CLEAR EACH ROOM. FIND ITEMS. GO DOWN.", INK, 1, false);+ gfx_text_c(FONT_UPHEAVAL, 160, 204, "BEAT MOM, AND WHAT LIES BELOW HER.", INK, 1, false);+}++static void stat_line(int x, int y, const char *name, float v) {+ char s[24];+ int n = 0;+ while (*name) s[n++] = *name++;+ s[n++] = ' ';+ int whole = (int)v, frac = (int)((v - whole) * 100 + 0.5f);+ if (frac >= 100) whole++, frac -= 100;+ if (v < 0) whole = -whole, frac = 0, s[n++] = '-';+ char d[8];+ int nd = 0;+ do d[nd++] = (char)('0' + whole % 10), whole /= 10; while (whole && nd < 5);+ while (nd) s[n++] = d[--nd];+ s[n++] = '.', s[n++] = (char)('0' + frac / 10), s[n++] = (char)('0' + frac % 10), s[n] = 0;+ gfx_text(FONT_UPHEAVAL, x, y, s, INK, 1, false);+}++static void draw_pause(void) {+ draw_room();+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 16);+ paper(20, 20, 280, 200);+ gfx_text_c(FONT_UPHEAVAL, 160, 28, "PAUSED", INK, 2, false);+ static const char *const opts[] = {"RESUME", "QUIT GAME"};+ if (g_mode == MODE_CONFIRM) {+ gfx_text_c(FONT_UPHEAVAL, 160, 70, "QUIT? YOUR RUN IS KEPT", INK, 1, false);+ static const char *const yn[] = {"NO", "YES"};+ menu_list(yn, 2, confirm_sel, 160, 92);+ return;+ }+ menu_list(opts, 2, pause_sel, 100, 58);+ /* his stats, as the game lists them */+ stat_line(190, 54, "DMG", pl.damage);+ stat_line(190, 66, "TEARS", 30.0f / (pl.tear_delay + 1));+ stat_line(190, 78, "RANGE", pl.range / 3.65f);+ stat_line(190, 90, "SHOTSPD", pl.shot_speed);+ stat_line(190, 102, "SPEED", pl.speed);+ stat_line(190, 114, "LUCK", pl.luck);+ /* the items he found */+ for (int i = 0; i < pl.norder && i < 30; i++) gfx_sprite(item_sprite(pl.order[i]), 0, 40 + (i % 10) * 25, 158 + (i / 10) * 24, 0, 32);+ gfx_text_c(FONT_UPHEAVAL, 160, 128, stage_name(fl.stage, fl.alt), INK2, 1, false);+}++static void draw_stage(void) {+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 32);+ int k = mode_t;+ /* Isaac falls through the dark to the next floor */+ int y = k * 4 - 30;+ if (y > 150) y = 150;+ gfx_sprite(SPR_ISAAC_BODY, 0, 160, y + 7, 0, 32);+ gfx_sprite(SPR_ISAAC_HEAD, 1, 160, y, 0, 32);+ gfx_text_c(FONT_UPHEAVAL, 160, 90, stage_name(fl.stage, fl.alt), C_WHITE, 3, false);+ if (fl.curse) {+ static const char *const names[6] = {"CURSE OF DARKNESS", "CURSE OF THE LOST", "CURSE OF THE UNKNOWN", "CURSE OF THE MAZE",+ "CURSE OF THE BLIND", "CURSE OF THE LABYRINTH"};+ int i = 0;+ while (i < 5 && !(fl.curse & (1 << i))) i++;+ gfx_text_c(FONT_UPHEAVAL, 160, 124, names[i], rgb565(0xB040C0), 1, false);+ }+}++static void draw_dead(void) {+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, rgb565(0x140808), 32);+ paper(40, 16, 240, 208);+ gfx_text_c(FONT_UPHEAVAL, 160, 26, "YOU DIED", rgb565(0x901010), 3, false);+ gfx_sprite(SPR_ISAAC_BODY, 0, 90, 110, 0, 32);+ gfx_sprite(SPR_ISAAC_HEAD, 1, 90, 103, 0, 32);+ gfx_text(FONT_UPHEAVAL, 120, 66, "KILLED BY", INK2, 1, false);+ const char *who = mon_name(g_killer);+ if (g_killer >= MON_MONSTRO) {+ switch (g_killer) {+ case MON_MOM: case MON_MOM_HAND: case MON_MOM_FOOT: who = "MOM"; break;+ default: who = NULL; break;+ }+ }+ static const char *const causes[] = {"", "A BOMB", "SPIKES", "A CURSED DOOR", "FIRE", "GREED", "A SPIKED CHEST",+ "THE DEVIL", "HIS OWN BLOOD", "A BAD PILL", "A SHOT", "A LASER", "CREEP"};+ if (!who) who = g_killer < 0 && -g_killer < (int)(sizeof causes / sizeof causes[0]) ? causes[-g_killer] : "SOMETHING";+ gfx_text(FONT_UPHEAVAL, 120, 80, who, INK, 1, false);+ gfx_text(FONT_UPHEAVAL, 120, 100, "ON", INK2, 1, false);+ gfx_text(FONT_UPHEAVAL, 120, 114, stage_name(fl.stage, fl.alt), INK, 1, false);+ for (int i = 0; i < pl.norder && i < 16; i++) gfx_sprite(item_sprite(pl.order[i]), 0, 64 + (i % 8) * 25, 160 + (i / 8) * 24, 0, 32);+ if (mode_t > 30) gfx_text_c(FONT_UPHEAVAL, 160, 206, "OK: NEW RUN BACK: MENU", INK, 1, false);+}++static void draw_win(void) {+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 32);+ static const char *const text[4][4] = {+ {"MOM IS DEAD.", "BUT ISAAC'S STORY ISN'T OVER:", "THE WOMB IS BELOW HER ROOM.", ""},+ {"MOM'S HEART STOPS.", "ISAAC HIDES IN HIS CHEST", "AND WAITS... DOWN TO SHEOL,", "OR UP TO THE CATHEDRAL?"},+ {"SATAN FALLS.", "ISAAC HAS FACED HIS DEMONS", "AND THE DARK IS QUIET NOW.", "THE END."},+ {"ISAAC HAS FACED HIMSELF.", "IN THE LIGHT, NOTHING IS LEFT", "TO BE AFRAID OF.", "THE END."},+ };+ int e = ending & 3;+ gfx_sprite(SPR_ISAAC_BODY, 0, 160, 200, 0, 32);+ gfx_sprite(SPR_ISAAC_HEAD, e >= 2 ? 0 : 1, 160, 193, 0, 32);+ for (int i = 0; i < 4; i++)+ if (mode_t > i * 25) gfx_text_c(FONT_UPHEAVAL, 160, 50 + i * 26, text[e][i], C_WHITE, i == 0 || (e >= 2 && i == 3) ? 2 : 1, false);+ if (mode_t > 110) gfx_text_c(FONT_UPHEAVAL, 160, 222, e >= 2 ? "OK: TO THE MENU" : "OK: GO ON", rgb565(0xB0B0B0), 1, false);+}++void game_draw(void) {+ gfx_begin();+ switch (g_mode) {+ case MODE_TITLE: draw_title(); break;+ case MODE_MENU: draw_menu(); break;+ case MODE_HELP: draw_help(); break;+ case MODE_PLAY: case MODE_MAP: draw_room(); break;+ case MODE_PAUSE: case MODE_CONFIRM: draw_pause(); break;+ case MODE_STAGE: draw_stage(); break;+ case MODE_DEAD: draw_dead(); break;+ case MODE_WIN: draw_win(); break;+ default: break;+ }+ gfx_end();+}++/* ---------------------------------------------------------------- the keys */+static int up_down(int sel, int n) {+ if (g_pressed & (K_UP | K_SUP)) sel = (sel + n - 1) % n;+ if (g_pressed & (K_DOWN | K_SDOWN)) sel = (sel + 1) % n;+ return sel;+}++void game_frame(uint32_t keys) {+ g_pressed = keys & ~g_keys;+ g_keys = keys;+ if (keys & K_HOME) { /* Home and On/Off: the run kept, then out */+ save_run();+ save_progress();+ g_running = false;+ return;+ }+ bool ok = g_pressed & K_OK;+ mode_t++;+ switch (g_mode) {+ case MODE_TITLE:+ g_frame++;+ if (ok || (g_pressed & (K_SDOWN | K_ITEM))) g_mode = MODE_MENU, mode_t = 0, menu_sel = run_saved() ? 1 : 0;+ else if (g_pressed & K_BACK) g_running = false;+ break;+ case MODE_MENU: {+ g_frame++;+ bool cont = run_saved();+ int n = cont ? 3 : 2;+ menu_sel = up_down(menu_sel, n);+ if (g_pressed & K_BACK) g_mode = MODE_TITLE;+ if (ok) {+ int pick = cont ? menu_sel : (menu_sel == 0 ? 0 : 2);+ if (pick == 0) game_new_run();+ else if (pick == 1) {+ if (load_run()) {+ floor_enter_room(fl.cur, DIR_NONE);+ player_stats();+ g_mode = MODE_PLAY;+ } else+ game_new_run();+ } else+ g_mode = MODE_HELP;+ }+ break;+ }+ case MODE_HELP:+ if (ok || (g_pressed & K_BACK)) g_mode = MODE_MENU;+ break;+ case MODE_STAGE:+ g_frame++;+ if (mode_t > 75 || (ok && mode_t > 10)) {+ g_mode = MODE_PLAY;+ hud_banner(stage_name(fl.stage, fl.alt), "");+ banner_t = 40;+ }+ break;+ case MODE_PLAY:+ play_update();+ break;+ case MODE_PAUSE:+ pause_sel = up_down(pause_sel, 2);+ if (g_pressed & K_BACK) g_mode = MODE_PLAY;+ if (ok) {+ if (pause_sel == 0) g_mode = MODE_PLAY;+ else g_mode = MODE_CONFIRM, confirm_sel = 0;+ }+ break;+ case MODE_CONFIRM:+ confirm_sel = up_down(confirm_sel, 2);+ if (g_pressed & K_BACK) g_mode = MODE_PAUSE;+ if (ok) {+ if (confirm_sel == 1) {+ g_mode = MODE_PLAY;+ save_run();+ save_progress();+ g_mode = MODE_MENU, menu_sel = 1;+ } else+ g_mode = MODE_PAUSE;+ }+ break;+ case MODE_DEAD:+ if (mode_t > 30 && ok) game_new_run();+ else if (mode_t > 30 && (g_pressed & K_BACK)) g_mode = MODE_MENU, menu_sel = 0;+ break;+ case MODE_WIN:+ if (mode_t > 110 && ok) {+ if (ending >= 2) g_mode = MODE_MENU, menu_sel = 0;+ else g_mode = MODE_PLAY;+ }+ break;+ default: break;+ }+}diff --git a/games/isaac/src/game.h b/games/isaac/src/game.hnew file mode 100644index 00000000..dc42421f--- /dev/null+++ b/games/isaac/src/game.h@@ -0,0 +1,350 @@+/* The Binding of Isaac: what the whole game shares.+ *+ * The game runs as the original does, 30 times a second. A room is 13 x 7+ * cells of 20 pixels on the screen (the game's 26), its floor from (30, 50)+ * to (290, 190); positions are screen pixels. The game's own distances (40+ * units a cell) are half a pixel each here. */+#ifndef IS_GAME_H+#define IS_GAME_H+#include <stdbool.h>+#include <stdint.h>+#include "data.h"+#include "plat.h"++#define TILE 20+#define COLS 13+#define ROWS 7+#define ROOM_X 30 /* the floor's top-left on the screen */+#define ROOM_Y 50+#define ROOM_R (ROOM_X + COLS * TILE)+#define ROOM_B (ROOM_Y + ROWS * TILE)+#define FPS 30++/* ---------------------------------------------------------------- random numbers */+uint32_t rng_next(uint32_t *s);+int rnd(int n); /* 0 .. n-1, the game's own */+float rndf(void); /* 0 .. 1 */+bool chance(float p);+extern uint32_t g_rng;++/* ---------------------------------------------------------------- the player */+enum { DIR_DOWN, DIR_LEFT, DIR_UP, DIR_RIGHT, DIR_NONE };+extern const int8_t DIR_X[4], DIR_Y[4];++/* how tears behave (items give them) */+enum {+ TF_HOMING = 1 << 0, TF_PIERCE = 1 << 1, TF_SPECTRAL = 1 << 2, TF_POISON = 1 << 3, TF_SLOW = 1 << 4,+ TF_FREEZE = 1 << 5, TF_SPLIT = 1 << 6, TF_EXPLODE = 1 << 7, TF_BOOMERANG = 1 << 8, TF_CHARM = 1 << 9,+ TF_FEAR = 1 << 10, TF_CONFUSE = 1 << 11, TF_TOOTH = 1 << 12, TF_BLOOD = 1 << 13, TF_KNOCK = 1 << 14,+ TF_ENEMY = 1 << 15, TF_BOMB = 1 << 16, TF_ARC = 1 << 17, TF_COAL = 1 << 18, TF_FAMILIAR = 1 << 19,+ TF_BONE = 1 << 20, TF_BIG = 1 << 21, TF_PROPTOSIS = 1 << 22, TF_HOLY = 1 << 23, TF_FIRE = 1 << 24+};++#define ITEM_MAX 262+typedef struct {+ float x, y, vx, vy;+ int8_t face, shoot_dir; /* where he looks; the way he cries (DIR_NONE: not crying) */+ uint8_t walk; /* the walk's frame counter */+ /* stats, as the game shows them */+ float damage, tear_delay, range, shot_speed, speed, luck;+ float tears_ups, damage_ups; /* (what the items add, before the game's formulas) */+ uint32_t tear_flags;+ uint8_t shots; /* tears at once (The Inner Eye: 3, Mutant Spider: 4, 20/20: 2) */+ float fire_cd; /* frames until he can cry again */+ int16_t charge; /* Chocolate Milk, Brimstone, Mom's Knife: how long he has charged */+ uint8_t tear_eye; /* which eye cries next */+ /* health: red containers and red hearts, in halves; soul hearts in halves, black ones a bit each */+ uint8_t containers, red, soul, eternal;+ uint16_t black; /* bit i: soul heart i (whole hearts, from the left) is black */+ uint8_t coins, bombs, keys;+ int16_t active, active_charge; /* the active item (0: none) and its charge */+ uint8_t pocket_kind, pocket; /* a pill or a card */+ uint8_t has[ITEM_MAX]; /* the items he has, how many of each */+ uint8_t order[64], norder; /* the order he found them in */+ bool flight;+ int16_t invuln; /* frames he can't be hurt */+ int16_t hurt_anim, hold_anim; /* hurt flash; holding an item up */+ int16_t hold_item;+ int16_t belial, bible, shadows, unicorn, hourglass, telepathy, nail, pony, shoop, lemon; /* active items' room effects */+ float room_damage; /* damage only for this room (Book of Belial, Blood Rights) */+ int16_t lust; /* Bloody Lust's hits this room */+ uint8_t lives; /* 1up!, Dead Cat, Ankh, Lazarus... extra lives */+ bool dead;+ int16_t vampire; /* kills toward Charm of the Vampire's heal */+ uint8_t pill_known[16]; /* which pill colours he knows (their effect + 1) */+ uint8_t guppy, lotf; /* transformations: Guppy, Lord of the Flies (items counted) */+ uint8_t damage_taken_floor; /* red damage this floor (the Devil's room) */+ int16_t creep; /* frames of creep left under him (Lemon Mishap) */+ uint8_t pill_effect[14]; /* what each pill colour does this run */+ float pill_tears, pill_range, pill_speed, pill_luck; /* what pills did to his stats */+ uint8_t devil_deals; /* deals taken (no angels after one) */+} Player;+extern Player pl;++void player_reset(void);+void player_stats(void); /* recomputes stats from the items */+void player_update(uint32_t keys);+void player_draw_shadow(void);+void player_draw(void);+bool player_hurt(int halves, int source); /* true if it took */+void player_heal(int halves);+void player_add_soul(int halves, bool black);+void player_add_containers(int n);+int player_hearts_total(void); /* halves of every kind */+void player_kill(int source);++/* ---------------------------------------------------------------- tears, shots, lasers, bombs */+typedef struct {+ float x, y, z, vx, vy, vz; /* z: height over the floor (negative is up) */+ float dmg, size, dist, range;+ uint32_t flags;+ uint8_t active;+ int8_t lasthit; /* the last monster it went through (piercing) */+ int16_t life;+ float ang; /* homing, orbiting */+ uint16_t color;+} Tear;+#define MAX_TEARS 96+extern Tear tears[MAX_TEARS];+Tear *tear_fire(float x, float y, float vx, float vy, float dmg, uint32_t flags);+Tear *enemy_shot(float x, float y, float vx, float vy, int kind); /* kind: 0 blood, 1 bone, 2 fire */+void enemy_shots_ring(float x, float y, int n, float speed, float a0);+void enemy_shot_at(float x, float y, float speed);+void tears_update(void);+void tears_draw(void);+void tears_clear(void);++typedef struct {+ float x, y;+ int8_t dir; /* DIR_*, or -1: from (x, y) to (x2, y2) */+ float x2, y2;+ float width, dmg;+ int16_t life, max;+ uint8_t active, enemy, kind; /* kind: 0 blood (Brimstone), 1 tech, 2 holy, 3 Shoop */+} Laser;+#define MAX_LASERS 8+extern Laser lasers[MAX_LASERS];+Laser *laser_fire(float x, float y, int dir, float width, float dmg, int life, int kind, bool enemy);+void lasers_update(void);+void lasers_draw(void);++void bomb_place(float x, float y, int kind); /* kind: 0 Isaac's, 1 troll, 2 big (Mr. Boom), 3 Dr. Fetus */+void explode(float x, float y, float radius, float dmg, bool hurt_player, int kind);++/* ---------------------------------------------------------------- the room's things */+enum { EK_NONE, EK_MON, EK_PICKUP, EK_BOMB, EK_FX, EK_FAMILIAR, EK_PEDESTAL, EK_MACHINE, EK_TRAPDOOR, EK_CREEP,+ EK_FIRE, EK_DECAL };+/* pickups */+enum { PK_HEART, PK_HALF_HEART, PK_SOUL, PK_BLACK, PK_ETERNAL, PK_DOUBLE_HEART, PK_HALF_SOUL, PK_PENNY, PK_NICKEL,+ PK_DIME, PK_KEY, PK_GOLDEN_KEY, PK_KEY_RING, PK_BOMB, PK_DOUBLE_BOMB, PK_TROLL_BOMB, PK_CHEST, PK_LOCKED_CHEST,+ PK_RED_CHEST, PK_PILL, PK_CARD, PK_BATTERY, PK_GRAB_BAG, PK_SPIKED_CHEST, PK_GOLDEN_BOMB, PK_COUNT };+/* machines and beggars */+enum { MA_SLOT, MA_BLOOD, MA_FORTUNE, MA_BEGGAR, MA_DEVIL_BEGGAR, MA_KEY_MASTER, MA_BOMB_BUM, MA_SHOPKEEPER,+ MA_DEVIL_STATUE, MA_ANGEL_STATUE };+/* effects */+enum { FX_POOF, FX_BLOOD, FX_EXPLOSION, FX_SPLASH, FX_GIB, FX_TEXT, FX_SPARK, FX_FLAME, FX_LIGHT, FX_SHOCKWAVE,+ FX_STOMP };++enum {+ EF_FLYING = 1 << 0, EF_BOSS = 1 << 1, EF_INVULN = 1 << 2, EF_NOHIT = 1 << 3, EF_CHAMPION = 1 << 4,+ EF_SPAWNED = 1 << 5, EF_NOGRID = 1 << 6, EF_HIDDEN = 1 << 7, EF_SEGMENT = 1 << 8, EF_FRIEND = 1 << 9,+ EF_SHOP = 1 << 10, EF_DEVIL = 1 << 11, EF_NOCOUNT = 1 << 12, EF_ANGEL = 1 << 13, EF_NOKNOCK = 1 << 14,+ EF_FLIP = 1 << 15+};++typedef struct {+ uint8_t kind, type, sub, state;+ uint16_t flags;+ int16_t spr, frame;+ float x, y, z, vx, vy, vz;+ float r; /* radius */+ float hp, maxhp;+ int16_t t, t2, t3, price;+ float a, b, c;+ int8_t parent, child;+ uint8_t flash, poison, slow, freeze, charm, fear, confuse;+ float poison_dmg;+ uint8_t alpha; /* 32 opaque */+} Ent;+#define MAX_ENTS 112+extern Ent ents[MAX_ENTS];+Ent *ent_new(int kind, float x, float y);+void ents_clear(void);+int ents_alive_monsters(void); /* monsters that keep the doors shut */+Ent *ent_nearest_monster(float x, float y, float maxd);+void ents_update(void);+void ents_draw(void); /* everything, sorted by depth, the player among them */+void ent_damage(Ent *e, float dmg, uint32_t flags, float kx, float ky);+void ent_kill(Ent *e);++/* monsters (monsters.c), bosses (bosses.c) */+Ent *mon_spawn(int type, float x, float y);+void mon_update(Ent *e);+void mon_draw(Ent *e);+bool mon_on_death(Ent *e); /* false: it isn't dead after all (a Globin) */+const char *mon_name(int type);+bool boss_update(Ent *e); /* false if e isn't a boss's */+void boss_spawn(int type, float x, float y);+bool boss_on_death(Ent *e);+void boss_draw(Ent *e);+float bosses_hp(float *max); /* the boss bar */+void mon_set_sprite(Ent *e);++/* pickups (pickups.c) */+Ent *pickup_spawn(int type, int sub, float x, float y, bool pop);+void pickup_update(Ent *e);+int pickup_random_type(void);+void pickup_drop_random(float x, float y);+void room_clear_reward(void);+Ent *pedestal_spawn(int item, float x, float y, int price, bool devil);+Ent *machine_spawn(int type, float x, float y);+void machine_update(Ent *e);+void fx_spawn(int type, float x, float y, int life);+Ent *fx_text(float x, float y, const char *s);+void decal_blood(float x, float y, float size);+Ent *creep_spawn(float x, float y, float r, int life, bool enemy, int color);++/* ---------------------------------------------------------------- items (items.c) */+enum { POOL_TREASURE, POOL_SHOP, POOL_BOSS, POOL_DEVIL, POOL_ANGEL, POOL_SECRET, POOL_LIBRARY, POOL_CURSE,+ POOL_COUNT };+typedef struct {+ uint16_t id;+ const char *name, *text;+ uint8_t pools; /* POOL_* bits */+ uint8_t charge; /* an active item's charges (0: passive; 255: used once) */+ uint8_t quality;+} ItemInfo;+const ItemInfo *item_info(int id);+int item_sprite(int id);+int item_from_pool(int pool); /* takes it out of the pool */+void item_give(int id); /* picking it up: its effect, its text */+void item_use_active(void);+void item_room_cleared(void);+void items_new_run(void);+bool item_is_active(int id);+int item_count_in_run(void);+void pill_use(int color);+const char *pill_name(int color, bool known);+void card_use(int card);+const char *card_name(int card);+int familiars_count(void);+void familiars_spawn(void);+void familiar_update(Ent *e);+void familiar_draw(Ent *e);+void item_reroll_room(void); /* The D6 */++/* ---------------------------------------------------------------- the floor and its rooms (floor.c) */+#define MAP_W 13+#define MAP_H 9+#define MAX_ROOMS 28+#define ROOM_SAVED 12+enum { RS_VISITED = 1, RS_CLEARED = 2, RS_SEEN = 4, RS_FOUND = 8, RS_PAID = 16, RS_SPIKED = 32 };+typedef struct {+ uint8_t kind, type, sub, flags;+ uint16_t val; /* a pedestal's item, a pickup's price */+ int16_t x, y;+} Saved; /* a thing left lying in a room */+typedef struct {+ uint8_t kind, tmpl, group, state;+ uint8_t mx, my; /* its cell on the map */+ uint8_t grid[ROWS][COLS];+ uint8_t gridhp[ROWS][COLS]; /* poop's state, fire's */+ uint8_t nsaved, boss, variant;+ uint8_t doors_open; /* bit d: the door that way is open for good (keys, bombs) */+ Saved saved[ROOM_SAVED];+} Room;+typedef struct {+ uint8_t stage; /* 1: Basement I .. 8: Utero II, 9: Sheol, 10: Cathedral... */+ uint8_t alt; /* Cellar, Catacombs, Necropolis, Utero; Cathedral */+ uint8_t curse;+ uint8_t nrooms;+ int8_t map[MAP_H][MAP_W]; /* room index, or -1 */+ int8_t cur, start, boss_room, devil_room;+ uint8_t boss; /* the boss's MON_* */+ bool devil_open, devil_angel, boss_beaten, trapdoor;+ uint32_t seed;+ Room rooms[MAX_ROOMS];+} Floor;+extern Floor fl;+enum { CURSE_DARKNESS = 1, CURSE_LOST = 2, CURSE_UNKNOWN = 4, CURSE_MAZE = 8, CURSE_BLIND = 16, CURSE_LABYRINTH = 32 };++void floor_new(int stage);+void floor_enter_room(int index, int from_dir); /* from_dir: the door he came through, DIR_NONE: the middle */+Room *room_cur(void);+int room_backdrop(void);+const char *stage_name(int stage, bool alt);+int chapter_of(int stage); /* 1 Basement .. 4 Womb, 5 Sheol */+void room_save_things(void);+void floor_reveal(int what); /* 1: the map, 2: special rooms, 4: secret rooms */+bool room_door_exists(int d, int *kind); /* a door that way, and the room's kind behind it */+bool room_door_open(int d);+void room_open_doors(void);+void room_teleport(int index);+void floor_open_devil(void);++/* the grid (room.c) */+bool grid_solid(int c, int r, bool flying); /* blocks walking (or flying) */+bool grid_blocks_tears(int c, int r);+bool grid_pit(int c, int r);+bool solid_at(float x, float y, bool flying);+void move_ent(float *x, float *y, float *vx, float *vy, float r, bool flying, bool nogrid);+void grid_hit(int c, int r, float dmg); /* a tear hits a poop or a fire */+void grid_explode(float x, float y, float radius);+void grid_draw_row(int r);+bool door_try_unlock(int d);+void door_bombed(float x, float y);+int floor_neighbour(int d);+void doors_draw(void);+bool grid_line_clear(float x0, float y0, float x1, float y1);+int grid_spikes_at(float x, float y);+void room_draw_floor(void); /* the walls and floor, decals, creep */+int path_dir(float x, float y, float tx, float ty, bool flying, float *dx, float *dy); /* the way to walk */++/* ---------------------------------------------------------------- the HUD (hud.c) */+void hud_draw(void);+void hud_banner(const char *title, const char *sub); /* an item's name, a floor's */+void map_draw_full(void);+extern int16_t banner_t;++/* ---------------------------------------------------------------- the game */+enum { MODE_TITLE, MODE_MENU, MODE_PLAY, MODE_PAUSE, MODE_MAP, MODE_DEAD, MODE_WIN, MODE_STAGE, MODE_HELP,+ MODE_CONFIRM, MODE_STATS };+extern int g_mode;+extern bool g_running;+extern uint32_t g_frame; /* frames since the run began */+extern uint32_t g_keys, g_pressed;+extern int g_room_frames; /* frames since the room was entered */+extern int g_shake; /* screen shake left */+extern int g_killer; /* what killed him (MON_*, or -1..-n: other causes) */+extern bool g_room_was_cleared;+extern uint32_t g_run_seed;+extern int g_transition; /* frames of the dip to black between rooms */+extern uint8_t g_wins; /* runs won: Mom, Mom's Heart, Satan, Isaac (progress kept) */+extern uint16_t g_deaths, g_runs;+extern uint8_t g_best_stage;++void game_init(void);+void game_frame(uint32_t keys);+void game_draw(void);+void game_new_run(void);+void game_next_floor(void);+void game_won(int how);+void game_over(void);++/* saves (save.c) */+bool save_run(void);+bool load_run(void);+bool run_saved(void);+void save_remove_run(void);+void save_progress(void);+void load_progress(void);+void save_restore_copy(void);++static inline float clampf(float v, float a, float b) { return v < a ? a : v > b ? b : v; }+static inline float absf(float v) { return v < 0 ? -v : v; }+float fsqrt(float v);+float fatan2(float y, float x);+float fsin(float a);+float fcos(float a);+float dist2(float ax, float ay, float bx, float by);+#endifdiff --git a/games/isaac/src/gfx.c b/games/isaac/src/gfx.cnew file mode 100644index 00000000..08082a20--- /dev/null+++ b/games/isaac/src/gfx.c@@ -0,0 +1,334 @@+/* The renderer: a draw list rasterised in bands of GFX_BAND rows.+ *+ * Each command keeps its screen bounds (clipped); a band walks the commands+ * that cross it, in order. Sprites are runs of palette indices (res.h), so a+ * band only reads the rows it shows. Alpha has 33 steps (0..32), applied with+ * the usual RGB565 trick. */+#include <string.h>+#include "gfx.h"+#include "res.h"++enum { I_RECT, I_SPRITE, I_BACKDROP, I_TEXT, I_ELLIPSE, I_DARK };++typedef struct {+ uint8_t kind, flags, alpha, amount;+ int16_t x0, y0, x1, y1; /* screen bounds, clipped: [x0, x1) x [y0, y1) */+ int16_t x, y; /* sprite: its top-left (scaled); text: its top-left; ellipse: centre */+ int16_t w, h; /* sprite: its size on screen; ellipse: radii */+ int16_t ref, sub; /* sprite and frame / text: font and pool offset / backdrop */+ uint16_t color, scale; /* text: its scale; sprite: x scale */+ uint16_t scale_y;+} Item;++#ifdef HOST+static uint16_t host_band[SCREEN_W * GFX_BAND];+static uint16_t *band = host_band;+#else+static uint16_t *band;+#endif+void gfx_set_band_memory(uint16_t *px) {+#ifdef HOST+ band = px ? px : host_band;+#else+ band = px;+#endif+}++static Item items[GFX_MAX_ITEMS];+static int nitems;+static char pool[GFX_TEXT_POOL];+static int npool;+static int clip_x0, clip_y0, clip_x1 = SCREEN_W, clip_y1 = SCREEN_H;+int gfx_dropped;+static int dropped_now;++uint16_t gfx_mix(uint16_t f, uint16_t b, int a) {+ uint32_t x = (f | (uint32_t)f << 16) & 0x07E0F81F, y = (b | (uint32_t)b << 16) & 0x07E0F81F;+ y = (y + ((x - y) * (uint32_t)a >> 5)) & 0x07E0F81F;+ return (uint16_t)(y | y >> 16);+}++void gfx_begin(void) {+ nitems = 0, npool = 0, dropped_now = 0;+ gfx_clip_all();+}++void gfx_clip(int x, int y, int w, int h) {+ clip_x0 = x < 0 ? 0 : x, clip_y0 = y < 0 ? 0 : y;+ clip_x1 = x + w > SCREEN_W ? SCREEN_W : x + w, clip_y1 = y + h > SCREEN_H ? SCREEN_H : y + h;+}+void gfx_clip_all(void) { clip_x0 = 0, clip_y0 = 0, clip_x1 = SCREEN_W, clip_y1 = SCREEN_H; }++/* a new command over [x0, x1) x [y0, y1), clipped; NULL if it shows nothing */+static Item *add(int kind, int x0, int y0, int x1, int y1) {+ if (x0 < clip_x0) x0 = clip_x0;+ if (y0 < clip_y0) y0 = clip_y0;+ if (x1 > clip_x1) x1 = clip_x1;+ if (y1 > clip_y1) y1 = clip_y1;+ if (x0 >= x1 || y0 >= y1) return NULL;+ if (nitems >= GFX_MAX_ITEMS) {+ dropped_now++;+ return NULL;+ }+ Item *it = &items[nitems++];+ memset(it, 0, sizeof *it);+ it->kind = (uint8_t)kind;+ it->x0 = (int16_t)x0, it->y0 = (int16_t)y0, it->x1 = (int16_t)x1, it->y1 = (int16_t)y1;+ it->alpha = 32;+ return it;+}++static int clamp_alpha(int a) { return a < 0 ? 0 : a > 32 ? 32 : a; }++void gfx_backdrop(int b) {+ Item *it = add(I_BACKDROP, 0, 0, SCREEN_W, SCREEN_H);+ if (it) it->ref = (int16_t)b;+}++void gfx_rect(int x, int y, int w, int h, uint16_t color, int alpha) {+ alpha = clamp_alpha(alpha);+ if (!alpha) return;+ Item *it = add(I_RECT, x, y, x + w, y + h);+ if (it) it->color = color, it->alpha = (uint8_t)alpha;+}++void gfx_ellipse(int cx, int cy, int rx, int ry, uint16_t color, int alpha) {+ alpha = clamp_alpha(alpha);+ if (!alpha || rx <= 0 || ry <= 0) return;+ Item *it = add(I_ELLIPSE, cx - rx, cy - ry, cx + rx + 1, cy + ry + 1);+ if (it) it->x = (int16_t)cx, it->y = (int16_t)cy, it->w = (int16_t)rx, it->h = (int16_t)ry, it->color = color, it->alpha = (uint8_t)alpha;+}++void gfx_dark(int x, int y, int r, int alpha) {+ Item *it = add(I_DARK, 0, 0, SCREEN_W, SCREEN_H);+ if (it) it->x = (int16_t)x, it->y = (int16_t)y, it->w = (int16_t)r, it->alpha = (uint8_t)clamp_alpha(alpha);+}++void gfx_sprite_scaled(int s, int i, int x, int y, int sx, int sy, int flags, uint16_t color, int amount, int alpha) {+ Frame f;+ alpha = clamp_alpha(alpha);+ if (!alpha || sx <= 0 || sy <= 0 || !frame_get(s, i, &f)) return;+ int w = (f.w * sx + 128) >> 8, h = (f.h * sy + 128) >> 8;+ if (w <= 0 || h <= 0) return;+ int ox = (f.ox * sx) >> 8, oy = (f.oy * sy) >> 8;+ int tx = x - ((flags & GFX_FLIPX) ? w - 1 - ox : ox), ty = y - ((flags & GFX_FLIPY) ? h - 1 - oy : oy);+ Item *it = add(I_SPRITE, tx, ty, tx + w, ty + h);+ if (!it) return;+ it->x = (int16_t)tx, it->y = (int16_t)ty, it->w = (int16_t)w, it->h = (int16_t)h;+ it->ref = (int16_t)s, it->sub = (int16_t)i, it->flags = (uint8_t)flags, it->alpha = (uint8_t)alpha;+ it->color = color, it->amount = (uint8_t)clamp_alpha(amount);+ it->scale = (uint16_t)sx, it->scale_y = (uint16_t)sy;+}+void gfx_sprite(int s, int i, int x, int y, int flags, int alpha) { gfx_sprite_scaled(s, i, x, y, 256, 256, flags, 0, 0, alpha); }+void gfx_sprite_tint(int s, int i, int x, int y, int flags, uint16_t color, int amount, int alpha) {+ gfx_sprite_scaled(s, i, x, y, 256, 256, flags, color, amount, alpha);+}++int gfx_text_width(int font, const char *s, int scale) {+ int w = 0, best = 0;+ Glyph g;+ for (; *s; s++) {+ if (*s == '\n') {+ if (w > best) best = w;+ w = 0;+ continue;+ }+ if (glyph_get(font, (uint8_t)*s, &g)) w += g.adv * scale;+ }+ return w > best ? w : best;+}++int gfx_text(int font, int x, int y, const char *s, uint16_t color, int scale, bool outline) {+ int n = (int)strlen(s), w = gfx_text_width(font, s, scale), lines = 1;+ for (const char *p = s; *p; p++) lines += *p == '\n';+ if (npool + n + 1 > GFX_TEXT_POOL) return w;+ int pad = outline ? 1 : 0;+ Item *it = add(I_TEXT, x - pad, y - pad - 4 * scale, x + w + pad, y + lines * font_height(font) * scale + pad + 2 * scale);+ if (!it) return w;+ memcpy(pool + npool, s, (size_t)n + 1);+ it->x = (int16_t)x, it->y = (int16_t)y, it->ref = (int16_t)font, it->sub = (int16_t)npool;+ it->color = color, it->scale = (uint16_t)scale, it->flags = outline;+ npool += n + 1;+ return w;+}+void gfx_text_c(int font, int x, int y, const char *s, uint16_t color, int scale, bool outline) {+ gfx_text(font, x - gfx_text_width(font, s, scale) / 2, y, s, color, scale, outline);+}++/* ---------------------------------------------------------------- rasterising */+static int by0, by1; /* the band: rows [by0, by1) */+static inline uint16_t *px_at(int x, int y) { return band + (y - by0) * SCREEN_W + x; }++static void r_rect(const Item *it, int y0, int y1) {+ for (int y = y0; y < y1; y++) {+ uint16_t *p = px_at(it->x0, y);+ int n = it->x1 - it->x0;+ if (it->alpha >= 32)+ for (int i = 0; i < n; i++) p[i] = it->color;+ else+ for (int i = 0; i < n; i++) p[i] = gfx_mix(it->color, p[i], it->alpha);+ }+}++static void r_ellipse(const Item *it, int y0, int y1) {+ int rx = it->w, ry = it->h;+ for (int y = y0; y < y1; y++) {+ int dy = y - it->y;+ /* the half width at this row: rx * sqrt(1 - (dy / ry)^2) */+ int32_t t = (int32_t)ry * ry - dy * dy;+ if (t < 0) continue;+ int hw = 0;+ while ((hw + 1) * (hw + 1) * ry * ry <= t * rx * rx) hw++;+ int a = it->x - hw, b = it->x + hw + 1;+ if (a < it->x0) a = it->x0;+ if (b > it->x1) b = it->x1;+ uint16_t *p = px_at(0, y);+ for (int x = a; x < b; x++) p[x] = gfx_mix(it->color, p[x], it->alpha);+ }+}++static void r_dark(const Item *it, int y0, int y1) {+ int r2 = it->w * it->w, soft = (it->w + 24) * (it->w + 24);+ for (int y = y0; y < y1; y++) {+ uint16_t *p = px_at(0, y);+ int dy = y - it->y;+ for (int x = 0; x < SCREEN_W; x++) {+ int dx = x - it->x, d = dx * dx + dy * dy;+ if (d <= r2) continue;+ int a = d >= soft ? it->alpha : it->alpha * (d - r2) / (soft - r2);+ p[x] = gfx_mix(0, p[x], a);+ }+ }+}++static void r_backdrop(const Item *it, int y0, int y1) {+ Backdrop bd;+ if (!backdrop_get(it->ref, &bd)) return;+ /* the room 2 w x 2 h, its quarter mirrored; the screen shows it from x = w - 160 */+ int off = bd.w * 2 - SCREEN_W; /* (40: 20 each side) */+ for (int y = y0; y < y1; y++) {+ int qy = y < bd.h ? y : 2 * bd.h - 1 - y;+ if (qy < 0) qy = 0;+ const uint8_t *row = bd.px + qy * bd.w;+ uint16_t *p = px_at(0, y);+ int x = 0, half = bd.w - off / 2; /* screen columns of the left half */+ for (; x < half; x++) p[x] = bd.pal[row[x + off / 2]];+ for (; x < SCREEN_W; x++) p[x] = bd.pal[row[2 * bd.w - 1 - (x + off / 2)]];+ }+}++/* a sprite's row decoded into indices (255: nothing) */+static uint8_t line[512];+static void decode_row(const Frame *f, int r, int w) {+ memset(line, 255, (size_t)w);+ const uint8_t *p = f->rows + f->rowoff[r];+ int n = *p++, x = 0;+ while (n--) {+ x += *p++;+ int len = *p++;+ for (int i = 0; i < len && x < w; i++) line[x++] = *p++;+ if (x >= w) break;+ }+}++static void r_sprite(const Item *it, int y0, int y1) {+ Frame f;+ if (!frame_get(it->ref, it->sub, &f)) return;+ bool scaled = it->scale != 256 || it->scale_y != 256;+ bool flipx = it->flags & GFX_FLIPX, flipy = it->flags & GFX_FLIPY;+ int alpha = it->alpha, amount = it->amount;+ uint16_t tint = it->color;+ for (int y = y0; y < y1; y++) {+ int sy = y - it->y;+ if (scaled) sy = sy * 256 / it->scale_y;+ if (flipy) sy = f.h - 1 - sy;+ if (sy < 0 || sy >= f.h) continue;+ uint16_t *p = px_at(0, y);+ if (!scaled) {+ const uint8_t *q = f.rows + f.rowoff[sy];+ int n = *q++, x = 0;+ while (n--) {+ x += *q++;+ int len = *q++;+ for (int i = 0; i < len; i++, x++) {+ uint8_t c = *q++;+ int dx = flipx ? it->x + f.w - 1 - x : it->x + x;+ if (dx < it->x0 || dx >= it->x1) continue;+ int a = f.alpha[c] * alpha >> 5;+ uint16_t col = f.pal[c];+ if (amount) col = gfx_mix(tint, col, amount);+ p[dx] = a >= 32 ? col : gfx_mix(col, p[dx], a);+ }+ }+ } else {+ decode_row(&f, sy, f.w);+ for (int dx = it->x0; dx < it->x1; dx++) {+ int sx = (dx - it->x) * 256 / it->scale;+ if (flipx) sx = f.w - 1 - sx;+ if (sx < 0 || sx >= f.w) continue;+ uint8_t c = line[sx];+ if (c == 255) continue;+ int a = f.alpha[c] * alpha >> 5;+ uint16_t col = f.pal[c];+ if (amount) col = gfx_mix(tint, col, amount);+ p[dx] = a >= 32 ? col : gfx_mix(col, p[dx], a);+ }+ }+ }+}++static void r_glyphs(const Item *it, int y0, int y1, int ox, int oy, uint16_t color) {+ const char *s = pool + it->sub;+ int sc = it->scale, x = it->x + ox, y = it->y + oy, fh = font_height(it->ref);+ Glyph g;+ for (; *s; s++) {+ if (*s == '\n') {+ x = it->x + ox, y += fh * sc;+ continue;+ }+ if (!glyph_get(it->ref, (uint8_t)*s, &g)) continue;+ int gx = x + g.ox * sc, gy = y + g.oy * sc, stride = (g.w + 7) >> 3;+ for (int r = 0; r < g.h; r++)+ for (int k = 0; k < sc; k++) {+ int yy = gy + r * sc + k;+ if (yy < y0 || yy >= y1) continue;+ uint16_t *p = px_at(0, yy);+ const uint8_t *bits = g.bits + r * stride;+ for (int c = 0; c < g.w; c++)+ if (bits[c >> 3] & (0x80 >> (c & 7)))+ for (int m = 0; m < sc; m++) {+ int xx = gx + c * sc + m;+ if (xx >= it->x0 && xx < it->x1) p[xx] = color;+ }+ }+ x += g.adv * sc;+ }+}+static void r_text(const Item *it, int y0, int y1) {+ if (it->flags) {+ static const int8_t d[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};+ for (int k = 0; k < 8; k++) r_glyphs(it, y0, y1, d[k][0], d[k][1], C_BLACK);+ }+ r_glyphs(it, y0, y1, 0, 0, it->color);+}++void gfx_end(void) {+ gfx_dropped = dropped_now;+ for (by0 = 0; by0 < SCREEN_H; by0 += GFX_BAND) {+ by1 = by0 + GFX_BAND;+ for (int i = 0; i < nitems; i++) {+ const Item *it = &items[i];+ if (it->y1 <= by0 || it->y0 >= by1) continue;+ int y0 = it->y0 > by0 ? it->y0 : by0, y1 = it->y1 < by1 ? it->y1 : by1;+ switch (it->kind) {+ case I_RECT: r_rect(it, y0, y1); break;+ case I_SPRITE: r_sprite(it, y0, y1); break;+ case I_BACKDROP: r_backdrop(it, y0, y1); break;+ case I_TEXT: r_text(it, y0, y1); break;+ case I_ELLIPSE: r_ellipse(it, y0, y1); break;+ case I_DARK: r_dark(it, y0, y1); break;+ }+ }+ plat_push(0, by0, SCREEN_W, GFX_BAND, band);+ }+}diff --git a/games/isaac/src/gfx.h b/games/isaac/src/gfx.hnew file mode 100644index 00000000..32a138b6--- /dev/null+++ b/games/isaac/src/gfx.h@@ -0,0 +1,53 @@+/* The Binding of Isaac: drawing.+ *+ * A frame is a draw list, rasterised band by band (the calculator has no+ * frame buffer) and pushed to the screen, painted in the order given. The+ * room shows whole, as in the game: its walls and floor 360 x 240 pixels+ * (the game's own, 20 for its 26), 20 of them cut off on each side. */+#ifndef IS_GFX_H+#define IS_GFX_H+#include <stdbool.h>+#include <stdint.h>+#include "plat.h"++static inline uint16_t rgb565(uint32_t rgb) { /* 0xRRGGBB */+ return (uint16_t)(((rgb >> 8) & 0xF800) | ((rgb >> 5) & 0x07E0) | ((rgb >> 3) & 0x001F));+}+#define C_BLACK 0x0000+#define C_WHITE 0xFFFF+#define C_RED 0xF800++enum { GFX_FLIPX = 1, GFX_FLIPY = 2 };++#define GFX_BAND 16 /* screen rows rasterised at a time */+#define GFX_MAX_ITEMS 560 /* commands in a frame (more are dropped) */+#define GFX_TEXT_POOL 2048 /* bytes of text in a frame */++void gfx_set_band_memory(uint16_t *px); /* SCREEN_W * GFX_BAND pixels (main's stack on the calculator) */+void gfx_begin(void);+void gfx_end(void);+/* later commands draw only inside this rectangle (gfx_clip_all(): all of the screen) */+void gfx_clip(int x, int y, int w, int h);+void gfx_clip_all(void);++/* alpha goes from 0 (nothing) to 32 (opaque) */+void gfx_backdrop(int b); /* a room's walls and floor, all of the screen */+void gfx_rect(int x, int y, int w, int h, uint16_t color, int alpha);+void gfx_ellipse(int cx, int cy, int rx, int ry, uint16_t color, int alpha);+/* frame i of sprite s, drawn with its origin at (x, y) */+void gfx_sprite(int s, int i, int x, int y, int flags, int alpha);+/* the same, mixed towards a colour by amount (0 to 32: a flash, a silhouette) */+void gfx_sprite_tint(int s, int i, int x, int y, int flags, uint16_t color, int amount, int alpha);+/* the same, scaled (8.8 fixed point, 256 = as it is) around its origin */+void gfx_sprite_scaled(int s, int i, int x, int y, int sx, int sy, int flags, uint16_t color, int amount, int alpha);+/* text, its top-left at (x, y), each pixel `scale` pixels; an outline of black around it; returns its width */+int gfx_text(int font, int x, int y, const char *s, uint16_t color, int scale, bool outline);+int gfx_text_width(int font, const char *s, int scale);+/* text centred on x */+void gfx_text_c(int font, int x, int y, const char *s, uint16_t color, int scale, bool outline);+/* the darkness of the Curse of the Darkness: all but a circle around (x, y) dimmed */+void gfx_dark(int x, int y, int r, int alpha);++uint16_t gfx_mix(uint16_t f, uint16_t b, int a); /* f over b, a from 0 (b) to 32 (f) */+extern int gfx_dropped;+#endifdiff --git a/games/isaac/src/hud.c b/games/isaac/src/hud.cnew file mode 100644index 00000000..b333e88f--- /dev/null+++ b/games/isaac/src/hud.c@@ -0,0 +1,174 @@+/* The HUD, where Rebirth has it: the active item and its charge top left,+ * the hearts beside it, coins, bombs and keys down the left, the map top+ * right, the pill or card bottom right, the boss's health along the bottom,+ * and the banners for floors and items. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++int16_t banner_t;+static char banner_title[40], banner_sub[48];++void hud_banner(const char *title, const char *sub) {+ strncpy(banner_title, title, sizeof banner_title - 1);+ banner_title[sizeof banner_title - 1] = 0;+ strncpy(banner_sub, sub, sizeof banner_sub - 1);+ banner_sub[sizeof banner_sub - 1] = 0;+ banner_t = 75;+}++static void number(int x, int y, int n) {+ char s[4];+ s[0] = (char)('0' + n / 10 % 10), s[1] = (char)('0' + n % 10), s[2] = 0;+ gfx_text(FONT_UPHEAVAL, x, y, s, C_WHITE, 1, true);+}++static void hearts(int x0, int y0) {+ if (fl.curse & CURSE_UNKNOWN) {+ gfx_text(FONT_UPHEAVAL, x0 + 2, y0 + 1, "?", C_WHITE, 2, true);+ return;+ }+ int i = 0;+ for (int c = 0; c < pl.containers; c++, i++) {+ int h = pl.red - c * 2;+ int f = h >= 2 ? 0 : h == 1 ? 1 : 2;+ gfx_sprite(SPR_HUD_HEART, f, x0 + (i % 6) * 11, y0 + (i / 6) * 11, 0, 32);+ }+ for (int s = 0; s < (pl.soul + 1) / 2; s++, i++) {+ bool black = pl.black & (1 << s), half = s * 2 + 1 == pl.soul;+ int f = black ? (half ? 6 : 5) : (half ? 4 : 3);+ gfx_sprite(SPR_HUD_HEART, f, x0 + (i % 6) * 11, y0 + (i / 6) * 11, 0, 32);+ }+ if (pl.eternal) { /* the eternal heart: a white one over the last red */+ int k = pl.containers ? pl.containers - 1 : 0;+ gfx_sprite_scaled(SPR_HUD_HEART, 7, x0 + (k % 6) * 11 + 2, y0 + (k / 6) * 11 + 2, 160, 160, 0, 0, 0, 32);+ }+}++/* ---------------------------------------------------------------- the map */+static void room_cell(int x, int y, int w, int h, Room *r, int i, bool big) {+ uint16_t c;+ if (i == fl.cur) c = C_WHITE;+ else if (r->state & RS_VISITED) c = rgb565(0x9A9A9A);+ else c = rgb565(0x4A4A4A);+ gfx_rect(x, y, w, h, C_BLACK, 32);+ gfx_rect(x + 1, y + 1, w - 2, h - 2, c, 32);+ int icon = -1;+ switch (r->kind) {+ case ROOM_BOSS: icon = 0; break;+ case ROOM_TREASURE: icon = 1; break;+ case ROOM_SHOP: icon = 2; break;+ case ROOM_SECRET: case ROOM_SUPERSECRET: icon = 3; break;+ case ROOM_CURSE: icon = 4; break;+ case ROOM_ARCADE: icon = 5; break;+ case ROOM_SACRIFICE: icon = 6; break;+ case ROOM_LIBRARY: icon = 7; break;+ case ROOM_MINIBOSS: icon = 8; break;+ case ROOM_CHALLENGE: icon = 9; break;+ default: break;+ }+ if (icon >= 0 && ((r->state & (RS_VISITED | RS_FOUND)) || pl.has[21] || (r->state & RS_SEEN))) {+ static const uint32_t col[10] = {0xC02020, 0xE0C030, 0x40A040, 0x606060, 0x902020, 0xD050A0, 0xA03030, 0x806040, 0xC06020, 0xA02020};+ gfx_sprite_tint(SPR_MAP_ICON, icon, x + w / 2, y + h / 2, 0, rgb565(col[icon]), 32, 32);+ (void)big;+ }+}++static bool shown(Room *r) {+ return r->state & (RS_SEEN | RS_VISITED | RS_FOUND);+}++static void minimap(void) {+ if (fl.curse & CURSE_LOST) return;+ int cw = 9, ch = 7, vx = 250, vy = 3, vw = 66, vh = 43;+ gfx_rect(vx - 1, vy - 1, vw + 2, vh + 2, C_BLACK, 14);+ Room *cr = room_cur();+ int cmx = fl.cur == fl.devil_room ? fl.rooms[fl.boss_room].mx : cr->mx, cmy = fl.cur == fl.devil_room ? fl.rooms[fl.boss_room].my : cr->my;+ gfx_clip(vx, vy, vw, vh);+ for (int i = 0; i < fl.nrooms; i++) {+ Room *r = &fl.rooms[i];+ if (i == fl.devil_room || !shown(r)) continue;+ int x = vx + vw / 2 - cw / 2 + (r->mx - cmx) * cw, y = vy + vh / 2 - ch / 2 + (r->my - cmy) * ch;+ room_cell(x, y, cw, ch, r, i, false);+ }+ gfx_clip_all();+}++void map_draw_full(void) {+ gfx_rect(0, 0, SCREEN_W, SCREEN_H, C_BLACK, 20);+ gfx_text_c(FONT_UPHEAVAL, 160, 8, stage_name(fl.stage, fl.alt), C_WHITE, 2, true);+ int cw = 22, ch = 17;+ int minx = 99, maxx = -1, miny = 99, maxy = -1;+ for (int i = 0; i < fl.nrooms; i++) {+ Room *r = &fl.rooms[i];+ if (i == fl.devil_room || !shown(r)) continue;+ if (r->mx < minx) minx = r->mx;+ if (r->mx > maxx) maxx = r->mx;+ if (r->my < miny) miny = r->my;+ if (r->my > maxy) maxy = r->my;+ }+ if (maxx < 0) return;+ int ox = 160 - (minx + maxx + 1) * cw / 2, oy = 128 - (miny + maxy + 1) * ch / 2;+ for (int i = 0; i < fl.nrooms; i++) {+ Room *r = &fl.rooms[i];+ if (i == fl.devil_room || !shown(r)) continue;+ room_cell(ox + r->mx * cw, oy + r->my * ch, cw - 2, ch - 2, r, i, true);+ }+}++/* ---------------------------------------------------------------- all of it */+void hud_draw(void) {+ int x = 4;+ /* the active item and its charge */+ if (pl.active) {+ const ItemInfo *ii = item_info(pl.active);+ gfx_sprite(item_sprite(pl.active), 0, 16, 26, 0, 32);+ if (ii && ii->charge && ii->charge != 255) {+ int h = 22, n = ii->charge;+ gfx_rect(29, 4, 6, h + 2, C_BLACK, 32);+ int full = pl.active_charge > n ? n : pl.active_charge;+ gfx_rect(30, 5 + h - h * full / n, 4, h * full / n, pl.active_charge >= n ? rgb565(0xF0E040) : rgb565(0x60C040), 32);+ if (pl.active_charge > n) gfx_rect(30, 5 + h - h * (pl.active_charge - n) / n, 4, h * (pl.active_charge - n) / n, rgb565(0x40A0F0), 32);+ for (int k = 1; k < n && n <= 6; k++) gfx_rect(30, 5 + h * k / n, 4, 1, C_BLACK, 32);+ }+ x = 40;+ }+ hearts(x, 4);+ /* coins, bombs, keys */+ int cy = 30;+ gfx_sprite(SPR_HUD_ICON, 0, 3, cy, 0, 32), number(14, cy, pl.coins);+ gfx_sprite(SPR_HUD_ICON, 1, 3, cy + 11, 0, 32), number(14, cy + 11, pl.bombs);+ gfx_sprite(SPR_HUD_ICON, 2, 3, cy + 22, 0, 32), number(14, cy + 22, pl.keys);+ if (pl.has[175] || pl.keys >= 99) {}+ minimap();+ /* the pill or card */+ if (pl.pocket_kind) {+ int spr = pl.pocket_kind == 1 ? SPR_PILL : SPR_CARD;+ gfx_sprite(spr, pl.pocket - 1, 306, 234, 0, 32);+ }+ /* the boss's health */+ float max, hp = bosses_hp(&max);+ if (max > 0 && hp > 0) {+ int w = 150, f = (int)(w * hp / max);+ gfx_rect(85, 226, w + 4, 9, C_BLACK, 26);+ gfx_rect(87, 228, f, 5, rgb565(0xC01818), 32);+ gfx_rect(87, 228, f, 1, rgb565(0xF06060), 32);+ }+ /* banners: an item's name and what it does, or the floor's */+ if (banner_t > 0) {+ int a = banner_t > 8 ? 32 : banner_t * 4;+ int y = 62;+ int w = gfx_text_width(FONT_UPHEAVAL, banner_title, 2);+ int ws = gfx_text_width(FONT_UPHEAVAL, banner_sub, 1);+ if (ws > w) w = ws;+ w += 24;+ gfx_rect(160 - w / 2, y - 4, w, banner_sub[0] ? 38 : 26, rgb565(0xE8D8B8), a * 7 / 8);+ gfx_rect(160 - w / 2, y - 4, w, 2, rgb565(0x6A4A30), a);+ gfx_rect(160 - w / 2, y + (banner_sub[0] ? 32 : 20), w, 2, rgb565(0x6A4A30), a);+ if (a > 16) {+ gfx_text_c(FONT_UPHEAVAL, 160, y, banner_title, rgb565(0x302018), 2, false);+ if (banner_sub[0]) gfx_text_c(FONT_UPHEAVAL, 160, y + 22, banner_sub, rgb565(0x5A4030), 1, false);+ }+ }+}diff --git a/games/isaac/src/icon.png b/games/isaac/src/icon.pngnew file mode 100644index 00000000..946b22a8Binary files /dev/null and b/games/isaac/src/icon.png differdiff --git a/games/isaac/src/items.c b/games/isaac/src/items.cnew file mode 100644index 00000000..0dd12835--- /dev/null+++ b/games/isaac/src/items.c@@ -0,0 +1,760 @@+/* The items, by Rebirth's numbers and names, the text the game shows when+ * one is picked up, the rooms each comes from (its pools) and what it does;+ * the active items' charges; pills, cards and familiars. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++enum { T = 1 << POOL_TREASURE, S = 1 << POOL_SHOP, BO = 1 << POOL_BOSS, D = 1 << POOL_DEVIL, A = 1 << POOL_ANGEL,+ X = 1 << POOL_SECRET, L = 1 << POOL_LIBRARY, C = 1 << POOL_CURSE };++/* id, name, pickup text, pools, charge (0: passive, 255: once), quality (0 to 4) */+static const ItemInfo items[] = {+ {1, "THE SAD ONION", "TEARS UP", T, 0, 3},+ {2, "THE INNER EYE", "TRIPLE SHOT", T, 0, 2},+ {3, "SPOON BENDER", "PSYCHIC SHOT", T | X, 0, 3},+ {4, "CRICKET'S HEAD", "DMG UP", T | X, 0, 4},+ {5, "MY REFLECTION", "BOOMERANG TEARS", T, 0, 1},+ {6, "NUMBER ONE", "TEARS UP", T, 0, 1},+ {7, "BLOOD OF THE MARTYR", "DMG UP", T, 0, 3},+ {8, "BROTHER BOBBY", "FRIENDS 'TILL THE END", T | A, 0, 1},+ {10, "HALO OF FLIES", "PROJECTILE PROTECTION", T, 0, 2},+ {11, "1UP!", "EXTRA LIFE", T | X, 0, 2},+ {12, "MAGIC MUSHROOM", "ALL STATS UP!", T, 0, 4},+ {13, "THE VIRUS", "POISON TOUCH", T, 0, 1},+ {14, "ROID RAGE", "SPEED AND RANGE UP", T, 0, 1},+ {15, "<3", "HP UP", T | BO, 0, 1},+ {16, "RAW LIVER", "HP UP", BO, 0, 1},+ {17, "SKELETON KEY", "99 KEYS", T | X, 0, 2},+ {18, "A DOLLAR", "$$$", T | X, 0, 2},+ {19, "BOOM!", "10 BOMBS", T, 0, 1},+ {20, "TRANSCENDENCE", "WE ALL FLOAT DOWN HERE...", T, 0, 2},+ {21, "THE COMPASS", "REVEALS ICONS", T | S, 0, 1},+ {22, "LUNCH", "HP UP", BO, 0, 0},+ {23, "DINNER", "HP UP", BO, 0, 0},+ {24, "DESSERT", "HP UP", BO, 0, 0},+ {25, "BREAKFAST", "HP UP", BO, 0, 0},+ {26, "ROTTEN MEAT", "HP UP", BO, 0, 0},+ {27, "WOODEN SPOON", "SPEED UP", BO, 0, 1},+ {28, "THE BELT", "SPEED UP", BO, 0, 1},+ {29, "MOM'S UNDERWEAR", "RANGE UP", BO, 0, 1},+ {30, "MOM'S HEELS", "RANGE UP", BO, 0, 1},+ {31, "MOM'S LIPSTICK", "RANGE UP", BO, 0, 1},+ {32, "WIRE COAT HANGER", "TEARS UP", T, 0, 2},+ {33, "THE BIBLE", "TEMPORARY FLIGHT", L | A | S, 4, 2},+ {34, "THE BOOK OF BELIAL", "TEMPORARY DMG UP", L | D, 3, 2},+ {35, "THE NECRONOMICON", "MEGA BLAST", L | D, 3, 2},+ {36, "THE POOP", "PLOP!", T, 1, 0},+ {37, "MR. BOOM", "REUSABLE BOMBS", T, 2, 1},+ {38, "TAMMY'S HEAD", "REUSABLE TEAR BURST", T, 1, 2},+ {39, "MOM'S BRA", "MASS PETRIFY", T, 3, 1},+ {41, "MOM'S PAD", "MASS FEAR", T, 3, 0},+ {42, "BOB'S ROTTEN HEAD", "REUSABLE RANGED BOMB", T, 3, 1},+ {44, "TELEPORT!", "TELEPORT", T | S, 2, 1},+ {45, "YUM HEART", "REUSABLE REGENERATION", T | S, 4, 2},+ {46, "LUCKY FOOT", "LUCK UP", T | S, 0, 2},+ {47, "DOCTOR'S REMOTE", "AIR STRIKE", T, 2, 1},+ {48, "CUPID'S ARROW", "PIERCING SHOTS", T, 0, 2},+ {49, "SHOOP DA WHOOP!", "BLLLARRRRGGG!", T, 2, 2},+ {50, "STEVEN", "DMG UP", BO, 0, 3},+ {51, "PENTAGRAM", "DMG UP", T | D, 0, 3},+ {52, "DR. FETUS", "WHAT DOES IT MEAN?", T, 0, 3},+ {53, "MAGNETO", "ITEM SNATCHER", T | S, 0, 1},+ {54, "TREASURE MAP", "FULL MAP", T | S, 0, 1},+ {55, "MOM'S EYE", "EYE IN THE BACK OF YOUR HEAD", T, 0, 1},+ {56, "LEMON MISHAP", "OOPS...", T, 2, 0},+ {57, "DISTANT ADMIRATION", "ATTACK FLY", T, 0, 1},+ {58, "BOOK OF SHADOWS", "TEMPORARY INVINCIBILITY", L | A | T, 3, 2},+ {60, "THE LADDER", "BUILDING BRIDGES", T | S, 0, 1},+ {62, "CHARM OF THE VAMPIRE", "KILLS HEAL", T, 0, 2},+ {63, "THE BATTERY", "STORES ENERGY", S, 0, 2},+ {64, "STEAM SALE", "50% OFF", S, 0, 2},+ {65, "ANARCHIST COOKBOOK", "SUMMON BOMBS", T, 3, 1},+ {66, "THE HOURGLASS", "TEMPORARY ENEMY SLOWDOWN", T, 2, 1},+ {67, "SISTER MAGGY", "FRIENDS 'TILL THE END", T, 0, 1},+ {68, "TECHNOLOGY", "LASER TEARS", T, 0, 3},+ {69, "CHOCOLATE MILK", "CHARGED TEARS", T, 0, 3},+ {70, "GROWTH HORMONES", "SPEED + DMG UP", BO, 0, 2},+ {71, "MINI MUSH", "SPEED + RANGE UP", T, 0, 1},+ {72, "ROSARY", "FAITH UP", A | T, 0, 2},+ {73, "CUBE OF MEAT", "GOTTA MEAT 'EM ALL", T, 0, 2},+ {74, "A QUARTER", "+25 COINS", T | X, 0, 1},+ {75, "PHD", "BETTER PILLS", T | S, 0, 2},+ {76, "X-RAY VISION", "I CAN SEE FOREVER!", T | S, 0, 1},+ {77, "MY LITTLE UNICORN", "TEMPORARY BADASS", T, 6, 2},+ {78, "BOOK OF REVELATIONS", "REUSABLE SOUL PROTECTION", L | A, 6, 2},+ {79, "THE MARK", "DMG UP + SPEED UP", D, 0, 3},+ {80, "THE PACT", "DMG + TEARS UP", D, 0, 3},+ {81, "DEAD CAT", "9 LIVES", D | T, 0, 2},+ {82, "LORD OF THE PIT", "DEMON WINGS", D, 0, 3},+ {83, "THE NAIL", "TEMPORARY DEMON FORM", D, 6, 1},+ {84, "WE NEED TO GO DEEPER!", "REUSABLE TRAPDOOR", T | S, 6, 2},+ {85, "DECK OF CARDS", "REUSABLE CARD GENERATOR", T | S, 6, 2},+ {86, "MONSTRO'S TOOTH", "SUMMON MONSTRO", T, 3, 1},+ {87, "LOKI'S HORNS", "CROSS TEARS", T, 0, 1},+ {88, "LITTLE CHUBBY", "ATTACK BUDDY", T, 0, 2},+ {89, "SPIDER BITE", "SLOW EFFECT", T, 0, 1},+ {90, "THE SMALL ROCK", "DMG UP", X, 0, 2},+ {91, "SPELUNKER HAT", "SEE THROUGH WALLS", T | S, 0, 1},+ {92, "SUPER BANDAGE", "+2 SOUL HEARTS", BO, 0, 2},+ {94, "SACK OF PENNIES", "GIVES MONEY", T, 0, 1},+ {95, "ROBO-BABY", "FRIENDS 'TILL THE END", T, 0, 1},+ {96, "LITTLE C.H.A.D.", "GIVES KISSES", T, 0, 1},+ {97, "THE BOOK OF SIN", "REUSABLE ITEM GENERATOR", L | D, 4, 2},+ {98, "THE RELIC", "SOUL GENERATOR", A, 0, 2},+ {99, "LITTLE GISH", "STICKY FRIEND", T, 0, 1},+ {100, "LITTLE STEVEN", "PSYCHIC FRIEND", T, 0, 1},+ {101, "THE HALO", "ALL STATS UP", A, 0, 3},+ {102, "MOM'S BOTTLE OF PILLS", "REUSABLE PILL GENERATOR", T | S, 6, 1},+ {103, "THE COMMON COLD", "POISON DAMAGE", T, 0, 1},+ {104, "THE PARASITE", "SPLIT SHOT", T, 0, 2},+ {105, "THE D6", "REROLL YOUR DESTINY", T | X, 6, 4},+ {106, "MR. MEGA", "BLAST DAMAGE", T, 0, 2},+ {108, "THE WAFER", "DAMAGE RESISTANCE", T | A, 0, 3},+ {109, "MONEY = POWER", "$ = DMG", T | S, 0, 1},+ {110, "MOM'S CONTACTS", "FREEZE EFFECT", T, 0, 1},+ {111, "THE BEAN", "TOOT ON COMMAND", T, 1, 0},+ {112, "GUARDIAN ANGEL", "ANGEL SHIELD", A, 0, 2},+ {113, "DEMON BABY", "AUTO TURRET", D | T, 0, 2},+ {114, "MOM'S KNIFE", "STAB STAB STAB", D | T, 0, 4},+ {115, "OUIJA BOARD", "SPECTRAL TEARS", T, 0, 1},+ {117, "DEAD BIRD", "PROTECTIVE FRIEND", T, 0, 1},+ {118, "BRIMSTONE", "BLOOD LASER BARRAGE", D, 0, 4},+ {119, "BLOOD BAG", "HP UP", T, 0, 1},+ {120, "ODD MUSHROOM", "SPEED + TEARS UP", T, 0, 1},+ {121, "ODD MUSHROOM", "DMG + RANGE + HP UP", T, 0, 2},+ {122, "WHORE OF BABYLON", "CURSE UP", D, 0, 2},+ {123, "MONSTER MANUAL", "TEMPORARY BUDDY", T | L, 3, 1},+ {124, "DEAD SEA SCROLLS", "IT'S A MYSTERY", T | L, 2, 1},+ {126, "RAZOR BLADE", "FEEL MY PAIN", D, 1, 1},+ {127, "FORGET ME NOW", "REROLL YOUR FLOOR", X | D, 255, 1},+ {128, "FOREVER ALONE", "ATTACK FLY", T, 0, 1},+ {129, "BUCKET OF LARD", "HP UP", BO, 0, 1},+ {130, "A PONY", "FLIGHT + DASH ATTACK", T, 2, 2},+ {131, "BOMB BAG", "GIVES BOMBS", T, 0, 1},+ {132, "A LUMP OF COAL", "MY XMAS PRESENT", T, 0, 2},+ {133, "GUPPY'S PAW", "SOUL CONVERTER", D, 1, 2},+ {134, "GUPPY'S TAIL", "CURSED?", D, 0, 1},+ {135, "IV BAG", "PORTABLE BLOOD BANK", S, 0, 1},+ {136, "BEST FRIEND", "REUSABLE DECOY", T | S, 3, 0},+ {137, "REMOTE DETONATOR", "REMOTE BOMB DETONATION", T | S, 1, 1},+ {138, "STIGMATA", "DMG + HP UP", T, 0, 2},+ {140, "BOB'S CURSE", "POISON BOMBS", T, 0, 1},+ {142, "SCAPULAR", "PROTECTION", A, 0, 2},+ {143, "SPEED BALL", "SPEED + SHOT SPEED UP", T, 0, 1},+ {144, "BUM FRIEND", "HE'S GREEDY", T | S, 0, 1},+ {145, "GUPPY'S HEAD", "REUSABLE FLY HIVE", D, 1, 2},+ {146, "PRAYER CARD", "REUSABLE ETERNITY", A, 6, 2},+ {147, "NOTCHED AXE", "ROCKS DON'T STAND A CHANCE", S, 0, 1},+ {149, "IPECAC", "EXPLOSIVE SHOTS", T, 0, 3},+ {150, "TOUGH LOVE", "TOOTH SHOT", T, 0, 2},+ {152, "TECHNOLOGY 2", "LASER", T, 0, 2},+ {153, "MUTANT SPIDER", "QUAD SHOT", T, 0, 3},+ {154, "CHEMICAL PEEL", "DMG UP", T, 0, 1},+ {155, "THE PEEPER", "PLOP!", T, 0, 1},+ {157, "BLOODY LUST", "RAGE", T, 0, 2},+ {158, "CRYSTAL BALL", "FORTUNE TELLER", S, 6, 1},+ {159, "SPIRIT OF THE NIGHT", "SCARY", D, 0, 2},+ {160, "CRACK THE SKY", "HOLY WHITE DEATH", A, 4, 2},+ {161, "ANKH", "ETERNAL LIFE?", T | X, 0, 2},+ {162, "CELTIC CROSS", "YOU FEEL BLESSED", T, 0, 1},+ {163, "GHOST BABY", "SPECTRAL BUDDY", T, 0, 1},+ {164, "THE CANDLE", "BURN", S, 2, 1},+ {165, "CAT-O-NINE-TAILS", "SHOT SPEED + DMG UP", T, 0, 1},+ {166, "D20", "REROLL THE BASICS", S, 6, 2},+ {167, "HARLEQUIN BABY", "DOUBLE SPREAD BUDDY", T, 0, 2},+ {169, "POLYPHEMUS", "MEGA TEARS", T, 0, 3},+ {170, "DADDY LONGLEGS", "DADDY'S LOVE", T, 3, 1},+ {171, "SPIDER BUTT", "BUG ZAPPER", T, 2, 1},+ {173, "MITRE", "BLESSING UP", A, 0, 1},+ {174, "RAINBOW BABY", "RANDOM BUDDY", T, 0, 1},+ {175, "DAD'S KEY", "OPEN SESAME", S, 2, 1},+ {176, "STEM CELLS", "HP UP + SHOT SPEED UP", BO, 0, 1},+ {177, "PORTABLE SLOT", "GAMBLE 24/7", S, 0, 1},+ {178, "HOLY WATER", "SPLASH!", A, 0, 1},+ {179, "FATE", "FLIGHT ETERNAL", A, 0, 3},+ {182, "SACRED HEART", "HOMING SHOTS + DMG UP", A, 0, 4},+ {183, "TOOTH PICKS", "TEARS + SHOT SPEED UP", T, 0, 1},+ {184, "HOLY GRAIL", "FLIGHT + HP UP", A, 0, 3},+ {185, "DEAD DOVE", "FLIGHT + SPECTRAL TEARS", A, 0, 2},+ {186, "BLOOD RIGHTS", "BLOODY RAGE", D, 3, 1},+ {188, "ABEL", "MIRRORED BUDDY", T, 0, 1},+ {189, "SMB SUPER FAN", "ALL STATS UP", T, 0, 3},+ {190, "PYRO", "99 BOMBS", T | X, 0, 2},+ {192, "TELEPATHY FOR DUMMIES", "TEMPORARY PSYCHIC", T | L, 2, 1},+ {193, "MEAT!", "DMG + HP UP", BO, 0, 2},+ {194, "MAGIC 8 BALL", "SHOT SPEED UP", T, 0, 1},+ {196, "SQUEEZY", "SOUL HEARTS", T, 0, 1},+ {197, "JESUS JUICE", "DMG + RANGE UP", T, 0, 2},+ {199, "MOM'S KEY", "ITEM DROP UP + 2 KEYS", T, 0, 1},+ {201, "IRON BAR", "DMG UP + CONCUSSIVE TEARS", T, 0, 2},+ {245, "20/20", "DOUBLE SHOT", T, 0, 3},+ {261, "PROPTOSIS", "SHORT RANGE MEGA TEARS", T, 0, 3},+};+#define NITEMS (int)(sizeof items / sizeof items[0])++static const uint16_t sprite_ids[ITEM_SPRITE_COUNT] = ITEM_ID_TABLE;++const ItemInfo *item_info(int id) {+ for (int i = 0; i < NITEMS; i++)+ if (items[i].id == id) return &items[i];+ return NULL;+}+int item_sprite(int id) {+ for (int i = 0; i < ITEM_SPRITE_COUNT; i++)+ if (sprite_ids[i] == id) return SPR_ITEM_1 + i;+ return SPR_ITEM_1;+}+bool item_is_active(int id) {+ const ItemInfo *ii = item_info(id);+ return ii && ii->charge;+}++/* ---------------------------------------------------------------- pools: each item once a run */+uint8_t item_taken[ITEM_MAX];+#define taken item_taken+void items_new_run(void) { memset(taken, 0, sizeof taken); }++int item_from_pool(int pool) {+ int cand[NITEMS], n = 0;+ for (int i = 0; i < NITEMS; i++)+ if ((items[i].pools & (1 << pool)) && !taken[items[i].id] && !pl.has[items[i].id]) cand[n++] = i;+ if (!n) /* the pool emptied: the treasure room's then, or Breakfast, as the game does */+ for (int i = 0; i < NITEMS; i++)+ if ((items[i].pools & T) && !taken[items[i].id] && !pl.has[items[i].id]) cand[n++] = i;+ if (!n) return 25;+ int id = items[cand[rnd(n)]].id;+ taken[id] = 1;+ return id;+}++int item_count_in_run(void) { return pl.norder; }++/* ---------------------------------------------------------------- picking one up */+static bool is_familiar(int id) {+ switch (id) {+ case 8: case 10: case 57: case 67: case 73: case 88: case 94: case 95: case 96: case 98: case 99: case 100: case 112:+ case 113: case 128: case 131: case 144: case 155: case 163: case 167: case 174: case 188:+ return true;+ default: return false;+ }+}++void item_give(int id) {+ const ItemInfo *ii = item_info(id);+ if (!ii) return;+ if (pl.has[id] < 255) pl.has[id]++;+ if (pl.norder < 64) pl.order[pl.norder++] = (uint8_t)(id < 256 ? id : 0);+ taken[id] = 1;+ hud_banner(ii->name, ii->text);+ if (ii->charge) {+ pl.active = (int16_t)id;+ pl.active_charge = ii->charge == 255 ? 1 : ii->charge;+ }+ switch (id) {+ case 11: pl.lives++; break;+ case 81: pl.lives += 9; pl.containers = 1; pl.red = 2; if (pl.soul > 2) pl.soul = 2; break;+ case 161: pl.lives++; break;+ case 12: case 15: case 22: case 23: case 24: case 25: case 26: case 121: case 138: case 176: case 184: case 193:+ player_add_containers(1), player_heal(id == 15 || id == 12 ? 24 : 2);+ break;+ case 101: player_add_containers(1), player_heal(2); break;+ case 182: player_add_containers(1), player_heal(24); break;+ case 16: player_add_containers(2), player_heal(4); break;+ case 92: player_add_containers(1), player_heal(2), player_add_soul(4, false); break;+ case 119: player_add_containers(1), player_heal(10); break;+ case 129: player_add_containers(2), player_heal(1); break;+ case 72: player_add_soul(6, false); break;+ case 80: player_add_soul(4, false); break;+ case 79: player_add_soul(2, true); break;+ case 196: player_add_soul(4, false); break;+ case 173: player_add_soul(2, false); break;+ case 179: pl.eternal = 1; break;+ case 17: pl.keys = 99; break;+ case 18: pl.coins = 99; break;+ case 74: pl.coins = (uint8_t)(pl.coins + 25 > 99 ? 99 : pl.coins + 25); break;+ case 19: pl.bombs = (uint8_t)(pl.bombs + 10 > 99 ? 99 : pl.bombs + 10); break;+ case 190: pl.bombs = 99; break;+ case 106: case 140: pl.bombs = (uint8_t)(pl.bombs + 5 > 99 ? 99 : pl.bombs + 5); break;+ case 199: pl.keys = (uint8_t)(pl.keys + 2 > 99 ? 99 : pl.keys + 2); break;+ case 75: player_heal(4); break;+ case 21: floor_reveal(2); break;+ case 54: floor_reveal(1); break;+ case 76: floor_reveal(4); break;+ case 194: pickup_spawn(PK_CARD, 0, pl.x, pl.y + 14, true); break;+ case 116: break;+ default: break;+ }+ if (is_familiar(id)) familiars_spawn();+ player_stats();+}++/* ---------------------------------------------------------------- active items */+static int last_dir(void) { return pl.shoot_dir != DIR_NONE ? pl.shoot_dir : pl.face; }++static void hurt_all(float dmg) {+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && !(ents[i].flags & EF_FRIEND)) ent_damage(&ents[i], dmg, 0, 0, 0);+}++static void blue_flies(int n) {+ for (int i = 0; i < n; i++) mon_spawn(MON_BLUE_FLY, pl.x + rnd(10) - 5, pl.y - 6);+}++static void random_room_teleport(int kind) {+ int cand[MAX_ROOMS], n = 0;+ for (int i = 0; i < fl.nrooms; i++)+ if (i != fl.cur && (kind < 0 ? fl.rooms[i].kind != ROOM_SUPERSECRET && fl.rooms[i].kind != ROOM_SECRET && i != fl.devil_room : fl.rooms[i].kind == kind)) cand[n++] = i;+ if (n) room_teleport(cand[rnd(n)]);+}++static bool use_effect(int id) {+ switch (id) {+ case 33: /* The Bible: flight; and Mom or her heart dies of it */+ pl.bible = 1;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && (ents[i].type == MON_MOM || ents[i].type == MON_MOMS_HEART) && !ents[i].sub)+ ent_damage(&ents[i], 99999, 0, 0, 0);+ break;+ case 34: pl.belial = 1; break;+ case 35: hurt_all(40); g_shake = 10; break;+ case 36: {+ int c = (int)((pl.x - ROOM_X) / TILE), r = (int)((pl.y - ROOM_Y) / TILE);+ Room *rm = room_cur();+ if (c >= 0 && r >= 0 && c < COLS && r < ROWS && rm->grid[r][c] == CELL_EMPTY) rm->grid[r][c] = CELL_POOP, rm->gridhp[r][c] = 4;+ else return false;+ break;+ }+ case 37: bomb_place(pl.x, pl.y, 2); break;+ case 38: {+ float dmg = pl.damage + 25;+ for (int i = 0; i < 10; i++) {+ float a = i * 0.6283f;+ tear_fire(pl.x, pl.y, fcos(a) * 5, fsin(a) * 5, dmg, pl.tear_flags);+ }+ break;+ }+ case 39: for (int i = 0; i < MAX_ENTS; i++) if (ents[i].kind == EK_MON) ents[i].freeze = 120; break;+ case 41: for (int i = 0; i < MAX_ENTS; i++) if (ents[i].kind == EK_MON) ents[i].fear = 150; break;+ case 42: {+ int d = last_dir();+ explode(pl.x + DIR_X[d] * 60, pl.y + DIR_Y[d] * 50, 36, 70, false, 2);+ break;+ }+ case 44: random_room_teleport(-1); break;+ case 45: player_heal(2); break;+ case 47: {+ Ent *e = ent_nearest_monster(pl.x, pl.y, 999);+ if (e) explode(e->x, e->y, 40, 50, false, 0);+ else return false;+ break;+ }+ case 49: laser_fire(pl.x, pl.y - 10, last_dir(), 16, pl.damage * 0.9f, 20, 3, false); break;+ case 56: creep_spawn(pl.x, pl.y, 26, 300, false, 2); break;+ case 58: pl.shadows = 300; break;+ case 65:+ for (int i = 0; i < 6; i++) {+ float a = i * 1.047f;+ bomb_place(clampf(pl.x + fcos(a) * 45, ROOM_X + 8, ROOM_R - 8), clampf(pl.y + fsin(a) * 35, ROOM_Y + 8, ROOM_B - 8), 1);+ }+ break;+ case 66: pl.hourglass = 240; break;+ case 77: pl.unicorn = 180; player_stats(); break;+ case 78: player_add_soul(2, false); break;+ case 83: pl.nail = 1, player_add_soul(2, true), pl.room_damage += 0.7f; player_stats(); break;+ case 84: {+ Ent *t = ent_new(EK_TRAPDOOR, pl.x, pl.y + 2);+ if (t) t->spr = SPR_TRAPDOOR, t->t = 0;+ break;+ }+ case 85: pickup_spawn(PK_CARD, 0, pl.x, pl.y + 12, true); break;+ case 86: {+ Ent *e = ent_nearest_monster(pl.x, pl.y, 999);+ if (!e) return false;+ fx_spawn(FX_SHOCKWAVE, e->x, e->y, 14);+ g_shake = 10;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && dist2(ents[i].x, ents[i].y, e->x, e->y) < 40 * 40) ent_damage(&ents[i], 120, 0, 0, 0);+ break;+ }+ case 97: {+ static const uint8_t k[] = {PK_HEART, PK_PENNY, PK_BOMB, PK_KEY, PK_PILL, PK_CARD, PK_SOUL, PK_CHEST};+ pickup_spawn(k[rnd(sizeof k)], 0, pl.x, pl.y + 12, true);+ break;+ }+ case 102: pickup_spawn(PK_PILL, 0, pl.x, pl.y + 12, true); break;+ case 105: item_reroll_room(); break;+ case 111:+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || dist2(e->x, e->y, pl.x, pl.y) > 50 * 50) continue;+ e->poison = 60, e->poison_dmg = 3;+ float dx = e->x - pl.x, dy = e->y - pl.y, d = fsqrt(dx * dx + dy * dy) + 0.1f;+ e->vx += dx / d * 4, e->vy += dy / d * 4;+ }+ fx_spawn(FX_POOF, pl.x, pl.y, 12);+ break;+ case 123: blue_flies(3); break;+ case 124: {+ static const int16_t eff[] = {35, 39, 41, 45, 58, 65, 66, 77, 111};+ return use_effect(eff[rnd(sizeof eff / sizeof eff[0])]);+ }+ case 126: if (!player_hurt(2, -8)) return false; pl.room_damage += 1.2f; player_stats(); break;+ case 127: floor_new(fl.stage); floor_enter_room(fl.start, DIR_NONE); break;+ case 130: pl.pony = 10; break;+ case 133:+ if (!pl.containers) return false;+ player_add_containers(-1), player_add_soul(6, false);+ break;+ case 135:+ if (!player_hurt(1, -5)) return false;+ pl.coins = (uint8_t)(pl.coins + 1 + rnd(2) > 99 ? 99 : pl.coins + 1 + rnd(2));+ break;+ case 136: bomb_place(pl.x, pl.y, 0); break;+ case 137: for (int i = 0; i < MAX_ENTS; i++) if (ents[i].kind == EK_BOMB) ents[i].t = 1; break;+ case 145: blue_flies(2 + rnd(3)); break;+ case 146: pl.eternal = 1; break;+ case 158: floor_reveal(7); player_add_soul(2, false); break;+ case 160: {+ for (int k = 0; k < 4; k++) {+ Ent *e = NULL;+ for (int t = 0; t < 10 && !e; t++) {+ Ent *c = &ents[rnd(MAX_ENTS)];+ if (c->kind == EK_MON && !(c->flags & EF_FRIEND)) e = c;+ }+ float x = e ? e->x : ROOM_X + 20 + rnd(220), y = e ? e->y : ROOM_Y + 20 + rnd(100);+ laser_fire(x, y, -1, 14, pl.damage * 0.5f + 1, 25, 2, false);+ }+ break;+ }+ case 164: {+ int d = last_dir();+ Tear *t = tear_fire(pl.x, pl.y, DIR_X[d] * 4, DIR_Y[d] * 4, 23, TF_PIERCE | TF_SPECTRAL);+ if (t) t->size = 1.8f, t->flags |= TF_BLOOD;+ break;+ }+ case 166: /* D20: the room's pickups rolled again */+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_PICKUP && !(ents[i].flags & EF_SHOP)) {+ float x = ents[i].x, y = ents[i].y;+ ents[i].kind = EK_NONE;+ pickup_spawn(pickup_random_type(), 0, x, y, false);+ }+ break;+ case 170: {+ Ent *e = ent_nearest_monster(pl.x, pl.y, 999);+ if (e) ent_damage(e, 40, 0, 0, 0), fx_spawn(FX_SHOCKWAVE, e->x, e->y, 12);+ break;+ }+ case 171: for (int i = 0; i < MAX_ENTS; i++) if (ents[i].kind == EK_MON) ent_damage(&ents[i], 10, 0, 0, 0), ents[i].slow = 120; break;+ case 175: {+ Room *r = room_cur();+ for (int d = 0; d < 4; d++) r->doors_open |= (uint8_t)(1 << d);+ break;+ }+ case 177: {+ if (!pl.coins) return false;+ pl.coins--;+ int k = rnd(100);+ if (k < 15) pickup_spawn(PK_PENNY, 0, pl.x, pl.y + 12, true), pickup_spawn(PK_PENNY, 0, pl.x, pl.y + 12, true);+ else if (k < 22) pickup_spawn(PK_HEART, 0, pl.x, pl.y + 12, true);+ else if (k < 27) pickup_spawn(PK_BOMB, 0, pl.x, pl.y + 12, true);+ else if (k < 32) pickup_spawn(PK_KEY, 0, pl.x, pl.y + 12, true);+ else if (k < 34) mon_spawn(MON_FLY, pl.x, pl.y + 20);+ break;+ }+ case 186: if (!player_hurt(2, -8)) return false; hurt_all(40); break;+ case 192: pl.telepathy = 1; player_stats(); break;+ default: return false;+ }+ return true;+}++void item_use_active(void) {+ const ItemInfo *ii = item_info(pl.active);+ if (!ii) return;+ int need = ii->charge == 255 ? 1 : ii->charge;+ if (pl.active_charge < need && ii->charge != 0) return;+ if (!use_effect(pl.active)) return;+ if (ii->charge == 255) { /* used once: gone */+ pl.active = 0, pl.active_charge = 0;+ return;+ }+ pl.active_charge = (int16_t)(pl.active_charge - need);+ if (pl.active_charge < 0) pl.active_charge = 0;+ player_stats();+}++/* a room cleared: a charge, and the familiars that bring things */+void item_room_cleared(void) {+ const ItemInfo *ii = item_info(pl.active);+ if (ii && ii->charge && ii->charge != 255) {+ int max = ii->charge * (pl.has[63] ? 2 : 1);+ pl.active_charge = (int16_t)(pl.active_charge + 1 + (pl.has[116] ? 1 : 0));+ if (pl.active_charge > max) pl.active_charge = (int16_t)max;+ }+ static uint8_t rooms;+ rooms++;+ if (pl.has[94] && rooms % 2 == 0) pickup_spawn(PK_PENNY, 0, pl.x, pl.y + 10, true);+ if (pl.has[96] && rooms % 3 == 0) pickup_spawn(PK_HALF_HEART, 0, pl.x, pl.y + 10, true);+ if (pl.has[131] && rooms % 3 == 1) pickup_spawn(PK_BOMB, 0, pl.x, pl.y + 10, true);+ if (pl.has[98] && rooms % 6 == 0) pickup_spawn(PK_SOUL, 0, pl.x, pl.y + 10, true);+}++/* The D6: every item in the room rolled again, from the room's own pool */+void item_reroll_room(void) {+ int kind = room_cur()->kind;+ int pool = kind == ROOM_SHOP ? POOL_SHOP : kind == ROOM_BOSS ? POOL_BOSS : kind == ROOM_DEVIL ? POOL_DEVIL+ : kind == ROOM_ANGEL ? POOL_ANGEL : kind == ROOM_SECRET ? POOL_SECRET : kind == ROOM_LIBRARY ? POOL_LIBRARY : POOL_TREASURE;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_PEDESTAL && ents[i].t3) {+ ents[i].t3 = (int16_t)item_from_pool(pool);+ fx_spawn(FX_POOF, ents[i].x, ents[i].y - 8, 10);+ }+}++/* ---------------------------------------------------------------- pills: 13 colours, each run's effects shuffled */+enum { PL_BAD_GAS, PL_BAD_TRIP, PL_BALLS, PL_BOMBS_KEYS, PL_DIARRHEA, PL_FULL_HEALTH, PL_HEALTH_DOWN, PL_HEALTH_UP,+ PL_PILLS, PL_PUBERTY, PL_PRETTY_FLY, PL_RANGE_DOWN, PL_RANGE_UP, PL_SPEED_DOWN, PL_SPEED_UP, PL_TEARS_DOWN,+ PL_TEARS_UP, PL_LUCK_DOWN, PL_LUCK_UP, PL_TELEPILLS, PL_48H, PL_HEMATEMESIS, PL_PARALYSIS, PL_SEE, PL_PHEROMONES,+ PL_AMNESIA, PL_LEMON, PL_COUNT };+static const char *const pill_names[PL_COUNT] = {+ "BAD GAS", "BAD TRIP", "BALLS OF STEEL", "BOMBS ARE KEY", "EXPLOSIVE DIARRHEA", "FULL HEALTH", "HEALTH DOWN",+ "HEALTH UP", "I FOUND PILLS", "PUBERTY", "PRETTY FLY", "RANGE DOWN", "RANGE UP", "SPEED DOWN", "SPEED UP", "TEARS DOWN",+ "TEARS UP", "LUCK DOWN", "LUCK UP", "TELEPILLS", "48 HOUR ENERGY", "HEMATEMESIS", "PARALYSIS", "I CAN SEE FOREVER!",+ "PHEROMONES", "AMNESIA", "LEMON PARTY"};+#define pill_effect pl.pill_effect+static bool pill_bad(int e) {+ return e == PL_BAD_TRIP || e == PL_HEALTH_DOWN || e == PL_RANGE_DOWN || e == PL_SPEED_DOWN || e == PL_TEARS_DOWN+ || e == PL_LUCK_DOWN || e == PL_AMNESIA || e == PL_PARALYSIS;+}+void pills_shuffle(void) {+ uint8_t all[PL_COUNT];+ for (int i = 0; i < PL_COUNT; i++) all[i] = (uint8_t)i;+ for (int i = PL_COUNT - 1; i > 0; i--) {+ int j = rnd(i + 1);+ uint8_t t = all[i];+ all[i] = all[j], all[j] = t;+ }+ for (int i = 0; i < 14; i++) pill_effect[i] = all[i];+}+const char *pill_name(int color, bool known) { return known ? pill_names[pill_effect[color % 14]] : "PILL"; }++void pill_use(int color) {+ int e = pill_effect[color % 14];+ /* PHD: the bad ones turn good */+ if (pl.has[75] && pill_bad(e)) {+ static const uint8_t good[] = {PL_BALLS, PL_HEALTH_UP, PL_RANGE_UP, PL_SPEED_UP, PL_TEARS_UP, PL_LUCK_UP, PL_FULL_HEALTH};+ e = good[e % sizeof good];+ }+ pl.pill_known[color % 16] = (uint8_t)(e + 1);+ hud_banner(pill_names[e], "");+ switch (e) {+ case PL_BAD_GAS: use_effect(111); break;+ case PL_BAD_TRIP: if (player_hearts_total() <= 2) player_add_soul(4, false), hud_banner("BALLS OF STEEL", ""); else player_hurt(2, -9); break;+ case PL_BALLS: player_add_soul(4, false); break;+ case PL_BOMBS_KEYS: { uint8_t t = pl.bombs; pl.bombs = pl.keys, pl.keys = t; break; }+ case PL_DIARRHEA: for (int i = 0; i < 5; i++) bomb_place(pl.x + rnd(20) - 10, pl.y + rnd(16) - 8, 0); break;+ case PL_FULL_HEALTH: player_heal(24); break;+ case PL_HEALTH_DOWN: if (pl.containers > 1) player_add_containers(-1); break;+ case PL_HEALTH_UP: player_add_containers(1); break;+ case PL_PRETTY_FLY: blue_flies(2); break;+ case PL_RANGE_DOWN: pl.pill_range -= 2; break;+ case PL_RANGE_UP: pl.pill_range += 2.5f; break;+ case PL_SPEED_DOWN: pl.pill_speed -= 0.12f; break;+ case PL_SPEED_UP: pl.pill_speed += 0.15f; break;+ case PL_TEARS_DOWN: pl.pill_tears -= 0.28f; break;+ case PL_TEARS_UP: pl.pill_tears += 0.35f; break;+ case PL_LUCK_DOWN: pl.pill_luck -= 1; break;+ case PL_LUCK_UP: pl.pill_luck += 1; break;+ case PL_TELEPILLS: random_room_teleport(-1); break;+ case PL_48H: { const ItemInfo *ii = item_info(pl.active); if (ii && ii->charge && ii->charge != 255) pl.active_charge = ii->charge; break; }+ case PL_HEMATEMESIS: { int n = pl.red / 2; pl.red = pl.containers ? 1 : 0; for (int i = 0; i < n && i < 5; i++) pickup_spawn(PK_HEART, 0, pl.x, pl.y, true); break; }+ case PL_PARALYSIS: pl.invuln = 0; pl.vx = pl.vy = 0; break;+ case PL_SEE: floor_reveal(4); break;+ case PL_PHEROMONES: for (int i = 0; i < MAX_ENTS; i++) if (ents[i].kind == EK_MON && !(ents[i].flags & EF_BOSS) && dist2(ents[i].x, ents[i].y, pl.x, pl.y) < 80 * 80) ents[i].flags |= EF_FRIEND; break;+ case PL_AMNESIA: for (int i = 0; i < fl.nrooms; i++) if (i != fl.cur) fl.rooms[i].state &= (uint8_t)~RS_SEEN; break;+ case PL_LEMON: creep_spawn(pl.x, pl.y, 30, 240, false, 2); break;+ default: break;+ }+ if (pl.has[75]) player_heal(1);+ player_stats();+}++/* ---------------------------------------------------------------- cards */+static const char *const card_names[32] = {+ "", "0 - THE FOOL", "I - THE MAGICIAN", "II - THE HIGH PRIESTESS", "III - THE EMPRESS", "IV - THE EMPEROR",+ "V - THE HIEROPHANT", "VI - THE LOVERS", "VII - THE CHARIOT", "VIII - JUSTICE", "IX - THE HERMIT",+ "X - WHEEL OF FORTUNE", "XI - STRENGTH", "XII - THE HANGED MAN", "XIII - DEATH", "XIV - TEMPERANCE",+ "XV - THE DEVIL", "XVI - THE TOWER", "XVII - THE STARS", "XVIII - THE MOON", "XIX - THE SUN", "XX - JUDGEMENT",+ "XXI - THE WORLD", "2 OF CLUBS", "2 OF DIAMONDS", "2 OF SPADES", "2 OF HEARTS", "ACE OF CLUBS", "ACE OF DIAMONDS",+ "ACE OF SPADES", "ACE OF HEARTS", "JOKER"};+const char *card_name(int c) { return c > 0 && c < 32 ? card_names[c] : "CARD"; }++static void pickups_to(int type) {+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_PICKUP && !(ents[i].flags & EF_SHOP)) {+ float x = ents[i].x, y = ents[i].y;+ ents[i].kind = EK_NONE;+ pickup_spawn(type, 0, x, y, false);+ }+}++void card_use(int c) {+ hud_banner(card_name(c), "");+ switch (c) {+ case 1: room_teleport(fl.start); break;+ case 2: pl.telepathy = 1; break;+ case 3: use_effect(86); break;+ case 4: pl.room_damage += 1.5f; pl.unicorn = 0; break;+ case 5: room_teleport(fl.boss_room); break;+ case 6: player_add_soul(4, false); break;+ case 7: for (int i = 0; i < 2; i++) pickup_spawn(PK_HEART, 0, pl.x, pl.y + 10, true); break;+ case 8: pl.unicorn = 240; break;+ case 9: { static const uint8_t k[4] = {PK_PENNY, PK_BOMB, PK_KEY, PK_HEART}; for (int i = 0; i < 4; i++) pickup_spawn(k[i], 0, pl.x, pl.y + 10, true); break; }+ case 10: for (int i = 0; i < fl.nrooms; i++) if (fl.rooms[i].kind == ROOM_SHOP) { room_teleport(i); break; } break;+ case 11: machine_spawn(rnd(2) ? MA_SLOT : MA_FORTUNE, pl.x, pl.y - 30); break;+ case 12: pl.room_damage += 1.0f; player_add_containers(0); break;+ case 13: pl.bible = 1; break;+ case 14: hurt_all(40); break;+ case 15: machine_spawn(MA_BLOOD, pl.x, pl.y - 30); break;+ case 16: pl.belial = 1; break;+ case 17: use_effect(65); break;+ case 18: for (int i = 0; i < fl.nrooms; i++) if (fl.rooms[i].kind == ROOM_TREASURE) { room_teleport(i); break; } break;+ case 19: for (int i = 0; i < fl.nrooms; i++) if (fl.rooms[i].kind == ROOM_SECRET) { room_teleport(i); break; } break;+ case 20: player_heal(24); floor_reveal(1); hurt_all(100); break;+ case 21: machine_spawn(MA_BEGGAR, pl.x, pl.y - 30); break;+ case 22: floor_reveal(7); break;+ case 23: pl.bombs = (uint8_t)(pl.bombs * 2 > 99 ? 99 : pl.bombs * 2); break;+ case 24: pl.coins = (uint8_t)(pl.coins * 2 > 99 ? 99 : pl.coins * 2); break;+ case 25: pl.keys = (uint8_t)(pl.keys * 2 > 99 ? 99 : pl.keys * 2); break;+ case 26: player_heal(pl.red); break;+ case 27: pickups_to(PK_BOMB); break;+ case 28: pickups_to(PK_PENNY); break;+ case 29: pickups_to(PK_KEY); break;+ case 30: pickups_to(PK_HEART); break;+ case 31: /* the Joker: to the Devil's (or an angel's) room */+ floor_open_devil();+ room_teleport(fl.devil_room);+ break;+ default: break;+ }+ player_stats();+}++/* ---------------------------------------------------------------- familiars */+static const uint8_t familiar_items[] = {8, 10, 57, 67, 73, 88, 95, 99, 100, 112, 113, 128, 144, 155, 163, 167, 174, 188};+int familiars_count(void) {+ int n = 0;+ for (unsigned i = 0; i < sizeof familiar_items; i++) n += pl.has[familiar_items[i]];+ return n;+}++void familiars_spawn(void) {+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_FAMILIAR) ents[i].kind = EK_NONE;+ int follow = 0, orbit = 0;+ for (unsigned k = 0; k < sizeof familiar_items; k++) {+ int id = familiar_items[k];+ for (int n = 0; n < pl.has[id]; n++) {+ int count = id == 10 ? 2 : 1;+ for (int j = 0; j < count; j++) {+ Ent *e = ent_new(EK_FAMILIAR, pl.x, pl.y + 10);+ if (!e) return;+ e->type = (uint8_t)(id & 0xFF), e->t3 = (int16_t)id, e->spr = (int16_t)item_sprite(id), e->r = 5;+ bool orbital = id == 10 || id == 57 || id == 73 || id == 112 || id == 128;+ e->state = orbital;+ e->sub = (uint8_t)(orbital ? orbit++ : follow++);+ e->t = (int16_t)(rnd(20));+ }+ }+ }+}++void familiar_update(Ent *e) {+ int id = e->t3;+ if (e->state) { /* orbitals: round and round him, stopping shots */+ float r = id == 57 || id == 128 ? 34 : id == 112 ? 26 : 18, sp = id == 57 || id == 128 ? 0.07f : 0.1f;+ float a = g_frame * sp + e->sub * 2.4f;+ e->x = pl.x + fcos(a) * r, e->y = pl.y - 4 + fsin(a) * r * 0.8f;+ for (int i = 0; i < MAX_TEARS; i++) {+ Tear *t = &tears[i];+ if (t->active && (t->flags & TF_ENEMY) && dist2(t->x, t->y, e->x, e->y) < 8 * 8) t->active = 0;+ }+ if (id == 57 || id == 128 || id == 73 || id == 112)+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *m = &ents[i];+ if (m->kind == EK_MON && !(m->flags & EF_FRIEND) && dist2(m->x, m->y, e->x, e->y) < (m->r + 6) * (m->r + 6) && (g_frame & 3) == 0)+ ent_damage(m, id == 57 ? 5 : id == 128 ? 2 : 3, 0, 0, 0);+ }+ return;+ }+ /* followers: a line behind him */+ float tx = pl.x - DIR_X[pl.face] * (14 + e->sub * 12), ty = pl.y - DIR_Y[pl.face] * (12 + e->sub * 10) + 2;+ if (id == 188) tx = 320 - pl.x, ty = 240 - pl.y + 0; /* Abel: across the room */+ if (id == 155) { /* The Peeper: bounces about, hurting */+ if (!e->vx && !e->vy) e->vx = 1.4f, e->vy = 1.1f;+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, 5, true, true);+ if (!e->vx) e->vx = -ox;+ if (!e->vy) e->vy = -oy;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && !(ents[i].flags & EF_FRIEND) && dist2(ents[i].x, ents[i].y, e->x, e->y) < (ents[i].r + 6) * (ents[i].r + 6) && (g_frame & 3) == 0)+ ent_damage(&ents[i], 2, 0, 0, 0);+ return;+ }+ if (id == 88 && e->c > 0) { /* Little Chubby: thrown at them */+ e->x += e->vx, e->y += e->vy, e->c -= 1;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_MON && !(ents[i].flags & EF_FRIEND) && dist2(ents[i].x, ents[i].y, e->x, e->y) < (ents[i].r + 6) * (ents[i].r + 6))+ ent_damage(&ents[i], 1.5f, 0, 0, 0);+ if (e->x < ROOM_X || e->x > ROOM_R || e->y < ROOM_Y || e->y > ROOM_B) e->c = 0;+ return;+ }+ e->x += (tx - e->x) * 0.12f, e->y += (ty - e->y) * 0.12f;+ if (e->t > 0) {+ e->t--;+ return;+ }+ /* they cry when he does */+ int dir = pl.shoot_dir;+ if (id == 113) { /* Demon Baby: at the nearest monster, on its own */+ Ent *m = ent_nearest_monster(e->x, e->y, 110);+ if (m) {+ float dx = m->x - e->x, dy = m->y - e->y, d = fsqrt(dx * dx + dy * dy) + 0.1f;+ tear_fire(e->x, e->y, dx / d * 5, dy / d * 5, 3, TF_FAMILIAR);+ e->t = 12;+ }+ return;+ }+ if (dir == DIR_NONE || id == 144 || id == 99 * 0) return;+ float sx = DIR_X[dir] * 5, sy = DIR_Y[dir] * 5;+ if (id == 188) sx = -sx, sy = -sy;+ switch (id) {+ case 8: tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR); e->t = 22; break;+ case 67: tear_fire(e->x, e->y, sx, sy, 7, TF_FAMILIAR | TF_BLOOD); e->t = 22; break;+ case 100: tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR | TF_HOMING); e->t = 22; break;+ case 163: tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR | TF_SPECTRAL | TF_PIERCE); e->t = 22; break;+ case 99: tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR | TF_SLOW); e->t = 22; break;+ case 95: laser_fire(e->x, e->y - 6, dir, 2, 3.5f, 3, 1, false); e->t = 30; break;+ case 167: for (int k = -1; k <= 1; k += 2) tear_fire(e->x, e->y, sx + DIR_Y[dir] * k, sy + DIR_X[dir] * k, 4, TF_FAMILIAR); e->t = 22; break;+ case 174: { static const uint32_t f[4] = {TF_HOMING, TF_PIERCE, TF_POISON, TF_SLOW}; tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR | f[rnd(4)]); e->t = 22; break; }+ case 188: tear_fire(e->x, e->y, sx, sy, 3.5f, TF_FAMILIAR); e->t = 22; break;+ case 88: e->vx = sx * 1.2f, e->vy = sy * 1.2f, e->c = 40, e->t = 60; break;+ default: break;+ }+}++void familiar_draw(Ent *e) {+ int bob = (int)((g_frame / 5 + e->sub) % 2);+ if (e->t3 == 10) { /* the halo's flies */+ gfx_sprite(SPR_FLY, 0, (int)e->x, (int)e->y + 3 - bob, 0, 32);+ return;+ }+ if (e->t3 == 57 || e->t3 == 128) {+ gfx_sprite_tint(SPR_ATTACK_FLY, 0, (int)e->x, (int)e->y + 3 - bob, 0, rgb565(0x80FF80), e->t3 == 57 ? 8 : 0, 32);+ return;+ }+ gfx_sprite_scaled(e->spr, 0, (int)e->x, (int)e->y + 4 - bob, 200, 200, 0, 0, 0, 32);+}diff --git a/games/isaac/src/main.c b/games/isaac/src/main.cnew file mode 100644index 00000000..818777f1--- /dev/null+++ b/games/isaac/src/main.c@@ -0,0 +1,46 @@+/* The Binding of Isaac for the NumWorks calculator.+ *+ * Inspired by The Binding of Isaac by Edmund McMillen and Florian Himsl, not+ * affiliated with them. Made by Mason Chen as part of NumPlay. */+#include "game.h"+#include "gfx.h"+#include "plat.h"+#include "res.h"++#ifndef HOST+const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Isaac";+const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;+extern const uint8_t is_data[];++uint32_t perf_frames __attribute__((used)); /* read by tools/emu.py */+uint32_t perf_updates __attribute__((used));++int main(void) {+ uint16_t band[SCREEN_W * GFX_BAND]; /* (here: on the stack, out of the RAM apps get) */+ gfx_set_band_memory(band);+ plat_begin();+ is_bin = is_data;+ g_rng ^= plat_millis() * 2654435761u;+ game_init();+ uint32_t next = plat_millis() * 3; /* in thirds of a millisecond: 100 a frame, the game's 30 a second */+ while (g_running) {+ /* the game's frames: drawn when caught up (at most 2 updates for a drawing) */+ int n = 0;+ uint32_t now = plat_millis() * 3;+ while ((int32_t)(now - next) >= 0 && n < 2 && g_running) {+ game_frame(plat_keys());+ perf_updates++;+ next += 100;+ n++;+ }+ if (!g_running) break;+ if ((int32_t)(now - next) > 600) next = now; /* far behind: no rushing */+ if (n) {+ game_draw();+ perf_frames++;+ } else+ plat_sleep(1);+ }+ return plat_end();+}+#endifdiff --git a/games/isaac/src/monsters.c b/games/isaac/src/monsters.cnew file mode 100644index 00000000..1d8e409d--- /dev/null+++ b/games/isaac/src/monsters.c@@ -0,0 +1,727 @@+/* The monsters, and how each moves, as in Rebirth: health, speed and the+ * way it hunts or shoots. Speeds are pixels a frame (the game's units+ * halved); a shot goes 3 to 4 a frame. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++enum {+ AI_CHASE, AI_FLY, AI_ATTACK_FLY, AI_POOTER, AI_HORF, AI_HOST, AI_CLOTTY, AI_FLEE, AI_HIVE, AI_CHARGER, AI_GLOBIN,+ AI_BOOM_FLY, AI_MAW, AI_HOPPER, AI_LEAPER, AI_SPIDER, AI_KNIGHT, AI_DIP, AI_GUSHER, AI_SKINNY, AI_BONY, AI_BABY,+ AI_VIS, AI_EYE, AI_SUCKER, AI_BOIL, AI_SPITTY, AI_BRAIN, AI_NEST, AI_DEATHS_HEAD, AI_MOMS_HAND, AI_GRIMACE,+ AI_BAT, AI_IMP, AI_TUMOR, AI_POKY, AI_BURROW, AI_LEECH, AI_TETHER, AI_WALL, AI_GAPING, AI_FATTY, AI_KEEPER,+ AI_SWARMER, AI_MULLIGAN, AI_WANDER, AI_BLUE_FLY+};++typedef struct {+ const char *name;+ uint8_t ai;+ uint8_t hp;+ uint8_t r; /* radius */+ uint8_t speed; /* tenths of a pixel a frame */+ uint8_t flying;+ int16_t spr;+} MonInfo;++#define M(n, ai, hp, r, sp, fl, spr) {n, ai, hp, r, sp, fl, spr}+static const MonInfo mons[] = {+ [MON_GAPER] = M("GAPER", AI_CHASE, 10, 7, 14, 0, SPR_GAPER),+ [MON_FROWNING_GAPER] = M("FROWNING GAPER", AI_CHASE, 10, 7, 12, 0, SPR_FROWNING_GAPER),+ [MON_GUSHER] = M("GUSHER", AI_GUSHER, 10, 7, 12, 0, SPR_GUSHER),+ [MON_PACER] = M("PACER", AI_WANDER, 10, 7, 14, 0, SPR_PACER),+ [MON_HORF] = M("HORF", AI_HORF, 10, 7, 0, 0, SPR_HORF),+ [MON_FLY] = M("FLY", AI_FLY, 5, 5, 6, 1, SPR_FLY),+ [MON_POOTER] = M("POOTER", AI_POOTER, 8, 6, 8, 1, SPR_POOTER),+ [MON_SUPER_POOTER] = M("SUPER POOTER", AI_POOTER, 14, 7, 8, 1, SPR_SUPER_POOTER),+ [MON_CLOTTY] = M("CLOTTY", AI_CLOTTY, 15, 8, 10, 0, SPR_CLOTTY),+ [MON_CLOT] = M("CLOT", AI_CLOTTY, 13, 7, 10, 0, SPR_CLOT),+ [MON_IBLOB] = M("I. BLOB", AI_CLOTTY, 20, 8, 10, 0, SPR_IBLOB),+ [MON_MULLIGAN] = M("MULLIGAN", AI_MULLIGAN, 13, 7, 14, 0, SPR_MULLIGAN),+ [MON_ATTACK_FLY] = M("ATTACK FLY", AI_ATTACK_FLY, 5, 5, 20, 1, SPR_ATTACK_FLY),+ [MON_MAGGOT] = M("MAGGOT", AI_CHARGER, 7, 6, 8, 0, SPR_MAGGOT),+ [MON_HIVE] = M("HIVE", AI_HIVE, 25, 9, 10, 0, SPR_HIVE),+ [MON_CHARGER] = M("CHARGER", AI_CHARGER, 10, 6, 10, 0, SPR_CHARGER),+ [MON_GLOBIN] = M("GLOBIN", AI_GLOBIN, 20, 8, 13, 0, SPR_GLOBIN),+ [MON_BOOM_FLY] = M("BOOM FLY", AI_BOOM_FLY, 20, 7, 15, 1, SPR_BOOM_FLY),+ [MON_RED_BOOM_FLY] = M("RED BOOM FLY", AI_BOOM_FLY, 20, 7, 15, 1, SPR_RED_BOOM_FLY),+ [MON_MAW] = M("MAW", AI_MAW, 15, 7, 8, 1, SPR_MAW),+ [MON_RED_MAW] = M("RED MAW", AI_MAW, 25, 7, 8, 1, SPR_RED_MAW),+ [MON_HOST] = M("HOST", AI_HOST, 14, 7, 0, 0, SPR_HOST),+ [MON_RED_HOST] = M("RED HOST", AI_HOST, 20, 7, 0, 0, SPR_RED_HOST),+ [MON_HOPPER] = M("HOPPER", AI_HOPPER, 10, 6, 0, 0, SPR_HOPPER),+ [MON_TRITE] = M("TRITE", AI_LEAPER, 10, 6, 0, 0, SPR_TRITE),+ [MON_BOIL] = M("BOIL", AI_BOIL, 15, 7, 0, 0, SPR_BOIL),+ [MON_SPITTY] = M("SPITTY", AI_SPITTY, 15, 7, 12, 0, SPR_SPITTY),+ [MON_BRAIN] = M("BRAIN", AI_BRAIN, 20, 8, 16, 0, SPR_BRAIN),+ [MON_LEAPER] = M("LEAPER", AI_LEAPER, 20, 8, 0, 0, SPR_LEAPER),+ [MON_MR_MAW] = M("MR. MAW", AI_TETHER, 22, 7, 14, 0, SPR_MR_MAW),+ [MON_BABY] = M("BABY", AI_BABY, 12, 7, 0, 1, SPR_BABY),+ [MON_ANGELIC_BABY] = M("ANGELIC BABY", AI_BABY, 25, 7, 0, 1, SPR_ANGELIC_BABY),+ [MON_VIS] = M("VIS", AI_VIS, 22, 8, 12, 0, SPR_VIS),+ [MON_GUTS] = M("GUTS", AI_WALL, 20, 7, 14, 0, SPR_GUTS),+ [MON_KNIGHT] = M("KNIGHT", AI_KNIGHT, 12, 7, 10, 0, SPR_KNIGHT),+ [MON_SELFLESS_KNIGHT] = M("SELFLESS KNIGHT", AI_KNIGHT, 18, 7, 10, 0, SPR_SELFLESS_KNIGHT),+ [MON_LEECH] = M("LEECH", AI_LEECH, 15, 6, 22, 0, SPR_LEECH),+ [MON_KAMIKAZE_LEECH] = M("KAMIKAZE LEECH", AI_LEECH, 12, 6, 24, 0, SPR_KAMIKAZE_LEECH),+ [MON_PARA_BITE] = M("PARA-BITE", AI_BURROW, 15, 6, 0, 0, SPR_PARA_BITE),+ [MON_EYE] = M("EYE", AI_EYE, 20, 7, 0, 0, SPR_EYE),+ [MON_BLOODSHOT_EYE] = M("BLOODSHOT EYE", AI_EYE, 25, 7, 0, 0, SPR_BLOODSHOT_EYE),+ [MON_SUCKER] = M("SUCKER", AI_SUCKER, 10, 6, 12, 1, SPR_SUCKER),+ [MON_SPIT] = M("SPIT", AI_SUCKER, 10, 6, 12, 1, SPR_SPIT),+ [MON_EMBRYO] = M("EMBRYO", AI_HOPPER, 10, 6, 0, 0, SPR_EMBRYO),+ [MON_MOTER] = M("MOTER", AI_ATTACK_FLY, 12, 6, 14, 1, SPR_MOTER),+ [MON_SPIDER] = M("SPIDER", AI_SPIDER, 5, 5, 30, 0, SPR_SPIDER),+ [MON_KEEPER] = M("KEEPER", AI_KEEPER, 18, 7, 0, 0, SPR_KEEPER),+ [MON_GURGLE] = M("GURGLE", AI_CHASE, 20, 7, 18, 0, SPR_GURGLE),+ [MON_WALKING_BOIL] = M("WALKING BOIL", AI_SPITTY, 25, 8, 9, 0, SPR_WALKING_BOIL),+ [MON_BUTTLICKER] = M("BUTTLICKER", AI_SPIDER, 20, 7, 22, 0, SPR_BUTTLICKER),+ [MON_HANGER] = M("HANGER", AI_POOTER, 20, 7, 10, 1, SPR_HANGER),+ [MON_SWARMER] = M("SWARMER", AI_SWARMER, 15, 6, 10, 1, SPR_SWARMER),+ [MON_HEART] = M("HEART", AI_FLY, 15, 6, 10, 1, SPR_HEART),+ [MON_MASK] = M("MASK", AI_CHARGER, 255, 7, 14, 0, SPR_MASK),+ [MON_BIG_SPIDER] = M("BIG SPIDER", AI_SPIDER, 13, 7, 26, 0, SPR_BIG_SPIDER),+ [MON_NEST] = M("NEST", AI_NEST, 25, 9, 0, 0, SPR_NEST),+ [MON_BABY_LONG_LEGS] = M("BABY LONG LEGS", AI_NEST, 40, 9, 10, 0, SPR_BABY_LONG_LEGS),+ [MON_FATTY] = M("FATTY", AI_FATTY, 25, 9, 7, 0, SPR_FATTY),+ [MON_PALE_FATTY] = M("PALE FATTY", AI_FATTY, 30, 9, 7, 0, SPR_PALE_FATTY),+ [MON_FAT_SACK] = M("FAT SACK", AI_FATTY, 35, 9, 6, 0, SPR_FAT_SACK),+ [MON_DEATHS_HEAD] = M("DEATH'S HEAD", AI_DEATHS_HEAD, 255, 7, 18, 1, SPR_DEATHS_HEAD),+ [MON_MOMS_HAND] = M("MOM'S HAND", AI_MOMS_HAND, 50, 10, 0, 1, SPR_MOMS_HAND),+ [MON_DIP] = M("DIP", AI_DIP, 6, 5, 0, 0, SPR_DIP),+ [MON_SQUIRT] = M("SQUIRT", AI_DIP, 15, 8, 0, 0, SPR_SQUIRT),+ [MON_SKINNY] = M("SKINNY", AI_SKINNY, 20, 7, 8, 0, SPR_SKINNY),+ [MON_BONY] = M("BONY", AI_BONY, 10, 7, 11, 0, SPR_BONY),+ [MON_ONE_TOOTH] = M("ONE TOOTH", AI_BAT, 15, 6, 16, 1, SPR_ONE_TOOTH),+ [MON_GAPING_MAW] = M("GAPING MAW", AI_GAPING, 30, 8, 0, 1, SPR_GAPING_MAW),+ [MON_RAGLING] = M("RAGLING", AI_GLOBIN, 16, 7, 16, 0, SPR_RAGLING),+ [MON_NIGHT_CRAWLER] = M("NIGHT CRAWLER", AI_BURROW, 20, 7, 0, 0, SPR_NIGHT_CRAWLER),+ [MON_DART_FLY] = M("DART FLY", AI_ATTACK_FLY, 8, 5, 26, 1, SPR_DART_FLY),+ [MON_FAT_BAT] = M("FAT BAT", AI_BAT, 25, 8, 12, 1, SPR_FAT_BAT),+ [MON_IMP] = M("IMP", AI_IMP, 25, 7, 0, 0, SPR_IMP),+ [MON_TUMOR] = M("TUMOR", AI_TUMOR, 20, 7, 6, 1, SPR_TUMOR),+ [MON_POKY] = M("POKY", AI_POKY, 255, 7, 20, 0, SPR_POKY),+ [MON_STONE_GRIMACE] = M("STONE GRIMACE", AI_GRIMACE, 255, 8, 0, 0, SPR_STONE_GRIMACE),+ [MON_CONJOINED_FATTY] = M("CONJOINED FATTY", AI_FATTY, 40, 10, 6, 0, SPR_CONJOINED_FATTY),+ [MON_ROUND_WORM] = M("ROUND WORM", AI_BURROW, 15, 6, 0, 0, SPR_ROUND_WORM),+ [MON_WALL_CREEP] = M("WALL CREEP", AI_WALL, 15, 7, 12, 0, SPR_WALL_CREEP),+ [MON_MEMBRAIN] = M("MEMBRAIN", AI_BRAIN, 45, 10, 10, 0, SPR_MEMBRAIN),+ [MON_LUMP] = M("LUMP", AI_BURROW, 20, 7, 0, 0, SPR_LUMP),+ [MON_FRED] = M("FRED", AI_BURROW, 30, 8, 0, 0, SPR_FRED),+ [MON_HOMUNCULUS] = M("HOMUNCULUS", AI_TETHER, 25, 7, 16, 0, SPR_HOMUNCULUS),+ [MON_BEGOTTEN] = M("BEGOTTEN", AI_TETHER, 35, 8, 16, 0, SPR_BEGOTTEN),+ [MON_CHUBBER] = M("CHUBBER", AI_VIS, 25, 8, 10, 0, SPR_CHUBBER),+ [MON_FLAMING_HOPPER] = M("FLAMING HOPPER", AI_HOPPER, 15, 6, 0, 0, SPR_FLAMING_HOPPER),+ [MON_BLUE_FLY] = M("BLUE FLY", AI_BLUE_FLY, 1, 4, 30, 1, SPR_FLY),+};++const char *mon_name(int type) {+ if (type >= 0 && type < (int)(sizeof mons / sizeof mons[0]) && mons[type].name) return mons[type].name;+ return NULL;+}++void mon_set_sprite(Ent *e) {+ if (e->type < sizeof mons / sizeof mons[0] && mons[e->type].name) e->spr = mons[e->type].spr;+}++Ent *mon_spawn(int type, float x, float y) {+ if (type >= MON_MONSTRO) {+ boss_spawn(type, x, y);+ return NULL;+ }+ if (type < 0 || type >= (int)(sizeof mons / sizeof mons[0]) || !mons[type].name) return NULL;+ Ent *e = ent_new(EK_MON, x, y);+ if (!e) return NULL;+ const MonInfo *m = &mons[type];+ e->type = (uint8_t)type;+ e->hp = e->maxhp = m->hp == 255 ? 1000 : m->hp * (1 + (fl.stage - 1) * 0.12f);+ e->r = m->r;+ e->spr = m->spr;+ if (m->flying) e->flags |= EF_FLYING;+ if (m->hp == 255) e->flags |= EF_INVULN | EF_NOCOUNT;+ if (m->ai == AI_HOST) e->flags |= EF_INVULN;+ if (m->ai == AI_MOMS_HAND) e->flags |= EF_HIDDEN | EF_NOHIT;+ if (m->ai == AI_BURROW) e->flags |= EF_HIDDEN | EF_NOHIT | EF_INVULN;+ if (type == MON_BLUE_FLY) e->flags |= EF_FRIEND;+ e->t = (int16_t)(20 + rnd(40));+ e->a = rndf() * 6.2831853f;+ e->c = x, e->b = y; /* (where it began: tethers) */+ return e;+}++/* ---------------------------------------------------------------- helpers */+static float to_player(Ent *e, float *dx, float *dy) {+ float x = pl.x - e->x, y = pl.y - e->y, d = fsqrt(x * x + y * y);+ if (d < 0.01f) d = 0.01f;+ *dx = x / d, *dy = y / d;+ return d;+}+static float spd(Ent *e) {+ float s = mons[e->type].speed / 10.0f;+ if (e->flags & EF_CHAMPION && e->sub == 1) s *= 1.4f;+ return s;+}+static void walk(Ent *e, float dx, float dy, float s, float acc) {+ if (e->fear) dx = -dx, dy = -dy;+ if (e->confuse) dx = fcos(e->a), dy = fsin(e->a), e->a += 0.1f;+ e->vx += (dx * s - e->vx) * acc, e->vy += (dy * s - e->vy) * acc;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.8f, e->flags & EF_FLYING, false);+}+static bool aligned(Ent *e, float tol) { return absf(pl.x - e->x) < tol || absf(pl.y - e->y) < tol; }+static void shot4(Ent *e, float s, bool diag) {+ for (int d = 0; d < 4; d++) {+ float ax = DIR_X[d], ay = DIR_Y[d];+ if (diag) ax = (d & 1) ? 0.7071f : -0.7071f, ay = (d & 2) ? 0.7071f : -0.7071f;+ enemy_shot(e->x, e->y - 6, ax * s, ay * s, 0);+ }+}+static int dir_to_player(Ent *e) {+ float dx = pl.x - e->x, dy = pl.y - e->y;+ if (absf(dx) > absf(dy)) return dx < 0 ? DIR_LEFT : DIR_RIGHT;+ return dy < 0 ? DIR_UP : DIR_DOWN;+}++/* ---------------------------------------------------------------- the monsters' minds */+void mon_update(Ent *e) {+ const MonInfo *m = &mons[e->type];+ float dx, dy, d = to_player(e, &dx, &dy), s = spd(e);+ e->t--;+ if (e->flags & EF_FRIEND && e->type != MON_BLUE_FLY) { /* (charmed: it goes for the others) */+ Ent *o = ent_nearest_monster(e->x, e->y, 400);+ if (o) {+ float ox = o->x - e->x, oy = o->y - e->y, od = fsqrt(ox * ox + oy * oy) + 0.01f;+ walk(e, ox / od, oy / od, s > 0 ? s : 1, 0.2f);+ if (od < e->r + o->r) ent_damage(o, 1, 0, 0, 0);+ }+ return;+ }+ switch (m->ai) {+ case AI_CHASE: { /* walks at him, around what's in the way */+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.15f);+ if (e->type == MON_GURGLE && (e->t & 7) == 0) creep_spawn(e->x, e->y, 7, 90, true, 0);+ break;+ }+ case AI_WANDER: case AI_GUSHER: /* headless: wanders, sprays blood now and then */+ if (e->t <= 0) e->a = rndf() * 6.2831853f, e->t = (int16_t)(30 + rnd(40));+ walk(e, fcos(e->a), fsin(e->a), s, 0.1f);+ if (e->vx == 0 || e->vy == 0) e->a += 1.5f;+ if (m->ai == AI_GUSHER && rnd(70) == 0) enemy_shot(e->x, e->y - 10, (rndf() - 0.5f) * 3, -1 - rndf() * 2, 0);+ break;+ case AI_FLY: { /* a lazy buzz towards him */+ e->a += (rndf() - 0.5f) * 0.8f;+ float wx = dx + fcos(e->a) * 0.9f, wy = dy + fsin(e->a) * 0.9f;+ walk(e, wx, wy, s, 0.1f);+ break;+ }+ case AI_ATTACK_FLY: case AI_BLUE_FLY: {+ if (m->ai == AI_BLUE_FLY) { /* Isaac's: at the nearest monster, then pop */+ Ent *o = ent_nearest_monster(e->x, e->y, 400);+ if (!o) {+ walk(e, dx * (d > 30 ? 1 : -0.2f), dy * (d > 30 ? 1 : -0.2f), 2, 0.2f);+ break;+ }+ float ox = o->x - e->x, oy = o->y - e->y, od = fsqrt(ox * ox + oy * oy) + 0.01f;+ walk(e, ox / od, oy / od, s, 0.25f);+ if (od < e->r + o->r) ent_damage(o, pl.damage * 2, 0, 0, 0), e->kind = EK_NONE;+ break;+ }+ e->a += (rndf() - 0.5f) * 0.4f;+ walk(e, dx + fcos(e->a) * 0.3f, dy + fsin(e->a) * 0.3f, s, 0.12f);+ if (e->type == MON_DART_FLY && e->t <= 0) e->vx = dx * 5, e->vy = dy * 5, e->t = 50;+ break;+ }+ case AI_POOTER: { /* floats this way and that; a shot at him every so often */+ e->a += (rndf() - 0.5f) * 0.5f;+ walk(e, fcos(e->a) * 0.7f + dx * 0.3f, fsin(e->a) * 0.7f + dy * 0.3f, s, 0.06f);+ if (e->t <= 0 && d < 160) {+ e->t = (int16_t)(50 + rnd(30));+ if (e->type == MON_SUPER_POOTER) {+ for (int k = -1; k <= 1; k += 2) enemy_shot(e->x, e->y - 8, dx * 3.5f - dy * k * 0.6f, dy * 3.5f + dx * k * 0.6f, 0);+ } else+ enemy_shot_at(e->x, e->y - 8, 3.5f);+ }+ break;+ }+ case AI_HORF: /* shakes where it is; shoots when he comes near */+ if (e->t <= 0 && d < 150) e->t = 45, enemy_shot_at(e->x, e->y - 6, 4);+ break;+ case AI_HOST: /* a skull, shut: it opens to fire three, then shuts */+ if (e->state == 0) {+ e->flags |= EF_INVULN;+ if (e->t <= 0 && d < 140) e->state = 1, e->t = 30, e->flags &= (uint16_t)~EF_INVULN;+ } else {+ if (e->t == 18) {+ float a = fatan2(dy, dx);+ for (int k = -1; k <= 1; k++) enemy_shot(e->x, e->y - 8, fcos(a + k * 0.3f) * 3.6f, fsin(a + k * 0.3f) * 3.6f, 0);+ if (e->type == MON_RED_HOST) for (int k = -2; k <= 2; k += 4) enemy_shot(e->x, e->y - 8, fcos(a + k * 0.3f) * 3.6f, fsin(a + k * 0.3f) * 3.6f, 0);+ }+ if (e->t <= 0) e->state = 0, e->t = (int16_t)(40 + rnd(30));+ }+ break;+ case AI_CLOTTY: { /* blobs about; four shots at once (an I. Blob, eight) */+ if (e->t <= 0) {+ e->t = (int16_t)(50 + rnd(40));+ if (e->type == MON_IBLOB) shot4(e, 3, false), shot4(e, 3, true);+ else shot4(e, 3, e->type == MON_CLOT);+ e->a = rndf() * 6.2831853f;+ }+ walk(e, fcos(e->a), fsin(e->a), e->t > 25 ? s : 0, 0.1f);+ break;+ }+ case AI_FLEE: case AI_MULLIGAN: case AI_HIVE: /* runs from him; a hive lets flies out */+ walk(e, -dx + fcos(e->a) * 0.4f, -dy + fsin(e->a) * 0.4f, d < 120 ? s : s * 0.3f, 0.1f);+ e->a += 0.05f;+ if (m->ai == AI_HIVE && e->t <= 0) {+ e->t = 80;+ int n = 0;+ for (int i = 0; i < MAX_ENTS; i++) n += ents[i].kind == EK_MON && ents[i].type == MON_FLY;+ if (n < 4) mon_spawn(MON_FLY, e->x, e->y - 6);+ }+ break;+ case AI_CHARGER: case AI_KNIGHT: { /* straight lines; a charge when he is in line */+ if (e->state == 0) {+ if (e->t <= 0 || (e->vx == 0 && e->vy == 0 && e->t < 40)) e->c = (float)rnd(4), e->t = (int16_t)(30 + rnd(50));+ int dd = (int)e->c;+ walk(e, DIR_X[dd], DIR_Y[dd], s, 0.3f);+ e->frame = 0;+ if (e->type != MON_MAGGOT && aligned(e, 8) && d < 200 && grid_line_clear(e->x, e->y, pl.x, pl.y)) {+ int pd = dir_to_player(e);+ if (e->type == MON_KNIGHT || e->type == MON_SELFLESS_KNIGHT || pd == dd || e->type == MON_CHARGER || e->type == MON_MASK)+ e->state = 1, e->c = (float)pd, e->t = 40;+ }+ } else {+ int dd = (int)e->c;+ walk(e, DIR_X[dd], DIR_Y[dd], s * 3, 0.4f);+ if (e->t <= 0 || (e->vx == 0 && e->vy == 0)) e->state = 0, e->t = 30;+ }+ if (m->ai == AI_KNIGHT) e->b = e->c; /* (the way its shield faces) */+ break;+ }+ case AI_GLOBIN: /* falls into goo when beaten, gets back up unless hurt again */+ if (e->state == 1) {+ e->vx = e->vy = 0;+ if (e->t <= 0) e->state = 0, e->hp = e->maxhp, e->flags &= (uint16_t)~EF_NOHIT;+ break;+ }+ {+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.15f);+ }+ break;+ case AI_BOOM_FLY: /* bounces about on the diagonals */+ if (!e->vx && !e->vy) e->vx = s * (rnd(2) ? 1 : -1), e->vy = s * (rnd(2) ? 1 : -1);+ {+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, true, false);+ if (e->vx == 0) e->vx = -ox;+ if (e->vy == 0) e->vy = -oy;+ }+ break;+ case AI_MAW: /* a floating head: drifts at him, shoots when near */+ walk(e, dx, dy, s, 0.05f);+ if (e->t <= 0 && d < 130) e->t = (int16_t)(60 + rnd(30)), enemy_shot_at(e->x, e->y - 8, 3.5f);+ break;+ case AI_HOPPER: case AI_LEAPER: { /* hops; a leaper's jump lands on him */+ if (e->state == 0) {+ e->z = 0;+ if (e->t <= 0) {+ e->state = 1;+ float range = m->ai == AI_LEAPER ? (d < 100 ? d : 100) : 40;+ float a = m->ai == AI_LEAPER ? fatan2(dy, dx) : fatan2(dy, dx) + (rndf() - 0.5f) * 2;+ e->a = a, e->b = range;+ e->t2 = m->ai == AI_LEAPER ? 30 : 16;+ e->t3 = e->t2;+ e->flags |= m->ai == AI_LEAPER ? EF_NOHIT : 0;+ }+ } else {+ float k = 1 - (float)e->t2 / e->t3;+ e->z = -(m->ai == AI_LEAPER ? 60 : 14) * 4 * k * (1 - k);+ float step = e->b / e->t3;+ e->vx = fcos(e->a) * step, e->vy = fsin(e->a) * step;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.8f, e->z < -8, false);+ if (--e->t2 <= 0) {+ e->state = 0, e->z = 0, e->t = (int16_t)(m->ai == AI_LEAPER ? 40 + rnd(30) : 10 + rnd(25));+ e->flags &= (uint16_t)~EF_NOHIT;+ if (e->type == MON_LEAPER) shot4(e, 3, false);+ if (e->type == MON_FLAMING_HOPPER) creep_spawn(e->x, e->y, 8, 60, true, 2);+ }+ }+ break;+ }+ case AI_SPIDER: /* skitters at him in quick dashes */+ if (e->t <= 0) {+ e->t = (int16_t)(12 + rnd(20));+ float a = fatan2(dy, dx) + (rndf() - 0.5f) * 1.6f;+ e->a = a, e->state = (uint8_t)(rnd(3) != 0);+ }+ walk(e, fcos(e->a), fsin(e->a), e->state ? s : 0, 0.3f);+ break;+ case AI_DIP: /* slides in spurts */+ if (e->t <= 0) {+ e->t = (int16_t)(25 + rnd(20));+ float a = rndf() * 6.2831853f;+ float sp = e->type == MON_SQUIRT ? 4.5f : 3.5f;+ e->vx = fcos(a) * sp, e->vy = fsin(a) * sp;+ }+ e->vx *= 0.9f, e->vy *= 0.9f;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r * 0.8f, false, false);+ break;+ case AI_SKINNY: /* slow until he hurts it, then it runs */+ {+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, e->hp < e->maxhp ? s * 2.2f : s, 0.15f);+ }+ break;+ case AI_BONY: /* walks, throws bones when he is in line */+ {+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, d > 60 ? s : 0, 0.15f);+ if (e->t <= 0 && aligned(e, 10) && d < 180) {+ int pd = dir_to_player(e);+ enemy_shot(e->x, e->y - 6, DIR_X[pd] * 4, DIR_Y[pd] * 4, 1);+ e->t = 50;+ }+ }+ break;+ case AI_BABY: case AI_IMP: /* gone, and back elsewhere; then a shot */+ if (e->state == 0) {+ if (e->t <= 0) e->state = 1, e->t = 12;+ } else if (e->state == 1) {+ e->alpha = (uint8_t)(e->t * 2);+ if (e->t <= 0) {+ for (int k = 0; k < 20; k++) {+ float x = ROOM_X + 20 + rnd(220), y = ROOM_Y + 20 + rnd(100);+ if (!solid_at(x, y, false) && dist2(x, y, pl.x, pl.y) > 50 * 50) {+ e->x = x, e->y = y;+ break;+ }+ }+ e->state = 2, e->t = 12;+ }+ } else {+ e->alpha = (uint8_t)(32 - e->t * 2);+ if (e->t <= 0) {+ e->alpha = 32, e->state = 0, e->t = (int16_t)(40 + rnd(30));+ if (m->ai == AI_IMP) shot4(e, 3.5f, false);+ else enemy_shot_at(e->x, e->y - 10, 3.5f);+ }+ }+ break;+ case AI_VIS: /* walks; a blood beam when he is in line */+ if (e->state == 0) {+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.12f);+ if (e->t <= 0 && aligned(e, 8) && d < 200) e->state = 1, e->t = 20, e->c = (float)dir_to_player(e);+ } else {+ e->vx = e->vy = 0;+ if (e->t == 8) laser_fire(e->x, e->y - 10, (int)e->c, 7, 0, 14, 0, true);+ if (e->t <= 0) e->state = 0, e->t = 70;+ }+ break;+ case AI_EYE: /* stares; a beam (or shots) when he is in line */+ if (e->t <= 0 && aligned(e, 9)) {+ e->t = 70;+ int pd = dir_to_player(e);+ if (e->type == MON_EYE) laser_fire(e->x, e->y - 10, pd, 5, 0, 12, 0, true);+ else enemy_shot(e->x, e->y - 8, DIR_X[pd] * 4, DIR_Y[pd] * 4, 0), enemy_shot(e->x, e->y - 8, DIR_X[pd] * 3, DIR_Y[pd] * 3, 0);+ }+ break;+ case AI_SUCKER: case AI_SWARMER: /* flies at him */+ e->a += (rndf() - 0.5f) * 0.6f;+ walk(e, dx + fcos(e->a) * 0.5f, dy + fsin(e->a) * 0.5f, s, 0.08f);+ break;+ case AI_BOIL: /* grows, then spits a fountain */+ if (e->t <= 0 && d < 170) {+ e->t = 80;+ for (int k = 0; k < 4; k++) {+ Tear *t = enemy_shot(e->x, e->y - 8, dx * (2 + rndf()) + (rndf() - 0.5f), dy * (2 + rndf()) + (rndf() - 0.5f), 0);+ if (t) t->vz = -2.5f, t->flags |= TF_ARC, t->range = 1e9f;+ }+ }+ break;+ case AI_SPITTY: { /* walks at him, spits when in line */+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.12f);+ if (e->t <= 0 && aligned(e, 8) && d < 200) {+ int pd = dir_to_player(e);+ enemy_shot(e->x, e->y - 6, DIR_X[pd] * 4, DIR_Y[pd] * 4, 0);+ e->t = 45;+ }+ break;+ }+ case AI_BRAIN: /* rolls and bounces, leaving creep */+ if (!e->vx && !e->vy) e->vx = s * (rnd(2) ? 1 : -1), e->vy = s * 0.7f * (rnd(2) ? 1 : -1);+ {+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, false, false);+ if (e->vx == 0) e->vx = -ox;+ if (e->vy == 0) e->vy = -oy;+ }+ if ((e->t & 7) == 0) creep_spawn(e->x, e->y, 6, 80, true, 0);+ if (e->type == MON_MEMBRAIN && e->t <= 0) e->t = 60, enemy_shots_ring(e->x, e->y - 8, 6, 3, 0);+ break;+ case AI_NEST: /* spiders come out of it */+ if (e->type == MON_BABY_LONG_LEGS) walk(e, dx, dy, d > 70 ? s : 0, 0.1f);+ if (e->t <= 0) {+ e->t = 100;+ int n = 0;+ for (int i = 0; i < MAX_ENTS; i++) n += ents[i].kind == EK_MON && ents[i].type == MON_SPIDER;+ if (n < 3) mon_spawn(MON_SPIDER, e->x, e->y + 6);+ }+ break;+ case AI_DEATHS_HEAD: /* a skull that bounces about for good (it dies with the room's last monster) */+ if (!e->vx && !e->vy) e->vx = s, e->vy = s;+ {+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, true, false);+ if (e->vx == 0) e->vx = -ox;+ if (e->vy == 0) e->vy = -oy;+ }+ if (!ents_alive_monsters()) ent_kill(e), e->kind = EK_NONE;+ break;+ case AI_MOMS_HAND: /* Mom's hand: a shadow on him, then it comes down; it takes him away */+ if (e->state == 0) {+ e->flags |= EF_HIDDEN | EF_NOHIT;+ if (e->t <= 0) e->state = 1, e->t = 40;+ } else if (e->state == 1) {+ e->x += (pl.x - e->x) * 0.15f, e->y += (pl.y - e->y) * 0.15f;+ if (e->t <= 0) e->state = 2, e->t = 10, e->z = -80;+ } else if (e->state == 2) {+ e->flags &= (uint16_t)~(EF_HIDDEN | EF_NOHIT);+ e->z += 8;+ if (e->z >= 0) {+ e->z = 0, e->state = 3, e->t = 30;+ if (dist2(e->x, e->y, pl.x, pl.y) < 15 * 15 && pl.invuln == 0) {+ player_hurt(1, MON_MOMS_HAND);+ e->state = 4, e->t = 20; /* got him */+ }+ }+ } else if (e->state == 3) {+ if (e->t <= 0) e->state = 0, e->t = 60, e->z = -80;+ } else {+ pl.x = e->x, pl.y = e->y, e->z -= 4;+ if (e->t <= 0) room_teleport(fl.start), e->kind = EK_NONE;+ }+ break;+ case AI_GRIMACE: /* a stone face in a wall: shoots along its line, can't be hurt */+ if (e->t <= 0 && aligned(e, 10)) {+ e->t = 40;+ int pd = dir_to_player(e);+ enemy_shot(e->x, e->y - 6, DIR_X[pd] * 4, DIR_Y[pd] * 4, 0);+ }+ break;+ case AI_BAT: /* flits about, then swoops at him */+ e->a += (rndf() - 0.5f) * 0.6f;+ walk(e, fcos(e->a) * 0.8f + dx * 0.5f, fsin(e->a) * 0.8f + dy * 0.5f, s, 0.1f);+ if (e->type == MON_FAT_BAT && e->t <= 0) e->t = 70, enemy_shot_at(e->x, e->y - 8, 3.5f);+ break;+ case AI_TUMOR: /* floats; spreads shots */+ walk(e, fcos(e->a), fsin(e->a), s, 0.05f);+ e->a += 0.02f;+ if (e->t <= 0 && d < 170) e->t = 70, enemy_shots_ring(e->x, e->y - 8, 5, 3, fatan2(dy, dx));+ break;+ case AI_POKY: /* spikes that slide back and forth for good */+ if (!e->vx && !e->vy) {+ if (e->state & 1) e->vy = e->state & 2 ? -s : s;+ else e->vx = e->state & 2 ? -s : s;+ e->state ^= 2;+ }+ {+ float ox = e->vx, oy = e->vy;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, false, false);+ if (ox && !e->vx) e->vx = 0, e->vy = 0;+ if (oy && !e->vy) e->vx = 0, e->vy = 0;+ }+ if (!ents_alive_monsters()) e->kind = EK_NONE;+ break;+ case AI_BURROW: /* under the floor; comes up near him to shoot, then down again */+ if (e->state == 0) {+ e->flags |= EF_HIDDEN | EF_NOHIT | EF_INVULN;+ if (e->t <= 0) {+ for (int k = 0; k < 20; k++) {+ float a = rndf() * 6.2831853f, r = 40 + rnd(40);+ float x = pl.x + fcos(a) * r, y = pl.y + fsin(a) * r;+ if (!solid_at(x, y, false)) {+ e->x = x, e->y = y;+ break;+ }+ }+ e->state = 1, e->t = 50;+ e->flags &= (uint16_t)~(EF_HIDDEN | EF_NOHIT | EF_INVULN);+ }+ } else {+ if (e->t == 30) {+ if (e->type == MON_ROUND_WORM) enemy_shot_at(e->x, e->y - 8, 4);+ else if (e->type == MON_LUMP) enemy_shots_ring(e->x, e->y - 8, 6, 3, 0);+ else if (e->type == MON_PARA_BITE || e->type == MON_NIGHT_CRAWLER) {+ int pd = dir_to_player(e);+ e->vx = DIR_X[pd] * 2, e->vy = DIR_Y[pd] * 2;+ } else+ enemy_shot_at(e->x, e->y - 8, 3.5f), enemy_shot_at(e->x + 3, e->y - 8, 3);+ }+ e->vx *= 0.9f, e->vy *= 0.9f;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, false, false);+ if (e->t <= 0) e->state = 0, e->t = (int16_t)(30 + rnd(40));+ }+ break;+ case AI_LEECH: /* races straight at him along a line */+ if (e->state == 0 || e->t <= 0) {+ e->c = (float)dir_to_player(e), e->state = 1, e->t = 40;+ }+ {+ int dd = (int)e->c;+ walk(e, DIR_X[dd], DIR_Y[dd], s, 0.2f);+ if (e->vx == 0 && e->vy == 0) e->state = 0;+ }+ if (e->type == MON_KAMIKAZE_LEECH && d < e->r + 8) explode(e->x, e->y, 30, 20, true, 0), e->kind = EK_NONE;+ break;+ case AI_TETHER: { /* on a cord from where it began: lunges at him */+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.12f);+ float ox = e->x - e->c, oy = e->y - e->b, od = fsqrt(ox * ox + oy * oy);+ if (od > 60) e->x = e->c + ox / od * 60, e->y = e->b + oy / od * 60;+ if (e->type == MON_MR_MAW && e->t <= 0 && d < 100) e->t = 60, enemy_shot_at(e->x, e->y - 8, 3.5f);+ break;+ }+ case AI_WALL: /* along the walls, shooting across the room */+ if (e->t <= 0) e->t = (int16_t)(40 + rnd(40)), e->c = (float)rnd(4);+ {+ int dd = (int)e->c;+ walk(e, DIR_X[dd], DIR_Y[dd], s, 0.2f);+ }+ if (e->type == MON_WALL_CREEP && (e->t % 30) == 0 && aligned(e, 12)) {+ int pd = dir_to_player(e);+ enemy_shot(e->x, e->y - 6, DIR_X[pd] * 4, DIR_Y[pd] * 4, 0);+ }+ break;+ case AI_GAPING: /* breathes him in */+ if (d < 140) pl.vx -= dx * 0.12f, pl.vy -= dy * 0.12f;+ break;+ case AI_FATTY: { /* slow; a fat sack lets flies out, a pale one sprays */+ float px, py;+ path_dir(e->x, e->y, pl.x, pl.y, false, &px, &py);+ walk(e, px, py, s, 0.1f);+ if (e->type == MON_PALE_FATTY && e->t <= 0 && d < 100) e->t = 80, enemy_shots_ring(e->x, e->y - 10, 6, 2.5f, 0);+ if (e->type == MON_FAT_SACK && e->t <= 0) e->t = 120, mon_spawn(MON_ATTACK_FLY, e->x, e->y - 10);+ break;+ }+ case AI_KEEPER: /* hops at him and spits */+ if (e->t <= 0) {+ e->t = 45;+ e->vx = dx * 3, e->vy = dy * 3;+ enemy_shot_at(e->x, e->y - 10, 3.5f);+ }+ e->vx *= 0.88f, e->vy *= 0.88f;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, e->r, false, false);+ break;+ default: break;+ }+}++bool mon_on_death(Ent *e) {+ switch (e->type) {+ case MON_MULLIGAN: case MON_HIVE:+ for (int i = 0; i < 3 + rnd(3); i++) mon_spawn(MON_FLY, e->x + rnd(10) - 5, e->y + rnd(10) - 5);+ break;+ case MON_MOTER:+ for (int i = 0; i < 2; i++) mon_spawn(MON_ATTACK_FLY, e->x + i * 6 - 3, e->y);+ break;+ case MON_BOOM_FLY:+ explode(e->x, e->y, 30, 15, true, 0);+ break;+ case MON_RED_BOOM_FLY:+ enemy_shots_ring(e->x, e->y - 8, 6, 3.5f, 0);+ break;+ case MON_SUCKER:+ shot4(e, 3.5f, true);+ break;+ case MON_SPIT:+ creep_spawn(e->x, e->y, 12, 150, true, 1);+ break;+ case MON_BIG_SPIDER:+ for (int i = 0; i < 2; i++) mon_spawn(MON_SPIDER, e->x + i * 8 - 4, e->y);+ break;+ case MON_SQUIRT:+ for (int i = 0; i < 2; i++) mon_spawn(MON_DIP, e->x + i * 8 - 4, e->y);+ break;+ case MON_CONJOINED_FATTY:+ mon_spawn(MON_FATTY, e->x, e->y);+ break;+ case MON_SWARMER:+ mon_spawn(MON_ATTACK_FLY, e->x, e->y);+ break;+ case MON_GLOBIN: case MON_RAGLING:+ if (e->state == 0) { /* down, not dead: unless hurt again soon */+ e->state = 1, e->t = 120, e->hp = e->maxhp * 0.3f, e->flags &= (uint16_t)~EF_NOHIT;+ decal_blood(e->x, e->y, 6);+ return false;+ }+ break;+ case MON_FAT_SACK:+ for (int i = 0; i < 2; i++) mon_spawn(MON_ATTACK_FLY, e->x, e->y);+ break;+ default: break;+ }+ return true;+}++/* ---------------------------------------------------------------- drawing */+void mon_draw(Ent *e) {+ if (e->type >= MON_MONSTRO) {+ boss_draw(e);+ return;+ }+ const MonInfo *m = &mons[e->type];+ int x = (int)e->x, y = (int)(e->y + e->z), flags = 0;+ if (e->flags & EF_HIDDEN) {+ if (m->ai == AI_MOMS_HAND && e->state == 1) /* its shadow, coming */+ gfx_ellipse(x, (int)e->y, 12, 5, C_BLACK, 16);+ else if (m->ai == AI_BURROW)+ gfx_ellipse(x, (int)e->y, 5, 2, rgb565(0x3A2010), 16);+ return;+ }+ if (e->vx < -0.1f) flags = GFX_FLIPX;+ /* life: bobbing, squashing, wings */+ int sx = 256, sy = 256, lift = 0;+ if (e->flags & EF_FLYING) lift = 8 + (int)((g_frame + e->x) / 3) % 3;+ switch (m->ai) {+ case AI_CHASE: case AI_SKINNY: case AI_BONY: case AI_SPITTY: case AI_WANDER: case AI_GUSHER: case AI_FATTY:+ case AI_TETHER: case AI_WALL: case AI_KNIGHT: case AI_CHARGER: case AI_VIS:+ if (absf(e->vx) + absf(e->vy) > 0.2f) {+ int k = (int)((g_frame + (int)e->c) / 3) % 4;+ sy = 256 + (k == 1 ? 14 : k == 3 ? -14 : 0), sx = 512 - sy;+ }+ break;+ case AI_HORF: if (e->t < 10) x += (g_frame & 1) ? 1 : -1; break;+ case AI_HOST: if (e->state == 0) sy = 170, sx = 256; break;+ case AI_CLOTTY: case AI_DIP: case AI_GLOBIN: {+ int k = (int)(g_frame / 4 + e->x) % 6;+ sy = 256 + (k < 3 ? k : 6 - k) * 10 - 15, sx = 512 - sy;+ if (m->ai == AI_GLOBIN && e->state == 1) sy = 90, sx = 380;+ break;+ }+ case AI_BOIL: sx = sy = 200 + (80 - e->t > 0 ? (80 - e->t) : 0); if (sx > 300) sx = sy = 300; break;+ case AI_LEAPER: case AI_HOPPER: if (e->state == 0 && e->t < 6) sy = 220, sx = 290; break;+ default: break;+ }+ uint16_t tint = 0;+ int amount = 0;+ if (e->flash) tint = C_WHITE, amount = 20;+ else if (e->freeze) tint = rgb565(0xC0C0C0), amount = 24;+ else if (e->poison) tint = rgb565(0x40C040), amount = 12;+ else if (e->flags & EF_CHAMPION) {+ static const uint32_t champ[4] = {0xFF2020, 0xFFE020, 0x30C030, 0x202020};+ tint = rgb565(champ[e->sub & 3]), amount = 14;+ }+ if (e->flags & EF_FRIEND && e->type != MON_BLUE_FLY) tint = rgb565(0xFF80C0), amount = 14;+ if (e->type == MON_BLUE_FLY) tint = rgb565(0x4080FF), amount = 22;+ gfx_sprite_scaled(e->spr, 0, x, y + 3 - lift, sx, sy, flags, tint, amount, e->alpha);+}diff --git a/games/isaac/src/pickups.c b/games/isaac/src/pickups.cnew file mode 100644index 00000000..4128f8a2--- /dev/null+++ b/games/isaac/src/pickups.c@@ -0,0 +1,536 @@+/* What lies about a room: pickups and chests, items on their pedestals,+ * shops, machines and beggars, bombs, effects, blood and creep. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++static const int16_t pk_sprite[PK_COUNT] = {+ [PK_HEART] = SPR_HEART_FULL, [PK_HALF_HEART] = SPR_HEART_HALF, [PK_SOUL] = SPR_HEART_SOUL, [PK_BLACK] = SPR_HEART_BLACK,+ [PK_ETERNAL] = SPR_HEART_ETERNAL, [PK_DOUBLE_HEART] = SPR_HEART_DOUBLE, [PK_HALF_SOUL] = SPR_HEART_HALFSOUL,+ [PK_PENNY] = SPR_PENNY, [PK_NICKEL] = SPR_NICKEL, [PK_DIME] = SPR_DIME, [PK_KEY] = SPR_KEY, [PK_GOLDEN_KEY] = SPR_GOLDEN_KEY,+ [PK_KEY_RING] = SPR_KEY_RING, [PK_BOMB] = SPR_BOMB_PICKUP, [PK_DOUBLE_BOMB] = SPR_DOUBLE_BOMB, [PK_TROLL_BOMB] = SPR_TROLL_BOMB,+ [PK_CHEST] = SPR_CHEST, [PK_LOCKED_CHEST] = SPR_LOCKED_CHEST, [PK_RED_CHEST] = SPR_RED_CHEST, [PK_PILL] = SPR_PILL,+ [PK_CARD] = SPR_CARD, [PK_BATTERY] = SPR_BATTERY, [PK_GRAB_BAG] = SPR_GRAB_BAG, [PK_SPIKED_CHEST] = SPR_SPIKED_CHEST,+ [PK_GOLDEN_BOMB] = SPR_GOLDEN_BOMB,+};++Ent *pickup_spawn(int type, int sub, float x, float y, bool pop) {+ if (type == PK_TROLL_BOMB) { /* a troll bomb is lit as it lands */+ bomb_place(x, y, 1);+ return NULL;+ }+ Ent *e = ent_new(EK_PICKUP, x, y);+ if (!e) return NULL;+ e->type = (uint8_t)type, e->r = 6;+ if (type == PK_PILL && !sub) sub = 1 + rnd(13);+ if (type == PK_CARD && !sub) sub = rnd(6) == 0 ? 23 + rnd(9) : 1 + rnd(22);+ e->sub = (uint8_t)sub;+ e->spr = pk_sprite[type];+ e->frame = type == PK_PILL ? sub - 1 : type == PK_CARD ? sub - 1 : 0;+ if (pop) {+ float a = rndf() * 6.2831853f, s = 1 + rndf() * 2;+ e->vx = fcos(a) * s, e->vy = fsin(a) * s, e->vz = -3, e->z = -2;+ }+ e->t = 10; /* (it can't be picked up the moment it appears) */+ return e;+}++/* a random pickup, as rooms and rocks drop them */+int pickup_random_type(void) {+ int r = rnd(100);+ if (r < 30) return rnd(10) == 0 ? PK_NICKEL : rnd(30) == 0 ? PK_DIME : PK_PENNY;+ if (r < 50) return rnd(4) == 0 ? PK_HALF_HEART : rnd(8) == 0 ? PK_SOUL : rnd(25) == 0 ? PK_BLACK : PK_HEART;+ if (r < 64) return rnd(15) == 0 ? PK_KEY_RING : rnd(40) == 0 ? PK_GOLDEN_KEY : PK_KEY;+ if (r < 78) return rnd(8) == 0 ? PK_DOUBLE_BOMB : rnd(30) == 0 ? PK_TROLL_BOMB : PK_BOMB;+ if (r < 86) return rnd(6) == 0 ? PK_LOCKED_CHEST : PK_CHEST;+ if (r < 93) return PK_PILL;+ if (r < 99) return PK_CARD;+ return PK_BATTERY;+}++void pickup_drop_random(float x, float y) { pickup_spawn(pickup_random_type(), 0, x, y, true); }++/* the room's reward when it is cleared: one time in two or so, better with luck */+void room_clear_reward(void) {+ Room *r = room_cur();+ if (r->kind == ROOM_BOSS || r->kind == ROOM_MINIBOSS) return;+ float luck = pl.luck;+ int roll = rnd(100) + (int)(luck * 4);+ if (roll < 45) return;+ /* where: the room's middle, or the nearest free cell */+ float x = 160, y = 120;+ for (int k = 0; k < 20 && solid_at(x, y, false); k++) x = 60 + rnd(200), y = 70 + rnd(100);+ pickup_drop_random(x, y);+}++static void collect_coins(int n) {+ int c = pl.coins + n;+ pl.coins = (uint8_t)(c > 99 ? 99 : c);+ if (pl.has[109]) player_stats();+}++/* opening a chest: what spills out */+static void open_chest(Ent *e) {+ int t = e->type;+ e->state = 1; /* (open, empty) */+ e->frame = 0;+ if (t == PK_SPIKED_CHEST) player_hurt(1, -6);+ if (t == PK_RED_CHEST) {+ int r = rnd(100);+ if (r < 10 && fl.stage > 1) pedestal_spawn(item_from_pool(POOL_DEVIL), e->x, e->y, 0, false);+ else if (r < 35) for (int i = 0; i < 2; i++) pickup_spawn(PK_SOUL, 0, e->x, e->y, true);+ else if (r < 50) pickup_spawn(PK_BLACK, 0, e->x, e->y, true);+ else if (r < 65) for (int i = 0; i < 2; i++) pickup_spawn(PK_PILL, 0, e->x, e->y, true);+ else if (r < 80) for (int i = 0; i < 3; i++) mon_spawn(MON_SPIDER, e->x, e->y);+ else for (int i = 0; i < 2; i++) pickup_spawn(PK_TROLL_BOMB, 0, e->x, e->y, true);+ e->kind = EK_NONE;+ return;+ }+ bool golden = t == PK_LOCKED_CHEST;+ if (golden && rnd(5) == 0) {+ pedestal_spawn(item_from_pool(POOL_TREASURE), e->x, e->y, 0, false);+ e->kind = EK_NONE;+ return;+ }+ int n = golden ? 2 + rnd(4) : 1 + rnd(3);+ for (int i = 0; i < n; i++) {+ int k = rnd(golden ? 8 : 6);+ static const uint8_t brown[6] = {PK_PENNY, PK_PENNY, PK_HEART, PK_KEY, PK_BOMB, PK_PILL};+ static const uint8_t gold[8] = {PK_PENNY, PK_NICKEL, PK_SOUL, PK_KEY, PK_BOMB, PK_PILL, PK_CARD, PK_PENNY};+ pickup_spawn(golden ? gold[k] : brown[k], 0, e->x, e->y, true);+ }+ if (rnd(golden ? 10 : 20) == 0) pickup_spawn(PK_CARD, 0, e->x, e->y, true);+ e->kind = EK_NONE;+}++static bool try_collect(Ent *e) {+ int t = e->type;+ if (e->flags & EF_SHOP) { /* for sale */+ if (pl.coins < e->price) return false;+ }+ int max = pl.containers * 2;+ switch (t) {+ case PK_HEART: case PK_HALF_HEART: case PK_DOUBLE_HEART:+ if (pl.red >= max) return false;+ player_heal(t == PK_HEART ? 2 : t == PK_HALF_HEART ? 1 : 4);+ break;+ case PK_SOUL: case PK_HALF_SOUL: case PK_BLACK:+ if (pl.containers * 2 + pl.soul >= 24) return false;+ player_add_soul(t == PK_HALF_SOUL ? 1 : 2, t == PK_BLACK);+ break;+ case PK_ETERNAL:+ if (pl.eternal) player_add_containers(1), player_heal(2), pl.eternal = 0;+ else pl.eternal = 1;+ break;+ case PK_PENNY: collect_coins(1); break;+ case PK_NICKEL: collect_coins(5); break;+ case PK_DIME: collect_coins(10); break;+ case PK_KEY: if (pl.keys < 99) pl.keys++; break;+ case PK_KEY_RING: pl.keys = (uint8_t)(pl.keys + 2 > 99 ? 99 : pl.keys + 2); break;+ case PK_GOLDEN_KEY: pl.keys = (uint8_t)(pl.keys + 1 > 99 ? 99 : pl.keys + 1), pl.has[175] |= 0; break;+ case PK_BOMB: if (pl.bombs < 99) pl.bombs++; break;+ case PK_DOUBLE_BOMB: pl.bombs = (uint8_t)(pl.bombs + 2 > 99 ? 99 : pl.bombs + 2); break;+ case PK_GOLDEN_BOMB: pl.bombs = (uint8_t)(pl.bombs + 1 > 99 ? 99 : pl.bombs + 1); break;+ case PK_BATTERY: {+ const ItemInfo *ii = item_info(pl.active);+ if (!ii || !ii->charge || ii->charge == 255 || pl.active_charge >= ii->charge) return false;+ pl.active_charge = ii->charge;+ break;+ }+ case PK_PILL: case PK_CARD: {+ if (pl.pocket_kind) { /* (the one he had drops where he stands) */+ Ent *o = pickup_spawn(pl.pocket_kind == 1 ? PK_PILL : PK_CARD, pl.pocket, pl.x, pl.y + 8, false);+ if (o) o->t = 40;+ }+ pl.pocket_kind = t == PK_PILL ? 1 : 2, pl.pocket = e->sub;+ hud_banner(t == PK_PILL ? pill_name(e->sub, pl.pill_known[e->sub] != 0) : card_name(e->sub), "");+ break;+ }+ case PK_GRAB_BAG:+ for (int i = 0; i < 2; i++) pickup_spawn(rnd(3) == 0 ? PK_KEY : rnd(2) ? PK_PENNY : PK_BOMB, 0, e->x, e->y, true);+ break;+ case PK_CHEST: case PK_SPIKED_CHEST: case PK_RED_CHEST:+ open_chest(e);+ return true;+ case PK_LOCKED_CHEST:+ if (!pl.keys && !pl.has[175]) return false;+ if (!pl.has[175]) pl.keys--;+ open_chest(e);+ return true;+ default: break;+ }+ if (e->flags & EF_SHOP) pl.coins = (uint8_t)(pl.coins - e->price), player_stats();+ fx_spawn(FX_SPARK, e->x, e->y - 6, 8);+ e->kind = EK_NONE;+ return true;+}++void pickup_update(Ent *e) {+ if (e->t > 0) e->t--;+ /* falling and bouncing */+ if (e->z < 0 || e->vz) {+ e->vz += 0.6f, e->z += e->vz;+ if (e->z >= 0) {+ e->z = 0;+ e->vz = e->vz > 1.5f ? -e->vz * 0.4f : 0;+ }+ }+ /* Magneto pulls coins, hearts and keys */+ if (pl.has[53] && e->type <= PK_GOLDEN_BOMB && !(e->flags & EF_SHOP) && e->type != PK_CHEST && e->type != PK_LOCKED_CHEST) {+ float dx = pl.x - e->x, dy = pl.y - e->y, d = fsqrt(dx * dx + dy * dy);+ if (d > 1 && d < 120) e->vx += dx / d * 0.25f, e->vy += dy / d * 0.25f;+ }+ e->vx *= 0.88f, e->vy *= 0.88f;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, 5, false, false);+ if (e->t > 0 || e->z < -4) return;+ float dx = pl.x - e->x, dy = pl.y - e->y;+ if (dx * dx + dy * dy < 11 * 11) {+ if (!try_collect(e)) {+ /* not taken: nudged aside (chests, hearts he doesn't need) */+ if (!(e->flags & EF_SHOP) && e->type != PK_LOCKED_CHEST) {+ float d = fsqrt(dx * dx + dy * dy);+ if (d > 0.5f) e->vx -= dx / d * 0.8f, e->vy -= dy / d * 0.8f;+ }+ }+ }+}++void pickup_draw(Ent *e) {+ int x = (int)e->x, y = (int)(e->y + e->z);+ if (e->kind == EK_BOMB) {+ int a = e->t < 15 && (e->t & 2) ? 20 : 0;+ int s = 256 + (e->t < 15 ? ((e->t & 3) * 12) : 0);+ gfx_sprite_scaled(e->spr, 0, x, y + 3, s, s, 0, C_RED, a, 32);+ return;+ }+ gfx_sprite(e->spr, e->frame, x, y + 4, 0, 32);+ if (e->flags & EF_SHOP) { /* its price under it */+ char s[8];+ int p = e->price, n = 0;+ if (p >= 10) s[n++] = (char)('0' + p / 10);+ s[n++] = (char)('0' + p % 10), s[n] = 0;+ gfx_text_c(FONT_UPHEAVAL, x, y + 8, s, C_WHITE, 1, true);+ }+}++/* ---------------------------------------------------------------- items on pedestals */+Ent *pedestal_spawn(int item, float x, float y, int price, bool devil) {+ Ent *e = ent_new(EK_PEDESTAL, x, y);+ if (!e) return NULL;+ e->t3 = (int16_t)item, e->price = (int16_t)price, e->r = 8;+ if (devil) e->flags |= EF_DEVIL;+ e->t = 15;+ return e;+}++/* what an item costs in the Devil's room: a red heart container, two for the best, or three soul hearts */+static int devil_price(int item) {+ const ItemInfo *ii = item_info(item);+ return ii && ii->quality >= 3 ? 2 : 1;+}++void pedestal_update(Ent *e) {+ if (e->t > 0) e->t--;+ if (!e->t3 || e->t > 0) return;+ float dx = pl.x - e->x, dy = pl.y - e->y;+ if (dx * dx + dy * dy > 11 * 11) return;+ if (e->price && !(e->flags & EF_DEVIL)) { /* the shop */+ if (pl.coins < e->price) return;+ pl.coins = (uint8_t)(pl.coins - e->price);+ }+ if (e->flags & EF_DEVIL) {+ int hp = devil_price(e->t3);+ if (pl.containers >= hp) player_add_containers(-hp);+ else if (pl.soul >= 6 && pl.containers == 0) pl.soul = (uint8_t)(pl.soul - 6);+ else if (pl.containers > 0) player_add_containers(-pl.containers);+ else return;+ if (!pl.containers && !pl.soul) player_kill(-7);+ }+ int item = e->t3;+ e->t3 = 0, e->price = 0;+ e->flags &= (uint16_t)~EF_DEVIL;+ /* an active item swaps with the one he holds */+ if (item_is_active(item) && pl.active) {+ e->t3 = pl.active, e->t = 30;+ }+ item_give(item);+ pl.hold_anim = 30, pl.hold_item = (int16_t)item;+}++void pedestal_draw(Ent *e) {+ int x = (int)e->x, y = (int)e->y;+ gfx_sprite(SPR_PEDESTAL, 0, x, y + 6, 0, 32);+ if (e->t3) {+ int bob = (int)(g_frame / 4 + e->x) % 8;+ bob = bob < 4 ? bob : 8 - bob;+ int spr = item_sprite(e->t3);+ if ((fl.curse & CURSE_BLIND) && !e->price && room_cur()->kind != ROOM_SHOP) spr = SPR_ITEM_1, gfx_text_c(FONT_UPHEAVAL, x, y - 22, "?", C_WHITE, 2, true);+ else gfx_sprite(spr, 0, x, y - 4 - bob, 0, 32);+ if (e->price) {+ char s[8];+ int n = 0, p = e->flags & EF_DEVIL ? devil_price(e->t3) : e->price;+ if (p >= 10) s[n++] = (char)('0' + p / 10);+ s[n++] = (char)('0' + p % 10), s[n] = 0;+ if (e->flags & EF_DEVIL) {+ for (int k = 0; k < p; k++) gfx_sprite(SPR_HUD_HEART, 0, x - p * 6 + k * 12, y + 8, 0, 32);+ } else+ gfx_text_c(FONT_UPHEAVAL, x, y + 9, s, C_WHITE, 1, true);+ }+ }+}++/* ---------------------------------------------------------------- machines and beggars */+static const int16_t ma_sprite[] = {SPR_SLOT_MACHINE, SPR_BLOOD_MACHINE, SPR_FORTUNE_MACHINE, SPR_BEGGAR, SPR_DEVIL_BEGGAR,+ SPR_KEY_MASTER, SPR_BOMB_BUM, SPR_SHOPKEEPER, SPR_SATAN, SPR_GABRIEL};++Ent *machine_spawn(int type, float x, float y) {+ Ent *e = ent_new(EK_MACHINE, x, y);+ if (!e) return NULL;+ e->type = (uint8_t)type, e->spr = ma_sprite[type], e->r = 12;+ return e;+}++static const char *const fortunes[] = {+ "YOU WILL FIND\nWHAT YOU SEEK", "BEWARE THE\nHUNGRY ONE", "FRIENDS ARE\nNEARBY", "A SECRET LIES\nWHERE ROOMS MEET",+ "YOUR LUCK\nWILL CHANGE", "DON'T EAT THE\nYELLOW SNOW", "MOM IS\nWATCHING", "THE FLY IS\nYOUR FRIEND",+};++static void machine_pay(Ent *e) {+ e->t2 = 12; /* (its little shake) */+ switch (e->type) {+ case MA_SLOT: { /* a coin: mostly nothing, or a pickup, a fly, sometimes a prize */+ if (!pl.coins) return;+ pl.coins--;+ int r = rnd(100) - (int)pl.luck * 2;+ if (r < 5) for (int i = 0; i < 2; i++) pickup_spawn(PK_PENNY, 0, e->x, e->y + 14, true);+ else if (r < 10) pickup_spawn(PK_HEART, 0, e->x, e->y + 14, true);+ else if (r < 15) pickup_spawn(PK_KEY, 0, e->x, e->y + 14, true);+ else if (r < 20) pickup_spawn(PK_BOMB, 0, e->x, e->y + 14, true);+ else if (r < 23) pickup_spawn(PK_PILL, 0, e->x, e->y + 14, true);+ else if (r < 27) mon_spawn(MON_FLY, e->x, e->y + 16);+ else if (r < 29) for (int i = 0; i < 6; i++) pickup_spawn(PK_PENNY, 0, e->x, e->y + 14, true);+ else if (r < 30 && rnd(3) == 0) pedestal_spawn(item_from_pool(POOL_TREASURE), e->x, e->y + 26, 0, false);+ break;+ }+ case MA_BLOOD: /* half a heart: coins */+ if (!player_hurt(1, -5)) return;+ pl.invuln = 10;+ for (int i = 0; i < 1 + rnd(2); i++) pickup_spawn(PK_PENNY, 0, e->x, e->y + 14, true);+ if (rnd(40) == 0) pickup_spawn(PK_NICKEL, 0, e->x, e->y + 14, true);+ break;+ case MA_FORTUNE: {+ if (!pl.coins) return;+ pl.coins--;+ int r = rnd(100);+ if (r < 4) pedestal_spawn(item_from_pool(POOL_TREASURE), e->x, e->y + 26, 0, false);+ else if (r < 12) pickup_spawn(PK_SOUL, 0, e->x, e->y + 14, true);+ else if (r < 22) pickup_spawn(PK_CARD, 0, e->x, e->y + 14, true);+ else hud_banner(fortunes[rnd(sizeof fortunes / sizeof fortunes[0])], "");+ break;+ }+ case MA_BEGGAR: /* a coin: a pickup some of the time, an item once he has had enough */+ if (!pl.coins) return;+ pl.coins--;+ e->sub++;+ if (e->sub >= 6 + rnd(6) && rnd(4) == 0) {+ pedestal_spawn(item_from_pool(POOL_SHOP), e->x, e->y + 26, 0, false);+ e->state = 255, e->kind = EK_NONE;+ fx_spawn(FX_POOF, e->x, e->y, 10);+ } else if (rnd(4) == 0)+ pickup_drop_random(e->x, e->y + 14);+ break;+ case MA_DEVIL_BEGGAR:+ if (!player_hurt(2, -5)) return;+ if (rnd(3) == 0) pedestal_spawn(item_from_pool(POOL_DEVIL), e->x, e->y + 26, 0, false), e->state = 255, e->kind = EK_NONE;+ else pickup_spawn(PK_SOUL, 0, e->x, e->y + 14, true);+ break;+ case MA_BOMB_BUM:+ if (!pl.bombs) return;+ pl.bombs--;+ if (rnd(3) == 0) pickup_spawn(rnd(2) ? PK_NICKEL : PK_HEART, 0, e->x, e->y + 14, true);+ break;+ case MA_KEY_MASTER:+ if (!pl.keys) return;+ pl.keys--;+ pickup_spawn(rnd(3) ? PK_CHEST : PK_LOCKED_CHEST, 0, e->x, e->y + 16, true);+ break;+ default: break;+ }+}++void machine_update(Ent *e) {+ if (e->t2 > 0) e->t2--;+ if (e->t > 0) {+ e->t--;+ return;+ }+ if (e->type >= MA_SHOPKEEPER) return;+ float dx = pl.x - e->x, dy = pl.y - e->y;+ if (dx * dx + dy * dy < 15 * 15) {+ machine_pay(e);+ e->t = 15;+ /* he bumps off it */+ float d = fsqrt(dx * dx + dy * dy);+ if (d > 0.5f) pl.vx += dx / d * 2, pl.vy += dy / d * 2;+ }+}++void machine_draw(Ent *e) {+ int sh = e->t2 ? ((e->t2 & 2) ? 1 : -1) : 0;+ if (e->type == MA_DEVIL_STATUE || e->type == MA_ANGEL_STATUE) {+ gfx_sprite_tint(e->spr, 0, (int)e->x, (int)e->y, 0, rgb565(0x707070), 18, 32);+ return;+ }+ gfx_sprite(e->spr, 0, (int)e->x + sh, (int)e->y + 8, 0, 32);+}++/* ---------------------------------------------------------------- bombs */+void bomb_update(Ent *e) {+ /* pushed about by Isaac, slowing down */+ float dx = e->x - pl.x, dy = e->y - pl.y;+ if (dx * dx + dy * dy < 10 * 10 && e->type != 3) {+ float d = fsqrt(dx * dx + dy * dy);+ if (d > 0.5f) e->vx += dx / d * 0.6f, e->vy += dy / d * 0.6f;+ }+ if (e->type == 3) { /* Dr. Fetus: it flies, and goes off on what it hits */+ Ent *m = ent_nearest_monster(e->x, e->y, 14);+ if (m || e->t <= 0) e->t = 0;+ } else+ e->vx *= 0.85f, e->vy *= 0.85f;+ move_ent(&e->x, &e->y, &e->vx, &e->vy, 5, false, false);+ if (--e->t <= 0) {+ bool mega = e->spr == SPR_BIG_BOMB;+ explode(e->x, e->y, mega ? 52 : 36, e->hp > 0 ? e->hp : 60, e->type != 3, e->spr == SPR_POISON_BOMB ? 2 : 0);+ e->kind = EK_NONE;+ }+}++/* ---------------------------------------------------------------- effects, blood, creep */+void fx_spawn(int type, float x, float y, int life) {+ Ent *e = ent_new(EK_FX, x, y);+ if (!e) return;+ e->type = (uint8_t)type, e->t = (int16_t)life, e->t2 = (int16_t)life;+ if (type == FX_GIB) {+ float a = rndf() * 6.2831853f, s = 1 + rndf() * 2.5f;+ e->vx = fcos(a) * s, e->vy = fsin(a) * s * 0.6f, e->vz = -2 - rndf() * 3, e->z = -6, e->a = 1 + rndf() * 2;+ }+}++Ent *fx_text(float x, float y, const char *s) {+ Ent *e = ent_new(EK_FX, x, y);+ if (!e) return NULL;+ e->type = FX_TEXT, e->t = e->t2 = 40;+ e->spr = 0;+ /* (the text: 1UP, the only one) */+ (void)s;+ return e;+}++void fx_update(Ent *e) {+ if (e->type == FX_SPARK + 100) return; /* (Mom's Knife) */+ if (e->type == FX_GIB) {+ e->x += e->vx, e->y += e->vy, e->vz += 0.5f, e->z += e->vz;+ if (e->z > 0) {+ e->z = 0, e->vx = e->vy = 0;+ if (e->vz > 0) decal_blood(e->x, e->y, 1.5f + e->a), e->vz = 0;+ }+ }+ if (e->type == FX_TEXT) e->y -= 0.5f;+ if (--e->t <= 0) e->kind = EK_NONE;+}++void fx_draw(Ent *e) {+ int x = (int)e->x, y = (int)e->y;+ int k = e->t2 - e->t; /* frames since it began */+ switch (e->type) {+ case FX_POOF: {+ int s = 120 + k * 22;+ gfx_sprite_scaled(SPR_SMOKE, 0, x, y + 6, s, s, 0, 0, 0, 24 - k * 2 > 0 ? 24 - k * 2 : 0);+ break;+ }+ case FX_SPLASH:+ gfx_ellipse(x, y, 2 + k, 1 + k / 2, rgb565(0xA0C8F0), 20 - k * 3);+ break;+ case FX_SPARK:+ gfx_ellipse(x, y - k, 3 + k, 3 + k, C_WHITE, 20 - k * 2);+ break;+ case FX_GIB:+ gfx_ellipse(x, (int)(y + e->z), (int)e->a + 1, (int)e->a, rgb565(0x9A1010), 32);+ break;+ case FX_EXPLOSION: {+ float r = e->a * (0.5f + k * 0.12f);+ if (r > e->a * 1.2f) r = e->a * 1.2f;+ int a = 32 - k * 2;+ gfx_ellipse(x, y - 8, (int)r, (int)(r * 0.8f), rgb565(0x302020), a / 2);+ gfx_ellipse(x, y - 8, (int)(r * 0.8f), (int)(r * 0.65f), rgb565(0xF07020), a);+ gfx_ellipse(x, y - 10, (int)(r * 0.5f), (int)(r * 0.4f), rgb565(0xFFE070), a);+ break;+ }+ case FX_TEXT:+ gfx_text_c(FONT_UPHEAVAL, x, y, "1UP!", C_WHITE, 1, true);+ break;+ case FX_SPARK + 100: /* Mom's Knife */+ gfx_sprite(SPR_KNIFE, 0, x, y, 0, 32);+ break;+ case FX_SHOCKWAVE:+ gfx_ellipse(x, y, k * 4, k * 2, rgb565(0x504030), 20 - k);+ break;+ case FX_LIGHT:+ gfx_rect(x - 10, 0, 20, y, C_WHITE, e->t < 8 ? e->t * 3 : 24);+ break;+ default: break;+ }+}++void decal_blood(float x, float y, float size) {+ /* (a room keeps the last few) */+ int n = 0, oldest = -1;+ for (int i = 0; i < MAX_ENTS; i++)+ if (ents[i].kind == EK_DECAL) {+ if (oldest < 0) oldest = i;+ n++;+ }+ if (n >= 14 && oldest >= 0) ents[oldest].kind = EK_NONE;+ Ent *e = ent_new(EK_DECAL, x, y);+ if (e) e->a = size;+}++Ent *creep_spawn(float x, float y, float r, int life, bool enemy, int color) {+ Ent *e = ent_new(EK_CREEP, x, y);+ if (!e) return NULL;+ e->r = r, e->t = (int16_t)life, e->type = (uint8_t)color, e->state = enemy;+ return e;+}++void creep_update(Ent *e) {+ if (--e->t <= 0) {+ e->kind = EK_NONE;+ return;+ }+ if (e->state) { /* a monster's: it hurts Isaac's feet */+ float dx = pl.x - e->x, dy = (pl.y - e->y) / 0.6f;+ if (!pl.flight && dx * dx + dy * dy < e->r * e->r) player_hurt(1, -12);+ } else if ((e->t & 3) == 0) { /* Isaac's (Lemon Mishap): it hurts theirs */+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *m = &ents[i];+ if (m->kind != EK_MON || (m->flags & EF_FLYING)) continue;+ float dx = m->x - e->x, dy = (m->y - e->y) / 0.6f;+ if (dx * dx + dy * dy < e->r * e->r) ent_damage(m, 3, 0, 0, 0);+ }+ }+}++/* the way down: to the next floor */+void trapdoor_update(Ent *e) {+ if (e->t < 30) {+ e->t++;+ return;+ }+ float dx = pl.x - e->x, dy = pl.y - e->y;+ if (dx * dx + dy * dy < 8 * 8) {+ if (e->type == 1) fl.alt = 1; /* the beam: the Cathedral */+ else if (fl.stage == 8) fl.alt = 0;+ game_next_floor();+ }+}diff --git a/games/isaac/src/plat.h b/games/isaac/src/plat.hnew file mode 100644index 00000000..0255a21a--- /dev/null+++ b/games/isaac/src/plat.h@@ -0,0 +1,47 @@+/* The Binding of Isaac: the platform (the calculator, or a computer for tests).+ *+ * plat_eadk.c: the calculator's EADK display, keyboard, time and files.+ * plat_host.c: a frame buffer that tests save as images, scripted keys and+ * simulated time (tests/play.c). */+#ifndef IS_PLAT_H+#define IS_PLAT_H+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>++#define SCREEN_W 320+#define SCREEN_H 240++/* keys held now, as bits. The arrows walk, 8 4 6 2 (and 5) cry tears the+ * way they point, like the game's arrow keys; 9 drops a bomb, 7 or OK uses+ * the active item, 1 or 3 the pill or card, the toolbox key opens the map. */+enum {+ K_LEFT = 1 << 0, K_RIGHT = 1 << 1, K_UP = 1 << 2, K_DOWN = 1 << 3,+ K_SLEFT = 1 << 4, K_SRIGHT = 1 << 5, K_SUP = 1 << 6, K_SDOWN = 1 << 7,+ K_OK = 1 << 8, /* OK, EXE: confirm in menus; the active item in a room */+ K_BACK = 1 << 9, /* Back: pause, cancel */+ K_BOMB = 1 << 10, /* 9, shift */+ K_ITEM = 1 << 11, /* 7 (OK too, in a room) */+ K_CARD = 1 << 12, /* 1, 3: the pill or card */+ K_MAP = 1 << 13, /* toolbox, var: the whole map */+ K_HOME = 1 << 14, /* Home, On/Off: keep the run and quit */+ K_ANY = 1 << 15+};+#define K_MOVE (K_LEFT | K_RIGHT | K_UP | K_DOWN)+#define K_SHOOT (K_SLEFT | K_SRIGHT | K_SUP | K_SDOWN)++uint32_t plat_keys(void);+uint32_t plat_millis(void);+void plat_sleep(uint32_t ms);+void plat_push(int x, int y, int w, int h, const uint16_t *px);+bool plat_save(const char *name, const void *data, uint32_t len);+const uint8_t *plat_load(const char *name, uint32_t *len); /* NULL if absent */+/* a file of len bytes to write in place (the calculator's storage is RAM): plat_reserve gives its+ * bytes, plat_reserved says they are written; NULL if there is no room */+uint8_t *plat_reserve(const char *name, uint32_t len);+void plat_reserved(const char *name, uint32_t len);+bool plat_remove(const char *name);+void plat_begin(void);+int plat_end(void);+bool plat_slow(void); /* an N0110 or N0115 (216 MHz, the N0120 has 550) */+#endifdiff --git a/games/isaac/src/plat_eadk.c b/games/isaac/src/plat_eadk.cnew file mode 100644index 00000000..a9960eed--- /dev/null+++ b/games/isaac/src/plat_eadk.c@@ -0,0 +1,46 @@+/* The calculator: EADK display, keyboard, time and files. */+#include <eadk.h>+#include "../../common/epsilon_app.h"+#include "../../common/epsilon_files.h"+#include "plat.h"++#define KEY(k) ((uint64_t)1 << (k))++uint32_t plat_keys(void) {+ uint64_t k = eadk_keyboard_scan();+ uint32_t r = 0;+ if (k & KEY(eadk_key_left)) r |= K_LEFT;+ if (k & KEY(eadk_key_right)) r |= K_RIGHT;+ if (k & KEY(eadk_key_up)) r |= K_UP;+ if (k & KEY(eadk_key_down)) r |= K_DOWN;+ if (k & KEY(eadk_key_four)) r |= K_SLEFT;+ if (k & KEY(eadk_key_six)) r |= K_SRIGHT;+ if (k & KEY(eadk_key_eight)) r |= K_SUP;+ if (k & (KEY(eadk_key_two) | KEY(eadk_key_five))) r |= K_SDOWN;+ if (k & (KEY(eadk_key_ok) | KEY(eadk_key_exe))) r |= K_OK;+ if (k & (KEY(eadk_key_back) | KEY(eadk_key_backspace))) r |= K_BACK;+ if (k & (KEY(eadk_key_nine) | KEY(eadk_key_shift) | KEY(eadk_key_multiplication))) r |= K_BOMB;+ if (k & (KEY(eadk_key_seven) | KEY(eadk_key_alpha))) r |= K_ITEM;+ if (k & (KEY(eadk_key_one) | KEY(eadk_key_three) | KEY(eadk_key_minus))) r |= K_CARD;+ if (k & (KEY(eadk_key_toolbox) | KEY(eadk_key_var) | KEY(eadk_key_xnt))) r |= K_MAP;+ if (k & (KEY(eadk_key_home) | KEY(eadk_key_on_off))) r |= K_HOME;+ if (k) r |= K_ANY;+ return r;+}++uint32_t plat_millis(void) { return (uint32_t)eadk_timing_millis(); }+void plat_sleep(uint32_t ms) { eadk_timing_msleep(ms); }+void plat_push(int x, int y, int w, int h, const uint16_t *px) {+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, px);+}+bool plat_save(const char *name, const void *data, uint32_t len) { return ef_write(name, data, len); }+const uint8_t *plat_load(const char *name, uint32_t *len) { return ef_read(name, len); }+uint8_t *plat_reserve(const char *name, uint32_t len) {+ uint32_t n;+ return ef_write(name, NULL, len) ? (uint8_t *)(uintptr_t)ef_read(name, &n) : NULL; /* (the storage is RAM) */+}+void plat_reserved(const char *name, uint32_t len) { (void)name, (void)len; }+bool plat_remove(const char *name) { return ef_remove(name); }+void plat_begin(void) { np_app_begin(); }+int plat_end(void) { return np_app_end(); }+bool plat_slow(void) { return epsilon_slow_model(); }diff --git a/games/isaac/src/plat_host.c b/games/isaac/src/plat_host.cnew file mode 100644index 00000000..91a01972--- /dev/null+++ b/games/isaac/src/plat_host.c@@ -0,0 +1,70 @@+/* A computer: the screen goes to a frame buffer that tests save as images,+ * keys come from a script, time is simulated. */+#include <stdio.h>+#include <stdlib.h>+#include "plat.h"++uint16_t host_fb[SCREEN_W * SCREEN_H];+uint32_t host_time; /* ms */+uint32_t host_keys;++uint32_t plat_keys(void) { return host_keys; }+uint32_t plat_millis(void) { return host_time; }+void plat_sleep(uint32_t ms) { host_time += ms; }+void plat_push(int x, int y, int w, int h, const uint16_t *px) {+ for (int r = 0; r < h; r++)+ for (int c = 0; c < w; c++)+ if ((unsigned)(x + c) < SCREEN_W && (unsigned)(y + r) < SCREEN_H) host_fb[(y + r) * SCREEN_W + x + c] = px[r * w + c];+}++static char save_dir[256] = "build/host-saves";+void host_save_dir(const char *d) { snprintf(save_dir, sizeof save_dir, "%s", d); }+bool plat_save(const char *name, const void *data, uint32_t len) {+ char p[512];+ snprintf(p, sizeof p, "%s/%s", save_dir, name);+ FILE *f = fopen(p, "wb");+ if (!f) return false;+ bool ok = fwrite(data, 1, len, f) == len;+ fclose(f);+ return ok;+}+/* (each file read gets its own buffer: callers may hold two at once) */+static uint8_t loaded[4][1 << 16];+static int next_load;+const uint8_t *plat_load(const char *name, uint32_t *len) {+ char p[512];+ snprintf(p, sizeof p, "%s/%s", save_dir, name);+ FILE *f = fopen(p, "rb");+ if (!f) return NULL;+ uint8_t *b = loaded[next_load++ & 3];+ *len = (uint32_t)fread(b, 1, sizeof loaded[0], f);+ fclose(f);+ return b;+}+static uint8_t reserved[1 << 16];+uint8_t *plat_reserve(const char *name, uint32_t len) {+ (void)name;+ return len <= sizeof reserved ? reserved : NULL;+}+void plat_reserved(const char *name, uint32_t len) { plat_save(name, reserved, len); }+bool plat_remove(const char *name) {+ char p[512];+ snprintf(p, sizeof p, "%s/%s", save_dir, name);+ return remove(p) == 0;+}+void plat_begin(void) {}+int plat_end(void) { return 0; }+bool plat_slow(void) { return false; }++/* writes the screen as a PPM image */+void host_shot(const char *path) {+ FILE *f = fopen(path, "wb");+ if (!f) return;+ fprintf(f, "P6\n%d %d\n255\n", SCREEN_W, SCREEN_H);+ for (int i = 0; i < SCREEN_W * SCREEN_H; i++) {+ uint16_t c = host_fb[i];+ uint8_t rgb[3] = {(uint8_t)((c >> 11) * 255 / 31), (uint8_t)(((c >> 5) & 63) * 255 / 63), (uint8_t)((c & 31) * 255 / 31)};+ fwrite(rgb, 1, 3, f);+ }+ fclose(f);+}diff --git a/games/isaac/src/player.c b/games/isaac/src/player.cnew file mode 100644index 00000000..0dc474e5--- /dev/null+++ b/games/isaac/src/player.c@@ -0,0 +1,429 @@+/* Isaac: walking, crying, his hearts, and his stats as Rebirth works them out.+ *+ * Damage: 3.5 x sqrt(1 + 1.2 x the damage ups) plus what is added flat, then+ * the items that multiply it. Tears: a delay between tears of 16 - 6 x+ * sqrt(1.3 x the tears ups + 1) frames (10 to begin with, 5 at most), which+ * some items stretch. Range 23.75, shot speed 1 (a tear goes 10 of the game's+ * units a frame) and speed 1 (2 at most). Damage takes soul and black hearts+ * before red ones; a black heart lost hurts every monster in the room. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++Player pl;+const int8_t DIR_X[4] = {0, -1, 0, 1}, DIR_Y[4] = {1, 0, -1, 0};++#define BASE_SPEED 3.2f /* pixels a frame at speed 1 */++void player_reset(void) {+ memset(&pl, 0, sizeof pl);+ pl.containers = 3, pl.red = 6;+ pl.bombs = 1;+ pl.face = DIR_DOWN, pl.shoot_dir = DIR_NONE;+ pl.x = 160, pl.y = 120;+ player_stats();+}++static float tear_delay_of(float t) {+ float d = t >= 0 ? 16 - 6 * fsqrt(1.3f * t + 1) : 16 - 6 * fsqrt(1.3f * t + 1 > 0 ? 1.3f * t + 1 : 0) - 6 * t;+ return d < 5 ? 5 : d;+}++/* every item's stats, from scratch (as the game's cache) */+void player_stats(void) {+ const uint8_t *h = pl.has;+ float dmg_up = 0, flat = 0, mult = 1, tears = 0, range = 23.75f, shot = 1, speed = 1, luck = 0;+ uint32_t tf = 0;+ /* damage ups */+ dmg_up += h[7] * 1.0f + h[51] * 1.0f + h[50] * 1.0f + h[79] * 1.0f + h[70] * 1.0f + h[90] * 1.0f + h[193] * 0.3f+ + h[138] * 0.3f + h[80] * 0.5f + h[4] * 0.5f + h[12] * 0.3f + h[121] * 1.0f + h[197] * 0.5f + h[201] * 0.3f+ + h[165] * 0.3f + h[101] * 0.3f + h[189] * 0.3f + h[182] * 1.0f + h[122] * 0 + h[154] * 0;+ if (h[4]) mult *= 1.5f;+ if (h[12]) mult *= 1.5f;+ if (h[169]) mult *= 2, flat += 4;+ if (h[182]) mult *= 2.3f;+ if (h[152]) mult *= 0.65f;+ if (h[261]) mult *= 2;+ if (h[120]) mult *= 0.9f;+ if (h[2] || h[153]) mult *= 1; /* (multi-shot keeps the damage) */+ if (h[122] && pl.red <= 1 && pl.soul == 0) dmg_up += 1.5f, speed += 0.3f; /* Whore of Babylon at half a heart */+ if (h[109]) flat += pl.coins * 0.04f; /* Money = Power */+ if (h[157]) flat += pl.lust * 0.5f > 3 ? 3 : pl.lust * 0.5f; /* Bloody Lust */+ /* tears */+ tears += h[1] * 0.7f + h[32] * 0.7f + h[6] * 1.5f + h[72] * 0.5f + h[80] * 0.7f + h[183] * 0.7f + h[120] * 1.7f+ + h[189] * 0.2f + h[101] * 0.2f + h[173] * 0 + h[12] * 0 - h[182] * 0.4f;+ /* range (Rebirth's units, 23.75 to begin with) */+ range += h[29] * 5.25f + h[30] * 5.25f + h[31] * 5.25f + h[14] * 5.25f + h[71] * 2.5f + h[12] * 5.25f + h[121] * 5.25f+ + h[101] * 0.25f + h[189] * 0.25f + h[197] * 0.25f - h[6] * 17.62f + h[5] * 5.25f - h[261] * 10;+ /* shot speed */+ shot += h[143] * 0.2f + h[165] * 0.23f + h[176] * 0.16f + h[183] * 0.16f + h[194] * 0.16f - h[169] * 0 - h[2] * 0;+ /* speed */+ speed += h[13] * 0.2f + h[14] * 0.6f + h[27] * 0.3f + h[28] * 0.3f + h[70] * 0.4f + h[71] * 0.3f + h[12] * 0.3f+ + h[79] * 0.2f + h[82] * 0.3f + h[101] * 0.3f + h[189] * 0.2f + h[143] * 0.3f + h[119] * 0.3f + h[120] * 0.3f+ - h[121] * 0.1f - h[90] * 0.2f - h[129] * 0.2f;+ if (pl.unicorn) speed += 0.28f;+ luck += h[46] * 1.0f + h[189] * 1.0f + h[101] * 0;+ tears += pl.pill_tears, range += pl.pill_range, speed += pl.pill_speed, luck += pl.pill_luck;+ /* the damage */+ float d = 3.5f * fsqrt(1 + 1.2f * dmg_up) + flat;+ if (pl.belial) d += 2;+ d += pl.room_damage;+ d *= mult;+ if (h[245]) d *= 1; /* 20/20: two tears at full damage (Rebirth) */+ pl.damage = d < 0.5f ? 0.5f : d;+ pl.tears_ups = tears, pl.damage_ups = dmg_up;+ float delay = tear_delay_of(tears);+ if (h[2]) delay = delay * 2.1f + 3; /* The Inner Eye */+ if (h[153]) delay = delay * 2.1f + 3; /* Mutant Spider */+ if (h[169]) delay = delay * 2.1f + 3; /* Polyphemus */+ if (h[52]) delay = delay * 2.5f; /* Dr. Fetus */+ if (h[149]) delay = delay * 1.33f + 10; /* Ipecac */+ if (h[118]) delay = delay * 3; /* Brimstone: charging */+ if (h[69]) delay = delay * 2.5f; /* Chocolate Milk */+ if (h[152]) delay = 2; /* Technology 2: a continuous laser (shots of it) */+ if (h[6]) delay = tear_delay_of(tears) < 5 ? 5 : delay;+ pl.tear_delay = delay;+ if (range < 6) range = 6;+ pl.range = range;+ if (shot < 0.6f) shot = 0.6f;+ pl.shot_speed = shot;+ if (speed > 2) speed = 2;+ if (speed < 0.1f) speed = 0.1f;+ pl.speed = speed;+ pl.luck = luck;+ /* what tears do */+ if (h[3] || pl.telepathy || h[182]) tf |= TF_HOMING;+ if (h[48]) tf |= TF_PIERCE;+ if (h[115] || h[185] || h[159]) tf |= TF_SPECTRAL;+ if (h[103]) tf |= TF_POISON;+ if (h[89]) tf |= TF_SLOW;+ if (h[110]) tf |= TF_FREEZE;+ if (h[104]) tf |= TF_SPLIT;+ if (h[149]) tf |= TF_EXPLODE | TF_ARC;+ if (h[5]) tf |= TF_BOOMERANG;+ if (h[150]) tf |= TF_TOOTH;+ if (h[132]) tf |= TF_COAL;+ if (h[201]) tf |= TF_CONFUSE;+ if (h[261]) tf |= TF_PROPTOSIS;+ if (h[169] || h[261]) tf |= TF_BIG;+ if (h[7] || h[51] || h[79] || h[80] || h[118] || h[157] || h[193] || h[186] || pl.belial || pl.damage >= 9) tf |= TF_BLOOD;+ pl.tear_flags = tf;+ pl.shots = h[153] ? 4 : h[2] ? 3 : h[245] ? 2 : 1;+ pl.flight = h[20] || h[82] || h[159] || h[179] || h[184] || h[185] || pl.bible || pl.pony;+}++int player_hearts_total(void) { return pl.red + pl.soul + pl.eternal; }++void player_heal(int halves) {+ int max = pl.containers * 2;+ pl.red = (uint8_t)(pl.red + halves > max ? max : pl.red + halves);+}++void player_add_soul(int halves, bool black) {+ int room = 24 - pl.containers * 2 - pl.soul; /* twelve hearts in all */+ if (room <= 0) return;+ if (halves > room) halves = room;+ for (int i = 0; i < halves; i++) {+ if (black) pl.black |= (uint16_t)(1 << (pl.soul / 2));+ pl.soul++;+ }+}++void player_add_containers(int n) {+ int c = pl.containers + n;+ if (c > 12) c = 12;+ if (c < 0) c = 0;+ pl.containers = (uint8_t)c;+ if (pl.containers * 2 + pl.soul > 24) pl.soul = (uint8_t)(24 - pl.containers * 2);+ if (pl.red > pl.containers * 2) pl.red = (uint8_t)(pl.containers * 2);+}++void player_kill(int source) {+ g_killer = source;+ if (pl.lives) { /* 1up!, Dead Cat: back up, at the room's start */+ pl.lives--;+ if (pl.has[81] && !pl.has[11]) {+ pl.containers = 1, pl.red = 2, pl.soul = 0, pl.black = 0;+ } else {+ pl.red = (uint8_t)(pl.containers * 2);+ if (!pl.containers) pl.soul = 6;+ }+ pl.invuln = 60;+ fx_text(pl.x, pl.y - 30, "1UP");+ return;+ }+ pl.dead = true;+ pl.hurt_anim = 50;+}++bool player_hurt(int halves, int source) {+ if (pl.invuln > 0 || pl.dead || pl.shadows > 0 || pl.unicorn > 0) return false;+ if (pl.has[108]) halves = 1; /* The Wafer */+ g_shake = 6;+ pl.hurt_anim = 12;+ pl.invuln = pl.has[162] ? 45 : 30;+ if (pl.has[156] && pl.active) { /* Habit: a hit charges the active item */+ const ItemInfo *ii = item_info(pl.active);+ if (ii && pl.active_charge < ii->charge) pl.active_charge++;+ }+ if (pl.has[157]) pl.lust++, player_stats();+ for (int i = 0; i < halves; i++) {+ if (pl.eternal) {+ pl.eternal = 0;+ continue;+ }+ if (pl.soul) {+ int idx = (pl.soul - 1) / 2;+ bool black = pl.black & (1 << idx);+ pl.soul--;+ if (pl.soul % 2 == 0 && black) { /* a whole black heart: the Necronomicon's blast */+ pl.black &= (uint16_t)~(1 << idx);+ for (int k = 0; k < MAX_ENTS; k++)+ if (ents[k].kind == EK_MON && !(ents[k].flags & EF_FRIEND)) ent_damage(&ents[k], 40, 0, 0, 0);+ }+ continue;+ }+ if (pl.red) {+ pl.red--;+ pl.damage_taken_floor = 1;+ }+ }+ if (pl.has[142] && pl.red + pl.soul <= 1) player_add_soul(2, false), pl.has[142] = 2; /* Scapular */+ if (pl.red == 0 && pl.soul == 0 && pl.eternal == 0) player_kill(source);+ if (pl.has[122]) player_stats();+ return true;+}++/* ---------------------------------------------------------------- crying */+static void cry(int dir) {+ float sp = pl.shot_speed * 5;+ float vx = DIR_X[dir] * sp, vy = DIR_Y[dir] * sp;+ /* (some of his own movement goes into the tear, as in the game) */+ vx += pl.vx * 0.25f, vy += pl.vy * 0.25f;+ float hx = pl.x, hy = pl.y;+ uint32_t flags = pl.tear_flags;+ float dmg = pl.damage;+ /* Chocolate Milk: as charged */+ if (pl.has[69]) {+ float k = pl.charge / (pl.tear_delay * 1.0f);+ if (k > 1) k = 1;+ dmg *= 0.25f + 1.75f * k;+ }+ if (pl.has[52]) { /* Dr. Fetus: bombs instead */+ Ent *b = ent_new(EK_BOMB, hx + DIR_X[dir] * 8, hy + DIR_Y[dir] * 6);+ if (b) b->vx = vx * 0.9f, b->vy = vy * 0.9f, b->t = 30, b->type = 3, b->hp = dmg * 5 + 30, b->spr = SPR_BOMB;+ return;+ }+ if (pl.has[68] || pl.has[152]) { /* Technology: a laser */+ laser_fire(hx, hy - 10, dir, 3, pl.has[152] ? dmg * 0.35f : dmg, pl.has[152] ? 2 : 3, 1, false);+ if (!pl.has[152]) return;+ }+ int n = pl.shots;+ /* Loki's Horns and Mom's Eye: by luck, more ways */+ bool loki = pl.has[87] && rnd(100) < 25 + (int)(pl.luck * 5);+ bool back = pl.has[55] && rnd(100) < 50 + (int)(pl.luck * 10);+ for (int k = 0; k < n; k++) {+ float ox = 0, oy = 0, ax = 0, ay = 0;+ if (n > 1) {+ float s = k - (n - 1) / 2.0f;+ if (pl.has[245] && n == 2) { /* 20/20: side by side */+ ox = DIR_Y[dir] * s * 6, oy = DIR_X[dir] * s * 6;+ } else { /* a fan */+ ax = DIR_Y[dir] * s * 0.9f * (pl.has[153] ? 0.6f : 1), ay = -DIR_X[dir] * s * 0.9f * (pl.has[153] ? 0.6f : 1);+ ax = -ax, ay = -ay;+ }+ } else {+ ox = DIR_Y[dir] * (pl.tear_eye ? 3 : -3), oy = DIR_X[dir] * (pl.tear_eye ? 3 : -3);+ }+ Tear *t = tear_fire(hx + ox, hy + oy, vx + ax, vy + ay, dmg, flags);+ (void)t;+ }+ if (loki)+ for (int d = 0; d < 4; d++)+ if (d != dir) tear_fire(hx, hy, DIR_X[d] * sp, DIR_Y[d] * sp, dmg, flags);+ if (back && !loki) tear_fire(hx, hy, -DIR_X[dir] * sp, -DIR_Y[dir] * sp, dmg, flags);+ pl.tear_eye ^= 1;+}++/* Mom's Knife: held in front of him; charged, it flies out and comes back */+static Ent *knife;+static void knife_update(int dir, bool held) {+ if (!knife || knife->kind != EK_FX || knife->type != FX_SPARK + 100) {+ knife = ent_new(EK_FX, pl.x, pl.y);+ if (!knife) return;+ knife->type = FX_SPARK + 100, knife->t = 32767, knife->spr = SPR_KNIFE, knife->state = 0;+ }+ Ent *k = knife;+ k->t = 32767;+ if (k->state == 0) { /* held: charging while he cries */+ if (dir != DIR_NONE) k->a = (float)dir;+ int d = (int)k->a;+ if (held) pl.charge = pl.charge < 40 ? pl.charge + 1 : 40;+ else if (pl.charge > 4) {+ k->state = 1, k->b = 8 + pl.charge * 0.35f, k->c = 0, k->hp = pl.charge;+ pl.charge = 0;+ } else+ pl.charge = 0;+ k->x = pl.x + DIR_X[d] * 12, k->y = pl.y + DIR_Y[d] * 10 - 6;+ } else { /* thrown: out, then back */+ int d = (int)k->a;+ k->c += k->b;+ k->b -= 1.1f;+ if (k->c <= 0 && k->b < 0) k->state = 0, k->c = 0;+ k->x = pl.x + DIR_X[d] * (12 + k->c), k->y = pl.y + DIR_Y[d] * (10 + k->c) - 6;+ }+ /* it cuts whatever it touches, every other frame */+ if (g_frame & 1) {+ float dmg = pl.damage * (k->state ? 2 + k->hp / 10.0f : 2);+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || (e->flags & (EF_FRIEND | EF_HIDDEN))) continue;+ float dx = e->x - k->x, dy = e->y - (k->y + 6);+ if (dx * dx + dy * dy < (e->r + 7) * (e->r + 7)) ent_damage(e, dmg, 0, DIR_X[(int)k->a] * 2.0f, DIR_Y[(int)k->a] * 2.0f);+ }+ }+}++static int last_shoot_key = DIR_NONE;+static int shoot_dir_from(uint32_t keys) {+ static const uint32_t kk[4] = {K_SDOWN, K_SLEFT, K_SUP, K_SRIGHT};+ /* the key pressed last wins, as the game does with two at once */+ for (int d = 0; d < 4; d++)+ if ((g_pressed & kk[d])) last_shoot_key = d;+ if (last_shoot_key != DIR_NONE && (keys & kk[last_shoot_key])) return last_shoot_key;+ for (int d = 0; d < 4; d++)+ if (keys & kk[d]) return d;+ return DIR_NONE;+}++void player_update(uint32_t keys) {+ if (pl.dead) {+ if (pl.hurt_anim > 0) pl.hurt_anim--;+ return;+ }+ if (pl.invuln > 0) pl.invuln--;+ if (pl.hurt_anim > 0) pl.hurt_anim--;+ if (pl.hold_anim > 0) pl.hold_anim--;+ /* walking: towards the speed the arrows ask for, with the game's slide */+ float mx = 0, my = 0;+ if (keys & K_LEFT) mx -= 1;+ if (keys & K_RIGHT) mx += 1;+ if (keys & K_UP) my -= 1;+ if (keys & K_DOWN) my += 1;+ if (mx && my) mx *= 0.7071f, my *= 0.7071f;+ float max = BASE_SPEED * pl.speed;+ { /* cobwebs hold him back */+ int c = (int)((pl.x - ROOM_X) / TILE), r = (int)((pl.y - ROOM_Y) / TILE);+ if (c >= 0 && r >= 0 && c < COLS && r < ROWS && room_cur()->grid[r][c] == CELL_COBWEB && !pl.flight) max *= 0.3f;+ }+ float tx = mx * max, ty = my * max;+ float acc = (mx || my) ? 0.35f : 0.25f;+ pl.vx += (tx - pl.vx) * acc, pl.vy += (ty - pl.vy) * acc;+ if (absf(pl.vx) < 0.05f && !mx) pl.vx = 0;+ if (absf(pl.vy) < 0.05f && !my) pl.vy = 0;+ if (pl.pony > 0 && (mx || my)) pl.vx = mx * 7, pl.vy = my * 7; /* (A Pony's dash) */+ move_ent(&pl.x, &pl.y, &pl.vx, &pl.vy, 5, pl.flight, false);+ if (mx || my) {+ pl.walk++;+ if (absf(mx) > absf(my)) pl.face = mx < 0 ? DIR_LEFT : DIR_RIGHT;+ else pl.face = my < 0 ? DIR_UP : DIR_DOWN;+ } else+ pl.walk = 0;+ /* crying */+ int dir = shoot_dir_from(keys);+ pl.shoot_dir = (int8_t)dir;+ if (pl.fire_cd > 0) pl.fire_cd -= 1;+ if (pl.has[114]) {+ knife_update(dir, dir != DIR_NONE);+ } else if (pl.has[118]) { /* Brimstone: charge, then let go */+ static int bdir = DIR_DOWN;+ if (dir != DIR_NONE) {+ bdir = dir;+ if (pl.charge < pl.tear_delay) pl.charge++;+ } else if (pl.charge >= pl.tear_delay && pl.charge > 0) {+ laser_fire(pl.x, pl.y - 10, bdir, 9, pl.damage * 0.33f, 15, 0, false);+ pl.charge = 0;+ } else+ pl.charge = 0;+ } else if (pl.has[69]) { /* Chocolate Milk: hold to charge */+ static int cdir = DIR_DOWN;+ if (dir != DIR_NONE) {+ cdir = dir;+ if (pl.charge < pl.tear_delay) pl.charge++;+ } else if (pl.charge > 0) {+ cry(cdir);+ pl.charge = 0;+ }+ } else if (dir != DIR_NONE && pl.fire_cd <= 0) {+ cry(dir);+ pl.fire_cd += pl.tear_delay + 1;+ if (pl.fire_cd < 1) pl.fire_cd = 1;+ }+ if (dir == DIR_NONE && pl.fire_cd < 0) pl.fire_cd = 0;+ /* spikes (not flying) */+ if (!pl.flight && grid_spikes_at(pl.x, pl.y)) {+ if (room_cur()->kind == ROOM_SACRIFICE) {+ if (player_hurt(1, -2)) {+ Room *r = room_cur();+ r->variant++;+ /* the sacrifice room's rewards, as in Rebirth */+ int n = r->variant;+ if (n <= 2 && rnd(2) == 0) pickup_spawn(PK_PENNY, 0, 160, 140, true);+ else if (n == 3 || n == 4) pickup_spawn(PK_CHEST, 0, 160, 145, true);+ else if (n == 6) pedestal_spawn(item_from_pool(POOL_ANGEL), 160, 150, 0, false);+ else if (n >= 8 && n <= 11 && rnd(3) == 0) pickup_spawn(PK_SOUL, 0, 160, 140, true);+ else if (n == 12) floor_enter_room(fl.boss_room, DIR_NONE);+ }+ } else+ player_hurt(1, -2);+ }+ if (pl.creep > 0) pl.creep--;+ if (pl.belial > 0) pl.belial--;+}++/* ---------------------------------------------------------------- drawing */+void player_draw_shadow(void) {+ int hover = pl.flight ? 3 : 0;+ gfx_ellipse((int)pl.x, (int)pl.y, 7 - hover / 2, 3, C_BLACK, 12);+}++void player_draw(void) {+ int x = (int)pl.x, y = (int)pl.y;+ if (pl.flight) y -= 4 + (int)((g_frame / 6) % 2);+ bool flash = pl.invuln > 0 && (pl.invuln / 3) % 2 == 0;+ int alpha = 32;+ uint16_t tint = C_RED;+ int amount = flash ? 14 : 0;+ if (pl.dead) { /* fallen over, crying */+ gfx_sprite_tint(SPR_ISAAC_HEAD, 1, x, y + 2, 0, tint, 0, alpha);+ return;+ }+ if (pl.hold_anim > 0) { /* holding up what he just found */+ gfx_sprite(SPR_ISAAC_BODY, 8, x, y, 0, alpha);+ gfx_sprite(SPR_ISAAC_HEAD, 0, x, y - 7, 0, alpha);+ if (pl.hold_item > 0) gfx_sprite(item_sprite(pl.hold_item), 0, x, y - 22, 0, 32);+ return;+ }+ int body, flags = 0;+ int step = (pl.walk / 4) % 4;+ int f = pl.face;+ if (f == DIR_LEFT || f == DIR_RIGHT) body = 4 + (pl.walk ? step : 0), flags = f == DIR_LEFT ? GFX_FLIPX : 0;+ else body = pl.walk ? step : 0;+ int bob = (pl.walk && (step & 1)) ? 1 : 0;+ gfx_sprite_tint(SPR_ISAAC_BODY, body, x, y, flags, tint, amount, alpha);+ /* the head looks the way he cries, or else the way he walks */+ int hd = pl.shoot_dir != DIR_NONE ? pl.shoot_dir : pl.face;+ bool crying = pl.shoot_dir != DIR_NONE && (pl.fire_cd > pl.tear_delay * 0.5f || pl.has[118] || pl.has[69]);+ int head = hd == DIR_UP ? 4 : (hd == DIR_LEFT || hd == DIR_RIGHT) ? 2 : 0;+ if (crying) head++;+ int hflags = hd == DIR_LEFT ? GFX_FLIPX : 0;+ gfx_sprite_tint(SPR_ISAAC_HEAD, head, x, y - 7 + bob, hflags, tint, amount, alpha);+ /* Brimstone and Chocolate Milk charge in a bar over his head */+ if ((pl.has[118] || pl.has[69] || pl.has[114]) && pl.charge > 0) {+ float max = pl.has[114] ? 40 : pl.tear_delay;+ int w = (int)(14 * pl.charge / max);+ gfx_rect(x - 8, y - 34, 16, 4, C_BLACK, 24);+ gfx_rect(x - 7, y - 33, w, 2, pl.charge >= max ? rgb565(0xFFE060) : rgb565(0xE04040), 32);+ }+}diff --git a/games/isaac/src/res.c b/games/isaac/src/res.cnew file mode 100644index 00000000..f14b4c3f--- /dev/null+++ b/games/isaac/src/res.c@@ -0,0 +1,71 @@+/* The data.bin sections (tools/pack.py says what each holds). */+#include "res.h"++const uint8_t *is_bin;++static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }+static const uint8_t *section(int s) { return is_bin + rd32(is_bin + 8 + 4 * s); }+/* entry i of a section that is a table: its count, then each entry's offset from the section's start */+static const uint8_t *entry(int s, uint32_t i) {+ const uint8_t *t = section(s);+ if (i >= rd32(t)) return 0;+ return t + rd32(t + 4 + 4 * i);+}++int sprite_frames(int spr) {+ const uint8_t *t = section(SEC_SPRITES);+ if ((uint32_t)spr >= rd32(t)) return 0;+ const uint16_t *s = (const uint16_t *)(t + 4 + 4 * spr);+ return s[1];+}++bool frame_get(int spr, int i, Frame *f) {+ const uint8_t *t = section(SEC_SPRITES);+ if ((uint32_t)spr >= rd32(t)) return false;+ const uint16_t *s = (const uint16_t *)(t + 4 + 4 * spr);+ if (!s[1]) return false;+ if (i < 0 || i >= s[1]) i = ((i % s[1]) + s[1]) % s[1];+ const uint8_t *p = entry(SEC_FRAMES, s[0] + (uint32_t)i);+ if (!p) return false;+ const uint16_t *h = (const uint16_t *)p;+ f->w = h[0], f->h = h[1], f->ox = (int16_t)h[2], f->oy = (int16_t)h[3];+ int n = h[4];+ f->pal = h + 6;+ f->alpha = (const uint8_t *)(f->pal + n);+ f->rowoff = (const uint16_t *)(f->alpha + ((n + 1) & ~1));+ f->rows = (const uint8_t *)(f->rowoff + f->h);+ return true;+}++bool backdrop_get(int b, Backdrop *bd) {+ const uint8_t *p = entry(SEC_BACKDROPS, (uint32_t)b);+ if (!p) return false;+ const uint16_t *h = (const uint16_t *)p;+ bd->w = h[0], bd->h = h[1];+ bd->pal = h + 4;+ bd->px = (const uint8_t *)(bd->pal + h[2]);+ return true;+}++int font_height(int font) {+ const uint8_t *p = entry(SEC_FONTS, (uint32_t)font);+ return p ? p[0] : 0;+}++bool glyph_get(int font, int c, Glyph *g) {+ const uint8_t *p = entry(SEC_FONTS, (uint32_t)font);+ if (!p) return false;+ int first = p[1], count = p[2];+ if (c >= 'a' && c <= 'z' && p[3]) c -= 32; /* (a font of capitals only) */+ if (c < first || c >= first + count) return false;+ const uint8_t *e = p + 4 + 8 * (c - first);+ g->adv = e[0], g->w = e[1], g->h = e[2], g->ox = (int8_t)e[3], g->oy = (int8_t)e[4];+ g->bits = p + 4 + 8 * count + (e[6] | e[7] << 8);+ return true;+}++int room_count(void) { return (int)rd32(section(SEC_ROOMS)); }+const RoomTemplate *room_get(int i) {+ if (i < 0 || i >= room_count()) return 0;+ return (const RoomTemplate *)(section(SEC_ROOMS) + 4 + sizeof(RoomTemplate) * (uint32_t)i);+}diff --git a/games/isaac/src/res.h b/games/isaac/src/res.hnew file mode 100644index 00000000..075af832--- /dev/null+++ b/games/isaac/src/res.h@@ -0,0 +1,44 @@+/* The Binding of Isaac: the pictures, fonts and rooms in data.bin (tools/pack.py). */+#ifndef IS_RES_H+#define IS_RES_H+#include <stdbool.h>+#include <stdint.h>+#include "data.h"++extern const uint8_t *is_bin; /* data.bin, wherever it lives */++/* a picture: palette indices in runs, row by row; each colour has its opacity (0 to 32) */+typedef struct {+ int w, h, ox, oy; /* size, and the point it is drawn at (its feet, mostly) */+ const uint16_t *pal;+ const uint8_t *alpha;+ const uint16_t *rowoff; /* each row's runs, from rows */+ const uint8_t *rows; /* a row: its number of runs, then (skip, length, length indices) each */+} Frame;+bool frame_get(int spr, int i, Frame *f); /* frame i of sprite spr (wrapping round its frames) */+int sprite_frames(int spr);++/* a room's walls and floor: a quarter of it (the rest is it mirrored), one byte a pixel */+typedef struct {+ int w, h;+ const uint16_t *pal;+ const uint8_t *px;+} Backdrop;+bool backdrop_get(int b, Backdrop *bd);++/* a glyph of a font: its bits, row by row, highest bit first, rows of whole bytes */+typedef struct {+ int adv, w, h, ox, oy;+ const uint8_t *bits;+} Glyph;+int font_height(int font);+bool glyph_get(int font, int c, Glyph *g);++/* a room's layout (tools/rooms.py): its kind, the floors it may be on, how often, and its 13 x 7 cells */+typedef struct {+ uint8_t kind, chapters, weight, flags;+ uint8_t cell[7][13];+} RoomTemplate;+int room_count(void);+const RoomTemplate *room_get(int i);+#endifdiff --git a/games/isaac/src/room.c b/games/isaac/src/room.cnew file mode 100644index 00000000..dfaa4231--- /dev/null+++ b/games/isaac/src/room.c@@ -0,0 +1,362 @@+/* A room's grid: rocks, poop, pits, spikes and fire; walking around them, blowing them up, and the doors. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++#define GRID_RUBBLE 100 /* (a rock blown up: rubble on the floor) */++static uint8_t g(int c, int r) {+ if (c < 0 || r < 0 || c >= COLS || r >= ROWS) return CELL_BLOCK;+ return room_cur()->grid[r][c];+}+static uint8_t ghp(int c, int r) { return room_cur()->gridhp[r][c]; }++static bool is_poop(int k) { return k == CELL_POOP || k == CELL_CORNY_POOP || k == CELL_RED_POOP || k == CELL_GOLDEN_POOP; }++bool grid_solid(int c, int r, bool flying) {+ int k = g(c, r);+ switch (k) {+ case CELL_ROCK: case CELL_BLOCK: case CELL_TINTED: case CELL_BOMB_ROCK: case CELL_TNT: case CELL_KEY_BLOCK:+ return !flying || k == CELL_BLOCK || k == CELL_KEY_BLOCK;+ case CELL_PIT: return !flying;+ case CELL_FIRE: case CELL_RED_FIRE: return ghp(c, r) > 0 && !flying;+ default: return is_poop(k) && ghp(c, r) > 0 && !flying;+ }+}++bool grid_blocks_tears(int c, int r) {+ int k = g(c, r);+ switch (k) {+ case CELL_ROCK: case CELL_BLOCK: case CELL_TINTED: case CELL_BOMB_ROCK: case CELL_TNT: case CELL_KEY_BLOCK: return true;+ case CELL_FIRE: case CELL_RED_FIRE: return ghp(c, r) > 0;+ default: return is_poop(k) && ghp(c, r) > 0;+ }+}++bool grid_pit(int c, int r) { return g(c, r) == CELL_PIT; }++static int col_of(float x) { return (int)((x - ROOM_X) / TILE + 100) - 100; }+static int row_of(float y) { return (int)((y - ROOM_Y) / TILE + 100) - 100; }++bool solid_at(float x, float y, bool flying) {+ if (x < ROOM_X || x >= ROOM_R || y < ROOM_Y || y >= ROOM_B) return true;+ return grid_solid(col_of(x), row_of(y), flying);+}++int grid_spikes_at(float x, float y) {+ int k = g(col_of(x), row_of(y));+ return k == CELL_SPIKES;+}++/* moves a body of radius r by its speed, sliding along the walls and the grid (as a square: simple and fair) */+void move_ent(float *x, float *y, float *vx, float *vy, float r, bool flying, bool nogrid) {+ float nx = *x + *vx;+ float lo = ROOM_X + r, hi = ROOM_R - r;+ if (nx < lo) nx = lo, *vx = 0;+ if (nx > hi) nx = hi, *vx = 0;+ if (!nogrid) {+ int r0 = row_of(*y - r + 0.5f), r1 = row_of(*y + r - 0.5f);+ if (*vx > 0) {+ int c = col_of(nx + r);+ for (int rr = r0; rr <= r1; rr++)+ if (grid_solid(c, rr, flying)) {+ nx = ROOM_X + c * TILE - r - 0.01f, *vx = 0;+ break;+ }+ } else if (*vx < 0) {+ int c = col_of(nx - r);+ for (int rr = r0; rr <= r1; rr++)+ if (grid_solid(c, rr, flying)) {+ nx = ROOM_X + (c + 1) * TILE + r + 0.01f, *vx = 0;+ break;+ }+ }+ }+ *x = nx;+ float ny = *y + *vy;+ lo = ROOM_Y + r, hi = ROOM_B - r;+ if (ny < lo) ny = lo, *vy = 0;+ if (ny > hi) ny = hi, *vy = 0;+ if (!nogrid) {+ int c0 = col_of(*x - r + 0.5f), c1 = col_of(*x + r - 0.5f);+ if (*vy > 0) {+ int rr = row_of(ny + r);+ for (int c = c0; c <= c1; c++)+ if (grid_solid(c, rr, flying)) {+ ny = ROOM_Y + rr * TILE - r - 0.01f, *vy = 0;+ break;+ }+ } else if (*vy < 0) {+ int rr = row_of(ny - r);+ for (int c = c0; c <= c1; c++)+ if (grid_solid(c, rr, flying)) {+ ny = ROOM_Y + (rr + 1) * TILE + r + 0.01f, *vy = 0;+ break;+ }+ }+ }+ *y = ny;+}++bool grid_line_clear(float x0, float y0, float x1, float y1) {+ float dx = x1 - x0, dy = y1 - y0, d = fsqrt(dx * dx + dy * dy);+ int n = (int)(d / 6) + 1;+ for (int i = 1; i < n; i++) {+ float x = x0 + dx * i / n, y = y0 + dy * i / n;+ if (grid_blocks_tears(col_of(x), row_of(y)) || grid_pit(col_of(x), row_of(y))) return false;+ }+ return true;+}++/* ---------------------------------------------------------------- the way to the player (a flow field) */+static uint8_t flow[ROWS][COLS];+static int flow_tc = -1, flow_tr = -1;+static uint32_t flow_frame = 0xFFFFFFFF;++static void flow_build(int tc, int tr) {+ memset(flow, 255, sizeof flow);+ if (tc < 0 || tr < 0 || tc >= COLS || tr >= ROWS) return;+ static uint8_t qc[ROWS * COLS], qr[ROWS * COLS];+ int h = 0, t = 0;+ flow[tr][tc] = 0, qc[t] = (uint8_t)tc, qr[t++] = (uint8_t)tr;+ while (h < t) {+ int c = qc[h], r = qr[h++];+ for (int d = 0; d < 4; d++) {+ int nc = c + DIR_X[d], nr = r + DIR_Y[d];+ if (nc < 0 || nr < 0 || nc >= COLS || nr >= ROWS || flow[nr][nc] != 255 || grid_solid(nc, nr, false)) continue;+ flow[nr][nc] = (uint8_t)(flow[r][c] + 1);+ qc[t] = (uint8_t)nc, qr[t++] = (uint8_t)nr;+ }+ }+}++/* the way a walker goes from (x, y) to (tx, ty): straight when nothing is in the way, else along the floor's+ * shortest path; returns 0 when there is no way */+int path_dir(float x, float y, float tx, float ty, bool flying, float *dx, float *dy) {+ float ex = tx - x, ey = ty - y, d = fsqrt(ex * ex + ey * ey);+ if (d < 0.01f) {+ *dx = *dy = 0;+ return 0;+ }+ if (flying || grid_line_clear(x, y, tx, ty)) {+ *dx = ex / d, *dy = ey / d;+ return 1;+ }+ int tc = col_of(tx), tr = row_of(ty);+ if (flow_frame != g_frame || tc != flow_tc || tr != flow_tr) {+ flow_build(tc, tr);+ flow_frame = g_frame, flow_tc = tc, flow_tr = tr;+ }+ int c = col_of(x), r = row_of(y), best = 255, bc = c, br = r;+ if (c < 0 || r < 0 || c >= COLS || r >= ROWS) {+ *dx = ex / d, *dy = ey / d;+ return 1;+ }+ for (int k = 0; k < 4; k++) {+ int nc = c + DIR_X[k], nr = r + DIR_Y[k];+ if (nc < 0 || nr < 0 || nc >= COLS || nr >= ROWS) continue;+ if (flow[nr][nc] < best) best = flow[nr][nc], bc = nc, br = nr;+ }+ if (best == 255) {+ *dx = ex / d, *dy = ey / d;+ return 0;+ }+ float gx = ROOM_X + bc * TILE + TILE / 2 - x, gy = ROOM_Y + br * TILE + TILE / 2 - y, gd = fsqrt(gx * gx + gy * gy);+ if (gd < 0.01f) gd = 1;+ *dx = gx / gd, *dy = gy / gd;+ return 1;+}++/* ---------------------------------------------------------------- breaking things */+static void cell_center(int c, int r, float *x, float *y) { *x = ROOM_X + c * TILE + TILE / 2, *y = ROOM_Y + r * TILE + TILE / 2; }++static void destroy_cell(int c, int r) {+ Room *rm = room_cur();+ int k = rm->grid[r][c];+ float x, y;+ cell_center(c, r, &x, &y);+ switch (k) {+ case CELL_ROCK: case CELL_BOMB_ROCK:+ rm->grid[r][c] = GRID_RUBBLE;+ fx_spawn(FX_POOF, x, y, 10);+ if (k == CELL_ROCK && rnd(30) == 0) pickup_drop_random(x, y);+ break;+ case CELL_TINTED: {+ rm->grid[r][c] = GRID_RUBBLE;+ fx_spawn(FX_POOF, x, y, 10);+ /* Rebirth's: a soul heart, bombs, keys or a chest; The Small Rock now and then */+ int roll = rnd(100);+ if (roll < 40) pickup_spawn(PK_SOUL, 0, x, y, true);+ else if (roll < 60) pickup_spawn(PK_BOMB, 0, x, y, true), pickup_spawn(PK_BOMB, 0, x, y, true);+ else if (roll < 75) pickup_spawn(PK_KEY, 0, x, y, true);+ else if (roll < 90) pickup_spawn(PK_CHEST, 0, x, y, true);+ else if (!pl.has[90]) pedestal_spawn(90, x, y, 0, false);+ else pickup_spawn(PK_SOUL, 0, x, y, true);+ break;+ }+ case CELL_TNT:+ rm->grid[r][c] = GRID_RUBBLE;+ explode(x, y, 30, 10, true, 0);+ break;+ case CELL_KEY_BLOCK: case CELL_COBWEB:+ rm->grid[r][c] = CELL_EMPTY;+ break;+ default:+ if (is_poop(k) && rm->gridhp[r][c]) {+ rm->gridhp[r][c] = 0;+ fx_spawn(FX_POOF, x, y, 8);+ if (k == CELL_GOLDEN_POOP) for (int i = 0; i < 3; i++) pickup_spawn(PK_PENNY, 0, x, y, true);+ else if (rnd(10) == 0) pickup_drop_random(x, y);+ if (k == CELL_CORNY_POOP && rnd(3) == 0) mon_spawn(MON_FLY, x, y);+ }+ if ((k == CELL_FIRE || k == CELL_RED_FIRE) && rm->gridhp[r][c]) {+ rm->gridhp[r][c] = 0;+ fx_spawn(FX_POOF, x, y - 4, 8);+ if (rnd(4) == 0) pickup_drop_random(x, y);+ }+ break;+ }+}++void grid_hit(int c, int r, float dmg) {+ if (c < 0 || r < 0 || c >= COLS || r >= ROWS) return;+ Room *rm = room_cur();+ int k = rm->grid[r][c];+ if (is_poop(k) || k == CELL_FIRE || k == CELL_RED_FIRE) {+ if (!rm->gridhp[r][c]) return;+ /* a hit or more (big tears take more) */+ int hits = dmg >= 10 ? 2 : 1;+ if (rm->gridhp[r][c] <= hits) destroy_cell(c, r);+ else rm->gridhp[r][c] = (uint8_t)(rm->gridhp[r][c] - hits);+ } else if (k == CELL_TNT) {+ if (++rm->gridhp[r][c] >= 4) destroy_cell(c, r);+ }+}++void door_bombed(float x, float y);+void grid_explode(float x, float y, float radius) {+ for (int r = 0; r < ROWS; r++)+ for (int c = 0; c < COLS; c++) {+ float cx, cy;+ cell_center(c, r, &cx, &cy);+ float dx = cx - x, dy = cy - y;+ if (dx * dx + dy * dy > (radius + 10) * (radius + 10)) continue;+ int k = g(c, r);+ if (k == CELL_ROCK || k == CELL_TINTED || k == CELL_BOMB_ROCK || k == CELL_TNT || is_poop(k) || k == CELL_FIRE+ || k == CELL_RED_FIRE || k == CELL_COBWEB)+ destroy_cell(c, r);+ if (k == CELL_ROCK && pl.has[147]) destroy_cell(c, r);+ }+ door_bombed(x, y);+}++/* ---------------------------------------------------------------- drawing */+static int rock_sprite(void) {+ Room *r = room_cur();+ if (r->kind == ROOM_SECRET || r->kind == ROOM_SUPERSECRET) return SPR_ROCKS_SECRETROOM;+ switch (chapter_of(fl.stage)) {+ case 1: return fl.alt ? SPR_ROCKS_CELLAR : SPR_ROCKS_GENERIC;+ case 2: return fl.alt ? SPR_ROCKS_CATACOMBS : SPR_ROCKS_CAVES;+ case 3: return SPR_ROCKS_DEPTHS;+ case 4: return fl.alt ? SPR_ROCKS_SCARREDWOMB : SPR_ROCKS_WOMB;+ default: return fl.alt ? SPR_ROCKS_CATHEDRAL : SPR_ROCKS_SHEOL;+ }+}++/* the flat things (pits, spikes, rubble, webs), under everything */+void room_draw_floor(void) {+ gfx_backdrop(room_backdrop());+ Room *rm = room_cur();+ int rs = rock_sprite();+ for (int r = 0; r < ROWS; r++)+ for (int c = 0; c < COLS; c++) {+ int k = rm->grid[r][c];+ float x, y;+ cell_center(c, r, &x, &y);+ switch (k) {+ case CELL_PIT: gfx_sprite(SPR_PIT, 0, (int)x, (int)y, 0, 32); break;+ case CELL_SPIKES: gfx_sprite(SPR_SPIKES, 0, (int)x, (int)y, 0, 32); break;+ case CELL_COBWEB: gfx_sprite(SPR_COBWEB, 0, (int)x, (int)y, 0, 28); break;+ case GRID_RUBBLE: gfx_sprite(rs, 7, (int)x, (int)y, 0, 32); break;+ default: break;+ }+ }+}++/* the standing things, each drawn in its row (by ents_draw) */+void grid_draw_row(int r) {+ Room *rm = room_cur();+ int rs = rock_sprite();+ for (int c = 0; c < COLS; c++) {+ int k = rm->grid[r][c], hp = rm->gridhp[r][c];+ float x, y;+ cell_center(c, r, &x, &y);+ int ix = (int)x, iy = (int)y;+ switch (k) {+ case CELL_ROCK: gfx_sprite(rs, (c * 7 + r * 13) % 3, ix, iy - 2, 0, 32); break;+ case CELL_BLOCK: gfx_sprite(rs, 3, ix, iy - 2, 0, 32); break;+ case CELL_TINTED: gfx_sprite(rs, 4, ix, iy - 2, 0, 32); break;+ case CELL_BOMB_ROCK: gfx_sprite(rs, 5, ix, iy - 2, 0, 32); break;+ case CELL_TNT: gfx_sprite(SPR_TNT, 0, ix, iy + 9, 0, 32); break;+ case CELL_KEY_BLOCK: gfx_sprite(SPR_KEY_BLOCK, 0, ix, iy + 9, 0, 32); break;+ case CELL_FIRE: case CELL_RED_FIRE: {+ int spr = k == CELL_FIRE ? SPR_FIREPLACE : SPR_RED_FIREPLACE;+ if (hp) {+ int sy = 256 + ((int)((g_frame + c * 5) % 6) - 3) * 8 - (4 - hp) * 30;+ gfx_sprite_scaled(spr, 0, ix, iy + 9, 256, sy, 0, 0, 0, 32);+ } else+ gfx_sprite_scaled(spr, 0, ix, iy + 9, 256, 90, 0, C_BLACK, 20, 32);+ break;+ }+ default:+ if (is_poop(k) && hp) {+ int spr = k == CELL_POOP ? SPR_POOP : k == CELL_CORNY_POOP ? SPR_CORNY_POOP : k == CELL_RED_POOP ? SPR_RED_POOP : SPR_GOLDEN_POOP;+ if (spr == SPR_POOP) gfx_sprite(spr, 4 - hp, ix, iy + 9, 0, 32);+ else gfx_sprite_scaled(spr, 0, ix, iy + 9, 256, 256 * hp / 4, 0, 0, 0, 32);+ }+ break;+ }+ }+}++/* ---------------------------------------------------------------- doors */+static const int door_x[4] = {160, ROOM_X, 160, ROOM_R}, door_y[4] = {ROOM_B, 120, ROOM_Y, 120};++static int door_frame_kind(int kind, bool here_kind_special, int here) {+ (void)here_kind_special;+ int k = kind;+ if (here == ROOM_BOSS || here == ROOM_TREASURE || here == ROOM_SHOP || here == ROOM_CURSE || here == ROOM_SECRET+ || here == ROOM_SUPERSECRET || here == ROOM_ARCADE || here == ROOM_DEVIL || here == ROOM_ANGEL)+ if (kind == ROOM_NORMAL || kind == ROOM_START) k = here;+ switch (k) {+ case ROOM_TREASURE: return 1;+ case ROOM_BOSS: return 2;+ case ROOM_SECRET: case ROOM_SUPERSECRET: return 3;+ case ROOM_SHOP: case ROOM_LIBRARY: return 4;+ case ROOM_CURSE: return 5;+ case ROOM_DEVIL: return 6;+ case ROOM_ANGEL: return 7;+ case ROOM_ARCADE: return 8;+ default: return 0;+ }+}++void doors_draw(void) {+ int here = room_cur()->kind;+ for (int d = 0; d < 4; d++) {+ int kind;+ if (!room_door_exists(d, &kind)) continue;+ int fk = door_frame_kind(kind, false, here);+ bool open = room_door_open(d);+ int f = fk * 2 + (open ? 0 : 1);+ if (fk == 3) f = 6; /* (a hole stays a hole) */+ if (d == DIR_UP) gfx_sprite(SPR_DOOR, f, door_x[2], door_y[2], 0, 32);+ else if (d == DIR_DOWN) gfx_sprite(SPR_DOOR, f, door_x[0], door_y[0] + 30, GFX_FLIPY, 32);+ else {+ int spr = SPR_DOOR_SIDE_NORMAL + fk;+ if (d == DIR_LEFT) gfx_sprite(spr, open ? 0 : 1, door_x[1], door_y[1], 0, 32);+ else gfx_sprite(spr, open ? 0 : 1, door_x[3], door_y[3], GFX_FLIPX, 32);+ }+ }+}diff --git a/games/isaac/src/save.c b/games/isaac/src/save.cnew file mode 100644index 00000000..7fe10b63--- /dev/null+++ b/games/isaac/src/save.c@@ -0,0 +1,254 @@+/* Saves: the run in progress (isaacrun.sav, kept on Home and at each new+ * floor, gone when the run ends), the progress over all runs (isaac.sav),+ * and a copy of both in a Python script: installing apps from the NumWorks+ * website keeps only Python scripts, so the saves come back from it when the+ * files themselves are gone. Each is a comment line "#>name:base64" (the way+ * NumPlay and its big games keep theirs). */+#include <string.h>+#include "game.h"++#define RUN_NAME "isaacrun.sav"+#define PROGRESS_NAME "isaac.sav"+#define COPY_NAME "isaac_saves.py"+#define RUN_MAGIC 0x31525349u /* "ISR1" */+#define PROGRESS_MAGIC 0x31505349u /* "ISP1" */++extern uint8_t item_taken[ITEM_MAX];++static const char *const kept[] = {PROGRESS_NAME, RUN_NAME};+#define NKEPT (int)(sizeof kept / sizeof kept[0])+static const char copy_head[] =+ "# The Binding of Isaac keeps a copy of your run and progress\n"+ "# here, so that reinstalling it doesn't erase them. If you\n"+ "# delete this file, the game writes it again.\n";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";++static void copy_write(void) {+ uint32_t total = 1 + (sizeof copy_head - 1) + 1, len;+ int n = 0;+ for (int k = 0; k < NKEPT; k++)+ if (plat_load(kept[k], &len) && len) total += 2 + (uint32_t)strlen(kept[k]) + 1 + (len + 2) / 3 * 4 + 1, n++;+ plat_remove(COPY_NAME);+ if (!n) return;+ uint8_t *o = plat_reserve(COPY_NAME, total), *p = o;+ if (!o) return;+ *p++ = 0; /* (the script's status byte: not imported) */+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;+ for (int k = 0; k < NKEPT; k++) {+ const uint8_t *d = plat_load(kept[k], &len);+ if (!d || !len) continue;+ uint32_t nl = (uint32_t)strlen(kept[k]);+ *p++ = '#', *p++ = '>';+ memcpy(p, kept[k], nl), p += nl;+ *p++ = ':';+ for (uint32_t i = 0; i < len; i += 3, p += 4) {+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see NumPlay's+ * launcher/src/storage.c) */+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);+ p[0] = (uint8_t)b64[v >> 18 & 63];+ p[1] = (uint8_t)b64[v >> 12 & 63];+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';+ }+ *p++ = '\n';+ }+ *p++ = 0;+ plat_reserved(COPY_NAME, (uint32_t)(p - o));+}++static int b64_value(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}++static const uint8_t *copy_line(int k, uint32_t *len) {+ uint32_t n, nl = (uint32_t)strlen(kept[k]);+ const uint8_t *c = plat_load(COPY_NAME, &n);+ if (!c) return NULL;+ for (uint32_t i = 0; i + nl + 3 <= n; i++)+ if (c[i] == '#' && c[i + 1] == '>' && !memcmp(c + i + 2, kept[k], nl) && c[i + 2 + nl] == ':') {+ const uint8_t *l = c + i + 3 + nl;+ uint32_t m = 0;+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;+ *len = m;+ return l;+ }+ return NULL;+}++void save_restore_copy(void) {+ for (int k = 0; k < NKEPT; k++) {+ uint32_t have, len;+ if (plat_load(kept[k], &have)) continue;+ const uint8_t *l = copy_line(k, &len);+ if (!l || !len || len % 4) continue;+ uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');+ uint8_t *out = plat_reserve(kept[k], size);+ if (!out) continue;+ l = copy_line(k, &len);+ bool ok = l != NULL && len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=') == size;+ uint32_t o = 0;+ for (uint32_t i = 0; ok && i < len; i += 4) {+ int v0 = l[i] == '=' ? 0 : b64_value(l[i]), v1 = l[i + 1] == '=' ? 0 : b64_value(l[i + 1]);+ int v2 = l[i + 2] == '=' ? 0 : b64_value(l[i + 2]), v3 = l[i + 3] == '=' ? 0 : b64_value(l[i + 3]);+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) ok = false;+ uint32_t wd = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);+ if (o < size) out[o++] = (uint8_t)(wd >> 16);+ if (o < size) out[o++] = (uint8_t)(wd >> 8);+ if (o < size) out[o++] = (uint8_t)wd;+ }+ if (ok && o == size) plat_reserved(kept[k], size);+ else plat_remove(kept[k]);+ }+}++/* ---------------------------------------------------------------- the run */+static uint8_t buf[6144];+static uint32_t at;+static bool over;+static void put(const void *p, uint32_t n) {+ if (at + n > sizeof buf) {+ over = true;+ return;+ }+ memcpy(buf + at, p, n), at += n;+}+static void put8(int v) {+ uint8_t b = (uint8_t)v;+ put(&b, 1);+}+static const uint8_t *src;+static uint32_t src_len, rp;+static bool bad;+static void get(void *p, uint32_t n) {+ if (rp + n > src_len) {+ bad = true;+ memset(p, 0, n);+ return;+ }+ memcpy(p, src + rp, n), rp += n;+}+static int get8(void) {+ uint8_t b;+ get(&b, 1);+ return b;+}++bool save_run(void) {+ if (g_mode != MODE_PLAY && g_mode != MODE_PAUSE && g_mode != MODE_MAP && g_mode != MODE_STAGE && g_mode != MODE_CONFIRM) return false;+ if (pl.dead) return false;+ room_save_things();+ at = 0, over = false;+ uint32_t magic = RUN_MAGIC, size = sizeof(Player);+ put(&magic, 4), put(&size, 4);+ put(&g_run_seed, 4), put(&g_rng, 4), put(&g_frame, 4);+ put(&pl, sizeof pl);+ /* the items already seen this run, as bits */+ for (int i = 0; i < ITEM_MAX; i += 8) {+ int b = 0;+ for (int k = 0; k < 8 && i + k < ITEM_MAX; k++) b |= (item_taken[i + k] ? 1 : 0) << k;+ put8(b);+ }+ /* the floor, then each room: its grid in runs of (cell, state) */+ put8(fl.stage), put8(fl.alt), put8(fl.curse), put8(fl.nrooms), put(fl.map, sizeof fl.map);+ put8(fl.cur), put8(fl.start), put8(fl.boss_room), put8(fl.devil_room), put8(fl.boss);+ put8(fl.devil_open | fl.devil_angel << 1 | fl.boss_beaten << 2 | fl.trapdoor << 3);+ put(&fl.seed, 4);+ for (int i = 0; i < fl.nrooms; i++) {+ Room *r = &fl.rooms[i];+ put(r, 6); /* kind, template, group, state, map cell (the fields before the grid; those after it below) */+ put8(r->nsaved), put8(r->boss), put8(r->variant), put8(r->doors_open);+ int n = 0;+ while (n < ROWS * COLS) {+ int c = r->grid[n / COLS][n % COLS], h = r->gridhp[n / COLS][n % COLS], len = 1;+ while (n + len < ROWS * COLS && len < 255 && r->grid[(n + len) / COLS][(n + len) % COLS] == c+ && r->gridhp[(n + len) / COLS][(n + len) % COLS] == h)+ len++;+ put8(len), put8(c), put8(h);+ n += len;+ }+ put(r->saved, sizeof(Saved) * r->nsaved);+ }+ if (over) return false;+ bool ok = plat_save(RUN_NAME, buf, at);+ copy_write();+ return ok;+}++bool load_run(void) {+ uint32_t len;+ src = plat_load(RUN_NAME, &len);+ if (!src || len < 16) return false;+ src_len = len, rp = 0, bad = false;+ uint32_t magic, size;+ get(&magic, 4), get(&size, 4);+ if (magic != RUN_MAGIC || size != sizeof(Player)) return false;+ get(&g_run_seed, 4), get(&g_rng, 4), get(&g_frame, 4);+ get(&pl, sizeof pl);+ for (int i = 0; i < ITEM_MAX; i += 8) {+ int b = get8();+ for (int k = 0; k < 8 && i + k < ITEM_MAX; k++) item_taken[i + k] = (uint8_t)((b >> k) & 1);+ }+ memset(&fl, 0, sizeof fl);+ fl.stage = (uint8_t)get8(), fl.alt = (uint8_t)get8(), fl.curse = (uint8_t)get8(), fl.nrooms = (uint8_t)get8();+ get(fl.map, sizeof fl.map);+ fl.cur = (int8_t)get8(), fl.start = (int8_t)get8(), fl.boss_room = (int8_t)get8(), fl.devil_room = (int8_t)get8();+ fl.boss = (uint8_t)get8();+ int b = get8();+ fl.devil_open = b & 1, fl.devil_angel = (b >> 1) & 1, fl.boss_beaten = (b >> 2) & 1, fl.trapdoor = (b >> 3) & 1;+ get(&fl.seed, 4);+ if (fl.nrooms > MAX_ROOMS) return false;+ for (int i = 0; i < fl.nrooms && !bad; i++) {+ Room *r = &fl.rooms[i];+ get(r, 6);+ r->nsaved = (uint8_t)get8(), r->boss = (uint8_t)get8(), r->variant = (uint8_t)get8(), r->doors_open = (uint8_t)get8();+ int n = 0;+ while (n < ROWS * COLS && !bad) {+ int l = get8(), c = get8(), h = get8();+ if (!l) bad = true;+ for (int k = 0; k < l && n < ROWS * COLS; k++, n++) r->grid[n / COLS][n % COLS] = (uint8_t)c, r->gridhp[n / COLS][n % COLS] = (uint8_t)h;+ }+ if (r->nsaved > ROOM_SAVED) bad = true;+ else get(r->saved, sizeof(Saved) * r->nsaved);+ }+ if (bad || fl.cur < 0 || fl.cur >= fl.nrooms) return false;+ return true;+}++bool run_saved(void) {+ uint32_t len;+ const uint8_t *d = plat_load(RUN_NAME, &len);+ uint32_t magic, size;+ if (!d || len < 8) return false;+ memcpy(&magic, d, 4), memcpy(&size, d + 4, 4);+ return magic == RUN_MAGIC && size == sizeof(Player);+}++void save_remove_run(void) {+ plat_remove(RUN_NAME);+ copy_write();+}++/* ---------------------------------------------------------------- progress over every run */+typedef struct {+ uint32_t magic;+ uint16_t runs, deaths;+ uint8_t wins, best_stage, pad[2];+} Progress;++void save_progress(void) {+ Progress p = {PROGRESS_MAGIC, g_runs, g_deaths, g_wins, g_best_stage, {0, 0}};+ plat_save(PROGRESS_NAME, &p, sizeof p);+ copy_write();+}++void load_progress(void) {+ uint32_t len;+ const uint8_t *d = plat_load(PROGRESS_NAME, &len);+ Progress p;+ if (!d || len != sizeof p) return;+ memcpy(&p, d, sizeof p);+ if (p.magic != PROGRESS_MAGIC) return;+ g_runs = p.runs, g_deaths = p.deaths, g_wins = p.wins, g_best_stage = p.best_stage;+}diff --git a/games/isaac/src/tears.c b/games/isaac/src/tears.cnew file mode 100644index 00000000..e0e74759--- /dev/null+++ b/games/isaac/src/tears.c@@ -0,0 +1,362 @@+/* Tears, the monsters' shots, lasers and bombs.+ *+ * A tear flies at its height over its shadow until it has gone its range+ * (23.75 is 6.5 cells), then drops and splashes. Brimstone's laser runs to+ * the wall and hurts all it crosses for a moment; a bomb goes off after a+ * second and a half, a heart's worth of hurt for Isaac in its blast. */+#include <string.h>+#include "game.h"+#include "gfx.h"+#include "res.h"++Tear tears[MAX_TEARS];+Laser lasers[MAX_LASERS];++#define RANGE_PX (130.0f / 23.75f) /* pixels of flight for each point of range */++void tears_clear(void) {+ memset(tears, 0, sizeof tears);+ memset(lasers, 0, sizeof lasers);+}++static Tear *tear_new(void) {+ for (int i = 0; i < MAX_TEARS; i++)+ if (!tears[i].active) {+ memset(&tears[i], 0, sizeof tears[i]);+ tears[i].active = 1;+ tears[i].lasthit = -1;+ return &tears[i];+ }+ return NULL;+}++static float tear_size(float dmg, uint32_t flags) {+ float s = 0.75f + 0.25f * fsqrt(dmg / 3.5f);+ if (flags & TF_BIG) s *= 1.6f;+ if (s > 2.6f) s = 2.6f;+ return s;+}++Tear *tear_fire(float x, float y, float vx, float vy, float dmg, uint32_t flags) {+ Tear *t = tear_new();+ if (!t) return NULL;+ t->x = x, t->y = y + 1, t->z = -13, t->vx = vx, t->vy = vy;+ t->dmg = dmg, t->flags = flags;+ /* Tough Love: a tooth now and then, three times as hard */+ if ((flags & TF_TOOTH) && rnd(100) < 10 + (int)(pl.luck * 3)) t->dmg *= 3.2f, t->flags |= TF_TOOTH << 0;+ else t->flags &= ~(uint32_t)TF_TOOTH;+ t->size = tear_size(t->dmg, flags);+ t->range = pl.range * RANGE_PX;+ if (flags & TF_ARC) t->vz = -3.2f, t->range = 1e9f;+ return t;+}++Tear *enemy_shot(float x, float y, float vx, float vy, int kind) {+ Tear *t = tear_new();+ if (!t) return NULL;+ t->x = x, t->y = y, t->z = -10, t->vx = vx, t->vy = vy;+ t->dmg = 1, t->size = 1, t->flags = TF_ENEMY | (kind == 1 ? TF_BONE : kind == 2 ? TF_FIRE : 0);+ t->range = 260;+ return t;+}++void enemy_shots_ring(float x, float y, int n, float speed, float a0) {+ for (int i = 0; i < n; i++) {+ float a = a0 + 6.2831853f * i / n;+ enemy_shot(x, y, fcos(a) * speed, fsin(a) * speed, 0);+ }+}++void enemy_shot_at(float x, float y, float speed) {+ float dx = pl.x - x, dy = pl.y - y, d = fsqrt(dx * dx + dy * dy);+ if (d < 1) d = 1;+ enemy_shot(x, y, dx / d * speed, dy / d * speed, 0);+}++static void splash(Tear *t) {+ if (t->flags & TF_EXPLODE) explode(t->x, t->y, 26, t->dmg + 30, false, 1);+ else fx_spawn(FX_SPLASH, t->x, t->y + t->z, 6);+ t->active = 0;+}++static void tear_hit_monster(Tear *t, Ent *e) {+ float dmg = t->dmg;+ if (t->flags & TF_COAL) dmg *= 1 + t->dist / 110;+ if (t->flags & TF_PROPTOSIS) {+ float k = 1 - t->dist / 150;+ dmg *= k < 0.25f ? 0.25f : k;+ }+ float d = fsqrt(t->vx * t->vx + t->vy * t->vy);+ float kx = d > 0 ? t->vx / d : 0, ky = d > 0 ? t->vy / d : 0;+ ent_damage(e, dmg, t->flags, kx * (t->flags & TF_CONFUSE ? 2.5f : 1.2f), ky * (t->flags & TF_CONFUSE ? 2.5f : 1.2f));+ /* what the items add */+ int luck = (int)pl.luck;+ if ((t->flags & TF_POISON) && rnd(100) < 35 + luck * 5) e->poison = 60, e->poison_dmg = pl.damage * 0.25f;+ if ((t->flags & TF_SLOW) && rnd(100) < 25 + luck * 5) e->slow = 75;+ if ((t->flags & TF_FREEZE) && rnd(100) < 20 + luck * 5) e->freeze = 45;+ if ((t->flags & TF_CONFUSE) && rnd(100) < 15 + luck * 5) e->confuse = 50;+ if (t->flags & TF_SPLIT) { /* The Parasite: two smaller tears off to the sides */+ for (int s = -1; s <= 1; s += 2) {+ Tear *n = tear_new();+ if (!n) break;+ *n = *t;+ n->vx = -t->vy * s * 0.8f, n->vy = t->vx * s * 0.8f;+ n->dmg = t->dmg * 0.5f, n->size = t->size * 0.7f, n->flags &= ~(uint32_t)TF_SPLIT, n->lasthit = (int8_t)(e - ents);+ n->dist = 0, n->range = 60;+ }+ }+ if (t->flags & TF_EXPLODE) {+ explode(t->x, t->y, 26, t->dmg + 30, false, 1);+ t->active = 0;+ return;+ }+ if (t->flags & TF_PIERCE) {+ t->lasthit = (int8_t)(e - ents);+ return;+ }+ splash(t);+}++static void tear_update(Tear *t) {+ bool enemy = t->flags & TF_ENEMY;+ /* homing: a turn towards the nearest monster */+ if ((t->flags & TF_HOMING) && !enemy) {+ Ent *e = ent_nearest_monster(t->x, t->y, 140);+ if (e) {+ float sp = fsqrt(t->vx * t->vx + t->vy * t->vy), dx = e->x - t->x, dy = e->y - t->y, d = fsqrt(dx * dx + dy * dy);+ if (d > 1) {+ t->vx += (dx / d * sp - t->vx) * 0.12f, t->vy += (dy / d * sp - t->vy) * 0.12f;+ float n = fsqrt(t->vx * t->vx + t->vy * t->vy);+ if (n > 0) t->vx *= sp / n, t->vy *= sp / n;+ }+ }+ }+ /* My Reflection: back to Isaac past half its way */+ if ((t->flags & TF_BOOMERANG) && t->dist > t->range * 0.5f && !(t->flags & TF_ENEMY)) {+ float dx = pl.x - t->x, dy = pl.y - t->y, d = fsqrt(dx * dx + dy * dy);+ float sp = fsqrt(t->vx * t->vx + t->vy * t->vy);+ if (d < 8) {+ t->active = 0;+ return;+ }+ t->vx += (dx / d * sp - t->vx) * 0.15f, t->vy += (dy / d * sp - t->vy) * 0.15f;+ }+ t->x += t->vx, t->y += t->vy;+ float step = fsqrt(t->vx * t->vx + t->vy * t->vy);+ t->dist += step;+ t->life++;+ /* height: a tear falls once it has gone its range (an arc from the start for Ipecac) */+ if (t->flags & TF_ARC) {+ t->vz += 0.22f, t->z += t->vz;+ if (t->z >= 0) {+ splash(t);+ return;+ }+ } else if (t->dist > t->range) {+ t->vz += 0.55f, t->z += t->vz;+ if (t->z >= 0) {+ splash(t);+ return;+ }+ }+ /* walls */+ if (t->x < ROOM_X - 4 || t->x > ROOM_R + 4 || t->y < ROOM_Y - 4 || t->y > ROOM_B + 4) {+ if (t->flags & TF_BOOMERANG) t->vx = -t->vx, t->vy = -t->vy;+ else {+ splash(t);+ return;+ }+ }+ /* the grid */+ int c = (int)((t->x - ROOM_X) / TILE), r = (int)((t->y - ROOM_Y) / TILE);+ if (t->x >= ROOM_X && t->y >= ROOM_Y && c < COLS && r < ROWS && grid_blocks_tears(c, r) && !(t->flags & (TF_SPECTRAL | TF_ARC))) {+ if (!enemy) grid_hit(c, r, t->dmg);+ splash(t);+ return;+ }+ if (enemy) {+ float dx = pl.x - t->x, dy = (pl.y - 4) - t->y;+ if (dx * dx + dy * dy < 7 * 7 * t->size) {+ if (player_hurt(1, -10)) {}+ splash(t);+ }+ return;+ }+ /* monsters */+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || (e->flags & (EF_FRIEND | EF_HIDDEN | EF_NOHIT)) || i == t->lasthit) continue;+ float rr = e->r + 3 * t->size;+ float dx = e->x - t->x, dy = e->y - t->y;+ if (dx * dx + dy * dy < rr * rr && (e->flags & EF_FLYING || t->z > -40)) {+ tear_hit_monster(t, e);+ if (!t->active) return;+ }+ }+ /* fire places and machines take tears too */+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MACHINE || e->type > MA_BOMB_BUM) continue;+ float dx = e->x - t->x, dy = (e->y - 8) - t->y;+ if (dx * dx + dy * dy < 12 * 12) {+ splash(t);+ return;+ }+ }+}++void tears_update(void) {+ for (int i = 0; i < MAX_TEARS; i++)+ if (tears[i].active) tear_update(&tears[i]);+}++void tears_draw(void) {+ for (int i = 0; i < MAX_TEARS; i++) {+ Tear *t = &tears[i];+ if (!t->active) continue;+ int s = (int)(t->size * 170);+ if (t->flags & TF_ENEMY) {+ gfx_ellipse((int)t->x, (int)t->y, 3, 1, C_BLACK, 10);+ int spr = (t->flags & TF_BONE) ? SPR_BONE_SHOT : SPR_ENEMY_SHOT;+ gfx_sprite_scaled(spr, 0, (int)t->x, (int)(t->y + t->z), 180, 180, 0, (t->flags & TF_FIRE) ? rgb565(0xFF8020) : 0,+ (t->flags & TF_FIRE) ? 16 : 0, 32);+ continue;+ }+ gfx_ellipse((int)t->x, (int)t->y, 2 + (int)t->size, 1, C_BLACK, 9);+ int spr = (t->flags & TF_BLOOD) ? SPR_BLOOD_TEAR : SPR_TEAR;+ uint16_t tint = 0;+ int amount = 0;+ if (t->flags & TF_HOMING) tint = rgb565(0xB040E0), amount = 14;+ else if (t->flags & TF_POISON) tint = rgb565(0x40C040), amount = 16;+ else if (t->flags & TF_FREEZE) tint = rgb565(0xFFFFFF), amount = 16;+ else if (t->flags & TF_SLOW) tint = rgb565(0xA0A0A0), amount = 18;+ else if (t->flags & TF_EXPLODE) tint = rgb565(0x30C030), amount = 18;+ else if (t->flags & TF_COAL) tint = rgb565(0x202020), amount = 22;+ else if (t->flags & TF_SPECTRAL) tint = rgb565(0xFFFFFF), amount = 10;+ if (pl.has[6] && !(t->flags & TF_BLOOD)) tint = rgb565(0xF0E040), amount = 18;+ gfx_sprite_scaled(spr, 0, (int)t->x, (int)(t->y + t->z), s, s, 0, tint, amount, (t->flags & TF_SPECTRAL) ? 26 : 32);+ }+}++/* ---------------------------------------------------------------- lasers */+Laser *laser_fire(float x, float y, int dir, float width, float dmg, int life, int kind, bool enemy) {+ for (int i = 0; i < MAX_LASERS; i++)+ if (!lasers[i].active) {+ Laser *l = &lasers[i];+ memset(l, 0, sizeof *l);+ l->active = 1, l->x = x, l->y = y, l->dir = (int8_t)dir, l->width = width, l->dmg = dmg, l->life = l->max = (int16_t)life;+ l->kind = (uint8_t)kind, l->enemy = enemy;+ return l;+ }+ return NULL;+}++/* the beam's rectangle on the floor */+static void laser_box(const Laser *l, float *x0, float *y0, float *x1, float *y1) {+ float hw = l->width / 2;+ if (l->dir < 0) { /* (a beam from the sky: Crack the Sky) */+ *x0 = l->x - hw, *x1 = l->x + hw, *y0 = 0, *y1 = l->y;+ return;+ }+ float fy = l->y + 10; /* (its floor line: the head is 10 over the feet) */+ switch (l->dir) {+ case DIR_UP: *x0 = l->x - hw, *x1 = l->x + hw, *y0 = 0, *y1 = fy; break;+ case DIR_DOWN: *x0 = l->x - hw, *x1 = l->x + hw, *y0 = fy, *y1 = SCREEN_H; break;+ case DIR_LEFT: *x0 = 0, *x1 = l->x, *y0 = fy - hw, *y1 = fy + hw; break;+ default: *x0 = l->x, *x1 = SCREEN_W, *y0 = fy - hw, *y1 = fy + hw; break;+ }+}++void lasers_update(void) {+ for (int i = 0; i < MAX_LASERS; i++) {+ Laser *l = &lasers[i];+ if (!l->active) continue;+ if (!l->enemy && l->kind == 0 && l->dir >= 0) l->x = pl.x, l->y = pl.y - 10; /* (Brimstone follows him) */+ float x0, y0, x1, y1;+ laser_box(l, &x0, &y0, &x1, &y1);+ if (l->enemy) {+ if (pl.x + 4 > x0 && pl.x - 4 < x1 && pl.y > y0 && pl.y - 8 < y1 && l->life < l->max - 4) player_hurt(1, -11);+ } else {+ for (int k = 0; k < MAX_ENTS; k++) {+ Ent *e = &ents[k];+ if (e->kind != EK_MON || (e->flags & (EF_FRIEND | EF_HIDDEN))) continue;+ if (e->x + e->r > x0 && e->x - e->r < x1 && e->y + e->r > y0 && e->y - e->r < y1) ent_damage(e, l->dmg, 0, 0, 0);+ }+ /* poop and fire burn away under it */+ if ((l->life & 3) == 0)+ for (int r = 0; r < ROWS; r++)+ for (int c = 0; c < COLS; c++) {+ float cx = ROOM_X + c * TILE + TILE / 2, cy = ROOM_Y + r * TILE + TILE / 2;+ if (cx > x0 && cx < x1 && cy > y0 && cy < y1) grid_hit(c, r, l->dmg);+ }+ }+ if (--l->life <= 0) l->active = 0;+ }+}++void lasers_draw(void) {+ for (int i = 0; i < MAX_LASERS; i++) {+ Laser *l = &lasers[i];+ if (!l->active) continue;+ float x0, y0, x1, y1;+ laser_box(l, &x0, &y0, &x1, &y1);+ /* drawn at its height: the beam a head over its floor line */+ int lift = l->dir == DIR_LEFT || l->dir == DIR_RIGHT ? 10 : 0;+ int a = l->life > 3 ? 32 : l->life * 8;+ uint16_t edge, core;+ switch (l->kind) {+ case 1: edge = rgb565(0xE02040), core = rgb565(0xFFC0D0); break;+ case 2: edge = rgb565(0xFFF0A0), core = rgb565(0xFFFFFF); break;+ default: edge = rgb565(0x900000), core = rgb565(0xFF4040); break;+ }+ if (l->enemy) edge = rgb565(0x700000), core = rgb565(0xFF2020);+ int ix0 = (int)x0, iy0 = (int)y0 - lift, w = (int)(x1 - x0), h = (int)(y1 - y0);+ gfx_rect(ix0, iy0, w, h, edge, a * 3 / 4);+ if (l->dir == DIR_UP || l->dir == DIR_DOWN || l->dir < 0) gfx_rect(ix0 + w / 4, iy0, w / 2 > 0 ? w / 2 : 1, h, core, a);+ else gfx_rect(ix0, iy0 + h / 4, w, h / 2 > 0 ? h / 2 : 1, core, a);+ }+}++/* ---------------------------------------------------------------- bombs and explosions */+void bomb_place(float x, float y, int kind) {+ Ent *b = ent_new(EK_BOMB, x, y);+ if (!b) return;+ b->type = (uint8_t)kind;+ b->t = 45;+ b->r = 6;+ b->spr = kind == 1 ? SPR_TROLL_BOMB_LIT : (pl.has[106] && kind == 0) || kind == 2 ? SPR_BIG_BOMB+ : pl.has[140] && kind == 0 ? SPR_POISON_BOMB : SPR_BOMB;+ b->hp = kind == 2 ? 185 : pl.has[106] ? 185 : 60; /* (its damage) */+}++void explode(float x, float y, float radius, float dmg, bool hurt_player, int kind) {+ Ent *f = ent_new(EK_FX, x, y);+ if (f) f->type = FX_EXPLOSION, f->t = 14, f->a = radius;+ g_shake = 8;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON && e->kind != EK_PICKUP && e->kind != EK_BOMB && e->kind != EK_MACHINE) continue;+ float dx = e->x - x, dy = e->y - y, d2 = dx * dx + dy * dy;+ if (d2 > (radius + e->r) * (radius + e->r)) continue;+ float d = fsqrt(d2);+ if (d < 1) d = 1;+ if (e->kind == EK_MON && !(e->flags & EF_FRIEND)) {+ ent_damage(e, dmg, 0, dx / d * 4, dy / d * 4);+ if (kind == 2 || pl.has[140]) e->poison = 60, e->poison_dmg = 3;+ } else if (e->kind == EK_PICKUP || e->kind == EK_BOMB) {+ e->vx += dx / d * 4, e->vy += dy / d * 4;+ if (e->kind == EK_BOMB && e->t > 2) e->t = 2; /* (a chain) */+ } else if (e->kind == EK_MACHINE && e->type <= MA_BOMB_BUM && e->state != 255) {+ /* a blown-up machine pays out once */+ e->state = 255;+ fx_spawn(FX_POOF, e->x, e->y, 10);+ if (e->type == MA_SLOT) for (int k = 0; k < 2 + rnd(3); k++) pickup_spawn(PK_PENNY, 0, e->x, e->y, true);+ else pickup_drop_random(e->x, e->y);+ e->kind = EK_NONE;+ }+ }+ grid_explode(x, y, radius);+ float dx = pl.x - x, dy = pl.y - y;+ if (hurt_player && dx * dx + dy * dy < (radius + 4) * (radius + 4) && !pl.has[190]) player_hurt(2, -1);+ decal_blood(x, y, -radius * 0.7f); /* (a scorch mark: negative is black) */+}diff --git a/games/isaac/tests/play.c b/games/isaac/tests/play.cnew file mode 100644index 00000000..1b0778f0--- /dev/null+++ b/games/isaac/tests/play.c@@ -0,0 +1,200 @@+/* Plays The Binding of Isaac on a computer, with scripted keys, and saves frames.+ *+ * play [--frames N] [--keys "10-40:right,50:ok,..."] [--shots 30,60|all|every:N] [--out DIR] [--saves DIR]+ * [--seed N] [--run] [--stage S] [--give ID,ID...] [--bot] [--god] [--boss MON] [--room KIND]+ *+ * Keys: left right up down sleft sright sup sdown ok back bomb item card map home; "a-b:key" holds the key+ * from frame a to b, "a:key" for one frame. --run starts a new run past the title (--stage: on that floor).+ * --bot plays by itself: it goes for the monsters, crying at them, then on through the doors to the rooms it+ * hasn't seen, then down the trapdoor; --god keeps Isaac alive. Screenshots are DIR/shot_<frame>.ppm. */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include "../src/game.h"+#include "../src/gfx.h"+#include "../src/res.h"++extern uint32_t host_keys, host_time;+void host_shot(const char *path);+void host_save_dir(const char *d);+extern const uint8_t is_data[];+int floor_neighbour(int d);++typedef struct { int a, b; uint32_t k; } Hold;+static Hold holds[4096];+static int nholds;++static uint32_t key_of(const char *s) {+ static const struct { const char *n; uint32_t k; } names[] = {+ {"left", K_LEFT}, {"right", K_RIGHT}, {"up", K_UP}, {"down", K_DOWN}, {"sleft", K_SLEFT}, {"sright", K_SRIGHT},+ {"sup", K_SUP}, {"sdown", K_SDOWN}, {"ok", K_OK}, {"back", K_BACK}, {"bomb", K_BOMB}, {"item", K_ITEM},+ {"card", K_CARD}, {"map", K_MAP}, {"home", K_HOME}};+ for (unsigned i = 0; i < sizeof names / sizeof names[0]; i++)+ if (!strcmp(s, names[i].n)) return names[i].k;+ fprintf(stderr, "unknown key %s\n", s);+ exit(2);+}++static void parse_keys(char *s) {+ for (char *tok = strtok(s, ","); tok; tok = strtok(NULL, ",")) {+ char *colon = strchr(tok, ':');+ if (!colon) continue;+ *colon = 0;+ int a, b;+ if (sscanf(tok, "%d-%d", &a, &b) != 2) b = a = atoi(tok);+ holds[nholds++] = (Hold){a, b, key_of(colon + 1)};+ }+}++/* ---------------------------------------------------------------- the bot */+static int bot_stuck, bot_last_room = -1, bot_target_door = -1;+static float bot_lx, bot_ly;+static uint32_t bot_keys(void) {+ uint32_t k = 0;+ if (g_mode == MODE_WIN || g_mode == MODE_STAGE) return (g_frame & 8) ? K_OK : 0;+ if (g_mode != MODE_PLAY || g_transition) return 0;+ Ent *m = NULL;+ float best = 1e9f;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ if (e->kind != EK_MON || (e->flags & (EF_FRIEND | EF_HIDDEN))) continue;+ float d = dist2(e->x, e->y, pl.x, pl.y);+ if (d < best) best = d, m = e;+ }+ float tx = 160, ty = 120;+ if (m) {+ /* line up with it, a little way off, and cry at it */+ float dx = m->x - pl.x, dy = m->y - pl.y;+ if (absf(dx) < absf(dy)) {+ tx = m->x, ty = m->y + (dy > 0 ? -50 : 50);+ if (absf(dx) < 12) k |= dy > 0 ? K_SDOWN : K_SUP;+ } else {+ ty = m->y, tx = m->x + (dx > 0 ? -55 : 55);+ if (absf(dy) < 12) k |= dx > 0 ? K_SRIGHT : K_SLEFT;+ }+ if (!(k & K_SHOOT)) k |= absf(dx) > absf(dy) ? (dx > 0 ? K_SRIGHT : K_SLEFT) : (dy > 0 ? K_SDOWN : K_SUP);+ } else {+ /* pickups and items first, then the trapdoor, then a door to a room not yet visited */+ Ent *p = NULL;+ best = 1e9f;+ for (int i = 0; i < MAX_ENTS; i++) {+ Ent *e = &ents[i];+ bool want = (e->kind == EK_PEDESTAL && e->t3 && !(e->flags & EF_DEVIL) && !e->price) || e->kind == EK_TRAPDOOR+ || (e->kind == EK_PICKUP && !(e->flags & EF_SHOP) && e->type != PK_LOCKED_CHEST);+ if (!want) continue;+ float d = dist2(e->x, e->y, pl.x, pl.y) + (e->kind == EK_TRAPDOOR ? 1e6f : 0);+ if (d < best) best = d, p = e;+ }+ if (p && !(p->kind == EK_TRAPDOOR && bot_target_door >= 0 && fl.cur != fl.boss_room)) tx = p->x, ty = p->y;+ else {+ if (fl.cur != bot_last_room) bot_last_room = fl.cur, bot_target_door = -1;+ if (bot_target_door < 0) {+ int unvisited = -1, any = -1;+ for (int d = 0; d < 4; d++) {+ if (!room_door_exists(d, NULL) || !room_door_open(d)) continue;+ int j = floor_neighbour(d);+ if (j < 0) continue;+ if (any < 0 || rand() % 3 == 0) any = d;+ if (!(fl.rooms[j].state & RS_VISITED) && (unvisited < 0 || rand() % 2)) unvisited = d;+ }+ bot_target_door = unvisited >= 0 ? unvisited : any;+ }+ static const float dx[4] = {160, ROOM_X + 2, 160, ROOM_R - 2}, dy[4] = {ROOM_B - 2, 120, ROOM_Y + 2, 120};+ if (bot_target_door >= 0) tx = dx[bot_target_door], ty = dy[bot_target_door];+ }+ }+ float px, py;+ path_dir(pl.x, pl.y, tx, ty, pl.flight, &px, &py);+ if (dist2(tx, ty, pl.x, pl.y) > 9) {+ if (px < -0.3f) k |= K_LEFT;+ if (px > 0.3f) k |= K_RIGHT;+ if (py < -0.3f) k |= K_UP;+ if (py > 0.3f) k |= K_DOWN;+ }+ if (bot_target_door >= 0 && !m && dist2(tx, ty, pl.x, pl.y) < 20 * 20) {+ static const uint32_t way[4] = {K_DOWN, K_LEFT, K_UP, K_RIGHT};+ k |= way[bot_target_door];+ }+ /* stuck: a random way for a while; a bomb now and then */+ if (dist2(pl.x, pl.y, bot_lx, bot_ly) < 0.5f && (k & K_MOVE)) bot_stuck++;+ else bot_stuck = 0;+ bot_lx = pl.x, bot_ly = pl.y;+ if (bot_stuck > 40) {+ k = (k & ~K_MOVE) | (uint32_t[]){K_LEFT, K_RIGHT, K_UP, K_DOWN}[(g_frame / 20) % 4];+ if (bot_stuck > 200) bot_target_door = -1, bot_stuck = 0;+ }+ if (rand() % 400 == 0 && pl.bombs > 3) k |= K_BOMB;+ if (rand() % 200 == 0) k |= K_ITEM;+ if (rand() % 300 == 0) k |= K_CARD;+ return k;+}++int main(int argc, char **argv) {+ int frames = 600, seed = 1, stage = 1, every = 0;+ char keys[65536] = "", shots[65536] = "", out[512] = "build/shots", saves[512] = "build/host-saves", give[1024] = "";+ bool all = false, run = false, bot = false, god = false;+ int boss = -1;+ for (int i = 1; i < argc; i++) {+ const char *a = argv[i];+ const char *v = i + 1 < argc ? argv[i + 1] : "";+ if (!strcmp(a, "--frames")) frames = atoi(v), i++;+ else if (!strcmp(a, "--keys")) snprintf(keys, sizeof keys, "%s", v), i++;+ else if (!strcmp(a, "--shots")) {+ if (!strcmp(v, "all")) all = true;+ else if (!strncmp(v, "every:", 6)) every = atoi(v + 6);+ else snprintf(shots, sizeof shots, ",%s,", v);+ i++;+ } else if (!strcmp(a, "--out")) snprintf(out, sizeof out, "%s", v), i++;+ else if (!strcmp(a, "--saves")) snprintf(saves, sizeof saves, "%s", v), i++;+ else if (!strcmp(a, "--seed")) seed = atoi(v), i++;+ else if (!strcmp(a, "--stage")) stage = atoi(v), i++;+ else if (!strcmp(a, "--give")) snprintf(give, sizeof give, "%s", v), i++;+ else if (!strcmp(a, "--boss")) boss = atoi(v), i++;+ else if (!strcmp(a, "--run")) run = true;+ else if (!strcmp(a, "--bot")) bot = true;+ else if (!strcmp(a, "--god")) god = true;+ }+ mkdir(out, 0755);+ mkdir(saves, 0755);+ host_save_dir(saves);+ parse_keys(keys);+ srand((unsigned)seed);+ is_bin = is_data;+ g_rng = 0x9E3779B9u * (uint32_t)seed;+ game_init();+ if (run) {+ host_time = (uint32_t)seed * 7919u;+ game_new_run();+ if (stage > 1) {+ while (fl.stage < stage) game_next_floor();+ }+ if (boss >= 0) fl.boss = (uint8_t)boss, fl.rooms[fl.boss_room].boss = (uint8_t)boss;+ for (char *tok = strtok(give, ","); tok; tok = strtok(NULL, ",")) item_give(atoi(tok));+ g_mode = MODE_PLAY;+ }+ int floors_seen = fl.stage;+ for (int f = 0; f < frames && g_running; f++) {+ uint32_t k = 0;+ for (int i = 0; i < nholds; i++)+ if (f >= holds[i].a && f <= holds[i].b) k |= holds[i].k;+ if (bot && !k) k = bot_keys();+ if (god) pl.invuln = pl.invuln > 2 ? pl.invuln : 2, pl.dead = false;+ host_keys = k;+ host_time += 33;+ game_frame(k);+ if (fl.stage > floors_seen) floors_seen = fl.stage;+ game_draw();+ char tag[32];+ snprintf(tag, sizeof tag, ",%d,", f);+ if (all || (every && f % every == 0) || strstr(shots, tag)) {+ char p[600];+ snprintf(p, sizeof p, "%s/shot_%05d.ppm", out, f);+ host_shot(p);+ }+ }+ printf("END mode %d stage %d room %d kind %d hp %d/%d soul %d coins %d bombs %d keys %d items %d frames %u floors %d dropped %d\n",+ g_mode, fl.stage, fl.cur, room_cur()->kind, pl.red, pl.containers * 2, pl.soul, pl.coins, pl.bombs, pl.keys, pl.norder,+ g_frame, floors_seen, gfx_dropped);+ return 0;+}diff --git a/games/isaac/tools/art.py b/games/isaac/tools/art.pynew file mode 100644index 00000000..8909e353--- /dev/null+++ b/games/isaac/tools/art.py@@ -0,0 +1,382 @@+"""Pictures drawn for the port, in the game's own colours, where the editor's resources (tools/fetch.py) have none:+Isaac turning and walking (the resources show him only from the front), the HUD's hearts and counters, the map's+room icons, and the doors. Drawn at the game's size (26 pixels a cell), then scaled down with the rest."""+from PIL import Image, ImageDraw++# Isaac's colours (from his picture): outline, skin, shade, deeper shade, white, tears+ISAAC = {"#": (8, 0, 0), "+": (227, 198, 197), "o": (207, 156, 155), "r": (185, 115, 113), "w": (255, 255, 255),+ "b": (156, 200, 204), "B": (103, 151, 156), "x": (120, 70, 70)}+++def art(rows, pal):+ """rows of characters -> RGBA, '.' transparent"""+ h, w = len(rows), max(len(r) for r in rows)+ im = Image.new("RGBA", (w, h), (0, 0, 0, 0))+ for y, r in enumerate(rows):+ for x, ch in enumerate(r):+ if ch in pal:+ c = pal[ch]+ im.putpixel((x, y), tuple(c) + ((255,) if len(c) == 3 else ()))+ return im+++# Isaac's head, 28 x 24, from the front (his picture), and the ways it turns+HEAD_FRONT = [+ "..........########..........",+ "........############........",+ "......####oooooooo####......",+ ".....###o++++++++++o###.....",+ "....##o++++++++++++++o##....",+ "...##o++++++++++++++++o##...",+ "..##o++++++++++++++++++o##..",+ "..##++++++++++++++++++++##..",+ ".##o++++++++++++++++++++o##.",+ ".##++++++++++++++++++++++##.",+ "##o++++++++++++++++++++++o##",+ "##o+o####o++++++++o####o+o##",+ "##o+#ww###++++++++#ww###+o##",+ "##oo#ww###++++++++#ww###oo##",+ "##ro######++####++######or##",+ ".##o######+######+######o##.",+ ".##ro####o+#wwww#+o####or##.",+ "..##obbbo++######++obbbo##..",+ "..##rbbb++++++++++++bbbr##..",+ "...##Bbb++++++++++++bbB##...",+ "....##Bbo++++++++++obB##....",+ ".....###Bo++++++++oB###.....",+ "......####roooooor####......",+ ".......##############.......",+]+# crying: his eyes screwed shut, his mouth wide+HEAD_FRONT_CRY = [+ "..........########..........",+ "........############........",+ "......####oooooooo####......",+ ".....###o++++++++++o###.....",+ "....##o++++++++++++++o##....",+ "...##o++++++++++++++++o##...",+ "..##o++++++++++++++++++o##..",+ "..##++++++++++++++++++++##..",+ ".##o++++++++++++++++++++o##.",+ ".##++++++++++++++++++++++##.",+ "##o++++++++++++++++++++++o##",+ "##o+o++++o++++++++o++++o+o##",+ "##o+##+++##++++++##+++##+o##",+ "##oo+#####++++++++#####+oo##",+ "##ro++++++++####++++++++or##",+ ".##o++++++######++++++++o##.",+ ".##ro+++++##xxxx##+++++or##.",+ "..##obbbo+#xxxxxx#+obbbo##..",+ "..##rbbb+++######+++bbbr##..",+ "...##Bbb++++++++++++bbB##...",+ "....##Bbo++++++++++obB##....",+ ".....###Bo++++++++oB###.....",+ "......####roooooor####......",+ ".......##############.......",+]+# turned to his right: one eye, his mouth at the edge+HEAD_SIDE = [+ "..........########..........",+ "........############........",+ "......####oooooooo####......",+ ".....###o++++++++++o###.....",+ "....##o++++++++++++++o##....",+ "...##o++++++++++++++++o##...",+ "..##o++++++++++++++++++o##..",+ "..##++++++++++++++++++++##..",+ ".##o++++++++++++++++++++o##.",+ ".##+++++++++++++++++++++o##.",+ "##o+++++++++++++++o####o+##.",+ "##o+++++++++++++++#ww###+o##",+ "##o+++++++++++++++#ww###++##",+ "##oo++++++++++++++######++##",+ "##ro+++++++++++++o######++##",+ ".##o++++++++++++++o####o####",+ ".##ro+++++++++++++obbbo+##w#",+ "..##o+++++++++++++rbbb+++###",+ "..##r+++++++++++++Bbbb++##..",+ "...##o++++++++++++Bbbo+##...",+ "....##o+++++++++++oBb+##....",+ ".....###oo+++++++++oB##.....",+ "......####roooooorr###......",+ ".......##############.......",+]+HEAD_SIDE_CRY = [+ "..........########..........",+ "........############........",+ "......####oooooooo####......",+ ".....###o++++++++++o###.....",+ "....##o++++++++++++++o##....",+ "...##o++++++++++++++++o##...",+ "..##o++++++++++++++++++o##..",+ "..##++++++++++++++++++++##..",+ ".##o++++++++++++++++++++o##.",+ ".##+++++++++++++++++++++o##.",+ "##o+++++++++++++++o++++o+##.",+ "##o+++++++++++++++##+++##o##",+ "##o++++++++++++++++#####++##",+ "##oo++++++++++++++++++++++##",+ "##ro+++++++++++++o+++++####.",+ ".##o++++++++++++++o+++#xxx#.",+ ".##ro+++++++++++++obbb#xxx#.",+ "..##o+++++++++++++rbbb+####.",+ "..##r+++++++++++++Bbbb++##..",+ "...##o++++++++++++Bbbo+##...",+ "....##o+++++++++++oBb+##....",+ ".....###oo+++++++++oB##.....",+ "......####roooooorr###......",+ ".......##############.......",+]+# the back of his head+HEAD_BACK = [+ "..........########..........",+ "........############........",+ "......####oooooooo####......",+ ".....###o++++++++++o###.....",+ "....##o++++++++++++++o##....",+ "...##o++++++++++++++++o##...",+ "..##o++++++++++++++++++o##..",+ "..##++++++++++++++++++++##..",+ ".##o++++++++++++++++++++o##.",+ ".##++++++++++++++++++++++##.",+ "##o++++++++++++++++++++++o##",+ "##o++++++++++++++++++++++o##",+ "##o++++++++++++++++++++++o##",+ "##oo++++++++++++++++++++oo##",+ "##ro++++++++++++++++++++or##",+ ".##o++++++++++++++++++++o##.",+ ".##ro++++++++++++++++++or##.",+ "..##oo++++++++++++++++oo##..",+ "..##roo++++++++++++++oor##..",+ "...##roo++++++++++++oor##...",+ "....##rroo++++++++oorr##....",+ ".....###rrooooooooorr###....",+ "......####rrrrrrrr####......",+ ".......##############.......",+]+# his body, 28 x 10 (its top row is under his chin), facing the screen or walking to the right+BODY_FRONT = [+ ".......##############.......",+ "......##o+########+o##......",+ ".....##o++oooooooo++o##.....",+ ".....##o+#o++++++o#+o##.....",+ ".....##o+#++++++++#+o##.....",+ "......####++o##o++####......",+ ".......###+++##o++###.......",+ "........###++##o+###........",+ ".........##########.........",+ "..........###..###..........",+]+BODY_FRONT_STEP = [+ ".......##############.......",+ "......##o+########+o##......",+ ".....##o++oooooooo++o##.....",+ ".....##o+#o++++++o#+o##.....",+ ".....##o+#++++++++#+o##.....",+ "......####++o##o++####......",+ ".......###+++##o++###.......",+ "........###++#####..........",+ ".........######o+##.........",+ "..........###.####..........",+]+BODY_SIDE = [+ ".........###########........",+ ".........##o+++++o##........",+ "........##o+++++++o##.......",+ "........##o++#++++o##.......",+ "........##o++#+++++##.......",+ ".........###++o++###........",+ "..........##++o++##.........",+ "..........##+o+++##.........",+ "..........#########.........",+ "...........###.###..........",+]+BODY_SIDE_STEP1 = [+ ".........###########........",+ ".........##o+++++o##........",+ "........##o+++++++o##.......",+ "........##o++#++++o##.......",+ "........##o++#+++++##.......",+ ".........###++o++###........",+ "........###+o####++##.......",+ ".......##+o##..##++o##......",+ ".......#####....######......",+ "............................",+]+BODY_SIDE_STEP2 = [+ ".........###########........",+ ".........##o+++++o##........",+ "........##o+++++++o##.......",+ "........##o++#++++o##.......",+ "........##o++#+++++##.......",+ ".........###+o+++###........",+ ".........###++##+o###.......",+ "........##++o####+o##.......",+ "........######..#####.......",+ "............................",+]+# arms up, holding what he found over his head+BODY_HOLD = [+ "..##...##############...##..",+ "..#+#.##o+########+o##.#+#..",+ "..#+o##o++oooooooo++o##o+#..",+ "...#o+#o++++++++++++o#+o#...",+ "....##++++++++++++++++##....",+ "......####++o##o++####......",+ ".......###+++##o++###.......",+ "........###++##o+###........",+ ".........##########.........",+ "..........###..###..........",+]+++def isaac_frames():+ """head: front, front crying, side, side crying, back, back crying; body: front x 4, side x 4, holding"""+ heads = [art(r, ISAAC) for r in (HEAD_FRONT, HEAD_FRONT_CRY, HEAD_SIDE, HEAD_SIDE_CRY, HEAD_BACK, HEAD_BACK)]+ fs = art(BODY_FRONT_STEP, ISAAC)+ bodies = [art(BODY_FRONT, ISAAC), fs, art(BODY_FRONT, ISAAC), fs.transpose(Image.FLIP_LEFT_RIGHT),+ art(BODY_SIDE, ISAAC), art(BODY_SIDE_STEP1, ISAAC), art(BODY_SIDE, ISAAC), art(BODY_SIDE_STEP2, ISAAC),+ art(BODY_HOLD, ISAAC)]+ return heads, bodies+++# ---------------------------------------------------------------- the HUD (drawn at the screen's size)+HEART = [+ "..###.###..",+ ".#aaa#aaa#.",+ "#aawaaaaaa#",+ "#awaaaaaaa#",+ "#aaaaaaaaa#",+ ".#aaaaaaa#.",+ "..#aaaaa#..",+ "...#aaa#...",+ "....#a#....",+ ".....#.....",+]+HEARTS = {"red": (210, 30, 30), "soul": (110, 140, 205), "black": (40, 36, 44), "eternal": (250, 250, 250),+ "empty": (40, 16, 16)}+++def heart(kind, half=False):+ """a HUD heart: full, or its left half (the right one an empty container)"""+ rows = [list(r) for r in HEART]+ pal = {"#": (0, 0, 0), "a": HEARTS[kind], "w": (255, 255, 255) if kind != "eternal" else (200, 200, 220),+ "e": HEARTS["empty"]}+ if kind == "black":+ pal["w"] = (100, 90, 110)+ if half:+ for r in rows:+ for x in range(6, len(r)):+ if r[x] in "aw":+ r[x] = "e" if kind == "red" else "."+ if kind != "red": # soul and black halves are the left half alone+ for x in range(6, len(r)):+ if r[x] == "#" and (x > 6 and "." in r[x - 1:x]):+ r[x] = "."+ if kind != "red":+ rows = [r[:6] + ["#" if r[5] in "aw#" and ch != "." else "." for ch in r[6:7]] + ["."] * 4 for r in rows]+ rows[0][6] = "."+ return art(["".join(r) for r in rows], pal)+++ICONS = {+ # 9 x 9: a coin, a bomb, a key+ "coin": ["..#####..", ".#yyyyy#.", "#yyYyyyy#", "#yYyy#yy#", "#yYy#yyy#", "#yyy#yyy#", "#yyyyyyy#",+ ".#yyyyy#.", "..#####.."],+ "bomb": ["......#..", ".....#.y.", "..####...", ".#gggg#..", "#gwgggg#.", "#gwggggg#", "#gggggg#.",+ ".#gggg#..", "..####..."],+ "key": ["..###....", ".#yyy#...", "#yy#yy#..", "#y#.#y#..", ".#yyy#...", "..#y#....", "..#yy#...",+ "..#y#....", "..#yy#..."],+}+ICON_PAL = {"#": (20, 12, 8), "y": (250, 200, 60), "Y": (200, 140, 30), "g": (70, 70, 80), "w": (170, 170, 190)}++# the map's room icons, 7 x 7: a skull, a crown, a dollar, a question mark, a heart (curse), an arcade die...+MAP_ICONS = {+ "boss": ["..###..", ".#####.", "#.#.#.#", "#######", ".#####.", ".#.#.#.", "......."],+ "treasure": ["#..#..#", "##.#.##", "#######", "#######", ".#####.", ".......", "......."],+ "shop": ["...#...", ".#####.", "#..#...", ".#####.", "...#..#", ".#####.", "...#..."],+ "secret": [".####..", "#....#.", ".....#.", "...##..", "...#...", ".......", "...#..."],+ "curse": ["#.....#", "##...##", ".#####.", ".#.#.#.", ".#####.", "..#.#..", "......."],+ "arcade": ["#######", "#.....#", "#.#.#.#", "#.....#", "#.#.#.#", "#.....#", "#######"],+ "sacrifice": ["...#...", "..###..", ".#####.", "#######", "...#...", "...#...", "...#..."],+ "library": ["##.##..", "#.#.#..", "#.#.#..", "#.#.#..", "#.#.#..", "##.##..", "......."],+ "miniboss": ["#.....#", "##...##", "#.###.#", "#######", ".#####.", "..#.#..", "......."],+ "challenge": ["...#...", "..#.#..", "..#.#..", ".#####.", "...#...", "..###..", "......."],+ "chest": ["..###..", ".#####.", "#######", "###.###", "#######", "#######", "......."],+}+++def map_icon(name, colour):+ return art(MAP_ICONS[name], {"#": colour})+++def door(kind, open_):+ """a door in the top wall, at the game's size (the room's 26-pixel cells): 52 wide, 40 deep; the floor's+ side is its bottom. The other three walls turn it."""+ w, h = 52, 40+ im = Image.new("RGBA", (w, h), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ frames = {"normal": ((92, 74, 66), (60, 46, 42), (130, 108, 96)),+ "treasure": ((222, 170, 40), (150, 100, 20), (255, 230, 120)),+ "boss": ((140, 20, 20), (70, 10, 10), (200, 60, 50)),+ "secret": ((70, 60, 54), (40, 34, 30), (90, 80, 72)),+ "shop": ((120, 90, 60), (70, 50, 30), (170, 130, 90)),+ "curse": ((110, 20, 26), (50, 8, 10), (160, 40, 40)),+ "devil": ((40, 10, 10), (20, 4, 4), (110, 20, 20)),+ "angel": ((230, 230, 240), (170, 170, 190), (255, 255, 255)),+ "arcade": ((200, 60, 140), (110, 30, 80), (250, 140, 200))}+ face, dark, light = frames[kind]+ if kind == "secret": # a hole blown in the wall+ d.ellipse([10, 6, 42, 40], fill=(16, 10, 8, 255))+ d.ellipse([14, 10, 38, 40], fill=(4, 2, 2, 255))+ for x, y in ((8, 10), (40, 12), (12, 30), (38, 32), (26, 4)):+ d.ellipse([x - 3, y - 2, x + 3, y + 2], fill=(60, 50, 44, 255))+ return im+ # the frame: an arch with its sides+ d.polygon([(4, 0), (48, 0), (46, 38), (6, 38)], fill=dark + (255,))+ d.polygon([(7, 2), (45, 2), (43, 36), (9, 36)], fill=face + (255,))+ d.line([(8, 3), (44, 3)], fill=light + (255,), width=2)+ # the opening+ d.polygon([(14, 4), (38, 4), (37, 38), (15, 38)], fill=(8, 4, 4, 255))+ if not open_: # two door halves, shut+ leaf = (88, 64, 50) if kind not in ("treasure", "angel") else (200, 160, 60)+ if kind == "boss":+ leaf = (90, 24, 20)+ d.rectangle([15, 8, 25, 37], fill=leaf + (255,))+ d.rectangle([27, 8, 36, 37], fill=leaf + (255,))+ d.line([(26, 8), (26, 37)], fill=(20, 10, 8, 255))+ for y in (14, 24, 33):+ d.line([(15, y), (36, y)], fill=(40, 26, 20, 255))+ if kind == "boss": # its spikes and eyes+ for x in (6, 14, 38, 46):+ d.polygon([(x - 3, 0), (x + 3, 0), (x, -0 + 6)], fill=(230, 220, 200, 255))+ d.ellipse([18, 0, 24, 5], fill=(255, 230, 120, 255))+ d.ellipse([28, 0, 34, 5], fill=(255, 230, 120, 255))+ if kind == "treasure":+ d.polygon([(22, 0), (26, -0), (24, 4)], fill=(255, 250, 200, 255))+ if kind == "curse":+ for x in (8, 16, 36, 44):+ d.polygon([(x - 3, 38), (x + 3, 38), (x, 30)], fill=(220, 210, 190, 255))+ return im+++def mom_foot():+ """Mom's foot, coming down from above: her leg, her foot and painted toenails (the game's size)"""+ w, h = 72, 132+ im = Image.new("RGBA", (w, h), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ skin, shade, dark, line = (232, 190, 176), (206, 156, 146), (170, 116, 108), (40, 16, 16)+ d.rectangle([18, 0, 54, 92], fill=line + (255,))+ d.rectangle([20, 0, 52, 92], fill=skin + (255,))+ d.rectangle([42, 0, 52, 92], fill=shade + (255,))+ d.ellipse([4, 74, 68, 128], fill=line + (255,))+ d.ellipse([6, 76, 66, 126], fill=skin + (255,))+ d.ellipse([40, 80, 64, 124], fill=shade + (255,))+ for i, x in enumerate((8, 20, 32, 44, 55)):+ r = 7 if i == 0 else 6 if i < 3 else 5+ y = 114 - (2 if i in (1, 2) else 0)+ d.ellipse([x - 1, y - 1, x + 2 * r + 1, y + 2 * r - 2], fill=line + (255,))+ d.ellipse([x, y, x + 2 * r, y + 2 * r - 3], fill=skin + (255,))+ d.ellipse([x + 2, y + r - 2, x + 2 * r - 2, y + 2 * r - 4], fill=(200, 30, 40, 255))+ d.line([(36, 20), (36, 80)], fill=dark + (255,), width=1)+ return imdiff --git a/games/isaac/tools/fetch.py b/games/isaac/tools/fetch.pynew file mode 100644index 00000000..e0c3e0be--- /dev/null+++ b/games/isaac/tools/fetch.py@@ -0,0 +1,45 @@+#!/usr/bin/env python3+"""Gathers the public material the pack is made from, into REF (not committed):++ python3 -I tools/fetch.py REF++- REF/br: Basement Renovator (github.com/Basement-Renovator/basement-renovator), the room editor of The Binding+ of Isaac's modders. Its resources hold the game's own pictures, as the editor shows them: every monster, boss,+ pickup, grid object and item (resources/Entities), and each floor's walls, rocks and pits (resources/Backgrounds)+- REF/fonts/upheavtt.ttf: Upheaval, by Brian Kent (freeware), the font of the game's titles++Files already there are kept: run it again to fetch only what is missing."""+import io+import os+import subprocess+import sys+import urllib.request+import zipfile++BR = "https://github.com/Basement-Renovator/basement-renovator"+BR_COMMIT = "c2ae4956764096238f47615face6d67325489cbc"+UPHEAVAL = "https://dl.dafont.com/dl/?f=upheaval"+UA = {"User-Agent": "NumPlay-Isaac/1.0 (fan port for the NumWorks calculator; one-time asset fetch)"}+++def main():+ if len(sys.argv) != 2:+ sys.exit(__doc__)+ ref = sys.argv[1]+ os.makedirs(ref, exist_ok=True)+ br = os.path.join(ref, "br")+ if not os.path.isdir(os.path.join(br, "resources")):+ subprocess.run(["git", "clone", "-q", BR, br], check=True)+ subprocess.run(["git", "-C", br, "checkout", "-q", BR_COMMIT], check=True)+ fonts = os.path.join(ref, "fonts")+ os.makedirs(fonts, exist_ok=True)+ if not os.path.exists(os.path.join(fonts, "upheavtt.ttf")):+ data = urllib.request.urlopen(urllib.request.Request(UPHEAVAL, headers=UA), timeout=60).read()+ with zipfile.ZipFile(io.BytesIO(data)) as z:+ for n in ("upheavtt.ttf", "upheaval.txt"):+ open(os.path.join(fonts, n), "wb").write(z.read(n))+ print("REF ready:", ref)+++if __name__ == "__main__":+ main()diff --git a/games/isaac/tools/pack.py b/games/isaac/tools/pack.pynew file mode 100644index 00000000..20a2c705--- /dev/null+++ b/games/isaac/tools/pack.py@@ -0,0 +1,453 @@+#!/usr/bin/env python3+"""The Binding of Isaac's pictures, rooms and font -> src/data.bin + src/data.h.++ python3 -I tools/pack.py REF (REF: the folder tools/fetch.py filled)++The game's pictures come at its own size, 26 pixels a cell of a room; the calculator shows a room whole at 20+(a room's walls and floor 360 x 240, its screen 320 x 240), so every picture is scaled by 20/26 here.++data.bin: "ISA1", the number of sections, their offsets, then the sections (SEC_* in data.h):+- FRAMES: the count, each frame's offset, then the frames: w, h, origin x, y, colours (u16 x 5, 0), the colours+ (RGB565), their opacity (0 to 32, a byte each, to an even count), each row's offset (u16) from the rows, then+ the rows: the number of runs, then each run's skip, length and palette indices+- SPRITES: for each sprite (SPR_*) its first frame and its number of frames (u16, u16)+- BACKDROPS: the count and offsets; each: w, h, colours (u16 x 3, 0), the colours, then w x h indices: the+ top-left quarter of a room, the rest of it mirrored+- FONTS: the count and offsets; each: height, first character, count, capitals only (bytes), 8 bytes a glyph+ (advance, w, h, ox, oy, 0, bitmap offset u16), then the bitmaps (rows of whole bytes, highest bit first)+- ROOMS: the count, then the rooms' layouts (tools/rooms.py): kind, chapters, weight, flags, 7 x 13 cells"""+import glob+import os+import struct+import sys++from PIL import Image, ImageDraw, ImageFont++HERE = os.path.dirname(os.path.abspath(__file__))+sys.path.insert(0, HERE)+import art as A # noqa: E402+import rooms as R # noqa: E402++OUT_BIN = os.path.join(HERE, "..", "src", "data.bin")+OUT_H = os.path.join(HERE, "..", "src", "data.h")+SECTIONS = ["FRAMES", "SPRITES", "BACKDROPS", "FONTS", "ROOMS"]+SCALE = 20 / 26+REF = None+++def rgb565(r, g, b):+ return ((r * 31 + 127) // 255) << 11 | ((g * 63 + 127) // 255) << 5 | ((b * 31 + 127) // 255)+++# ---------------------------------------------------------------- loading+def ent(name):+ """a picture of the editor's resources/Entities, by its file name (without .png)"""+ p = os.path.join(REF, "br", "resources", "Entities", name + ".png")+ if not os.path.exists(p):+ hits = glob.glob(os.path.join(REF, "br", "resources", "Entities", "**", name + ".png"), recursive=True)+ if not hits:+ raise SystemExit(f"pack: no picture {name}")+ p = hits[0]+ return Image.open(p).convert("RGBA")+++def item_pic(n):+ hits = glob.glob(os.path.join(REF, "br", "resources", "Entities", "Items", f"5.100.{n} - *.png"))+ if not hits:+ raise SystemExit(f"pack: no item {n}")+ return Image.open(hits[0]).convert("RGBA")+++def bg(name):+ return Image.open(os.path.join(REF, "br", "resources", "Backgrounds", name + ".png")).convert("RGBA")+++def trim(im):+ bb = im.getbbox()+ return im.crop(bb) if bb else im+++def scaled(im, s=SCALE):+ w, h = max(1, round(im.size[0] * s)), max(1, round(im.size[1] * s))+ # premultiplied, so the transparent pixels' colours don't bleed into the edges+ pm = im.convert("RGBa").resize((w, h), Image.LANCZOS)+ return pm.convert("RGBA")+++# ---------------------------------------------------------------- frames+class Frame:+ def __init__(self, im, ox, oy):+ self.im, self.ox, self.oy = im, ox, oy+++def feet(im, s=SCALE, dy=0):+ """a picture trimmed and scaled, its origin the middle of its bottom (where it stands)"""+ im = scaled(trim(im), s) if s != 1 else trim(im)+ return Frame(im, im.size[0] // 2, im.size[1] - 1 + dy)+++def centred(im, s=SCALE):+ im = scaled(trim(im), s) if s != 1 else trim(im)+ return Frame(im, im.size[0] // 2, im.size[1] // 2)+++def at(im, ox, oy):+ return Frame(im, ox, oy)+++def encode_frame(fr):+ im = fr.im.convert("RGBA")+ w, h = im.size+ px = im.load()+ # opacity in 5 steps; colours as the screen has them+ keyed = {}+ cells = [[None] * w for _ in range(h)]+ for y in range(h):+ for x in range(w):+ r, g, b, a = px[x, y]+ al = (a * 4 + 127) // 255 * 8 # 0, 8, 16, 24, 32+ if al == 0:+ continue+ k = (rgb565(r, g, b), al)+ keyed[k] = keyed.get(k, 0) + 1+ cells[y][x] = k+ if len(keyed) > 254:+ # too many: the colours brought down to 254 by a median cut, opacities kept+ rgb = Image.new("RGB", (w, h))+ rp = rgb.load()+ for y in range(h):+ for x in range(w):+ r, g, b, a = px[x, y]+ rp[x, y] = (r, g, b)+ q = rgb.quantize(colors=200, method=Image.Quantize.MEDIANCUT, dither=Image.Dither.NONE)+ qp, qpal = q.load(), q.getpalette()+ keyed = {}+ for y in range(h):+ for x in range(w):+ if cells[y][x] is None:+ continue+ i = qp[x, y]+ k = (rgb565(*qpal[3 * i:3 * i + 3]), cells[y][x][1])+ keyed[k] = keyed.get(k, 0) + 1+ cells[y][x] = k+ assert len(keyed) <= 254, len(keyed)+ pal = sorted(keyed, key=lambda k: -keyed[k])+ index = {k: i for i, k in enumerate(pal)}+ rows, offs = bytearray(), []+ for y in range(h):+ offs.append(len(rows))+ runs, x, last = [], 0, 0+ while x < w:+ if cells[y][x] is None:+ x += 1+ continue+ s = x+ while x < w and cells[y][x] is not None and x - s < 255:+ x += 1+ runs.append((s - last, [index[cells[y][i]] for i in range(s, x)]))+ last = x+ # skips over 255 are split into empty runs+ out = []+ for skip, ids in runs:+ while skip > 255:+ out.append((255, []))+ skip -= 255+ out.append((skip, ids))+ assert len(out) < 256+ rows.append(len(out))+ for skip, ids in out:+ rows += bytes([skip, len(ids)]) + bytes(ids)+ assert len(rows) < 65536+ n = len(pal)+ head = struct.pack("<6H", w, h, fr.ox & 0xFFFF, fr.oy & 0xFFFF, n, 0)+ body = b"".join(struct.pack("<H", c) for c, _ in pal) + bytes(a for _, a in pal) + bytes(n & 1)+ body += b"".join(struct.pack("<H", o) for o in offs)+ out = head + body + bytes(rows)+ return out + bytes(-len(out) % 4)+++# ---------------------------------------------------------------- the sprites+SPRITES = [] # (name, [Frame])+++def add(name, frames):+ if not isinstance(frames, list):+ frames = [frames]+ SPRITES.append((name, frames))+++def rock_cells(sheet, cells, s=SCALE):+ """32 x 32 cells of a rocks sheet, by (column, row), standing on the bottom of their cell's 26 pixels"""+ out = []+ for c, r in cells:+ im = sheet.crop((c * 32, r * 32, c * 32 + 32, r * 32 + 32))+ im = scaled(im, s)+ out.append(Frame(im, im.size[0] // 2, im.size[1] // 2))+ return out+++def sprites():+ heads, bodies = A.isaac_frames()+ add("ISAAC_HEAD", [Frame(scaled(h), round(14 * SCALE), round(23 * SCALE)) for h in heads])+ add("ISAAC_BODY", [Frame(scaled(b), round(14 * SCALE), round(9 * SCALE)) for b in bodies])+ # tears and shots+ add("TEAR", centred(ent("2.0.0 - Tear"), 1))+ add("BLOOD_TEAR", centred(ent("2.1.0 - Blood Tear"), 1))+ add("ENEMY_SHOT", centred(ent("9.0.0 - Enemy Tear"), 1))+ add("BONE_SHOT", centred(ent("9.0.1 - Bone")))+ add("KNIFE", centred(ent("8.0.0 - Knife")))+ # monsters (Rebirth's ids)+ monsters = [+ ("GAPER", "10.1.0 - Gaper"), ("FROWNING_GAPER", "10.0.0 - Frowning Gaper"), ("GUSHER", "11.0.0 - Gusher"),+ ("PACER", "11.1.0 - Pacer"), ("HORF", "12.0.0 - Horf"), ("FLY", "13.0.0 - Fly"), ("POOTER", "14.0.0 - Pooter"),+ ("SUPER_POOTER", "14.1.0 - Super Pooter"), ("CLOTTY", "15.0.0 - Clotty"), ("CLOT", "15.1.0 - Clot"),+ ("IBLOB", "15.2.0 - I.Blob"), ("MULLIGAN", "16.0.0 - Mulligan"), ("ATTACK_FLY", "18.0.0 - Attack Fly"),+ ("MAGGOT", "21.0.0 - Maggot"), ("HIVE", "22.0.0 - Hive"), ("CHARGER", "23.0.0 - Charger"),+ ("GLOBIN", "24.0.0 - Globin"), ("BOOM_FLY", "25.0.0 - Boom Fly"), ("RED_BOOM_FLY", "25.1.0 - Red Boom Fly"),+ ("MAW", "26.0.0 - Maw"), ("RED_MAW", "26.1.0 - Red Maw"), ("HOST", "27.0.0 - Host"), ("RED_HOST", "27.1.0 - Red Host"),+ ("HOPPER", "29.0.0 - Hopper"), ("TRITE", "29.1.0 - Trite"), ("BOIL", "30.0.0 - Boil"), ("SPITTY", "31.0.0 - Spitty"),+ ("BRAIN", "32.0.0 - Brain"), ("LEAPER", "34.0.0 - Leaper"), ("MR_MAW", "35.0.0 - Mr. Maw"), ("BABY", "38.0.0 - Baby"),+ ("ANGELIC_BABY", "38.1.0 - Angelic Baby"), ("VIS", "39.0.0 - Vis"), ("GUTS", "40.0.0 - Guts"), ("KNIGHT", "41.0.0 - Knight"),+ ("SELFLESS_KNIGHT", "41.1.0 - Selfless Knight"), ("LEECH", "55.0.0 - Leech"), ("KAMIKAZE_LEECH", "55.1.0 - Kamikaze Leech"),+ ("PARA_BITE", "58.0.0 - Para-Bite"), ("EYE", "60.0.0 - Eye"), ("BLOODSHOT_EYE", "60.1.0 - Bloodshot Eye"),+ ("SUCKER", "61.0.0 - Sucker"), ("SPIT", "61.1.0 - Spit"), ("EMBRYO", "77.0.0 - Embryo"), ("MOTER", "80.0.0 - Moter"),+ ("SPIDER", "85.0.0 - Spider"), ("KEEPER", "86.0.0 - Keeper"), ("GURGLE", "87.0.0 - Gurgle"),+ ("WALKING_BOIL", "88.0.0 - Walking Boil"), ("BUTTLICKER", "89.0.0 - Buttlicker"), ("HANGER", "90.0.0 - Hanger"),+ ("SWARMER", "91.0.0 - Swarmer"), ("HEART", "92.0.0 - Heart"), ("MASK", "93.0.0 - Mask"), ("BIG_SPIDER", "94.0.0 - Big Spider"),+ ("NEST", "205.0.0 - Nest"), ("BABY_LONG_LEGS", "206.0.0 - Baby Long Legs"), ("FATTY", "208.0.0 - Fatty"),+ ("PALE_FATTY", "208.1.0 - Pale Fatty"), ("FAT_SACK", "209.0.0 - Fat Sack"), ("DEATHS_HEAD", "212.0.0 - Death's Head"),+ ("MOMS_HAND", "213.0.0 - Mom's Hand"), ("DIP", "217.0.0 - Dip"), ("SQUIRT", "220.0.0 - Squirt"),+ ("SKINNY", "226.0.0 - Skinny"), ("BONY", "227.0.0 - Bony"), ("ONE_TOOTH", "234.0.0 - One Tooth"),+ ("GAPING_MAW", "235.0.0 - Gaping Maw"), ("RAGLING", "246.0.0 - Ragling"), ("NIGHT_CRAWLER", "255.0.0 - Night Crawler"),+ ("DART_FLY", "256.0.0 - Dart Fly"), ("FAT_BAT", "258.0.0 - Fat Bat"), ("IMP", "259.0.0 - Imp"),+ ("TUMOR", "229.0.0 - Tumor"), ("POKY", "44.0.0 - Poky"), ("STONE_GRIMACE", "42.0.0 - Stone Grimace"),+ ("CONJOINED_FATTY", "257.0.0 - Conjoined Fatty"), ("ROUND_WORM", "244.0.0 - Round Worm"),+ ("WALL_CREEP", "240.0.0 - Wall Creep"), ("MEMBRAIN", "57.0.0 - MemBrain"), ("LUMP", "56.0.0 - Lump"),+ ("FRED", "59.0.0 - Fred"), ("HOMUNCULUS", "228.0.0 - Homunculus"), ("BEGOTTEN", "251.0.0 - Begotten"),+ ("SQUIRT_DANK", "220.1.0 - Dank Squirt"), ("CHUBBER", "39.2.0 - Chubber"), ("FLAMING_HOPPER", "54.0.0 - Flaming Hopper"),+ ]+ for n, f in monsters:+ add(n, feet(ent(f)))+ # bosses+ bosses = [+ ("MONSTRO", "20.0.0 - Monstro"), ("LARRY", "19.0.0 - Larry Jr"), ("THE_HOLLOW", "19.1.0 - The Hollow"),+ ("DUKE", "67.0.0 - The Duke of Flies"), ("HUSK", "67.1.0 - The Husk"), ("GEMINI", "79.0.0 - Gemini"),+ ("GEMINI_BABY", "79.10.0 - Gemini Baby"), ("STEVEN", "79.1.0 - Steven"), ("STEVEN_BABY", "79.11.0 - Steven Baby"),+ ("BLIGHTED_OVUM", "79.2.0 - The Blighted Ovum"), ("UMBILICAL", "79.20.0 - Umbilical Cord"),+ ("GURDY", "36.0.0 - Gurdy"), ("GURDY_JR", "99.0.0 - Gurdy Jr"), ("PEEP", "68.0.0 - Peep"), ("BLOAT", "68.1.0 - The Bloat"),+ ("CHUB", "28.0.0 - Chub"), ("CARRION_QUEEN", "28.2.0 - The Carrion Queen"), ("LOKI", "69.0.0 - Loki"),+ ("LOKII", "69.1.0 - Lokii"), ("MOM_EYE", "45.0.0 - Mom"), ("MOMS_HEART", "78.0.0 - Mom's Heart"),+ ("IT_LIVES", "78.1.0 - It Lives"), ("FAMINE", "63.0.0 - Famine"), ("PESTILENCE", "64.0.0 - Pestilence"),+ ("WAR", "65.0.0 - War"), ("CONQUEST", "65.1.0 - Conquest"), ("DEATH", "66.0.0 - Death"), ("DEATH_HORSE", "66.20.0 - Death Horse"),+ ("PIN", "62.0.0 - Pin"), ("SCOLEX", "62.1.0 - Scolex"), ("FRAIL", "62.2.0 - The Frail"),+ ("MONSTRO2", "43.0.0 - Monstro II"), ("GISH", "43.1.0 - Gish"), ("MASK_OF_INFAMY", "97.0.0 - Mask of Infamy"),+ ("HEART_OF_INFAMY", "98.0.0 - Heart of Infamy"), ("WIDOW", "100.0.0 - Widow"), ("WRETCHED", "100.1.0 - The Wretched"),+ ("DADDY_LONG_LEGS", "101.0.0 - Daddy Long Legs"), ("TRIACHNID", "101.1.0 - Triachnid"),+ ("FISTULA_BIG", "71.0.0 - Fistula Big"), ("FISTULA_MED", "72.0.0 - Fistula Medium"), ("FISTULA_SMALL", "73.0.0 - Fistula Small"),+ ("TERATOMA_BIG", "71.1.0 - Teratoma Big"), ("BLASTOCYST_BIG", "74.0.0 - Blastocyst Big"),+ ("BLASTOCYST_MED", "75.0.0 - Blastocyst Medium"), ("BLASTOCYST_SMALL", "76.0.0 - Blastocyst Small"),+ ("SATAN", "84.0.0 - Satan"), ("SATAN_STOMP", "84.10.0 - Satan Stomp"), ("FALLEN", "81.0.0 - The Fallen"),+ ("KRAMPUS", "81.1.0 - Krampus"), ("ISAAC_BOSS", "102.0.0 - Isaac (final boss)"), ("BLUE_BABY", "102.1.0 - Blue Baby (final boss)"),+ ("HEADLESS_HORSEMAN", "82.0.0 - Headless Horseman Body"), ("HORSEMAN_HEAD", "83.0.0 - Headless Horsemans Head"),+ ("DINGLE", "261.0.0 - Dingle"), ("HAUNT", "260.0.0 - Haunt"), ("LIL_HAUNT", "260.10.0 - Lil' Haunt"),+ ("MEGA_FATTY", "264.0.0 - Mega Fatty"), ("CAGE", "265.0.0 - The Cage"), ("MAMA_GURDY", "266.0.0 - Mama Gurdy"),+ ("DARK_ONE", "267.0.0 - Dark One"), ("ADVERSARY", "268.0.0 - The Adversary"), ("POLYCEPHALUS", "269.0.0 - Polycephalus"),+ ("MR_FRED", "270.0.0 - Mr. Fred"), ("LAMB", "273.0.0 - The Lamb"), ("URIEL", "271.0.0 - Uriel"), ("GABRIEL", "272.0.0 - Gabriel"),+ ("GURGLING", "237.1.0 - Gurgling (boss)"), ("MEGA_MAW", "262.0.0 - Mega Maw"), ("GATE", "263.0.0 - The Gate"),+ ("SLOTH", "46.0.0 - Sloth"), ("LUST", "47.0.0 - Lust"), ("WRATH", "48.0.0 - Wrath"), ("GLUTTONY", "49.0.0 - Gluttony"),+ ("GREED", "50.0.0 - Greed"), ("ENVY", "51.0.0 - Envy"), ("ENVY_HALF", "51.10.0 - Envy (1-2)"), ("ENVY_QUARTER", "51.20.0 - Envy (1-4)"),+ ("PRIDE", "52.0.0 - Pride"), ("SUPER_SLOTH", "46.1.0 - Super Sloth"),+ ]+ for n, f in bosses:+ add(n, feet(ent(f)))+ # grid things+ poop = ent("1500.0.0 - Poop")+ add("POOP", [feet(poop.crop((0, int(poop.size[1] * k / 4), poop.size[0], poop.size[1])), dy=0) for k in range(4)])+ add("RED_POOP", feet(ent("1490.0.0 - Red Poop")))+ add("GOLDEN_POOP", feet(ent("1496.0.0 - Golden Poop")))+ add("CORNY_POOP", feet(ent("1495.0.0 - Chunky Poop")))+ add("TNT", feet(ent("1300.0.0 - TNT")))+ add("BLOCK", feet(ent("1900.0.0 - Block")))+ add("KEY_BLOCK", feet(ent("4000.0.0 - Key Block")))+ add("SPIKES", centred(ent("1930.0.0 - Spikes")))+ add("COBWEB", centred(ent("1940.0.0 - Cobweb")))+ add("PIT", centred(ent("3000.0.0 - Pit")))+ add("TRAPDOOR", centred(ent("9000.0.0 - Trap Door")))+ for st in ("generic", "cellar", "caves", "catacombs", "depths", "womb", "sheol", "cathedral",+ "secretroom", "scarredwomb"):+ sheet = bg("rocks_" + st)+ # three rocks, a metal block, a tinted rock, a bomb rock, a pot, rubble+ add("ROCKS_" + st.upper(), rock_cells(sheet, [(0, 0), (1, 0), (2, 0), (0, 1), (3, 2) if sheet.size[0] > 128 else (3, 2),+ (0, 2), (2, 1), (3, 0)]))+ add("MOM_FOOT", feet(A.mom_foot()))+ add("FIREPLACE", feet(ent("33.0.0 - Fire Place")))+ add("RED_FIREPLACE", feet(ent("33.1.0 - Red Fire Place")))+ add("BLUE_FIREPLACE", feet(ent("33.2.0 - Blue Fire Place")))+ add("PURPLE_FIREPLACE", feet(ent("33.3.0 - Purple Fire Place")))+ # pickups+ for n, f in [("HEART_FULL", "5.10.1 - Heart"), ("HEART_HALF", "5.10.2 - Heart (half)"), ("HEART_SOUL", "5.10.3 - Heart (soul)"),+ ("HEART_ETERNAL", "5.10.4 - Heart (eternal)"), ("HEART_DOUBLE", "5.10.5 - Heart (double)"),+ ("HEART_BLACK", "5.10.6 - Black Heart"), ("HEART_HALFSOUL", "5.10.8 - Heart (halfsoul)"),+ ("PENNY", "5.20.1 - Penny"), ("NICKEL", "5.20.2 - Nickel"), ("DIME", "5.20.3 - Dime"), ("LUCKY_PENNY", "5.20.5 - Lucky Penny"),+ ("KEY", "5.30.1 - Key"), ("GOLDEN_KEY", "5.30.2 - Golden Key"), ("KEY_RING", "5.30.3 - Key Ring"),+ ("BOMB_PICKUP", "5.40.1 - Bomb"), ("DOUBLE_BOMB", "5.40.2 - Double Bomb"), ("TROLL_BOMB", "5.40.3 - Troll Bomb"),+ ("GOLDEN_BOMB", "5.40.4 - Golden Bomb"),+ ("CHEST", "5.50.0 - Chest"), ("BOMB_CHEST", "5.51.0 - Bomb Chest"), ("SPIKED_CHEST", "5.52.0 - Spiked Chest"),+ ("ETERNAL_CHEST", "5.53.0 - Eternal Chest"), ("LOCKED_CHEST", "5.60.0 - Locked Chest"), ("RED_CHEST", "5.360.0 - Red Chest"),+ ("GRAB_BAG", "5.69.1 - Grab Bag"), ("BATTERY", "5.90.1 - Lil' Battery"), ("TROPHY", "5.370.0 - Trophy"),+ ("BIG_CHEST", "5.340.0 - Big Chest"), ("PRICE", "5.150.0 - Shop Item")]:+ add(n, feet(ent(f)))+ pills = ["5.70.1 - Blue Pill", "5.70.2 - White-Blue Pill", "5.70.3 - Orange Pill", "5.70.4 - White Pill",+ "5.70.5 - Dots-Red Pill", "5.70.6 - Pink-Red Pill", "5.70.7 - Blue-Cadetblue Pill", "5.70.8 - Yellow-Orange Pill",+ "5.70.9 - Dots-White Pill", "5.70.10 - White-Azure Pill", "5.70.11 - Black-Yellow Pill",+ "5.70.12 - White-Black Pill", "5.70.13 - White-Yellow Pill"]+ add("PILL", [feet(ent(p)) for p in pills])+ cards = sorted(glob.glob(os.path.join(REF, "br", "resources", "Entities", "5.300.*.png")),+ key=lambda p: int(os.path.basename(p).split(" ")[0].split(".")[2]))+ card_pics = {int(os.path.basename(p).split(" ")[0].split(".")[2]): p for p in cards}+ add("CARD", [feet(Image.open(card_pics[i]).convert("RGBA")) for i in range(1, 32)]) # the 22 tarot, playing cards, joker+ add("RUNE", [feet(Image.open(card_pics[i]).convert("RGBA")) for i in range(32, 42)])+ add("PEDESTAL", feet(ent("5.100.0 - Collectible").crop((0, 30, 32, 44))))+ # the items, by Rebirth's numbers (src/items.c gives the list)+ for n in R.ITEM_IDS:+ add(f"ITEM_{n}", feet(item_pic(n)))+ # machines, beggars, shopkeepers+ for n, f in [("SLOT_MACHINE", "6.1.0 - Slot Machine"), ("BLOOD_MACHINE", "6.2.0 - Blood Donation Machine"),+ ("FORTUNE_MACHINE", "6.3.0 - Fortune Telling Machine"), ("BEGGAR", "6.4.0 - Beggar"),+ ("DEVIL_BEGGAR", "6.5.0 - Devil Beggar"), ("KEY_MASTER", "6.7.0 - Key Master"), ("BOMB_BUM", "6.9.0 - Bomb Bum"),+ ("SHOPKEEPER", "17.0.0 - Keeper"), ("HANGING_KEEPER", "17.1.0 - Hanging Keeper"),+ ("BOMB", "4.0.0 - Bomb"), ("BIG_BOMB", "4.0.10 - Mr Mega Bomb"), ("TROLL_BOMB_LIT", "4.0.3 - Troll Bomb"),+ ("POISON_BOMB", "4.0.5 - Poison Bomb"), ("SMOKE", "1000.141.1 - Smoke Cloud")]:+ add(n, feet(ent(f)))+ # the HUD (drawn at the screen's size)+ hearts = [A.heart(k, h) for k, h in (("red", False), ("red", True), ("empty", False), ("soul", False), ("soul", True),+ ("black", False), ("black", True), ("eternal", False))]+ add("HUD_HEART", [Frame(h, 0, 0) for h in hearts])+ add("HUD_ICON", [Frame(A.art(A.ICONS[k], A.ICON_PAL), 0, 0) for k in ("coin", "bomb", "key")])+ icons = ["boss", "treasure", "shop", "secret", "curse", "arcade", "sacrifice", "library", "miniboss", "challenge", "chest"]+ add("MAP_ICON", [Frame(A.map_icon(k, (255, 255, 255)), 3, 3) for k in icons])+ # the doors, in the top wall; the game turns them for the others+ kinds = ["normal", "treasure", "boss", "secret", "shop", "curse", "devil", "angel", "arcade"]+ frames = []+ for k in kinds:+ for o in (True, False):+ d = scaled(A.door(k, o))+ frames.append(Frame(d, d.size[0] // 2, d.size[1] - 1))+ add("DOOR", frames)+ for k in kinds:+ side = []+ for o in (True, False):+ d = scaled(A.door(k, o).rotate(90, expand=True)) # (the left wall's: its floor side on the right)+ side.append(Frame(d, d.size[0] - 1, d.size[1] // 2))+ add("DOOR_SIDE_" + k.upper(), side)+++# ---------------------------------------------------------------- the rooms' walls and floors+BACKDROPS = [+ ("BASEMENT", "01_basement"), ("CELLAR", "02_cellar"), ("CAVES", "03_caves"), ("CATACOMBS", "04_catacombs"),+ ("DEPTHS", "05_depths"), ("NECROPOLIS", "06_necropolis"), ("WOMB", "07_womb"), ("UTERO", "08_utero"),+ ("SHEOL", "09_sheol"), ("CATHEDRAL", "10_cathedral"), ("CHEST", "11_chest"), ("DARKROOM", "12_darkroom"),+ ("SCARRED", "16_scarredwomb"), ("LIBRARY", "0a_library"), ("SHOP", "0b_shop"), ("ISAACS_ROOM", "0c_isaacsroom"),+ ("BARREN", "0d_barrenroom"), ("ARCADE", "0e_arcade"), ("SECRET", "0f_secretroom"), ("SACRIFICE", "0g_sacrificeroom"),+]+++def encode_backdrop(name):+ im = scaled(bg(name)).convert("RGB") # 234 x 156 -> 180 x 120+ assert im.size == (180, 120), im.size+ q = im.quantize(colors=256, method=Image.Quantize.MEDIANCUT, dither=Image.Dither.NONE)+ pal = q.getpalette()[:256 * 3]+ n = len(pal) // 3+ head = struct.pack("<4H", im.size[0], im.size[1], n, 0)+ cols = b"".join(struct.pack("<H", rgb565(*pal[3 * i:3 * i + 3])) for i in range(n))+ out = head + cols + q.tobytes()+ return out + bytes(-len(out) % 4)+++# ---------------------------------------------------------------- the font+def encode_font(path, size, first=32, last=126, caps=True):+ f = ImageFont.truetype(path, size)+ asc, desc = f.getmetrics()+ glyphs, bits = [], bytearray()+ for c in range(first, last + 1):+ ch = chr(c)+ im = Image.new("L", (size * 3, size * 3), 0)+ ImageDraw.Draw(im).text((size, size), ch, font=f, fill=255)+ im = im.point(lambda v: 255 if v > 127 else 0)+ bb = im.getbbox()+ adv = round(f.getlength(ch))+ if not bb:+ glyphs.append((adv, 0, 0, 0, 0, len(bits)))+ continue+ g = im.crop(bb)+ w, h = g.size+ off = len(bits)+ stride = (w + 7) // 8+ gp = g.load()+ for y in range(h):+ row = bytearray(stride)+ for x in range(w):+ if gp[x, y]:+ row[x >> 3] |= 0x80 >> (x & 7)+ bits += row+ glyphs.append((adv, w, h, bb[0] - size, bb[1] - size, off))+ out = bytearray([asc + desc, first, last - first + 1, 1 if caps else 0])+ for adv, w, h, ox, oy, off in glyphs:+ out += struct.pack("<BBBbbBH", adv, w, h, ox, oy, 0, off)+ out += bits+ return bytes(out) + bytes(-len(out) % 4)+++# ---------------------------------------------------------------- writing+def table(entries):+ out, offs = bytearray(), []+ base = 4 + 4 * len(entries)+ for e in entries:+ offs.append(base + len(out))+ out += e+ out += bytes(-len(out) % 4)+ return struct.pack("<I", len(entries)) + b"".join(struct.pack("<I", o) for o in offs) + bytes(out)+++def main():+ global REF+ if len(sys.argv) != 2:+ sys.exit(__doc__)+ REF = sys.argv[1]+ sprites()+ frames, spr = [], bytearray(struct.pack("<I", len(SPRITES)))+ for name, fr in SPRITES:+ spr += struct.pack("<HH", len(frames), len(fr))+ frames += [encode_frame(f) for f in fr]+ font = os.path.join(REF, "fonts", "upheavtt.ttf")+ rooms = R.encode()+ secs = {"FRAMES": table(frames), "SPRITES": bytes(spr), "BACKDROPS": table([encode_backdrop(f) for _, f in BACKDROPS]),+ "FONTS": table([encode_font(font, 10)]), "ROOMS": rooms}+ head = b"ISA1" + struct.pack("<I", len(SECTIONS))+ base = len(head) + 4 * len(SECTIONS)+ offs, body = [], bytearray()+ for s in SECTIONS:+ offs.append(base + len(body))+ body += secs[s]+ body += bytes(-len(body) % 4)+ data = head + b"".join(struct.pack("<I", o) for o in offs) + bytes(body)+ open(OUT_BIN, "wb").write(data)+ with open(OUT_H, "w") as h:+ h.write("/* Generated by tools/pack.py: what data.bin holds. */\n#ifndef IS_DATA_H\n#define IS_DATA_H\n\n")+ for i, s in enumerate(SECTIONS):+ h.write(f"#define SEC_{s} {i}\n")+ h.write("\nenum {\n")+ for name, _ in SPRITES:+ h.write(f" SPR_{name},\n")+ h.write(" SPR_COUNT\n};\n\nenum {\n")+ for name, _ in BACKDROPS:+ h.write(f" BD_{name},\n")+ h.write(" BD_COUNT\n};\n\n#define FONT_UPHEAVAL 0\n")+ h.write(R.header())+ h.write("\n#endif\n")+ sizes = {s: len(secs[s]) for s in SECTIONS}+ print(f"data.bin: {len(data)} bytes, {len(SPRITES)} sprites, {len(frames)} frames", sizes)+++if __name__ == "__main__":+ main()diff --git a/games/isaac/tools/rooms.py b/games/isaac/tools/rooms.pynew file mode 100644index 00000000..8d9ba410--- /dev/null+++ b/games/isaac/tools/rooms.py@@ -0,0 +1,501 @@+"""The rooms of a floor: 13 x 7 cells each, in the game's style (its rocks, poop, pits and spikes, its monsters+standing where the game's own rooms put them), and the monsters each chapter fills them with.++A layout's cells: '.' floor, 'r' rock, 'm' metal block, 't' tinted rock, 'b' bomb rock, 'p' poop, 'q' corny poop,+'o' pit, 's' spikes, 'x' TNT, 'k' key block, 'w' cobweb, 'f' fire place, 'F' red fire place, '1' to '4' a monster+of the room's group (1: a walker, 2: a flier, 3: one that stays and shoots, 4: a tough one), '*' a pickup, 'c' a+chest, 'I' an item on its pedestal, and capitals a layout names for itself.++The packed rooms (ROOMS in data.bin): kind, chapters (bits: 1 Basement, 2 Caves, 4 Depths, 8 Womb, 16 Sheol), weight,+group (for a normal room: 0, its group picked when the room is made), then 7 x 13 cells: CELL_* below the+monsters, MON_* + CELL_MON above."""+import struct++# Rebirth's numbers of the items in the game (src/items.c holds what each does)+ITEM_IDS = [+ 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,+ 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 63, 64,+ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,+ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118,+ 119, 120, 121, 122, 123, 124, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 140, 142, 143,+ 144, 145, 146, 147, 149, 150, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 169,+ 170, 171, 173, 174, 175, 176, 177, 178, 179, 182, 183, 184, 185, 186, 188, 189, 190, 192, 193, 194, 196, 197,+ 199, 201, 245, 261,+]++CELLS = ["EMPTY", "ROCK", "BLOCK", "TINTED", "BOMB_ROCK", "POOP", "CORNY_POOP", "PIT", "SPIKES", "TNT", "KEY_BLOCK",+ "COBWEB", "FIRE", "RED_FIRE", "SLOT1", "SLOT2", "SLOT3", "SLOT4", "PICKUP", "CHEST", "ITEM", "RED_POOP",+ "GOLDEN_POOP"]+CELL_MON = 64 # a monster: CELL_MON + MON_*++CHARS = {".": "EMPTY", "r": "ROCK", "m": "BLOCK", "t": "TINTED", "b": "BOMB_ROCK", "p": "POOP", "q": "CORNY_POOP",+ "o": "PIT", "s": "SPIKES", "x": "TNT", "k": "KEY_BLOCK", "w": "COBWEB", "f": "FIRE", "F": "RED_FIRE",+ "1": "SLOT1", "2": "SLOT2", "3": "SLOT3", "4": "SLOT4", "*": "PICKUP", "c": "CHEST", "I": "ITEM"}++# the monsters (src/monsters.c says how each one moves)+MONSTERS = [+ "GAPER", "FROWNING_GAPER", "GUSHER", "PACER", "HORF", "FLY", "POOTER", "SUPER_POOTER", "CLOTTY", "CLOT", "IBLOB",+ "MULLIGAN", "ATTACK_FLY", "MAGGOT", "HIVE", "CHARGER", "GLOBIN", "BOOM_FLY", "RED_BOOM_FLY", "MAW", "RED_MAW",+ "HOST", "RED_HOST", "HOPPER", "TRITE", "BOIL", "SPITTY", "BRAIN", "LEAPER", "MR_MAW", "BABY", "ANGELIC_BABY",+ "VIS", "GUTS", "KNIGHT", "SELFLESS_KNIGHT", "LEECH", "KAMIKAZE_LEECH", "PARA_BITE", "EYE", "BLOODSHOT_EYE",+ "SUCKER", "SPIT", "EMBRYO", "MOTER", "SPIDER", "KEEPER", "GURGLE", "WALKING_BOIL", "BUTTLICKER", "HANGER",+ "SWARMER", "HEART", "MASK", "BIG_SPIDER", "NEST", "BABY_LONG_LEGS", "FATTY", "PALE_FATTY", "FAT_SACK",+ "DEATHS_HEAD", "MOMS_HAND", "DIP", "SQUIRT", "SKINNY", "BONY", "ONE_TOOTH", "GAPING_MAW", "RAGLING",+ "NIGHT_CRAWLER", "DART_FLY", "FAT_BAT", "IMP", "TUMOR", "POKY", "STONE_GRIMACE", "CONJOINED_FATTY",+ "ROUND_WORM", "WALL_CREEP", "MEMBRAIN", "LUMP", "FRED", "HOMUNCULUS", "BEGOTTEN", "CHUBBER", "FLAMING_HOPPER",+ "BLUE_FLY",+ # bosses and their parts+ "MONSTRO", "LARRY", "THE_HOLLOW", "DUKE", "HUSK", "GEMINI", "GEMINI_BABY", "STEVEN", "STEVEN_BABY",+ "BLIGHTED_OVUM", "GURDY", "GURDY_JR", "PEEP", "BLOAT", "CHUB", "CARRION_QUEEN", "LOKI", "LOKII", "MOM",+ "MOMS_HEART", "IT_LIVES", "FAMINE", "PESTILENCE", "WAR", "CONQUEST", "DEATH", "PIN", "SCOLEX", "FRAIL",+ "MONSTRO2", "GISH", "MASK_OF_INFAMY", "HEART_OF_INFAMY", "WIDOW", "WRETCHED", "DADDY_LONG_LEGS", "TRIACHNID",+ "FISTULA", "TERATOMA", "BLASTOCYST", "SATAN", "FALLEN", "KRAMPUS", "ISAAC", "HEADLESS_HORSEMAN", "DINGLE",+ "HAUNT", "LIL_HAUNT", "MEGA_FATTY", "MAMA_GURDY", "DARK_ONE", "POLYCEPHALUS", "MR_FRED", "GURGLING",+ "SLOTH", "LUST", "WRATH", "GLUTTONY", "GREED", "ENVY", "PRIDE", "MOM_HAND", "MOM_FOOT", "SATAN_STOMP",+ "DEATH_SCYTHE",+]+MON = {n: i for i, n in enumerate(MONSTERS)}++KINDS = ["NORMAL", "START", "TREASURE", "SHOP", "BOSS", "SECRET", "SUPERSECRET", "CURSE", "ARCADE", "SACRIFICE",+ "LIBRARY", "MINIBOSS", "CHALLENGE", "DEVIL", "ANGEL"]++# ---------------------------------------------------------------- monster groups: what each chapter's rooms hold+# (chapters, walker, flier, shooter, tough): a slot left None stays empty+GROUPS = [+ # the Basement and the Cellar+ (1, "GAPER", "FLY", "HORF", "GLOBIN"),+ (1, "FROWNING_GAPER", "ATTACK_FLY", "POOTER", "MULLIGAN"),+ (1, "HOPPER", "FLY", "CLOTTY", "BOOM_FLY"),+ (1, "GAPER", "POOTER", "HOST", "GUSHER"),+ (1, "SPIDER", "ATTACK_FLY", "HORF", "BIG_SPIDER"),+ (1, "MAGGOT", "FLY", "CLOT", "CHARGER"),+ (1, "DIP", "POOTER", "SQUIRT", "SQUIRT"),+ (1, "PACER", "MOTER", "MAW", "GLOBIN"),+ (1, "GUSHER", "SUCKER", "HOST", "HIVE"),+ (1, "GAPER", "ATTACK_FLY", "CLOTTY", "MULLIGAN"),+ (1, "HOPPER", "POOTER", "HORF", "BIG_SPIDER"),+ (1, "SPIDER", "FLY", "HOST", "BOOM_FLY"),+ (1, "FROWNING_GAPER", "FLY", "MAW", "FATTY"),+ # the Caves and the Catacombs+ (2, "HOPPER", "SUCKER", "HOST", "KNIGHT"),+ (2, "TRITE", "MOTER", "MAW", "LEAPER"),+ (2, "CHARGER", "BOOM_FLY", "CLOTTY", "FATTY"),+ (2, "GLOBIN", "ATTACK_FLY", "BOIL", "MR_MAW"),+ (2, "SPIDER", "DART_FLY", "SPITTY", "BIG_SPIDER"),+ (2, "SKINNY", "POOTER", "HORF", "WALKING_BOIL"),+ (2, "GURGLE", "SWARMER", "IBLOB", "BUTTLICKER"),+ (2, "BONY", "FAT_BAT", "RED_HOST", "KNIGHT"),+ (2, "NIGHT_CRAWLER", "SUCKER", "TUMOR", "HIVE"),+ (2, "GAPER", "SUPER_POOTER", "CLOT", "FAT_SACK"),+ (2, "TRITE", "BOOM_FLY", "SPITTY", "PALE_FATTY"),+ (2, "RAGLING", "MOTER", "HOST", "CHUBBER"),+ # the Depths and the Necropolis+ (4, "KNIGHT", "RED_BOOM_FLY", "RED_MAW", "SELFLESS_KNIGHT"),+ (4, "BONY", "FAT_BAT", "RED_HOST", "VIS"),+ (4, "BABY", "MOTER", "EYE", "FRED"),+ (4, "LEAPER", "SWARMER", "BRAIN", "HANGER"),+ (4, "IMP", "SUCKER", "BLOODSHOT_EYE", "GAPING_MAW"),+ (4, "DEATHS_HEAD", "RED_BOOM_FLY", "STONE_GRIMACE", "HOMUNCULUS"),+ (4, "GUTS", "ATTACK_FLY", "MR_MAW", "BEGOTTEN"),+ (4, "GLOBIN", "DART_FLY", "ONE_TOOTH", "KNIGHT"),+ (4, "SKINNY", "MOTER", "RED_HOST", "PALE_FATTY"),+ (4, "RAGLING", "SWARMER", "SPITTY", "CONJOINED_FATTY"),+ # the Womb and Utero+ (8, "GUTS", "SWARMER", "EYE", None),+ (8, "PARA_BITE", "LEECH", "BLOODSHOT_EYE", "FRED"),+ (8, "KAMIKAZE_LEECH", "EMBRYO", "LUMP", "MEMBRAIN"),+ (8, "BABY", "HEART", "VIS", "WALL_CREEP"),+ (8, "GURGLE", "LEECH", "NEST", "CONJOINED_FATTY"),+ (8, "ROUND_WORM", "SUCKER", "EYE", "VIS"),+ (8, "GUTS", "BOOM_FLY", "BLOODSHOT_EYE", "BEGOTTEN"),+ (8, "PARA_BITE", "SWARMER", "LUMP", "FAT_SACK"),+ (8, "BABY", "LEECH", "TUMOR", "HOMUNCULUS"),+ # Sheol and the Cathedral+ (16, "IMP", "RED_BOOM_FLY", "ONE_TOOTH", "BEGOTTEN"),+ (16, "BABY", "FAT_BAT", "BLOODSHOT_EYE", "KNIGHT"),+ (16, "ANGELIC_BABY", "SWARMER", "EYE", "SELFLESS_KNIGHT"),+ (16, "DEATHS_HEAD", "ATTACK_FLY", "STONE_GRIMACE", "VIS"),+ (16, "BONY", "LEECH", "RED_HOST", "FRED"),+]++# ---------------------------------------------------------------- layouts+L = [] # (kind, chapters, weight, rows, own monsters)+++def room(rows, kind="NORMAL", chapters=31, weight=1, mons=None):+ assert len(rows) == 7 and all(len(r) == 13 for r in rows), rows+ L.append((kind, chapters, weight, rows, mons or {}))+++# the starting room: empty (the game shows its keys on the floor)+room(["............."] * 7, "START")++# ---- normal rooms+room([".............",+ "..1.......1..",+ ".............",+ "......r......",+ ".............",+ "..1.......1..",+ "............."], weight=3)+room([".............",+ ".r.........r.",+ "...2.....2...",+ ".............",+ "...1.....1...",+ ".r.........r.",+ "............."], weight=3)+room(["r...........r",+ ".............",+ "..rr..3..rr..",+ "..r.......r..",+ "..rr.....rr..",+ ".......1.....",+ "r...........r"], weight=2)+room([".............",+ "..ppp...ppp..",+ "..p1.....1p..",+ ".............",+ "..p.......p..",+ "..ppp.2.ppp..",+ "............."], weight=2)+room([".............",+ ".1.........1.",+ "....rrrrr....",+ "....r.4.r....",+ "....r...r....",+ ".1.........1.",+ "............."], weight=2)+room(["......t......",+ ".2...........",+ "...r.....r...",+ "......3......",+ "...r.....r...",+ "...........2.",+ "............."], weight=2)+room(["ooo.......ooo",+ "o.....1.....o",+ "...ooo.ooo...",+ ".3.o.....o.3.",+ "...ooo.ooo...",+ "o...........o",+ "ooo.......ooo"], chapters=30)+room([".............",+ ".mmm.....mmm.",+ ".m.........m.",+ "......4......",+ ".m.........m.",+ ".mmm.....mmm.",+ "............."], chapters=30)+room([".r.r.r.r.r.r.",+ ".............",+ "..1.......1..",+ "......2......",+ "..1.......1..",+ ".............",+ ".r.r.r.r.r.r."], weight=2)+room([".............",+ ".............",+ "..s.s.s.s.s..",+ "..s.s.3.s.s..",+ "..s.s.s.s.s..",+ ".1.........1.",+ "............."])+room(["......o......",+ "......o......",+ "..1...o...1..",+ "ooo.......ooo",+ "..2...o...2..",+ "......o......",+ "......o......"], chapters=30)+room([".............",+ ".....rrr.....",+ "..2..r*r..2..",+ ".....rbr.....",+ ".............",+ "..1.......1..",+ "............."], weight=2)+room(["pp.........pp",+ "p...........p",+ "...1.....1...",+ "......q......",+ "...1.....1...",+ "p...........p",+ "pp.........pp"])+room([".............",+ ".............",+ "...f.....f...",+ "......1......",+ "...f.....f...",+ ".............",+ "............."], weight=2)+room(["2...........2",+ ".............",+ ".............",+ "......4......",+ ".............",+ ".............",+ "2...........2"], weight=2)+room(["r.r.r...r.r.r",+ ".............",+ "r.....3.....r",+ ".............",+ "r...1...1...r",+ ".............",+ "r.r.r...r.r.r"])+room([".............",+ ".rr.......rr.",+ ".r..1...1..r.",+ ".....mmm.....",+ ".r..1...1..r.",+ ".rr.......rr.",+ "............."], weight=2)+room(["......r......",+ "......r......",+ "...3..r..3...",+ ".............",+ "...2..r..2...",+ "......r......",+ "......r......"])+room([".............",+ ".............",+ ".1..1..1..1..",+ ".............",+ ".............",+ ".............",+ "............."])+room([".............",+ "...rrrrrrr...",+ "...r.....r...",+ "...r..4..r...",+ "...r.....r...",+ "...rrr.rrr...",+ "............."])+room(["o.o.o.o.o.o.o",+ ".............",+ "o.....2.....o",+ ".............",+ "o...2...2...o",+ ".............",+ "o.o.o.o.o.o.o"], chapters=30)+room([".............",+ "..x.......x..",+ ".............",+ "...1.....1...",+ ".............",+ "..x.......x..",+ "............."])+room(["mmmmm...mmmmm",+ "m...........m",+ "m..1.....1..m",+ ".............",+ "m..3.....3..m",+ "m...........m",+ "mmmmm...mmmmm"], chapters=30)+room([".............",+ ".s.........s.",+ "..s...1...s..",+ "...s.....s...",+ "..s...1...s..",+ ".s.........s.",+ "............."], chapters=30)+room(["..ppp...ppp..",+ ".............",+ "p.....2.....p",+ "p...........p",+ "p.....2.....p",+ ".............",+ "..ppp...ppp.."])+room([".............",+ "....rr.rr....",+ "...r.....r...",+ "...r..c..r...",+ "...r.....r...",+ "....rr.rr....",+ ".1.........1."])+room([".............",+ ".1.r.....r.1.",+ "...r.....r...",+ "...r..3..r...",+ "...r.....r...",+ ".1.r.....r.1.",+ "............."])+room(["w...........w",+ ".............",+ "..2.......2..",+ ".....www.....",+ "..2.......2..",+ ".............",+ "w...........w"], chapters=6)+room([".............",+ "......1......",+ ".............",+ ".1....t....1.",+ ".............",+ "......1......",+ "............."], weight=2)+room(["...rr...rr...",+ "...r.....r...",+ ".3.........3.",+ ".............",+ ".............",+ "...r..1..r...",+ "...rr...rr..."])+room([".............",+ "..r.r.r.r.r..",+ ".............",+ "..r.r4r.r.r..",+ ".............",+ "..r.r.r.r.r..",+ "............."])+room(["oooo.....oooo",+ "oooo..2..oooo",+ ".............",+ ".1.........1.",+ ".............",+ "oooo.....oooo",+ "oooo.....oooo"], chapters=30)+room(["............."] * 3 + ["......*......"] + ["............."] * 3) # an empty room, a pickup in it+room([".............",+ ".....mmm.....",+ "..1..m.m..1..",+ ".....m*m.....",+ "..2..m.m..2..",+ ".....m.m.....",+ "............."], chapters=30)+room(["F...........F",+ ".............",+ "...1.....1...",+ ".............",+ "...1.....1...",+ ".............",+ "F...........F"], chapters=28)+room([".............",+ ".f.........f.",+ ".............",+ "...r..2..r...",+ ".............",+ ".f.........f.",+ "............."])+room([".............",+ "..2.......2..",+ ".............",+ ".......4.....",+ ".............",+ "..2.......2..",+ "............."], weight=2)++# ---- special rooms+room([".............",+ ".............",+ ".............",+ "......I......",+ ".............",+ ".............",+ "............."], "TREASURE", weight=4)+room([".............",+ ".............",+ ".....r.r.....",+ "......I......",+ ".....r.r.....",+ ".............",+ "............."], "TREASURE", weight=2)+room([".............",+ ".............",+ "....sssss....",+ "....s.I.s....",+ "....sssss....",+ ".............",+ "............."], "TREASURE", chapters=30)+room([".............",+ "....rrrrr....",+ "...r.....r...",+ "...r..I..r...",+ "...r.....r...",+ "....rr.rr....",+ "............."], "TREASURE")+room([".............",+ ".............",+ "....oooo.....",+ "....oIoo.....",+ "....oooo.....",+ ".............",+ "............."], "TREASURE", chapters=30)+room([".............",+ ".............",+ ".............",+ ".....*.*.....",+ ".............",+ ".............",+ "............."], "SECRET", weight=3)+room([".............",+ ".............",+ ".............",+ "......I......",+ ".............",+ ".............",+ "............."], "SECRET", weight=2)+room([".............",+ "..*.......*..",+ ".............",+ "......c......",+ ".............",+ "..*.......*..",+ "............."], "SECRET", weight=2)+room([".............",+ ".............",+ ".............",+ "......I......",+ ".............",+ ".............",+ "............."], "SUPERSECRET")+room([".............",+ ".............",+ ".............",+ ".....c.c.....",+ ".............",+ ".............",+ "............."], "CURSE", weight=2)+room([".............",+ ".............",+ "......s......",+ ".....sIs.....",+ "......s......",+ ".............",+ "............."], "CURSE")+room([".............",+ "..1.......1..",+ ".............",+ "......c......",+ ".............",+ "..1.......1..",+ "............."], "CURSE")+for k in ("SHOP", "BOSS", "ARCADE", "SACRIFICE", "LIBRARY", "MINIBOSS", "CHALLENGE", "DEVIL", "ANGEL"):+ room(["............."] * 7, k) # (laid out by the game itself)+++def encode():+ out = bytearray(struct.pack("<I", len(L)))+ for kind, chapters, weight, rows, mons in L:+ out += bytes([KINDS.index(kind), chapters, weight, 0])+ for r in rows:+ for ch in r:+ if ch in mons:+ out.append(CELL_MON + MON[mons[ch]])+ else:+ out.append(CELLS.index(CHARS[ch]))+ return bytes(out)+++def header():+ s = "\nenum {\n" + "".join(f" CELL_{c},\n" for c in CELLS) + " CELL_COUNT\n};\n"+ s += f"#define CELL_MON {CELL_MON}\n\nenum {{\n" + "".join(f" MON_{m},\n" for m in MONSTERS) + " MON_COUNT\n};\n"+ s += "\nenum {\n" + "".join(f" ROOM_{k},\n" for k in KINDS) + " ROOM_KIND_COUNT\n};\n"+ s += "\n/* the monster groups: chapters, then the monster of each slot (MON_COUNT: none) */\n"+ s += f"#define GROUP_COUNT {len(GROUPS)}\n"+ s += "#define GROUP_TABLE {"+ for g in GROUPS:+ s += "{" + str(g[0]) + "," + ",".join(f"MON_{m}" if m in MON else "MON_COUNT" for m in g[1:]) + "},"+ s += "}\n"+ s += f"#define ITEM_SPRITE_COUNT {len(ITEM_IDS)}\n#define ITEM_ID_TABLE {{" + ",".join(map(str, ITEM_IDS)) + "}\n"+ return s