Skip to content

Commit 60777dc

Browse files
committed
setCameraFieldOfView improvements & resetCameraFieldOfView
1 parent a79a764 commit 60777dc

File tree

7 files changed

+244
-45
lines changed

7 files changed

+244
-45
lines changed

Client/game_sa/CSettingsSA.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,15 @@ float ms_fFOVCarMax = 100; // at high vehicle velocity
494494
bool ms_bFOVPlayerFromScript = false;
495495
bool ms_bFOVVehicleFromScript = false;
496496

497+
float ms_fovAiming = 70.0f;
498+
float ms_fovSniperAiming_Min = 0.0f; // default 15
499+
float ms_fovSniperAiming_Max = 179.0f; // default 70
500+
float ms_fov1stPersonAiming = 70.0f;
501+
float ms_fovSniperAiming = 70.0f;
502+
bool ms_fovAimingFromScript = false;
503+
bool ms_fovSniperAimingFromScript = false;
504+
bool ms_fov1stPersonAimingFromScript = false;
505+
497506
// consider moving this to the camera class - qaisjp
498507
float CSettingsSA::GetFieldOfViewPlayer()
499508
{
@@ -510,6 +519,82 @@ float CSettingsSA::GetFieldOfViewVehicleMax()
510519
return ms_fFOVCarMax;
511520
}
512521

522+
float CSettingsSA::GetFieldOfViewAiming()
523+
{
524+
CCamera* camera = pGame->GetCamera();
525+
CCam* cam = camera->GetCam(camera->GetActiveCam());
526+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
527+
528+
return (cameraViewMode == MODE_AIMWEAPON || cameraViewMode == MODE_AIMWEAPON_FROMCAR || cameraViewMode == MODE_AIMWEAPON_ATTACHED) ? cam->GetFOV() : ms_fovAiming;
529+
}
530+
531+
float CSettingsSA::GetFieldOfViewSniperAiming()
532+
{
533+
CCamera* camera = pGame->GetCamera();
534+
CCam* cam = camera->GetCam(camera->GetActiveCam());
535+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
536+
537+
return (cameraViewMode == MODE_SNIPER) ? cam->GetFOV() : ms_fovSniperAiming;
538+
}
539+
540+
float CSettingsSA::GetFieldOfView1stPersonAiming()
541+
{
542+
return ms_fov1stPersonAiming;
543+
}
544+
545+
void CSettingsSA::ResetFieldOfViewPlayer()
546+
{
547+
float fieldOfView;
548+
g_pCore->GetCVars()->Get("fov", fieldOfView);
549+
fieldOfView = Clamp(70.f, fieldOfView, 100.f);
550+
551+
ms_bFOVPlayerFromScript = false;
552+
SetFieldOfViewPlayer(fieldOfView, false);
553+
}
554+
555+
void CSettingsSA::ResetFieldOfViewVehicle()
556+
{
557+
float fieldOfView;
558+
g_pCore->GetCVars()->Get("fov", fieldOfView);
559+
fieldOfView = Clamp(70.f, fieldOfView, 100.f);
560+
561+
ms_bFOVVehicleFromScript = false;
562+
SetFieldOfViewVehicle(fieldOfView, false);
563+
}
564+
565+
void CSettingsSA::ResetFieldOfViewVehicleMax()
566+
{
567+
ms_bFOVVehicleFromScript = false;
568+
SetFieldOfViewVehicleMax(100, false);
569+
}
570+
571+
void CSettingsSA::ResetFieldOfViewSniperAiming()
572+
{
573+
// Restore original bytes
574+
// Zoom in
575+
MemPut<void*>(0x51089F, (void*)0x858CE0);
576+
MemPut<void*>(0x5108B8, (void*)0x858CE0);
577+
MemPut<void*>(0x5108AE, "\x8C\x42");
578+
MemPut<void*>(0x5108CF, "\x8C\x42");
579+
580+
// Zoom out
581+
MemPut<void*>(0x5109A3, (void*)0x858B48);
582+
MemPut<void*>(0x5109BC, (void*)0x858B48);
583+
MemPut<void*>(0x5109B2, "\x70\x41");
584+
MemPut<void*>(0x5109CF, "\x70\x41");
585+
586+
ms_fovSniperAimingFromScript = false;
587+
ms_fovSniperAiming = 70.0f;
588+
}
589+
590+
void CSettingsSA::ResetFieldOfView1stPersonAiming()
591+
{
592+
MemPut((void*)0x510711, "\xC7\x07\x00\x00\x8C\x42");
593+
594+
ms_fov1stPersonAimingFromScript = false;
595+
ms_fov1stPersonAiming = 70.0f;
596+
}
597+
513598
void CSettingsSA::UpdateFieldOfViewFromSettings()
514599
{
515600
float fFieldOfView;
@@ -518,12 +603,19 @@ void CSettingsSA::UpdateFieldOfViewFromSettings()
518603
SetFieldOfViewPlayer(fFieldOfView, false);
519604
SetFieldOfViewVehicle(fFieldOfView, false);
520605
SetFieldOfViewVehicleMax(100, false);
606+
607+
ResetFieldOfViewSniperAiming();
608+
ResetFieldOfView1stPersonAiming();
521609
}
522610

