Skip to content

Commit

Permalink
Make crosshair color separately configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVanheer committed Aug 31, 2023
1 parent 6c23fdc commit 70f1ae9
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/game/client/ui/hud/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ void CHudAmmo::DrawCrosshair(int x, int y)
}
};

renderer(x, y, m_Crosshair, gHUD.m_HudItemColor);
renderer(x, y, m_Crosshair, gHUD.m_CrosshairColor);
renderer(x, y, m_AutoaimCrosshair, RGB_REDISH);
}

Expand Down
9 changes: 9 additions & 0 deletions src/game/client/ui/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void __CmdFunc_OpenCommandMenu()
void CHud::Init()
{
g_ClientUserMessages.RegisterHandler("HudColor", &CHud::MsgFunc_HudColor, this);
g_ClientUserMessages.RegisterHandler("CrsshrClr", &CHud::MsgFunc_CrosshairColor, this);
g_ClientUserMessages.RegisterHandler("Logo", &CHud::MsgFunc_Logo, this);
g_ClientUserMessages.RegisterHandler("ResetHUD", &CHud::MsgFunc_ResetHUD, this);
g_ClientUserMessages.RegisterHandler("GameMode", &CHud::MsgFunc_GameMode, this);
Expand Down Expand Up @@ -222,6 +223,7 @@ void CHud::VidInit()
// Reset to default on new map load
m_HudColor = RGB_HUD_COLOR;
m_HudItemColor = RGB_HUD_COLOR;
m_CrosshairColor = RGB_CROSSHAIR_COLOR;

for (auto hudElement : m_HudList)
{
Expand All @@ -242,6 +244,13 @@ void CHud::MsgFunc_HudColor(const char* pszName, BufferReader& reader)
}
}

void CHud::MsgFunc_CrosshairColor(const char* pszName, BufferReader& reader)
{
m_CrosshairColor.Red = reader.ReadByte();
m_CrosshairColor.Green = reader.ReadByte();
m_CrosshairColor.Blue = reader.ReadByte();
}

