Files
DarkflameServer/dGame/dUtilities/PropertyBounds.cpp
Aaron Kimbrell aa9c811ad4 feat(property): a model can only be put down inside the plot
The client tests a box against the property's boundary path and build height:
the box around the path's points, raised to maxBuildHeight above its lowest
point and with no floor, then the box's centre and bottom corners against the
path as a polygon. The server placed a model wherever UpdateModelFromClient
said.

PropertyBounds is that test, and UpdateModelPosition runs the model's position
through it with the path and height DownloadPropertyData sends. The server has
no box for a model, so only the position is tested. A refused placement leaves
the item in the inventory and is answered with PlaceModelResponse 12, for which
the client shows its placement bounds message.

Models already on a property are not tested, moved or removed, wherever they
stand; a property without a boundary path refuses nothing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 01:02:35 -06:00

176 lines
6.0 KiB
C++

#include "PropertyBounds.h"
#include <algorithm>
#include <cmath>
#include <limits>
namespace {
// The margin of the side test, and how much taller than the build space a box may be
constexpr float SIDE_MARGIN = 0.05f;
constexpr float HEIGHT_MARGIN = 0.32f;
struct Flat {
float x{};
float z{};
};
Flat Flatten(const NiPoint3& point) {
return { point.x, point.z };
}
float Length(const float x, const float z) {
return std::sqrt(x * x + z * z);
}
// y of (b - a) x (c - b): positive where the path turns towards its inside
float Turn(const Flat& a, const Flat& b, const Flat& c) {
return (b.x - a.x) * (c.z - b.z) - (b.z - a.z) * (c.x - b.x);
}
// What one edge says about the point (an unnamed client function). distanceToEdge keeps its last value when the point is not
// beside the edge, as the client's does.
struct EdgeTest {
bool inside{};
float distanceToStart{};
float distanceToEnd{};
float distanceToEdge{};
float along{};
void Run(const Flat& point, const Flat& start, const Flat& end) {
const Flat edge{ end.x - start.x, end.z - start.z };
const Flat offset{ point.x - start.x, point.z - start.z };
distanceToStart = Length(offset.x, offset.z);
distanceToEnd = Length(point.x - end.x, point.z - end.z);
const float side = edge.x * offset.z - edge.z * offset.x;
inside = distanceToStart < SIDE_MARGIN || distanceToEnd < SIDE_MARGIN || SIDE_MARGIN < side || -SIDE_MARGIN < side;
const float lengthSquared = edge.x * edge.x + edge.z * edge.z;
along = lengthSquared <= SIDE_MARGIN ? 0.0f : (offset.x * edge.x + offset.z * edge.z) / lengthSquared;
if (along >= 0.0f && along <= 1.0f) {
distanceToEdge = Length(edge.x * along - offset.x, edge.z * along - offset.z);
}
}
};
}
bool PropertyBounds::PointInPolygonXZ(const NiPoint3& point, const std::vector<NiPoint3>& path, float* const distance) {
if (path.empty()) return false;
const Flat flatPoint = Flatten(point);
Flat start = Flatten(path.back());
Flat end = Flatten(path.front());
// The start of the edge before the corner the point may turn out to be nearest to, and that edge's answer
Flat cornerFrom{};
bool cornerInside = false;
// The closing edge's answer when the point lies before its start: the last corner is judged with it
bool firstInside = false;
bool result = false;
float nearest = std::numeric_limits<float>::max();
EdgeTest test;
test.Run(flatPoint, start, end);
if (test.along >= 0.0f) {
if (test.along > 1.0f) {
cornerFrom = start;
cornerInside = test.inside;
} else {
result = test.inside;
nearest = test.distanceToEdge;
if (test.distanceToEdge == 0.0f) {
if (distance) *distance = std::numeric_limits<float>::max();
return test.inside;
}
}
} else {
firstInside = test.inside;
}
for (size_t i = 1; i < path.size(); i++) {
start = end;
end = Flatten(path[i]);
test.Run(flatPoint, start, end);
if (distance) *distance = test.distanceToEdge;
if (test.along >= 0.0f || nearest <= test.distanceToStart) {
if (test.along < 0.0f || test.along > 1.0f || nearest <= test.distanceToEdge) {
if (test.along > 1.0f && test.distanceToEnd < nearest) {
cornerFrom = start;
cornerInside = test.inside;
}
} else {
// Beside this edge and nearer to it than to anything before
result = test.inside;
nearest = test.distanceToEdge;
if (test.distanceToEdge == 0.0f) break;
}
} else {
// Nearest to the corner at this edge's start: inside both edges at a corner that points out, either at one
// that points in
result = Turn(cornerFrom, start, end) > 0.0f ? (cornerInside && test.inside) : (cornerInside || test.inside);
nearest = test.distanceToStart;
}
}
// Past the last edge's end and nearest to it: the corner the path closes at
if (test.along > 1.0f && test.distanceToEnd < nearest) {
result = Turn(start, end, Flatten(path.front())) <= 0.0f ? (firstInside || test.inside) : (firstInside && test.inside);
nearest = test.distanceToEnd;
}
if (distance) *distance = nearest;
return result;
}
int32_t PropertyBounds::CheckBoxAgainstPath(const NiPoint3& boxMin, const NiPoint3& boxMax, const std::vector<NiPoint3>& path, const bool checkHeight, const float maxBuildHeight, float* distance) {
if (path.empty()) return NoPath;
float unused = 0.0f;
if (!distance) distance = &unused;
NiPoint3 plotMin = path.front();
NiPoint3 plotMax = path.front();
for (const auto& point : path) {
plotMin = { std::min(plotMin.x, point.x), std::min(plotMin.y, point.y), std::min(plotMin.z, point.z) };
plotMax = { std::max(plotMax.x, point.x), std::max(plotMax.y, point.y), std::max(plotMax.z, point.z) };
}
// The build space reaches maxBuildHeight above the lowest point of the path
if (checkHeight && maxBuildHeight > 0.0f) plotMax.y = std::max(plotMax.y, plotMin.y + maxBuildHeight);
const NiPoint3 plotSize = plotMax - plotMin;
const NiPoint3 boxSize = boxMax - boxMin;
int32_t result = Inside;
if (plotSize.x < boxSize.x) result |= TooWide;
if (plotSize.z < boxSize.z) result |= TooLong;
if (checkHeight && plotSize.y + HEIGHT_MARGIN <= boxSize.y) result |= TooTall;
if (result != Inside) return result;
if (boxMin.x < plotMin.x) {
*distance = plotMin.x - boxMin.x;
result = Outside;
}
if (plotMax.x < boxMax.x) {
*distance = boxMax.x - plotMax.x;
result = Outside;
}
if (boxMin.z < plotMin.z) {
*distance = plotMin.z - boxMin.z;
result = Outside;
}
if (plotMax.z < boxMax.z) {
*distance = boxMax.z - plotMax.z;
result = Outside;
}
if (checkHeight && plotMax.y < boxMax.y) {
*distance = boxMax.y - plotMax.y;
result = Outside;
}
if (result != Inside) return result;
const NiPoint3 centre = (boxMin + boxMax) * 0.5f;
if (!PointInPolygonXZ(centre, path, distance)) return CentreOutside;
if (!PointInPolygonXZ(boxMin, path, distance)) return Outside;
if (!PointInPolygonXZ(boxMax, path, distance)) return Outside;
if (!PointInPolygonXZ({ boxMin.x, boxMin.y, boxMax.z }, path, distance)) return Outside;
if (!PointInPolygonXZ({ boxMax.x, boxMin.y, boxMin.z }, path, distance)) return Outside;
return Inside;
}