diff --git a/src/openrct2-ui/windows/Guest.cpp b/src/openrct2-ui/windows/Guest.cpp index a849035142..b4331995d5 100644 --- a/src/openrct2-ui/windows/Guest.cpp +++ b/src/openrct2-ui/windows/Guest.cpp @@ -677,7 +677,7 @@ namespace OpenRCT2::Ui::Windows { uint32_t guestFlags = peep->PeepFlags ^ PEEP_FLAGS_TRACKING; - auto guestSetFlagsAction = GuestSetFlagsAction(EntityId::FromUnderlying(number), guestFlags); + auto guestSetFlagsAction = GameActions::GuestSetFlagsAction(EntityId::FromUnderlying(number), guestFlags); GameActions::Execute(&guestSetFlagsAction); } break; @@ -961,7 +961,7 @@ namespace OpenRCT2::Ui::Windows if (text.empty()) return; std::string sText(text); - auto gameAction = GuestSetNameAction(EntityId::FromUnderlying(number), sText); + auto gameAction = GameActions::GuestSetNameAction(EntityId::FromUnderlying(number), sText); GameActions::Execute(&gameAction); } diff --git a/src/openrct2-ui/windows/Land.cpp b/src/openrct2-ui/windows/Land.cpp index 0f963256e1..8be794310f 100644 --- a/src/openrct2-ui/windows/Land.cpp +++ b/src/openrct2-ui/windows/Land.cpp @@ -339,7 +339,7 @@ namespace OpenRCT2::Ui::Windows if (_landToolMountainMode) { - auto landSmoothAction = LandSmoothAction( + auto landSmoothAction = GameActions::LandSmoothAction( { centreX, centreY }, { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, gMapSelectType, false); @@ -348,7 +348,7 @@ namespace OpenRCT2::Ui::Windows return res.Error == GameActions::Status::Ok ? res.Cost : kMoney64Undefined; } - auto landRaiseAction = LandRaiseAction( + auto landRaiseAction = GameActions::LandRaiseAction( { centreX, centreY }, { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, gMapSelectType); auto res = (flag & GAME_COMMAND_FLAG_APPLY) ? GameActions::Execute(&landRaiseAction) @@ -370,7 +370,7 @@ namespace OpenRCT2::Ui::Windows if (_landToolMountainMode) { - auto landSmoothAction = LandSmoothAction( + auto landSmoothAction = GameActions::LandSmoothAction( { centreX, centreY }, { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, gMapSelectType, true); @@ -379,7 +379,7 @@ namespace OpenRCT2::Ui::Windows return res.Error == GameActions::Status::Ok ? res.Cost : kMoney64Undefined; } - auto landLowerAction = LandLowerAction( + auto landLowerAction = GameActions::LandLowerAction( { centreX, centreY }, { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, gMapSelectType); auto res = (flag & GAME_COMMAND_FLAG_APPLY) ? GameActions::Execute(&landLowerAction) diff --git a/src/openrct2-ui/windows/LandRights.cpp b/src/openrct2-ui/windows/LandRights.cpp index 7359cf747d..a82fcc6e9b 100644 --- a/src/openrct2-ui/windows/LandRights.cpp +++ b/src/openrct2-ui/windows/LandRights.cpp @@ -421,12 +421,12 @@ namespace OpenRCT2::Ui::Windows } } - LandBuyRightsAction GetLandBuyAction() + GameActions::LandBuyRightsAction GetLandBuyAction() { - auto mode = (_landRightsMode == LandRightsMode::BuyLand) ? LandBuyRightSetting::BuyLand - : LandBuyRightSetting::BuyConstructionRights; + auto mode = (_landRightsMode == LandRightsMode::BuyLand) ? GameActions::LandBuyRightSetting::BuyLand + : GameActions::LandBuyRightSetting::BuyConstructionRights; - return LandBuyRightsAction( + return GameActions::LandBuyRightsAction( { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, mode); } @@ -448,11 +448,11 @@ namespace OpenRCT2::Ui::Windows } } - LandSetRightsAction GetLandSetAction() + GameActions::LandSetRightsAction GetLandSetAction() { - return LandSetRightsAction( + return GameActions::LandSetRightsAction( { gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y }, - LandSetRightSetting::SetOwnershipWithChecks, GetDesiredOwnership()); + GameActions::LandSetRightSetting::SetOwnershipWithChecks, GetDesiredOwnership()); } void OnToolUpdate(WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords) override diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index f8ab85d4b8..b2de30d6d4 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -1299,7 +1299,7 @@ namespace OpenRCT2::Ui::Windows if (newSpeed >= 5) newSpeed = 8; - auto setSpeedAction = GameSetSpeedAction(newSpeed); + auto setSpeedAction = GameActions::GameSetSpeedAction(newSpeed); GameActions::Execute(&setSpeedAction); } } diff --git a/src/openrct2/Editor.cpp b/src/openrct2/Editor.cpp index 108550d788..2ef606e736 100644 --- a/src/openrct2/Editor.cpp +++ b/src/openrct2/Editor.cpp @@ -238,11 +238,12 @@ namespace OpenRCT2::Editor auto& gameState = getGameState(); MapRange range = { 2 * kCoordsXYStep, 2 * kCoordsXYStep, (gameState.mapSize.x - 3) * kCoordsXYStep, (gameState.mapSize.y - 3) * kCoordsXYStep }; - auto landSetRightsAction = LandSetRightsAction(range, LandSetRightSetting::SetForSale); + + auto landSetRightsAction = GameActions::LandSetRightsAction(range, GameActions::LandSetRightSetting::SetForSale); landSetRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND); GameActions::Execute(&landSetRightsAction); - auto landBuyRightsAction = LandBuyRightsAction(range, LandBuyRightSetting::BuyLand); + auto landBuyRightsAction = GameActions::LandBuyRightsAction(range, GameActions::LandBuyRightSetting::BuyLand); landBuyRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND); GameActions::Execute(&landBuyRightsAction); } diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index b1f7156051..d029fd5a4f 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -102,7 +102,7 @@ using namespace OpenRCT2; void GameResetSpeed() { - auto setSpeedAction = GameSetSpeedAction(1); + auto setSpeedAction = GameActions::GameSetSpeedAction(1); GameActions::Execute(&setSpeedAction); } @@ -112,7 +112,7 @@ void GameIncreaseGameSpeed() if (newSpeed == 5) newSpeed = 8; - auto setSpeedAction = GameSetSpeedAction(newSpeed); + auto setSpeedAction = GameActions::GameSetSpeedAction(newSpeed); GameActions::Execute(&setSpeedAction); } @@ -122,7 +122,7 @@ void GameReduceGameSpeed() if (newSpeed == 7) newSpeed = 4; - auto setSpeedAction = GameSetSpeedAction(newSpeed); + auto setSpeedAction = GameActions::GameSetSpeedAction(newSpeed); GameActions::Execute(&setSpeedAction); } diff --git a/src/openrct2/actions/GameSetSpeedAction.cpp b/src/openrct2/actions/GameSetSpeedAction.cpp index aaa7cf81bd..af791f6bc6 100644 --- a/src/openrct2/actions/GameSetSpeedAction.cpp +++ b/src/openrct2/actions/GameSetSpeedAction.cpp @@ -13,64 +13,63 @@ #include "../config/Config.h" #include "../ui/WindowManager.h" -using namespace OpenRCT2; - -GameSetSpeedAction::GameSetSpeedAction(uint8_t speed) - : _speed(speed) +namespace OpenRCT2::GameActions { -} - -void GameSetSpeedAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit("speed", _speed); -} - -uint16_t GameSetSpeedAction::GetActionFlags() const -{ - return GameAction::GetActionFlags() | GameActions::Flags::AllowWhilePaused | GameActions::Flags::IgnoreForReplays; -} - -void GameSetSpeedAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_speed); -} - -GameActions::Result GameSetSpeedAction::Query() const -{ - GameActions::Result res = GameActions::Result(); - - if (!IsValidSpeed(_speed)) + GameSetSpeedAction::GameSetSpeedAction(uint8_t speed) + : _speed(speed) { - LOG_ERROR("Invalid speed %u", _speed); - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); } - return res; -} - -GameActions::Result GameSetSpeedAction::Execute() const -{ - GameActions::Result res = GameActions::Result(); - - if (!IsValidSpeed(_speed)) + void GameSetSpeedAction::AcceptParameters(GameActionParameterVisitor& visitor) { - LOG_ERROR("Invalid speed %u", _speed); - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); + visitor.Visit("speed", _speed); } - gGameSpeed = _speed; + uint16_t GameSetSpeedAction::GetActionFlags() const + { + return GameAction::GetActionFlags() | Flags::AllowWhilePaused | Flags::IgnoreForReplays; + } - auto* windowMgr = Ui::GetWindowManager(); - windowMgr->InvalidateByClass(WindowClass::TopToolbar); + void GameSetSpeedAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); - return res; -} + stream << DS_TAG(_speed); + } -bool GameSetSpeedAction::IsValidSpeed(uint8_t speed) const -{ - return speed >= 1 && (speed <= 4 || (Config::Get().general.DebuggingTools && speed <= 8)); -} + Result GameSetSpeedAction::Query() const + { + Result res = Result(); + + if (!IsValidSpeed(_speed)) + { + LOG_ERROR("Invalid speed %u", _speed); + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); + } + + return res; + } + + Result GameSetSpeedAction::Execute() const + { + Result res = Result(); + + if (!IsValidSpeed(_speed)) + { + LOG_ERROR("Invalid speed %u", _speed); + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); + } + + gGameSpeed = _speed; + + auto* windowMgr = Ui::GetWindowManager(); + windowMgr->InvalidateByClass(WindowClass::TopToolbar); + + return res; + } + + bool GameSetSpeedAction::IsValidSpeed(uint8_t speed) const + { + return speed >= 1 && (speed <= 4 || (Config::Get().general.DebuggingTools && speed <= 8)); + } +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/GameSetSpeedAction.h b/src/openrct2/actions/GameSetSpeedAction.h index 25e5d1fd98..89c84b0db2 100644 --- a/src/openrct2/actions/GameSetSpeedAction.h +++ b/src/openrct2/actions/GameSetSpeedAction.h @@ -11,23 +11,26 @@ #include "GameAction.h" -class GameSetSpeedAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - uint8_t _speed{ 1 }; + class GameSetSpeedAction final : public GameActionBase + { + private: + uint8_t _speed{ 1 }; -public: - GameSetSpeedAction() = default; - GameSetSpeedAction(uint8_t speed); + public: + GameSetSpeedAction() = default; + GameSetSpeedAction(uint8_t speed); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; -private: - bool IsValidSpeed(uint8_t speed) const; -}; + private: + bool IsValidSpeed(uint8_t speed) const; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/GuestSetFlagsAction.cpp b/src/openrct2/actions/GuestSetFlagsAction.cpp index 6333df89f4..1a9069bafc 100644 --- a/src/openrct2/actions/GuestSetFlagsAction.cpp +++ b/src/openrct2/actions/GuestSetFlagsAction.cpp @@ -14,53 +14,54 @@ #include "../OpenRCT2.h" #include "../entity/EntityRegistry.h" -using namespace OpenRCT2; - -GuestSetFlagsAction::GuestSetFlagsAction(EntityId peepId, uint32_t flags) - : _peepId(peepId) - , _newFlags(flags) +namespace OpenRCT2::GameActions { -} - -void GuestSetFlagsAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit("peep", _peepId); - visitor.Visit("guestFlags", _newFlags); -} - -uint16_t GuestSetFlagsAction::GetActionFlags() const -{ - return GameAction::GetActionFlags() | GameActions::Flags::AllowWhilePaused; -} - -void GuestSetFlagsAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_peepId) << DS_TAG(_newFlags); -} - -GameActions::Result GuestSetFlagsAction::Query() const -{ - auto* peep = TryGetEntity(_peepId); - if (peep == nullptr) + GuestSetFlagsAction::GuestSetFlagsAction(EntityId peepId, uint32_t flags) + : _peepId(peepId) + , _newFlags(flags) { - LOG_ERROR("Guest entity not found for peepID %u", _peepId.ToUnderlying()); - return GameActions::Result(GameActions::Status::InvalidParameters, STR_CANT_CHANGE_THIS, kStringIdNone); - } - return GameActions::Result(); -} - -GameActions::Result GuestSetFlagsAction::Execute() const -{ - auto* peep = TryGetEntity(_peepId); - if (peep == nullptr) - { - LOG_ERROR("Guest entity not found for peepID %u", _peepId.ToUnderlying()); - return GameActions::Result(GameActions::Status::InvalidParameters, STR_CANT_CHANGE_THIS, kStringIdNone); } - peep->PeepFlags = _newFlags; + void GuestSetFlagsAction::AcceptParameters(GameActionParameterVisitor& visitor) + { + visitor.Visit("peep", _peepId); + visitor.Visit("guestFlags", _newFlags); + } - return GameActions::Result(); -} + uint16_t GuestSetFlagsAction::GetActionFlags() const + { + return GameAction::GetActionFlags() | Flags::AllowWhilePaused; + } + + void GuestSetFlagsAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_peepId) << DS_TAG(_newFlags); + } + + Result GuestSetFlagsAction::Query() const + { + auto* peep = TryGetEntity(_peepId); + if (peep == nullptr) + { + LOG_ERROR("Guest entity not found for peepID %u", _peepId.ToUnderlying()); + return Result(Status::InvalidParameters, STR_CANT_CHANGE_THIS, kStringIdNone); + } + return Result(); + } + + Result GuestSetFlagsAction::Execute() const + { + auto* peep = TryGetEntity(_peepId); + if (peep == nullptr) + { + LOG_ERROR("Guest entity not found for peepID %u", _peepId.ToUnderlying()); + return Result(Status::InvalidParameters, STR_CANT_CHANGE_THIS, kStringIdNone); + } + + peep->PeepFlags = _newFlags; + + return Result(); + } +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/GuestSetFlagsAction.h b/src/openrct2/actions/GuestSetFlagsAction.h index c048f89a02..ac9ddac1e0 100644 --- a/src/openrct2/actions/GuestSetFlagsAction.h +++ b/src/openrct2/actions/GuestSetFlagsAction.h @@ -11,21 +11,24 @@ #include "GameAction.h" -class GuestSetFlagsAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - EntityId _peepId{ EntityId::GetNull() }; - uint32_t _newFlags{}; + class GuestSetFlagsAction final : public GameActionBase + { + private: + EntityId _peepId{ EntityId::GetNull() }; + uint32_t _newFlags{}; -public: - GuestSetFlagsAction() = default; - GuestSetFlagsAction(EntityId peepId, uint32_t flags); + public: + GuestSetFlagsAction() = default; + GuestSetFlagsAction(EntityId peepId, uint32_t flags); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; -}; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/GuestSetNameAction.cpp b/src/openrct2/actions/GuestSetNameAction.cpp index 2d08cdffed..eeace67360 100644 --- a/src/openrct2/actions/GuestSetNameAction.cpp +++ b/src/openrct2/actions/GuestSetNameAction.cpp @@ -19,89 +19,90 @@ #include "../windows/Intent.h" #include "../world/Park.h" -using namespace OpenRCT2; - -GuestSetNameAction::GuestSetNameAction(EntityId spriteIndex, const std::string& name) - : _spriteIndex(spriteIndex) - , _name(name) +namespace OpenRCT2::GameActions { -} - -EntityId GuestSetNameAction::GetSpriteIndex() const -{ - return _spriteIndex; -} - -std::string GuestSetNameAction::GetGuestName() const -{ - return _name; -} - -void GuestSetNameAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit("peep", _spriteIndex); - visitor.Visit("name", _name); -} - -uint16_t GuestSetNameAction::GetActionFlags() const -{ - return GameAction::GetActionFlags() | GameActions::Flags::AllowWhilePaused; -} - -void GuestSetNameAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_spriteIndex) << DS_TAG(_name); -} - -GameActions::Result GuestSetNameAction::Query() const -{ - if (_spriteIndex.ToUnderlying() >= kMaxEntities || _spriteIndex.IsNull()) + GuestSetNameAction::GuestSetNameAction(EntityId spriteIndex, const std::string& name) + : _spriteIndex(spriteIndex) + , _name(name) { - return GameActions::Result(GameActions::Status::InvalidParameters, STR_CANT_NAME_GUEST, STR_ERR_VALUE_OUT_OF_RANGE); } - auto guest = TryGetEntity(_spriteIndex); - if (guest == nullptr) + EntityId GuestSetNameAction::GetSpriteIndex() const { - LOG_ERROR("Guest entity not found for spriteIndex %u", _spriteIndex); - return GameActions::Result(GameActions::Status::InvalidParameters, STR_CANT_NAME_GUEST, kStringIdNone); + return _spriteIndex; } - return GameActions::Result(); -} - -GameActions::Result GuestSetNameAction::Execute() const -{ - auto guest = TryGetEntity(_spriteIndex); - if (guest == nullptr) + std::string GuestSetNameAction::GetGuestName() const { - LOG_ERROR("Guest entity not found for spriteIndex %u", _spriteIndex); - return GameActions::Result(GameActions::Status::InvalidParameters, STR_CANT_NAME_GUEST, kStringIdNone); + return _name; } - auto curName = guest->GetName(); - if (curName == _name) + void GuestSetNameAction::AcceptParameters(GameActionParameterVisitor& visitor) { - return GameActions::Result(); + visitor.Visit("peep", _spriteIndex); + visitor.Visit("name", _name); } - if (!guest->SetName(_name)) + uint16_t GuestSetNameAction::GetActionFlags() const { - return GameActions::Result(GameActions::Status::Unknown, STR_CANT_NAME_GUEST, kStringIdNone); + return GameAction::GetActionFlags() | Flags::AllowWhilePaused; } - // Easter egg functions are for guests only - guest->HandleEasterEggName(); + void GuestSetNameAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); - GfxInvalidateScreen(); + stream << DS_TAG(_spriteIndex) << DS_TAG(_name); + } - auto intent = Intent(INTENT_ACTION_REFRESH_GUEST_LIST); - ContextBroadcastIntent(&intent); + Result GuestSetNameAction::Query() const + { + if (_spriteIndex.ToUnderlying() >= kMaxEntities || _spriteIndex.IsNull()) + { + return Result(Status::InvalidParameters, STR_CANT_NAME_GUEST, STR_ERR_VALUE_OUT_OF_RANGE); + } - auto res = GameActions::Result(); - res.Position = guest->GetLocation(); + auto guest = TryGetEntity(_spriteIndex); + if (guest == nullptr) + { + LOG_ERROR("Guest entity not found for spriteIndex %u", _spriteIndex); + return Result(Status::InvalidParameters, STR_CANT_NAME_GUEST, kStringIdNone); + } - return res; -} + return Result(); + } + + Result GuestSetNameAction::Execute() const + { + auto guest = TryGetEntity(_spriteIndex); + if (guest == nullptr) + { + LOG_ERROR("Guest entity not found for spriteIndex %u", _spriteIndex); + return Result(Status::InvalidParameters, STR_CANT_NAME_GUEST, kStringIdNone); + } + + auto curName = guest->GetName(); + if (curName == _name) + { + return Result(); + } + + if (!guest->SetName(_name)) + { + return Result(Status::Unknown, STR_CANT_NAME_GUEST, kStringIdNone); + } + + // Easter egg functions are for guests only + guest->HandleEasterEggName(); + + GfxInvalidateScreen(); + + auto intent = Intent(INTENT_ACTION_REFRESH_GUEST_LIST); + ContextBroadcastIntent(&intent); + + auto res = Result(); + res.Position = guest->GetLocation(); + + return res; + } +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/GuestSetNameAction.h b/src/openrct2/actions/GuestSetNameAction.h index a1266f0c7b..292f792a3f 100644 --- a/src/openrct2/actions/GuestSetNameAction.h +++ b/src/openrct2/actions/GuestSetNameAction.h @@ -11,24 +11,27 @@ #include "GameAction.h" -class GuestSetNameAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - EntityId _spriteIndex{ EntityId::GetNull() }; - std::string _name; + class GuestSetNameAction final : public GameActionBase + { + private: + EntityId _spriteIndex{ EntityId::GetNull() }; + std::string _name; -public: - GuestSetNameAction() = default; - GuestSetNameAction(EntityId spriteIndex, const std::string& name); + public: + GuestSetNameAction() = default; + GuestSetNameAction(EntityId spriteIndex, const std::string& name); - EntityId GetSpriteIndex() const; - std::string GetGuestName() const; + EntityId GetSpriteIndex() const; + std::string GetGuestName() const; - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; -}; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandBuyRightsAction.cpp b/src/openrct2/actions/LandBuyRightsAction.cpp index eb503e6201..bdaf4bcfc1 100644 --- a/src/openrct2/actions/LandBuyRightsAction.cpp +++ b/src/openrct2/actions/LandBuyRightsAction.cpp @@ -24,152 +24,150 @@ #include "../world/Scenery.h" #include "../world/tile_element/SurfaceElement.h" -using namespace OpenRCT2; - -LandBuyRightsAction::LandBuyRightsAction(const MapRange& range, LandBuyRightSetting setting) - : _range(range) - , _setting(setting) +namespace OpenRCT2::GameActions { -} - -LandBuyRightsAction::LandBuyRightsAction(const CoordsXY& coord, LandBuyRightSetting setting) - : _range(coord.x, coord.y, coord.x, coord.y) - , _setting(setting) -{ -} - -void LandBuyRightsAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_range); - visitor.Visit("setting", _setting); -} - -uint16_t LandBuyRightsAction::GetActionFlags() const -{ - return GameAction::GetActionFlags(); -} - -void LandBuyRightsAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_range) << DS_TAG(_setting); -} - -GameActions::Result LandBuyRightsAction::Query() const -{ - return QueryExecute(false); -} - -GameActions::Result LandBuyRightsAction::Execute() const -{ - return QueryExecute(true); -} - -GameActions::Result LandBuyRightsAction::QueryExecute(bool isExecuting) const -{ - auto res = GameActions::Result(); - - MapRange normRange = _range.Normalise(); - // Keep big coordinates within map boundaries - auto mapSizeMaxXY = GetMapSizeMaxXY(); - auto aX = std::max(32, normRange.GetLeft()); - auto bX = std::min(mapSizeMaxXY.x, normRange.GetRight()); - auto aY = std::max(32, normRange.GetTop()); - auto bY = std::min(mapSizeMaxXY.y, normRange.GetBottom()); - - MapRange validRange = MapRange{ aX, aY, bX, bY }; - - CoordsXYZ centre{ (validRange.GetLeft() + validRange.GetRight()) / 2 + 16, - (validRange.GetTop() + validRange.GetBottom()) / 2 + 16, 0 }; - centre.z = TileElementHeight(centre); - - res.Position = centre; - res.Expenditure = ExpenditureType::landPurchase; - - // Game command modified to accept selection size - for (auto y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + LandBuyRightsAction::LandBuyRightsAction(const MapRange& range, LandBuyRightSetting setting) + : _range(range) + , _setting(setting) { - for (auto x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) + } + + LandBuyRightsAction::LandBuyRightsAction(const CoordsXY& coord, LandBuyRightSetting setting) + : _range(coord.x, coord.y, coord.x, coord.y) + , _setting(setting) + { + } + + void LandBuyRightsAction::AcceptParameters(GameActionParameterVisitor& visitor) + { + visitor.Visit(_range); + visitor.Visit("setting", _setting); + } + + uint16_t LandBuyRightsAction::GetActionFlags() const + { + return GameAction::GetActionFlags(); + } + + void LandBuyRightsAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_range) << DS_TAG(_setting); + } + + Result LandBuyRightsAction::Query() const + { + return QueryExecute(false); + } + + Result LandBuyRightsAction::Execute() const + { + return QueryExecute(true); + } + + Result LandBuyRightsAction::QueryExecute(bool isExecuting) const + { + auto res = Result(); + + MapRange normRange = _range.Normalise(); + // Keep big coordinates within map boundaries + auto mapSizeMaxXY = GetMapSizeMaxXY(); + auto aX = std::max(32, normRange.GetLeft()); + auto bX = std::min(mapSizeMaxXY.x, normRange.GetRight()); + auto aY = std::max(32, normRange.GetTop()); + auto bY = std::min(mapSizeMaxXY.y, normRange.GetBottom()); + + MapRange validRange = MapRange{ aX, aY, bX, bY }; + + CoordsXYZ centre{ (validRange.GetLeft() + validRange.GetRight()) / 2 + 16, + (validRange.GetTop() + validRange.GetBottom()) / 2 + 16, 0 }; + centre.z = TileElementHeight(centre); + + res.Position = centre; + res.Expenditure = ExpenditureType::landPurchase; + + // Game command modified to accept selection size + for (auto y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) { - if (!LocationValid({ x, y })) - continue; - auto result = MapBuyLandRightsForTile({ x, y }, isExecuting); - if (result.Error == GameActions::Status::Ok) + for (auto x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) { - res.Cost += result.Cost; + if (!LocationValid({ x, y })) + continue; + auto result = MapBuyLandRightsForTile({ x, y }, isExecuting); + if (result.Error == Status::Ok) + { + res.Cost += result.Cost; + } } } - } - if (isExecuting) - { - MapCountRemainingLandRights(); - } - return res; -} - -GameActions::Result LandBuyRightsAction::MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const -{ - if (_setting >= LandBuyRightSetting::Count) - { - LOG_ERROR("Invalid land buying setting %u", _setting); - return GameActions::Result(GameActions::Status::InvalidParameters, kErrorTitles[0], STR_ERR_VALUE_OUT_OF_RANGE); + if (isExecuting) + { + MapCountRemainingLandRights(); + } + return res; } - SurfaceElement* surfaceElement = MapGetSurfaceElementAt(loc); - if (surfaceElement == nullptr) + Result LandBuyRightsAction::MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const { - LOG_ERROR("No surface at x = %d, y = %d", loc.x, loc.y); - return GameActions::Result( - GameActions::Status::InvalidParameters, kErrorTitles[EnumValue(_setting)], STR_ERR_SURFACE_ELEMENT_NOT_FOUND); - } - - auto res = GameActions::Result(); - switch (_setting) - { - case LandBuyRightSetting::BuyLand: // 0 - if ((surfaceElement->GetOwnership() & OWNERSHIP_OWNED) != 0) - { // If the land is already owned - return res; - } - - if (gLegacyScene == LegacyScene::scenarioEditor || (surfaceElement->GetOwnership() & OWNERSHIP_AVAILABLE) == 0) - { - return GameActions::Result( - GameActions::Status::NotOwned, kErrorTitles[EnumValue(_setting)], STR_LAND_NOT_FOR_SALE); - } - if (isExecuting) - { - surfaceElement->SetOwnership(OWNERSHIP_OWNED); - Park::UpdateFencesAroundTile(loc); - } - res.Cost = getGameState().scenarioOptions.landPrice; - return res; - - case LandBuyRightSetting::BuyConstructionRights: // 2 - if ((surfaceElement->GetOwnership() & (OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) != 0) - { // If the land or construction rights are already owned - return res; - } - - if (gLegacyScene == LegacyScene::scenarioEditor - || (surfaceElement->GetOwnership() & OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE) == 0) - { - return GameActions::Result( - GameActions::Status::NotOwned, kErrorTitles[EnumValue(_setting)], STR_CONSTRUCTION_RIGHTS_NOT_FOR_SALE); - } - - if (isExecuting) - { - surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED); - uint16_t baseZ = surfaceElement->GetBaseZ(); - MapInvalidateTile({ loc, baseZ, baseZ + 16 }); - } - res.Cost = getGameState().scenarioOptions.constructionRightsPrice; - return res; - - default: + if (_setting >= LandBuyRightSetting::Count) + { LOG_ERROR("Invalid land buying setting %u", _setting); - return GameActions::Result(GameActions::Status::InvalidParameters, kErrorTitles[0], STR_ERR_VALUE_OUT_OF_RANGE); + return Result(Status::InvalidParameters, kErrorTitles[0], STR_ERR_VALUE_OUT_OF_RANGE); + } + + SurfaceElement* surfaceElement = MapGetSurfaceElementAt(loc); + if (surfaceElement == nullptr) + { + LOG_ERROR("No surface at x = %d, y = %d", loc.x, loc.y); + return Result(Status::InvalidParameters, kErrorTitles[EnumValue(_setting)], STR_ERR_SURFACE_ELEMENT_NOT_FOUND); + } + + auto res = Result(); + switch (_setting) + { + case LandBuyRightSetting::BuyLand: // 0 + if ((surfaceElement->GetOwnership() & OWNERSHIP_OWNED) != 0) + { // If the land is already owned + return res; + } + + if (gLegacyScene == LegacyScene::scenarioEditor || (surfaceElement->GetOwnership() & OWNERSHIP_AVAILABLE) == 0) + { + return Result(Status::NotOwned, kErrorTitles[EnumValue(_setting)], STR_LAND_NOT_FOR_SALE); + } + if (isExecuting) + { + surfaceElement->SetOwnership(OWNERSHIP_OWNED); + Park::UpdateFencesAroundTile(loc); + } + res.Cost = getGameState().scenarioOptions.landPrice; + return res; + + case LandBuyRightSetting::BuyConstructionRights: // 2 + if ((surfaceElement->GetOwnership() & (OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) != 0) + { // If the land or construction rights are already owned + return res; + } + + if (gLegacyScene == LegacyScene::scenarioEditor + || (surfaceElement->GetOwnership() & OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE) == 0) + { + return Result(Status::NotOwned, kErrorTitles[EnumValue(_setting)], STR_CONSTRUCTION_RIGHTS_NOT_FOR_SALE); + } + + if (isExecuting) + { + surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED); + uint16_t baseZ = surfaceElement->GetBaseZ(); + MapInvalidateTile({ loc, baseZ, baseZ + 16 }); + } + res.Cost = getGameState().scenarioOptions.constructionRightsPrice; + return res; + + default: + LOG_ERROR("Invalid land buying setting %u", _setting); + return Result(Status::InvalidParameters, kErrorTitles[0], STR_ERR_VALUE_OUT_OF_RANGE); + } } -} +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandBuyRightsAction.h b/src/openrct2/actions/LandBuyRightsAction.h index 7f2cf7a918..577f5617e2 100644 --- a/src/openrct2/actions/LandBuyRightsAction.h +++ b/src/openrct2/actions/LandBuyRightsAction.h @@ -11,38 +11,41 @@ #include "GameAction.h" -enum class LandBuyRightSetting : uint8_t +namespace OpenRCT2::GameActions { - BuyLand, - BuyConstructionRights, - Count -}; - -class LandBuyRightsAction final : public GameActionBase -{ -private: - MapRange _range; - LandBuyRightSetting _setting{ LandBuyRightSetting::Count }; - - constexpr static StringId kErrorTitles[] = { - STR_CANT_BUY_LAND, - STR_CANT_BUY_CONSTRUCTION_RIGHTS_HERE, + enum class LandBuyRightSetting : uint8_t + { + BuyLand, + BuyConstructionRights, + Count }; -public: - LandBuyRightsAction() = default; - LandBuyRightsAction(const MapRange& range, LandBuyRightSetting setting); - LandBuyRightsAction(const CoordsXY& coord, LandBuyRightSetting setting); + class LandBuyRightsAction final : public GameActionBase + { + private: + MapRange _range; + LandBuyRightSetting _setting{ LandBuyRightSetting::Count }; - void AcceptParameters(GameActionParameterVisitor& visitor) override; + constexpr static StringId kErrorTitles[] = { + STR_CANT_BUY_LAND, + STR_CANT_BUY_CONSTRUCTION_RIGHTS_HERE, + }; - uint16_t GetActionFlags() const override; + public: + LandBuyRightsAction() = default; + LandBuyRightsAction(const MapRange& range, LandBuyRightSetting setting); + LandBuyRightsAction(const CoordsXY& coord, LandBuyRightSetting setting); - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; -private: - OpenRCT2::GameActions::Result QueryExecute(bool isExecuting) const; - OpenRCT2::GameActions::Result MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const; -}; + uint16_t GetActionFlags() const override; + + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; + + private: + Result QueryExecute(bool isExecuting) const; + Result MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandLowerAction.cpp b/src/openrct2/actions/LandLowerAction.cpp index dfbc21ef21..2e29f81172 100644 --- a/src/openrct2/actions/LandLowerAction.cpp +++ b/src/openrct2/actions/LandLowerAction.cpp @@ -25,125 +25,125 @@ #include "../world/tile_element/Slope.h" #include "../world/tile_element/SurfaceElement.h" -using namespace OpenRCT2; - -LandLowerAction::LandLowerAction(const CoordsXY& coords, MapRange range, uint8_t selectionType) - : _coords(coords) - , _range(range) - , _selectionType(selectionType) +namespace OpenRCT2::GameActions { -} - -void LandLowerAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_coords); - visitor.Visit(_range); - visitor.Visit("selectionType", _selectionType); -} - -uint16_t LandLowerAction::GetActionFlags() const -{ - return GameAction::GetActionFlags(); -} - -void LandLowerAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType); -} - -GameActions::Result LandLowerAction::Query() const -{ - return QueryExecute(false); -} - -GameActions::Result LandLowerAction::Execute() const -{ - return QueryExecute(true); -} - -GameActions::Result LandLowerAction::QueryExecute(bool isExecuting) const -{ - auto res = GameActions::Result(); - size_t tableRow = _selectionType; - - // The selections between MAP_SELECT_TYPE_FULL and MAP_SELECT_TYPE_EDGE_0 are not included in the tables - if (_selectionType >= MAP_SELECT_TYPE_EDGE_0 && _selectionType <= MAP_SELECT_TYPE_EDGE_3) - tableRow -= MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1; - - auto validRange = ClampRangeWithinMap(_range); - - res.Position = { _coords.x, _coords.y, TileElementHeight(_coords) }; - res.Expenditure = ExpenditureType::landscaping; - - if (isExecuting) + LandLowerAction::LandLowerAction(const CoordsXY& coords, MapRange range, uint8_t selectionType) + : _coords(coords) + , _range(range) + , _selectionType(selectionType) { - OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, { _coords.x, _coords.y, TileElementHeight(_coords) }); } - uint8_t maxHeight = MapGetHighestLandHeight(validRange); - bool withinOwnership = false; - - for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + void LandLowerAction::AcceptParameters(GameActionParameterVisitor& visitor) { - for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) - { - if (!LocationValid({ x, y })) - continue; - auto* surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, y }); - if (surfaceElement == nullptr) - continue; + visitor.Visit(_coords); + visitor.Visit(_range); + visitor.Visit("selectionType", _selectionType); + } - if (gLegacyScene != LegacyScene::scenarioEditor && !getGameState().cheats.sandboxMode) + uint16_t LandLowerAction::GetActionFlags() const + { + return GameAction::GetActionFlags(); + } + + void LandLowerAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType); + } + + Result LandLowerAction::Query() const + { + return QueryExecute(false); + } + + Result LandLowerAction::Execute() const + { + return QueryExecute(true); + } + + Result LandLowerAction::QueryExecute(bool isExecuting) const + { + auto res = Result(); + size_t tableRow = _selectionType; + + // The selections between MAP_SELECT_TYPE_FULL and MAP_SELECT_TYPE_EDGE_0 are not included in the tables + if (_selectionType >= MAP_SELECT_TYPE_EDGE_0 && _selectionType <= MAP_SELECT_TYPE_EDGE_3) + tableRow -= MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1; + + auto validRange = ClampRangeWithinMap(_range); + + res.Position = { _coords.x, _coords.y, TileElementHeight(_coords) }; + res.Expenditure = ExpenditureType::landscaping; + + if (isExecuting) + { + OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, { _coords.x, _coords.y, TileElementHeight(_coords) }); + } + + uint8_t maxHeight = MapGetHighestLandHeight(validRange); + bool withinOwnership = false; + + for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + { + for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) { - if (!MapIsLocationInPark(CoordsXY{ x, y })) - { + if (!LocationValid({ x, y })) continue; + auto* surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, y }); + if (surfaceElement == nullptr) + continue; + + if (gLegacyScene != LegacyScene::scenarioEditor && !getGameState().cheats.sandboxMode) + { + if (!MapIsLocationInPark(CoordsXY{ x, y })) + { + continue; + } + } + withinOwnership = true; + + uint8_t height = surfaceElement->BaseHeight; + if (surfaceElement->GetSlope() & kTileSlopeRaisedCornersMask) + height += 2; + if (surfaceElement->GetSlope() & kTileSlopeDiagonalFlag) + height += 2; + + if (height < maxHeight) + continue; + + height = surfaceElement->BaseHeight; + uint8_t currentSlope = surfaceElement->GetSlope(); + uint8_t newSlope = LowerSurfaceCornerFlags(tableRow, currentSlope); + if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) + height -= 2; + + newSlope &= kTileSlopeMask; + + auto landSetHeightAction = LandSetHeightAction({ x, y }, height, newSlope); + landSetHeightAction.SetFlags(GetFlags()); + auto result = isExecuting ? ExecuteNested(&landSetHeightAction) : QueryNested(&landSetHeightAction); + if (result.Error == Status::Ok) + { + res.Cost += result.Cost; + } + else + { + result.ErrorTitle = STR_CANT_LOWER_LAND_HERE; + return result; } } - withinOwnership = true; - - uint8_t height = surfaceElement->BaseHeight; - if (surfaceElement->GetSlope() & kTileSlopeRaisedCornersMask) - height += 2; - if (surfaceElement->GetSlope() & kTileSlopeDiagonalFlag) - height += 2; - - if (height < maxHeight) - continue; - - height = surfaceElement->BaseHeight; - uint8_t currentSlope = surfaceElement->GetSlope(); - uint8_t newSlope = LowerSurfaceCornerFlags(tableRow, currentSlope); - if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) - height -= 2; - - newSlope &= kTileSlopeMask; - - auto landSetHeightAction = LandSetHeightAction({ x, y }, height, newSlope); - landSetHeightAction.SetFlags(GetFlags()); - auto result = isExecuting ? GameActions::ExecuteNested(&landSetHeightAction) - : GameActions::QueryNested(&landSetHeightAction); - if (result.Error == GameActions::Status::Ok) - { - res.Cost += result.Cost; - } - else - { - result.ErrorTitle = STR_CANT_LOWER_LAND_HERE; - return result; - } } + + if (!withinOwnership) + { + return Result(Status::Disallowed, STR_CANT_LOWER_LAND_HERE, STR_LAND_NOT_OWNED_BY_PARK); + } + + // Force ride construction to recheck area + _currentTrackSelectionFlags.set(TrackSelectionFlag::recheck); + + return res; } - - if (!withinOwnership) - { - return GameActions::Result(GameActions::Status::Disallowed, STR_CANT_LOWER_LAND_HERE, STR_LAND_NOT_OWNED_BY_PARK); - } - - // Force ride construction to recheck area - _currentTrackSelectionFlags.set(TrackSelectionFlag::recheck); - - return res; -} +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandLowerAction.h b/src/openrct2/actions/LandLowerAction.h index 98a0e8688d..a9eed1fc4a 100644 --- a/src/openrct2/actions/LandLowerAction.h +++ b/src/openrct2/actions/LandLowerAction.h @@ -11,25 +11,28 @@ #include "GameAction.h" -class LandLowerAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - CoordsXY _coords; - MapRange _range; - uint8_t _selectionType{}; + class LandLowerAction final : public GameActionBase + { + private: + CoordsXY _coords; + MapRange _range; + uint8_t _selectionType{}; -public: - LandLowerAction() = default; - LandLowerAction(const CoordsXY& coords, MapRange range, uint8_t selectionType); + public: + LandLowerAction() = default; + LandLowerAction(const CoordsXY& coords, MapRange range, uint8_t selectionType); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; -private: - OpenRCT2::GameActions::Result QueryExecute(bool isExecuting) const; -}; + private: + Result QueryExecute(bool isExecuting) const; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandRaiseAction.cpp b/src/openrct2/actions/LandRaiseAction.cpp index c6fb849907..045aea296c 100644 --- a/src/openrct2/actions/LandRaiseAction.cpp +++ b/src/openrct2/actions/LandRaiseAction.cpp @@ -26,128 +26,128 @@ #include "../world/tile_element/Slope.h" #include "../world/tile_element/SurfaceElement.h" -using namespace OpenRCT2; - -LandRaiseAction::LandRaiseAction(const CoordsXY& coords, MapRange range, uint8_t selectionType) - : _coords(coords) - , _range(range) - , _selectionType(selectionType) +namespace OpenRCT2::GameActions { -} - -void LandRaiseAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_coords); - visitor.Visit(_range); - visitor.Visit("selectionType", _selectionType); -} - -uint16_t LandRaiseAction::GetActionFlags() const -{ - return GameAction::GetActionFlags(); -} - -void LandRaiseAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType); -} - -GameActions::Result LandRaiseAction::Query() const -{ - return QueryExecute(false); -} - -GameActions::Result LandRaiseAction::Execute() const -{ - return QueryExecute(true); -} - -GameActions::Result LandRaiseAction::QueryExecute(bool isExecuting) const -{ - auto res = GameActions::Result(); - size_t tableRow = _selectionType; - - // The selections between MAP_SELECT_TYPE_FULL and MAP_SELECT_TYPE_EDGE_0 are not included in the tables - if (_selectionType >= MAP_SELECT_TYPE_EDGE_0 && _selectionType <= MAP_SELECT_TYPE_EDGE_3) - tableRow -= MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1; - - auto validRange = ClampRangeWithinMap(_range); - - res.Position = { _coords.x, _coords.y, TileElementHeight(_coords) }; - res.Expenditure = ExpenditureType::landscaping; - - if (isExecuting) + LandRaiseAction::LandRaiseAction(const CoordsXY& coords, MapRange range, uint8_t selectionType) + : _coords(coords) + , _range(range) + , _selectionType(selectionType) { - OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, { _coords.x, _coords.y, TileElementHeight(_coords) }); } - uint8_t minHeight = MapGetLowestLandHeight(validRange); - bool withinOwnership = false; - - for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + void LandRaiseAction::AcceptParameters(GameActionParameterVisitor& visitor) { - for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) + visitor.Visit(_coords); + visitor.Visit(_range); + visitor.Visit("selectionType", _selectionType); + } + + uint16_t LandRaiseAction::GetActionFlags() const + { + return GameAction::GetActionFlags(); + } + + void LandRaiseAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType); + } + + Result LandRaiseAction::Query() const + { + return QueryExecute(false); + } + + Result LandRaiseAction::Execute() const + { + return QueryExecute(true); + } + + Result LandRaiseAction::QueryExecute(bool isExecuting) const + { + auto res = Result(); + size_t tableRow = _selectionType; + + // The selections between MAP_SELECT_TYPE_FULL and MAP_SELECT_TYPE_EDGE_0 are not included in the tables + if (_selectionType >= MAP_SELECT_TYPE_EDGE_0 && _selectionType <= MAP_SELECT_TYPE_EDGE_3) + tableRow -= MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1; + + auto validRange = ClampRangeWithinMap(_range); + + res.Position = { _coords.x, _coords.y, TileElementHeight(_coords) }; + res.Expenditure = ExpenditureType::landscaping; + + if (isExecuting) { - if (!LocationValid({ x, y })) - continue; - auto* surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, y }); - if (surfaceElement == nullptr) - continue; + OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, { _coords.x, _coords.y, TileElementHeight(_coords) }); + } - if (gLegacyScene != LegacyScene::scenarioEditor && !getGameState().cheats.sandboxMode) + uint8_t minHeight = MapGetLowestLandHeight(validRange); + bool withinOwnership = false; + + for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + { + for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) { - if (!MapIsLocationInPark(CoordsXY{ x, y })) - { + if (!LocationValid({ x, y })) + continue; + auto* surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, y }); + if (surfaceElement == nullptr) continue; - } - } - withinOwnership = true; - uint8_t height = surfaceElement->BaseHeight; - - if (height > minHeight) - continue; - - uint8_t currentSlope = surfaceElement->GetSlope(); - uint8_t newSlope = RaiseSurfaceCornerFlags(tableRow, currentSlope); - if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) - { - if (height + 2 > UINT8_MAX) + if (gLegacyScene != LegacyScene::scenarioEditor && !getGameState().cheats.sandboxMode) { - height = UINT8_MAX; + if (!MapIsLocationInPark(CoordsXY{ x, y })) + { + continue; + } + } + withinOwnership = true; + + uint8_t height = surfaceElement->BaseHeight; + + if (height > minHeight) + continue; + + uint8_t currentSlope = surfaceElement->GetSlope(); + uint8_t newSlope = RaiseSurfaceCornerFlags(tableRow, currentSlope); + if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) + { + if (height + 2 > UINT8_MAX) + { + height = UINT8_MAX; + } + else + { + height += 2; + } + } + newSlope &= kTileSlopeMask; + + auto landSetHeightAction = LandSetHeightAction({ x, y }, height, newSlope); + landSetHeightAction.SetFlags(GetFlags()); + auto result = isExecuting ? ExecuteNested(&landSetHeightAction) : QueryNested(&landSetHeightAction); + if (result.Error == Status::Ok) + { + res.Cost += result.Cost; } else { - height += 2; + result.ErrorTitle = STR_CANT_RAISE_LAND_HERE; + return result; } } - newSlope &= kTileSlopeMask; - - auto landSetHeightAction = LandSetHeightAction({ x, y }, height, newSlope); - landSetHeightAction.SetFlags(GetFlags()); - auto result = isExecuting ? GameActions::ExecuteNested(&landSetHeightAction) - : GameActions::QueryNested(&landSetHeightAction); - if (result.Error == GameActions::Status::Ok) - { - res.Cost += result.Cost; - } - else - { - result.ErrorTitle = STR_CANT_RAISE_LAND_HERE; - return result; - } } + + if (!withinOwnership) + { + return Result(Status::Disallowed, STR_CANT_RAISE_LAND_HERE, STR_LAND_NOT_OWNED_BY_PARK); + } + + // Force ride construction to recheck area + _currentTrackSelectionFlags.set(TrackSelectionFlag::recheck); + + return res; } - - if (!withinOwnership) - { - return GameActions::Result(GameActions::Status::Disallowed, STR_CANT_RAISE_LAND_HERE, STR_LAND_NOT_OWNED_BY_PARK); - } - - // Force ride construction to recheck area - _currentTrackSelectionFlags.set(TrackSelectionFlag::recheck); - - return res; -} +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandRaiseAction.h b/src/openrct2/actions/LandRaiseAction.h index 8542824039..0ea3f92372 100644 --- a/src/openrct2/actions/LandRaiseAction.h +++ b/src/openrct2/actions/LandRaiseAction.h @@ -11,25 +11,28 @@ #include "GameAction.h" -class LandRaiseAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - CoordsXY _coords; - MapRange _range; - uint8_t _selectionType{}; + class LandRaiseAction final : public GameActionBase + { + private: + CoordsXY _coords; + MapRange _range; + uint8_t _selectionType{}; -public: - LandRaiseAction() = default; - LandRaiseAction(const CoordsXY& coords, MapRange range, uint8_t selectionType); + public: + LandRaiseAction() = default; + LandRaiseAction(const CoordsXY& coords, MapRange range, uint8_t selectionType); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; -private: - OpenRCT2::GameActions::Result QueryExecute(bool isExecuting) const; -}; + private: + Result QueryExecute(bool isExecuting) const; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSetHeightAction.cpp b/src/openrct2/actions/LandSetHeightAction.cpp index fcbe2075d8..bc02e6625d 100644 --- a/src/openrct2/actions/LandSetHeightAction.cpp +++ b/src/openrct2/actions/LandSetHeightAction.cpp @@ -29,284 +29,108 @@ #include "../world/tile_element/SurfaceElement.h" #include "../world/tile_element/TrackElement.h" -using namespace OpenRCT2; - -LandSetHeightAction::LandSetHeightAction(const CoordsXY& coords, uint8_t height, uint8_t style) - : _coords(coords) - , _height(height) - , _style(style) +namespace OpenRCT2::GameActions { -} - -void LandSetHeightAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_coords); - visitor.Visit("height", _height); - visitor.Visit("style", _style); -} - -uint16_t LandSetHeightAction::GetActionFlags() const -{ - return GameAction::GetActionFlags() | GameActions::Flags::EditorOnly; -} - -void LandSetHeightAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_coords) << DS_TAG(_height) << DS_TAG(_style); -} - -GameActions::Result LandSetHeightAction::Query() const -{ - auto& gameState = getGameState(); - if (gameState.park.flags & PARK_FLAGS_FORBID_LANDSCAPE_CHANGES) + LandSetHeightAction::LandSetHeightAction(const CoordsXY& coords, uint8_t height, uint8_t style) + : _coords(coords) + , _height(height) + , _style(style) { - return GameActions::Result(GameActions::Status::Disallowed, STR_FORBIDDEN_BY_THE_LOCAL_AUTHORITY, kStringIdNone); } - StringId errorMessage = CheckParameters(); - if (errorMessage != kStringIdNone) + void LandSetHeightAction::AcceptParameters(GameActionParameterVisitor& visitor) { - return GameActions::Result(GameActions::Status::Disallowed, kStringIdNone, errorMessage); + visitor.Visit(_coords); + visitor.Visit("height", _height); + visitor.Visit("style", _style); } - if (gLegacyScene != LegacyScene::scenarioEditor && !gameState.cheats.sandboxMode) + uint16_t LandSetHeightAction::GetActionFlags() const { - if (!MapIsLocationInPark(_coords)) + return GameAction::GetActionFlags() | Flags::EditorOnly; + } + + void LandSetHeightAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_coords) << DS_TAG(_height) << DS_TAG(_style); + } + + Result LandSetHeightAction::Query() const + { + auto& gameState = getGameState(); + if (gameState.park.flags & PARK_FLAGS_FORBID_LANDSCAPE_CHANGES) { - return GameActions::Result(GameActions::Status::Disallowed, STR_LAND_NOT_OWNED_BY_PARK, kStringIdNone); + return Result(Status::Disallowed, STR_FORBIDDEN_BY_THE_LOCAL_AUTHORITY, kStringIdNone); } - } - money64 sceneryRemovalCost = 0; - if (!gameState.cheats.disableClearanceChecks) - { - if (gameState.park.flags & PARK_FLAGS_FORBID_TREE_REMOVAL) - { - // Check for obstructing large trees - TileElement* tileElement = CheckTreeObstructions(); - if (tileElement != nullptr) - { - auto res = GameActions::Result(GameActions::Status::Disallowed, kStringIdNone, kStringIdNone); - MapGetObstructionErrorText(tileElement, res); - return res; - } - } - sceneryRemovalCost = GetSmallSceneryRemovalCost(); - } - - // Check for ride support limits - if (!gameState.cheats.disableSupportLimits) - { - errorMessage = CheckRideSupports(); + StringId errorMessage = CheckParameters(); if (errorMessage != kStringIdNone) { - return GameActions::Result(GameActions::Status::Disallowed, kStringIdNone, errorMessage); + return Result(Status::Disallowed, kStringIdNone, errorMessage); } - } - auto* surfaceElement = MapGetSurfaceElementAt(_coords); - if (surfaceElement == nullptr) - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); - - // We need to check if there is _currently_ a level crossing on the tile. - // For that, we need the old height, so we can't use the _height variable. - auto oldCoords = CoordsXYZ{ _coords, surfaceElement->GetBaseZ() }; - auto* pathElement = MapGetFootpathElement(oldCoords); - if (pathElement != nullptr && pathElement->AsPath()->IsLevelCrossing(oldCoords)) - { - return GameActions::Result(GameActions::Status::Disallowed, STR_REMOVE_LEVEL_CROSSING_FIRST, kStringIdNone); - } - - TileElement* tileElement = CheckFloatingStructures(reinterpret_cast(surfaceElement), _height); - if (tileElement != nullptr) - { - auto res = GameActions::Result(GameActions::Status::Disallowed, kStringIdNone, kStringIdNone); - MapGetObstructionErrorText(tileElement, res); - return res; - } - - if (!gameState.cheats.disableClearanceChecks) - { - uint8_t zCorner = _height; - if (_style & kTileSlopeRaisedCornersMask) + if (gLegacyScene != LegacyScene::scenarioEditor && !gameState.cheats.sandboxMode) { - zCorner += 2; - if (_style & kTileSlopeDiagonalFlag) + if (!MapIsLocationInPark(_coords)) { - zCorner += 2; + return Result(Status::Disallowed, STR_LAND_NOT_OWNED_BY_PARK, kStringIdNone); } } - auto clearResult = MapCanConstructWithClearAt( - { _coords, _height * kCoordsZStep, zCorner * kCoordsZStep }, &MapSetLandHeightClearFunc, { 0b1111, 0 }, 0, - CreateCrossingMode::none); - if (clearResult.Error != GameActions::Status::Ok) + money64 sceneryRemovalCost = 0; + if (!gameState.cheats.disableClearanceChecks) { - clearResult.Error = GameActions::Status::Disallowed; - return clearResult; - } - } - auto res = GameActions::Result(); - res.Cost = sceneryRemovalCost + GetSurfaceHeightChangeCost(surfaceElement); - res.Expenditure = ExpenditureType::landscaping; - return res; -} - -GameActions::Result LandSetHeightAction::Execute() const -{ - money64 cost = 0.00_GBP; - auto surfaceHeight = TileElementHeight(_coords); - FootpathRemoveLitter({ _coords, surfaceHeight }); - - if (!getGameState().cheats.disableClearanceChecks) - { - WallRemoveAt({ _coords, _height * 8 - 16, _height * 8 + 32 }); - cost += GetSmallSceneryRemovalCost(); - SmallSceneryRemoval(); - } - - auto* surfaceElement = MapGetSurfaceElementAt(_coords); - if (surfaceElement == nullptr) - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); - - cost += GetSurfaceHeightChangeCost(surfaceElement); - SetSurfaceHeight(reinterpret_cast(surfaceElement)); - - auto res = GameActions::Result(); - res.Position = { _coords.x + 16, _coords.y + 16, surfaceHeight }; - res.Cost = cost; - res.Expenditure = ExpenditureType::landscaping; - return res; -} - -StringId LandSetHeightAction::CheckParameters() const -{ - if (!LocationValid(_coords) || MapIsEdge(_coords)) - { - return STR_OFF_EDGE_OF_MAP; - } - - if (_height < kMinimumLandHeight) - { - return STR_TOO_LOW; - } - - // Divide by 2 and subtract 7 to get the in-game units. - if (_height > kMaximumLandHeight) - { - return STR_TOO_HIGH; - } - - if (_height > kMaximumLandHeight - 2 && (_style & kTileSlopeMask) != 0) - { - return STR_TOO_HIGH; - } - - if (_height == kMaximumLandHeight - 2 && (_style & kTileSlopeDiagonalFlag)) - { - return STR_TOO_HIGH; - } - - return kStringIdNone; -} - -TileElement* LandSetHeightAction::CheckTreeObstructions() const -{ - for (auto* sceneryElement : TileElementsView(_coords)) - { - if (_height > sceneryElement->ClearanceHeight) - continue; - if (_height + 4 < sceneryElement->BaseHeight) - continue; - - auto* sceneryEntry = sceneryElement->GetEntry(); - if (!sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_IS_TREE)) - continue; - - return sceneryElement->as(); - } - return nullptr; -} - -money64 LandSetHeightAction::GetSmallSceneryRemovalCost() const -{ - money64 cost{ 0 }; - - for (auto* sceneryElement : TileElementsView(_coords)) - { - if (_height > sceneryElement->ClearanceHeight) - continue; - if (_height + 4 < sceneryElement->BaseHeight) - continue; - - auto* sceneryEntry = sceneryElement->GetEntry(); - if (sceneryEntry == nullptr) - continue; - - cost += sceneryEntry->removal_price; - } - - return cost; -} - -void LandSetHeightAction::SmallSceneryRemoval() const -{ - TileElement* tileElement = MapGetFirstElementAt(_coords); - do - { - if (tileElement == nullptr) - break; - if (tileElement->GetType() != TileElementType::SmallScenery) - continue; - if (_height > tileElement->ClearanceHeight) - continue; - if (_height + 4 < tileElement->BaseHeight) - continue; - TileElementRemove(tileElement--); - } while (!(tileElement++)->IsLastForTile()); -} - -StringId LandSetHeightAction::CheckRideSupports() const -{ - for (auto* trackElement : TileElementsView(_coords)) - { - RideId rideIndex = trackElement->GetRideIndex(); - - auto ride = GetRide(rideIndex); - if (ride == nullptr) - continue; - - const auto* rideEntry = ride->getRideEntry(); - if (rideEntry == nullptr) - continue; - - int32_t maxHeight = rideEntry->max_height; - if (maxHeight == 0) - { - maxHeight = ride->getRideTypeDescriptor().Heights.MaxHeight; + if (gameState.park.flags & PARK_FLAGS_FORBID_TREE_REMOVAL) + { + // Check for obstructing large trees + TileElement* tileElement = CheckTreeObstructions(); + if (tileElement != nullptr) + { + auto res = Result(Status::Disallowed, kStringIdNone, kStringIdNone); + MapGetObstructionErrorText(tileElement, res); + return res; + } + } + sceneryRemovalCost = GetSmallSceneryRemovalCost(); } - int32_t zDelta = trackElement->ClearanceHeight - _height; - if (zDelta >= 0 && zDelta / 2 > maxHeight) + // Check for ride support limits + if (!gameState.cheats.disableSupportLimits) { - return STR_SUPPORTS_CANT_BE_EXTENDED; + errorMessage = CheckRideSupports(); + if (errorMessage != kStringIdNone) + { + return Result(Status::Disallowed, kStringIdNone, errorMessage); + } } - } - return kStringIdNone; -} -TileElement* LandSetHeightAction::CheckFloatingStructures(TileElement* surfaceElement, uint8_t zCorner) const -{ - if (surfaceElement->AsSurface()->HasTrackThatNeedsWater()) - { - uint32_t waterHeight = surfaceElement->AsSurface()->GetWaterHeight(); - if (waterHeight != 0) + auto* surfaceElement = MapGetSurfaceElementAt(_coords); + if (surfaceElement == nullptr) + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); + + // We need to check if there is _currently_ a level crossing on the tile. + // For that, we need the old height, so we can't use the _height variable. + auto oldCoords = CoordsXYZ{ _coords, surfaceElement->GetBaseZ() }; + auto* pathElement = MapGetFootpathElement(oldCoords); + if (pathElement != nullptr && pathElement->AsPath()->IsLevelCrossing(oldCoords)) { - if (_style & kTileSlopeMask) + return Result(Status::Disallowed, STR_REMOVE_LEVEL_CROSSING_FIRST, kStringIdNone); + } + + TileElement* tileElement = CheckFloatingStructures(reinterpret_cast(surfaceElement), _height); + if (tileElement != nullptr) + { + auto res = Result(Status::Disallowed, kStringIdNone, kStringIdNone); + MapGetObstructionErrorText(tileElement, res); + return res; + } + + if (!gameState.cheats.disableClearanceChecks) + { + uint8_t zCorner = _height; + if (_style & kTileSlopeRaisedCornersMask) { zCorner += 2; if (_style & kTileSlopeDiagonalFlag) @@ -314,50 +138,225 @@ TileElement* LandSetHeightAction::CheckFloatingStructures(TileElement* surfaceEl zCorner += 2; } } - if (zCorner > (waterHeight / kCoordsZStep) - 2) + + auto clearResult = MapCanConstructWithClearAt( + { _coords, _height * kCoordsZStep, zCorner * kCoordsZStep }, &MapSetLandHeightClearFunc, { 0b1111, 0 }, 0, + CreateCrossingMode::none); + if (clearResult.Error != Status::Ok) { - return ++surfaceElement; + clearResult.Error = Status::Disallowed; + return clearResult; } } + auto res = Result(); + res.Cost = sceneryRemovalCost + GetSurfaceHeightChangeCost(surfaceElement); + res.Expenditure = ExpenditureType::landscaping; + return res; } - return nullptr; -} -money64 LandSetHeightAction::GetSurfaceHeightChangeCost(SurfaceElement* surfaceElement) const -{ - money64 cost{ 0 }; - for (Direction i : kAllDirections) + Result LandSetHeightAction::Execute() const { - int32_t cornerHeight = TileElementGetCornerHeight(surfaceElement, i); - cornerHeight -= MapGetCornerHeight(_height, _style & kTileSlopeMask, i); - cost += 2.50_GBP * abs(cornerHeight); - } - return cost; -} + money64 cost = 0.00_GBP; + auto surfaceHeight = TileElementHeight(_coords); + FootpathRemoveLitter({ _coords, surfaceHeight }); -void LandSetHeightAction::SetSurfaceHeight(TileElement* surfaceElement) const -{ - surfaceElement->BaseHeight = _height; - surfaceElement->ClearanceHeight = _height; - surfaceElement->AsSurface()->SetSlope(_style); - int32_t waterHeight = surfaceElement->AsSurface()->GetWaterHeight() / kCoordsZStep; - if (waterHeight != 0 && waterHeight <= _height) + if (!getGameState().cheats.disableClearanceChecks) + { + WallRemoveAt({ _coords, _height * 8 - 16, _height * 8 + 32 }); + cost += GetSmallSceneryRemovalCost(); + SmallSceneryRemoval(); + } + + auto* surfaceElement = MapGetSurfaceElementAt(_coords); + if (surfaceElement == nullptr) + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); + + cost += GetSurfaceHeightChangeCost(surfaceElement); + SetSurfaceHeight(reinterpret_cast(surfaceElement)); + + auto res = Result(); + res.Position = { _coords.x + 16, _coords.y + 16, surfaceHeight }; + res.Cost = cost; + res.Expenditure = ExpenditureType::landscaping; + return res; + } + + StringId LandSetHeightAction::CheckParameters() const { - surfaceElement->AsSurface()->SetWaterHeight(0); + if (!LocationValid(_coords) || MapIsEdge(_coords)) + { + return STR_OFF_EDGE_OF_MAP; + } + + if (_height < kMinimumLandHeight) + { + return STR_TOO_LOW; + } + + // Divide by 2 and subtract 7 to get the in-game units. + if (_height > kMaximumLandHeight) + { + return STR_TOO_HIGH; + } + + if (_height > kMaximumLandHeight - 2 && (_style & kTileSlopeMask) != 0) + { + return STR_TOO_HIGH; + } + + if (_height == kMaximumLandHeight - 2 && (_style & kTileSlopeDiagonalFlag)) + { + return STR_TOO_HIGH; + } + + return kStringIdNone; } - MapInvalidateTileFull(_coords); -} + TileElement* LandSetHeightAction::CheckTreeObstructions() const + { + for (auto* sceneryElement : TileElementsView(_coords)) + { + if (_height > sceneryElement->ClearanceHeight) + continue; + if (_height + 4 < sceneryElement->BaseHeight) + continue; -int32_t 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; + auto* sceneryEntry = sceneryElement->GetEntry(); + if (!sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_IS_TREE)) + continue; - if ((*tile_element)->GetType() == TileElementType::SmallScenery) - return 0; + return sceneryElement->as(); + } + return nullptr; + } - return 1; -} + money64 LandSetHeightAction::GetSmallSceneryRemovalCost() const + { + money64 cost{ 0 }; + + for (auto* sceneryElement : TileElementsView(_coords)) + { + if (_height > sceneryElement->ClearanceHeight) + continue; + if (_height + 4 < sceneryElement->BaseHeight) + continue; + + auto* sceneryEntry = sceneryElement->GetEntry(); + if (sceneryEntry == nullptr) + continue; + + cost += sceneryEntry->removal_price; + } + + return cost; + } + + void LandSetHeightAction::SmallSceneryRemoval() const + { + TileElement* tileElement = MapGetFirstElementAt(_coords); + do + { + if (tileElement == nullptr) + break; + if (tileElement->GetType() != TileElementType::SmallScenery) + continue; + if (_height > tileElement->ClearanceHeight) + continue; + if (_height + 4 < tileElement->BaseHeight) + continue; + TileElementRemove(tileElement--); + } while (!(tileElement++)->IsLastForTile()); + } + + StringId LandSetHeightAction::CheckRideSupports() const + { + for (auto* trackElement : TileElementsView(_coords)) + { + RideId rideIndex = trackElement->GetRideIndex(); + + auto ride = GetRide(rideIndex); + if (ride == nullptr) + continue; + + const auto* rideEntry = ride->getRideEntry(); + if (rideEntry == nullptr) + continue; + + int32_t maxHeight = rideEntry->max_height; + if (maxHeight == 0) + { + maxHeight = ride->getRideTypeDescriptor().Heights.MaxHeight; + } + + int32_t zDelta = trackElement->ClearanceHeight - _height; + if (zDelta >= 0 && zDelta / 2 > maxHeight) + { + return STR_SUPPORTS_CANT_BE_EXTENDED; + } + } + return kStringIdNone; + } + + TileElement* LandSetHeightAction::CheckFloatingStructures(TileElement* surfaceElement, uint8_t zCorner) const + { + if (surfaceElement->AsSurface()->HasTrackThatNeedsWater()) + { + uint32_t waterHeight = surfaceElement->AsSurface()->GetWaterHeight(); + if (waterHeight != 0) + { + if (_style & kTileSlopeMask) + { + zCorner += 2; + if (_style & kTileSlopeDiagonalFlag) + { + zCorner += 2; + } + } + if (zCorner > (waterHeight / kCoordsZStep) - 2) + { + return ++surfaceElement; + } + } + } + return nullptr; + } + + money64 LandSetHeightAction::GetSurfaceHeightChangeCost(SurfaceElement* surfaceElement) const + { + money64 cost{ 0 }; + for (Direction i : kAllDirections) + { + int32_t cornerHeight = TileElementGetCornerHeight(surfaceElement, i); + cornerHeight -= MapGetCornerHeight(_height, _style & kTileSlopeMask, i); + cost += 2.50_GBP * abs(cornerHeight); + } + return cost; + } + + void LandSetHeightAction::SetSurfaceHeight(TileElement* surfaceElement) const + { + surfaceElement->BaseHeight = _height; + surfaceElement->ClearanceHeight = _height; + surfaceElement->AsSurface()->SetSlope(_style); + int32_t waterHeight = surfaceElement->AsSurface()->GetWaterHeight() / kCoordsZStep; + if (waterHeight != 0 && waterHeight <= _height) + { + surfaceElement->AsSurface()->SetWaterHeight(0); + } + + MapInvalidateTileFull(_coords); + } + + int32_t 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; + + if ((*tile_element)->GetType() == TileElementType::SmallScenery) + return 0; + + return 1; + } +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSetHeightAction.h b/src/openrct2/actions/LandSetHeightAction.h index 540a096382..eab40d6b79 100644 --- a/src/openrct2/actions/LandSetHeightAction.h +++ b/src/openrct2/actions/LandSetHeightAction.h @@ -11,40 +11,43 @@ #include "GameAction.h" -class LandSetHeightAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - CoordsXY _coords; - uint8_t _height{}; - uint8_t _style{}; + class LandSetHeightAction final : public GameActionBase + { + private: + CoordsXY _coords; + uint8_t _height{}; + uint8_t _style{}; -public: - LandSetHeightAction() = default; - LandSetHeightAction(const CoordsXY& coords, uint8_t height, uint8_t style); + public: + LandSetHeightAction() = default; + LandSetHeightAction(const CoordsXY& coords, uint8_t height, uint8_t style); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; -private: - StringId CheckParameters() const; - OpenRCT2::TileElement* CheckTreeObstructions() const; - money64 GetSmallSceneryRemovalCost() const; - void SmallSceneryRemoval() const; - StringId CheckRideSupports() const; - OpenRCT2::TileElement* CheckFloatingStructures(OpenRCT2::TileElement* surfaceElement, uint8_t zCorner) const; - money64 GetSurfaceHeightChangeCost(OpenRCT2::SurfaceElement* surfaceElement) const; - void SetSurfaceHeight(OpenRCT2::TileElement* surfaceElement) const; + private: + StringId CheckParameters() const; + TileElement* CheckTreeObstructions() const; + money64 GetSmallSceneryRemovalCost() const; + void SmallSceneryRemoval() const; + StringId CheckRideSupports() const; + TileElement* CheckFloatingStructures(TileElement* surfaceElement, uint8_t zCorner) const; + money64 GetSurfaceHeightChangeCost(SurfaceElement* surfaceElement) const; + void SetSurfaceHeight(TileElement* surfaceElement) const; - /** - * - * rct2: 0x00663CB9 - */ - static int32_t MapSetLandHeightClearFunc( - OpenRCT2::TileElement** tile_element, [[maybe_unused]] const CoordsXY& coords, [[maybe_unused]] uint8_t flags, - [[maybe_unused]] money64* price); -}; + /** + * + * rct2: 0x00663CB9 + */ + static int32_t MapSetLandHeightClearFunc( + TileElement** tile_element, [[maybe_unused]] const CoordsXY& coords, [[maybe_unused]] uint8_t flags, + [[maybe_unused]] money64* price); + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSetRightsAction.cpp b/src/openrct2/actions/LandSetRightsAction.cpp index 0b7bc0861f..27d1b74774 100644 --- a/src/openrct2/actions/LandSetRightsAction.cpp +++ b/src/openrct2/actions/LandSetRightsAction.cpp @@ -26,213 +26,214 @@ #include "../world/tile_element/EntranceElement.h" #include "../world/tile_element/SurfaceElement.h" -using namespace OpenRCT2; -using namespace OpenRCT2::Numerics; - -LandSetRightsAction::LandSetRightsAction(const MapRange& range, LandSetRightSetting setting, uint8_t ownership) - : _range(range) - , _setting(setting) - , _ownership(ownership) +namespace OpenRCT2::GameActions { -} + using namespace OpenRCT2::Numerics; -LandSetRightsAction::LandSetRightsAction(const CoordsXY& coord, LandSetRightSetting setting, uint8_t ownership) - : _range(coord.x, coord.y, coord.x, coord.y) - , _setting(setting) - , _ownership(ownership) -{ -} - -void LandSetRightsAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_range); - visitor.Visit("setting", _setting); - visitor.Visit("ownership", _ownership); -} - -uint16_t LandSetRightsAction::GetActionFlags() const -{ - return GameAction::GetActionFlags() | GameActions::Flags::EditorOnly; -} - -void LandSetRightsAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_range) << DS_TAG(_setting) << DS_TAG(_ownership); -} - -GameActions::Result LandSetRightsAction::Query() const -{ - return QueryExecute(false); -} - -GameActions::Result LandSetRightsAction::Execute() const -{ - return QueryExecute(true); -} - -GameActions::Result LandSetRightsAction::QueryExecute(bool isExecuting) const -{ - auto res = GameActions::Result(); - - auto validRange = ClampRangeWithinMap(_range.Normalise()); - CoordsXYZ centre{ (validRange.GetLeft() + validRange.GetRight()) / 2 + 16, - (validRange.GetTop() + validRange.GetBottom()) / 2 + 16, 0 }; - centre.z = TileElementHeight(centre); - - res.Position = centre; - res.Expenditure = ExpenditureType::landPurchase; - - if (!isInEditorMode() && !getGameState().cheats.sandboxMode) + LandSetRightsAction::LandSetRightsAction(const MapRange& range, LandSetRightSetting setting, uint8_t ownership) + : _range(range) + , _setting(setting) + , _ownership(ownership) { - return GameActions::Result(GameActions::Status::NotInEditorMode, kStringIdNone, STR_LAND_NOT_FOR_SALE); } - // Game command modified to accept selection size - for (auto y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + LandSetRightsAction::LandSetRightsAction(const CoordsXY& coord, LandSetRightSetting setting, uint8_t ownership) + : _range(coord.x, coord.y, coord.x, coord.y) + , _setting(setting) + , _ownership(ownership) { - for (auto x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) + } + + void LandSetRightsAction::AcceptParameters(GameActionParameterVisitor& visitor) + { + visitor.Visit(_range); + visitor.Visit("setting", _setting); + visitor.Visit("ownership", _ownership); + } + + uint16_t LandSetRightsAction::GetActionFlags() const + { + return GameAction::GetActionFlags() | Flags::EditorOnly; + } + + void LandSetRightsAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_range) << DS_TAG(_setting) << DS_TAG(_ownership); + } + + Result LandSetRightsAction::Query() const + { + return QueryExecute(false); + } + + Result LandSetRightsAction::Execute() const + { + return QueryExecute(true); + } + + Result LandSetRightsAction::QueryExecute(bool isExecuting) const + { + auto res = Result(); + + auto validRange = ClampRangeWithinMap(_range.Normalise()); + CoordsXYZ centre{ (validRange.GetLeft() + validRange.GetRight()) / 2 + 16, + (validRange.GetTop() + validRange.GetBottom()) / 2 + 16, 0 }; + centre.z = TileElementHeight(centre); + + res.Position = centre; + res.Expenditure = ExpenditureType::landPurchase; + + if (!isInEditorMode() && !getGameState().cheats.sandboxMode) { - if (!LocationValid({ x, y })) - continue; - auto result = MapBuyLandRightsForTile({ x, y }, isExecuting); - if (result.Error == GameActions::Status::Ok) + return Result(Status::NotInEditorMode, kStringIdNone, STR_LAND_NOT_FOR_SALE); + } + + // Game command modified to accept selection size + for (auto y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + { + for (auto x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) { - res.Cost += result.Cost; + if (!LocationValid({ x, y })) + continue; + auto result = MapBuyLandRightsForTile({ x, y }, isExecuting); + if (result.Error == Status::Ok) + { + res.Cost += result.Cost; + } } } - } - if (isExecuting) - { - MapCountRemainingLandRights(); - OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, centre); - } - return res; -} - -GameActions::Result LandSetRightsAction::MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const -{ - SurfaceElement* surfaceElement = MapGetSurfaceElementAt(loc); - if (surfaceElement == nullptr) - { - LOG_ERROR("No surface at x = %d, y = %d", loc.x, loc.y); - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); - } - - auto res = GameActions::Result(); - switch (_setting) - { - case LandSetRightSetting::UnownLand: - if (isExecuting) - { - surfaceElement->SetOwnership( - surfaceElement->GetOwnership() & ~(OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)); - Park::UpdateFencesAroundTile(loc); - } - return res; - case LandSetRightSetting::UnownConstructionRights: - if (isExecuting) - { - surfaceElement->SetOwnership(surfaceElement->GetOwnership() & ~OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED); - uint16_t baseZ = surfaceElement->GetBaseZ(); - MapInvalidateTile({ loc, baseZ, baseZ + 16 }); - } - return res; - case LandSetRightSetting::SetForSale: - if (isExecuting) - { - surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_AVAILABLE); - uint16_t baseZ = surfaceElement->GetBaseZ(); - MapInvalidateTile({ loc, baseZ, baseZ + 16 }); - } - return res; - case LandSetRightSetting::SetConstructionRightsForSale: - if (isExecuting) - { - surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE); - uint16_t baseZ = surfaceElement->GetBaseZ(); - MapInvalidateTile({ loc, baseZ, baseZ + 16 }); - } - return res; - case LandSetRightSetting::SetOwnershipWithChecks: + if (isExecuting) { - if (_ownership == surfaceElement->GetOwnership()) - { - return res; - } + MapCountRemainingLandRights(); + OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, centre); + } + return res; + } - for (auto* entranceElement : TileElementsView(loc)) - { - if (entranceElement->GetEntranceType() != ENTRANCE_TYPE_PARK_ENTRANCE) - continue; + Result LandSetRightsAction::MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const + { + SurfaceElement* surfaceElement = MapGetSurfaceElementAt(loc); + if (surfaceElement == nullptr) + { + LOG_ERROR("No surface at x = %d, y = %d", loc.x, loc.y); + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); + } - // Do not allow ownership of park entrance. - if (_ownership == OWNERSHIP_OWNED || _ownership == OWNERSHIP_AVAILABLE) - return res; - - // Allow construction rights available / for sale on park entrances on surface. - // There is no need to check the height if _ownership is 0 (unowned and no rights available). - if (_ownership == OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED || _ownership == OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE) + auto res = Result(); + switch (_setting) + { + case LandSetRightSetting::UnownLand: + if (isExecuting) { - if (entranceElement->BaseHeight - 3 > surfaceElement->BaseHeight - || entranceElement->BaseHeight < surfaceElement->BaseHeight) - { + surfaceElement->SetOwnership( + surfaceElement->GetOwnership() & ~(OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)); + Park::UpdateFencesAroundTile(loc); + } + return res; + case LandSetRightSetting::UnownConstructionRights: + if (isExecuting) + { + surfaceElement->SetOwnership(surfaceElement->GetOwnership() & ~OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED); + uint16_t baseZ = surfaceElement->GetBaseZ(); + MapInvalidateTile({ loc, baseZ, baseZ + 16 }); + } + return res; + case LandSetRightSetting::SetForSale: + if (isExecuting) + { + surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_AVAILABLE); + uint16_t baseZ = surfaceElement->GetBaseZ(); + MapInvalidateTile({ loc, baseZ, baseZ + 16 }); + } + return res; + case LandSetRightSetting::SetConstructionRightsForSale: + if (isExecuting) + { + surfaceElement->SetOwnership(surfaceElement->GetOwnership() | OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE); + uint16_t baseZ = surfaceElement->GetBaseZ(); + MapInvalidateTile({ loc, baseZ, baseZ + 16 }); + } + return res; + case LandSetRightSetting::SetOwnershipWithChecks: + { + if (_ownership == surfaceElement->GetOwnership()) + { + return res; + } + + for (auto* entranceElement : TileElementsView(loc)) + { + if (entranceElement->GetEntranceType() != ENTRANCE_TYPE_PARK_ENTRANCE) + continue; + + // Do not allow ownership of park entrance. + if (_ownership == OWNERSHIP_OWNED || _ownership == OWNERSHIP_AVAILABLE) return res; + + // Allow construction rights available / for sale on park entrances on surface. + // There is no need to check the height if _ownership is 0 (unowned and no rights available). + if (_ownership == OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED + || _ownership == OWNERSHIP_CONSTRUCTION_RIGHTS_AVAILABLE) + { + if (entranceElement->BaseHeight - 3 > surfaceElement->BaseHeight + || entranceElement->BaseHeight < surfaceElement->BaseHeight) + { + return res; + } } } - } - auto& gameState = getGameState(); - const uint8_t currentOwnership = surfaceElement->GetOwnership(); + auto& gameState = getGameState(); + const uint8_t currentOwnership = surfaceElement->GetOwnership(); - // Are land rights or construction rights currently owned? - if (!(currentOwnership & (OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED))) - { - // Buying land - if (!(currentOwnership & OWNERSHIP_OWNED) && (_ownership & OWNERSHIP_OWNED)) - res.Cost = gameState.scenarioOptions.landPrice; - - // Buying construction rights - if (!(currentOwnership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED) - && (_ownership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) - res.Cost = gameState.scenarioOptions.constructionRightsPrice; - } - else - { - // Selling land - if ((currentOwnership & OWNERSHIP_OWNED) && !(_ownership & OWNERSHIP_OWNED)) - res.Cost = -gameState.scenarioOptions.landPrice; - - // Selling construction rights - if ((currentOwnership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED) - && !(_ownership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) - res.Cost = -gameState.scenarioOptions.constructionRightsPrice; - } - - if (isExecuting) - { - if (_ownership != OWNERSHIP_UNOWNED) + // Are land rights or construction rights currently owned? + if (!(currentOwnership & (OWNERSHIP_OWNED | OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED))) { - gameState.peepSpawns.erase( - std::remove_if( - gameState.peepSpawns.begin(), gameState.peepSpawns.end(), - [x = loc.x, y = loc.y](const auto& spawn) { - return floor2(spawn.x, 32) == x && floor2(spawn.y, 32) == y; - }), - gameState.peepSpawns.end()); + // Buying land + if (!(currentOwnership & OWNERSHIP_OWNED) && (_ownership & OWNERSHIP_OWNED)) + res.Cost = gameState.scenarioOptions.landPrice; + + // Buying construction rights + if (!(currentOwnership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED) + && (_ownership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) + res.Cost = gameState.scenarioOptions.constructionRightsPrice; } - surfaceElement->SetOwnership(_ownership); - Park::UpdateFencesAroundTile(loc); - gMapLandRightsUpdateSuccess = true; + else + { + // Selling land + if ((currentOwnership & OWNERSHIP_OWNED) && !(_ownership & OWNERSHIP_OWNED)) + res.Cost = -gameState.scenarioOptions.landPrice; + + // Selling construction rights + if ((currentOwnership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED) + && !(_ownership & OWNERSHIP_CONSTRUCTION_RIGHTS_OWNED)) + res.Cost = -gameState.scenarioOptions.constructionRightsPrice; + } + + if (isExecuting) + { + if (_ownership != OWNERSHIP_UNOWNED) + { + gameState.peepSpawns.erase( + std::remove_if( + gameState.peepSpawns.begin(), gameState.peepSpawns.end(), + [x = loc.x, y = loc.y](const auto& spawn) { + return floor2(spawn.x, 32) == x && floor2(spawn.y, 32) == y; + }), + gameState.peepSpawns.end()); + } + surfaceElement->SetOwnership(_ownership); + Park::UpdateFencesAroundTile(loc); + gMapLandRightsUpdateSuccess = true; + } + return res; } - return res; + default: + LOG_ERROR("Invalid setting %u to set land rights", _setting); + return Result(Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); } - default: - LOG_ERROR("Invalid setting %u to set land rights", _setting); - return GameActions::Result( - GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_VALUE_OUT_OF_RANGE); } -} +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSetRightsAction.h b/src/openrct2/actions/LandSetRightsAction.h index 63fc9ebb49..bf804da984 100644 --- a/src/openrct2/actions/LandSetRightsAction.h +++ b/src/openrct2/actions/LandSetRightsAction.h @@ -11,37 +11,40 @@ #include "GameAction.h" -enum class LandSetRightSetting : uint8_t +namespace OpenRCT2::GameActions { - UnownLand, - UnownConstructionRights, - SetForSale, - SetConstructionRightsForSale, - SetOwnershipWithChecks, - Count -}; + enum class LandSetRightSetting : uint8_t + { + UnownLand, + UnownConstructionRights, + SetForSale, + SetConstructionRightsForSale, + SetOwnershipWithChecks, + Count + }; -class LandSetRightsAction final : public GameActionBase -{ -private: - MapRange _range; - LandSetRightSetting _setting{ LandSetRightSetting::Count }; - uint8_t _ownership{}; + class LandSetRightsAction final : public GameActionBase + { + private: + MapRange _range; + LandSetRightSetting _setting{ LandSetRightSetting::Count }; + uint8_t _ownership{}; -public: - LandSetRightsAction() = default; - LandSetRightsAction(const MapRange& range, LandSetRightSetting setting, uint8_t ownership = 0); - LandSetRightsAction(const CoordsXY& coord, LandSetRightSetting setting, uint8_t ownership = 0); + public: + LandSetRightsAction() = default; + LandSetRightsAction(const MapRange& range, LandSetRightSetting setting, uint8_t ownership = 0); + LandSetRightsAction(const CoordsXY& coord, LandSetRightSetting setting, uint8_t ownership = 0); - void AcceptParameters(GameActionParameterVisitor& visitor) override; + void AcceptParameters(GameActionParameterVisitor& visitor) override; - uint16_t GetActionFlags() const override; + uint16_t GetActionFlags() const override; - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; -private: - OpenRCT2::GameActions::Result QueryExecute(bool isExecuting) const; - OpenRCT2::GameActions::Result MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const; -}; + private: + Result QueryExecute(bool isExecuting) const; + Result MapBuyLandRightsForTile(const CoordsXY& loc, bool isExecuting) const; + }; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSmoothAction.cpp b/src/openrct2/actions/LandSmoothAction.cpp index 6a1701e8d6..af078b5dc6 100644 --- a/src/openrct2/actions/LandSmoothAction.cpp +++ b/src/openrct2/actions/LandSmoothAction.cpp @@ -28,617 +28,629 @@ #include -using namespace OpenRCT2; - -LandSmoothAction::LandSmoothAction(const CoordsXY& coords, MapRange range, uint8_t selectionType, bool isLowering) - : _coords(coords) - , _range(range) - , _selectionType(selectionType) - , _isLowering(isLowering) +namespace OpenRCT2::GameActions { -} - -void LandSmoothAction::AcceptParameters(GameActionParameterVisitor& visitor) -{ - visitor.Visit(_coords); - visitor.Visit(_range); - visitor.Visit("selectionType", _selectionType); - visitor.Visit("isLowering", _isLowering); -} - -uint16_t LandSmoothAction::GetActionFlags() const -{ - return GameAction::GetActionFlags(); -} - -void LandSmoothAction::Serialise(DataSerialiser& stream) -{ - GameAction::Serialise(stream); - - stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType) << DS_TAG(_isLowering); -} - -GameActions::Result LandSmoothAction::Query() const -{ - return SmoothLand(false); -} - -GameActions::Result LandSmoothAction::Execute() const -{ - return SmoothLand(true); -} - -GameActions::Result LandSmoothAction::SmoothLandTile( - int32_t direction, bool isExecuting, const CoordsXY& loc, SurfaceElement* surfaceElement) const -{ - int32_t targetBaseZ = surfaceElement->BaseHeight; - int32_t slope = surfaceElement->GetSlope(); - if (_isLowering) + LandSmoothAction::LandSmoothAction(const CoordsXY& coords, MapRange range, uint8_t selectionType, bool isLowering) + : _coords(coords) + , _range(range) + , _selectionType(selectionType) + , _isLowering(isLowering) { - slope = LowerSurfaceCornerFlags(direction, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) + } + + void LandSmoothAction::AcceptParameters(GameActionParameterVisitor& visitor) + { + visitor.Visit(_coords); + visitor.Visit(_range); + visitor.Visit("selectionType", _selectionType); + visitor.Visit("isLowering", _isLowering); + } + + uint16_t LandSmoothAction::GetActionFlags() const + { + return GameAction::GetActionFlags(); + } + + void LandSmoothAction::Serialise(DataSerialiser& stream) + { + GameAction::Serialise(stream); + + stream << DS_TAG(_coords) << DS_TAG(_range) << DS_TAG(_selectionType) << DS_TAG(_isLowering); + } + + Result LandSmoothAction::Query() const + { + return SmoothLand(false); + } + + Result LandSmoothAction::Execute() const + { + return SmoothLand(true); + } + + Result LandSmoothAction::SmoothLandTile( + int32_t direction, bool isExecuting, const CoordsXY& loc, SurfaceElement* surfaceElement) const + { + int32_t targetBaseZ = surfaceElement->BaseHeight; + int32_t slope = surfaceElement->GetSlope(); + if (_isLowering) { - targetBaseZ -= 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; - } - } - else - { - slope = RaiseSurfaceCornerFlags(direction, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) - { - targetBaseZ += 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; - } - } - - auto landSetHeightAction = LandSetHeightAction(loc, targetBaseZ, slope); - landSetHeightAction.SetFlags(GetFlags()); - auto res = isExecuting ? GameActions::ExecuteNested(&landSetHeightAction) : GameActions::QueryNested(&landSetHeightAction); - - return res; -} - -money64 LandSmoothAction::SmoothLandRowByEdge( - bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight1, int32_t expectedLandHeight2, int32_t stepX, - int32_t stepY, int32_t direction1, int32_t direction2, int32_t checkDirection1, int32_t checkDirection2) const -{ - uint8_t shouldContinue = 0xF; - int32_t landChangePerTile = _isLowering ? 2 : -2; - money64 totalCost = 0; - - // check if we need to start at all - if (!LocationValid(loc) || !LocationValid({ loc.x + stepX, loc.y + stepY })) - { - return 0; - } - auto surfaceElement = MapGetSurfaceElementAt(loc); - auto nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ loc.x + stepX, loc.y + stepY }); - if (surfaceElement == nullptr || nextSurfaceElement == nullptr) - { - return 0; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection1) != expectedLandHeight1 + landChangePerTile) - { - shouldContinue &= ~0x1; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection2) != expectedLandHeight2 + landChangePerTile) - { - shouldContinue &= ~0x2; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection1) - != TileElementGetCornerHeight(nextSurfaceElement, direction1)) - { - shouldContinue &= ~0x1; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection2) - != TileElementGetCornerHeight(nextSurfaceElement, direction2)) - { - shouldContinue &= ~0x2; - } - auto nextLoc = loc; - while ((shouldContinue & 0x3) != 0) - { - shouldContinue = ((shouldContinue << 2) | 0x3) & shouldContinue; - nextLoc.x += stepX; - nextLoc.y += stepY; - // check if we need to continue after raising the current tile - // this needs to be checked before the tile is changed - if (!LocationValid({ nextLoc.x + stepX, nextLoc.y + stepY })) - { - shouldContinue &= ~0x3; + slope = LowerSurfaceCornerFlags(direction, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ -= 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } } else { - surfaceElement = nextSurfaceElement; - nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ nextLoc.x + stepX, nextLoc.y + stepY }); - if (nextSurfaceElement == nullptr) + slope = RaiseSurfaceCornerFlags(direction, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ += 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } + } + + auto landSetHeightAction = LandSetHeightAction(loc, targetBaseZ, slope); + landSetHeightAction.SetFlags(GetFlags()); + auto res = isExecuting ? ExecuteNested(&landSetHeightAction) : QueryNested(&landSetHeightAction); + + return res; + } + + money64 LandSmoothAction::SmoothLandRowByEdge( + bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight1, int32_t expectedLandHeight2, int32_t stepX, + int32_t stepY, int32_t direction1, int32_t direction2, int32_t checkDirection1, int32_t checkDirection2) const + { + uint8_t shouldContinue = 0xF; + int32_t landChangePerTile = _isLowering ? 2 : -2; + money64 totalCost = 0; + + // check if we need to start at all + if (!LocationValid(loc) || !LocationValid({ loc.x + stepX, loc.y + stepY })) + { + return 0; + } + auto surfaceElement = MapGetSurfaceElementAt(loc); + auto nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ loc.x + stepX, loc.y + stepY }); + if (surfaceElement == nullptr || nextSurfaceElement == nullptr) + { + return 0; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection1) != expectedLandHeight1 + landChangePerTile) + { + shouldContinue &= ~0x1; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection2) != expectedLandHeight2 + landChangePerTile) + { + shouldContinue &= ~0x2; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection1) + != TileElementGetCornerHeight(nextSurfaceElement, direction1)) + { + shouldContinue &= ~0x1; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection2) + != TileElementGetCornerHeight(nextSurfaceElement, direction2)) + { + shouldContinue &= ~0x2; + } + auto nextLoc = loc; + while ((shouldContinue & 0x3) != 0) + { + shouldContinue = ((shouldContinue << 2) | 0x3) & shouldContinue; + nextLoc.x += stepX; + nextLoc.y += stepY; + // check if we need to continue after raising the current tile + // this needs to be checked before the tile is changed + if (!LocationValid({ nextLoc.x + stepX, nextLoc.y + stepY })) { shouldContinue &= ~0x3; } - if (TileElementGetCornerHeight(surfaceElement, direction1) + landChangePerTile - != TileElementGetCornerHeight(surfaceElement, checkDirection1)) + else { - shouldContinue &= ~0x1; - } - if (TileElementGetCornerHeight(surfaceElement, direction2) + landChangePerTile - != TileElementGetCornerHeight(surfaceElement, checkDirection2)) - { - shouldContinue &= ~0x2; - } - if ((shouldContinue & 0x1) - && TileElementGetCornerHeight(surfaceElement, checkDirection1) - != TileElementGetCornerHeight(nextSurfaceElement, direction1)) - { - shouldContinue &= ~0x1; - } - if ((shouldContinue & 0x2) - && TileElementGetCornerHeight(surfaceElement, checkDirection2) - != TileElementGetCornerHeight(nextSurfaceElement, direction2)) - { - shouldContinue &= ~0x2; - } - } - expectedLandHeight1 += landChangePerTile; - - // change land of current tile - int32_t targetBaseZ = surfaceElement->BaseHeight; - int32_t slope = surfaceElement->GetSlope(); - int32_t oldSlope = slope; - if (_isLowering) - { - if (shouldContinue & 0x4) - { - slope = LowerSurfaceCornerFlags(direction1, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) + surfaceElement = nextSurfaceElement; + nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ nextLoc.x + stepX, nextLoc.y + stepY }); + if (nextSurfaceElement == nullptr) { - targetBaseZ -= 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + shouldContinue &= ~0x3; + } + if (TileElementGetCornerHeight(surfaceElement, direction1) + landChangePerTile + != TileElementGetCornerHeight(surfaceElement, checkDirection1)) + { + shouldContinue &= ~0x1; + } + if (TileElementGetCornerHeight(surfaceElement, direction2) + landChangePerTile + != TileElementGetCornerHeight(surfaceElement, checkDirection2)) + { + shouldContinue &= ~0x2; + } + if ((shouldContinue & 0x1) + && TileElementGetCornerHeight(surfaceElement, checkDirection1) + != TileElementGetCornerHeight(nextSurfaceElement, direction1)) + { + shouldContinue &= ~0x1; + } + if ((shouldContinue & 0x2) + && TileElementGetCornerHeight(surfaceElement, checkDirection2) + != TileElementGetCornerHeight(nextSurfaceElement, direction2)) + { + shouldContinue &= ~0x2; } } - if ((shouldContinue & 0x8) - && MapGetCornerHeight(surfaceElement->BaseHeight, oldSlope, direction2) - == MapGetCornerHeight(targetBaseZ, slope, direction2)) + expectedLandHeight1 += landChangePerTile; + + // change land of current tile + int32_t targetBaseZ = surfaceElement->BaseHeight; + int32_t slope = surfaceElement->GetSlope(); + int32_t oldSlope = slope; + if (_isLowering) { - slope = LowerSurfaceCornerFlags(direction2, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) + if (shouldContinue & 0x4) { - targetBaseZ -= 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + slope = LowerSurfaceCornerFlags(direction1, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ -= 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } } - } - } - else - { - if (shouldContinue & 0x4) - { - slope = RaiseSurfaceCornerFlags(direction1, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) + if ((shouldContinue & 0x8) + && MapGetCornerHeight(surfaceElement->BaseHeight, oldSlope, direction2) + == MapGetCornerHeight(targetBaseZ, slope, direction2)) { - targetBaseZ += 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + slope = LowerSurfaceCornerFlags(direction2, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ -= 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } } } - if ((shouldContinue & 0x8) - && MapGetCornerHeight(surfaceElement->BaseHeight, oldSlope, direction2) - == MapGetCornerHeight(targetBaseZ, slope, direction2)) - { - slope = RaiseSurfaceCornerFlags(direction2, slope); - if (slope & kTileSlopeRaiseOrLowerBaseHeight) - { - targetBaseZ += 2; - slope &= ~kTileSlopeRaiseOrLowerBaseHeight; - } - } - } - auto landSetHeightAction = LandSetHeightAction(nextLoc, targetBaseZ, slope); - landSetHeightAction.SetFlags(GetFlags()); - auto res = isExecuting ? GameActions::ExecuteNested(&landSetHeightAction) - : GameActions::QueryNested(&landSetHeightAction); - if (res.Error == GameActions::Status::Ok) - { - totalCost += res.Cost; - } - } - return totalCost; -} - -money64 LandSmoothAction::SmoothLandRowByCorner( - bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight, int32_t stepX, int32_t stepY, int32_t direction, - int32_t checkDirection) const -{ - bool shouldContinue = true; - money64 totalCost = 0; - int32_t landChangePerTile; - if (stepX == 0 || stepY == 0) - { - landChangePerTile = _isLowering ? 2 : -2; - } - else - { - landChangePerTile = _isLowering ? 4 : -4; - } - - // check if we need to start at all - if (!LocationValid(loc) || !LocationValid({ loc.x + stepX, loc.y + stepY })) - { - return 0; - } - auto surfaceElement = MapGetSurfaceElementAt(loc); - auto nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ loc.x + stepX, loc.y + stepY }); - if (surfaceElement == nullptr || nextSurfaceElement == nullptr) - { - return 0; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection) != expectedLandHeight + (_isLowering ? 2 : -2)) - { - return 0; - } - if (TileElementGetCornerHeight(surfaceElement, checkDirection) != TileElementGetCornerHeight(nextSurfaceElement, direction)) - { - return 0; - } - - auto nextLoc = loc; - while (shouldContinue) - { - nextLoc.x += stepX; - nextLoc.y += stepY; - // check if we need to continue after raising the current tile - // this needs to be checked before the tile is changed - if (!LocationValid({ nextLoc.x + stepX, nextLoc.y + stepY })) - { - shouldContinue = false; - } - else - { - surfaceElement = nextSurfaceElement; - nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ nextLoc.x + stepX, nextLoc.y + stepY }); - if (nextSurfaceElement == nullptr) - { - shouldContinue = false; - } - if (TileElementGetCornerHeight(surfaceElement, direction) + landChangePerTile - != TileElementGetCornerHeight(surfaceElement, checkDirection)) - { - shouldContinue = false; - } - if (shouldContinue - && TileElementGetCornerHeight(surfaceElement, checkDirection) - != TileElementGetCornerHeight(nextSurfaceElement, direction)) - { - shouldContinue = false; - } - } - if (stepX * stepY != 0) - { - totalCost += SmoothLandRowByCorner( - isExecuting, nextLoc, expectedLandHeight + (landChangePerTile / 2), 0, stepY, direction, checkDirection ^ 3); - totalCost += SmoothLandRowByCorner( - isExecuting, nextLoc, expectedLandHeight + (landChangePerTile / 2), stepX, 0, direction, checkDirection ^ 1); - } - expectedLandHeight += landChangePerTile; - // change land of current tile - auto result = SmoothLandTile(direction, isExecuting, nextLoc, surfaceElement); - if (result.Error == GameActions::Status::Ok) - { - totalCost += result.Cost; - } - } - return totalCost; -} - -GameActions::Result LandSmoothAction::SmoothLand(bool isExecuting) const -{ - const bool raiseLand = !_isLowering; - const int32_t selectionType = _selectionType; - const int32_t heightOffset = raiseLand ? 2 : -2; - - auto normRange = _range.Normalise(); - // Cap bounds to map - auto l = std::max(normRange.GetLeft(), 32); - auto t = std::max(normRange.GetTop(), 32); - auto r = std::clamp(normRange.GetRight(), 0, kMaximumTileStartXY); - auto b = std::clamp(normRange.GetBottom(), 0, kMaximumTileStartXY); - auto validRange = MapRange{ l, t, r, b }; - - int32_t centreZ = TileElementHeight(_coords); - - auto res = GameActions::Result(); - res.ErrorTitle = kErrorTitles[_isLowering ? 0 : 1]; - res.Expenditure = ExpenditureType::landscaping; - res.Position = { _coords.x, _coords.y, centreZ }; - - // Do the smoothing - switch (selectionType) - { - case MAP_SELECT_TYPE_FULL: - { - uint8_t minHeight = heightOffset + MapGetLowestLandHeight(validRange); - uint8_t maxHeight = heightOffset + MapGetHighestLandHeight(validRange); - - // Smooth the 4 corners - { // top-left - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); - if (surfaceElement != nullptr) - { - int32_t z = std::clamp( - static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); - } - } - { // bottom-left - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetBottom() }); - if (surfaceElement != nullptr) - { - int32_t z = std::clamp( - static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetBottom() }, z, -32, 32, 1, 3); - } - } - { // bottom-right - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), validRange.GetBottom() }); - if (surfaceElement != nullptr) - { - int32_t z = std::clamp( - static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetRight(), validRange.GetBottom() }, z, 32, 32, 2, 0); - } - } - { // top-right - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), validRange.GetTop() }); - if (surfaceElement != nullptr) - { - int32_t z = std::clamp( - static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetRight(), validRange.GetTop() }, z, 32, -32, 3, 1); - } - } - - // Smooth the edges - int32_t z1, z2; - for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) - { - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), y }); - if (surfaceElement != nullptr) - { - z1 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); - z2 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); - res.Cost += SmoothLandRowByEdge(isExecuting, { validRange.GetLeft(), y }, z1, z2, -32, 0, 0, 1, 3, 2); - } - - surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), y }); - if (surfaceElement != nullptr) - { - z1 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); - z2 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); - res.Cost += SmoothLandRowByEdge(isExecuting, { validRange.GetRight(), y }, z1, z2, 32, 0, 2, 3, 1, 0); - } - } - - for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) - { - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, validRange.GetTop() }); - if (surfaceElement != nullptr) - { - z1 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); - z2 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); - res.Cost += SmoothLandRowByEdge(isExecuting, { x, validRange.GetTop() }, z1, z2, 0, -32, 0, 3, 1, 2); - } - - surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, validRange.GetBottom() }); - if (surfaceElement != nullptr) - { - z1 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); - z2 = std::clamp(static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); - res.Cost += SmoothLandRowByEdge(isExecuting, { x, validRange.GetBottom() }, z1, z2, 0, 32, 1, 2, 0, 3); - } - } - break; - } - case MAP_SELECT_TYPE_CORNER_0: - case MAP_SELECT_TYPE_CORNER_1: - case MAP_SELECT_TYPE_CORNER_2: - case MAP_SELECT_TYPE_CORNER_3: - { - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); - if (surfaceElement == nullptr) - break; - uint8_t newBaseZ = surfaceElement->BaseHeight; - uint8_t newSlope = surfaceElement->GetSlope(); - - if (raiseLand) - { - newSlope = RaiseSurfaceCornerFlags(selectionType, newSlope); - } else { - newSlope = LowerSurfaceCornerFlags(selectionType, newSlope); + if (shouldContinue & 0x4) + { + slope = RaiseSurfaceCornerFlags(direction1, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ += 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } + } + if ((shouldContinue & 0x8) + && MapGetCornerHeight(surfaceElement->BaseHeight, oldSlope, direction2) + == MapGetCornerHeight(targetBaseZ, slope, direction2)) + { + slope = RaiseSurfaceCornerFlags(direction2, slope); + if (slope & kTileSlopeRaiseOrLowerBaseHeight) + { + targetBaseZ += 2; + slope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } + } } - - if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) + auto landSetHeightAction = LandSetHeightAction(nextLoc, targetBaseZ, slope); + landSetHeightAction.SetFlags(GetFlags()); + auto res = isExecuting ? ExecuteNested(&landSetHeightAction) : QueryNested(&landSetHeightAction); + if (res.Error == Status::Ok) { - newBaseZ += heightOffset; - newSlope &= ~kTileSlopeRaiseOrLowerBaseHeight; + totalCost += res.Cost; } - - // Smooth the corners - int32_t z = MapGetCornerHeight(newBaseZ, newSlope, 2); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); - z = MapGetCornerHeight(newBaseZ, newSlope, 0); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 32, 2, 0); - z = MapGetCornerHeight(newBaseZ, newSlope, 3); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 32, 1, 3); - z = MapGetCornerHeight(newBaseZ, newSlope, 1); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, -32, 3, 1); - - // Smooth the edges - switch (selectionType) - { - case MAP_SELECT_TYPE_CORNER_0: - z = MapGetCornerHeight(newBaseZ, newSlope, 0); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 3, 0); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 1, 0); - z = MapGetCornerHeight(newBaseZ, newSlope, 3); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 0, 3); - z = MapGetCornerHeight(newBaseZ, newSlope, 1); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 0, 1); - break; - case MAP_SELECT_TYPE_CORNER_1: - z = MapGetCornerHeight(newBaseZ, newSlope, 1); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 2, 1); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 0, 1); - z = MapGetCornerHeight(newBaseZ, newSlope, 2); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 1, 2); - z = MapGetCornerHeight(newBaseZ, newSlope, 0); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 1, 0); - break; - case MAP_SELECT_TYPE_CORNER_2: - z = MapGetCornerHeight(newBaseZ, newSlope, 2); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 1, 2); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 3, 2); - z = MapGetCornerHeight(newBaseZ, newSlope, 1); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 2, 1); - z = MapGetCornerHeight(newBaseZ, newSlope, 3); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 2, 3); - break; - case MAP_SELECT_TYPE_CORNER_3: - z = MapGetCornerHeight(newBaseZ, newSlope, 3); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 0, 3); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 2, 3); - z = MapGetCornerHeight(newBaseZ, newSlope, 0); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 3, 0); - z = MapGetCornerHeight(newBaseZ, newSlope, 2); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 3, 2); - break; - } - break; } - case MAP_SELECT_TYPE_EDGE_0: - case MAP_SELECT_TYPE_EDGE_1: - case MAP_SELECT_TYPE_EDGE_2: - case MAP_SELECT_TYPE_EDGE_3: + return totalCost; + } + + money64 LandSmoothAction::SmoothLandRowByCorner( + bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight, int32_t stepX, int32_t stepY, int32_t direction, + int32_t checkDirection) const + { + bool shouldContinue = true; + money64 totalCost = 0; + int32_t landChangePerTile; + if (stepX == 0 || stepY == 0) { - // TODO: Handle smoothing by edge - // Get the two corners to raise - auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); - if (surfaceElement == nullptr) - break; - uint8_t newBaseZ = surfaceElement->BaseHeight; - uint8_t oldSlope = surfaceElement->GetSlope(); - int32_t rowIndex = selectionType - (MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1); - uint8_t newSlope = raiseLand ? RaiseSurfaceCornerFlags(rowIndex, oldSlope) - : LowerSurfaceCornerFlags(rowIndex, oldSlope); - - const bool changeBaseHeight = newSlope & kTileSlopeRaiseOrLowerBaseHeight; - if (changeBaseHeight) - { - newBaseZ += heightOffset; - newSlope &= ~kTileSlopeRaiseOrLowerBaseHeight; - } - - const uint8_t edge = selectionType - MAP_SELECT_TYPE_EDGE_0; - - // Table with corners for each edge selection. The first two are the selected corners, the latter - // two are the opposites - static constexpr uint8_t cornerIndices[][4] = { - { 2, 3, 1, 0 }, // MAP_SELECT_TYPE_EDGE_0 - { 3, 0, 2, 1 }, // MAP_SELECT_TYPE_EDGE_1 - { 0, 1, 3, 2 }, // MAP_SELECT_TYPE_EDGE_2 - { 1, 2, 0, 3 }, // MAP_SELECT_TYPE_EDGE_3 - }; - // Big coordinate offsets for the neighbouring tile for the given edge selection - static constexpr CoordsXY stepOffsets[] = { - { -32, 0 }, - { 0, 32 }, - { 32, 0 }, - { 0, -32 }, - }; - - // Smooth higher and lower edges - uint8_t c1 = cornerIndices[edge][0]; - uint8_t c2 = cornerIndices[edge][1]; - uint8_t c3 = cornerIndices[edge][2]; - uint8_t c4 = cornerIndices[edge][3]; - uint8_t z1 = MapGetCornerHeight(newBaseZ, newSlope, c1); - uint8_t z2 = MapGetCornerHeight(newBaseZ, newSlope, c2); - uint8_t z3 = MapGetCornerHeight(newBaseZ, newSlope, c3); - uint8_t z4 = MapGetCornerHeight(newBaseZ, newSlope, c4); - // Smooth the edge at the top of the new slope - res.Cost += SmoothLandRowByEdge( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z1, z2, stepOffsets[edge].x, stepOffsets[edge].y, - c3, c4, c1, c2); - // Smooth the edge at the bottom of the new slope - res.Cost += SmoothLandRowByEdge( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z3, z4, -stepOffsets[edge].x, -stepOffsets[edge].y, - c1, c2, c3, c4); - - // Smooth corners - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z1, -stepOffsets[edge].y, stepOffsets[edge].x, c2, - c1); - res.Cost += SmoothLandRowByCorner( - isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z2, stepOffsets[edge].y, -stepOffsets[edge].x, c1, - c2); - int32_t z = MapGetCornerHeight(newBaseZ, newSlope, 2); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); - z = MapGetCornerHeight(newBaseZ, newSlope, 0); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 32, 2, 0); - z = MapGetCornerHeight(newBaseZ, newSlope, 3); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 32, 1, 3); - z = MapGetCornerHeight(newBaseZ, newSlope, 1); - res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, -32, 3, 1); - break; + landChangePerTile = _isLowering ? 2 : -2; + } + else + { + landChangePerTile = _isLowering ? 4 : -4; } - default: - LOG_ERROR("Invalid map selection type %u", _selectionType); - return GameActions::Result( - GameActions::Status::InvalidParameters, std::get(res.ErrorTitle), STR_ERR_VALUE_OUT_OF_RANGE); - } // switch selectionType - // Raise / lower the land tool selection area - GameActions::Result result; - if (raiseLand) - { - auto raiseLandAction = LandRaiseAction({ _coords.x, _coords.y }, validRange, selectionType); - raiseLandAction.SetFlags(GetFlags()); - result = isExecuting ? GameActions::ExecuteNested(&raiseLandAction) : GameActions::QueryNested(&raiseLandAction); - } - else - { - auto lowerLandAction = LandLowerAction({ _coords.x, _coords.y }, validRange, selectionType); - lowerLandAction.SetFlags(GetFlags()); - result = isExecuting ? GameActions::ExecuteNested(&lowerLandAction) : GameActions::QueryNested(&lowerLandAction); - } - if (result.Error != GameActions::Status::Ok) - { - return result; + // check if we need to start at all + if (!LocationValid(loc) || !LocationValid({ loc.x + stepX, loc.y + stepY })) + { + return 0; + } + auto surfaceElement = MapGetSurfaceElementAt(loc); + auto nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ loc.x + stepX, loc.y + stepY }); + if (surfaceElement == nullptr || nextSurfaceElement == nullptr) + { + return 0; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection) != expectedLandHeight + (_isLowering ? 2 : -2)) + { + return 0; + } + if (TileElementGetCornerHeight(surfaceElement, checkDirection) + != TileElementGetCornerHeight(nextSurfaceElement, direction)) + { + return 0; + } + + auto nextLoc = loc; + while (shouldContinue) + { + nextLoc.x += stepX; + nextLoc.y += stepY; + // check if we need to continue after raising the current tile + // this needs to be checked before the tile is changed + if (!LocationValid({ nextLoc.x + stepX, nextLoc.y + stepY })) + { + shouldContinue = false; + } + else + { + surfaceElement = nextSurfaceElement; + nextSurfaceElement = MapGetSurfaceElementAt(CoordsXY{ nextLoc.x + stepX, nextLoc.y + stepY }); + if (nextSurfaceElement == nullptr) + { + shouldContinue = false; + } + if (TileElementGetCornerHeight(surfaceElement, direction) + landChangePerTile + != TileElementGetCornerHeight(surfaceElement, checkDirection)) + { + shouldContinue = false; + } + if (shouldContinue + && TileElementGetCornerHeight(surfaceElement, checkDirection) + != TileElementGetCornerHeight(nextSurfaceElement, direction)) + { + shouldContinue = false; + } + } + if (stepX * stepY != 0) + { + totalCost += SmoothLandRowByCorner( + isExecuting, nextLoc, expectedLandHeight + (landChangePerTile / 2), 0, stepY, direction, + checkDirection ^ 3); + totalCost += SmoothLandRowByCorner( + isExecuting, nextLoc, expectedLandHeight + (landChangePerTile / 2), stepX, 0, direction, + checkDirection ^ 1); + } + expectedLandHeight += landChangePerTile; + // change land of current tile + auto result = SmoothLandTile(direction, isExecuting, nextLoc, surfaceElement); + if (result.Error == Status::Ok) + { + totalCost += result.Cost; + } + } + return totalCost; } - if (isExecuting) + Result LandSmoothAction::SmoothLand(bool isExecuting) const { - OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, { _coords.x, _coords.y, centreZ }); + const bool raiseLand = !_isLowering; + const int32_t selectionType = _selectionType; + const int32_t heightOffset = raiseLand ? 2 : -2; + + auto normRange = _range.Normalise(); + // Cap bounds to map + auto l = std::max(normRange.GetLeft(), 32); + auto t = std::max(normRange.GetTop(), 32); + auto r = std::clamp(normRange.GetRight(), 0, kMaximumTileStartXY); + auto b = std::clamp(normRange.GetBottom(), 0, kMaximumTileStartXY); + auto validRange = MapRange{ l, t, r, b }; + + int32_t centreZ = TileElementHeight(_coords); + + auto res = Result(); + res.ErrorTitle = kErrorTitles[_isLowering ? 0 : 1]; + res.Expenditure = ExpenditureType::landscaping; + res.Position = { _coords.x, _coords.y, centreZ }; + + // Do the smoothing + switch (selectionType) + { + case MAP_SELECT_TYPE_FULL: + { + uint8_t minHeight = heightOffset + MapGetLowestLandHeight(validRange); + uint8_t maxHeight = heightOffset + MapGetHighestLandHeight(validRange); + + // Smooth the 4 corners + { // top-left + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); + if (surfaceElement != nullptr) + { + int32_t z = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); + } + } + { // bottom-left + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetBottom() }); + if (surfaceElement != nullptr) + { + int32_t z = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetBottom() }, z, -32, 32, 1, 3); + } + } + { // bottom-right + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), validRange.GetBottom() }); + if (surfaceElement != nullptr) + { + int32_t z = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetRight(), validRange.GetBottom() }, z, 32, 32, 2, 0); + } + } + { // top-right + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), validRange.GetTop() }); + if (surfaceElement != nullptr) + { + int32_t z = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetRight(), validRange.GetTop() }, z, 32, -32, 3, 1); + } + } + + // Smooth the edges + int32_t z1, z2; + for (int32_t y = validRange.GetTop(); y <= validRange.GetBottom(); y += kCoordsXYStep) + { + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), y }); + if (surfaceElement != nullptr) + { + z1 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); + z2 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); + res.Cost += SmoothLandRowByEdge(isExecuting, { validRange.GetLeft(), y }, z1, z2, -32, 0, 0, 1, 3, 2); + } + + surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetRight(), y }); + if (surfaceElement != nullptr) + { + z1 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); + z2 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); + res.Cost += SmoothLandRowByEdge(isExecuting, { validRange.GetRight(), y }, z1, z2, 32, 0, 2, 3, 1, 0); + } + } + + for (int32_t x = validRange.GetLeft(); x <= validRange.GetRight(); x += kCoordsXYStep) + { + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, validRange.GetTop() }); + if (surfaceElement != nullptr) + { + z1 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 1)), minHeight, maxHeight); + z2 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 2)), minHeight, maxHeight); + res.Cost += SmoothLandRowByEdge(isExecuting, { x, validRange.GetTop() }, z1, z2, 0, -32, 0, 3, 1, 2); + } + + surfaceElement = MapGetSurfaceElementAt(CoordsXY{ x, validRange.GetBottom() }); + if (surfaceElement != nullptr) + { + z1 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 0)), minHeight, maxHeight); + z2 = std::clamp( + static_cast(TileElementGetCornerHeight(surfaceElement, 3)), minHeight, maxHeight); + res.Cost += SmoothLandRowByEdge(isExecuting, { x, validRange.GetBottom() }, z1, z2, 0, 32, 1, 2, 0, 3); + } + } + break; + } + case MAP_SELECT_TYPE_CORNER_0: + case MAP_SELECT_TYPE_CORNER_1: + case MAP_SELECT_TYPE_CORNER_2: + case MAP_SELECT_TYPE_CORNER_3: + { + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); + if (surfaceElement == nullptr) + break; + uint8_t newBaseZ = surfaceElement->BaseHeight; + uint8_t newSlope = surfaceElement->GetSlope(); + + if (raiseLand) + { + newSlope = RaiseSurfaceCornerFlags(selectionType, newSlope); + } + else + { + newSlope = LowerSurfaceCornerFlags(selectionType, newSlope); + } + + if (newSlope & kTileSlopeRaiseOrLowerBaseHeight) + { + newBaseZ += heightOffset; + newSlope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } + + // Smooth the corners + int32_t z = MapGetCornerHeight(newBaseZ, newSlope, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); + z = MapGetCornerHeight(newBaseZ, newSlope, 0); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 32, 2, 0); + z = MapGetCornerHeight(newBaseZ, newSlope, 3); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 32, 1, 3); + z = MapGetCornerHeight(newBaseZ, newSlope, 1); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, -32, 3, 1); + + // Smooth the edges + switch (selectionType) + { + case MAP_SELECT_TYPE_CORNER_0: + z = MapGetCornerHeight(newBaseZ, newSlope, 0); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 3, 0); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 1, 0); + z = MapGetCornerHeight(newBaseZ, newSlope, 3); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 0, 3); + z = MapGetCornerHeight(newBaseZ, newSlope, 1); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 0, 1); + break; + case MAP_SELECT_TYPE_CORNER_1: + z = MapGetCornerHeight(newBaseZ, newSlope, 1); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 2, 1); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 0, 1); + z = MapGetCornerHeight(newBaseZ, newSlope, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 1, 2); + z = MapGetCornerHeight(newBaseZ, newSlope, 0); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 1, 0); + break; + case MAP_SELECT_TYPE_CORNER_2: + z = MapGetCornerHeight(newBaseZ, newSlope, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 1, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 3, 2); + z = MapGetCornerHeight(newBaseZ, newSlope, 1); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 2, 1); + z = MapGetCornerHeight(newBaseZ, newSlope, 3); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 2, 3); + break; + case MAP_SELECT_TYPE_CORNER_3: + z = MapGetCornerHeight(newBaseZ, newSlope, 3); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 0, 0, 3); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, 32, 2, 3); + z = MapGetCornerHeight(newBaseZ, newSlope, 0); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 0, 3, 0); + z = MapGetCornerHeight(newBaseZ, newSlope, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 0, -32, 3, 2); + break; + } + break; + } + case MAP_SELECT_TYPE_EDGE_0: + case MAP_SELECT_TYPE_EDGE_1: + case MAP_SELECT_TYPE_EDGE_2: + case MAP_SELECT_TYPE_EDGE_3: + { + // TODO: Handle smoothing by edge + // Get the two corners to raise + auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ validRange.GetLeft(), validRange.GetTop() }); + if (surfaceElement == nullptr) + break; + uint8_t newBaseZ = surfaceElement->BaseHeight; + uint8_t oldSlope = surfaceElement->GetSlope(); + int32_t rowIndex = selectionType - (MAP_SELECT_TYPE_EDGE_0 - MAP_SELECT_TYPE_FULL - 1); + uint8_t newSlope = raiseLand ? RaiseSurfaceCornerFlags(rowIndex, oldSlope) + : LowerSurfaceCornerFlags(rowIndex, oldSlope); + + const bool changeBaseHeight = newSlope & kTileSlopeRaiseOrLowerBaseHeight; + if (changeBaseHeight) + { + newBaseZ += heightOffset; + newSlope &= ~kTileSlopeRaiseOrLowerBaseHeight; + } + + const uint8_t edge = selectionType - MAP_SELECT_TYPE_EDGE_0; + + // Table with corners for each edge selection. The first two are the selected corners, the latter + // two are the opposites + static constexpr uint8_t cornerIndices[][4] = { + { 2, 3, 1, 0 }, // MAP_SELECT_TYPE_EDGE_0 + { 3, 0, 2, 1 }, // MAP_SELECT_TYPE_EDGE_1 + { 0, 1, 3, 2 }, // MAP_SELECT_TYPE_EDGE_2 + { 1, 2, 0, 3 }, // MAP_SELECT_TYPE_EDGE_3 + }; + // Big coordinate offsets for the neighbouring tile for the given edge selection + static constexpr CoordsXY stepOffsets[] = { + { -32, 0 }, + { 0, 32 }, + { 32, 0 }, + { 0, -32 }, + }; + + // Smooth higher and lower edges + uint8_t c1 = cornerIndices[edge][0]; + uint8_t c2 = cornerIndices[edge][1]; + uint8_t c3 = cornerIndices[edge][2]; + uint8_t c4 = cornerIndices[edge][3]; + uint8_t z1 = MapGetCornerHeight(newBaseZ, newSlope, c1); + uint8_t z2 = MapGetCornerHeight(newBaseZ, newSlope, c2); + uint8_t z3 = MapGetCornerHeight(newBaseZ, newSlope, c3); + uint8_t z4 = MapGetCornerHeight(newBaseZ, newSlope, c4); + // Smooth the edge at the top of the new slope + res.Cost += SmoothLandRowByEdge( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z1, z2, stepOffsets[edge].x, + stepOffsets[edge].y, c3, c4, c1, c2); + // Smooth the edge at the bottom of the new slope + res.Cost += SmoothLandRowByEdge( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z3, z4, -stepOffsets[edge].x, + -stepOffsets[edge].y, c1, c2, c3, c4); + + // Smooth corners + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z1, -stepOffsets[edge].y, stepOffsets[edge].x, + c2, c1); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z2, stepOffsets[edge].y, -stepOffsets[edge].x, + c1, c2); + int32_t z = MapGetCornerHeight(newBaseZ, newSlope, 2); + res.Cost += SmoothLandRowByCorner( + isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, -32, 0, 2); + z = MapGetCornerHeight(newBaseZ, newSlope, 0); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, 32, 2, 0); + z = MapGetCornerHeight(newBaseZ, newSlope, 3); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, -32, 32, 1, 3); + z = MapGetCornerHeight(newBaseZ, newSlope, 1); + res.Cost += SmoothLandRowByCorner(isExecuting, { validRange.GetLeft(), validRange.GetTop() }, z, 32, -32, 3, 1); + break; + } + default: + LOG_ERROR("Invalid map selection type %u", _selectionType); + return Result(Status::InvalidParameters, std::get(res.ErrorTitle), STR_ERR_VALUE_OUT_OF_RANGE); + } // switch selectionType + + // Raise / lower the land tool selection area + Result result; + if (raiseLand) + { + auto raiseLandAction = LandRaiseAction({ _coords.x, _coords.y }, validRange, selectionType); + raiseLandAction.SetFlags(GetFlags()); + result = isExecuting ? ExecuteNested(&raiseLandAction) : QueryNested(&raiseLandAction); + } + else + { + auto lowerLandAction = LandLowerAction({ _coords.x, _coords.y }, validRange, selectionType); + lowerLandAction.SetFlags(GetFlags()); + result = isExecuting ? ExecuteNested(&lowerLandAction) : QueryNested(&lowerLandAction); + } + if (result.Error != Status::Ok) + { + return result; + } + + if (isExecuting) + { + Audio::Play3D(Audio::SoundId::PlaceItem, { _coords.x, _coords.y, centreZ }); + } + res.Cost += result.Cost; + return res; } - res.Cost += result.Cost; - return res; -} +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/actions/LandSmoothAction.h b/src/openrct2/actions/LandSmoothAction.h index 2af68946cf..9889a63f41 100644 --- a/src/openrct2/actions/LandSmoothAction.h +++ b/src/openrct2/actions/LandSmoothAction.h @@ -11,39 +11,41 @@ #include "GameAction.h" -class LandSmoothAction final : public GameActionBase +namespace OpenRCT2::GameActions { -private: - CoordsXY _coords; - MapRange _range; - uint8_t _selectionType{}; - bool _isLowering{}; + class LandSmoothAction final : public GameActionBase + { + private: + CoordsXY _coords; + MapRange _range; + uint8_t _selectionType{}; + bool _isLowering{}; - constexpr static StringId kErrorTitles[] = { - STR_CANT_LOWER_LAND_HERE, - STR_CANT_RAISE_LAND_HERE, + constexpr static StringId kErrorTitles[] = { + STR_CANT_LOWER_LAND_HERE, + STR_CANT_RAISE_LAND_HERE, + }; + + public: + LandSmoothAction() = default; + LandSmoothAction(const CoordsXY& coords, MapRange range, uint8_t selectionType, bool isLowering); + + void AcceptParameters(GameActionParameterVisitor& visitor) override; + + uint16_t GetActionFlags() const override; + + void Serialise(DataSerialiser& stream) override; + Result Query() const override; + Result Execute() const override; + + private: + Result SmoothLandTile(int32_t direction, bool isExecuting, const CoordsXY& loc, SurfaceElement* surfaceElement) const; + money64 SmoothLandRowByEdge( + bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight1, int32_t expectedLandHeight2, int32_t stepX, + int32_t stepY, int32_t direction1, int32_t direction2, int32_t checkDirection1, int32_t checkDirection2) const; + money64 SmoothLandRowByCorner( + bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight, int32_t stepX, int32_t stepY, int32_t direction, + int32_t checkDirection) const; + Result SmoothLand(bool isExecuting) const; }; - -public: - LandSmoothAction() = default; - LandSmoothAction(const CoordsXY& coords, MapRange range, uint8_t selectionType, bool isLowering); - - void AcceptParameters(GameActionParameterVisitor& visitor) override; - - uint16_t GetActionFlags() const override; - - void Serialise(DataSerialiser& stream) override; - OpenRCT2::GameActions::Result Query() const override; - OpenRCT2::GameActions::Result Execute() const override; - -private: - OpenRCT2::GameActions::Result SmoothLandTile( - int32_t direction, bool isExecuting, const CoordsXY& loc, OpenRCT2::SurfaceElement* surfaceElement) const; - money64 SmoothLandRowByEdge( - bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight1, int32_t expectedLandHeight2, int32_t stepX, - int32_t stepY, int32_t direction1, int32_t direction2, int32_t checkDirection1, int32_t checkDirection2) const; - money64 SmoothLandRowByCorner( - bool isExecuting, const CoordsXY& loc, int32_t expectedLandHeight, int32_t stepX, int32_t stepY, int32_t direction, - int32_t checkDirection) const; - OpenRCT2::GameActions::Result SmoothLand(bool isExecuting) const; -}; +} // namespace OpenRCT2::GameActions diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index e88ac0e7ca..5ee22f3a33 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -907,7 +907,7 @@ static void ConsoleCommandSet(InteractiveConsole& console, const arguments_t& ar } else if (varName == "game_speed" && InvalidArguments(&invalidArgs, int_valid[0])) { - ConsoleSetVariableAction(console, varName, int_val[0]); + ConsoleSetVariableAction(console, varName, int_val[0]); } else if (varName == "console_small_font" && InvalidArguments(&invalidArgs, int_valid[0])) { diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index e3725ec5ed..4cf92a1c91 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -1012,7 +1012,8 @@ static void FootpathFixOwnership(const CoordsXY& mapPos) ownership = OWNERSHIP_UNOWNED; } - auto landSetRightsAction = LandSetRightsAction(mapPos, LandSetRightSetting::SetOwnershipWithChecks, ownership); + auto landSetRightsAction = GameActions::LandSetRightsAction( + mapPos, GameActions::LandSetRightSetting::SetOwnershipWithChecks, ownership); landSetRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND); GameActions::Execute(&landSetRightsAction); }