diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 4960b6f528..cb85c37cc5 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -998,35 +998,36 @@ rct_vehicle_sound_params Vehicle::CreateSoundParam(uint16_t priority) const * * rct2: 0x006BB9FF */ -void Vehicle::UpdateSoundParams( - std::array vehicleSoundParamsList, - rct_vehicle_sound_params* vehicleSoundParamsListEnd) const +void Vehicle::UpdateSoundParams(std::vector& vehicleSoundParamsList) const { if (!SoundCanPlay()) return; uint16_t soundPriority = GetSoundPriority(); - rct_vehicle_sound_params* soundParam; // Find a sound param of lower priority to use - for (soundParam = &vehicleSoundParamsList[0]; - soundParam < vehicleSoundParamsListEnd && soundPriority <= soundParam->priority; soundParam++) - ; + auto soundParamIter = std::find_if( + vehicleSoundParamsList.begin(), vehicleSoundParamsList.end(), + [soundPriority](rct_vehicle_sound_params param) { return soundPriority > param.priority; }); - if (soundParam >= &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) - return; - - if (vehicleSoundParamsListEnd < &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) + if (soundParamIter == std::end(vehicleSoundParamsList)) { - vehicleSoundParamsListEnd++; + if (vehicleSoundParamsList.size() < AUDIO_MAX_VEHICLE_SOUNDS) + { + vehicleSoundParamsList.push_back(CreateSoundParam(soundPriority)); + } } - - // Shift all sound params down one if using a free space - if (soundParam != vehicleSoundParamsListEnd) + else { - std::memmove( - soundParam + 1, soundParam, ((vehicleSoundParamsListEnd - soundParam) - 1) * sizeof(rct_vehicle_sound_params)); + if (vehicleSoundParamsList.size() < AUDIO_MAX_VEHICLE_SOUNDS) + { + // Shift all sound params down one if using a free space + vehicleSoundParamsList.insert(soundParamIter, CreateSoundParam(soundPriority)); + } + else + { + *soundParamIter = CreateSoundParam(soundPriority); + } } - *soundParam = CreateSoundParam(soundPriority); } static void vehicle_sounds_update_window_setup() @@ -1273,15 +1274,14 @@ void vehicle_sounds_update() if (gAudioCurrentDevice == -1 || gGameSoundsOff || !gConfigSound.sound_enabled || gOpenRCT2Headless) return; - std::array vehicleSoundParamsList; - rct_vehicle_sound_params* vehicleSoundParamsListEnd; + std::vector vehicleSoundParamsList; + vehicleSoundParamsList.reserve(AUDIO_MAX_VEHICLE_SOUNDS); vehicle_sounds_update_window_setup(); - vehicleSoundParamsListEnd = &vehicleSoundParamsList[0]; for (uint16_t i = gSpriteListHead[SPRITE_LIST_VEHICLE_HEAD]; i != SPRITE_INDEX_NULL; i = get_sprite(i)->vehicle.next) { - get_sprite(i)->vehicle.UpdateSoundParams(vehicleSoundParamsList, vehicleSoundParamsListEnd); + get_sprite(i)->vehicle.UpdateSoundParams(vehicleSoundParamsList); } // Stop all playing sounds that no longer have priority to play after vehicle_update_sound_params @@ -1290,10 +1290,9 @@ void vehicle_sounds_update() if (vehicle_sound.id != SOUND_ID_NULL) { bool keepPlaying = false; - for (rct_vehicle_sound_params* vehicle_sound_params = &vehicleSoundParamsList[0]; - vehicle_sound_params != vehicleSoundParamsListEnd; vehicle_sound_params++) + for (auto vehicleSoundParams : vehicleSoundParamsList) { - if (vehicle_sound.id == vehicle_sound_params->id) + if (vehicle_sound.id == vehicleSoundParams.id) { keepPlaying = true; break; @@ -1315,21 +1314,20 @@ void vehicle_sounds_update() } } - for (rct_vehicle_sound_params* vehicleSoundParams = &vehicleSoundParamsList[0]; - vehicleSoundParams < vehicleSoundParamsListEnd; vehicleSoundParams++) + for (auto& vehicleSoundParams : vehicleSoundParamsList) { - uint8_t panVol = vehicle_sounds_update_get_pan_volume(vehicleSoundParams); + uint8_t panVol = vehicle_sounds_update_get_pan_volume(&vehicleSoundParams); - rct_vehicle_sound* vehicleSound = vehicle_sounds_update_get_vehicle_sound(vehicleSoundParams); + rct_vehicle_sound* vehicleSound = vehicle_sounds_update_get_vehicle_sound(&vehicleSoundParams); // No free vehicle sound slots (RCT2 corrupts the pointer here) if (vehicleSound == nullptr) continue; // Move the Sound Volume towards the SoundsParam Volume int32_t tempvolume = vehicleSound->volume; - if (tempvolume != vehicleSoundParams->volume) + if (tempvolume != vehicleSoundParams.volume) { - if (tempvolume < vehicleSoundParams->volume) + if (tempvolume < vehicleSoundParams.volume) { tempvolume += 4; } @@ -1341,9 +1339,9 @@ void vehicle_sounds_update() vehicleSound->volume = tempvolume; panVol = std::max(0, panVol - tempvolume); - Vehicle* vehicle = GET_VEHICLE(vehicleSoundParams->id); - vehicle_sounds_update_sound_1(vehicle, vehicleSoundParams, vehicleSound, panVol); - vehicle_sounds_update_sound_2(vehicle, vehicleSoundParams, vehicleSound, panVol); + Vehicle* vehicle = GET_VEHICLE(vehicleSoundParams.id); + vehicle_sounds_update_sound_1(vehicle, &vehicleSoundParams, vehicleSound, panVol); + vehicle_sounds_update_sound_2(vehicle, &vehicleSoundParams, vehicleSound, panVol); } } @@ -7921,7 +7919,8 @@ static void sub_6DBF3E(Vehicle* vehicle) if (vehicleEntry->flags & VEHICLE_ENTRY_FLAG_GO_KART) { // Determine the stop positions for the karts. If in left lane it's further along the track than the right lane. - // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and 8). + // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and + // 8). cx = vehicle->TrackSubposition == VEHICLE_TRACK_SUBPOSITION_GO_KARTS_RIGHT_LANE ? 18 : 20; } diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h index 53369b0beb..d9b106259c 100644 --- a/src/openrct2/ride/Vehicle.h +++ b/src/openrct2/ride/Vehicle.h @@ -302,9 +302,7 @@ struct Vehicle : SpriteBase void Invalidate(); void SetState(VEHICLE_STATUS vehicleStatus, uint8_t subState = 0); bool IsGhost() const; - void UpdateSoundParams( - std::array vehicleSoundParamsList, - rct_vehicle_sound_params* vehicleSoundParamsListEnd) const; + void UpdateSoundParams(std::vector& vehicleSoundParamsList) const; private: bool SoundCanPlay() const;