The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
formula_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "formula/string_utils.hpp"
21 #include "map/location.hpp"
22 #include "log.hpp"
23 
25 {
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  {
39 
40  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  i->second.serialize_to_string(str);
74  if (!str.empty())
75  {
76  ai_vars[i->first] = str;
77  str.clear();
78  }
79  }
80  }
81  }
82 }
83 
bool has_loop_formula() const
attribute_map::value_type attribute
Definition: config.hpp:393
void clear()
Definition: config.cpp:1055
void write(config &cfg)
Definitions for the interface to Wesnoth Markup Language (WML).
std::string unit_loop_formula_
boost::intrusive_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:176
std::string unit_priority_formula_
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
void read(const config &ai)
config & add_child(const std::string &key)
Definition: config.cpp:743
game_logic::map_formula_callable_ptr formula_vars_
bool has_priority_formula() const
size_t i
Definition: function.cpp:1057
bool has_formula() const
void add_formula_var(std::string str, variant var)
config & child(const std::string &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:658
Standard logging facilities (interface).
void serialize_from_string(const std::string &str)
Definition: variant.cpp:1136
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
std::map< std::string, variant >::const_iterator const_iterator
Definition: callable.hpp:163
GLsizei const GLcharARB ** string
Definition: glew.h:4503