Files
isaac-port/notes/systems/rooms_use.md
T

255 lines
19 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# isaac-ng.exe (Rep+ 1.9.7.17) — how room-config (.stb) data is consumed
All addresses are VAs (ImageBase 0x400000). Struct layouts are from CONTEXT_rooms.md. One correction comes first.
## 0. Correction: weight fields
`ReadRoom` (0x82afe1..0x82aff0) writes the same float to both +0x30 and +0x34. The rest of the code shows which is which:
* **+0x30 = InitialWeight** (never written at runtime; it is the source for every reset)
* **+0x34 = Weight** (the runtime value; GetRandomRoom reads it for sums and multiplies it by 0.1 after a pick)
So CONTEXT_rooms.md has the two names swapped.
Global layout used below: `Game* g = [0xc71678]`. `g+0` is the Level (`+0 stage`, `+4 stageType`, `+0xc curses`), `g+0x18300` is `Room*`, `g+0x18910` is `RoomConfig` (embedded), and `g+0x1bbd8` is the seed-effects `std::set<int>`. `g+0x269c8` is difficulty (2/3 = greed/greedier, which gives mode 1).
`RoomConfig`: `+stageId*0xc4` is the stage (37 stages, 0..36). `+0x1e20` is `std::list<Room>*` of generated rooms (node 0x64 = next, prev, Room@+8), and `+0x1e24` is its count.
Mode resolution, used everywhere (for example 0x82c738 and 0x82ce84): `if (mode == -1) mode = (g && (g->difficulty==2 || g->difficulty==3)) ? 1 : 0;`. If `!stageMode.loaded` (+0x28), it calls LoadStageBinary first.
## 1. RNG class (0x7e8fe0 / 0x7e9020 / 0x7e9080 / 0x7e90f0)
```cpp
struct RNG { u32 seed; u32 s1, s2, s3; }; // 0x10 bytes
static const u32 kShifts[81][3] @0xb1f4c8; // listed below
void RNG::SetSeed(u32 seed, u32 idx) { // 0x7e8fe0
this->seed = seed; idx = min(idx, 80u); // (idx==0 -> 0)
s1,s2,s3 = kShifts[idx];
}
u32 RNG::Next() { // 0x7e90f0 ("RNG Seed is zero!" + int3 if seed==0)
u32 x = seed; x ^= x >> s1; x ^= x << s2; x ^= x >> s3; return seed = x;
}
u32 RNG::RandomInt(u32 n) { u32 x = Next(); return n ? x % n : 0; } // 0x7e9020
float RNG::RandomFloat() { return (float)(double)Next() * 2.3283062e-10f; } // 0x7e9080
// float conversion: cvtdq2pd + (x>>31 ? 4294967296.0 : 0) [table 0xbacb00] -> cvtpd2ps -> mulss 0x2f7ffffe
```
`kShifts` (index: s1,s2,s3):
```
0:(1,3,10) 1:(1,5,16) 2:(1,5,19) 3:(1,9,29) 4:(1,11,6) 5:(1,11,16) 6:(1,19,3) 7:(1,21,20) 8:(1,27,27)
9:(2,5,15) 10:(2,5,21) 11:(2,7,7) 12:(2,7,9) 13:(2,7,25) 14:(2,9,15) 15:(2,15,17) 16:(2,15,25) 17:(2,21,9)
18:(3,1,14) 19:(3,3,26) 20:(3,3,28) 21:(3,3,29) 22:(3,5,20) 23:(3,5,22) 24:(3,5,25) 25:(3,7,29) 26:(3,13,7)
27:(3,23,25) 28:(3,25,24) 29:(3,27,11) 30:(4,3,17) 31:(4,3,27) 32:(4,5,15) 33:(5,3,21) 34:(5,7,22) 35:(5,9,7)
36:(5,9,28) 37:(5,9,31) 38:(5,13,6) 39:(5,15,17) 40:(5,17,13) 41:(5,21,12) 42:(5,27,8) 43:(5,27,21) 44:(5,27,25)
45:(5,27,28) 46:(6,1,11) 47:(6,3,17) 48:(6,17,9) 49:(6,21,7) 50:(6,21,13) 51:(7,1,9) 52:(7,1,18) 53:(7,1,25)
54:(7,13,25) 55:(7,17,21) 56:(7,25,12) 57:(7,25,20) 58:(8,7,23) 59:(8,9,23) 60:(9,5,14) 61:(9,5,25) 62:(9,11,19)
63:(9,21,16) 64:(10,9,21) 65:(10,9,25) 66:(11,7,12) 67:(11,7,16) 68:(11,17,13) 69:(11,21,13) 70:(12,9,23)
71:(13,3,17) 72:(13,3,27) 73:(13,5,19) 74:(13,17,15) 75:(14,1,15) 76:(14,13,15) 77:(15,1,29) 78:(17,15,20)
79:(17,15,23) 80:(17,15,26)
```
Inlined RNGs often load a triplet straight from the table (`movq xmm0,[0xb1f4c8+idx*12]`). This gives `idx = (addr-0xb1f4c8)/12`.
## 2. RoomConfig::GetRandomRoom — 0x82c7d0 (thiscall, ret 0x30)
```cpp
Room* RoomConfig::GetRandomRoom(u32 seed, bool reduceWeight, int stageId, int type, int shape /*13 = any*/,
u32 minVariant, u32 maxVariant, int minDifficulty, int maxDifficulty,
const std::bitset<8>* requiredDoors, int subtype /*-1 = any*/, int mode /*-1 = auto*/)
{
// (a) Curse of the Giant (LevelCurse bit 0x80). 0x82c846..0x82c8e4
u32 curses = (g->level.curses /*g+0xc*/ | g+0x26550 | Seeds_ForcedCurses() /*0x6f9400*/) & ~Seeds_PreventedCurses() /*0x6f95a0*/;
if (curses & 0x80) {
RNG r{seed, kShifts[17]}; // (2,21,9)
if ((r.Next() & 1) == 0) {
Room* g2 = GetRandomSplitRoom(seed, reduceWeight, stageId, type, shape, minV, maxV, minD, maxD, requiredDoors, subtype, mode); // 0x82d880
if (g2) return g2;
}
}
// (b) Candidate collection. 0x82c8ec..0x82ca03
std::vector<Room*> all;
if (g->level.stage == 12 /*Void*/ && stageId != 0 && stageId != 35 /*Home*/) {
for (int s = 1; s <= 17; ++s) { if (s == 13) continue; // skip Blue Womb
auto v = CollectRooms(s, type, shape, minV, maxV, minD, /*maxD*/ 20, requiredDoors, subtype, mode);
all.insert(all.end(), v.begin(), v.end()); } // NOTE: the requested stageId is ignored; maxDifficulty is forced to 20
} else
all = CollectRooms(stageId, type, shape, minV, maxV, minD, maxD, requiredDoors, subtype, mode); // 0x82ce40
// (c) Sums. 0x82ca21..0x82caa5 (float, accumulated in vector order)
float total = 0, exactSum = 0; std::vector<Room*> exact;
for (Room* r : all) {
total += r->Weight /*+0x34*/;
if (requiredDoors->any() /*0x6da750*/ && r->doorMask /*+0x38*/ == requiredDoors->to_ulong()) {
exact.push_back(r); exactSum += r->Weight; }
}
if (!(total > 0.0f)) { // comiss 0,total ; jb => fail when 0 >= total (also when empty)
KAGE_LogMessage(1, "[warn] GetRandomRoom: unable to find matching room ... RequiredDoors %s, Subtype %d, returning NULL",
seed, stageId, type, shape, minV, maxV, minD, maxD, requiredDoors->to_string() /*0x752c30*/, subtype);
return nullptr;
}
// (d) Weighted pick. 0x82cba4..0x82cd9a
RNG rng{seed, kShifts[7]}; // (1,21,20)
u32 r1 = rng.Next(); float f1 = toFloat(r1); // RandomFloat math
float exactChance = (exactSum / total) * 9.99f; // 0x411fd70a
Room* pick = nullptr;
if (!exact.empty()) {
u32 r2 = rng.Next(); float f2 = toFloat(r2); // 2nd draw is only consumed when exact is non-empty
if (exactChance > f2) { // comiss xmm4,xmm0 ; jbe
float target = f1 * exactSum, acc = 0;
for (Room* r : exact) { acc += r->Weight; if (acc > target) { pick = r; break; } }
goto done; // NOTE: if not found -> NULL, with no fallback to `all`
}
}
{ float target = f1 * total, acc = 0;
for (Room* r : all) { acc += r->Weight; if (acc > target) { pick = r; break; } } }
done:
if (pick && reduceWeight)
pick->Weight = maxss(1e-7f /*0x33d6bf95*/, pick->Weight * 0.1f /*0x3dcccccd*/); // 0x82cd7c
return pick; // may be NULL when float rounding means acc never exceeds target
}
```
Notes:
* Every comparison is strict `acc > target` (`comiss acc,target; ja`).
* A room's weight goes into the sums as it currently is, so weights already reduced earlier in the run take part.
* "exact" means the room's door mask is exactly the requested set. CollectRooms only requires the requested doors to be a subset. This makes rooms that fit the doors exactly much more likely: they are chosen with probability `min(1, 9.99*exactSum/total)`.
* Fallback when nothing matches: it logs the warning and returns NULL. There is no retry inside the function. Callers retry, for example the wrapper below.
### CollectRooms — 0x82ce40 (thiscall, ret 0x2c, returns vector by out-ptr)
```cpp
for (u32 i = 0; i < m.count; ++i) { Room* r = &m.rooms[i]; // m = stage(stageId).modes[mode]
if (r->type != type) continue;
if (shape != 13 && r->shape /*+0x48*/ != shape) continue; // 13 = ROOMSHAPE_NUM acts as a wildcard
if (r->variant < minVariant || r->variant > maxVariant) continue; // unsigned compare (jb/ja)
if (r->difficulty < minDifficulty || r->difficulty > maxDifficulty) continue; // signed (jl/jg)
if ((r->doorMask & *requiredDoors) != *requiredDoors) continue; // must have all the required doors (superset allowed)
if (subtype != -1 && r->subtype != subtype) continue;
if (Seeds.contains(79 /*0x4f*/) && (r->flags /*+0x4c*/ & 0x80)) continue; // flag 0x80 = room contains a 4500.0 entity (pressure plate)
out.push_back(r);
}
```
The weight is not filtered here, so rooms with weight 0 are collected but can never be picked (they add 0 to the sums). The value 79 is a SeedEffect id. Its name was not checked; it is probably the "no pressure plates"-style seed.
### Wrapper 0x82cdd0 (ret 0x34, 13 stack args)
`r = GetRandomRoom(a1 seed, a2 reduce, a3 stageId, a5 type, a6 shape, 0, 0xFFFFFFFF, a9 minD, a10 maxD, a11 doors, a12 subtype, -1)`. If that returns NULL, it repeats the call with `stageId = a4` (fallback stage).
Args a7, a8 and a13 appear unused (uncertain). Called from 16 sites in Level generation (0x7392c7, 0x73a3df, ...).
### Curse of the Giant split rooms — 0x82d880 (same signature as GetRandomRoom)
This path is only taken if `type == 1` and `shape != 13`, and the split table `0xb1f494[shape]` must be non-empty. Each table entry is 32 bytes: `{shapeA, offXA, offYA, doorsA, shapeB, offXB, offYB, doorsB}`:
```
4(IH): {1,0,0,8, 1,0,1,2} 5(IIV):{3,0,0,8, 3,0,1,2} 6(1x2? per enum 6): {1,0,0,4, 1,1,0,1} 7: {2,0,0,4, 2,1,0,1}
8(2x2): {4,0,0,0x44,4,1,0,0x11} {6,0,0,0x88,6,0,1,0x22} {9,0,0,3,1,0,0,0xc} {10,0,0,0x24,1,1,0,9} {11,0,0,0x18,1,0,1,6} {12,0,0,0xc0,1,0,1,3}
9: {1,0,1,4,4,1,0,0x10} {1,1,0,8,6,0,1,0x20} 10: {4,0,0,0x40,1,1,1,1} {1,0,0,8,6,0,1,2}
11: {1,0,0,4,4,1,0,1} {6,0,0,0x80,1,1,1,2} 12: {4,0,0,4,1,1,0,1} {6,0,0,8,1,0,1,2}
```
The function works like this:
1. `RNG rng{seed, kShifts[55]}` and `e = table[rng.RandomInt(count)]`.
2. Each required door d (0..7) is remapped into each half. If `offY>0 && d∈{4,6}` or `offX>0 && d∈{5,7}`, then `d -= 4`. The door is kept only if it is valid for the half's shape (valid-door mask per shape is at 0xb1f894: `[0,0xf,5,0xa,0x5f,0xa,0xaf,5,0xff,0xff,0xff,0xff,0xff]`). The result is OR-ed into `doorsA` / `doorsB`.
3. `sA = next(rng.seed)` with (7,17,21), and `A = GetRandomRoom(sA, reduce, stageId, 1, shapeA, minV, maxV, minD, maxD, &doorsA, subtype, mode)`.
4. `sB = next(sA)`, and B is picked the same way with `shapeB`.
5. If both are found, it returns `0x82d4a0(A, B, shape)`. That function builds a merged Room and appends it to the `RoomConfig+0x1e20` generated list (count at +0x1e24). Otherwise it returns NULL.
### GetRoomByStageTypeAndVariant — 0x82c720 (thiscall, ret 0x10)
```cpp
Room* RoomConfig::GetRoomByStageTypeAndVariant(int stageId, int type, int variant, int mode /*-1 auto*/) {
// mode resolve + lazy load
for (u32 i = 0; i < m.count; ++i) if (m.rooms[i].type == type && m.rooms[i].variant == variant) return &m.rooms[i];
KAGE_LogMessage(1, "[warn] StageID %d Room type %d, variant %d not found!\n", stageId, type, variant);
return nullptr;
}
```
It does a linear scan, returns the first match and ignores subtype.
### GetStageID — 0x82d030 (ecx=levelStage, edx=stageType, [esp+4]=mode; caller cleans)
This maps LevelStage and StageType to the stb id.
* **Greed** (`mode==1`, or `mode==-1` with greed difficulty):
* stage 7 → 25 and stage 6 → 24 (`stage+18`); stage 5 → 14 (`stage+9`).
* Otherwise: type 4 → `2*stage+25`, type 5 → `2*stage+26`, else `3*stage-2+type`.
* **Normal**:
* stage 13 → 35 (Home).
* stage < 9: with `n = stage-1`, type 4 → `(n&~1)+27`, type 5 → `(n>>1)*2+28`, else `(n>>1)*3+type+1`.
* stage 9 → 13, or 36 when type 4.
* stage 12 → 26.
* Otherwise → `(stage-3)*2+type` (10 → 14/15, 11 → 16/17).
## 3. RoomConfig::Spawn::PickEntry — 0x82d290 (thiscall on Spawn, 1 float arg, ret 4)
```cpp
const Entry* Spawn::PickEntry(float r /*[0,1)*/) const {
float target = r * sumWeights; // +0xc
float acc = 0;
for (u8 i = 0; i < entryCount /*+8*/; ++i) { // u8 counter
acc += entries[i].weight; // +8 of 0xc-byte Entry
if (acc > target) return &entries[i]; // comiss; ja (strict)
}
KAGE_LogMessage(1, "[warn] RoomConfig::Spawn::PickEntry did not terminate properly. Picking first entry: (%d, %d, %d, %f)\n",
entries[0].type, entries[0].variant, entries[0].subtype, (double)entries[0].weight);
return &entries[0];
}
```
Callers always check `entryCount > 0` before calling it.
## 4. Runtime Weight (+0x34) modification and reset
* **Reduced**: only in GetRandomRoom when `reduceWeight` is true (0x82cd72..0x82cd95): `w = max(1e-7, w*0.1)`. No other writer was found in native code. Lua exposes the `Weight` / `InitialWeight` properties (strings 0xb6a14c / 0xb735c4, used by bindings near 0x86be2d / 0x86bf71); I did not check whether they are writable.
* **Reset (`Weight = InitialWeight` for every room of a stage/mode)**:
* `RoomConfig::ResetStageWeights(stageId, mode)` at **0x82a9c0**. If the stage is not loaded it calls LoadStageBinary instead, which also gives fresh weights. Otherwise it loops over the rooms.
* **Level::Init 0x744940** (log "Level::Init m_Stage %d, m_StageType %d Seed %u"), right after `ClearGeneratedRooms()` (call at 0x74510c):
* Level stage 12 (Void) resets stb 1..17 except 13, both loaded and mode-resolved (0x74511c..0x7451cb).
* Any other stage resets `GetStageID(stage, stageType, -1)` (0x7451dc..0x74526c).
* Additionally, if stageType is 4/5 and (stage==4, or stage==3 with Labyrinth curse bit 2 set), it resets stb 29 (Mines; 0x7452af..0x745362).
* Not greed: unless stage==13/the specific level-gen cases (stage 13, stage≤6 with g+0x2654c&0x10000, challenge g+0x26584==0x2c, stage 9), it calls `ResetStageWeights(0 /*special rooms*/, -1)` at 0x7453e7 and then generates (0x740e10).
* Greed: it resets stb 0 inline (0x7453f9..0x74545a) and then calls 0x743550.
* So the **special-room (stb 0) weights are reset on each normal level init**, except on Home (stage 13), Blue Womb (stage 9), challenge 0x2c, and stage≤6 with the g+0x2654c&0x10000 flag. Those go to the dedicated generators 0x74de30, 0x74a500, 0x7508c0 and 0x742ef0, and whether those reset stb 0 was not checked.
* **Home generation 0x751650**: resets or loads stb 35 (mode 0).
* **0x6fa0c0** (called from the shutdown path 0x9ab84a; probably Game::Exit/end-of-run): resets every loaded stage, 37 stages × 2 modes (0x6fa3c5..0x6fa41a).
* **Generated rooms**: `RoomConfig::ClearGeneratedRooms` at **0x82d320** logs `"delete %d generated rooms.\n"` with count `+0x1e24`. For each list node it frees every Spawn's entries (`Room+0x40`/`+0x44`) and the spawn array, destroys the name string, frees the 0x64-byte node, and resets the list and count. It is called from Level::Init (0x74510c) and from 0x7477e5. Generated rooms are created by the Giant-curse merge (0x82d4a0 → 0x82de80 list insert).
## 5. From Room spawns to entities (high level)
`Room::Init(RoomConfig::Room* cfg, RoomDescriptor* desc)` at **0x7f2800** (thiscall on `g+0x18300`, called from 0x73e8a2). Grid width is `Room+0xc` and height is `Room+0x10`. The grid-entity pointer array is `Room+0x24 + idx*4`, with indices up to 0x1bf (448 cells).
**Pass 1, pre-scan (0x7f319a..0x7f3339).**
* It logs `"SpawnRNG seed: %u"` with `desc+0x5c` (SpawnSeed), then builds an RNG from that seed with kShifts[11] (2,7,7).
* For each spawn i: `rng.Next()`, `f = toFloat`; if entryCount > 0 it calls `e = PickEntry(f)`.
* It switches on `e->type` in 825..970 (table 0x7f7928/0x7f7908) and sets room or descriptor flags.
* Type **970** (room-config meta entity) is handled by variant:
* variant 0 → `desc+0x44 |= 0x200`
* variant 1 → by subtype: 20 / 11 → local flags; 10 → `desc+0x44 |= 0x1000`; 0..3 → `Room+0x7460/0x7464 = table 0xc5d0e8[subtype]` (a direction vector)
* variant 3 → `desc+0x44 |= 0x2000`
* variant 4 → uses the grid position (0x7f342d)
* Types 825/907, 829, 876/918, 882, 905 and 919 have their own cases.
**Pass 2, real spawn (0x7f39b3..0x7f3aff).** It restarts from the same `desc+0x5c` seed, so pass 1 and pass 2 make identical picks.
```cpp
RNG rng{desc->spawnSeed, kShifts[11]};
for (u16 i = 0; i < cfg->spawnCount; ++i) {
Spawn& s = cfg->spawns[i];
u32 seed = rng.Next(); float f = toFloat(seed); // advanced even if entryCount==0
int gridIdx = (s.x + 1) + (s.y + 1) * room->width; // stb x,y are relative to the inner area (no walls)
if (s.entryCount == 0) continue;
const Entry* e = s.PickEntry(f);
if (!desc->set80.contains(gridIdx)) // std::set<int> at RoomDescriptor+0x80 (probably removed/restricted grid indices; meaning unverified)
room->SpawnEntry(gridIdx, e, seed, false, false); // 0x7ed310
}
```
**`Room::SpawnEntry(gridIdx, Entry*, seed, bool, bool)`** at **0x7ed310** (ret 0x14).
* It copies the entry, then calls 0x7ec100 (entry transform/replacement; likely where MC_PRE_ROOM_ENTITY_SPAWN and stage replacements happen; unverified).
* The per-entity seed RNG uses kShifts[35] (5,9,7).
* Position of a grid index: `x = (idx % w)*40 + 40`, `y = (idx / w)*40 + 120` (0x7ee4dc, 0x815cef).
* **type < 1000** spawns an entity via `Game::Spawn(type, variant, pos, Vector0, nullptr, subtype, seed)` (0x428b20). Special cases:
* 970 (0x3ca) spawns nothing (returns true; 0x7ed936)
* 969 (0x3c9) is special-cased (0x7ed93c)
* 999 (0x3e7), 965 (0x3c5) and 911 (0x38f) are also special-cased
* **type ≥ 1000** creates a grid entity: `CreateGridEntity(gridType, gridIdx)` (0x70b2e0), then `grid->variant = v`, then `Init(seed)` (0x709db0). The stb-to-GridEntityType mapping (0x7ede09..0x7ee64d):
* 1000..1011: jump table at 0x7ee6a0 (1000 → ROCK 2, etc.)
* 1300 → special case
* 1400..1501: table at 0x7ee700 (fire places/poops → 14 POOP with variants, `desc+0x50++`)
* 1900 → 3 (BLOCK)
* 1901..1940: table at 0x7ee790 (10, 8, 9, 24 …)
* 1999 → 15
* 3000..3009: table at 0x7ee7c8; 3000 → PIT 7, `desc+0x54++`
* 4000 → 11 (LOCK)
* 4500 → 20 (PRESSURE_PLATE)
* 5000 → 21 (STATUE) var 0; 5001 → 21 var 1
* 6000/6001 → 7 (also 0x7ed9a4)
* 6100 → 23 (TELEPORTER)
* 9000 → 17 (TRAPDOOR) var 0; 9100 → 18 (CRAWLSPACE); 10000 → 19 (GRAVITY)
* anything else → 1 (DECORATION)
* 0x815ae0 is a separate re-spawn routine (shop restock for room types 2/22). It uses the same grid-index and position math and spawns only 5.150 entries, plus a 1000.15 poof.
## Uncertainties
* The 0x82cdd0 argument meanings (a7, a8, a13 unused) were not checked against callers.
* SeedEffect 79 has not been named. `g+0x26550` is assumed to be extra curses.
* The meaning of `RoomDescriptor+0x80` and of the 0x7ec100 transform.
* 0x6fa0c0 being "end of run". Its only direct caller is in the shutdown path, so a reset between runs in the same session may happen only through Level::Init. That means non-current stages and stb 0 in some flows keep their reduced weights.
* The 0x82d4a0 merge internals (how the spawn offsets and doors are combined) were not decoded.