The Battle for Wesnoth  1.19.7+dev
race.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
19 #include "global.hpp"
20 #include "utils/name_generator.hpp"
21 
22 #include <array>
23 #include <memory>
24 
25 class unit_race
26 {
27 public:
29  static const std::string s_female;
30  static const std::string s_male;
31 
32 
33  explicit unit_race(const config& cfg);
34 
35  const config& get_cfg() const { return cfg_; }
36  const std::string& id() const { return id_; }
37  const std::string& editor_icon() const { return icon_; }
38  const t_string& name(GENDER gender=MALE) const { return name_[gender]; }
39  const t_string& plural_name() const { return plural_name_; }
40  const t_string& description() const { return description_; }
41 
42  std::string generate_name(GENDER gender) const;
43  const name_generator& generator(GENDER gender) const;
44 
45  bool uses_global_traits() const;
46 
49  unsigned int num_traits() const;
50  const std::string& undead_variation() const { return undead_variation_; }
51 
52  /**
53  * Gets this race's icon path without state/size suffix and extension.
54  *
55  * This doesn't return the full path. Its output looks something like this:
56  * @c icons/unit-groups/race_elf
57  *
58  * This is because this output is used in the editor for a GUI1 button's icon,
59  * and GUI1 automatically appends the state extension, such as "_30-pressed.png"
60  *
61  * If a custom icon has been provided, that is used. Else, it checks if an
62  * appropriate icon exists in icons/unit-groups/. If not, a generic custom race
63  * icon will be used.
64  */
65  std::string get_icon_path_stem() const;
66 
67  const std::string& help_taxonomy() const { return help_taxonomy_; }
68 
69  /** Dummy race used when a race is not yet known. */
70  static const unit_race null_race;
71 
72 private:
73  /** Only used to construct null_race. */
74  unit_race();
75 
76  const config cfg_;
77 
78  std::string id_;
79  std::string icon_;
80  std::array<t_string, NUM_GENDERS> name_;
83  unsigned int ntraits_;
84  std::array<std::shared_ptr<name_generator>, NUM_GENDERS> name_generator_;
85 
89  std::string undead_variation_;
90  std::string help_taxonomy_;
91 };
92 
94 const std::string& gender_string(unit_race::GENDER gender);
95 
96 /**
97  * Chooses a value from the given config based on gender. If the value for
98  * the specified gender is blank, then @a default_key is used to look up a
99  * value instead.
100  */
102  const config & cfg, unit_race::GENDER gender, const std::string & male_key,
103  const std::string & female_key, const std::string & default_key);
104 
105 typedef std::map<std::string,unit_race> race_map;
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:296
const std::string & id() const
Definition: race.hpp:36
bool global_traits_
Definition: race.hpp:88
bool uses_global_traits() const
Definition: race.cpp:121
t_string description_
Definition: race.hpp:82
std::string get_icon_path_stem() const
Gets this race's icon path without state/size suffix and extension.
Definition: race.cpp:165
const config cfg_
Definition: race.hpp:76
std::string icon_
Definition: race.hpp:79
const std::string & editor_icon() const
Definition: race.hpp:37
static const unit_race null_race
Dummy race used when a race is not yet known.
Definition: race.hpp:70
std::string undead_variation_
Definition: race.hpp:89
const t_string & name(GENDER gender=MALE) const
Definition: race.hpp:38
const std::string & undead_variation() const
Definition: race.hpp:50
const t_string & plural_name() const
Definition: race.hpp:39
const t_string & description() const
Definition: race.hpp:40
const config & get_cfg() const
Definition: race.hpp:35
const config::const_child_itors & additional_topics() const
Definition: race.cpp:131
config::const_child_itors topics_
Definition: race.hpp:87
std::string generate_name(GENDER gender) const
Definition: race.cpp:111
unit_race()
Only used to construct null_race.
Definition: race.cpp:49
const std::string & help_taxonomy() const
Definition: race.hpp:67
std::array< std::shared_ptr< name_generator >, NUM_GENDERS > name_generator_
Definition: race.hpp:84
unsigned int num_traits() const
Definition: race.cpp:136
unsigned int ntraits_
Definition: race.hpp:83
std::array< t_string, NUM_GENDERS > name_
Definition: race.hpp:80
std::string id_
Definition: race.hpp:78
config::const_child_itors traits_
Definition: race.hpp:86
const config::const_child_itors & additional_traits() const
Definition: race.cpp:126
static const std::string s_female
Standard string id (not translatable) for FEMALE.
Definition: race.hpp:29
static const std::string s_male
Standard string id (not translatable) for MALE.
Definition: race.hpp:30
std::string help_taxonomy_
Definition: race.hpp:90
t_string plural_name_
Definition: race.hpp:81
const name_generator & generator(GENDER gender) const
Definition: race.cpp:116
@ NUM_GENDERS
Definition: race.hpp:28
@ FEMALE
Definition: race.hpp:28
@ MALE
Definition: race.hpp:28
Definitions for the interface to Wesnoth Markup Language (WML).
#define NOT_DANGLING
Definition: global.hpp:65
const std::string & gender_string(unit_race::GENDER gender)
Definition: race.cpp:139
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def=unit_race::MALE)
Definition: race.cpp:149
NOT_DANGLING const config::attribute_value & gender_value(const config &cfg, unit_race::GENDER gender, const std::string &male_key, const std::string &female_key, const std::string &default_key)
Chooses a value from the given config based on gender.
Definition: race.cpp:158
std::map< std::string, unit_race > race_map
Definition: race.hpp:105