The Battle for Wesnoth  1.19.23+dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2025
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 "color.hpp"
19 #include "formula/callable.hpp"
20 #include "formula/formula.hpp"
21 #include "map/location.hpp"
22 #include "units/ptr.hpp"
23 
24 class display_context;
25 class gamemap;
26 class team;
27 class terrain_type;
28 class unit;
29 class unit_type;
30 namespace game_events {struct queued_event;}
31 
32 namespace wfl
33 {
34 
35 /** An object representing the state of the game, providing access to the map and basic information */
37 {
38 public:
39  // Currently it accesses all its state through the resources namespace, so nothing is passed into it.
40 
41  void get_inputs(formula_input_vector& inputs) const override;
42  variant get_value(const std::string& key) const override;
43 };
44 
45 /** An object representing the state of the current event; equivalent to Lua's wesnoth.current.event_context */
47 {
48 public:
50 
51  void get_inputs(formula_input_vector& inputs) const override;
52  variant get_value(const std::string& key) const override;
53 private:
55  mutable std::shared_ptr<attack_type> first_weapon, second_weapon;
56 };
57 
59 {
60 public:
62 
63  variant get_value(const std::string& key) const override;
64  void get_inputs(formula_input_vector& inputs) const override;
65 
66  int do_compare(const formula_callable* callable) const override;
67 
68  const terrain_type& get_terrain_type() const { return t_; }
69 
70 private:
72  const terrain_type& t_;
73  const int owner_;
74 };
75 
77 {
78 public:
80  {}
81 
82  void get_inputs(formula_input_vector& inputs) const override;
83  variant get_value(const std::string& key) const override;
84 
85  const gamemap& get_gamemap() const;
86 
87 private:
89 };
90 
92 {
93 public:
95  {
96  type_ = LOCATION_C;
97  }
98 
99  /** Inherited from formula_callable. */
100  std::string serialize_to_string() const override;
101 
102  const map_location& loc() const { return loc_; }
103 
104 private:
106 
107  variant get_value(const std::string& key) const override;
108 
109  void get_inputs(formula_input_vector& inputs) const override;
110  int do_compare(const formula_callable* callable) const override;
111 };
112 
114 {
115 public:
116  explicit attack_type_callable(const attack_type& attack);
117 
118  variant get_value(const std::string& key) const override;
119  void get_inputs(formula_input_vector& inputs) const override;
120 
121  int do_compare(const formula_callable* callable) const override;
122 
123  const attack_type& get_attack_type() const { return *att_; }
124 
125 private:
127 };
128 
130 {
131 public:
132  unit_callable(const map_location& loc, const unit& u) : loc_(loc), u_(u)
133  {
134  type_ = UNIT_C;
135  }
136 
137  explicit unit_callable(const unit &u);
138 
139  variant get_value(const std::string& key) const override;
140  void get_inputs(formula_input_vector& inputs) const override;
141 
142  int do_compare(const formula_callable* callable) const override;
143 
144  const unit& get_unit() const { return u_; }
145  const map_location& get_location() const { return loc_; }
146 
147 private:
149  const unit& u_;
150 };
151 
153 {
154 public:
155  explicit unit_type_callable(const unit_type& u) : u_(u)
156  {
157  type_ = UNIT_TYPE_C;
158  }
159 
160  variant get_value(const std::string& key) const override;
161  void get_inputs(formula_input_vector& inputs) const override;
162 
163  int do_compare(const formula_callable* callable) const override;
164 
165  const unit_type& get_unit_type() const { return u_; }
166 
167 private:
168  const unit_type& u_;
169 };
170 
172 {
173 public:
174  explicit config_callable(const config& c) : cfg_(c) {}
175 
176  variant get_value(const std::string& key) const override;
177  void get_inputs(formula_input_vector& inputs) const override;
178  int do_compare(const formula_callable* callable) const override;
179 
180  const config& get_config() const { return cfg_; }
181 
182 private:
183  const config& cfg_;
184 };
185 
187 {
188 public:
189  explicit team_callable(const team& t) : team_(t) {}
190 
191  void get_inputs(formula_input_vector& inputs) const override;
192  variant get_value(const std::string& key) const override;
193 
194  const team& get_team() const { return team_; }
195 
196 private:
197  const team& team_;
198 };
199 
200 
202 {
203 public:
205  : clr_(clr)
206  {}
207 
208  void get_inputs(formula_input_vector& inputs) const override;
209  variant get_value(const std::string& key) const override;
210 
211  const color_t get_color() const { return clr_; }
212 
213 private:
215 };
216 
217 
218 
220 {
221 public:
222  set_var_callable(const std::string& key, const variant& value)
223  : key_(key), value_(value)
224  {}
225 
226  const std::string& key() const { return key_; }
227  variant value() const { return value_; }
228  variant execute_self(variant ctxt) override;
229 
230 private:
231  std::string key_;
233  variant get_value(const std::string& key) const override;
234 
235  void get_inputs(formula_input_vector& inputs) const override;
236 };
237 
239 {
240 
241 public:
243  : main_(main)
244  , backup_()
245  , backup_formula_(backup)
246  {}
247 
248  const variant& get_main() const { return main_; }
249  const expression_ptr& get_backup() const { return backup_formula_; }
250 
251  void set_backup_result(const variant& v)
252  {
253  backup_ = v;
254  }
255 
256  variant execute_self(variant ctxt) override;
257 
258 private:
262  variant get_value(const std::string& key) const override;
263 
264  void get_inputs(formula_input_vector& inputs) const override;
265 };
266 
268 {
269 public:
271  : failed_callable_(callable)
273  , status_(status)
274  {}
275 
276 private:
279  const int status_;
280 
281  variant get_value(const std::string& key) const override;
282 
283  void get_inputs(formula_input_vector& inputs) const override;
284 };
285 
286 } // namespace wfl
map_location loc
Definition: move.cpp:172
double t
Definition: astarsearch.cpp:63
double g
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
Encapsulates the map of the game.
Definition: map.hpp:176
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
A single unit type that the player may recruit.
Definition: types.hpp:43
This class represents a single unit of a specific type.
Definition: unit.hpp:39
const attack_type & get_attack_type() const
int do_compare(const formula_callable *callable) const override
variant get_value(const std::string &key) const override
attack_type_callable(const attack_type &attack)
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
const color_t get_color() const
void get_inputs(formula_input_vector &inputs) const override
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
int do_compare(const formula_callable *callable) const override
const config & get_config() const
config_callable(const config &c)
An object representing the state of the current event; equivalent to Lua's wesnoth....
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
std::shared_ptr< attack_type > second_weapon
const game_events::queued_event & event_info
std::shared_ptr< attack_type > first_weapon
event_callable(const game_events::queued_event &event)
formula_input_vector inputs() const
Definition: callable.hpp:63
const display_context & board_
const gamemap & get_gamemap() const
void get_inputs(formula_input_vector &inputs) const override
gamemap_callable(const display_context &g)
variant get_value(const std::string &key) const override
An object representing the state of the game, providing access to the map and basic information.
variant get_value(const std::string &key) const override
void get_inputs(formula_input_vector &inputs) const override
location_callable(const map_location &loc)
const map_location & loc() const
void get_inputs(formula_input_vector &inputs) const override
std::string serialize_to_string() const override
Inherited from formula_callable.
int do_compare(const formula_callable *callable) const override
variant get_value(const std::string &key) const override
const expression_ptr & get_backup() const
void set_backup_result(const variant &v)
variant execute_self(variant ctxt) override
safe_call_callable(const variant &main, const expression_ptr &backup)
variant get_value(const std::string &key) const override
const variant & get_main() const
void get_inputs(formula_input_vector &inputs) const override
const map_location current_unit_location_
void get_inputs(formula_input_vector &inputs) const override
const_formula_callable_ptr failed_callable_
safe_call_result(const_formula_callable_ptr callable, int status, const map_location &loc=map_location())
variant get_value(const std::string &key) const override
void get_inputs(formula_input_vector &inputs) const override
variant get_value(const std::string &key) const override
const std::string & key() const
variant execute_self(variant ctxt) override
set_var_callable(const std::string &key, const variant &value)
team_callable(const team &t)
void get_inputs(formula_input_vector &inputs) const override
const team & get_team() const
variant get_value(const std::string &key) const override
variant get_value(const std::string &key) const override
const terrain_type & t_
terrain_callable(const display_context &m, const map_location &loc)
const terrain_type & get_terrain_type() const
void get_inputs(formula_input_vector &inputs) const override
const map_location loc_
int do_compare(const formula_callable *callable) const override
variant get_value(const std::string &key) const override
const map_location & loc_
const unit & get_unit() const
void get_inputs(formula_input_vector &inputs) const override
unit_callable(const map_location &loc, const unit &u)
const map_location & get_location() const
int do_compare(const formula_callable *callable) const override
variant get_value(const std::string &key) const override
unit_type_callable(const unit_type &u)
int do_compare(const formula_callable *callable) const override
void get_inputs(formula_input_vector &inputs) const override
const unit_type & get_unit_type() const
Domain specific events.
Definition: callable.hpp:26
std::vector< formula_input > formula_input_vector
std::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:29
std::shared_ptr< const formula_callable > const_formula_callable_ptr
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
int main(int, char **)
Definition: sdl2.cpp:25
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:51
Encapsulates the map of the game.
Definition: location.hpp:46
mock_char c