The Battle for Wesnoth  1.17.0-dev
game_data.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 class t_string;
26 
27 class game_data : public variable_set {
28 public:
29  explicit game_data(const config& level);
30  game_data(const game_data& data);
31 
32  std::vector<scoped_wml_variable*> scoped_variables;
33 
34  const config& get_variables() const { return variables_; }
35  /** throws invalid_variablename_exception if varname is no valid variable name. */
36  config::attribute_value &get_variable(const std::string &varname);
37  /** returns a blank attribute value if varname is no valid variable name. */
38  virtual config::attribute_value get_variable_const(const std::string& varname) const;
39  /** throws invalid_variablename_exception if varname is no valid variable name. */
40  config& get_variable_cfg(const std::string& varname);
41  /** does nothing if varname is no valid variable name. */
42  void set_variable(const std::string& varname, const t_string& value);
43  /** throws invalid_variablename_exception if varname is no valid variable name. */
44  config& add_variable_cfg(const std::string& varname, const config& value=config());
45  /** returns a variable_access that cannot be used to change the game variables */
46  variable_access_const get_variable_access_read(const std::string& varname) const
47  {
48  activate_scope_variable(varname);
49  return variable_access_const(varname, variables_);
50  }
51  /** returns a variable_access that can be used to change the game variables */
53  {
54  activate_scope_variable(varname);
55  return variable_access_create(varname, variables_);
56  }
57  /**
58  * Clears attributes config children
59  * does nothing if varname is no valid variable name.
60  */
61  void clear_variable(const std::string& varname);
62  /**
63  * Clears only the config children
64  * does nothing if varname is no valid variable name.
65  */
66  void clear_variable_cfg(const std::string& varname);
67 
68  const randomness::mt_rng& rng() const { return rng_; }
69  randomness::mt_rng& rng() { return rng_; }
70 
71  enum PHASE {
77  };
78 
79  PHASE phase() const { return phase_; }
81 
84  }
85  bool allow_end_turn() const { return can_end_turn_; }
86  void set_allow_end_turn(bool value, const t_string& reason = "") {
87  can_end_turn_ = value;
88  cannot_end_turn_reason_ = reason;
89  }
90 
91  /** the last location where a select event fired. Used by wml menu items with needs_select=yes*/
93 
94  void write_snapshot(config& cfg) const;
95 
96  const std::string& next_scenario() const { return next_scenario_; }
98 
99  const std::string& get_id() const { return id_; }
100  void set_id(const std::string& value) { id_ = value; }
101 
102  const std::string& get_theme() const { return theme_; }
103  void set_theme(const std::string& value) { theme_ = value; }
104 
105  const std::vector<std::string>& get_defeat_music() const { return defeat_music_; }
106  void set_defeat_music(std::vector<std::string> value) { defeat_music_ = std::move(value); }
107 
108  const std::vector<std::string>& get_victory_music() const { return victory_music_; }
109  void set_victory_music(std::vector<std::string> value) { victory_music_ = std::move(value); }
110 
111 private:
112  void activate_scope_variable(std::string var_name) const;
113  /** Used to delete variables. */
115  {
116  activate_scope_variable(varname);
117  return variable_access_throw(varname, variables_);
118  }
119 
125  /** the scenario coming next (for campaigns) */
126  std::string next_scenario_;
127  // the id of a scenario cannot change during a scenario
128  std::string id_;
129  std::string theme_;
130  std::vector<std::string> defeat_music_;
131  std::vector<std::string> victory_music_;
132 };
variable_info_mutable< variable_info_implementation::vi_policy_throw > variable_access_throw
&#39;Throw if nonexistent&#39; access.
randomness::mt_rng & rng()
Definition: game_data.hpp:69
map_location last_selected
the last location where a select event fired.
Definition: game_data.hpp:92
Variant for storing WML attributes.
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:80
void set_next_scenario(const std::string &next_scenario)
Definition: game_data.hpp:97
const randomness::mt_rng & rng() const
Definition: game_data.hpp:68
bool allow_end_turn() const
Definition: game_data.hpp:85
const std::string & get_theme() const
Definition: game_data.hpp:102
Additional functionality for a non-const variable_info.
const std::string & get_id() const
Definition: game_data.hpp:99
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
const config & get_variables() const
Definition: game_data.hpp:34
bool can_end_turn_
Definition: game_data.hpp:123
Definitions for the interface to Wesnoth Markup Language (WML).
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:63
void set_phase(PHASE phase)
Definition: game_data.hpp:80
variable_info< const variable_info_implementation::vi_policy_const > variable_access_const
Read-only access.
void set_defeat_music(std::vector< std::string > value)
Definition: game_data.hpp:106
const t_string & cannot_end_turn_reason()
Definition: game_data.hpp:82
std::string next_scenario_
the scenario coming next (for campaigns)
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:52
void set_variable(const std::string &varname, const t_string &value)
does nothing if varname is no valid variable name.
Definition: game_data.cpp:85
std::string theme_
Definition: game_data.hpp:129
PHASE phase_
Definition: game_data.hpp:122
void set_id(const std::string &value)
Definition: game_data.hpp:100
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
Definition: game_data.hpp:114
void activate_scope_variable(std::string var_name) const
Definition: game_data.cpp:150
Encapsulates the map of the game.
Definition: location.hpp:38
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:32
void set_theme(const std::string &value)
Definition: game_data.hpp:103
t_string cannot_end_turn_reason_
Definition: game_data.hpp:124
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:103
const std::string & next_scenario() const
Definition: game_data.hpp:96
variable_info_mutable< variable_info_implementation::vi_policy_create > variable_access_create
&#39;Create if nonexistent&#39; access.
std::vector< std::string > defeat_music_
Definition: game_data.hpp:130
const std::vector< std::string > & get_defeat_music() const
Definition: game_data.hpp:105
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:68
Information on a WML variable.
void set_victory_music(std::vector< std::string > value)
Definition: game_data.hpp:109
std::vector< std::string > victory_music_
Definition: game_data.hpp:131
std::string id_
Definition: game_data.hpp:128
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:97
void set_allow_end_turn(bool value, const t_string &reason="")
Definition: game_data.hpp:86
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:115
void write_snapshot(config &cfg) const
Definition: game_data.cpp:127
PHASE phase() const
Definition: game_data.hpp:79
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
config variables_
Definition: game_data.hpp:121
randomness::mt_rng rng_
Definition: game_data.hpp:120
const std::vector< std::string > & get_victory_music() const
Definition: game_data.hpp:108
game_data(const config &level)
Definition: game_data.cpp:34
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:46