#include "npc_fireplace.h" #include "entity_pickup.h" #include "entity_player.h" #include "entity_projectile.h" #include "game.h" #include "npc_ai.h" #include "room.h" #include // A fireplace's variants (entities2.xml's 33): the fires, the moveable ones and Coal. enum FireplaceVariant : int { FIRE_NORMAL = 0, FIRE_RED = 1, FIRE_BLUE = 2, FIRE_PURPLE = 3, FIRE_WHITE = 4, FIRE_MOVEABLE = 10, FIRE_COAL = 11, FIRE_MOVEABLE_BLUE = 12, FIRE_MOVEABLE_PURPLE = 13 }; // A fire burns lit, is out, and is told to light again by others. constexpr int FIRE_STATE_OUT = NPC_STATE_IDLE; constexpr int FIRE_STATE_LIT = NPC_STATE_ATTACK; constexpr int FIRE_STATE_RELIGHT = NPC_STATE_ATTACK2; // A fire's three looks (its subtype), each with its burning, burnt out and dying animation. constexpr int FIRE_LOOKS = 3; constexpr const char* FLICKERING[FIRE_LOOKS] = {"Flickering", "Flickering2", "Flickering3"}; constexpr const char* NO_FIRE[FIRE_LOOKS] = {"NoFire", "NoFire2", "NoFire3"}; constexpr const char* DISAPPEAR[FIRE_LOOKS] = {"Dissapear", "Dissapear2", "Dissapear3"}; // The layer of the flame, which shrinks with the fire's health, and how deep a fire that is out draws. constexpr int FLAME_LAYER = 1; constexpr int FIRE_OUT_DEPTH = -1000; // A fire's health: it burns above 1.5 and is out at 1. A hit of 1000 puts a normal fire out at once, and its hits cost // it 2 or 3 above 10 damage, else 1 or 2. constexpr float FIRE_LIT_HEALTH = 1.5f; constexpr float FIRE_OUT_HEALTH = 1; constexpr float FIRE_HUGE_HIT = 1000; constexpr float FIRE_BIG_HIT = 10; constexpr int FIRE_HIT_ODDS = 3; // Water douses normal fires: every 5 frames a fire takes a hit while the room's water is above 0.2. constexpr float FIRE_WATER_LEVEL = 0.2f; constexpr int FIRE_WATER_FRAMES = 5; constexpr int FIRE_WATER_DAMAGE_COUNTDOWN = 30; // The odds a blue fire turns purple on the room's first frame, and a fire's grid path while lit. constexpr uint32_t PURPLE_FIRE_ODDS = 100; constexpr int FIRE_PATH = 950; // A fire's shot cooldown when placed and when lit again. constexpr uint32_t FIRE_COOLDOWN_RANGE = 30; constexpr int FIRE_COOLDOWN_MIN = 60; constexpr uint32_t RELIGHT_COOLDOWN_RANGE = 60; constexpr int RELIGHT_COOLDOWN_MIN = 90; // How red and purple fires spit: their mouth above them and a little toward the target, the range they see in, the // odds of trying and of shooting, the cooldown, and the shot's speed and a purple shot's size. constexpr float FIRE_MOUTH_HEIGHT = 15; constexpr float FIRE_MOUTH_REACH = 25; constexpr float FIRE_SIGHT = 160; constexpr int FIRE_AIM_ODDS = 5; constexpr int FIRE_SHOOT_ODDS = 15; constexpr int FIRE_SHOOT_COOLDOWN = 84; constexpr float FIRE_SHOT_SPEED = 5; constexpr float PURPLE_SHOT_SCALE = 1.5f; constexpr int CHECK_LINE_PROJECTILE = 3; constexpr uint64_t PROJECTILE_SMART = 1; // The entities whose fires do not spit (Lua's ENTITY_HERETIC and the unused ENTITY_RAGLICH). constexpr int ENTITY_HERETIC = 905; constexpr int ENTITY_RAGLICH = 919; // The embers a lit fire throws: their odds, where over the flame they start, and how they drift up. constexpr int EMBER_ODDS = 7; constexpr float EMBER_SPREAD = 16; constexpr float EMBER_HEIGHT = 15; constexpr float EMBER_HEALTH_RISE = 6; constexpr float EMBER_RISE_MIN = 0.8f; constexpr float EMBER_RISE_RANGE = 1.6f; constexpr float EMBER_DRIFT = 0.4f; constexpr int EMBER_PRIORITY = 255; constexpr ColorMod EMBER_RED = {{1, 0.5f, 0.3f, 1}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr ColorMod EMBER_BLUE = {{1, 1, 1, 1}, {0.2f, 1, 1.5f, 1}, {0, 0, 0}}; constexpr ColorMod EMBER_PURPLE = {{1, 1, 1, 1}, {1, 0.2f, 1, 1}, {0, 0, 0}}; constexpr ColorMod EMBER_WHITE = {{1, 1, 1, 1}, {1.5f, 1.5f, 1.5f, 1}, {0, 0, 0}}; // The poof of a fire lit again, a little above it, and the colors it takes from a red or another fire. constexpr float RELIGHT_POOF_HEIGHT = 6; constexpr float RELIGHT_RED_OFFSET = 0.6f; constexpr float RELIGHT_RED_BLUE = 0; constexpr float RELIGHT_OFFSET = 0.35f; constexpr float RELIGHT_BLUE = 0.5f; // The sounds of a fire: lit again, and put out. constexpr int SOUND_CANDLE_LIGHT = 471; constexpr int SOUND_STEAM_HALFSEC = 261; constexpr float PUT_OUT_VOLUME = 0.85f; // The effects a fire spawns (Lua's EffectVariant). constexpr int EFFECT_POOF01 = 15; constexpr int EFFECT_EMBER_PARTICLE = 66; // What a fire drops: one time in four (one in ten on Hard), from normal fires a heart one time in four and else a // coin, from blue fires a soul heart (a black heart past one Daemon's Tail). Daemon's Tail turns four hearts in five // away, by its own RNG off the drop RNG. constexpr int COLLECTIBLE_BLACK_POWDER = 420; constexpr int TRINKET_DAEMONS_TAIL = 22; constexpr float LOOT_CHANCE = 0.25f; constexpr float LOOT_CHANCE_HARD = 0.1f; constexpr float LOOT_HEART_CHANCE = 0.25f; constexpr uint32_t DAEMONS_TAIL_ODDS = 5; // Plays the animation of the fire's look, past the third look the third. static void fire_play(Entity_NPC& npc, const char* const (&names)[FIRE_LOOKS]) { npc.sprite.Play(names[std::min((uint32_t)npc.subtype, (uint32_t)FIRE_LOOKS - 1)]); } // Sizes the flame to the fire's health. static void flame_size_set(Entity_NPC& npc) { float share = npc.health / npc.maxHitPoints; npc.sprite.GetLayer(FLAME_LAYER)->size = vector_make(share, share); } // Sets the room's path on the fire's cell, unless it rides a minecart. static void fire_path_set(Entity_NPC& npc, int value) { if (npc.minecart) return; Room& room = *g_game->room; room.SetGridPath(room.GetGridIndex(npc.pos), value); } // Lights the fire: flickering from a frame its seed picks, with the flame at its health's size. static void fire_light(Entity_NPC& npc) { npc.state = FIRE_STATE_LIT; fire_play(npc, FLICKERING); const AnimationData* data = npc.sprite.animState.animData; if (data && data->length > 0) npc.sprite.animState.SetFrameFloat((float)(int)(npc.initSeed % (uint32_t)data->length)); flame_size_set(npc); } // Puts the fire out: it collides with nothing and draws under everything. static void fire_put_out(Entity_NPC& npc) { npc.state = FIRE_STATE_OUT; npc.entityCollisionClass = ENTCOLL_NONE; npc.entityGridCollisionClass = GRIDCOLL_NONE; fire_play(npc, NO_FIRE); npc.renderZOffset = FIRE_OUT_DEPTH; } // Whether a fire is one water does not douse and that keeps burning in water: blue, purple and white ones. static bool is_magic_fire(int variant) { return variant == FIRE_BLUE || variant == FIRE_PURPLE || variant == FIRE_WHITE; } // The first frame: a blue fire may turn purple and every fire takes a random look on the room's first frame. It // stays put and, burning, claims its cell. Water douses normal fires and the Mausoleum's heart every fire. static void fire_init(Entity_NPC& npc) { Game& game = *g_game; Room& room = *game.room; RNG rng = {npc.initSeed, 5, 9, 7}; npc.AddEntityFlags(FLAG_NO_DEATH_TRIGGER); if (room.isInitialized && game.frameCount == room.enterFrame) { if (npc.variant == FIRE_BLUE && rng.RandomInt(PURPLE_FIRE_ODDS) == 0) npc.Morph(npc.type, FIRE_PURPLE, npc.subtype, -1); npc.subtype = (int)rng.RandomInt(FIRE_LOOKS); } npc.targetPosition = npc.pos; npc.projectileCooldown = (int)(Random() % FIRE_COOLDOWN_RANGE) + FIRE_COOLDOWN_MIN; npc.flags = (npc.flags & ~FLAG_APPEAR) | FLAG_NO_STATUS_EFFECTS | FLAG_NO_TARGET | FLAG_NO_BLOOD_SPLASH | FLAG_NO_DEATH_TRIGGER | FLAG_NO_FLASH_ON_DAMAGE | FLAG_NO_PHYSICS_KNOCKBACK; bool isDoused = (room.waterAmount > 0 && !is_magic_fire(npc.variant) && !npc.minecart) || game.IsMausoleumHeartStage(); if (npc.health > FIRE_LIT_HEALTH && isDoused) npc.health = FIRE_OUT_HEALTH; if (npc.health > FIRE_LIT_HEALTH) { fire_path_set(npc, FIRE_PATH); fire_light(npc); } else { fire_path_set(npc, GRID_PATH_FREED); fire_put_out(npc); } npc.UpdatePickupGhosts(); } // Lights a fire that was out again, at full health, with a colored poof and a sound. static void fire_relight(Entity_NPC& npc) { npc.health = npc.maxHitPoints; fire_light(npc); npc.entityCollisionClass = ENTCOLL_ALL; npc.renderZOffset = 0; fire_path_set(npc, FIRE_PATH); Entity* poof = g_game->Spawn({.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()}); bool isRed = npc.variant == FIRE_RED; poof->posOffset = vector_make(0, -RELIGHT_POOF_HEIGHT); poof->sprite.color._offset[0] = isRed ? RELIGHT_RED_OFFSET : RELIGHT_OFFSET; poof->sprite.color._offset[1] = 0; poof->sprite.color._offset[2] = isRed ? RELIGHT_RED_BLUE : RELIGHT_BLUE; npc.PlaySound(SOUND_CANDLE_LIGHT, 1, 2, false, 1); npc.projectileCooldown = (int)(Random() % RELIGHT_COOLDOWN_RANGE) + RELIGHT_COOLDOWN_MIN; } // What a hit costs a normal fire: 2 or 3 past 10 damage, else 1 or 2. static float hit_cost_get(float damage) { bool isBig = damage > FIRE_BIG_HIT; bool isMore = Random() % FIRE_HIT_ODDS == 0; return (float)(isBig + isMore + 1); } // The player whose hit, the last one, put the fire out. static Entity_Player* fire_putter_get(Entity_NPC& npc) { Entity* source = npc.damageEvents.empty() ? nullptr : npc.damageEvents.back().source.entity; return source ? static_cast(source->GetPlayerAncestor()) : nullptr; } // Entity_NPC::fireplace_get_loot (006d1580, which needs a LootList), advancing the drop RNG, with each drop spawned // and updated at once: what a fire put out drops, nothing from white, moveable or Coal fires or without rewards. static void fire_loot_spawn(Entity_NPC& npc) { PlayerManager& players = g_game->playerManager; RNG& rng = npc.dropRNG; int variant = npc.variant; bool isLootless = npc.HasEntityFlags(FLAG_NO_REWARD) || variant == FIRE_WHITE || variant >= FIRE_MOVEABLE; auto isTailed = [&] { RNG tail = {rng._seed, 1, 11, 6}; return players.AnyoneHasTrinket(TRINKET_DAEMONS_TAIL) && tail.RandomInt(DAEMONS_TAIL_ODDS) != 0; }; if (isLootless || rng.RandomFloat() >= (game_is_hard_mode() ? LOOT_CHANCE_HARD : LOOT_CHANCE)) return; int pickup = PICKUP_HEART; int subtype = 0; if (variant == FIRE_BLUE || variant == FIRE_PURPLE) { if (isTailed()) return; subtype = players.GetTrinketMultiplier(TRINKET_DAEMONS_TAIL) < 2 ? HEART_SOUL : HEART_BLACK; } else if (rng.RandomFloat() >= LOOT_HEART_CHANCE || isTailed()) pickup = PICKUP_COIN; uint32_t seed = rng.Next(); Entity* drop = g_game->Spawn({.position = npc.pos, .subType = subtype, .type = ENTITY_PICKUP, .variant = pickup, .seed = seed}); drop->Update(); g_game->room->isPickupVisionInvalid = true; } // The fire's hits this frame. A normal or red fire loses health to each and goes out at the last of it, or at once to // an explosion, its death or a huge hit, with a hiss. Blue and purple fires go out only to explosions or death, in // a poof. A fire put out drops its loot, and Black Powder's owner blows it up. A fire never dies. static void fire_damage_update(Entity_NPC& npc) { bool isExploded = npc.pendingDamageFlags & DAMAGE_EXPLOSION; bool isLit = npc.state == FIRE_STATE_LIT; if (npc.pendingDamage <= 0 && !npc.isDead) return; if ((npc.variant == FIRE_NORMAL || npc.variant == FIRE_RED) && isLit) { bool isQuenched = npc.isDead || isExploded || npc.pendingDamage >= FIRE_HUGE_HIT; for (DamageEvent& event : npc.damageEvents) if (event.damage > 0) npc.health -= hit_cost_get(event.damage); npc.health = std::max(npc.health, FIRE_OUT_HEALTH); flame_size_set(npc); if (npc.health >= FIRE_LIT_HEALTH && !isQuenched) return; fire_path_set(npc, GRID_PATH_FREED); npc.health = FIRE_OUT_HEALTH; fire_put_out(npc); npc.PlaySound(SOUND_STEAM_HALFSEC, PUT_OUT_VOLUME, 2, false, 1); fire_play(npc, DISAPPEAR); Entity_Player* player = fire_putter_get(npc); if (player && player->HasCollectible(COLLECTIBLE_BLACK_POWDER)) reconstruction_missing("NpcAI::ENTITY_FIREPLACE 006d18d0: Black Powder's blast (Game::BombExplosionEffects)"); } else if ((npc.variant == FIRE_BLUE || npc.variant == FIRE_PURPLE) && isLit) { if (!isExploded && !npc.isDead) return; npc.health = FIRE_OUT_HEALTH; fire_put_out(npc); npc.PlaySound(SOUND_STEAM_HALFSEC, PUT_OUT_VOLUME, 2, false, 1); g_game->Spawn({.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()}); } else { if (npc.isDead) npc.health = FIRE_OUT_HEALTH; npc.isDead = false; return; } // The online stats tally of fires put out (Player_stats_add 00425ac0) is not written yet. fire_loot_spawn(npc); npc.UpdatePickupGhosts(); if (npc.isDead) npc.health = FIRE_OUT_HEALTH; npc.isDead = false; } // Where a fire aims: from its mouth at the nearest player, starting a little out toward it. struct FireAim { Vector start{}; Vector target{}; Vector direction{}; float distance = 0; }; static FireAim fire_aim_get(Entity_NPC& npc) { FireAim aim; Vector mouth = vector_make(npc.pos.x, npc.pos.y - FIRE_MOUTH_HEIGHT); aim.target = game_nearest_player(npc.pos)->pos; Vector toTarget = vector_sub(aim.target, mouth); aim.direction = vector_normalized(toTarget); aim.distance = vector_length(toTarget); aim.start = vector_add(mouth, vector_scale(aim.direction, FIRE_MOUTH_REACH)); return aim; } // Whether the fire spits: red and purple fires, unless the Heretic or Raglich made them. static bool is_spitting_fire(Entity_NPC& npc) { return (npc.variant == FIRE_RED || npc.variant == FIRE_PURPLE) && npc.spawnerType != ENTITY_HERETIC && npc.spawnerType != ENTITY_RAGLICH; } // A spitting fire now and then readies a shot at the nearest player in sight and range. The shot leaves on its // overlay's Shoot event, a purple one homing and bigger, while the overlay carries the flame along. static void fire_shoot_update(Entity_NPC& npc) { if (is_spitting_fire(npc) && npc.projectileCooldown < 1 && npc.state == FIRE_STATE_LIT && Random() % FIRE_AIM_ODDS == 0) { FireAim aim = fire_aim_get(npc); if (aim.distance < FIRE_SIGHT && room_check_line(aim.start, aim.target, CHECK_LINE_PROJECTILE, 0) && Random() % FIRE_SHOOT_ODDS == 0) { npc.sprite.PlayOverlay("Shoot"); npc.projectileCooldown = FIRE_SHOOT_COOLDOWN; } } else if (npc.projectileCooldown > 0) npc.projectileCooldown--; if (!npc.sprite.overlayAnimState.isPlaying) return; if (const NullFrame* frame = npc.sprite.overlayAnimState.GetNullFrameById(0)) { LayerState* flame = npc.sprite.GetLayer(FLAME_LAYER); float share = npc.health / npc.maxHitPoints; flame->pos = frame->pos; flame->size = vector_make(frame->scale.x * share, share * frame->scale.y); } if (!npc.sprite.overlayAnimState.IsEventTriggered("Shoot") || npc.spawnerType == ENTITY_HERETIC || npc.spawnerType == ENTITY_RAGLICH) return; FireAim aim = fire_aim_get(npc); uint32_t seed = Random(); Entity* shot = g_game->Spawn({.position = aim.start, .velocity = vector_scale(aim.direction, FIRE_SHOT_SPEED), .spawner = &npc, .type = ENTITY_PROJECTILE, .seed = seed}); if (npc.variant != FIRE_PURPLE) return; Entity_Projectile& projectile = static_cast(*shot); projectile.projectileFlags |= PROJECTILE_SMART; projectile.scale = PURPLE_SHOT_SCALE; } // A lit fire now and then throws an ember up out of its flame, in its fire's color. static void fire_ember_update(Entity_NPC& npc) { if (npc.state != FIRE_STATE_LIT || Random() % EMBER_ODDS != 0) return; float height = random_unit(); float x = random_unit() * EMBER_SPREAD - EMBER_SPREAD / 2 + npc.pos.x; float y = -EMBER_HEIGHT - npc.health * EMBER_HEIGHT / EMBER_HEALTH_RISE - height * EMBER_HEIGHT + npc.pos.y; float rise = random_unit() * -EMBER_RISE_RANGE - EMBER_RISE_MIN; float drift = random_unit() * EMBER_DRIFT - EMBER_DRIFT / 2; Entity* ember = g_game->Spawn({.position = vector_make(x, y), .velocity = vector_make(drift, rise), .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_EMBER_PARTICLE, .seed = Random()}); const ColorMod* color = nullptr; switch (npc.variant) { case FIRE_RED: color = &EMBER_RED; break; case FIRE_BLUE: color = &EMBER_BLUE; break; case FIRE_PURPLE: color = &EMBER_PURPLE; break; case FIRE_WHITE: color = &EMBER_WHITE; break; } if (color) ember->SetColor(*color, -1, EMBER_PRIORITY, false, true); } // 006d18d0 The fireplace's AI: normal fires are doused by water and put out by shots, red ones spit, blue and purple // ones go out only to explosions, white ones never. A lit fire hurts what touches it, throws embers and drops loot // when put out. A fire that is out and finds a grid entity under it, off a minecart, is removed. Moveable fires and // Coal have their own AIs. void npc_fireplace_update(Entity_NPC& npc) { if (!is_magic_fire(npc.variant) && g_game->room->waterAmount > FIRE_WATER_LEVEL && npc.IsFrame(FIRE_WATER_FRAMES, (int)npc.index)) npc.TakeDamage(1, 0, EntityRef(), FIRE_WATER_DAMAGE_COUNTDOWN); if (npc.variant == FIRE_COAL) return NpcAI::ENTITY_ROCK_SPIDER(&npc); if (npc.variant == FIRE_MOVEABLE || npc.variant == FIRE_MOVEABLE_BLUE || npc.variant == FIRE_MOVEABLE_PURPLE) return NpcHelper::fireplace_moveable_update(&npc); if (npc.state == NPC_STATE_INIT) return fire_init(npc); if (npc.state == FIRE_STATE_RELIGHT) fire_relight(npc); if (npc.state == FIRE_STATE_LIT && !npc.sprite.IsPlaying()) fire_play(npc, FLICKERING); npc.velocity = vector_sub(npc.targetPosition, npc.pos); fire_damage_update(npc); npc.damageEvents.clear(); npc.pendingDamage = 0; npc.pendingDamageFlags = 0; fire_shoot_update(npc); fire_ember_update(npc); Room& room = *g_game->room; if (npc.state == FIRE_STATE_OUT && room.GetGridCollision(room.GetGridIndex(npc.pos)) != COLLISION_NONE && !npc.minecart) npc.Remove(); }