The Battle for Wesnoth  1.15.5+dev
race.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 /**
16  * @file
17  * Generate race-specific unit-names.
18  */
19 
20 #include "units/race.hpp"
21 
22 #include "filesystem.hpp"
23 #include "log.hpp"
26 #include "utils/name_generator.hpp"
28 
29 /// Dummy race used when a race is not yet known.
31 /// Standard string id (not translatable) for FEMALE
32 const std::string unit_race::s_female("female");
33 /// Standard string id (not translatable) for MALE
34 const std::string unit_race::s_male("male");
35 
36 
37 static const config &empty_traits() {
38  static config cfg;
39  return cfg;
40 }
41 
42 static const config &empty_topics() {
43  static config cfg;
44  return cfg;
45 }
46 
48  cfg_(),
49  id_(),
50  name_(),
51  plural_name_(),
52  description_(),
53  ntraits_(0),
54  traits_(empty_traits().child_range("trait")),
55  topics_(empty_topics().child_range("topic")),
56  global_traits_(true),
57  undead_variation_(),
58  help_taxonomy_()
59 {
60  for(auto& generator : name_generator_) {
61  generator.reset(new name_generator());
62  }
63 }
64 
66  cfg_(cfg),
67  id_(cfg["id"]),
68  icon_(cfg["editor_icon"]),
69  plural_name_(cfg["plural_name"].t_str()),
70  description_(cfg["description"].t_str()),
71  ntraits_(cfg["num_traits"]),
72  traits_(cfg.child_range("trait")),
73  topics_(cfg.child_range("topic")),
74  global_traits_(!cfg["ignore_global_traits"].to_bool()),
75  undead_variation_(cfg["undead_variation"]),
76  help_taxonomy_(cfg["help_taxonomy"])
77 
78 {
79  if (id_.empty()) {
80  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing an id field.";
81  }
82  if (plural_name_.empty()) {
83  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing a plural_name field.";
84  plural_name_ = (cfg["name"]);
85  }
86 
87  // use "name" if "male_name" or "female_name" aren't available
88  name_[MALE] = cfg["male_name"];
89  if(name_[MALE].empty()) {
90  name_[MALE] = (cfg["name"]);
91  }
92  name_[FEMALE] = cfg["female_name"];
93  if(name_[FEMALE].empty()) {
94  name_[FEMALE] = (cfg["name"]);
95  }
96 
97  name_generator_factory generator_factory = name_generator_factory(cfg, {"male", "female"});
98 
99  for(int i=MALE; i<NUM_GENDERS; i++) {
100  GENDER gender = static_cast<GENDER>(i);
101  name_generator_[i] = generator_factory.get_name_generator(gender_string(gender));
102  }
103 }
104 
106 {
107  return name_generator_[gender]->generate();
108 }
109 
111 {
112  return *name_generator_[gender];
113 }
114 
116 {
117  return global_traits_;
118 }
119 
121 {
122  return traits_;
123 }
124 
126 {
127  return topics_;
128 }
129 
130 unsigned int unit_race::num_traits() const { return ntraits_; }
131 
132 
134  switch(gender) {
135  case unit_race::FEMALE:
136  return unit_race::s_female;
137  default:
138  case unit_race::MALE:
139  return unit_race::s_male;
140  }
141 }
142 
144  if ( str == unit_race::s_male ) {
145  return unit_race::MALE;
146  } else if ( str == unit_race::s_female ) {
147  return unit_race::FEMALE;
148  }
149  return def;
150 }
151 
153 {
154  if(!icon_.empty()) {
155  return icon_;
156  }
157 
158  std::string path = "icons/unit-groups/race_" + id_;
159 
160  // FIXME: hardcoded '30' is bad...
161  if(!filesystem::file_exists(filesystem::get_binary_file_location("images", path + "_30.png"))) {
162  path = "icons/unit-groups/race_custom";
163  }
164 
165  return path;
166 }
static const config & empty_traits()
Definition: race.cpp:37
static const std::string s_male
Standard string id (not translatable) for MALE.
Definition: race.hpp:28
std::array< std::shared_ptr< name_generator >, NUM_GENDERS > name_generator_
Definition: race.hpp:82
unsigned int ntraits_
Definition: race.hpp:81
std::string undead_variation_
Definition: race.hpp:87
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:267
const config::const_child_itors & additional_topics() const
Definition: race.cpp:125
const name_generator & generator(GENDER gender) const
Definition: race.cpp:110
std::string get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type or an empty string if the file isn&#39;t prese...
std::string str
Definition: statement.cpp:110
std::shared_ptr< name_generator > get_name_generator()
Gets the default name generator.
static const std::string s_female
Standard string id (not translatable) for FEMALE.
Definition: race.hpp:27
static const unit_race null_race
Dummy race used when a race is not yet known.
Definition: race.hpp:68
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
Definition: race.cpp:143
std::array< t_string, NUM_GENDERS > name_
Definition: race.hpp:78
std::string path
Definition: game_config.cpp:39
t_string description_
Definition: race.hpp:80
const config::const_child_itors & additional_traits() const
Definition: race.cpp:120
unsigned int num_traits() const
Definition: race.cpp:130
const std::string & gender_string(unit_race::GENDER gender)
Definition: race.cpp:133
t_string description_
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:210
config::const_child_itors topics_
Definition: race.hpp:85
std::size_t i
Definition: function.cpp:933
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
std::string id_
Definition: race.hpp:76
std::string icon_
Definition: race.hpp:77
static const config & empty_topics()
Definition: race.cpp:42
bool uses_global_traits() const
Definition: race.cpp:115
const config cfg_
Definition: race.hpp:74
Declarations for File-IO.
std::string id_
bool empty() const
Definition: tstring.hpp:182
t_string plural_name_
Definition: race.hpp:79
unit_race()
Only used to construct null_race.
Definition: race.cpp:47
std::string help_taxonomy_
Definition: race.hpp:88
Standard logging facilities (interface).
config::const_child_itors traits_
Definition: race.hpp:84
std::string get_icon_path_stem() const
Gets this race&#39;s icon path without state/size suffix and extension.
Definition: race.cpp:152
bool global_traits_
Definition: race.hpp:86
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
std::string generate_name(GENDER gender) const
Definition: race.cpp:105