Skip to content

Commit 31c1deb

Browse files
committed
optional/mandatory reader for activity_level
To allow using optional/mandatory for types that need to read activity level. Remove some obsolete code too.
1 parent c81ca18 commit 31c1deb

File tree

6 files changed

+25
-36
lines changed

6 files changed

+25
-36
lines changed

src/activity_type.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,7 @@ void activity_type::load( const JsonObject &jo )
9999
optional( jo, was_loaded, "do_turn_eoc", do_turn_EOC );
100100
optional( jo, was_loaded, "ignored_distractions", default_ignored_distractions_ );
101101
optional( jo, was_loaded, "based_on", based_on_ );
102-
103-
auto act_level_it = activity_levels_map.find( jo.get_string( "activity_level", "undefined" ) );
104-
if( act_level_it == activity_levels_map.end() ) {
105-
debugmsg( "activity_type '%s' has invalid activity_level '%s', defaulting to 'LIGHT_EXERCISE'",
106-
result.id().c_str() );
107-
activity_level = activity_levels_map.at( "LIGHT_EXERCISE" );
108-
} else {
109-
activity_level = act_level_it->second;
110-
}
102+
mandatory( jo, was_loaded, "activity_level", activity_level, activity_level_reader{} );
111103
}
112104

113105
void activity_type::load_all( const JsonObject &jo )

src/construction.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "flexbuffer_json.h"
3333
#include "game.h"
3434
#include "game_constants.h"
35+
#include "generic_factory.h"
3536
#include "input.h"
3637
#include "input_context.h"
3738
#include "inventory.h"
@@ -2269,14 +2270,8 @@ void load_construction( const JsonObject &jo )
22692270
con.post_is_furniture = true;
22702271
}
22712272

2272-
std::string activity_level = jo.get_string( "activity_level", "MODERATE_EXERCISE" );
2273-
const auto activity_it = activity_levels_map.find( activity_level );
2274-
if( activity_it != activity_levels_map.end() ) {
2275-
con.activity_level = activity_it->second;
2276-
} else {
2277-
jo.throw_error( string_format( "Invalid activity level %s in construction %s", activity_level,
2278-
con.str_id.str() ) );
2279-
}
2273+
optional( jo, false/*con.was_loaded*/, "activity_level", con.activity_level,
2274+
activity_level_reader{}, MODERATE_EXERCISE );
22802275

22812276
if( jo.has_member( "pre_flags" ) ) {
22822277
con.pre_flags.clear();

src/generic_factory.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "generic_factory.h"
22

33
#include "catacharset.h"
4+
#include "game_constants.h"
45
#include "output.h"
56
#include "wcwidth.h"
67

@@ -75,3 +76,15 @@ float read_proportional_entry( const JsonObject &jo, std::string_view key )
7576
}
7677
return 1.0f;
7778
}
79+
80+
float activity_level_reader::get_next( const JsonValue &jv ) const
81+
{
82+
if( !jv.test_string() ) {
83+
jv.throw_error( "Invalid activity level" );
84+
}
85+
auto it = activity_levels_map.find( jv.get_string() );
86+
if( it == activity_levels_map.end() ) {
87+
jv.throw_error( "Invalid activity level" );
88+
}
89+
return it->second;
90+
}

src/generic_factory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,12 @@ class text_style_check_reader : public generic_typed_reader<text_style_check_rea
18261826
allow_object object_allowed;
18271827
};
18281828

1829+
class activity_level_reader : public generic_typed_reader<activity_level_reader>
1830+
{
1831+
public:
1832+
float get_next( const JsonValue &jv ) const;
1833+
};
1834+
18291835
struct dbl_or_var;
18301836

18311837
class dbl_or_var_reader : public generic_typed_reader<dbl_or_var>

src/move_mode.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "assign.h"
99
#include "debug.h"
1010
#include "flexbuffer_json.h"
11-
#include "game_constants.h"
1211
#include "generic_factory.h"
1312
#include "translations.h"
1413

@@ -57,11 +56,7 @@ void move_mode::load( const JsonObject &jo, std::string_view/*src*/ )
5756
assign( jo, "panel_color", _panel_color );
5857
assign( jo, "symbol_color", _symbol_color );
5958

60-
std::string exert = jo.get_string( "exertion_level" );
61-
if( !activity_levels_map.count( exert ) ) {
62-
jo.throw_error_at( id.str(), "Invalid activity level for move mode " + id.str() );
63-
}
64-
_exertion_level = activity_levels_map.at( exert );
59+
mandatory( jo, was_loaded, "exertion_level", _exertion_level, activity_level_reader{} );
6560

6661
mandatory( jo, was_loaded, "change_good_none", change_messages_success[steed_type::NONE] );
6762
mandatory( jo, was_loaded, "change_good_animal", change_messages_success[steed_type::ANIMAL] );

src/recipe.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
283283
}
284284

285285
// Mandatory: This recipe's exertion level
286-
mandatory( jo, was_loaded, "activity_level", exertion_str );
287-
// Remove after 0.H
288-
if( exertion_str == "fake" ) {
289-
debugmsg( "Depreciated activity level \"fake\" found in recipe %s from source %s. Setting activity level to MODERATE_EXERCISE.",
290-
id.c_str(), src );
291-
exertion_str = "MODERATE_EXERCISE";
292-
}
293-
const auto it = activity_levels_map.find( exertion_str );
294-
if( it == activity_levels_map.end() ) {
295-
jo.throw_error_at(
296-
"activity_level", string_format( "Invalid activity level %s", exertion_str ) );
297-
}
298-
exertion = it->second;
286+
mandatory( jo, was_loaded, "activity_level", exertion, activity_level_reader{} );
299287

300288
// Never let the player have a debug or NPC recipe
301289
if( jo.has_bool( "never_learn" ) ) {

0 commit comments

Comments
 (0)