The Battle for Wesnoth  1.15.9+dev
mp_create_game.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "filesystem.hpp"
20 #include "formatter.hpp"
21 #include "formula/string_utils.hpp"
22 #include "game_config.hpp"
23 #include "game_config_manager.hpp"
25 #include "gettext.hpp"
26 #include "gui/auxiliary/field.hpp"
27 #include "gui/dialogs/message.hpp"
30 #include "gui/widgets/button.hpp"
31 #include "gui/widgets/image.hpp"
33 #include "gui/widgets/listbox.hpp"
35 #include "gui/widgets/minimap.hpp"
36 #include "gui/widgets/slider.hpp"
39 #include "gui/widgets/text_box.hpp"
42 #include "log.hpp"
43 #include "map_settings.hpp"
44 #include "preferences/game.hpp"
45 #include "save_index.hpp"
46 #include "savegame.hpp"
47 
48 #include <boost/algorithm/string.hpp>
49 
50 static lg::log_domain log_mp_create("mp/create");
51 
52 #define DBG_MP LOG_STREAM(debug, log_mp_create)
53 #define WRN_MP LOG_STREAM(warn, log_mp_create)
54 #define ERR_MP LOG_STREAM(err, log_mp_create)
55 
56 namespace gui2
57 {
58 namespace dialogs
59 {
60 
61 // Special retval value for loading a game
62 static const int LOAD_GAME = 100;
63 
64 // Shorthand
65 namespace prefs = preferences;
66 
68 
69 mp_create_game::mp_create_game(saved_game& state, bool local_mode)
70  : create_engine_(state)
71  , config_engine_()
72  , options_manager_()
73  , selected_game_index_(-1)
74  , selected_rfm_index_(-1)
75  , use_map_settings_(register_bool( "use_map_settings", true, prefs::use_map_settings, prefs::set_use_map_settings,
76  std::bind(&mp_create_game::update_map_settings, this)))
77  , fog_(register_bool("fog", true, prefs::fog, prefs::set_fog))
78  , shroud_(register_bool("shroud", true, prefs::shroud, prefs::set_shroud))
79  , start_time_(register_bool("random_start_time", true, prefs::random_start_time, prefs::set_random_start_time))
80  , time_limit_(register_bool("time_limit", true, prefs::countdown, prefs::set_countdown,
81  std::bind(&mp_create_game::update_map_settings, this)))
82  , shuffle_sides_(register_bool("shuffle_sides", true, prefs::shuffle_sides, prefs::set_shuffle_sides))
83  , observers_(register_bool("observers", true, prefs::allow_observers, prefs::set_allow_observers))
84  , strict_sync_(register_bool("strict_sync", true))
85  , private_replay_(register_bool("private_replay", true))
86  , turns_(register_integer("turn_count", true, prefs::turns, prefs::set_turns))
87  , gold_(register_integer("village_gold", true, prefs::village_gold, prefs::set_village_gold))
88  , support_(register_integer("village_support", true, prefs::village_support, prefs::set_village_support))
89  , experience_(register_integer("experience_modifier", true, prefs::xp_modifier, prefs::set_xp_modifier))
90  , init_turn_limit_(register_integer("init_turn_limit", true, prefs::countdown_init_time, prefs::set_countdown_init_time))
91  , turn_bonus_(register_integer("turn_bonus", true, prefs::countdown_turn_bonus, prefs::set_countdown_turn_bonus))
92  , reservoir_(register_integer("reservoir", true, prefs::countdown_reservoir_time, prefs::set_countdown_reservoir_time))
93  , action_bonus_(register_integer("action_bonus", true, prefs::countdown_action_bonus, prefs::set_countdown_action_bonus))
94  , mod_list_()
95  , eras_menu_button_()
96  , local_mode_(local_mode)
97 {
98  level_types_ = {
99  {ng::level::TYPE::SCENARIO, _("Scenarios")},
100  {ng::level::TYPE::CAMPAIGN, _("Multiplayer Campaigns")},
101  {ng::level::TYPE::SP_CAMPAIGN, _("Singleplayer Campaigns")},
102  {ng::level::TYPE::USER_MAP, _("Custom Maps")},
103  {ng::level::TYPE::USER_SCENARIO, _("Custom Scenarios")},
104  {ng::level::TYPE::RANDOM_MAP, _("Random Maps")},
105  };
106 
107  level_types_.erase(std::remove_if(level_types_.begin(), level_types_.end(),
108  [this](level_type_info& type_info) {
109  return create_engine_.get_levels_by_type_unfiltered(type_info.first).empty();
110  }), level_types_.end());
111 
112  rfm_types_ = {
113  mp_game_settings::RANDOM_FACTION_MODE::DEFAULT,
114  mp_game_settings::RANDOM_FACTION_MODE::NO_MIRROR,
115  mp_game_settings::RANDOM_FACTION_MODE::NO_ALLY_MIRROR,
116  };
117 
118  set_show_even_without_video(true);
119 
120  create_engine_.init_active_mods();
121 
122  create_engine_.get_state().clear();
123  create_engine_.get_state().classification().campaign_type = game_classification::CAMPAIGN_TYPE::MULTIPLAYER;
124 
125  // Need to set this in the constructor, pre_show() is too late
126  set_allow_plugin_skip(false);
127 }
128 
130 {
131  find_widget<text_box>(&win, "game_name", false).set_value(local_mode_ ? "" : ng::configure_engine::game_name_default());
132 
134  find_widget<button>(&win, "random_map_regenerate", false),
135  std::bind(&mp_create_game::regenerate_random_map, this));
136 
138  find_widget<button>(&win, "random_map_settings", false),
139  std::bind(&mp_create_game::show_generator_settings, this));
140 
142  find_widget<button>(&win, "load_game", false),
143  std::bind(&mp_create_game::load_game_callback, this));
144 
145  // Custom dialog close hook
146  win.set_exit_hook_ok_only([this](window& w)->bool { return dialog_exit_hook(w); });
147 
148  //
149  // Set up the options manager. Needs to be done before selecting an initial tab
150  //
152 
153  //
154  // Set up filtering
155  //
156  connect_signal_notify_modified(find_widget<slider>(&win, "num_players", false),
157  std::bind(&mp_create_game::on_filter_change<slider>, this, "num_players", true));
158 
159  text_box& filter = find_widget<text_box>(&win, "game_filter", false);
160 
162  std::bind(&mp_create_game::on_filter_change<text_box>, this, "game_filter", true));
163 
164  // Note this cannot be in the keyboard chain or it will capture focus from other text boxes
165  win.keyboard_capture(&filter);
166 
167  //
168  // Set up game types menu_button
169  //
170  std::vector<config> game_types;
171  for(level_type_info& type_info : level_types_) {
172  game_types.emplace_back("label", type_info.second);
173  }
174 
175  if(game_types.empty()) {
176  gui2::show_transient_message("", _("No games found."));
177  throw game::error(_("No games found."));
178  }
179 
180  menu_button& game_menu_button = find_widget<menu_button>(&win, "game_types", false);
181 
182  // Helper to make sure the initially selected level type is valid
183  auto get_initial_type_index = [this]()->int {
184  const auto index = std::find_if(level_types_.begin(), level_types_.end(), [](level_type_info& info) {
185  return info.first == ng::level::TYPE::from_int(preferences::level_type());
186  });
187 
188  if(index != level_types_.end()) {
189  return std::distance(level_types_.begin(), index);
190  }
191 
192  return 0;
193  };
194 
195  game_menu_button.set_values(game_types, get_initial_type_index());
196 
197  connect_signal_notify_modified(game_menu_button,
198  std::bind(&mp_create_game::update_games_list, this));
199 
200  //
201  // Set up mods list
202  //
203  mod_list_ = &find_widget<listbox>(&win, "mod_list", false);
204 
205  const auto& activemods = preferences::modifications();
207  std::map<std::string, string_map> data;
209 
210  item["label"] = mod->name;
211  data.emplace("mod_name", item);
212 
213  grid* row_grid = &mod_list_->add_row(data);
214 
215  find_widget<toggle_panel>(row_grid, "panel", false).set_tooltip(mod->description);
216 
217  toggle_button& mog_toggle = find_widget<toggle_button>(row_grid, "mod_active_state", false);
218 
219  const int i = mod_list_->get_item_count() - 1;
220  if(std::find(activemods.begin(), activemods.end(), mod->id) != activemods.end()) {
221  create_engine_.active_mods().push_back(mod->id);
222  mog_toggle.set_value_bool(true);
223  }
224 
225  connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, i, &mog_toggle));
226  }
227 
228  // No mods, hide the header
229  if(mod_list_->get_item_count() <= 0) {
230  find_widget<styled_widget>(&win, "mods_header", false).set_visible(widget::visibility::invisible);
231  }
232 
233  //
234  // Set up eras menu_button
235  //
236  eras_menu_button_ = &find_widget<menu_button>(&win, "eras", false);
237 
238  std::vector<config> era_names;
240  era_names.emplace_back("label", era->name, "tooltip", era->description);
241  }
242 
243  if(era_names.empty()) {
244  gui2::show_transient_message("", _("No eras found."));
245  throw config::error(_("No eras found"));
246  }
247 
248  eras_menu_button_->set_values(era_names);
249 
251  std::bind(&mp_create_game::on_era_select, this));
252 
254  if(era_selection >= 0) {
255  eras_menu_button_->set_selected(era_selection);
256  }
257 
258  on_era_select();
259 
260  //
261  // Set up random faction mode menu_button
262  //
263  std::vector<config> rfm_options;
264  for(const auto& type : rfm_types_) {
265  // HACK: The labels are defined for the wesnoth textdomain in a header,
266  // see mp_game_settings::RANDOM_FACTION_MODE in src/mp_game_settings.hpp
267  rfm_options.emplace_back("label",
268  translation::dsgettext("wesnoth", mp_game_settings::RANDOM_FACTION_MODE::enum_to_string(type).c_str())
269  );
270  };
271 
272  // Manually insert tooltips. Need to find a better way to do this
273  rfm_options[0]["tooltip"] = _("Independent: Random factions assigned independently");
274  rfm_options[1]["tooltip"] = _("No Mirror: No two players will get the same faction");
275  rfm_options[2]["tooltip"] = _("No Ally Mirror: No two allied players will get the same faction");
276 
277  const int initial_index = std::distance(rfm_types_.begin(), std::find(rfm_types_.begin(), rfm_types_.end(),
278  mp_game_settings::RANDOM_FACTION_MODE::string_to_enum(prefs::random_faction_mode(), mp_game_settings::RANDOM_FACTION_MODE::DEFAULT))
279  );
280 
281  menu_button& rfm_menu_button = find_widget<menu_button>(&win, "random_faction_mode", false);
282 
283  rfm_menu_button.set_values(rfm_options, initial_index);
284 
285  connect_signal_notify_modified(rfm_menu_button,
287 
289 
290  //
291  // Set up the setting status labels
292  //
293  bind_status_label<slider>(&win, turns_->id());
294  bind_status_label<slider>(&win, gold_->id());
295  bind_status_label<slider>(&win, support_->id());
296  bind_status_label<slider>(&win, experience_->id());
297 
298  bind_status_label<slider>(&win, init_turn_limit_->id());
299  bind_status_label<slider>(&win, turn_bonus_->id());
300  bind_status_label<slider>(&win, reservoir_->id());
301  bind_status_label<slider>(&win, action_bonus_->id());
302 
303  //
304  // Disable certain settings if we're playing a local game.
305  //
306 
307  if(local_mode_) {
308  find_widget<text_box>(&win, "game_name", false).set_active(false);
309  find_widget<text_box>(&win, "game_password", false).set_active(false);
310 
311  observers_->widget_set_enabled(win, false, false);
312  strict_sync_->widget_set_enabled(win, false, false);
313  private_replay_->widget_set_enabled(win, false, false);
314  }
315 
316  //
317  // Set up tab control
318  //
319  listbox& tab_bar = find_widget<listbox>(&win, "tab_bar", false);
320 
322  std::bind(&mp_create_game::on_tab_select, this));
323 
324  // Allow the settings stack to find widgets in all pages, regardless of which is selected.
325  // This ensures settings (especially game settings) widgets are appropriately updated when
326  // a new game is selected, regardless of which settings tab is active at the time.
327  find_widget<stacked_widget>(&win, "pager", false).set_find_in_all_layers(true);
328 
329  // We call on_tab_select farther down.
330 
331  //
332  // Main games list
333  //
334  listbox& list = find_widget<listbox>(&win, "games_list", false);
335 
337  std::bind(&mp_create_game::on_game_select, this));
338 
339  win.add_to_keyboard_chain(&list);
340 
341  // This handles the initial game selection as well
342  display_games_of_type(level_types_[get_initial_type_index()].first, preferences::level());
343 
344  // Initial tab selection must be done after game selection so the field widgets are set to their correct active state.
345  on_tab_select();
346 
347  //
348  // Set up the Lua plugin context
349  //
350  plugins_context_.reset(new plugins_context("Multiplayer Create"));
351 
352  plugins_context_->set_callback("create", [&win](const config&) { win.set_retval(retval::OK); }, false);
353  plugins_context_->set_callback("quit", [&win](const config&) { win.set_retval(retval::CANCEL); }, false);
354  plugins_context_->set_callback("load", [this](const config&) { load_game_callback(); }, false);
355 
356 #define UPDATE_ATTRIBUTE(field, convert) \
357  do { if(cfg.has_attribute(#field)) { field##_->set_widget_value(win, cfg[#field].convert()); } } while(false) \
358 
359  plugins_context_->set_callback("update_settings", [this, &win](const config& cfg) {
360  UPDATE_ATTRIBUTE(turns, to_int);
361  UPDATE_ATTRIBUTE(gold, to_int);
362  UPDATE_ATTRIBUTE(support, to_int);
363  UPDATE_ATTRIBUTE(experience, to_int);
364  UPDATE_ATTRIBUTE(start_time, to_bool);
365  UPDATE_ATTRIBUTE(fog, to_bool);
366  UPDATE_ATTRIBUTE(shroud, to_bool);
367  UPDATE_ATTRIBUTE(time_limit, to_bool);
368  UPDATE_ATTRIBUTE(init_turn_limit, to_int);
369  UPDATE_ATTRIBUTE(turn_bonus, to_int);
370  UPDATE_ATTRIBUTE(reservoir, to_int);
371  UPDATE_ATTRIBUTE(action_bonus, to_int);
372  UPDATE_ATTRIBUTE(observers, to_bool);
373  UPDATE_ATTRIBUTE(strict_sync, to_bool);
374  UPDATE_ATTRIBUTE(private_replay, to_bool);
376  }, true);
377 
378 #undef UPDATE_ATTRIBUTE
379 
380  plugins_context_->set_callback("set_name", [this](const config& cfg) {
381  config_engine_->set_game_name(cfg["name"]); }, true);
382 
383  plugins_context_->set_callback("set_password", [this](const config& cfg) {
384  config_engine_->set_game_password(cfg["password"]); }, true);
385 
386  plugins_context_->set_callback("select_level", [this](const config& cfg) {
387  selected_game_index_ = convert_to_game_filtered_index(cfg["index"].to_int());
389  }, true);
390 
391  plugins_context_->set_callback("select_type", [this](const config& cfg) {
392  create_engine_.set_current_level_type(ng::level::TYPE::string_to_enum(cfg["type"], ng::level::TYPE::SCENARIO)); }, true);
393 
394  plugins_context_->set_callback("select_era", [this](const config& cfg) {
395  create_engine_.set_current_era_index(cfg["index"].to_int()); }, true);
396 
397  plugins_context_->set_callback("select_mod", [this](const config& cfg) {
398  on_mod_toggle(cfg["index"].to_int(), nullptr);
399  }, true);
400 
401  plugins_context_->set_accessor("get_selected", [this](const config&) {
402  const ng::level& current_level = create_engine_.current_level();
403  return config {
404  "id", current_level.id(),
405  "name", current_level.name(),
406  "icon", current_level.icon(),
407  "description", current_level.description(),
408  "allow_era_choice", current_level.allow_era_choice(),
410  };
411  });
412 
413  plugins_context_->set_accessor("find_level", [this](const config& cfg) {
414  const std::string id = cfg["id"].str();
415  return config {
416  "index", create_engine_.find_level_by_id(id).second,
417  "type", create_engine_.find_level_by_id(id).first,
418  };
419  });
420 
421  plugins_context_->set_accessor_int("find_era", [this](const config& cfg) {
423  });
424 
425  plugins_context_->set_accessor_int("find_mod", [this](const config& cfg) {
427  });
428 }
429 
431 {
432  DBG_MP << "sync_with_depcheck: start\n";
433 
435  DBG_MP << "sync_with_depcheck: correcting era\n";
436  const int new_era_index = create_engine_.dependency_manager().get_era_index();
437 
438  create_engine_.set_current_era_index(new_era_index, true);
439  eras_menu_button_->set_value(new_era_index);
440  }
441 
443  DBG_MP << "sync_with_depcheck: correcting scenario\n";
444 
445  // Match scenario and scenario type
447  const bool different_type = new_level_index.first != create_engine_.current_level_type();
448 
449  if(new_level_index.second != -1) {
450  create_engine_.set_current_level_type(new_level_index.first);
451  create_engine_.set_current_level(new_level_index.second);
452  selected_game_index_ = new_level_index.second;
453 
454  auto& game_types_list = find_widget<menu_button>(get_window(), "game_types", false);
455  game_types_list.set_value(std::distance(level_types_.begin(), std::find_if(level_types_.begin(), level_types_.begin(), [&](const level_type_info& info){ return info.first == new_level_index.first; })));
456 
457  if(different_type) {
458  display_games_of_type(new_level_index.first, create_engine_.current_level().id());
459  } else {
460  // This function (or rather on_game_select) might be triggered by a listbox callback, in
461  // which case we cannot use display_games_of_type since it destroys the list (and its
462  // elements) which might result in segfaults. Instead, we assume that a listbox-triggered
463  // sync_with_depcheck call never changes the game type and goes to this branch instead.
464  find_widget<listbox>(get_window(), "games_list", false).select_row(new_level_index.second);
465 
466  // Override the last selection so on_game_select selects the new level
468 
469  on_game_select();
470  }
471  }
472  }
473 
475  DBG_MP << "sync_with_depcheck: correcting modifications\n";
477  }
478 
480  DBG_MP << "sync_with_depcheck: end\n";
481 }
482 
483 template<typename widget>
484 void mp_create_game::on_filter_change(const std::string& id, bool do_select)
485 {
486  create_engine_.apply_level_filter(find_widget<widget>(get_window(), id, false).get_value());
487 
488  listbox& game_list = find_widget<listbox>(get_window(), "games_list", false);
489 
490  boost::dynamic_bitset<> filtered(game_list.get_item_count());
492  filtered[i] = true;
493  }
494 
495  game_list.set_row_shown(filtered);
496 
497  if(do_select) {
498  on_game_select();
499  }
500 }
501 
503 {
504  const int selected_game = find_widget<listbox>(get_window(), "games_list", false).get_selected_row();
505 
506  if(selected_game == selected_game_index_) {
507  return;
508  }
509 
510  // Convert the absolute-index get_selected_row to a relative index for the create_engine to handle
512 
514 
516 
517  update_details();
518 
519  // General settings
520  const bool can_select_era = create_engine_.current_level().allow_era_choice();
521 
522  if(!can_select_era) {
523  eras_menu_button_->set_label(_("No eras available for this game."));
524  } else {
526  }
527 
528  eras_menu_button_->set_active(can_select_era);
529 
530  // Custom options
531  options_manager_->update_game_options();
532 
533  // Game settings
535 }
536 
538 {
539  const int i = find_widget<listbox>(get_window(), "tab_bar", false).get_selected_row();
540  find_widget<stacked_widget>(get_window(), "pager", false).select_layer(i);
541 }
542 
544 {
545  if(sender && (sender->get_value_bool() == create_engine_.dependency_manager().is_modification_active(index))) {
546  ERR_MP << "ignoring on_mod_toggle that is already set\n";
547  return;
548  }
549 
550  create_engine_.toggle_mod(index);
551 
553 
554  options_manager_->update_mod_options();
555 }
556 
558 {
560 
562 
564 
565  options_manager_->update_era_options();
566 }
567 
569 {
570  selected_rfm_index_ = find_widget<menu_button>(get_window(), "random_faction_mode", false).get_value();
571 }
572 
573 void mp_create_game::show_description(const std::string& new_description)
574 {
575  styled_widget& description = find_widget<styled_widget>(get_window(), "description", false);
576 
577  description.set_label(!new_description.empty() ? new_description : _("No description available."));
578  description.set_use_markup(true);
579 }
580 
582 {
583  const int index = find_widget<menu_button>(get_window(), "game_types", false).get_value();
584 
586 }
587 
589 {
591 
592  listbox& list = find_widget<listbox>(get_window(), "games_list", false);
593 
594  list.clear();
595 
596  for(const auto& game : create_engine_.get_levels_by_type_unfiltered(type)) {
597  std::map<std::string, string_map> data;
599 
600  if(type == ng::level::TYPE::CAMPAIGN || type == ng::level::TYPE::SP_CAMPAIGN) {
601  item["label"] = game->icon();
602  data.emplace("game_icon", item);
603  }
604 
605  item["label"] = game->name();
606  data.emplace("game_name", item);
607 
608  list.add_row(data);
609  }
610 
611  if(!level.empty() && !list.get_rows_shown().empty()) {
612  // Recalculate which rows should be visible
613  on_filter_change<slider>("num_players", false);
614  on_filter_change<text_box>("game_filter", false);
615 
616  int level_index = create_engine_.find_level_by_id(level).second;
617  if(level_index >= 0 && std::size_t(level_index) < list.get_item_count()) {
618  list.select_row(level_index);
619  }
620  }
621 
622  const bool is_random_map = type == ng::level::TYPE::RANDOM_MAP;
623 
624  find_widget<button>(get_window(), "random_map_regenerate", false).set_active(is_random_map);
625  find_widget<button>(get_window(), "random_map_settings", false).set_active(is_random_map);
626 
627  // Override the last selection so on_game_select selects the new level
629 
630  on_game_select();
631 }
632 
634 {
636 
638 }
639 
641 {
643 
644  update_details();
645 }
646 
647 int mp_create_game::convert_to_game_filtered_index(const unsigned int initial_index)
648 {
649  const std::vector<std::size_t>& filtered_indices = create_engine_.get_filtered_level_indices(create_engine_.current_level_type());
650  return std::distance(filtered_indices.begin(), std::find(filtered_indices.begin(), filtered_indices.end(), initial_index));
651 }
652 
654 {
655  styled_widget& players = find_widget<styled_widget>(get_window(), "map_num_players", false);
656  styled_widget& map_size = find_widget<styled_widget>(get_window(), "map_size", false);
657 
658  if(create_engine_.current_level_type() == ng::level::TYPE::RANDOM_MAP) {
659  // If the current random map doesn't have data, generate it
661  create_engine_.current_level().data()["map_data"].empty() &&
662  create_engine_.current_level().data()["map_file"].empty()) {
664  }
665 
666  find_widget<button>(get_window(), "random_map_settings", false).set_active(create_engine_.generator_has_settings());
667  }
668 
669  create_engine_.current_level().set_metadata();
670 
671  // Reset the config_engine with new values
673  config_engine_->update_initial_cfg(create_engine_.current_level().data());
674  config_engine_->set_default_values();
675 
676  // Set the title, with newlines replaced. Newlines are sometimes found in SP Campaign names
677  std::string title = create_engine_.current_level().name();
678  boost::replace_all(title, "\n", " " + font::unicode_em_dash + " ");
679  find_widget<styled_widget>(get_window(), "game_title", false).set_label(title);
680 
682 
683  switch(create_engine_.current_level_type().v) {
685  case ng::level::TYPE::USER_MAP:
686  case ng::level::TYPE::USER_SCENARIO:
687  case ng::level::TYPE::RANDOM_MAP: {
688  ng::scenario* current_scenario = dynamic_cast<ng::scenario*>(&create_engine_.current_level());
689 
690  assert(current_scenario);
691 
693 
694  find_widget<stacked_widget>(get_window(), "minimap_stack", false).select_layer(0);
695 
696  if(current_scenario->data()["map_data"].empty()) {
697  saved_game::expand_map_file(current_scenario->data());
698  current_scenario->set_metadata();
699  }
700 
701  find_widget<minimap>(get_window(), "minimap", false).set_map_data(current_scenario->data()["map_data"]);
702 
703  players.set_label(std::to_string(current_scenario->num_players()));
704  map_size.set_label(current_scenario->map_size());
705 
706  break;
707  }
708  case ng::level::TYPE::CAMPAIGN:
709  case ng::level::TYPE::SP_CAMPAIGN: {
710  ng::campaign* current_campaign = dynamic_cast<ng::campaign*>(&create_engine_.current_level());
711 
712  assert(current_campaign);
713 
714  create_engine_.get_state().classification().campaign = current_campaign->data()["id"].str();
715 
716  const std::string img = formatter() << current_campaign->data()["image"] << "~SCALE_INTO(265,265)";
717 
718  find_widget<stacked_widget>(get_window(), "minimap_stack", false).select_layer(1);
719  find_widget<image>(get_window(), "campaign_image", false).set_image(img);
720 
721  const int p_min = current_campaign->min_players();
722  const int p_max = current_campaign->max_players();
723 
724  if(p_max > p_min) {
725  players.set_label(VGETTEXT("number of players^$min to $max", {{"min", std::to_string(p_min)}, {"max", std::to_string(p_max)}}));
726  } else {
727  players.set_label(std::to_string(p_min));
728  }
729 
730  map_size.set_label(font::unicode_em_dash);
731 
732  break;
733  }
734  }
735 }
736 
738 {
739  window& window = *get_window();
740 
741  if(config_engine_->force_lock_settings()) {
742  use_map_settings_->widget_set_enabled(window, false, false);
743  use_map_settings_->set_widget_value(window, true);
744  } else {
745  use_map_settings_->widget_set_enabled(window, true, false);
746  }
747 
749 
750  config_engine_->set_use_map_settings(use_map_settings);
751 
752  fog_ ->widget_set_enabled(window, !use_map_settings, false);
753  shroud_ ->widget_set_enabled(window, !use_map_settings, false);
754  start_time_ ->widget_set_enabled(window, !use_map_settings, false);
755 
756  turns_ ->widget_set_enabled(window, !use_map_settings, false);
757  gold_ ->widget_set_enabled(window, !use_map_settings, false);
758  support_ ->widget_set_enabled(window, !use_map_settings, false);
759  experience_ ->widget_set_enabled(window, !use_map_settings, false);
760 
761  const bool time_limit = time_limit_->get_widget_value(window);
762 
763  init_turn_limit_->widget_set_enabled(window, time_limit, false);
764  turn_bonus_ ->widget_set_enabled(window, time_limit, false);
765  reservoir_ ->widget_set_enabled(window, time_limit, false);
766  action_bonus_ ->widget_set_enabled(window, time_limit, false);
767 
768  if(use_map_settings) {
769  fog_ ->set_widget_value(window, config_engine_->fog_game_default());
770  shroud_ ->set_widget_value(window, config_engine_->shroud_game_default());
771  start_time_->set_widget_value(window, config_engine_->random_start_time_default());
772 
773  turns_ ->set_widget_value(window, config_engine_->num_turns_default());
774  gold_ ->set_widget_value(window, config_engine_->village_gold_default());
775  support_ ->set_widget_value(window, config_engine_->village_support_default());
776  experience_->set_widget_value(window, config_engine_->xp_modifier_default());
777  }
778 }
779 
781 {
783 
784  if(!load.load_multiplayer_game()) {
785  return;
786  }
787 
788  if(load.data().cancel_orders) {
790  }
791 
792  set_retval(LOAD_GAME);
793 }
794 
795 std::vector<std::string> mp_create_game::get_active_mods()
796 {
797  int i = 0;
798  std::set<std::string> res;
800  if(find_widget<toggle_button>(mod_list_->get_row_grid(i), "mod_active_state", false).get_value_bool()) {
801  res.insert(mod->id);
802  }
803  ++i;
804  }
805  return std::vector<std::string>(res.begin(), res.end());
806 }
807 
808 void mp_create_game::set_active_mods(const std::vector<std::string>& val)
809 {
810  std::set<std::string> val2(val.begin(), val.end());
811  int i = 0;
812  std::set<std::string> res;
814  find_widget<toggle_button>(mod_list_->get_row_grid(i), "mod_active_state", false).set_value_bool(val2.find(mod->id) != val2.end());
815  ++i;
816  }
817 }
818 
820 {
822  gui2::show_transient_error_message(_("The selected game has no sides!"));
823  return false;
824  }
825 
827  std::stringstream msg;
828  // TRANSLATORS: This sentence will be followed by some details of the error, most likely the "Map could not be loaded" message from create_engine.cpp
829  msg << _("The selected game cannot be created.");
830  msg << "\n\n";
833  return false;
834  }
835 
836  if(!create_engine_.is_campaign()) {
837  return true;
838  }
839 
840  return create_engine_.select_campaign_difficulty() != "CANCEL";
841 }
842 
844 {
845  plugins_context_.reset();
846 
847  // Show all tabs so that find_widget works correctly
848  find_widget<stacked_widget>(&window, "pager", false).select_layer(-1);
849 
850  if(get_retval() == LOAD_GAME) {
852 
853  // We don't need the LOAD_GAME retval past this point. For convenience, reset it to OK so we can use the execute wrapper, then exit.
855  return;
856  }
857 
858  if(get_retval() == retval::OK) {
863 
865 
866  if(create_engine_.current_level_type() == ng::level::TYPE::CAMPAIGN ||
867  create_engine_.current_level_type() == ng::level::TYPE::SP_CAMPAIGN) {
871  } else {
872  // This means define= doesn't work for randomly generated scenarios
874  }
875 
877 
879 
880  std::vector<const config*> entry_points;
881  std::vector<std::string> entry_point_titles;
882 
883  const auto& tagname = create_engine_.get_state().classification().get_tagname();
884 
885  if(tagname == "scenario") {
886  const std::string first_scenario = create_engine_.current_level().data()["first_scenario"];
887  for(const config& scenario : game_config_manager::get()->game_config().child_range(tagname)) {
888  const bool is_first = scenario["id"] == first_scenario;
889  if(scenario["allow_new_game"].to_bool(false) || is_first || game_config::debug ) {
890  const std::string& title = !scenario["new_game_title"].empty()
891  ? scenario["new_game_title"]
892  : scenario["name"];
893 
894  entry_points.insert(is_first ? entry_points.begin() : entry_points.end(), &scenario);
895  entry_point_titles.insert(is_first ? entry_point_titles.begin() : entry_point_titles.end(), title);
896  }
897  }
898  }
899 
900  if(entry_points.size() > 1) {
901  gui2::dialogs::simple_item_selector dlg(_("Choose Starting Scenario"), _("Select at which point to begin this campaign."), entry_point_titles);
902 
903  dlg.set_single_button(true);
904  dlg.show();
905 
906  const config& scenario = *entry_points[dlg.selected_index()];
907 
908  create_engine_.get_state().mp_settings().hash = scenario.hash();
910  }
911 
912  config_engine_->set_use_map_settings(use_map_settings_->get_widget_value(window));
913 
914  if(!config_engine_->force_lock_settings()) {
915  // Max slider value (in this case, 100) means 'unlimited turns', so pass the value -1
916  const int num_turns = turns_->get_widget_value(window);
917  config_engine_->set_num_turns(num_turns < ::settings::turns_max ? num_turns : - 1);
918  config_engine_->set_village_gold(gold_->get_widget_value(window));
919  config_engine_->set_village_support(support_->get_widget_value(window));
920  config_engine_->set_xp_modifier(experience_->get_widget_value(window));
921  config_engine_->set_random_start_time(start_time_->get_widget_value(window));
922  config_engine_->set_fog_game(fog_->get_widget_value(window));
923  config_engine_->set_shroud_game(shroud_->get_widget_value(window));
924 
925  config_engine_->write_parameters();
926  }
927 
928  config_engine_->set_mp_countdown(time_limit_->get_widget_value(window));
929  config_engine_->set_mp_countdown_init_time(init_turn_limit_->get_widget_value(window));
930  config_engine_->set_mp_countdown_turn_bonus(turn_bonus_->get_widget_value(window));
931  config_engine_->set_mp_countdown_reservoir_time(reservoir_->get_widget_value(window));
932  config_engine_->set_mp_countdown_action_bonus(action_bonus_->get_widget_value(window));
933 
934  config_engine_->set_allow_observers(observers_->get_widget_value(window));
935  config_engine_->set_private_replay(private_replay_->get_widget_value(window));
936  config_engine_->set_oos_debug(strict_sync_->get_widget_value(window));
937  config_engine_->set_shuffle_sides(shuffle_sides_->get_widget_value(window));
938 
939  config_engine_->set_random_faction_mode(rfm_types_[selected_rfm_index_]);
940 
941  // Since we don't have a field handling this option, we need to save the value manually
942  prefs::set_random_faction_mode(mp_game_settings::RANDOM_FACTION_MODE::enum_to_string(rfm_types_[selected_rfm_index_]));
943 
944  // Save custom option settings
945  config_engine_->set_options(options_manager_->get_options_config());
946 
947  // Set game name
948  const std::string name = find_widget<text_box>(&window, "game_name", false).get_value();
949  if(!name.empty() && (name != ng::configure_engine::game_name_default())) {
950  config_engine_->set_game_name(name);
951  }
952 
953  // Set game password
954  const std::string password = find_widget<text_box>(&window, "game_password", false).get_value();
955  if(!password.empty()) {
956  config_engine_->set_game_password(password);
957  }
958  }
959 }
960 
961 } // namespace dialogs
962 } // namespace gui2
TYPE
Used to specify the rendering format of images.
Definition: picture.hpp:228
const std::string & get_scenario() const
Returns the selected scenario.
Definition: depcheck.hpp:121
Dialog was closed with the CANCEL button.
Definition: retval.hpp:37
std::string map_size() const
level::TYPE current_level_type() const
void set_value_bool(bool value, bool fire_event=false)
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
void set_selected(unsigned selected, bool fire_event=true)
void set_village_support(int value)
Definition: game.cpp:674
configure_engine
saved_game & get_state()
const int turns_max
maximum number of turns
void set_current_era_index(const std::size_t index, bool force=false)
std::string random_faction_mode()
Definition: game.cpp:479
The class for loading a savefile.
Definition: savegame.hpp:99
int village_support
Definition: game_config.cpp:55
void set_countdown_action_bonus(int value)
Definition: game.cpp:654
void set_countdown_turn_bonus(int value)
Definition: game.cpp:644
int min_players() const
std::string era()
Definition: game.cpp:689
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:64
void set_shroud(bool value)
Definition: game.cpp:548
std::vector< mp_game_settings::RANDOM_FACTION_MODE > rfm_types_
void set_countdown_init_time(int value)
Definition: game.cpp:624
bool generator_assigned() const
int xp_modifier()
Definition: game.cpp:679
std::vector< extras_metadata_ptr > & get_extras_by_type(const MP_EXTRA extra_type)
int get_era_index() const
Returns the selected era.
Definition: depcheck.cpp:397
int find_extra_by_id(const MP_EXTRA extra_type, const std::string &id) const
void set_scenario(config scenario)
Definition: saved_game.cpp:565
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:35
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
logger & info()
Definition: log.cpp:91
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
Definition: save_index.cpp:188
bool generator_has_settings() const
bool shuffle_sides()
Definition: game.cpp:469
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
int max_players() const
std::vector< level_type_info > level_types_
void display_games_of_type(ng::level::TYPE type, const std::string &level)
void set_current_level(const std::size_t index)
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:90
bool is_modification_active(int index) const
Tells whether a certain mod is activated.
Definition: depcheck.cpp:426
void on_mod_toggle(const int index, toggle_button *sender)
void set_random_faction_mode(const std::string &value)
Definition: game.cpp:484
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
T get_widget_value(window &window)
Gets the value of the field.
Definition: field.hpp:395
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
static std::string _(const char *str)
Definition: gettext.hpp:92
bool show(const unsigned auto_close_time=0)
Shows the window.
void set_countdown(bool value)
Definition: game.cpp:614
const mp_game_settings & get_parameters()
void prepare_for_era_and_mods()
bool is_campaign() const
Wrapper to simplify the is-type-campaign-or-sp-campaign check.
int countdown_init_time()
Definition: game.cpp:619
bool current_level_has_side_data()
Returns true if the current level has one or more [side] tags.
std::string dsgettext(const char *domainname, const char *msgid)
Definition: gettext.cpp:404
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:251
Class for a single line text area.
Definition: text_box.hpp:140
Generic file dialog.
Definition: field-fwd.hpp:22
std::string select_campaign_difficulty(int set_value=-1)
select_campaign_difficulty
virtual std::string id() const
const config & data() const
std::vector< std::string > get_active_mods()
bool fog()
Definition: game.cpp:533
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:42
void set_current_level_type(const level::TYPE type)
Base container class.
Definition: grid.hpp:30
static game_config_manager * get()
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:87
field_bool * use_map_settings_
All fields are also in the normal field vector, but they need to be manually controlled as well so ad...
int countdown_turn_bonus()
Definition: game.cpp:639
bool toggle_mod(int index, bool force=false)
void set_level_type(int value)
Definition: game.cpp:714
void set_widget_value(window &window, CT value)
Sets the value of the field.
Definition: field.hpp:358
static void expand_map_file(config &scenario)
reads scenario["map_file"]
Definition: saved_game.cpp:459
A simple one-column listbox with OK and Cancel buttons.
void set_tooltip(const t_string &tooltip)
std::string level()
Definition: game.cpp:699
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:186
#define DBG_MP
int num_players() const
virtual std::string name() const
void set_metadata()
const std::string & id() const
Definition: field.hpp:196
void prepare_for_campaign(const std::string &difficulty="")
std::ostringstream wrapper.
Definition: formatter.hpp:38
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:126
virtual void pre_show(window &window) override
Inherited from modal_dialog.
std::unique_ptr< plugins_context > plugins_context_
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:171
int countdown_reservoir_time()
Definition: game.cpp:629
void set_random_start_time(bool value)
Definition: game.cpp:528
static lg::log_domain log_mp_create("mp/create")
void set_values(const std::vector<::config > &values, unsigned selected=0)
Modify, read and display user preferences.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:132
virtual void set_use_markup(bool use_markup)
int village_gold()
Definition: game.cpp:659
std::string campaign
The id of the campaign being played.
#define ERR_MP
ng::create_engine create_engine_
General settings and defaults for scenarios.
virtual void post_show(window &window) override
Inherited from modal_dialog.
void set_active_mods(const std::vector< std::string > &val)
std::vector< std::string > & active_mods()
void apply_level_filter(const std::string &name)
void init_generated_level_data()
static const int LOAD_GAME
Various uncategorised dialogs.
std::pair< level::TYPE, int > find_level_by_id(const std::string &id) const
void set_level(const std::string &value)
Definition: game.cpp:704
#define UPDATE_ATTRIBUTE(field, convert)
static std::string game_name_default()
virtual std::string icon() const
void set_allow_observers(bool value)
Definition: game.cpp:464
std::pair< ng::level::TYPE, std::string > level_type_info
bool countdown()
Definition: game.cpp:609
bool shroud()
Definition: game.cpp:543
void widget_set_enabled(window &window, const bool enable, const bool sync)
Enables a widget.
Definition: field.hpp:170
std::size_t i
Definition: function.cpp:933
void on_filter_change(const std::string &id, bool do_select)
int selected_index() const
Returns the selected item index after displaying.
level & current_level() const
std::unique_ptr< mp_options_helper > options_manager_
int convert_to_game_filtered_index(const unsigned int initial_index)
Game configuration data as global variables.
Definition: build_info.cpp:56
The user set the widget invisible, that means:
std::string password(const std::string &server, const std::string &login)
std::string name
Definition: sdl_ttf.cpp:70
bool use_map_settings()
Definition: game.cpp:489
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:67
Declarations for File-IO.
int w
const bool & debug
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
Base class for all visible items.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
virtual std::string description() const
boost::dynamic_bitset get_rows_shown() const
Returns a list of visible rows.
Definition: listbox.cpp:222
std::size_t current_era_index() const
void set_turns(int value)
Definition: game.cpp:558
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:238
bool allow_observers()
Definition: game.cpp:459
void cancel_orders()
Definition: saved_game.cpp:684
void show_transient_error_message(const std::string &message, const std::string &image, const bool message_use_markup)
Shows a transient error message to the user.
int turns()
Definition: game.cpp:553
int get_retval() const
Returns the cached window exit code.
const std::vector< std::string > & modifications(bool mp)
Definition: game.cpp:719
const extras_metadata & current_era() const
const depcheck::manager & dependency_manager() const
game_classification & classification()
Definition: saved_game.hpp:54
void set_modifications(const std::vector< std::string > &value, bool mp)
Definition: game.cpp:728
const std::string unicode_em_dash
Definition: constants.cpp:40
const std::vector< std::string > & get_modifications() const
Returns the enabled modifications.
Definition: depcheck.hpp:128
Standard logging facilities (interface).
Base class for all level type classes.
std::unique_ptr< ng::configure_engine > config_engine_
const std::vector< extras_metadata_ptr > & get_const_extras_by_type(const MP_EXTRA extra_type) const
int countdown_action_bonus()
Definition: game.cpp:649
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:74
void set_era(const std::string &value)
Definition: game.cpp:694
void set_shuffle_sides(bool value)
Definition: game.cpp:474
void set_fog(bool value)
Definition: game.cpp:538
std::vector< std::size_t > get_filtered_level_indices(level::TYPE type) const
Dialog was closed with the OK button.
Definition: retval.hpp:34
bool dialog_exit_hook(window &)
Dialog exit hook to bring up the difficulty dialog when starting a campaign.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
void show_description(const std::string &new_description)
void set_countdown_reservoir_time(int value)
Definition: game.cpp:634
bool random_start_time()
Definition: game.cpp:523
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:58
void set_xp_modifier(int value)
Definition: game.cpp:684
void set_retval(int retval)
Convenience wrapper to set the window&#39;s exit code.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:144
std::string hash() const
Definition: config.cpp:1335
void set_village_gold(int value)
Definition: game.cpp:664
void set_use_map_settings(bool value)
Definition: game.cpp:494
bool empty() const
Definition: config.cpp:884
std::string get_tagname() const
Class for a toggle button.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:407
virtual bool allow_era_choice() const
virtual bool can_launch_game() const =0
void set_single_button(bool value)
Sets whether the Cancel button should be hidden or not.
int level_type()
Definition: game.cpp:709