void CHud::MsgFunc_Logo(const char* pszName, BufferReader& reader)
{
// update Train data
Expand Down
3 changes: 3 additions & 0 deletions src/game/client/ui/hud/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ class CHud
*/
RGB24 m_HudItemColor = RGB_HUD_COLOR;

RGB24 m_CrosshairColor = RGB_CROSSHAIR_COLOR;

int m_iFontHeight;
int DrawHudNumber(int x, int y, int iFlags, int iNumber, const RGB24& color);
int DrawHudString(int x, int y, int iMaxX, const char* szString, const RGB24& color);
Expand Down Expand Up @@ -831,6 +833,7 @@ class CHud
void MsgFunc_Damage(const char* pszName, BufferReader& reader);
void MsgFunc_GameMode(const char* pszName, BufferReader& reader);
void MsgFunc_HudColor(const char* pszName, BufferReader& reader);
void MsgFunc_CrosshairColor(const char* pszName, BufferReader& reader);
void MsgFunc_Logo(const char* pszName, BufferReader& reader);
void MsgFunc_ResetHUD(const char* pszName, BufferReader& reader);
void MsgFunc_InitHUD(const char* pszName, BufferReader& reader);
Expand Down
1 change: 1 addition & 0 deletions src/game/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ target_sources(server PRIVATE
config/GameConfig.h
config/sections/BaseFileNamesListSection.h
config/sections/CommandsSection.h
config/sections/CrosshairColorSection.h
config/sections/EchoSection.h
config/sections/EntityClassificationsSection.h
config/sections/EntityTemplatesSection.h
Expand Down
1 change: 1 addition & 0 deletions src/game/server/MapState.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MapState final
MapState& operator=(MapState&&) = default;

std::optional<RGB24> m_HudColor;
std::optional<RGB24> m_CrosshairColor;
std::optional<SuitLightType> m_LightType;

// Initialized during config load.
Expand Down
8 changes: 8 additions & 0 deletions src/game/server/ServerLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "config/ConditionEvaluator.h"
#include "config/GameConfig.h"
#include "config/sections/CommandsSection.h"
#include "config/sections/CrosshairColorSection.h"
#include "config/sections/EchoSection.h"
#include "config/sections/EntityClassificationsSection.h"
#include "config/sections/EntityTemplatesSection.h"
Expand Down Expand Up @@ -362,6 +363,12 @@ void ServerLibrary::PlayerActivating(CBasePlayer* player)
player->SetHudColor(*m_MapState->m_HudColor);
}

// Override the crosshair color.
if (m_MapState->m_CrosshairColor)
{
player->SetCrosshairColor(*m_MapState->m_CrosshairColor);
}

// Override the light type.
if (m_MapState->m_LightType)
{
Expand Down Expand Up @@ -422,6 +429,7 @@ void ServerLibrary::CreateConfigDefinitions()

sections.push_back(std::make_unique<EchoSection<ServerConfigContext>>());
sections.push_back(std::make_unique<CommandsSection<ServerConfigContext>>(&g_CommandWhitelist));
sections.push_back(std::make_unique<CrosshairColorSection>());
sections.push_back(std::make_unique<SentencesSection>());
sections.push_back(std::make_unique<MaterialsSection>());
sections.push_back(std::make_unique<SkillSection>());
Expand Down
1 change: 1 addition & 0 deletions src/game/server/UserMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void LinkUserMessages()
gmsgStatusIcon = g_engfuncs.pfnRegUserMsg("StatusIcon", -1);
gmsgPlayerBrowse = g_engfuncs.pfnRegUserMsg("PlyrBrowse", -1);
gmsgHudColor = g_engfuncs.pfnRegUserMsg("HudColor", 3);
gmsgCrosshairColor = g_engfuncs.pfnRegUserMsg("CrsshrClr", 3);
gmsgFlagIcon = g_engfuncs.pfnRegUserMsg("FlagIcon", -1);
gmsgFlagTimer = g_engfuncs.pfnRegUserMsg("FlagTimer", -1);
gmsgPlayerIcon = g_engfuncs.pfnRegUserMsg("PlayerIcon", -1);
Expand Down
1 change: 1 addition & 0 deletions src/game/server/UserMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ inline int gmsgSpectator = 0;
inline int gmsgStatusIcon = 0;
inline int gmsgPlayerBrowse = 0;
inline int gmsgHudColor = 0;
inline int gmsgCrosshairColor = 0;
inline int gmsgFlagIcon = 0;
inline int gmsgFlagTimer = 0;
inline int gmsgPlayerIcon = 0;
Expand Down
1 change: 1 addition & 0 deletions src/game/server/cdll_dll.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ constexpr RGB24 RGB_GREENISH{0, 160, 0};
constexpr RGB24 RGB_BLUEISH{95, 95, 255};

constexpr RGB24 RGB_HUD_COLOR{RGB_YELLOWISH};
constexpr RGB24 RGB_CROSSHAIR_COLOR{RGB_YELLOWISH};

/**
* @brief Amount of time, in seconds, between entity info update checks.
Expand Down
17 changes: 17 additions & 0 deletions src/game/server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,23 @@ void SV_CreateClientCommands()
} },
{.Flags = ClientCommandFlag::Cheat});

g_ClientCommands.Create("set_crosshair_color", [](CBasePlayer* player, const auto& args)
{
if (args.Count() >= 4)
{
Vector color{255, 255, 255};
UTIL_StringToVector(color, CMD_ARGS());

player->SetCrosshairColor({static_cast<std::uint8_t>(color.x),
static_cast<std::uint8_t>(color.y),
static_cast<std::uint8_t>(color.z)});
}
else
{
UTIL_ConsolePrint(player, "Usage: set_crosshair_color <r> <g> <b> (values in range 0-255)\n");
} },
{.Flags = ClientCommandFlag::Cheat});

g_ClientCommands.Create("set_suit_light_type", [](CBasePlayer* player, const auto& args)
{
if (args.Count() > 1)
Expand Down
61 changes: 61 additions & 0 deletions src/game/server/config/sections/CrosshairColorSection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/***
*
* Copyright (c) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/

#pragma once

#include "palette.h"
#include "ServerConfigContext.h"

#include "config/GameConfig.h"

#include "utils/JSONSystem.h"
#include "utils/shared_utils.h"

/**
* @brief Allows a configuration file to specify the player's crosshair color.
*/
class CrosshairColorSection final : public GameConfigSection<ServerConfigContext>
{
public:
explicit CrosshairColorSection() = default;

std::string_view GetName() const override final { return "CrosshairColor"; }

json::value_t GetType() const override final { return json::value_t::string; }

std::string GetSchema() const override final
{
return fmt::format(R"("pattern": "^\\d\\d?\\d? \\d\\d?\\d? \\d\\d?\\d?$")");
}

bool TryParse(GameConfigContext<ServerConfigContext>& context) const override final
{
const auto color = context.Input.get<std::string>();

if (!color.empty())
{
Vector colorValue{255, 255, 255};

UTIL_StringToVector(colorValue, color);

context.Data.State.m_CrosshairColor = {
static_cast<std::uint8_t>(colorValue.x),
static_cast<std::uint8_t>(colorValue.y),
static_cast<std::uint8_t>(colorValue.z)};
}

return true;
}
};
13 changes: 13 additions & 0 deletions src/game/server/entities/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ DEFINE_FIELD(m_SuitLightType, FIELD_INTEGER),
DEFINE_FIELD(m_DisplacerReturn, FIELD_POSITION_VECTOR),
DEFINE_FIELD(m_DisplacerSndRoomtype, FIELD_INTEGER),
DEFINE_FIELD(m_HudColor, FIELD_INTEGER),
DEFINE_FIELD(m_CrosshairColor, FIELD_INTEGER),

DEFINE_FIELD(m_bInfiniteAir, FIELD_BOOLEAN),
DEFINE_FIELD(m_bInfiniteArmor, FIELD_BOOLEAN),
Expand Down Expand Up @@ -4002,6 +4003,7 @@ void CBasePlayer::UpdateClientData()
{
// Reinitialize hud color to saved off value.
SetHudColor(RGB24::FromInteger(m_HudColor));
SetCrosshairColor(RGB24::FromInteger(m_CrosshairColor));
}

// Update Flashlight
Expand Down Expand Up @@ -4931,6 +4933,17 @@ void CBasePlayer::SetHudColor(RGB24 color)
g_engfuncs.pfnMessageEnd();
}

void CBasePlayer::SetCrosshairColor(RGB24 color)
{
m_CrosshairColor = color.ToInteger();

MESSAGE_BEGIN(MSG_ONE, gmsgCrosshairColor, nullptr, this);
g_engfuncs.pfnWriteByte(color.Red);
g_engfuncs.pfnWriteByte(color.Green);
g_engfuncs.pfnWriteByte(color.Blue);
g_engfuncs.pfnMessageEnd();
}

static void SendScoreInfoMessage(CBasePlayer* owner)
{
WRITE_BYTE(owner->entindex());
Expand Down
7 changes: 7 additions & 0 deletions src/game/server/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class CBasePlayer : public CBaseMonster

// For saving and level changes.
int m_HudColor = RGB_HUD_COLOR.ToInteger();
int m_CrosshairColor = RGB_CROSSHAIR_COLOR.ToInteger();

bool m_bInfiniteAir;
bool m_bInfiniteArmor;
Expand All @@ -635,6 +636,12 @@ class CBasePlayer : public CBaseMonster
*/
void SetHudColor(RGB24 color);

/**
* @brief Sets the player's crosshair color
* @details The player must be fully connected and ready to receive user messages for this to work
*/
void SetCrosshairColor(RGB24 color);

void SendScoreInfo(CBasePlayer* destination);
void SendScoreInfoAll();

Expand Down
72 changes: 72 additions & 0 deletions src/game/server/entities/player_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,78 @@ void CPlayerSetHudColor::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_
player->SetHudColor(color);
}

class CPlayerSetCrosshairColor : public CPointEntity
{
DECLARE_CLASS(CPlayerSetCrosshairColor, CPointEntity);
DECLARE_DATAMAP();

public:
enum class Action
{
Set = 0,
Reset = 1
};

bool KeyValue(KeyValueData* pkvd) override;

void Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value) override;

private:
Vector m_CrosshairColor{vec3_origin};
Action m_Action{Action::Set};
};

