Skip to content

Commit ee98678

Browse files
authored
Merge pull request #82165 from ehughsbaird/activity-optional
Use optional/mandatory for activity type
2 parents cff3b21 + 9909eae commit ee98678

File tree

8 files changed

+51
-55
lines changed

8 files changed

+51
-55
lines changed

src/activity_type.cpp

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
#include "activity_actor.h"
1010
#include "activity_handlers.h"
11-
#include "assign.h"
1211
#include "debug.h"
1312
#include "enums.h"
14-
#include "flexbuffer_json.h"
1513
#include "generic_factory.h"
1614
#include "sounds.h"
1715
#include "string_formatter.h"
@@ -88,30 +86,28 @@ std::string enum_to_string<distraction_type>( distraction_type data )
8886

8987
void activity_type::load( const JsonObject &jo )
9088
{
91-
activity_type result;
89+
mandatory( jo, was_loaded, "id", id_ );
90+
optional( jo, was_loaded, "rooted", rooted_, false );
91+
optional( jo, was_loaded, "verb", verb_, to_translation( "THIS IS A BUG" ) );
92+
optional( jo, was_loaded, "interruptable", interruptable_, true );
93+
optional( jo, was_loaded, "interruptable_with_kb", interruptable_with_kb_, true );
94+
optional( jo, was_loaded, "can_resume", can_resume_, true );
95+
optional( jo, was_loaded, "multi_activity", multi_activity_, false );
96+
optional( jo, was_loaded, "refuel_fires", refuel_fires, false );
97+
optional( jo, was_loaded, "auto_needs", auto_needs, false );
98+
optional( jo, was_loaded, "completion_eoc", completion_EOC );
99+
optional( jo, was_loaded, "do_turn_eoc", do_turn_EOC );
100+
optional( jo, was_loaded, "ignored_distractions", default_ignored_distractions_ );
101+
optional( jo, was_loaded, "based_on", based_on_ );
102+
mandatory( jo, was_loaded, "activity_level", activity_level, activity_level_reader{} );
103+
}
92104

93-
result.id_ = activity_id( jo.get_string( "id" ) );
94-
assign( jo, "rooted", result.rooted_, true );
95-
assign( jo, "verb", result.verb_, true );
96-
assign( jo, "interruptable", result.interruptable_, true );
97-
assign( jo, "interruptable_with_kb", result.interruptable_with_kb_, true );
98-
assign( jo, "can_resume", result.can_resume_, true );
99-
assign( jo, "multi_activity", result.multi_activity_, false );
100-
assign( jo, "refuel_fires", result.refuel_fires, false );
101-
assign( jo, "auto_needs", result.auto_needs, false );
102-
optional( jo, false, "completion_eoc", result.completion_EOC );
103-
optional( jo, false, "do_turn_eoc", result.do_turn_EOC );
104-
optional( jo, false, "ignored_distractions", result.default_ignored_distractions_ );
105-
optional( jo, false, "based_on", result.based_on_ );
106-
107-
auto act_level_it = activity_levels_map.find( jo.get_string( "activity_level", "undefined" ) );
108-
if( act_level_it == activity_levels_map.end() ) {
109-
debugmsg( "activity_type '%s' has invalid activity_level '%s', defaulting to 'LIGHT_EXERCISE'",
110-
result.id().c_str() );
111-
result.activity_level = activity_levels_map.at( "LIGHT_EXERCISE" );
112-
} else {
113-
result.activity_level = act_level_it->second;
114-
}
105+
void activity_type::load_all( const JsonObject &jo )
106+
{
107+
activity_type result;
108+
result.load( jo );
109+
// FIXME: use generic factory
110+
result.was_loaded = true;
115111

116112
if( activity_type_all.count( result.id_ ) ) {
117113
debugmsg( "Redefinition of %s", result.id_.c_str() );

src/activity_type.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct enum_traits<based_on_type> {
3636
class activity_type
3737
{
3838
private:
39+
bool was_loaded = false;
40+
3941
activity_id id_;
4042
bool rooted_ = false;
4143
translation verb_ = to_translation( "THIS IS A BUG" );
@@ -100,7 +102,8 @@ class activity_type
100102
bool call_finish( player_activity *, Character * ) const;
101103

102104
/** JSON stuff */
103-
static void load( const JsonObject &jo );
105+
void load( const JsonObject &jo );
106+
static void load_all( const JsonObject &jo );
104107
static void check_consistency();
105108
static void reset();
106109
};

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/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ void DynamicDataLoader::initialize()
278278
add( "ammo_effect", &ammo_effects::load );
279279
add( "emit", &emit::load_emit );
280280
add( "help", &help::load );
281-
add( "activity_type", &activity_type::load );
281+
add( "activity_type", &activity_type::load_all );
282282
add( "addiction_type", &add_type::load_add_types );
283283
add( "movement_mode", &move_mode::load_move_mode );
284284
add( "vitamin", &vitamin::load_vitamin );

src/move_mode.cpp

Lines changed: 2 additions & 7 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] );
@@ -230,4 +225,4 @@ void move_mode::set_cycle( const move_mode_id &mode ) const
230225
void move_mode::set_cycle_back( const move_mode_id &mode ) const
231226
{
232227
cycle_back = mode;
233-
}
228+
}

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)