mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-23 07:43:01 +01:00
Remove duplicate functions (#10067)
* Reduce code duplication in src\openrct2\interface\Window.cpp `window_resize_gui_scenario_editor` was a subset of `window_resize_gui` * Reduce code duplication in src\openrct2\network\Twitch.cpp Both `GetFollowers()` and `GetMessage()` were very similar * Reduce code duplication in src/openrct2/world/SmallScenery.cpp `map_place_non_scenery_clear_func` and `map_place_scenery_clear_func` did basically the same thing, with a single branch difference * Reduce code duplication in src/openrct2-ui/windows/TopToolbar.cpp Make `top_toolbar_tool_update_scenery_clear` call `top_toolbar_tool_update_land_paint` and then do the additional differing calculation it did.
This commit is contained in:
@@ -1961,12 +1961,10 @@ static void window_top_toolbar_scenery_tool_down(int16_t x, int16_t y, rct_windo
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x0068E213
|
||||
*/
|
||||
static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y)
|
||||
static uint8_t top_toolbar_tool_update_land_paint(int16_t x, int16_t y)
|
||||
{
|
||||
uint8_t state_changed = 0;
|
||||
|
||||
map_invalidate_selection_rect();
|
||||
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
|
||||
|
||||
@@ -1980,11 +1978,9 @@ static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y)
|
||||
gClearSceneryCost = MONEY32_UNDEFINED;
|
||||
window_invalidate_by_class(WC_CLEAR_SCENERY);
|
||||
}
|
||||
return;
|
||||
return state_changed;
|
||||
}
|
||||
|
||||
uint8_t state_changed = 0;
|
||||
|
||||
if (!(gMapSelectFlags & MAP_SELECT_FLAG_ENABLE))
|
||||
{
|
||||
gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE;
|
||||
@@ -2034,7 +2030,16 @@ static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y)
|
||||
}
|
||||
|
||||
map_invalidate_selection_rect();
|
||||
if (!state_changed)
|
||||
return state_changed;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x0068E213
|
||||
*/
|
||||
static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y)
|
||||
{
|
||||
if (!top_toolbar_tool_update_land_paint(x, y))
|
||||
return;
|
||||
|
||||
auto action = GetClearAction();
|
||||
@@ -2047,79 +2052,6 @@ static void top_toolbar_tool_update_scenery_clear(int16_t x, int16_t y)
|
||||
}
|
||||
}
|
||||
|
||||
static void top_toolbar_tool_update_land_paint(int16_t x, int16_t y)
|
||||
{
|
||||
map_invalidate_selection_rect();
|
||||
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
|
||||
|
||||
LocationXY16 mapTile = {};
|
||||
screen_get_map_xy(x, y, &mapTile.x, &mapTile.y, nullptr);
|
||||
|
||||
if (mapTile.x == LOCATION_NULL)
|
||||
{
|
||||
if (gClearSceneryCost != MONEY32_UNDEFINED)
|
||||
{
|
||||
gClearSceneryCost = MONEY32_UNDEFINED;
|
||||
window_invalidate_by_class(WC_CLEAR_SCENERY);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t state_changed = 0;
|
||||
|
||||
if (!(gMapSelectFlags & MAP_SELECT_FLAG_ENABLE))
|
||||
{
|
||||
gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
if (gMapSelectType != MAP_SELECT_TYPE_FULL)
|
||||
{
|
||||
gMapSelectType = MAP_SELECT_TYPE_FULL;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
int16_t tool_size = std::max<uint16_t>(1, gLandToolSize);
|
||||
int16_t tool_length = (tool_size - 1) * 32;
|
||||
|
||||
// Move to tool bottom left
|
||||
mapTile.x -= (tool_size - 1) * 16;
|
||||
mapTile.y -= (tool_size - 1) * 16;
|
||||
mapTile.x &= 0xFFE0;
|
||||
mapTile.y &= 0xFFE0;
|
||||
|
||||
if (gMapSelectPositionA.x != mapTile.x)
|
||||
{
|
||||
gMapSelectPositionA.x = mapTile.x;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
if (gMapSelectPositionA.y != mapTile.y)
|
||||
{
|
||||
gMapSelectPositionA.y = mapTile.y;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
mapTile.x += tool_length;
|
||||
mapTile.y += tool_length;
|
||||
|
||||
if (gMapSelectPositionB.x != mapTile.x)
|
||||
{
|
||||
gMapSelectPositionB.x = mapTile.x;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
if (gMapSelectPositionB.y != mapTile.y)
|
||||
{
|
||||
gMapSelectPositionB.y = mapTile.y;
|
||||
state_changed++;
|
||||
}
|
||||
|
||||
map_invalidate_selection_rect();
|
||||
if (!state_changed)
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x00664280
|
||||
|
||||
@@ -1614,40 +1614,9 @@ void window_relocate_windows(int32_t width, int32_t height)
|
||||
*/
|
||||
void window_resize_gui(int32_t width, int32_t height)
|
||||
{
|
||||
window_resize_gui_scenario_editor(width, height);
|
||||
if (gScreenFlags & SCREEN_FLAGS_EDITOR)
|
||||
{
|
||||
window_resize_gui_scenario_editor(width, height);
|
||||
return;
|
||||
}
|
||||
rct_window* mainWind = window_get_main();
|
||||
if (mainWind != nullptr)
|
||||
{
|
||||
rct_viewport* viewport = mainWind->viewport;
|
||||
mainWind->width = width;
|
||||
mainWind->height = height;
|
||||
viewport->width = width;
|
||||
viewport->height = height;
|
||||
viewport->view_width = width << viewport->zoom;
|
||||
viewport->view_height = height << viewport->zoom;
|
||||
if (mainWind->widgets != nullptr && mainWind->widgets[WC_MAIN_WINDOW__0].type == WWT_VIEWPORT)
|
||||
{
|
||||
mainWind->widgets[WC_MAIN_WINDOW__0].right = width;
|
||||
mainWind->widgets[WC_MAIN_WINDOW__0].bottom = height;
|
||||
}
|
||||
}
|
||||
|
||||
rct_window* topWind = window_find_by_class(WC_TOP_TOOLBAR);
|
||||
if (topWind != nullptr)
|
||||
{
|
||||
topWind->width = std::max(640, width);
|
||||
}
|
||||
|
||||
rct_window* bottomWind = window_find_by_class(WC_BOTTOM_TOOLBAR);
|
||||
if (bottomWind != nullptr)
|
||||
{
|
||||
bottomWind->y = height - 32;
|
||||
bottomWind->width = std::max(640, width);
|
||||
}
|
||||
|
||||
rct_window* titleWind = window_find_by_class(WC_TITLE_MENU);
|
||||
if (titleWind != nullptr)
|
||||
|
||||
@@ -273,10 +273,7 @@ namespace Twitch
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /channel/:channel/audience
|
||||
*/
|
||||
static void GetFollowers()
|
||||
static void Get(const std::string& requestFormat, int operation)
|
||||
{
|
||||
char url[256];
|
||||
|
||||
@@ -286,12 +283,12 @@ namespace Twitch
|
||||
context->WriteLine("API URL is empty! skipping request...");
|
||||
return;
|
||||
}
|
||||
snprintf(url, sizeof(url), "%s/channel/%s/audience", gConfigTwitch.api_url, gConfigTwitch.channel);
|
||||
snprintf(url, sizeof(url), requestFormat.c_str(), gConfigTwitch.api_url, gConfigTwitch.channel);
|
||||
|
||||
_twitchState = TWITCH_STATE_WAITING;
|
||||
_twitchIdle = false;
|
||||
|
||||
Http::DoAsync({ url }, [](Http::Response res) {
|
||||
Http::DoAsync({ url }, [operation](Http::Response res) {
|
||||
std::shared_ptr<void> _(nullptr, [&](...) { _twitchIdle = true; });
|
||||
|
||||
if (res.status != Http::Status::OK)
|
||||
@@ -301,40 +298,24 @@ namespace Twitch
|
||||
}
|
||||
|
||||
_twitchJsonResponse = res;
|
||||
_twitchState = TWITCH_STATE_GET_FOLLOWERS;
|
||||
_twitchState = operation;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /channel/:channel/audience
|
||||
*/
|
||||
static void GetFollowers()
|
||||
{
|
||||
Get("%s/channel/%s/audience", TWITCH_STATE_GET_FOLLOWERS);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /channel/:channel/messages
|
||||
*/
|
||||
static void GetMessages()
|
||||
{
|
||||
char url[256];
|
||||
|
||||
if (gConfigTwitch.api_url == nullptr || strlen(gConfigTwitch.api_url) == 0)
|
||||
{
|
||||
auto context = GetContext();
|
||||
context->WriteLine("API URL is empty! skipping request...");
|
||||
return;
|
||||
}
|
||||
snprintf(url, sizeof(url), "%s/channel/%s/messages", gConfigTwitch.api_url, gConfigTwitch.channel);
|
||||
|
||||
_twitchState = TWITCH_STATE_WAITING;
|
||||
_twitchIdle = false;
|
||||
|
||||
Http::DoAsync({ url }, [](Http::Response res) {
|
||||
std::shared_ptr<void> _(nullptr, [&](...) { _twitchIdle = true; });
|
||||
|
||||
if (res.status != Http::Status::OK)
|
||||
{
|
||||
_twitchState = TWITCH_STATE_JOINED;
|
||||
return;
|
||||
}
|
||||
|
||||
_twitchJsonResponse = res;
|
||||
_twitchState = TWITCH_STATE_GET_MESSAGES;
|
||||
});
|
||||
Get("%s/channel/%s/messages", TWITCH_STATE_GET_MESSAGES);
|
||||
}
|
||||
|
||||
static void ParseFollowers()
|
||||
|
||||
@@ -24,16 +24,13 @@
|
||||
#include "Scenery.h"
|
||||
#include "Surface.h"
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E0D6E, 0x006B8D88
|
||||
*/
|
||||
int32_t map_place_scenery_clear_func(TileElement** tile_element, int32_t x, int32_t y, uint8_t flags, money32* price)
|
||||
static int32_t map_place_clear_func(
|
||||
TileElement** tile_element, int32_t x, int32_t y, uint8_t flags, money32* price, bool is_scenery)
|
||||
{
|
||||
if ((*tile_element)->GetType() != TILE_ELEMENT_TYPE_SMALL_SCENERY)
|
||||
return 1;
|
||||
|
||||
if (!(flags & GAME_COMMAND_FLAG_PATH_SCENERY))
|
||||
if (is_scenery && !(flags & GAME_COMMAND_FLAG_PATH_SCENERY))
|
||||
return 1;
|
||||
|
||||
rct_scenery_entry* scenery = (*tile_element)->AsSmallScenery()->GetEntry();
|
||||
@@ -61,38 +58,22 @@ int32_t map_place_scenery_clear_func(TileElement** tile_element, int32_t x, int3
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E0D6E, 0x006B8D88
|
||||
*/
|
||||
int32_t map_place_scenery_clear_func(TileElement** tile_element, int32_t x, int32_t y, uint8_t flags, money32* price)
|
||||
{
|
||||
return map_place_clear_func(tile_element, x, y, flags, price, /*is_scenery=*/true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006C5A4F, 0x006CDE57, 0x006A6733, 0x0066637E
|
||||
*/
|
||||
int32_t map_place_non_scenery_clear_func(TileElement** tile_element, int32_t x, int32_t y, uint8_t flags, money32* price)
|
||||
{
|
||||
if ((*tile_element)->GetType() != TILE_ELEMENT_TYPE_SMALL_SCENERY)
|
||||
return 1;
|
||||
|
||||
rct_scenery_entry* scenery = (*tile_element)->AsSmallScenery()->GetEntry();
|
||||
|
||||
if (gParkFlags & PARK_FLAGS_FORBID_TREE_REMOVAL)
|
||||
{
|
||||
if (scenery_small_entry_has_flag(scenery, SMALL_SCENERY_FLAG_IS_TREE))
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(gParkFlags & PARK_FLAGS_NO_MONEY))
|
||||
*price += scenery->small_scenery.removal_price * 10;
|
||||
|
||||
if (flags & GAME_COMMAND_FLAG_GHOST)
|
||||
return 0;
|
||||
|
||||
if (!(flags & GAME_COMMAND_FLAG_APPLY))
|
||||
return 0;
|
||||
|
||||
map_invalidate_tile(x, y, (*tile_element)->base_height * 8, (*tile_element)->clearance_height * 8);
|
||||
|
||||
tile_element_remove(*tile_element);
|
||||
|
||||
(*tile_element)--;
|
||||
return 0;
|
||||
return map_place_clear_func(tile_element, x, y, flags, price, /*is_scenery=*/false);
|
||||
}
|
||||
|
||||
bool scenery_small_entry_has_flag(const rct_scenery_entry* sceneryEntry, uint32_t flags)
|
||||
|
||||
Reference in New Issue
Block a user