Use std::optional instead of std::pair

This commit is contained in:
Gymnasiast 2021-08-08 12:27:06 +02:00
parent 72404f1b85
commit c071b61d3e
No known key found for this signature in database
GPG Key ID: DBFFF47AB2CA3EDD
1 changed files with 55 additions and 51 deletions

View File

@ -964,24 +964,30 @@ static bool TrackDesignPlaceSceneryElementGetPlaceZ(const TrackDesignSceneryElem
return true; return true;
} }
static std::pair<bool, money32> TrackDesignPlaceSceneryElement( static std::optional<money32> TrackDesignPlaceSceneryElement(
CoordsXY mapCoord, uint8_t mode, const TrackDesignSceneryElement& scenery, uint8_t rotation, int32_t originZ, CoordsXY mapCoord, uint8_t mode, const TrackDesignSceneryElement& scenery, uint8_t rotation, int32_t originZ,
money32 totalCost) money32 totalCost)
{ {
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES && mode == 0) if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES && mode == 0)
{ {
track_design_add_selection_tile(mapCoord); track_design_add_selection_tile(mapCoord);
return std::pair(true, totalCost); return totalCost;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST && mode == 0) if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST && mode == 0)
{ {
return std::pair(TrackDesignPlaceSceneryElementRemoveGhost(mapCoord, scenery, rotation, originZ), totalCost); if (TrackDesignPlaceSceneryElementRemoveGhost(mapCoord, scenery, rotation, originZ))
return totalCost;
return std::nullopt;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z) if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{ {
return std::pair(TrackDesignPlaceSceneryElementGetPlaceZ(scenery), totalCost); if (TrackDesignPlaceSceneryElementGetPlaceZ(scenery))
return totalCost;
return std::nullopt;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY || _trackDesignPlaceOperation == PTD_OPERATION_PLACE if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY || _trackDesignPlaceOperation == PTD_OPERATION_PLACE
@ -992,7 +998,7 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
ObjectEntryIndex entry_index; ObjectEntryIndex entry_index;
if (TrackDesignPlaceSceneryElementGetEntry(entry_type, entry_index, scenery)) if (TrackDesignPlaceSceneryElementGetEntry(entry_type, entry_index, scenery))
{ {
return std::pair(true, totalCost); return totalCost;
} }
money32 cost; money32 cost;
@ -1006,11 +1012,11 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
{ {
if (mode != 0) if (mode != 0)
{ {
return std::pair(true, totalCost); return totalCost;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z) if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{ {
return std::pair(true, totalCost); return totalCost;
} }
rotation += scenery.flags; rotation += scenery.flags;
@ -1054,11 +1060,11 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
{ {
if (mode != 0) if (mode != 0)
{ {
return std::pair(true, totalCost); return totalCost;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z) if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{ {
return std::pair(true, totalCost); return totalCost;
} }
rotation += scenery.flags; rotation += scenery.flags;
@ -1098,11 +1104,11 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
{ {
if (mode != 0) if (mode != 0)
{ {
return std::pair(true, totalCost); return totalCost;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z) if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{ {
return std::pair(true, totalCost); return totalCost;
} }
z = scenery.z * COORDS_Z_STEP + originZ; z = scenery.z * COORDS_Z_STEP + originZ;
@ -1141,7 +1147,7 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
case ObjectType::Paths: case ObjectType::Paths:
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z) if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{ {
return std::pair(true, totalCost); return totalCost;
} }
z = (scenery.z * COORDS_Z_STEP + originZ) / COORDS_Z_STEP; z = (scenery.z * COORDS_Z_STEP + originZ) / COORDS_Z_STEP;
@ -1193,14 +1199,14 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
{ {
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY) if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{ {
return std::pair(true, totalCost); return totalCost;
} }
auto* pathElement = map_get_path_element_at({ mapCoord.x / 32, mapCoord.y / 32, z }); auto* pathElement = map_get_path_element_at({ mapCoord.x / 32, mapCoord.y / 32, z });
if (pathElement == nullptr) if (pathElement == nullptr)
{ {
return std::pair(true, totalCost); return totalCost;
} }
footpath_queue_chain_reset(); footpath_queue_chain_reset();
@ -1222,12 +1228,12 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
} }
footpath_connect_edges(mapCoord, reinterpret_cast<TileElement*>(pathElement), flags); footpath_connect_edges(mapCoord, reinterpret_cast<TileElement*>(pathElement), flags);
footpath_update_queue_chains(); footpath_update_queue_chains();
return std::pair(true, totalCost); return totalCost;
} }
break; break;
default: default:
_trackDesignPlaceStateSceneryUnavailable = true; _trackDesignPlaceStateSceneryUnavailable = true;
return std::pair(true, totalCost); return totalCost;
} }
totalCost = add_clamp_money32(totalCost, cost); totalCost = add_clamp_money32(totalCost, cost);
if (_trackDesignPlaceOperation != PTD_OPERATION_PLACE) if (_trackDesignPlaceOperation != PTD_OPERATION_PLACE)
@ -1239,22 +1245,22 @@ static std::pair<bool, money32> TrackDesignPlaceSceneryElement(
} }
if (totalCost != MONEY32_UNDEFINED) if (totalCost != MONEY32_UNDEFINED)
{ {
return std::pair(true, totalCost); return totalCost;
} }
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE) if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE)
{ {
return std::pair(true, totalCost); return totalCost;
} }
return std::pair(false, totalCost); return std::nullopt;
} }
return std::pair(true, totalCost); return totalCost;
} }
/** /**
* *
* rct2: 0x006D0964 * rct2: 0x006D0964
*/ */
static std::pair<bool, money32> track_design_place_all_scenery( static std::optional<money32> track_design_place_all_scenery(
const std::vector<TrackDesignSceneryElement>& sceneryList, const CoordsXYZ& origin, money32 cost) const std::vector<TrackDesignSceneryElement>& sceneryList, const CoordsXYZ& origin, money32 cost)
{ {
for (uint8_t mode = 0; mode <= 1; mode++) for (uint8_t mode = 0; mode <= 1; mode++)
@ -1279,20 +1285,18 @@ static std::pair<bool, money32> track_design_place_all_scenery(
auto mapCoord = CoordsXYZ{ tileCoords.ToCoordsXY(), origin.z }; auto mapCoord = CoordsXYZ{ tileCoords.ToCoordsXY(), origin.z };
track_design_update_max_min_coordinates(mapCoord); track_design_update_max_min_coordinates(mapCoord);
auto [placementSuccess, newCost] = TrackDesignPlaceSceneryElement( auto placementCost = TrackDesignPlaceSceneryElement(mapCoord, mode, scenery, rotation, origin.z, cost);
mapCoord, mode, scenery, rotation, origin.z, cost); if (!placementCost.has_value() || placementCost == MONEY32_UNDEFINED)
cost = newCost;
if (!placementSuccess)
{ {
return std::pair(false, cost); return std::nullopt;
} }
cost = placementCost.value();
} }
} }
return std::pair(true, cost); return cost;
} }
static std::pair<bool, money32> track_design_place_maze( static std::optional<money32> track_design_place_maze(TrackDesign* td6, const CoordsXYZ& coords, Ride* ride)
TrackDesign* td6, const CoordsXYZ& coords, Ride* ride, money32 totalCost)
{ {
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES) if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
{ {
@ -1302,7 +1306,7 @@ static std::pair<bool, money32> track_design_place_maze(
} }
_trackDesignPlaceZ = 0; _trackDesignPlaceZ = 0;
totalCost = 0; money32 totalCost = 0;
for (const auto& maze_element : td6->maze_elements) for (const auto& maze_element : td6->maze_elements)
{ {
@ -1443,7 +1447,7 @@ static std::pair<bool, money32> track_design_place_maze(
if (cost == MONEY32_UNDEFINED) if (cost == MONEY32_UNDEFINED)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
} }
@ -1490,11 +1494,10 @@ static std::pair<bool, money32> track_design_place_maze(
} }
_trackPreviewOrigin = coords; _trackPreviewOrigin = coords;
return std::pair(true, totalCost); return totalCost;
} }
static std::pair<bool, money32> track_design_place_ride( static std::optional<money32> track_design_place_ride(TrackDesign* td6, const CoordsXYZ& origin, Ride* ride)
TrackDesign* td6, const CoordsXYZ& origin, Ride* ride, money32 totalCost)
{ {
_trackPreviewOrigin = origin; _trackPreviewOrigin = origin;
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES) if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
@ -1505,7 +1508,7 @@ static std::pair<bool, money32> track_design_place_ride(
} }
_trackDesignPlaceZ = 0; _trackDesignPlaceZ = 0;
totalCost = 0; money32 totalCost = 0;
uint8_t rotation = _currentTrackPieceDirection; uint8_t rotation = _currentTrackPieceDirection;
// Track elements // Track elements
@ -1594,7 +1597,7 @@ static std::pair<bool, money32> track_design_place_ride(
totalCost += cost; totalCost += cost;
if (cost == MONEY32_UNDEFINED) if (cost == MONEY32_UNDEFINED)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
break; break;
} }
@ -1612,7 +1615,7 @@ static std::pair<bool, money32> track_design_place_ride(
auto surfaceElement = map_get_surface_element_at(tile); auto surfaceElement = map_get_surface_element_at(tile);
if (surfaceElement == nullptr) if (surfaceElement == nullptr)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
int32_t surfaceZ = surfaceElement->GetBaseZ(); int32_t surfaceZ = surfaceElement->GetBaseZ();
@ -1685,7 +1688,7 @@ static std::pair<bool, money32> track_design_place_ride(
newCoords.z += entrance.z; newCoords.z += entrance.z;
if (tile_element == nullptr) if (tile_element == nullptr)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
do do
@ -1730,7 +1733,7 @@ static std::pair<bool, money32> track_design_place_ride(
if (res->Error != GameActions::Status::Ok) if (res->Error != GameActions::Status::Ok)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
_trackDesignPlaceStateEntranceExitPlaced = true; _trackDesignPlaceStateEntranceExitPlaced = true;
break; break;
@ -1744,7 +1747,7 @@ static std::pair<bool, money32> track_design_place_ride(
auto res = RideEntranceExitPlaceAction::TrackPlaceQuery(newCoords, false); auto res = RideEntranceExitPlaceAction::TrackPlaceQuery(newCoords, false);
if (res->Error != GameActions::Status::Ok) if (res->Error != GameActions::Status::Ok)
{ {
return std::pair(false, MONEY32_UNDEFINED); return std::nullopt;
} }
else else
{ {
@ -1762,7 +1765,7 @@ static std::pair<bool, money32> track_design_place_ride(
sub_6CB945(ride); sub_6CB945(ride);
ride->Delete(); ride->Delete();
} }
return std::pair(true, totalCost); return totalCost;
} }
/** /**
@ -1799,27 +1802,27 @@ money32 place_virtual_track(TrackDesign* td6, uint8_t ptdOperation, bool placeSc
_trackPreviewMax = coords; _trackPreviewMax = coords;
_trackDesignPlaceSceneryZ = 0; _trackDesignPlaceSceneryZ = 0;
bool trackPlaceSuccess = false; std::optional<money32> trackPlaceCost;
money32 trackPlaceCost = 0;
if (td6->type == RIDE_TYPE_MAZE) if (td6->type == RIDE_TYPE_MAZE)
{ {
std::tie(trackPlaceSuccess, trackPlaceCost) = track_design_place_maze(td6, coords, ride, trackPlaceCost); trackPlaceCost = track_design_place_maze(td6, coords, ride);
} }
else else
{ {
std::tie(trackPlaceSuccess, trackPlaceCost) = track_design_place_ride(td6, coords, ride, trackPlaceCost); trackPlaceCost = track_design_place_ride(td6, coords, ride);
} }
// Scenery elements // Scenery elements
if (trackPlaceSuccess) if (trackPlaceCost.has_value())
{ {
bool success = false; auto trackPlusSceneryCost = track_design_place_all_scenery(
std::tie(success, trackPlaceCost) = track_design_place_all_scenery( td6->scenery_elements, _trackPreviewOrigin, trackPlaceCost.value());
td6->scenery_elements, _trackPreviewOrigin, trackPlaceCost); if (!trackPlusSceneryCost.has_value())
if (!success)
{ {
return MONEY32_UNDEFINED; return MONEY32_UNDEFINED;
} }
trackPlaceCost = trackPlusSceneryCost;
} }
// 0x6D0FE6 // 0x6D0FE6
@ -1837,7 +1840,8 @@ money32 place_virtual_track(TrackDesign* td6, uint8_t ptdOperation, bool placeSc
// from _trackDesignPlaceZ, causing bug #259. // from _trackDesignPlaceZ, causing bug #259.
return _trackDesignPlaceZ - _trackDesignPlaceSceneryZ; return _trackDesignPlaceZ - _trackDesignPlaceSceneryZ;
} }
return trackPlaceCost;
return trackPlaceCost.has_value() ? trackPlaceCost.value() : MONEY32_UNDEFINED;
} }
static money32 track_design_ride_create_command(int32_t type, int32_t subType, int32_t flags, ride_id_t* outRideIndex) static money32 track_design_ride_create_command(int32_t type, int32_t subType, int32_t flags, ride_id_t* outRideIndex)