1350 lines
62 KiB
C++
1350 lines
62 KiB
C++
#include "boss_ai.h"
|
|
#include "boss_repentance.h"
|
|
#include "entity_config.h"
|
|
#include "entity_effect.h"
|
|
#include "grid_entity.h"
|
|
#include "kage_graphics.h"
|
|
#include "loot_list.h"
|
|
#include "npc_basement.h"
|
|
#include "render_passes.h"
|
|
#include "room_grid.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <cmath>
|
|
|
|
// The Scourge's states: hovering after its target, dragging two chains down, slicing, the hover ring, its broccoli
|
|
// spit, and drawing its chains back.
|
|
enum ScourgeState
|
|
{
|
|
SCOURGE_IDLE = NPC_STATE_IDLE,
|
|
SCOURGE_HOVER = NPC_STATE_JUMP,
|
|
SCOURGE_HOVER_END = NPC_STATE_STOMP,
|
|
SCOURGE_TRAP = NPC_STATE_ATTACK,
|
|
SCOURGE_RECALL = NPC_STATE_ATTACK2,
|
|
SCOURGE_SLICE = NPC_STATE_ATTACK3,
|
|
SCOURGE_BROCCOLI = NPC_STATE_ATTACK4
|
|
};
|
|
|
|
// Its attacks, each with its own cooldown (one byte of v2 each, counted down every 10 frames).
|
|
enum ScourgeAttack
|
|
{
|
|
SCOURGE_ATTACK_NONE,
|
|
SCOURGE_ATTACK_TRAP,
|
|
SCOURGE_ATTACK_SLICE_BOTH,
|
|
SCOURGE_ATTACK_SLICE,
|
|
SCOURGE_ATTACK_BROCCOLI,
|
|
SCOURGE_ATTACK_HOVER,
|
|
SCOURGE_ATTACKS
|
|
};
|
|
|
|
constexpr std::array<int, SCOURGE_ATTACKS> SCOURGE_ATTACK_STATES = {
|
|
SCOURGE_IDLE, SCOURGE_TRAP, SCOURGE_SLICE, SCOURGE_SLICE, SCOURGE_BROCCOLI, SCOURGE_HOVER};
|
|
constexpr std::array<uint8_t, SCOURGE_ATTACKS> SCOURGE_ATTACK_COOLDOWNS = {0, 90, 90, 54, 15, 90};
|
|
constexpr float SCOURGE_COOLDOWN_EVERY = 10;
|
|
constexpr int SCOURGE_ATTACK_WEIGHT = 10;
|
|
constexpr int SCOURGE_REST_WEIGHT = 1;
|
|
constexpr float SCOURGE_TRAP_BELOW = 0.95f;
|
|
constexpr float SCOURGE_LEAD = 0.1f;
|
|
constexpr uint32_t SCOURGE_PICK_SHIFT_1 = 5;
|
|
constexpr uint32_t SCOURGE_PICK_SHIFT_2 = 9;
|
|
constexpr uint32_t SCOURGE_PICK_SHIFT_3 = 7;
|
|
|
|
// Below half health the broccoli spits three times and waits 165 counts (its cooldown less 106, wrapped) to again.
|
|
constexpr uint8_t SCOURGE_BROCCOLI_WAIT = 106;
|
|
constexpr int SCOURGE_BROCCOLI_SPITS = 3;
|
|
constexpr float SCOURGE_ANGRY_HEALTH = 0.5f;
|
|
|
|
// The halves of its packed counters: its attack's frames and its idle frames in projectileCooldown, its rise's
|
|
// frames and whether it slices one side in projectileDelay.
|
|
enum ScourgeHalf
|
|
{
|
|
SCOURGE_HALF_LOW,
|
|
SCOURGE_HALF_HIGH
|
|
};
|
|
|
|
// Idle it hovers 120 over its target, eases toward it at 1.2 (a tenth of the gap, at most 1), and now and then (one
|
|
// time in 20) picks another spot 160 to 200 over it within 45 degrees. After 30 idle frames it attacks one time in 10.
|
|
constexpr float SCOURGE_HOVER_HEIGHT = -120;
|
|
constexpr float SCOURGE_IDLE_FRICTION = 0.8f;
|
|
constexpr float SCOURGE_ROOM_MARGIN = 4;
|
|
constexpr float SCOURGE_EASE = 0.1f;
|
|
constexpr float SCOURGE_IDLE_SPEED = 1.2f;
|
|
constexpr uint32_t SCOURGE_SPOT_ODDS = 20;
|
|
constexpr float SCOURGE_SPOT_RANGE = 40;
|
|
constexpr float SCOURGE_SPOT_HEIGHT = -200;
|
|
constexpr float SCOURGE_SPOT_TURN = 90;
|
|
constexpr float SCOURGE_SPOT_TURN_MIN = -45;
|
|
constexpr int SCOURGE_IDLE_FRAMES = 30;
|
|
constexpr uint32_t SCOURGE_ATTACK_ODDS = 10;
|
|
|
|
// Attacking it slows by 0.6 a frame. Its death spits a burst from 60 up.
|
|
constexpr float SCOURGE_ATTACK_FRICTION = 0.6f;
|
|
constexpr float SCOURGE_DEATH_RAISE = -60;
|
|
constexpr float SCOURGE_SPLAT_RAISE = -20;
|
|
|
|
// The trap: two chains thrown down to either side (4 off its middle), rising 14 frames by at most 4 a frame. Every
|
|
// 10 frames it lets go once its target leaves the space between the chains, or after 300 frames. Latched, the chains
|
|
// pull it like springs (260 long, 0.1 stiff, 0.1 damped), and it shoots every 30 frames (both chains slicing) or 15
|
|
// (one chain).
|
|
constexpr float SCOURGE_TRAP_THROW = 20;
|
|
constexpr float SCOURGE_TRAP_OFFSET = 4;
|
|
constexpr int SCOURGE_TRAP_RISE = 14;
|
|
constexpr float SCOURGE_TRAP_RISE_MAX = 4;
|
|
constexpr float SCOURGE_TRAP_CHECK_EVERY = 10;
|
|
constexpr int SCOURGE_TRAP_FRAMES = 300;
|
|
constexpr float SCOURGE_TRAP_LEVEL = 20;
|
|
constexpr int SCOURGE_VOLLEY_EVERY = 30;
|
|
constexpr int SCOURGE_CHAIN_SHOT_EVERY = 15;
|
|
constexpr float SCOURGE_SPRING_STIFFNESS = 0.1f;
|
|
constexpr float SCOURGE_SPRING_DAMPING = 0.1f;
|
|
constexpr float SCOURGE_SPRING_LENGTH = 260;
|
|
|
|
// The slice: chains thrown at 26 at its target (both ways, or one), then it pulls on them, drifting to the room's
|
|
// middle (a 0.05 damped spring), ringing 12 shots at 6 every 30 frames, for 240 frames (60 for one chain).
|
|
constexpr float SCOURGE_SLICE_THROW = 26;
|
|
constexpr float SCOURGE_SLICE_ONE_FRAME = 15;
|
|
constexpr float SCOURGE_CENTER_DAMPING = 0.05f;
|
|
constexpr int SCOURGE_SLICE_FRAMES = 240;
|
|
constexpr int SCOURGE_SLICE_ONE_FRAMES = 60;
|
|
constexpr float SCOURGE_RING_SPEED = 6;
|
|
constexpr float SCOURGE_RING_SHOTS = 12;
|
|
constexpr float SCOURGE_RING_FALL = -0.12f;
|
|
|
|
// The hover: 300 frames drifting to the room's middle (at most 2 a frame) as a ring of shots closes in. Every other
|
|
// frame up to 240 a pair of shots appears opposite each other on a circle as wide as the room plus 60 to 80, and every
|
|
// 12 a bigger one somewhere on it. The pair's angle turns by a speed that turns toward ±12 at twice its speed,
|
|
// reversing one time in 50. Every 6 frames and 3 frames it leaves a puff of its big pull.
|
|
constexpr float SCOURGE_HOVER_MAX_STEP = 2;
|
|
constexpr int SCOURGE_HOVER_FRAMES = 300;
|
|
constexpr int SCOURGE_HOVER_SHOT_FRAMES = 240;
|
|
constexpr int SCOURGE_HOVER_BIG_EVERY = 12;
|
|
constexpr float SCOURGE_HOVER_RING_OUT = 60;
|
|
constexpr float SCOURGE_HOVER_RING_RANGE = 20;
|
|
constexpr float SCOURGE_HOVER_TURN = 12;
|
|
constexpr uint32_t SCOURGE_HOVER_REVERSE_ODDS = 50;
|
|
constexpr int SCOURGE_HOVER_PUFF_EVERY = 6;
|
|
constexpr int SCOURGE_HOVER_PUFF_SMALL_EVERY = 3;
|
|
constexpr float SCOURGE_HOVER_PUFF_RAISE = -24;
|
|
constexpr ColorMod SCOURGE_PUFF_COLOR = {{0.5f, 0.6f, 0.3f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
|
|
// The hover ring's shots: faded in over 30 frames, homing (0.1) on the room's middle through walls and enemies,
|
|
// sized 2/3 to 10/3, and the bigger ones 4/3 and greener.
|
|
constexpr uint64_t SCOURGE_RING_FLAGS =
|
|
PROJECTILE_HIT_ENEMIES | PROJECTILE_NO_WALL_COLLIDE | PROJECTILE_ACCELERATE_TO_POSITION;
|
|
constexpr int SCOURGE_RING_FADE = 30;
|
|
constexpr float SCOURGE_RING_SHOT_FALL = -0.05f;
|
|
constexpr float SCOURGE_RING_HOMING = 0.1f;
|
|
constexpr float SCOURGE_RING_FRICTION = 0.8f;
|
|
constexpr float SCOURGE_RING_BIG_FRICTION = 0.99f;
|
|
constexpr uint32_t SCOURGE_RING_SIZE_RANGE = 3;
|
|
constexpr uint32_t SCOURGE_RING_SIZE_MIN = 5;
|
|
constexpr uint32_t SCOURGE_RING_SIZE_MAX = 13;
|
|
constexpr float SCOURGE_RING_SIZE_BASE = 3;
|
|
constexpr float SCOURGE_RING_BIG_SCALE = 1.3333334f;
|
|
constexpr ColorMod SCOURGE_RING_BIG_COLOR = {{1, 1, 1, 1}, {2, 3, 1, 1}, {0, 0, 0}};
|
|
|
|
// The broccoli: on Shoot a sick shot (14, or a smaller 5 below half health) at 14 from 23 up, homing 0.5, the
|
|
// Scourge kicked back at 10 for 10 frames (decaying 0.8 a frame) and the screen shaken. While spits are left it
|
|
// loops back to frame 30 past frame 56.
|
|
constexpr int SCOURGE_BROCCOLI_SHAKE = 7;
|
|
constexpr int SCOURGE_BROCCOLI_KICK_FRAMES = 10;
|
|
constexpr float SCOURGE_BROCCOLI_KICK = -10;
|
|
constexpr float SCOURGE_BROCCOLI_KICK_DECAY = 0.8f;
|
|
constexpr float SCOURGE_BROCCOLI_HEIGHT = -23;
|
|
constexpr float SCOURGE_BROCCOLI_SPEED = 14;
|
|
constexpr float SCOURGE_BROCCOLI_FALL = -0.08f;
|
|
constexpr float SCOURGE_BROCCOLI_HOMING = 0.5f;
|
|
constexpr int SCOURGE_BROCCOLI_BIG = 14;
|
|
constexpr int SCOURGE_BROCCOLI_SMALL = 5;
|
|
constexpr float SCOURGE_BROCCOLI_SMALL_SCALE = 0.6f;
|
|
constexpr float SCOURGE_BROCCOLI_LOOP_FRAME = 56;
|
|
constexpr float SCOURGE_BROCCOLI_LOOP_TO = 30;
|
|
constexpr ColorMod SCOURGE_SPIT_COLOR = {{1, 1, 1, 1}, {2, 4, 1, 1}, {0, 0, 0}};
|
|
|
|
// The volleys between latched chains: a fan of shots over the gap (12 per unit of the chains' spread, none within 5
|
|
// degrees of a gap swaying ±10 a time within the fan's ends, 20 in from them and 10 at least) at 4 to 5, and one at
|
|
// the target at 8 to 10. Shots on its chains leave at 5 to 6 from a spot one time in five level with the target.
|
|
constexpr float SCOURGE_SHOT_RAISE = 10;
|
|
constexpr float SCOURGE_SHOT_FALL = -0.14f;
|
|
constexpr float SCOURGE_FAN_INSET = 20;
|
|
constexpr float SCOURGE_FAN_MIN = 10;
|
|
constexpr float SCOURGE_FAN_DENSITY = 0.707f * 12;
|
|
constexpr float SCOURGE_GAP_SWAY = 20;
|
|
constexpr float SCOURGE_GAP_WIDTH = 5;
|
|
constexpr float SCOURGE_FAN_SPEED = 4;
|
|
constexpr float SCOURGE_AIMED_SPEED = 8;
|
|
constexpr float SCOURGE_CHAIN_SHOT_SPEED = 5;
|
|
constexpr uint32_t SCOURGE_CHAIN_SHOT_LEVEL_ODDS = 5;
|
|
constexpr float SCOURGE_CHAIN_SHOT_OUT = 4;
|
|
|
|
// A chain: 20 links thrown from a point over 10 along its throw. Its tip flies at 10 times its heading a frame (80 at
|
|
// most), waving (0.03 per unit of the chain's length). Retracting it shrinks by 0.8 a frame and goes inside 10 or
|
|
// below a tenth.
|
|
constexpr int SCOURGE_LINKS = 20;
|
|
constexpr float SCOURGE_CHAIN_SPAN = 10;
|
|
constexpr float SCOURGE_LINK_RAISE = -20;
|
|
constexpr float SCOURGE_TIP_FLIGHT = 15;
|
|
constexpr float SCOURGE_TIP_WAVE = 0.03f;
|
|
constexpr float SCOURGE_TIP_MASS = 1000;
|
|
constexpr float SCOURGE_TIP_THRUST = 10;
|
|
constexpr float SCOURGE_TIP_SPEED_MAX = 80;
|
|
constexpr float SCOURGE_RETRACT_PULL = 0.5f;
|
|
constexpr float SCOURGE_RETRACT_SHRINK = 0.8f;
|
|
constexpr float SCOURGE_RETRACT_NEAR = 10;
|
|
constexpr float SCOURGE_RETRACT_SMALL = 0.1f;
|
|
constexpr float SCOURGE_HEAD_DEPTH = 100;
|
|
constexpr float SCOURGE_LINK_REACH = 10;
|
|
|
|
// The walls a tip latches onto lie 34 past the room's edges (the top and bottom 20 lower), turning its sprite
|
|
// (left, top, right, bottom). It hits with a thud, shaking the screen 8, bursting 6 rock pieces at up to 12 (5 fast)
|
|
// within 45 degrees, waving 0.06 and shrinking its flight to 22.
|
|
constexpr float SCOURGE_WALL_OUT = 34;
|
|
constexpr float SCOURGE_WALL_LOWER = 20;
|
|
constexpr std::array<float, 4> SCOURGE_WALL_ROTATIONS = {90, 180, -90, 0};
|
|
constexpr int SCOURGE_IMPACT_SHAKE = 8;
|
|
constexpr int SCOURGE_IMPACT_PIECES = 6;
|
|
constexpr float SCOURGE_PIECE_SPREAD = 45;
|
|
constexpr float SCOURGE_PIECE_OUT = 12;
|
|
constexpr float SCOURGE_PIECE_SPEED = 5;
|
|
constexpr float SCOURGE_BLOOD_RAISE = -10;
|
|
constexpr float SCOURGE_BLOOD_RAISE_RANGE = 20;
|
|
constexpr float SCOURGE_LATCHED_WAVE = 0.06f;
|
|
constexpr float SCOURGE_LATCHED_FLIGHT = 22;
|
|
constexpr float SCOURGE_SLIDE_START = -1;
|
|
|
|
// A slicing tip crawls along the walls, turning the corners: at up to 13 (1 more a frame), or 24 (1.1 more) for one
|
|
// chain, slowing to 0.3 at corners and one time in 30. The chain sags up to 40 (2 a frame) toward its side. Faster
|
|
// than 2 it bleeds one time in 6 (3 for one chain), sometimes (one in 3, always for one chain) with a gib and shake
|
|
// 4. One chain also leaves green creep at its gib (1.5 big), a splat, a maggot one time in 3 and a sick shot 140 on.
|
|
constexpr float SCOURGE_SLIDE_STEP = 1;
|
|
constexpr float SCOURGE_SLIDE_MAX = 13;
|
|
constexpr float SCOURGE_SLIDE_ONE_STEP = 1.1f;
|
|
constexpr float SCOURGE_SLIDE_ONE_MAX = 24;
|
|
constexpr uint32_t SCOURGE_SLIDE_SLOW_ODDS = 30;
|
|
constexpr float SCOURGE_SLIDE_SLOW = 0.3f;
|
|
constexpr float SCOURGE_SAG_STEP = 2;
|
|
constexpr float SCOURGE_SAG_MAX = 40;
|
|
constexpr float SCOURGE_BLEED_SPEED = 2;
|
|
constexpr int SCOURGE_BLEED_ODDS = 6;
|
|
constexpr int SCOURGE_BLEED_ONE_ODDS = 3;
|
|
constexpr uint32_t SCOURGE_GIB_ODDS = 3;
|
|
constexpr int SCOURGE_GIB_SHAKE = 4;
|
|
constexpr int SCOURGE_CREEP_SUBTYPE = 100;
|
|
constexpr float SCOURGE_CREEP_SCALE = 1.5f;
|
|
constexpr float SCOURGE_CREEP_SPOT_RANGE = 60;
|
|
constexpr float SCOURGE_CREEP_SPOT_MIN = 20;
|
|
constexpr float SCOURGE_SPLAT_SIZE = 1.5f;
|
|
constexpr uint32_t SCOURGE_MAGGOT_ODDS = 3;
|
|
constexpr float SCOURGE_MAGGOT_MARGIN = 16;
|
|
constexpr float SCOURGE_MAGGOT_HEIGHT = -4;
|
|
constexpr float SCOURGE_SLIDE_SHOT_REACH = 140;
|
|
constexpr float SCOURGE_CHAIN_FRICTION = 0.9f;
|
|
constexpr float SCOURGE_LATCHED_DECAY = 0.8f;
|
|
|
|
// Links even out their health: any above the weakest takes the difference, and the Scourge a tenth of the most.
|
|
constexpr float SCOURGE_HEALTH_UNSET = 10000000000.0f;
|
|
constexpr float SCOURGE_SHARED_DAMAGE = 0.1f;
|
|
constexpr int SCOURGE_LINK_DAMAGE_COUNTDOWN = 30;
|
|
|
|
// The chain's texture strip stops 16 short of its end on a latched trap chain.
|
|
constexpr float SCOURGE_STRIP_LATCHED_INSET = 16;
|
|
|
|
// A short of a packed counter.
|
|
static int16_t scourge_half_get(int field, ScourgeHalf half)
|
|
{
|
|
return (int16_t)((uint32_t)field >> (16 * half));
|
|
}
|
|
|
|
static void scourge_half_set(int& field, ScourgeHalf half, int value)
|
|
{
|
|
uint32_t shift = 16 * (uint32_t)half;
|
|
field = (int)(((uint32_t)field & ~(0xffffu << shift)) | ((uint32_t)(uint16_t)value << shift));
|
|
}
|
|
|
|
// The attacks' cooldowns, a byte of v2 each.
|
|
static uint8_t* scourge_cooldowns(Entity_NPC& npc)
|
|
{
|
|
return reinterpret_cast<uint8_t*>(&npc.v2);
|
|
}
|
|
|
|
// A link keeps its wave and crawl speed as floats in i1 and i2.
|
|
static float link_float_get(int field)
|
|
{
|
|
return std::bit_cast<float>(field);
|
|
}
|
|
|
|
static int link_float_make(float value)
|
|
{
|
|
return std::bit_cast<int>(value);
|
|
}
|
|
|
|
static bool is_scourge_link(Entity* entity)
|
|
{
|
|
return entity && entity->type == ENTITY_SCOURGE && entity->variant == SCOURGE_CHAIN;
|
|
}
|
|
|
|
// (00582a60, 00582a80, 00582aa0) The chain's first link (by parents) and its tip (by children).
|
|
static Entity_NPC& link_head_get(Entity_NPC& link)
|
|
{
|
|
Entity_NPC* head = &link;
|
|
while (is_scourge_link(head->parent.get())) head = static_cast<Entity_NPC*>(head->parent.get());
|
|
return *head;
|
|
}
|
|
|
|
static Entity_NPC& link_tip_get(Entity_NPC& link)
|
|
{
|
|
Entity_NPC* tip = &link;
|
|
while (is_scourge_link(tip->child.get())) tip = static_cast<Entity_NPC*>(tip->child.get());
|
|
return *tip;
|
|
}
|
|
|
|
// (00582ad0, 00582b00) Whether a chain's tip has latched onto a wall, and where it is.
|
|
static bool chain_is_latched(Entity* chain)
|
|
{
|
|
return link_tip_get(*static_cast<Entity_NPC*>(chain)).state == SCOURGE_LINK_LATCHED;
|
|
}
|
|
|
|
static Vector chain_tip_position(Entity* chain)
|
|
{
|
|
return link_tip_get(*static_cast<Entity_NPC*>(chain)).pos;
|
|
}
|
|
|
|
// 00582b40 The Scourge's child when it is a chain.
|
|
static Entity* scourge_child_chain(Entity_NPC& npc)
|
|
{
|
|
return is_scourge_link(npc.child.get()) ? npc.child.get() : nullptr;
|
|
}
|
|
|
|
// 00583110 Flips a chain's first link and tip to a side.
|
|
static void chain_flip_set(Entity* chain, bool isFlipped)
|
|
{
|
|
if (!chain) return;
|
|
auto& link = *static_cast<Entity_NPC*>(chain);
|
|
link_head_get(link).sprite.isFlipX = isFlipped;
|
|
link_tip_get(link).sprite.isFlipX = isFlipped;
|
|
}
|
|
|
|
// 005830e0 Stops a chain's tip crawling.
|
|
static void chain_crawl_stop(Entity* chain)
|
|
{
|
|
link_tip_get(*static_cast<Entity_NPC*>(chain)).i2 = link_float_make(0);
|
|
}
|
|
|
|
// 00586b50 Pulls the Scourge toward a point like a spring of a length and stiffness, damped.
|
|
static void scourge_spring_pull(Entity_NPC& npc, Vector anchor, Vector spring, float damping)
|
|
{
|
|
Vector away = vector_sub(npc.pos, anchor);
|
|
float length = vector_length(away);
|
|
if (length > 0) away = vector_scale(away, 1 / length);
|
|
float stretch = length - spring.x;
|
|
npc.velocity.x -= (stretch * away.x * spring.y + npc.velocity.x * damping) * npc.speedMultiplier;
|
|
npc.velocity.y -= (stretch * away.y * spring.y + npc.velocity.y * damping) * npc.speedMultiplier;
|
|
}
|
|
|
|
// A splash of the Scourge's green blood, a size up.
|
|
static void scourge_splash(int subType, const Vector& position, float raise)
|
|
{
|
|
blood_explosion_spawn(subType, position, vector_make(0, raise), COLOR_GREEN_SPLAT, {});
|
|
}
|
|
|
|
// A shot of its volleys: lower, falling slowly upward, fiery.
|
|
static ProjectileParams scourge_shot_params(uint64_t flags)
|
|
{
|
|
ProjectileParams params;
|
|
params.heightModifier += SCOURGE_SHOT_RAISE;
|
|
params.fallingAccelModifier += SCOURGE_SHOT_FALL;
|
|
params.color = SHOT_COLOR_FIERY;
|
|
params.bulletFlags = flags;
|
|
return params;
|
|
}
|
|
|
|
// A chain thrown (NpcHelper::scourge_spawn_chain's arguments): from where at what velocity, its kind (its links'
|
|
// subtype), the entity it hangs from and its first link's offset from it.
|
|
struct ScourgeChainThrow
|
|
{
|
|
Vector position{};
|
|
Vector velocity{};
|
|
Vector offset{};
|
|
Entity* parent = nullptr;
|
|
int kind = SCOURGE_CHAIN_TRAP;
|
|
};
|
|
|
|
// 00582b60 Throws a chain of 20 links strung from a point along its throw, its tip flying off, its first link
|
|
// hanging from its parent and whipping. Returns the first link.
|
|
static Entity_NPC* scourge_chain_spawn(const ScourgeChainThrow& chainThrow)
|
|
{
|
|
Vector span = vector_sub(vector_add(vector_scale(vector_normalized(chainThrow.velocity), SCOURGE_CHAIN_SPAN),
|
|
chainThrow.position),
|
|
chainThrow.position);
|
|
std::vector<Entity_NPC*> links;
|
|
Entity_NPC* previous = nullptr;
|
|
|
|
for (int index = SCOURGE_LINKS - 1; index >= 0; index--)
|
|
{
|
|
float share = (float)index / (float)(SCOURGE_LINKS - 1);
|
|
Vector at = vector_make(span.x * share + chainThrow.position.x, span.y * share + chainThrow.position.y);
|
|
uint32_t seed = Random();
|
|
auto* link = static_cast<Entity_NPC*>(g_game->Spawn(
|
|
{.position = at, .subType = chainThrow.kind, .type = ENTITY_SCOURGE, .variant = SCOURGE_CHAIN, .seed = seed}));
|
|
link->projectileDelay = index;
|
|
link->projectileCooldown = SCOURGE_LINKS;
|
|
link->ClearEntityFlags(FLAG_APPEAR);
|
|
if (previous)
|
|
{
|
|
link->child = previous;
|
|
previous->parent = link;
|
|
}
|
|
link->posOffset.y = SCOURGE_LINK_RAISE;
|
|
previous = link;
|
|
links.push_back(link);
|
|
}
|
|
|
|
Entity_NPC& tip = *links.front();
|
|
tip.velocity = chainThrow.velocity;
|
|
tip.targetPosition = vector_normalized(chainThrow.velocity);
|
|
tip.aiFloat = SCOURGE_TIP_FLIGHT;
|
|
tip.i1 = link_float_make(SCOURGE_TIP_WAVE);
|
|
|
|
Entity_NPC& head = *links.back();
|
|
head.parent = chainThrow.parent;
|
|
head.v1 = chainThrow.offset;
|
|
head.sprite.Play(chainThrow.kind == SCOURGE_CHAIN_TRAP ? "Whip1" : "Whip2");
|
|
for (Entity_NPC* link : links) link->Update();
|
|
return &head;
|
|
}
|
|
|
|
// 00582f60 Draws a chain back: its tip retracts, its first link whips again with a crack.
|
|
static void scourge_whip(Entity_NPC& chain)
|
|
{
|
|
Entity_NPC& tip = link_tip_get(chain);
|
|
tip.state = SCOURGE_LINK_RETRACTING;
|
|
tip.targetPosition = {};
|
|
link_head_get(chain).sprite.Play(chain.subtype == SCOURGE_CHAIN_TRAP ? "Whip1" : "Whip2");
|
|
npc_sound_play(SOUND_MEAT_JUMPS, 1, 2, champion_pitch_get(chain));
|
|
}
|
|
|
|
// 00583170 A chain broken off its ends (its first link not first, its tip not last, or its owner gone) falls apart:
|
|
// every link but the last is killed.
|
|
static void scourge_chain_check(Entity_NPC& link)
|
|
{
|
|
Entity_NPC& head = link_head_get(link);
|
|
Entity_NPC& tip = link_tip_get(link);
|
|
Entity* owner = head.parent.get();
|
|
bool isWhole = head.projectileDelay == 0 && tip.projectileDelay == link.projectileCooldown - 1 && owner && !owner->isDead;
|
|
if (isWhole) return;
|
|
|
|
for (Entity_NPC* each = &head; is_scourge_link(each->child.get()); each = static_cast<Entity_NPC*>(each->child.get()))
|
|
each->Kill(EntityRef());
|
|
}
|
|
|
|
// 005864b0 Picks its next attack by weight among those cooled down: the trap with its target (led by its speed)
|
|
// right below, slices with it lower, the broccoli and the hover always, resting the least.
|
|
static void scourge_attack_pick(Entity_NPC& npc)
|
|
{
|
|
Entity* target = npc.GetPlayerTarget();
|
|
Vector targetPos = target->pos;
|
|
Vector targetVelocity = npc.GetPlayerTarget()->velocity;
|
|
float distance = vector_distance(npc.pos, targetPos);
|
|
Vector lead = vector_make(targetVelocity.x * distance * SCOURGE_LEAD + targetPos.x,
|
|
targetVelocity.y * distance * SCOURGE_LEAD + targetPos.y);
|
|
uint8_t* cooldowns = scourge_cooldowns(npc);
|
|
bool isBelow = npc.pos.y < lead.y;
|
|
|
|
WeightedOutcomePicker picker;
|
|
if (cooldowns[SCOURGE_ATTACK_TRAP] == 0)
|
|
{
|
|
Vector toward = vector_sub(lead, npc.pos);
|
|
float length = vector_length(toward);
|
|
float down = length <= 0 ? toward.y : toward.y * (1 / length);
|
|
if (down > SCOURGE_TRAP_BELOW) picker.AddOutcomeWeight(SCOURGE_ATTACK_TRAP, SCOURGE_ATTACK_WEIGHT);
|
|
}
|
|
if (cooldowns[SCOURGE_ATTACK_SLICE_BOTH] == 0 && isBelow)
|
|
picker.AddOutcomeWeight(SCOURGE_ATTACK_SLICE_BOTH, SCOURGE_ATTACK_WEIGHT);
|
|
if (cooldowns[SCOURGE_ATTACK_SLICE] == 0 && isBelow) picker.AddOutcomeWeight(SCOURGE_ATTACK_SLICE, SCOURGE_ATTACK_WEIGHT);
|
|
if (cooldowns[SCOURGE_ATTACK_BROCCOLI] == 0) picker.AddOutcomeWeight(SCOURGE_ATTACK_BROCCOLI, SCOURGE_ATTACK_WEIGHT);
|
|
if (cooldowns[SCOURGE_ATTACK_HOVER] == 0) picker.AddOutcomeWeight(SCOURGE_ATTACK_HOVER, SCOURGE_ATTACK_WEIGHT);
|
|
picker.AddOutcomeWeight(SCOURGE_ATTACK_NONE, SCOURGE_REST_WEIGHT);
|
|
|
|
int attack = picker.PickOutcome({Random() | 1, SCOURGE_PICK_SHIFT_1, SCOURGE_PICK_SHIFT_2, SCOURGE_PICK_SHIFT_3});
|
|
if (attack == SCOURGE_ATTACK_NONE) return;
|
|
|
|
npc.state = SCOURGE_ATTACK_STATES[attack];
|
|
cooldowns[attack] = SCOURGE_ATTACK_COOLDOWNS[attack];
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, -1);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, -1);
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_HIGH, 0);
|
|
|
|
switch (attack)
|
|
{
|
|
case SCOURGE_ATTACK_TRAP:
|
|
npc.sprite.Play("TrapDown");
|
|
break;
|
|
case SCOURGE_ATTACK_SLICE_BOTH:
|
|
npc.sprite.Play("SliceBoth");
|
|
break;
|
|
case SCOURGE_ATTACK_SLICE:
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_HIGH, 1);
|
|
npc.sprite.Play(npc.pos.x < lead.x ? "SliceRight" : "SliceLeft");
|
|
break;
|
|
case SCOURGE_ATTACK_BROCCOLI:
|
|
npc.sprite.Play("BroccoliAttack");
|
|
if (npc.health / npc.maxHitPoints >= SCOURGE_ANGRY_HEALTH) npc.i1 = 1;
|
|
else
|
|
{
|
|
npc.i1 = SCOURGE_BROCCOLI_SPITS;
|
|
cooldowns[attack] -= SCOURGE_BROCCOLI_WAIT;
|
|
}
|
|
break;
|
|
case SCOURGE_ATTACK_HOVER:
|
|
{
|
|
npc.sprite.Play("HoverStart");
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, 0);
|
|
npc.vectorDeque1[0].x = random_unit() * FULL_TURN_DEGREES;
|
|
npc.vectorDeque1[1].x = (Random() & 1) == 0 ? -SCOURGE_HOVER_TURN : SCOURGE_HOVER_TURN;
|
|
npc.vectorDeque1[0].y = npc.vectorDeque1[1].x;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 00586c40 A volley between its latched chains: a fan over the space between them, leaving a gap that sways about,
|
|
// and a shot at its target.
|
|
static void scourge_volley(Entity_NPC& npc)
|
|
{
|
|
Vector first = vector_sub(chain_tip_position(npc.child.get()), npc.pos);
|
|
Vector second = vector_sub(chain_tip_position(npc.entityRef.get()), npc.pos);
|
|
first = vector_normalized(first);
|
|
second = vector_normalized(second);
|
|
float spread = angle_wrapped(vector_angle(first) - vector_angle(second));
|
|
float half = std::max(std::abs(spread * HALF) - SCOURGE_FAN_INSET, SCOURGE_FAN_MIN);
|
|
Vector across = vector_sub(second, first);
|
|
int shots = std::max((int)(vector_length(across) * SCOURGE_FAN_DENSITY), 1) + 1;
|
|
Random();
|
|
|
|
float gap = std::clamp((random_unit() - HALF) * SCOURGE_GAP_SWAY + npc.vectorDeque1[1].y, -half, half);
|
|
for (int shot = 1; shot < shots; shot++)
|
|
{
|
|
float share = (float)shot / (float)shots;
|
|
if (std::abs(((half + half) * share - half) - gap) <= SCOURGE_GAP_WIDTH) continue;
|
|
|
|
ProjectileParams params = scourge_shot_params(0);
|
|
float speed = random_unit() + SCOURGE_FAN_SPEED;
|
|
Vector velocity = vector_make((across.x * share + first.x) * speed, (across.y * share + first.y) * speed);
|
|
npc.FireProjectiles(npc.pos, velocity, PROJECTILES_SINGLE, params);
|
|
}
|
|
|
|
Vector aim = vector_normalized(vector_sub(npc.GetPlayerTarget()->pos, npc.pos));
|
|
ProjectileParams params = scourge_shot_params(0);
|
|
float speed = random_unit();
|
|
speed = speed + speed + SCOURGE_AIMED_SPEED;
|
|
npc.FireProjectiles(npc.pos, vector_scale(aim, speed), PROJECTILES_SINGLE, params);
|
|
npc.vectorDeque1[1].y = gap;
|
|
scourge_splash(2, npc.pos, SCOURGE_SPLAT_RAISE);
|
|
npc_sound_play(SOUND_BLOODSHOOT, 1, 2, champion_pitch_get(npc));
|
|
}
|
|
|
|
// 00587480 Shots along its latched chains: two, each from a spot on one chain toward the other, at random spots or
|
|
// one time in five level with its target.
|
|
static void scourge_chain_shoot(Entity_NPC& npc)
|
|
{
|
|
Vector first = vector_sub(chain_tip_position(npc.child.get()), npc.pos);
|
|
Vector second = vector_sub(chain_tip_position(npc.entityRef.get()), npc.pos);
|
|
Vector toTarget = vector_sub(npc.GetPlayerTarget()->pos, npc.pos);
|
|
Vector along = vector_normalized(vector_sub(second, first));
|
|
float onto = -first.y * along.y - first.x * along.x;
|
|
Vector nearest = vector_make(onto * along.x + first.x, onto * along.y + first.y);
|
|
float level = (nearest.y * toTarget.y + nearest.x * toTarget.x) / (nearest.y * nearest.y + nearest.x * nearest.x);
|
|
|
|
for (int side = 0; side < 2; side++)
|
|
{
|
|
float share = Random() % SCOURGE_CHAIN_SHOT_LEVEL_ODDS == 0 ? level : random_unit();
|
|
Vector onFirst = vector_make(first.x * share + npc.pos.x, first.y * share + npc.pos.y);
|
|
Vector onSecond = vector_make(second.x * share + npc.pos.x, second.y * share + npc.pos.y);
|
|
Vector from = side == 0 ? onSecond : onFirst;
|
|
Vector direction = vector_normalized(side == 0 ? vector_sub(onFirst, onSecond) : vector_sub(onSecond, onFirst));
|
|
|
|
ProjectileParams params = scourge_shot_params(PROJECTILE_NO_WALL_COLLIDE);
|
|
float speed = random_unit() + SCOURGE_CHAIN_SHOT_SPEED;
|
|
npc.FireProjectiles(from, vector_scale(direction, speed), PROJECTILES_SINGLE, params);
|
|
scourge_splash(1, vector_add(vector_scale(direction, SCOURGE_CHAIN_SHOT_OUT), from), SCOURGE_SPLAT_RAISE);
|
|
}
|
|
npc_sound_play(SOUND_BLOODSHOOT, 1, 2, champion_pitch_get(npc));
|
|
}
|
|
|
|
// Its idle: easing toward its spot over its target, facing it, attacking now and then.
|
|
static void scourge_idle(Entity_NPC& npc, int ticks)
|
|
{
|
|
npc.friction *= SCOURGE_IDLE_FRICTION;
|
|
Vector spot = g_game->room->GetClampedPositionImpl(vector_add(npc.GetPlayerTarget()->pos, npc.v1), SCOURGE_ROOM_MARGIN,
|
|
SCOURGE_ROOM_MARGIN, SCOURGE_ROOM_MARGIN, SCOURGE_ROOM_MARGIN);
|
|
Vector step = vector_scale(vector_sub(spot, npc.pos), SCOURGE_EASE);
|
|
float length = vector_length(step);
|
|
if (length > 1 && length > 0) step = vector_scale(step, 1 / length);
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(step, SCOURGE_IDLE_SPEED * npc.speedMultiplier));
|
|
|
|
Vector toTarget = vector_sub(npc.GetPlayerTarget()->pos, npc.pos);
|
|
const AnimationData* data = npc.sprite.animState.animData;
|
|
bool isIdling = data && npc.sprite.animState.isPlaying && data->name.starts_with("Idle");
|
|
if (!isIdling) npc.sprite.Play("Idle");
|
|
bool isSideways = std::abs(toTarget.x) > std::abs(toTarget.y) && toTarget.y > 0;
|
|
std::string_view facing = !isSideways ? "Idle" : toTarget.x >= 0 ? "IdleRight" : "IdleLeft";
|
|
npc.sprite.SetAnimation(facing, SWITCH_KEEP_FRAME);
|
|
|
|
if (Random() % SCOURGE_SPOT_ODDS == 0)
|
|
{
|
|
float height = random_unit() * SCOURGE_SPOT_RANGE + SCOURGE_SPOT_HEIGHT;
|
|
float turn = random_unit() * SCOURGE_SPOT_TURN + SCOURGE_SPOT_TURN_MIN;
|
|
npc.v1 = vector_rotated(vector_make(0, height), turn);
|
|
}
|
|
|
|
int idleFrames = scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_HIGH) + ticks;
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_HIGH, idleFrames);
|
|
if (scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_HIGH) > SCOURGE_IDLE_FRAMES && Random() % SCOURGE_ATTACK_ODDS == 0)
|
|
scourge_attack_pick(npc);
|
|
}
|
|
|
|
// A shot of the hover ring on its circle, closing in on the middle.
|
|
static Entity_Projectile* scourge_ring_shot(Entity_NPC& npc, const Vector& position, const Vector& center,
|
|
const ColorMod& color)
|
|
{
|
|
uint32_t seed = Random();
|
|
auto* shot = static_cast<Entity_Projectile*>(
|
|
g_game->Spawn({.position = position, .spawner = &npc, .type = ENTITY_PROJECTILE, .seed = seed}));
|
|
ColorMod faded = color;
|
|
faded._tint[3] = 0;
|
|
shot->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
shot->SetColor(faded, SCOURGE_RING_FADE, COLOR_PRIORITY_DEFAULT, true, true);
|
|
shot->parent = &npc;
|
|
shot->targetPosition = center;
|
|
shot->projectileFlags = SCOURGE_RING_FLAGS;
|
|
shot->fallingAccel = SCOURGE_RING_SHOT_FALL;
|
|
shot->homingStrength = SCOURGE_RING_HOMING;
|
|
return shot;
|
|
}
|
|
|
|
// A puff of its big pull at it, small or big.
|
|
static void scourge_puff(Entity_NPC& npc, int subType)
|
|
{
|
|
uint32_t seed = Random();
|
|
Entity* puff = g_game->Spawn({.position = npc.pos, .subType = subType, .type = ENTITY_EFFECT, .variant = EFFECT_BIG_ATTRACT, .seed = seed});
|
|
puff->posOffset = vector_make(0, SCOURGE_HOVER_PUFF_RAISE);
|
|
puff->SetColor(SCOURGE_PUFF_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
puff->Update();
|
|
}
|
|
|
|
// A frame of the hover ring: a pair of shots on the circle (and now and then a bigger one), the pair's angle turning.
|
|
static void scourge_ring_update(Entity_NPC& npc, const Vector& center, int frame)
|
|
{
|
|
Room& room = *g_game->room;
|
|
float radius = vector_distance(center, room.topLeftPos);
|
|
radius = random_unit() * SCOURGE_HOVER_RING_RANGE + radius + SCOURGE_HOVER_RING_OUT;
|
|
Vector out = vector_scale(vector_from_angle(npc.vectorDeque1[0].x), radius);
|
|
for (float side : {-1.0f, 1.0f})
|
|
{
|
|
Entity_Projectile* shot = scourge_ring_shot(npc, vector_add(vector_scale(out, side), center), center, COLOR_GREEN_SPLAT);
|
|
uint32_t size = std::min(Random() % SCOURGE_RING_SIZE_RANGE + SCOURGE_RING_SIZE_MIN, SCOURGE_RING_SIZE_MAX);
|
|
shot->friction = SCOURGE_RING_FRICTION;
|
|
shot->scale = ((float)size - SCOURGE_RING_SIZE_BASE) / SCOURGE_RING_SIZE_BASE;
|
|
}
|
|
if (frame % SCOURGE_HOVER_BIG_EVERY == 0)
|
|
{
|
|
Vector spot = vector_add(vector_scale(vector_from_angle(random_unit() * FULL_TURN_DEGREES), radius), center);
|
|
Entity_Projectile* shot = scourge_ring_shot(npc, spot, center, SCOURGE_RING_BIG_COLOR);
|
|
shot->scale = SCOURGE_RING_BIG_SCALE;
|
|
shot->friction = SCOURGE_RING_BIG_FRICTION;
|
|
}
|
|
|
|
npc.vectorDeque1[0].x = angle_wrapped(npc.vectorDeque1[0].y + npc.vectorDeque1[0].x);
|
|
npc.vectorDeque1[0].y = angle_approach(npc.vectorDeque1[0].y, npc.vectorDeque1[1].x, npc.speedMultiplier + npc.speedMultiplier);
|
|
if (Random() % SCOURGE_HOVER_REVERSE_ODDS == 0) npc.vectorDeque1[1].x = -npc.vectorDeque1[1].x;
|
|
}
|
|
|
|
// Its hover: drifting to the room's middle while the ring closes in, puffing, for 300 frames.
|
|
static void scourge_hover(Entity_NPC& npc, const Vector& center, int ticks)
|
|
{
|
|
npc.friction *= SCOURGE_ATTACK_FRICTION;
|
|
Vector step = vector_scale(vector_sub(center, npc.pos), SCOURGE_EASE);
|
|
if (vector_length(step) > SCOURGE_HOVER_MAX_STEP) step = vector_scale(vector_normalized(step), SCOURGE_HOVER_MAX_STEP);
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(step, npc.speedMultiplier));
|
|
|
|
if (scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) >= 0)
|
|
for (int tick = ticks; tick > 0; tick--)
|
|
{
|
|
int frame = scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW);
|
|
if (frame % 2 == 0 && frame <= SCOURGE_HOVER_SHOT_FRAMES) scourge_ring_update(npc, center, frame);
|
|
|
|
frame = (int16_t)(frame + 1);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, frame);
|
|
if (frame >= SCOURGE_HOVER_FRAMES)
|
|
{
|
|
npc.sprite.Play("HoverEnd");
|
|
npc.state = SCOURGE_HOVER_END;
|
|
continue;
|
|
}
|
|
if (frame % SCOURGE_HOVER_PUFF_EVERY == 0) scourge_puff(npc, 0);
|
|
if (frame % SCOURGE_HOVER_PUFF_SMALL_EVERY == 0) scourge_puff(npc, 1);
|
|
}
|
|
if (!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying) npc.sprite.Play("HoverLoop");
|
|
}
|
|
|
|
// Lets go of both chains and draws them back.
|
|
static void scourge_recall(Entity_NPC& npc)
|
|
{
|
|
npc.state = SCOURGE_RECALL;
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, -1);
|
|
if (npc.child) scourge_whip(*static_cast<Entity_NPC*>(npc.child.get()));
|
|
if (npc.entityRef) scourge_whip(*static_cast<Entity_NPC*>(npc.entityRef.get()));
|
|
npc.sprite.Play("TrapEndDown");
|
|
}
|
|
|
|
// Whether its target stands between its two latched chains, as seen from it.
|
|
static bool scourge_is_target_trapped(Entity_NPC& npc, const Vector& target)
|
|
{
|
|
Vector toTarget = vector_sub(target, npc.pos);
|
|
Vector first = vector_sub(chain_tip_position(npc.child.get()), npc.pos);
|
|
Vector second = vector_sub(chain_tip_position(npc.entityRef.get()), npc.pos);
|
|
return toTarget.x * first.y - toTarget.y * first.x >= 0 && toTarget.x * second.y - toTarget.y * second.x <= 0;
|
|
}
|
|
|
|
// The trap: its chains thrown down, rising, shooting between them while its target stays trapped.
|
|
static void scourge_trap(Entity_NPC& npc, int ticks)
|
|
{
|
|
npc.friction *= SCOURGE_ATTACK_FRICTION;
|
|
if (npc.sprite.IsEventTriggered("Sound")) npc.PlaySound(SOUND_SKIN_PULL, 1, 2, false, 1);
|
|
|
|
int rise = scourge_half_get(npc.projectileDelay, SCOURGE_HALF_LOW);
|
|
bool isShooting = npc.sprite.IsEventTriggered("Shoot");
|
|
if (isShooting)
|
|
{
|
|
npc.PlaySound(SOUND_WHIP, 1, 2, false, 1);
|
|
npc.child = scourge_chain_spawn({.position = npc.pos,
|
|
.velocity = vector_make(-SCOURGE_TRAP_THROW, SCOURGE_TRAP_THROW),
|
|
.offset = vector_make(-SCOURGE_TRAP_OFFSET, 0),
|
|
.parent = &npc});
|
|
npc.entityRef = scourge_chain_spawn({.position = npc.pos,
|
|
.velocity = vector_make(SCOURGE_TRAP_THROW, SCOURGE_TRAP_THROW),
|
|
.offset = vector_make(SCOURGE_TRAP_OFFSET, 0),
|
|
.parent = &npc});
|
|
rise = SCOURGE_TRAP_RISE;
|
|
}
|
|
if (isShooting || rise > 0)
|
|
{
|
|
rise = std::max(rise - ticks, 0);
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, rise);
|
|
npc.velocity.y -= std::min((float)(int16_t)rise, SCOURGE_TRAP_RISE_MAX);
|
|
}
|
|
if (!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying)
|
|
{
|
|
npc.sprite.Play("TrapLoopDown");
|
|
rise = scourge_half_get(npc.projectileDelay, SCOURGE_HALF_LOW);
|
|
}
|
|
|
|
if ((int16_t)rise >= 0)
|
|
{
|
|
bool isLost = !npc.child || !npc.entityRef || scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) > SCOURGE_TRAP_FRAMES;
|
|
if (!isLost && npc.GetIntervalTicks(SCOURGE_TRAP_CHECK_EVERY, 0))
|
|
{
|
|
Vector target = npc.GetPlayerTarget()->pos;
|
|
isLost = chain_is_latched(npc.child.get()) && chain_is_latched(npc.entityRef.get()) &&
|
|
!scourge_is_target_trapped(npc, target);
|
|
}
|
|
if (isLost) scourge_recall(npc);
|
|
else if (scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) < 0 && chain_is_latched(npc.child.get()) &&
|
|
chain_is_latched(npc.entityRef.get()))
|
|
{
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, 0);
|
|
npc.vectorDeque1[1].y = 0;
|
|
int isSingle = (int)(Random() & 1);
|
|
if (std::abs(npc.child->pos.y - npc.entityRef->pos.y) > SCOURGE_TRAP_LEVEL) isSingle = 0;
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_HIGH, isSingle);
|
|
}
|
|
}
|
|
|
|
int frame = scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW);
|
|
if (frame >= 0)
|
|
{
|
|
int next = (int16_t)(frame + ticks);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, next);
|
|
int previous = next - ticks;
|
|
bool isBoth = npc.child && npc.entityRef;
|
|
bool isSingle = scourge_half_get(npc.projectileDelay, SCOURGE_HALF_HIGH) != 0;
|
|
if (!isSingle && previous / SCOURGE_VOLLEY_EVERY < next / SCOURGE_VOLLEY_EVERY && isBoth) scourge_volley(npc);
|
|
else if (isSingle && previous / SCOURGE_CHAIN_SHOT_EVERY < next / SCOURGE_CHAIN_SHOT_EVERY && isBoth)
|
|
scourge_chain_shoot(npc);
|
|
}
|
|
|
|
// The original reads an unset register for the springs' length, clamped to 260 to 400. Its usual small value
|
|
// gives 260.
|
|
for (Entity* chain : {npc.child.get(), npc.entityRef.get()})
|
|
if (chain && chain_is_latched(chain))
|
|
scourge_spring_pull(npc, chain_tip_position(chain), vector_make(SCOURGE_SPRING_LENGTH, SCOURGE_SPRING_STIFFNESS),
|
|
SCOURGE_SPRING_DAMPING);
|
|
}
|
|
|
|
// The slice's throw on Shoot: both chains at its target and away, or one, each flipped to a random side.
|
|
static void scourge_slice_throw(Entity_NPC& npc)
|
|
{
|
|
Vector direction = vector_normalized(vector_sub(npc.targetPosition, npc.pos));
|
|
bool isFlipped = Random() & 1;
|
|
npc.PlaySound(SOUND_WHIP, 1, 2, false, 1);
|
|
if (scourge_half_get(npc.projectileDelay, SCOURGE_HALF_HIGH) == 0)
|
|
{
|
|
npc.child = scourge_chain_spawn({.position = npc.pos,
|
|
.velocity = vector_scale(direction, SCOURGE_SLICE_THROW),
|
|
.parent = &npc,
|
|
.kind = SCOURGE_CHAIN_SLICE});
|
|
chain_flip_set(scourge_child_chain(npc), isFlipped);
|
|
npc.entityRef = scourge_chain_spawn({.position = npc.pos,
|
|
.velocity = vector_scale(direction, -SCOURGE_SLICE_THROW),
|
|
.parent = &npc,
|
|
.kind = SCOURGE_CHAIN_SLICE});
|
|
chain_flip_set(is_scourge_link(npc.entityRef.get()) ? npc.entityRef.get() : nullptr, isFlipped);
|
|
}
|
|
else
|
|
{
|
|
npc.child = scourge_chain_spawn({.position = npc.pos,
|
|
.velocity = vector_scale(direction, SCOURGE_SLICE_THROW),
|
|
.parent = &npc,
|
|
.kind = SCOURGE_CHAIN_SLICE_ONE});
|
|
chain_flip_set(scourge_child_chain(npc), isFlipped);
|
|
}
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, 0);
|
|
}
|
|
|
|
// The slice: thrown chains, then pulled along the walls once latched, ringing shots, drifting to the middle.
|
|
static void scourge_slice(Entity_NPC& npc, const Vector& center, int ticks)
|
|
{
|
|
npc.friction *= SCOURGE_ATTACK_FRICTION;
|
|
bool isSingle = scourge_half_get(npc.projectileDelay, SCOURGE_HALF_HIGH) != 0;
|
|
if (scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) < 0)
|
|
{
|
|
if (npc.sprite.IsEventTriggered("Sound"))
|
|
{
|
|
npc.PlaySound(SOUND_SKIN_PULL, 1, 2, false, 1);
|
|
npc.targetPosition = npc.GetPlayerTarget()->pos;
|
|
}
|
|
if (npc.sprite.IsEventTriggered("Shoot")) scourge_slice_throw(npc);
|
|
else if (scourge_half_get(npc.projectileDelay, SCOURGE_HALF_LOW) < 0) return;
|
|
|
|
Entity* first = npc.child.get();
|
|
Entity* second = npc.entityRef.get();
|
|
bool isLatched = (first || second) && (!first || chain_is_latched(first)) && (!second || chain_is_latched(second));
|
|
if (isLatched)
|
|
{
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, 0);
|
|
Entity* shown = first ? first : second;
|
|
npc.sprite.Play(shown->sprite.isFlipX ? "SlicePullLeft" : "SlicePullRight");
|
|
if (isSingle && npc.sprite.animState.animData) npc.sprite.animState.SetFrameFloat(SCOURGE_SLICE_ONE_FRAME);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (npc.sprite.IsEventTriggered("Shoot"))
|
|
{
|
|
if (npc.child) chain_crawl_stop(npc.child.get());
|
|
if (npc.entityRef) chain_crawl_stop(npc.entityRef.get());
|
|
}
|
|
Entity* shown = npc.child ? npc.child.get() : npc.entityRef.get();
|
|
if ((!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying) && shown)
|
|
npc.sprite.Play(shown->sprite.isFlipX ? "SlicePullLoopLeft" : "SlicePullLoopRight");
|
|
|
|
for (int tick = ticks; tick > 0; tick--)
|
|
{
|
|
int frame = (int16_t)(scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) + 1);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, frame);
|
|
if (isSingle || frame < SCOURGE_VOLLEY_EVERY || frame % SCOURGE_VOLLEY_EVERY != 0) continue;
|
|
|
|
ProjectileParams params;
|
|
params.color = SHOT_COLOR_FIERY;
|
|
params.fallingAccelModifier = SCOURGE_RING_FALL;
|
|
npc.FireProjectiles(npc.pos, vector_make(SCOURGE_RING_SPEED, SCOURGE_RING_SHOTS), PROJECTILES_RING, params);
|
|
scourge_splash(2, npc.pos, SCOURGE_SPLAT_RAISE);
|
|
npc_sound_play(SOUND_BLOODSHOOT, 1, 2, champion_pitch_get(npc));
|
|
}
|
|
}
|
|
|
|
if (scourge_half_get(npc.projectileDelay, SCOURGE_HALF_LOW) < 0) return;
|
|
scourge_spring_pull(npc, center, vector_make(0, SCOURGE_SPRING_STIFFNESS), SCOURGE_CENTER_DAMPING);
|
|
int limit = isSingle ? SCOURGE_SLICE_ONE_FRAMES : SCOURGE_SLICE_FRAMES;
|
|
if (scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) > limit || (!npc.child && !npc.entityRef)) scourge_recall(npc);
|
|
}
|
|
|
|
// The broccoli: its spit (once, or three times below half health) kicking it back.
|
|
static void scourge_broccoli(Entity_NPC& npc, int ticks)
|
|
{
|
|
npc.friction *= SCOURGE_ATTACK_FRICTION;
|
|
if (npc.sprite.IsEventTriggered("Sound"))
|
|
{
|
|
int count = (int16_t)(scourge_half_get(npc.projectileCooldown, SCOURGE_HALF_LOW) + 1);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, count);
|
|
if (count == 0 || count == 2) npc.PlaySound(SOUND_MEAT_JUMPS, 1, 2, false, 1);
|
|
else if (count == 1) npc.PlaySound(SOUND_ANIMAL_SQUISH, 1, 2, false, 1);
|
|
}
|
|
if (npc.sprite.IsEventTriggered("Shoot"))
|
|
{
|
|
npc.PlaySound(SOUND_MEATHEADSHOOT, 1, 2, false, 1);
|
|
npc.PlaySound(SOUND_HEARTOUT, 1, 2, false, 1);
|
|
game_shake_screen(SCOURGE_BROCCOLI_SHAKE);
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, SCOURGE_BROCCOLI_KICK_FRAMES);
|
|
Vector aim = vector_normalized(vector_sub(npc.GetPlayerTarget()->pos, npc.pos));
|
|
npc.targetPosition = vector_scale(aim, SCOURGE_BROCCOLI_KICK);
|
|
|
|
ProjectileParams params;
|
|
params.color = COLOR_GREEN_SPLAT;
|
|
params.bulletFlags = PROJECTILE_BROCCOLI;
|
|
Entity_Projectile* shot = npc.FireBossProjectiles(1, {}, 0, params);
|
|
shot->height = SCOURGE_BROCCOLI_HEIGHT;
|
|
shot->velocity = vector_scale(aim, SCOURGE_BROCCOLI_SPEED);
|
|
shot->fallingAccel = SCOURGE_BROCCOLI_FALL;
|
|
shot->fallingSpeed = 0;
|
|
shot->homingStrength = SCOURGE_BROCCOLI_HOMING;
|
|
bool isHealthy = npc.health / npc.maxHitPoints >= SCOURGE_ANGRY_HEALTH;
|
|
shot->subtype = isHealthy ? SCOURGE_BROCCOLI_BIG : SCOURGE_BROCCOLI_SMALL;
|
|
shot->scale = isHealthy ? 1 : SCOURGE_BROCCOLI_SMALL_SCALE;
|
|
blood_explosion_spawn(4, npc.pos, vector_make(0, SCOURGE_SPLAT_RAISE), SCOURGE_SPIT_COLOR, {});
|
|
npc.i1--;
|
|
}
|
|
|
|
int kick = scourge_half_get(npc.projectileDelay, SCOURGE_HALF_LOW);
|
|
if (kick > 0)
|
|
{
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, kick - ticks);
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(npc.targetPosition, npc.speedMultiplier));
|
|
npc.targetPosition = vector_scale(npc.targetPosition, timescale_factor(SCOURGE_BROCCOLI_KICK_DECAY, npc.speedMultiplier));
|
|
}
|
|
if (npc.sprite.animState.animFrame > SCOURGE_BROCCOLI_LOOP_FRAME && npc.i1 >= 1 && npc.sprite.animState.animData)
|
|
npc.sprite.animState.SetFrameFloat(SCOURGE_BROCCOLI_LOOP_TO);
|
|
}
|
|
|
|
// Its death throes: the Death animation's Shoot bursts a splash of its blood.
|
|
static void scourge_death_update(Entity_NPC& npc)
|
|
{
|
|
const AnimationData* data = npc.sprite.animState.animData;
|
|
if (npc.state != NPC_STATE_DEATH || !data || !npc.sprite.animState.isPlaying || data->name != "Death") return;
|
|
if (!npc.sprite.animState.IsEventTriggered("Shoot")) return;
|
|
|
|
float pitch = champion_pitch_get(npc);
|
|
npc_sound_play(SOUND_SKIN_PULL, 1, 2, pitch);
|
|
npc_sound_play(SOUND_DEATH_BURST_SMALL, 1, 2, pitch);
|
|
scourge_splash(3, npc.pos, SCOURGE_DEATH_RAISE);
|
|
}
|
|
|
|
// 00587aa0 The Scourge (909.0): it hovers over its target, then traps it between two chains thrown down, slices
|
|
// along the walls with one or two chains, spits broccoli shots, or hovers in the middle inside a closing ring.
|
|
void boss_scourge_update(Entity_NPC& npc)
|
|
{
|
|
Room& room = *g_game->room;
|
|
Vector center = vector_make((room.bottomRightPos.x + room.topLeftPos.x) * HALF, (room.bottomRightPos.y + room.topLeftPos.y) * HALF);
|
|
int ticks = npc_ticks_get(npc);
|
|
if (npc.aiCallPhase == AI_PHASE_PRE_UPDATE) return scourge_death_update(npc);
|
|
|
|
while (npc.vectorDeque1.size() < 2) npc.vectorDeque1.push_back({});
|
|
int state = npc.state;
|
|
switch (state)
|
|
{
|
|
case NPC_STATE_INIT:
|
|
npc.state = SCOURGE_IDLE;
|
|
npc.v1 = vector_make(0, SCOURGE_HOVER_HEIGHT);
|
|
npc.splatColor = COLOR_GREEN_SPLAT;
|
|
npc.aiCallFlags = AI_CALL_PRE_UPDATE;
|
|
npc.v2.x = 0;
|
|
scourge_cooldowns(npc)[4] = 0;
|
|
scourge_cooldowns(npc)[5] = 0;
|
|
npc.projectileCooldown = 0;
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, 0);
|
|
npc.vectorDeque1[0] = {};
|
|
npc.vectorDeque1[1] = {};
|
|
break;
|
|
case SCOURGE_IDLE:
|
|
scourge_idle(npc, ticks);
|
|
break;
|
|
case SCOURGE_HOVER:
|
|
scourge_hover(npc, center, ticks);
|
|
break;
|
|
case SCOURGE_HOVER_END:
|
|
case SCOURGE_RECALL:
|
|
npc.friction *= SCOURGE_ATTACK_FRICTION;
|
|
break;
|
|
case SCOURGE_TRAP:
|
|
scourge_trap(npc, ticks);
|
|
break;
|
|
case SCOURGE_SLICE:
|
|
scourge_slice(npc, center, ticks);
|
|
break;
|
|
case SCOURGE_BROCCOLI:
|
|
scourge_broccoli(npc, ticks);
|
|
break;
|
|
}
|
|
|
|
bool isEnding = state == SCOURGE_HOVER_END || state == SCOURGE_RECALL || state == SCOURGE_BROCCOLI;
|
|
if (isEnding && (!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying))
|
|
{
|
|
npc.state = SCOURGE_IDLE;
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_LOW, -1);
|
|
scourge_half_set(npc.projectileDelay, SCOURGE_HALF_LOW, -1);
|
|
scourge_half_set(npc.projectileCooldown, SCOURGE_HALF_HIGH, 0);
|
|
}
|
|
|
|
if (npc.GetIntervalTicks(SCOURGE_COOLDOWN_EVERY, 0))
|
|
for (int attack = 0; attack < SCOURGE_ATTACKS; attack++)
|
|
if (scourge_cooldowns(npc)[attack] != 0) scourge_cooldowns(npc)[attack]--;
|
|
|
|
if (npc.isDead)
|
|
for (Entity* shot : entity_query(ENTITY_PROJECTILE, 0))
|
|
if (shot->parent.get() == &npc) shot->isDead = true;
|
|
if (npc.knockbackCountdown >= 3) npc.knockbackCountdown = 2;
|
|
}
|
|
|
|
// Moves a chain's tip that is still flying: on along its heading until it meets a wall, where it latches.
|
|
static void chain_tip_fly(Entity_NPC& npc, Entity_NPC& head)
|
|
{
|
|
Room& room = *g_game->room;
|
|
npc.mass = SCOURGE_TIP_MASS;
|
|
if (vector_is_zero(npc.targetPosition)) return;
|
|
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(npc.targetPosition, SCOURGE_TIP_THRUST * npc.speedMultiplier));
|
|
float speed = vector_length(npc.velocity);
|
|
if (speed > SCOURGE_TIP_SPEED_MAX && speed > 0) npc.velocity = vector_scale(npc.velocity, SCOURGE_TIP_SPEED_MAX / speed);
|
|
|
|
float left = room.topLeftPos.x - SCOURGE_WALL_OUT;
|
|
float right = room.bottomRightPos.x + SCOURGE_WALL_OUT;
|
|
float top = room.topLeftPos.y - SCOURGE_WALL_OUT + SCOURGE_WALL_LOWER;
|
|
float bottom = room.bottomRightPos.y + SCOURGE_WALL_OUT + SCOURGE_WALL_LOWER;
|
|
int side = 0;
|
|
if (npc.pos.x < left) side = 0;
|
|
else if (npc.pos.x > right) side = 2;
|
|
else if (npc.pos.y < top) side = 1;
|
|
else if (npc.pos.y > bottom) side = 3;
|
|
else return;
|
|
|
|
if (npc.pos.x < left) npc.pos.x = left;
|
|
else if (npc.pos.x > right) npc.pos.x = right;
|
|
if (npc.pos.y < top) npc.pos.y = top;
|
|
else if (npc.pos.y > bottom) npc.pos.y = bottom;
|
|
npc.PlaySound(SOUND_MEATY_DEATHS, 1, 2, false, 1);
|
|
game_shake_screen(SCOURGE_IMPACT_SHAKE);
|
|
float raise = SCOURGE_BLOOD_RAISE - random_unit() * SCOURGE_BLOOD_RAISE_RANGE;
|
|
blood_explosion_spawn(4, npc.pos, vector_make(0, raise), COLOR_DEFAULT, {})
|
|
->SetColor(COLOR_GREEN_SPLAT, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
for (int piece = 0; piece < SCOURGE_IMPACT_PIECES; piece++)
|
|
{
|
|
Vector direction = vector_rotated(npc.targetPosition, (random_unit() - HALF) * SCOURGE_PIECE_SPREAD);
|
|
float out = random_unit();
|
|
Vector at = vector_make(direction.x * out * SCOURGE_PIECE_OUT + npc.pos.x, direction.y * out * SCOURGE_PIECE_OUT + npc.pos.y);
|
|
uint32_t seed = Random();
|
|
g_game->Spawn({.position = at, .velocity = vector_scale(direction, SCOURGE_PIECE_SPEED), .type = ENTITY_EFFECT,
|
|
.variant = EFFECT_ROCK_PARTICLE, .seed = seed})
|
|
->Update();
|
|
}
|
|
|
|
npc.velocity = {};
|
|
npc.targetPosition = npc.pos;
|
|
npc.state = SCOURGE_LINK_LATCHED;
|
|
npc.v2.x = vector_distance(head.pos, npc.pos);
|
|
npc.i1 = link_float_make(SCOURGE_LATCHED_WAVE);
|
|
npc.aiFloat = SCOURGE_LATCHED_FLIGHT;
|
|
head.sprite.Play(npc.subtype == SCOURGE_CHAIN_TRAP ? "Whip1_Latched" : "Whip2_Latched");
|
|
npc.sprite.Play("WallImpact");
|
|
npc.sprite.rotation = SCOURGE_WALL_ROTATIONS[side];
|
|
if (npc.subtype == SCOURGE_CHAIN_TRAP) return;
|
|
npc.i2 = link_float_make(SCOURGE_SLIDE_START);
|
|
npc.stateFrame = (side + 1 + (npc.sprite.isFlipX ? 2 : 0)) & 3;
|
|
}
|
|
|
|
// The mess a crawling single chain leaves where it bleeds: creep, a splat, maybe a maggot, and a sick shot toward
|
|
// its first link.
|
|
static void chain_tip_creep(Entity_NPC& npc, Entity_NPC& head, const Vector& gibPos)
|
|
{
|
|
Room& room = *g_game->room;
|
|
Vector creepPos = room.GetClampedPositionImpl(gibPos, SCOURGE_ROOM_MARGIN, SCOURGE_ROOM_MARGIN, SCOURGE_ROOM_MARGIN,
|
|
SCOURGE_ROOM_MARGIN);
|
|
Vector toHead = vector_normalized(vector_sub(head.pos, creepPos));
|
|
|
|
// The original works out a free spot near the creep that it never uses. Its draws stay.
|
|
float reach = random_unit() * SCOURGE_CREEP_SPOT_RANGE + SCOURGE_CREEP_SPOT_MIN;
|
|
Vector spot = vector_add(vector_scale(toHead, reach), creepPos);
|
|
if (room_grid_collision_at(spot) != COLLISION_NONE)
|
|
{
|
|
room_free_pickup_position_find({.position = spot});
|
|
random_unit();
|
|
random_unit();
|
|
}
|
|
|
|
uint32_t seed = Random();
|
|
Entity* creep = g_game->Spawn({.position = creepPos, .spawner = &npc, .subType = SCOURGE_CREEP_SUBTYPE, .type = ENTITY_EFFECT,
|
|
.variant = EFFECT_CREEP_GREEN, .seed = seed});
|
|
creep->sprite.scale = vector_make(SCOURGE_CREEP_SCALE, SCOURGE_CREEP_SCALE);
|
|
Entity_Effect* splat = npc.MakeSplat((random_unit() * HALF + HALF) * SCOURGE_SPLAT_SIZE);
|
|
splat->SetColor(COLOR_GREEN_SPLAT, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
splat->Update();
|
|
if (Random() % SCOURGE_MAGGOT_ODDS == 0)
|
|
small_maggot_jump_spawn(gibPos, SCOURGE_MAGGOT_HEIGHT,
|
|
room.GetClampedPositionImpl(gibPos, SCOURGE_MAGGOT_MARGIN, SCOURGE_MAGGOT_MARGIN,
|
|
SCOURGE_MAGGOT_MARGIN, SCOURGE_MAGGOT_MARGIN));
|
|
|
|
ProjectileParams params;
|
|
params.color = SHOT_COLOR_SICK;
|
|
params.bulletFlags = PROJECTILE_NO_WALL_COLLIDE;
|
|
npc.FireBossProjectiles(1, vector_add(vector_scale(toHead, SCOURGE_SLIDE_SHOT_REACH), creepPos), 0, params);
|
|
}
|
|
|
|
// A crawling tip bleeds now and then, faster for a single chain.
|
|
static void chain_tip_bleed(Entity_NPC& npc, Entity_NPC& head)
|
|
{
|
|
bool isSingle = npc.subtype == SCOURGE_CHAIN_SLICE_ONE;
|
|
if (random_below(isSingle ? SCOURGE_BLEED_ONE_ODDS : SCOURGE_BLEED_ODDS) != 0) return;
|
|
|
|
Vector wall = vector_from_direction((npc.stateFrame + 1 + (npc.sprite.isFlipX ? 2 : 0)) & 3);
|
|
scourge_splash(4, npc.pos, SCOURGE_BLOOD_RAISE - random_unit() * SCOURGE_BLOOD_RAISE_RANGE);
|
|
if (Random() % SCOURGE_GIB_ODDS != 0 && !isSingle) return;
|
|
|
|
Vector direction = vector_rotated(wall, (random_unit() - HALF) * SCOURGE_PIECE_SPREAD);
|
|
float out = random_unit();
|
|
Vector gibPos = vector_make(out * direction.x * SCOURGE_PIECE_OUT + npc.pos.x, out * direction.y * SCOURGE_PIECE_OUT + npc.pos.y);
|
|
uint32_t seed = Random();
|
|
Entity* gib = g_game->Spawn({.position = gibPos, .velocity = vector_scale(direction, SCOURGE_PIECE_SPEED), .type = ENTITY_EFFECT,
|
|
.variant = EFFECT_BLOOD_PARTICLE, .seed = seed});
|
|
gib->sprite.color = COLOR_GREEN_SPLAT;
|
|
gib->Update();
|
|
game_shake_screen(SCOURGE_GIB_SHAKE);
|
|
if (isSingle) chain_tip_creep(npc, head, gibPos);
|
|
}
|
|
|
|
// A latched tip: a slicing one crawls along the walls (turning their corners), sags its chain and bleeds.
|
|
static void chain_tip_latched(Entity_NPC& npc, Entity_NPC& head)
|
|
{
|
|
Room& room = *g_game->room;
|
|
float speed = link_float_get(npc.i2);
|
|
if (npc.stateFrame >= 0 && speed >= 0)
|
|
{
|
|
bool isSingle = npc.subtype == SCOURGE_CHAIN_SLICE_ONE;
|
|
speed = isSingle ? std::min(speed + SCOURGE_SLIDE_ONE_STEP, SCOURGE_SLIDE_ONE_MAX) : std::min(speed + SCOURGE_SLIDE_STEP, SCOURGE_SLIDE_MAX);
|
|
Vector step = vector_from_direction(npc.stateFrame);
|
|
npc.targetPosition = vector_make(speed * step.x + npc.targetPosition.x, speed * step.y + npc.targetPosition.y);
|
|
|
|
bool isCorner = false;
|
|
float left = room.topLeftPos.x - SCOURGE_WALL_OUT;
|
|
float right = room.bottomRightPos.x + SCOURGE_WALL_OUT;
|
|
float top = room.topLeftPos.y - SCOURGE_WALL_OUT + SCOURGE_WALL_LOWER;
|
|
float bottom = room.bottomRightPos.y + SCOURGE_WALL_OUT + SCOURGE_WALL_LOWER;
|
|
if (npc.targetPosition.x < left)
|
|
{
|
|
isCorner = npc.stateFrame == 0;
|
|
npc.targetPosition.x = left;
|
|
}
|
|
else if (npc.targetPosition.x > right)
|
|
{
|
|
isCorner = npc.stateFrame == 2;
|
|
npc.targetPosition.x = right;
|
|
}
|
|
if (npc.targetPosition.y < top)
|
|
{
|
|
npc.targetPosition.y = top;
|
|
isCorner = isCorner || npc.stateFrame == 1;
|
|
}
|
|
else if (npc.targetPosition.y > bottom)
|
|
{
|
|
isCorner = isCorner || npc.stateFrame == 3;
|
|
npc.targetPosition.y = bottom;
|
|
}
|
|
|
|
if (Random() % SCOURGE_SLIDE_SLOW_ODDS == 0) speed *= SCOURGE_SLIDE_SLOW;
|
|
int flip = npc.sprite.isFlipX ? 2 : 0;
|
|
if (isCorner)
|
|
{
|
|
speed *= SCOURGE_SLIDE_SLOW;
|
|
npc.stateFrame = (npc.stateFrame + 1 + flip) & 3;
|
|
}
|
|
npc.i2 = link_float_make(speed);
|
|
npc.sprite.rotation = SCOURGE_WALL_ROTATIONS[(npc.stateFrame + 1 + (2 - flip)) & 3];
|
|
npc.v2.y = flip ? std::max(npc.v2.y - SCOURGE_SAG_STEP, -SCOURGE_SAG_MAX) : std::min(npc.v2.y + SCOURGE_SAG_STEP, SCOURGE_SAG_MAX);
|
|
if (speed > SCOURGE_BLEED_SPEED) chain_tip_bleed(npc, head);
|
|
}
|
|
|
|
npc.velocity = vector_sub(npc.targetPosition, npc.pos);
|
|
npc.friction *= SCOURGE_LATCHED_DECAY;
|
|
npc.aiFloat *= SCOURGE_LATCHED_DECAY;
|
|
npc.i1 = link_float_make(link_float_get(npc.i1) * SCOURGE_LATCHED_DECAY);
|
|
}
|
|
|
|
// A middle link hangs on the line between its first link and its tip, waving and sagging with the tip.
|
|
static void chain_link_hang(Entity_NPC& npc, Entity_NPC& head, Entity_NPC& tip)
|
|
{
|
|
Vector span = vector_sub(tip.pos, head.pos);
|
|
float length = vector_distance(head.pos, tip.pos);
|
|
Vector side = vector_rotated(vector_normalized(span), RIGHT_ANGLE_DEGREES);
|
|
float share = (float)npc.projectileDelay / (float)(npc.projectileCooldown - 1);
|
|
Vector at = vector_make(span.x * share + head.pos.x, span.y * share + head.pos.y);
|
|
npc.posOffset.y = (tip.posOffset.y - head.posOffset.y) * share + head.posOffset.y;
|
|
|
|
float peak = share < HALF ? share + share : (1 - share) + (1 - share);
|
|
float wave = std::sin(share * length * link_float_get(tip.i1)) * link_float_get(tip.i2) * peak;
|
|
float bend = (share + share) - 1;
|
|
float offset = wave + (1 - (float)((double)bend * (double)bend)) * tip.v2.y;
|
|
npc.velocity = vector_make(side.x * offset + at.x - npc.pos.x, side.y * offset + at.y - npc.pos.y);
|
|
}
|
|
|
|
// The links share their hurt: each above the weakest drops to it, and the Scourge takes a tenth of the most.
|
|
static void chain_health_share(Entity_NPC& npc, Entity_NPC& head)
|
|
{
|
|
float weakest = SCOURGE_HEALTH_UNSET;
|
|
for (Entity* link = &head; is_scourge_link(link); link = link->child.get())
|
|
weakest = std::min(link->health - link->pendingDamage, weakest);
|
|
|
|
float most = 0;
|
|
EntityRef source(&npc);
|
|
for (Entity* link = &head; is_scourge_link(link); link = link->child.get())
|
|
{
|
|
float left = link->health - link->pendingDamage;
|
|
if (left <= weakest) continue;
|
|
link->TakeDamage(left - weakest, 0, source, SCOURGE_LINK_DAMAGE_COUNTDOWN);
|
|
most = std::max(left - weakest, most);
|
|
}
|
|
if (head.parent && most > 0) head.parent->TakeDamage(most * SCOURGE_SHARED_DAMAGE, 0, source, SCOURGE_LINK_DAMAGE_COUNTDOWN);
|
|
}
|
|
|
|
// A dead link lets go of its neighbours, checks what is left of its chain and shows its wall impact.
|
|
static void chain_link_death(Entity_NPC& npc)
|
|
{
|
|
if (!npc.isDead) return;
|
|
Entity_NPC& head = link_head_get(npc);
|
|
Entity_NPC& tip = link_tip_get(npc);
|
|
npc.parent = nullptr;
|
|
npc.child = nullptr;
|
|
scourge_chain_check(head);
|
|
scourge_chain_check(tip);
|
|
npc.sprite.Play("WallImpact");
|
|
}
|
|
|
|
// 00584140 A link of the Scourge's chain (909.10): the first hangs from the Scourge, the tip flies until it latches
|
|
// onto a wall (where a slicing one crawls), and the others hang between them. Touching only far from the Scourge.
|
|
void scourge_chain_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.aiCallPhase == AI_PHASE_PRE_UPDATE) return chain_link_death(npc);
|
|
if (npc.state == NPC_STATE_INIT)
|
|
{
|
|
npc.AddEntityFlags(FLAG_NO_STATUS_EFFECTS);
|
|
npc.state = SCOURGE_LINK_FLYING;
|
|
npc.aiCallFlags = AI_CALL_PRE_UPDATE;
|
|
npc.stateFrame = -1;
|
|
npc.isBoss = false;
|
|
npc.splatColor = COLOR_GREEN_SPLAT;
|
|
}
|
|
if (npc.isDead) return;
|
|
|
|
Entity_NPC& head = link_head_get(npc);
|
|
Entity_NPC& tip = link_tip_get(npc);
|
|
Entity* owner = head.parent.get();
|
|
bool isFar = !owner || vector_distance(head.pos, npc.pos) > npc.config->collisionRadius + owner->size + SCOURGE_LINK_REACH;
|
|
npc.entityCollisionClass = isFar ? ENTCOLL_ALL : ENTCOLL_NONE;
|
|
if (npc.state != NPC_STATE_INIT) scourge_chain_check(npc);
|
|
|
|
if (&npc == &head)
|
|
{
|
|
npc.renderDepthOffset = tip.pos.y - npc.pos.y >= 0 ? SCOURGE_HEAD_DEPTH : -SCOURGE_HEAD_DEPTH;
|
|
if (npc.subtype == SCOURGE_CHAIN_TRAP) npc.sprite.isFlipX = npc.pos.x < tip.pos.x;
|
|
if (!owner) npc.Kill(EntityRef());
|
|
else npc.velocity = vector_sub(vector_add(owner->pos, npc.v1), npc.pos);
|
|
}
|
|
else if (&npc == &tip && npc.state == SCOURGE_LINK_RETRACTING)
|
|
{
|
|
npc.velocity = vector_scale(vector_sub(head.pos, npc.pos), SCOURGE_RETRACT_PULL);
|
|
npc.friction = 1;
|
|
float shrink = timescale_factor(SCOURGE_RETRACT_SHRINK, npc.speedMultiplier);
|
|
head.sprite.scale = vector_scale(head.sprite.scale, shrink);
|
|
if (vector_distance(head.pos, npc.pos) < SCOURGE_RETRACT_NEAR || head.sprite.scale.x < SCOURGE_RETRACT_SMALL)
|
|
{
|
|
std::vector<Entity*> links;
|
|
for (Entity* link = &head; is_scourge_link(link); link = link->child.get()) links.push_back(link);
|
|
for (Entity* link : links) link->Remove();
|
|
return;
|
|
}
|
|
}
|
|
else if (&npc == &tip && npc.state == SCOURGE_LINK_LATCHED) chain_tip_latched(npc, head);
|
|
else if (&npc == &tip) chain_tip_fly(npc, head);
|
|
else chain_link_hang(npc, head, tip);
|
|
|
|
chain_health_share(npc, head);
|
|
npc.friction *= SCOURGE_CHAIN_FRICTION;
|
|
}
|
|
|
|
// 005832c0 The first link draws the whole chain as one strip of its first layer's frame, a quad from each link to
|
|
// the next, the frame's height spread over them (16 shorter while a trap chain is latched).
|
|
static void scourge_chain_strip_render(Entity_NPC& npc, const Vector& offset)
|
|
{
|
|
namespace Graphics = KAGE::Graphics;
|
|
ColorMod color = npc.sprite.color;
|
|
Entity_NPC& tip = link_tip_get(npc);
|
|
int count = 0;
|
|
for (Entity* link = npc.child.get(); is_scourge_link(link); link = link->child.get()) count++;
|
|
|
|
const AnimationFrame* frame = npc.sprite.animState.GetLayerFrame(0);
|
|
if (!frame) return;
|
|
|
|
float halfWidth = frame->width * HALF * npc.sprite.scale.x * frame->scale.x;
|
|
float height = npc.subtype == SCOURGE_CHAIN_TRAP && tip.state == SCOURGE_LINK_LATCHED
|
|
? (frame->height - SCOURGE_STRIP_LATCHED_INSET) * npc.sprite.scale.y
|
|
: npc.sprite.scale.y * frame->height;
|
|
float step = height / (float)count;
|
|
|
|
std::vector<Graphics::ColorQuad> quads;
|
|
std::vector<SourceQuad> sources;
|
|
float left = npc.sprite.isFlipX ? frame->crop.x + frame->width : frame->crop.x;
|
|
float right = npc.sprite.isFlipX ? frame->crop.x : frame->crop.x + frame->width;
|
|
float top = frame->crop.y;
|
|
Vector from = vector_add(npc.pos, npc.posOffset);
|
|
for (Entity* link = npc.child.get(); is_scourge_link(link); link = link->child.get())
|
|
{
|
|
Vector to = vector_add(link->pos, link->posOffset);
|
|
Vector direction = vector_normalized(vector_sub(to, from));
|
|
Vector edge = vector_make(-direction.y * halfWidth, direction.x * halfWidth);
|
|
Vector start = render_position_get(from, RENDER_SNAPPED);
|
|
Vector end = render_position_get(to, RENDER_SNAPPED);
|
|
DestinationQuad quad;
|
|
quad._topLeft = vector_add(vector_add(start, edge), offset);
|
|
quad._topRight = vector_add(vector_sub(start, edge), offset);
|
|
quad._bottomLeft = vector_add(vector_add(end, edge), offset);
|
|
quad._bottomRight = vector_add(vector_sub(end, edge), offset);
|
|
quads.emplace_back(quad, kcolor_rgba(1, 1, 1, 1));
|
|
|
|
SourceQuad source;
|
|
source._topLeft = vector_make(left, top);
|
|
source._topRight = vector_make(right, top);
|
|
source._bottomLeft = vector_make(left, top + step);
|
|
source._bottomRight = vector_make(right, top + step);
|
|
source._coordinateSpace = Graphics::COORDINATES_PIXEL;
|
|
sources.push_back(source);
|
|
top += step;
|
|
from = to;
|
|
}
|
|
if (quads.empty()) return;
|
|
|
|
Graphics::BlendMode saved = Graphics::manager.blendMode;
|
|
Graphics::manager.blendMode.SetMode(Graphics::BLEND_NORMAL);
|
|
LayerState* layer = npc.sprite.GetLayer(0);
|
|
layer->wrapSMode = 0;
|
|
layer->wrapTMode = 0;
|
|
if (layer->spriteSheet)
|
|
for (size_t index = 0; index < quads.size(); index++)
|
|
{
|
|
apply_sprite_shader_params(color, SPRITE_SHADER_COLOR_APPLY, *layer->spriteSheet);
|
|
layer->spriteSheet->Render({&sources[index], 1}, {&quads[index], 1});
|
|
}
|
|
Graphics::manager.blendMode = saved;
|
|
Graphics::manager.shader_default_set();
|
|
}
|
|
|
|
// 00589de0 A link of the Scourge's chain: the first draws the whole chain, the others draw themselves unflipped only
|
|
// while latched.
|
|
void scourge_chain_render(Entity_NPC& npc, const Vector& offset)
|
|
{
|
|
if (!is_scourge_link(npc.parent.get())) return scourge_chain_strip_render(npc, offset);
|
|
if (npc.state != SCOURGE_LINK_LATCHED) return;
|
|
|
|
bool isFlipX = npc.sprite.isFlipX;
|
|
npc.sprite.isFlipX = false;
|
|
npc.Entity::Render(offset);
|
|
npc.sprite.isFlipX = isFlipX;
|
|
}
|