The Battle for Wesnoth  1.15.13+dev
game_data.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
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_end_exceptions.hpp"
19 #include "map/location.hpp"
20 #include "mt_rng.hpp"
21 #include "variable_info.hpp"
22 
24 class t_string;
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 {
76  };
77 
78  PHASE phase() const { return phase_; }
80 
83  }
84  bool allow_end_turn() const { return can_end_turn_; }
85  void set_allow_end_turn(bool value, const t_string& reason = "") {
86  can_end_turn_ = value;
87  cannot_end_turn_reason_ = reason;
88  }
89 
90  /** the last location where a select event fired. Used by wml menu items with needs_select=yes*/
92 
93  void write_snapshot(config& cfg) const;
94 
95  const std::string& next_scenario() const { return next_scenario_; }
97 
98  const std::string& get_id() const { return id_; }
99  void set_id(const std::string& value) { id_ = value; }
100 
101  const std::string& get_theme() const { return theme_; }
102  void set_theme(const std::string& value) { theme_ = value; }
103 
104  const std::vector<std::string>& get_defeat_music() const { return defeat_music_; }
105  void set_defeat_music(std::vector<std::string> value) { defeat_music_ = std::move(value); }
106 
107  const std::vector<std::string>& get_victory_music() const { return victory_music_; }
108  void set_victory_music(std::vector<std::string> value) { victory_music_ = std::move(value); }
109 
110 private:
111  void activate_scope_variable(std::string var_name) const;
112  /** Used to delete variables. */
114  {
115  activate_scope_variable(varname);
116  return variable_access_throw(varname, variables_);
117  }
118 
124  /** the scenario coming next (for campaigns) */
125  std::string next_scenario_;
126  // the id of a scenario cannot change during a scenario
127  std::string id_;
128  std::string theme_;
129  std::vector<std::string> defeat_music_;
130  std::vector<std::string> victory_music_;
131 };
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:68
map_location last_selected
the last location where a select event fired.
Definition: game_data.hpp:91
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:78
void set_next_scenario(const std::string &next_scenario)
Definition: game_data.hpp:96
const randomness::mt_rng & rng() const
Definition: game_data.hpp:67
bool allow_end_turn() const
Definition: game_data.hpp:84
const std::string & get_theme() const
Definition: game_data.hpp:101
Additional functionality for a non-const variable_info.
const std::string & get_id() const
Definition: game_data.hpp:98
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
const config & get_variables() const
Definition: game_data.hpp:33
bool can_end_turn_
Definition: game_data.hpp:122
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:61
void set_phase(PHASE phase)
Definition: game_data.hpp:79
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:105
const t_string & cannot_end_turn_reason()
Definition: game_data.hpp:81
std::string next_scenario_
the scenario coming next (for campaigns)
Definition: game_data.hpp:125
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
void set_variable(const std::string &varname, const t_string &value)
does nothing if varname is no valid variable name.
Definition: game_data.cpp:83
std::string theme_
Definition: game_data.hpp:128
PHASE phase_
Definition: game_data.hpp:121
void set_id(const std::string &value)
Definition: game_data.hpp:99
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
Definition: game_data.hpp:113
void activate_scope_variable(std::string var_name) const
Definition: game_data.cpp:147
Encapsulates the map of the game.
Definition: location.hpp:37
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:31
void set_theme(const std::string &value)
Definition: game_data.hpp:102
t_string cannot_end_turn_reason_
Definition: game_data.hpp:123
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:101
const std::string & next_scenario() const
Definition: game_data.hpp:95
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:129
const std::vector< std::string > & get_defeat_music() const
Definition: game_data.hpp:104
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:66
Information on a WML variable.
void set_victory_music(std::vector< std::string > value)
Definition: game_data.hpp:108
std::vector< std::string > victory_music_
Definition: game_data.hpp:130
std::string id_
Definition: game_data.hpp:127
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:95
void set_allow_end_turn(bool value, const t_string &reason="")
Definition: game_data.hpp:85
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:113
void write_snapshot(config &cfg) const
Definition: game_data.cpp:125
PHASE phase() const
Definition: game_data.hpp:78
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
config variables_
Definition: game_data.hpp:120
randomness::mt_rng rng_
Definition: game_data.hpp:119
const std::vector< std::string > & get_victory_music() const
Definition: game_data.hpp:107
game_data(const config &level)
Definition: game_data.cpp:33
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