From 983239174aba38bbebfbcb00150050afd128ceeb Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Sun, 2 Aug 2020 15:32:33 +0300 Subject: [PATCH 1/2] Fix place npc iuse --- doc/JSON_INFO.md | 1 + src/iuse_actor.cpp | 24 ++++++++++-------------- src/iuse_actor.h | 1 + 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/JSON_INFO.md b/doc/JSON_INFO.md index 94e6f5ba7df13..4b66f5c69051b 100644 --- a/doc/JSON_INFO.md +++ b/doc/JSON_INFO.md @@ -2771,6 +2771,7 @@ The contents of use_action fields can either be a string indicating a built-in f "summon_msg": "You summon a food hero!", // (optional) message when summoning the npc. "place_randomly": true, // if true: places npc randomly around the player, if false: let the player decide where to put it (default: false) "moves": 50 // how many move points the action takes. + "radius": 1 // maximum radius for random npc placement. }, "use_action" : { "type" : "delayed_transform", // Like transform, but it will only transform when the item has a certain age diff --git a/src/iuse_actor.cpp b/src/iuse_actor.cpp index df574b8b8e96f..9fb83194bd683 100644 --- a/src/iuse_actor.cpp +++ b/src/iuse_actor.cpp @@ -971,20 +971,16 @@ void place_npc_iuse::load( const JsonObject &obj ) int place_npc_iuse::use( player &p, item &, bool, const tripoint & ) const { map &here = get_map(); - cata::optional target_pos; - if( place_randomly ) { - const tripoint_range target_range = points_in_radius( p.pos(), 1 ); - target_pos = random_point( target_range, [&here]( const tripoint & t ) { - return !here.passable( t ); - } ); - } else { - const std::string query = _( "Place npc where?" ); - target_pos = choose_adjacent( _( "Place npc where?" ) ); - } - if( !target_pos ) { - return 0; - } - if( !here.passable( target_pos.value() ) ) { + const tripoint_range target_range = place_randomly ? + points_in_radius( p.pos(), radius ) : + points_in_radius( choose_adjacent( _( "Place npc where?" ) ).value_or( p.pos() ), 0 ); + + const cata::optional target_pos = + random_point( target_range, [&here]( const tripoint & t ) { + return here.passable( t ) && here.has_floor_or_support( t ) && !g->critter_at( t ); + } ); + + if( !target_pos.has_value() ) { p.add_msg_if_player( m_info, _( "There is no square to spawn npc in!" ) ); return 0; } diff --git a/src/iuse_actor.h b/src/iuse_actor.h index fa8a0310acfa0..a15cddd3d75dd 100644 --- a/src/iuse_actor.h +++ b/src/iuse_actor.h @@ -377,6 +377,7 @@ class place_npc_iuse : public iuse_actor public: string_id npc_class_id; bool place_randomly = false; + int radius = 1; int moves = 100; std::string summon_msg; From a5a7b47e8377d0633f3421915d4d4a0a23bb1166 Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Sun, 2 Aug 2020 15:41:39 +0300 Subject: [PATCH 2/2] Apply suggestions from code review --- doc/JSON_INFO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/JSON_INFO.md b/doc/JSON_INFO.md index 4b66f5c69051b..bf9230f6797d6 100644 --- a/doc/JSON_INFO.md +++ b/doc/JSON_INFO.md @@ -2770,7 +2770,7 @@ The contents of use_action fields can either be a string indicating a built-in f "npc_class_id": "true_foodperson", // npc class id, see npcs/classes.json "summon_msg": "You summon a food hero!", // (optional) message when summoning the npc. "place_randomly": true, // if true: places npc randomly around the player, if false: let the player decide where to put it (default: false) - "moves": 50 // how many move points the action takes. + "moves": 50, // how many move points the action takes. "radius": 1 // maximum radius for random npc placement. }, "use_action" : {