# company_manager_face.h

Go to the documentation of this file.

    00001 /* $Id: company_manager_face.h 17248 2009-08-21 20:21:05Z 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
    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 };
    00087 assert_compile(lengthof(_cmf_info) == CMFV_END);
    00088
    00097 static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00098 {
    00099   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00100
    00101   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00102 }
    00103
    00112 static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00113 {
    00114   assert(val < _cmf_info[cmfv].valid_values[ge]);
    00115
    00116   SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
    00117 }
    00118
    00131 static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
    00132 {
    00133   int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
    00134
    00135   /* scales the new value to the correct scope */
    00136   if (val >= _cmf_info[cmfv].valid_values[ge]) {
    00137     val = 0;
    00138   } else if (val < 0) {
    00139     val = _cmf_info[cmfv].valid_values[ge] - 1;
    00140   }
    00141
    00142   SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
    00143 }
    00144
    00152 static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00153 {
    00154   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
    00155 }
    00156
    00165 static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00166 {
    00167   assert(val < (1U << _cmf_info[cmfv].length));
    00168
    00169   return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
    00170 }
    00171
    00177 static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
    00178 {
    00179   IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
    00180
    00181   GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
    00182
    00183   /* Is a male face with moustache. Need to reduce CPU load in the loop. */
    00184   bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
    00185
    00186   for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
    00187
    00188     /* The moustache variable will be scaled only if it is a male face with has a moustache */
    00189     if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
    00190       IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
    00191     }
    00192   }
    00193 }
    00194
    00206 static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv)
    00207 {
    00208   cmf = InteractiveRandom(); // random all company manager's face bits
    00209
    00210   /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
    00211   ge = (GenderEthnicity)((uint)ge % GE_END);
    00212
    00213   /* set the gender (and ethnicity) for the new company manager's face */
    00214   if (adv) {
    00215     SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
    00216   } else {
    00217     SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
    00218   }
    00219
    00220   /* scales all company manager's face bits to the correct scope */
    00221   ScaleAllCompanyManagerFaceBits(cmf);
    00222 }
    00223
    00232 static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00233 {
    00234   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00235
    00236   return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00237 }
    00238
    00239 void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y);
    00240 bool IsValidCompanyManagerFace(CompanyManagerFace cmf);
    00241
    00242 #endif /* COMPANY_MANAGER_FACE_H */