The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
attack_predictions.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 - 2025
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 "attack_prediction.hpp"
21#include "color.hpp"
22#include "config.hpp"
24#include "formatter.hpp"
25#include "formula/variant.hpp"
26#include "game_board.hpp"
28#include "game_config.hpp"
30#include "gui/widgets/label.hpp"
31#include "gettext.hpp"
32#include "language.hpp"
33#include "resources.hpp"
34#include "units/abilities.hpp"
35#include "units/unit.hpp"
36
37#include <iomanip>
38#include <utility>
39
40namespace gui2::dialogs
41{
42
43REGISTER_DIALOG(attack_predictions)
44
45const unsigned int attack_predictions::graph_width = 270;
46const unsigned int attack_predictions::graph_height = 170;
47const unsigned int attack_predictions::graph_max_rows = 10;
48
50 battle_context& bc, unit_const_ptr attacker, unit_const_ptr defender)
51 : modal_dialog(window_id())
52 , attacker_data_(std::move(attacker), bc.get_attacker_combatant(), bc.get_attacker_stats())
53 , defender_data_(std::move(defender), bc.get_defender_combatant(), bc.get_defender_stats())
54{
55}
56
58{
59 label& title = find_widget<label>("title");
60 title.set_label(_("Damage Calculations") + " (" + rng_mode_name() + ")");
62
65}
66
68{
70
71 if(mode == "biased") {
72 return _("Reduced RNG");
73 }
74
75 if(mode == "deterministic") {
76 return _("Predictable RNG");
77 }
78
79 return _("Default RNG");
80}
81
83{
85
86 // These are the same strings as the options in the campaign selection dialog.
87 std::string description;
88 if(mode == "biased") {
89 // xgettext: no-c-format
90 description = _("Hits and misses are much more consistent. This tends to make small-scale engagements easier to plan.\n\nExample: if you strike three times with 50% accuracy, you will always hit at least once and miss at least once.");
91 } else if(mode == "deterministic") {
92 description = _("Identical to Default RNG, except loading a saved game will not change the outcome of an attack.\n\nExample: you strike twice and get lucky, hitting both strikes. You then load an earlier save and make the same attack again. Both strikes will still hit.");
93 } else {
94 // xgettext: no-c-format
95 description = _("Pure, unbiased randomness; the way Wesnoth is intended to be played.\n\nExample: if you strike twice with 50% accuracy, you’re most likely to hit once and miss once, but might also hit twice or miss twice.");
96 }
97
98 return rng_mode_name() + ": " + description;
99}
100
101static std::string get_probability_string(const double prob)
102{
103 std::ostringstream ss;
104 ss << std::fixed << std::setprecision(1) << 100.0 * prob << '%';
105
106 return ss.str();
107}
108
109void attack_predictions::set_data(const combatant_data& attacker, const combatant_data& defender)
110{
111 // Each data widget in this dialog has its id prefixed by either of these identifiers.
112 const std::string widget_id_prefix = attacker.stats_.is_attacker ? "attacker" : "defender";
113
114 const auto get_prefixed_widget_id = [&widget_id_prefix](const std::string& id) {
115 return (formatter() << widget_id_prefix << "_" << id).str();
116 };
117
118 // Helpers for setting or hiding labels
119 const auto set_label_helper = [&, this](const std::string& id, const std::string& value) {
120 // MSVC does not compile without this-> (26-09-2024)
121 label& lbl = this->find_widget<label>(get_prefixed_widget_id(id));
122 lbl.set_label(value);
123 };
124
125 const auto hide_label_helper = [&, this](const std::string& id) {
126 // MSVC does not compile without this-> (26-09-2024)
127 label& lbl = this->find_widget<label>(get_prefixed_widget_id(id));
129 label& lbl2 = this->find_widget<label>(get_prefixed_widget_id(id) + "_label");
131 };
132
133 std::stringstream ss;
134
135 //
136 // Always visible fields
137 //
138
139 // Unscathed probability
140 const color_t ndc_color = game_config::red_to_green(attacker.combatant_.untouched * 100);
141
143 set_label_helper("chance_unscathed", ss.str());
144
145 // HP probability graph
146 drawing& graph_widget = find_widget<drawing>(get_prefixed_widget_id("hp_graph"));
147 draw_hp_graph(graph_widget, attacker, defender);
148
149 //
150 // Weapon detail fields (only shown if a weapon is present)
151 //
152
153 if(!attacker.stats_.weapon) {
154 set_label_helper("base_damage", _("No usable weapon"));
155
156 // FIXME: would rather have a list somewhere that I can loop over instead of hardcoding...
157 hide_label_helper("tod_modifier");
158 hide_label_helper("leadership_modifier");
159 hide_label_helper("slowed_modifier");
160
161 return;
162 }
163
164 ss.str("");
165
166 // Set specials context (for safety, it should not have changed normally).
167 const_attack_ptr weapon = attacker.stats_.weapon, opp_weapon = defender.stats_.weapon;
168
169 auto ctx = specials_context_t::make(
170 { attacker.unit_, attacker.unit_->get_location(), weapon },
171 { defender.unit_, defender.unit_->get_location(), opp_weapon }, attacker.stats_.is_attacker);
172
173 // Get damage modifiers.
174 active_ability_list dmg_specials = weapon->get_specials_and_abilities("damage");
175 unit_abilities::effect dmg_effect(dmg_specials, weapon->damage());
176
177 // Get the SET damage modifier, if any.
178 auto set_dmg_effect = std::find_if(dmg_effect.begin(), dmg_effect.end(),
179 [](const unit_abilities::individual_effect& e) { return e.type == unit_abilities::SET; }
180 );
181
182 // Either user the SET modifier or the base weapon damage.
183 if(set_dmg_effect == dmg_effect.end()) {
184 ss << weapon->damage() << " (" << markup::italic(weapon->name()) << ")";
185 } else {
186 assert(set_dmg_effect->ability);
187 ss << set_dmg_effect->value << " (" << markup::italic((*set_dmg_effect->ability)["name"]) << ")";
188 }
189
190 // Process the ADD damage modifiers.
191 for(const auto& e : dmg_effect) {
192 if(e.type == unit_abilities::ADD) {
193 ss << "\n";
194
195 if(e.value >= 0) {
196 ss << '+';
197 }
198
199 ss << e.value;
200 ss << " (" << markup::italic((*e.ability)["name"]) << ")";
201 }
202 }
203
204 // Process the MUL damage modifiers.
205 for(const auto& e : dmg_effect) {
206 if(e.type == unit_abilities::MUL) {
207 ss << "\n";
208 ss << font::unicode_multiplication_sign << (e.value / 100);
209
210 if(e.value % 100) {
211 ss << "." << ((e.value % 100) / 10);
212 if(e.value % 10) {
213 ss << (e.value % 10);
214 }
215 }
216
217 ss << " (" << markup::italic((*e.ability)["name"]) << ")";
218 }
219 }
220
221 set_label_helper("base_damage", ss.str());
222
223 ss.str("");
224
225 // Resistance modifier.
226 const auto [damage_type, resistance_modifier] = weapon->effective_damage_type();
227 if(resistance_modifier != 100) {
228 if(attacker.stats_.is_attacker) {
229 if(resistance_modifier < 100) {
230 ss << _("Defender resistance vs") << " ";
231 } else {
232 ss << _("Defender vulnerability vs") << " ";
233 }
234 } else {
235 if(resistance_modifier < 100) {
236 ss << _("Attacker resistance vs") << " ";
237 } else {
238 ss << _("Attacker vulnerability vs") << " ";
239 }
240 }
241
242 ss << string_table["type_" + damage_type];
243
244 set_label_helper("resis_label", ss.str());
245
246 ss.str("");
247 ss << font::unicode_multiplication_sign << (resistance_modifier / 100) << "." << ((resistance_modifier % 100) / 10);
248
249 set_label_helper("resis", ss.str());
250 }
251
252 ss.str("");
253
254 // TODO: color format the modifiers
255
256 // Time of day modifier.
257 const unit& u = *attacker.unit_;
258
259 unit_alignments::type alignment = weapon->alignment().value_or(u.alignment());
260 const int tod_modifier = combat_modifier(resources::gameboard->units(), resources::gameboard->map(),
261 u.get_location(), alignment, u.is_fearless());
262
263 if(tod_modifier != 0) {
264 set_label_helper("tod_modifier", utils::signed_percent(tod_modifier));
265 } else {
266 hide_label_helper("tod_modifier");
267 }
268
269 if(attacker.stats_.leadership_bonus != 0) {
270 set_label_helper("leadership_modifier", utils::signed_percent(attacker.stats_.leadership_bonus));
271 } else {
272 hide_label_helper("leadership_modifier");
273 }
274
275 // Slowed penalty.
276 if(attacker.stats_.is_slowed) {
277 set_label_helper("slowed_modifier", "/ 2");
278 } else {
279 hide_label_helper("slowed_modifier");
280 }
281
282 // Total damage.
283 const int base_damage = weapon->damage();
284
285 color_t dmg_color = font::weapon_color;
286 if(attacker.stats_.damage > base_damage) {
287 dmg_color = font::good_dmg_color;
288 } else if(attacker.stats_.damage < base_damage) {
289 dmg_color = font::bad_dmg_color;
290 }
291
292 ss << markup::span_color(dmg_color, attacker.stats_.damage)
294
295 set_label_helper("total_damage", ss.str());
296
297 // Chance to hit
298 const color_t cth_color = game_config::red_to_green(attacker.stats_.chance_to_hit);
299
300 ss.str("");
301 ss << markup::span_color(cth_color, attacker.stats_.chance_to_hit, "%");
302
303 set_label_helper("chance_to_hit", ss.str());
304}
305
306void attack_predictions::draw_hp_graph(drawing& hp_graph, const combatant_data& attacker, const combatant_data& defender)
307{
308 // Font size. If you change this, you must update the separator space.
309 // TODO: probably should remove this.
310 const int fs = font::SIZE_SMALL;
311
312 // Space before HP separator.
313 const int hp_sep = 30;
314
315 // Space after percentage separator.
316 const int percent_sep = 50;
317
318 // Bar space between both separators.
319 const int bar_space = graph_width - hp_sep - percent_sep - 4;
320
321 // Set some variables for the WML portion of the graph to use.
322 canvas& hp_graph_canvas = hp_graph.get_drawing_canvas();
323
324 hp_graph_canvas.set_variable("hp_column_width", wfl::variant(hp_sep));
325 hp_graph_canvas.set_variable("chance_column_width", wfl::variant(percent_sep));
326
327 config cfg, shape;
328
329 int i = 0;
330
331 // Draw the rows (lower HP values are at the bottom).
332 for(const auto& probability : get_hitpoint_probabilities(attacker.combatant_.hp_dist)) {
333
334 // Get the HP and probability.
335 auto [hp, prob] = probability;
336
337 color_t row_color;
338
339 // Death line is red.
340 if(hp == 0) {
341 row_color = {229, 0, 0};
342 }
343
344 // Below current hitpoints value is orange.
345 else if(hp < static_cast<int>(attacker.stats_.hp)) {
346 // Stone is grey.
347 if(defender.stats_.petrifies) {
348 row_color = {154, 154, 154};
349 } else {
350 row_color = {244, 201, 0};
351 }
352 }
353
354 // Current hitpoints value and above is green.
355 else {
356 row_color = {8, 202, 0};
357 }
358
359 shape["text"] = hp;
360 shape["x"] = 4;
361 shape["y"] = 2 + (fs + 2) * i;
362 shape["w"] = "(text_width)";
363 shape["h"] = "(text_height)";
364 shape["font_size"] = 12;
365 shape["color"] = "255, 255, 255, 255";
366 shape["text_alignment"] = "(text_alignment)";
367
368 cfg.add_child("text", shape);
369
370 shape.clear();
371 shape["text"] = get_probability_string(prob);
372 shape["x"] = graph_width - percent_sep + 2;
373 shape["y"] = 2 + (fs + 2) * i;
374 shape["w"] = "(text_width)";
375 shape["h"] = "(text_height)";
376 shape["font_size"] = 12;
377 shape["color"] = "255, 255, 255, 255";
378 shape["text_alignment"] = "(text_alignment)";
379
380 cfg.add_child("text", shape);
381
382 const int bar_len = std::max(static_cast<int>((prob * (bar_space - 4)) + 0.5), 2);
383
384 const rect bar_rect_1 {
385 hp_sep + 4,
386 6 + (fs + 2) * i,
387 bar_len,
388 8
389 };
390
391 shape.clear();
392 shape["x"] = bar_rect_1.x;
393 shape["y"] = bar_rect_1.y;
394 shape["w"] = bar_rect_1.w;
395 shape["h"] = bar_rect_1.h;
396 shape["fill_color"] = row_color.to_rgba_string();
397
398 cfg.add_child("rectangle", shape);
399
400 ++i;
401 }
402
403 hp_graph.append_drawing_data(cfg);
404}
405
407{
408 hp_probability_vector res, temp_vec;
409
410 // First, extract any relevant probability values
411 for(int i = 0; i < static_cast<int>(hp_dist.size()); ++i) {
412 const double prob = hp_dist[i];
413
414 // We keep only values above 0.1%.
415 if(prob > 0.001) {
416 temp_vec.emplace_back(i, prob);
417 }
418 }
419
420 // Then sort by descending probability.
421 std::sort(temp_vec.begin(), temp_vec.end(), [](const auto& pair1, const auto& pair2) {
422 return pair1.second > pair2.second;
423 });
424
425 // Take only the highest probability values.;
426 std::copy_n(temp_vec.begin(), std::min<int>(graph_max_rows, temp_vec.size()), std::back_inserter(res));
427
428 // Then, we sort the hitpoint values in descending order.
429 std::sort(res.begin(), res.end(), [](const auto& pair1, const auto& pair2) {
430 return pair1.first > pair2.first;
431 });
432
433 return res;
434}
435
436} // namespace dialogs
int combat_modifier(const unit_map &units, const gamemap &map, const map_location &loc, unit_alignments::type alignment, bool is_fearless)
Returns the amount that a unit's damage should be multiplied by due to the current time of day.
Definition attack.cpp:1518
Computes the statistics of a battle between an attacker and a defender unit.
Definition attack.hpp:183
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
config & add_child(std::string_view key)
Definition config.cpp:436
void clear()
Definition config.cpp:802
std::ostringstream wrapper.
Definition formatter.hpp:40
A simple canvas which can be drawn upon.
Definition canvas.hpp:45
void set_variable(const std::string &key, wfl::variant &&value)
Definition canvas.hpp:165
static const unsigned int graph_width
attack_predictions(battle_context &bc, unit_const_ptr attacker, unit_const_ptr defender)
void draw_hp_graph(drawing &hp_graph, const combatant_data &attacker, const combatant_data &defender)
static const unsigned int graph_height
void set_data(const combatant_data &attacker, const combatant_data &defender)
static const unsigned int graph_max_rows
virtual void pre_show() override
Actions to be taken before showing the window.
hp_probability_vector get_hitpoint_probabilities(const std::vector< double > &hp_dist) const
Abstract base class for all modal dialogs.
void append_drawing_data(const ::config &cfg)
Definition drawing.hpp:44
canvas & get_drawing_canvas()
Definition drawing.hpp:34
void set_tooltip(const t_string &tooltip)
virtual void set_label(const t_string &text)
void set_visible(const visibility visible)
Definition widget.cpp:479
@ invisible
The user set the widget invisible, that means:
show_mode mode() const
Returns the dialog mode for this window.
Definition window.hpp:669
static specials_context_t make(specials_combatant &&self, specials_combatant &&other, bool attacking)
const_iterator end() const
const_iterator begin() const
This class represents a single unit of a specific type.
Definition unit.hpp:39
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
static std::string _(const char *str)
Definition gettext.hpp:100
unit_alignments::type alignment() const
The alignment of this unit.
Definition unit.hpp:397
const map_location & get_location() const
The current map location this unit is at.
Definition unit.hpp:1327
bool is_fearless() const
Gets whether this unit is fearless - ie, unaffected by time of day.
Definition unit.hpp:1186
std::string id
Text to match against addon_info.tags()
Definition manager.cpp:199
auto string_table
Definition language.hpp:81
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
const std::string unicode_multiplication_sign
Definition constants.cpp:46
const int SIZE_SMALL
Definition constants.cpp:24
const color_t good_dmg_color
const color_t weapon_color
const std::string weapon_numbers_sep
Definition constants.cpp:49
const color_t bad_dmg_color
color_t red_to_green(double val, bool for_text)
Return a color corresponding to the value val red for val=0.0 to green for val=100....
static std::string get_probability_string(const double prob)
std::vector< std::pair< int, double > > hp_probability_vector
std::string italic(Args &&... data)
Applies italic Pango markup to the input.
Definition markup.hpp:176
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition markup.hpp:110
game_board * gameboard
Definition resources.cpp:20
game_classification * classification
Definition resources.cpp:34
STL namespace.
std::string signed_percent(int val)
Convert into a percentage (using the Unicode "−" and +0% convention.
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
std::shared_ptr< const attack_type > const_attack_ptr
Definition ptr.hpp:34
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
unsigned int num_blows
Effective number of blows, takes swarm into account.
Definition attack.hpp:99
bool petrifies
Attack petrifies opponent when it hits.
Definition attack.hpp:80
unsigned int hp
Hitpoints of the unit at the beginning of the battle.
Definition attack.hpp:92
bool is_attacker
True if the unit is the attacker.
Definition attack.hpp:75
const_attack_ptr weapon
The weapon used by the unit to attack the opponent, or nullptr if there is none.
Definition attack.hpp:73
bool is_slowed
True if the unit is slowed at the beginning of the battle.
Definition attack.hpp:77
int damage
Effective damage of the weapon (all factors accounted for).
Definition attack.hpp:95
unsigned int chance_to_hit
Effective chance to hit as a percentage (all factors accounted for).
Definition attack.hpp:94
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
std::string to_rgba_string() const
Returns the stored color as an "R,G,B,A" string.
Definition color.cpp:105
std::vector< double > hp_dist
Resulting probability distribution (might be not as large as max_hp)
double untouched
Resulting chance we were not hit by this opponent (important if it poisons)
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49
#define i
#define e