Skip to content

Commit

Permalink
Consume menu fix (#40108)
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT authored May 4, 2020
1 parent 141e5ff commit 54afa14
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
78 changes: 35 additions & 43 deletions src/game_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,35 +528,38 @@ class comestible_inventory_preset : public inventory_selector_preset
public:
comestible_inventory_preset( const player &p ) : p( p ) {

append_cell( [ &p, this ]( const item_location & loc ) {
const nutrients nutr = p.compute_effective_nutrients( get_consumable_item( loc ) );
_indent_entries = false;

append_cell( [&p]( const item_location & loc ) {
const nutrients nutr = p.compute_effective_nutrients( *loc );
return good_bad_none( nutr.kcal );
}, _( "CALORIES" ) );

append_cell( [ this ]( const item_location & loc ) {
return good_bad_none( get_edible_comestible( loc ).quench );
append_cell( []( const item_location & loc ) {
return good_bad_none( loc->is_comestible() ? loc->get_comestible()->quench : 0 );
}, _( "QUENCH" ) );

append_cell( [ &p, this ]( const item_location & loc ) {
const item &it = get_consumable_item( loc );
append_cell( [&p]( const item_location & loc ) {
const item &it = *loc;
if( it.has_flag( flag_MUSHY ) ) {
return highlight_good_bad_none( p.fun_for( get_consumable_item( loc ) ).first );
return highlight_good_bad_none( p.fun_for( *loc ).first );
} else {
return good_bad_none( p.fun_for( get_consumable_item( loc ) ).first );
return good_bad_none( p.fun_for( *loc ).first );
}
}, _( "JOY" ) );

append_cell( [ this ]( const item_location & loc ) {
const time_duration spoils = get_edible_comestible( loc ).spoils;
append_cell( []( const item_location & loc ) {
const time_duration spoils = loc->is_comestible() ? loc->get_comestible()->spoils :
calendar::INDEFINITELY_LONG_DURATION;
if( spoils > 0_turns ) {
return to_string_clipped( spoils );
}
//~ Used for permafood shelf life in the Eat menu
return std::string( _( "indefinite" ) );
}, _( "SHELF LIFE" ) );

append_cell( [ this ]( const item_location & loc ) {
const item &it = get_consumable_item( loc );
append_cell( []( const item_location & loc ) {
const item &it = *loc;

int converted_volume_scale = 0;
const int charges = std::max( it.charges, 1 );
Expand All @@ -569,8 +572,7 @@ class comestible_inventory_preset : public inventory_selector_preset

append_cell( [this]( const item_location & loc ) {
if( g->u.can_estimate_rot() ) {
const islot_comestible item = get_edible_comestible( loc );
if( item.spoils > 0_turns ) {
if( loc->is_comestible() && loc->get_comestible()->spoils > 0_turns ) {
return get_freshness( loc );
}
return std::string( "---" );
Expand All @@ -580,9 +582,8 @@ class comestible_inventory_preset : public inventory_selector_preset

append_cell( [ this ]( const item_location & loc ) {
if( g->u.can_estimate_rot() ) {
const islot_comestible item = get_edible_comestible( loc );
if( item.spoils > 0_turns ) {
if( !get_consumable_item( loc ).rotten() ) {
if( loc->is_comestible() && loc->get_comestible()->spoils > 0_turns ) {
if( !loc->rotten() ) {
return get_time_left_rounded( loc );
}
}
Expand All @@ -591,10 +592,10 @@ class comestible_inventory_preset : public inventory_selector_preset
return std::string();
}, _( "SPOILS IN" ) );

append_cell( [ this, &p ]( const item_location & loc ) {
append_cell( [&p]( const item_location & loc ) {
std::string cbm_name;

switch( p.get_cbm_rechargeable_with( get_consumable_item( loc ) ) ) {
switch( p.get_cbm_rechargeable_with( *loc ) ) {
case rechargeable_cbm::none:
break;
case rechargeable_cbm::reactor:
Expand All @@ -604,7 +605,7 @@ class comestible_inventory_preset : public inventory_selector_preset
cbm_name = _( "Furnace" );
break;
case rechargeable_cbm::other:
std::vector<bionic_id> bids = p.get_bionic_fueled_with( get_consumable_item( loc ) );
std::vector<bionic_id> bids = p.get_bionic_fueled_with( *loc );
if( !bids.empty() ) {
bionic_id bid = p.get_most_efficient_bionic( bids );
cbm_name = bid->name.translated();
Expand All @@ -619,29 +620,29 @@ class comestible_inventory_preset : public inventory_selector_preset
return std::string();
}, _( "CBM" ) );

append_cell( [ this, &p ]( const item_location & loc ) {
return good_bad_none( p.get_acquirable_energy( get_consumable_item( loc ) ) );
append_cell( [&p]( const item_location & loc ) {
return good_bad_none( p.get_acquirable_energy( *loc ) );
}, _( "ENERGY (kJ)" ) );
}

bool is_shown( const item_location &loc ) const override {
return p.can_consume( *loc );
return p.can_consume_as_is( *loc );
}

std::string get_denial( const item_location &loc ) const override {
const item &med = *loc;

if( loc->made_of_from_type( LIQUID ) && !g->m.has_flag( flag_LIQUIDCONT, loc.position() ) ) {
if( loc->made_of_from_type( LIQUID ) && loc.where() != item_location::type::container ) {
return _( "Can't drink spilt liquids" );
}

if( med.is_medication() && !p.can_use_heal_item( med ) ) {
return _( "Your biology is not compatible with that item." );
}

const auto &it = get_consumable_item( loc );
const auto res = p.can_eat( it );
const auto cbm = p.get_cbm_rechargeable_with( it );
const item &it = *loc;
const ret_val<edible_rating> res = p.can_eat( it );
const rechargeable_cbm cbm = p.get_cbm_rechargeable_with( it );

if( !res.success() && cbm == rechargeable_cbm::none ) {
return res.str();
Expand All @@ -666,7 +667,7 @@ class comestible_inventory_preset : public inventory_selector_preset

protected:
int get_order( const item_location &loc, const time_duration &time ) const {
if( get_consumable_item( loc ).rotten() ) {
if( loc->rotten() ) {
if( p.has_trait( trait_SAPROPHAGE ) || p.has_trait( trait_SAPROVORE ) ) {
return 1;
} else {
Expand All @@ -679,16 +680,6 @@ class comestible_inventory_preset : public inventory_selector_preset
}
}

// WARNING: this can return consumables which are not necessarily possessing
// the comestible type. please dereference responsibly.
const item &get_consumable_item( const item_location &loc ) const {
return p.get_consumable_from( const_cast<item &>( *loc ) );
}

const islot_comestible &get_edible_comestible( const item_location &loc ) const {
return get_edible_comestible( get_consumable_item( loc ) );
}

const islot_comestible &get_edible_comestible( const item &it ) const {
if( it.is_comestible() && p.can_eat( it ).success() ) {
// Ok since can_eat() returns false if is_craft() is true
Expand All @@ -700,9 +691,10 @@ class comestible_inventory_preset : public inventory_selector_preset

time_duration get_time_left( const item_location &loc ) const {
time_duration time_left = 0_turns;
const time_duration shelf_life = get_edible_comestible( loc ).spoils;
const time_duration shelf_life = loc->is_comestible() ? loc->get_comestible()->spoils :
calendar::INDEFINITELY_LONG_DURATION;
if( shelf_life > 0_turns ) {
const item &it = get_consumable_item( loc );
const item &it = *loc;
const double relative_rot = it.get_relative_rot();
time_left = shelf_life - shelf_life * relative_rot;

Expand All @@ -717,7 +709,7 @@ class comestible_inventory_preset : public inventory_selector_preset
}

std::string get_time_left_rounded( const item_location &loc ) const {
const item &it = get_consumable_item( loc );
const item &it = *loc;
if( it.is_going_bad() ) {
return _( "soon!" );
}
Expand All @@ -727,7 +719,7 @@ class comestible_inventory_preset : public inventory_selector_preset
}

std::string get_freshness( const item_location &loc ) {
const item &it = get_consumable_item( loc );
const item &it = *loc;
const double rot_progress = it.get_relative_rot();
if( it.is_fresh() ) {
return _( "fresh" );
Expand Down Expand Up @@ -787,7 +779,7 @@ class comestible_filtered_inventory_preset : public comestible_inventory_preset

bool is_shown( const item_location &loc ) const override {
return comestible_inventory_preset::is_shown( loc ) &&
predicate( get_consumable_item( loc ) );
predicate( *loc );
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ void inventory_column::draw( const catacurses::window &win, size_t x, size_t y )
}

int contained_offset = 0;
if( entry.is_item() ) {
if( entry.is_item() && indent_entries() ) {
// indent items that are contained
contained_offset = num_parents( entry.locations.front() );
}
Expand Down
15 changes: 15 additions & 0 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class inventory_entry
private:
const item_category *custom_category = nullptr;
bool enabled = true;
protected:
// indents the entry if it is contained in an item
bool _indent = true;

};

Expand Down Expand Up @@ -165,6 +168,10 @@ class inventory_selector_preset
virtual std::function<bool( const inventory_entry & )> get_filter( const std::string &filter )
const;

bool indent_entries() const {
return _indent_entries;
}

protected:
/** Text of the first column (default: item name) */
virtual std::string get_caption( const inventory_entry &entry ) const;
Expand All @@ -182,6 +189,9 @@ class inventory_selector_preset
const std::string &stub = std::string() );
bool check_components = false;

// whether to indent contained entries in the menu
bool _indent_entries = true;

private:
class cell_t
{
Expand Down Expand Up @@ -321,6 +331,11 @@ class inventory_column

void set_filter( const std::string &filter );

// whether or not to indent contained entries
bool indent_entries() const {
return preset.indent_entries();
};

protected:
struct entry_cell_cache_t {
bool assigned = false;
Expand Down

0 comments on commit 54afa14

Please sign in to comment.