Skip to content

Commit 74d8aef

Browse files
committed
manadatory/optional for bodypart
Add a reader for encumbrance_per_weight to enable using mandatory/optional. These functions have the best error reporting and support for extended features.
1 parent 1cee9dc commit 74d8aef

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/bodypart.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <utility>
99
#include <vector>
1010

11-
#include "assign.h"
1211
#include "body_part_set.h"
1312
#include "creature.h"
1413
#include "debug.h"
@@ -285,6 +284,21 @@ const std::vector<body_part_type> &body_part_type::get_all()
285284
return body_part_factory.get_all();
286285
}
287286

287+
class encumbrance_per_weight_reader : public generic_typed_reader<encumbrance_per_weight_reader>
288+
{
289+
public:
290+
std::pair<units::mass, int> get_next( const JsonValue &jv ) const {
291+
std::pair<units::mass, int> ret;
292+
if( !jv.test_object() ) {
293+
jv.throw_error( "Invalid format" );
294+
}
295+
JsonObject entry = jv.get_object();
296+
entry.read( "weight", ret.first, true );
297+
entry.read( "encumbrance", ret.second, true );
298+
return ret;
299+
}
300+
};
301+
288302
void body_part_type::load( const JsonObject &jo, std::string_view )
289303
{
290304
mandatory( jo, was_loaded, "id", id );
@@ -459,18 +473,8 @@ void body_part_type::load( const JsonObject &jo, std::string_view )
459473

460474
mandatory( jo, was_loaded, "sub_parts", sub_parts );
461475

462-
if( jo.has_array( "encumbrance_per_weight" ) ) {
463-
const JsonArray &jarr = jo.get_array( "encumbrance_per_weight" );
464-
for( const JsonObject jval : jarr ) {
465-
units::mass weight = 0_gram;
466-
int encumbrance = 0;
467-
468-
assign( jval, "weight", weight, true );
469-
mandatory( jval, was_loaded, "encumbrance", encumbrance );
470-
471-
encumbrance_per_weight.insert( std::pair<units::mass, int>( weight, encumbrance ) );
472-
}
473-
}
476+
optional( jo, was_loaded, "encumbrance_per_weight", encumbrance_per_weight,
477+
encumbrance_per_weight_reader{} );
474478
}
475479

476480
void bp_onhit_effect::load( const JsonObject &jo )

0 commit comments

Comments
 (0)