222 lines
9.1 KiB
C#

using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using Comfort.Common;
using EFT;
using System;
using System.Collections;
using UnityEngine;
// DLL dependencies needed to update the Uniform Aim Mod for newer versions of the game
//%tarkovdir%\BepInEx\core\BepInEx.dll
//%tarkovdir%\EscapeFromTarkov_Data\Managed\Assembly-CSharp.dll
//%tarkovdir%\EscapeFromTarkov_Data\Managed\Aki.Reflection.dll
//%tarkovdir%\EscapeFromTarkov_Data\Managed\Comfort.dll
//%tarkovdir%\EscapeFromTarkov_Data\Managed\UnityEngine.dll
//%tarkovdir%\EscapeFromTarkov_Data\Managed\UnityEngine.CoreModule.dll
namespace notGreg.UniformAim
{
[BepInPlugin("com.notGreg.UniformAim", "notGreg's Uniform Aim for Tarkov", "3.8.0")]
[BepInDependency("RealismMod", BepInDependency.DependencyFlags.SoftDependency)]
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> configExponent;
ConfigEntry<int> configSens;
ConfigEntry<bool> enableDebug;
public static bool isRealismModPresent = Chainloader.PluginInfos.ContainsKey("RealismMod");
void Awake()
{
configExponent = Config.Bind("General", "Coefficient", 133, new ConfigDescription("", new AcceptableValueRange<int>(10, 500)));
//configSens = Config.Bind("General", "Sensitivity", 1.0f, new ConfigDescription("", new AcceptableValueRange<float>(0.01f, 2.0f))); // an old float-based sensitivity for future reference
configSens = Config.Bind("General", "Sensitivity", 100, new ConfigDescription("", new AcceptableValueRange<int>(10, 200)));
enableDebug = Config.Bind("Debug", "Enable debug logging", false);
if (!isRealismModPresent)
{
new get_AimingSensitivityPatch().Enable();
}
else
{
if(enableDebug.Value) Logger.LogInfo("RealismMod detected! Abandoning aimingSens patch...\nMake sure to use the compatibility plugin!");
}
}
Player mainPlayer;
int inGameFOV;
float inGameAimedSens;
public static float aimingSens = 1.0f;
//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 FixedUpdate()
{
//check if the world instance exists
if (Singleton<AbstractGame>.Instance == null)
{
return;
}
//grab the game status of the existing instance
GameStatus currentGameStatus = Singleton<AbstractGame>.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)
if (currentGameStatus == GameStatus.Started && mainPlayer != null)
{
if (mainPlayer.HandsController.IsAiming)
{
aimingSens = calculateSensitivity();
}
return;
}
if(enableDebug.Value) Logger.LogInfo("Switch on GameStatus");
switch (currentGameStatus)
{
case GameStatus.Started:
{
mainPlayer = getLocalPlayer();
if(enableDebug.Value) Logger.LogInfo($"Subscribing to onAimingChanged event");
subscribeOnAimingChanged();
if(enableDebug.Value) Logger.LogInfo("TryGetCameras coroutines");
StartCoroutine(tryGetMainCamera());
StartCoroutine(tryGetScopeCamera());
break;
}
case GameStatus.SoftStopping:
case GameStatus.Stopped:
{
mainPlayer = null;
break;
}
default:
{
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()
{
int fov = Singleton<SharedGameSettingsClass>.Instance.Game.Settings.FieldOfView;
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()
{
float sens = Singleton<SharedGameSettingsClass>.Instance.Control.Settings.MouseAimingSensitivity;
if(enableDebug.Value) Logger.LogInfo($"In-game AimSens: {sens}");
return sens;
}
Player getLocalPlayer()
{
if(enableDebug.Value) Logger.LogInfo("Setting local player...");
return Singleton<GameWorld>.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer) as Player;
}
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";
if (GameObject.Find(cameraName) != null)
{
mainCamera = GameObject.Find(cameraName).GetComponent<Camera>();
if (enableDebug.Value) Logger.LogInfo($"{mainCamera.name} found!");
}
else
{
if (enableDebug.Value) Logger.LogMessage($"Camera \"{cameraName}\" not found, rescheduling...");
yield return myDelay;
StartCoroutine(tryGetMainCamera());
yield break;
}
yield return null;
}
//this coroutine attempts to find existing baseOpticCamera in the scene.
IEnumerator tryGetScopeCamera()
{
string cameraName = "BaseOpticCamera(Clone)";
if (GameObject.Find(cameraName) != null)
{
scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>();
if(enableDebug.Value) Logger.LogInfo($"{scopeCamera.name} found!");
}
yield break;
}
//figure out whether the player is using a magnified optic or not. Return the camera of the sight.
float determineCurrentAimedFOV()
{
if(enableDebug.Value)
{
Logger.LogInfo($"Current scope: {mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.Item.LocalizedName()} isOptic? {mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic}");
}
if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic)
{
return scopeCamera.fieldOfView;
}
return mainCamera.fieldOfView;
}
float calculateSensitivity()
{
if(enableDebug.Value) Logger.LogInfo("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 * Camera.VerticalToHorizontalFieldOfView(determineCurrentAimedFOV(), mainCamera.aspect);
//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) * inGameAimedSens;
if(enableDebug.Value) Logger.LogInfo($"Sensitivity: {sensitivity}");
return sensitivity * (configSens.Value / 100.0f);
}
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) =>
{
//onAimingChanged triggers whenever the player starts or stops aiming.
mainPlayer.HandsController.OnAimingChanged += (aimArgs) =>
{
if (enableDebug.Value) Logger.LogInfo($"Scope: {mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.Item.LocalizedName()} isOptic? {mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic}");
inGameFOV = getInGameFOV();
inGameAimedSens = getInGameAimSens();
StartCoroutine(tryGetScopeCamera());
};
};
}
}
}