The Battle for Wesnoth  1.15.0-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 http://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 "display.hpp" // for display
19 #include "display_context.hpp" // for display_context
20 #include "game_config.hpp" // for debug, menu_contract, etc
21 #include "game_config_manager.hpp" // for game_config_manager
22 #include "preferences/game.hpp" // for encountered_terrains, etc
23 #include "gettext.hpp" // for _, gettext, N_
25 #include "hotkey/hotkey_command.hpp" // for is_scope_active, etc
26 #include "picture.hpp" // for get_image, locator
27 #include "log.hpp" // for LOG_STREAM, logger, etc
28 #include "utils/make_enum.hpp" // for operator<<
29 #include "map/map.hpp" // for gamemap
30 #include "font/marked-up_text.hpp" // for is_cjk_char, word_wrap_text
31 #include "font/standard_colors.hpp" // for NORMAL_COLOR
32 #include "units/race.hpp" // for unit_race, etc
33 #include "resources.hpp" // for tod_manager, config_manager
34 #include "sdl/surface.hpp" // for surface
35 #include "serialization/string_utils.hpp" // for split, quoted_split, etc
36 #include "serialization/unicode_cast.hpp" // for unicode_cast
37 #include "serialization/utf8_exception.hpp" // for char_t, etc
38 #include "terrain/terrain.hpp" // for terrain_type
39 #include "terrain/translation.hpp" // for operator==, ter_list, etc
40 #include "terrain/type_data.hpp" // for terrain_type_data, etc
41 #include "time_of_day.hpp" // for time_of_day
42 #include "tod_manager.hpp" // for tod_manager
43 #include "tstring.hpp" // for t_string, operator<<
44 #include "units/types.hpp" // for unit_type, unit_type_data, etc
45 #include "serialization/unicode.hpp" // for iterator
46 #include "color.hpp"
47 
48 #include <cassert> // for assert
49 #include <algorithm> // for sort, find, transform, etc
50 #include <iostream> // for operator<<, basic_ostream, etc
51 #include <iterator> // for back_insert_iterator, etc
52 #include <map> // for map, etc
53 #include <set>
54 #include <SDL.h>
55 
56 static lg::log_domain log_display("display");
57 #define WRN_DP LOG_STREAM(warn, log_display)
58 
59 static lg::log_domain log_help("help");
60 #define WRN_HP LOG_STREAM(warn, log_help)
61 #define DBG_HP LOG_STREAM(debug, log_help)
62 
63 namespace help {
64 
65 const config *game_cfg = nullptr;
66 // The default toplevel.
68 // All sections and topics not referenced from the default toplevel.
70 
73 boost::tribool last_debug_state = boost::indeterminate;
74 
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  //TODO: harmonize topics/sections sorting
185  if ((*section_cfg)["sort_sections"] == "yes") {
186  std::sort(sec.sections.begin(),sec.sections.end(), 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 
329 {
330  if (generator_ && --generator_->count == 0)
331  delete generator_;
332 }
333 
335 {
336  if (generator_)
337  ++generator_->count;
338 }
339 
341 {
342  if (generator_ && --generator_->count == 0)
343  delete generator_;
344  generator_ = g;
345  return *this;
346 }
347 
348 const std::vector<std::string>& topic_text::parsed_text() const
349 {
350  if (generator_) {
352  if (--generator_->count == 0)
353  delete generator_;
354  generator_ = nullptr;
355  }
356  return parsed_text_;
357 }
358 
359 std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
360 {
361  std::vector<topic> topics;
362  std::stringstream toplevel;
363 
364  if (! resources::tod_manager) {
365  toplevel << _("Only available during a scenario.");
366  topics.emplace_back("Time of Day Schedule", "..schedule", toplevel.str());
367  return topics;
368  }
369  const std::vector<time_of_day>& times = resources::tod_manager->times();
370  for (const time_of_day& time : times)
371  {
372  const std::string id = "time_of_day_" + time.id;
373  const std::string image = "<img>src='" + time.image + "'</img>";
374  std::stringstream text;
375 
376  toplevel << make_link(time.name.str(), id) << jump_to(160) <<
377  image << jump(30) << time.lawful_bonus << '\n';
378 
379  text << image << '\n' <<
380  time.description.str() << '\n' <<
381  "Lawful Bonus: " << time.lawful_bonus << '\n' <<
382  '\n' << make_link(_("Schedule"), "..schedule");
383 
384  topics.emplace_back(time.name.str(), id, text.str());
385  }
386 
387  topics.emplace_back("Time of Day Schedule", "..schedule", toplevel.str());
388  return topics;
389 }
390 
391 std::vector<topic> generate_weapon_special_topics(const bool sort_generated)
392 {
393  std::vector<topic> topics;
394 
395  std::map<t_string, std::string> special_description;
396  std::map<t_string, std::set<std::string, string_less>> special_units;
397 
398  for (const unit_type_data::unit_type_map::value_type &type_mapping : unit_types.types())
399  {
400  const unit_type &type = type_mapping.second;
401  // Only show the weapon special if we find it on a unit that
402  // detailed description should be shown about.
403  if (description_type(type) != FULL_DESCRIPTION)
404  continue;
405 
406  for (const attack_type& atk : type.attacks()) {
407 
408  std::vector<std::pair<t_string, t_string>> specials = atk.special_tooltips();
409  for ( std::size_t i = 0; i != specials.size(); ++i )
410  {
411  special_description.emplace(specials[i].first, specials[i].second);
412 
413  if (!type.hide_help()) {
414  //add a link in the list of units having this special
415  std::string type_name = type.type_name();
416  //check for variations (walking corpse/soulless etc)
417  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
418  std::string ref_id = section_prefix + unit_prefix + type.id();
419  //we put the translated name at the beginning of the hyperlink,
420  //so the automatic alphabetic sorting of std::set can use it
421  std::string link = make_link(type_name, ref_id);
422  special_units[specials[i].first].insert(link);
423  }
424  }
425  }
426 
427  for(config adv : type.modification_advancements()) {
428  for(config effect : adv.child_range("effect")) {
429  if(effect["apply_to"] == "new_attack" && effect.has_child("specials")) {
430  for(config::any_child spec : effect.child("specials").all_children_range()) {
431  if(!spec.cfg["name"].empty()) {
432  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
433  if(!type.hide_help()) {
434  //add a link in the list of units having this special
435  std::string type_name = type.type_name();
436  //check for variations (walking corpse/soulless etc)
437  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
438  std::string ref_id = section_prefix + unit_prefix + type.id();
439  //we put the translated name at the beginning of the hyperlink,
440  //so the automatic alphabetic sorting of std::set can use it
441  std::string link = make_link(type_name, ref_id);
442  special_units[spec.cfg["name"]].insert(link);
443  }
444  }
445  }
446  } else if(effect["apply_to"] == "attack" && effect.has_child("set_specials")) {
447  for(config::any_child spec : effect.child("set_specials").all_children_range()) {
448  if(!spec.cfg["name"].empty()) {
449  special_description.emplace(spec.cfg["name"].t_str(), spec.cfg["description"].t_str());
450  if(!type.hide_help()) {
451  //add a link in the list of units having this special
452  std::string type_name = type.type_name();
453  //check for variations (walking corpse/soulless etc)
454  const std::string section_prefix = type.show_variations_in_help() ? ".." : "";
455  std::string ref_id = section_prefix + unit_prefix + type.id();
456  //we put the translated name at the beginning of the hyperlink,
457  //so the automatic alphabetic sorting of std::set can use it
458  std::string link = make_link(type_name, ref_id);
459  special_units[spec.cfg["name"]].insert(link);
460  }
461  }
462  }
463  }
464  }
465  }
466  }
467 
468  for (std::map<t_string, std::string>::iterator s = special_description.begin();
469  s != special_description.end(); ++s) {
470  // use untranslated name to have universal topic id
471  std::string id = "weaponspecial_" + s->first.base_str();
472  std::stringstream text;
473  text << s->second;
474  text << "\n\n" << _("<header>text='Units with this special attack'</header>") << "\n";
475  std::set<std::string, string_less> &units = special_units[s->first];
476  for (std::set<std::string, string_less>::iterator u = units.begin(); u != units.end(); ++u) {
477  text << font::unicode_bullet << " " << (*u) << "\n";
478  }
479 
480  topics.emplace_back(s->first, id, text.str());
481  }
482 
483  if (sort_generated)
484  std::sort(topics.begin(), topics.end(), title_less());
485  return topics;
486 }
487 
488 std::vector<topic> generate_ability_topics(const bool sort_generated)
489 {
490  std::vector<topic> topics;
491 
492  std::map<std::string, const unit_type::ability_metadata*> ability_topic_data;
493  std::map<std::string, std::set<std::string, string_less>> ability_units;
494 
495  const auto parse = [&](const unit_type& type, const unit_type::ability_metadata& ability) {
496  // NOTE: neither ability names nor ability ids are necessarily unique. Creating
497  // topics for either each unique name or each unique id means certain abilities
498  // will be excluded from help. So... the ability topic ref id is a combination
499  // of id and (untranslated) name. It's rather ugly, but it works.
500  const std::string topic_ref = ability.id + ability.name.base_str();
501 
502  ability_topic_data.emplace(topic_ref, &ability);
503 
504  if(!type.hide_help()) {
505  // Add a link in the list of units with this ability
506  // We put the translated name at the beginning of the hyperlink,
507  // so the automatic alphabetic sorting of std::set can use it
508  const std::string link = make_link(type.type_name(), unit_prefix + type.id());
509  ability_units[topic_ref].insert(link);
510  }
511  };
512 
513  // Look through all the unit types. If a unit of that type would have a full
514  // description, add its abilities to the potential topic list. We don't want
515  // to show abilities that the user has not encountered yet.
516  for(const auto& type_mapping : unit_types.types()) {
517  const unit_type& type = type_mapping.second;
518 
519  if(description_type(type) != FULL_DESCRIPTION) {
520  continue;
521  }
522 
523  for(const unit_type::ability_metadata& ability : type.abilities_metadata()) {
524  parse(type, ability);
525  }
526 
527  for(const unit_type::ability_metadata& ability : type.adv_abilities_metadata()) {
528  parse(type, ability);
529  }
530  }
531 
532  for(const auto& a : ability_topic_data) {
533  if (a.second->name.empty()) {
534  continue;
535  }
536  std::ostringstream text;
537  text << a.second->description;
538  text << "\n\n" << _("<header>text='Units with this ability'</header>") << "\n";
539 
540  for(const auto& u : ability_units[a.first]) { // first is the topic ref id
541  text << font::unicode_bullet << " " << u << "\n";
542  }
543 
544  topics.emplace_back(a.second->name, ability_prefix + a.first, text.str());
545  }
546 
547  if(sort_generated) {
548  std::sort(topics.begin(), topics.end(), title_less());
549  }
550 
551  return topics;
552 }
553 
554 std::vector<topic> generate_era_topics(const bool sort_generated, const std::string & era_id)
555 {
556  std::vector<topic> topics;
557 
558  const config & era = game_cfg->find_child("era","id", era_id);
559  if(era && !era["hide_help"].to_bool()) {
560  topics = generate_faction_topics(era, sort_generated);
561 
562  std::vector<std::string> faction_links;
563  for (const topic & t : topics) {
564  faction_links.push_back(make_link(t.title, t.id));
565  }
566 
567  std::stringstream text;
568  text << "<header>text='" << _("Era:") << " " << era["name"] << "'</header>" << "\n";
569  text << "\n";
570  const config::attribute_value& description = era["description"];
571  if (!description.empty()) {
572  text << description.t_str() << "\n";
573  text << "\n";
574  }
575 
576  text << "<header>text='" << _("Factions") << "'</header>" << "\n";
577 
578  std::sort(faction_links.begin(), faction_links.end());
579  for (const std::string &link : faction_links) {
580  text << font::unicode_bullet << " " << link << "\n";
581  }
582 
583  topic era_topic(era["name"], ".." + era_prefix + era["id"].str(), text.str());
584 
585  topics.push_back( era_topic );
586  }
587  return topics;
588 }
589 
590 std::vector<topic> generate_faction_topics(const config & era, const bool sort_generated)
591 {
592  std::vector<topic> topics;
593  for (const config &f : era.child_range("multiplayer_side")) {
594  const std::string& id = f["id"];
595  if (id == "Random")
596  continue;
597 
598  std::stringstream text;
599 
600  const config::attribute_value& description = f["description"];
601  if (!description.empty()) {
602  text << description.t_str() << "\n";
603  text << "\n";
604  }
605 
606  const std::vector<std::string> recruit_ids = utils::split(f["recruit"]);
607  std::set<std::string> races;
608  std::set<std::string> alignments;
609 
610  for (const std::string & u_id : recruit_ids) {
611  if (const unit_type * t = unit_types.find(u_id, unit_type::HELP_INDEXED)) {
612  assert(t);
613  const unit_type & type = *t;
614 
615  if (const unit_race *r = unit_types.find_race(type.race_id())) {
616  races.insert(make_link(r->plural_name(), std::string("..") + race_prefix + r->id()));
617  }
618  DBG_HP << type.alignment() << " -> " << type.alignment_description(type.alignment(), type.genders().front()) << "\n";
619  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
620  }
621  }
622 
623  if (!races.empty()) {
624  std::set<std::string>::iterator it = races.begin();
625  text << _("Races: ") << *(it++);
626  while(it != races.end()) {
627  text << ", " << *(it++);
628  }
629  text << "\n\n";
630  }
631 
632  if (!alignments.empty()) {
633  std::set<std::string>::iterator it = alignments.begin();
634  text << _("Alignments: ") << *(it++);
635  while(it != alignments.end()) {
636  text << ", " << *(it++);
637  }
638  text << "\n\n";
639  }
640 
641  text << "<header>text='" << _("Leaders") << "'</header>" << "\n";
642  const std::vector<std::string> leaders =
643  make_unit_links_list( utils::split(f["leader"]), true );
644  for (const std::string &link : leaders) {
645  text << font::unicode_bullet << " " << link << "\n";
646  }
647 
648  text << "\n";
649 
650  text << "<header>text='" << _("Recruits") << "'</header>" << "\n";
651  const std::vector<std::string> recruit_links =
652  make_unit_links_list( recruit_ids, true );
653  for (const std::string &link : recruit_links) {
654  text << font::unicode_bullet << " " << link << "\n";
655  }
656 
657  const std::string name = f["name"];
658  const std::string ref_id = faction_prefix + era["id"] + "_" + id;
659  topics.emplace_back(name, ref_id, text.str());
660  }
661  if (sort_generated)
662  std::sort(topics.begin(), topics.end(), title_less());
663  return topics;
664 }
665 
666 std::vector<topic> generate_trait_topics(const bool sort_generated)
667 {
668  std::vector<topic> topics;
669  std::map<t_string, const config> trait_list;
670 
671  for (const config & trait : unit_types.traits()) {
672  const std::string trait_id = trait["id"];
673  trait_list.emplace(trait_id, trait);
674  }
675 
676 
677  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
678  {
679  const unit_type &type = i.second;
680  if (description_type(type) == FULL_DESCRIPTION) {
681  if (config::const_child_itors traits = type.possible_traits()) {
682  for (const config & trait : traits) {
683  const std::string trait_id = trait["id"];
684  trait_list.emplace(trait_id, trait);
685  }
686  }
687  if (const unit_race *r = unit_types.find_race(type.race_id())) {
688  for (const config & trait : r->additional_traits()) {
689  const std::string trait_id = trait["id"];
690  trait_list.emplace(trait_id, trait);
691  }
692  }
693  }
694  }
695 
696  for (std::map<t_string, const config>::iterator a = trait_list.begin(); a != trait_list.end(); ++a) {
697  std::string id = "traits_" + a->first;
698  const config trait = a->second;
699 
700  std::string name = trait["male_name"].str();
701  if (name.empty()) name = trait["female_name"].str();
702  if (name.empty()) name = trait["name"].str();
703  if (name.empty()) continue; // Hidden trait
704 
705  std::stringstream text;
706  if (!trait["help_text"].empty()) {
707  text << trait["help_text"];
708  } else if (!trait["description"].empty()) {
709  text << trait["description"];
710  } else {
711  text << _("No description available.");
712  }
713  text << "\n\n";
714  if (trait["availability"] == "musthave") {
715  text << _("Availability: ") << _("Must-have") << "\n";
716  } else if (trait["availability"] == "none") {
717  text << _("Availability: ") << _("Unavailable") << "\n";
718  }
719  topics.emplace_back(name, id, text.str());
720  }
721 
722  if (sort_generated)
723  std::sort(topics.begin(), topics.end(), title_less());
724  return topics;
725 }
726 
727 
728 std::string make_unit_link(const std::string& type_id)
729 {
730  std::string link;
731 
733  if (!type) {
734  std::cerr << "Unknown unit type : " << type_id << "\n";
735  // don't return an hyperlink (no page)
736  // instead show the id (as hint)
737  link = type_id;
738  } else if (!type->hide_help()) {
739  std::string name = type->type_name();
740  std::string ref_id;
741  if (description_type(*type) == FULL_DESCRIPTION) {
742  const std::string section_prefix = type->show_variations_in_help() ? ".." : "";
743  ref_id = section_prefix + unit_prefix + type->id();
744  } else {
745  ref_id = unknown_unit_topic;
746  name += " (?)";
747  }
748  link = make_link(name, ref_id);
749  } // if hide_help then link is an empty string
750 
751  return link;
752 }
753 
754 std::vector<std::string> make_unit_links_list(const std::vector<std::string>& type_id_list, bool ordered)
755 {
756  std::vector<std::string> links_list;
757  for (const std::string &type_id : type_id_list) {
758  std::string unit_link = make_unit_link(type_id);
759  if (!unit_link.empty())
760  links_list.push_back(unit_link);
761  }
762 
763  if (ordered)
764  std::sort(links_list.begin(), links_list.end());
765 
766  return links_list;
767 }
768 
769 void generate_races_sections(const config *help_cfg, section &sec, int level)
770 {
771  std::set<std::string, string_less> races;
772  std::set<std::string, string_less> visible_races;
773 
774  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
775  {
776  const unit_type &type = i.second;
777  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
778  if (desc_type == FULL_DESCRIPTION) {
779  races.insert(type.race_id());
780  if (!type.hide_help())
781  visible_races.insert(type.race_id());
782  }
783  }
784 
785  for(std::set<std::string, string_less>::iterator it = races.begin(); it != races.end(); ++it) {
786  section race_section;
787  config section_cfg;
788 
789  bool hidden = (visible_races.count(*it) == 0);
790 
791  section_cfg["id"] = hidden_symbol(hidden) + race_prefix + *it;
792 
793  std::string title;
794  if (const unit_race *r = unit_types.find_race(*it)) {
795  title = r->plural_name();
796  } else {
797  title = _ ("race^Miscellaneous");
798  }
799  section_cfg["title"] = title;
800 
801  section_cfg["sections_generator"] = "units:" + *it;
802  section_cfg["generator"] = "units:" + *it;
803 
804  parse_config_internal(help_cfg, &section_cfg, race_section, level+1);
805  sec.add_section(race_section);
806  }
807 }
808 
809 void generate_era_sections(const config* help_cfg, section & sec, int level)
810 {
811  for (const config & era : game_cfg->child_range("era")) {
812  if (era["hide_help"].to_bool()) {
813  continue;
814  }
815 
816  DBG_HP << "Adding help section: " << era["id"].str() << "\n";
817 
818  section era_section;
819  config section_cfg;
820  section_cfg["id"] = era_prefix + era["id"].str();
821  section_cfg["title"] = era["name"];
822 
823  section_cfg["generator"] = "era:" + era["id"].str();
824 
825  DBG_HP << section_cfg.debug() << "\n";
826 
827  parse_config_internal(help_cfg, &section_cfg, era_section, level+1);
828  sec.add_section(era_section);
829  }
830 }
831 
832 void generate_terrain_sections(const config* /*help_cfg*/, section& sec, int /*level*/)
833 {
835 
836  if (!tdata) {
837  WRN_HP << "When building terrain help sections, couldn't acquire terrain types data, aborting.\n";
838  return;
839  }
840 
841  std::map<std::string, section> base_map;
842 
843  const t_translation::ter_list& t_listi = tdata->list();
844 
845  for (const t_translation::terrain_code& t : t_listi) {
846 
847  const terrain_type& info = tdata->get_terrain_info(t);
848 
849  bool hidden = info.is_combined() || info.hide_help();
850 
852  == preferences::encountered_terrains().end() && !info.is_overlay())
853  hidden = true;
854 
855  topic terrain_topic;
856  terrain_topic.title = info.editor_name();
857  terrain_topic.id = hidden_symbol(hidden) + terrain_prefix + info.id();
858  terrain_topic.text = new terrain_topic_generator(info);
859 
860  t_translation::ter_list base_terrains = tdata->underlying_union_terrain(t);
861  for (const t_translation::terrain_code& base : base_terrains) {
862 
863  const terrain_type& base_info = tdata->get_terrain_info(base);
864 
865  if (!base_info.is_nonnull() || base_info.hide_help())
866  continue;
867 
868  section& base_section = base_map[base_info.id()];
869 
870  base_section.id = terrain_prefix + base_info.id();
871  base_section.title = base_info.editor_name();
872 
873  if (base_info.id() == info.id())
874  terrain_topic.id = ".." + terrain_prefix + info.id();
875  base_section.topics.push_back(terrain_topic);
876  }
877  }
878 
879  for (std::map<std::string, section>::const_iterator it = base_map.begin(); it != base_map.end(); ++it) {
880  sec.add_section(it->second);
881  }
882 }
883 
884 void generate_unit_sections(const config* /*help_cfg*/, section& sec, int level, const bool /*sort_generated*/, const std::string& race)
885 {
886  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
887  const unit_type &type = i.second;
888 
889  if (type.race_id() != race)
890  continue;
891 
892  if (!type.show_variations_in_help())
893  continue;
894 
895  section base_unit;
896  for (const std::string &variation_id : type.variations()) {
897  // TODO: Do we apply encountered stuff to variations?
898  const unit_type &var_type = type.get_variation(variation_id);
899  const std::string topic_name = var_type.type_name() + "\n" + var_type.variation_name();
900  const std::string var_ref = hidden_symbol(var_type.hide_help()) + variation_prefix + var_type.id() + "_" + variation_id;
901 
902  topic var_topic(topic_name, var_ref, "");
903  var_topic.text = new unit_topic_generator(var_type, variation_id);
904  base_unit.topics.push_back(var_topic);
905  }
906 
907  const std::string type_name = type.type_name();
908  const std::string ref_id = hidden_symbol(type.hide_help()) + unit_prefix + type.id();
909 
910  base_unit.id = ref_id;
911  base_unit.title = type_name;
912  base_unit.level = level +1;
913 
914  sec.add_section(base_unit);
915  }
916 }
917 
918 std::vector<topic> generate_unit_topics(const bool sort_generated, const std::string& race)
919 {
920  std::vector<topic> topics;
921  std::set<std::string, string_less> race_units;
922  std::set<std::string, string_less> race_topics;
923  std::set<std::string> alignments;
924 
925  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
926  {
927  const unit_type &type = i.second;
928 
929  if (type.race_id() != race)
930  continue;
931 
932  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
933  if (desc_type != FULL_DESCRIPTION)
934  continue;
935 
936  const std::string type_name = type.type_name();
937  const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
938  const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
939  topic unit_topic(type_name, ref_id, "");
940  unit_topic.text = new unit_topic_generator(type);
941  topics.push_back(unit_topic);
942 
943  if (!type.hide_help()) {
944  // we also record an hyperlink of this unit
945  // in the list used for the race topic
946  std::string link = make_link(type_name, ref_id);
947  race_units.insert(link);
948 
949  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
950  }
951  }
952 
953  //generate the hidden race description topic
954  std::string race_id = "..race_"+race;
955  std::string race_name;
956  std::string race_description;
957  if (const unit_race *r = unit_types.find_race(race)) {
958  race_name = r->plural_name();
959  race_description = r->description();
960  // if (description.empty()) description = _("No description Available");
961  for (const config &additional_topic : r->additional_topics())
962  {
963  std::string id = additional_topic["id"];
964  std::string title = additional_topic["title"];
965  std::string text = additional_topic["text"];
966  //topic additional_topic(title, id, text);
967  topics.emplace_back(title,id,text);
968  std::string link = make_link(title, id);
969  race_topics.insert(link);
970  }
971  } else {
972  race_name = _ ("race^Miscellaneous");
973  // description = _("Here put the description of the Miscellaneous race");
974  }
975 
976  std::stringstream text;
977 
978  if (!race_description.empty()) {
979  text << race_description << "\n\n";
980  }
981 
982  if (!alignments.empty()) {
983  std::set<std::string>::iterator it = alignments.begin();
984  text << (alignments.size() > 1 ? _("Alignments: ") : _("Alignment: ")) << *(it++);
985  while(it != alignments.end()) {
986  text << ", " << *(it++);
987  }
988  text << "\n\n";
989  }
990 
991  text << _("<header>text='Units of this race'</header>") << "\n";
992  for (std::set<std::string, string_less>::iterator u = race_units.begin(); u != race_units.end(); ++u) {
993  text << font::unicode_bullet << " " << (*u) << "\n";
994  }
995 
996  topics.emplace_back(race_name, race_id, text.str());
997 
998  if (sort_generated)
999  std::sort(topics.begin(), topics.end(), title_less());
1000 
1001  return topics;
1002 }
1003 
1005 {
1008  return FULL_DESCRIPTION;
1009  }
1010 
1011  const std::set<std::string> &encountered_units = preferences::encountered_units();
1012  if (encountered_units.find(type.id()) != encountered_units.end()) {
1013  return FULL_DESCRIPTION;
1014  }
1015  return NO_DESCRIPTION;
1016 }
1017 
1018 std::string generate_contents_links(const std::string& section_name, config const *help_cfg)
1019 {
1020  const config& section_cfg = help_cfg->find_child("section", "id", section_name);
1021  if (!section_cfg) {
1022  return std::string();
1023  }
1024 
1025  std::ostringstream res;
1026 
1027  std::vector<std::string> topics = utils::quoted_split(section_cfg["topics"]);
1028 
1029  // we use an intermediate structure to allow a conditional sorting
1030  typedef std::pair<std::string,std::string> link;
1031  std::vector<link> topics_links;
1032 
1034  // Find all topics in this section.
1035  for (t = topics.begin(); t != topics.end(); ++t) {
1036  if (const config& topic_cfg = help_cfg->find_child("topic", "id", *t)) {
1037  std::string id = topic_cfg["id"];
1038  if (is_visible_id(id))
1039  topics_links.emplace_back(topic_cfg["title"], id);
1040  }
1041  }
1042 
1043  if (section_cfg["sort_topics"] == "yes") {
1044  std::sort(topics_links.begin(),topics_links.end());
1045  }
1046 
1048  for (l = topics_links.begin(); l != topics_links.end(); ++l) {
1049  std::string link = make_link(l->first, l->second);
1050  res << font::unicode_bullet << " " << link << "\n";
1051  }
1052 
1053  return res.str();
1054 }
1055 
1056 std::string generate_contents_links(const section &sec, const std::vector<topic>& topics)
1057 {
1058  std::stringstream res;
1059 
1060  section_list::const_iterator s;
1061  for (s = sec.sections.begin(); s != sec.sections.end(); ++s) {
1062  if (is_visible_id((*s)->id)) {
1063  std::string link = make_link((*s)->title, ".."+(*s)->id);
1064  res << font::unicode_bullet << " " << link << "\n";
1065  }
1066  }
1067 
1068  std::vector<topic>::const_iterator t;
1069  for (t = topics.begin(); t != topics.end(); ++t) {
1070  if (is_visible_id(t->id)) {
1071  std::string link = make_link(t->title, t->id);
1072  res << font::unicode_bullet << " " << link << "\n";
1073  }
1074  }
1075  return res.str();
1076 }
1077 
1078 bool topic::operator==(const topic &t) const
1079 {
1080  return t.id == id;
1081 }
1082 
1083 bool topic::operator<(const topic &t) const
1084 {
1085  return id < t.id;
1086 }
1087 
1089 {
1090  std::for_each(sections.begin(), sections.end(), delete_section());
1091 }
1092 
1094  title(sec.title),
1095  id(sec.id),
1096  topics(sec.topics),
1097  sections(),
1098  level(sec.level)
1099 {
1100  std::transform(sec.sections.begin(), sec.sections.end(),
1101  std::back_inserter(sections), create_section());
1102 }
1103 
1105 {
1106  title = sec.title;
1107  id = sec.id;
1108  level = sec.level;
1109  topics.insert(topics.end(), sec.topics.begin(), sec.topics.end());
1110  std::transform(sec.sections.begin(), sec.sections.end(),
1111  std::back_inserter(sections), create_section());
1112  return *this;
1113 }
1114 
1115 
1116 bool section::operator==(const section &sec) const
1117 {
1118  return sec.id == id;
1119 }
1120 
1121 bool section::operator<(const section &sec) const
1122 {
1123  return id < sec.id;
1124 }
1125 
1127 {
1128  sections.push_back(new section(s));
1129 }
1130 
1132 {
1133  topics.clear();
1134  std::for_each(sections.begin(), sections.end(), delete_section());
1135  sections.clear();
1136 }
1137 
1138 
1139 
1140 const topic *find_topic(const section &sec, const std::string &id)
1141 {
1142  topic_list::const_iterator tit =
1143  std::find_if(sec.topics.begin(), sec.topics.end(), has_id(id));
1144  if (tit != sec.topics.end()) {
1145  return &(*tit);
1146  }
1147  section_list::const_iterator sit;
1148  for (sit = sec.sections.begin(); sit != sec.sections.end(); ++sit) {
1149  const topic *t = find_topic(*(*sit), id);
1150  if (t != nullptr) {
1151  return t;
1152  }
1153  }
1154  return nullptr;
1155 }
1156 
1157 const section *find_section(const section &sec, const std::string &id)
1158 {
1159  section_list::const_iterator sit =
1160  std::find_if(sec.sections.begin(), sec.sections.end(), has_id(id));
1161  if (sit != sec.sections.end()) {
1162  return *sit;
1163  }
1164  for (sit = sec.sections.begin(); sit != sec.sections.end(); ++sit) {
1165  const section *s = find_section(*(*sit), id);
1166  if (s != nullptr) {
1167  return s;
1168  }
1169  }
1170  return nullptr;
1171 }
1172 
1173 std::vector<std::string> parse_text(const std::string &text)
1174 {
1175  std::vector<std::string> res;
1176  bool last_char_escape = false;
1177  const char escape_char = '\\';
1178  std::stringstream ss;
1179  std::size_t pos;
1180  enum { ELEMENT_NAME, OTHER } state = OTHER;
1181  for (pos = 0; pos < text.size(); ++pos) {
1182  const char c = text[pos];
1183  if (c == escape_char && !last_char_escape) {
1184  last_char_escape = true;
1185  }
1186  else {
1187  if (state == OTHER) {
1188  if (c == '<') {
1189  if (last_char_escape) {
1190  ss << c;
1191  }
1192  else {
1193  res.push_back(ss.str());
1194  ss.str("");
1195  state = ELEMENT_NAME;
1196  }
1197  }
1198  else {
1199  ss << c;
1200  }
1201  }
1202  else if (state == ELEMENT_NAME) {
1203  if (c == '/') {
1204  std::string msg = "Erroneous / in element name.";
1205  throw parse_error(msg);
1206  }
1207  else if (c == '>') {
1208  // End of this name.
1209  std::stringstream s;
1210  const std::string element_name = ss.str();
1211  ss.str("");
1212  s << "</" << element_name << ">";
1213  const std::string end_element_name = s.str();
1214  std::size_t end_pos = text.find(end_element_name, pos);
1215  if (end_pos == std::string::npos) {
1216  std::stringstream msg;
1217  msg << "Unterminated element: " << element_name;
1218  throw parse_error(msg.str());
1219  }
1220  s.str("");
1221  const std::string contents = text.substr(pos + 1, end_pos - pos - 1);
1222  const std::string element = convert_to_wml(element_name, contents);
1223  res.push_back(element);
1224  pos = end_pos + end_element_name.size() - 1;
1225  state = OTHER;
1226  }
1227  else {
1228  ss << c;
1229  }
1230  }
1231  last_char_escape = false;
1232  }
1233  }
1234  if (state == ELEMENT_NAME) {
1235  std::stringstream msg;
1236  msg << "Element '" << ss.str() << "' continues through end of string.";
1237  throw parse_error(msg.str());
1238  }
1239  if (!ss.str().empty()) {
1240  // Add the last string.
1241  res.push_back(ss.str());
1242  }
1243  return res;
1244 }
1245 
1246 std::string convert_to_wml(const std::string &element_name, const std::string &contents)
1247 {
1248  std::stringstream ss;
1249  bool in_quotes = false;
1250  bool last_char_escape = false;
1251  const char escape_char = '\\';
1252  std::vector<std::string> attributes;
1253  // Find the different attributes.
1254  // No checks are made for the equal sign or something like that.
1255  // Attributes are just separated by spaces or newlines.
1256  // Attributes that contain spaces must be in single quotes.
1257  for (std::size_t pos = 0; pos < contents.size(); ++pos) {
1258  const char c = contents[pos];
1259  if (c == escape_char && !last_char_escape) {
1260  last_char_escape = true;
1261  }
1262  else {
1263  if (c == '\'' && !last_char_escape) {
1264  ss << '"';
1265  in_quotes = !in_quotes;
1266  }
1267  else if ((c == ' ' || c == '\n') && !last_char_escape && !in_quotes) {
1268  // Space or newline, end of attribute.
1269  attributes.push_back(ss.str());
1270  ss.str("");
1271  }
1272  else {
1273  ss << c;
1274  }
1275  last_char_escape = false;
1276  }
1277  }
1278  if (in_quotes) {
1279  std::stringstream msg;
1280  msg << "Unterminated single quote after: '" << ss.str() << "'";
1281  throw parse_error(msg.str());
1282  }
1283  if (!ss.str().empty()) {
1284  attributes.push_back(ss.str());
1285  }
1286  ss.str("");
1287  // Create the WML.
1288  ss << "[" << element_name << "]\n";
1289  for (std::vector<std::string>::const_iterator it = attributes.begin();
1290  it != attributes.end(); ++it) {
1291  ss << *it << "\n";
1292  }
1293  ss << "[/" << element_name << "]\n";
1294  return ss.str();
1295 }
1296 
1297 color_t string_to_color(const std::string &cmp_str)
1298 {
1299  if (cmp_str == "green") {
1300  return font::GOOD_COLOR;
1301  }
1302  if (cmp_str == "red") {
1303  return font::BAD_COLOR;
1304  }
1305  if (cmp_str == "black") {
1306  return font::BLACK_COLOR;
1307  }
1308  if (cmp_str == "yellow") {
1309  return font::YELLOW_COLOR;
1310  }
1311  if (cmp_str == "white") {
1312  return font::BIGMAP_COLOR;
1313  }
1314  // a #rrggbb color in pango format.
1315  if (*cmp_str.c_str() == '#' && cmp_str.size() == 7) {
1316  return color_t::from_argb_bytes(strtoul(cmp_str.c_str() + 1, nullptr, 16));
1317  }
1318  return font::NORMAL_COLOR;
1319 }
1320 
1321 std::vector<std::string> split_in_width(const std::string &s, const int font_size,
1322  const unsigned width)
1323 {
1324  std::vector<std::string> res;
1325  try {
1326  const std::string& first_line = font::word_wrap_text(s, font_size, width, -1, 1, true);
1327  res.push_back(first_line);
1328  if(s.size() > first_line.size()) {
1329  res.push_back(s.substr(first_line.size()));
1330  }
1331  }
1333  {
1334  throw parse_error (_("corrupted original file"));
1335  }
1336 
1337  return res;
1338 }
1339 
1340 std::string remove_first_space(const std::string& text)
1341 {
1342  if (text.length() > 0 && text[0] == ' ') {
1343  return text.substr(1);
1344  }
1345  return text;
1346 }
1347 
1348 std::string get_first_word(const std::string &s)
1349 {
1350  std::size_t first_word_start = s.find_first_not_of(' ');
1351  if (first_word_start == std::string::npos) {
1352  return s;
1353  }
1354  std::size_t first_word_end = s.find_first_of(" \n", first_word_start);
1355  if( first_word_end == first_word_start ) {
1356  // This word is '\n'.
1357  first_word_end = first_word_start+1;
1358  }
1359 
1360  //if no gap(' ' or '\n') found, test if it is CJK character
1361  std::string re = s.substr(0, first_word_end);
1362 
1363  utf8::iterator ch(re);
1364  if (ch == utf8::iterator::end(re))
1365  return re;
1366 
1367  char32_t firstchar = *ch;
1368  if (font::is_cjk_char(firstchar)) {
1369  re = unicode_cast<std::string>(firstchar);
1370  }
1371  return re;
1372 }
1373 
1375 {
1376  default_toplevel.clear();
1377  hidden_sections.clear();
1378  if (game_cfg != nullptr) {
1379  const config *help_config = &game_cfg->child("help");
1380  if (!*help_config) {
1381  help_config = &dummy_cfg;
1382  }
1383  try {
1384  default_toplevel = parse_config(help_config);
1385  // Create a config object that contains everything that is
1386  // not referenced from the toplevel element. Read this
1387  // config and save these sections and topics so that they
1388  // can be referenced later on when showing help about
1389  // specified things, but that should not be shown when
1390  // opening the help browser in the default manner.
1391  config hidden_toplevel;
1392  std::stringstream ss;
1393  for (const config &section : help_config->child_range("section"))
1394  {
1395  const std::string id = section["id"];
1396  if (find_section(default_toplevel, id) == nullptr) {
1397  // This section does not exist referenced from the
1398  // toplevel. Hence, add it to the hidden ones if it
1399  // is not referenced from another section.
1400  if (!section_is_referenced(id, *help_config)) {
1401  if (!ss.str().empty()) {
1402  ss << ",";
1403  }
1404  ss << id;
1405  }
1406  }
1407  }
1408  hidden_toplevel["sections"] = ss.str();
1409  ss.str("");
1410  for (const config &topic : help_config->child_range("topic"))
1411  {
1412  const std::string id = topic["id"];
1413  if (find_topic(default_toplevel, id) == nullptr) {
1414  if (!topic_is_referenced(id, *help_config)) {
1415  if (!ss.str().empty()) {
1416  ss << ",";
1417  }
1418  ss << id;
1419  }
1420  }
1421  }
1422  hidden_toplevel["topics"] = ss.str();
1423  config hidden_cfg = *help_config;
1424  // Change the toplevel to our new, custom built one.
1425  hidden_cfg.clear_children("toplevel");
1426  hidden_cfg.add_child("toplevel", std::move(hidden_toplevel));
1427  hidden_sections = parse_config(&hidden_cfg);
1428  }
1429  catch (parse_error& e) {
1430  std::stringstream msg;
1431  msg << "Parse error when parsing help text: '" << e.message << "'";
1432  std::cerr << msg.str() << std::endl;
1433  }
1434  }
1435 }
1436 
1437 // id starting with '.' are hidden
1438 std::string hidden_symbol(bool hidden) {
1439  return (hidden ? "." : "");
1440 }
1441 
1442 bool is_visible_id(const std::string &id) {
1443  return (id.empty() || id[0] != '.');
1444 }
1445 
1446 /// Return true if the id is valid for user defined topics and
1447 /// sections. Some IDs are special, such as toplevel and may not be
1448 /// be defined in the config.
1449 bool is_valid_id(const std::string &id) {
1450  if (id == "toplevel") {
1451  return false;
1452  }
1453  if (id.compare(0, unit_prefix.length(), unit_prefix) == 0 || id.compare(hidden_symbol().length(), unit_prefix.length(), unit_prefix) == 0) {
1454  return false;
1455  }
1456  if (id.compare(0, 8, "ability_") == 0) {
1457  return false;
1458  }
1459  if (id.compare(0, 14, "weaponspecial_") == 0) {
1460  return false;
1461  }
1462  if (id == "hidden") {
1463  return false;
1464  }
1465  return true;
1466 }
1467 
1468 
1469 // Return the width for the image with filename.
1470 unsigned image_width(const std::string &filename)
1471 {
1472  image::locator loc(filename);
1473  surface surf(image::get_image(loc));
1474  if (surf != nullptr) {
1475  return surf->w;
1476  }
1477  return 0;
1478 }
1479 
1480 void push_tab_pair(std::vector<std::pair<std::string, unsigned int>> &v, const std::string &s)
1481 {
1482  v.emplace_back(s, font::line_width(s, normal_font_size));
1483 }
1484 
1485 std::string generate_table(const table_spec &tab, const unsigned int spacing)
1486 {
1487  table_spec::const_iterator row_it;
1488  std::vector<std::pair<std::string, unsigned>>::const_iterator col_it;
1489  unsigned int num_cols = 0;
1490  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1491  if (row_it->size() > num_cols) {
1492  num_cols = row_it->size();
1493  }
1494  }
1495  std::vector<unsigned int> col_widths(num_cols, 0);
1496  // Calculate the width of all columns, including spacing.
1497  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1498  unsigned int col = 0;
1499  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1500  if (col_widths[col] < col_it->second + spacing) {
1501  col_widths[col] = col_it->second + spacing;
1502  }
1503  ++col;
1504  }
1505  }
1506  std::vector<unsigned int> col_starts(num_cols);
1507  // Calculate the starting positions of all columns
1508  for (unsigned int i = 0; i < num_cols; ++i) {
1509  unsigned int this_col_start = 0;
1510  for (unsigned int j = 0; j < i; ++j) {
1511  this_col_start += col_widths[j];
1512  }
1513  col_starts[i] = this_col_start;
1514  }
1515  std::stringstream ss;
1516  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1517  unsigned int col = 0;
1518  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1519  ss << jump_to(col_starts[col]) << col_it->first;
1520  ++col;
1521  }
1522  ss << "\n";
1523  }
1524  return ss.str();
1525 }
1526 
1527 /// Prepend all chars with meaning inside attributes with a backslash.
1528 std::string escape(const std::string &s)
1529 {
1530  return utils::escape(s, "'\\");
1531 }
1532 
1533 /// Load the appropriate terrain types data to use
1535  if (display::get_singleton()) {
1537  } else if (game_config_manager::get()){
1539  } else {
1540  return ter_data_cache();
1541  }
1542 }
1543 
1544 
1545 } // end namespace help
std::string jump_to(const unsigned pos)
Definition: help_impl.hpp:365
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:1022
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: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:165
::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:728
const std::string open_section_img
Definition: help_impl.cpp:85
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:666
const std::string unit_prefix
Definition: help_impl.cpp:89
config dummy_cfg
Definition: help_impl.cpp:75
std::vector< topic > generate_unit_topics(const bool sort_generated, const std::string &race)
Definition: help_impl.cpp:918
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:1275
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:884
const std::string topic_img
Definition: help_impl.cpp:83
void clear_children(T... keys)
Definition: config.hpp:509
std::string era()
Definition: game.cpp:698
const int title_size
Definition: help_impl.cpp:78
const ter_data_cache & terrain_types() const
bool is_overlay() const
Definition: terrain.hpp:79
const std::string closed_section_img
Definition: help_impl.cpp:84
std::string remove_first_space(const std::string &text)
Definition: help_impl.cpp:1340
bool is_scope_active(scope s)
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:143
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:310
logger & info()
Definition: log.cpp:90
#define a
#define WRN_HP
Definition: help_impl.cpp:60
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:73
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:221
bool operator<(const section &) const
Comparison on the ID.
Definition: help_impl.cpp:1121
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:1452
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
const int normal_font_size
Definition: help_impl.cpp:81
topic_generator * generator_
Definition: help_impl.hpp:84
unsigned image_width(const std::string &filename)
Definition: help_impl.cpp:1470
const std::string terrain_prefix
Definition: help_impl.cpp:90
int last_num_encountered_terrains
Definition: help_impl.cpp:72
const config * game_cfg
Definition: help_impl.cpp:65
color_t string_to_color(const std::string &cmp_str)
Return the color the string represents.
Definition: help_impl.cpp:1297
const unit_type_map & types() const
Definition: types.hpp:359
bool is_visible_id(const std::string &id)
Definition: help_impl.cpp:1442
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:391
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:754
A single unit type that the player may recruit.
Definition: types.hpp:42
bool is_nonnull() const
Definition: terrain.hpp:52
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:69
section_list sections
Definition: help_impl.hpp:167
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:1157
std::string id
Definition: help_impl.hpp:136
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:174
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
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:81
bool is_combined() const
Definition: terrain.hpp:80
std::string generate_table(const table_spec &tab, const unsigned int spacing)
Definition: help_impl.cpp:1485
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:239
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1438
bool hide_help() const
Definition: types.cpp:587
std::string title
Definition: help_impl.hpp:165
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:554
std::vector< std::vector< std::pair< std::string, unsigned int > > > table_spec
Definition: help_impl.hpp:386
REMOVE_EMPTY: remove empty elements.
UNIT_DESCRIPTION_TYPE
Definition: help_impl.hpp:249
void generate_terrain_sections(const config *, section &sec, int)
Definition: help_impl.cpp:832
std::vector< topic > generate_faction_topics(const config &era, const bool sort_generated)
Definition: help_impl.cpp:590
std::set< t_translation::terrain_code > & encountered_terrains()
Definition: game.cpp:944
const std::vector< ability_metadata > & abilities_metadata() const
Definition: types.hpp:209
std::string race_id() const
Returns the ID of this type&#39;s race without the need to build the type.
Definition: types.hpp:262
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:83
ter_data_cache load_terrain_types_data()
Load the appropriate terrain types data to use.
Definition: help_impl.cpp:1534
static lg::log_domain log_help("help")
const color_t YELLOW_COLOR
bool show_all_units_in_help()
Definition: game.cpp:932
const color_t NORMAL_COLOR
const config::const_child_itors traits() const
Definition: types.hpp:361
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:238
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:194
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:1420
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:769
Thrown by operations encountering invalid UTF-8 data.
const ter_data_cache & tdata() const
Definition: map.hpp:69
topic_text & operator=(topic_generator *g)
Definition: help_impl.cpp:340
int last_num_encountered_units
Definition: help_impl.cpp:71
std::string make_link(const std::string &text, const std::string &dst)
Definition: help_impl.hpp:359
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:1246
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:786
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:940
const bool & debug
bool operator<(const topic &) const
Comparison on the ID.
Definition: help_impl.cpp:1083
void generate_races_sections(const config *help_cfg, section &sec, int level)
Definition: help_impl.cpp:769
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:212
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:1528
std::vector< std::string > parse_text(const std::string &text)
Parse a text string.
Definition: help_impl.cpp:1173
config & add_child(config_key_type key)
Definition: config.cpp:476
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:359
config::const_child_itors possible_traits() const
Definition: types.hpp:223
static lg::log_domain log_display("display")
#define DBG_HP
Definition: help_impl.cpp:61
std::string generate_contents_links(const std::string &section_name, config const *help_cfg)
Definition: help_impl.cpp:1018
const std::string faction_prefix
Definition: help_impl.cpp:92
std::string jump(const unsigned amount)
Definition: help_impl.hpp:372
#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:466
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1374
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:1078
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:1140
const std::vector< std::string > & parsed_text() const
Definition: help_impl.cpp:348
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
Generate a topic text on the fly.
Definition: help_impl.hpp:62
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:111
#define e
Definition: help.cpp:56
const_attack_itors attacks() const
Definition: types.cpp:494
std::string title
Definition: help_impl.hpp:136
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:1449
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
static std::string alignment_description(ALIGNMENT align, unit_race::GENDER gender=unit_race::MALE)
mock_char c
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:1348
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:1126
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:1004
std::vector< topic > generate_ability_topics(const bool sort_generated)
Definition: help_impl.cpp:488
void generate_era_sections(const config *help_cfg, section &sec, int level)
Definition: help_impl.cpp:809
const std::string & variation_name() const
Definition: types.hpp:160
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
To be used as a function object when sorting topic lists on the title.
Definition: help_impl.hpp:186
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:1321
std::string debug() const
Definition: config.cpp:1277
void push_tab_pair(std::vector< std::pair< std::string, unsigned int >> &v, const std::string &s)
Definition: help_impl.cpp:1480
topic_list topics
Definition: help_impl.hpp:166
topic_text text
Definition: help_impl.hpp:137
const color_t BAD_COLOR
section & operator=(const section &)
Definition: help_impl.cpp:1104
bool operator==(const section &) const
Two sections are equal if their IDs are equal.
Definition: help_impl.cpp:1116
std::shared_ptr< terrain_type_data > ter_data_cache
help::section default_toplevel
Definition: help_impl.cpp:67