1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-06 06:32:56 +01:00

Merge pull request #25587 from Gymnasiast/refactor/slope-checking

Refactor slope checking
This commit is contained in:
Michael Steenbeek
2025-12-02 22:48:15 +01:00
committed by GitHub
11 changed files with 83 additions and 67 deletions

View File

@@ -155,7 +155,7 @@ namespace OpenRCT2::GameActions
auto crossingMode = isQueue || (_slope.type != FootpathSlopeType::flat) ? CreateCrossingMode::none
: CreateCrossingMode::pathOverTrack;
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, crossingMode);
{ _loc, zLow, zHigh }, MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, crossingMode);
if (!entrancePath && canBuild.Error != Status::Ok)
{
canBuild.ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
@@ -224,7 +224,7 @@ namespace OpenRCT2::GameActions
auto crossingMode = isQueue || (_slope.type != FootpathSlopeType::flat) ? CreateCrossingMode::none
: CreateCrossingMode::pathOverTrack;
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceNonSceneryClearFunc, quarterTile, GAME_COMMAND_FLAG_APPLY | GetFlags(),
{ _loc, zLow, zHigh }, MapPlaceNonSceneryClearFunc, quarterTile, GAME_COMMAND_FLAG_APPLY | GetFlags(),
kTileSlopeFlat, crossingMode);
if (!entrancePath && canBuild.Error != Status::Ok)
{

View File

@@ -320,7 +320,7 @@ namespace OpenRCT2::GameActions
auto crossingMode = isQueue || (_slope.type != FootpathSlopeType::flat) ? CreateCrossingMode::none
: CreateCrossingMode::pathOverTrack;
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, crossingMode);
{ _loc, zLow, zHigh }, MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, crossingMode);
if (!entrancePath && canBuild.Error != Status::Ok)
{
canBuild.ErrorTitle = STR_CANT_BUILD_FOOTPATH_HERE;
@@ -388,7 +388,7 @@ namespace OpenRCT2::GameActions
auto crossingMode = isQueue || (_slope.type != FootpathSlopeType::flat) ? CreateCrossingMode::none
: CreateCrossingMode::pathOverTrack;
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceNonSceneryClearFunc, quarterTile, GAME_COMMAND_FLAG_APPLY | GetFlags(),
{ _loc, zLow, zHigh }, MapPlaceNonSceneryClearFunc, quarterTile, GAME_COMMAND_FLAG_APPLY | GetFlags(),
kTileSlopeFlat, crossingMode);
if (!entrancePath && canBuild.Error != Status::Ok)
{

View File

@@ -140,7 +140,7 @@ namespace OpenRCT2::GameActions
}
auto clearResult = MapCanConstructWithClearAt(
{ _coords, _height * kCoordsZStep, zCorner * kCoordsZStep }, &MapSetLandHeightClearFunc, { 0b1111, 0 }, 0,
{ _coords, _height * kCoordsZStep, zCorner * kCoordsZStep }, MapSetLandHeightClearFunc, { 0b1111, 0 }, 0,
_style, CreateCrossingMode::none);
if (clearResult.Error != Status::Ok)
{
@@ -347,16 +347,16 @@ namespace OpenRCT2::GameActions
MapInvalidateTileFull(_coords);
}
int32_t LandSetHeightAction::MapSetLandHeightClearFunc(
bool LandSetHeightAction::MapSetLandHeightClearFunc(
TileElement** tile_element, [[maybe_unused]] const CoordsXY& coords, [[maybe_unused]] uint8_t flags,
[[maybe_unused]] money64* price)
{
if ((*tile_element)->GetType() == TileElementType::Surface)
return 0;
return true;
if ((*tile_element)->GetType() == TileElementType::SmallScenery)
return 0;
return true;
return 1;
return false;
}
} // namespace OpenRCT2::GameActions

View File

@@ -46,7 +46,7 @@ namespace OpenRCT2::GameActions
*
* rct2: 0x00663CB9
*/
static int32_t MapSetLandHeightClearFunc(
static bool MapSetLandHeightClearFunc(
TileElement** tile_element, [[maybe_unused]] const CoordsXY& coords, [[maybe_unused]] uint8_t flags,
[[maybe_unused]] money64* price);
};

