Skip to content

Commit bb6810b

Browse files
committed
overmap optional/mandatory
1 parent 61d09a9 commit bb6810b

File tree

2 files changed

+81
-85
lines changed

2 files changed

+81
-85
lines changed

src/omdata.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <utility>
1515
#include <vector>
1616

17-
#include "assign.h"
1817
#include "color.h"
1918
#include "common_types.h"
2019
#include "coordinates.h"
@@ -140,7 +139,7 @@ class overmap_land_use_code
140139

141140
// Used by generic_factory
142141
bool was_loaded = false;
143-
void load( const JsonObject &jo, const std::string &src );
142+
void load( const JsonObject &jo, std::string_view src );
144143
void finalize();
145144
void check() const;
146145
};
@@ -369,7 +368,7 @@ struct oter_type_t {
369368
flags.set( flag, value );
370369
}
371370

372-
void load( const JsonObject &jo, const std::string &src );
371+
void load( const JsonObject &jo, std::string_view src );
373372
void check() const;
374373
void finalize();
375374

@@ -635,14 +634,7 @@ struct overmap_special_connection {
635634
string_id<overmap_connection> connection;
636635
bool existing = false;
637636

638-
void deserialize( const JsonValue &jsin ) {
639-
JsonObject jo = jsin.get_object();
640-
jo.read( "point", p );
641-
jo.read( "terrain", terrain );
642-
jo.read( "existing", existing );
643-
jo.read( "connection", connection );
644-
assign( jo, "from", from );
645-
}
637+
void deserialize( const JsonObject &jo );
646638
};
647639

648640
struct overmap_special_placement_constraints {
@@ -729,7 +721,7 @@ class overmap_special
729721

730722
// Used by generic_factory
731723
bool was_loaded = false;
732-
void load( const JsonObject &jo, const std::string &src );
724+
void load( const JsonObject &jo, std::string_view src );
733725
void finalize();
734726
void finalize_mapgen_parameters();
735727
void check() const;

src/overmap.cpp

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <vector>
1919

2020
#include "all_enum_values.h"
21-
#include "assign.h"
2221
#include "auto_note.h"
2322
#include "avatar.h"
2423
#include "cached_options.h"
@@ -470,22 +469,19 @@ std::string overmap_land_use_code::get_symbol() const
470469
return utf32_to_utf8( symbol );
471470
}
472471

473-
void overmap_land_use_code::load( const JsonObject &jo, const std::string &src )
472+
void overmap_land_use_code::load( const JsonObject &jo, const std::string_view )
474473
{
475-
const bool strict = src == "dda";
476-
assign( jo, "land_use_code", land_use_code, strict );
477-
assign( jo, "name", name, strict );
478-
assign( jo, "detailed_definition", detailed_definition, strict );
474+
optional( jo, was_loaded, "land_use_code", land_use_code );
475+
optional( jo, was_loaded, "name", name );
476+
optional( jo, was_loaded, "detailed_definition", detailed_definition );
479477

480478
optional( jo, was_loaded, "sym", symbol, unicode_codepoint_from_symbol_reader, NULL_UNICODE );
481-
482479
if( symbol == NULL_UNICODE ) {
483480
DebugLog( D_ERROR, D_GAME ) << "`sym` node is not defined properly for `land_use_code`: "
484481
<< id.c_str() << " (" << name << ")";
485482
}
486483

487-
optional( jo, was_loaded, "color", color, nc_color_reader{} );
488-
484+
optional( jo, was_loaded, "color", color, nc_color_reader{}, c_black );
489485
}
490486

491487
void overmap_land_use_code::finalize()
@@ -860,7 +856,7 @@ void oter_vision::level::deserialize( const JsonObject &jo )
860856
}
861857
mandatory( jo, false, "name", name );
862858
mandatory( jo, false, "sym", symbol, unicode_codepoint_from_symbol_reader );
863-
optional( jo, false, "color", color, nc_color_reader{} );
859+
mandatory( jo, false, "color", color, nc_color_reader{} );
864860
optional( jo, false, "looks_like", looks_like );
865861
}
866862

@@ -907,35 +903,32 @@ void oter_vision::check() const
907903
}
908904

909905

