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

feat: allow holstering to use a weapon_category instead of a flag #5632

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion data/json/items/armor/bandolier.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@
"min_volume": "15 ml",
"max_volume": "250 ml",
"draw_cost": 50,
"flags": [ "SHEATH_KNIFE" ]
"flags": [ "SHEATH_KNIFE" ],
"weapon_category": "KNIVES"
},
"flags": [ "WATER_FRIENDLY", "WAIST", "OVERSIZE" ]
}
Expand Down
2 changes: 1 addition & 1 deletion data/json/items/melee/swords_and_blades.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"symbol": ";",
"color": "light_gray",
"qualities": [ [ "CUT", 1 ], [ "CUT_FINE", 1 ], [ "BUTCHER", 12 ] ],
"flags": [ "STAB", "BELT_CLIP", "SHEATH_KNIFE" ]
"flags": [ "STAB", "BELT_CLIP" ]
},
{
"id": "knife_combat",
Expand Down
11 changes: 9 additions & 2 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@
return !get_map().passable( t );
} );
} else {
const std::string query = _( "Place npc where?" );

Check warning on line 1165 in src/iuse_actor.cpp

View workflow job for this annotation

GitHub Actions / build

unused local variable 'query' of type 'const std::string' (aka 'const basic_string<char>') [bugprone-unused-local-non-trivial-variable]
target_pos = choose_adjacent( _( "Place npc where?" ) );
}
if( !target_pos ) {
Expand Down Expand Up @@ -2670,6 +2670,10 @@
} );

flags = obj.get_string_array( "flags" );
auto wc = obj.get_string_maybe( "weapon_category" );
if( wc != std::nullopt ) {
weapon_category = weapon_category_id( *wc );
}
}

bool holster_actor::can_holster( const item &obj ) const
Expand All @@ -2686,7 +2690,8 @@
return std::any_of( flags.begin(), flags.end(), [&]( const std::string & f ) {
return obj.has_flag( flag_id( f ) );
} ) ||
std::find( skills.begin(), skills.end(), obj.gun_skill() ) != skills.end();
std::find( skills.begin(), skills.end(), obj.gun_skill() ) != skills.end() ||
( weapon_category != std::nullopt && obj.type->weapon_category.contains( *weapon_category ) );
}

detached_ptr<item> holster_actor::store( player &p, item &holster, detached_ptr<item> &&obj ) const
Expand Down Expand Up @@ -2723,7 +2728,9 @@

if( std::none_of( flags.begin(), flags.end(), [&]( const std::string & f ) {
return obj->has_flag( flag_id( f ) );
} ) &&
} ) && !( weapon_category != std::nullopt &&
obj->type->weapon_category.contains( *weapon_category ) )
&&
std::find( skills.begin(), skills.end(), obj->gun_skill() ) == skills.end() ) {
p.add_msg_if_player( m_info, _( "You can't put your %1$s in your %2$s" ),
obj->tname(), holster.tname() );
Expand Down
2 changes: 2 additions & 0 deletions src/iuse_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ class holster_actor : public iuse_actor
std::vector<skill_id> skills;
/** Items with any of these flags set can be holstered */
std::vector<std::string> flags;
/** Items in this weapon_cateogry can be holstered */
std::optional<weapon_category_id> weapon_category;

/** Check if obj could be stored in the holster */
bool can_holster( const item &obj ) const;
Expand Down
11 changes: 11 additions & 0 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,17 @@
return jsin->get_string();
}

std::optional<std::string> JsonObject::get_string_maybe( const std::string &name ) const
{
int pos = verify_position( name, false );
if( !pos ) {
return std::nullopt;
}
mark_visited( name );
jsin->seek( pos );
return jsin->get_string();
}

/* returning containers by name */

JsonArray JsonObject::get_array( const std::string &name ) const
Expand Down Expand Up @@ -1361,7 +1372,7 @@
if( !stream->get( ch ) ) {
error( "unexpected end of input", 0 );
}
if( ( ret.negative = ch == '-' ) ) {

Check warning on line 1375 in src/json.cpp

View workflow job for this annotation

GitHub Actions / build

an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
if( !stream->get( ch ) ) {
error( "unexpected end of input", 0 );
}
Expand Down Expand Up @@ -1397,7 +1408,7 @@
error( "unexpected end of input", 0 );
}
bool neg;
if( ( neg = ch == '-' ) || ch == '+' ) {

Check warning on line 1411 in src/json.cpp

View workflow job for this annotation

GitHub Actions / build

an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
if( !stream->get( ch ) ) {
error( "unexpected end of input", 0 );
}
Expand Down
1 change: 1 addition & 0 deletions src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ class JsonObject
double get_float( const std::string &name, double fallback ) const;
std::string get_string( const std::string &name ) const;
std::string get_string( const std::string &name, const std::string &fallback ) const;
std::optional<std::string> get_string_maybe( const std::string &name ) const;

template<typename E>
E get_enum_value( const std::string &name,
Expand Down
Loading