1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include "TestData.h"
#include "openrct2/core/StringReader.h"
#include "openrct2/entity/Guest.h"
#include "openrct2/peep/GuestPathfinding.h"
#include "openrct2/ride/Station.h"
#include "openrct2/scenario/Scenario.h"
#include <gtest/gtest.h>
#include <memory>
#include <openrct2/Context.h>
#include <openrct2/Game.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/ParkImporter.h>
#include <openrct2/platform/Platform.h>
#include <openrct2/world/Footpath.h>
#include <openrct2/world/Map.h>
#include <ostream>
#include <string>
using namespace OpenRCT2;
static std::ostream& operator<<(std::ostream& os, const TileCoordsXYZ& coords)
{
return os << "(" << coords.x << ", " << coords.y << ", " << coords.z << ")";
}
class PathfindingTestBase : public testing::Test
{
public:
static void SetUpTestCase()
{
Platform::CoreInit();
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
_context = CreateContext();
const bool initialised = _context->Initialise();
ASSERT_TRUE(initialised);
std::string parkPath = TestData::GetParkPath("pathfinding-tests.sv6");
GetContext()->LoadParkFromFile(parkPath);
game_load_init();
}
void SetUp() override
{
// Use a consistent random seed in every test
scenario_rand_seed(0x12345678, 0x87654321);
}
static void TearDownTestCase()
{
_context = nullptr;
}
protected:
static Ride* FindRideByName(const char* name)
{
for (auto& ride : GetRideManager())
{
auto thisName = ride.GetName();
if (!_strnicmp(thisName.c_str(), name, sizeof(thisName)))
{
return &ride;
}
}
return nullptr;
}
static bool FindPath(TileCoordsXYZ* pos, const TileCoordsXYZ& goal, int expectedSteps, RideId targetRideID)
{
// Our start position is in tile coordinates, but we need to give the peep spawn
// position in actual world coords (32 units per tile X/Y, 8 per Z level).
// Add 16 so the peep spawns in the centre of the tile.
auto* peep = Guest::Generate(pos->ToCoordsXYZ().ToTileCentre());
// Peeps that are outside of the park use specialized pathfinding which we don't want to
// use here
peep->OutsideOfPark = false;
// An earlier iteration of this code just gave peeps a target position to walk to, but it turns out
// that with no actual ride to head towards, when a peep reaches a junction they use the 'aimless'
// pathfinder instead of pursuing their original pathfinding target. So, we always need to give them
// an actual ride to walk to the entrance of.
peep->GuestHeadingToRideId = targetRideID;
// Pick the direction the peep should initially move in, given the goal position.
// This will also store the goal position and initialize pathfinding data for the peep.
gPeepPathFindGoalPosition = goal;
const Direction moveDir = gGuestPathfinder->ChooseDirection(*pos, *peep);
if (moveDir == INVALID_DIRECTION)
{
// Couldn't determine a direction to move off in
return false;
}
// We have already set up the peep's overall pathfinding goal, but we also have to set their initial
// 'destination' which is a close position that they will walk towards in a straight line - in this case, one
// tile away. Stepping the peep will move them towards their destination, and once they reach it, a new
// destination will be picked, to try and get the peep towards the overall pathfinding goal.
peep->PeepDirection = moveDir;
auto destination = CoordsDirectionDelta[moveDir] + peep->GetLocation();
peep->SetDestination(destination, 2);
// Repeatedly step the peep, until they reach the target position or until the expected number of steps have
// elapsed. Each step, check that the tile they are standing on is not marked as forbidden in the test data
// (red neon ground type).
int step = 0;
while (!(*pos == goal) && step < expectedSteps)
{
uint8_t pathingResult = 0;
peep->PerformNextAction(pathingResult);
++step;
*pos = TileCoordsXYZ(peep->GetLocation());
EXPECT_PRED_FORMAT1(AssertIsNotForbiddenPosition, *pos);
// Check that the peep is still on a footpath. Use next_z instead of pos->z here because pos->z will change
// when the peep is halfway up a slope, but next_z will not change until they move to the next tile.
EXPECT_NE(MapGetFootpathElement({ pos->ToCoordsXY(), peep->NextLoc.z }), nullptr);
}
// Clean up the peep, because we're reusing this loaded context for all tests.
peep_sprite_remove(peep);
// Require that the number of steps taken is exactly what we expected. The pathfinder is supposed to be
// deterministic, and we reset the RNG seed for each test, everything should be entirely repeatable; as
// such a change in the number of steps taken on one of these paths needs to be reviewed. For the negative
// tests, we will not have reached the goal but we still expect the loop to have run for the total number
// of steps requested before giving up.
EXPECT_EQ(step, expectedSteps);
return *pos == goal;
}
static ::testing::AssertionResult AssertIsStartPosition(const char*, const TileCoordsXYZ& location)
{
const uint32_t expectedSurfaceStyle = 11u;
const uint32_t style = MapGetSurfaceElementAt(location.ToCoordsXYZ())->GetSurfaceStyle();
if (style != expectedSurfaceStyle)
return ::testing::AssertionFailure()
<< "Start location " << location << " should have surface style " << expectedSurfaceStyle
<< " but actually has style " << style
<< ". Either the test map is not set up correctly, or you got the coordinates wrong.";
return ::testing::AssertionSuccess();
}
static ::testing::AssertionResult AssertIsNotForbiddenPosition(const char*, const TileCoordsXYZ& location)
{
const uint32_t forbiddenSurfaceStyle = 8u;
const uint32_t style = MapGetSurfaceElementAt(location.ToCoordsXYZ())->GetSurfaceStyle();
if (style == forbiddenSurfaceStyle)
return ::testing::AssertionFailure()
<< "Path traversed location " << location << ", but it is marked as a forbidden location (surface style "
<< forbiddenSurfaceStyle << "). Either the map is set up incorrectly, or the pathfinder went the wrong way.";
return ::testing::AssertionSuccess();
}
private:
static std::shared_ptr<IContext> _context;
};
std::shared_ptr<IContext> PathfindingTestBase::_context;
struct SimplePathfindingScenario
{
const char* name;
TileCoordsXYZ start;
uint32_t steps;
SimplePathfindingScenario(const char* _name, const TileCoordsXYZ& _start, int _steps)
: name(_name)
, start(_start)
, steps(_steps)
{
}
static std::string ToName(const ::testing::TestParamInfo<SimplePathfindingScenario>& param_info)
{
return param_info.param.name;
}
};
class SimplePathfindingTest : public PathfindingTestBase, public ::testing::WithParamInterface<SimplePathfindingScenario>
{
};
TEST_P(SimplePathfindingTest, CanFindPathFromStartToGoal)
{
const SimplePathfindingScenario& scenario = GetParam();
ASSERT_PRED_FORMAT1(AssertIsStartPosition, scenario.start);
TileCoordsXYZ pos = scenario.start;
auto ride = FindRideByName(scenario.name);
ASSERT_NE(ride, nullptr);
auto entrancePos = ride->GetStation().Entrance;
TileCoordsXYZ goal = TileCoordsXYZ(
entrancePos.x - TileDirectionDelta[entrancePos.direction].x,
entrancePos.y - TileDirectionDelta[entrancePos.direction].y, entrancePos.z);
const auto succeeded = FindPath(&pos, goal, scenario.steps, ride->id) ? ::testing::AssertionSuccess()
: ::testing::AssertionFailure()
<< "Failed to find path from " << scenario.start << " to " << goal << " in " << scenario.steps << " steps; reached "
<< pos << " before giving up.";
EXPECT_TRUE(succeeded);
}
INSTANTIATE_TEST_SUITE_P(
ForScenario, SimplePathfindingTest,
::testing::Values(
SimplePathfindingScenario("StraightFlat", { 19, 15, 14 }, 24), SimplePathfindingScenario("SBend", { 15, 12, 14 }, 87),
SimplePathfindingScenario("UBend", { 17, 9, 14 }, 87), SimplePathfindingScenario("CBend", { 14, 5, 14 }, 164),
SimplePathfindingScenario("TwoEqualRoutes", { 9, 13, 14 }, 89),
SimplePathfindingScenario("TwoUnequalRoutes", { 3, 13, 14 }, 89),
SimplePathfindingScenario("StraightUpBridge", { 12, 15, 14 }, 24),
SimplePathfindingScenario("StraightUpSlope", { 14, 15, 14 }, 24),
SimplePathfindingScenario("SelfCrossingPath", { 6, 5, 14 }, 211)),
SimplePathfindingScenario::ToName);
class ImpossiblePathfindingTest : public PathfindingTestBase, public ::testing::WithParamInterface<SimplePathfindingScenario>
{
};
TEST_P(ImpossiblePathfindingTest, CannotFindPathFromStartToGoal)
{
const SimplePathfindingScenario& scenario = GetParam();
TileCoordsXYZ pos = scenario.start;
ASSERT_PRED_FORMAT1(AssertIsStartPosition, scenario.start);
auto ride = FindRideByName(scenario.name);
ASSERT_NE(ride, nullptr);
auto entrancePos = ride->GetStation().Entrance;
TileCoordsXYZ goal = TileCoordsXYZ(
entrancePos.x + TileDirectionDelta[entrancePos.direction].x,
entrancePos.y + TileDirectionDelta[entrancePos.direction].y, entrancePos.z);
EXPECT_FALSE(FindPath(&pos, goal, 10000, ride->id));
}
INSTANTIATE_TEST_SUITE_P(
ForScenario, ImpossiblePathfindingTest,
::testing::Values(
SimplePathfindingScenario("PathWithGap", { 1, 6, 14 }, 10000),
SimplePathfindingScenario("PathWithFences", { 11, 6, 14 }, 10000),
SimplePathfindingScenario("PathWithCliff", { 7, 17, 14 }, 10000)),
SimplePathfindingScenario::ToName);
|