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