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 #include "deprecation.hpp"
24 
26 {
27  if(!formula_vars_) formula_vars_ = std::make_shared<wfl::map_formula_callable>();
28  formula_vars_->add(str, var);
29 }
30 
32 {
33  unit_formula_ = ai.get_deprecated_attribute("formula", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
34  unit_loop_formula_ = ai.get_deprecated_attribute("loop_formula", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
35  unit_priority_formula_ = ai.get_deprecated_attribute("priority", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
36 
37  if (auto ai_vars = ai.get_deprecated_child("vars", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17"))
38  {
39  formula_vars_ = std::make_shared<wfl::map_formula_callable>();
40 
41  wfl::variant var;
42  for (const config::attribute &i : ai_vars->attribute_range()) {
43  var.serialize_from_string(i.second);
44  formula_vars_->add(i.first, var);
45  }
46  } else {
48  }
49 }
50 
52 {
53  if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
54 
55  config &ai = cfg.add_child("ai");
56 
57  if (has_formula())
58  ai["formula"] = unit_formula_;
59 
60  if (has_loop_formula())
61  ai["loop_formula"] = unit_loop_formula_;
62 
64  ai["priority"] = unit_priority_formula_;
65 
66 
67  if (formula_vars_ && formula_vars_->empty() == false)
68  {
69  config &ai_vars = ai.add_child("vars");
70 
71  std::string str;
73  {
74  str = i->second.serialize_to_string();
75  if (!str.empty())
76  {
77  ai_vars[i->first] = str;
78  str.clear();
79  }
80  }
81  }
82  }
83 }
void add_formula_var(std::string str, wfl::variant var)
attribute_map::value_type attribute
Definition: config.hpp:222
void clear()
Definition: config.cpp:920
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:967
bool has_priority_formula() const
utils::optional_reference< const config > get_deprecated_child(config_key_type old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated child and log a deprecation message.
Definition: config.cpp:488
bool has_loop_formula() const
config & add_child(config_key_type key)
Definition: config.cpp:514
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:61
std::shared_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:312
const attribute_value & get_deprecated_attribute(config_key_type old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated attribute without a direct substitute, and log a deprecation message.
Definition: config.cpp:824
std::string str(const std::string &fallback="") const