Seven agents took over the backdrop's floor and wall rendering, room transitions (StartRoomTransition, on_leave, room_enter), grid collision with tear hits and decal baking, Entity_NPC's core with the AI dispatch and the first enemies (flies, Gaper, Pooter, Clotty, Horf, Dip, Spider), the run's save, player damage, PlayerManager::Update, ShowName, pickups, slots and the common effects. Integration added LuaEngine::PostNewLevel/PostNewRoom, one shared lua_callback_check (4 copies merged) and the enemy type range the original checks. Under gdb a run draws its rooms with floors and walls, walks between rooms, fights Gapers with contact damage and the damage flash, and collects pickups. Next: tears hitting enemies, pathfinding, item pedestals, death. 2891 functions taken over. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
320 lines
14 KiB
C++
320 lines
14 KiB
C++
#include "npc_ai.h"
|
|
#include "entity_player.h"
|
|
#include "game.h"
|
|
#include "grid_entity.h"
|
|
#include "room.h"
|
|
#include "score_sheet.h"
|
|
|
|
constexpr int ENTITY_ISAAC_BOSS = 102;
|
|
constexpr int ENTITY_ORBIT_SPAWNER = 873;
|
|
constexpr int SPAWNER_UNBOUNDED = 67;
|
|
constexpr int COLLECTIBLE_SKATOLE_ID = 9;
|
|
constexpr int POOTER_TRIPLE_VARIANT = 2;
|
|
constexpr int POOTER_TWO_SHOT_VARIANT = 1;
|
|
|
|
// The fly types this file covers: the fly, the Pooter and the attack fly.
|
|
constexpr int ENTITY_FLY = 13;
|
|
constexpr int ENTITY_POOTER = 14;
|
|
constexpr int ENTITY_ATTACKFLY = 18;
|
|
|
|
// 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 uint64_t PROJECTILE_FLAG_54 = 1ull << 54;
|
|
|
|
// The super Pooter's shot.
|
|
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;
|
|
|
|
// 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_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 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 (an attack fly nearer), and how hard it pulls.
|
|
constexpr float TARGET_REACH = 30;
|
|
constexpr float TARGET_REACH_ATTACK = 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 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;
|
|
|
|
// The fly's sprite: Fly while it flies (restarted when it stopped), the Pooter's back to Fly after its attack.
|
|
static void fly_animation_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.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;
|
|
return;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
// A fly that crosses a poop (one chance in a thousand a frame) eats 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 ? room.GetGridEntity(gridIndex) : nullptr;
|
|
if (grid && grid->type == GRID_POOP) reconstruction_missing("npc_fly_update 00506bd0: a fly eating poop");
|
|
}
|
|
|
|
// 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_FLAG_54;
|
|
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);
|
|
}
|
|
}
|
|
|
|
// Every third frame a fly picks where it heads: an attack fly (or any under Skatole) its target, a plain fly or
|
|
// Pooter eyes a random spot and its poop, or now and then a fireplace, though it heads nowhere in the end.
|
|
static void fly_target_pick(Entity_NPC& npc, bool isSkatoleActive)
|
|
{
|
|
if (!npc.IsFrame(FLY_RETARGET_FRAMES, 0)) return;
|
|
if (npc.type == ENTITY_ATTACKFLY && npc.parent && npc.controllerID == -1)
|
|
reconstruction_missing("npc_fly_update 00506bd0: an attack fly circling its parent");
|
|
|
|
if (npc.type != ENTITY_ATTACKFLY && !isSkatoleActive)
|
|
{
|
|
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 ? room.GetGridEntity(gridIndex) : nullptr;
|
|
if (grid && grid->type == GRID_POOP) npc.targetPosition = room.GetGridPosition(gridIndex);
|
|
if (Random() % FLY_FIREPLACE_ODDS == 0)
|
|
{
|
|
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;
|
|
}
|
|
return;
|
|
}
|
|
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.
|
|
static void fly_target_follow(Entity_NPC& npc)
|
|
{
|
|
if (vector_is_zero(npc.targetPosition) || npc.GetIntervalTicks(CHARGE_INTERVAL, 0) == 0) return;
|
|
|
|
float reach = npc.type == ENTITY_ATTACKFLY ? TARGET_REACH_ATTACK : TARGET_REACH;
|
|
if (npc.sprite.scale.x > BIG_FLY_SCALE) reach = TARGET_REACH_BIG;
|
|
|
|
Vector toward = vector_sub(npc.targetPosition, npc.pos);
|
|
if (npc.controllerID == -1)
|
|
{
|
|
toward.y *= TARGET_Y_SHARE;
|
|
if (npc.parent) toward = vector_scale(toward, TARGET_PARENT_SHARE);
|
|
}
|
|
else toward = vector_scale(npc.GetControllerInput(0), FLEE_DISTANCE);
|
|
|
|
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 (npc.type != ENTITY_ATTACKFLY) npc.v1 = vector_scale(npc.v1, POOTER_DRIFT_DECAY);
|
|
if (distance > reach)
|
|
npc.AddVelocity_divided(vector_scale(direction, std::min((distance - reach) * TARGET_PULL, TARGET_PULL_MAX)),
|
|
false);
|
|
}
|
|
|
|
// A fly's buzzing: without a target it now and then darts toward its player, 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) reconstruction_missing("npc_fly_update 00506bd0: a controlled fly's steering");
|
|
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));
|
|
}
|
|
}
|
|
|
|
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 and attack fly, with Moter, Sucker and their kin left to come): Skatole
|
|
// befriends them, Lord of the Flies turns them blue, they buzz about drifting, the attack fly chases its target and
|
|
// the Pooter shoots at it.
|
|
void npc_fly_update(Entity_NPC& npc)
|
|
{
|
|
bool isCovered = npc.type == ENTITY_FLY || npc.type == ENTITY_POOTER || npc.type == ENTITY_ATTACKFLY;
|
|
if (!isCovered) reconstruction_missing("npc_fly_update 00506bd0: the Sucker, Moter and the other fly kin");
|
|
|
|
if (npc.state == NPC_STATE_INIT)
|
|
{
|
|
npc.entityGridCollisionClass = GRIDCOLL_WALLS;
|
|
npc.v2 = {};
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.i1 = 0;
|
|
}
|
|
|
|
PlayerManager& players = g_game->playerManager;
|
|
bool isSkatoleActive =
|
|
players.FirstCollectibleOwner(COLLECTIBLE_SKATOLE_ID, 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)
|
|
{
|
|
if (npc.type == ENTITY_ATTACKFLY || npc.type == ENTITY_FLY)
|
|
{
|
|
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.i1 > 0)
|
|
{
|
|
npc.health = std::min(npc.health + npc.maxHitPoints / REGENERATION_FRAMES, npc.maxHitPoints);
|
|
npc.i1--;
|
|
}
|
|
|
|
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) NpcAI::ENTITY_FLY_isaac_spawned(&npc);
|
|
else if (npc.spawnerType == ENTITY_ORBIT_SPAWNER) return NpcAI::ENTITY_FLY_orbit_parent(&npc);
|
|
else 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);
|
|
|
|
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);
|
|
}
|