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>
129 lines
5.5 KiB
C++
129 lines
5.5 KiB
C++
#include "npc_ai.h"
|
|
#include "entity_effect.h"
|
|
#include "entity_familiar.h"
|
|
#include "entity_player.h"
|
|
#include "game.h"
|
|
#include "room.h"
|
|
|
|
constexpr int ENTITY_DIP = 217;
|
|
constexpr int DIP_CORN = 1;
|
|
constexpr int DIP_BROWNIE = 2;
|
|
constexpr int DIP_BIG_CORN = 3;
|
|
constexpr int FAMILIAR_DIP_CORN = 2;
|
|
constexpr int FAMILIAR_DIP_BROWNIE = 20;
|
|
constexpr int COLLECTIBLE_DIRTY_MIND_ID = 576;
|
|
constexpr int TRINKET_DIPPING_TRINKET_ID = 163;
|
|
constexpr int PROJECTILE_CORN = 5;
|
|
constexpr int SOUND_DIP_HURT = 60;
|
|
constexpr int COLOR_PRIORITY_DEFAULT = 255;
|
|
|
|
// How a Dip hops: one chance in ten a frame from rest, at a random speed (doubled for the corn dip), and how it
|
|
// slows, splats and squeaks.
|
|
constexpr int HOP_ODDS = 10;
|
|
constexpr float HOP_SPEED_MIN = 5;
|
|
constexpr float HOP_SPEED_RANGE = 5;
|
|
constexpr float HOP_FRICTION = 0.8f;
|
|
constexpr float SLIDE_FRICTION = 0.95f;
|
|
constexpr float SPLAT_SIZE_MIN = 0.2f;
|
|
constexpr float SPLAT_SIZE_RANGE = 0.5f;
|
|
constexpr int SPLAT_ODDS = 10;
|
|
constexpr float HURT_VOLUME = 0.8f;
|
|
constexpr int HURT_SOUND_DELAY = 15;
|
|
constexpr float CORN_AIM_LEAD = 0.1f;
|
|
constexpr float CORN_SHOT_SPEED = 10;
|
|
constexpr float CONTROLLER_DEADZONE = 0.1f;
|
|
constexpr ColorMod DIP_SPLAT = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.25490198f, 0.16078432f, 0.13725491f}};
|
|
constexpr ColorMod BROWNIE_SPLAT = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.6f, 0.4f, 0}};
|
|
|
|
// A hop: a random way (away from its target while it fears it), faster for the corn dips.
|
|
static void dip_hop(Entity_NPC& npc)
|
|
{
|
|
npc.state = NPC_STATE_MOVE;
|
|
bool isFleeing = npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && npc.controllerID == -1;
|
|
Vector direction;
|
|
if (isFleeing) direction = vector_normalized(vector_sub(npc.pos, npc.GetPlayerTarget()->pos));
|
|
else
|
|
{
|
|
direction = random_direction();
|
|
if (npc.controllerID != -1) direction = npc.GetControllerInput(0);
|
|
}
|
|
|
|
float speed = random_unit() * HOP_SPEED_RANGE + HOP_SPEED_MIN;
|
|
bool isDoubled = npc.variant == DIP_CORN || (isFleeing && npc.variant == DIP_BROWNIE);
|
|
Vector hop = vector_scale(direction, isDoubled ? speed * 2 : speed);
|
|
if (npc.friction != 0)
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(hop, npc.speedMultiplier / npc.friction));
|
|
}
|
|
|
|
// The big corn dip's dying shot, aimed where its target is heading.
|
|
static void dip_corn_shoot(Entity_NPC& npc)
|
|
{
|
|
Entity* target = npc.GetPlayerTarget();
|
|
float distance = vector_length(vector_sub(npc.pos, target->pos));
|
|
Vector lead = vector_add(target->pos, vector_scale(target->velocity, CORN_AIM_LEAD * distance));
|
|
ProjectileParams params;
|
|
params.variant = PROJECTILE_CORN;
|
|
npc.FireProjectiles(npc.pos, vector_scale(vector_normalized(vector_sub(lead, npc.pos)), CORN_SHOT_SPEED),
|
|
PROJECTILES_SINGLE, params);
|
|
}
|
|
|
|
// 00534860 The Dip's AI: it rests, hops about (fleeing what it fears), slides to a stop and leaves poop splats. Dirty
|
|
// Mind and the Dipping trinket turn it into the players' own dip.
|
|
void npc_dip_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.type != ENTITY_DIP) reconstruction_missing("npc_dip_update 00534860: the other dip kin");
|
|
|
|
PlayerManager& players = g_game->playerManager;
|
|
bool isTamed = players.FirstCollectibleOwner(COLLECTIBLE_DIRTY_MIND_ID, LAZARUS_SHARE_CHECK) ||
|
|
players.AnyoneHasTrinket(TRINKET_DIPPING_TRINKET_ID);
|
|
if (!npc.isDead && isTamed)
|
|
{
|
|
Entity* owner = players.RandomCollectibleOwner(COLLECTIBLE_DIRTY_MIND_ID, npc.initSeed, nullptr);
|
|
if (!owner) owner = players.RandomTrinketOwner(TRINKET_DIPPING_TRINKET_ID, npc.initSeed, nullptr);
|
|
if (owner)
|
|
{
|
|
npc.entityCollisionClass = ENTCOLL_NONE;
|
|
npc.Remove();
|
|
bool isCorn = npc.variant == DIP_CORN || npc.variant == DIP_BIG_CORN;
|
|
int dip = npc.variant == DIP_BROWNIE ? FAMILIAR_DIP_BROWNIE : isCorn ? FAMILIAR_DIP_CORN : 0;
|
|
Entity_Familiar::ThrowDip(dip, npc.pos, owner);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (npc.state == NPC_STATE_INIT)
|
|
{
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.splatColor = npc.variant == DIP_BROWNIE ? BROWNIE_SPLAT : DIP_SPLAT;
|
|
return;
|
|
}
|
|
bool isFloating = npc.variant == DIP_BIG_CORN && g_game->room->waterAmount > 0;
|
|
npc.entityGridCollisionClass = isFloating ? GRIDCOLL_NOPITS : GRIDCOLL_GROUND;
|
|
|
|
if (npc.state == NPC_STATE_IDLE || npc.state == NPC_STATE_SPECIAL)
|
|
{
|
|
npc.sprite.Play("Idle");
|
|
bool isHopping = npc.state == NPC_STATE_SPECIAL ||
|
|
(npc.controllerID == -1 ? Random() % HOP_ODDS == 0
|
|
: !vector_is_zero(npc.GetControllerInput(CONTROLLER_DEADZONE)));
|
|
if (isHopping) dip_hop(npc);
|
|
npc.friction *= HOP_FRICTION;
|
|
}
|
|
else if (npc.state == NPC_STATE_MOVE)
|
|
{
|
|
npc.sprite.Play("Move");
|
|
npc.friction *= SLIDE_FRICTION;
|
|
if (vector_length_squared(npc.velocity) < 1) npc.state = NPC_STATE_IDLE;
|
|
}
|
|
|
|
npc.sprite.isFlipX = npc.velocity.x < 0;
|
|
if (Random() % SPLAT_ODDS == 0)
|
|
npc.MakeSplat(random_unit() * SPLAT_SIZE_RANGE + SPLAT_SIZE_MIN)
|
|
->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
if (npc.pendingDamage > 0 && (Random() & 1) == 0)
|
|
npc.PlaySound(SOUND_DIP_HURT, HURT_VOLUME, HURT_SOUND_DELAY, false, 1);
|
|
if (npc.variant == DIP_BIG_CORN && npc.controllerID != -1)
|
|
reconstruction_missing("npc_dip_update 00534860: a controlled corn dip bursting");
|
|
if (npc.isDead && npc.variant == DIP_BIG_CORN && !npc.HasEntityFlags(FLAG_SHRINK)) dip_corn_shoot(npc);
|
|
}
|