359 lines
18 KiB
C++
359 lines
18 KiB
C++
#include "entity_bomb.h"
|
|
#include "boss_ai.h"
|
|
#include "entity_combat.h"
|
|
#include "entity_player.h"
|
|
#include "entity_save_state.h"
|
|
#include "entity_tear.h"
|
|
#include "game.h"
|
|
#include "log.h"
|
|
#include "npc_ai.h"
|
|
#include "room.h"
|
|
#include "room_descriptor.h"
|
|
#include "room_grid.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <cmath>
|
|
#include <set>
|
|
|
|
// An explosion's reach by its damage, a giga bomb's, and the crater's size at the smallest reach.
|
|
constexpr float RADIUS_SMALL = 75;
|
|
constexpr float RADIUS_MEDIUM = 90;
|
|
constexpr float RADIUS_LARGE = 105;
|
|
constexpr float DAMAGE_MEDIUM = 140;
|
|
constexpr float DAMAGE_LARGE = 175;
|
|
|
|
// Safety Scissors shrink the explosions no player set off: by a quarter for two, by half for more.
|
|
constexpr int TRINKET_SAFETY_SCISSORS = 63;
|
|
constexpr float SCISSORS_DOUBLE_SCALE = 0.75f;
|
|
constexpr float SCISSORS_MORE_SCALE = 0.5f;
|
|
|
|
// The shake of a strong explosion, the push it gives (less for heavy entities, a player's capped), and the damage a
|
|
// player or familiar takes: half a heart, a whole one from a strong explosion, a fifth of it in a versus match.
|
|
constexpr float SHAKE_DAMAGE = 166;
|
|
constexpr float SHAKE_DAMAGE_CROSS = 125;
|
|
constexpr int SHAKE_BASE = 7;
|
|
constexpr int SHAKE_EXTRA = 7;
|
|
constexpr float PUSH_MAX = 15;
|
|
constexpr float PUSH_HEAVY_MASS = 20;
|
|
constexpr float PUSH_HEAVY_SCALE = 0.5f;
|
|
constexpr float PUSH_VELOCITY_KEPT = 0.5f;
|
|
constexpr float PUSH_PLAYER_MAX = 10;
|
|
constexpr float PLAYER_DAMAGE_STRONG = 85;
|
|
constexpr float DEATHMATCH_DAMAGE_SCALE = 0.2f;
|
|
constexpr float IMMOVABLE_MASS = 1000;
|
|
constexpr int HIT_COUNTDOWN = 30;
|
|
constexpr float TEAR_EFFECT_DAMAGE = 3.5f;
|
|
constexpr uint64_t DAMAGE_NO_PENALTIES = 1ull << 28;
|
|
|
|
// The grid an explosion breaks: rays cast every so far around its reach, stepping so far, and a line walked in steps.
|
|
constexpr float RAY_DENSITY = 8;
|
|
constexpr float RAY_STEP = 20;
|
|
constexpr float LINE_STEP = 10;
|
|
|
|
// The entities and effects an explosion treats apart.
|
|
constexpr int ENTITY_MEGA_SATAN = 274;
|
|
constexpr int CHALLENGE_BLUE_BOMBER = 23;
|
|
constexpr int EFFECT_ISAACS_CARPET = 74;
|
|
constexpr int CARPET_BLOWN = 1000;
|
|
constexpr std::array<int, 6> EFFECTS_BLOWN_AWAY = {21, 33, 63, 64, 158, 159};
|
|
constexpr int BOMB_EXPLOSION_EFFECT = 1;
|
|
constexpr int BOMB_CRATER_EFFECT = 18;
|
|
constexpr int FIREWORKS_EFFECT = 104;
|
|
constexpr int SPECIAL_DAILY_FIREWORKS = 12;
|
|
constexpr int TRINKET_BROKEN_PADLOCK = 136;
|
|
constexpr size_t SAVED_EFFECTS_MAX = 128;
|
|
|
|
// The colors poison, butt and brimstone bombs tint the explosion with.
|
|
constexpr ColorMod POISON_COLOR = {{0.4f, 0.97f, 0.5f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod BUTT_COLOR = {{0.4431373f, 0.2901961f, 0.06666667f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod BRIMSTONE_COLOR = {{1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
|
|
// The Mom's Shovel quest: a bomb in the first floor's start room while the quest is on digs the shovel up.
|
|
constexpr uint32_t LEVEL_STATE_SHOVEL_QUEST = 1u << 16;
|
|
constexpr int COLLECTIBLE_BROKEN_SHOVEL = 550;
|
|
|
|
// Whether Safety Scissors shrink an explosion: one no player or player's entity set off.
|
|
static bool is_scissored(Entity* source)
|
|
{
|
|
return !source || (source->type != ENTITY_PLAYER && !source->GetPlayerAncestor());
|
|
}
|
|
|
|
// Room::CheckLine's explosion mode (2): the line walked in steps, cell by cell. A metal block stops it, and so does a
|
|
// wall, unless it is a door, a wall the check ignores, or a lock Broken Padlock lets the blast through.
|
|
static bool explosion_line_clear(const Vector& start, const Vector& end, bool isIgnoreWalls)
|
|
{
|
|
Room& room = *g_game->room;
|
|
Vector step = vector_sub(start, end);
|
|
float length = vector_length(step);
|
|
if (length <= 0) return true;
|
|
|
|
step = vector_scale(step, LINE_STEP / length);
|
|
Vector at = start;
|
|
for (float walked = 0; walked < length; walked += LINE_STEP)
|
|
{
|
|
at = vector_sub(at, step);
|
|
int gridIndex = room.GetGridIndex(at);
|
|
uint32_t collision = room.GetGridCollision(gridIndex);
|
|
GridEntity* grid = gridIndex >= 0 ? room.GetGridEntity((uint32_t)gridIndex) : nullptr;
|
|
if (grid && collision != COLLISION_NONE && grid->type == GRID_ROCKB) return false;
|
|
if (collision != COLLISION_WALL) continue;
|
|
|
|
bool isPadlock = grid && grid->type == GRID_LOCK && g_game->playerManager.AnyoneHasTrinket(TRINKET_BROKEN_PADLOCK);
|
|
bool isOpening = grid && (grid->type == GRID_DOOR || (grid->type == GRID_WALL && isIgnoreWalls));
|
|
if (!isPadlock && !isOpening) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Pushes an entity away from an explosion, harder the deeper inside it is, a heavy one half as far and with Knockback
|
|
// twice as far. A player keeps its push and half its speed, at most so fast.
|
|
// Past a mass of 20 the original's further tiers (30, 50, 150) are never reached. They are left out.
|
|
static void explosion_push(const Explosion& explosion, Entity& entity, const TearFlags& flags, Vector away, float distance)
|
|
{
|
|
float force = std::min(explosion.radius + entity.size - distance, PUSH_MAX);
|
|
if (entity.mass > PUSH_HEAVY_MASS) force *= PUSH_HEAVY_SCALE;
|
|
bool isPlayer = entity.type == ENTITY_PLAYER;
|
|
if (flags[TEAR_KNOCKBACK] && !isPlayer) force += force;
|
|
|
|
Vector pushed = vector_add(vector_scale(away, force / distance), vector_scale(entity.velocity, PUSH_VELOCITY_KEPT));
|
|
if (isPlayer) pushed = vector_resized(pushed, std::min(vector_length(pushed), PUSH_PLAYER_MAX));
|
|
entity.velocity = pushed;
|
|
}
|
|
|
|
// Hurts a player or familiar caught in an explosion: half a heart, a whole one for a strong explosion.
|
|
static void explosion_player_hurt(const Explosion& explosion, Entity& entity, bool isPlayerSource)
|
|
{
|
|
if (explosion.damage <= 0) return;
|
|
|
|
EntityRef source(explosion.source);
|
|
if (isPlayerSource) source.flags |= ENTITY_REF_FRIENDLY;
|
|
float damage = explosion.damage < PLAYER_DAMAGE_STRONG ? 1.0f : 2.0f;
|
|
if (explosion.damageFlags & DAMAGE_NO_PENALTIES) damage = 1;
|
|
if (g_game->deathmatchManager.is_active()) damage = explosion.damage * DEATHMATCH_DAMAGE_SCALE;
|
|
if (entity.type == ENTITY_PLAYER && entity.ToPlayer()->is_evil_charm_immune(source)) return;
|
|
entity.TakeDamage(damage, explosion.damageFlags, source, HIT_COUNTDOWN);
|
|
}
|
|
|
|
// Hurts an entity caught in an explosion (not a player or familiar): its damage, the blue flies and other flags the
|
|
// source's tear effects add, then those effects on a vulnerable enemy. Ambient effects are blown away.
|
|
static void explosion_entity_hurt(Explosion& explosion, Entity& entity, const TearFlags& flags)
|
|
{
|
|
if (entity.type == ENTITY_EFFECT)
|
|
{
|
|
if (std::ranges::count(EFFECTS_BLOWN_AWAY, entity.variant))
|
|
{
|
|
entity.isDead = true;
|
|
return;
|
|
}
|
|
if (entity.variant == EFFECT_ISAACS_CARPET)
|
|
{
|
|
if (entity.subtype != 0) return;
|
|
reconstruction_missing("Game::BombDamage 006ff2a0: the hole under Isaac's carpet (stairs or a trapdoor)");
|
|
entity.subtype = CARPET_BLOWN;
|
|
return;
|
|
}
|
|
}
|
|
if (flags[TEAR_CROSS_BOMB]) reconstruction_missing("Game::BombDamage 006ff2a0: Bomber Boy's shared damage");
|
|
|
|
TearFlags effects = flags;
|
|
effects.reset(TEAR_EXPLOSIVE);
|
|
explosion.damageFlags |= tear_damage_flags_get(effects, explosion.source);
|
|
entity.TakeDamage(explosion.damage, explosion.damageFlags, EntityRef(explosion.source), HIT_COUNTDOWN);
|
|
|
|
Entity* source = explosion.source;
|
|
bool isTearEffecting = flags.any() && source && !Entity::is_enemy_type(source->spawnerType);
|
|
if (isTearEffecting && entity.IsVulnerableEnemy(nullptr))
|
|
Entity_Tear::ApplyTearFlagEffects(&entity, explosion.position, effects, source, TEAR_EFFECT_DAMAGE);
|
|
}
|
|
|
|
// 00711fd0 Blows a lock block open: Broken Padlock opens a closed one, otherwise it is hurt as by any explosion.
|
|
static bool lock_blast(GridEntity& lock)
|
|
{
|
|
if (lock.state == 0 && g_game->playerManager.AnyoneHasTrinket(TRINKET_BROKEN_PADLOCK))
|
|
reconstruction_missing("GridEntity_Lock::break_open 00711bb0: a lock Broken Padlock opens for no player");
|
|
return lock.Hurt(4, EntityRef());
|
|
}
|
|
|
|
// The cells an explosion reaches: its own, and every grid entity along rays cast around it, stopping at metal blocks.
|
|
static std::set<int> explosion_cells_get(const Explosion& explosion)
|
|
{
|
|
Room& room = *g_game->room;
|
|
std::set<int> cells;
|
|
int rays = (int)std::ceil(explosion.radius * RAY_DENSITY / GRID_CELL_SIZE);
|
|
float rayAngle = 2 * PI_F / (float)rays;
|
|
int center = room.GetGridIndex(explosion.position);
|
|
if (center >= 0) cells.insert(center);
|
|
|
|
float angle = 0;
|
|
for (int ray = 0; ray < rays; ray++, angle += rayAngle)
|
|
{
|
|
Vector direction = vector_make(std::cos(angle), std::sin(angle));
|
|
for (float walked = 0; walked < explosion.radius;)
|
|
{
|
|
walked = std::min(walked + RAY_STEP, explosion.radius);
|
|
GridEntity* grid = room_grid_entity_at(vector_add(explosion.position, vector_scale(direction, walked)));
|
|
if (!grid) continue;
|
|
|
|
cells.insert(grid->gridIndex);
|
|
if (grid->type == GRID_ROCKB) break;
|
|
}
|
|
}
|
|
return cells;
|
|
}
|
|
|
|
// Breaks the grid entities an explosion reaches in a clear line. A solid one that breaks fills the pit behind it.
|
|
static void explosion_grid_break(const Explosion& explosion)
|
|
{
|
|
Room& room = *g_game->room;
|
|
for (int gridIndex : explosion_cells_get(explosion))
|
|
{
|
|
Vector cell = room.GetGridPosition(gridIndex);
|
|
if (!explosion_line_clear(explosion.position, cell, false)) continue;
|
|
|
|
uint32_t collision = room.GetGridCollision(gridIndex);
|
|
GridEntity* grid = room.GetGridEntity((uint32_t)gridIndex);
|
|
bool isBroken = grid && grid->type == GRID_LOCK && lock_blast(*grid);
|
|
if (!isBroken) isBroken = room_grid_destroy({.source = EntityRef(explosion.source), .gridIndex = gridIndex});
|
|
if (!isBroken || collision != COLLISION_SOLID) continue;
|
|
|
|
Vector beyond = vector_add(cell, vector_scale(vector_cardinal(vector_sub(cell, explosion.position)), GRID_CELL_SIZE));
|
|
int beyondIndex = room.GetGridIndex(beyond);
|
|
if (beyondIndex >= 0) room_bridge_make(room.GetGridEntity((uint32_t)beyondIndex), grid);
|
|
}
|
|
}
|
|
|
|
// 006ff2a0 An explosion's damage: a strong one shakes the screen, everything in its reach and in a clear line is
|
|
// pushed away and hurt (players and familiars as by any explosion), and the grid it reaches breaks. Safety Scissors
|
|
// shrink one no player set off.
|
|
void explosion_damage(Explosion explosion)
|
|
{
|
|
if (is_scissored(explosion.source))
|
|
{
|
|
int scissors = g_game->playerManager.GetTrinketMultiplier(TRINKET_SAFETY_SCISSORS);
|
|
if (scissors == 2) explosion.radius *= SCISSORS_DOUBLE_SCALE;
|
|
else if (scissors > 2) explosion.radius *= SCISSORS_MORE_SCALE;
|
|
}
|
|
bool isCross = explosion.tearFlags[TEAR_CROSS_BOMB];
|
|
if (explosion.damage >= SHAKE_DAMAGE || (isCross && explosion.damage >= SHAKE_DAMAGE_CROSS))
|
|
game_shake_screen((int)(Random() % SHAKE_EXTRA) + SHAKE_BASE);
|
|
|
|
Entity* source = explosion.source;
|
|
bool isPlayerSource = source && source->type == ENTITY_PLAYER && !explosion.isDamageSource;
|
|
TearFlags flags = explosion.tearFlags;
|
|
for (Entity* entity : entity_query_radius(explosion.position, explosion.radius, QUERY_ALL))
|
|
{
|
|
int type = entity->type;
|
|
bool isSkipped = (entity == source && !explosion.isDamageSource) || entity->isDead ||
|
|
entity->entityCollisionClass == ENTCOLL_NONE || entity->entityGridCollisionClass == GRIDCOLL_BULLET ||
|
|
entity->mass == 0 || type == ENTITY_TEAR || type == ENTITY_PROJECTILE;
|
|
bool isMegaSatanSpared = source && type == ENTITY_MEGA_SATAN && source->type == ENTITY_MEGA_SATAN &&
|
|
g_game->challenge != CHALLENGE_BLUE_BOMBER;
|
|
if (isSkipped || isMegaSatanSpared) continue;
|
|
|
|
Vector away = vector_sub(entity->pos, explosion.position);
|
|
float distance = vector_length(away);
|
|
if (explosion.isLineCheck && !explosion_line_clear(explosion.position, entity->pos, true)) continue;
|
|
if (source && source->type == ENTITY_BOMB) flags |= static_cast<Entity_Bomb*>(source)->tearFlags;
|
|
|
|
bool isPlayerSpared = type == ENTITY_PLAYER && (entity->mass == IMMOVABLE_MASS || (source && source->IsFriendly()) ||
|
|
isPlayerSource);
|
|
if (distance > 0 && type != ENTITY_EFFECT && !entity->HasEntityFlags(FLAG_NO_KNOCKBACK) && !isPlayerSpared)
|
|
explosion_push(explosion, *entity, flags, away, distance);
|
|
|
|
if (type == ENTITY_PLAYER || type == ENTITY_FAMILIAR) explosion_player_hurt(explosion, *entity, isPlayerSource);
|
|
else explosion_entity_hurt(explosion, *entity, flags);
|
|
}
|
|
explosion_grid_break(explosion);
|
|
}
|
|
|
|
// 007009a0 The tear effects an explosion spreads (Butt, Sad, Hot, Glitter, Sticky Bombs' eggs, Bob's Curse, Blood,
|
|
// Brimstone and Giga Bombs, the Mysterious Liquid). A plain explosion has none.
|
|
void explosion_tear_flag_effects(const Explosion& explosion)
|
|
{
|
|
constexpr std::array<int, 10> SPREAD_FLAGS = {TEAR_BUTT_BOMB, TEAR_SAD_BOMB, TEAR_MYSTERIOUS_LIQUID_CREEP, TEAR_BURN,
|
|
TEAR_GLITTER_BOMB, TEAR_EGG_FLAG, TEAR_POISON, TEAR_BLOOD_BOMB,
|
|
TEAR_BRIMSTONE_BOMB, TEAR_GIGA_BOMB};
|
|
Entity* source = explosion.source;
|
|
bool isBrimstoneBomb = source && source->type == ENTITY_BOMB && source->variant == BOMB_BRIMSTONE;
|
|
bool isSpread = std::ranges::any_of(SPREAD_FLAGS, [&](int bit) { return explosion.tearFlags[bit]; });
|
|
if (isSpread || isBrimstoneBomb) reconstruction_missing("Game::BombTearflagEffects 007009a0: the bombs' tear effects");
|
|
}
|
|
|
|
// 008207a0 Digs Mom's Shovel up when a bomb goes off in the first floor's start room while its quest is on.
|
|
static void shovel_quest_dig()
|
|
{
|
|
Game& game = *g_game;
|
|
bool isQuestRoom = game.stage == 1 && (game.levelStateFlags & LEVEL_STATE_SHOVEL_QUEST) &&
|
|
game.currentRoomIdx == game.startingRoomIdx;
|
|
if (isQuestRoom && !game.itemPool.removedCollectibles[COLLECTIBLE_BROKEN_SHOVEL])
|
|
reconstruction_missing("Room 008207a0: Mom's Shovel dug up by a bomb");
|
|
}
|
|
|
|
// 00700060, 00700061 An explosion as a bomb makes it: its damage and reach (larger for a stronger one), a crater (smaller
|
|
// under Safety Scissors), the blast itself in the bomb's color (or the poison, butt or brimstone bomb's), and the tear
|
|
// effects it spreads.
|
|
void explosion_effects(const Explosion& explosion)
|
|
{
|
|
Entity* source = explosion.source;
|
|
if (source && explosion.tearFlags[TEAR_GIGA_BOMB])
|
|
reconstruction_missing("Game::BombExplosionEffects 00700061: the giga explosion's wave");
|
|
if (explosion.tearFlags[TEAR_CROSS_BOMB])
|
|
reconstruction_missing("Game::BombExplosionEffects 00700061: Bomber Boy's cross of explosions");
|
|
|
|
float radius = explosion.damage >= DAMAGE_LARGE ? RADIUS_LARGE : explosion.damage > DAMAGE_MEDIUM ? RADIUS_MEDIUM : RADIUS_SMALL;
|
|
float craterScale = radius / RADIUS_SMALL * explosion.radiusMultiplier;
|
|
if (is_scissored(source))
|
|
{
|
|
int scissors = g_game->playerManager.GetTrinketMultiplier(TRINKET_SAFETY_SCISSORS);
|
|
if (scissors == 2) craterScale *= SCISSORS_DOUBLE_SCALE;
|
|
else if (scissors > 2) craterScale *= SCISSORS_MORE_SCALE;
|
|
}
|
|
Explosion damaged = explosion;
|
|
damaged.radius = radius * explosion.radiusMultiplier;
|
|
explosion_damage(damaged);
|
|
bomb_crater_spawn(explosion.position, nullptr, craterScale);
|
|
|
|
Entity* blast = g_game->Spawn({.position = explosion.position, .spawner = source, .type = ENTITY_EFFECT,
|
|
.variant = BOMB_EXPLOSION_EFFECT, .seed = Random()});
|
|
blast->SetColor(explosion.color, -1, 255, false, true);
|
|
const float* tint = explosion.color._tint;
|
|
bool isWhite = tint[0] == 1 && tint[1] == 1 && tint[2] == 1 && tint[3] == 1;
|
|
if (explosion.tearFlags[TEAR_POISON] && isWhite) blast->SetColor(POISON_COLOR, -1, 255, false, true);
|
|
if (explosion.tearFlags[TEAR_BUTT_BOMB]) blast->SetColor(BUTT_COLOR, -1, 255, false, true);
|
|
blast->sprite.scale = vector_make(craterScale, craterScale);
|
|
if (source && source->type == ENTITY_BOMB && source->variant == BOMB_BRIMSTONE)
|
|
blast->SetColor(BRIMSTONE_COLOR, -1, 255, false, true);
|
|
|
|
bool isFetusBomb = source && source->type == ENTITY_BOMB && static_cast<Entity_Bomb*>(source)->isFetus;
|
|
if (g_game->dailyChallenge._specialDailyId == SPECIAL_DAILY_FIREWORKS && isFetusBomb)
|
|
{
|
|
Entity* fireworks = g_game->Spawn({.position = explosion.position, .subType = 1, .type = ENTITY_EFFECT,
|
|
.variant = FIREWORKS_EFFECT, .seed = Random()});
|
|
fireworks->isDead = true;
|
|
fireworks->Update();
|
|
}
|
|
explosion_tear_flag_effects(damaged);
|
|
shovel_quest_dig();
|
|
}
|
|
|
|
// 00703190 Leaves a crater at a size, kept in the room's saved effects (the oldest go past 128).
|
|
Entity* bomb_crater_spawn(const Vector& position, Entity* spawner, float scale)
|
|
{
|
|
uint32_t seed = Random();
|
|
Entity* crater = g_game->Spawn({.position = position, .spawner = spawner, .type = ENTITY_EFFECT,
|
|
.variant = BOMB_CRATER_EFFECT, .seed = seed});
|
|
crater->sprite.scale = vector_make(scale, scale);
|
|
crater->Update();
|
|
|
|
std::vector<EntitySaveState>& saved = g_game->room->descriptor->savedEffects;
|
|
while (saved.size() + 1 > SAVED_EFFECTS_MAX) saved.erase(saved.begin());
|
|
EntitySaveState& state = saved.emplace_back();
|
|
state.type = ENTITY_EFFECT;
|
|
state.variant = BOMB_CRATER_EFFECT;
|
|
state.targetPosition = position;
|
|
state.initSeed = crater->initSeed;
|
|
state.intStorage1 = std::bit_cast<uint32_t>(scale);
|
|
return crater;
|
|
}
|