OpenTTD Source: src/company_manager_face.h Source File

OpenTTD Source 1.11.0-RC1

company_manager_face.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef COMPANY_MANAGER_FACE_H
11 #define COMPANY_MANAGER_FACE_H
12
13 #include "core/random_func.hpp"
14 #include "core/bitmath_func.hpp"
15 #include "table/sprites.h"
16 #include "company_type.h"
17
19 enum GenderEthnicity {
20 GENDER_FEMALE = 0,
21 ETHNICITY_BLACK = 1,
22
23 GE_WM = 0,
24 GE_WF = 1 << GENDER_FEMALE,
25 GE_BM = 1 << ETHNICITY_BLACK,
26 GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE,
27 GE_END,
28 };
29 DECLARE_ENUM_AS_BIT_SET(GenderEthnicity)
30
32 enum CompanyManagerFaceVariable {
33 CMFV_GENDER,
34 CMFV_ETHNICITY,
35 CMFV_GEN_ETHN,
36 CMFV_HAS_MOUSTACHE,
37 CMFV_HAS_TIE_EARRING,
38 CMFV_HAS_GLASSES,
39 CMFV_EYE_COLOUR,
40 CMFV_CHEEKS,
41 CMFV_CHIN,
42 CMFV_EYEBROWS,
43 CMFV_MOUSTACHE,
44 CMFV_LIPS,
45 CMFV_NOSE,
46 CMFV_HAIR,
47 CMFV_JACKET,
48 CMFV_COLLAR,
49 CMFV_TIE_EARRING,
50 CMFV_GLASSES,
51 CMFV_END,
52 };
53 DECLARE_POSTFIX_INCREMENT(CompanyManagerFaceVariable)
54
55
56 struct CompanyManagerFaceBitsInfo {
57 byte offset;
58 byte length;
59 byte valid_values[GE_END];
60 SpriteID first_sprite[GE_END];
61 };
62
64 static const CompanyManagerFaceBitsInfo _cmf_info[] = {
65 /* Index off len WM WF BM BF WM WF BM BF
66 * CMFV_GENDER */ { 0, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
67 /* CMFV_ETHNICITY */ { 1, 2, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
68 /* CMFV_GEN_ETHN */ { 0, 3, { 4, 4, 4, 4 }, { 0, 0, 0, 0 } },
69 /* CMFV_HAS_MOUSTACHE */ { 3, 1, { 2, 0, 2, 0 }, { 0, 0, 0, 0 } },
70 /* CMFV_HAS_TIE_EARRING */ { 3, 1, { 0, 2, 0, 2 }, { 0, 0, 0, 0 } },
71 /* CMFV_HAS_GLASSES */ { 4, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
72 /* CMFV_EYE_COLOUR */ { 5, 2, { 3, 3, 1, 1 }, { 0, 0, 0, 0 } },
73 /* CMFV_CHEEKS */ { 0, 0, { 1, 1, 1, 1 }, { 0x325, 0x326, 0x390, 0x3B0 } },
74 /* CMFV_CHIN */ { 7, 2, { 4, 1, 2, 2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
75 /* CMFV_EYEBROWS */ { 9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
76 /* CMFV_MOUSTACHE */ { 13, 2, { 3, 0, 3, 0 }, { 0x367, 0, 0x397, 0 } },
77 /* CMFV_LIPS */ { 13, 4, { 12, 10, 9, 9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } },
78 /* CMFV_NOSE */ { 17, 3, { 8, 4, 4, 5 }, { 0x349, 0x34C, 0x393, 0x3B3 } },
79 /* CMFV_HAIR */ { 20, 4, { 9, 5, 5, 5 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
80 /* CMFV_JACKET */ { 24, 2, { 3, 3, 3, 3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
81 /* CMFV_COLLAR */ { 26, 2, { 4, 4, 4, 4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
82 /* CMFV_TIE_EARRING */ { 28, 3, { 6, 3, 6, 3 }, { 0x372, 0x37F, 0x372, 0x3D1 } },
83 /* CMFV_GLASSES */ { 31, 1, { 2, 2, 2, 2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }
84 };
86 static_assert(lengthof(_cmf_info) == CMFV_END);
87
96 static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
97 {
98 assert(_cmf_info[cmfv].valid_values[ge] != 0);
99
100 return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
101 }
102
111 static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
112 {
113 assert(val < _cmf_info[cmfv].valid_values[ge]);
114
115 SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
116 }
117
130 static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
131 {
132 int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
133
134 /* scales the new value to the correct scope */
135 if (val >= _cmf_info[cmfv].valid_values[ge]) {
136 val = 0;
137 } else if (val < 0) {
138 val = _cmf_info[cmfv].valid_values[ge] - 1;
139 }
140
141 SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
142 }
143
151 static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
152 {
153 return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
154 }
155
164 static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
165 {
166 assert(val < (1U << _cmf_info[cmfv].length));
167
168 return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
169 }
170
176 static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
177 {
178 IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
179
180 GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
181
182 /* Is a male face with moustache. Need to reduce CPU load in the loop. */
183 bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
184
185 for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
186
187 /* The moustache variable will be scaled only if it is a male face with has a moustache */
188 if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
189 IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
190 }
191 }
192 }
193
206 static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv, bool interactive = true)
207 {
208 /* This method is called from a command when not interactive and
209 * then we must use Random to get the same result on all clients. */
210 cmf = interactive ? InteractiveRandom() : Random(); // random all company manager's face bits
211
212 /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
213 ge = (GenderEthnicity)((uint)ge % GE_END);
214
215 /* set the gender (and ethnicity) for the new company manager's face */
216 if (adv) {
217 SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
218 } else {
219 SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
220 }
221
222 /* scales all company manager's face bits to the correct scope */
223 ScaleAllCompanyManagerFaceBits(cmf);
224 }
225
234 static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
235 {
236 assert(_cmf_info[cmfv].valid_values[ge] != 0);
237
238 return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
239 }
240
241 void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y);
242
243 #endif /* COMPANY_MANAGER_FACE_H */
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
CompanyManagerFace
uint32 CompanyManagerFace
Company manager face bits, info see in company_manager_face.h.
Definition: company_type.h:51
GetCompanyManagerFaceSprite
static SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
Gets the sprite to draw for the given company manager's face variable.
Definition: company_manager_face.h:234
GenderEthnicity
GenderEthnicity
The gender/race combinations that we have faces for.
Definition: company_manager_face.h:19
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GENDER_FEMALE
@ GENDER_FEMALE
This bit set means a female, otherwise male.
Definition: company_manager_face.h:20
GetCompanyManagerFaceBits
static uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
Make sure the table's size is right.
Definition: company_manager_face.h:96
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
bitmath_func.hpp
CompanyManagerFaceBitsInfo::first_sprite
SpriteID first_sprite[GE_END]
The first sprite per gender/ethnicity.
Definition: company_manager_face.h:60
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
AreCompanyManagerFaceBitsValid
static bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
Checks whether the company manager's face bits have a valid range.
Definition: company_manager_face.h:151
CompanyManagerFaceBitsInfo::valid_values
byte valid_values[GE_END]
The number of valid values per gender/ethnicity.
Definition: company_manager_face.h:59
CompanyManagerFaceBitsInfo
Information about the valid values of CompanyManagerFace bitgroups as well as the sprites to draw.
Definition: company_manager_face.h:56
sprites.h
CompanyManagerFaceBitsInfo::length
byte length
Number of bits used in the CompanyManagerFace.
Definition: company_manager_face.h:58
DrawCompanyManagerFace
void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y)
Draws the face of a company manager's face.
Definition: company_gui.cpp:1113
IncreaseCompanyManagerFaceBits
static void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
Increase/Decrease the company manager's face variable by the given amount.
Definition: company_manager_face.h:130
CompanyManagerFaceBitsInfo::offset
byte offset
Offset in bits into the CompanyManagerFace.
Definition: company_manager_face.h:57
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
random_func.hpp
SetCompanyManagerFaceBits
static void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
Sets the company manager's face bits for the given company manager's face variable.
Definition: company_manager_face.h:111
GE_WM
@ GE_WM
A male of Caucasian origin (white)
Definition: company_manager_face.h:23
ScaleAllCompanyManagerFaceBits
static void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
Scales all company manager's face bits to the correct scope.
Definition: company_manager_face.h:176
ScaleCompanyManagerFaceValue
static uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
Scales a company manager's face bits variable to the correct scope.
Definition: company_manager_face.h:164
_cmf_info
static const CompanyManagerFaceBitsInfo _cmf_info[]
Lookup table for indices into the CompanyManagerFace, valid ranges and sprites.
Definition: company_manager_face.h:64
ETHNICITY_BLACK
@ ETHNICITY_BLACK
This bit set means black, otherwise white.
Definition: company_manager_face.h:21
company_type.h
GE_BF
@ GE_BF
A female of African origin (black)
Definition: company_manager_face.h:26
RandomCompanyManagerFaceBits
static void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv, bool interactive=true)
Make a random new face.
Definition: company_manager_face.h:206
GE_WF
@ GE_WF
A female of Caucasian origin (white)
Definition: company_manager_face.h:24
GE_BM
@ GE_BM
A male of African origin (black)
Definition: company_manager_face.h:25