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