Skip to content

Commit

Permalink
Fix horses making engine sounds. (#34287)
Browse files Browse the repository at this point in the history
* Remove commented out code.

* Fix generating engine noise:

- Combine the two arrays into one. The can't be changed independently anyway (adding more messages than sound levels does not have any effect and less messages will cause UB).
- Make the array static (the strings are only marked for translation, but translation happens when they get actually used).
- Emit smoke and noise only for combustion engines.
  • Loading branch information
BevapDin authored and ZhilkinSerg committed Sep 28, 2019
1 parent f119497 commit 832c578
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3582,12 +3582,11 @@ void vehicle::spew_field( double joules, int part, field_type_id type, int inten
*/
void vehicle::noise_and_smoke( int load, time_duration time )
{
const std::array<int, 8> sound_levels = {{ 0, 15, 30, 60, 100, 140, 180, INT_MAX }};
const std::array<std::string, 8> sound_msgs = {{
translate_marker( "hmm" ), translate_marker( "hummm!" ),
translate_marker( "whirrr!" ), translate_marker( "vroom!" ),
translate_marker( "roarrr!" ), translate_marker( "ROARRR!" ),
translate_marker( "BRRROARRR!" ), translate_marker( "BRUMBRUMBRUMBRUM!" )
static const std::array<std::pair<std::string, int>, 8> sounds = { {
{ translate_marker( "hmm" ), 0 }, { translate_marker( "hummm!" ), 15 },
{ translate_marker( "whirrr!" ), 30 }, { translate_marker( "vroom!" ), 60 },
{ translate_marker( "roarrr!" ), 100 }, { translate_marker( "ROARRR!" ), 140 },
{ translate_marker( "BRRROARRR!" ), 180 }, { translate_marker( "BRUMBRUMBRUMBRUM!" ), INT_MAX }
}
};
double noise = 0.0;
Expand All @@ -3604,6 +3603,7 @@ void vehicle::noise_and_smoke( int load, time_duration time )
}

bool bad_filter = false;
bool combustion = false;

for( size_t e = 0; e < engines.size(); e++ ) {
int p = engines[e];
Expand All @@ -3619,6 +3619,7 @@ void vehicle::noise_and_smoke( int load, time_duration time )
double part_noise = cur_stress * part_info( p ).engine_noise_factor();

if( part_info( p ).has_flag( "E_COMBUSTION" ) ) {
combustion = true;
double health = parts[p].health_percent();
if( parts[ p ].base.faults.count( fault_filter_fuel ) ) {
health = 0.0;
Expand All @@ -3639,13 +3640,16 @@ void vehicle::noise_and_smoke( int load, time_duration time )
mufflesmoke += j;
}
part_noise = ( part_noise + max_stress * 3 + 5 ) * muffle;
//add_msg( m_good, "PART NOISE (2nd): %d", static_cast<int>( part_noise ) );
}
noise = std::max( noise, part_noise ); // Only the loudest engine counts.
}
}
if( ( exhaust_part != -1 ) && engine_on &&
has_engine_type_not( fuel_type_muscle, true ) ) { // No engine, no smoke
if( !combustion ) {
return;
}
/// @todo handle other engine types: muscle / animal / wind / coal / ...

if( exhaust_part != -1 && engine_on ) {
spew_field( mufflesmoke, exhaust_part, fd_smoke,
bad_filter ? fd_smoke.obj().get_max_intensity() : 1 );
}
Expand All @@ -3654,16 +3658,14 @@ void vehicle::noise_and_smoke( int load, time_duration time )
// Even a vehicle with engines off will make noise traveling at high speeds
noise = std::max( noise, static_cast<double>( fabs( velocity / 500.0 ) ) );
int lvl = 0;
if( one_in( 4 ) && rng( 0, 30 ) < noise && has_engine_type_not( fuel_type_muscle, true ) ) {
while( noise > sound_levels[lvl] ) {
if( one_in( 4 ) && rng( 0, 30 ) < noise ) {
while( noise > sounds[lvl].second ) {
lvl++;
}
}
add_msg( m_debug, "VEH NOISE final: %d", static_cast<int>( noise ) );
vehicle_noise = static_cast<unsigned char>( noise );
if( has_engine_type_not( fuel_type_muscle, true ) ) {
sounds::sound( global_pos3(), noise, sounds::sound_t::movement, _( sound_msgs[lvl] ), true );
}
sounds::sound( global_pos3(), noise, sounds::sound_t::movement, _( sounds[lvl].first ), true );
}

int vehicle::wheel_area() const
Expand Down

0 comments on commit 832c578

Please sign in to comment.