mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 19:13:07 +01:00
Refactor viewport_coord_to_map_coord to return a CoordXY
This commit is contained in:
@@ -1612,10 +1612,9 @@ void input_scroll_viewport(ScreenCoordsXY scrollScreenCoords)
|
||||
int32_t x = mainWindow->saved_view_x + viewport->view_width / 2 + dx;
|
||||
int32_t y = mainWindow->saved_view_y + viewport->view_height / 2;
|
||||
int32_t y_dy = mainWindow->saved_view_y + viewport->view_height / 2 + dy;
|
||||
LocationXY16 mapCoord, mapCoord_dy;
|
||||
|
||||
mapCoord = viewport_coord_to_map_coord(x, y, 0);
|
||||
mapCoord_dy = viewport_coord_to_map_coord(x, y_dy, 0);
|
||||
auto mapCoord = viewport_coord_to_map_coord(x, y, 0);
|
||||
auto mapCoord_dy = viewport_coord_to_map_coord(x, y_dy, 0);
|
||||
|
||||
// Check if we're crossing the boundary
|
||||
// Clamp to the map minimum value
|
||||
|
||||
@@ -684,18 +684,18 @@ void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y)
|
||||
}
|
||||
|
||||
LocationXY16 initialVPPos = screen_coord_to_viewport_coord(viewport, screenX, screenY);
|
||||
LocationXY16 mapPos = { (int16_t)(initialPos.x + 16), (int16_t)(initialPos.y + 16) };
|
||||
CoordsXY mapPos = initialPos + CoordsXY{ 16, 16 };
|
||||
|
||||
for (int32_t i = 0; i < 5; i++)
|
||||
{
|
||||
int16_t z = waterHeight;
|
||||
if (interactionType != VIEWPORT_INTERACTION_ITEM_WATER)
|
||||
{
|
||||
z = tile_element_height({ mapPos.x, mapPos.y });
|
||||
z = tile_element_height(mapPos);
|
||||
}
|
||||
mapPos = viewport_coord_to_map_coord(initialVPPos.x, initialVPPos.y, z);
|
||||
mapPos.x = std::clamp<int16_t>(mapPos.x, initialPos.x, initialPos.x + 31);
|
||||
mapPos.y = std::clamp<int16_t>(mapPos.y, initialPos.y, initialPos.y + 31);
|
||||
mapPos.x = std::clamp(mapPos.x, initialPos.x, initialPos.x + 31);
|
||||
mapPos.y = std::clamp(mapPos.y, initialPos.y, initialPos.y + 31);
|
||||
}
|
||||
|
||||
*x = mapPos.x & ~0x1F;
|
||||
|
||||
@@ -219,21 +219,20 @@ void viewport_adjust_for_map_height(int16_t* x, int16_t* y, int16_t* z)
|
||||
int16_t height = 0;
|
||||
|
||||
uint32_t rotation = get_current_rotation();
|
||||
LocationXY16 pos;
|
||||
CoordsXY pos{};
|
||||
for (int32_t i = 0; i < 6; i++)
|
||||
{
|
||||
pos = viewport_coord_to_map_coord(start_x, start_y, height);
|
||||
height = tile_element_height({ (0xFFFF) & pos.x, (0xFFFF) & pos.y });
|
||||
height = tile_element_height(pos);
|
||||
|
||||
// HACK: This is to prevent the x and y values being set to values outside
|
||||
// of the map. This can happen when the height is larger than the map size.
|
||||
int16_t max = gMapSizeMinus2;
|
||||
if (pos.x > max && pos.y > max)
|
||||
{
|
||||
int32_t x_corr[] = { -1, 1, 1, -1 };
|
||||
int32_t y_corr[] = { -1, -1, 1, 1 };
|
||||
pos.x += x_corr[rotation] * height;
|
||||
pos.y += y_corr[rotation] * height;
|
||||
const CoordsXY corr[] = { { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } };
|
||||
pos.x += corr[rotation].x * height;
|
||||
pos.y += corr[rotation].y * height;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,9 +556,8 @@ void viewport_update_position(rct_window* window)
|
||||
|
||||
int16_t x = window->saved_view_x + viewport->view_width / 2;
|
||||
int16_t y = window->saved_view_y + viewport->view_height / 2;
|
||||
LocationXY16 mapCoord;
|
||||
|
||||
mapCoord = viewport_coord_to_map_coord(x, y, 0);
|
||||
auto mapCoord = viewport_coord_to_map_coord(x, y, 0);
|
||||
|
||||
// Clamp to the map minimum value
|
||||
int32_t at_map_edge = 0;
|
||||
@@ -1052,9 +1050,9 @@ LocationXY16 screen_coord_to_viewport_coord(rct_viewport* viewport, uint16_t x,
|
||||
return ret;
|
||||
}
|
||||
|
||||
LocationXY16 viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z)
|
||||
CoordsXY viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z)
|
||||
{
|
||||
LocationXY16 ret = {};
|
||||
CoordsXY ret{};
|
||||
switch (get_current_rotation())
|
||||
{
|
||||
case 0:
|
||||
@@ -1766,11 +1764,11 @@ void screen_get_map_xy(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y,
|
||||
}
|
||||
|
||||
LocationXY16 start_vp_pos = screen_coord_to_viewport_coord(myViewport, screenX, screenY);
|
||||
LocationXY16 map_pos = { (int16_t)(my_x + 16), (int16_t)(my_y + 16) };
|
||||
CoordsXY map_pos = { my_x + 16, my_y + 16 };
|
||||
|
||||
for (int32_t i = 0; i < 5; i++)
|
||||
{
|
||||
int32_t z = tile_element_height({ map_pos.x, map_pos.y });
|
||||
int32_t z = tile_element_height(map_pos);
|
||||
map_pos = viewport_coord_to_map_coord(start_vp_pos.x, start_vp_pos.y, z);
|
||||
map_pos.x = std::clamp<int16_t>(map_pos.x, my_x, my_x + 31);
|
||||
map_pos.y = std::clamp<int16_t>(map_pos.y, my_y, my_y + 31);
|
||||
@@ -1799,7 +1797,7 @@ void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16
|
||||
screenX = viewport->view_x + ((screenX - viewport->x) << viewport->zoom);
|
||||
screenY = viewport->view_y + ((screenY - viewport->y) << viewport->zoom);
|
||||
|
||||
LocationXY16 mapPosition = viewport_coord_to_map_coord(screenX, screenY + z, 0);
|
||||
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))
|
||||
{
|
||||
*mapX = LOCATION_NULL;
|
||||
|
||||
@@ -140,7 +140,7 @@ void viewport_paint(
|
||||
void viewport_adjust_for_map_height(int16_t* x, int16_t* y, int16_t* z);
|
||||
|
||||
LocationXY16 screen_coord_to_viewport_coord(rct_viewport* viewport, uint16_t x, uint16_t y);
|
||||
LocationXY16 viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z);
|
||||
CoordsXY viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z);
|
||||
void screen_pos_to_map_pos(int16_t* x, int16_t* y, int32_t* direction);
|
||||
|
||||
void show_gridlines();
|
||||
|
||||
@@ -252,17 +252,17 @@ void footpath_get_coordinates_from_pos(
|
||||
int32_t z = 0, interactionType;
|
||||
TileElement* myTileElement;
|
||||
rct_viewport* viewport;
|
||||
LocationXY16 position = {};
|
||||
LocationXY16 position16 = {};
|
||||
|
||||
get_map_coordinates_from_pos(
|
||||
screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH, &position.x, &position.y, &interactionType, &myTileElement,
|
||||
screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH, &position16.x, &position16.y, &interactionType, &myTileElement,
|
||||
&viewport);
|
||||
if (interactionType != VIEWPORT_INTERACTION_ITEM_FOOTPATH
|
||||
|| !(viewport->flags & (VIEWPORT_FLAG_UNDERGROUND_INSIDE | VIEWPORT_FLAG_HIDE_BASE | VIEWPORT_FLAG_HIDE_VERTICAL)))
|
||||
{
|
||||
get_map_coordinates_from_pos(
|
||||
screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH & VIEWPORT_INTERACTION_MASK_TERRAIN, &position.x, &position.y,
|
||||
&interactionType, &myTileElement, &viewport);
|
||||
screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH & VIEWPORT_INTERACTION_MASK_TERRAIN, &position16.x,
|
||||
&position16.y, &interactionType, &myTileElement, &viewport);
|
||||
if (interactionType == VIEWPORT_INTERACTION_ITEM_NONE)
|
||||
{
|
||||
if (x != nullptr)
|
||||
@@ -271,11 +271,11 @@ void footpath_get_coordinates_from_pos(
|
||||
}
|
||||
}
|
||||
|
||||
LocationXY16 minPosition = position;
|
||||
LocationXY16 maxPosition = { int16_t(position.x + 31), int16_t(position.y + 31) };
|
||||
CoordsXY position = { position16.x, position16.y };
|
||||
auto minPosition = position;
|
||||
auto maxPosition = position + CoordsXY{ 31, 31 };
|
||||
|
||||
position.x += 16;
|
||||
position.y += 16;
|
||||
position += CoordsXY{ 16, 16 };
|
||||
|
||||
if (interactionType == VIEWPORT_INTERACTION_ITEM_FOOTPATH)
|
||||
{
|
||||
@@ -292,7 +292,7 @@ void footpath_get_coordinates_from_pos(
|
||||
{
|
||||
if (interactionType != VIEWPORT_INTERACTION_ITEM_FOOTPATH)
|
||||
{
|
||||
z = tile_element_height({ position.x, position.y });
|
||||
z = tile_element_height(position);
|
||||
}
|
||||
position = viewport_coord_to_map_coord(start_vp_pos.x, start_vp_pos.y, z);
|
||||
position.x = std::clamp(position.x, minPosition.x, maxPosition.x);
|
||||
|
||||
Reference in New Issue
Block a user