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