The Battle for Wesnoth  1.15.3+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 "game_config.hpp" // for debug, menu_contract, etc
22 #include "game_config_manager.hpp" // for game_config_manager
23 #include "preferences/game.hpp" // for encountered_terrains, etc
24 #include "gettext.hpp" // for _, gettext, N_
26 #include "hotkey/hotkey_command.hpp" // for is_scope_active, etc
27 #include "picture.hpp" // for get_image, locator
28 #include "log.hpp" // for LOG_STREAM, logger, etc
29 #include "utils/make_enum.hpp" // for operator<<
30 #include "map/map.hpp" // for gamemap
31 #include "font/marked-up_text.hpp" // for is_cjk_char, word_wrap_text
32 #include "font/standard_colors.hpp" // for NORMAL_COLOR
33 #include "units/race.hpp" // for unit_race, etc
34 #include "resources.hpp" // for tod_manager, config_manager
35 #include "sdl/surface.hpp" // for surface
36 #include "serialization/string_utils.hpp" // for split, quoted_split, etc
37 #include "serialization/unicode_cast.hpp" // for unicode_cast
38 #include "serialization/utf8_exception.hpp" // for char_t, etc
39 #include "terrain/terrain.hpp" // for terrain_type
40 #include "terrain/translation.hpp" // for operator==, ter_list, etc
41 #include "terrain/type_data.hpp" // for terrain_type_data, etc
42 #include "time_of_day.hpp" // for time_of_day
43 #include "tod_manager.hpp" // for tod_manager
44 #include "tstring.hpp" // for t_string, operator<<
45 #include "units/types.hpp" // for unit_type, unit_type_data, etc
46 #include "serialization/unicode.hpp" // for iterator
47 #include "color.hpp"
48 
49 #include <cassert> // for assert
50 #include <algorithm> // for sort, find, transform, etc
51 #include <iostream> // for operator<<, basic_ostream, etc
52 #include <iterator> // for back_insert_iterator, etc
53 #include <map> // for map, etc
54 #include <set>
55 #include <SDL2/SDL.h>
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  sec.level = level;
155  std::string id = level == 0 ? "toplevel" : (*section_cfg)["id"].str();
156  if (level != 0) {
157  if (!is_valid_id(id)) {
158  std::stringstream ss;
159  ss << "Invalid ID, used for internal purpose: '" << id << "'";
160  throw parse_error(ss.str());
161  }
162  }
163  std::string title = level == 0 ? "" : (*section_cfg)["title"].str();
164  sec.id = id;
165  sec.title = title;
166  std::vector<std::string>::const_iterator it;
167  // Find all child sections.
168  for (it = sections.begin(); it != sections.end(); ++it) {
169  if (const config &child_cfg = help_cfg->find_child("section", "id", *it))
170  {
171  section child_section;
172  parse_config_internal(help_cfg, &child_cfg, child_section, level + 1);
173  sec.add_section(child_section);
174  }
175  else {
176  std::stringstream ss;
177  ss << "Help-section '" << *it << "' referenced from '"
178  << id << "' but could not be found.";
179  throw parse_error(ss.str());
180  }
181  }
182 
183  generate_sections(help_cfg, (*section_cfg)["sections_generator"], sec, level);
184  if ((*section_cfg)["sort_sections"] == "yes") {
185  sec.sections.sort(section_less());
186  }
187 
188  bool sort_topics = false;
189  bool sort_generated = true;
190 
191  if ((*section_cfg)["sort_topics"] == "yes") {
192  sort_topics = true;
193  sort_generated = false;
194  } else if ((*section_cfg)["sort_topics"] == "no") {
195  sort_topics = false;
196  sort_generated = false;
197  } else if ((*section_cfg)["sort_topics"] == "generated") {
198  sort_topics = false;
199  sort_generated = true;
200  } else if (!(*section_cfg)["sort_topics"].empty()) {
201  std::stringstream ss;
202  ss << "Invalid sort option: '" << (*section_cfg)["sort_topics"] << "'";
203  throw parse_error(ss.str());
204  }
205 
206  std::vector<topic> generated_topics =
207  generate_topics(sort_generated,(*section_cfg)["generator"]);
208 
209  const std::vector<std::string> topics_id = utils::quoted_split((*section_cfg)["topics"]);
210  std::vector<topic> topics;
211 
212  // Find all topics in this section.
213  for (it = topics_id.begin(); it != topics_id.end(); ++it) {
214  if (const config &topic_cfg = help_cfg->find_child("topic", "id", *it))
215  {
216  std::string text = topic_cfg["text"];
217  text += generate_topic_text(topic_cfg["generator"], help_cfg, sec, generated_topics);
218  topic child_topic(topic_cfg["title"], topic_cfg["id"], text);
219  if (!is_valid_id(child_topic.id)) {
220  std::stringstream ss;
221  ss << "Invalid ID, used for internal purpose: '" << id << "'";
222  throw parse_error(ss.str());
223  }
224  topics.push_back(child_topic);
225  }
226  else {
227  std::stringstream ss;
228  ss << "Help-topic '" << *it << "' referenced from '" << id
229  << "' but could not be found." << std::endl;
230  throw parse_error(ss.str());
231  }
232  }
233 
234  if (sort_topics) {
235  std::sort(topics.begin(),topics.end(), title_less());
236  std::sort(generated_topics.begin(),
237  generated_topics.end(), title_less());
238  std::merge(generated_topics.begin(),
239  generated_topics.end(),topics.begin(),topics.end()
240  ,std::back_inserter(sec.topics),title_less());
241  }
242  else {
243  sec.topics.insert(sec.topics.end(),
244  topics.begin(), topics.end());
245  sec.topics.insert(sec.topics.end(),
246  generated_topics.begin(), generated_topics.end());
247  }
248  }
249 }
250 
252 {
253  section sec;
254  if (cfg != nullptr) {
255  const config& toplevel_cfg = cfg->child("toplevel");
256  parse_config_internal(cfg, toplevel_cfg ? &toplevel_cfg : nullptr, sec);
257  }
258  return sec;
259 }
260 
261 std::vector<topic> generate_topics(const bool sort_generated,const std::string &generator)
262 {
263  std::vector<topic> res;
264  if (generator.empty()) {
265  return res;
266  }
267 
268  if (generator == "abilities") {
269  res = generate_ability_topics(sort_generated);
270  } else if (generator == "weapon_specials") {
271  res = generate_weapon_special_topics(sort_generated);
272  } else if (generator == "time_of_days") {
273  res = generate_time_of_day_topics(sort_generated);
274  } else if (generator == "traits") {
275  res = generate_trait_topics(sort_generated);
276  } else {
277  std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
278  if (parts.size() > 1 && parts[0] == "units") {
279  res = generate_unit_topics(sort_generated, parts[1]);
280  } else if (parts[0] == "era" && parts.size()>1) {
281  res = generate_era_topics(sort_generated, parts[1]);
282  } else {
283  WRN_HP << "Found a topic generator that I didn't recognize: " << generator << "\n";
284  }
285  }
286 
287  return res;
288 }
289 
290 void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level)
291 {
292  if (generator == "races") {
293  generate_races_sections(help_cfg, sec, level);
294  } else if (generator == "terrains") {
295  generate_terrain_sections(sec, level);
296  } else if (generator == "eras") {
297  DBG_HP << "Generating eras...\n";
298  generate_era_sections(help_cfg, sec, level);
299  } else {
300  std::vector<std::string> parts = utils::split(generator, ':', utils::STRIP_SPACES);
301  if (parts.size() > 1 && parts[0] == "units") {
302  generate_unit_sections(help_cfg, sec, level, true, parts[1]);
303  } else if (generator.size() > 0) {
304  WRN_HP << "Found a section generator that I didn't recognize: " << generator << "\n";
305  }
306  }
307 }
308 
309 std::string generate_topic_text(const std::string &generator, const config *help_cfg, const section &sec, const std::vector<topic>& generated_topics)
310 {
311  std::string empty_string = "";
312  if (generator.empty()) {
313  return empty_string;
314  } else {
315  std::vector<std::string> parts = utils::split(generator, ':');
316  if (parts.size() > 1 && parts[0] == "contents") {
317  if (parts[1] == "generated") {
318  return generate_contents_links(sec, generated_topics);
319  } else {
320  return generate_contents_links(parts[1], help_cfg);
321  }
322  }
323  }
324  return empty_string;
325 }
326 
327 topic_text& topic_text::operator=(std::shared_ptr<topic_generator> g)
328 {
329  generator_ = g;
330  return *this;
331 }
332 
333 const std::vector<std::string>& topic_text::parsed_text() const
334 {
335  if (generator_) {
337  // This caches the result, so doesn't need the generator any more
338  generator_.reset();
339  }
340  return parsed_text_;
341 }
342 
343 std::string time_of_day_bonus_colored(const int time_of_day_bonus)
344 {
345  // Use same red/green colouring scheme as time_of_day_at() in reports.cpp for consistency
346  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>";
347 }
348 
349 std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
350 {
351  std::vector<topic> topics;
352  std::stringstream toplevel;
353 
354  if (! resources::tod_manager) {
355  toplevel << _("Only available during a scenario.");
356  topics.emplace_back(_("Time of Day Schedule"), "..schedule", toplevel.str());
357  return topics;
358  }
359 
360  const std::vector<time_of_day>& times = resources::tod_manager->times();
361  for (const time_of_day& time : times)
362  {
363  const std::string id = "time_of_day_" + time.id;
364  const std::string image = "<img>src='" + time.image + "'</img>";
365  const std::string image_lawful = "<img>src='icons/alignments/alignment_lawful_30.png'</img>";
366  const std::string image_neutral = "<img>src='icons/alignments/alignment_neutral_30.png'</img>";
367  const std::string image_chaotic = "<img>src='icons/alignments/alignment_chaotic_30.png'</img>";
368  const std::string image_liminal = "<img>src='icons/alignments/alignment_liminal_30.png'</img>";
369  std::stringstream text;
370 
371  const int lawful_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::LAWFUL, false, resources::tod_manager->get_max_liminal_bonus());
372  const int neutral_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::NEUTRAL, false, resources::tod_manager->get_max_liminal_bonus());
373  const int chaotic_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::CHAOTIC, false, resources::tod_manager->get_max_liminal_bonus());
374  const int liminal_bonus = generic_combat_modifier(time.lawful_bonus, unit_type::ALIGNMENT::LIMINAL, false, resources::tod_manager->get_max_liminal_bonus());
375 
376  toplevel << make_link(time.name.str(), id) << jump_to(160) << image << jump(30) <<
377  image_lawful << time_of_day_bonus_colored(lawful_bonus) << jump_to(390) <<
378  image_neutral << time_of_day_bonus_colored(neutral_bonus) << jump_to(450) <<
379  image_chaotic << time_of_day_bonus_colored(chaotic_bonus) << jump_to(520) <<
380  image_liminal << time_of_day_bonus_colored(liminal_bonus) << '\n';
381 
382  text << image << '\n' << time.description.str() << '\n' <<
383  image_lawful << _("Lawful Bonus:") << ' ' << time_of_day_bonus_colored(lawful_bonus) << '\n' <<
384  image_neutral << _("Neutral Bonus:") << ' ' << time_of_day_bonus_colored(neutral_bonus) << '\n' <<
385  image_chaotic << _("Chaotic Bonus:") << ' ' << time_of_day_bonus_colored(chaotic_bonus) << '\n' <<
386  image_liminal << _("Liminal Bonus:") << ' ' << time_of_day_bonus_colored(liminal_bonus) << '\n' <<
387  '\n' << make_link(_("Schedule"), "..schedule");
388 
389  topics.emplace_back(time.name.str(), id, text.str());
390  }
391 
392  topics.emplace_back(_("Time of Day Schedule"), "..schedule", toplevel.str());
393  return topics;
394 }
395 
396 std::vector<topic> generate_weapon_special_topics(const bool sort_generated)
397 {
398  std::vector<topic> topics;
399 
400  std::map<t_string, std::string> special_description;
401  std::map<t_string, std::set<std::string, string_less>> special_units;
402 
403  for (const unit_type_data::unit_type_map::value_type &type_mapping : unit_types.types())
404  {
405  const unit_type &type = type_mapping.second;
406  // Only show the weapon special if we find it on a unit that
407  // detailed description should be shown about.
408  if (description_type(type) != FULL_DESCRIPTION)
409  continue;
410 
411  for (const attack_type& atk : type.attacks()) {
412 
413  std::vector<std::pair<t_string, t_string>> specials = atk.special_tooltips();
414  for ( std::size_t i = 0; i != specials.size(); ++i )
415  {
416  special_description.emplace(specials[i].first, specials[i].second);
417 
418  if (!type.hide_help()) {
419  //add a link in the list of units having this special
420  std::string type_name = type.type_name();
421  //check for variations (walking corpse/soulless etc)
422  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
423  std::string ref_id = section_prefix + unit_prefix + type.id();
424  //we put the translated name at the beginning of the hyperlink,
425  //so the automatic alphabetic sorting of std::set can use it
426  std::string link = make_link(type_name, ref_id);
427  special_units[specials[i].first].insert(link);
428  }
429  }
430  }
431 
432  for(config adv : type.modification_advancements()) {
433  for(config effect : adv.child_range("effect")) {
434  if(effect["apply_to"] == "new_attack" && effect.has_child("specials")) {
435  for(config::any_child spec : effect.child("specials").all_children_range()) {
436  if(!spec.cfg["name"].empty()) {
437  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
438  if(!type.hide_help()) {
439  //add a link in the list of units having this special
440  std::string type_name = type.type_name();
441  //check for variations (walking corpse/soulless etc)
442  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
443  std::string ref_id = section_prefix + unit_prefix + type.id();
444  //we put the translated name at the beginning of the hyperlink,
445  //so the automatic alphabetic sorting of std::set can use it
446  std::string link = make_link(type_name, ref_id);
447  special_units[spec.cfg["name"]].insert(link);
448  }
449  }
450  }
451  } else if(effect["apply_to"] == "attack" && effect.has_child("set_specials")) {
452  for(config::any_child spec : effect.child("set_specials").all_children_range()) {
453  if(!spec.cfg["name"].empty()) {
454  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
455  if(!type.hide_help()) {
456  //add a link in the list of units having this special
457  std::string type_name = type.type_name();
458  //check for variations (walking corpse/soulless etc)
459  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
460  std::string ref_id = section_prefix + unit_prefix + type.id();
461  //we put the translated name at the beginning of the hyperlink,
462  //so the automatic alphabetic sorting of std::set can use it
463  std::string link = make_link(type_name, ref_id);
464  special_units[spec.cfg["name"]].insert(link);
465  }
466  }
467  }
468  }
469  }
470  }
471  }
472 
473  for (std::map<t_string, std::string>::iterator s = special_description.begin();
474  s != special_description.end(); ++s) {
475  // use untranslated name to have universal topic id
476  std::string id = "weaponspecial_" + s->first.base_str();
477  std::stringstream text;
478  text << s->second;
479  text << "\n\n" << _("<header>text='Units with this special attack'</header>") << "\n";
480  std::set<std::string, string_less> &units = special_units[s->first];
481  for (std::set<std::string, string_less>::iterator u = units.begin(); u != units.end(); ++u) {
482  text << font::unicode_bullet << " " << (*u) << "\n";
483  }
484 
485  topics.emplace_back(s->first, id, text.str());
486  }
487 
488  if (sort_generated)
489  std::sort(topics.begin(), topics.end(), title_less());
490  return topics;
491 }
492 
493 std::vector<topic> generate_ability_topics(const bool sort_generated)
494 {
495  std::vector<topic> topics;
496 
497  std::map<std::string, const unit_type::ability_metadata*> ability_topic_data;
498  std::map<std::string, std::set<std::string, string_less>> ability_units;
499 
500  const auto parse = [&](const unit_type& type, const unit_type::ability_metadata& ability) {
501  // NOTE: neither ability names nor ability ids are necessarily unique. Creating
502  // topics for either each unique name or each unique id means certain abilities
503  // will be excluded from help. So... the ability topic ref id is a combination
504  // of id and (untranslated) name. It's rather ugly, but it works.
505  const std::string topic_ref = ability.id + ability.name.base_str();
506 
507  ability_topic_data.emplace(topic_ref, &ability);
508 
509  if(!type.hide_help()) {
510  // Add a link in the list of units with this ability
511  // We put the translated name at the beginning of the hyperlink,
512  // so the automatic alphabetic sorting of std::set can use it
513  const std::string link = make_link(type.type_name(), unit_prefix + type.id());
514  ability_units[topic_ref].insert(link);
515  }
516  };
517 
518  // Look through all the unit types. If a unit of that type would have a full
519  // description, add its abilities to the potential topic list. We don't want
520  // to show abilities that the user has not encountered yet.
521  for(const auto& type_mapping : unit_types.types()) {
522  const unit_type& type = type_mapping.second;
523 
524  if(description_type(type) != FULL_DESCRIPTION) {
525  continue;
526  }
527 
528  for(const unit_type::ability_metadata& ability : type.abilities_metadata()) {
529  parse(type, ability);
530  }
531 
532  for(const unit_type::ability_metadata& ability : type.adv_abilities_metadata()) {
533  parse(type, ability);
534  }
535  }
536 
537  for(const auto& a : ability_topic_data) {
538  if (a.second->name.empty()) {
539  continue;
540  }
541  std::ostringstream text;
542  text << a.second->description;
543  text << "\n\n" << _("<header>text='Units with this ability'</header>") << "\n";
544 
545  for(const auto& u : ability_units[a.first]) { // first is the topic ref id
546  text << font::unicode_bullet << " " << u << "\n";
547  }
548 
549  topics.emplace_back(a.second->name, ability_prefix + a.first, text.str());
550  }
551 
552  if(sort_generated) {
553  std::sort(topics.begin(), topics.end(), title_less());
554  }
555 
556  return topics;
557 }
558 
559 std::vector<topic> generate_era_topics(const bool sort_generated, const std::string & era_id)
560 {
561  std::vector<topic> topics;
562 
563  const config & era = game_cfg->find_child("era","id", era_id);
564  if(era && !era["hide_help"].to_bool()) {
565  topics = generate_faction_topics(era, sort_generated);
566 
567  std::vector<std::string> faction_links;
568  for (const topic & t : topics) {
569  faction_links.push_back(make_link(t.title, t.id));
570  }
571 
572  std::stringstream text;
573  text << "<header>text='" << _("Era:") << " " << era["name"] << "'</header>" << "\n";
574  text << "\n";
575  const config::attribute_value& description = era["description"];
576  if (!description.empty()) {
577  text << description.t_str() << "\n";
578  text << "\n";
579  }
580 
581  text << "<header>text='" << _("Factions") << "'</header>" << "\n";
582 
583  std::sort(faction_links.begin(), faction_links.end());
584  for (const std::string &link : faction_links) {
585  text << font::unicode_bullet << " " << link << "\n";
586  }
587 
588  topic era_topic(era["name"], ".." + era_prefix + era["id"].str(), text.str());
589 
590  topics.push_back( era_topic );
591  }
592  return topics;
593 }
594 
595 std::vector<topic> generate_faction_topics(const config & era, const bool sort_generated)
596 {
597  std::vector<topic> topics;
598  for (const config &f : era.child_range("multiplayer_side")) {
599  const std::string& id = f["id"];
600  if (id == "Random")
601  continue;
602 
603  std::stringstream text;
604 
605  const config::attribute_value& description = f["description"];
606  if (!description.empty()) {
607  text << description.t_str() << "\n";
608  text << "\n";
609  }
610 
611  const std::vector<std::string> recruit_ids = utils::split(f["recruit"]);
612  std::set<std::string> races;
613  std::set<std::string> alignments;
614 
615  for (const std::string & u_id : recruit_ids) {
616  if (const unit_type * t = unit_types.find(u_id, unit_type::HELP_INDEXED)) {
617  assert(t);
618  const unit_type & type = *t;
619 
620  if (const unit_race *r = unit_types.find_race(type.race_id())) {
621  races.insert(make_link(r->plural_name(), std::string("..") + race_prefix + r->id()));
622  }
623  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
624  }
625  }
626 
627  if (!races.empty()) {
628  std::set<std::string>::iterator it = races.begin();
629  text << _("Races: ") << *(it++);
630  while(it != races.end()) {
631  text << ", " << *(it++);
632  }
633  text << "\n\n";
634  }
635 
636  if (!alignments.empty()) {
637  std::set<std::string>::iterator it = alignments.begin();
638  text << _("Alignments: ") << *(it++);
639  while(it != alignments.end()) {
640  text << ", " << *(it++);
641  }
642  text << "\n\n";
643  }
644 
645  text << "<header>text='" << _("Leaders") << "'</header>" << "\n";
646  const std::vector<std::string> leaders =
647  make_unit_links_list( utils::split(f["leader"]), true );
648  for (const std::string &link : leaders) {
649  text << font::unicode_bullet << " " << link << "\n";
650  }
651 
652  text << "\n";
653 
654  text << "<header>text='" << _("Recruits") << "'</header>" << "\n";
655  const std::vector<std::string> recruit_links =
656  make_unit_links_list( recruit_ids, true );
657  for (const std::string &link : recruit_links) {
658  text << font::unicode_bullet << " " << link << "\n";
659  }
660 
661  const std::string name = f["name"];
662  const std::string ref_id = faction_prefix + era["id"] + "_" + id;
663  topics.emplace_back(name, ref_id, text.str());
664  }
665  if (sort_generated)
666  std::sort(topics.begin(), topics.end(), title_less());
667  return topics;
668 }
669 
670 std::vector<topic> generate_trait_topics(const bool sort_generated)
671 {
672  std::vector<topic> topics;
673  std::map<t_string, const config> trait_list;
674 
675  for (const config & trait : unit_types.traits()) {
676  const std::string trait_id = trait["id"];
677  trait_list.emplace(trait_id, trait);
678  }
679 
680 
681  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
682  {
683  const unit_type &type = i.second;
684  if (description_type(type) == FULL_DESCRIPTION) {
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 {
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  base_unit.level = level +1;
917 
918  sec.add_section(base_unit);
919  }
920 }
921 
922 std::vector<topic> generate_unit_topics(const bool sort_generated, const std::string& race)
923 {
924  std::vector<topic> topics;
925  std::set<std::string, string_less> race_units;
926  std::set<std::string, string_less> race_topics;
927  std::set<std::string> alignments;
928 
929  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
930  {
931  const unit_type &type = i.second;
932 
933  if (type.race_id() != race)
934  continue;
935 
936  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
937  if (desc_type != FULL_DESCRIPTION)
938  continue;
939 
940  const std::string debug_suffix = (game_config::debug ? " (" + type.id() + ")" : "");
941  const std::string type_name = type.type_name() + (type.id() == type.type_name().str() ? "" : debug_suffix);
942  const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
943  const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
944  topic unit_topic(type_name, ref_id, "");
945  unit_topic.text = std::make_shared<unit_topic_generator>(type);
946  topics.push_back(unit_topic);
947 
948  if (!type.hide_help()) {
949  // we also record an hyperlink of this unit
950  // in the list used for the race topic
951  std::string link = make_link(type_name, ref_id);
952  race_units.insert(link);
953 
954  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
955  }
956  }
957 
958  //generate the hidden race description topic
959  std::string race_id = "..race_"+race;
960  std::string race_name;
961  std::string race_description;
962  if (const unit_race *r = unit_types.find_race(race)) {
963  race_name = r->plural_name();
964  race_description = r->description();
965  // if (description.empty()) description = _("No description Available");
966  for (const config &additional_topic : r->additional_topics())
967  {
968  std::string id = additional_topic["id"];
969  std::string title = additional_topic["title"];
970  std::string text = additional_topic["text"];
971  //topic additional_topic(title, id, text);
972  topics.emplace_back(title,id,text);
973  std::string link = make_link(title, id);
974  race_topics.insert(link);
975  }
976  } else {
977  race_name = _ ("race^Miscellaneous");
978  // description = _("Here put the description of the Miscellaneous race");
979  }
980 
981  std::stringstream text;
982 
983  if (!race_description.empty()) {
984  text << race_description << "\n\n";
985  }
986 
987  if (!alignments.empty()) {
988  std::set<std::string>::iterator it = alignments.begin();
989  text << (alignments.size() > 1 ? _("Alignments: ") : _("Alignment: ")) << *(it++);
990  while(it != alignments.end()) {
991  text << ", " << *(it++);
992  }
993  text << "\n\n";
994  }
995 
996  text << _("<header>text='Units of this race'</header>") << "\n";
997  for (std::set<std::string, string_less>::iterator u = race_units.begin(); u != race_units.end(); ++u) {
998  text << font::unicode_bullet << " " << (*u) << "\n";
999  }
1000 
1001  topics.emplace_back(race_name, race_id, text.str());
1002 
1003  if (sort_generated)
1004  std::sort(topics.begin(), topics.end(), title_less());
1005 
1006  return topics;
1007 }
1008 
1010 {
1013  return FULL_DESCRIPTION;
1014  }
1015 
1016  const std::set<std::string> &encountered_units = preferences::encountered_units();
1017  if (encountered_units.find(type.id()) != encountered_units.end()) {
1018  return FULL_DESCRIPTION;
1019  }
1020  return NO_DESCRIPTION;
1021 }
1022 
1023 std::string generate_contents_links(const std::string& section_name, config const *help_cfg)
1024 {
1025  const config& section_cfg = help_cfg->find_child("section", "id", section_name);
1026  if (!section_cfg) {
1027  return std::string();
1028  }
1029 
1030  std::ostringstream res;
1031 
1032  std::vector<std::string> topics = utils::quoted_split(section_cfg["topics"]);
1033 
1034  // we use an intermediate structure to allow a conditional sorting
1035  typedef std::pair<std::string,std::string> link;
1036  std::vector<link> topics_links;
1037 
1039  // Find all topics in this section.
1040  for (t = topics.begin(); t != topics.end(); ++t) {
1041  if (const config& topic_cfg = help_cfg->find_child("topic", "id", *t)) {
1042  std::string id = topic_cfg["id"];
1043  if (is_visible_id(id))
1044  topics_links.emplace_back(topic_cfg["title"], id);
1045  }
1046  }
1047 
1048  if (section_cfg["sort_topics"] == "yes") {
1049  std::sort(topics_links.begin(),topics_links.end());
1050  }
1051 
1053  for (l = topics_links.begin(); l != topics_links.end(); ++l) {
1054  std::string link = make_link(l->first, l->second);
1055  res << font::unicode_bullet << " " << link << "\n";
1056  }
1057 
1058  return res.str();
1059 }
1060 
1061 std::string generate_contents_links(const section &sec, const std::vector<topic>& topics)
1062 {
1063  std::stringstream res;
1064 
1065  for (auto &s : sec.sections) {
1066  if (is_visible_id(s.id)) {
1067  std::string link = make_link(s.title, ".."+s.id);
1068  res << font::unicode_bullet << " " << link << "\n";
1069  }
1070  }
1071 
1072  for (auto &t : topics) {
1073  if (is_visible_id(t.id)) {
1074  std::string link = make_link(t.title, t.id);
1075  res << font::unicode_bullet << " " << link << "\n";
1076  }
1077  }
1078  return res.str();
1079 }
1080 
1081 bool topic::operator==(const topic &t) const
1082 {
1083  return t.id == id;
1084 }
1085 
1086 bool topic::operator<(const topic &t) const
1087 {
1088  return id < t.id;
1089 }
1090 
1091 bool section::operator==(const section &sec) const
1092 {
1093  return sec.id == id;
1094 }
1095 
1096 bool section::operator<(const section &sec) const
1097 {
1098  return id < sec.id;
1099 }
1100 
1102 {
1103  sections.emplace_back(s);
1104 }
1105 
1107 {
1108  topics.clear();
1109  sections.clear();
1110 }
1111 
1112 
1113 
1114 const topic *find_topic(const section &sec, const std::string &id)
1115 {
1116  topic_list::const_iterator tit =
1117  std::find_if(sec.topics.begin(), sec.topics.end(), has_id(id));
1118  if (tit != sec.topics.end()) {
1119  return &(*tit);
1120  }
1121  for (const auto &s : sec.sections) {
1122  const auto t = find_topic(s, id);
1123  if (t != nullptr) {
1124  return t;
1125  }
1126  }
1127  return nullptr;
1128 }
1129 
1130 const section *find_section(const section &sec, const std::string &id)
1131 {
1132  const auto &sit =
1133  std::find_if(sec.sections.begin(), sec.sections.end(), has_id(id));
1134  if (sit != sec.sections.end()) {
1135  return &*sit;
1136  }
1137  for (const auto &subsection : sec.sections) {
1138  const auto s = find_section(subsection, id);
1139  if (s != nullptr) {
1140  return s;
1141  }
1142  }
1143  return nullptr;
1144 }
1145 
1146 std::vector<std::string> parse_text(const std::string &text)
1147 {
1148  std::vector<std::string> res;
1149  bool last_char_escape = false;
1150  const char escape_char = '\\';
1151  std::stringstream ss;
1152  std::size_t pos;
1153  enum { ELEMENT_NAME, OTHER } state = OTHER;
1154  for (pos = 0; pos < text.size(); ++pos) {
1155  const char c = text[pos];
1156  if (c == escape_char && !last_char_escape) {
1157  last_char_escape = true;
1158  }
1159  else {
1160  if (state == OTHER) {
1161  if (c == '<') {
1162  if (last_char_escape) {
1163  ss << c;
1164  }
1165  else {
1166  res.push_back(ss.str());
1167  ss.str("");
1168  state = ELEMENT_NAME;
1169  }
1170  }
1171  else {
1172  ss << c;
1173  }
1174  }
1175  else if (state == ELEMENT_NAME) {
1176  if (c == '/') {
1177  std::string msg = "Erroneous / in element name.";
1178  throw parse_error(msg);
1179  }
1180  else if (c == '>') {
1181  // End of this name.
1182  std::stringstream s;
1183  const std::string element_name = ss.str();
1184  ss.str("");
1185  s << "</" << element_name << ">";
1186  const std::string end_element_name = s.str();
1187  std::size_t end_pos = text.find(end_element_name, pos);
1188  if (end_pos == std::string::npos) {
1189  std::stringstream msg;
1190  msg << "Unterminated element: " << element_name;
1191  throw parse_error(msg.str());
1192  }
1193  s.str("");
1194  const std::string contents = text.substr(pos + 1, end_pos - pos - 1);
1195  const std::string element = convert_to_wml(element_name, contents);
1196  res.push_back(element);
1197  pos = end_pos + end_element_name.size() - 1;
1198  state = OTHER;
1199  }
1200  else {
1201  ss << c;
1202  }
1203  }
1204  last_char_escape = false;
1205  }
1206  }
1207  if (state == ELEMENT_NAME) {
1208  std::stringstream msg;
1209  msg << "Element '" << ss.str() << "' continues through end of string.";
1210  throw parse_error(msg.str());
1211  }
1212  if (!ss.str().empty()) {
1213  // Add the last string.
1214  res.push_back(ss.str());
1215  }
1216  return res;
1217 }
1218 
1219 std::string convert_to_wml(const std::string &element_name, const std::string &contents)
1220 {
1221  std::stringstream ss;
1222  bool in_quotes = false;
1223  bool last_char_escape = false;
1224  const char escape_char = '\\';
1225  std::vector<std::string> attributes;
1226  // Find the different attributes.
1227  // No checks are made for the equal sign or something like that.
1228  // Attributes are just separated by spaces or newlines.
1229  // Attributes that contain spaces must be in single quotes.
1230  for (std::size_t pos = 0; pos < contents.size(); ++pos) {
1231  const char c = contents[pos];
1232  if (c == escape_char && !last_char_escape) {
1233  last_char_escape = true;
1234  }
1235  else {
1236  if (c == '\'' && !last_char_escape) {
1237  ss << '"';
1238  in_quotes = !in_quotes;
1239  }
1240  else if ((c == ' ' || c == '\n') && !last_char_escape && !in_quotes) {
1241  // Space or newline, end of attribute.
1242  attributes.push_back(ss.str());
1243  ss.str("");
1244  }
1245  else {
1246  ss << c;
1247  }
1248  last_char_escape = false;
1249  }
1250  }
1251  if (in_quotes) {
1252  std::stringstream msg;
1253  msg << "Unterminated single quote after: '" << ss.str() << "'";
1254  throw parse_error(msg.str());
1255  }
1256  if (!ss.str().empty()) {
1257  attributes.push_back(ss.str());
1258  }
1259  ss.str("");
1260  // Create the WML.
1261  ss << "[" << element_name << "]\n";
1262  for (std::vector<std::string>::const_iterator it = attributes.begin();
1263  it != attributes.end(); ++it) {
1264  ss << *it << "\n";
1265  }
1266  ss << "[/" << element_name << "]\n";
1267  return ss.str();
1268 }
1269 
1270 color_t string_to_color(const std::string &cmp_str)
1271 {
1272  if (cmp_str == "green") {
1273  return font::GOOD_COLOR;
1274  }
1275  if (cmp_str == "red") {
1276  return font::BAD_COLOR;
1277  }
1278  if (cmp_str == "black") {
1279  return font::BLACK_COLOR;
1280  }
1281  if (cmp_str == "yellow") {
1282  return font::YELLOW_COLOR;
1283  }
1284  if (cmp_str == "white") {
1285  return font::BIGMAP_COLOR;
1286  }
1287  // a #rrggbb color in pango format.
1288  if (*cmp_str.c_str() == '#' && cmp_str.size() == 7) {
1289  return color_t::from_argb_bytes(strtoul(cmp_str.c_str() + 1, nullptr, 16));
1290  }
1291  return font::NORMAL_COLOR;
1292 }
1293 
1294 std::vector<std::string> split_in_width(const std::string &s, const int font_size,
1295  const unsigned width)
1296 {
1297  std::vector<std::string> res;
1298  try {
1299  const std::string& first_line = font::word_wrap_text(s, font_size, width, -1, 1, true);
1300  res.push_back(first_line);
1301  if(s.size() > first_line.size()) {
1302  res.push_back(s.substr(first_line.size()));
1303  }
1304  }
1306  {
1307  throw parse_error (_("corrupted original file"));
1308  }
1309 
1310  return res;
1311 }
1312 
1313 std::string remove_first_space(const std::string& text)
1314 {
1315  if (text.length() > 0 && text[0] == ' ') {
1316  return text.substr(1);
1317  }
1318  return text;
1319 }
1320 
1321 std::string get_first_word(const std::string &s)
1322 {
1323  std::size_t first_word_start = s.find_first_not_of(' ');
1324  if (first_word_start == std::string::npos) {
1325  return s;
1326  }
1327  std::size_t first_word_end = s.find_first_of(" \n", first_word_start);
1328  if( first_word_end == first_word_start ) {
1329  // This word is '\n'.
1330  first_word_end = first_word_start+1;
1331  }
1332 
1333  //if no gap(' ' or '\n') found, test if it is CJK character
1334  std::string re = s.substr(0, first_word_end);
1335 
1336  utf8::iterator ch(re);
1337  if (ch == utf8::iterator::end(re))
1338  return re;
1339 
1340  char32_t firstchar = *ch;
1341  if (font::is_cjk_char(firstchar)) {
1342  re = unicode_cast<std::string>(firstchar);
1343  }
1344  return re;
1345 }
1346 
1348 {
1349  default_toplevel.clear();
1350  hidden_sections.clear();
1351  if (game_cfg != nullptr) {
1352  const config *help_config = &game_cfg->child_or_empty("help");
1353  try {
1354  default_toplevel = parse_config(help_config);
1355  // Create a config object that contains everything that is
1356  // not referenced from the toplevel element. Read this
1357  // config and save these sections and topics so that they
1358  // can be referenced later on when showing help about
1359  // specified things, but that should not be shown when
1360  // opening the help browser in the default manner.
1361  config hidden_toplevel;
1362  std::stringstream ss;
1363  for (const config &section : help_config->child_range("section"))
1364  {
1365  const std::string id = section["id"];
1366  if (find_section(default_toplevel, id) == nullptr) {
1367  // This section does not exist referenced from the
1368  // toplevel. Hence, add it to the hidden ones if it
1369  // is not referenced from another section.
1370  if (!section_is_referenced(id, *help_config)) {
1371  if (!ss.str().empty()) {
1372  ss << ",";
1373  }
1374  ss << id;
1375  }
1376  }
1377  }
1378  hidden_toplevel["sections"] = ss.str();
1379  ss.str("");
1380  for (const config &topic : help_config->child_range("topic"))
1381  {
1382  const std::string id = topic["id"];
1383  if (find_topic(default_toplevel, id) == nullptr) {
1384  if (!topic_is_referenced(id, *help_config)) {
1385  if (!ss.str().empty()) {
1386  ss << ",";
1387  }
1388  ss << id;
1389  }
1390  }
1391  }
1392  hidden_toplevel["topics"] = ss.str();
1393  config hidden_cfg = *help_config;
1394  // Change the toplevel to our new, custom built one.
1395  hidden_cfg.clear_children("toplevel");
1396  hidden_cfg.add_child("toplevel", std::move(hidden_toplevel));
1397  hidden_sections = parse_config(&hidden_cfg);
1398  }
1399  catch (parse_error& e) {
1400  std::stringstream msg;
1401  msg << "Parse error when parsing help text: '" << e.message << "'";
1402  std::cerr << msg.str() << std::endl;
1403  }
1404  }
1405 }
1406 
1407 // id starting with '.' are hidden
1408 std::string hidden_symbol(bool hidden) {
1409  return (hidden ? "." : "");
1410 }
1411 
1412 bool is_visible_id(const std::string &id) {
1413  return (id.empty() || id[0] != '.');
1414 }
1415 
1416 /// Return true if the id is valid for user defined topics and
1417 /// sections. Some IDs are special, such as toplevel and may not be
1418 /// be defined in the config.
1419 bool is_valid_id(const std::string &id) {
1420  if (id == "toplevel") {
1421  return false;
1422  }
1423  if (id.compare(0, unit_prefix.length(), unit_prefix) == 0 || id.compare(hidden_symbol().length(), unit_prefix.length(), unit_prefix) == 0) {
1424  return false;
1425  }
1426  if (id.compare(0, 8, "ability_") == 0) {
1427  return false;
1428  }
1429  if (id.compare(0, 14, "weaponspecial_") == 0) {
1430  return false;
1431  }
1432  if (id == "hidden") {
1433  return false;
1434  }
1435  return true;
1436 }
1437 
1438 
1439 // Return the width for the image with filename.
1440 unsigned image_width(const std::string &filename)
1441 {
1442  image::locator loc(filename);
1443  surface surf(image::get_image(loc));
1444  if (surf != nullptr) {
1445  return surf->w;
1446  }
1447  return 0;
1448 }
1449 
1450 void push_tab_pair(std::vector<help::item> &v, const std::string &s, const boost::optional<std::string> &image, unsigned padding)
1451 {
1452  help::item item(s, font::line_width(s, normal_font_size));
1453  if (image) {
1454  // If the image doesn't exist, don't add padding.
1455  auto width = image_width(image.get());
1456  padding = (width ? padding : 0);
1457 
1458  item.first = "<img>src='" + image.get() + "'</img>" + (padding ? jump(padding) : "") + s;
1459  item.second += width + padding;
1460  }
1461  v.emplace_back(item);
1462 }
1463 
1464 std::string generate_table(const table_spec &tab, const unsigned int spacing)
1465 {
1466  table_spec::const_iterator row_it;
1467  std::vector<std::pair<std::string, unsigned>>::const_iterator col_it;
1468  unsigned int num_cols = 0;
1469  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1470  if (row_it->size() > num_cols) {
1471  num_cols = row_it->size();
1472  }
1473  }
1474  std::vector<unsigned int> col_widths(num_cols, 0);
1475  // Calculate the width of all columns, including spacing.
1476  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1477  unsigned int col = 0;
1478  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1479  if (col_widths[col] < col_it->second + spacing) {
1480  col_widths[col] = col_it->second + spacing;
1481  }
1482  ++col;
1483  }
1484  }
1485  std::vector<unsigned int> col_starts(num_cols);
1486  // Calculate the starting positions of all columns
1487  for (unsigned int i = 0; i < num_cols; ++i) {
1488  unsigned int this_col_start = 0;
1489  for (unsigned int j = 0; j < i; ++j) {
1490  this_col_start += col_widths[j];
1491  }
1492  col_starts[i] = this_col_start;
1493  }
1494  std::stringstream ss;
1495  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1496  unsigned int col = 0;
1497  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1498  ss << jump_to(col_starts[col]) << col_it->first;
1499  ++col;
1500  }
1501  ss << "\n";
1502  }
1503  return ss.str();
1504 }
1505 
1506 /// Prepend all chars with meaning inside attributes with a backslash.
1507 std::string escape(const std::string &s)
1508 {
1509  return utils::escape(s, "'\\");
1510 }
1511 
1512 /// Load the appropriate terrain types data to use
1514  if (display::get_singleton()) {
1516  } else if (game_config_manager::get()){
1518  } else {
1519  return ter_data_cache();
1520  }
1521 }
1522 
1523 
1524 } // end namespace help
std::string jump_to(const unsigned pos)
Definition: help_impl.hpp:350
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:833
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:251
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
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:88
std::vector< topic > generate_trait_topics(const bool sort_generated)
Definition: help_impl.cpp:670
const std::string unit_prefix
Definition: help_impl.cpp:89
std::vector< topic > generate_unit_topics(const bool sort_generated, const std::string &race)
Definition: help_impl.cpp:922
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:1157
const std::string era_prefix
Definition: help_impl.cpp:93
void generate_unit_sections(const config *, section &sec, int level, const bool, const std::string &race)
Definition: help_impl.cpp:888
const std::string topic_img
Definition: help_impl.cpp:83
void clear_children(T... keys)
Definition: config.hpp:479
std::string era()
Definition: game.cpp:723
const int title_size
Definition: help_impl.cpp:78
const ter_data_cache & terrain_types() const
bool is_overlay() const
Definition: terrain.hpp:155
config_array_view traits() const
Definition: types.hpp:378
const std::string closed_section_img
Definition: help_impl.cpp:84
std::string remove_first_space(const std::string &text)
Definition: help_impl.cpp:1313
bool is_scope_active(scope s)
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:142
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:309
logger & info()
Definition: log.cpp:90
#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
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:1096
std::vector< std::string > empty_string_vector
Definition: help_impl.cpp:76
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:50
unit_type_data unit_types
Definition: types.cpp:1376
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:1592
const int normal_font_size
Definition: help_impl.cpp:81
unsigned image_width(const std::string &filename)
Definition: help_impl.cpp:1440
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:1270
const unit_type_map & types() const
Definition: types.hpp:376
bool is_visible_id(const std::string &id)
Definition: help_impl.cpp:1412
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< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::vector< topic > generate_weapon_special_topics(const bool sort_generated)
Definition: help_impl.cpp:396
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:290
const int SIZE_NORMAL
Definition: constants.cpp:19
help::section hidden_sections
Definition: help_impl.cpp:70
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:1130
std::string id
Definition: help_impl.hpp:133
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
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()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
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:78
std::string generate_table(const table_spec &tab, const unsigned int spacing)
Definition: help_impl.cpp:1464
void push_tab_pair(std::vector< help::item > &v, const std::string &s, const boost::optional< std::string > &image, unsigned padding)
Definition: help_impl.cpp:1450
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:238
std::vector< std::vector< help::item > > table_spec
Definition: help_impl.hpp:374
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1408
bool hide_help() const
Definition: types.cpp:592
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:136
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:559
REMOVE_EMPTY: remove empty elements.
UNIT_DESCRIPTION_TYPE
Definition: help_impl.hpp:234
std::vector< topic > generate_faction_topics(const config &era, const bool sort_generated)
Definition: help_impl.cpp:595
std::string time_of_day_bonus_colored(const int time_of_day_bonus)
Definition: help_impl.cpp:343
std::set< t_translation::terrain_code > & encountered_terrains()
Definition: game.cpp:958
const std::vector< ability_metadata > & abilities_metadata() const
Definition: types.hpp:208
std::string race_id() const
Returns the ID of this type&#39;s race without the need to build the type.
Definition: types.hpp:261
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:139
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:80
ter_data_cache load_terrain_types_data()
Load the appropriate terrain types data to use.
Definition: help_impl.cpp:1513
static lg::log_domain log_help("help")
const color_t YELLOW_COLOR
bool show_all_units_in_help()
Definition: game.cpp:946
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:81
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:210
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
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:118
const unit_race * find_race(const std::string &) const
Definition: types.cpp:1263
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:713
Thrown by operations encountering invalid UTF-8 data.
void generate_terrain_sections(section &sec, int)
Definition: help_impl.cpp:836
const ter_data_cache & tdata() const
Definition: map.hpp:69
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:344
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:1219
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:730
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
const display_context & get_disp_context() const
Definition: display.hpp:168
const game_config_view * game_cfg
Definition: help_impl.cpp:66
std::set< std::string > & encountered_units()
Definition: game.cpp:954
const bool & debug
bool operator<(const topic &) const
Comparison on the ID.
Definition: help_impl.cpp:1086
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
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:211
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:1507
std::vector< std::string > parse_text(const std::string &text)
Parse a text string.
Definition: help_impl.cpp:1146
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:349
config::const_child_itors possible_traits() const
Definition: types.hpp:222
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:1023
const std::string faction_prefix
Definition: help_impl.cpp:92
std::string jump(const unsigned amount)
Definition: help_impl.hpp:357
#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:474
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1347
bool find(E event, F functor)
Tests whether an event handler is available.
std::vector< topic > generate_topics(const bool sort_generated, const std::string &generator)
Definition: help_impl.cpp:261
bool operator==(const topic &) const
Two topics are equal if their IDs are equal.
Definition: help_impl.cpp:1081
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:1114
const t_string & variation_name() const
Definition: types.hpp:164
const std::vector< std::string > & parsed_text() const
Definition: help_impl.cpp:333
this module manages the cache of images.
Standard logging facilities (interface).
const std::string & id() const
Definition: terrain.hpp:51
std::vector< terrain_code > ter_list
Definition: translation.hpp:78
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:108
#define e
Definition: help.cpp:55
const_attack_itors attacks() const
Definition: types.cpp:511
std::string title
Definition: help_impl.hpp:133
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:1419
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
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:1321
ALIGNMENT alignment() const
Definition: types.hpp:184
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:1101
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:1009
std::vector< topic > generate_ability_topics(const bool sort_generated)
Definition: help_impl.cpp:493
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:416
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. All parts are less than width.
Definition: help_impl.cpp:1294
std::string debug() const
Definition: config.cpp:1290
topic_list topics
Definition: help_impl.hpp:162
topic_text text
Definition: help_impl.hpp:134
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:372
const color_t BAD_COLOR
bool operator==(const section &) const
Two sections are equal if their IDs are equal.
Definition: help_impl.cpp:1091
std::shared_ptr< terrain_type_data > ter_data_cache
help::section default_toplevel
Definition: help_impl.cpp:68