523611
void CSettingsSA::ResetFieldOfViewFromScript()
524612
{
525613
ms_bFOVPlayerFromScript = false;
526614
ms_bFOVVehicleFromScript = false;
615+
ms_fovAimingFromScript = false;
616+
ms_fovSniperAimingFromScript = false;
617+
ms_fov1stPersonAimingFromScript = false;
618+
527619
UpdateFieldOfViewFromSettings();
528620
}
529621

@@ -598,6 +690,82 @@ void CSettingsSA::SetFieldOfViewVehicleMax(float fAngle, bool bFromScript, bool
598690
MemPut<float>(0x0524BC5, ms_fFOVCarMax);
599691
}
600692

693+
//////////////////////////////////////////////
694+
// This needs to be called every frame, because CCam::Process_AimWeapon overrides the FOV every frame
695+
//////////////////////////////////////////////
696+
bool CSettingsSA::SetFieldOfViewAiming(float angle, bool fromScript)
697+
{
698+
if (!fromScript && ms_fovAimingFromScript)
699+
return false;
700+
701+
CCamera* camera = pGame->GetCamera();
702+
CCam* cam = camera->GetCam(camera->GetActiveCam());
703+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
704+
705+
if (cameraViewMode != MODE_AIMWEAPON && cameraViewMode != MODE_AIMWEAPON_FROMCAR && cameraViewMode != MODE_AIMWEAPON_ATTACHED)
706+
return false;
707+
708+
cam->SetFOV(angle);
709+
ms_fovAimingFromScript = fromScript;
710+
ms_fovAiming = angle;
711+
712+
return true;
713+
}
714+
715+
bool CSettingsSA::SetFieldOfViewSniperAiming(float angle, bool fromScript)
716+
{
717+
if (!fromScript && ms_fovSniperAimingFromScript)
718+
return false;
719+
720+
CCamera* camera = pGame->GetCamera();
721+
CCam* cam = camera->GetCam(camera->GetActiveCam());
722+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
723+
724+
if (!ms_fovSniperAimingFromScript && fromScript)
725+
{
726+
// Patch sniper zoom-in/zoom-out limit
727+
// zoom in
728+
MemPut<void*>(0x51089F, &ms_fovSniperAiming_Max);
729+
MemPut<void*>(0x5108B8, &ms_fovSniperAiming_Max);
730+
731+
MemPut<void*>(0x5108AE, "\x33\x43");
732+
MemPut<void*>(0x5108CF, "\x33\x43");
733+
734+
// zoom out
735+
MemPut<void*>(0x5109A3, &ms_fovSniperAiming_Min);
736+
MemPut<void*>(0x5109BC, &ms_fovSniperAiming_Min);
737+
MemPut<void*>(0x5109B2, "\x00\x00");
738+
MemPut<void*>(0x5109CF, "\x00\x00");
739+
}
740+
741+
if (cameraViewMode == MODE_SNIPER)
742+
cam->SetFOV(angle);
743+
744+
ms_fovSniperAimingFromScript = fromScript;
745+
ms_fovSniperAiming = angle;
746+
return true;
747+
}
748+
749+
bool CSettingsSA::SetFieldOfView1stPersonAiming(float angle, bool fromScript)
750+
{
751+
if (!fromScript && ms_fov1stPersonAimingFromScript)
752+
return false;
753+
754+
CCamera* camera = pGame->GetCamera();
755+
CCam* cam = camera->GetCam(camera->GetActiveCam());
756+
eCamMode cameraViewMode = static_cast<eCamMode>(camera->GetCam(camera->GetActiveCam())->GetMode());
757+
758+
if (!ms_fov1stPersonAimingFromScript && fromScript)
759+
MemSet((void*)0x510711, 0x90, 6);
760+
761+
if (cameraViewMode == MODE_SNIPER || cameraViewMode == MODE_M16_1STPERSON || cameraViewMode == MODE_HELICANNON_1STPERSON || cameraViewMode == MODE_CAMERA)
762+
cam->SetFOV(angle);
763+
764+
ms_fov1stPersonAimingFromScript = fromScript;
765+
ms_fov1stPersonAiming = angle;
766+
return true;
767+
}
768+
601769
////////////////////////////////////////////////
602770
//
603771
// Vehicles LOD draw distance

