The Battle for Wesnoth  1.19.0-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 "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 namespace game_events {struct queued_event;}
30 
31 namespace wfl
32 {
33 
34 /** An object representing the state of the game, providing access to the map and basic information */
36 {
37 public:
38  // Currently it accesses all its state through the resources namespace, so nothing is passed into it.
39 
40  void get_inputs(formula_input_vector& inputs) const override;
41  variant get_value(const std::string& key) const override;
42 };
43 
44 /** An object representing the state of the current event; equivalent to Lua's wesnoth.current.event_context */
46 {
47 public:
49 
50  void get_inputs(formula_input_vector& inputs) const override;
51  variant get_value(const std::string& key) const override;
52 private:
54  mutable std::shared_ptr<attack_type> first_weapon, second_weapon;
55 };
56 
58 {
59 public:
60  terrain_callable(const display_context& m, const map_location& loc);
61 
62  variant get_value(const std::string& key) const override;
63  void get_inputs(formula_input_vector& inputs) const override;
64 
65  int do_compare(const formula_callable* callable) const override;
66 
67 private:
69  const terrain_type& t_;
70  const int owner_;
71 };
72 
74 {
75 public:
77  {}
78 
79  void get_inputs(formula_input_vector& inputs) const override;
80  variant get_value(const std::string& key) const override;
81 
82  const gamemap& get_gamemap() const;
83 
84 private:
86 };
87 
89 {
90 public:
92  {
93  type_ = LOCATION_C;
94  }
95 
96  void serialize_to_string(std::string& str) const override;
97 
98  const map_location& loc() const { return loc_; }
99 
100 private:
102 
103  variant get_value(const std::string& key) const override;
104 
105  void get_inputs(formula_input_vector& inputs) const override;
106  int do_compare(const formula_callable* callable) const override;
107 };
108 
110 {
111 public:
112  explicit attack_type_callable(const attack_type& attack);
113 
114  variant get_value(const std::string& key) const override;
115  void get_inputs(formula_input_vector& inputs) const override;
116 
117  int do_compare(const formula_callable* callable) const override;
118 
119  const attack_type& get_attack_type() const { return *att_; }
120 
121 private:
123 };
124 
126 {
127 public:
128  unit_callable(const map_location& loc, const unit& u) : loc_(loc), u_(u)
129  {
130  type_ = UNIT_C;
131  }
132 
133  explicit unit_callable(const unit &u);
134 
135  variant get_value(const std::string& key) const override;
136  void get_inputs(formula_input_vector& inputs) const override;
137 
138  int do_compare(const formula_callable* callable) const override;
139 
140  const unit& get_unit() const { return u_; }
141  const map_location& get_location() const { return loc_; }
142 
143 private:
145  const unit& u_;
146 };
147 
149 {
150 public:
151  explicit unit_type_callable(const unit_type& u) : u_(u)
152  {
153  type_ = UNIT_TYPE_C;
154  }
155 
156  variant get_value(const std::string& key) const override;
157  void get_inputs(formula_input_vector& inputs) const override;
158 
159  int do_compare(const formula_callable* callable) const override;
160 
161  const unit_type& get_unit_type() const { return u_; }
162 
163 private:
164  const unit_type& u_;
165 };
166 
168 {
169 public:
170  explicit config_callable(const config& c) : cfg_(c) {}
171 
172  variant get_value(const std::string& key) const override;
173  void get_inputs(formula_input_vector& inputs) const override;
174  int do_compare(const formula_callable* callable) const override;
175 
176  const config& get_config() const { return cfg_; }
177 
178 private:
179  const config& cfg_;
180 };
181 
183 {
184 public:
185  explicit team_callable(const team& t) : team_(t) {}
186 
187  void get_inputs(formula_input_vector& inputs) const override;
188  variant get_value(const std::string& key) const override;
189 
190  const team& get_team() const { return team_; }
191 
192 private:
193  const team& team_;
194 };
195 
197 {
198 public:
199  set_var_callable(const std::string& key, const variant& value)
200  : key_(key), value_(value)
201  {}
202 
203  const std::string& key() const { return key_; }
204  variant value() const { return value_; }
205  variant execute_self(variant ctxt) override;
206 
207 private:
208  std::string key_;
210  variant get_value(const std::string& key) const override;
211 
212  void get_inputs(formula_input_vector& inputs) const override;
213 };
214 
216 {
217 
218 public:
220  : main_(main)
221  , backup_()
222  , backup_formula_(backup)
223  {}
224 
225  const variant& get_main() const { return main_; }
226  const expression_ptr& get_backup() const { return backup_formula_; }
227 
228  void set_backup_result(const variant& v)
229  {
230  backup_ = v;
231  }
232 
233  variant execute_self(variant ctxt) override;
234 
235 private:
239  variant get_value(const std::string& key) const override;
240 
241  void get_inputs(formula_input_vector& inputs) const override;
242 };
243 
245 {
246 public:
248  : failed_callable_(callable)
250  , status_(status)
251  {}
252 
253 private:
256  const int status_;
257 
258  variant get_value(const std::string& key) const override;
259 
260  void get_inputs(formula_input_vector& inputs) const override;
261 };
262 
263 } // 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:159
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: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: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
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 **argv)
Definition: sdl2.cpp:19
Encapsulates the map of the game.
Definition: location.hpp:38
mock_char c