From 1ed751b9b0aea382b393b1c97a6b848b4ca525ac Mon Sep 17 00:00:00 2001 From: anothersimulacrum Date: Sat, 7 Dec 2019 11:48:24 +0000 Subject: [PATCH] Add vitamin flags, other contents to food display 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. --- data/json/vitamin.json | 1 + doc/VITAMIN.md | 12 +++++++++++- src/item.cpp | 21 ++++++++++++++++++++- src/iteminfo_query.h | 1 + src/vitamin.cpp | 4 ++++ src/vitamin.h | 6 ++++++ 6 files changed, 43 insertions(+), 2 deletions(-) diff --git a/data/json/vitamin.json b/data/json/vitamin.json index 9e448c98b0337..c60267600955e 100644 --- a/data/json/vitamin.json +++ b/data/json/vitamin.json @@ -73,6 +73,7 @@ "min": 0, "max": 40, "rate": "4 h", + "flags": [ "NO_DISPLAY" ], "disease_excess": [ [ 10, 19 ], [ 20, 29 ], [ 30, 40 ] ] } ] diff --git a/doc/VITAMIN.md b/doc/VITAMIN.md index adc9224043311..2abcb854a2708 100644 --- a/doc/VITAMIN.md +++ b/doc/VITAMIN.md @@ -1,4 +1,6 @@ -## vitamin +# vitamin + +## definition ```JSON { @@ -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 ] ] }, @@ -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. @@ -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 diff --git a/src/item.cpp b/src/item.cpp index 59d36daa9025b..34679209a3e14 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -1359,16 +1359,35 @@ void item::food_info( const item *food_item, std::vector &info, nutr.vitamins.end(), []( const std::pair &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( 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 &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( 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 allergic reaction." ) ); diff --git a/src/iteminfo_query.h b/src/iteminfo_query.h index 57b528f8bec5f..29c27d3d2c987 100644 --- a/src/iteminfo_query.h +++ b/src/iteminfo_query.h @@ -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, diff --git a/src/vitamin.cpp b/src/vitamin.cpp index 92a8f762b1e40..e6425c2ec1a65 100644 --- a/src/vitamin.cpp +++ b/src/vitamin.cpp @@ -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 { diff --git a/src/vitamin.h b/src/vitamin.h index 34bc54f238a55..9d03d4fc9c155 100644 --- a/src/vitamin.h +++ b/src/vitamin.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "calendar.h" @@ -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_; @@ -102,6 +107,7 @@ class vitamin time_duration rate_; std::vector> disease_; std::vector> disease_excess_; + std::set flags_; }; #endif