From 0c3c2ebcd2113e316516f7a903393cfc4167683c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 5 Apr 2023 23:13:47 +0300 Subject: [PATCH] Refactor MapGetSurfaceElementAt to accept TileCoordsXY --- src/openrct2/Game.cpp | 2 +- src/openrct2/actions/CheatSetAction.cpp | 2 +- src/openrct2/entity/Peep.cpp | 2 +- src/openrct2/rct2/S6Importer.cpp | 4 ++-- src/openrct2/world/Map.cpp | 19 ++++++++++++------- src/openrct2/world/Map.h | 1 + src/openrct2/world/MapGen.cpp | 14 +++++++------- src/openrct2/world/MapHelpers.cpp | 25 ++++++++++++------------- 8 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 193f2f6660..1c08e50f03 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -414,7 +414,7 @@ void GameFixSaveVars() { for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) { - auto* surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto* surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement == nullptr) { diff --git a/src/openrct2/actions/CheatSetAction.cpp b/src/openrct2/actions/CheatSetAction.cpp index b712dd6a31..d5457dc7e0 100644 --- a/src/openrct2/actions/CheatSetAction.cpp +++ b/src/openrct2/actions/CheatSetAction.cpp @@ -367,7 +367,7 @@ void CheatSetAction::SetGrassLength(int32_t length) const { for (int32_t x = 0; x < gMapSize.x; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement == nullptr) continue; diff --git a/src/openrct2/entity/Peep.cpp b/src/openrct2/entity/Peep.cpp index 9c51a3927c..12bd2997b8 100644 --- a/src/openrct2/entity/Peep.cpp +++ b/src/openrct2/entity/Peep.cpp @@ -628,7 +628,7 @@ GameActions::Result Peep::Place(const TileCoordsXYZ& location, bool apply) TileElement* tileElement = reinterpret_cast(pathElement); if (pathElement == nullptr) { - tileElement = reinterpret_cast(MapGetSurfaceElementAt(location.ToCoordsXYZ())); + tileElement = reinterpret_cast(MapGetSurfaceElementAt(location)); } if (tileElement == nullptr) { diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 97e2eaa33c..04fc5502f2 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -650,7 +650,7 @@ namespace RCT2 if (String::Equals(_s6.ScenarioFilename, "Infernal Views.SC6", true) || String::Equals(_s6.ScenarioFilename, "infernal views.sea", true)) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ 45, 62 }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ 45, 62 }); surfaceElement->SetWaterHeight(96); } @@ -659,7 +659,7 @@ namespace RCT2 || String::Equals(_s6.ScenarioFilename, "six flags holland.sea", true)) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ 126, 73 }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ 126, 73 }); surfaceElement->SetWaterHeight(96); } diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index bbcc019c3d..3f27ff8ee6 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -397,13 +397,18 @@ void MapSetTileElement(const TileCoordsXY& tilePos, TileElement* elements) _tileIndex.SetTile(tilePos, elements); } -SurfaceElement* MapGetSurfaceElementAt(const CoordsXY& coords) +SurfaceElement* MapGetSurfaceElementAt(const TileCoordsXY& coords) { auto view = TileElementsView(coords); return *view.begin(); } +SurfaceElement* MapGetSurfaceElementAt(const CoordsXY& coords) +{ + return MapGetSurfaceElementAt(TileCoordsXY{ coords }); +} + PathElement* MapGetPathElementAt(const TileCoordsXYZ& loc) { for (auto* element : TileElementsView(loc.ToCoordsXY())) @@ -465,7 +470,7 @@ void MapCountRemainingLandRights() { for (int32_t x = 0; x < gMapSize.x; x++) { - auto* surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto* surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); // Surface elements are sometimes hacked out to save some space for other map elements if (surfaceElement == nullptr) { @@ -1475,8 +1480,8 @@ void MapExtendBoundarySurfaceY() auto y = gMapSize.y - 2; for (auto x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) { - auto existingTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y - 1 }.ToCoordsXY()); - auto newTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto existingTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y - 1 }); + auto newTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (existingTileElement != nullptr && newTileElement != nullptr) { @@ -1495,8 +1500,8 @@ void MapExtendBoundarySurfaceX() auto x = gMapSize.x - 2; for (auto y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++) { - auto existingTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y }.ToCoordsXY()); - auto newTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto existingTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y }); + auto newTileElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (existingTileElement != nullptr && newTileElement != nullptr) { MapExtendBoundarySurfaceExtendTile(*existingTileElement, *newTileElement); @@ -2285,7 +2290,7 @@ void FixLandOwnershipTilesWithOwnership(std::initializer_list tile { for (const TileCoordsXY* tile = tiles.begin(); tile != tiles.end(); ++tile) { - auto surfaceElement = MapGetSurfaceElementAt(tile->ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(*tile); if (surfaceElement != nullptr) { if (doNotDowngrade && surfaceElement->GetOwnership() == OWNERSHIP_OWNED) diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h index 2663816076..926886ed3d 100644 --- a/src/openrct2/world/Map.h +++ b/src/openrct2/world/Map.h @@ -168,6 +168,7 @@ TileElement* MapGetFirstTileElementWithBaseHeightBetween(const TileCoordsXYRange void MapSetTileElement(const TileCoordsXY& tilePos, TileElement* elements); int32_t MapHeightFromSlope(const CoordsXY& coords, int32_t slopeDirection, bool isSloped); BannerElement* MapGetBannerElementAt(const CoordsXYZ& bannerPos, uint8_t direction); +SurfaceElement* MapGetSurfaceElementAt(const TileCoordsXY& coords); SurfaceElement* MapGetSurfaceElementAt(const CoordsXY& coords); PathElement* MapGetPathElementAt(const TileCoordsXYZ& loc); WallElement* MapGetWallElementAt(const CoordsXYZD& wallCoords); diff --git a/src/openrct2/world/MapGen.cpp b/src/openrct2/world/MapGen.cpp index 06ad2e9be9..976bf6c79a 100644 --- a/src/openrct2/world/MapGen.cpp +++ b/src/openrct2/world/MapGen.cpp @@ -126,7 +126,7 @@ void MapGenGenerateBlank(MapGenSettings* settings) { for (x = 1; x < settings->mapSize.x - 1; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement != nullptr) { surfaceElement->SetSurfaceStyle(settings->floor); @@ -136,7 +136,7 @@ void MapGenGenerateBlank(MapGenSettings* settings) } } } - + MapGenSetWaterLevel(settings->water_level); } @@ -190,7 +190,7 @@ void MapGenGenerate(MapGenSettings* settings) { for (auto x = 1; x < mapSize.x - 1; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement != nullptr) { surfaceElement->SetSurfaceStyle(floorTextureId); @@ -240,7 +240,7 @@ void MapGenGenerate(MapGenSettings* settings) { for (auto x = 1; x < mapSize.x - 1; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement != nullptr && surfaceElement->BaseHeight < waterLevel + 6) surfaceElement->SetSurfaceStyle(beachTextureId); @@ -419,7 +419,7 @@ static void MapGenSetWaterLevel(int32_t waterLevel) { for (int32_t x = 1; x < gMapSize.x - 1; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement != nullptr && surfaceElement->BaseHeight < waterLevel) surfaceElement->SetWaterHeight(waterLevel * COORDS_Z_STEP); } @@ -480,7 +480,7 @@ static void MapGenSetHeight(MapGenSettings* settings) uint8_t baseHeight = (q00 + q01 + q10 + q11) / 4; - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement == nullptr) continue; surfaceElement->BaseHeight = std::max(2, baseHeight * 2); @@ -847,7 +847,7 @@ void MapGenGenerateFromHeightmap(MapGenSettings* settings) { // The x and y axis are flipped in the world, so this uses y for x and x for y. auto tileCoords = MapgenHeightmapCoordToTileCoordsXY(x, y); - auto* const surfaceElement = MapGetSurfaceElementAt(tileCoords.ToCoordsXY()); + auto* const surfaceElement = MapGetSurfaceElementAt(tileCoords); if (surfaceElement == nullptr) continue; diff --git a/src/openrct2/world/MapHelpers.cpp b/src/openrct2/world/MapHelpers.cpp index 7c69eb7e73..af8bde63e5 100644 --- a/src/openrct2/world/MapHelpers.cpp +++ b/src/openrct2/world/MapHelpers.cpp @@ -16,7 +16,7 @@ static uint8_t GetBaseHeightOrZero(int32_t x, int32_t y) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); return surfaceElement != nullptr ? surfaceElement->BaseHeight : 0; } @@ -31,7 +31,7 @@ int32_t MapSmooth(int32_t l, int32_t t, int32_t r, int32_t b) { for (x = l; x < r; x++) { - auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }.ToCoordsXY()); + auto surfaceElement = MapGetSurfaceElementAt(TileCoordsXY{ x, y }); if (surfaceElement == nullptr) continue; surfaceElement->SetSlope(TILE_ELEMENT_SLOPE_FLAT); @@ -146,36 +146,36 @@ int32_t MapSmooth(int32_t l, int32_t t, int32_t r, int32_t b) { uint8_t slope = surfaceElement->GetSlope(); // Corners - auto surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y + 1 }.ToCoordsXY()); + auto surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y + 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_N_CORNER_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y + 1 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y + 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_W_CORNER_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y - 1 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y - 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_E_CORNER_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y - 1 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y - 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_S_CORNER_UP; // Sides - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y + 0 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 1, y + 0 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_NE_SIDE_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y + 0 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x - 1, y + 0 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_SW_SIDE_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 0, y - 1 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 0, y - 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_SE_SIDE_UP; - surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 0, y + 1 }.ToCoordsXY()); + surfaceElement2 = MapGetSurfaceElementAt(TileCoordsXY{ x + 0, y + 1 }); if (surfaceElement2 != nullptr && surfaceElement2->BaseHeight > surfaceElement->BaseHeight) slope |= TILE_ELEMENT_SLOPE_NW_SIDE_UP; @@ -200,7 +200,7 @@ int32_t MapSmooth(int32_t l, int32_t t, int32_t r, int32_t b) */ int32_t TileSmooth(const TileCoordsXY& tileCoords) { - auto* const surfaceElement = MapGetSurfaceElementAt(tileCoords.ToCoordsXY()); + auto* const surfaceElement = MapGetSurfaceElementAt(tileCoords); if (surfaceElement == nullptr) return 0; @@ -241,8 +241,7 @@ int32_t TileSmooth(const TileCoordsXY& tileCoords) continue; // Get neighbour height. If the element is not valid (outside of map) assume the same height - auto* neighbourSurfaceElement = MapGetSurfaceElementAt( - (tileCoords + TileCoordsXY{ x_offset, y_offset }).ToCoordsXY()); + auto* neighbourSurfaceElement = MapGetSurfaceElementAt(tileCoords + TileCoordsXY{ x_offset, y_offset }); neighbourHeightOffset.baseheight[index] = neighbourSurfaceElement != nullptr ? neighbourSurfaceElement->BaseHeight : surfaceElement->BaseHeight;