The Battle for Wesnoth  1.15.2+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 {
59  for(auto& generator : name_generator_) {
60  generator.reset(new name_generator());
61  }
62 }
63 
65  cfg_(cfg),
66  id_(cfg["id"]),
67  icon_(cfg["editor_icon"]),
68  plural_name_(cfg["plural_name"].t_str()),
69  description_(cfg["description"].t_str()),
70  ntraits_(cfg["num_traits"]),
71  traits_(cfg.child_range("trait")),
72  topics_(cfg.child_range("topic")),
73  global_traits_(!cfg["ignore_global_traits"].to_bool()),
74  undead_variation_(cfg["undead_variation"])
75 
76 {
77  if (id_.empty()) {
78  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing an id field.";
79  }
80  if (plural_name_.empty()) {
81  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing a plural_name field.";
82  plural_name_ = (cfg["name"]);
83  }
84 
85  // use "name" if "male_name" or "female_name" aren't available
86  name_[MALE] = cfg["male_name"];
87  if(name_[MALE].empty()) {
88  name_[MALE] = (cfg["name"]);
89  }
90  name_[FEMALE] = cfg["female_name"];
91  if(name_[FEMALE].empty()) {
92  name_[FEMALE] = (cfg["name"]);
93  }
94 
95  name_generator_factory generator_factory = name_generator_factory(cfg, {"male", "female"});
96 
97  for(int i=MALE; i<NUM_GENDERS; i++) {
98  GENDER gender = static_cast<GENDER>(i);
99  name_generator_[i] = generator_factory.get_name_generator(gender_string(gender));
100  }
101 }
102 
104 {
105  return name_generator_[gender]->generate();
106 }
107 
109 {
110  return *name_generator_[gender];
111 }
112 
114 {
115  return global_traits_;
116 }
117 
119 {
120  return traits_;
121 }
122 
124 {
125  return topics_;
126 }
127 
128 unsigned int unit_race::num_traits() const { return ntraits_; }
129 
130 
131 const std::string& gender_string(unit_race::GENDER gender) {
132  switch(gender) {
133  case unit_race::FEMALE:
134  return unit_race::s_female;
135  default:
136  case unit_race::MALE:
137  return unit_race::s_male;
138  }
139 }
140 
141 unit_race::GENDER string_gender(const std::string& str, unit_race::GENDER def) {
142  if ( str == unit_race::s_male ) {
143  return unit_race::MALE;
144  } else if ( str == unit_race::s_female ) {
145  return unit_race::FEMALE;
146  }
147  return def;
148 }
149 
150 std::string unit_race::get_icon_path_stem() const
151 {
152  if(!icon_.empty()) {
153  return icon_;
154  }
155 
156  std::string path = "icons/unit-groups/race_" + id_;
157 
158  // FIXME: hardcoded '30' is bad...
159  if(!filesystem::file_exists(filesystem::get_binary_file_location("images", path + "_30.png"))) {
160  path = "icons/unit-groups/race_custom";
161  }
162 
163  return path;
164 }
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:80
unsigned int ntraits_
Definition: race.hpp:79
std::string undead_variation_
Definition: race.hpp:85
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:266
const config::const_child_itors & additional_topics() const
Definition: race.cpp:123
const name_generator & generator(GENDER gender) const
Definition: race.cpp:108
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::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:66
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
Definition: race.cpp:141
std::array< t_string, NUM_GENDERS > name_
Definition: race.hpp:76
std::string path
Definition: game_config.cpp:39
t_string description_
Definition: race.hpp:78
const config::const_child_itors & additional_traits() const
Definition: race.cpp:118
unsigned int num_traits() const
Definition: race.cpp:128
const std::string & gender_string(unit_race::GENDER gender)
Definition: race.cpp:131
t_string description_
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:210
config::const_child_itors topics_
Definition: race.hpp:83
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:74
std::string icon_
Definition: race.hpp:75
static const config & empty_topics()
Definition: race.cpp:42
bool uses_global_traits() const
Definition: race.cpp:113
const config cfg_
Definition: race.hpp:72
Declarations for File-IO.
std::string id_
bool empty() const
Definition: tstring.hpp:182
t_string plural_name_
Definition: race.hpp:77
unit_race()
Only used to construct null_race.
Definition: race.cpp:47
Standard logging facilities (interface).
config::const_child_itors traits_
Definition: race.hpp:82
std::string get_icon_path_stem() const
Gets this race&#39;s icon path without state/size suffix and extension.
Definition: race.cpp:150
bool global_traits_
Definition: race.hpp:84
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:103