625 lines
28 KiB
C++
625 lines
28 KiB
C++
#include "homing_laser.h"
|
|
#include "boss_ai.h"
|
|
#include "entity_familiar.h"
|
|
#include "entity_knife.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 float RING_HANDLE = 0.3466f;
|
|
constexpr int RING_SEGMENTS = 6;
|
|
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;
|
|
constexpr float OBSTACLE_MARGIN = 20;
|
|
|
|
// A hunting path: 40 long steps turned 0.3 toward the nearest target, up to 25 points, smoothed at 0.4. A path shorter
|
|
// than 0.1 does not hunt.
|
|
constexpr float HUNT_STEP = 40;
|
|
constexpr float HUNT_TURN = 0.3f;
|
|
constexpr size_t HUNT_MAX_POINTS = 25;
|
|
constexpr float HUNT_TENSION = 0.4f;
|
|
constexpr float HUNT_MIN_LENGTH_SQUARED = 0.01f;
|
|
|
|
// A homing path: up to six points, pulled only off segments at least 120 long and not within 80 of their ends.
|
|
constexpr size_t HOMING_MAX_POINTS = 6;
|
|
constexpr float HOMING_SEGMENT_MIN_SQUARED = 14400;
|
|
constexpr float HOMING_END_CLEARANCE_SQUARED = 6400;
|
|
|
|
// Eye of the Occult's bend: the thirds of the curve, the margin its end keeps from the wall, its handles' length and
|
|
// the kink near the start of a player's loop.
|
|
constexpr float OCCULT_THIRD = 0.333f;
|
|
constexpr float OCCULT_TWO_THIRDS = 0.666f;
|
|
constexpr float OCCULT_WALL_MARGIN = 20;
|
|
constexpr float OCCULT_HANDLE = 50;
|
|
constexpr float OCCULT_KINK = 35;
|
|
|
|
// 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};
|
|
}
|
|
|
|
// The entities a homing path bends toward: vulnerable enemies (of another type than the source for that path type),
|
|
// a versus rival's punching bag, and the players too for a path that hits them.
|
|
static std::vector<Entity*> homing_targets_get(BezierPathType pathType, Entity* source)
|
|
{
|
|
std::vector<Entity*> targets;
|
|
for (Entity* entity : g_game->room->entityList.updateEL)
|
|
{
|
|
bool isTarget = entity->IsVulnerableEnemy(nullptr) && !entity->HasEntityFlags(FLAG_FRIENDLY | FLAG_NO_TARGET) &&
|
|
entity != source && !(pathType == BEZIER_PATH_OTHER_TYPES && entity->type == source->type);
|
|
bool isRivalBag = g_game->deathmatchManager.is_active() && entity->type == ENTITY_FAMILIAR &&
|
|
entity->variant == Entity_Familiar::PUNCHING_BAG &&
|
|
!entity->IsDescendantOf(source->spawnerEntity);
|
|
if (isTarget || isRivalBag) targets.push_back(entity);
|
|
if (pathType == BEZIER_PATH_WITH_PLAYERS && entity->type == ENTITY_PLAYER && entity != source)
|
|
targets.push_back(entity);
|
|
}
|
|
return targets;
|
|
}
|
|
|
|
// A path that hunts its way from its origin through the room: 40 long steps, each turned toward the nearest target
|
|
// (more so the more it already faces it), until it leaves the room or has 25 points.
|
|
static void homing_hunt(HomingLaser& path, const std::vector<Entity*>& targets)
|
|
{
|
|
Vector heading = vector_sub(path.bezier.back().point, path.bezier.front().point);
|
|
if (vector_length_squared(heading) < HUNT_MIN_LENGTH_SQUARED) return;
|
|
|
|
path.bezier.pop_back();
|
|
while (!targets.empty())
|
|
{
|
|
Vector last = path.bezier.back().point;
|
|
Entity* nearest = nullptr;
|
|
float best = INFINITY;
|
|
for (Entity* target : targets)
|
|
{
|
|
float distance = vector_length_squared(vector_sub(last, target->pos));
|
|
if (distance >= best) continue;
|
|
nearest = target;
|
|
best = distance;
|
|
}
|
|
if (!nearest) break;
|
|
|
|
Vector toward = vector_normalized(vector_sub(nearest->pos, last));
|
|
float facing = (vector_dot(vector_normalized(heading), toward) + 1) * BEZIER_HALF;
|
|
size_t count = path.bezier.size();
|
|
if (count > 1) heading = vector_sub(path.bezier[count - 1].point, path.bezier[count - 2].point);
|
|
heading = vector_normalized(vector_add(vector_normalized(heading), vector_scale(toward, HUNT_TURN * facing)));
|
|
|
|
Vector next = vector_add(last, vector_scale(heading, HUNT_STEP));
|
|
path.bezier.push_back({next, {}, {}});
|
|
if (!g_game->room->IsPositionInRoom(next, 0) || path.bezier.size() >= HUNT_MAX_POINTS) break;
|
|
}
|
|
path.compute_bezier_handles(HUNT_TENSION);
|
|
}
|
|
|
|
// Eye of the Occult's bend for a laser no player aims: a curve through the target, a third and two thirds of the way
|
|
// along the straight beam stretched to the target's distance, going on past the target to the wall.
|
|
static void occult_curve(HomingLaser& path, const Vector& target)
|
|
{
|
|
Vector origin = path.origin;
|
|
Vector toTarget = vector_sub(target, origin);
|
|
Vector line = vector_sub(path.endPoint, origin);
|
|
float lineLength = vector_length(line);
|
|
if (lineLength > 0) line = vector_scale(line, vector_length(toTarget) / lineLength);
|
|
|
|
Vector stretched = vector_sub(vector_add(origin, line), origin);
|
|
auto blend = [&](float t) {
|
|
Vector along = vector_add(vector_scale(stretched, t), origin);
|
|
return vector_add(vector_scale(vector_sub(vector_add(vector_scale(toTarget, t), origin), along), t), along);
|
|
};
|
|
Vector first = blend(OCCULT_THIRD);
|
|
Vector second = blend(OCCULT_TWO_THIRDS);
|
|
path.endPoint = Entity_Laser::CalculateEndPoint(target, vector_sub(target, second), {}, nullptr, OCCULT_WALL_MARGIN);
|
|
|
|
Vector beyond = vector_sub(path.endPoint, target);
|
|
path.bezier.clear();
|
|
path.bezier.push_back({origin, origin, first});
|
|
path.bezier.push_back({target, second, vector_add(vector_scale(beyond, OCCULT_THIRD), target)});
|
|
path.bezier.push_back({path.endPoint, vector_add(vector_scale(beyond, OCCULT_TWO_THIRDS), target), path.endPoint});
|
|
}
|
|
|
|
// Eye of the Occult's bend for a player's laser: the path's last point is swung out past a kink near its start, then
|
|
// through the target and back to where it ended, with axis-aligned handles.
|
|
static void occult_loop(HomingLaser& path, const Vector& target)
|
|
{
|
|
size_t count = path.bezier.size();
|
|
Vector start = count < 2 ? path.origin : path.bezier[count - 2].point;
|
|
Vector last = count == 0 ? path.endPoint : path.bezier[count - 1].point;
|
|
Vector toTarget = vector_normalized(vector_sub(target, start));
|
|
Vector back = vector_normalized(vector_sub(last, target));
|
|
Vector line = vector_normalized(vector_sub(last, start));
|
|
Vector handle = vector_scale(line, OCCULT_HANDLE);
|
|
if (count > 1) path.bezier.pop_back();
|
|
if (path.bezier.empty()) path.bezier.push_back({start, start, vector_add(start, handle)});
|
|
|
|
Vector kink = vector_add(vector_add(vector_make(-line.y, line.x), vector_scale(line, OCCULT_KINK)), start);
|
|
path.bezier.push_back({kink, vector_sub(kink, handle), vector_add(kink, vector_scale(toTarget, OCCULT_HANDLE))});
|
|
path.bezier.push_back({target, vector_sub(target, handle), vector_add(target, handle)});
|
|
path.bezier.push_back({last, vector_sub(last, vector_scale(back, OCCULT_HANDLE)), last});
|
|
path.compute_axis_bezier_handles();
|
|
}
|
|
|
|
// Eye of the Occult: a laser carrying it bends toward its target position, or its player's Marked target.
|
|
static void occult_bend(HomingLaser& path)
|
|
{
|
|
Entity_Laser* laser = path.owner && path.owner->type == ENTITY_LASER ? static_cast<Entity_Laser*>(path.owner) : nullptr;
|
|
if (!laser || !laser->tearFlags[TEAR_OCCULT]) return;
|
|
|
|
Vector target = laser->targetPosition;
|
|
Entity* spawner = laser->spawnerEntity;
|
|
Entity_Player* player = spawner && spawner->is_player() ? spawner->ToPlayer() : nullptr;
|
|
if (player && player->markedTarget) target = player->markedTarget->pos;
|
|
if (vector_is_zero(target)) return;
|
|
if (player) occult_loop(path, target);
|
|
else occult_curve(path, target);
|
|
}
|
|
|
|
// A homing straight path pulls a point onto each target it passes near (closer than the reach, ahead of the point and
|
|
// not within 80 of either end of a segment at least 120 long), up to six points. A longer path moves its inner points
|
|
// onto the targets they pass near instead.
|
|
static void homing_bend(HomingLaser& path, std::vector<Entity*>& targets)
|
|
{
|
|
float reachSquared = path.reach * path.reach;
|
|
float unusedT = 0;
|
|
if (path.bezier.size() != 2)
|
|
{
|
|
bool isMoved = false;
|
|
for (size_t i = 1; i + 1 < path.bezier.size(); i++)
|
|
for (auto target = targets.begin(); target != targets.end(); target++)
|
|
{
|
|
Vector spot = (*target)->pos;
|
|
if (bezier_closest_point(spot, path.bezier[i], path.bezier[i + 1], unusedT) >= reachSquared) continue;
|
|
path.bezier[i].point = spot;
|
|
targets.erase(target);
|
|
isMoved = true;
|
|
break;
|
|
}
|
|
if (isMoved) path.compute_axis_bezier_handles();
|
|
return;
|
|
}
|
|
|
|
bool isInserted = true;
|
|
while (isInserted && path.bezier.size() < HOMING_MAX_POINTS)
|
|
{
|
|
isInserted = false;
|
|
for (size_t segment = 0; segment + 1 < path.bezier.size() && !isInserted; segment++)
|
|
{
|
|
BezierPoint& from = path.bezier[segment];
|
|
Vector to = path.bezier[segment + 1].point;
|
|
if (vector_length_squared(vector_sub(to, from.point)) < HOMING_SEGMENT_MIN_SQUARED) continue;
|
|
for (auto target = targets.begin(); target != targets.end(); target++)
|
|
{
|
|
Vector spot = (*target)->pos;
|
|
if (bezier_closest_point(spot, from, path.bezier[segment + 1], unusedT) >= reachSquared) continue;
|
|
bool isAhead = vector_dot(vector_sub(from.handleOut, from.point), vector_sub(spot, from.point)) > 0;
|
|
if (!isAhead || vector_length_squared(vector_sub(spot, from.point)) <= HOMING_END_CLEARANCE_SQUARED ||
|
|
vector_length_squared(vector_sub(spot, to)) <= HOMING_END_CLEARANCE_SQUARED)
|
|
continue;
|
|
path.bezier.insert(path.bezier.begin() + (long)segment + 1, {spot, {}, {}});
|
|
targets.erase(target);
|
|
isInserted = true;
|
|
path.compute_axis_bezier_handles();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 0099c790 Lays out the path's bezier points: a straight one from the origin to the end unless some are already
|
|
// there, bent by Eye of the Occult, then toward its targets for a homing path, through every target of another type
|
|
// for that path type, or hunting its targets for a path that hits players.
|
|
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});
|
|
}
|
|
|
|
std::vector<Entity*> targets;
|
|
if (pathType != BEZIER_PATH_STRAIGHT) targets = homing_targets_get(pathType, source);
|
|
occult_bend(*this);
|
|
if (pathType == BEZIER_PATH_HOMING) homing_bend(*this, targets);
|
|
else if (pathType == BEZIER_PATH_OTHER_TYPES && bezier.size() == 2)
|
|
{
|
|
for (Entity* target : targets) bezier.insert(bezier.end() - 1, {target->pos, {}, {}});
|
|
compute_axis_bezier_handles();
|
|
}
|
|
else if (pathType == BEZIER_PATH_WITH_PLAYERS && bezier.size() == 2) homing_hunt(*this, targets);
|
|
}
|
|
|
|
// 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 axis a vector leans along most, keeping its length along it (vertical on a tie).
|
|
static Vector vector_axis_part(Vector v)
|
|
{
|
|
return std::fabs(v.x) <= std::fabs(v.y) ? vector_make(0, v.y) : vector_make(v.x, 0);
|
|
}
|
|
|
|
// 0099dca0 Gives the path right-angled handles: the first point's out handle runs along the axis its handle leans
|
|
// along, half way to the next point. Each inner point's handles run across the axis its neighbours' directions add
|
|
// up along, toward the previous point, half way to each neighbour (twice as far when the previous point's handle
|
|
// points away from it). The last point's in handle runs half way along the axis toward the point before it.
|
|
void HomingLaser::compute_axis_bezier_handles()
|
|
{
|
|
if (bezier.size() < 2) return;
|
|
|
|
BezierPoint& first = bezier[0];
|
|
Vector firstAxis = vector_cardinal(vector_sub(first.handleOut, first.point));
|
|
Vector firstReach = vector_scale(firstAxis, vector_length(vector_sub(bezier[1].point, first.point)) * BEZIER_HALF);
|
|
first.handleOut = vector_add(first.point, firstReach);
|
|
bezier[1].handleIn = vector_sub(bezier[1].point, firstReach);
|
|
|
|
for (size_t i = 1; i + 1 < bezier.size(); i++)
|
|
{
|
|
BezierPoint& previous = bezier[i - 1];
|
|
BezierPoint& point = bezier[i];
|
|
Vector toPrevious = vector_sub(previous.point, point.point);
|
|
Vector toNext = vector_sub(bezier[i + 1].point, point.point);
|
|
float nextLength = vector_length(toNext);
|
|
Vector sum = vector_add(vector_normalized(toNext), vector_normalized(toPrevious));
|
|
Vector axis = vector_cardinal(sum);
|
|
Vector across = vector_make(axis.y, -axis.x);
|
|
if (vector_dot(toPrevious, across) < 0) across = vector_scale(across, -1);
|
|
if (vector_dot(vector_sub(previous.handleOut, previous.point), vector_sub(point.point, previous.point)) < 0)
|
|
across = vector_scale(across, 2);
|
|
point.handleOut = vector_add(point.point, vector_scale(across, -nextLength * BEZIER_HALF));
|
|
point.handleIn = vector_add(point.point, vector_scale(across, vector_length(toPrevious) * BEZIER_HALF));
|
|
}
|
|
|
|
BezierPoint& last = bezier.back();
|
|
Vector toPrevious = vector_axis_part(vector_sub(bezier[bezier.size() - 2].point, last.point));
|
|
last.handleIn = vector_add(last.point, vector_scale(toPrevious, BEZIER_HALF));
|
|
}
|
|
|
|
// 0099e160 Smooths the path: each point's handles lie along the line from its neighbours, scaled by the tension
|
|
// (halved between two neighbours). A path ending where it started (three points or more) wraps around.
|
|
void HomingLaser::compute_bezier_handles(float tension)
|
|
{
|
|
if (bezier.empty()) return;
|
|
|
|
size_t last = bezier.size() - 1;
|
|
bool isClosed = vector_length_squared(vector_sub(bezier[last].point, bezier[0].point)) < HUNT_MIN_LENGTH_SQUARED &&
|
|
bezier.size() >= 3;
|
|
for (size_t i = 0; i <= last; i++)
|
|
{
|
|
Vector previous{};
|
|
Vector next{};
|
|
if (isClosed && i == 0) previous = vector_sub(bezier[0].point, bezier[last - 1].point);
|
|
else
|
|
{
|
|
if (isClosed && i == last) next = vector_sub(bezier[1].point, bezier[i].point);
|
|
if (i != 0) previous = vector_sub(bezier[i].point, bezier[i - 1].point);
|
|
}
|
|
if (i < last) next = vector_sub(bezier[i + 1].point, bezier[i].point);
|
|
|
|
Vector handle = vector_scale(vector_add(previous, next), tension);
|
|
if (isClosed || (i != 0 && i < last)) handle = vector_scale(handle, BEZIER_HALF);
|
|
bezier[i].handleIn = vector_sub(bezier[i].point, handle);
|
|
bezier[i].handleOut = vector_add(bezier[i].point, handle);
|
|
}
|
|
}
|
|
|
|
// 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 Cuts the path at the first segment past the first that a wall or obstacle stops, the stop taking the
|
|
// place of the segment's start.
|
|
void HomingLaser::truncate_samples_at_obstacle(const Vector& offset)
|
|
{
|
|
for (size_t i = 1; i + 1 < samples.size(); i++)
|
|
{
|
|
Vector start = samples[i];
|
|
Vector segment = vector_sub(samples[i + 1], start);
|
|
Vector stop = Entity_Laser::CalculateEndPoint(start, vector_normalized(segment), offset, nullptr, OBSTACLE_MARGIN);
|
|
if (vector_length_squared(vector_sub(start, stop)) >= vector_length_squared(segment)) continue;
|
|
|
|
samples[i] = stop;
|
|
samples.resize(i + 1);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 009a0fa0
|
|
void HomingLaser::apply_sample_wiggle(float, float)
|
|
{
|
|
reconstruction_missing("HomingLaser::apply_sample_wiggle 009a0fa0");
|
|
}
|
|
|
|
// 009a0850 Lays out a ring of radius around a center: seven points (the last closing the circle) with handles along
|
|
// its tangent. Spoon Bender's ring bending toward enemies is not reconstructed yet.
|
|
void HomingLaser::build_ring_bezier(const Vector& center, bool isHoming, Entity*, float radius)
|
|
{
|
|
for (int i = 0; i <= RING_SEGMENTS; i++)
|
|
{
|
|
float angle = 0 - ((float)i / RING_SEGMENTS * PI_F + (float)i / RING_SEGMENTS * PI_F);
|
|
Vector point = vector_make(std::cos(angle) * radius + center.x, std::sin(angle) * radius + center.y);
|
|
float tangentAngle = angle - PI_F / 2;
|
|
Vector tangent = vector_make(std::cos(tangentAngle) * radius * RING_HANDLE,
|
|
std::sin(tangentAngle) * radius * RING_HANDLE);
|
|
bezier.push_back({point, vector_sub(point, tangent), vector_add(point, tangent)});
|
|
}
|
|
if (isHoming) reconstruction_missing("HomingLaser::build_ring_bezier 009a0850 (a homing ring)");
|
|
}
|
|
|
|
// 0099f2c0
|
|
void HomingLaser::trace_entities(uint32_t)
|
|
{
|
|
reconstruction_missing("laser_sample_trace_entities 0099f2c0");
|
|
}
|