Skip to content

Commit 079429d

Browse files
committed
recipe optional/mandatory
Replace assign with optional, it has better support for JSON features. Remove unecessary constructor too. There's a lot more work to be done on recipe loading, but this is the first (easy) step.
1 parent 4d6886d commit 079429d

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/recipe.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <sstream>
99
#include <unordered_map>
1010

11-
#include "assign.h"
1211
#include "cached_options.h"
1312
#include "calendar.h"
1413
#include "cartesian_product.h"
@@ -55,8 +54,6 @@ static const itype_id itype_hotplate( "hotplate" );
5554
static const std::string flag_FULL_MAGAZINE( "FULL_MAGAZINE" );
5655

5756

58-
recipe::recipe() : skill_used( skill_id::NULL_ID() ) {}
59-
6057
int recipe::get_difficulty( const Character &crafter ) const
6158
{
6259
if( is_practice() && skill_used ) {
@@ -155,10 +152,8 @@ bool recipe::has_flag( const std::string &flag_name ) const
155152
return flags.count( flag_name );
156153
}
157154

158-
void recipe::load( const JsonObject &jo, const std::string &src )
155+
void recipe::load( const JsonObject &jo, const std::string_view src )
159156
{
160-
bool strict = src == "dda";
161-
162157
abstract = jo.has_string( "abstract" );
163158

164159
const std::string type = jo.get_string( "type" );
@@ -218,7 +213,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
218213
}
219214

220215
if( jo.has_bool( "obsolete" ) ) {
221-
assign( jo, "obsolete", obsolete );
216+
mandatory( jo, was_loaded, "obsolete", obsolete );
222217
}
223218

224219
// If it's an obsolete recipe, we don't need any more data, skip loading
@@ -230,25 +225,28 @@ void recipe::load( const JsonObject &jo, const std::string &src )
230225
time = to_moves<int>( read_from_json_string<time_duration>( jo.get_member( "time" ),
231226
time_duration::units ) );
232227
}
233-
assign( jo, "difficulty", difficulty, strict, 0, MAX_SKILL );
234-
assign( jo, "flags", flags );
228+
optional( jo, was_loaded, "difficulty", difficulty, numeric_bound_reader<int> {0, MAX_SKILL} );
229+
optional( jo, was_loaded, "flags", flags );
235230

236231
// automatically set contained if we specify as container
237-
assign( jo, "contained", contained, strict );
238-
contained |= assign( jo, "container", container, strict );
232+
optional( jo, was_loaded, "contained", contained, false );
233+
if( jo.has_member( "container" ) ) {
234+
contained = true;
235+
optional( jo, was_loaded, "container", container, itype_id::NULL_ID() );
236+
}
239237
optional( jo, false, "container_variant", container_variant );
240-
assign( jo, "sealed", sealed, strict );
238+
optional( jo, was_loaded, "sealed", sealed, true );
241239

242240
if( jo.has_array( "batch_time_factors" ) ) {
243241
JsonArray batch = jo.get_array( "batch_time_factors" );
244242
batch_rscale = batch.get_int( 0 ) / 100.0;
245243
batch_rsize = batch.get_int( 1 );
246244
}
247245

248-
assign( jo, "charges", charges );
249-
assign( jo, "result_mult", result_mult );
246+
optional( jo, was_loaded, "charges", charges );
247+
optional( jo, was_loaded, "result_mult", result_mult, 1 );
250248

251-
assign( jo, "skill_used", skill_used, strict );
249+
optional( jo, was_loaded, "skill_used", skill_used, skill_id::NULL_ID() );
252250

