The Battle for Wesnoth  1.15.2+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 
26 #include <boost/range/adaptor/reversed.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  , next_scenario_(level["next_scenario"])
43  , id_(level["id"])
44  , theme_(level["theme"])
45  , defeat_music_(utils::split(level["defeat_music"]))
46  , victory_music_(utils::split(level["victory_music"]))
47 {
48 }
49 
51  : variable_set() // variable set is just an interface.
54  , rng_(data.rng_)
55  , variables_(data.variables_)
56  , phase_(data.phase_)
59 {
60 }
61 //throws
63 {
65 }
66 
68 {
69  try
70  {
71  return get_variable_access_read(key).as_scalar();
72  }
73  catch(const invalid_variablename_exception&)
74  {
75  return config::attribute_value();
76  }
77 }
78 //throws
79 config& game_data::get_variable_cfg(const std::string& key)
80 {
82 }
83 
84 void game_data::set_variable(const std::string& key, const t_string& value)
85 {
86  try
87  {
88  get_variable(key) = value;
89  }
90  catch(const invalid_variablename_exception&)
91  {
92  ERR_NG << "variable " << key << "cannot be set to " << value << std::endl;
93  }
94 }
95 //throws
96 config& game_data::add_variable_cfg(const std::string& key, const config& value)
97 {
98  std::vector<config> temp {value};
99  return get_variable_access_write(key).append_array(temp).front();
100 }
101 
102 void game_data::clear_variable_cfg(const std::string& varname)
103 {
104  try
105  {
106  get_variable_access_throw(varname).clear(true);
107  }
108  catch(const invalid_variablename_exception&)
109  {
110  //variable doesn't exist, nothing to delete
111  }
112 }
113 
114 void game_data::clear_variable(const std::string& varname)
115 {
116  try
117  {
118  get_variable_access_throw(varname).clear(false);
119  }
120  catch(const invalid_variablename_exception&)
121  {
122  //variable doesn't exist, nothing to delete
123  }
124 }
125 
127 {
128  cfg["next_scenario"] = next_scenario_;
129  cfg["id"] = id_;
130  cfg["theme"] = theme_;
131  cfg["defeat_music"] = utils::join(defeat_music_);
132  cfg["victory_music"] = utils::join(victory_music_);
133 
134  cfg["can_end_turn"] = can_end_turn_;
135 
136  cfg["random_seed"] = rng_.get_random_seed_str();
137  cfg["random_calls"] = rng_.get_random_calls();
138 
139  cfg.add_child("variables", variables_);
140 
141 }
142 
143 namespace {
144  bool recursive_activation = false;
145 
146 } // end anonymous namespace
147 
148 void game_data::activate_scope_variable(std::string var_name) const
149 {
150  if (recursive_activation) {
151  return;
152  }
153 
154  const std::string::iterator itor = std::find(var_name.begin(),var_name.end(),'.');
155  if(itor != var_name.end()) {
156  var_name.erase(itor, var_name.end());
157  }
158 
160  if (v->name() == var_name) {
161  recursive_activation = true;
162  if (!v->activated()) {
163  v->activate();
164  }
165  recursive_activation = false;
166  break;
167  }
168  }
169 }
map_location last_selected
the last location where a select event fired.
Definition: game_data.hpp:89
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:79
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...
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:120
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
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
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: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
config::child_itors append_array(std::vector< config > children) const
PHASE phase_
Definition: game_data.hpp:119
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
unsigned int get_random_calls() const
Definition: mt_rng.hpp:55
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:33
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
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:476
std::vector< std::string > defeat_music_
Definition: game_data.hpp:127
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
std::vector< std::string > victory_music_
Definition: game_data.hpp:128
bool find(E event, F functor)
Tests whether an event handler is available.
Standard logging facilities (interface).
std::string id_
Definition: game_data.hpp:125
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:96
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:193
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
config_attribute_value attribute_value
Variant for storing WML attributes.
Definition: config.hpp:219
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
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
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