The Battle for Wesnoth  1.15.9+dev
helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 /**
16  * @file
17  * Support functions for dealing with units.
18  */
19 
20 #include "units/unit.hpp"
21 #include "units/helper.hpp"
22 #include "units/types.hpp"
23 #include "resources.hpp"
24 #include "play_controller.hpp"
25 #include "saved_game.hpp"
26 
27 namespace unit_helper {
28 
30 {
31  return u.advances_to().size() + u.get_modification_advances().size();
32 }
33 
35 {
36  if(!u.valid()) {
37  return false;
38  }
40  const auto& active_mods = resources::controller->get_saved_game().classification().active_mods;
41  bool delay_advancements = std::find(active_mods.begin(), active_mods.end(), "delay_advancements") != active_mods.end();
42  if(delay_advancements && resources::controller->current_side() != u->side()) {
43  return false;
44  }
45  }
46  return u->advances() && number_of_possible_advances(*u) > 0;
47 }
48 
49 std::string resistance_color(const int resistance)
50 {
51  if (resistance < 0)
52  return std::string("#FF0000");
53 
54  if (resistance <= 20)
55  return std::string("#FFFF00");
56 
57  if (resistance <= 40)
58  return std::string("#FFFFFF");
59 
60  return std::string("#00FF00");
61 }
62 
63 std::string unit_level_tooltip(const int level, const std::vector<std::string> &adv_to_types, const std::vector<config> &adv_to_mods)
64 {
65  std::ostringstream tooltip;
66  tooltip << _("Level: ") << "<b>" << level << "</b>\n";
67  const bool has_advancements = !adv_to_types.empty() || !adv_to_mods.empty();
68  if(has_advancements) {
69  tooltip << _("Advancements:") << "\n<b>\t";
70  if(!adv_to_types.empty())
71  tooltip << utils::join(adv_to_types, "\n\t");
72  if(!adv_to_mods.empty()) {
73  if(!adv_to_types.empty())
74  tooltip << "\n\t";
75  std::vector<std::string> descriptions;
76  for(const config& adv : adv_to_mods)
77  descriptions.push_back(adv["description"].str());
78  tooltip << utils::join(descriptions, "\n\t");
79  }
80  tooltip << "</b>";
81  } else {
82  tooltip << _("No advancement");
83  }
84  return tooltip.str();
85 }
86 
87 std::string unit_level_tooltip(const unit &u)
88 {
90 }
91 
92 std::string unit_level_tooltip(const unit_type &type)
93 {
94  const auto mod_adv_iters = type.modification_advancements();
95  const std::vector<config> mod_advancements(mod_adv_iters.begin(), mod_adv_iters.end());
96 
97  return unit_level_tooltip(type.level(), type.advances_to(), mod_advancements);
98 }
99 
100 }
play_controller * controller
Definition: resources.cpp:21
saved_game & get_saved_game()
This class represents a single unit of a specific type.
Definition: unit.hpp:119
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
const std::vector< std::string > & advances_to() const
A vector of unit_type ids that this unit_type can advance to.
Definition: types.hpp:117
bool will_certainly_advance(const unit_map::iterator &u)
Encapsulates the logic for deciding whether an iterator u points to a unit that can advance...
Definition: helper.cpp:34
static std::string _(const char *str)
Definition: gettext.hpp:92
A single unit type that the player may recruit.
Definition: types.hpp:44
std::vector< std::string > active_mods
const std::vector< std::string > advances_to_translated() const
Gets the names of the possible types this unit can advance to on level-up.
Definition: unit.cpp:1145
int level() const
The current level of this unit.
Definition: unit.hpp:551
config::const_child_itors modification_advancements() const
Returns two iterators pointing to a range of AMLA configs.
Definition: types.hpp:122
std::string resistance_color(const int resistance)
Definition: helper.cpp:49
int number_of_possible_advances(const unit &u)
Determines the total number of available advancements (of any kind) for a given unit.
Definition: helper.cpp:29
int level() const
Definition: types.hpp:158
game_classification & classification()
Definition: saved_game.hpp:54
std::vector< config > get_modification_advances() const
Gets any non-typed advanced options set by modifications.
Definition: unit.cpp:1721
std::string unit_level_tooltip(const int level, const std::vector< std::string > &adv_to_types, const std::vector< config > &adv_to_mods)
Definition: helper.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
bool valid() const
Definition: map.hpp:273
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
Definition: unit.hpp:233