diff --git a/src/config.cpp b/src/config.cpp index 792690f7daa1..60c15d7cf85d 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1226,6 +1226,19 @@ void config::inherit_from(const config& c) swap(scratch); } +/** + * Merge the attributes of config 'c' into this config, preserving this config's values. + */ +void config::inherit_attributes(const config& cfg) +{ + check_valid(cfg); + for(const attribute& v : cfg.values_) { + attribute_value& v2 = values_[v.first]; + if(v2.blank()) { + v2 = v.second; + } + } +} bool config::matches(const config& filter) const { check_valid(filter); diff --git a/src/config.hpp b/src/config.hpp index d83850972a61..7fac2f77bc62 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -693,6 +693,10 @@ class config * Merge config 'c' into this config, preserving this config's values. */ void inherit_from(const config& c); + /** + * Merge the attributes of config 'c' into this config, preserving this config's values. + */ + void inherit_attributes(const config& c); bool matches(const config &filter) const; diff --git a/src/gui/dialogs/multiplayer/mp_create_game.cpp b/src/gui/dialogs/multiplayer/mp_create_game.cpp index 1ddf5cca57ab..b34502bad3b5 100644 --- a/src/gui/dialogs/multiplayer/mp_create_game.cpp +++ b/src/gui/dialogs/multiplayer/mp_create_game.cpp @@ -709,14 +709,13 @@ void mp_create_game::update_details(window& win) create_engine_.get_state().classification().campaign = ""; find_widget(&win, "minimap_stack", false).select_layer(0); - const std::string map_data = !current_scenario->data()["map_data"].empty() - ? current_scenario->data()["map_data"] - : filesystem::read_map(current_scenario->data()["map_file"]); - if (current_scenario->data()["map_data"].empty()) { - current_scenario->data()["map_data"] = map_data; + + if(current_scenario->data()["map_data"].empty()) { + saved_game::expand_map_file(current_scenario->data()); current_scenario->set_metadata(); } - find_widget(&win, "minimap", false).set_map_data(map_data); + + find_widget(&win, "minimap", false).set_map_data(current_scenario->data()["map_data"]); players.set_label(std::to_string(current_scenario->num_players())); map_size.set_label(current_scenario->map_size()); diff --git a/src/saved_game.cpp b/src/saved_game.cpp index 82124548a714..b4e13897009d 100644 --- a/src/saved_game.cpp +++ b/src/saved_game.cpp @@ -432,6 +432,40 @@ void saved_game::expand_mp_options() } } +static void inherit_scenario(config& scenario, config& map_scenario) +{ + config sides; + sides.splice_children(map_scenario, "side"); + scenario.append_children(map_scenario); + for(config& side_from : sides.child_range("side")) { + config& side_to = scenario.find_child("side", "side", side_from["side"]); + if(side_to) { + side_to.inherit_attributes(side_from); + side_to.append_children(side_from); + } + else { + scenario.add_child("side", side_from); + } + } +} + +void saved_game::expand_map_file(config& scenario) +{ + if(scenario["map_data"].empty() && !scenario["map_file"].empty()) { + std::string map_data = filesystem::read_map(scenario["map_file"]); + if (map_data.find("map_data") != std::string::npos) { + //we have a scenario, gnerated by the editor + config map_data_cfg; + read(map_data_cfg, map_data); + inherit_scenario(scenario, map_data_cfg); + } + else { + //we have an plain map_data file + scenario["map_data"]= map_data; + } + } +} + void saved_game::expand_random_scenario() { expand_scenario(); @@ -453,10 +487,7 @@ void saved_game::expand_random_scenario() } // If no map_data is provided, try to load the specified file directly - if(starting_point_["map_data"].empty() && !starting_point_["map_file"].empty()) { - starting_point_["map_data"] = filesystem::read_map(starting_point_["map_file"]); - } - + expand_map_file(starting_point_); // If the map should be randomly generated // We don’t want that we accidentally to this twice so we check for starting_point_["map_data"].empty() if(starting_point_["map_data"].empty() && !starting_point_["map_generation"].empty()) { diff --git a/src/saved_game.hpp b/src/saved_game.hpp index 867e6c0e406b..e12c4931de07 100644 --- a/src/saved_game.hpp +++ b/src/saved_game.hpp @@ -82,6 +82,8 @@ class saved_game void expand_random_scenario(); /// copies attributes & tags from the 'outer' [scenario] to the scenario that is generated by scenario_generation= static void post_scenario_generation(const config& old_scenario, config& generated_scenario); + /// reads scenario["map_file"] + static void expand_map_file(config& scenario); /// Add addon_id information if needed. void check_require_scenario(); bool valid() const;