The Battle for Wesnoth  1.13.10+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 - 2017 by Mark de Wever <koraq@xs4all.nl>
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/auxiliary/tips.hpp"
18 
19 #include "config.hpp"
20 #include "preferences/game.hpp"
21 #include "random.hpp"
23 
24 namespace gui2
25 {
26 game_tip::game_tip(const t_string& text, const t_string& source, const std::string& unit_filter)
27  : text_(text)
28  , source_(source)
29  , unit_filter_(utils::split(unit_filter))
30 {
31 }
32 
33 namespace tip_of_the_day
34 {
35 std::vector<game_tip> load(const config& cfg)
36 {
37  std::vector<game_tip> result;
38 
39  for(const auto& tip : cfg.child_range("tip")) {
40  result.emplace_back(tip["text"], tip["source"], tip["encountered_units"]);
41  }
42 
43  return result;
44 }
45 
46 std::vector<game_tip> shuffle(const std::vector<game_tip>& tips)
47 {
48  std::vector<game_tip> result = tips;
49  const std::set<std::string>& units = preferences::encountered_units();
50 
51  // Remove entries whose filters do not match from the tips list.
52  const auto iter = std::remove_if(result.begin(), result.end(), [&units](const game_tip& tip) {
53  const auto& filters = tip.unit_filter_;
54 
55  // Filter passes there's no filter at all or if every unit specified has already been
56  // encountered in-game.
57  const bool passes_filter = filters.empty()
58  ? true
59  : std::any_of(filters.begin(), filters.end(), [&units](const std::string& u) {
60  return units.find(u) != units.end();
61  });
62 
63  return !passes_filter;
64  });
65 
66  // Prune invalid entries.
67  result.erase(iter, result.end());
68 
69  // Shuffle the list.
70  std::shuffle(result.begin(), result.end(), randomness::rng::default_instance());
71  return result;
72 }
73 
74 } // namespace tips
75 
76 } // namespace gui2
std::vector< char_t > string
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:35
child_itors child_range(config_key_type key)
Definition: config.cpp:306
static std::vector< game_tip > tips
Definition: settings.cpp:60
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
Generic file dialog.
Definition: field-fwd.hpp:22
game_tip(const t_string &text, const t_string &source, const std::string &unit_filter)
Definition: tips.cpp:26
static tooltip & tip()
Definition: tooltip.cpp:143
std::set< std::string > & encountered_units()
Definition: game.cpp:942
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition: tips.cpp:46
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
The tips of day structure.
Definition: tips.hpp:52
static rng & default_instance()
Definition: random.cpp:70
unit_map * units
Definition: resources.cpp:34