The Battle for Wesnoth  1.15.9+dev
help_impl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "help/help_impl.hpp"
16 
17 #include "about.hpp" // for get_text
18 #include "actions/attack.hpp" // for time_of_day bonus
19 #include "display.hpp" // for display
20 #include "display_context.hpp" // for display_context
21 #include "formula/string_utils.hpp" // for VGETTEXT
22 #include "game_config.hpp" // for debug, menu_contract, etc
23 #include "game_config_manager.hpp" // for game_config_manager
24 #include "preferences/game.hpp" // for encountered_terrains, etc
25 #include "gettext.hpp" // for _, gettext, N_
27 #include "hotkey/hotkey_command.hpp" // for is_scope_active, etc
28 #include "picture.hpp" // for get_image, locator
29 #include "log.hpp" // for LOG_STREAM, logger, etc
30 #include "utils/make_enum.hpp" // for operator<<
31 #include "map/map.hpp" // for gamemap
32 #include "font/marked-up_text.hpp" // for is_cjk_char, word_wrap_text
33 #include "font/standard_colors.hpp" // for NORMAL_COLOR
34 #include "units/race.hpp" // for unit_race, etc
35 #include "resources.hpp" // for tod_manager, config_manager
36 #include "sdl/surface.hpp" // for surface
37 #include "serialization/string_utils.hpp" // for split, quoted_split, etc
38 #include "serialization/unicode_cast.hpp" // for unicode_cast
39 #include "serialization/utf8_exception.hpp" // for char_t, etc
40 #include "terrain/terrain.hpp" // for terrain_type
41 #include "terrain/translation.hpp" // for operator==, ter_list, etc
42 #include "terrain/type_data.hpp" // for terrain_type_data, etc
43 #include "time_of_day.hpp" // for time_of_day
44 #include "tod_manager.hpp" // for tod_manager
45 #include "tstring.hpp" // for t_string, operator<<
46 #include "units/types.hpp" // for unit_type, unit_type_data, etc
47 #include "serialization/unicode.hpp" // for iterator
48 #include "color.hpp"
49 
50 #include <cassert> // for assert
51 #include <algorithm> // for sort, find, transform, etc
52 #include <iostream> // for operator<<, basic_ostream, etc
53 #include <iterator> // for back_insert_iterator, etc
54 #include <map> // for map, etc
55 #include <set>
56 
57 static lg::log_domain log_display("display");
58 #define WRN_DP LOG_STREAM(warn, log_display)
59 
60 static lg::log_domain log_help("help");
61 #define WRN_HP LOG_STREAM(warn, log_help)
62 #define DBG_HP LOG_STREAM(debug, log_help)
63 
64 namespace help {
65 
66 const game_config_view *game_cfg = nullptr;
67 // The default toplevel.
69 // All sections and topics not referenced from the default toplevel.
71 
74 boost::tribool last_debug_state = boost::indeterminate;
75 
76 std::vector<std::string> empty_string_vector;
77 const int max_section_level = 15;
80 const int box_width = 2;
82 const unsigned max_history = 100;
83 const std::string topic_img = "help/topic.png";
84 const std::string closed_section_img = "help/closed_section.png";
85 const std::string open_section_img = "help/open_section.png";
86 // The topic to open by default when opening the help dialog.
87 const std::string default_show_topic = "..introduction";
88 const std::string unknown_unit_topic = ".unknown_unit";
89 const std::string unit_prefix = "unit_";
90 const std::string terrain_prefix = "terrain_";
91 const std::string race_prefix = "race_";
92 const std::string faction_prefix = "faction_";
93 const std::string era_prefix = "era_";
94 const std::string variation_prefix = "variation_";
95 const std::string ability_prefix = "ability_";
96 
97 bool section_is_referenced(const std::string &section_id, const config &cfg)
98 {
99  if (const config &toplevel = cfg.child("toplevel"))
100  {
101  const std::vector<std::string> toplevel_refs
102  = utils::quoted_split(toplevel["sections"]);
103  if (std::find(toplevel_refs.begin(), toplevel_refs.end(), section_id)
104  != toplevel_refs.end()) {
105  return true;
106  }
107  }
108 
109  for (const config &section : cfg.child_range("section"))
110  {
111  const std::vector<std::string> sections_refd
112  = utils::quoted_split(section["sections"]);
113  if (std::find(sections_refd.begin(), sections_refd.end(), section_id)
114  != sections_refd.end()) {
115  return true;
116  }
117  }
118  return false;
119 }
120 
121 bool topic_is_referenced(const std::string &topic_id, const config &cfg)
122 {
123  if (const config &toplevel = cfg.child("toplevel"))
124  {
125  const std::vector<std::string> toplevel_refs
126  = utils::quoted_split(toplevel["topics"]);
127  if (std::find(toplevel_refs.begin(), toplevel_refs.end(), topic_id)
128  != toplevel_refs.end()) {
129  return true;
130  }
131  }
132 
133  for (const config &section : cfg.child_range("section"))
134  {
135  const std::vector<std::string> topics_refd
136  = utils::quoted_split(section["topics"]);
137  if (std::find(topics_refd.begin(), topics_refd.end(), topic_id)
138  != topics_refd.end()) {
139  return true;
140  }
141  }
142  return false;
143 }
144 
145 void parse_config_internal(const config *help_cfg, const config *section_cfg,
146  section &sec, int level)
147 {
148  if (level > max_section_level) {
149  std::cerr << "Maximum section depth has been reached. Maybe circular dependency?"
150  << std::endl;
151  }
152  else if (section_cfg != nullptr) {
153  const std::vector<std::string> sections = utils::quoted_split((*section_cfg)["sections"]);
154  std::string id = level == 0 ? "toplevel" : (*section_cfg)["id"].str();
155  if (level != 0) {
156  if (!is_valid_id(id)) {
157  std::stringstream ss;
158  ss << "Invalid ID, used for internal purpose: '" << id << "'";
159  throw parse_error(ss.str());
160  }
161  }
162  std::string title = level == 0 ? "" : (*section_cfg)["title"].str();
163  sec.id = id;
164  sec.title = title;
165  std::vector<std::string>::const_iterator it;
166  // Find all child sections.
167  for (it = sections.begin(); it != sections.end(); ++it) {
168  if (const config &child_cfg = help_cfg->find_child("section", "id", *it))
169  {
170  section child_section;
171  parse_config_internal(help_cfg, &child_cfg, child_section, level + 1);
172  sec.add_section(child_section);
173  }
174  else {
175  std::stringstream ss;
176  ss << "Help-section '" << *it << "' referenced from '"
177  << id << "' but could not be found.";
178  throw parse_error(ss.str());
179  }
180  }
181 
182  generate_sections(help_cfg, (*section_cfg)["sections_generator"], sec, level);
183  if ((*section_cfg)["sort_sections"] == "yes") {
184  sec.sections.sort(section_less());
185  }
186 
187  bool sort_topics = false;
188  bool sort_generated = true;
189 
190  if ((*section_cfg)["sort_topics"] == "yes") {
191  sort_topics = true;
192  sort_generated = false;
193  } else if ((*section_cfg)["sort_topics"] == "no") {
194  sort_topics = false;
195  sort_generated = false;
196  } else if ((*section_cfg)["sort_topics"] == "generated") {
197  sort_topics = false;
198  sort_generated = true;
199  } else if (!(*section_cfg)["sort_topics"].empty()) {
200  std::stringstream ss;
201  ss << "Invalid sort option: '" << (*section_cfg)["sort_topics"] << "'";
202  throw parse_error(ss.str());
203  }
204 
205  std::vector<topic> generated_topics =
206  generate_topics(sort_generated,(*section_cfg)["generator"]);
207 
208  const std::vector<std::string> topics_id = utils::quoted_split((*section_cfg)["topics"]);
209  std::vector<topic> topics;
210 
211  // Find all topics in this section.
212  for (it = topics_id.begin(); it != topics_id.end(); ++it) {
213  if (const config &topic_cfg = help_cfg->find_child("topic", "id", *it))
214  {
215  std::string text = topic_cfg["text"];
216  text += generate_topic_text(topic_cfg["generator"], help_cfg, sec, generated_topics);
217  topic child_topic(topic_cfg["title"], topic_cfg["id"], text);
218  if (!is_valid_id(child_topic.id)) {
219  std::stringstream ss;
220  ss << "Invalid ID, used for internal purpose: '" << id << "'";
221  throw parse_error(ss.str());
222  }
223  topics.push_back(child_topic);
224  }
225  else {
226  std::stringstream ss;
227  ss << "Help-topic '" << *it << "' referenced from '" << id
228  << "' but could not be found." << std::endl;
229  throw parse_error(ss.str());
230  }
231  }
232 
233  if (sort_topics) {
234  std::sort(topics.begin(),topics.end(), title_less());
235  std::sort(generated_topics.begin(),
236  generated_topics.end(), title_less());
237  std::merge(generated_topics.begin(),
238  generated_topics.end(),topics.begin(),topics.end()
239  ,std::back_inserter(sec.topics),title_less());
240  }
241  else {
242  sec.topics.insert(sec.topics.end(),
243  topics.begin(), topics.end());
244  sec.topics.insert(sec.topics.end(),
245  generated_topics.begin(), generated_topics.end());
246  }
247  }
248 }
249 
251 {
252  section sec;
253  if (cfg != nullptr) {
254  const config& toplevel_cfg = cfg->child("toplevel");
255  parse_config_internal(cfg, toplevel_cfg ? &toplevel_cfg : nullptr, sec);
256  }
257  return sec;
258 }
259 
260 std::vector<topic> generate_topics(const bool sort_generated,const std::string &generator)
261 {
262  std::vector<topic> res;
263  if (generator.empty()) {
264  return res;
265  }
266 
267  if (generator == "abilities") {
268  res = generate_ability_topics(sort_generated);
269  } else if (generator == "weapon_specials") {
270  res = generate_weapon_special_topics(sort_generated);
271  } else if (generator == "time_of_days") {
272  res = generate_time_of_day_topics(sort_generated);
273  } else if (generator == "traits") {
274  res = generate_trait_topics(sort_generated);
275  } else {
276  std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
277  if (parts.size() > 1 && parts[0] == "units") {
278  res = generate_unit_topics(sort_generated, parts[1]);
279  } else if (parts[0] == "era" && parts.size()>1) {
280  res = generate_era_topics(sort_generated, parts[1]);
281  } else {
282  WRN_HP << "Found a topic generator that I didn't recognize: " << generator << "\n";
283  }
284  }
285 
286  return res;
287 }
288 
289 void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level)
290 {
291  if (generator == "races") {
292  generate_races_sections(help_cfg, sec, level);
293  } else if (generator == "terrains") {
294  generate_terrain_sections(sec, level);
295  } else if (generator == "eras") {
296  DBG_HP << "Generating eras...\n";
297  generate_era_sections(help_cfg, sec, level);
298  } else {
299  std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
300  if (parts.size() > 1 && parts[0] == "units") {
301  generate_unit_sections(help_cfg, sec, level, true, parts[1]);
302  } else if (generator.size() > 0) {
303  WRN_HP << "Found a section generator that I didn't recognize: " << generator << "\n";
304  }
305  }
306 }
307 
308 std::string generate_topic_text(const std::string &generator, const config *help_cfg, const section &sec, const std::vector<topic>& generated_topics)
309 {
310  std::string empty_string = "";
311  if (generator.empty()) {
312  return empty_string;
313  } else {
314  std::vector<std::string> parts = utils::split(generator, ':');
315  if (parts.size() > 1 && parts[0] == "contents") {
316  if (parts[1] == "generated") {
317  return generate_contents_links(sec, generated_topics);
318  } else {
319  return generate_contents_links(parts[1], help_cfg);
320  }
321  }
322  }
323  return empty_string;
324 }
325 
326 topic_text& topic_text::operator=(std::shared_ptr<topic_generator> g)
327 {
328  generator_ = g;
329  return *this;
330 }
331 
332 const std::vector<std::string>& topic_text::parsed_text() const
333 {
334  if (generator_) {
336  // This caches the result, so doesn't need the generator any more
337  generator_.reset();
338  }
339  return parsed_text_;
340 }
341 
342 std::string time_of_day_bonus_colored(const int time_of_day_bonus)
343 {
344  // Use same red/green colouring scheme as time_of_day_at() in reports.cpp for consistency
345  return std::string("<format>color='") + (time_of_day_bonus > 0 ? "green" : (time_of_day_bonus < 0 ? "red" : "white")) + "' text='" + std::to_string(time_of_day_bonus) + "'</format>";
346 }
347 
348 std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
349 {
350  std::vector<topic> topics;
351  std::stringstream toplevel;
352 
353  if (! resources::tod_manager) {
354  toplevel << _("Only available during a scenario.");
355  topics.emplace_back(_("Time of Day Schedule"), "..schedule", toplevel.str());
356  return topics;
357  }
358 
359  const std::vector<time_of_day>& times = resources::tod_manager->times();
360  for (const time_of_day& time : times)
361  {
362  const std::string id = "time_of_day_" + time.id;
363  const std::string image = "<img>src='" + time.image + "'</img>";
364  const std::string image_lawful = "<img>src='icons/alignments/alignment_lawful_30.png'</img>";
365  const std::string image_neutral = "<img>src='icons/alignments/alignment_neutral_30.png'</img>";
366  const std::string image_chaotic = "<img>src='icons/alignments/alignment_chaotic_30.png'</img>";
367  const std::string image_liminal = "<img>src='icons/alignments/alignment_liminal_30.png'</img>";
368  std::stringstream text;
369 
370  const int lawful_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::LAWFUL, false, resources::tod_manager->get_max_liminal_bonus());
371  const int neutral_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::NEUTRAL, false, resources::tod_manager->get_max_liminal_bonus());
372  const int chaotic_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::CHAOTIC, false, resources::tod_manager->get_max_liminal_bonus());
373  const int liminal_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::LIMINAL, false, resources::tod_manager->get_max_liminal_bonus());
374 
375  toplevel << make_link(time.name.str(), id) << jump_to(160) << image << jump(30) <<
376  image_lawful << time_of_day_bonus_colored(lawful_bonus) << jump_to(390) <<
377  image_neutral << time_of_day_bonus_colored(neutral_bonus) << jump_to(450) <<
378  image_chaotic << time_of_day_bonus_colored(chaotic_bonus) << jump_to(520) <<
379  image_liminal << time_of_day_bonus_colored(liminal_bonus) << '\n';
380 
381  text << image << '\n' << time.description.str() << '\n' <<
382  image_lawful << _("Lawful Bonus:") << ' ' << time_of_day_bonus_colored(lawful_bonus) << '\n' <<
383  image_neutral << _("Neutral Bonus:") << ' ' << time_of_day_bonus_colored(neutral_bonus) << '\n' <<
384  image_chaotic << _("Chaotic Bonus:") << ' ' << time_of_day_bonus_colored(chaotic_bonus) << '\n' <<
385  image_liminal << _("Liminal Bonus:") << ' ' << time_of_day_bonus_colored(liminal_bonus) << '\n' <<
386  '\n' << make_link(_("Schedule"), "..schedule");
387 
388  topics.emplace_back(time.name.str(), id, text.str());
389  }
390 
391  topics.emplace_back(_("Time of Day Schedule"), "..schedule", toplevel.str());
392  return topics;
393 }
394 
395 std::vector<topic> generate_weapon_special_topics(const bool sort_generated)
396 {
397  std::vector<topic> topics;
398 
399  std::map<t_string, std::string> special_description;
400  std::map<t_string, std::set<std::string, string_less>> special_units;
401 
402  for (const unit_type_data::unit_type_map::value_type &type_mapping : unit_types.types())
403  {
404  const unit_type &type = type_mapping.second;
405  // Only show the weapon special if we find it on a unit that
406  // detailed description should be shown about.
407  if (description_type(type) != FULL_DESCRIPTION)
408  continue;
409 
410  for (const attack_type& atk : type.attacks()) {
411 
412  std::vector<std::pair<t_string, t_string>> specials = atk.special_tooltips();
413  for ( std::size_t i = 0; i != specials.size(); ++i )
414  {
415  special_description.emplace(specials[i].first, specials[i].second);
416 
417  if (!type.hide_help()) {
418  //add a link in the list of units having this special
419  std::string type_name = type.type_name();
420  //check for variations (walking corpse/soulless etc)
421  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
422  std::string ref_id = section_prefix + unit_prefix + type.id();
423  //we put the translated name at the beginning of the hyperlink,
424  //so the automatic alphabetic sorting of std::set can use it
425  std::string link = make_link(type_name, ref_id);
426  special_units[specials[i].first].insert(link);
427  }
428  }
429  }
430 
431  for(config adv : type.modification_advancements()) {
432  for(config effect : adv.child_range("effect")) {
433  if(effect["apply_to"] == "new_attack" && effect.has_child("specials")) {
434  for(config::any_child spec : effect.child("specials").all_children_range()) {
435  if(!spec.cfg["name"].empty()) {
436  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
437  if(!type.hide_help()) {
438  //add a link in the list of units having this special
439  std::string type_name = type.type_name();
440  //check for variations (walking corpse/soulless etc)
441  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
442  std::string ref_id = section_prefix + unit_prefix + type.id();
443  //we put the translated name at the beginning of the hyperlink,
444  //so the automatic alphabetic sorting of std::set can use it
445  std::string link = make_link(type_name, ref_id);
446  special_units[spec.cfg["name"]].insert(link);
447  }
448  }
449  }
450  } else if(effect["apply_to"] == "attack" && effect.has_child("set_specials")) {
451  for(config::any_child spec : effect.child("set_specials").all_children_range()) {
452  if(!spec.cfg["name"].empty()) {
453  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
454  if(!type.hide_help()) {
455  //add a link in the list of units having this special
456  std::string type_name = type.type_name();
457  //check for variations (walking corpse/soulless etc)
458  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
459  std::string ref_id = section_prefix + unit_prefix + type.id();
460  //we put the translated name at the beginning of the hyperlink,
461  //so the automatic alphabetic sorting of std::set can use it
462  std::string link = make_link(type_name, ref_id);
463  special_units[spec.cfg["name"]].insert(link);
464  }
465  }
466  }
467  }
468  }
469  }
470  }
471 
472  for (std::map<t_string, std::string>::iterator s = special_description.begin();
473  s != special_description.end(); ++s) {
474  // use untranslated name to have universal topic id
475  std::string id = "weaponspecial_" + s->first.base_str();
476  std::stringstream text;
477  text << s->second;
478  text << "\n\n" << _("<header>text='Units with this special attack'</header>") << "\n";
479  std::set<std::string, string_less> &units = special_units[s->first];
480  for (std::set<std::string, string_less>::iterator u = units.begin(); u != units.end(); ++u) {
481  text << font::unicode_bullet << " " << (*u) << "\n";
482  }
483 
484  topics.emplace_back(s->first, id, text.str());
485  }
486 
487  if (sort_generated)
488  std::sort(topics.begin(), topics.end(), title_less());
489  return topics;
490 }
491 
492 std::vector<topic> generate_ability_topics(const bool sort_generated)
493 {
494  std::vector<topic> topics;
495 
496  std::map<std::string, const unit_type::ability_metadata*> ability_topic_data;
497  std::map<std::string, std::set<std::string, string_less>> ability_units;
498 
499  const auto parse = [&](const unit_type& type, const unit_type::ability_metadata& ability) {
500  // NOTE: neither ability names nor ability ids are necessarily unique. Creating
501  // topics for either each unique name or each unique id means certain abilities
502  // will be excluded from help. So... the ability topic ref id is a combination
503  // of id and (untranslated) name. It's rather ugly, but it works.
504  const std::string topic_ref = ability.id + ability.name.base_str();
505 
506  ability_topic_data.emplace(topic_ref, &ability);
507 
508  if(!type.hide_help()) {
509  // Add a link in the list of units with this ability
510  // We put the translated name at the beginning of the hyperlink,
511  // so the automatic alphabetic sorting of std::set can use it
512  const std::string link = make_link(type.type_name(), unit_prefix + type.id());
513  ability_units[topic_ref].insert(link);
514  }
515  };
516 
517  // Look through all the unit types. If a unit of that type would have a full
518  // description, add its abilities to the potential topic list. We don't want
519  // to show abilities that the user has not encountered yet.
520  for(const auto& type_mapping : unit_types.types()) {
521  const unit_type& type = type_mapping.second;
522 
523  if(description_type(type) != FULL_DESCRIPTION) {
524  continue;
525  }
526 
527  for(const unit_type::ability_metadata& ability : type.abilities_metadata()) {
528  parse(type, ability);
529  }
530 
531  for(const unit_type::ability_metadata& ability : type.adv_abilities_metadata()) {
532  parse(type, ability);
533  }
534  }
535 
536  for(const auto& a : ability_topic_data) {
537  if (a.second->name.empty()) {
538  continue;
539  }
540  std::ostringstream text;
541  text << a.second->description;
542  text << "\n\n" << _("<header>text='Units with this ability'</header>") << "\n";
543 
544  for(const auto& u : ability_units[a.first]) { // first is the topic ref id
545  text << font::unicode_bullet << " " << u << "\n";
546  }
547 
548  topics.emplace_back(a.second->name, ability_prefix + a.first, text.str());
549  }
550 
551  if(sort_generated) {
552  std::sort(topics.begin(), topics.end(), title_less());
553  }
554 
555  return topics;
556 }
557 
558 std::vector<topic> generate_era_topics(const bool sort_generated, const std::string & era_id)
559 {
560  std::vector<topic> topics;
561 
562  const config & era = game_cfg->find_child("era","id", era_id);
563  if(era && !era["hide_help"].to_bool()) {
564  topics = generate_faction_topics(era, sort_generated);
565 
566  std::vector<std::string> faction_links;
567  for (const topic & t : topics) {
568  faction_links.push_back(make_link(t.title, t.id));
569  }
570 
571  std::stringstream text;
572  text << "<header>text='" << _("Era:") << " " << era["name"] << "'</header>" << "\n";
573  text << "\n";
574  const config::attribute_value& description = era["description"];
575  if (!description.empty()) {
576  text << description.t_str() << "\n";
577  text << "\n";
578  }
579 
580  text << "<header>text='" << _("Factions") << "'</header>" << "\n";
581 
582  std::sort(faction_links.begin(), faction_links.end());
583  for (const std::string &link : faction_links) {
584  text << font::unicode_bullet << " " << link << "\n";
585  }
586 
587  topic era_topic(era["name"], ".." + era_prefix + era["id"].str(), text.str());
588 
589  topics.push_back( era_topic );
590  }
591  return topics;
592 }
593 
594 std::vector<topic> generate_faction_topics(const config & era, const bool sort_generated)
595 {
596  std::vector<topic> topics;
597  for (const config &f : era.child_range("multiplayer_side")) {
598  const std::string& id = f["id"];
599  if (id == "Random")
600  continue;
601 
602  std::stringstream text;
603 
604  const config::attribute_value& description = f["description"];
605  if (!description.empty()) {
606  text << description.t_str() << "\n";
607  text << "\n";
608  }
609 
610  const std::vector<std::string> recruit_ids = utils::split(f["recruit"]);
611  std::set<std::string> races;
612  std::set<std::string> alignments;
613 
614  for (const std::string & u_id : recruit_ids) {
615  if (const unit_type * t = unit_types.find(u_id, unit_type::HELP_INDEXED)) {
616  assert(t);
617  const unit_type & type = *t;
618 
619  if (const unit_race *r = unit_types.find_race(type.race_id())) {
620  races.insert(make_link(r->plural_name(), std::string("..") + race_prefix + r->id()));
621  }
622  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
623  }
624  }
625 
626  if (!races.empty()) {
627  std::set<std::string>::iterator it = races.begin();
628  text << _("Races: ") << *(it++);
629  while(it != races.end()) {
630  text << ", " << *(it++);
631  }
632  text << "\n\n";
633  }
634 
635  if (!alignments.empty()) {
636  std::set<std::string>::iterator it = alignments.begin();
637  text << _("Alignments: ") << *(it++);
638  while(it != alignments.end()) {
639  text << ", " << *(it++);
640  }
641  text << "\n\n";
642  }
643 
644  text << "<header>text='" << _("Leaders") << "'</header>" << "\n";
645  const std::vector<std::string> leaders =
646  make_unit_links_list( utils::split(f["leader"]), true );
647  for (const std::string &link : leaders) {
648  text << font::unicode_bullet << " " << link << "\n";
649  }
650 
651  text << "\n";
652 
653  text << "<header>text='" << _("Recruits") << "'</header>" << "\n";
654  const std::vector<std::string> recruit_links =
655  make_unit_links_list( recruit_ids, true );
656  for (const std::string &link : recruit_links) {
657  text << font::unicode_bullet << " " << link << "\n";
658  }
659 
660  const std::string name = f["name"];
661  const std::string ref_id = faction_prefix + era["id"] + "_" + id;
662  topics.emplace_back(name, ref_id, text.str());
663  }
664  if (sort_generated)
665  std::sort(topics.begin(), topics.end(), title_less());
666  return topics;
667 }
668 
669 std::vector<topic> generate_trait_topics(const bool sort_generated)
670 {
671  std::vector<topic> topics;
672  std::map<t_string, const config> trait_list;
673 
674  for (const config & trait : unit_types.traits()) {
675  const std::string trait_id = trait["id"];
676  trait_list.emplace(trait_id, trait);
677  }
678 
679 
680  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
681  {
682  const unit_type &type = i.second;
683  const auto desc_type = description_type(type);
684  if (desc_type == FULL_DESCRIPTION || desc_type == HIDDEN_BUT_SHOW_MACROS) {
685  if (config::const_child_itors traits = type.possible_traits()) {
686  for (const config & trait : traits) {
687  const std::string trait_id = trait["id"];
688  trait_list.emplace(trait_id, trait);
689  }
690  }
691  if (const unit_race *r = unit_types.find_race(type.race_id())) {
692  for (const config & trait : r->additional_traits()) {
693  const std::string trait_id = trait["id"];
694  trait_list.emplace(trait_id, trait);
695  }
696  }
697  }
698  }
699 
700  for (std::map<t_string, const config>::iterator a = trait_list.begin(); a != trait_list.end(); ++a) {
701  std::string id = "traits_" + a->first;
702  const config trait = a->second;
703 
704  std::string name = trait["male_name"].str();
705  if (name.empty()) name = trait["female_name"].str();
706  if (name.empty()) name = trait["name"].str();
707  if (name.empty()) continue; // Hidden trait
708 
709  std::stringstream text;
710  if (!trait["help_text"].empty()) {
711  text << trait["help_text"];
712  } else if (!trait["description"].empty()) {
713  text << trait["description"];
714  } else {
715  text << _("No description available.");
716  }
717  text << "\n\n";
718  if (trait["availability"] == "musthave") {
719  text << _("Availability: ") << _("Must-have") << "\n";
720  } else if (trait["availability"] == "none") {
721  text << _("Availability: ") << _("Unavailable") << "\n";
722  }
723  topics.emplace_back(name, id, text.str());
724  }
725 
726  if (sort_generated)
727  std::sort(topics.begin(), topics.end(), title_less());
728  return topics;
729 }
730 
731 
732 std::string make_unit_link(const std::string& type_id)
733 {
734  std::string link;
735 
737  if (!type) {
738  std::cerr << "Unknown unit type : " << type_id << "\n";
739  // don't return an hyperlink (no page)
740  // instead show the id (as hint)
741  link = type_id;
742  } else if (!type->hide_help()) {
743  std::string name = type->type_name();
744  std::string ref_id;
745  if (description_type(*type) == FULL_DESCRIPTION) {
746  const std::string section_prefix = type->show_variations_in_help() ? ".." : "";
747  ref_id = section_prefix + unit_prefix + type->id();
748  } else {
749  ref_id = unknown_unit_topic;
750  name += " (?)";
751  }
752  link = make_link(name, ref_id);
753  } // if hide_help then link is an empty string
754 
755  return link;
756 }
757 
758 std::vector<std::string> make_unit_links_list(const std::vector<std::string>& type_id_list, bool ordered)
759 {
760  std::vector<std::string> links_list;
761  for (const std::string &type_id : type_id_list) {
762  std::string unit_link = make_unit_link(type_id);
763  if (!unit_link.empty())
764  links_list.push_back(unit_link);
765  }
766 
767  if (ordered)
768  std::sort(links_list.begin(), links_list.end());
769 
770  return links_list;
771 }
772 
773 void generate_races_sections(const config *help_cfg, section &sec, int level)
774 {
775  std::set<std::string, string_less> races;
776  std::set<std::string, string_less> visible_races;
777 
778  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
779  {
780  const unit_type &type = i.second;
781  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
782  if (desc_type == FULL_DESCRIPTION) {
783  races.insert(type.race_id());
784  if (!type.hide_help())
785  visible_races.insert(type.race_id());
786  }
787  }
788 
789  for(std::set<std::string, string_less>::iterator it = races.begin(); it != races.end(); ++it) {
790  section race_section;
791  config section_cfg;
792 
793  bool hidden = (visible_races.count(*it) == 0);
794 
795  section_cfg["id"] = hidden_symbol(hidden) + race_prefix + *it;
796 
797  std::string title;
798  if (const unit_race *r = unit_types.find_race(*it)) {
799  title = r->plural_name();
800  } else {
801  title = _ ("race^Miscellaneous");
802  }
803  section_cfg["title"] = title;
804 
805  section_cfg["sections_generator"] = "units:" + *it;
806  section_cfg["generator"] = "units:" + *it;
807 
808  parse_config_internal(help_cfg, &section_cfg, race_section, level+1);
809  sec.add_section(race_section);
810  }
811 }
812 
813 void generate_era_sections(const config* help_cfg, section & sec, int level)
814 {
815  for (const config & era : game_cfg->child_range("era")) {
816  if (era["hide_help"].to_bool()) {
817  continue;
818  }
819 
820  DBG_HP << "Adding help section: " << era["id"].str() << "\n";
821 
822  section era_section;
823  config section_cfg;
824  section_cfg["id"] = era_prefix + era["id"].str();
825  section_cfg["title"] = era["name"];
826 
827  section_cfg["generator"] = "era:" + era["id"].str();
828 
829  DBG_HP << section_cfg.debug() << "\n";
830 
831  parse_config_internal(help_cfg, &section_cfg, era_section, level+1);
832  sec.add_section(era_section);
833  }
834 }
835 
836 void generate_terrain_sections(section& sec, int /*level*/)
837 {
838  std::shared_ptr<terrain_type_data> tdata = load_terrain_types_data();
839 
840  if (!tdata) {
841  WRN_HP << "When building terrain help sections, couldn't acquire terrain types data, aborting.\n";
842  return;
843  }
844 
845  std::map<std::string, section> base_map;
846 
847  const t_translation::ter_list& t_listi = tdata->list();
848 
849  for (const t_translation::terrain_code& t : t_listi) {
850 
851  const terrain_type& info = tdata->get_terrain_info(t);
852 
853  bool hidden = info.hide_help();
854 
856  == preferences::encountered_terrains().end() && !info.is_overlay())
857  hidden = true;
858 
859  topic terrain_topic;
860  terrain_topic.title = info.editor_name();
861  terrain_topic.id = hidden_symbol(hidden) + terrain_prefix + info.id();
862  terrain_topic.text = std::make_shared<terrain_topic_generator>(info);
863 
864  t_translation::ter_list base_terrains = tdata->underlying_union_terrain(t);
865  for (const t_translation::terrain_code& base : base_terrains) {
866 
867  const terrain_type& base_info = tdata->get_terrain_info(base);
868 
869  if (!base_info.is_nonnull() || base_info.hide_help())
870  continue;
871 
872  section& base_section = base_map[base_info.id()];
873 
874  base_section.id = terrain_prefix + base_info.id();
875  base_section.title = base_info.editor_name();
876 
877  if (base_info.id() == info.id())
878  terrain_topic.id = ".." + terrain_prefix + info.id();
879  base_section.topics.push_back(terrain_topic);
880  }
881  }
882 
883  for (const auto& base : base_map) {
884  sec.add_section(base.second);
885  }
886 }
887 
888 void generate_unit_sections(const config* /*help_cfg*/, section& sec, int /*level*/, const bool /*sort_generated*/, const std::string& race)
889 {
890  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
891  const unit_type &type = i.second;
892 
893  if (type.race_id() != race)
894  continue;
895 
896  if (!type.show_variations_in_help())
897  continue;
898 
899  section base_unit;
900  for (const std::string &variation_id : type.variations()) {
901  // TODO: Do we apply encountered stuff to variations?
902  const unit_type &var_type = type.get_variation(variation_id);
903  const std::string topic_name = var_type.type_name() + "\n" + var_type.variation_name();
904  const std::string var_ref = hidden_symbol(var_type.hide_help()) + variation_prefix + var_type.id() + "_" + variation_id;
905 
906  topic var_topic(topic_name, var_ref, "");
907  var_topic.text = std::make_shared<unit_topic_generator>(var_type, variation_id);
908  base_unit.topics.push_back(var_topic);
909  }
910 
911  const std::string type_name = type.type_name();
912  const std::string ref_id = hidden_symbol(type.hide_help()) + unit_prefix + type.id();
913 
914  base_unit.id = ref_id;
915  base_unit.title = type_name;
916 
917  sec.add_section(base_unit);
918  }
919 }
920 
921 std::vector<topic> generate_unit_topics(const bool sort_generated, const std::string& race)
922 {
923  std::vector<topic> topics;
924  std::set<std::string, string_less> race_units;
925  std::set<std::string, string_less> race_topics;
926  std::set<std::string> alignments;
927 
928  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
929  {
930  const unit_type &type = i.second;
931 
932  if (type.race_id() != race)
933  continue;
934 
935  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
936  if (desc_type != FULL_DESCRIPTION)
937  continue;
938 
939  const std::string debug_suffix = (game_config::debug ? " (" + type.id() + ")" : "");
940  const std::string type_name = type.type_name() + (type.id() == type.type_name().str() ? "" : debug_suffix);
941  const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
942  const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
943  topic unit_topic(type_name, ref_id, "");
944  unit_topic.text = std::make_shared<unit_topic_generator>(type);
945  topics.push_back(unit_topic);
946 
947  if (!type.hide_help()) {
948  // we also record an hyperlink of this unit
949  // in the list used for the race topic
950  std::string link = make_link(type_name, ref_id);
951  race_units.insert(link);
952 
953  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
954  }
955  }
956 
957  //generate the hidden race description topic
958  std::string race_id = "..race_"+race;
959  std::string race_name;
960  std::string race_description;
961  std::string race_help_taxonomy;
962  if (const unit_race *r = unit_types.find_race(race)) {
963  race_name = r->plural_name();
964  race_description = r->description();
965  race_help_taxonomy = r->help_taxonomy();
966  // if (description.empty()) description = _("No description Available");
967  for (const config &additional_topic : r->additional_topics())
968  {
969  std::string id = additional_topic["id"];
970  std::string title = additional_topic["title"];
971  std::string text = additional_topic["text"];
972  //topic additional_topic(title, id, text);
973  topics.emplace_back(title,id,text);
974  std::string link = make_link(title, id);
975  race_topics.insert(link);
976  }
977  } else {
978  race_name = _ ("race^Miscellaneous");
979  // description = _("Here put the description of the Miscellaneous race");
980  }
981 
982  // Find any other races whose [race]help_taxonomy points to the current race
983  std::map<std::string, t_string> subgroups;
984  for (const auto &r : unit_types.races()) {
985  if (r.second.help_taxonomy() == race) {
986  if (!r.second.plural_name().empty())
987  subgroups[r.first] = r.second.plural_name();
988  else
989  subgroups[r.first] = r.first;
990  }
991  }
992 
993  std::stringstream text;
994 
995  if (!race_description.empty()) {
996  text << race_description << "\n\n";
997  }
998 
999  if (!alignments.empty()) {
1000  std::set<std::string>::iterator it = alignments.begin();
1001  text << (alignments.size() > 1 ? _("Alignments: ") : _("Alignment: ")) << *(it++);
1002  while(it != alignments.end()) {
1003  text << ", " << *(it++);
1004  }
1005  text << "\n\n";
1006  }
1007 
1008  if (!race_help_taxonomy.empty()) {
1009  utils::string_map symbols;
1010  symbols["topic_id"] = "..race_"+race_help_taxonomy;
1011  if (const unit_race *r = unit_types.find_race(race_help_taxonomy)) {
1012  symbols["help_taxonomy"] = r->plural_name();
1013  } else {
1014  // Fall back to using showing the race id for the race that we couldn't find.
1015  // Not great, but probably useful if UMC has a broken link.
1016  symbols["help_taxonomy"] = race_help_taxonomy;
1017  }
1018  // TRANSLATORS: this is expected to say "[Dunefolk are] a group of units, all of whom are Humans",
1019  // or "[Quenoth Elves are] a group of units, all of whom are Elves".
1020  text << VGETTEXT("This is a group of units, all of whom are <ref>dst='$topic_id' text='$help_taxonomy'</ref>", symbols) << "\n\n";
1021  }
1022 
1023  if (!subgroups.empty()) {
1024  if (!race_help_taxonomy.empty()) {
1025  text << _("<header>text='Subgroups of units within this group'</header>") << "\n";
1026  } else {
1027  text << _("<header>text='Groups of units within this race'</header>") << "\n";
1028  }
1029  for (const auto &sg : subgroups) {
1030  text << font::unicode_bullet << " " << make_link(sg.second, "..race_" + sg.first) << "\n";
1031  }
1032  text << "\n";
1033  }
1034 
1035  if (!race_help_taxonomy.empty()) {
1036  text << _("<header>text='Units of this group'</header>") << "\n";
1037  } else {
1038  text << _("<header>text='Units of this race'</header>") << "\n";
1039  }
1040  for (const auto &u : race_units) {
1041  text << font::unicode_bullet << " " << u << "\n";
1042  }
1043 
1044  topics.emplace_back(race_name, race_id, text.str());
1045 
1046  if (sort_generated)
1047  std::sort(topics.begin(), topics.end(), title_less());
1048 
1049  return topics;
1050 }
1051 
1053 {
1056  return FULL_DESCRIPTION;
1057  }
1058 
1059  const std::set<std::string> &encountered_units = preferences::encountered_units();
1060  if (encountered_units.find(type.id()) != encountered_units.end()) {
1061  return FULL_DESCRIPTION;
1062  }
1063 
1064  // See the docs of HIDDEN_BUT_SHOW_MACROS
1065  if (type.id() == "Fog Clearer") {
1066  return HIDDEN_BUT_SHOW_MACROS;
1067  }
1068 
1069  return NO_DESCRIPTION;
1070 }
1071 
1072 std::string generate_contents_links(const std::string& section_name, config const *help_cfg)
1073 {
1074  const config& section_cfg = help_cfg->find_child("section", "id", section_name);
1075  if (!section_cfg) {
1076  return std::string();
1077  }
1078 
1079  std::ostringstream res;
1080 
1081  std::vector<std::string> topics = utils::quoted_split(section_cfg["topics"]);
1082 
1083  // we use an intermediate structure to allow a conditional sorting
1084  typedef std::pair<std::string,std::string> link;
1085  std::vector<link> topics_links;
1086 
1088  // Find all topics in this section.
1089  for (t = topics.begin(); t != topics.end(); ++t) {
1090  if (const config& topic_cfg = help_cfg->find_child("topic", "id", *t)) {
1091  std::string id = topic_cfg["id"];
1092  if (is_visible_id(id))
1093  topics_links.emplace_back(topic_cfg["title"], id);
1094  }
1095  }
1096 
1097  if (section_cfg["sort_topics"] == "yes") {
1098  std::sort(topics_links.begin(),topics_links.end());
1099  }
1100 
1102  for (l = topics_links.begin(); l != topics_links.end(); ++l) {
1103  std::string link = make_link(l->first, l->second);
1104  res << font::unicode_bullet << " " << link << "\n";
1105  }
1106 
1107  return res.str();
1108 }
1109 
1110 std::string generate_contents_links(const section &sec, const std::vector<topic>& topics)
1111 {
1112  std::stringstream res;
1113 
1114  for (auto &s : sec.sections) {
1115  if (is_visible_id(s.id)) {
1116  std::string link = make_link(s.title, ".."+s.id);
1117  res << font::unicode_bullet << " " << link << "\n";
1118  }
1119  }
1120 
1121  for (auto &t : topics) {
1122  if (is_visible_id(t.id)) {
1123  std::string link = make_link(t.title, t.id);
1124  res << font::unicode_bullet << " " << link << "\n";
1125  }
1126  }
1127  return res.str();
1128 }
1129 
1130 bool topic::operator==(const topic &t) const
1131 {
1132  return t.id == id;
1133 }
1134 
1135 bool topic::operator<(const topic &t) const
1136 {
1137  return id < t.id;
1138 }
1139 
1140 bool section::operator==(const section &sec) const
1141 {
1142  return sec.id == id;
1143 }
1144 
1145 bool section::operator<(const section &sec) const
1146 {
1147  return id < sec.id;
1148 }
1149 
1151 {
1152  sections.emplace_back(s);
1153 }
1154 
1156 {
1157  topics.clear();
1158  sections.clear();
1159 }
1160 
1161 const topic *find_topic(const section &sec, const std::string &id)
1162 {
1163  topic_list::const_iterator tit =
1164  std::find_if(sec.topics.begin(), sec.topics.end(), has_id(id));
1165  if (tit != sec.topics.end()) {
1166  return &(*tit);
1167  }
1168  for (const auto &s : sec.sections) {
1169  const auto t = find_topic(s, id);
1170  if (t != nullptr) {
1171  return t;
1172  }
1173  }
1174  return nullptr;
1175 }
1176 
1177 const section *find_section(const section &sec, const std::string &id)
1178 {
1179  const auto &sit =
1180  std::find_if(sec.sections.begin(), sec.sections.end(), has_id(id));
1181  if (sit != sec.sections.end()) {
1182  return &*sit;
1183  }
1184  for (const auto &subsection : sec.sections) {
1185  const auto s = find_section(subsection, id);
1186  if (s != nullptr) {
1187  return s;
1188  }
1189  }
1190  return nullptr;
1191 }
1192 
1193 section *find_section(section &sec, const std::string &id)
1194 {
1195  return const_cast<section *>(find_section(const_cast<const section &>(sec), id));
1196 }
1197 
1198 std::vector<std::string> parse_text(const std::string &text)
1199 {
1200  std::vector<std::string> res;
1201  bool last_char_escape = false;
1202  const char escape_char = '\\';
1203  std::stringstream ss;
1204  std::size_t pos;
1205  enum { ELEMENT_NAME, OTHER } state = OTHER;
1206  for (pos = 0; pos < text.size(); ++pos) {
1207  const char c = text[pos];
1208  if (c == escape_char && !last_char_escape) {
1209  last_char_escape = true;
1210  }
1211  else {
1212  if (state == OTHER) {
1213  if (c == '<') {
1214  if (last_char_escape) {
1215  ss << c;
1216  }
1217  else {
1218  res.push_back(ss.str());
1219  ss.str("");
1220  state = ELEMENT_NAME;
1221  }
1222  }
1223  else {
1224  ss << c;
1225  }
1226  }
1227  else if (state == ELEMENT_NAME) {
1228  if (c == '/') {
1229  std::string msg = "Erroneous / in element name.";
1230  throw parse_error(msg);
1231  }
1232  else if (c == '>') {
1233  // End of this name.
1234  std::stringstream s;
1235  const std::string element_name = ss.str();
1236  ss.str("");
1237  s << "</" << element_name << ">";
1238  const std::string end_element_name = s.str();
1239  std::size_t end_pos = text.find(end_element_name, pos);
1240  if (end_pos == std::string::npos) {
1241  std::stringstream msg;
1242  msg << "Unterminated element: " << element_name;
1243  throw parse_error(msg.str());
1244  }
1245  s.str("");
1246  const std::string contents = text.substr(pos + 1, end_pos - pos - 1);
1247  const std::string element = convert_to_wml(element_name, contents);
1248  res.push_back(element);
1249  pos = end_pos + end_element_name.size() - 1;
1250  state = OTHER;
1251  }
1252  else {
1253  ss << c;
1254  }
1255  }
1256  last_char_escape = false;
1257  }
1258  }
1259  if (state == ELEMENT_NAME) {
1260  std::stringstream msg;
1261  msg << "Element '" << ss.str() << "' continues through end of string.";
1262  throw parse_error(msg.str());
1263  }
1264  if (!ss.str().empty()) {
1265  // Add the last string.
1266  res.push_back(ss.str());
1267  }
1268  return res;
1269 }
1270 
1271 std::string convert_to_wml(const std::string &element_name, const std::string &contents)
1272 {
1273  std::stringstream ss;
1274  bool in_quotes = false;
1275  bool last_char_escape = false;
1276  const char escape_char = '\\';
1277  std::vector<std::string> attributes;
1278  // Find the different attributes.
1279  // No checks are made for the equal sign or something like that.
1280  // Attributes are just separated by spaces or newlines.
1281  // Attributes that contain spaces must be in single quotes.
1282  for (std::size_t pos = 0; pos < contents.size(); ++pos) {
1283  const char c = contents[pos];
1284  if (c == escape_char && !last_char_escape) {
1285  last_char_escape = true;
1286  }
1287  else {
1288  if (c == '\'' && !last_char_escape) {
1289  ss << '"';
1290  in_quotes = !in_quotes;
1291  }
1292  else if ((c == ' ' || c == '\n') && !last_char_escape && !in_quotes) {
1293  // Space or newline, end of attribute.
1294  attributes.push_back(ss.str());
1295  ss.str("");
1296  }
1297  else {
1298  ss << c;
1299  }
1300  last_char_escape = false;
1301  }
1302  }
1303  if (in_quotes) {
1304  std::stringstream msg;
1305  msg << "Unterminated single quote after: '" << ss.str() << "'";
1306  throw parse_error(msg.str());
1307  }
1308  if (!ss.str().empty()) {
1309  attributes.push_back(ss.str());
1310  }
1311  ss.str("");
1312  // Create the WML.
1313  ss << "[" << element_name << "]\n";
1314  for (std::vector<std::string>::const_iterator it = attributes.begin();
1315  it != attributes.end(); ++it) {
1316  ss << *it << "\n";
1317  }
1318  ss << "[/" << element_name << "]\n";
1319  return ss.str();
1320 }
1321 
1322 color_t string_to_color(const std::string &cmp_str)
1323 {
1324  if (cmp_str == "green") {
1325  return font::GOOD_COLOR;
1326  }
1327  if (cmp_str == "red") {
1328  return font::BAD_COLOR;
1329  }
1330  if (cmp_str == "black") {
1331  return font::BLACK_COLOR;
1332  }
1333  if (cmp_str == "yellow") {
1334  return font::YELLOW_COLOR;
1335  }
1336  if (cmp_str == "white") {
1337  return font::BIGMAP_COLOR;
1338  }
1339  // a #rrggbb color in pango format.
1340  if (*cmp_str.c_str() == '#' && cmp_str.size() == 7) {
1341  return color_t::from_argb_bytes(strtoul(cmp_str.c_str() + 1, nullptr, 16));
1342  }
1343  return font::NORMAL_COLOR;
1344 }
1345 
1346 std::vector<std::string> split_in_width(const std::string &s, const int font_size,
1347  const unsigned width)
1348 {
1349  std::vector<std::string> res;
1350  try {
1351  const std::string& first_line = font::word_wrap_text(s, font_size, width, -1, 1, true);
1352  res.push_back(first_line);
1353  if(s.size() > first_line.size()) {
1354  res.push_back(s.substr(first_line.size()));
1355  }
1356  }
1358  {
1359  throw parse_error (_("corrupted original file"));
1360  }
1361 
1362  return res;
1363 }
1364 
1365 std::string remove_first_space(const std::string& text)
1366 {
1367  if (text.length() > 0 && text[0] == ' ') {
1368  return text.substr(1);
1369  }
1370  return text;
1371 }
1372 
1373 std::string get_first_word(const std::string &s)
1374 {
1375  std::size_t first_word_start = s.find_first_not_of(' ');
1376  if (first_word_start == std::string::npos) {
1377  return s;
1378  }
1379  std::size_t first_word_end = s.find_first_of(" \n", first_word_start);
1380  if( first_word_end == first_word_start ) {
1381  // This word is '\n'.
1382  first_word_end = first_word_start+1;
1383  }
1384 
1385  //if no gap(' ' or '\n') found, test if it is CJK character
1386  std::string re = s.substr(0, first_word_end);
1387 
1388  utf8::iterator ch(re);
1389  if (ch == utf8::iterator::end(re))
1390  return re;
1391 
1392  char32_t firstchar = *ch;
1393  if (font::is_cjk_char(firstchar)) {
1394  re = unicode_cast<std::string>(firstchar);
1395  }
1396  return re;
1397 }
1398 
1400 {
1401  default_toplevel.clear();
1402  hidden_sections.clear();
1403  if (game_cfg != nullptr) {
1404  const config *help_config = &game_cfg->child_or_empty("help");
1405  try {
1406  default_toplevel = parse_config(help_config);
1407  // Create a config object that contains everything that is
1408  // not referenced from the toplevel element. Read this
1409  // config and save these sections and topics so that they
1410  // can be referenced later on when showing help about
1411  // specified things, but that should not be shown when
1412  // opening the help browser in the default manner.
1413  config hidden_toplevel;
1414  std::stringstream ss;
1415  for (const config &section : help_config->child_range("section"))
1416  {
1417  const std::string id = section["id"];
1418  if (find_section(default_toplevel, id) == nullptr) {
1419  // This section does not exist referenced from the
1420  // toplevel. Hence, add it to the hidden ones if it
1421  // is not referenced from another section.
1422  if (!section_is_referenced(id, *help_config)) {
1423  if (!ss.str().empty()) {
1424  ss << ",";
1425  }
1426  ss << id;
1427  }
1428  }
1429  }
1430  hidden_toplevel["sections"] = ss.str();
1431  ss.str("");
1432  for (const config &topic : help_config->child_range("topic"))
1433  {
1434  const std::string id = topic["id"];
1435  if (find_topic(default_toplevel, id) == nullptr) {
1436  if (!topic_is_referenced(id, *help_config)) {
1437  if (!ss.str().empty()) {
1438  ss << ",";
1439  }
1440  ss << id;
1441  }
1442  }
1443  }
1444  hidden_toplevel["topics"] = ss.str();
1445  config hidden_cfg = *help_config;
1446  // Change the toplevel to our new, custom built one.
1447  hidden_cfg.clear_children("toplevel");
1448  hidden_cfg.add_child("toplevel", std::move(hidden_toplevel));
1449  hidden_sections = parse_config(&hidden_cfg);
1450  }
1451  catch (parse_error& e) {
1452  std::stringstream msg;
1453  msg << "Parse error when parsing help text: '" << e.message << "'";
1454  std::cerr << msg.str() << std::endl;
1455  }
1456  }
1457 }
1458 
1459 // id starting with '.' are hidden
1460 std::string hidden_symbol(bool hidden) {
1461  return (hidden ? "." : "");
1462 }
1463 
1464 bool is_visible_id(const std::string &id) {
1465  return (id.empty() || id[0] != '.');
1466 }
1467 
1468 /**
1469  * Return true if the id is valid for user defined topics and
1470  * sections. Some IDs are special, such as toplevel and may not be
1471  * be defined in the config.
1472  */
1473 bool is_valid_id(const std::string &id) {
1474  if (id == "toplevel") {
1475  return false;
1476  }
1477  if (id.compare(0, unit_prefix.length(), unit_prefix) == 0 || id.compare(hidden_symbol().length(), unit_prefix.length(), unit_prefix) == 0) {
1478  return false;
1479  }
1480  if (id.compare(0, 8, "ability_") == 0) {
1481  return false;
1482  }
1483  if (id.compare(0, 14, "weaponspecial_") == 0) {
1484  return false;
1485  }
1486  if (id == "hidden") {
1487  return false;
1488  }
1489  return true;
1490 }
1491 
1492 
1493 // Return the width for the image with filename.
1494 unsigned image_width(const std::string &filename)
1495 {
1496  image::locator loc(filename);
1497  surface surf(image::get_image(loc));
1498  if (surf != nullptr) {
1499  return surf->w;
1500  }
1501  return 0;
1502 }
1503 
1504 void push_tab_pair(std::vector<help::item> &v, const std::string &s, const std::optional<std::string> &image, unsigned padding)
1505 {
1506  help::item item(s, font::line_width(s, normal_font_size));
1507  if (image) {
1508  // If the image doesn't exist, don't add padding.
1509  auto width = image_width(*image);
1510  padding = (width ? padding : 0);
1511 
1512  item.first = "<img>src='" + *image + "'</img>" + (padding ? jump(padding) : "") + s;
1513  item.second += width + padding;
1514  }
1515  v.emplace_back(item);
1516 }
1517 
1518 std::string generate_table(const table_spec &tab, const unsigned int spacing)
1519 {
1520  table_spec::const_iterator row_it;
1521  std::vector<std::pair<std::string, unsigned>>::const_iterator col_it;
1522  unsigned int num_cols = 0;
1523  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1524  if (row_it->size() > num_cols) {
1525  num_cols = row_it->size();
1526  }
1527  }
1528  std::vector<unsigned int> col_widths(num_cols, 0);
1529  // Calculate the width of all columns, including spacing.
1530  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1531  unsigned int col = 0;
1532  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1533  if (col_widths[col] < col_it->second + spacing) {
1534  col_widths[col] = col_it->second + spacing;
1535  }
1536  ++col;
1537  }
1538  }
1539  std::vector<unsigned int> col_starts(num_cols);
1540  // Calculate the starting positions of all columns
1541  for (unsigned int i = 0; i < num_cols; ++i) {
1542  unsigned int this_col_start = 0;
1543  for (unsigned int j = 0; j < i; ++j) {
1544  this_col_start += col_widths[j];
1545  }
1546  col_starts[i] = this_col_start;
1547  }
1548  std::stringstream ss;
1549  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1550  unsigned int col = 0;
1551  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1552  ss << jump_to(col_starts[col]) << col_it->first;
1553  ++col;
1554  }
1555  ss << "\n";
1556  }
1557  return ss.str();
1558 }
1559 
1560 /** Prepend all chars with meaning inside attributes with a backslash. */
1561 std::string escape(const std::string &s)
1562 {
1563  return utils::escape(s, "'\\");
1564 }
1565 
1566 /** Load the appropriate terrain types data to use */
1567 std::shared_ptr<terrain_type_data> load_terrain_types_data()
1568 {
1569  if (display::get_singleton()) {
1571  } else if (game_config_manager::get()){
1573  } else {
1574  return {};
1575  }
1576 }
1577 
1578 
1579 } // end namespace help
std::string jump_to(const unsigned pos)
Definition: help_impl.hpp:385
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
bool empty() const
Tests for an attribute that either was never set or was set to "".
const std::string ability_prefix
Definition: help_impl.cpp:95
section parse_config(const config *cfg)
Parse a help config, return the top level section.
Definition: help_impl.cpp:250
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
std::string id
Definition: help_impl.hpp:161
::tod_manager * tod_manager
Definition: resources.cpp:29
Ignore this unit for documentation purposes.
Definition: help_impl.hpp:239
std::string make_unit_link(const std::string &type_id)
return a hyperlink with the unit&#39;s name and pointing to the unit page return empty string if this uni...
Definition: help_impl.cpp:732
const std::string open_section_img
Definition: help_impl.cpp:85
int get_max_liminal_bonus() const
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:90
std::vector< topic > generate_trait_topics(const bool sort_generated)
Definition: help_impl.cpp:669
const std::string unit_prefix
Definition: help_impl.cpp:89
void push_tab_pair(std::vector< help::item > &v, const std::string &s, const std::optional< std::string > &image, unsigned padding)
Definition: help_impl.cpp:1504
std::vector< topic > generate_unit_topics(const bool sort_generated, const std::string &race)
Definition: help_impl.cpp:921
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1222
std::map< std::string, t_string > string_map
const std::string era_prefix
Definition: help_impl.cpp:93
const std::string topic_img
Definition: help_impl.cpp:83
void clear_children(T... keys)
Definition: config.hpp:490
std::string era()
Definition: game.cpp:689
const int title_size
Definition: help_impl.cpp:78
bool is_overlay() const
Definition: terrain.hpp:154
config_array_view traits() const
Definition: types.hpp:391
const std::string closed_section_img
Definition: help_impl.cpp:84
std::string remove_first_space(const std::string &text)
Definition: help_impl.cpp:1365
bool is_scope_active(scope s)
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:143
Various functions that implement attacks and attack calculations.
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:836
const std::string unknown_unit_topic
Definition: help_impl.cpp:88
Variant for storing WML attributes.
const std::string race_prefix
Definition: help_impl.cpp:91
const color_t GOOD_COLOR
std::string generate_topic_text(const std::string &generator, const config *help_cfg, const section &sec, const std::vector< topic > &generated_topics)
Definition: help_impl.cpp:308
logger & info()
Definition: log.cpp:91
#define a
config_array_view child_range(config_key_type key) const
#define WRN_HP
Definition: help_impl.cpp:61
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
Definition: gettext.cpp:458
boost::tribool last_debug_state
Definition: help_impl.cpp:74
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)
child_itors child_range(config_key_type key)
Definition: config.cpp:362
REMOVE_EMPTY: remove empty elements.
Thrown when the help system fails to parse something.
Definition: help_impl.hpp:206
bool operator<(const section &) const
Comparison on the ID.
Definition: help_impl.cpp:1145
std::vector< std::string > empty_string_vector
Definition: help_impl.cpp:76
Although the unit itself is hidden, traits reachable via this unit are not hidden.
Definition: help_impl.hpp:248
std::string word_wrap_text(const std::string &unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool partial_line)
Wrap text.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:48
unit_type_data unit_types
Definition: types.cpp:1441
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
int generic_combat_modifier(int lawful_bonus, unit_type::ALIGNMENT alignment, bool is_fearless, int max_liminal_bonus)
Returns the amount that a unit&#39;s damage should be multiplied by due to a given lawful_bonus.
Definition: attack.cpp:1615
const race_map & races() const
Definition: types.hpp:390
const int normal_font_size
Definition: help_impl.cpp:81
unsigned image_width(const std::string &filename)
Definition: help_impl.cpp:1494
static std::string _(const char *str)
Definition: gettext.hpp:92
const std::string terrain_prefix
Definition: help_impl.cpp:90
int last_num_encountered_terrains
Definition: help_impl.cpp:73
color_t string_to_color(const std::string &cmp_str)
Return the color the string represents.
Definition: help_impl.cpp:1322
const unit_type_map & types() const
Definition: types.hpp:389
bool is_visible_id(const std::string &id)
Definition: help_impl.cpp:1464
virtual const gamemap & map() const =0
bool hide_help() const
For instances created from a [terrain_type] tag, the value in the tag (with default false)...
Definition: terrain.hpp:60
std::vector< topic > generate_weapon_special_topics(const bool sort_generated)
Definition: help_impl.cpp:395
std::vector< std::string > make_unit_links_list(const std::vector< std::string > &type_id_list, bool ordered)
return a list of hyperlinks to unit&#39;s pages (ordered or not)
Definition: help_impl.cpp:758
A single unit type that the player may recruit.
Definition: types.hpp:44
bool is_nonnull() const
True if this object represents some sentinel values.
Definition: terrain.hpp:128
void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level)
Dispatch generators to their appropriate functions.
Definition: help_impl.cpp:289
const int SIZE_NORMAL
Definition: constants.cpp:19
help::section hidden_sections
Definition: help_impl.cpp:70
const std::shared_ptr< terrain_type_data > & terrain_types() const
section_list sections
Definition: help_impl.hpp:163
const section * find_section(const section &sec, const std::string &id)
Search for the section with the specified identifier in the section and its subsections.
Definition: help_impl.cpp:1177
std::string id
Definition: help_impl.hpp:134
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:55
To be used as a function object to locate sections and topics with a specified ID.
Definition: help_impl.hpp:170
static game_config_manager * get()
const t_string & editor_name() const
Definition: terrain.hpp:48
const color_t BIGMAP_COLOR
The text displayed in a topic.
Definition: help_impl.hpp:79
std::string generate_table(const table_spec &tab, const unsigned int spacing)
Definition: help_impl.cpp:1518
const std::vector< unit_race::GENDER > & genders() const
The returned vector will not be empty, provided this has been built to the HELP_INDEXED status...
Definition: types.hpp:244
std::vector< std::vector< help::item > > table_spec
Definition: help_impl.hpp:409
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1460
bool hide_help() const
Definition: types.cpp:593
std::shared_ptr< terrain_type_data > load_terrain_types_data()
Load the appropriate terrain types data to use.
Definition: help_impl.cpp:1567
std::string title
Definition: help_impl.hpp:161
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:140
const int SIZE_15
Definition: constants.cpp:25
std::vector< topic > generate_era_topics(const bool sort_generated, const std::string &era_id)
Definition: help_impl.cpp:558
UNIT_DESCRIPTION_TYPE
Definition: help_impl.hpp:236
std::vector< topic > generate_faction_topics(const config &era, const bool sort_generated)
Definition: help_impl.cpp:594
std::string time_of_day_bonus_colored(const int time_of_day_bonus)
Definition: help_impl.cpp:342
std::set< t_translation::terrain_code > & encountered_terrains()
Definition: game.cpp:929
const std::vector< ability_metadata > & abilities_metadata() const
Definition: types.hpp:212
std::string race_id() const
Returns the ID of this type&#39;s race without the need to build the type.
Definition: types.hpp:267
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:143
map_display and display: classes which take care of displaying the map and game-data on the screen...
std::vector< std::string > parsed_text_
Definition: help_impl.hpp:81
const std::shared_ptr< terrain_type_data > & tdata() const
Definition: map.hpp:66
static lg::log_domain log_help("help")
const color_t YELLOW_COLOR
bool show_all_units_in_help()
Definition: game.cpp:914
const config & child_or_empty(config_key_type key) const
const color_t NORMAL_COLOR
std::shared_ptr< topic_generator > generator_
Definition: help_impl.hpp:82
Generic locator abstracting the location of an image.
Definition: picture.hpp:59
void parse_config_internal(const config *help_cfg, const config *section_cfg, section &sec, int level)
Recursive function used by parse_config.
Definition: help_impl.cpp:145
static color_t from_argb_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:87
const int box_width
Definition: help_impl.cpp:80
std::string escape(const std::string &str, const char *special_chars)
Prepends a configurable set of characters with a backslash.
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:205
bool section_is_referenced(const std::string &section_id, const config &cfg)
Return true if the section with id section_id is referenced from another section in the config...
Definition: help_impl.cpp:97
bool topic_is_referenced(const std::string &topic_id, const config &cfg)
Return true if the topic with id topic_id is referenced from another section in the config...
Definition: help_impl.cpp:121
void generate_unit_sections(const config *, section &sec, int, const bool, const std::string &race)
Definition: help_impl.cpp:888
To be used as a function object when sorting section lists on the title.
Definition: help_impl.hpp:190
config::const_child_itors modification_advancements() const
Returns two iterators pointing to a range of AMLA configs.
Definition: types.hpp:122
const unit_race * find_race(const std::string &) const
Definition: types.cpp:1328
const color_t BLACK_COLOR
std::size_t i
Definition: function.cpp:933
const std::string variation_prefix
Definition: help_impl.cpp:94
std::vector< std::string > variations() const
Definition: types.cpp:714
Thrown by operations encountering invalid UTF-8 data.
void generate_terrain_sections(section &sec, int)
Definition: help_impl.cpp:836
int last_num_encountered_units
Definition: help_impl.cpp:72
std::string make_link(const std::string &text, const std::string &dst)
Definition: help_impl.hpp:379
std::string convert_to_wml(const std::string &element_name, const std::string &contents)
Convert the contents to wml attributes, surrounded within [element_name]...[/element_name].
Definition: help_impl.cpp:1271
static map_location::DIRECTION s
bool show_variations_in_help() const
Whether the unit type has at least one help-visible variation.
Definition: types.cpp:731
const std::vector< time_of_day > & times(const map_location &loc=map_location::null_location()) const
std::vector< std::string > quoted_split(const std::string &val, char c, int flags, char quote)
This function is identical to split(), except it does not split when it otherwise would if the previo...
double g
Definition: astarsearch.cpp:64
std::string name
Definition: sdl_ttf.cpp:70
const display_context & get_disp_context() const
Definition: display.hpp:170
const game_config_view * game_cfg
Definition: help_impl.cpp:66
std::set< std::string > & encountered_units()
Definition: game.cpp:924
const bool & debug
bool operator<(const topic &) const
Comparison on the ID.
Definition: help_impl.cpp:1135
void generate_races_sections(const config *help_cfg, section &sec, int level)
Definition: help_impl.cpp:773
const std::string unicode_bullet
Definition: constants.cpp:43
static int sort(lua_State *L)
Definition: ltablib.cpp:411
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static iterator_base end(const string_type &str)
const std::vector< ability_metadata > & adv_abilities_metadata() const
Some extra abilities that may be gained through AMLA advancements.
Definition: types.hpp:215
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
std::string escape(const std::string &s)
Prepend all chars with meaning inside attributes with a backslash.
Definition: help_impl.cpp:1561
std::vector< std::string > parse_text(const std::string &text)
Parse a text string.
Definition: help_impl.cpp:1198
config & add_child(config_key_type key)
Definition: config.cpp:476
const config & find_child(config_key_type key, const std::string &name, const std::string &value) const
const unsigned max_history
Definition: help_impl.cpp:82
const int SIZE_LARGE
Definition: constants.cpp:27
std::vector< topic > generate_time_of_day_topics(const bool)
Definition: help_impl.cpp:348
config::const_child_itors possible_traits() const
Definition: types.hpp:226
static lg::log_domain log_display("display")
#define DBG_HP
Definition: help_impl.cpp:62
std::string generate_contents_links(const std::string &section_name, config const *help_cfg)
Definition: help_impl.cpp:1072
const std::string faction_prefix
Definition: help_impl.cpp:92
std::string jump(const unsigned amount)
Definition: help_impl.hpp:392
#define f
double t
Definition: astarsearch.cpp:64
const std::string default_show_topic
Definition: help_impl.cpp:87
const unit_type & get_variation(const std::string &id) const
Definition: types.cpp:475
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1399
std::vector< std::string > split(const config_attribute_value &val)
std::vector< topic > generate_topics(const bool sort_generated, const std::string &generator)
Definition: help_impl.cpp:260
bool operator==(const topic &) const
Two topics are equal if their IDs are equal.
Definition: help_impl.cpp:1130
const topic * find_topic(const section &sec, const std::string &id)
Search for the topic with the specified identifier in the section and its subsections.
Definition: help_impl.cpp:1161
const t_string & variation_name() const
Definition: types.hpp:168
const std::vector< std::string > & parsed_text() const
Definition: help_impl.cpp:332
Functions to load and save images from/to disk.
Standard logging facilities (interface).
const std::string & id() const
Definition: terrain.hpp:51
std::vector< terrain_code > ter_list
Definition: translation.hpp:76
std::string message
Definition: exceptions.hpp:31
const int title2_size
Definition: help_impl.cpp:79
A topic contains a title, an id and some text.
Definition: help_impl.hpp:109
#define e
Definition: help.cpp:55
const_attack_itors attacks() const
Definition: types.cpp:512
std::string title
Definition: help_impl.hpp:134
bool is_valid_id(const std::string &id)
Return true if the id is valid for user defined topics and sections.
Definition: help_impl.cpp:1473
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
static std::string alignment_description(ALIGNMENT align, unit_race::GENDER gender=unit_race::MALE)
mock_char c
const std::string & str() const
Definition: tstring.hpp:186
std::string get_first_word(const std::string &s)
Return the first word in s, not removing any spaces in the start of it.
Definition: help_impl.cpp:1373
ALIGNMENT alignment() const
Definition: types.hpp:188
const int font_size
Definition: button.cpp:40
bool is_cjk_char(const char32_t ch)
Determine if a char32_t is a CJK character.
void add_section(const section &s)
Allocate memory for and add the section.
Definition: help_impl.cpp:1150
UNIT_DESCRIPTION_TYPE description_type(const unit_type &type)
Return the type of description that should be shown for a unit of the given kind. ...
Definition: help_impl.cpp:1052
std::vector< topic > generate_ability_topics(const bool sort_generated)
Definition: help_impl.cpp:492
void generate_era_sections(const config *help_cfg, section &sec, int level)
Definition: help_impl.cpp:813
Defines the MAKE_ENUM macro.
int line_width(const std::string &line, int font_size, int style)
Determine the width of a line of text given a certain font size.
Definition: sdl_ttf.cpp:378
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
const int max_section_level
Definition: help_impl.cpp:77
topic_text & operator=(topic_text &&t)=default
To be used as a function object when sorting topic lists on the title.
Definition: help_impl.hpp:182
std::vector< std::string > split_in_width(const std::string &s, const int font_size, const unsigned width)
Make a best effort to word wrap s.
Definition: help_impl.cpp:1346
std::string debug() const
Definition: config.cpp:1290
topic_list topics
Definition: help_impl.hpp:162
topic_text text
Definition: help_impl.hpp:135
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:407
const color_t BAD_COLOR
bool operator==(const section &) const
Two sections are equal if their IDs are equal.
Definition: help_impl.cpp:1140
help::section default_toplevel
Definition: help_impl.cpp:68