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:
1 parent
8ae02a8422
commit
6e3da161fc
138 files changed
+16754
-6825
No files matched your search
@@ -0,0 +1,348 @@
|
||||
#include "homing_laser.h"
|
||||
#include "boss_ai.h"
|
||||
#include "entity_laser.h"
|
||||
#include "entity_player.h"
|
||||
#include "entity_tear.h"
|
||||
#include "log.h"
|
||||
#include "player_manager.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
constexpr float BEZIER_HALF = 0.5f;
|
||||
constexpr int BEZIER_LENGTH_STEPS = 10;
|
||||
constexpr float SMOOTHING_FULL_LENGTH = 200;
|
||||
constexpr float PLAYER_CUT_DISTANCE_SQUARED = 100;
|
||||
constexpr uint32_t PLAYER_CUT_FIRST_SAMPLE = 2;
|
||||
constexpr float GRID_HALF_CELL = GRID_CELL_SIZE / 2;
|
||||
|
||||
// A point on the cubic curve from one bezier point to the next, t from 0 to 1.
|
||||
static Vector bezier_evaluate(const BezierPoint& from, const BezierPoint& to, float t)
|
||||
{
|
||||
float u = 1 - t;
|
||||
float weight0 = u * u * u;
|
||||
float weight1 = u * u * 3 * t;
|
||||
float weight2 = u * 3 * t * t;
|
||||
float weight3 = t * t * t;
|
||||
return vector_make(
|
||||
weight0 * from.point.x + weight1 * from.handleOut.x + weight2 * to.handleIn.x + weight3 * to.point.x,
|
||||
weight0 * from.point.y + weight1 * from.handleOut.y + weight2 * to.handleIn.y + weight3 * to.point.y);
|
||||
}
|
||||
|
||||
// 0099c340 Where a point lands on a segment, clamped to it (0 for a segment of no length), and how far from there.
|
||||
SegmentProjection segment_project_point(const Vector& point, const Vector& start, const Vector& end)
|
||||
{
|
||||
Vector segment = vector_sub(end, start);
|
||||
float lengthSquared = vector_length_squared(segment);
|
||||
float t = 0;
|
||||
if (lengthSquared != 0) t = std::clamp(vector_dot(segment, vector_sub(point, start)) / lengthSquared, 0.0f, 1.0f);
|
||||
|
||||
Vector offset = vector_sub(point, vector_add(start, vector_scale(segment, t)));
|
||||
return {vector_length_squared(offset), t};
|
||||
}
|
||||
|
||||
// 0070be60 Whether a cell of a grid collision blocks an entity of a grid collision class.
|
||||
bool grid_collision_blocks(int gridCollisionClass, int collision)
|
||||
{
|
||||
bool isWall = collision == COLLISION_WALL || collision == COLLISION_WALL_EXCEPT_PLAYER;
|
||||
bool isStanding = collision == COLLISION_OBJECT || collision == COLLISION_SOLID || isWall;
|
||||
switch (gridCollisionClass)
|
||||
{
|
||||
case GRIDCOLL_WALLS_X:
|
||||
case GRIDCOLL_WALLS_Y:
|
||||
case GRIDCOLL_WALLS:
|
||||
return isWall;
|
||||
case GRIDCOLL_GROUND:
|
||||
return collision == COLLISION_PIT || isStanding;
|
||||
case GRIDCOLL_BULLET:
|
||||
case GRIDCOLL_NOPITS:
|
||||
return isStanding;
|
||||
case GRIDCOLL_PITSONLY:
|
||||
return collision != COLLISION_PIT;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 007f0870 Room::check_line_collision: walks a line cell by cell to the first cell that blocks the grid collision
|
||||
// class. Walls and doors may be passed through, for the beams that the room's edge stops instead.
|
||||
LineCheck room_line_check(const Vector& from, const Vector& to, int gridCollisionClass, bool isWallsIgnored)
|
||||
{
|
||||
Room& room = *g_game->room;
|
||||
Vector delta = vector_sub(to, from);
|
||||
Vector position = from;
|
||||
int column = (int)((from.x - GRID_ORIGIN_X) / GRID_CELL_SIZE + BEZIER_HALF);
|
||||
int row = (int)((from.y - GRID_ORIGIN_Y) / GRID_CELL_SIZE + BEZIER_HALF);
|
||||
int stepX = delta.x > 0 ? 1 : -1;
|
||||
int stepY = delta.y > 0 ? 1 : -1;
|
||||
float progress = 0;
|
||||
|
||||
if (!vector_is_zero(delta))
|
||||
while (true)
|
||||
{
|
||||
int index = room.GetGridIndexByTile(column, row);
|
||||
GridEntity* grid = index >= 0 ? room.GetGridEntity(index) : nullptr;
|
||||
bool isPassedWall = isWallsIgnored && grid && (grid->type == GRID_WALL || grid->type == GRID_DOOR);
|
||||
if (grid_collision_blocks(gridCollisionClass, (int)room.GetGridCollision(index)) && !isPassedWall) break;
|
||||
|
||||
float edgeX = (float)column * GRID_CELL_SIZE + GRID_ORIGIN_X + (float)stepX * GRID_HALF_CELL;
|
||||
float edgeY = (float)row * GRID_CELL_SIZE + GRID_ORIGIN_Y + (float)stepY * GRID_HALF_CELL;
|
||||
float toX = delta.x == 0 ? INFINITY : (edgeX - position.x) / delta.x;
|
||||
float toY = delta.y == 0 ? INFINITY : (edgeY - position.y) / delta.y;
|
||||
if (toY <= toX)
|
||||
row += stepY;
|
||||
else
|
||||
column += stepX;
|
||||
progress += std::min(toX, toY);
|
||||
position = vector_add(from, vector_scale(delta, progress));
|
||||
if (progress >= 1) break;
|
||||
}
|
||||
return {progress < 1 ? position : to, progress >= 1};
|
||||
}
|
||||
|
||||
// 0099c790 Lays out the path's bezier points: a straight one from the origin to the end unless some are already
|
||||
// there. The homing paths and Eye of the Occult's bend are not reconstructed yet.
|
||||
void HomingLaser::build_bezier_path(BezierPathType pathType, Entity* source)
|
||||
{
|
||||
if (bezier.empty())
|
||||
{
|
||||
Vector middle = vector_scale(vector_add(origin, endPoint), BEZIER_HALF);
|
||||
bezier.push_back({origin, origin, middle});
|
||||
bezier.push_back({endPoint, middle, endPoint});
|
||||
}
|
||||
|
||||
Entity_Laser* laser = owner && owner->type == ENTITY_LASER ? static_cast<Entity_Laser*>(owner) : nullptr;
|
||||
if (laser && laser->tearFlags[TEAR_OCCULT])
|
||||
reconstruction_missing("HomingLaser::build_bezier_path 0099c790: Eye of the Occult's bend");
|
||||
if (pathType != BEZIER_PATH_STRAIGHT && source)
|
||||
reconstruction_missing("HomingLaser::build_bezier_path 0099c790: a homing path");
|
||||
}
|
||||
|
||||
// 0099e360 Samples the bezier path evenly along its length, then eases the kept samples toward them: a ring all at
|
||||
// once by blend, a line less toward its end the longer it is.
|
||||
void HomingLaser::CalculateSamples(float blend)
|
||||
{
|
||||
if (bezier.size() < 2) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"Bezier Path Size is less than 2!\n");
|
||||
|
||||
std::vector<float> lengths = {0};
|
||||
totalLength = 0;
|
||||
for (size_t segment = 0; segment + 1 < bezier.size(); segment++)
|
||||
{
|
||||
float length = 0;
|
||||
Vector previous = bezier_evaluate(bezier[segment], bezier[segment + 1], 0);
|
||||
for (int step = 1; step <= BEZIER_LENGTH_STEPS; step++)
|
||||
{
|
||||
Vector next = bezier_evaluate(bezier[segment], bezier[segment + 1], (float)step / BEZIER_LENGTH_STEPS);
|
||||
length += vector_length(vector_sub(next, previous));
|
||||
previous = next;
|
||||
}
|
||||
lengths.push_back(length);
|
||||
totalLength += length;
|
||||
}
|
||||
if (totalLength != 0)
|
||||
{
|
||||
lengths[0] /= totalLength;
|
||||
for (size_t i = 1; i < lengths.size(); i++) lengths[i] = lengths[i] / totalLength + lengths[i - 1];
|
||||
}
|
||||
|
||||
std::vector<Vector> fresh;
|
||||
uint32_t segmentCount = (uint32_t)lengths.size() - 1;
|
||||
uint32_t lengthIndex = 0;
|
||||
for (uint32_t i = 0; i <= sampleNum; i++)
|
||||
{
|
||||
float fraction = (float)i / (float)sampleNum;
|
||||
float along = fraction;
|
||||
for (uint32_t j = lengthIndex; j < segmentCount && lengths[j] < fraction; j++)
|
||||
{
|
||||
along = (fraction - lengths[j]) / (lengths[j + 1] - lengths[j]) / (float)segmentCount +
|
||||
(float)j / (float)segmentCount;
|
||||
lengthIndex = j;
|
||||
}
|
||||
|
||||
if (bezier.size() < 2) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"Bezier Path Size less than 2!\n");
|
||||
int lastSegment = (int)bezier.size() - 1;
|
||||
float position = (float)lastSegment * along;
|
||||
int segment = (int)position;
|
||||
if (segment < 0)
|
||||
fresh.push_back(bezier.front().point);
|
||||
else if (segment < lastSegment)
|
||||
fresh.push_back(bezier_evaluate(bezier[segment], bezier[segment + 1], position - (float)segment));
|
||||
else
|
||||
fresh.push_back(bezier.back().point);
|
||||
}
|
||||
|
||||
if (smoothedSamples.size() != fresh.size())
|
||||
smoothedSamples = fresh;
|
||||
else if (isRing)
|
||||
for (size_t i = 0; i < fresh.size(); i++)
|
||||
smoothedSamples[i] =
|
||||
vector_add(smoothedSamples[i], vector_scale(vector_sub(fresh[i], smoothedSamples[i]), blend));
|
||||
else
|
||||
{
|
||||
float lineBlend = blend * std::clamp(totalLength / SMOOTHING_FULL_LENGTH, 0.0f, 1.0f);
|
||||
float last = (float)(smoothedSamples.size() - 1);
|
||||
for (size_t i = 0; i < fresh.size(); i++)
|
||||
{
|
||||
float weight = 1 - (float)i / last * lineBlend;
|
||||
smoothedSamples[i] =
|
||||
vector_add(smoothedSamples[i], vector_scale(vector_sub(fresh[i], smoothedSamples[i]), weight));
|
||||
}
|
||||
}
|
||||
samples = smoothedSamples;
|
||||
}
|
||||
|
||||
// 009a0520 Cuts the samples at the first one in a cell that blocks the grid collision class (keeping two), ending
|
||||
// them where the last segment meets that cell. A first sample already blocked keeps them as they are. Whether one
|
||||
// was blocked.
|
||||
bool HomingLaser::clip_samples_to_grid(const Vector& offset, int gridCollisionClass)
|
||||
{
|
||||
if (gridCollisionClass == GRIDCOLL_WALLS || gridCollisionClass == GRIDCOLL_NONE) return false;
|
||||
|
||||
Room& room = *g_game->room;
|
||||
for (size_t i = 0; i < samples.size(); i++)
|
||||
{
|
||||
Vector position = vector_add(offset, samples[i]);
|
||||
int column = (int)((position.x - GRID_ORIGIN_X) / GRID_CELL_SIZE + BEZIER_HALF);
|
||||
int row = (int)((position.y - GRID_ORIGIN_Y) / GRID_CELL_SIZE + BEZIER_HALF);
|
||||
if (!grid_collision_blocks(gridCollisionClass,
|
||||
(int)room.GetGridCollision(room.GetGridIndexByTile(column, row))))
|
||||
continue;
|
||||
|
||||
samples.resize(std::min(samples.size(), std::max<size_t>(i, 2)));
|
||||
if (i == 0 || samples.size() < 2) return true;
|
||||
|
||||
Vector from = vector_add(offset, samples[samples.size() - 2]);
|
||||
Vector to = vector_add(offset, samples.back());
|
||||
samples.back() = vector_sub(room_line_check(from, to, gridCollisionClass, true).hit, offset);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 009a0740 Cuts the samples, from the third on, at the first one within 10 of a player (Challenge #30's lasers).
|
||||
void HomingLaser::truncate_samples_at_player()
|
||||
{
|
||||
for (uint32_t i = PLAYER_CUT_FIRST_SAMPLE; i < samples.size(); i++)
|
||||
for (Entity_Player* player : g_game->playerManager.playerList)
|
||||
if (i < samples.size() &&
|
||||
vector_length_squared(vector_sub(player->pos, samples[i])) < PLAYER_CUT_DISTANCE_SQUARED)
|
||||
samples.resize(i);
|
||||
}
|
||||
|
||||
// 009a0de0 Keeps the samples as they are as the non-optimized ones, then drops each inner sample its neighbours
|
||||
// already line up through.
|
||||
void HomingLaser::remove_collinear_samples(float tolerance)
|
||||
{
|
||||
nonOptimizedSamples = samples;
|
||||
if (samples.empty()) return;
|
||||
|
||||
size_t last = samples.size() - 1;
|
||||
for (size_t i = 1; i < last; i++)
|
||||
{
|
||||
Vector in = vector_normalized(vector_sub(samples[i], samples[i - 1]));
|
||||
Vector out = vector_normalized(vector_sub(samples[i + 1], samples[i]));
|
||||
if (std::abs(1 - vector_dot(in, out)) >= tolerance) continue;
|
||||
|
||||
samples.erase(samples.begin() + (long)i);
|
||||
i--;
|
||||
last--;
|
||||
}
|
||||
}
|
||||
|
||||
// 009a1520 Cuts the path where it reaches a length along it. Whether it cut it.
|
||||
// The original works out the point on the segment reaching that length, then ends the path before it, at the
|
||||
// segment's start. That is kept.
|
||||
bool HomingLaser::truncate_samples(float maxLength)
|
||||
{
|
||||
float remaining = maxLength;
|
||||
for (size_t i = 0; i + 1 < samples.size(); i++)
|
||||
{
|
||||
float length = vector_length(vector_sub(samples[i + 1], samples[i]));
|
||||
if (remaining <= length)
|
||||
{
|
||||
samples.resize(i + 1);
|
||||
return true;
|
||||
}
|
||||
remaining -= length;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 009a12b0 The first entity of a type and variant the path runs through, from the second segment on, skipping the
|
||||
// ones the source's player owns. The path stops on it, where and in which direction it gets there.
|
||||
Entity* HomingLaser::find_targets(Vector& outPosition, Vector& outDirection, int type, int variant, Entity* source)
|
||||
{
|
||||
if (samples.empty()) return nullptr;
|
||||
|
||||
std::vector<Entity*> candidates = entity_query(type, variant);
|
||||
for (size_t i = 1; i + 1 < samples.size(); i++)
|
||||
for (Entity* candidate : candidates)
|
||||
{
|
||||
if (source && candidate->IsDescendantOf(source->GetPlayerAncestor())) continue;
|
||||
|
||||
SegmentProjection projection = segment_project_point(candidate->pos, samples[i], samples[i + 1]);
|
||||
if (projection.distanceSquared >= candidate->size * candidate->size) continue;
|
||||
|
||||
outDirection = vector_normalized(vector_sub(samples[i + 1], samples[i]));
|
||||
outPosition = vector_add(samples[i], vector_scale(vector_sub(samples[i + 1], samples[i]), projection.t));
|
||||
samples[i + 1] = outPosition;
|
||||
samples.resize(i + 2);
|
||||
return candidate;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// The path shapes of the tear flags and homing are not reconstructed yet.
|
||||
|
||||
// 0099dca0
|
||||
void HomingLaser::compute_axis_bezier_handles()
|
||||
{
|
||||
reconstruction_missing("HomingLaser::compute_axis_bezier_handles 0099dca0");
|
||||
}
|
||||
|
||||
// 0099e160
|
||||
void HomingLaser::compute_bezier_handles(float)
|
||||
{
|
||||
reconstruction_missing("HomingLaser::compute_bezier_handles 0099e160");
|
||||
}
|
||||
|
||||
// 0099ec00
|
||||
void HomingLaser::apply_sine_offset(float, float)
|
||||
{
|
||||
reconstruction_missing("HomingLaser::apply_sine_offset 0099ec00");
|
||||
}
|
||||
|
||||
// 0099fa60
|
||||
void HomingLaser::build_curl_path(float)
|
||||
{
|
||||
reconstruction_missing("HomingLaser::build_curl_path 0099fa60");
|
||||
}
|
||||
|
||||
// 0099fed0
|
||||
void HomingLaser::build_wiggle_bezier()
|
||||
{
|
||||
reconstruction_missing("HomingLaser::build_wiggle_bezier 0099fed0");
|
||||
}
|
||||
|
||||
// 009a0c50
|
||||
void HomingLaser::truncate_samples_at_obstacle(const Vector&)
|
||||
{
|
||||
reconstruction_missing("HomingLaser::truncate_samples_at_obstacle 009a0c50");
|
||||
}
|
||||
|
||||
// 009a0fa0
|
||||
void HomingLaser::apply_sample_wiggle(float, float)
|
||||
{
|
||||
reconstruction_missing("HomingLaser::apply_sample_wiggle 009a0fa0");
|
||||
}
|
||||
|
||||
// 009a0850
|
||||
void HomingLaser::build_ring_bezier()
|
||||
{
|
||||
reconstruction_missing("HomingLaser::build_ring_bezier 009a0850");
|
||||
}
|
||||
|
||||
// 0099f2c0
|
||||
void HomingLaser::trace_entities(uint32_t)
|
||||
{
|
||||
reconstruction_missing("laser_sample_trace_entities 0099f2c0");
|
||||
}
|
||||
Reference in new issue
Block a user