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>
This commit is contained in:
Aaron Kimbrell
2026-11-10 20:50:33 -06:00
parent a05cedbe01
commit aa9c811ad4
8 changed files with 377 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
#include "BrickByBrick.h"
#include "eLootSourceType.h"
#include "eKillType.h"
#include "PropertyBounds.h"
#include "PropertyBuilders.h"
#include "BuildingMessages.h"
@@ -211,6 +212,13 @@ std::vector<NiPoint3> PropertyManagementComponent::GetPaths() const {
return paths;
}
bool PropertyManagementComponent::IsInsidePlot(const NiPoint3& position) const {
// The same path and height DownloadPropertyData gives the client
const auto zoneId = Game::zoneManager->GetZone()->GetWorldID();
const auto propertyTemplate = CDClientManager::GetTable<CDPropertyTemplateTable>()->GetByMapID(zoneId);
return PropertyBounds::IsPositionInside(position, GetPaths(), propertyTemplate.maxBuildHeight);
}
PropertyPrivacyOption PropertyManagementComponent::GetPrivacyOption() const {
return privacyOption;
}
@@ -538,6 +546,20 @@ void PropertyManagementComponent::UpdateModelPosition(Entity& builder, const LWO
return;
}
// Outside the plot: nothing is placed and the item stays where it is. The client shows its "outside the property
// bounds" message for this answer and lets go of the placement.
if (!IsInsidePlot(position)) {
LOG("Player %llu tried to place model %llu at <%f, %f, %f>, outside the plot", builder.GetObjectID(), id, position.x, position.y, position.z);
GameMessages::PlaceModelResponse msg;
msg.target = entity->GetObjectID();
msg.position = position;
msg.propertyPlaqueID = m_Parent->GetObjectID();
msg.response = BrickByBrick::PLACE_MODEL_OUT_OF_BOUNDS;
msg.rotation = rotation;
msg.Send(entity->GetSystemAddress());
return;
}
const NiQuaternion originalRotation = rotation;
if (rotation != QuatUtils::IDENTITY) {
rotation = { rotation.w, rotation.z, rotation.y, rotation.x };

View File

@@ -84,6 +84,12 @@ public:
*/
std::vector<NiPoint3> GetPaths() const;
/**
* Whether a model may be put down at this position: inside the plot's boundary path and under its build height, as
* the client tests a box against the same data (PropertyBounds). Models already placed are never tested.
*/
bool IsInsidePlot(const NiPoint3& position) const;
/**
* Returns the privacy options for this property
* @return the privacy options for this property

View File

@@ -41,6 +41,7 @@ namespace BrickByBrick {
};
// PlaceModelResponse's response
constexpr int32_t PLACE_MODEL_OUT_OF_BOUNDS = 12; // the client's "placement bounds" refusal
constexpr int32_t PLACE_MODEL_PLACED = 14;
constexpr int32_t PLACE_MODEL_REMOVED = 16;

View File

@@ -11,6 +11,7 @@ set(DGAME_DUTILITIES_SOURCES "BrickDatabase.cpp"
"HolidayEvents.cpp"
"Contraband.cpp"
"ZoneEditor.cpp"
"PropertyBounds.cpp"
"PropertyBuilders.cpp"
"PropertyRent.cpp"
"PropertyReputation.cpp"

View File

@@ -0,0 +1,175 @@
#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;
}

View File

@@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include <vector>
#include "NiPoint3.h"
// Whether a box is inside a property plot, as the client decides it from the boundary path and build height that
// DownloadPropertyData gives it.
namespace PropertyBounds {
// What the check answers (the client's values)
enum Result : int32_t {
NoPath = -1, // the property has no boundary path
Inside = 0,
TooWide = 1, // the box is wider than the plot (3 when it is too long as well, or instead)
TooTall = 2, // the box is taller than the plot's build space
TooLong = 3,
CentreOutside = 4, // the box's centre is outside the boundary polygon
Outside = 5 // a side of the box is outside the plot's box, or a bottom corner outside the polygon
};
// The point's side of the boundary edge nearest to it, heights ignored.
// The path is a closed polygon whose points run so that the inside is on the positive side of every edge; distance,
// when given, receives the distance to that nearest edge.
bool PointInPolygonXZ(const NiPoint3& point, const std::vector<NiPoint3>& path, float* distance = nullptr);
// The plot is the box around the path's points; with checkHeight
// and a build height it reaches up to the path's lowest point plus maxBuildHeight (there is no floor). The box must
// fit in it, and its centre and four bottom corners must be inside the polygon.
int32_t CheckBoxAgainstPath(const NiPoint3& boxMin, const NiPoint3& boxMax, const std::vector<NiPoint3>& path, bool checkHeight, float maxBuildHeight, float* distance = nullptr);
// A placed model's position against the plot. The server does not hold a model's box, so only the point is tested:
// this never refuses what the box test would allow for a model that stands on its position.
inline bool IsPositionInside(const NiPoint3& position, const std::vector<NiPoint3>& path, const float maxBuildHeight) {
const auto result = CheckBoxAgainstPath(position, position, path, true, maxBuildHeight);
// Nothing to test against is not a refusal
return result == Inside || result == NoPath;
}
}

View File

@@ -18,6 +18,7 @@ set(DGAMETEST_SOURCES
"PowerUpStatisticTests.cpp"
"PlayerStatisticTests.cpp"
"BrickByBrickTests.cpp"
"PropertyBoundsTests.cpp"
"PropertyBuildersTests.cpp"
"ModularBuildTableTests.cpp"
"ModularBuildIdMigrationTests.cpp"

View File

@@ -0,0 +1,132 @@
// The plot test a placed model's position goes through: the client's box against the boundary path and build height.
#include "PropertyBounds.h"
#include <gtest/gtest.h>
#include <algorithm>
#include <vector>
namespace {
constexpr float GROUND = 400.0f;
std::vector<NiPoint3> Path(const std::vector<std::pair<float, float>>& points) {
std::vector<NiPoint3> path;
for (const auto& [x, z] : points) path.emplace_back(x, GROUND, z);
return path;
}
// The inside is on the positive side of every edge, as on every property map
const auto SQUARE = Path({ { 0, 0 }, { 10, 0 }, { 10, 10 }, { 0, 10 } });
// An L: the square from (10, 10) to (20, 20) is cut out
const auto ELL = Path({ { 0, 0 }, { 20, 0 }, { 20, 10 }, { 10, 10 }, { 10, 20 }, { 0, 20 } });
bool InPolygon(const std::vector<NiPoint3>& path, const float x, const float z) {
return PropertyBounds::PointInPolygonXZ({ x, GROUND, z }, path);
}
}
TEST(PropertyBoundsTests, PointsInsideAndOutsideASquare) {
EXPECT_TRUE(InPolygon(SQUARE, 5, 5));
EXPECT_TRUE(InPolygon(SQUARE, 0.01f, 0.01f));
// On an edge and on a corner counts as inside
EXPECT_TRUE(InPolygon(SQUARE, 5, 0));
EXPECT_TRUE(InPolygon(SQUARE, 0, 0));
EXPECT_TRUE(InPolygon(SQUARE, 10, 10));
// Beside each edge
EXPECT_FALSE(InPolygon(SQUARE, -1, 5));
EXPECT_FALSE(InPolygon(SQUARE, 11, 5));
EXPECT_FALSE(InPolygon(SQUARE, 5, -1));
EXPECT_FALSE(InPolygon(SQUARE, 5, 11));
// Past each corner, where no edge is beside the point
EXPECT_FALSE(InPolygon(SQUARE, -1, -1));
EXPECT_FALSE(InPolygon(SQUARE, 11, 11));
EXPECT_FALSE(InPolygon(SQUARE, 11, -1));
EXPECT_FALSE(InPolygon(SQUARE, -1, 11));
EXPECT_FALSE(InPolygon(SQUARE, -3, -0.5f));
EXPECT_FALSE(InPolygon(SQUARE, 13, 10.5f));
}
TEST(PropertyBoundsTests, TheHeightOfThePointAndOfThePathIsIgnoredByThePolygon) {
auto path = SQUARE;
path[2].y = GROUND + 30.0f;
EXPECT_TRUE(PropertyBounds::PointInPolygonXZ({ 5, -1000, 5 }, path));
EXPECT_FALSE(PropertyBounds::PointInPolygonXZ({ 11, GROUND, 5 }, path));
}
TEST(PropertyBoundsTests, TheCornerThatPointsInwardsOfAnEll) {
EXPECT_TRUE(InPolygon(ELL, 5, 5));
EXPECT_TRUE(InPolygon(ELL, 15, 5));
EXPECT_TRUE(InPolygon(ELL, 5, 15));
EXPECT_TRUE(InPolygon(ELL, 9, 9));
EXPECT_TRUE(InPolygon(ELL, 10, 10));
// In the part that is cut out
EXPECT_FALSE(InPolygon(ELL, 15, 15));
EXPECT_FALSE(InPolygon(ELL, 11, 11));
EXPECT_FALSE(InPolygon(ELL, 10.5f, 10.5f));
EXPECT_FALSE(InPolygon(ELL, 12, 10.2f));
EXPECT_FALSE(InPolygon(ELL, 10.2f, 12));
EXPECT_FALSE(InPolygon(ELL, 25, 5));
EXPECT_FALSE(InPolygon(ELL, 5, 25));
EXPECT_FALSE(InPolygon(ELL, 21, 11));
}
// The client's test depends on the direction the path runs in: the other way round, inside and outside swap
TEST(PropertyBoundsTests, APathTheOtherWayRoundReadsInsideOut) {
auto reversed = SQUARE;
std::reverse(reversed.begin(), reversed.end());
EXPECT_FALSE(InPolygon(reversed, 5, 5));
EXPECT_TRUE(InPolygon(reversed, -1, 5));
}
TEST(PropertyBoundsTests, ThePositionOfAModel) {
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND, 5 }, ELL, 128.0f));
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 15, GROUND + 3, 5 }, ELL, 128.0f));
// Outside the box around the path, and inside it but outside the path
EXPECT_FALSE(PropertyBounds::IsPositionInside({ -5, GROUND, 5 }, ELL, 128.0f));
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 5, GROUND, 21 }, ELL, 128.0f));
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 15, GROUND, 15 }, ELL, 128.0f));
}
// The build space ends maxBuildHeight above the path's lowest point; there is no floor
TEST(PropertyBoundsTests, TheBuildHeight) {
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND + 128.0f, 5 }, SQUARE, 128.0f));
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 5, GROUND + 128.5f, 5 }, SQUARE, 128.0f));
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND - 50.0f, 5 }, SQUARE, 128.0f));
// Counted from the lowest point; a path point above that height raises the ceiling to itself
auto sloped = SQUARE;
sloped[0].y = GROUND - 10.0f;
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND + 6.0f, 5 }, sloped, 16.0f));
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 5, GROUND + 6.5f, 5 }, sloped, 16.0f));
sloped[1].y = GROUND + 20.0f;
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND + 20.0f, 5 }, sloped, 16.0f));
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 5, GROUND + 20.5f, 5 }, sloped, 16.0f));
// No build height: the path's own heights are the space
EXPECT_FALSE(PropertyBounds::IsPositionInside({ 5, GROUND + 1.0f, 5 }, SQUARE, 0.0f));
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 5, GROUND, 5 }, SQUARE, 0.0f));
}
// A property without a boundary path has nothing to refuse a placement with
TEST(PropertyBoundsTests, NoPathRefusesNothing) {
EXPECT_EQ(PropertyBounds::CheckBoxAgainstPath({ 1, 2, 3 }, { 1, 2, 3 }, {}, true, 128.0f), PropertyBounds::NoPath);
EXPECT_TRUE(PropertyBounds::IsPositionInside({ 1, 2, 3 }, {}, 128.0f));
EXPECT_FALSE(PropertyBounds::PointInPolygonXZ({ 1, 2, 3 }, {}));
}
TEST(PropertyBoundsTests, ABoxAgainstThePlot) {
using namespace PropertyBounds;
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND, 2 }, { 8, GROUND + 5, 8 }, SQUARE, true, 128.0f), Inside);
// Larger than the plot
EXPECT_EQ(CheckBoxAgainstPath({ -1, GROUND, 2 }, { 12, GROUND + 5, 8 }, SQUARE, true, 128.0f), TooWide);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND, -1 }, { 8, GROUND + 5, 12 }, SQUARE, true, 128.0f), TooLong);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND, 2 }, { 8, GROUND + 129, 8 }, SQUARE, true, 128.0f), TooTall);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND, 2 }, { 8, GROUND + 129, 8 }, SQUARE, false, 128.0f), Inside);
// Sticking out of a side, and out of the top
EXPECT_EQ(CheckBoxAgainstPath({ 6, GROUND, 2 }, { 12, GROUND + 5, 8 }, SQUARE, true, 128.0f), Outside);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND + 125, 2 }, { 8, GROUND + 130, 8 }, SQUARE, true, 128.0f), Outside);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND + 125, 2 }, { 8, GROUND + 130, 8 }, SQUARE, false, 128.0f), Inside);
// Inside the box around the path, with the centre or one bottom corner in the part of the L that is cut out
EXPECT_EQ(CheckBoxAgainstPath({ 12, GROUND, 12 }, { 18, GROUND + 5, 18 }, ELL, true, 128.0f), CentreOutside);
EXPECT_EQ(CheckBoxAgainstPath({ 4, GROUND, 4 }, { 12, GROUND + 5, 12 }, ELL, true, 128.0f), Outside);
EXPECT_EQ(CheckBoxAgainstPath({ 2, GROUND, 2 }, { 9, GROUND + 5, 9 }, ELL, true, 128.0f), Inside);
}