The Battle for Wesnoth  1.17.0-dev
formula_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "formula/callable_objects.hpp"
19 #include "config.hpp"
20 #include "formula/formula.hpp"
21 #include "map/location.hpp"
22 #include "log.hpp"
23 
25 {
26  if(!formula_vars_) formula_vars_ = std::make_shared<wfl::map_formula_callable>();
27  formula_vars_->add(str, var);
28 }
29 
31 {
32  unit_formula_ = ai["formula"].str();
33  unit_loop_formula_ = ai["loop_formula"].str();
34  unit_priority_formula_ = ai["priority"].str();
35 
36  if (const config &ai_vars = ai.child("vars"))
37  {
38  formula_vars_ = std::make_shared<wfl::map_formula_callable>();
39 
40  wfl::variant var;
41  for (const config::attribute &i : ai_vars.attribute_range()) {
42  var.serialize_from_string(i.second);
43  formula_vars_->add(i.first, var);
44  }
45  } else {
47  }
48 }
49 
51 {
52  if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
53 
54  config &ai = cfg.add_child("ai");
55 
56  if (has_formula())
57  ai["formula"] = unit_formula_;
58 
59  if (has_loop_formula())
60  ai["loop_formula"] = unit_loop_formula_;
61 
63  ai["priority"] = unit_priority_formula_;
64 
65 
66  if (formula_vars_ && formula_vars_->empty() == false)
67  {
68  config &ai_vars = ai.add_child("vars");
69 
70  std::string str;
72  {
73  str = i->second.serialize_to_string();
74  if (!str.empty())
75  {
76  ai_vars[i->first] = str;
77  str.clear();
78  }
79  }
80  }
81  }
82 }
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:419
void add_formula_var(std::string str, wfl::variant var)
attribute_map::value_type attribute
Definition: config.hpp:221
void clear()
Definition: config.cpp:901
void write(config &cfg)
Definitions for the interface to Wesnoth Markup Language (WML).
std::string unit_loop_formula_
std::string unit_priority_formula_
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
void read(const config &ai)
void serialize_from_string(const std::string &str)
Definition: variant.cpp:632
wfl::map_formula_callable_ptr formula_vars_
std::size_t i
Definition: function.cpp:941
bool has_priority_formula() const
bool has_loop_formula() const
config & add_child(config_key_type key)
Definition: config.cpp:505
std::map< std::string, variant >::const_iterator const_iterator
Definition: callable.hpp:274
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::shared_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:312