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.

Hollow Knight: focus to heal (Spell Control)

Mason Chene08bdd9842fd
diff --git a/games/hollowknight/Makefile b/games/hollowknight/Makefile
index d16ede5f..f360e21d 100644
--- a/games/hollowknight/Makefile
+++ b/games/hollowknight/Makefile
@@ -9,7 +9,7 @@ CC = arm-none-eabi-gcc
NWLINK ?= npx --yes -- [email protected]
STRIP ?= arm-none-eabi-strip --strip-unneeded
BUILD_DIR = output
-GAME = src/res.c src/tex.c src/room.c src/gfx.c src/phys.c src/anim.c src/hero.c src/camera.c src/game.c src/world.c src/obj.c src/enemy.c src/hud.c src/lzma/LzmaDec.c
+GAME = src/res.c src/tex.c src/room.c src/gfx.c src/phys.c src/anim.c src/hero.c src/camera.c src/game.c src/world.c src/obj.c src/enemy.c src/hud.c src/spell.c src/lzma/LzmaDec.c
SRC = $(GAME) src/plat_eadk.c src/main.c
HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index 9cff3e3c..4411ed3e 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -170,6 +170,20 @@ void hero_hazard_respawn(void); /* HeroController.HazardRespawn */
void hero_check_damage(void); /* HeroBox: the hazards and enemies touching it */
void hero_late_update(void); /* (HeroBox.LateUpdate: a buffered hit) */
void hero_soul_gain(void); /* a nail's hit on an enemy */
+void hero_add_health(int amount);
+void hero_relinquish_control(void); /* RelinquishControl, RegainControl */
+void hero_regain_control(void);
+void hero_stop_anim_control(void); /* StopAnimationControl, StartAnimationControl */
+void hero_start_anim_control(void);
+bool hero_can_focus(void);
+bool hero_can_cast(void);
+
+/* the Spell Control FSM (spell.c): focus, spells */
+void spell_reset(void);
+void spell_update(void);
+void spell_cancel(void);
+bool spell_busy(void);
+void spell_cast(bool up, bool down);
void hero_add_geo(int amount);
/* the nail's slashes (NailSlash), children of the Knight */
diff --git a/games/hollowknight/src/hero.c b/games/hollowknight/src/hero.c
index 8be28b64..ec44b8ec 100644
--- a/games/hollowknight/src/hero.c
+++ b/games/hollowknight/src/hero.c
@@ -754,6 +754,7 @@ void hero_init(float x, float y, bool facing_right) {
Hero *h = &g_hero;
static BodyEvent events[MAX_EVENTS];
memset(h, 0, sizeof *h);
+ spell_reset();
h->entry_gate = -1;
h->body.events = events;
h->body.x = x, h->body.y = y;
@@ -797,6 +798,7 @@ void hero_update(void) {
invulnerable_tick();
respawn_tick();
entering_tick();
+ spell_update();
if (h->anim_control) animation();
h->anim.events = 0;
anim_update(&h->anim, DT);
@@ -942,6 +944,7 @@ void fx_dash_burst(float x, float y, bool facing_right, bool on_ground) { (void)
void hero_leave_scene(int gate) {
Hero *h = &g_hero;
+ spell_cancel(); /* (LEAVING SCENE) */
h->accepting_input = false; /* (IgnoreInputWithoutReset) */
h->hard_landing_timer = 0;
set_state(HS_NO_INPUT);
@@ -1224,6 +1227,7 @@ void hero_take_damage(int side, int damage, int hazard) {
Hero *h = &g_hero;
if (damage <= 0) return;
if (can_take_damage()) {
+ spell_cancel(); /* (HERO DAMAGED) */
if (h->damage_mode == DAMAGE_HAZARD_ONLY && hazard == HAZ_NORMAL) return;
cancel_attack();
if (h->cs.recoiling_left || h->cs.recoiling_right) cancel_recoil_horizontal();
@@ -1341,6 +1345,63 @@ void hero_soul_gain(void) { add_mp_charge(g_pd.mp < g_pd.max_mp ? 11 : 6); }
void hero_add_geo(int amount) { g_pd.geo += amount; }
+void hero_add_health(int amount) {
+ g_pd.health = (int8_t)(g_pd.health + amount >= g_pd.max_health ? g_pd.max_health : g_pd.health + amount);
+}
+
+/* ---------------------------------------------------------------- control (the FSMs take it and give it back) */
+void hero_relinquish_control(void) {
+ Hero *h = &g_hero;
+ if (h->control_relinquished || h->cs.dead) return;
+ reset_input();
+ reset_motion();
+ h->accepting_input = false;
+ h->control_relinquished = true;
+ reset_look();
+ reset_attacks();
+ h->touching_wall_l = h->touching_wall_r = false;
+}
+
+void hero_regain_control(void) {
+ Hero *h = &g_hero;
+ h->accepting_input = true;
+ h->state = HS_IDLE;
+ if (!h->control_relinquished || h->cs.dead) return;
+ affected_by_gravity(true);
+ /* SetStartingMotionState */
+ h->move_input = 0;
+ h->cs.touching_wall = false;
+ if (hero_touching_ground()) h->cs.on_ground = true, set_state(HS_GROUNDED), h->air_dashed = false;
+ else h->cs.on_ground = false, set_state(HS_AIRBORNE);
+ h->control_relinquished = false;
+}
+
+void hero_stop_anim_control(void) { g_hero.anim_control = false; }
+
+void hero_start_anim_control(void) {
+ Hero *h = &g_hero;
+ h->anim_state = h->state;
+ if (!h->anim_control) {
+ h->anim_control = true;
+ play_idle();
+ }
+}
+
+/* CanFocus, CanCast */
+bool hero_can_focus(void) {
+ const Hero *h = &g_hero;
+ return !g_game.paused && h->state != HS_NO_INPUT && !h->cs.dashing && !(h->cs.attacking && h->attack_time < ATTACK_RECOVERY_TIME) &&
+ !h->cs.recoiling && h->cs.on_ground && !h->cs.transitioning && !h->cs.recoil_frozen && !h->cs.hazard_death &&
+ !h->cs.hazard_respawning && h->accepting_input;
+}
+
+bool hero_can_cast(void) {
+ const Hero *h = &g_hero;
+ return !g_game.paused && !h->cs.dashing && h->state != HS_NO_INPUT && !(h->cs.attacking && h->attack_time < ATTACK_RECOVERY_TIME) &&
+ !h->cs.recoiling && !h->cs.recoil_frozen && !h->cs.transitioning && !h->cs.hazard_death && !h->cs.hazard_respawning &&
+ h->accepting_input;
+}
+
void hero_late_update(void) {
Hero *h = &g_hero;
if (h->hit_buffered) {
diff --git a/games/hollowknight/src/spell.c b/games/hollowknight/src/spell.c
new file mode 100644
index 00000000..924da7dd
--- /dev/null
+++ b/games/hollowknight/src/spell.c
@@ -0,0 +1,187 @@
+/* The Knight's Spell Control FSM: holding the cast key focuses soul into health; a tap casts a spell. */
+#include "game.h"
+
+#define DT 0.02f
+#define BUTTON_DOWN_TIME 0.25f
+#define FOCUS_START_TIME 0.25f
+#define TIME_PER_MP_DRAIN 0.027f /* (Time Per MP Drain UnCH: no charm) */
+#define GRACE_TIME 0.45f
+#define MP_COST 33
+
+enum {
+ SP_INACTIVE, SP_BUTTON_DOWN, SP_CANCEL, SP_FOCUS_START, SP_FOCUS, SP_FOCUS_HEAL, SP_FOCUS_GET_FINISH, SP_FOCUS_CANCEL,
+ SP_BACK_IN
+};
+
+static struct {
+ uint8_t state;
+ float t, grace;
+ bool pressed_up, pressed_down, refocusing, didnt_hit_full, can_cancel;
+ int start_mp;
+ /* (HeroController's MP drain) */
+ bool drain;
+ float drain_timer;
+ int drained;
+} sp;
+
+static bool cast_held(void) { return g_hero.keys & K_SPELL; }
+static bool cast_pressed(void) { return (g_hero.keys & K_SPELL) && !(g_hero.prev_keys & K_SPELL); }
+
+void spell_reset(void) { memset(&sp, 0, sizeof sp); }
+
+/* StartMPDrain, StopMPDrain; the drain itself is HeroController.Update's */
+static void drain_start(void) { sp.drain = true, sp.drain_timer = 0, sp.drained = 0; }
+
+static void focus_end_common(void) {
+ sp.drain = false;
+ g_hero.cs.focusing = false;
+}
+
+static void regain_control(void) {
+ /* Regain Control, then Back In?: holding still (and not stopped at full health) goes on focusing */
+ g_hero.cs.focusing = false;
+ hero_regain_control();
+ hero_start_anim_control();
+ sp.can_cancel = false;
+ sp.state = SP_BACK_IN;
+}
+
+static void can_focus(void) {
+ /* Can Focus? */
+ if (g_pd.mp < MP_COST || !hero_can_focus()) {
+ sp.didnt_hit_full = false;
+ sp.state = SP_CANCEL, sp.t = DT; /* (Cancel: the next frame) */
+ return;
+ }
+ /* Start Slug Anim, Focus Start */
+ sp.can_cancel = true;
+ sp.grace = 0, sp.didnt_hit_full = true, sp.refocusing = false;
+ g_hero.cs.focusing = true;
+ hero_relinquish_control();
+ hero_stop_anim_control();
+ g_hero.body.vx = 0;
+ anim_play(&g_hero.anim, CLIP_KNIGHT_FOCUS);
+ sp.state = SP_FOCUS_START, sp.t = FOCUS_START_TIME;
+}
+
+static void focus_cancel(void) {
+ focus_end_common();
+ anim_play_from_frame(&g_hero.anim, CLIP_KNIGHT_FOCUS_END, 0);
+ sp.state = SP_FOCUS_CANCEL;
+}
+
+static void focus_enter(void) {
+ sp.start_mp = g_pd.mp;
+ sp.state = SP_FOCUS;
+ drain_start();
+}
+
+/* Grace Check: let go soon after starting (or after a heal), the soul is given back */
+static void grace_check(void) {
+ float limit = sp.refocusing ? GRACE_TIME : 0.2f;
+ if (sp.grace <= limit) g_pd.mp = (int16_t)sp.start_mp;
+ focus_cancel();
+}
+
+void spell_update(void) {
+ Hero *h = &g_hero;
+ /* HeroController.Update's MP drain */
+ if (sp.drain) {
+ sp.drain_timer += DT;
+ while (sp.drain_timer >= TIME_PER_MP_DRAIN) {
+ sp.drained++;
+ sp.drain_timer -= TIME_PER_MP_DRAIN;
+ if (g_pd.mp > 0) g_pd.mp--;
+ if (sp.drained == MP_COST && sp.state == SP_FOCUS) {
+ /* FOCUS COMPLETED: Spore Cloud, Set HP Amount, Focus Heal */
+ sp.drain = false;
+ sp.grace = 0, sp.refocusing = true;
+ anim_play_from_frame(&h->anim, CLIP_KNIGHT_FOCUS_GET, 0);
+ cam_shake(SHAKE_ENEMY_KILL);
+ hero_add_health(1);
+ sp.state = SP_FOCUS_HEAL, sp.t = 0.2f;
+ }
+ }
+ }
+ switch (sp.state) {
+ case SP_INACTIVE:
+ sp.can_cancel = false;
+ if (cast_pressed()) {
+ sp.state = SP_BUTTON_DOWN, sp.t = BUTTON_DOWN_TIME;
+ sp.pressed_up = sp.pressed_down = false;
+ } else if (cast_held())
+ can_focus(); /* (ALREADY DOWN) */
+ break;
+ case SP_BUTTON_DOWN:
+ if (h->keys & K_UP) sp.pressed_up = true;
+ if (h->keys & K_DOWN) sp.pressed_down = true;
+ if (!cast_held()) {
+ /* Can Cast?, Spell Choice: no spells yet, back to Inactive */
+ sp.state = SP_INACTIVE;
+ spell_cast(sp.pressed_up, sp.pressed_down);
+ } else if ((sp.t -= DT) <= 0)
+ can_focus();
+ break;
+ case SP_CANCEL:
+ if ((sp.t -= DT) <= 0) sp.state = SP_INACTIVE;
+ break;
+ case SP_FOCUS_START:
+ h->body.vx = 0;
+ if (!cast_held() || !h->cs.on_ground) focus_cancel();
+ else if ((sp.t -= DT) <= 0) focus_enter();
+ break;
+ case SP_FOCUS:
+ h->body.vx = 0;
+ sp.grace += DT;
+ if (!cast_held() || !h->cs.on_ground) {
+ focus_end_common();
+ grace_check();
+ }
+ break;
+ case SP_FOCUS_HEAL:
+ h->body.vx = 0;
+ if ((sp.t -= DT) <= 0) {
+ /* Full HP? */
+ if (g_pd.health >= g_pd.max_health || g_pd.mp < MP_COST) {
+ sp.didnt_hit_full = false;
+ focus_end_common();
+ anim_play_from_frame(&h->anim, CLIP_KNIGHT_FOCUS_GET_ONCE, 0);
+ sp.state = SP_FOCUS_GET_FINISH, sp.t = 0.23f;
+ } else
+ focus_enter();
+ }
+ break;
+ case SP_FOCUS_GET_FINISH:
+ if ((sp.t -= DT) <= 0) regain_control();
+ break;
+ case SP_FOCUS_CANCEL:
+ if (h->anim.events & ANIM_DONE || !h->anim.playing) regain_control();
+ break;
+ case SP_BACK_IN:
+ sp.state = SP_INACTIVE;
+ if (cast_held() && sp.didnt_hit_full) can_focus();
+ break;
+ }
+}
+
+/* the FSM's global events: the Knight hit (Cancel All), leaving the room (Cancel Some) */
+void spell_cancel(void) {
+ if (sp.state == SP_INACTIVE) return;
+ bool focusing = sp.state >= SP_FOCUS_START && sp.state <= SP_FOCUS_CANCEL;
+ sp.drain = false;
+ sp.didnt_hit_full = false;
+ g_hero.cs.focusing = false;
+ sp.state = SP_INACTIVE;
+ if (focusing) hero_start_anim_control();
+}
+
+bool spell_busy(void) { return sp.state != SP_INACTIVE && sp.state != SP_BUTTON_DOWN; }
+
+/* Can Cast?, Spell Choice: up a scream, down a quake, else a fireball (the ones the Knight has) */
+void spell_cast(bool up, bool down) {
+ (void)up, (void)down;
+ if (g_pd.mp < MP_COST || !hero_can_cast()) return;
+ if (g_pd.fireball_level > 0 && !up && !down) {
+ /* (Vengeful Spirit: fireball.c) */
+ }
+}
diff --git a/games/hollowknight/tests/play.c b/games/hollowknight/tests/play.c
index da9d5641..a41ed1f0 100644
--- a/games/hollowknight/tests/play.c
+++ b/games/hollowknight/tests/play.c
@@ -54,6 +54,8 @@ int main(int argc, char **argv) {
/* the game with scripted keys: "KEYS*TICKS,..." (L R U D J jump A attack S dash F focus/cast, _ none) */
game_new();
g_pd.can_dash = true;
+ if (getenv("HKMP")) g_pd.mp = (int16_t)atoi(getenv("HKMP"));
+ if (getenv("HKHEALTH")) g_pd.health = (int8_t)atoi(getenv("HKHEALTH"));
game_enter(id, px, py, true);
int tick = 0;
for (const char *p = play; *p;) {
@@ -70,6 +72,7 @@ int main(int argc, char **argv) {
printf("%4d keys %02x pos %.3f,%.3f v %.3f,%.3f state %d ground %d jump %d fall %d clip %d frame %d cam %.2f,%.2f\n", tick,
keys, g_hero.body.x, g_hero.body.y, g_hero.body.vx, g_hero.body.vy, g_hero.state, g_hero.cs.on_ground,
g_hero.cs.jumping, g_hero.cs.falling, g_hero.anim.clip, g_hero.anim.frame, g_cam_x, g_cam_y);
+ if (trace && getenv("HKSTATS")) printf(" health %d mp %d geo %d\n", g_pd.health, g_pd.mp, (int)g_pd.geo);
if (trace && getenv("ENEMIES")) {
extern void enemies_debug(void);
enemies_debug();