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

Adds info about Holsters to the inventory UI #55755

Merged
merged 3 commits into from
Mar 4, 2022
Merged
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
30 changes: 30 additions & 0 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7906,6 +7906,36 @@ int Character::empty_holsters() const
return e_holsters;
}

int Character::used_holsters() const
{
int e_holsters = 0;
e_holsters += weapon.get_used_holsters();
for( const item &w : worn ) {
e_holsters += w.get_used_holsters();
}
return e_holsters;
}

int Character::total_holsters() const
{
int e_holsters = 0;
e_holsters += weapon.get_total_holsters();
for( const item &w : worn ) {
e_holsters += w.get_total_holsters();
}
return e_holsters;
}

units::volume Character::free_holster_volume() const
{
units::volume holster_volume = 0_ml;
holster_volume += weapon.get_total_holster_volume() - weapon.get_used_holster_volume();
for( const item &w : worn ) {
holster_volume += w.get_total_holster_volume() - w.get_used_holster_volume();
}
return holster_volume;
}

units::volume Character::small_pocket_volume( const units::volume &threshold ) const
{
units::volume small_spaces = 0_ml;
Expand Down
10 changes: 10 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,17 @@ class Character : public Creature, public visitable
* Returns the total volume of all worn holsters.
*/
units::volume holster_volume() const;

/**
* Used and total holsters
*/
int used_holsters() const;
int total_holsters() const;
units::volume free_holster_volume() const;

// this is just used for pack rat maybe should be moved to the above more robust functions
int empty_holsters() const;

/**
* Returns the total volume of all pockets less than or equal to the volume passed in
* @param volume threshold for pockets to be considered
Expand Down
26 changes: 21 additions & 5 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,8 @@ inventory_selector::stat display_stat( const std::string &caption, int cur_value
inventory_selector::stats inventory_selector::get_weight_and_volume_stats(
units::mass weight_carried, units::mass weight_capacity,
const units::volume &volume_carried, const units::volume &volume_capacity,
const units::length &longest_length, const units::volume &largest_free_volume )
const units::length &longest_length, const units::volume &largest_free_volume,
const units::volume &holster_volume, const int used_holsters, const int total_holsters )
{
// This is a bit of a hack, we're prepending two entries to the weight and length stat blocks.
std::string length_weight_caption = string_format( _( "Longest Length (%s): %s Weight (%s):" ),
Expand All @@ -1947,6 +1948,10 @@ inventory_selector::stats inventory_selector::get_weight_and_volume_stats(
volume_units_abbr(),
colorize( format_volume( largest_free_volume ), c_light_gray ),
volume_units_abbr() );

std::string holster_caption = string_format( _( "Free Holster Volume (%s): %s Used Holsters:" ),
volume_units_abbr(),
colorize( format_volume( holster_volume ), c_light_gray ) );
return {
{
display_stat( length_weight_caption,
Expand All @@ -1960,6 +1965,12 @@ inventory_selector::stats inventory_selector::get_weight_and_volume_stats(
units::to_milliliter( volume_capacity ), []( int v )
{
return format_volume( units::from_milliliter( v ) );
} ),
display_stat( holster_caption,
used_holsters,
total_holsters, []( int v )
{
return string_format( "%d", v );
} )
}
};
Expand All @@ -1969,13 +1980,14 @@ inventory_selector::stats inventory_selector::get_raw_stats() const
{
return get_weight_and_volume_stats( u.weight_carried(), u.weight_capacity(),
u.volume_carried(), u.volume_capacity(),
u.max_single_item_length(), u.max_single_item_volume() );
u.max_single_item_length(), u.max_single_item_volume(),
u.free_holster_volume(), u.used_holsters(), u.total_holsters() );
}

std::vector<std::string> inventory_selector::get_stats() const
{
// Stats consist of arrays of cells.
const size_t num_stats = 2;
const size_t num_stats = 3;
const std::array<stat, num_stats> stats = get_raw_stats();
// Streams for every stat.
std::array<std::string, num_stats> lines;
Expand Down Expand Up @@ -3037,7 +3049,8 @@ inventory_selector::stats inventory_drop_selector::get_raw_stats() const
u.weight_capacity(),
u.volume_carried_with_tweaks( to_use ),
u.volume_capacity_with_tweaks( to_use ),
u.max_single_item_length(), u.max_single_item_volume() );
u.max_single_item_length(), u.max_single_item_volume(),
u.free_holster_volume(), u.used_holsters(), u.total_holsters() );
}

pickup_selector::pickup_selector( Character &p, const inventory_selector_preset &preset,
Expand Down Expand Up @@ -3196,7 +3209,10 @@ inventory_selector::stats pickup_selector::get_raw_stats() const
u.volume_carried() + volume,
u.volume_capacity(),
u.max_single_item_length(),
u.max_single_item_volume() );
u.max_single_item_volume(),
u.free_holster_volume(),
u.used_holsters(),
u.total_holsters() );
}

bool inventory_examiner::check_parent_item()
Expand Down
5 changes: 3 additions & 2 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class inventory_selector

// An array of cells for the stat lines. Example: ["Weight (kg)", "10", "/", "20"].
using stat = std::array<std::string, 4>;
using stats = std::array<stat, 2>;
using stats = std::array<stat, 3>;

protected:
Character &u;
Expand Down Expand Up @@ -668,7 +668,8 @@ class inventory_selector
static stats get_weight_and_volume_stats(
units::mass weight_carried, units::mass weight_capacity,
const units::volume &volume_carried, const units::volume &volume_capacity,
const units::length &longest_length, const units::volume &largest_free_volume );
const units::length &longest_length, const units::volume &largest_free_volume,
const units::volume &holster_volume, const int used_holsters, const int total_holsters );

/** Get stats to display in top right.
*
Expand Down
17 changes: 17 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10933,6 +10933,23 @@ units::mass item::get_total_contained_weight( const bool unrestricted_pockets_on
return contents.total_contained_weight( unrestricted_pockets_only );
}

int item::get_used_holsters() const
{
return contents.get_used_holsters();
}
int item::get_total_holsters() const
{
return contents.get_total_holsters();
}
units::volume item::get_total_holster_volume() const
{
return contents.get_total_holster_volume();
}
units::volume item::get_used_holster_volume() const
{
return contents.get_used_holster_volume();
}

int item::get_remaining_capacity_for_liquid( const item &liquid, bool allow_bucket,
std::string *err ) const
{
Expand Down
5 changes: 5 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,11 @@ class item : public visitable
units::volume get_total_contained_volume( bool unrestricted_pockets_only = false ) const;
units::mass get_total_contained_weight( bool unrestricted_pockets_only = false ) const;

int get_used_holsters() const;
int get_total_holsters() const;
units::volume get_total_holster_volume() const;
units::volume get_used_holster_volume() const;

// recursive function that checks pockets for remaining free space
units::volume check_for_free_space() const;
units::volume get_selected_stack_volume( const std::map<const item *, int> &without ) const;
Expand Down
47 changes: 45 additions & 2 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,50 @@ std::vector< const item_pocket *> item_contents::get_all_reloadable_pockets() co
return pockets;
}

int item_contents::get_used_holsters() const
{
int holsters = 0;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) && pocket.holster_full() ) {
holsters++;
}
}
return holsters;
}

int item_contents::get_total_holsters() const
{
int holsters = 0;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) && pocket.is_holster() ) {
holsters++;
}
}
return holsters;
}

units::volume item_contents::get_used_holster_volume() const
{
units::volume holster_volume = 0_ml;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) && pocket.holster_full() ) {
holster_volume += pocket.volume_capacity();
}
}
return holster_volume;
}

units::volume item_contents::get_total_holster_volume() const
{
units::volume holster_volume = 0_ml;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) && pocket.is_holster() ) {
holster_volume += pocket.volume_capacity();
}
}
return holster_volume;
}

units::volume item_contents::total_container_capacity( const bool unrestricted_pockets_only ) const
{
units::volume total_vol = 0_ml;
Expand All @@ -1610,12 +1654,11 @@ units::volume item_contents::total_container_capacity( const bool unrestricted_p
restriction_condition = restriction_condition && !pocket.is_restricted();
}
if( restriction_condition ) {
const pocket_data *p_data = pocket.get_pocket_data();
// if the pocket has default volume or is a holster that has an
// item in it or is a pocket that has normal pickup disabled
// instead of returning the volume return the volume of things contained
if( pocket.volume_capacity() >= pocket_data::max_volume_for_container ||
pocket.settings.is_disabled() || ( p_data->holster && !pocket.all_items_top().empty() ) ) {
pocket.settings.is_disabled() || ( pocket.holster_full() ) ) {
total_vol += pocket.contains_volume();
} else {
total_vol += pocket.volume_capacity();
Expand Down
6 changes: 6 additions & 0 deletions src/item_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ class item_contents
units::volume get_nested_content_volume_recursive( const std::map<const item *, int> &without )
const;

// get all holsters
int get_used_holsters() const;
int get_total_holsters() const;
units::volume get_total_holster_volume() const;
units::volume get_used_holster_volume() const;

// gets all CONTAINER pockets contained in this item
ret_val<std::vector<const item_pocket *>> get_all_contained_pockets() const;
ret_val<std::vector<item_pocket *>> get_all_contained_pockets();
Expand Down
8 changes: 7 additions & 1 deletion src/item_pocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,8 @@ void item_pocket::general_info( std::vector<iteminfo> &info, int pocket_number,
}

if( data->holster ) {
info.emplace_back( "DESCRIPTION", _( "This pocket only holds <info>one item at a time</info>." ) );
info.emplace_back( "DESCRIPTION",
_( "This is a <info>holster</info>, it only holds <info>one item at a time</info>." ) );
}

if( data->extra_encumbrance > 0 ) {
Expand Down Expand Up @@ -1843,6 +1844,11 @@ bool item_pocket::is_ablative() const
return get_pocket_data()->ablative;
}

bool item_pocket::is_holster() const
{
return get_pocket_data()->holster;
}

const translation &item_pocket::get_description() const
{
return get_pocket_data()->description;
Expand Down
1 change: 1 addition & 0 deletions src/item_pocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class item_pocket
bool is_valid() const;
bool is_type( pocket_type ptype ) const;
bool is_ablative() const;
bool is_holster() const;
// checks if the pocket is a holster and if it has something in it
bool holster_full() const;
bool empty() const;
Expand Down
2 changes: 1 addition & 1 deletion tests/iteminfo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ TEST_CASE( "pocket info for a multi-pocket item", "[iteminfo][pocket][multiple]"
"Item length: <color_c_yellow>0</color> cm to <color_c_yellow>70</color> cm\n"
"Minimum item volume: <color_c_yellow>0.050</color> L\n"
"Base moves to remove item: <color_c_yellow>50</color>\n"
"This pocket only holds <color_c_cyan>one item at a time</color>.\n"
"This is a <color_c_cyan>holster</color>, it only holds <color_c_cyan>one item at a time</color>.\n"
"<color_c_white>Restrictions</color>:\n"
"* Item must clip onto a belt loop\n"
"* <color_c_white>or</color> Item must fit in a sheath\n" );
Expand Down