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: benches, saves, death and the shade

Mason Chenb6fdbf27ab37
diff --git a/games/hollowknight/Makefile b/games/hollowknight/Makefile
index 4af86547..04e82cce 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/spell.c src/text.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/text.c src/npc.c src/save.c src/death.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/data.bin b/games/hollowknight/src/data.bin
index 1a9a716d..5b09481d 100644
Binary files a/games/hollowknight/src/data.bin and b/games/hollowknight/src/data.bin differ
diff --git a/games/hollowknight/src/data.h b/games/hollowknight/src/data.h
index 675b4611..2eaf1a46 100644
--- a/games/hollowknight/src/data.h
+++ b/games/hollowknight/src/data.h
@@ -14,6 +14,7 @@
#define SEC_STR 11
#define SEC_TEXT 12
#define SEC_FONT 13
+#define SEC_PHASH 14
#define NUM_ROOMS 41
#define ROOM_TUTORIAL_01 0
#define ROOM_TOWN 1
@@ -136,68 +137,93 @@
#define CLIP_KNIGHT_COLLECT_HEART_PIECE_END 77
#define CLIP_KNIGHT_COLLECT_STANDTOIDLE 78
#define CLIP_KNIGHT_GETUPTOIDLE 79
-#define CLIP_CRAWLER_WALK 80
-#define CLIP_CRAWLER_TURN 81
-#define CLIP_CRAWLER_IDLE 82
-#define CLIP_CRAWLER_DEATH_AIR 83
-#define CLIP_CRAWLER_DEATH_LAND 84
-#define CLIP_BUZZER_CHASE 85
-#define CLIP_BUZZER_IDLE 86
-#define CLIP_BUZZER_STARTLE 87
-#define CLIP_BUZZER_DEATH_AIR 88
-#define CLIP_BUZZER_DEATH_LAND 89
-#define CLIP_BUZZER_FLY 90
-#define CLIP_BUZZER_TURNTOFLY 91
-#define CLIP_BUZZER_TURNTOIDLE 92
-#define CLIP_GEOROCK_IDLE_1 93
-#define CLIP_GEOROCK_GLEAM_1 94
-#define CLIP_GEOROCK_BROKEN_1 95
-#define CLIP_GEOROCK_IDLE_2 96
-#define CLIP_GEOROCK_GLEAM_2 97
-#define CLIP_GEOROCK_BROKEN_2 98
-#define CLIP_CHEST_IDLE 99
-#define CLIP_CHEST_OPEN 100
-#define CLIP_CHEST_BURST 101
-#define CLIP_CHEST_OPENED 102
-#define CLIP_HUD_HEALTH_EMPTY 103
-#define CLIP_HUD_HEALTH_IDLE 104
-#define CLIP_HUD_HEALTH_BREAK 105
-#define CLIP_HUD_HEALTH_REFILL 106
-#define CLIP_HUD_HEALTH_APPEAR 107
-#define CLIP_HUD_BLUE_APPEAR 108
-#define CLIP_HUD_BLUE_IDLE 109
-#define CLIP_HUD_BLUE_BREAK 110
-#define CLIP_HUD_HUD_FRAME 111
-#define CLIP_HUD_HUD_FRAME_IDLE 112
-#define CLIP_HUD_COIN_APPEAR 113
-#define CLIP_HUD_COIN_IDLE 114
-#define CLIP_HUD_COIN_GET 115
-#define CLIP_HUD_SOUL_BURST 116
-#define CLIP_LIQUID_FILL 117
-#define CLIP_LIQUID_IDLE 118
-#define CLIP_LIQUID_SHRINK 119
-#define CLIP_LIQUID_DRAIN 120
-#define CLIP_GEO_SMALL_IDLE 121
-#define CLIP_GEO_SMALL_AIR 122
-#define CLIP_GEO_MED_IDLE 123
-#define CLIP_GEO_MED_AIR 124
-#define CLIP_GEO_LARGE_IDLE 125
-#define CLIP_GEO_LARGE_AIR 126
-#define CLIP_DIALOGUE_ARROW_UP 127
-#define CLIP_DIALOGUE_ARROW_DOWN 128
-#define CLIP_DIALOGUE_STOP_UP 129
-#define CLIP_DIALOGUE_STOP_DOWN 130
-#define CLIP_DIALOGUE_FLEUR_TOP_UP 131
-#define CLIP_DIALOGUE_FLEUR_TOP_DOWN 132
-#define CLIP_DIALOGUE_FLEUR_BOT_UP 133
-#define CLIP_DIALOGUE_FLEUR_BOT_DOWN 134
-#define CLIP_PROMPT_UP 135
-#define CLIP_PROMPT_DOWN 136
-#define CLIP_PROMPT_BLANK 137
-#define SPRITE_HERO_LIGHT 577
-#define SPRITE_DIALOGUE_BACKBOARD 578
-#define SPRITE_PROMPT_SHADOW 579
-#define SPRITE_DIGIT0 760
+#define CLIP_KNIGHT_DEATH_HEAD_CRACKED 80
+#define CLIP_CRAWLER_WALK 81
+#define CLIP_CRAWLER_TURN 82
+#define CLIP_CRAWLER_IDLE 83
+#define CLIP_CRAWLER_DEATH_AIR 84
+#define CLIP_CRAWLER_DEATH_LAND 85
+#define CLIP_BUZZER_CHASE 86
+#define CLIP_BUZZER_IDLE 87
+#define CLIP_BUZZER_STARTLE 88
+#define CLIP_BUZZER_DEATH_AIR 89
+#define CLIP_BUZZER_DEATH_LAND 90
+#define CLIP_BUZZER_FLY 91
+#define CLIP_BUZZER_TURNTOFLY 92
+#define CLIP_BUZZER_TURNTOIDLE 93
+#define CLIP_GEOROCK_IDLE_1 94
+#define CLIP_GEOROCK_GLEAM_1 95
+#define CLIP_GEOROCK_BROKEN_1 96
+#define CLIP_GEOROCK_IDLE_2 97
+#define CLIP_GEOROCK_GLEAM_2 98
+#define CLIP_GEOROCK_BROKEN_2 99
+#define CLIP_CHEST_IDLE 100
+#define CLIP_CHEST_OPEN 101
+#define CLIP_CHEST_BURST 102
+#define CLIP_CHEST_OPENED 103
+#define CLIP_HUD_HEALTH_EMPTY 104
+#define CLIP_HUD_HEALTH_IDLE 105
+#define CLIP_HUD_HEALTH_BREAK 106
+#define CLIP_HUD_HEALTH_REFILL 107
+#define CLIP_HUD_HEALTH_APPEAR 108
+#define CLIP_HUD_BLUE_APPEAR 109
+#define CLIP_HUD_BLUE_IDLE 110
+#define CLIP_HUD_BLUE_BREAK 111
+#define CLIP_HUD_HUD_FRAME 112
+#define CLIP_HUD_HUD_FRAME_IDLE 113
+#define CLIP_HUD_HUD_FRAME_CRACKAPPEAR 114
+#define CLIP_HUD_HUD_FRAME_CRACKED 115
+#define CLIP_HUD_COIN_APPEAR 116
+#define CLIP_HUD_COIN_IDLE 117
+#define CLIP_HUD_COIN_GET 118
+#define CLIP_HUD_SOUL_BURST 119
+#define CLIP_LIQUID_FILL 120
+#define CLIP_LIQUID_IDLE 121
+#define CLIP_LIQUID_SHRINK 122
+#define CLIP_LIQUID_DRAIN 123
+#define CLIP_GEO_SMALL_IDLE 124
+#define CLIP_GEO_SMALL_AIR 125
+#define CLIP_GEO_MED_IDLE 126
+#define CLIP_GEO_MED_AIR 127
+#define CLIP_GEO_LARGE_IDLE 128
+#define CLIP_GEO_LARGE_AIR 129
+#define CLIP_DIALOGUE_ARROW_UP 130
+#define CLIP_DIALOGUE_ARROW_DOWN 131
+#define CLIP_DIALOGUE_STOP_UP 132
+#define CLIP_DIALOGUE_STOP_DOWN 133
+#define CLIP_DIALOGUE_FLEUR_TOP_UP 134
+#define CLIP_DIALOGUE_FLEUR_TOP_DOWN 135
+#define CLIP_DIALOGUE_FLEUR_BOT_UP 136
+#define CLIP_DIALOGUE_FLEUR_BOT_DOWN 137
+#define CLIP_PROMPT_UP 138
+#define CLIP_PROMPT_DOWN 139
+#define CLIP_PROMPT_BLANK 140
+#define CLIP_SHADE_IDLE 141
+#define CLIP_SHADE_STARTLE 142
+#define CLIP_SHADE_FLY 143
+#define CLIP_SHADE_TURNTOFLY 144
+#define CLIP_SHADE_TURNTOIDLE 145
+#define CLIP_SHADE_SLASH_ANTIC 146
+#define CLIP_SHADE_SLASH 147
+#define CLIP_SHADE_SLASH_CD 148
+#define CLIP_SHADE_SLASH_EFFECT 149
+#define CLIP_SHADE_CAST_ANTIC 150
+#define CLIP_SHADE_CAST_CHARGE 151
+#define CLIP_SHADE_CAST 152
+#define CLIP_SHADE_RETREAT_START 153
+#define CLIP_SHADE_RETREAT_END 154
+#define CLIP_SHADE_DEATH_START 155
+#define CLIP_SHADE_DEATH 156
+#define CLIP_SHADE_DEPART 157
+#define CLIP_SHADE_APPEAR 158
+#define CLIP_SHADE_FIREBALL 159
+#define CLIP_SHADE_FIREBALL_END 160
+#define CLIP_SHADE_CAST_RING 161
+#define SPRITE_HERO_LIGHT 698
+#define SPRITE_DIALOGUE_BACKBOARD 699
+#define SPRITE_PROMPT_SHADOW 700
+#define SPRITE_CORPSE_NAIL 701
+#define SPRITE_DIGIT0 882
#define STYLE_DIALOGUE 0
#define STYLE_PROMPT 1
#define TEXT_K 1.333333f
diff --git a/games/hollowknight/src/death.c b/games/hollowknight/src/death.c
new file mode 100644
index 00000000..c27f60a4
--- /dev/null
+++ b/games/hollowknight/src/death.c
@@ -0,0 +1,182 @@
+/* The Knight's death: his Hero Death object (the Hero Death Anim FSM: his body bursting, soul and geo lost, a shade
+ * left where he fell), GameManager.PlayerDead (the save, the fade), then the respawn at his bench. */
+#include <math.h>
+#include "game.h"
+
+#define DT 0.02f
+#define DEATH_WAIT 2.85f /* (HeroController.DEATH_WAIT) */
+
+enum { DD_NONE, DD_START, DD_ASH, DD_END };
+typedef struct {
+ bool on, stuck, chipped;
+ float x, y, vx, vy, ang, w, gravity;
+} Corpse;
+static struct {
+ uint8_t phase;
+ float t; /* the FSM's timer */
+ float game_t; /* PlayerDead's */
+ bool fading;
+ float x, y, sx; /* the Hero Death object: its place, its x scale (the Knight's) */
+ Anim body, shade;
+ bool body_on, shade_on, soul_cracked;
+ Corpse nail, head; /* (Corpse Nail Hero, Corpse Head) */
+ Anim head_anim;
+} dd;
+
+void death_reset(void) { memset(&dd, 0, sizeof dd); }
+
+static void corpse_fling(Corpse *c, float x, float y, float angle, float speed, float gravity, float ang, float w) {
+ float a = angle * (float)M_PI / 180;
+ c->on = true, c->stuck = false, c->chipped = false;
+ c->x = x, c->y = y, c->vx = cosf(a) * speed, c->vy = sinf(a) * speed;
+ c->gravity = gravity, c->ang = ang, c->w = w;
+}
+
+/* a corpse piece: a ray along its step, bouncing off the ground (ObjectBounce); the nail sticks in it */
+static void corpse_step(Corpse *c, bool nail) {
+ if (!c->on || c->stuck) return;
+ c->vy -= 60 * c->gravity * DT; /* (Physics2D's gravity: 60 units/s^2 here) */
+ c->ang += c->w * DT;
+ float dx = c->vx * DT, dy = c->vy * DT, len = sqrtf(dx * dx + dy * dy);
+ PhysHit hit;
+ if (len > 0 && phys_ray(c->x, c->y, dx / len, dy / len, len + 0.1f, CF_TERRAIN, &hit)) {
+ c->x = hit.x + hit.nx * 0.1f, c->y = hit.y + hit.ny * 0.1f;
+ if (nail && hit.ny > 0.99f) {
+ /* Stick: upright-ish in the ground */
+ float a = fmodf(c->ang, 360);
+ if (a < 0) a += 360;
+ c->ang = a < rand_range(40, 50) ? 45 : a > rand_range(70, 80) ? 75 : a;
+ c->stuck = true, c->vx = c->vy = 0;
+ return;
+ }
+ if (!nail && !c->chipped) {
+ c->chipped = true; /* (Chip: the cracked head) */
+ anim_play(&dd.head_anim, CLIP_KNIGHT_DEATH_HEAD_CRACKED);
+ }
+ float speed = sqrtf(c->vx * c->vx + c->vy * c->vy);
+ if (speed > 1) {
+ float d = c->vx * hit.nx + c->vy * hit.ny;
+ c->vx = (c->vx - 2 * d * hit.nx) * 0.6f, c->vy = (c->vy - 2 * d * hit.ny) * 0.6f;
+ c->w *= 0.6f;
+ } else
+ c->vx = c->vy = 0, c->w = 0;
+ } else
+ c->x += dx, c->y += dy;
+}
+
+/* (Set Shade: the nearest shade marker, else where the Knight fell) */
+static void set_shade(void) {
+ strncpy(g_pd.shade_scene, room_name(g_room.id), SCENE_NAME - 1);
+ g_pd.shade_scene[SCENE_NAME - 1] = 0;
+ float best = 1e30f, sx = dd.x, sy = dd.y;
+ int n;
+ const Ent *es = room_ents(&n);
+ for (int i = 0; i < n; i++) {
+ if (es[i].type != ENT_SHADE_MARKER) continue;
+ float ex = es[i].x1 - dd.x, ey = es[i].y1 - dd.y, d = ex * ex + ey * ey;
+ if (d < best) best = d, sx = es[i].x0, sy = es[i].y0;
+ }
+ g_pd.shade_x = sx, g_pd.shade_y = sy;
+ int hp = g_pd.max_health / 2;
+ g_pd.shade_health = (int8_t)(hp < 1 ? 1 : hp > 99 ? 99 : hp);
+ g_pd.shade_fireball_level = (int8_t)g_pd.fireball_level;
+ /* Check MP: by the soul vessels the Knight has */
+ int r = g_pd.mp_reserve_max;
+ g_pd.shade_mp = (int16_t)(r >= 132 ? 5 : r >= 99 ? 4 : r >= 66 ? 3 : r >= 33 ? 2 : 1);
+}
+
+void death_start(float x, float y, bool facing_right) {
+ death_reset();
+ dd.sx = facing_right ? -1.0f : 1.0f;
+ dd.x = x + 0.21f * dd.sx, dd.y = y;
+ /* Remove Geo, Limit Soul, Set Shade, Check MP, Save */
+ g_pd.geo_pool = g_pd.geo, g_pd.geo = 0;
+ bool cracked = !g_pd.soul_limited;
+ g_pd.soul_limited = true;
+ set_shade();
+ save_game();
+ /* (Send Events: HERO LEAVE) */
+ enemies_hero_leave();
+ /* Initiate: a shake, the moment frozen; Start: the Death animation */
+ cam_shake(SHAKE_ENEMY_KILL);
+ game_freeze(0.01f, 0.35f, 0.1f, 0, false);
+ anim_play_from_frame(&dd.body, CLIP_KNIGHT_DEATH, 0);
+ dd.body_on = true;
+ dd.phase = DD_START, dd.t = 0;
+ dd.soul_cracked = cracked; /* (for Limit Soul?: the HUD's limiter up) */
+ /* PlayerDead: the camera stays */
+ cam_freeze();
+}
+
+void death_tick(void) {
+ if (dd.phase == DD_NONE) return;
+ dd.t += DT, dd.game_t += DT;
+ dd.body.events = 0;
+ anim_update(&dd.body, DT);
+ if (dd.shade_on) anim_update(&dd.shade, DT);
+ dd.head_anim.events = 0;
+ anim_update(&dd.head_anim, DT);
+ corpse_step(&dd.nail, true);
+ corpse_step(&dd.head, false);
+ switch (dd.phase) {
+ case DD_START:
+ if (dd.t >= 0.5f) {
+ /* Drain Soul; Blow: the nail flung */
+ g_pd.mp = 0, g_pd.mp_reserve = 0;
+ corpse_fling(&dd.nail, dd.x, dd.y, rand_range(50, 130), rand_range(18, 22), 0.8f, rand_range(0, 360), rand_range(-720, 720));
+ dd.phase = DD_ASH;
+ }
+ break;
+ case DD_ASH:
+ if ((dd.body.events & ANIM_DONE) || !dd.body.playing) {
+ /* Facing Check, Head Left / Right: the head flung */
+ if (dd.sx > 0) corpse_fling(&dd.head, dd.x + 0.2f, dd.y - 0.02f, 100, 15, 1.5f, -15, 200);
+ else corpse_fling(&dd.head, dd.x - 0.2f, dd.y - 0.02f, 80, 15, 1.5f, 20, -200);
+ anim_play(&dd.head_anim, CLIP_KNIGHT_DEATH_HEAD_NORMAL);
+ /* Limit Soul?, End: the soul limited; Shade?: the shade rises */
+ if (dd.soul_cracked) hud_soul_limiter(true);
+ g_pd.max_mp = 66;
+ if (g_pd.mp > g_pd.max_mp) g_pd.mp = g_pd.max_mp;
+ dd.body_on = false;
+ anim_play_from_frame(&dd.shade, CLIP_SHADE_APPEAR, 0);
+ dd.shade_on = true;
+ dd.phase = DD_END;
+ }
+ break;
+ }
+ /* PlayerDead: after DEATH_WAIT the camera fades (RESPAWN FADE), 0.8 s later the respawn */
+ if (!dd.fading && dd.game_t >= DEATH_WAIT) {
+ dd.fading = true;
+ game_fade(1, 0.75f, 0);
+ }
+ if (dd.fading && dd.game_t >= DEATH_WAIT + 0.8f) {
+ death_reset();
+ if (!game_respawn()) {
+ /* (a save names its rooms: never; then King's Pass) */
+ strcpy(g_pd.respawn_scene, "Tutorial_01");
+ g_pd.respawn_type = 0;
+ game_respawn();
+ }
+ }
+}
+
+void death_draw(void) {
+ if (dd.phase == DD_NONE) return;
+ Inst in;
+ if (dd.body_on && dd.body.sprite >= 0) {
+ sprite_inst(dd.body.sprite, dd.x, dd.y, 0.004f, dd.sx, 1, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+ if (dd.shade_on && dd.shade.sprite >= 0) {
+ sprite_inst(dd.shade.sprite, dd.x - 0.1436f * dd.sx, dd.y + 0.201f, 0.004f, dd.sx, 1, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+ if (dd.nail.on) {
+ sprite_inst_rot(SPRITE_CORPSE_NAIL, dd.nail.x, dd.nail.y, 0.003f, 1, 1, dd.nail.ang, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+ if (dd.head.on && dd.head_anim.sprite >= 0) {
+ sprite_inst_rot(dd.head_anim.sprite, dd.head.x, dd.head.y, 0.003f, dd.sx, 1, dd.head.ang, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+}
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index a81d7e64..c31fb397 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -9,7 +9,8 @@
#define TERRAIN_FRICTION 0.2f /* (the Terrain material; with another, their geometric mean) */
/* ---------------------------------------------------------------- what each kind is */
-enum { EK_CRAWLER = 1, EK_BUZZER };
+enum { EK_CRAWLER = 1, EK_BUZZER, EK_SHADE };
+#define NO_ENT 0xFFFF /* (an enemy spawned, not one of the room's) */
typedef struct {
/* Recoil */
float recoil_speed, recoil_time;
@@ -25,6 +26,7 @@ static const Kind kinds[] = {
CLIP_CRAWLER_DEATH_AIR, CLIP_CRAWLER_DEATH_LAND, 1, 0.2828f},
[EK_BUZZER] = {15, 0.25f, false, 0, 15, 0.7f, 0.1016f, -0.2188f, 1.1406f / 2, 1.3125f / 2, -1, true,
CLIP_BUZZER_DEATH_AIR, CLIP_BUZZER_DEATH_LAND, 0, 0.2828f},
+ [EK_SHADE] = {15, 0.15f, false, 0, 0, 0, 0, 0, 0, 0, -1, false, -1, -1, 0, 0.2828f},
};
/* ---------------------------------------------------------------- an enemy */
@@ -33,7 +35,10 @@ enum { RC_READY, RC_RECOILING };
enum { CS_AIR, CS_DEATH_ANIM, CS_LANDED };
typedef struct {
uint8_t mode, kind, st, flags;
- uint16_t ent;
+ uint16_t ent; /* its record (NO_ENT: spawned) */
+ int8_t damage; /* DamageHero's */
+ uint8_t geo_s, geo_m, geo_l; /* the geo it drops */
+ float z;
Body body;
Anim anim;
float sx; /* its transform's x scale: which way it faces */
@@ -48,7 +53,7 @@ typedef struct {
float flash_t;
bool flashing;
/* the FSM's state */
- float t0, t1, wait, start_x, start_y, ax, ay, pause0, pause1;
+ float t0, t1, wait, start_x, start_y, ax, ay, pause0, pause1, jx, jy;
bool b0, b1;
/* alert range (local circle) and sight */
float ar_x, ar_y, ar_r;
@@ -225,14 +230,18 @@ static void corpse_start(Enemy *e, float direction, bool has_direction) {
}
/* HealthManager.Die, EnemyDeathEffects */
+static void shade_killed(Enemy *e);
static void enemy_die(Enemy *e, float direction, bool has_direction) {
- const Ent *d = ent_at(e->ent);
+ if (e->kind == EK_SHADE) {
+ shade_killed(e);
+ return;
+ }
/* the geo: SpawnAndFling, small, medium then large */
float x = e->body.x, y = e->body.y;
- geo_fling(0, (int)d->p2, x, y, 15, 30, 80, 100);
- geo_fling(1, (int)d->p3, x, y, 15, 30, 80, 100);
- geo_fling(2, d->group, x, y, 15, 30, 80, 100);
- persist_set(d->persist);
+ geo_fling(0, e->geo_s, x, y, 15, 30, 80, 100);
+ geo_fling(1, e->geo_m, x, y, 15, 30, 80, 100);
+ geo_fling(2, e->geo_l, x, y, 15, 30, 80, 100);
+ if (e->ent != NO_ENT) persist_set(ent_at(e->ent)->persist);
corpse_start(e, direction, has_direction);
cam_shake(SHAKE_ENEMY_KILL);
FREEZE_MOMENT_1();
@@ -243,10 +252,10 @@ static void enemy_hit(Enemy *e, float direction, int damage) {
if (e->mode != EM_ALIVE || e->evasion > 0 || damage <= 0) return;
int dir = cardinal(direction);
recoil_by_direction(e, dir, 1);
- hero_soul_gain();
+ if (e->kind != EK_SHADE) hero_soul_gain(); /* (enemyType 3, a shade: no soul) */
e->flashing = true, e->flash_t = 0;
e->hp = (int16_t)(e->hp - damage < -50 ? -50 : e->hp - damage);
- if (e->kind == EK_BUZZER && e->st == 0) e->b1 = true; /* (TOOK DAMAGE, in Idle) */
+ if ((e->kind == EK_BUZZER || e->kind == EK_SHADE) && e->st == 0) e->b1 = true; /* (TOOK DAMAGE, in Idle) */
if (e->hp > 0)
e->evasion = 0.2f;
else
@@ -438,6 +447,354 @@ static void buzzer_update(Enemy *e, const Ent *d) {
}
}
+/* ---------------------------------------------------------------- the Hollow Shade: its Shade Control FSM */
+enum {
+ SH_IDLE, SH_STARTLE, SH_FLY, SH_POSITION, SH_SLASH_ANTIC, SH_SLASH, SH_SLASH_BOX, SH_SLASH_CD, SH_FIREBALL_POS,
+ SH_CAST_ANTIC, SH_CAST_CHARGE, SH_CHARGE_WAIT, SH_CAST, SH_DECEL, SH_COOLDOWN, SH_RETREAT_START, SH_RETREAT,
+ SH_RETREAT_END, SH_DEATH_START, SH_DEATH, SH_DEATH_END, SH_DISSIPATE, SH_LEAVE_PAUSE, SH_DEPART, SH_GONE
+};
+#define SHADE_MAX_ROAM 25.0f
+#define MAX_BALLS 3
+/* the shade's (Shadow Ball): flies 1 s, then slows and fades */
+typedef struct {
+ bool on, ending;
+ float x, y, vx, t;
+ Anim anim;
+} Ball;
+static Ball balls[MAX_BALLS];
+static struct {
+ bool on, slash_on; /* (its Slash child's collider) */
+ Anim slash;
+ int8_t sp; /* its soul: casts left */
+ float qx, qy; /* (the tween back to its start) */
+} sh;
+
+/* the Slash's polygon (Slash's local points, at its place and x scale), on the shade facing its way */
+static const float shade_slash_pts[5][2] = {{-1.8682f, -0.2095f}, {0.8395f, 0.0287f}, {-0.9918f, -0.9513f}, {-2.0697f, -0.8551f},
+ {-2.4798f, -0.5897f}};
+
+static void shade_face_hero(Enemy *e, int clip) {
+ /* FaceObject (sprite facing left), playing its turn clip on turning */
+ float want = hero_x() > e->body.x ? -1.0f : 1.0f;
+ if (e->sx != want) {
+ set_scale_x(e, want);
+ if (clip >= 0) anim_play_from_frame(&e->anim, clip, 0);
+ }
+}
+
+static float hero_dist(const Enemy *e) {
+ float dx = e->body.x - hero_x(), dy = e->body.y - hero_y();
+ return sqrtf(dx * dx + dy * dy);
+}
+
+static bool roamed_off(const Enemy *e) {
+ float dx = e->body.x - e->start_x, dy = e->body.y - e->start_y;
+ return dx * dx + dy * dy > SHADE_MAX_ROAM * SHADE_MAX_ROAM;
+}
+
+/* DistanceFly (targets height): keeps a distance from the hero, at its height */
+static void distance_fly(Enemy *e, float dist, float vmax, float accel) {
+ float vx = e->body.vx, vy = e->body.vy, d = hero_dist(e);
+ bool left = e->body.x < hero_x();
+ vx += (d > dist) == left ? accel : -accel;
+ if (e->body.y < hero_y()) vy += accel;
+ if (e->body.y > hero_y()) vy -= accel;
+ e->body.vx = vx > vmax ? vmax : vx < -vmax ? -vmax : vx;
+ e->body.vy = vy > vmax ? vmax : vy < -vmax ? -vmax : vy;
+}
+
+static void shade_retreat(Enemy *e) {
+ /* Retreat Start: no collider, slowing */
+ e->st = SH_RETREAT_START;
+ e->flags |= 1; /* (collider off) */
+ anim_play_from_frame(&e->anim, CLIP_SHADE_RETREAT_START, 0);
+}
+
+static void shade_fly(Enemy *e) {
+ e->st = SH_FLY;
+ anim_play(&e->anim, CLIP_SHADE_FLY);
+ e->wait = rand_range(1, 2);
+}
+
+/* Attack Choice (after Quake? and Scream?: the shade has neither here): a fireball a fifth of the time, if it can */
+static void shade_attack_choice(Enemy *e) {
+ for (;;) {
+ if (rand_range(0, 1) < 0.2f) {
+ /* Sp Check */
+ if (g_pd.shade_fireball_level >= 1 && sh.sp >= 1) {
+ sh.sp--;
+ e->st = SH_FIREBALL_POS, e->t0 = 0;
+ return;
+ }
+ continue; /* (RETURN: chooses again) */
+ }
+ /* Q Other?: nothing else to choose; Position */
+ e->st = SH_POSITION, e->t0 = 0;
+ return;
+ }
+}
+
+static void shade_spawn(void) {
+ Enemy *e = NULL;
+ for (int i = 0; i < MAX_ENEMIES && !e; i++)
+ if (en[i].mode == EM_OFF) e = &en[i];
+ if (!e) return;
+ memset(e, 0, sizeof *e);
+ memset(&sh, 0, sizeof sh);
+ memset(balls, 0, sizeof balls);
+ sh.on = true;
+ e->mode = EM_ALIVE, e->kind = EK_SHADE, e->ent = NO_ENT;
+ e->damage = 1, e->z = 0.006f;
+ /* (Init: its hp the shade's health in nail hits) */
+ e->hp = (int16_t)(g_pd.nail_damage * g_pd.shade_health);
+ sh.sp = (int8_t)g_pd.shade_mp;
+ Body *b = &e->body;
+ b->x = g_pd.shade_x, b->y = g_pd.shade_y;
+ b->ox = 0, b->oy = -0.37f, b->hx = 0.25f, b->hy = 0.57f;
+ b->gravity_scale = 0, b->mask = CF_SOLID;
+ e->ar_r = 7.27f;
+ e->sx = 1;
+ e->start_x = b->x, e->start_y = b->y;
+ shade_face_hero(e, -1);
+ e->st = SH_IDLE;
+ anim_play(&e->anim, CLIP_SHADE_IDLE);
+}
+
+void shade_spawn_check(void) {
+ if (g_pd.soul_limited && !strcmp(g_pd.shade_scene, room_name(g_room.id))) shade_spawn();
+}
+
+/* (Killed: the Hollow Shade Death in its place) */
+static void shade_killed(Enemy *e) {
+ /* Death Start: the soul unlimited, the geo back, the shade gone from the save */
+ g_pd.soul_limited = false;
+ g_pd.max_mp = 99;
+ int geo_back = g_pd.geo_pool;
+ g_pd.geo += geo_back;
+ g_pd.geo_pool = 0;
+ strcpy(g_pd.shade_scene, "None");
+ e->body.vx = e->body.vy = 0;
+ e->flags |= 1;
+ sh.slash_on = false;
+ cam_shake(SHAKE_AVERAGE);
+ anim_play(&e->anim, CLIP_SHADE_DEATH_START);
+ e->st = SH_DEATH_START, e->t0 = 0;
+ e->mode = EM_CORPSE;
+}
+
+/* HERO LEAVE: the shade departs (Hollow Shade Depart) */
+void enemies_hero_leave(void) {
+ for (int i = 0; i < MAX_ENEMIES; i++) {
+ Enemy *e = &en[i];
+ if (e->kind != EK_SHADE || e->mode != EM_ALIVE) continue;
+ e->mode = EM_CORPSE, e->flags |= 1, sh.slash_on = false;
+ anim_play(&e->anim, CLIP_SHADE_FLY);
+ shade_face_hero(e, CLIP_SHADE_TURNTOFLY);
+ e->st = SH_LEAVE_PAUSE, e->t0 = 0;
+ }
+}
+
+static void shade_fixed(Enemy *e) {
+ switch (e->st) {
+ case SH_FLY: {
+ chase_object(e, 4, 0.2f);
+ /* ChaseObjectV2: a force towards the hero, the speed clamped (the force acts in the step) */
+ float dx = hero_x() - e->body.x, dy = hero_y() - e->body.y, l = sqrtf(dx * dx + dy * dy);
+ if (l > 1) dx /= l, dy /= l;
+ float sp = sqrtf(e->body.vx * e->body.vx + e->body.vy * e->body.vy);
+ if (sp > 4) e->body.vx *= 4 / sp, e->body.vy *= 4 / sp;
+ e->body.vx += dx * 8 * DT, e->body.vy += dy * 8 * DT;
+ break;
+ }
+ case SH_POSITION:
+ case SH_SLASH_ANTIC: distance_fly(e, 3, 4, 0.2f); break;
+ case SH_FIREBALL_POS: distance_fly(e, 12, 4, 0.2f); break;
+ case SH_CAST_ANTIC: e->body.vx *= 0.8f, e->body.vy *= 0.8f; break;
+ case SH_DECEL:
+ case SH_COOLDOWN:
+ case SH_LEAVE_PAUSE: e->body.vx *= 0.9f, e->body.vy *= 0.9f; break;
+ case SH_RETREAT_START: e->body.vx *= 0.85f, e->body.vy *= 0.85f; break;
+ default: break;
+ }
+}
+
+static void shade_update(Enemy *e) {
+ bool done = (e->anim.events & ANIM_DONE) != 0;
+ e->t0 += DT;
+ switch (e->st) {
+ case SH_IDLE:
+ if ((e->in_alert && e->can_see) || e->b1) {
+ e->b1 = false;
+ e->st = SH_STARTLE;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_STARTLE, 0);
+ }
+ break;
+ case SH_STARTLE:
+ if (done) shade_fly(e);
+ break;
+ case SH_FLY:
+ shade_face_hero(e, CLIP_SHADE_TURNTOFLY);
+ if (roamed_off(e)) shade_retreat(e);
+ else if ((e->wait -= DT) <= 0) shade_attack_choice(e);
+ break;
+ case SH_POSITION: {
+ shade_face_hero(e, CLIP_SHADE_TURNTOFLY);
+ bool same_y = fabsf(hero_y() - e->body.y) <= 0.2f;
+ if (hero_dist(e) < 5 && same_y) {
+ e->st = SH_SLASH_ANTIC;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_SLASH_ANTIC, 0);
+ } else if (roamed_off(e))
+ shade_retreat(e);
+ else if (e->t0 >= 6)
+ shade_attack_choice(e); /* (END: Quake?, Scream?, Attack Choice) */
+ break;
+ }
+ case SH_SLASH_ANTIC:
+ if (done) {
+ /* Check Dir, Right / Left: a lunge its way; Slash */
+ e->body.vx = e->sx < 0 ? 8 : -8;
+ sh.slash_on = true;
+ anim_play_from_frame(&sh.slash, CLIP_SHADE_SLASH_EFFECT, 0);
+ anim_play_from_frame(&e->anim, CLIP_SHADE_SLASH, 0);
+ e->st = SH_SLASH, e->t0 = 0;
+ }
+ break;
+ case SH_SLASH:
+ if (e->t0 >= 0.083f) e->st = SH_SLASH_BOX; /* (its clip may have ended meanwhile: Slash Box sees that) */
+ break;
+ case SH_SLASH_BOX:
+ if (done || !e->anim.playing) {
+ sh.slash_on = false;
+ e->st = SH_SLASH_CD;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_SLASH_CD, 0);
+ }
+ break;
+ case SH_SLASH_CD:
+ if (done) shade_fly(e);
+ break;
+ case SH_FIREBALL_POS: {
+ shade_face_hero(e, CLIP_SHADE_TURNTOFLY);
+ bool same_y = fabsf(hero_y() - e->body.y) <= 0.2f, at = fabsf(hero_dist(e) - 12) <= 1;
+ if ((at && same_y) || e->t0 >= 3) {
+ e->st = SH_CAST_ANTIC, e->t0 = 0;
+ anim_play(&e->anim, CLIP_SHADE_CAST_ANTIC);
+ } else if (roamed_off(e))
+ shade_retreat(e);
+ break;
+ }
+ case SH_CAST_ANTIC:
+ if (e->t0 >= 0.5f) {
+ e->st = SH_CAST_CHARGE, e->t0 = 0;
+ anim_play(&e->anim, CLIP_SHADE_CAST_CHARGE);
+ e->body.vx = e->body.vy = 0;
+ /* Check Dir 2: it will be pushed back as it casts */
+ e->ax = e->sx < 0 ? -10 : 10;
+ e->st = SH_CHARGE_WAIT;
+ }
+ break;
+ case SH_CHARGE_WAIT:
+ if (e->t0 >= 0.25f) {
+ /* Shoot R / L: a Shadow Ball, fast its way */
+ for (int i = 0; i < MAX_BALLS; i++)
+ if (!balls[i].on) {
+ Ball *b = &balls[i];
+ float k = e->sx < 0 ? 1.0f : -1.0f;
+ b->on = true, b->ending = false, b->t = 0;
+ b->x = e->body.x + 1.764f * k, b->y = e->body.y + 0.12f, b->vx = 20 * k;
+ anim_play_from_frame(&b->anim, CLIP_SHADE_FIREBALL, 0);
+ break;
+ }
+ cam_shake(SHAKE_AVERAGE);
+ anim_play_from_frame(&e->anim, CLIP_SHADE_CAST, 0);
+ e->body.vx = e->ax;
+ e->st = SH_DECEL, e->t0 = 0;
+ }
+ break;
+ case SH_DECEL:
+ if (done) {
+ e->st = SH_COOLDOWN, e->t0 = 0;
+ anim_play(&e->anim, CLIP_SHADE_FLY);
+ }
+ break;
+ case SH_COOLDOWN:
+ if (e->t0 >= 0.25f) shade_fly(e);
+ break;
+ case SH_RETREAT_START:
+ if (done) {
+ e->body.vx = e->body.vy = 0;
+ sh.qx = e->body.x, sh.qy = e->body.y;
+ e->st = SH_RETREAT, e->t0 = 0;
+ }
+ break;
+ case SH_RETREAT: {
+ /* iTweenMoveTo its start, 1 s, easeInOutCubic */
+ float t = e->t0 >= 1 ? 1 : e->t0, k = t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2;
+ e->body.x = sh.qx + (e->start_x - sh.qx) * k, e->body.y = sh.qy + (e->start_y - sh.qy) * k;
+ if (e->t0 >= 1) {
+ e->st = SH_RETREAT_END;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_RETREAT_END, 0);
+ }
+ break;
+ }
+ case SH_RETREAT_END:
+ if (done) {
+ anim_play(&e->anim, CLIP_SHADE_IDLE);
+ e->flags &= (uint8_t)~1;
+ e->st = SH_IDLE;
+ }
+ break;
+ /* (Hollow Shade Death) */
+ case SH_DEATH_START:
+ e->jx = rand_range(-0.1f, 0.1f), e->jy = rand_range(-0.1f, 0.1f); /* (ObjectJitter) */
+ if (e->t0 >= 0.5f) {
+ e->jx = e->jy = 0;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_DEATH, 0);
+ cam_shake(SHAKE_AVERAGE);
+ e->st = SH_DEATH_END, e->t0 = 0;
+ }
+ break;
+ case SH_DEATH_END:
+ if (e->t0 >= 0.75f) e->st = SH_DISSIPATE, e->t0 = 0;
+ break;
+ case SH_DISSIPATE:
+ if (e->t0 >= 1) {
+ hud_soul_limiter(false); /* (Give Geo: SOUL LIMITER DOWN) */
+ e->mode = EM_OFF, sh.on = false;
+ }
+ break;
+ case SH_LEAVE_PAUSE:
+ if (e->t0 >= 2) {
+ e->body.vx = e->body.vy = 0;
+ anim_play_from_frame(&e->anim, CLIP_SHADE_DEPART, 0);
+ e->st = SH_DEPART, e->t0 = 0;
+ }
+ break;
+ case SH_DEPART:
+ if (e->t0 >= 0.6f) e->mode = EM_OFF, sh.on = false;
+ break;
+ }
+ if (sh.slash_on) anim_update(&sh.slash, DT);
+}
+
+static void balls_tick(void) {
+ for (int i = 0; i < MAX_BALLS; i++) {
+ Ball *b = &balls[i];
+ if (!b->on) continue;
+ b->t += DT;
+ b->anim.events = 0;
+ anim_update(&b->anim, DT);
+ if (!b->ending && b->t >= 1) {
+ b->ending = true;
+ anim_play_from_frame(&b->anim, CLIP_SHADE_FIREBALL_END, 0);
+ }
+ if (b->ending) {
+ b->vx *= 0.1f;
+ if (b->anim.events & ANIM_DONE) b->on = false;
+ }
+ b->x += b->vx * DT;
+ }
+}
+
/* ---------------------------------------------------------------- the room's */
void enemies_enter(void) {
memset(en, 0, sizeof en);
@@ -449,6 +806,8 @@ void enemies_enter(void) {
if (d->type != ENT_OBJ || d->flags != OK_ENEMY || persist_get(d->persist)) continue;
Enemy *e = &en[k++];
e->mode = EM_ALIVE, e->kind = (uint8_t)d->a, e->ent = (uint16_t)i;
+ e->damage = (int8_t)d->p1, e->geo_s = (uint8_t)d->p2, e->geo_m = (uint8_t)d->p3, e->geo_l = (uint8_t)d->group;
+ e->z = d->x1;
e->sx = d->y1 ? d->y1 : 1;
e->hp = (int16_t)d->p0;
Body *b = &e->body;
@@ -460,6 +819,9 @@ void enemies_enter(void) {
if (e->kind == EK_CRAWLER) crawler_start(e, d);
else if (e->kind == EK_BUZZER) buzzer_start(e, d);
}
+ memset(&sh, 0, sizeof sh);
+ memset(balls, 0, sizeof balls);
+ shade_spawn_check();
}
/* FixedUpdate and the physics step */
@@ -467,6 +829,12 @@ void enemies_fixed(void) {
for (int i = 0; i < MAX_ENEMIES; i++) {
Enemy *e = &en[i];
if (e->mode == EM_OFF) continue;
+ if (e->kind == EK_SHADE) {
+ if (e->mode == EM_ALIVE) shade_fixed(e), recoil_fixed(e);
+ else shade_fixed(e);
+ if (e->st != SH_RETREAT) body_step(&e->body, DT);
+ continue;
+ }
if (e->mode == EM_ALIVE) {
if (e->kind == EK_CRAWLER) crawler_fixed(e);
else if (e->kind == EK_BUZZER) buzzer_fixed(e);
@@ -498,6 +866,7 @@ void enemies_fixed(void) {
}
}
geo_tick();
+ balls_tick();
}
/* Update: the FSMs' every-frame actions, the animations, the timers */
@@ -512,7 +881,10 @@ void enemies_update(void) {
if (e->evasion > 0) e->evasion -= DT;
if (e->ar_r > 0) sight_update(e);
if (e->kind == EK_BUZZER) buzzer_update(e, ent_at(e->ent));
- } else if (e->st == CS_DEATH_ANIM && !e->anim.playing)
+ else if (e->kind == EK_SHADE) shade_update(e);
+ } else if (e->kind == EK_SHADE)
+ shade_update(e);
+ else if (e->st == CS_DEATH_ANIM && !e->anim.playing)
e->mode = EM_OFF;
}
}
@@ -522,9 +894,20 @@ void enemies_draw(void) {
const Enemy *e = &en[i];
if (e->mode == EM_OFF) continue;
Inst in;
- const Ent *d = ent_at(e->ent);
- float z = e->mode == EM_CORPSE ? 0.0085f : d->x1;
- sprite_inst(e->anim.sprite, e->body.x, e->body.y, z, e->sx, 1, flash_tint(e, 2 + i % 5), &in);
+ float z = e->mode == EM_CORPSE && e->kind != EK_SHADE ? 0.0085f : e->z;
+ if (e->kind == EK_SHADE && e->st == SH_DISSIPATE) continue; /* (its renderer off) */
+ sprite_inst(e->anim.sprite, e->body.x + e->jx, e->body.y + e->jy, z, e->sx, 1, flash_tint(e, 2 + i % 5), &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ if (e->kind == EK_SHADE && sh.slash_on) {
+ sprite_inst(sh.slash.sprite, e->body.x - 0.0486f * e->sx, e->body.y - 0.0117f, z - 0.001f, 1.27f * e->sx, 1, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+ }
+ for (int i = 0; i < MAX_BALLS; i++) {
+ const Ball *b = &balls[i];
+ if (!b->on) continue;
+ Inst in;
+ sprite_inst(b->anim.sprite, b->x, b->y, -0.1f, b->vx < 0 ? -1.0f : 1.0f, 1, 0, &in);
gfx_actor(&in, SORT_KEY(0, 0));
}
for (int i = 0; i < MAX_GEO; i++) {
@@ -544,7 +927,7 @@ int enemies_nail(const float *pts, int npts, float direction, int damage) {
int out = 0;
for (int i = 0; i < MAX_ENEMIES; i++) {
Enemy *e = &en[i];
- if (e->mode != EM_ALIVE) continue;
+ if (e->mode != EM_ALIVE || (e->flags & 1)) continue;
float x0, y0, x1, y1;
enemy_box(e, &x0, &y0, &x1, &y1);
if (!box_meets_shape(x0, y0, x1, y1, pts, npts)) continue;
@@ -560,14 +943,31 @@ int enemies_nail(const float *pts, int npts, float direction, int damage) {
int enemies_touch_hero(float x0, float y0, float x1, float y1, int *side) {
for (int i = 0; i < MAX_ENEMIES; i++) {
const Enemy *e = &en[i];
- if (e->mode != EM_ALIVE) continue;
- const Ent *d = ent_at(e->ent);
- if (d->p1 <= 0) continue;
+ if (e->mode != EM_ALIVE || e->damage <= 0 || (e->flags & 1)) continue;
float a0, b0, a1, b1;
enemy_box(e, &a0, &b0, &a1, &b1);
if (x1 > a0 && x0 < a1 && y1 > b0 && y0 < b1) {
*side = e->body.x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
- return (int)d->p1;
+ return e->damage;
+ }
+ if (e->kind == EK_SHADE && sh.slash_on) {
+ /* its Slash's polygon */
+ float pts[10];
+ for (int k = 0; k < 5; k++)
+ pts[2 * k] = e->body.x + (-0.0486f + shade_slash_pts[k][0] * 1.27f) * e->sx, pts[2 * k + 1] = e->body.y - 0.0117f + shade_slash_pts[k][1];
+ if (box_meets_shape(x0, y0, x1, y1, pts, 5)) {
+ *side = e->body.x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
+ return 1;
+ }
+ }
+ }
+ for (int i = 0; i < MAX_BALLS; i++) {
+ const Ball *b = &balls[i];
+ if (!b->on || b->ending) continue;
+ float k = b->vx < 0 ? -1.0f : 1.0f, cx = b->x + 0.26f * k, cy = b->y - 0.0496f;
+ if (x1 > cx - 0.99f && x0 < cx + 0.99f && y1 > cy - 0.6204f && y0 < cy + 0.6204f) {
+ *side = b->x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
+ return 1;
}
}
return 0;
diff --git a/games/hollowknight/src/game.c b/games/hollowknight/src/game.c
index 9d492e2c..f940fb61 100644
--- a/games/hollowknight/src/game.c
+++ b/games/hollowknight/src/game.c
@@ -13,8 +13,12 @@ void game_new(void) {
g_pd.max_mp = 99;
g_pd.nail_damage = 5;
g_pd.can_dash = false;
+ g_pd.charm_slots = 3;
+ strcpy(g_pd.respawn_scene, "Tutorial_01");
+ strcpy(g_pd.respawn_marker, "Death Respawn Marker");
dialogue_reset();
prompts_reset();
+ death_reset();
}
bool game_enter(int room, float x, float y, bool facing_right) {
@@ -29,6 +33,45 @@ bool game_enter(int room, float x, float y, bool facing_right) {
return true;
}
+/* HeroController.Respawn, as GameManager starts it: the respawn scene, the Knight at its marker (on the ground below),
+ * health back, soul gone; on a bench he wakes on it, else on the ground */
+bool game_respawn(void) {
+ int room = -1;
+ for (int i = 0; i < NUM_ROOMS && room < 0; i++)
+ if (!strcmp(room_name(i), g_pd.respawn_scene)) room = i;
+ if (room < 0 || !room_load(room)) return false;
+ memset(inside, 0, sizeof inside);
+ prompts_reset();
+ world_enter();
+ float x = g_room.h->w / 2, y = g_room.h->h / 2;
+ bool right = g_pd.respawn_facing_right;
+ int n;
+ const Ent *es = room_ents(&n);
+ for (int i = 0; i < n; i++)
+ if (es[i].type == ENT_RESPAWN && !strcmp(str_at(es[i].s0), g_pd.respawn_marker)) {
+ x = es[i].x0, y = es[i].y0;
+ if (g_pd.respawn_type != 1) right = es[i].flags & FACING_RIGHT;
+ break;
+ }
+ hero_init(x, y, right);
+ g_hero.body.y = hero_ground_y(x, y);
+ g_pd.health = g_pd.max_health;
+ g_pd.mp = 0; /* (ClearMP) */
+ g_game.hazard_x = g_hero.body.x, g_game.hazard_y = g_hero.body.y, g_game.hazard_facing_right = right;
+ world_hero_in_position();
+ if (g_pd.respawn_type == 1) {
+ /* (FinishedEnteringScene, then the bench's RESPAWN) */
+ hero_finished_entering_scene(true);
+ if (!bench_respawn(g_pd.respawn_marker)) hero_wake_up_ground();
+ } else
+ hero_wake_up_ground();
+ cam_init();
+ /* (the camera's RESPAWN: from black) */
+ g_game.fade = 1, g_screen_fade = 255;
+ game_fade(0, 0.5f, 0.1f);
+ return true;
+}
+
const Ent *room_ents(int *n) {
*n = g_room.h->nent;
return (const Ent *)(const void *)(section(SEC_RBLOB) + g_room.h->ents);
@@ -244,7 +287,9 @@ static void step(uint32_t keys) {
triggers_tick();
hero_check_damage();
hero_update();
+ death_tick();
world_tick();
+ benches_tick();
prompts_tick();
dialogue_tick();
hazard_tick();
@@ -272,5 +317,6 @@ void game_draw(void) {
prompts_draw();
obj_draw();
hero_draw();
+ death_draw();
gfx_frame();
}
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index 0c5d76eb..54fe1aa7 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -4,22 +4,53 @@
/* ---------------------------------------------------------------- PlayerData (what a save keeps) */
#define MAX_PERSIST 1024
+#define SCENE_NAME 32
+/* the game's many bools (PlayerData's), by number: new ones only ever added at the end (saves keep them) */
+enum {
+ PDF_AT_BENCH, PDF_HAS_MAP, PDF_HAS_QUILL, PDF_HAS_CHARM, PDF_CHARM_BENCH_MSG, PDF_MET_ELDERBUG, PDF_VISITED_CROSSROADS,
+ PDF_COUNT
+};
typedef struct {
- uint8_t persist[MAX_PERSIST / 8]; /* the objects' states (PersistentBoolItem) */
- int8_t health, max_health, health_blue;
+ /* (saved: the layout only ever grows into reserved) */
+ int8_t health, max_health, health_blue, nail_damage;
int16_t mp, mp_reserve, max_mp, mp_reserve_max;
int32_t geo;
- int8_t nail_damage;
- bool can_dash, has_spell, has_dash;
- uint8_t fireball_level;
+ uint8_t fireball_level, charm_slots, respawn_type;
+ bool can_dash, has_dash, has_spell, respawn_facing_right, soul_limited;
+ char respawn_scene[SCENE_NAME], respawn_marker[SCENE_NAME];
+ /* the shade: where the Knight died, what it keeps */
+ char shade_scene[SCENE_NAME];
+ float shade_x, shade_y;
+ int8_t shade_health, shade_fireball_level;
+ int16_t shade_mp;
+ int32_t geo_pool;
+ float play_time;
+ uint8_t flags[32]; /* PDF_* */
+ uint8_t reserved[64];
+ /* (saved apart, by the objects' names: their states) */
+ uint8_t persist[MAX_PERSIST / 8];
+ /* (not saved) */
bool disable_pause;
} PlayerData;
+#define PD_SAVED offsetof(PlayerData, persist)
extern PlayerData g_pd;
+static inline bool pd_flag(int f) { return g_pd.flags[f >> 3] >> (f & 7) & 1; }
+static inline void pd_set_flag(int f, bool on) {
+ if (on) g_pd.flags[f >> 3] |= (uint8_t)(1 << (f & 7));
+ else g_pd.flags[f >> 3] &= (uint8_t)~(1 << (f & 7));
+}
+/* the saves (save.c): GameManager.SaveGame, LoadGame; a slot's file */
+#define SAVE_SLOTS 4
+bool save_game(void);
+bool save_load(int slot);
+bool save_exists(int slot);
+void save_select(int slot);
+void save_set_respawn(const char *marker, bool facing_right); /* (Bench Control's Rest Burst) */
/* ---------------------------------------------------------------- the room's game objects (tools/ents.py) */
enum { ENT_CAMLOCK = 1, ENT_GATE, ENT_HAZARD_MARKER, ENT_RESPAWN, ENT_HAZARD_TRIGGER, ENT_MASK, ENT_DAMAGE, ENT_SHAPE,
- ENT_BOX, ENT_OBJ, ENT_PIECE }; /* (shape, box, piece: more of the record before) */
-enum { OK_BREAKABLE = 1, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST }; /* objects (ENT_OBJ's flags) */
+ ENT_BOX, ENT_OBJ, ENT_PIECE, ENT_SHADE_MARKER }; /* (shape, box, piece: more of the record before) */
+enum { OK_BREAKABLE = 1, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST, OK_BENCH }; /* objects (ENT_OBJ's flags) */
enum { HB_BOUNCE = 1, HB_RECOIL = 2 }; /* a hit box (ENT_BOX's flags): a down slash bounces off it, a slash recoils */
enum { HAZ_NONE, HAZ_NORMAL, HAZ_SPIKES, HAZ_ACID, HAZ_LAVA, HAZ_PIT }; /* DamageHero.hazardType */
enum { MK_SECRET = 1, MK_REMASK = 2, MK_SIMPLE = 4 }; /* masks: the unmasker, remasker and inverse FSMs */
@@ -63,6 +94,7 @@ void enemies_draw(void);
void enemies_swing_start(void);
int enemies_nail(const float *pts, int npts, float direction, int damage);
int enemies_touch_hero(float x0, float y0, float x1, float y1, int *side); /* -> its damage, 0: none */
+void enemies_hero_leave(void); /* HERO LEAVE (the Knight dead): the shade departs */
/* geo of a size (0 small, 1 medium, 2 large) flung from (x, y), each from a little about it (FlingUtils) */
void geo_fling_at(int type, int n, float x, float y, float smin, float smax, float amin, float amax, float spread);
int cardinal(float degrees); /* DirectionUtils.GetCardinalDirection */
@@ -139,7 +171,7 @@ typedef struct {
bool hidden, hit_buffered, enter_without_input;
int16_t entry_gate; /* the record of the gate it came in by (-1: none) */
int8_t buffered_side, buffered_damage, buffered_hazard;
- float invuln_freeze, invuln_time, pulse_t, recoil_timer2, respawn_timer;
+ float invuln_freeze, invuln_time, pulse_t, recoil_timer2, respawn_timer, wake_timer;
bool invuln_routine, pulsing, pulse_reverse, respawning;
int8_t thunk_dir;
bool thunk_hit;
@@ -177,6 +209,23 @@ void hero_stop_anim_control(void); /* StopAnimationControl, StartAnimationCo
void hero_start_anim_control(void);
bool hero_can_focus(void);
bool hero_can_cast(void);
+void hero_face(bool right); /* FaceRight, FaceLeft */
+void hero_gravity(bool on); /* AffectedByGravity */
+void hero_max_health(void); /* MaxHealth */
+float hero_ground_y(float x, float y); /* FindGroundPoint */
+void hero_wake_up_ground(void);
+
+/* the Knight's death (death.c): his Hero Death object, GameManager.PlayerDead, then the respawn */
+void death_start(float x, float y, bool facing_right);
+void death_reset(void);
+void death_tick(void);
+void death_draw(void);
+void shade_spawn_check(void); /* (SceneManager: the shade where the Knight died, if this is that room) */
+
+/* benches (npc.c) */
+void benches_enter(void);
+void benches_tick(void);
+bool bench_respawn(const char *name); /* RESPAWN: the Knight asleep on the bench so named */
/* the Spell Control FSM (spell.c): focus, spells */
void spell_reset(void);
@@ -197,6 +246,7 @@ void fx_dash_burst(float x, float y, bool facing_right, bool on_ground);
void game_new(void);
bool game_enter(int room, float x, float y, bool facing_right);
+bool game_respawn(void); /* the Knight at the save's respawn point (a loaded game, after dying) */
void game_tick(uint32_t keys); /* 1/50 s */
void game_draw(void);
@@ -226,6 +276,7 @@ void prompts_draw(void);
/* ---------------------------------------------------------------- the HUD (hud.c) */
void hud_reset(void);
void hud_slide(bool out); /* the Hud Canvas's Slide Out FSM: OUT, IN */
+void hud_soul_limiter(bool up); /* SOUL LIMITER UP, DOWN: the soul orb cracked or whole */
void hud_tick(void);
void hud_draw(void);
diff --git a/games/hollowknight/src/hero.c b/games/hollowknight/src/hero.c
index 33cc3fd6..eb25f32b 100644
--- a/games/hollowknight/src/hero.c
+++ b/games/hollowknight/src/hero.c
@@ -827,10 +827,22 @@ void hero_draw(void) {
slash_draw();
}
-/* (the Knight dying: for now, as a hazard) */
+/* HeroController.Die: the Knight still and unseen, his Hero Death object in his place (death.c) */
void hero_die(void) {
- g_pd.health = g_pd.max_health;
- die_from_hazard();
+ Hero *h = &g_hero;
+ if (h->cs.dead) return;
+ g_pd.disable_pause = true;
+ h->body.vx = h->body.vy = 0;
+ cancel_recoil_horizontal();
+ affected_by_gravity(false);
+ set_state(HS_NO_INPUT);
+ h->cs.dead = true;
+ reset_motion();
+ h->hard_landing_timer = 0;
+ h->hidden = true;
+ spell_cancel();
+ slash_cancel();
+ death_start(h->body.x, h->body.y, h->cs.facing_right);
}
/* ---------------------------------------------------------------- NailSlash: the slash effect, a child of the Knight */
@@ -1282,8 +1294,37 @@ void hero_hazard_respawn(void) {
cam_snap_to_hero();
}
+/* HeroController.Respawn's end, the Knight not on a bench: he wakes up on the ground (Wake Up Ground), then has control */
+void hero_wake_up_ground(void) {
+ Hero *h = &g_hero;
+ anim_play_from_frame(&h->anim, CLIP_KNIGHT_WAKE_UP_GROUND, 0);
+ h->anim_control = false;
+ h->control_relinquished = true;
+ h->wake_timer = clip_duration(CLIP_KNIGHT_WAKE_UP_GROUND);
+}
+
+void hero_face(bool right) {
+ if (right) face_right();
+ else face_left();
+}
+void hero_gravity(bool on) { affected_by_gravity(on); }
+void hero_max_health(void) { g_pd.health = g_pd.max_health; }
+
+/* FindGroundPoint: where the Knight stands on the ground below (x, y) (no ground: there) */
+float hero_ground_y(float x, float y) {
+ PhysHit hit;
+ if (!phys_ray(x, y, 0, -1, 10, CF_TERRAIN, &hit)) return y;
+ return hit.y + g_hero.body.hy - g_hero.body.oy + 0.01f;
+}
+
static void respawn_tick(void) {
Hero *h = &g_hero;
+ if (h->wake_timer > 0 && (h->wake_timer -= DT) <= 0) {
+ h->anim_control = true;
+ h->anim_state = h->state;
+ h->control_relinquished = false;
+ hero_finished_entering_scene(true);
+ }
if (!h->respawning) return;
h->respawn_timer -= DT;
if (h->respawn_timer > 0) return;
diff --git a/games/hollowknight/src/hud.c b/games/hollowknight/src/hud.c
index 7f1415d4..bcc4d096 100644
--- a/games/hollowknight/src/hud.c
+++ b/games/hollowknight/src/hud.c
@@ -22,7 +22,8 @@
static struct {
Anim mask[MAX_MASKS];
int8_t shown_health; /* what the masks show */
- Anim frame, liquid, coin;
+ Anim frame, liquid, coin, burst;
+ bool burst_on;
float liquid_grey; /* (Liquid Control: grey while it cannot heal) */
bool started;
/* Slide Out: the canvas shrinks about its corner (iTweenScaleTo, 0.15 s), then goes off the screen */
@@ -38,11 +39,18 @@ void hud_slide(bool out) {
hd.scale_from = hd.scale, hd.scale_t = 0;
}
+/* the soul orb's HUD_frame (Load Animation): cracked while the soul is limited; Limiter Burst (Animate) */
+void hud_soul_limiter(bool up) {
+ anim_play_from_frame(&hd.frame, up ? CLIP_HUD_HUD_FRAME_CRACKAPPEAR : CLIP_HUD_HUD_FRAME_IDLE, 0);
+ anim_play_from_frame(&hd.burst, CLIP_HUD_SOUL_BURST, 0);
+ hd.burst_on = true;
+}
+
void hud_reset(void) {
memset(&hd, 0, sizeof hd);
hd.shown_health = -1;
hd.scale = 1, hd.scale_t = SCALE_TIME;
- anim_play(&hd.frame, CLIP_HUD_HUD_FRAME_IDLE);
+ anim_play(&hd.frame, g_pd.soul_limited ? CLIP_HUD_HUD_FRAME_CRACKED : CLIP_HUD_HUD_FRAME_IDLE);
anim_play(&hd.liquid, CLIP_LIQUID_IDLE);
anim_play(&hd.coin, CLIP_HUD_COIN_IDLE);
}
@@ -63,7 +71,14 @@ void hud_tick(void) {
anim_update(a, DT);
if (a->events & ANIM_DONE) anim_play(a, a->clip == CLIP_HUD_HEALTH_BREAK ? CLIP_HUD_HEALTH_EMPTY : CLIP_HUD_HEALTH_IDLE);
}
+ hd.frame.events = 0;
anim_update(&hd.frame, DT);
+ if ((hd.frame.events & ANIM_DONE) && hd.frame.clip == CLIP_HUD_HUD_FRAME_CRACKAPPEAR) anim_play(&hd.frame, CLIP_HUD_HUD_FRAME_CRACKED);
+ if (hd.burst_on) {
+ hd.burst.events = 0;
+ anim_update(&hd.burst, DT);
+ if (hd.burst.events & ANIM_DONE) hd.burst_on = false;
+ }
anim_update(&hd.liquid, DT);
anim_update(&hd.coin, DT);
if (hd.scale_t < SCALE_TIME) {
@@ -98,6 +113,7 @@ void hud_draw(void) {
hud_sprite(hd.liquid.sprite, FILL_X - 2.23f, y, 1, gfx_dyn_tint(9, g, g, g, 255), 1);
}
hud_sprite(hd.frame.sprite, ORB_X + 0.12f, ORB_Y + 0.92f, 1, white, 0);
+ if (hd.burst_on) hud_sprite(hd.burst.sprite, ORB_X - 0.715f, ORB_Y + 0.4318f, 1, white, 0);
/* the masks */
int max = g_pd.max_health > MAX_MASKS ? MAX_MASKS : g_pd.max_health;
for (int i = 0; i < max; i++) hud_sprite(hd.mask[i].sprite, HEALTH_X - 10.32f + 0.94f * i, HEALTH_Y + 7.7f, 1, white, 0);
diff --git a/games/hollowknight/src/npc.c b/games/hollowknight/src/npc.c
new file mode 100644
index 00000000..fe20aaf1
--- /dev/null
+++ b/games/hollowknight/src/npc.c
@@ -0,0 +1,230 @@
+/* What the Knight interacts with by pressing up: benches (Bench Control, its Detect Range's Detect Hero). */
+#include <math.h>
+#include "game.h"
+
+#define DT 0.02f
+#define MAX_BENCHES 2
+
+enum {
+ BS_IDLE, BS_IN_RANGE, BS_CANCEL_FRAME, BS_START_REST, BS_SAVE_FRAME, BS_PAUSE, BS_RESTING_INIT, BS_RESTING,
+ BS_WAKE, BS_GET_OFF, BS_IDLE_PAUSE, BS_REGAIN, BS_RESPAWN_ASLEEP, BS_STARTLE
+};
+
+typedef struct {
+ uint16_t ent;
+ uint8_t state;
+ int8_t prompt;
+ bool near; /* (Detect Hero: the Knight in its Detect Range) */
+ bool sleeping, get_off_wake, face_right;
+ float t, sx, sy, fx, fy; /* (a timer; the Knight's tween: from, to) */
+} Bench;
+static Bench benches[MAX_BENCHES];
+static int nbenches;
+
+static const Ent *ent_at(int i) {
+ int n;
+ return &room_ents(&n)[i];
+}
+
+/* the Knight's collider in a trigger's box */
+static bool hero_in(const Ent *b) {
+ const Body *k = &g_hero.body;
+ float x0 = k->x + k->ox - k->hx, x1 = k->x + k->ox + k->hx, y0 = k->y + k->oy - k->hy, y1 = k->y + k->oy + k->hy;
+ return x0 < b->x1 && x1 > b->x0 && y0 < b->y1 && y1 > b->y0;
+}
+
+static uint32_t pressed(void) { return g_hero.keys & ~g_hero.prev_keys; }
+
+static void near_changed(Bench *b, const Ent *e, bool near) {
+ /* Detect Hero: Close, Idle (EaseColor over 0.5 s: the bench lit, its light brighter) */
+ b->near = near;
+ if (e->group) group_fade(e->group, near ? 1 : 0, 0.5f);
+ if (e->group2) group_fade(e->group2, near ? 1 : 0.3686f, 0.5f);
+}
+
+void benches_enter(void) {
+ nbenches = 0;
+ int n;
+ const Ent *es = room_ents(&n);
+ for (int i = 0; i < n && nbenches < MAX_BENCHES; i++) {
+ if (es[i].type != ENT_OBJ || es[i].flags != OK_BENCH) continue;
+ Bench *b = &benches[nbenches++];
+ memset(b, 0, sizeof *b);
+ b->ent = (uint16_t)i, b->prompt = -1;
+ if (es[i].group) group_fade(es[i].group, 0, 0);
+ if (es[i].group2) group_fade(es[i].group2, 0.3686f, 0);
+ }
+}
+
+/* the Knight sits at the bench: Start Rest */
+static void start_rest(Bench *b, const Ent *e) {
+ Hero *h = &g_hero;
+ hero_relinquish_control();
+ hero_stop_anim_control();
+ hero_gravity(false);
+ prompt_hide(b->prompt), b->prompt = -1;
+ anim_play(&h->anim, CLIP_KNIGHT_SIT);
+ /* (iTweenMoveTo: over the bench, a little up, in 0.2 s) */
+ b->sx = h->body.x, b->sy = h->body.y, b->fx = e->x0 + e->x1, b->fy = h->body.y + e->y1;
+ b->t = 0;
+ b->state = BS_START_REST;
+}
+
+/* Rest Burst: health back, the bench the place to come back to, then the game saved */
+static void rest_burst(Bench *b, const Ent *e) {
+ hero_max_health();
+ b->sleeping = false, b->get_off_wake = false;
+ save_set_respawn(str_at(e->s0), b->face_right = g_hero.cs.facing_right);
+ b->state = BS_PAUSE, b->t = 0;
+}
+
+static void get_off(Bench *b, const Ent *e) {
+ /* Get Off: the Knight back down from the bench */
+ Hero *h = &g_hero;
+ hero_gravity(true);
+ anim_play_from_frame(&h->anim, b->get_off_wake ? CLIP_KNIGHT_WAKE : CLIP_KNIGHT_GET_OFF, 0);
+ hud_slide(false);
+ b->sx = h->body.x, b->sy = h->body.y, b->fx = h->body.x - e->x1, b->fy = h->body.y - e->y1;
+ b->t = 0;
+ b->state = BS_GET_OFF;
+}
+
+static void tween_hero(Bench *b, float time, bool ease_out) {
+ /* (iTweenMoveTo, easeOutSine; iTweenMoveBy, linear) */
+ Hero *h = &g_hero;
+ float k = b->t >= time ? 1 : b->t / time;
+ if (ease_out) k = sinf(k * 1.5707964f);
+ h->body.x = b->sx + (b->fx - b->sx) * k, h->body.y = b->sy + (b->fy - b->sy) * k;
+ h->body.vx = h->body.vy = 0;
+}
+
+void benches_tick(void) {
+ Hero *h = &g_hero;
+ for (int i = 0; i < nbenches; i++) {
+ Bench *b = &benches[i];
+ const Ent *e = ent_at(b->ent);
+ const Ent *rest = e + 1, *range = e + 2;
+ bool near = hero_in(range);
+ if (near != b->near) near_changed(b, e, near);
+ bool in = hero_in(rest);
+ b->t += DT;
+ switch (b->state) {
+ case BS_IDLE:
+ if (in) {
+ b->prompt = (int8_t)prompt_show(b->prompt, TXT_PROMPT_REST, e->p0, e->p1);
+ b->state = BS_IN_RANGE;
+ }
+ break;
+ case BS_IN_RANGE:
+ if (!in) {
+ prompt_hide(b->prompt), b->prompt = -1;
+ b->state = BS_IDLE;
+ } else if (pressed() & (K_UP | K_DOWN)) {
+ /* Can Rest? */
+ if (!h->accepting_input || !h->cs.on_ground || h->cs.attacking || h->cs.up_attacking || h->cs.down_attacking ||
+ h->cs.dashing)
+ b->state = BS_CANCEL_FRAME;
+ else
+ start_rest(b, e);
+ }
+ break;
+ case BS_CANCEL_FRAME:
+ prompt_hide(b->prompt), b->prompt = -1;
+ b->state = BS_IDLE;
+ break;
+ case BS_START_REST:
+ tween_hero(b, 0.2f, true);
+ if (b->t >= 0.3f) b->state = BS_SAVE_FRAME;
+ break;
+ case BS_SAVE_FRAME:
+ rest_burst(b, e);
+ break;
+ case BS_PAUSE:
+ if (b->t >= 0.1f) {
+ save_game();
+ /* (no map, no charm message yet) Resting Init: 0.5 s */
+ b->state = BS_RESTING_INIT, b->t = 0;
+ }
+ break;
+ case BS_RESTING_INIT:
+ if (b->t >= 0.5f) b->state = BS_RESTING, b->t = 0;
+ break;
+ case BS_RESTING: {
+ uint32_t p = pressed();
+ bool up = p & (K_JUMP | K_ATTACK | K_UP | K_DOWN), left = p & K_LEFT, right = p & K_RIGHT;
+ if (up || left || right) {
+ /* Get Up, Get Left, Get Right: facing that way (Facing?) */
+ bool r = left ? false : right ? true : h->cs.facing_right;
+ if (r) hero_face(true);
+ else hero_face(false);
+ b->state = BS_WAKE, b->t = 0;
+ if (b->sleeping) anim_play_from_frame(&h->anim, CLIP_KNIGHT_WAKE_TO_SIT, 0);
+ } else if (b->t >= 10 && !b->sleeping) {
+ /* Fall Asleep */
+ anim_play(&h->anim, CLIP_KNIGHT_SIT_FALL_ASLEEP);
+ b->get_off_wake = true, b->sleeping = true;
+ b->t = 0;
+ }
+ break;
+ }
+ case BS_WAKE:
+ /* Wake?: the Wake To Sit animation first if asleep */
+ if (!b->sleeping || (h->anim.events & ANIM_DONE) || !h->anim.playing) get_off(b, e);
+ break;
+ case BS_GET_OFF:
+ tween_hero(b, 0.2f, false);
+ if (b->t >= 0.25f || (h->anim.events & ANIM_DONE)) {
+ anim_play(&h->anim, CLIP_KNIGHT_IDLE);
+ b->state = BS_IDLE_PAUSE, b->t = 0;
+ }
+ break;
+ case BS_IDLE_PAUSE:
+ if (b->t >= 0.1f) {
+ hero_regain_control();
+ hero_start_anim_control();
+ b->state = BS_REGAIN, b->t = 0;
+ }
+ break;
+ case BS_REGAIN:
+ if (b->t >= 0.5f) b->state = BS_IDLE; /* (Reactivate: Idle, in range or not) */
+ break;
+ case BS_RESPAWN_ASLEEP:
+ /* Init Resting 2 (1.2 s), three frames, Kinemetise (up by the adjust vector), Init Resting (1.2 s): asleep,
+ * then Startle */
+ h->body.vx = h->body.vy = 0;
+ if (b->t >= 1.26f && !b->sleeping) b->sleeping = true, h->body.x += e->x1, h->body.y += e->y1;
+ if (b->t >= 2.46f) {
+ anim_play_from_frame(&h->anim, CLIP_KNIGHT_WAKE_TO_SIT, 0);
+ b->state = BS_STARTLE;
+ }
+ break;
+ case BS_STARTLE:
+ h->body.vx = h->body.vy = 0;
+ if ((h->anim.events & ANIM_DONE) || !h->anim.playing) {
+ b->get_off_wake = false, b->sleeping = false; /* (Startle: Get Off Anim "Get Off") */
+ b->state = BS_RESTING, b->t = 0;
+ }
+ break;
+ }
+ }
+}
+
+/* RESPAWN at the bench named so: the Knight (at its marker, on the ground) asleep on it; false if no such bench */
+bool bench_respawn(const char *name) {
+ for (int i = 0; i < nbenches; i++) {
+ Bench *b = &benches[i];
+ const Ent *e = ent_at(b->ent);
+ if (strcmp(str_at(e->s0), name)) continue;
+ Hero *h = &g_hero;
+ hero_relinquish_control();
+ hero_stop_anim_control();
+ hero_gravity(false);
+ hero_max_health();
+ anim_play(&h->anim, CLIP_KNIGHT_SIT_FALL_ASLEEP);
+ h->body.vx = h->body.vy = 0;
+ b->sleeping = false, b->get_off_wake = false; /* (sleeping: here, once moved up) */
+ b->state = BS_RESPAWN_ASLEEP, b->t = 0;
+ return true;
+ }
+ return false;
+}
diff --git a/games/hollowknight/src/obj.c b/games/hollowknight/src/obj.c
index de7e7d91..e7d6dec8 100644
--- a/games/hollowknight/src/obj.c
+++ b/games/hollowknight/src/obj.c
@@ -308,13 +308,14 @@ void obj_enter(void) {
nobjs = 0;
phys_colliders_reset();
enemies_enter();
+ benches_enter();
memset(pieces, 0, sizeof pieces);
memset(swing_hit, 0, sizeof swing_hit);
int n;
const Ent *es = room_ents(&n);
for (int i = 0; i < n && nobjs < MAX_OBJS; i++) {
if (es[i].type != ENT_OBJ) continue;
- if (es[i].flags == OK_ENEMY) continue; /* (enemy.c's) */
+ if (es[i].flags == OK_ENEMY || es[i].flags == OK_BENCH) continue; /* (enemy.c's, npc.c's) */
Obj *o = &objs[nobjs++];
memset(o, 0, sizeof *o);
o->kind = es[i].flags, o->ent = (uint16_t)i;
diff --git a/games/hollowknight/src/save.c b/games/hollowknight/src/save.c
new file mode 100644
index 00000000..b0286641
--- /dev/null
+++ b/games/hollowknight/src/save.c
@@ -0,0 +1,115 @@
+/* GameManager.SaveGame and LoadGame: PlayerData in a file of the calculator's, one a slot. The file: a header with a
+ * checksum, PlayerData's saved part, then the objects' states by name (tools/ents.py: PHASH), so that a save keeps
+ * meaning the same objects whatever the data's version. A file that does not check out is never loaded. */
+#include "game.h"
+
+#define SAVE_MAGIC 0x56534B48u /* "HKSV" */
+#define SAVE_VERSION 1
+
+typedef struct {
+ uint32_t magic, version, size, crc; /* (size, crc: of what follows the header) */
+} SaveHead;
+
+static int cur_slot;
+
+static uint32_t crc32(const uint8_t *p, uint32_t n) {
+ uint32_t c = 0xFFFFFFFFu;
+ while (n--) {
+ c ^= *p++;
+ for (int k = 0; k < 8; k++) c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1)));
+ }
+ return ~c;
+}
+
+static void slot_name(int slot, char *out) {
+ memcpy(out, "hk1.sav", 8);
+ out[2] = (char)('1' + slot);
+}
+
+void save_select(int slot) { cur_slot = slot < 0 || slot >= SAVE_SLOTS ? 0 : slot; }
+
+static const uint8_t *phash(uint32_t *n) {
+ const uint8_t *s = section(SEC_PHASH);
+ *n = rd32(s);
+ return s + 4;
+}
+
+bool save_game(void) {
+ /* (built in the decoders' scratch memory: no decoding goes on meanwhile) */
+ uint8_t *buf = (uint8_t *)g_scratch;
+ uint32_t cap = sizeof g_scratch;
+ SaveHead h = {SAVE_MAGIC, SAVE_VERSION, 0, 0};
+ uint32_t at = sizeof h;
+ memcpy(buf + at, &g_pd, PD_SAVED);
+ at += PD_SAVED;
+ uint32_t nbits, count = 0;
+ const uint8_t *names = phash(&nbits);
+ uint32_t count_at = at;
+ at += 4;
+ for (uint32_t i = 0; i < nbits && i < MAX_PERSIST; i++) {
+ if (!(g_pd.persist[i >> 3] >> (i & 7) & 1)) continue;
+ if (at + 4 > cap) return false;
+ memcpy(buf + at, names + 4 * i, 4);
+ at += 4, count++;
+ }
+ memcpy(buf + count_at, &count, 4);
+ h.size = at - sizeof h;
+ h.crc = crc32(buf + sizeof h, h.size);
+ memcpy(buf, &h, sizeof h);
+ char name[8];
+ slot_name(cur_slot, name);
+ return plat_save(name, buf, at);
+}
+
+/* a slot's file, checked: its PlayerData part, its states' names */
+static const uint8_t *checked(int slot, uint32_t *count) {
+ char name[8];
+ slot_name(slot, name);
+ uint32_t len;
+ const uint8_t *p = plat_load(name, &len);
+ SaveHead h;
+ if (!p || len < sizeof h + PD_SAVED + 4) return NULL;
+ memcpy(&h, p, sizeof h);
+ if (h.magic != SAVE_MAGIC || h.version != SAVE_VERSION || h.size != len - sizeof h || h.crc != crc32(p + sizeof h, h.size))
+ return NULL;
+ memcpy(count, p + sizeof h + PD_SAVED, 4);
+ if (sizeof h + PD_SAVED + 4 + 4ull * *count != len) return NULL;
+ return p + sizeof h;
+}
+
+bool save_exists(int slot) {
+ uint32_t count;
+ return checked(slot, &count) != NULL;
+}
+
+bool save_load(int slot) {
+ uint32_t count;
+ const uint8_t *p = checked(slot, &count);
+ if (!p) return false;
+ save_select(slot);
+ memset(&g_pd, 0, sizeof g_pd);
+ memcpy(&g_pd, p, PD_SAVED);
+ /* (names kept within their bounds whatever the file says) */
+ g_pd.respawn_scene[SCENE_NAME - 1] = g_pd.respawn_marker[SCENE_NAME - 1] = g_pd.shade_scene[SCENE_NAME - 1] = 0;
+ uint32_t nbits;
+ const uint8_t *names = phash(&nbits);
+ const uint8_t *q = p + PD_SAVED + 4;
+ for (uint32_t k = 0; k < count; k++, q += 4) {
+ uint32_t want = rd32(q);
+ for (uint32_t i = 0; i < nbits && i < MAX_PERSIST; i++)
+ if (rd32(names + 4 * i) == want) {
+ g_pd.persist[i >> 3] |= (uint8_t)(1 << (i & 7));
+ break;
+ }
+ }
+ return true;
+}
+
+void save_set_respawn(const char *marker, bool facing_right) {
+ /* (SetPlayerDataString respawnMarkerName, respawnScene; respawnType 1: a bench) */
+ strncpy(g_pd.respawn_marker, marker, SCENE_NAME - 1);
+ strncpy(g_pd.respawn_scene, room_name(g_room.id), SCENE_NAME - 1);
+ g_pd.respawn_marker[SCENE_NAME - 1] = g_pd.respawn_scene[SCENE_NAME - 1] = 0;
+ g_pd.respawn_type = 1;
+ g_pd.respawn_facing_right = facing_right;
+}
diff --git a/games/hollowknight/tests/play.c b/games/hollowknight/tests/play.c
index 8f33da44..75757e40 100644
--- a/games/hollowknight/tests/play.c
+++ b/games/hollowknight/tests/play.c
@@ -56,7 +56,15 @@ int main(int argc, char **argv) {
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);
+ if (getenv("HKGEO")) g_pd.geo = atoi(getenv("HKGEO"));
+ /* (HKLOAD=slot: that save loaded, the Knight at its respawn point) */
+ if (getenv("HKLOAD")) {
+ if (!save_load(atoi(getenv("HKLOAD"))) || (getenv("HKGEO") && (g_pd.geo = atoi(getenv("HKGEO")), 0)) || !game_respawn()) {
+ printf("load failed\n");
+ return 1;
+ }
+ } else
+ game_enter(id, px, py, true);
int tick = 0;
for (const char *p = play; *p;) {
uint32_t keys = 0;
@@ -72,6 +80,7 @@ int main(int argc, char **argv) {
dialogue_box_up();
dialogue_start(atoi(strchr(getenv("HKTALK"), ':') + 1));
}
+ if (getenv("HKKILL") && atoi(getenv("HKKILL")) == tick) hero_take_damage(SIDE_LEFT, 99, HAZ_NORMAL);
if (getenv("HKPROMPT") && atoi(getenv("HKPROMPT")) == tick)
prompt_show(-1, TXT_PROMPT_LISTEN, g_hero.body.x, g_hero.body.y + 1.5f);
game_tick(keys);
diff --git a/games/hollowknight/tools/actors.py b/games/hollowknight/tools/actors.py
index 6ffee6bf..7b0148c6 100644
--- a/games/hollowknight/tools/actors.py
+++ b/games/hollowknight/tools/actors.py
@@ -17,7 +17,7 @@ KNIGHT = ("resources.assets", 20600, [
"Map Away", "Map Update", "Enter", "Exit", "Exit Door To Idle", "TurnToBG", "TurnFromBG", "Roar Lock",
"Prostrate", "Prostrate Rise", "Wake Up Ground", "Collect Normal 1", "Collect Normal 2", "Collect Normal 3",
"Collect Magical 1", "Collect Magical 2", "Collect Magical 3", "Collect Magical Fall", "Collect Magical Land",
- "Collect Heart Piece", "Collect Heart Piece End", "Collect StandToIdle", "GetUpToIdle",
+ "Collect Heart Piece", "Collect Heart Piece End", "Collect StandToIdle", "GetUpToIdle", "Death Head Cracked",
])
# enemies: their libraries (all their clips)
@@ -29,13 +29,19 @@ ACTORS = {"knight": KNIGHT,
# the HUD: drawn by its own camera, 11.48 pixels a unit (scenes: K0), masks at 0.7135
"hud": ("resources.assets", 20665, ["Health Empty", "Health Idle", "Health Break", "Health Refill", "Health Appear",
"Blue Appear", "Blue Idle", "Blue Break", "HUD Frame", "HUD Frame Idle",
- "Coin Appear", "Coin Idle", "Coin Get", "Soul Burst"]),
+ "HUD Frame CrackAppear", "HUD Frame Cracked", "Coin Appear", "Coin Idle",
+ "Coin Get", "Soul Burst"]),
"liquid": ("resources.assets", 20843, None),
"geo": ("resources.assets", 23266, ["Small Idle", "Small Air", "Med Idle", "Med Air", "Large Idle", "Large Air"]),
# the dialogue box (on the HUD, a third bigger with its text) and the prompt markers
"dialogue": ("resources.assets", 20724, ["Arrow Up", "Arrow Down", "Stop Up", "Stop Down", "Fleur Top Up",
"Fleur Top Down", "Fleur Bot Up", "Fleur Bot Down"]),
- "prompt": ("resources.assets", 23333, ["Up", "Down", "Blank"])}
+ "prompt": ("resources.assets", 23333, ["Up", "Down", "Blank"]),
+ # the shade (Hollow Shade, and the Hero Death's rising one)
+ "shade": ("resources.assets", 22801, ["Idle", "Startle", "Fly", "TurnToFly", "TurnToIdle", "Slash Antic", "Slash",
+ "Slash CD", "Slash Effect", "Cast Antic", "Cast Charge", "Cast",
+ "Retreat Start", "Retreat End", "Death Start", "Death", "Depart",
+ "Appear", "Fireball", "Fireball End", "Cast Ring"])}
# sprites of prefab objects the game draws itself: name -> (file, prefab, object, resolution)
NAMED = {
@@ -43,6 +49,7 @@ NAMED = {
# (these at a scale of their own: the box's, with its text's; the shadow's x)
"DIALOGUE_BACKBOARD": ("resources.assets", 4446, "DialogueManager/DialogueBox/backboard", "hud", 2.325 * text.TEXT_K),
"PROMPT_SHADOW": ("resources.assets", 8342, "Arrow Prompt New/Shadow", 1.0, 0.8),
+ "CORPSE_NAIL": ("resources.assets", 6648, "Corpse Nail Hero", 1.0),
}
diff --git a/games/hollowknight/tools/ents.py b/games/hollowknight/tools/ents.py
index e097d5f5..312fed0e 100644
--- a/games/hollowknight/tools/ents.py
+++ b/games/hollowknight/tools/ents.py
@@ -6,9 +6,9 @@ import numpy as np
# record types (src/game.h: ENT_*)
ENT_CAMLOCK, ENT_GATE, ENT_HAZARD_MARKER, ENT_RESPAWN, ENT_HAZARD_TRIGGER, ENT_MASK, ENT_DAMAGE, ENT_SHAPE, ENT_BOX, \
- ENT_OBJ, ENT_PIECE = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
+ ENT_OBJ, ENT_PIECE, ENT_SHADE_MARKER = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
# objects (ENT_OBJ's flags: src/obj.c)
-OK_BREAKABLE, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST = 1, 2, 3, 4, 5
+OK_BREAKABLE, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST, OK_BENCH = 1, 2, 3, 4, 5, 6
# enemies (an OK_ENEMY's a: src/enemy.c), by their FSM and animation library
EK_CRAWLER, EK_BUZZER = 1, 2
ENEMIES = {("Crawler", 1113): EK_CRAWLER, ("chaser", 1150): EK_BUZZER}
@@ -29,15 +29,28 @@ NO_PERSIST = 0xFFFF
class Persist:
"""The objects whose state a save keeps (PersistentBoolItem): one bit each (or a few: a number)."""
def __init__(self):
- self.keys, self.n = {}, 0
+ self.keys, self.n, self.bits = {}, 0, []
def id(self, room, path, bits=1):
if (room, path) not in self.keys:
self.keys[(room, path)] = self.n
+ self.bits.append(bits)
self.n += bits
assert self.n <= 1024, self.n # (src/game.h: MAX_PERSIST)
return self.keys[(room, path)]
+ def blob(self):
+ """PHASH: each bit's name (a CRC of its room, object and bit), as saves keep them: the same objects across
+ versions of the game's data"""
+ import zlib
+ out = [0] * self.n
+ for (room, path), first in self.keys.items():
+ n = self.bits[list(self.keys).index((room, path))]
+ for k in range(n):
+ out[first + k] = zlib.crc32(("%s|%s|%d" % (room, path, k)).encode()) or 1
+ assert len(set(out)) == len(out)
+ return struct.pack("<I%dI" % len(out), len(out), *out)
+
class Strings:
def __init__(self):
@@ -416,6 +429,12 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None):
x, y = o["pos"][:2]
recs.append(rec(ENT_RESPAWN, FACING_RIGHT if v.get("respawnFacingRight") else 0, (x, y, x, y),
s0=strings.id(o["name"])))
+ f = _fsm(o, ("Spawn Offset",))
+ if f:
+ # (where a shade goes when the Knight dies near: the marker and its offset; its special type)
+ var = lambda k, dflt: (f.get("vars", {}).get(k) or [0, dflt])[1]
+ x, y = o["pos"][0] + var("X", 0), o["pos"][1] + var("Y", 0)
+ recs.append(rec(ENT_SHADE_MARKER, box=(x, y, o["pos"][0], o["pos"][1]), a=var("Special Type", 0)))
for o in objs:
f = _fsm(o, ("unmasker", "remasker", "remasker_inverse"))
if f:
@@ -431,6 +450,23 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None):
persist=persist.id(name, o["path"]) if persistent else NO_PERSIST))
recs += _more_boxes(o)
classes = {c.get("class") for c in o["c"]}
+ f = _fsm(o, ("Bench Control",))
+ if f:
+ # a bench: its place, the Knight's offset sitting, its prompt's place; its Lit and Light sprites' groups
+ # (they glow with the Knight near); then its own trigger (Rest) and its Detect Range's
+ var = lambda k, dflt: (f.get("vars", {}).get(k) or [0, dflt])[1]
+ adj = var("Adjust Vector", [0, 0, 0])
+ kids = {by_id[c]["name"]: by_id[c] for c in o.get("children", [])}
+ pm = kids.get("Prompt Marker")
+ g_lit = new_group(kids["Lit"]["id"]) if "Lit" in kids else 0
+ g_light = new_group(kids["Light"]["id"]) if "Light" in kids else 0
+ box = _trigger(o) or (0, 0, 0, 0)
+ dr = _trigger(kids["Detect Range"]) if "Detect Range" in kids else None
+ recs.append(rec(ENT_OBJ, OK_BENCH, (o["pos"][0], o["pos"][1], adj[0], adj[1]),
+ (pm["pos"][0] if pm else o["pos"][0], pm["pos"][1] if pm else o["pos"][1] + 3, 0, 0),
+ group=g_lit, group2=g_light, s0=strings.id(o["name"])))
+ recs.append(rec(ENT_BOX, 0, box))
+ recs.append(rec(ENT_BOX, 0, dr or box))
en = _enemy(o, by_id, persist, name) or _props(d, o, by_id, rooms, strings, persist, sprites, owners, name)
if en:
recs += en
diff --git a/games/hollowknight/tools/pack.py b/games/hollowknight/tools/pack.py
index 2ea0bbc0..d97c9946 100644
--- a/games/hollowknight/tools/pack.py
+++ b/games/hollowknight/tools/pack.py
@@ -28,7 +28,7 @@ def room_data(name):
ROOM_DATA[name] = (cr, ents.room(d, ROOMS, STRINGS, PERSIST, name, SPRITES, cr[3]))
return ROOM_DATA[name]
-SECTIONS = ["TEX", "TMAP", "PAL", "BIDX", "BLK", "ROOMS", "RBLOB", "PRIOR", "SOFT", "SPR", "CLIP", "STR", "TEXT", "FONT"]
+SECTIONS = ["TEX", "TMAP", "PAL", "BIDX", "BLK", "ROOMS", "RBLOB", "PRIOR", "SOFT", "SPR", "CLIP", "STR", "TEXT", "FONT", "PHASH"]
BLENDS = {"alpha": 0, "add": 1, "screen": 2, "linearlight": 3, "overlay": 4, "multiply": 5}
F_LIT, F_ROT, F_SOLID, F_DYN, F_GRASS = 8, 16, 32, 64, 128
@@ -393,6 +393,7 @@ def main():
secs["STR"].b += STRINGS.blob()
secs["TEXT"].b += TEXTS.blob()
secs["FONT"].b += TEXTS.fonts()
+ secs["PHASH"].b += PERSIST.blob()
# the file: "HKNW", count, then (offset, size) per section
head = struct.pack("<4sI", b"HKNW", len(SECTIONS))
pos = len(head) + 8 * len(SECTIONS)