From 4180cc3e486e2ee4c78b05bbfa2a880d98ef26de Mon Sep 17 00:00:00 2001 From: Anton Simakov <67688115+GuardianDll@users.noreply.github.com> Date: Tue, 20 Aug 2024 21:07:58 +0200 Subject: [PATCH] backport #75668 (#75828) --- data/json/items/tool/explosives.json | 3 ++- src/iuse.cpp | 20 ++++++++++++++------ src/npc_attack.cpp | 4 +++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/data/json/items/tool/explosives.json b/data/json/items/tool/explosives.json index d3dd5340ade73..ddb43f068ff22 100644 --- a/data/json/items/tool/explosives.json +++ b/data/json/items/tool/explosives.json @@ -55,7 +55,8 @@ "color": "light_gray", "use_action": { "type": "message", "message": "You've already set the %s's timer, you might want to get away from it." }, "countdown_action": { "type": "explosion", "explosion": { "power": 2000 } }, - "flags": [ "TRADER_AVOID" ] + "countdown_interval": "6 seconds", + "flags": [ "TRADER_AVOID", "NPC_THROW_NOW" ] }, { "id": "dynamite", diff --git a/src/iuse.cpp b/src/iuse.cpp index f6e3a0a02905f..1613eeff5c50f 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -3643,16 +3643,24 @@ std::optional iuse::granade_act( Character *, item *it, const tripoint &pos std::optional iuse::c4( Character *p, item *it, const tripoint & ) { - int time; - bool got_value = query_int( time, _( "Set the timer to how many seconds (0 to cancel)?" ) ); - if( !got_value || time <= 0 ) { - p->add_msg_if_player( _( "Never mind." ) ); - return std::nullopt; + int time = 0; + bool got_value = false; + if( p->is_avatar() ) { + got_value = query_int( time, _( "Set the timer to how many seconds (0 to cancel)?" ) ); + if( !got_value || time <= 0 ) { + p->add_msg_if_player( _( "Never mind." ) ); + return std::nullopt; + } } p->add_msg_if_player( n_gettext( "You set the timer to %d second.", "You set the timer to %d seconds.", time ), time ); it->convert( itype_c4armed ); - it->countdown_point = calendar::turn + time_duration::from_seconds( time ); + if( got_value ) { + it->countdown_point = calendar::turn + time_duration::from_seconds( time ); + } else { + // Uses value from the converted type (e.g. currently hardcoded c4armed) + it->countdown_point = calendar::turn + it->type->countdown_interval; + } it->active = true; return 1; } diff --git a/src/npc_attack.cpp b/src/npc_attack.cpp index 3563a6c485a47..d92265018b577 100644 --- a/src/npc_attack.cpp +++ b/src/npc_attack.cpp @@ -579,8 +579,10 @@ void npc_attack_activate_item::use( npc &source, const tripoint &/*location*/ ) if( !source.wield( activatable_item ) ) { debugmsg( "%s can't wield %s it tried to activate", source.disp_name(), activatable_item.display_name() ); + return; } - source.activate_item( activatable_item ); + // npc::wield may invalidate activatable_item's reference + source.activate_item( *source.get_wielded_item() ); } bool npc_attack_activate_item::can_use( const npc &source ) const