Client/game_sa/CSettingsSA.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,20 @@ class CSettingsSA : public CGameSettings
165165
void SetFieldOfViewPlayer(float fAngle, bool bFromScript, bool instant = false);
166166
void SetFieldOfViewVehicle(float fAngle, bool bFromScript, bool instant = false);
167167
void SetFieldOfViewVehicleMax(float fAngle, bool bFromScript, bool instant = false);
168+
bool SetFieldOfViewAiming(float angle, bool fromScript);
169+
bool SetFieldOfViewSniperAiming(float angle, bool fromScript);
170+
bool SetFieldOfView1stPersonAiming(float angle, bool fromScript);
168171
float GetFieldOfViewPlayer();
169172
float GetFieldOfViewVehicle();
170173
float GetFieldOfViewVehicleMax();
174+
float GetFieldOfViewAiming();
175+
float GetFieldOfViewSniperAiming();
176+
float GetFieldOfView1stPersonAiming();
177+
void ResetFieldOfViewPlayer();
178+
void ResetFieldOfViewVehicle();
179+
void ResetFieldOfViewVehicleMax();
180+
void ResetFieldOfViewSniperAiming();
181+
void ResetFieldOfView1stPersonAiming();
171182

172183
void SetVehiclesLODDistance(float fVehiclesLODDistance, float fTrainsPlanesLODDistance, bool bFromScript);
173184
void ResetVehiclesLODDistance(bool bForceDefault = false);

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ ADD_ENUM(FOV_MODE_PLAYER, "player")
554554
ADD_ENUM(FOV_MODE_VEHICLE, "vehicle")
555555
ADD_ENUM(FOV_MODE_VEHICLE_MAX, "vehicle_max")
556556
ADD_ENUM(FOV_MODE_AIMING, "aiming")
557+
ADD_ENUM(FOV_MODE_SNIPER_AIMING, "sniper_aiming")
558+
ADD_ENUM(FOV_MODE_1ST_PERSON_AIMING, "1stperson_aiming")
557559
IMPLEMENT_ENUM_END("fieldofview-mode")
558560

559561
IMPLEMENT_ENUM_BEGIN(eTrayIconType)

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ enum eFieldOfViewMode
130130
FOV_MODE_PLAYER,
131131
FOV_MODE_VEHICLE,
132132
FOV_MODE_VEHICLE_MAX,
133-
FOV_MODE_AIMING
133+
FOV_MODE_AIMING,
134+
FOV_MODE_SNIPER_AIMING,
135+
FOV_MODE_1ST_PERSON_AIMING
134136
};
135137
DECLARE_ENUM(eFieldOfViewMode);
136138

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ void CLuaCameraDefs::LoadFunctions()
3333

