The Battle for Wesnoth  1.17.4+dev
unit_attack.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2022
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 "font/text_formatting.hpp"
23 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/label.hpp"
25 #include "gui/widgets/image.hpp"
26 #include "gui/widgets/listbox.hpp"
27 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/window.hpp"
30 #include "game_config.hpp"
31 #include "game_display.hpp"
32 #include "gettext.hpp"
33 #include "help/help.hpp"
34 #include "language.hpp"
35 #include "color.hpp"
36 #include "units/unit.hpp"
37 
38 #include <functional>
39 
40 namespace gui2::dialogs
41 {
42 
44 
45 unit_attack::unit_attack(const unit_map::iterator& attacker_itor,
46  const unit_map::iterator& defender_itor,
47  std::vector<battle_context>&& weapons,
48  const int best_weapon)
49  : selected_weapon_(-1)
50  , attacker_itor_(attacker_itor)
51  , defender_itor_(defender_itor)
52  , weapons_(std::move(weapons))
53  , best_weapon_(best_weapon)
54 {
55 }
56 
58 {
59  const std::size_t index = find_widget<listbox>(get_window(), "weapon_list", false).get_selected_row();
60  attack_predictions::display(weapons_[index], attacker_itor_.get_shared_ptr(), defender_itor_.get_shared_ptr());
61 }
62 
64 {
66  find_widget<button>(&window, "damage_calculation", false),
67  std::bind(&unit_attack::damage_calc_callback, this));
68 
69  find_widget<unit_preview_pane>(&window, "attacker_pane", false)
70  .set_displayed_unit(*attacker_itor_);
71 
72  find_widget<unit_preview_pane>(&window, "defender_pane", false)
73  .set_displayed_unit(*defender_itor_);
74 
75  selected_weapon_ = -1;
76 
77  listbox& weapon_list = find_widget<listbox>(&window, "weapon_list", false);
78  window.keyboard_capture(&weapon_list);
79 
80  // Possible TODO: If a "blank weapon" is generally useful, add it as a static member in attack_type.
81  static const config empty;
82  static const_attack_ptr no_weapon(new attack_type(empty));
83 
84  for(const auto & weapon : weapons_) {
85  const battle_context_unit_stats& attacker = weapon.get_attacker_stats();
86  const battle_context_unit_stats& defender = weapon.get_defender_stats();
87 
88  const attack_type& attacker_weapon =
89  *attacker.weapon;
90  const attack_type& defender_weapon = defender.weapon ?
91  *defender.weapon : *no_weapon;
92 
93  const color_t a_cth_color = game_config::red_to_green(attacker.chance_to_hit);
94  const color_t d_cth_color = game_config::red_to_green(defender.chance_to_hit);
95 
96  const std::string attw_name = !attacker_weapon.name().empty() ? attacker_weapon.name() : " ";
97  const std::string defw_name = !defender_weapon.name().empty() ? defender_weapon.name() : " ";
98 
99  std::string range = attacker_weapon.range().empty() ? defender_weapon.range() : attacker_weapon.range();
100  if (!range.empty()) {
101  range = string_table["range_" + range];
102  }
103 
104  auto a_ctx = attacker_weapon.specials_context(
107  attacker_itor_->get_location(),
108  defender_itor_->get_location(), true, defender.weapon
109  );
110 
111  auto d_ctx = defender_weapon.specials_context(
114  defender_itor_->get_location(),
115  attacker_itor_->get_location(), false, attacker.weapon
116  );
117 
118  const std::set<std::string> checking_tags_other = {"disable", "berserk", "drains", "heal_on_hit", "plague", "slow", "petrifies", "firststrike", "poison"};
119  std::string attw_specials = attacker_weapon.weapon_specials(attacker.backstab_pos);
120  std::string attw_specials_dmg = attacker_weapon.weapon_specials_value({"leadership", "damage"});
121  std::string attw_specials_atk = attacker_weapon.weapon_specials_value({"attacks", "swarm"});
122  std::string attw_specials_cth = attacker_weapon.weapon_specials_value({"chance_to_hit"});
123  std::string attw_specials_others = attacker_weapon.weapon_specials_value(checking_tags_other);
124  bool defender_attack = !(defender_weapon.name().empty() && defender_weapon.damage() == 0 && defender_weapon.num_attacks() == 0 && defender.chance_to_hit == 0);
125  std::string defw_specials = defender_attack ? defender_weapon.weapon_specials() : "";
126  std::string defw_specials_dmg = defender_attack ? defender_weapon.weapon_specials_value({"leadership", "damage"}) : "";
127  std::string defw_specials_atk = defender_attack ? defender_weapon.weapon_specials_value({"attacks", "swarm"}) : "";
128  std::string defw_specials_cth = defender_attack ? defender_weapon.weapon_specials_value({"chance_to_hit"}) : "";
129  std::string defw_specials_others = defender_attack ? defender_weapon.weapon_specials_value(checking_tags_other) : "";
130 
131  if(!attw_specials.empty()) {
132  attw_specials = " " + attw_specials;
133  }
134  if(!attw_specials_dmg.empty()) {
135  attw_specials_dmg = " " + attw_specials_dmg;
136  }
137  if(!attw_specials_atk.empty()) {
138  attw_specials_atk = " " + attw_specials_atk;
139  }
140  if(!attw_specials_cth.empty()) {
141  attw_specials_cth = " " + attw_specials_cth;
142  }
143  if(!attw_specials_others.empty()) {
144  attw_specials_others = "\n" + ("<b>"+translation::dsgettext("wesnoth", "Other aspects: ")+"</b>") + "\n" + ("<i>"+attw_specials_others+"</i>");
145  }
146  if(!defw_specials.empty()) {
147  defw_specials = " " + defw_specials;
148  }
149  if(!defw_specials_dmg.empty()) {
150  defw_specials_dmg = " " + defw_specials_dmg;
151  }
152  if(!defw_specials_atk.empty()) {
153  defw_specials_atk = " " + defw_specials_atk;
154  }
155  if(!defw_specials_cth.empty()) {
156  defw_specials_cth = " " + defw_specials_cth;
157  }
158  if(!defw_specials_others.empty()) {
159  defw_specials_others = "\n" + ("<b>"+translation::dsgettext("wesnoth", "Other aspects: ")+"</b>") + "\n" + ("<i>"+defw_specials_others+"</i>");
160  }
161 
162  std::stringstream attacker_stats, defender_stats, attacker_tooltip, defender_tooltip;
163 
164  // Use attacker/defender.num_blows instead of attacker/defender_weapon.num_attacks() because the latter does not consider the swarm weapon special
165  attacker_stats << "<b>" << attw_name << "</b>" << "\n"
166  << attacker.damage << font::weapon_numbers_sep << attacker.num_blows
167  << attw_specials << "\n"
168  << font::span_color(a_cth_color) << attacker.chance_to_hit << "%</span>";
169 
170  attacker_tooltip << translation::dsgettext("wesnoth", "Weapon: ") << "<b>" << attw_name << "</b>" << "\n"
171  << translation::dsgettext("wesnoth", "Damage: ") << attacker.damage << "<i>" << attw_specials_dmg << "</i>" << "\n"
172  << translation::dsgettext("wesnoth", "Attacks: ") << attacker.num_blows << "<i>" << attw_specials_atk << "</i>" << "\n"
173  << translation::dsgettext("wesnoth", "Chance to hit: ") << font::span_color(a_cth_color) << attacker.chance_to_hit << "%</span>"<< "<i>" << attw_specials_cth << "</i>"
174  << attw_specials_others;
175 
176  defender_stats << "<b>" << defw_name << "</b>" << "\n"
177  << defender.damage << font::weapon_numbers_sep << defender.num_blows
178  << defw_specials << "\n"
179  << font::span_color(d_cth_color) << defender.chance_to_hit << "%</span>";
180 
181  defender_tooltip << translation::dsgettext("wesnoth", "Weapon: ") << "<b>" << defw_name << "</b>" << "\n"
182  << translation::dsgettext("wesnoth", "Damage: ") << defender.damage << "<i>" << defw_specials_dmg << "</i>" << "\n"
183  << translation::dsgettext("wesnoth", "Attacks: ") << defender.num_blows << "<i>" << defw_specials_atk << "</i>" << "\n"
184  << translation::dsgettext("wesnoth", "Chance to hit: ") << font::span_color(d_cth_color) << defender.chance_to_hit << "%</span>"<< "<i>" << defw_specials_cth << "</i>"
185  << defw_specials_others;
186 
187  std::map<std::string, string_map> data;
189 
190  item["use_markup"] = "true";
191 
192  item["label"] = attacker_weapon.icon();
193  data.emplace("attacker_weapon_icon", item);
194 
195  item["tooltip"] = attacker_tooltip.str();
196  item["label"] = attacker_stats.str();
197  data.emplace("attacker_weapon", item);
198  item["tooltip"] = "";
199 
200  item["label"] = "<span color='#a69275'>" + font::unicode_em_dash + " " + range + " " + font::unicode_em_dash + "</span>";
201  data.emplace("range", item);
202 
203  item["tooltip"] = defender_attack ? defender_tooltip.str() : "";
204  item["label"] = defender_stats.str();
205  data.emplace("defender_weapon", item);
206 
207  item["tooltip"] = "";
208  item["label"] = defender_weapon.icon();
209  data.emplace("defender_weapon_icon", item);
210 
211  weapon_list.add_row(data);
212  }
213 
214  // If these two aren't the same size, we can't use list selection incides
215  // to access to weapons list!
216  assert(weapons_.size() == weapon_list.get_item_count());
217 
218  weapon_list.select_row(best_weapon_);
219 }
220 
222 {
223  if(get_retval() == retval::OK) {
224  selected_weapon_ = find_widget<listbox>(&window, "weapon_list", false).get_selected_row();
225  }
226 }
227 
228 } // namespace dialogs
const_attack_ptr weapon
The weapon used by the unit to attack the opponent, or nullptr if there is none.
Definition: attack.hpp:53
std::string weapon_specials_value(const std::set< std::string > checking_tags) const
Definition: abilities.cpp:905
int best_weapon_
The best weapon, aka the one high-lighted.
Definition: unit_attack.hpp:78
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:172
This file contains the window object, this object is a top level container which has the event manage...
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
int num_attacks() const
Definition: attack_type.hpp:54
unsigned int chance_to_hit
Effective chance to hit as a percentage (all factors accounted for).
Definition: attack.hpp:76
std::string dsgettext(const char *domainname, const char *msgid)
Definition: gettext.cpp:426
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:246
std::vector< battle_context > weapons_
List of all battle contexts used for getting the weapons.
Definition: unit_attack.hpp:75
The listbox class.
Definition: listbox.hpp:45
bool backstab_pos
True if the attacker is in position to backstab the defender (this is used to determine whether to ap...
Definition: attack.hpp:63
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
int damage
Effective damage of the weapon (all factors accounted for).
Definition: attack.hpp:77
This file contains the settings handling of the widget library.
const t_string & name() const
Definition: attack_type.hpp:43
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:173
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
const std::string & range() const
Definition: attack_type.hpp:47
std::string weapon_specials(bool is_backstab=false) const
Returns a comma-separated string of active names for the specials of *this.
Definition: abilities.cpp:862
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:127
void unit_attack(display *disp, game_board &board, const map_location &a, const map_location &b, int damage, const attack_type &attack, const_attack_ptr secondary_attack, int swing, const std::string &hit_text, int drain_amount, const std::string &att_text, const std::vector< std::string > *extra_hit_sounds, bool attacking)
Make the unit on tile &#39;a&#39; attack the unit on tile &#39;b&#39;.
Definition: udisplay.cpp:600
const std::string & icon() const
Definition: attack_type.hpp:46
Structure describing the statistics of a unit involved in the battle.
Definition: attack.hpp:51
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:218
int damage() const
Definition: attack_type.hpp:53
int selected_weapon_
The index of the selected weapon.
Definition: unit_attack.hpp:66
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:62
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
int get_retval() const
Returns the cached window exit code.
bool empty() const
Definition: tstring.hpp:187
const std::string weapon_numbers_sep
Definition: constants.cpp:49
color_t red_to_green(int val, bool for_text)
Return a color corresponding to the value val red for val=0 to green for val=100, passing by yellow...
This shows the dialog for attacking units.
Definition: unit_attack.hpp:41
symbol_table string_table
Definition: language.cpp:65
const std::string unicode_em_dash
Definition: constants.cpp:44
unit_map::iterator attacker_itor_
Iterator pointing to the attacker.
Definition: unit_attack.hpp:69
Container associating units to locations.
Definition: map.hpp:98
unsigned int num_blows
Effective number of blows, takes swarm into account.
Definition: attack.hpp:81
specials_context_t specials_context(unit_const_ptr self, unit_const_ptr other, const map_location &unit_loc, const map_location &other_loc, bool attacking, const_attack_ptr other_attack) const
Dialog was closed with the OK button.
Definition: retval.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: unit_attack.cpp:63
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
unit_map::iterator defender_itor_
Iterator pointing to the defender.
Definition: unit_attack.hpp:72