Refactor screen_get_map_xy_with_z to use CoordsXY structs

This commit is contained in:
duncanspumpkin 2019-11-14 20:53:24 +00:00
parent c04cb0b94b
commit 1d6a923298
5 changed files with 41 additions and 45 deletions

View File

@ -2111,15 +2111,14 @@ static void window_ride_construction_update(rct_window* w)
static std::optional<CoordsXY> ride_get_place_position_from_screen_position(ScreenCoordsXY screenCoords) static std::optional<CoordsXY> ride_get_place_position_from_screen_position(ScreenCoordsXY screenCoords)
{ {
CoordsXY mapCoords; CoordsXY mapCoords;
int16_t mapZ;
int32_t interactionType;
rct_viewport* viewport = nullptr;
if (!_trackPlaceCtrlState) if (!_trackPlaceCtrlState)
{ {
if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z) if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z)
{ {
TileElement* tileElement; TileElement* tileElement;
rct_viewport* viewport = nullptr;
int32_t interactionType;
get_map_coordinates_from_pos(screenCoords, 0xFCCA, mapCoords, &interactionType, &tileElement, &viewport); get_map_coordinates_from_pos(screenCoords, 0xFCCA, mapCoords, &interactionType, &tileElement, &viewport);
if (interactionType != 0) if (interactionType != 0)
{ {
@ -2184,7 +2183,7 @@ static std::optional<CoordsXY> ride_get_place_position_from_screen_position(Scre
auto surfaceElement = map_get_surface_element_at(mapCoords.x >> 5, mapCoords.y >> 5); auto surfaceElement = map_get_surface_element_at(mapCoords.x >> 5, mapCoords.y >> 5);
if (surfaceElement == nullptr) if (surfaceElement == nullptr)
return std::nullopt; return std::nullopt;
mapZ = floor2(surfaceElement->base_height * 8, 16); auto mapZ = floor2(surfaceElement->base_height * 8, 16);
mapZ += _trackPlaceShiftZ; mapZ += _trackPlaceShiftZ;
mapZ = std::max<int16_t>(mapZ, 16); mapZ = std::max<int16_t>(mapZ, 16);
_trackPlaceZ = mapZ; _trackPlaceZ = mapZ;
@ -2192,10 +2191,9 @@ static std::optional<CoordsXY> ride_get_place_position_from_screen_position(Scre
} }
else else
{ {
int16_t mapX, mapY; auto mapZ = _trackPlaceCtrlZ;
mapZ = _trackPlaceCtrlZ; mapCoords = screen_get_map_xy_with_z(screenCoords, mapZ);
screen_get_map_xy_with_z(screenCoords.x, screenCoords.y, mapZ, &mapX, &mapY);
mapCoords = { mapX, mapY };
if (_trackPlaceShiftState != 0) if (_trackPlaceShiftState != 0)
{ {
mapZ += _trackPlaceShiftZ; mapZ += _trackPlaceShiftZ;

View File

@ -1456,8 +1456,9 @@ static void sub_6E1F34(
else else
{ {
int16_t z = gSceneryCtrlPressZ; int16_t z = gSceneryCtrlPressZ;
screen_get_map_xy_with_z(x, y, z, grid_x, grid_y); auto coords = screen_get_map_xy_with_z({ x, y }, z);
*grid_x = coords.x;
*grid_y = coords.y;
// If SHIFT pressed // If SHIFT pressed
if (gSceneryShiftPressed) if (gSceneryShiftPressed)
{ {
@ -1616,7 +1617,9 @@ static void sub_6E1F34(
else else
{ {
int16_t z = gSceneryCtrlPressZ; int16_t z = gSceneryCtrlPressZ;
screen_get_map_xy_with_z(x, y, z, grid_x, grid_y); auto coords = screen_get_map_xy_with_z({ x, y }, z);
*grid_x = coords.x;
*grid_y = coords.y;
// If SHIFT pressed // If SHIFT pressed
if (gSceneryShiftPressed) if (gSceneryShiftPressed)

View File

@ -1806,27 +1806,23 @@ CoordsXY screen_get_map_xy(ScreenCoordsXY screenCoords, rct_viewport** viewport)
* *
* rct2: 0x006894D4 * rct2: 0x006894D4
*/ */
void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY) CoordsXY screen_get_map_xy_with_z(ScreenCoordsXY screenCoords, int16_t z)
{ {
rct_viewport* viewport = viewport_find_from_point(screenX, screenY); CoordsXY ret{ LOCATION_NULL, 0 };
rct_viewport* viewport = viewport_find_from_point(screenCoords.x, screenCoords.y);
if (viewport == nullptr) if (viewport == nullptr)
{ {
*mapX = LOCATION_NULL; return ret;
return;
} }
screenX = viewport->view_x + ((screenX - viewport->x) << viewport->zoom); auto vpCoords = screen_coord_to_viewport_coord(viewport, screenCoords);
screenY = viewport->view_y + ((screenY - viewport->y) << viewport->zoom); auto mapPosition = viewport_coord_to_map_coord(vpCoords.x, vpCoords.y, z);
auto mapPosition = viewport_coord_to_map_coord(screenX, screenY + z, 0);
if (mapPosition.x < 0 || mapPosition.x >= (256 * 32) || mapPosition.y < 0 || mapPosition.y > (256 * 32)) if (mapPosition.x < 0 || mapPosition.x >= (256 * 32) || mapPosition.y < 0 || mapPosition.y > (256 * 32))
{ {
*mapX = LOCATION_NULL; return ret;
return;
} }
*mapX = mapPosition.x; return mapPosition;
*mapY = mapPosition.y;
} }
/** /**
@ -1850,13 +1846,13 @@ CoordsXY screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, uint8_t* quadra
void screen_get_map_xy_quadrant_with_z( void screen_get_map_xy_quadrant_with_z(
int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant) int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant)
{ {
screen_get_map_xy_with_z(screenX, screenY, z, mapX, mapY); auto coords = screen_get_map_xy_with_z({ screenX, screenY }, z);
if (*mapX == LOCATION_NULL) if (coords.x == LOCATION_NULL)
return; return;
*quadrant = map_get_tile_quadrant(*mapX, *mapY); *quadrant = map_get_tile_quadrant(coords.x, coords.y);
*mapX = floor2(*mapX, 32); *mapX = floor2(coords.x, 32);
*mapY = floor2(*mapY, 32); *mapY = floor2(coords.y, 32);
} }
/** /**
@ -1879,13 +1875,13 @@ CoordsXY screen_get_map_xy_side(ScreenCoordsXY screenCoords, uint8_t* side)
*/ */
void screen_get_map_xy_side_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* side) void screen_get_map_xy_side_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* side)
{ {
screen_get_map_xy_with_z(screenX, screenY, z, mapX, mapY); auto coords = screen_get_map_xy_with_z({ screenX, screenY }, z);
if (*mapX == LOCATION_NULL) if (coords.x == LOCATION_NULL)
return; return;
*side = map_get_tile_side(*mapX, *mapY); *side = map_get_tile_side(coords.x, coords.y);
*mapX = floor2(*mapX, 32); *mapX = floor2(coords.x, 32);
*mapY = floor2(*mapY, 32); *mapY = floor2(coords.y, 32);
} }
/** /**

View File

@ -174,7 +174,7 @@ void sub_68B2B7(paint_session* session, CoordsXY mapCoords);
void viewport_invalidate(rct_viewport* viewport, int32_t left, int32_t top, int32_t right, int32_t bottom); void viewport_invalidate(rct_viewport* viewport, int32_t left, int32_t top, int32_t right, int32_t bottom);
CoordsXY screen_get_map_xy(ScreenCoordsXY screenCoords, rct_viewport** viewport); CoordsXY screen_get_map_xy(ScreenCoordsXY screenCoords, rct_viewport** viewport);
void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY); CoordsXY screen_get_map_xy_with_z(ScreenCoordsXY screenCoords, int16_t z);
CoordsXY screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, uint8_t* quadrant); CoordsXY screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, uint8_t* quadrant);
void screen_get_map_xy_quadrant_with_z( void screen_get_map_xy_quadrant_with_z(
int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant); int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant);

View File

@ -6176,7 +6176,6 @@ static int32_t loc_6CD18E(
*/ */
CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsXY screenCoords) CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsXY screenCoords)
{ {
int16_t mapX, mapY;
int16_t entranceMinX, entranceMinY, entranceMaxX, entranceMaxY, word_F4418C, word_F4418E; int16_t entranceMinX, entranceMinY, entranceMaxX, entranceMaxY, word_F4418C, word_F4418E;
int32_t interactionType, stationDirection; int32_t interactionType, stationDirection;
uint8_t stationHeight; uint8_t stationHeight;
@ -6187,7 +6186,7 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX
gRideEntranceExitPlaceDirection = 255; gRideEntranceExitPlaceDirection = 255;
get_map_coordinates_from_pos( get_map_coordinates_from_pos(
screenCoords.x, screenCoords.y, 0xFFFB, &mapX, &mapY, &interactionType, &tileElement, &viewport); screenCoords.x, screenCoords.y, 0xFFFB, nullptr, nullptr, &interactionType, &tileElement, &viewport);
if (interactionType != 0) if (interactionType != 0)
{ {
if (tileElement->GetType() == TILE_ELEMENT_TYPE_TRACK) if (tileElement->GetType() == TILE_ELEMENT_TYPE_TRACK)
@ -6218,17 +6217,17 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX
stationHeight = ride->stations[gRideEntranceExitPlaceStationIndex].Height; stationHeight = ride->stations[gRideEntranceExitPlaceStationIndex].Height;
screen_get_map_xy_with_z(screenCoords.x, screenCoords.y, stationHeight * 8, &mapX, &mapY); auto coords = screen_get_map_xy_with_z(screenCoords, stationHeight * 8);
if (mapX == LOCATION_NULL) if (coords.x == LOCATION_NULL)
{ {
entranceExitCoords.x = LOCATION_NULL; entranceExitCoords.x = LOCATION_NULL;
return entranceExitCoords; return entranceExitCoords;
} }
word_F4418C = mapX; word_F4418C = coords.x;
word_F4418E = mapY; word_F4418E = coords.y;
entranceExitCoords = { floor2(mapX, 32), floor2(mapY, 32), stationHeight, INVALID_DIRECTION }; entranceExitCoords = { floor2(coords.x, 32), floor2(coords.y, 32), stationHeight, INVALID_DIRECTION };
if (ride->type == RIDE_TYPE_NULL) if (ride->type == RIDE_TYPE_NULL)
{ {
@ -6245,8 +6244,8 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX
if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_3)) if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_3))
{ {
mapX = (word_F4418C & 0x1F) - 16; auto mapX = (word_F4418C & 0x1F) - 16;
mapY = (word_F4418E & 0x1F) - 16; auto mapY = (word_F4418E & 0x1F) - 16;
if (std::abs(mapX) < std::abs(mapY)) if (std::abs(mapX) < std::abs(mapY))
{ {
entranceExitCoords.direction = mapY < 0 ? 3 : 1; entranceExitCoords.direction = mapY < 0 ? 3 : 1;
@ -6300,8 +6299,8 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX
} }
else else
{ {
mapX = stationStart.x * 32; auto mapX = stationStart.x * 32;
mapY = stationStart.y * 32; auto mapY = stationStart.y * 32;
entranceMinX = mapX; entranceMinX = mapX;
entranceMinY = mapY; entranceMinY = mapY;