View File

@@ -138,7 +138,7 @@ namespace OpenRCT2::GameActions
QuarterTile quarterTile = QuarterTile{ tile.corners, 0 }.Rotate(_loc.direction);
const auto isTree = (sceneryEntry->flags & LARGE_SCENERY_FLAG_IS_TREE) != 0;
auto canBuild = MapCanConstructWithClearAt(
{ curTile, zLow, zHigh }, &MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
{ curTile, zLow, zHigh }, MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
CreateCrossingMode::none, isTree);
if (canBuild.Error != Status::Ok)
{
@@ -270,7 +270,7 @@ namespace OpenRCT2::GameActions
QuarterTile quarterTile = QuarterTile{ tile.corners, 0 }.Rotate(_loc.direction);
const auto isTree = (sceneryEntry->flags & LARGE_SCENERY_FLAG_IS_TREE) != 0;
auto canBuild = MapCanConstructWithClearAt(
{ curTile, zLow, zHigh }, &MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
{ curTile, zLow, zHigh }, MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
CreateCrossingMode::none, isTree);
if (canBuild.Error != Status::Ok)
{

View File

@@ -107,7 +107,7 @@ namespace OpenRCT2::GameActions
}
auto canBuild = MapCanConstructWithClearAt(
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags(),
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags(),
kTileSlopeFlat);
if (canBuild.Error != Status::Ok)
{
@@ -172,7 +172,7 @@ namespace OpenRCT2::GameActions
auto clearanceHeight = _loc.z + kMazeClearanceHeight;
auto canBuild = MapCanConstructWithClearAt(
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &MapPlaceNonSceneryClearFunc, { 0b1111, 0 },
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, MapPlaceNonSceneryClearFunc, { 0b1111, 0 },
GetFlags() | GAME_COMMAND_FLAG_APPLY, kTileSlopeFlat);
if (canBuild.Error != Status::Ok)
{

View File

@@ -116,7 +116,7 @@ namespace OpenRCT2::GameActions
}
auto clear_z = z + (_isExit ? RideExitHeight : RideEntranceHeight);
auto canBuild = MapCanConstructWithClearAt(
{ _loc, z, clear_z }, &MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags(), kTileSlopeFlat);
{ _loc, z, clear_z }, MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags(), kTileSlopeFlat);
if (canBuild.Error != Status::Ok)
{
canBuild.ErrorTitle = errorTitle;
@@ -186,7 +186,7 @@ namespace OpenRCT2::GameActions
auto clear_z = z + (_isExit ? RideExitHeight : RideEntranceHeight);
auto canBuild = MapCanConstructWithClearAt(
{ _loc, z, clear_z }, &MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags() | GAME_COMMAND_FLAG_APPLY,
{ _loc, z, clear_z }, MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, GetFlags() | GAME_COMMAND_FLAG_APPLY,
kTileSlopeFlat);
if (canBuild.Error != Status::Ok)
{
@@ -254,7 +254,7 @@ namespace OpenRCT2::GameActions
int16_t baseZ = loc.z;
int16_t clearZ = baseZ + (isExit ? RideExitHeight : RideEntranceHeight);
auto canBuild = MapCanConstructWithClearAt(
{ loc, baseZ, clearZ }, &MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, 0, kTileSlopeFlat);
{ loc, baseZ, clearZ }, MapPlaceNonSceneryClearFunc, { 0b1111, 0 }, 0, kTileSlopeFlat);
if (canBuild.Error != Status::Ok)
{
canBuild.ErrorTitle = errorTitle;

View File

@@ -267,7 +267,7 @@ namespace OpenRCT2::GameActions
QuarterTile quarterTile = QuarterTile{ collisionQuadrants, supports }.Rotate(quadRotation);
const auto isTree = sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_IS_TREE);
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, CreateCrossingMode::none,
{ _loc, zLow, zHigh }, MapPlaceSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat, CreateCrossingMode::none,
isTree);
if (canBuild.Error != Status::Ok)
{
@@ -406,7 +406,7 @@ namespace OpenRCT2::GameActions
QuarterTile quarterTile = QuarterTile{ collisionQuadrants, supports }.Rotate(quadRotation);
const auto isTree = sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_IS_TREE);
auto canBuild = MapCanConstructWithClearAt(
{ _loc, zLow, zHigh }, &MapPlaceSceneryClearFunc, quarterTile, GetFlags() | GAME_COMMAND_FLAG_APPLY, kTileSlopeFlat,
{ _loc, zLow, zHigh }, MapPlaceSceneryClearFunc, quarterTile, GetFlags() | GAME_COMMAND_FLAG_APPLY, kTileSlopeFlat,
CreateCrossingMode::none, isTree);
if (canBuild.Error != Status::Ok)
{

View File

@@ -263,7 +263,7 @@ namespace OpenRCT2::GameActions
? CreateCrossingMode::trackOverPath
: CreateCrossingMode::none;
auto canBuild = MapCanConstructWithClearAt(
{ mapLoc, baseZ, clearanceZ }, &MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
{ mapLoc, baseZ, clearanceZ }, MapPlaceNonSceneryClearFunc, quarterTile, GetFlags(), kTileSlopeFlat,
crossingMode);
if (canBuild.Error != Status::Ok)
{
@@ -468,7 +468,7 @@ namespace OpenRCT2::GameActions
? CreateCrossingMode::trackOverPath
: CreateCrossingMode::none;
auto canBuild = MapCanConstructWithClearAt(
mapLocWithClearance, &MapPlaceNonSceneryClearFunc, quarterTile, GetFlags() | GAME_COMMAND_FLAG_APPLY,
mapLocWithClearance, MapPlaceNonSceneryClearFunc, quarterTile, GetFlags() | GAME_COMMAND_FLAG_APPLY,
kTileSlopeFlat, crossingMode);
if (canBuild.Error != Status::Ok)
{

View File

@@ -35,14 +35,14 @@
using namespace OpenRCT2;
static int32_t MapPlaceClearFunc(
static bool MapPlaceClearFunc(
TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price, bool is_scenery)
{
if ((*tile_element)->GetType() != TileElementType::SmallScenery)
return 1;
return false;
if (is_scenery && !(flags & GAME_COMMAND_FLAG_TRACK_DESIGN))
return 1;
return false;
auto* scenery = (*tile_element)->AsSmallScenery()->GetEntry();
@@ -50,31 +50,31 @@ static int32_t MapPlaceClearFunc(
if (park.flags & PARK_FLAGS_FORBID_TREE_REMOVAL)
{
if (scenery != nullptr && scenery->HasFlag(SMALL_SCENERY_FLAG_IS_TREE))
return 1;
return false;
}
if (!(park.flags & PARK_FLAGS_NO_MONEY) && scenery != nullptr)
*price += scenery->removal_price;
if (flags & GAME_COMMAND_FLAG_GHOST)
return 0;
return true;
if (!(flags & GAME_COMMAND_FLAG_APPLY))
return 0;
return true;
MapInvalidateTile({ coords, (*tile_element)->GetBaseZ(), (*tile_element)->GetClearanceZ() });
TileElementRemove(*tile_element);
(*tile_element)--;
return 0;
return true;
}
/**
*
* rct2: 0x006E0D6E, 0x006B8D88
*/
int32_t MapPlaceSceneryClearFunc(TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price)
bool MapPlaceSceneryClearFunc(TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price)
{
return MapPlaceClearFunc(tile_element, coords, flags, price, /*is_scenery=*/true);
}
@@ -83,42 +83,58 @@ int32_t MapPlaceSceneryClearFunc(TileElement** tile_element, const CoordsXY& coo
*
* rct2: 0x006C5A4F, 0x006CDE57, 0x006A6733, 0x0066637E
*/
int32_t MapPlaceNonSceneryClearFunc(TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price)
bool MapPlaceNonSceneryClearFunc(TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price)
{
return MapPlaceClearFunc(tile_element, coords, flags, price, /*is_scenery=*/false);
}
static bool landSlopeFitsUnderTrack(int32_t baseZ, uint8_t slope, const TrackElement& trackElement)
{
const auto [slopeNorthZ, slopeEastZ, slopeSouthZ, slopeWestZ] = GetSlopeCornerHeights(baseZ, slope);
const TrackElemType trackElemType = trackElement.GetTrackType();
const auto& ted = OpenRCT2::TrackMetaData::GetTrackElementDescriptor(trackElemType);
const uint8_t sequenceIndex = trackElemType == TrackElemType::Maze ? 0 : trackElement.GetSequenceIndex();
const auto& trackClearances = ted.sequences[sequenceIndex].clearance;
const auto trackQuarters = trackClearances.quarterTile.Rotate(trackElement.GetDirection());
const auto trackQuarterHeights = trackQuarters.GetQuarterHeights(trackElement.GetBaseZ());
const uint8_t trackOccupiedQuarters = trackQuarters.GetBaseQuarterOccupied();
if ((!(trackOccupiedQuarters & 0b0001) || slopeNorthZ <= trackQuarterHeights.north)
&& (!(trackOccupiedQuarters & 0b0010) || slopeEastZ <= trackQuarterHeights.east)
&& (!(trackOccupiedQuarters & 0b0100) || slopeSouthZ <= trackQuarterHeights.south)
&& (!(trackOccupiedQuarters & 0b1000) || slopeWestZ <= trackQuarterHeights.west))
{
return true;
}
return false;
}
static bool landSlopeFitsUnderPath(int32_t baseZ, uint8_t slope, const PathElement& pathElement)
{
const auto slopeCornerHeights = GetSlopeCornerHeights(baseZ, slope);
const uint8_t pathSlope = Numerics::rol4(kTileSlopeSWSideUp, pathElement.GetSlopeDirection());
const auto pathCornerHeights = GetSlopeCornerHeights(pathElement.GetBaseZ(), pathSlope);
return (slopeCornerHeights <= pathCornerHeights);
}
static bool MapLoc68BABCShouldContinue(
TileElement** tileElementPtr, const CoordsXYRangedZ& pos, CLEAR_FUNC clearFunc, const uint8_t flags, money64& price,
TileElement** tileElementPtr, const CoordsXYRangedZ& pos, ClearingFunction clearFunc, const uint8_t flags, money64& price,
const CreateCrossingMode crossingMode, const bool canBuildCrossing, const uint8_t slope)
{
if (clearFunc != nullptr)
if (clearFunc(tileElementPtr, pos, flags, &price))
{
if (!clearFunc(tileElementPtr, pos, flags, &price))
{
return true;
}
return true;
}
const TileElement* const tileElement = *tileElementPtr;
if (slope != kTileSlopeFlat && tileElement->GetType() == TileElementType::Track)
{
const auto [slopeNorthZ, slopeEastZ, slopeSouthZ, slopeWestZ] = GetSlopeCornerHeights(pos.baseZ, slope);
const TrackElement* const trackElement = tileElement->AsTrack();
const TrackElemType trackElemType = trackElement->GetTrackType();
const auto& ted = OpenRCT2::TrackMetaData::GetTrackElementDescriptor(trackElemType);
const uint8_t sequenceIndex = trackElemType == TrackElemType::Maze ? 0 : trackElement->GetSequenceIndex();
const auto& trackClearances = ted.sequences[sequenceIndex].clearance;
const auto trackQuarters = trackClearances.quarterTile.Rotate(trackElement->GetDirection());
const auto trackQuarterHeights = trackQuarters.GetQuarterHeights(trackElement->GetBaseZ());
const uint8_t trackOccupiedQuarters = trackQuarters.GetBaseQuarterOccupied();
if ((!(trackOccupiedQuarters & 0b0001) || slopeNorthZ <= trackQuarterHeights.north)
&& (!(trackOccupiedQuarters & 0b0010) || slopeEastZ <= trackQuarterHeights.east)
&& (!(trackOccupiedQuarters & 0b0100) || slopeSouthZ <= trackQuarterHeights.south)
&& (!(trackOccupiedQuarters & 0b1000) || slopeWestZ <= trackQuarterHeights.west))
if (landSlopeFitsUnderTrack(pos.baseZ, slope, *tileElement->AsTrack()))
{
return true;
}
@@ -126,13 +142,7 @@ static bool MapLoc68BABCShouldContinue(
if (slope != kTileSlopeFlat && tileElement->GetType() == TileElementType::Path && tileElement->AsPath()->IsSloped())
{
const auto slopeCornerHeights = GetSlopeCornerHeights(pos.baseZ, slope);
const PathElement& pathElement = *tileElement->AsPath();
const uint8_t pathSlope = Numerics::rol4(kTileSlopeSWSideUp, pathElement.GetSlopeDirection());
const auto pathCornerHeights = GetSlopeCornerHeights(pathElement.GetBaseZ(), pathSlope);
if (slopeCornerHeights <= pathCornerHeights)
if (landSlopeFitsUnderPath(pos.baseZ, slope, *tileElement->AsPath()))
{
return true;
}
@@ -169,8 +179,8 @@ static bool MapLoc68BABCShouldContinue(
* bl = bl
*/
GameActions::Result MapCanConstructWithClearAt(
const CoordsXYRangedZ& pos, CLEAR_FUNC clearFunc, const QuarterTile quarterTile, const uint8_t flags, const uint8_t slope,
const CreateCrossingMode crossingMode, const bool isTree)
const CoordsXYRangedZ& pos, ClearingFunction clearFunc, const QuarterTile quarterTile, const uint8_t flags,
const uint8_t slope, const CreateCrossingMode crossingMode, const bool isTree)
{
auto res = GameActions::Result();
@@ -228,7 +238,7 @@ GameActions::Result MapCanConstructWithClearAt(
groundFlags |= ELEMENT_IS_UNDERWATER;
if (waterHeight < pos.clearanceZ)
{
if (clearFunc != nullptr && clearFunc(&tileElement, pos, flags, &res.Cost))
if (!clearFunc(&tileElement, pos, flags, &res.Cost))
{
res.Error = GameActions::Status::NoClearance;
res.ErrorMessage = STR_CANNOT_BUILD_PARTLY_ABOVE_AND_PARTLY_BELOW_WATER;
@@ -297,9 +307,16 @@ GameActions::Result MapCanConstructWithClearAt(
return res;
}
static bool dummyClearFunc(
[[maybe_unused]] OpenRCT2::TileElement** tile_element, [[maybe_unused]] const CoordsXY& coords,
[[maybe_unused]] uint8_t flags, [[maybe_unused]] money64* price)
{
return false;
}
GameActions::Result MapCanConstructAt(const CoordsXYRangedZ& pos, QuarterTile bl)
{
return MapCanConstructWithClearAt(pos, nullptr, bl, 0, kTileSlopeFlat);
return MapCanConstructWithClearAt(pos, dummyClearFunc, bl, 0, kTileSlopeFlat);
}
/**

View File

@@ -23,7 +23,7 @@ struct CoordsXY;
struct CoordsXYRangedZ;
class QuarterTile;
using CLEAR_FUNC = int32_t (*)(OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
using ClearingFunction = bool (&)(OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
enum
{
@@ -42,9 +42,8 @@ enum class CreateCrossingMode
pathOverTrack,
};
int32_t MapPlaceNonSceneryClearFunc(
OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
int32_t MapPlaceSceneryClearFunc(OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
bool MapPlaceNonSceneryClearFunc(OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
bool MapPlaceSceneryClearFunc(OpenRCT2::TileElement** tile_element, const CoordsXY& coords, uint8_t flags, money64* price);
struct ConstructClearResult
{
@@ -52,7 +51,7 @@ struct ConstructClearResult
};
[[nodiscard]] OpenRCT2::GameActions::Result MapCanConstructWithClearAt(
const CoordsXYRangedZ& pos, CLEAR_FUNC clearFunc, QuarterTile quarterTile, uint8_t flags, uint8_t slope,
const CoordsXYRangedZ& pos, ClearingFunction clearFunc, QuarterTile quarterTile, uint8_t flags, uint8_t slope,
CreateCrossingMode crossingMode = CreateCrossingMode::none, bool isTree = false);
[[nodiscard]] OpenRCT2::GameActions::Result MapCanConstructAt(const CoordsXYRangedZ& pos, QuarterTile bl);