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: objects switched by PlayerData; King's Pass's first crawler

Mason Chen3fbd1d902bcb
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.bin
index 7de44ca8..b6de99aa 100644
Binary files a/games/hollowknight/src/data.bin and b/games/hollowknight/src/data.bin differ
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index fb7f4ea3..7925703c 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -28,7 +28,8 @@ 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, ET_HITBOX, ET_ZONE };
+enum { ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE, ET_VARS, ET_TERRAIN, ET_HITBOX, ET_ZONE, ET_COND,
+ ET_CONTACT };
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) */
@@ -38,7 +39,8 @@ 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, 16 collider off
- * by its frame, 32 not drawn) */
+ * by its frame, 32 not drawn, 64 its FSMs off (FSMActivator), 128 its object off till the Knight touches a trigger
+ * (ActivateChildrenOnContact)) */
typedef struct {
uint8_t mode, kind, st, flags;
uint16_t ent; /* its record (NO_ENT: spawned) */
@@ -3958,7 +3960,10 @@ void enemies_enter(void) {
for (int i = 0; i < n && k < MAX_ENEMIES; i++) {
const Ent *d = &es[i];
if (d->type != ENT_OBJ || d->flags != OK_ENEMY) continue;
- if (persist_get(d->persist) || ((d->s1 & EF_ARENA_GONE) && arena_done()) || ((d->s1 & EF_ARENA_LATER) && !arena_done())) {
+ bool off = false; /* (DeactivateIfPlayerdata: ET_COND) */
+ for (int j = 1; j <= d->s0; j++)
+ if (d[j].type == ENT_BOX && d[j].flags == ET_COND && pd_flag((int)d[j].p0) == (d[j].p1 != 0)) off = true;
+ if (off || persist_get(d->persist) || ((d->s1 & EF_ARENA_GONE) && arena_done()) || ((d->s1 & EF_ARENA_LATER) && !arena_done())) {
enemy_terrain_off(d);
continue;
}
@@ -3981,7 +3986,10 @@ void enemies_enter(void) {
e->rc_base = rc ? rc->x0 : 15, e->rc_dur = rc ? rc->y0 : 0.5f, e->rc_flags = rc ? (uint8_t)rc->a : RF_NONE;
const Ent *wk = enemy_rec(e, ET_WALKER);
e->wk_rec = wk ? (uint16_t)(wk - room_ents(&n)) : 0;
- if (d->s1 & EF_DORMANT) {
+ if (d->s1 & EF_CONTACT) {
+ /* (its object off: nothing of it there) */
+ e->flags |= 128 | 32 | 1;
+ } else if (d->s1 & EF_DORMANT) {
/* (its FSMs off till the active region round the camera meets it: FSMActivator; its sprite still) */
int c = CLIP(e, R_IDLE) >= 0 ? CLIP(e, R_IDLE) : CLIP(e, R_A1) >= 0 ? CLIP(e, R_A1) : CLIP(e, R_WALK);
if (c >= 0) anim_play(&e->anim, c), e->anim.paused = true;
@@ -4028,7 +4036,7 @@ void enemies_fixed(void) {
else body_step(&e->body, DT);
continue;
}
- if (e->mode == EM_ALIVE && FSM(e) == EF_FKHEAD) continue;
+ if (e->mode == EM_ALIVE && (FSM(e) == EF_FKHEAD || (e->flags & 128))) continue; /* (the head: its body's; off) */
if (e->mode == EM_ALIVE && FSM(e) == EF_CLIMBER) {
/* (a kinematic body: its velocity, nothing in its way) */
recoil_fixed(e);
@@ -4101,6 +4109,17 @@ void enemies_update(void) {
frame_collider(e);
if (e->evasion > 0) e->evasion -= DT;
if (e->ar_r > 0) sight_update(e);
+ if (e->flags & 128) {
+ /* ActivateChildrenOnContact: on as the Knight touches the trigger */
+ const Ent *t = enemy_rec(e, ET_CONTACT);
+ const Body *k = &g_hero.body;
+ if (t && !g_hero.hidden && k->x + k->ox + k->hx > t->x0 && k->x + k->ox - k->hx < t->x1 &&
+ k->y + k->oy + k->hy > t->y0 && k->y + k->oy - k->hy < t->y1) {
+ e->flags &= (uint8_t)~(128 | 32 | 1);
+ enemy_fsm_start(e, ent_at(e->ent));
+ }
+ continue;
+ }
if (e->flags & 64) enemy_dormant_check(e);
if (e->flags & 64) {} /* (dormant) */
else if (FSM(e) == EF_BUZZER) buzzer_update(e, ent_at(e->ent));
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index 3e55d2fa..fa3ac952 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -9,6 +9,7 @@
enum {
PDF_AT_BENCH, PDF_HAS_MAP, PDF_HAS_QUILL, PDF_HAS_CHARM, PDF_CHARM_BENCH_MSG, PDF_MET_ELDERBUG, PDF_VISITED_CROSSROADS,
PDF_FALSE_KNIGHT_DEFEATED, PDF_FK_FIRST_PLOP, PDF_MAPPER_SHOP, PDF_CORN_CROSSROADS_LEFT, PDF_HORNET1_DEFEATED,
+ PDF_VISITED_DIRTMOUTH, PDF_TISO_ENCOUNTERED_TOWN, PDF_HAS_DASH, PDF_SHAMAN_PILLAR,
PDF_COUNT
};
typedef struct {
@@ -53,7 +54,7 @@ void save_set_respawn(const char *marker, bool facing_right); /* (Bench Contro
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, 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, OK_BATTLE, OK_FK_FLOOR, OK_BGATE,
- OK_ARENA, OK_EVENT, OK_SUMMON }; /* objects (ENT_OBJ's flags) */
+ OK_ARENA, OK_EVENT, OK_SUMMON, OK_COND }; /* objects (ENT_OBJ's flags) */
/* an enemy record's s1: starts alert (or first); startles; one of an arena's Pre Battle Enemies; its death counts for its arena; gone once its arena's
* fight is over; spawned by its mother's burster; there only once its arena's fight is over; its FSMs off till near
* the camera (FSMActivator); then its death's effects
@@ -61,6 +62,7 @@ enum { OK_BREAKABLE = 1, OK_ENEMY, OK_GREAT_DOOR, OK_GEO_ROCK, OK_CHEST, OK_BENC
enum { EF_START = 1, EF_STARTLES = 2, EF_PREBATTLE = 4, EF_BATTLE = 8, EF_ARENA_GONE = 16, EF_SPAWNED = 32, EF_ARENA_LATER = 64,
EF_DORMANT = 128 };
#define EF_DEATH_SHIFT 8
+#define EF_CONTACT 0x4000 /* (off till the Knight touches its parent's trigger: ActivateChildrenOnContact) */
/* battle gates (BG Control: their events) and arenas (Battle Control: obj.c) */
enum { BG_CLOSE, BG_QUICK_CLOSE, BG_OPEN, BG_QUICK_OPEN, BG_DESTROY };
void gates_event(int ev);
diff --git a/games/hollowknight/src/obj.c b/games/hollowknight/src/obj.c
index bd5cf921..b6d8ee1d 100644
--- a/games/hollowknight/src/obj.c
+++ b/games/hollowknight/src/obj.c
@@ -485,6 +485,14 @@ void obj_enter(void) {
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_COND) {
+ /* DeactivateIfPlayerdataTrue / False: off as the room loads, its sprites and colliders */
+ if (pd_flag((int)es[i].p0) == (es[i].p1 != 0)) {
+ if (es[i].group2) group_fade(es[i].group2, 0, 0);
+ for (int c = es[i].a; c < es[i].a + es[i].group; c++) phys_collider_enable(c, false);
+ }
+ continue;
+ }
if (es[i].flags == OK_ENEMY || es[i].flags == OK_BENCH || es[i].flags == OK_ARENA || es[i].flags == OK_EVENT ||
es[i].flags == OK_SUMMON)
continue; /* (enemy.c's, npc.c's; below) */
diff --git a/games/hollowknight/tools/ents.py b/games/hollowknight/tools/ents.py
index 4a281dd1..7b0e8d01 100644
--- a/games/hollowknight/tools/ents.py
+++ b/games/hollowknight/tools/ents.py
@@ -9,7 +9,7 @@ ENT_CAMLOCK, ENT_GATE, ENT_HAZARD_MARKER, ENT_RESPAWN, ENT_HAZARD_TRIGGER, ENT_M
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, OK_BENCH, OK_BATTLE, OK_FK_FLOOR, OK_BGATE, OK_ARENA, \
- OK_EVENT, OK_SUMMON = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
+ OK_EVENT, OK_SUMMON, OK_COND = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
# the battle gates' events (src/game.h: BG_*)
BG_EVENTS = ["BG CLOSE", "BG QUICK CLOSE", "BG OPEN", "BG QUICK OPEN", "BG DESTROY"]
# battle gates (BG Control: OK_BGATE's s0): closed at first, the bone ones' clips, gone once a PlayerData bool is set
@@ -30,14 +30,15 @@ ENEMY_VARS = {"Zombie Swipe": (["Lunge Speed", "Idle Time"], ["Coward"]),
"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, ET_HITBOX, ET_ZONE = \
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+ET_COLLIDER, ET_ALERT, ET_RANGE, ET_WALKER, ET_RECOIL, ET_CORPSE, ET_VARS, ET_TERRAIN, ET_HITBOX, ET_ZONE, ET_COND, \
+ ET_CONTACT = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
# (FSM bools: First Crawler or Start Alert; Startles; one of an arena's Pre Battle Enemies; its death counts for its
# arena (HealthManager.battleScene); gone once its arena's fight is over; spawned by its mother's burster (Fly Spawn);
# there only once its arena's fight is over; its FSMs off till near the camera: FSMActivator)
EF_START, EF_STARTLES, EF_PREBATTLE, EF_BATTLE, EF_ARENA_GONE, EF_SPAWNED, EF_ARENA_LATER, EF_DORMANT = \
1, 2, 4, 8, 16, 32, 64, 128
EF_DEATH_SHIFT = 8 # (bits 8-11: its enemyDeathType; 12-13: EnemyDeathEffects (0), Uninfected, NoEffect, BlackKnight)
+EF_CONTACT = 0x4000 # (off till the Knight touches its parent's trigger: ActivateChildrenOnContact; ET_CONTACT)
# 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}
MAX_GROUPS = 256
@@ -672,6 +673,16 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None, rec_base=0
objs = [o for o in d["objects"] if o["active"]]
by_id = {o["id"]: o for o in d["objects"]}
arena_gone, arena_counted, arena_later = _arenas(d, by_id)
+ import state
+ pdf = pd_flags()
+
+ def conds(o):
+ """(PlayerData flag, off if true) of the object and those above it, for the bools that change as one plays"""
+ out = []
+ while o is not None:
+ out += [(pdf[n], t) for n, t in state.conditions(o) if n in state.DYNAMIC]
+ o = by_id.get(o.get("parent"))
+ return out
enemy_rec_of, summons = {}, [] # (enemies' records; summoners, to point at theirs)
def subtree(oid, g):
@@ -795,6 +806,10 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None, rec_base=0
g = _gate(d, o, owners)
if g:
recs.append(g)
+ for flag, off_if in [(pdf[n], t) for n, t in state.conditions(o) if n in state.DYNAMIC]:
+ # an object off as the room loads with a PlayerData bool so: its sprites (and what is under it), its colliders
+ c0, cn = _subcols(d, owners, o["id"]) if owners else (0, 0)
+ recs.append(rec(ENT_OBJ, OK_COND, p=(flag, 1 if off_if else 0, 0, 0), a=c0, group=cn, group2=new_group(o["id"])))
ev = next((c.get("v") for c in o["c"] if c.get("class") == "SendPlaymakerEventOnEnable" and c.get("v")), None)
if ev:
# (an event broadcast as the room starts)
@@ -830,8 +845,27 @@ def room(d, rooms, strings, persist, name, sprites=None, owners=None, rec_base=0
s0=len(extra)))
recs += extra
en = _enemy(o, by_id, persist, name, d, strings, owners) or _props(d, o, by_id, rooms, strings, persist, sprites, owners, name)
+ if not en and any(c.get("class") == "ActivateChildrenOnContact" for c in o["c"]):
+ # (its children, off till the Knight touches its trigger: an enemy, flagged, the trigger after its records)
+ for ch in o.get("children", []):
+ q = by_id[ch]
+ en = _enemy(q, by_id, persist, name, d, strings, owners) if not q["self_active"] else None
+ if en:
+ en.append(rec(ENT_BOX, ET_CONTACT, box=_trigger(o) or (0, 0, 0, 0)))
+ b = bytearray(en[0])
+ struct.pack_into("<HH", b, 40, struct.unpack_from("<H", b, 40)[0] + 1,
+ struct.unpack_from("<H", b, 42)[0] | EF_CONTACT)
+ en[0] = bytes(b)
+ o = q
+ break
if en:
if en[0][1] == OK_ENEMY:
+ for flag, off_if in conds(o):
+ # (gone as the room loads, with the PlayerData bool so)
+ en.append(rec(ENT_BOX, ET_COND, p=(flag, 1 if off_if else 0, 0, 0)))
+ b = bytearray(en[0])
+ struct.pack_into("<H", b, 40, struct.unpack_from("<H", b, 40)[0] + 1)
+ en[0] = bytes(b)
enemy_rec_of[o["id"]] = len(recs)
fl = EF_PREBATTLE if o["id"] in prebattle else 0
fl |= EF_ARENA_LATER if o["id"] in arena_later else 0
diff --git a/games/hollowknight/tools/state.py b/games/hollowknight/tools/state.py
new file mode 100644
index 00000000..46e17f07
--- /dev/null
+++ b/games/hollowknight/tools/state.py
@@ -0,0 +1,37 @@
+"""PlayerData as the scenes see it when they load (DeactivateIfPlayerdataTrue, DeactivateIfPlayerdataFalse): the bools
+that keep the value they start with all through this part of the game turn their objects off for good here; those that
+change are left to the room as it loads (ents.py: OK_COND)."""
+
+# (as a new game has them, and as they stay up to Hornet)
+CONSTANT = {"crossroadsInfected": False, "troupeInTown": False, "clothInTown": False, "killedTraitorLord": False,
+ "clothKilled": False, "clothLeftTown": False, "brettaLeftTown": False, "zoteDefeated": False,
+ "brettaRescued": False, "visitedRuins": False, "visitedDeepnest": False, "colosseumSilverOpened": False,
+ "nymmInTown": False, "killedGrimm": False, "divineInTown": False, "fatGrubKing": False}
+DYNAMIC = {"visitedDirtmouth", "tisoEncounteredTown", "hasDash", "hornet1Defeated", "shamanPillar"}
+
+
+def conditions(o):
+ """An object's DeactivateIfPlayerdata components -> [(bool name, off if true (else off if false))]."""
+ out = []
+ for c in o["c"]:
+ cl = c.get("class") or ""
+ if cl in ("DeactivateIfPlayerdataTrue", "DeactivateIfPlayerdataFalse"):
+ out.append(((c.get("v") or {}).get("boolName"), cl.endswith("True")))
+ return out
+
+
+def apply(d):
+ """Turns off (with what is under them) the scene's objects that the constant bools turn off."""
+ by = {o["id"]: o for o in d["objects"]}
+ for o in d["objects"]:
+ for name, off_if in conditions(o):
+ if name in CONSTANT:
+ if CONSTANT[name] == off_if:
+ todo = [o["id"]]
+ while todo:
+ q = by[todo.pop()]
+ q["active"] = False
+ todo += q.get("children", [])
+ elif name not in DYNAMIC:
+ print("state: %s: %s not known" % (o["path"], name))
+ return d
diff --git a/games/hollowknight/tools/unity.py b/games/hollowknight/tools/unity.py
index ff0c0208..b17f7839 100644
--- a/games/hollowknight/tools/unity.py
+++ b/games/hollowknight/tools/unity.py
@@ -254,15 +254,16 @@ def decode_fsm(f, v):
@functools.lru_cache(maxsize=None)
def scene(name):
- """A scene as a dict (cached as JSON in tools/cache)."""
+ """A scene as a dict (cached as JSON in tools/cache), its objects that PlayerData keeps off turned off (state.py)."""
+ import state
os.makedirs(CACHE, exist_ok=True)
p = os.path.join(CACHE, name + ".json")
if os.path.exists(p):
- return json.load(open(p))
+ return state.apply(json.load(open(p)))
with in_data():
d = _dump(name)
json.dump(d, open(p, "w"), separators=(",", ":"))
- return d
+ return state.apply(d)
@functools.lru_cache(maxsize=None)