From 0f781df1dd8592728fa9f2c803b64519260ab0ee Mon Sep 17 00:00:00 2001 From: Learath Date: Thu, 25 Apr 2019 14:48:53 +0300 Subject: [PATCH] Try handling colors in console --- src/base/color.h | 5 ++ src/engine/console.h | 1 + src/engine/shared/config.cpp | 4 ++ src/engine/shared/config.h | 2 + src/engine/shared/console.cpp | 100 +++++++++++++++++++++++++++++++ src/engine/shared/console.h | 1 + src/game/server/teehistorian.cpp | 3 + src/game/variables.h | 4 +- src/test/teehistorian.cpp | 2 + 9 files changed, 120 insertions(+), 2 deletions(-) diff --git a/src/base/color.h b/src/base/color.h index 13f559801..4ca72a02f 100644 --- a/src/base/color.h +++ b/src/base/color.h @@ -200,6 +200,11 @@ inline vec3 UnpackColor(int v) return vec3(fmod(((v>>16)&0xff)/255.0f, 1.0f), ((v>>8)&0xff)/255.0f, 0.5f+(v&0xff)/255.0f*0.5f); } +inline int PackColor(vec3 hsl) +{ + return ((int)(hsl.h * 255.0f) << 16) + ((int)(hsl.s * 255.0f) << 8) + (int)(hsl.l * 255.0f); +} + inline vec4 Color3to4(vec3 col) { return vec4(col[0], col[1], col[2], 1.0f); diff --git a/src/engine/console.h b/src/engine/console.h index a078c25c0..7cec12a2b 100644 --- a/src/engine/console.h +++ b/src/engine/console.h @@ -45,6 +45,7 @@ public: virtual int GetInteger(unsigned Index) = 0; virtual float GetFloat(unsigned Index) = 0; virtual const char *GetString(unsigned Index) = 0; + virtual int GetColor(unsigned Index) = 0; int NumArguments() const { return m_NumArgs; } int m_ClientID; diff --git a/src/engine/shared/config.cpp b/src/engine/shared/config.cpp index 6c60f5ec0..193277d22 100644 --- a/src/engine/shared/config.cpp +++ b/src/engine/shared/config.cpp @@ -50,11 +50,13 @@ public: virtual void Reset() { #define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) g_Config.m_##Name = def; + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) #define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) str_copy(g_Config.m_##Name, def, len); #include "config_variables.h" #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR } @@ -74,11 +76,13 @@ public: char aEscapeBuf[1024*2]; #define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if((flags)&CFGFLAG_SAVE) { str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); } + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) #define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if((flags)&CFGFLAG_SAVE) { EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLine(aLineBuf); } #include "config_variables.h" #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR for(int i = 0; i < m_NumCallbacks; i++) diff --git a/src/engine/shared/config.h b/src/engine/shared/config.h index c63a81fb1..2c34a536e 100644 --- a/src/engine/shared/config.h +++ b/src/engine/shared/config.h @@ -12,9 +12,11 @@ struct CConfiguration { #define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) int m_##Name; + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) #define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Save,Desc) char m_##Name[Len]; // Flawfinder: ignore #include "config_variables.h" #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR }; diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 8f8fd4631..93b5ec887 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include @@ -35,6 +37,63 @@ float CConsole::CResult::GetFloat(unsigned Index) return str_tofloat(m_apArgs[Index]); } +int CConsole::CResult::GetColor(unsigned Index) +{ + if(Index >= m_NumArgs) + return -1; + + const char *pStr = m_apArgs[Index]; + if(str_isallnum(pStr)) // Teeworlds Color (Packed HSL) + { + return str_toint(pStr); + } + else if(*pStr == 'r') // Hex RGB + { + vec3 rgb; + int Len = str_length(pStr); + if(Len == 4) + { + unsigned Num = str_toint_base(pStr + 1, 16); + rgb.r = ((Num >> 8) & 0x0F + (Num >> 4) & 0xF0) / 255.0f; + rgb.g = ((Num >> 4) & 0x0F + (Num >> 0) & 0xF0) / 255.0f; + rgb.b = ((Num >> 0) & 0x0F + (Num << 4) & 0xF0) / 255.0f; + } + else if(Len == 7) + { + unsigned Num = str_toint_base(pStr + 1, 16); + rgb.r = ((Num >> 16) & 0xFF) / 255.0f; + rgb.g = ((Num >> 8) & 0xFF) / 255.0f; + rgb.b = ((Num >> 0) & 0xFF) / 255.0f; + } + else + { + return -1; + } + + return PackColor(RgbToHsl(rgb)); + } + else if(!str_comp_nocase(pStr, "red")) + return PackColor(vec3(0.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "yellow")) + return PackColor(vec3(1.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "green")) + return PackColor(vec3(2.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "cyan")) + return PackColor(vec3(3.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "blue")) + return PackColor(vec3(4.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "magenta")) + return PackColor(vec3(5.0f/6.0f, 1, .5f)); + else if(!str_comp_nocase(pStr, "white")) + return PackColor(vec3(0, 0, 1)); + else if(!str_comp_nocase(pStr, "gray")) + return PackColor(vec3(0, 0, .5f)); + else if(!str_comp_nocase(pStr, "black")) + return PackColor(vec3(0, 0, 0)); + + return -1; +} + const IConsole::CCommandInfo *CConsole::CCommand::NextCommandInfo(int AccessLevel, int FlagMask) const { const CCommand *pInfo = m_pNext; @@ -682,6 +741,37 @@ static void IntVariableCommand(IConsole::IResult *pResult, void *pUserData) } } +static void ColVariableCommand(IConsole::IResult *pResult, void *pUserData) +{ + CIntVariableData *pData = (CIntVariableData *)pUserData; + + if(pResult->NumArguments()) + { + int Val = pResult->GetColor(0); + + // do clamping + if(pData->m_Min != pData->m_Max) + { + if (Val < pData->m_Min) + Val = pData->m_Min; + if (pData->m_Max != 0 && Val > pData->m_Max) + Val = pData->m_Max; + } + + *(pData->m_pVariable) = Val; + if(pResult->m_ClientID != IConsole::CLIENT_ID_GAME) + pData->m_OldValue = Val; + } + else + { + char aBuf[32]; + str_format(aBuf, sizeof(aBuf), "Value: %d", *(pData->m_pVariable)); + pData->m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf); + } +} + + + static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData) { CStrVariableData *pData = (CStrVariableData *)pUserData; @@ -837,6 +927,12 @@ CConsole::CConsole(int FlagMask) Register(#ScriptName, "?i", Flags, IntVariableCommand, &Data, Desc); \ } + #define MACRO_CONFIG_COL(Name,ScriptName,Def,Min,Max,Flags,Desc) \ + { \ + static CIntVariableData Data = { this, &g_Config.m_##Name, Min, Max, Def }; \ + Register(#ScriptName, "?i", Flags, ColVariableCommand, &Data, Desc); \ + } + #define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \ { \ static char OldValue[Len] = Def; \ @@ -847,6 +943,7 @@ CConsole::CConsole(int FlagMask) #include "config_variables.h" #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR // DDRace @@ -1110,6 +1207,8 @@ void CConsole::ResetServerGameSettings() } \ } + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) + #define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \ { \ if(((Flags) & (CFGFLAG_SERVER|CFGFLAG_GAME)) == (CFGFLAG_SERVER|CFGFLAG_GAME)) \ @@ -1131,6 +1230,7 @@ void CConsole::ResetServerGameSettings() #include "config_variables.h" #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR } diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 119013704..c06365944 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -119,6 +119,7 @@ class CConsole : public IConsole virtual const char *GetString(unsigned Index); virtual int GetInteger(unsigned Index); virtual float GetFloat(unsigned Index); + virtual int GetColor(unsigned Index); // DDRace diff --git a/src/game/server/teehistorian.cpp b/src/game/server/teehistorian.cpp index b5d76e20b..7f036715c 100644 --- a/src/game/server/teehistorian.cpp +++ b/src/game/server/teehistorian.cpp @@ -115,6 +115,8 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo) First = false; \ } + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) + #define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \ if((Flags)&CFGFLAG_SERVER && !((Flags)&CFGFLAG_NONTEEHISTORIC) && str_comp(pGameInfo->m_pConfig->m_##Name, (Def)) != 0) \ { \ @@ -129,6 +131,7 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo) #include #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR str_format(aJson, sizeof(aJson), "},\"tuning\":{"); diff --git a/src/game/variables.h b/src/game/variables.h index f6a1fd8a7..a1b60f213 100644 --- a/src/game/variables.h +++ b/src/game/variables.h @@ -77,8 +77,8 @@ MACRO_CONFIG_INT(ClAutoStatboardScreenshotMax, cl_auto_statboard_screenshot_max, MACRO_CONFIG_INT(ClDefaultZoom, cl_default_zoom, 10, 0, 20, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Default zoom level (10 default, min 0, max 20)") MACRO_CONFIG_INT(ClPlayerUseCustomColor, player_use_custom_color, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Toggles usage of custom colors") -MACRO_CONFIG_INT(ClPlayerColorBody, player_color_body, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player body color") -MACRO_CONFIG_INT(ClPlayerColorFeet, player_color_feet, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player feet color") +MACRO_CONFIG_COL(ClPlayerColorBody, player_color_body, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player body color") +MACRO_CONFIG_COL(ClPlayerColorFeet, player_color_feet, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player feet color") MACRO_CONFIG_STR(ClPlayerSkin, player_skin, 24, "default", CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player skin") MACRO_CONFIG_STR(ClSkinPrefix, cl_skin_prefix, 100, "", CFGFLAG_CLIENT|CFGFLAG_SAVE, "Replace the skins by skins with this prefix (e.g. kitty, coala, santa)") MACRO_CONFIG_INT(ClFatSkins, cl_fat_skins, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Enable fat skins") diff --git a/src/test/teehistorian.cpp b/src/test/teehistorian.cpp index 8079eddaa..df3d821b7 100644 --- a/src/test/teehistorian.cpp +++ b/src/test/teehistorian.cpp @@ -33,10 +33,12 @@ protected: mem_zero(&m_Config, sizeof(m_Config)); #define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) \ m_Config.m_##Name = (Def); + #define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__) #define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Save,Desc) \ str_copy(m_Config.m_##Name, (Def), sizeof(m_Config.m_##Name)); #include #undef MACRO_CONFIG_STR + #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_INT RegisterUuids(&m_UuidManager);