-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
race.cpp
164 lines (136 loc) · 4.08 KB
/
race.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Generate race-specific unit-names.
*/
#include "units/race.hpp"
#include "filesystem.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode_cast.hpp"
#include "utils/name_generator.hpp"
#include "utils/name_generator_factory.hpp"
/// Dummy race used when a race is not yet known.
const unit_race unit_race::null_race;
/// Standard string id (not translatable) for FEMALE
const std::string unit_race::s_female("female");
/// Standard string id (not translatable) for MALE
const std::string unit_race::s_male("male");
static const config &empty_traits() {
static config cfg;
return cfg;
}
static const config &empty_topics() {
static config cfg;
return cfg;
}
unit_race::unit_race() :
cfg_(),
id_(),
plural_name_(),
description_(),
ntraits_(0),
traits_(empty_traits().child_range("trait")),
topics_(empty_topics().child_range("topic")),
global_traits_(true),
undead_variation_()
{
name_[MALE] = "";
name_[FEMALE] = "";
name_generator_[MALE].reset(new name_generator());
name_generator_[FEMALE].reset(new name_generator());
}
unit_race::unit_race(const config& cfg) :
cfg_(cfg),
id_(cfg["id"]),
icon_(cfg["editor_icon"]),
plural_name_(cfg["plural_name"].t_str()),
description_(cfg["description"].t_str()),
ntraits_(cfg["num_traits"]),
traits_(cfg.child_range("trait")),
topics_(cfg.child_range("topic")),
global_traits_(!cfg["ignore_global_traits"].to_bool()),
undead_variation_(cfg["undead_variation"])
{
if (id_.empty()) {
lg::wml_error() << "[race] '" << cfg["name"] << "' is missing an id field.";
}
if (plural_name_.empty()) {
lg::wml_error() << "[race] '" << cfg["name"] << "' is missing a plural_name field.";
plural_name_ = (cfg["name"]);
}
// use "name" if "male_name" or "female_name" aren't available
name_[MALE] = cfg["male_name"];
if(name_[MALE].empty()) {
name_[MALE] = (cfg["name"]);
}
name_[FEMALE] = cfg["female_name"];
if(name_[FEMALE].empty()) {
name_[FEMALE] = (cfg["name"]);
}
name_generator_factory generator_factory = name_generator_factory(cfg, {"male", "female"});
for(int i=MALE; i<NUM_GENDERS; i++) {
GENDER gender = static_cast<GENDER>(i);
name_generator_[i] = generator_factory.get_name_generator(gender_string(gender));
}
}
std::string unit_race::generate_name(unit_race::GENDER gender) const
{
return name_generator_[gender]->generate();
}
const name_generator& unit_race::generator(unit_race::GENDER gender) const
{
return *name_generator_[gender];
}
bool unit_race::uses_global_traits() const
{
return global_traits_;
}
const config::const_child_itors &unit_race::additional_traits() const
{
return traits_;
}
const config::const_child_itors &unit_race::additional_topics() const
{
return topics_;
}
unsigned int unit_race::num_traits() const { return ntraits_; }
const std::string& gender_string(unit_race::GENDER gender) {
switch(gender) {
case unit_race::FEMALE:
return unit_race::s_female;
default:
case unit_race::MALE:
return unit_race::s_male;
}
}
unit_race::GENDER string_gender(const std::string& str, unit_race::GENDER def) {
if ( str == unit_race::s_male ) {
return unit_race::MALE;
} else if ( str == unit_race::s_female ) {
return unit_race::FEMALE;
}
return def;
}
std::string unit_race::get_icon_path_stem() const
{
if(!icon_.empty()) {
return icon_;
}
std::string path = "icons/unit-groups/race_" + id_;
// FIXME: hardcoded '30' is bad...
if(!filesystem::file_exists(filesystem::get_binary_file_location("images", path + "_30.png"))) {
path = "icons/unit-groups/race_custom";
}
return path;
}