forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitamin.cpp
131 lines (114 loc) · 3.65 KB
/
vitamin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "vitamin.h"
#include <cstdlib>
#include <map>
#include "calendar.h"
#include "debug.h"
#include "enum_conversions.h"
#include "json.h"
#include "units.h"
static std::map<vitamin_id, vitamin> vitamins_all;
/** @relates string_id */
template<>
bool string_id<vitamin>::is_valid() const
{
return vitamins_all.count( *this );
}
/** @relates string_id */
template<>
const vitamin &string_id<vitamin>::obj() const
{
const auto found = vitamins_all.find( *this );
if( found == vitamins_all.end() ) {
debugmsg( "Tried to get invalid vitamin: %s", c_str() );
static const vitamin null_vitamin{};
return null_vitamin;
}
return found->second;
}
int vitamin::severity( int qty ) const
{
for( int i = 0; i != static_cast<int>( disease_.size() ); ++i ) {
if( ( qty >= disease_[ i ].first && qty <= disease_[ i ].second ) ||
( qty <= disease_[ i ].first && qty >= disease_[ i ].second ) ) {
return i + 1;
}
}
for( int i = 0; i != static_cast<int>( disease_excess_.size() ); ++i ) {
if( ( qty >= disease_excess_[ i ].first && qty <= disease_excess_[ i ].second ) ||
( qty <= disease_excess_[ i ].first && qty >= disease_excess_[ i ].second ) ) {
return -i - 1;
}
}
return 0;
}
void vitamin::load_vitamin( const JsonObject &jo )
{
vitamin vit;
vit.id_ = vitamin_id( jo.get_string( "id" ) );
jo.read( "name", vit.name_ );
vit.deficiency_ = efftype_id::NULL_ID();
jo.read( "deficiency", vit.deficiency_ );
vit.excess_ = efftype_id::NULL_ID();
jo.read( "excess", vit.excess_ );
vit.min_ = jo.get_int( "min" );
vit.max_ = jo.get_int( "max", 0 );
vit.rate_ = read_from_json_string<time_duration>( jo.get_member( "rate" ), time_duration::units );
if( !jo.has_string( "vit_type" ) ) {
jo.throw_error_at( "vit_type", "vitamin must have a vitamin type" );
}
vit.type_ = jo.get_enum_value<vitamin_type>( "vit_type" );
for( JsonArray e : jo.get_array( "disease" ) ) {
vit.disease_.emplace_back( e.get_int( 0 ), e.get_int( 1 ) );
}
for( JsonArray e : jo.get_array( "disease_excess" ) ) {
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_at( "id", "parsed vitamin overwrites existing definition" );
} else {
vitamins_all[ vit.id_ ] = vit;
}
}
const std::map<vitamin_id, vitamin> &vitamin::all()
{
return vitamins_all;
}
void vitamin::check_consistency()
{
for( const auto &v : vitamins_all ) {
if( !( v.second.deficiency_.is_null() || v.second.deficiency_.is_valid() ) ) {
debugmsg( "vitamin %s has unknown deficiency %s", v.second.id_.c_str(),
v.second.deficiency_.c_str() );
}
if( !( v.second.excess_.is_null() || v.second.excess_.is_valid() ) ) {
debugmsg( "vitamin %s has unknown excess %s", v.second.id_.c_str(), v.second.excess_.c_str() );
}
}
}
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;
}
cata_fatal( "Invalid vitamin_type" );
}
} // namespace io