Skip to content

Commit

Permalink
support scenario files in map_file (#4804)
Browse files Browse the repository at this point in the history
* support scenario files in map_file

Now a scenario generated by the scenario editor can be loaded
just like a simple map file.

fixes #4803
  • Loading branch information
gfgtdf committed Mar 15, 2020
1 parent 2f9a82a commit 528dbcc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/config.cpp
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/config.hpp
Expand Up @@ -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;

Expand Down
11 changes: 5 additions & 6 deletions src/gui/dialogs/multiplayer/mp_create_game.cpp
Expand Up @@ -709,14 +709,13 @@ void mp_create_game::update_details(window& win)
create_engine_.get_state().classification().campaign = "";

find_widget<stacked_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<minimap>(&win, "minimap", false).set_map_data(map_data);

find_widget<minimap>(&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());
Expand Down
39 changes: 35 additions & 4 deletions src/saved_game.cpp
Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/saved_game.hpp
Expand Up @@ -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;
Expand Down

0 comments on commit 528dbcc

Please sign in to comment.