# company_manager_face.h

Go to the documentation of this file.

    00001 /* $Id: company_manager_face.h 25490 2013-06-27 20:20:13Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef COMPANY_MANAGER_FACE_H
    00013 #define COMPANY_MANAGER_FACE_H
    00014
    00015 #include "core/random_func.hpp"
    00016 #include "core/bitmath_func.hpp"
    00017 #include "table/sprites.h"
    00018 #include "company_type.h"
    00019
    00021 enum GenderEthnicity {
    00022   GENDER_FEMALE    = 0,
    00023   ETHNICITY_BLACK  = 1,
    00024
    00025   GE_WM = 0,
    00026   GE_WF = 1 << GENDER_FEMALE,
    00027   GE_BM = 1 << ETHNICITY_BLACK,
    00028   GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE,
    00029   GE_END,
    00030 };
    00031 DECLARE_ENUM_AS_BIT_SET(GenderEthnicity)
    00032
    00034 enum CompanyManagerFaceVariable {
    00035   CMFV_GENDER,
    00036   CMFV_ETHNICITY,
    00037   CMFV_GEN_ETHN,
    00038   CMFV_HAS_MOUSTACHE,
    00039   CMFV_HAS_TIE_EARRING,
    00040   CMFV_HAS_GLASSES,
    00041   CMFV_EYE_COLOUR,
    00042   CMFV_CHEEKS,
    00043   CMFV_CHIN,
    00044   CMFV_EYEBROWS,
    00045   CMFV_MOUSTACHE,
    00046   CMFV_LIPS,
    00047   CMFV_NOSE,
    00048   CMFV_HAIR,
    00049   CMFV_JACKET,
    00050   CMFV_COLLAR,
    00051   CMFV_TIE_EARRING,
    00052   CMFV_GLASSES,
    00053   CMFV_END,
    00054 };
    00055 DECLARE_POSTFIX_INCREMENT(CompanyManagerFaceVariable)
    00056
    00057
    00058 struct CompanyManagerFaceBitsInfo {
    00059   byte     offset;
    00060   byte     length;
    00061   byte     valid_values[GE_END];
    00062   SpriteID first_sprite[GE_END];
    00063 };
    00064
    00066 static const CompanyManagerFaceBitsInfo _cmf_info[] = {
    00067   /* Index                   off len   WM  WF  BM  BF         WM     WF     BM     BF
    00068    * CMFV_GENDER          */ {  0, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00069   /* CMFV_ETHNICITY       */ {  1, 2, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00070   /* CMFV_GEN_ETHN        */ {  0, 3, {  4,  4,  4,  4 }, {     0,     0,     0,     0 } },
    00071   /* CMFV_HAS_MOUSTACHE   */ {  3, 1, {  2,  0,  2,  0 }, {     0,     0,     0,     0 } },
    00072   /* CMFV_HAS_TIE_EARRING */ {  3, 1, {  0,  2,  0,  2 }, {     0,     0,     0,     0 } },
    00073   /* CMFV_HAS_GLASSES     */ {  4, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00074   /* CMFV_EYE_COLOUR      */ {  5, 2, {  3,  3,  1,  1 }, {     0,     0,     0,     0 } },
    00075   /* CMFV_CHEEKS          */ {  0, 0, {  1,  1,  1,  1 }, { 0x325, 0x326, 0x390, 0x3B0 } },
    00076   /* CMFV_CHIN            */ {  7, 2, {  4,  1,  2,  2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
    00077   /* CMFV_EYEBROWS        */ {  9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
    00078   /* CMFV_MOUSTACHE       */ { 13, 2, {  3,  0,  3,  0 }, { 0x367,     0, 0x397,     0 } },
    00079   /* CMFV_LIPS            */ { 13, 4, { 12, 10,  9,  9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } },
    00080   /* CMFV_NOSE            */ { 17, 3, {  8,  4,  4,  5 }, { 0x349, 0x34C, 0x393, 0x3B3 } },
    00081   /* CMFV_HAIR            */ { 20, 4, {  9,  5,  5,  4 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
    00082   /* CMFV_JACKET          */ { 24, 2, {  3,  3,  3,  3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
    00083   /* CMFV_COLLAR          */ { 26, 2, {  4,  4,  4,  4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
    00084   /* CMFV_TIE_EARRING     */ { 28, 3, {  6,  3,  6,  3 }, { 0x372, 0x37F, 0x372, 0x3D1 } },
    00085   /* CMFV_GLASSES         */ { 31, 1, {  2,  2,  2,  2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }
    00086 };
    00088 assert_compile(lengthof(_cmf_info) == CMFV_END);
    00089
    00098 static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00099 {
    00100   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00101
    00102   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00103 }
    00104
    00113 static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00114 {
    00115   assert(val < _cmf_info[cmfv].valid_values[ge]);
    00116
    00117   SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
    00118 }
    00119
    00132 static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
    00133 {
    00134   int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
    00135
    00136   /* scales the new value to the correct scope */
    00137   if (val >= _cmf_info[cmfv].valid_values[ge]) {
    00138     val = 0;
    00139   } else if (val < 0) {
    00140     val = _cmf_info[cmfv].valid_values[ge] - 1;
    00141   }
    00142
    00143   SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
    00144 }
    00145
    00153 static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00154 {
    00155   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
    00156 }
    00157
    00166 static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00167 {
    00168   assert(val < (1U << _cmf_info[cmfv].length));
    00169
    00170   return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
    00171 }
    00172
    00178 static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
    00179 {
    00180   IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
    00181
    00182   GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
    00183
    00184   /* Is a male face with moustache. Need to reduce CPU load in the loop. */
    00185   bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
    00186
    00187   for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
    00188
    00189     /* The moustache variable will be scaled only if it is a male face with has a moustache */
    00190     if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
    00191       IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
    00192     }
    00193   }
    00194 }
    00195
    00208 static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv, bool interactive = true)
    00209 {
    00210   /* This method is called from a command when not interactive and
    00211    * then we must use Random to get the same result on all clients. */
    00212   cmf = interactive ? InteractiveRandom() : Random(); // random all company manager's face bits
    00213
    00214   /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
    00215   ge = (GenderEthnicity)((uint)ge % GE_END);
    00216
    00217   /* set the gender (and ethnicity) for the new company manager's face */
    00218   if (adv) {
    00219     SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
    00220   } else {
    00221     SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
    00222   }
    00223
    00224   /* scales all company manager's face bits to the correct scope */
    00225   ScaleAllCompanyManagerFaceBits(cmf);
    00226 }
    00227
    00236 static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00237 {
    00238   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00239
    00240   return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00241 }
    00242
    00243 void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y);
    00244
    00245 #endif /* COMPANY_MANAGER_FACE_H */