The Battle for Wesnoth  1.17.0-dev
singleplayer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 
16 
17 #include "config.hpp"
21 #include "game_launcher.hpp"
23 #include "gui/dialogs/message.hpp"
26 #include "gui/widgets/retval.hpp"
27 #include "wml_exception.hpp"
28 
29 static lg::log_domain log_engine("engine");
30 #define ERR_NG LOG_STREAM(err, log_engine)
31 
32 namespace sp
33 {
34 bool select_campaign(saved_game& state, jump_to_campaign_info jump_to_campaign)
35 {
36  while(true) {
37  ng::create_engine create_eng(state);
38  create_eng.set_current_level_type(ng::level::TYPE::SP_CAMPAIGN);
39 
40  const std::vector<ng::create_engine::level_ptr> campaigns =
41  create_eng.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
42 
43  if(campaigns.empty()) {
44  gui2::show_error_message(_("No campaigns are available."));
45  return false;
46  }
47 
48  std::string random_mode = "", difficulty = "";
49 
50  // No campaign selected from command line
51  if(jump_to_campaign.campaign_id.empty()) {
52  gui2::dialogs::campaign_selection dlg(create_eng);
53 
54  try {
55  dlg.show();
56  } catch(const wml_exception& e) {
57  e.show();
58  return false;
59  }
60 
61  if(dlg.get_retval() != gui2::retval::OK) {
62  return false;
63  }
64 
65  switch(dlg.get_rng_mode()) {
67  random_mode = "";
68  break;
70  random_mode = "deterministic";
71  break;
73  random_mode = "biased";
74  break;
75  }
76 
77  difficulty = dlg.get_difficulty();
78  } else {
79  // Don't reset the campaign_id_ so we can know
80  // if we should quit the game or return to the main menu
81 
82  // Checking for valid campaign name
83  const auto campaign = std::find_if(campaigns.begin(), campaigns.end(), [&jump_to_campaign](ng::create_engine::level_ptr level) {
84  return level->data()["id"] == jump_to_campaign.campaign_id;
85  });
86 
87  // Didn't find a campaign with that id
88  if(campaign == campaigns.end()) {
89  ERR_NG << "No such campaign id to jump to: [" << jump_to_campaign.campaign_id << "]" << std::endl;
90  return false;
91  }
92 
93  create_eng.set_current_level(std::distance(campaigns.begin(), campaign));
94  }
95 
96  state.classification().random_mode = random_mode;
97 
98  const auto selected_difficulty = difficulty;
99 
100  if(selected_difficulty == "FAIL") return false;
101  if(selected_difficulty == "CANCEL") {
102  if(!jump_to_campaign.campaign_id.empty()) {
103  jump_to_campaign.campaign_id = "";
104  }
105 
106  // Canceled difficulty dialog, relaunch the campaign selection dialog
107  return select_campaign(state, jump_to_campaign);
108  }
109 
110  create_eng.prepare_for_era_and_mods();
111  create_eng.prepare_for_campaign(selected_difficulty);
112 
113  if(!jump_to_campaign.scenario_id.empty()) {
115  config {"next_scenario", jump_to_campaign.scenario_id}
116  );
117  }
118 
119  if(!state.valid()) {
120  ERR_NG << "Cannot load scenario with id=" << state.get_scenario_id() << std::endl;
121  return false;
122  }
123 
124  if(configure_campaign(state, create_eng)) {
125  break;
126  }
127  }
128 
129  return true;
130 }
131 
133 {
134  // We create the config engine here in order to ensure values like use_map_settings are set correctly
135  // TODO: should this be passed to this function instead of created here?
136  ng::configure_engine config_eng(create_eng.get_state());
137 
138  // TODO: needed?
139  config_eng.update_initial_cfg(create_eng.current_level().data());
140 
141  if(!gui2::dialogs::sp_options_configure::execute(create_eng, config_eng)) {
142  return false;
143  }
144 
145  create_eng.get_parameters();
146  create_eng.prepare_for_new_level();
147 
148  ng::connect_engine connect_eng(state, true, nullptr);
149  connect_eng.start_game();
150 
151  return true;
152 }
153 
154 } // end namespace sp
void update_initial_cfg(const config &cfg)
configure_engine
saved_game & get_state()
std::string scenario_id
The ID of the scenario within the campaign to jump to.
std::shared_ptr< level > level_ptr
This shows the dialog which allows the user to choose which campaign to play.
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
bool valid() const
Definition: saved_game.cpp:553
void set_current_level(const std::size_t index)
static std::string _(const char *str)
Definition: gettext.hpp:93
bool show(const unsigned auto_close_time=0)
Shows the window.
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).
std::string get_scenario_id() const
Definition: saved_game.cpp:648
const config & data() const
void show() const
Shows the error in a dialog.
void set_current_level_type(const level::TYPE type)
const std::string & get_difficulty() const
void prepare_for_campaign(const std::string &difficulty="")
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
Helper class, don&#39;t construct this directly.
static bool execute(ng::create_engine &create_engine, ng::configure_engine &config_engine)
Execute function.
level & current_level() const
int get_retval() const
Returns the cached window exit code.
bool select_campaign(saved_game &state, jump_to_campaign_info jump_to_campaign)
bool configure_campaign(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:161
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:206
#define e
Dialog was closed with the OK button.
Definition: retval.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
#define ERR_NG
std::string campaign_id
The ID of the campaign to launch.