Skip to content

Commit 7a1e601

Browse files
committed
Sync some color and skin code with DDNet
1 parent 81a6b87 commit 7a1e601

File tree

8 files changed

+68
-54
lines changed

8 files changed

+68
-54
lines changed

src/base/color.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ class color4_base
137137
return col;
138138
}
139139

140+
DerivedT Multiply(const DerivedT &Other) const
141+
{
142+
DerivedT Color(static_cast<const DerivedT &>(*this));
143+
Color.x *= Other.x;
144+
Color.y *= Other.y;
145+
Color.z *= Other.z;
146+
Color.a *= Other.a;
147+
return Color;
148+
}
149+
140150
template<typename UnpackT>
141151
static UnpackT UnpackAlphaLast(unsigned Color, bool Alpha = true)
142152
{
@@ -166,8 +176,9 @@ class ColorHSLA : public color4_base<ColorHSLA>
166176
ColorHSLA(){};
167177

168178
constexpr static const float DARKEST_LGT = 0.5f;
179+
constexpr static const float DARKEST_LGT7 = 61.0f / 255.0f;
169180

170-
ColorHSLA UnclampLighting(float Darkest = DARKEST_LGT) const
181+
ColorHSLA UnclampLighting(float Darkest) const
171182
{
172183
ColorHSLA col = *this;
173184
col.l = Darkest + col.l * (1.0f - Darkest);

src/engine/console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class IConsole : public IInterface
5353
virtual int GetInteger(unsigned Index) const = 0;
5454
virtual float GetFloat(unsigned Index) const = 0;
5555
virtual const char *GetString(unsigned Index) const = 0;
56-
virtual ColorHSLA GetColor(unsigned Index, bool Light) const = 0;
56+
virtual ColorHSLA GetColor(unsigned Index, float DarkestLighting) const = 0;
5757

5858
int GetClientId() { return m_ClientId; }
5959

src/engine/server/mapconverter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void MakeGrayScale(CImageInfo *pImg)
207207

208208
void SetQuadColor(CQuad *Quad, int Color)
209209
{
210-
ColorRGBA BodyColor = color_cast<ColorRGBA>(ColorHSLA(Color).UnclampLighting());
210+
ColorRGBA BodyColor = color_cast<ColorRGBA>(ColorHSLA(Color).UnclampLighting(ColorHSLA::DARKEST_LGT));
211211
CColor TypedColor;
212212
TypedColor.r = BodyColor.r * 255;
213213
TypedColor.g = BodyColor.g * 255;

src/engine/shared/console.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ float CConsole::CResult::GetFloat(unsigned Index) const
3939
return str_tofloat(m_apArgs[Index]);
4040
}
4141

42-
ColorHSLA CConsole::CResult::GetColor(unsigned Index, bool Light) const
42+
ColorHSLA CConsole::CResult::GetColor(unsigned Index, float DarkestLighting) const
4343
{
4444
ColorHSLA Hsla = ColorHSLA(0, 0, 0);
4545
if(Index >= m_NumArgs)
@@ -49,8 +49,8 @@ ColorHSLA CConsole::CResult::GetColor(unsigned Index, bool Light) const
4949
if(str_isallnum(pStr) || ((pStr[0] == '-' || pStr[0] == '+') && str_isallnum(pStr + 1))) // Teeworlds Color (Packed HSL)
5050
{
5151
Hsla = ColorHSLA(str_toulong_base(pStr, 10), true);
52-
if(Light)
53-
Hsla = Hsla.UnclampLighting();
52+
if(DarkestLighting)
53+
Hsla = Hsla.UnclampLighting(DarkestLighting);
5454
}
5555
else if(*pStr == '$') // Hex RGB
5656
{
@@ -762,7 +762,7 @@ struct CColVariableData
762762
{
763763
IConsole *m_pConsole;
764764
unsigned *m_pVariable;
765-
bool m_Light;
765+
float m_DarkestLighting;
766766
bool m_Alpha;
767767
unsigned m_OldValue;
768768
};
@@ -841,12 +841,12 @@ static void ColVariableCommand(IConsole::IResult *pResult, void *pUserData)
841841

842842
if(pResult->NumArguments())
843843
{
844-
ColorHSLA Col = pResult->GetColor(0, pData->m_Light);
845-
int Val = Col.Pack(pData->m_Light ? 0.5f : 0.0f, pData->m_Alpha);
844+
const ColorHSLA Color = pResult->GetColor(0, pData->m_DarkestLighting);
845+
const unsigned Value = Color.Pack(pData->m_DarkestLighting, pData->m_Alpha);
846846

847-
*(pData->m_pVariable) = Val;
847+
*(pData->m_pVariable) = Value;
848848
if(pResult->m_ClientId != IConsole::CLIENT_ID_GAME)
849-
pData->m_OldValue = Val;
849+
pData->m_OldValue = Value;
850850
}
851851
else
852852
{
@@ -855,13 +855,11 @@ static void ColVariableCommand(IConsole::IResult *pResult, void *pUserData)
855855
pData->m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
856856
pResult->m_Value = *(pData->m_pVariable);
857857

858-
ColorHSLA Hsla(*(pData->m_pVariable), true);
859-
if(pData->m_Light)
860-
Hsla = Hsla.UnclampLighting();
858+
const ColorHSLA Hsla = ColorHSLA(*pData->m_pVariable, true).UnclampLighting(pData->m_DarkestLighting);
861859
str_format(aBuf, sizeof(aBuf), "H: %d°, S: %d%%, L: %d%%", round_truncate(Hsla.h * 360), round_truncate(Hsla.s * 100), round_truncate(Hsla.l * 100));
862860
pData->m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
863861

864-
ColorRGBA Rgba = color_cast<ColorRGBA>(Hsla);
862+
const ColorRGBA Rgba = color_cast<ColorRGBA>(Hsla);
865863
str_format(aBuf, sizeof(aBuf), "R: %d, G: %d, B: %d, #%06X", round_truncate(Rgba.r * 255), round_truncate(Rgba.g * 255), round_truncate(Rgba.b * 255), Rgba.Pack(false));
866864
pData->m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
867865

@@ -996,13 +994,12 @@ void CConsole::ConToggle(IConsole::IResult *pResult, void *pUser)
996994
else if(pfnCallback == ColVariableCommand)
997995
{
998996
CColVariableData *pData = static_cast<CColVariableData *>(pUserData);
999-
bool Light = pData->m_Light;
1000-
float Darkest = Light ? 0.5f : 0.0f;
997+
float DarkestLighting = pData->m_DarkestLighting;
1001998
bool Alpha = pData->m_Alpha;
1002999
unsigned Cur = *pData->m_pVariable;
1003-
ColorHSLA Val = Cur == pResult->GetColor(1, Light).Pack(Darkest, Alpha) ? pResult->GetColor(2, Light) : pResult->GetColor(1, Light);
1000+
ColorHSLA Val = Cur == pResult->GetColor(1, DarkestLighting).Pack(DarkestLighting, Alpha) ? pResult->GetColor(2, DarkestLighting) : pResult->GetColor(1, DarkestLighting);
10041001

1005-
str_format(aBuf, sizeof(aBuf), "%s %u", pResult->GetString(0), Val.Pack(Darkest, Alpha));
1002+
str_format(aBuf, sizeof(aBuf), "%s %u", pResult->GetString(0), Val.Pack(DarkestLighting, Alpha));
10061003
pConsole->ExecuteLine(aBuf);
10071004
aBuf[0] = 0;
10081005
}

src/engine/shared/console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class CConsole : public IConsole
127127
const char *GetString(unsigned Index) const override;
128128
int GetInteger(unsigned Index) const override;
129129
float GetFloat(unsigned Index) const override;
130-
ColorHSLA GetColor(unsigned Index, bool Light) const override;
130+
ColorHSLA GetColor(unsigned Index, float DarkestLighting) const override;
131131

132132
void RemoveArgument(unsigned Index) override
133133
{

src/engine/shared/protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ enum
104104

105105
MAX_NAME_LENGTH = 16,
106106
MAX_CLAN_LENGTH = 12,
107+
MAX_SKIN_LENGTH = 24,
107108

108109
// message packing
109110
MSGFLAG_VITAL = 1,

src/game/server/teeinfo.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#include <base/color.h>
22
#include <base/system.h>
3+
#include <game/generated/protocol7.h>
34

45
#include "teeinfo.h"
56

67
struct StdSkin
78
{
8-
char m_aSkinName[64];
9+
char m_aSkinName[24];
910
// body, marking, decoration, hands, feet, eyes
10-
char m_apSkinPartNames[6][24];
11-
bool m_aUseCustomColors[6];
12-
int m_aSkinPartColors[6];
11+
char m_apSkinPartNames[protocol7::NUM_SKINPARTS][24];
12+
bool m_aUseCustomColors[protocol7::NUM_SKINPARTS];
13+
int m_aSkinPartColors[protocol7::NUM_SKINPARTS];
1314
};
1415

1516
static StdSkin g_aStdSkins[] = {
1617
{"default", {"standard", "", "", "standard", "standard", "standard"}, {true, false, false, true, true, false}, {1798004, 0, 0, 1799582, 1869630, 0}},
17-
{"bluekitty", {"kitty", "whisker", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {8681144, -8229413, 0, 7885547, 7885547, 0}},
18+
{"bluekitty", {"kitty", "whisker", "", "standard", "standard", "negative"}, {true, true, false, true, true, true}, {8681144, -8229413, 0, 7885547, 8868585, 9043712}},
1819
{"bluestripe", {"standard", "stripes", "", "standard", "standard", "standard"}, {true, false, false, true, true, false}, {10187898, 0, 0, 750848, 1944919, 0}},
1920
{"brownbear", {"bear", "bear", "hair", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {1082745, -15634776, 0, 1082745, 1147174, 0}},
2021
{"cammo", {"standard", "cammo2", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {5334342, -11771603, 0, 750848, 1944919, 0}},
2122
{"cammostripes", {"standard", "cammostripes", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {5334342, -14840320, 0, 750848, 1944919, 0}},
2223
{"coala", {"koala", "twinbelly", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {184, -15397662, 0, 184, 9765959, 0}},
23-
{"limekitty", {"kitty", "whisker", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {4612803, -12229920, 0, 3827951, 3827951, 0}},
24+
{"limekitty", {"kitty", "whisker", "", "standard", "standard", "negative"}, {true, true, false, true, true, true}, {4612803, -12229920, 0, 3827951, 3827951, 8256000}},
2425
{"pinky", {"standard", "whisker", "", "standard", "standard", "standard"}, {true, true, false, true, true, false}, {15911355, -801066, 0, 15043034, 15043034, 0}},
2526
{"redbopp", {"standard", "donny", "unibop", "standard", "standard", "standard"}, {true, true, true, true, true, false}, {16177260, -16590390, 16177260, 16177260, 7624169, 0}},
2627
{"redstripe", {"standard", "stripe", "", "standard", "standard", "standard"}, {true, false, false, true, true, false}, {16307835, 0, 0, 184, 9765959, 0}},
@@ -38,9 +39,9 @@ CTeeInfo::CTeeInfo(const char *pSkinName, int UseCustomColor, int ColorBody, int
3839
m_ColorFeet = ColorFeet;
3940
}
4041

41-
CTeeInfo::CTeeInfo(const char *apSkinPartNames[6], const int *pUseCustomColors, const int *pSkinPartColors)
42+
CTeeInfo::CTeeInfo(const char *apSkinPartNames[protocol7::NUM_SKINPARTS], const int *pUseCustomColors, const int *pSkinPartColors)
4243
{
43-
for(int i = 0; i < 6; i++)
44+
for(int i = 0; i < protocol7::NUM_SKINPARTS; i++)
4445
{
4546
str_copy(m_apSkinPartNames[i], apSkinPartNames[i], sizeof(m_apSkinPartNames[i]));
4647
m_aUseCustomColors[i] = pUseCustomColors[i];
@@ -51,7 +52,7 @@ CTeeInfo::CTeeInfo(const char *apSkinPartNames[6], const int *pUseCustomColors,
5152
void CTeeInfo::ToSixup()
5253
{
5354
// reset to default skin
54-
for(int p = 0; p < 6; p++)
55+
for(int p = 0; p < protocol7::NUM_SKINPARTS; p++)
5556
{
5657
str_copy(m_apSkinPartNames[p], g_aStdSkins[0].m_apSkinPartNames[p], 24);
5758
m_aUseCustomColors[p] = g_aStdSkins[0].m_aUseCustomColors[p];
@@ -63,7 +64,7 @@ void CTeeInfo::ToSixup()
6364
{
6465
if(!str_comp(m_aSkinName, StdSkin.m_aSkinName))
6566
{
66-
for(int p = 0; p < 6; p++)
67+
for(int p = 0; p < protocol7::NUM_SKINPARTS; p++)
6768
{
6869
str_copy(m_apSkinPartNames[p], StdSkin.m_apSkinPartNames[p], 24);
6970
m_aUseCustomColors[p] = StdSkin.m_aUseCustomColors[p];
@@ -75,18 +76,18 @@ void CTeeInfo::ToSixup()
7576

7677
if(m_UseCustomColor)
7778
{
78-
int ColorBody = ColorHSLA(m_ColorBody).UnclampLighting().Pack(ms_DarkestLGT7);
79-
int ColorFeet = ColorHSLA(m_ColorFeet).UnclampLighting().Pack(ms_DarkestLGT7);
80-
m_aUseCustomColors[0] = true;
81-
m_aUseCustomColors[1] = true;
82-
m_aUseCustomColors[2] = true;
83-
m_aUseCustomColors[3] = true;
84-
m_aUseCustomColors[4] = true;
85-
m_aSkinPartColors[0] = ColorBody;
86-
m_aSkinPartColors[1] = 0x22FFFFFF;
87-
m_aSkinPartColors[2] = ColorBody;
88-
m_aSkinPartColors[3] = ColorBody;
89-
m_aSkinPartColors[4] = ColorFeet;
79+
int ColorBody = ColorHSLA(m_ColorBody).UnclampLighting(ColorHSLA::DARKEST_LGT).Pack(ColorHSLA::DARKEST_LGT7);
80+
int ColorFeet = ColorHSLA(m_ColorFeet).UnclampLighting(ColorHSLA::DARKEST_LGT).Pack(ColorHSLA::DARKEST_LGT7);
81+
m_aUseCustomColors[protocol7::SKINPART_BODY] = true;
82+
m_aUseCustomColors[protocol7::SKINPART_MARKING] = true;
83+
m_aUseCustomColors[protocol7::SKINPART_DECORATION] = true;
84+
m_aUseCustomColors[protocol7::SKINPART_HANDS] = true;
85+
m_aUseCustomColors[protocol7::SKINPART_FEET] = true;
86+
m_aSkinPartColors[protocol7::SKINPART_BODY] = ColorBody;
87+
m_aSkinPartColors[protocol7::SKINPART_MARKING] = 0x22FFFFFF;
88+
m_aSkinPartColors[protocol7::SKINPART_DECORATION] = ColorBody;
89+
m_aSkinPartColors[protocol7::SKINPART_HANDS] = ColorBody;
90+
m_aSkinPartColors[protocol7::SKINPART_FEET] = ColorFeet;
9091
}
9192
}
9293

@@ -102,7 +103,7 @@ void CTeeInfo::FromSixup()
102103
for(auto &StdSkin : g_aStdSkins)
103104
{
104105
bool match = true;
105-
for(int p = 0; p < 6; p++)
106+
for(int p = 0; p < protocol7::NUM_SKINPARTS; p++)
106107
{
107108
if(str_comp(m_apSkinPartNames[p], StdSkin.m_apSkinPartNames[p]) || m_aUseCustomColors[p] != StdSkin.m_aUseCustomColors[p] || (m_aUseCustomColors[p] && m_aSkinPartColors[p] != StdSkin.m_aSkinPartColors[p]))
108109
{
@@ -118,24 +119,28 @@ void CTeeInfo::FromSixup()
118119
}
119120

120121
// find closest match
121-
int best_skin = 0;
122-
int best_matches = -1;
122+
int BestSkin = 0;
123+
int BestMatches = -1;
123124
for(int s = 0; s < 16; s++)
124125
{
125126
int matches = 0;
126127
for(int p = 0; p < 3; p++)
127128
if(str_comp(m_apSkinPartNames[p], g_aStdSkins[s].m_apSkinPartNames[p]) == 0)
128129
matches++;
129130

130-
if(matches > best_matches)
131+
if(matches > BestMatches)
131132
{
132-
best_matches = matches;
133-
best_skin = s;
133+
BestMatches = matches;
134+
BestSkin = s;
134135
}
135136
}
136137

137-
str_copy(m_aSkinName, g_aStdSkins[best_skin].m_aSkinName, sizeof(m_aSkinName));
138+
str_copy(m_aSkinName, g_aStdSkins[BestSkin].m_aSkinName, sizeof(m_aSkinName));
138139
m_UseCustomColor = true;
139-
m_ColorBody = ColorHSLA(m_aUseCustomColors[0] ? m_aSkinPartColors[0] : 255).UnclampLighting(ms_DarkestLGT7).Pack(ColorHSLA::DARKEST_LGT);
140-
m_ColorFeet = ColorHSLA(m_aUseCustomColors[4] ? m_aSkinPartColors[4] : 255).UnclampLighting(ms_DarkestLGT7).Pack(ColorHSLA::DARKEST_LGT);
140+
m_ColorBody = ColorHSLA(m_aUseCustomColors[protocol7::SKINPART_BODY] ? m_aSkinPartColors[protocol7::SKINPART_BODY] : 255)
141+
.UnclampLighting(ColorHSLA::DARKEST_LGT7)
142+
.Pack(ColorHSLA::DARKEST_LGT);
143+
m_ColorFeet = ColorHSLA(m_aUseCustomColors[protocol7::SKINPART_FEET] ? m_aSkinPartColors[protocol7::SKINPART_FEET] : 255)
144+
.UnclampLighting(ColorHSLA::DARKEST_LGT7)
145+
.Pack(ColorHSLA::DARKEST_LGT);
141146
}

src/game/server/teeinfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef GAME_SERVER_TEEINFO_H
22
#define GAME_SERVER_TEEINFO_H
33

4+
#include <engine/shared/protocol.h>
5+
46
class CTeeInfo
57
{
68
public:
7-
constexpr static const float ms_DarkestLGT7 = 61 / 255.0f;
8-
9-
char m_aSkinName[64] = {'\0'};
9+
char m_aSkinName[MAX_SKIN_LENGTH] = "";
1010
int m_UseCustomColor = 0;
1111
int m_ColorBody = 0;
1212
int m_ColorFeet = 0;

0 commit comments

Comments
 (0)