forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.cpp
1995 lines (1868 loc) · 85.2 KB
/
sounds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sounds.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <unordered_map>
#include "activity_type.h"
#include "cached_options.h" // IWYU pragma: keep
#include "calendar.h"
#include "character.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "creature_tracker.h"
#include "debug.h"
#include "effect.h"
#include "enums.h"
#include "game.h"
#include "game_constants.h"
#include "itype.h" // IWYU pragma: keep
#include "line.h"
#include "make_static.h"
#include "map.h"
#include "map_iterator.h"
#include "messages.h"
#include "monster.h"
#include "music.h"
#include "npc.h"
#include "output.h"
#include "overmapbuffer.h"
#include "player_activity.h"
#include "point.h"
#include "rng.h"
#include "safemode_ui.h"
#include "string_formatter.h"
#include "translations.h"
#include "type_id.h"
#include "uistate.h"
#include "units.h"
#include "veh_type.h" // IWYU pragma: keep
#include "vehicle.h"
#include "vpart_position.h"
#include "weather.h"
#include "weather_type.h"
#if defined(SDL_SOUND)
# if defined(_MSC_VER) && defined(USE_VCPKG)
# include <SDL2/SDL_mixer.h>
# else
# include <SDL_mixer.h>
# endif
# include <thread>
# if defined(_WIN32) && !defined(_MSC_VER)
# include "mingw.thread.h"
# endif
# define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
static int prev_hostiles = 0;
static int previous_speed = 0;
static int previous_gear = 0;
static bool audio_muted = false;
#endif
static weather_type_id previous_weather;
static float g_sfx_volume_multiplier = 1.0f;
static auto start_sfx_timestamp = std::chrono::high_resolution_clock::now();
static auto end_sfx_timestamp = std::chrono::high_resolution_clock::now();
static auto sfx_time = end_sfx_timestamp - start_sfx_timestamp;
static activity_id act;
static std::pair<std::string, std::string> engine_external_id_and_variant;
static const bionic_id bio_sleep_shutdown( "bio_sleep_shutdown" );
static const efftype_id effect_alarm_clock( "alarm_clock" );
static const efftype_id effect_deaf( "deaf" );
static const efftype_id effect_narcosis( "narcosis" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_slept_through_alarm( "slept_through_alarm" );
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_muscle( "muscle" );
static const itype_id fuel_type_wind( "wind" );
static const itype_id itype_weapon_fire_suppressed( "weapon_fire_suppressed" );
static const material_id material_bone( "bone" );
static const material_id material_flesh( "flesh" );
static const material_id material_hflesh( "hflesh" );
static const material_id material_iflesh( "iflesh" );
static const material_id material_steel( "steel" );
static const material_id material_stone( "stone" );
static const material_id material_veggy( "veggy" );
static const skill_id skill_bashing( "bashing" );
static const skill_id skill_cutting( "cutting" );
static const skill_id skill_stabbing( "stabbing" );
static const ter_str_id ter_t_bridge( "t_bridge" );
static const ter_str_id ter_t_chainfence( "t_chainfence" );
static const ter_str_id ter_t_clay( "t_clay" );
static const ter_str_id ter_t_claymound( "t_claymound" );
static const ter_str_id ter_t_conveyor( "t_conveyor" );
static const ter_str_id ter_t_dirt( "t_dirt" );
static const ter_str_id ter_t_dirtfloor( "t_dirtfloor" );
static const ter_str_id ter_t_dirtmound( "t_dirtmound" );
static const ter_str_id ter_t_dirtmoundfloor( "t_dirtmoundfloor" );
static const ter_str_id ter_t_elevator( "t_elevator" );
static const ter_str_id ter_t_golf_hole( "t_golf_hole" );
static const ter_str_id ter_t_grass( "t_grass" );
static const ter_str_id ter_t_grass_dead( "t_grass_dead" );
static const ter_str_id ter_t_grass_golf( "t_grass_golf" );
static const ter_str_id ter_t_grass_long( "t_grass_long" );
static const ter_str_id ter_t_grass_tall( "t_grass_tall" );
static const ter_str_id ter_t_grass_white( "t_grass_white" );
static const ter_str_id ter_t_grate( "t_grate" );
static const ter_str_id ter_t_guardrail_bg_dp( "t_guardrail_bg_dp" );
static const ter_str_id ter_t_machinery_electronic( "t_machinery_electronic" );
static const ter_str_id ter_t_machinery_heavy( "t_machinery_heavy" );
static const ter_str_id ter_t_machinery_light( "t_machinery_light" );
static const ter_str_id ter_t_machinery_old( "t_machinery_old" );
static const ter_str_id ter_t_metal_floor( "t_metal_floor" );
static const ter_str_id ter_t_moss( "t_moss" );
static const ter_str_id ter_t_ov_smreb_cage( "t_ov_smreb_cage" );
static const ter_str_id ter_t_palisade_gate_o( "t_palisade_gate_o" );
static const ter_str_id ter_t_railroad_rubble( "t_railroad_rubble" );
static const ter_str_id ter_t_railroad_tie( "t_railroad_tie" );
static const ter_str_id ter_t_railroad_tie_d( "t_railroad_tie_d" );
static const ter_str_id ter_t_railroad_tie_d1( "t_railroad_tie_d1" );
static const ter_str_id ter_t_railroad_tie_d2( "t_railroad_tie_d2" );
static const ter_str_id ter_t_railroad_tie_h( "t_railroad_tie_h" );
static const ter_str_id ter_t_railroad_tie_v( "t_railroad_tie_v" );
static const ter_str_id ter_t_railroad_track( "t_railroad_track" );
static const ter_str_id ter_t_railroad_track_d( "t_railroad_track_d" );
static const ter_str_id ter_t_railroad_track_d1( "t_railroad_track_d1" );
static const ter_str_id ter_t_railroad_track_d2( "t_railroad_track_d2" );
static const ter_str_id ter_t_railroad_track_d_on_tie( "t_railroad_track_d_on_tie" );
static const ter_str_id ter_t_railroad_track_h( "t_railroad_track_h" );
static const ter_str_id ter_t_railroad_track_h_on_tie( "t_railroad_track_h_on_tie" );
static const ter_str_id ter_t_railroad_track_on_tie( "t_railroad_track_on_tie" );
static const ter_str_id ter_t_railroad_track_v( "t_railroad_track_v" );
static const ter_str_id ter_t_railroad_track_v_on_tie( "t_railroad_track_v_on_tie" );
static const ter_str_id ter_t_rootcellar( "t_rootcellar" );
static const ter_str_id ter_t_sand( "t_sand" );
static const ter_str_id ter_t_sandbox( "t_sandbox" );
static const ter_str_id ter_t_sandmound( "t_sandmound" );
static const ter_str_id ter_t_shrub( "t_shrub" );
static const ter_str_id ter_t_shrub_blackberry( "t_shrub_blackberry" );
static const ter_str_id ter_t_shrub_blackberry_harvested( "t_shrub_blackberry_harvested" );
static const ter_str_id ter_t_shrub_blueberry( "t_shrub_blueberry" );
static const ter_str_id ter_t_shrub_blueberry_harvested( "t_shrub_blueberry_harvested" );
static const ter_str_id ter_t_shrub_grape( "t_shrub_grape" );
static const ter_str_id ter_t_shrub_grape_harvested( "t_shrub_grape_harvested" );
static const ter_str_id ter_t_shrub_huckleberry( "t_shrub_huckleberry" );
static const ter_str_id ter_t_shrub_huckleberry_harvested( "t_shrub_huckleberry_harvested" );
static const ter_str_id ter_t_shrub_hydrangea( "t_shrub_hydrangea" );
static const ter_str_id ter_t_shrub_hydrangea_harvested( "t_shrub_hydrangea_harvested" );
static const ter_str_id ter_t_shrub_lilac( "t_shrub_lilac" );
static const ter_str_id ter_t_shrub_lilac_harvested( "t_shrub_lilac_harvested" );
static const ter_str_id ter_t_shrub_peanut( "t_shrub_peanut" );
static const ter_str_id ter_t_shrub_peanut_harvested( "t_shrub_peanut_harvested" );
static const ter_str_id ter_t_shrub_raspberry( "t_shrub_raspberry" );
static const ter_str_id ter_t_shrub_raspberry_harvested( "t_shrub_raspberry_harvested" );
static const ter_str_id ter_t_shrub_rose( "t_shrub_rose" );
static const ter_str_id ter_t_shrub_rose_harvested( "t_shrub_rose_harvested" );
static const ter_str_id ter_t_shrub_strawberry( "t_shrub_strawberry" );
static const ter_str_id ter_t_shrub_strawberry_harvested( "t_shrub_strawberry_harvested" );
static const ter_str_id ter_t_slide( "t_slide" );
static const ter_str_id ter_t_stump( "t_stump" );
static const ter_str_id ter_t_trunk( "t_trunk" );
static const ter_str_id ter_t_underbrush( "t_underbrush" );
static const ter_str_id ter_t_underbrush_harvested_autumn( "t_underbrush_harvested_autumn" );
static const ter_str_id ter_t_underbrush_harvested_spring( "t_underbrush_harvested_spring" );
static const ter_str_id ter_t_underbrush_harvested_summer( "t_underbrush_harvested_summer" );
static const ter_str_id ter_t_underbrush_harvested_winter( "t_underbrush_harvested_winter" );
static const trait_id trait_HEAVYSLEEPER( "HEAVYSLEEPER" );
static const trait_id trait_HEAVYSLEEPER2( "HEAVYSLEEPER2" );
static const trait_id trait_NOPAIN( "NOPAIN" );
struct monster_sound_event {
int volume;
bool provocative;
};
struct sound_event {
int volume;
sounds::sound_t category;
std::string description;
bool ambient;
bool footstep;
std::string id;
std::string variant;
std::string season;
};
struct centroid {
// Values have to be floats to prevent rounding errors.
float x;
float y;
float z;
float volume;
float weight;
bool provocative;
};
namespace io
{
// *INDENT-OFF*
template<>
std::string enum_to_string<sounds::sound_t>( sounds::sound_t data )
{
switch ( data ) {
case sounds::sound_t::background: return "background";
case sounds::sound_t::weather: return "weather";
case sounds::sound_t::music: return "music";
case sounds::sound_t::movement: return "movement";
case sounds::sound_t::speech: return "speech";
case sounds::sound_t::electronic_speech: return "electronic_speech";
case sounds::sound_t::activity: return "activity";
case sounds::sound_t::destructive_activity: return "destructive_activity";
case sounds::sound_t::alarm: return "alarm";
case sounds::sound_t::combat: return "combat";
case sounds::sound_t::alert: return "alert";
case sounds::sound_t::order: return "order";
case sounds::sound_t::LAST: break;
}
cata_fatal( "Invalid valid_target" );
}
// *INDENT-ON*
} // namespace io
// Static globals tracking sounds events of various kinds.
// The sound events since the last monster turn.
static std::vector<std::pair<tripoint, monster_sound_event>> recent_sounds;
// The sound events since the last interactive player turn. (doesn't count sleep etc)
static std::vector<std::pair<tripoint, sound_event>> sounds_since_last_turn;
// The sound events currently displayed to the player.
static std::unordered_map<tripoint, sound_event> sound_markers;
// This is an attempt to handle attenuation of sound for underground areas.
// The main issue it addresses is that you can hear activity
// relatively deep underground while on the surface.
// My research indicates that attenuation through soil-like materials is as
// high as 100x the attenuation through air, plus vertical distances are
// roughly five times as large as horizontal ones.
static int sound_distance( const tripoint &source, const tripoint &sink )
{
const int lower_z = std::min( source.z, sink.z );
const int upper_z = std::max( source.z, sink.z );
const int vertical_displacement = upper_z - lower_z;
int vertical_attenuation = vertical_displacement;
if( lower_z < 0 && vertical_displacement > 0 ) {
// Apply a moderate bonus attenuation (5x) for the first level of vertical displacement.
vertical_attenuation += 4;
// At displacements greater than one, apply a large additional attenuation (100x) per level.
const int underground_displacement = std::min( -lower_z, vertical_displacement );
vertical_attenuation += ( underground_displacement - 1 ) * 20;
}
// Regardless of underground effects, scale the vertical distance by 5x.
vertical_attenuation *= 5;
return rl_dist( source.xy(), sink.xy() ) + vertical_attenuation;
}
static std::string season_str( const season_type &season )
{
switch( season ) {
case season_type::SPRING:
return "spring";
case season_type::SUMMER:
return "summer";
case season_type::AUTUMN:
return "autumn";
case season_type::WINTER:
return "winter";
default:
return "";
}
}
static bool is_provocative( sounds::sound_t category )
{
switch( category ) {
case sounds::sound_t::background:
case sounds::sound_t::weather:
case sounds::sound_t::music:
case sounds::sound_t::activity:
case sounds::sound_t::destructive_activity:
case sounds::sound_t::alarm:
case sounds::sound_t::combat:
case sounds::sound_t::movement:
return false;
case sounds::sound_t::speech:
case sounds::sound_t::electronic_speech:
case sounds::sound_t::alert:
case sounds::sound_t::order:
return true;
case sounds::sound_t::LAST:
break;
}
cata_fatal( "Invalid sound_t category" );
}
void sounds::ambient_sound( const tripoint &p, int vol, sound_t category,
const std::string &description )
{
sound( p, vol, category, description, true );
}
void sounds::sound( const tripoint &p, int vol, sound_t category, const std::string &description,
bool ambient, const std::string &id, const std::string &variant )
{
if( vol < 0 ) {
// Bail out if no volume.
debugmsg( "negative sound volume %d", vol );
return;
}
// Description is not an optional parameter
if( description.empty() ) {
debugmsg( "Sound at %d:%d has no description!", p.x, p.y );
}
const season_type seas = season_of_year( calendar::turn );
const std::string seas_str = season_str( seas );
recent_sounds.emplace_back( std::make_pair( p, monster_sound_event{ vol, is_provocative( category ) } ) );
sounds_since_last_turn.emplace_back( std::make_pair( p,
sound_event { vol, category, description, ambient,
false, id, variant, seas_str } ) );
}
void sounds::sound( const tripoint &p, int vol, sound_t category, const translation &description,
bool ambient, const std::string &id, const std::string &variant )
{
sounds::sound( p, vol, category, description.translated(), ambient, id, variant );
}
void sounds::add_footstep( const tripoint &p, int volume, int, monster *,
const std::string &footstep )
{
const season_type seas = season_of_year( calendar::turn );
const std::string seas_str = season_str( seas );
sounds_since_last_turn.emplace_back( std::make_pair( p, sound_event { volume,
sound_t::movement, footstep, false, true, "", "", seas_str} ) );
}
template <typename C>
static void vector_quick_remove( std::vector<C> &source, int index )
{
if( source.size() != 1 ) {
// Swap the target and the last element of the vector.
// This scrambles the vector, but makes removal O(1).
std::iter_swap( source.begin() + index, source.end() - 1 );
}
source.pop_back();
}
static std::vector<centroid> cluster_sounds( std::vector<std::pair<tripoint, monster_sound_event>>
input_sounds )
{
// If there are too many monsters and too many noise sources (which can be monsters, go figure),
// applying sound events to monsters can dominate processing time for the whole game,
// so we cluster sounds and apply the centroids of the sounds to the monster AI
// to fight the combinatorial explosion.
std::vector<centroid> sound_clusters;
if( input_sounds.empty() ) {
return sound_clusters;
}
const int num_seed_clusters =
std::max( std::min( input_sounds.size(), static_cast<size_t>( 10 ) ),
static_cast<size_t>( std::log( input_sounds.size() ) ) );
const size_t stopping_point = input_sounds.size() - num_seed_clusters;
const size_t max_map_distance = sound_distance( tripoint( point_zero, OVERMAP_DEPTH ),
tripoint( MAPSIZE_X, MAPSIZE_Y, OVERMAP_HEIGHT ) );
// Randomly choose cluster seeds.
for( size_t i = input_sounds.size(); i > stopping_point; i-- ) {
size_t index = rng( 0, i - 1 );
// The volume and cluster weight are the same for the first element.
sound_clusters.push_back(
// Assure the compiler that these int->float conversions are safe.
{
static_cast<float>( input_sounds[index].first.x ), static_cast<float>( input_sounds[index].first.y ),
static_cast<float>( input_sounds[index].first.z ),
static_cast<float>( input_sounds[index].second.volume ), static_cast<float>( input_sounds[index].second.volume ),
input_sounds[index].second.provocative
} );
vector_quick_remove( input_sounds, index );
}
for( const auto &sound_event_pair : input_sounds ) {
auto found_centroid = sound_clusters.begin();
float dist_factor = max_map_distance;
const auto cluster_end = sound_clusters.end();
for( auto centroid_iter = sound_clusters.begin(); centroid_iter != cluster_end;
++centroid_iter ) {
// Scale the distance between the two by the max possible distance.
tripoint centroid_pos { static_cast<int>( centroid_iter->x ), static_cast<int>( centroid_iter->y ), static_cast<int>( centroid_iter->z ) };
const int dist = sound_distance( sound_event_pair.first, centroid_pos );
if( dist * dist < dist_factor ) {
found_centroid = centroid_iter;
dist_factor = dist * dist;
}
}
const float volume_sum = static_cast<float>( sound_event_pair.second.volume ) +
found_centroid->weight;
// Set the centroid location to the average of the two locations, weighted by volume.
found_centroid->x = static_cast<float>( ( sound_event_pair.first.x *
sound_event_pair.second.volume ) +
( found_centroid->x * found_centroid->weight ) ) / volume_sum;
found_centroid->y = static_cast<float>( ( sound_event_pair.first.y *
sound_event_pair.second.volume ) +
( found_centroid->y * found_centroid->weight ) ) / volume_sum;
found_centroid->z = static_cast<float>( ( sound_event_pair.first.z *
sound_event_pair.second.volume ) +
( found_centroid->z * found_centroid->weight ) ) / volume_sum;
// Set the centroid volume to the larger of the volumes.
found_centroid->volume = std::max( found_centroid->volume,
static_cast<float>( sound_event_pair.second.volume ) );
// Set the centroid weight to the sum of the weights.
found_centroid->weight = volume_sum;
// Set and keep provocative if any sound in the centroid is provocative
found_centroid->provocative |= sound_event_pair.second.provocative;
}
return sound_clusters;
}
static int get_signal_for_hordes( const centroid ¢r )
{
//Volume in tiles. Signal for hordes in submaps
//modify vol using weather vol.Weather can reduce monster hearing
const int vol = centr.volume - get_weather().weather_id->sound_attn;
const int min_vol_cap = 60; //Hordes can't hear volume lower than this
const int underground_div = 2; //Coefficient for volume reduction underground
const int hordes_sig_div = SEEX; //Divider coefficient for hordes
const int min_sig_cap = 8; //Signal for hordes can't be lower that this if it pass min_vol_cap
const int max_sig_cap = 26; //Signal for hordes can't be higher that this
//Lower the level - lower the sound
int vol_hordes = ( ( centr.z < 0 ) ? vol / ( underground_div * std::abs( centr.z ) ) : vol );
if( vol_hordes > min_vol_cap ) {
//Calculating horde hearing signal
int sig_power = std::ceil( static_cast<float>( vol_hordes ) / hordes_sig_div );
//Capping minimum horde hearing signal
sig_power = std::max( sig_power, min_sig_cap );
//Capping extremely high signal to hordes
sig_power = std::min( sig_power, max_sig_cap );
add_msg_debug( debugmode::DF_SOUND, "vol %d vol_hordes %d sig_power %d ", vol, vol_hordes,
sig_power );
return sig_power;
}
return 0;
}
void sounds::process_sounds()
{
std::vector<centroid> sound_clusters = cluster_sounds( recent_sounds );
const int weather_vol = get_weather().weather_id->sound_attn;
for( const centroid &this_centroid : sound_clusters ) {
// Since monsters don't go deaf ATM we can just use the weather modified volume
// If they later get physical effects from loud noises we'll have to change this
// to use the unmodified volume for those effects.
const int vol = this_centroid.volume - weather_vol;
const tripoint source = tripoint( this_centroid.x, this_centroid.y, this_centroid.z );
// --- Monster sound handling here ---
// Alert all hordes
int sig_power = get_signal_for_hordes( this_centroid );
if( sig_power > 0 ) {
const point abs_ms = get_map().getabs( source.xy() );
// TODO: fix point types
const point_abs_sm abs_sm( ms_to_sm_copy( abs_ms ) );
const tripoint_abs_sm target( abs_sm, source.z );
overmap_buffer.signal_hordes( target, sig_power );
}
// Alert all monsters (that can hear) to the sound.
for( monster &critter : g->all_monsters() ) {
// TODO: Generalize this to Creature::hear_sound
const int dist = sound_distance( source, critter.pos() );
if( vol * 2 > dist ) {
// Exclude monsters that certainly won't hear the sound
critter.hear_sound( source, vol, dist, this_centroid.provocative );
}
}
}
recent_sounds.clear();
}
// skip some sounds to avoid message spam
static bool describe_sound( sounds::sound_t category, bool from_player_position )
{
if( from_player_position ) {
switch( category ) {
case sounds::sound_t::LAST:
debugmsg( "ERROR: Incorrect sound category" );
return false;
case sounds::sound_t::background:
case sounds::sound_t::weather:
case sounds::sound_t::music:
// detailed music descriptions are printed in iuse::play_music
case sounds::sound_t::movement:
case sounds::sound_t::activity:
case sounds::sound_t::destructive_activity:
case sounds::sound_t::combat:
case sounds::sound_t::alert:
case sounds::sound_t::order:
case sounds::sound_t::speech:
return false;
case sounds::sound_t::electronic_speech:
case sounds::sound_t::alarm:
return true;
}
} else {
switch( category ) {
case sounds::sound_t::background:
case sounds::sound_t::weather:
case sounds::sound_t::music:
case sounds::sound_t::movement:
case sounds::sound_t::activity:
case sounds::sound_t::destructive_activity:
return one_in( 100 );
case sounds::sound_t::speech:
case sounds::sound_t::electronic_speech:
case sounds::sound_t::alarm:
case sounds::sound_t::combat:
case sounds::sound_t::alert:
case sounds::sound_t::order:
return true;
case sounds::sound_t::LAST:
debugmsg( "ERROR: Incorrect sound category" );
return false;
}
}
return true;
}
void sounds::process_sound_markers( Character *you )
{
bool is_deaf = you->is_deaf();
const float volume_multiplier = you->hearing_ability();
const int weather_vol = get_weather().weather_id->sound_attn;
// NOLINTNEXTLINE(modernize-loop-convert)
for( std::size_t i = 0; i < sounds_since_last_turn.size(); i++ ) {
// copy values instead of making references here to fix use-after-free error
// sounds_since_last_turn may be inserted with new elements inside the loop
// so the references may become invalid after the vector enlarged its internal buffer
const tripoint pos = sounds_since_last_turn[i].first;
const sound_event sound = sounds_since_last_turn[i].second;
const int distance_to_sound = sound_distance( you->pos(), pos );
const int raw_volume = sound.volume;
// The felt volume of a sound is not affected by negative multipliers, such as already
// deafened players or players with sub-par hearing to begin with.
const int felt_volume = static_cast<int>( raw_volume * std::min( 1.0f,
volume_multiplier ) ) - distance_to_sound;
// Deafening is based on the felt volume, as a player may be too deaf to
// hear the deafening sound but still suffer additional hearing loss.
const bool is_sound_deafening = rng( felt_volume / 2, felt_volume ) >= 150;
// Deaf players hear no sound, but still are at risk of additional hearing loss.
if( is_deaf ) {
if( is_sound_deafening && !you->is_immune_effect( effect_deaf ) ) {
you->add_effect( effect_deaf, std::min( 4_minutes,
time_duration::from_turns( felt_volume - 130 ) / 8 ) );
if( !you->has_trait( trait_NOPAIN ) ) {
you->add_msg_if_player( m_bad, _( "Your eardrums suddenly ache!" ) );
if( you->get_pain() < 10 ) {
you->mod_pain( rng( 0, 2 ) );
}
}
}
continue;
}
if( is_sound_deafening && !you->is_immune_effect( effect_deaf ) ) {
const time_duration deafness_duration = time_duration::from_turns( felt_volume - 130 ) / 4;
you->add_effect( effect_deaf, deafness_duration );
if( you->is_deaf() && !is_deaf ) {
is_deaf = true;
continue;
}
}
// The heard volume of a sound is the player heard volume, regardless of true volume level.
const int heard_volume = static_cast<int>( ( raw_volume - weather_vol ) *
volume_multiplier ) - distance_to_sound;
if( heard_volume <= 0 && pos != you->pos() ) {
continue;
}
// Player volume meter includes all sounds from their tile and adjacent tiles
if( distance_to_sound <= 1 ) {
you->volume = std::max( you->volume, heard_volume );
}
// Noises from vehicle player is in.
if( you->controlling_vehicle ) {
vehicle *veh = veh_pointer_or_null( get_map().veh_at( you->pos() ) );
const int noise = veh ? static_cast<int>( veh->vehicle_noise ) : 0;
you->volume = std::max( you->volume, noise );
}
// Secure the flag before wake_up() clears the effect
bool slept_through = you->has_effect( effect_slept_through_alarm );
// See if we need to wake someone up
if( you->has_effect( effect_sleep ) ) {
if( ( ( !( you->has_trait( trait_HEAVYSLEEPER ) ||
you->has_trait( trait_HEAVYSLEEPER2 ) ) && dice( 2, 15 ) < heard_volume ) ||
( you->has_trait( trait_HEAVYSLEEPER ) && dice( 3, 15 ) < heard_volume ) ||
( you->has_trait( trait_HEAVYSLEEPER2 ) && dice( 6, 15 ) < heard_volume ) ) &&
!you->has_effect( effect_narcosis ) &&
!you->has_bionic( bio_sleep_shutdown ) ) {
//Not kidding about sleep-through-firefight
you->wake_up();
add_msg( m_warning, _( "Something is making noise." ) );
} else {
continue;
}
}
const std::string &description = sound.description.empty() ? _( "a noise" ) : sound.description;
if( you->is_npc() ) {
if( !sound.ambient ) {
npc *guy = dynamic_cast<npc *>( you );
guy->handle_sound( sound.category, description, heard_volume, pos );
}
continue;
}
if( sound.category == sound_t::music ) {
music::activate_music_id( music::music_id::sound );
}
// don't print our own noise or things without descriptions
if( !sound.ambient && ( pos != you->pos() ) && !get_map().pl_sees( pos, distance_to_sound ) ) {
if( uistate.distraction_noise &&
!you->activity.is_distraction_ignored( distraction_type::noise ) &&
!get_safemode().is_sound_safe( sound.description, distance_to_sound, you->controlling_vehicle ) ) {
const std::string query = string_format( _( "Heard %s!" ),
trim_trailing_punctuations( description ) );
g->cancel_activity_or_ignore_query( distraction_type::noise, query );
}
}
// skip some sounds to avoid message spam
const bool from_player = pos == you->pos() || ( sound.category == sound_t::movement &&
distance_to_sound <= 1 );
if( describe_sound( sound.category, from_player ) ) {
game_message_type severity = m_info;
if( sound.category == sound_t::combat || sound.category == sound_t::alarm ) {
severity = m_warning;
}
// if we can see it, don't print a direction
if( pos == you->pos() ) {
add_msg( severity, _( "From your position you hear %1$s" ), description );
} else if( you->sees( pos ) ) {
add_msg( severity, _( "You hear %1$s" ), description );
} else {
std::string direction = direction_name( direction_from( you->pos(), pos ) );
add_msg( severity, _( "From the %1$s you hear %2$s" ), direction, description );
}
}
if( !you->has_effect( effect_sleep ) && you->has_effect( effect_alarm_clock ) &&
!you->has_flag( STATIC( json_character_flag( "ALARMCLOCK" ) ) ) ) {
// if we don't have effect_sleep but we're in_sleep_state, either
// we were trying to fall asleep for so long our alarm is now going
// off or something disturbed us while trying to sleep
const bool trying_to_sleep = you->in_sleep_state();
if( you->get_effect( effect_alarm_clock ).get_duration() == 1_turns ) {
if( slept_through ) {
add_msg( _( "Your alarm clock finally wakes you up." ) );
} else if( !trying_to_sleep ) {
add_msg( _( "Your alarm clock wakes you up." ) );
} else {
add_msg( _( "Your alarm clock goes off and you haven't slept a wink." ) );
you->activity.set_to_null();
}
add_msg( _( "You turn off your alarm-clock." ) );
you->get_effect( effect_alarm_clock ).set_duration( 0_turns );
}
}
const std::string &sfx_id = sound.id;
const std::string &sfx_variant = sound.variant;
const std::string &sfx_season = sound.season;
const bool indoors = !is_creature_outside( get_player_character() );
const bool night = is_night( calendar::turn );
if( !sfx_id.empty() ) {
sfx::play_variant_sound( sfx_id, sfx_variant, sfx_season, indoors, night,
sfx::get_heard_volume( pos ) );
}
// Place footstep markers.
if( pos == you->pos() || you->sees( pos ) ) {
// If we are or can see the source, don't draw a marker.
continue;
}
int err_offset;
if( ( heard_volume + distance_to_sound ) / distance_to_sound < 2 ) {
err_offset = 3;
} else if( ( heard_volume + distance_to_sound ) / distance_to_sound < 3 ) {
err_offset = 2;
} else {
err_offset = 1;
}
// If Z-coordinate is different, draw even when you can see the source
const bool diff_z = pos.z != you->posz();
// Enumerate the valid points the player *cannot* see.
// Unless the source is on a different z-level, then any point is fine
std::vector<tripoint> unseen_points;
for( const tripoint &newp : get_map().points_in_radius( pos, err_offset ) ) {
if( diff_z || !you->sees( newp ) ) {
unseen_points.emplace_back( newp );
}
}
// Then place the sound marker in a random one.
if( !unseen_points.empty() ) {
sound_markers.emplace( random_entry( unseen_points ), sound );
}
}
if( you->is_avatar() ) {
sounds_since_last_turn.clear();
}
}
void sounds::reset_sounds()
{
recent_sounds.clear();
sounds_since_last_turn.clear();
sound_markers.clear();
}
void sounds::reset_markers()
{
sound_markers.clear();
}
std::vector<tripoint> sounds::get_footstep_markers()
{
// Optimization, make this static and clear it in reset_markers?
std::vector<tripoint> footsteps;
footsteps.reserve( sound_markers.size() );
for( const auto &mark : sound_markers ) {
footsteps.push_back( mark.first );
}
return footsteps;
}
std::pair<std::vector<tripoint>, std::vector<tripoint>> sounds::get_monster_sounds()
{
auto sound_clusters = cluster_sounds( recent_sounds );
std::vector<tripoint> sound_locations;
sound_locations.reserve( recent_sounds.size() );
for( const auto &sound : recent_sounds ) {
sound_locations.push_back( sound.first );
}
std::vector<tripoint> cluster_centroids;
cluster_centroids.reserve( sound_clusters.size() );
for( const centroid &sound : sound_clusters ) {
cluster_centroids.emplace_back( static_cast<int>( sound.x ), static_cast<int>( sound.y ),
static_cast<int>( sound.z ) );
}
return { sound_locations, cluster_centroids };
}
std::string sounds::sound_at( const tripoint &location )
{
auto this_sound = sound_markers.find( location );
if( this_sound == sound_markers.end() ) {
return std::string();
}
if( !this_sound->second.description.empty() ) {
return this_sound->second.description;
}
return _( "a sound" );
}
#if defined(SDL_SOUND)
void sfx::fade_audio_group( group group, int duration )
{
if( test_mode ) {
return;
}
Mix_FadeOutGroup( static_cast<int>( group ), duration );
}
void sfx::fade_audio_channel( channel channel, int duration )
{
if( test_mode ) {
return;
}
Mix_FadeOutChannel( static_cast<int>( channel ), duration );
}
bool sfx::is_channel_playing( channel channel )
{
if( test_mode ) {
return false;
}
return Mix_Playing( static_cast<int>( channel ) ) != 0;
}
void sfx::stop_sound_effect_fade( channel channel, int duration )
{
if( test_mode ) {
return;
}
if( Mix_FadeOutChannel( static_cast<int>( channel ), duration ) == -1 ) {
dbg( D_ERROR ) << "Failed to stop sound effect: " << Mix_GetError();
}
}
void sfx::stop_sound_effect_timed( channel channel, int time )
{
if( test_mode ) {
return;
}
Mix_ExpireChannel( static_cast<int>( channel ), time );
}
int sfx::set_channel_volume( channel channel, int volume )
{
if( test_mode ) {
return 0;
}
int ch = static_cast<int>( channel );
if( !Mix_Playing( ch ) ) {
return -1;
}
if( Mix_FadingChannel( ch ) != MIX_NO_FADING ) {
return -1;
}
return Mix_Volume( ch, volume );
}
void sfx::do_vehicle_engine_sfx()
{
if( test_mode ) {
return;
}
static const channel ch = channel::interior_engine_sound;
const Character &player_character = get_player_character();
if( !player_character.in_vehicle ) {
fade_audio_channel( ch, 300 );
add_msg_debug( debugmode::DF_SOUND, "STOP interior_engine_sound, OUT OF CAR" );
return;
}
if( player_character.in_sleep_state() && !audio_muted ) {
fade_audio_channel( channel::any, 300 );
audio_muted = true;
return;
} else if( player_character.in_sleep_state() && audio_muted ) {
return;
}
optional_vpart_position vpart_opt = get_map().veh_at( player_character.pos() );
vehicle *veh;
if( vpart_opt.has_value() ) {
veh = &vpart_opt->vehicle();
} else {
return;
}
if( !veh->engine_on ) {
fade_audio_channel( ch, 100 );
add_msg_debug( debugmode::DF_SOUND, "STOP interior_engine_sound" );
return;
}
std::pair<std::string, std::string> id_and_variant;
const season_type seas = season_of_year( calendar::turn );
const std::string seas_str = season_str( seas );
const bool indoors = !is_creature_outside( player_character );
const bool night = is_night( calendar::turn );
for( size_t e = 0; e < veh->engines.size(); ++e ) {
if( veh->is_engine_on( e ) ) {
if( sfx::has_variant_sound( "engine_working_internal",
veh->part_info( veh->engines[ e ] ).get_id().str(),
seas_str, indoors, night ) ) {
id_and_variant = std::make_pair( "engine_working_internal",
veh->part_info( veh->engines[ e ] ).get_id().str() );
} else if( veh->is_engine_type( e, fuel_type_muscle ) ) {
id_and_variant = std::make_pair( "engine_working_internal", "muscle" );
} else if( veh->is_engine_type( e, fuel_type_wind ) ) {
id_and_variant = std::make_pair( "engine_working_internal", "wind" );
} else if( veh->is_engine_type( e, fuel_type_battery ) ) {
id_and_variant = std::make_pair( "engine_working_internal", "electric" );
} else {
id_and_variant = std::make_pair( "engine_working_internal", "combustion" );
}
}
}
if( !is_channel_playing( ch ) ) {
play_ambient_variant_sound( id_and_variant.first, id_and_variant.second,
seas_str, indoors, night,
sfx::get_heard_volume( player_character.pos() ), ch, 1000 );
add_msg_debug( debugmode::DF_SOUND, "START %s %s", id_and_variant.first, id_and_variant.second );
} else {
add_msg_debug( debugmode::DF_SOUND, "PLAYING" );
}
int current_speed = veh->velocity;
bool in_reverse = false;
if( current_speed <= -1 ) {
current_speed = current_speed * -1;
in_reverse = true;
}
double pitch = 1.0;
int safe_speed = veh->safe_velocity();
int current_gear;
if( in_reverse ) {
current_gear = -1;
} else if( current_speed == 0 ) {
current_gear = 0;
} else if( current_speed > 0 && current_speed <= safe_speed / 12 ) {
current_gear = 1;
} else if( current_speed > safe_speed / 12 && current_speed <= safe_speed / 5 ) {
current_gear = 2;
} else if( current_speed > safe_speed / 5 && current_speed <= safe_speed / 4 ) {
current_gear = 3;
} else if( current_speed > safe_speed / 4 && current_speed <= safe_speed / 3 ) {
current_gear = 4;
} else if( current_speed > safe_speed / 3 && current_speed <= safe_speed / 2 ) {
current_gear = 5;
} else {
current_gear = 6;
}
if( veh->has_engine_type( fuel_type_muscle, true ) ||
veh->has_engine_type( fuel_type_wind, true ) ) {
current_gear = previous_gear;
}
if( current_gear > previous_gear ) {
play_variant_sound( "vehicle", "gear_shift", seas_str, indoors, night,
get_heard_volume( player_character.pos() ), 0_degrees, 0.8, 0.8 );
add_msg_debug( debugmode::DF_SOUND, "GEAR UP" );
} else if( current_gear < previous_gear ) {
play_variant_sound( "vehicle", "gear_shift", seas_str, indoors, night,
get_heard_volume( player_character.pos() ), 0_degrees, 1.2, 1.2 );
add_msg_debug( debugmode::DF_SOUND, "GEAR DOWN" );
}
if( safe_speed != 0 ) {
if( current_gear == 0 ) {
pitch = 1.0;
} else if( current_gear == -1 ) {
pitch = 1.2;
} else {
pitch = 1.0 - static_cast<double>( current_speed ) / static_cast<double>( safe_speed );
}
}
if( pitch <= 0.5 ) {
pitch = 0.5;
}
if( current_speed != previous_speed ) {
Mix_HaltChannel( static_cast<int>( ch ) );
add_msg_debug( debugmode::DF_SOUND, "STOP speed %d =/= %d", current_speed, previous_speed );
play_ambient_variant_sound( id_and_variant.first, id_and_variant.second,
seas_str, indoors, night,
sfx::get_heard_volume( player_character.pos() ), ch, 1000, pitch );
add_msg_debug( debugmode::DF_SOUND, "PITCH %f", pitch );
}
previous_speed = current_speed;
previous_gear = current_gear;
}
void sfx::do_vehicle_exterior_engine_sfx()
{
if( test_mode ) {
return;
}
static const channel ch = channel::exterior_engine_sound;
static const int ch_int = static_cast<int>( ch );
const Character &player_character = get_player_character();
// early bail-outs for efficiency
if( player_character.in_vehicle ) {
fade_audio_channel( ch, 300 );
add_msg_debug( debugmode::DF_SOUND, "STOP exterior_engine_sound, IN CAR" );
return;
}
if( player_character.in_sleep_state() && !audio_muted ) {
fade_audio_channel( channel::any, 300 );
audio_muted = true;
return;
} else if( player_character.in_sleep_state() && audio_muted ) {
return;
}
VehicleList vehs = get_map().get_vehicles();
unsigned char noise_factor = 0;
unsigned char vol = 0;
vehicle *veh = nullptr;
for( wrapped_vehicle vehicle : vehs ) {
if( vehicle.v->vehicle_noise > 0 &&