Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Sonic: rings, monitors, the HUD, cards, signposts, lampposts, springs and spikes

Mason3634c3ddcc3229b
diff --git a/games/sonic/Makefile b/games/sonic/Makefile
index b1e7e0ac..5a34a21d 100644
--- a/games/sonic/Makefile
+++ b/games/sonic/Makefile
@@ -23,7 +23,7 @@ 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 += -O2 -Wall -Wextra -Wno-unused-parameter -fno-math-errno -fsingle-precision-constant -fwrapv
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
@@ -59,8 +59,8 @@ $(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_FLAGS = -std=gnu11 -O1 -g -fwrapv -Wall -Wextra -Wno-unused-parameter -Isrc -DHOST -DDATA_BIN='"src/data.bin"'
+HOST_SAN = -fsanitize=address,undefined -fno-sanitize-recover=undefined -fno-sanitize=shift-base
HOST_SRC = $(filter-out src/plat_eadk.c src/main.c,$(wildcard src/*.c))
host: build/play
build/play: $(HOST_SRC) src/main.c tests/play.c src/data.S src/data.bin $(HEADERS)
diff --git a/games/sonic/src/hud.c b/games/sonic/src/hud.c
new file mode 100644
index 00000000..20086b7c
--- /dev/null
+++ b/games/sonic/src/hud.c
@@ -0,0 +1,116 @@
+/* The HUD: SCORE, TIME and RINGS (the words flash red when there are no rings or the time nears
+ * ten minutes) and the lives counter. Their numbers are art: each digit's two tiles are put into
+ * video memory where the HUD's sprite shows them, as the original writes them there in VBlank.
+ * The clock runs here too: at 9:59 the time is over. */
+#include "s1.h"
+#include "game.h"
+
+extern const uint8_t vdp_blank_pattern[32];
+#define ART(a) ((const uint8_t *)DATA(D_TILES) + (uint32_t)DATAW(D_ARTS)[(a) * 2] * 32)
+
+void HUD(Obj *o) {
+ if (o->routine == 0) {
+ o->routine = 2;
+ o->x.w.i = 0x80 + 0x10;
+ obScreenY(o) = 0x80 + 0x88;
+ o->map = map_get(MAP_HUD);
+ o->gfx = ArtTile_HUD;
+ o->render = 0;
+ o->priority = 0;
+ }
+ if (V.rings) {
+ o->frame = 0;
+ DisplaySprite(o);
+ return;
+ }
+ uint8_t f = 0;
+ if (!(V.framecount & 8)) {
+ f = 1;
+ if (V.timemin == 9) f += 2;
+ }
+ o->frame = f;
+ DisplaySprite(o);
+}
+
+/* an 8x16 digit (tile index d of the HUD numbers, and the next) at tile t */
+static void put16(int t, int d) {
+ const uint8_t *a = ART(ART_Art_Hud);
+ if (d < 0) vram[t] = vram[t + 1] = vdp_blank_pattern;
+ else vram[t] = a + d * 32, vram[t + 1] = a + (d + 1) * 32;
+}
+
+static const uint32_t powers[6] = {100000, 10000, 1000, 100, 10, 1};
+
+/* n digits of v at tile t, two tiles each; lead: leading zeros shown */
+static void digits16(int t, uint32_t v, int first, int n, bool lead, bool clear) {
+ bool started = lead;
+ for (int i = 0; i < n; i++) {
+ uint32_t p = powers[first + i];
+ int d = 0;
+ while (v >= p) v -= p, d++;
+ if (d) started = true;
+ if (started) put16(t + i * 2, d * 2);
+ else if (clear) put16(t + i * 2, -1);
+ }
+}
+
+static void Hud_Lives(void) {
+ const uint8_t *a = ART(ART_Art_LivesNums);
+ uint32_t v = V.lives;
+ int tens = 0;
+ while (v >= 10) v -= 10, tens++;
+ vram[ArtTile_Lives_Counter_Num] = tens ? a + tens * 32 : vdp_blank_pattern;
+ vram[ArtTile_Lives_Counter_Num + 2] = a + v * 32;
+}
+
+void Hud_Base(void) {
+ Hud_Lives();
+ static const int8_t base[15] = {0x16, -1, -1, -1, -1, -1, -1, 0, 0, 0x14, 0, 0, -1, -1, 0};
+ for (int i = 0; i < 15; i++) put16(ArtTile_HUDScore_E + i * 2, base[i]);
+}
+
+static void Hud_ResetRings(void) {
+ static const int8_t base[3] = {-1, -1, 0};
+ for (int i = 0; i < 3; i++) put16(ArtTile_HUDRings + i * 2, base[i]);
+}
+
+void HUD_Update(void) {
+ if (V.f_scorecount) {
+ V.f_scorecount = 0;
+ digits16(ArtTile_HUDScore, V.score, 0, 6, false, false);
+ }
+ if (V.f_ringcount) {
+ if (V.f_ringcount & 0x80) Hud_ResetRings();
+ V.f_ringcount = 0;
+ digits16(ArtTile_HUDRings, V.rings, 3, 3, false, false);
+ }
+ if (V.f_timecount && !V.f_pause) {
+ if (V.timemin == 9 && V.timesec == 59 && V.timecent == 59) {
+ V.f_timecount = 0;
+ KillSonic(player, player);
+ V.f_timeover = 1;
+ return;
+ }
+ if (++V.timecent >= 60) {
+ V.timecent = 0;
+ if (++V.timesec >= 60) {
+ V.timesec = 0;
+ if (++V.timemin >= 9) V.timemin = 9;
+ }
+ digits16(ArtTile_HUDTimeMins, V.timemin, 5, 1, true, false);
+ digits16(ArtTile_HUDTimeSecs, V.timesec, 4, 2, true, false);
+ }
+ }
+ if (V.f_lifecount) {
+ V.f_lifecount = 0;
+ Hud_Lives();
+ }
+ if (V.f_endactbonus) {
+ V.f_endactbonus = 0;
+ digits16(ArtTile_Bonuses, V.timebonus, 2, 4, false, true);
+ digits16(ArtTile_Bonuses + 8, V.ringbonus, 2, 4, false, true);
+ }
+}
+
+/* the continue screen's countdown */
+void ContScrCounter(uint32_t v) { digits16(ArtTile_Continue_Number, v, 4, 2, true, false); }
diff --git a/games/sonic/src/main.c b/games/sonic/src/main.c
index b8ae099d..dad425a9 100644
--- a/games/sonic/src/main.c
+++ b/games/sonic/src/main.c
@@ -22,19 +22,12 @@ const uint8_t *ani_get(int id) { return DATA(((const uint32_t *)DATA(D_ANIS))[id
/* ------------------------------------------------------------------ numbers */
uint32_t RandomNumber(void) {
- uint32_t d1 = V.random;
- if (!d1) d1 = 0x2A6D365A;
- uint32_t d0 = d1;
- d1 = (d1 << 2) + d0;
- d1 = (d1 << 3) + d0;
- uint16_t lo = (uint16_t)((d1 & 0xFFFF) + (d1 >> 16));
- d1 = (d1 & 0xFFFF0000u) | lo;
- d1 = (d1 << 16) | (d1 >> 16);
- d1 = (d1 & 0xFFFF0000u) | lo;
- d1 = (d1 << 16) | (d1 >> 16);
- V.random = d1;
- /* (d0: its low word the sum, its high word the original's d0 high word) */
- return (d0 & 0xFFFF0000u) | lo;
+ uint32_t seed = V.random ? V.random : 0x2A6D365A;
+ uint32_t m = seed * 41;
+ uint16_t lo = (uint16_t)(m + (m >> 16));
+ V.random = (uint32_t)lo << 16 | (m & 0xFFFF);
+ /* (the original's d0: the old seed's high word and the new number) */
+ return (seed & 0xFFFF0000u) | lo;
}
int16_t CalcSine(uint8_t angle, int16_t *cos) {
diff --git a/games/sonic/src/obj.c b/games/sonic/src/obj.c
index cafc5606..d8c96ac4 100644
--- a/games/sonic/src/obj.c
+++ b/games/sonic/src/obj.c
@@ -6,11 +6,14 @@
struct spritequeue_s spritequeue[8];
+int obj_d7; /* the objects left to run after this one (the original's d7, which some objects read) */
+
void ExecuteObjects(void) {
int n = NOBJ;
if (player->routine >= 6) n = NRESERVED;
for (int i = 0; i < n; i++) {
Obj *o = &objs[i];
+ obj_d7 = n - 1 - i;
if (o->id && obj_index[o->id]) obj_index[o->id](o);
}
if (n == NOBJ) return;
diff --git a/games/sonic/src/obj_cards.c b/games/sonic/src/obj_cards.c
new file mode 100644
index 00000000..fd852150
--- /dev/null
+++ b/games/sonic/src/obj_cards.c
@@ -0,0 +1,423 @@
+/* The cards: the zone's name sliding in before an act, GAME OVER and TIME OVER, and the tally
+ * after an act (time and ring bonuses counted into the score, then the next act in the original's
+ * order). The signpost that ends an act, spinning when passed, and the lampposts that keep where
+ * Sonic restarts. */
+#include "s1.h"
+#include "game.h"
+#include "objs.h"
+
+#define card_mainX 0x30
+#define card_finalX 0x32
+
+/* x towards target by step a frame; on screen: shown */
+static bool card_move(Obj *o, int16_t target, int16_t step) {
+ if (target != o->x.w.i) o->x.w.i = (int16_t)(o->x.w.i + (target > o->x.w.i ? step : -step));
+ return target == o->x.w.i;
+}
+static void card_show(Obj *o) {
+ if (o->x.w.i >= 0 && o->x.w.i < 0x80 + 320 + 64) DisplaySprite(o);
+}
+
+void TitleCard(Obj *o) {
+ static const int16_t con[7][8] = {
+ {0x000, 0x120, -0x104, 0x13C, 0x414, 0x154, 0x214, 0x154},
+ {0x000, 0x120, -0x10C, 0x134, 0x40C, 0x14C, 0x20C, 0x14C},
+ {0x000, 0x120, -0x120, 0x120, 0x3F8, 0x138, 0x1F8, 0x138},
+ {0x000, 0x120, -0x104, 0x13C, 0x414, 0x154, 0x214, 0x154},
+ {0x000, 0x120, -0x0FC, 0x144, 0x41C, 0x15C, 0x21C, 0x15C},
+ {0x000, 0x120, -0x0FC, 0x144, 0x41C, 0x15C, 0x21C, 0x15C},
+ {0x000, 0x120, -0x11C, 0x124, 0x3EC, 0x3EC, 0x1EC, 0x12C},
+ };
+ static const struct {
+ int16_t y;
+ uint8_t routine, frame;
+ } items[4] = {{0xD0, 2, 0}, {0xE4, 2, 6}, {0xEA, 2, 7}, {0xE0, 2, 0xA}};
+ switch (o->routine) {
+ case 0: {
+ int z = V.zone;
+ if (ZONE_ACT == id_LZ_act4) z = 5;
+ int d2 = z;
+ if (ZONE_ACT == id_FZ) z = 6, d2 = 0xB;
+ for (int i = 0; i < 4; i++) {
+ Obj *a1 = o + i;
+ a1->id = id_TitleCard;
+ a1->x.w.i = OW(a1, card_finalX) = con[z][i * 2];
+ OW(a1, card_mainX) = con[z][i * 2 + 1];
+ obScreenY(a1) = items[i].y;
+ a1->routine = items[i].routine;
+ uint8_t f = items[i].frame;
+ if (!f) f = (uint8_t)d2;
+ if (f == 7) {
+ f = (uint8_t)(f + V.act);
+ if (V.act == 3) f--;
+ }
+ a1->frame = f;
+ a1->map = map_get(MAP_Card);
+ a1->gfx = ArtTile_Title_Card | Tile_Prio;
+ a1->actwid = 240 / 2;
+ a1->render = sprite_cam_screen;
+ a1->priority = 0;
+ set_timew(a1, 60);
+ }
+ }
+ /* fall through */
+ case 2:
+ card_move(o, OW(o, card_mainX), 0x10);
+ card_show(o);
+ return;
+ default:
+ if (timew(o)) {
+ set_timew(o, (uint16_t)(timew(o) - 1));
+ DisplaySprite(o);
+ return;
+ }
+ if ((o->render & 0x80) && OW(o, card_finalX) != o->x.w.i) {
+ card_move(o, OW(o, card_finalX), 0x20);
+ card_show(o);
+ return;
+ }
+ if (o->routine == 4) {
+ AddPLC(plcid_Explode);
+ AddPLC(plcid_GHZAnimals + V.zone);
+ }
+ DeleteObject(o);
+ }
+}
+
+void GameOverCard(Obj *o) {
+ switch (o->routine) {
+ case 0:
+ o->routine += 2;
+ o->x.w.i = 0x80 - 48;
+ if (o->frame & 1) o->x.w.i = 0x80 + 320 + 48;
+ obScreenY(o) = 0x80 + 224 / 2;
+ o->map = map_get(MAP_Over);
+ o->gfx = ArtTile_Game_Over | Tile_Prio;
+ o->render = sprite_cam_screen;
+ o->priority = 0;
+ /* fall through */
+ case 2:
+ if (o->x.w.i != 0x80 + 320 / 2) {
+ o->x.w.i = (int16_t)(o->x.w.i + ((uint16_t)o->x.w.i < 0x80 + 320 / 2 ? 0x10 : -0x10));
+ DisplaySprite(o);
+ return;
+ }
+ set_timew(o, 12 * 60);
+ o->routine += 2;
+ return;
+ case 4:
+ if (!(V.jpadpress1 & btnABC)) {
+ if (o->frame & 1) {
+ DisplaySprite(o);
+ return;
+ }
+ if (timew(o)) {
+ set_timew(o, (uint16_t)(timew(o) - 1));
+ DisplaySprite(o);
+ return;
+ }
+ }
+ if (V.f_timeover) {
+ V.lamp_timemin = V.lamp_timesec = V.lamp_timecent = 0;
+ V.f_restart = 1;
+ } else {
+ V.gamemode = id_Continue;
+ if (!V.continues) V.gamemode = id_Sega;
+ }
+ DisplaySprite(o);
+ }
+}
+
+#define got_mainX 0x30
+#define got_finalX 0x32
+
+static const uint16_t level_order[6][4] = {
+ {id_GHZ_act2, id_GHZ_act3, id_MZ_act1, 0}, {id_LZ_act2, id_LZ_act3, id_SLZ_act1, id_FZ},
+ {id_MZ_act2, id_MZ_act3, id_SYZ_act1, 0}, {id_SLZ_act2, id_SLZ_act3, id_SBZ_act1, 0},
+ {id_SYZ_act2, id_SYZ_act3, id_LZ_act1, 0}, {id_SBZ_act2, id_LZ_act4, 0, 0},
+};
+
+void GotThroughCard(Obj *o) {
+ static const struct {
+ int16_t x, main, y;
+ uint8_t routine, frame;
+ } items[7] = {{0x004, 0x124, 0xBC, 2, 0}, {-0x120, 0x120, 0xD0, 2, 1}, {0x40C, 0x14C, 0xD6, 2, 6},
+ {0x520, 0x120, 0xEC, 2, 2}, {0x540, 0x120, 0xFC, 2, 3}, {0x560, 0x120, 0x10C, 2, 4},
+ {0x20C, 0x14C, 0xCC, 2, 5}};
+ switch (o->routine) {
+ case 0:
+ for (int i = 0; i < 7; i++) {
+ Obj *a1 = o + i;
+ a1->id = id_GotThroughCard;
+ a1->x.w.i = OW(a1, got_finalX) = items[i].x;
+ OW(a1, got_mainX) = items[i].main;
+ obScreenY(a1) = items[i].y;
+ a1->routine = items[i].routine;
+ uint8_t f = items[i].frame;
+ if (f == 6) f = (uint8_t)(f + V.act);
+ a1->frame = f;
+ a1->map = map_get(MAP_Got);
+ a1->gfx = ArtTile_Title_Card | Tile_Prio;
+ a1->render = sprite_cam_screen;
+ }
+ /* fall through */
+ case 2:
+ if (!card_move(o, OW(o, got_mainX), 0x10)) {
+ card_show(o);
+ return;
+ }
+ if (OBJ(23 + 5)->routine == 0xE) {
+ o->routine = 0xE;
+ goto moveout;
+ }
+ if (o->frame != 4) {
+ card_show(o);
+ return;
+ }
+ o->routine += 2;
+ set_timew(o, 3 * 60);
+ /* fall through */
+ case 4:
+ case 8:
+ case 0xC:
+ set_timew(o, (uint16_t)(timew(o) - 1));
+ if (!timew(o)) o->routine += 2;
+ DisplaySprite(o);
+ return;
+ case 6: {
+ DisplaySprite(o);
+ V.f_endactbonus = 1;
+ int d0 = 0;
+ if (V.timebonus) d0 += 10, V.timebonus -= 10;
+ if (V.ringbonus) d0 += 10, V.ringbonus -= 10;
+ if (d0) {
+ AddPoints(d0);
+ return;
+ }
+ o->routine += 2;
+ if (ZONE_ACT == id_SBZ_act2) o->routine += 4;
+ set_timew(o, 3 * 60);
+ return;
+ }
+ case 0xA: {
+ uint16_t next = level_order[V.zone & 7][V.act & 3];
+ set_zone_act(next);
+ if (!next) V.gamemode = id_Sega;
+ else {
+ V.lastlamp = 0;
+ if (V.f_bigring) V.gamemode = id_Special;
+ else V.f_restart = 1;
+ }
+ DisplaySprite(o);
+ return;
+ }
+ case 0xE:
+ moveout:
+ if (!card_move(o, OW(o, got_finalX), 0x20)) {
+ card_show(o);
+ return;
+ }
+ if (o->frame != 4) {
+ DeleteObject(o);
+ return;
+ }
+ o->routine += 2;
+ V.f_lockctrl = 0;
+ return;
+ case 0x10:
+ V.limitright2 = (int16_t)(V.limitright2 + 2);
+ if (V.limitright2 == boss_sbz2_x + 0xB0) DeleteObject(o);
+ return;
+ }
+}
+
+/* ------------------------------------------------------------------ the signpost */
+#define spintime 0x30
+#define sparkletime 0x32
+#define sparkle_id 0x34
+
+void GotThroughAct(void) {
+ if (OBJ(23)->id) return;
+ V.limitleft2 = V.limitright2;
+ V.invinc = 0;
+ V.f_timecount = 0;
+ OBJ(23)->id = id_GotThroughCard;
+ AddPLC(plcid_TitleCard);
+ V.f_endactbonus = 1;
+ static const uint16_t bonuses[21] = {5000, 5000, 1000, 500, 400, 400, 300, 300, 200, 200, 200,
+ 200, 100, 100, 100, 100, 50, 50, 50, 50, 0};
+ unsigned t = (V.timemin * 60u + V.timesec) / 15;
+ if (t > 20) t = 20;
+ V.timebonus = bonuses[t];
+ V.ringbonus = (uint16_t)(V.rings * 10);
+}
+
+void Signpost(Obj *o) {
+ static const int8_t spark[8][2] = {{-0x18, -0x10}, {8, 8}, {-0x10, 0}, {0x18, -8},
+ {0, -8}, {0x10, 0}, {-0x18, 8}, {0x18, 0x10}};
+ switch (o->routine) {
+ case 0:
+ o->routine += 2;
+ o->map = map_get(MAP_Sign);
+ o->gfx = ArtTile_Signpost;
+ o->render = sprite_cam_field;
+ o->actwid = 48 / 2;
+ o->priority = 4;
+ /* fall through */
+ case 2: {
+ uint16_t d = (uint16_t)(player->x.w.i - o->x.w.i);
+ if (d < 32) {
+ V.f_timecount = 0;
+ V.limitleft2 = V.limitright2;
+ o->routine += 2;
+ }
+ break;
+ }
+ case 4: {
+ if ((int16_t)--OW(o, spintime) < 0) {
+ OW(o, spintime) = 60;
+ if (++o->anim == 3) o->routine += 2;
+ }
+ if ((int16_t)--OW(o, sparkletime) >= 0) break;
+ OW(o, sparkletime) = 12 - 1;
+ int k = OB(o, sparkle_id) >> 1;
+ OB(o, sparkle_id) = (uint8_t)((OB(o, sparkle_id) + 2) & 0xE);
+ Obj *a1 = FindFreeObj();
+ if (!a1) break;
+ a1->id = id_Rings;
+ a1->routine = 6;
+ a1->x.w.i = (int16_t)(o->x.w.i + spark[k][0]);
+ a1->y.w.i = (int16_t)(o->y.w.i + spark[k][1]);
+ a1->map = map_get(MAP_Ring);
+ a1->gfx = ArtTile_Ring | Tile_Pal2;
+ a1->render = sprite_cam_field;
+ a1->priority = 2;
+ a1->actwid = 8;
+ break;
+ }
+ case 6:
+ if (V.debuguse) break;
+ if (!(player->status & status_in_air)) {
+ V.f_lockctrl = 1;
+ V.jpadhold2 = btnR, V.jpadpress2 = 0;
+ }
+ if (player->id && (uint16_t)player->x.w.i < (uint16_t)(V.limitright2 + 320 - 24)) break;
+ o->routine += 2;
+ GotThroughAct();
+ break;
+ }
+ AnimateSprite(o, ani_get(ANI_Sign));
+ DisplaySprite(o);
+ if (obj_far(o)) DeleteObject(o);
+}
+
+/* ------------------------------------------------------------------ lampposts */
+#define lamp_origX 0x30
+#define lamp_origY 0x32
+#define lamp_time 0x36
+
+static void Lamp_StoreInfo(Obj *o) {
+ V.lastlamp = obSubtype(o);
+ V.lastlamp_dup = V.lastlamp;
+ V.lamp_xpos = o->x.w.i;
+ V.lamp_ypos = o->y.w.i;
+ V.lamp_rings = V.rings;
+ V.lamp_lives = V.lifecount;
+ V.lamp_timemin = V.timemin, V.lamp_timesec = V.timesec, V.lamp_timecent = V.timecent;
+ V.lamp_dle = V.dle_routine;
+ V.lamp_limitbtm = V.limitbtm2;
+ V.lamp_scrx = V.cam[0].x.w.i, V.lamp_scry = V.cam[0].y.w.i;
+ V.lamp_bgscrx = V.cam[1].x.w.i, V.lamp_bgscry = V.cam[1].y.w.i;
+ V.lamp_bg2scrx = V.cam[2].x.w.i, V.lamp_bg2scry = V.cam[2].y.w.i;
+ V.lamp_bg3scrx = V.cam[3].x.w.i, V.lamp_bg3scry = V.cam[3].y.w.i;
+ V.lamp_wtrpos = V.waterpos2;
+ V.lamp_wtrrout = V.wtr_routine;
+ V.lamp_wtrstat = V.f_wtr_state;
+}
+
+void Lamp_LoadInfo(void) {
+ V.lastlamp = V.lastlamp_dup;
+ player->x.w.i = V.lamp_xpos;
+ player->y.w.i = V.lamp_ypos;
+ /* (the rings and the extra-life flags kept are cleared at once, as in the original) */
+ V.rings = 0;
+ V.lifecount = 0;
+ V.timemin = V.lamp_timemin, V.timesec = V.lamp_timesec;
+ V.timecent = 60 - 1;
+ V.timesec--;
+ V.dle_routine = V.lamp_dle;
+ V.wtr_routine = V.lamp_wtrrout;
+ V.limitbtm2 = V.limitbtm1 = V.lamp_limitbtm;
+ V.cam[0].x.w.i = V.lamp_scrx, V.cam[0].y.w.i = V.lamp_scry;
+ V.cam[1].x.w.i = V.lamp_bgscrx, V.cam[1].y.w.i = V.lamp_bgscry;
+ V.cam[2].x.w.i = V.lamp_bg2scrx, V.cam[2].y.w.i = V.lamp_bg2scry;
+ V.cam[3].x.w.i = V.lamp_bg3scrx, V.cam[3].y.w.i = V.lamp_bg3scry;
+ if (V.zone == id_LZ) {
+ V.waterpos2 = V.lamp_wtrpos;
+ V.wtr_routine = V.lamp_wtrrout;
+ V.f_wtr_state = V.lamp_wtrstat;
+ }
+ if (V.lastlamp & 0x80) V.limitleft2 = (int16_t)(V.lamp_xpos - 320 / 2);
+}
+
+void Lamppost(Obj *o) {
+ uint8_t *st = &V.objstate[2 + o->respawn];
+ switch (o->routine) {
+ case 0:
+ o->routine += 2;
+ o->map = map_get(MAP_Lamp);
+ o->gfx = ArtTile_Lamppost;
+ o->render = sprite_cam_field;
+ o->actwid = 16 / 2;
+ o->priority = 5;
+ *st &= 0x7F;
+ if ((*st & 1) || (V.lastlamp & 0x7F) >= (obSubtype(o) & 0x7F)) {
+ *st |= 1;
+ o->routine = 4;
+ o->frame = 3;
+ break;
+ }
+ /* fall through */
+ case 2: {
+ if (V.debuguse || (V.f_playerctrl & 0x80)) break;
+ if ((V.lastlamp & 0x7F) >= (obSubtype(o) & 0x7F)) {
+ *st |= 1;
+ o->routine = 4;
+ o->frame = 3;
+ break;
+ }
+ if ((uint16_t)(player->x.w.i - o->x.w.i + 8) >= 16) break;
+ if ((uint16_t)(player->y.w.i - o->y.w.i + 0x40) >= 0x68) break;
+ o->routine += 2;
+ Obj *a1 = FindFreeObj();
+ if (a1) {
+ a1->id = id_Lamppost;
+ a1->routine = 6;
+ OW(a1, lamp_origX) = o->x.w.i;
+ OW(a1, lamp_origY) = (int16_t)(o->y.w.i - 0x18);
+ a1->map = map_get(MAP_Lamp);
+ a1->gfx = ArtTile_Lamppost;
+ a1->render = sprite_cam_field;
+ a1->actwid = 16 / 2;
+ a1->priority = 4;
+ a1->frame = 2;
+ OW(a1, lamp_time) = 32;
+ }
+ o->frame = 1;
+ Lamp_StoreInfo(o);
+ *st |= 1;
+ break;
+ }
+ case 4: break;
+ case 6: {
+ if ((int16_t)--OW(o, lamp_time) < 0) o->routine = 4;
+ uint8_t a = o->angle;
+ o->angle = (uint8_t)(o->angle - 0x10);
+ int16_t c, s = CalcSine((uint8_t)(a - 0x40), &c);
+ o->x.w.i = (int16_t)(((int32_t)c * 0xC00 >> 16) + OW(o, lamp_origX));
+ o->y.w.i = (int16_t)(((int32_t)s * 0xC00 >> 16) + OW(o, lamp_origY));
+ break;
+ }
+ }
+ RememberState(o);
+}
diff --git a/games/sonic/src/obj_common.c b/games/sonic/src/obj_common.c
new file mode 100644
index 00000000..f307ab00
--- /dev/null
+++ b/games/sonic/src/obj_common.c
@@ -0,0 +1,635 @@
+/* Objects of every zone: the shield and invincibility stars, springs, spikes, the giant ring to
+ * the special stage and its flash, hidden bonus points, the prison capsule at the end of each third
+ * act, buttons, invisible walls; and what objects share: smashing into pieces, a boss's explosions
+ * and its movement. */
+#include "s1.h"
+#include "game.h"
+#include "objs.h"
+
+/* ------------------------------------------------------------------ shield and stars */
+#define stars_lag 0x30
+
+void ShieldItem(Obj *o) {
+ switch (o->routine) {
+ case 0:
+ o->routine += 2;
+ o->map = map_get(MAP_Shield);
+ o->render = sprite_cam_field;
+ o->priority = 1;
+ o->actwid = 32 / 2;
+ if (o->anim) {
+ o->routine += 2;
+ o->gfx = ArtTile_Invincibility;
+ } else o->gfx = ArtTile_Shield;
+ return;
+ case 2:
+ if (V.invinc) return;
+ if (!V.shield) {
+ DeleteObject(o);
+ return;
+ }
+ o->x.w.i = player->x.w.i;
+ o->y.w.i = player->y.w.i;
+ o->status = player->status;
+ break;
+ default: {
+ if (!V.invinc) {
+ DeleteObject(o);
+ return;
+ }
+ uint8_t d0 = (uint8_t)V.trackpos;
+ uint8_t d1 = (uint8_t)((o->anim - 1) * 24 + 4);
+ d0 = (uint8_t)(d0 - d1);
+ d1 = OB(o, stars_lag);
+ d0 = (uint8_t)(d0 - d1);
+ d1 = (uint8_t)(d1 + 4);
+ if (d1 >= 6 * 4) d1 = 0;
+ OB(o, stars_lag) = d1;
+ const uint16_t *p = tracksonic[d0 >> 2];
+ o->x.w.i = (int16_t)p[0];
+ o->y.w.i = (int16_t)p[1];
+ o->status = player->status;
+ break;
+ }
+ }
+ AnimateSprite(o, ani_get(ANI_Shield));
+ DisplaySprite(o);
+}
+
+/* ------------------------------------------------------------------ springs */
+#define spring_pow 0x30
+
+void Springs(Obj *o) {
+ Obj *a1 = player;
+ switch (o->routine) {
+ case 0: {
+ o->routine += 2;
+ o->map = map_get(MAP_Spring);
+ o->gfx = ArtTile_Spring_Horizontal;
+ o->render |= sprite_cam_field;
+ o->actwid = 32 / 2;
+ o->priority = 4;
+ uint8_t d0 = obSubtype(o);
+ if (d0 & 0x10) {
+ o->routine = 8;
+ o->anim = 1;
+ o->frame = 3;
+ o->gfx = ArtTile_Spring_Vertical;
+ o->actwid = 16 / 2;
+ }
+ if (d0 & 0x20) {
+ o->routine = 0xE;
+ o->status |= 2;
+ }
+ if (d0 & 2) o->gfx |= 0x2000;
+ OW(o, spring_pow) = (d0 & 0xF) ? -0xA00 : -0x1000;
+ break;
+ }
+ case 2:
+ SolidObject(o, 32 / 2 + sonic_solid_width, 16 / 2, 32 / 2, o->x.w.i);
+ if (!o->routine2) break;
+ o->routine += 2;
+ a1->y.w.i = (int16_t)(a1->y.w.i + 8);
+ a1->vy = OW(o, spring_pow);
+ a1->status |= status_in_air;
+ a1->status &= (uint8_t)~status_on_object;
+ a1->anim = id_Spring;
+ a1->routine = 2;
+ o->status &= (uint8_t)~status_on_object;
+ o->routine2 = 0;
+ /* fall through */
+ case 4:
+ case 0xA:
+ case 0x10:
+ AnimateSprite(o, ani_get(ANI_Spring));
+ break;
+ case 6:
+ case 0x12:
+ o->prevani = 1;
+ o->routine -= 4;
+ break;
+ case 8:
+ SolidObject(o, 16 / 2 + sonic_solid_width, 28 / 2, 30 / 2, o->x.w.i);
+ if (o->routine == 2) o->routine = 8;
+ if (!(o->status & status_pushing)) break;
+ o->routine += 2;
+ a1->vx = OW(o, spring_pow);
+ a1->x.w.i = (int16_t)(a1->x.w.i + 8);
+ if (!(o->status & 1)) {
+ a1->x.w.i = (int16_t)(a1->x.w.i - 16);
+ a1->vx = (int16_t)-a1->vx;
+ }
+ OW(a1, locktime) = 15;
+ a1->inertia = a1->vx;
+ a1->status ^= 1;
+ if (!(a1->status & status_rolling)) a1->anim = id_Walk;
+ o->status &= (uint8_t)~status_pushing;
+ a1->status &= (uint8_t)~status_pushing;
+ AnimateSprite(o, ani_get(ANI_Spring));
+ break;
+ case 0xC:
+ o->prevani = 2;
+ o->routine -= 4;
+ break;
+ case 0xE: {
+ int d4 = SolidObject(o, 32 / 2 + sonic_solid_width, 16 / 2, 32 / 2, o->x.w.i);
+ if (o->routine == 2) o->routine = 0xE;
+ if (o->routine2 || d4 >= 0) break;
+ o->routine += 2;
+ a1->y.w.i = (int16_t)(a1->y.w.i - 8);
+ a1->vy = (int16_t)-OW(o, spring_pow);
+ a1->status |= status_in_air;
+ a1->status &= (uint8_t)~status_on_object;
+ a1->routine = 2;
+ o->status &= (uint8_t)~status_on_object;
+ o->routine2 = 0;
+ AnimateSprite(o, ani_get(ANI_Spring));
+ break;
+ }
+ }
+ DisplaySprite(o);
+ if (obj_far(o)) DeleteObject(o);
+}
+
+/* ------------------------------------------------------------------ spikes */
+#define spikes_origX 0x30
+#define spikes_origY 0x32
+#define spikes_move_pos 0x34
+#define spikes_move_direction 0x36
+#define spikes_move_delay 0x38
+
+static void Spikes_WaitAndMove(Obj *o) {
+ if (OW(o, spikes_move_delay)) {
+ OW(o, spikes_move_delay)--;
+ return;
+ }
+ if (OW(o, spikes_move_direction)) {
+ if (OUW(o, spikes_move_pos) >= 0x800) {
+ OUW(o, spikes_move_pos) -= 0x800;
+ return;
+ }
+ OW(o, spikes_move_pos) = 0;
+ OW(o, spikes_move_direction) = 0;
+ OW(o, spikes_move_delay) = 60;
+ return;
+ }
+ OUW(o, spikes_move_pos) += 0x800;
+ if (OUW(o, spikes_move_pos) < 0x2000) return;
+ OW(o, spikes_move_pos) = 0x2000;
+ OW(o, spikes_move_direction) = 1;
+ OW(o, spikes_move_delay) = 60;
+}
+
+void Spikes(Obj *o) {
+ static const uint8_t config[6][2] = {{0, 20}, {1, 16}, {2, 4}, {3, 28}, {4, 64}, {5, 16}};
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_Spike);
+ o->gfx = ArtTile_Spikes;
+ o->render |= sprite_cam_field;
+ o->priority = 4;
+ int k = (obSubtype(o) & 0xF0) >> 4;
+ obSubtype(o) &= 0xF;
+ o->frame = config[k][0];
+ o->actwid = config[k][1];
+ OW(o, spikes_origX) = o->x.w.i;
+ OW(o, spikes_origY) = o->y.w.i;
+ }
+ switch (obSubtype(o)) {
+ case 1:
+ Spikes_WaitAndMove(o);
+ o->y.w.i = (int16_t)(OW(o, spikes_origY) + OWH(o, spikes_move_pos));
+ break;
+ case 2:
+ Spikes_WaitAndMove(o);
+ o->x.w.i = (int16_t)(OW(o, spikes_origX) + OWH(o, spikes_move_pos));
+ break;
+ }
+ bool hurt;
+ if (o->frame == 5 || o->frame == 1) {
+ int16_t d2 = o->frame == 1 ? 40 / 2 : 8 / 2;
+ int d4 = SolidObject(o, 32 / 2 + sonic_solid_width, d2, (int16_t)(d2 + 1), o->x.w.i);
+ hurt = !(o->status & status_on_object) && d4 == 1;
+ } else {
+ int d4 = SolidObject(o, (int16_t)(o->actwid + sonic_solid_width), 32 / 2, 32 / 2 + 1, o->x.w.i);
+ hurt = (o->status & status_on_object) || d4 < 0;
+ }
+ if (hurt && !V.invinc && player->routine < 4) {
+ player->y.l -= (int32_t)player->vy << 8;
+ HurtSonic(player, o);
+ }
+ DisplaySprite(o);
+ if (obj_far_x(OW(o, spikes_origX))) DeleteObject(o);
+}
+
+/* ------------------------------------------------------------------ the giant ring */
+#define gring_parent 0x3C
+
+void GiantRing(Obj *o) {
+ switch (o->routine) {
+ case 0:
+ o->map = map_get(MAP_GRing);
+ o->gfx = ArtTile_Giant_Ring | Tile_Pal2;
+ o->render |= sprite_cam_field;
+ o->actwid = 128 / 2;
+ if (o->render & 0x80) {
+ if (V.emeralds == 6) {
+ DeleteObject(o);
+ return;
+ }
+ if (V.rings < 50) return;
+ o->routine += 2;
+ o->priority = 2;
+ o->coltype = col_16x32 | col_item;
+ V.gfxbigring = 1;
+ }
+ break;
+ case 4: {
+ o->routine -= 2;
+ o->coltype = col_none;
+ Obj *a1 = FindFreeObj();
+ if (a1) {
+ a1->id = id_RingFlash;
+ a1->x.w.i = o->x.w.i;
+ a1->y.w.i = o->y.w.i;
+ OUW(a1, gring_parent) = OADDR(o);
+ if ((uint16_t)player->x.w.i >= (uint16_t)o->x.w.i) a1->render |= sprite_xflip;
+ }
+ break;
+ }
+ case 6: DeleteObject(o); return;
+ }
+ o->frame = V.ani_frame[0];
+ if (obj_far(o)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+}
+
+void RingFlash(Obj *o) {
+ switch (o->routine) {
+ case 0:
+ o->routine += 2;
+ o->map = map_get(MAP_Flash);
+ o->gfx = ArtTile_Giant_Ring_Flash | Tile_Pal2;
+ o->render |= sprite_cam_field;
+ o->priority = 0;
+ o->actwid = 64 / 2;
+ o->frame = 0xFF;
+ /* fall through */
+ case 2:
+ if ((int8_t)--o->timeframe < 0) {
+ o->timeframe = 1;
+ o->frame++;
+ if (o->frame >= 8) {
+ o->routine += 2;
+ player->id = 0, player->render = 0;
+ return;
+ }
+ if (o->frame == 3) {
+ OPTR(OUW(o, gring_parent))->routine = 6;
+ player->anim = id_Null;
+ V.f_bigring = 1;
+ V.invinc = 0;
+ V.shield = 0;
+ }
+ }
+ if (obj_far(o)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+ return;
+ default: DeleteObject(o);
+ }
+}
+
+/* ------------------------------------------------------------------ hidden bonus points */
+#define bonus_timelen 0x30
+
+void HiddenBonus(Obj *o) {
+ if (o->routine == 0) {
+ if ((uint16_t)(player->x.w.i - o->x.w.i + 16) < 32 && (uint16_t)(player->y.w.i - o->y.w.i + 16) < 32 &&
+ !V.debuguse && !V.f_bigring) {
+ o->routine += 2;
+ o->map = map_get(MAP_Bonus);
+ o->gfx = ArtTile_Hidden_Points | Tile_Prio;
+ o->render |= sprite_cam_field;
+ o->priority = 0;
+ o->actwid = 32 / 2;
+ o->frame = obSubtype(o);
+ OW(o, bonus_timelen) = 120 - 1;
+ static const uint16_t points[4] = {0, 1000, 100, 1};
+ AddPoints(points[obSubtype(o) & 3]);
+ }
+ if (obj_far(o)) DeleteObject(o);
+ return;
+ }
+ if (--OW(o, bonus_timelen) < 0 || obj_far(o)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+}
+
+/* ------------------------------------------------------------------ shared */
+/* the object's frame as pieces flying apart at these speeds */
+void SmashObject(Obj *o, const int16_t *speeds, int n, int16_t gravity) {
+ const uint8_t *p = map_frame(o->map, o->frame) + 2;
+ o->render |= sprite_rawmappings;
+ uint8_t id = o->id, r = o->render;
+ Obj *a1 = o;
+ for (int i = 0; i < n; i++) {
+ if (i) {
+ a1 = FindFreeObj();
+ if (!a1) break;
+ p += 6;
+ }
+ a1->routine = 4;
+ a1->id = id;
+ a1->map = p;
+ a1->render = r;
+ a1->x.w.i = o->x.w.i;
+ a1->y.w.i = o->y.w.i;
+ a1->gfx = o->gfx;
+ a1->priority = o->priority;
+ a1->actwid = o->actwid;
+ a1->vx = *speeds++;
+ a1->vy = *speeds++;
+ if (a1 < o) { /* (already passed this frame: moved and shown now) */
+ SpeedToPos(a1);
+ a1->vy = (int16_t)(a1->vy + gravity);
+ DisplaySprite(a1);
+ }
+ }
+}
+
+/* explosions over a defeated boss, one every 8 frames */
+void BossDefeated(Obj *o) {
+ if (vblank_byte & 7) return;
+ Obj *a1 = FindFreeObj();
+ if (!a1) return;
+ a1->id = id_Explosion;
+ a1->x.w.i = o->x.w.i;
+ a1->y.w.i = o->y.w.i;
+ uint32_t r = RandomNumber();
+ a1->x.w.i = (int16_t)(a1->x.w.i + ((uint8_t)r >> 2) - 0x20);
+ a1->y.w.i = (int16_t)(a1->y.w.i + ((uint8_t)(r >> 8) >> 3));
+}
+
+void BossMove(Obj *o) {
+ obBossX(o).l += (int32_t)o->vx << 8;
+ obBossY(o).l += (int32_t)o->vy << 8;
+}
+
+/* ------------------------------------------------------------------ the prison capsule */
+#define pri_origY 0x30
+#define animal_prisondelay 0x36
+
+void Prison(Obj *o) {
+ static const uint8_t var[4][4] = {{2, 64 / 2, 4, 0}, {4, 24 / 2, 5, 1}, {6, 32 / 2, 4, 3}, {8, 32 / 2, 3, 5}};
+ switch (o->routine) {
+ case 0: {
+ o->map = map_get(MAP_Pri);
+ o->gfx = ArtTile_Prison_Capsule;
+ o->render = sprite_cam_field;
+ OW(o, pri_origY) = o->y.w.i;
+ int k = obSubtype(o) & 3;
+ o->routine = var[k][0];
+ o->actwid = var[k][1];
+ o->priority = var[k][2];
+ o->frame = var[k][3];
+ if (obSubtype(o) == 2) {
+ o->coltype = col_32x32 | col_boss;
+ obBossHits(o) = 8;
+ }
+ break;
+ }
+ case 2:
+ if (V.bossstatus != 2) {
+ SolidObject(o, 64 / 2 + sonic_solid_width, 48 / 2, 48 / 2, o->x.w.i);
+ break;
+ }
+ if (o->routine2) {
+ o->routine2 = 0;
+ player->status &= (uint8_t)~status_on_object;
+ player->status |= status_in_air;
+ }
+ o->frame = 2;
+ break;
+ case 4:
+ SolidObject(o, 24 / 2 + sonic_solid_width, 16 / 2, 16 / 2, o->x.w.i);
+ AnimateSprite(o, ani_get(ANI_Pri));
+ o->y.w.i = OW(o, pri_origY);
+ if (!o->routine2) break;
+ o->y.w.i = (int16_t)(o->y.w.i + 8);
+ o->routine = 0xA;
+ set_timew(o, 60);
+ V.f_timecount = 0;
+ V.f_lockscreen = 0;
+ V.f_lockctrl = 1;
+ V.jpadhold2 = btnR, V.jpadpress2 = 0;
+ o->routine2 = 0;
+ player->status &= (uint8_t)~status_on_object;
+ player->status |= status_in_air;
+ break;
+ case 6:
+ case 8:
+ case 0xA:
+ BossDefeated(o);
+ set_timew(o, (uint16_t)(timew(o) - 1));
+ if (timew(o)) break;
+ V.bossstatus = 2;
+ o->routine = 0xC;
+ o->frame = 6;
+ set_timew(o, 2 * 60 + 30);
+ o->y.w.i = (int16_t)(o->y.w.i + 32);
+ for (int i = 0, d5 = 2 * 60 + 34, d4 = -28; i < 8; i++, d4 += 7, d5 -= 8) {
+ Obj *a1 = FindFreeObj();
+ if (!a1) break;
+ a1->id = id_Animals;
+ a1->x.w.i = (int16_t)(o->x.w.i + d4);
+ a1->y.w.i = o->y.w.i;
+ OW(a1, animal_prisondelay) = (int16_t)d5;
+ }
+ break;
+ case 0xC: {
+ if (!(vblank_byte & 7)) {
+ Obj *a1 = FindFreeObj();
+ if (a1) {
+ a1->id = id_Animals;
+ a1->x.w.i = o->x.w.i;
+ a1->y.w.i = o->y.w.i;
+ int16_t d0 = (int16_t)((RandomNumber() & 0x1F) - 6);
+ if ((int16_t)V.random < 0) d0 = (int16_t)-d0;
+ a1->x.w.i = (int16_t)(a1->x.w.i + d0);
+ OW(a1, animal_prisondelay) = 12;
+ }
+ }
+ set_timew(o, (uint16_t)(timew(o) - 1));
+ if (!timew(o)) o->routine += 2, set_timew(o, 3 * 60);
+ break;
+ }
+ case 0xE:
+ for (int i = 1; i < 64; i++)
+ if (objs[i].id == id_Animals) goto show;
+ GotThroughAct();
+ DeleteObject(o);
+ return;
+ }
+show:
+ if (obj_far(o)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+}
+
+/* ------------------------------------------------------------------ invisible walls */
+void Invisibarrier(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_Invis);
+ o->gfx = ArtTile_Monitor | Tile_Prio;
+ o->render |= sprite_cam_field;
+ o->actwid = (uint8_t)(((obSubtype(o) & 0xF0) + 0x10) >> 1);
+ o->height = (uint8_t)(((obSubtype(o) & 0xF) + 1) << 3);
+ }
+ if (ChkObjectVisible(o))
+ SolidObject_NoRenderChk(o, (int16_t)(o->actwid + sonic_solid_width), o->height, (int16_t)(o->height + 1),
+ o->x.w.i);
+ if (obj_far(o)) {
+ DeleteObject(o);
+ return;
+ }
+ if (V.debuguse) DisplaySprite(o);
+}
+
+/* ------------------------------------------------------------------ buttons */
+/* MZ: a pushable block on the button */
+static bool But_MZBlock(Obj *o) {
+ int16_t d2 = (int16_t)(o->x.w.i - 0x10), d3 = (int16_t)(o->y.w.i - 8);
+ for (int i = NRESERVED; i < NOBJ; i++) {
+ Obj *a1 = &objs[i];
+ if (!(a1->render & 0x80) || a1->id != id_PushBlock) continue;
+ uint16_t ax = (uint16_t)(a1->x.w.i - 0x10), d0 = (uint16_t)(ax - (uint16_t)d2);
+ if (ax >= (uint16_t)d2) {
+ if (d0 > 0x20) continue;
+ } else if ((uint32_t)d0 + 0x20 <= 0xFFFF) continue;
+ uint16_t ay = (uint16_t)(a1->y.w.i - 0x10), e0 = (uint16_t)(ay - (uint16_t)d3);
+ if (ay >= (uint16_t)d3) {
+ if (e0 > 0x10) continue;
+ } else if ((uint32_t)e0 + 0x20 <= 0xFFFF) continue;
+ return true;
+ }
+ return false;
+}
+
+void Button(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_But);
+ o->gfx = ArtTile_Button_Main | Tile_Pal3;
+ if (V.zone != id_MZ) o->gfx = ArtTile_Button_Main;
+ o->render = sprite_cam_field;
+ o->actwid = 32 / 2;
+ o->priority = 4;
+ o->y.w.i = (int16_t)(o->y.w.i + 3);
+ }
+ if (o->render & 0x80) {
+ SolidObject(o, 32 / 2 + sonic_solid_width, 10 / 2, 10 / 2, o->x.w.i);
+ o->frame &= (uint8_t)~1;
+ uint8_t *a3 = &V.f_switch[obSubtype(o) & 0xF];
+ int d3 = (obSubtype(o) & 0x40) ? 7 : 0;
+ bool pressed = ((obSubtype(o) & 0x80) && But_MZBlock(o)) || o->routine2;
+ if (pressed) {
+ *a3 |= (uint8_t)(1 << d3);
+ o->frame |= 1;
+ } else *a3 &= (uint8_t)~(1 << d3);
+ if ((obSubtype(o) & 0x20) && (int8_t)--o->timeframe < 0) {
+ o->timeframe = 7;
+ o->frame ^= 2;
+ }
+ }
+ DisplaySprite(o);
+ if (obj_far(o)) DeleteObject(o);
+}
+
+/* ------------------------------------------------------------------ GHZ: the waterfall's sound, edge walls */
+void WaterSound(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->render = sprite_cam_field;
+ }
+ if (obj_far(o)) DeleteObject(o);
+}
+
+/* a wall Sonic can't pass but can stand on: pushes him out sideways, or stops him from below */
+static void EdgeWall_SolidWall(Obj *o, int16_t d1, int16_t d2) {
+ Obj *a1 = player;
+ int d4 = 0;
+ int16_t d0 = (int16_t)(a1->x.w.i - o->x.w.i + d1), d3 = 0;
+ if (d0 >= 0 && (uint16_t)d0 <= (uint16_t)(d1 * 2)) {
+ d2 = (int16_t)(d2 + (int8_t)a1->height);
+ d3 = (int16_t)(a1->y.w.i - o->y.w.i + d2);
+ int16_t d4w = (int16_t)(d2 * 2);
+ if (d3 >= 0 && (uint16_t)d3 < (uint16_t)d4w && !(V.f_playerctrl & 0x80) && a1->routine < 6 && !V.debuguse) {
+ uint16_t d5 = (uint16_t)d0;
+ if ((uint16_t)d1 < (uint16_t)d0) {
+ d0 = (int16_t)(d0 - d1 * 2);
+ d5 = (uint16_t)-d0;
+ }
+ uint16_t e1 = (uint16_t)d3;
+ if ((uint16_t)d2 < (uint16_t)d3) {
+ d3 = (int16_t)(d3 - d4w);
+ e1 = (uint16_t)-d3;
+ }
+ d4 = d5 > e1 ? -1 : 1;
+ }
+ }
+ if (d4 == 0) {
+ if (!(o->status & status_pushing)) return;
+ o->status &= (uint8_t)~status_pushing;
+ a1->status &= (uint8_t)~status_pushing;
+ return;
+ }
+ if (d4 < 0) {
+ if (a1->vy < 0 && d3 < 0) {
+ a1->y.w.i = (int16_t)(a1->y.w.i - d3);
+ a1->vy = 0;
+ }
+ return;
+ }
+ if (d0 && ((d0 < 0) == (a1->vx < 0))) {
+ a1->x.w.i = (int16_t)(a1->x.w.i - d0);
+ a1->inertia = 0;
+ a1->vx = 0;
+ }
+ if (a1->status & status_in_air) {
+ o->status &= (uint8_t)~status_pushing;
+ a1->status &= (uint8_t)~status_pushing;
+ return;
+ }
+ a1->status |= status_pushing;
+ o->status |= status_pushing;
+}
+
+void EdgeWalls(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_Edge);
+ o->gfx = ArtTile_GHZ_Edge_Wall | Tile_Pal3;
+ o->render |= sprite_cam_field;
+ o->actwid = 16 / 2;
+ o->priority = 6;
+ o->frame = obSubtype(o);
+ if (o->frame & 0x10) {
+ o->frame &= (uint8_t)~0x10;
+ o->routine += 2;
+ }
+ }
+ if (o->routine == 2) EdgeWall_SolidWall(o, 38 / 2, 80 / 2);
+ DisplaySprite(o);
+ if (obj_far(o)) DeleteObject(o);
+}
diff --git a/games/sonic/src/obj_items.c b/games/sonic/src/obj_items.c
new file mode 100644
index 00000000..99cc9498
--- /dev/null
+++ b/games/sonic/src/obj_items.c
@@ -0,0 +1,677 @@
+/* Rings (in groups of up to 7 from one placement, each remembered once collected), the rings Sonic
+ * scatters when hurt, monitors and what comes out of them, the explosion of an enemy with the
+ * animal it freed and its points, and the bomb's explosion. */
+#include "s1.h"
+#include "collide.h"
+#include "game.h"
+#include "objs.h"
+
+#define ring_origX 0x32
+#define ring_respawnbit 0x34
+
+static void CollectRing(void) {
+ V.rings++;
+ V.f_ringcount |= 1;
+ if (V.rings < 100) return;
+ if (!(V.lifecount & 2)) {
+ V.lifecount |= 2;
+ } else {
+ if (V.rings < 200 || (V.lifecount & 4)) return;
+ V.lifecount |= 4;
+ }
+ V.lives++;
+ V.f_lifecount++;
+}
+
+void Rings(Obj *o) {
+ static const int8_t pos[16][2] = {{0x10, 0}, {0x18, 0}, {0x20, 0}, {0, 0x10}, {0, 0x18}, {0, 0x20},
+ {0x10, 0x10}, {0x18, 0x18}, {0x20, 0x20}, {-0x10, 0x10}, {-0x18, 0x18},
+ {-0x20, 0x20}, {0x10, 8}, {0x18, 0x10}, {-0x10, 8}, {-0x18, 0x10}};
+ switch (o->routine) {
+ case 0: {
+ uint8_t *st = &V.objstate[2 + o->respawn];
+ uint8_t d4 = *st;
+ int n = obSubtype(o) & 7;
+ if (n == 7) n = 6;
+ int8_t d5 = pos[obSubtype(o) >> 4][0], d6 = pos[obSubtype(o) >> 4][1];
+ int16_t x = o->x.w.i, y = o->y.w.i;
+ Obj *a1 = o;
+ for (int i = 0; i <= n; i++, x += d5, y += d6, d4 >>= 1) {
+ if (d4 & 1) continue;
+ *st &= 0x7F;
+ if (i) {
+ a1 = FindFreeObj();
+ if (!a1) break;
+ }
+ a1->id = id_Rings;
+ a1->routine += 2;
+ a1->x.w.i = x;
+ OW(a1, ring_origX) = o->x.w.i;
+ a1->y.w.i = y;
+ a1->map = map_get(MAP_Ring);
+ a1->gfx = ArtTile_Ring | 0x2000;
+ a1->render = 4;
+ a1->priority = 2;
+ a1->coltype = 0x47; /* col_12x12 | col_item */
+ a1->actwid = 8;
+ a1->respawn = o->respawn;
+ OB(a1, ring_respawnbit) = (uint8_t)i;
+ }
+ if (*st & 1) {
+ DeleteObject(o);
+ return;
+ }
+ }
+ /* fall through */
+ case 2:
+ o->frame = V.ani_frame[1];
+ DisplaySprite(o);
+ if (obj_far_x(OW(o, ring_origX))) DeleteObject(o);
+ return;
+ case 4:
+ o->routine += 2;
+ o->coltype = 0;
+ o->priority = 1;
+ CollectRing();
+ V.objstate[2 + o->respawn] |= (uint8_t)(1 << OB(o, ring_respawnbit));
+ /* fall through */
+ case 6:
+ AnimateSprite(o, ani_get(ANI_Ring));
+ DisplaySprite(o);
+ return;
+ default: DeleteObject(o); return;
+ }
+}
+
+extern int obj_d7;
+
+void RingLoss(Obj *o) {
+ switch (o->routine) {
+ case 0: {
+ Obj *a1 = o;
+ int n = V.rings < 32 ? V.rings : 32;
+ int16_t d4 = 0x288, d2 = 0, d3 = 0;
+ for (int i = 0; i < n; i++) {
+ if (i) {
+ a1 = FindFreeObj();
+ if (!a1) break;
+ }
+ a1->id = id_RingLoss;
+ a1->routine += 2;
+ a1->height = a1->width = 8;
+ a1->x.w.i = o->x.w.i, a1->y.w.i = o->y.w.i;
+ a1->map = map_get(MAP_Ring);
+ a1->gfx = ArtTile_Ring | 0x2000;
+ a1->render = 4;
+ a1->priority = 3;
+ a1->coltype = 0x47;
+ a1->actwid = 8;
+ V.ani_time[3] = 0xFF;
+ if (d4 >= 0) {
+ int16_t c, s = CalcSine((uint8_t)d4, &c);
+ int sh = (uint16_t)d4 >> 8;
+ d2 = (int16_t)(s << sh), d3 = (int16_t)(c << sh);
+ uint8_t lo = (uint8_t)d4;
+ if ((uint16_t)lo + 0x10 > 0xFF) { /* (carry out of the low byte) */
+ d4 = (int16_t)((d4 & 0xFF00) | (uint8_t)(lo + 0x10));
+ if ((uint16_t)d4 < 0x80) d4 = 0x288;
+ else d4 = (int16_t)(d4 - 0x80);
+ } else d4 = (int16_t)((d4 & 0xFF00) | (uint8_t)(lo + 0x10));
+ }
+ a1->vx = d2, a1->vy = d3;
+ d2 = (int16_t)-d2;
+ d4 = (int16_t)-d4;
+ }
+ V.rings = 0;
+ V.f_ringcount = 0x80;
+ V.lifecount = 0;
+ }
+ /* fall through */
+ case 2:
+ o->frame = V.ani_frame[3];
+ SpeedToPos(o);
+ o->vy += 0x18;
+ if (o->vy >= 0 && !((uint8_t)((uint8_t)V.vblank_count + obj_d7) & 3)) {
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ o->y.w.i += d1;
+ o->vy = (int16_t)(o->vy - (o->vy >> 2));
+ o->vy = (int16_t)-o->vy;
+ }
+ }
+ if (!V.ani_time[3] || (uint16_t)(V.limitbtm2 + 224) < (uint16_t)o->y.w.i) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+ return;
+ case 4:
+ o->routine += 2;
+ o->coltype = 0;
+ o->priority = 1;
+ CollectRing();
+ /* fall through */
+ case 6:
+ AnimateSprite(o, ani_get(ANI_Ring));
+ DisplaySprite(o);
+ return;
+ default: DeleteObject(o); return;
+ }
+}
+
+/* ------------------------------------------------------------------ monitors */
+static int mon_sides(Obj *o, int16_t d1, int16_t d2, int16_t *d0o, int16_t *d3o) {
+ Obj *a1 = player;
+ int16_t d0 = (int16_t)(a1->x.w.i - o->x.w.i + d1);
+ if (d0 < 0 || (uint16_t)d0 > (uint16_t)(d1 * 2)) return 0;
+ d2 = (int16_t)(d2 + (int8_t)a1->height);
+ int16_t d3 = (int16_t)(a1->y.w.i - o->y.w.i + d2);
+ if (d3 < 0 || (uint16_t)d3 >= (uint16_t)(d2 * 2)) return 0;
+ if ((V.f_playerctrl & 0x80) || player->routine >= 6 || V.debuguse) return 0;
+ if ((uint16_t)d1 < (uint16_t)d0) d0 = (int16_t)(d0 - d1 * 2);
+ *d0o = d0, *d3o = d3;
+ if ((uint16_t)d3 >= 0x10) return 1;
+ int16_t e = (int16_t)(o->actwid + 4), w = (int16_t)(e * 2);
+ e = (int16_t)(e + a1->x.w.i - o->x.w.i);
+ if (e < 0 || (uint16_t)e >= (uint16_t)w) return 1;
+ return -1;
+}
+
+void Monitor(Obj *o) {
+ Obj *a1 = player;
+ switch (o->routine) {
+ case 0:
+ o->routine = 2;
+ o->height = o->width = 14;
+ o->map = map_get(MAP_Monitor);
+ o->gfx = ArtTile_Monitor;
+ o->render = 4;
+ o->priority = 3;
+ o->actwid = 15;
+ V.objstate[2 + o->respawn] &= 0x7F;
+ if (V.objstate[2 + o->respawn] & 1) {
+ o->routine = 8;
+ o->frame = 0xB;
+ return;
+ }
+ o->coltype = 0x46;
+ o->anim = obSubtype(o);
+ /* fall through */
+ case 2:
+ if (o->routine2) {
+ if (o->routine2 == 2) {
+ ExitPlatform(o, (int16_t)(o->actwid + 11));
+ if (a1->status & 8) MvSonicOnPtfm(o, o->x.w.i, 16);
+ else o->routine2 = 0;
+ } else {
+ ObjectFall(o);
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ o->y.w.i += d1;
+ o->vy = 0;
+ o->routine2 = 0;
+ }
+ }
+ goto animate;
+ } else {
+ int16_t d0 = 0, d3 = 0;
+ int r = mon_sides(o, 15 + 11, 15, &d0, &d3);
+ if (!r || (a1->vy >= 0 && a1->anim == id_Roll)) {
+ if (o->status & 0x20) {
+ a1->anim = 0, a1->prevani = id_Run;
+ o->status &= (uint8_t)~0x20;
+ a1->status &= (uint8_t)~0x20;
+ }
+ goto animate;
+ }
+ if (r < 0) {
+ a1->y.w.i -= d3;
+ Plat_NoCheck_ext(o);
+ o->routine2 = 2;
+ goto animate;
+ }
+ if (d0 > 0) {
+ if (a1->vx >= 0) goto stop;
+ } else if (d0 < 0) {
+ if (a1->vx < 0) goto stop;
+ }
+ goto push;
+ stop:
+ a1->x.w.i -= d0;
+ a1->inertia = 0;
+ a1->vx = 0;
+ push:
+ if (a1->status & 2) {
+ o->status &= (uint8_t)~0x20;
+ a1->status &= (uint8_t)~0x20;
+ } else {
+ a1->status |= 0x20;
+ o->status |= 0x20;
+ }
+ }
+ /* fall through */
+ animate:
+ case 6:
+ AnimateSprite(o, ani_get(ANI_Monitor));
+ /* fall through */
+ case 8:
+ DisplaySprite(o);
+ if (obj_far(o)) DeleteObject(o);
+ return;
+ case 4: {
+ o->routine += 2;
+ o->coltype = 0;
+ Obj *p = FindFreeObj();
+ if (p) {
+ p->id = id_PowerUp;
+ p->x.w.i = o->x.w.i, p->y.w.i = o->y.w.i;
+ p->anim = o->anim;
+ }
+ p = FindFreeObj();
+ if (p) {
+ p->id = id_ExplosionItem;
+ p->routine += 2;
+ p->x.w.i = o->x.w.i, p->y.w.i = o->y.w.i;
+ }
+ V.objstate[2 + o->respawn] |= 1;
+ o->anim = 9;
+ DisplaySprite(o);
+ return;
+ }
+ }
+}
+
+void PowerUp(Obj *o) {
+ switch (o->routine) {
+ case 0: {
+ o->routine = 2;
+ o->gfx = ArtTile_Monitor;
+ o->render = 0x24;
+ o->priority = 3;
+ o->actwid = 8;
+ o->vy = -0x300;
+ uint8_t f = (uint8_t)(o->anim + 2);
+ o->frame = f;
+ o->map = map_frame(map_get(MAP_Monitor), f) + 2;
+ }
+ /* fall through */
+ case 2:
+ if (o->vy < 0) {
+ SpeedToPos(o);
+ o->vy += 0x18;
+ break;
+ }
+ o->routine += 2;
+ OW(o, 0x3E) = 30 - 1; /* (obTimeFrame, as a word) */
+ switch (o->anim) {
+ case 2:
+ V.lives++, V.f_lifecount++;
+ break;
+ case 3:
+ V.shoes = 1;
+ OW(player, 0x34) = 20 * 60;
+ V.sonspeedmax = 0x600 * 2, V.sonspeedacc = 0xC * 2, V.sonspeeddec = 0x80;
+ break;
+ case 4:
+ V.shield = 1;
+ OBJ(6)->id = id_ShieldItem;
+ break;
+ case 5:
+ V.invinc = 1;
+ OW(player, 0x32) = 20 * 60;
+ for (int i = 0; i < 4; i++) OBJ(8 + i)->id = id_ShieldItem, OBJ(8 + i)->anim = (uint8_t)(1 + i);
+ break;
+ case 6:
+ V.rings += 10;
+ V.f_ringcount |= 1;
+ if (V.rings < 100) break;
+ if (!(V.lifecount & 2)) {
+ V.lifecount |= 2;
+ V.lives++, V.f_lifecount++;
+ break;
+ }
+ if (V.rings < 200 || (V.lifecount & 4)) break;
+ V.lifecount |= 4;
+ V.lives++, V.f_lifecount++;
+ break;
+ default: break;
+ }
+ break;
+ default:
+ if (--OW(o, 0x3E) < 0) {
+ DeleteObject(o);
+ return;
+ }
+ break;
+ }
+ DisplaySprite(o);
+}
+
+/* ------------------------------------------------------------------ explosions, animals, points */
+#define exitem_pointsframe 0x3E
+#define animal_doublehop 0x29
+#define animal_id 0x30
+#define animal_speedX 0x32
+#define animal_speedY 0x34
+#define animal_prisondelay 0x36
+#define animal_pointsframe 0x3E
+
+static void exitem_animate(Obj *o) {
+ if ((int8_t)--o->timeframe < 0) {
+ o->timeframe = 7;
+ if (++o->frame == 5) {
+ DeleteObject(o);
+ return;
+ }
+ }
+ DisplaySprite(o);
+}
+
+void ExplosionItem(Obj *o) {
+ switch (o->routine) {
+ case 0: {
+ o->routine += 2;
+ Obj *a1 = FindFreeObj();
+ if (a1) {
+ a1->id = id_Animals;
+ a1->x.w.i = o->x.w.i, a1->y.w.i = o->y.w.i;
+ OW(a1, animal_pointsframe) = OW(o, exitem_pointsframe);
+ }
+ }
+ /* fall through */
+ case 2:
+ o->routine += 2;
+ o->map = map_get(MAP_ExplodeItem);
+ o->gfx = ArtTile_Explosion;
+ o->render = 4;
+ o->priority = 1;
+ o->coltype = 0;
+ o->actwid = 12;
+ o->timeframe = 7;
+ o->frame = 0;
+ /* fall through */
+ default: exitem_animate(o); return;
+ }
+}
+
+void Explosion(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_ExplodeBomb);
+ o->gfx = ArtTile_Explosion;
+ o->render = 4;
+ o->priority = 1;
+ o->coltype = 0;
+ o->actwid = 12;
+ o->timeframe = 7;
+ o->frame = 0;
+ return;
+ }
+ exitem_animate(o);
+}
+
+static const uint8_t anml_varindex[6][2] = {{0, 5}, {2, 3}, {6, 3}, {4, 5}, {4, 1}, {0, 1}};
+static const struct {
+ int16_t vx, vy;
+ uint8_t map;
+} anml_vars[7] = {{-0x200, -0x400, 0}, {-0x200, -0x300, 1}, {-0x180, -0x300, 0}, {-0x140, -0x180, 1},
+ {-0x1C0, -0x300, 2}, {-0x300, -0x400, 1}, {-0x280, -0x380, 2}};
+static const int animal_maps[3] = {MAP_Animal1, MAP_Animal2, MAP_Animal3};
+static const int16_t anml_endspeed[11][2] = {{-0x440, -0x400}, {-0x440, -0x400}, {-0x440, -0x400}, {-0x300, -0x400},
+ {-0x300, -0x400}, {-0x180, -0x300}, {-0x180, -0x300}, {-0x140, -0x180},
+ {-0x1C0, -0x300}, {-0x200, -0x300}, {-0x280, -0x380}};
+static const uint8_t anml_endmap[11] = {1, 1, 1, 0, 0, 0, 0, 1, 2, 1, 2};
+static const uint16_t anml_endvram[11] = {
+ ArtTile_Ending_Flicky, ArtTile_Ending_Flicky, ArtTile_Ending_Flicky, ArtTile_Ending_Rabbit, ArtTile_Ending_Rabbit,
+ ArtTile_Ending_Penguin, ArtTile_Ending_Penguin, ArtTile_Ending_Seal, ArtTile_Ending_Pig, ArtTile_Ending_Chicken,
+ ArtTile_Ending_Squirrel};
+
+static void anml_end_chkdel(Obj *o) {
+ int16_t d0 = (int16_t)(o->x.w.i - player->x.w.i);
+ if ((uint16_t)o->x.w.i >= (uint16_t)player->x.w.i && (int16_t)(d0 - (320 + 64)) < 0 && !(o->render & 0x80)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+}
+
+static void anml_animate2(Obj *o) {
+ if ((int8_t)--o->timeframe < 0) {
+ o->timeframe = 1;
+ o->frame = (uint8_t)((o->frame + 1) & 1);
+ }
+}
+
+static void anml_chkdel(Obj *o) {
+ if (obSubtype(o)) {
+ anml_end_chkdel(o);
+ return;
+ }
+ if (!(o->render & 0x80)) {
+ DeleteObject(o);
+ return;
+ }
+ DisplaySprite(o);
+}
+
+static void anml_normal(Obj *o) {
+ ObjectFall(o);
+ o->frame = 1;
+ if (o->vy >= 0) {
+ o->frame = 0;
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) o->y.w.i += d1, o->vy = OW(o, animal_speedY);
+ }
+ anml_chkdel(o);
+}
+
+static void anml_slow(Obj *o) {
+ SpeedToPos(o);
+ o->vy += 0x18;
+ if (o->vy >= 0) {
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ o->y.w.i += d1;
+ o->vy = OW(o, animal_speedY);
+ if (obSubtype(o) && obSubtype(o) != 0xA) {
+ o->vx = (int16_t)-o->vx;
+ o->render ^= 1;
+ }
+ }
+ }
+ anml_animate2(o);
+ anml_chkdel(o);
+}
+
+static int16_t close_to_sonic(Obj *o) { return (int16_t)(player->x.w.i - o->x.w.i - (160 + 24)); }
+
+static void anml_end_bounce(Obj *o) {
+ o->frame = 1;
+ if (o->vy < 0) return;
+ o->frame = 0;
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) o->y.w.i += d1, o->vy = OW(o, animal_speedY);
+}
+static void anml_face_sonic(Obj *o) {
+ o->render |= 1;
+ if ((uint16_t)o->x.w.i < (uint16_t)player->x.w.i) o->render &= (uint8_t)~1;
+}
+
+void Animals(Obj *o) {
+ switch (o->routine) {
+ case 0: {
+ if (obSubtype(o)) {
+ int d0 = obSubtype(o) * 2;
+ o->routine = (uint8_t)d0;
+ int k = (d0 - 0x14) / 2;
+ o->gfx = anml_endvram[k];
+ o->map = map_get(animal_maps[anml_endmap[k]]);
+ OW(o, animal_speedX) = o->vx = anml_endspeed[k][0];
+ OW(o, animal_speedY) = o->vy = anml_endspeed[k][1];
+ o->height = 12;
+ o->render = 4 | 1;
+ o->priority = 6;
+ o->actwid = 8;
+ o->timeframe = 7;
+ DisplaySprite(o);
+ return;
+ }
+ o->routine += 2;
+ int r = RandomNumber() & 1;
+ uint8_t id = anml_varindex[V.zone > 5 ? 0 : V.zone][r];
+ OB(o, animal_id) = id;
+ OW(o, animal_speedX) = anml_vars[id].vx;
+ OW(o, animal_speedY) = anml_vars[id].vy;
+ o->map = map_get(animal_maps[anml_vars[id].map]);
+ o->gfx = id & 1 ? ArtTile_Animal_2 : ArtTile_Animal_1;
+ o->height = 12;
+ o->render = 4 | 1;
+ o->priority = 6;
+ o->actwid = 8;
+ o->timeframe = 7;
+ o->frame = 2;
+ o->vy = -0x400;
+ if (V.bossstatus) {
+ o->routine = 0x12;
+ o->vx = 0;
+ DisplaySprite(o);
+ return;
+ }
+ Obj *a1 = FindFreeObj();
+ if (a1) {
+ a1->id = id_Points;
+ a1->x.w.i = o->x.w.i, a1->y.w.i = o->y.w.i;
+ a1->frame = (uint8_t)((uint16_t)OW(o, animal_pointsframe) >> 1);
+ }
+ DisplaySprite(o);
+ return;
+ }
+ case 2:
+ if (!(o->render & 0x80)) {
+ DeleteObject(o);
+ return;
+ }
+ ObjectFall(o);
+ if (o->vy >= 0) {
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ o->y.w.i += d1;
+ o->vx = OW(o, animal_speedX), o->vy = OW(o, animal_speedY);
+ o->frame = 1;
+ o->routine = (uint8_t)(OB(o, animal_id) * 2 + 4);
+ if (V.bossstatus && (V.vblank_count & 0x10)) {
+ o->vx = (int16_t)-o->vx;
+ o->render ^= 1;
+ }
+ }
+ }
+ DisplaySprite(o);
+ return;
+ case 4: case 8: case 0xA: case 0xC: case 0x10: anml_normal(o); return;
+ case 6: case 0xE: anml_slow(o); return;
+ case 0x12:
+ if (!(o->render & 0x80)) {
+ DeleteObject(o);
+ return;
+ }
+ if (!--OW(o, animal_prisondelay)) o->routine = 2, o->priority = 3;
+ DisplaySprite(o);
+ return;
+ case 0x14: case 0x16: /* fly to the left */
+ if ((uint16_t)close_to_sonic(o) >= 0x8000 || 1) {
+ int16_t d = close_to_sonic(o);
+ if ((uint16_t)(player->x.w.i - o->x.w.i) >= 160 + 24) {
+ (void)d;
+ anml_end_chkdel(o);
+ return;
+ }
+ }
+ o->vx = OW(o, animal_speedX), o->vy = OW(o, animal_speedY);
+ o->routine = 0xE;
+ anml_slow(o);
+ return;
+ case 0x18: /* stay and face Sonic, slowly */
+ if (close_to_sonic(o) >= 0) break;
+ o->vx = 0, OW(o, animal_speedX) = 0;
+ SpeedToPos(o);
+ o->vy += 0x18;
+ anml_end_bounce(o);
+ anml_face_sonic(o);
+ anml_animate2(o);
+ break;
+ case 0x1A: /* hop to the left */
+ if (close_to_sonic(o) >= 0) break;
+ o->vx = OW(o, animal_speedX), o->vy = OW(o, animal_speedY);
+ o->routine = 4;
+ anml_normal(o);
+ return;
+ case 0x1C: case 0x20: case 0x24: /* stay and face Sonic */
+ if (close_to_sonic(o) >= 0) break;
+ o->vx = 0, OW(o, animal_speedX) = 0;
+ ObjectFall(o);
+ anml_end_bounce(o);
+ anml_face_sonic(o);
+ break;
+ case 0x1E: case 0x22: /* hop around */
+ if (close_to_sonic(o) >= 0) break;
+ ObjectFall(o);
+ o->frame = 1;
+ if (o->vy >= 0) {
+ o->frame = 0;
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ o->vx = (int16_t)-o->vx;
+ o->render ^= 1;
+ o->y.w.i += d1;
+ o->vy = OW(o, animal_speedY);
+ }
+ }
+ break;
+ case 0x26: /* fly, turning every other bounce */
+ if (close_to_sonic(o) >= 0) break;
+ SpeedToPos(o);
+ o->vy += 0x18;
+ if (o->vy >= 0) {
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ OB(o, animal_doublehop) = (uint8_t)~OB(o, animal_doublehop);
+ if (!OB(o, animal_doublehop)) o->vx = (int16_t)-o->vx, o->render ^= 1;
+ o->y.w.i += d1;
+ o->vy = OW(o, animal_speedY);
+ }
+ }
+ anml_animate2(o);
+ break;
+ case 0x28: /* hop, turning every other bounce */
+ ObjectFall(o);
+ o->frame = 1;
+ if (o->vy >= 0) {
+ o->frame = 0;
+ int16_t d1 = ObjFloorDist(o, NULL);
+ if (d1 < 0) {
+ OB(o, animal_doublehop) = (uint8_t)~OB(o, animal_doublehop);
+ if (!OB(o, animal_doublehop)) o->vx = (int16_t)-o->vx, o->render ^= 1;
+ o->y.w.i += d1;
+ o->vy = OW(o, animal_speedY);
+ }
+ }
+ break;
+ default: break;
+ }
+ anml_end_chkdel(o);
+}
+
+void Points(Obj *o) {
+ if (o->routine == 0) {
+ o->routine += 2;
+ o->map = map_get(MAP_Poi);
+ o->gfx = ArtTile_Points | 0x2000;
+ o->render = 4;
+ o->priority = 1;
+ o->actwid = 8;
+ o->vy = -0x300;
+ }
+ if (o->vy >= 0) DeleteObject(o);
+ else SpeedToPos(o), o->vy += 0x18;
+ DisplaySprite(o);
+}
diff --git a/games/sonic/src/objects.c b/games/sonic/src/objects.c
index 709c8157..95b4981e 100644
--- a/games/sonic/src/objects.c
+++ b/games/sonic/src/objects.c
@@ -5,4 +5,29 @@
const ObjFn obj_index[0x8D + 1] = {
[id_SonicPlayer] = SonicPlayer,
+ [id_Signpost] = Signpost,
+ [id_Rings] = Rings,
+ [id_RingLoss] = RingLoss,
+ [id_Monitor] = Monitor,
+ [id_PowerUp] = PowerUp,
+ [id_ExplosionItem] = ExplosionItem,
+ [id_Explosion] = Explosion,
+ [id_Animals] = Animals,
+ [id_Points] = Points,
+ [id_HUD] = HUD,
+ [id_TitleCard] = TitleCard,
+ [id_GameOverCard] = GameOverCard,
+ [id_GotThroughCard] = GotThroughCard,
+ [id_Lamppost] = Lamppost,
+ [id_ShieldItem] = ShieldItem,
+ [id_Springs] = Springs,
+ [id_Spikes] = Spikes,
+ [id_GiantRing] = GiantRing,
+ [id_RingFlash] = RingFlash,
+ [id_HiddenBonus] = HiddenBonus,
+ [id_Prison] = Prison,
+ [id_Invisibarrier] = Invisibarrier,
+ [id_Button] = Button,
+ [id_WaterSound] = WaterSound,
+ [id_EdgeWalls] = EdgeWalls,
};
diff --git a/games/sonic/src/objs.h b/games/sonic/src/objs.h
index ba86757d..f1e323b8 100644
--- a/games/sonic/src/objs.h
+++ b/games/sonic/src/objs.h
@@ -1,5 +1,93 @@
-/* The objects' code, by file (each object is the original's object of that id). */
+/* The objects' code, by file (each object is the original's object of that id), and what they share. */
#ifndef S1_OBJS_H
#define S1_OBJS_H
#include "s1.h"
+
+/* tile word flags */
+#define Tile_Prio 0x8000
+#define Tile_Pal2 0x2000
+#define Tile_Pal3 0x4000
+#define Tile_Pal4 0x6000
+
+/* the zone and act as one word, as the original's v_zone_act */
+#define ZONE_ACT ((uint16_t)(V.zone << 8 | V.act))
+static inline void set_zone_act(uint16_t za) { V.zone = (uint8_t)(za >> 8), V.act = (uint8_t)za; }
+
+/* obTimeFrame and obDelayAni read together as a word */
+static inline uint16_t timew(const Obj *o) { return (uint16_t)(o->timeframe << 8 | o->delayani); }
+static inline void set_timew(Obj *o, uint16_t v) { o->timeframe = (uint8_t)(v >> 8), o->delayani = (uint8_t)v; }
+
+/* the high and low bytes of an object's own word (move.b of a word's address reads its high byte) */
+#define OWH(o, off) ((uint8_t)(OUW(o, off) >> 8))
+
+/* what touching an object does (obColType): its hitbox, and its kind */
+enum {
+ col_none = 0, col_badnik = 0, col_boss = 0, col_item = 0x40, col_hurt = 0x80, col_special = 0xC0,
+ col_40x40 = 1, col_24x40, col_40x24, col_8x32, col_24x36, col_32x32, col_12x12, col_48x24, col_24x32,
+ col_32x24, col_16x16, col_40x32, col_40x16, col_28x28, col_48x48, col_80x32, col_32x48, col_16x32,
+ col_64x224, col_128x64, col_256x64, col_64x64, col_16x16_alt, col_8x8, col_64x16, col_24x24, col_16x8,
+ col_48x8, col_80x8, col_8x16, col_8x48, col_8x80, col_8x64, col_48x48_alt, col_24x48, col_144x16
+};
+
+/* a boss's base position (longs at $30 and $38), its hits left and flash time */
+#define OFIX(o, off) (*(fix_t *)(void *)&(o)->s.b[(off) - 0x28])
+#define obBossX(o) OFIX(o, 0x30)
+#define obBossY(o) OFIX(o, 0x38)
+#define obBossHits(o) ((o)->colprop)
+#define obBossFlash 0x3E
+
+#define vblank_byte ((uint8_t)V.vblank_count)
+
+/* Sonic's own variables */
+#define flashtime 0x30
+#define invtime 0x32
+#define shoetime 0x34
+#define angleright 0x36
+#define angleleft 0x37
+#define sticktoconvex 0x38
+#define restartime 0x3A
+#define jumping 0x3C
+#define standonobject 0x3D
+#define locktime 0x3E
+#define sonic_solid_width 11
+
+/* the animation of Sonic's ids (id_Walk, id_Roll...) are in s1const.h */
+
+/* cards.c */
+void TitleCard(Obj *o);
+void GameOverCard(Obj *o);
+void GotThroughCard(Obj *o);
+void Signpost(Obj *o);
+void GotThroughAct(void);
+void Lamppost(Obj *o);
+void Lamp_LoadInfo(void);
+
+/* items.c, hud.c */
+void Rings(Obj *o);
+void RingLoss(Obj *o);
+void Monitor(Obj *o);
+void PowerUp(Obj *o);
+void ExplosionItem(Obj *o);
+void Explosion(Obj *o);
+void Animals(Obj *o);
+void Points(Obj *o);
+void HUD(Obj *o);
+void ContScrCounter(uint32_t v);
+
+/* common.c */
+void ShieldItem(Obj *o);
+void Springs(Obj *o);
+void Spikes(Obj *o);
+void GiantRing(Obj *o);
+void RingFlash(Obj *o);
+void HiddenBonus(Obj *o);
+void Prison(Obj *o);
+void SmashObject(Obj *o, const int16_t *speeds, int n, int16_t gravity);
+void BossDefeated(Obj *o);
+void BossMove(Obj *o);
+void Invisibarrier(Obj *o);
+void Button(Obj *o);
+void PushBlock(Obj *o);
+void WaterSound(Obj *o);
+void EdgeWalls(Obj *o);
#endif
diff --git a/games/sonic/src/s1.h b/games/sonic/src/s1.h
index 544abb23..c248e5d2 100644
--- a/games/sonic/src/s1.h
+++ b/games/sonic/src/s1.h
@@ -194,7 +194,7 @@ typedef struct {
uint8_t timemin, timesec, timecent; /* v_time: (unused), minutes, seconds, frames */
uint32_t score;
uint8_t shield, invinc, shoes, unused1;
- uint8_t lastlamp;
+ uint8_t lastlamp, lastlamp_dup;
int16_t lamp_xpos, lamp_ypos;
uint16_t lamp_rings;
uint8_t lamp_timemin, lamp_timesec, lamp_timecent;
diff --git a/games/sonic/src/sonic.c b/games/sonic/src/sonic.c
index 93241b35..9d6332e3 100644
--- a/games/sonic/src/sonic.c
+++ b/games/sonic/src/sonic.c
@@ -8,6 +8,7 @@
#include "s1.h"
#include "collide.h"
#include "game.h"
+#include "objs.h"
#define son_maxspeed 0x600
#define son_acceleration 0xC
@@ -18,16 +19,6 @@
#define sonic_roll_width 7
#define sonic_roll_height 14
#define sonic_quick_size 10
-#define flashtime 0x30
-#define invtime 0x32
-#define shoetime 0x34
-#define angleright 0x36
-#define angleleft 0x37
-#define sticktoconvex 0x38
-#define restartime 0x3A
-#define jumping 0x3C
-#define standonobject 0x3D
-#define locktime 0x3E
uint16_t tracksonic[64][2]; /* v_tracksonic: where he was, for the invincibility stars */
diff --git a/games/sonic/src/stubs.c b/games/sonic/src/stubs.c
index 4439d4f5..ea816842 100644
--- a/games/sonic/src/stubs.c
+++ b/games/sonic/src/stubs.c
@@ -4,12 +4,9 @@
void DebugMode(Obj *o) { (void)o; }
void ResumeMusic(void) { V.air = 30; }
-void HUD_Update(void) {}
-void Hud_Base(void) {}
void AnimateLevelGfx(void) {}
void LZWaterFeatures(void) {}
void PalCycle_SS(void) {}
-void Lamp_LoadInfo(void) {}
void pause_overlay_draw(void) {}
void PauseGame(void) {}
void save_restore_copy(void) {}
@@ -19,7 +16,7 @@ void game_init(void) {
V.gamemode = id_Level;
V.lives = 3;
#ifdef HOST
- if (host_level >= 0) V.zone = (uint8_t)host_level, V.act = (uint8_t)host_act;
+ if (host_level >= 0) V.zone = (uint8_t)host_level, V.act = (uint8_t)host_act, AddPLC(plcid_Main);
#endif
}
void GM_Sega(void) {}
diff --git a/games/sonic/src/vdp.c b/games/sonic/src/vdp.c
index 671b2062..dc0da411 100644
--- a/games/sonic/src/vdp.c
+++ b/games/sonic/src/vdp.c
@@ -20,7 +20,7 @@ bool vdp_line_scroll = true;
int vdp_hint_line = -1;
bool vdp_display = true;
-static const uint8_t blank_pattern[32];
+const uint8_t vdp_blank_pattern[32];
#define PATTERNS ((const uint8_t *)DATA(D_TILES))
#define ARTS DATAW(D_ARTS)
@@ -34,7 +34,7 @@ void vdp_clear_planes(void) {
memset(plane_a, 0, sizeof plane_a);
memset(plane_b, 0, sizeof plane_b);
for (int i = 0; i < 2048; i++)
- if (!vram[i]) vram[i] = blank_pattern;
+ if (!vram[i]) vram[i] = vdp_blank_pattern;
}
/* a rectangle of tile words into a plane, each plus add */