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