Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPCs can funk out to music, too. #77137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -137,7 +137,7 @@
static const json_character_flag json_flag_BIONIC_LIMB( "BIONIC_LIMB" );
static const json_character_flag json_flag_MANUAL_CBM_INSTALLATION( "MANUAL_CBM_INSTALLATION" );

static const morale_type morale_music( "morale_music" );

Check failure on line 140 in src/iuse_actor.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Variable 'morale_music' declared but not used. [cata-unused-statics,-warnings-as-errors]
static const morale_type morale_pyromania_nofire( "morale_pyromania_nofire" );
static const morale_type morale_pyromania_startfire( "morale_pyromania_startfire" );

Expand Down Expand Up @@ -2243,14 +2243,14 @@

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
Loading