Curation round 14: console and Lua API, effects, lasers, enemies, eleven bosses, the Depths and Mom, status effects, the playable characters

This commit is contained in:
shweet committed 2026-11-20 19:04:41 -05:00
1 parent 8ae02a8422
commit 6e3da161fc
138 files changed
+16754 -6825

No files matched your search

+194 -4
View File
@@ -1,4 +1,5 @@
#include "path_finder.h"
#include "boss_ai.h"
#include "game.h"
#include "npc_ai.h"
#include "npc_basement.h"
@@ -20,6 +21,28 @@ constexpr float WANDER_STALL_SPEED_SQUARED = 0.1f;
constexpr int WANDER_GIVE_UP_ODDS = 10;
constexpr float WANDER_FRIENDLY_SPEED = 0.5f;
// How an owner wanders along the axes: up to 10 tries a call at a run of up to 11 cells (1 to 4 while it flees) that
// ends before a cell felt over 300 (998 on the last try, 800 while fleeing). A cell marked 999 is never a spot.
constexpr int AXIS_ATTEMPTS = 10;
constexpr uint32_t AXIS_RUN_CELLS = 12;
constexpr int AXIS_BLOCKING_PATH = 300;
constexpr int AXIS_LAST_TRY_BLOCKING_PATH = 998;
constexpr int AXIS_FLEE_BLOCKING_PATH = 800;
constexpr int AXIS_AVOIDED_PATH = 999;
constexpr int AXIS_SIDE_OPEN_PATH = 200;
constexpr uint32_t AXIS_SIDE_ODDS = 10;
constexpr uint32_t AXIS_SHORT_ODDS = 10;
constexpr uint32_t AXIS_TURN_BACK_ODDS = 5;
constexpr float AXIS_FLEE_AWAY_DOT = 0.95f;
constexpr float AXIS_ARRIVED = 5;
constexpr float AXIS_STALL_SPEED = 0.6f;
constexpr int AXIS_STALL_CHECKS = 10;
constexpr int AXIS_HAZARD_PATH = 700;
constexpr float AXIS_HAZARD_SPEED = 3;
constexpr int AXIS_HAZARD_CHECKS = 2;
constexpr float AXIS_FOLLOW_NEAR_SQUARED = 1600;
constexpr float AXIS_FOLLOW_FRICTION = 0.8f;
// How an owner evades: within 250 of its target (170 when the target cannot walk to it) it keeps fleeing for three
// checks of every 14 frames, pushing away at 1.5 and trying up to 7 random cells 30 to 480 past a point 30 away
// (fewer as the evasion widens toward 20) that lie farther from the target than it by 100 and a third of their reach.
@@ -124,7 +147,7 @@ void PathFinder::Reset()
visitedGridIndices.clear();
direction = PATH_DIRECTION_NONE;
directionTimer = 0;
targetGridIndex = -1;
lastMoveFrame = -1;
canCrushRocks = false;
movementTarget = {};
moveDirection = {};
@@ -553,10 +576,177 @@ bool PathFinder::MoveRandomly(StatusMode mode)
return false;
}
// 007e5340
void PathFinder::MoveRandomlyAxisAligned(float, StatusMode)
// A run of cells straight from the owner in a direction: how far it reaches before a cell it would not cross (none past
// the room's box), and none when both cells beside its end are blocked, unless one time in ten.
static int axis_run_get(PathFinder& finder, int direction, int attemptsLeft)
{
reconstruction_missing("PathFinder::MoveRandomlyAxisAligned 007e5340");
Entity* entity = finder.entity;
Room& room = *g_game->room;
int cells = (int)(Random() % AXIS_RUN_CELLS);
bool isFleeing = entity->HasEntityFlags(FLAG_FEAR | FLAG_SHRINK);
if (isFleeing) cells = (int)(Random() & 3) + 1;
Vector axis = vector_from_direction(direction);
Vector end = vector_add(entity->pos, vector_scale(axis, (float)cells * GRID_CELL_SIZE));
bool isInside = end.x <= room.bottomRightPos.x && end.x >= room.topLeftPos.x && end.y <= room.bottomRightPos.y &&
end.y >= room.topLeftPos.y;
if (!isInside || vector_is_zero(end)) return 0;
for (int step = 1; step < cells; step++)
{
Vector cell = vector_add(entity->pos, vector_scale(axis, (float)step * GRID_CELL_SIZE));
int path = finder.grid_path_felt(room.GetGridPath(room.GetGridIndex(cell)));
bool isBlocked = path > AXIS_BLOCKING_PATH && (attemptsLeft > 0 || path > AXIS_LAST_TRY_BLOCKING_PATH) &&
(!isFleeing || path > AXIS_FLEE_BLOCKING_PATH);
if (!isBlocked) continue;
cells = step - 1;
break;
}
end = vector_add(entity->pos, vector_scale(axis, (float)cells * GRID_CELL_SIZE));
bool isOpen = false;
for (int side : {direction + 3, direction + 1})
{
Vector step = vector_scale(vector_from_direction(side % PATH_DIRECTION_COUNT), GRID_CELL_SIZE);
Vector beside = vector_add(end, step);
isOpen = isOpen || finder.grid_path_felt(room.GetGridPath(room.GetGridIndex(beside))) < AXIS_SIDE_OPEN_PATH;
}
return isOpen || Random() % AXIS_SIDE_ODDS == 0 ? cells : 0;
}
// The direction of the longest run, looked over from a random start. A fleeing NPC takes the longest run away from its
// target, else across it. best is the run's length.
static int axis_direction_pick(PathFinder& finder, const std::array<int, PATH_DIRECTION_COUNT>& runs, int& best)
{
Entity* entity = finder.entity;
auto* npc = dynamic_cast<Entity_NPC*>(entity);
int chosen = PATH_DIRECTION_NONE;
int start = (int)(Random() & 3);
auto longest_take = [&](int direction) {
if (runs[direction] <= best) return;
best = runs[direction];
chosen = direction;
};
if (npc && entity->HasEntityFlags(FLAG_FEAR | FLAG_SHRINK))
{
Vector toward = vector_sub(npc->GetPlayerTarget()->pos, entity->pos);
for (int i = start; i < start + PATH_DIRECTION_COUNT; i++)
if (vector_dot(toward, vector_from_direction(i & 3)) <= 0) longest_take(i & 3);
int across = std::abs(toward.y) <= std::abs(toward.x) ? PATH_DIRECTION_UP : PATH_DIRECTION_LEFT;
if (best == 0)
for (int direction = across; direction < PATH_DIRECTION_COUNT; direction += 2) longest_take(direction);
if (best != 0) return chosen;
}
for (int i = start; i < start + PATH_DIRECTION_COUNT; i++) longest_take(i & 3);
return chosen;
}
// One try at a run to wander: the longest, though one under two cells only one time in ten and a turn back only one
// time in five (a fleeing NPC takes either, but no run within 0.95 of straight at its target). Its end cell becomes
// the spot to reach unless marked avoided or not free, and the owner stops across the run's axis.
static void axis_run_try(PathFinder& finder, int attemptsLeft)
{
Entity* entity = finder.entity;
Room& room = *g_game->room;
std::array<int, PATH_DIRECTION_COUNT> runs{};
for (int direction = 0; direction < PATH_DIRECTION_COUNT; direction++)
runs[direction] = axis_run_get(finder, direction, attemptsLeft);
int best = 0;
int chosen = axis_direction_pick(finder, runs, best);
bool isFleeing = entity->HasEntityFlags(FLAG_FEAR | FLAG_SHRINK);
if (best < 2 && Random() % AXIS_SHORT_ODDS != 0 && !isFleeing) chosen = PATH_DIRECTION_NONE;
bool isTaken = std::abs(finder.direction - chosen) != 2 || Random() % AXIS_TURN_BACK_ODDS == 0 || isFleeing;
finder.directionTimer = 0;
if (!isTaken || chosen < 0) return;
Vector axis = vector_from_direction(chosen);
if (isFleeing)
{
Vector toward = vector_sub(static_cast<Entity_NPC*>(entity)->GetPlayerTarget()->pos, entity->pos);
if (vector_dot(vector_normalized(toward), axis) > AXIS_FLEE_AWAY_DOT) return;
}
Vector end = vector_add(entity->pos, vector_scale(axis, (float)best * GRID_CELL_SIZE));
Vector center = room.GetGridPosition(room.GetGridIndex(end));
int cell = room.GetGridIndex(center);
if (room.GetGridPath(cell) == AXIS_AVOIDED_PATH) return;
finder.direction = chosen;
auto collision = (GridCollision)room.GetGridCollision(cell);
bool isFree = collision == COLLISION_NONE ||
(entity->entityGridCollisionClass == GRIDCOLL_WALLS && collision != COLLISION_WALL);
if (!isFree) return;
finder.movementTarget = center;
if (std::abs(axis.x) <= std::abs(axis.y)) entity->velocity.x = 0;
else entity->velocity.y = 0;
}
// Drives toward the spot to reach, kept in the room's box. It drops the spot after a skipped frame, once within 5,
// once slower than 0.6 for over 10 checks, or once heading at over 3 into a cell it feels over 700 for over 2 checks.
static void axis_steer(PathFinder& finder, float speed, int elapsed)
{
Entity* entity = finder.entity;
Room& room = *g_game->room;
Vector& target = finder.movementTarget;
if (elapsed > 1)
{
target = {};
return;
}
target.x = std::min(std::max(target.x, room.topLeftPos.x), room.bottomRightPos.x);
target.y = std::min(std::max(target.y, room.topLeftPos.y), room.bottomRightPos.y);
Vector away = vector_sub(vector_add(entity->pos, entity->velocity), target);
if (vector_length(away) < AXIS_ARRIVED) target = {};
else if (vector_length(entity->velocity) < AXIS_STALL_SPEED && ++finder.directionTimer > AXIS_STALL_CHECKS)
target = {};
away = vector_normalized(away);
if (entity->friction != 0)
{
entity->velocity.y += entity->speedMultiplier * -away.y * speed / entity->friction;
entity->velocity.x += entity->speedMultiplier * -away.x * speed / entity->friction;
}
Vector ahead = vector_add(entity->pos, vector_scale(vector_normalized(entity->velocity), GRID_CELL_SIZE));
int felt = finder.grid_path_felt(room.GetGridPath(room.GetGridIndex(ahead)));
if (felt > AXIS_HAZARD_PATH && vector_length(entity->velocity) > AXIS_HAZARD_SPEED &&
++finder.directionTimer > AXIS_HAZARD_CHECKS)
target = {};
}
// 007e5340 Wanders along the axes: up to 10 tries a call at a run of cells left, up, right or down to a free cell,
// then drives there at speed. A friendly owner paths to its target instead, sliding along the axis it leans on most,
// and holds still near a player it follows.
void PathFinder::MoveRandomlyAxisAligned(float speed, StatusMode mode)
{
if (entity->minecart) return;
if (mode == STATUS_OBEYED && is_entity_friendly())
{
auto* npc = static_cast<Entity_NPC*>(entity);
Vector target = npc->CalcTargetPosition(0);
bool isNear = npc->HasEntityFlags(FLAG_FRIENDLY) && npc->GetPlayerTarget()->type == ENTITY_PLAYER &&
vector_length_squared(vector_sub(npc->pos, target)) < AXIS_FOLLOW_NEAR_SQUARED;
if (isNear)
{
npc->friction *= AXIS_FOLLOW_FRICTION;
return;
}
FindGridPath(npc->CalcTargetPosition(0), speed, 0, DIRECT_PATH_FAR);
npc->velocity = vector_scale(vector_cardinal(npc->velocity), vector_length(npc->velocity));
return;
}
int elapsed = g_game->frameCount - lastMoveFrame;
lastMoveFrame = g_game->frameCount;
for (int attempts = AXIS_ATTEMPTS; vector_is_zero(movementTarget) && attempts > 0; attempts--)
axis_run_try(*this, attempts - 1);
if (!vector_is_zero(movementTarget)) axis_steer(*this, speed, elapsed);
}
// 007e6650