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