Skip to content

Commit

Permalink
npc, sound: pass sound information to NPCs
Browse files Browse the repository at this point in the history
call process_sound_markers() on all NPCs within MAP_VIEW_DISTANCE of the
player, and have them call a new npc::handle_sound() function for each
sound marker, and then bail before actually placing the sound markers
which remains a player only function.

npc::handle_sound() ignores speech (for not) and sounds from locations
that the NPC can see.  If the NPC currently has a low assessment of
danger and hears combat noises or movement, they will complain to the
player.
  • Loading branch information
mlangsdorf committed Dec 20, 2018
1 parent ec31d71 commit fe3a4bd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions data/json/npcs/talk_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,28 @@
"You're gonna rot in hell for this!"
]
},
{
"type": "snippet",
"category": "<combat_noise_warning>",
"text": [
"That sounds bad.",
"Be alert, something is up!",
"Did you hear that?",
"What's that noise?",
"Who's there?"
]
},
{
"type": "snippet",
"category": "<movement_noise_warning>",
"text": [
"I hear something moving - sounded like",
"What's that sound? I heard",
"Who's there? I heard",
"Did you hear that? Sounded like",
"Who is making that sound? I can hear the"
]
},
{
"type": "snippet",
"category": "<BGSS_intro_question>",
Expand Down
7 changes: 7 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,13 @@ bool game::do_turn()

perhaps_add_random_npc();

// Process NPC sound events before they move or they hear themselves talking
for( npc &guy : all_npcs() ) {
if( rl_dist( guy.pos(), u.pos() ) < MAX_VIEW_DISTANCE ) {
sounds::process_sound_markers( &guy );
}
}

process_activity();

// Process sound events into sound markers for display to the player.
Expand Down
4 changes: 4 additions & 0 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@ class npc : public player
void warn_about( const std::string &type, const time_duration &d = 10_minutes,
const std::string &name = "" );
bool complain(); // Finds something to complain about and complains. Returns if complained.

void handle_sound( int priority, const std::string &description, int heard_volume,
const tripoint &spos );

/* shift() works much like monster::shift(), and is called when the player moves
* from one submap to an adjacent submap. It updates our position (shifting by
* 12 tiles), as well as our plans.
Expand Down
4 changes: 4 additions & 0 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3214,6 +3214,10 @@ void npc::warn_about( const std::string &type, const time_duration &d, const std
snip = is_enemy() ? "<kill_npc_h>" : "<kill_npc>";
} else if( type == "kill_player" ) {
snip = is_enemy() ? "<kill_player_h>" : "";
} else if( type == "combat_noise" ) {
snip = "<combat_noise_warning>";
} else if( type == "movement_noise" ) {
snip = "<movement_noise_warning>";
} else {
return;
}
Expand Down
26 changes: 26 additions & 0 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ void game::chat()
refresh_all();
}

void npc::handle_sound( int priority, const std::string &description, int heard_volume,
const tripoint &spos )
{
if( priority == 7 || sees( spos ) ) {
return;
}
add_msg( m_debug, "%s heard '%s', priority %d at volume %d from %d:%d, my pos %d:%d",
disp_name(), description, priority, heard_volume, spos.x, spos.y, pos().x, pos().y );
switch( priority ) {
case 6: // combat noise is only worth comment if we're not fighting
// TODO: Brave NPCs should be less jumpy
if( ai_cache.total_danger < 1.0f ) {
warn_about( "combat_noise", rng( 1, 10 ) * 1_minutes );
}
break;
case 4: // movement is is only worth comment if we're not fighting and out of a vehicle
if( ai_cache.total_danger < 1.0f && !in_vehicle ) {
// replace with warn_about when that merges
warn_about( "footsteps", rng( 1, 10 ) * 1_minutes, description );
}
break;
default:
break;
};
}

void npc_chatbin::check_missions()
{
// TODO: or simply fail them? Some missions might only need to be reported.
Expand Down
5 changes: 5 additions & 0 deletions src/sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ void sounds::process_sound_markers( player *p )
}

const std::string &description = sound.description.empty() ? "a noise" : sound.description;
if( p->is_npc() ) {
npc *guy = dynamic_cast<npc *>( p );
guy->handle_sound( static_cast<int>( sound.category ), description, heard_volume, pos );
continue;
}

// don't print our own noise or things without descriptions
if( !sound.ambient && ( pos != p->pos() ) && !g->m.pl_sees( pos, distance_to_sound ) ) {
Expand Down

0 comments on commit fe3a4bd

Please sign in to comment.