#include "boss_pool.h" #include "data.h" #include "game.h" #include "log.h" #include "manager.h" #include "platform.h" #include #include constexpr const char* BOSS_POOLS_XML = "bosspools.xml"; // The xorshift triples by shift index: the one stepping init's seed into each pool, and the pools' own. constexpr uint32_t POOL_SEEDER_SHIFT = 26; constexpr uint32_t POOL_RNG_SHIFT = 17; constexpr int BOSS_PICK_ATTEMPTS = 10; constexpr int BOSS_REPICKS = 9; constexpr uint32_t HORSEMAN_ODDS = 10; constexpr uint32_t HEADLESS_HORSEMAN_ODDS = 10; constexpr uint32_t FALLEN_ODDS = 10; constexpr uint32_t DOUBLE_TROUBLE_ODDS_ULTRA_HARD = 1; constexpr uint32_t DOUBLE_TROUBLE_ODDS_CHAPTER_2 = 50; constexpr uint32_t DOUBLE_TROUBLE_ODDS_CHAPTER_3 = 25; constexpr uint32_t DOUBLE_TROUBLE_ODDS_CHAPTER_4 = 40; constexpr int BOSS_MONSTRO = 1; constexpr int BOSS_MOM = 6; constexpr int BOSS_MOMS_HEART = 8; constexpr int BOSS_DEATH = 12; constexpr int BOSS_HEADLESS_HORSEMAN = 22; constexpr int BOSS_THE_FALLEN = 23; constexpr int BOSS_SATAN = 24; constexpr int BOSS_IT_LIVES = 25; constexpr int BOSS_THE_BLOAT = 30; constexpr int BOSS_CONQUEST = 38; constexpr int BOSS_ISAAC = 39; constexpr int BOSS_BLUE_BABY = 40; constexpr int BOSS_THE_LAMB = 54; constexpr int BOSS_DELIRIUM = 70; constexpr int BOSS_REAP_CREEP = 74; constexpr int BOSS_MOTHER = 88; constexpr int BOSS_MOM_MAUSOLEUM = 89; // The Horsemen of the first floor of chapters 1 to 4, by chapter. constexpr std::array HORSEMEN = {9, 10, 11, BOSS_DEATH}; // The bosses the dark bosses special daily fixes on stages 1 to 7 (none on stage 6). constexpr int SPECIAL_DAILY_DARK_BOSSES = 13; constexpr std::array SPECIAL_DAILY_BOSSES = {0, 60, 43, 50, 23, 51, 0, 15}; constexpr int ACHIEVEMENT_HARBINGERS = 5; constexpr int ACHIEVEMENT_IT_LIVES = 34; constexpr int ACHIEVEMENT_FORGOTTEN_HORSEMAN = 66; constexpr int STAGE_CHAPTER_1 = 1; constexpr int STAGE_CHAPTER_2 = 3; constexpr int STAGE_CHAPTER_2_END = 4; constexpr int STAGE_CHAPTER_3 = 5; constexpr int STAGE_MOM = 6; constexpr int STAGE_CHAPTER_4 = 7; constexpr int STAGE_MOMS_HEART = 8; constexpr int STAGE_SHEOL = 10; constexpr int STAGE_DARK_ROOM = 11; constexpr int STAGE_VOID = 12; constexpr int CHALLENGE_APRILS_FOOL = 32; constexpr int CHALLENGE_ULTRA_HARD = 34; constexpr int CHALLENGE_SCAT_MAN = 36; // A pool element's name and the stage id (StbType) whose pool it fills. struct BossPoolStage { std::string_view name; int stageId = 0; }; constexpr std::array BOSS_POOL_STAGES = {{{"basement", 1}, {"cellar", 2}, {"burning basement", 3}, {"caves", 4}, {"catacombs", 5}, {"flooded caves", 6}, {"depths", 7}, {"necropolis", 8}, {"dank depths", 9}, {"womb", 10}, {"utero", 11}, {"scarred womb", 12}, {"blue womb", 13}, {"sheol", 14}, {"cathedral", 15}, {"dark room", 16}, {"chest", 17}, {"void", 26}, {"downpour", 27}, {"dross", 28}, {"mines", 29}, {"ashpit", 30}, {"mausoleum", 31}, {"gehenna", 32}, {"corpse", 33}, {"mortis", 34}}}; // Calls visit with each attribute of an element, in document order. template static void attrs_walk(DataNode node, Visit visit) { simdjson::dom::object attrs; if (node["attrs"].get(attrs) != simdjson::SUCCESS) return; for (auto [name, value] : attrs) visit(name, value.get_string().value_unsafe()); } // Whether a boss id is one the pools track, as only those have a bit in the removed and blacklisted sets. static bool is_boss_tracked(int bossId) { return bossId >= 0 && bossId < BOSS_ID_COUNT; } // Adds a boss to a set of bosses. The original sets the bit of an untracked id past the set, here it is dropped. static void boss_mark(std::bitset& bosses, int bossId) { if (is_boss_tracked(bossId)) bosses.set(bossId); } // Whether a boss may show up in this run: Reap Creep sits out Scat Man, and a boss needs its achievement. static bool is_boss_allowed(BossPool_Entry& entry) { if (entry.id == BOSS_REAP_CREEP && g_game->challenge == CHALLENGE_SCAT_MAN) return false; return entry.achievementID < 0 || g_manager->persistentGameData.Unlocked(entry.achievementID); } // The boss a chapter's end always has, or 0. The alternate path has its own Mom and Mother. static int chapter_end_boss(int levelStage, int stageType) { bool isAltPath = stageType == STAGETYPE_REPENTANCE || stageType == STAGETYPE_REPENTANCE_B; bool isWotl = stageType == STAGETYPE_WOTL; switch (levelStage) { case STAGE_MOM: return isAltPath ? BOSS_MOM_MAUSOLEUM : BOSS_MOM; case STAGE_MOMS_HEART: if (isAltPath) return BOSS_MOTHER; return g_manager->persistentGameData.Unlocked(ACHIEVEMENT_IT_LIVES) ? BOSS_IT_LIVES : BOSS_MOMS_HEART; case STAGE_SHEOL: if (isAltPath) return BOSS_BLUE_BABY; return isWotl ? BOSS_ISAAC : BOSS_SATAN; case STAGE_DARK_ROOM: if (isAltPath) return BOSS_BLUE_BABY; return isWotl ? BOSS_BLUE_BABY : BOSS_THE_LAMB; case STAGE_VOID: return BOSS_DELIRIUM; default: return 0; } } // How rarely a stage's floor has its Double Trouble room, or 0 when it never does. Ultra Hard always does. static uint32_t double_trouble_odds(int levelStage) { if (g_game->challenge == CHALLENGE_ULTRA_HARD) return DOUBLE_TROUBLE_ODDS_ULTRA_HARD; if (levelStage == STAGE_CHAPTER_2 || levelStage == STAGE_CHAPTER_2_END) return DOUBLE_TROUBLE_ODDS_CHAPTER_2; if (levelStage == STAGE_CHAPTER_3) return DOUBLE_TROUBLE_ODDS_CHAPTER_3; if (levelStage == STAGE_CHAPTER_4) return DOUBLE_TROUBLE_ODDS_CHAPTER_4; return 0; } // 004218e0 Starts the run's pools: seeds each pool's generator from seed, forgets every boss met, reads // bosspools.xml (and the mods', which are ignored) and shuffles each pool. // The original stops in the debugger on a zero seed. Here it only logs and leaves the pools as they are. void BossPool::init(uint32_t seed) { RNG seeder{}; if (seed == 0) { log_printf(LOG_ASSERT, "RNG Seed is zero!\n"); return; } seeder.SetSeed(seed, POOL_SEEDER_SHIFT); for (BossPool_Pool& pool : pools) pool.rng.SetSeed(seeder.Next(), POOL_RNG_SHIFT); removedBosses.reset(); levelBlacklist.reset(); load_xml(BOSS_POOLS_XML, BOSS_POOL_GAME); g_manager->modManager.LoadModBossPools(); for (BossPool_Pool& pool : pools) { std::mt19937 twister(pool.rng._seed); for (size_t count = pool.entries.size(); count > 1; count--) std::swap(pool.entries[count - 1], pool.entries[twister() % count]); } } // 00421bf0 Reads a bosspools.xml: each named after a stage is emptied and refilled with its children's bosses // (id, weight, alternate room range), its Double Trouble range kept. A mod's file is ignored, as in the original. // A boss without a weight takes the one before it, from 0. The original reads that from memory it never wrote, and an // out of bounds id's achievement past the boss table. Here that id has none. void BossPool::load_xml(std::string_view path, BossPoolSource source) { Data data; float weight = 0; if (source == BOSS_POOL_MOD) return; data_load(data, path); if (!data.isLoaded || node_tag(data.root) != "bosspools") return; std::vector& bosses = g_manager->entityConfig.bosses; for (DataNode poolNode : node_children(data.root)) { std::string name; int doubleTrouble = 0; attrs_walk(poolNode, [&](std::string_view attr, std::string_view value) { if (attr == "name") name = value; else if (attr == "doubletrouble") doubleTrouble = std::atoi(std::string(value).c_str()); }); auto stage = std::ranges::find(BOSS_POOL_STAGES, std::string_view(name), &BossPoolStage::name); if (stage == BOSS_POOL_STAGES.end()) { log_printf(LOG_WARN, "Boss pool: Unknown stage '%s'\n", name.c_str()); continue; } BossPool_Pool& pool = pools[stage->stageId]; pool.name = name; pool.doubleTroubleRoomVariantStart = doubleTrouble; pool.entries.clear(); pool.totalWeight = 0; for (DataNode bossNode : node_children(poolNode)) { BossPool_Entry entry; attrs_walk(bossNode, [&](std::string_view attr, std::string_view value) { std::string text(value); if (attr == "id") entry.id = std::atoi(text.c_str()); else if (attr == "weight") weight = (float)std::atof(text.c_str()); else if (attr == "room") entry.roomVariantStart = std::atoi(text.c_str()); }); bool isKnownBoss = entry.id >= 0 && entry.id < (int)bosses.size(); if (!isKnownBoss) log_printf(LOG_ASSERT, "BossID out of bounds\n"); entry.initialWeight = weight; entry.weight = weight; entry.achievementID = isKnownBoss ? bosses[entry.id].achievement : -1; pool.entries.push_back(entry); pool.totalWeight += weight; } } } // 00422830 The boss of a stage's boss room: a challenge's, the special daily's or a chapter end's fixed boss, one of // the Horsemen (on one in ten first floors of chapters 1 to 4 once unlocked), the pool's Double Trouble room (as minus // its first room variant), The Fallen after a devil room, else a pick from the stage's pool, which ten failed picks // leave on Monstro. Each roll comes from the pool's own generator. // The original's RNG argument is never read, so it is left out. int BossPool::GetBossId(int levelStage, int stageType) { Game& game = *g_game; PersistentGameData& data = g_manager->persistentGameData; BossPool_Pool& pool = pools[RoomConfig::GetStageID(levelStage, stageType, ROOM_MODE_CURRENT)]; bool isAltPath = stageType == STAGETYPE_REPENTANCE || stageType == STAGETYPE_REPENTANCE_B; if (game.challenge == CHALLENGE_APRILS_FOOL) return BOSS_THE_BLOAT; bool isDailyStage = levelStage >= 0 && levelStage < (int)SPECIAL_DAILY_BOSSES.size(); if (game.dailyChallenge._specialDailyId == SPECIAL_DAILY_DARK_BOSSES && isDailyStage && SPECIAL_DAILY_BOSSES[levelStage] != 0) return SPECIAL_DAILY_BOSSES[levelStage]; if (int boss = chapter_end_boss(levelStage, stageType)) return boss; uint32_t conquestRoll = pool.rng.Next(); uint32_t horsemanRoll = pool.rng.Next(); uint32_t headlessRoll = pool.rng.Next(); uint32_t doubleTroubleRoll = pool.rng.Next(); uint32_t fallenRoll = pool.rng.Next(); pool.rng.Next(); pool.rng.Next(); RNG pickRng = pool.rng; bool isConquest = (conquestRoll & 1) == 0 && data.Unlocked(ACHIEVEMENT_FORGOTTEN_HORSEMAN); bool isHorsemanStage = levelStage == STAGE_CHAPTER_1 || levelStage == STAGE_CHAPTER_2 || levelStage == STAGE_CHAPTER_3 || levelStage == STAGE_CHAPTER_4; if (horsemanRoll % HORSEMAN_ODDS == 0 && data.Unlocked(ACHIEVEMENT_HARBINGERS) && isHorsemanStage && !isAltPath) { if (headlessRoll % HEADLESS_HORSEMAN_ODDS == 0 && !WasBossRemoved(BOSS_HEADLESS_HORSEMAN)) { boss_mark(levelBlacklist, BOSS_HEADLESS_HORSEMAN); return BOSS_HEADLESS_HORSEMAN; } int horseman = HORSEMEN[levelStage / 2]; if (!WasBossRemoved(horseman)) { boss_mark(levelBlacklist, horseman); if (horseman != BOSS_DEATH || !isConquest) return horseman; boss_mark(levelBlacklist, BOSS_CONQUEST); return BOSS_CONQUEST; } } uint32_t doubleTroubleOdds = double_trouble_odds(levelStage); if (pool.doubleTroubleRoomVariantStart != 0 && doubleTroubleOdds != 0 && doubleTroubleRoll % doubleTroubleOdds == 0) return -pool.doubleTroubleRoomVariantStart; if (fallenRoll % FALLEN_ODDS == 0 && !isAltPath && game.GetStateFlag(GAME_STATE_DEVILROOM_VISITED) && !WasBossRemoved(BOSS_THE_FALLEN)) { boss_mark(levelBlacklist, BOSS_THE_FALLEN); return BOSS_THE_FALLEN; } for (int attempt = 0; attempt < BOSS_PICK_ATTEMPTS; attempt++) { BossPool_Entry* entry = PickBoss(pool, pickRng.RandomFloat() * pool.totalWeight); if (!entry) { for (BossPool_Entry& met : pool.entries) if (is_boss_tracked(met.id)) { removedBosses.reset(met.id); levelBlacklist.reset(met.id); } continue; } if (!is_boss_allowed(*entry)) continue; return entry->roomVariantStart != 0 ? -entry->roomVariantStart : entry->id; } log_printf(LOG_WARN, "Failed to pick a boss room variant, defaulting to Monstro\n"); return BOSS_MONSTRO; } // The first boss after a pool's entry, around the pool, that can still be picked, or null. static BossPool_Entry* boss_after_find(BossPool& bossPool, BossPool_Pool& pool, BossPool_Entry& entry) { size_t index = &entry - pool.entries.data(); log_printf(LOG_WARN, "boss pool ran out of repicks\n"); for (size_t i = (index + 1) % pool.entries.size(); i != index; i = (i + 1) % pool.entries.size()) { BossPool_Entry& other = pool.entries[i]; if (other.weight > 0 && is_boss_allowed(other) && !bossPool.WasBossRemoved(other.id)) return &other; } return nullptr; } // 00422620 A boss of the pool by weight, roll at most the pool's total: the entry roll lands in, when it can still // win (weighted, allowed, not met this run or floor). Else roll moves through that entry's share, up to nine times, // then the next boss around the pool that can be picked does. The pick joins the floor's blacklist. Null for an empty // or weightless pool, or when nothing can be picked. BossPool_Entry* BossPool::PickBoss(BossPool_Pool& pool, float roll) { BossPool_Entry* picked = nullptr; if (pool.entries.empty() || !(pool.totalWeight > 0)) return nullptr; for (int repicks = BOSS_REPICKS; repicks >= 0 && !picked; repicks--) { float end = 0; for (BossPool_Entry& entry : pool.entries) { float start = end; end += entry.initialWeight; if (!(roll < end)) continue; bool isWinning = entry.weight > 0 && entry.weight + start > roll; if (isWinning && is_boss_allowed(entry) && !WasBossRemoved(entry.id)) picked = &entry; else if (repicks == 0) picked = boss_after_find(*this, pool, entry); else roll = (end - roll) / entry.initialWeight * pool.totalWeight; break; } } if (!picked) { log_printf(LOG_WARN, "failed to pick random boss from pool\n"); return nullptr; } boss_mark(levelBlacklist, picked->id); return picked; } // 00421720 Whether a boss was met on an earlier floor or picked for this one. An untracked id never was. bool BossPool::WasBossRemoved(int bossId) { return is_boss_tracked(bossId) && (removedBosses[bossId] || levelBlacklist[bossId]); } // 007382c0 Forgets the bosses picked for the floor, as a floor's generation starts over. void BossPool::blacklist_clear() { levelBlacklist.reset(); } // 00421b50 Counts the floor's picked bosses as met for the rest of the run. void BossPool::blacklist_commit() { removedBosses |= levelBlacklist; levelBlacklist.reset(); } // 00422f80 Saves each pool's generator seed and the bosses met. void BossPool::store_game_state(BossPoolState& state) { for (int i = 0; i < BOSS_POOL_COUNT; i++) state.rngSeeds[i] = pools[i].rng._seed; state.removedBosses = removedBosses; } // 00422ea0 Restores each pool's generator and the bosses met, with nothing picked for the floor. void BossPool::restore_game_state(const BossPoolState& state) { for (int i = 0; i < BOSS_POOL_COUNT; i++) pools[i].rng.SetSeed(state.rngSeeds[i], POOL_RNG_SHIFT); removedBosses = state.removedBosses; levelBlacklist.reset(); }