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: Leaping Husk, Husk Guard, shockwaves; shared palettes

Mason Chen31fc36f0d1be
diff --git a/games/hollowknight/src/camera.c b/games/hollowknight/src/camera.c
index b97c1d64..1419e8d1 100644
--- a/games/hollowknight/src/camera.c
+++ b/games/hollowknight/src/camera.c
@@ -69,17 +69,25 @@ static void keep_in_scene(float *x, float *y) {
static struct {
float ex, time, t, priority;
float dx, dy;
+ uint8_t rumble; /* (Rumbling*: a looping shake, whenever no other) */
} sh;
void cam_shake(int kind) {
- static const float shakes[][3] = {{0, 0, 0}, {0.105f, 0.5f, 6}, {0.15f, 1, 7}, {0.5f, 1, 10}}; /* extents, time, priority */
- if (kind <= 0 || kind > SHAKE_BIG || shakes[kind][2] <= sh.priority) return;
+ /* extents, time, priority: EnemyKillShake, AverageShake, BigShake, SmallShake */
+ static const float shakes[][3] = {{0, 0, 0}, {0.105f, 0.5f, 6}, {0.15f, 1, 7}, {0.5f, 1, 10}, {0.08f, 0.5f, 3}};
+ if (kind <= 0 || kind > SHAKE_SMALL || shakes[kind][2] <= sh.priority) return;
sh.ex = shakes[kind][0], sh.time = shakes[kind][1], sh.priority = shakes[kind][2], sh.t = 0;
}
+void cam_rumble(int kind) { sh.rumble = (uint8_t)kind; }
+
static void shake_tick(void) {
sh.dx = sh.dy = 0;
- if (sh.priority <= 0) return;
+ if (sh.priority <= 0) {
+ static const float rumbles[] = {0, 0.08f, 0.15f, 0.5f}; /* SmallShake, AverageShake, BigShake */
+ if (sh.rumble) sh.dx = rumbles[sh.rumble] * rand_range(-1, 1), sh.dy = rumbles[sh.rumble] * rand_range(-1, 1);
+ return;
+ }
sh.t += 0.02f * g_game.time_scale;
float k = 1 - sh.t / sh.time;
if (k < 0) k = 0;
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.bin
index 8f785226..ac3c8db8 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 50475b56..676e42e9 100644
--- a/games/hollowknight/src/data.h
+++ b/games/hollowknight/src/data.h
@@ -270,13 +270,38 @@
#define CLIP_HORNHEAD_IDLE 210
#define CLIP_HORNHEAD_TURN 211
#define CLIP_HORNHEAD_WALK 212
-#define CLIP_BULLET_IDLE 213
-#define CLIP_BULLET_IMPACT 214
-#define SPRITE_HERO_LIGHT 915
-#define SPRITE_DIALOGUE_BACKBOARD 916
-#define SPRITE_PROMPT_SHADOW 917
-#define SPRITE_CORPSE_NAIL 918
-#define SPRITE_DIGIT0 1099
+#define CLIP_LEAPER_ATTACK 213
+#define CLIP_LEAPER_DEATH_AIR 214
+#define CLIP_LEAPER_DEATH_LAND 215
+#define CLIP_LEAPER_IDLE 216
+#define CLIP_LEAPER_LAND 217
+#define CLIP_LEAPER_TURN 218
+#define CLIP_LEAPER_WALK 219
+#define CLIP_GUARD_ANTICIPATE 220
+#define CLIP_GUARD_ATTACK2 221
+#define CLIP_GUARD_DEATH_AIR 222
+#define CLIP_GUARD_DEATH_LAND 223
+#define CLIP_GUARD_DORMANT 224
+#define CLIP_GUARD_IDLE 225
+#define CLIP_GUARD_RUN 226
+#define CLIP_GUARD_STARTLE 227
+#define CLIP_GUARD_STOMP_ANTIC 228
+#define CLIP_GUARD_STOMP_JUMP 229
+#define CLIP_GUARD_STOMP_LAND 230
+#define CLIP_GUARD_STOP_RUN 231
+#define CLIP_GUARD_STOP_WALK 232
+#define CLIP_GUARD_SWIPE 233
+#define CLIP_GUARD_TURN 234
+#define CLIP_GUARD_WAKE 235
+#define CLIP_GUARD_WALK 236
+#define CLIP_BULLET_IDLE 237
+#define CLIP_BULLET_IMPACT 238
+#define CLIP_BULLET_SHOCKWAVE_SPURT 239
+#define SPRITE_HERO_LIGHT 1034
+#define SPRITE_DIALOGUE_BACKBOARD 1035
+#define SPRITE_PROMPT_SHADOW 1036
+#define SPRITE_CORPSE_NAIL 1037
+#define SPRITE_DIGIT0 1218
#define STYLE_DIALOGUE 0
#define STYLE_PROMPT 1
#define TEXT_K 1.333333f
@@ -292,8 +317,10 @@
#define EK_HUSK 9
#define EK_BARGER 10
#define EK_HORNHEAD 11
-#define NUM_KINDS 12
-#define KIND_TABLE {{0}, {EF_CRAWLER, {-1, 150, 149, -1, -1, -1, -1, -1, -1, -1, -1, 152, 153}}, {EF_BUZZER, {155, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 157, 158}}, {EF_SHADE, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}, {EF_CLIMBER, {-1, -1, 165, 164, -1, -1, -1, -1, -1, -1, -1, 162, 163}}, {EF_BOUNCER, {-1, -1, -1, 168, -1, -1, -1, -1, -1, -1, -1, 166, 167}}, {EF_SPITTER, {-1, -1, -1, 171, 172, 170, -1, -1, -1, -1, -1, 169, -1}}, {EF_ROLLER, {175, -1, -1, 177, 176, 178, -1, -1, -1, -1, -1, 173, 174}}, {EF_BLOCKER, {185, -1, -1, 181, 186, 179, 180, 187, 188, 184, 183, -1, 182}}, {EF_HUSK, {194, 195, 196, 189, 191, 190, -1, -1, -1, -1, -1, 192, 193}}, {EF_HUSK, {202, 203, 204, 197, 199, 198, -1, -1, -1, -1, -1, 200, 201}}, {EF_HUSK, {210, 211, 212, 205, 207, 206, -1, -1, -1, -1, -1, 208, 209}}}
+#define EK_LEAPER 12
+#define EK_GUARD 13
+#define NUM_KINDS 14
+#define KIND_TABLE {{0}, {EF_CRAWLER, {-1, 150, 149, -1, -1, -1, -1, -1, -1, -1, -1, 152, 153}}, {EF_BUZZER, {155, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 157, 158}}, {EF_SHADE, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}, {EF_CLIMBER, {-1, -1, 165, 164, -1, -1, -1, -1, -1, -1, -1, 162, 163}}, {EF_BOUNCER, {-1, -1, -1, 168, -1, -1, -1, -1, -1, -1, -1, 166, 167}}, {EF_SPITTER, {-1, -1, -1, 171, 172, 170, -1, -1, -1, -1, -1, 169, -1}}, {EF_ROLLER, {175, -1, -1, 177, 176, 178, -1, -1, -1, -1, -1, 173, 174}}, {EF_BLOCKER, {185, -1, -1, 181, 186, 179, 180, 187, 188, 184, 183, -1, 182}}, {EF_HUSK, {194, 195, 196, 189, 191, 190, -1, -1, -1, -1, -1, 192, 193}}, {EF_HUSK, {202, 203, 204, 197, 199, 198, -1, -1, -1, -1, -1, 200, 201}}, {EF_HUSK, {210, 211, 212, 205, 207, 206, -1, -1, -1, -1, -1, 208, 209}}, {EF_LEAPER, {216, 218, 219, 213, 217, -1, -1, -1, -1, -1, -1, 214, 215}}, {EF_GUARD, {225, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 222, 223}}}
#define TXT_PROMPT_LISTEN 0
#define TXT_PROMPT_REST 1
#define TXT_PROMPT_INSPECT 2
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index bc1f0988..92151bcf 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -14,7 +14,7 @@
/* ---------------------------------------------------------------- what each kind is */
/* (data.h: EK_* each kind, KIND_TABLE: the FSM each runs and the clips its roles play) */
-enum { EF_CRAWLER = 1, EF_BUZZER, EF_SHADE, EF_HUSK, EF_CLIMBER, EF_BOUNCER, EF_SPITTER, EF_ROLLER, EF_BLOCKER };
+enum { EF_CRAWLER = 1, EF_BUZZER, EF_SHADE, EF_HUSK, EF_CLIMBER, EF_BOUNCER, EF_SPITTER, EF_ROLLER, EF_BLOCKER, EF_LEAPER, EF_GUARD };
enum { R_IDLE, R_TURN, R_WALK, R_A1, R_A2, R_A3, R_A4, R_A5, R_A6, R_A7, R_A8, R_DEATH_AIR, R_DEATH_LAND, NUM_ROLES };
typedef struct {
uint8_t fsm;
@@ -25,7 +25,7 @@ static const Kind kinds[NUM_KINDS] = KIND_TABLE;
#define FSM(e) (kinds[(e)->kind].fsm)
#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, ET_VARS, ET_TERRAIN };
+enum { ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE, ET_VARS, ET_TERRAIN, ET_HITBOX };
enum { CF_BREAKER = 1, CF_FACES_RIGHT = 2, CF_LOW_ARC = 4, CF_NO_COLLIDER = 32 }; /* 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) */
@@ -34,6 +34,7 @@ enum { WF_PAUSES = 1, WF_IGNORE_HOLES = 2, WF_NO_TURN_TO_HERO = 4, WF_START_INAC
enum { EM_OFF, EM_ALIVE, EM_CORPSE };
enum { RC_READY, RC_RECOILING, RC_FROZEN };
enum { CS_AIR, CS_DEATH_ANIM, CS_LANDED };
+/* (flags: 1 collider off, 2 RECOIL HORIZONTAL, 4 invincible, 8 TOOK DAMAGE: for its FSM this frame) */
typedef struct {
uint8_t mode, kind, st, flags;
uint16_t ent; /* its record (NO_ENT: spawned) */
@@ -62,6 +63,9 @@ typedef struct {
float t0, t1, wait, start_x, start_y, ax, ay, pause0, pause1, jx, jy;
float ang, qx, qy, tx, ty; /* (its rotation, degrees; a tween's from and to) */
bool b0, b1;
+ uint8_t c0, c1; /* (counters) */
+ uint8_t hb_on, sub_hb; /* its attacks on (ET_HITBOX's bits); the one whose sprite plays (k + 1) */
+ Anim sub; /* (that sprite's) */
/* 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;
@@ -310,6 +314,7 @@ static void enemy_hit(Enemy *e, float direction, int damage) {
recoil_by_direction(e, dir, 1);
if (FSM(e) != EF_SHADE) hero_soul_gain(); /* (enemyType 3, a shade: no soul) */
e->flashing = true, e->flash_t = 0;
+ e->flags |= 8;
e->hp = (int16_t)(e->hp - damage < -50 ? -50 : e->hp - damage);
if ((FSM(e) == EF_BUZZER || FSM(e) == EF_SHADE || FSM(e) == EF_HUSK) && e->st == 0) e->b1 = true; /* (TOOK DAMAGE, in Idle / Ready) */
if (FSM(e) == EF_BLOCKER && e->hp > 0) blocker_hit(e);
@@ -1421,6 +1426,455 @@ static void husk_fixed(Enemy *e) {
}
}
+/* ---------------------------------------------------------------- attacks: children that hurt the Knight */
+/* its hitbox k (ET_HITBOX's, in order), or NULL */
+static const Ent *hitbox_rec(const Enemy *e, int k) {
+ 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 == ET_HITBOX && k-- == 0) return &d[i];
+ return NULL;
+}
+
+/* (the child on: its collider, and the sprite it plays till its clip ends: DeactivateAfter2dtkAnimation) */
+static void hitbox_on(Enemy *e, int k, int clip) {
+ e->hb_on |= (uint8_t)(1 << k);
+ if (clip >= 0) anim_play_from_frame(&e->sub, clip, 0), e->sub_hb = (uint8_t)(k + 1);
+}
+
+static void hitbox_tick(Enemy *e) {
+ if (!e->sub_hb) return;
+ e->sub.events = 0;
+ anim_update(&e->sub, DT);
+ if ((e->sub.events & ANIM_DONE) || !e->sub.playing) e->hb_on &= (uint8_t)~(1 << (e->sub_hb - 1)), e->sub_hb = 0;
+}
+
+/* the Knight's box and its hitboxes on: the damage of the first it meets */
+static int hitbox_touch(const Enemy *e, float x0, float y0, float x1, float y1) {
+ float k = e->sx < 0 ? -1.0f : 1.0f;
+ for (int i = 0; i < 8 && e->hb_on >> i; i++) {
+ if (!(e->hb_on >> i & 1)) continue;
+ const Ent *h = hitbox_rec(e, i);
+ if (!h) continue;
+ float a0 = e->body.x + (k > 0 ? h->x0 : -h->x1), a1 = e->body.x + (k > 0 ? h->x1 : -h->x0);
+ if (!(x1 > a0 && x0 < a1 && y1 > e->body.y + h->y0 && y0 < e->body.y + h->y1)) continue;
+ float pts[16];
+ int np = (int)h->p0;
+ for (int j = 0; j < np && j < 8; j++) {
+ const float *f = &h[1 + j / 4].x0;
+ pts[2 * j] = e->body.x + f[2 * (j & 3)] * k, pts[2 * j + 1] = e->body.y + f[2 * (j & 3) + 1];
+ }
+ if (box_meets_shape(x0, y0, x1, y1, pts, np < 8 ? np : 8)) return (int)h->p1;
+ }
+ return 0;
+}
+
+/* ---------------------------------------------------------------- shockwaves (the shockwave FSM, its spurts) */
+#define MAX_WAVES 4
+#define MAX_SPURTS 32
+typedef struct {
+ bool on, spurting;
+ float x, y, speed, inc, scale, t;
+ int8_t dir;
+} Wave;
+typedef struct {
+ bool on;
+ float x, y, scale, t;
+ int8_t dir;
+ Anim anim;
+} Spurt;
+static Wave waves[MAX_WAVES];
+static Spurt spurts[MAX_SPURTS];
+
+/* a wave from (x, y) along the ground, its speed and scale (Start, Right / Left, Start Move) */
+static void wave_spawn(float x, float y, bool right, float speed, float scale) {
+ Wave *w = NULL;
+ for (int i = 0; i < MAX_WAVES && !w; i++)
+ if (!waves[i].on) w = &waves[i];
+ if (!w) return;
+ memset(w, 0, sizeof *w);
+ w->on = w->spurting = true;
+ w->x = x, w->y = y, w->scale = scale, w->dir = right ? 1 : -1;
+ w->inc = speed * 2 * w->dir, w->speed = speed * 0.025f * w->dir;
+}
+
+static void spurt_spawn(const Wave *w) {
+ Spurt *s = NULL;
+ for (int i = 0; i < MAX_SPURTS && !s; i++)
+ if (!spurts[i].on) s = &spurts[i];
+ if (!s) return;
+ s->on = true, s->x = w->x, s->y = w->y, s->scale = w->scale, s->t = 0, s->dir = w->dir;
+ anim_play_from_frame(&s->anim, CLIP_BULLET_SHOCKWAVE_SPURT, 0);
+}
+
+static void waves_tick(void) {
+ for (int i = 0; i < MAX_WAVES; i++) {
+ Wave *w = &waves[i];
+ if (!w->on) continue;
+ w->t += DT;
+ if (w->spurting) {
+ /* Move: faster and faster; into a wall (its trigger meeting the terrain), or past the ground's end: done. (Its
+ * trigger: scaled by the wave's x scale, which Left sets to -1; the ground ray from a unit ahead, unscaled) */
+ w->speed += w->inc * DT;
+ float dx = w->speed * DT, kx = w->dir > 0 ? w->scale : 1;
+ float front = w->x + (-0.2374f * w->dir + 0.32855f * w->dir) * kx;
+ if (w->t >= 0)
+ for (int k = 0; k < 3; k++)
+ if (phys_ray(front, w->y + 0.4f + 0.65f * (float)k, w->dir, 0, fabsf(dx), CF_TERRAIN, NULL)) {
+ w->t = -0.15f; /* (End Pause: spurts a moment more) */
+ break;
+ }
+ w->x += dx;
+ if (w->t >= 0 && !phys_ray(w->x + w->dir, w->y + 1, 0, -1, 1.6f, CF_TERRAIN, NULL))
+ w->spurting = false, w->t = 0; /* (HIT: End Particle) */
+ else if (w->t < 0 && w->t + DT >= 0)
+ w->spurting = false, w->t = 0;
+ if (w->spurting) spurt_spawn(w);
+ } else {
+ w->x += w->speed * DT;
+ if (w->t >= 1) w->on = false; /* (End Particle: a second, then recycled) */
+ }
+ }
+ for (int i = 0; i < MAX_SPURTS; i++) {
+ Spurt *s = &spurts[i];
+ if (!s->on) continue;
+ s->t += DT;
+ s->anim.events = 0;
+ anim_update(&s->anim, DT);
+ if ((s->anim.events & ANIM_DONE) || !s->anim.playing) s->on = false; /* (RecycleAfter2dtkAnimation) */
+ }
+}
+
+/* (Damage timing: a spurt hurts from 0.05 s to 0.1 s) */
+static int spurts_touch(float x0, float y0, float x1, float y1, int *side) {
+ for (int i = 0; i < MAX_SPURTS; i++) {
+ const Spurt *s = &spurts[i];
+ if (!s->on || s->t < 0.05f || s->t >= 0.1f + DT / 2) continue;
+ float cx = s->x - 0.0473f * s->dir * s->scale, cy = s->y + 0.87f * s->scale;
+ float hx = 0.2022f * s->scale, hy = 0.8718f * s->scale;
+ if (x1 > cx - hx && x0 < cx + hx && y1 > cy - hy && y0 < cy + hy) {
+ *side = s->x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static void spurts_draw(void) {
+ for (int i = 0; i < MAX_SPURTS; i++) {
+ const Spurt *s = &spurts[i];
+ if (!s->on || s->anim.sprite < 0) continue;
+ Inst in;
+ sprite_inst(s->anim.sprite, s->x, s->y, -0.12f, s->scale * s->dir, s->scale, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
+}
+
+/* ---------------------------------------------------------------- Leaping Husks: the Zombie Leap FSM, a Walker */
+enum { ZL_READY, ZL_ANTICIPATE, ZL_LAUNCH, ZL_LUNGE, ZL_COOLDOWN, ZL_IDLE };
+
+static void leaper_start(Enemy *e) {
+ walker_init(e);
+ e->st = ZL_READY;
+}
+
+static void leaper_update(Enemy *e) {
+ bool done = (e->anim.events & ANIM_DONE) != 0;
+ switch (e->st) {
+ case ZL_READY:
+ walker_update(e);
+ if (e->can_see && e->in_alert) {
+ /* Left or Right?: the walker stopped, its leap aimed a little past the hero, facing it */
+ walker_stop(e, STOP_CONTROLLED);
+ bool right = hero_x() > e->body.x;
+ e->ax = (hero_x() - e->body.x) * 1.25f;
+ set_scale_x(e, right ? -1.0f : 1.0f);
+ e->wk_facing = right ? 1 : -1;
+ /* Anticipate */
+ e->body.vx = e->body.vy = 0;
+ anim_play_from_frame(&e->anim, CLIP(e, R_A1), 0);
+ e->st = ZL_ANTICIPATE;
+ }
+ break;
+ case ZL_ANTICIPATE:
+ if (e->anim.events & ANIM_TRIGGER) {
+ e->body.vx = e->ax, e->body.vy = 20; /* (Launch) */
+ e->st = ZL_LAUNCH;
+ }
+ break;
+ case ZL_LAUNCH:
+ e->st = ZL_LUNGE;
+ break;
+ case ZL_LUNGE:
+ if (e->body.ncontacts && side_hit(e, 3)) {
+ anim_play_from_frame(&e->anim, CLIP(e, R_A2), 0); /* (Cooldown: Land) */
+ e->body.vx = 0;
+ e->st = ZL_COOLDOWN;
+ }
+ break;
+ case ZL_COOLDOWN:
+ if (done) {
+ anim_play(&e->anim, CLIP(e, R_IDLE));
+ e->st = ZL_IDLE, e->t0 = 0;
+ }
+ break;
+ case ZL_IDLE: {
+ const Ent *v = enemy_rec(e, ET_VARS);
+ if ((e->t0 += DT) >= (v ? v->p0 : 0.5f)) {
+ /* Reset: StartWalker */
+ anim_play(&e->anim, CLIP(e, R_IDLE));
+ walker_start(e);
+ e->st = ZL_READY;
+ }
+ break;
+ }
+ }
+}
+
+static void leaper_fixed(Enemy *e) {
+ if (e->st == ZL_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;
+ }
+}
+
+/* ---------------------------------------------------------------- Husk Guards: the Zombie Guard FSM */
+enum {
+ GD_DORMANT, GD_WAKE, GD_COOLDOWN, GD_IDLE, GD_WAIT, GD_STARTLE, GD_TURN, GD_WALK, GD_RUN, GD_STOP, GD_TURN_BACK,
+ GD_RETURN, GD_ANTIC, GD_ATTACK, GD_RECOIL, GD_ATTACK_END, GD_STOMP_ANTIC, GD_STOMP_AIR, GD_STOMP_LAND, GD_STOMP_CD
+};
+#define GD_ATTACK_SCALE 10.0f /* (Wake: its Attack Range's x scale) */
+/* (e->b0 facing right, b1 running, wk_stop woken; ax walk speed, ay run speed; qx, qy its roam's ends; tx its idle
+ * spot; c0, c1 clubs, stomps in a row) */
+
+static float guard_var(const Enemy *e, int i) {
+ const Ent *v = enemy_rec(e, ET_VARS);
+ return v ? (i == 0 ? v->p0 : v->p1) : (i == 0 ? 9 : 23.5f);
+}
+
+/* in its Attack Range (smaller once woken) */
+static bool guard_in_attack(const Enemy *e) {
+ const Ent *r = enemy_rec(e, ET_RANGE);
+ if (!r) return false;
+ float k = e->st == GD_DORMANT || r->p0 <= 0 ? 1 : GD_ATTACK_SCALE / r->p0;
+ return hero_in_box(e, r->x0 * k, r->y0, r->x1 * k, r->y1);
+}
+
+static void guard_face(Enemy *e, bool right) {
+ e->b0 = right;
+ set_scale_x(e, right ? 1.0f : -1.0f);
+}
+
+static void guard_idle(Enemy *e) {
+ e->b1 = false;
+ e->body.vx = 0;
+ anim_play(&e->anim, CLIP_GUARD_IDLE);
+ cam_rumble(RUMBLE_OFF);
+ e->st = GD_IDLE, e->t0 = 0;
+}
+
+static bool guard_hero_in_roam(const Enemy *e) { return hero_x() >= e->qx && hero_x() <= e->qy; }
+
+/* Face Hero: towards the hero (Turn), else an attack (Attack Choice) */
+static void guard_face_hero(Enemy *e) {
+ bool right = hero_x() > e->body.x;
+ if (hero_x() == e->body.x) return;
+ e->ax = right ? 5 : -5, e->ay = right ? 10 : -10;
+ cam_rumble(RUMBLE_OFF);
+ if (right != e->b0) {
+ guard_face(e, right);
+ anim_play_from_frame(&e->anim, CLIP_GUARD_TURN, 0);
+ e->st = GD_TURN;
+ return;
+ }
+ for (;;) {
+ bool club = rand_range(0, 1) < 0.75f;
+ if (club && e->c0 < 4) {
+ e->c0++, e->c1 = 0;
+ e->b1 = false;
+ anim_play_from_frame(&e->anim, CLIP_GUARD_ANTICIPATE, 0);
+ e->body.vx = 0;
+ e->st = GD_ANTIC;
+ return;
+ }
+ if (!club && e->c1 < 2) {
+ e->c1++, e->c0 = 0;
+ anim_play_from_frame(&e->anim, CLIP_GUARD_STOMP_ANTIC, 0);
+ e->body.vx = 0;
+ e->st = GD_STOMP_ANTIC, e->t0 = 0;
+ return;
+ }
+ }
+}
+
+/* Alert: towards the hero, walking or running (Chase), or turning first */
+static void guard_alert(Enemy *e) {
+ float dx = hero_x() - e->body.x;
+ if (dx == 0) return;
+ bool right = dx > 0;
+ e->ax = right ? 5 : -5, e->ay = right ? 10 : -10;
+ if (right != e->b0) {
+ cam_rumble(RUMBLE_OFF);
+ guard_face(e, right);
+ anim_play_from_frame(&e->anim, CLIP_GUARD_TURN, 0);
+ e->st = GD_TURN;
+ return;
+ }
+ float dy = hero_y() - e->body.y, dist = sqrtf(dx * dx + dy * dy);
+ if (e->b1 || dist > guard_var(e, 0)) {
+ if (e->st != GD_RUN) cam_shake(SHAKE_SMALL), cam_rumble(RUMBLE_SMALL); /* (SmallRumble) */
+ e->b1 = true;
+ anim_play(&e->anim, CLIP_GUARD_RUN);
+ e->st = GD_RUN;
+ } else {
+ cam_rumble(RUMBLE_OFF);
+ anim_play(&e->anim, CLIP_GUARD_WALK);
+ e->st = GD_WALK;
+ }
+}
+
+static void guard_start(Enemy *e) {
+ /* Initiate: its roam (and idle spot) about where it starts; Start Left / Right */
+ float roam = guard_var(e, 1);
+ e->qx = e->body.x - roam, e->qy = e->body.x + roam, e->tx = e->body.x;
+ const Ent *v = enemy_rec(e, ET_VARS);
+ guard_face(e, !(v && (v->a & 1)));
+ e->wk_stop = 1; /* (Woken) */
+ anim_play(&e->anim, CLIP_GUARD_DORMANT);
+ e->st = GD_DORMANT;
+}
+
+static bool guard_overhead(const Enemy *e) { return hero_in_box(e, 0, 1.9629f, 1.545f, 1.7664f); }
+
+static void guard_update(Enemy *e) {
+ bool done = (e->anim.events & ANIM_DONE) != 0, hit = (e->flags & 8) != 0;
+ bool attack = e->can_see && guard_in_attack(e), alert = e->can_see && e->in_alert;
+ bool lose = !e->can_see || !e->in_alert || hero_x() > e->qy || hero_x() < e->qx;
+ e->t0 += DT;
+ switch (e->st) {
+ case GD_DORMANT:
+ if (attack || hit) anim_play_from_frame(&e->anim, CLIP_GUARD_WAKE, 0), e->st = GD_WAKE;
+ break;
+ case GD_WAKE:
+ if (done) anim_play(&e->anim, CLIP_GUARD_IDLE), e->st = GD_COOLDOWN, e->t0 = 0;
+ break;
+ case GD_COOLDOWN:
+ if (e->t0 >= 0.21f) guard_idle(e);
+ break;
+ case GD_IDLE:
+ if (attack) guard_face_hero(e);
+ else if (alert && guard_hero_in_roam(e)) {
+ /* In Roam Distance?, Woken?: startled first if it went back to its spot */
+ if (e->wk_stop) guard_alert(e);
+ else anim_play_from_frame(&e->anim, CLIP_GUARD_STARTLE, 0), e->wk_stop = 1, e->st = GD_STARTLE;
+ } else if (e->t0 >= 4) {
+ /* Return Check: back to its spot */
+ if (e->body.x > e->tx + 1 || e->body.x < e->tx - 1) {
+ bool right = e->body.x < e->tx - 1;
+ if (right != e->b0) {
+ guard_face(e, right);
+ anim_play_from_frame(&e->anim, CLIP_GUARD_TURN, 0);
+ e->st = GD_TURN_BACK;
+ } else
+ e->st = GD_RETURN, anim_play(&e->anim, CLIP_GUARD_WALK), e->wk_stop = 0, e->b1 = false;
+ } else
+ e->st = GD_WAIT;
+ }
+ break;
+ case GD_WAIT:
+ guard_idle(e);
+ break;
+ case GD_STARTLE:
+ if (done) guard_alert(e);
+ break;
+ case GD_TURN:
+ if (guard_overhead(e)) {
+ e->b1 = false;
+ anim_play_from_frame(&e->anim, CLIP_GUARD_ANTICIPATE, 0);
+ e->body.vx = 0;
+ e->st = GD_ANTIC;
+ } else if (done)
+ guard_idle(e);
+ break;
+ case GD_WALK:
+ case GD_RUN: {
+ float dx = hero_x() - e->body.x, dy = hero_y() - e->body.y;
+ if (e->st == GD_WALK && sqrtf(dx * dx + dy * dy) > guard_var(e, 0)) guard_alert(e);
+ else if (attack) guard_face_hero(e);
+ else if (lose) {
+ cam_rumble(RUMBLE_OFF);
+ e->body.vx = 0;
+ anim_play_from_frame(&e->anim, e->st == GD_RUN ? CLIP_GUARD_STOP_RUN : CLIP_GUARD_STOP_WALK, 0);
+ e->b1 = false;
+ e->st = GD_STOP;
+ } else if (e->t0 > 0)
+ guard_alert(e), e->t0 = 0; /* (WAIT: Alert again, the next frame) */
+ break;
+ }
+ case GD_STOP:
+ if (done) guard_idle(e);
+ break;
+ case GD_TURN_BACK:
+ if (done) e->st = GD_RETURN, anim_play(&e->anim, CLIP_GUARD_WALK), e->wk_stop = 0, e->b1 = false;
+ break;
+ case GD_RETURN:
+ if (attack) guard_face_hero(e);
+ else if (alert && guard_hero_in_roam(e)) guard_alert(e);
+ else if (e->b0 ? e->body.x > e->tx - 1 : e->body.x < e->tx + 1) guard_idle(e);
+ break;
+ case GD_ANTIC:
+ if (done) {
+ /* Attack: its club swung (Swipe), then the impact */
+ hitbox_on(e, 0, CLIP_GUARD_SWIPE);
+ anim_play_from_frame(&e->anim, CLIP_GUARD_ATTACK2, 0);
+ e->st = GD_ATTACK, e->t0 = 0;
+ }
+ break;
+ case GD_ATTACK:
+ if (e->t0 >= 0.201f) {
+ cam_shake(SHAKE_AVERAGE); /* (Attack Recoil) */
+ e->st = GD_RECOIL, e->t0 = 0;
+ }
+ break;
+ case GD_RECOIL:
+ if (e->t0 >= 0.14f) e->body.vx = 0, e->st = GD_ATTACK_END;
+ break;
+ case GD_ATTACK_END:
+ if (done || !e->anim.playing) anim_play(&e->anim, CLIP_GUARD_IDLE), e->st = GD_COOLDOWN, e->t0 = 0;
+ break;
+ case GD_STOMP_ANTIC:
+ if (e->t0 >= 0.35f) anim_play_from_frame(&e->anim, CLIP_GUARD_STOMP_JUMP, 0), e->st = GD_STOMP_AIR;
+ break;
+ case GD_STOMP_AIR:
+ if (done) {
+ /* Land: two shockwaves, either way */
+ e->body.vx = 0;
+ anim_play_from_frame(&e->anim, CLIP_GUARD_STOMP_LAND, 0);
+ wave_spawn(e->body.x, e->body.y - 4, true, 18, 1.25f);
+ wave_spawn(e->body.x, e->body.y - 4, false, 18, 1.25f);
+ cam_shake(SHAKE_AVERAGE);
+ e->st = GD_STOMP_LAND;
+ }
+ break;
+ case GD_STOMP_LAND:
+ if (done) anim_play(&e->anim, CLIP_GUARD_IDLE), e->st = GD_STOMP_CD, e->t0 = 0;
+ break;
+ case GD_STOMP_CD:
+ if (e->t0 >= 0.4f) e->st = GD_COOLDOWN, e->t0 = 0;
+ break;
+ }
+}
+
+static void guard_fixed(Enemy *e) {
+ switch (e->st) {
+ case GD_WALK: e->body.vx = e->ax; break;
+ case GD_RUN: e->body.vx = e->ay; break;
+ case GD_RETURN: e->body.vx = e->b0 ? 5 : -5; break;
+ case GD_RECOIL: e->body.vx = e->b0 ? -5 : 5; break; /* (Recoil) */
+ case GD_STOMP_AIR: e->body.vx = e->b0 ? -10 : 10; break; /* (Jump Velocity: back) */
+ }
+}
+
/* ---------------------------------------------------------------- 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,
@@ -1799,6 +2253,13 @@ void enemies_enter(void) {
else if (FSM(e) == EF_SPITTER) spitter_start(e, d);
else if (FSM(e) == EF_ROLLER) roller_start(e);
else if (FSM(e) == EF_BLOCKER) blocker_start(e);
+ else if (FSM(e) == EF_LEAPER) leaper_start(e);
+ else if (FSM(e) == EF_GUARD) guard_start(e);
+ for (int h = 0; h < 8; h++) {
+ const Ent *r = hitbox_rec(e, h);
+ if (!r) break;
+ if (r->a) e->hb_on |= (uint8_t)(1 << h);
+ }
}
#ifdef HOST
if (getenv("ENEMYCOUNT")) {
@@ -1810,6 +2271,8 @@ void enemies_enter(void) {
memset(&sh, 0, sizeof sh);
memset(balls, 0, sizeof balls);
memset(bullets, 0, sizeof bullets);
+ memset(waves, 0, sizeof waves);
+ memset(spurts, 0, sizeof spurts);
shade_spawn_check();
}
@@ -1837,6 +2300,8 @@ void enemies_fixed(void) {
else if (FSM(e) == EF_BOUNCER) bouncer_fixed(e);
else if (FSM(e) == EF_SPITTER) spitter_fixed(e);
else if (FSM(e) == EF_ROLLER) roller_fixed(e);
+ else if (FSM(e) == EF_LEAPER) leaper_fixed(e);
+ else if (FSM(e) == EF_GUARD) guard_fixed(e);
recoil_fixed(e);
body_step(&e->body, DT);
} else if (FSM(e) != EF_BLOCKER) {
@@ -1869,6 +2334,7 @@ void enemies_fixed(void) {
geo_tick();
balls_tick();
bullets_tick();
+ waves_tick();
}
/* Update: the FSMs' every-frame actions, the animations, the timers */
@@ -1890,6 +2356,10 @@ void enemies_update(void) {
else if (FSM(e) == EF_SPITTER) spitter_update(e);
else if (FSM(e) == EF_ROLLER) roller_update(e);
else if (FSM(e) == EF_BLOCKER) blocker_update(e);
+ else if (FSM(e) == EF_LEAPER) leaper_update(e);
+ else if (FSM(e) == EF_GUARD) guard_update(e);
+ hitbox_tick(e);
+ e->flags &= (uint8_t)~8;
} else if (FSM(e) == EF_SHADE)
shade_update(e);
else if (FSM(e) == EF_BLOCKER)
@@ -1911,12 +2381,17 @@ void enemies_draw(void) {
else
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->sub_hb && e->sub.sprite >= 0 && e->mode == EM_ALIVE) {
+ sprite_inst(e->sub.sprite, e->body.x, e->body.y, z - 0.001f, e->sx, 1, 0, &in);
+ gfx_actor(&in, SORT_KEY(0, 0));
+ }
if (FSM(e) == EF_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));
}
}
bullets_draw();
+ spurts_draw();
for (int i = 0; i < MAX_BALLS; i++) {
const Ball *b = &balls[i];
if (!b->on) continue;
@@ -1964,6 +2439,11 @@ int enemies_touch_hero(float x0, float y0, float x1, float y1, int *side) {
*side = e->body.x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
return e->damage;
}
+ int hd = e->hb_on ? hitbox_touch(e, x0, y0, x1, y1) : 0;
+ if (hd) {
+ *side = e->body.x > g_hero.body.x ? SIDE_RIGHT : SIDE_LEFT;
+ return hd;
+ }
if (FSM(e) == EF_SHADE && sh.slash_on) {
/* its Slash's polygon */
float pts[10];
@@ -1977,6 +2457,8 @@ int enemies_touch_hero(float x0, float y0, float x1, float y1, int *side) {
}
int dmg = bullets_touch_hero(x0, y0, x1, y1, side);
if (dmg) return dmg;
+ dmg = spurts_touch(x0, y0, x1, y1, side);
+ if (dmg) return dmg;
for (int i = 0; i < MAX_BALLS; i++) {
const Ball *b = &balls[i];
if (!b->on || b->ending) continue;
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index 975629f2..d1a95e06 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -285,8 +285,10 @@ void hud_draw(void);
void cam_init(void);
void cam_tick(void);
void cam_snap_to_hero(void); /* (after a respawn) */
-enum { SHAKE_ENEMY_KILL = 1, SHAKE_AVERAGE, SHAKE_BIG }; /* the CameraShake FSM's events */
+enum { SHAKE_ENEMY_KILL = 1, SHAKE_AVERAGE, SHAKE_BIG, SHAKE_SMALL }; /* the CameraShake FSM's events */
void cam_shake(int kind);
+enum { RUMBLE_OFF, RUMBLE_SMALL, RUMBLE_MED, RUMBLE_BIG };
+void cam_rumble(int kind); /* (RumblingSmall, ...: on until turned off) */
void cam_freeze(void); /* FreezeInPlace (both) */
void cam_lock(int ent); /* CameraController.LockToArea, the hero entering the area's trigger */
void cam_release(int ent); /* ReleaseLock */
diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.c
index 65edf91d..dffffdbf 100644
--- a/games/hollowknight/src/gfx.c
+++ b/games/hollowknight/src/gfx.c
@@ -190,9 +190,12 @@ static int pal_get(uint16_t tex, uint8_t tint, uint8_t flags, uint32_t alpha) {
/* (a flash: towards its color by its amount, the alpha kept) */
float fk = tint >= DYN_TINT ? t[7] / 255.0f : 0, fr = fk ? t[4] / 255.0f * fk : 0, fg = fk ? t[5] / 255.0f * fk : 0,
fb = fk ? t[6] / 255.0f * fk : 0;
- for (int i = 0; i < 15; i++)
- p->c[i + 1] = grade(pc[4 * i] / 255.0f * tr * (1 - fk) + fr, pc[4 * i + 1] / 255.0f * tg * (1 - fk) + fg,
- pc[4 * i + 2] / 255.0f * tb * (1 - fk) + fb, pc[4 * i + 3] / 255.0f * ta);
+ for (int i = 0; i < 15; i++) {
+ /* (RGB565, then alpha) */
+ unsigned v = pc[3 * i] | pc[3 * i + 1] << 8;
+ p->c[i + 1] = grade((float)(v >> 11) / 31 * tr * (1 - fk) + fr, (float)(v >> 5 & 63) / 63 * tg * (1 - fk) + fg,
+ (float)(v & 31) / 31 * tb * (1 - fk) + fb, pc[3 * i + 2] / 255.0f * ta);
+ }
}
return s;
}
diff --git a/games/hollowknight/tools/actors.py b/games/hollowknight/tools/actors.py
index a5ae043d..cd87704d 100644
--- a/games/hollowknight/tools/actors.py
+++ b/games/hollowknight/tools/actors.py
@@ -40,6 +40,12 @@ ENEMY_KINDS = [
{"IDLE": "Idle", "TURN": "Turn", "WALK": "Walk", "A1": "Attack Anticipate", "A2": "Attack Lunge", "A3": "Attack Cooldown"}),
("hornhead", "Zombie Swipe", ("sharedassets40.assets", 192), "HUSK",
{"IDLE": "Idle", "TURN": "Turn", "WALK": "Walk", "A1": "Attack Anticipate", "A2": "Attack Lunge", "A3": "Attack Cooldown"}),
+ ("leaper", "Zombie Leap", ("sharedassets57.assets", 82), "LEAPER",
+ {"IDLE": "Idle", "TURN": "Turn", "WALK": "Walk", "A1": "Attack", "A2": "Land"}),
+ # (its own code plays its clips by name: CLIP_GUARD_*)
+ ("guard", "Zombie Guard", ("sharedassets58.assets", 57), "GUARD", {"IDLE": "Idle"},
+ ["Dormant", "Wake", "Walk", "Run", "Stop Run", "Stop Walk", "Turn", "Anticipate", "Startle", "Stomp Antic",
+ "Stomp Land", "Attack2", "Swipe", "Stomp Jump"]),
]
ENEMY_CORPSE_LIBS = {} # (name -> its corpse's library, if not its own)
@@ -50,21 +56,21 @@ def enemy_kind_id(name):
def _enemy_actors():
out = {}
- for name, fsm, (f, pid), code, roles in ENEMY_KINDS:
+ for name, fsm, (f, pid), code, roles, *extra in ENEMY_KINDS:
if name in ("crawler", "buzzer"):
out[name] = (f, pid, None)
continue
if name == "shade":
continue
have = {unity.S(c["name"]) for c in tk2d.animation(f, pid)["clips"]}
- out[name] = (f, pid, sorted((set(roles.values()) | {"Death Air", "Death Land"}) & have))
+ out[name] = (f, pid, sorted((set(roles.values()) | set(extra[0] if extra else []) | {"Death Air", "Death Land"}) & have))
return out
def kind_table(clip_ids):
"""src/data.h's KIND_TABLE: each kind's FSM code and its roles' clips (-1: none)"""
rows = []
- for name, fsm, lib, code, roles in ENEMY_KINDS:
+ for name, fsm, lib, code, roles, *extra in ENEMY_KINDS:
ids = []
for r in ROLES:
clip = roles.get(r) or {"DEATH_AIR": "Death Air", "DEATH_LAND": "Death Land"}.get(r)
@@ -94,7 +100,7 @@ ACTORS = {"knight": KNIGHT,
"Appear", "Fireball", "Fireball End", "Cast Ring"])}
ACTORS.update(_enemy_actors())
# enemies' shots (EnemyBullet)
-ACTORS["bullet"] = ("sharedassets32.assets", 745, ["Idle", "Impact"])
+ACTORS["bullet"] = ("sharedassets32.assets", 745, ["Idle", "Impact", "Shockwave Spurt"])
# sprites of prefab objects the game draws itself: name -> (file, prefab, object, resolution)
NAMED = {
@@ -160,6 +166,12 @@ def build():
frames.append((index[key], bool(f.get("triggerEvent"))))
clips.append({"id": clip_id(actor, name), "fps": float(c["fps"]), "wrap": int(c["wrapMode"]),
"loop": int(c.get("loopStart", 0)), "frames": frames})
+ # (the clip's new frames share one palette: theirs together)
+ new = sorted({i for i, _ in frames if sprites[i]["job"].pal is None})
+ if new:
+ pal = art.joint_palette([sprites[i]["job"].arr for i in new])
+ for i in new:
+ sprites[i]["job"].pal = pal
return sprites, clips
diff --git a/games/hollowknight/tools/art.py b/games/hollowknight/tools/art.py
index 5704426f..d1884be4 100644
--- a/games/hollowknight/tools/art.py
+++ b/games/hollowknight/tools/art.py
@@ -89,6 +89,43 @@ def kmeans_palette(px, k, iters=10, seed=0):
return c, labels
+def _features(p):
+ """RGBA texels (N x 4 float) -> what colors are told apart by: premultiplied color, and alpha weighted more"""
+ al = p[:, 3:4] / 255.0
+ return np.concatenate([p[:, :3] * al, p[:, 3:4] * 1.5], 1)
+
+
+def joint_palette(arrs):
+ """The palette (15 x RGBA, by brightness) several images share (an animation's frames)."""
+ px = [a[a[..., 3] >= 8].astype(np.float32) for a in arrs]
+ px = np.concatenate([p for p in px if len(p)] or [np.zeros((0, 4), np.float32)])
+ pal = np.zeros((15, 4), np.uint8)
+ if not len(px):
+ return pal
+ c, _ = kmeans_palette(_features(px), 15)
+ ca = np.clip(c[:, 3] / 1.5, 1, 255)
+ ca[ca >= 250] = 255
+ rgb = np.clip(c[:, :3] / (ca[:, None] / 255.0), 0, 255)
+ cols = np.concatenate([rgb, ca[:, None]], 1)
+ cols = cols[np.argsort(cols[:, :3].sum(1) * cols[:, 3])]
+ pal[:len(cols)] = np.round(cols).astype(np.uint8)
+ return pal
+
+
+def quantize_to(a, pal):
+ """RGBA uint8 image, a palette -> indices (0 transparent, 1..15: the nearest of the palette's colors)."""
+ mask = a[..., 3] >= 8
+ idx = np.zeros(a.shape[:2], np.uint8)
+ if mask.any():
+ f = _features(a[mask].astype(np.float32))
+ c = _features(pal.astype(np.float32))
+ lab = np.empty(len(f), np.int32)
+ for i in range(0, len(f), 65536):
+ lab[i:i + 65536] = ((f[i:i + 65536, None, :] - c[None]) ** 2).sum(-1).argmin(1)
+ idx[mask] = (lab + 1).astype(np.uint8)
+ return idx
+
+
def quantize(a):
"""RGBA uint8 image -> (indices: 0 transparent, 1..15), palette 15 x RGBA (straight alpha)."""
alpha = a[..., 3].astype(np.float32)
@@ -203,7 +240,8 @@ def encode_job(job):
if _SRC_HASH is None:
_SRC_HASH = hashlib.sha1(open(__file__, "rb").read()).hexdigest()
if isinstance(job, ImageJob):
- key = hashlib.sha1(_SRC_HASH.encode() + job.arr.tobytes() + repr(job.arr.shape).encode()).hexdigest()
+ key = hashlib.sha1(_SRC_HASH.encode() + job.arr.tobytes() + repr(job.arr.shape).encode() +
+ (job.pal.tobytes() if getattr(job, "pal", None) is not None else b"")).hexdigest()
else:
key = hashlib.sha1(repr((_SRC_HASH, job.key, job.scale, job.blur, job.alpha_only, job.all_cells, job.cells,
job.magnified)).encode()).hexdigest()
@@ -291,8 +329,9 @@ def soft_factor(arr, magnified):
class ImageJob:
"""A texture made from an image already at its size (actors' sprite frames): all of it kept."""
- def __init__(self, arr, name):
+ def __init__(self, arr, name, pal=None):
self.arr, self.name = arr, name
+ self.pal = pal # (a palette shared with others: its clip's)
self.alpha_only = False
self.blur = False
self.magnified = False
@@ -341,6 +380,8 @@ def encode_variant(v):
if v.alpha_only:
idx = np.clip(np.round(arr[..., 3] / 85.0), 0, 3).astype(np.uint8)
t.pal = None
+ elif getattr(v, "pal", None) is not None:
+ idx, t.pal = quantize_to(arr, v.pal), v.pal
else:
idx, t.pal = quantize(arr)
TW = WIDE if v.alpha_only else TILE
diff --git a/games/hollowknight/tools/ents.py b/games/hollowknight/tools/ents.py
index 6e25566a..495c15b9 100644
--- a/games/hollowknight/tools/ents.py
+++ b/games/hollowknight/tools/ents.py
@@ -12,14 +12,16 @@ OK_BREAKABLE, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST, OK_BENCH = 1, 2, 3
# enemies (an OK_ENEMY's a: src/enemy.c), by their FSM and animation library
import actors as _actors
# enemies: by the FSM (or component) that runs them and their animation library -> their kind (actors.ENEMY_KINDS)
-ENEMIES = {(fsm, f, pid): i + 1 for i, (name, fsm, (f, pid), code, roles) in enumerate(_actors.ENEMY_KINDS) if fsm}
+ENEMIES = {(k[1], k[2][0], k[2][1]): i + 1 for i, k in enumerate(_actors.ENEMY_KINDS) if k[1]}
# their FSMs' variables an enemy's record keeps (ET_VARS: up to four numbers, then bools as bits)
ENEMY_VARS = {"Zombie Swipe": (["Lunge Speed", "Idle Time"], ["Coward"]),
"Bouncer Control": (["Speed"], ["Starts Inactive", "Start Up"]),
"Roller": (["Acceleration", "Max Speed", "Roll time Min", "Roll time Max", "Stop Time"], ["Moving Right"]),
- "Blocker Control": (["Shot Y Speed"], ["Facing Right"])}
+ "Blocker Control": (["Shot Y Speed"], ["Facing Right"]),
+ "Zombie Leap": (["Idle Time"], []),
+ "Zombie Guard": (["Chase Distance", "Roam Distance"], ["Start Facing Left"])}
# what follows an enemy's record: ENT_BOX records, tagged
-ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE, ET_VARS, ET_TERRAIN = 0, 1, 2, 3, 4, 5, 6, 7
+ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE, ET_VARS, ET_TERRAIN, ET_HITBOX = 0, 1, 2, 3, 4, 5, 6, 7, 8
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}
@@ -358,10 +360,31 @@ def _enemy(o, by_id, persist, name, d=None, strings=None, owners=None):
r = _alert_range(q)
if r is None:
continue
+ # (p0, p1: its scale, for an FSM that changes it)
+ k = (abs(q["lscale"][0]), abs(q["lscale"][1]), 0, 0)
if q["name"] == "Alert Range New":
- out.append(rec(ENT_BOX, ET_ALERT, box=r))
+ out.append(rec(ENT_BOX, ET_ALERT, box=r, p=k))
else:
- out.append(rec(ENT_BOX, ET_RANGE, box=r, s0=strings.id(q["name"]) if strings else 0))
+ out.append(rec(ENT_BOX, ET_RANGE, box=r, p=k, s0=strings.id(q["name"]) if strings else 0))
+ # its attacks: children that hurt the Knight (DamageHero), their outlines in its own units (x as if facing its
+ # scale's way), then their shapes' points; a: on at first; s0: the child's name
+ sgn = -1.0 if o["lscale"][0] < 0 else 1.0
+ for ch in o.get("children", []):
+ q = by_id[ch]
+ qd = next((c.get("v") for c in q["c"] if c.get("class") == "DamageHero" and c.get("v") is not None), None)
+ qc = next((c for c in q["c"] if c["type"] in ("BoxCollider2D", "PolygonCollider2D") and c.get("v") and
+ c["v"].get("m_IsTrigger") and c["v"].get("m_Enabled", 1)), None)
+ if qd is None or qc is None:
+ continue
+ pts = [((x - o["pos"][0]) * sgn, y - o["pos"][1]) for x, y in _shape(q, qc)][:8]
+ xs, ys = [t[0] for t in pts], [t[1] for t in pts]
+ nrec = (len(pts) + 3) // 4
+ out.append(rec(ENT_BOX, ET_HITBOX, box=(min(xs), min(ys), max(xs), max(ys)),
+ p=(len(pts), qd.get("damageDealt", 1), qd.get("hazardType", 1), 0), a=1 if q["self_active"] else 0,
+ group=nrec, s0=strings.id(q["name"]) if strings else 0))
+ for i in range(nrec):
+ t = pts[4 * i:4 * i + 4] + [pts[-1]] * (4 - len(pts[4 * i:4 * i + 4]))
+ out.append(rec(ENT_SHAPE, box=(t[0][0], t[0][1], t[1][0], t[1][1]), p=(t[2][0], t[2][1], t[3][0], t[3][1])))
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) | \
diff --git a/games/hollowknight/tools/pack.py b/games/hollowknight/tools/pack.py
index 0bd41f60..5ad6d4ae 100644
--- a/games/hollowknight/tools/pack.py
+++ b/games/hollowknight/tools/pack.py
@@ -62,8 +62,8 @@ class Blob:
while len(self.b) % n:
self.b.append(0)
- def add(self, data):
- self.align()
+ def add(self, data, align=4):
+ self.align(align)
o = len(self.b)
self.b += data
return o
@@ -322,13 +322,19 @@ def main():
# the starting probabilities as the game uses them: each with its count (tilecode.h: tc_pack)
secs["PRIOR"].b += b"".join(struct.pack("<H", v << 4 | TC_N0) for v in struct.unpack("<%dH" % (len(prior) // 2), prior))
tex_id = {}
+ pal_at = {} # (palettes shared: once each)
nblk = 0
texrec = bytearray()
for i, (v, t) in enumerate(zip(variants, texs)):
tex_id[id(v)] = (i, t.w, t.h)
pal_off = 0xFFFFFFFF
if t.pal is not None:
- pal_off = secs["PAL"].add(t.pal.tobytes())
+ # (15 colors: RGB565, then alpha; read a byte at a time)
+ pb = b"".join(struct.pack("<HB", (int(r) * 31 + 127) // 255 << 11 | (int(g) * 63 + 127) // 255 << 5 |
+ (int(b) * 31 + 127) // 255, int(a)) for r, g, b, a in t.pal)
+ if pb not in pal_at:
+ pal_at[pb] = secs["PAL"].add(pb, 1)
+ pal_off = pal_at[pb]
if t.fmt == art.FMT_SOFTA: # (alpha only: read in place)
pal_off = secs["SOFT"].add(t.soft)
texrec += struct.pack("<HHBBBBIII", t.w, t.h, 0, 0, t.fmt, 0, pal_off, t.base, 0)
@@ -357,6 +363,16 @@ def main():
secs["BLK"].b += bl
bidx += struct.pack("<I", len(secs["BLK"].b))
texrec += struct.pack("<HHBBBBIII", t.w, t.h, t.tw, t.th, t.fmt, 1 if v.blur else 0, pal_off, map_off, first)
+ if os.environ.get("ACTSTATS"):
+ import collections
+ tot = collections.Counter()
+ for j, sp in enumerate(sprites):
+ t = texs[len(variants) - len(sprites) + j]
+ nb = sum(len(b) for b in coded[t.first_block:t.first_block + (t.ntiles + art.BLOCK_TILES - 1) // art.BLOCK_TILES]) if t.fmt not in (art.FMT_SOFTA, art.FMT_SOFT) else len(t.soft or b"")
+ nb += (len(t.pal.tobytes()) if t.pal is not None else 0)
+ tot[str(sp.get("key", ("?",)))[:30].split(",")[0]] += nb
+ for k, v in tot.most_common(40):
+ print("ACT", v, k)
secs["TEX"].b += struct.pack("<I", len(variants)) + texrec
# actors: each sprite frame's texture and where its top-left corner is (local units), units a texel; the clips
first_actor = len(variants) - len(sprites)
@@ -388,7 +404,7 @@ def main():
off = secs["RBLOB"].add(hc)
assert len(r) < 32, r
rooms += struct.pack("<32sIII", r.encode(), off, len(hc), len(body))
- print("room %-26s %5d instances in %2d sectors, %6d packed" % (r, len(keep), len(blobs), packed + len(hc)), flush=True)
+ print("room %-26s %5d instances in %2d sectors, %6d packed, ground %d, recs %d" % (r, len(keep), len(blobs), packed + len(hc), len(ground), len(recs)), flush=True)
secs["ROOMS"].b += rooms
secs["STR"].b += STRINGS.blob()
secs["TEXT"].b += TEXTS.blob()