Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't silently fail when attempting to modify vits #1476

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,15 @@ time_duration Character::vitamin_rate( const vitamin_id &vit ) const

int Character::vitamin_mod( const vitamin_id &vit, int qty, bool capped )
{
auto it = vitamin_levels.find( vit );
if( it == vitamin_levels.end() ) {
if( !vit.is_valid() ) {
debugmsg( "Vitamin with id %s does not exist, and cannot be modified", vit.str() );
return 0;
}
const auto &v = it->first.obj();
// What's going on here? Emplace returns either an iterator to the inserted
// item or, if it already exists, an iterator to the (unchanged) extant item
// (Okay, technically it returns a pair<iterator, bool>, the iterator is what we want)
auto it = vitamin_levels.emplace( vit, 0 ).first;
const vitamin &v = *it->first;

if( qty > 0 ) {
// Accumulations can never occur from food sources
Expand Down
7 changes: 4 additions & 3 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,10 @@ void Character::load( const JsonObject &data )
JsonObject vits = data.get_object( "vitamin_levels" );
vits.allow_omitted_members();
for( const std::pair<const vitamin_id, vitamin> &v : vitamin::all() ) {
int lvl = vits.get_int( v.first.str(), 0 );
lvl = std::max( std::min( lvl, v.first.obj().max() ), v.first.obj().min() );
vitamin_levels[v.first] = lvl;
if( vits.has_member( v.first.str() ) ) {
int lvl = vits.get_int( v.first.str() );
vitamin_levels[v.first] = clamp( lvl, v.first->min(), v.first->max() );
}
}
data.read( "consumption_history", consumption_history );
data.read( "activity", activity );
Expand Down