UniformAim/TarkovUniformAim/UniformAimPlugin.cs

137 lines
5.5 KiB
C#
Raw Normal View History

using BepInEx;
using BepInEx.Configuration;
using System;
using UnityEngine;
namespace UniformAim
{
[BepInPlugin("com.greg.tarkovuniformaim", "Uniform Aim for Tarkov", "1.0.0")]
[BepInProcess("EscapeFromTarkov.exe")]
public class Plugin : BaseUnityPlugin
{
//Bepinex.Configurator fields
public static ConfigEntry<int> configFOV;
public static ConfigEntry<int> configCoeff;
public static ConfigEntry<int> configSens;
public static ConfigEntry<bool> configUseHFOV;
//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
public static float mySens = 1f;
public static float aimingSens;
//sight data for hacky workarounds
public static int SelectedScope = 0;
public static int SelectedScopeMode = 0;
public static bool isAiming = false;
//human-friendly names for variables used later
float FPSCameraFOV;
float ScopeFOV;
float currentFOV;
2022-08-02 11:12:35 +02:00
//values to prevent unnecessary repetition
float cachedFOV;
string cachedDebugInfo;
2022-08-02 11:12:35 +02:00
//aspect ratio for further calculations
float GetAspectRatio()
{
2022-08-02 11:12:35 +02:00
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)
{
float vFOVRad = FOV / 2 * Mathf.Deg2Rad;
float hFOVRad = (float)(2 * Math.Atan(GetAspectRatio() * Math.Tan(vFOVRad)));
return (float)(hFOVRad * Mathf.Rad2Deg);
}
2022-08-02 11:12:35 +02:00
//calculate sensitivity based on FOV difference and the coefficient
float CalculateSensitivity(float aimedFOV, float hipFOV)
2022-08-02 11:12:35 +02:00
{
//clamp to avoid invalid values
2022-08-02 11:12:35 +02:00
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
2022-08-02 11:12:35 +02:00
if (configUseHFOV.Value)
{
2022-08-02 11:12:35 +02:00
aimedFOV = CalculateHFOV(aimedFOV);
hipFOV = CalculateHFOV(hipFOV);
}
float exponent = (float)(100f / configCoeff.Value);
2022-08-02 11:12:35 +02:00
float tanRatio = (float)(Math.Tan(aimedFOV / 2) / Math.Tan(hipFOV / 2));
float sensitivity = configSens.Value / 100f;
float result = (float)Math.Pow(tanRatio, exponent) * sensitivity;
return result;
}
//determine if the player is looking through the scope
bool isScopeCameraActive()
{
if (Camera.allCamerasCount > 1) { return Camera.allCameras[1].GetComponent<Behaviour>().isActiveAndEnabled; }
return false;
}
//determine the correct FOV for calculations
void DetermineCurrentFOV()
{
2022-08-02 11:12:35 +02:00
if (SelectedScope == 0 && isScopeCameraActive()) { currentFOV = ScopeFOV; } else { currentFOV = FPSCameraFOV; }
//dirty fix for the Ultima MP-155 shotgun
2022-08-02 11:12:35 +02:00
if (FPSCameraFOV > 35 && isScopeCameraActive() && ScopeFOV == 15) { currentFOV = FPSCameraFOV; }
2022-06-22 18:35:54 +02:00
}
void Awake()
{
new UpdateSensitivityPatch().Enable();
new get_AimingSensitivityPatch().Enable();
new get_SelectedScopeIndexPatch().Enable();
new get_SelectedScopeModePatch().Enable();
new get_IsAimingPatch().Enable();
//add configuration slider for field of view
configFOV = Config.Bind("General", "FOV", 75, new ConfigDescription("In-game Field of View value", new AcceptableValueRange<int>(51, 75)));
//add coefficient slider
configCoeff = Config.Bind("General", "Coefficient", 133, new ConfigDescription("Coefficient - increases sensitivity at higher zoom levels", new AcceptableValueRange<int>(1, 300)));
//add sensitivity slider
configSens = Config.Bind("General", "Sensitivity", 25, new ConfigDescription("Sensitivity while aiming", new AcceptableValueRange<int>(1, 200)));
//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."));
}
2022-06-22 22:19:14 +02:00
void FixedUpdate()
{
//FixedUpdate() at 50Hz (Unity default) tickrate appears to resolve the issue of this script breaking when AI spawns.
2022-08-02 11:12:35 +02:00
Time.fixedDeltaTime = (1f / 50f);
if (isAiming)
{
//Grab FOV values for calculation
2022-08-02 11:12:35 +02:00
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
2022-06-22 18:35:54 +02:00
//Figure out if the FPSCamera is zoomed in, prevents the script from ticking while the player is healing
2022-08-02 11:12:35 +02:00
if (FPSCameraFOV < configFOV.Value)
{
DetermineCurrentFOV();
//do not update sensitivity if currentFOV hasn't changed
if (cachedFOV != currentFOV) { mySens = CalculateSensitivity(currentFOV, configFOV.Value); cachedFOV = currentFOV; }
2022-08-02 11:12:35 +02:00
}
}
}
}
}