-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
reports.cpp
1661 lines (1445 loc) · 52.2 KB
/
reports.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project https://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.
*/
#include "actions/attack.hpp"
#include "attack_prediction.hpp"
#include "desktop/battery_info.hpp"
#include "font/pango/escape.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "preferences/game.hpp"
#include "gettext.hpp"
#include "language.hpp"
#include "map/map.hpp"
#include "mouse_events.hpp"
#include "reports.hpp"
#include "color.hpp"
#include "team.hpp"
#include "terrain/movement.hpp"
#include "tod_manager.hpp"
#include "units/unit.hpp"
#include "units/helper.hpp"
#include "whiteboard/manager.hpp"
#include <cassert>
#include <ctime>
#include <iomanip>
#include <boost/dynamic_bitset.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include "utils/io.hpp"
static void add_text(config &report, const std::string &text,
const std::string &tooltip, const std::string &help = "")
{
config &element = report.add_child("element");
element["text"] = text;
if (!tooltip.empty()) element["tooltip"] = tooltip;
if (!help.empty()) element["help"] = help;
}
static void add_image(config &report, const std::string &image,
const std::string &tooltip, const std::string &help = "")
{
config &element = report.add_child("element");
element["image"] = image;
if (!tooltip.empty()) element["tooltip"] = tooltip;
if (!help.empty()) element["help"] = help;
}
static config text_report(const std::string &text,
const std::string &tooltip = "", const std::string &help = "")
{
config r;
add_text(r, text, tooltip, help);
return r;
}
static config image_report(const std::string &image,
const std::string &tooltip = "", const std::string &help = "")
{
config r;
add_image(r, image, tooltip, help);
return r;
}
using font::span_color;
static void add_status(config &r,
char const *path, char const *desc1, char const *desc2)
{
std::ostringstream s;
s << translation::gettext(desc1) << translation::gettext(desc2);
add_image(r, path, s.str());
}
static std::string flush(std::ostringstream &s)
{
std::string r(s.str());
s.str(std::string());
return r;
}
static const time_of_day get_visible_time_of_day_at(reports::context & rc, const map_location & hex)
{
const team &viewing_team = rc.teams()[rc.screen().viewing_team()];
if (viewing_team.shrouded(hex)) {
// Don't show time on shrouded tiles.
return rc.tod().get_time_of_day();
} else if (viewing_team.fogged(hex)) {
// Don't show illuminated time on fogged tiles.
return rc.tod().get_time_of_day(hex);
} else {
return rc.tod().get_illuminated_time_of_day(rc.units(), rc.map(), hex);
}
}
typedef std::map<std::string, reports::generator_function> static_report_generators;
static static_report_generators static_generators;
struct report_generator_helper
{
report_generator_helper(const char *name, reports::generator_function g)
{
static_generators.insert(static_report_generators::value_type(name, g));
}
};
#define REPORT_GENERATOR(n, cn) \
static config report_##n(reports::context & cn); \
static report_generator_helper reg_gen_##n(#n, &report_##n); \
static config report_##n(reports::context & cn)
static char const *naps = "</span>";
static const unit *get_visible_unit(reports::context & rc)
{
return rc.dc().get_visible_unit(rc.screen().displayed_unit_hex(),
rc.teams()[rc.screen().viewing_team()],
rc.screen().show_everything());
}
static const unit *get_selected_unit(reports::context & rc)
{
return rc.dc().get_visible_unit(rc.screen().selected_hex(),
rc.teams()[rc.screen().viewing_team()],
rc.screen().show_everything());
}
static config gray_inactive(reports::context & rc, const std::string &str, const std::string& tooltip = "")
{
if ( rc.screen().viewing_side() == rc.screen().playing_side() )
return text_report(str, tooltip);
return text_report(span_color(font::GRAY_COLOR) + str + naps, tooltip);
}
static config unit_name(const unit *u)
{
if (!u) {
return config();
}
/*
* The name needs to be escaped, it might be set by the user and using
* markup. Also names often contain a forbidden single quote.
*/
const std::string& name = font::escape_text(u->name());
std::ostringstream str, tooltip;
str << "<b>" << name << "</b>";
tooltip << _("Name: ") << "<b>" << name << "</b>";
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_name, rc)
{
const unit *u = get_visible_unit(rc);
return unit_name(u);
}
REPORT_GENERATOR(selected_unit_name, rc)
{
const unit *u = get_selected_unit(rc);
return unit_name(u);
}
static config unit_type(const unit* u)
{
if (!u) return config();
std::string has_variations_prefix = (u->type().show_variations_in_help() ? ".." : "");
std::ostringstream str, tooltip;
str << u->type_name();
tooltip << _("Type: ") << "<b>" << u->type_name() << "</b>\n"
<< u->unit_description();
return text_report(str.str(), tooltip.str(), has_variations_prefix + "unit_" + u->type_id());
}
REPORT_GENERATOR(unit_type, rc)
{
const unit *u = get_visible_unit(rc);
return unit_type(u);
}
REPORT_GENERATOR(selected_unit_type, rc)
{
const unit *u = get_selected_unit(rc);
return unit_type(u);
}
static config unit_race(const unit* u)
{
if (!u) return config();
std::ostringstream str, tooltip;
str << u->race()->name(u->gender());
tooltip << _("Race: ") << "<b>" << u->race()->name(u->gender()) << "</b>";
return text_report(str.str(), tooltip.str(), "..race_" + u->race()->id());
}
REPORT_GENERATOR(unit_race, rc)
{
const unit *u = get_visible_unit(rc);
return unit_race(u);
}
REPORT_GENERATOR(selected_unit_race, rc)
{
const unit *u = get_selected_unit(rc);
return unit_race(u);
}
static config unit_side(reports::context & rc, const unit* u)
{
if (!u) return config();
config report;
const team &u_team = rc.dc().get_team(u->side());
std::string flag_icon = u_team.flag_icon();
std::string old_rgb = game_config::flag_rgb;
std::string new_rgb = u_team.color();
std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")";
if (flag_icon.empty())
flag_icon = game_config::images::flag_icon;
std::stringstream text;
text << " " << u->side();
std::ostringstream tooltip;
if (!u_team.side_name().empty()) {
tooltip << _("Side:") << " <b>" << u_team.side_name() << "</b>";
}
add_image(report, flag_icon + mods, tooltip.str(), "");
add_text(report, text.str(), tooltip.str(), "");
return report;
}
REPORT_GENERATOR(unit_side, rc)
{
const unit *u = get_visible_unit(rc);
return unit_side(rc,u);
}
REPORT_GENERATOR(selected_unit_side, rc)
{
const unit *u = get_selected_unit(rc);
return unit_side(rc, u);
}
static config unit_level(const unit* u)
{
if (!u) return config();
std::ostringstream str, tooltip;
str << u->level();
tooltip << _("Level: ") << "<b>" << u->level() << "</b>\n";
const std::vector<std::string> &adv_to_types = u->advances_to_translated();
const std::vector<config> &adv_to_mods = u->get_modification_advances();
const bool has_advancements = !adv_to_types.empty() || !adv_to_mods.empty();
if(has_advancements) {
tooltip << _("Advancements:") << "\n<b>\t";
if(!adv_to_types.empty())
tooltip << utils::join(adv_to_types, "\n\t");
if(!adv_to_mods.empty()) {
if(!adv_to_types.empty())
tooltip << "\n\t";
std::vector<std::string> descriptions;
for(const config& adv : adv_to_mods)
descriptions.push_back(adv["description"].str());
tooltip << utils::join(descriptions, "\n\t");
}
tooltip << "</b>";
} else {
tooltip << _("No advancement");
}
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_level, rc)
{
const unit *u = get_visible_unit(rc);
return unit_level(u);
}
REPORT_GENERATOR(selected_unit_level, rc)
{
const unit *u = get_selected_unit(rc);
return unit_level(u);
}
REPORT_GENERATOR(unit_amla, rc)
{
const unit *u = get_visible_unit(rc);
if (!u) return config();
config res;
typedef std::pair<std::string, std::string> pair_string;
for (const pair_string &ps : u->amla_icons()) {
add_image(res, ps.first, ps.second);
}
return res;
}
static config unit_traits(const unit* u)
{
if (!u) return config();
config res;
const std::vector<t_string> &traits = u->trait_names();
const std::vector<t_string> &descriptions = u->trait_descriptions();
const std::vector<std::string> &trait_ids = u->get_traits_list();
unsigned nb = traits.size();
for (unsigned i = 0; i < nb; ++i)
{
std::ostringstream str, tooltip;
str << traits[i];
if (i != nb - 1 ) str << ", ";
tooltip << _("Trait: ") << "<b>" << traits[i] << "</b>\n"
<< descriptions[i];
add_text(res, str.str(), tooltip.str(), "traits_" + trait_ids[i]);
}
return res;
}
REPORT_GENERATOR(unit_traits, rc)
{
const unit *u = get_visible_unit(rc);
return unit_traits(u);
}
REPORT_GENERATOR(selected_unit_traits, rc)
{
const unit *u = get_selected_unit(rc);
return unit_traits(u);
}
static config unit_status(reports::context & rc, const unit* u)
{
if (!u) return config();
config res;
map_location displayed_unit_hex = rc.screen().displayed_unit_hex();
if (rc.map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex, rc.dc())) {
add_status(res, "misc/invisible.png", N_("invisible: "),
N_("This unit is invisible. It cannot be seen or attacked by enemy units."));
}
if (u->get_state(unit::STATE_SLOWED)) {
add_status(res, "misc/slowed.png", N_("slowed: "),
N_("This unit has been slowed. It will only deal half its normal damage when attacking and its movement cost is doubled."));
}
if (u->get_state(unit::STATE_POISONED)) {
add_status(res, "misc/poisoned.png", N_("poisoned: "),
N_("This unit is poisoned. It will lose 8 HP every turn until it can seek a cure to the poison in a village or from a friendly unit with the ‘cures’ ability.\n\nUnits cannot be killed by poison alone. The poison will not reduce it below 1 HP."));
}
if (u->get_state(unit::STATE_PETRIFIED)) {
add_status(res, "misc/petrified.png", N_("petrified: "),
N_("This unit has been petrified. It may not move or attack."));
}
return res;
}
REPORT_GENERATOR(unit_status,rc)
{
const unit *u = get_visible_unit(rc);
return unit_status(rc,u);
}
REPORT_GENERATOR(selected_unit_status, rc)
{
const unit *u = get_selected_unit(rc);
return unit_status(rc, u);
}
static config unit_alignment(reports::context & rc, const unit* u, const map_location& hex)
{
if (!u) return config();
std::ostringstream str, tooltip;
const std::string align = unit_type::alignment_description(u->alignment(), u->gender());
const std::string align_id = u->alignment().to_string();
const time_of_day effective_tod = get_visible_time_of_day_at(rc, hex);
int cm = combat_modifier(effective_tod, u->alignment(), u->is_fearless());
color_t color = font::weapon_color;
if (cm != 0)
color = (cm > 0) ? font::good_dmg_color : font::bad_dmg_color;
str << align << " (" << span_color(color) << utils::signed_percent(cm)
<< naps << ")";
tooltip << _("Alignment: ") << "<b>" << align << "</b>\n"
<< string_table[align_id + "_description"];
return text_report(str.str(), tooltip.str(), "time_of_day");
}
REPORT_GENERATOR(unit_alignment, rc)
{
const unit *u = get_visible_unit(rc);
const map_location& mouseover_hex = rc.screen().mouseover_hex();
const map_location& displayed_unit_hex = rc.screen().displayed_unit_hex();
const map_location& hex = mouseover_hex.valid() ? mouseover_hex : displayed_unit_hex;
return unit_alignment(rc, u, hex);
}
REPORT_GENERATOR(selected_unit_alignment, rc)
{
const unit *u = get_selected_unit(rc);
const map_location& attack_indicator_src = game_display::get_singleton()->get_attack_indicator_src();
const map_location& hex_to_show_alignment_at =
attack_indicator_src.valid() ? attack_indicator_src : u->get_location();
return unit_alignment(rc, u, hex_to_show_alignment_at);
}
static config unit_abilities(const unit* u, const map_location& loc)
{
if (!u) return config();
config res;
boost::dynamic_bitset<> active;
const std::vector<std::tuple<std::string, t_string,t_string,t_string>> &abilities = u->ability_tooltips(active, loc);
const std::size_t abilities_size = abilities.size();
for ( std::size_t i = 0; i != abilities_size; ++i )
{
// Aliases for readability:
const std::string& id = std::get<0>(abilities[i]);
const std::string& base_name = std::get<1>(abilities[i]).base_str();
const t_string& display_name = std::get<2>(abilities[i]);
const t_string& description = std::get<3>(abilities[i]);
std::ostringstream str, tooltip;
if ( active[i] )
str << display_name;
else
str << span_color(font::inactive_ability_color) << display_name << naps;
if ( i + 1 != abilities_size )
str << ", ";
tooltip << _("Ability: ") << "<b>" << display_name << "</b>";
if ( !active[i] )
tooltip << "<i>" << _(" (inactive)") << "</i>";
tooltip << '\n' << description;
add_text(res, str.str(), tooltip.str(), "ability_" + id + base_name);
}
return res;
}
REPORT_GENERATOR(unit_abilities, rc)
{
const unit *u = get_visible_unit(rc);
const map_location& mouseover_hex = rc.screen().mouseover_hex();
return unit_abilities(u, mouseover_hex);
}
REPORT_GENERATOR(selected_unit_abilities, rc)
{
const unit *u = get_selected_unit(rc);
const map_location& mouseover_hex = rc.screen().mouseover_hex();
const unit *visible_unit = get_visible_unit(rc);
if(visible_unit && u && visible_unit->id() != u->id() && mouseover_hex.valid())
return unit_abilities(u, mouseover_hex);
else
return unit_abilities(u, u->get_location());
}
static config unit_hp(reports::context& rc, const unit* u)
{
if (!u) return config();
std::ostringstream str, tooltip;
str << span_color(u->hp_color()) << u->hitpoints()
<< '/' << u->max_hitpoints() << naps;
std::set<std::string> resistances_table;
bool att_def_diff = false;
map_location displayed_unit_hex = rc.screen().displayed_unit_hex();
for (const utils::string_map::value_type &resist : u->get_base_resistances())
{
std::ostringstream line;
line << translation::gettext(resist.first.c_str()) << ": ";
// Some units have different resistances when attacking or defending.
int res_att = 100 - u->resistance_against(resist.first, true, displayed_unit_hex);
int res_def = 100 - u->resistance_against(resist.first, false, displayed_unit_hex);
const std::string def_color = unit_helper::resistance_color(res_def);
if (res_att == res_def) {
line << "<span foreground=\"" << def_color << "\">" << utils::signed_percent(res_def)
<< naps << '\n';
} else {
const std::string att_color = unit_helper::resistance_color(res_att);
line << "<span foreground=\"" << att_color << "\">" << utils::signed_percent(res_att)
<< naps << "/"
<< "<span foreground=\"" << def_color << "\">" << utils::signed_percent(res_def)
<< naps << '\n';
att_def_diff = true;
}
resistances_table.insert(line.str());
}
tooltip << _("Resistances: ");
if (att_def_diff)
tooltip << _("(Att / Def)");
tooltip << '\n';
for (const std::string &line : resistances_table) {
tooltip << line;
}
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_hp, rc)
{
const unit *u = get_visible_unit(rc);
return unit_hp(rc, u);
}
REPORT_GENERATOR(selected_unit_hp, rc)
{
const unit *u = get_selected_unit(rc);
return unit_hp(rc, u);
}
static config unit_xp(const unit* u)
{
if (!u) return config();
std::ostringstream str, tooltip;
str << span_color(u->xp_color());
if(u->can_advance()) {
str << u->experience() << '/' << u->max_experience();
} else {
str << font::unicode_en_dash;
}
str << naps;
int exp_mod = unit_experience_accelerator::get_acceleration();
tooltip << _("Experience Modifier: ") << exp_mod << '%';
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_xp, rc)
{
const unit *u = get_visible_unit(rc);
return unit_xp(u);
}
REPORT_GENERATOR(selected_unit_xp, rc)
{
const unit *u = get_selected_unit(rc);
return unit_xp(u);
}
static config unit_advancement_options(const unit* u)
{
if (!u) return config();
config res;
typedef std::pair<std::string, std::string> pair_string;
for (const pair_string &ps : u->advancement_icons()) {
add_image(res, ps.first, ps.second);
}
return res;
}
REPORT_GENERATOR(unit_advancement_options, rc)
{
const unit *u = get_visible_unit(rc);
return unit_advancement_options(u);
}
REPORT_GENERATOR(selected_unit_advancement_options, rc)
{
const unit *u = get_selected_unit(rc);
return unit_advancement_options(u);
}
static config unit_defense(reports::context & rc, const unit* u, const map_location& displayed_unit_hex)
{
if(!u) {
return config();
}
std::ostringstream str, tooltip;
const gamemap &map = rc.map();
if(!rc.map().on_board(displayed_unit_hex)) {
return config();
}
const t_translation::terrain_code &terrain = map[displayed_unit_hex];
int def = 100 - u->defense_modifier(terrain);
color_t color = game_config::red_to_green(def);
str << span_color(color) << def << '%' << naps;
tooltip << _("Terrain: ") << "<b>" << map.get_terrain_info(terrain).description() << "</b>\n";
const t_translation::ter_list &underlyings = map.underlying_def_terrain(terrain);
if (underlyings.size() != 1 || underlyings.front() != terrain)
{
bool revert = false;
for (const t_translation::terrain_code &t : underlyings)
{
if (t == t_translation::MINUS) {
revert = true;
} else if (t == t_translation::PLUS) {
revert = false;
} else {
int t_def = 100 - u->defense_modifier(t);
color_t t_color = game_config::red_to_green(t_def);
tooltip << '\t' << map.get_terrain_info(t).description() << ": "
<< span_color(t_color) << t_def << '%' << naps
<< (revert ? _("maximum^max.") : _("minimum^min.")) << '\n';
}
}
}
tooltip << "<b>" << _("Defense: ") << span_color(color) << def << '%' << naps << "</b>";
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_defense,rc)
{
const unit *u = get_visible_unit(rc);
const map_location& displayed_unit_hex = rc.screen().displayed_unit_hex();
return unit_defense(rc, u, displayed_unit_hex);
}
REPORT_GENERATOR(selected_unit_defense, rc)
{
const unit *u = get_selected_unit(rc);
const map_location& selected_hex = rc.screen().selected_hex();
return unit_defense(rc, u, selected_hex);
}
static config unit_vision(const unit* u)
{
if (!u) return config();
// TODO
std::ostringstream str, tooltip;
if (u->vision() != u->total_movement()) {
str << _("vision:") << ' ' << u->vision();
tooltip << _("vision:") << ' ' << u->vision() << '\n';
}
if (u->jamming() != 0) {
if (static_cast<std::streamoff>(str.tellp()) == 0)
str << _("jamming:") << ' ' << u->jamming();
tooltip << _("jamming:") << ' ' << u->jamming() << '\n';
}
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_vision, rc)
{
const unit* u = get_visible_unit(rc);
return unit_vision(u);
}
REPORT_GENERATOR(selected_unit_vision, rc)
{
const unit* u = get_selected_unit(rc);
return unit_vision(u);
}
static config unit_moves(reports::context & rc, const unit* u)
{
if (!u) return config();
std::ostringstream str, tooltip;
double movement_frac = 1.0;
std::set<terrain_movement> terrain_moves;
if (u->side() == rc.screen().playing_side()) {
movement_frac = static_cast<double>(u->movement_left()) / std::max<int>(1, u->total_movement());
if (movement_frac > 1.0)
movement_frac = 1.0;
}
tooltip << _("Movement Costs:") << "\n";
for (t_translation::terrain_code terrain : preferences::encountered_terrains()) {
if (terrain == t_translation::FOGGED || terrain == t_translation::VOID_TERRAIN || t_translation::terrain_matches(terrain, t_translation::ALL_OFF_MAP))
continue;
const terrain_type& info = rc.map().get_terrain_info(terrain);
if (info.union_type().size() == 1 && info.union_type()[0] == info.number() && info.is_nonnull()) {
terrain_moves.emplace(info.name(), u->movement_cost(terrain));
}
}
for (const terrain_movement& tm : terrain_moves) {
tooltip << tm.name << ": ";
std::string color;
//movement - range: 1 .. 5, movetype::UNREACHABLE=impassable
const bool cannot_move = tm.moves > u->total_movement();
if (cannot_move) // cannot move in this terrain
color = "red";
else if (tm.moves > 1)
color = "yellow";
else
color = "white";
tooltip << "<span foreground=\"" << color << "\">";
// A 5 MP margin; if the movement costs go above
// the unit's max moves + 5, we replace it with dashes.
if (cannot_move && (tm.moves > u->total_movement() + 5)) {
tooltip << font::unicode_figure_dash;
} else {
tooltip << tm.moves;
}
tooltip << naps << '\n';
}
int grey = 128 + static_cast<int>((255 - 128) * movement_frac);
color_t c = color_t(grey, grey, grey);
str << span_color(c) << u->movement_left() << '/' << u->total_movement() << naps;
return text_report(str.str(), tooltip.str());
}
REPORT_GENERATOR(unit_moves, rc)
{
const unit *u = get_visible_unit(rc);
return unit_moves(rc, u);
}
REPORT_GENERATOR(selected_unit_moves, rc)
{
const unit *u = get_selected_unit(rc);
return unit_moves(rc, u);
}
static inline const color_t attack_info_percent_color(int resistance)
{
// Compare unit_helper::resistance_color()
if (resistance < 0) return font::BAD_COLOR;
if (resistance > 0) return font::GOOD_COLOR;
return font::YELLOW_COLOR;
}
static int attack_info(reports::context & rc, const attack_type &at, config &res, const unit &u, const map_location &hex)
{
std::ostringstream str, tooltip;
int damage = 0;
{
auto ctx = at.specials_context(unit_const_ptr(&u), hex, u.side() == rc.screen().playing_side());
int base_damage = at.damage();
int specials_damage = at.modified_damage(false);
int damage_multiplier = 100;
int tod_bonus = combat_modifier(get_visible_time_of_day_at(rc, hex), u.alignment(), u.is_fearless());
damage_multiplier += tod_bonus;
int leader_bonus = under_leadership(u, hex);
if (leader_bonus != 0)
damage_multiplier += leader_bonus;
bool slowed = u.get_state(unit::STATE_SLOWED);
int damage_divisor = slowed ? 20000 : 10000;
// Assume no specific resistance (i.e. multiply by 100).
damage = round_damage(specials_damage, damage_multiplier * 100, damage_divisor);
// Hit points are used to calculate swarm, so they need to be bounded.
unsigned max_hp = u.max_hitpoints();
unsigned cur_hp = std::min<unsigned>(std::max(0, u.hitpoints()), max_hp);
unsigned base_attacks = at.num_attacks();
unsigned min_attacks, max_attacks;
at.modified_attacks(false, min_attacks, max_attacks);
unsigned num_attacks = swarm_blows(min_attacks, max_attacks, cur_hp, max_hp);
color_t dmg_color = font::weapon_color;
if ( damage > specials_damage )
dmg_color = font::good_dmg_color;
else if ( damage < specials_damage )
dmg_color = font::bad_dmg_color;
str << span_color(dmg_color) << " " << damage << naps << span_color(font::weapon_color)
<< font::weapon_numbers_sep << num_attacks << ' ' << at.name()
<< "</span>\n";
tooltip << _("Weapon: ") << "<b>" << at.name() << "</b>\n"
<< _("Damage: ") << "<b>" << damage << "</b>\n";
if ( tod_bonus || leader_bonus || slowed || specials_damage != base_damage )
{
tooltip << '\t' << _("Base damage: ") << base_damage << '\n';
if ( specials_damage != base_damage ) {
tooltip << '\t' << _("With specials: ") << specials_damage << '\n';
}
if (tod_bonus) {
tooltip << '\t' << _("Time of day: ")
<< utils::signed_percent(tod_bonus) << '\n';
}
if (leader_bonus) {
tooltip << '\t' << _("Leadership: ")
<< utils::signed_percent(leader_bonus) << '\n';
}
if (slowed) {
tooltip << '\t' << _("Slowed: ") << "/ 2" << '\n';
}
}
tooltip << _("Attacks: ") << "<b>" << num_attacks << "</b>\n";
if ( max_attacks != min_attacks && cur_hp != max_hp ) {
if ( max_attacks < min_attacks ) {
// "Reverse swarm"
tooltip << '\t' << _("Max swarm bonus: ") << (min_attacks-max_attacks) << '\n';
tooltip << '\t' << _("Swarm: ") << "* "<< (100 - cur_hp*100/max_hp) << "%\n";
tooltip << '\t' << _("Base attacks: ") << '+' << base_attacks << '\n';
// The specials line will not necessarily match up with how the
// specials are calculated, but for an unusual case, simple brevity
// trumps complexities.
if ( max_attacks != base_attacks ) {
int attack_diff = static_cast<int>(max_attacks) - static_cast<int>(base_attacks);
tooltip << '\t' << _("Specials: ") << utils::signed_value(attack_diff) << '\n';
}
}
else {
// Regular swarm
tooltip << '\t' << _("Base attacks: ") << base_attacks << '\n';
if ( max_attacks != base_attacks ) {
tooltip << '\t' << _("With specials: ") << max_attacks << '\n';
}
if ( min_attacks != 0 ) {
tooltip << '\t' << _("Subject to swarm: ") << (max_attacks-min_attacks) << '\n';
}
tooltip << '\t' << _("Swarm: ") << "* "<< (cur_hp*100/max_hp) << "%\n";
}
}
else if ( num_attacks != base_attacks ) {
tooltip << '\t' << _("Base attacks: ") << base_attacks << '\n';
tooltip << '\t' << _("With specials: ") << num_attacks << '\n';
}
add_text(res, flush(str), flush(tooltip));
std::string range = string_table["range_" + at.range()];
std::string lang_type = string_table["type_" + at.type()];
str << span_color(font::weapon_details_color) << " " << " "
<< range << font::weapon_details_sep
<< lang_type << "</span>\n";
tooltip << _("Weapon range: ") << "<b>" << range << "</b>\n"
<< _("Damage type: ") << "<b>" << lang_type << "</b>\n"
<< _("Damage versus: ") << '\n';
// Show this weapon damage and resistance against all the different units.
// We want weak resistances (= good damage) first.
std::map<int, std::set<std::string>, std::greater<int>> resistances;
std::set<std::string> seen_types;
const team &unit_team = rc.dc().get_team(u.side());
const team &viewing_team = rc.teams()[rc.screen().viewing_team()];
for (const unit &enemy : rc.units())
{
if (enemy.incapacitated()) //we can't attack statues so don't display them in this tooltip
continue;
if (!unit_team.is_enemy(enemy.side()))
continue;
const map_location &loc = enemy.get_location();
if (viewing_team.fogged(loc) ||
(viewing_team.is_enemy(enemy.side()) && enemy.invisible(loc, rc.dc())))
continue;
bool new_type = seen_types.insert(enemy.type_id()).second;
if (new_type) {
int resistance = enemy.resistance_against(at, false, loc);
resistances[resistance].insert(enemy.type_name());
}
}
typedef std::pair<int, std::set<std::string>> resist_units;
for (const resist_units &resist : resistances) {
int damage_with_resistance = round_damage(specials_damage, damage_multiplier * resist.first, damage_divisor);
tooltip << "<b>" << damage_with_resistance << "</b> "
<< "<span color='" << attack_info_percent_color(resist.first-100).to_hex_string() << "'>"
<< "<i>(" << utils::signed_percent(resist.first-100) << ")</i>"
<< naps
<< " : \t" // spaces to align the tab to a multiple of 8
<< utils::join(resist.second, " " + font::unicode_bullet + " ") << '\n';
}
add_text(res, flush(str), flush(tooltip));
const std::string &accuracy_parry = at.accuracy_parry_description();
if (!accuracy_parry.empty())
{
str << span_color(font::weapon_details_color)
<< " " << accuracy_parry << "</span>\n";
int accuracy = at.accuracy();
if (accuracy) {
tooltip << _("Accuracy:") << "<b>"
<< utils::signed_percent(accuracy) << "</b>\n";
}
int parry = at.parry();
if (parry) {
tooltip << _("Parry:") << "<b>"
<< utils::signed_percent(parry) << "</b>\n";
}
add_text(res, flush(str), flush(tooltip));
}
}
{
auto ctx = at.specials_context_for_listing();
boost::dynamic_bitset<> active;
const std::vector<std::pair<t_string, t_string>> &specials = at.special_tooltips(&active);
const size_t specials_size = specials.size();
for ( size_t i = 0; i != specials_size; ++i )
{
// Aliases for readability:
const t_string &name = specials[i].first;
const t_string &description = specials[i].second;
const color_t &details_color = active[i] ? font::weapon_details_color :
font::inactive_details_color;
str << span_color(details_color) << " " << " " << name << naps << '\n';
std::string help_page = "weaponspecial_" + name.base_str();
tooltip << _("Weapon special: ") << "<b>" << name << "</b>";
if ( !active[i] )
tooltip << "<i>" << _(" (inactive)") << "</i>";
tooltip << '\n' << description;
add_text(res, flush(str), flush(tooltip), help_page);
}
}
return damage;
}
// Conversion routine for both unscathed and damage change percentage.
static std::string format_prob(double prob)
{
if(prob > 0.9995) {
return "100%";
} else if(prob < 0.0005) {
return "0%";
}
std::ostringstream res;
res << std::setprecision(prob < 0.1 ? 2 : 3) << 100.0 * prob << "%";
return res.str();
}
static std::string format_hp(unsigned hp)
{
std::ostringstream res;
res << ' ' << std::setw(3) << hp;
return res.str();
}
static config unit_weapons(reports::context & rc, const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker)
{
if (!attacker || !defender) return config();
const unit* u = show_attacker ? attacker : defender;
const map_location unit_loc = show_attacker ? attacker_pos : defender->get_location();
std::ostringstream str, tooltip;
config res;
std::vector<battle_context> weapons;
for (unsigned int i = 0; i < attacker->attacks().size(); i++) {
// skip weapons with attack_weight=0
if (attacker->attacks()[i].attack_weight() > 0) {
battle_context weapon(rc.units(), attacker_pos, defender->get_location(), i, -1, 0.0, nullptr, attacker);
weapons.emplace_back(std::move(weapon));
}
}
for (const battle_context& weapon : weapons) {
// Predict the battle outcome.
combatant attacker_combatant(weapon.get_attacker_stats());
combatant defender_combatant(weapon.get_defender_stats());
attacker_combatant.fight(defender_combatant);
const battle_context_unit_stats& context_unit_stats =
show_attacker ? weapon.get_attacker_stats() : weapon.get_defender_stats();
int total_damage = 0;
int base_damage = 0;
int num_blows = 0;
int chance_to_hit = 0;
t_string weapon_name = _("weapon^None");
color_t dmg_color = font::weapon_color;
if (context_unit_stats.weapon) {
base_damage = attack_info(rc, *context_unit_stats.weapon, res, *u, unit_loc);
total_damage = context_unit_stats.damage;
num_blows = context_unit_stats.num_blows;
chance_to_hit = context_unit_stats.chance_to_hit;
weapon_name = context_unit_stats.weapon->name();
if ( total_damage > base_damage )
dmg_color = font::good_dmg_color;
else if ( total_damage < base_damage )
dmg_color = font::bad_dmg_color;
} else {
str << span_color(font::weapon_color) << weapon_name << naps << "\n";
tooltip << _("Weapon: ") << "<b>" << weapon_name << "</b>\n"
<< _("Damage: ") << "<b>" << "0" << "</b>\n";
}
color_t chance_color = game_config::red_to_green(chance_to_hit);
// Total damage.
str << " " << span_color(dmg_color) << total_damage << naps << span_color(font::weapon_color)
<< font::unicode_en_dash << num_blows
<< " (" << span_color(chance_color) << chance_to_hit << "%" << naps << ")"
<< naps << "\n";
tooltip << _("Weapon: ") << "<b>" << weapon_name << "</b>\n"
<< _("Total damage") << "<b>" << total_damage << "</b>\n";
// Create the hitpoints distribution.
std::vector<std::pair<int, double>> hp_prob_vector;
// First, we sort the probabilities in ascending order.
std::vector<std::pair<double, int>> prob_hp_vector;
int i;
combatant* c = show_attacker ? &attacker_combatant : &defender_combatant;
for(i = 0; i < static_cast<int>(c->hp_dist.size()); i++) {
double prob = c->hp_dist[i];
// We keep only values above 0.1%.
if(prob > 0.001)
prob_hp_vector.emplace_back(prob, i);
}