Files
isaac-port/src/npc_brain.cpp
T

207 lines
9.5 KiB
C++

#include "npc_caves.h"
#include "boss_ai.h"
#include "entity_bomb.h"
#include "entity_effect.h"
#include "entity_tear.h"
#include "sfx_manager.h"
// The Brain, the Black Globin's head and the Poison Mind each crawl about bleeding and leaving creep, then lunge.
constexpr uint64_t FLAG_BRAIN_CALM = FLAG_CONFUSION | FLAG_FEAR | FLAG_SHRINK;
constexpr uint64_t FLAG_BRAIN_HELD = FLAG_CHARM | FLAG_CONFUSION | FLAG_FEAR | FLAG_SHRINK;
constexpr ColorMod POISON_MIND_SPLAT = {{0, 1, 0, 1}, {0, 0, 0, 0}, {0, 0.19607845f, 0}};
// They bleed a splat every third frame and keep their cell cheap to walk through.
constexpr float BRAIN_SPLAT_FRAMES = -3;
constexpr float BRAIN_SPLAT_MIN = 0.2f;
constexpr float BRAIN_SPLAT_RANGE = 0.39f;
constexpr int BRAIN_GRID_PATH_LIMIT = 950;
// The Brain crawls the grid's axes at 0.75 and lunges along one when its target lines up within 200. The Black Globin's
// head chases at 0.5. The Poison Mind drifts diagonally, turning every few seconds, and lunges diagonally.
constexpr float BRAIN_SPEED = 0.75f;
constexpr float BRAIN_FRICTION_X = 0.85f;
constexpr float BRAIN_FRICTION_Y = 0.8f;
constexpr float GLOBIN_HEAD_SPEED = 0.5f;
constexpr float POISON_MIND_FRICTION = 0.8f;
constexpr uint32_t POISON_MIND_TURN_FRAMES = 90;
constexpr float POISON_MIND_DIAGONAL = 0.7853982f;
constexpr float POISON_MIND_PUSH = 0.5f;
constexpr int BRAIN_AIM_FRAMES = 7;
constexpr float BRAIN_AIM_LEAD = 16;
constexpr float BRAIN_MARGIN = 20;
constexpr float BRAIN_LINE_WIDTH = 20;
constexpr float BRAIN_RANGE = 200;
constexpr int BRAIN_SOUND_DELAY = 30;
constexpr float BRAIN_LUNGE_FRICTION = 0.8f;
constexpr float BRAIN_CENTERING = 0.1f;
// Their creep: every 12 frames (24 in a crowd of five or more), or 6 frames later while fast.
constexpr int BRAIN_CROWD = 5;
constexpr int BRAIN_CREEP_FRAMES = 12;
constexpr int BRAIN_CROWD_CREEP_FRAMES = 24;
constexpr float BRAIN_CREEP_FAST_SQUARED = 9;
constexpr float BRAIN_CREEP_SCALE = 1.23f;
constexpr float POISON_MIND_BLAST_DAMAGE = 40;
// One of the four axis directions turned by an angle in radians.
static Vector brain_turn(const Vector& direction, float angle)
{
float c = std::cos(angle);
float s = std::sin(angle);
return vector_make(c * direction.x - s * direction.y, s * direction.x + c * direction.y);
}
// Where the target will be in a moment, kept in the room.
static Vector brain_target_lead(Entity_NPC& npc)
{
Vector targetPos = npc.GetPlayerTarget()->pos;
Vector targetVelocity = npc.GetPlayerTarget()->velocity;
Vector lead = vector_add(targetPos, vector_scale(targetVelocity, random_unit() * BRAIN_AIM_LEAD));
return g_game->room->GetClampedPosition(lead, BRAIN_MARGIN);
}
// The Poison Mind drifts along a diagonal it changes every few seconds or on a wall (its player's steered one when
// controlled), and lunges diagonally when its target lines up.
static void poison_mind_drift(Entity_NPC& npc)
{
npc.velocity = vector_scale(npc.velocity, npc_friction_scaled(POISON_MIND_FRICTION, npc.speedMultiplier));
npc.stateFrame -= npc_ticks_get(npc);
if (npc.stateFrame < 0 || npc.isCollidesWithGrid)
{
npc.stateFrame = (int)(Random() % POISON_MIND_TURN_FRAMES);
npc.targetPosition = brain_turn(vector_from_direction((int)(Random() & 3)), POISON_MIND_DIAGONAL);
}
if (npc.controllerID != -1)
{
npc.targetPosition = npc.GetControllerInput(0);
if (!vector_is_zero(npc.targetPosition))
{
Vector input = npc.targetPosition;
Vector diagonal =
brain_turn(vector_cardinal(brain_turn(input, -POISON_MIND_DIAGONAL)), POISON_MIND_DIAGONAL);
npc.targetPosition = vector_scale(diagonal, vector_length(input));
}
}
npc.velocity = vector_add(npc.velocity, vector_scale(npc.targetPosition, POISON_MIND_PUSH * npc.speedMultiplier));
if (!npc.GetIntervalTicks(BRAIN_AIM_FRAMES, (float)(npc.index % BRAIN_AIM_FRAMES)) || npc.controllerID != -1 ||
npc.HasEntityFlags(FLAG_BRAIN_CALM))
return;
Vector gap = vector_sub(npc.pos, brain_target_lead(npc));
Vector turned = brain_turn(gap, POISON_MIND_DIAGONAL);
if (std::abs(turned.x) >= BRAIN_LINE_WIDTH && std::abs(turned.y) >= BRAIN_LINE_WIDTH) return;
if (vector_length(gap) >= BRAIN_RANGE) return;
npc.targetPosition = vector_resized(gap, -1);
npc.state = NPC_STATE_ATTACK;
npc_sound_play(SOUND_POISON_MIND_HURT, 1, 2, 1);
}
// The Brain crawls the grid's axes, squelching on each new spot, and lunges along one when its target lines up.
static void brain_crawl(Entity_NPC& npc)
{
Vector lastSpot = npc.pathfinder.movementTarget;
npc.pathfinder.MoveRandomlyAxisAligned(BRAIN_SPEED, STATUS_OBEYED);
npc.velocity.x *= npc_friction_scaled(BRAIN_FRICTION_X, npc.speedMultiplier);
npc.velocity.y *= npc_friction_scaled(BRAIN_FRICTION_Y, npc.speedMultiplier);
bool isBrain = npc.type == ENTITY_BRAIN;
if (isBrain && !vector_is_zero(vector_sub(lastSpot, npc.pathfinder.movementTarget)))
npc_sound_play(SOUND_BRAIN_MOVE, 1, BRAIN_SOUND_DELAY, 1);
if (!npc.IsFrame(BRAIN_AIM_FRAMES, npc.index) || npc.controllerID != -1 || npc.HasEntityFlags(FLAG_BRAIN_CALM))
return;
Room& room = *g_game->room;
Vector lead = brain_target_lead(npc);
Vector cell = room.GetGridPosition(npc.pathfinder.gridIndex);
Vector gap = vector_sub(cell, lead);
if (std::abs(gap.x) >= BRAIN_LINE_WIDTH && std::abs(gap.y) >= BRAIN_LINE_WIDTH) return;
if (vector_length(gap) >= BRAIN_RANGE || !room_check_line(cell, lead, LINE_MODE_GROUND, 0)) return;
if (std::abs(gap.x) <= std::abs(gap.y)) npc.targetPosition = vector_make(0, gap.y >= 0 ? -1 : 1);
else npc.targetPosition = vector_make(gap.x >= 0 ? -1 : 1, 0);
npc.state = NPC_STATE_ATTACK;
if (isBrain) npc_sound_play(SOUND_BRAIN_MOVE, 1, BRAIN_SOUND_DELAY, 1);
}
// The lunge: on along its direction, kept to its cell's line (but the Poison Mind), until it hits a wall or a status
// effect or a forced target or a player stops it.
static void brain_lunge(Entity_NPC& npc)
{
if (npc.isCollidesWithGrid || npc.HasEntityFlags(FLAG_BRAIN_HELD) || npc.forcedTarget || npc.controllerID != -1)
{
npc.pathfinder.movementTarget = {};
npc.pathfinder.moveDirection = {};
npc.state = NPC_STATE_MOVE;
npc.targetPosition = {};
return;
}
npc.friction *= BRAIN_LUNGE_FRICTION;
if (npc.friction != 0)
npc.velocity = vector_add(npc.velocity, vector_scale(npc.targetPosition, npc.speedMultiplier / npc.friction));
if (npc.type == ENTITY_POISON_MIND) return;
Vector cell = g_game->room->GetGridPosition(npc.pathfinder.gridIndex);
if (std::abs(npc.velocity.x) <= std::abs(npc.velocity.y))
npc.pos.x = cell.x * BRAIN_CENTERING + npc.pos.x * (1 - BRAIN_CENTERING);
else npc.pos.y = npc.pos.y * (1 - BRAIN_CENTERING) + cell.y * BRAIN_CENTERING;
}
// Red creep (the Poison Mind's green) behind it, every 12 frames or 24 in a crowd, or half way between while fast.
static void brain_creep_update(Entity_NPC& npc)
{
int interval = npc.GetAliveEnemyCount() < BRAIN_CROWD ? BRAIN_CREEP_FRAMES : BRAIN_CROWD_CREEP_FRAMES;
int offset = (int)(npc.index % (uint32_t)interval);
bool isCreepTick = npc.GetIntervalTicks((float)interval, (float)offset) > 0;
bool isFastTick = npc.GetIntervalTicks((float)interval, (float)(offset + interval / 2)) > 0;
bool isFast = vector_length_squared(npc.velocity) > BRAIN_CREEP_FAST_SQUARED;
if (npc.HasEntityFlags(FLAG_SHRINK) || !(isCreepTick || (isFast && isFastTick))) return;
int variant = npc.type == ENTITY_POISON_MIND ? EFFECT_CREEP_GREEN : EFFECT_CREEP_RED;
Entity_Effect* creep = static_cast<Entity_Effect*>(g_game->Spawn(
{.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = variant, .seed = Random()}));
creep->sprite.scale = vector_make(BRAIN_CREEP_SCALE, BRAIN_CREEP_SCALE);
creep->rotation = 0;
creep->Update();
}
// 00512110 The Brain's AI, shared with the Black Globin's head and the Poison Mind: they bleed and leave creep as they
// crawl about (the head chasing), lunging when their target lines up. The Poison Mind bursts into a poison blast.
void npc_brain_update(Entity_NPC& npc)
{
if (npc.state == NPC_STATE_INIT)
{
npc.state = NPC_STATE_MOVE;
if (npc.type != ENTITY_POISON_MIND) return;
npc.splatColor = POISON_MIND_SPLAT;
npc.entityGridCollisionClass = GRIDCOLL_WALLS;
return;
}
if (npc.GetIntervalTicks(BRAIN_SPLAT_FRAMES, 0) != 0)
npc.MakeSplat(random_unit() * BRAIN_SPLAT_RANGE + BRAIN_SPLAT_MIN)
->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
Room& room = *g_game->room;
int cell = room.GetGridIndex(npc.pos);
if (cell >= 0 && room.gridPaths[cell] <= BRAIN_GRID_PATH_LIMIT) room.gridPaths[cell] = GRID_PATH_FREED;
npc_anim_loop(npc.sprite, "Walk");
npc.pathfinder.UpdateGridIndex();
if (npc.state != NPC_STATE_MOVE) brain_lunge(npc);
else if (npc.type == ENTITY_BLACK_GLOBIN_HEAD)
{
npc.pathfinder.FindGridPath(npc.CalcTargetPosition(0), GLOBIN_HEAD_SPEED, GRID_PATH_FREED, DIRECT_PATH_FAR);
npc.sprite.isFlipX = npc.velocity.x > 0;
}
else if (npc.type == ENTITY_POISON_MIND) poison_mind_drift(npc);
else brain_crawl(npc);
brain_creep_update(npc);
if (!npc.isDead || npc.type != ENTITY_POISON_MIND || npc.HasEntityFlags(FLAG_SHRINK)) return;
Explosion blast{.position = npc.pos, .source = &npc, .damage = POISON_MIND_BLAST_DAMAGE};
blast.tearFlags[TEAR_POISON] = true;
explosion_effects(blast);
}