Curation round 10: combat, pathfinding, death and game over, items and stats, room state, Continue, Lua callbacks

Seven agents took over tear and contact collision, NPC death with blood and gibs, champions, PathFinder, the Gaper's
kin, the player's death with the game over screen, item pedestals with collectible pickup, AddCollectible and
EvaluateItems, room state save and restore, GameState::read, pressure plates, fireplaces, entity lights, ambient
sounds, and one Lua callback enum with gated runners. Integration reconstructed Room::Update's loot preview and
Entity_NPC::UpdatePickupGhosts and replaced a leftover Sacred Orb stub that stopped every pedestal roll.
Under gdb: Gapers die with blood and gibs, a cleared room opens its doors, the treasure room's item is taken (banner,
costume, stats: a Magic Mushroom gives damage 3.5 -> 6.12 and a heart), death leads to the game over diary, and a saved
run is read back on the next boot with Continue offered. 3028 functions taken over.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-11-20 09:55:14 -05:00
1 parent 48933cd312
commit d8385e6c64
64 files changed
+8665 -3059

No files matched your search

+307 -16
View File
@@ -1,26 +1,87 @@
#include "path_finder.h"
#include "entity_npc.h"
#include "game.h"
#include "log.h"
#include "npc_ai.h"
#include "room.h"
#include <algorithm>
#include <cmath>
#include <queue>
static_assert(PATH_GRID_CELLS == ROOM_GRID_CELLS);
constexpr float GRID_CELL = 40;
constexpr float GRID_TOP = 120;
constexpr float HALF = 0.5f;
constexpr int PATH_ENEMY_TYPE_LAST = 999;
// The grid paths a cell's cost means: a pit band and a blocked band some grid collisions ignore, the highest cost a
// path still crosses, and what a rock crusher pays to walk through rocks and through hazards.
constexpr uint32_t GRID_PATH_BAND = 1000;
constexpr uint32_t GRID_PATH_PIT = 3000;
constexpr int GRID_PATH_IMPASSABLE = 950;
constexpr int GRID_PATH_SLOWING_MAX = 949;
constexpr int CRUSH_COST_EASY = 1;
constexpr int CRUSH_COST_DANGEROUS = 800;
// A body wider than a cell paths with its collision points, and one an odd number of cells wide shifts by about half.
constexpr float LARGE_BODY_PERIOD = 80;
constexpr float ODD_BODY_SHIFT = 19.96f;
// How the cell its owner counts itself in lags behind: 10 a tick, 50 while walking straight, refilled to 20 / speed.
constexpr int CELL_DELAY_TICK = 10;
constexpr int CELL_DELAY_DIRECT_TICK = 50;
constexpr float CELL_DELAY = 20;
constexpr float CELL_DELAY_SPEED_MIN = 0.2f;
// Every 10 to 19 ticks the cost map is flooded again, each step costing 1, or 7 into a cell with a cost.
constexpr int REPATH_MIN = 10;
constexpr int REPATH_RANGE = 10;
constexpr int STEP_COST = 1;
constexpr int STEP_COST_ROUGH = 7;
// How often it looks ahead for slowing cells and for a clear line, and how far straight walking reaches.
constexpr int SLOW_PROBE_FRAMES = 3;
constexpr float SLOWDOWN = 0.7f;
constexpr int LINE_CHECK_FRAMES = 10;
constexpr int LINE_CHECK_GROUND = 0;
constexpr float DIRECT_RANGE_NEAR = 70;
constexpr float DIRECT_RANGE_FAR = 750;
// The steering: no target brakes, an arrival brakes harder, else a push of a tenth of the distance up to 2.
constexpr float FRICTION_IDLE = 0.85f;
constexpr float FRICTION_ARRIVED = 0.5f;
constexpr float FRICTION_MOVING = 0.7f;
constexpr float ARRIVE_DISTANCE = 1;
constexpr float PUSH_PER_DISTANCE = 0.1f;
constexpr float PUSH_MAX = 2;
// The cells left of, above, right of and below a cell, each -1 off the grid.
static std::array<int, PATH_DIRECTION_COUNT> grid_neighbors_get(Room& room, int gridIndex)
{
int column = gridIndex % room.gridWidth;
int row = gridIndex / room.gridWidth;
return {room.GetGridIndexByTile(column - 1, row), room.GetGridIndexByTile(column, row - 1),
room.GetGridIndexByTile(column + 1, row), room.GetGridIndexByTile(column, row + 1)};
}
// 004cdd30 Raises a cell's path marker (its cost below the thousands) to marker, never past the impassable ones.
bool room_grid_path_mark(Room& room, int gridIndex, int marker)
{
if ((uint32_t)gridIndex >= ROOM_GRID_CELLS || marker > GRID_PATH_IMPASSABLE) return false;
int& path = room.gridPaths[gridIndex];
if (path % (int)GRID_PATH_BAND < marker) path = path - path % (int)GRID_PATH_BAND + marker;
return true;
}
// 007e3260 Forgets every path, target and evasion, and its grid cell.
void PathFinder::Reset()
{
gridIndex = -1;
pathStep = 0;
gridIndexDelay = 0;
moveCountdown = 0;
hasDirectPath = false;
isPathFound = false;
gridPathTarget = {};
evadeMovementCountdown = 0;
evadeDistance = 0;
stuckFrames = 0;
evadeDistance = 0;
visitedGridIndices.clear();
direction = PATH_DIRECTION_NONE;
directionTimer = 0;
@@ -40,11 +101,7 @@ void PathFinder::ResetMovementTarget()
// 007e6ff0 Notes the grid cell its owner stands in, or -1 outside the grid.
void PathFinder::UpdateGridIndex()
{
Room& room = *g_game->room;
int column = (int)((entity->pos.x - GRID_CELL) / GRID_CELL + HALF);
int row = (int)((entity->pos.y - GRID_TOP) / GRID_CELL + HALF);
bool isInside = column >= 0 && column < room.gridWidth && row >= 0 && row < room.gridHeight;
gridIndex = isInside ? room.gridWidth * row + column : -1;
gridIndex = g_game->room->GetGridIndex(entity->pos);
}
// 007e3340 Whether its owner paths as the players' side: charmed, the Blood Puppy, after a baited target, or an
@@ -60,10 +117,244 @@ bool PathFinder::is_entity_friendly()
return npc->controllerID != -1 || npc->forcedTarget;
}
// 007e33c0
void PathFinder::FindGridPath(const Vector&, float, int, bool)
// 007e7fb0 A grid path as its owner's grid collision feels it: flying over pits drops the pit band, walking on the
// ground keeps all, and anything else drops both the blocked and the pit bands.
int PathFinder::grid_path_felt(uint32_t gridPath)
{
reconstruction_missing("PathFinder::FindGridPath 007e33c0");
if (!entity || entity->entityGridCollisionClass == GRIDCOLL_GROUND) return (int)gridPath;
if (entity->entityGridCollisionClass == GRIDCOLL_NOPITS)
return (int)(gridPath - GRID_PATH_PIT < GRID_PATH_BAND ? gridPath - GRID_PATH_PIT : gridPath);
uint32_t felt = gridPath - GRID_PATH_BAND < GRID_PATH_BAND ? gridPath - GRID_PATH_BAND : gridPath;
return (int)(felt - GRID_PATH_PIT < GRID_PATH_BAND ? felt - GRID_PATH_PIT : felt);
}
// Whether a target's cell is worth remembering: one that is not solid, or for a rock crusher a grid entity that is
// neither easy nor dangerous to crush.
bool PathFinder::is_target_kept(Room& room, int targetIndex)
{
if (!canCrushRocks) return targetIndex >= 0 && room.GetGridPath(targetIndex) < GRID_PATH_SOLID;
GridEntity* grid = room.GetGridEntity(targetIndex);
return grid && !grid->IsEasyCrushableOrWalkable() && !grid->IsDangerousCrushableOrWalkable();
}
// The cost map: the room's grid paths, or for a large body each cell's cost under the first half of its collision
// points (the most blocked of them, a rock crusher paying less for rocks and hazards).
void PathFinder::grid_costs_fill(Room& room, const Vector& offset, bool isLarge)
{
if (!isLarge)
{
gridCosts = room.gridPaths;
return;
}
std::vector<Vector> offsets{offset};
for (const Vector& point : entity->collisionPoints) offsets.push_back(vector_add(offset, point));
gridCosts.fill(0);
for (int cell = 0; cell < room.gridWidth * room.gridHeight; cell++)
{
Vector center = room.GetGridPosition(cell);
for (size_t i = 0; i < offsets.size() / 2; i++)
{
int covered = room.GetGridIndex(vector_add(center, offsets[i]));
int path = room.GetGridPath(covered);
int cost = path;
GridEntity* grid = canCrushRocks ? room.GetGridEntity(covered) : nullptr;
if (grid)
{
if (grid->IsEasyCrushableOrWalkable()) cost = CRUSH_COST_EASY;
if (grid->IsDangerousCrushableOrWalkable()) cost = CRUSH_COST_DANGEROUS;
cost = std::min(cost, path);
}
if (i == 0 || (path > gridCosts[cell] && path > GRID_PATH_FREED)) gridCosts[cell] = cost;
}
}
}
// A large body aiming at a cell it cannot fit into aims at the cheapest cell its collision points reach from there.
// The original reads its countdown as the cost of a point off the grid. Such points are skipped here.
int PathFinder::large_target_get(Room& room, const Vector& position, int targetIndex)
{
if (targetIndex < 0 || gridCosts[targetIndex] <= GRID_PATH_FREED) return targetIndex;
if (room.GetGridPath(targetIndex) > GRID_PATH_FREED) return targetIndex;
Vector center = room.GetGridPosition(room.GetGridIndex(position));
int best = gridCosts[targetIndex];
for (size_t i = 0; i < entity->collisionPoints.size() / 2; i++)
{
int cell = room.GetGridIndex(vector_sub(center, entity->collisionPoints[i]));
if (cell < 0 || gridCosts[cell] >= best) continue;
best = gridCosts[cell];
targetIndex = cell;
}
return targetIndex;
}
// (007e4860, 007e85a0) Floods the cost map from the target, cheapest first, marking each reached cell with its
// negated cost until it reaches its own cell or half the room's cells in cost.
void PathFinder::grid_costs_flood(Room& room, int targetIndex, bool isFriendly)
{
auto isCostlier = [](const PathNode& first, const PathNode& second) {
return first.cost > second.cost;
};
std::priority_queue<PathNode, std::vector<PathNode>, decltype(isCostlier)> open(isCostlier);
open.push({targetIndex, 0});
isPathFound = false;
while (!open.empty() && !isPathFound)
{
PathNode node = open.top();
if (node.cost >= room.gridWidth * room.gridHeight / 2) break;
open.pop();
for (int next : grid_neighbors_get(room, node.gridIndex))
{
if (next < 0 || gridCosts[next] < 0 || gridCosts[next] >= GRID_PATH_IMPASSABLE) continue;
int cost = node.cost + (gridCosts[next] > 0 && !isFriendly ? STEP_COST_ROUGH : STEP_COST);
gridCosts[next] = -cost;
if (next == gridIndex) isPathFound = true;
open.push({next, cost});
}
}
}
// The neighbour of its cell the flood reached cheapest, or its own cell. A crawlspace ladder above is never taken.
int PathFinder::path_next_get(Room& room)
{
std::array<int, PATH_DIRECTION_COUNT> neighbors = grid_neighbors_get(room, gridIndex);
int next = gridIndex;
int best = -GRID_PATH_SOLID;
for (int direction = 0; direction < PATH_DIRECTION_COUNT; direction++)
{
int cell = neighbors[direction];
GridEntity* grid = room.GetGridEntity(cell);
bool isLadder = room.roomType == ROOM_DUNGEON && direction == PATH_DIRECTION_UP && grid &&
grid->type == GRID_GRAVITY;
if (isLadder || cell < 0 || gridCosts[cell] >= 0 || gridCosts[cell] <= best) continue;
best = gridCosts[cell];
next = cell;
}
return next;
}
// Pushes its owner from where it stands toward the spot, braking when there is none or it has arrived.
void PathFinder::path_steer(const Vector& moveTarget, const Vector& from, float speed)
{
if (vector_is_zero(moveTarget))
{
entity->friction *= FRICTION_IDLE;
return;
}
Vector delta = vector_sub(moveTarget, from);
float distance = vector_length(delta);
if (distance <= ARRIVE_DISTANCE)
{
entity->friction *= FRICTION_ARRIVED;
return;
}
float push = std::min(distance * PUSH_PER_DISTANCE, PUSH_MAX) * speed;
entity->friction *= FRICTION_MOVING;
entity->AddVelocity_divided(vector_make(delta.x / distance * push, delta.y / distance * push), false);
}
// 007e33c0 Steers its owner toward position: straight at it when the line is clear and near enough, else down the
// cost map it floods from the target every 10 to 19 ticks. A controlled enemy moves by its player's input instead.
// A versus match's player never paths, so only enemies are tested for a controller.
void PathFinder::FindGridPath(const Vector& position, float speed, int pathMarker, DirectPathMode mode)
{
bool isEnemy = entity->type >= ENTITY_FIRST_ENEMY && entity->type <= PATH_ENEMY_TYPE_LAST;
if (isEnemy && static_cast<Entity_NPC*>(entity)->controllerID != -1)
{
SimulatePlayerMovement(static_cast<Entity_NPC*>(entity)->GetControllerInput(0), 1);
UpdateGridIndex();
hasDirectPath = true;
return;
}
if (entity->HasEntityFlags(FLAG_CONFUSION) && !entity->HasEntityFlags(FLAG_CHARM))
{
MoveRandomly(STATUS_IGNORED);
Vector heading = vector_normalized(entity->velocity);
entity->velocity = {};
entity->AddVelocity_divided(vector_scale(heading, speed), false);
}
Vector body = vector_scale(entity->sizeMulti, entity->size * 2);
bool isLarge = body.x > GRID_CELL_SIZE || body.y > GRID_CELL_SIZE;
bool isFamiliar = entity->type == ENTITY_FAMILIAR && pathMarker <= 0;
bool isFriendly = is_entity_friendly() || pathMarker < 0;
Vector offset{};
if (std::fmod(body.x, LARGE_BODY_PERIOD) > GRID_CELL_SIZE) offset.x = ODD_BODY_SHIFT;
if (std::fmod(body.y, LARGE_BODY_PERIOD) > GRID_CELL_SIZE) offset.y = ODD_BODY_SHIFT;
Vector from = vector_sub(entity->pos, offset);
Room& room = *g_game->room;
int targetIndex = room.GetGridIndex(position);
if (is_target_kept(room, targetIndex)) gridPathTarget = position;
else if (!vector_is_zero(gridPathTarget)) targetIndex = room.GetGridIndex(gridPathTarget);
int ticks = entity->update_ticks_get();
int ownIndex = room.GetGridIndex(from);
Vector ownCenter = room.GetGridPosition(ownIndex);
if (ownIndex != gridIndex && (gridIndexDelay -= ticks * CELL_DELAY_TICK) < 0)
{
gridIndex = ownIndex;
gridIndexDelay = (int)(CELL_DELAY / std::max(speed, CELL_DELAY_SPEED_MIN));
}
float slowdown = 1;
if (!isFamiliar)
{
if (entity->IsFrame(SLOW_PROBE_FRAMES, 1))
{
Vector ahead = vector_add(vector_scale(vector_normalized(entity->velocity), GRID_CELL_SIZE), from);
int felt = grid_path_felt(room.GetGridPath(room.GetGridIndex(ahead)));
if ((uint32_t)(felt - 1) < GRID_PATH_SLOWING_MAX) slowdown = SLOWDOWN;
}
if (pathMarker >= 0 && room.GetGridPath(gridIndex) < GRID_PATH_IMPASSABLE)
room_grid_path_mark(room, gridIndex, pathMarker);
}
if (entity->IsFrame(LINE_CHECK_FRAMES, entity->index))
{
Vector targetCenter = room.GetGridPosition(room.GetGridIndex(position));
int threshold = isFriendly || isFamiliar ? GRID_PATH_FREED : 0;
if (entity->entityGridCollisionClass == GRIDCOLL_WALLS) hasDirectPath = true;
else if (canCrushRocks)
reconstruction_missing("Room::CheckLine 007f0df0: the rocks a rock crusher sees through");
else hasDirectPath = room_check_line(ownCenter, targetCenter, LINE_CHECK_GROUND, threshold);
}
float directRange = mode == DIRECT_PATH_FAR ? DIRECT_RANGE_FAR : DIRECT_RANGE_NEAR;
if (isLarge && mode == DIRECT_PATH_NEAR)
directRange += std::max(entity->size * entity->sizeMulti.x, entity->size * entity->sizeMulti.y);
Vector moveTarget{};
if ((hasDirectPath && vector_length(vector_sub(position, entity->pos)) < directRange) || gridIndex == targetIndex)
{
moveTarget = vector_sub(position, offset);
moveCountdown -= ticks * CELL_DELAY_TICK;
gridIndexDelay -= ticks * CELL_DELAY_DIRECT_TICK;
}
else
{
if (room.GetGridPath(gridIndex) == GRID_PATH_SOLID || moveCountdown > 0) moveCountdown -= ticks;
else
{
moveCountdown = (int)(Random() % REPATH_RANGE) + REPATH_MIN;
grid_costs_fill(room, offset, isLarge);
if (isLarge) targetIndex = large_target_get(room, position, targetIndex);
grid_costs_flood(room, targetIndex, isFriendly);
}
if (isPathFound) moveTarget = room.GetGridPosition(path_next_get(room));
}
path_steer(moveTarget, from, slowdown * speed);
}
// 007e4940
@@ -103,7 +394,7 @@ bool PathFinder::HasPathToPos(const Vector&, bool)
}
// 007e8010
void PathFinder::SimulatePlayerMovement(float*)
void PathFinder::SimulatePlayerMovement(const Vector&, float)
{
reconstruction_missing("PathFinder::SimulatePlayerMovement 007e8010");
}