Skip to content

Commit

Permalink
NPCs can funk out to music, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenechCDDA committed Oct 18, 2024
1 parent 79b8ed9 commit 56fd799
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
44 changes: 29 additions & 15 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3858,38 +3858,52 @@ static std::string get_music_description()
}

void iuse::play_music( Character *p, const tripoint &source, const int volume,
const int max_morale )
const int max_morale, bool play_sounds )
{
// TODO: what about other "player", e.g. when a NPC is listening or when the PC is listening,
// the other characters around should be able to profit as well.
const bool do_effects = p && p->can_hear( source, volume ) && !p->in_sleep_state();
std::string sound = "music";

auto lambda_should_do_effects = [&source, &volume]( Character * p ) {
return p && p->can_hear( source, volume ) && !p->in_sleep_state();
};

auto lambda_add_music_effects = [&max_morale, &volume]( Character & guy ) {
guy.add_effect( effect_music, 1_turns );
guy.add_morale( morale_music, 1, max_morale, 5_minutes, 2_minutes, true );
// mp3 player reduces hearing
if( volume == 0 ) {
guy.add_effect( effect_earphones, 1_turns );
}
};

// check NPCs that can hear the source of the music
for( npc &guy : g->all_npcs() ) {
if( guy.is_active() && lambda_should_do_effects( &guy ) ) {
lambda_add_music_effects( guy );
}
}

// player is not a NPC so they need to check separately
Character &player_character = get_player_character();
if( lambda_should_do_effects( &player_character ) ) {
lambda_add_music_effects( player_character );
}

if( calendar::once_every( time_duration::from_minutes(
get_option<int>( "DESCRIBE_MUSIC_FREQUENCY" ) ) ) ) {
// Every X minutes, describe the music
const std::string music = get_music_description();
if( !music.empty() ) {
sound = music;
// descriptions aren't printed for sounds at our position
if( do_effects && p->pos() == source ) {
if( lambda_should_do_effects( p ) && p->pos() == source ) {
p->add_msg_if_player( _( "You listen to %s" ), music );
}
}
}

if( volume != 0 ) {
if( volume != 0 && play_sounds ) {
sounds::ambient_sound( source, volume, sounds::sound_t::music, sound );
}

if( do_effects ) {
p->add_effect( effect_music, 1_turns );
p->add_morale( morale_music, 1, max_morale, 5_minutes, 2_minutes, true );
// mp3 player reduces hearing
if( volume == 0 ) {
p->add_effect( effect_earphones, 1_turns );
}
}
}

std::optional<int> iuse::mp3_on( Character *p, item *, const tripoint &pos )
Expand Down
3 changes: 2 additions & 1 deletion src/iuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ std::optional<int> disassemble( Character *, item *, const tripoint & );

// Helper functions for other iuse functions
void cut_log_into_planks( Character & );
void play_music( Character *p, const tripoint &source, int volume, int max_morale );
void play_music( Character *p, const tripoint &source, int volume, int max_morale,
bool play_sounds = true );
std::optional<int> purify_water( Character *p, item *purifier, item_location &water );
int towel_common( Character *, item *, bool );

Expand Down
10 changes: 5 additions & 5 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,14 +2243,14 @@ std::optional<int> musical_instrument_actor::use( Character *p, item &it,

if( !p->has_effect( effect_music ) && p->can_hear( p->pos(), volume ) ) {
// Sound code doesn't describe noises at the player position
if( p->is_avatar() && desc != "music" ) {
add_msg( m_info, desc );
if( desc != "music" ) {
p->add_msg_if_player( m_info, desc );
}
p->add_effect( effect_music, 1_turns );
const int sign = morale_effect > 0 ? 1 : -1;
p->add_morale( morale_music, sign, morale_effect, 5_minutes, 2_minutes, true );
}

// We already played the sounds, just handle applying effects now
iuse::play_music( p, p->pos(), volume, morale_effect, /*play_sounds=*/false );

return 0;
}

Expand Down

0 comments on commit 56fd799

Please sign in to comment.