The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
race.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
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 /**
16  * @file
17  * Generate race-specific unit-names.
18  */
19 
20 #include "global.hpp"
21 
22 #include "units/race.hpp"
23 
24 #include "log.hpp"
29 
30 /// Dummy race used when a race is not yet known.
32 /// Standard string id (not translatable) for FEMALE
33 const std::string unit_race::s_female("female");
34 /// Standard string id (not translatable) for MALE
35 const std::string unit_race::s_male("male");
36 
37 
38 static const config &empty_traits() {
39  static config cfg;
40  return cfg;
41 }
42 
43 static const config &empty_topics() {
44  static config cfg;
45  return cfg;
46 }
47 
49  cfg_(),
50  id_(),
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),
58 {
59  name_[MALE] = "";
60  name_[FEMALE] = "";
61 }
62 
64  cfg_(cfg),
65  id_(cfg["id"]),
66  plural_name_(cfg["plural_name"].t_str()),
67  description_(cfg["description"].t_str()),
68  ntraits_(cfg["num_traits"]),
69  traits_(cfg.child_range("trait")),
70  topics_(cfg.child_range("topic")),
71  global_traits_(!cfg["ignore_global_traits"].to_bool()),
72  undead_variation_(cfg["undead_variation"])
73 
74 {
75  if (id_.empty()) {
76  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing an id field.";
77  }
78  if (plural_name_.empty()) {
79  lg::wml_error() << "[race] '" << cfg["name"] << "' is missing a plural_name field.";
80  plural_name_ = (cfg["name"]);
81  }
82  // use "name" if "male_name" or "female_name" aren't available
83  name_[MALE] = cfg["male_name"];
84  if(name_[MALE].empty()) {
85  name_[MALE] = (cfg["name"]);
86  }
87  name_[FEMALE] = cfg["female_name"];
88  if(name_[FEMALE].empty()) {
89  name_[FEMALE] = (cfg["name"]);
90  }
91 
92  config::attribute_value male_generator = cfg["male_name_generator"];
93  config::attribute_value female_generator = cfg["female_name_generator"];
94  if(male_generator.blank()) {
95  male_generator = cfg["name_generator"];
96  }
97  if(female_generator.blank()) {
98  female_generator = cfg["name_generator"];
99  }
100 
101  if(!male_generator.blank()) {
102  name_generator_[MALE].reset(new context_free_grammar_generator(male_generator));
103  if(!name_generator_[MALE]->is_valid()) {
104  name_generator_[MALE].reset();
105  }
106  }
107  if(!female_generator.blank()) {
108  name_generator_[FEMALE].reset(new context_free_grammar_generator(female_generator));
109  if(!name_generator_[FEMALE]->is_valid()) {
110  name_generator_[FEMALE].reset();
111  }
112  }
113 
114  int chain_size = cfg["markov_chain_size"].to_int(2);
115  if(!name_generator_[MALE]) {
116  name_generator_[MALE].reset(new markov_generator(utils::split(cfg["male_names"]), chain_size, 12));
117  }
118  if(!name_generator_[FEMALE]) {
119  name_generator_[FEMALE].reset(new markov_generator(utils::split(cfg["female_names"]), chain_size, 12));
120  }
121 }
122 
124 {
125  return name_generator_[gender]->generate();
126 }
127 
129 {
130  return *name_generator_[gender];
131 }
132 
134 {
135  return global_traits_;
136 }
137 
139 {
140  return traits_;
141 }
142 
144 {
145  return topics_;
146 }
147 
148 unsigned int unit_race::num_traits() const { return ntraits_; }
149 
150 
152  switch(gender) {
153  case unit_race::FEMALE:
154  return unit_race::s_female;
155  default:
156  case unit_race::MALE:
157  return unit_race::s_male;
158  }
159 }
160 
162  if ( str == unit_race::s_male ) {
163  return unit_race::MALE;
164  } else if ( str == unit_race::s_female ) {
165  return unit_race::FEMALE;
166  }
167  return def;
168 }
static const config & empty_traits()
Definition: race.cpp:38
static const std::string s_male
Standard string id (not translatable) for MALE.
Definition: race.hpp:27
bool uses_global_traits() const
Definition: race.cpp:133
std::string const & gender_string(unit_race::GENDER gender)
Definition: race.cpp:151
unsigned int ntraits_
Definition: race.hpp:61
std::string id_
Definition: formula.cpp:636
const config::const_child_itors & additional_topics() const
Definition: race.cpp:143
boost::shared_ptr< name_generator > name_generator_[NUM_GENDERS]
Definition: race.hpp:62
t_string name_[NUM_GENDERS]
Definition: race.hpp:58
std::pair< const_child_iterator, const_child_iterator > const_child_itors
Definition: config.hpp:214
Variant for storing WML attributes.
Definition: config.hpp:223
const config::const_child_itors & additional_traits() const
Definition: race.cpp:138
bool blank() const
Tests for an attribute that was never set.
Definition: config.cpp:367
static const std::string s_female
Standard string id (not translatable) for FEMALE.
Definition: race.hpp:26
static const unit_race null_race
Dummy race used when a race is not yet known.
Definition: race.hpp:49
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
Definition: race.cpp:161
unit_race::GENDER gender() const
Definition: unit.hpp:203
config::const_child_itors topics_
Definition: race.hpp:65
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:262
std::string id_
Definition: race.hpp:57
static const config & empty_topics()
Definition: race.cpp:43
const name_generator & generator(GENDER gender) const
Definition: race.cpp:128
std::string generate_name(GENDER gender) const
Definition: race.cpp:123
t_string description_
Definition: unit.hpp:526
t_string plural_name_
Definition: race.hpp:59
unit_race()
Only used to construct null_race.
Definition: race.cpp:48
Standard logging facilities (interface).
config::const_child_itors traits_
Definition: race.hpp:64
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
unsigned int num_traits() const
Definition: race.cpp:148
bool global_traits_
Definition: race.hpp:66
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool empty() const
Definition: tstring.hpp:166
std::string undead_variation_
Definition: unit.hpp:449