# ANM2 handling in isaac-ng.exe (Repentance+ v1.9.7.17) These notes reconstruct how the game parses `.anm2` files and how sprites play them, from the 32-bit `isaac-ng.exe` in the game root. Addresses assume ImageBase `0x400000`. The `zhl.log` addresses on disk come from an older build and don't match this one. | File | Contents | |---|---| | `anm2.hpp` / `anm2.cpp` | Reimplementation: parser, load-time frame baking, AnimationState, Sprite API | | `test.cpp` | Behaviour checks, plus an optional run against a real file | | `docs/parser_decomp.md` | Field-by-field parser decompilation, with an address for every claim | | `docs/cache_load_decomp.md` | AnmCache, the ANM2 object layout (0x114), and ctor/copy/Load/Reload | | `docs/labels_*.json` | Function names for this build, for a disassembler | Build and test: ``` g++ -std=c++17 -O2 -msse2 -mfpmath=sse -ffp-contract=off anm2.cpp test.cpp -o anm2_test ./anm2_test path/to/file.anm2 AnimName EventName ``` `-ffp-contract=off` matters: the game never fuses multiply-adds, and the interpolated values must match bit for bit. ## Pipeline 1. **Load.** `ANM2::Load` (0x40bd50) calls `AnmCache::Load` (0x40db90). - The cache key is djb2 of `mountPrefix + path`, lowercased with `\` turned into `/`. - On a cache miss, `AnmCache::LoadFile` (0x40ed50) parses the file with rapidxml. - The sprite borrows pointers into the cache entry, and entries are refcounted. 2. **Bake.** Everything is precomputed at load time: - Interpolated keyframes become one-tick frames (0x415170 for layers, 0x414e00 for nulls and root). - The RootAnimation is multiplied into every layer and null track, splitting frames at root-frame boundaries. - Nothing is interpolated at runtime, and root frames are not kept. 3. **Play.** Each sprite holds two `AnimationState`s: main at +0x30 and overlay at +0x50. - Each one keeps a float `frame` and a current keyframe index per track. - It also keeps two event bitmasks: - `eventsThisUpdate` (+0x18) answers `IsEventTriggered`. - `eventsSinceStart` (+0x1c) answers `WasEventTriggered`. ## Events and triggers - **Event table:** `` holds at most **16** names, and each id must equal its position. The sprite looks an event name up linearly and tests bit `i` of the mask. - **Triggers:** `` holds raw ints, at most 128 per animation, with no validation. - **Firing (0x408c90):** for each trigger, if `(float)AtFrame == frame` exactly, the game sets bit `EventId & 31` in **both** masks. - **`Update` (0x409030):** this is the only place frames are stepped. - It computes `nf = floor(frame + PlaybackSpeed)`. - If `nf < FrameNum`, it calls `AdvancePosition(nf)` and then stores `frame + speed`, so fractional frames are kept. - `AdvancePosition` first clears `eventsThisUpdate`, then runs `frame = floor(frame)+1` and fires triggers on every whole frame up to `nf`. So with speed > 1, no event is skipped mid-animation. - If `nf >= FrameNum` and the animation loops, it calls `Rewind()` instead. - Rewind sets the frame to 0, clears both masks and fires the frame-0 triggers. - The frames between the old position and the end are **not** stepped, so with speed > 1 an event near the end of a loop can be skipped. - If `nf >= FrameNum` and the animation doesn't loop, it calls `AdvancePosition(FrameNum-1)`, but only when that isn't behind the current frame, and then sets `playing = false`. - **Masks while stopped:** `Update` returns immediately when stopped, so the masks are left as they were. - If the update that finished the animation crossed an event frame, `IsEventTriggered` stays true from then on. - The same happens after `Stop()`. - In the normal speed-1 case the final update clears the mask, because it re-advances to the frame it is already on. - **`Play(name)` (0x40a380):** it does `SetAnimationData` (frame=-1, masks cleared, indices -1), then `Rewind`, then `playing = true`. - **Frame-0 events are already set when `Play` returns.** - With the same name and no `force`: - a non-looping animation is never restarted, even after it has finished; - a looping animation restarts only if it was stopped. - **`SetFrame(f)` (0x408e00):** clamps to `nextafterf(FrameNum, 0)` and clears only `eventsThisUpdate`. - Triggers fire only when the clamped frame exactly equals AtFrame. - Track indices are reset to 0 and moved forward to the **unclamped** f. - `Sprite:SetFrame(name, f)` also stops playback; `SetOverlayFrame(name, f)` does not. - **`NextEventTrigger` (0x40af10):** returns the nearest AtFrame strictly greater than `(int)frame` (truncated, not floored), or -1. - **Lua `Sprite:IsEventTriggered` / `WasEventTriggered`:** these only look at the main state. - **Playback speed:** `PlaybackSpeed` (+0x104) is shared by both states. A negative speed logs "cannot go backward" and fires nothing. ## Bugs in the original These are not reproduced; each one is marked `ORIGINAL:` in the code. - **`AnimationState::ChangeAnimationKeepFrame` (0x408970)** is reached through `SetAnimation(name, false)`. It has two bugs: - When the new animation has a different, non-zero null-animation count, the new null index array is stored into the *layer* slot (`mov [esi+8], ebx` at 0x408b6a). The null slot is left NULL, so the next `Update` dereferences NULL. - It also copies the *old* layer count into the new array, which overflows the heap when the new animation has fewer layers. - **Parser:** - Every limit (16/64/128/5000/256) only logs and keeps writing. - A missing required element (`Content`, `RootAnimation`, `Triggers`, …) is a NULL dereference. - `LayerId="-1"` overwrites the animation's `Loop` flag. - Frame attributes that are missing keep stale values from a reused scratch buffer, so a missing XScale can mean scale 0. - An animation with no root frames applies an uninitialised root transform. - **`GetLayerFrame` (0x40a220)** has an off-by-one bounds check (`jg`). - **`ANM2::Load`** sets `loaded` even when loading failed. - **`Sprite:Reload`** leaks one cache reference per call. ## Not covered - **Rendering:** `AnimationLayer::RenderFrame` (0x409120) only reads the current baked frame, through `layerFrameIdx`. - **Texture loading.** - **The spritesheet path resolver** (0x67f670): it was only skimmed, and `ResolveRelativePath` here approximates it.