Skip to content

Commit

Permalink
Add vitamin flags, other contents to food display
Browse files Browse the repository at this point in the history
Add flags to vitamins, so that vitamins can have different effects
without requiring entirely new members.

Add FOOD_VIT_EFFECTS to item info, which displays vitamins that are not
of subtype vitamin, because these things are not used as vitamins, but
instead have other effects within food.

Some 'vitamins' should not be display, so add a `NO_DISPLAY` flag that
can be used to exclude them from display.
  • Loading branch information
anothersimulacrum committed Dec 20, 2019
1 parent f1e683c commit 1ed751b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/json/vitamin.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"min": 0,
"max": 40,
"rate": "4 h",
"flags": [ "NO_DISPLAY" ],
"disease_excess": [ [ 10, 19 ], [ 20, 29 ], [ 30, 40 ] ]
}
]
12 changes: 11 additions & 1 deletion doc/VITAMIN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## vitamin
# vitamin

## definition

```JSON
{
Expand All @@ -11,6 +13,7 @@
"min": -12000,
"max": 3600,
"rate": "15 m",
"flags": [ "FOO" ],
"disease": [ [ -4800, -5600 ], [ -5601, -6400 ], [ -6401, -12000 ] ],
"disease_excess": [ [ 10, 19 ], [ 20, 29 ], [ 30, 40 ] ]
},
Expand Down Expand Up @@ -55,6 +58,9 @@ The highest amount of this vitamin that the avatar can have.
### `rate`
How long it takes to lose one unit of this vitamin.

### `flags`
An array of string flags, see the flags section below for valid ones

### `disease`
What the thresholds of deficiency of this vitamin are.
Each pair in the list determines the start and end points of that tier of deficiency.
Expand All @@ -64,3 +70,7 @@ Each tier of deficiency corresponds to the intensity level of the effect defined
What the thresholds of excess of this vitamin are.
Each pair in the list determines the start and end points of that tier of excess.
Each tier of excess corresponds to the intensity level of the effect defined in `excess`.

## flags

- ```NO_DISPLAY``` - This vitamin will not be shown when examining a food
21 changes: 20 additions & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,16 +1359,35 @@ void item::food_info( const item *food_item, std::vector<iteminfo> &info,
nutr.vitamins.end(),
[]( const std::pair<vitamin_id, int> &v ) {
// only display vitamins that we actually require
return ( g->u.vitamin_rate( v.first ) > 0_turns && v.second != 0 ) ?
return ( g->u.vitamin_rate( v.first ) > 0_turns && v.second != 0 &&
v.first->type() == vitamin_type::VITAMIN && !v.first->has_flag( "NO_DISPLAY" ) ) ?
string_format( "%s (%i%%)", v.first.obj().name(),
static_cast<int>( v.second * g->u.vitamin_rate( v.first ) /
1_days * 100 ) ) :
std::string();
} );

const std::string effect_vits = enumerate_as_string(
nutr.vitamins.begin(),
nutr.vitamins.end(),
[]( const std::pair<vitamin_id, int> &v ) {
// only display vitamins that we actually require
return ( g->u.vitamin_rate( v.first ) > 0_turns && v.second != 0 &&
v.first->type() != vitamin_type::VITAMIN && !v.first->has_flag( "NO_DISPLAY" ) ) ?
string_format( "%s (%i%%)", v.first.obj().name(),
static_cast<int>( v.second * g->u.vitamin_rate( v.first ) /
1_days * 100 ) ) :
std::string();
} );

if( !required_vits.empty() && parts->test( iteminfo_parts::FOOD_VITAMINS ) ) {
info.emplace_back( "FOOD", _( "Vitamins (RDA): " ), required_vits );
}

if( !effect_vits.empty() && parts->test( iteminfo_parts::FOOD_VIT_EFFECTS ) ) {
info.emplace_back( "FOOD", _( "Other contents: " ), effect_vits );
}

if( g->u.allergy_type( *food_item ) != morale_type( "morale_null" ) ) {
info.emplace_back( "DESCRIPTION",
_( "* This food will cause an <bad>allergic reaction</bad>." ) );
Expand Down
1 change: 1 addition & 0 deletions src/iteminfo_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum class iteminfo_parts : size_t {
FOOD_PORTIONS,
FOOD_SMELL,
FOOD_VITAMINS,
FOOD_VIT_EFFECTS,
FOOD_CANNIBALISM,
FOOD_TAINT,
FOOD_POISON,
Expand Down
4 changes: 4 additions & 0 deletions src/vitamin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void vitamin::load_vitamin( const JsonObject &jo )
vit.disease_excess_.emplace_back( e.get_int( 0 ), e.get_int( 1 ) );
}

for( std::string e : jo.get_array( "flags" ) ) {
vit.flags_.insert( e );
}

if( vitamins_all.find( vit.id_ ) != vitamins_all.end() ) {
jo.throw_error( "parsed vitamin overwrites existing definition", "id" );
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/vitamin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <map>
#include <utility>
#include <vector>
#include <set>
#include <string>

#include "calendar.h"
Expand Down Expand Up @@ -48,6 +49,10 @@ class vitamin
return name_.translated();
}

bool has_flag( const std::string &flag ) const {
return flags_.count( flag ) > 0;
}

/** Disease effect with increasing intensity proportional to vitamin deficiency */
const efftype_id &deficiency() const {
return deficiency_;
Expand Down Expand Up @@ -102,6 +107,7 @@ class vitamin
time_duration rate_;
std::vector<std::pair<int, int>> disease_;
std::vector<std::pair<int, int>> disease_excess_;
std::set<std::string> flags_;
};

#endif

0 comments on commit 1ed751b

Please sign in to comment.