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