gui/auxiliary/tips.cpp

Go to the documentation of this file.
00001 /* $Id: tips.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/auxiliary/tips.hpp"
00019 
00020 #include "config.hpp"
00021 #include "foreach.hpp"
00022 #include "game_preferences.hpp"
00023 #include "serialization/string_utils.hpp"
00024 
00025 namespace gui2 {
00026 
00027 ttip::ttip(const t_string& text
00028         , const t_string& source
00029         , const std::string& unit_filter)
00030     : text_(text)
00031     , source_(source)
00032     , unit_filter_(utils::split(unit_filter))
00033 {
00034 }
00035 
00036 namespace tips {
00037 
00038 std::vector<ttip> load(const config& cfg)
00039 {
00040     std::vector<ttip> result;
00041 
00042     foreach(const config &tip, cfg.child_range("tip")) {
00043         result.push_back(ttip(tip["text"]
00044                 , tip["source"]
00045                 , tip["encountered_units"]));
00046     }
00047 
00048     return result;
00049 }
00050 
00051 std::vector<ttip> shuffle(const std::vector<ttip>& tips)
00052 {
00053     std::vector<ttip> result;
00054 
00055     const std::set<std::string>& units = preferences::encountered_units();
00056 
00057     foreach(const ttip& tip, tips) {
00058         if(tip.unit_filter_.empty()) {
00059             result.push_back(tip);
00060         } else {
00061             foreach(const std::string& unit, tip.unit_filter_) {
00062                 if(units.find(unit) != units.end()) {
00063                     result.push_back(tip);
00064                     break;
00065                 }
00066             }
00067         }
00068     }
00069 
00070     std::random_shuffle(result.begin(), result.end());
00071     return result;
00072 }
00073 
00074 } // namespace tips
00075 
00076 } // namespace gui2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs