OpenTTD: src/company_manager_face.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

company_manager_face.h
Go to the documentation of this file.
1 /* $Id: company_manager_face.h 27975 2018-03-11 07:16:58Z peter1138 $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef COMPANY_MANAGER_FACE_H
13 #define COMPANY_MANAGER_FACE_H
14
15 #include "core/random_func.hpp"
16 #include "core/bitmath_func.hpp"
17 #include "table/sprites.h"
18 #include "company_type.h"
19
21 enum GenderEthnicity {
22 GENDER_FEMALE = 0,
23 ETHNICITY_BLACK = 1,
24
25 GE_WM = 0,
26 GE_WF = 1 << GENDER_FEMALE,
27 GE_BM = 1 << ETHNICITY_BLACK,
28 GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE,
29 GE_END,
30 };
31 DECLARE_ENUM_AS_BIT_SET(GenderEthnicity)
32
34 enum CompanyManagerFaceVariable {
35 CMFV_GENDER,
36 CMFV_ETHNICITY,
37 CMFV_GEN_ETHN,
38 CMFV_HAS_MOUSTACHE,
39 CMFV_HAS_TIE_EARRING,
40 CMFV_HAS_GLASSES,
41 CMFV_EYE_COLOUR,
42 CMFV_CHEEKS,
43 CMFV_CHIN,
44 CMFV_EYEBROWS,
45 CMFV_MOUSTACHE,
46 CMFV_LIPS,
47 CMFV_NOSE,
48 CMFV_HAIR,
49 CMFV_JACKET,
50 CMFV_COLLAR,
51 CMFV_TIE_EARRING,
52 CMFV_GLASSES,
53 CMFV_END,
54 };
55 DECLARE_POSTFIX_INCREMENT(CompanyManagerFaceVariable)
56
57
58 struct CompanyManagerFaceBitsInfo {
59 byte offset;
60 byte length;
61 byte valid_values[GE_END];
62 SpriteID first_sprite[GE_END];
63 };
64
66 static const CompanyManagerFaceBitsInfo _cmf_info[] = {
67 /* Index off len WM WF BM BF WM WF BM BF
68 * CMFV_GENDER */ { 0, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
69 /* CMFV_ETHNICITY */ { 1, 2, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
70 /* CMFV_GEN_ETHN */ { 0, 3, { 4, 4, 4, 4 }, { 0, 0, 0, 0 } },
71 /* CMFV_HAS_MOUSTACHE */ { 3, 1, { 2, 0, 2, 0 }, { 0, 0, 0, 0 } },
72 /* CMFV_HAS_TIE_EARRING */ { 3, 1, { 0, 2, 0, 2 }, { 0, 0, 0, 0 } },
73 /* CMFV_HAS_GLASSES */ { 4, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } },
74 /* CMFV_EYE_COLOUR */ { 5, 2, { 3, 3, 1, 1 }, { 0, 0, 0, 0 } },
75 /* CMFV_CHEEKS */ { 0, 0, { 1, 1, 1, 1 }, { 0x325, 0x326, 0x390, 0x3B0 } },
76 /* CMFV_CHIN */ { 7, 2, { 4, 1, 2, 2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
77 /* CMFV_EYEBROWS */ { 9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
78 /* CMFV_MOUSTACHE */ { 13, 2, { 3, 0, 3, 0 }, { 0x367, 0, 0x397, 0 } },
79 /* CMFV_LIPS */ { 13, 4, { 12, 10, 9, 9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } },
80 /* CMFV_NOSE */ { 17, 3, { 8, 4, 4, 5 }, { 0x349, 0x34C, 0x393, 0x3B3 } },
81 /* CMFV_HAIR */ { 20, 4, { 9, 5, 5, 5 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
82 /* CMFV_JACKET */ { 24, 2, { 3, 3, 3, 3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
83 /* CMFV_COLLAR */ { 26, 2, { 4, 4, 4, 4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
84 /* CMFV_TIE_EARRING */ { 28, 3, { 6, 3, 6, 3 }, { 0x372, 0x37F, 0x372, 0x3D1 } },
85 /* CMFV_GLASSES */ { 31, 1, { 2, 2, 2, 2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }
86 };
88 assert_compile(lengthof(_cmf_info) == CMFV_END);
89
98 static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
99 {
100 assert(_cmf_info[cmfv].valid_values[ge] != 0);
101
102 return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
103 }
104
113 static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
114 {
115 assert(val < _cmf_info[cmfv].valid_values[ge]);
116
117 SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
118 }
119
132 static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
133 {
134 int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
135
136 /* scales the new value to the correct scope */
137 if (val >= _cmf_info[cmfv].valid_values[ge]) {
138 val = 0;
139 } else if (val < 0) {
140 val = _cmf_info[cmfv].valid_values[ge] - 1;
141 }
142
143 SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
144 }
145
153 static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
154 {
155 return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
156 }
157
166 static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
167 {
168 assert(val < (1U << _cmf_info[cmfv].length));
169
170 return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
171 }
172
178 static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
179 {
180 IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
181
182 GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
183
184 /* Is a male face with moustache. Need to reduce CPU load in the loop. */
185 bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
186
187 for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
188
189 /* The moustache variable will be scaled only if it is a male face with has a moustache */
190 if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
191 IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
192 }
193 }
194 }
195
208 static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv, bool interactive = true)
209 {
210 /* This method is called from a command when not interactive and
211 * then we must use Random to get the same result on all clients. */
212 cmf = interactive ? InteractiveRandom() : Random(); // random all company manager's face bits
213
214 /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
215 ge = (GenderEthnicity)((uint)ge % GE_END);
216
217 /* set the gender (and ethnicity) for the new company manager's face */
218 if (adv) {
219 SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
220 } else {
221 SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
222 }
223
224 /* scales all company manager's face bits to the correct scope */
225 ScaleAllCompanyManagerFaceBits(cmf);
226 }
227
236 static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
237 {
238 assert(_cmf_info[cmfv].valid_values[ge] != 0);
239
240 return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
241 }
242
243 void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y);
244
245 #endif /* COMPANY_MANAGER_FACE_H */