910-
void oter_type_t::load( const JsonObject &jo, const std::string &src )
906+
void oter_type_t::load( const JsonObject &jo, const std::string_view )
911907
{
912-
const bool strict = src == "dda";
913-
914908
optional( jo, was_loaded, "sym", symbol, unicode_codepoint_from_symbol_reader, NULL_UNICODE );
915909

916-
assign( jo, "name", name, strict );
910+
optional( jo, was_loaded, "name", name );
917911
// For some reason an enum can be read as a number??
918912
if( jo.has_number( "see_cost" ) ) {
919913
jo.throw_error( string_format( "In %s: See cost uses invalid number format", id.str() ) );
920914
}
921915
mandatory( jo, was_loaded, "see_cost", see_cost );
922-
assign( jo, "extras", extras, strict );
923-
assign( jo, "mondensity", mondensity, strict );
924-
assign( jo, "entry_eoc", entry_EOC, strict );
925-
assign( jo, "exit_eoc", exit_EOC, strict );
926-
assign( jo, "spawns", static_spawns, strict );
916+
optional( jo, was_loaded, "extras", extras, "none" );
917+
optional( jo, was_loaded, "mondensity", mondensity, 0 );
918+
optional( jo, was_loaded, "entry_eoc", entry_EOC );
919+
optional( jo, was_loaded, "exit_eoc", exit_EOC );
920+
optional( jo, was_loaded, "spawns", static_spawns );
927921
optional( jo, was_loaded, "color", color, nc_color_reader{} );
928-
assign( jo, "land_use_code", land_use_code, strict );
922+
optional( jo, was_loaded, "land_use_code", land_use_code, overmap_land_use_code_id::NULL_ID() );
929923

930924
if( jo.has_member( "looks_like" ) ) {
931-
std::vector<std::string> ll;
932925
if( jo.has_array( "looks_like" ) ) {
933-
jo.read( "looks_like", ll );
934-
} else if( jo.has_string( "looks_like" ) ) {
935-
const std::string one_look = jo.get_string( "looks_like" );
936-
ll.push_back( one_look );
926+
mandatory( jo, was_loaded, "looks_like", looks_like );
927+
} else {
928+
std::string one_look;
929+
mandatory( jo, was_loaded, "looks_like", one_look );
930+
looks_like = { one_look };
937931
}
938-
looks_like = ll;
939932
} else if( jo.has_member( "copy-from" ) ) {
940933
looks_like.insert( looks_like.begin(), jo.get_string( "copy-from" ) );
941934
}
@@ -1396,6 +1389,15 @@ tripoint displace( cube_direction d )
13961389
cata_fatal( "Invalid cube_direction" );
13971390
}
13981391

1392+
void overmap_special_connection::deserialize( const JsonObject &jo )
1393+
{
1394+
optional( jo, false, "point", p );
1395+
optional( jo, false, "terrain", terrain );
1396+
optional( jo, false, "existing", existing, false );
1397+
optional( jo, false, "connection", connection );
1398+
optional( jo, false, "from", from );
1399+
}
1400+
13991401
struct special_placement_result {
14001402
std::vector<tripoint_om_omt> omts_used;
14011403
std::vector<std::pair<om_pos_dir, std::string>> joins_used;
@@ -2909,6 +2911,46 @@ struct mutable_overmap_special_data : overmap_special_data {
29092911

29102912
return { result, unresolved.all_used() };
29112913
}
2914+
2915+
void load( const JsonObject &jo, bool was_loaded ) {
2916+
optional( jo, was_loaded, "check_for_locations", check_for_locations );
2917+
if( jo.has_array( "check_for_locations_area" ) ) {
2918+
JsonArray jar = jo.get_array( "check_for_locations_area" );
2919+
while( jar.has_more() ) {
2920+
JsonObject joc = jar.next_object();
2921+
2922+
cata::flat_set<string_id<overmap_location>> type;
2923+
tripoint_rel_omt from;
2924+
tripoint_rel_omt to;
2925+
mandatory( joc, was_loaded, "type", type );
2926+
mandatory( joc, was_loaded, "from", from );
2927+
mandatory( joc, was_loaded, "to", to );
2928+
if( from.x() > to.x() ) {
2929+
std::swap( from.x(), to.x() );
2930+
}
2931+
if( from.y() > to.y() ) {
2932+
std::swap( from.y(), to.y() );
2933+
}
2934+
if( from.z() > to.z() ) {
2935+
std::swap( from.z(), to.z() );
2936+
}
2937+
for( int x = from.x(); x <= to.x(); x++ ) {
2938+
for( int y = from.y(); y <= to.y(); y++ ) {
2939+
for( int z = from.z(); z <= to.z(); z++ ) {
2940+
overmap_special_locations loc;
2941+
loc.p = tripoint_rel_omt( x, y, z );
2942+
loc.locations = type;
2943+
check_for_locations.push_back( loc );
2944+
}
2945+
}
2946+
}
2947+
}
2948+
}
2949+
mandatory( jo, was_loaded, "joins", joins_vec );
2950+
mandatory( jo, was_loaded, "overmaps", overmaps );
2951+
mandatory( jo, was_loaded, "root", root );
2952+
mandatory( jo, was_loaded, "phases", phases );
2953+
}
29122954
};
29132955

29142956
overmap_special::overmap_special( const overmap_special_id &i, const overmap_special_terrain &ter )
@@ -3020,15 +3062,15 @@ mapgen_arguments overmap_special::get_args( const mapgendata &md ) const
30203062
return mapgen_params_.get_args( md, mapgen_parameter_scope::overmap_special );
30213063
}
30223064

