The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 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 #ifndef CALLABLE_OBJECTS_HPP_INCLUDED
17 #define CALLABLE_OBJECTS_HPP_INCLUDED
18 
19 #include "formula/callable.hpp"
20 #include "map/map.hpp"
21 #include "team.hpp"
22 #include "units/unit.hpp"
23 
24 #define CALLABLE_WRAPPER_START(klass) \
25 class klass##_callable : public game_logic::formula_callable { \
26  const klass& object_; \
27 public: \
28  explicit klass##_callable(const klass& object) : object_(object) \
29  {} \
30  \
31  const klass& get_##klass() const { return object_; } \
32  void get_inputs(std::vector<game_logic::formula_input>* inputs) const \
33  { \
34  using game_logic::FORMULA_READ_ONLY;
35 
36 #define CALLABLE_WRAPPER_INPUT(VAR) \
37  inputs->push_back(game_logic::formula_input(#VAR, FORMULA_READ_ONLY));
38 
39 #define CALLABLE_WRAPPER_INPUT_END \
40  } \
41  \
42  variant get_value(const std::string& key) const {
43 
44 #define CALLABLE_WRAPPER_VAR(VAR) \
45  if(key == #VAR) { \
46  return variant(object_.VAR); \
47  } else
48 
49 #define CALLABLE_WRAPPER_FN2(VAR, FN) \
50  if(key == #VAR) { \
51  return variant(object_.FN()); \
52  } else
53 
54 #define CALLABLE_WRAPPER_FN(VAR) CALLABLE_WRAPPER_FN2(VAR, VAR)
55 
56 #define CALLABLE_WRAPPER_END \
57  { return variant(); } \
58  } \
59 };
60 
61 template <typename T, typename K> variant convert_map( const std::map<T,K>& map );
62 
63 template <typename T> variant convert_vector( const std::vector<T>& input_vector );
64 
65 
67 public:
69  terrain_callable(const terrain_type& t, const location& loc)
70  : loc_(loc), t_(t)
71  {
72  type_ = TERRAIN_C;
73  }
74 
75  variant get_value(const std::string& key) const;
76  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
77 
78  int do_compare(const formula_callable* callable) const;
79 private:
80  const location loc_;
81  const terrain_type &t_;
82 };
83 
89  if(key == "terrain") {
90  int w = object_.w();
91  int h = object_.h();
92  std::vector<variant> vars;
93  for(int i = 0;i < w; i++) {
94  for(int j = 0;j < h; j++) {
95  const map_location loc(i,j);
96  vars.push_back(variant(new terrain_callable(object_.get_terrain_info(loc), loc)));
97  }
98  }
99  return variant(&vars);
100  } else
104 
105 class location_callable : public game_logic::formula_callable {
107 
108  variant get_value(const std::string& key) const;
109 
110  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
111  int do_compare(const game_logic::formula_callable* callable) const;
112 public:
113  explicit location_callable(const map_location& loc) : loc_(loc)
114  {
115  type_ = LOCATION_C;
116  }
117  explicit location_callable(int x, int y) : loc_(map_location(x,y))
118  {}
119 
120  const map_location& loc() const { return loc_; }
121 
122  void serialize_to_string(std::string& str) const;
123 };
124 
125 
127 public:
130  : att_(attack)
131  {
133  }
134 
135  const attack_type& get_attack_type() const { return att_; }
136  variant get_value(const std::string& key) const;
137  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
138 
139  int do_compare(const formula_callable* callable) const;
140 private:
142 };
143 
144 
146 public:
148  unit_callable(const location& loc, const unit& u)
149  : loc_(loc), u_(u)
150  {
151  type_ = UNIT_C;
152  }
153 
154  unit_callable(const unit &u)
155  : loc_(u.get_location()), u_(u)
156  {
157  type_ = UNIT_C;
158  }
159 
160  const unit& get_unit() const { return u_; }
161  const location& get_location() const { return loc_; }
162  variant get_value(const std::string& key) const;
163  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
164 
165  int do_compare(const formula_callable* callable) const;
166 private:
167  const location& loc_;
168  const unit& u_;
169 };
170 
171 
173 public:
175  : u_(u)
176  {
177  type_ = UNIT_TYPE_C;
178  }
179 
180  const unit_type& get_unit_type() const { return u_; }
181  variant get_value(const std::string& key) const;
182  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
183 
184  int do_compare(const formula_callable* callable) const;
185 private:
186  const unit_type& u_;
187 };
188 
190 public:
191  config_callable(const config& c) : cfg_(c) {}
192  variant get_value(const std::string& key) const;
193  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
194  int do_compare(const formula_callable* callable) const;
195  const config& get_config() const {return cfg_;}
196 private:
197  const config& cfg_;
198 };
199 
200 
205 CALLABLE_WRAPPER_INPUT(start_gold)
207 CALLABLE_WRAPPER_INPUT(total_income)
212 CALLABLE_WRAPPER_INPUT(is_human)
214 CALLABLE_WRAPPER_INPUT(is_network)
220 CALLABLE_WRAPPER_INPUT(team_name)
221 CALLABLE_WRAPPER_INPUT(faction)
222 CALLABLE_WRAPPER_INPUT(faction_name)
224 CALLABLE_WRAPPER_INPUT(share_vision)
225 CALLABLE_WRAPPER_INPUT(carryover_bonus)
226 CALLABLE_WRAPPER_INPUT(carryover_percentage)
227 CALLABLE_WRAPPER_INPUT(carryover_add)
228 CALLABLE_WRAPPER_INPUT(recruit)
229 CALLABLE_WRAPPER_INPUT(wml_vars)
232 CALLABLE_WRAPPER_FN2(id, save_id)
233 CALLABLE_WRAPPER_FN(save_id)
235  if(key == "start_gold") {
236  return variant(lexical_cast<int>(object_.start_gold()));
237  } else
239 CALLABLE_WRAPPER_FN(total_income)
243 CALLABLE_WRAPPER_FN2(is_human, is_local_human)
244 CALLABLE_WRAPPER_FN2(is_ai, is_local_ai)
245 CALLABLE_WRAPPER_FN(is_network)
246 CALLABLE_WRAPPER_FN2(fog, uses_fog)
247 CALLABLE_WRAPPER_FN2(shroud, uses_shroud)
248 CALLABLE_WRAPPER_FN(hidden)
251 CALLABLE_WRAPPER_FN(team_name)
253 CALLABLE_WRAPPER_FN2(share_vision, share_vision().to_string)
254 CALLABLE_WRAPPER_FN(carryover_bonus)
255 CALLABLE_WRAPPER_FN(carryover_percentage)
256 CALLABLE_WRAPPER_FN(carryover_add)
257  if(key == "recruit") {
258  const std::set<std::string>& recruits = object_.recruits();
259  std::vector<variant> result;
260  for(std::set<std::string>::const_iterator it = recruits.begin(); it != recruits.end(); ++it) {
261  result.push_back(variant(*it));
262  }
263  return variant(&result);
264  } else if(key == "wml_vars") {
265  return variant(new config_callable(object_.variables()));
266  } else
268 
269 #endif
variant get_value(const std::string &key) const
location_callable(int x, int y)
const attack_type att_
int village_support
Definition: game_config.cpp:39
Definition: unit.hpp:95
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const GLfloat * c
Definition: glew.h:12741
int do_compare(const formula_callable *callable) const
#define CALLABLE_WRAPPER_END
unit_callable(const location &loc, const unit &u)
variant get_value(const std::string &key) const
int do_compare(const formula_callable *callable) const
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
const map_location & loc() const
#define CALLABLE_WRAPPER_INPUT(VAR)
#define h
variant get_value(const std::string &key) const
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
#define CALLABLE_WRAPPER_INPUT_END
-file sdl_utils.hpp
GLdouble GLdouble t
Definition: glew.h:1366
const unit & get_unit() const
variant convert_vector(const std::vector< T > &input_vector)
std::string flag_icon
Definition: game_config.cpp:84
map_location loc_
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
map_location location
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
config_callable(const config &c)
GLuint64EXT * result
Definition: glew.h:10727
map_location location
const unit_type & u_
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
const unit_type & get_unit_type() const
const attack_type & get_attack_type() const
variant get_value(const std::string &key) const
Encapsulates the map of the game.
Definition: map.hpp:37
const terrain_type & t_
variant get_value(const std::string &key) const
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
formula_callable(bool has_self=true)
Definition: callable.hpp:36
GLuint color
Definition: glew.h:5801
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
Encapsulates the map of the game.
Definition: location.hpp:38
const config & get_config() const
const location & get_location() const
variant convert_map(const std::map< T, K > &map)
#define CALLABLE_WRAPPER_START(klass)
unit_type_callable(const unit_type &u)
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
int do_compare(const formula_callable *callable) const
attack_type_callable(const attack_type &attack)
GLuint const GLchar * name
Definition: glew.h:1782
const location & loc_
#define CALLABLE_WRAPPER_FN(VAR)
CALLABLE_WRAPPER_INPUT_END if(key=="terrain")
location_callable(const map_location &loc)
const config & cfg_
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
int do_compare(const formula_callable *callable) const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
#define CALLABLE_WRAPPER_FN2(VAR, FN)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_callable(const unit &u)
const location loc_
terrain_callable(const terrain_type &t, const location &loc)
int do_compare(const formula_callable *callable) const