Files
isaac-port/src/npc_fly.cpp
T

736 lines
32 KiB
C++

#include "npc_basement.h"
#include "boss_ai.h"
#include "entity_bomb.h"
#include "entity_config.h"
#include "entity_player.h"
#include "game.h"
#include "manager.h"
#include "grid_entity.h"
#include "npc_alt.h"
#include "room.h"
#include "score_sheet.h"
#include <algorithm>
// The fly kin this AI runs: the fly, Pooter, attack fly, the Sucker's family, Moter, the level 2 fly, the full fly and
// the fly bomb.
constexpr int SPAWNER_UNBOUNDED = 67;
// The Sucker's variants: the Sucker, Spit, Soul Sucker, Ink, Mama Fly, the Bulbs and the Bloodfly.
constexpr int SUCKER_SPIT = 1;
constexpr int SUCKER_SOUL = 2;
constexpr int SUCKER_INK = 3;
constexpr int SUCKER_MAMA_FLY = 4;
constexpr int SUCKER_BULB_BLACK = 6;
constexpr int POOTER_TWO_SHOT_VARIANT = 1;
constexpr int POOTER_TRIPLE_VARIANT = 2;
constexpr int FLY_BOMB_ORBITING = 1;
constexpr int FLY_BOMB_BOMB_VARIANT = 14;
constexpr ColorMod SPIT_SPLAT_COLOR = {{0.5f, 0.5f, 0.5f, 1}, {0, 0, 0, 0}, {-0.19607845f, 0.50980395f, 0.19607845f}};
constexpr ColorMod INK_SPLAT_COLOR = {{0.02f, 0.05f, 0.05f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr ColorMod FLY_BOMB_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.11764707f, 0.11764707f, 0.11764707f}};
// The Suckers' death shots: a plus, or the Bloodfly's ring of six.
constexpr float SUCKER_SHOT_SPEED = 8;
constexpr float SUCKER_HARD_SHOT_SPEED = 10;
constexpr float BLOODFLY_RING_SHOTS = 6;
constexpr float BLOODFLY_SHOT_SCALE = 1.8f;
// The Spit's death shot: lobbed high and slow at the player, bigger and green, splashing where it lands.
constexpr float SPIT_SHOT_TRAJECTORY = 10;
constexpr float SPIT_SHOT_ACCEL_DROP = 0.3f;
constexpr float SPIT_SHOT_SCALE = 1.6f;
constexpr float SPIT_SHOT_SLOW = 0.55f;
constexpr float SPIT_SHOT_RISE_SCALE = 0.7f;
constexpr float SPIT_SHOT_RISE = 13;
constexpr int SPIT_SHOT_COLOR_PRIORITY = 255;
constexpr ColorMod SPIT_SHOT_COLOR = {{1, 1, 1, 1}, {0.4f, 2, 0.5f, 1}, {0, 0, 0}};
static const Vector SOUL_SWIRL_OFFSET = vector_make(0, -15);
constexpr ColorMod SOUL_TRACER_COLOR = {{0.6f, 0.05f, 0, 0.8f}, {0, 0, 0, 0}, {0, 0, 0}};
constexpr float SOUL_TRACER_SCALE = 4;
constexpr int SOUL_TRACER_TIME = 24;
constexpr ColorMod INK_SHOT_COLOR = {{1, 1, 1, 1}, {0.5f, 0.5f, 0.5f, 1}, {0, 0, 0}};
constexpr float INK_CREEP_SCALE = 4;
constexpr int INK_CREEP_TIME = 450;
constexpr float MOTER_SPLIT_SPEED = 10;
constexpr float MOTER_OVERKILL = -20;
// How a Pooter shoots: when its target is in range (checked every 7 frames), its cooldowns, and its shot.
constexpr float POOTER_RANGE = 200;
constexpr int POOTER_RANGE_FRAMES = 7;
constexpr int POOTER_COOLDOWN = 35;
constexpr int POOTER_TRIPLE_COOLDOWN = 63;
constexpr float POOTER_SHOT_SPEED = 7;
constexpr float POOTER_HARD_SHOT_SPEED = 10;
constexpr float POOTER_FRICTION = 0.8f;
constexpr float POOTER_DRIFT_DECAY = 0.7f;
constexpr int SOUND_POOTER_SHOOT = 178;
constexpr float SUPER_POOTER_SCALE = 1.4f;
constexpr float SUPER_POOTER_FALL_ACCEL = -0.15f;
constexpr float SUPER_POOTER_FALL_SPEED = 1;
constexpr float SUPER_POOTER_SPREAD = 1.2f;
// The fly bomb's cannonball: shot at 10, it deploys after 45 frames or below 5, bouncing off walls. Its orbiting kind
// circles its parent at 40, and drops a bomb that blows 46 to 60 frames later.
constexpr float CANNONBALL_SPEED = 10;
constexpr float CANNONBALL_MASS = 50;
constexpr int CANNONBALL_FRAMES = 45;
constexpr float CANNONBALL_MIN_SPEED = 5;
constexpr float CANNONBALL_FRICTION = 0.85f;
constexpr int SOUND_CANNONBALL_BOUNCE = 40;
constexpr float ORBIT_TURN = 5;
constexpr float ORBIT_RADIUS = 40;
constexpr float ORBIT_DROP_SPEED = 15;
constexpr int BOMB_FUSE = 60;
constexpr int BOMB_FUSE_RANGE = 15;
constexpr int BOMB_APPEAR_FRAME = 8;
// How a fly wanders: the spot it eyes (and a poop or fireplace there), its drift and the bounds it stays within.
constexpr int FLY_RETARGET_FRAMES = 3;
constexpr float FLY_WANDER = 240;
constexpr int FLY_FIREPLACE_ODDS = 100;
constexpr int FLY_POOP_ODDS = 1000;
constexpr int FLY_CHASE_ODDS = 5;
constexpr float FLY_CHASE_SPEED = 0.7f;
constexpr float FLY_STEER_SPEED = 1.1f;
constexpr float FLY_DRIFT_KEEP = 0.4f;
constexpr float FLY_DRIFT_NEW = 0.6f;
constexpr float FLY_DRIFT_STEP = 0.06f;
constexpr float FLY_TOP = 230;
constexpr float FLY_BOTTOM = 420;
constexpr float FLY_LEFT = 80;
constexpr float FLY_RIGHT = 540;
constexpr float FLY_FRICTION = 0.6f;
constexpr int FLY_DRIFT_PERIOD = 4;
constexpr int FLY_STEER_PERIOD = 3;
// How a kin fly circles its parent: within 260, at 65 give or take a slow sway of 5.
constexpr float PARENT_RANGE = 260;
constexpr float PARENT_ORBIT = 65;
constexpr float PARENT_SWAY = 5;
constexpr float PARENT_SWAY_RATE = 0.02f;
constexpr float PARENT_PULL = 0.5f;
constexpr float PARENT_SPIN = 0.2f;
constexpr float PARENT_SPIN_DAMP = 2;
// How a fly flees what it fears: within 400 (at full distance under 200), aimed in a random arc around away.
constexpr float FLEE_RANGE = 400;
constexpr float FLEE_NEAR = 200;
constexpr float FLEE_DISTANCE = 100;
constexpr float FLEE_ARC = 220;
constexpr float FLEE_ARC_BLOCKED = 360;
constexpr uint64_t FLEE_BLOCKERS = FLAG_FREEZE | FLAG_CHARM | FLAG_CONFUSION | FLAG_MIDAS_FREEZE;
// How it flies to its target: how near it stops (the kin nearer, Suckers and big flies nearer still), and how hard
// it pulls. The zigzagging kin pull twice as hard, and bounce off walls now and then.
constexpr float TARGET_REACH = 30;
constexpr float TARGET_REACH_KIN = 25;
constexpr float TARGET_REACH_BIG = 15;
constexpr float TARGET_PULL = 0.08f;
constexpr float TARGET_PULL_MAX = 1.2f;
constexpr float TARGET_Y_SHARE = 0.5f;
constexpr float TARGET_PARENT_SHARE = 0.2f;
constexpr float CONTROLLER_REACH = 100;
constexpr float BIG_FLY_SCALE = 1.1f;
constexpr float KILL_ON_TOUCH_FRICTION = 0.9f;
constexpr float CHARGE_INTERVAL = 2;
constexpr float REGENERATION_FRAMES = 60;
constexpr uint32_t GRID_COLLISION_WALL = 4;
constexpr int BOUNCE_ODDS = 3;
constexpr float BOUNCE_MIN = 2;
constexpr float BOUNCE_RANGE = 1.5f;
constexpr float BOUNCE_DECAY = 0.9f;
static bool is_kin(int type)
{
return type == ENTITY_ATTACKFLY || type == ENTITY_MOTER || type == ENTITY_FLY_L2 || type == ENTITY_FULL_FLY ||
type == ENTITY_FLY_BOMB;
}
static bool is_zigzag(int type)
{
return type == ENTITY_FLY_L2 || type == ENTITY_FULL_FLY || type == ENTITY_FLY_BOMB;
}
// The first frame: idle, with a Spit's green, an Ink's black and a fly bomb's dark blood. The orbiting fly bomb is
// out of reach.
static void fly_init(Entity_NPC& npc)
{
npc.entityGridCollisionClass = GRIDCOLL_WALLS;
npc.v2 = {};
npc.state = NPC_STATE_IDLE;
if (npc.type == ENTITY_SUCKER && npc.variant == SUCKER_SPIT) npc.splatColor = SPIT_SPLAT_COLOR;
else if (npc.type == ENTITY_SUCKER && npc.variant == SUCKER_INK) npc.splatColor = INK_SPLAT_COLOR;
else if (npc.type == ENTITY_FLY_BOMB)
{
npc.splatColor = FLY_BOMB_SPLAT_COLOR;
if (npc.variant == FLY_BOMB_ORBITING)
{
npc.AddEntityFlags(FLAG_NO_STATUS_EFFECTS | FLAG_NO_TARGET);
npc.isInvincible = true;
npc.entityGridCollisionClass = GRIDCOLL_NONE;
}
}
npc.i1 = 0;
}
// 0060b9d0 (NpcHelper::spawn_tracer_effect198) A warning line along a direction from a spot, as the following tracers
// show: only in an online session and outside the 1x1 and narrow rooms.
static Entity_Effect* tracer_spawn(const Vector& position, const Vector& direction)
{
int shape = g_game->room->GetRoomShape();
if (!g_manager->netplayManager.is_online() || !g_game->room->descriptor || !g_game->room->descriptor->data ||
shape == ROOMSHAPE_1x1 || shape == ROOMSHAPE_IH || shape == ROOMSHAPE_IV)
return nullptr;
auto* tracer = static_cast<Entity_Effect*>(g_game->Spawn(
{.position = position, .type = ENTITY_EFFECT, .variant = EFFECT_GENERIC_TRACER, .seed = Random()}));
if (!tracer) return nullptr;
tracer->sprite.color = SOUL_TRACER_COLOR;
tracer->sprite.scale = vector_make(SOUL_TRACER_SCALE, SOUL_TRACER_SCALE);
tracer->targetPosition = direction;
tracer->timeout = SOUL_TRACER_TIME;
tracer->lifespan = SOUL_TRACER_TIME;
tracer->Update();
return tracer;
}
// A dying Sucker bursts by its variant: a plus of shots, the Spit's arcing puddle shot, the Soul Sucker's brimstone
// swirl (with warning lines online), the Ink's cross of inky shots over a pool of black creep, or the Bloodfly's ring.
static void sucker_death(Entity_NPC& npc)
{
if (npc.HasEntityFlags(FLAG_SHRINK)) return;
switch (npc.variant)
{
case 0:
{
ProjectileParams params;
float speed = game_is_hard_mode() ? SUCKER_HARD_SHOT_SPEED : SUCKER_SHOT_SPEED;
npc.FireProjectiles(npc.pos, vector_make(speed, 0), PROJECTILES_PLUS, params);
break;
}
case SUCKER_SPIT:
{
Entity_Projectile* shot = npc.FireBossProjectiles(1, npc.GetPlayerTarget()->pos, SPIT_SHOT_TRAJECTORY, {});
shot->projectileFlags |= PROJECTILE_EXPLODE;
shot->fallingAccel -= SPIT_SHOT_ACCEL_DROP;
shot->scale = SPIT_SHOT_SCALE;
shot->velocity = vector_make(shot->velocity.x * SPIT_SHOT_SLOW, shot->velocity.y * SPIT_SHOT_SLOW);
shot->fallingSpeed = shot->fallingSpeed * SPIT_SHOT_RISE_SCALE - SPIT_SHOT_RISE;
shot->SetColor(SPIT_SHOT_COLOR, -1, SPIT_SHOT_COLOR_PRIORITY, false, true);
break;
}
case SUCKER_SOUL:
{
Entity* swirl = g_game->Spawn({.position = npc.pos,
.spawner = &npc,
.type = ENTITY_EFFECT,
.variant = EFFECT_ENEMY_BRIMSTONE_SWIRL,
.seed = Random()});
swirl->posOffset = SOUL_SWIRL_OFFSET;
swirl->Update();
for (int direction : {DIRECTION_RIGHT, DIRECTION_DOWN, DIRECTION_LEFT, DIRECTION_UP})
tracer_spawn(npc.pos, vector_from_direction(direction));
break;
}
case SUCKER_INK:
{
ProjectileParams params;
params.color = INK_SHOT_COLOR;
float speed = game_is_hard_mode() ? SUCKER_HARD_SHOT_SPEED : SUCKER_SHOT_SPEED;
npc.FireProjectiles(npc.pos, vector_make(speed, 0), PROJECTILES_CROSS, params);
auto* creep = static_cast<Entity_Effect*>(g_game->Spawn(
{.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_CREEP_BLACK, .seed = Random()}));
creep->sprite.scale = vector_make(INK_CREEP_SCALE, INK_CREEP_SCALE);
creep->timeout = INK_CREEP_TIME;
creep->lifespan = INK_CREEP_TIME;
creep->Update();
break;
}
case SUCKER_BLOODFLY:
{
ProjectileParams params;
params.scale = BLOODFLY_SHOT_SCALE;
params.bulletFlags = PROJECTILE_BURST8;
params.circleAngle = 0;
npc.FireProjectiles(npc.pos, vector_make(SUCKER_SHOT_SPEED, BLOODFLY_RING_SHOTS), PROJECTILES_RING, params);
break;
}
}
}
// A Moter killed outright splits into two attack flies, flung apart across its target's line.
static void moter_split(Entity_NPC& npc)
{
Vector toward = vector_sub(npc.GetPlayerTarget()->pos, npc.pos);
float distance = vector_length(toward);
Vector fling = distance > 0
? vector_make(-(toward.y / distance) * MOTER_SPLIT_SPEED, toward.x / distance * MOTER_SPLIT_SPEED)
: vector_scale(random_direction(), MOTER_SPLIT_SPEED);
uint32_t seed = Random();
Entity* first = g_game->Spawn({.position = vector_add(npc.pos, fling),
.velocity = fling,
.spawner = &npc,
.type = ENTITY_ATTACKFLY,
.seed = seed});
seed = Random();
Entity* second = g_game->Spawn({.position = vector_sub(npc.pos, fling),
.velocity = vector_sub({}, fling),
.spawner = &npc,
.type = ENTITY_ATTACKFLY,
.seed = seed});
first->ClearEntityFlags(FLAG_APPEAR);
second->ClearEntityFlags(FLAG_APPEAR);
first->Update();
second->Update();
}
// The fly bomb's bomb, lit to blow 46 to 60 frames later.
static void fly_bomb_drop(Entity_NPC& npc, Entity* parent)
{
Entity_Bomb* bomb = static_cast<Entity_Bomb*>(g_game->Spawn({.position = npc.pos,
.velocity = npc.velocity,
.spawner = &npc,
.type = ENTITY_BOMB,
.variant = FLY_BOMB_BOMB_VARIANT,
.seed = Random()}));
bomb->sprite.Play("Appear");
if (bomb->sprite.animState.animData) bomb->sprite.animState.SetFrameFloat(BOMB_APPEAR_FRAME);
bomb->parent = parent;
bomb->SetExplosionCountdown(BOMB_FUSE - (int)(Random() % BOMB_FUSE_RANGE));
}
// The fly's sprite: Fly while it flies (restarted when it stopped, a Sucker facing its way), the Pooter's back to
// Fly after its attack, the kin idling.
static void fly_animation_update(Entity_NPC& npc)
{
int type = npc.type;
if (type == ENTITY_FLY || type == ENTITY_MOTER || type == ENTITY_SUCKER || type == ENTITY_ATTACKFLY)
{
const AnimationData* current = npc.sprite.animState.animData;
bool isFlying = current && current->name == "Fly" && (!current->isLooping || npc.sprite.animState.isPlaying);
if (!isFlying && npc.sprite.SetAnimation("Fly", SWITCH_KEEP_FRAME))
{
npc.sprite.animState.Rewind();
npc.sprite.animState.isPlaying = true;
}
if (type == ENTITY_SUCKER) npc.sprite.isFlipX = npc.velocity.x < 0;
}
else if (type == ENTITY_POOTER)
{
bool isAttackDone = !npc.sprite.animState.animData || !npc.sprite.animState.isPlaying;
if (npc.state < NPC_STATE_SUICIDE) npc.sprite.Play("Fly");
else if (npc.state == NPC_STATE_ATTACK && isAttackDone) npc.state = NPC_STATE_MOVE;
}
else if (type == ENTITY_FLY_L2 || type == ENTITY_FULL_FLY || (type == ENTITY_FLY_BOMB && npc.state == NPC_STATE_IDLE))
npc.sprite.Play("Idle");
}
// A fly that crosses a poop (one chance in a thousand a frame) eats some of it and becomes a Pooter.
static void fly_poop_eat(Entity_NPC& npc)
{
if (npc.type != ENTITY_FLY || Random() % FLY_POOP_ODDS != 0) return;
Room& room = *g_game->room;
int gridIndex = room.GetGridIndex(npc.pos);
GridEntity* grid = gridIndex >= 0 && gridIndex < ROOM_GRID_CELLS ? room.gridEntities[gridIndex] : nullptr;
if (!grid || grid->type != GRID_POOP) return;
grid->Hurt(1, EntityRef(&npc));
npc.Morph(ENTITY_POOTER, 0, 0, -1);
npc.health = npc.maxHitPoints;
}
// A Pooter in range of its target turns to it and shoots on its Shoot event: a single shot, two for the twin
// shooter, three heavy ones for the super Pooter.
static void pooter_attack_update(Entity_NPC& npc)
{
if (npc.projectileCooldown < 1 && npc.IsTargetInAttackRange(POOTER_RANGE, POOTER_RANGE_FRAMES, false))
{
npc.projectileCooldown = npc.variant == POOTER_TRIPLE_VARIANT ? POOTER_TRIPLE_COOLDOWN : POOTER_COOLDOWN;
npc.sprite.Play("Attack");
npc.state = NPC_STATE_ATTACK;
npc.UpdateControllerMovement(true);
float facing = vector_is_zero(npc.controllerMovement) ? npc.GetPlayerTarget()->pos.x - npc.pos.x
: npc.controllerMovement.x;
npc.sprite.isFlipX = facing < 0;
}
if (npc.state == NPC_STATE_ATTACK)
{
npc.UpdateControllerMovement(false);
if (npc.sprite.animState.animData && npc.sprite.animState.IsEventTriggered("Shoot"))
{
Vector aim = vector_is_zero(npc.controllerMovement) ? vector_sub(npc.GetPlayerTarget()->pos, npc.pos)
: npc.controllerMovement;
ProjectileParams params;
ProjectilesMode mode = npc.variant == POOTER_TWO_SHOT_VARIANT ? PROJECTILES_TWO : PROJECTILES_SINGLE;
float speed = game_is_hard_mode() ? POOTER_HARD_SHOT_SPEED : POOTER_SHOT_SPEED;
if (npc.variant == POOTER_TRIPLE_VARIANT)
{
params.scale = SUPER_POOTER_SCALE;
params.fallingAccelModifier = SUPER_POOTER_FALL_ACCEL;
params.fallingSpeedModifier = SUPER_POOTER_FALL_SPEED;
params.spread = SUPER_POOTER_SPREAD;
params.bulletFlags = PROJECTILE_BACK_SPLIT;
params.isFallingKept = true;
mode = PROJECTILES_FOUR;
speed = POOTER_SHOT_SPEED;
}
npc.FireProjectiles(npc.pos, vector_resized(aim, speed), mode, params);
npc.PlaySound(SOUND_POOTER_SHOOT, 1, 2, false, 1);
}
}
if (npc.projectileCooldown > 0)
{
npc.friction *= POOTER_FRICTION;
npc.projectileCooldown -= npc_ticks_get(npc);
npc.v1 = vector_scale(npc.v1, POOTER_DRIFT_DECAY);
}
}
// The fly bomb shot as a cannonball flies at 10 until it slows or 45 frames pass, bouncing off walls, then deploys.
// The orbiting one circles its parent, dropping its bomb once the parent is gone. Whether the update ends here.
static bool fly_bomb_update(Entity_NPC& npc)
{
if (npc.state == NPC_STATE_SPECIAL)
{
if (npc.stateFrame == -1)
{
npc.friction *= CANNONBALL_FRICTION;
if (!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying) npc.state = NPC_STATE_IDLE;
return true;
}
if (npc.mass < CANNONBALL_MASS)
{
npc.velocity = vector_resized(npc.velocity, CANNONBALL_SPEED);
npc.mass = CANNONBALL_MASS;
}
npc.sprite.Play("Cannonball");
npc.stateFrame++;
if (npc.stateFrame > CANNONBALL_FRAMES || vector_length(npc.velocity) < CANNONBALL_MIN_SPEED)
{
npc.sprite.Play("Deploy");
npc.stateFrame = -1;
npc.mass = npc.config->mass;
}
if (!npc.isCollidesWithGrid) return true;
Vector normal = npc.gridCollisionNormal;
Vector hit = npc.gridCollisionVelocity;
npc.velocity = vector_sub(hit, vector_scale(normal, 2 * vector_dot(hit, normal)));
npc_sound_play(SOUND_CANNONBALL_BOUNCE, 1, 2, 1);
return true;
}
if (npc.variant != FLY_BOMB_ORBITING) return false;
Entity* parent = npc.parent.get();
npc.isInvincible = true;
if (!parent)
{
npc.Kill(EntityRef());
npc.velocity = vector_scale(vector_from_angle(npc.v1.x), ORBIT_DROP_SPEED);
fly_bomb_drop(npc, npc.parent.get());
return true;
}
npc.v1.x = normalize_angle_degrees(npc.speedMultiplier * ORBIT_TURN + npc.v1.x);
npc.targetPosition = vector_add(parent->pos, vector_scale(vector_from_angle(npc.v1.x), ORBIT_RADIUS));
npc.velocity = vector_sub(npc.targetPosition, npc.pos);
return true;
}
// A kin fly near its parent keeps a swaying distance around it and circles it.
static void fly_parent_circle(Entity_NPC& npc)
{
Entity* parent = npc.parent.get();
if (!parent || npc.controllerID != -1) return;
Vector toward = vector_sub(parent->pos, npc.pos);
float distance = vector_length(toward);
if (distance >= PARENT_RANGE) return;
float sway = std::sin((float)g_game->frameCount * PARENT_SWAY_RATE) * PARENT_SWAY + PARENT_ORBIT;
float offset = std::clamp(distance - sway, -PARENT_SWAY, PARENT_SWAY);
npc.AddVelocity_divided(vector_make(offset * toward.x * PARENT_PULL / distance,
offset * toward.y * PARENT_PULL / distance),
false);
float spin = std::abs(offset) + PARENT_SPIN_DAMP;
npc.AddVelocity_divided(vector_make(-(toward.y * PARENT_SPIN / spin) / npc.friction,
toward.x * PARENT_SPIN / spin / npc.friction),
false);
}
// A plain fly or Pooter eyes a random spot within 240 and its poop, or now and then a fireplace.
static void fly_wander_pick(Entity_NPC& npc)
{
Room& room = *g_game->room;
Vector direction = random_direction();
float scaleFirst = random_unit();
float distance = random_unit() * scaleFirst * FLY_WANDER;
int gridIndex = room.GetGridIndex(vector_add(npc.pos, vector_scale(direction, distance)));
GridEntity* grid = gridIndex >= 0 && gridIndex < ROOM_GRID_CELLS ? room.gridEntities[gridIndex] : nullptr;
if (grid && grid->type == GRID_POOP) npc.targetPosition = room.GetGridPosition(gridIndex);
if (Random() % FLY_FIREPLACE_ODDS != 0) return;
std::vector<Entity*> fireplaces;
for (Entity* entity : room.entityList.mainEL)
if (entity->type == ENTITY_FIREPLACE) fireplaces.push_back(entity);
if (!fireplaces.empty()) npc.targetPosition = fireplaces[random_below((int)fireplaces.size())]->pos;
}
// Every third frame a fly picks where it heads: the kin circle their parent, plain flies and Pooters eye a spot,
// and in the end each heads for its target (a plain fly or Pooter forgets it right after).
static void fly_target_pick(Entity_NPC& npc, bool isSkatoleActive)
{
if (!npc.IsFrame(FLY_RETARGET_FRAMES, 0)) return;
if (is_kin(npc.type)) fly_parent_circle(npc);
bool isWanderer = npc.type != ENTITY_ATTACKFLY && npc.type != ENTITY_SUCKER && !is_kin(npc.type);
if (isWanderer && !isSkatoleActive) fly_wander_pick(npc);
npc.targetPosition = npc.CalcTargetPosition(0);
}
// A feared or shrunk fly within 400 of its target heads off to a random spot in an arc away from it, wider when it
// touches a wall.
static void fly_flee_update(Entity_NPC& npc)
{
if (!npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) || npc.HasEntityFlags(FLEE_BLOCKERS) || npc.controllerID != -1)
return;
float distance = vector_length(vector_sub(npc.pos, npc.GetPlayerTarget()->pos));
if (distance >= FLEE_RANGE) return;
float reach =
distance > FLEE_NEAR ? FLEE_DISTANCE - (distance - FLEE_NEAR) / FLEE_NEAR * FLEE_DISTANCE : FLEE_DISTANCE;
float arc = npc.isCollidesWithGrid ? FLEE_ARC_BLOCKED : FLEE_ARC;
Vector away = vector_sub(npc.pos, npc.GetPlayerTarget()->pos);
float angle = (random_unit() * arc - arc * 0.5f) * DEGREES_TO_RADIANS;
Vector turned = vector_make(std::cos(angle) * away.x - away.y * std::sin(angle),
away.y * std::cos(angle) + std::sin(angle) * away.x);
npc.targetPosition = vector_add(vector_resized(turned, reach), npc.pos);
npc.v1 = vector_scale(npc.v1, 0.5f);
}
// Every other tick a fly with a target pulls toward it once farther than its reach, harder the farther. The level 2
// fly and fly bomb veer one way, the full fly the other, twice as hard.
static void fly_target_follow(Entity_NPC& npc, bool isSkatoleActive)
{
if (vector_is_zero(npc.targetPosition) || npc.GetIntervalTicks(CHARGE_INTERVAL, 0) == 0) return;
int type = npc.type;
float reach = is_kin(type) ? TARGET_REACH_KIN : TARGET_REACH;
if (type == ENTITY_SUCKER || npc.sprite.scale.x > BIG_FLY_SCALE) reach = TARGET_REACH_BIG;
Vector toward = vector_sub(npc.targetPosition, npc.pos);
if (npc.controllerID == -1)
{
bool isVeering = !isSkatoleActive;
if ((type == ENTITY_FLY_L2 || type == ENTITY_FLY_BOMB) && isVeering)
toward = vector_make(toward.x - toward.y, toward.y + toward.x);
else if (type == ENTITY_FULL_FLY && isVeering) toward = vector_make(toward.x + toward.y, toward.y - toward.x);
toward.y *= TARGET_Y_SHARE;
if (npc.parent) toward = vector_scale(toward, TARGET_PARENT_SHARE);
}
else toward = vector_scale(npc.GetControllerInput(0), CONTROLLER_REACH);
float distance = vector_length(toward);
Vector direction = distance > 0 ? vector_scale(toward, 1 / distance) : toward;
if (npc.isKillOnTouch) npc.friction *= KILL_ON_TOUCH_FRICTION;
if (type != ENTITY_SUCKER && !is_kin(type)) npc.v1 = vector_scale(npc.v1, POOTER_DRIFT_DECAY);
if (distance <= reach) return;
float pull = std::min((distance - reach) * TARGET_PULL, TARGET_PULL_MAX);
if (is_zigzag(type) && !isSkatoleActive) pull += pull;
npc.AddVelocity_divided(vector_scale(direction, pull), false);
}
// The zigzagging kin now and then bounce off a wall they touch, the bounce fading.
static void fly_bounce_update(Entity_NPC& npc)
{
if (npc.isCollidesWithGrid && Random() % BOUNCE_ODDS == 0 && !npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK))
npc.v2 = vector_resized(npc.gridCollisionNormal, -BOUNCE_MIN - random_unit() * BOUNCE_RANGE);
if (vector_length_squared(npc.v2) <= 1)
{
npc.v2 = {};
return;
}
npc.AddVelocity_divided(npc.v2, false);
npc.v2 = vector_scale(npc.v2, BOUNCE_DECAY);
}
// A fly's buzzing: without a target it now and then darts toward its player (or its player steers it), every fourth
// frame its drift turns, every third it drifts back inside the room's middle and its drift moves it.
static void fly_drift_update(Entity_NPC& npc)
{
if (vector_is_zero(npc.targetPosition))
{
if (npc.controllerID == -1)
{
if (Random() % FLY_CHASE_ODDS == 0)
{
Vector toward = vector_normalized(vector_sub(npc.GetPlayerTarget()->pos, npc.pos));
npc.velocity = vector_add(npc.velocity, vector_scale(toward, FLY_CHASE_SPEED * npc.speedMultiplier));
}
}
else if (npc.GetIntervalTicks(CHARGE_INTERVAL, 0) != 0)
{
Vector input = npc.GetControllerInput(0);
npc.velocity = vector_add(npc.velocity, vector_scale(input, FLY_STEER_SPEED * npc.speedMultiplier));
}
}
if (g_game->frameCount % FLY_DRIFT_PERIOD == 1)
npc.v1 = vector_scale(vector_add(vector_scale(random_direction(), FLY_DRIFT_NEW), npc.v1), FLY_DRIFT_KEEP);
if (g_game->frameCount % FLY_STEER_PERIOD != 2) return;
if (npc.spawnerType != SPAWNER_UNBOUNDED)
{
if (npc.pos.y < FLY_TOP) npc.v1.y += FLY_DRIFT_STEP;
else if (npc.pos.y > FLY_BOTTOM) npc.v1.y -= FLY_DRIFT_STEP;
if (npc.pos.x > FLY_RIGHT) npc.v1.x -= FLY_DRIFT_STEP;
else if (npc.pos.x < FLY_LEFT) npc.v1.x += FLY_DRIFT_STEP;
}
if (npc.friction != 0)
npc.velocity = vector_add(npc.velocity, vector_scale(npc.v1, 2 * npc.speedMultiplier / npc.friction));
npc.friction *= FLY_FRICTION;
}
// 00506bd0 The flies' AI: the fly, Pooter, attack fly, Moter, the Suckers, the level 2 and full flies and the fly
// bomb. Skatole befriends them, Lord of the Flies turns them blue, they buzz about drifting, the kin circle their
// parent and chase their target, the Pooter shoots at it, and the Suckers and Moter burst as they die.
void npc_fly_update(Entity_NPC& npc)
{
if (npc.type == ENTITY_SUCKER && (npc.variant == SUCKER_BULB || npc.variant == SUCKER_BULB_BLACK))
return npc_bulb_update(npc);
if (npc.type == ENTITY_SUCKER && npc.variant == SUCKER_MAMA_FLY) return npc_mama_fly_update(npc);
if (npc.state == NPC_STATE_INIT) fly_init(npc);
PlayerManager& players = g_game->playerManager;
bool isSkatoleActive = players.FirstCollectibleOwner(COLLECTIBLE_SKATOLE, LAZARUS_SHARE_CHECK) && !npc.IsFriendly();
if (isSkatoleActive && npc.type == ENTITY_ATTACKFLY) npc.Morph(ENTITY_FLY, 0, 0, -1);
Entity_Player* beelzebub = players.GetRandomPlayer(npc.initSeed);
if (beelzebub && !npc.isDead)
{
bool isBlued = npc.type == ENTITY_ATTACKFLY || npc.type == ENTITY_FLY || npc.type == ENTITY_FLY_L2 ||
npc.type == ENTITY_FULL_FLY;
if (isBlued)
{
g_game->scoreSheet.AddKilledEnemy(npc);
npc.Remove();
beelzebub->AddBlueFlies(1, npc.pos, nullptr);
}
else if (!npc.IsBoss() && !npc.IsFriendly()) npc.AddCharmed(EntityRef(beelzebub), -1, true, false);
}
if (npc.type == ENTITY_SUCKER && npc.controllerID != -1 && npc.IsControllerActionPressed(CONTROLLER_ACTION_SHOOT))
npc.Kill(EntityRef());
if (npc.isDead && npc.type == ENTITY_SUCKER) sucker_death(npc);
else if (npc.isDead && npc.type == ENTITY_MOTER && npc.health > MOTER_OVERKILL && !isSkatoleActive) moter_split(npc);
if (npc.i1 > 0)
{
npc.health = std::min(npc.health + npc.maxHitPoints / REGENERATION_FRAMES, npc.maxHitPoints);
npc.i1--;
}
if (npc.isDead && npc.type == ENTITY_FLY_BOMB) fly_bomb_drop(npc, npc.parent.get());
fly_animation_update(npc);
fly_poop_eat(npc);
if (npc.type == ENTITY_POOTER && !isSkatoleActive) pooter_attack_update(npc);
bool isIsaacsFly = npc.spawnerType == ENTITY_ISAAC_BOSS && npc.subtype != 0;
if (isIsaacsFly) npc_isaac_fly_update(npc);
else if (npc.spawnerType == ENTITY_FLY_TRAP) return fly_trap_fly_update(npc);
else
{
if (npc.type == ENTITY_FLY_BOMB && fly_bomb_update(npc)) return;
fly_target_pick(npc, isSkatoleActive);
}
if ((npc.type == ENTITY_FLY || npc.type == ENTITY_POOTER) && !isIsaacsFly) npc.targetPosition = {};
fly_flee_update(npc);
if (!isIsaacsFly)
{
fly_target_follow(npc, isSkatoleActive);
if (is_zigzag(npc.type)) fly_bounce_update(npc);
}
if (npc.entityGridCollisionClass == GRIDCOLL_NONE && g_game->room->IsPositionInRoom(npc.pos, npc.size) &&
room_grid_collision_at(npc.pos) < GRID_COLLISION_WALL)
npc.entityGridCollisionClass = GRIDCOLL_WALLS;
fly_drift_update(npc);
if (npc.type == ENTITY_FULL_FLY && npc.isDead && !npc.HasEntityFlags(FLAG_SHRINK))
{
ProjectileParams params;
npc.FireProjectiles(npc.pos, vector_make(SUCKER_SHOT_SPEED, BLOODFLY_RING_SHOTS), PROJECTILES_RING, params);
}
}
// The dart fly circles while it homes in on where its target is heading, at up to 8.
constexpr float DART_TURN = 0.18849558f;
constexpr float DART_FULL_TURN = 6.2831855f;
constexpr float DART_LEAD = 0.25f;
constexpr float DART_CIRCLE = 10;
constexpr float DART_HOMING = 15;
constexpr float DART_HOMING_SHARE = 0.9f;
constexpr float DART_ACCEL = 0.75f;
constexpr float DART_ACCEL_DAMP = 0.9f;
constexpr float DART_ACCEL_SHARE = 0.033333335f;
constexpr float DART_MAX_SPEED = 8;
constexpr float DART_FRICTION = 0.95f;
// 005400d0 The dart fly's AI: Lord of the Flies turns it blue and Skatole tames it into a fly. It weaves along a
// circle while homing in on where its target is heading, at up to 8.
void npc_dart_fly_update(Entity_NPC& npc)
{
PlayerManager& players = g_game->playerManager;
Entity_Player* beelzebub = players.GetRandomPlayer(npc.initSeed);
if (beelzebub && !npc.isDead)
{
g_game->scoreSheet.AddKilledEnemy(npc);
npc.Remove();
beelzebub->AddBlueFlies(1, npc.pos, nullptr);
}
if (players.FirstCollectibleOwner(COLLECTIBLE_SKATOLE, LAZARUS_SHARE_CHECK) && !npc.IsFriendly())
{
npc.Morph(ENTITY_FLY, 0, 0, -1);
npc.v2 = {};
npc.v1 = {};
}
if (npc.state == NPC_STATE_INIT)
{
npc.stateFrame = 0;
npc.state = NPC_STATE_MOVE;
float turn = random_unit();
npc.v1.x = (turn + turn) * PI_F;
}
if (npc.state == NPC_STATE_MOVE)
{
if (npc.controllerID != -1)
reconstruction_missing("NpcAI::ENTITY_DART_FLY 005400d0: a dart fly under a player's control");
npc.v1.x += DART_TURN;
if (npc.v1.x >= DART_FULL_TURN) npc.v1.x -= DART_FULL_TURN;
Vector toward = vector_sub(npc.CalcTargetPosition(0), npc.pos);
float distance = vector_length(toward);
Vector lead = npc.GetPlayerTarget()->velocity;
Vector aim = vector_normalized(vector_make((lead.x + lead.x) * distance * DART_LEAD + toward.x,
(lead.y + lead.y) * distance * DART_LEAD + toward.y));
float circleX = (float)std::cos((double)(npc.v1.x * 0.5f));
float circleY = (float)std::sin((double)npc.v1.x);
float steerX = circleX * DART_CIRCLE + aim.x * DART_HOMING * DART_HOMING_SHARE;
float steerY = circleY * DART_CIRCLE + aim.y * DART_HOMING * DART_HOMING_SHARE;
npc.velocity.x += steerX * DART_ACCEL * DART_ACCEL_DAMP * DART_ACCEL_SHARE * npc.speedMultiplier;
npc.velocity.y += steerY * DART_ACCEL * DART_ACCEL_DAMP * DART_ACCEL_SHARE * npc.speedMultiplier;
if (vector_length(npc.velocity) > DART_MAX_SPEED) npc.velocity = vector_resized(npc.velocity, DART_MAX_SPEED);
npc.friction *= DART_FRICTION;
}
npc.sprite.Play("Fly");
npc.sprite.isFlipX = npc.velocity.x > 0;
}