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: Walker, Wandering Husk; enemy data from the game files

Mason Chen05695dfdc552
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.bin
index 5b09481d..14c78a44 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 2eaf1a46..c8ab7229 100644
--- a/games/hollowknight/src/data.h
+++ b/games/hollowknight/src/data.h
@@ -151,79 +151,88 @@
#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 CLIP_HUSK_ATTACK_ANTICIPATE 94
+#define CLIP_HUSK_IDLE 95
+#define CLIP_HUSK_WALK 96
+#define CLIP_HUSK_TURN 97
+#define CLIP_HUSK_FALL 98
+#define CLIP_HUSK_ATTACK_LUNGE 99
+#define CLIP_HUSK_ATTACK_COOLDOWN 100
+#define CLIP_HUSK_DEATH_AIR 101
+#define CLIP_HUSK_DEATH_LAND 102
+#define CLIP_GEOROCK_IDLE_1 103
+#define CLIP_GEOROCK_GLEAM_1 104
+#define CLIP_GEOROCK_BROKEN_1 105
+#define CLIP_GEOROCK_IDLE_2 106
+#define CLIP_GEOROCK_GLEAM_2 107
+#define CLIP_GEOROCK_BROKEN_2 108
+#define CLIP_CHEST_IDLE 109
+#define CLIP_CHEST_OPEN 110
+#define CLIP_CHEST_BURST 111
+#define CLIP_CHEST_OPENED 112
+#define CLIP_HUD_HEALTH_EMPTY 113
+#define CLIP_HUD_HEALTH_IDLE 114
+#define CLIP_HUD_HEALTH_BREAK 115
+#define CLIP_HUD_HEALTH_REFILL 116
+#define CLIP_HUD_HEALTH_APPEAR 117
+#define CLIP_HUD_BLUE_APPEAR 118
+#define CLIP_HUD_BLUE_IDLE 119
+#define CLIP_HUD_BLUE_BREAK 120
+#define CLIP_HUD_HUD_FRAME 121
+#define CLIP_HUD_HUD_FRAME_IDLE 122
+#define CLIP_HUD_HUD_FRAME_CRACKAPPEAR 123
+#define CLIP_HUD_HUD_FRAME_CRACKED 124
+#define CLIP_HUD_COIN_APPEAR 125
+#define CLIP_HUD_COIN_IDLE 126
+#define CLIP_HUD_COIN_GET 127
+#define CLIP_HUD_SOUL_BURST 128
+#define CLIP_LIQUID_FILL 129
+#define CLIP_LIQUID_IDLE 130
+#define CLIP_LIQUID_SHRINK 131
+#define CLIP_LIQUID_DRAIN 132
+#define CLIP_GEO_SMALL_IDLE 133
+#define CLIP_GEO_SMALL_AIR 134
+#define CLIP_GEO_MED_IDLE 135
+#define CLIP_GEO_MED_AIR 136
+#define CLIP_GEO_LARGE_IDLE 137
+#define CLIP_GEO_LARGE_AIR 138
+#define CLIP_DIALOGUE_ARROW_UP 139
+#define CLIP_DIALOGUE_ARROW_DOWN 140
+#define CLIP_DIALOGUE_STOP_UP 141
+#define CLIP_DIALOGUE_STOP_DOWN 142
+#define CLIP_DIALOGUE_FLEUR_TOP_UP 143
+#define CLIP_DIALOGUE_FLEUR_TOP_DOWN 144
+#define CLIP_DIALOGUE_FLEUR_BOT_UP 145
+#define CLIP_DIALOGUE_FLEUR_BOT_DOWN 146
+#define CLIP_PROMPT_UP 147
+#define CLIP_PROMPT_DOWN 148
+#define CLIP_PROMPT_BLANK 149
+#define CLIP_SHADE_IDLE 150
+#define CLIP_SHADE_STARTLE 151
+#define CLIP_SHADE_FLY 152
+#define CLIP_SHADE_TURNTOFLY 153
+#define CLIP_SHADE_TURNTOIDLE 154
+#define CLIP_SHADE_SLASH_ANTIC 155
+#define CLIP_SHADE_SLASH 156
+#define CLIP_SHADE_SLASH_CD 157
+#define CLIP_SHADE_SLASH_EFFECT 158
+#define CLIP_SHADE_CAST_ANTIC 159
+#define CLIP_SHADE_CAST_CHARGE 160
+#define CLIP_SHADE_CAST 161
+#define CLIP_SHADE_RETREAT_START 162
+#define CLIP_SHADE_RETREAT_END 163
+#define CLIP_SHADE_DEATH_START 164
+#define CLIP_SHADE_DEATH 165
+#define CLIP_SHADE_DEPART 166
+#define CLIP_SHADE_APPEAR 167
+#define CLIP_SHADE_FIREBALL 168
+#define CLIP_SHADE_FIREBALL_END 169
+#define CLIP_SHADE_CAST_RING 170
+#define SPRITE_HERO_LIGHT 732
+#define SPRITE_DIALOGUE_BACKBOARD 733
+#define SPRITE_PROMPT_SHADOW 734
+#define SPRITE_CORPSE_NAIL 735
+#define SPRITE_DIGIT0 916
#define STYLE_DIALOGUE 0
#define STYLE_PROMPT 1
#define TEXT_K 1.333333f
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index c31fb397..bfdddb68 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -9,24 +9,22 @@
#define TERRAIN_FRICTION 0.2f /* (the Terrain material; with another, their geometric mean) */
/* ---------------------------------------------------------------- what each kind is */
-enum { EK_CRAWLER = 1, EK_BUZZER, EK_SHADE };
+enum { EK_CRAWLER = 1, EK_BUZZER, EK_SHADE, EK_HUSK };
#define NO_ENT 0xFFFF /* (an enemy spawned, not one of the room's) */
+/* the records after an enemy's (tools/ents.py: ET_*) */
+enum { ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE };
+enum { CF_BREAKER = 1, CF_FACES_RIGHT = 2, CF_LOW_ARC = 4 }; /* a corpse's (ET_CORPSE) */
+enum { WF_PAUSES = 1, WF_IGNORE_HOLES = 2, WF_NO_TURN_TO_HERO = 4, WF_START_INACTIVE = 8, WF_AMBUSH = 16, WF_WAIT_HERO_X = 32,
+ WF_PREVENT_TURN = 64, WF_NO_SCALE = 128, WF_RIGHT_NEG = 256 }; /* a Walker's (ET_WALKER) */
typedef struct {
- /* Recoil */
- float recoil_speed, recoil_time;
- bool stop_vx_up;
- /* the corpse (EnemyDeathEffects, its Corpse prefab) */
- float corpse_spawn_y, corpse_fling, corpse_gravity, corpse_ox, corpse_oy, corpse_hx, corpse_hy, corpse_bounce;
- bool breaker;
- int clip_death_air, clip_death_land;
- float gravity, friction; /* its Rigidbody2D, its collider's material with the terrain's */
+ int clip_death_air, clip_death_land; /* its corpse's (Corpse: Death Air, Death Land) */
+ int clip_idle, clip_turn, clip_walk; /* (Walker's) */
} Kind;
static const Kind kinds[] = {
- [EK_CRAWLER] = {15, 0.15f, true, 0.5f, 15, 0.8f, 0.0078f, -0.3906f, 1.4219f / 2, 0.9062f / 2, 0.3f, false,
- 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},
+ [EK_CRAWLER] = {CLIP_CRAWLER_DEATH_AIR, CLIP_CRAWLER_DEATH_LAND, -1, -1, -1},
+ [EK_BUZZER] = {CLIP_BUZZER_DEATH_AIR, CLIP_BUZZER_DEATH_LAND, -1, -1, -1},
+ [EK_SHADE] = {-1, -1, -1, -1, -1},
+ [EK_HUSK] = {CLIP_HUSK_DEATH_AIR, CLIP_HUSK_DEATH_LAND, CLIP_HUSK_IDLE, CLIP_HUSK_TURN, CLIP_HUSK_WALK},
};
/* ---------------------------------------------------------------- an enemy */
@@ -45,18 +43,23 @@ typedef struct {
int16_t hp;
float evasion;
/* Recoil */
- uint8_t rc_state;
+ uint8_t rc_state, rc_flags;
int8_t rc_dir;
bool rc_sweep;
- float rc_time, rc_speed;
+ float rc_time, rc_speed, rc_base, rc_dur;
+ /* Walker */
+ uint8_t wk_state, wk_stop;
+ int8_t wk_facing, wk_turning;
+ float wk_walk, wk_pause, wk_turn_cd;
+ uint16_t wk_rec; /* its record (0: none) */
/* SpriteFlash (flashInfected) */
float flash_t;
bool flashing;
/* the FSM's state */
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;
+ /* alert range (local circle, or box: ar_hy >= 0) and sight */
+ float ar_x, ar_y, ar_r, ar_hy;
bool in_alert, can_see;
float cbox_ox, cbox_oy, cbox_hx, cbox_hy; /* its collider */
} Enemy;
@@ -74,6 +77,15 @@ static const Ent *ent_at(int i) {
return &room_ents(&n)[i];
}
+/* an enemy's record of a tag (ET_*), or NULL */
+static const Ent *enemy_rec(const Enemy *e, int tag) {
+ if (e->ent == NO_ENT) return NULL;
+ const Ent *d = ent_at(e->ent);
+ for (int i = 1; i <= d->s0; i++)
+ if (d[i].type == ENT_BOX && d[i].flags == tag) return &d[i];
+ return NULL;
+}
+
/* the collider's world box */
static void enemy_box(const Enemy *e, float *x0, float *y0, float *x1, float *y1) {
float cx = e->body.x + e->body.ox, cy = e->body.y + e->body.oy;
@@ -178,13 +190,14 @@ static uint8_t flash_tint(const Enemy *e, int slot) {
/* ---------------------------------------------------------------- Recoil */
static void recoil_by_direction(Enemy *e, int dir, float magnitude) {
- const Kind *k = &kinds[e->kind];
if (e->rc_state != RC_READY) return;
+ if (dir == 1 && (e->rc_flags & 4)) return; /* (preventRecoilUp) */
e->rc_state = RC_RECOILING;
- e->rc_speed = k->recoil_speed * magnitude;
+ e->rc_speed = e->rc_base * magnitude;
e->rc_dir = (int8_t)dir;
e->rc_sweep = true;
- e->rc_time = k->recoil_time;
+ e->rc_time = e->rc_dur;
+ if (dir == 0 || dir == 2) e->flags |= 2; /* (RECOIL HORIZONTAL to its FSM) */
}
/* Sweep.Check: three rays from the collider's side, the distance clipped by the terrain */
@@ -210,12 +223,17 @@ static void recoil_fixed(Enemy *e) {
/* ---------------------------------------------------------------- death */
static void corpse_start(Enemy *e, float direction, bool has_direction) {
const Kind *k = &kinds[e->kind];
+ const Ent *c = enemy_rec(e, ET_CORPSE);
+ if (!c || k->clip_death_air < 0) {
+ e->mode = EM_OFF; /* (no corpse) */
+ return;
+ }
e->mode = EM_CORPSE, e->st = CS_AIR;
- e->body.y += k->corpse_spawn_y;
- e->body.ox = k->corpse_ox, e->body.oy = k->corpse_oy, e->body.hx = k->corpse_hx, e->body.hy = k->corpse_hy;
- e->body.gravity_scale = k->corpse_gravity, e->body.friction = 0.2f, e->body.mask = CF_SOLID;
+ e->body.y += c->p3;
+ e->body.ox = c->x0, e->body.oy = c->y0, e->body.hx = c->x1, e->body.hy = c->y1;
+ e->body.gravity_scale = c->p0, e->body.friction = 0.2f, e->body.mask = CF_SOLID;
e->body.ncontacts = 0;
- float speed = k->corpse_fling, angle = 90, sign = e->sx < 0 ? -1.0f : 1.0f;
+ float speed = c->p2, angle = 90, sign = e->sx < 0 ? -1.0f : 1.0f;
switch (cardinal(direction)) {
case 0: angle = 60, e->sx = sign; break; /* (corpseFacesRight: no) */
case 2: angle = 120, e->sx = -sign; break;
@@ -255,7 +273,7 @@ static void enemy_hit(Enemy *e, float direction, int damage) {
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->kind == EK_SHADE) && e->st == 0) e->b1 = true; /* (TOOK DAMAGE, in Idle) */
+ if ((e->kind == EK_BUZZER || e->kind == EK_SHADE || e->kind == EK_HUSK) && e->st == 0) e->b1 = true; /* (TOOK DAMAGE, in Idle / Ready) */
if (e->hp > 0)
e->evasion = 0.2f;
else
@@ -285,10 +303,15 @@ static void face_direction(Enemy *e, float *pause, int clip) {
static void sight_update(Enemy *e) {
float cx = g_hero.body.x + g_hero.body.ox, cy = g_hero.body.y + g_hero.body.oy;
float ax = e->body.x + e->ar_x, ay = e->body.y + e->ar_y;
- /* circle against the Knight's box */
- float qx = ax < cx - g_hero.body.hx ? cx - g_hero.body.hx : ax > cx + g_hero.body.hx ? cx + g_hero.body.hx : ax;
- float qy = ay < cy - g_hero.body.hy ? cy - g_hero.body.hy : ay > cy + g_hero.body.hy ? cy + g_hero.body.hy : ay;
- e->in_alert = !g_hero.hidden && e->ar_r > 0 && (qx - ax) * (qx - ax) + (qy - ay) * (qy - ay) < e->ar_r * e->ar_r;
+ if (e->ar_hy >= 0) {
+ /* a box against the Knight's */
+ e->in_alert = !g_hero.hidden && fabsf(cx - ax) < e->ar_r + g_hero.body.hx && fabsf(cy - ay) < e->ar_hy + g_hero.body.hy;
+ } else {
+ /* a circle against the Knight's box */
+ float qx = ax < cx - g_hero.body.hx ? cx - g_hero.body.hx : ax > cx + g_hero.body.hx ? cx + g_hero.body.hx : ax;
+ float qy = ay < cy - g_hero.body.hy ? cy - g_hero.body.hy : ay > cy + g_hero.body.hy ? cy + g_hero.body.hy : ay;
+ e->in_alert = !g_hero.hidden && e->ar_r > 0 && (qx - ax) * (qx - ax) + (qy - ay) * (qy - ay) < e->ar_r * e->ar_r;
+ }
if (!e->in_alert) {
e->can_see = false;
return;
@@ -447,6 +470,230 @@ static void buzzer_update(Enemy *e, const Ent *d) {
}
}
+/* ---------------------------------------------------------------- Walker */
+enum { WK_NOT_READY, WK_WAITING, WK_STOPPED, WK_WALKING, WK_TURNING };
+enum { STOP_BORED, STOP_CONTROLLED };
+
+static const Ent *walker_rec(const Enemy *e) {
+ int n;
+ return e->wk_rec ? &room_ents(&n)[e->wk_rec] : NULL;
+}
+
+/* (its transform's x scale: facing times rightScale) */
+static void walker_scale(Enemy *e, int facing) {
+ const Ent *w = walker_rec(e);
+ if (w->a & WF_NO_SCALE) return;
+ float rs = (w->a & WF_RIGHT_NEG) ? -1.0f : 1.0f;
+ set_scale_x(e, (float)facing * rs * fabsf(e->sx));
+}
+
+static void walker_begin_walking(Enemy *e, int facing) {
+ const Ent *w = walker_rec(e);
+ e->wk_state = WK_WALKING;
+ anim_play(&e->anim, kinds[e->kind].clip_walk);
+ walker_scale(e, facing);
+ e->wk_walk = rand_range(w->p0, w->p1);
+ e->body.vx = facing > 0 ? w->y0 : w->x0;
+}
+
+static void walker_begin_turning(Enemy *e, int facing) {
+ const Ent *w = walker_rec(e);
+ e->wk_state = WK_TURNING;
+ e->wk_turning = (int8_t)facing;
+ if (w->a & WF_PREVENT_TURN) {
+ e->wk_facing = e->wk_turning;
+ walker_begin_walking(e, e->wk_facing);
+ return;
+ }
+ e->wk_turn_cd = w->p2;
+ e->body.vx = 0;
+ anim_play_from_frame(&e->anim, kinds[e->kind].clip_turn, 0);
+}
+
+static void walker_walk_or_turn(Enemy *e, int facing) {
+ if (e->wk_facing == facing) walker_begin_walking(e, facing);
+ else walker_begin_turning(e, facing);
+}
+
+static void walker_end_stopping(Enemy *e) {
+ const Ent *w = walker_rec(e);
+ if (e->wk_facing == 0) walker_walk_or_turn(e, rand_range(0, 2) < 1 ? 1 : -1);
+ else if (rand_range(0, 100) < w->group) walker_begin_turning(e, -e->wk_facing);
+ else walker_begin_walking(e, e->wk_facing);
+}
+
+static void walker_stop(Enemy *e, int reason) {
+ const Ent *w = walker_rec(e);
+ e->wk_state = WK_STOPPED, e->wk_stop = (uint8_t)reason;
+ if (reason == STOP_BORED) {
+ anim_play(&e->anim, kinds[e->kind].clip_idle);
+ e->body.vx = 0;
+ if (w->a & WF_PAUSES) e->wk_pause = rand_range(w->x1, w->y1);
+ else walker_end_stopping(e);
+ }
+}
+
+static void walker_update(Enemy *e);
+/* StartMoving */
+static void walker_start(Enemy *e) {
+ if (e->wk_state == WK_STOPPED || e->wk_state == WK_WAITING) {
+ int facing = e->wk_facing ? e->wk_facing : (rand_range(0, 2) < 1 ? 1 : -1);
+ walker_walk_or_turn(e, facing);
+ }
+ walker_update(e);
+}
+
+/* (Start: its facing from its scale, then waiting for its conditions) */
+static void walker_init(Enemy *e) {
+ const Ent *w = walker_rec(e);
+ float rs = (w->a & WF_RIGHT_NEG) ? -1.0f : 1.0f;
+ e->wk_facing = e->sx * rs >= 0 ? 1 : -1;
+ e->wk_turn_cd = -1e-6f;
+ e->wk_state = WK_WAITING;
+}
+
+/* Sweep.Check: rays from a side of the collider, from (x, y), along a cardinal direction: is there terrain within? */
+static bool sweep(const Enemy *e, float x, float y, int dir, float dist) {
+ float dx = (float)(dir == 0) - (float)(dir == 2), dy = (float)(dir == 1) - (float)(dir == 3);
+ float bx = e->body.ox + e->body.hx * dx, by = e->body.oy + e->body.hy * dy;
+ for (int i = 0; i < 3; i++) {
+ float f = (float)i - 1;
+ float rx = x + bx + e->body.hx * fabsf(dy) * f - dx * 0.1f, ry = y + by + e->body.hy * fabsf(dx) * f - dy * 0.1f;
+ PhysHit hit;
+ if (phys_ray(rx, ry, dx, dy, dist + 0.1f, CF_TERRAIN, &hit) && hit.dist - 0.1f < dist) return true;
+ }
+ return false;
+}
+
+static void walker_update(Enemy *e) {
+ const Ent *w = walker_rec(e);
+ if (!w) return;
+ e->wk_turn_cd -= DT;
+ switch (e->wk_state) {
+ case WK_WAITING: {
+ /* (near the camera; at the hero's x if it waits for that; not an ambush nor inactive) */
+ bool hero_x_ok = !(w->a & WF_WAIT_HERO_X) || fabsf(hero_x() - w->s1 / 16.0f) < 1;
+ if (hero_x_ok && !(w->a & (WF_START_INACTIVE | WF_AMBUSH))) {
+ walker_stop(e, STOP_BORED);
+ walker_start(e);
+ }
+ break;
+ }
+ case WK_STOPPED:
+ if (e->wk_stop == STOP_BORED && (e->wk_pause -= DT) <= 0) walker_end_stopping(e);
+ break;
+ case WK_WALKING:
+ if (e->wk_turn_cd <= 0) {
+ float ext = e->body.hx;
+ if (sweep(e, e->body.x, e->body.y, 1 - e->wk_facing, ext + 0.5f)) {
+ walker_begin_turning(e, -e->wk_facing);
+ return;
+ }
+ if (!(w->a & WF_NO_TURN_TO_HERO) && (hero_x() > e->body.x) != (e->wk_facing > 0) && e->can_see && e->in_alert) {
+ walker_begin_turning(e, -e->wk_facing);
+ return;
+ }
+ if (!(w->a & WF_IGNORE_HOLES) &&
+ !sweep(e, e->body.x + (ext + 0.5f + w->p3) * e->wk_facing, e->body.y, 3, 0.25f)) {
+ walker_begin_turning(e, -e->wk_facing);
+ return;
+ }
+ }
+ if ((w->a & WF_PAUSES) && (e->wk_walk -= DT) <= 0) {
+ walker_stop(e, STOP_BORED);
+ return;
+ }
+ e->body.vx = e->wk_facing > 0 ? w->y0 : w->x0;
+ break;
+ case WK_TURNING:
+ e->body.vx = 0;
+ if (!e->anim.playing) {
+ e->wk_facing = e->wk_turning;
+ walker_begin_walking(e, e->wk_facing);
+ }
+ break;
+ }
+}
+
+/* ---------------------------------------------------------------- Husks: the Zombie Swipe FSM, a Walker */
+enum { ZS_READY, ZS_ANTICIPATE, ZS_LUNGE, ZS_COOLDOWN, ZS_IDLE };
+#define ZS_LUNGE_SPEED 6.0f
+#define ZS_IDLE_TIME 0.25f
+
+static void husk_start(Enemy *e) {
+ walker_init(e);
+ e->st = ZS_READY;
+}
+
+static void husk_ready(Enemy *e) {
+ /* Reset: StartWalker; (Coward: no) Ready */
+ e->st = ZS_READY;
+ walker_start(e);
+}
+
+static void husk_attack(Enemy *e) {
+ /* Left or Right?: the walker stopped, facing the hero (Face Left / Face Right) */
+ walker_stop(e, STOP_CONTROLLED);
+ bool right = hero_x() > e->body.x;
+ e->ax = right ? ZS_LUNGE_SPEED : -ZS_LUNGE_SPEED;
+ set_scale_x(e, right ? -fabsf(e->sx) : fabsf(e->sx));
+ e->wk_facing = right ? 1 : -1; /* (SetWalkerFacing: ChangeFacing) */
+ /* Anticipate */
+ e->body.vx = e->body.vy = 0;
+ anim_play_from_frame(&e->anim, CLIP_HUSK_ATTACK_ANTICIPATE, 0);
+ e->st = ZS_ANTICIPATE;
+}
+
+static void husk_update(Enemy *e) {
+ bool done = (e->anim.events & ANIM_DONE) != 0;
+ if (e->flags & 2) { /* (RECOIL HORIZONTAL: Reset) */
+ e->flags &= (uint8_t)~2;
+ if (e->st != ZS_READY) {
+ husk_ready(e);
+ return;
+ }
+ }
+ switch (e->st) {
+ case ZS_READY:
+ walker_update(e);
+ if ((e->can_see && e->in_alert) || e->b1) {
+ e->b1 = false;
+ husk_attack(e);
+ }
+ break;
+ case ZS_ANTICIPATE:
+ if (done) {
+ e->st = ZS_LUNGE;
+ anim_play_from_frame(&e->anim, CLIP_HUSK_ATTACK_LUNGE, 0);
+ }
+ break;
+ case ZS_LUNGE:
+ if (done) {
+ e->st = ZS_COOLDOWN;
+ anim_play_from_frame(&e->anim, CLIP_HUSK_ATTACK_COOLDOWN, 0);
+ e->body.vx = 0;
+ }
+ break;
+ case ZS_COOLDOWN:
+ if (done) {
+ e->st = ZS_IDLE, e->t0 = 0;
+ anim_play(&e->anim, CLIP_HUSK_IDLE);
+ }
+ break;
+ case ZS_IDLE:
+ if ((e->t0 += DT) >= ZS_IDLE_TIME) husk_ready(e);
+ break;
+ }
+}
+
+static void husk_fixed(Enemy *e) {
+ if (e->st == ZS_LUNGE) e->body.vx = e->ax; /* (SetVelocity2d every frame) */
+ else if (e->st == ZS_READY && e->wk_state == WK_WALKING) {
+ const Ent *w = walker_rec(e);
+ if (w) e->body.vx = e->wk_facing > 0 ? w->y0 : w->x0;
+ }
+}
+
/* ---------------------------------------------------------------- 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,
@@ -545,6 +792,7 @@ static void shade_spawn(void) {
sh.on = true;
e->mode = EM_ALIVE, e->kind = EK_SHADE, e->ent = NO_ENT;
e->damage = 1, e->z = 0.006f;
+ e->rc_base = 15, e->rc_dur = 0.15f;
/* (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;
@@ -552,7 +800,7 @@ static void shade_spawn(void) {
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->ar_r = 7.27f, e->ar_hy = -1;
e->sx = 1;
e->start_x = b->x, e->start_y = b->y;
shade_face_hero(e, -1);
@@ -812,12 +1060,20 @@ void enemies_enter(void) {
e->hp = (int16_t)d->p0;
Body *b = &e->body;
b->x = d->x0, b->y = d->y0;
- const Ent *box = d + 1;
- b->ox = box->x0 * (e->sx < 0 ? -1 : 1), b->oy = box->y0, b->hx = box->x1, b->hy = box->y1;
- b->gravity_scale = kinds[e->kind].gravity, b->friction = kinds[e->kind].friction, b->mask = CF_SOLID;
- if (d->s0 > 1) e->ar_x = (d + 2)->x0 * (e->sx < 0 ? -1 : 1), e->ar_y = (d + 2)->y0, e->ar_r = (d + 2)->x1;
+ const Ent *box = enemy_rec(e, ET_COLLIDER);
+ if (box) b->ox = box->x0 * (e->sx < 0 ? -1 : 1), b->oy = box->y0, b->hx = box->x1, b->hy = box->y1, b->gravity_scale = box->p0;
+ b->friction = 0.2828f, b->mask = CF_SOLID; /* (the default material with the terrain's) */
+ /* its sight: the alert range, else its first other range */
+ const Ent *ar = enemy_rec(e, ET_ALERT);
+ if (!ar) ar = enemy_rec(e, ET_RANGE);
+ if (ar) e->ar_x = ar->x0 * (e->sx < 0 ? -1 : 1), e->ar_y = ar->y0, e->ar_r = ar->x1, e->ar_hy = ar->y1;
+ const Ent *rc = enemy_rec(e, ET_RECOIL);
+ e->rc_base = rc ? rc->x0 : 15, e->rc_dur = rc ? rc->y0 : 0.5f, e->rc_flags = rc ? (uint8_t)rc->a : 0;
+ const Ent *wk = enemy_rec(e, ET_WALKER);
+ e->wk_rec = wk ? (uint16_t)(wk - room_ents(&n)) : 0;
if (e->kind == EK_CRAWLER) crawler_start(e, d);
else if (e->kind == EK_BUZZER) buzzer_start(e, d);
+ else if (e->kind == EK_HUSK) husk_start(e);
}
memset(&sh, 0, sizeof sh);
memset(balls, 0, sizeof balls);
@@ -838,11 +1094,15 @@ void enemies_fixed(void) {
if (e->mode == EM_ALIVE) {
if (e->kind == EK_CRAWLER) crawler_fixed(e);
else if (e->kind == EK_BUZZER) buzzer_fixed(e);
+ else if (e->kind == EK_HUSK) husk_fixed(e);
recoil_fixed(e);
body_step(&e->body, DT);
} else {
/* Corpse: falls, lands (or smashes, a breaker) and stays */
const Kind *k = &kinds[e->kind];
+ const Ent *cr = enemy_rec(e, ET_CORPSE);
+ bool breaker = cr && (cr->a & CF_BREAKER);
+ float cbounce = cr ? cr->p1 : -1;
float pvx = e->body.vx, pvy = e->body.vy;
int had = e->body.ncontacts;
if (e->st != CS_LANDED || e->body.vx || e->body.vy) body_step(&e->body, DT);
@@ -850,17 +1110,17 @@ void enemies_fixed(void) {
bool ground = false;
for (int c = 0; c < e->body.ncontacts; c++) ground |= e->body.cny[c] > 0.5f;
if (ground) {
- if (k->breaker) {
+ if (breaker) {
anim_play(&e->anim, k->clip_death_land);
e->body.vx = e->body.vy = 0;
e->st = CS_DEATH_ANIM;
} else {
- bounce(&e->body, pvx, pvy, had, k->corpse_bounce);
+ bounce(&e->body, pvx, pvy, had, cbounce);
anim_play(&e->anim, k->clip_death_land);
e->st = CS_LANDED;
}
} else
- bounce(&e->body, pvx, pvy, had, k->corpse_bounce);
+ bounce(&e->body, pvx, pvy, had, cbounce);
if (e->body.y < -10) e->mode = EM_OFF;
}
}
@@ -882,6 +1142,7 @@ void enemies_update(void) {
if (e->ar_r > 0) sight_update(e);
if (e->kind == EK_BUZZER) buzzer_update(e, ent_at(e->ent));
else if (e->kind == EK_SHADE) shade_update(e);
+ else if (e->kind == EK_HUSK) husk_update(e);
} else if (e->kind == EK_SHADE)
shade_update(e);
else if (e->st == CS_DEATH_ANIM && !e->anim.playing)
diff --git a/games/hollowknight/tools/actors.py b/games/hollowknight/tools/actors.py
index 7b0148c6..417f3990 100644
--- a/games/hollowknight/tools/actors.py
+++ b/games/hollowknight/tools/actors.py
@@ -24,6 +24,7 @@ KNIGHT = ("resources.assets", 20600, [
ACTORS = {"knight": KNIGHT,
"crawler": ("sharedassets6.assets", 1113, None),
"buzzer": ("sharedassets6.assets", 1150, None),
+ "husk": ("sharedassets37.assets", 149, None),
"georock": ("sharedassets6.assets", 1149, None),
"chest": ("sharedassets6.assets", 1148, None),
# the HUD: drawn by its own camera, 11.48 pixels a unit (scenes: K0), masks at 0.7135
diff --git a/games/hollowknight/tools/ents.py b/games/hollowknight/tools/ents.py
index 312fed0e..59f93cd1 100644
--- a/games/hollowknight/tools/ents.py
+++ b/games/hollowknight/tools/ents.py
@@ -10,8 +10,10 @@ ENT_CAMLOCK, ENT_GATE, ENT_HAZARD_MARKER, ENT_RESPAWN, ENT_HAZARD_TRIGGER, ENT_M
# objects (ENT_OBJ's flags: src/obj.c)
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}
+EK_CRAWLER, EK_BUZZER, EK_SHADE, EK_HUSK = 1, 2, 3, 4
+ENEMIES = {("Crawler", 1113): EK_CRAWLER, ("chaser", 1150): EK_BUZZER, ("Zombie Swipe", 149): EK_HUSK}
+# what follows an enemy's record: ENT_BOX records, tagged
+ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE = 0, 1, 2, 3, 4, 5
EF_START, EF_STARTLES = 1, 2 # (FSM bools: First Crawler or Start Alert; Startles)
# the layers the nail's slashes touch (Physics2D's collision matrix, layer 17 Attack)
ATTACK_HITS = {3, 6, 7, 8, 11, 12, 17, 19, 20, 21, 25, 31}
@@ -287,19 +289,42 @@ def _pieces(d, o, ids, by_id, sprites):
return out
-def _enemy(o, by_id, persist, name):
+def _ref_file(d, ref):
+ """A scene's (or prefab's) reference -> (asset file, path id)."""
+ fid, pid = ref
+ return (d["level"] if fid == 0 else d["externals"][fid - 1].split("/")[-1], pid)
+
+
+def _alert_range(q):
+ """An alert range's trigger (circle or box) -> (cx, cy, r or hx, hy or -1) in its owner's units, or None."""
+ k = (abs(q["lscale"][0]), abs(q["lscale"][1]))
+ for c in q["c"]:
+ v = c.get("v")
+ if not v or not v.get("m_Enabled", 1):
+ continue
+ if c["type"] == "CircleCollider2D":
+ r = v["m_Radius"] * max(k)
+ return (q["lpos"][0] + v["m_Offset"]["x"] * k[0], q["lpos"][1] + v["m_Offset"]["y"] * k[1], r, -1)
+ if c["type"] == "BoxCollider2D":
+ return (q["lpos"][0] + v["m_Offset"]["x"] * k[0], q["lpos"][1] + v["m_Offset"]["y"] * k[1],
+ v["m_Size"]["x"] / 2 * k[0], v["m_Size"]["y"] / 2 * k[1])
+ return None
+
+
+def _enemy(o, by_id, persist, name, d=None, strings=None):
"""An enemy -> its records: ENT_OBJ (OK_ENEMY: its kind, place (x, y, z, x scale), hp, damage, small and medium
- geo; large geo in group), then ENT_BOX its collider (local offset and half size, scaled) and ENT_BOX its alert
- range (a circle: local center and radius), or None."""
+ geo; large geo in group; s0 how many records follow), then ENT_BOX records each tagged (flags, ET_*): its
+ collider, alert ranges, Walker, Recoil, corpse; or None."""
an = next((c.get("v") for c in o["c"] if c.get("class") == "tk2dSpriteAnimator" and c.get("v")), None)
hm = next((c.get("v") for c in o["c"] if c.get("class") == "HealthManager" and c.get("v")), None)
if not an or not hm:
return None
- kind = None
+ kind, fsm = None, {}
for c in o["c"]:
f = c.get("fsm")
- if f and (f["name"], an["library"][1]) in ENEMIES:
- kind, fsm = ENEMIES[(f["name"], an["library"][1])], f
+ key = (f["name"] if f else c.get("class"), an["library"][1])
+ if key in ENEMIES:
+ kind, fsm = ENEMIES[key], f or {}
if kind is None:
return None
dh = next((c.get("v") or {} for c in o["c"] if c.get("class") == "DamageHero"), {})
@@ -313,16 +338,61 @@ def _enemy(o, by_id, persist, name):
fl |= EF_START
if (var.get("Startles") or [0, False])[1]:
fl |= EF_STARTLES
- out = [rec(ENT_BOX, box=(box["m_Offset"]["x"] * abs(sx), box["m_Offset"]["y"] * abs(sy),
- box["m_Size"]["x"] / 2 * abs(sx), box["m_Size"]["y"] / 2 * abs(sy)))]
+ rb = next((c["v"] for c in o["c"] if c["type"] == "Rigidbody2D" and c.get("v")), {})
+ out = [rec(ENT_BOX, ET_COLLIDER, box=(box["m_Offset"]["x"] * abs(sx), box["m_Offset"]["y"] * abs(sy),
+ box["m_Size"]["x"] / 2 * abs(sx), box["m_Size"]["y"] / 2 * abs(sy)),
+ p=(rb.get("m_GravityScale", 1), 0, 0, 0))]
+ # its alert ranges (children with AlertRange): "Alert Range New" the main one, others by name
for ch in o.get("children", []):
q = by_id[ch]
+ if not any(c.get("class") == "AlertRange" for c in q["c"]) and q["name"] != "Alert Range New":
+ continue
+ r = _alert_range(q)
+ if r is None:
+ continue
if q["name"] == "Alert Range New":
- cc = next((c["v"] for c in q["c"] if c["type"] == "CircleCollider2D" and c.get("v")), None)
- if cc:
- k = max(abs(q["lscale"][0]), abs(q["lscale"][1]))
- out.append(rec(ENT_BOX, box=(q["lpos"][0] + cc["m_Offset"]["x"] * k, q["lpos"][1] + cc["m_Offset"]["y"] * k,
- cc["m_Radius"] * k, 0)))
+ out.append(rec(ENT_BOX, ET_ALERT, box=r))
+ else:
+ out.append(rec(ENT_BOX, ET_RANGE, box=r, s0=strings.id(q["name"]) if strings else 0))
+ w = next((c.get("v") for c in o["c"] if c.get("class") == "Walker" and c.get("v")), None)
+ if w:
+ wf = (1 if w.get("pauses") else 0) | (2 if w.get("ignoreHoles") else 0) | \
+ (4 if w.get("preventTurningToFaceHero") else 0) | (8 if w.get("startInactive") else 0) | \
+ (16 if w.get("ambush") else 0) | (32 if w.get("waitForHeroX") else 0) | (64 if w.get("preventTurn") else 0) | \
+ (128 if w.get("preventScaleChange") else 0) | (256 if w.get("rightScale", 1) < 0 else 0)
+ out.append(rec(ENT_BOX, ET_WALKER, box=(w.get("walkSpeedL", 0), w.get("walkSpeedR", 0), w.get("pauseTimeMin", 0),
+ w.get("pauseTimeMax", 0)),
+ p=(w.get("pauseWaitMin", 0), w.get("pauseWaitMax", 0), w.get("turnPause", 0), w.get("edgeXAdjuster", 0)),
+ group=w.get("turnAfterIdlePercentage", 0), a=wf,
+ s1=max(0, min(65535, int(round(w.get("waitHeroX", 0) * 16))))))
+ rc = next((c.get("v") for c in o["c"] if c.get("class") == "Recoil" and c.get("v")), None)
+ if rc:
+ out.append(rec(ENT_BOX, ET_RECOIL, box=(rc.get("recoilSpeedBase", 15), rc.get("recoilDuration", 0.15), 0, 0),
+ a=(1 if rc.get("stopVelocityXWhenRecoilingUp") else 0) | (2 if rc.get("freezeInPlace") else 0) |
+ (4 if rc.get("preventRecoilUp") else 0)))
+ de = next((c.get("v") for c in o["c"] if c.get("class") == "EnemyDeathEffects" and c.get("v")), None)
+ if de and de.get("corpsePrefab") and d is not None:
+ try:
+ import unity
+ cf, cp = _ref_file(d, de["corpsePrefab"])
+ cd = unity.prefab(cf, cp)
+ co = cd["objects"][0]
+ cb = next((c["v"] for c in co["c"] if c["type"] == "BoxCollider2D" and c.get("v")), None)
+ rb = next((c["v"] for c in co["c"] if c["type"] == "Rigidbody2D" and c.get("v")), {})
+ ob = next((c["v"] for c in co["c"] if c.get("class") == "ObjectBounce" and c.get("v")), None)
+ cc = next((c for c in co["c"] if (c.get("class") or "").startswith("Corpse")), None)
+ cv = (cc or {}).get("v") or {}
+ k = (abs(co["lscale"][0]), abs(co["lscale"][1]))
+ sp = de.get("corpseSpawnPoint") or {"y": 0}
+ cfl = (1 if cv.get("breaker") else 0) | (2 if de.get("corpseFacesRight") else 0) | \
+ (4 if de.get("lowCorpseArc") else 0) | (8 if cv.get("instantChunker") else 0) | (16 if cv.get("massless") else 0)
+ if cb:
+ out.append(rec(ENT_BOX, ET_CORPSE, box=(cb["m_Offset"]["x"] * k[0], cb["m_Offset"]["y"] * k[1],
+ cb["m_Size"]["x"] / 2 * k[0], cb["m_Size"]["y"] / 2 * k[1]),
+ p=(rb.get("m_GravityScale", 1), ob.get("bounceFactor", -1) if ob else -1,
+ de.get("corpseFlingSpeed", 15), sp.get("y", 0)), a=cfl))
+ except Exception as e:
+ print("ents: %s: corpse %s" % (o["path"], e))
persistent = any(c.get("class") == "PersistentBoolItem" for c in o["c"])
head = rec(ENT_OBJ, OK_ENEMY, (o["pos"][0], o["pos"][1], o["pos"][2], sx),
(hm.get("hp", 1), dh.get("damageDealt", 0), hm.get("smallGeoDrops", 0), hm.get("mediumGeoDrops", 0)),
@@ -467,7 +537,7 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None):
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)
+ en = _enemy(o, by_id, persist, name, d, strings) or _props(d, o, by_id, rooms, strings, persist, sprites, owners, name)
if en:
recs += en
br = next((c for c in o["c"] if c.get("class") == "Breakable" and c.get("v") is not None), None)