The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tips.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Mark de Wever <[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 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/core/tips.hpp"
18 
19 #include "config.hpp"
20 #include "game_preferences.hpp"
22 
23 namespace gui2
24 {
25 
26 ttip::ttip(const t_string& text,
27  const t_string& source,
28  const std::string& unit_filter)
29  : text_(text), source_(source), unit_filter_(utils::split(unit_filter))
30 {
31 }
32 
33 namespace tips
34 {
35 
36 std::vector<ttip> load(const config& cfg)
37 {
38  std::vector<ttip> result;
39 
40  for(const auto & tip : cfg.child_range("tip"))
41  {
42  result.push_back(
43  ttip(tip["text"], tip["source"], tip["encountered_units"]));
44  }
45 
46  return result;
47 }
48 
49 std::vector<ttip> shuffle(const std::vector<ttip>& tips)
50 {
51  std::vector<ttip> result;
52 
53  const std::set<std::string>& units = preferences::encountered_units();
54 
55  for(const auto & tip : tips)
56  {
57  if(tip.unit_filter_.empty()) {
58  result.push_back(tip);
59  } else {
60  for(const auto & unit : tip.unit_filter_)
61  {
62  if(units.find(unit) != units.end()) {
63  result.push_back(tip);
64  break;
65  }
66  }
67  }
68  }
69 
70  std::random_shuffle(result.begin(), result.end());
71  return result;
72 }
73 
74 } // namespace tips
75 
76 } // namespace gui2
child_itors child_range(const std::string &key)
Definition: config.cpp:613
std::vector< std::string > unit_filter_
List of units to filter the tip upon.
Definition: tips.hpp:87
Definition: unit.hpp:95
std::vector< ttip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:36
tformula< t_string > text_
The text to draw.
Definition: canvas.cpp:1268
Definitions for the interface to Wesnoth Markup Language (WML).
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
GLuint64EXT * result
Definition: glew.h:10727
std::vector< ttip > shuffle(const std::vector< ttip > &tips)
Shuffles the tips.
Definition: tips.cpp:49
ttip()
Definition: tip.cpp:71
static ttip & tip()
Definition: tip.cpp:123
std::set< std::string > & encountered_units()
The tips of day structure.
Definition: tips.hpp:55
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
GLsizei GLsizei GLchar * source
Definition: glew.h:1800
unit_map * units
Definition: resources.cpp:35
static std::vector< ttip > tips
Definition: settings.cpp:65