Files
isaac-port/src/npc_clickety_clack.cpp
T

304 lines
12 KiB
C++

#include "npc_alt.h"
#include "entity_config.h"
#include "entity_player.h"
#include "entity_projectile.h"
#include "grid_entity.h"
#include "path_finder.h"
#include "room.h"
#include "room_grid.h"
#include <algorithm>
#include <array>
// The Clickety Clack walks at 0.75, keeping off the enemies near it (it picks one within 60 to keep off for 30 to 44
// frames once more than 80 from its target), its head glowing. Clutch's Clickety Clacks are its orbitals.
constexpr float CLACK_SPEED = 0.75f;
constexpr float CLACK_FRICTION = 0.97f;
constexpr float CLACK_FLEE_FRICTION = 0.8f;
constexpr float CLACK_LONELY_SQUARED = 6400;
constexpr float CLACK_SPACING = 60;
constexpr uint32_t CLACK_SPACING_TIME_RANGE = 15;
constexpr int CLACK_SPACING_TIME = 30;
constexpr float CLACK_STILL = 0.1f;
constexpr std::array<std::string_view, 4> CLACK_WALKS = {"WalkLeft", "WalkUp", "WalkRight", "WalkDown"};
constexpr int CLACK_HEAD_LAYER = 2;
constexpr ColorMod CLACK_HEAD_COLOR = {{1, 1, 1, 1}, {1.3f, 1, 2, 1}, {0, 0, 0}};
constexpr int CLACK_FIRST_DELAY = -30;
// Killed, it collapses into a pile of bones for 150 frames (or burns with Clutch) and gets up again with 10 health
// (9 after its first time). When the last one standing collapses they all fall for good.
constexpr float CLACK_REVIVE_HEALTH = 10;
constexpr float CLACK_PILE_MASS = 200;
constexpr int CLACK_PILE_FRAMES = 150;
constexpr int CLACK_PILE_COOLDOWN = 30;
constexpr int CLACK_PILE_DELAY = -60;
constexpr int CLACK_FLAME = 99;
constexpr uint64_t CLACK_PILE_FLAGS = FLAG_HIDE_HP_BAR | FLAG_NO_STATUS_EFFECTS | FLAG_NO_TARGET;
constexpr float CLACK_PILE_FRICTION = 0.5f;
constexpr float CLACK_FLAME_REACH_SQUARED = 14400;
constexpr float CLACK_FLAME_EVERY = 2;
constexpr int CLACK_FLAME_COOLDOWN = 45;
// Clutch's commands: the stomp (a ring of eight bones at 10), the stun and the quick revert.
constexpr float CLACK_STOMP_FRICTION = 0.5f;
constexpr float CLACK_STOMP_SPEED = 10;
constexpr float CLACK_STUN_FRICTION = 0.3f;
constexpr int CLUTCH_ORBITING = 6;
constexpr int CLUTCH_REVERT_WAIT = -1000;
constexpr int CLUTCH_DOWN = NPC_STATE_STOMP;
// The Clickety Clack's states: Clutch's orbiting, walking, the stomp, the stun, getting up, the quick revert and the
// pile of bones.
constexpr int CLACK_ORBITING = NPC_STATE_IDLE;
constexpr int CLACK_WALK = NPC_STATE_MOVE;
constexpr int CLACK_STOMP = NPC_STATE_SUICIDE;
constexpr int CLACK_STUNNED = NPC_STATE_STOMP;
constexpr int CLACK_REGEN = NPC_STATE_SUMMON;
constexpr int CLACK_REVERT = NPC_STATE_SUMMON2;
constexpr int CLACK_PILE = NPC_STATE_SPECIAL;
// Standing again: hurt, targeted and moved as usual.
static void clack_stand(Entity_NPC& npc)
{
npc.ClearEntityFlags(CLACK_PILE_FLAGS);
npc.isInvincible = false;
npc.mass = npc.config->mass;
}
// The enemy it keeps off: kept while its time lasts and it stays an enemy, else the first near one of its side once
// it is far from its target.
static Entity* clack_shy_get(Entity_NPC& npc, Vector target)
{
Entity* shy = npc.entityRef.get();
if (shy && shy->IsEnemy()) return shy;
npc.entityRef = nullptr;
if (vector_length_squared(vector_sub(target, npc.pos)) <= CLACK_LONELY_SQUARED) return nullptr;
bool isFriendly = npc.HasEntityFlags(FLAG_FRIENDLY);
for (Entity* other : entity_query_radius(npc.pos, CLACK_SPACING, QUERY_ENEMIES))
{
bool isNpc = other->type >= 10 && other->type <= 999;
bool isVersusPlayer = g_game->deathmatchManager.is_active() && other->type == ENTITY_PLAYER;
if ((!isNpc && !isVersusPlayer) || other == &npc) continue;
if (other->HasEntityFlags(FLAG_FRIENDLY) != isFriendly || other->HasEntityFlags(FLAG_NO_TARGET)) continue;
npc.entityRef = other;
npc.i1 = (int)(Random() % CLACK_SPACING_TIME_RANGE) + CLACK_SPACING_TIME;
return other;
}
return nullptr;
}
// Walking at its target, keeping off its neighbor, or away from what it fears (or from Clutch's target).
static void clack_walk_update(Entity_NPC& npc)
{
PathFinder& path = npc.pathfinder;
if (npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && npc.controllerID == -1)
{
path.EvadeTarget(npc.GetPlayerTarget()->pos, STATUS_OBEYED);
npc.friction *= CLACK_FLEE_FRICTION;
}
else
{
Vector target = npc.CalcTargetPosition(0);
if (npc.i1 < 1 || (npc.i1 -= npc_ticks_get(npc)) < 1) npc.entityRef = nullptr;
Entity_NPC* clutch = npc.ParentNPC();
Entity* shy = nullptr;
if (clutch && npc.i2 == 1) reconstruction_missing("npc_clickety_clack_update 004efb90: holding Clutch's line");
else if (clutch && npc.i2 == 2) shy = npc.GetPlayerTarget();
else shy = clack_shy_get(npc, target);
if (shy)
{
path.EvadeTarget(shy->pos, STATUS_OBEYED);
npc.friction *= CLACK_FLEE_FRICTION;
}
else path.FindGridPath(npc.CalcTargetPosition(0), CLACK_SPEED, GRID_PATH_FREED, DIRECT_PATH_FAR);
}
npc.friction *= CLACK_FRICTION;
if (vector_length(npc.velocity) < CLACK_STILL) npc.sprite.SetFrame(CLACK_WALKS[DIRECTION_DOWN], 0);
else npc.sprite.Play(CLACK_WALKS[vector_to_direction(npc.velocity)]);
}
// Whether any Clickety Clack of its side (not friendly) still stands.
static bool clack_any_standing()
{
for (Entity* other : g_game->room->entityList.updateEL)
{
if (other->type != ENTITY_CLICKETY_CLACK) continue;
if (!other->HasEntityFlags(FLAG_FRIENDLY) && !other->HasEntityFlags(FLAG_NO_TARGET)) return true;
}
return false;
}
// A pile of bones: burning with Clutch, or, alone, ending them all when it was the last standing, then getting up.
// Whether the update goes on.
static bool clack_pile_update(Entity_NPC& npc)
{
npc.friction *= CLACK_PILE_FRICTION;
if (npc.i2 == CLACK_FLAME)
{
if (!npc.sprite.IsPlaying()) npc.sprite.Play("FlameIdle");
Entity_NPC* clutch = npc.ParentNPC();
bool isAiming = npc.i2 == CLACK_FLAME && npc.sprite.animState.IsLoopingAnimation() &&
npc.projectileCooldown < 1 && npc.GetIntervalTicks(CLACK_FLAME_EVERY, 0) && clutch &&
clutch->state == CLUTCH_ORBITING &&
vector_length_squared(vector_sub(npc.pos, npc.GetPlayerTarget()->pos)) < CLACK_FLAME_REACH_SQUARED;
if (isAiming)
{
npc.sprite.Play("FlameShoot");
npc.projectileCooldown = CLACK_FLAME_COOLDOWN;
}
}
if (npc.projectileCooldown > 0) npc.projectileCooldown = std::max(npc.projectileCooldown - npc_ticks_get(npc), 0);
if (npc.parent)
{
reconstruction_missing("npc_clickety_clack_update 004efb90: Clutch's burning bones");
return true;
}
if (npc.sprite.IsEventTriggered("Shoot") && !npc.IsFriendly() && !clack_any_standing())
{
for (Entity* other : g_game->room->entityList.updateEL)
if (other->type == ENTITY_CLICKETY_CLACK && !other->HasEntityFlags(FLAG_FRIENDLY)) other->Kill(EntityRef());
return false;
}
npc.stateFrame -= npc_ticks_get(npc);
if (npc.stateFrame > 0) return true;
npc.sprite.Play("Regen");
npc.state = CLACK_REGEN;
return true;
}
// Clutch's commands: the stomp's ring of bones, the stun and the quick revert, back to orbiting or walking.
static void clack_command_update(Entity_NPC& npc)
{
if (npc.state == CLACK_STOMP)
{
npc.friction *= CLACK_STOMP_FRICTION;
if (npc.sprite.IsEventTriggered("Shoot"))
{
npc_sound_play(SOUND_MONSTER_ROAR_0, 1, 2, 1);
npc_sound_play(SOUND_BLACK_POOF, 1, 2, 1);
npc.MakeGroundPoofAtSelf(COLOR_DEFAULT, 1);
ProjectileParams params;
params.variant = PROJECTILE_BONE;
npc.FireProjectiles(npc.pos, vector_make(CLACK_STOMP_SPEED, 0), PROJECTILES_EIGHT, params);
npc.projectileDelay = CLACK_FIRST_DELAY;
npc.projectileCooldown = 0;
}
if (npc.sprite.IsPlaying()) return;
clack_stand(npc);
npc.state = CLACK_ORBITING;
npc.entityRef = nullptr;
npc.i1 = 0;
return;
}
if (npc.state == CLACK_STUNNED)
{
npc.friction *= CLACK_STUN_FRICTION;
if (!npc.sprite.IsPlaying()) npc.sprite.Play("StunnedLoop");
return;
}
if (npc.state == CLACK_REGEN)
{
npc.friction *= CLACK_PILE_FRICTION;
if (npc.sprite.IsEventTriggered("Shoot")) clack_stand(npc);
}
else
{
npc.friction *= CLACK_STUN_FRICTION;
Entity_NPC* clutch = npc.ParentNPC();
if (npc.sprite.IsEventTriggered("Shoot") && clutch && clutch->type == ENTITY_CLUTCH &&
clutch->state == CLUTCH_ORBITING)
{
clutch->state = NPC_STATE_IDLE;
clutch->stateFrame = CLUTCH_REVERT_WAIT;
}
}
if (npc.sprite.IsPlaying()) return;
clack_stand(npc);
npc.state = CLACK_WALK;
npc.targetPosition = {};
}
// Its head layer: placed by the HeadOffset null while orbiting, else straight, its overlay dropped.
static void clack_head_update(Entity_NPC& npc)
{
LayerState& head = *npc.sprite.GetLayer(1);
const NullFrame* guide = npc.state == CLACK_ORBITING && npc.sprite.animState.animData
? npc.sprite.animState.GetNullFrame("HeadOffset")
: nullptr;
if (npc.state != CLACK_ORBITING && npc.sprite.overlayAnimState.animData)
npc.sprite.overlayAnimState.SetAnimationData(nullptr);
else if (npc.state != CLACK_ORBITING) return;
head.pos = guide ? guide->pos : Vector{};
head.size = guide ? guide->scale : vector_make(1, 1);
head.rotation = guide ? guide->rotation : 0;
}
// Killed: it collapses into its pile of bones (burning with Clutch), unless it was a pile already.
static void clack_collapse(Entity_NPC& npc)
{
Entity_NPC* clutch = npc.ParentNPC();
// The original converts subtype - 1 unsigned, so a first collapse (subtype 0) also gets up with 9.
float firsts = std::min(std::max((float)(uint32_t)(npc.subtype - 1) * 2, 0.0f), 1.0f);
npc.health = clutch ? npc.config->baseHP : CLACK_REVIVE_HEALTH - firsts;
npc.AddEntityFlags(CLACK_PILE_FLAGS);
npc.isDead = false;
npc.RemoveStatusEffects();
npc.mass = CLACK_PILE_MASS;
npc.targetPosition = npc.pos;
npc.isInvincible = true;
npc.state = CLACK_PILE;
npc.projectileCooldown = CLACK_PILE_COOLDOWN;
npc.projectileDelay = CLACK_PILE_DELAY;
if (clutch && clutch->child.get() == &npc)
{
npc.sprite.Play("FlameCollapse");
npc.i2 = CLACK_FLAME;
}
else npc.sprite.Play("Collapse");
npc.stateFrame = CLACK_PILE_FRAMES;
if (!clutch) npc.subtype++;
}
// 004efb90 The Clickety Clack's AI: a skeleton walking at its target that collapses when killed and gets up again
// until the last one falls. Clutch drives its orbitals' stomps, stuns and reverts.
void npc_clickety_clack_update(Entity_NPC& npc)
{
switch (npc.state)
{
case NPC_STATE_INIT:
npc.AddEntityFlags(FLAG_NO_BLOOD_SPLASH);
npc.state = CLACK_WALK;
npc.projectileDelay = CLACK_FIRST_DELAY;
npc.i2 = 0;
if (npc.sprite.layerStates.size() > CLACK_HEAD_LAYER) npc.sprite.GetLayer(CLACK_HEAD_LAYER)->color = CLACK_HEAD_COLOR;
break;
case CLACK_ORBITING:
reconstruction_missing("npc_clickety_clack_update 004efb90: orbiting Clutch");
break;
case CLACK_WALK:
clack_walk_update(npc);
break;
case CLACK_PILE:
if (!clack_pile_update(npc)) return;
break;
case CLACK_STOMP:
case CLACK_STUNNED:
case CLACK_REGEN:
case CLACK_REVERT:
clack_command_update(npc);
break;
}
clack_head_update(npc);
room_path_raise_at(npc.pos, GRID_PATH_FREED);
if (!vector_is_zero(npc.targetPosition)) npc.velocity = vector_sub(npc.targetPosition, npc.pos);
Entity_NPC* clutch = npc.ParentNPC();
bool isBoneLoose = npc.isDead || npc.state != CLACK_PILE || !clutch || clutch->state == CLUTCH_DOWN;
Entity* bone = npc.entityRef.get();
if (isBoneLoose && bone && bone->type == ENTITY_PROJECTILE) bone->isDead = true;
if (npc.isDead && !npc.HasEntityFlags(FLAG_NO_TARGET)) clack_collapse(npc);
}