The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
singleplayer.cpp
Go to the documentation of this file.
1 #include "singleplayer.hpp"
2 #include "config.hpp"
3 #include "config_assign.hpp"
7 #include "gui/widgets/window.hpp"
8 #include "multiplayer.hpp"
10 #include "multiplayer_connect.hpp"
11 #include "multiplayer_ui.hpp"
12 #include "playcampaign.hpp"
13 #include "resources.hpp"
14 #include "wml_exception.hpp"
15 
16 namespace {
17 
18 mp::chat gamechat;
19 config gamelist;
20 
21 }
22 
23 namespace sp {
24 
26  saved_game& state, jump_to_campaign_info jump_to_campaign, bool local_players_only) {
27 
28  bool configure_canceled = false;
29 
30  do {
31 
32  ng::create_engine create_eng(video, state);
33  create_eng.set_current_level_type(ng::level::TYPE::SP_CAMPAIGN);
34 
35  std::vector<ng::create_engine::level_ptr> campaigns(
36  create_eng.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN));
37 
38  if (campaigns.empty()) {
40  _("No campaigns are available.\n"));
41  return false;
42  }
43 
44  bool use_deterministic_mode = false;
45  // No campaign selected from command line
46  if (jump_to_campaign.campaign_id_.empty() == true)
47  {
48  gui2::tcampaign_selection dlg(create_eng);
49 
50  try {
51  dlg.show(video);
52  } catch(twml_exception& e) {
53  e.show(video);
54  return false;
55  }
56 
57  if(dlg.get_retval() != gui2::twindow::OK) {
58  return false;
59  }
60 
61  use_deterministic_mode = dlg.get_deterministic();
62 
63  }
64  else
65  {
66  // don't reset the campaign_id_ so we can know
67  // if we should quit the game or return to the main menu
68 
69  // checking for valid campaign name
70  bool not_found = true;
71  for(size_t i = 0; i < campaigns.size(); ++i)
72  {
73  if (campaigns[i]->data()["id"] == jump_to_campaign.campaign_id_)
74  {
75  create_eng.set_current_level(i);
76  not_found = false;
77  break;
78  }
79 
80  }
81 
82  // didn't find any campaign with that id
83  if (not_found)
84  {
85  //TODO: use ERR_NG or similar
86  std::cerr<<"No such campaign id to jump to: ["<<jump_to_campaign.campaign_id_<<"]\n";
87  return false;
88  }
89 
90  }
91 
92  std::string random_mode = use_deterministic_mode ? "deterministic" : "";
93  state.classification().random_mode = random_mode;
94 
95  std::string selected_difficulty = create_eng.select_campaign_difficulty(jump_to_campaign.difficulty_);
96 
97  if (selected_difficulty == "FAIL") return false;
98  if (selected_difficulty == "CANCEL") {
99  if (jump_to_campaign.campaign_id_.empty() == false)
100  {
101  jump_to_campaign.campaign_id_ = "";
102  }
103  // canceled difficulty dialog, relaunch the campaign selection dialog
104  return enter_create_mode(video, game_config, state, jump_to_campaign, local_players_only);
105  }
106 
107  create_eng.prepare_for_era_and_mods();
108  create_eng.prepare_for_campaign(selected_difficulty);
109 
110  if(!jump_to_campaign.scenario_id_.empty()) {
112  config_of("next_scenario", jump_to_campaign.scenario_id_)
113  );
114  }
115  create_eng.prepare_for_new_level();
116 
117  create_eng.get_parameters();
118  if(!state.valid())
119  {
120  //TODO: use ERR_NG or similar
121  std::cerr << "Cannot load scenario with id=" << state.get_scenario_id() << "\n";
122  return false;
123  }
124  configure_canceled = !enter_configure_mode(video, game_config_manager::get()->game_config(), state, local_players_only);
125 
126  } while (configure_canceled);
127 
128  return true;
129 }
130 
131 bool enter_configure_mode(CVideo& video, const config& game_config, saved_game& state, bool local_players_only) {
132  bool connect_canceled;
133  do {
134  connect_canceled = false;
135 
137 
138  {
139  mp::configure ui(video, 0, game_config, gamechat, gamelist, state, local_players_only);
140  mp::run_lobby_loop(video, ui);
141  res = ui.get_result();
142  ui.get_parameters();
143  }
144 
145  switch (res) {
146  case mp::ui::CREATE:
147  connect_canceled = !enter_connect_mode(video, game_config, state, local_players_only);
148  break;
149  case mp::ui::QUIT:
150  default:
151  return false;
152  }
153  } while (connect_canceled);
154  return true;
155 }
156 
158  saved_game& state, bool local_players_only) {
159 
160  ng::connect_engine connect_eng(state, true, nullptr);
161 
162  if (state.mp_settings().show_connect) {
164  gamelist.clear();
165  {
166  mp::connect ui(video, 0, state.mp_settings().name, game_config, gamechat, gamelist, connect_eng);
167  mp::run_lobby_loop(video, ui);
168  res = ui.get_result();
169 
170  if (res == mp::ui::PLAY) {
171  ui.start_game();
172  }
173  }
174  switch (res) {
175  case mp::ui::PLAY:
176  return true;
177  case mp::ui::CREATE:
178  enter_create_mode(video, game_config, state, jump_to_campaign_info(false, -1, "", ""), local_players_only);
179  break;
180  case mp::ui::QUIT:
181  default:
182  return false;
183  }
184  return true;
185  } else {
186  connect_eng.start_game();
187  return true;
188  }
189 }
190 
191 } // end namespace sp
void show_error_message(CVideo &video, const std::string &message, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:198
int get_retval() const
Definition: dialog.hpp:161
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
Definition: video.hpp:58
result get_result()
Returns the result of the current widget.
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
void show(CVideo &video)
Shows the error in a dialog.
void set_current_level(const size_t index)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
const mp_game_settings & get_parameters()
void prepare_for_era_and_mods()
Definitions for the interface to Wesnoth Markup Language (WML).
std::string select_campaign_difficulty(int set_value=-1)
select_campaign_difficulty
bool enter_connect_mode(CVideo &video, const config &game_config, saved_game &state, bool local_players_only)
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
Dialog is closed with ok button.
Definition: window.hpp:125
bool enter_create_mode(CVideo &video, const config &game_config, saved_game &state, jump_to_campaign_info jump_to_campaign, bool local_players_only)
bool enter_configure_mode(CVideo &video, const config &game_config, saved_game &state, bool local_players_only)
GLuint res
Definition: glew.h:9258
Game configuration data as global variables.
Definition: build_info.cpp:38
std::string get_scenario_id()
Definition: saved_game.cpp:533
size_t i
Definition: function.cpp:1057
void prepare_for_campaign(const std::string &difficulty)
-file mapgen.hpp
game_classification & classification()
Definition: saved_game.hpp:54
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:113
void set_current_level_type(const level::TYPE)
#define e
this class memorizes a chat session.
bool valid()
Definition: saved_game.cpp:446
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
Helper class, don't construct this directly.
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:58
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void run_lobby_loop(CVideo &video, mp::ui &ui)
Definition: multiplayer.cpp:72