# company_manager_face.h

Go to the documentation of this file.

    00001 /* $Id: company_manager_face.h 15903 2009-03-30 23:15:05Z rubidium $ */
    00002
    00005 #ifndef COMPANY_MANAGER_FACE_H
    00006 #define COMPANY_MANAGER_FACE_H
    00007
    00008 #include "core/random_func.hpp"
    00009 #include "core/bitmath_func.hpp"
    00010 #include "table/sprites.h"
    00011 #include "company_type.h"
    00012
    00014 enum GenderEthnicity {
    00015   GENDER_FEMALE    = 0,
    00016   ETHNICITY_BLACK  = 1,
    00017
    00018   GE_WM = 0,
    00019   GE_WF = 1 << GENDER_FEMALE,
    00020   GE_BM = 1 << ETHNICITY_BLACK,
    00021   GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE,
    00022   GE_END,
    00023 };
    00024 DECLARE_ENUM_AS_BIT_SET(GenderEthnicity);
    00025
    00027 enum CompanyManagerFaceVariable {
    00028   CMFV_GENDER,
    00029   CMFV_ETHNICITY,
    00030   CMFV_GEN_ETHN,
    00031   CMFV_HAS_MOUSTACHE,
    00032   CMFV_HAS_TIE_EARRING,
    00033   CMFV_HAS_GLASSES,
    00034   CMFV_EYE_COLOUR,
    00035   CMFV_CHEEKS,
    00036   CMFV_CHIN,
    00037   CMFV_EYEBROWS,
    00038   CMFV_MOUSTACHE,
    00039   CMFV_LIPS,
    00040   CMFV_NOSE,
    00041   CMFV_HAIR,
    00042   CMFV_JACKET,
    00043   CMFV_COLLAR,
    00044   CMFV_TIE_EARRING,
    00045   CMFV_GLASSES,
    00046   CMFV_END
    00047 };
    00048 DECLARE_POSTFIX_INCREMENT(CompanyManagerFaceVariable);
    00049
    00051 struct CompanyManagerFaceBitsInfo {
    00052   byte     offset;
    00053   byte     length;
    00054   byte     valid_values[GE_END];
    00055   SpriteID first_sprite[GE_END];
    00056 };
    00057
    00059 static const CompanyManagerFaceBitsInfo _cmf_info[] = {
    00060   /* Index                   off len   WM  WF  BM  BF         WM     WF     BM     BF
    00061    * CMFV_GENDER          */ {  0, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00062   /* CMFV_ETHNICITY       */ {  1, 2, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00063   /* CMFV_GEN_ETHN        */ {  0, 3, {  4,  4,  4,  4 }, {     0,     0,     0,     0 } },
    00064   /* CMFV_HAS_MOUSTACHE   */ {  3, 1, {  2,  0,  2,  0 }, {     0,     0,     0,     0 } },
    00065   /* CMFV_HAS_TIE_EARRING */ {  3, 1, {  0,  2,  0,  2 }, {     0,     0,     0,     0 } },
    00066   /* CMFV_HAS_GLASSES     */ {  4, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } },
    00067   /* CMFV_EYE_COLOUR      */ {  5, 2, {  3,  3,  1,  1 }, {     0,     0,     0,     0 } },
    00068   /* CMFV_CHEEKS          */ {  0, 0, {  1,  1,  1,  1 }, { 0x325, 0x326, 0x390, 0x3B0 } },
    00069   /* CMFV_CHIN            */ {  7, 2, {  4,  1,  2,  2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
    00070   /* CMFV_EYEBROWS        */ {  9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
    00071   /* CMFV_MOUSTACHE       */ { 13, 2, {  3,  0,  3,  0 }, { 0x367,     0, 0x397,     0 } },
    00072   /* CMFV_LIPS            */ { 13, 4, { 12, 10,  9,  9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } },
    00073   /* CMFV_NOSE            */ { 17, 3, {  8,  4,  4,  5 }, { 0x349, 0x34C, 0x393, 0x3B3 } },
    00074   /* CMFV_HAIR            */ { 20, 4, {  9,  5,  5,  4 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
    00075   /* CMFV_JACKET          */ { 24, 2, {  3,  3,  3,  3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
    00076   /* CMFV_COLLAR          */ { 26, 2, {  4,  4,  4,  4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
    00077   /* CMFV_TIE_EARRING     */ { 28, 3, {  6,  3,  6,  3 }, { 0x372, 0x37F, 0x372, 0x3D1 } },
    00078   /* CMFV_GLASSES         */ { 31, 1, {  2,  2,  2,  2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }
    00079 };
    00080 assert_compile(lengthof(_cmf_info) == CMFV_END);
    00081
    00090 static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00091 {
    00092   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00093
    00094   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00095 }
    00096
    00105 static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00106 {
    00107   assert(val < _cmf_info[cmfv].valid_values[ge]);
    00108
    00109   SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
    00110 }
    00111
    00124 static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
    00125 {
    00126   int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
    00127
    00128   /* scales the new value to the correct scope */
    00129   if (val >= _cmf_info[cmfv].valid_values[ge]) {
    00130     val = 0;
    00131   } else if (val < 0) {
    00132     val = _cmf_info[cmfv].valid_values[ge] - 1;
    00133   }
    00134
    00135   SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
    00136 }
    00137
    00145 static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00146 {
    00147   return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
    00148 }
    00149
    00158 static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
    00159 {
    00160   assert(val < (1U << _cmf_info[cmfv].length));
    00161
    00162   return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
    00163 }
    00164
    00170 static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
    00171 {
    00172   IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
    00173
    00174   GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
    00175
    00176   /* Is a male face with moustache. Need to reduce CPU load in the loop. */
    00177   bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
    00178
    00179   for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
    00180
    00181     /* The moustache variable will be scaled only if it is a male face with has a moustache */
    00182     if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
    00183       IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
    00184     }
    00185   }
    00186 }
    00187
    00199 static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv)
    00200 {
    00201   cmf = InteractiveRandom(); // random all company manager's face bits
    00202
    00203   /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
    00204   ge = (GenderEthnicity)((uint)ge % GE_END);
    00205
    00206   /* set the gender (and ethnicity) for the new company manager's face */
    00207   if (adv) {
    00208     SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
    00209   } else {
    00210     SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
    00211   }
    00212
    00213   /* scales all company manager's face bits to the correct scope */
    00214   ScaleAllCompanyManagerFaceBits(cmf);
    00215 }
    00216
    00225 static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
    00226 {
    00227   assert(_cmf_info[cmfv].valid_values[ge] != 0);
    00228
    00229   return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
    00230 }
    00231
    00232 void DrawCompanyManagerFace(CompanyManagerFace face, int colour, int x, int y);
    00233 bool IsValidCompanyManagerFace(CompanyManagerFace cmf);
    00234
    00235 #endif /* COMPANY_MANAGER_FACE_H */