The Battle for Wesnoth  1.19.18+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  void serialize_to_string(std::string& str) const override;
100 
101  const map_location& loc() const { return loc_; }
102 
103 private:
105 
106  variant get_value(const std::string& key) const override;
107 
108  void get_inputs(formula_input_vector& inputs) const override;
109  int do_compare(const formula_callable* callable) const override;
110 };
111 
113 {
114 public:
115  explicit attack_type_callable(const attack_type& attack);
116 
117  variant get_value(const std::string& key) const override;
118  void get_inputs(formula_input_vector& inputs) const override;
119 
120  int do_compare(const formula_callable* callable) const override;
121 
122  const attack_type& get_attack_type() const { return *att_; }
123 
124 private:
126 };
127 
129 {
130 public:
131  unit_callable(const map_location& loc, const unit& u) : loc_(loc), u_(u)
132  {
133  type_ = UNIT_C;
134  }
135 
136  explicit unit_callable(const unit &u);
137 
138  variant get_value(const std::string& key) const override;
139  void get_inputs(formula_input_vector& inputs) const override;
140 
141  int do_compare(const formula_callable* callable) const override;
142 
143  const unit& get_unit() const { return u_; }
144  const map_location& get_location() const { return loc_; }
145 
146 private:
148  const unit& u_;
149 };
150 
152 {
153 public:
154  explicit unit_type_callable(const unit_type& u) : u_(u)
155  {
156  type_ = UNIT_TYPE_C;
157  }
158 
159  variant get_value(const std::string& key) const override;
160  void get_inputs(formula_input_vector& inputs) const override;
161 
162  int do_compare(const formula_callable* callable) const override;
163 
164  const unit_type& get_unit_type() const { return u_; }
165 
166 private:
167  const unit_type& u_;
168 };
169 
171 {
172 public:
173  explicit config_callable(const config& c) : cfg_(c) {}
174 
175  variant get_value(const std::string& key) const override;
176  void get_inputs(formula_input_vector& inputs) const override;
177  int do_compare(const formula_callable* callable) const override;
178 
179  const config& get_config() const { return cfg_; }
180 
181 private:
182  const config& cfg_;
183 };
184 
186 {
187 public:
188  explicit team_callable(const team& t) : team_(t) {}
189 
190  void get_inputs(formula_input_vector& inputs) const override;
191  variant get_value(const std::string& key) const override;
192 
193  const team& get_team() const { return team_; }
194 
195 private:
196  const team& team_;
197 };
198 
199 
201 {
202 public:
204  : clr_(clr)
205  {}
206 
207  void get_inputs(formula_input_vector& inputs) const override;
208  variant get_value(const std::string& key) const override;
209 
210  const color_t get_color() const { return clr_; }
211 
212 private:
214 };
215 
216 
217 
219 {
220 public:
221  set_var_callable(const std::string& key, const variant& value)
222  : key_(key), value_(value)
223  {}
224 
225  const std::string& key() const { return key_; }
226  variant value() const { return value_; }
227  variant execute_self(variant ctxt) override;
228 
229 private:
230  std::string key_;
232  variant get_value(const std::string& key) const override;
233 
234  void get_inputs(formula_input_vector& inputs) const override;
235 };
236 
238 {
239 
240 public:
242  : main_(main)
243  , backup_()
244  , backup_formula_(backup)
245  {}
246 
247  const variant& get_main() const { return main_; }
248  const expression_ptr& get_backup() const { return backup_formula_; }
249 
250  void set_backup_result(const variant& v)
251  {
252  backup_ = v;
253  }
254 
255  variant execute_self(variant ctxt) override;
256 
257 private:
261  variant get_value(const std::string& key) const override;
262 
263  void get_inputs(formula_input_vector& inputs) const override;
264 };
265 
267 {
268 public:
270  : failed_callable_(callable)
272  , status_(status)
273  {}
274 
275 private:
278  const int status_;
279 
280  variant get_value(const std::string& key) const override;
281 
282  void get_inputs(formula_input_vector& inputs) const override;
283 };
284 
285 } // 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:173
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 serialize_to_string(std::string &str) const override
void get_inputs(formula_input_vector &inputs) const override
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:61
Encapsulates the map of the game.
Definition: location.hpp:46
mock_char c