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: colliders set by animation frames

Mason Cheneabdb4697f97
diff --git a/games/hollowknight/src/anim.c b/games/hollowknight/src/anim.c
index 6aedf966..5649d57b 100644
--- a/games/hollowknight/src/anim.c
+++ b/games/hollowknight/src/anim.c
@@ -18,6 +18,16 @@ static const uint16_t *clip_frames(const ClipRec *c) {
const SpriteRec *sprite_rec(int id) { return (const SpriteRec *)(const void *)section(SEC_SPR) + id; }
+int sprite_collider(int sprite, const float **data, int *n) {
+ if (sprite < 0) return SC_UNSET;
+ unsigned c = sprite_rec(sprite)->col;
+ if (c < 2) return (int)c;
+ const uint8_t *s = section(SEC_SCOL), *e = s + rd32(s + 4 + 4 * (c - 2));
+ *data = (const float *)(const void *)(e + 4);
+ *n = e[1];
+ return e[0] == 2 ? SC_BOX : SC_SHAPE;
+}
+
float clip_duration(int clip) {
const ClipRec *c = clip_rec(clip);
return c->n / c->fps;
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.bin
index ac3c8db8..c8b58ee0 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 676e42e9..6502e825 100644
--- a/games/hollowknight/src/data.h
+++ b/games/hollowknight/src/data.h
@@ -15,6 +15,7 @@
#define SEC_TEXT 12
#define SEC_FONT 13
#define SEC_PHASH 14
+#define SEC_SCOL 15
#define NUM_ROOMS 41
#define ROOM_TUTORIAL_01 0
#define ROOM_TOWN 1
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index 92151bcf..fcec0368 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -34,7 +34,8 @@ 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) */
+/* (flags: 1 collider off, 2 RECOIL HORIZONTAL, 4 invincible, 8 TOOK DAMAGE: for its FSM this frame, 16 collider off
+ * by its frame) */
typedef struct {
uint8_t mode, kind, st, flags;
uint16_t ent; /* its record (NO_ENT: spawned) */
@@ -65,6 +66,7 @@ typedef struct {
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) */
+ int16_t col_sprite; /* (the frame whose collider it has, + 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;
@@ -101,6 +103,20 @@ static void enemy_terrain_off(const Ent *d) {
for (int c = d[i].a; c < d[i].a + d[i].group; c++) phys_collider_enable(c, false);
}
+/* 2D Toolkit: the frame shown sets the collider (a box: its body's; none: off) */
+static void frame_collider(Enemy *e) {
+ if (e->anim.sprite + 1 == e->col_sprite) return;
+ e->col_sprite = (int16_t)(e->anim.sprite + 1);
+ const float *d;
+ int n, t = sprite_collider(e->anim.sprite, &d, &n);
+ float k = fabsf(e->sx);
+ if (t == SC_BOX) {
+ e->body.ox = d[0] * e->sx, e->body.oy = d[1] * k, e->body.hx = d[2] * k, e->body.hy = d[3] * k;
+ e->flags &= (uint8_t)~16;
+ } else if (t == SC_NONE)
+ e->flags |= 16;
+}
+
/* 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;
@@ -1456,6 +1472,24 @@ static int hitbox_touch(const Enemy *e, float x0, float y0, float x1, float y1)
if (!(e->hb_on >> i & 1)) continue;
const Ent *h = hitbox_rec(e, i);
if (!h) continue;
+ if (e->sub_hb == i + 1) {
+ /* (its sprite's frame sets its collider) */
+ const float *d;
+ int n, t = sprite_collider(e->sub.sprite, &d, &n);
+ if (t == SC_NONE) continue;
+ float ks = fabsf(e->sx);
+ if (t == SC_BOX) {
+ float cx = e->body.x + d[0] * e->sx, cy = e->body.y + d[1] * ks;
+ if (x1 > cx - d[2] * ks && x0 < cx + d[2] * ks && y1 > cy - d[3] * ks && y0 < cy + d[3] * ks) return (int)h->p1;
+ continue;
+ }
+ if (t == SC_SHAPE) {
+ float pts[16];
+ for (int j = 0; j < n && j < 8; j++) pts[2 * j] = e->body.x + d[2 * j] * e->sx, pts[2 * j + 1] = e->body.y + d[2 * j + 1] * ks;
+ if (box_meets_shape(x0, y0, x1, y1, pts, n < 8 ? n : 8)) return (int)h->p1;
+ 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];
@@ -2346,6 +2380,7 @@ void enemies_update(void) {
anim_update(&e->anim, DT);
if (e->flashing && (e->flash_t += DT) > 0.27f) e->flashing = false;
if (e->mode == EM_ALIVE) {
+ frame_collider(e);
if (e->evasion > 0) e->evasion -= DT;
if (e->ar_r > 0) sight_update(e);
if (FSM(e) == EF_BUZZER) buzzer_update(e, ent_at(e->ent));
@@ -2416,7 +2451,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 || (e->flags & 1)) continue;
+ if (e->mode != EM_ALIVE || (e->flags & 17)) continue;
float x0, y0, x1, y1;
enemy_box(e, &x0, &y0, &x1, &y1);
if (!box_meets_shape(x0, y0, x1, y1, pts, npts)) continue;
@@ -2432,7 +2467,7 @@ 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 || e->damage <= 0 || (e->flags & 1)) continue;
+ if (e->mode != EM_ALIVE || e->damage <= 0 || (e->flags & 17)) continue;
float a0, b0, a1, b1;
enemy_box(e, &a0, &b0, &a1, &b1);
if (x1 > a0 && x0 < a1 && y1 > b0 && y0 < b1) {
diff --git a/games/hollowknight/src/game.c b/games/hollowknight/src/game.c
index f940fb61..2534e7c8 100644
--- a/games/hollowknight/src/game.c
+++ b/games/hollowknight/src/game.c
@@ -80,7 +80,7 @@ const Ent *room_ents(int *n) {
/* ---------------------------------------------------------------- triggers: the Knight's box in the objects' */
/* ---------------------------------------------------------------- scene transitions (TransitionPoint, BeginSceneTransition) */
-enum { SC_NONE, SC_LEAVING };
+enum { SP_NONE, SP_LEAVING };
/* TransitionPoint.GetGatePosition: by its name */
static int gate_kind(const Ent *e) {
@@ -97,8 +97,8 @@ void game_fade_scene_in(void) { game_fade(0, 0.5f, 0.1f); }
void game_transition(int room, int entry, int gate, float delay, bool without_input) {
Game *g = &g_game;
- if (g->scene_phase != SC_NONE || room < 0) return;
- g->scene_phase = SC_LEAVING, g->scene_t = 0;
+ if (g->scene_phase != SP_NONE || room < 0) return;
+ g->scene_phase = SP_LEAVING, g->scene_t = 0;
g->next_room = (int16_t)room, g->next_entry = (uint16_t)entry, g->next_delay = delay;
g_hero.enter_without_input = without_input;
hero_leave_scene(gate);
@@ -109,7 +109,7 @@ void game_transition(int room, int entry, int gate, float delay, bool without_in
/* TryDoTransition: the Knight through a gate (facing it, not recoiling), or pushed back out of it */
static void gate_touched(const Ent *e) {
Hero *h = &g_hero;
- if ((e->flags & G_DOOR) || g_game.scene_phase != SC_NONE) return;
+ if ((e->flags & G_DOOR) || g_game.scene_phase != SP_NONE) return;
int g = gate_kind(e);
bool back = h->cs.recoiling || (g == GATE_RIGHT && !h->cs.facing_right) || (g == GATE_LEFT && h->cs.facing_right) ||
e->a == 0xFFFF; /* (or to a room this game leaves out) */
@@ -127,7 +127,7 @@ static void gate_touched(const Ent *e) {
/* the new room, the Knight at its entry gate */
static void scene_load(void) {
Game *g = &g_game;
- g->scene_phase = SC_NONE;
+ g->scene_phase = SP_NONE;
if (!room_load(g->next_room)) return;
memset(inside, 0, sizeof inside);
prompts_reset(); /* (PromptMarker.RecycleOnLevelLoad) */
@@ -150,7 +150,7 @@ static void scene_load(void) {
static void scene_tick(float real_dt) {
Game *g = &g_game;
- if (g->scene_phase != SC_LEAVING) return;
+ if (g->scene_phase != SP_LEAVING) return;
/* (the scene loads once the camera has faded: half a second) */
g->scene_t += real_dt;
if (g->scene_t >= 0.5f) scene_load();
diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.h
index 56826f57..f901f38c 100644
--- a/games/hollowknight/src/hk.h
+++ b/games/hollowknight/src/hk.h
@@ -127,11 +127,15 @@ const char *room_name(int id);
/* ---------------------------------------------------------------- sprites and animations (anim.c) */
typedef struct {
- uint16_t tex, pad;
+ uint16_t tex, col; /* (col: its collider, sprite_collider) */
float lx, ty; /* the texture's top-left corner, from the sprite's origin (units) */
float tu, tv; /* units a texel */
} SpriteRec;
const SpriteRec *sprite_rec(int id);
+/* the collider 2D Toolkit sets as a frame shows: SC_UNSET (left as it was), SC_NONE (off), SC_BOX (*data: center x y,
+ * half width height), SC_SHAPE (*n points, x y pairs); sprite units */
+enum { SC_UNSET, SC_NONE, SC_BOX, SC_SHAPE };
+int sprite_collider(int sprite, const float **data, int *n);
enum { ANIM_DONE = 1, ANIM_TRIGGER = 2 };
typedef struct {
int16_t clip, frame, sprite;
diff --git a/games/hollowknight/tools/actors.py b/games/hollowknight/tools/actors.py
index cd87704d..e856aea6 100644
--- a/games/hollowknight/tools/actors.py
+++ b/games/hollowknight/tools/actors.py
@@ -162,7 +162,8 @@ def build():
arr = np.asarray(small).copy()
index[key] = len(sprites)
sprites.append({"key": key, "job": art.ImageJob(arr, "%s/%s" % (actor, name)),
- "lx": lx, "ty": ty, "tu": wu / w, "tv": hu / h})
+ "lx": lx, "ty": ty, "tu": wu / w, "tv": hu / h,
+ "col": _collider(col["spriteDefinitions"][f["spriteId"]])})
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})
@@ -175,6 +176,23 @@ def build():
return sprites, clips
+def _collider(sd):
+ """A frame's collider as 2D Toolkit sets it when the frame shows: None (left as it is), ("none",), ("box", cx, cy, hx,
+ hy) or ("poly", [(x, y)...]); sprite units"""
+ if sd.get("physicsEngine") != 1:
+ return None
+ t = sd.get("colliderType")
+ if t == 1:
+ return ("none",)
+ if t == 2:
+ v = sd["colliderVertices"]
+ return ("box", v[0]["x"], v[0]["y"], abs(v[1]["x"]), abs(v[1]["y"]))
+ if t == 3 and sd.get("polygonCollider2D"):
+ pts = [(q["x"], q["y"]) for q in sd["polygonCollider2D"][0]["points"]]
+ return ("poly", pts[:8])
+ return None
+
+
def unity_sprites(keys):
"""Unity sprites objects show as actors (ents.Sprites: debris...) -> sprite records like build()'s; each at its
object's scale."""
diff --git a/games/hollowknight/tools/pack.py b/games/hollowknight/tools/pack.py
index 5ad6d4ae..02471910 100644
--- a/games/hollowknight/tools/pack.py
+++ b/games/hollowknight/tools/pack.py
@@ -28,7 +28,8 @@ 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", "PHASH"]
+SECTIONS = ["TEX", "TMAP", "PAL", "BIDX", "BLK", "ROOMS", "RBLOB", "PRIOR", "SOFT", "SPR", "CLIP", "STR", "TEXT", "FONT", "PHASH",
+ "SCOL"]
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
@@ -376,8 +377,27 @@ def main():
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)
+ # (their colliders, as frames set them: 0 none given, 1 none, 2 + an entry: a box or a shape)
+ col_ids, col_entries = {}, []
for i, sp in enumerate(sprites):
- secs["SPR"].b += struct.pack("<HHffff", first_actor + i, 0, sp["lx"], sp["ty"], sp["tu"], sp["tv"])
+ c, cid = sp.get("col"), 0
+ if c and c[0] == "none":
+ cid = 1
+ elif c:
+ b = struct.pack("<BBH4f", 2, 0, 0, *c[1:]) if c[0] == "box" else \
+ struct.pack("<BBH", 3, len(c[1]), 0) + b"".join(struct.pack("<2f", x, y) for x, y in c[1])
+ if b not in col_ids:
+ col_ids[b] = len(col_entries)
+ col_entries.append(b)
+ cid = 2 + col_ids[b]
+ secs["SPR"].b += struct.pack("<HHffff", first_actor + i, cid, sp["lx"], sp["ty"], sp["tu"], sp["tv"])
+ at = 4 + 4 * len(col_entries)
+ secs["SCOL"].b += struct.pack("<I", len(col_entries))
+ for b in col_entries:
+ secs["SCOL"].b += struct.pack("<I", at)
+ at += len(b)
+ for b in col_entries:
+ secs["SCOL"].b += b
secs["CLIP"].b += struct.pack("<I", len(clips))
frames = []
for c in clips: