The Battle for Wesnoth  1.15.11+dev
callable_objects.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "formula/callable_objects.hpp"
16 
17 #include "config.hpp"
18 #include "formula/function.hpp"
19 #include "map/map.hpp"
20 #include "display_context.hpp"
21 #include "team.hpp"
22 #include "units/attack_type.hpp"
24 #include "units/unit.hpp"
25 #include "units/types.hpp"
26 #include "log.hpp"
27 
28 static lg::log_domain log_scripting_formula("scripting/formula");
29 #define LOG_SF LOG_STREAM(info, log_scripting_formula)
30 #define ERR_SF LOG_STREAM(err, log_scripting_formula)
31 
32 namespace wfl
33 {
34 
35 variant location_callable::get_value(const std::string& key) const
36 {
37  if(key == "x") {
38  return variant(loc_.wml_x());
39  } else if(key == "y") {
40  return variant(loc_.wml_y());
41  }
42 
43  return variant();
44 }
45 
47 {
48  add_input(inputs, "x");
49  add_input(inputs, "y");
50 }
51 
53 {
54  const location_callable* loc_callable = dynamic_cast<const location_callable*>(callable);
55  if(loc_callable == nullptr) {
56  return formula_callable::do_compare(callable);
57  }
58 
59  const map_location& other_loc = loc_callable->loc();
60  return loc_.do_compare(other_loc);
61 }
62 
63 void location_callable::serialize_to_string(std::string& str) const
64 {
65  std::ostringstream s;
66  s << "loc(" << (loc_.wml_x()) << "," << (loc_.wml_y()) << ")";
67  str += s.str();
68 }
69 
70 attack_type_callable::attack_type_callable(const attack_type& attack) : att_(attack.shared_from_this())
71 {
73 }
74 
75 variant attack_type_callable::get_value(const std::string& key) const
76 {
77  if(key == "id" || key == "name") {
78  return variant(att_->id());
79  } else if(key == "description") {
80  return variant(att_->name());
81  } else if(key == "type") {
82  return variant(att_->type());
83  } else if(key == "icon") {
84  return variant(att_->icon());
85  } else if(key == "range") {
86  return variant(att_->range());
87  } else if(key == "damage") {
88  return variant(att_->damage());
89  } else if(key == "number_of_attacks" || key == "number" || key == "num_attacks" || key == "attacks") {
90  return variant(att_->num_attacks());
91  } else if(key == "attack_weight") {
92  return variant(att_->attack_weight(), variant::DECIMAL_VARIANT);
93  } else if(key == "defense_weight") {
94  return variant(att_->defense_weight(), variant::DECIMAL_VARIANT);
95  } else if(key == "accuracy") {
96  return variant(att_->accuracy());
97  } else if(key == "parry") {
98  return variant(att_->parry());
99  } else if(key == "movement_used") {
100  return variant(att_->movement_used());
101  } else if(key == "specials" || key == "special") {
102  std::vector<variant> res;
103 
104  for(const auto& special : att_->specials().all_children_range()) {
105  if(!special.cfg["id"].empty()) {
106  res.emplace_back(special.cfg["id"].str());
107  }
108  }
109  return variant(res);
110  }
111 
112  return variant();
113 }
114 
116 {
117  add_input(inputs, "name");
118  add_input(inputs, "type");
119  add_input(inputs, "description");
120  add_input(inputs, "icon");
121  add_input(inputs, "range");
122  add_input(inputs, "damage");
123  add_input(inputs, "number");
124  add_input(inputs, "accuracy");
125  add_input(inputs, "parry");
126  add_input(inputs, "movement_used");
127  add_input(inputs, "attack_weight");
128  add_input(inputs, "defense_weight");
129  add_input(inputs, "specials");
130 }
131 
133 {
134  const attack_type_callable* att_callable = dynamic_cast<const attack_type_callable*>(callable);
135  if(att_callable == nullptr) {
136  return formula_callable::do_compare(callable);
137  }
138 
139  if(att_->damage() != att_callable->att_->damage()) {
140  return att_->damage() - att_callable->att_->damage();
141  }
142 
143  if(att_->num_attacks() != att_callable->att_->num_attacks()) {
144  return att_->num_attacks() - att_callable->att_->num_attacks();
145  }
146 
147  if(att_->id() != att_callable->att_->id()) {
148  return att_->id().compare(att_callable->att_->id());
149  }
150 
151  if(att_->type() != att_callable->att_->type()) {
152  return att_->type().compare(att_callable->att_->type());
153  }
154 
155  if(att_->range() != att_callable->att_->range()) {
156  return att_->range().compare(att_callable->att_->range());
157  }
158 
159  return att_->weapon_specials().compare(att_callable->att_->weapon_specials());
160 }
161 
163 {
164  type_ = UNIT_C;
165 }
166 
167 variant unit_callable::get_value(const std::string& key) const
168 {
169  if(key == "x") {
171  return variant();
172  }
173 
174  return variant(loc_.wml_x());
175  } else if(key == "y") {
177  return variant();
178  }
179 
180  return variant(loc_.wml_y());
181  } else if(key == "loc") {
183  return variant();
184  }
185 
186  return variant(std::make_shared<location_callable>(loc_));
187  } else if(key == "id") {
188  return variant(u_.id());
189  } else if(key == "type") {
190  return variant(u_.type_id());
191  } else if(key == "name") {
192  return variant(u_.name());
193  } else if(key == "usage") {
194  return variant(u_.usage());
195  } else if(key == "leader" || key == "canrecruit") {
196  return variant(u_.can_recruit());
197  } else if(key == "undead") {
198  return variant(u_.get_state("not_living") ? 1 : 0);
199  } else if(key == "attacks") {
200  std::vector<variant> res;
201  for(const attack_type& att : u_.attacks()) {
202  res.emplace_back(std::make_shared<attack_type_callable>(att));
203  }
204 
205  return variant(res);
206  } else if(key == "abilities") {
208  } else if(key == "hitpoints") {
209  return variant(u_.hitpoints());
210  } else if(key == "max_hitpoints") {
211  return variant(u_.max_hitpoints());
212  } else if(key == "experience") {
213  return variant(u_.experience());
214  } else if(key == "max_experience") {
215  return variant(u_.max_experience());
216  } else if(key == "level" || key == "full") {
217  // This allows writing "upkeep == full"
218  return variant(u_.level());
219  } else if(key == "total_movement" || key == "max_moves") {
220  return variant(u_.total_movement());
221  } else if(key == "movement_left" || key == "moves") {
222  return variant(u_.movement_left());
223  } else if(key == "attacks_left") {
224  return variant(u_.attacks_left());
225  } else if(key == "max_attacks") {
226  return variant(u_.max_attacks());
227  } else if(key == "traits") {
229  } else if(key == "extra_recruit") {
231  } else if(key == "advances_to") {
233  } else if(key == "states" || key == "status") {
235  } else if(key == "side") {
236  return variant(u_.side()-1);
237  } else if(key == "cost") {
238  return variant(u_.cost());
239  } else if(key == "upkeep") {
240  return variant(u_.upkeep());
241  } else if(key == "loyal") {
242  // So we can write "upkeep == loyal"
243  return variant(0);
244  } else if(key == "hidden") {
245  return variant(u_.get_hidden());
246  } else if(key == "petrified") {
247  return variant(u_.incapacitated());
248  } else if(key == "resting") {
249  return variant(u_.resting());
250  } else if(key == "role") {
251  return variant(u_.get_role());
252  } else if(key == "race") {
253  return variant(u_.race()->id());
254  } else if(key == "gender") {
255  return variant(gender_string(u_.gender()));
256  } else if(key == "variation") {
257  return variant(u_.variation());
258  } else if(key == "zoc") {
259  return variant(u_.get_emit_zoc());
260  } else if(key == "alignment") {
261  return variant(u_.alignment().to_string());
262  } else if(key == "facing") {
264  } else if(key == "resistance" || key == "movement_cost" || key == "vision_cost" || key == "jamming_cost" || key == "defense") {
265  const auto& mt = u_.movement_type();
266  config cfg;
267  bool needs_flip = false;
268  if(key == "resistance") {
269  mt.get_resistances().write(cfg);
270  needs_flip = true;
271  } else if(key == "movement_cost") {
272  mt.get_movement().write(cfg);
273  } else if(key == "vision_cost") {
274  mt.get_vision().write(cfg);
275  } else if(key == "jamming_cost") {
276  mt.get_vision().write(cfg);
277  } else if(key == "defense") {
278  mt.get_defense().write(cfg);
279  needs_flip = true;
280  }
281  std::map<variant, variant> res;
282  for(const auto& p : cfg.attribute_range()) {
283  int val = p.second;
284  if(needs_flip) {
285  val = 100 - val;
286  }
287  res.emplace(variant(p.first), variant(val));
288  }
289 
290  return variant(res);
291  } else if(key == "flying") {
292  return variant(u_.is_flying());
293  } else if(key == "vars") {
296  }
297 
298  return variant();
299  } else if(key == "wml_vars") {
300  return variant(std::make_shared<config_callable>(u_.variables()));
301  } else if(key == "n" || key == "s" || key == "ne" || key == "se" || key == "nw" || key == "sw" ||
302  key == "lawful" || key == "neutral" || key == "chaotic" || key == "liminal" ||
303  key == "male" || key == "female")
304  {
305  return variant(key);
306  }
307 
308  return variant();
309 }
310 
312 {
313  add_input(inputs, "x");
314  add_input(inputs, "y");
315  add_input(inputs, "loc");
316  add_input(inputs, "id");
317  add_input(inputs, "type");
318  add_input(inputs, "name");
319  add_input(inputs, "canrecruit");
320  add_input(inputs, "undead");
321  add_input(inputs, "traits");
322  add_input(inputs, "attacks");
323  add_input(inputs, "abilities");
324  add_input(inputs, "hitpoints");
325  add_input(inputs, "max_hitpoints");
326  add_input(inputs, "experience");
327  add_input(inputs, "max_experience");
328  add_input(inputs, "level");
329  add_input(inputs, "moves");
330  add_input(inputs, "max_moves");
331  add_input(inputs, "attacks_left");
332  add_input(inputs, "max_attacks");
333  add_input(inputs, "side");
334  add_input(inputs, "extra_recruit");
335  add_input(inputs, "advances_to");
336  add_input(inputs, "status");
337  add_input(inputs, "cost");
338  add_input(inputs, "usage");
339  add_input(inputs, "upkeep");
340  add_input(inputs, "hidden");
341  add_input(inputs, "petrified");
342  add_input(inputs, "resting");
343  add_input(inputs, "role");
344  add_input(inputs, "race");
345  add_input(inputs, "gender");
346  add_input(inputs, "variation");
347  add_input(inputs, "zoc");
348  add_input(inputs, "alignment");
349  add_input(inputs, "facing");
350  add_input(inputs, "resistance");
351  add_input(inputs, "movement_cost");
352  add_input(inputs, "vision_cost");
353  add_input(inputs, "jamming_cost");
354  add_input(inputs, "defense");
355  add_input(inputs, "flying");
356  add_input(inputs, "vars");
357  add_input(inputs, "wml_vars");
358 }
359 
360 int unit_callable::do_compare(const formula_callable* callable) const
361 {
362  const unit_callable* u_callable = dynamic_cast<const unit_callable*>(callable);
363  if(u_callable == nullptr) {
364  return formula_callable::do_compare(callable);
365  }
366 
367  return u_.underlying_id() - u_callable->u_.underlying_id();
368 }
369 
370 variant unit_type_callable::get_value(const std::string& key) const
371 {
372  if(key == "id") {
373  return variant(u_.id());
374  } else if(key == "type") {
375  return variant(u_.type_name());
376  } else if(key == "alignment") {
377  return variant(u_.alignment().to_string());
378  } else if(key == "race") {
379  return variant(u_.race_id());
380  } else if(key == "abilities") {
382  } else if(key == "traits") {
383  std::vector<variant> res;
384  for(const auto& config : u_.possible_traits()) {
385  res.emplace_back(config["id"].str());
386  }
387 
388  return variant(res);
389  } else if(key == "attacks") {
390  std::vector<variant> res;
391  for(const attack_type& att : u_.attacks()) {
392  res.emplace_back(std::make_shared<attack_type_callable>(att));
393  }
394 
395  return variant(res);
396  } else if(key == "hitpoints" || key == "max_hitpoints") {
397  return variant(u_.hitpoints());
398  } else if(key == "experience" || key == "max_experience") {
399  return variant(u_.experience_needed(true));
400  } else if(key == "level") {
401  return variant(u_.level());
402  } else if(key == "total_movement" || key == "max_moves" || key == "moves") {
403  return variant(u_.movement());
404  } else if(key == "unpoisonable") {
405  return variant(u_.musthave_status("unpoisonable"));
406  } else if(key == "undrainable") {
407  return variant(u_.musthave_status("undrainable"));
408  } else if(key == "unplagueable") {
409  return variant(u_.musthave_status("unplagueable"));
410  } else if(key == "cost") {
411  return variant(u_.cost());
412  } else if(key == "recall_cost") {
413  return variant(u_.recall_cost());
414  } else if(key == "usage") {
415  return variant(u_.usage());
416  }
417 
418  return variant();
419 }
420 
422 {
423  add_input(inputs, "id");
424  add_input(inputs, "type");
425  add_input(inputs, "race");
426  add_input(inputs, "alignment");
427  add_input(inputs, "abilities");
428  add_input(inputs, "traits");
429  add_input(inputs, "attacks");
430  add_input(inputs, "hitpoints");
431  add_input(inputs, "experience");
432  add_input(inputs, "level");
433  add_input(inputs, "total_movement");
434  add_input(inputs, "undead");
435  add_input(inputs, "cost");
436  add_input(inputs, "recall_cost");
437  add_input(inputs, "usage");
438 }
439 
441 {
442  const unit_type_callable* u_callable = dynamic_cast<const unit_type_callable*>(callable);
443  if(u_callable == nullptr) {
444  return formula_callable::do_compare(callable);
445  }
446 
447  return u_.id().compare(u_callable->u_.id());
448 }
449 
451 #ifdef USING_BOOST_VARIANT
452  : public boost::static_visitor<variant>
453 #endif
454 {
455  variant operator()(bool b) const { return variant(b ? 1 : 0); }
456  variant operator()(int i) const { return variant(i); }
457  variant operator()(unsigned long long i) const { return variant(i); }
458  variant operator()(double i) const { return variant(i * 1000, variant::DECIMAL_VARIANT); }
459  // TODO: Should comma-separated lists of stuff be returned as a list?
460  // The challenge is to distinguish them from ordinary strings that happen to contain a comma
461  // (or should we assume that such strings will be translatable?).
462  variant operator()(const std::string& s) const { return variant(s); }
463  variant operator()(const t_string& s) const { return variant(s.str()); }
464  variant operator()(utils::monostate) const { return variant(); }
465 };
466 
467 variant config_callable::get_value(const std::string& key) const
468 {
469  if(cfg_.has_attribute(key)) {
470  return cfg_[key].apply_visitor(fai_variant_visitor());
471  } else if(cfg_.has_child(key)) {
472  std::vector<variant> result;
473  for(const auto& child : cfg_.child_range(key)) {
474  result.emplace_back(std::make_shared<config_callable>(child));
475  }
476 
477  return variant(result);
478  } else if(key == "__all_children") {
479  std::vector<variant> result;
480  for(const auto& child : cfg_.all_children_range()) {
481  const variant cfg_child(std::make_shared<config_callable>(child.cfg));
482  const variant kv(std::make_shared<key_value_pair>(variant(child.key), cfg_child));
483  result.push_back(kv);
484  }
485 
486  return variant(result);
487  } else if(key == "__children") {
488  std::map<std::string, std::vector<variant>> build;
489  for(const auto& child : cfg_.all_children_range()) {
490  const variant cfg_child(std::make_shared<config_callable>(child.cfg));
491  build[child.key].push_back(cfg_child);
492  }
493 
494  std::map<variant,variant> result;
495  for(auto& p : build) {
496  result[variant(p.first)] = variant(p.second);
497  }
498 
499  return variant(result);
500  } else if(key == "__attributes") {
501  std::map<variant,variant> result;
502  for(const auto& val : cfg_.attribute_range()) {
503  result[variant(val.first)] = val.second.apply_visitor(fai_variant_visitor());
504  }
505 
506  return variant(result);
507  }
508 
509  return variant();
510 }
511 
513 {
514  add_input(inputs, "__all_children");
515  add_input(inputs, "__children");
516  add_input(inputs, "__attributes");
517 
518  for(const auto& val : cfg_.attribute_range()) {
519  if(val.first.find_first_not_of(formula::id_chars) != std::string::npos) {
520  add_input(inputs, val.first);
521  }
522  }
523 }
524 
526 {
527  const config_callable* cfg_callable = dynamic_cast<const config_callable*>(callable);
528  if(cfg_callable == nullptr) {
529  return formula_callable::do_compare(callable);
530  }
531 
532  if(cfg_ == cfg_callable->get_config()) {
533  return 0;
534  }
535 
536  return cfg_.hash().compare(cfg_callable->get_config().hash());
537 }
538 
539 terrain_callable::terrain_callable(const display_context& dc, const map_location& loc) : loc_(loc), t_(dc.map().get_terrain_info(loc)), owner_(dc.village_owner(loc) - 1)
540 {
541  type_ = TERRAIN_C;
542 }
543 
544 variant terrain_callable::get_value(const std::string& key) const
545 {
546  if(key == "x") {
547  return variant(loc_.wml_x());
548  } else if(key == "y") {
549  return variant(loc_.wml_y());
550  } else if(key == "loc") {
551  return variant(std::make_shared<location_callable>(loc_));
552  } else if(key == "id") {
553  return variant(std::string(t_.id()));
554  } else if(key == "name") {
555  return variant(t_.name());
556  } else if(key == "editor_name") {
557  return variant(t_.editor_name());
558  } else if(key == "description") {
559  return variant(t_.description());
560  } else if(key == "icon") {
561  return variant(t_.icon_image());
562  } else if(key == "light") {
563  return variant(t_.light_bonus(0));
564  } else if(key == "village") {
565  return variant(t_.is_village());
566  } else if(key == "castle") {
567  return variant(t_.is_castle());
568  } else if(key == "keep") {
569  return variant(t_.is_keep());
570  } else if(key == "healing") {
571  return variant(t_.gives_healing());
572  } else if(key == "owner") {
573  return variant(owner_);
574  }
575 
576  return variant();
577 }
578 
580 {
581  add_input(inputs, "x");
582  add_input(inputs, "y");
583  add_input(inputs, "loc");
584  add_input(inputs, "id");
585  add_input(inputs, "name");
586  add_input(inputs, "editor_name");
587  add_input(inputs, "description");
588  add_input(inputs, "icon");
589  add_input(inputs, "light");
590  add_input(inputs, "village");
591  add_input(inputs, "castle");
592  add_input(inputs, "keep");
593  add_input(inputs, "healing");
594  add_input(inputs, "owner");
595 }
596 
598 {
599  const terrain_callable* terr_callable = dynamic_cast<const terrain_callable*>(callable);
600  if(terr_callable == nullptr) {
601  return formula_callable::do_compare(callable);
602  }
603 
604  const map_location& other_loc = terr_callable->loc_;
605  return loc_.do_compare(other_loc);
606 }
607 
609  return board_.map();
610 }
611 
613 {
614  add_input(inputs, "w");
615  add_input(inputs, "h");
616 }
617 
618 variant gamemap_callable::get_value(const std::string& key) const
619 {
620  if(key == "terrain") {
621  int w = get_gamemap().w();
622  int h = get_gamemap().h();
623 
624  std::vector<variant> vars;
625  for(int i = 0; i < w; i++) {
626  for(int j = 0; j < h; j++) {
627  const map_location loc(i, j);
628  vars.emplace_back(std::make_shared<terrain_callable>(board_, loc));
629  }
630  }
631 
632  return variant(vars);
633  } else if(key == "gamemap") {
634  int w = get_gamemap().w();
635  int h = get_gamemap().h();
636 
637  std::map<variant, variant> vars;
638  for(int i = 0; i < w; i++) {
639  for(int j = 0; j < h; j++) {
640  const map_location loc(i, j);
641  vars.emplace(std::make_shared<location_callable>(loc), std::make_shared<terrain_callable>(board_, loc));
642  }
643  }
644 
645  return variant(vars);
646  } else if(key == "w") {
647  return variant(get_gamemap().w());
648  } else if(key == "h") {
649  return variant(get_gamemap().h());
650  } else {
651  return variant();
652  }
653 }
654 
656 {
657  add_input(inputs, "side");
658  add_input(inputs, "id");
659  add_input(inputs, "gold");
660  add_input(inputs, "start_gold");
661  add_input(inputs, "base_income");
662  add_input(inputs, "total_income");
663  add_input(inputs, "village_gold");
664  add_input(inputs, "village_support");
665  add_input(inputs, "recall_cost");
666  add_input(inputs, "name");
667  add_input(inputs, "is_human");
668  add_input(inputs, "is_ai");
669  add_input(inputs, "is_network");
670  add_input(inputs, "fog");
671  add_input(inputs, "shroud");
672  add_input(inputs, "hidden");
673  add_input(inputs, "flag");
674  add_input(inputs, "flag_icon");
675  add_input(inputs, "team_name");
676  add_input(inputs, "faction");
677  add_input(inputs, "faction_name");
678  add_input(inputs, "color");
679  add_input(inputs, "share_vision");
680  add_input(inputs, "carryover_bonus");
681  add_input(inputs, "carryover_percentage");
682  add_input(inputs, "carryover_add");
683  add_input(inputs, "recruit");
684  add_input(inputs, "wml_vars");
685 }
686 
687 variant team_callable::get_value(const std::string& key) const
688 {
689  if(key == "side") {
690  return variant(team_.side());
691  } else if(key == "id") {
692  return variant(team_.save_id());
693  } else if(key == "save_id") {
694  return variant(team_.save_id());
695  } else if(key == "gold") {
696  return variant(team_.gold());
697  } else if(key == "start_gold") {
698  return variant(team_.start_gold());
699  } else if(key == "base_income") {
700  return variant(team_.base_income());
701  } else if(key == "total_income") {
702  return variant(team_.total_income());
703  } else if(key == "village_gold") {
704  return variant(team_.village_gold());
705  } else if(key == "village_support") {
706  return variant(team_.village_support());
707  } else if(key == "recall_cost") {
708  return variant(team_.recall_cost());
709  } else if(key == "is_human") {
710  return variant(team_.is_local_human());
711  } else if(key == "is_ai") {
712  return variant(team_.is_local_ai());
713  } else if(key == "is_network") {
714  return variant(team_.is_network());
715  } else if(key == "fog") {
716  return variant(team_.uses_fog());
717  } else if(key == "shroud") {
718  return variant(team_.uses_shroud());
719  } else if(key == "hidden") {
720  return variant(team_.hidden());
721  } else if(key == "flag") {
722  return variant(team_.flag());
723  } else if(key == "flag_icon") {
724  return variant(team_.flag_icon());
725  } else if(key == "team_name") {
726  return variant(team_.team_name());
727  } else if(key == "color") {
728  return variant(team_.color());
729  } else if(key == "share_vision") {
730  return variant(team_.share_vision().to_string());
731  } else if(key == "carryover_bonus") {
732  return variant(team_.carryover_bonus(), variant::DECIMAL_VARIANT);
733  } else if(key == "carryover_percentage") {
734  return variant(team_.carryover_percentage());
735  } else if(key == "carryover_add") {
736  return variant(team_.carryover_add());
737  } else if(key == "recruit") {
738  std::vector<variant> result;
739  for(const auto& recruit : team_.recruits()) {
740  result.emplace_back(recruit);
741  }
742 
743  return variant(result);
744  } else if(key == "wml_vars") {
745  return variant(std::make_shared<config_callable>(team_.variables()));
746  }
747 
748  return variant();
749 }
750 
751 variant set_var_callable::get_value(const std::string& key) const
752 {
753  if(key == "key") {
754  return variant(key_);
755  } else if(key == "value") {
756  return value_;
757  }
758 
759  return variant();
760 }
761 
763 {
764  add_input(inputs, "key");
765  add_input(inputs, "value");
766 }
767 
769 {
770  //if(infinite_loop_guardian_.set_var_check()) {
771  if(auto obj = ctxt.try_convert<formula_callable>()) {
772  LOG_SF << "Setting variable: " << key_ << " -> " << value_.to_debug_string() << "\n";
773  obj->mutate_value(key_, value_);
774  return variant(true);
775  }
776  //}
777  //too many calls in a row - possible infinite loop
778  ERR_SF << "ERROR #" << 5001 << " while executing 'set_var' formula function" << std::endl;
779 
780  return variant(std::make_shared<safe_call_result>(fake_ptr(), 5001));
781 }
782 
783 variant safe_call_callable::get_value(const std::string& key) const
784 {
785  if(key == "main") {
786  return variant(main_);
787  } else if(key == "backup") {
788  return variant(backup_);
789  }
790 
791  return variant();
792 }
793 
795 {
796  add_input(inputs, "main");
797  add_input(inputs, "backup");
798 }
799 
801 {
802  variant res;
803  if(auto action = main_.try_convert<action_callable>()) {
804  res = action->execute_self(ctxt);
805  }
806 
807  if(res.try_convert<safe_call_result>()) {
808  /* If we have safe_call formula and either an error occurred, or the current action
809  * was not recognized, then evaluate backup formula from safe_call and execute it
810  * during the next loop
811  */
812 
813  map_formula_callable callable(ctxt.as_callable());
814  callable.add("error", res);
815 
816  /* Store the result in safe_call_callable in case we would like to display it to the user,
817  * for example if this formula was executed from the commandline.
818  */
819  backup_ = get_backup()->evaluate(callable);
820  ctxt.execute_variant(backup_);
821  }
822  return variant(true);
823 }
824 
825 variant safe_call_result::get_value(const std::string& key) const
826 {
827  if(key == "status") {
828  return variant(status_);
829  } else if(key == "object") {
830  if(failed_callable_) {
831  return variant(failed_callable_);
832  }
833 
834  return variant();
835  } else if(key == "current_loc" && current_unit_location_ != map_location()) {
836  return variant(std::make_shared<location_callable>(current_unit_location_));
837  }
838 
839  return variant();
840 }
841 
843 {
844  add_input(inputs, "status");
845  add_input(inputs, "object");
846 
847  if(current_unit_location_ != map_location()) {
848  add_input(inputs, "current_loc");
849  }
850 }
851 
852 } // namespace wfl
const map_location & loc() const
variant execute_variant(const variant &to_exec)
Definition: variant.cpp:655
variant get_value(const std::string &key) const override
int attacks_left() const
Gets the remaining number of attacks this unit can perform this turn.
Definition: unit.hpp:985
bool is_castle() const
Definition: terrain.hpp:141
void get_inputs(formula_input_vector &inputs) const override
const t_string & description() const
Definition: terrain.hpp:49
void get_inputs(formula_input_vector &inputs) const override
This class represents a single unit of a specific type.
Definition: unit.hpp:120
const std::string & type_id() const
The id of this unit&#39;s type.
Definition: unit.cpp:1769
variant get_value(const std::string &key) const override
bool is_flying() const
Check if the unit is a flying unit.
Definition: unit.hpp:1457
const std::string & id() const
Definition: race.hpp:34
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
Definition: unit.cpp:1279
variant get_value(const std::string &key) const override
unit_race::GENDER gender() const
The gender of this unit.
Definition: unit.hpp:458
const movetype & movement_type() const
Get the unit&#39;s movement type.
Definition: unit.hpp:1421
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:492
config & variables()
Gets any user-defined variables this unit &#39;owns&#39;.
Definition: unit.hpp:696
const wfl::map_formula_callable_ptr & formula_vars() const
int do_compare(const map_location &a) const
three-way comparator
Definition: location.hpp:104
const std::string & variation() const
The ID of the variation of this unit&#39;s type.
Definition: unit.hpp:565
int wml_x() const
Definition: location.hpp:152
std::vector< formula_input > formula_input_vector
map_location::DIRECTION facing() const
The current direction this unit is facing within its hex.
Definition: unit.hpp:1364
void get_inputs(formula_input_vector &inputs) const override
static variant convert_set(const std::set< T > &input_set)
Definition: callable.hpp:114
#define h
#define ERR_SF
void get_inputs(formula_input_vector &inputs) const override
bool resting() const
Checks whether this unit is &#39;resting&#39;.
Definition: unit.hpp:1317
void get_inputs(formula_input_vector &inputs) const override
bool get_emit_zoc() const
Gets the raw zone-of-control flag, disregarding incapacitated.
Definition: unit.hpp:1335
bool get_hidden() const
Gets whether this unit is currently hidden on the map.
Definition: unit.hpp:713
Definitions for the interface to Wesnoth Markup Language (WML).
int cost() const
How much gold is required to recruit this unit.
Definition: unit.hpp:626
const_attr_itors attribute_range() const
Definition: config.cpp:833
static lg::log_domain log_scripting_formula("scripting/formula")
variant operator()(const t_string &s) const
map_location loc_
#define b
bool is_village() const
Definition: terrain.hpp:140
variant operator()(int i) const
variant get_value(const std::string &key) const override
int light_bonus(int base) const
Returns the light (lawful) bonus for this terrain when the time of day gives a base bonus...
Definition: terrain.hpp:131
void get_inputs(formula_input_vector &inputs) const override
variant execute_self(variant ctxt) override
const t_string & editor_name() const
Definition: terrain.hpp:48
const std::string & id() const
Gets this unit&#39;s id.
Definition: unit.hpp:370
int do_compare(const formula_callable *callable) const override
const config & get_config() const
attack_type_callable(const attack_type &attack)
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:252
int wml_y() const
Definition: location.hpp:153
int do_compare(const formula_callable *callable) const override
Encapsulates the map of the game.
Definition: map.hpp:170
int do_compare(const formula_callable *callable) const override
const t_string & name() const
Gets this unit&#39;s translatable display name.
Definition: unit.hpp:393
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
Definition: unit.cpp:1558
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:522
int level() const
The current level of this unit.
Definition: unit.hpp:552
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:143
void serialize_to_string(std::string &str) const override
const t_string & type_name() const
Gets the translatable name of this unit&#39;s type.
Definition: unit.hpp:359
int gives_healing() const
Definition: terrain.hpp:139
variant get_value(const std::string &key) const override
void get_inputs(formula_input_vector &inputs) const override
const map_location & loc_
variant get_value(const std::string &key) const override
const std::string & gender_string(unit_race::GENDER gender)
Definition: race.cpp:133
void get_inputs(formula_input_vector &inputs) const override
bool is_keep() const
Definition: terrain.hpp:142
#define LOG_SF
Encapsulates the map of the game.
Definition: location.hpp:37
const_formula_callable_ptr as_callable() const
Definition: variant.hpp:82
const gamemap & get_gamemap() const
variant operator()(bool b) const
std::size_t i
Definition: function.cpp:940
formula_callable_ptr fake_ptr()
Definition: callable.hpp:41
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:498
const std::string & get_role() const
Gets this unit&#39;s role.
Definition: unit.hpp:662
variant operator()(double i) const
mock_party p
static std::string get_location(const std::string &loc)
int do_compare(const formula_callable *callable) const override
static map_location::DIRECTION s
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
const map_location loc_
variant operator()(const std::string &s) const
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:605
std::string usage() const
Gets this unit&#39;s usage.
Definition: unit.hpp:679
variant get_value(const std::string &key) const override
attack_itors attacks()
Gets an iterator over this unit&#39;s attacks.
Definition: unit.hpp:918
unit_callable(const map_location &loc, const unit &u)
variant operator()(utils::monostate) const
const terrain_type & t_
std::vector< std::string > get_traits_list() const
Gets a list of the traits this unit currently has.
Definition: unit.cpp:830
int w
const unit_race * race() const
Gets this unit&#39;s race.
Definition: unit.hpp:486
variant get_value(const std::string &key) const override
formula_input_vector inputs() const
Definition: callable.hpp:62
const t_string & name() const
Definition: terrain.hpp:47
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
Definition: unit.hpp:617
static const char *const id_chars
Definition: formula.hpp:74
unit_formula_manager & formula_manager() const
Get the unit formula manager.
Definition: unit.hpp:1804
std::shared_ptr< T > try_convert() const
Definition: variant.hpp:89
int experience() const
The current number of experience points this unit has.
Definition: unit.hpp:516
UNIT_ALIGNMENT alignment() const
The alignment of this unit.
Definition: unit.hpp:468
Definition: contexts.hpp:43
int do_compare(const formula_callable *callable) const override
variant get_value(const std::string &key) const override
Standard logging facilities (interface).
const std::string & id() const
Definition: terrain.hpp:51
static variant convert_vector(const std::vector< T > &input_vector)
Definition: callable.hpp:125
variant execute_self(variant ctxt) override
static const map_location & null_location()
Definition: location.hpp:80
bool incapacitated() const
Check if the unit has been petrified.
Definition: unit.hpp:896
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1257
terrain_callable(const display_context &m, const map_location &loc)
int side() const
The side this unit belongs to.
Definition: unit.hpp:333
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
std::vector< std::string > get_ability_list() const
Get a list of all abilities by ID.
Definition: abilities.cpp:238
static void add_input(formula_input_vector &inputs, const std::string &key, FORMULA_ACCESS_TYPE access_type=FORMULA_READ_ONLY)
Definition: callable.hpp:135
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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual int do_compare(const formula_callable *callable) const
Definition: callable.hpp:145
const std::string & str() const
Definition: tstring.hpp:190
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
Definition: unit.cpp:1262
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
Definition: unit.hpp:234
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side&#39;s default recall cost is used...
Definition: unit.hpp:633
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:140
std::string hash() const
Definition: config.cpp:1367
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
Definition: unit.hpp:1273
std::size_t underlying_id() const
This unit&#39;s unique internal ID.
Definition: unit.hpp:382
variant operator()(unsigned long long i) const
int max_attacks() const
The maximum number of attacks this unit may perform per turn, usually 1.
Definition: unit.hpp:969
const std::string & icon_image() const
Definition: terrain.hpp:43