diff --git a/src/openrct2-ui/interface/ViewportInteraction.cpp b/src/openrct2-ui/interface/ViewportInteraction.cpp index 5402b6fa34..e9444b2f85 100644 --- a/src/openrct2-ui/interface/ViewportInteraction.cpp +++ b/src/openrct2-ui/interface/ViewportInteraction.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include #include @@ -465,10 +467,11 @@ int32_t viewport_interaction_right_click(int32_t x, int32_t y) */ static void viewport_interaction_remove_scenery(TileElement* tileElement, int32_t x, int32_t y) { - gGameCommandErrorTitle = STR_CANT_REMOVE_THIS; - game_do_command( - x, (tileElement->AsSmallScenery()->GetSceneryQuadrant() << 8) | GAME_COMMAND_FLAG_APPLY, y, - (tileElement->AsSmallScenery()->GetEntryIndex() << 8) | tileElement->base_height, GAME_COMMAND_REMOVE_SCENERY, 0, 0); + auto removeSceneryAction = SmallSceneryRemoveAction( + x, y, tileElement->base_height, tileElement->AsSmallScenery()->GetSceneryQuadrant(), + tileElement->AsSmallScenery()->GetEntryIndex()); + + GameActions::Execute(&removeSceneryAction); } /** @@ -572,11 +575,9 @@ static void viewport_interaction_remove_large_scenery(TileElement* tileElement, } else { - gGameCommandErrorTitle = STR_CANT_REMOVE_THIS; - game_do_command( - x, 1 | (tileElement->GetDirection() << 8), y, - tileElement->base_height | (tileElement->AsLargeScenery()->GetSequenceIndex() << 8), - GAME_COMMAND_REMOVE_LARGE_SCENERY, 0, 0); + auto removeSceneryAction = LargeSceneryRemoveAction( + x, y, tileElement->base_height, tileElement->GetDirection(), tileElement->AsLargeScenery()->GetSequenceIndex()); + GameActions::Execute(&removeSceneryAction); } } diff --git a/src/openrct2-ui/windows/Sign.cpp b/src/openrct2-ui/windows/Sign.cpp index dede2f54c8..c8a9705e91 100644 --- a/src/openrct2-ui/windows/Sign.cpp +++ b/src/openrct2-ui/windows/Sign.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -214,6 +215,7 @@ static void window_sign_mouseup(rct_window* w, rct_widgetindex widgetIndex) window_close(w); break; case WIDX_SIGN_DEMOLISH: + { while (1) { if (tile_element->GetType() == TILE_ELEMENT_TYPE_LARGE_SCENERY) @@ -228,11 +230,14 @@ static void window_sign_mouseup(rct_window* w, rct_widgetindex widgetIndex) } tile_element++; } - game_do_command( - x, 1 | (tile_element->GetDirection() << 8), y, - tile_element->base_height | (tile_element->AsLargeScenery()->GetSequenceIndex() << 8), - GAME_COMMAND_REMOVE_LARGE_SCENERY, 0, 0); + + auto sceneryRemoveAction = LargeSceneryRemoveAction( + x, y, tile_element->base_height, tile_element->GetDirection(), + tile_element->AsLargeScenery()->GetSequenceIndex()); + GameActions::Execute(&sceneryRemoveAction); + break; + } case WIDX_SIGN_TEXT: if (banner->flags & BANNER_FLAG_LINKED_TO_RIDE) { diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index f11f255355..48a64c1115 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -292,6 +293,8 @@ static void toggle_water_window(rct_window* topToolbar, rct_widgetindex widgetIn static money32 selection_lower_land(uint8_t flags); static money32 selection_raise_land(uint8_t flags); +static ClearAction GetClearAction(); + static bool _menuDropdownIncludesTwitch; static uint8_t _unkF64F0E; static int16_t _unkF64F0A; @@ -1940,18 +1943,13 @@ static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y) if (!state_changed) return; - int32_t eax = gMapSelectPositionA.x; - int32_t ecx = gMapSelectPositionA.y; - int32_t edi = gMapSelectPositionB.x; - int32_t ebp = gMapSelectPositionB.y; - int32_t clear = (gClearSmallScenery << 0) | (gClearLargeScenery << 1) | (gClearFootpath << 2); - money32 cost = game_do_command(eax, 0, ecx, clear, GAME_COMMAND_CLEAR_SCENERY, edi, ebp); - + auto action = GetClearAction(); + auto result = GameActions::Query(&action); + auto cost = (result->Error == GA_ERROR::OK ? result->Cost : MONEY32_UNDEFINED); if (gClearSceneryCost != cost) { gClearSceneryCost = cost; window_invalidate_by_class(WC_CLEAR_SCENERY); - return; } } @@ -2815,16 +2813,12 @@ static void window_top_toolbar_tool_down(rct_window* w, rct_widgetindex widgetIn switch (widgetIndex) { case WIDX_CLEAR_SCENERY: - if (!(gMapSelectFlags & MAP_SELECT_FLAG_ENABLE)) - break; - - gGameCommandErrorTitle = STR_UNABLE_TO_REMOVE_ALL_SCENERY_FROM_HERE; - - game_do_command( - gMapSelectPositionA.x, 1, gMapSelectPositionA.y, - ((gClearSmallScenery ? 1 : 0) | (gClearLargeScenery ? 1 : 0) << 1 | (gClearFootpath ? 1 : 0) << 2), - GAME_COMMAND_CLEAR_SCENERY, gMapSelectPositionB.x, gMapSelectPositionB.y); - gCurrentToolId = TOOL_CROSSHAIR; + if (gMapSelectFlags & MAP_SELECT_FLAG_ENABLE) + { + auto action = GetClearAction(); + GameActions::Execute(&action); + gCurrentToolId = TOOL_CROSSHAIR; + } break; case WIDX_LAND: if (gMapSelectFlags & MAP_SELECT_FLAG_ENABLE) @@ -3008,19 +3002,12 @@ static void window_top_toolbar_tool_drag(rct_window* w, rct_widgetindex widgetIn switch (widgetIndex) { case WIDX_CLEAR_SCENERY: - if (window_find_by_class(WC_ERROR) != nullptr) - break; - - if (!(gMapSelectFlags & MAP_SELECT_FLAG_ENABLE)) - break; - - gGameCommandErrorTitle = STR_UNABLE_TO_REMOVE_ALL_SCENERY_FROM_HERE; - - game_do_command( - gMapSelectPositionA.x, 1, gMapSelectPositionA.y, - ((gClearSmallScenery ? 1 : 0) | (gClearLargeScenery ? 1 : 0) << 1 | (gClearFootpath ? 1 : 0) << 2), - GAME_COMMAND_CLEAR_SCENERY, gMapSelectPositionB.x, gMapSelectPositionB.y); - gCurrentToolId = TOOL_CROSSHAIR; + if (window_find_by_class(WC_ERROR) == nullptr && (gMapSelectFlags & MAP_SELECT_FLAG_ENABLE)) + { + auto action = GetClearAction(); + GameActions::Execute(&action); + gCurrentToolId = TOOL_CROSSHAIR; + } break; case WIDX_LAND: // Custom setting to only change land style instead of raising or lowering land @@ -3539,3 +3526,19 @@ bool water_tool_is_active() return false; return true; } + +static ClearAction GetClearAction() +{ + auto range = MapRange(gMapSelectPositionA.x, gMapSelectPositionA.y, gMapSelectPositionB.x, gMapSelectPositionB.y); + + ClearableItems itemsToClear = 0; + + if (gClearSmallScenery) + itemsToClear |= CLEARABLE_ITEMS::SCENERY_SMALL; + if (gClearLargeScenery) + itemsToClear |= CLEARABLE_ITEMS::SCENERY_LARGE; + if (gClearFootpath) + itemsToClear |= CLEARABLE_ITEMS::SCENERY_FOOTPATH; + + return ClearAction(range, itemsToClear); +} diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 57429537c2..97574ac59c 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -1469,7 +1469,7 @@ GAME_COMMAND_POINTER* new_game_command_table[GAME_COMMAND_COUNT] = { game_command_set_ride_setting, game_command_place_ride_entrance_or_exit, game_command_remove_ride_entrance_or_exit, - game_command_remove_scenery, + nullptr, game_command_place_scenery, game_command_set_water_height, game_command_place_footpath, @@ -1500,7 +1500,7 @@ GAME_COMMAND_POINTER* new_game_command_table[GAME_COMMAND_COUNT] = { game_command_place_wall, nullptr, game_command_place_large_scenery, - game_command_remove_large_scenery, + nullptr, nullptr, nullptr, game_command_place_track_design, @@ -1513,7 +1513,7 @@ GAME_COMMAND_POINTER* new_game_command_table[GAME_COMMAND_COUNT] = { game_command_set_large_scenery_colour, game_command_set_banner_colour, game_command_set_land_ownership, - game_command_clear_scenery, + nullptr, nullptr, nullptr, game_command_set_banner_style, diff --git a/src/openrct2/Game.h b/src/openrct2/Game.h index 000790d790..a5026ee75c 100644 --- a/src/openrct2/Game.h +++ b/src/openrct2/Game.h @@ -32,7 +32,7 @@ enum GAME_COMMAND GAME_COMMAND_SET_RIDE_SETTING, GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, GAME_COMMAND_REMOVE_RIDE_ENTRANCE_OR_EXIT, - GAME_COMMAND_REMOVE_SCENERY, + GAME_COMMAND_REMOVE_SCENERY, // GA GAME_COMMAND_PLACE_SCENERY, GAME_COMMAND_SET_WATER_HEIGHT, GAME_COMMAND_PLACE_PATH, @@ -63,7 +63,7 @@ enum GAME_COMMAND GAME_COMMAND_PLACE_WALL, GAME_COMMAND_REMOVE_WALL, // GA GAME_COMMAND_PLACE_LARGE_SCENERY, - GAME_COMMAND_REMOVE_LARGE_SCENERY, + GAME_COMMAND_REMOVE_LARGE_SCENERY, // GA GAME_COMMAND_SET_CURRENT_LOAN, // GA GAME_COMMAND_SET_RESEARCH_FUNDING, // GA GAME_COMMAND_PLACE_TRACK_DESIGN, @@ -76,7 +76,7 @@ enum GAME_COMMAND GAME_COMMAND_SET_LARGE_SCENERY_COLOUR, GAME_COMMAND_SET_BANNER_COLOUR, GAME_COMMAND_SET_LAND_OWNERSHIP, - GAME_COMMAND_CLEAR_SCENERY, + GAME_COMMAND_CLEAR_SCENERY, // GA GAME_COMMAND_SET_BANNER_NAME, // GA GAME_COMMAND_SET_SIGN_NAME, // GA GAME_COMMAND_SET_BANNER_STYLE, diff --git a/src/openrct2/actions/ClearAction.hpp b/src/openrct2/actions/ClearAction.hpp new file mode 100644 index 0000000000..ba5e28ddee --- /dev/null +++ b/src/openrct2/actions/ClearAction.hpp @@ -0,0 +1,247 @@ +/***************************************************************************** + * Copyright (c) 2014-2019 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "../Context.h" +#include "../core/MemoryStream.h" +#include "../drawing/Drawing.h" +#include "../localisation/StringIds.h" +#include "../management/Finance.h" +#include "../world/LargeScenery.h" +#include "../world/Location.hpp" +#include "../world/Map.h" +#include "FootpathRemoveAction.hpp" +#include "GameAction.h" +#include "LargeSceneryRemoveAction.hpp" +#include "SmallSceneryRemoveAction.hpp" +#include "WallRemoveAction.hpp" + +#include + +using namespace OpenRCT2; + +using ClearableItems = uint8_t; + +namespace CLEARABLE_ITEMS +{ + constexpr ClearableItems SCENERY_SMALL = 1 << 0; + constexpr ClearableItems SCENERY_LARGE = 1 << 1; + constexpr ClearableItems SCENERY_FOOTPATH = 1 << 2; +} // namespace CLEARABLE_ITEMS + +DEFINE_GAME_ACTION(ClearAction, GAME_COMMAND_CLEAR_SCENERY, GameActionResult) +{ +private: + MapRange _range; + ClearableItems _itemsToClear; + +public: + ClearAction() + { + } + ClearAction(MapRange range, ClearableItems itemsToClear) + : _range(range) + , _itemsToClear(itemsToClear) + { + } + + void Serialise(DataSerialiser & stream) override + { + GameAction::Serialise(stream); + + stream << DS_TAG(_range) << DS_TAG(_itemsToClear); + } + + GameActionResult::Ptr Query() const override + { + return QueryExecute(false); + } + + GameActionResult::Ptr Execute() const override + { + return QueryExecute(true); + } + +private: + GameActionResult::Ptr CreateResult() const + { + auto result = MakeResult(); + result->ErrorTitle = STR_UNABLE_TO_REMOVE_ALL_SCENERY_FROM_HERE; + result->ExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; + + auto x = (_range.GetLeft() + _range.GetRight()) / 2 + 16; + auto y = (_range.GetTop() + _range.GetBottom()) / 2 + 16; + auto z = tile_element_height(x, y); + result->Position = CoordsXYZ(x, y, z); + + return result; + } + + GameActionResult::Ptr QueryExecute(bool executing) const + { + auto result = CreateResult(); + + auto noValidTiles = true; + auto error = GA_ERROR::OK; + rct_string_id errorMessage = STR_NONE; + money32 totalCost = 0; + + auto x0 = std::max(_range.GetLeft(), 32); + auto y0 = std::max(_range.GetTop(), 32); + auto x1 = std::min(_range.GetRight(), (int32_t)gMapSizeMaxXY); + auto y1 = std::min(_range.GetBottom(), (int32_t)gMapSizeMaxXY); + + for (int32_t y = y0; y <= y1; y += 32) + { + for (int32_t x = x0; x <= x1; x += 32) + { + if (MapCanClearAt(x, y)) + { + auto cost = ClearSceneryFromTile(x / 32, y / 32, executing); + if (cost != MONEY32_UNDEFINED) + { + noValidTiles = false; + totalCost += cost; + } + } + else + { + error = GA_ERROR::NOT_OWNED; + errorMessage = STR_LAND_NOT_OWNED_BY_PARK; + } + } + } + + if (_itemsToClear & CLEARABLE_ITEMS::SCENERY_LARGE) + { + ResetClearLargeSceneryFlag(); + } + + if (noValidTiles) + { + result->Error = error; + result->ErrorMessage = errorMessage; + } + + result->Cost = totalCost; + return result; + } + + money32 ClearSceneryFromTile(int32_t x, int32_t y, bool executing) const + { + // Pass down all flags. + TileElement* tileElement = nullptr; + money32 totalCost = 0; + bool tileEdited; + do + { + tileEdited = false; + tileElement = map_get_first_element_at(x, y); + do + { + auto type = tileElement->GetType(); + switch (type) + { + case TILE_ELEMENT_TYPE_PATH: + if (_itemsToClear & CLEARABLE_ITEMS::SCENERY_FOOTPATH) + { + auto footpathRemoveAction = FootpathRemoveAction(x * 32, y * 32, tileElement->base_height); + footpathRemoveAction.SetFlags(GetFlags()); + + auto res = executing ? footpathRemoveAction.Execute() : footpathRemoveAction.Query(); + if (res->Error != GA_ERROR::OK) + return MONEY32_UNDEFINED; + + totalCost += res->Cost; + tileEdited = executing; + } + break; + case TILE_ELEMENT_TYPE_SMALL_SCENERY: + if (_itemsToClear & CLEARABLE_ITEMS::SCENERY_SMALL) + { + auto removeSceneryAction = SmallSceneryRemoveAction( + x * 32, y * 32, tileElement->base_height, tileElement->AsSmallScenery()->GetSceneryQuadrant(), + tileElement->AsSmallScenery()->GetEntryIndex()); + removeSceneryAction.SetFlags(GetFlags()); + + auto res = executing ? removeSceneryAction.Execute() : removeSceneryAction.Query(); + if (res->Error != GA_ERROR::OK) + return MONEY32_UNDEFINED; + + totalCost += res->Cost; + tileEdited = executing; + } + break; + case TILE_ELEMENT_TYPE_WALL: + if (_itemsToClear & CLEARABLE_ITEMS::SCENERY_SMALL) + { + TileCoordsXYZD wallLocation = { x, y, tileElement->base_height, tileElement->GetDirection() }; + auto wallRemoveAction = WallRemoveAction(wallLocation); + wallRemoveAction.SetFlags(GetFlags()); + + auto res = executing ? wallRemoveAction.Execute() : wallRemoveAction.Query(); + if (res->Error != GA_ERROR::OK) + return MONEY32_UNDEFINED; + + totalCost += res->Cost; + tileEdited = executing; + } + break; + case TILE_ELEMENT_TYPE_LARGE_SCENERY: + if (_itemsToClear & CLEARABLE_ITEMS::SCENERY_LARGE) + { + auto removeSceneryAction = LargeSceneryRemoveAction( + x * 32, y * 32, tileElement->base_height, tileElement->GetDirection(), + tileElement->AsLargeScenery()->GetSequenceIndex()); + removeSceneryAction.SetFlags(GetFlags() | GAME_COMMAND_FLAG_PATH_SCENERY); + + auto res = executing ? removeSceneryAction.Execute() : removeSceneryAction.Query(); + if (res->Error != GA_ERROR::OK) + return MONEY32_UNDEFINED; + + totalCost += res->Cost; + tileEdited = executing; + } + break; + } + } while (!tileEdited && !(tileElement++)->IsLastForTile()); + } while (tileEdited); + + return totalCost; + } + + /** + * Function to clear the flag that is set to prevent cost duplication + * when using the clear scenery tool with large scenery. + */ + static void ResetClearLargeSceneryFlag() + { + // TODO: Improve efficiency of this + for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++) + { + for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) + { + auto tileElement = map_get_first_element_at(x, y); + do + { + if (tileElement->GetType() == TILE_ELEMENT_TYPE_LARGE_SCENERY) + { + tileElement->flags &= ~(1 << 6); + } + } while (!(tileElement++)->IsLastForTile()); + } + } + } + + static bool MapCanClearAt(int32_t x, int32_t y) + { + return (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) || gCheatsSandboxMode || map_is_location_owned_or_has_rights(x, y); + } +}; diff --git a/src/openrct2/actions/GameActionRegistration.cpp b/src/openrct2/actions/GameActionRegistration.cpp index d5c77b8671..e769d1ff9e 100644 --- a/src/openrct2/actions/GameActionRegistration.cpp +++ b/src/openrct2/actions/GameActionRegistration.cpp @@ -8,10 +8,12 @@ *****************************************************************************/ #include "BannerSetNameAction.hpp" +#include "ClearAction.hpp" #include "ClimateSetAction.hpp" #include "FootpathRemoveAction.hpp" #include "GameAction.h" #include "GuestSetNameAction.hpp" +#include "LargeSceneryRemoveAction.hpp" #include "MazeSetTrackAction.hpp" #include "ParkMarketingAction.hpp" #include "ParkSetLoanAction.hpp" @@ -26,6 +28,7 @@ #include "RideSetStatus.hpp" #include "SetParkEntranceFeeAction.hpp" #include "SignSetNameAction.hpp" +#include "SmallSceneryRemoveAction.hpp" #include "StaffSetColourAction.hpp" #include "StaffSetNameAction.hpp" #include "WallRemoveAction.hpp" @@ -55,5 +58,8 @@ namespace GameActions Register(); Register(); Register(); + Register(); + Register(); + Register(); } } // namespace GameActions diff --git a/src/openrct2/actions/LargeSceneryRemoveAction.hpp b/src/openrct2/actions/LargeSceneryRemoveAction.hpp new file mode 100644 index 0000000000..e3d8cc4d97 --- /dev/null +++ b/src/openrct2/actions/LargeSceneryRemoveAction.hpp @@ -0,0 +1,251 @@ +/***************************************************************************** + * Copyright (c) 2014-2018 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "../Cheats.h" +#include "../OpenRCT2.h" +#include "../common.h" +#include "../core/MemoryStream.h" +#include "../interface/Window.h" +#include "../localisation/Localisation.h" +#include "../localisation/StringIds.h" +#include "../management/Finance.h" +#include "../ride/Ride.h" +#include "../world/Park.h" +#include "../world/SmallScenery.h" +#include "../world/Sprite.h" +#include "GameAction.h" + +DEFINE_GAME_ACTION(LargeSceneryRemoveAction, GAME_COMMAND_REMOVE_LARGE_SCENERY, GameActionResult) +{ +private: + int16_t _x; + int16_t _y; + uint8_t _baseHeight; + uint8_t _direction; + uint16_t _tileIndex; + +public: + LargeSceneryRemoveAction() = default; + + LargeSceneryRemoveAction(int16_t x, int16_t y, uint8_t baseHeight, uint8_t direction, uint16_t tileIndex) + : _x(x) + , _y(y) + , _baseHeight(baseHeight) + , _direction(direction) + , _tileIndex(tileIndex) + { + } + + uint16_t GetActionFlags() const override + { + return GameAction::GetActionFlags(); + } + + void Serialise(DataSerialiser & stream) override + { + GameAction::Serialise(stream); + + stream << DS_TAG(_x) << DS_TAG(_y) << DS_TAG(_baseHeight) << DS_TAG(_direction) << DS_TAG(_tileIndex); + } + + GameActionResult::Ptr Query() const override + { + GameActionResult::Ptr res = std::make_unique(); + + const uint32_t flags = GetFlags(); + + int32_t z = tile_element_height(_x, _y); + res->Position.x = _x + 16; + res->Position.y = _y + 16; + res->Position.z = z; + res->ExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; + res->Cost = 0; + + TileElement* tileElement = FindLargeSceneryElement(); + if (tileElement == nullptr) + { + log_warning("Invalid game command for scenery removal, x = %d, y = %d", _x, _y); + return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_SELECTION_OF_OBJECTS); + } + + rct_scenery_entry* scenery_entry = tileElement->AsLargeScenery()->GetEntry(); + + LocationXYZ16 firstTile = { + scenery_entry->large_scenery.tiles[_tileIndex].x_offset, scenery_entry->large_scenery.tiles[_tileIndex].y_offset, + static_cast((_baseHeight * 8) - scenery_entry->large_scenery.tiles[_tileIndex].z_offset) + }; + + rotate_map_coordinates(&firstTile.x, &firstTile.y, _direction); + + firstTile.x = _x - firstTile.x; + firstTile.y = _y - firstTile.y; + + bool calculate_cost = true; + for (int32_t i = 0; scenery_entry->large_scenery.tiles[i].x_offset != -1; i++) + { + LocationXYZ16 currentTile = { scenery_entry->large_scenery.tiles[i].x_offset, + scenery_entry->large_scenery.tiles[i].y_offset, + scenery_entry->large_scenery.tiles[i].z_offset }; + + rotate_map_coordinates(¤tTile.x, ¤tTile.y, _direction); + + currentTile.x += firstTile.x; + currentTile.y += firstTile.y; + currentTile.z += firstTile.z; + + if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !gCheatsSandboxMode) + { + if (!map_is_location_owned(currentTile.x, currentTile.y, currentTile.z)) + { + return MakeResult(GA_ERROR::NO_CLEARANCE, STR_CANT_REMOVE_THIS, STR_LAND_NOT_OWNED_BY_PARK); + } + } + + // NOTE: Not sure what to make of this, is this actually correct? + if (flags & GAME_COMMAND_FLAG_PATH_SCENERY) + { + if (tileElement->flags & (1 << 6)) + calculate_cost = false; + + tileElement->flags |= (1 << 6); + } + } + + if (calculate_cost == true) + res->Cost = scenery_entry->large_scenery.removal_price * 10; + + return res; + } + + GameActionResult::Ptr Execute() const override + { + GameActionResult::Ptr res = std::make_unique(); + + const uint32_t flags = GetFlags(); + + int32_t z = tile_element_height(_x, _y); + res->Position.x = _x + 16; + res->Position.y = _y + 16; + res->Position.z = z; + res->ExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; + res->Cost = 0; + + TileElement* tileElement = FindLargeSceneryElement(); + if (tileElement == nullptr) + { + log_warning("Invalid game command for scenery removal, x = %d, y = %d", _x, _y); + return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_SELECTION_OF_OBJECTS); + } + + tile_element_remove_banner_entry(tileElement); + + rct_scenery_entry* scenery_entry = tileElement->AsLargeScenery()->GetEntry(); + + LocationXYZ16 firstTile = { + scenery_entry->large_scenery.tiles[_tileIndex].x_offset, scenery_entry->large_scenery.tiles[_tileIndex].y_offset, + static_cast((_baseHeight * 8) - scenery_entry->large_scenery.tiles[_tileIndex].z_offset) + }; + + rotate_map_coordinates(&firstTile.x, &firstTile.y, _direction); + + firstTile.x = _x - firstTile.x; + firstTile.y = _y - firstTile.y; + + for (int32_t i = 0; scenery_entry->large_scenery.tiles[i].x_offset != -1; i++) + { + LocationXYZ16 currentTile = { scenery_entry->large_scenery.tiles[i].x_offset, + scenery_entry->large_scenery.tiles[i].y_offset, + scenery_entry->large_scenery.tiles[i].z_offset }; + + rotate_map_coordinates(¤tTile.x, ¤tTile.y, _direction); + + currentTile.x += firstTile.x; + currentTile.y += firstTile.y; + currentTile.z += firstTile.z; + + if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !gCheatsSandboxMode) + { + if (!map_is_location_owned(currentTile.x, currentTile.y, currentTile.z)) + { + return MakeResult(GA_ERROR::NO_CLEARANCE, STR_CANT_REMOVE_THIS, STR_LAND_NOT_OWNED_BY_PARK); + } + } + + TileElement* sceneryElement = map_get_first_element_at(currentTile.x / 32, currentTile.y / 32); + bool element_found = false; + do + { + if (sceneryElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) + continue; + + if (sceneryElement->GetDirection() != _direction) + continue; + + if (sceneryElement->AsLargeScenery()->GetSequenceIndex() != i) + continue; + + if (sceneryElement->base_height != currentTile.z / 8) + continue; + + // If we are removing ghost elements + if ((flags & GAME_COMMAND_FLAG_GHOST) && sceneryElement->IsGhost() == false) + continue; + + map_invalidate_tile_full(currentTile.x, currentTile.y); + tile_element_remove(sceneryElement); + + element_found = true; + break; + } while (!(sceneryElement++)->IsLastForTile()); + + if (element_found == false) + { + log_error("Tile not found when trying to remove element!"); + } + } + + res->Cost = scenery_entry->large_scenery.removal_price * 10; + + return res; + } + +private: + TileElement* FindLargeSceneryElement() const + { + TileElement* tileElement = map_get_first_element_at(_x / 32, _y / 32); + if (tileElement == nullptr) + return nullptr; + + do + { + if (tileElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) + continue; + + if (tileElement->base_height != _baseHeight) + continue; + + if (tileElement->AsLargeScenery()->GetSequenceIndex() != _tileIndex) + continue; + + if (tileElement->GetDirection() != _direction) + continue; + + // If we are removing ghost elements + if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && tileElement->IsGhost() == false) + continue; + + return tileElement; + + } while (!(tileElement++)->IsLastForTile()); + + return nullptr; + } +}; diff --git a/src/openrct2/actions/SmallSceneryRemoveAction.hpp b/src/openrct2/actions/SmallSceneryRemoveAction.hpp new file mode 100644 index 0000000000..60d6ab1925 --- /dev/null +++ b/src/openrct2/actions/SmallSceneryRemoveAction.hpp @@ -0,0 +1,171 @@ +/***************************************************************************** + * Copyright (c) 2014-2018 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "../Cheats.h" +#include "../OpenRCT2.h" +#include "../common.h" +#include "../core/MemoryStream.h" +#include "../interface/Window.h" +#include "../localisation/Localisation.h" +#include "../localisation/StringIds.h" +#include "../management/Finance.h" +#include "../ride/Ride.h" +#include "../world/Park.h" +#include "../world/SmallScenery.h" +#include "../world/Sprite.h" +#include "GameAction.h" + +DEFINE_GAME_ACTION(SmallSceneryRemoveAction, GAME_COMMAND_REMOVE_SCENERY, GameActionResult) +{ +private: + int16_t _x; + int16_t _y; + uint8_t _baseHeight; + uint8_t _quadrant; + uint8_t _sceneryType; + +public: + SmallSceneryRemoveAction() = default; + + SmallSceneryRemoveAction(int16_t x, int16_t y, uint8_t baseHeight, uint8_t quadrant, uint8_t sceneryType) + : _x(x) + , _y(y) + , _baseHeight(baseHeight) + , _quadrant(quadrant) + , _sceneryType(sceneryType) + { + } + + uint16_t GetActionFlags() const override + { + return GameAction::GetActionFlags(); + } + + void Serialise(DataSerialiser & stream) override + { + GameAction::Serialise(stream); + + stream << DS_TAG(_x) << DS_TAG(_y) << DS_TAG(_baseHeight) << DS_TAG(_quadrant) << DS_TAG(_sceneryType); + } + + GameActionResult::Ptr Query() const override + { + GameActionResult::Ptr res = std::make_unique(); + + if (!map_is_location_valid({ _x, _y })) + { + return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_REMOVE_THIS, STR_LAND_NOT_OWNED_BY_PARK); + } + + rct_scenery_entry* entry = get_small_scenery_entry(_sceneryType); + if (entry == nullptr) + { + return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_REMOVE_THIS, STR_INVALID_SELECTION_OF_OBJECTS); + } + + res->Cost = entry->small_scenery.removal_price * 10; + res->ExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; + res->Position.x = _x; + res->Position.y = _y; + res->Position.z = _baseHeight * 8; + + if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !(GetFlags() & GAME_COMMAND_FLAG_GHOST) && !gCheatsSandboxMode) + { + // Check if allowed to remove item + if (gParkFlags & PARK_FLAGS_FORBID_TREE_REMOVAL) + { + if (entry->small_scenery.height > 64) + { + res->Error = GA_ERROR::NO_CLEARANCE; + res->ErrorTitle = STR_CANT_REMOVE_THIS; + res->ErrorMessage = STR_FORBIDDEN_BY_THE_LOCAL_AUTHORITY; + return res; + } + } + + // Check if the land is owned + if (!map_is_location_owned(_x, _y, _baseHeight * 8)) + { + res->Error = GA_ERROR::NO_CLEARANCE; + res->ErrorTitle = STR_CANT_REMOVE_THIS; + res->ErrorMessage = STR_LAND_NOT_OWNED_BY_PARK; + return res; + } + } + + TileElement* tileElement = FindSceneryElement(); + if (tileElement == nullptr) + { + res->Cost = 0; + return res; + } + + return res; + } + + GameActionResult::Ptr Execute() const override + { + GameActionResult::Ptr res = std::make_unique(); + + rct_scenery_entry* entry = get_small_scenery_entry(_sceneryType); + if (entry == nullptr) + { + return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_SELECTION_OF_OBJECTS); + } + + res->Cost = entry->small_scenery.removal_price * 10; + res->ExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; + res->Position.x = _x; + res->Position.y = _y; + res->Position.z = _baseHeight * 8; + + TileElement* tileElement = FindSceneryElement(); + if (tileElement == nullptr) + { + res->Cost = 0; + return res; + } + + res->Position.z = tile_element_height(res->Position.x, res->Position.y); + + map_invalidate_tile_full(_x, _y); + tile_element_remove(tileElement); + + return res; + } + +private: + TileElement* FindSceneryElement() const + { + TileElement* tileElement = map_get_first_element_at(_x / 32, _y / 32); + if (!tileElement) + return nullptr; + + do + { + if (tileElement->GetType() != TILE_ELEMENT_TYPE_SMALL_SCENERY) + continue; + if ((tileElement->AsSmallScenery()->GetSceneryQuadrant()) != _quadrant) + continue; + if (tileElement->base_height != _baseHeight) + continue; + if (tileElement->AsSmallScenery()->GetEntryIndex() != _sceneryType) + continue; + if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && tileElement->IsGhost() == false) + continue; + + return tileElement; + + } while (!(tileElement++)->IsLastForTile()); + + return nullptr; + } +}; diff --git a/src/openrct2/actions/WallRemoveAction.hpp b/src/openrct2/actions/WallRemoveAction.hpp index 4746113587..ea385c75dd 100644 --- a/src/openrct2/actions/WallRemoveAction.hpp +++ b/src/openrct2/actions/WallRemoveAction.hpp @@ -99,6 +99,9 @@ private: TileElement* GetFirstWallElementAt(const TileCoordsXYZD& location, bool isGhost) const { TileElement* tileElement = map_get_first_element_at(location.x, location.y); + if (!tileElement) + return nullptr; + do { if (tileElement->GetType() != TILE_ELEMENT_TYPE_WALL) diff --git a/src/openrct2/core/DataSerialiserTraits.h b/src/openrct2/core/DataSerialiserTraits.h index a95cebe730..c496398259 100644 --- a/src/openrct2/core/DataSerialiserTraits.h +++ b/src/openrct2/core/DataSerialiserTraits.h @@ -14,6 +14,7 @@ #include "../network/NetworkTypes.h" #include "../network/network.h" #include "../ride/Ride.h" +#include "../world/Location.hpp" #include "DataSerialiserTag.h" #include "Endianness.h" #include "MemoryStream.h" @@ -292,3 +293,31 @@ template struct DataSerializerTraitsWrite("}", 1); } }; + +template<> struct DataSerializerTraits +{ + static void encode(IStream* stream, const MapRange& v) + { + stream->WriteValue(ByteSwapBE(v.LeftTop.x)); + stream->WriteValue(ByteSwapBE(v.LeftTop.y)); + stream->WriteValue(ByteSwapBE(v.RightBottom.x)); + stream->WriteValue(ByteSwapBE(v.RightBottom.y)); + } + static void decode(IStream* stream, MapRange& v) + { + auto l = ByteSwapBE(stream->ReadValue()); + auto t = ByteSwapBE(stream->ReadValue()); + auto r = ByteSwapBE(stream->ReadValue()); + auto b = ByteSwapBE(stream->ReadValue()); + v = MapRange(l, t, r, b); + } + static void log(IStream* stream, const MapRange& v) + { + char coords[128] = {}; + snprintf( + coords, sizeof(coords), "MapRange(l = %d, t = %d, r = %d, b = %d)", v.LeftTop.x, v.LeftTop.y, v.RightBottom.x, + v.RightBottom.y); + + stream->Write(coords, strlen(coords)); + } +}; diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index a58661a970..d3713b59ab 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -30,7 +30,7 @@ // This string specifies which version of network stream current build uses. // It is used for making sure only compatible builds get connected, even within // single OpenRCT2 version. -#define NETWORK_STREAM_VERSION "19" +#define NETWORK_STREAM_VERSION "20" #define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION static rct_peep* _pickup_peep = nullptr; diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index e53d9f82f3..6a32094701 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -12,6 +12,8 @@ #include "../Cheats.h" #include "../Game.h" #include "../OpenRCT2.h" +#include "../actions/LargeSceneryRemoveAction.hpp" +#include "../actions/SmallSceneryRemoveAction.hpp" #include "../actions/WallRemoveAction.hpp" #include "../audio/audio.h" #include "../core/File.h" @@ -828,17 +830,24 @@ static int32_t track_design_place_scenery( } z = (scenery->z * 8 + originZ) / 8; - game_do_command( - mapCoord.x, flags | quadrant << 8, mapCoord.y, (entry_index << 8) | z, GAME_COMMAND_REMOVE_SCENERY, - 0, 0); + + auto removeSceneryAction = SmallSceneryRemoveAction(mapCoord.x, mapCoord.y, z, quadrant, entry_index); + removeSceneryAction.SetFlags(flags); + removeSceneryAction.Execute(); + break; } case OBJECT_TYPE_LARGE_SCENERY: + { z = (scenery->z * 8 + originZ) / 8; - game_do_command( - mapCoord.x, flags | (((rotation + scenery->flags) & 0x3) << 8), mapCoord.y, z, - GAME_COMMAND_REMOVE_LARGE_SCENERY, 0, 0); + + auto removeSceneryAction = LargeSceneryRemoveAction( + mapCoord.x, mapCoord.y, z, (rotation + scenery->flags) & 0x3, 0); + removeSceneryAction.SetFlags(flags); + removeSceneryAction.Execute(); + break; + } case OBJECT_TYPE_WALLS: { z = (scenery->z * 8 + originZ) / 8; @@ -850,8 +859,9 @@ static int32_t track_design_place_scenery( wallRemoveAction.SetFlags(flags); GameActions::Execute(&wallRemoveAction); + + break; } - break; case OBJECT_TYPE_PATHS: z = (scenery->z * 8 + originZ) / 8; footpath_remove(mapCoord.x, mapCoord.y, z, flags); diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 950a2937c6..66cf5e71d9 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -11,6 +11,8 @@ #include "../common.h" +#include + #define LOCATION_NULL ((int16_t)(uint16_t)0x8000) #define RCT_XY8_UNDEFINED 0xFFFF #define MakeXY16(x, y) \ @@ -59,7 +61,15 @@ constexpr int32_t COORDS_NULL = -1; */ struct CoordsXY { - int32_t x, y; + int32_t x = 0; + int32_t y = 0; + + CoordsXY() = default; + constexpr CoordsXY(int32_t _x, int32_t _y) + : x(_x) + , y(_y) + { + } }; struct TileCoordsXY @@ -86,7 +96,17 @@ struct TileCoordsXY struct CoordsXYZ { - int32_t x, y, z; + int32_t x = 0; + int32_t y = 0; + int32_t z = 0; + + CoordsXYZ() = default; + constexpr CoordsXYZ(int32_t _x, int32_t _y, int32_t _z) + : x(_x) + , y(_y) + , z(_z) + { + } }; struct TileCoordsXYZ @@ -150,3 +170,47 @@ struct TileCoordsXYZD return x == COORDS_NULL; }; }; + +/** + * Represents a rectangular range of the map using regular coordinates (32 per tile). + */ +struct MapRange +{ + CoordsXY LeftTop; + CoordsXY RightBottom; + + int32_t GetLeft() const + { + return LeftTop.x; + } + int32_t GetTop() const + { + return LeftTop.y; + } + int32_t GetRight() const + { + return RightBottom.x; + } + int32_t GetBottom() const + { + return RightBottom.y; + } + + MapRange() + : MapRange(0, 0, 0, 0) + { + } + MapRange(int32_t left, int32_t top, int32_t right, int32_t bottom) + : LeftTop(left, top) + , RightBottom(right, bottom) + { + } + + MapRange Normalise() const + { + auto result = MapRange( + std::min(GetLeft(), GetRight()), std::min(GetTop(), GetBottom()), std::max(GetLeft(), GetRight()), + std::max(GetTop(), GetBottom())); + return result; + } +}; diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index 1059523993..a7a40a7393 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -15,6 +15,8 @@ #include "../Input.h" #include "../OpenRCT2.h" #include "../actions/FootpathRemoveAction.hpp" +#include "../actions/LargeSceneryRemoveAction.hpp" +#include "../actions/SmallSceneryRemoveAction.hpp" #include "../actions/WallRemoveAction.hpp" #include "../audio/audio.h" #include "../config/Config.h" @@ -788,167 +790,6 @@ bool map_is_location_owned_or_has_rights(int32_t x, int32_t y) return false; } -/** - * - * rct2: 0x006B8E1B - */ -void game_command_remove_large_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, [[maybe_unused]] int32_t* esi, [[maybe_unused]] int32_t* edi, - [[maybe_unused]] int32_t* ebp) -{ - uint8_t base_height = *edx; - uint8_t tileIndex = *edx >> 8; - uint8_t tile_element_direction = *ebx >> 8; - int32_t x = *eax; - int32_t y = *ecx; - int32_t z = tile_element_height(x, y); - uint8_t flags = *ebx & 0xFF; - gCommandPosition.x = x + 16; - gCommandPosition.y = y + 16; - gCommandPosition.z = z; - gCommandExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; - - if (!(flags & GAME_COMMAND_FLAG_GHOST) && game_is_paused() && !gCheatsBuildInPauseMode) - { - gGameCommandErrorText = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; - *ebx = MONEY32_UNDEFINED; - return; - } - - bool element_found = false; - TileElement* tileElement = map_get_first_element_at(x / 32, y / 32); - if (tileElement == nullptr) - { - log_warning("Invalid game command for scenery removal, x = %d, y = %d", x, y); - *ebx = MONEY32_UNDEFINED; - return; - } - do - { - if (tileElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) - continue; - - if (tileElement->base_height != base_height) - continue; - - if (tileElement->AsLargeScenery()->GetSequenceIndex() != tileIndex) - continue; - - if (tileElement->GetDirection() != tile_element_direction) - continue; - - // If we are removing ghost elements - if ((flags & GAME_COMMAND_FLAG_GHOST) && !(tileElement->flags & TILE_ELEMENT_FLAG_GHOST)) - continue; - - element_found = true; - break; - } while (!(tileElement++)->IsLastForTile()); - - if (element_found == false) - { - *ebx = 0; - return; - } - - if (flags & GAME_COMMAND_FLAG_APPLY) - { - tile_element_remove_banner_entry(tileElement); - } - - rct_scenery_entry* scenery_entry = tileElement->AsLargeScenery()->GetEntry(); - LocationXYZ16 firstTile = { - scenery_entry->large_scenery.tiles[tileIndex].x_offset, scenery_entry->large_scenery.tiles[tileIndex].y_offset, - static_cast((base_height * 8) - scenery_entry->large_scenery.tiles[tileIndex].z_offset) - }; - - rotate_map_coordinates(&firstTile.x, &firstTile.y, tile_element_direction); - - firstTile.x = x - firstTile.x; - firstTile.y = y - firstTile.y; - - bool calculate_cost = true; - for (int32_t i = 0; scenery_entry->large_scenery.tiles[i].x_offset != -1; i++) - { - LocationXYZ16 currentTile = { scenery_entry->large_scenery.tiles[i].x_offset, - scenery_entry->large_scenery.tiles[i].y_offset, - scenery_entry->large_scenery.tiles[i].z_offset }; - - rotate_map_coordinates(¤tTile.x, ¤tTile.y, tile_element_direction); - - currentTile.x += firstTile.x; - currentTile.y += firstTile.y; - currentTile.z += firstTile.z; - - if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !gCheatsSandboxMode) - { - if (!map_is_location_owned(currentTile.x, currentTile.y, currentTile.z)) - { - *ebx = MONEY32_UNDEFINED; - return; - } - } - - // If not applying then no need to delete the actual element - if (!(flags & GAME_COMMAND_FLAG_APPLY)) - { - if (flags & (1 << 7)) - { - if (tileElement->flags & (1 << 6)) - calculate_cost = false; - tileElement->flags |= (1 << 6); - } - continue; - } - - TileElement* sceneryElement = map_get_first_element_at(currentTile.x / 32, currentTile.y / 32); - element_found = false; - do - { - if (sceneryElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) - continue; - - if (sceneryElement->GetDirection() != tile_element_direction) - continue; - - if (sceneryElement->AsLargeScenery()->GetSequenceIndex() != i) - continue; - - if (sceneryElement->base_height != currentTile.z / 8) - continue; - - // If we are removing ghost elements - if ((flags & GAME_COMMAND_FLAG_GHOST) && !(sceneryElement->flags & TILE_ELEMENT_FLAG_GHOST)) - continue; - - map_invalidate_tile_full(currentTile.x, currentTile.y); - tile_element_remove(sceneryElement); - element_found = true; - break; - } while (!(sceneryElement++)->IsLastForTile()); - - if (element_found == false) - { - log_error("Tile not found when trying to remove element!"); - } - } - - if (flags & GAME_COMMAND_FLAG_APPLY && gGameCommandNestLevel == 1 && !(flags & GAME_COMMAND_FLAG_GHOST)) - { - LocationXYZ16 coord; - coord.x = x + 16; - coord.y = y + 16; - coord.z = tile_element_height(coord.x, coord.y); - network_set_player_last_action_coord(network_get_player_index(game_command_playerid), coord); - } - - *ebx = scenery_entry->large_scenery.removal_price * 10; - if (gParkFlags & PARK_FLAGS_NO_MONEY || calculate_cost == false) - { - *ebx = 0; - } -} - /** * * rct2: 0x006B909A @@ -1032,203 +873,6 @@ void game_command_set_large_scenery_colour( *ebx = 0; } -// This will cause clear scenery to remove paths -// This should be a flag for the game command which can be set via a checkbox on the clear scenery window. -// #define CLEAR_SCENERY_REMOVES_PATHS - -/** - * - * rct2: 0x0068DFE4 - */ -static money32 map_clear_scenery_from_tile(int32_t x, int32_t y, int32_t clear, int32_t flags) -{ - int32_t type; - money32 cost, totalCost; - TileElement* tileElement; - - totalCost = 0; - -restart_from_beginning: - tileElement = map_get_first_element_at(x, y); - do - { - type = tileElement->GetType(); - switch (type) - { - case TILE_ELEMENT_TYPE_PATH: - if (clear & (1 << 2)) - { - auto footpathRemoveAction = FootpathRemoveAction(x * 32, y * 32, tileElement->base_height); - footpathRemoveAction.SetFlags(flags); - auto res - = ((flags & GAME_COMMAND_FLAG_APPLY) ? footpathRemoveAction.Execute() : footpathRemoveAction.Query()); - if (res->Error == GA_ERROR::OK) - { - totalCost += res->Cost; - } - else - { - return MONEY32_UNDEFINED; - } - - if (flags & GAME_COMMAND_FLAG_APPLY) - goto restart_from_beginning; - } - break; - case TILE_ELEMENT_TYPE_SMALL_SCENERY: - if (clear & (1 << 0)) - { - int32_t eax = x * 32; - int32_t ebx = (tileElement->AsSmallScenery()->GetSceneryQuadrant() << 8) | flags; - int32_t ecx = y * 32; - int32_t edx = (tileElement->AsSmallScenery()->GetEntryIndex() << 8) | (tileElement->base_height); - int32_t edi = 0, ebp = 0; - cost = game_do_command(eax, ebx, ecx, edx, GAME_COMMAND_REMOVE_SCENERY, edi, ebp); - - if (cost == MONEY32_UNDEFINED) - return MONEY32_UNDEFINED; - - totalCost += cost; - - if (flags & GAME_COMMAND_FLAG_APPLY) - goto restart_from_beginning; - } - break; - case TILE_ELEMENT_TYPE_WALL: - if (clear & (1 << 0)) - { - // NOTE: We execute the game action directly as this function is already called from such. - TileCoordsXYZD wallLocation = { x, y, tileElement->base_height, tileElement->GetDirection() }; - auto wallRemoveAction = WallRemoveAction(wallLocation); - wallRemoveAction.SetFlags(flags); - auto res = ((flags & GAME_COMMAND_FLAG_APPLY) ? wallRemoveAction.Execute() : wallRemoveAction.Query()); - if (res->Error == GA_ERROR::OK) - { - totalCost += res->Cost; - } - } - break; - case TILE_ELEMENT_TYPE_LARGE_SCENERY: - if (clear & (1 << 1)) - { - int32_t eax = x * 32; - int32_t ebx = flags | ((tileElement->GetDirection()) << 8); - int32_t ecx = y * 32; - int32_t edx = tileElement->base_height | (tileElement->AsLargeScenery()->GetSequenceIndex() << 8); - int32_t edi = 0, ebp = 0; - cost = game_do_command(eax, ebx | (1 << 7), ecx, edx, GAME_COMMAND_REMOVE_LARGE_SCENERY, edi, ebp); - - if (cost == MONEY32_UNDEFINED) - return MONEY32_UNDEFINED; - - totalCost += cost; - - if (flags & GAME_COMMAND_FLAG_APPLY) - goto restart_from_beginning; - } - break; - } - } while (!(tileElement++)->IsLastForTile()); - - return totalCost; -} - -/** - * Function to clear the flag that is set to prevent cost duplication - * when using the clear scenery tool with large scenery. - */ -static void map_reset_clear_large_scenery_flag() -{ - TileElement* tileElement; - // TODO: Improve efficiency of this - for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++) - { - for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) - { - tileElement = map_get_first_element_at(x, y); - do - { - if (tileElement->GetType() == TILE_ELEMENT_TYPE_LARGE_SCENERY) - { - tileElement->flags &= ~(1 << 6); - } - } while (!(tileElement++)->IsLastForTile()); - } - } -} - -money32 map_clear_scenery(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t clear, int32_t flags) -{ - int32_t x, y, z; - money32 totalCost, cost; - bool noValidTiles; - - gCommandExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; - - x = (x0 + x1) / 2 + 16; - y = (y0 + y1) / 2 + 16; - z = tile_element_height(x, y); - gCommandPosition.x = x; - gCommandPosition.y = y; - gCommandPosition.z = z; - - x0 = std::max(x0, 32); - y0 = std::max(y0, 32); - x1 = std::min(x1, (int32_t)gMapSizeMaxXY); - y1 = std::min(y1, (int32_t)gMapSizeMaxXY); - - noValidTiles = true; - totalCost = 0; - for (y = y0; y <= y1; y += 32) - { - for (x = x0; x <= x1; x += 32) - { - if ((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) || gCheatsSandboxMode - || map_is_location_owned_or_has_rights(x, y)) - { - cost = map_clear_scenery_from_tile(x / 32, y / 32, clear, flags); - if (cost != MONEY32_UNDEFINED) - { - noValidTiles = false; - totalCost += cost; - } - } - else - { - gGameCommandErrorText = STR_LAND_NOT_OWNED_BY_PARK; - } - } - } - - if (gGameCommandNestLevel == 1 && flags & GAME_COMMAND_FLAG_APPLY) - { - LocationXYZ16 coord; - coord.x = ((x0 + x1) / 2) + 16; - coord.y = ((y0 + y1) / 2) + 16; - coord.z = tile_element_height(coord.x, coord.y); - network_set_player_last_action_coord(network_get_player_index(game_command_playerid), coord); - } - - if (clear & (1 << 1)) - { - map_reset_clear_large_scenery_flag(); - } - - return noValidTiles ? MONEY32_UNDEFINED : totalCost; -} - -/** - * - * rct2: 0x0068DF91 - */ -void game_command_clear_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, [[maybe_unused]] int32_t* esi, int32_t* edi, int32_t* ebp) -{ - *ebx = map_clear_scenery( - (int16_t)(*eax & 0xFFFF), (int16_t)(*ecx & 0xFFFF), (int16_t)(*edi & 0xFFFF), (int16_t)(*ebp & 0xFFFF), *edx, - *ebx & 0xFF); -} - /** * * rct2: 0x00663CCD @@ -3858,12 +3502,12 @@ static void clear_element_at(int32_t x, int32_t y, TileElement** elementPtr) } break; case TILE_ELEMENT_TYPE_LARGE_SCENERY: - gGameCommandErrorTitle = STR_CANT_REMOVE_THIS; - game_do_command( - x, (GAME_COMMAND_FLAG_APPLY) | (element->GetDirection() << 8), y, - (element->base_height) | (element->AsLargeScenery()->GetSequenceIndex() << 8), - GAME_COMMAND_REMOVE_LARGE_SCENERY, 0, 0); - break; + { + auto removeSceneryAction = LargeSceneryRemoveAction( + x, y, element->base_height, element->GetDirection(), element->AsLargeScenery()->GetSequenceIndex()); + GameActions::Execute(&removeSceneryAction); + } + break; case TILE_ELEMENT_TYPE_BANNER: gGameCommandErrorTitle = STR_CANT_REMOVE_THIS; game_do_command( diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h index 2c037d08d7..e0fa34a4ab 100644 --- a/src/openrct2/world/Map.h +++ b/src/openrct2/world/Map.h @@ -187,10 +187,6 @@ void game_command_set_land_height( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_set_land_ownership( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); -void game_command_remove_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); -void game_command_remove_large_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_remove_banner( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_set_scenery_colour( @@ -201,8 +197,6 @@ void game_command_set_large_scenery_colour( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_set_banner_colour( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); -void game_command_clear_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_change_surface_style( int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); void game_command_raise_land(int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp); diff --git a/src/openrct2/world/Scenery.cpp b/src/openrct2/world/Scenery.cpp index e88f54b59e..6aed6e3766 100644 --- a/src/openrct2/world/Scenery.cpp +++ b/src/openrct2/world/Scenery.cpp @@ -12,6 +12,8 @@ #include "../Cheats.h" #include "../Context.h" #include "../Game.h" +#include "../actions/LargeSceneryRemoveAction.hpp" +#include "../actions/SmallSceneryRemoveAction.hpp" #include "../actions/WallRemoveAction.hpp" #include "../common.h" #include "../localisation/Localisation.h" @@ -184,10 +186,10 @@ void scenery_remove_ghost_tool_placement() if (gSceneryGhostType & SCENERY_GHOST_FLAG_0) { gSceneryGhostType &= ~SCENERY_GHOST_FLAG_0; - uint8_t flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 - | GAME_COMMAND_FLAG_GHOST; - game_do_command( - x, flags | (gSceneryQuadrant << 8), y, z | (gSceneryPlaceObject << 8), GAME_COMMAND_REMOVE_SCENERY, 0, 0); + + auto removeSceneryAction = SmallSceneryRemoveAction(x, y, z, gSceneryQuadrant, gSceneryPlaceObject); + removeSceneryAction.SetFlags(GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 | GAME_COMMAND_FLAG_GHOST); + removeSceneryAction.Execute(); } if (gSceneryGhostType & SCENERY_GHOST_FLAG_1) @@ -223,13 +225,19 @@ void scenery_remove_ghost_tool_placement() if (gSceneryGhostType & SCENERY_GHOST_FLAG_3) { gSceneryGhostType &= ~SCENERY_GHOST_FLAG_3; - game_do_command(x, 105 | (gSceneryPlaceRotation << 8), y, z, GAME_COMMAND_REMOVE_LARGE_SCENERY, 0, 0); + + auto removeSceneryAction = LargeSceneryRemoveAction(x, y, z, gSceneryPlaceRotation, 0); + removeSceneryAction.SetFlags( + GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5); + removeSceneryAction.Execute(); } if (gSceneryGhostType & SCENERY_GHOST_FLAG_4) { gSceneryGhostType &= ~SCENERY_GHOST_FLAG_4; - game_do_command(x, 105, y, z | (gSceneryPlaceRotation << 8), GAME_COMMAND_REMOVE_BANNER, 0, 0); + constexpr uint32_t flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED + | GAME_COMMAND_FLAG_5; + game_do_command(x, flags, y, z | (gSceneryPlaceRotation << 8), GAME_COMMAND_REMOVE_BANNER, 0, 0); } } diff --git a/src/openrct2/world/SmallScenery.cpp b/src/openrct2/world/SmallScenery.cpp index 97ae0958f4..2ccd2f4501 100644 --- a/src/openrct2/world/SmallScenery.cpp +++ b/src/openrct2/world/SmallScenery.cpp @@ -24,95 +24,6 @@ #include "Scenery.h" #include "Surface.h" -static money32 SmallSceneryRemove( - int16_t x, int16_t y, uint8_t baseHeight, uint8_t quadrant, uint8_t sceneryType, uint8_t flags) -{ - if (!map_is_location_valid({ x, y })) - { - return MONEY32_UNDEFINED; - } - money32 cost; - - rct_scenery_entry* entry = get_small_scenery_entry(sceneryType); - if (entry == nullptr) - { - log_warning("Invalid game command for scenery removal, scenery_type = %u", sceneryType); - return MONEY32_UNDEFINED; - } - cost = entry->small_scenery.removal_price * 10; - - gCommandExpenditureType = RCT_EXPENDITURE_TYPE_LANDSCAPING; - gCommandPosition.x = x + 16; - gCommandPosition.y = y + 16; - gCommandPosition.z = baseHeight * 8; - - if (!(flags & GAME_COMMAND_FLAG_GHOST) && game_is_paused() && !gCheatsBuildInPauseMode) - { - gGameCommandErrorText = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; - return MONEY32_UNDEFINED; - } - - if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !(flags & GAME_COMMAND_FLAG_GHOST) && !gCheatsSandboxMode) - { - // Check if allowed to remove item - if (gParkFlags & PARK_FLAGS_FORBID_TREE_REMOVAL) - { - if (entry->small_scenery.height > 64) - { - gGameCommandErrorText = STR_FORBIDDEN_BY_THE_LOCAL_AUTHORITY; - return MONEY32_UNDEFINED; - } - } - - // Check if the land is owned - if (!map_is_location_owned(x, y, gCommandPosition.z)) - { - return MONEY32_UNDEFINED; - } - } - - bool sceneryFound = false; - TileElement* tileElement = map_get_first_element_at(x / 32, y / 32); - do - { - if (tileElement->GetType() != TILE_ELEMENT_TYPE_SMALL_SCENERY) - continue; - if ((tileElement->AsSmallScenery()->GetSceneryQuadrant()) != quadrant) - continue; - if (tileElement->base_height != baseHeight) - continue; - if (tileElement->AsSmallScenery()->GetEntryIndex() != sceneryType) - continue; - if ((flags & GAME_COMMAND_FLAG_GHOST) && !(tileElement->flags & TILE_ELEMENT_FLAG_GHOST)) - continue; - - sceneryFound = true; - break; - } while (!(tileElement++)->IsLastForTile()); - - if (!sceneryFound) - { - return 0; - } - - // Remove element - if (flags & GAME_COMMAND_FLAG_APPLY) - { - if (gGameCommandNestLevel == 1 && !(flags & GAME_COMMAND_FLAG_GHOST)) - { - LocationXYZ16 coord; - coord.x = x + 16; - coord.y = y + 16; - coord.z = tile_element_height(coord.x, coord.y); - network_set_player_last_action_coord(network_get_player_index(game_command_playerid), coord); - } - - map_invalidate_tile_full(x, y); - tile_element_remove(tileElement); - } - return (gParkFlags & PARK_FLAGS_NO_MONEY) ? 0 : cost; -} - static money32 SmallScenerySetColour( int16_t x, int16_t y, uint8_t baseHeight, uint8_t quadrant, uint8_t sceneryType, uint8_t primaryColour, uint8_t secondaryColour, uint8_t flags) @@ -412,17 +323,6 @@ static money32 SmallSceneryPlace( return cost; } -/** - * - * rct2: 0x006E0E01 - */ -void game_command_remove_scenery( - int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, [[maybe_unused]] int32_t* esi, [[maybe_unused]] int32_t* edi, - [[maybe_unused]] int32_t* ebp) -{ - *ebx = SmallSceneryRemove(*eax & 0xFFFF, *ecx & 0xFFFF, *edx & 0xFF, ((*ebx >> 8) & 0xFF), (*edx >> 8) & 0xFF, *ebx & 0xFF); -} - /** * * rct2: 0x006E0F26 diff --git a/test/tests/testdata/replays/issue5953.sv6r b/test/tests/testdata/replays/issue5953.sv6r index 1f5ea46f03..18ff8b47c5 100644 Binary files a/test/tests/testdata/replays/issue5953.sv6r and b/test/tests/testdata/replays/issue5953.sv6r differ diff --git a/test/tests/testdata/replays/issue6807.sv6r b/test/tests/testdata/replays/issue6807.sv6r index 81ed5cfc7b..1b316d030e 100644 Binary files a/test/tests/testdata/replays/issue6807.sv6r and b/test/tests/testdata/replays/issue6807.sv6r differ diff --git a/test/tests/testdata/replays/issue7828.sv6r b/test/tests/testdata/replays/issue7828.sv6r index 38099852f4..f34fe2188d 100644 Binary files a/test/tests/testdata/replays/issue7828.sv6r and b/test/tests/testdata/replays/issue7828.sv6r differ diff --git a/test/tests/testdata/replays/issue7867.sv6r b/test/tests/testdata/replays/issue7867.sv6r index f12ad4ebb0..dceea7135c 100644 Binary files a/test/tests/testdata/replays/issue7867.sv6r and b/test/tests/testdata/replays/issue7867.sv6r differ diff --git a/test/tests/testdata/replays/issue8203.sv6r b/test/tests/testdata/replays/issue8203.sv6r index 39b801251c..0f0e1fc261 100644 Binary files a/test/tests/testdata/replays/issue8203.sv6r and b/test/tests/testdata/replays/issue8203.sv6r differ diff --git a/test/tests/testdata/replays/issue8216.sv6r b/test/tests/testdata/replays/issue8216.sv6r index 2893f2f81f..ec4b0f930a 100644 Binary files a/test/tests/testdata/replays/issue8216.sv6r and b/test/tests/testdata/replays/issue8216.sv6r differ diff --git a/test/tests/testdata/replays/issue8345.sv6r b/test/tests/testdata/replays/issue8345.sv6r index 9aef2189bb..3aa268a3c6 100644 Binary files a/test/tests/testdata/replays/issue8345.sv6r and b/test/tests/testdata/replays/issue8345.sv6r differ diff --git a/test/tests/testdata/replays/issue8426.sv6r b/test/tests/testdata/replays/issue8426.sv6r index 0055e5a2d1..389ed0e180 100644 Binary files a/test/tests/testdata/replays/issue8426.sv6r and b/test/tests/testdata/replays/issue8426.sv6r differ