3023-
void overmap_special::load( const JsonObject &jo, const std::string &src )
3065+
void overmap_special::load( const JsonObject &jo, const std::string_view src )
30243066
{
3025-
const bool strict = src == "dda";
30263067
// city_building is just an alias of overmap_special
30273068
// TODO: This comparison is a hack. Separate them properly.
30283069
const bool is_special = jo.get_string( "type", "" ) == "overmap_special";
30293070

30303071
optional( jo, was_loaded, "subtype", subtype_, overmap_special_subtype::fixed );
30313072
optional( jo, was_loaded, "locations", default_locations_ );
3073+
// FIXME: eoc reader for generic factory
30323074
if( jo.has_member( "eoc" ) ) {
30333075
eoc = effect_on_conditions::load_inline_eoc( jo.get_member( "eoc" ), src );
30343076
has_eoc_ = true;
@@ -3047,45 +3089,7 @@ void overmap_special::load( const JsonObject &jo, const std::string &src )
30473089
case overmap_special_subtype::mutable_: {
30483090
shared_ptr_fast<mutable_overmap_special_data> mutable_data =
30493091
make_shared_fast<mutable_overmap_special_data>( id );
3050-
std::vector<overmap_special_locations> check_for_locations_merged_data;
3051-
optional( jo, was_loaded, "check_for_locations", check_for_locations_merged_data );
3052-
if( jo.has_array( "check_for_locations_area" ) ) {
3053-
JsonArray jar = jo.get_array( "check_for_locations_area" );
3054-
while( jar.has_more() ) {
3055-
JsonObject joc = jar.next_object();
3056-
3057-
cata::flat_set<string_id<overmap_location>> type;
3058-
tripoint_rel_omt from;
3059-
tripoint_rel_omt to;
3060-
mandatory( joc, was_loaded, "type", type );
3061-
mandatory( joc, was_loaded, "from", from );
3062-
mandatory( joc, was_loaded, "to", to );
3063-
if( from.x() > to.x() ) {
3064-
std::swap( from.x(), to.x() );
3065-
}
3066-
if( from.y() > to.y() ) {
3067-
std::swap( from.y(), to.y() );
3068-
}
3069-
if( from.z() > to.z() ) {
3070-
std::swap( from.z(), to.z() );
3071-
}
3072-
for( int x = from.x(); x <= to.x(); x++ ) {
3073-
for( int y = from.y(); y <= to.y(); y++ ) {
3074-
for( int z = from.z(); z <= to.z(); z++ ) {
3075-
overmap_special_locations loc;
3076-
loc.p = tripoint_rel_omt( x, y, z );
3077-
loc.locations = type;
3078-
check_for_locations_merged_data.push_back( loc );
3079-
}
3080-
}
3081-
}
3082-
}
3083-
}
3084-
mutable_data->check_for_locations = check_for_locations_merged_data;
3085-
mandatory( jo, was_loaded, "joins", mutable_data->joins_vec );
3086-
mandatory( jo, was_loaded, "overmaps", mutable_data->overmaps );
3087-
mandatory( jo, was_loaded, "root", mutable_data->root );
3088-
mandatory( jo, was_loaded, "phases", mutable_data->phases );
3092+
mutable_data->load( jo, was_loaded );
30893093
data_ = std::move( mutable_data );
30903094
break;
30913095
}
@@ -3094,18 +3098,18 @@ void overmap_special::load( const JsonObject &jo, const std::string &src )
30943098
io::enum_to_string( subtype_ ) ) );
30953099
}
30963100

3097-
assign( jo, "city_sizes", constraints_.city_size, strict );
3101+
optional( jo, was_loaded, "city_sizes", constraints_.city_size, { 0, INT_MAX } );
30983102

30993103
if( is_special ) {
31003104
mandatory( jo, was_loaded, "occurrences", constraints_.occurrences );
3101-
assign( jo, "city_distance", constraints_.city_distance, strict );
3102-
assign( jo, "priority", priority_, strict );
3105+
optional( jo, was_loaded, "city_distance", constraints_.city_distance, { 0, INT_MAX } );
3106+
optional( jo, was_loaded, "priority", priority_, 0 );
31033107
}
31043108

3105-
assign( jo, "spawns", monster_spawns_, strict );
3109+
optional( jo, was_loaded, "spawns", monster_spawns_ );
31063110

3107-
assign( jo, "rotate", rotatable_, strict );
3108-
assign( jo, "flags", flags_, strict );
3111+
optional( jo, was_loaded, "rotate", rotatable_, true );
3112+
optional( jo, was_loaded, "flags", flags_, string_reader{} );
31093113
}
31103114

31113115
void overmap_special::finalize()

0 commit comments

Comments
 (0)