253251
if( jo.has_member( "skills_required" ) ) {
254252
JsonArray sk = jo.get_array( "skills_required" );
@@ -273,7 +271,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
273271

274272
// simplified autolearn sets requirements equal to required skills at finalization
275273
if( jo.has_bool( "autolearn" ) ) {
276-
assign( jo, "autolearn", autolearn );
274+
optional( jo, was_loaded, "autolearn", autolearn, false );
277275

278276
} else if( jo.has_array( "autolearn" ) ) {
279277
autolearn = true;
@@ -286,9 +284,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
286284
mandatory( jo, was_loaded, "activity_level", exertion, activity_level_reader{} );
287285

288286
// Never let the player have a debug or NPC recipe
289-
if( jo.has_bool( "never_learn" ) ) {
290-
assign( jo, "never_learn", never_learn );
291-
}
287+
optional( jo, was_loaded, "never_learn", never_learn, false );
292288

293289
if( jo.has_member( "decomp_learn" ) ) {
294290
learn_by_disassembly.clear();
@@ -297,7 +293,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
297293
if( !skill_used ) {
298294
jo.throw_error( "decomp_learn specified with no skill_used" );
299295
}
300-
assign( jo, "decomp_learn", learn_by_disassembly[skill_used] );
296+
optional( jo, false, "decomp_learn", learn_by_disassembly[skill_used] );
301297

302298
} else if( jo.has_array( "decomp_learn" ) ) {
303299
for( JsonArray arr : jo.get_array( "decomp_learn" ) ) {
@@ -335,10 +331,10 @@ void recipe::load( const JsonObject &jo, const std::string &src )
335331

336332
mandatory( jo, was_loaded, "category", category );
337333
mandatory( jo, was_loaded, "subcategory", subcategory );
338-
assign( jo, "description", description, strict );
334+
optional( jo, was_loaded, "description", description );
339335

340336
if( jo.has_bool( "reversible" ) ) {
341-
assign( jo, "reversible", reversible, strict );
337+
mandatory( jo, was_loaded, "reversible", reversible );
342338
} else if( jo.has_object( "reversible" ) ) {
343339
reversible = true;
344340
// Convert duration to time in moves
@@ -363,10 +359,10 @@ void recipe::load( const JsonObject &jo, const std::string &src )
363359
byproduct_group = item_group::load_item_group( jo.get_member( "byproduct_group" ),
364360
"collection", "byproducts of recipe " + id.str() );
365361
}
366-
assign( jo, "construction_blueprint", blueprint );
362+
optional( jo, was_loaded, "construction_blueprint", blueprint );
367363
if( !blueprint.is_empty() ) {
368-
assign( jo, "blueprint_name", bp_name );
369-
assign( jo, "blueprint_parameter_names", bp_parameter_names );
364+
optional( jo, was_loaded, "blueprint_name", bp_name );
365+
optional( jo, was_loaded, "blueprint_parameter_names", bp_parameter_names );
370366
bp_resources.clear();
371367
for( const std::string resource : jo.get_array( "blueprint_resources" ) ) {
372368
bp_resources.emplace_back( resource );
@@ -423,7 +419,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
423419
mandatory( jo, false, "name", name_ );
424420
mandatory( jo, was_loaded, "category", category );
425421
mandatory( jo, was_loaded, "subcategory", subcategory );
426-
assign( jo, "description", description, strict );
422+
optional( jo, was_loaded, "description", description );
427423
mandatory( jo, was_loaded, "practice_data", practice_data );
428424

429425
if( jo.has_member( "byproducts" ) ) {
@@ -446,7 +442,7 @@ void recipe::load( const JsonObject &jo, const std::string &src )
446442
mandatory( jo, false, "name", name_ );
447443
mandatory( jo, was_loaded, "category", category );
448444
mandatory( jo, was_loaded, "subcategory", subcategory );
449-
assign( jo, "description", description, strict );
445+
optional( jo, was_loaded, "description", description );
450446
mandatory( jo, was_loaded, "nested_category_data", nested_category_data );
451447

452448
} else {

src/recipe.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <optional>
1010
#include <set>
1111
#include <string>
12+
#include <string_view>
1213
#include <utility>
1314
#include <vector>
1415

@@ -98,7 +99,7 @@ class recipe
9899
float exertion = 0.0f;
99100

100101
public:
101-
recipe();
102+
recipe() = default;
102103

103104
bool is_null() const {
104105
return id.is_null();
@@ -269,7 +270,7 @@ class recipe
269270
return reversible;
270271
}
271272

272-
void load( const JsonObject &jo, const std::string &src );
273+
void load( const JsonObject &jo, std::string_view src );
273274
void finalize();
274275

275276
/** Returns a non-empty string describing an inconsistency (if any) in the recipe. */

0 commit comments

Comments
 (0)