Files
isaac-port/src/familiar_dip.cpp
T

396 lines
17 KiB
C++

#include "entity_effect.h"
#include "entity_familiar.h"
#include "entity_npc.h"
#include "entity_player.h"
#include "game.h"
#include "grid_entity_poop.h"
#include "npc_ai.h"
#include "platform.h"
#include "room_grid.h"
// A player keeps at most this many Dips, the first dip subtype a thrown poop turns into a plain Dip, and the last.
constexpr int DIP_OWNED_LIMIT = 32;
constexpr int DIP_PLAIN_FROM = POOP_GIGA_TOP_LEFT;
constexpr int DIP_PLAIN_TO = POOP_CHARMING;
// How much a Dip is worth keeping by subtype, above how long it has lived: corn first to go, the rarer ones last.
constexpr float DIP_KEEP_SCORES[] = {0, 10000, -200, 20000, 20000, 10000, 20000, 0, 0, 0, 0, 0, 10000, 10000, 10000};
constexpr float DIP_KEEP_SCORE_MAX = 1e10f;
// A Dip thrown toward a spot: how far around a free spot it lands, its speed toward it and its lift.
constexpr float DIP_SPOT_DISTANCE = 80;
constexpr float DIP_LANDING_SPREAD = 20;
constexpr float DIP_THROW_SPEED = 0.04f;
constexpr float DIP_THROW_LIFT = -8;
// The share of a versus player's color a Dip's glow takes, and the color slot of a player without one.
constexpr float DIP_VERSUS_GLOW = 0.25f;
constexpr int DIP_VERSUS_NO_SLOT = 5;
// A Dip's states: idling, scooting, and thrown.
enum DipState
{
DIP_STATE_IDLE,
DIP_STATE_MOVING,
DIP_STATE_THROWN
};
// A Dip's scoots: their odds by subtype (one in ten, Stone one in twenty, Flaming one in three), their speed, how far
// around its player it wanders, the red one's charge, the golden one's slowness, and the drag of each state.
constexpr uint32_t DIP_SCOOT_ODDS = 10;
constexpr uint32_t DIP_SCOOT_ODDS_STONE = 20;
constexpr uint32_t DIP_SCOOT_ODDS_FLAMING = 3;
constexpr float DIP_SCOOT_SPEED = 5;
constexpr float DIP_WANDER = 80;
constexpr float DIP_RED_CHARGE = 2;
constexpr float DIP_GOLDEN_SLOWNESS = 0.8f;
constexpr float DIP_IDLE_DRAG = 0.8f;
constexpr float DIP_MOVING_DRAG = 0.95f;
constexpr int DIP_PATH_THRESHOLD = 999;
// The enemies a Dip looks for: the red one far and often, the others near and seldom, and on landing.
constexpr TargetSearch DIP_RED_SEARCH = {.range = 240, .frameInterval = 4};
constexpr TargetSearch DIP_NEAR_SEARCH = {.range = 40, .frameInterval = 4};
constexpr TargetSearch DIP_LANDING_SEARCH = {.range = 80};
constexpr uint32_t DIP_RED_SEARCH_ODDS = 3;
constexpr uint32_t DIP_NEAR_SEARCH_ODDS = 10;
// A thrown Dip's fall and the heights its grid collision changes at.
constexpr float DIP_FALL = 0.7f;
constexpr float DIP_HIGH = -26;
constexpr float DIP_LOW = -3;
// The explosions a Dip shrugs off past 3.5, its splats, the red one's regrowth and lifetime, the rainbow one's colors,
// the white one's aura (reach, size, height and sorting), the black one's creep and the stinky one's gas.
constexpr float DIP_EXPLOSION_CAP = 3.5f;
constexpr uint32_t DIP_SPLAT_ODDS = 20;
constexpr float DIP_SPLAT_MIN = 0.2f;
constexpr float DIP_SPLAT_RANGE = 0.5f;
constexpr float DIP_RED_REGROWTH = 0.03f;
constexpr int DIP_RED_LIFETIME = 450;
constexpr uint8_t DIP_RAINBOW_COLORS[][3] = {{154, 40, 45}, {170, 45, 147}, {41, 79, 175},
{39, 171, 177}, {90, 180, 37}, {181, 170, 36}};
constexpr int DIP_AURA_INTERVAL = 3;
constexpr float DIP_AURA_REACH = 23.33f;
constexpr int DIP_AURA_FRAMES = 4;
constexpr uint32_t DIP_AURA_CACHE = 99;
constexpr float DIP_AURA_SCALE = 0.33f;
constexpr float DIP_AURA_HEIGHT = -8;
constexpr int DIP_AURA_DEPTH = 10000;
constexpr int DIP_CREEP_INTERVAL = 4;
constexpr int DIP_CREEP_FRAMES = 90;
constexpr float DIP_CREEP_SCALE = 0.4f;
constexpr int DIP_GAS_INTERVAL = 8;
constexpr uint32_t DIP_GAS_ODDS = 3;
constexpr int DIP_GAS_FRAMES = 60;
constexpr float DIP_GAS_SCALE = 0.5f;
constexpr float DIP_TRAIL_SCALE_RANGE = 0.2f;
// 00639000 Spawns a Dip of a subtype for its owner (the poop variants without a Dip of their own give a plain one),
// first killing the owner's least worth keeping while it has more than 32. In versus it takes its player's color.
Entity* Entity_Familiar::ThrowDip(int subType, const Vector& position, Entity* spawner)
{
while (true)
{
Entity_Familiar* worst = nullptr;
float worstScore = DIP_KEEP_SCORE_MAX;
int count = 0;
for (Entity_Familiar* dip : familiar_query(DIP))
{
if (dip->isDead || dip->player != spawner) continue;
float score =
dip->subtype >= 0 && dip->subtype < (int)std::size(DIP_KEEP_SCORES) ? DIP_KEEP_SCORES[dip->subtype] : 0;
score -= (float)(g_game->frameCount - dip->spawnFrame);
if (score < worstScore)
{
worst = dip;
worstScore = score;
}
count++;
}
if (count <= DIP_OWNED_LIMIT) break;
if (!worst)
{
log_printf(LOG_WARN, "Entity_Familiar::ThrowDip: couldn't find a candidate for removal, remaining: %d\n",
count);
break;
}
worst->Kill(EntityRef());
}
bool isPlain = subType >= DIP_PLAIN_FROM && subType <= DIP_PLAIN_TO;
Entity* dip = g_game->Spawn({.position = position,
.spawner = spawner,
.subType = isPlain ? 0 : subType,
.type = ENTITY_FAMILIAR,
.variant = DIP,
.seed = Random()});
dip->ClearEntityFlags(FLAG_APPEAR);
if (!g_game->deathmatchManager.is_active() || !spawner || spawner->type != ENTITY_PLAYER) return dip;
std::array<int, COOP_SLOT_COUNT>& slots = g_game->playerManager.colorControllers;
auto slot = std::ranges::find(slots, static_cast<Entity_Player*>(spawner)->controllerIndex);
int colorSlot = slot == slots.end() ? DIP_VERSUS_NO_SLOT : (int)(slot - slots.begin());
KColor color = PlayerManager::GetPlayerColorValue(colorSlot);
dip->sprite.color = {{color._red, color._green, color._blue, 1},
{0, 0, 0, 0},
{color._red * DIP_VERSUS_GLOW, color._green * DIP_VERSUS_GLOW, color._blue * DIP_VERSUS_GLOW}};
return dip;
}
// 006393a0 Throws a Dip from a position to a target, or without one to a random spot near a free one 80 away. It
// arcs there, as thrown Dips do.
Entity* spawn_dip_at(int subType, const Vector* position, Entity* spawner, const Vector* targetPosition)
{
Vector target = *targetPosition;
if (vector_is_zero(target))
{
Vector direction = random_direction();
Vector probe =
vector_make(direction.x * DIP_SPOT_DISTANCE + position->x, direction.y * DIP_SPOT_DISTANCE + position->y);
Vector spot = room_free_pickup_position_find(
{.position = probe, .step = DIP_SPOT_DISTANCE, .isAvoidActiveEntities = true});
Vector landing = random_direction();
float spread = random_unit();
target = vector_make(landing.x * spread * DIP_LANDING_SPREAD + spot.x,
landing.y * spread * DIP_LANDING_SPREAD + spot.y);
}
Vector velocity =
vector_make((target.x - position->x) * DIP_THROW_SPEED, (target.y - position->y) * DIP_THROW_SPEED);
Entity_Familiar& dip = *static_cast<Entity_Familiar*>(Entity_Familiar::ThrowDip(subType, *position, spawner));
dip.targetPosition = target;
dip.velocity = velocity;
dip.orbitSpeed = DIP_THROW_LIFT;
dip.orbitDistance = velocity;
dip.state = DIP_STATE_THROWN;
return &dip;
}
// The white Dip's holy aura: every third frame the players beside it are blessed with Hallowed Ground's tears, and it
// keeps the aura effect under itself.
static void dip_aura_update(Entity_Familiar& dip)
{
if (!dip.HasEntityFlags(FLAG_INTERPOLATION_UPDATE) && !g_game->IsPaused() &&
g_game->frameCount % DIP_AURA_INTERVAL == 0)
for (Entity_Player* player : g_game->playerManager.playerList)
{
if (familiar_versus() && !dip.IsDescendantOf(player)) continue;
if (vector_length(vector_sub(dip.pos, player->pos)) >= player->size + DIP_AURA_REACH) continue;
int before = player->hallowedGroundCountdown;
player->hallowedGroundCountdown = std::min(before + DIP_AURA_FRAMES, DIP_AURA_FRAMES);
if ((before > 0) != (player->hallowedGroundCountdown > 0))
{
player->cacheFlags |= DIP_AURA_CACHE;
player->EvaluateItems();
}
}
if (dip.child) return;
auto* aura = static_cast<Entity_Effect*>(g_game->Spawn(
{.position = dip.pos, .type = ENTITY_EFFECT, .variant = EFFECT_HALLOWED_GROUND, .seed = Random()}));
aura->parent = &dip;
aura->isFollowing = true;
aura->sprite.offset = vector_make(0, DIP_AURA_HEIGHT);
aura->parentOffset = vector_sub(aura->pos, dip.pos);
aura->renderZOffset = DIP_AURA_DEPTH;
aura->sprite.scale = vector_make(DIP_AURA_SCALE, DIP_AURA_SCALE);
aura->Update();
dip.child = aura;
}
// A Dip at rest now and then scoots: toward its target (dropped now and then), else around its player, by the grid
// path when the way is blocked. The red one charges its target.
static void dip_scoot(Entity_Familiar& dip, float speedScale)
{
dip.sprite.Play("Idle");
uint32_t odds = dip.subtype == DIP_SUBTYPE_STONE ? DIP_SCOOT_ODDS_STONE
: dip.subtype == DIP_SUBTYPE_FLAMING ? DIP_SCOOT_ODDS_FLAMING
: DIP_SCOOT_ODDS;
if (Random() % odds == 0 && dip.isVisible)
{
if (dip.subtype != DIP_SUBTYPE_RED && dip.target && dip.headFrameDelay == 0 && (Random() & 3) == 0)
dip.target = nullptr;
Entity* goal = dip.target ? dip.target.get() : dip.player;
Vector destination = goal->pos;
if (!room_check_line(dip.pos, goal->pos, 1, DIP_PATH_THRESHOLD))
{
Vector savedVelocity = dip.velocity;
float savedFriction = dip.friction;
dip.pathfinder.Reset();
dip.pathfinder.FindGridPath(goal->pos, 1, 0, DIRECT_PATH_FAR);
destination = vector_add(dip.pos, dip.velocity);
dip.velocity = savedVelocity;
dip.friction = savedFriction;
}
else if (goal == dip.player)
destination = vector_add(destination, vector_scale(random_direction(), random_unit() * DIP_WANDER));
else if (dip.subtype == DIP_SUBTYPE_RED) speedScale = DIP_RED_CHARGE;
Vector heading = vector_is_zero(vector_sub(destination, dip.pos))
? vector_from_angle(random_unit() * PI_ROUGH * 2 * RADIANS_TO_DEGREES)
: vector_normalized(vector_sub(destination, dip.pos));
float speed = random_unit() * DIP_SCOOT_SPEED + DIP_SCOOT_SPEED;
dip.velocity = vector_add(dip.velocity, vector_scale(heading, speed * speedScale / dip.friction));
dip.state = DIP_STATE_MOVING;
}
dip.friction *= DIP_IDLE_DRAG;
}
// A thrown Dip arcs down. Thrown at nothing it bounces where it lands, its grid collision rising with it, and dies in a
// pit; at a spot it flies straight there. Landed, it goes for an enemy near and thuds.
static void dip_fall(Entity_Familiar& dip)
{
dip.sprite.Play("Move");
bool isFree = vector_is_zero(dip.targetPosition);
if (!isFree)
{
dip.entityGridCollisionClass = GRIDCOLL_WALLS;
dip.entityCollisionClass = ENTCOLL_NONE;
dip.velocity = dip.orbitDistance;
}
dip.orbitSpeed += DIP_FALL;
dip.posOffset.y += dip.orbitSpeed;
if (isFree)
dip.entityGridCollisionClass = dip.posOffset.y < DIP_HIGH
? GRIDCOLL_WALLS
: (dip.posOffset.y < DIP_LOW ? GRIDCOLL_NOPITS : GRIDCOLL_GROUND);
if (dip.posOffset.y <= -dip.orbitSpeed) return;
if (isFree)
{
if (room_grid_collision_at(dip.pos) == COLLISION_PIT)
{
g_game->Spawn(
{.position = dip.pos, .subType = 1, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()});
return dip.Remove();
}
if (!dip.target) dip.PickEnemyTarget(DIP_LANDING_SEARCH);
if (dip.target) dip.headFrameDelay = 1;
}
dip.posOffset.y = 0;
dip.orbitSpeed = 0;
dip.targetPosition = {};
dip.state = DIP_STATE_IDLE;
familiar_sound_play(dip.subtype == DIP_SUBTYPE_STONE ? SOUND_STONE_IMPACT : SOUND_SPLATTER);
}
// A Dip's trail as it lives: the black one's creep, the stinky one's gas, the red one's regrowth until it rots.
static void dip_trail(Entity_Familiar& dip)
{
bool isActing = !dip.HasEntityFlags(FLAG_INTERPOLATION_UPDATE) && !g_game->IsPaused();
int tick = g_game->frameCount + (int)dip.index;
if (dip.subtype == DIP_SUBTYPE_RED)
{
if (g_game->frameCount - dip.spawnFrame < DIP_RED_LIFETIME)
dip.health = std::min(dip.health + DIP_RED_REGROWTH, dip.maxHitPoints);
else dip.Kill(EntityRef{});
return;
}
Entity* trail = nullptr;
if (dip.subtype == DIP_SUBTYPE_BLACK && isActing && tick % DIP_CREEP_INTERVAL == 0 && (Random() & 1) == 0)
{
trail = g_game->Spawn({.position = dip.pos,
.spawner = &dip,
.type = ENTITY_EFFECT,
.variant = EFFECT_PLAYER_CREEP_BLACK,
.seed = Random()});
float scale = random_unit() * DIP_TRAIL_SCALE_RANGE + DIP_CREEP_SCALE;
static_cast<Entity_Effect*>(trail)->timeout = DIP_CREEP_FRAMES;
static_cast<Entity_Effect*>(trail)->lifespan = DIP_CREEP_FRAMES;
trail->sprite.scale = vector_make(scale, scale);
}
else if (dip.subtype == DIP_SUBTYPE_STINKY && isActing && tick % DIP_GAS_INTERVAL == 0 &&
Random() % DIP_GAS_ODDS == 0)
{
trail = g_game->Spawn({.position = dip.pos,
.spawner = dip.player,
.type = ENTITY_EFFECT,
.variant = EFFECT_SMOKE_CLOUD,
.seed = Random()});
float scale = random_unit() * DIP_TRAIL_SCALE_RANGE + DIP_GAS_SCALE;
static_cast<Entity_Effect*>(trail)->timeout = DIP_GAS_FRAMES;
static_cast<Entity_Effect*>(trail)->lifespan = DIP_GAS_FRAMES;
trail->sprite.scale = vector_make(scale, scale);
trail->SetCollisionDamage(dip.player->meleeDamage * 0.5f);
}
if (trail) trail->Update();
}
// 006414c0 A Dip's frame (ZHL's misfit name): it shrugs off big blasts, scoots about after enemies or its player,
// arcs down when thrown, splats, and acts on its kind: the red one hunts and regrows until it rots, the golden one is
// slow, the rainbow one shimmers, the white one blesses, the black one leaves creep, the stinky one gas. BFFS! halves
// its hurts.
void Entity_Familiar::zhl_misfit_006414c0()
{
float speedScale = 1;
entityGridCollisionClass = GRIDCOLL_GROUND;
entityCollisionClass = ENTCOLL_ALL;
if (pendingDamage > 0)
{
pendingDamage = 0;
for (DamageEvent& hit : damageEvents)
{
if (hit.damageFlags & DAMAGE_EXPLOSION) hit.damage = std::min(hit.damage, DIP_EXPLOSION_CAP);
pendingDamage += hit.damage;
}
}
switch (subtype)
{
case DIP_SUBTYPE_RED:
if (Random() % DIP_RED_SEARCH_ODDS == 0) PickEnemyTarget(DIP_RED_SEARCH);
break;
case DIP_SUBTYPE_GOLDEN:
speedScale = DIP_GOLDEN_SLOWNESS;
break;
case DIP_SUBTYPE_RAINBOW:
if (!HasEntityFlags(FLAG_INTERPOLATION_UPDATE) && !g_game->IsPaused() && (g_game->frameCount & 1) == 0)
{
const uint8_t* rgb = DIP_RAINBOW_COLORS[Random() % std::size(DIP_RAINBOW_COLORS)];
for (int i = 0; i < 3; i++) splatColor._offset[i] = (float)rgb[i] * COLOR_OFFSET_UNIT;
}
break;
case DIP_SUBTYPE_WHITE:
dip_aura_update(*this);
break;
}
if (subtype != DIP_SUBTYPE_RED && Random() % DIP_NEAR_SEARCH_ODDS == 0) PickEnemyTarget(DIP_NEAR_SEARCH);
if (!target) headFrameDelay = 0;
if (state == DIP_STATE_IDLE) dip_scoot(*this, speedScale);
else if (state == DIP_STATE_MOVING)
{
sprite.Play("Move");
if (vector_length_squared(velocity) < 1) state = DIP_STATE_IDLE;
friction *= DIP_MOVING_DRAG;
}
else if (state == DIP_STATE_THROWN)
{
dip_fall(*this);
if (!isExists) return;
}
if (Random() % DIP_SPLAT_ODDS == 0 && subtype != DIP_SUBTYPE_STONE)
{
Entity* splat = g_game->Spawn(
{.position = pos, .spawner = this, .type = ENTITY_EFFECT, .variant = EFFECT_BLOOD_SPLAT, .seed = Random()});
float size = random_unit() * DIP_SPLAT_RANGE + DIP_SPLAT_MIN;
splat->sprite.scale = vector_make(size, size);
splat->Update();
splat->SetColor(splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
}
if (!isDead) dip_trail(*this);
if (pendingDamage > 0 && player->HasCollectible(COLLECTIBLE_BFFS))
{
pendingDamage = 0;
for (DamageEvent& hit : damageEvents)
{
hit.damage *= 0.5f;
pendingDamage += hit.damage;
}
}
sprite.isFlipX = velocity.x < 0;
}