Skip to content

Commit

Permalink
Merge pull request ddnet#8987 from Robyt3/Client-Skins-Randomize-Func…
Browse files Browse the repository at this point in the history
…tion

Move `CMenus::RandomSkin` function to `CSkins::RandomizeSkin`
  • Loading branch information
def- authored Sep 18, 2024
2 parents d2348b2 + 96cc8c5 commit 5407db0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/game/client/components/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class CMenus : public CComponent
void DoJoystickBar(const CUIRect *pRect, float Current, float Tolerance, bool Active);

bool m_SkinListNeedsUpdate = false;
void RandomSkin();

// menus_settings_assets.cpp
public:
Expand Down
47 changes: 2 additions & 45 deletions src/game/client/components/menus_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,50 +406,6 @@ void CMenus::OnRefreshSkins()
m_SkinListNeedsUpdate = true;
}

void CMenus::RandomSkin()
{
static const float s_aSchemes[] = {1.0f / 2.0f, 1.0f / 3.0f, 1.0f / -3.0f, 1.0f / 12.0f, 1.0f / -12.0f}; // complementary, triadic, analogous
const bool UseCustomColor = !m_Dummy ? g_Config.m_ClPlayerUseCustomColor : g_Config.m_ClDummyUseCustomColor;
if(UseCustomColor)
{
float GoalSat = random_float(0.3f, 1.0f);
float MaxBodyLht = 1.0f - GoalSat * GoalSat; // max allowed lightness before we start losing saturation

ColorHSLA Body;
Body.h = random_float();
Body.l = random_float(0.0f, MaxBodyLht);
Body.s = clamp(GoalSat * GoalSat / (1.0f - Body.l), 0.0f, 1.0f);

ColorHSLA Feet;
Feet.h = std::fmod(Body.h + s_aSchemes[rand() % std::size(s_aSchemes)], 1.0f);
Feet.l = random_float();
Feet.s = clamp(GoalSat * GoalSat / (1.0f - Feet.l), 0.0f, 1.0f);

unsigned *pColorBody = !m_Dummy ? &g_Config.m_ClPlayerColorBody : &g_Config.m_ClDummyColorBody;
unsigned *pColorFeet = !m_Dummy ? &g_Config.m_ClPlayerColorFeet : &g_Config.m_ClDummyColorFeet;

*pColorBody = Body.Pack(false);
*pColorFeet = Feet.Pack(false);
}

const size_t SkinNameSize = !m_Dummy ? sizeof(g_Config.m_ClPlayerSkin) : sizeof(g_Config.m_ClDummySkin);
char aRandomSkinName[24];
str_copy(aRandomSkinName, "default", SkinNameSize);
if(!m_pClient->m_Skins.GetSkinsUnsafe().empty())
{
do
{
auto it = m_pClient->m_Skins.GetSkinsUnsafe().begin();
std::advance(it, rand() % m_pClient->m_Skins.GetSkinsUnsafe().size());
str_copy(aRandomSkinName, (*it).second->GetName(), SkinNameSize);
} while(!str_comp(aRandomSkinName, "x_ninja") || !str_comp(aRandomSkinName, "x_spec"));
}
char *pSkinName = !m_Dummy ? g_Config.m_ClPlayerSkin : g_Config.m_ClDummySkin;
str_copy(pSkinName, aRandomSkinName, SkinNameSize);

SetNeedSendInfo();
}

void CMenus::Con_AddFavoriteSkin(IConsole::IResult *pResult, void *pUserData)
{
auto *pSelf = (CMenus *)pUserData;
Expand Down Expand Up @@ -675,7 +631,8 @@ void CMenus::RenderSettingsTee(CUIRect MainView)
TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
if(DoButton_Menu(&s_RandomSkinButton, s_apDice[s_CurrentDie], 0, &RandomSkinButton, nullptr, IGraphics::CORNER_ALL, 5.0f, -0.2f))
{
RandomSkin();
GameClient()->m_Skins.RandomizeSkin(m_Dummy);
SetNeedSendInfo();
s_CurrentDie = rand() % std::size(s_apDice);
}
TextRender()->SetRenderFlags(0);
Expand Down
42 changes: 42 additions & 0 deletions src/game/client/components/skins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,45 @@ const CSkin *CSkins::FindImpl(const char *pName)

return nullptr;
}

void CSkins::RandomizeSkin(int Dummy)
{
static const float s_aSchemes[] = {1.0f / 2.0f, 1.0f / 3.0f, 1.0f / -3.0f, 1.0f / 12.0f, 1.0f / -12.0f}; // complementary, triadic, analogous
const bool UseCustomColor = Dummy ? g_Config.m_ClDummyUseCustomColor : g_Config.m_ClPlayerUseCustomColor;
if(UseCustomColor)
{
float GoalSat = random_float(0.3f, 1.0f);
float MaxBodyLht = 1.0f - GoalSat * GoalSat; // max allowed lightness before we start losing saturation

ColorHSLA Body;
Body.h = random_float();
Body.l = random_float(0.0f, MaxBodyLht);
Body.s = clamp(GoalSat * GoalSat / (1.0f - Body.l), 0.0f, 1.0f);

ColorHSLA Feet;
Feet.h = std::fmod(Body.h + s_aSchemes[rand() % std::size(s_aSchemes)], 1.0f);
Feet.l = random_float();
Feet.s = clamp(GoalSat * GoalSat / (1.0f - Feet.l), 0.0f, 1.0f);

unsigned *pColorBody = Dummy ? &g_Config.m_ClDummyColorBody : &g_Config.m_ClPlayerColorBody;
unsigned *pColorFeet = Dummy ? &g_Config.m_ClDummyColorFeet : &g_Config.m_ClPlayerColorFeet;

*pColorBody = Body.Pack(false);
*pColorFeet = Feet.Pack(false);
}

const size_t SkinNameSize = Dummy ? sizeof(g_Config.m_ClDummySkin) : sizeof(g_Config.m_ClPlayerSkin);
char aRandomSkinName[24];
str_copy(aRandomSkinName, "default", SkinNameSize);
if(!m_pClient->m_Skins.GetSkinsUnsafe().empty())
{
do
{
auto it = m_pClient->m_Skins.GetSkinsUnsafe().begin();
std::advance(it, rand() % m_pClient->m_Skins.GetSkinsUnsafe().size());
str_copy(aRandomSkinName, (*it).second->GetName(), SkinNameSize);
} while(!str_comp(aRandomSkinName, "x_ninja") || !str_comp(aRandomSkinName, "x_spec"));
}
char *pSkinName = Dummy ? g_Config.m_ClDummySkin : g_Config.m_ClPlayerSkin;
str_copy(pSkinName, aRandomSkinName, SkinNameSize);
}
1 change: 1 addition & 0 deletions src/game/client/components/skins.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CSkins : public CComponent
std::unordered_map<std::string_view, std::unique_ptr<CSkin>> &GetSkinsUnsafe() { return m_Skins; }
const CSkin *FindOrNullptr(const char *pName, bool IgnorePrefix = false);
const CSkin *Find(const char *pName);
void RandomizeSkin(int Dummy);

bool IsDownloadingSkins() { return m_DownloadingSkins; }

Expand Down

0 comments on commit 5407db0

Please sign in to comment.