The Battle for Wesnoth  1.17.0-dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 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 #pragma once
17 
18 #include "formula/callable.hpp"
19 #include "formula/formula.hpp"
20 #include "map/location.hpp"
21 #include "units/ptr.hpp"
22 
23 class display_context;
24 class gamemap;
25 class team;
26 class terrain_type;
27 class unit;
28 class unit_type;
29 
30 namespace wfl
31 {
32 
34 {
35 public:
36  terrain_callable(const display_context& m, const map_location& loc);
37 
38  variant get_value(const std::string& key) const override;
39  void get_inputs(formula_input_vector& inputs) const override;
40 
41  int do_compare(const formula_callable* callable) const override;
42 
43 private:
45  const terrain_type& t_;
46  const int owner_;
47 };
48 
50 {
51 public:
52  explicit gamemap_callable(const display_context& g) : board_(g)
53  {}
54 
55  void get_inputs(formula_input_vector& inputs) const override;
56  variant get_value(const std::string& key) const override;
57 
58  const gamemap& get_gamemap() const;
59 
60 private:
62 };
63 
65 {
66 public:
67  explicit location_callable(const map_location& loc) : loc_(loc)
68  {
69  type_ = LOCATION_C;
70  }
71 
72  void serialize_to_string(std::string& str) const override;
73 
74  const map_location& loc() const { return loc_; }
75 
76 private:
78 
79  variant get_value(const std::string& key) const override;
80 
81  void get_inputs(formula_input_vector& inputs) const override;
82  int do_compare(const formula_callable* callable) const override;
83 };
84 
86 {
87 public:
88  explicit attack_type_callable(const attack_type& attack);
89 
90  variant get_value(const std::string& key) const override;
91  void get_inputs(formula_input_vector& inputs) const override;
92 
93  int do_compare(const formula_callable* callable) const override;
94 
95  const attack_type& get_attack_type() const { return *att_; }
96 
97 private:
99 };
100 
102 {
103 public:
104  unit_callable(const map_location& loc, const unit& u) : loc_(loc), u_(u)
105  {
106  type_ = UNIT_C;
107  }
108 
109  explicit unit_callable(const unit &u);
110 
111  variant get_value(const std::string& key) const override;
112  void get_inputs(formula_input_vector& inputs) const override;
113 
114  int do_compare(const formula_callable* callable) const override;
115 
116  const unit& get_unit() const { return u_; }
117  const map_location& get_location() const { return loc_; }
118 
119 private:
121  const unit& u_;
122 };
123 
125 {
126 public:
127  explicit unit_type_callable(const unit_type& u) : u_(u)
128  {
129  type_ = UNIT_TYPE_C;
130  }
131 
132  variant get_value(const std::string& key) const override;
133  void get_inputs(formula_input_vector& inputs) const override;
134 
135  int do_compare(const formula_callable* callable) const override;
136 
137  const unit_type& get_unit_type() const { return u_; }
138 
139 private:
140  const unit_type& u_;
141 };
142 
144 {
145 public:
146  explicit config_callable(const config& c) : cfg_(c) {}
147 
148  variant get_value(const std::string& key) const override;
149  void get_inputs(formula_input_vector& inputs) const override;
150  int do_compare(const formula_callable* callable) const override;
151 
152  const config& get_config() const { return cfg_; }
153 
154 private:
155  const config& cfg_;
156 };
157 
159 {
160 public:
161  explicit team_callable(const team& t) : team_(t) {}
162 
163  void get_inputs(formula_input_vector& inputs) const override;
164  variant get_value(const std::string& key) const override;
165 
166  const team& get_team() const { return team_; }
167 
168 private:
169  const team& team_;
170 };
171 
173 {
174 public:
175  set_var_callable(const std::string& key, const variant& value)
176  : key_(key), value_(value)
177  {}
178 
179  const std::string& key() const { return key_; }
180  variant value() const { return value_; }
181  variant execute_self(variant ctxt) override;
182 
183 private:
184  std::string key_;
186  variant get_value(const std::string& key) const override;
187 
188  void get_inputs(formula_input_vector& inputs) const override;
189 };
190 
192 {
193 
194 public:
196  : main_(main)
197  , backup_()
198  , backup_formula_(backup)
199  {}
200 
201  const variant& get_main() const { return main_; }
202  const expression_ptr& get_backup() const { return backup_formula_; }
203 
204  void set_backup_result(const variant& v)
205  {
206  backup_ = v;
207  }
208 
209  variant execute_self(variant ctxt) override;
210 
211 private:
215  variant get_value(const std::string& key) const override;
216 
217  void get_inputs(formula_input_vector& inputs) const override;
218 };
219 
221 {
222 public:
224  : failed_callable_(callable)
225  , current_unit_location_(loc)
226  , status_(status)
227  {}
228 
229 private:
232  const int status_;
233 
234  variant get_value(const std::string& key) const override;
235 
236  void get_inputs(formula_input_vector& inputs) const override;
237 };
238 
239 } // namespace wfl
const map_location & loc() const
set_var_callable(const std::string &key, const variant &value)
void get_inputs(formula_input_vector &inputs) const override
safe_call_result(const_formula_callable_ptr callable, int status, const map_location &loc=map_location())
This class represents a single unit of a specific type.
Definition: unit.hpp:121
team_callable(const team &t)
const display_context & board_
const unit_type & get_unit_type() const
std::vector< formula_input > formula_input_vector
config_callable(const config &c)
unit_type_callable(const unit_type &u)
const expression_ptr & get_backup() const
std::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:29
int main(int argc, char **argv)
Definition: SDLMain.mm:115
A single unit type that the player may recruit.
Definition: types.hpp:45
const attack_type & get_attack_type() const
const_formula_callable_ptr failed_callable_
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
virtual void serialize_to_string(std::string &) const
Definition: callable.hpp:161
void set_backup_result(const variant &v)
gamemap_callable(const display_context &g)
const config & get_config() const
Encapsulates the map of the game.
Definition: map.hpp:171
const map_location & loc_
Encapsulates the map of the game.
Definition: location.hpp:38
int do_compare(const formula_callable *callable) const override
const variant & get_main() const
variant get_value(const std::string &key) const override
double g
Definition: astarsearch.cpp:65
const map_location loc_
const std::string & key() const
unit_callable(const map_location &loc, const unit &u)
const terrain_type & t_
std::shared_ptr< const formula_callable > const_formula_callable_ptr
location_callable(const map_location &loc)
formula_input_vector inputs() const
Definition: callable.hpp:63
const map_location & get_location() const
double t
Definition: astarsearch.cpp:65
Definition: contexts.hpp:44
terrain_callable(const display_context &m, const map_location &loc)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
const map_location current_unit_location_
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
safe_call_callable(const variant &main, const expression_ptr &backup)
const unit & get_unit() const
const team & get_team() const