The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
singleplayer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
15 #include "config.hpp"
16 #include "game_config_manager.hpp"
18 #include "gui/dialogs/message.hpp"
21 #include "gui/widgets/window.hpp"
22 #include "wml_exception.hpp"
23 
24 static lg::log_domain log_engine("engine");
25 #define ERR_NG LOG_STREAM(err, log_engine)
26 
27 namespace sp {
28 
29 bool enter_create_mode(CVideo& video, const config& game_config, saved_game& state, jump_to_campaign_info jump_to_campaign)
30 {
31  bool configure_canceled = false;
32 
33  do {
34  ng::create_engine create_eng(video, state);
35 
36  create_eng.set_current_level_type(ng::level::TYPE::SP_CAMPAIGN);
37 
38  const std::vector<ng::create_engine::level_ptr> campaigns =
39  create_eng.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
40 
41  if(campaigns.empty()) {
42  gui2::show_error_message(video, _("No campaigns are available."));
43  return false;
44  }
45 
46  std::string random_mode = "";
47 
48  // No campaign selected from command line
49  if(jump_to_campaign.campaign_id_.empty()) {
50  gui2::dialogs::campaign_selection dlg(create_eng);
51 
52  try {
53  dlg.show(video);
54  } catch(wml_exception& e) {
55  e.show(video);
56  return false;
57  }
58 
59  if(dlg.get_retval() != gui2::window::OK) {
60  return false;
61  }
62 
63  if(dlg.get_deterministic()) {
64  random_mode = "deterministic";
65  }
66  } else {
67  // Don't reset the campaign_id_ so we can know
68  // if we should quit the game or return to the main menu
69 
70  // Checking for valid campaign name
71  const auto campaign = std::find_if(campaigns.begin(), campaigns.end(), [&jump_to_campaign](ng::create_engine::level_ptr level) {
72  return level->data()["id"] == jump_to_campaign.campaign_id_;
73  });
74 
75  // Didn't find a campaign with that id
76  if(campaign == campaigns.end()) {
77  ERR_NG << "No such campaign id to jump to: [" << jump_to_campaign.campaign_id_ << "]" << std::endl;
78  return false;
79  }
80 
81  create_eng.set_current_level(campaign - campaigns.begin());
82  }
83 
84  state.classification().random_mode = random_mode;
85 
86  const std::string selected_difficulty = create_eng.select_campaign_difficulty(jump_to_campaign.difficulty_);
87 
88  if(selected_difficulty == "FAIL") return false;
89  if(selected_difficulty == "CANCEL") {
90  if(!jump_to_campaign.campaign_id_.empty()) {
91  jump_to_campaign.campaign_id_ = "";
92  }
93 
94  // Canceled difficulty dialog, relaunch the campaign selection dialog
95  return enter_create_mode(video, game_config, state, jump_to_campaign);
96  }
97 
98  create_eng.prepare_for_era_and_mods();
99  create_eng.prepare_for_campaign(selected_difficulty);
100 
101  if(!jump_to_campaign.scenario_id_.empty()) {
103  config {"next_scenario", jump_to_campaign.scenario_id_}
104  );
105  }
106 
107  if(!state.valid()) {
108  ERR_NG << "Cannot load scenario with id=" << state.get_scenario_id() << std::endl;
109  return false;
110  }
111 
112  configure_canceled = !enter_configure_mode(video, game_config_manager::get()->game_config(), state, create_eng);
113 
114  } while (configure_canceled);
115 
116  return true;
117 }
118 
120 {
121  // We create the config engine here in order to ensure values like use_map_settings are set correctly
122  // TODO: should this be passed to this function instead of created here?
123  ng::configure_engine config_eng(create_eng.get_state());
124 
125  // TODO: needed?
126  config_eng.update_initial_cfg(create_eng.current_level().data());
127 
128  if(!gui2::dialogs::sp_options_configure::execute(create_eng, config_eng, video)) {
129  return false;
130  }
131 
132  create_eng.get_parameters();
133  create_eng.prepare_for_new_level();
134 
135  enter_connect_mode(video, game_config, state);
136 
137  return true;
138 }
139 
140 bool enter_connect_mode(CVideo& /*video*/, const config& /*game_config*/, saved_game& state)
141 {
142  ng::connect_engine connect_eng(state, true, nullptr);
143 
144  // TODO: fix. Dialog starts game regardless of selection
145 #if 0
146  if(state.mp_settings().show_connect) {
147  lobby_info li(game_config, std::vector<std::string>());
148 
149  gui2::dialogs::mp_staging dlg(connect_eng, li);
150  dlg.show(video);
151 
152  if(dlg.get_retval() != gui2::window::OK) {
153  // TODO: enable the workflow loops from GUI1
154  //return enter_create_mode(video, game_config, state, jump_to_campaign_info(false, -1, "", ""));
155 
156  return false;
157  }
158  }
159 #endif
160 
161  connect_eng.start_game();
162 
163  return true;
164 }
165 
166 } // end namespace sp
void update_initial_cfg(const config &cfg)
void show_error_message(CVideo &video, const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:207
Dialog is closed with ok button.
Definition: window.hpp:111
std::vector< char_t > string
configure_engine
saved_game & get_state()
std::shared_ptr< level > level_ptr
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
level & current_level() const
Definition: video.hpp:29
This file contains the window object, this object is a top level container which has the event manage...
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
bool enter_connect_mode(CVideo &, const config &, saved_game &state)
void set_current_level(const size_t index)
const config & data() const
const mp_game_settings & get_parameters()
static lg::log_domain log_engine("engine")
void prepare_for_era_and_mods()
Definitions for the interface to Wesnoth Markup Language (WML).
void show(CVideo &video)
Shows the error in a dialog.
std::string select_campaign_difficulty(int set_value=-1)
select_campaign_difficulty
void set_current_level_type(const level::TYPE type)
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
void prepare_for_campaign(const std::string &difficulty="")
static bool execute(ng::create_engine &create_engine, ng::configure_engine &config_engine, CVideo &video)
Execute function.
Helper class, don't construct this directly.
Game configuration data as global variables.
Definition: build_info.cpp:53
std::string get_scenario_id()
Definition: saved_game.cpp:554
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
bool valid() const
Definition: saved_game.cpp:459
bool enter_configure_mode(CVideo &video, const config &game_config, saved_game &state, ng::create_engine &create_eng)
game_classification & classification()
Definition: saved_game.hpp:55
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:124
bool enter_create_mode(CVideo &video, const config &game_config, saved_game &state, jump_to_campaign_info jump_to_campaign)
#define e
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:59
#define ERR_NG