From 1532ee2b2327e262038c0de29f677913c3f0ee15 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sat, 30 Nov 2019 12:47:23 -0300 Subject: [PATCH] Create CoordsXY::ToTileStart() and CoordsXY::ToTileCentre() --- .../interface/ViewportInteraction.cpp | 2 +- src/openrct2-ui/windows/LandRights.cpp | 3 +-- src/openrct2-ui/windows/RideConstruction.cpp | 2 +- src/openrct2-ui/windows/TopToolbar.cpp | 10 ++++------ src/openrct2/interface/Viewport.cpp | 12 +++++------- src/openrct2/peep/Guest.cpp | 6 ++---- src/openrct2/peep/Peep.cpp | 7 ++++--- src/openrct2/ride/Ride.cpp | 2 +- src/openrct2/world/Footpath.cpp | 6 ++++-- src/openrct2/world/Location.hpp | 16 ++++++++++++++++ 10 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/openrct2-ui/interface/ViewportInteraction.cpp b/src/openrct2-ui/interface/ViewportInteraction.cpp index beeeb3dd79..2199af7cfc 100644 --- a/src/openrct2-ui/interface/ViewportInteraction.cpp +++ b/src/openrct2-ui/interface/ViewportInteraction.cpp @@ -695,5 +695,5 @@ CoordsXY sub_68A15E(ScreenCoordsXY screenCoords) mapPos.y = std::clamp(mapPos.y, initialPos.y, initialPos.y + 31); } - return { mapPos.x & ~0x1F, mapPos.y & ~0x1F }; + return mapPos.ToTileStart(); } diff --git a/src/openrct2-ui/windows/LandRights.cpp b/src/openrct2-ui/windows/LandRights.cpp index e5005ffbd3..338af88cc6 100644 --- a/src/openrct2-ui/windows/LandRights.cpp +++ b/src/openrct2-ui/windows/LandRights.cpp @@ -330,8 +330,7 @@ static void window_land_rights_tool_update_land_rights(ScreenCoordsXY screenCoor // Move to tool bottom left mapTile->x -= (tool_size - 1) * 16; mapTile->y -= (tool_size - 1) * 16; - mapTile->x &= 0xFFE0; - mapTile->y &= 0xFFE0; + mapTile = mapTile->ToTileStart(); if (gMapSelectPositionA.x != mapTile->x) { diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index d7b72c21b6..2fbb235fbb 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -2212,7 +2212,7 @@ static std::optional ride_get_place_position_from_screen_position(Scre if (mapCoords.x == LOCATION_NULL) return std::nullopt; - return CoordsXY{ floor2(mapCoords.x, 32), floor2(mapCoords.y, 32) }; + return mapCoords.ToTileStart(); } /** diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index bf45f1af45..0604afaf3a 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -2048,8 +2048,7 @@ static uint8_t top_toolbar_tool_update_land_paint(int16_t x, int16_t y) // Move to tool bottom left mapTile->x -= (tool_size - 1) * 16; mapTile->y -= (tool_size - 1) * 16; - mapTile->x &= 0xFFE0; - mapTile->y &= 0xFFE0; + mapTile = mapTile->ToTileStart(); if (gMapSelectPositionA.x != mapTile->x) { @@ -2263,20 +2262,19 @@ static void top_toolbar_tool_update_land(int16_t x, int16_t y) case MAP_SELECT_TYPE_EDGE_2: // Line mapTile->y -= (tool_size - 1) * 16; - mapTile->y &= 0xFFE0; + mapTile->y = mapTile->ToTileStart().y; break; case MAP_SELECT_TYPE_EDGE_1: case MAP_SELECT_TYPE_EDGE_3: // Line mapTile->x -= (tool_size - 1) * 16; - mapTile->x &= 0xFFE0; + mapTile->x = mapTile->ToTileStart().x; break; default: // Move to tool bottom left mapTile->x -= (tool_size - 1) * 16; mapTile->y -= (tool_size - 1) * 16; - mapTile->x &= 0xFFE0; - mapTile->y &= 0xFFE0; + mapTile = mapTile->ToTileStart(); break; } diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index 6d2fe484a6..f7bbf965e8 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -1037,11 +1037,9 @@ CoordsXY screen_pos_to_map_pos(ScreenCoordsXY screenCoords, int32_t* direction) } } - mapCoords->x = mapCoords->x & ~0x1F; - mapCoords->y = mapCoords->y & ~0x1F; if (direction != nullptr) *direction = my_direction; - return *mapCoords; + return mapCoords->ToTileStart(); } LocationXY16 screen_coord_to_viewport_coord(rct_viewport* viewport, ScreenCoordsXY screenCoords) @@ -1815,7 +1813,7 @@ std::optional screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, return std::nullopt; *quadrant = map_get_tile_quadrant(mapCoords->x, mapCoords->y); - return CoordsXY(floor2(mapCoords->x, 32), floor2(mapCoords->y, 32)); + return mapCoords->ToTileStart(); } /** @@ -1829,7 +1827,7 @@ std::optional screen_get_map_xy_quadrant_with_z(ScreenCoordsXY screenC return std::nullopt; *quadrant = map_get_tile_quadrant(mapCoords->x, mapCoords->y); - return CoordsXY(floor2(mapCoords->x, 32), floor2(mapCoords->y, 32)); + return mapCoords->ToTileStart(); } /** @@ -1843,7 +1841,7 @@ std::optional screen_get_map_xy_side(ScreenCoordsXY screenCoords, uint return std::nullopt; *side = map_get_tile_side(mapCoords->x, mapCoords->y); - return CoordsXY(floor2(mapCoords->x, 32), floor2(mapCoords->y, 32)); + return mapCoords->ToTileStart(); } /** @@ -1857,7 +1855,7 @@ std::optional screen_get_map_xy_side_with_z(ScreenCoordsXY screenCoord return std::nullopt; *side = map_get_tile_side(mapCoords->x, mapCoords->y); - return CoordsXY(floor2(mapCoords->x, 32), floor2(mapCoords->y, 32)); + return mapCoords->ToTileStart(); } /** diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index 0d1b287bb1..42a87aec40 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -4981,13 +4981,11 @@ void Guest::UpdateRideMazePathfinding() targetLoc.y = destination_y; if (chosenEdge & 1) { - targetLoc.x &= 0xFFE0; - targetLoc.x += 16; + targetLoc.x = targetLoc.ToTileCentre().x; } else { - targetLoc.y &= 0xFFE0; - targetLoc.y += 16; + targetLoc.y = targetLoc.ToTileCentre().y; } destination_x = targetLoc.x; destination_y = targetLoc.y; diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 7a1f9c1f9b..f18243d660 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -3078,7 +3078,8 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result) } auto newLoc = *loc; - if ((newLoc.x & 0xFFE0) == next_x && (newLoc.y & 0xFFE0) == next_y) + CoordsXY truncatedNewLoc = newLoc.ToTileStart(); + if (truncatedNewLoc.x == next_x && truncatedNewLoc.y == next_y) { int16_t height = GetZOnSlope(newLoc.x, newLoc.y); MoveTo(newLoc.x, newLoc.y, height); @@ -3175,8 +3176,8 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result) } // The peep is on a surface and not on a path - next_x = newLoc.x & 0xFFE0; - next_y = newLoc.y & 0xFFE0; + next_x = truncatedNewLoc.x; + next_y = truncatedNewLoc.y; next_z = surfaceElement->base_height; SetNextFlags(0, false, true); diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 6e59eabb5f..722a0cfb9a 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -6227,7 +6227,7 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX word_F4418C = coords->x; word_F4418E = coords->y; - entranceExitCoords = { floor2(coords->x, 32), floor2(coords->y, 32), stationHeight, INVALID_DIRECTION }; + entranceExitCoords = { coords->ToTileStart(), stationHeight, INVALID_DIRECTION }; if (ride->type == RIDE_TYPE_NULL) { diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index f01f47383c..c2107c5554 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -323,10 +323,12 @@ void footpath_get_coordinates_from_pos( } } + position = position.ToTileStart(); + if (x != nullptr) - *x = position.x & ~0x1F; + *x = position.x; if (y != nullptr) - *y = position.y & ~0x1F; + *y = position.y; if (direction != nullptr) *direction = myDirection; if (tileElement != nullptr) diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 8bc9db8feb..4d3aa1e6ba 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -139,6 +139,16 @@ struct CoordsXY return rotatedCoords; } + + CoordsXY ToTileCentre() const + { + return ToTileStart() + CoordsXY{ 16, 16 }; + } + + CoordsXY ToTileStart() const + { + return { floor2(x, 32), floor2(y, 32) }; + } }; struct TileCoordsXY @@ -328,6 +338,12 @@ struct CoordsXYZD : public CoordsXYZ { } + constexpr CoordsXYZD(CoordsXY _c, int32_t _z, Direction _d) + : CoordsXYZ(_c, _z) + , direction(_d) + { + } + bool operator==(const CoordsXYZD& other) const { return x == other.x && y == other.y && z == other.z && direction == other.direction;