The Battle for Wesnoth  1.19.0-dev
game_data.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
19 #include "game_end_exceptions.hpp"
20 #include "map/location.hpp"
21 #include "mt_rng.hpp"
22 #include "variable_info.hpp"
23 
25 
26 class game_data : public variable_set {
27 public:
28  explicit game_data(const config& level);
29  game_data(const game_data& data);
30 
31  std::vector<scoped_wml_variable*> scoped_variables;
32 
33  const config& get_variables() const { return variables_; }
34  /** throws invalid_variablename_exception if varname is no valid variable name. */
35  config::attribute_value &get_variable(const std::string &varname);
36  /** returns a blank attribute value if varname is no valid variable name. */
37  virtual config::attribute_value get_variable_const(const std::string& varname) const;
38  /** throws invalid_variablename_exception if varname is no valid variable name. */
39  config& get_variable_cfg(const std::string& varname);
40  /** does nothing if varname is no valid variable name. */
41  void set_variable(const std::string& varname, const t_string& value);
42  /** throws invalid_variablename_exception if varname is no valid variable name. */
43  config& add_variable_cfg(const std::string& varname, const config& value=config());
44  /** returns a variable_access that cannot be used to change the game variables */
45  variable_access_const get_variable_access_read(const std::string& varname) const
46  {
47  activate_scope_variable(varname);
48  return variable_access_const(varname, variables_);
49  }
50  /** returns a variable_access that can be used to change the game variables */
52  {
53  activate_scope_variable(varname);
54  return variable_access_create(varname, variables_);
55  }
56  /**
57  * Clears attributes config children
58  * does nothing if varname is no valid variable name.
59  */
60  void clear_variable(const std::string& varname);
61  /**
62  * Clears only the config children
63  * does nothing if varname is no valid variable name.
64  */
65  void clear_variable_cfg(const std::string& varname);
66 
67  const randomness::mt_rng& rng() const { return rng_; }
68  randomness::mt_rng& rng() { return rng_; }
69 
70  enum PHASE {
71  /// creating intitial [unit]s, executing toplevel [lua] etc.
72  /// next phase: PRELOAD
74  /// the preload [event] is fired
75  /// next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game), TURN_PLAYING (reloaded game) or GAME_ENDED (reloadedgame)
77  /// the prestart [event] is fired
78  /// next phase: START (default), GAME_ENDING
80  /// the start [event] is fired
81  /// next phase: TURN_STARTING_WAITING (default), GAME_ENDING
83  /// we are waiting for the turn to start.
84  /// The game can be saved here.
85  /// next phase: TURN_STARTING
87  /// the turn, side turn etc. [event]s are being fired
88  /// next phase: TURN_PLAYING (default), GAME_ENDING
90  /// The User is controlling the game and invoking actions
91  /// The game can be saved here.
92  /// next phase: TURN_PLAYING (default), GAME_ENDING
94  /// The turn_end, side_turn_end etc [events] are fired
95  /// next phase: TURN_STARTING_WAITING (default), GAME_ENDING
97  /// The victory etc. [event]s are fired.
98  /// next phase: GAME_ENDED
100  /// The game has ended and the user is observing the final state "lingering"
101  /// The game can be saved here.
103  };
104 
105  PHASE phase() const { return phase_; }
107  /// returns where there is currently a well defiend "current player",
108  /// that is for example not the case during start events or during linger mode.
109  bool has_current_player() const;
110  bool is_before_screen() const;
111  bool is_after_start() const;
112 
113  static PHASE read_phase(const config& cfg);
114  static void write_phase(config& cfg, game_data::PHASE phase);
115 
118  }
119  bool allow_end_turn() const { return can_end_turn_; }
120  void set_allow_end_turn(bool value, const t_string& reason = "") {
121  can_end_turn_ = value;
122  cannot_end_turn_reason_ = reason;
123  }
124 
125  /** the last location where a select event fired. Used by wml menu items with needs_select=yes*/
127 
128  void write_snapshot(config& cfg) const;
129 
130  const std::string& next_scenario() const { return next_scenario_; }
132 
133  const std::string& get_id() const { return id_; }
134  void set_id(const std::string& value) { id_ = value; }
135 
136  const std::string& get_theme() const { return theme_; }
137  void set_theme(const std::string& value) { theme_ = value; }
138 
139  const std::vector<std::string>& get_defeat_music() const { return defeat_music_; }
140  void set_defeat_music(std::vector<std::string> value) { defeat_music_ = std::move(value); }
141 
142  const std::vector<std::string>& get_victory_music() const { return victory_music_; }
143  void set_victory_music(std::vector<std::string> value) { victory_music_ = std::move(value); }
144 
145  void set_end_turn_forced(bool v) { end_turn_forced_ = v; }
146  bool end_turn_forced() const { return end_turn_forced_; }
147 private:
148  void activate_scope_variable(std::string var_name) const;
149  /** Used to delete variables. */
151  {
152  activate_scope_variable(varname);
153  return variable_access_throw(varname, variables_);
154  }
155 
162  /** the scenario coming next (for campaigns) */
163  std::string next_scenario_;
164  // the id of a scenario cannot change during a scenario
165  std::string id_;
166  std::string theme_;
167  std::vector<std::string> defeat_music_;
168  std::vector<std::string> victory_music_;
169 };
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:118
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:31
const std::string & get_id() const
Definition: game_data.hpp:133
PHASE phase_
Definition: game_data.hpp:158
bool allow_end_turn() const
Definition: game_data.hpp:119
t_string cannot_end_turn_reason_
Definition: game_data.hpp:161
virtual config::attribute_value get_variable_const(const std::string &varname) const
returns a blank attribute value if varname is no valid variable name.
Definition: game_data.cpp:71
const t_string & cannot_end_turn_reason()
Definition: game_data.hpp:116
void set_end_turn_forced(bool v)
Definition: game_data.hpp:145
void set_phase(PHASE phase)
Definition: game_data.hpp:106
bool is_before_screen() const
Definition: game_data.cpp:197
void clear_variable_cfg(const std::string &varname)
Clears only the config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:106
const config & get_variables() const
Definition: game_data.hpp:33
void set_defeat_music(std::vector< std::string > value)
Definition: game_data.hpp:140
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
Definition: game_data.hpp:76
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
Definition: game_data.hpp:73
@ GAME_ENDED
The game has ended and the user is observing the final state "lingering" The game can be saved here.
Definition: game_data.hpp:102
@ PRESTART
the prestart [event] is fired next phase: START (default), GAME_ENDING
Definition: game_data.hpp:79
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
Definition: game_data.hpp:93
@ TURN_ENDED
The turn_end, side_turn_end etc [events] are fired next phase: TURN_STARTING_WAITING (default),...
Definition: game_data.hpp:96
@ TURN_STARTING_WAITING
we are waiting for the turn to start.
Definition: game_data.hpp:86
@ START
the start [event] is fired next phase: TURN_STARTING_WAITING (default), GAME_ENDING
Definition: game_data.hpp:82
@ GAME_ENDING
The victory etc.
Definition: game_data.hpp:99
@ TURN_STARTING
the turn, side turn etc.
Definition: game_data.hpp:89
static PHASE read_phase(const config &cfg)
Definition: game_data.cpp:177
PHASE phase() const
Definition: game_data.hpp:105
bool can_end_turn_
Definition: game_data.hpp:159
const std::string & get_theme() const
Definition: game_data.hpp:136
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
Definition: game_data.hpp:150
void activate_scope_variable(std::string var_name) const
Definition: game_data.cpp:154
bool has_current_player() const
returns where there is currently a well defiend "current player", that is for example not the case du...
Definition: game_data.cpp:192
std::string id_
Definition: game_data.hpp:165
map_location last_selected
the last location where a select event fired.
Definition: game_data.hpp:126
variable_access_create get_variable_access_write(const std::string &varname)
returns a variable_access that can be used to change the game variables
Definition: game_data.hpp:51
std::string theme_
Definition: game_data.hpp:166
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:83
void set_theme(const std::string &value)
Definition: game_data.hpp:137
void set_allow_end_turn(bool value, const t_string &reason="")
Definition: game_data.hpp:120
bool end_turn_forced() const
Definition: game_data.hpp:146
bool end_turn_forced_
Definition: game_data.hpp:160
const randomness::mt_rng & rng() const
Definition: game_data.hpp:67
randomness::mt_rng & rng()
Definition: game_data.hpp:68
void set_next_scenario(const std::string &next_scenario)
Definition: game_data.hpp:131
void write_snapshot(config &cfg) const
Definition: game_data.cpp:130
variable_access_const get_variable_access_read(const std::string &varname) const
returns a variable_access that cannot be used to change the game variables
Definition: game_data.hpp:45
void set_victory_music(std::vector< std::string > value)
Definition: game_data.hpp:143
const std::vector< std::string > & get_victory_music() const
Definition: game_data.hpp:142
static void write_phase(config &cfg, game_data::PHASE phase)
Definition: game_data.cpp:207
randomness::mt_rng rng_
Definition: game_data.hpp:156
void set_id(const std::string &value)
Definition: game_data.hpp:134
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:66
const std::vector< std::string > & get_defeat_music() const
Definition: game_data.hpp:139
config & add_variable_cfg(const std::string &varname, const config &value=config())
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:100
config variables_
Definition: game_data.hpp:157
std::vector< std::string > victory_music_
Definition: game_data.hpp:168
game_data(const config &level)
Definition: game_data.cpp:34
std::string next_scenario_
the scenario coming next (for campaigns)
Definition: game_data.hpp:163
const std::string & next_scenario() const
Definition: game_data.hpp:130
std::vector< std::string > defeat_music_
Definition: game_data.hpp:167
bool is_after_start() const
Definition: game_data.cpp:202
void set_variable(const std::string &varname, const t_string &value)
does nothing if varname is no valid variable name.
Definition: game_data.cpp:88
Additional functionality for a non-const variable_info.
Information on a WML variable.
Contains the exception interfaces used to signal completion of a scenario, campaign or turn.
std::string_view data
Definition: picture.cpp:194
Encapsulates the map of the game.
Definition: location.hpp:38
variable_info< const variable_info_implementation::vi_policy_const > variable_access_const
Read-only access.
variable_info_mutable< variable_info_implementation::vi_policy_throw > variable_access_throw
'Throw if nonexistent' access.
variable_info_mutable< variable_info_implementation::vi_policy_create > variable_access_create
'Create if nonexistent' access.