Skip to content

Commit 03e851a

Browse files
authored
Add new function isPlayerCrosshairVisible (#3720)
1 parent ed2d08f commit 03e851a

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

Client/game_sa/CHudSA.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include "StdInc.h"
1313
#include "CHudSA.h"
14+
#include "CGameSA.h"
15+
#include "CCameraSA.h"
16+
17+
extern CGameSA* pGame;
1418

1519
char szVehicleName[50] = {'\0'};
1620
char szZoneName[50] = {'\0'};
@@ -171,3 +175,38 @@ void CHudSA::ResetComponentAdjustment()
171175
MemPut<float>(m_pfCameraCrosshairScale, 192.0f);
172176
m_fSniperCrosshairScale = 210.0f;
173177
}
178+
179+
bool CHudSA::IsCrosshairVisible()
180+
{
181+
if (!IsComponentVisible(HUD_CROSSHAIR))
182+
return false;
183+
184+
CCamera* camera = pGame->GetCamera();
185+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
186+
187+
switch (cameraViewMode)
188+
{
189+
case MODE_SNIPER_RUNABOUT:
190+
case MODE_ROCKETLAUNCHER_RUNABOUT:
191+
case MODE_ROCKETLAUNCHER_RUNABOUT_HS:
192+
case MODE_M16_1STPERSON_RUNABOUT:
193+
case MODE_1STPERSON_RUNABOUT:
194+
case MODE_AIMWEAPON:
195+
case MODE_AIMWEAPON_ATTACHED:
196+
case MODE_AIMWEAPON_FROMCAR:
197+
case MODE_M16_1STPERSON:
198+
case MODE_HELICANNON_1STPERSON:
199+
case MODE_SNIPER:
200+
case MODE_ROCKETLAUNCHER:
201+
case MODE_ROCKETLAUNCHER_HS:
202+
case MODE_AIMING:
203+
case MODE_CAMERA:
204+
return true;
205+
default:
206+
break;
207+
}
208+
209+
// Check CTheScripts::bDrawCrossHair
210+
std::uint8_t crossHairType = *reinterpret_cast<std::uint8_t*>(VAR_CTheScripts_bDrawCrossHair);
211+
return crossHairType > 0;
212+
}

Client/game_sa/CHudSA.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
#define CODE_ShowMoney 0x58F47D
4040

41+
#define VAR_CTheScripts_bDrawCrossHair 0xA44490
42+
4143
struct SHudComponent
4244
{
4345
bool bIsPartOfAll;
@@ -59,6 +61,7 @@ class CHudSA : public CHud
5961
bool IsComponentVisible(eHudComponent component);
6062
void AdjustComponents(float fAspectRatio);
6163
void ResetComponentAdjustment();
64+
bool IsCrosshairVisible();
6265

6366
protected:
6467
void InitComponentList();

Client/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void CLuaPlayerDefs::LoadFunctions()
2929
{"getPlayerMoney", GetPlayerMoney},
3030
{"getPlayerWantedLevel", GetPlayerWantedLevel},
3131
{"getPlayerScriptDebugLevel", ArgumentParser<GetPlayerScriptDebugLevel>},
32+
{"isPlayerCrosshairVisible", ArgumentParser<IsPlayerCrosshairVisible>},
3233

3334
// Player set funcs
3435
{"showPlayerHudComponent", ShowPlayerHudComponent},
@@ -91,13 +92,15 @@ void CLuaPlayerDefs::AddClass(lua_State* luaVM)
9192
lua_classfunction(luaVM, "getScriptDebugLevel", "getPlayerScriptDebugLevel");
9293

9394
lua_classfunction(luaVM, "isNametagShowing", "isPlayerNametagShowing");
95+
lua_classfunction(luaVM, "isCrosshairVisible", "isPlayerCrosshairVisible");
9496

9597
lua_classvariable(luaVM, "ping", NULL, "getPlayerPing");
9698
lua_classvariable(luaVM, "name", NULL, "getPlayerName");
9799
lua_classvariable(luaVM, "team", NULL, "getPlayerTeam");
98100
lua_classvariable(luaVM, "scriptDebugLevel", nullptr, "getPlayerScriptDebugLevel");
99101
lua_classvariable(luaVM, "nametagText", "setPlayerNametagText", "getPlayerNametagText");
100102
lua_classvariable(luaVM, "nametagShowing", "setPlayerNametagShowing", "isPlayerNametagShowing");
103+
lua_classvariable(luaVM, "crosshairVisible", nullptr, "isPlayerCrosshairVisible");
101104

102105
lua_registerclass(luaVM, "Player", "Ped");
103106
}
@@ -636,3 +639,8 @@ unsigned char CLuaPlayerDefs::GetPlayerMapOpacity()
636639
int iMapOpacity = g_pCore->GetCVars()->GetValue<int>("mapalpha");
637640
return static_cast<unsigned char>(Clamp(0, iMapOpacity, 255));
638641
}
642+
643+
bool CLuaPlayerDefs::IsPlayerCrosshairVisible()
644+
{
645+
return g_pGame->GetHud()->IsCrosshairVisible();
646+
}

Client/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CLuaPlayerDefs : public CLuaDefs
3030
LUA_DECLARE(GetPlayerMoney);
3131
LUA_DECLARE(GetPlayerWantedLevel);
3232
static std::uint8_t GetPlayerScriptDebugLevel() noexcept;
33+
static bool IsPlayerCrosshairVisible();
3334

3435
// Player set
3536
LUA_DECLARE(ShowPlayerHudComponent);

Client/sdk/game/CHud.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ class CHud
4141
virtual bool IsComponentVisible(eHudComponent component) = 0;
4242
virtual void AdjustComponents(float fAspectRatio) = 0;
4343
virtual void ResetComponentAdjustment() = 0;
44+
virtual bool IsCrosshairVisible() = 0;
4445
};

0 commit comments

Comments
 (0)