Files
isaac-port/src/entity_projectile.cpp
T

2679 lines
118 KiB
C++

#include "entity_projectile.h"
#include "boss_ai.h"
#include "boss_repentance.h"
#include "entity_bomb.h"
#include "entity_combat.h"
#include "entity_effect.h"
#include "entity_effect_combat.h"
#include "entity_familiar.h"
#include "entity_knife.h"
#include "entity_laser.h"
#include "entity_pickup.h"
#include "entity_player.h"
#include "entity_tear.h"
#include "game.h"
#include "grid_entity.h"
#include "lua_callbacks.h"
#include "lua_engine.h"
#include "manager.h"
#include "npc_ai.h"
#include "npc_basement.h"
#include "room.h"
#include "room_grid.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
// The shot a fresh projectile is: its height, slight random fall, homing, curving and change defaults.
constexpr float HEIGHT_START = -23;
constexpr float FALL_SPREAD = 0.2f;
constexpr float CURVING_DEFAULT = 0.015f;
constexpr float ACCELERATION_DEFAULT = 1.01f;
constexpr int CHANGE_TIMEOUT_DEFAULT = 30;
constexpr float WEAKNESS_SPEED = 0.8f;
// The heights a shot flies between: it lands above the floor one, and hits low only above the high one.
constexpr float FLOOR_HEIGHT = -5;
constexpr float HIGH_HEIGHT = -50;
constexpr float GRAVITY_PULL = 0.1f;
constexpr float FALL_DRAG = 0.9f;
constexpr float CRAWLSPACE_OFFSET_Y = -15;
constexpr float CRAWLSPACE_MIN_ACCEL = 0.001f;
constexpr float CRAWLSPACE_MARGIN_TOP = -600;
constexpr float CRAWLSPACE_MARGIN = -200;
constexpr float FIRE_CRAWLSPACE_LAYER_Y = 8;
// The flags that let a shot pass the grid, and the bit flag set that changes one at once.
constexpr uint64_t GRID_PASS_FLAGS =
PROJECTILE_GHOST | PROJECTILE_ORBIT_CW | PROJECTILE_ORBIT_CCW | PROJECTILE_NO_WALL_COLLIDE | PROJECTILE_CONTINUUM;
constexpr uint64_t FRIENDLY_FLAGS = PROJECTILE_HIT_ENEMIES | PROJECTILE_CANT_HIT_PLAYER;
constexpr uint64_t CHANGE_BOTH_FLAGS =
PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT | PROJECTILE_CHANGE_VELOCITY_AFTER_TIMEOUT;
constexpr uint64_t BURST3_CHILD_CLEARED = PROJECTILE_CURVE_LEFT | PROJECTILE_CURVE_RIGHT | PROJECTILE_SINE_VELOCITY;
// The flags whose shots leave fire waves or fires where they land.
constexpr uint64_t FIRE_FLAGS = PROJECTILE_FIRE_WAVE | PROJECTILE_FIRE_WAVE_X | PROJECTILE_FIRE_SPAWN;
// The glow a fiery, Heretic's, blue fire or side wave shot carries.
constexpr KColor GLOW_FIRE = kcolor_rgba(1, 0.2f, 0, 1);
constexpr KColor GLOW_HERETIC = kcolor_rgba(0.8f, 0.2f, 1, 1);
constexpr KColor GLOW_BLUE_FIRE = kcolor_rgba(0.1f, 0.8f, 1, 1);
constexpr KColor GLOW_SIDE_WAVE = kcolor_rgba(1, 0, 0, 1);
constexpr float GLOW_SCALE = 0.75f;
constexpr int GLOW_FADE_FRAMES = 10;
// The glowing trails: a particle every other tick, its offset a random 0.3 to 1 of its hue.
constexpr float TRAIL_TICKS = 2;
constexpr float TRAIL_GLOW_MIN = 0.3f;
constexpr float TRAIL_GLOW_RANGE = 0.7f;
constexpr float TRAIL_CYAN_GREEN = 0.7f;
constexpr float TRAIL_PURPLE_RED = 0.4f;
constexpr float TRAIL_PURPLE_GREEN = 0.3f;
constexpr float TRAIL_PURPLE_BLUE = 0.7f;
constexpr float TRAIL_PURPLE_SCALE = 0.66f;
constexpr float TRAIL_DEPTH_OFFSET = -5;
// The fires shots leave: four waves 90 apart, a jet, and fireplaces or flames of a random share of their health
// or size.
constexpr int FIRE_WAVE_COUNT = 4;
constexpr int FIRE_WAVE_SOUND_DELAY = 45;
constexpr float FIRE_WAVE_DIAGONAL = 45;
constexpr int FIRE_LEAVE_ODDS = 10;
constexpr float FIRE_SHARE_RANGE = 0.2f;
constexpr float FIRE_SHARE = 0.4f;
constexpr float BLUE_FIRE_SHARE = 0.6f;
constexpr float BLUE_FLAME_DAMAGE = 6;
constexpr float RED_FLAME_DAMAGE = 12;
constexpr int BLUE_FLAME_TIMEOUT = 150;
constexpr int RED_FLAME_TIMEOUT_MIN = 300;
constexpr int RED_FLAME_TIMEOUT_RANGE = 120;
constexpr int FIREPLACE_RED_MOVING = 10;
constexpr int FIREPLACE_BLUE_MOVING = 12;
constexpr ColorMod BLUE_FIRE_POOF_COLOR = {{1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0.4f, 0.6f}};
// Time: the share a slowed shot keeps, the near stop of a held or paused one, and the Hourglass's fade.
constexpr float SLOW_SCALE = 0.5f;
constexpr float HELD_SCALE = 0.02f;
constexpr float HOURGLASS_FADE_FRAMES = 30;
constexpr ColorMod SLOWED_COLOR = {{1, 1, 1.3f, 1}, {0, 0, 0, 0}, {40 / 255.0f, 40 / 255.0f, 40 / 255.0f}};
constexpr int SLOWED_COLOR_FRAMES = 2;
// The Broken Modem's freeze: its chance by luck, and how long a frozen shot waits before vanishing.
constexpr int MODEM_CHANCE_BASE = 67;
constexpr int MODEM_CHANCE_MIN = 50;
constexpr int MODEM_THAW_SPREAD = 7;
constexpr int MODEM_THAW_MIN = 12;
constexpr int MODEM_SEED_FRAMES = 30;
constexpr int MODEM_SEED_STEP = 100;
constexpr RNG MODEM_RNG = {0, 5, 21, 12};
constexpr float MODEM_CLAMP_MAX = 16;
constexpr float MODEM_SCREEN_SCALE = 0.65f;
constexpr float MODEM_ORIGIN_X = 60;
constexpr float MODEM_ORIGIN_Y = 140;
constexpr float MODEM_SCREEN_WIDTH = 338;
constexpr float MODEM_SCREEN_HEIGHT = 182;
// The animations: the size steps of the tear-like ones and the keys the others are cached by.
constexpr float SIZE_STEP_SCALE = 3;
constexpr int SIZE_STEP_MAX = 13;
constexpr float CORN_BIG_STEP = 6;
constexpr float FCUK_BIG_SCALE = 1.5f;
constexpr float COIN_LAYER_SCALE = 2.75f;
constexpr float FIRE_GROW_FRAMES = 15;
constexpr float FIRE_SIZE_MIN = 0.1f;
constexpr int ANIM_KEY_BONE = 100;
constexpr int ANIM_KEY_FIRE = 200;
constexpr int ANIM_KEY_CORN = 300;
constexpr int ANIM_KEY_COIN = 400;
constexpr int ANIM_KEY_MEAT = 600;
constexpr int ANIM_KEY_FCUK = 700;
constexpr int ANIM_KEY_ROCK = 500;
constexpr int ANIM_KEY_WING = 800;
constexpr int ANIM_KEY_LAVA = 900;
constexpr int ANIM_KEY_HEAD = 1000;
constexpr int ANIM_KEY_PEEP = 1100;
// The rock's spin by size, the heads' strip of faces and tilt, and the Peeper's red trail.
constexpr float ROCK_STEP_SCALE = 2;
constexpr int ROCK_STEP_MAX = 6;
constexpr float PEEP_HALF_TURN = 180;
constexpr float PEEP_QUARTER_TURN = 90;
constexpr uint32_t HEAD_FACES = 4;
constexpr float HEAD_FACE_WIDTH = 48;
constexpr float HEAD_TILT = 10;
constexpr float HEAD_SPIN = 2;
constexpr ColorMod PEEP_TRAIL_COLOR = {{0.9f, 0, 0, 0.8f}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float PEEP_TRAIL_RADIUS = 0.15f;
constexpr float PEEP_TRAIL_ALPHA = 0.5f;
// The splashes: a thrown grid entity's packed subtype, the rock's poof tint and chips, the meat ball's Globin and the
// head's gibs.
constexpr int GRID_TYPE_SHIFT = 16;
constexpr int GRID_VARIANT_MASK = 0xffff;
constexpr ColorMod ROCK_POOF_COLOR = {{140 / 255.0f, 123 / 255.0f, 123 / 255.0f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float ROCK_SOUND_MARGIN = -20;
constexpr float ROCK_CHIPS_SCALE = 4;
constexpr int ROCK_CHIPS_MAX = 16;
constexpr float ROCK_CHIP_SPEED = 2;
constexpr int ROCK_CHIP_SUBTYPE = 1;
constexpr float ROCK_CHIP_TURN = 360;
constexpr int MEAT_BURST_SUBTYPE = 2;
constexpr uint32_t MEAT_GAZING_ODDS = 10;
constexpr float MEAT_GLOBIN_PUSH = 0.1f;
constexpr float MEAT_GLOBIN_HEALTH = 24;
constexpr int MEAT_GLOBIN_SHRINK = -150;
constexpr int HEAD_GIBS = 8;
constexpr float HEAD_GIB_SPREAD = 40;
constexpr uint32_t HEAD_GIB_FLAGS = GIB_FLAG_BLOOD | GIB_FLAG_BONE | GIB_FLAG_GUT | GIB_FLAG_EYE;
constexpr float DOGMA_DEPTH_OFFSET = 300;
constexpr uint64_t DOGMA_ELECTRIC_FLAGS = PROJECTILE_SHIELDED | PROJECTILE_BLUE_FIRE_SPAWN;
// Homing: the reach of SMART and its pull, and SMART_PERFECT's reach and pull.
constexpr float SMART_RANGE = 500;
constexpr float SMART_REACH = 100;
constexpr float SMART_LEAD = 0.5f;
constexpr float SMART_PULL = 1.4f;
constexpr float SMART_DRAG = 0.92f;
constexpr int SMART_RETARGET_FRAMES = 3;
constexpr float PERFECT_RANGE = 5000;
constexpr float PERFECT_PULL = 0.1f;
constexpr float PERFECT_SPEED_MIN = 0.001f;
constexpr ColorMod SMART_COLOR = {{1, 1, 1, 1}, {0.8f, 0.15f, 1, 1}, {0.26f, 0.05f, 0.4f}};
constexpr ColorMod GHOST_COLOR = {{0.2f, 0.2f, 0.2f, 0.5f}, {0, 0, 0, 0}, {175 / 255.0f, 175 / 255.0f, 175 / 255.0f}};
// Flight: the orbits' pull, the wiggles' period and size, the zigzags' turns and the speed caps.
constexpr float ORBIT_PULL = 0.5f;
constexpr float WAVE_FRAMES = 30;
constexpr float WIGGLE_SIZE = 3;
constexpr float MEGA_WIGGLE_GROWTH = 0.04f;
constexpr float MEGA_WIGGLE_GROWN = 4;
constexpr float MEGA_WIGGLE_BASE = 3;
constexpr float ROTGUT_WIGGLE_MIN = 2;
constexpr float ROTGUT_WIGGLE_MAX = 5;
constexpr float ROTGUT_WIGGLE_PERIOD = 100;
constexpr float ROTGUT_WIGGLE_PUSH = 0.0075f;
constexpr int ROTGUT_RNG_SHIFT = 8;
constexpr float SAWTOOTH_FRAMES = 30;
constexpr float TRIANGLE_FRAMES = 15;
constexpr float ZIGZAG_START_TURN = 45;
constexpr float SAWTOOTH_TURN = 135;
constexpr float TRIANGLE_TURN = 90;
constexpr float TURN_ROOM_MARGIN = 120;
constexpr float TURN_ALIGN_MAX = 0.05f;
constexpr float TURN_ALIGN_MIN = -0.4f;
constexpr float TURN_SPEED = 2;
constexpr float TARGET_ARRIVED = 10;
constexpr float TARGET_HOVER_FRAMES = 150;
constexpr float TARGET_HOVER_HEIGHT = -15;
constexpr float ACCELERATE_EX_FRAMES = 20;
constexpr float HUSH_SPEED_MAX = 9;
constexpr float SHADY_SPEED_MAX = 10;
// The splits: BURST3's wait and chance, the speed below which a child speeds up, and the children's fall.
constexpr float BURST3_WAIT = 20;
constexpr int BURST3_CHANCE = 30;
constexpr float SPLIT_SLOW_SPEED_SQUARED = 25;
constexpr float SPLIT_SPEED_BOOST = 1.6f;
constexpr float SPLIT_HEIGHT_SHARE = 0.6f;
constexpr float SPLIT_SCALE_LOSS = 0.667f;
constexpr float SPLIT_FALL_ACCEL = 0.1f;
constexpr float SPLIT_FALL_SPEED = -3;
constexpr float BURST_HEIGHT = -11.875f;
constexpr float BURST_FALL_SPREAD = 0.2f;
constexpr float BURST_FALL_SPEED = 0.06f;
constexpr float BURST_SCALE_SHARE = 0.6f;
constexpr int BURST_COUNT = 4;
constexpr float BURST8_LIFE = 45;
constexpr float BURST8_BLOODFLY_LIFE = 10;
constexpr int BURST8_COUNT = 8;
constexpr int BURST8_BLOODFLY_COUNT = 6;
constexpr float BURST8_SPEED = 10;
constexpr float BURST8_CHILD_SCALE = 0.66f;
constexpr float BACK_SPLIT_TURN = 24;
constexpr float BACK_SPLIT_SPEED = 10;
constexpr float BACK_SPLIT_ACCEL = -0.04f;
constexpr float SIDE_WAVE_ACCELERATION = 1.12f;
constexpr float SIDE_WAVE_FALL = -0.04f;
constexpr float SIDE_WAVE_SPEED = 0.15f;
constexpr float SIDE_WAVE_TICKS = 8;
constexpr int BURSTSPLIT_COUNT = 16;
constexpr float BURSTSPLIT_SPEED_MIN = -10;
constexpr float BURSTSPLIT_SPEED_MAX = -22;
constexpr float BURSTSPLIT_ANGLE = 70;
constexpr float BURSTSPLIT_ACCEL = 0.75f;
constexpr float BURSTSPLIT_SCALE_MIN = 0.9f;
constexpr ColorMod BURSTSPLIT_SPLASH_COLOR = {{1, 1, 1, 1}, {0.2f, 1.2f, 0, 1}, {0, 0, 0}};
// The splash a dead shot leaves: poof sizes and sounds, the creep its flags leave and the explosion.
constexpr float POOF_SCALE_SHARE = 0.8f;
constexpr float TEAR_POOF_SMALL = 0.4f;
constexpr float TEAR_POOF_LARGE = 0.8f;
constexpr float POOF_SOUND_MARGIN = -80;
constexpr float CORN_PARTICLE_SPEED = 2;
constexpr int CORN_PARTICLES = 4;
constexpr int BONE_PARTICLES = 8;
constexpr float COIN_PARTICLE_SPEED = 3;
constexpr float COIN_PARTICLE_SPEED_MIN = 2;
constexpr int COIN_PARTICLES = 4;
constexpr float COIN_SOUND_PITCH = 2;
constexpr float COIN_SOUND_PITCH_SPREAD = 0.5f;
constexpr float COIN_SOUND_VOLUME = 0.2f;
constexpr float FIRE_POOF_ALPHA = 0.3f;
constexpr float POOF_RAISE = -10;
constexpr ColorMod PUKE_POOF_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {124 / 255.0f, 94 / 255.0f, 75 / 255.0f}};
constexpr ColorMod FCUK_POOF_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float EXPLOSION_DAMAGE = 50;
constexpr float EXPLOSION_RADIUS = 60;
// The Heretic's fireball bursts into six purple fire shots.
constexpr int HERETIC_BURST_SHOTS = 6;
constexpr float HERETIC_BURST_STEP = 60;
constexpr float HERETIC_BURST_SPEED = 8;
constexpr float HERETIC_BURST_FALL = 0.05f;
constexpr ColorMod HERETIC_BURST_COLOR = {{1, 1, 1, 1}, {1.3f, 1, 2, 1}, {0, 0, 0}};
constexpr float ACID_SCALE = 3;
constexpr float ACID_GHOST_SCALE = 1.5f;
constexpr int ACID_GHOST_TIMEOUT = 10;
constexpr float ACID_SPLAT_SCALE = 0.8f;
constexpr float GOO_SCALE = 3;
constexpr float GOO_SMALL_SCALE = 2;
constexpr int GOO_TIMEOUT = 150;
constexpr int RED_CREEP_TIMEOUT = 90;
constexpr int RED_CREEP_FRAMES = 2;
constexpr float BROWN_CREEP_TICKS = 20;
constexpr int BROWN_CREEP_TIMEOUT = 180;
// The Soul pushes shots from its holder, a few at a time.
constexpr int SOUL_CHANCE = 3;
constexpr float SOUL_REACH = 80;
constexpr float SOUL_PUSH = 0.1f;
constexpr float SOUL_PUSH_MIN = 0.05f;
constexpr float SOUL_DISTANCE_START = 1e9f;
constexpr float ANTI_GRAVITY_RATE = 0.2f;
constexpr float ANTI_GRAVITY_BOB = 2;
constexpr float FADEOUT_RATE = 0.1f;
constexpr float BOUNCE_FLOOR_SPEED = 2;
constexpr float BOUNCE_FLOOR_REBOUND = -2;
constexpr float REFLECT_REBOUND = 0.85f;
constexpr int CIRCLE_RNG_SHIFT = 44;
constexpr int CIRCLE_CHANCE = 100;
constexpr int CIRCLE_PROC_MAX = 32;
// Hits: a small player lets shots pass over, hats block falling ones, Infamy blocks some, the frozen flash.
constexpr float PLAYER_HIT_SCALE_MIN = 0.5f;
constexpr float HAT_CRAWLSPACE_SIDE = 0.8f;
constexpr float ENEMY_HIT_BONUS = 5;
constexpr float GRID_HIT_DAMAGE = 35;
constexpr int FREEZE_DURATION = 90;
constexpr int GREED_COINS_MAX = 3;
constexpr float GREED_COIN_SPEED = 5;
constexpr float GREED_COIN_SPEED_MIN = 2;
constexpr float SINGE_PUSH = 0.2f;
constexpr int MOTHER_SELF_HIT_FRAMES = 5;
constexpr int INFAMY_COLOR_FRAMES = 15;
constexpr ColorMod FROZEN_COLOR = {{0.6f, 0.6f, 0.6f, 1}, {0, 0, 0, 0}, {0.4f, 0.4f, 0.4f}};
constexpr int FROZEN_FRAMES = 30;
// Spawn flags: a champion's shot hits harder and bigger, a weak shooter's smaller.
constexpr float CHAMPION_DAMAGE_MAX = 2;
constexpr float CHAMPION_SHOT_SCALE = 1.2173913f;
constexpr float WEAK_SHOT_SCALE = 0.8f;
constexpr int COLOR_PRIORITY = 255;
constexpr int COLOR_PRIORITY_NONE = -1;
// A Continuum shot wraps 100 past the edges, its colorize stepping every other frame through nine, black at the
// sixth.
constexpr float CONTINUUM_WRAP_MARGIN = -100;
constexpr int CONTINUUM_COLORS = 9;
constexpr int CONTINUUM_DARK_STEP = 5;
constexpr std::array<std::array<float, 3>, CONTINUUM_COLORS> CONTINUUM_COLORIZES = {{{1, 0.33f, 1},
{1, 0.66f, 1},
{1, 1, 1},
{0.66f, 0.66f, 0.66f},
{0.33f, 0.33f, 0.33f},
{0, 0, 0},
{0.33f, 0, 0.33f},
{0.66f, 0, 0.66f},
{1, 0, 1}}};
constexpr int ENTITY_SHADY = 888;
constexpr int MOTHER_BALL = 10;
constexpr int BEAST_FIRE_BALL = 20;
constexpr int EFFECT_BONE_PARTICLE = 35;
constexpr int EFFECT_CORN_POOF = 43;
constexpr int EFFECT_CORN_PARTICLE = 58;
constexpr int EFFECT_COIN_POOF = 97;
constexpr int EFFECT_GODHEAD_AURA = 123;
constexpr int EFFECT_DOGMA_POOF = 200;
constexpr int GODHEAD_AURA_SUBTYPE = 4;
constexpr int ACID_SPLAT_SUBTYPE = 0;
// Rotgut's heart's shots: two out of its fire's burst, thirteen out of its spit, each at 6 to 10 up.
constexpr float ROTGUT_SHOT_SPEED_MIN = -6;
constexpr float ROTGUT_SHOT_SPEED_MAX = -10;
constexpr float ROTGUT_BURST_TURN = 30;
constexpr float ROTGUT_BURST_RISE = -6;
constexpr int ROTGUT_BURST_TIMEOUT = 6;
constexpr float ROTGUT_BURST_FALL = 0.2f;
constexpr int ROTGUT_SPLIT_SHOTS = 13;
constexpr float ROTGUT_SPLIT_TURN = 75;
constexpr int ROTGUT_SPLIT_TIMEOUT = 45;
constexpr float ROTGUT_SPLIT_FALL = -0.1f;
constexpr float ROTGUT_SPLIT_SCALE = 0.8f;
constexpr float ROTGUT_SPLIT_SCALE_RANGE = 0.6f;
constexpr int FIRE_POOF_SUBTYPE = 1;
constexpr int BONE_PARTICLE_SUBTYPE = 1;
constexpr int SOUND_BLOOD_SPLASH = 30;
constexpr int SOUND_COIN_IMPACT = 138;
constexpr int SOUND_INFAMY_BLOCK = 863;
constexpr int SOUND_PIN_GOO = 934;
constexpr int SOUND_BEAST_FIRE = 700;
constexpr int COLLECTIBLE_SOUL = 335;
constexpr int COLLECTIBLE_BETRAYAL = 391;
constexpr uint32_t CHALLENGE_BROKEN_WATCH = 22;
// The familiars shots hurt (the two Infamy guards as the player does), and the subtype of the one that is spared.
constexpr std::array<int, 6> HURT_FAMILIARS = {62, 67, 100, 206, 211, 237};
constexpr std::array<int, 2> INFAMY_FAMILIARS = {62, 67};
constexpr int SPARED_FAMILIAR = 206;
constexpr int SPARED_FAMILIAR_SUBTYPE = 702;
// The variants whose splash is a poof sized by the shot.
constexpr std::array<int, 8> POOF_VARIANTS = {PROJECTILE_NORMAL, PROJECTILE_PUKE, PROJECTILE_TEAR, PROJECTILE_CORN,
PROJECTILE_HUSH, PROJECTILE_FCUK, PROJECTILE_WING, PROJECTILE_PEEP};
template <size_t N>
static bool is_listed(const std::array<int, N>& list, int value)
{
return std::find(list.begin(), list.end(), value) != list.end();
}
static bool is_crawlspace()
{
return g_game->room->roomType == ROOM_DUNGEON;
}
static float lerp(float from, float to, float t)
{
return from + (to - from) * t;
}
// The phase of a wave of WAVE_FRAMES frames at a time, in radians.
static float wave_phase(float time)
{
return time * PI_F * 2 / WAVE_FRAMES;
}
// The wiggles' sideways direction, a unit vector across the velocity (or the velocity's own when it has none).
static Vector wiggle_side(Vector velocity)
{
return vector_normalized(vector_make(velocity.y, -velocity.x));
}
// Plays a sound through the fart seeds, at a pitch and volume, unless the sound is muted by the frame delay.
static void sound_play(int sound, float pitch, float volume)
{
seed_fart_sound_override(&sound);
g_manager->sfxManager.Play(sound, {.volume = volume, .pitch = pitch});
}
// Makes a sprite layer add its light to what is under it (BlendMode::SetMode's additive mode).
static void layer_additive_set(ANM2& sprite, int layerId)
{
LayerState* layer = sprite.GetLayer(layerId);
bool isPremultiplied = KAGE::Graphics::manager.isPremultipliedAlpha;
layer->blendMode[BLEND_EQUATION] = BLEND_EQUATION_ADD;
layer->blendMode[BLEND_RGB_SOURCE] = isPremultiplied ? BLEND_FACTOR_ONE : BLEND_FACTOR_SRC_ALPHA;
layer->blendMode[BLEND_RGB_DEST] = BLEND_FACTOR_ONE;
layer->blendMode[BLEND_ALPHA_SOURCE] = BLEND_FACTOR_ONE;
layer->blendMode[BLEND_ALPHA_DEST] = BLEND_FACTOR_ONE;
}
// Makes a sprite layer cover what is under it by its alpha (BlendMode::SetMode's normal mode).
static void layer_normal_set(ANM2& sprite, int layerId)
{
LayerState* layer = sprite.GetLayer(layerId);
bool isPremultiplied = KAGE::Graphics::manager.isPremultipliedAlpha;
layer->blendMode[BLEND_EQUATION] = BLEND_EQUATION_ADD;
layer->blendMode[BLEND_RGB_SOURCE] = isPremultiplied ? BLEND_FACTOR_ONE : BLEND_FACTOR_SRC_ALPHA;
layer->blendMode[BLEND_RGB_DEST] = BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
layer->blendMode[BLEND_ALPHA_SOURCE] = BLEND_FACTOR_ONE;
layer->blendMode[BLEND_ALPHA_DEST] = BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
}
// Spawns an effect at a position. Game::Spawn returning nothing crashes the original. Callers skip what it sets.
static Entity_Effect* effect_spawn(int variant, const Vector& position, Entity* spawner, int subType = 0)
{
uint32_t seed = Random();
Entity* effect = g_game->Spawn({.position = position, .spawner = spawner, .subType = subType,
.type = ENTITY_EFFECT, .variant = variant, .seed = seed});
return static_cast<Entity_Effect*>(effect);
}
// Spawns a moving effect at a position.
static Entity_Effect* effect_spawn_moving(int variant, const Vector& position, const Vector& velocity, Entity* spawner)
{
uint32_t seed = Random();
Entity* effect = g_game->Spawn({.position = position, .velocity = velocity, .spawner = spawner,
.type = ENTITY_EFFECT, .variant = variant, .seed = seed});
return static_cast<Entity_Effect*>(effect);
}
// Spawns a shot from a shot, taking on its spawner type and variant as the splits do.
static Entity_Projectile* child_spawn(Entity_Projectile& source, const EntitySpawn& spawn)
{
Entity* spawned = g_game->Spawn(spawn);
if (!spawned) return nullptr;
auto* child = static_cast<Entity_Projectile*>(spawned);
child->spawnerType = source.spawnerType;
child->spawnerVariant = source.spawnerVariant;
return child;
}
// Whether the shot is the players' (it hits enemies and not players).
static bool is_friendly_shot(uint64_t flags)
{
return (flags & FRIENDLY_FLAGS) == FRIENDLY_FLAGS;
}
static bool is_any_player_holding(int collectible)
{
return g_game->playerManager.AnyoneHasCollectible(collectible);
}
// 00652b90 Starts a shot 23 high with a slight random fall, no flags, its config's sprite and the default homing,
// curving and change. Fire glows, and a weak shooter's shots are weak and slow.
void Entity_Projectile::Init(int entityType, int entityVariant, int entitySubType, uint32_t seed)
{
Entity::Init(entityType, entityVariant, entitySubType, seed);
entityGridCollisionClass = GRIDCOLL_BULLET;
if (config) sprite.Load(config->anm2Path, GRAPHICS_LOAD);
height = HEIGHT_START;
fallingSpeed = (random_unit() - HALF) * FALL_SPREAD;
fallingAccel = 0;
scale = 1;
projectileFlags = 0;
homingStrength = 1;
curvingStrength = CURVING_DEFAULT;
acceleration = ACCELERATION_DEFAULT;
orbitDistance = 0;
animKey = -1;
collisionDamage = 1;
isRenderedAtGround = false;
changeFlags = 0;
changeVelocity = 0;
changeTimeout = CHANGE_TIMEOUT_DEFAULT;
maxHitPoints = 0;
modemPosition = {};
motionHook = nullptr;
if (variant == PROJECTILE_FIRE)
{
layer_additive_set(sprite, 0);
if (is_crawlspace()) sprite.GetLayer(0)->pos = vector_make(0, FIRE_CRAWLSPACE_LAYER_Y);
}
if (spawnerEntity && spawnerEntity->HasEntityFlags(FLAG_WEAKNESS))
{
AddEntityFlags(FLAG_WEAKNESS);
velocity = vector_scale(velocity, WEAKNESS_SPEED);
}
orbitParentVelocity = {};
lua_engine_get().PostProjectileInit(this);
}
// 00652df0 Its time scale: the room's, halved for a hostile shot by the Stop Watch, the Broken Watch's slow rooms
// and the Hourglass fading out, and for a friendly one by the room's slowdown. Slowed shots go at half speed, held
// or paused ones nearly stop.
float Entity_Projectile::time_scale_get()
{
Room& room = *g_game->room;
float timeScale = room.GetTimeScale(this);
if (is_friendly_shot(projectileFlags))
{
if (room.HasSlowDown()) timeScale *= SLOW_SCALE;
}
else if (!parent || !parent->IsFriendly())
{
TemporaryEffects& effects = room.temporaryEffects;
bool isBrokenWatchSlow =
g_game->challenge != CHALLENGE_BROKEN_WATCH && room.brokenWatchState == BROKEN_WATCH_SLOW;
if (effects.HasCollectibleEffect(COLLECTIBLE_STOP_WATCH) || isBrokenWatchSlow) timeScale *= SLOW_SCALE;
else if (TemporaryEffect* hourglass = effects.GetCollectibleEffect(COLLECTIBLE_HOURGLASS))
timeScale *= 1 - std::min(std::max((float)hourglass->cooldown / HOURGLASS_FADE_FRAMES, 0.0f), 1.0f) *
SLOW_SCALE;
}
if (projectileFlags & PROJECTILE_SLOWED) timeScale *= SLOW_SCALE;
if (pauseTimer > 0 || holdCountdown > 0) timeScale *= HELD_SCALE;
return timeScale;
}
// 00653070 Flies the shot for a frame: its animation, the base update, its flags' steering and wiggles, its fall,
// A fly ball (a broccoli shot): a shot whose subtype counts the flies (less 100 past 100, which then carry the count)
// that orbit it from its first frame, itself unseen. It homes in on the nearest player (not Mother's), goes once its
// flies are gone, and bursts them out backwards when it dies.
constexpr int FLY_BALL_LIVE = 1000;
constexpr int FLY_BALL_NESTED = 100;
constexpr uint64_t FLY_FLAGS = PROJECTILE_BROCCOLI | PROJECTILE_NO_WALL_COLLIDE;
constexpr float FLY_BRIGHT = 4;
constexpr float FLY_RADIUS_X = 20;
constexpr float FLY_RADIUS_Y = 8;
constexpr float FLY_RADIUS_LEAST = 0.8f;
constexpr float FLY_RADIUS_RANGE = 0.2f;
constexpr float FLY_SCALE_LEAST = 1.4f;
constexpr float FLY_SCALE_RANGE = 0.6f;
constexpr int FLY_ORBIT_FRAMES = 30;
constexpr float FLY_ORBIT_HEIGHT = -23;
constexpr float FLY_BURST_SPREAD = 80;
constexpr float FLY_BURST_REACH = 6;
constexpr float FLY_BURST_SPEED = 9;
constexpr float FLY_BURST_SPEED_RANGE = 3;
constexpr float FLY_BURST_FALL = -0.094f;
constexpr int FLY_BURST_WIGGLE = 30;
constexpr float FLY_BALL_MARGIN = 3;
constexpr ColorMod FLY_POP_COLOR = {{1, 1, 1, 1}, {4, 5, 1, 1}, {0, 0, 0}};
constexpr ColorMod FLY_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {81 / 255.0f, 112 / 255.0f, 29 / 255.0f}};
constexpr float FLY_POP_SCALE = 1.5f;
constexpr float FLY_POP_RISE = -20;
constexpr float FLY_POP_DEPTH = 4;
constexpr int FLY_MAGGOTS = 6;
constexpr float FLY_MAGGOT_REACH = 80;
constexpr float FLY_MAGGOT_FREE_REACH = 64;
constexpr float FLY_MAGGOT_JITTER = 38;
constexpr float FLY_MAGGOT_HEIGHT = -30;
constexpr float FLY_MAGGOT_HEIGHT_RANGE = 15;
constexpr float FLY_MAGGOT_STEP = 15;
constexpr float FLY_SPLAT_SIZE = 1.5f;
constexpr int FLY_BURST_SHAKE = 5;
// The flies still orbiting a fly ball.
static std::vector<Entity_Projectile*> fly_ball_flies(Entity_Projectile& ball)
{
std::vector<Entity_Projectile*> flies;
for (Entity* fly : g_game->room->entityList.updateEL)
if (fly->type == ENTITY_PROJECTILE && fly->target == &ball)
flies.push_back(static_cast<Entity_Projectile*>(fly));
return flies;
}
// Its first frame: the flies spawn about it, each bright one time in two, on an orbit of its own, and it hides.
static void fly_ball_init(Entity_Projectile& ball)
{
if (!(ball.projectileFlags & PROJECTILE_BROCCOLI) || ball.subtype < 1 || ball.subtype >= FLY_BALL_LIVE) return;
int count = ball.subtype > FLY_BALL_NESTED ? ball.subtype - FLY_BALL_NESTED : ball.subtype;
for (int i = 0; i < count; i++)
{
auto* fly = static_cast<Entity_Projectile*>(g_game->Spawn(
{.position = ball.pos, .spawner = ball.spawnerEntity, .type = ENTITY_PROJECTILE, .seed = Random()}));
fly->target = &ball;
fly->parent = ball.parent.get();
ColorMod color = ball.sprite.color;
float bright = random_below(2) == 0 ? FLY_BRIGHT : 1;
color._tint[0] = color._tint[1] = color._tint[2] = bright;
fly->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true);
fly->projectileFlags = FLY_FLAGS;
fly->homingStrength = random_unit() * FULL_TURN_DEGREES;
float radius = random_unit() * FLY_RADIUS_RANGE + FLY_RADIUS_LEAST;
fly->targetPosition = vector_scale(vector_make(FLY_RADIUS_X * ball.scale, FLY_RADIUS_Y * ball.scale), radius);
fly->scale = random_unit() * FLY_SCALE_RANGE + FLY_SCALE_LEAST;
if (ball.subtype > FLY_BALL_NESTED) fly->subtype = count;
}
ball.sprite.color._tint[3] = 0;
ball.subtype = FLY_BALL_LIVE;
}
// A fly circles its ball on an ellipse turned by its own angle (its homing strength), once every 30 frames.
static void fly_orbit_update(Entity_Projectile& fly)
{
Entity* ball = fly.target;
if (!(fly.projectileFlags & PROJECTILE_BROCCOLI) || !ball || !ball->IsProjectile()) return;
Vector across = vector_scale(vector_from_angle(fly.homingStrength), fly.targetPosition.x);
Vector along = vector_scale(vector_from_angle(fly.homingStrength + RIGHT_ANGLE_DEGREES), fly.targetPosition.y);
float turn =
(float)((uint32_t)(g_game->frameCount + (int)fly.initSeed) % FLY_ORBIT_FRAMES) * TAU / FLY_ORBIT_FRAMES;
Vector spot =
vector_add(vector_add(ball->pos, vector_scale(across, std::cos(turn))), vector_scale(along, std::sin(turn)));
fly.velocity = vector_sub(spot, fly.pos);
fly.friction = 1;
fly.fallingSpeed = 0;
fly.height = FLY_ORBIT_HEIGHT;
}
// The ball dies: its flies burst out backwards across 160 degrees (any way when it was still), with maggots and green
// creep but for Mother's, and a splat.
static void fly_ball_burst(Entity_Projectile& ball)
{
ball.subtype = 0;
npc_sound_play(SOUND_DEATH_BURST_SMALL, 1, 2, 1);
Entity* pop = g_game->Spawn({.position = ball.pos,
.subType = 3,
.type = ENTITY_EFFECT,
.variant = EFFECT_BLOOD_EXPLOSION,
.seed = Random()});
pop->sprite.scale = vector_make(FLY_POP_SCALE, FLY_POP_SCALE);
pop->posOffset = vector_make(0, FLY_POP_RISE);
pop->renderDepthOffset = FLY_POP_DEPTH;
pop->SetColor(FLY_POP_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
std::vector<Entity_Projectile*> flies = fly_ball_flies(ball);
Vector back = vector_scale(vector_normalized(ball.velocity), -1);
float first = -FLY_BURST_SPREAD;
float last = FLY_BURST_SPREAD;
if (vector_is_zero(back))
{
back = random_direction();
first = -HALF_TURN_DEGREES;
last = HALF_TURN_DEGREES;
}
float step = 0;
if (flies.size() < 2) first = 0;
else step = (last - first) / (float)(flies.size() - 1);
for (size_t i = 0; i < flies.size(); i++)
{
Entity_Projectile& fly = *flies[i];
Vector direction = vector_rotated(back, (float)i * step + first);
fly.pos = vector_add(ball.pos, vector_scale(direction, FLY_BURST_REACH));
fly.velocity = vector_scale(direction, random_unit() * FLY_BURST_SPEED_RANGE + FLY_BURST_SPEED);
fly.projectileFlags = PROJECTILE_HIT_ENEMIES;
if (fly.subtype == FLY_BALL_LIVE) fly.projectileFlags |= PROJECTILE_BROCCOLI;
fly.entityGridCollisionClass = GRIDCOLL_NOPITS;
fly.fallingAccel = FLY_BURST_FALL;
fly.target = nullptr;
fly.wiggleFrameOffset = random_below(FLY_BURST_WIGGLE);
}
ball.pos = g_game->room->GetClampedPositionImpl(ball.pos, FLY_BALL_MARGIN, FLY_BALL_MARGIN, FLY_BALL_MARGIN,
FLY_BALL_MARGIN);
if (ball.spawnerType != ENTITY_MOTHER)
{
for (int i = 0; i < FLY_MAGGOTS; i++)
{
Vector spot = random_direction();
spot = vector_add(ball.pos, vector_scale(spot, random_unit() * FLY_MAGGOT_REACH));
if (room_grid_collision_at(spot) > 0)
{
float jitterY = random_unit() * FLY_MAGGOT_JITTER - FLY_MAGGOT_JITTER * HALF;
float jitterX = random_unit() * FLY_MAGGOT_JITTER - FLY_MAGGOT_JITTER * HALF;
spot = vector_add(room_free_tile_position_find(spot, FLY_MAGGOT_FREE_REACH),
vector_make(jitterX, jitterY));
}
Vector away = vector_normalized(vector_sub(spot, ball.pos));
float height = random_unit() * FLY_MAGGOT_HEIGHT_RANGE + FLY_MAGGOT_HEIGHT;
float reach = random_unit() * FLY_MAGGOT_STEP;
small_maggot_jump_spawn(vector_add(ball.pos, vector_scale(away, reach)), height, ball.pos);
}
Entity* creep = g_game->Spawn({.position = ball.pos,
.spawner = &ball,
.type = ENTITY_EFFECT,
.variant = EFFECT_CREEP_GREEN,
.seed = Random()});
creep->sprite.scale = vector_make(FLY_POP_SCALE, FLY_POP_SCALE);
}
float size = (random_unit() * HALF + HALF) * FLY_SPLAT_SIZE;
Entity* splat = spawn_at(ENTITY_EFFECT, EFFECT_BLOOD_SPLAT, 0, ball.pos, &ball);
splat->sprite.scale = vector_make(size, size);
splat->Update();
splat->SetColor(FLY_SPLAT_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
splat->Update();
game_shake_screen(FLY_BURST_SHAKE);
}
// A live fly ball homes in on the nearest player (Mother's flies straight), goes when its flies are all gone, and
// bursts when it dies.
static void fly_ball_update(Entity_Projectile& ball)
{
if (!(ball.projectileFlags & PROJECTILE_BROCCOLI) || ball.subtype != FLY_BALL_LIVE) return;
if (ball.isDead) return fly_ball_burst(ball);
if (ball.spawnerType != ENTITY_MOTHER)
{
if (!ball.target)
if (Entity_Player* player = game_nearest_player(ball.pos)) ball.target = player;
if (Entity* target = ball.target)
{
Vector aim = vector_normalized(vector_sub(target->pos, ball.pos));
if (vector_dot(ball.velocity, aim) > 0)
{
float speed = vector_length(ball.velocity);
ball.velocity =
vector_resized(vector_add(ball.velocity, vector_scale(aim, ball.speedMultiplier)), speed);
}
}
}
if (ball.GetIntervalTicks(4, 0) && fly_ball_flies(ball).empty()) ball.Remove();
}
// 00653070 A shot's frame: its look, trails, homing, steering, speed and fall, what its flags do on the way and its
// splash once it has died. A shot the Broken Modem froze waits, then vanishes, and one held in stasis floats by its
// parent.
void Entity_Projectile::Update()
{
speedMultiplier = time_scale_get();
if (pauseTimer > 0) pauseTimer--;
if (projectileFlags & GRID_PASS_FLAGS) isCollidesWithGrid = false;
if (modem_freeze_update()) return;
animation_update();
if (is_crawlspace() && FrameCount() == 1)
{
if (fallingAccel > 0 && std::fabs(velocity.x) > velocity.y) velocity.y = fallingSpeed * HALF + velocity.y;
if (!g_game->room->IsBeastDungeon() && (projectileFlags & PROJECTILE_EXPLODE))
projectileFlags &= ~(PROJECTILE_GHOST | PROJECTILE_NO_WALL_COLLIDE);
}
Entity::Update();
if (FrameCount() == 1)
{
first_frame_update();
fly_ball_init(*this);
}
if (projectileFlags & PROJECTILE_BURST8)
{
bool isBloodfly = subtype == 0 && spawnerType == ENTITY_SUCKER && spawnerVariant == SUCKER_BLOODFLY;
if (updateTime > (isBloodfly ? BURST8_BLOODFLY_LIFE : BURST8_LIFE)) isDead = true;
}
fly_orbit_update(*this);
trails_update();
if (isDead) return death_update();
if ((projectileFlags & PROJECTILE_STASIS) && stasis_update()) return lua_engine_get().PostProjectileUpdate(this);
if (motionHook) motionHook(*this, PROJECTILE_STEP_UPDATE);
homing_update();
steering_update();
if (wiggles_update()) return;
speed_update();
fall_update();
late_update();
lua_engine_get().PostProjectileUpdate(this);
}
// Stasis: a shot held by its parent drifts back toward 50 from it, carried along with it, settling to its usual
// height and flashing every 8 frames, until its timed change. It then flies off the way it last faced from the parent
// at its changed speed. Whether it was held, which ends its frame.
bool Entity_Projectile::stasis_update()
{
constexpr float HOLD_DISTANCE = 50;
constexpr float HOLD_PULL = 0.1f;
constexpr float HOLD_EASE = 0.3f;
constexpr float HEIGHT_EASE = 0.4f;
constexpr int STASIS_FLASH_FRAMES = 8;
constexpr int STASIS_FLASH_DURATION = 4;
constexpr float RELEASE_CURVE = 0.015f;
constexpr ColorMod STASIS_FLASH_COLOR = {{1, 1, 1, 1}, {0, 0, 0, 0}, {0.35f, 0.45f, 0.5f}};
bool isChangeDue = changeTimeout < 1 || GetIntervalTicks((float)changeTimeout, 0) != 0;
bool isReleased = (projectileFlags & PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT) && FrameCount() > 0 && isChangeDue;
if (!parent || isReleased)
{
velocity = vector_scale(vector_from_angle(curvingStrength), changeVelocity);
curvingStrength = RELEASE_CURVE;
return false;
}
Vector away = vector_sub(vector_add(pos, velocity), parent->pos);
float distance = vector_length(away);
if (distance != 0)
{
Vector direction = vector_scale(away, 1 / distance);
curvingStrength = vector_angle(direction);
velocity = vector_sub(velocity, targetPosition);
vector_lerp(velocity, vector_scale(direction, (HOLD_DISTANCE - distance) * HOLD_PULL), HOLD_EASE);
velocity = vector_add(velocity, parent->velocity);
targetPosition = parent->velocity;
}
height = lerp(height, HEIGHT_START, HEIGHT_EASE);
update_height_collision();
if (IsFrame(STASIS_FLASH_FRAMES, 0))
{
ColorMod flash = STASIS_FLASH_COLOR;
color_multiply(flash, color);
SetColor(flash, STASIS_FLASH_DURATION, COLOR_PRIORITY, true, true);
}
return true;
}
// The Broken Modem's freeze: a frozen shot vanishes after a while, and waits in place until then. Whether it did.
bool Entity_Projectile::modem_freeze_update()
{
if (!HasEntityFlags(FLAG_FREEZE)) return false;
if (freezeCountdown <= 0 && IsFrame((int)(Random() % MODEM_THAW_SPREAD) + MODEM_THAW_MIN, 0))
{
ClearEntityFlags(FLAG_FREEZE);
modemPosition = {};
Remove();
return true;
}
if (isDead) return false;
velocity = {};
Entity::Update();
return true;
}
// The animation of its variant, played once per change of size or variant: the tear-like ones by size, the bones,
// fire, corn and coins at a random frame, and its shadow by its scale.
void Entity_Projectile::animation_update()
{
char name[32];
int sizeStep = std::min((int)(scale * SIZE_STEP_SCALE + SIZE_STEP_SCALE), SIZE_STEP_MAX);
auto frame_randomize = [this]()
{
int length = sprite.animState.GetAnimationLength();
if (length > 0) sprite.SetFrame((float)(dropRNG._seed % (uint32_t)length));
};
switch (variant)
{
case PROJECTILE_NORMAL:
case PROJECTILE_PUKE:
case PROJECTILE_TEAR:
case PROJECTILE_HUSH:
if (animKey == sizeStep) break;
if (spawnerType == ENTITY_DOGMA)
{
bool isElectric = projectileFlags & DOGMA_ELECTRIC_FLAGS;
std::snprintf(name, sizeof(name), isElectric ? "DogmaElectricTear%d" : "DogmaTear%d", sizeStep);
renderDepthOffset = DOGMA_DEPTH_OFFSET;
sprite.bitflags |= ANIM_RENDER_STATIC;
}
else std::snprintf(name, sizeof(name), "RegularTear%d", sizeStep);
sprite.Play(name);
animKey = sizeStep;
break;
case PROJECTILE_BONE:
if (animKey == ANIM_KEY_BONE) break;
sprite.Play("Move");
if (sprite.animState.animData) frame_randomize();
animKey = ANIM_KEY_BONE;
break;
case PROJECTILE_FIRE:
if (animKey != ANIM_KEY_FIRE)
{
sprite.Play("Flickering");
frame_randomize();
layer_additive_set(sprite, 0);
animKey = ANIM_KEY_FIRE;
isUnknown176 = false;
}
sprite.GetLayer(0)->size =
vector_scale(vector_make(1, 1), std::clamp(updateTime / FIRE_GROW_FRAMES, FIRE_SIZE_MIN, 1.0f));
break;
case PROJECTILE_CORN:
{
if (animKey == ANIM_KEY_CORN) break;
bool isBig = scale * SIZE_STEP_SCALE + SIZE_STEP_SCALE > CORN_BIG_STEP;
std::snprintf(name, sizeof(name), isBig ? "Big%02d" : "Small%02d", random_below(4) + 1);
sprite.Play(name);
animKey = ANIM_KEY_CORN;
break;
}
case PROJECTILE_COIN:
if (animKey != ANIM_KEY_COIN)
{
sprite.Play("MoveHori");
frame_randomize();
animKey = ANIM_KEY_COIN;
}
sprite.rotation = vector_angle(velocity);
sprite.GetLayer(0)->size = vector_scale(vector_make(1, 1), scale / COIN_LAYER_SCALE);
break;
case PROJECTILE_FCUK:
if (animKey == ANIM_KEY_FCUK) break;
sprite.Play(scale < FCUK_BIG_SCALE ? "Small" : "Big");
animKey = ANIM_KEY_FCUK;
break;
case PROJECTILE_ROCK:
if (animKey == ANIM_KEY_ROCK) break;
std::snprintf(name, sizeof(name), "Rotate%d",
std::min((int)(scale * ROCK_STEP_SCALE + 1), ROCK_STEP_MAX));
sprite.Play(name);
animKey = ANIM_KEY_ROCK;
break;
case PROJECTILE_WING:
case PROJECTILE_LAVA:
{
int key = variant == PROJECTILE_WING ? ANIM_KEY_WING : ANIM_KEY_LAVA;
if (animKey != key)
{
sprite.Play("Move");
sprite.isFlipY = dropRNG._seed & 1;
frame_randomize();
animKey = key;
if (variant == PROJECTILE_WING) sprite.bitflags |= ANIM_RENDER_STATIC;
sprite.scale = vector_make(scale, scale);
}
sprite.rotation = (vector_angle(velocity) + PEEP_HALF_TURN) * (sprite.isFlipY ? -1 : 1);
break;
}
case PROJECTILE_HEAD:
if (animKey == ANIM_KEY_HEAD) break;
sprite.Play("Idle");
sprite.isFlipX = dropRNG._seed & 1;
sprite.GetLayer(0)->cropOffset.x = (float)dropRNG.RandomInt(HEAD_FACES) * HEAD_FACE_WIDTH;
sprite.rotation = random_unit() * HEAD_TILT * 2 - HEAD_TILT;
animKey = ANIM_KEY_HEAD;
sprite.scale = vector_make(scale, scale);
break;
case PROJECTILE_PEEP:
{
int key = sizeStep + ANIM_KEY_PEEP;
if (animKey != key)
{
std::snprintf(name, sizeof(name), "RegularTear%d", sizeStep);
sprite.Play(name);
animKey = key;
}
peep_update();
break;
}
}
// A shot without a config crashes the original here. This leaves its shadow.
if (config) shadowSize = scale * config->shadowSize;
if (variant == PROJECTILE_GRID) shadowSize += shadowSize;
else if (variant == PROJECTILE_MEAT && animKey != ANIM_KEY_MEAT)
{
sprite.Play("Move");
frame_randomize();
animKey = ANIM_KEY_MEAT;
}
}
// Its first frame after the base update: a fiery shot glows orange, the Heretic's exploding one purple, a blue fire
// spawning one blue and a side wave one red, and a Godhead shot without a glow takes its aura.
void Entity_Projectile::first_frame_update()
{
KColor glow{};
if (projectileFlags & FIRE_FLAGS) glow = GLOW_FIRE;
else if ((projectileFlags & PROJECTILE_EXPLODE) && spawnerType == ENTITY_HERETIC) glow = GLOW_HERETIC;
else if (projectileFlags & PROJECTILE_BLUE_FIRE_SPAWN) glow = GLOW_BLUE_FIRE;
else if (projectileFlags & PROJECTILE_SIDE_WAVE) glow = GLOW_SIDE_WAVE;
if (glow._alpha > 0)
{
effect_light_create(
{.color = glow, .position = pos, .parent = this, .scale = GLOW_SCALE, .fadeFrames = GLOW_FADE_FRAMES});
return;
}
if (!(projectileFlags & PROJECTILE_GODHEAD)) return;
uint32_t seed = Random();
Entity* aura = g_game->Spawn({.position = pos, .velocity = velocity, .spawner = spawnerEntity,
.subType = GODHEAD_AURA_SUBTYPE, .type = ENTITY_EFFECT,
.variant = EFFECT_GODHEAD_AURA, .seed = seed});
if (!aura) return;
aura->sprite.scale = vector_make(scale, scale);
aura->parent = this;
aura->Update();
}
// The Peeper's eye: it faces along its flight, upright either way, and drags a red trail at half its alpha.
void Entity_Projectile::peep_update()
{
float angle = vector_angle(velocity);
bool isBackward = angle < -PEEP_QUARTER_TURN || angle > PEEP_QUARTER_TURN;
sprite.isFlipX = isBackward;
sprite.rotation = isBackward ? PEEP_HALF_TURN - angle : angle;
if (!child)
{
auto* trail = static_cast<Entity_Effect*>(g_game->Spawn({.position = pos, .spawner = this,
.type = ENTITY_EFFECT,
.variant = EFFECT_SPRITE_TRAIL, .seed = Random()}));
trail->sprite.color = PEEP_TRAIL_COLOR;
layer_normal_set(trail->sprite, 0);
trail->sprite.scale = vector_make(scale + scale, scale + scale);
trail->SetRadii(PEEP_TRAIL_RADIUS, PEEP_TRAIL_RADIUS);
trail->parent = this;
trail->Update();
child = trail;
}
Entity* trail = child.get();
if (!isVisible)
{
trail->sprite.color._tint[3] = 0;
return;
}
trail->sprite.color._tint[3] = sprite.color._tint[3] * PEEP_TRAIL_ALPHA;
trail->pos = vector_add(pos, posOffset);
trail->velocity = velocity;
}
// The splash of a thrown grid entity: it breaks where it lands as the rock its subtype packs (type high, variant
// low), keeping the shot's health and seed.
void Entity_Projectile::grid_splash()
{
sprite.color = COLOR_DEFAULT;
GridEntityDesc desc{};
desc._type = (int)((uint32_t)subtype >> GRID_TYPE_SHIFT);
desc._variant = subtype & GRID_VARIANT_MASK;
desc._state = (int)health;
desc._spawnSeed = initSeed;
desc._varData = HasEntityFlags(FLAG_NO_DEATH_TRIGGER);
DestroyRockGridAt(pos, &desc, this, wiggleFrameOffset);
}
// A rock's splash: a poof its size in its own color, and, in the room, a crumble and a handful of rock-tinted chips.
void Entity_Projectile::rock_splash()
{
ColorMod color = ROCK_POOF_COLOR;
color_multiply(color, sprite.color);
Entity* poof = effect_spawn(EFFECT_ROCK_POOF, pos, this);
poof->posOffset = posOffset;
poof->SetColor(this->color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
poof->Update();
poof->sprite.scale = vector_make(scale, scale);
poof->sprite.rotation = 0;
if (!g_game->room->IsPositionInRoom(pos, ROCK_SOUND_MARGIN)) return;
sound_play(SOUND_ROCK_CRUMBLE, 1, 1);
int chips = std::min(std::max((int)(scale * ROCK_CHIPS_SCALE * (random_unit() * HALF + 1)), 1), ROCK_CHIPS_MAX);
for (int i = 0; i < chips; i++)
{
Vector direction = vector_from_angle(random_unit() * ROCK_CHIP_TURN);
float speed = random_unit() * ROCK_CHIP_SPEED + 1;
Vector spot = vector_add(pos, vector_scale(direction, random_unit() * size));
Entity* chip = g_game->Spawn({.position = spot, .velocity = vector_scale(direction, speed),
.subType = ROCK_CHIP_SUBTYPE, .type = ENTITY_EFFECT,
.variant = EFFECT_TOOTH_PARTICLE, .seed = Random()});
chip->posOffset = posOffset;
chip->SetColor(color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
chip->Update();
}
}
// A meat ball's splash: a blood burst, then, off the grid, a Globin (one in ten a Gazing one) pulling itself back
// together, shrunk if the shot was.
void Entity_Projectile::meat_splash()
{
Entity* burst = effect_spawn(EFFECT_BLOOD_EXPLOSION, pos, nullptr, MEAT_BURST_SUBTYPE);
burst->posOffset = posOffset;
sound_play(SOUND_SPLATTER, 1, 1);
if (room_grid_collision_at(pos) != 0) return;
int globinVariant = initSeed % MEAT_GAZING_ODDS == 0;
auto* globin = static_cast<Entity_NPC*>(g_game->Spawn({.position = pos,
.velocity = vector_scale(velocity, MEAT_GLOBIN_PUSH),
.spawner = spawnerEntity, .type = ENTITY_GLOBIN,
.variant = globinVariant, .seed = Random()}));
globin->ClearEntityFlags(FLAG_APPEAR);
globin->health = MEAT_GLOBIN_HEALTH;
globin->sprite.Play("ReGenProjectile");
globin->state = NPC_STATE_IDLE;
if (HasEntityFlags(FLAG_SHRINK)) globin->AddShrink(EntityRef(this), MEAT_GLOBIN_SHRINK, false);
}
// A head's splash: a green burst, a small death burst's sound and green gibs.
void Entity_Projectile::head_splash()
{
Entity* burst = effect_spawn(EFFECT_BLOOD_EXPLOSION, pos, nullptr, BLOOD_EXPLOSION_SPLASH);
burst->SetColor(COLOR_GREEN_SPLAT, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
burst->Update();
npc_sound_play(SOUND_DEATH_BURST_SMALL, 1, 2, 1);
gibs_spawn({.color = COLOR_GREEN_SPLAT, .position = pos, .spawner = this, .spread = HEAD_GIB_SPREAD,
.amount = HEAD_GIBS, .gibFlags = HEAD_GIB_FLAGS});
}
// A glowing additive trail particle every other tick: cyan behind a blue fire spawning shot, purple and smaller
// behind a Necro's bone.
void Entity_Projectile::glow_trail_spawn(bool isPurple)
{
Entity_Effect* particle = effect_spawn_moving(EFFECT_HAEMO_TRAIL, pos, vector_scale(velocity, HALF), this);
layer_additive_set(particle->sprite, 0);
particle->posOffset = vector_add(posOffset, vector_make(0, fallingSpeed * HALF));
float glow = random_unit() * TRAIL_GLOW_RANGE + TRAIL_GLOW_MIN;
ColorMod color = tint_color_make(0, 0, 0, 1);
if (isPurple)
{
color._offset[0] = glow * TRAIL_PURPLE_RED;
color._offset[1] = glow * TRAIL_PURPLE_GREEN;
color._offset[2] = glow * TRAIL_PURPLE_BLUE;
particle->sprite.scale = vector_make(TRAIL_PURPLE_SCALE, TRAIL_PURPLE_SCALE);
}
else
{
color._offset[1] = glow * TRAIL_CYAN_GREEN;
color._offset[2] = glow;
}
particle->SetColor(color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
particle->renderDepthOffset = TRAIL_DEPTH_OFFSET;
particle->Update();
}
// A side wave shot sheds two speeding shots off its sides every 8 ticks, of its color and height.
static void side_wave_spawn(Entity_Projectile& shot)
{
Vector across = vector_rotated(shot.velocity, RIGHT_ANGLE_DEGREES);
for (int side = -1; side < 2; side += 2)
{
uint32_t seed = Random();
Vector velocity = vector_scale(across, SIDE_WAVE_SPEED * (float)side);
auto* wave = static_cast<Entity_Projectile*>(
g_game->Spawn({.position = shot.pos, .velocity = velocity, .type = ENTITY_PROJECTILE, .seed = seed}));
wave->spawnerEntity = shot.spawnerEntity.get();
wave->spawnerType = shot.spawnerType;
wave->spawnerVariant = shot.spawnerVariant;
wave->SetColor(shot.color, -1, COLOR_PRIORITY_DEFAULT, false, true);
wave->height = shot.height;
wave->projectileFlags = PROJECTILE_ACCELERATE;
wave->fallingAccel = SIDE_WAVE_FALL;
wave->acceleration = SIDE_WAVE_ACCELERATION;
wave->Update();
}
}
// The trails shots leave: a blue fire spawning shot's and a Necro bone's glow, a side wave's shots, the Beast's fire
// trail and the drops Mother's pound shots leave.
void Entity_Projectile::trails_update()
{
if ((projectileFlags & PROJECTILE_BLUE_FIRE_SPAWN) && GetIntervalTicks(TRAIL_TICKS, 0)) glow_trail_spawn(false);
else if (spawnerType == ENTITY_NECRO && variant == PROJECTILE_BONE && GetIntervalTicks(TRAIL_TICKS, 0))
glow_trail_spawn(true);
if ((projectileFlags & PROJECTILE_SIDE_WAVE) && GetIntervalTicks(SIDE_WAVE_TICKS, 0)) side_wave_spawn(*this);
// War's fire balls in the Beast's fight leave a glowing trail of haemo every other frame, its glow random.
constexpr float FIRE_TRAIL_SPEED = 0.5f;
constexpr float FIRE_TRAIL_DROP = 0.5f;
constexpr float FIRE_TRAIL_GLOW = 0.7f;
constexpr float FIRE_TRAIL_GLOW_LEAST = 0.3f;
constexpr float FIRE_TRAIL_RED = 0.12f;
constexpr float FIRE_TRAIL_GREEN = 0.4f;
constexpr int FIRE_TRAIL_DEPTH = -500;
bool isBeastFire = (projectileFlags & PROJECTILE_EXPLODE) && spawnerType == ENTITY_BEAST &&
spawnerVariant == BEAST_FIRE_BALL;
if (isBeastFire && GetIntervalTicks(2, 0))
{
uint32_t seed = Random();
Entity* trail = g_game->Spawn({.position = pos,
.velocity = vector_scale(velocity, FIRE_TRAIL_SPEED),
.spawner = this,
.type = ENTITY_EFFECT,
.variant = EFFECT_HAEMO_TRAIL,
.seed = seed});
trail->posOffset = vector_add(posOffset, vector_make(0, fallingSpeed * FIRE_TRAIL_DROP));
float glow = random_unit() * FIRE_TRAIL_GLOW + FIRE_TRAIL_GLOW_LEAST;
trail->SetColor({{1, 1, 1, 1}, {0.4f, 2, 0.5f, 1}, {glow * FIRE_TRAIL_RED, glow * FIRE_TRAIL_GREEN, 0}}, -1,
COLOR_PRIORITY_DEFAULT, false, true);
trail->renderZOffset = FIRE_TRAIL_DEPTH;
trail->Update();
}
fly_ball_update(*this);
// Mother's pound shots drop a falling shot of their color every 4 frames.
constexpr float MOTHER_TRAIL_SPEED = 0.2f;
constexpr float MOTHER_TRAIL_RISE = -6;
constexpr float MOTHER_TRAIL_FALL = 1;
bool isMotherShot = spawnerType == ENTITY_MOTHER && spawnerVariant == 0 && subtype == 1;
if (!isMotherShot || !GetIntervalTicks(4, 0)) return;
uint32_t seed = Random();
auto* drop = static_cast<Entity_Projectile*>(g_game->Spawn({.position = pos,
.velocity = vector_scale(velocity, MOTHER_TRAIL_SPEED),
.type = ENTITY_PROJECTILE,
.seed = seed}));
drop->spawnerEntity = spawnerEntity.get();
drop->spawnerType = spawnerType;
drop->spawnerVariant = spawnerVariant;
drop->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true);
drop->fallingSpeed = MOTHER_TRAIL_RISE;
drop->fallingAccel = MOTHER_TRAIL_FALL;
drop->Update();
}
// Its splash: a poof by its variant or an explosion, the creep and splits its flags leave, then it is removed.
// A frozen shot just vanishes.
void Entity_Projectile::death_update()
{
if (HasEntityFlags(FLAG_FREEZE)) return Remove();
death_poof_spawn();
death_flags_apply();
death_bursts_spawn();
if (projectileFlags & PROJECTILE_BLUE_FIRE_SPAWN) blue_fire_spawn();
if (projectileFlags & PROJECTILE_FIRE_SPAWN) fire_spawn();
if (projectileFlags & PROJECTILE_LASER_SHOT) laser_shot_dot_spawn();
if (projectileFlags & PROJECTILE_SIDE_WAVE) side_wave_burst();
if (variant == PROJECTILE_HEAD) head_splash();
Remove();
if (variant == PROJECTILE_FIRE && spawnerType == ENTITY_BEAST) npc_sound_play(SOUND_BEAST_FIRE, 1, 2, 1);
}
// Part of 00653070: a dead laser shot leaves a Tech dot shining toward its target (found anywhere when it had none),
// or back the way it came.
void Entity_Projectile::laser_shot_dot_spawn()
{
constexpr float LASER_SHOT_RANGE = 5000;
constexpr int TECH_DOT_SHOT = 1;
constexpr ColorMod TECH_DOT_COLOR = {{1, 1, 1, 1}, {2.5f, 2.5f, 2.5f, 1}, {0, 0, 0}};
if (!target) target_find(LASER_SHOT_RANGE);
Entity_Effect* dot = effect_spawn(EFFECT_TECH_DOT, pos, spawnerEntity, TECH_DOT_SHOT);
dot->sprite.color = TECH_DOT_COLOR;
dot->targetPosition = target ? vector_normalized(vector_sub(target->pos, pos))
: vector_scale(vector_normalized(velocity), -1);
dot->Update();
}
// Part of 00653070: a dead side wave shot bursts in a splash into five shots fanned back the way it came, 24 apart,
// aimed at it and tinted its color.
void Entity_Projectile::side_wave_burst()
{
constexpr int SIDE_FAN_SIDE = 2;
constexpr float SIDE_FAN_TURN = 24;
constexpr float SIDE_FAN_SPEED = 10;
Vector back = vector_scale(vector_normalized(velocity), -1);
npc_sound_play(SOUND_DEATH_BURST_SMALL, 1, 2, 1);
effect_spawn(EFFECT_BLOOD_EXPLOSION, pos, nullptr, BLOOD_EXPLOSION_SPLASH)->Update();
for (int i = -SIDE_FAN_SIDE; i <= SIDE_FAN_SIDE; i++)
{
uint32_t seed = Random();
Vector shotVelocity = vector_scale(vector_rotated(back, (float)i * SIDE_FAN_TURN), SIDE_FAN_SPEED);
Entity* shot = g_game->Spawn({.position = pos, .velocity = shotVelocity, .spawner = spawnerEntity,
.type = ENTITY_PROJECTILE, .seed = seed});
shot->target = this;
shot->parent = parent.get();
shot->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true);
shot->Update();
}
}
// Part of 00653070: whether the shot is on the players' side, which its fires take.
bool Entity_Projectile::is_friendly_fire()
{
return HasEntityFlags(FLAG_FRIENDLY) || (projectileFlags & PROJECTILE_CANT_HIT_PLAYER);
}
// Part of 00653070: four fire waves from where a fire wave shot landed, along the axes (FIRE_WAVE_X: the diagonals,
// keeping the shot's spawner kind).
void Entity_Projectile::fire_waves_spawn(FireWaveShape shape)
{
npc_sound_play(SOUND_FLAME_BURST, 1, FIRE_WAVE_SOUND_DELAY, 1);
for (int i = 0; i < FIRE_WAVE_COUNT; i++)
{
Entity_Effect* wave = effect_spawn(EFFECT_FIRE_WAVE, pos, spawnerEntity);
wave->parent = this;
if (shape == FIRE_WAVE_CROSS)
{
wave->spawnerType = spawnerType;
wave->spawnerVariant = spawnerVariant;
}
wave->rotation = (float)i * RIGHT_ANGLE_DEGREES + (shape == FIRE_WAVE_CROSS ? FIRE_WAVE_DIAGONAL : 0);
if (is_friendly_fire()) wave->AddEntityFlags(FLAG_FRIENDLY);
}
}
// Part of 00653070: a fire a shot leaves inside the room: a moving fireplace for an enemy's shot, a player's flame
// (with a timeout) for a friendly one, its health or size a random share.
void Entity_Projectile::fire_leave(const LeftFire& fire)
{
if (!is_friendly_fire())
{
Entity* place = g_game->Spawn({.position = pos, .spawner = this, .type = ENTITY_FIREPLACE,
.variant = fire.fireplaceVariant, .seed = Random()});
place->targetPosition = pos;
place->health = place->health * (random_unit() * FIRE_SHARE_RANGE + fire.share);
return;
}
Entity_Effect* flame = effect_spawn(fire.flameVariant, pos, this);
if (fire.flameVariant == EFFECT_BLUE_FLAME)
{
flame->spawnerType = ENTITY_PLAYER;
flame->spawnerVariant = 0;
}
flame->scale = random_unit() * FIRE_SHARE_RANGE + fire.share;
flame->SetCollisionDamage(fire.flameDamage);
flame->SetTimeout(fire.timeoutRange > 0 ? random_below(fire.timeoutRange) + fire.timeoutMin
: fire.timeoutMin);
}
// Part of 00653070: a blue fire spawning shot (a bone one with a crack) lands in a blue poof and leaves a blue fire.
void Entity_Projectile::blue_fire_spawn()
{
if (variant == PROJECTILE_BONE) npc_sound_play(SOUND_FORTUNE_COOKIE, 1, 2, 1);
if (!g_game->room->IsPositionInRoom(pos, 0)) return;
Entity* poof = effect_spawn(EFFECT_POOF01, pos, spawnerEntity);
poof->SetColor(BLUE_FIRE_POOF_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
poof->Update();
fire_leave({.flameDamage = BLUE_FLAME_DAMAGE,
.share = BLUE_FIRE_SHARE,
.fireplaceVariant = FIREPLACE_BLUE_MOVING,
.flameVariant = EFFECT_BLUE_FLAME,
.timeoutMin = BLUE_FLAME_TIMEOUT});
npc_sound_play(SOUND_CANDLE_LIGHT, 1, 2, 1);
}
// Part of 00653070: a fire spawning shot bursts in a fire jet, and one in ten inside the room leaves a fire.
void Entity_Projectile::fire_spawn()
{
Entity* jet = effect_spawn(EFFECT_FIRE_JET, pos, spawnerEntity);
jet->spawnerType = spawnerType;
jet->spawnerVariant = variant;
if (is_friendly_fire()) jet->AddEntityFlags(FLAG_FRIENDLY);
if (random_below(FIRE_LEAVE_ODDS) != 0 || !g_game->room->IsPositionInRoom(pos, 0)) return;
fire_leave({.flameDamage = RED_FLAME_DAMAGE,
.share = FIRE_SHARE,
.fireplaceVariant = FIREPLACE_RED_MOVING,
.flameVariant = EFFECT_RED_CANDLE_FLAME,
.timeoutMin = RED_FLAME_TIMEOUT_MIN,
.timeoutRange = RED_FLAME_TIMEOUT_RANGE});
}
// 0065ad30 (misnamed reroll_entity_to_free_tile) A tick of the lava the Beast's fire ball raised: a pair of glowing
// lava shots thrown up either side, as far out as its state counts in cells and slower the farther, then the next.
static void beast_lava_tick(Entity_Effect* timer)
{
constexpr float RISE_PER_STEP = 3;
constexpr float RISE_BASE = -30;
constexpr float LAVA_SHOT_SCALE = 1.5f;
constexpr ColorMod LAVA_SHOT_COLOR = {{2, 2, 2, 1}, {0, 0, 0, 0}, {0.4f, 0.12f, -0.3f}};
for (int side = -1; side <= 1; side += 2)
{
Vector position = vector_make((float)side * (float)timer->state * GRID_CELL_SIZE + timer->pos.x, timer->pos.y);
float jitter = random_unit();
float rise = (float)timer->state * RISE_PER_STEP + RISE_BASE + (jitter + jitter);
auto* shot = static_cast<Entity_Projectile*>(g_game->Spawn(
{.position = position, .velocity = vector_make(0, rise), .type = ENTITY_PROJECTILE, .seed = Random()}));
shot->scale = LAVA_SHOT_SCALE;
shot->spawnerEntity = timer->spawnerEntity;
shot->spawnerType = timer->spawnerType;
shot->spawnerVariant = timer->spawnerVariant;
shot->SetColor(LAVA_SHOT_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
shot->fallingSpeed = 0;
shot->fallingAccel = 1;
shot->Update();
}
timer->state++;
}
// Part of 00653070: the Beast's fire ball bursts with a shake into fourteen rising smoke clouds and as many lava shots
// thrown up around it. Over the lava, the lava rises either side in four ticks.
void Entity_Projectile::beast_fire_ball_burst()
{
constexpr int FIRE_BALL_SHAKE = 8;
constexpr int FIRE_BALL_BURSTS = 14;
constexpr float SMOKE_RISE = -2;
constexpr float SMOKE_RISE_STEP = 7;
constexpr float SMOKE_SPREAD = 10;
constexpr float FIRE_BALL_SMOKE_SCALE_MIN = 1.2f;
constexpr float SMOKE_SCALE_RANGE = 0.3f;
constexpr int SMOKE_TIMEOUT = 360;
constexpr float SHOT_RISE_MIN = -10;
constexpr float SHOT_RISE_RANGE = 28;
constexpr float SHOT_SPREAD_X = 60;
constexpr float SHOT_SPREAD_Y = 20;
constexpr float FIRE_BALL_SHOT_SCALE_MIN = 1;
constexpr float SHOT_SCALE_RANGE = 0.7f;
constexpr uint32_t LAVA_TICK_DELAY = 5;
constexpr uint32_t LAVA_TICKS = 4;
game_shake_screen(FIRE_BALL_SHAKE);
for (int i = 0; i < FIRE_BALL_BURSTS; i++)
{
uint32_t smokeSeed = Random();
Vector smokeVelocity = vector_make(0, SMOKE_RISE - (float)i * SMOKE_RISE_STEP);
Vector smokePosition = vector_add(pos, vector_scale(random_direction(), SMOKE_SPREAD));
Entity* smoke = g_game->Spawn({.position = smokePosition, .velocity = smokeVelocity, .spawner = spawnerEntity,
.type = ENTITY_EFFECT, .variant = EFFECT_SMOKE_CLOUD, .seed = smokeSeed});
smoke->spawnerType = spawnerType;
smoke->spawnerVariant = spawnerVariant;
float smokeScale = random_unit() * SMOKE_SCALE_RANGE + FIRE_BALL_SMOKE_SCALE_MIN;
smoke->sprite.scale = vector_make(smokeScale, smokeScale);
smoke->Update();
static_cast<Entity_Effect*>(smoke)->SetTimeout(SMOKE_TIMEOUT);
uint32_t shotSeed = Random();
Vector shotVelocity = vector_make(0, SHOT_RISE_MIN - random_unit() * SHOT_RISE_RANGE);
float offsetY = random_unit() * SHOT_SPREAD_Y - SHOT_SPREAD_Y * HALF;
float offsetX = random_unit() * SHOT_SPREAD_X - SHOT_SPREAD_X * HALF;
Vector shotPosition = vector_add(pos, vector_make(offsetX, offsetY));
auto* shot = static_cast<Entity_Projectile*>(g_game->Spawn(
{.position = shotPosition, .velocity = shotVelocity, .type = ENTITY_PROJECTILE, .seed = shotSeed}));
shot->scale = random_unit() * SHOT_SCALE_RANGE + FIRE_BALL_SHOT_SCALE_MIN;
shot->spawnerEntity = spawnerEntity.get();
shot->spawnerType = spawnerType;
shot->spawnerVariant = spawnerVariant;
shot->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true);
shot->fallingSpeed = 0;
shot->fallingAccel = 1;
shot->Update();
}
float lavaLine = g_game->room->get_lava_line_y();
if (pos.y < lavaLine) return;
Entity_Effect* lava =
Entity_Effect::CreateTimer(reinterpret_cast<void*>(&beast_lava_tick), LAVA_TICK_DELAY, LAVA_TICKS, false);
lava->spawnerEntity = spawnerEntity.get();
lava->spawnerType = spawnerType;
lava->spawnerVariant = spawnerVariant;
lava->pos = vector_make(pos.x, lavaLine);
lava->state = 1;
}
// Part of 00653070: a Heretic's fireball bursts into six purple fire shots.
void Entity_Projectile::heretic_burst_spawn()
{
for (int i = 0; i < HERETIC_BURST_SHOTS; i++)
{
Vector velocity = vector_scale(vector_from_angle((float)i * HERETIC_BURST_STEP + 0), HERETIC_BURST_SPEED);
auto* shot = static_cast<Entity_Projectile*>(g_game->Spawn({.position = pos,
.velocity = velocity,
.spawner = parent,
.type = ENTITY_PROJECTILE,
.variant = PROJECTILE_FIRE,
.seed = Random()}));
shot->spawnerType = spawnerType;
shot->spawnerVariant = spawnerVariant;
shot->fallingAccel = HERETIC_BURST_FALL;
shot->SetColor(HERETIC_BURST_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
}
}
// The poof of a dead shot by its variant, or the explosion of an exploding one, tinted its color.
void Entity_Projectile::death_poof_spawn()
{
Entity_Effect* poof = nullptr;
if (projectileFlags & PROJECTILE_EXPLODE)
{
poof = effect_spawn(EFFECT_BOMB_EXPLOSION, pos, nullptr);
explosion_damage({.position = pos,
.source = parent ? parent.get() : this,
.damageFlags = DAMAGE_EXPLOSION,
.damage = EXPLOSION_DAMAGE,
.radius = EXPLOSION_RADIUS});
projectileFlags &= ~PROJECTILE_EXPLODE;
if (spawnerType == ENTITY_HERETIC) heretic_burst_spawn();
if (spawnerType == ENTITY_BEAST && spawnerVariant == BEAST_FIRE_BALL) beast_fire_ball_burst();
}
else if (is_listed(POOF_VARIANTS, variant))
{
int effect = EFFECT_BULLET_POOF;
int sound = height > FLOOR_HEIGHT ? SOUND_SPLATTER : SOUND_TEAR_IMPACTS;
float poofScale = scale * POOF_SCALE_SHARE;
float volume = 1;
if (variant == PROJECTILE_TEAR)
{
if (scale < TEAR_POOF_SMALL) effect = EFFECT_TEAR_POOF_VERYSMALL;
else if (scale < TEAR_POOF_LARGE) effect = EFFECT_TEAR_POOF_SMALL;
else effect = height <= FLOOR_HEIGHT ? EFFECT_TEAR_POOF_A : EFFECT_TEAR_POOF_B;
if (scale < TEAR_POOF_LARGE) poofScale = 1;
if (scale < TEAR_POOF_SMALL) volume = POOF_VOLUME_TINY;
else if (scale < TEAR_POOF_LARGE) volume = POOF_VOLUME_SMALL;
}
else if (variant == PROJECTILE_CORN)
{
game_particles_spawn({.color = sprite.color,
.position = pos,
.height = height,
.speed = random_unit() * CORN_PARTICLE_SPEED + 1,
.count = CORN_PARTICLES,
.variant = EFFECT_CORN_PARTICLE});
effect = EFFECT_CORN_POOF;
}
else if (variant == PROJECTILE_HUSH) effect = EFFECT_BULLET_POOF_HUSH;
else if (sprite.bitflags & ANIM_RENDER_STATIC) effect = EFFECT_DOGMA_POOF;
poof = effect_spawn(effect, pos, nullptr);
if (poof)
{
poof->posOffset = posOffset;
poof->sprite.scale = vector_make(poofScale, poofScale);
poof->sprite.rotation = (float)random_below(4) * RIGHT_ANGLE_DEGREES;
}
if (g_game->room->IsPositionInRoom(pos, POOF_SOUND_MARGIN)) sound_play(sound, 1, volume);
}
else if (variant == PROJECTILE_BONE)
{
game_particles_spawn({.color = sprite.color,
.position = pos,
.height = height,
.speed = random_unit() * CORN_PARTICLE_SPEED + 1,
.count = BONE_PARTICLES,
.subType = BONE_PARTICLE_SUBTYPE,
.variant = EFFECT_BONE_PARTICLE});
}
else if (variant == PROJECTILE_FIRE)
{
Entity_Effect* smoke = effect_spawn(EFFECT_POOF01, vector_add(pos, vector_make(0, POOF_RAISE)), nullptr,
FIRE_POOF_SUBTYPE);
ColorMod color = sprite.color;
color._tint[3] *= FIRE_POOF_ALPHA;
if (smoke) smoke->SetColor(color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
else if (variant == PROJECTILE_COIN)
{
Entity_Effect* coinPoof = effect_spawn(EFFECT_COIN_POOF, vector_add(pos, vector_make(0, POOF_RAISE)), nullptr);
if (coinPoof) coinPoof->SetColor(COLOR_DEFAULT, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
float speed = random_unit() * COIN_PARTICLE_SPEED + COIN_PARTICLE_SPEED_MIN;
game_particles_spawn({.color = sprite.color,
.position = pos,
.height = height,
.speed = speed,
.count = random_below(2) + COIN_PARTICLES,
.variant = EFFECT_COIN_PARTICLE});
sound_play(SOUND_COIN_IMPACT, random_unit() * COIN_SOUND_PITCH_SPREAD + COIN_SOUND_PITCH, COIN_SOUND_VOLUME);
}
else if (variant == PROJECTILE_GRID) grid_splash();
else if (variant == PROJECTILE_ROCK) rock_splash();
else if (variant == PROJECTILE_MEAT) meat_splash();
if (!(projectileFlags & PROJECTILE_EXPLODE) && (projectileFlags & PROJECTILE_CREEP_BROWN))
{
Entity_Effect* creep = effect_spawn(EFFECT_CREEP_BROWN, pos, this);
if (creep)
{
creep->Update();
creep->SetTimeout(BROWN_CREEP_TIMEOUT);
}
}
if (!poof) return;
poof->SetColor(sprite.color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
if (variant == PROJECTILE_PUKE) poof->SetColor(PUKE_POOF_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
else if (variant == PROJECTILE_FCUK)
poof->SetColor(FCUK_POOF_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
// The creep a dead shot's flags leave: acid with a splat, and goo, smaller from Pin's, Tar Boy's and the Leapers'.
void Entity_Projectile::death_flags_apply()
{
if (projectileFlags & (PROJECTILE_ACID_GREEN | PROJECTILE_ACID_RED))
{
int creepVariant = (projectileFlags & PROJECTILE_ACID_GREEN) ? EFFECT_CREEP_GREEN : EFFECT_CREEP_RED;
Entity_Effect* creep = effect_spawn(creepVariant, pos, this);
if (creep)
{
creep->spawnerType = spawnerType;
bool isGhost = projectileFlags & PROJECTILE_GHOST;
float creepScale = isGhost ? ACID_GHOST_SCALE : ACID_SCALE;
creep->sprite.scale = vector_make(creepScale, creepScale);
if (isGhost) creep->SetTimeout(ACID_GHOST_TIMEOUT);
creep->Update();
}
Entity_Effect* splat = effect_spawn(EFFECT_BLOOD_SPLAT, pos, nullptr, ACID_SPLAT_SUBTYPE);
if (splat)
{
float splatScale = random_unit() + ACID_SPLAT_SCALE;
splat->sprite.scale = vector_make(splatScale, splatScale);
splat->SetColor(sprite.color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
splat->Update();
}
}
if (!(projectileFlags & PROJECTILE_GOO)) return;
bool isPin = spawnerType == ENTITY_PIN;
bool isSmall = isPin || spawnerType == ENTITY_TARBOY || spawnerType == ENTITY_LEAPER;
Entity_Effect* goo = effect_spawn(isPin ? EFFECT_CREEP_WHITE : EFFECT_CREEP_BLACK, pos, this);
if (goo)
{
if (!isPin && isSmall) goo->SetTimeout(GOO_TIMEOUT);
float gooScale = isSmall ? GOO_SMALL_SCALE : GOO_SCALE;
goo->sprite.scale = vector_make(gooScale, gooScale);
goo->Update();
}
if (isPin) npc_sound_play(SOUND_PIN_GOO, 1, 2, 1);
}
// Rotgut's heart's fire bursting into two shots, 30 degrees either side of up, that start bouncing after 6 frames.
static void rotgut_burst_spawn(Entity_Projectile& shot)
{
for (int side = -1; side <= 1; side += 2)
{
uint32_t seed = Random();
float speed = lerp(ROTGUT_SHOT_SPEED_MIN, ROTGUT_SHOT_SPEED_MAX, random_unit());
Vector velocity = vector_rotated(vector_make(0, speed), (float)side * ROTGUT_BURST_TURN);
Entity_Projectile* child = child_spawn(shot, {.position = vector_add(shot.pos, vector_make(0, ROTGUT_BURST_RISE)),
.velocity = velocity,
.spawner = shot.spawnerEntity,
.type = ENTITY_PROJECTILE,
.seed = seed});
child->projectileFlags = PROJECTILE_NO_WALL_COLLIDE | PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT;
child->changeFlags = 0;
child->changeTimeout = ROTGUT_BURST_TIMEOUT;
child->fallingAccel = ROTGUT_BURST_FALL;
child->SetColor(SHOT_COLOR_FIERY, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
}
// Rotgut's heart's spit splitting into a fan of 13 shots up across 150 degrees that bounce after 45 frames, keeping
// its other flags.
static void rotgut_split_spawn(Entity_Projectile& shot)
{
uint64_t kept = shot.projectileFlags & ~PROJECTILE_BURSTSPLIT;
for (int i = 0; i < ROTGUT_SPLIT_SHOTS; i++)
{
float speed = lerp(ROTGUT_SHOT_SPEED_MIN, ROTGUT_SHOT_SPEED_MAX, random_unit());
float angle = lerp_range((float)i, {.inMax = ROTGUT_SPLIT_SHOTS - 1, .outMin = -ROTGUT_SPLIT_TURN, .outMax = ROTGUT_SPLIT_TURN});
Entity_Projectile* child = child_spawn(shot, {.position = shot.pos,
.velocity = vector_rotated(vector_make(0, speed), angle),
.spawner = shot.spawnerEntity,
.type = ENTITY_PROJECTILE,
.seed = Random()});
child->projectileFlags = kept | PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT | PROJECTILE_BOUNCE;
child->changeFlags = kept;
child->changeTimeout = ROTGUT_SPLIT_TIMEOUT;
child->fallingAccel = ROTGUT_SPLIT_FALL;
child->scale = random_unit() * ROTGUT_SPLIT_SCALE_RANGE + ROTGUT_SPLIT_SCALE;
}
}
// The shots a dead shot splits into: four for BURST (a cross from Pin's), eight or six for BURST8, two back for
// BACK_SPLIT and a fan of sixteen for BURSTSPLIT.
void Entity_Projectile::death_bursts_spawn()
{
bool isRotgutHeart = spawnerType == ENTITY_ROTGUT && spawnerVariant == ROTGUT_HEART;
if ((projectileFlags & PROJECTILE_BURST) && isRotgutHeart) rotgut_burst_spawn(*this);
else if (projectileFlags & PROJECTILE_BURST)
{
bool isPin = spawnerType == ENTITY_PIN;
float speed = vector_length(velocity);
Vector shot = isPin ? vector_make(speed, 0) : vector_scale(random_direction(), speed);
if (isPin && random_below(2) == 0) shot = vector_rotated(shot, ZIGZAG_START_TURN);
if (isPin && vector_length_squared(shot) < SPLIT_SLOW_SPEED_SQUARED)
shot = vector_scale(shot, SPLIT_SPEED_BOOST);
for (int i = 0; i < BURST_COUNT; i++)
{
Vector childVelocity = shot;
if (!isPin)
{
childVelocity = i < 2 ? shot : vector_make(shot.y, -shot.x);
if (i & 1) childVelocity = vector_scale(childVelocity, -1);
}
Entity_Projectile* child = child_spawn(*this, {.position = vector_sub(pos, velocity),
.velocity = childVelocity,
.spawner = isPin ? nullptr : parent.get(),
.type = type,
.variant = variant,
.seed = Random()});
if (isPin) shot = vector_make(shot.y, -shot.x);
if (!child) continue;
if (isPin)
{
child->height = std::min(height * SPLIT_HEIGHT_SHARE, HEIGHT_START);
child->scale = std::max(scale - SPLIT_SCALE_LOSS, 1.0f);
child->fallingAccel = SPLIT_FALL_ACCEL;
child->fallingSpeed = SPLIT_FALL_SPEED;
}
else
{
child->height = BURST_HEIGHT;
child->fallingAccel = fallingAccel;
child->fallingSpeed = random_unit() * BURST_FALL_SPREAD - BURST_FALL_SPEED;
child->SetCollisionDamage(1);
child->scale = scale * BURST_SCALE_SHARE;
}
child->projectileFlags = projectileFlags & ~PROJECTILE_BURST;
child->SetColor(sprite.color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
child->parent = parent.get();
child->Update();
}
}
if (projectileFlags & PROJECTILE_FIRE_WAVE) fire_waves_spawn(FIRE_WAVE_PLUS);
if (projectileFlags & PROJECTILE_FIRE_WAVE_X) fire_waves_spawn(FIRE_WAVE_CROSS);
if (projectileFlags & PROJECTILE_BURST8)
{
pos = vector_sub(pos, vector_scale(gridCollisionVelocity, 2));
sound_play(SOUND_BLOOD_SPLASH, 1, 1);
effect_spawn(EFFECT_BLOOD_EXPLOSION, pos, nullptr, BLOOD_EXPLOSION_SPLASH);
bool isBloodfly = spawnerType == ENTITY_SUCKER && spawnerVariant == SUCKER_BLOODFLY;
int count = isBloodfly ? BURST8_BLOODFLY_COUNT : BURST8_COUNT;
for (int i = 0; i < count; i++)
{
Vector shot = vector_scale(vector_from_angle((float)i * 360 / (float)count), BURST8_SPEED);
Entity_Projectile* child = child_spawn(*this, {.position = vector_add(pos, shot),
.velocity = shot,
.spawner = spawnerEntity,
.type = ENTITY_PROJECTILE,
.seed = Random()});
if (!child) continue;
uint64_t flags = projectileFlags & ~PROJECTILE_BURST8;
if (subtype > 0)
{
child->scale = scale * BURST8_CHILD_SCALE;
child->subtype = subtype - 1;
flags = projectileFlags;
}
child->projectileFlags = flags;
}
}
if (projectileFlags & PROJECTILE_BACK_SPLIT)
{
Vector back = vector_scale(vector_normalized(velocity), -1);
for (int side = -1; side < 2; side += 2)
{
Vector shot = vector_scale(vector_rotated(back, (float)side * BACK_SPLIT_TURN), BACK_SPLIT_SPEED);
Entity_Projectile* child = child_spawn(*this, {.position = vector_add(pos, shot),
.velocity = shot,
.spawner = spawnerEntity,
.type = ENTITY_PROJECTILE,
.seed = Random()});
if (!child) continue;
child->fallingAccel = BACK_SPLIT_ACCEL;
child->projectileFlags = projectileFlags & ~PROJECTILE_BACK_SPLIT;
}
}
if (!(projectileFlags & PROJECTILE_BURSTSPLIT)) return;
sound_play(SOUND_BLOOD_SPLASH, 1, 1);
Entity_Effect* splash = effect_spawn(EFFECT_BLOOD_EXPLOSION, pos, nullptr, BLOOD_EXPLOSION_SPLASH);
if (splash) splash->SetColor(BURSTSPLIT_SPLASH_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
if (isRotgutHeart) return rotgut_split_spawn(*this);
for (int i = 0; i < BURSTSPLIT_COUNT; i++)
{
float speed = lerp(BURSTSPLIT_SPEED_MIN, BURSTSPLIT_SPEED_MAX, random_unit());
float angle = lerp(-BURSTSPLIT_ANGLE, BURSTSPLIT_ANGLE, random_unit());
Vector shot = vector_rotated(vector_make(0, speed), angle);
Entity_Projectile* child = child_spawn(*this, {.position = pos,
.velocity = shot,
.spawner = spawnerEntity,
.type = ENTITY_PROJECTILE,
.seed = Random()});
if (!child) continue;
child->SetColor(SHOT_COLOR_SICK_PALE, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
child->projectileFlags = projectileFlags & ~(PROJECTILE_BURSTSPLIT | PROJECTILE_EXPLODE);
child->fallingAccel = BURSTSPLIT_ACCEL;
child->scale = random_unit() * BURST_SCALE_SHARE + BURSTSPLIT_SCALE_MIN;
}
}
// The shot's next target within a range: the nearest enemy for a friendly shot (charmed shooter or one that cannot
// hit the player), else the player its shooter would go after.
void Entity_Projectile::target_find(float range)
{
bool isFriendly = (spawnerEntity && spawnerEntity->HasEntityFlags(FLAG_CHARM)) ||
(projectileFlags & PROJECTILE_CANT_HIT_PLAYER);
target = isFriendly ? game_nearest_enemy(pos, spawnerEntity, range)
: game_nearest_target_player(pos, spawnerEntity, false, true, range);
}
// Homing: SMART_PERFECT turns straight at its target, SMART pulls toward a near one (purple while it does). A
// friendly shot goes after enemies.
void Entity_Projectile::homing_update()
{
if (projectileFlags & PROJECTILE_SMART_PERFECT)
{
if (!target) target_find(PERFECT_RANGE);
if (!target) return;
Vector pull = vector_scale(vector_sub(target->pos, pos), PERFECT_PULL);
velocity = vector_resized(vector_add(velocity, pull), std::max(vector_length(velocity), PERFECT_SPEED_MIN));
return;
}
if (!(projectileFlags & PROJECTILE_SMART)) return;
if (IsFrame(SMART_RETARGET_FRAMES, 0)) target = nullptr;
if (!target) target_find(SMART_RANGE);
if (target)
{
Vector lead = vector_sub(vector_add(target->pos, vector_scale(target->velocity, SMART_LEAD)), pos);
if (vector_length(lead) < SMART_REACH)
{
Vector pull = vector_scale(vector_normalized(vector_sub(target->pos, pos)), SMART_PULL);
velocity = vector_add(velocity, vector_scale(pull, homingStrength * speedMultiplier));
velocity = vector_scale(velocity,
timescale_factor(lerp(1, SMART_DRAG, homingStrength), speedMultiplier));
}
}
if (color._tint[0] == 1) SetColor(SMART_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
// The steering flags: the ghost's look, the creep trails, the orbits, the curves, the turn toward a player at
// their height and the pull along a tractor beam.
void Entity_Projectile::steering_update()
{
if ((projectileFlags & PROJECTILE_GHOST) && spawnerType != ENTITY_RED_GHOST)
SetColor(GHOST_COLOR, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
if ((projectileFlags & PROJECTILE_RED_CREEP) && IsFrame(RED_CREEP_FRAMES, 0))
{
Entity_Effect* creep = effect_spawn(EFFECT_CREEP_RED, pos, this);
if (creep && spawnerType != ENTITY_MR_FRED) creep->SetTimeout(RED_CREEP_TIMEOUT);
}
if ((projectileFlags & PROJECTILE_CREEP_BROWN) && GetIntervalTicks(BROWN_CREEP_TICKS, 0))
{
Entity_Effect* creep = effect_spawn(EFFECT_CREEP_BROWN, pos, this);
if (creep)
{
creep->Update();
creep->SetTimeout(BROWN_CREEP_TIMEOUT);
}
}
if (projectileFlags & PROJECTILE_CONTINUUM) continuum_update();
orbit_update();
if (projectileFlags & (PROJECTILE_CURVE_LEFT | PROJECTILE_CURVE_RIGHT))
{
float speed = vector_length(velocity);
Vector side = vector_make(-velocity.y, velocity.x);
if (projectileFlags & PROJECTILE_CURVE_LEFT) side = vector_scale(side, -1);
velocity = vector_add(velocity, vector_scale(side, speed * curvingStrength * speedMultiplier));
velocity = vector_resized(velocity, speed);
}
if (projectileFlags & PROJECTILE_TURN_HORIZONTAL)
{
Room& room = *g_game->room;
for (Entity_Player* player : g_game->playerManager.playerList)
{
Vector toPlayer = vector_sub(player->pos, pos);
float alignment = vector_dot(vector_make(0, 1), vector_normalized(toPlayer));
bool isInRoom =
room.topLeftPos.x - TURN_ROOM_MARGIN < pos.x && pos.x < room.bottomRightPos.x + TURN_ROOM_MARGIN;
if (alignment >= TURN_ALIGN_MAX || alignment <= TURN_ALIGN_MIN || !isInRoom) continue;
Vector turn = vector_make(vector_length(velocity) * TURN_SPEED, 0);
velocity = vector_dot(toPlayer, turn) <= 0 ? vector_scale(turn, -1) : turn;
projectileFlags &= ~PROJECTILE_TURN_HORIZONTAL;
break;
}
}
if (!(projectileFlags & PROJECTILE_TRACTOR_BEAM)) return;
bool isBeamGone = !spawnerEntity || vector_is_zero(spawnerEntity->targetPosition);
float beamLength = isBeamGone ? 0 : vector_length(spawnerEntity->targetPosition);
Vector origin = isBeamGone ? Vector{} : spawnerEntity->pos;
Vector direction = isBeamGone ? Vector{} : vector_scale(spawnerEntity->targetPosition, 1 / beamLength);
Vector offset = vector_sub(pos, origin);
float along = vector_dot(offset, direction) < 0 ? -vector_length(offset) : vector_length(offset);
if (isBeamGone || along < 0 || beamLength < along)
{
isDead = true;
projectileFlags &= ~PROJECTILE_TRACTOR_BEAM;
return;
}
float speed = vector_length(velocity);
pos = vector_add(origin, vector_scale(direction, along));
velocity = vector_scale(direction, speed);
}
// Part of 00653070: a Continuum shot wraps around the screen's edges, cycling every other frame through a colorize
// of the nine its age picks (pink to white to grey, black, then purple).
void Entity_Projectile::continuum_update()
{
pos = room_screen_wrap(pos, CONTINUUM_WRAP_MARGIN);
if (!IsFrame(2, (int)index)) return;
SetColor(continuum_tint_get((int)(updateTime * HALF)), COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
// The tint a Continuum shot or tear takes at a step of its cycle: a colorize of the nine, or black.
ColorMod continuum_tint_get(int step)
{
ColorMod tint = COLOR_DEFAULT;
step %= CONTINUUM_COLORS;
if (step == CONTINUUM_DARK_STEP)
{
tint._tint[0] = tint._tint[1] = tint._tint[2] = 0;
return tint;
}
for (int i = 0; i < 3; i++) tint._colorize[i] = CONTINUUM_COLORIZES[step][i];
tint._colorize[3] = 1;
return tint;
}
// The orbits: it circles its target position (or its parent with ORBIT_PARENT, moving along with it), pulled to its
// orbit distance.
void Entity_Projectile::orbit_update()
{
if (!(projectileFlags & (PROJECTILE_ORBIT_CW | PROJECTILE_ORBIT_CCW))) return;
bool isParentOrbit = (projectileFlags & PROJECTILE_ORBIT_PARENT) && parent;
if (isParentOrbit) velocity = vector_sub(velocity, orbitParentVelocity);
Vector center = isParentOrbit ? parent->pos : targetPosition;
Vector offset = vector_sub(pos, center);
Vector tangent = vector_make(-offset.y, offset.x);
if (projectileFlags & PROJECTILE_ORBIT_CCW) tangent = vector_scale(tangent, -1);
float pull = orbitDistance <= 0
? ORBIT_PULL
: std::clamp(1 - vector_length(offset) / orbitDistance, -1.0f, 1.0f) * ORBIT_PULL;
Vector heading = vector_normalized(vector_add(vector_scale(offset, pull), tangent));
velocity = vector_scale(heading, vector_length(velocity));
if (!isParentOrbit) return;
velocity = vector_add(velocity, parent->velocity);
orbitParentVelocity = parent->velocity;
targetPosition = parent->pos;
}
// The wiggles and zigzags, and the pull back to its parent, which removes it once it arrives. Whether it did.
bool Entity_Projectile::wiggles_update()
{
if (projectileFlags & PROJECTILE_WIGGLE_ROTGUT)
{
RNG rng;
rng.SetSeed(initSeed, ROTGUT_RNG_SHIFT);
float strength = lerp(ROTGUT_WIGGLE_MIN, ROTGUT_WIGGLE_MAX, rng.RandomFloat());
float phase = updateTime / ROTGUT_WIGGLE_PERIOD + rng.RandomFloat();
float length = vector_length(targetPosition);
Vector direction = vector_scale(targetPosition, 1 / length);
float along = vector_dot(velocity, direction);
Vector forward = vector_scale(direction, along);
Vector wiggle = vector_scale(vector_rotated(direction, RIGHT_ANGLE_DEGREES), strength * std::sin(phase * 2 * PI_F));
if (along < length) forward = vector_add(forward, vector_scale(direction, length * ROTGUT_WIGGLE_PUSH));
velocity = vector_add(forward, wiggle);
}
if (projectileFlags & PROJECTILE_SINE_VELOCITY)
pos = vector_add(pos, vector_scale(velocity, std::sin(wave_phase(updateTime))));
float wiggleTime = (float)wiggleFrameOffset + updateTime;
if (projectileFlags & PROJECTILE_WIGGLE)
{
float speed = spawnerType == ENTITY_MOTHER ? 2 : 1;
float wave = std::cos(wave_phase(wiggleTime * speed)) * WIGGLE_SIZE;
pos = vector_add(pos, vector_scale(wiggle_side(velocity), wave));
}
if (projectileFlags & PROJECTILE_MEGA_WIGGLE)
{
float size = std::min(wiggleTime * MEGA_WIGGLE_GROWTH, MEGA_WIGGLE_GROWN) + MEGA_WIGGLE_BASE;
pos = vector_add(pos, vector_scale(wiggle_side(velocity), std::cos(wave_phase(wiggleTime)) * size));
}
auto zigzag = [this](float frames, float turn)
{
int peak = (int)(std::sqrt(2.0f) * frames / (std::sqrt(2.0f) + 1));
if (FrameCount() == 1) velocity = vector_rotated(velocity, ZIGZAG_START_TURN);
else if (update_ticks_get() >= 1)
{
int step = (int)updateTime % (int)frames;
if (step == peak) velocity = vector_rotated(velocity, -turn);
else if (step == 0) velocity = vector_rotated(velocity, turn);
}
};
if (projectileFlags & PROJECTILE_SAWTOOTH_WIGGLE) zigzag(SAWTOOTH_FRAMES, SAWTOOTH_TURN);
if (projectileFlags & PROJECTILE_TRIANGLE) zigzag(TRIANGLE_FRAMES, TRIANGLE_TURN);
if (!(projectileFlags & PROJECTILE_MOVE_TO_PARENT) || !parent) return false;
Vector toParent = vector_sub(parent->pos, pos);
velocity = vector_resized(toParent, vector_length(velocity));
if (vector_length_squared(toParent) >= vector_length_squared(velocity)) return false;
Remove();
return true;
}
// The speed flags: the pull to a position, acceleration (capped for Hush's and Shady's shots), deceleration and the
// acceleration that grows in, and BURST3's split into three.
void Entity_Projectile::speed_update()
{
if (projectileFlags & PROJECTILE_ACCELERATE_TO_POSITION)
{
Vector toTarget = vector_sub(targetPosition, pos);
float distance = vector_length(toTarget);
if (distance >= TARGET_ARRIVED)
{
AddVelocity(vector_scale(toTarget, homingStrength / distance), false);
if (updateTime < TARGET_HOVER_FRAMES)
{
fallingSpeed = 0;
height = TARGET_HOVER_HEIGHT;
}
}
else isDead = true;
}
if (projectileFlags & PROJECTILE_ACCELERATE)
{
velocity = vector_scale(velocity, timescale_factor(acceleration, speedMultiplier));
float speed = vector_length(velocity);
float speedMax = spawnerType == ENTITY_HUSH ? HUSH_SPEED_MAX : SHADY_SPEED_MAX;
bool isCapped = spawnerType == ENTITY_HUSH || spawnerType == ENTITY_SHADY;
if (isCapped && speed > speedMax) velocity = vector_scale(velocity, speedMax / speed);
}
if (projectileFlags & PROJECTILE_DECELERATE)
velocity = vector_scale(velocity, timescale_factor(1 / acceleration, speedMultiplier));
if (projectileFlags & PROJECTILE_ACCELERATE_EX)
{
float grown = lerp(1, acceleration, std::min(updateTime / ACCELERATE_EX_FRAMES, 1.0f));
velocity = vector_scale(velocity, timescale_factor(grown, speedMultiplier));
}
if (!(projectileFlags & PROJECTILE_BURST3) || updateTime <= BURST3_WAIT) return;
bool isSplitting = (update_ticks_get() > 0 && random_below(BURST3_CHANCE) == 0) || spawnerType == ENTITY_PIN;
if (!isSplitting) return;
std::array<Vector, 3> shots = {vector_make(-velocity.y, velocity.x), vector_make(velocity.y, -velocity.x),
velocity};
constexpr std::array<uint64_t, 3> CURVES = {PROJECTILE_CURVE_LEFT, PROJECTILE_CURVE_RIGHT,
PROJECTILE_SINE_VELOCITY};
for (int i = 0; i < 3; i++)
{
Vector shot = shots[i];
if (vector_length_squared(shot) < SPLIT_SLOW_SPEED_SQUARED) shot = vector_scale(shot, SPLIT_SPEED_BOOST);
Entity_Projectile* child = child_spawn(*this, {.position = vector_sub(pos, velocity),
.velocity = shot,
.type = type,
.variant = variant,
.seed = Random()});
if (!child) continue;
child->height = std::min(height * SPLIT_HEIGHT_SHARE, HEIGHT_START);
child->scale = std::max(scale - SPLIT_SCALE_LOSS, 1.0f);
if (scale > 1)
{
child->fallingAccel = fallingAccel;
random_unit();
}
else
{
projectileFlags &= ~PROJECTILE_BURST3;
child->fallingAccel = SPLIT_FALL_ACCEL;
child->fallingSpeed = SPLIT_FALL_SPEED;
}
child->projectileFlags = (projectileFlags & ~BURST3_CHILD_CLEARED) | CURVES[i];
child->SetColor(sprite.color, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
child->parent = parent.get();
child->Update();
}
Kill(EntityRef());
}
// Its fall by its speed and gravity. In a crawlspace it drops under gravity and is removed once it heads out of the
// room.
void Entity_Projectile::fall_update()
{
if (is_any_player_holding(COLLECTIBLE_CIRCLE_OF_PROTECTION))
{
RNG rng;
rng.SetSeed(initSeed, CIRCLE_RNG_SHIFT);
if ((int)rng.RandomInt(CIRCLE_CHANCE) <= CIRCLE_PROC_MAX) circle_reflect();
}
fallingSpeed = speedMultiplier * fallingAccel + speedMultiplier * GRAVITY_PULL +
timescale_factor(FALL_DRAG, speedMultiplier) * fallingSpeed;
height = fallingSpeed * speedMultiplier + height;
if (variant == PROJECTILE_HEAD) sprite.rotation = angle_wrap(sprite.rotation + fallingSpeed * HEAD_SPIN);
if (!is_crawlspace()) return;
if (fallingAccel >= CRAWLSPACE_MIN_ACCEL) velocity.y = fallingAccel * speedMultiplier + velocity.y;
else if (height > CRAWLSPACE_SINK_HEIGHT) velocity.y = speedMultiplier * CRAWLSPACE_SINK_SPEED + velocity.y;
Room& room = *g_game->room;
Vector clamped = room.GetClampedPositionImpl(pos, CRAWLSPACE_MARGIN_TOP, CRAWLSPACE_MARGIN, CRAWLSPACE_MARGIN,
CRAWLSPACE_MARGIN);
if ((clamped.x != pos.x || clamped.y != pos.y) && vector_dot(vector_sub(clamped, pos), velocity) < 0) Remove();
if (room.IsBeastDungeon() && FrameCount() > 2 && velocity.y > 0 && pos.y > room.get_lava_line_y()) lava_sink();
}
// Circle of Protection: a shot inside a player's light ring becomes that player's homing tear (a plain one's without
// a player), flying back the way it came at its height and fall, purple.
void Entity_Projectile::circle_reflect()
{
constexpr int LIGHT_RING_CIRCLE = 3;
constexpr ColorMod CIRCLE_TEAR_COLOR = {{0.4f, 0.15f, 0.38f, 1}, {0, 0, 0, 0}, {0.2784314f, 0, 0.454902f}};
for (Entity* entity : entity_query(ENTITY_LASER, LASER_LIGHT_RING))
{
auto& ring = *static_cast<Entity_Laser*>(entity);
Vector offset = vector_sub(pos, ring.pos);
if (ring.subtype != LIGHT_RING_CIRCLE || vector_length_squared(offset) >= ring.radius * ring.radius) continue;
Remove();
Entity* spawner = ring.spawnerEntity;
auto* player = spawner && spawner->is_player() ? static_cast<Entity_Player*>(spawner) : nullptr;
TearParams params =
player ? player->GetTearHitParams(WEAPON_TEARS, 1, random_below(2) * 2 - 1, nullptr) : TearParams{};
auto* tear = static_cast<Entity_Tear*>(g_game->Spawn({.position = pos,
.velocity = vector_scale(velocity, -1),
.spawner = player,
.type = ENTITY_TEAR,
.seed = Random()}));
tear->SetHeight(height);
tear->fallingSpeed = fallingSpeed;
tear->SetCollisionDamage(params.tearDamage);
tear->SetTearFlags(TearFlags().set(TEAR_HOMING));
tear->SetScale(params.tearScale);
tear->fallingAccel = fallingAccel;
tear->SetColor(CIRCLE_TEAR_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
tear->parent = player;
tear->Update();
}
}
// The Beast's lava swallows a falling shot past its line with a splash (a big one with a sound for the Beast's lava
// ball). An exploding shot dies there instead.
void Entity_Projectile::lava_sink()
{
if (projectileFlags & PROJECTILE_EXPLODE)
{
isDead = true;
return;
}
Remove();
bool isLavaBall = variant == PROJECTILE_LAVA && spawnerType == ENTITY_BEAST;
if (isLavaBall) npc_sound_play(SOUND_BEAST_LAVA_BALL_SPLASH, 1, 2, 1);
int splash = isLavaBall ? POOF02_LAVA_SPLASH_LARGE : POOF02_LAVA_SPLASH;
effect_spawn(EFFECT_POOF02, pos, nullptr, splash)->Update();
}
// The rest of its frame: the slowdown's tint, the timed changes, the bounces, its height's collision, The Soul's
// push, its landing, the Broken Modem's freeze, the fade out and the hover.
void Entity_Projectile::late_update()
{
Room& room = *g_game->room;
bool isSlowedLook = projectileFlags & PROJECTILE_SLOWED;
if (!isSlowedLook && is_friendly_shot(projectileFlags)) isSlowedLook = room.HasSlowDown();
else if (!isSlowedLook && (!parent || !parent->IsFriendly()))
isSlowedLook = room.temporaryEffects.HasCollectibleEffect(COLLECTIBLE_HOURGLASS) ||
room.temporaryEffects.HasCollectibleEffect(COLLECTIBLE_STOP_WATCH) ||
room.GetBrokenWatchState() == BROKEN_WATCH_SLOW;
if (isSlowedLook)
{
ColorMod slowed = SLOWED_COLOR;
color_multiply(slowed, color);
SetColor(slowed, SLOWED_COLOR_FRAMES, COLOR_PRIORITY, false, true);
}
auto is_change_due = [this]()
{ return changeTimeout < 1 || GetIntervalTicks((float)changeTimeout, 0) != 0; };
if ((projectileFlags & PROJECTILE_CHANGE_VELOCITY_AFTER_TIMEOUT) && is_change_due() && FrameCount() > 0)
{
velocity = vector_resized(velocity, changeVelocity);
if ((changeFlags & CHANGE_BOTH_FLAGS) != CHANGE_BOTH_FLAGS)
projectileFlags &= ~PROJECTILE_CHANGE_VELOCITY_AFTER_TIMEOUT;
}
ground_update();
if ((projectileFlags & PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT) && FrameCount() > 0 && is_change_due())
std::swap(projectileFlags, changeFlags);
update_height_collision();
if (update_ticks_get() > 0 && random_below(SOUL_CHANCE) == 0 && is_any_player_holding(COLLECTIBLE_SOUL))
{
Entity_Player* nearest = nullptr;
float best = SOUL_DISTANCE_START;
for (Entity_Player* player : g_game->playerManager.playerList)
{
float distance = vector_length_squared(vector_sub(player->pos, pos));
if (player->HasCollectible(COLLECTIBLE_SOUL) && distance < best)
{
nearest = player;
best = distance;
}
}
if (nearest && best < SOUL_REACH * SOUL_REACH)
{
float push = random_unit() * (SOUL_REACH - std::sqrt(best)) * SOUL_PUSH + SOUL_PUSH_MIN;
velocity = vector_add(velocity, vector_resized(vector_sub(pos, nearest->pos), push));
}
}
if (height > FLOOR_HEIGHT || (isCollidesWithGrid && height >= HIGH_HEIGHT)) isDead = true;
if (is_any_player_holding(COLLECTIBLE_BROKEN_MODEM))
{
int luck = (int)g_game->playerManager.get_total_luck();
if (random_below(std::max(MODEM_CHANCE_BASE - luck, MODEM_CHANCE_MIN)) == 0 && !isDead)
{
modemPosition = pos;
AddEntityFlags(FLAG_FREEZE);
velocity = {};
}
}
if (projectileFlags & PROJECTILE_FADEOUT)
{
entityCollisionClass = ENTCOLL_NONE;
sprite.color._tint[3] -= speedMultiplier * FADEOUT_RATE;
if (sprite.color._tint[3] <= 0) Remove();
}
if (!(projectileFlags & PROJECTILE_ANTI_GRAVITY)) return;
height = lerp(height, HIGH_HEIGHT, ANTI_GRAVITY_RATE);
fallingSpeed = 0;
posOffset.y = std::sin(updateTime * ANTI_GRAVITY_RATE) * ANTI_GRAVITY_BOB + height;
if (!parent) isDead = true;
}
// The bounces: off the floor (or what stands on it) with BOUNCE_FLOOR, and off the walls its collision points
// touch with BOUNCE, the walls alone when it flies high.
void Entity_Projectile::ground_update()
{
if (projectileFlags & PROJECTILE_BOUNCE_FLOOR)
{
uint32_t collision = room_grid_collision_at(pos);
bool isBlocked = collision == COLLISION_OBJECT || collision == COLLISION_SOLID;
float floor = isBlocked ? HIGH_HEIGHT : FLOOR_HEIGHT;
if ((isBlocked || collision == COLLISION_NONE) && height > floor && fallingSpeed > BOUNCE_FLOOR_SPEED)
{
height = floor;
fallingSpeed *= BOUNCE_FLOOR_REBOUND;
}
}
if (!(projectileFlags & PROJECTILE_BOUNCE)) return;
Vector normal{};
uint32_t threshold = height < HIGH_HEIGHT ? COLLISION_WALL : COLLISION_OBJECT;
for (const Vector& point : collisionPoints)
if (room_grid_collision_at(vector_add(pos, point)) >= threshold) normal = vector_add(normal, point);
if (vector_is_zero(normal)) return;
normal = vector_normalized(normal);
float into = -vector_dot(normal, velocity);
if (into >= 0) return;
velocity = vector_add(velocity, vector_scale(normal, into * 2));
pos = vector_add(pos, velocity);
}
// 0065c510 Moves it half a step for the frame between updates, with its wiggles and tractor beam, resolves the grid
// and places its sprite at the height it will fall to.
void Entity_Projectile::Interpolate()
{
if (HasEntityFlags(FLAG_NO_INTERPOLATE))
{
ClearEntityFlags(FLAG_NO_INTERPOLATE);
return;
}
preInterpolatePos = pos;
pos = vector_add(pos, vector_scale(velocity, friction * speedMultiplier * HALF));
if (motionHook) motionHook(*this, PROJECTILE_STEP_INTERPOLATE);
float wiggleTime = (float)wiggleFrameOffset + updateTime;
if (projectileFlags & PROJECTILE_SINE_VELOCITY)
pos = vector_add(pos, vector_scale(velocity, std::sin(wave_phase(updateTime)) * HALF));
if (projectileFlags & PROJECTILE_WIGGLE)
{
float speed = spawnerType == ENTITY_MOTHER ? 2 : 1;
float wave = std::cos(wave_phase(wiggleTime * speed)) * WIGGLE_SIZE * HALF;
pos = vector_add(pos, vector_scale(wiggle_side(velocity), wave));
}
if (projectileFlags & PROJECTILE_MEGA_WIGGLE)
{
float size = std::min(wiggleTime * MEGA_WIGGLE_GROWTH, MEGA_WIGGLE_GROWN) + MEGA_WIGGLE_BASE;
pos = vector_add(pos, vector_scale(wiggle_side(velocity), std::cos(wave_phase(wiggleTime)) * size * HALF));
}
if ((projectileFlags & PROJECTILE_TRACTOR_BEAM) && spawnerEntity && !vector_is_zero(spawnerEntity->targetPosition))
{
float beamLength = vector_length(spawnerEntity->targetPosition);
Vector direction = vector_scale(spawnerEntity->targetPosition, 1 / beamLength);
Vector offset = vector_sub(pos, spawnerEntity->pos);
float along = vector_dot(offset, direction) < 0 ? -vector_length(offset) : vector_length(offset);
if (along >= 0 && along <= beamLength)
{
float speed = vector_length(velocity);
pos = vector_add(spawnerEntity->pos, vector_scale(direction, along));
velocity = vector_scale(direction, speed);
}
}
isInterpolating = true;
CollideWithGrid(GRID_COLLISION_SIMULATE);
if (is_crawlspace())
{
posOffset.y = CRAWLSPACE_OFFSET_Y;
return;
}
float next = (projectileFlags & PROJECTILE_STASIS) ? (HEIGHT_START - height) * ANTI_GRAVITY_RATE
: fallingSpeed * speedMultiplier * HALF;
next += height;
if (fallingAccel < 0)
{
float aboveFloor = std::max(next + HOVER_FLOOR_DISTANCE, 0.0f);
next = next * HALF + HOVER_OFFSET_LIFT + aboveFloor * aboveFloor;
}
if (projectileFlags & PROJECTILE_ANTI_GRAVITY)
next = std::sin((speedMultiplier * HALF + updateTime) * ANTI_GRAVITY_RATE) * ANTI_GRAVITY_BOB + height;
posOffset.y = next;
}
// 0065cb60 Draws the shot, at the floor when it is rendered at the ground. A shot the Broken Modem froze draws
// where it froze, scaled down and glitching.
void Entity_Projectile::Render(const Vector& offset)
{
if (isRenderedAtGround) posOffset.y -= height;
bool isModemFrozen = HasEntityFlags(FLAG_FREEZE) && !vector_is_zero(modemPosition);
if (!isModemFrozen) Entity::Render(offset);
else
{
float pixelScale = g_displayPixelRatio * g_screenPointScale;
auto screen_get = [pixelScale](float world, float origin, float screenSize)
{
float screen = ((world - origin) * MODEM_SCREEN_SCALE + screenSize * HALF) * pixelScale + HALF;
return std::floor(screen) / pixelScale;
};
Vector frozen = vector_add(modemPosition, posOffset);
renderPosition.x = screen_get(frozen.x, MODEM_ORIGIN_X, g_screenWidth - MODEM_SCREEN_WIDTH) + offset.x;
renderPosition.y = screen_get(frozen.y, MODEM_ORIGIN_Y, g_screenHeight - MODEM_SCREEN_HEIGHT) + offset.y;
RNG rng = MODEM_RNG;
rng._seed = (uint32_t)(g_game->frameCount / MODEM_SEED_FRAMES) * MODEM_SEED_STEP + 1;
SpriteDraw draw = {renderPosition, {}, {}};
draw.topLeftClamp.y = rng.RandomFloat() * MODEM_CLAMP_MAX;
draw.topLeftClamp.x = rng.RandomFloat() * MODEM_CLAMP_MAX;
draw.bottomRightClamp.y = rng.RandomFloat() * MODEM_CLAMP_MAX;
draw.bottomRightClamp.x = rng.RandomFloat() * MODEM_CLAMP_MAX;
sprite.Render(draw);
}
if (isRenderedAtGround) posOffset.y += height;
lua_engine_get().PostProjectileRender(this, offset);
}
// 0065b500 Places its sprite at its height (lifted on a curve near the floor when it rises) and lets it hit
// entities and the grid while low. A crawlspace keeps it at a fixed height and lets it hit everything.
void Entity_Projectile::update_height_collision()
{
bool isGridPassing = projectileFlags & (GRID_PASS_FLAGS | PROJECTILE_BOUNCE);
if (is_crawlspace())
{
posOffset.y = CRAWLSPACE_OFFSET_Y;
if (height > CRAWLSPACE_HEIGHT_MAX) height = CRAWLSPACE_HEIGHT_MAX;
entityCollisionClass = ENTCOLL_ALL;
bool isGridHit = !isGridPassing || (projectileFlags & PROJECTILE_EXPLODE);
entityGridCollisionClass = isGridHit ? GRIDCOLL_BULLET : GRIDCOLL_NONE;
if (g_game->room->IsBeastDungeon()) entityGridCollisionClass = GRIDCOLL_NONE;
}
else
{
float offsetY = height;
if (fallingAccel < 0)
{
float aboveFloor = std::max(height + HOVER_FLOOR_DISTANCE, 0.0f);
offsetY = height * HALF + HOVER_OFFSET_LIFT + aboveFloor * aboveFloor;
}
posOffset.y = offsetY;
if (height >= HIGH_HEIGHT)
{
entityCollisionClass = ENTCOLL_ALL;
entityGridCollisionClass = isGridPassing ? GRIDCOLL_NONE : GRIDCOLL_BULLET;
}
else if (!(projectileFlags & PROJECTILE_ANY_HEIGHT_ENTITY_HIT)) entityCollisionClass = ENTCOLL_NONE;
}
if (HasEntityFlags(FLAG_FREEZE)) entityCollisionClass = ENTCOLL_NONE;
}
// 0065b670 Hits what it touches: a player above its reach (unless a hat or Infamy blocks it), an enemy when it is
// friendly, and the familiars that take hits. Singe's ball, frozen enemies and poop break it. Whether the collision
// is ignored. The Beast's lava ball never stops a collision.
bool Entity_Projectile::HandleCollision(Entity* collider, bool isLow)
{
LuaResult luaResult = lua_engine_get().PreProjectileCollision(this, collider, isLow);
if (luaResult == LUA_RESULT_TRUE) return true;
if (luaResult == LUA_RESULT_FALSE) return false;
bool isLava = variant == PROJECTILE_LAVA;
if (isLow)
{
if (collider->type == ENTITY_SINGE && collider->variant == SINGE_BALL)
{
isDead = true;
if (collider->friction == 0) return isLava;
Vector push = vector_scale(velocity, SINGE_PUSH * collider->speedMultiplier / collider->friction);
collider->velocity = vector_add(collider->velocity, push);
return isLava;
}
if (collider->IsEnemy() && collider->HasEntityFlags(FLAG_ICE_FROZEN))
{
collider->TakeDamage(1, 0, EntityRef(), HIT_COUNTDOWN);
isDead = true;
return false;
}
if (collider->type == ENTITY_POOP)
{
collider->TakeDamage(1, DAMAGE_COUNTDOWN, EntityRef(this), HIT_COUNTDOWN);
isDead = true;
return false;
}
}
if (spawnerType == ENTITY_DARK_ESAU)
{
if (collider->type == ENTITY_FAMILIAR) return true;
if (collider->type == ENTITY_PLAYER && spawnerEntity && spawnerEntity->spawnerEntity.get() != collider)
return true;
}
if (!isLow && collider->type == ENTITY_PLAYER) return player_hit(*static_cast<Entity_Player*>(collider));
if (isLow && (collider->IsVulnerableEnemy(nullptr) || collider->type == ENTITY_DARK_ESAU))
{
bool isOwnHit = parent && parent->GetHitListIndex() == collider->GetHitListIndex();
if (!isOwnHit) return enemy_hit(collider);
bool isMotherBall = parent->type == ENTITY_MOTHER && parent->variant == MOTHER_BALL;
if (isMotherBall && g_game->frameCount - spawnFrame > MOTHER_SELF_HIT_FRAMES) return enemy_hit(collider);
return isLava;
}
if (isLow) return isLava;
return familiar_hit(collider);
}
// A player hit: friendly shots spare players their shooter is friendly with, a shrunk player lets high shots pass,
// a hat blocks falling ones and Infamy some, then it hurts. Greed's shots take coins, frozen ones stop the player.
bool Entity_Projectile::player_hit(Entity_Player& player)
{
if (projectileFlags & PROJECTILE_CANT_HIT_PLAYER)
{
Entity* shooter = spawnerEntity;
if (!shooter || !shooter->HasEntityFlags(FLAG_FRIENDLY | FLAG_CHARM)) return true;
if (shooter->IsDamageSourceFriendly(EntityRef(&player))) return true;
}
if (g_game->deathmatchManager.is_active() && IsDescendantOf(&player)) return true;
if (player.isCoopGhost) return true;
float playerSize = std::clamp(player.sprite.scale.y, PLAYER_HIT_SCALE_MIN, 1.0f);
if (posOffset.y < playerSize * HIGH_HEIGHT) return true;
isDead = true;
bool isDamaged = false;
bool isHatBlocked = false;
if (player.HasCollectible(COLLECTIBLE_SPELUNKER_HAT) || player.HasCollectible(COLLECTIBLE_HOST_HAT))
{
if (is_crawlspace()) isHatBlocked = vector_normalized(vector_sub(pos, player.pos)).x < HAT_CRAWLSPACE_SIDE;
else isHatBlocked = fallingAccel > 0 && height - fallingSpeed * speedMultiplier < HIGH_HEIGHT;
}
if (!(projectileFlags & PROJECTILE_EXPLODE) && !isHatBlocked)
{
int chance = g_game->deathmatchManager.is_active() ? 4 : 2;
if (!player.HasCollectible(COLLECTIBLE_INFAMY) || random_below(chance) != 0)
{
uint64_t damageFlags = (projectileFlags & PROJECTILE_FIRE_FLAG) ? DAMAGE_FIRE : 0;
isDamaged = player.TakeDamage(collisionDamage, damageFlags, EntityRef(this), HIT_COUNTDOWN);
}
else
{
sound_play(SOUND_INFAMY_BLOCK, 1, 1);
player.SetColor(INFAMY_COLOR, INFAMY_COLOR_FRAMES, COLOR_PRIORITY, true, true);
}
}
if ((projectileFlags & PROJECTILE_GREED) && isDamaged && player.numCoins > 0)
{
player.AddCoins(-1);
int dropped = std::min((int)(Random() % GREED_COINS_MAX) + 1, player.numCoins);
for (int i = 0; i < dropped; i++)
{
uint32_t seed = Random();
Vector direction = random_direction();
float speed = random_unit() * GREED_COIN_SPEED + GREED_COIN_SPEED_MIN;
g_game->Spawn({.position = pos,
.velocity = vector_scale(direction, speed),
.subType = COIN_PENNY,
.type = ENTITY_PICKUP,
.variant = PICKUP_COIN,
.seed = seed});
}
player.AddCoins(-dropped);
}
if (projectileFlags & PROJECTILE_FREEZE)
{
player.controlsCooldown = FROZEN_FRAMES;
player.SetColor(FROZEN_COLOR, FROZEN_FRAMES, COLOR_PRIORITY_NONE, true, true);
}
return isDead ? variant == PROJECTILE_LAVA : true;
}
// An enemy hit: only a friendly shot (or any shot at a friendly or baited enemy) hurts it, harder than a player,
// and a frozen shot freezes it. Mother's and Mega Satan's shots pass their own kind.
bool Entity_Projectile::enemy_hit(Entity* collider)
{
bool isLava = variant == PROJECTILE_LAVA;
if ((projectileFlags & PROJECTILE_CANT_HIT_PLAYER) &&
(collider->HasEntityFlags(FLAG_FRIENDLY) || collider->type == ENTITY_DARK_ESAU))
return true;
bool isTargetable = collider->HasEntityFlags(FLAG_BAITED | FLAG_FRIENDLY) || collider->type == ENTITY_DARK_ESAU;
if (!(projectileFlags & PROJECTILE_HIT_ENEMIES) && !isTargetable) return true;
if (parent && parent->type == ENTITY_MOTHER)
{
if (parent->variant == MOTHER_BALL && collider->type == ENTITY_MOTHER && collider->variant == MOTHER_BALL)
{
isDead = true;
return isLava;
}
}
else if (parent && (parent->type == ENTITY_MEGA_SATAN || parent->type == ENTITY_MEGA_SATAN_2))
{
if (collider->type == ENTITY_MEGA_SATAN || collider->type == ENTITY_MEGA_SATAN_2) return isLava;
}
isDead = true;
if (projectileFlags & PROJECTILE_EXPLODE) return isLava;
float hitDamage = variant == PROJECTILE_GRID ? GRID_HIT_DAMAGE : collisionDamage + ENEMY_HIT_BONUS;
collider->TakeDamage(hitDamage, 0, EntityRef(this), HIT_COUNTDOWN);
if (projectileFlags & PROJECTILE_FREEZE) collider->AddFreeze(EntityRef(this), FREEZE_DURATION, false);
return isLava;
}
// A familiar hit: the familiars that take shots are hurt unless the shot is friendly. Infamy guards a player-like
// familiar as it does the player.
bool Entity_Projectile::familiar_hit(Entity* collider)
{
bool isLava = variant == PROJECTILE_LAVA;
if (collider->type != ENTITY_FAMILIAR || !is_listed(HURT_FAMILIARS, collider->variant)) return isLava;
if (collider->variant == SPARED_FAMILIAR && collider->subtype == SPARED_FAMILIAR_SUBTYPE) return isLava;
if (projectileFlags & PROJECTILE_CANT_HIT_PLAYER) return true;
isDead = true;
int chance = g_game->deathmatchManager.is_active() ? 4 : 2;
Entity_Player* player = static_cast<Entity_Familiar*>(collider)->player;
bool isInfamyBlocked = is_listed(INFAMY_FAMILIARS, collider->variant) && player &&
player->HasCollectible(COLLECTIBLE_INFAMY) && random_below(chance) == 0;
if ((projectileFlags & PROJECTILE_EXPLODE) || isInfamyBlocked) return isLava;
collider->TakeDamage(collisionDamage, 0, EntityRef(this), HIT_COUNTDOWN);
return false;
}
// 00652f60 Only a Globin's meat shot holds the doors shut, unless it cannot hit players.
bool Entity_Projectile::CanShutDoors()
{
return variant == PROJECTILE_MEAT && !(projectileFlags & PROJECTILE_CANT_HIT_PLAYER);
}
// 00456420 Raises it.
void Entity_Projectile::AddHeight(float amount)
{
height = amount + height;
}
// 00456450 Speeds its fall.
void Entity_Projectile::AddFallingSpeed(float amount)
{
fallingSpeed = amount + fallingSpeed;
}
// 00456470 Strengthens its gravity.
void Entity_Projectile::AddFallingAccel(float amount)
{
fallingAccel = amount + fallingAccel;
}
// 00456490 Sets projectile flags.
void Entity_Projectile::AddProjectileFlags(uint64_t added)
{
projectileFlags |= added;
}
// 006b82f0 Sets the flags it takes when its change timeout ends.
void Entity_Projectile::AddChangeFlags(uint64_t added)
{
changeFlags |= added;
}
// 0085e320 Grows it.
void Entity_Projectile::AddScale(float amount)
{
scale = amount + scale;
}
// 00652fa0 Turns it on the enemies at a new velocity: it stops homing, orbiting, curving and changing its flags,
// hits only enemies, leaves its parent and lives on, bouncing back up if it was falling.
void Entity_Projectile::Reflect(const Vector& newVelocity)
{
constexpr uint64_t CLEARED = PROJECTILE_SMART | PROJECTILE_ORBIT_CW | PROJECTILE_ORBIT_CCW |
PROJECTILE_CURVE_LEFT | PROJECTILE_CURVE_RIGHT | PROJECTILE_TURN_HORIZONTAL |
PROJECTILE_MOVE_TO_PARENT | PROJECTILE_CHANGE_FLAGS_AFTER_TIMEOUT;
projectileFlags = (projectileFlags & ~CLEARED) | FRIENDLY_FLAGS;
changeFlags = (changeFlags & ~CLEARED) | FRIENDLY_FLAGS;
velocity = newVelocity;
parent = nullptr;
isDead = false;
if (fallingAccel > 0 && fallingSpeed > 0) fallingSpeed = -fallingSpeed * REFLECT_REBOUND;
}
// 00652f80 Kills it without the effects of its flags.
void Entity_Projectile::die_without_flags()
{
projectileFlags = 0;
isDead = true;
}
// 006cb010 Hands a fired shot what its shooter carries: the hits a charmed, friendly or baited shooter's shots deal
// (and, with Betrayal held, any enemy's), the parent friendly shots keep, and a champion's color, damage and size.
void Entity_Projectile::apply_spawn_flags(const ProjectileShooter& shooter)
{
if (shooter.flags & FLAG_SLOW) projectileFlags |= PROJECTILE_SLOWED;
if (shooter.flags & FLAG_CHARM) projectileFlags |= PROJECTILE_HIT_ENEMIES;
if (shooter.flags & FLAG_FRIENDLY) projectileFlags |= PROJECTILE_CANT_HIT_PLAYER;
if (Entity* entity = shooter.entity)
{
bool isEnemyShooter = (g_game->deathmatchManager.is_active() && entity->type == ENTITY_PLAYER) ||
Entity::is_enemy_type(entity->type);
if (isEnemyShooter &&
g_game->playerManager.FirstCollectibleOwner(COLLECTIBLE_BETRAYAL, LAZARUS_SHARE_CHECK))
projectileFlags |= PROJECTILE_HIT_ENEMIES;
if ((projectileFlags & PROJECTILE_HIT_ENEMIES) || (shooter.flags & FLAG_BAITED)) parent = entity;
}
float sizeScale = 1;
if (shooter.championColor >= 0)
{
if (variant == PROJECTILE_BONE)
{
ColorMod tinted = color;
color_multiply(tinted, champion_sprite_color_get(shooter.championColor));
SetColor(tinted, COLOR_PRIORITY_NONE, COLOR_PRIORITY, false, true);
}
if (shooter.championColor != CHAMPION_TINY)
{
SetCollisionDamage(std::min(collisionDamage + collisionDamage, CHAMPION_DAMAGE_MAX));
sizeScale = CHAMPION_SHOT_SCALE;
if (shooter.championColor == CHAMPION_ORANGE) projectileFlags |= PROJECTILE_GREED;
else if (shooter.championColor == CHAMPION_TRANSPARENT) projectileFlags |= PROJECTILE_GHOST;
}
}
sizeScale *= shooter.scale;
if (shooter.flags & FLAG_WEAKNESS) sizeScale *= WEAK_SHOT_SCALE;
if (sizeScale != 1) scale = sizeScale * scale;
}