Skip to content

Commit

Permalink
Stop NPCs throwing guns, fix throwing logic (#56746)
Browse files Browse the repository at this point in the history
When #49907 was cleaning up NPC attack logic, it introduced a check into
many of the npc_attack_*::use functions to ensure that the NPC was
wielding the weapon of choice, and if they were not wielding it, ensure
that they did so they could perform that attack next.

However, for the throwing attack, it checked if the wielded item was
not being wielded, causing this section to never be entered, and so the
NPC would throw whatever their wielded item was, or, if they were not
wielding an item, fail to perform an attack.

In addition to this, add a check to ensure we can wield the appropriate
item to npc_attack_throw::can_use.

Also, make sure all NPC actions have a name to ease debugging.
  • Loading branch information
ehughsbaird authored Apr 18, 2022
1 parent 529fb0f commit 4240dc0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/npc_attack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ std::vector<npc_attack_rating> npc_attack_activate_item::all_evaluations( const

void npc_attack_throw::use( npc &source, const tripoint &location ) const
{
if( !source.is_wielding( source.get_wielded_item() ) ) {
if( !source.wield( source.get_wielded_item() ) ) {
if( !source.is_wielding( thrown_item ) ) {
if( !source.wield( thrown_item ) ) {
debugmsg( "ERROR: npc tried to equip a weapon it couldn't wield" );
}
return;
Expand Down Expand Up @@ -608,6 +608,10 @@ bool npc_attack_throw::can_use( const npc &source ) const
return false;
}

if( !source.is_wielding( thrown_item ) && !source.can_wield( thrown_item ).success() ) {
return false;
}

item single_item( thrown_item );
if( single_item.count_by_charges() ) {
single_item.charges = 1;
Expand Down
8 changes: 7 additions & 1 deletion src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4385,9 +4385,15 @@ std::string npc_action_name( npc_action action )
return "Escape explosion";
case npc_player_activity:
return "Performing activity";
default:
case npc_noop:
return "Do nothing";
case npc_do_attack:
return "Attack";
case num_npc_actions:
return "Unnamed action";
}

return "Unnamed action";
}

void print_action( const char *prepend, npc_action action )
Expand Down

0 comments on commit 4240dc0

Please sign in to comment.