#include "entity_pickup.h" #include "entity_bomb.h" #include "entity_effect.h" #include "entity_npc.h" #include "entity_player.h" #include "game.h" #include "item_pool.h" #include "log.h" #include "lua_engine.h" #include "manager.h" #include "npc_ai.h" #include "room.h" #include "room_grid.h" #include "room_transition.h" #include "sfx_manager.h" #include #include #include // The shift index of the RNG every outcome draw seeds (5, 9, 7), and the one a loot's spot angle comes from. constexpr int OUTCOME_SHIFT_INDEX = 35; constexpr int LOOT_ANGLE_SHIFT_INDEX = 18; constexpr int LOOT_DROP_SHIFT_INDEX = 12; constexpr float LOOT_ANGLE_SCALE = 3.14f; // The chests a player opens by touch, a key for the locked ones, and their pedestals for the items they give. constexpr std::array LOCKED_CHESTS = {PICKUP_LOCKEDCHEST, PICKUP_ETERNALCHEST, PICKUP_OLDCHEST}; constexpr std::array GOLDEN_CHESTS = {PICKUP_LOCKEDCHEST, PICKUP_BOMBCHEST, PICKUP_ETERNALCHEST, PICKUP_MEGACHEST}; constexpr int EVENT_LOCKED_CHESTS_OPENED = 198; constexpr float CHEST_VANISH_VOLUME = 0.7f; constexpr float SPIKED_CHEST_DAMAGE = 1; constexpr int SPIKED_CHEST_DAMAGE_COOLDOWN = 30; constexpr int CHEST_WAIT = 20; constexpr int ETERNAL_CHEST_RECLOSE = 60; constexpr float CHEST_PUSH_RADIUS = 40; constexpr float CHEST_PUSH_STRENGTH = 0.4f; constexpr float ENEMY_LOOT_DISTANCE = 20; constexpr float CHEST_POOF_HEIGHT = -10; constexpr float CHEST_POOF_SCALE = 0.8f; constexpr float CHEST_POOF_ALPHA = 0.3f; constexpr float CHEST_POOF_DEPTH = 4; constexpr float MEGA_CHEST_SPEED_BASE = 0.4f; constexpr float MEGA_CHEST_SPEED_STEP = 0.05f; constexpr int PICKUP_COUNT_MIN = 2; constexpr int PICKUP_COUNT_ROLL = 4; constexpr int GOLDEN_PICKUP_COUNT_ROLL = 7; constexpr int CHEST_COINS_ROLL = 3; constexpr int MEGA_CHEST_DRAWS = 5; constexpr size_t WOODEN_CHEST_TRINKETS = 2; constexpr int CHEST_TRINKET_ODDS = 100; // The pickups a chest's own draw gives (the outcome ids of GetChestLoot's first draw). enum ChestOutcome { CHEST_OUTCOME_TRINKET, CHEST_OUTCOME_CHEST, CHEST_OUTCOME_LOCKED_CHEST, CHEST_OUTCOME_PICKUPS, CHEST_OUTCOME_PILL, CHEST_OUTCOME_CARD, CHEST_OUTCOME_ITEM, CHEST_OUTCOME_GILDED_PICKUPS }; // The pickups a chest's pickups draw gives (the outcome ids of its second draw). Gilded Key adds the rarer ones. enum ChestPickup { CHEST_PICKUP_COINS, CHEST_PICKUP_HEART, CHEST_PICKUP_KEY, CHEST_PICKUP_BOMB, CHEST_PICKUP_PILL, CHEST_PICKUP_CARD, CHEST_PICKUP_RUNE, CHEST_PICKUP_TRINKET, CHEST_PICKUP_BATTERY }; // What a wooden chest gives: an item, cards or pills. enum WoodenChestOutcome { WOODEN_OUTCOME_ITEM, WOODEN_OUTCOME_CARDS, WOODEN_OUTCOME_PILLS }; // A trinket that may add a pickup to a chest: the pickup, and the shift index of the RNG it draws from. struct ChestTrinket { int trinket = 0; int variant = 0; int shiftIndex = 0; }; // The bonus trinkets in the order they roll, then the order their pickups take precedence in. constexpr std::array CHEST_TRINKETS = {{{TRINKET_ACE_OF_SPADES, PICKUP_TAROTCARD, 2}, {TRINKET_SAFETY_CAP, PICKUP_PILL, 3}, {TRINKET_CHILDS_HEART, PICKUP_HEART, 5}, {TRINKET_MATCH_STICK, PICKUP_BOMB, 4}, {TRINKET_RUSTED_KEY, PICKUP_KEY, 6}}}; constexpr std::array CHEST_TRINKET_PRECEDENCE = {0, 1, 3, 2, 4}; // The odds out of a hundred that one, two or three copies of a bonus trinket add their pickup. constexpr std::array CHEST_TRINKET_CHANCES = {33, 66, 100}; // The pedestal an item from each chest stands on, with its pay to play look, by chest variant. struct ChestPedestal { int variant = 0; int pedestal = 0; int payPedestal = 0; }; constexpr std::array CHEST_PEDESTALS = {{{PICKUP_CHEST, 10, 10}, {PICKUP_BOMBCHEST, 6, 6}, {PICKUP_SPIKEDCHEST, 7, 7}, {PICKUP_ETERNALCHEST, 8, 17}, {PICKUP_MIMICCHEST, 7, 7}, {PICKUP_OLDCHEST, 12, 18}, {PICKUP_WOODENCHEST, 13, 13}, {PICKUP_MEGACHEST, 14, 19}, {PICKUP_LOCKEDCHEST, 4, 16}, {PICKUP_REDCHEST, 5, 5}, {PICKUP_MOMSCHEST, 11, 11}}}; // The draws GetChestLoot makes: the chest's RNG (or a copy that leaves it be), how items leave their pool, and the // bonus trinkets' RNGs and whether each added its pickup. struct ChestDraw { std::array trinketRngs{}; std::array isTrinketProc{}; LootList loot; RNG daemonsTailRng{}; RNG* rng = nullptr; Entity_Player* player = nullptr; uint32_t poolFlags = 0; bool isDaemonsTail = false; bool isPokerChip = false; bool isPokerDouble = false; }; // The outcomes of an old chest, a red chest and a sack. A sack's outcome is its pickup's subtype and variant. enum OldChestOutcome { OLD_OUTCOME_SOUL_HEARTS, OLD_OUTCOME_TRINKETS, OLD_OUTCOME_ITEM, OLD_OUTCOME_ANGEL_ITEM }; enum RedChestOutcome { RED_OUTCOME_ITEM, RED_OUTCOME_TELEPORT, RED_OUTCOME_SPIDERS, RED_OUTCOME_TROLL_BOMB, RED_OUTCOME_FLIES, RED_OUTCOME_BLUE_SPIDERS, RED_OUTCOME_SOUL_HEARTS, RED_OUTCOME_BOMBS, RED_OUTCOME_PILLS }; constexpr int SACK_OUTCOME_SHIFT = 16; constexpr int SACK_OUTCOME_MASK = 0xffff; constexpr int SACK_BLACK = 2; constexpr int SACK_BLACK_HEART = HEART_BLACK << SACK_OUTCOME_SHIFT | PICKUP_HEART; constexpr int SACK_BONE_HEART = HEART_BONE << SACK_OUTCOME_SHIFT | PICKUP_HEART; // What an old, red or Mom's chest gives beyond its outcomes: three trinkets, Left Hand's extra pickup (from two // copies) or its spiders' ban (from three), the Red Key until Mom's chests' items are unlocked, the familiars and // entities a red chest lets out, and the Poker Chip's banned item qualities. constexpr int OLD_CHEST_TRINKETS = 3; constexpr int OLD_CHEST_SOUL_HEARTS = 3; constexpr int RED_CHEST_FAMILIARS = 3; constexpr int LEFT_HAND_EXTRA = 2; constexpr int LEFT_HAND_NO_SPIDERS = 3; constexpr int FAMILIAR_BLUE_FLY = 43; constexpr int FAMILIAR_BLUE_SPIDER = 73; constexpr int RED_CHEST_TELEPORT = 1; constexpr float RED_CHEST_SPIDER_STEP = 70; constexpr float RED_CHEST_SPIDER_HEIGHT = -30; constexpr uint32_t POKER_QUALITY_BANS = 0x1f0; constexpr int POKER_TOP_QUALITY = 2; constexpr std::array DAEMONS_TAIL_SHIFTS = {1, 5, 16}; constexpr std::array POKER_CHIP_SHIFTS = {1, 21, 20}; // Adds weight to an outcome, never below zero, or adds the outcome (inlined in the original). void WeightedOutcomePicker::AddOutcomeWeight(int id, int weight) { for (Outcome& outcome : outcomes) if (outcome.id == id) { outcome.weight = std::max(0, outcome.weight + weight); return; } outcomes.push_back({id, weight}); } // 0049cbf0 Draws an outcome: a roll below the total weight lands in one outcome's share, the first with no weight. int WeightedOutcomePicker::PickOutcome(RNG rng) { uint32_t total = 0; int sum = 0; for (Outcome& outcome : outcomes) total += outcome.weight; uint32_t roll = rng.Next(); roll = total ? roll % total : 0; for (Outcome& outcome : outcomes) if ((int)roll < (sum += outcome.weight)) return outcome.id; if (sum <= 0) log_printf(LOG_ASSERT, "No weights were added. At least one outcome needs weight."); return 0; } // Draws an outcome from a picker, seeded by the next draw of an RNG. static int outcome_draw(WeightedOutcomePicker& picker, RNG& rng) { RNG draw; draw.SetSeed(rng.Next(), OUTCOME_SHIFT_INDEX); return picker.PickOutcome(draw); } // Adds a pickup to the loot, seeded by the next draw. static void loot_add(ChestDraw& draw, int variant, int subType = 0) { uint32_t seed = draw.rng->Next(); draw.loot.entries.push_back({.subType = subType, .type = ENTITY_PICKUP, .variant = variant, .seed = seed}); } // Adds an item from a pool to the loot: its seed first, then the pool's draw. static void loot_item_add(ChestDraw& draw, int pool) { uint32_t seed = draw.rng->Next(); int item = g_game->itemPool.GetCollectible((ItemPoolType)pool, draw.rng->Next(), draw.poolFlags, 0, 0); draw.loot.entries.push_back({.subType = item, .type = ENTITY_PICKUP, .variant = PICKUP_COLLECTIBLE, .seed = seed}); } // The Poker Chip's item (FUN_006ee640): a first draw (left in the pool) shows a quality, and the item drawn is of the // next quality up (a quality 3 or 4 its own), the first when none is. static void loot_poker_item_add(ChestDraw& draw, int pool, int fallbackPool) { uint32_t seed = draw.rng->Next(); ItemPool& itemPool = g_game->itemPool; int first = itemPool.GetCollectible((ItemPoolType)pool, draw.rng->Next(), draw.poolFlags | POOL_FLAG_NO_DECREASE, 0, 0); int quality = g_manager->itemConfig.GetCollectible(first)->quality; int target = quality > POKER_TOP_QUALITY ? quality : quality + 1; uint32_t flags = ~(POOL_FLAG_BAN_QUALITY_0 << target) & (draw.poolFlags | POKER_QUALITY_BANS); int item = itemPool.GetCollectible((ItemPoolType)pool, draw.rng->Next(), flags, 0, fallbackPool); if (item == COLLECTIBLE_BREAKFAST) item = first; draw.loot.entries.push_back({.subType = item, .type = ENTITY_PICKUP, .variant = PICKUP_COLLECTIBLE, .seed = seed}); } // The Poker Chip doubles a chest's pickups, or half the time trades them for an attack fly. static int poker_chip_count(ChestDraw& draw, int count) { if (!draw.isPokerChip) return count; if (draw.isPokerDouble && count != 0) return count * 2; draw.loot.entries.push_back({.type = ENTITY_ATTACKFLY, .seed = draw.rng->Next()}); return 0; } // Rolls each bonus trinket for the first player holding it (a Lazarus twin's own copy counts for globally shared // ones): one in five passes Child's Heart under Daemon's Tail, then one, two or three copies add their pickup a // third, two thirds or every time. static void chest_trinkets_roll(ChestDraw& draw) { PlayerManager& players = g_game->playerManager; for (size_t index = 0; index < CHEST_TRINKETS.size(); index++) { int trinket = CHEST_TRINKETS[index].trinket; ItemConfig_Item* config = g_manager->itemConfig.GetTrinket(trinket); bool isShared = config && config->HasTags(TAG_LAZ_SHARED_GLOBAL); auto isHolder = [&](Entity_Player* player) { return player->variant == PLAYER_VARIANT_PLAYER && (player->HasTrinket(trinket) || (isShared && player->backupPlayer && player->backupPlayer->HasTrinket(trinket))); }; if (std::ranges::none_of(players.playerList, isHolder)) continue; if (trinket == TRINKET_CHILDS_HEART && draw.isDaemonsTail && draw.rng->Next() % DAEMONS_TAIL_ODDS != 0) continue; int copies = std::clamp(players.GetTrinketMultiplier(trinket), 1, (int)CHEST_TRINKET_CHANCES.size()); draw.isTrinketProc[index] = (int)(draw.rng->Next() % CHEST_TRINKET_ODDS) < CHEST_TRINKET_CHANCES[copies - 1]; } } // Adds the pickup of the bonus trinket that takes precedence, from its own RNG. Wooden chests only know the first // two. static void chest_trinket_pickup_add(ChestDraw& draw, size_t precedenceCount) { for (size_t rank = 0; rank < precedenceCount; rank++) { int index = CHEST_TRINKET_PRECEDENCE[rank]; if (!draw.isTrinketProc[index]) continue; uint32_t seed = draw.trinketRngs[index].Next(); draw.loot.entries.push_back({.type = ENTITY_PICKUP, .variant = CHEST_TRINKETS[index].variant, .seed = seed}); return; } } // A chest's count of pickups grown by Lucky Toe two times in three and doubled by Mom's Key. static int chest_pickup_count_grow(ChestDraw& draw, int count) { PlayerManager& players = g_game->playerManager; if (draw.rng->RandomInt(LUCKY_TOE_ODDS) != 0 && players.AnyoneHasTrinket(TRINKET_LUCKY_TOE)) count++; if (players.FirstCollectibleOwner(COLLECTIBLE_MOMS_KEY, LAZARUS_SHARE_CHECK)) count *= 2; return count; } // The pickups a chest's pickups outcome gives: coins (one to three), hearts, keys and bombs, or with Gilded Key also // pills, cards, runes, trinkets and batteries. static void chest_pickups_add(ChestDraw& draw, int count, bool isGilded) { WeightedOutcomePicker picker; int heartWeight = draw.isDaemonsTail ? 4 : 20; if (isGilded) picker.outcomes = {{CHEST_PICKUP_COINS, 25}, {CHEST_PICKUP_HEART, heartWeight}, {CHEST_PICKUP_KEY, 10}, {CHEST_PICKUP_BOMB, 20}, {CHEST_PICKUP_PILL, 5}, {CHEST_PICKUP_CARD, 5}, {CHEST_PICKUP_RUNE, 5}, {CHEST_PICKUP_TRINKET, 5}, {CHEST_PICKUP_BATTERY, 5}}; else picker.outcomes = {{CHEST_PICKUP_COINS, 35}, {CHEST_PICKUP_HEART, heartWeight}, {CHEST_PICKUP_KEY, 15}, {CHEST_PICKUP_BOMB, 30}}; constexpr std::array PICKUP_VARIANTS = {PICKUP_COIN, PICKUP_HEART, PICKUP_KEY, PICKUP_BOMB, PICKUP_PILL, PICKUP_TAROTCARD, PICKUP_TAROTCARD, PICKUP_TRINKET, PICKUP_LIL_BATTERY}; for (; count > 0; count--) { int outcome = outcome_draw(picker, *draw.rng); if (outcome == CHEST_PICKUP_COINS) { for (int coins = draw.rng->RandomInt(CHEST_COINS_ROLL) + 1; coins > 0; coins--) loot_add(draw, PICKUP_COIN); continue; } if (outcome != CHEST_PICKUP_RUNE) { loot_add(draw, PICKUP_VARIANTS[outcome]); continue; } uint32_t seed = draw.rng->Next(); uint32_t cardSeed = draw.rng->Next(); int card = ItemPool::GetCardEx((int)cardSeed, 0, 1, 0, true); RNG cardRng; cardRng.SetSeed(cardSeed, OUTCOME_SHIFT_INDEX); card = lua_engine_get().GetCard({.rng = &cardRng, .card = card, .isOnlyRunes = true, .isRunes = true}); draw.loot.entries.push_back( {.subType = card, .type = ENTITY_PICKUP, .variant = PICKUP_TAROTCARD, .seed = seed}); } } // An ordinary or golden chest's loot: one draw (five for a Mega Chest) among a trinket, a chest, a locked chest, a // pill or a card, an item from the golden chest pool and the pickups (two to four, or two to six from golden chests), // Gilded Key turning a golden chest's pickups richer. A Mega Chest gives two treasure items half the time. static void chest_loot_draw(ChestDraw& draw, int variant) { bool isMega = variant == PICKUP_MEGACHEST; bool isGolden = std::ranges::count(GOLDEN_CHESTS, variant) > 0; if (isMega && draw.rng->RandomInt(2) == 0) { loot_item_add(draw, POOL_TREASURE); loot_item_add(draw, POOL_TREASURE); return; } WeightedOutcomePicker picker; if (isGolden && g_game->playerManager.AnyoneHasTrinket(TRINKET_GILDED_KEY)) { picker.AddOutcomeWeight(CHEST_OUTCOME_GILDED_PICKUPS, 80); if (!isMega) picker.AddOutcomeWeight(CHEST_OUTCOME_ITEM, 20); } else { picker.AddOutcomeWeight(CHEST_OUTCOME_TRINKET, 10); if (!isMega) picker.AddOutcomeWeight(CHEST_OUTCOME_CHEST, 1); if (!isMega) picker.AddOutcomeWeight(CHEST_OUTCOME_LOCKED_CHEST, 1); picker.AddOutcomeWeight(isGolden ? CHEST_OUTCOME_CARD : CHEST_OUTCOME_PILL, 10); if (isGolden && !isMega) picker.AddOutcomeWeight(CHEST_OUTCOME_ITEM, 20); picker.AddOutcomeWeight(CHEST_OUTCOME_PICKUPS, isGolden ? 58 : 78); } for (int draws = isMega ? MEGA_CHEST_DRAWS : 1; draws > 0; draws--) { int outcome = outcome_draw(picker, *draw.rng); switch (outcome) { case CHEST_OUTCOME_TRINKET: loot_add(draw, PICKUP_TRINKET); break; case CHEST_OUTCOME_CHEST: loot_add(draw, PICKUP_CHEST); break; case CHEST_OUTCOME_LOCKED_CHEST: loot_add(draw, PICKUP_LOCKEDCHEST); break; case CHEST_OUTCOME_PILL: loot_add(draw, PICKUP_PILL); break; case CHEST_OUTCOME_CARD: loot_add(draw, PICKUP_TAROTCARD); break; case CHEST_OUTCOME_ITEM: if (!draw.isPokerChip) loot_item_add(draw, POOL_GOLDEN_CHEST); else loot_poker_item_add(draw, POOL_GOLDEN_CHEST, variant == PICKUP_ETERNALCHEST ? POOL_ANGEL : 0); break; default: { int roll = isGolden ? GOLDEN_PICKUP_COUNT_ROLL : PICKUP_COUNT_ROLL; int count = std::max(PICKUP_COUNT_MIN, (int)draw.rng->RandomInt(roll)); chest_trinket_pickup_add(draw, CHEST_TRINKET_PRECEDENCE.size()); count = poker_chip_count(draw, chest_pickup_count_grow(draw, count)); chest_pickups_add(draw, count, outcome == CHEST_OUTCOME_GILDED_PICKUPS); } } } } // A wooden chest's loot: an item from the wooden chest pool, or two or three cards or pills. A first draw only // decides whether Lucky Toe may add one, and Ace of Spades or Safety Cap add their pickup. static void wooden_chest_loot_draw(ChestDraw& draw) { WeightedOutcomePicker picker; picker.outcomes = {{WOODEN_OUTCOME_ITEM, 2}, {WOODEN_OUTCOME_CARDS, 10}, {WOODEN_OUTCOME_PILLS, 10}}; int first = outcome_draw(picker, *draw.rng); int count = (draw.rng->Next() & 1) + PICKUP_COUNT_MIN; if (first != WOODEN_OUTCOME_ITEM && draw.rng->Next() % LUCKY_TOE_ODDS != 0 && g_game->playerManager.AnyoneHasTrinket(TRINKET_LUCKY_TOE)) count++; chest_trinket_pickup_add(draw, WOODEN_CHEST_TRINKETS); if (g_game->playerManager.FirstCollectibleOwner(COLLECTIBLE_MOMS_KEY, LAZARUS_SHARE_CHECK)) count *= 2; if (draw.isPokerChip && poker_chip_count(draw, count) == 0) return; if (draw.isPokerChip) count *= 2; int outcome = outcome_draw(picker, *draw.rng); if (outcome == WOODEN_OUTCOME_ITEM && draw.isPokerChip) return loot_poker_item_add(draw, POOL_WOODEN_CHEST, 0); if (outcome == WOODEN_OUTCOME_ITEM) return loot_item_add(draw, POOL_WOODEN_CHEST); for (; count > 0; count--) loot_add(draw, outcome == WOODEN_OUTCOME_CARDS ? PICKUP_TAROTCARD : PICKUP_PILL); } // An old chest's loot: soul hearts, three trinkets, an old chest's item, or rarely an angel room's. static void old_chest_loot_draw(ChestDraw& draw) { WeightedOutcomePicker picker; picker.outcomes = {{OLD_OUTCOME_SOUL_HEARTS, draw.isDaemonsTail ? 9 : 43}, {OLD_OUTCOME_TRINKETS, 42}, {OLD_OUTCOME_ITEM, 10}, {OLD_OUTCOME_ANGEL_ITEM, 5}}; switch (outcome_draw(picker, *draw.rng)) { case OLD_OUTCOME_SOUL_HEARTS: for (int i = (int)draw.rng->RandomInt(OLD_CHEST_SOUL_HEARTS) + 1; i > 0; i--) loot_add(draw, PICKUP_HEART, HEART_SOUL); break; case OLD_OUTCOME_TRINKETS: for (int i = 0; i < OLD_CHEST_TRINKETS; i++) loot_add(draw, PICKUP_TRINKET); break; case OLD_OUTCOME_ITEM: loot_item_add(draw, POOL_OLD_CHEST); break; case OLD_OUTCOME_ANGEL_ITEM: loot_item_add(draw, POOL_ANGEL); break; } } // Adds an entity that is no pickup to a chest's loot, seeded by the next draw. static void loot_entity_add(ChestDraw& draw, int type, int variant) { draw.loot.entries.push_back({.type = type, .variant = variant, .seed = draw.rng->Next()}); } // A red chest's loot: a devil item on the Chest's first room, else a red chest's item, a teleport to the devil room // (not from the devil room or Genesis), spiders (unless three Left Hands), a Mega Troll Bomb, blue flies or spiders, // soul hearts, troll bombs or pills. Two Left Hands add a pill or a soul heart. static void red_chest_loot_draw(ChestDraw& draw, bool isChestFloor) { Game& game = *g_game; if (isChestFloor && game.startingRoomIdx == game.currentRoomIdx) { uint32_t seed = draw.rng->Next(); int item = game.itemPool.GetCollectible(POOL_DEVIL, draw.rng->Next(), draw.poolFlags | POOL_FLAG_BAN_DEVIL_SACRIFICE, 0, 0); draw.loot.entries.push_back( {.subType = item, .type = ENTITY_PICKUP, .variant = PICKUP_COLLECTIBLE, .seed = seed}); return; } int leftHands = draw.player ? draw.player->GetTrinketMultiplier(TRINKET_LEFT_HAND) : 0; WeightedOutcomePicker picker; picker.outcomes = {{RED_OUTCOME_ITEM, 10}}; if (game.currentRoomIdx != ROOM_DEVIL_IDX && game.currentRoomIdx != ROOM_GENESIS_IDX) picker.AddOutcomeWeight(RED_OUTCOME_TELEPORT, 5); if (!draw.player || leftHands < LEFT_HAND_NO_SPIDERS) picker.AddOutcomeWeight(RED_OUTCOME_SPIDERS, 13); picker.AddOutcomeWeight(RED_OUTCOME_TROLL_BOMB, 13); picker.AddOutcomeWeight(RED_OUTCOME_FLIES, 7); picker.AddOutcomeWeight(RED_OUTCOME_BLUE_SPIDERS, 7); picker.AddOutcomeWeight(RED_OUTCOME_SOUL_HEARTS, draw.isDaemonsTail ? 3 : 15); picker.AddOutcomeWeight(RED_OUTCOME_BOMBS, 15); picker.AddOutcomeWeight(RED_OUTCOME_PILLS, 15); int outcome = outcome_draw(picker, *draw.rng); switch (outcome) { case RED_OUTCOME_ITEM: loot_item_add(draw, POOL_RED_CHEST); break; case RED_OUTCOME_TELEPORT: draw.loot.entries.push_back({.variant = RED_CHEST_TELEPORT}); break; case RED_OUTCOME_SPIDERS: loot_entity_add(draw, ENTITY_SPIDER, 0); loot_entity_add(draw, ENTITY_SPIDER, 0); break; case RED_OUTCOME_TROLL_BOMB: loot_entity_add(draw, ENTITY_BOMB, BOMB_SUPERTROLL); break; case RED_OUTCOME_FLIES: case RED_OUTCOME_BLUE_SPIDERS: for (int i = 0; i < RED_CHEST_FAMILIARS; i++) loot_entity_add(draw, ENTITY_FAMILIAR, outcome == RED_OUTCOME_FLIES ? FAMILIAR_BLUE_FLY : FAMILIAR_BLUE_SPIDER); break; case RED_OUTCOME_SOUL_HEARTS: { loot_add(draw, PICKUP_HEART, HEART_SOUL); bool isSecond = draw.rng->RandomInt(2) == 0 && (!draw.isDaemonsTail || draw.daemonsTailRng.RandomInt(DAEMONS_TAIL_ODDS) == 0); if (isSecond) loot_add(draw, PICKUP_HEART, HEART_SOUL); break; } case RED_OUTCOME_BOMBS: loot_add(draw, PICKUP_BOMB, BOMB_SUBTYPE_TROLL); loot_add(draw, PICKUP_BOMB, BOMB_SUBTYPE_TROLL); break; case RED_OUTCOME_PILLS: loot_add(draw, PICKUP_PILL); loot_add(draw, PICKUP_PILL); break; } if (!draw.player || leftHands < LEFT_HAND_EXTRA) return; bool isPill = (draw.rng->Next() & 1) == 0; loot_add(draw, isPill ? PICKUP_PILL : PICKUP_HEART, HEART_SOUL); } // A sack's loot: two or three pickups, bombs, keys, coins, batteries or cards, or from a black sack bombs, pills and a // black or bone heart (one heart at most). static void sack_loot_draw(ChestDraw& draw, int subtype) { WeightedOutcomePicker picker; if (subtype == SACK_BLACK) picker.outcomes = {{PICKUP_BOMB, 15}, {SACK_BLACK_HEART, 10}, {SACK_BONE_HEART, 5}, {PICKUP_PILL, 15}}; else picker.outcomes = { {PICKUP_BOMB, 15}, {PICKUP_KEY, 20}, {PICKUP_COIN, 15}, {PICKUP_LIL_BATTERY, 4}, {PICKUP_TAROTCARD, 4}}; for (int count = (int)draw.rng->RandomInt(2) + 2; count > 0; count--) { int outcome = outcome_draw(picker, *draw.rng); int variant = outcome & SACK_OUTCOME_MASK; draw.rng->Next(); loot_add(draw, variant, outcome >> SACK_OUTCOME_SHIFT); if (subtype != SACK_BLACK || variant != PICKUP_HEART) continue; picker.AddOutcomeWeight(SACK_BLACK_HEART, -10); picker.AddOutcomeWeight(SACK_BONE_HEART, -5); } } // Mom's chest's loot: a Mom's chest item, the Red Key until those are unlocked (outside a daily or debug run). static void moms_chest_loot_draw(ChestDraw& draw) { bool isLocked = !g_manager->persistentGameData.achievements[ACHIEVEMENT_RED_KEY] && (g_manager->state != MANAGER_STATE_GAME || (g_game->dailyChallenge.id == 0 && !g_game->isDebug)); if (!isLocked) return loot_item_add(draw, POOL_MOMS_CHEST); uint32_t seed = draw.rng->Next(); draw.loot.entries.push_back( {.subType = COLLECTIBLE_RED_KEY, .type = ENTITY_PICKUP, .variant = PICKUP_COLLECTIBLE, .seed = seed}); } // 006dae40 What the chest gives, drawn from its RNG, or from a copy with POOL_DRAW_PEEK, which also leaves the items // in their pools. An eternal chest gives nothing a quarter of the time (and stays). Ace of Spades, Safety Cap, // Match Stick, Child's Heart and Rusted Key may add their pickups, and in the Chest every chest gives a treasure item. // Old chests give hearts, trinkets or items, red chests also teleports and enemies, Mom's chests their item, sacks // pickups, and the Poker Chip doubles a chest's pickups or trades them for an attack fly, and raises its items' // quality. LootList Entity_Pickup::GetChestLoot(PoolDraw draw, Entity_Player* player) { ChestDraw chest; RNG peek = dropRNG; chest.rng = draw == POOL_DRAW_TAKE ? &dropRNG : &peek; chest.player = player; chest.poolFlags = draw == POOL_DRAW_TAKE ? 0 : POOL_FLAG_NO_DECREASE; chest.isDaemonsTail = g_game->playerManager.AnyoneHasTrinket(TRINKET_DAEMONS_TAIL); chest.daemonsTailRng = {dropRNG._seed, DAEMONS_TAIL_SHIFTS[0], DAEMONS_TAIL_SHIFTS[1], DAEMONS_TAIL_SHIFTS[2]}; for (size_t index = 0; index < CHEST_TRINKETS.size(); index++) chest.trinketRngs[index].SetSeed(dropRNG._seed, CHEST_TRINKETS[index].shiftIndex); Game& game = *g_game; if (variant == PICKUP_ETERNALCHEST && game.stage != STAGE_DARK_ROOM && subtype < 2 && (chest.rng->Next() & 3) == 0) return chest.loot; chest_trinkets_roll(chest); bool isDeathmatch = game.deathmatchManager.is_active(); bool isChestFloor = !game.IsGreedMode() && game.stage == STAGE_DARK_ROOM && !isDeathmatch; bool isPokerChip = isDeathmatch ? player && player->HasTrinket(TRINKET_POKER_CHIP) : game.playerManager.AnyoneHasTrinket(TRINKET_POKER_CHIP); if (isPokerChip) { RNG poker = {chest.rng->_seed, POKER_CHIP_SHIFTS[0], POKER_CHIP_SHIFTS[1], POKER_CHIP_SHIFTS[2]}; chest.isPokerChip = true; chest.isPokerDouble = (poker.Next() & 1) == 0; } if (variant == PICKUP_WOODENCHEST) wooden_chest_loot_draw(chest); else if (variant == PICKUP_REDCHEST) red_chest_loot_draw(chest, isChestFloor); else if (variant == PICKUP_MOMSCHEST) moms_chest_loot_draw(chest); else if (variant == PICKUP_OLDCHEST) old_chest_loot_draw(chest); else if (variant == PICKUP_GRAB_BAG) sack_loot_draw(chest, subtype); else if (!isChestFloor) chest_loot_draw(chest, variant); else { loot_item_add(chest, POOL_TREASURE); if (variant == PICKUP_MEGACHEST) loot_item_add(chest, POOL_TREASURE); } return chest.loot; } // The poof a chest opens in, small and faint over its lid. static void chest_poof_spawn(Entity_Pickup& chest) { Entity* poof = g_game->Spawn( {.position = chest.pos, .subType = 2, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()}); poof->posOffset = vector_make(chest.sprite.scale.x * 0, chest.sprite.scale.x * CHEST_POOF_HEIGHT); poof->sprite.scale = vector_make(chest.sprite.scale.x * CHEST_POOF_SCALE, chest.sprite.scale.y * CHEST_POOF_SCALE); poof->sprite.color = {{1, 1, 1, CHEST_POOF_ALPHA}, {0, 0, 0, 0}, {0, 0, 0}}; poof->renderDepthOffset = CHEST_POOF_DEPTH; } // The deathmatch's chests vanish once open, in a poof. static void chest_vanish(Entity_Pickup& chest) { g_game->Spawn({.position = chest.pos, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()}); int sound = SOUND_SUMMONSOUND; seed_fart_sound_override(&sound); g_manager->sfxManager.Play(sound, {.volume = CHEST_VANISH_VOLUME}); chest.Remove(); } // 006e6e40 Opens the chest for a player (the nearest when none): it plays Open with its sounds and a faint poof, // gives its loot and stays open. An eternal chest that gives nothing closes again. An item turns the chest into its // pedestal, and a pickup or enemy coming out pushes a close player away. Whether a closed chest opened. bool Entity_Pickup::TryOpenChest(Entity_Player* player) { if (!player) player = game_nearest_player(pos); if (subtype == 0) return false; sprite.Play("Open", PLAY_FORCED); sound_play(SOUND_CHEST_OPEN); if (std::ranges::count(LOCKED_CHESTS, variant) > 0) { sound_play(SOUND_UNLOCK); g_manager->persistentGameData.IncreaseEventCounter(EVENT_LOCKED_CHESTS_OPENED, 1); } chest_poof_spawn(*this); int chestVariant = variant; LootList loot = GetChestLoot(POOL_DRAW_TAKE, player); subtype = 0; Game& game = *g_game; if (chestVariant == PICKUP_ETERNALCHEST && loot.entries.empty()) { chestReCloseTimer = 0; game.room->isPickupVisionInvalid = true; if (game.deathmatchManager.is_active()) chest_vanish(*this); return true; } chest_loot_spawn(loot, *player, chestVariant); if (variant == PICKUP_ETERNALCHEST) chestReCloseTimer = ETERNAL_CHEST_RECLOSE; if (game.deathmatchManager.is_active() && IsChest(variant) && variant != PICKUP_ETERNALCHEST) chest_vanish(*this); game.room->isPickupVisionInvalid = true; return true; } // What a red chest lets out that is no ordinary spawn: a teleport to the devil room, spiders (the first thrown halfway // to the player), blue flies and blue spiders. Whether the entry was spawned here. static bool red_chest_entry_spawn(Entity_Pickup& chest, const LootEntry& entry, Entity_Player& player, bool isFirst) { if (entry.type == 0 && entry.variant == RED_CHEST_TELEPORT) { Game& game = *g_game; game.InitializeDevilAngelRoom(false, false); game.leaveDoor = -1; game.StartRoomTransition({.player = &player, .animation = TRANSITION_TELEPORT, .roomIndex = ROOM_DEVIL_IDX}); return true; } if (entry.type == ENTITY_SPIDER) { Vector target{}; if (isFirst) { // The player's NPC target fields (0x144c, 0x1450) are not known yet and taken as empty. Entity* aim = player.darkArtsLastSnare ? player.darkArtsLastSnare->GetLastParent() : &player; Vector halfway = vector_scale(vector_add(aim->pos, chest.pos), 0.5f); target = room_free_pickup_position_find({.position = halfway, .step = RED_CHEST_SPIDER_STEP}); } Entity_NPC::ThrowSpider(chest.pos, &chest, target, false, RED_CHEST_SPIDER_HEIGHT); return true; } if (entry.type != ENTITY_FAMILIAR) return false; if (entry.variant == FAMILIAR_BLUE_FLY) player.AddBlueFlies(1, chest.pos, nullptr); else if (entry.variant == FAMILIAR_BLUE_SPIDER) player.ThrowBlueSpider(chest.pos, {}); return true; } // Spawns what a chest gives: the first item turns the chest into its pedestal (a pay to play look for those) and // more stand beside it, pickups and enemies fly out from a spot beside the chest (a Mega Chest's slower at first), // enemies placed towards the player, and chests come out smaller. Red chests' spiders, flies and teleports stop the // game until reached. void Entity_Pickup::chest_loot_spawn(const LootList& loot, Entity_Player& player, int chestVariant) { RNG dropRng; dropRng.SetSeed(dropRNG._seed, LOOT_DROP_SHIFT_INDEX); bool isPushing = false; int spawned = 0; for (const LootEntry& entry : loot.entries) { if (entry.type == ENTITY_PICKUP && entry.variant == PICKUP_COLLECTIBLE) { Entity_Pickup* item = this; if (variant == PICKUP_COLLECTIBLE) item = static_cast(g_game->Spawn({.position = pos, .subType = entry.subType, .type = ENTITY_PICKUP, .variant = PICKUP_COLLECTIBLE, .seed = entry.seed})); else Morph(ENTITY_PICKUP, PICKUP_COLLECTIBLE, entry.subType, false, false, false); if (item->variant == PICKUP_COLLECTIBLE) { item->targetPosition = pos; for (const ChestPedestal& pedestal : CHEST_PEDESTALS) if (pedestal.variant == chestVariant) item->SetAlternatePedestal(isPayToPlay ? pedestal.payPedestal : pedestal.pedestal); } isPushing = true; continue; } if (entry.type != ENTITY_PICKUP && red_chest_entry_spawn(*this, entry, player, &entry == &loot.entries.front())) continue; float speed = variant == PICKUP_MEGACHEST ? std::min(1.0f, (float)spawned * MEGA_CHEST_SPEED_STEP + MEGA_CHEST_SPEED_BASE) : 1; Vector velocity = GetRandomPickupVelocity(pos, &dropRng, 0); RNG angleRng; angleRng.SetSeed(dropRng.Next(), LOOT_ANGLE_SHIFT_INDEX); float angle = angleRng.RandomFloat() * LOOT_ANGLE_SCALE * 2; Entity* out = g_game->Spawn({.position = vector_make(std::cos(angle) + pos.x, std::sin(angle) + pos.y), .velocity = vector_make(velocity.x * speed, velocity.y * speed), .subType = entry.subType, .type = entry.type, .variant = entry.variant, .seed = entry.seed}); if (out->type == ENTITY_PICKUP && variant == PICKUP_MEGACHEST) static_cast(out)->dropDelay = spawned / 2; out->Update(); spawned++; if (Entity::is_enemy_type(out->type)) { Vector toward = vector_sub(pos, player.pos); float length = std::sqrt(toward.x * toward.x + toward.y * toward.y); float step = length > 0 ? ENEMY_LOOT_DISTANCE / length : 1; out->pos = vector_make(pos.x + toward.x * step, pos.y + toward.y * step); isPushing = true; } else if (out->type == ENTITY_PICKUP && IsChest(out->variant)) out->sprite.scale = vector_make(sprite.scale.x * CHEST_POOF_SCALE, sprite.scale.y * CHEST_POOF_SCALE); } if (!isPushing) return; Vector away = vector_sub(pos, player.pos); float distance = std::sqrt(away.x * away.x + away.y * away.y); if (distance * distance < CHEST_PUSH_RADIUS * CHEST_PUSH_RADIUS) { if (distance > 0) away = vector_make(away.x / distance, away.y / distance); float push = (CHEST_PUSH_RADIUS - distance) * CHEST_PUSH_STRENGTH; player.velocity = vector_make((player.velocity.x - push * away.x) * 0.5f, (player.velocity.y - push * away.y) * 0.5f); } wait = CHEST_WAIT; } // The chest part of 006e8ae0: a locked, eternal or old chest takes a key (a coin to pay to play) unless the player // holds Paper Clip, or two Strange Keys in the Blue Womb, then the chest opens. A spiked chest hurts a player still // touching it. Whether the touch counts as opening, which every unlocked touch does. bool Entity_Pickup::chest_touch(Entity_Player& player) { bool isLocked = !player.HasTrinket(TRINKET_PAPER_CLIP) && std::ranges::count(LOCKED_CHESTS, variant) > 0; if (isLocked && subtype == 0) return false; bool isStrangeKeyed = g_game->stage == STAGE_BLUE_WOMB && player.GetTrinketMultiplier(TRINKET_STRANGE_KEY) >= 2; if (isLocked && !isStrangeKeyed && !chest_lock_pay(player)) return false; int chestVariant = variant; if (!TryOpenChest(&player) || chestVariant != PICKUP_SPIKEDCHEST) return true; Vector apart = vector_sub(pos, player.pos); if (std::sqrt(apart.x * apart.x + apart.y * apart.y) <= size + player.size) player.TakeDamage(SPIKED_CHEST_DAMAGE, DAMAGE_CHEST | DAMAGE_NO_PENALTIES, EntityRef(this), SPIKED_CHEST_DAMAGE_COOLDOWN); return true; } // Pays a chest's lock: a key, or a coin for a pay to play chest. Whether it was paid. bool Entity_Pickup::chest_lock_pay(Entity_Player& player) { if (!isPayToPlay) return player.TryUseKey(); if (player.numCoins <= 0) return false; player.AddCoins(-1); return true; } // The Mega Chest part of 006e8ae0: a closed Mega Chest not already animating takes a key (or a coin) and plays its // unlock towards the player, opening when the animation ends. void Entity_Pickup::mega_chest_touch(Entity_Player& player) { bool isAnimating = sprite.animState.animData && sprite.animState.isPlaying; if (subtype == 0 || isAnimating || !chest_lock_pay(player)) return; target = &player; if (isPayToPlay) { sound_play(SOUND_COIN_INSERT); sprite.Play("UseCoin", PLAY_FORCED); return; } sound_play(SOUND_UNLOCK); sprite.Play(player.hasGoldenKey ? "UseGoldenKey" : "UseKey", PLAY_FORCED); }