3434
// Cam set funcs
3535
{"setCameraMatrix", SetCameraMatrix},
36-
{"setCameraFieldOfView", SetCameraFieldOfView},
36+
{"setCameraFieldOfView", ArgumentParserWarn<false, SetCameraFieldOfView>},
37+
{"resetCameraFieldOfView", ArgumentParser<ResetCameraFieldOfView>},
3738
{"setCameraTarget", SetCameraTarget},
3839
{"setCameraInterior", SetCameraInterior},
3940
{"fadeCamera", FadeCamera},
@@ -77,6 +78,7 @@ void CLuaCameraDefs::AddClass(lua_State* luaVM)
7778
lua_classfunction(luaVM, "setRotation", OOP_SetCameraRotation);
7879
lua_classfunction(luaVM, "setMatrix", "setCameraMatrix");
7980
lua_classfunction(luaVM, "setFieldOfView", "setCameraFieldOfView");
81+
lua_classfunction(luaVM, "resetFieldOfView", "resetCameraFieldOfView");
8082
lua_classfunction(luaVM, "setInterior", "setCameraInterior");
8183
lua_classfunction(luaVM, "setTarget", "setCameraTarget");
8284
lua_classfunction(luaVM, "setViewMode", "setCameraViewMode");
@@ -211,54 +213,55 @@ int CLuaCameraDefs::SetCameraMatrix(lua_State* luaVM)
211213
return 1;
212214
}
213215

214-
// Only when onfoot/invehicle
215-
int CLuaCameraDefs::SetCameraFieldOfView(lua_State* luaVM)
216+
bool CLuaCameraDefs::SetCameraFieldOfView(eFieldOfViewMode mode, float fov, std::optional<bool> instant)
216217
{
217-
float fFOV;
218-
eFieldOfViewMode eMode;
219-
bool instant;
220-
CScriptArgReader argStream(luaVM);
221-
argStream.ReadEnumString(eMode);
222-
argStream.ReadNumber(fFOV);
223-
argStream.ReadBool(instant, false);
218+
if (fov < 0 || fov > 179)
219+
throw std::invalid_argument("Invalid FOV range (0-179)");
224220

225-
if (!argStream.HasErrors())
221+
switch (mode)
226222
{
227-
while (true)
228-
{
229-
if (fFOV < 0 || fFOV > 179)
230-
{
231-
argStream.SetCustomError("Invalid FOV range (0-179)");
232-
break;
233-
}
223+
case FOV_MODE_PLAYER:
224+
g_pGame->GetSettings()->SetFieldOfViewPlayer(fov, true, instant.value_or(false));
225+
break;
226+
case FOV_MODE_VEHICLE:
227+
g_pGame->GetSettings()->SetFieldOfViewVehicle(fov, true, instant.value_or(false));
228+
break;
229+
case FOV_MODE_VEHICLE_MAX:
230+
g_pGame->GetSettings()->SetFieldOfViewVehicleMax(fov, true, instant.value_or(false));
231+
break;
232+
case FOV_MODE_AIMING:
233+
return g_pGame->GetSettings()->SetFieldOfViewAiming(fov, true);
234+
case FOV_MODE_SNIPER_AIMING:
235+
return g_pGame->GetSettings()->SetFieldOfViewSniperAiming(fov, true);
236+
case FOV_MODE_1ST_PERSON_AIMING:
237+
return g_pGame->GetSettings()->SetFieldOfView1stPersonAiming(fov, true);
238+
}
234239

235-
if (eMode == FOV_MODE_PLAYER)
236-
{
237-
g_pGame->GetSettings()->SetFieldOfViewPlayer(fFOV, true, instant);
238-
}
239-
else if (eMode == FOV_MODE_VEHICLE)
240-
{
241-
g_pGame->GetSettings()->SetFieldOfViewVehicle(fFOV, true, instant);
242-
}
243-
else if (eMode == FOV_MODE_VEHICLE_MAX)
244-
{
245-
g_pGame->GetSettings()->SetFieldOfViewVehicleMax(fFOV, true, instant);
246-
}
247-
else
248-
{
249-
argStream.m_iIndex = 1;
250-
argStream.SetCustomError(SString("Enum not yet implemented: " + EnumToString(eMode)));
251-
break;
252-
}
240+
return true;
241+
}
253242

254-
lua_pushboolean(luaVM, true);
255-
return 1;
256-
}
243+
void CLuaCameraDefs::ResetCameraFieldOfView(eFieldOfViewMode mode)
244+
{
245+
switch (mode)
246+
{
247+
case FOV_MODE_PLAYER:
248+
g_pGame->GetSettings()->ResetFieldOfViewPlayer();
249+
break;
250+
case FOV_MODE_VEHICLE:
251+
g_pGame->GetSettings()->ResetFieldOfViewVehicle();
252+
break;
253+
case FOV_MODE_VEHICLE_MAX:
254+
g_pGame->GetSettings()->ResetFieldOfViewVehicleMax();
255+
break;
256+
case FOV_MODE_AIMING:
257+
break;
258+
case FOV_MODE_SNIPER_AIMING:
259+
g_pGame->GetSettings()->ResetFieldOfViewSniperAiming();
260+
break;
261+
case FOV_MODE_1ST_PERSON_AIMING:
262+
g_pGame->GetSettings()->ResetFieldOfView1stPersonAiming();
263+
break;
257264
}
258-
259-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
260-
lua_pushboolean(luaVM, false);
261-
return 1;
262265
}
263266

