Curation round 12: basement enemies, projectiles, tear flags, lighting and blend modes, actives, boss deaths, familiars, shops and secret rooms
This commit is contained in:
1 parent
698eeb74e9
commit
f8ce24fc6c
146 files changed
+16389
-3104
No files matched your search
+189
-8
@@ -1,6 +1,7 @@
|
||||
#include "path_finder.h"
|
||||
#include "game.h"
|
||||
#include "npc_ai.h"
|
||||
#include "npc_basement.h"
|
||||
#include "room.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -10,6 +11,43 @@ static_assert(PATH_GRID_CELLS == ROOM_GRID_CELLS);
|
||||
|
||||
constexpr int PATH_ENEMY_TYPE_LAST = 999;
|
||||
|
||||
// How an owner wanders: to a free spot 40 away, pushed at half its speed, until within 3 of it, against a wall, or
|
||||
// (one time in ten) once slower than about 0.3. A friendly one paths to its target instead.
|
||||
constexpr float WANDER_STEP = 40;
|
||||
constexpr float WANDER_PUSH = 0.5f;
|
||||
constexpr float WANDER_ARRIVED = 3;
|
||||
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 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.
|
||||
constexpr int EVADE_CHECK_FRAMES = 14;
|
||||
constexpr int EVADE_FORGET_FRAMES = 30;
|
||||
constexpr int EVADE_RECHECK_FRAMES = 10;
|
||||
constexpr int EVADE_COUNTDOWN = 3;
|
||||
constexpr float EVADE_RANGE_SEEN = 250;
|
||||
constexpr float EVADE_RANGE_HIDDEN = 170;
|
||||
constexpr float EVADE_WIDE = 20;
|
||||
constexpr float EVADE_WIDEN = 0.35f;
|
||||
constexpr float EVADE_DECAY = 0.9f;
|
||||
constexpr float EVADE_DIAGONAL = 0.25f;
|
||||
constexpr float EVADE_PUSH = 1.5f;
|
||||
constexpr float EVADE_BASE_REACH = 30;
|
||||
constexpr float EVADE_ATTEMPTS = 7;
|
||||
constexpr float EVADE_ATTEMPT_SHARE = 0.3f;
|
||||
constexpr float EVADE_REVISIT_REFUND = 0.7f;
|
||||
constexpr float EVADE_REACH_MIN = 30;
|
||||
constexpr float EVADE_REACH_RANGE = 450;
|
||||
constexpr float EVADE_REACH_SHARE = 3;
|
||||
constexpr float EVADE_FLATTEN = 0.5f;
|
||||
constexpr float EVADE_MARGIN = 100;
|
||||
constexpr int EVADE_BAR_STEP = 10;
|
||||
constexpr float EVADE_GRID_TOP = 120;
|
||||
constexpr float EVADE_SPOT_RANGE_SQUARED = 40000;
|
||||
constexpr float EVADE_PATH_SPEED = 1.079f;
|
||||
|
||||
// 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;
|
||||
@@ -39,7 +77,6 @@ constexpr int STEP_COST_ROUGH = 7;
|
||||
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;
|
||||
|
||||
@@ -328,7 +365,7 @@ void PathFinder::FindGridPath(const Vector& position, float speed, int pathMarke
|
||||
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);
|
||||
else hasDirectPath = room_check_line(ownCenter, targetCenter, LINE_MODE_GROUND, threshold);
|
||||
}
|
||||
|
||||
float directRange = mode == DIRECT_PATH_FAR ? DIRECT_RANGE_FAR : DIRECT_RANGE_NEAR;
|
||||
@@ -357,16 +394,160 @@ void PathFinder::FindGridPath(const Vector& position, float speed, int pathMarke
|
||||
path_steer(moveTarget, from, slowdown * speed);
|
||||
}
|
||||
|
||||
// 007e4940
|
||||
void PathFinder::EvadeTarget(const Vector&, StatusMode)
|
||||
// Whether the owner's periodic checks run this frame: every period frames by its index, not while paused or
|
||||
// interpolating.
|
||||
static bool is_evade_check_frame(Entity& entity, int period)
|
||||
{
|
||||
reconstruction_missing("PathFinder::EvadeTarget 007e4940");
|
||||
if (entity.HasEntityFlags(FLAG_INTERPOLATION_UPDATE) || g_game->IsPaused()) return false;
|
||||
return ((int)entity.index + g_game->frameCount) % period == 0;
|
||||
}
|
||||
|
||||
// 007e6300
|
||||
bool PathFinder::MoveRandomly(StatusMode)
|
||||
// One try at a spot to flee to: a random cell within 30 to 480 of base, not tried this half second, free and in
|
||||
// walking reach. It is taken while the target cannot walk to the owner (or the evasion is wide) and it lies far
|
||||
// enough from the target. Each taken spot raises the bar for the next.
|
||||
static void evade_spot_try(PathFinder& finder, const Vector& targetPos, const Vector& base, float distance,
|
||||
float& attempt)
|
||||
{
|
||||
reconstruction_missing("PathFinder::MoveRandomly 007e6300");
|
||||
Entity* entity = finder.entity;
|
||||
Room& room = *g_game->room;
|
||||
float reach = random_unit() * EVADE_REACH_RANGE + EVADE_REACH_MIN;
|
||||
float angle = random_unit() * RANDOM_HALF_TURN;
|
||||
angle += angle;
|
||||
Vector spot = vector_make(std::cos(angle) * reach + base.x, std::sin(angle) * EVADE_FLATTEN * reach + base.y);
|
||||
int column = (int)((spot.x - GRID_CELL_SIZE) / GRID_CELL_SIZE + 0.5f);
|
||||
int row = (int)((spot.y - EVADE_GRID_TOP) / GRID_CELL_SIZE + 0.5f);
|
||||
bool isInside = column >= 0 && column < room.gridWidth && row >= 0 && row < room.gridHeight;
|
||||
int cell = isInside ? room.gridWidth * row + column : -1;
|
||||
if (!finder.visitedGridIndices.insert(cell).second)
|
||||
{
|
||||
attempt -= EVADE_REVISIT_REFUND;
|
||||
return;
|
||||
}
|
||||
|
||||
Vector center = vector_make((float)(cell % room.gridWidth) * GRID_CELL_SIZE + GRID_CELL_SIZE,
|
||||
(float)(cell / room.gridWidth) * GRID_CELL_SIZE + EVADE_GRID_TOP);
|
||||
if (room_grid_collision_at(center) != 0 || !room_check_line(entity->pos, center, LINE_MODE_CELL, 0)) return;
|
||||
if (room_check_line(targetPos, entity->pos, LINE_MODE_CELL, 0) && finder.evadeDistance < EVADE_WIDE)
|
||||
{
|
||||
finder.evadeDistance += EVADE_WIDEN;
|
||||
return;
|
||||
}
|
||||
|
||||
float bar = reach / EVADE_REACH_SHARE + distance + EVADE_MARGIN + finder.evadeDistance;
|
||||
bar -= (float)finder.stuckFrames;
|
||||
if (bar <= vector_length(vector_sub(targetPos, center)))
|
||||
{
|
||||
finder.movementTarget = center;
|
||||
if (finder.evadeDistance < EVADE_WIDE) finder.evadeDistance *= 0.5f;
|
||||
}
|
||||
finder.stuckFrames += EVADE_BAR_STEP;
|
||||
}
|
||||
|
||||
// 007e4940 Flees targetPos: within 250 of it (170 out of its walking sight) it keeps evading for 3 more checks of
|
||||
// every 14 frames, pushing away and seeking a cell to run to that lies well away from the target, and it wanders
|
||||
// once the evasion runs out. A friendly owner paths to its target instead. stuckFrames holds the bar a spot must clear.
|
||||
void PathFinder::EvadeTarget(const Vector& targetPos, StatusMode mode)
|
||||
{
|
||||
if (mode == STATUS_OBEYED && is_entity_friendly())
|
||||
{
|
||||
FindGridPath(static_cast<Entity_NPC*>(entity)->GetPlayerTarget()->pos, EVADE_PATH_SPEED, 0, DIRECT_PATH_FAR);
|
||||
return;
|
||||
}
|
||||
|
||||
Vector delta = vector_sub(targetPos, entity->pos);
|
||||
float distance = vector_length(delta);
|
||||
evadeDistance = std::min(evadeDistance, EVADE_WIDE);
|
||||
if (is_evade_check_frame(*entity, EVADE_CHECK_FRAMES))
|
||||
{
|
||||
float range = room_check_line(entity->pos, targetPos, LINE_MODE_CELL, 0) ? EVADE_RANGE_SEEN : EVADE_RANGE_HIDDEN;
|
||||
if (distance >= range) evadeMovementCountdown -= entity->update_ticks_get();
|
||||
else
|
||||
{
|
||||
if (evadeMovementCountdown < 1) movementTarget = {};
|
||||
evadeMovementCountdown = EVADE_COUNTDOWN;
|
||||
}
|
||||
}
|
||||
if (is_evade_check_frame(*entity, EVADE_FORGET_FRAMES)) visitedGridIndices.clear();
|
||||
if (evadeMovementCountdown < 1)
|
||||
{
|
||||
stuckFrames = 0;
|
||||
evadeDistance *= EVADE_DECAY;
|
||||
MoveRandomly(STATUS_IGNORED);
|
||||
entity->friction *= EVADE_DECAY;
|
||||
return;
|
||||
}
|
||||
|
||||
if (vector_is_zero(movementTarget) || evadeDistance > EVADE_WIDE)
|
||||
{
|
||||
Vector away = distance > 0 ? vector_make(delta.x / -distance, delta.y / -distance) : Vector{};
|
||||
if (std::abs(std::abs(away.x) - std::abs(away.y)) < EVADE_DIAGONAL)
|
||||
{
|
||||
if ((Random() & 1) == 0) away.x *= 0.5f;
|
||||
else away.y *= 0.5f;
|
||||
}
|
||||
if (entity->friction != 0)
|
||||
{
|
||||
entity->velocity.x += away.x * EVADE_PUSH * entity->speedMultiplier / entity->friction;
|
||||
entity->velocity.y += away.y * EVADE_PUSH * entity->speedMultiplier / entity->friction;
|
||||
}
|
||||
Vector base = vector_add(entity->pos, vector_scale(away, EVADE_BASE_REACH));
|
||||
for (float attempt = 0; attempt < EVADE_ATTEMPTS - evadeDistance * EVADE_ATTEMPT_SHARE; attempt += 1)
|
||||
evade_spot_try(*this, targetPos, base, distance, attempt);
|
||||
}
|
||||
if (vector_is_zero(movementTarget)) return;
|
||||
|
||||
if (is_evade_check_frame(*entity, EVADE_RECHECK_FRAMES))
|
||||
{
|
||||
bool isDropped = room_check_line(targetPos, movementTarget, LINE_MODE_CELL, 0) && evadeDistance < EVADE_WIDE;
|
||||
Vector fromTarget = vector_sub(targetPos, movementTarget);
|
||||
isDropped = isDropped || vector_length_squared(fromTarget) < EVADE_SPOT_RANGE_SQUARED ||
|
||||
std::abs(fromTarget.x) < EVADE_WIDE || std::abs(fromTarget.y) < EVADE_WIDE;
|
||||
if (isDropped) movementTarget = {};
|
||||
}
|
||||
if (!vector_is_zero(movementTarget)) FindGridPath(movementTarget, EVADE_PATH_SPEED, 0, DIRECT_PATH_FAR);
|
||||
}
|
||||
|
||||
// 007e6300 Wanders: picks a free spot 40 away when it has none and drifts toward it, dropping it once there, against a
|
||||
// wall, or now and then once stalled. Unless told to ignore its status, a friendly owner paths to its target and a
|
||||
// feared or shrunk one flees instead. Whether it finished a wander (or did something else).
|
||||
bool PathFinder::MoveRandomly(StatusMode mode)
|
||||
{
|
||||
if (entity->minecart) return true;
|
||||
if (mode == STATUS_OBEYED && is_entity_friendly())
|
||||
{
|
||||
FindGridPath(static_cast<Entity_NPC*>(entity)->CalcTargetPosition(0), WANDER_FRIENDLY_SPEED, 0, DIRECT_PATH_FAR);
|
||||
return true;
|
||||
}
|
||||
if (mode == STATUS_OBEYED && entity->HasEntityFlags(FLAG_FEAR | FLAG_SHRINK))
|
||||
{
|
||||
EvadeTarget(static_cast<Entity_NPC*>(entity)->GetPlayerTarget()->pos, STATUS_OBEYED);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vector_is_zero(movementTarget))
|
||||
{
|
||||
float angle = random_unit() * RANDOM_HALF_TURN;
|
||||
angle += angle;
|
||||
Vector spot = vector_make(std::cos(angle) * WANDER_STEP + entity->pos.x,
|
||||
std::sin(angle) * WANDER_STEP + entity->pos.y);
|
||||
if (room_grid_collision_at(spot) == 0) movementTarget = spot;
|
||||
}
|
||||
if (vector_is_zero(movementTarget)) return false;
|
||||
|
||||
Vector delta = vector_sub(movementTarget, entity->pos);
|
||||
float distance = vector_length(delta);
|
||||
bool isStalled = vector_length_squared(entity->velocity) < WANDER_STALL_SPEED_SQUARED;
|
||||
if (distance < WANDER_ARRIVED || entity->isCollidesWithGrid || (isStalled && Random() % WANDER_GIVE_UP_ODDS == 0))
|
||||
{
|
||||
movementTarget = {};
|
||||
return true;
|
||||
}
|
||||
if (distance > 0 && entity->friction != 0)
|
||||
{
|
||||
entity->velocity.x += delta.x / distance * WANDER_PUSH * entity->speedMultiplier / entity->friction;
|
||||
entity->velocity.y += delta.y / distance * WANDER_PUSH * entity->speedMultiplier / entity->friction;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 007e5340
|
||||
|
||||
Reference in new issue
Block a user