The Battle for Wesnoth  1.15.2+dev
help_impl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "help/help_impl.hpp"
16 
17 #include "about.hpp" // for get_text
18 #include "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  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
603  }
604  }
605 
606  if (!races.empty()) {
607  std::set<std::string>::iterator it = races.begin();
608  text << _("Races: ") << *(it++);
609  while(it != races.end()) {
610  text << ", " << *(it++);
611  }
612  text << "\n\n";
613  }
614 
615  if (!alignments.empty()) {
616  std::set<std::string>::iterator it = alignments.begin();
617  text << _("Alignments: ") << *(it++);
618  while(it != alignments.end()) {
619  text << ", " << *(it++);
620  }
621  text << "\n\n";
622  }
623 
624  text << "<header>text='" << _("Leaders") << "'</header>" << "\n";
625  const std::vector<std::string> leaders =
626  make_unit_links_list( utils::split(f["leader"]), true );
627  for (const std::string &link : leaders) {
628  text << font::unicode_bullet << " " << link << "\n";
629  }
630 
631  text << "\n";
632 
633  text << "<header>text='" << _("Recruits") << "'</header>" << "\n";
634  const std::vector<std::string> recruit_links =
635  make_unit_links_list( recruit_ids, true );
636  for (const std::string &link : recruit_links) {
637  text << font::unicode_bullet << " " << link << "\n";
638  }
639 
640  const std::string name = f["name"];
641  const std::string ref_id = faction_prefix + era["id"] + "_" + id;
642  topics.emplace_back(name, ref_id, text.str());
643  }
644  if (sort_generated)
645  std::sort(topics.begin(), topics.end(), title_less());
646  return topics;
647 }
648 
649 std::vector<topic> generate_trait_topics(const bool sort_generated)
650 {
651  std::vector<topic> topics;
652  std::map<t_string, const config> trait_list;
653 
654  for (const config & trait : unit_types.traits()) {
655  const std::string trait_id = trait["id"];
656  trait_list.emplace(trait_id, trait);
657  }
658 
659 
660  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
661  {
662  const unit_type &type = i.second;
663  if (description_type(type) == FULL_DESCRIPTION) {
664  if (config::const_child_itors traits = type.possible_traits()) {
665  for (const config & trait : traits) {
666  const std::string trait_id = trait["id"];
667  trait_list.emplace(trait_id, trait);
668  }
669  }
670  if (const unit_race *r = unit_types.find_race(type.race_id())) {
671  for (const config & trait : r->additional_traits()) {
672  const std::string trait_id = trait["id"];
673  trait_list.emplace(trait_id, trait);
674  }
675  }
676  }
677  }
678 
679  for (std::map<t_string, const config>::iterator a = trait_list.begin(); a != trait_list.end(); ++a) {
680  std::string id = "traits_" + a->first;
681  const config trait = a->second;
682 
683  std::string name = trait["male_name"].str();
684  if (name.empty()) name = trait["female_name"].str();
685  if (name.empty()) name = trait["name"].str();
686  if (name.empty()) continue; // Hidden trait
687 
688  std::stringstream text;
689  if (!trait["help_text"].empty()) {
690  text << trait["help_text"];
691  } else if (!trait["description"].empty()) {
692  text << trait["description"];
693  } else {
694  text << _("No description available.");
695  }
696  text << "\n\n";
697  if (trait["availability"] == "musthave") {
698  text << _("Availability: ") << _("Must-have") << "\n";
699  } else if (trait["availability"] == "none") {
700  text << _("Availability: ") << _("Unavailable") << "\n";
701  }
702  topics.emplace_back(name, id, text.str());
703  }
704 
705  if (sort_generated)
706  std::sort(topics.begin(), topics.end(), title_less());
707  return topics;
708 }
709 
710 
711 std::string make_unit_link(const std::string& type_id)
712 {
713  std::string link;
714 
716  if (!type) {
717  std::cerr << "Unknown unit type : " << type_id << "\n";
718  // don't return an hyperlink (no page)
719  // instead show the id (as hint)
720  link = type_id;
721  } else if (!type->hide_help()) {
722  std::string name = type->type_name();
723  std::string ref_id;
724  if (description_type(*type) == FULL_DESCRIPTION) {
725  const std::string section_prefix = type->show_variations_in_help() ? ".." : "";
726  ref_id = section_prefix + unit_prefix + type->id();
727  } else {
728  ref_id = unknown_unit_topic;
729  name += " (?)";
730  }
731  link = make_link(name, ref_id);
732  } // if hide_help then link is an empty string
733 
734  return link;
735 }
736 
737 std::vector<std::string> make_unit_links_list(const std::vector<std::string>& type_id_list, bool ordered)
738 {
739  std::vector<std::string> links_list;
740  for (const std::string &type_id : type_id_list) {
741  std::string unit_link = make_unit_link(type_id);
742  if (!unit_link.empty())
743  links_list.push_back(unit_link);
744  }
745 
746  if (ordered)
747  std::sort(links_list.begin(), links_list.end());
748 
749  return links_list;
750 }
751 
752 void generate_races_sections(const config *help_cfg, section &sec, int level)
753 {
754  std::set<std::string, string_less> races;
755  std::set<std::string, string_less> visible_races;
756 
757  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
758  {
759  const unit_type &type = i.second;
760  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
761  if (desc_type == FULL_DESCRIPTION) {
762  races.insert(type.race_id());
763  if (!type.hide_help())
764  visible_races.insert(type.race_id());
765  }
766  }
767 
768  for(std::set<std::string, string_less>::iterator it = races.begin(); it != races.end(); ++it) {
769  section race_section;
770  config section_cfg;
771 
772  bool hidden = (visible_races.count(*it) == 0);
773 
774  section_cfg["id"] = hidden_symbol(hidden) + race_prefix + *it;
775 
776  std::string title;
777  if (const unit_race *r = unit_types.find_race(*it)) {
778  title = r->plural_name();
779  } else {
780  title = _ ("race^Miscellaneous");
781  }
782  section_cfg["title"] = title;
783 
784  section_cfg["sections_generator"] = "units:" + *it;
785  section_cfg["generator"] = "units:" + *it;
786 
787  parse_config_internal(help_cfg, &section_cfg, race_section, level+1);
788  sec.add_section(race_section);
789  }
790 }
791 
792 void generate_era_sections(const config* help_cfg, section & sec, int level)
793 {
794  for (const config & era : game_cfg->child_range("era")) {
795  if (era["hide_help"].to_bool()) {
796  continue;
797  }
798 
799  DBG_HP << "Adding help section: " << era["id"].str() << "\n";
800 
801  section era_section;
802  config section_cfg;
803  section_cfg["id"] = era_prefix + era["id"].str();
804  section_cfg["title"] = era["name"];
805 
806  section_cfg["generator"] = "era:" + era["id"].str();
807 
808  DBG_HP << section_cfg.debug() << "\n";
809 
810  parse_config_internal(help_cfg, &section_cfg, era_section, level+1);
811  sec.add_section(era_section);
812  }
813 }
814 
815 void generate_terrain_sections(const config* /*help_cfg*/, section& sec, int /*level*/)
816 {
818 
819  if (!tdata) {
820  WRN_HP << "When building terrain help sections, couldn't acquire terrain types data, aborting.\n";
821  return;
822  }
823 
824  std::map<std::string, section> base_map;
825 
826  const t_translation::ter_list& t_listi = tdata->list();
827 
828  for (const t_translation::terrain_code& t : t_listi) {
829 
830  const terrain_type& info = tdata->get_terrain_info(t);
831 
832  bool hidden = info.is_combined() || info.hide_help();
833 
835  == preferences::encountered_terrains().end() && !info.is_overlay())
836  hidden = true;
837 
838  topic terrain_topic;
839  terrain_topic.title = info.editor_name();
840  terrain_topic.id = hidden_symbol(hidden) + terrain_prefix + info.id();
841  terrain_topic.text = std::make_shared<terrain_topic_generator>(info);
842 
843  t_translation::ter_list base_terrains = tdata->underlying_union_terrain(t);
844  for (const t_translation::terrain_code& base : base_terrains) {
845 
846  const terrain_type& base_info = tdata->get_terrain_info(base);
847 
848  if (!base_info.is_nonnull() || base_info.hide_help())
849  continue;
850 
851  section& base_section = base_map[base_info.id()];
852 
853  base_section.id = terrain_prefix + base_info.id();
854  base_section.title = base_info.editor_name();
855 
856  if (base_info.id() == info.id())
857  terrain_topic.id = ".." + terrain_prefix + info.id();
858  base_section.topics.push_back(terrain_topic);
859  }
860  }
861 
862  for (const auto& base : base_map) {
863  sec.add_section(base.second);
864  }
865 }
866 
867 void generate_unit_sections(const config* /*help_cfg*/, section& sec, int level, const bool /*sort_generated*/, const std::string& race)
868 {
869  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
870  const unit_type &type = i.second;
871 
872  if (type.race_id() != race)
873  continue;
874 
875  if (!type.show_variations_in_help())
876  continue;
877 
878  section base_unit;
879  for (const std::string &variation_id : type.variations()) {
880  // TODO: Do we apply encountered stuff to variations?
881  const unit_type &var_type = type.get_variation(variation_id);
882  const std::string topic_name = var_type.type_name() + "\n" + var_type.variation_name();
883  const std::string var_ref = hidden_symbol(var_type.hide_help()) + variation_prefix + var_type.id() + "_" + variation_id;
884 
885  topic var_topic(topic_name, var_ref, "");
886  var_topic.text = std::make_shared<unit_topic_generator>(var_type, variation_id);
887  base_unit.topics.push_back(var_topic);
888  }
889 
890  const std::string type_name = type.type_name();
891  const std::string ref_id = hidden_symbol(type.hide_help()) + unit_prefix + type.id();
892 
893  base_unit.id = ref_id;
894  base_unit.title = type_name;
895  base_unit.level = level +1;
896 
897  sec.add_section(base_unit);
898  }
899 }
900 
901 std::vector<topic> generate_unit_topics(const bool sort_generated, const std::string& race)
902 {
903  std::vector<topic> topics;
904  std::set<std::string, string_less> race_units;
905  std::set<std::string, string_less> race_topics;
906  std::set<std::string> alignments;
907 
908  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
909  {
910  const unit_type &type = i.second;
911 
912  if (type.race_id() != race)
913  continue;
914 
915  UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
916  if (desc_type != FULL_DESCRIPTION)
917  continue;
918 
919  const std::string debug_suffix = (game_config::debug ? " (" + type.id() + ")" : "");
920  const std::string type_name = type.type_name() + (type.id() == type.type_name().str() ? "" : debug_suffix);
921  const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
922  const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
923  topic unit_topic(type_name, ref_id, "");
924  unit_topic.text = std::make_shared<unit_topic_generator>(type);
925  topics.push_back(unit_topic);
926 
927  if (!type.hide_help()) {
928  // we also record an hyperlink of this unit
929  // in the list used for the race topic
930  std::string link = make_link(type_name, ref_id);
931  race_units.insert(link);
932 
933  alignments.insert(make_link(type.alignment_description(type.alignment(), type.genders().front()), "time_of_day"));
934  }
935  }
936 
937  //generate the hidden race description topic
938  std::string race_id = "..race_"+race;
939  std::string race_name;
940  std::string race_description;
941  if (const unit_race *r = unit_types.find_race(race)) {
942  race_name = r->plural_name();
943  race_description = r->description();
944  // if (description.empty()) description = _("No description Available");
945  for (const config &additional_topic : r->additional_topics())
946  {
947  std::string id = additional_topic["id"];
948  std::string title = additional_topic["title"];
949  std::string text = additional_topic["text"];
950  //topic additional_topic(title, id, text);
951  topics.emplace_back(title,id,text);
952  std::string link = make_link(title, id);
953  race_topics.insert(link);
954  }
955  } else {
956  race_name = _ ("race^Miscellaneous");
957  // description = _("Here put the description of the Miscellaneous race");
958  }
959 
960  std::stringstream text;
961 
962  if (!race_description.empty()) {
963  text << race_description << "\n\n";
964  }
965 
966  if (!alignments.empty()) {
967  std::set<std::string>::iterator it = alignments.begin();
968  text << (alignments.size() > 1 ? _("Alignments: ") : _("Alignment: ")) << *(it++);
969  while(it != alignments.end()) {
970  text << ", " << *(it++);
971  }
972  text << "\n\n";
973  }
974 
975  text << _("<header>text='Units of this race'</header>") << "\n";
976  for (std::set<std::string, string_less>::iterator u = race_units.begin(); u != race_units.end(); ++u) {
977  text << font::unicode_bullet << " " << (*u) << "\n";
978  }
979 
980  topics.emplace_back(race_name, race_id, text.str());
981 
982  if (sort_generated)
983  std::sort(topics.begin(), topics.end(), title_less());
984 
985  return topics;
986 }
987 
989 {
992  return FULL_DESCRIPTION;
993  }
994 
995  const std::set<std::string> &encountered_units = preferences::encountered_units();
996  if (encountered_units.find(type.id()) != encountered_units.end()) {
997  return FULL_DESCRIPTION;
998  }
999  return NO_DESCRIPTION;
1000 }
1001 
1002 std::string generate_contents_links(const std::string& section_name, config const *help_cfg)
1003 {
1004  const config& section_cfg = help_cfg->find_child("section", "id", section_name);
1005  if (!section_cfg) {
1006  return std::string();
1007  }
1008 
1009  std::ostringstream res;
1010 
1011  std::vector<std::string> topics = utils::quoted_split(section_cfg["topics"]);
1012 
1013  // we use an intermediate structure to allow a conditional sorting
1014  typedef std::pair<std::string,std::string> link;
1015  std::vector<link> topics_links;
1016 
1018  // Find all topics in this section.
1019  for (t = topics.begin(); t != topics.end(); ++t) {
1020  if (const config& topic_cfg = help_cfg->find_child("topic", "id", *t)) {
1021  std::string id = topic_cfg["id"];
1022  if (is_visible_id(id))
1023  topics_links.emplace_back(topic_cfg["title"], id);
1024  }
1025  }
1026 
1027  if (section_cfg["sort_topics"] == "yes") {
1028  std::sort(topics_links.begin(),topics_links.end());
1029  }
1030 
1032  for (l = topics_links.begin(); l != topics_links.end(); ++l) {
1033  std::string link = make_link(l->first, l->second);
1034  res << font::unicode_bullet << " " << link << "\n";
1035  }
1036 
1037  return res.str();
1038 }
1039 
1040 std::string generate_contents_links(const section &sec, const std::vector<topic>& topics)
1041 {
1042  std::stringstream res;
1043 
1044  for (auto &s : sec.sections) {
1045  if (is_visible_id(s.id)) {
1046  std::string link = make_link(s.title, ".."+s.id);
1047  res << font::unicode_bullet << " " << link << "\n";
1048  }
1049  }
1050 
1051  for (auto &t : topics) {
1052  if (is_visible_id(t.id)) {
1053  std::string link = make_link(t.title, t.id);
1054  res << font::unicode_bullet << " " << link << "\n";
1055  }
1056  }
1057  return res.str();
1058 }
1059 
1060 bool topic::operator==(const topic &t) const
1061 {
1062  return t.id == id;
1063 }
1064 
1065 bool topic::operator<(const topic &t) const
1066 {
1067  return id < t.id;
1068 }
1069 
1070 bool section::operator==(const section &sec) const
1071 {
1072  return sec.id == id;
1073 }
1074 
1075 bool section::operator<(const section &sec) const
1076 {
1077  return id < sec.id;
1078 }
1079 
1081 {
1082  sections.emplace_back(s);
1083 }
1084 
1086 {
1087  topics.clear();
1088  sections.clear();
1089 }
1090 
1091 
1092 
1093 const topic *find_topic(const section &sec, const std::string &id)
1094 {
1095  topic_list::const_iterator tit =
1096  std::find_if(sec.topics.begin(), sec.topics.end(), has_id(id));
1097  if (tit != sec.topics.end()) {
1098  return &(*tit);
1099  }
1100  for (const auto &s : sec.sections) {
1101  const auto t = find_topic(s, id);
1102  if (t != nullptr) {
1103  return t;
1104  }
1105  }
1106  return nullptr;
1107 }
1108 
1109 const section *find_section(const section &sec, const std::string &id)
1110 {
1111  const auto &sit =
1112  std::find_if(sec.sections.begin(), sec.sections.end(), has_id(id));
1113  if (sit != sec.sections.end()) {
1114  return &*sit;
1115  }
1116  for (const auto &subsection : sec.sections) {
1117  const auto s = find_section(subsection, id);
1118  if (s != nullptr) {
1119  return s;
1120  }
1121  }
1122  return nullptr;
1123 }
1124 
1125 std::vector<std::string> parse_text(const std::string &text)
1126 {
1127  std::vector<std::string> res;
1128  bool last_char_escape = false;
1129  const char escape_char = '\\';
1130  std::stringstream ss;
1131  std::size_t pos;
1132  enum { ELEMENT_NAME, OTHER } state = OTHER;
1133  for (pos = 0; pos < text.size(); ++pos) {
1134  const char c = text[pos];
1135  if (c == escape_char && !last_char_escape) {
1136  last_char_escape = true;
1137  }
1138  else {
1139  if (state == OTHER) {
1140  if (c == '<') {
1141  if (last_char_escape) {
1142  ss << c;
1143  }
1144  else {
1145  res.push_back(ss.str());
1146  ss.str("");
1147  state = ELEMENT_NAME;
1148  }
1149  }
1150  else {
1151  ss << c;
1152  }
1153  }
1154  else if (state == ELEMENT_NAME) {
1155  if (c == '/') {
1156  std::string msg = "Erroneous / in element name.";
1157  throw parse_error(msg);
1158  }
1159  else if (c == '>') {
1160  // End of this name.
1161  std::stringstream s;
1162  const std::string element_name = ss.str();
1163  ss.str("");
1164  s << "</" << element_name << ">";
1165  const std::string end_element_name = s.str();
1166  std::size_t end_pos = text.find(end_element_name, pos);
1167  if (end_pos == std::string::npos) {
1168  std::stringstream msg;
1169  msg << "Unterminated element: " << element_name;
1170  throw parse_error(msg.str());
1171  }
1172  s.str("");
1173  const std::string contents = text.substr(pos + 1, end_pos - pos - 1);
1174  const std::string element = convert_to_wml(element_name, contents);
1175  res.push_back(element);
1176  pos = end_pos + end_element_name.size() - 1;
1177  state = OTHER;
1178  }
1179  else {
1180  ss << c;
1181  }
1182  }
1183  last_char_escape = false;
1184  }
1185  }
1186  if (state == ELEMENT_NAME) {
1187  std::stringstream msg;
1188  msg << "Element '" << ss.str() << "' continues through end of string.";
1189  throw parse_error(msg.str());
1190  }
1191  if (!ss.str().empty()) {
1192  // Add the last string.
1193  res.push_back(ss.str());
1194  }
1195  return res;
1196 }
1197 
1198 std::string convert_to_wml(const std::string &element_name, const std::string &contents)
1199 {
1200  std::stringstream ss;
1201  bool in_quotes = false;
1202  bool last_char_escape = false;
1203  const char escape_char = '\\';
1204  std::vector<std::string> attributes;
1205  // Find the different attributes.
1206  // No checks are made for the equal sign or something like that.
1207  // Attributes are just separated by spaces or newlines.
1208  // Attributes that contain spaces must be in single quotes.
1209  for (std::size_t pos = 0; pos < contents.size(); ++pos) {
1210  const char c = contents[pos];
1211  if (c == escape_char && !last_char_escape) {
1212  last_char_escape = true;
1213  }
1214  else {
1215  if (c == '\'' && !last_char_escape) {
1216  ss << '"';
1217  in_quotes = !in_quotes;
1218  }
1219  else if ((c == ' ' || c == '\n') && !last_char_escape && !in_quotes) {
1220  // Space or newline, end of attribute.
1221  attributes.push_back(ss.str());
1222  ss.str("");
1223  }
1224  else {
1225  ss << c;
1226  }
1227  last_char_escape = false;
1228  }
1229  }
1230  if (in_quotes) {
1231  std::stringstream msg;
1232  msg << "Unterminated single quote after: '" << ss.str() << "'";
1233  throw parse_error(msg.str());
1234  }
1235  if (!ss.str().empty()) {
1236  attributes.push_back(ss.str());
1237  }
1238  ss.str("");
1239  // Create the WML.
1240  ss << "[" << element_name << "]\n";
1241  for (std::vector<std::string>::const_iterator it = attributes.begin();
1242  it != attributes.end(); ++it) {
1243  ss << *it << "\n";
1244  }
1245  ss << "[/" << element_name << "]\n";
1246  return ss.str();
1247 }
1248 
1249 color_t string_to_color(const std::string &cmp_str)
1250 {
1251  if (cmp_str == "green") {
1252  return font::GOOD_COLOR;
1253  }
1254  if (cmp_str == "red") {
1255  return font::BAD_COLOR;
1256  }
1257  if (cmp_str == "black") {
1258  return font::BLACK_COLOR;
1259  }
1260  if (cmp_str == "yellow") {
1261  return font::YELLOW_COLOR;
1262  }
1263  if (cmp_str == "white") {
1264  return font::BIGMAP_COLOR;
1265  }
1266  // a #rrggbb color in pango format.
1267  if (*cmp_str.c_str() == '#' && cmp_str.size() == 7) {
1268  return color_t::from_argb_bytes(strtoul(cmp_str.c_str() + 1, nullptr, 16));
1269  }
1270  return font::NORMAL_COLOR;
1271 }
1272 
1273 std::vector<std::string> split_in_width(const std::string &s, const int font_size,
1274  const unsigned width)
1275 {
1276  std::vector<std::string> res;
1277  try {
1278  const std::string& first_line = font::word_wrap_text(s, font_size, width, -1, 1, true);
1279  res.push_back(first_line);
1280  if(s.size() > first_line.size()) {
1281  res.push_back(s.substr(first_line.size()));
1282  }
1283  }
1285  {
1286  throw parse_error (_("corrupted original file"));
1287  }
1288 
1289  return res;
1290 }
1291 
1292 std::string remove_first_space(const std::string& text)
1293 {
1294  if (text.length() > 0 && text[0] == ' ') {
1295  return text.substr(1);
1296  }
1297  return text;
1298 }
1299 
1300 std::string get_first_word(const std::string &s)
1301 {
1302  std::size_t first_word_start = s.find_first_not_of(' ');
1303  if (first_word_start == std::string::npos) {
1304  return s;
1305  }
1306  std::size_t first_word_end = s.find_first_of(" \n", first_word_start);
1307  if( first_word_end == first_word_start ) {
1308  // This word is '\n'.
1309  first_word_end = first_word_start+1;
1310  }
1311 
1312  //if no gap(' ' or '\n') found, test if it is CJK character
1313  std::string re = s.substr(0, first_word_end);
1314 
1315  utf8::iterator ch(re);
1316  if (ch == utf8::iterator::end(re))
1317  return re;
1318 
1319  char32_t firstchar = *ch;
1320  if (font::is_cjk_char(firstchar)) {
1321  re = unicode_cast<std::string>(firstchar);
1322  }
1323  return re;
1324 }
1325 
1327 {
1328  default_toplevel.clear();
1329  hidden_sections.clear();
1330  if (game_cfg != nullptr) {
1331  const config *help_config = &game_cfg->child("help");
1332  if (!*help_config) {
1333  help_config = &dummy_cfg;
1334  }
1335  try {
1336  default_toplevel = parse_config(help_config);
1337  // Create a config object that contains everything that is
1338  // not referenced from the toplevel element. Read this
1339  // config and save these sections and topics so that they
1340  // can be referenced later on when showing help about
1341  // specified things, but that should not be shown when
1342  // opening the help browser in the default manner.
1343  config hidden_toplevel;
1344  std::stringstream ss;
1345  for (const config &section : help_config->child_range("section"))
1346  {
1347  const std::string id = section["id"];
1348  if (find_section(default_toplevel, id) == nullptr) {
1349  // This section does not exist referenced from the
1350  // toplevel. Hence, add it to the hidden ones if it
1351  // is not referenced from another section.
1352  if (!section_is_referenced(id, *help_config)) {
1353  if (!ss.str().empty()) {
1354  ss << ",";
1355  }
1356  ss << id;
1357  }
1358  }
1359  }
1360  hidden_toplevel["sections"] = ss.str();
1361  ss.str("");
1362  for (const config &topic : help_config->child_range("topic"))
1363  {
1364  const std::string id = topic["id"];
1365  if (find_topic(default_toplevel, id) == nullptr) {
1366  if (!topic_is_referenced(id, *help_config)) {
1367  if (!ss.str().empty()) {
1368  ss << ",";
1369  }
1370  ss << id;
1371  }
1372  }
1373  }
1374  hidden_toplevel["topics"] = ss.str();
1375  config hidden_cfg = *help_config;
1376  // Change the toplevel to our new, custom built one.
1377  hidden_cfg.clear_children("toplevel");
1378  hidden_cfg.add_child("toplevel", std::move(hidden_toplevel));
1379  hidden_sections = parse_config(&hidden_cfg);
1380  }
1381  catch (parse_error& e) {
1382  std::stringstream msg;
1383  msg << "Parse error when parsing help text: '" << e.message << "'";
1384  std::cerr << msg.str() << std::endl;
1385  }
1386  }
1387 }
1388 
1389 // id starting with '.' are hidden
1390 std::string hidden_symbol(bool hidden) {
1391  return (hidden ? "." : "");
1392 }
1393 
1394 bool is_visible_id(const std::string &id) {
1395  return (id.empty() || id[0] != '.');
1396 }
1397 
1398 /// Return true if the id is valid for user defined topics and
1399 /// sections. Some IDs are special, such as toplevel and may not be
1400 /// be defined in the config.
1401 bool is_valid_id(const std::string &id) {
1402  if (id == "toplevel") {
1403  return false;
1404  }
1405  if (id.compare(0, unit_prefix.length(), unit_prefix) == 0 || id.compare(hidden_symbol().length(), unit_prefix.length(), unit_prefix) == 0) {
1406  return false;
1407  }
1408  if (id.compare(0, 8, "ability_") == 0) {
1409  return false;
1410  }
1411  if (id.compare(0, 14, "weaponspecial_") == 0) {
1412  return false;
1413  }
1414  if (id == "hidden") {
1415  return false;
1416  }
1417  return true;
1418 }
1419 
1420 
1421 // Return the width for the image with filename.
1422 unsigned image_width(const std::string &filename)
1423 {
1424  image::locator loc(filename);
1425  surface surf(image::get_image(loc));
1426  if (surf != nullptr) {
1427  return surf->w;
1428  }
1429  return 0;
1430 }
1431 
1432 void push_tab_pair(std::vector<help::item> &v, const std::string &s, const boost::optional<std::string> &image, unsigned padding)
1433 {
1434  help::item item(s, font::line_width(s, normal_font_size));
1435  if (image) {
1436  // If the image doesn't exist, don't add padding.
1437  auto width = image_width(image.get());
1438  padding = (width ? padding : 0);
1439 
1440  item.first = "<img>src='" + image.get() + "'</img>" + (padding ? jump(padding) : "") + s;
1441  item.second += width + padding;
1442  }
1443  v.emplace_back(item);
1444 }
1445 
1446 std::string generate_table(const table_spec &tab, const unsigned int spacing)
1447 {
1448  table_spec::const_iterator row_it;
1449  std::vector<std::pair<std::string, unsigned>>::const_iterator col_it;
1450  unsigned int num_cols = 0;
1451  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1452  if (row_it->size() > num_cols) {
1453  num_cols = row_it->size();
1454  }
1455  }
1456  std::vector<unsigned int> col_widths(num_cols, 0);
1457  // Calculate the width of all columns, including spacing.
1458  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1459  unsigned int col = 0;
1460  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1461  if (col_widths[col] < col_it->second + spacing) {
1462  col_widths[col] = col_it->second + spacing;
1463  }
1464  ++col;
1465  }
1466  }
1467  std::vector<unsigned int> col_starts(num_cols);
1468  // Calculate the starting positions of all columns
1469  for (unsigned int i = 0; i < num_cols; ++i) {
1470  unsigned int this_col_start = 0;
1471  for (unsigned int j = 0; j < i; ++j) {
1472  this_col_start += col_widths[j];
1473  }
1474  col_starts[i] = this_col_start;
1475  }
1476  std::stringstream ss;
1477  for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
1478  unsigned int col = 0;
1479  for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
1480  ss << jump_to(col_starts[col]) << col_it->first;
1481  ++col;
1482  }
1483  ss << "\n";
1484  }
1485  return ss.str();
1486 }
1487 
1488 /// Prepend all chars with meaning inside attributes with a backslash.
1489 std::string escape(const std::string &s)
1490 {
1491  return utils::escape(s, "'\\");
1492 }
1493 
1494 /// Load the appropriate terrain types data to use
1496  if (display::get_singleton()) {
1498  } else if (game_config_manager::get()){
1500  } else {
1501  return ter_data_cache();
1502  }
1503 }
1504 
1505 
1506 } // 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:711
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:649
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:901
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:1270
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:867
const std::string topic_img
Definition: help_impl.cpp:83
void clear_children(T... keys)
Definition: config.hpp:477
std::string era()
Definition: game.cpp:709
const int title_size
Definition: help_impl.cpp:78
const ter_data_cache & terrain_types() const
bool is_overlay() const
Definition: terrain.hpp:134
const std::string closed_section_img
Definition: help_impl.cpp:84
std::string remove_first_space(const std::string &text)
Definition: help_impl.cpp:1292
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:1075
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:1528
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:1422
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:1249
const unit_type_map & types() const
Definition: types.hpp:363
bool is_visible_id(const std::string &id)
Definition: help_impl.cpp:1394
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:737
A single unit type that the player may recruit.
Definition: types.hpp:42
bool is_nonnull() const
True if this object represents some sentinel values.
Definition: terrain.hpp:107
void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level)
Dispatch generators to their appropriate functions.
Definition: help_impl.cpp: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:1109
std::string id
Definition: help_impl.hpp:132
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
To be used as a function object to locate sections and topics with a specified ID.
Definition: help_impl.hpp:169
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
const t_string & editor_name() const
Definition: terrain.hpp:34
const color_t BIGMAP_COLOR
The text displayed in a topic.
Definition: help_impl.hpp:77
bool is_combined() const
Definition: terrain.hpp:135
std::string generate_table(const table_spec &tab, const unsigned int spacing)
Definition: help_impl.cpp:1446
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:1432
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:240
std::vector< std::vector< help::item > > table_spec
Definition: help_impl.hpp:373
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1390
bool hide_help() const
Definition: types.cpp:589
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:815
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:944
const std::vector< ability_metadata > & abilities_metadata() const
Definition: types.hpp:210
std::string race_id() const
Returns the ID of this type&#39;s race without the need to build the type.
Definition: types.hpp:263
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:1495
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
std::shared_ptr< topic_generator > generator_
Definition: help_impl.hpp:80
const config::const_child_itors traits() const
Definition: types.hpp:365
void parse_config_internal(const config *help_cfg, const config *section_cfg, section &sec, int level)
Recursive function used by parse_config.
Definition: help_impl.cpp:145
static color_t from_argb_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:87
const int box_width
Definition: help_impl.cpp:80
std::string escape(const std::string &str, const char *special_chars)
Prepends a configurable set of characters with a backslash.
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:210
bool section_is_referenced(const std::string &section_id, const config &cfg)
Return true if the section with id section_id is referenced from another section in the config...
Definition: help_impl.cpp:97
bool topic_is_referenced(const std::string &topic_id, const config &cfg)
Return true if the topic with id topic_id is referenced from another section in the config...
Definition: help_impl.cpp:121
To be used as a function object when sorting section lists on the title.
Definition: help_impl.hpp: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:1415
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:771
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:1198
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:788
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:1065
void generate_races_sections(const config *help_cfg, section &sec, int level)
Definition: help_impl.cpp:752
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:213
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:1489
std::vector< std::string > parse_text(const std::string &text)
Parse a text string.
Definition: help_impl.cpp:1125
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:224
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:1002
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:471
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1326
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:1060
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:1093
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:508
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:1401
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
static std::string alignment_description(ALIGNMENT align, unit_race::GENDER gender=unit_race::MALE)
mock_char c
const std::string & str() const
Definition: tstring.hpp:186
std::string get_first_word(const std::string &s)
Return the first word in s, not removing any spaces in the start of it.
Definition: help_impl.cpp:1300
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:1080
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:988
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:792
const std::string & variation_name() const
Definition: types.hpp:161
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:1273
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:1070
std::shared_ptr< terrain_type_data > ter_data_cache
help::section default_toplevel
Definition: help_impl.cpp:67