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