rubidium@6190: /* $Id$ */ rubidium@6190: rubidium@6190: /** @file player_face.h Functionality related to the player's face */ rubidium@6190: rubidium@6190: #ifndef PLAYER_FACE_H rubidium@6190: #define PLAYER_FACE_H rubidium@6190: rubidium@8131: #include "core/random_func.hpp" rubidium@8236: #include "core/bitmath_func.hpp" rubidium@8131: rubidium@6190: /** The gender/race combinations that we have faces for */ rubidium@6190: enum GenderEthnicity { rubidium@6190: GENDER_FEMALE = 0, ///< This bit set means a female, otherwise male rubidium@6190: ETHNICITY_BLACK = 1, ///< This bit set means black, otherwise white rubidium@6190: rubidium@6190: GE_WM = 0, ///< A male of Caucasian origin (white) rubidium@6190: GE_WF = 1 << GENDER_FEMALE, ///< A female of Caucasian origin (white) rubidium@6190: GE_BM = 1 << ETHNICITY_BLACK, ///< A male of African origin (black) rubidium@6190: GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE, ///< A female of African origin (black) rubidium@6190: GE_END, rubidium@6190: }; rubidium@6190: DECLARE_ENUM_AS_BIT_SET(GenderEthnicity); ///< See GenderRace as a bitset rubidium@6190: rubidium@6190: /** Bitgroups of the PlayerFace variable */ rubidium@6190: enum PlayerFaceVariable { rubidium@6190: PFV_GENDER, rubidium@6190: PFV_ETHNICITY, rubidium@6190: PFV_GEN_ETHN, rubidium@6190: PFV_HAS_MOUSTACHE, rubidium@6190: PFV_HAS_TIE_EARRING, rubidium@6190: PFV_HAS_GLASSES, rubidium@6190: PFV_EYE_COLOUR, rubidium@6190: PFV_CHEEKS, rubidium@6190: PFV_CHIN, rubidium@6190: PFV_EYEBROWS, rubidium@6190: PFV_MOUSTACHE, rubidium@6190: PFV_LIPS, rubidium@6190: PFV_NOSE, rubidium@6190: PFV_HAIR, rubidium@6190: PFV_JACKET, rubidium@6190: PFV_COLLAR, rubidium@6190: PFV_TIE_EARRING, rubidium@6190: PFV_GLASSES, rubidium@6190: PFV_END rubidium@6190: }; rubidium@6190: DECLARE_POSTFIX_INCREMENT(PlayerFaceVariable); rubidium@6190: rubidium@6190: /** Information about the valid values of PlayerFace bitgroups as well as the sprites to draw */ rubidium@6190: struct PlayerFaceBitsInfo { rubidium@6190: byte offset; ///< Offset in bits into the PlayerFace rubidium@6190: byte length; ///< Number of bits used in the PlayerFace rubidium@6190: byte valid_values[GE_END]; ///< The number of valid values per gender/ethnicity rubidium@6190: SpriteID first_sprite[GE_END]; ///< The first sprite per gender/ethnicity rubidium@6190: }; rubidium@6190: rubidium@6190: /** Lookup table for indices into the PlayerFace, valid ranges and sprites */ rubidium@6190: static const PlayerFaceBitsInfo _pf_info[] = { rubidium@6190: /* Index off len WM WF BM BF WM WF BM BF */ rubidium@6190: /* PFV_GENDER */ { 0, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } }, ///< 0 = male, 1 = female rubidium@6190: /* PFV_ETHNICITY */ { 1, 2, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } }, ///< 0 = (Western-)Caucasian, 1 = African(-American)/Black rubidium@6190: /* PFV_GEN_ETHN */ { 0, 3, { 4, 4, 4, 4 }, { 0, 0, 0, 0 } }, ///< Shortcut to get/set gender _and_ ethnicity rubidium@6190: /* PFV_HAS_MOUSTACHE */ { 3, 1, { 2, 0, 2, 0 }, { 0, 0, 0, 0 } }, ///< Females do not have a moustache rubidium@6190: /* PFV_HAS_TIE_EARRING */ { 3, 1, { 0, 2, 0, 2 }, { 0, 0, 0, 0 } }, ///< Draw the earring for females or not. For males the tie is always drawn. rubidium@6190: /* PFV_HAS_GLASSES */ { 4, 1, { 2, 2, 2, 2 }, { 0, 0, 0, 0 } }, ///< Whether to draw glasses or not rubidium@7734: /* PFV_EYE_COLOUR */ { 5, 2, { 3, 3, 1, 1 }, { 0, 0, 0, 0 } }, ///< Palette modification rubidium@6190: /* PFV_CHEEKS */ { 0, 0, { 1, 1, 1, 1 }, { 0x325, 0x326, 0x390, 0x3B0 } }, ///< Cheeks are only indexed by their gender/ethnicity rubidium@6190: /* PFV_CHIN */ { 7, 2, { 4, 1, 2, 2 }, { 0x327, 0x327, 0x391, 0x3B1 } }, rubidium@6190: /* PFV_EYEBROWS */ { 9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } }, rubidium@6190: /* PFV_MOUSTACHE */ { 13, 2, { 3, 0, 3, 0 }, { 0x367, 0, 0x397, 0 } }, ///< Depends on PFV_HAS_MOUSTACHE rubidium@6345: /* PFV_LIPS */ { 13, 4, { 12, 10, 9, 9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } }, ///< Depends on !PFV_HAS_MOUSTACHE rubidium@6190: /* PFV_NOSE */ { 17, 3, { 8, 4, 4, 5 }, { 0x349, 0x34C, 0x393, 0x3B3 } }, ///< Depends on !PFV_HAS_MOUSTACHE rubidium@6190: /* PFV_HAIR */ { 20, 4, { 9, 5, 5, 4 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } }, rubidium@6190: /* PFV_JACKET */ { 24, 2, { 3, 3, 3, 3 }, { 0x36B, 0x378, 0x36B, 0x378 } }, rubidium@6190: /* PFV_COLLAR */ { 26, 2, { 4, 4, 4, 4 }, { 0x36E, 0x37B, 0x36E, 0x37B } }, rubidium@6190: /* PFV_TIE_EARRING */ { 28, 3, { 6, 3, 6, 3 }, { 0x372, 0x37F, 0x372, 0x3D1 } }, ///< Depends on PFV_HAS_TIE_EARRING rubidium@6190: /* PFV_GLASSES */ { 31, 1, { 2, 2, 2, 2 }, { 0x347, 0x347, 0x3AE, 0x3AE } } ///< Depends on PFV_HAS_GLASSES rubidium@6190: }; rubidium@6190: assert_compile(lengthof(_pf_info) == PFV_END); rubidium@6190: rubidium@6190: /** rubidium@6190: * Gets the player's face bits for the given player face variable rubidium@6190: * @param pf the face to extract the bits from rubidium@6190: * @param pfv the face variable to get the data of rubidium@6190: * @param ge the gender and ethnicity of the face rubidium@6190: * @pre _pf_info[pfv].valid_values[ge] != 0 rubidium@6190: * @return the requested bits rubidium@6190: */ rubidium@6190: static inline uint GetPlayerFaceBits(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge) rubidium@6190: { rubidium@6190: assert(_pf_info[pfv].valid_values[ge] != 0); rubidium@6190: rubidium@6190: return GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length); rubidium@6190: } rubidium@6190: rubidium@6190: /** rubidium@6190: * Sets the player's face bits for the given player face variable rubidium@6190: * @param pf the face to write the bits to rubidium@6190: * @param pfv the face variable to write the data of rubidium@6190: * @param ge the gender and ethnicity of the face rubidium@6190: * @param val the new value rubidium@6190: * @pre val < _pf_info[pfv].valid_values[ge] rubidium@6190: */ rubidium@6190: static inline void SetPlayerFaceBits(PlayerFace &pf, PlayerFaceVariable pfv, GenderEthnicity ge, uint val) rubidium@6190: { rubidium@6190: assert(val < _pf_info[pfv].valid_values[ge]); rubidium@6190: rubidium@6190: SB(pf, _pf_info[pfv].offset, _pf_info[pfv].length, val); rubidium@6190: } rubidium@6190: rubidium@6190: /** rubidium@7734: * Increase/Decrease the player face variable by the given amount. rubidium@7734: * If the new value greater than the max value for this variable it will be set to 0. rubidium@7734: * Or is it negativ (< 0) it will be set to max value. rubidium@7734: * rubidium@7734: * @param pf the player face to write the bits to rubidium@7734: * @param pfv the player face variable to write the data of rubidium@7734: * @param ge the gender and ethnicity of the player face rubidium@7734: * @param amount the amount which change the value rubidium@7734: * rubidium@7734: * @pre 0 <= val < _pf_info[pfv].valid_values[ge] rubidium@7734: */ rubidium@7734: static inline void IncreasePlayerFaceBits(PlayerFace &pf, PlayerFaceVariable pfv, GenderEthnicity ge, int8 amount) rubidium@7734: { rubidium@7734: int8 val = GetPlayerFaceBits(pf, pfv, ge) + amount; // the new value for the pfv rubidium@7734: rubidium@7734: /* scales the new value to the correct scope */ rubidium@7734: if (val >= _pf_info[pfv].valid_values[ge]) { rubidium@7734: val = 0; rubidium@7734: } else if (val < 0) { rubidium@7734: val = _pf_info[pfv].valid_values[ge] - 1; rubidium@7734: } rubidium@7734: rubidium@7734: SetPlayerFaceBits(pf, pfv, ge, val); // save the new value rubidium@7734: } rubidium@7734: rubidium@7734: /** rubidium@6190: * Checks whether the player bits have a valid range rubidium@6190: * @param pf the face to extract the bits from rubidium@6190: * @param pfv the face variable to get the data of rubidium@6190: * @param ge the gender and ethnicity of the face rubidium@6190: * @return true if and only if the bits are valid rubidium@6190: */ rubidium@6190: static inline bool ArePlayerFaceBitsValid(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge) rubidium@6190: { rubidium@6190: return GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length) < _pf_info[pfv].valid_values[ge]; rubidium@6190: } rubidium@6190: rubidium@6190: /** rubidium@6190: * Scales a player face bits variable to the correct scope rubidium@6190: * @param pfv the face variable to write the data of rubidium@6190: * @param ge the gender and ethnicity of the face rubidium@6190: * @param val the to value to scale rubidium@6190: * @pre val < (1U << _pf_info[pfv].length), i.e. val has a value of 0..2^(bits used for this variable)-1 rubidium@6190: * @return the scaled value rubidium@6190: */ rubidium@6190: static inline uint ScalePlayerFaceValue(PlayerFaceVariable pfv, GenderEthnicity ge, uint val) rubidium@6190: { rubidium@6190: assert(val < (1U << _pf_info[pfv].length)); rubidium@6190: rubidium@6190: return (val * _pf_info[pfv].valid_values[ge]) >> _pf_info[pfv].length; rubidium@6190: } rubidium@6190: rubidium@6190: /** rubidium@7734: * Scales all player face bits to the correct scope rubidium@7734: * rubidium@7734: * @param pf the player face to write the bits to rubidium@7734: */ rubidium@7734: static inline void ScaleAllPlayerFaceBits(PlayerFace &pf) rubidium@7734: { rubidium@7734: IncreasePlayerFaceBits(pf, PFV_ETHNICITY, GE_WM, 0); // scales the ethnicity rubidium@7734: rubidium@7734: GenderEthnicity ge = (GenderEthnicity)GB(pf, _pf_info[PFV_GEN_ETHN].offset, _pf_info[PFV_GEN_ETHN].length); // gender & ethnicity of the face rubidium@7734: rubidium@7734: /* Is a male face with moustache. Need to reduce CPU load in the loop. */ skidd13@7928: bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE, ge) != 0; rubidium@7734: rubidium@7734: for (PlayerFaceVariable pfv = PFV_EYE_COLOUR; pfv < PFV_END; pfv++) { // scales all other variables rubidium@7734: rubidium@7734: /* The moustache variable will be scaled only if it is a male face with has a moustache */ rubidium@7734: if (pfv != PFV_MOUSTACHE || is_moust_male) { rubidium@7734: IncreasePlayerFaceBits(pf, pfv, ge, 0); rubidium@7734: } rubidium@7734: } rubidium@7734: } rubidium@7734: rubidium@7734: /** rubidium@7734: * Make a random new face. rubidium@7734: * If it is for the advanced player face window then the new face have the same gender rubidium@7734: * and ethnicity as the old one, else the gender is equal and the ethnicity is random. rubidium@7734: * rubidium@7734: * @param pf the player face to write the bits to rubidium@7734: * @param ge the gender and ethnicity of the old player face rubidium@7734: * @param adv if it for the advanced player face window rubidium@7734: * rubidium@7734: * @pre scale 'ge' to a valid gender/ethnicity combination rubidium@7734: */ rubidium@7734: static inline void RandomPlayerFaceBits(PlayerFace &pf, GenderEthnicity ge, bool adv) rubidium@7734: { rubidium@7754: pf = InteractiveRandom(); // random all player face bits rubidium@7734: rubidium@7734: /* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */ glx@7741: ge = (GenderEthnicity)((uint)ge % GE_END); rubidium@7734: rubidium@7734: /* set the gender (and ethnicity) for the new player face */ rubidium@7734: if (adv) { rubidium@7734: SetPlayerFaceBits(pf, PFV_GEN_ETHN, ge, ge); rubidium@7734: } else { skidd13@7928: SetPlayerFaceBits(pf, PFV_GENDER, ge, HasBit(ge, GENDER_FEMALE)); rubidium@7734: } rubidium@7734: rubidium@7734: /* scales all player face bits to the correct scope */ rubidium@7734: ScaleAllPlayerFaceBits(pf); rubidium@7734: } rubidium@7734: rubidium@7734: /** rubidium@6190: * Gets the sprite to draw for the given player face variable rubidium@6190: * @param pf the face to extract the data from rubidium@6190: * @param pfv the face variable to get the sprite of rubidium@6190: * @param ge the gender and ethnicity of the face rubidium@6190: * @pre _pf_info[pfv].valid_values[ge] != 0 rubidium@6190: * @return sprite to draw rubidium@6190: */ rubidium@6190: static inline SpriteID GetPlayerFaceSprite(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge) rubidium@6190: { rubidium@6190: assert(_pf_info[pfv].valid_values[ge] != 0); rubidium@6190: rubidium@6190: return _pf_info[pfv].first_sprite[ge] + GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length); rubidium@6190: } rubidium@6190: rubidium@6190: void DrawPlayerFace(PlayerFace face, int color, int x, int y); rubidium@6190: PlayerFace ConvertFromOldPlayerFace(uint32 face); rubidium@6190: bool IsValidPlayerFace(PlayerFace pf); rubidium@6190: rubidium@6190: #endif /* PLAYER_FACE_H */