The Battle for Wesnoth  1.19.7+dev
tips.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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"
22 #include "random.hpp"
24 #include "utils/general.hpp"
25 
26 namespace gui2
27 {
28 game_tip::game_tip(const t_string& text, const t_string& source, const std::string& unit_filter)
29  : text_(text)
30  , source_(source)
31  , unit_filter_(utils::split(unit_filter))
32 {
33 }
34 
35 namespace tip_of_the_day
36 {
37 std::vector<game_tip> load(const config& cfg)
38 {
39  std::vector<game_tip> result;
40 
41  for(const auto& tip : cfg.child_range("tip")) {
42  result.emplace_back(tip["text"].t_str(), tip["source"].t_str(), tip["encountered_units"].str());
43  }
44 
45  return result;
46 }
47 
48 std::vector<game_tip> shuffle(const std::vector<game_tip>& tips)
49 {
50  std::vector<game_tip> result = tips;
51  const std::set<std::string>& units = prefs::get().encountered_units();
52 
53  // Remove entries whose filters do not match from the tips list.
54  utils::erase_if(result, [&units](const game_tip& tip) {
55  const auto& filters = tip.unit_filter_;
56 
57  // Filter passes there's no filter at all or if every unit specified has already been
58  // encountered in-game.
59  const bool passes_filter = filters.empty()
60  ? true
61  : std::any_of(filters.begin(), filters.end(), [&units](const std::string& u) {
62  return units.find(u) != units.end();
63  });
64 
65  return !passes_filter;
66  });
67 
68  // Shuffle the list.
69  std::shuffle(result.begin(), result.end(), randomness::rng::default_instance());
70  return result;
71 }
72 
73 } // namespace tips
74 
75 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
child_itors child_range(config_key_type key)
Definition: config.cpp:272
The tips of day structure.
Definition: tips.hpp:54
game_tip(const t_string &text, const t_string &source, const std::string &unit_filter)
Definition: tips.cpp:28
std::set< std::string > & encountered_units()
static prefs & get()
static rng & default_instance()
Definition: random.cpp:73
Definitions for the interface to Wesnoth Markup Language (WML).
static std::unique_ptr< tooltip > tip
Definition: tooltip.cpp:62
std::vector< game_tip > tips
Definition: settings.cpp:47
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:37
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition: tips.cpp:48
Generic file dialog.
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition: general.hpp:106
std::vector< std::string > split(const config_attribute_value &val)