The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
game_data.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "global.hpp"
21 
22 #include "game_data.hpp"
23 
24 #include "log.hpp" //LOG_STREAM
25 #include "util.hpp" //lexical_cast
26 #include "variable.hpp" //scoped_wml_variable
27 
28 #include <boost/assign.hpp>
29 
30 static lg::log_domain log_engine("engine");
31 #define ERR_NG LOG_STREAM(err, log_engine)
32 #define WRN_NG LOG_STREAM(warn, log_engine)
33 #define LOG_NG LOG_STREAM(info, log_engine)
34 #define DBG_NG LOG_STREAM(debug, log_engine)
35 
37  : variable_set()
38  , scoped_variables()
39  , last_selected(map_location::null_location())
40  , rng_()
41  , variables_()
42  , phase_(INITIAL)
43  , can_end_turn_(true)
44  , next_scenario_()
45  {}
46 
48  : variable_set()
49  , scoped_variables()
50  , last_selected(map_location::null_location())
51  , rng_(level)
52  , variables_(level.child_or_empty("variables"))
53  , phase_(INITIAL)
54  , can_end_turn_(level["can_end_turn"].to_bool(true))
55  , next_scenario_(level["next_scenario"])
56 {
57 }
58 
60  : variable_set() // variable set is just an interface.
61  , scoped_variables(data.scoped_variables)
62  , last_selected(data.last_selected)
63  , rng_(data.rng_)
64  , variables_(data.variables_)
65  , phase_(data.phase_)
66  , can_end_turn_(data.can_end_turn_)
67  , next_scenario_(data.next_scenario_)
68 {}
69 //throws
71 {
73 }
74 
76 {
77  try
78  {
79  return get_variable_access_read(key).as_scalar();
80  }
81  catch(const invalid_variablename_exception&)
82  {
83  return config::attribute_value();
84  }
85 }
86 //throws
88 {
90 }
91 
93 {
94  try
95  {
96  get_variable(key) = value;
97  }
98  catch(const invalid_variablename_exception&)
99  {
100  ERR_NG << "variable " << key << "cannot be set to " << value << std::endl;
101  }
102 }
103 //throws
105 {
106  std::vector<config> temp = {value};
107  return *get_variable_access_write(key).append_array(temp).first;
108 }
109 
111 {
112  try
113  {
114  get_variable_access_throw(varname).clear(true);
115  }
116  catch(const invalid_variablename_exception&)
117  {
118  //variable doesn't exist, nothing to delete
119  }
120 }
121 
123 {
124  try
125  {
126  get_variable_access_throw(varname).clear(false);
127  }
128  catch(const invalid_variablename_exception&)
129  {
130  //variable doesn't exist, nothing to delete
131  }
132 }
133 
135 {
136  cfg["next_scenario"] = next_scenario_;
137 
138  cfg["can_end_turn"] = can_end_turn_;
139 
140  cfg["random_seed"] = rng_.get_random_seed_str();
141  cfg["random_calls"] = rng_.get_random_calls();
142 
143  cfg.add_child("variables", variables_);
144 
145 }
146 
147 namespace {
148  bool recursive_activation = false;
149 
150 } // end anonymous namespace
151 
153 {
154 
155  if(recursive_activation)
156  return;
157  const std::string::iterator itor = std::find(var_name.begin(),var_name.end(),'.');
158  if(itor != var_name.end()) {
159  var_name.erase(itor, var_name.end());
160  }
161  std::vector<scoped_wml_variable*>::const_reverse_iterator rit;
162  for(
163  rit = scoped_variables.rbegin();
164  rit != scoped_variables.rend();
165  ++rit) {
166  if((**rit).name() == var_name) {
167  recursive_activation = true;
168  if(!(**rit).activated()) {
169  (**rit).activate();
170  }
171  recursive_activation = false;
172  break;
173  }
174  }
175 }
variable_info_detail::maybe_const< vit, config::attribute_value >::type & as_scalar() const
might throw invalid_variablename_exception NOTE: If vit == vit_const, then the lifime of the returned...
void activate_scope_variable(std::string var_name) const
Definition: game_data.cpp:152
GLint level
Definition: glew.h:1220
void clear(bool only_tables=false) const
clears the vale this object points to if only_tables = true it will not clear attribute values...
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:87
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
bool can_end_turn_
Definition: game_data.hpp:104
std::string get_random_seed_str() const
Definition: mt_rng.cpp:99
Variant for storing WML attributes.
Definition: config.hpp:223
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:70
rand_rng::mt_rng rng_
Definition: game_data.hpp:101
unsigned int get_random_calls() const
Definition: mt_rng.hpp:59
std::string next_scenario_
the scenario coming next (for campaigns)
Definition: game_data.hpp:105
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:75
void write_snapshot(config &cfg) const
Definition: game_data.cpp:134
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:55
GLsizei const GLfloat * value
Definition: glew.h:1817
void set_variable(const std::string &varname, const t_string &value)
does nothing if varname is no valid variable name.
Definition: game_data.cpp:92
config & add_child(const std::string &key)
Definition: config.cpp:743
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
Definition: game_data.hpp:95
Templates and utility-routines for strings and numbers.
Encapsulates the map of the game.
Definition: location.hpp:38
variable_info_detail::maybe_const< vit, config >::type & as_container() const
might throw invalid_variablename_exception
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:35
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
Information on a WML variable.
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:110
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:49
#define ERR_NG
Definition: game_data.cpp:31
bool find(E event, F functor)
Tests whether an event handler is available.
Standard logging facilities (interface).
static lg::log_domain log_engine("engine")
config::child_itors append_array(std::vector< config > childs) const
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:104
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:122
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
config variables_
Definition: game_data.hpp:102
GLsizei const GLcharARB ** string
Definition: glew.h:4503