#include "entity_bomb.h" #include "entity_effect.h" #include "entity_player.h" #include "entity_projectile.h" #include "game.h" #include "grid_entity_poop.h" #include "log.h" #include "npc_ai.h" #include "room.h" #include "room_grid.h" #include "sfx_manager.h" #include // The hazard effects: the farts, the shockwaves and crackwaves that break the floor and crush what stands there, the // white poop's hallowed ground and the statues' dying flash. // A fart: red for subtype 1, quiet from subtype 10 and from Mega Fatty, drawn over what stands near. constexpr int FART_RED = 1; constexpr int FART_QUIET = 10; constexpr int FART_SOUND_DELAY = 20; constexpr int FART_RENDER_Z = 1000; // How long a wave runs and the radius it grows between: the plain and directional shockwaves, the random one. constexpr int SHOCKWAVE_PLAIN_FRAMES = 20; constexpr float SHOCKWAVE_MIN_RADIUS = 20; constexpr float SHOCKWAVE_MAX_RADIUS = 95; constexpr int SHOCKWAVE_DIRECTIONAL_FRAMES = 50; constexpr float SHOCKWAVE_DIRECTIONAL_MIN_RADIUS = 45; constexpr float SHOCKWAVE_DIRECTIONAL_MAX_RADIUS = 320; constexpr int SHOCKWAVE_RANDOM_FRAMES = 8; // A shockwave cracks its ring every 5 frames, a crack each 30 units around it, 15 units deep at random. The // directional one cracks only within an arc around straight down, narrowing from a half turn to a tenth. constexpr int SHOCKWAVE_PERIOD = 5; constexpr float SHOCKWAVE_CRACK_SPACING = 30; constexpr float SHOCKWAVE_CRACK_DEPTH = 15; constexpr float SHOCKWAVE_ARC_WIDEST = PI_F / 2; constexpr float SHOCKWAVE_ARC_NARROWEST = PI_F / 20; // A random shockwave cracks every 3 frames, 30 units further out each time, at angles 30 to 210 degrees apart. constexpr int SHOCKWAVE_RANDOM_PERIOD = 3; constexpr float SHOCKWAVE_RANDOM_SPACING = 30; constexpr float SHOCKWAVE_RANDOM_STEP_MIN = 30; constexpr float SHOCKWAVE_RANDOM_STEP_SPAN = 180; // A crack's crush: its damage (more in versus mode) and reach, and the sound of a ring breaking. constexpr float CRACK_DAMAGE = 10; constexpr float CRACK_VERSUS_DAMAGE = 70; constexpr float CRACK_RADIUS = 20; constexpr float CRACK_VOLUME = 0.4f; // A crackwave's crawl: 30 units a step every 2 frames (60 every 4 for the Beast's subtype 10), wobbling up to 75 // degrees either way. Subtype 0 starts straight, subtype 1 never wobbles, subtype 2 wobbles more over 20 frames and // subtype 3 homes on the grid cell in its state, turning harder near it, for 150 frames at most. constexpr int CRACKWAVE_WOBBLE = 0; constexpr int CRACKWAVE_GROWING = 2; constexpr int CRACKWAVE_HOMING = 3; constexpr int CRACKWAVE_BEAST = 10; constexpr int CRACKWAVE_FRAMES = 240; constexpr int CRACKWAVE_PERIOD = 2; constexpr int CRACKWAVE_BEAST_PERIOD = 4; constexpr float CRACKWAVE_STEP = 30; constexpr float CRACKWAVE_BEAST_STEP = 60; constexpr float CRACKWAVE_WOBBLE_SPAN = 150; constexpr int CRACKWAVE_START_FRAMES = 6; constexpr float CRACKWAVE_GROWTH_FRAMES = 20; constexpr int CRACKWAVE_HOMING_FRAMES = 150; constexpr float CRACKWAVE_HOMING_NEAR = 80; constexpr float CRACKWAVE_HOMING_FAR = 400; constexpr float CRACKWAVE_HOMING_TURN_NEAR = 40; constexpr float CRACKWAVE_HOMING_TURN_FAR = 5; constexpr float CRACKWAVE_UNHOMED_DISTANCE = 1000; constexpr uint32_t CRACKWAVE_BEAST_COLLISION = 3; // The Beast's crackwave: the ghostly burst it leaves and its sound and shake. constexpr int CRACKWAVE_BEAST_SHAKE = 6; constexpr float CRACKWAVE_BEAST_VOLUME = 0.8f; constexpr float CRACKWAVE_BEAST_PITCH_MIN = 0.9f; constexpr float CRACKWAVE_BEAST_PITCH_SPAN = 0.2f; constexpr int ENEMY_GHOST_BURST = 1; // A crackwave from an entity with sticky spiders throws three rocks 1 time in 4, each larger and further on. constexpr int CRACKWAVE_ROCK_ODDS = 4; constexpr int CRACKWAVE_ROCK_COUNT = 3; constexpr float CRACKWAVE_ROCK_SPEED = 6; constexpr float CRACKWAVE_ROCK_GRAVITY = 0.5f; constexpr float CRACKWAVE_ROCK_RISE_MIN = -5; constexpr float CRACKWAVE_ROCK_RISE_SPAN = 25; constexpr float CRACKWAVE_ROCK_HEIGHT = -10; constexpr float CRACKWAVE_ROCK_SCALE_MIN = 0.6f; constexpr float CRACKWAVE_ROCK_SCALE_SPAN = 0.2f; constexpr float CRACKWAVE_ROCK_GROWTH = 1.2f; constexpr float CRACKWAVE_ROCK_DRAG = 0.9f; constexpr float CRACKWAVE_ROCK_PULL = 0.1f; constexpr ColorMod ROCK_PROJECTILE_COLOR = {{1, 1, 1, 1}, {4, 3.5f, 3.2f, 1}, {0, 0, 0}}; // The hallowed ground: -1 until it finds the white poop under it, 9999 while it follows a parent, -2 once it fades. // Its glow breathes by up to 2 percent. constexpr int HALLOWED_UNPLACED = -1; constexpr int HALLOWED_FADING = -2; constexpr int HALLOWED_FOLLOWING = 9999; constexpr float HALLOWED_BREATH_MIN = 0.98f; constexpr float HALLOWED_BREATH_SPAN = 0.04f; // A statue sinks 18 units into its cell when it appears, and from state 10 (its dying flash) plays a death cry. constexpr int STATUE_DYING = 10; constexpr float STATUE_SINK = 18; // Whether a wave can crack the floor at a point: inside the room and not over a pit or a wall. static bool is_crackable(Vector point) { Vector clamped = g_game->room->GetClampedPosition(point, 0); if (point.x != clamped.x || point.y != clamped.y) return false; GridEntity* grid = room_grid_entity_at(point); return !grid || (grid->type != GRID_PIT && grid->type != GRID_WALL); } // Breaks the floor at a point and crushes what stands there, for a source. static Entity* floor_crack(Vector point, Entity* source, float damage, float radius) { Entity* rock = g_game->Spawn({.position = point, .type = ENTITY_EFFECT, .variant = EFFECT_ROCK_EXPLOSION, .seed = Random()}); explosion_damage( {.position = point, .source = source, .damageFlags = DAMAGE_CRUSH, .damage = damage, .radius = radius, .isLineCheck = false}); return rock; } // A fart: red for subtype 1, mirrored at random, heard unless quiet, drawn in front. static void fart_init(Entity_Effect& fart) { if (fart.subtype == FART_RED) { fart.sprite.ReplaceSpritesheet(0, "gfx/Effects/Effect_Fart_Red.png"); fart.sprite.LoadGraphics(); } fart.sprite.isFlipX = !(Random() & 1); Entity* spawner = fart.spawnerEntity; if ((uint32_t)fart.subtype < FART_QUIET && (!spawner || spawner->type != ENTITY_MEGA_FATTY)) npc_sound_play(SOUND_FART, 1, FART_SOUND_DELAY, 1); fart.renderZOffset = FART_RENDER_Z; } // A shockwave is unseen: only its cracks show. static void shockwave_init(Entity_Effect& wave) { wave.isVisible = false; wave.timeout = SHOCKWAVE_PLAIN_FRAMES; wave.SetRadii(SHOCKWAVE_MIN_RADIUS, SHOCKWAVE_MAX_RADIUS); } static void shockwave_directional_init(Entity_Effect& wave) { wave.isVisible = false; wave.timeout = SHOCKWAVE_DIRECTIONAL_FRAMES; wave.SetRadii(SHOCKWAVE_DIRECTIONAL_MIN_RADIUS, SHOCKWAVE_DIRECTIONAL_MAX_RADIUS); } static void shockwave_random_init(Entity_Effect& wave) { wave.isVisible = false; wave.timeout = SHOCKWAVE_RANDOM_FRAMES; } static void crackwave_init(Entity_Effect& wave) { wave.isVisible = false; wave.timeout = CRACKWAVE_FRAMES; wave.targetPosition = {}; } // A ring of cracks growing from the minimum radius to the maximum as the timeout runs out, its first timeout kept // in its state. The directional wave keeps to its narrowing arc. static void shockwave_update(Entity_Effect& wave) { if (wave.state < wave.timeout) wave.state = wave.timeout; float timeout = (float)wave.timeout; float radius = lerp_clamped(timeout, (float)wave.state, 0, wave.minRadius, wave.maxRadius); float arc = lerp_clamped(timeout * timeout, (float)(wave.state * wave.state), 0, SHOCKWAVE_ARC_WIDEST, SHOCKWAVE_ARC_NARROWEST); if (wave.timeout % SHOCKWAVE_PERIOD == 0) { int count = (int)(radius * PI_F * 2 / SHOCKWAVE_CRACK_SPACING); float damage = g_game->deathmatchManager.is_active() ? CRACK_VERSUS_DAMAGE : CRACK_DAMAGE; for (int i = 0; i < count; i++) { float angle = (float)i / (float)count * (PI_F * 2); if (wave.variant != EFFECT_SHOCKWAVE && std::fabs(angle - PI_F / 2) >= arc) continue; Vector direction = vector_make(std::cos(angle), std::sin(angle)); float reach = random_unit() * SHOCKWAVE_CRACK_DEPTH + radius; Vector point = vector_add(vector_scale(direction, reach), wave.pos); if (is_crackable(point)) floor_crack(point, wave.parent, damage, CRACK_RADIUS); } npc_sound_play(SOUND_ROCK_CRUMBLE, CRACK_VOLUME, 2, 1); } if (wave.isDead) wave.Remove(); } // Cracks every 3 frames at random angles around the effect, 30 units further out each time. static void shockwave_random_update(Entity_Effect& wave) { int frame = wave.FrameCount(); if (frame % SHOCKWAVE_RANDOM_PERIOD == 0) { float start = random_unit() * DEGREES_PER_TURN; float rings = (float)(frame / SHOCKWAVE_RANDOM_PERIOD); for (float angle = start; angle < start + DEGREES_PER_TURN;) { Vector step = vector_scale(vector_from_angle(angle), SHOCKWAVE_RANDOM_SPACING); Vector point = vector_add(wave.pos, vector_scale(step, rings)); if (is_crackable(point)) floor_crack(point, wave.spawnerEntity, CRACK_DAMAGE, CRACK_RADIUS); npc_sound_play(SOUND_ROCK_CRUMBLE, CRACK_VOLUME, 2, 1); angle += random_unit() * SHOCKWAVE_RANDOM_STEP_SPAN + SHOCKWAVE_RANDOM_STEP_MIN; } } if (wave.isDead) wave.Remove(); } // How far a crackwave strays from its heading this step, by its subtype. The homing one turns toward its cell. static float crackwave_wobble(Entity_Effect& wave, int frame) { switch (wave.subtype) { case CRACKWAVE_WOBBLE: { float wobble = random_unit() * CRACKWAVE_WOBBLE_SPAN - CRACKWAVE_WOBBLE_SPAN / 2; return frame < CRACKWAVE_START_FRAMES ? wobble * (float)(frame % 2) / 4 : wobble; } case CRACKWAVE_GROWING: { if (frame <= CRACKWAVE_START_FRAMES) return 0; float growth = std::min((float)(frame - CRACKWAVE_START_FRAMES) / CRACKWAVE_GROWTH_FRAMES, 1.0f); return (random_unit() - 0.5f) * CRACKWAVE_WOBBLE_SPAN * growth; } case CRACKWAVE_HOMING: { float distance = CRACKWAVE_UNHOMED_DISTANCE; if (wave.state > 0) { Vector cell = g_game->room->GetGridPosition(wave.state); distance = vector_length(vector_sub(cell, wave.targetPosition)); float turn = lerp_clamped(distance, CRACKWAVE_HOMING_NEAR, CRACKWAVE_HOMING_FAR, CRACKWAVE_HOMING_TURN_NEAR, CRACKWAVE_HOMING_TURN_FAR); wave.rotation = angle_approach(wave.rotation, vector_angle(vector_sub(cell, wave.targetPosition)), turn); if (room_grid_collision_at(cell) == 0 || wave.FrameCount() > CRACKWAVE_HOMING_FRAMES) wave.state = -1; } if (wave.FrameCount() <= CRACKWAVE_START_FRAMES) return 0; float wobble = random_unit() - 0.5f; return lerp_clamped(distance, CRACKWAVE_HOMING_NEAR, CRACKWAVE_HOMING_FAR, 0, 1) * CRACKWAVE_WOBBLE_SPAN * wobble; } default: return 0; } } // Three rocks thrown from where a crackwave broke the floor, each larger and further along its arc. static void crackwave_rocks_throw(Entity_Effect& wave) { Vector position = wave.targetPosition; Vector heading = random_direction(); Vector velocity = vector_scale(vector_scale(heading, random_unit()), CRACKWAVE_ROCK_SPEED); float fallingSpeed = CRACKWAVE_ROCK_RISE_MIN - random_unit() * CRACKWAVE_ROCK_RISE_SPAN; float height = CRACKWAVE_ROCK_HEIGHT; float scale = random_unit() * CRACKWAVE_ROCK_SCALE_SPAN + CRACKWAVE_ROCK_SCALE_MIN; for (int i = 0; i < CRACKWAVE_ROCK_COUNT; i++) { auto* rock = static_cast(g_game->Spawn({.position = position, .velocity = velocity, .spawner = wave.spawnerEntity, .type = ENTITY_PROJECTILE, .seed = Random()})); rock->fallingAccel = CRACKWAVE_ROCK_GRAVITY; rock->fallingSpeed = fallingSpeed; rock->height = height; rock->SetColor(ROCK_PROJECTILE_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true); rock->scale = scale; position = vector_add(position, velocity); fallingSpeed = CRACKWAVE_ROCK_GRAVITY + CRACKWAVE_ROCK_DRAG * fallingSpeed + CRACKWAVE_ROCK_PULL; height += fallingSpeed; scale *= CRACKWAVE_ROCK_GROWTH; } } // A crackwave's step: it crawls on from its last crack, gone once it leaves the room or meets a pit, a wall or a // solid cell, cracking and crushing where it lands. static void crackwave_step(Entity_Effect& wave, int frame) { bool isBeast = wave.subtype == CRACKWAVE_BEAST; if (vector_is_zero(wave.targetPosition)) wave.targetPosition = wave.pos; float wobble = crackwave_wobble(wave, frame); float step = isBeast ? CRACKWAVE_BEAST_STEP : CRACKWAVE_STEP; Vector heading = vector_from_angle(wave.rotation + wobble); wave.targetPosition = vector_add(wave.targetPosition, vector_scale(heading, step)); if (!is_crackable(wave.targetPosition)) return wave.Remove(); if (room_grid_collision_at(wave.targetPosition) > (isBeast ? CRACKWAVE_BEAST_COLLISION : 0)) wave.Remove(); else if (isBeast) { npc_sound_play(SOUND_DEMON_HIT, CRACKWAVE_BEAST_VOLUME, 2, random_unit() * CRACKWAVE_BEAST_PITCH_SPAN + CRACKWAVE_BEAST_PITCH_MIN); game_shake_screen(CRACKWAVE_BEAST_SHAKE); Entity* ghost = g_game->Spawn({.position = wave.targetPosition, .spawner = wave.spawnerEntity, .subType = ENEMY_GHOST_BURST, .type = ENTITY_EFFECT, .variant = EFFECT_ENEMY_GHOST, .seed = Random()}); ghost->sprite.rotation = random_unit() * DEGREES_PER_TURN; ghost->SetCollisionDamage(0); ghost->Update(); } else { Entity* rock = floor_crack(wave.targetPosition, wave.spawnerEntity, CRACK_DAMAGE, wave.sprite.scale.x * CRACK_RADIUS); rock->sprite.scale = wave.sprite.scale; } if (isBeast) reconstruction_missing("Entity_Effect::Update 005d4380: the Beast's crackwave damage (Game::DamageRadius)"); if (wave.HasEntityFlags(FLAG_SPAWN_STICKY_SPIDERS) && random_below(CRACKWAVE_ROCK_ODDS) == 0) crackwave_rocks_throw(wave); } // 005fbe1a (Update's crackwave case) A line of cracks crawling across the room every few frames. static void crackwave_update(Entity_Effect& wave) { int frame = wave.FrameCount(); int period = wave.subtype == CRACKWAVE_BEAST ? CRACKWAVE_BEAST_PERIOD : CRACKWAVE_PERIOD; if (frame % period == 0) { crackwave_step(wave, frame); if (wave.subtype != CRACKWAVE_BEAST) npc_sound_play(SOUND_ROCK_CRUMBLE, CRACK_VOLUME, 2, 1); } if (wave.isDead) wave.Remove(); } // The hallowed ground glows additively as it appears. static void hallowed_ground_init(Entity_Effect& ground) { layer_additive_set(*ground.sprite.GetLayer(0)); ground.sprite.Play("Appear"); ground.state = HALLOWED_UNPLACED; } // Whether the white poop under the hallowed ground still stands. static bool is_hallowed_poop_whole(Entity_Effect& ground) { GridEntity* poop = g_game->room->GetGridEntity((uint32_t)ground.state); return poop && poop->type == GRID_POOP && poop->variant == POOP_WHITE && poop->state != POOP_STATE_BROKEN; } // 00602d32 (Update's hallowed ground case) Settles on the cell it stands on, or on its parent, breathes while it // idles and fades once its white poop breaks or its parent goes. static EffectUpdateEnd hallowed_ground_update(Entity_Effect& ground) { if (ground.state == HALLOWED_UNPLACED) { if (ground.parent) ground.state = HALLOWED_FOLLOWING; else { ground.state = g_game->room->GetGridIndex(ground.pos); if (ground.state < 0) { ground.Remove(); return EFFECT_UPDATE_RETURN; } } } if (ground.parent) ground.posOffset = ground.parent->posOffset; ANM2& sprite = ground.sprite; if (ground.state < 0) { if (sprite.IsFinished()) ground.Remove(); return EFFECT_UPDATE_FINISH; } if (sprite.IsFinished()) sprite.Play("Idle"); if (sprite.animState.IsLoopingAnimation()) { float breath = random_unit() * HALLOWED_BREATH_SPAN + HALLOWED_BREATH_MIN; sprite.GetLayer(0)->size = vector_make(breath, breath); } bool isFading = ground.state == HALLOWED_FOLLOWING ? !ground.parent : !is_hallowed_poop_whole(ground); if (!isFading) return EFFECT_UPDATE_FINISH; sprite.Play("Disappear"); ground.state = HALLOWED_FADING; return EFFECT_UPDATE_FINISH; } // 005efb10 (Update's statue case) Sinks into its cell on its first frame. Dying, it cries in its player's voice. static void statue_update(Entity_Effect& statue) { if (statue.state == 0) { statue.pos.y += STATUE_SINK; statue.state++; } if (statue.state != STATUE_DYING || !statue.sprite.IsEventTriggered("DeathSound")) return; Entity* spawner = statue.spawnerEntity; Entity_Player* player = spawner ? spawner->ToPlayer() : nullptr; npc_sound_play(SOUND_ISAACDIES, 1, 2, player ? player->get_voice_pitch() : 1); } constexpr auto HAZARD_BEHAVIOURS = std::to_array({ {EFFECT_DEVIL, nullptr, effect_finishing, nullptr}, {EFFECT_ANGEL, nullptr, effect_finishing, nullptr}, {EFFECT_FART, fart_init, effect_poof_update, nullptr}, {EFFECT_SHOCKWAVE, shockwave_init, effect_finishing, nullptr}, {EFFECT_SHOCKWAVE_DIRECTIONAL, shockwave_directional_init, effect_finishing, nullptr}, {EFFECT_CRACKWAVE, crackwave_init, effect_finishing, nullptr}, {EFFECT_SHOCKWAVE_RANDOM, shockwave_random_init, effect_finishing, nullptr}, {EFFECT_HALLOWED_GROUND, hallowed_ground_init, hallowed_ground_update, nullptr}, }); std::span hazard_effect_behaviours() { return HAZARD_BEHAVIOURS; }