From 94e4c2f9887a6beea19a0c8d7233ed3e5aa62558 Mon Sep 17 00:00:00 2001 From: -_YoYo178_- <95132723+YoYo178@users.noreply.github.com> Date: Sat, 9 Mar 2024 17:45:11 +0530 Subject: [PATCH] new lua functions and mp3 player enabled The mp3 player was disabled during the linux port by azzy, it has now been re-enabled. Added 3 new LUA functions: `AddColoredHudText` `AddColoredHudTextToTeam` `AddColoredHudTextToAll` These functions do exactly what their non-colored counterparts do (`AddHudText`, etc), but with color support. --- mp/src/game/client/ff/hud/ff_hud_lua.cpp | 136 +++++++++++ mp/src/game/client/ff/hud/ff_hud_lua.h | 1 + mp/src/game/client/mp3player.cpp | 7 +- .../game/server/ff/lua/ff_lualib_globals.cpp | 220 +++++++++++++++++- mp/src/game/shared/ff/ff_utils.cpp | 36 +++ mp/src/game/shared/ff/ff_utils.h | 2 + 6 files changed, 399 insertions(+), 3 deletions(-) diff --git a/mp/src/game/client/ff/hud/ff_hud_lua.cpp b/mp/src/game/client/ff/hud/ff_hud_lua.cpp index fcfda611..f19f8253 100644 --- a/mp/src/game/client/ff/hud/ff_hud_lua.cpp +++ b/mp/src/game/client/ff/hud/ff_hud_lua.cpp @@ -179,6 +179,28 @@ void CHudLua::MsgFunc_FF_HudLua(bf_read &msg) break; } + case HUD_TEXT_COLOR: + { + int xPos = msg.ReadShort(); + int yPos = msg.ReadShort(); + + char szText[256]; + if (!msg.ReadString(szText, 255)) + return; + + int r = msg.ReadShort(); + int g = msg.ReadShort(); + int b = msg.ReadShort(); + int a = msg.ReadShort(); + + int iAlignX = msg.ReadShort(); + int iAlignY = msg.ReadShort(); + int iSize = msg.ReadShort(); + + HudTextColored(hudIdentifier, xPos, yPos, szText, iAlignX, iAlignY, iSize, Color(r, g, b, a)); + + break; + } case HUD_TIMER: { int xPos = msg.ReadShort(); @@ -481,6 +503,103 @@ void CHudLua::HudText(int hudIdentifier, int iX, int iY, const char *pszText, in pLabel->SetVisible(true); } +//----------------------------------------------------------------------------- +// Purpose: Create a new text box on the hud - x & y alignment +//----------------------------------------------------------------------------- +void CHudLua::HudTextColored(int hudIdentifier, int iX, int iY, const char* pszText, int iAlignX, int iAlignY, int iSize, Color clr) +{ + // Create or find the correct hud element + Label* pLabel = dynamic_cast (GetHudElement(hudIdentifier, HUD_TEXT_COLOR)); + + if (!pLabel) + return; + + // set to item-specific defaults if values weren't set or are invalid + iAlignX = (iAlignX < 0) ? 0 : iAlignX; + iAlignY = (iAlignY < 0) ? 0 : iAlignY; + + char szTranslatedText[1024]; + + // Now set this label up + if (TranslateKeyCommand(pszText, szTranslatedText, sizeof(szTranslatedText))) + pLabel->SetText(szTranslatedText); + else + pLabel->SetText(pszText); + + if (iSize >= 1 && iSize <= 5) + { + IScheme* pScheme = scheme()->GetIScheme(pLabel->GetScheme()); + if (pScheme) + { + HFont font = pScheme->GetFont(VarArgs("LuaText%d", iSize), pLabel->IsProportional()); + if (font != INVALID_FONT) + pLabel->SetFont(font); + } + } + + pLabel->SetFgColor(clr); + + pLabel->SizeToContents(); + + int iProperXPosition = 0; + int iProperYPosition = 0; + int scaledX = scheme()->GetProportionalScaledValue(iX); + int scaledY = scheme()->GetProportionalScaledValue(iY); + int scaledW = 0; // surface()->GetCharacterWidth(pTimer->GetFont(), '0' ) * 5; + int scaledH = 0; // surface()->GetFontTall( pTimer->GetFont() ); + pLabel->GetContentSize(scaledW, scaledH); + + switch (iAlignX) + { + case 1: //HUD_ALIGNX_RIGHT : + iProperXPosition = (ScreenWidth() - scaledX) - scaledW; + break; + case 2: //HUD_ALIGNX_CENTERLEFT : + iProperXPosition = ((ScreenWidth() / 2) - scaledX) - scaledW; + break; + case 3: //HUD_ALIGNX_CENTERRIGHT : + iProperXPosition = (ScreenWidth() / 2) + scaledX; + break; + case 4: //HUD_ALIGNX_CENTER : + iProperXPosition = (ScreenWidth() / 2) - (scaledW / 2) + scaledX; + break; + case 5: //HUD_ALIGNX_RIGHT_STRINGSTART : + iProperXPosition = ScreenWidth() - scaledX; + break; + case 6: //HUD_ALIGNX_LEFT_STRINGEND : + iProperXPosition = scaledX - scaledW; + break; + case 0: //HUD_ALIGNX_LEFT : + default: + iProperXPosition = scaledX; + break; + } + switch (iAlignY) + { + case 1: //HUD_ALIGNY_BOTTOM : + iProperYPosition = (ScreenHeight() - scaledY) - scaledH; + break; + case 2: //HUD_ALIGNY_CENTERUP : + iProperYPosition = ((ScreenHeight() / 2) - scaledY) - scaledH; + break; + case 3: //HUD_ALIGNY_CENTERDOWN : + iProperYPosition = (ScreenHeight() / 2) + scaledY; + break; + case 4: //HUD_ALIGNY_CENTER : + iProperYPosition = (ScreenHeight() / 2) - (scaledH / 2) + scaledY; + break; + case 0: //HUD_ALIGNY_TOP : + default: + iProperYPosition = scaledY; + break; + } + + pLabel->SetPos(iProperXPosition, iProperYPosition); + + //pLabel->MoveToFront(); + pLabel->SetVisible(true); +} + //------------------------------------------------------------------------------- // Purpose: Translate key commands into user's current key bind, and add the // hint text to the text box. @@ -714,6 +833,23 @@ Panel *CHudLua::GetHudElement(int hudIdentifier, HudElementType_t iType) } } break; + case HUD_TEXT_COLOR: + { + pPanel = new Label(this, szPanelName, ""); + + Label* pLabel = dynamic_cast(pPanel); + if (pLabel) + { + IScheme* pScheme = scheme()->GetIScheme(pLabel->GetScheme()); + if (pScheme) + { + HFont font = pScheme->GetFont("LuaText_Default", pLabel->IsProportional()); + if (font != INVALID_FONT) + pLabel->SetFont(font); + } + } + } + break; case HUD_TIMER: { diff --git a/mp/src/game/client/ff/hud/ff_hud_lua.h b/mp/src/game/client/ff/hud/ff_hud_lua.h index 5c5cfd68..c1a085eb 100644 --- a/mp/src/game/client/ff/hud/ff_hud_lua.h +++ b/mp/src/game/client/ff/hud/ff_hud_lua.h @@ -62,6 +62,7 @@ class CHudLua : public CHudElement, public vgui::Panel void HudIcon(int hudIdentifier, int iX, int iY, const char *pszSource, int iWidth, int iHeight, int iAlignX, int iAlignY); void HudBox(int hudIdentifier, int iX, int iY, int iWidth, int iHeight, Color clr, Color clrBorder, int iBorderWidth, int iAlignX, int iAlignY); void HudText(int hudIdentifier, int iX, int iY, const char *pszText, int iAlignX, int iAlignY, int iSize); + void HudTextColored(int hudIdentifier, int iX, int iY, const char *pszText, int iAlignX, int iAlignY, int iSize, Color clr = Color(200, 220, 255, 255)); bool TranslateKeyCommand( const char *szMessage, char *szTranslated, int iBufferSizeInBytes ); diff --git a/mp/src/game/client/mp3player.cpp b/mp/src/game/client/mp3player.cpp index 8d047102..45de46fd 100644 --- a/mp/src/game/client/mp3player.cpp +++ b/mp/src/game/client/mp3player.cpp @@ -6,10 +6,11 @@ #include "cbase.h" -#if 0 +#if 1 #include "mp3player.h" #include "KeyValues.h" #include "filesystem.h" +#include #include "vgui_controls/MenuButton.h" #include "vgui_controls/Menu.h" @@ -1368,7 +1369,9 @@ void CMP3Player::GetLocalCopyOfSong( const MP3File_t &mp3, char *outsong, size_t // !!!HACK HACK: // Total hack right now, using windows OS calls to copy file to full destination - int success = ::CopyFileA( sourcepath, destpath, TRUE ); + // not anymore because + // we now have std::filesystem::copy_file which we can use (since C++17) + int success = std::filesystem::copy_file( sourcepath, destpath/*, TRUE*/ ); if ( success > 0 ) { Q_snprintf( outsong, outlen, "_mp3/%s.mp3", hexname ); diff --git a/mp/src/game/server/ff/lua/ff_lualib_globals.cpp b/mp/src/game/server/ff/lua/ff_lualib_globals.cpp index 78c4602e..96bf1245 100644 --- a/mp/src/game/server/ff/lua/ff_lualib_globals.cpp +++ b/mp/src/game/server/ff/lua/ff_lualib_globals.cpp @@ -2247,6 +2247,178 @@ namespace FFLib } } } + + void AddColoredHudText(CFFPlayer* pPlayer, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y) + { + if (!pPlayer || !pszIdentifier || !pszText) + return; + + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText); + } + + void AddColoredHudText(CFFPlayer* pPlayer, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, int iAlign) + { + if (!pPlayer || !pszIdentifier || !pszText) + return; + + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, iAlign); + } + + void AddColoredHudText(CFFPlayer* pPlayer, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY) + { + if (!pPlayer || !pszIdentifier || !pszText) + return; + + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY); + } + + void AddColoredHudText(CFFPlayer* pPlayer, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY, int iSize) + { + if (!pPlayer || !pszIdentifier || !pszText) + return; + + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY, iSize); + } + + void AddColoredHudTextToTeam(CFFTeam* pTeam, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + if (pPlayer->GetTeam()->GetTeamNumber() == pTeam->GetTeamNumber()) + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText); + } + } + } + + void AddColoredHudTextToTeam(CFFTeam* pTeam, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, int iAlign) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + if (pPlayer->GetTeam()->GetTeamNumber() == pTeam->GetTeamNumber()) + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, iAlign); + } + } + } + + void AddColoredHudTextToTeam(CFFTeam* pTeam, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + if (pPlayer->GetTeam()->GetTeamNumber() == pTeam->GetTeamNumber()) + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY); + } + } + } + + void AddColoredHudTextToTeam(CFFTeam* pTeam, const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY, int iSize) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + if (pPlayer->GetTeam()->GetTeamNumber() == pTeam->GetTeamNumber()) + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY, iSize); + } + } + } + + void AddColoredHudTextToAll(const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText); + } + } + } + + void AddColoredHudTextToAll(const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, int iAlign) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, iAlign); + } + } + } + + void AddColoredHudTextToAll(const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY); + } + } + } + + void AddColoredHudTextToAll(const char* pszIdentifier, const char* pszText, const char* pszColor, float x, float y, float flAlignX, float flAlignY, int iSize) + { + if (!pszIdentifier || !pszText) + return; + + // loop through each player + for (int i = 1; i <= gpGlobals->maxClients; i++) + { + CBasePlayer* ent = UTIL_PlayerByIndex(i); + if (ent && ent->IsPlayer()) + { + CFFPlayer* pPlayer = ToFFPlayer(ent); + FF_LuaHudTextColored(pPlayer, pszIdentifier, pszColor, x, y, pszText, flAlignX, flAlignY, iSize); + } + } + } void AddTimer( const char *pszIdentifier, float flStartValue, float flSpeed ) { @@ -3000,6 +3172,30 @@ namespace FFLib { return CBaseEntity::PrecacheModel(name, true); } + + void HudMsg(int iChannel, float x, float y, int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2, int iEffect, float flFadeIn, float flFadeOut, float flHoldTime, float flFXTime, const char* pszMessage) + { + CBroadcastRecipientFilter filter; + UserMessageBegin(filter, "HudMsg"); + WRITE_BYTE(iChannel); + WRITE_FLOAT(x); + WRITE_FLOAT(y); + WRITE_BYTE(r1); + WRITE_BYTE(g1); + WRITE_BYTE(b1); + WRITE_BYTE(a1); + WRITE_BYTE(r2); + WRITE_BYTE(g2); + WRITE_BYTE(b2); + WRITE_BYTE(a2); + WRITE_BYTE(iEffect); + WRITE_FLOAT(flFadeIn); + WRITE_FLOAT(flFadeOut); + WRITE_FLOAT(flHoldTime); + WRITE_FLOAT(flFXTime); + WRITE_STRING(pszMessage); + MessageEnd(); + } } // namespace FFLib //--------------------------------------------------------------------------- @@ -3121,6 +3317,27 @@ void CFFLuaLib::InitGlobals(lua_State* L) overload(&FFLib::AddHudTextToAll) ) + .addFunction("AddColoredHudText", + overload(&FFLib::AddColoredHudText), + overload(&FFLib::AddColoredHudText), + overload(&FFLib::AddColoredHudText), + overload(&FFLib::AddColoredHudText) + ) + + .addFunction("AddColoredHudTextToTeam", + overload(&FFLib::AddColoredHudTextToTeam), + overload(&FFLib::AddColoredHudTextToTeam), + overload(&FFLib::AddColoredHudTextToTeam), + overload(&FFLib::AddColoredHudTextToTeam) + ) + + .addFunction("AddColoredHudTextToAll", + overload(&FFLib::AddColoredHudTextToAll), + overload(&FFLib::AddColoredHudTextToAll), + overload(&FFLib::AddColoredHudTextToAll), + overload(&FFLib::AddColoredHudTextToAll) + ) + .addFunction("AddTimer", &FFLib::AddTimer) .addFunction("RemoveTimer", &FFLib::RemoveTimer) .addFunction("GetTimerTime", &FFLib::GetTimerTime) @@ -3365,5 +3582,6 @@ void CFFLuaLib::InitGlobals(lua_State* L) overload(&FFLib::SpawnEntity) ) - .addFunction("GetMapName", &FFLib::GetMapName); + .addFunction("GetMapName", &FFLib::GetMapName) + .addFunction("HudMsg", &FFLib::HudMsg); } diff --git a/mp/src/game/shared/ff/ff_utils.cpp b/mp/src/game/shared/ff/ff_utils.cpp index d01754c2..4b41bb17 100644 --- a/mp/src/game/shared/ff/ff_utils.cpp +++ b/mp/src/game/shared/ff/ff_utils.cpp @@ -782,6 +782,42 @@ void FF_LuaHudText(CFFPlayer *pPlayer, const char *pszIdentifier, int x, int y, MessageEnd(); } +//----------------------------------------------------------------------------- +// Purpose: Set some colored text on the hud +//----------------------------------------------------------------------------- +void FF_LuaHudTextColored(CFFPlayer* pPlayer, const char* pszIdentifier, const char* pszColor, int x, int y, const char* pszText, int iAlignX/* = -1*/, int iAlignY/* = -1*/, int iSize/* = -1*/) +{ + if (!pPlayer) + return; + + int r, g, b, a; + if ( sscanf( pszColor, "%i %i %i %i", &r, &g, &b, &a ) != 4 ) + r = g = b = a = 255; + + r = clamp(r, 0, 255); + g = clamp(g, 0, 255); + b = clamp(b, 0, 255); + a = clamp(a, 0, 255); + + CSingleUserRecipientFilter user(pPlayer); + user.MakeReliable(); + + UserMessageBegin(user, "FF_HudLua"); + WRITE_BYTE(HUD_TEXT_COLOR); + WRITE_SHORT(_scriptman.GetOrAddHudElementIndex(pszIdentifier)); + WRITE_SHORT(x); + WRITE_SHORT(y); + WRITE_STRING(pszText); + WRITE_SHORT(r); + WRITE_SHORT(g); + WRITE_SHORT(b); + WRITE_SHORT(a); + WRITE_SHORT(iAlignX); + WRITE_SHORT(iAlignY); + WRITE_SHORT(iSize); + MessageEnd(); +} + //----------------------------------------------------------------------------- // Purpose: Set a timer on the hud //----------------------------------------------------------------------------- diff --git a/mp/src/game/shared/ff/ff_utils.h b/mp/src/game/shared/ff/ff_utils.h index 2e1c2e32..50e9b31e 100644 --- a/mp/src/game/shared/ff/ff_utils.h +++ b/mp/src/game/shared/ff/ff_utils.h @@ -66,6 +66,7 @@ enum HudElementType_t HUD_ICON = 0, HUD_BOX, HUD_TEXT, + HUD_TEXT_COLOR, HUD_TIMER, HUD_REMOVE, }; @@ -115,6 +116,7 @@ void FF_DecalTrace( CBaseEntity *pEntity, float flRadius, const char *pszDecalNa void FF_LuaHudIcon(CFFPlayer *pPlayer, const char *pszIdentifier, int x, int y, const char *pszImage, int iWidth, int iHeight, int iAlignX = -1, int iAlignY = -1); void FF_LuaHudBox(CFFPlayer *pPlayer, const char *pszIdentifier, int x, int y, int iWidth, int iHeight, Color clr, Color clrBorder, int iBorderWidth, int iAlignX = -1, int iAlignY = -1); void FF_LuaHudText(CFFPlayer *pPlayer, const char *pszIdentifier, int x, int y, const char *pszText, int iAlignX = -1, int iAlignY = -1, int iSize = -1); +void FF_LuaHudTextColored(CFFPlayer *pPlayer, const char *pszIdentifier, const char *pszColor, int x, int y, const char *pszText, int iAlignX = -1, int iAlignY = -1, int iSize = -1); void FF_LuaHudTimer(CFFPlayer *pPlayer, const char *pszIdentifier, int x, int y, float flStartValue, float flSpeed, int iAlignX = -1, int iAlignY = -1, int iSize = -1); void FF_LuaHudRemove(CFFPlayer *pPlayer, const char *pszIdentifier);