From 6e200459d3f4e50199d2c49a9315415f40889b02 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 24 Aug 2019 22:41:01 +0200 Subject: [PATCH] Refactor some accessors to tile element type and flags fields --- src/openrct2-ui/windows/RideConstruction.cpp | 3 +- src/openrct2/core/DataSerialiserTraits.h | 8 ++++++ src/openrct2/rct1/S4Importer.cpp | 6 ++-- src/openrct2/ride/TrackDesign.cpp | 2 +- src/openrct2/ride/Vehicle.cpp | 15 +++++----- src/openrct2/world/Map.cpp | 9 +++--- src/openrct2/world/MapAnimation.cpp | 2 +- src/openrct2/world/TileElement.cpp | 14 ++++++++++ src/openrct2/world/TileElement.h | 9 ++---- src/openrct2/world/TileInspector.cpp | 10 ++----- test/testpaint/Compat.cpp | 13 +++++++++ test/testpaint/TestTrack.cpp | 29 ++++++++------------ test/testpaint/generate.cpp | 4 +-- 13 files changed, 74 insertions(+), 50 deletions(-) diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index 3de0bbb2f0..2088f61e2a 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -2453,7 +2453,8 @@ static void sub_6CBCE2( _tempTrackTileElement.SetType(TILE_ELEMENT_TYPE_TRACK); _tempTrackTileElement.SetDirection(trackDirection); _tempTrackTileElement.AsTrack()->SetHasChain((edx & 0x10000) != 0); - _tempTrackTileElement.flags = quarterTile.GetBaseQuarterOccupied() | TILE_ELEMENT_FLAG_LAST_TILE; + _tempTrackTileElement.flags = quarterTile.GetBaseQuarterOccupied(); + _tempTrackTileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); _tempTrackTileElement.base_height = baseZ; _tempTrackTileElement.clearance_height = clearanceZ; _tempTrackTileElement.AsTrack()->SetTrackType(trackType); diff --git a/src/openrct2/core/DataSerialiserTraits.h b/src/openrct2/core/DataSerialiserTraits.h index f44389e675..c6b91dc10e 100644 --- a/src/openrct2/core/DataSerialiserTraits.h +++ b/src/openrct2/core/DataSerialiserTraits.h @@ -393,6 +393,10 @@ template<> struct DataSerializerTraits { stream->WriteValue(tileElement.pad_04[i]); } + for (int i = 0; i < 8; ++i) + { + stream->WriteValue(tileElement.pad_08[i]); + } } static void decode(IStream* stream, TileElement& tileElement) { @@ -404,6 +408,10 @@ template<> struct DataSerializerTraits { tileElement.pad_04[i] = stream->ReadValue(); } + for (int i = 0; i < 8; ++i) + { + tileElement.pad_08[i] = stream->ReadValue(); + } } static void log(IStream* stream, const TileElement& tileElement) { diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 8cd788f202..c506723857 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -2015,7 +2015,7 @@ private: void ImportTileElement(TileElement* dst, const RCT12TileElement* src) { - // Todo: allow for changing defition of OpenRCT2 tile element types - replace with a map + // Todo: allow for changing definition of OpenRCT2 tile element types - replace with a map uint8_t tileElementType = src->GetType(); dst->ClearAs(tileElementType); dst->SetDirection(src->GetDirection()); @@ -2694,7 +2694,7 @@ private: for (int32_t y = 0; y < RCT1_MAX_MAP_SIZE; y++) { nextFreeTileElement->ClearAs(TILE_ELEMENT_TYPE_SURFACE); - nextFreeTileElement->flags = TILE_ELEMENT_FLAG_LAST_TILE; + nextFreeTileElement->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); nextFreeTileElement->AsSurface()->SetSlope(TILE_ELEMENT_SLOPE_FLAT); nextFreeTileElement->AsSurface()->SetSurfaceStyle(TERRAIN_GRASS); nextFreeTileElement->AsSurface()->SetEdgeStyle(TERRAIN_EDGE_ROCK); @@ -2708,7 +2708,7 @@ private: for (int32_t y = 0; y < 128 * 256; y++) { nextFreeTileElement->ClearAs(TILE_ELEMENT_TYPE_SURFACE); - nextFreeTileElement->flags = TILE_ELEMENT_FLAG_LAST_TILE; + nextFreeTileElement->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); nextFreeTileElement->AsSurface()->SetSlope(TILE_ELEMENT_SLOPE_FLAT); nextFreeTileElement->AsSurface()->SetSurfaceStyle(TERRAIN_GRASS); nextFreeTileElement->AsSurface()->SetEdgeStyle(TERRAIN_EDGE_ROCK); diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index 32e616a506..a4ef65e500 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -2411,7 +2411,7 @@ static void track_design_preview_clear_map() { TileElement* tile_element = &gTileElements[i]; tile_element->ClearAs(TILE_ELEMENT_TYPE_SURFACE); - tile_element->flags = TILE_ELEMENT_FLAG_LAST_TILE; + tile_element->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tile_element->AsSurface()->SetSlope(TILE_ELEMENT_SLOPE_FLAT); tile_element->AsSurface()->SetWaterHeight(0); tile_element->AsSurface()->SetSurfaceStyle(TERRAIN_GRASS); diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index a36e058338..7c8762e954 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -5169,18 +5169,18 @@ static TileElement* vehicle_check_collision(int16_t x, int16_t y, int16_t z) return nullptr; } - uint8_t bl; + uint8_t quadrant; if ((x & 0x1F) >= 16) { - bl = 1; + quadrant = 1; if ((y & 0x1F) < 16) - bl = 2; + quadrant = 2; } else { - bl = 4; + quadrant = 4; if ((y & 0x1F) >= 16) - bl = 8; + quadrant = 8; } do @@ -5191,7 +5191,7 @@ static TileElement* vehicle_check_collision(int16_t x, int16_t y, int16_t z) if (z / 8 >= tileElement->clearance_height) continue; - if (tileElement->flags & bl) + if (tileElement->flags & quadrant) return tileElement; } while (!(tileElement++)->IsLastForTile()); @@ -6769,7 +6769,8 @@ static void vehicle_update_block_brakes_open_previous_section(rct_vehicle* vehic slowY = slowTrackBeginEnd.end_y; slowTileElement = *(slowTrackBeginEnd.begin_element); if (slowX == x && slowY == y && slowTileElement.base_height == tileElement->base_height - && slowTileElement.type == tileElement->type) + && slowTileElement.GetType() == tileElement->GetType() + && slowTileElement.GetDirection() == tileElement->GetDirection()) { return; } diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index db2b487bef..436fe2cbb4 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -310,7 +310,7 @@ void map_init(int32_t size) { TileElement* tile_element = &gTileElements[i]; tile_element->ClearAs(TILE_ELEMENT_TYPE_SURFACE); - tile_element->flags = TILE_ELEMENT_FLAG_LAST_TILE; + tile_element->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tile_element->base_height = 14; tile_element->clearance_height = 14; tile_element->AsSurface()->SetWaterHeight(0); @@ -957,7 +957,7 @@ void tile_element_remove(TileElement* tileElement) } // Mark the latest element with the last element flag. - (tileElement - 1)->flags |= TILE_ELEMENT_FLAG_LAST_TILE; + (tileElement - 1)->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement->base_height = 0xFF; if ((tileElement + 1) == gNextFreeTileElement) @@ -1178,10 +1178,11 @@ TileElement* tile_element_insert(const TileCoordsXYZ& loc, int32_t flags) originalTileElement++; newTileElement++; - if ((newTileElement - 1)->flags & TILE_ELEMENT_FLAG_LAST_TILE) + if ((newTileElement - 1)->IsLastForTile()) { // No more elements above the insert element - (newTileElement - 1)->flags &= ~TILE_ELEMENT_FLAG_LAST_TILE; + (newTileElement - 1)->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, false); + ; flags |= TILE_ELEMENT_FLAG_LAST_TILE; break; } diff --git a/src/openrct2/world/MapAnimation.cpp b/src/openrct2/world/MapAnimation.cpp index aea7adfb70..85adc90060 100644 --- a/src/openrct2/world/MapAnimation.cpp +++ b/src/openrct2/world/MapAnimation.cpp @@ -131,7 +131,7 @@ static bool map_animation_invalidate_queue_banner(int32_t x, int32_t y, int32_t continue; if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH) continue; - if (!(tileElement->flags & 1)) + if (!(tileElement->AsPath()->IsQueue())) continue; if (!tileElement->AsPath()->HasQueueBanner()) continue; diff --git a/src/openrct2/world/TileElement.cpp b/src/openrct2/world/TileElement.cpp index 9e05815f5f..3efd7d3e48 100644 --- a/src/openrct2/world/TileElement.cpp +++ b/src/openrct2/world/TileElement.cpp @@ -154,6 +154,7 @@ void TileElement::ClearAs(uint8_t newType) base_height = 2; clearance_height = 2; std::fill_n(pad_04, sizeof(pad_04), 0x00); + std::fill_n(pad_08, sizeof(pad_08), 0x00); } void TileElementBase::Remove() @@ -204,3 +205,16 @@ const QuarterTile QuarterTile::Rotate(uint8_t amount) const return QuarterTile{ 0 }; } } + +bool TileElementBase::HasFlag(uint8_t flag) const +{ + return (flags & flag); +} + +void TileElementBase::SetFlag(uint8_t flag, bool on) +{ + if (on) + flags |= flag; + else + flags &= ~flag; +} diff --git a/src/openrct2/world/TileElement.h b/src/openrct2/world/TileElement.h index cf25092a6b..a1fe1cdcb9 100644 --- a/src/openrct2/world/TileElement.h +++ b/src/openrct2/world/TileElement.h @@ -74,6 +74,9 @@ struct TileElementBase bool IsGhost() const; void SetGhost(bool isGhost); void Remove(); + + bool HasFlag(uint8_t flag) const; + void SetFlag(uint8_t flag, bool on); }; /** @@ -123,10 +126,6 @@ public: { return as(); } - CorruptElement* AsCorrupt() const - { - return as(); - } void ClearAs(uint8_t newType); }; @@ -246,8 +245,6 @@ public: uint8_t GetAdditionStatus() const; void SetAdditionStatus(uint8_t newStatus); - uint8_t GetRCT1PathType() const; - bool ShouldDrawPathOverSupports(); void SetShouldDrawPathOverSupports(bool on); }; diff --git a/src/openrct2/world/TileInspector.cpp b/src/openrct2/world/TileInspector.cpp index e40676e139..80bc7eb025 100644 --- a/src/openrct2/world/TileInspector.cpp +++ b/src/openrct2/world/TileInspector.cpp @@ -65,8 +65,8 @@ static bool map_swap_elements_at(CoordsXY loc, int16_t first, int16_t second) // Swap the 'last map element for tile' flag if either one of them was last if ((firstElement)->IsLastForTile() || (secondElement)->IsLastForTile()) { - firstElement->flags ^= TILE_ELEMENT_FLAG_LAST_TILE; - secondElement->flags ^= TILE_ELEMENT_FLAG_LAST_TILE; + firstElement->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, !firstElement->IsLastForTile()); + secondElement->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, !secondElement->IsLastForTile()); } return true; @@ -324,11 +324,7 @@ GameActionResult::Ptr tile_inspector_paste_element_at(CoordsXY loc, TileElement bool lastForTile = pastedElement->IsLastForTile(); *pastedElement = element; - pastedElement->flags &= ~TILE_ELEMENT_FLAG_LAST_TILE; - if (lastForTile) - { - pastedElement->flags |= TILE_ELEMENT_FLAG_LAST_TILE; - } + pastedElement->SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, lastForTile); map_invalidate_tile_full(loc.x, loc.y); diff --git a/test/testpaint/Compat.cpp b/test/testpaint/Compat.cpp index 35fae2e08d..61eb2a01ca 100644 --- a/test/testpaint/Compat.cpp +++ b/test/testpaint/Compat.cpp @@ -155,6 +155,19 @@ rct_sprite* get_sprite(size_t sprite_idx) return &sprite_list[sprite_idx]; } +bool TileElementBase::HasFlag(uint8_t flag) const +{ + return (flags & flag); +} + +void TileElementBase::SetFlag(uint8_t flag, bool on) +{ + if (on) + flags |= flag; + else + flags &= ~flag; +} + bool TileElementBase::IsLastForTile() const { return (this->flags & TILE_ELEMENT_FLAG_LAST_TILE) != 0; diff --git a/test/testpaint/TestTrack.cpp b/test/testpaint/TestTrack.cpp index 13617be7dd..ff26a725c9 100644 --- a/test/testpaint/TestTrack.cpp +++ b/test/testpaint/TestTrack.cpp @@ -101,14 +101,7 @@ public: uint8_t rideType, uint8_t trackType, int variant, TileElement* tileElement, TileElement* surfaceElement, Ride* ride, rct_ride_entry* rideEntry) override { - if (variant == 0) - { - tileElement->type &= ~TRACK_ELEMENT_TYPE_FLAG_CHAIN_LIFT; - } - else - { - tileElement->type |= TRACK_ELEMENT_TYPE_FLAG_CHAIN_LIFT; - } + tileElement->AsTrack()->SetHasChain(variant != 0); } }; @@ -269,13 +262,13 @@ static uint8_t TestTrackElementPaintCalls(uint8_t rideType, uint8_t trackType, u TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = height / 16; g_currently_drawn_item = &tileElement; TileElement surfaceElement = {}; - surfaceElement.type = TILE_ELEMENT_TYPE_SURFACE; + surfaceElement.SetType(TILE_ELEMENT_TYPE_SURFACE); surfaceElement.base_height = 2; gSurfaceElement = &surfaceElement; gDidPassSurface = true; @@ -432,13 +425,13 @@ static uint8_t TestTrackElementSegmentSupportHeight( TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = height / 16; g_currently_drawn_item = &tileElement; TileElement surfaceElement = {}; - surfaceElement.type = TILE_ELEMENT_TYPE_SURFACE; + surfaceElement.SetType(TILE_ELEMENT_TYPE_SURFACE); surfaceElement.base_height = 2; gSurfaceElement = &surfaceElement; gDidPassSurface = true; @@ -519,13 +512,13 @@ static uint8_t TestTrackElementGeneralSupportHeight( TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = height / 16; g_currently_drawn_item = &tileElement; TileElement surfaceElement = {}; - surfaceElement.type = TILE_ELEMENT_TYPE_SURFACE; + surfaceElement.SetType(TILE_ELEMENT_TYPE_SURFACE); surfaceElement.base_height = 2; gSurfaceElement = &surfaceElement; gDidPassSurface = true; @@ -620,13 +613,13 @@ static uint8_t TestTrackElementSideTunnels(uint8_t rideType, uint8_t trackType, TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = height / 16; g_currently_drawn_item = &tileElement; TileElement surfaceElement = {}; - surfaceElement.type = TILE_ELEMENT_TYPE_SURFACE; + surfaceElement.SetType(TILE_ELEMENT_TYPE_SURFACE); surfaceElement.base_height = 2; gSurfaceElement = &surfaceElement; gDidPassSurface = true; @@ -748,13 +741,13 @@ static uint8_t TestTrackElementVerticalTunnels(uint8_t rideType, uint8_t trackTy TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = height / 16; g_currently_drawn_item = &tileElement; TileElement surfaceElement = {}; - surfaceElement.type = TILE_ELEMENT_TYPE_SURFACE; + surfaceElement.SetType(TILE_ELEMENT_TYPE_SURFACE); surfaceElement.base_height = 2; gSurfaceElement = &surfaceElement; gDidPassSurface = true; diff --git a/test/testpaint/generate.cpp b/test/testpaint/generate.cpp index 28cd40dfee..bf5ca86faa 100644 --- a/test/testpaint/generate.cpp +++ b/test/testpaint/generate.cpp @@ -446,7 +446,7 @@ private: { TileElement tileElement = {}; tileElement.SetType(TILE_ELEMENT_TYPE_TRACK); - tileElement.flags |= TILE_ELEMENT_FLAG_LAST_TILE; + tileElement.SetFlag(TILE_ELEMENT_FLAG_LAST_TILE, true); tileElement.AsTrack()->SetTrackType(trackType); tileElement.base_height = 3; if (_invertedTrack) @@ -482,7 +482,7 @@ private: } // Get chain lift calls - tileElement.type |= 0x80; + tileElement.AsTrack()->SetHasChain(true); PaintIntercept::ClearCalls(); CallOriginal(trackType, direction, trackSequence, height, &tileElement); numCalls = PaintIntercept::GetCalls(callBuffer);