#include "entity_bomb.h" #include "entity_effect.h" #include "entity_list.h" #include "entity_player.h" #include "entity_projectile.h" #include "entity_tear.h" #include "weapon_rockets.h" #include "backdrop.h" #include "game.h" #include "loot_list.h" #include "room_grid.h" #include "grid_entity_trap_door.h" #include "npc_ai.h" #include "room.h" #include #include #include // The blasts that sweep a room: Mama Mega's (red) and Mega Bean's (green, freezing and poisoning). The room-wide ones // (subtypes 0 and 2) grow from the target's distance to the room to 200 past its farthest corner, the small one // (subtype 1) from its minimum radius to its maximum, in rings of fire. // The room-wide blast shakes the screen for 50 frames, blooms for 6 and ripples the floor. constexpr int MEGA_BLAST_SHAKE = 50; constexpr int MEGA_BLAST_BLOOM = 6; constexpr float MEGA_BLAST_RIPPLE_AMPLITUDE = 0.5f; constexpr float MEGA_BLAST_RIPPLE_SPEED = 0.07f; constexpr int MEGA_BLAST_RIPPLE_DURATION = 20; constexpr float MEGA_BLAST_REACH_PAST = 200; constexpr int MEGA_BLAST_SOUND_DELAY = 15; constexpr float MEGA_BLAST_RUMBLE_VOLUME = 1.1f; // The small blast's ripple and rings scale with its reach from 60 to 600. constexpr float SMALL_BLAST_REACH_LOW = 60; constexpr float SMALL_BLAST_REACH_HIGH = 600; constexpr float SMALL_BLAST_AMPLITUDE_LOW = 0.01f; constexpr float SMALL_BLAST_AMPLITUDE_HIGH = 0.08f; constexpr float SMALL_BLAST_SPEED_LOW = 0.01f; constexpr float SMALL_BLAST_SPEED_HIGH = 0.07f; constexpr float SMALL_BLAST_DURATION_LOW = 8; constexpr float SMALL_BLAST_DURATION_HIGH = 20; // Its rings of fire (the big attract effect's subtypes 10 and 11): a full ring and three arcs a third of a turn apart, // each swung up to 30 either way, 14 up. constexpr int BLAST_RING_FULL = 10; constexpr int BLAST_RING_ARC = 11; constexpr int BLAST_RING_ARCS = 3; constexpr float BLAST_RING_ARC_SPAN = 120; constexpr float BLAST_RING_ARC_SWING = 60; constexpr float BLAST_RING_INNER = 10; constexpr float BLAST_RING_REACH = 0.3f; constexpr float BLAST_ARC_REACH_MIN = 0.15f; constexpr float BLAST_ARC_REACH_SPAN = 0.15f; constexpr float BLAST_ARC_HEIGHT_MIN = 0.6f; constexpr float BLAST_ARC_HEIGHT_SPAN = 0.4f; constexpr float BLAST_RING_HEIGHT = -14; // The blast's front moves a third of its reach every other frame (30 to 100), pushing what it reaches by 3. It hurts // each enemy once for its damage, then a fiftieth (an eighth for the small blast) each tick, 1 at least. constexpr int BLAST_TICK_FRAMES = 2; constexpr float BLAST_STEP_SHARE = 3; constexpr float BLAST_STEP_MIN = 30; constexpr float BLAST_STEP_MAX = 100; constexpr float BLAST_PUSH = 3; constexpr float BLAST_TICK_SHARE = 50; constexpr float SMALL_BLAST_TICK_SHARE = 8; constexpr uint64_t BLAST_DAMAGE_FLAGS = DAMAGE_EXPLOSION | DAMAGE_CRUSH; constexpr int BLAST_HIT_COUNTDOWN = 30; constexpr int BEAN_FREEZE = -50; constexpr int BEAN_POISON = 90; constexpr float BEAN_POISON_DAMAGE = 5; // The small effects the front clears away: tiny bugs and flies, worms, beetles and lil ghosts. constexpr std::array BLAST_SWEPT_EFFECTS = {EFFECT_TINY_BUG, EFFECT_TINY_FLY, EFFECT_WORM, EFFECT_BEETLE, EFFECT_LIL_GHOST}; // Behind the front, half the cells inside the room (80 in) more than 50 back throw up dust (blood for the small // blast) up to 50 off the cell, and the room-wide blast breaks every grid entity it passes (the small one a quarter). constexpr float DEBRIS_MARGIN = -80; constexpr float DEBRIS_TRAIL = 50; constexpr float DEBRIS_SCATTER = 50; constexpr float DEBRIS_SPEED_MIN = 4; constexpr float DEBRIS_SPEED_SPAN = 4; constexpr float BLAST_DUST_SPIN_MIN = 2; constexpr float BLAST_DUST_SPIN_SPAN = 10; constexpr int BLAST_DUST_LIFE_MIN = 10; constexpr int BLAST_DUST_LIFE_SPAN = 10; constexpr float BLAST_DUST_ALPHA_MIN = 0.2f; constexpr float BLAST_DUST_ALPHA_SPAN = 0.5f; constexpr float BLAST_DUST_SCALE_MIN = 1.2f; constexpr float BLAST_DUST_SCALE_SPAN = 3; constexpr float BLOOD_SCALE_MIN = 0.8f; constexpr float BLOOD_SCALE_SPAN = 0.4f; constexpr float BLOOD_SPLAT_SCALE = 3; constexpr float BLOOD_LARGE_REACH = 300; constexpr float BLOOD_MEDIUM_REACH = 150; constexpr float BLAST_DOORS_REACH = 300; // The blasts' colours: Mama Mega's red and Mega Bean's green, and the dust of the glowing blast (subtype 2). constexpr ColorMod MAMA_MEGA_COLOR = {{1, 0.2f, 0.2f, 1}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr ColorMod MEGA_BEAN_COLOR = {{0.2f, 1, 0.2f, 1}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr ColorMod MAMA_MEGA_GLOW = {{5, 0.5f, 0.2f, 1}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr ColorMod MEGA_BEAN_GLOW = {{0.2f, 0.5f, 0.2f, 1}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr float BEAN_DUST_TINT = 0.7f; // The blast's states: about to start, sweeping, and past the doors it may open. enum BlastState { MEGA_BLAST_STARTING, MEGA_BLAST_SWEEPING, MEGA_BLAST_DOORS_DONE }; // Whether the blast sweeps the whole room (or is the small one). static bool is_room_blast(Entity_Effect& blast) { return blast.subtype == 0 || blast.subtype == 2; } // Part of 005cdb20 (Init's case 127 and 202) The blast is unseen and never overwritten. Mega Bean's blares at once. static void mega_blast_init(Entity_Effect& blast) { blast.isVisible = false; blast.AddEntityFlags(FLAG_DONT_OVERWRITE); if (blast.variant == EFFECT_MEGA_BEAN_EXPLOSION) effect_sound_play(SOUND_MEGABEAN_BLAST); } // A ring of fire of the small blast at its target, in its colour, out to a share of its reach. static Entity_Effect* blast_ring_spawn(Entity_Effect& blast, int subtype, const ColorMod& color, int timeout, float reach) { auto* ring = static_cast(g_game->Spawn({.position = blast.targetPosition, .spawner = &blast, .subType = subtype, .type = ENTITY_EFFECT, .variant = EFFECT_BIG_ATTRACT, .seed = Random()})); ring->posOffset = vector_make(0, BLAST_RING_HEIGHT); ring->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true); ring->SetTimeout(timeout); ring->SetRadii(BLAST_RING_INNER, reach); return ring; } // The small blast's ripple and rings last 8 to 20 frames with its reach. static int small_blast_duration(Entity_Effect& blast) { float frames = lerp_range(blast.maxRadius, {SMALL_BLAST_REACH_LOW, SMALL_BLAST_REACH_HIGH, SMALL_BLAST_DURATION_LOW, SMALL_BLAST_DURATION_HIGH}); return (int)std::ceil(frames); } // The blast's first frame. The room-wide one reaches from the target's distance to the room to 200 past its farthest // corner, shaking, blooming and rippling the room (and may dig up Mom's Shovel). The small one ripples and rings. static void mega_blast_start(Entity_Effect& blast, const ColorMod& color) { if (!is_room_blast(blast)) { blast.scale = blast.minRadius; blast.state = MEGA_BLAST_SWEEPING; float amplitude = lerp_range(blast.maxRadius, {SMALL_BLAST_REACH_LOW, SMALL_BLAST_REACH_HIGH, SMALL_BLAST_AMPLITUDE_LOW, SMALL_BLAST_AMPLITUDE_HIGH}); float speed = lerp_range(blast.maxRadius, {SMALL_BLAST_REACH_LOW, SMALL_BLAST_REACH_HIGH, SMALL_BLAST_SPEED_LOW, SMALL_BLAST_SPEED_HIGH}); int duration = small_blast_duration(blast); shockwave_make(blast.targetPosition, amplitude, speed, duration); blast_ring_spawn(blast, BLAST_RING_FULL, color, duration * 2, blast.maxRadius * BLAST_RING_REACH)->Update(); return; } Room& room = *g_game->room; Vector centre = vector_scale(vector_add(room.topLeftPos, room.bottomRightPos), 0.5f); Vector half = vector_scale(vector_sub(room.bottomRightPos, room.topLeftPos), 0.5f); Vector offset = vector_sub(blast.targetPosition, centre); Vector nearest = vector_make(std::max(std::fabs(offset.x) - half.x, 0.0f), std::max(std::fabs(offset.y) - half.y, 0.0f)); Vector farthest = vector_make(std::max(std::fabs(offset.x + half.x), std::fabs(offset.x - half.x)), std::max(std::fabs(offset.y + half.y), std::fabs(offset.y - half.y))); blast.minRadius = vector_length(nearest); blast.maxRadius = vector_length(farthest) + MEGA_BLAST_REACH_PAST; blast.scale = blast.minRadius; blast.state = MEGA_BLAST_SWEEPING; Game& game = *g_game; game_shake_screen(MEGA_BLAST_SHAKE); game.bloomDuration = MEGA_BLAST_BLOOM; game.bloomTimer = MEGA_BLAST_BLOOM; game.bloomStrength = 1; shockwave_make(blast.targetPosition, MEGA_BLAST_RIPPLE_AMPLITUDE, MEGA_BLAST_RIPPLE_SPEED, MEGA_BLAST_RIPPLE_DURATION); if (blast.variant == EFFECT_MAMA_MEGA_EXPLOSION) effect_sound_play(SOUND_EXPLOSION_STRONG, {.frameDelay = MEGA_BLAST_SOUND_DELAY}); shovel_quest_dig(); } // The small blast's rings each tick: a full ring and three arcs at random turns, of random reach and height. static void small_blast_rings(Entity_Effect& blast, const ColorMod& color) { int duration = small_blast_duration(blast); blast_ring_spawn(blast, BLAST_RING_FULL, color, duration * 2, blast.maxRadius * BLAST_RING_REACH)->Update(); float turn = random_unit() * DEGREES_PER_TURN; for (int i = 0; i < BLAST_RING_ARCS; i++) { uint32_t seed = Random(); auto* arc = static_cast(g_game->Spawn({.position = blast.targetPosition, .spawner = &blast, .subType = BLAST_RING_ARC, .type = ENTITY_EFFECT, .variant = EFFECT_BIG_ATTRACT, .seed = seed})); float swing = random_unit() * BLAST_RING_ARC_SWING - BLAST_RING_ARC_SWING / 2; arc->sprite.rotation = (float)i * BLAST_RING_ARC_SPAN + turn + swing; arc->posOffset = vector_make(0, BLAST_RING_HEIGHT); arc->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true); arc->SetTimeout(duration); arc->SetRadii(BLAST_RING_INNER, (random_unit() * BLAST_ARC_REACH_SPAN + BLAST_ARC_REACH_MIN) * blast.maxRadius); arc->sprite.GetLayer(0)->size.y = random_unit() * BLAST_ARC_HEIGHT_SPAN + BLAST_ARC_HEIGHT_MIN; arc->Update(); } } // The blast's push away from its target. static Vector blast_push(Entity_Effect& blast, Entity& entity) { return vector_scale(vector_normalized(vector_sub(entity.pos, blast.targetPosition)), BLAST_PUSH); } // Hurts an entity the front reaches the first time, true then. static bool blast_first_hit(Entity_Effect& blast, Entity& entity, float damage) { if (EntityList::HitListContains(blast.hitList, entity)) return false; hit_list_insert(blast.hitList, entity); entity.TakeDamage(damage, BLAST_DAMAGE_FLAGS, EntityRef(blast.spawnerEntity.get()), BLAST_HIT_COUNTDOWN); return true; } // The front reaches an entity: enemies are pushed and hurt (Mega Bean's freezes and poisons them once, Mama Mega's // keeps hurting them a little), enemy shots vanish, machines and pickups are pushed and hurt, small critters die. static void blast_entity_hit(Entity_Effect& blast, Entity& entity, float tickDamage) { float damage = blast.collisionDamage; EntityRef source(blast.spawnerEntity.get()); if (entity.IsEnemy() && !entity.HasEntityFlags(FLAG_FRIENDLY) && !entity.isDead) { entity.AddVelocity(blast_push(blast, entity), false); if (blast_first_hit(blast, entity, damage)) { if (blast.variant != EFFECT_MEGA_BEAN_EXPLOSION) return; entity.AddFreeze(source, BEAN_FREEZE, false); entity.AddPoison(source, BEAN_POISON, BEAN_POISON_DAMAGE, false); } else if (blast.variant != EFFECT_MEGA_BEAN_EXPLOSION) entity.TakeDamage(tickDamage, BLAST_DAMAGE_FLAGS, source, BLAST_HIT_COUNTDOWN); return; } if (entity.IsProjectile()) { auto& shot = static_cast(entity); if (shot.projectileFlags & PROJECTILE_CANT_HIT_PLAYER) return; shot.projectileFlags = 0; shot.isDead = true; } else if (entity.is_slot()) { entity.AddVelocity(blast_push(blast, entity), false); if (!entity.isDead) entity.TakeDamage(damage, BLAST_DAMAGE_FLAGS, source, BLAST_HIT_COUNTDOWN); } else if (entity.is_pickup()) { entity.AddVelocity(blast_push(blast, entity), false); blast_first_hit(blast, entity, damage); } else if (entity.type == ENTITY_EFFECT && std::find(BLAST_SWEPT_EFFECTS.begin(), BLAST_SWEPT_EFFECTS.end(), entity.variant) != BLAST_SWEPT_EFFECTS.end()) entity.isDead = true; } // A cloud of dust thrown up behind the room-wide front, spinning away from the cell, glowing for subtype 2, else in // the blast's faint colour. // The original aims it at the cell plus the target (not away from the target), kept here. static void blast_dust_throw(Entity_Effect& blast, Vector cell, Vector spot, Vector direction) { uint32_t seed = Random(); Vector velocity = vector_scale(direction, random_unit() * DEBRIS_SPEED_SPAN + DEBRIS_SPEED_MIN); auto* dust = static_cast(g_game->Spawn( {.position = spot, .velocity = velocity, .type = ENTITY_EFFECT, .variant = EFFECT_DUST_CLOUD, .seed = seed})); float side = vector_dot(vector_sub(dust->pos, cell), vector_make(direction.y, -direction.x)) < 0 ? -1.0f : 1.0f; dust->rotation = (random_unit() * BLAST_DUST_SPIN_SPAN + BLAST_DUST_SPIN_MIN) * side; dust->SetTimeout(random_below(BLAST_DUST_LIFE_SPAN) + BLAST_DUST_LIFE_MIN); if (blast.subtype == 2) { LayerState& layer = *dust->sprite.GetLayer(0); layer_additive_set(layer); layer.color = blast.variant == EFFECT_MAMA_MEGA_EXPLOSION ? MAMA_MEGA_GLOW : MEGA_BEAN_GLOW; } else { float tint = blast.variant == EFFECT_MAMA_MEGA_EXPLOSION ? 1 : BEAN_DUST_TINT; ColorMod color = tint_color_make(tint, 1, tint, random_unit() * BLAST_DUST_ALPHA_SPAN + BLAST_DUST_ALPHA_MIN); dust->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true); } float size = random_unit() * BLAST_DUST_SCALE_SPAN + BLAST_DUST_SCALE_MIN; dust->sprite.scale = vector_make(size, size); dust->Update(); } // A burst of blood behind the small front, larger for a longer reach, with a big splat under it. static void blast_blood_throw(Entity_Effect& blast, Vector spot, Vector direction) { int size = blast.maxRadius > BLOOD_LARGE_REACH ? 4 : blast.maxRadius > BLOOD_MEDIUM_REACH ? 3 : 2; uint32_t seed = Random(); Vector velocity = vector_scale(direction, random_unit() * DEBRIS_SPEED_SPAN + DEBRIS_SPEED_MIN); auto* blood = static_cast(g_game->Spawn({.position = spot, .velocity = velocity, .subType = size, .type = ENTITY_EFFECT, .variant = EFFECT_BLOOD_EXPLOSION, .seed = seed})); blood->rotation = random_unit() * DEGREES_PER_TURN; float scale = random_unit() * BLOOD_SCALE_SPAN + BLOOD_SCALE_MIN; blood->sprite.scale = vector_make(scale, scale); blood->Update(); Entity* splat = g_game->Spawn({.position = spot, .type = ENTITY_EFFECT, .variant = EFFECT_BLOOD_SPLAT, .seed = Random()}); splat->sprite.scale = vector_make(BLOOD_SPLAT_SCALE, BLOOD_SPLAT_SCALE); splat->Update(); } // The cells inside the front: the room-wide blast breaks their grid entities (the small one a quarter of them), and // half the cells well behind the front throw up dust or blood. static void blast_cells_hit(Entity_Effect& blast, float reachSquared) { Room& room = *g_game->room; for (int column = 0; column < room.gridWidth; column++) { for (int row = 0; row < room.gridHeight; row++) { int index = room.GetGridIndexByTile(column, row); Vector cell = room.GetGridPosition(index); float distance = vector_length_squared(vector_sub(cell, blast.targetPosition)); if (distance > reachSquared) continue; if (is_room_blast(blast) || random_below(4) == 0) { if (GridEntity* grid = room.GetGridEntity((uint32_t)index)) grid->Destroy(false, EntityRef(&blast)); } if (random_below(2) != 0 || !room.IsPositionInRoom(cell, DEBRIS_MARGIN)) continue; float trail = blast.scale - DEBRIS_TRAIL; if (trail * trail >= distance) continue; Vector scatter = random_direction(); Vector spot = vector_add(cell, vector_scale(scatter, random_unit() * DEBRIS_SCATTER)); Vector direction = vector_normalized(vector_add(spot, blast.targetPosition)); if (is_room_blast(blast)) blast_dust_throw(blast, cell, spot, direction); else blast_blood_throw(blast, spot, direction); } } } // 005fa30d (Update's case 127 and 202) The blast starts on its first frame, then every other frame its front moves // out, hurting and pushing what it reaches and breaking the room behind it, until it passes its reach. The room-wide // one may open the Boss Rush and Blue Womb doors once 300 out (or once dead), and rumbles on. static void mega_blast_update(Entity_Effect& blast) { const ColorMod& color = blast.variant == EFFECT_MAMA_MEGA_EXPLOSION ? MAMA_MEGA_COLOR : MEGA_BEAN_COLOR; if (blast.state == MEGA_BLAST_STARTING) return mega_blast_start(blast, color); if (blast.GetIntervalTicks(BLAST_TICK_FRAMES, 0) == 0) return; if (!is_room_blast(blast)) small_blast_rings(blast, color); float step = std::clamp(blast.maxRadius / BLAST_STEP_SHARE, BLAST_STEP_MIN, BLAST_STEP_MAX); blast.scale = std::min(blast.scale + step, blast.maxRadius); float share = blast.subtype == 0 ? BLAST_TICK_SHARE : SMALL_BLAST_TICK_SHARE; float tickDamage = std::max(blast.collisionDamage / share, 1.0f); float reachSquared = blast.scale * blast.scale; for (Entity* entity : g_game->room->entityList.updateEL) { if (entity == blast.spawnerEntity.get()) continue; if (vector_length_squared(vector_sub(entity->pos, blast.targetPosition)) <= reachSquared) blast_entity_hit(blast, *entity, tickDamage); } blast_cells_hit(blast, reachSquared); if (blast.maxRadius <= blast.scale) blast.Remove(); bool isDoorsTime = blast.isDead || blast.scale > BLAST_DOORS_REACH; if (blast.state == MEGA_BLAST_SWEEPING && is_room_blast(blast) && isDoorsTime) { blast.state = MEGA_BLAST_DOORS_DONE; Difficulty difficulty = g_game->difficulty; if (difficulty == DIFFICULTY_NORMAL || difficulty == DIFFICULTY_HARD) { g_game->room->TrySpawnBossRushDoor(true, false); g_game->room->TrySpawnBlueWombDoor(true, true, false); } } if (blast.subtype == 0) effect_sound_play(SOUND_EXPLOSION_DEBRIS, {.volume = MEGA_BLAST_RUMBLE_VOLUME, .frameDelay = MEGA_BLAST_SOUND_DELAY}); } // A small rocket (Epic Fetus' smaller ones) flies up from 5 at 30 a frame (10 more for each state past the first), // falling back at 6 a frame squared, faster the further its target (80 to 800 out, up to three times), and lands // on it with its player's rocket blast, ten times their damage plus 30 but at most twenty times. constexpr float SMALL_ROCKET_START_HEIGHT = -5; constexpr float SMALL_ROCKET_LIFT = -30; constexpr float SMALL_ROCKET_LIFT_STEP = -10; constexpr float SMALL_ROCKET_GRAVITY = 6; constexpr float SMALL_ROCKET_NEAR = 80; constexpr float SMALL_ROCKET_FAR = 800; constexpr float SMALL_ROCKET_FAR_LIFT = 3; constexpr float SMALL_ROCKET_TILT = 90; constexpr float SMALL_ROCKET_DAMAGE_SCALE = 10; constexpr float SMALL_ROCKET_DAMAGE_BASE = 30; constexpr float SMALL_ROCKET_DAMAGE_CAP = 20; // The small rocket's launch: up at its state's speed, aimed to come down on its target. static void small_rocket_launch(Entity_Effect& rocket) { rocket.sprite.Play("Flying"); rocket.height = SMALL_ROCKET_START_HEIGHT; rocket.fallingSpeed = SMALL_ROCKET_LIFT + (float)(rocket.state - 1) * SMALL_ROCKET_LIFT_STEP; rocket.fallingAcceleration = SMALL_ROCKET_GRAVITY; if (vector_is_zero(rocket.targetPosition)) return; float distance = vector_length(vector_sub(rocket.targetPosition, rocket.pos)); rocket.fallingSpeed *= lerp_range(distance, {SMALL_ROCKET_NEAR, SMALL_ROCKET_FAR, 1, SMALL_ROCKET_FAR_LIFT}); float root = std::sqrt(rocket.fallingSpeed * rocket.fallingSpeed - rocket.fallingAcceleration * 4 * rocket.height); float pace = rocket.fallingAcceleration / (root - rocket.fallingSpeed); rocket.velocity = vector_scale(vector_sub(rocket.targetPosition, rocket.pos), pace); } // 00605172 (Update's case 118) A launched small rocket (state 1 and up) arcs up and down, pointing along its flight, // and blows up with its player's tear effects once it lands. static void small_rocket_update(Entity_Effect& rocket) { if (rocket.state < 1) return; if (!rocket.sprite.animState.IsLoopingAnimation()) small_rocket_launch(rocket); rocket.height += rocket.fallingSpeed; rocket.fallingSpeed += rocket.fallingAcceleration; rocket.posOffset.y = rocket.height; rocket.sprite.rotation = vector_angle(vector_add(rocket.velocity, vector_make(0, rocket.fallingSpeed))) + SMALL_ROCKET_TILT; if (rocket.height < 0) return; Entity_Player* player = g_game->GetPlayer(0); if (rocket.parent && rocket.parent->is_player()) player = static_cast(rocket.parent.get()); TearParams params = player->GetTearHitParams(WEAPON_ROCKETS, 1, random_below(2) * 2 - 1, nullptr); if (is_rocket_burst(*player)) params.tearFlags.set(TEAR_SAD_BOMB); float damage = std::min(params.tearDamage * SMALL_ROCKET_DAMAGE_SCALE + SMALL_ROCKET_DAMAGE_BASE, params.tearDamage * SMALL_ROCKET_DAMAGE_CAP); explosion_effects({.tearFlags = params.tearFlags, .color = params.tearColor, .position = rocket.pos, .source = &rocket, .damage = damage}); rocket.Remove(); } // The reverse explosion (Reverse Tower) shows once its time is up: a blast, or (subtype 1) rubble in the room's rock // sheet that settles into a rock (80 in 100), a tinted rock (3) or an alternate rock (17) where one may stand. constexpr uint32_t RUBBLE_RNG_SHIFT = 3; constexpr uint32_t RUBBLE_ROCK_RNG_SHIFT = 35; constexpr int RUBBLE_ROCK_WEIGHT = 80; constexpr int RUBBLE_TINTED_WEIGHT = 3; constexpr int RUBBLE_ALT_WEIGHT = 17; // Part of 005cdb20 (Init's case 177) The reverse explosion waits unseen. static void reverse_explosion_init(Entity_Effect& explosion) { explosion.isVisible = false; } // The rubble shows in the room's rock sheet, flipped and one of two kinds at random. static void rubble_show(Entity_Effect& rubble) { RNG rng = rng_make(rubble.initSeed, RUBBLE_RNG_SHIFT); rubble.isVisible = true; Backdrop& backdrop = g_game->room->backdrop; const std::string& rocks = backdrop.configs[backdrop.backdropId].rocks; for (int i = 0; i < (int)rubble.sprite.layerStates.size(); i++) rubble.sprite.ReplaceSpritesheet(i, rocks); rubble.sprite.LoadGraphics(); rubble.sprite.isFlipX = rng.RandomInt(2) == 0; rubble.sprite.Play(rng.RandomInt(2) == 0 ? "Rubble02" : "Rubble01", PLAY_FORCED); } // The rubble settles into a rock of a random kind, if one may stand there. static void rubble_settle(Entity_Effect& rubble) { if (!room_can_spawn_obstacle_at(rubble.pos, false)) return; WeightedOutcomePicker picker; picker.AddOutcomeWeight(GRID_ROCK, RUBBLE_ROCK_WEIGHT); picker.AddOutcomeWeight(GRID_ROCKT, RUBBLE_TINTED_WEIGHT); picker.AddOutcomeWeight(GRID_ROCK_ALT, RUBBLE_ALT_WEIGHT); int type = picker.PickOutcome(rng_make(rubble.dropRNG.Next(), RUBBLE_ROCK_RNG_SHIFT)); uint32_t seed = rubble.dropRNG.Next(); Room& room = *g_game->room; room.SpawnGridEntity(room.GetGridIndex(rubble.pos), (GridEntityType)type, 0, seed, 0); } // 005df989 (Update's case 177) The reverse explosion shows once its time is up (the blast with its sound), then goes // once its animation ends, rubble settling into a rock. static void reverse_explosion_update(Entity_Effect& explosion) { if (explosion.timeout < 1 && !explosion.isVisible) { if (explosion.subtype == 1) rubble_show(explosion); else { explosion.isVisible = true; explosion.sprite.Play("Explosion", PLAY_FORCED); effect_sound_play(SOUND_REVERSE_EXPLOSION); } } if (!explosion.isVisible || !explosion.sprite.IsFinished()) return; explosion.Remove(); if (explosion.subtype == 1) rubble_settle(explosion); } // The fireworks: a launcher (subtype 0) sends a rocket (1) up every 30 frames at 1 to 3 a frame. A rocket climbs // against a growing fall, sparkling one frame in eight, and dies into a burst (2) and a ring of 5 to 7 stars (3) at // 10 to 11 a frame, in its colour or (one time in four) each a random one. Stars sparkle (4) every other frame, // wobbling, and sparks shrink. All glow. constexpr int FIREWORK_LAUNCHER = 0; constexpr int FIREWORK_ROCKET = 1; constexpr int FIREWORK_BURST = 2; constexpr int FIREWORK_STAR = 3; constexpr int FIREWORK_SPARK = 4; constexpr int FIREWORK_LAUNCH_FRAMES = 30; constexpr float FIREWORK_LAUNCH_SPEED_MIN = 1; constexpr float FIREWORK_LAUNCH_SPEED_SPAN = 2; constexpr int FIREWORK_ROCKET_LIFE_MIN = 10; constexpr int FIREWORK_ROCKET_LIFE_SPAN = 10; constexpr float FIREWORK_LIFT_MIN = 12; constexpr float FIREWORK_LIFT_SPAN = 4; constexpr float FIREWORK_GRAVITY = 0.4f; constexpr float FIREWORK_ROCKET_FRICTION = 0.98f; constexpr float FIREWORK_TILT = 90; constexpr int FIREWORK_STARS_MIN = 5; constexpr int FIREWORK_STARS_SPAN = 3; constexpr float FIREWORK_STAR_SPEED = 10; constexpr float FIREWORK_STAR_SPREAD_MIN = 0.75f; constexpr float FIREWORK_STAR_SPREAD_SPAN = 0.5f; constexpr int FIREWORK_STAR_LIFE_MIN = 30; constexpr int FIREWORK_STAR_LIFE_SPAN = 5; constexpr int FIREWORK_RAINBOW_ODDS = 4; constexpr float FIREWORK_BOOM_VOLUME = 0.5f; constexpr float FIREWORK_BOOM_PITCH = 1.2f; constexpr int FIREWORK_SPARKLE_ODDS = 8; constexpr float FIREWORK_SPARK_CARRY = 0.1f; constexpr float FIREWORK_SPARK_JITTER = 0.5f; constexpr float FIREWORK_SPARK_SCALE_MIN = 0.5f; constexpr float FIREWORK_ROCKET_SPARK_SCALE_SPAN = 0.2f; constexpr float FIREWORK_STAR_SPARK_SCALE_SPAN = 0.5f; constexpr int FIREWORK_SPARK_LIFE = 20; constexpr int FIREWORK_STAR_SPARK_FRAMES = 2; constexpr float FIREWORK_STAR_WOBBLE = 0.4f; constexpr float FIREWORK_STAR_WOBBLE_RATE = 0.5f; constexpr int FIREWORK_STAR_WOBBLE_PHASE = 15; constexpr float FIREWORK_STAR_FRICTION = 0.9f; constexpr float FIREWORK_SPARK_FRICTION = 0.99f; constexpr float FIREWORK_SPARK_SHRINK = 0.97f; constexpr float FIREWORK_STAR_COLORIZE = 0.5f; constexpr std::array FIREWORK_PARTICLES = {"Particle1", "Particle2", "Particle3", "Particle4"}; // A random bright tint, its three parts summing to one. static ColorMod firework_rainbow() { float red = random_unit(); float green = random_unit(); float blue = random_unit(); float share = 1 / (red + green + blue); return tint_color_make(red * share, green * share, blue * share, 1); } // Part of 005cdb20 (Init's case 104) A launcher is unseen. A rocket flies up in a random bright colour. A burst // explodes, a star or spark is one of four particles. All but rockets take a random turn, and all glow. static void firework_init(Entity_Effect& firework) { if (firework.subtype == FIREWORK_LAUNCHER) firework.isVisible = false; else if (firework.subtype == FIREWORK_ROCKET) { firework.sprite.Play("Projectile"); firework.height = -(random_unit() * FIREWORK_LIFT_SPAN + FIREWORK_LIFT_MIN); firework.SetColor(firework_rainbow(), -1, COLOR_PRIORITY_DEFAULT, false, true); } else if (firework.subtype == FIREWORK_BURST) firework.sprite.Play("Explode"); else firework.sprite.Play(FIREWORK_PARTICLES[random_below((int)FIREWORK_PARTICLES.size())]); if (firework.subtype != FIREWORK_ROCKET) firework.sprite.rotation = random_unit() * DEGREES_PER_TURN - DEGREES_PER_TURN / 2; layer_additive_set(*firework.sprite.GetLayer(0)); } // A firework part of the given subtype from this one, at its height. static Entity_Effect* firework_spawn(Entity_Effect& from, int subtype, Vector velocity, uint32_t seed) { auto* part = static_cast(g_game->Spawn({.position = from.pos, .velocity = velocity, .spawner = &from, .subType = subtype, .type = ENTITY_EFFECT, .variant = EFFECT_FIREWORKS, .seed = seed})); part->posOffset = from.posOffset; return part; } // A spark trailing a rocket or star: carried along, jittered, of a random size, lasting longer the faster it went. static Entity_Effect* firework_spark(Entity_Effect& from, float scaleSpan) { uint32_t seed = Random(); Vector carry = vector_scale(from.velocity, FIREWORK_SPARK_CARRY); Vector jitter = random_direction(); jitter = vector_scale(jitter, random_unit() * FIREWORK_SPARK_JITTER); Entity_Effect* spark = firework_spawn(from, FIREWORK_SPARK, vector_add(carry, jitter), seed); float scale = random_unit() * scaleSpan + FIREWORK_SPARK_SCALE_MIN; spark->sprite.scale = vector_make(scale, scale); spark->SetTimeout((int)vector_length(from.velocity) + FIREWORK_SPARK_LIFE + random_below(FIREWORK_SPARK_LIFE)); return spark; } // A rocket's end: a burst in its colour and a ring of stars, all its colour or each a random one, with a boom. static void firework_explode(Entity_Effect& rocket) { Entity_Effect* burst = firework_spawn(rocket, FIREWORK_BURST, {}, Random()); burst->SetColor(rocket.color, -1, COLOR_PRIORITY_DEFAULT, false, true); burst->Update(); int stars = random_below(FIREWORK_STARS_SPAN) + FIREWORK_STARS_MIN; float spread = random_unit() * FIREWORK_STAR_SPREAD_SPAN + FIREWORK_STAR_SPREAD_MIN; bool isRainbow = random_below(FIREWORK_RAINBOW_ODDS) == 0; for (int i = 0; i < stars; i++) { uint32_t seed = Random(); Vector way = vector_from_angle((float)i / (float)stars * DEGREES_PER_TURN); Vector velocity = vector_scale(way, (random_unit() + FIREWORK_STAR_SPEED) * spread); Entity_Effect* star = firework_spawn(rocket, FIREWORK_STAR, velocity, seed); star->SetTimeout(random_below(FIREWORK_STAR_LIFE_SPAN) + FIREWORK_STAR_LIFE_MIN); star->SetColor(isRainbow ? firework_rainbow() : rocket.color, -1, COLOR_PRIORITY_DEFAULT, false, true); star->Update(); } rocket.Remove(); effect_sound_play(SOUND_BOSS_EXPLOSION, {.volume = FIREWORK_BOOM_VOLUME, .pitch = FIREWORK_BOOM_PITCH}); } // A rocket climbing: its fall grows, it bursts once dead, points along its flight and sometimes sparkles. static void firework_rocket_update(Entity_Effect& rocket) { rocket.posOffset.y += rocket.height; rocket.height += FIREWORK_GRAVITY; rocket.friction *= FIREWORK_ROCKET_FRICTION; if (rocket.isDead) firework_explode(rocket); Vector flight = vector_make(rocket.velocity.x, rocket.height + rocket.velocity.y); rocket.sprite.rotation = vector_angle(flight) + FIREWORK_TILT; if (random_below(FIREWORK_SPARKLE_ODDS) != 0) return; Entity_Effect* spark = firework_spark(rocket, FIREWORK_ROCKET_SPARK_SCALE_SPAN); spark->SetColor(rocket.color, -1, COLOR_PRIORITY_DEFAULT, false, true); spark->Update(); } // A star: every other frame a spark in its colour colorized at random, and it wobbles as it slows. static void firework_star_update(Entity_Effect& star) { if (star.IsFrame(FIREWORK_STAR_SPARK_FRAMES, 0)) { Entity_Effect* spark = firework_spark(star, FIREWORK_STAR_SPARK_SCALE_SPAN); float red = random_unit(); float green = random_unit(); float blue = random_unit(); float share = 1 / (red + green + blue); ColorMod color = star.color; color._colorize[0] = red * share; color._colorize[1] = green * share; color._colorize[2] = blue * share; color._colorize[3] = FIREWORK_STAR_COLORIZE; spark->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true); spark->Update(); } float jitter = random_unit() - 0.5f; float phase = (float)(g_game->frameCount + star.index * FIREWORK_STAR_WOBBLE_PHASE); float wobble = std::sin(phase * FIREWORK_STAR_WOBBLE_RATE) * vector_length(star.velocity) * FIREWORK_STAR_WOBBLE; star.velocity = tear_vector_rotate(star.velocity, jitter + wobble); star.friction *= FIREWORK_STAR_FRICTION; } // 0060002b (Update's case 104) Each firework part plays its part: the launcher launches, the rocket climbs and bursts, // the burst goes once shown, stars sparkle and sparks shrink. Any part goes once dead. static void firework_update(Entity_Effect& firework) { if (firework.subtype == FIREWORK_LAUNCHER) { if (firework.IsFrame(FIREWORK_LAUNCH_FRAMES, 0)) { uint32_t seed = Random(); Vector way = random_direction(); Vector velocity = vector_scale(way, random_unit() * FIREWORK_LAUNCH_SPEED_SPAN + FIREWORK_LAUNCH_SPEED_MIN); Entity_Effect* rocket = firework_spawn(firework, FIREWORK_ROCKET, velocity, seed); rocket->SetTimeout(random_below(FIREWORK_ROCKET_LIFE_SPAN) + FIREWORK_ROCKET_LIFE_MIN); rocket->Update(); } } else if (firework.subtype == FIREWORK_ROCKET) firework_rocket_update(firework); else if (firework.subtype == FIREWORK_BURST) { if (firework.sprite.IsFinished()) firework.Remove(); } else if (firework.subtype == FIREWORK_STAR) firework_star_update(firework); else if (firework.subtype == FIREWORK_SPARK) { firework.friction *= FIREWORK_SPARK_FRICTION; firework.sprite.scale = vector_scale(firework.sprite.scale, FIREWORK_SPARK_SHRINK); } if (firework.isDead) firework.Remove(); firework.state++; } constexpr auto BLAST_EFFECT_BEHAVIOURS = std::to_array({ {EFFECT_MAMA_MEGA_EXPLOSION, mega_blast_init, effect_finishing, nullptr}, {EFFECT_MEGA_BEAN_EXPLOSION, mega_blast_init, effect_finishing, nullptr}, {EFFECT_SMALL_ROCKET, nullptr, effect_finishing, nullptr}, {EFFECT_REVERSE_EXPLOSION, reverse_explosion_init, effect_finishing, nullptr}, {EFFECT_FIREWORKS, firework_init, effect_finishing, nullptr}, }); std::span blast_effect_behaviours() { return BLAST_EFFECT_BEHAVIOURS; }