245 lines
10 KiB
C#
Raw Normal View History

2022-09-19 22:19:21 +02:00
using BepInEx;
using BepInEx.Configuration;
using Comfort.Common;
using EFT;
using System;
using System.Collections;
using UnityEngine;
namespace notGreg.UniformAim
{
[BepInPlugin("com.notGreg.UniformAim", "notGreg's Uniform Aim for Tarkov", "3.3.0")]
2022-09-19 22:19:21 +02:00
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> configExponent;
ConfigEntry<float> configSens;
2022-09-19 22:19:21 +02:00
void Awake()
{
configExponent = Config.Bind("General", "Coefficient", 133, new ConfigDescription("", new AcceptableValueRange<int>(10, 200)));
configSens = Config.Bind("General", "Sensitivity", 1.0f, new ConfigDescription("", new AcceptableValueRange<float>(0.01f, 2.0f)));
2022-09-19 22:19:21 +02:00
new get_AimingSensitivityPatch().Enable();
}
Player mainPlayer;
int inGameFOV;
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.
2022-09-19 22:19:21 +02:00
void Update()
{
//check if the world instance exists
2022-09-19 22:19:21 +02:00
//Logger.LogInfo("Checking world instance");
if (Singleton<AbstractGame>.Instance == null)
{
return;
}
//grab the game status of the existing instance
2022-09-19 22:19:21 +02:00
//Logger.LogInfo("Checking game status");
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)
2022-09-19 22:19:21 +02:00
//Logger.LogInfo("Checking GameStatus and isAiming");
if (currentGameStatus == GameStatus.Started && mainPlayer != null)
{
//Logger.LogInfo($"isAiming? {mainPlayer.HandsController.IsAiming}");
if (mainPlayer.HandsController.IsAiming)
{
aimingSens = calculateSensitivity();
}
return;
}
//Logger.LogInfo("Switch on GameStatus");
switch (currentGameStatus)
{
case GameStatus.Started:
{
mainPlayer = getLocalPlayer();
//Logger.LogInfo($"Subscribing to onAimingChanged event");
subscribeOnAimingChanged();
//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.
2022-09-19 22:19:21 +02:00
int getInGameFOV()
{
//int fov = Singleton<GClass1642>.Instance.Game.Settings.FieldOfView; //SPT-AKI 3.2.3
int fov = Singleton<GClass1653>.Instance.Game.Settings.FieldOfView; //SPT-AKI 3.3.0
//Logger.LogInfo($"In-game FOV: {fov}");
return fov;
2022-09-19 22:19:21 +02:00
}
//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.
2022-09-19 22:19:21 +02:00
float getInGameAimSens()
{
//float sens = Singleton<GClass1642>.Instance.Control.Settings.MouseAimingSensitivity; //SPT-AKI 3.2.*
float sens = Singleton<GClass1653>.Instance.Control.Settings.MouseAimingSensitivity; //SPT-AKI 3.3.0
//Logger.LogInfo($"In-game AimSens: {sens}");
return sens;
2022-09-19 22:19:21 +02:00
}
Player getLocalPlayer()
{
return Singleton<GameWorld>.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer);
}
WaitForSecondsRealtime myDelay = new WaitForSecondsRealtime(1f);
Camera mainCamera;
Camera scopeCamera;
//this coroutine attempts to find the FPS Camera in the scene.
2022-09-19 22:19:21 +02:00
IEnumerator tryGetMainCamera()
{
string cameraName = "FPS Camera";
if (GameObject.Find(cameraName) != null)
{
mainCamera = GameObject.Find(cameraName).GetComponent<Camera>();
//Logger.LogInfo($"{mainCamera.name} found!");
}
else
{
//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. The state of this camera is used to determine whether the player is using a magnified optic or not.
2022-09-19 22:19:21 +02:00
IEnumerator tryGetScopeCamera()
{
string cameraName = "BaseOpticCamera(Clone)";
if (GameObject.Find(cameraName) != null)
{
scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>();
//Logger.LogInfo($"{scopeCamera.name} found!");
}
yield break;
}
//convert vertical FOV (degrees) to horizontal FOV (degrees)
2022-09-19 22:19:21 +02:00
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.
2022-09-19 22:19:21 +02:00
Camera determineCurrentAimedFOV()
{
var currentAimingMode = mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod;
2022-09-19 22:19:21 +02:00
//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
2022-09-19 22:19:21 +02:00
switch (scopeName)
{
case "tactical_mp155_kalashnikov_ultima_camera":
2022-09-19 22:19:21 +02:00
{
//Logger.LogInfo("MP-155 Thermal");
return mainCamera;
}
//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":
2022-09-19 22:19:21 +02:00
{
if (scopeMode == 0)
{
//Logger.LogInfo($"Leupold D-EVO BUIS FOV: {mainCamera.fieldOfView}");
return mainCamera;
}
else
{
//Logger.LogInfo($"Leupold D-EVO Scope FOV: {scopeCamera.fieldOfView}");
return scopeCamera;
}
}
default:
{
if (scopeCamera == null || scopeCamera.isActiveAndEnabled == false)
{
//Logger.LogInfo($"Non-magnified: {scopeName} FOV: {mainCamera.fieldOfView}");
return mainCamera;
}
else
{
//Logger.LogInfo($"Magnified: {scopeName} FOV: {scopeCamera.fieldOfView}");
return scopeCamera;
}
}
}
}
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
2022-09-19 22:19:21 +02:00
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
2022-09-19 22:19:21 +02:00
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
2022-09-19 22:19:21 +02:00
float exponent = 100f / configExponent.Value;
float tanRatio = (float)(Mathf.Tan(aimedFOV / 2) / Mathf.Tan(hipFOV / 2));
float sensitivity = (float)Math.Pow(tanRatio, exponent) * inGameAimedSens;
2022-09-19 22:19:21 +02:00
//Logger.LogInfo($"Sensitivity: {sensitivity}");
return sensitivity * configSens.Value;
2022-09-19 22:19:21 +02:00
}
2022-09-19 22:19:21 +02:00
void subscribeOnAimingChanged()
{
//HandsChangedEvent triggers whenever player changes weapons
//without it the patch would cease to work as expected when weapons were changed
2022-09-19 22:19:21 +02:00
mainPlayer.HandsChangedEvent += (handsArgs) =>
{
//onAimingChanged triggers whenever the player starts or stops aiming.
2022-09-19 22:19:21 +02:00
mainPlayer.HandsController.OnAimingChanged += (aimArgs) =>
{
inGameFOV = getInGameFOV();
inGameAimedSens = getInGameAimSens();
StartCoroutine(tryGetScopeCamera());
};
};
}
}
}