The Battle for Wesnoth  1.17.0-dev
game_data.cpp
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 /**
17  * @file
18  * Maintain game variables + misc.
19  */
20 
21 #include "game_data.hpp"
22 
23 #include "log.hpp" //LOG_STREAM
24 #include "variable.hpp" //scoped_wml_variable
26 #include "utils/ranges.hpp"
27 
28 static lg::log_domain log_engine("engine");
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 #define WRN_NG LOG_STREAM(warn, log_engine)
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 #define DBG_NG LOG_STREAM(debug, log_engine)
33 
35  : variable_set()
36  , scoped_variables()
37  , last_selected(map_location::null_location())
38  , rng_(level)
39  , variables_(level.child_or_empty("variables"))
40  , phase_(INITIAL)
41  , can_end_turn_(level["can_end_turn"].to_bool(true))
42  , cannot_end_turn_reason_(level["cannot_end_turn_reason"].t_str())
43  , next_scenario_(level["next_scenario"])
44  , id_(level["id"])
45  , theme_(level["theme"])
46  , defeat_music_(utils::split(level["defeat_music"]))
47  , victory_music_(utils::split(level["victory_music"]))
48 {
49 }
50 
52  : variable_set() // variable set is just an interface.
55  , rng_(data.rng_)
56  , variables_(data.variables_)
57  , phase_(data.phase_)
60 {
61 }
62 //throws
64 {
66 }
67 
69 {
70  try
71  {
72  return get_variable_access_read(key).as_scalar();
73  }
74  catch(const invalid_variablename_exception&)
75  {
76  return config::attribute_value();
77  }
78 }
79 //throws
80 config& game_data::get_variable_cfg(const std::string& key)
81 {
83 }
84 
85 void game_data::set_variable(const std::string& key, const t_string& value)
86 {
87  try
88  {
89  get_variable(key) = value;
90  }
91  catch(const invalid_variablename_exception&)
92  {
93  ERR_NG << "variable " << key << "cannot be set to " << value << std::endl;
94  }
95 }
96 //throws
97 config& game_data::add_variable_cfg(const std::string& key, const config& value)
98 {
99  std::vector<config> temp {value};
100  return get_variable_access_write(key).append_array(temp).front();
101 }
102 
103 void game_data::clear_variable_cfg(const std::string& varname)
104 {
105  try
106  {
107  get_variable_access_throw(varname).clear(true);
108  }
109  catch(const invalid_variablename_exception&)
110  {
111  //variable doesn't exist, nothing to delete
112  }
113 }
114 
115 void game_data::clear_variable(const std::string& varname)
116 {
117  try
118  {
119  get_variable_access_throw(varname).clear(false);
120  }
121  catch(const invalid_variablename_exception&)
122  {
123  //variable doesn't exist, nothing to delete
124  }
125 }
126 
128 {
129  cfg["next_scenario"] = next_scenario_;
130  cfg["id"] = id_;
131  cfg["theme"] = theme_;
132  cfg["defeat_music"] = utils::join(defeat_music_);
133  cfg["victory_music"] = utils::join(victory_music_);
134 
135  cfg["can_end_turn"] = can_end_turn_;
136  cfg["cannot_end_turn_reason"] = cannot_end_turn_reason_;
137 
138  cfg["random_seed"] = rng_.get_random_seed_str();
139  cfg["random_calls"] = rng_.get_random_calls();
140 
141  cfg.add_child("variables", variables_);
142 
143 }
144 
145 namespace {
146  bool recursive_activation = false;
147 
148 } // end anonymous namespace
149 
150 void game_data::activate_scope_variable(std::string var_name) const
151 {
152  if (recursive_activation) {
153  return;
154  }
155 
156  const std::string::iterator itor = std::find(var_name.begin(),var_name.end(),'.');
157  if(itor != var_name.end()) {
158  var_name.erase(itor, var_name.end());
159  }
160 
162  if (v->name() == var_name) {
163  recursive_activation = true;
164  if (!v->activated()) {
165  v->activate();
166  }
167  recursive_activation = false;
168  break;
169  }
170  }
171 }
map_location last_selected
the last location where a select event fired.
Definition: game_data.hpp:92
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
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
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
auto reversed_view(T &container)
Definition: ranges.hpp:28
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
bool can_end_turn_
Definition: game_data.hpp:123
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
std::string get_random_seed_str() const
Definition: mt_rng.cpp:100
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
config::child_itors append_array(std::vector< config > children) const
PHASE phase_
Definition: game_data.hpp:122
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
unsigned int get_random_calls() const
Definition: mt_rng.hpp:56
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:32
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
void clear(bool only_tables=false) const
Clears the value this object points to.
#define ERR_NG
Definition: game_data.cpp:29
config & add_child(config_key_type key)
Definition: config.cpp:514
std::vector< std::string > defeat_music_
Definition: game_data.hpp:130
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
std::vector< std::string > victory_music_
Definition: game_data.hpp:131
std::vector< std::string > split(const config_attribute_value &val)
Standard logging facilities (interface).
std::string id_
Definition: game_data.hpp:128
static lg::log_domain log_engine("engine")
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 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
config_attribute_value attribute_value
Variant for storing WML attributes.
Definition: config.hpp:215
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
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
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