The Battle for Wesnoth  1.19.8+dev
saved_game.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 /**
16  * Some information about savefiles:
17  *
18  * A savefile can contain:
19  *
20  * - General information (toplevel attributes, [multiplayer])
21  * This is present in all savefiles
22  *
23  * - [statistics]
24  * This is present in all savefiles.
25  *
26  * - [snapshot]
27  * If a savegame was saved during a scenario this contains a snapshot of the game at the point when
28  * it was saved.
29  *
30  * - [carryover_sides_start]
31  * At start-of-scenario saves this contains data from the previous scenario that was preserved.
32  *
33  * - [carryover_sides]
34  * In savefile made during the game, this tag contains data from [carryover_sides_start] that was not
35  * used in the current scenario but should be saved for a next scenario
36  *
37  * - [replay_start]
38  * A snapshot made very early to replay the game from.
39  *
40  * - [replay]
41  * A record of game actions that was made between the creation of [replay_start] and [snapshot].
42  *
43  *
44  * The following types of savegames are known:
45  *
46  * - Start of scenario savefiles
47  * These files only contain general information, statistics, and [carryover_sides_start]. When these
48  * saves are loaded, the scenario data is loaded form the game config using the next_scenario attribute
49  * from [carryover_sides_start].
50  *
51  * - Expanded Start of scenario savefiles
52  * Similar to normal Start-of-scenario savefiles, but the also contain a [scenario] that contains the
53  * scenario data. This type is only used internally and usually doesn't get written to the disk.
54  *
55  * - In-game savefile
56  * These files contain general information, statistics, [snapshot], [replay], [replay_start], [snapshot],
57  * and [carryover_sides]. These files don't contain a [carryover_sides_start] because both starting points
58  * ([replay_start] and [snapshot]) were made after [carryover_sides_start] was merged into the scenario.
59  *
60  * - Replay savefiles
61  * Like a in-game save made during linger mode, but without the [snapshot].
62  */
63 
64 #include "saved_game.hpp"
65 
66 #include "carryover.hpp"
67 #include "config.hpp"
68 #include "cursor.hpp"
69 #include "formula/string_utils.hpp"
70 #include "game_config_manager.hpp"
72 #include "log.hpp"
73 #include "random.hpp"
75 #include "side_controller.hpp"
76 #include "utils/general.hpp"
77 #include "team.hpp" // for team::attributes, team::variables
78 #include "variable.hpp" // for config_variable_set
79 #include "variable_info.hpp"
80 
81 #include <cassert>
82 #include <iomanip>
83 
84 static lg::log_domain log_engine("engine");
85 #define ERR_NG LOG_STREAM(err, log_engine)
86 #define WRN_NG LOG_STREAM(warn, log_engine)
87 #define LOG_NG LOG_STREAM(info, log_engine)
88 #define DBG_NG LOG_STREAM(debug, log_engine)
89 
90 namespace
91 {
92 bool variable_to_bool(const config& vars, const std::string& expression)
93 {
94  std::string res = utils::interpolate_variables_into_string(expression, config_variable_set(vars));
95  return res == "true" || res == "yes" || res == "1";
96 }
97 
98 // helper objects for saved_game::expand_mp_events()
99 struct modevents_entry
100 {
101  modevents_entry(const std::string& _type, const std::string& _id)
102  : type(_type)
103  , id(_id)
104  {
105  }
106 
107  std::string type;
108  std::string id;
109 };
110 
111 bool is_illegal_file_char(char c)
112 {
113  return c == '/' || c == '\\' || c == ':' || (c >= 0x00 && c < 0x20)
114 #ifdef _WIN32
115  || c == '?' || c == '|' || c == '<' || c == '>' || c == '*' || c == '"'
116 #endif
117  ;
118 }
119 
120 } // end anon namespace
121 
123  : has_carryover_expanded_(false)
124  , carryover_(carryover_info().to_config())
125  , replay_start_()
126  , classification_()
127  , mp_settings_()
128  , starting_point_type_(starting_point::NONE)
129  , starting_point_()
130  , replay_data_()
131  , statistics_()
132  , skip_story_(false)
133 {
134 }
135 
137  : has_carryover_expanded_(false)
138  , carryover_()
139  , replay_start_()
140  , classification_(cfg)
141  , mp_settings_()
142  , starting_point_type_(starting_point::NONE)
143  , starting_point_()
144  , replay_data_()
145  , statistics_()
146  , skip_story_(false)
147 {
148  set_data(cfg);
149 }
150 
152  : has_carryover_expanded_(state.has_carryover_expanded_)
153  , carryover_(state.carryover_)
154  , replay_start_(state.replay_start_)
155  , classification_(state.classification_)
156  , mp_settings_(state.mp_settings_)
157  , starting_point_type_(state.starting_point_type_)
158  , starting_point_(state.starting_point_)
159  , replay_data_(state.replay_data_)
160  , statistics_(state.statistics_)
161  , skip_story_(state.skip_story_)
162 {
163 }
164 
166 {
167  carryover_.swap(carryover_sides_start);
168  has_carryover_expanded_ = false;
169 }
170 
172 {
173  if(has_carryover_expanded_ || !carryover_["random_seed"].empty()) {
174  return;
175  }
176 
177  std::stringstream stream;
178  stream << std::setfill('0') << std::setw(8) << std::hex << randomness::generator->get_random_int(0, std::numeric_limits<int>::max());
179  carryover_["random_seed"] = stream.str();
180  carryover_["random_calls"] = 0;
181 }
182 
184 {
185  write_general_info(out);
187 
188  if(!replay_start_.empty()) {
189  out.write_child("replay_start", replay_start_);
190  }
191 
192  out.open_child("replay");
193  replay_data_.write(out);
194  out.close_child("replay");
195  write_carryover(out);
196 }
197 
199 {
201  out.write_child("snapshot", starting_point_);
203  out.write_child("scenario", starting_point_);
204  }
205 }
206 
208 {
209  assert(not_corrupt());
210  out.write_child(has_carryover_expanded_ ? "carryover_sides" : "carryover_sides_start", carryover_);
211 }
212 
214 {
216  out.write_child("multiplayer", mp_settings_.to_config());
217  out.open_child("statistics");
218  statistics().write(out);
219  out.close_child("statistics");
220 }
221 
223 {
224  const bool is_loaded_game = starting_point_type_ != starting_point::SCENARIO;
225  const bool is_multiplayer_tag = classification().get_tagname() == "multiplayer";
227 
228  static const std::vector<std::string> team_defaults {
229  "carryover_percentage",
230  "carryover_add",
231  };
232 
233  if(auto campaign = game_config.find_child("campaign", "id", classification_.campaign)) {
234  // FIXME: The mp code could use `require_scenario` to check whether we have the addon in question installed.
235  // But since [scenario]s are usually hidden behind `#ifdef CAMPAIGN_DEFINE` it would not be able to find them.
236  // Investigate how this should actually work.
237  bool require_campaign = campaign["require_campaign"].to_bool(true);
238  starting_point_["require_scenario"] = require_campaign;
239  }
240 
241  for(config& side : starting_point_.child_range("side")) {
242  // Set save_id default value directly after loading to its default to prevent different default behaviour in
243  // mp_connect code and sp code.
244 
245  if(side["no_leader"].to_bool()) {
246  side["leader_lock"] = true;
247  side.remove_attribute("type");
248  }
249 
250  if(side["save_id"].empty()) {
251  side["save_id"] = side["id"];
252  }
253  if(side["save_id"].empty()) {
254  side["save_id"] = side.child_or_empty("leader")["id"];
255  }
256 
257  // If this side tag describes the leader of the side, convert it into a [leader] tag here, by doing this here,
258  // all code that follows, no longer has to hande the possibility of leader information directly in [side].
259 
260  // If this side tag describes the leader of the side
261  if(!side["type"].empty() && side["type"] != "null") {
262  auto temp = config{};
263 
264  for(const std::string& tag : team::tags) {
265  temp.append_children_by_move(side, tag);
266  }
267  for(const std::string& attr : team::attributes) {
268  if(side.has_attribute(attr)) {
269  temp[attr] = side[attr];
270  side.remove_attribute(attr);
271  }
272  }
273  temp["side"] = side["side"];
274  temp.swap(side);
275  temp.swap(side.add_child_at("leader", config(), 0));
276  }
277 
278  if(!is_multiplayer_tag) {
279  if(side["name"].blank()) {
280  side["name"] = side.child_or_empty("leader")["name"];
281  }
282  if(side["side_name"].blank()) {
283  side["side_name"] = side["name"];
284  }
285  }
286 
287  if(!is_loaded_game && !side["current_player"].empty()) {
288  ERR_NG << "Removed invalid 'current_player' attribute from [side] while loading a scenario. Consider using "
289  "'side_name' instead";
290 
291  side["current_player"] = config::attribute_value();
292  }
293 
294  // Set some team specific values to their defaults specified in scenario
295  for(const std::string& att_name : team_defaults) {
296  const config::attribute_value* scenario_value = starting_point_.get(att_name);
297  config::attribute_value& team_value = side[att_name];
298 
299  if(scenario_value && team_value.empty()) {
300  team_value = *scenario_value;
301  }
302  }
303  }
304 }
305 
307 {
310 
312  auto scenario =
313  game_config.find_child(classification().get_tagname(), "id", carryover_["next_scenario"]);
314 
315  if(scenario) {
317  starting_point_ = *scenario;
318 
319  // A hash has to be generated using an unmodified scenario data.
320  mp_settings_.hash = scenario->hash();
321 
323 
324  update_label();
325  set_defaults();
326  } else {
327  ERR_NG << "Couldn't find [" << classification().get_tagname() << "] with id=" << carryover_["next_scenario"];
330  }
331  }
332 }
333 
335 {
336  const std::string version_default = starting_point_["addon_id"].empty() ? game_config::wesnoth_version.str() : "";
337  config scenario;
338  scenario["id"] = starting_point_["addon_id"].str("mainline");
339  scenario["name"] = starting_point_["addon_title"].str("mainline");
340  scenario["version"] = starting_point_["addon_version"].str(version_default);
341  scenario["min_version"] = starting_point_["addon_min_version"];
342  scenario["required"] = starting_point_["require_scenario"].to_bool(false);
343  config& content = scenario.add_child("content");
344  content["id"] = starting_point_["id"];
345  content["name"] = starting_point_["name"];
346  // TODO: would it be better if this used the actual tagname ([multiplayer]/[scenario]) instead of always using [scenario]?
347  content["type"] = "scenario";
348 
350 }
351 
352 // "non scenario" at the time of writing this meaning any era, campaign, mods, or resources (see expand_mp_events() below).
353 void saved_game::load_non_scenario(const std::string& type, const std::string& id, size_t pos)
354 {
355  if(auto cfg = game_config_manager::get()->game_config().find_child(type, "id", id)) {
356  // Note the addon_id if this mod is required to play the game in mp.
357  std::string require_attr = "require_" + type;
358 
359  // anything with no addon_id is from mainline, and therefore isn't required in the sense that all players already have it
360  const std::string version_default = cfg["addon_id"].empty() ? game_config::wesnoth_version.str() : "";
361  config non_scenario;
362  // if there's no addon_id, then this isn't an add-on
363  non_scenario["id"] = cfg["addon_id"].str("mainline");
364  non_scenario["name"] = cfg["addon_title"].str("mainline");
365  non_scenario["version"] = cfg["addon_version"].str(version_default);
366  non_scenario["min_version"] = cfg["addon_min_version"];
367  non_scenario["required"] = cfg[require_attr].to_bool(!cfg["addon_id"].empty());
368  config& content = non_scenario.add_child("content");
369  content["id"] = id;
370  content["name"] = cfg["addon_title"].str(cfg["name"].str(""));
371  content["type"] = type;
372 
374 
375  // Copy events
376  for(const config& modevent : cfg->child_range("event")) {
377  if(modevent["enable_if"].empty()
378  || variable_to_bool(carryover_.child_or_empty("variables"), modevent["enable_if"])
379  ) {
380  starting_point_.add_child_at_total("event", modevent, pos++);
381  }
382  }
383 
384  // Copy lua
385  for(const config& modlua : cfg->child_range("lua")) {
386  starting_point_.add_child_at_total("lua", modlua, pos++);
387  }
388 
389  // Copy modify_unit_type
390  for(const config& modlua : cfg->child_range("modify_unit_type")) {
391  starting_point_.add_child_at_total("modify_unit_type", modlua, pos++);
392  }
393 
394  // Copy load_resource
395  for(const config& load_resource : cfg->child_range("load_resource")) {
396  starting_point_.add_child_at_total("load_resource", load_resource, pos++);
397  }
398  } else {
399  // TODO: A user message instead?
400  ERR_NG << "Couldn't find [" << type << "] with id=" << id;
401  }
402 }
403 
404 // Gets the ids of the mp_era and modifications which were set to be active, then fetches these configs from the
405 // game_config and copies their [event] and [lua] to the starting_point_.
406 // At this time, also collect the addon_id attributes which appeared in them and put this list in the addon_ids
407 // attribute of the mp_settings.
409 {
410  expand_scenario();
411 
412  if(starting_point_type_ == starting_point::SCENARIO && !starting_point_["has_mod_events"].to_bool(false)) {
413  std::vector<modevents_entry> mods;
414  std::set<std::string> loaded_resources;
415 
416  std::transform(classification_.active_mods.begin(), classification_.active_mods.end(), std::back_inserter(mods),
417  [](const std::string& id) { return modevents_entry("modification", id); }
418  );
419 
420  // We don't want the error message below if there is no era (= if this is a sp game).
421  if(!classification_.era_id.empty()) {
422  mods.emplace_back("era", classification_.era_id);
423  }
424 
425  if(!classification_.campaign.empty()) {
426  mods.emplace_back("campaign", classification_.campaign);
427  }
428 
429  for(modevents_entry& mod : mods) {
431  }
432  mods.clear();
433 
434  while(starting_point_.has_child("load_resource")) {
435  assert(starting_point_.child_count("load_resource") > 0);
436  std::string id = starting_point_.mandatory_child("load_resource")["id"];
437  size_t pos = starting_point_.find_total_first_of("load_resource");
438  starting_point_.remove_child("load_resource", 0);
439  if(loaded_resources.find(id) == loaded_resources.end()) {
440  loaded_resources.insert(id);
441  load_non_scenario("resource", id, pos);
442  }
443  }
444  starting_point_["has_mod_events"] = true;
445  starting_point_["loaded_resources"] = utils::join(loaded_resources);
446  }
447 }
448 
450 {
452  std::vector<modevents_entry> mods;
453 
454  std::transform(classification_.active_mods.begin(), classification_.active_mods.end(), std::back_inserter(mods),
455  [](const std::string& id) { return modevents_entry("modification", id); }
456  );
457 
458  mods.emplace_back("era", classification_.era_id);
459  mods.emplace_back("multiplayer", get_scenario_id());
460  mods.emplace_back("campaign", classification().campaign);
461 
462  config& variables = carryover_.child_or_add("variables");
463 
464  for(modevents_entry& mod : mods) {
465  if(auto cfg = mp_settings().options.find_child(mod.type, "id", mod.id)) {
466  for(const config& option : cfg->child_range("option")) {
467  try {
468  variable_access_create(option["id"], variables).as_scalar() = option["value"];
469  } catch(const invalid_variablename_exception&) {
470  ERR_NG << "variable " << option["id"] << "cannot be set to " << option["value"];
471  }
472  }
473  } else {
474  LOG_NG << "Couldn't find [" << mod.type << "] with id=" << mod.id << " for [option]s";
475  }
476  }
477  }
478 }
479 
480 static void inherit_scenario(config& scenario, config& map_scen)
481 {
482  config& map_scenario = map_scen.has_child("multiplayer") ? map_scen.mandatory_child("multiplayer") : (map_scen.has_child("scenario") ? map_scen.mandatory_child("scenario") : map_scen);
483  config sides;
484  sides.splice_children(map_scenario, "side");
485  scenario.append_children(map_scenario);
486  scenario.inherit_attributes(map_scenario);
487  for(config& side_from : sides.child_range("side")) {
488  auto side_to = scenario.find_child("side", "side", side_from["side"]);
489  if(side_to) {
490  side_to->inherit_attributes(side_from);
491  side_to->append_children(side_from);
492  } else {
493  scenario.add_child("side", side_from);
494  }
495  }
496 }
497 
499 {
500  if(!scenario["include_file"].empty()) {
501  std::string include_data = filesystem::read_scenario(scenario["include_file"]);
502  if(!include_data.empty()) {
503  config include_data_cfg;
504  read(include_data_cfg, include_data);
505  inherit_scenario(scenario, include_data_cfg);
506  }
507  // this method gets called two additional times, so without this you end up calling inherit_scenario() three times total
508  // this is equivalent to the below check for map_data being empty
509  scenario["include_file"] = "";
510  }
511 
512  if(scenario["map_data"].empty() && !scenario["map_file"].empty()) {
513  std::string map_data = filesystem::read_map(scenario["map_file"]);
514  if(map_data.find("map_data") != std::string::npos) {
515  // we have a scenario, generated by the editor
516  deprecated_message("map_file cfg", DEP_LEVEL::FOR_REMOVAL, "1.19", "Providing a .cfg file to the map_file attribute is deprecated. Use map_file for .map files and include_file for .cfg files.");
517  config map_data_cfg;
518  read(map_data_cfg, map_data);
519  inherit_scenario(scenario, map_data_cfg);
520  } else {
521  // we have an plain map_data file
522  scenario["map_data"] = map_data;
523  }
524  }
525 }
526 
528 {
529  expand_scenario();
530 
532  // If the entire scenario should be randomly generated
533  if(!starting_point_["scenario_generation"].empty()) {
534  LOG_NG << "randomly generating scenario...";
535  const cursor::setter cursor_setter(cursor::WAIT);
536 
537  config scenario_new =
538  random_generate_scenario(starting_point_["scenario_generation"], starting_point_.mandatory_child("generator"), &carryover_.child_or_empty("variables"));
539 
541  starting_point_ = std::move(scenario_new);
542 
543  update_label();
544  set_defaults();
545  }
546 
547  // If no map_data is provided, try to load the specified file directly
549  // If the map should be randomly generated
550  // We don’t want that we accidentally to this twice so we check for starting_point_["map_data"].empty()
551  if(starting_point_["map_data"].empty() && !starting_point_["map_generation"].empty()) {
552  LOG_NG << "randomly generating map...";
553  const cursor::setter cursor_setter(cursor::WAIT);
554 
555  starting_point_["map_data"] =
556  random_generate_map(starting_point_["map_generation"], starting_point_.mandatory_child("generator"), &carryover_.child_or_empty("variables"));
557  }
558  }
559 }
560 
561 void saved_game::post_scenario_generation(const config& old_scenario, config& generated_scenario)
562 {
563  static const std::vector<std::string> attributes_to_copy {
564  "id",
565  "addon_id",
566  "addon_title",
567  "addon_version",
568  "addon_min_version",
569  "require_scenario",
570  };
571 
572  // TODO: should we add "description" to this list?
573  // TODO: in theory it is possible that whether the scenario is required depends on the generated scenario, so maybe remove require_scenario from this list.
574 
575  for(const auto& str : attributes_to_copy) {
576  generated_scenario[str] = old_scenario[str];
577  }
578 
579  // Preserve "story" from the scenario toplevel.
580  // Note that it does not delete [story] tags in generated_scenario, so you can still have your story
581  // dependent on the generated scenario.
582  for(const config& story : old_scenario.child_range("story")) {
583  generated_scenario.add_child("story", story);
584  }
585 }
586 
587 
589 {
590  expand_scenario();
592  carryover_info sides(carryover_);
593 
595  for(config& side_cfg : get_starting_point().child_range("side")) {
596  sides.transfer_all_to(side_cfg);
597  }
598 
599  carryover_ = sides.to_config();
602  }
603 }
604 
605 bool saved_game::valid() const
606 {
608 }
609 
611 {
613  starting_point_.swap(snapshot);
614 
615  return starting_point_;
616 }
617 
619 {
621  starting_point_.swap(scenario);
622 
623  has_carryover_expanded_ = false;
624 
625  update_label();
626 }
627 
629 {
632 }
633 
635 {
636  return starting_point_;
637 }
638 
640 {
641  if(!replay_start_.empty()) {
642  return replay_start_;
643  }
644 
646  // Try to load the scenario form game config or from [scenario] if there is no [replay_start]
647  expand_scenario();
649  }
650 
652  return starting_point_;
653  }
654 
655  throw config::error("No replay_start found");
656 }
657 
659 {
661 
662  carryover_info sides(starting_point_, true);
663 
665  sides.rng().rotate_random();
666 
667  carryover_ = sides.to_config();
668 
669  has_carryover_expanded_ = false;
670 
673 
674  remove_snapshot();
675 }
676 
678 {
679  // TODO: remove this code duplication with write_... functions.
681 
682  if(!replay_start_.empty()) {
683  r.add_child("replay_start", replay_start_);
684  }
685 
686  replay_data_.write(r.add_child("replay"));
687 
689  r.add_child("snapshot", starting_point_);
691  r.add_child("scenario", starting_point_);
692  }
693 
694  r.add_child(has_carryover_expanded_ ? "carryover_sides" : "carryover_sides_start", carryover_);
695  r.add_child("multiplayer", mp_settings_.to_config());
696  r.add_child("statistics", statistics_.to_config());
697 
698  return r;
699 }
700 
701 std::string saved_game::get_scenario_id() const
702 {
703  std::string scenario_id;
704 
706  scenario_id = starting_point_["id"].str();
707  } else if(!has_carryover_expanded_) {
708  scenario_id = carryover_["next_scenario"].str();
709  } else if(!replay_start_.empty()) {
710  scenario_id = replay_start_["id"].str();
711  } else {
712  assert(!"cannot figure out scenario_id");
713  throw "assertion ignored";
714  }
715 
716  return scenario_id == "null" ? "" : scenario_id;
717 }
718 
720 {
721  return true;
722 }
723 
725 {
726  std::string& label = classification().label;
727 
728  if(classification().abbrev.empty()) {
729  label = starting_point_["name"].str();
730  } else {
731  label = classification().abbrev + "-" + starting_point_["name"];
732  }
733 
734  utils::erase_if(label, is_illegal_file_char);
735  std::replace(label.begin(), label.end(), '_', ' ');
736 }
737 
739 {
740  for(config& side : starting_point_.child_range("side")) {
741  // for humans "goto_x/y" is used for multi-turn-moves
742  // for the ai "goto_x/y" is a way for wml to order the ai to move a unit to a certain place.
743  // we want to cancel human order but not to break wml.
744  if(side["controller"] != side_controller::human) {
745  continue;
746  }
747 
748  for(config& unit : side.child_range("unit")) {
749  unit["goto_x"] = -999;
750  unit["goto_y"] = -999;
751  }
752  }
753 }
754 
756 {
757  for(config& side : starting_point_.child_range("side")) {
758  side.remove_attribute("is_local");
759  }
760 }
761 
763 {
764  swap(other);
765  return *this;
766 }
767 
769 {
770  carryover_.swap(other.carryover_);
771 
775 
779 
781 }
782 
784 {
785  log_scope("read_game");
786 
787  if(auto caryover_sides = cfg.optional_child("carryover_sides")) {
788  carryover_.swap(*caryover_sides);
790  } else if(auto caryover_sides_start = cfg.optional_child("carryover_sides_start")) {
791  carryover_.swap(*caryover_sides_start);
792  has_carryover_expanded_ = false;
793  } else {
794  carryover_.clear();
795  has_carryover_expanded_ = false;
796  }
797 
798  if(auto replay_start = cfg.optional_child("replay_start")) {
800  } else {
802  }
803 
805 
806  // Serversided replays can contain multiple [replay]
807  for(config& replay : cfg.child_range("replay")) {
809  }
810 
812 
813  if(auto snapshot = cfg.optional_child("snapshot")) {
815  starting_point_.swap(*snapshot);
816  } else if(auto scenario = cfg.optional_child("scenario")) {
818  starting_point_.swap(*scenario);
819  } else {
822  }
823 
824  LOG_NG << "scenario: '" << carryover_["next_scenario"].str() << "'";
825 
826  if(auto stats = cfg.optional_child("statistics")) {
827  statistics_.read(*stats);
828  }
829 
831  mp_settings_ = { cfg.child_or_empty("multiplayer") };
832 
833  cfg.clear();
834 }
835 
837 {
838  carryover_.clear();
839  classification_ = {};
840  has_carryover_expanded_ = false;
841  mp_settings_ = {};
842  replay_data_.swap({});
847 }
848 
849 void swap(saved_game& lhs, saved_game& rhs)
850 {
851  lhs.swap(rhs);
852 }
const config to_config()
Definition: carryover.cpp:223
void merge_old_carryover(const carryover_info &old_carryover)
Definition: carryover.cpp:254
void transfer_to(config &level)
Definition: carryover.cpp:194
void transfer_all_to(config &side_cfg)
Definition: carryover.cpp:168
const randomness::mt_rng & rng() const
Definition: carryover.hpp:92
Variant for storing WML attributes.
bool empty() const
Tests for an attribute that either was never set or was set to "".
Class for writing a config out to a file in pieces.
void close_child(const std::string &key)
void write(const config &cfg)
void write_child(const std::string &key, const config &cfg)
void open_child(const std::string &key)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:390
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:362
void remove_child(config_key_type key, std::size_t index)
Definition: config.cpp:639
config_attribute_value attribute_value
Variant for storing WML attributes.
Definition: config.hpp:291
std::size_t child_count(config_key_type key) const
Definition: config.cpp:292
optional_config_impl< config > find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:780
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:312
child_itors child_range(config_key_type key)
Definition: config.cpp:268
config & child_or_add(config_key_type key)
Returns a reference to the first child with the given key.
Definition: config.cpp:401
std::size_t all_children_count() const
Definition: config.cpp:302
config & add_child_at_total(config_key_type key, const config &val, std::size_t pos)
Definition: config.cpp:512
void remove_attribute(config_key_type key)
Definition: config.cpp:162
void inherit_attributes(const config &c)
Merge the attributes of config 'c' into this config, preserving this config's values.
Definition: config.cpp:1181
std::size_t find_total_first_of(config_key_type key, std::size_t start=0)
Definition: config.cpp:498
void swap(config &cfg)
Definition: config.cpp:1332
void append_children_by_move(config &cfg, config_key_type key)
Moves children with the given name from the given config to this one.
Definition: config.cpp:224
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:167
bool empty() const
Definition: config.cpp:845
void splice_children(config &src, config_key_type key)
Moves all the children with tag key from src to this.
Definition: config.cpp:577
void clear()
Definition: config.cpp:824
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:681
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:380
config & add_child(config_key_type key)
Definition: config.cpp:436
std::vector< std::string > active_mods
std::string label
Name of the game (e.g.
std::string get_tagname() const
std::string campaign
The id of the campaign being played.
std::string abbrev
the campaign abbreviation
static game_config_manager * get()
void load_game_config_for_game(const game_classification &classification, const std::string &scenario_id)
const game_config_view & game_config() const
A class grating read only view to a vector of config objects, viewed as one config with all children ...
void rotate_random()
Resets the random to the 0 calls and the seed to the random this way we stay in the same sequence but...
Definition: mt_rng.cpp:74
int get_random_int(int min, int max)
Definition: random.hpp:51
void write(config_writer &out) const
void append_config(const config &data)
void swap(replay_recorder_base &other)
game_classification & classification()
Definition: saved_game.hpp:56
bool not_corrupt() const
Definition: saved_game.cpp:719
saved_game & operator=(const saved_game &other)=delete
void set_random_seed()
sets the random seed if that didn't already happen.
Definition: saved_game.cpp:171
config replay_start_
snapshot made before the start event.
Definition: saved_game.hpp:158
void swap(saved_game &other)
Definition: saved_game.cpp:768
void write_config(config_writer &out) const
writes the config information into a stream (file)
Definition: saved_game.cpp:183
void expand_scenario()
copies the content of a [scenario] with the correct id attribute from the game config into this objec...
Definition: saved_game.cpp:306
void expand_mp_options()
adds values of [option]s into [carryover_sides_start][variables] so that they are applied in the next...
Definition: saved_game.cpp:449
void unify_controllers()
Definition: saved_game.cpp:755
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:165
config to_config() const
Definition: saved_game.cpp:677
starting_point starting_point_type_
Definition: saved_game.hpp:163
std::string get_scenario_id() const
Definition: saved_game.cpp:701
static void post_scenario_generation(const config &old_scenario, config &generated_scenario)
copies attributes & tags from the 'outer' [scenario] to the scenario that is generated by scenario_ge...
Definition: saved_game.cpp:561
replay_recorder_base replay_data_
Definition: saved_game.hpp:170
void clear()
Definition: saved_game.cpp:836
void load_non_scenario(const std::string &type, const std::string &id, size_t pos)
helper for expand_mp_events();
Definition: saved_game.cpp:353
void update_label()
sets classification().label to the correct value.
Definition: saved_game.cpp:724
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:60
statistics_record::campaign_stats_t statistics_
Definition: saved_game.hpp:172
void set_scenario(config scenario)
Definition: saved_game.cpp:618
bool has_carryover_expanded_
Definition: saved_game.hpp:150
config & replay_start()
Definition: saved_game.hpp:128
static void expand_map_file(config &scenario)
reads scenario["map_file"]
Definition: saved_game.cpp:498
void remove_snapshot()
Definition: saved_game.cpp:628
config & get_starting_point()
Definition: saved_game.cpp:634
config starting_point_
The starting pos where the (non replay) game will be started from.
Definition: saved_game.hpp:168
const config & get_replay_starting_point()
Definition: saved_game.cpp:639
void check_require_scenario()
Add addon_id information if needed.
Definition: saved_game.cpp:334
void write_starting_point(config_writer &out) const
Definition: saved_game.cpp:198
void expand_mp_events()
adds [event]s from [era] and [modification] into this scenario does NOT expand [option]s because vari...
Definition: saved_game.cpp:408
config carryover_
depends on has_carryover_expanded_: if true: The carryover information for all sides from the previou...
Definition: saved_game.hpp:156
void expand_random_scenario()
takes care of generate_map=, generate_scenario=, map= attributes This should be called before expandi...
Definition: saved_game.cpp:527
mp_game_settings mp_settings_
Definition: saved_game.hpp:161
void expand_carryover()
merges [carryover_sides_start] into [scenario] and saves the rest into [carryover_sides] Removes [car...
Definition: saved_game.cpp:588
@ SNAPSHOT
We have a [snapshot] (mid-game-savefile).
@ SCENARIO
We have a [scenario] (start-of-scenario) savefile.
@ NONE
There is no scenario stating pos data (start-of-scenario).
@ INVALID
We failed to get a starting pos in expand_scenario.
statistics_record::campaign_stats_t & statistics()
Definition: saved_game.hpp:143
void cancel_orders()
Definition: saved_game.cpp:738
void convert_to_start_save()
converts a normal savegame form the end of a scenaio to a start-of-scenario savefile for the next sce...
Definition: saved_game.cpp:658
bool valid() const
Definition: saved_game.cpp:605
void set_defaults()
does some post loading stuff must be used before passing the data to connect_engine
Definition: saved_game.cpp:222
void write_general_info(config_writer &out) const
Definition: saved_game.cpp:213
game_classification classification_
some general information of the game that doesn't change during the game
Definition: saved_game.hpp:160
void set_data(config &cfg)
destroys the passed config.
Definition: saved_game.cpp:783
config & set_snapshot(config snapshot)
Definition: saved_game.cpp:610
void write_carryover(config_writer &out) const
Definition: saved_game.cpp:207
static const std::set< std::string > tags
Stores the child tags recognized by [side].
Definition: team.hpp:160
static const std::set< std::string > attributes
Stores the attributes recognized by [side].
Definition: team.hpp:155
This class represents a single unit of a specific type.
Definition: unit.hpp:133
std::string str() const
Serializes the version number into string form.
Definitions for the interface to Wesnoth Markup Language (WML).
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:201
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:199
Standard logging facilities (interface).
#define log_scope(description)
Definition: log.hpp:276
std::string random_generate_map(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:44
config random_generate_scenario(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:56
@ WAIT
Definition: cursor.hpp:28
std::string read_scenario(const std::string &name)
std::string read_map(const std::string &name)
Game configuration data as global variables.
Definition: build_info.cpp:61
const version_info wesnoth_version(VERSION)
@ NONE
Default, unset return value.
Definition: retval.hpp:32
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified formatting tag.
Definition: markup.hpp:50
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
constexpr auto transform
Definition: ranges.hpp:41
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition: general.hpp:106
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void swap(saved_game &lhs, saved_game &rhs)
Implement non-member swap function for std::swap (calls saved_game::swap).
Definition: saved_game.cpp:849
static lg::log_domain log_engine("engine")
#define ERR_NG
Definition: saved_game.cpp:85
static void inherit_scenario(config &scenario, config &map_scen)
Definition: saved_game.cpp:480
#define LOG_NG
Definition: saved_game.cpp:87
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:629
void update_addon_requirements(const config &addon_cfg)
Takes a config with addon metadata (id, name, version, min_version) and adds it as a requirement for ...
config to_config() const
void new_scenario(const std::string &scenario_name)
Adds an entry for anew scenario to wrte to.
void read(const config &cfg, bool append=false)
void write(config_writer &out) const
mock_char c
variable_info_mutable< variable_info_implementation::vi_policy_create > variable_access_create
'Create if nonexistent' access.