Files
isaac-port/src/boss_pin.cpp
T

926 lines
38 KiB
C++

#include "boss_ai.h"
#include "entity_config.h"
#include "entity_laser.h"
#include "entity_player.h"
#include "entity_projectile.h"
#include <algorithm>
#include <array>
// Pin's kinds, by variant: Pin, Scolex and the Frail run here, Wormwood has its own AI.
enum PinVariant
{
PIN_PIN,
PIN_SCOLEX,
PIN_FRAIL,
PIN_WORMWOOD
};
// Pin's states: underground, rising through the air, and its attacks. The Frail's second phase also sweeps a beam
// (ATTACK) or fires bursts of beams (ATTACK3).
constexpr int PIN_BURIED = NPC_STATE_IDLE;
constexpr int PIN_JUMPING = NPC_STATE_MOVE;
constexpr int PIN_SPITTING = NPC_STATE_ATTACK;
constexpr int PIN_ROARING = NPC_STATE_ATTACK2;
constexpr int PIN_BEAMS = NPC_STATE_ATTACK3;
constexpr int PIN_ATTACK_DIVE = 10;
constexpr int PIN_POPPED_UP = 4;
// The segments each kind grows behind its head, Scolex's thick and thin ones, and the delay (in trail points) each
// body segment follows its head by.
constexpr int PIN_SEGMENTS = 6;
constexpr int SCOLEX_SEGMENTS = 9;
constexpr int SCOLEX_THIN_FIRST = 4;
constexpr int SCOLEX_THICK = 7;
constexpr float SCOLEX_THICK_SIZE = 1.5f;
constexpr float SCOLEX_THIN_SIZE = 0.7f;
constexpr std::array<int, 6> PIN_DELAYS = {0, 0, 2, 4, 6, 8};
constexpr std::array<int, 9> SCOLEX_DELAYS = {0, 0, 3, 6, 10, 14, 18, 21, 23};
constexpr int SCOLEX_SHIELDING_SEGMENTS = 2;
constexpr float SCOLEX_SHIELD_SLACK = 0.0001f;
constexpr int TAIL_DEPTH_BELOW = 1000;
constexpr int RENDER_DEPTH_STEP = 5;
constexpr int HEALTH_SYNC_COUNTDOWN = 30;
// The trail the head leaves: a point every 3 of motion or while buried, the newest 30 kept once it passes 60.
constexpr float PIN_TRAIL_STEP = 3;
constexpr size_t PIN_TRAIL_MAX = 60;
constexpr size_t TRAIL_KEPT = 30;
// The jump out of the ground: its speeds, the lift it starts with, gravity, and the depth it buries at again.
constexpr float JUMP_SPEED = 8;
constexpr float FRAIL_JUMP_FAST = 10;
constexpr float FRAIL_JUMP_SLOW = 6;
constexpr float JUMP_LIFT = -2;
constexpr float JUMP_LIFT_PER_DISTANCE = 0.02f;
constexpr float GRAVITY = 0.4f;
constexpr float BURIED_DEPTH = 16;
constexpr float SURFACE_DEPTH = 8;
constexpr float RISING_DEPTH = 6;
constexpr float SPAWN_DEPTH = 26;
constexpr float PIN_SPIT_HEIGHT = -20;
constexpr int DIVE_SOUND_DELAY = 10;
// Underground it circles the player at 250 and is pulled toward the room's middle, rising once it lags 15 behind.
constexpr float UNDERGROUND_FRICTION = 0.95f;
constexpr float PLAYER_ORBIT = 250;
constexpr float PLAYER_ORBIT_PULL = 0.003f;
constexpr float CENTER_PULL_MAX = 200;
constexpr float CENTER_PULL = 0.002f;
constexpr float RISE_LAG = 15;
constexpr float RISE_LAG_PER_DISTANCE = 20;
constexpr float RISE_MIN_DISTANCE = 100;
// The attacks: when they fire, the spit's and the Frail's rings' shots, and the big lobbed shot.
constexpr int ROAR_FIRE_FRAME = 20;
constexpr int FRAIL_ROAR_FIRE_FRAME = 47;
constexpr int SPIT_FIRE_FRAME = 46;
constexpr int SCOLEX_SPIT_LAST_FRAME = 48;
constexpr int ATTACK_END_FRAME = 105;
constexpr float PIN_SHOT_HEIGHT = -40;
constexpr int FRAIL_RING_SHOTS = 12;
constexpr float FRAIL_RING_SPEED = 7;
constexpr ColorMod FRAIL_RING_COLOR = {{0.3f, 0.8f, 0.8f, 1}, {0, 0, 0, 0}, {140 / 255.0f, 140 / 255.0f, 140 / 255.0f}};
constexpr int ROAR_SHOTS = 15;
constexpr float CUTE_GRUNT_VOLUME = 0.8f;
constexpr float SPIT_RING_LIFT = -20;
constexpr float SPIT_RING_FALL = -0.06f;
constexpr float SPIT_RING_SPEED = 8;
constexpr float SPIT_RING_SHOTS = 6;
constexpr float BIG_SHOT_SPEED = 10;
constexpr float BIG_SHOT_FALL = -0.3f;
constexpr float PIN_BIG_SHOT_SCALE = 1.6f;
constexpr float BIG_SHOT_LIFT_SCALE = 0.7f;
constexpr float BIG_SHOT_LIFT = -13;
constexpr float BIG_SHOT_SLOW = 0.55f;
constexpr int SEGMENT_SPIT_COOLDOWN = 30;
constexpr int SEGMENT_SPITTER = 4;
constexpr float SEGMENT_CROSS_SPEED = 5;
// The Frail's beams: the tracer warning of them, the sweep after the player and the bursts.
constexpr float PIN_TRACER_HEIGHT = -80;
constexpr ColorMod PIN_TRACER_COLOR = {{0.6f, 0.05f, 0, 0.8f}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float PIN_TRACER_SCALE = 4;
constexpr int SWEEP_TRACER_FRAMES = 60;
constexpr int BURST_TRACER_FRAMES = 24;
constexpr int SWEEP_FRAMES = 80;
constexpr int BURSTS_FRAMES = 120;
constexpr int SWEEP_START = 20;
constexpr int SWEEP_LASER_FRAMES = 60;
constexpr float SWEEP_TURN = 0.05f;
constexpr float SWEEP_TURN_MIN = 1;
constexpr int BURST_LASER_FRAMES = 20;
constexpr std::array<int, 2> BURST_TRACERS = {110, 60};
constexpr std::array<int, 2> BURST_LASERS = {100, 50};
constexpr int GLOW_LATE_FIRST = 90;
constexpr int GLOW_LATE_LAST = 110;
constexpr int GLOW_EARLY_FIRST = 40;
constexpr int GLOW_EARLY_LAST = 60;
constexpr float BEAM_SIDE_OFFSET = 25;
constexpr float BEAM_OFFSET_X = -5;
constexpr float PIN_BEAM_HEIGHT = -70;
constexpr float BEAM_DEPTH_BELOW = 3000;
constexpr float BEAM_DEPTH_ABOVE = -10;
constexpr float SHOOT_ANIM_STEP = 45;
constexpr int SHOOT_ANIM_ANGLES = 360;
constexpr std::array<std::string_view, 8> SHOOT_ANIMS = {
"Attack3ShootLeft", "Attack3ShootUpLeft", "Attack3ShootUp", "Attack3ShootUpRight",
"Attack3ShootRight", "Attack3ShootDownRight", "Attack3ShootDown", "Attack3ShootDownLeft"};
// The tail's hole closing, by kind.
constexpr int HOLE_CLOSE_FRAME = 15;
constexpr int SCOLEX_HOLE_CLOSE_FRAME = 25;
// The Frail's second phase: its sheet, a Delirium form's health, and the blood and dust of the change and its death.
constexpr std::string_view FRAIL_PHASE_SHEET = "gfx/Bosses/Afterbirth/Boss_TheFrail2.png";
constexpr std::string_view PIN_BOSSES_FOLDER = "Bosses";
constexpr std::string_view DELIRIUM_FOLDER = "Bosses/AfterbirthPlus/DeliriumForms";
constexpr size_t GFX_FOLDER_SIZE = 4;
constexpr float DELIRIUM_FRAIL_HEALTH = 75;
constexpr int PHASE_GIBS = 6;
constexpr std::array<int, 3> PHASE_GIB_KINDS = {0, 2, 3};
constexpr int PHASE_GIB_HEAD_ONLY = 3;
constexpr float PHASE_GIB_SPEED_MIN = 2;
constexpr float PHASE_GIB_SPEED_RANGE = 4;
constexpr float PHASE_GIB_LIFT = 0.5f;
constexpr float PHASE_GIB_SPREAD = 0.5f;
constexpr int DEATH_DUST_MIN = 3;
constexpr float PIN_DEATH_DUST_SPEED = 5;
constexpr float DEATH_DUST_SPREAD = 20;
constexpr float DEATH_DUST_SPIN_MIN = 2;
constexpr float DEATH_DUST_SPIN_RANGE = 10;
constexpr int DEATH_DUST_LIFE_MIN = 15;
constexpr int DEATH_DUST_LIFE_RANGE = 15;
constexpr float DEATH_DUST_ALPHA_MIN = 0.2f;
constexpr float DEATH_DUST_ALPHA_RANGE = 0.5f;
// i2 holds the Frail's phase in its low half and its beam timer in its high half.
static int frail_phase(const Entity_NPC& npc)
{
return (short)npc.i2;
}
static int beam_timer(const Entity_NPC& npc)
{
return (short)(npc.i2 >> 16);
}
static void beam_timer_set(Entity_NPC& npc, int timer)
{
npc.i2 = (int)(((uint32_t)(uint16_t)timer << 16) | (uint16_t)npc.i2);
}
static void frail_phase_advance(Entity_NPC& npc)
{
npc.i2 = (int)(((uint32_t)npc.i2 & 0xffff0000u) | (uint16_t)(frail_phase(npc) + 1));
}
// The chain's ends and the segment's depth below its head.
struct PinChain
{
Entity_NPC* head = nullptr;
Entity_NPC* tail = nullptr;
int depth = 0;
};
static PinChain pin_chain_get(Entity_NPC& npc)
{
PinChain chain{.head = &npc, .tail = &npc};
for (Entity_NPC* link = npc.ParentNPC(); link; link = link->ParentNPC())
{
chain.head = link;
chain.depth++;
}
for (Entity_NPC* link = npc.ChildNPC(); link; link = link->ChildNPC()) chain.tail = link;
return chain;
}
// Plays an animation from its start, the way the AI restarts one.
static void anim_restart(Entity_NPC& npc, std::string_view name)
{
npc.sprite.Play(name, PLAY_FORCED);
}
static bool is_anim_done(Entity_NPC& npc)
{
return !npc.sprite.animState.animData || !npc.sprite.animState.isPlaying;
}
// Grows the head's segments, the last one the hole it rises from, and buries the head.
static void pin_init(Entity_NPC& npc)
{
if (npc.variant == PIN_FRAIL)
{
npc.AddEntityFlags(FLAG_NO_BLOOD_SPLASH);
npc.health *= 0.5f;
npc.maxHitPoints *= 0.5f;
npc.entityDeque.resize(1);
}
npc.ClearEntityFlags(FLAG_APPEAR);
npc.AddEntityFlags(FLAG_NO_DEATH_TRIGGER);
npc.entityCollisionClass = ENTCOLL_NONE;
npc.state = PIN_BURIED;
npc.i1 = 0;
int segments = npc.variant == PIN_SCOLEX ? SCOLEX_SEGMENTS : PIN_SEGMENTS;
Entity_NPC* last = &npc;
for (int i = 0; i < segments; i++)
{
auto* segment = static_cast<Entity_NPC*>(g_game->Spawn(
{.position = npc.pos, .spawner = &npc, .type = ENTITY_PIN, .variant = npc.variant, .seed = Random()}));
segment->ClearEntityFlags(FLAG_APPEAR);
segment->isVisible = false;
segment->parent = last;
segment->maxHitPoints = npc.maxHitPoints;
segment->health = npc.health;
last->child = segment;
segment->state = PIN_BURIED;
segment->entityCollisionClass = ENTCOLL_NONE;
last = segment;
if (npc.variant == PIN_SCOLEX && i >= SCOLEX_THIN_FIRST && i < SCOLEX_SEGMENTS - 1)
{
float size = segment->size * (i == SCOLEX_THICK ? SCOLEX_THICK_SIZE : SCOLEX_THIN_SIZE);
segment->SetSize(size, vector_make(-1, -1), -1);
}
if (npc.variant == PIN_FRAIL) segment->AddEntityFlags(FLAG_NO_BLOOD_SPLASH);
}
last->renderZOffset = TAIL_DEPTH_BELOW;
last->sprite.GetLayer(0)->pos = vector_make(0, 0);
last->AddEntityFlags(FLAG_NO_BLOOD_SPLASH);
}
// The Frail's second phase sheet on every layer drawing from its first sheet, a Delirium form's from its folder.
static void frail_sheet_load(Entity_NPC& npc, Entity_NPC& segment)
{
for (size_t layer = 0; layer < segment.sprite.layerStates.size(); layer++)
{
const LayerData* data = segment.sprite.layerStates[layer].layerData;
if (!data || data->spriteSheetId != 0) continue;
std::string path(FRAIL_PHASE_SHEET);
if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM))
path.replace(GFX_FOLDER_SIZE, PIN_BOSSES_FOLDER.size(), DELIRIUM_FOLDER);
segment.sprite.ReplaceSpritesheet((int)layer, npc_sheet_path_get(npc, path));
}
segment.sprite.LoadGraphics();
segment.sprite.Update();
}
// A Frail spawned in its second phase: every segment changes to it at full health, in its boss color.
static void frail_phase_start(Entity_NPC& npc)
{
for (auto* segment = static_cast<Entity_NPC*>(npc.GetLastParent()); segment; segment = segment->ChildNPC())
{
segment->maxHitPoints = npc.config->baseHP;
if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) segment->maxHitPoints = DELIRIUM_FRAIL_HEALTH;
frail_phase_advance(*segment);
segment->health = segment->maxHitPoints;
segment->isDead = false;
frail_sheet_load(npc, *segment);
// The original also reads the boss color one past the end of the list. Here it stays within.
int bossColor = npc.bosscoloridx;
if (bossColor >= 0 && bossColor < (int)npc.config->bossColors.size())
segment->SetColor(npc.config->bossColors[bossColor].color, -1, COLOR_PRIORITY_DEFAULT, false, true);
}
}
// The head shares its lowest health with every segment but the hole, as harmless hits.
static void pin_health_share(Entity_NPC& npc, const PinChain& chain)
{
float lowest = npc.health - npc.pendingDamage;
for (Entity_NPC* link = npc.ChildNPC(); link; link = link->ChildNPC()) lowest = std::min(lowest, link->health);
for (Entity_NPC* link = &npc; link; link = link->ChildNPC())
{
bool isDelirium = npc.HasEntityFlags(FLAG_DELIRIUM_FORM);
bool isHurt = isDelirium ? npc.pendingDamage > 0 && link->health - lowest > 0
: lowest < link->health - link->pendingDamage;
if (isHurt && link != chain.tail)
link->TakeDamage(link->health - lowest, DAMAGE_INVINCIBLE, EntityRef(&npc), HEALTH_SYNC_COUNTDOWN);
}
}
// Scolex's segments shrug off hits from anything but itself while more than two segments lie behind them.
static void scolex_shield(Entity_NPC& npc, const PinChain& chain)
{
int behind = 0;
for (Entity_NPC* link = npc.ChildNPC(); link && link != chain.tail; link = link->ChildNPC()) behind++;
if (behind <= SCOLEX_SHIELDING_SEGMENTS) return;
for (DamageEvent& event : npc.damageEvents)
{
if (event.source.type == npc.type) continue;
npc.pendingDamage -= event.damage + SCOLEX_SHIELD_SLACK;
event.damage = 0;
}
}
// The tracer that warns of a Frail beam, pointing at an angle.
static Entity_Effect* beam_tracer_spawn(Entity_NPC& npc, float angle, int frames)
{
return following_effect_spawn(npc, {.offset = vector_make(0, PIN_TRACER_HEIGHT),
.targetPosition = vector_from_angle(angle),
.color = PIN_TRACER_COLOR,
.scale = PIN_TRACER_SCALE,
.timeout = frames});
}
// A beam held at the Frail's mouth, drawn behind it while it points down.
static void beam_place(Entity_Laser& laser, float depthAngle)
{
Direction direction = vector_to_direction(vector_from_angle(laser.angleDegrees));
float side = direction == DIRECTION_RIGHT ? BEAM_SIDE_OFFSET : direction == DIRECTION_LEFT ? -BEAM_SIDE_OFFSET : 0;
laser.posOffset = vector_make(side + BEAM_OFFSET_X, PIN_BEAM_HEIGHT);
laser.renderDepthOffset = depthAngle >= 0 ? BEAM_DEPTH_BELOW : BEAM_DEPTH_ABOVE;
}
static Entity_Laser* beam_shoot(Entity_NPC& npc, float angle, int frames)
{
Entity_Laser* laser = Entity_Laser::ShootAngle(
{.position = npc.pos, .source = &npc, .angleDegrees = angle, .timeout = frames, .variant = 1});
npc.entityRef = laser;
return laser;
}
// The sweep: a beam turning after the player at a twentieth of the way each frame, at least a degree.
static void beam_sweep(Entity_NPC& npc, float targetAngle)
{
auto* laser = static_cast<Entity_Laser*>(npc.entityRef.get());
bool isNew = !laser;
if (isNew) laser = beam_shoot(npc, npc.v2.x, SWEEP_LASER_FRAMES);
float angle = angle_lerp(laser->angleDegrees, targetAngle, SWEEP_TURN);
float turn = normalize_angle_degrees(angle - laser->angleDegrees);
if (std::abs(turn) < SWEEP_TURN_MIN)
{
float step = std::min(SWEEP_TURN_MIN, std::abs(turn));
angle = normalize_angle_degrees(laser->angleDegrees + (turn <= 0 ? -step : step));
}
laser->set_aim_angle(angle);
Entity* tracer = npc.entityDeque.empty() ? nullptr : npc.entityDeque.front().get();
if (tracer) tracer->targetPosition = vector_from_angle(angle);
beam_place(*laser, laser->angleDegrees);
if (isNew) laser->Update();
}
// The bursts: tracers then beams at the player twice, the eyes glowing around each.
static void beam_bursts(Entity_NPC& npc, int timer)
{
if (std::ranges::find(BURST_TRACERS, timer) != BURST_TRACERS.end())
{
npc.v2.x = vector_angle(vector_sub(npc.CalcTargetPosition(0), npc.pos));
beam_tracer_spawn(npc, npc.v2.x, BURST_TRACER_FRAMES);
}
else if (std::ranges::find(BURST_LASERS, timer) != BURST_LASERS.end())
{
Entity_Laser* laser = beam_shoot(npc, npc.v2.x, BURST_LASER_FRAMES);
beam_place(*laser, npc.v2.x);
}
bool isGlowing = (timer >= GLOW_LATE_FIRST && timer <= GLOW_LATE_LAST) ||
(timer >= GLOW_EARLY_FIRST && timer <= GLOW_EARLY_LAST);
npc.sprite.GetLayer("Effect1")->isVisible = isGlowing;
npc.sprite.GetLayer("EyesGlow")->isVisible = isGlowing;
}
// The Frail's beam attacks: it rises, charges, fires its sweep or bursts while turning its head with the beam, then
// sinks again.
static void frail_beams_update(Entity_NPC& npc, const PinChain& chain)
{
float targetAngle = vector_angle(vector_sub(npc.CalcTargetPosition(0), npc.pos));
float headAngle = targetAngle;
if (auto* laser = static_cast<Entity_Laser*>(npc.entityRef.get())) headAngle = laser->angleDegrees;
if (npc.stateFrame == 0)
{
npc.sprite.Play("Attack3Start");
chain.tail->isVisible = false;
npc.entityCollisionClass = ENTCOLL_ALL;
npc.isVisible = true;
npc.shadowSize = 0;
npc.velocity = vector_make(0, 0);
npc.posOffset.y = 0;
}
else if (npc.stateFrame == 2)
{
int timer = beam_timer(npc);
if (npc.state == PIN_SPITTING && timer > SWEEP_START) beam_sweep(npc, targetAngle);
else if (npc.state != PIN_SPITTING) beam_bursts(npc, timer);
beam_timer_set(npc, beam_timer(npc) - npc_ticks_get(npc));
if (beam_timer(npc) < 1)
{
npc.stateFrame++;
npc.sprite.Play("Attack3End");
npc.sprite.GetLayer("Effect1")->isVisible = true;
npc.sprite.GetLayer("EyesGlow")->isVisible = true;
}
}
if (is_anim_done(npc))
{
if (npc.stateFrame == 0)
{
npc.sprite.Play("Attack3Charge");
npc.stateFrame++;
npc.v2.x = vector_angle(vector_sub(npc.CalcTargetPosition(0), npc.pos));
npc.PlaySound(SOUND_FRAIL_CHARGE, 1, 2, false, 1);
}
else if (npc.stateFrame == 1 || npc.stateFrame == 2)
{
if (npc.stateFrame == 1)
{
beam_timer_set(npc, npc.state == PIN_SPITTING ? SWEEP_FRAMES : BURSTS_FRAMES);
npc.stateFrame = 2;
npc.PlaySound(SOUND_MONSTER_ROAR_2, 1, 2, false, 1);
}
float angle = normalize_angle_degrees(headAngle + SHOOT_ANIM_STEP - SHOOT_ANIM_STEP / 2);
int index = std::clamp((int)(angle + 180), 0, SHOOT_ANIM_ANGLES - 1) / (int)SHOOT_ANIM_STEP;
anim_restart(npc, SHOOT_ANIMS[index]);
}
else
{
npc.state = PIN_BURIED;
npc.targetPosition = npc.CalcTargetPosition(0);
npc.stateFrame = 0;
npc.isVisible = false;
npc.entityCollisionClass = ENTCOLL_NONE;
}
}
npc.velocity = vector_make(0, 0);
}
// The head's trail, newest first: where it is and how high, a point each 3 it moves or each frame it is buried.
static void trail_record(Entity_NPC& npc)
{
bool isFar =
npc.vectorDeque1.empty() || vector_length(vector_sub(npc.pos, npc.vectorDeque1.front())) > PIN_TRAIL_STEP;
if (isFar || npc.posOffset.y > BURIED_DEPTH)
{
npc.vectorDeque1.push_front(npc.pos);
npc.vectorDeque2.push_front(npc.posOffset);
}
if (npc.vectorDeque1.size() <= PIN_TRAIL_MAX) return;
npc.vectorDeque1.erase(npc.vectorDeque1.begin() + TRAIL_KEPT, npc.vectorDeque1.end());
npc.vectorDeque2.erase(npc.vectorDeque2.begin() + TRAIL_KEPT, npc.vectorDeque2.end());
}
// Underground it circles the player and drifts to the middle of the room, rising again once it has fallen far enough
// behind its arc.
static void pin_tunnel(Entity_NPC& npc)
{
npc.entityCollisionClass = ENTCOLL_NONE;
npc.isVisible = false;
npc.friction *= UNDERGROUND_FRICTION;
Vector toPlayer = vector_sub(npc.GetPlayerTarget()->pos, npc.pos);
float playerDistance = vector_length(toPlayer);
if (std::abs(playerDistance) > 1)
{
float lag = PLAYER_ORBIT - playerDistance;
npc.AddVelocity_divided(vector_make(-toPlayer.x * lag / playerDistance * PLAYER_ORBIT_PULL,
-toPlayer.y * lag / playerDistance * PLAYER_ORBIT_PULL),
false);
}
Vector toCenter = vector_sub(Room::GetCenterPos(), npc.pos);
float centerDistance = vector_length(toCenter);
if (std::abs(centerDistance) > 1)
{
float pull = std::min(centerDistance, CENTER_PULL_MAX);
npc.AddVelocity_divided(vector_make(pull * toCenter.x / centerDistance * CENTER_PULL,
pull * toCenter.y / centerDistance * CENTER_PULL),
false);
}
if (npc.v1.y - playerDistance / RISE_LAG_PER_DISTANCE > RISE_LAG && playerDistance > RISE_MIN_DISTANCE)
{
npc.targetPosition = npc.CalcTargetPosition(0);
npc.state = PIN_BURIED;
}
}
// The jump: it records its trail, falls under gravity, wiggles through the air and sinks into a new hole where the
// tail waits.
static void pin_jump_update(Entity_NPC& npc, const PinChain& chain)
{
trail_record(npc);
float depth = npc.posOffset.y;
npc.v1.y += npc.speedMultiplier * GRAVITY;
if (depth >= BURIED_DEPTH) return pin_tunnel(npc);
npc.entityCollisionClass = ENTCOLL_ALL;
std::string_view anim = npc.velocity.y > 0 ? "HeadWiggle"
: npc.variant == PIN_FRAIL ? "HeadWiggleUp"
: "Body1Wiggle";
npc.sprite.Play(anim);
npc.posOffset = vector_add(npc.posOffset, vector_scale(npc.v1, npc.speedMultiplier));
if (npc.posOffset.y <= SURFACE_DEPTH || npc.posOffset.y - npc.v1.y * npc.speedMultiplier >= SURFACE_DEPTH) return;
chain.tail->pos = vector_add(npc.pos, npc.velocity);
chain.tail->isVisible = true;
chain.tail->stateFrame = 0;
chain.tail->sprite.Play("Ground");
if (npc.isVisible)
{
npc.PlaySound(SOUND_MAGGOT_ENTER_GROUND, npc.variant == PIN_PIN ? 0.7f : 1, 2, false, 1);
chain.tail->UpdateDirtColor(true);
}
npc.isVisible = false;
}
// An attack picked as it surfaces: Scolex and the second phase Frail may roar, the second phase Frail fires beams,
// the rest spit.
static void pin_attack_pick(Entity_NPC& npc)
{
bool isFrailPhase = npc.variant == PIN_FRAIL && frail_phase(npc) == 1;
if ((npc.variant == PIN_SCOLEX || isFrailPhase) && Random() % 2 == 0 && npc.i1 != PIN_ROARING)
{
npc.i1 = PIN_ROARING;
npc.state = PIN_ROARING;
anim_restart(npc, "Attack2");
}
else if (isFrailPhase)
{
npc.i1 = (int)(Random() % 2) * 2 + PIN_SPITTING;
npc.state = npc.i1;
anim_restart(npc, "Attack3Start");
if (npc.state == PIN_SPITTING)
{
// The original writes the tracer into an empty deque for a Frail that skipped its start. Here it is
// dropped.
float angle = vector_angle(vector_sub(npc.CalcTargetPosition(0), npc.pos));
Entity_Effect* tracer = beam_tracer_spawn(npc, angle, SWEEP_TRACER_FRAMES);
if (!npc.entityDeque.empty()) npc.entityDeque.front() = tracer;
}
}
else
{
npc.i1 = PIN_SPITTING;
npc.state = PIN_SPITTING;
npc.sprite.Play("Attack1", PLAY_FORCED);
}
npc.UpdateDirtColor(true);
npc.stateFrame = 0;
}
// A dive: it bursts out toward its target (the room's middle the first time) with the hole left behind it.
static void pin_dive(Entity_NPC& npc, const PinChain& chain)
{
if (g_game->frameCount - npc.spawnFrame > DIVE_SOUND_DELAY)
npc.PlaySound(SOUND_MAGGOT_BURST_OUT, npc.variant == PIN_SCOLEX ? 1 : 0.7f, 2, false, 1);
if (vector_is_zero(npc.targetPosition))
{
npc.targetPosition = Room::GetCenterPos();
npc.shadowSize = 0;
npc.posOffset.y = SPAWN_DEPTH;
}
else
{
chain.tail->isVisible = true;
npc.isVisible = true;
npc.shadowSize = npc.config->shadowSize;
npc.posOffset.y = RISING_DEPTH;
npc.i1 = PIN_POPPED_UP;
}
Vector toTarget = vector_sub(npc.targetPosition, npc.pos);
float distance = vector_length(toTarget);
if (std::abs(distance) > 1)
{
float speed = JUMP_SPEED;
if (npc.variant == PIN_FRAIL) speed = Random() % 2 == 0 ? FRAIL_JUMP_FAST : FRAIL_JUMP_SLOW;
npc.velocity = vector_make(toTarget.x / distance * speed, toTarget.y / distance * speed);
}
npc.state = PIN_JUMPING;
chain.tail->pos = vector_add(npc.pos, npc.velocity);
chain.tail->sprite.Play("Ground");
chain.tail->stateFrame = 0;
chain.tail->UpdateDirtColor(true);
npc.vectorDeque1.clear();
npc.vectorDeque2.clear();
npc.v1 = vector_make(0, JUMP_LIFT - distance * JUMP_LIFT_PER_DISTANCE);
npc.vectorDeque1.push_front(npc.pos);
npc.vectorDeque2.push_front(npc.posOffset);
}
// Buried, it either attacks where it surfaced or dives on. A Delirium form in a cleared room only dives.
static void pin_buried_update(Entity_NPC& npc, const PinChain& chain)
{
bool isPicking =
(!npc.HasEntityFlags(FLAG_DELIRIUM_FORM) || !room_is_clear()) && Random() % 2 == 0 && npc.i1 != PIN_SPITTING;
bool isAttacking = isPicking && npc.i1 != 0 && npc.i1 != PIN_ROARING && npc.i1 != PIN_ATTACK_DIVE;
if (!isAttacking && npc.i1 == PIN_ROARING)
isAttacking = npc.variant == PIN_FRAIL && frail_phase(npc) == 1 && Random() % 5 != 0;
if (isAttacking) pin_attack_pick(npc);
else pin_dive(npc, chain);
}
// Its spit and roar shots, on the frame each fires.
static void pin_shots_fire(Entity_NPC& npc)
{
float pitch = champion_pitch_get(npc);
npc_sound_play(SOUND_BLOODSHOOT, 1, 2, pitch);
if (npc.state == PIN_ROARING)
{
npc_sound_play(SOUND_MONSTER_ROAR_2, 1, 2, pitch);
ProjectileParams params;
params.heightModifier = PIN_SHOT_HEIGHT;
if (npc.variant == PIN_FRAIL)
{
params.bulletFlags |= PROJECTILE_GOO | PROJECTILE_CANT_HIT_PLAYER;
params.color = FRAIL_RING_COLOR;
npc.FireBossProjectiles(FRAIL_RING_SHOTS, vector_make(0, 0), FRAIL_RING_SPEED, params);
}
else npc.FireBossProjectiles(ROAR_SHOTS, npc.GetPlayerTarget()->pos, 0, params);
}
else
{
bool isPin = npc.variant == PIN_PIN;
npc_sound_play(isPin ? SOUND_CUTE_GRUNT : SOUND_BOSS_LITE_HISS, isPin ? CUTE_GRUNT_VOLUME : 1, 2, pitch);
ProjectileParams params;
if (isPin && npc.subtype == 1)
{
Vector mouth = vector_make(npc.pos.x, npc.pos.y + SPIT_RING_LIFT);
params.bulletFlags |= PROJECTILE_ORBIT_CW;
params.fallingAccelModifier = SPIT_RING_FALL;
params.targetPosition = mouth;
npc.FireProjectiles(mouth, vector_make(SPIT_RING_SPEED, SPIT_RING_SHOTS), PROJECTILES_RING, params);
}
else
{
Entity_Projectile* shot = npc.FireBossProjectiles(1, npc.GetPlayerTarget()->pos, BIG_SHOT_SPEED, params);
shot->projectileFlags |= PROJECTILE_EXPLODE;
shot->fallingAccel += BIG_SHOT_FALL;
shot->scale = PIN_BIG_SHOT_SCALE;
shot->fallingSpeed = shot->fallingSpeed * BIG_SHOT_LIFT_SCALE + BIG_SHOT_LIFT;
shot->height += PIN_SHOT_HEIGHT;
shot->velocity = vector_scale(shot->velocity, BIG_SHOT_SLOW);
}
}
npc_sound_play(SOUND_PIN_SPIT, 1, 2, 1);
}
// Whether this frame of an attack fires: the roar on frame 20 (47 for the Frail), the spit on 46 (to 48 for Scolex).
static bool is_fire_frame(const Entity_NPC& npc, int frame)
{
if (npc.state == PIN_ROARING) return frame == (npc.variant == PIN_FRAIL ? FRAIL_ROAR_FIRE_FRAME : ROAR_FIRE_FRAME);
if (npc.state != PIN_SPITTING) return false;
if (frame == SPIT_FIRE_FRAME) return true;
return frame > SPIT_FIRE_FRAME && frame <= SCOLEX_SPIT_LAST_FRAME && npc.variant == PIN_SCOLEX;
}
// Its spit or roar above ground, a frame per tick, sinking back once the animation ends.
static void pin_attack_update(Entity_NPC& npc, const PinChain& chain)
{
chain.tail->isVisible = false;
npc.entityCollisionClass = ENTCOLL_ALL;
npc.isVisible = true;
npc.shadowSize = 0;
npc.velocity = vector_make(0, 0);
npc.posOffset.y = 0;
for (int ticks = npc_ticks_get(npc); ticks > 0; ticks--)
{
if (is_fire_frame(npc, npc.stateFrame)) pin_shots_fire(npc);
else if (npc.stateFrame >= ATTACK_END_FRAME)
{
npc.state = PIN_BURIED;
npc.targetPosition = npc.CalcTargetPosition(0);
npc.stateFrame = 0;
npc.entityCollisionClass = ENTCOLL_NONE;
npc.isVisible = false;
}
else if (is_anim_done(npc))
{
npc.entityCollisionClass = ENTCOLL_NONE;
npc.isVisible = false;
}
npc.stateFrame++;
}
}
// The head: shares its health, then digs, jumps or attacks.
static void pin_head_update(Entity_NPC& npc, const PinChain& chain)
{
pin_health_share(npc, chain);
bool isFrailBeam = npc.variant == PIN_FRAIL && frail_phase(npc) != 0;
if (npc.state == PIN_SPITTING && isFrailBeam)
{
if (frail_phase(npc) == 1) frail_beams_update(npc, chain);
return;
}
if (npc.state == PIN_BEAMS) return frail_beams_update(npc, chain);
if (npc.state == PIN_JUMPING) return pin_jump_update(npc, chain);
if (npc.state == PIN_BURIED) return pin_buried_update(npc, chain);
if (npc.state == PIN_SPITTING || npc.state == PIN_ROARING) pin_attack_update(npc, chain);
}
// The tail: the hole the head left, closing a while after it and sharing the segment above's health.
static void pin_tail_update(Entity_NPC& npc)
{
npc.stateFrame++;
npc.entityCollisionClass = ENTCOLL_NONE;
npc.shadowSize = 0;
bool isPinHole = npc.stateFrame == HOLE_CLOSE_FRAME && (npc.variant == PIN_PIN || npc.variant == PIN_FRAIL);
if (isPinHole || (npc.stateFrame == SCOLEX_HOLE_CLOSE_FRAME && npc.variant == PIN_SCOLEX))
{
anim_restart(npc, "HoleClose");
npc.UpdateDirtColor(true);
}
if (is_anim_done(npc)) npc.isVisible = false;
if (npc.parent) npc.health = npc.parent->health;
}
// A body segment's wiggle, by kind and place in the chain.
static std::string_view body_anim_get(Entity_NPC& npc, int depth)
{
if (npc.variant == PIN_PIN) return "Body2Wiggle";
if (npc.variant == PIN_SCOLEX)
{
if (depth == 2 || depth == 7) return "Body3Wiggle";
if (depth == SCOLEX_SEGMENTS - 1) return "TailWiggle";
return depth >= 1 && depth <= 6 ? "Body2Wiggle" : "";
}
if (npc.variant != PIN_FRAIL || depth < 1 || depth > PIN_SEGMENTS - 1) return "";
int place = depth - 1;
if (frail_phase(npc) == 0) return place == 0 || place == 2 ? "Body2Wiggle" : "Body3Wiggle";
if (place == 1) return "Body2Wiggle";
if (place == 3) return "Body3Wiggle";
return place == 4 ? "TailWiggleUp" : "Body1Wiggle";
}
// A body segment follows its head's trail some points behind, solid while near the ground. Pin's and the Frail's
// fourth segment spits while high in the air.
static void pin_body_update(Entity_NPC& npc, const PinChain& chain)
{
if (npc.isVisible) npc.shadowSize = npc.config->shadowSize;
if (chain.head->state != PIN_JUMPING)
{
npc.isVisible = false;
npc.entityCollisionClass = ENTCOLL_NONE;
return;
}
npc.sprite.Play(body_anim_get(npc, chain.depth));
int delay = npc.variant == PIN_SCOLEX ? SCOLEX_DELAYS[std::min(chain.depth, SCOLEX_SEGMENTS - 1)]
: PIN_DELAYS[std::min(chain.depth, PIN_SEGMENTS - 1)];
if (delay >= (int)chain.head->vectorDeque1.size()) return;
npc.pos = chain.head->vectorDeque1[delay];
npc.velocity = vector_make(0, 0);
npc.posOffset = chain.head->vectorDeque2[delay];
npc.entityCollisionClass = npc.posOffset.y < BURIED_DEPTH ? ENTCOLL_ALL : ENTCOLL_NONE;
npc.isVisible = npc.posOffset.y < BURIED_DEPTH;
bool isSpitter = (npc.variant == PIN_PIN || npc.variant == PIN_FRAIL) && chain.depth == SEGMENT_SPITTER;
if (!isSpitter || (npc.HasEntityFlags(FLAG_DELIRIUM_FORM) && room_is_clear())) return;
if (npc.projectileCooldown > 0) npc.projectileCooldown--;
if (Random() % 2 != 0 || npc.posOffset.y >= PIN_SPIT_HEIGHT || npc.projectileCooldown >= 1) return;
npc.projectileCooldown = SEGMENT_SPIT_COOLDOWN;
ProjectileParams params;
if (npc.variant == PIN_FRAIL && frail_phase(npc) == 1) params.bulletFlags |= PROJECTILE_BURST;
if (npc.variant == PIN_PIN && npc.subtype == 1)
npc.FireProjectiles(npc.pos, vector_make(SPIT_RING_SPEED, SPIT_RING_SHOTS), PROJECTILES_RING, params);
else npc.FireProjectiles(npc.pos, vector_make(SEGMENT_CROSS_SPEED, 0), PROJECTILES_CROSS, params);
npc_sound_play(SOUND_PIN_SPIT, 1, 2, 1);
}
// The Frail's first phase ends: every segment rises again at full health in its second phase sheet, bursting blood.
static void frail_phase_change(Entity_NPC& npc, const PinChain& chain)
{
npc.PlaySound(SOUND_DEATH_BURST_LARGE, 1, 2, false, 1);
for (auto* segment = static_cast<Entity_NPC*>(npc.GetLastParent()); segment; segment = segment->ChildNPC())
{
if (!segment->HasEntityFlags(FLAG_DELIRIUM_FORM)) segment->maxHitPoints = npc.config->baseHP;
frail_phase_advance(*segment);
segment->health = segment->maxHitPoints;
segment->isDead = false;
frail_sheet_load(npc, *segment);
segment->damageEvents.clear();
segment->pendingDamage = 0;
segment->pendingDamageFlags = 0;
for (int i = 0; i < PHASE_GIBS; i++)
{
float speed = random_unit() * PHASE_GIB_SPEED_RANGE + PHASE_GIB_SPEED_MIN;
int kind = PHASE_GIB_KINDS[Random() % PHASE_GIB_KINDS.size()];
if (kind == PHASE_GIB_HEAD_ONLY && segment != chain.head) kind = PHASE_GIB_KINDS[Random() % 2];
uint32_t seed = Random();
Vector heading = random_direction();
Vector velocity = vector_make(heading.x * speed, heading.y * PHASE_GIB_LIFT * speed);
Vector place = random_direction();
Vector position = vector_make(place.x * PHASE_GIB_SPREAD * segment->size + segment->pos.x,
place.y * PHASE_GIB_SPREAD * segment->size + segment->pos.y);
Entity* gib = g_game->Spawn({.position = position,
.velocity = velocity,
.spawner = &npc,
.subType = kind,
.type = ENTITY_EFFECT,
.variant = EFFECT_BLOOD_PARTICLE,
.seed = seed});
gib->splatColor = segment->splatColor;
}
Entity* burst = g_game->Spawn({.position = segment->pos,
.spawner = &npc,
.type = ENTITY_EFFECT,
.variant = EFFECT_LARGE_BLOOD_EXPLOSION,
.seed = Random()});
burst->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
}
}
// The Frail's second phase ends in faint spinning dust. A Delirium form takes the whole chain with it.
static void frail_death_dust(Entity_NPC& npc, const PinChain& chain)
{
int clouds = (int)(Random() % 4) + DEATH_DUST_MIN;
for (int i = 0; i < clouds; i++)
{
Vector heading = random_direction();
Vector velocity = vector_scale(heading, random_unit() * PIN_DEATH_DUST_SPEED);
uint32_t seed = Random();
Vector place = random_direction();
Vector position = vector_add(npc.pos, vector_scale(place, random_unit() * DEATH_DUST_SPREAD));
auto* dust = static_cast<Entity_Effect*>(g_game->Spawn({.position = position,
.velocity = velocity,
.spawner = &npc,
.type = ENTITY_EFFECT,
.variant = EFFECT_DUST_CLOUD,
.seed = seed}));
float side =
(dust->pos.y - npc.pos.y) * -velocity.x + (dust->pos.x - npc.pos.x) * velocity.y < 0 ? -1.0f : 1.0f;
dust->rotation = (random_unit() * DEATH_DUST_SPIN_RANGE + DEATH_DUST_SPIN_MIN) * side;
int life = (int)(Random() % DEATH_DUST_LIFE_RANGE) + DEATH_DUST_LIFE_MIN;
dust->timeout = life;
dust->lifespan = life;
ColorMod faint = tint_color_make(1, 1, 1, random_unit() * DEATH_DUST_ALPHA_RANGE + DEATH_DUST_ALPHA_MIN);
dust->SetColor(faint, -1, COLOR_PRIORITY_DEFAULT, false, true);
dust->Update();
if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM))
for (Entity* link = chain.tail; link; link = link->parent) link->Kill(EntityRef());
}
}
// What a dead segment does: the Frail's first death starts its second phase and its second leaves dust, and a
// Delirium form's chain all dies with it.
static void pin_death_update(Entity_NPC& npc, const PinChain& chain)
{
if (!npc.isDead) return;
if (npc.variant == PIN_FRAIL)
{
if (frail_phase(npc) == 0) frail_phase_change(npc, chain);
else frail_death_dust(npc, chain);
return;
}
if (npc.variant != PIN_PIN && npc.variant != PIN_SCOLEX) return;
if (!npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) return;
for (Entity* link = chain.tail; link; link = link->parent) link->Kill(EntityRef());
}
// 00476860 Pin, Scolex and the Frail: a worm that dives through the floor, its body following its trail, rising to
// spit and roar. The Frail changes into a second phase with beams. Wormwood runs its own AI.
void boss_pin_update(Entity_NPC& npc)
{
if (npc.variant == PIN_WORMWOOD) return boss_wormwood_update(npc);
PinChain chain = pin_chain_get(npc);
npc.renderZOffset = chain.depth * (npc.velocity.y < 0 ? -RENDER_DEPTH_STEP : RENDER_DEPTH_STEP);
if (npc.state == NPC_STATE_INIT && chain.depth == 0)
{
pin_init(npc);
chain = pin_chain_get(npc);
}
if (npc.subtype == 1 && frail_phase(npc) == 0 && npc.variant == PIN_FRAIL) frail_phase_start(npc);
if (chain.head == chain.tail)
{
npc.ClearEntityFlags(FLAG_NO_DEATH_TRIGGER);
npc.Kill(EntityRef());
return;
}
if (npc.variant == PIN_SCOLEX && npc.state != PIN_SPITTING) scolex_shield(npc, chain);
if (&npc == chain.head) pin_head_update(npc, chain);
else if (&npc == chain.tail) pin_tail_update(npc);
else pin_body_update(npc, chain);
pin_death_update(npc, chain);
if (npc.sprite.IsEventTriggered("Dive")) npc_sound_play(SOUND_PIN_DIVE, 1, 2, 1);
if (npc.sprite.IsEventTriggered("Popup")) npc_sound_play(SOUND_PIN_POPUP, 1, 2, 1);
}