Use ScreenCoordsXY for windows/Map (#10233)

* Use ScreenCoordsXY for windows/Footpath

- Refactor `place_park_entrance_get_map_position()` to receive `ScreenCoordsXY` and return `CoordsXYZD`
- Refactor `sub_68A15E()` to receive `ScreenCoordsXY` and return `CoordsXY`. Some of its usage are narrowing the result on purpose, as same variable is used for contexts where `int16_t` is still a hard requirement.

* Properly increment for big Z and use LOCATION_NULL
This commit is contained in:
Tulio Leao 2019-11-14 19:09:27 -03:00 committed by Michael Steenbeek
parent f47b199cd2
commit 93083780df
6 changed files with 88 additions and 82 deletions

View File

@ -658,7 +658,7 @@ static Peep* viewport_interaction_get_closest_peep(int32_t x, int32_t y, int32_t
*
* rct2: 0x0068A15E
*/
void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y)
CoordsXY sub_68A15E(ScreenCoordsXY screenCoords)
{
int16_t mapX, mapY;
CoordsXY initialPos{};
@ -666,15 +666,15 @@ void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y)
TileElement* tileElement;
rct_viewport* viewport;
get_map_coordinates_from_pos(
screenX, screenY, VIEWPORT_INTERACTION_MASK_TERRAIN & VIEWPORT_INTERACTION_MASK_WATER, &mapX, &mapY, &interactionType,
&tileElement, &viewport);
screenCoords.x, screenCoords.y, VIEWPORT_INTERACTION_MASK_TERRAIN & VIEWPORT_INTERACTION_MASK_WATER, &mapX, &mapY,
&interactionType, &tileElement, &viewport);
initialPos.x = mapX;
initialPos.y = mapY;
if (interactionType == VIEWPORT_INTERACTION_ITEM_NONE)
{
*x = LOCATION_NULL;
return;
initialPos.x = LOCATION_NULL;
return initialPos;
}
int16_t waterHeight = 0;
@ -683,7 +683,7 @@ void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y)
waterHeight = tileElement->AsSurface()->GetWaterHeight() << 4;
}
LocationXY16 initialVPPos = screen_coord_to_viewport_coord(viewport, { screenX, screenY });
LocationXY16 initialVPPos = screen_coord_to_viewport_coord(viewport, screenCoords);
CoordsXY mapPos = initialPos + CoordsXY{ 16, 16 };
for (int32_t i = 0; i < 5; i++)
@ -698,6 +698,5 @@ void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y)
mapPos.y = std::clamp(mapPos.y, initialPos.y, initialPos.y + 31);
}
*x = mapPos.x & ~0x1F;
*y = mapPos.y & ~0x1F;
return { mapPos.x & ~0x1F, mapPos.y & ~0x1F };
}

View File

