Files
DarkflameServer/tests/dGameTests/PropertyBoundsTests.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

133 lines
6.3 KiB
C++

// 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);
}