Skip to content

Commit c87d67f

Browse files
committed
Have presets support saving the same values as the last used scenario preferences
Resolves wesnoth#10377
1 parent 2168690 commit c87d67f

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

src/game_initialization/lobby_info.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "log.hpp"
2121
#include "mp_ui_alerts.hpp"
2222
#include "preferences/preferences.hpp"
23+
#include "serialization/string_utils.hpp"
2324

2425

2526
static lg::log_domain log_engine("engine");
@@ -123,9 +124,16 @@ void lobby_info::process_gamelist(const config& data)
123124
games_by_id_.clear();
124125

125126
for(const config& game : prefs::get().get_game_presets()) {
126-
optional_const_config scenario = game_config_manager::get()->game_config().find_child("multiplayer", "id", game["scenario"].str());
127+
const auto game_config = game_config_manager::get()->game_config();
128+
129+
optional_const_config scenario = game_config.find_child("multiplayer", "id", game["scenario"].str());
127130
if(!scenario) {
128-
ERR_LB << "Scenario " << game["scenario"].str() << " not found in game config";
131+
ERR_LB << "Scenario " << game["scenario"].str() << " not found in game preset " << game["id"];
132+
continue;
133+
}
134+
optional_const_config era = game_config.find_child("era", "id", game["era"].str());
135+
if(!era) {
136+
ERR_LB << "Era " << game["era"].str() << " not found in game preset " << game["id"];
129137
continue;
130138
}
131139

@@ -153,6 +161,7 @@ void lobby_info::process_gamelist(const config& data)
153161
qgame["mp_shroud"] = game["shroud"];
154162
qgame["mp_village_gold"] = game["village_gold"];
155163
qgame["experience_modifier"] = game["experience_modifier"];
164+
qgame["random_faction_mode"] = game["random_faction_mode"];
156165

157166
qgame["mp_countdown"] = game["countdown"];
158167
if(qgame["mp_countdown"].to_bool()) {
@@ -165,12 +174,30 @@ void lobby_info::process_gamelist(const config& data)
165174
qgame["observer"] = game["observer"];
166175
qgame["human_sides"] = human_sides;
167176

177+
std::vector<std::string> mods = utils::split(game["mp_modifications"].str());
178+
for(const std::string& mod : mods) {
179+
auto cfg = game_config.find_child("modification", "id", mod);
180+
181+
if(cfg) {
182+
config& m = qgame.add_child("modification");
183+
m["name"] = cfg["name"];
184+
m["id"] = mod;
185+
} else {
186+
ERR_LB << "Modification " << mod << " not found in game preset " << game["id"];
187+
continue;
188+
}
189+
}
190+
191+
if(game.has_child("options")) {
192+
qgame.add_child("options", game.mandatory_child("options"));
193+
}
194+
168195
if(scenario->has_attribute("map_data")) {
169196
qgame["map_data"] = scenario["map_data"];
170197
} else {
171198
qgame["map_data"] = filesystem::read_map(scenario["map_file"]);
172199
}
173-
qgame["hash"] = game_config_manager::get()->game_config().mandatory_child("multiplayer_hashes")[game["scenario"].str()];
200+
qgame["hash"] = game_config.mandatory_child("multiplayer_hashes")[game["scenario"].str()];
174201

175202
config& qchild = qgame.add_child("slot_data");
176203
qchild["vacant"] = human_sides;

src/gui/dialogs/multiplayer/mp_create_game.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mp_create_game::mp_create_game(saved_game& state, bool local_mode)
6565
, options_manager_()
6666
, selected_game_index_(-1)
6767
, selected_rfm_index_(-1)
68-
, use_map_settings_(register_bool( "use_map_settings", true,
68+
, use_map_settings_(register_bool("use_map_settings", true,
6969
[]() {return prefs::get().mp_use_map_settings();},
7070
[](bool v) {prefs::get().set_mp_use_map_settings(v);},
7171
std::bind(&mp_create_game::update_map_settings, this)))
@@ -219,8 +219,17 @@ void mp_create_game::quick_mp_setup(saved_game& state, const config presets)
219219
create.get_state().classification().oos_debug = false;
220220
params.shuffle_sides = presets["shuffle_sides"].to_bool();
221221

222-
params.mode = random_faction_mode::type::no_mirror;
222+
params.mode = random_faction_mode::get_enum(presets["random_faction_mode"].str()).value_or(random_faction_mode::type::independent);
223223
params.name = settings::game_name_default();
224+
225+
std::vector<std::string> mods = utils::split(presets["mp_modifications"].str());
226+
for(const std::string& mod : mods) {
227+
create.active_mods().push_back(mod);
228+
}
229+
230+
if(presets.has_child("options")) {
231+
params.options = presets.mandatory_child("options");
232+
}
224233
}
225234

226235
void mp_create_game::pre_show()
@@ -903,6 +912,14 @@ void mp_create_game::save_preset()
903912
preset["turns"] = turns_->get_widget_value();
904913
preset["observer"] = observers_->get_widget_value();
905914
preset["use_map_settings"] = use_map_settings_->get_widget_value();
915+
916+
// TODO: need to populate the same in the list of presets selectable in the game creation list
917+
// TODO: test that addon info is stored in the database correctly
918+
// TODO: test with non-mainline modifications/eras/scenarios
919+
preset["mp_modifications"] = utils::join(create_engine_.active_mods(), ",");
920+
preset.add_child("options", options_manager_->get_options_config());
921+
random_faction_mode::type type = random_faction_mode::get_enum(selected_rfm_index_).value_or(random_faction_mode::type::independent);
922+
preset["random_faction_mode"] = random_faction_mode::get_string(type);
906923

907924
prefs::get().add_game_preset(std::move(preset));
908925
}

0 commit comments

Comments
 (0)