Skip to content

Commit

Permalink
Use CoordsXYZ for ride view (#15434)
Browse files Browse the repository at this point in the history
* Use CoordsXYZ for ride view

Part of the NSF but also took the time to refactor slightly to use the more appropriate types. Annoyingly there isn't a operator/ for CoordsXYZ otherwise this could have been further simplified.

* Fix missed minx/maxx occurrence

Co-authored-by: Michael Steenbeek <m.o.steenbeek@gmail.com>
  • Loading branch information
duncanspumpkin and Gymnasiast committed Sep 19, 2021
1 parent 99e3883 commit 938792e
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/openrct2-ui/windows/Ride.cpp
Expand Up @@ -781,7 +781,7 @@ static std::unique_ptr<TrackDesign> _trackDesign;
// Cached overall view for each ride
// (Re)calculated when the ride window is opened
struct ride_overall_view {
int16_t x, y, z;
CoordsXYZ loc;
uint8_t zoom;
};

Expand Down Expand Up @@ -1167,10 +1167,10 @@ static void window_ride_update_overall_view(Ride* ride)

tile_element_iterator_begin(&it);

int32_t minx = std::numeric_limits<int32_t>::max(), miny = std::numeric_limits<int32_t>::max(),
minz = std::numeric_limits<int32_t>::max();
int32_t maxx = std::numeric_limits<int32_t>::min(), maxy = std::numeric_limits<int32_t>::min(),
maxz = std::numeric_limits<int32_t>::min();
CoordsXYZ min = { std::numeric_limits<int32_t>::max(), std::numeric_limits<int32_t>::max(),
std::numeric_limits<int32_t>::max() };
CoordsXYZ max = { std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::min() };

while (tile_element_iterator_next(&it))
{
Expand All @@ -1184,13 +1184,13 @@ static void window_ride_update_overall_view(Ride* ride)
int32_t baseZ = it.element->GetBaseZ();
int32_t clearZ = it.element->GetClearanceZ();

minx = std::min(minx, location.x);
miny = std::min(miny, location.y);
minz = std::min(minz, baseZ);
min.x = std::min(min.x, location.x);
min.y = std::min(min.y, location.y);
min.z = std::min(min.z, baseZ);

maxx = std::max(maxx, location.x);
maxy = std::max(maxy, location.y);
maxz = std::max(maxz, clearZ);
max.x = std::max(max.x, location.x);
max.y = std::max(max.y, location.y);
max.z = std::max(max.z, clearZ);
}

const auto rideIndex = EnumValue(ride->id);
Expand All @@ -1200,16 +1200,12 @@ static void window_ride_update_overall_view(Ride* ride)
}

auto& view = ride_overall_views[rideIndex];
view.x = (minx + maxx) / 2 + 16;
view.y = (miny + maxy) / 2 + 16;
view.z = (minz + maxz) / 2 - 8;
view.loc = CoordsXYZ{ (min.x + max.x) / 2, (min.y + max.y) / 2, (min.z + max.z) / 2 } + CoordsXYZ{ 16, 16, -8 };

// Calculate size to determine from how far away to view the ride
int32_t dx = maxx - minx;
int32_t dy = maxy - miny;
int32_t dz = maxz - minz;
const auto diff = max - min;

int32_t size = static_cast<int32_t>(std::sqrt(dx * dx + dy * dy + dz * dz));
const int32_t size = static_cast<int32_t>(std::sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z));

if (size >= 80)
{
Expand Down Expand Up @@ -1564,7 +1560,7 @@ static std::optional<StationIndex> GetStationIndexFromViewSelection(const rct_wi
return std::nullopt;
}

for (StationIndex index = 0; index < sizeof(ride->stations); ++index)
for (StationIndex index = 0; index < std::size(ride->stations); ++index)
{
const auto& station = ride->stations[index];
if (!station.Start.IsNull())
Expand Down Expand Up @@ -1634,8 +1630,7 @@ static void window_ride_init_viewport(rct_window* w)
if (w->number < ride_overall_views.size())
{
const auto& view = ride_overall_views[w->number];
CoordsXYZ loc = { view.x, view.y, view.z };
focus = Focus2(loc, view.zoom);
focus = Focus2(view.loc, view.zoom);
}
}

Expand Down

0 comments on commit 938792e

Please sign in to comment.