The Battle for Wesnoth  1.19.4+dev
saved_game.hpp
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 #pragma once
16 
17 #include "config.hpp"
18 #include "game_classification.hpp"
19 #include "mp_game_settings.hpp"
20 #include "replay_recorder_base.hpp"
21 #include "statistics_record.hpp"
22 
23 class config_writer;
24 
26 {
27  enum class starting_point {
28  /** There is no scenario stating pos data (start-of-scenario). */
29  NONE,
30  /** We have a [snapshot] (mid-game-savefile). */
31  SNAPSHOT,
32  /** We have a [scenario] (start-of-scenario) savefile. */
33  SCENARIO,
34  /** We failed to get a starting pos in expand_scenario. */
35  INVALID
36  };
37 
38 public:
39  saved_game();
40  saved_game(const saved_game& state);
41  explicit saved_game(config cfg);
43 
44  saved_game& operator=(const saved_game& other) = delete;
46  void swap(saved_game& other);
47  /** destroys the passed config. */
48  void set_data(config& cfg);
49  void clear();
50  /** writes the config information into a stream (file) */
51  void write_config(config_writer& out) const;
52  void write_general_info(config_writer& out) const;
53  void write_carryover(config_writer& out) const;
54  void write_starting_point(config_writer& out) const;
55  config to_config() const;
58 
59  /** Multiplayer parameters for this game */
61  const mp_game_settings& mp_settings() const { return mp_settings_; }
62 
63  void set_carryover_sides_start(config carryover_sides_start);
64 
65  /**
66  * copies the content of a [scenario] with the correct id attribute from the game config into this object.
67  * reloads the game config from disk if necessary.
68  */
69  void expand_scenario();
70  /**
71  * merges [carryover_sides_start] into [scenario] and saves the rest into [carryover_sides]
72  * Removes [carryover_sides_start] afterwards
73  */
74  void expand_carryover();
75  /**
76  * adds [event]s from [era] and [modification] into this scenario
77  * does NOT expand [option]s because variables are persitent anyway to we don't need it
78  * should be called after expand_scenario() but before expand_carryover()
79  */
80  void expand_mp_events();
81  /** helper for expand_mp_events(); */
82  void load_non_scenario(const std::string& type, const std::string& id, size_t pos);
83  /**
84  * adds values of [option]s into [carryover_sides_start][variables] so that they are applied in the next level.
85  * Note that since [variabels] are persistent we only use this once at the beginning
86  * of a campaign but calling it multiple times is no harm eigher
87  */
88  void expand_mp_options();
89  /**
90  * takes care of generate_map=, generate_scenario=, map= attributes
91  * This should be called before expanding carryover or mp_events because this might completely replace starting_point_.
92  */
94  /** copies attributes & tags from the 'outer' [scenario] to the scenario that is generated by scenario_generation= */
95  static void post_scenario_generation(const config& old_scenario, config& generated_scenario);
96  /** reads scenario["map_file"] */
97  static void expand_map_file(config& scenario);
98  /** Add addon_id information if needed. */
100  bool valid() const;
101  /** @return the snapshot in the savefile (get_starting_point) */
102  config& set_snapshot(config snapshot);
103  void set_scenario(config scenario);
104  void remove_snapshot();
105 
106  bool is_mid_game_save() const
107  {
109  }
110  bool is_start_of_scenario() const
111  {
112  return !has_carryover_expanded_;
113  }
114  /**
115  * converts a normal savegame form the end of a scenaio to a start-of-scenario savefile for the next scenaio,
116  * The saved_game must contain a [snapshot] made during the linger mode of the last scenaio.
117  */
118  void convert_to_start_save();
119  /** sets the random seed if that didn't already happen. */
120  void set_random_seed();
121  /** @return the starting pos for replays. Usually this is [replay_start] but it can also be a [scenario] if no [replay_start] is present */
123  /** @return the id of the currently played scenario or the id of the next scenario if this is a between-scenaios-save (also called start-of-scenario-save). */
124  std::string get_scenario_id() const;
125  /** @return the config from which the game will be started. (this is [scenario] or [snapshot] in the savefile) */
127  const config& get_starting_point() const { return starting_point_; }
129  const config& replay_start() const { return replay_start_; }
130 
131  bool not_corrupt() const;
132  /** sets classification().label to the correct value. */
133  void update_label();
134 
135  void cancel_orders();
136  /* removes network_ai and network controller types*/
137  void unify_controllers();
138  /** does some post loading stuff must be used before passing the data to connect_engine */
139  void set_defaults();
141  const replay_recorder_base& get_replay() const { return replay_data_; }
142 
145  /** Whether to play [story] tags */
146  bool skip_story() const { return skip_story_; }
148 
149 private:
151  /**
152  depends on has_carryover_expanded_:
153  if true: The carryover information for all sides from the previous scenario that aren't used in this scenario (to be carried over to the next scenario).
154  if false: The carryover information for all sides from the previous scenario.
155  */
157  /** snapshot made before the start event. To be used as a starting pos for replays */
159  /** some general information of the game that doesn't change during the game */
162 
164  /**
165  The starting pos where the (non replay) game will be started from.
166  This can eigher be a [scenario] for a fresh game or a [snapshot] if this is a reloaded game
167  */
169 
171 
173 
175 };
176 
177 /** Implement non-member swap function for std::swap (calls @ref saved_game::swap). */
178 void swap(saved_game& lhs, saved_game& rhs);
Class for writing a config out to a file in pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:163
game_classification & classification()
Definition: saved_game.hpp:56
bool not_corrupt() const
Definition: saved_game.cpp:696
const config & get_starting_point() const
Definition: saved_game.hpp:127
void set_skip_story(bool skip_story)
Definition: saved_game.hpp:147
saved_game & operator=(const saved_game &other)=delete
bool skip_story_
Definition: saved_game.hpp:174
bool is_mid_game_save() const
Definition: saved_game.hpp:106
bool is_start_of_scenario() const
Definition: saved_game.hpp:110
void set_random_seed()
sets the random seed if that didn't already happen.
Definition: saved_game.cpp:169
const statistics_record::campaign_stats_t & statistics() const
Definition: saved_game.hpp:144
config replay_start_
snapshot made before the start event.
Definition: saved_game.hpp:158
void swap(saved_game &other)
Definition: saved_game.cpp:745
void write_config(config_writer &out) const
writes the config information into a stream (file)
Definition: saved_game.cpp:181
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:283
const config & replay_start() const
Definition: saved_game.hpp:129
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:426
void unify_controllers()
Definition: saved_game.cpp:732
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:163
config to_config() const
Definition: saved_game.cpp:654
starting_point starting_point_type_
Definition: saved_game.hpp:163
std::string get_scenario_id() const
Definition: saved_game.cpp:678
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:538
replay_recorder_base replay_data_
Definition: saved_game.hpp:170
void clear()
Definition: saved_game.cpp:813
void load_non_scenario(const std::string &type, const std::string &id, size_t pos)
helper for expand_mp_events();
Definition: saved_game.cpp:330
const replay_recorder_base & get_replay() const
Definition: saved_game.hpp:141
void update_label()
sets classification().label to the correct value.
Definition: saved_game.cpp:701
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:595
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:475
const game_classification & classification() const
Definition: saved_game.hpp:57
void remove_snapshot()
Definition: saved_game.cpp:605
config & get_starting_point()
Definition: saved_game.cpp:611
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:616
void check_require_scenario()
Add addon_id information if needed.
Definition: saved_game.cpp:311
void write_starting_point(config_writer &out) const
Definition: saved_game.cpp:196
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:385
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:504
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:565
const mp_game_settings & mp_settings() const
Definition: saved_game.hpp:61
@ 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:715
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:635
bool valid() const
Definition: saved_game.cpp:582
void set_defaults()
does some post loading stuff must be used before passing the data to connect_engine
Definition: saved_game.cpp:220
void write_general_info(config_writer &out) const
Definition: saved_game.cpp:211
game_classification classification_
some general information of the game that doesn't change during the game
Definition: saved_game.hpp:160
replay_recorder_base & get_replay()
Definition: saved_game.hpp:140
void set_data(config &cfg)
destroys the passed config.
Definition: saved_game.cpp:760
config & set_snapshot(config snapshot)
Definition: saved_game.cpp:587
void write_carryover(config_writer &out) const
Definition: saved_game.cpp:205
bool skip_story() const
Whether to play [story] tags.
Definition: saved_game.hpp:146
Definitions for the interface to Wesnoth Markup Language (WML).
void swap(saved_game &lhs, saved_game &rhs)
Implement non-member swap function for std::swap (calls saved_game::swap).
Definition: saved_game.cpp:826