From acf71315b5c32c06c6187668de09074ef8790cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= Date: Wed, 6 Jan 2021 23:29:40 +0200 Subject: [PATCH] Cleanup variables usage and initialize them --- src/openrct2-ui/audio/MemoryAudioSource.cpp | 6 +- src/openrct2-ui/windows/About.cpp | 11 +-- src/openrct2/interface/Window.cpp | 3 +- src/openrct2/management/Finance.cpp | 16 ++-- src/openrct2/object/TerrainSurfaceObject.h | 2 +- src/openrct2/ride/Ride.cpp | 87 +++++++++------------ src/openrct2/ride/RideRatings.cpp | 9 +-- src/openrct2/ride/Vehicle.cpp | 29 ++++--- src/openrct2/util/SawyerCoding.cpp | 10 +-- src/openrct2/world/Footpath.cpp | 9 +-- src/openrct2/world/Map.h | 2 +- src/openrct2/world/Park.cpp | 4 +- 12 files changed, 79 insertions(+), 109 deletions(-) diff --git a/src/openrct2-ui/audio/MemoryAudioSource.cpp b/src/openrct2-ui/audio/MemoryAudioSource.cpp index 56a4336fce..4553fe8957 100644 --- a/src/openrct2-ui/audio/MemoryAudioSource.cpp +++ b/src/openrct2-ui/audio/MemoryAudioSource.cpp @@ -111,17 +111,17 @@ namespace OpenRCT2::Audio SDL_RWops* rw = SDL_RWFromFile(path, "rb"); if (rw != nullptr) { - uint32_t numSounds; + uint32_t numSounds{}; SDL_RWread(rw, &numSounds, sizeof(numSounds), 1); if (index < numSounds) { SDL_RWseek(rw, index * 4, RW_SEEK_CUR); - uint32_t pcmOffset; + uint32_t pcmOffset{}; SDL_RWread(rw, &pcmOffset, sizeof(pcmOffset), 1); SDL_RWseek(rw, pcmOffset, RW_SEEK_SET); - uint32_t pcmSize; + uint32_t pcmSize{}; SDL_RWread(rw, &pcmSize, sizeof(pcmSize), 1); _length = pcmSize; diff --git a/src/openrct2-ui/windows/About.cpp b/src/openrct2-ui/windows/About.cpp index dcb6471085..ee831f1da4 100644 --- a/src/openrct2-ui/windows/About.cpp +++ b/src/openrct2-ui/windows/About.cpp @@ -188,20 +188,17 @@ static void window_about_openrct2_paint(rct_window* w, rct_drawpixelinfo* dpi) { window_about_openrct2_common_paint(w, dpi); - int32_t width; - rct_size16 logoSize; - int32_t lineHeight = font_get_line_height(gCurrentFontSpriteBase); ScreenCoordsXY aboutCoords( w->windowPos.x + (w->width / 2), w->windowPos.y + w->widgets[WIDX_PAGE_BACKGROUND].top + lineHeight); - width = w->width - 20; + int32_t width = w->width - 20; aboutCoords.y += gfx_draw_string_centred_wrapped( dpi, nullptr, aboutCoords, width, STR_ABOUT_OPENRCT2_DESCRIPTION, w->colours[2]) + lineHeight; - logoSize = gfx_get_sprite_size(SPR_G2_LOGO); + rct_size16 logoSize = gfx_get_sprite_size(SPR_G2_LOGO); gfx_draw_sprite(dpi, SPR_G2_LOGO, aboutCoords - ScreenCoordsXY{ logoSize.width / 2, 0 }, 0); aboutCoords.y += logoSize.height + lineHeight * 2; @@ -267,11 +264,9 @@ static void window_about_rct2_mouseup(rct_window* w, rct_widgetindex widgetIndex */ static void window_about_rct2_paint(rct_window* w, rct_drawpixelinfo* dpi) { - int32_t yPage; - window_about_openrct2_common_paint(w, dpi); - yPage = w->windowPos.y + w->widgets[WIDX_PAGE_BACKGROUND].top + 5; + int32_t yPage = w->windowPos.y + w->widgets[WIDX_PAGE_BACKGROUND].top + 5; auto screenCoords = ScreenCoordsXY{ w->windowPos.x + 200, yPage + 5 }; diff --git a/src/openrct2/interface/Window.cpp b/src/openrct2/interface/Window.cpp index 0fabc9878f..37b2ff5958 100644 --- a/src/openrct2/interface/Window.cpp +++ b/src/openrct2/interface/Window.cpp @@ -1276,7 +1276,6 @@ void window_move_position(rct_window* w, const ScreenCoordsXY& deltaCoords) void window_resize(rct_window* w, int32_t dw, int32_t dh) { - int32_t i; if (dw == 0 && dh == 0) return; @@ -1291,7 +1290,7 @@ void window_resize(rct_window* w, int32_t dw, int32_t dh) window_event_invalidate_call(w); // Update scroll widgets - for (i = 0; i < 3; i++) + for (int32_t i = 0; i < 3; i++) { w->scrolls[i].h_right = WINDOW_SCROLL_UNDEFINED; w->scrolls[i].v_bottom = WINDOW_SCROLL_UNDEFINED; diff --git a/src/openrct2/management/Finance.cpp b/src/openrct2/management/Finance.cpp index 26d8e26cef..dc6942d818 100644 --- a/src/openrct2/management/Finance.cpp +++ b/src/openrct2/management/Finance.cpp @@ -123,14 +123,12 @@ void finance_pay_wages() **/ void finance_pay_research() { - uint8_t level; - if (gParkFlags & PARK_FLAGS_NO_MONEY) { return; } - level = gResearchFundingLevel; + const uint8_t level = gResearchFundingLevel; finance_payment(research_cost_table[level] / 4, ExpenditureType::Research); } @@ -140,18 +138,16 @@ void finance_pay_research() */ void finance_pay_interest() { - // This variable uses the 64-bit type as the computation below can involve multiplying very large numbers - // that will overflow money32 if the loan is greater than (1 << 31) / (5 * current_interest_rate) - money64 current_loan = gBankLoan; - uint8_t current_interest_rate = gBankLoanInterestRate; - money32 interest_to_pay; - if (gParkFlags & PARK_FLAGS_NO_MONEY) { return; } - interest_to_pay = (current_loan * 5 * current_interest_rate) >> 14; + // This variable uses the 64-bit type as the computation below can involve multiplying very large numbers + // that will overflow money32 if the loan is greater than (1 << 31) / (5 * current_interest_rate) + const money64 current_loan = gBankLoan; + const uint8_t current_interest_rate = gBankLoanInterestRate; + const money32 interest_to_pay = (current_loan * 5 * current_interest_rate) >> 14; finance_payment(interest_to_pay, ExpenditureType::Interest); } diff --git a/src/openrct2/object/TerrainSurfaceObject.h b/src/openrct2/object/TerrainSurfaceObject.h index 0b93f34dad..85a53afa97 100644 --- a/src/openrct2/object/TerrainSurfaceObject.h +++ b/src/openrct2/object/TerrainSurfaceObject.h @@ -54,7 +54,7 @@ public: money32 Price{}; TERRAIN_SURFACE_FLAGS Flags{}; - uint32_t NumImagesLoaded; + uint32_t NumImagesLoaded{}; explicit TerrainSurfaceObject(const rct_object_entry& entry) : Object(entry) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 7010d873cd..7d48d758e7 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3644,7 +3644,6 @@ int32_t ride_music_params_update( } OpenRCT2::Audio::RideMusic* ride_music = &OpenRCT2::Audio::gRideMusicList[0]; int32_t channel = 0; - uint32_t a1; while (ride_music->ride_id != ride->id || ride_music->tune_id != *tuneId) { ride_music++; @@ -3652,9 +3651,8 @@ int32_t ride_music_params_update( if (static_cast(channel) >= OpenRCT2::Audio::MaxRideMusic) { OpenRCT2::Audio::RideMusicInfo* ride_music_info = &OpenRCT2::Audio::gRideMusicInfoList[*tuneId]; - a1 = position + ride_music_info->offset; - - return ride_music_params_update_label_51(a1, tuneId, ride, v32, pan_x, sampleRate); + const uint32_t offset = position + ride_music_info->offset; + return ride_music_params_update_label_51(offset, tuneId, ride, v32, pan_x, sampleRate); } } int32_t playing = Mixer_Channel_IsPlaying(OpenRCT2::Audio::gRideMusicList[channel].sound_channel); @@ -3663,9 +3661,9 @@ int32_t ride_music_params_update( *tuneId = 0xFF; return 0; } - a1 = static_cast(Mixer_Channel_GetOffset(OpenRCT2::Audio::gRideMusicList[channel].sound_channel)); - - return ride_music_params_update_label_51(a1, tuneId, ride, v32, pan_x, sampleRate); + const uint32_t offset = static_cast( + Mixer_Channel_GetOffset(OpenRCT2::Audio::gRideMusicList[channel].sound_channel)); + return ride_music_params_update_label_51(offset, tuneId, ride, v32, pan_x, sampleRate); } else { @@ -3879,10 +3877,9 @@ static int32_t ride_check_for_entrance_exit(ride_id_t rideIndex) if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) return 1; - int32_t i; uint8_t entrance = 0; uint8_t exit = 0; - for (i = 0; i < MAX_STATIONS; i++) + for (int32_t i = 0; i < MAX_STATIONS; i++) { if (ride->stations[i].Start.isNull()) continue; @@ -3960,21 +3957,18 @@ void Ride::ChainQueues() const */ static int32_t ride_check_block_brakes(CoordsXYE* input, CoordsXYE* output) { - rct_window* w; - track_circuit_iterator it; - int32_t type; - ride_id_t rideIndex = input->element->AsTrack()->GetRideIndex(); - w = window_find_by_class(WC_RIDE_CONSTRUCTION); + rct_window* w = window_find_by_class(WC_RIDE_CONSTRUCTION); if (w != nullptr && _rideConstructionState != RIDE_CONSTRUCTION_STATE_0 && _currentRideIndex == rideIndex) ride_construction_invalidate_current_track(); + track_circuit_iterator it; track_circuit_iterator_begin(&it, *input); while (track_circuit_iterator_next(&it)) { if (it.current.element->AsTrack()->GetTrackType() == TrackElemType::BlockBrakes) { - type = it.last.element->AsTrack()->GetTrackType(); + int32_t type = it.last.element->AsTrack()->GetTrackType(); if (type == TrackElemType::EndStation) { gGameCommandErrorText = STR_BLOCK_BRAKES_CANNOT_BE_USED_DIRECTLY_AFTER_STATION; @@ -4180,7 +4174,6 @@ static int32_t ride_check_station_length(CoordsXYE* input, CoordsXYE* output) */ static bool ride_check_start_and_end_is_station(CoordsXYE* input) { - int32_t trackType; CoordsXYE trackBack, trackFront; ride_id_t rideIndex = input->element->AsTrack()->GetRideIndex(); @@ -4196,7 +4189,7 @@ static bool ride_check_start_and_end_is_station(CoordsXYE* input) // Check back of the track track_get_back(input, &trackBack); - trackType = trackBack.element->AsTrack()->GetTrackType(); + int32_t trackType = trackBack.element->AsTrack()->GetTrackType(); if (!(TrackSequenceProperties[trackType][0] & TRACK_SEQUENCE_FLAG_ORIGIN)) { return false; @@ -6596,11 +6589,8 @@ static std::optional ride_get_smallest_station_length(Ride* ride) */ static int32_t ride_get_track_length(Ride* ride) { - rct_window* w; TileElement* tileElement = nullptr; - track_circuit_iterator it, slowIt; - ride_id_t rideIndex; - int32_t trackType, result; + int32_t trackType; CoordsXYZ trackStart; bool foundTrack = false; @@ -6629,41 +6619,40 @@ static int32_t ride_get_track_length(Ride* ride) } while (!foundTrack && !(tileElement++)->IsLastForTile()); } - if (foundTrack) + if (!foundTrack) + return 0; + + ride_id_t rideIndex = tileElement->AsTrack()->GetRideIndex(); + + rct_window* w = window_find_by_class(WC_RIDE_CONSTRUCTION); + if (w != nullptr && _rideConstructionState != RIDE_CONSTRUCTION_STATE_0 && _currentRideIndex == rideIndex) { - rideIndex = tileElement->AsTrack()->GetRideIndex(); + ride_construction_invalidate_current_track(); + } - w = window_find_by_class(WC_RIDE_CONSTRUCTION); - if (w != nullptr && _rideConstructionState != RIDE_CONSTRUCTION_STATE_0 && _currentRideIndex == rideIndex) + bool moveSlowIt = true; + int32_t result = 0; + + track_circuit_iterator it; + track_circuit_iterator_begin(&it, { trackStart.x, trackStart.y, tileElement }); + + track_circuit_iterator slowIt = it; + while (track_circuit_iterator_next(&it)) + { + trackType = it.current.element->AsTrack()->GetTrackType(); + result += TrackPieceLengths[trackType]; + + moveSlowIt = !moveSlowIt; + if (moveSlowIt) { - ride_construction_invalidate_current_track(); - } - - bool moveSlowIt = true; - result = 0; - track_circuit_iterator_begin(&it, { trackStart.x, trackStart.y, tileElement }); - slowIt = it; - while (track_circuit_iterator_next(&it)) - { - trackType = it.current.element->AsTrack()->GetTrackType(); - result += TrackPieceLengths[trackType]; - - moveSlowIt = !moveSlowIt; - if (moveSlowIt) + track_circuit_iterator_next(&slowIt); + if (track_circuit_iterators_match(&it, &slowIt)) { - track_circuit_iterator_next(&slowIt); - if (track_circuit_iterators_match(&it, &slowIt)) - { - return 0; - } + return 0; } } - return result; - } - else - { - return 0; } + return result; } /** diff --git a/src/openrct2/ride/RideRatings.cpp b/src/openrct2/ride/RideRatings.cpp index cfd732763a..06ef26f83e 100644 --- a/src/openrct2/ride/RideRatings.cpp +++ b/src/openrct2/ride/RideRatings.cpp @@ -990,7 +990,6 @@ static void ride_ratings_apply_adjustments(Ride* ride, RatingTuple* ratings) if (RideTypeDescriptors[ride->type].Flags & RIDE_TYPE_FLAG_HAS_AIR_TIME) { int32_t excitementModifier; - int32_t nauseaModifier; if (rideEntry->flags & RIDE_ENTRY_FLAG_LIMIT_AIRTIME_BONUS) { // Limit airtime bonus for heartline twister coaster (see issues #2031 and #2064) @@ -1000,7 +999,7 @@ static void ride_ratings_apply_adjustments(Ride* ride, RatingTuple* ratings) { excitementModifier = ride->total_air_time / 8; } - nauseaModifier = ride->total_air_time / 16; + int32_t nauseaModifier = ride->total_air_time / 16; ride_ratings_add(ratings, excitementModifier, 0, nauseaModifier); } @@ -1013,7 +1012,7 @@ static void ride_ratings_apply_adjustments(Ride* ride, RatingTuple* ratings) */ static void ride_ratings_apply_intensity_penalty(RatingTuple* ratings) { - static const ride_rating intensityBounds[] = { 1000, 1100, 1200, 1320, 1450 }; + static constexpr ride_rating intensityBounds[] = { 1000, 1100, 1200, 1320, 1450 }; ride_rating excitement = ratings->Excitement; for (auto intensityBound : intensityBounds) { @@ -2999,8 +2998,6 @@ void ride_ratings_calculate_reverse_freefall_coaster(Ride* ride) void ride_ratings_calculate_lift(Ride* ride) { - int32_t totalLength; - if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_TESTED)) return; @@ -3010,7 +3007,7 @@ void ride_ratings_calculate_lift(Ride* ride) RatingTuple ratings; ride_ratings_set(&ratings, RIDE_RATING(1, 11), RIDE_RATING(0, 35), RIDE_RATING(0, 30)); - totalLength = ride_get_total_length(ride) >> 16; + int32_t totalLength = ride_get_total_length(ride) >> 16; ride_ratings_add(&ratings, (totalLength * 45875) >> 16, 0, (totalLength * 26214) >> 16); ride_ratings_apply_proximity(&ratings, 11183); diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index ee333ceea8..3540c817cd 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -2088,7 +2088,8 @@ void Vehicle::UpdateMovingToEndOfStation() if (curRide == nullptr) return; - int32_t curFlags, station; + int32_t curFlags = 0; + int32_t station = 0; switch (curRide->mode) { @@ -2836,10 +2837,9 @@ static bool ride_station_can_depart_synchronised(const Ride& ride, StationIndex */ int32_t direction = tileElement->GetDirectionWithOffset(1); - int32_t spaceBetween; int32_t maxCheckDistance = RIDE_ADJACENCY_CHECK_DISTANCE; + int32_t spaceBetween = maxCheckDistance; - spaceBetween = maxCheckDistance; while (_lastSynchronisedVehicle < &_synchronisedVehicles[SYNCHRONISED_VEHICLE_COUNT - 1]) { location += CoordsXYZ{ CoordsDirectionDelta[direction], 0 }; @@ -3891,6 +3891,8 @@ void Vehicle::UpdateArriving() return; uint8_t unkF64E35 = 1; + uint32_t curFlags = 0; + switch (curRide->mode) { case RideMode::Swing: @@ -4013,7 +4015,6 @@ void Vehicle::UpdateArriving() } } - uint32_t curFlags; loc_6D8E36: curFlags = UpdateTrackMotion(nullptr); if (curFlags & VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_COLLISION && unkF64E35 == 0) @@ -7128,7 +7129,7 @@ void Vehicle::UpdateSpinningCar() int32_t spinningInertia = vehicleEntry->spinning_inertia; int32_t trackType = GetTrackType(); int32_t dword_F64E08 = _vehicleVelocityF64E08; - int32_t spinSpeed; + int32_t spinSpeed{}; // An L spin adds to the spin speed, R does the opposite // The number indicates how much right shift of the velocity will become spin // The bigger the number the less change in spin. @@ -7251,8 +7252,9 @@ static void steam_particle_create(const CoordsXYZ& coords) */ void Vehicle::UpdateAdditionalAnimation() { - uint8_t al, ah; - uint32_t eax; + uint8_t al{}; + uint8_t ah{}; + uint32_t eax{}; uint32_t* curVar_C8 = reinterpret_cast(&var_C8); auto vehicleEntry = Entry(); @@ -8374,7 +8376,7 @@ bool Vehicle::UpdateTrackMotionBackwardsGetNewTrack(uint16_t trackType, Ride* cu return false; bool nextTileBackwards = true; - int32_t direction; + int32_t direction = 0; // loc_6DBB08:; auto trackPos = CoordsXYZ{ TrackLocation.x, TrackLocation.y, 0 }; @@ -8438,7 +8440,7 @@ bool Vehicle::UpdateTrackMotionBackwardsGetNewTrack(uint16_t trackType, Ride* cu // loc_6DBB4F:; CoordsXYE input; CoordsXYE output; - int32_t outputZ; + int32_t outputZ{}; input.x = trackPos.x; input.y = trackPos.y; @@ -8680,6 +8682,7 @@ void Vehicle::UpdateTrackMotionMiniGolfVehicle(Ride* curRide, rct_ride_entry* ri uint16_t otherVehicleIndex = SPRITE_INDEX_NULL; TileElement* tileElement = nullptr; CoordsXYZ trackPos; + int32_t direction{}; _vehicleUnkF64E10 = 1; acceleration = dword_9A2970[vehicle_sprite_type]; @@ -8794,10 +8797,10 @@ loc_6DC476: } tileElement = map_get_track_element_at_of_type_seq(TrackLocation, GetTrackType(), 0); - int32_t direction; { CoordsXYE output; - int32_t outZ, outDirection; + int32_t outZ{}; + int32_t outDirection{}; CoordsXYE input = { TrackLocation, tileElement }; if (!track_block_get_next(&input, &output, &outZ, &outDirection)) { @@ -9748,8 +9751,8 @@ void Vehicle::UpdateCrossings() const backVehicle = this; } - track_begin_end output; - int32_t direction; + track_begin_end output{}; + int32_t direction{}; CoordsXYE xyElement = { frontVehicle->TrackLocation, map_get_track_element_at_of_type_seq( diff --git a/src/openrct2/util/SawyerCoding.cpp b/src/openrct2/util/SawyerCoding.cpp index 7490169ff8..b8c2fb586d 100644 --- a/src/openrct2/util/SawyerCoding.cpp +++ b/src/openrct2/util/SawyerCoding.cpp @@ -27,9 +27,8 @@ bool gUseRLE = true; uint32_t sawyercoding_calculate_checksum(const uint8_t* buffer, size_t length) { - size_t i; uint32_t checksum = 0; - for (i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) checksum += buffer[i]; return checksum; @@ -125,14 +124,11 @@ size_t sawyercoding_decode_sc4(const uint8_t* src, uint8_t* dst, size_t length, size_t sawyercoding_encode_sv4(const uint8_t* src, uint8_t* dst, size_t length) { - size_t encodedLength; - uint32_t checksum; - // Encode - encodedLength = encode_chunk_rle(src, dst, length); + size_t encodedLength = encode_chunk_rle(src, dst, length); // Append checksum - checksum = sawyercoding_calculate_checksum(dst, encodedLength); + uint32_t checksum = sawyercoding_calculate_checksum(dst, encodedLength); *(reinterpret_cast(&dst[encodedLength])) = checksum; return encodedLength + 4; diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index 5357c5513c..da1eba5ee8 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -644,10 +644,7 @@ static void neighbour_list_sort(rct_neighbour_list* neighbourList) static TileElement* footpath_get_element(const CoordsXYRangedZ& footpathPos, int32_t direction) { - TileElement* tileElement; - int32_t slope; - - tileElement = map_get_first_element_at(footpathPos); + TileElement* tileElement = map_get_first_element_at(footpathPos); if (tileElement == nullptr) return nullptr; do @@ -659,7 +656,7 @@ static TileElement* footpath_get_element(const CoordsXYRangedZ& footpathPos, int { if (tileElement->AsPath()->IsSloped()) { - slope = tileElement->AsPath()->GetSlopeDirection(); + auto slope = tileElement->AsPath()->GetSlopeDirection(); if (slope != direction) break; } @@ -670,7 +667,7 @@ static TileElement* footpath_get_element(const CoordsXYRangedZ& footpathPos, int if (!tileElement->AsPath()->IsSloped()) break; - slope = direction_reverse(tileElement->AsPath()->GetSlopeDirection()); + auto slope = direction_reverse(tileElement->AsPath()->GetSlopeDirection()); if (slope != direction) break; diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h index 6873d5ea70..03c3ab2390 100644 --- a/src/openrct2/world/Map.h +++ b/src/openrct2/world/Map.h @@ -56,7 +56,7 @@ struct CoordsXYE : public CoordsXY , element(_e) { } - TileElement* element; + TileElement* element = nullptr; }; enum diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index e63317b019..464e9db39e 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -339,10 +339,8 @@ void Park::Update(const Date& date) int32_t Park::CalculateParkSize() const { - int32_t tiles; + int32_t tiles = 0; tile_element_iterator it; - - tiles = 0; tile_element_iterator_begin(&it); do {