The Battle for Wesnoth  1.15.2+dev
formula_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
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 
16 
17 #include "formula/callable_objects.hpp"
18 #include "config.hpp"
19 #include "formula/formula.hpp"
20 #include "map/location.hpp"
21 #include "log.hpp"
22 
24 {
25  if(!formula_vars_) formula_vars_ = std::make_shared<wfl::map_formula_callable>();
26  formula_vars_->add(str, var);
27 }
28 
30 {
31  unit_formula_ = ai["formula"].str();
32  unit_loop_formula_ = ai["loop_formula"].str();
33  unit_priority_formula_ = ai["priority"].str();
34 
35  if (const config &ai_vars = ai.child("vars"))
36  {
37  formula_vars_ = std::make_shared<wfl::map_formula_callable>();
38 
39  wfl::variant var;
40  for (const config::attribute &i : ai_vars.attribute_range()) {
41  var.serialize_from_string(i.second);
42  formula_vars_->add(i.first, var);
43  }
44  } else {
46  }
47 }
48 
50 {
51  if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
52 
53  config &ai = cfg.add_child("ai");
54 
55  if (has_formula())
56  ai["formula"] = unit_formula_;
57 
58  if (has_loop_formula())
59  ai["loop_formula"] = unit_loop_formula_;
60 
62  ai["priority"] = unit_priority_formula_;
63 
64 
65  if (formula_vars_ && formula_vars_->empty() == false)
66  {
67  config &ai_vars = ai.add_child("vars");
68 
69  std::string str;
71  {
72  str = i->second.serialize_to_string();
73  if (!str.empty())
74  {
75  ai_vars[i->first] = str;
76  str.clear();
77  }
78  }
79  }
80  }
81 }
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:420
void add_formula_var(std::string str, wfl::variant var)
attribute_map::value_type attribute
Definition: config.hpp:226
void clear()
Definition: config.cpp:863
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:58
void read(const config &ai)
void serialize_from_string(const std::string &str)
Definition: variant.cpp:629
wfl::map_formula_callable_ptr formula_vars_
std::size_t i
Definition: function.cpp:933
bool has_priority_formula() const
bool has_loop_formula() const
config & add_child(config_key_type key)
Definition: config.cpp:476
std::map< std::string, variant >::const_iterator const_iterator
Definition: callable.hpp:267
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
std::shared_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:305