Files
isaac-port/src/npc_danny.cpp
T

217 lines
9.9 KiB
C++

#include "npc_alt.h"
#include "entity_effect.h"
#include "npc_fireplace.h"
#include "path_finder.h"
#include "room.h"
#include <algorithm>
// Danny (variant 0) throws rocks and rock spiders, Coal Boy (variant 1) coal spiders and coal fires. Their splats are
// pale grey and glowing orange.
constexpr int DANNY_COAL_BOY = 1;
constexpr ColorMod DANNY_SPLAT_COLOR = {{1, 1, 1, 1}, {1, 1, 1, 0.1f}, {0, 0, 0}};
constexpr ColorMod COAL_BOY_SPLAT_COLOR = {{0, 0, 0, 1.1f}, {0, 0, 0, 0}, {1, 0.514f, 0.004f}};
// They walk at 0.6 and recharge over 150 frames (10 to 19 at first, twice as fast while hurt hard), then throw one
// time in 60 a frame (one in 20 more while hurt hard): always within 200 of the target, one time in ten within 400.
// Not with nine spiders (or coal fires) about, nor when Hornfel spawned them.
constexpr float DANNY_SPEED = 0.6f;
constexpr int DANNY_PATH_MARKER = 0;
constexpr float DANNY_WALK_THRESHOLD = 0.1f;
constexpr float DANNY_FLEE_FRICTION = 0.8f;
constexpr float DANNY_THROW_FRICTION = 0.7f;
constexpr int DANNY_RECHARGE = 150;
constexpr int DANNY_FIRST_WAIT = 10;
constexpr uint32_t DANNY_FIRST_WAIT_RANGE = 10;
constexpr float DANNY_HURT_HARD = 6;
constexpr float DANNY_HURT_RECOVERY = 0.11f;
constexpr uint32_t DANNY_THROW_ODDS = 60;
constexpr uint32_t DANNY_HURT_THROW_ODDS = 20;
constexpr float DANNY_NEAR = 200;
constexpr float DANNY_FAR = 400;
constexpr uint32_t DANNY_FAR_ODDS = 10;
constexpr int DANNY_CROWD = 9;
constexpr float DANNY_CONTROLLED_REACH = 1000;
constexpr float DANNY_STEER_AIM = 100;
constexpr uint32_t DANNY_GRUMBLE_ODDS = 3;
constexpr float DANNY_GRUMBLE_VOLUME = 0.8f;
constexpr int DANNY_GRUMBLE_DELAY = 120;
// The throw: up to 30 degrees off its aim, from 4 out, 15 up. Danny throws six rocks, or rock spiders for those of
// its two it lost. Coal Boy throws four, coal spiders for the one it lost and coal fires for the rest. Both leave a
// spray of grit.
constexpr float THROW_SPREAD = 30;
constexpr float THROW_SPREAD_CENTER = 0.5f;
constexpr float THROW_OUT = 4;
constexpr float DANNY_THROW_HEIGHT = -15;
constexpr int DANNY_ROCKS = 6;
constexpr int DANNY_SPIDERS = 2;
constexpr float DANNY_SPIDER_SPEED = 1;
constexpr float DANNY_SPIDER_SPEED_RANGE = 3;
constexpr int COAL_BOY_THROWS = 4;
constexpr int COAL_BOY_SPIDERS = 1;
constexpr float COAL_BOY_THROW_SPEED = 1;
constexpr float COAL_BOY_THROW_SPEED_RANGE = 9;
constexpr int DANNY_GRIT = 5;
constexpr int COAL_BOY_EMBERS = 6;
constexpr int DANNY_GRIT_SUBTYPE = 99;
constexpr float GRIT_SPEED = 2;
constexpr float GRIT_SPEED_RANGE = 4;
constexpr float GRIT_LIFT = 14;
constexpr ColorMod DANNY_GRIT_COLOR = {{0.54901963f, 0.48235294f, 0.48235294f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float DANNY_THROW_PITCH = 1.1f;
constexpr int DANNY_THROW_DELAY = 10;
// Danny's states: walking and throwing.
constexpr int DANNY_WALK = NPC_STATE_MOVE;
constexpr int DANNY_THROW = NPC_STATE_ATTACK;
// A direction up to 15 degrees either side of the aim.
static Vector throw_direction(Vector aim)
{
return vector_rotated(aim, (random_unit() - THROW_SPREAD_CENTER) * THROW_SPREAD);
}
// A spray of grit (embers for Coal Boy) from within its size along the aim.
static void throw_grit_spray(Entity_NPC& npc, Vector aim)
{
bool isCoal = npc.variant == DANNY_COAL_BOY;
for (int bit = 0; bit < (isCoal ? COAL_BOY_EMBERS : DANNY_GRIT); bit++)
{
Vector direction = throw_direction(aim);
float speed = random_unit() * GRIT_SPEED_RANGE + GRIT_SPEED;
Vector position = vector_add(npc.pos, vector_scale(direction, npc.size * random_unit()));
Entity* grit = g_game->Spawn({.position = position, .velocity = vector_scale(direction, speed),
.subType = isCoal ? 0 : DANNY_GRIT_SUBTYPE, .type = ENTITY_EFFECT,
.variant = isCoal ? EFFECT_EMBER_PARTICLE : EFFECT_TOOTH_PARTICLE, .seed = Random()});
grit->posOffset.y -= GRIT_LIFT;
if (!isCoal) grit->SetColor(DANNY_GRIT_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true);
grit->Update();
}
}
// The throw on Shoot: rocks or coal along the aim (its controller's way when steered), spiders for those lost.
static void danny_throw(Entity_NPC& npc)
{
npc.PlaySound(SOUND_MONSTER_GRUNT_2, 1, DANNY_THROW_DELAY, false, DANNY_THROW_PITCH);
Vector target = npc.GetPlayerTarget()->pos;
npc.UpdateControllerMovement(false);
if (npc.controllerID != -1) target = vector_add(npc.pos, vector_scale(npc.controllerMovement, DANNY_STEER_AIM));
Vector aim = vector_normalized(vector_sub(target, npc.pos));
int kept = (int)Entity_NPC::QueryNPCsGroup(npc.index).size();
if (npc.variant == DANNY_COAL_BOY)
{
int lost = std::max(COAL_BOY_SPIDERS - kept, 0);
for (int thrown = 0; thrown < COAL_BOY_THROWS; thrown++)
{
Vector direction = throw_direction(aim);
Vector from = vector_add(npc.pos, vector_scale(direction, THROW_OUT));
Vector velocity = vector_scale(direction, random_unit() * COAL_BOY_THROW_SPEED_RANGE + COAL_BOY_THROW_SPEED);
RockSpiderKind kind = thrown < lost ? SPIDER_THROW_COAL : SPIDER_THROW_COAL_FIRE;
Entity_NPC* spider = rock_spider_throw(from, velocity, &npc, kind, DANNY_THROW_HEIGHT);
if (thrown < lost) spider->groupIdx = npc.index;
}
return throw_grit_spray(npc, aim);
}
int lost = std::max(DANNY_SPIDERS - kept, 0);
ProjectileParams params;
params.variant = PROJECTILE_ROCK;
npc.FireBossProjectiles(DANNY_ROCKS - lost, target, 1, params);
for (int thrown = 0; thrown < lost; thrown++)
{
Vector direction = throw_direction(aim);
Vector from = vector_add(npc.pos, vector_scale(direction, THROW_OUT));
Vector velocity = vector_scale(direction, random_unit() * DANNY_SPIDER_SPEED_RANGE + DANNY_SPIDER_SPEED);
rock_spider_throw(from, velocity, &npc, SPIDER_THROW_ROCK, DANNY_THROW_HEIGHT)->groupIdx = npc.index;
}
throw_grit_spray(npc, aim);
}
// The spiders (Danny) or fires (Coal Boy) already in the room. Coal Boy counts blue fires (33.2), as the original
// does, not the coal fires (33.11) it throws.
static int danny_crowd_count(Entity_NPC& npc)
{
int crowd = 0;
for (Entity* entity : g_game->room->entityList.updateEL)
{
bool isCrowd = npc.variant == DANNY_COAL_BOY
? entity->type == ENTITY_FIREPLACE && entity->variant == FIRE_BLUE
: (entity->type == ENTITY_SPIDER && entity->variant == 0) || entity->type == ENTITY_ROCK_SPIDER;
if (isCrowd) crowd++;
}
return crowd;
}
// Whether it winds up a throw now: recharged, by chance (more when hurt hard), near enough and not crowded.
static bool danny_is_throwing(Entity_NPC& npc)
{
int ticks = npc_ticks_get(npc);
if (npc.stateFrame > 0) npc.stateFrame -= ticks;
if (npc.v1.x > DANNY_HURT_HARD && npc.stateFrame > 0) npc.stateFrame -= ticks;
if (npc.stateFrame > 0) return false;
bool isControlled = npc.controllerID != -1;
if (isControlled && !npc.IsTargetInAttackRange(DANNY_CONTROLLED_REACH, 0, true)) return false;
if (!isControlled && Random() % DANNY_THROW_ODDS != 0 &&
(npc.v1.x <= DANNY_HURT_HARD || Random() % DANNY_HURT_THROW_ODDS != 0))
return false;
if (npc.HasEntityFlags(FLAG_CONFUSION | FLAG_FEAR | FLAG_SHRINK) || npc.spawnerType == ENTITY_HORNFEL) return false;
float distance = vector_length(vector_sub(npc.pos, npc.GetPlayerTarget()->pos));
bool isInReach = distance < DANNY_NEAR || (distance < DANNY_FAR && Random() % DANNY_FAR_ODDS == 0) || isControlled;
return isInReach && danny_crowd_count(npc) < DANNY_CROWD;
}
// Walking at its target, steered away only when controlled and afraid, then maybe winding up a throw.
static void danny_walk_update(Entity_NPC& npc)
{
npc.AnimWalkFrame("WalkHori", "WalkVert", DANNY_WALK_THRESHOLD);
// The original evades only a controlled Danny that is afraid, so a frightened wild one still walks at its target.
if (npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && npc.controllerID != -1)
{
npc.pathfinder.EvadeTarget(npc.GetPlayerTarget()->pos, STATUS_OBEYED);
npc.friction *= DANNY_FLEE_FRICTION;
}
else npc.pathfinder.FindGridPath(npc.CalcTargetPosition(0), DANNY_SPEED, DANNY_PATH_MARKER, DIRECT_PATH_FAR);
if (npc.pathfinder.hasDirectPath && Random() % DANNY_GRUMBLE_ODDS == 0)
npc.PlaySound(SOUND_ANGRY_GURGLE, DANNY_GRUMBLE_VOLUME, DANNY_GRUMBLE_DELAY, false, 1);
npc.UpdateChargeBar(DANNY_RECHARGE, 0, (float)npc.stateFrame, true);
if (!danny_is_throwing(npc)) return;
Vector target = npc.GetPlayerTarget()->pos;
npc.UpdateControllerMovement(true);
float aimX = npc.controllerID == -1 ? target.x : npc.controllerMovement.x * DANNY_STEER_AIM + npc.pos.x;
npc.sprite.isFlipX = aimX < npc.pos.x;
npc.state = DANNY_THROW;
npc.sprite.Play("Attack");
npc.stateFrame = DANNY_RECHARGE;
}
// 004fbd10 Danny's and Coal Boy's AI: they walk at their target and throw rocks or coal, and spiders to replace the
// ones they lost.
void npc_danny_update(Entity_NPC& npc)
{
switch (npc.state)
{
case NPC_STATE_INIT:
npc.state = DANNY_WALK;
npc.sprite.Play("WalkVert");
npc.splatColor = npc.variant == DANNY_COAL_BOY ? COAL_BOY_SPLAT_COLOR : DANNY_SPLAT_COLOR;
npc.stateFrame = npc.controllerID == -1 ? (int)(Random() % DANNY_FIRST_WAIT_RANGE) + DANNY_FIRST_WAIT
: DANNY_FIRST_WAIT;
break;
case DANNY_WALK:
danny_walk_update(npc);
break;
case DANNY_THROW:
npc.friction *= DANNY_THROW_FRICTION;
if (npc.sprite.animState.animData && npc.sprite.animState.IsEventTriggered("Shoot")) danny_throw(npc);
if (!npc.sprite.IsPlaying()) npc.state = DANNY_WALK;
break;
}
if (npc.pendingDamage > 0) npc.v1.x += npc.pendingDamage;
if (npc.v1.x > 0) npc.v1.x = std::max(npc.v1.x - npc.speedMultiplier * DANNY_HURT_RECOVERY, 0.0f);
}