264267
// Only when onfoot/invehicle

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class CLuaCameraDefs : public CLuaDefs
3636
LUA_DECLARE(SetCameraMatrix);
3737
LUA_DECLARE(SetCameraTarget);
3838
LUA_DECLARE(SetCameraInterior);
39-
LUA_DECLARE(SetCameraFieldOfView);
39+
static bool SetCameraFieldOfView(eFieldOfViewMode mode, float fov, std::optional<bool> instant);
40+
static void ResetCameraFieldOfView(eFieldOfViewMode mode);
4041
LUA_DECLARE(FadeCamera);
4142
LUA_DECLARE(SetCameraClip);
4243
LUA_DECLARE(GetCameraClip);

Client/sdk/game/CSettings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,22 @@ class CGameSettings
158158
virtual void SetFieldOfViewPlayer(float fAngle, bool bFromScript, bool instant = false) = 0;
159159
virtual void SetFieldOfViewVehicle(float fAngle, bool bFromScript, bool instant = false) = 0;
160160
virtual void SetFieldOfViewVehicleMax(float fAngle, bool bFromScript, bool instant = false) = 0;
161+
virtual bool SetFieldOfViewAiming(float angle, bool fromScript) = 0;
162+
virtual bool SetFieldOfViewSniperAiming(float angle, bool fromScript) = 0;
163+
virtual bool SetFieldOfView1stPersonAiming(float angle, bool fromScript) = 0;
161164

162165
virtual float GetFieldOfViewPlayer() = 0;
163166
virtual float GetFieldOfViewVehicle() = 0;
164167
virtual float GetFieldOfViewVehicleMax() = 0;
168+
virtual float GetFieldOfViewAiming() = 0;
169+
virtual float GetFieldOfViewSniperAiming() = 0;
170+
virtual float GetFieldOfView1stPersonAiming() = 0;
171+
172+
virtual void ResetFieldOfViewPlayer() = 0;
173+
virtual void ResetFieldOfViewVehicle() = 0;
174+
virtual void ResetFieldOfViewVehicleMax() = 0;
175+
virtual void ResetFieldOfViewSniperAiming() = 0;
176+
virtual void ResetFieldOfView1stPersonAiming() = 0;
165177

166178
virtual void SetVehiclesLODDistance(float fVehiclesLODDistance, float fTrainsPlanesLODDistance, bool bFromScript) = 0;
167179
virtual void ResetVehiclesLODDistance(bool bForceDefault = false) = 0;

0 commit comments

Comments
 (0)