LINK_ENTITY_TO_CLASS(player_setcrosshaircolor, CPlayerSetCrosshairColor);

BEGIN_DATAMAP(CPlayerSetCrosshairColor)
DEFINE_FIELD(m_CrosshairColor, FIELD_VECTOR),
DEFINE_FIELD(m_Action, FIELD_INTEGER),
END_DATAMAP();

bool CPlayerSetCrosshairColor::KeyValue(KeyValueData* pkvd)
{
if (FStrEq(pkvd->szKeyName, "crosshair_color"))
{
UTIL_StringToVector(m_CrosshairColor, pkvd->szValue);
return true;
}
else if (FStrEq(pkvd->szKeyName, "action"))
{
m_Action = static_cast<Action>(atoi(pkvd->szValue));
return true;
}

return CPointEntity::KeyValue(pkvd);
}

void CPlayerSetCrosshairColor::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value)
{
auto player = ToBasePlayer(pActivator);

if (!player || !player->IsNetClient())
{
return;
}

const RGB24 color = [this]()
{
switch (m_Action)
{
case Action::Set:
return RGB24{
static_cast<std::uint8_t>(m_CrosshairColor.x),
static_cast<std::uint8_t>(m_CrosshairColor.y),
static_cast<std::uint8_t>(m_CrosshairColor.z)};

default:
case Action::Reset:
return RGB_HUD_COLOR;
}
}();

player->SetCrosshairColor(color);
}

class CPlayerSetSuitLightType : public CPointEntity
{
DECLARE_CLASS(CPlayerSetSuitLightType, CPointEntity);
Expand Down

0 comments on commit 70f1ae9

Please sign in to comment.