The Battle for Wesnoth  1.19.5+dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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:
61  terrain_callable(const display_context& m, const map_location& loc);
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 private:
70  const terrain_type& t_;
71  const int owner_;
72 };
73 
75 {
76 public:
78  {}
79 
80  void get_inputs(formula_input_vector& inputs) const override;
81  variant get_value(const std::string& key) const override;
82 
83  const gamemap& get_gamemap() const;
84 
85 private:
87 };
88 
90 {
91 public:
93  {
94  type_ = LOCATION_C;
95  }
96 
97  void serialize_to_string(std::string& str) const override;
98 
99  const map_location& loc() const { return loc_; }
100 
101 private:
103 
104  variant get_value(const std::string& key) const override;
105 
106  void get_inputs(formula_input_vector& inputs) const override;
107  int do_compare(const formula_callable* callable) const override;
108 };
109 
111 {
112 public:
113  explicit attack_type_callable(const attack_type& attack);
114 
115  variant get_value(const std::string& key) const override;
116  void get_inputs(formula_input_vector& inputs) const override;
117 
118  int do_compare(const formula_callable* callable) const override;
119 
120  const attack_type& get_attack_type() const { return *att_; }
121 
122 private:
124 };
125 
127 {
128 public:
129  unit_callable(const map_location& loc, const unit& u) : loc_(loc), u_(u)
130  {
131  type_ = UNIT_C;
132  }
133 
134  explicit unit_callable(const unit &u);
135 
136  variant get_value(const std::string& key) const override;
137  void get_inputs(formula_input_vector& inputs) const override;
138 
139  int do_compare(const formula_callable* callable) const override;
140 
141  const unit& get_unit() const { return u_; }
142  const map_location& get_location() const { return loc_; }
143 
144 private:
146  const unit& u_;
147 };
148 
150 {
151 public:
152  explicit unit_type_callable(const unit_type& u) : u_(u)
153  {
154  type_ = UNIT_TYPE_C;
155  }
156 
157  variant get_value(const std::string& key) const override;
158  void get_inputs(formula_input_vector& inputs) const override;
159 
160  int do_compare(const formula_callable* callable) const override;
161 
162  const unit_type& get_unit_type() const { return u_; }
163 
164 private:
165  const unit_type& u_;
166 };
167 
169 {
170 public:
171  explicit config_callable(const config& c) : cfg_(c) {}
172 
173  variant get_value(const std::string& key) const override;
174  void get_inputs(formula_input_vector& inputs) const override;
175  int do_compare(const formula_callable* callable) const override;
176 
177  const config& get_config() const { return cfg_; }
178 
179 private:
180  const config& cfg_;
181 };
182 
184 {
185 public:
186  explicit team_callable(const team& t) : team_(t) {}
187 
188  void get_inputs(formula_input_vector& inputs) const override;
189  variant get_value(const std::string& key) const override;
190 
191  const team& get_team() const { return team_; }
192 
193 private:
194  const team& team_;
195 };
196 
197 
199 {
200 public:
202  : clr_(clr)
203  {}
204 
205  void get_inputs(formula_input_vector& inputs) const override;
206  variant get_value(const std::string& key) const override;
207 
208  const color_t get_color() const { return clr_; }
209 
210 private:
212 };
213 
214 
215 
217 {
218 public:
219  set_var_callable(const std::string& key, const variant& value)
220  : key_(key), value_(value)
221  {}
222 
223  const std::string& key() const { return key_; }
224  variant value() const { return value_; }
225  variant execute_self(variant ctxt) override;
226 
227 private:
228  std::string key_;
230  variant get_value(const std::string& key) const override;
231 
232  void get_inputs(formula_input_vector& inputs) const override;
233 };
234 
236 {
237 
238 public:
240  : main_(main)
241  , backup_()
242  , backup_formula_(backup)
243  {}
244 
245  const variant& get_main() const { return main_; }
246  const expression_ptr& get_backup() const { return backup_formula_; }
247 
248  void set_backup_result(const variant& v)
249  {
250  backup_ = v;
251  }
252 
253  variant execute_self(variant ctxt) override;
254 
255 private:
259  variant get_value(const std::string& key) const override;
260 
261  void get_inputs(formula_input_vector& inputs) const override;
262 };
263 
265 {
266 public:
268  : failed_callable_(callable)
270  , status_(status)
271  {}
272 
273 private:
276  const int status_;
277 
278  variant get_value(const std::string& key) const override;
279 
280  void get_inputs(formula_input_vector& inputs) const override;
281 };
282 
283 } // namespace wfl
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:172
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:172
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
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:133
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)
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: contexts.hpp:43
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:59
Encapsulates the map of the game.
Definition: location.hpp:45
mock_char c