The Battle for Wesnoth  1.19.5+dev
formula_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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 "config.hpp"
19 #include "deprecation.hpp"
20 
22 {
23  if(!formula_vars_) formula_vars_ = std::make_shared<wfl::map_formula_callable>();
24  formula_vars_->add(str, var);
25 }
26 
28 {
29  unit_formula_ = ai.get_deprecated_attribute("formula", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
30  unit_loop_formula_ = ai.get_deprecated_attribute("loop_formula", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
31  unit_priority_formula_ = ai.get_deprecated_attribute("priority", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17").str();
32 
33  if (auto ai_vars = ai.get_deprecated_child("vars", "unit][ai", DEP_LEVEL::FOR_REMOVAL, "FormulaAI will be removed in 1.17"))
34  {
35  formula_vars_ = std::make_shared<wfl::map_formula_callable>();
36 
37  wfl::variant var;
38  for(const auto& [key, value] : ai_vars->attribute_range()) {
39  var.serialize_from_string(value);
40  formula_vars_->add(key, var);
41  }
42  } else {
44  }
45 }
46 
48 {
49  if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
50 
51  config &ai = cfg.add_child("ai");
52 
53  if (has_formula())
54  ai["formula"] = unit_formula_;
55 
56  if (has_loop_formula())
57  ai["loop_formula"] = unit_loop_formula_;
58 
60  ai["priority"] = unit_priority_formula_;
61 
62 
63  if (formula_vars_ && formula_vars_->empty() == false)
64  {
65  config &ai_vars = ai.add_child("vars");
66 
67  std::string str;
69  {
70  str = i->second.serialize_to_string();
71  if (!str.empty())
72  {
73  ai_vars[i->first] = str;
74  str.clear();
75  }
76  }
77  }
78  }
79 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
void clear()
Definition: config.cpp:828
config & add_child(config_key_type key)
Definition: config.cpp:440
std::string unit_loop_formula_
std::string unit_priority_formula_
void read(const config &ai)
void write(config &cfg)
bool has_loop_formula() const
bool has_priority_formula() const
void add_formula_var(std::string str, wfl::variant var)
wfl::map_formula_callable_ptr formula_vars_
std::map< std::string, variant >::const_iterator const_iterator
Definition: callable.hpp:274
void serialize_from_string(const std::string &str)
Definition: variant.cpp:628
Definitions for the interface to Wesnoth Markup Language (WML).
std::size_t i
Definition: function.cpp:1028
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:312