The Battle for Wesnoth  1.15.12+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::dialogs
57 {
58 
59 // Special retval value for loading a game
60 static const int LOAD_GAME = 100;
61 
62 // Shorthand
63 namespace prefs = preferences;
64 
66 
67 mp_create_game::mp_create_game(saved_game& state, bool local_mode)
68  : create_engine_(state)
69  , config_engine_()
70  , options_manager_()
71  , selected_game_index_(-1)
72  , selected_rfm_index_(-1)
73  , use_map_settings_(register_bool( "use_map_settings", true, prefs::use_map_settings, prefs::set_use_map_settings,
74  std::bind(&mp_create_game::update_map_settings, this)))
75  , fog_(register_bool("fog", true, prefs::fog, prefs::set_fog))
76  , shroud_(register_bool("shroud", true, prefs::shroud, prefs::set_shroud))
77  , start_time_(register_bool("random_start_time", true, prefs::random_start_time, prefs::set_random_start_time))
78  , time_limit_(register_bool("time_limit", true, prefs::countdown, prefs::set_countdown,
79  std::bind(&mp_create_game::update_map_settings, this)))
80  , shuffle_sides_(register_bool("shuffle_sides", true, prefs::shuffle_sides, prefs::set_shuffle_sides))
81  , observers_(register_bool("observers", true, prefs::allow_observers, prefs::set_allow_observers))
82  , strict_sync_(register_bool("strict_sync", true))
83  , private_replay_(register_bool("private_replay", true))
84  , turns_(register_integer("turn_count", true, prefs::turns, prefs::set_turns))
85  , gold_(register_integer("village_gold", true, prefs::village_gold, prefs::set_village_gold))
86  , support_(register_integer("village_support", true, prefs::village_support, prefs::set_village_support))
87  , experience_(register_integer("experience_modifier", true, prefs::xp_modifier, prefs::set_xp_modifier))
88  , init_turn_limit_(register_integer("init_turn_limit", true, prefs::countdown_init_time, prefs::set_countdown_init_time))
89  , turn_bonus_(register_integer("turn_bonus", true, prefs::countdown_turn_bonus, prefs::set_countdown_turn_bonus))
90  , reservoir_(register_integer("reservoir", true, prefs::countdown_reservoir_time, prefs::set_countdown_reservoir_time))
91  , action_bonus_(register_integer("action_bonus", true, prefs::countdown_action_bonus, prefs::set_countdown_action_bonus))
92  , mod_list_()
93  , eras_menu_button_()
94  , local_mode_(local_mode)
95 {
96  level_types_ = {
97  {ng::level::TYPE::SCENARIO, _("Scenarios")},
98  {ng::level::TYPE::CAMPAIGN, _("Multiplayer Campaigns")},
99  {ng::level::TYPE::SP_CAMPAIGN, _("Singleplayer Campaigns")},
100  {ng::level::TYPE::USER_MAP, _("Custom Maps")},
101  {ng::level::TYPE::USER_SCENARIO, _("Custom Scenarios")},
102  {ng::level::TYPE::RANDOM_MAP, _("Random Maps")},
103  };
104 
105  level_types_.erase(std::remove_if(level_types_.begin(), level_types_.end(),
106  [this](level_type_info& type_info) {
107  return create_engine_.get_levels_by_type_unfiltered(type_info.first).empty();
108  }), level_types_.end());
109 
110  rfm_types_ = {
111  mp_game_settings::RANDOM_FACTION_MODE::DEFAULT,
112  mp_game_settings::RANDOM_FACTION_MODE::NO_MIRROR,
113  mp_game_settings::RANDOM_FACTION_MODE::NO_ALLY_MIRROR,
114  };
115 
116  set_show_even_without_video(true);
117 
118  create_engine_.init_active_mods();
119 
120  create_engine_.get_state().clear();
121  create_engine_.get_state().classification().campaign_type = game_classification::CAMPAIGN_TYPE::MULTIPLAYER;
122 
123  // Need to set this in the constructor, pre_show() is too late
124  set_allow_plugin_skip(false);
125 }
126 
128 {
129  find_widget<text_box>(&win, "game_name", false).set_value(local_mode_ ? "" : ng::configure_engine::game_name_default());
130 
132  find_widget<button>(&win, "random_map_regenerate", false),
133  std::bind(&mp_create_game::regenerate_random_map, this));
134 
136  find_widget<button>(&win, "random_map_settings", false),
137  std::bind(&mp_create_game::show_generator_settings, this));
138 
140  find_widget<button>(&win, "load_game", false),
141  std::bind(&mp_create_game::load_game_callback, this));
142 
143  // Custom dialog close hook
144  win.set_exit_hook_ok_only([this](window& w)->bool { return dialog_exit_hook(w); });
145 
146  //
147  // Set up the options manager. Needs to be done before selecting an initial tab
148  //
150 
151  //
152  // Set up filtering
153  //
154  connect_signal_notify_modified(find_widget<slider>(&win, "num_players", false),
155  std::bind(&mp_create_game::on_filter_change<slider>, this, "num_players", true));
156 
157  text_box& filter = find_widget<text_box>(&win, "game_filter", false);
158 
160  std::bind(&mp_create_game::on_filter_change<text_box>, this, "game_filter", true));
161 
162  // Note this cannot be in the keyboard chain or it will capture focus from other text boxes
163  win.keyboard_capture(&filter);
164 
165  //
166  // Set up game types menu_button
167  //
168  std::vector<config> game_types;
169  for(level_type_info& type_info : level_types_) {
170  game_types.emplace_back("label", type_info.second);
171  }
172 
173  if(game_types.empty()) {
174  gui2::show_transient_message("", _("No games found."));
175  throw game::error(_("No games found."));
176  }
177 
178  menu_button& game_menu_button = find_widget<menu_button>(&win, "game_types", false);
179 
180  // Helper to make sure the initially selected level type is valid
181  auto get_initial_type_index = [this]()->int {
182  const auto index = std::find_if(level_types_.begin(), level_types_.end(), [](level_type_info& info) {
183  return info.first == ng::level::TYPE::from_int(preferences::level_type());
184  });
185 
186  if(index != level_types_.end()) {
187  return std::distance(level_types_.begin(), index);
188  }
189 
190  return 0;
191  };
192 
193  game_menu_button.set_values(game_types, get_initial_type_index());
194 
195  connect_signal_notify_modified(game_menu_button,
196  std::bind(&mp_create_game::update_games_list, this));
197 
198  //
199  // Set up mods list
200  //
201  mod_list_ = &find_widget<listbox>(&win, "mod_list", false);
202 
203  const auto& activemods = preferences::modifications();
205  std::map<std::string, string_map> data;
207 
208  item["label"] = mod->name;
209  data.emplace("mod_name", item);
210 
211  grid* row_grid = &mod_list_->add_row(data);
212 
213  find_widget<toggle_panel>(row_grid, "panel", false).set_tooltip(mod->description);
214 
215  toggle_button& mog_toggle = find_widget<toggle_button>(row_grid, "mod_active_state", false);
216 
217  const int i = mod_list_->get_item_count() - 1;
218  if(std::find(activemods.begin(), activemods.end(), mod->id) != activemods.end()) {
219  create_engine_.active_mods().push_back(mod->id);
220  mog_toggle.set_value_bool(true);
221  }
222 
223  connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, i, &mog_toggle));
224  }
225 
226  // No mods, hide the header
227  if(mod_list_->get_item_count() <= 0) {
228  find_widget<styled_widget>(&win, "mods_header", false).set_visible(widget::visibility::invisible);
229  }
230 
231  //
232  // Set up eras menu_button
233  //
234  eras_menu_button_ = &find_widget<menu_button>(&win, "eras", false);
235 
236  std::vector<config> era_names;
238  era_names.emplace_back("label", era->name, "tooltip", era->description);
239  }
240 
241  if(era_names.empty()) {
242  gui2::show_transient_message("", _("No eras found."));
243  throw config::error(_("No eras found"));
244  }
245 
246  eras_menu_button_->set_values(era_names);
247 
249  std::bind(&mp_create_game::on_era_select, this));
250 
252  if(era_selection >= 0) {
253  eras_menu_button_->set_selected(era_selection);
254  }
255 
256  on_era_select();
257 
258  //
259  // Set up random faction mode menu_button
260  //
261  std::vector<config> rfm_options;
262  for(const auto& type : rfm_types_) {
263  // HACK: The labels are defined for the wesnoth textdomain in a header,
264  // see mp_game_settings::RANDOM_FACTION_MODE in src/mp_game_settings.hpp
265  rfm_options.emplace_back("label",
266  translation::dsgettext("wesnoth", mp_game_settings::RANDOM_FACTION_MODE::enum_to_string(type).c_str())
267  );
268  };
269 
270  // Manually insert tooltips. Need to find a better way to do this
271  rfm_options[0]["tooltip"] = _("Independent: Random factions assigned independently");
272  rfm_options[1]["tooltip"] = _("No Mirror: No two players will get the same faction");
273  rfm_options[2]["tooltip"] = _("No Ally Mirror: No two allied players will get the same faction");
274 
275  const int initial_index = std::distance(rfm_types_.begin(), std::find(rfm_types_.begin(), rfm_types_.end(),
276  mp_game_settings::RANDOM_FACTION_MODE::string_to_enum(prefs::random_faction_mode(), mp_game_settings::RANDOM_FACTION_MODE::DEFAULT))
277  );
278 
279  menu_button& rfm_menu_button = find_widget<menu_button>(&win, "random_faction_mode", false);
280 
281  rfm_menu_button.set_values(rfm_options, initial_index);
282 
283  connect_signal_notify_modified(rfm_menu_button,
285 
287 
288  //
289  // Set up the setting status labels
290  //
291  bind_status_label<slider>(&win, turns_->id());
292  bind_status_label<slider>(&win, gold_->id());
293  bind_status_label<slider>(&win, support_->id());
294  bind_status_label<slider>(&win, experience_->id());
295 
296  bind_status_label<slider>(&win, init_turn_limit_->id());
297  bind_status_label<slider>(&win, turn_bonus_->id());
298  bind_status_label<slider>(&win, reservoir_->id());
299  bind_status_label<slider>(&win, action_bonus_->id());
300 
301  //
302  // Disable certain settings if we're playing a local game.
303  //
304 
305  if(local_mode_) {
306  find_widget<text_box>(&win, "game_name", false).set_active(false);
307  find_widget<text_box>(&win, "game_password", false).set_active(false);
308 
309  observers_->widget_set_enabled(win, false, false);
310  strict_sync_->widget_set_enabled(win, false, false);
311  private_replay_->widget_set_enabled(win, false, false);
312  }
313 
314  //
315  // Set up tab control
316  //
317  listbox& tab_bar = find_widget<listbox>(&win, "tab_bar", false);
318 
320  std::bind(&mp_create_game::on_tab_select, this));
321 
322  // Allow the settings stack to find widgets in all pages, regardless of which is selected.
323  // This ensures settings (especially game settings) widgets are appropriately updated when
324  // a new game is selected, regardless of which settings tab is active at the time.
325  find_widget<stacked_widget>(&win, "pager", false).set_find_in_all_layers(true);
326 
327  // We call on_tab_select farther down.
328 
329  //
330  // Main games list
331  //
332  listbox& list = find_widget<listbox>(&win, "games_list", false);
333 
335  std::bind(&mp_create_game::on_game_select, this));
336 
337  win.add_to_keyboard_chain(&list);
338 
339  // This handles the initial game selection as well
340  display_games_of_type(level_types_[get_initial_type_index()].first, preferences::level());
341 
342  // Initial tab selection must be done after game selection so the field widgets are set to their correct active state.
343  on_tab_select();
344 
345  //
346  // Set up the Lua plugin context
347  //
348  plugins_context_.reset(new plugins_context("Multiplayer Create"));
349 
350  plugins_context_->set_callback("create", [&win](const config&) { win.set_retval(retval::OK); }, false);
351  plugins_context_->set_callback("quit", [&win](const config&) { win.set_retval(retval::CANCEL); }, false);
352  plugins_context_->set_callback("load", [this](const config&) { load_game_callback(); }, false);
353 
354 #define UPDATE_ATTRIBUTE(field, convert) \
355  do { if(cfg.has_attribute(#field)) { field##_->set_widget_value(win, cfg[#field].convert()); } } while(false) \
356 
357  plugins_context_->set_callback("update_settings", [this, &win](const config& cfg) {
358  UPDATE_ATTRIBUTE(turns, to_int);
359  UPDATE_ATTRIBUTE(gold, to_int);
360  UPDATE_ATTRIBUTE(support, to_int);
361  UPDATE_ATTRIBUTE(experience, to_int);
362  UPDATE_ATTRIBUTE(start_time, to_bool);
363  UPDATE_ATTRIBUTE(fog, to_bool);
364  UPDATE_ATTRIBUTE(shroud, to_bool);
365  UPDATE_ATTRIBUTE(time_limit, to_bool);
366  UPDATE_ATTRIBUTE(init_turn_limit, to_int);
367  UPDATE_ATTRIBUTE(turn_bonus, to_int);
368  UPDATE_ATTRIBUTE(reservoir, to_int);
369  UPDATE_ATTRIBUTE(action_bonus, to_int);
370  UPDATE_ATTRIBUTE(observers, to_bool);
371  UPDATE_ATTRIBUTE(strict_sync, to_bool);
372  UPDATE_ATTRIBUTE(private_replay, to_bool);
374  }, true);
375 
376 #undef UPDATE_ATTRIBUTE
377 
378  plugins_context_->set_callback("set_name", [this](const config& cfg) {
379  config_engine_->set_game_name(cfg["name"]); }, true);
380 
381  plugins_context_->set_callback("set_password", [this](const config& cfg) {
382  config_engine_->set_game_password(cfg["password"]); }, true);
383 
384  plugins_context_->set_callback("select_level", [this](const config& cfg) {
385  selected_game_index_ = convert_to_game_filtered_index(cfg["index"].to_int());
387  }, true);
388 
389  plugins_context_->set_callback("select_type", [this](const config& cfg) {
390  create_engine_.set_current_level_type(ng::level::TYPE::string_to_enum(cfg["type"], ng::level::TYPE::SCENARIO)); }, true);
391 
392  plugins_context_->set_callback("select_era", [this](const config& cfg) {
393  create_engine_.set_current_era_index(cfg["index"].to_int()); }, true);
394 
395  plugins_context_->set_callback("select_mod", [this](const config& cfg) {
396  on_mod_toggle(cfg["index"].to_int(), nullptr);
397  }, true);
398 
399  plugins_context_->set_accessor("get_selected", [this](const config&) {
400  const ng::level& current_level = create_engine_.current_level();
401  return config {
402  "id", current_level.id(),
403  "name", current_level.name(),
404  "icon", current_level.icon(),
405  "description", current_level.description(),
406  "allow_era_choice", current_level.allow_era_choice(),
408  };
409  });
410 
411  plugins_context_->set_accessor("find_level", [this](const config& cfg) {
412  const std::string id = cfg["id"].str();
413  return config {
414  "index", create_engine_.find_level_by_id(id).second,
415  "type", create_engine_.find_level_by_id(id).first,
416  };
417  });
418 
419  plugins_context_->set_accessor_int("find_era", [this](const config& cfg) {
421  });
422 
423  plugins_context_->set_accessor_int("find_mod", [this](const config& cfg) {
425  });
426 }
427 
429 {
430  DBG_MP << "sync_with_depcheck: start\n";
431 
433  DBG_MP << "sync_with_depcheck: correcting era\n";
434  const int new_era_index = create_engine_.dependency_manager().get_era_index();
435 
436  create_engine_.set_current_era_index(new_era_index, true);
437  eras_menu_button_->set_value(new_era_index);
438  }
439 
441  DBG_MP << "sync_with_depcheck: correcting scenario\n";
442 
443  // Match scenario and scenario type
445  const bool different_type = new_level_index.first != create_engine_.current_level_type();
446 
447  if(new_level_index.second != -1) {
448  create_engine_.set_current_level_type(new_level_index.first);
449  create_engine_.set_current_level(new_level_index.second);
450  selected_game_index_ = new_level_index.second;
451 
452  auto& game_types_list = find_widget<menu_button>(get_window(), "game_types", false);
453  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; })));
454 
455  if(different_type) {
456  display_games_of_type(new_level_index.first, create_engine_.current_level().id());
457  } else {
458  // This function (or rather on_game_select) might be triggered by a listbox callback, in
459  // which case we cannot use display_games_of_type since it destroys the list (and its
460  // elements) which might result in segfaults. Instead, we assume that a listbox-triggered
461  // sync_with_depcheck call never changes the game type and goes to this branch instead.
462  find_widget<listbox>(get_window(), "games_list", false).select_row(new_level_index.second);
463 
464  // Override the last selection so on_game_select selects the new level
466 
467  on_game_select();
468  }
469  }
470  }
471 
473  DBG_MP << "sync_with_depcheck: correcting modifications\n";
475  }
476 
478  DBG_MP << "sync_with_depcheck: end\n";
479 }
480 
481 template<typename widget>
482 void mp_create_game::on_filter_change(const std::string& id, bool do_select)
483 {
484  create_engine_.apply_level_filter(find_widget<widget>(get_window(), id, false).get_value());
485 
486  listbox& game_list = find_widget<listbox>(get_window(), "games_list", false);
487 
488  boost::dynamic_bitset<> filtered(game_list.get_item_count());
490  filtered[i] = true;
491  }
492 
493  game_list.set_row_shown(filtered);
494 
495  if(do_select) {
496  on_game_select();
497  }
498 }
499 
501 {
502  const int selected_game = find_widget<listbox>(get_window(), "games_list", false).get_selected_row();
503 
504  if(selected_game == selected_game_index_) {
505  return;
506  }
507 
508  // Convert the absolute-index get_selected_row to a relative index for the create_engine to handle
510 
512 
514 
515  update_details();
516 
517  // General settings
518  const bool can_select_era = create_engine_.current_level().allow_era_choice();
519 
520  if(!can_select_era) {
521  eras_menu_button_->set_label(_("No eras available for this game."));
522  } else {
524  }
525 
526  eras_menu_button_->set_active(can_select_era);
527 
528  // Custom options
529  options_manager_->update_game_options();
530 
531  // Game settings
533 }
534 
536 {
537  const int i = find_widget<listbox>(get_window(), "tab_bar", false).get_selected_row();
538  find_widget<stacked_widget>(get_window(), "pager", false).select_layer(i);
539 }
540 
542 {
543  if(sender && (sender->get_value_bool() == create_engine_.dependency_manager().is_modification_active(index))) {
544  ERR_MP << "ignoring on_mod_toggle that is already set\n";
545  return;
546  }
547 
548  create_engine_.toggle_mod(index);
549 
551 
552  options_manager_->update_mod_options();
553 }
554 
556 {
558 
560 
562 
563  options_manager_->update_era_options();
564 }
565 
567 {
568  selected_rfm_index_ = find_widget<menu_button>(get_window(), "random_faction_mode", false).get_value();
569 }
570 
571 void mp_create_game::show_description(const std::string& new_description)
572 {
573  styled_widget& description = find_widget<styled_widget>(get_window(), "description", false);
574 
575  description.set_label(!new_description.empty() ? new_description : _("No description available."));
576  description.set_use_markup(true);
577 }
578 
580 {
581  const int index = find_widget<menu_button>(get_window(), "game_types", false).get_value();
582 
584 }
585 
587 {
589 
590  listbox& list = find_widget<listbox>(get_window(), "games_list", false);
591 
592  list.clear();
593 
594  for(const auto& game : create_engine_.get_levels_by_type_unfiltered(type)) {
595  std::map<std::string, string_map> data;
597 
598  if(type == ng::level::TYPE::CAMPAIGN || type == ng::level::TYPE::SP_CAMPAIGN) {
599  item["label"] = game->icon();
600  data.emplace("game_icon", item);
601  }
602 
603  item["label"] = game->name();
604  data.emplace("game_name", item);
605 
606  list.add_row(data);
607  }
608 
609  if(!level.empty() && !list.get_rows_shown().empty()) {
610  // Recalculate which rows should be visible
611  on_filter_change<slider>("num_players", false);
612  on_filter_change<text_box>("game_filter", false);
613 
614  int level_index = create_engine_.find_level_by_id(level).second;
615  if(level_index >= 0 && std::size_t(level_index) < list.get_item_count()) {
616  list.select_row(level_index);
617  }
618  }
619 
620  const bool is_random_map = type == ng::level::TYPE::RANDOM_MAP;
621 
622  find_widget<button>(get_window(), "random_map_regenerate", false).set_active(is_random_map);
623  find_widget<button>(get_window(), "random_map_settings", false).set_active(is_random_map);
624 
625  // Override the last selection so on_game_select selects the new level
627 
628  on_game_select();
629 }
630 
632 {
634 
636 }
637 
639 {
641 
642  update_details();
643 }
644 
645 int mp_create_game::convert_to_game_filtered_index(const unsigned int initial_index)
646 {
647  const std::vector<std::size_t>& filtered_indices = create_engine_.get_filtered_level_indices(create_engine_.current_level_type());
648  return std::distance(filtered_indices.begin(), std::find(filtered_indices.begin(), filtered_indices.end(), initial_index));
649 }
650 
652 {
653  styled_widget& players = find_widget<styled_widget>(get_window(), "map_num_players", false);
654  styled_widget& map_size = find_widget<styled_widget>(get_window(), "map_size", false);
655 
656  if(create_engine_.current_level_type() == ng::level::TYPE::RANDOM_MAP) {
657  // If the current random map doesn't have data, generate it
659  create_engine_.current_level().data()["map_data"].empty() &&
660  create_engine_.current_level().data()["map_file"].empty()) {
662  }
663 
664  find_widget<button>(get_window(), "random_map_settings", false).set_active(create_engine_.generator_has_settings());
665  }
666 
667  create_engine_.current_level().set_metadata();
668 
669  // Reset the config_engine with new values
671  config_engine_->update_initial_cfg(create_engine_.current_level().data());
672  config_engine_->set_default_values();
673 
674  // Set the title, with newlines replaced. Newlines are sometimes found in SP Campaign names
675  std::string title = create_engine_.current_level().name();
676  boost::replace_all(title, "\n", " " + font::unicode_em_dash + " ");
677  find_widget<styled_widget>(get_window(), "game_title", false).set_label(title);
678 
680 
681  switch(create_engine_.current_level_type().v) {
683  case ng::level::TYPE::USER_MAP:
684  case ng::level::TYPE::USER_SCENARIO:
685  case ng::level::TYPE::RANDOM_MAP: {
686  ng::scenario* current_scenario = dynamic_cast<ng::scenario*>(&create_engine_.current_level());
687 
688  assert(current_scenario);
689 
691 
692  find_widget<stacked_widget>(get_window(), "minimap_stack", false).select_layer(0);
693 
694  if(current_scenario->data()["map_data"].empty()) {
695  saved_game::expand_map_file(current_scenario->data());
696  current_scenario->set_metadata();
697  }
698 
699  find_widget<minimap>(get_window(), "minimap", false).set_map_data(current_scenario->data()["map_data"]);
700 
701  players.set_label(std::to_string(current_scenario->num_players()));
702  map_size.set_label(current_scenario->map_size());
703 
704  break;
705  }
706  case ng::level::TYPE::CAMPAIGN:
707  case ng::level::TYPE::SP_CAMPAIGN: {
708  ng::campaign* current_campaign = dynamic_cast<ng::campaign*>(&create_engine_.current_level());
709 
710  assert(current_campaign);
711 
712  create_engine_.get_state().classification().campaign = current_campaign->data()["id"].str();
713 
714  const std::string img = formatter() << current_campaign->data()["image"] << "~SCALE_INTO(265,265)";
715 
716  find_widget<stacked_widget>(get_window(), "minimap_stack", false).select_layer(1);
717  find_widget<image>(get_window(), "campaign_image", false).set_image(img);
718 
719  const int p_min = current_campaign->min_players();
720  const int p_max = current_campaign->max_players();
721 
722  if(p_max > p_min) {
723  players.set_label(VGETTEXT("number of players^$min to $max", {{"min", std::to_string(p_min)}, {"max", std::to_string(p_max)}}));
724  } else {
725  players.set_label(std::to_string(p_min));
726  }
727 
728  map_size.set_label(font::unicode_em_dash);
729 
730  break;
731  }
732  }
733 }
734 
736 {
737  window& window = *get_window();
738 
739  if(config_engine_->force_lock_settings()) {
740  use_map_settings_->widget_set_enabled(window, false, false);
741  use_map_settings_->set_widget_value(window, true);
742  } else {
743  use_map_settings_->widget_set_enabled(window, true, false);
744  }
745 
747 
748  config_engine_->set_use_map_settings(use_map_settings);
749 
750  fog_ ->widget_set_enabled(window, !use_map_settings, false);
751  shroud_ ->widget_set_enabled(window, !use_map_settings, false);
752  start_time_ ->widget_set_enabled(window, !use_map_settings, false);
753 
754  turns_ ->widget_set_enabled(window, !use_map_settings, false);
755  gold_ ->widget_set_enabled(window, !use_map_settings, false);
756  support_ ->widget_set_enabled(window, !use_map_settings, false);
757  experience_ ->widget_set_enabled(window, !use_map_settings, false);
758 
759  const bool time_limit = time_limit_->get_widget_value(window);
760 
761  init_turn_limit_->widget_set_enabled(window, time_limit, false);
762  turn_bonus_ ->widget_set_enabled(window, time_limit, false);
763  reservoir_ ->widget_set_enabled(window, time_limit, false);
764  action_bonus_ ->widget_set_enabled(window, time_limit, false);
765 
766  if(use_map_settings) {
767  fog_ ->set_widget_value(window, config_engine_->fog_game_default());
768  shroud_ ->set_widget_value(window, config_engine_->shroud_game_default());
769  start_time_->set_widget_value(window, config_engine_->random_start_time_default());
770 
771  turns_ ->set_widget_value(window, config_engine_->num_turns_default());
772  gold_ ->set_widget_value(window, config_engine_->village_gold_default());
773  support_ ->set_widget_value(window, config_engine_->village_support_default());
774  experience_->set_widget_value(window, config_engine_->xp_modifier_default());
775  }
776 }
777 
779 {
781 
782  if(!load.load_multiplayer_game()) {
783  return;
784  }
785 
786  if(load.data().cancel_orders) {
788  }
789 
790  set_retval(LOAD_GAME);
791 }
792 
793 std::vector<std::string> mp_create_game::get_active_mods()
794 {
795  int i = 0;
796  std::set<std::string> res;
798  if(find_widget<toggle_button>(mod_list_->get_row_grid(i), "mod_active_state", false).get_value_bool()) {
799  res.insert(mod->id);
800  }
801  ++i;
802  }
803  return std::vector<std::string>(res.begin(), res.end());
804 }
805 
806 void mp_create_game::set_active_mods(const std::vector<std::string>& val)
807 {
808  std::set<std::string> val2(val.begin(), val.end());
809  int i = 0;
810  std::set<std::string> res;
812  find_widget<toggle_button>(mod_list_->get_row_grid(i), "mod_active_state", false).set_value_bool(val2.find(mod->id) != val2.end());
813  ++i;
814  }
815 }
816 
818 {
820  gui2::show_transient_error_message(_("The selected game has no sides!"));
821  return false;
822  }
823 
825  std::stringstream msg;
826  // 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
827  msg << _("The selected game cannot be created.");
828  msg << "\n\n";
831  return false;
832  }
833 
834  if(!create_engine_.is_campaign()) {
835  return true;
836  }
837 
838  return create_engine_.select_campaign_difficulty() != "CANCEL";
839 }
840 
842 {
843  plugins_context_.reset();
844 
845  // Show all tabs so that find_widget works correctly
846  find_widget<stacked_widget>(&window, "pager", false).select_layer(-1);
847 
848  if(get_retval() == LOAD_GAME) {
850 
851  // 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.
853  return;
854  }
855 
856  if(get_retval() == retval::OK) {
861 
863 
864  if(create_engine_.current_level_type() == ng::level::TYPE::CAMPAIGN ||
865  create_engine_.current_level_type() == ng::level::TYPE::SP_CAMPAIGN) {
869  } else {
870  // This means define= doesn't work for randomly generated scenarios
872  }
873 
875 
877 
878  std::vector<const config*> entry_points;
879  std::vector<std::string> entry_point_titles;
880 
881  const auto& tagname = create_engine_.get_state().classification().get_tagname();
882 
883  if(tagname == "scenario") {
884  const std::string first_scenario = create_engine_.current_level().data()["first_scenario"];
885  for(const config& scenario : game_config_manager::get()->game_config().child_range(tagname)) {
886  const bool is_first = scenario["id"] == first_scenario;
887  if(scenario["allow_new_game"].to_bool(false) || is_first || game_config::debug ) {
888  const std::string& title = !scenario["new_game_title"].empty()
889  ? scenario["new_game_title"]
890  : scenario["name"];
891 
892  entry_points.insert(is_first ? entry_points.begin() : entry_points.end(), &scenario);
893  entry_point_titles.insert(is_first ? entry_point_titles.begin() : entry_point_titles.end(), title);
894  }
895  }
896  }
897 
898  if(entry_points.size() > 1) {
899  gui2::dialogs::simple_item_selector dlg(_("Choose Starting Scenario"), _("Select at which point to begin this campaign."), entry_point_titles);
900 
901  dlg.set_single_button(true);
902  dlg.show();
903 
904  const config& scenario = *entry_points[dlg.selected_index()];
905 
906  create_engine_.get_state().mp_settings().hash = scenario.hash();
908  }
909 
910  config_engine_->set_use_map_settings(use_map_settings_->get_widget_value(window));
911 
912  if(!config_engine_->force_lock_settings()) {
913  // Max slider value (in this case, 100) means 'unlimited turns', so pass the value -1
914  const int num_turns = turns_->get_widget_value(window);
915  config_engine_->set_num_turns(num_turns < ::settings::turns_max ? num_turns : - 1);
916  config_engine_->set_village_gold(gold_->get_widget_value(window));
917  config_engine_->set_village_support(support_->get_widget_value(window));
918  config_engine_->set_xp_modifier(experience_->get_widget_value(window));
919  config_engine_->set_random_start_time(start_time_->get_widget_value(window));
920  config_engine_->set_fog_game(fog_->get_widget_value(window));
921  config_engine_->set_shroud_game(shroud_->get_widget_value(window));
922 
923  config_engine_->write_parameters();
924  }
925 
926  config_engine_->set_mp_countdown(time_limit_->get_widget_value(window));
927  config_engine_->set_mp_countdown_init_time(init_turn_limit_->get_widget_value(window));
928  config_engine_->set_mp_countdown_turn_bonus(turn_bonus_->get_widget_value(window));
929  config_engine_->set_mp_countdown_reservoir_time(reservoir_->get_widget_value(window));
930  config_engine_->set_mp_countdown_action_bonus(action_bonus_->get_widget_value(window));
931 
932  config_engine_->set_allow_observers(observers_->get_widget_value(window));
933  config_engine_->set_private_replay(private_replay_->get_widget_value(window));
934  config_engine_->set_oos_debug(strict_sync_->get_widget_value(window));
935  config_engine_->set_shuffle_sides(shuffle_sides_->get_widget_value(window));
936 
937  config_engine_->set_random_faction_mode(rfm_types_[selected_rfm_index_]);
938 
939  // Since we don't have a field handling this option, we need to save the value manually
940  prefs::set_random_faction_mode(mp_game_settings::RANDOM_FACTION_MODE::enum_to_string(rfm_types_[selected_rfm_index_]));
941 
942  // Save custom option settings
943  config_engine_->set_options(options_manager_->get_options_config());
944 
945  // Set game name
946  const std::string name = find_widget<text_box>(&window, "game_name", false).get_value();
947  if(!name.empty() && (name != ng::configure_engine::game_name_default())) {
948  config_engine_->set_game_name(name);
949  }
950 
951  // Set game password
952  const std::string password = find_widget<text_box>(&window, "game_password", false).get_value();
953  if(!password.empty()) {
954  config_engine_->set_game_password(password);
955  }
956  }
957 }
958 
959 } // namespace dialogs
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:54
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:60
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:40
logger & info()
Definition: log.cpp:88
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:210
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:86
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:402
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
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:83
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
Actions to be taken before showing the window.
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
Actions to be taken after the window has been shown.
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
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:940
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:58
The user set the widget invisible, that means:
std::string password(const std::string &server, const std::string &login)
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:43
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:59
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:1367
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:916
std::string get_tagname() const
Class for a toggle button.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409
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