From c5dc51a0dbba4e2b6a1dfc14d1a2c27f11ff837b Mon Sep 17 00:00:00 2001 From: notGreg Date: Sat, 19 Nov 2022 19:15:44 +0100 Subject: [PATCH] Update for 3.3.0 Documented code for future maintability --- notGregs.UniformAim/Plugin.cs | 82 +++++++++++++------ notGregs.UniformAim/notGreg.UniformAim.csproj | 64 ++++++--------- 2 files changed, 82 insertions(+), 64 deletions(-) diff --git a/notGregs.UniformAim/Plugin.cs b/notGregs.UniformAim/Plugin.cs index 98c5f86..78d0b97 100644 --- a/notGregs.UniformAim/Plugin.cs +++ b/notGregs.UniformAim/Plugin.cs @@ -9,14 +9,16 @@ using UnityEngine; namespace notGreg.UniformAim { - [BepInPlugin("com.notGreg.UniformAim", "notGreg's Uniform Aim for Tarkov", "2.0.0")] + [BepInPlugin("com.notGreg.UniformAim", "notGreg's Uniform Aim for Tarkov", "3.3.0")] public class Plugin : BaseUnityPlugin { ConfigEntry configExponent; + ConfigEntry configSens; void Awake() { configExponent = Config.Bind("General", "Coefficient", 133, new ConfigDescription("", new AcceptableValueRange(10, 200))); + configSens = Config.Bind("General", "Sensitivity", 1.0f, new ConfigDescription("", new AcceptableValueRange(0.01f, 2.0f))); new get_AimingSensitivityPatch().Enable(); } @@ -26,16 +28,22 @@ namespace notGreg.UniformAim float inGameAimedSens; public static float aimingSens; + //this function can be replaced by FixedUpdate() at 50Hz (adjustable via Time.fixedDeltaTime) + //FixedUpdate() proved to be slightly more reliable in the past, however it had other unintended effects on the game. void Update() { + //check if the world instance exists //Logger.LogInfo("Checking world instance"); if (Singleton.Instance == null) { return; } + + //grab the game status of the existing instance //Logger.LogInfo("Checking game status"); GameStatus currentGameStatus = Singleton.Instance.Status; + //check if the game has started and if the player exists. If the player is aiming, update the scoped sensitivity (executed every frame while aiming) //Logger.LogInfo("Checking GameStatus and isAiming"); if (currentGameStatus == GameStatus.Started && mainPlayer != null) { @@ -56,12 +64,6 @@ namespace notGreg.UniformAim //Logger.LogInfo($"Subscribing to onAimingChanged event"); subscribeOnAimingChanged(); - inGameFOV = getInGameFOV(); - //Logger.LogInfo($"Getting in-game FOV: {inGameFOV}"); - - inGameAimedSens = getInGameAimSens(); - //Logger.LogInfo($"Getting in-game Aiming Sens: {inGameAimedSens}"); - //Logger.LogInfo("TryGetCameras coroutines"); StartCoroutine(tryGetMainCamera()); StartCoroutine(tryGetScopeCamera()); @@ -79,16 +81,26 @@ namespace notGreg.UniformAim break; } } - } + + //this function grabs the Field of View from the settings. This is the function that most often needs patching after update. + //Appropriate class can usually be found by searching for the ClearSettings function. int getInGameFOV() { - return Singleton.Instance.Game.Settings.FieldOfView; ; + //int fov = Singleton.Instance.Game.Settings.FieldOfView; //SPT-AKI 3.2.3 + int fov = Singleton.Instance.Game.Settings.FieldOfView; //SPT-AKI 3.3.0 + //Logger.LogInfo($"In-game FOV: {fov}"); + return fov; } + //this function grabs the Aiming Sensitivity from the settings. This is the function that most often needs patching after update. + //Appropriate class can usually be found by searching for the ClearSettings function. float getInGameAimSens() { - return Singleton.Instance.Control.Settings.MouseAimingSensitivity; + //float sens = Singleton.Instance.Control.Settings.MouseAimingSensitivity; //SPT-AKI 3.2.* + float sens = Singleton.Instance.Control.Settings.MouseAimingSensitivity; //SPT-AKI 3.3.0 + //Logger.LogInfo($"In-game AimSens: {sens}"); + return sens; } Player getLocalPlayer() @@ -99,6 +111,8 @@ namespace notGreg.UniformAim WaitForSecondsRealtime myDelay = new WaitForSecondsRealtime(1f); Camera mainCamera; Camera scopeCamera; + + //this coroutine attempts to find the FPS Camera in the scene. IEnumerator tryGetMainCamera() { string cameraName = "FPS Camera"; @@ -117,6 +131,7 @@ namespace notGreg.UniformAim yield return null; } + //this coroutine attempts to find existing baseOpticCamera in the scene. The state of this camera is used to determine whether the player is using a magnified optic or not. IEnumerator tryGetScopeCamera() { string cameraName = "BaseOpticCamera(Clone)"; @@ -125,35 +140,41 @@ namespace notGreg.UniformAim scopeCamera = GameObject.Find(cameraName).GetComponent(); //Logger.LogInfo($"{scopeCamera.name} found!"); } - //else - //{ - //Logger.LogMessage($"Camera \"{cameraName}\" not found");//, rescheduling..."); - //yield return myDelay; - //StartCoroutine(tryGetScopeCamera()); yield break; - //} - //yield return null; } + //convert vertical FOV (degrees) to horizontal FOV (degrees) float calculateHFOV(Camera camera) { return Camera.VerticalToHorizontalFieldOfView(camera.fieldOfView, camera.aspect); } + //figure out whether the player is using a magnified optic or not. Return the FOV of the sight. Camera determineCurrentAimedFOV() { - string scopeName = mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.Item.Name; - var scopeIndex = mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.SelectedScopeIndex; - var scopeMode = mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.SelectedScopeMode; + var currentAimingMode = mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod; + //get the name of the currently active scope + string scopeName = currentAimingMode.Item.Template.Name; + + //Logger.LogInfo($"Scope name: {scopeName}"); + + //scopeMode determines the scope being used (e.g. main magnified optic at index 0, backup RDS at index 1) + //scopeIndex determines the mode of the scope being used (e.g. x1 magnification at index 0, x4 magnification at index 1) + //there are exceptions to this rule thanks to BSG's inconsistency, some of them are patched below + var scopeIndex = currentAimingMode.SelectedScopeIndex; + var scopeMode = currentAimingMode.SelectedScopeMode; + + //patches for specific scopes, matches item name switch (scopeName) { - case "tactical_mp155_kalashnikov_ultima_camera(Clone)": + case "tactical_mp155_kalashnikov_ultima_camera": { //Logger.LogInfo("MP-155 Thermal"); return mainCamera; } - case "scope_leupold_d_evo(Clone)": + //SAM-SWAT's Leupold D-Evo optic patch. The modes on this scope are reversed. Causes detection based on baseOpticCamera to fail as the magnified optic camera is always active + case "scope_leupold_d_evo": { if (scopeMode == 0) { @@ -184,24 +205,33 @@ namespace notGreg.UniformAim float calculateSensitivity() { + //grab vertical hipfire field of view (FOV set by the user in the setting), then convert it to horizontal FOV + //convert degrees to radians float hipFOV = Mathf.Deg2Rad * Camera.VerticalToHorizontalFieldOfView(inGameFOV, mainCamera.aspect); + + //grab current field of view while aiming, then convert it to horizontal FOV + //convert degrees to radians float aimedFOV = Mathf.Deg2Rad * calculateHFOV(determineCurrentAimedFOV()); + //exponent applied to the ratio of aimedFOV to hipFOV, causes sights to become relatively faster or slower as zoom increases float exponent = 100f / configExponent.Value; float tanRatio = (float)(Mathf.Tan(aimedFOV / 2) / Mathf.Tan(hipFOV / 2)); - float sensitivity = (float)Math.Pow(tanRatio, exponent); + float sensitivity = (float)Math.Pow(tanRatio, exponent) * inGameAimedSens; - //Logger.LogInfo($"Final sensitivity: {sensitivity * inGameAimedSens}"); - return sensitivity * inGameAimedSens; + //Logger.LogInfo($"Sensitivity: {sensitivity}"); + return sensitivity * configSens.Value; } + void subscribeOnAimingChanged() { + //HandsChangedEvent triggers whenever player changes weapons + //without it the patch would cease to work as expected when weapons were changed mainPlayer.HandsChangedEvent += (handsArgs) => { - StartCoroutine(tryGetScopeCamera()); + //onAimingChanged triggers whenever the player starts or stops aiming. mainPlayer.HandsController.OnAimingChanged += (aimArgs) => { inGameFOV = getInGameFOV(); diff --git a/notGregs.UniformAim/notGreg.UniformAim.csproj b/notGregs.UniformAim/notGreg.UniformAim.csproj index 5b7c3d4..4453001 100644 --- a/notGregs.UniformAim/notGreg.UniformAim.csproj +++ b/notGregs.UniformAim/notGreg.UniformAim.csproj @@ -30,48 +30,36 @@ prompt 4 - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\Aki.Build.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\Aki.Common.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\Aki.Reflection.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\Assembly-CSharp.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\BepInEx\core\BepInEx.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\Comfort.dll - - - False - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\ItemComponent.Types.dll - - - - - - - - - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\UnityEngine.dll - - - E:\SPT-AKI\SPT-AKI 3.2.3\EscapeFromTarkov_Data\Managed\UnityEngine.CoreModule.dll - - + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\EscapeFromTarkov_Data\Managed\Aki.Reflection.dll + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\EscapeFromTarkov_Data\Managed\Assembly-CSharp.dll + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\BepInEx\core\BepInEx.dll + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\EscapeFromTarkov_Data\Managed\Comfort.dll + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\EscapeFromTarkov_Data\Managed\UnityEngine.dll + + + False + E:\SPT-AKI\SPT-AKI 3.3.0\EscapeFromTarkov_Data\Managed\UnityEngine.CoreModule.dll + + \ No newline at end of file