The Battle for Wesnoth  1.15.5+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 
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 
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 
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 
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 
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 
450 struct fai_variant_visitor : public boost::static_visitor<variant>
451 {
452  variant operator()(bool b) const { return variant(b ? 1 : 0); }
453  variant operator()(int i) const { return variant(i); }
454  variant operator()(unsigned long long i) const { return variant(i); }
455  variant operator()(double i) const { return variant(i * 1000, variant::DECIMAL_VARIANT); }
456  // TODO: Should comma-separated lists of stuff be returned as a list?
457  // The challenge is to distinguish them from ordinary strings that happen to contain a comma
458  // (or should we assume that such strings will be translatable?).
459  variant operator()(const std::string& s) const { return variant(s); }
460  variant operator()(const t_string& s) const { return variant(s.str()); }
461  variant operator()(boost::blank) const { return variant(); }
462 };
463 
465 {
466  if(cfg_.has_attribute(key)) {
467  return cfg_[key].apply_visitor(fai_variant_visitor());
468  } else if(cfg_.has_child(key)) {
469  std::vector<variant> result;
470  for(const auto& child : cfg_.child_range(key)) {
471  result.emplace_back(std::make_shared<config_callable>(child));
472  }
473 
474  return variant(result);
475  } else if(key == "__all_children") {
476  std::vector<variant> result;
477  for(const auto& child : cfg_.all_children_range()) {
478  const variant cfg_child(std::make_shared<config_callable>(child.cfg));
479  const variant kv(std::make_shared<key_value_pair>(variant(child.key), cfg_child));
480  result.push_back(kv);
481  }
482 
483  return variant(result);
484  } else if(key == "__children") {
485  std::map<std::string, std::vector<variant>> build;
486  for(const auto& child : cfg_.all_children_range()) {
487  const variant cfg_child(std::make_shared<config_callable>(child.cfg));
488  build[child.key].push_back(cfg_child);
489  }
490 
491  std::map<variant,variant> result;
492  for(auto& p : build) {
493  result[variant(p.first)] = variant(p.second);
494  }
495 
496  return variant(result);
497  } else if(key == "__attributes") {
498  std::map<variant,variant> result;
499  for(const auto& val : cfg_.attribute_range()) {
500  result[variant(val.first)] = val.second.apply_visitor(fai_variant_visitor());
501  }
502 
503  return variant(result);
504  }
505 
506  return variant();
507 }
508 
510 {
511  add_input(inputs, "__all_children");
512  add_input(inputs, "__children");
513  add_input(inputs, "__attributes");
514 
515  for(const auto& val : cfg_.attribute_range()) {
516  if(val.first.find_first_not_of(formula::id_chars) != std::string::npos) {
517  add_input(inputs, val.first);
518  }
519  }
520 }
521 
523 {
524  const config_callable* cfg_callable = dynamic_cast<const config_callable*>(callable);
525  if(cfg_callable == nullptr) {
526  return formula_callable::do_compare(callable);
527  }
528 
529  if(cfg_ == cfg_callable->get_config()) {
530  return 0;
531  }
532 
533  return cfg_.hash().compare(cfg_callable->get_config().hash());
534 }
535 
536 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)
537 {
538  type_ = TERRAIN_C;
539 }
540 
542 {
543  if(key == "x") {
544  return variant(loc_.wml_x());
545  } else if(key == "y") {
546  return variant(loc_.wml_y());
547  } else if(key == "loc") {
548  return variant(std::make_shared<location_callable>(loc_));
549  } else if(key == "id") {
550  return variant(std::string(t_.id()));
551  } else if(key == "name") {
552  return variant(t_.name());
553  } else if(key == "editor_name") {
554  return variant(t_.editor_name());
555  } else if(key == "description") {
556  return variant(t_.description());
557  } else if(key == "icon") {
558  return variant(t_.icon_image());
559  } else if(key == "light") {
560  return variant(t_.light_bonus(0));
561  } else if(key == "village") {
562  return variant(t_.is_village());
563  } else if(key == "castle") {
564  return variant(t_.is_castle());
565  } else if(key == "keep") {
566  return variant(t_.is_keep());
567  } else if(key == "healing") {
568  return variant(t_.gives_healing());
569  } else if(key == "owner") {
570  return variant(owner_);
571  }
572 
573  return variant();
574 }
575 
577 {
578  add_input(inputs, "x");
579  add_input(inputs, "y");
580  add_input(inputs, "loc");
581  add_input(inputs, "id");
582  add_input(inputs, "name");
583  add_input(inputs, "editor_name");
584  add_input(inputs, "description");
585  add_input(inputs, "icon");
586  add_input(inputs, "light");
587  add_input(inputs, "village");
588  add_input(inputs, "castle");
589  add_input(inputs, "keep");
590  add_input(inputs, "healing");
591  add_input(inputs, "owner");
592 }
593 
595 {
596  const terrain_callable* terr_callable = dynamic_cast<const terrain_callable*>(callable);
597  if(terr_callable == nullptr) {
598  return formula_callable::do_compare(callable);
599  }
600 
601  const map_location& other_loc = terr_callable->loc_;
602  return loc_.do_compare(other_loc);
603 }
604 
606  return board_.map();
607 }
608 
610 {
611  add_input(inputs, "w");
612  add_input(inputs, "h");
613 }
614 
616 {
617  if(key == "terrain") {
618  int w = get_gamemap().w();
619  int h = get_gamemap().h();
620 
621  std::vector<variant> vars;
622  for(int i = 0; i < w; i++) {
623  for(int j = 0; j < h; j++) {
624  const map_location loc(i, j);
625  vars.emplace_back(std::make_shared<terrain_callable>(board_, loc));
626  }
627  }
628 
629  return variant(vars);
630  } else if(key == "gamemap") {
631  int w = get_gamemap().w();
632  int h = get_gamemap().h();
633 
634  std::map<variant, variant> vars;
635  for(int i = 0; i < w; i++) {
636  for(int j = 0; j < h; j++) {
637  const map_location loc(i, j);
638  vars.emplace(std::make_shared<location_callable>(loc), std::make_shared<terrain_callable>(board_, loc));
639  }
640  }
641 
642  return variant(vars);
643  } else if(key == "w") {
644  return variant(get_gamemap().w());
645  } else if(key == "h") {
646  return variant(get_gamemap().h());
647  } else {
648  return variant();
649  }
650 }
651 
653 {
654  add_input(inputs, "side");
655  add_input(inputs, "id");
656  add_input(inputs, "gold");
657  add_input(inputs, "start_gold");
658  add_input(inputs, "base_income");
659  add_input(inputs, "total_income");
660  add_input(inputs, "village_gold");
661  add_input(inputs, "village_support");
662  add_input(inputs, "recall_cost");
663  add_input(inputs, "name");
664  add_input(inputs, "is_human");
665  add_input(inputs, "is_ai");
666  add_input(inputs, "is_network");
667  add_input(inputs, "fog");
668  add_input(inputs, "shroud");
669  add_input(inputs, "hidden");
670  add_input(inputs, "flag");
671  add_input(inputs, "flag_icon");
672  add_input(inputs, "team_name");
673  add_input(inputs, "faction");
674  add_input(inputs, "faction_name");
675  add_input(inputs, "color");
676  add_input(inputs, "share_vision");
677  add_input(inputs, "carryover_bonus");
678  add_input(inputs, "carryover_percentage");
679  add_input(inputs, "carryover_add");
680  add_input(inputs, "recruit");
681  add_input(inputs, "wml_vars");
682 }
683 
685 {
686  if(key == "side") {
687  return variant(team_.side());
688  } else if(key == "id") {
689  return variant(team_.save_id());
690  } else if(key == "save_id") {
691  return variant(team_.save_id());
692  } else if(key == "gold") {
693  return variant(team_.gold());
694  } else if(key == "start_gold") {
695  return variant(team_.start_gold());
696  } else if(key == "base_income") {
697  return variant(team_.base_income());
698  } else if(key == "total_income") {
699  return variant(team_.total_income());
700  } else if(key == "village_gold") {
701  return variant(team_.village_gold());
702  } else if(key == "village_support") {
703  return variant(team_.village_support());
704  } else if(key == "recall_cost") {
705  return variant(team_.recall_cost());
706  } else if(key == "is_human") {
707  return variant(team_.is_local_human());
708  } else if(key == "is_ai") {
709  return variant(team_.is_local_ai());
710  } else if(key == "is_network") {
711  return variant(team_.is_network());
712  } else if(key == "fog") {
713  return variant(team_.uses_fog());
714  } else if(key == "shroud") {
715  return variant(team_.uses_shroud());
716  } else if(key == "hidden") {
717  return variant(team_.hidden());
718  } else if(key == "flag") {
719  return variant(team_.flag());
720  } else if(key == "flag_icon") {
721  return variant(team_.flag_icon());
722  } else if(key == "team_name") {
723  return variant(team_.team_name());
724  } else if(key == "color") {
725  return variant(team_.color());
726  } else if(key == "share_vision") {
727  return variant(team_.share_vision().to_string());
728  } else if(key == "carryover_bonus") {
729  return variant(team_.carryover_bonus());
730  } else if(key == "carryover_percentage") {
731  return variant(team_.carryover_percentage());
732  } else if(key == "carryover_add") {
733  return variant(team_.carryover_add());
734  } else if(key == "recruit") {
735  std::vector<variant> result;
736  for(const auto& recruit : team_.recruits()) {
737  result.emplace_back(recruit);
738  }
739 
740  return variant(result);
741  } else if(key == "wml_vars") {
742  return variant(std::make_shared<config_callable>(team_.variables()));
743  }
744 
745  return variant();
746 }
747 
749 {
750  if(key == "key") {
751  return variant(key_);
752  } else if(key == "value") {
753  return value_;
754  }
755 
756  return variant();
757 }
758 
760 {
761  add_input(inputs, "key");
762  add_input(inputs, "value");
763 }
764 
766 {
767  //if(infinite_loop_guardian_.set_var_check()) {
768  if(auto obj = ctxt.try_convert<formula_callable>()) {
769  LOG_SF << "Setting variable: " << key_ << " -> " << value_.to_debug_string() << "\n";
770  obj->mutate_value(key_, value_);
771  return variant(true);
772  }
773  //}
774  //too many calls in a row - possible infinite loop
775  ERR_SF << "ERROR #" << 5001 << " while executing 'set_var' formula function" << std::endl;
776 
777  return variant(std::make_shared<safe_call_result>(fake_ptr(), 5001));
778 }
779 
781 {
782  if(key == "main") {
783  return variant(main_);
784  } else if(key == "backup") {
785  return variant(backup_);
786  }
787 
788  return variant();
789 }
790 
792 {
793  add_input(inputs, "main");
794  add_input(inputs, "backup");
795 }
796 
798 {
799  variant res;
800  if(auto action = main_.try_convert<action_callable>()) {
801  res = action->execute_self(ctxt);
802  }
803 
804  if(res.try_convert<safe_call_result>()) {
805  /* If we have safe_call formula and either an error occurred, or the current action
806  * was not recognized, then evaluate backup formula from safe_call and execute it
807  * during the next loop
808  */
809 
810  map_formula_callable callable(ctxt.as_callable());
811  callable.add("error", res);
812 
813  /* Store the result in safe_call_callable in case we would like to display it to the user,
814  * for example if this formula was executed from the commandline.
815  */
816  backup_ = get_backup()->evaluate(callable);
817  ctxt.execute_variant(backup_);
818  }
819  return variant(true);
820 }
821 
823 {
824  if(key == "status") {
825  return variant(status_);
826  } else if(key == "object") {
827  if(failed_callable_) {
828  return variant(failed_callable_);
829  }
830 
831  return variant();
832  } else if(key == "current_loc" && current_unit_location_ != map_location()) {
833  return variant(std::make_shared<location_callable>(current_unit_location_));
834  }
835 
836  return variant();
837 }
838 
840 {
841  add_input(inputs, "status");
842  add_input(inputs, "object");
843 
844  if(current_unit_location_ != map_location()) {
845  add_input(inputs, "current_loc");
846  }
847 }
848 
849 } // namespace wfl
const map_location & loc() const
variant execute_variant(const variant &to_exec)
Definition: variant.cpp:653
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:992
bool is_castle() const
Definition: terrain.hpp:142
variant operator()(boost::blank) const
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:129
const std::string & type_id() const
The id of this unit&#39;s type.
Definition: unit.cpp:1817
variant get_value(const std::string &key) const override
bool is_flying() const
Check if the unit is a flying unit.
Definition: unit.hpp:1452
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:1323
variant get_value(const std::string &key) const override
unit_race::GENDER gender() const
The gender of this unit.
Definition: unit.hpp:466
const movetype & movement_type() const
Get the unit&#39;s movement type.
Definition: unit.hpp:1416
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:500
config & variables()
Gets any user-defined variables this unit &#39;owns&#39;.
Definition: unit.hpp:704
const wfl::map_formula_callable_ptr & formula_vars() const
int do_compare(const map_location &a) const
three-way comparator
Definition: location.hpp:109
const std::string & variation() const
The ID of the variation of this unit&#39;s type.
Definition: unit.hpp:573
int wml_x() const
Definition: location.hpp:157
std::vector< formula_input > formula_input_vector
map_location::DIRECTION facing() const
The current directin this unit is facing within its hex.
Definition: unit.hpp:1359
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
std::string str
Definition: statement.cpp:110
bool resting() const
Checks whether this unit is &#39;resting&#39;.
Definition: unit.hpp:1312
void get_inputs(formula_input_vector &inputs) const override
-file sdl_utils.hpp
bool get_emit_zoc() const
Gets the raw zone-of-control flag, disregarding incapacitated.
Definition: unit.hpp:1330
bool get_hidden() const
Gets whether this unit is currently hidden on the map.
Definition: unit.hpp:721
Definitions for the interface to Wesnoth Markup Language (WML).
int cost() const
How much gold is required to recruit this unit.
Definition: unit.hpp:634
const_attr_itors attribute_range() const
Definition: config.cpp:809
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:141
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:132
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:378
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:158
int do_compare(const formula_callable *callable) const override
Encapsulates the map of the game.
Definition: map.hpp:36
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:401
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
Definition: unit.cpp:1602
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:530
int level() const
The current level of this unit.
Definition: unit.hpp:560
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:139
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:367
int gives_healing() const
Definition: terrain.hpp:140
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:143
#define LOG_SF
Encapsulates the map of the game.
Definition: location.hpp:42
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:933
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:506
const std::string & get_role() const
Gets this unit&#39;s role.
Definition: unit.hpp:670
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
window * build(const builder_window::window_resolution *definition)
Builds a window.
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:613
std::string usage() const
Gets this unit&#39;s usage.
Definition: unit.hpp:687
variant get_value(const std::string &key) const override
attack_itors attacks()
Gets an iterator over this unit&#39;s attacks.
Definition: unit.hpp:926
unit_callable(const map_location &loc, const unit &u)
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:871
int w
const unit_race * race() const
Gets this unit&#39;s race.
Definition: unit.hpp:494
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:625
static const char *const id_chars
Definition: formula.hpp:74
unit_formula_manager & formula_manager() const
Get the unit formula manager.
Definition: unit.hpp:1752
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:524
UNIT_ALIGNMENT alignment() const
The alignment of this unit.
Definition: unit.hpp:476
Definition: contexts.hpp:42
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:85
bool incapacitated() const
Check if the unit has been petrified.
Definition: unit.hpp:904
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1252
terrain_callable(const display_context &m, const map_location &loc)
int side() const
The side this unit belongs to.
Definition: unit.hpp:341
std::vector< std::string > get_ability_list() const
Get a list of all abilities by ID.
Definition: abilities.cpp:239
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:68
virtual int do_compare(const formula_callable *callable) const
Definition: callable.hpp:145
const std::string & str() const
Definition: tstring.hpp:186
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
Definition: unit.cpp:1306
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
Definition: unit.hpp:241
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:641
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:140
std::string hash() const
Definition: config.cpp:1335
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
Definition: unit.hpp:1268
std::size_t underlying_id() const
This unit&#39;s unique internal ID.
Definition: unit.hpp:390
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:976
const std::string & icon_image() const
Definition: terrain.hpp:43