@ -187,11 +187,11 @@ static void window_map_paint_hud_rectangle(rct_drawpixelinfo* dpi);
static void window_map_inputsize_land(rct_window* w);
static void window_map_inputsize_map(rct_window* w);
static void window_map_set_land_rights_tool_update(int32_t x, int32_t y);
static void window_map_place_park_entrance_tool_update(int32_t x, int32_t y);
static void window_map_set_peep_spawn_tool_update(int32_t x, int32_t y);
static void window_map_place_park_entrance_tool_down(int32_t x, int32_t y);
static void window_map_set_peep_spawn_tool_down(int32_t x, int32_t y);
static void window_map_set_land_rights_tool_update(ScreenCoordsXY screenCoords);
static void window_map_place_park_entrance_tool_update(ScreenCoordsXY screenCoords);
static void window_map_set_peep_spawn_tool_update(ScreenCoordsXY screenCoords);
static void window_map_place_park_entrance_tool_down(ScreenCoordsXY screenCoords);
static void window_map_set_peep_spawn_tool_down(ScreenCoordsXY screenCoords);
static void map_window_increase_map_size();
static void map_window_decrease_map_size();
static void map_window_set_pixels(rct_window* w);
@ -467,13 +467,13 @@ static void window_map_toolupdate(rct_window* w, rct_widgetindex widgetIndex, Sc
switch (widgetIndex)
{
case WIDX_SET_LAND_RIGHTS:
window_map_set_land_rights_tool_update(screenCoords.x, screenCoords.y);
window_map_set_land_rights_tool_update(screenCoords);
break;
case WIDX_BUILD_PARK_ENTRANCE:
window_map_place_park_entrance_tool_update(screenCoords.x, screenCoords.y);
window_map_place_park_entrance_tool_update(screenCoords);
break;
case WIDX_PEOPLE_STARTING_POSITION:
window_map_set_peep_spawn_tool_update(screenCoords.x, screenCoords.y);
window_map_set_peep_spawn_tool_update(screenCoords);
break;
}
}
@ -487,10 +487,10 @@ static void window_map_tooldown(rct_window* w, rct_widgetindex widgetIndex, Scre
switch (widgetIndex)
{
case WIDX_BUILD_PARK_ENTRANCE:
window_map_place_park_entrance_tool_down(screenCoords.x, screenCoords.y);
window_map_place_park_entrance_tool_down(screenCoords);
break;
case WIDX_PEOPLE_STARTING_POSITION:
window_map_set_peep_spawn_tool_down(screenCoords.x, screenCoords.y);
window_map_set_peep_spawn_tool_down(screenCoords);
break;
}
}
@ -1156,13 +1156,13 @@ static void window_map_paint_hud_rectangle(rct_drawpixelinfo* dpi)
*
* rct2: 0x0068D24E
*/
static void window_map_set_land_rights_tool_update(int32_t x, int32_t y)
static void window_map_set_land_rights_tool_update(ScreenCoordsXY screenCoords)
{
rct_viewport* viewport;
map_invalidate_selection_rect();
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
CoordsXY mapCoords = screen_get_map_xy({ x, y }, &viewport);
CoordsXY mapCoords = screen_get_map_xy(screenCoords, &viewport);
if (mapCoords.x == LOCATION_NULL)
return;
@ -1188,81 +1188,84 @@ static void window_map_set_land_rights_tool_update(int32_t x, int32_t y)
*
* rct2: 0x00666EEF
*/
static void place_park_entrance_get_map_position(
int32_t x, int32_t y, int16_t* mapX, int16_t* mapY, int16_t* mapZ, int32_t* direction)
static CoordsXYZD place_park_entrance_get_map_position(ScreenCoordsXY screenCoords)
{
sub_68A15E(x, y, mapX, mapY);
if (*mapX == LOCATION_NULL)
return;
CoordsXYZD parkEntranceMapPosition{ 0, 0, 0, INVALID_DIRECTION };
const CoordsXY mapCoords = sub_68A15E(screenCoords);
parkEntranceMapPosition = { mapCoords.x, mapCoords.y, 0, INVALID_DIRECTION };
if (parkEntranceMapPosition.x == LOCATION_NULL)
return parkEntranceMapPosition;
auto surfaceElement = map_get_surface_element_at(*mapX >> 5, *mapY >> 5);
*mapZ = surfaceElement->GetWaterHeight();
if (*mapZ == 0)
auto surfaceElement = map_get_surface_element_at(mapCoords);
parkEntranceMapPosition.z = surfaceElement->GetWaterHeight() * 8;
if (parkEntranceMapPosition.z == 0)
{
*mapZ = surfaceElement->base_height / 2;
parkEntranceMapPosition.z = surfaceElement->base_height * 8;
if ((surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_ALL_CORNERS_UP) != 0)
{
(*mapZ)++;
parkEntranceMapPosition.z += 16;
if (surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_DOUBLE_HEIGHT)
{
(*mapZ)++;
parkEntranceMapPosition.z += 16;
}
}
}
*direction = (gWindowSceneryRotation - get_current_rotation()) & 3;
parkEntranceMapPosition.direction = (gWindowSceneryRotation - get_current_rotation()) & 3;
return parkEntranceMapPosition;
}
/**
*
* rct2: 0x00666FD0
*/
static void window_map_place_park_entrance_tool_update(int32_t x, int32_t y)
static void window_map_place_park_entrance_tool_update(ScreenCoordsXY screenCoords)
{
int16_t mapX, mapY, mapZ = 0;
int32_t direction = 0, sideDirection;
int32_t sideDirection;
map_invalidate_selection_rect();
map_invalidate_map_selection_tiles();
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_CONSTRUCT;
place_park_entrance_get_map_position(x, y, &mapX, &mapY, &mapZ, &direction);
if (mapX == (int16_t)-1)
CoordsXYZD parkEntrancePosition = place_park_entrance_get_map_position(screenCoords);
if (parkEntrancePosition.x == LOCATION_NULL)
{
park_entrance_remove_ghost();
return;
}
sideDirection = (direction + 1) & 3;
sideDirection = (parkEntrancePosition.direction + 1) & 3;
gMapSelectionTiles.clear();
gMapSelectionTiles.push_back({ mapX, mapY });
gMapSelectionTiles.push_back(
{ mapX + CoordsDirectionDelta[sideDirection].x, mapY + CoordsDirectionDelta[sideDirection].y });
gMapSelectionTiles.push_back(
{ mapX - CoordsDirectionDelta[sideDirection].x, mapY - CoordsDirectionDelta[sideDirection].y });
gMapSelectionTiles.push_back({ parkEntrancePosition.x, parkEntrancePosition.y });
gMapSelectionTiles.push_back({ parkEntrancePosition.x + CoordsDirectionDelta[sideDirection].x,
parkEntrancePosition.y + CoordsDirectionDelta[sideDirection].y });
gMapSelectionTiles.push_back({ parkEntrancePosition.x - CoordsDirectionDelta[sideDirection].x,
parkEntrancePosition.y - CoordsDirectionDelta[sideDirection].y });
gMapSelectArrowPosition.x = mapX;
gMapSelectArrowPosition.y = mapY;
gMapSelectArrowPosition.z = mapZ * 16;
gMapSelectArrowDirection = direction;
gMapSelectArrowPosition.x = parkEntrancePosition.x;
gMapSelectArrowPosition.y = parkEntrancePosition.y;
gMapSelectArrowPosition.z = parkEntrancePosition.z;
gMapSelectArrowDirection = parkEntrancePosition.direction;
gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE_CONSTRUCT | MAP_SELECT_FLAG_ENABLE_ARROW;
map_invalidate_map_selection_tiles();
if (gParkEntranceGhostExists && mapX == gParkEntranceGhostPosition.x && mapY == gParkEntranceGhostPosition.y
&& direction == gParkEntranceGhostDirection)
if (gParkEntranceGhostExists && parkEntrancePosition.x == gParkEntranceGhostPosition.x
&& parkEntrancePosition.y == gParkEntranceGhostPosition.y
&& parkEntrancePosition.direction == gParkEntranceGhostDirection)
{
return;
}
park_entrance_remove_ghost();
park_entrance_place_ghost(mapX, mapY, mapZ, direction);
park_entrance_place_ghost(
parkEntrancePosition.x, parkEntrancePosition.y, parkEntrancePosition.z / 16, parkEntrancePosition.direction);
}
/**
*
* rct2: 0x0068D4E9
*/
static void window_map_set_peep_spawn_tool_update(int32_t x, int32_t y)
static void window_map_set_peep_spawn_tool_update(ScreenCoordsXY screenCoords)
{
int32_t mapX, mapY, mapZ, direction;
TileElement* tileElement;
@ -1270,7 +1273,7 @@ static void window_map_set_peep_spawn_tool_update(int32_t x, int32_t y)
map_invalidate_selection_rect();
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
footpath_bridge_get_info_from_pos({ x, y }, &mapX, &mapY, &direction, &tileElement);
footpath_bridge_get_info_from_pos(screenCoords, &mapX, &mapY, &direction, &tileElement);
if ((mapX & 0xFFFF) == 0x8000)
return;
@ -1301,16 +1304,15 @@ static void window_map_set_peep_spawn_tool_update(int32_t x, int32_t y)
*
* rct2: 0x006670A4
*/
static void window_map_place_park_entrance_tool_down(int32_t x, int32_t y)
static void window_map_place_park_entrance_tool_down(ScreenCoordsXY screenCoords)
{
park_entrance_remove_ghost();
int16_t mapX, mapY, mapZ;
int32_t direction;
place_park_entrance_get_map_position(x, y, &mapX, &mapY, &mapZ, &direction);
if (mapX != LOCATION_NULL)
CoordsXYZD parkEntrancePosition = place_park_entrance_get_map_position(screenCoords);
if (parkEntrancePosition.x != LOCATION_NULL)
{
money32 price = place_park_entrance(mapX, mapY, mapZ, direction);
money32 price = place_park_entrance(
parkEntrancePosition.x, parkEntrancePosition.y, parkEntrancePosition.z / 16, parkEntrancePosition.direction);
if (price != MONEY32_UNDEFINED)
{
audio_play_sound_at_location(SoundId::PlaceItem, { gCommandPosition.x, gCommandPosition.y, gCommandPosition.z });
@ -1322,13 +1324,13 @@ static void window_map_place_park_entrance_tool_down(int32_t x, int32_t y)
*
* rct2: 0x0068D573
*/
static void window_map_set_peep_spawn_tool_down(int32_t x, int32_t y)
static void window_map_set_peep_spawn_tool_down(ScreenCoordsXY screenCoords)
{
TileElement* tileElement;
int32_t mapX, mapY, mapZ, direction;
// Verify footpath exists at location, and retrieve coordinates
footpath_get_coordinates_from_pos({ x, y }, &mapX, &mapY, &direction, &tileElement);
footpath_get_coordinates_from_pos(screenCoords, &mapX, &mapY, &direction, &tileElement);
if (mapX == LOCATION_NULL)
return;

View File

@ -2176,7 +2176,9 @@ static bool ride_get_place_position_from_screen_position(int32_t screenX, int32_
if (!_trackPlaceCtrlState)
{
sub_68A15E(screenX, screenY, &mapX, &mapY);
const CoordsXY mapCoords = sub_68A15E({ screenX, screenY });
mapX = mapCoords.x;
mapY = mapCoords.y;
if (mapX == LOCATION_NULL)
return false;

View File

@ -1585,7 +1585,9 @@ static void sub_6E1F34(
// If CTRL not pressed
if (!gSceneryCtrlPressed)
{
sub_68A15E(x, y, grid_x, grid_y);
const CoordsXY mapCoords = sub_68A15E({ x, y });
*grid_x = mapCoords.x;
*grid_y = mapCoords.y;
if (*grid_x == LOCATION_NULL)
return;

View File

@ -246,7 +246,7 @@ static void window_track_place_update(rct_window* w)
*/
static void window_track_place_toolupdate(rct_window* w, rct_widgetindex widgetIndex, ScreenCoordsXY screenCoords)
{
int16_t mapX, mapY, mapZ;
int16_t mapZ;
map_invalidate_map_selection_tiles();
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE;
@ -254,24 +254,25 @@ static void window_track_place_toolupdate(rct_window* w, rct_widgetindex widgetI
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
// Get the tool map position
sub_68A15E(screenCoords.x, screenCoords.y, &mapX, &mapY);
if (mapX == LOCATION_NULL)
CoordsXY mapCoords = sub_68A15E(screenCoords);
if (mapCoords.x == LOCATION_NULL)
{
window_track_place_clear_provisional();
return;
}
// Check if tool map position has changed since last update
if (mapX == _window_track_place_last_x && mapY == _window_track_place_last_y)
if (mapCoords.x == _window_track_place_last_x && mapCoords.y == _window_track_place_last_y)
{
place_virtual_track(_trackDesign.get(), PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(0), mapX, mapY, 0);
place_virtual_track(
_trackDesign.get(), PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(0), mapCoords.x, mapCoords.y, 0);
return;
}
money32 cost = MONEY32_UNDEFINED;
// Get base Z position
mapZ = window_track_place_get_base_z(mapX, mapY);
mapZ = window_track_place_get_base_z(mapCoords.x, mapCoords.y);
if (game_is_not_paused() || gCheatsBuildInPauseMode)
{
window_track_place_clear_provisional();
@ -281,12 +282,12 @@ static void window_track_place_toolupdate(rct_window* w, rct_widgetindex widgetI
{
ride_id_t rideIndex;
uint16_t flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_NO_SPEND | GAME_COMMAND_FLAG_GHOST;
window_track_place_attempt_placement(_trackDesign.get(), mapX, mapY, mapZ, flags, &cost, &rideIndex);
window_track_place_attempt_placement(_trackDesign.get(), mapCoords.x, mapCoords.y, mapZ, flags, &cost, &rideIndex);
if (cost != MONEY32_UNDEFINED)
{
_window_track_place_ride_index = rideIndex;
_window_track_place_last_valid_x = mapX;
_window_track_place_last_valid_y = mapY;
_window_track_place_last_valid_x = mapCoords.x;
_window_track_place_last_valid_y = mapCoords.y;
_window_track_place_last_valid_z = mapZ;
_window_track_place_last_was_valid = true;
break;
@ -295,15 +296,16 @@ static void window_track_place_toolupdate(rct_window* w, rct_widgetindex widgetI
}
}
_window_track_place_last_x = mapX;
_window_track_place_last_y = mapY;
_window_track_place_last_x = mapCoords.x;
_window_track_place_last_y = mapCoords.y;
if (cost != _window_track_place_last_cost)
{
_window_track_place_last_cost = cost;
widget_invalidate(w, WIDX_PRICE);
}
place_virtual_track(_trackDesign.get(), PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(0), mapX, mapY, mapZ);
place_virtual_track(
_trackDesign.get(), PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(0), mapCoords.x, mapCoords.y, mapZ);
}
/**
@ -318,19 +320,18 @@ static void window_track_place_tooldown(rct_window* w, rct_widgetindex widgetInd
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_CONSTRUCT;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
int16_t mapX, mapY;
sub_68A15E(screenCoords.x, screenCoords.y, &mapX, &mapY);
if (mapX == LOCATION_NULL)
const CoordsXY mapCoords = sub_68A15E(screenCoords);
if (mapCoords.x == LOCATION_NULL)
return;
// Try increasing Z until a feasible placement is found
int16_t mapZ = window_track_place_get_base_z(mapX, mapY);
int16_t mapZ = window_track_place_get_base_z(mapCoords.x, mapCoords.y);
for (int32_t i = 0; i < 7; i++)
{
gDisableErrorWindowSound = true;
money32 cost = MONEY32_UNDEFINED;
ride_id_t rideIndex = RIDE_ID_NULL;
window_track_place_attempt_placement(_trackDesign.get(), mapX, mapY, mapZ, 1, &cost, &rideIndex);
window_track_place_attempt_placement(_trackDesign.get(), mapCoords.x, mapCoords.y, mapZ, 1, &cost, &rideIndex);
gDisableErrorWindowSound = false;
if (cost != MONEY32_UNDEFINED)
@ -339,7 +340,7 @@ static void window_track_place_tooldown(rct_window* w, rct_widgetindex widgetInd
if (ride != nullptr)
{
window_close_by_class(WC_ERROR);
audio_play_sound_at_location(SoundId::PlaceItem, { mapX, mapY, mapZ });
audio_play_sound_at_location(SoundId::PlaceItem, { mapCoords.x, mapCoords.y, mapZ });
_currentRideIndex = rideIndex;
if (track_design_are_entrance_and_exit_placed())
@ -367,7 +368,7 @@ static void window_track_place_tooldown(rct_window* w, rct_widgetindex widgetInd
}
// Unable to build track
audio_play_sound_at_location(SoundId::Error, { mapX, mapY, mapZ });
audio_play_sound_at_location(SoundId::Error, { mapCoords.x, mapCoords.y, mapZ });
}
/**

View File

@ -168,7 +168,7 @@ int32_t viewport_interaction_get_item_right(int32_t x, int32_t y, viewport_inter
int32_t viewport_interaction_right_over(int32_t x, int32_t y);
int32_t viewport_interaction_right_click(int32_t x, int32_t y);
void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y);
CoordsXY sub_68A15E(ScreenCoordsXY screenCoords);
void sub_68B2B7(paint_session* session, int32_t x, int32_t y);
void viewport_invalidate(rct_viewport* viewport, int32_t left, int32_t top, int32_t right, int32_t bottom);