Skip to content

Commit

Permalink
Fix Toxins not working with simplified nutrition
Browse files Browse the repository at this point in the history
This commit adds vitamin `types`, different variations of vitamins that
can later be expanded to have different effects.

These are dynamically loaded from JSON into 4 types, vitamin, toxin,
drug, and counter. Only vitamin, toxin, and counter are used right now.

Only the vitamin type has an effect, used with simplified nutrition to
make it so that that vitamin has no effect.
  • Loading branch information
anothersimulacrum committed Dec 6, 2019
1 parent f04c6d5 commit 3f5ade8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions data/json/vitamin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"id": "calcium",
"type": "vitamin",
"vit_type": "vitamin",
"name": "Calcium",
"deficiency": "hypocalcemia",
"min": -12000,
Expand All @@ -11,6 +12,7 @@
{
"id": "iron",
"type": "vitamin",
"vit_type": "vitamin",
"name": "Iron",
"excess": "hypervitaminosis",
"deficiency": "anemia",
Expand All @@ -22,6 +24,7 @@
{
"id": "vitA",
"type": "vitamin",
"vit_type": "vitamin",
"name": "Vitamin A",
"excess": "hypervitaminosis",
"deficiency": "hypovitA",
Expand All @@ -33,6 +36,7 @@
{
"id": "vitB",
"type": "vitamin",
"vit_type": "vitamin",
"name": "Vitamin B12",
"deficiency": "hypovitB",
"min": -5600,
Expand All @@ -42,6 +46,7 @@
{
"id": "vitC",
"type": "vitamin",
"vit_type": "vitamin",
"name": "Vitamin C",
"deficiency": "scurvy",
"min": -5600,
Expand All @@ -51,6 +56,7 @@
{
"id": "mutant_toxin",
"type": "vitamin",
"vit_type": "toxin",
"name": "Toxins",
"excess": "toxin_buildup",
"min": 0,
Expand All @@ -61,6 +67,7 @@
{
"id": "bad_food",
"type": "vitamin",
"vit_type": "counter",
"name": "Disgusting Diet",
"excess": "bad_food_ennui",
"min": 0,
Expand Down
2 changes: 1 addition & 1 deletion src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ void player::vitamins_mod( const std::map<vitamin_id, int> &vitamins, bool cappe

int Character::vitamin_get( const vitamin_id &vit ) const
{
if( get_option<bool>( "NO_VITAMINS" ) ) {
if( get_option<bool>( "NO_VITAMINS" ) && vit->type() == vitamin_type::VITAMIN ) {
return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ void Item_factory::finalize_pre( itype &obj )

if( obj.comestible ) {
if( get_option<bool>( "NO_VITAMINS" ) ) {
obj.comestible->vitamins.clear();
for( auto &vit : obj.comestible->vitamins ) {
if( vit.first->type() == vitamin_type::VITAMIN ) {
vit.second = 0;
}
}
} else if( obj.comestible->vitamins.empty() && obj.comestible->healthy >= 0 ) {
// Default vitamins of healthy comestibles to their edible base materials if none explicitly specified.
auto healthy = std::max( obj.comestible->healthy, 1 ) * 10;
Expand Down
27 changes: 27 additions & 0 deletions src/vitamin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ void vitamin::load_vitamin( JsonObject &jo )
vit.max_ = jo.get_int( "max", 0 );
vit.rate_ = read_from_json_string<time_duration>( *jo.get_raw( "rate" ), time_duration::units );

if( !jo.has_string( "vit_type" ) ) {
jo.throw_error( "vitamin must have a vitamin type", "vit_type" );
}
vit.type_ = jo.get_enum_value<vitamin_type>( "vit_type" );

if( vit.rate_ < 0_turns ) {
jo.throw_error( "vitamin consumption rate cannot be negative", "rate" );
}
Expand Down Expand Up @@ -102,3 +107,25 @@ void vitamin::reset()
{
vitamins_all.clear();
}

namespace io
{
template<>
std::string enum_to_string<vitamin_type>( vitamin_type data )
{
switch( data ) {
case vitamin_type::VITAMIN:
return "vitamin";
case vitamin_type::TOXIN:
return "toxin";
case vitamin_type::DRUG:
return "drug";
case vitamin_type::COUNTER:
return "counter";
case vitamin_type::num_vitamin_types:
break;
}
debugmsg( "Invalid vitamin_type" );
abort();
}
} // namespace io
18 changes: 18 additions & 0 deletions src/vitamin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@

class JsonObject;

enum vitamin_type {
VITAMIN,
TOXIN,
DRUG,
COUNTER,
num_vitamin_types
};

template<>
struct enum_traits<vitamin_type> {
static constexpr auto last = vitamin_type::num_vitamin_types;
};

class vitamin
{
public:
Expand All @@ -23,6 +36,10 @@ class vitamin
return id_;
}

const vitamin_type &type() const {
return type_;
}

bool is_null() const {
return id_ == vitamin_id( "null" );
}
Expand Down Expand Up @@ -76,6 +93,7 @@ class vitamin

private:
vitamin_id id_;
vitamin_type type_;
translation name_;
efftype_id deficiency_;
efftype_id excess_;
Expand Down

0 comments on commit 3f5ade8

Please sign in to comment.