The Battle for Wesnoth  1.17.12+dev
callable_objects.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2022
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:
48  event_callable(const game_events::queued_event& event) : event_info(event) {}
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:
76  explicit gamemap_callable(const display_context& g) : board_(g)
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:
91  explicit location_callable(const map_location& loc) : loc_(loc)
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)
249  , current_unit_location_(loc)
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
const map_location & loc() const
set_var_callable(const std::string &key, const variant &value)
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:133
event_callable(const game_events::queued_event &event)
int main(int, char **argv)
Definition: sdl2.cpp:5
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
A single unit type that the player may recruit.
Definition: types.hpp:45
map_location loc_
const game_events::queued_event & event_info
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:75
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
std::shared_ptr< attack_type > second_weapon
const map_location & loc_
Encapsulates the map of the game.
Definition: location.hpp:38
Domain specific events.
An object representing the state of the game, providing access to the map and basic information...
const variant & get_main() const
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)
const map_location & get_location() const
double t
Definition: astarsearch.cpp:65
An object representing the state of the current event; equivalent to Lua&#39;s wesnoth.current.event_context.
Definition: contexts.hpp:44
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
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