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