(svn r8969) -Codechange: rework of the player face bits.
authorrubidium
Fri, 02 Mar 2007 01:17:11 +0000
changeset 6516 ee6d057b9850
parent 6515 d2d05d42690f
child 6517 0bb604919089
(svn r8969) -Codechange: rework of the player face bits.
- introduce a new format (with backward compatability) that is more clear and needs a much simpler face drawer
- replace tons of ifs/switches/magic numbers by table lookups
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/economy.cpp
src/functions.h
src/misc_cmd.cpp
src/misc_gui.cpp
src/openttd.cpp
src/player.h
src/player_face.h
src/player_gui.cpp
src/players.cpp
src/saveload.cpp
--- a/projects/openttd.vcproj	Fri Mar 02 00:45:08 2007 +0000
+++ b/projects/openttd.vcproj	Fri Mar 02 01:17:11 2007 +0000
@@ -555,6 +555,9 @@
 				RelativePath=".\..\src\player.h">
 			</File>
 			<File
+				RelativePath=".\..\src\player_face.h">
+			</File>
+			<File
 				RelativePath=".\..\src\queue.h">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Fri Mar 02 00:45:08 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Fri Mar 02 01:17:11 2007 +0000
@@ -968,6 +968,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\player_face.h"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\queue.h"
 				>
 			</File>
--- a/source.list	Fri Mar 02 00:45:08 2007 +0000
+++ b/source.list	Fri Mar 02 01:17:11 2007 +0000
@@ -152,6 +152,7 @@
 openttd.h
 pathfind.h
 player.h
+player_face.h
 queue.h
 rail.h
 road_cmd.h
--- a/src/economy.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/economy.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -36,6 +36,7 @@
 #include "unmovable.h"
 #include "date.h"
 #include "cargotype.h"
+#include "player_face.h"
 
 /* Score info */
 const ScoreInfo _score_info[] = {
--- a/src/functions.h	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/functions.h	Fri Mar 02 01:17:11 2007 +0000
@@ -106,9 +106,6 @@
 uint32 InteractiveRandom(void); // Used for random sequences that are not the same on the other end of the multiplayer link
 uint InteractiveRandomRange(uint max);
 
-/* facedraw.cpp */
-void DrawPlayerFace(uint32 face, int color, int x, int y);
-
 /* texteff.cpp */
 void MoveAllTextEffects(void);
 void AddTextEffect(StringID msg, int x, int y, uint16 duration);
--- a/src/misc_cmd.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/misc_cmd.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -14,6 +14,7 @@
 #include "network/network.h"
 #include "variables.h"
 #include "livery.h"
+#include "player_face.h"
 
 /** Change the player's face.
  * @param tile unused
@@ -22,8 +23,12 @@
  */
 int32 CmdSetPlayerFace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
+	PlayerFace pf = (PlayerFace)p2;
+
+	if (!IsValidPlayerFace(pf)) return CMD_ERROR;
+
 	if (flags & DC_EXEC) {
-		GetPlayer(_current_player)->face = p2;
+		GetPlayer(_current_player)->face = pf;
 		MarkWholeScreenDirty();
 	}
 	return 0;
--- a/src/misc_gui.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/misc_gui.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -31,6 +31,7 @@
 #include "settings.h"
 #include "date.h"
 #include "cargotype.h"
+#include "player_face.h"
 
 #include "fios.h"
 /* Variables to display file lists */
--- a/src/openttd.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/openttd.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -55,6 +55,7 @@
 #include "clear_map.h"
 #include "fontcache.h"
 #include "newgrf_config.h"
+#include "player_face.h"
 
 #include "bridge_map.h"
 #include "clear_map.h"
@@ -1817,6 +1818,8 @@
 		}
 	}
 
+	if (CheckSavegameVersion(49)) FOR_ALL_PLAYERS(p) p->face = ConvertFromOldPlayerFace(p->face);
+
 	return true;
 }
 
--- a/src/player.h	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/player.h	Fri Mar 02 01:17:11 2007 +0000
@@ -147,6 +147,7 @@
 } PlayerAiNew;
 
 
