From eea9786bab4c3e386fb7ff8d78c6702a91530603 Mon Sep 17 00:00:00 2001 From: notGreg Date: Tue, 2 Aug 2022 11:12:35 +0200 Subject: [PATCH] code cleanup --- TarkovUniformAim/UniformAimPatch.cs | 3 ++ TarkovUniformAim/UniformAimPlugin.cs | 69 ++++++++-------------------- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/TarkovUniformAim/UniformAimPatch.cs b/TarkovUniformAim/UniformAimPatch.cs index e430820..6f4175f 100644 --- a/TarkovUniformAim/UniformAimPatch.cs +++ b/TarkovUniformAim/UniformAimPatch.cs @@ -6,6 +6,7 @@ namespace UniformAim { public class UpdateSensitivityPatch : ModulePatch { + protected override MethodBase GetTargetMethod() { return typeof(Player.FirearmController).GetMethod("UpdateSensitivity"); @@ -66,4 +67,6 @@ namespace UniformAim Plugin.isAiming = ____isAiming; } } + + } \ No newline at end of file diff --git a/TarkovUniformAim/UniformAimPlugin.cs b/TarkovUniformAim/UniformAimPlugin.cs index 31c8a5e..115b69b 100644 --- a/TarkovUniformAim/UniformAimPlugin.cs +++ b/TarkovUniformAim/UniformAimPlugin.cs @@ -15,7 +15,6 @@ namespace UniformAim public static ConfigEntry configCoeff; public static ConfigEntry configSens; public static ConfigEntry configUseHFOV; - public static ConfigEntry configEnableLogging; //TODO: figure out a way to read game settings to default the configFOV.Value to whatever the player has already set //TODO: figure out a way to read game settings to apply configSens.Value as a multiplier on top of Tarkov's stock sensitivity setting @@ -33,17 +32,14 @@ namespace UniformAim float ScopeFOV; float currentFOV; - //so we don't keep repeating ourselves + //values to prevent unnecessary repetition float cachedFOV; string cachedDebugInfo; - //Return aspect ratio based on game window resolution, currently unused but could be useful in the future + //aspect ratio for further calculations float GetAspectRatio() { - float resX = Convert.ToUInt16(Screen.width.ToString()); - float resY = Convert.ToUInt16(Screen.height.ToString()); - - return (resX / resY); + return Camera.allCameras[0].aspect; } //calculate horizontal FOV based on vertical FOV, currently unused but could be useful in the future float CalculateHFOV(float FOV) @@ -51,33 +47,25 @@ namespace UniformAim float vFOVRad = FOV / 2 * Mathf.Deg2Rad; float hFOVRad = (float)(2 * Math.Atan(GetAspectRatio() * Math.Tan(vFOVRad))); - if (configEnableLogging.Value) { Logger.LogInfo("Calculate HFOV: " + FOV + " Result: " + hFOVRad * Mathf.Rad2Deg); } - return (float)(hFOVRad * Mathf.Rad2Deg); } - //calculate sensitivity based on FOV difference and coefficient + //calculate sensitivity based on FOV difference and the coefficient float CalculateSensitivity(float aimedFOV, float hipFOV) - { + { //clamp to avoid invalid values - aimedFOV = Mathf.Clamp(aimedFOV, 1f, 75f); - hipFOV = Mathf.Clamp(hipFOV, 1f, 75f); + aimedFOV = Mathf.Clamp(aimedFOV, 1f, 75f) * Mathf.Deg2Rad; + hipFOV = Mathf.Clamp(hipFOV, 1f, 75f) * Mathf.Deg2Rad; //check if configUseHFOV is enabled, convert to horizontal degrees if true - if(configUseHFOV.Value) + if (configUseHFOV.Value) { - Logger.LogInfo("HFOV Hip: " + CalculateHFOV(hipFOV) + " HFOV Aim: " + CalculateHFOV(aimedFOV)); - aimedFOV = CalculateHFOV(aimedFOV) / 2 * Mathf.Deg2Rad; - hipFOV = CalculateHFOV(hipFOV) / 2 * Mathf.Deg2Rad; - } - else - { - aimedFOV = aimedFOV / 2 * Mathf.Deg2Rad; - hipFOV = hipFOV / 2 * Mathf.Deg2Rad; + aimedFOV = CalculateHFOV(aimedFOV); + hipFOV = CalculateHFOV(hipFOV); } float exponent = (float)(100f / configCoeff.Value); - float tanRatio = (float)(Math.Tan(aimedFOV) / Math.Tan(hipFOV)); + float tanRatio = (float)(Math.Tan(aimedFOV / 2) / Math.Tan(hipFOV / 2)); float sensitivity = configSens.Value / 100f; @@ -96,23 +84,10 @@ namespace UniformAim //determine the correct FOV for calculations void DetermineCurrentFOV() { - if (SelectedScope == 0 && isScopeCameraActive()) { currentFOV = ScopeFOV; } else {currentFOV = FPSCameraFOV; } + if (SelectedScope == 0 && isScopeCameraActive()) { currentFOV = ScopeFOV; } else { currentFOV = FPSCameraFOV; } //dirty fix for the Ultima MP-155 shotgun - if(FPSCameraFOV > 35 && isScopeCameraActive() && ScopeFOV == 15) { currentFOV = FPSCameraFOV; } - } - - void LogDebugInfo() - { - //preformatted debug info string - string currentDebugInfo = $"Current FOV: {currentFOV} FPS Camera FOV: {FPSCameraFOV} Scope FOV: {ScopeFOV} Cached FOV: {cachedFOV} Sens: {mySens} SelectedScope: {SelectedScope}"; - - //only output currentDebugInfo if there were any changes - if (currentDebugInfo != cachedDebugInfo) - { - Logger.LogInfo(currentDebugInfo); cachedDebugInfo = currentDebugInfo; - } - + if (FPSCameraFOV > 35 && isScopeCameraActive() && ScopeFOV == 15) { currentFOV = FPSCameraFOV; } } void Awake() @@ -134,33 +109,27 @@ namespace UniformAim //use HFOV instead of VFOV for sensitivity calculations configUseHFOV = Config.Bind("General", "Use Horizontal FOV?", true, new ConfigDescription("Toggles between using Horizontal FOV and Vertical FOV for sensitivity calculations.")); - - //enable logging - configEnableLogging = Config.Bind("Debug", "Enable logging", false, new ConfigDescription("Enables logging in BepInEx console, extremely spammy!")); } void FixedUpdate() { //FixedUpdate() at 50Hz (Unity default) tickrate appears to resolve the issue of this script breaking when AI spawns. - Time.fixedDeltaTime = (1f/50f); + Time.fixedDeltaTime = (1f / 50f); if (isAiming) { //Grab FOV values for calculation - FPSCameraFOV = Camera.allCameras[0].fieldOfView; - if (Camera.allCamerasCount > 1) { ScopeFOV = Camera.allCameras[1].fieldOfView; } + FPSCameraFOV = Camera.allCameras[0].fieldOfView; //Camera[0] tends to be FPS Camera + if (Camera.allCamerasCount > 1) { ScopeFOV = Camera.allCameras[1].fieldOfView; } //Camera[1] tends to be BaseOpticCamera //Figure out if the FPSCamera is zoomed in, prevents the script from ticking while the player is healing - if(FPSCameraFOV < configFOV.Value) - { + if (FPSCameraFOV < configFOV.Value) + { DetermineCurrentFOV(); //do not update sensitivity if currentFOV hasn't changed if (cachedFOV != currentFOV) { mySens = CalculateSensitivity(currentFOV, configFOV.Value); cachedFOV = currentFOV; } - } + } } - - //draw debug info - if (configEnableLogging.Value) { LogDebugInfo(); } } } }