-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
unit_attack.cpp
299 lines (234 loc) · 8.94 KB
/
unit_attack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (C) 2010 - 2016 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "attack_prediction_display.hpp"
#include "gui/dialogs/unit_attack.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/image.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "game_config.hpp"
#include "gettext.hpp"
#include "help/help.hpp"
#include "language.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "team.hpp"
#include "unit.hpp"
#include "utils/foreach.hpp"
#include <boost/bind.hpp>
namespace gui2
{
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_unit_attack
*
* == Unit attack ==
*
* This shows the dialog for attacking units.
*
* @begin{table}{dialog_widgets}
* attacker_portrait & & image & o & Shows the portrait of the attacking unit.
* $
* attacker_icon & & image & o & Shows the icon of the attacking unit. $
* attacker_name & & control & o & Shows the name of the attacking unit. $
*
* defender_portrait & & image & o & Shows the portrait of the defending unit.
* $
* defender_icon & & image & o & Shows the icon of the defending unit. $
* defender_name & & control & o & Shows the name of the defending unit. $
*
*
* weapon_list & & listbox & m & The list with weapons to choose from. $
* -attacker_weapon & & control & o & The weapon for the attacker to use. $
* -defender_weapon & & control & o & The weapon for the defender to use. $
*
* @end{table}
*/
REGISTER_DIALOG(unit_attack)
tunit_attack::tunit_attack(const unit_map::iterator& attacker_itor,
const unit_map::iterator& defender_itor,
const std::vector<battle_context>& weapons,
const int best_weapon)
: selected_weapon_(-1)
, attacker_itor_(attacker_itor)
, defender_itor_(defender_itor)
, weapons_(weapons)
, best_weapon_(best_weapon)
{
}
template <class T>
static void
set_label(twindow& window, const std::string& id, const std::string& label)
{
T* widget = find_widget<T>(&window, id, false, false);
if(widget) {
widget->set_label(label);
}
}
static std::string format_stats(const unit& u)
{
const std::string name = "<span size='large'>" + (!u.name().empty() ? u.name() : " ") + "</span>";
std::string traits;
BOOST_FOREACH(const std::string& trait, u.get_traits_list()) {
traits += (traits.empty() ? "" : ", ") + trait;
}
if (traits.empty()) {
traits = " ";
}
std::stringstream str;
str << name << "\n";
str << "<small>";
str << "<span color='#f5e6c1'>" << u.type_name() << "</span>" << "\n";
str << "Lvl " << u.level() << "\n";
str << u.alignment() << "\n";
str << traits << "\n";
str << font::span_color(u.hp_color())
<< _("HP: ") << u.hitpoints() << "/" << u.max_hitpoints() << "</span>" << "\n";
str << font::span_color(u.xp_color())
<< _("XP: ") << u.experience() << "/" << u.max_experience() << "</span>" << "\n";
str << "</small>";
return str.str();
}
static std::string get_image_mods(const unit& u)
{
std::string res
= "~RC(" + u.team_color() + ">" + team::get_side_color_index(u.side()) + ")";
if (u.can_recruit()) {
res += "~BLIT(" + unit::leader_crown() + ")";
}
BOOST_FOREACH(const std::string& overlay, u.overlays()) {
res += "~BLIT(" + overlay + ")";
}
return res;
}
static void set_attacker_info(twindow& window, const unit& u)
{
set_label<timage>(window, "attacker_image", u.absolute_image() + get_image_mods(u));
tcontrol& attacker_name =
find_widget<tcontrol>(&window, "attacker_stats", false);
attacker_name.set_use_markup(true);
attacker_name.set_label(format_stats(u));
}
static void set_defender_info(twindow& window, const unit& u)
{
// Ensure the defender image is always facing left
set_label<timage>(window, "defender_image", u.absolute_image() + "~FL(horiz)" + get_image_mods(u));
tcontrol& defender_name =
find_widget<tcontrol>(&window, "defender_stats", false);
defender_name.set_use_markup(true);
defender_name.set_label(format_stats(u));
}
static void set_weapon_info(twindow& window,
const std::vector<battle_context>& weapons,
const int best_weapon)
{
tlistbox& weapon_list
= find_widget<tlistbox>(&window, "weapon_list", false);
window.keyboard_capture(&weapon_list);
const config empty;
const attack_type no_weapon(empty);
FOREACH(const AUTO & weapon, weapons)
{
const battle_context_unit_stats& attacker = weapon.get_attacker_stats();
const battle_context_unit_stats& defender = weapon.get_defender_stats();
const attack_type& attacker_weapon =
*attacker.weapon;
const attack_type& defender_weapon = defender.weapon ?
*defender.weapon : no_weapon;
const SDL_Color a_cth_color =
int_to_color(game_config::red_to_green(attacker.chance_to_hit));
const SDL_Color d_cth_color =
int_to_color(game_config::red_to_green(defender.chance_to_hit));
const std::string attw_name = !attacker_weapon.name().empty() ? attacker_weapon.name() : " ";
const std::string defw_name = !defender_weapon.name().empty() ? defender_weapon.name() : " ";
std::string range = attacker_weapon.range().empty() ? defender_weapon.range() : attacker_weapon.range();
if (!range.empty()) {
range = string_table["range_" + range];
}
const std::string& attw_apecials =
!attacker_weapon.weapon_specials().empty() ? " " + attacker_weapon.weapon_specials() : "";
const std::string& defw_specials =
!defender_weapon.weapon_specials().empty() ? " " + defender_weapon.weapon_specials() : "";
std::stringstream attacker_stats, defender_stats;
attacker_stats << "<b>" << attw_name << "</b>" << "\n"
<< attacker.damage << font::weapon_numbers_sep << attacker_weapon.num_attacks()
<< attw_apecials << "\n"
<< font::span_color(a_cth_color) << attacker.chance_to_hit << "%</span>";
defender_stats << "<b>" << defw_name << "</b>" << "\n"
<< defender.damage << font::weapon_numbers_sep << defender_weapon.num_attacks()
<< defw_specials << "\n"
<< font::span_color(d_cth_color) << defender.chance_to_hit << "%</span>";
std::map<std::string, string_map> data;
string_map item;
item["label"] = attacker_weapon.icon();
data.insert(std::make_pair("attacker_weapon_icon", item));
item["label"] = attacker_stats.str();
item["use_markup"] = "true";
data.insert(std::make_pair("attacker_weapon", item));
item["label"] = "<span color='#a69275'>" + utils::unicode_em_dash + " " + range + " " + utils::unicode_em_dash + "</span>";
item["use_markup"] = "true";
data.insert(std::make_pair("range", item));
item["label"] = defender_stats.str();
item["use_markup"] = "true";
data.insert(std::make_pair("defender_weapon", item));
item["label"] = defender_weapon.icon();
data.insert(std::make_pair("defender_weapon_icon", item));
weapon_list.add_row(data);
}
assert(best_weapon < static_cast<int>(weapon_list.get_item_count()));
weapon_list.select_row(best_weapon);
}
void tunit_attack::profile_button_callback(twindow& window, const std::string& type)
{
help::show_unit_help(window.video(), type);
}
void tunit_attack::damage_calc_callback(twindow& window)
{
const int selection
= find_widget<tlistbox>(&window, "weapon_list", false).get_selected_row();
attack_prediction_displayer predition_dialog(weapons_, (*attacker_itor_).get_location(), (*defender_itor_).get_location());
predition_dialog.button_pressed(selection);
}
void tunit_attack::pre_show(twindow& window)
{
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "attacker_profile", false),
boost::bind(&tunit_attack::profile_button_callback, this, boost::ref(window),
(*attacker_itor_).type_id()));
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "defender_profile", false),
boost::bind(&tunit_attack::profile_button_callback, this, boost::ref(window),
(*defender_itor_).type_id()));
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "damage_calculation", false),
boost::bind(&tunit_attack::damage_calc_callback, this, boost::ref(window)));
set_attacker_info(window, *attacker_itor_);
set_defender_info(window, *defender_itor_);
selected_weapon_ = -1;
set_weapon_info(window, weapons_, best_weapon_);
}
void tunit_attack::post_show(twindow& window)
{
if(get_retval() == twindow::OK) {
selected_weapon_ = find_widget<tlistbox>(&window, "weapon_list", false)
.get_selected_row();
}
}
} // namespace gui2