1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-23 14:54:30 +01:00

Refactor viewport_coord_to_map_coord to return a CoordXY

This commit is contained in:
duncanspumpkin
2019-10-30 20:24:13 +00:00
parent 9bf2cffbb7
commit 5ca553dd08
5 changed files with 27 additions and 30 deletions

View File

@@ -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;