+typedef uint32 PlayerFace;
 
 typedef struct Player {
 	uint32 name_2;
@@ -155,7 +156,7 @@
 	uint16 president_name_1;
 	uint32 president_name_2;
 
-	uint32 face;
+	PlayerFace face;
 
 	int32 player_money;
 	int32 current_loan;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/player_face.h	Fri Mar 02 01:17:11 2007 +0000
@@ -0,0 +1,153 @@
+/* $Id$ */
+
+/** @file player_face.h Functionality related to the player's face */
+
+#ifndef PLAYER_FACE_H
+#define PLAYER_FACE_H
+
+/** The gender/race combinations that we have faces for */
+enum GenderEthnicity {
+	GENDER_FEMALE    = 0, ///< This bit set means a female, otherwise male
+	ETHNICITY_BLACK  = 1, ///< This bit set means black, otherwise white
+
+	GE_WM = 0,                                         ///< A male of Caucasian origin (white)
+	GE_WF = 1 << GENDER_FEMALE,                        ///< A female of Caucasian origin (white)
+	GE_BM = 1 << ETHNICITY_BLACK,                      ///< A male of African origin (black)
+	GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE, ///< A female of African origin (black)
+	GE_END,
+};
+DECLARE_ENUM_AS_BIT_SET(GenderEthnicity); ///< See GenderRace as a bitset
+
+/** Bitgroups of the PlayerFace variable */
+enum PlayerFaceVariable {
+	PFV_GENDER,
+	PFV_ETHNICITY,
+	PFV_GEN_ETHN,
+	PFV_HAS_MOUSTACHE,
+	PFV_HAS_TIE_EARRING,
+	PFV_HAS_GLASSES,
+	PFV_EYE_COLOUR,
+	PFV_CHEEKS,
+	PFV_CHIN,
+	PFV_EYEBROWS,
+	PFV_MOUSTACHE,
+	PFV_LIPS,
+	PFV_NOSE,
+	PFV_HAIR,
+	PFV_JACKET,
+	PFV_COLLAR,
+	PFV_TIE_EARRING,
+	PFV_GLASSES,
+	PFV_END
+};
+DECLARE_POSTFIX_INCREMENT(PlayerFaceVariable);
+
+/** Information about the valid values of PlayerFace bitgroups as well as the sprites to draw */
+struct PlayerFaceBitsInfo {
+	byte     offset;               ///< Offset in bits into the PlayerFace
+	byte     length;               ///< Number of bits used in the PlayerFace
+	byte     valid_values[GE_END]; ///< The number of valid values per gender/ethnicity
+	SpriteID first_sprite[GE_END]; ///< The first sprite per gender/ethnicity
+};
+
+/** Lookup table for indices into the PlayerFace, valid ranges and sprites */
+static const PlayerFaceBitsInfo _pf_info[] = {
+	/* Index                   off len   WM  WF  BM  BF         WM     WF     BM     BF */
+	/* PFV_GENDER          */ {  0, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< 0 = male, 1 = female
+	/* PFV_ETHNICITY       */ {  1, 2, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< 0 = (Western-)Caucasian, 1 = African(-American)/Black
+	/* PFV_GEN_ETHN        */ {  0, 3, {  4,  4,  4,  4 }, {     0,     0,     0,     0 } }, ///< Shortcut to get/set gender _and_ ethnicity
+	/* PFV_HAS_MOUSTACHE   */ {  3, 1, {  2,  0,  2,  0 }, {     0,     0,     0,     0 } }, ///< Females do not have a moustache
+	/* 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.
+	/* PFV_HAS_GLASSES     */ {  4, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< Whether to draw glasses or not
+	/* PFV_EYE_COLOUR      */ {  5, 2, {  3,  3,  3,  3 }, {     0,     0,     0,     0 } }, ///< Palette modification
+	/* PFV_CHEEKS          */ {  0, 0, {  1,  1,  1,  1 }, { 0x325, 0x326, 0x390, 0x3B0 } }, ///< Cheeks are only indexed by their gender/ethnicity
+	/* PFV_CHIN            */ {  7, 2, {  4,  1,  2,  2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
+	/* PFV_EYEBROWS        */ {  9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
+	/* PFV_MOUSTACHE       */ { 13, 2, {  3,  0,  3,  0 }, { 0x367,     0, 0x397,     0 } }, ///< Depends on PFV_HAS_MOUSTACHE
+	/* PFV_LIPS            */ { 13, 4, { 13, 10,  9,  9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } }, ///< Depends on !PFV_HAS_MOUSTACHE
+	/* PFV_NOSE            */ { 17, 3, {  8,  4,  4,  5 }, { 0x349, 0x34C, 0x393, 0x3B3 } }, ///< Depends on !PFV_HAS_MOUSTACHE
+	/* PFV_HAIR            */ { 20, 4, {  9,  5,  5,  4 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
+	/* PFV_JACKET          */ { 24, 2, {  3,  3,  3,  3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
+	/* PFV_COLLAR          */ { 26, 2, {  4,  4,  4,  4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
+	/* PFV_TIE_EARRING     */ { 28, 3, {  6,  3,  6,  3 }, { 0x372, 0x37F, 0x372, 0x3D1 } }, ///< Depends on PFV_HAS_TIE_EARRING
+	/* PFV_GLASSES         */ { 31, 1, {  2,  2,  2,  2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }  ///< Depends on PFV_HAS_GLASSES
+};
+assert_compile(lengthof(_pf_info) == PFV_END);
+
+/**
+ * Gets the player's face bits for the given player face variable
+ * @param pf  the face to extract the bits from
+ * @param pfv the face variable to get the data of
+ * @param ge  the gender and ethnicity of the face
+ * @pre _pf_info[pfv].valid_values[ge] != 0
+ * @return the requested bits
+ */
+static inline uint GetPlayerFaceBits(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge)
+{
+	assert(_pf_info[pfv].valid_values[ge] != 0);
+
+	return GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length);
+}
+
+/**
+ * Sets the player's face bits for the given player face variable
+ * @param pf  the face to write the bits to
+ * @param pfv the face variable to write the data of
+ * @param ge  the gender and ethnicity of the face
+ * @param val the new value
+ * @pre val < _pf_info[pfv].valid_values[ge]
+ */
+static inline void SetPlayerFaceBits(PlayerFace &pf, PlayerFaceVariable pfv, GenderEthnicity ge, uint val)
+{
+	assert(val < _pf_info[pfv].valid_values[ge]);
+
+	SB(pf, _pf_info[pfv].offset, _pf_info[pfv].length, val);
+}
+
+/**
+ * Checks whether the player bits have a valid range
+ * @param pf  the face to extract the bits from
+ * @param pfv the face variable to get the data of
+ * @param ge  the gender and ethnicity of the face
+ * @return true if and only if the bits are valid
+ */
+static inline bool ArePlayerFaceBitsValid(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge)
+{
+	return GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length) < _pf_info[pfv].valid_values[ge];
+}
+
+/**
+ * Scales a player face bits variable to the correct scope
+ * @param pfv the face variable to write the data of
+ * @param ge  the gender and ethnicity of the face
+ * @param val the to value to scale
+ * @pre val < (1U << _pf_info[pfv].length), i.e. val has a value of 0..2^(bits used for this variable)-1
+ * @return the scaled value
+ */
+static inline uint ScalePlayerFaceValue(PlayerFaceVariable pfv, GenderEthnicity ge, uint val)
+{
+	assert(val < (1U << _pf_info[pfv].length));
+
+	return (val * _pf_info[pfv].valid_values[ge]) >> _pf_info[pfv].length;
+}
+
+/**
+ * Gets the sprite to draw for the given player face variable
+ * @param pf  the face to extract the data from
+ * @param pfv the face variable to get the sprite of
+ * @param ge  the gender and ethnicity of the face
+ * @pre _pf_info[pfv].valid_values[ge] != 0
+ * @return sprite to draw
+ */
+static inline SpriteID GetPlayerFaceSprite(PlayerFace pf, PlayerFaceVariable pfv, GenderEthnicity ge)
+{
+	assert(_pf_info[pfv].valid_values[ge] != 0);
+
+	return _pf_info[pfv].first_sprite[ge] + GB(pf, _pf_info[pfv].offset, _pf_info[pfv].length);
+}
+
+void DrawPlayerFace(PlayerFace face, int color, int x, int y);
+PlayerFace ConvertFromOldPlayerFace(uint32 face);
+bool IsValidPlayerFace(PlayerFace pf);
+
+#endif /* PLAYER_FACE_H */
--- a/src/player_gui.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/player_gui.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -21,6 +21,7 @@
 #include "newgrf.h"
 #include "network/network_data.h"
 #include "network/network_client.h"
+#include "player_face.h"
 
 static void DoShowPlayerFinances(PlayerID player, bool show_small, bool show_stickied);
 
@@ -505,6 +506,44 @@
 	SelectPlayerLiveryWndProc
 };
 
+/**
+ * Draws the face of a player.
+ * @param pf    the player's face
+ * @param color the (background) color of the gradient
+ * @param x     x-position to draw the face
+ * @param y     y-position to draw the face
+ */
+void DrawPlayerFace(PlayerFace pf, int color, int x, int y)
+{
+	GenderEthnicity ge = (GenderEthnicity)GetPlayerFaceBits(pf, PFV_GEN_ETHN, GE_WM);
+
+	bool has_moustache   = !HASBIT(ge, GENDER_FEMALE) && GetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE,   ge) != 0;
+	bool has_tie_earring = !HASBIT(ge, GENDER_FEMALE) || GetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge) != 0;
+	bool has_glasses     = GetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge) != 0;
+	SpriteID pal;
+	switch (GetPlayerFaceBits(pf, PFV_EYE_COLOUR, ge)) {
+		default: NOT_REACHED();
+		case 0: pal = PALETTE_TO_BROWN; break;
+		case 1: pal = PALETTE_TO_BLUE;  break;
+		case 2: pal = PALETTE_TO_GREEN; break;
+	}
+
+	/* Draw the gradient (background) */
+	DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOR(color), x, y);
+
+	for (PlayerFaceVariable pfv = PFV_CHEEKS; pfv < PFV_END; pfv++) {
+		switch (pfv) {
+			case PFV_MOUSTACHE:   if (!has_moustache)   continue; break;
+			case PFV_LIPS:        /* FALL THROUGH */
+			case PFV_NOSE:        if (has_moustache)    continue; break;
+			case PFV_TIE_EARRING: if (!has_tie_earring) continue; break;
+			case PFV_GLASSES:     if (!has_glasses)     continue; break;
+			default: break;
+		}
+		DrawSprite(GetPlayerFaceSprite(pf, pfv, ge), (pfv == PFV_EYEBROWS) ? pal : PAL_NONE, x, y);
+	}
+}
+
 static void SelectPlayerFaceWndProc(Window *w, WindowEvent *e)
 {
 	switch (e->event) {
@@ -531,7 +570,7 @@
 			SetWindowDirty(w);
 			break;
 		case 7:
-			WP(w,facesel_d).face = (WP(w,facesel_d).gender << 31) + GB(InteractiveRandom(), 0, 31);
+			WP(w,facesel_d).face = ConvertFromOldPlayerFace((WP(w, facesel_d).gender << 31) + GB(InteractiveRandom(), 0, 31));
 			SetWindowDirty(w);
 			break;
 		}
--- a/src/players.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/players.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -26,6 +26,7 @@
 #include "ai/ai.h"
 #include "date.h"
 #include "window.h"
+#include "player_face.h"
 
 /**
  * Sets the local player and updates the patch settings that are set on a
@@ -64,22 +65,10 @@
 	DrawSprite(SPR_PLAYER_ICON, PLAYER_SPRITE_COLOR(p), x, y);
 }
 
-/** The gender/race combinations that we have faces for */
-enum GenderRace {
-	GENDER_FEMALE = 0,                                    ///< This bit set means a female, otherwise male
-	RACE_BLACK    = 1,                                    ///< This bit set means black, otherwise white
-
-	WHITE_MALE    = 0,                                    ///< A male of Caucasian origin
-	WHITE_FEMALE  = 1 << GENDER_FEMALE,                   ///< A female of Caucasian origin
-	BLACK_MALE    = 1 << RACE_BLACK,                      ///< A male of African origin
-	BLACK_FEMALE  = 1 << RACE_BLACK | 1 << GENDER_FEMALE, ///< A female of African origin
-};
-DECLARE_ENUM_AS_BIT_SET(GenderRace); ///< See GenderRace as a bitset
-
 /**
- * Draws the face of a player.
+ * Converts an old player face format to the new player face format
  *
- * Meaning of the bits in face (some bits are used in several times):
+ * Meaning of the bits in the old face (some bits are used in several times):
  * - 4 and 5: chin
  * - 6 to 9: eyebrows
  * - 10 to 13: nose
@@ -91,124 +80,87 @@
  * - 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white)
  * - 31: gender (0 = male, 1 = female)
  *
- * @param face  the bit-encoded representation of the face
- * @param color the (background) color of the gradient
- * @param x     x-position to draw the face
- * @param y     y-position to draw the face
- *
- * @note all magic hexadecimal numbers in this function as sprite IDs.
- * @todo replace magic hexadecimal numbers with enums
+ * @param face the face in the old format
+ * @return the face in the new format
  */
-void DrawPlayerFace(uint32 face, int color, int x, int y)
+PlayerFace ConvertFromOldPlayerFace(uint32 face)
 {
-	GenderRace gen_race = WHITE_MALE;
-
-	if (HASBIT(face, 31)) SetBitT(gen_race, GENDER_FEMALE);
-	if (HASBIT(face, 27) && (HASBIT(face, 26) == HASBIT(face, 19))) SetBitT(gen_race, RACE_BLACK);
-
-	/* Draw the gradient (background) */
-	DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOR(color), x, y);
-
-	/* Draw the cheeks */
-	static const SpriteID cheeks_table[] = { 0x325, 0x326, 0x390, 0x3B0 };
-	DrawSprite(cheeks_table[gen_race], PAL_NONE, x, y);
+	PlayerFace pf = 0;
+	GenderEthnicity ge = GE_WM;
 
-	/* Draw the chin */
-	uint chin = GB(face, 4, 2);
-	if (HASBIT(gen_race, RACE_BLACK)) {
-		DrawSprite((HASBIT(gen_race, GENDER_FEMALE) ? 0x3B1 : 0x391) + (chin >> 1), PAL_NONE, x, y);
-	} else {
-		DrawSprite(0x327 + (HASBIT(gen_race, GENDER_FEMALE) ? 0 : chin), PAL_NONE, x, y);
-	}
-
-	/* Draw the eyes */
-	uint eye_colour = GB(face, 20, 3);
-	uint eyebrows   = GB(face,  6, 4);
-	SpriteID pal;
+	if (HASBIT(face, 31)) SetBitT(ge, GENDER_FEMALE);
+	if (HASBIT(face, 27) && (HASBIT(face, 26) == HASBIT(face, 19))) SetBitT(ge, ETHNICITY_BLACK);
 
-	if (eye_colour < 6) {
-		pal = PALETTE_TO_BROWN;
-	} else if (eye_colour == 6) {
-		pal = PALETTE_TO_BLUE;
+	SetPlayerFaceBits(pf, PFV_GEN_ETHN,    ge, ge);
+	SetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
+	SetPlayerFaceBits(pf, PFV_EYE_COLOUR,  ge, clampu(GB(face, 20, 3), 5, 7) - 5);
+	SetPlayerFaceBits(pf, PFV_CHIN,        ge, ScalePlayerFaceValue(PFV_CHIN,     ge, GB(face,  4, 2)));
+	SetPlayerFaceBits(pf, PFV_EYEBROWS,    ge, ScalePlayerFaceValue(PFV_EYEBROWS, ge, GB(face,  6, 4)));
+	SetPlayerFaceBits(pf, PFV_HAIR,        ge, ScalePlayerFaceValue(PFV_HAIR,     ge, GB(face, 16, 4)));
+	SetPlayerFaceBits(pf, PFV_JACKET,      ge, ScalePlayerFaceValue(PFV_JACKET,   ge, GB(face, 20, 2)));
+	SetPlayerFaceBits(pf, PFV_COLLAR,      ge, ScalePlayerFaceValue(PFV_COLLAR,   ge, GB(face, 22, 2)));
+	SetPlayerFaceBits(pf, PFV_GLASSES,     ge, GB(face, 28, 1));
+
+	uint lips = GB(face, 10, 4);
+	if (!HASBIT(ge, GENDER_FEMALE) && lips < 4) {
+		SetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE, ge, true);
+		SetPlayerFaceBits(pf, PFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
 	} else {
-		pal = PALETTE_TO_GREEN;
-	}
+		if (!HASBIT(ge, GENDER_FEMALE)) {
+			lips -= 3;
+			if (HASBIT(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
+		} else {
+			lips = ScalePlayerFaceValue(PFV_LIPS, ge, lips);
+		}
+		SetPlayerFaceBits(pf, PFV_LIPS, ge, lips);
 
-	switch (gen_race) {
-		case WHITE_MALE:   DrawSprite(0x32B + (eyebrows * 12 >> 4), pal, x, y); break;
-		case WHITE_FEMALE: DrawSprite(0x337 + eyebrows,             pal, x, y); break;
-		case BLACK_MALE:   DrawSprite(0x39A + (eyebrows * 11 >> 4), pal, x, y); break;
-		case BLACK_FEMALE: DrawSprite(0x3B8 + eyebrows,             pal, x, y); break;
+		uint nose = GB(face, 13, 3);
+		if (ge == GE_WF) {
+			nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
+		} else {
+			nose = ScalePlayerFaceValue(PFV_NOSE, ge, nose);
+		}
+		SetPlayerFaceBits(pf, PFV_NOSE, ge, nose);
 	}
 
-	/* Draw the mouth */
-	uint nose = GB(face, 13, 3);
-	uint lips = GB(face, 10, 4);
-
-	if (!HASBIT(gen_race, GENDER_FEMALE)) {
-		lips = (lips * 15 >> 4);
-
-		if (lips < 3) {
-			/* Moustache, including nose and lips */
-			DrawSprite((HASBIT(gen_race, RACE_BLACK) ? 0x397 : 0x367) + lips, PAL_NONE, x, y);
-
-			/* Skip the rest */
-			goto skip_mouth;
-		}
-
-		/* Lips */
-		lips -= 3;
-		if (HASBIT(gen_race, RACE_BLACK)) {
-			if (lips > 8) lips = 0;
-			lips += 0x3A5 - 0x35B;
-		}
-		DrawSprite(lips + 0x35B, PAL_NONE, x, y);
-	} else if (HASBIT(gen_race, RACE_BLACK)) {
-		/* Female lips with make up */
-		DrawSprite((lips * 9 >> 4) + 0x3C8, PAL_NONE, x, y);
-	} else {
-		/* Female lips */
-		DrawSprite((lips * 10 >> 4) + 0x351, PAL_NONE, x, y);
+	uint tie_earring = GB(face, 24, 4);
+	if (!HASBIT(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
+		if (HASBIT(ge, GENDER_FEMALE)) SetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge, true);
+		SetPlayerFaceBits(pf, PFV_TIE_EARRING, ge, HASBIT(ge, GENDER_FEMALE) ? tie_earring : ScalePlayerFaceValue(PFV_TIE_EARRING, ge, tie_earring / 2));
 	}
 
-	{
-		/* Nose */
-		static const SpriteID mouth_table[] = { 0x34C, 0x34D, 0x34F };
-		switch (gen_race) {
-			case WHITE_MALE:   DrawSprite(0x349 + nose,                 PAL_NONE, x, y); break;
-			case WHITE_FEMALE: DrawSprite(mouth_table[(nose * 3 >> 3)], PAL_NONE, x, y); break;
-			case BLACK_MALE:   DrawSprite(0x393 + (nose & 3),           PAL_NONE, x, y); break;
-			case BLACK_FEMALE: DrawSprite(0x3B3 + (nose * 5 >> 3),      PAL_NONE, x, y); break;
+	return pf;
+}
+
+/**
+ * Checks whether a player's face is a valid encoding.
+ * Unused bits are not enforced to be 0.
+ * @param pf the fact to check
+ * @return true if and only if the face is valid
+ */
+bool IsValidPlayerFace(PlayerFace pf)
+{
+	if (!ArePlayerFaceBitsValid(pf, PFV_GEN_ETHN, GE_WM)) return false;
+
+	GenderEthnicity ge   = (GenderEthnicity)GetPlayerFaceBits(pf, PFV_GEN_ETHN, GE_WM);
+	bool has_moustache   = !HASBIT(ge, GENDER_FEMALE) && GetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE,   ge) != 0;
+	bool has_tie_earring = !HASBIT(ge, GENDER_FEMALE) || GetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge) != 0;
+	bool has_glasses     = GetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge) != 0;
+
+	if (!ArePlayerFaceBitsValid(pf, PFV_EYE_COLOUR, ge)) return false;
+	for (PlayerFaceVariable pfv = PFV_CHEEKS; pfv < PFV_END; pfv++) {
+		switch (pfv) {
+			case PFV_MOUSTACHE:   if (!has_moustache)   continue; break;
+			case PFV_LIPS:        /* FALL THROUGH */
+			case PFV_NOSE:        if (has_moustache)    continue; break;
+			case PFV_TIE_EARRING: if (!has_tie_earring) continue; break;
+			case PFV_GLASSES:     if (!has_glasses)     continue; break;
+			default: break;
 		}
-	}
-skip_mouth:
-
-	/* Draw the hair */
-	uint hair = GB(face, 16, 4);
-	switch (gen_race) {
-		case WHITE_MALE:   DrawSprite(0x382 + (hair * 9 >> 4), PAL_NONE, x, y); break;
-		case WHITE_FEMALE: DrawSprite(0x38B + (hair * 5 >> 4), PAL_NONE, x, y); break;
-		case BLACK_MALE:   DrawSprite(0x3D4 + (hair * 5 >> 4), PAL_NONE, x, y); break;
-		case BLACK_FEMALE: DrawSprite(0x3D9 + (hair * 5 >> 4), PAL_NONE, x, y); break;
+		if (!ArePlayerFaceBitsValid(pf, pfv, ge)) return false;
 	}
 
-	/* Draw the tie, ear rings etc. */
-	uint tie = GB(face, 20, 8);
-	if (HASBIT(gen_race, GENDER_FEMALE)) {
-		DrawSprite(0x378 + (GB(tie, 0, 2) * 3 >> 2), PAL_NONE, x, y);
-		DrawSprite(0x37B + (GB(tie, 2, 2) * 4 >> 2), PAL_NONE, x, y);
-
-		tie >>= 4;
-		if (tie < 3) DrawSprite((HASBIT(gen_race, RACE_BLACK) ? 0x3D1 : 0x37F) + tie, PAL_NONE, x, y);
-	} else {
-		DrawSprite(0x36B + (GB(tie, 0, 2) * 3 >> 2), PAL_NONE, x, y);
-		DrawSprite(0x36E + (GB(tie, 2, 2) * 4 >> 2), PAL_NONE, x, y);
-		DrawSprite(0x372 + (GB(tie, 4, 4) * 6 >> 4), PAL_NONE, x, y);
-	}
-
-	/* draw the glasses */
-	uint glasses = GB(face, 28, 3);
-	if (glasses <= 1) DrawSprite((HASBIT(gen_race, RACE_BLACK) ? 0x3AE : 0x347) + glasses, PAL_NONE, x, y);
+	return true;
 }
 
 void InvalidatePlayerWindows(const Player *p)
@@ -519,7 +471,7 @@
 
 	p->avail_railtypes = GetPlayerRailtypes(p->index);
 	p->inaugurated_year = _cur_year;
-	p->face = Random();
+	p->face = ConvertFromOldPlayerFace(Random());
 
 	/* Engine renewal settings */
 	p->engine_renew_list = NULL;
--- a/src/saveload.cpp	Fri Mar 02 00:45:08 2007 +0000
+++ b/src/saveload.cpp	Fri Mar 02 01:17:11 2007 +0000
@@ -28,7 +28,7 @@
 #include "variables.h"
 #include <setjmp.h>
 
-extern const uint16 SAVEGAME_VERSION = 48;
+extern const uint16 SAVEGAME_VERSION = 49;
 uint16 _sl_version;       ///< the major savegame version identifier
 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!