-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
help_impl.cpp
1553 lines (1353 loc) · 47.2 KB
/
help_impl.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 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.
*/
#include "help/help_impl.hpp"
#include "about.hpp" // for get_text
#include "display.hpp" // for display
#include "display_context.hpp" // for display_context
#include "game_config.hpp" // for debug, menu_contract, etc
#include "game_config_manager.hpp" // for game_config_manager
#include "preferences/game.hpp" // for encountered_terrains, etc
#include "gettext.hpp" // for _, gettext, N_
#include "help/help_topic_generators.hpp"
#include "hotkey/hotkey_command.hpp" // for is_scope_active, etc
#include "picture.hpp" // for get_image, locator
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "utils/make_enum.hpp" // for operator<<
#include "map/map.hpp" // for gamemap
#include "font/marked-up_text.hpp" // for is_cjk_char, word_wrap_text
#include "font/standard_colors.hpp" // for NORMAL_COLOR
#include "units/race.hpp" // for unit_race, etc
#include "resources.hpp" // for tod_manager, config_manager
#include "sdl/surface.hpp" // for surface
#include "serialization/parser.hpp"
#include "serialization/string_utils.hpp" // for split, quoted_split, etc
#include "serialization/unicode_cast.hpp" // for unicode_cast
#include "serialization/utf8_exception.hpp" // for char_t, etc
#include "terrain/terrain.hpp" // for terrain_type
#include "terrain/translation.hpp" // for operator==, ter_list, etc
#include "terrain/type_data.hpp" // for terrain_type_data, etc
#include "time_of_day.hpp" // for time_of_day
#include "tod_manager.hpp" // for tod_manager
#include "tstring.hpp" // for t_string, operator<<
#include "units/types.hpp" // for unit_type, unit_type_data, etc
#include "serialization/unicode.hpp" // for iterator
#include "color.hpp"
#include "config_assign.hpp"
#include <cassert> // for assert
#include <algorithm> // for sort, find, transform, etc
#include <iostream> // for operator<<, basic_ostream, etc
#include <iterator> // for back_insert_iterator, etc
#include <map> // for map, etc
#include <set>
#include <SDL.h>
static lg::log_domain log_display("display");
#define WRN_DP LOG_STREAM(warn, log_display)
static lg::log_domain log_help("help");
#define WRN_HP LOG_STREAM(warn, log_help)
#define DBG_HP LOG_STREAM(debug, log_help)
namespace help {
const config *game_cfg = nullptr;
// The default toplevel.
help::section default_toplevel;
// All sections and topics not referenced from the default toplevel.
help::section hidden_sections;
int last_num_encountered_units = -1;
int last_num_encountered_terrains = -1;
boost::tribool last_debug_state = boost::indeterminate;
config dummy_cfg;
std::vector<std::string> empty_string_vector;
const int max_section_level = 15;
const int title_size = font::SIZE_LARGE;
const int title2_size = font::SIZE_15;
const int box_width = 2;
const int normal_font_size = font::SIZE_NORMAL;
const unsigned max_history = 100;
const std::string topic_img = "help/topic.png";
const std::string closed_section_img = "help/closed_section.png";
const std::string open_section_img = "help/open_section.png";
// The topic to open by default when opening the help dialog.
const std::string default_show_topic = "..introduction";
const std::string unknown_unit_topic = ".unknown_unit";
const std::string unit_prefix = "unit_";
const std::string terrain_prefix = "terrain_";
const std::string race_prefix = "race_";
const std::string faction_prefix = "faction_";
const std::string era_prefix = "era_";
const std::string variation_prefix = "variation_";
const std::string ability_prefix = "ability_";
bool section_is_referenced(const std::string §ion_id, const config &cfg)
{
if (const config &toplevel = cfg.child("toplevel"))
{
const std::vector<std::string> toplevel_refs
= utils::quoted_split(toplevel["sections"]);
if (std::find(toplevel_refs.begin(), toplevel_refs.end(), section_id)
!= toplevel_refs.end()) {
return true;
}
}
for (const config §ion : cfg.child_range("section"))
{
const std::vector<std::string> sections_refd
= utils::quoted_split(section["sections"]);
if (std::find(sections_refd.begin(), sections_refd.end(), section_id)
!= sections_refd.end()) {
return true;
}
}
return false;
}
bool topic_is_referenced(const std::string &topic_id, const config &cfg)
{
if (const config &toplevel = cfg.child("toplevel"))
{
const std::vector<std::string> toplevel_refs
= utils::quoted_split(toplevel["topics"]);
if (std::find(toplevel_refs.begin(), toplevel_refs.end(), topic_id)
!= toplevel_refs.end()) {
return true;
}
}
for (const config §ion : cfg.child_range("section"))
{
const std::vector<std::string> topics_refd
= utils::quoted_split(section["topics"]);
if (std::find(topics_refd.begin(), topics_refd.end(), topic_id)
!= topics_refd.end()) {
return true;
}
}
return false;
}
void parse_config_internal(const config *help_cfg, const config *section_cfg,
section &sec, int level)
{
if (level > max_section_level) {
std::cerr << "Maximum section depth has been reached. Maybe circular dependency?"
<< std::endl;
}
else if (section_cfg != nullptr) {
const std::vector<std::string> sections = utils::quoted_split((*section_cfg)["sections"]);
sec.level = level;
std::string id = level == 0 ? "toplevel" : (*section_cfg)["id"].str();
if (level != 0) {
if (!is_valid_id(id)) {
std::stringstream ss;
ss << "Invalid ID, used for internal purpose: '" << id << "'";
throw parse_error(ss.str());
}
}
std::string title = level == 0 ? "" : (*section_cfg)["title"].str();
sec.id = id;
sec.title = title;
std::vector<std::string>::const_iterator it;
// Find all child sections.
for (it = sections.begin(); it != sections.end(); ++it) {
if (const config &child_cfg = help_cfg->find_child("section", "id", *it))
{
section child_section;
parse_config_internal(help_cfg, &child_cfg, child_section, level + 1);
sec.add_section(child_section);
}
else {
std::stringstream ss;
ss << "Help-section '" << *it << "' referenced from '"
<< id << "' but could not be found.";
throw parse_error(ss.str());
}
}
generate_sections(help_cfg, (*section_cfg)["sections_generator"], sec, level);
//TODO: harmonize topics/sections sorting
if ((*section_cfg)["sort_sections"] == "yes") {
std::sort(sec.sections.begin(),sec.sections.end(), section_less());
}
bool sort_topics = false;
bool sort_generated = true;
if ((*section_cfg)["sort_topics"] == "yes") {
sort_topics = true;
sort_generated = false;
} else if ((*section_cfg)["sort_topics"] == "no") {
sort_topics = false;
sort_generated = false;
} else if ((*section_cfg)["sort_topics"] == "generated") {
sort_topics = false;
sort_generated = true;
} else if (!(*section_cfg)["sort_topics"].empty()) {
std::stringstream ss;
ss << "Invalid sort option: '" << (*section_cfg)["sort_topics"] << "'";
throw parse_error(ss.str());
}
std::vector<topic> generated_topics = generate_topics(sort_generated,(*section_cfg)["generator"]);
const std::vector<std::string> topics_id = utils::quoted_split((*section_cfg)["topics"]);
std::vector<topic> topics;
// Find all topics in this section.
for (it = topics_id.begin(); it != topics_id.end(); ++it) {
if (const config &topic_cfg = help_cfg->find_child("topic", "id", *it))
{
std::string text = topic_cfg["text"];
text += generate_topic_text(topic_cfg["generator"], help_cfg, sec);
topic child_topic(topic_cfg["title"], topic_cfg["id"], text);
if (!is_valid_id(child_topic.id)) {
std::stringstream ss;
ss << "Invalid ID, used for internal purpose: '" << id << "'";
throw parse_error(ss.str());
}
topics.push_back(child_topic);
}
else {
std::stringstream ss;
ss << "Help-topic '" << *it << "' referenced from '" << id
<< "' but could not be found." << std::endl;
throw parse_error(ss.str());
}
}
if (sort_topics) {
std::sort(topics.begin(),topics.end(), title_less());
std::sort(generated_topics.begin(),
generated_topics.end(), title_less());
std::merge(generated_topics.begin(),
generated_topics.end(),topics.begin(),topics.end()
,std::back_inserter(sec.topics),title_less());
}
else {
sec.topics.insert(sec.topics.end(),
topics.begin(), topics.end());
sec.topics.insert(sec.topics.end(),
generated_topics.begin(), generated_topics.end());
}
}
}
section parse_config(const config *cfg)
{
section sec;
if (cfg != nullptr) {
const config& toplevel_cfg = cfg->child("toplevel");
parse_config_internal(cfg, toplevel_cfg ? &toplevel_cfg : nullptr, sec);
}
return sec;
}
std::vector<topic> generate_topics(const bool sort_generated,const std::string &generator)
{
std::vector<topic> res;
if (generator.empty()) {
return res;
}
if (generator == "abilities") {
res = generate_ability_topics(sort_generated);
} else if (generator == "weapon_specials") {
res = generate_weapon_special_topics(sort_generated);
} else if (generator == "time_of_days") {
res = generate_time_of_day_topics(sort_generated);
} else if (generator == "traits") {
res = generate_trait_topics(sort_generated);
} else {
std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
if (parts.size() > 1 && parts[0] == "units") {
res = generate_unit_topics(sort_generated, parts[1]);
} else if (parts[0] == "era" && parts.size()>1) {
res = generate_era_topics(sort_generated, parts[1]);
} else {
WRN_HP << "Found a topic generator that I didn't recognize: " << generator << "\n";
}
}
return res;
}
void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level)
{
if (generator == "races") {
generate_races_sections(help_cfg, sec, level);
} else if (generator == "terrains") {
generate_terrain_sections(help_cfg, sec, level);
} else if (generator == "eras") {
DBG_HP << "Generating eras...\n";
generate_era_sections(help_cfg, sec, level);
} else {
std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
if (parts.size() > 1 && parts[0] == "units") {
generate_unit_sections(help_cfg, sec, level, true, parts[1]);
} else if (generator.size() > 0) {
WRN_HP << "Found a section generator that I didn't recognize: " << generator << "\n";
}
}
}
std::string generate_topic_text(const std::string &generator, const config *help_cfg, const section &sec)
{
std::string empty_string = "";
if (generator.empty()) {
return empty_string;
} else {
std::vector<std::string> parts = utils::split(generator, ':');
if (parts.size() > 1 && parts[0] == "contents") {
if (parts[1] == "generated") {
return generate_contents_links(sec);
} else {
return generate_contents_links(parts[1], help_cfg);
}
}
}
return empty_string;
}
topic_text::~topic_text()
{
if (generator_ && --generator_->count == 0)
delete generator_;
}
topic_text::topic_text(const topic_text& t): parsed_text_(t.parsed_text_), generator_(t.generator_)
{
if (generator_)
++generator_->count;
}
topic_text &topic_text::operator=(topic_generator *g)
{
if (generator_ && --generator_->count == 0)
delete generator_;
generator_ = g;
return *this;
}
const config& topic_text::parsed_text() const
{
if (generator_) {
parsed_text_ = parse_text((*generator_)());
if (--generator_->count == 0)
delete generator_;
generator_ = nullptr;
}
return parsed_text_;
}
std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
{
std::vector<topic> topics;
std::stringstream toplevel;
if (! resources::tod_manager) {
toplevel << N_("Only available during a scenario.");
topics.emplace_back("Time of Day Schedule", "..schedule", toplevel.str());
return topics;
}
const std::vector<time_of_day>& times = resources::tod_manager->times();
for (const time_of_day& time : times)
{
const std::string id = "time_of_day_" + time.id;
const std::string image = "<img>src='" + time.image + "'</img>";
std::stringstream text;
toplevel << make_link(time.name.str(), id) << jump_to(160) <<
image << jump(30) << time.lawful_bonus << '\n';
text << image << '\n' <<
time.description.str() << '\n' <<
"Lawful Bonus: " << time.lawful_bonus << '\n' <<
'\n' << make_link(N_("Schedule"), "..schedule");
topics.emplace_back(time.name.str(), id, text.str());
}
topics.emplace_back("Time of Day Schedule", "..schedule", toplevel.str());
return topics;
}
std::vector<topic> generate_weapon_special_topics(const bool sort_generated)
{
std::vector<topic> topics;
std::map<t_string, std::string> special_description;
std::map<t_string, std::set<std::string, string_less>> special_units;
for (const unit_type_data::unit_type_map::value_type &type_mapping : unit_types.types())
{
const unit_type &type = type_mapping.second;
// Only show the weapon special if we find it on a unit that
// detailed description should be shown about.
if (description_type(type) != FULL_DESCRIPTION)
continue;
for (const attack_type& atk : type.attacks()) {
std::vector<std::pair<t_string, t_string>> specials = atk.special_tooltips();
for ( std::size_t i = 0; i != specials.size(); ++i )
{
special_description.emplace(specials[i].first, specials[i].second);
if (!type.hide_help()) {
//add a link in the list of units having this special
std::string type_name = type.type_name();
//check for variations (walking corpse/soulless etc)
const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
std::string ref_id = section_prefix + unit_prefix + type.id();
//we put the translated name at the beginning of the hyperlink,
//so the automatic alphabetic sorting of std::set can use it
std::string link = make_link(type_name, ref_id);
special_units[specials[i].first].insert(link);
}
}
}
for(config adv : type.modification_advancements()) {
for(config effect : adv.child_range("effect")) {
if(effect["apply_to"] == "new_attack" && effect.has_child("specials")) {
for(config::any_child spec : effect.child("specials").all_children_range()) {
if(!spec.cfg["name"].empty()) {
special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
if(!type.hide_help()) {
//add a link in the list of units having this special
std::string type_name = type.type_name();
//check for variations (walking corpse/soulless etc)
const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
std::string ref_id = section_prefix + unit_prefix + type.id();
//we put the translated name at the beginning of the hyperlink,
//so the automatic alphabetic sorting of std::set can use it
std::string link = make_link(type_name, ref_id);
special_units[spec.cfg["name"]].insert(link);
}
}
}
} else if(effect["apply_to"] == "attack" && effect.has_child("set_specials")) {
for(config::any_child spec : effect.child("set_specials").all_children_range()) {
if(!spec.cfg["name"].empty()) {
special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
if(!type.hide_help()) {
//add a link in the list of units having this special
std::string type_name = type.type_name();
//check for variations (walking corpse/soulless etc)
const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
std::string ref_id = section_prefix + unit_prefix + type.id();
//we put the translated name at the beginning of the hyperlink,
//so the automatic alphabetic sorting of std::set can use it
std::string link = make_link(type_name, ref_id);
special_units[spec.cfg["name"]].insert(link);
}
}
}
}
}
}
}
for (std::map<t_string, std::string>::iterator s = special_description.begin();
s != special_description.end(); ++s) {
// use untranslated name to have universal topic id
std::string id = "weaponspecial_" + s->first.base_str();
std::stringstream text;
text << s->second;
text << "\n\n" << _("<header>text='Units with this special attack'</header>") << "\n";
std::set<std::string, string_less> &units = special_units[s->first];
for (std::set<std::string, string_less>::iterator u = units.begin(); u != units.end(); ++u) {
text << font::unicode_bullet << " " << (*u) << "\n";
}
topics.emplace_back(s->first, id, text.str());
}
if (sort_generated)
std::sort(topics.begin(), topics.end(), title_less());
return topics;
}
std::vector<topic> generate_ability_topics(const bool sort_generated)
{
std::vector<topic> topics;
std::map<std::string, const unit_type::ability_metadata*> ability_topic_data;
std::map<std::string, std::set<std::string, string_less>> ability_units;
const auto parse = [&](const unit_type& type, const unit_type::ability_metadata& ability) {
// NOTE: neither ability names nor ability ids are necessarily unique. Creating
// topics for either each unique name or each unique id means certain abilities
// will be excluded from help. So... the ability topic ref id is a combination
// of id and (untranslated) name. It's rather ugly, but it works.
const std::string topic_ref = ability.id + ability.name.base_str();
ability_topic_data.emplace(topic_ref, &ability);
if(!type.hide_help()) {
// Add a link in the list of units with this ability
// We put the translated name at the beginning of the hyperlink,
// so the automatic alphabetic sorting of std::set can use it
const std::string link = make_link(type.type_name(), unit_prefix + type.id());
ability_units[topic_ref].insert(link);
}
};
// Look through all the unit types. If a unit of that type would have a full
// description, add its abilities to the potential topic list. We don't want
// to show abilities that the user has not encountered yet.
for(const auto& type_mapping : unit_types.types()) {
const unit_type& type = type_mapping.second;
if(description_type(type) != FULL_DESCRIPTION) {
continue;
}
for(const unit_type::ability_metadata& ability : type.abilities_metadata()) {
parse(type, ability);
}
for(const unit_type::ability_metadata& ability : type.adv_abilities_metadata()) {
parse(type, ability);
}
}
for(const auto& a : ability_topic_data) {
if (a.second->name.empty()) {
continue;
}
std::ostringstream text;
text << a.second->description;
text << "\n\n" << _("<header>text='Units with this ability'</header>") << "\n";
for(const auto& u : ability_units[a.first]) { // first is the topic ref id
text << font::unicode_bullet << " " << u << "\n";
}
topics.emplace_back(a.second->name, ability_prefix + a.first, text.str());
}
if(sort_generated) {
std::sort(topics.begin(), topics.end(), title_less());
}
return topics;
}
std::vector<topic> generate_era_topics(const bool sort_generated, const std::string & era_id)
{
std::vector<topic> topics;
const config & era = game_cfg->find_child("era","id", era_id);
if(era && !era["hide_help"].to_bool()) {
topics = generate_faction_topics(era, sort_generated);
std::vector<std::string> faction_links;
for (const topic & t : topics) {
faction_links.push_back(make_link(t.title, t.id));
}
std::stringstream text;
text << "<header>text='" << _("Era:") << " " << era["name"] << "'</header>" << "\n";
text << "\n";
const config::attribute_value& description = era["description"];
if (!description.empty()) {
text << description.t_str() << "\n";
text << "\n";
}
text << "<header>text='" << _("Factions") << "'</header>" << "\n";
std::sort(faction_links.begin(), faction_links.end());
for (const std::string &link : faction_links) {
text << font::unicode_bullet << " " << link << "\n";
}
topic era_topic(era["name"], ".." + era_prefix + era["id"].str(), text.str());
topics.push_back( era_topic );
}
return topics;
}
std::vector<topic> generate_faction_topics(const config & era, const bool sort_generated)
{
std::vector<topic> topics;
for (const config &f : era.child_range("multiplayer_side")) {
const std::string& id = f["id"];
if (id == "Random")
continue;
std::stringstream text;
const config::attribute_value& description = f["description"];
if (!description.empty()) {
text << description.t_str() << "\n";
text << "\n";
}
const std::vector<std::string> recruit_ids = utils::split(f["recruit"]);
std::set<std::string> races;
std::set<std::string> alignments;
for (const std::string & u_id : recruit_ids) {
if (const unit_type * t = unit_types.find(u_id, unit_type::HELP_INDEXED)) {
assert(t);
const unit_type & type = *t;
if (const unit_race *r = unit_types.find_race(type.race_id())) {
races.insert(make_link(r->plural_name(), std::string("..") + race_prefix + r->id()));
}
DBG_HP << type.alignment() << " -> " << type.alignment_description(type.alignment(), type.genders().front()) << "\n";
alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
}
}
if (!races.empty()) {
std::set<std::string>::iterator it = races.begin();
text << _("Races: ") << *(it++);
while(it != races.end()) {
text << ", " << *(it++);
}
text << "\n\n";
}
if (!alignments.empty()) {
std::set<std::string>::iterator it = alignments.begin();
text << _("Alignments: ") << *(it++);
while(it != alignments.end()) {
text << ", " << *(it++);
}
text << "\n\n";
}
text << "<header>text='" << _("Leaders") << "'</header>" << "\n";
const std::vector<std::string> leaders =
make_unit_links_list( utils::split(f["leader"]), true );
for (const std::string &link : leaders) {
text << font::unicode_bullet << " " << link << "\n";
}
text << "\n";
text << "<header>text='" << _("Recruits") << "'</header>" << "\n";
const std::vector<std::string> recruit_links =
make_unit_links_list( recruit_ids, true );
for (const std::string &link : recruit_links) {
text << font::unicode_bullet << " " << link << "\n";
}
const std::string name = f["name"];
const std::string ref_id = faction_prefix + era["id"] + "_" + id;
topics.emplace_back(name, ref_id, text.str());
}
if (sort_generated)
std::sort(topics.begin(), topics.end(), title_less());
return topics;
}
std::vector<topic> generate_trait_topics(const bool sort_generated)
{
std::vector<topic> topics;
std::map<t_string, const config> trait_list;
for (const config & trait : unit_types.traits()) {
const std::string trait_id = trait["id"];
trait_list.emplace(trait_id, trait);
}
for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
{
const unit_type &type = i.second;
if (description_type(type) == FULL_DESCRIPTION) {
if (config::const_child_itors traits = type.possible_traits()) {
for (const config & trait : traits) {
const std::string trait_id = trait["id"];
trait_list.emplace(trait_id, trait);
}
}
if (const unit_race *r = unit_types.find_race(type.race_id())) {
for (const config & trait : r->additional_traits()) {
const std::string trait_id = trait["id"];
trait_list.emplace(trait_id, trait);
}
}
}
}
for (std::map<t_string, const config>::iterator a = trait_list.begin(); a != trait_list.end(); ++a) {
std::string id = "traits_" + a->first;
const config trait = a->second;
std::string name = trait["male_name"].str();
if (name.empty()) name = trait["female_name"].str();
if (name.empty()) name = trait["name"].str();
if (name.empty()) continue; // Hidden trait
std::stringstream text;
if (!trait["help_text"].empty()) {
text << trait["help_text"];
} else if (!trait["description"].empty()) {
text << trait["description"];
} else {
text << _("No description available.");
}
text << "\n\n";
if (trait["availability"] == "musthave") {
text << _("Availability: ") << _("Must-have") << "\n";
} else if (trait["availability"] == "none") {
text << _("Availability: ") << _("Unavailable") << "\n";
}
topics.emplace_back(name, id, text.str());
}
if (sort_generated)
std::sort(topics.begin(), topics.end(), title_less());
return topics;
}
std::string make_unit_link(const std::string& type_id)
{
std::string link;
const unit_type *type = unit_types.find(type_id, unit_type::HELP_INDEXED);
if (!type) {
std::cerr << "Unknown unit type : " << type_id << "\n";
// don't return an hyperlink (no page)
// instead show the id (as hint)
link = type_id;
} else if (!type->hide_help()) {
std::string name = type->type_name();
std::string ref_id;
if (description_type(*type) == FULL_DESCRIPTION) {
const std::string section_prefix = type->show_variations_in_help() ? ".." : "";
ref_id = section_prefix + unit_prefix + type->id();
} else {
ref_id = unknown_unit_topic;
name += " (?)";
}
link = make_link(name, ref_id);
} // if hide_help then link is an empty string
return link;
}
std::vector<std::string> make_unit_links_list(const std::vector<std::string>& type_id_list, bool ordered)
{
std::vector<std::string> links_list;
for (const std::string &type_id : type_id_list) {
std::string unit_link = make_unit_link(type_id);
if (!unit_link.empty())
links_list.push_back(unit_link);
}
if (ordered)
std::sort(links_list.begin(), links_list.end());
return links_list;
}
void generate_races_sections(const config *help_cfg, section &sec, int level)
{
std::set<std::string, string_less> races;
std::set<std::string, string_less> visible_races;
for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
{
const unit_type &type = i.second;
UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
if (desc_type == FULL_DESCRIPTION) {
races.insert(type.race_id());
if (!type.hide_help())
visible_races.insert(type.race_id());
}
}
for(std::set<std::string, string_less>::iterator it = races.begin(); it != races.end(); ++it) {
section race_section;
config section_cfg;
bool hidden = (visible_races.count(*it) == 0);
section_cfg["id"] = hidden_symbol(hidden) + race_prefix + *it;
std::string title;
if (const unit_race *r = unit_types.find_race(*it)) {
title = r->plural_name();
} else {
title = _ ("race^Miscellaneous");
}
section_cfg["title"] = title;
section_cfg["sections_generator"] = "units:" + *it;
section_cfg["generator"] = "units:" + *it;
parse_config_internal(help_cfg, §ion_cfg, race_section, level+1);
sec.add_section(race_section);
}
}
void generate_era_sections(const config* help_cfg, section & sec, int level)
{
for (const config & era : game_cfg->child_range("era")) {
if (era["hide_help"].to_bool()) {
continue;
}
DBG_HP << "Adding help section: " << era["id"].str() << "\n";
section era_section;
config section_cfg;
section_cfg["id"] = era_prefix + era["id"].str();
section_cfg["title"] = era["name"];
section_cfg["generator"] = "era:" + era["id"].str();
DBG_HP << section_cfg.debug() << "\n";
parse_config_internal(help_cfg, §ion_cfg, era_section, level+1);
sec.add_section(era_section);
}
}
void generate_terrain_sections(const config* /*help_cfg*/, section& sec, int /*level*/)
{
ter_data_cache tdata = load_terrain_types_data();
if (!tdata) {
WRN_HP << "When building terrain help sections, couldn't acquire terrain types data, aborting.\n";
return;
}
std::map<std::string, section> base_map;
const t_translation::ter_list& t_listi = tdata->list();
for (const t_translation::terrain_code& t : t_listi) {
const terrain_type& info = tdata->get_terrain_info(t);
bool hidden = info.is_combined() || info.hide_help();
if (preferences::encountered_terrains().find(t)
== preferences::encountered_terrains().end() && !info.is_overlay())
hidden = true;
topic terrain_topic;
terrain_topic.title = info.editor_name();
terrain_topic.id = hidden_symbol(hidden) + terrain_prefix + info.id();
terrain_topic.text = new terrain_topic_generator(info);
t_translation::ter_list base_terrains = tdata->underlying_union_terrain(t);
for (const t_translation::terrain_code& base : base_terrains) {
const terrain_type& base_info = tdata->get_terrain_info(base);
if (!base_info.is_nonnull() || base_info.hide_help())
continue;
section& base_section = base_map[base_info.id()];
base_section.id = terrain_prefix + base_info.id();
base_section.title = base_info.editor_name();
if (base_info.id() == info.id())
terrain_topic.id = ".." + terrain_prefix + info.id();
base_section.topics.push_back(terrain_topic);
}
}
for (std::map<std::string, section>::const_iterator it = base_map.begin(); it != base_map.end(); ++it) {
sec.add_section(it->second);
}
}
void generate_unit_sections(const config* /*help_cfg*/, section& sec, int level, const bool /*sort_generated*/, const std::string& race)
{
for (const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
const unit_type &type = i.second;
if (type.race_id() != race)
continue;
if (!type.show_variations_in_help())
continue;
section base_unit;
for (const std::string &variation_id : type.variations()) {
// TODO: Do we apply encountered stuff to variations?
const unit_type &var_type = type.get_variation(variation_id);
const std::string topic_name = var_type.type_name() + "\n" + var_type.variation_name();
const std::string var_ref = hidden_symbol(var_type.hide_help()) + variation_prefix + var_type.id() + "_" + variation_id;
topic var_topic(topic_name, var_ref, "");
var_topic.text = new unit_topic_generator(var_type, variation_id);
base_unit.topics.push_back(var_topic);
}
const std::string type_name = type.type_name();
const std::string ref_id = hidden_symbol(type.hide_help()) + unit_prefix + type.id();
base_unit.id = ref_id;
base_unit.title = type_name;
base_unit.level = level +1;
sec.add_section(base_unit);
}
}
std::vector<topic> generate_unit_topics(const bool sort_generated, const std::string& race)
{
std::vector<topic> topics;
std::set<std::string, string_less> race_units;
std::set<std::string, string_less> race_topics;
std::set<std::string> alignments;
for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
{
const unit_type &type = i.second;
if (type.race_id() != race)
continue;
UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
if (desc_type != FULL_DESCRIPTION)
continue;
const std::string type_name = type.type_name();
const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
topic unit_topic(type_name, ref_id, "");
unit_topic.text = new unit_topic_generator(type);
topics.push_back(unit_topic);
if (!type.hide_help()) {
// we also record an hyperlink of this unit
// in the list used for the race topic
std::string link = make_link(type_name, ref_id);
race_units.insert(link);
alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
}
}
//generate the hidden race description topic
std::string race_id = "..race_"+race;
std::string race_name;
std::string race_description;
if (const unit_race *r = unit_types.find_race(race)) {
race_name = r->plural_name();
race_description = r->description();
// if (description.empty()) description = _("No description Available");
for (const config &additional_topic : r->additional_topics())
{
std::string id = additional_topic["id"];
std::string title = additional_topic["title"];
std::string text = additional_topic["text"];
//topic additional_topic(title, id, text);
topics.emplace_back(title,id,text);
std::string link = make_link(title, id);
race_topics.insert(link);
}
} else {
race_name = _ ("race^Miscellaneous");
// description = _("Here put the description of the Miscellaneous race");
}
std::stringstream text;
if (!race_description.empty()) {
text << race_description << "\n\n";
}
if (!alignments.empty()) {
std::set<std::string>::iterator it = alignments.begin();
text << (alignments.size() > 1 ? _("Alignments: ") : _("Alignment: ")) << *(it++);
while(it != alignments.end()) {
text << ", " << *(it++);
}
text << "\n\n";
}
text << _("<header>text='Units of this race'</header>") << "\n";
for (std::set<std::string, string_less>::iterator u = race_units.begin(); u != race_units.end(); ++u) {
text << font::unicode_bullet << " " << (*u) << "\n";
}
topics.emplace_back(race_name, race_id, text.str());
if (sort_generated)
std::sort(topics.begin(), topics.end(), title_less());