/
attack_predictions.cpp
388 lines (297 loc) · 11 KB
/
attack_predictions.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright (C) 2010 - 2017 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 "gui/dialogs/attack_predictions.hpp"
#include "attack_prediction.hpp"
#include "color.hpp"
#include "config.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "formula/variant.hpp"
#include "game_board.hpp"
#include "game_config.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/drawing.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "language.hpp"
#include "resources.hpp"
#include "units/abilities.hpp"
#include "units/unit.hpp"
#include <iomanip>
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(attack_predictions)
const unsigned int attack_predictions::graph_width = 270;
const unsigned int attack_predictions::graph_height = 170;
const unsigned int attack_predictions::graph_max_rows = 10;
attack_predictions::attack_predictions(battle_context& bc, const unit& attacker, const unit& defender)
: attacker_data_(attacker, bc.get_attacker_combatant(), bc.get_attacker_stats())
, defender_data_(defender, bc.get_defender_combatant(), bc.get_defender_stats())
{
}
void attack_predictions::pre_show(window& window)
{
set_data(window, attacker_data_, defender_data_);
set_data(window, defender_data_, attacker_data_);
}
static std::string get_probability_string(const double prob)
{
std::ostringstream ss;
if(prob > 0.9995) {
ss << "100%";
} else {
ss << std::fixed << std::setprecision(1) << 100.0 * prob << '%';
}
return ss.str();
}
void attack_predictions::set_data(window& window, const combatant_data& attacker, const combatant_data& defender)
{
// Each data widget in this dialog has its id prefixed by either of these identifiers.
const std::string widget_id_prefix = attacker.stats_.is_attacker ? "attacker" : "defender";
const auto get_prefixed_widget_id = [&widget_id_prefix](const std::string& id) {
return (formatter() << widget_id_prefix << "_" << id).str();
};
// Helpers for setting or hiding labels
const auto set_label_helper = [&](const std::string& id, const std::string& value) {
find_widget<label>(&window, get_prefixed_widget_id(id), false).set_label(value);
};
const auto hide_label_helper = [&](const std::string& id) {
find_widget<label>(&window, get_prefixed_widget_id(id), false).set_visible(widget::visibility::invisible);
find_widget<label>(&window, get_prefixed_widget_id(id) + "_label" , false).set_visible(widget::visibility::invisible);
};
std::stringstream ss;
//
// Always visible fields
//
// Unscathed probability
const color_t ndc_color = game_config::red_to_green(attacker.combatant_.untouched * 100);
ss << font::span_color(ndc_color) << get_probability_string(attacker.combatant_.untouched) << "</span>";
set_label_helper("chance_unscathed", ss.str());
// HP probability graph
drawing& graph_widget = find_widget<drawing>(&window, get_prefixed_widget_id("hp_graph"), false);
draw_hp_graph(graph_widget, attacker, defender);
//
// Weapon detail fields (only shown if a weapon is present)
//
if(!attacker.stats_.weapon) {
set_label_helper("base_damage", _("No usable weapon"));
// FIXME: would rather have a list somewhere that I can loop over instead of hardcoding...
hide_label_helper("tod_modifier");
hide_label_helper("leadership_modifier");
hide_label_helper("slowed_modifier");
return;
}
ss.str("");
// Set specials context (for safety, it should not have changed normally).
const attack_type* weapon = attacker.stats_.weapon;
weapon->set_specials_context(attacker.unit_.get_location(), defender.unit_.get_location(), attacker.stats_.is_attacker, defender.stats_.weapon);
// Get damage modifiers.
unit_ability_list dmg_specials = weapon->get_specials("damage");
unit_abilities::effect dmg_effect(dmg_specials, weapon->damage(), attacker.stats_.backstab_pos);
// Get the SET damage modifier, if any.
auto set_dmg_effect = std::find_if(dmg_effect.begin(), dmg_effect.end(),
[](const unit_abilities::individual_effect& e) { return e.type == unit_abilities::SET; }
);
// Either user the SET modifier or the base weapon damage.
if(set_dmg_effect == dmg_effect.end()) {
ss << weapon->damage() << " (<i>" << weapon->name() << "</i>)";
} else {
assert(set_dmg_effect->ability);
ss << set_dmg_effect->value << " (" << (*set_dmg_effect->ability)["name"] << ")";
}
// Process the ADD damage modifiers.
for(const auto& e : dmg_effect) {
if(e.type == unit_abilities::ADD) {
ss << "\n";
ss << (*e.ability)["name"] << ": ";
if(e.value >= 0) {
ss << '+';
}
ss << e.value;
}
}
// Process the MUL damage modifiers.
for(const auto& e : dmg_effect) {
if(e.type == unit_abilities::MUL) {
ss << "\n";
ss << (*e.ability)["name"] << ": " << font::unicode_multiplication_sign << (e.value / 100);
if(e.value % 100) {
ss << "." << ((e.value % 100) / 10);
if(e.value % 10) {
ss << (e.value % 10);
}
}
}
}
set_label_helper("base_damage", ss.str());
ss.str("");
// Resistance modifier.
const int resistance_modifier = defender.unit_.damage_from(*weapon, !attacker.stats_.is_attacker, defender.unit_.get_location());
if(resistance_modifier != 100) {
if(attacker.stats_.is_attacker) {
ss << _("Defender resistance vs") << " ";
} else {
ss << _("Attacker vulnerability vs") << " ";
}
ss << string_table["type_" + weapon->type()];
set_label_helper("resis_label", ss.str());
ss.str("");
ss << font::unicode_multiplication_sign << (resistance_modifier / 100) << "." << ((resistance_modifier % 100) / 10);
set_label_helper("resis", ss.str());
}
ss.str("");
// TODO: color format the modifiers
// Time of day modifier.
const unit& u = attacker.unit_;
const int tod_modifier = combat_modifier(resources::gameboard->units(), resources::gameboard->map(),
u.get_location(), u.alignment(), u.is_fearless());
if(tod_modifier != 0) {
set_label_helper("tod_modifier", utils::signed_percent(tod_modifier));
} else {
hide_label_helper("tod_modifier");
}
// Leadership bonus.
int leadership_bonus = 0;
under_leadership(resources::gameboard->units(), attacker.unit_.get_location(), &leadership_bonus);
if(leadership_bonus != 0) {
set_label_helper("leadership_modifier", utils::signed_percent(leadership_bonus));
} else {
hide_label_helper("leadership_modifier");
}
// Slowed penalty.
if(attacker.stats_.is_slowed) {
set_label_helper("slowed_modifier", "/ 2");
} else {
hide_label_helper("slowed_modifier");
}
// Total damage.
const int base_damage = weapon->damage();
color_t dmg_color = font::weapon_color;
if(attacker.stats_.damage > base_damage) {
dmg_color = font::good_dmg_color;
} else if(attacker.stats_.damage < base_damage) {
dmg_color = font::bad_dmg_color;
}
ss << font::span_color(dmg_color) << attacker.stats_.damage << "</span>"
<< font::weapon_numbers_sep << attacker.stats_.num_blows;
set_label_helper("total_damage", ss.str());
// Chance to hit
const color_t cth_color = game_config::red_to_green(attacker.stats_.chance_to_hit);
ss.str("");
ss << font::span_color(cth_color) << attacker.stats_.chance_to_hit << "%</span>";
set_label_helper("chance_to_hit", ss.str());
}
void attack_predictions::draw_hp_graph(drawing& hp_graph, const combatant_data& attacker, const combatant_data& defender)
{
// Font size. If you change this, you must update the separator space.
// TODO: probably should remove this.
const int fs = font::SIZE_SMALL;
// Space before HP separator.
const int hp_sep = 30;
// Space after percentage separator.
const int percent_sep = 50;
// Bar space between both separators.
const int bar_space = graph_width - hp_sep - percent_sep - 4;
// Set some variables for the WML portion of the graph to use.
canvas& hp_graph_canvas = hp_graph.get_drawing_canvas();
hp_graph_canvas.set_variable("hp_column_width", variant(hp_sep));
hp_graph_canvas.set_variable("chance_column_width", variant(percent_sep));
config cfg, shape;
int i = 0;
// Draw the rows (lower HP values are at the bottom).
for(const auto& probability : get_hitpoint_probabilities(attacker.combatant_.hp_dist)) {
// Get the HP and probability.
int hp; double prob;
std::tie(hp, prob) = probability;
color_t row_color;
// Death line is red.
if(hp == 0) {
row_color = {229, 0, 0};
}
// Below current hitpoints value is orange.
else if(hp < static_cast<int>(attacker.stats_.hp)) {
// Stone is grey.
if(defender.stats_.petrifies) {
row_color = {154, 154, 154};
} else {
row_color = {244, 201, 0};
}
}
// Current hitpoints value and above is green.
else {
row_color = {8, 202, 0};
}
shape["text"] = hp;
shape["x"] = 4;
shape["y"] = 2 + (fs + 2) * i;
shape["w"] = "(text_width)";
shape["h"] = "(text_height)";
shape["font_size"] = 12;
shape["color"] = "255, 255, 255, 255";
shape["text_alignment"] = "(text_alignment)";
cfg.add_child("text", shape);
shape.clear();
shape["text"] = get_probability_string(prob);
shape["x"] = graph_width - percent_sep + 2;
shape["y"] = 2 + (fs + 2) * i;
shape["w"] = "(text_width)";
shape["h"] = "(text_height)";
shape["font_size"] = 12;
shape["color"] = "255, 255, 255, 255";
shape["text_alignment"] = "(text_alignment)";
cfg.add_child("text", shape);
const int bar_len = std::max(static_cast<int>((prob * (bar_space - 4)) + 0.5), 2);
const SDL_Rect bar_rect_1 {
hp_sep + 4,
6 + (fs + 2) * i,
bar_len,
8
};
shape.clear();
shape["x"] = bar_rect_1.x;
shape["y"] = bar_rect_1.y;
shape["w"] = bar_rect_1.w;
shape["h"] = bar_rect_1.h;
shape["fill_color"] = row_color.to_rgba_string();
cfg.add_child("rectangle", shape);
++i;
}
hp_graph.append_drawing_data(cfg);
}
hp_probability_vector attack_predictions::get_hitpoint_probabilities(const std::vector<double>& hp_dist)
{
hp_probability_vector res, temp_vec;
// First, extract any relevant probability values
for(int i = 0; i < static_cast<int>(hp_dist.size()); ++i) {
const double prob = hp_dist[i];
// We keep only values above 0.1%.
if(prob > 0.001) {
temp_vec.push_back({i, prob});
}
}
// Then sort by descending probability.
std::sort(temp_vec.begin(), temp_vec.end(), [](const hp_probability_t& pair1, const hp_probability_t& pair2) {
return pair1.second > pair2.second;
});
// Take only the highest probability values.;
std::copy_n(temp_vec.begin(), std::min<int>(graph_max_rows, temp_vec.size()), std::back_inserter(res));
// Then, we sort the hitpoint values in descending order.
std::sort(res.begin(), res.end(), std::greater<hp_probability_t>());
return res;
}
} // namespace dialogs
} // namespace gui2