The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
tips.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 - 2025
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
19
20#include "config.hpp"
22#include "random.hpp"
24#include "utils/general.hpp"
25
26namespace gui2
27{
29 : text(cfg["text"].t_str())
30 , source(cfg["source"].t_str())
31 , unit_filter(utils::split(cfg["encountered_units"]))
32{
33}
34
35namespace tip_of_the_day
36{
37std::vector<game_tip> load(const config& cfg)
38{
39 const auto range = cfg.child_range("tip");
40 return { range.begin(), range.end() };
41}
42
43std::vector<game_tip> shuffle(const std::vector<game_tip>& tips)
44{
45 std::vector<game_tip> result = tips;
46 const std::set<std::string>& units = prefs::get().encountered_units();
47
48 // Remove entries whose filters do not match from the tips list.
49 utils::erase_if(result, [&units](const game_tip& tip) {
50 const auto& must_have_seen = tip.unit_filter;
51
52 // No units to encounter, tip is always visible.
53 if(must_have_seen.empty()) {
54 return false;
55 }
56
57 // At least one given unit type must have been encountered.
58 return std::none_of(must_have_seen.begin(), must_have_seen.end(),
59 [&units](const std::string& u) { return utils::contains(units, u); });
60 });
61
62 // Shuffle the list.
63 std::shuffle(result.begin(), result.end(), randomness::rng::default_instance());
64 return result;
65}
66
67} // namespace tip_of_the_day
68
69} // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
child_itors child_range(std::string_view key)
Definition config.cpp:268
static prefs & get()
std::set< std::string > & encountered_units()
static rng & default_instance()
Definition random.cpp:73
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
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:43
Generic file dialog.
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition general.hpp:108
game_tip(const config &cfg)
Definition tips.cpp:28
std::vector< std::string > unit_filter
List of units to filter the tip upon.
Definition tips.hpp:68