Skip to content

Commit 9cf0e65

Browse files
authored
Merge pull request #82767 from ehughsbaird/final-step
Use optional/mandatory in mapdata, recipe, mod manager, and monster groups
2 parents 6b6db9b + 533b7a2 commit 9cf0e65

File tree

5 files changed

+50
-51
lines changed

5 files changed

+50
-51
lines changed

src/mapdata.cpp

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

11-
#include "assign.h"
1211
#include "avatar.h"
1312
#include "calendar.h"
1413
#include "character.h"
@@ -514,9 +513,9 @@ bool furn_workbench_info::load( const JsonObject &jsobj, std::string_view member
514513
{
515514
JsonObject j = jsobj.get_object( member );
516515

517-
assign( j, "multiplier", multiplier );
518-
assign( j, "mass", allowed_mass );
519-
assign( j, "volume", allowed_volume );
516+
optional( j, false, "multiplier", multiplier, 1.0f );
517+
optional( j, false, "mass", allowed_mass, units::mass::max() );
518+
optional( j, false, "volume", allowed_volume, units::volume::max() );
520519

521520
return true;
522521
}
@@ -528,10 +527,10 @@ bool plant_data::load( const JsonObject &jsobj, std::string_view member )
528527
{
529528
JsonObject j = jsobj.get_object( member );
530529

531-
assign( j, "transform", transform );
532-
assign( j, "base", base );
533-
assign( j, "growth_multiplier", growth_multiplier );
534-
assign( j, "harvest_multiplier", harvest_multiplier );
530+
optional( j, false, "transform", transform, furn_str_id::NULL_ID() );
531+
optional( j, false, "base", base, furn_str_id::NULL_ID() );
532+
optional( j, false, "growth_multiplier", growth_multiplier, 1.0f );
533+
optional( j, false, "harvest_multiplier", harvest_multiplier, 1.0f );
535534

536535
return true;
537536
}
@@ -1196,7 +1195,7 @@ void ter_t::load( const JsonObject &jo, const std::string &src )
11961195
{
11971196
map_data_common_t::load( jo, src );
11981197
optional( jo, was_loaded, "move_cost", movecost );
1199-
assign( jo, "max_volume", max_volume, src == "dda" );
1198+
optional( jo, was_loaded, "max_volume", max_volume, DEFAULT_TILE_VOLUME );
12001199
optional( jo, was_loaded, "trap", trap_id_str );
12011200
optional( jo, was_loaded, "heat_radiation", heat_radiation );
12021201

src/mod_manager.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#include <queue>
1010
#include <stdexcept>
1111

12-
#include "assign.h"
1312
#include "cata_utility.h"
1413
#include "debug.h"
1514
#include "dependency_tree.h"
1615
#include "filesystem.h"
1716
#include "flexbuffer_json.h"
17+
#include "generic_factory.h"
1818
#include "input_context.h"
1919
#include "json.h"
2020
#include "localized_comparator.h"
@@ -280,21 +280,22 @@ void mod_manager::load_modfile( const JsonObject &jo, const cata_path &path )
280280
modfile.category = p_cat;
281281

282282
std::string mod_json_path;
283-
if( assign( jo, "path", mod_json_path ) ) {
283+
if( jo.has_member( "path" ) ) {
284+
optional( jo, false, "path", mod_json_path );
284285
modfile.path = path / mod_json_path;
285286
} else {
286287
modfile.path = path;
287288
}
288289

289-
assign( jo, "authors", modfile.authors );
290-
assign( jo, "maintainers", modfile.maintainers );
291-
assign( jo, "description", modfile.description );
292-
assign( jo, "version", modfile.version );
293-
assign( jo, "dependencies", modfile.dependencies );
294-
assign( jo, "conflicts", modfile.conflicts );
295-
assign( jo, "core", modfile.core );
296-
assign( jo, "obsolete", modfile.obsolete );
297-
assign( jo, "loading_images", modfile.loading_images );
290+
optional( jo, false, "authors", modfile.authors );
291+
optional( jo, false, "maintainers", modfile.maintainers );
292+
optional( jo, false, "description", modfile.description );
293+
optional( jo, false, "version", modfile.version );
294+
optional( jo, false, "dependencies", modfile.dependencies );
295+
optional( jo, false, "conflicts", modfile.conflicts );
296+
optional( jo, false, "core", modfile.core, false );
297+
optional( jo, false, "obsolete", modfile.obsolete, false );
298+
optional( jo, false, "loading_images", modfile.loading_images );
298299

299300
if( std::find( modfile.dependencies.begin(), modfile.dependencies.end(),
300301
modfile.ident ) != modfile.dependencies.end() ) {

src/mongroup.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#include <string>
55
#include <utility>
66

7-
#include "assign.h"
87
#include "calendar.h"
98
#include "cata_utility.h"
109
#include "debug.h"
1110
#include "enum_conversions.h"
1211
#include "flexbuffer_json.h"
12+
#include "generic_factory.h"
1313
#include "mtype.h"
1414
#include "options.h"
1515
#include "rng.h"
@@ -554,7 +554,9 @@ void MonsterGroupManager::LoadMonsterGroup( const JsonObject &jo )
554554
g.replace_monster_group = jo.get_bool( "replace_monster_group", false );
555555
g.new_monster_group = mongroup_id( jo.get_string( "new_monster_group_id",
556556
mongroup_id::NULL_ID().str() ) );
557-
assign( jo, "replacement_time", g.monster_group_time, false, 1_days );
557+
if( jo.has_member( "replacement_time" ) ) {
558+
mandatory( jo, false, "replacement_time", g.monster_group_time, time_bound_reader{ 1_days } );
559+
}
558560
g.is_safe = jo.get_bool( "is_safe", false );
559561

560562
g.freq_total = jo.get_int( "freq_total", ( extending ? g.freq_total : 0 ) + freq_total );

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)