From 3e8afda0e9386671afd8733c2ec807af75d0029c Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 10:47:16 -0400 Subject: [PATCH 01/31] nameplate improvements --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 179 ++++++++++++++++--------- Fika.Core/FikaPlugin.cs | 49 +++++-- 2 files changed, 156 insertions(+), 72 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 1b8cca51..f0eac703 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -43,29 +43,31 @@ protected void Update() playerPlate.gameObject.SetActive(true); } UpdateScreenSpacePosition(); - float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; - float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; - if (FikaPlugin.UseHealthNumber.Value) - { - if (!playerPlate.healthNumberBackgroundScreen.gameObject.activeSelf) + if (!FikaPlugin.HideHealthBar.Value) { + float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; + float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; + if (FikaPlugin.UseHealthNumber.Value) { - playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(true); - playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); + if (!playerPlate.healthNumberBackgroundScreen.gameObject.activeSelf) + { + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(true); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); + } + int healthNumberPercentage = (int)Math.Round((currentHealth / maxHealth) * 100); + playerPlate.SetHealthNumberText($"{healthNumberPercentage}%"); } - int healthNumberPercentage = (int)Math.Round((currentHealth / maxHealth) * 100); - playerPlate.SetHealthNumberText($"{healthNumberPercentage}%"); - } - else - { - if (!playerPlate.healthBarBackgroundScreen.gameObject.active) + else { - playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); - playerPlate.healthBarBackgroundScreen.gameObject.SetActive(true); - } + if (!playerPlate.healthBarBackgroundScreen.gameObject.active) + { + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(true); + } - float normalizedHealth = Mathf.Clamp01(currentHealth / maxHealth); - playerPlate.healthBarScreen.fillAmount = normalizedHealth; - UpdateHealthBarColor(normalizedHealth); + float normalizedHealth = Mathf.Clamp01(currentHealth / maxHealth); + playerPlate.healthBarScreen.fillAmount = normalizedHealth; + UpdateHealthBarColor(normalizedHealth); + } } if (!currentPlayer.HealthController.IsAlive) { @@ -80,20 +82,43 @@ protected void Update() private void UpdateScreenSpacePosition() { + Camera camera = CameraClass.Instance.Camera; + + float opacityMultiplier = 1f; if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) { + // Scope scaling and positioning if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) { - playerPlate.ScalarObjectScreen.active = false; - return; + if (FikaPlugin.HideNamePlateInOptic.Value) { + playerPlate.ScalarObjectScreen.active = false; + return; + } } + // Opacity in ADS + opacityMultiplier = FikaPlugin.OpacityInADS.Value; + } + + float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; + float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); + if (sqrDistance > maxDistanceToShow) + { + // Disable the nameplate if the player is too far away + playerPlate.ScalarObjectScreen.active = false; + return; } - else if (playerPlate.ScalarObjectScreen.active == false) + + if (playerPlate.ScalarObjectScreen.active == false) { playerPlate.ScalarObjectScreen.active = true; } - Camera camera = CameraClass.Instance.Camera; + float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); + Vector3 position; + + position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + + Vector3 screenPoint = camera.WorldToScreenPoint(position); if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) { @@ -102,55 +127,62 @@ private void UpdateScreenSpacePosition() screenScale = outputWidth / inputWidth; } - float distance = Vector3.Distance(CameraClass.Instance.Camera.transform.position, currentPlayer.Position) / 25; - distance = Mathf.Clamp(distance, 0.6f, 1f); - Vector3 position; - - position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * distance), currentPlayer.PlayerBones.Neck.position.z); - - Vector3 screenPoint = camera.WorldToScreenPoint(position); - if (screenPoint.z > 0) { playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / distance) * FikaPlugin.NamePlateScale.Value; - - float distanceToCenter = Vector3.Distance(screenPoint, new Vector3(Screen.width, Screen.height, 0) / 2); - #region Alpha Control for Health Bars. This code is ugly so its getting regioned so I can hide my shame. - if (distanceToCenter < 200) + // Less opaque when not looking at the player + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) { - playerPlate.playerNameScreen.color = new Color(playerPlate.playerNameScreen.color.r, playerPlate.playerNameScreen.color.g, playerPlate.playerNameScreen.color.b, Mathf.Max(0.1f, distanceToCenter / 200)); - playerPlate.healthBarScreen.color = new Color(playerPlate.healthBarScreen.color.r, playerPlate.healthBarScreen.color.g, playerPlate.healthBarScreen.color.b, Mathf.Max(0.1f, distanceToCenter / 200)); - playerPlate.healthBarBackgroundScreen.color = new Color(playerPlate.healthBarBackgroundScreen.color.r, playerPlate.healthBarBackgroundScreen.color.g, playerPlate.healthBarBackgroundScreen.color.b, Mathf.Clamp(Mathf.Max(0.1f, distanceToCenter / 200), 0f, 0.4392157f)); - playerPlate.healthNumberBackgroundScreen.color = new Color(playerPlate.healthNumberBackgroundScreen.color.r, playerPlate.healthNumberBackgroundScreen.color.g, playerPlate.healthNumberBackgroundScreen.color.b, Mathf.Clamp(Mathf.Max(0.1f, distanceToCenter / 200), 0f, 0.4392157f)); - playerPlate.healthNumberScreen.color = new Color(playerPlate.healthNumberScreen.color.r, playerPlate.healthNumberScreen.color.g, playerPlate.healthNumberScreen.color.b, Mathf.Max(0.1f, distanceToCenter / 200)); - playerPlate.usecPlateScreen.color = new Color(playerPlate.usecPlateScreen.color.r, playerPlate.usecPlateScreen.color.g, playerPlate.usecPlateScreen.color.b, Mathf.Max(0.1f, distanceToCenter / 200)); - playerPlate.bearPlateScreen.color = new Color(playerPlate.bearPlateScreen.color.r, playerPlate.bearPlateScreen.color.g, playerPlate.bearPlateScreen.color.b, Mathf.Max(0.1f, distanceToCenter / 200)); - + Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0); + float sqrDistFromCenter = (screenCenter - screenPoint).sqrMagnitude; + float maxSqrDistFromCenter = Mathf.Pow(Mathf.Min(Screen.width, Screen.height) / 2, 2); + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); } - else + + float alpha = 1f; + float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; + float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); + alpha = Mathf.Lerp(alpha, 0, lerpValue); + namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + + // Setting the nameplate scale + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + + // Setting the overall nameplate alpha + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + + float healthAlphaMultiplier = 1f; + if (FikaPlugin.HideHealthBar.Value) { - playerPlate.playerNameScreen.color = new Color(playerPlate.playerNameScreen.color.r, playerPlate.playerNameScreen.color.g, playerPlate.playerNameScreen.color.b, 1); - playerPlate.healthBarScreen.color = new Color(playerPlate.healthBarScreen.color.r, playerPlate.healthBarScreen.color.g, playerPlate.healthBarScreen.color.b, 1); - playerPlate.healthBarBackgroundScreen.color = new Color(playerPlate.healthBarBackgroundScreen.color.r, playerPlate.healthBarBackgroundScreen.color.g, playerPlate.healthBarBackgroundScreen.color.b, 1); - playerPlate.healthNumberBackgroundScreen.color = new Color(playerPlate.healthNumberBackgroundScreen.color.r, playerPlate.healthNumberBackgroundScreen.color.g, playerPlate.healthNumberBackgroundScreen.color.b, 1); - playerPlate.healthNumberScreen.color = new Color(playerPlate.healthNumberScreen.color.r, playerPlate.healthNumberScreen.color.g, playerPlate.healthNumberScreen.color.b, 1); - playerPlate.usecPlateScreen.color = new Color(playerPlate.usecPlateScreen.color.r, playerPlate.usecPlateScreen.color.g, playerPlate.usecPlateScreen.color.b, 1); - playerPlate.bearPlateScreen.color = new Color(playerPlate.bearPlateScreen.color.r, playerPlate.bearPlateScreen.color.g, playerPlate.bearPlateScreen.color.b, 1); + healthAlphaMultiplier = 0; } + + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); } else { - playerPlate.playerNameScreen.color = new Color(playerPlate.playerNameScreen.color.r, playerPlate.playerNameScreen.color.g, playerPlate.playerNameScreen.color.b, 0); - playerPlate.healthBarScreen.color = new Color(playerPlate.healthBarScreen.color.r, playerPlate.healthBarScreen.color.g, playerPlate.healthBarScreen.color.b, 0); - playerPlate.healthBarBackgroundScreen.color = new Color(playerPlate.healthBarBackgroundScreen.color.r, playerPlate.healthBarBackgroundScreen.color.g, playerPlate.healthBarBackgroundScreen.color.b, 0); - playerPlate.healthNumberBackgroundScreen.color = new Color(playerPlate.healthNumberBackgroundScreen.color.r, playerPlate.healthNumberBackgroundScreen.color.g, playerPlate.healthNumberBackgroundScreen.color.b, 0); - playerPlate.healthNumberScreen.color = new Color(playerPlate.healthNumberScreen.color.r, playerPlate.healthNumberScreen.color.g, playerPlate.healthNumberScreen.color.b, 0); - playerPlate.usecPlateScreen.color = new Color(playerPlate.usecPlateScreen.color.r, playerPlate.usecPlateScreen.color.g, playerPlate.usecPlateScreen.color.b, 0); - playerPlate.bearPlateScreen.color = new Color(playerPlate.bearPlateScreen.color.r, playerPlate.bearPlateScreen.color.g, playerPlate.bearPlateScreen.color.b, 0); + // Hide the nameplate if the player is behind the camera + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); + UpdateColorImage(playerPlate.healthBarScreen, 0); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); + UpdateColorImage(playerPlate.usecPlateScreen, 0); + UpdateColorImage(playerPlate.bearPlateScreen, 0); } - #endregion } private void CreateHealthBar() @@ -172,6 +204,13 @@ private void CreateHealthBar() playerPlate.bearPlateScreen.gameObject.SetActive(true); } } + if (FikaPlugin.HideHealthBar.Value) + { + playerPlate.healthBarScreen.gameObject.SetActive(false); + playerPlate.healthNumberScreen.gameObject.SetActive(false); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); + } if (FikaPlugin.DevelopersList.ContainsKey(currentPlayer.Profile.Nickname.ToLower())) { playerPlate.playerNameScreen.color = new Color(0, 0.6091f, 1, 1); @@ -200,6 +239,26 @@ private void UpdateHealthBarColor(float normalizedHealth) playerPlate.healthBarScreen.color = color; } + private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) + { + if (screenObject.gameObject.activeInHierarchy) + { + var color = screenObject.color; + color.a = alpha; + screenObject.color = color; + } + } + + private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, float alpha) + { + if (screenObject.gameObject.activeInHierarchy) + { + var color = screenObject.color; + color.a = alpha; + screenObject.color = color; + } + } + private void OnDestroy() { playerPlate.gameObject.SetActive(false); diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index 26d8c3cf..c81132e6 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -106,11 +106,20 @@ public class FikaPlugin : BaseUnityPlugin public static ConfigEntry FasterInventoryScrollSpeed { get; set; } public static ConfigEntry ExtractKey { get; set; } - // Coop | Custom + // Coop | NamePlates public static ConfigEntry UseNamePlates { get; set; } + public static ConfigEntry HideHealthBar { get; set; } public static ConfigEntry UseHealthNumber { get; set; } public static ConfigEntry UsePlateFactionSide { get; set; } + public static ConfigEntry HideNamePlateInOptic { get; set; } + public static ConfigEntry DecreaseOpacityNotLookingAt { get; set; } public static ConfigEntry NamePlateScale { get; set; } + public static ConfigEntry OpacityInADS { get; set; } + public static ConfigEntry MaxDistanceToShow { get; set; } + public static ConfigEntry MinimumOpacity { get; set; } + public static ConfigEntry MinimumNamePlateScale { get; set; } + + // Coop | Custom public static ConfigEntry UsePingSystem { get; set; } public static ConfigEntry PingButton { get; set; } public static ConfigEntry PingColor { get; set; } @@ -121,7 +130,7 @@ public class FikaPlugin : BaseUnityPlugin // Coop | Debug public static ConfigEntry FreeCamButton { get; set; } - // Performance + // Performance public static ConfigEntry EnforcedSpawnLimits { get; set; } public static ConfigEntry DespawnFurthest { get; set; } public static ConfigEntry DynamicAI { get; set; } @@ -130,7 +139,7 @@ public class FikaPlugin : BaseUnityPlugin public static ConfigEntry CullPlayers { get; set; } public static ConfigEntry CullingRange { get; set; } - // Bot Limits + // Bot Limits public static ConfigEntry MaxBotsFactory { get; set; } public static ConfigEntry MaxBotsCustoms { get; set; } public static ConfigEntry MaxBotsInterchange { get; set; } @@ -253,17 +262,33 @@ private void SetupConfig() FasterInventoryScrollSpeed = Config.Bind("Coop", "Faster Inventory Scroll Speed", 63, new ConfigDescription("The speed at which the inventory scrolls at. Default is 63.", new AcceptableValueRange(63, 500), new ConfigurationManagerAttributes() { Order = 2 })); ExtractKey = Config.Bind("Coop", "Extract Key", new KeyboardShortcut(KeyCode.F8), new ConfigDescription("The key used to extract from the raid.", tags: new ConfigurationManagerAttributes() { Order = 1 })); - + + // Coop | NamePlates + + UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); + + HideHealthBar = Config.Bind("Coop | NamePlates", "Hide Health Bar", false, new ConfigDescription("Completely hides the health bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); + + UseHealthNumber = Config.Bind("Coop | NamePlates", "Show HP% instead of bar", false, new ConfigDescription("Shows health in % amount instead of using the bar.", tags: new ConfigurationManagerAttributes() { Order = 8 })); + + UsePlateFactionSide = Config.Bind("Coop | NamePlates", "Show Player Faction Icon", true, new ConfigDescription("Shows the player faction icon next to the HP bar.", tags: new ConfigurationManagerAttributes() { Order = 7 })); + + HideNamePlateInOptic = Config.Bind("Coop | NamePlates", "Hide Name Plate in Optic", true, new ConfigDescription("Hides the name plate when viewing through PiP scopes since it's kinda janky.", tags: new ConfigurationManagerAttributes() { Order = 6 })); + + DecreaseOpacityNotLookingAt = Config.Bind("Coop | NamePlates", "Decrease Opacity In Peripheral", true, new ConfigDescription("Decreases the opacity of the name plates when not looking at a player.", tags: new ConfigurationManagerAttributes() { Order = 5 })); + + NamePlateScale = Config.Bind("Coop | NamePlates", "Name Plate Scale", 0.22f, new ConfigDescription("Size of the name plates", new AcceptableValueRange(0.05f, 1f), new ConfigurationManagerAttributes() { Order = 4 })); + + OpacityInADS = Config.Bind("Coop | NamePlates", "Opacity in ADS", 0.75f, new ConfigDescription("The opacity of the name plates when aiming down sights.", new AcceptableValueRange(0.1f, 1f), new ConfigurationManagerAttributes() { Order = 3 })); + + MaxDistanceToShow = Config.Bind("Coop | NamePlates", "Max Distance to Show", 500f, new ConfigDescription("The maximum distance at which name plates will become invisible, starts to fade at half the input value.", new AcceptableValueRange(10f, 1000f), new ConfigurationManagerAttributes() { Order = 2 })); + + MinimumOpacity = Config.Bind("Coop | NamePlates", "Minimum Opacity", 0.1f, new ConfigDescription("The minimum opacity of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 1 })); + + MinimumNamePlateScale = Config.Bind("Coop | NamePlates", "Minimum Name Plate Scale", 0.01f, new ConfigDescription("The minimum scale of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 0 })); + // Coop | Custom - UseNamePlates = Config.Bind("Coop | Custom", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); - - UseHealthNumber = Config.Bind("Coop | Custom", "Show HP% instead of bar", false, new ConfigDescription("Shows health in % amount instead of using the bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); - - UsePlateFactionSide = Config.Bind("Coop | Custom", "Show Player Faction Icon", true, new ConfigDescription("Shows the player faction icon next to the HP bar.", tags: new ConfigurationManagerAttributes() { Order = 8 })); - - NamePlateScale = Config.Bind("Coop | Custom", "Name Plate Scale", 0.22f, new ConfigDescription("Size of the name plates", new AcceptableValueRange(0.05f, 1f), new ConfigurationManagerAttributes() { Order = 7 })); - UsePingSystem = Config.Bind("Coop | Custom", "Ping System", false, new ConfigDescription("Toggle Ping System. If enabled you can receive and send pings by pressing the ping key.", tags: new ConfigurationManagerAttributes() { Order = 6 })); PingButton = Config.Bind("Coop | Custom", "Ping Button", new KeyboardShortcut(KeyCode.U), new ConfigDescription("Button used to send pings.", tags: new ConfigurationManagerAttributes() { Order = 5 })); From 369bbca114728a26c868943fee0746d67fc68233 Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 15:23:32 -0400 Subject: [PATCH 02/31] peripheral checks now properly handle up/down scaling --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 121 +++++++++++-------------- 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index f0eac703..a8c87c2a 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -82,99 +82,40 @@ protected void Update() private void UpdateScreenSpacePosition() { - Camera camera = CameraClass.Instance.Camera; - float opacityMultiplier = 1f; if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) { - // Scope scaling and positioning - if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) + if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic && FikaPlugin.HideNamePlateInOptic.Value) { - if (FikaPlugin.HideNamePlateInOptic.Value) { - playerPlate.ScalarObjectScreen.active = false; - return; - } + playerPlate.ScalarObjectScreen.active = false; + return; } - // Opacity in ADS opacityMultiplier = FikaPlugin.OpacityInADS.Value; } + Camera camera = CameraClass.Instance.Camera; float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); if (sqrDistance > maxDistanceToShow) { - // Disable the nameplate if the player is too far away playerPlate.ScalarObjectScreen.active = false; return; } - - if (playerPlate.ScalarObjectScreen.active == false) - { - playerPlate.ScalarObjectScreen.active = true; - } - - float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); - Vector3 position; - position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + playerPlate.ScalarObjectScreen.active = true; + float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); + Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); Vector3 screenPoint = camera.WorldToScreenPoint(position); - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) + SSAA ssaa = CameraClass.Instance.SSAA; + if (ssaa != null && ssaa.isActiveAndEnabled) { - int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); - float inputWidth = CameraClass.Instance.SSAA.GetInputWidth(); - screenScale = outputWidth / inputWidth; + screenScale = ssaa.GetOutputWidth() / ssaa.GetInputWidth(); } - if (screenPoint.z > 0) - { - playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - - // Less opaque when not looking at the player - float distFromCenterMultiplier = 1f; - if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) - { - Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0); - float sqrDistFromCenter = (screenCenter - screenPoint).sqrMagnitude; - float maxSqrDistFromCenter = Mathf.Pow(Mathf.Min(Screen.width, Screen.height) / 2, 2); - distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); - } - - float alpha = 1f; - float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; - float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); - alpha = Mathf.Lerp(alpha, 0, lerpValue); - namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); - namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); - - // Setting the nameplate scale - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; - - // Setting the overall nameplate alpha - alpha *= opacityMultiplier; - alpha *= distFromCenterMultiplier; - alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); - - float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); - - float healthAlphaMultiplier = 1f; - if (FikaPlugin.HideHealthBar.Value) - { - healthAlphaMultiplier = 0; - } - - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); - UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.usecPlateScreen, alpha); - UpdateColorImage(playerPlate.bearPlateScreen, alpha); - } - else + if (screenPoint.z <= 0) { - // Hide the nameplate if the player is behind the camera UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); UpdateColorImage(playerPlate.healthBarScreen, 0); UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); @@ -182,7 +123,47 @@ private void UpdateScreenSpacePosition() UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); UpdateColorImage(playerPlate.usecPlateScreen, 0); UpdateColorImage(playerPlate.bearPlateScreen, 0); + return; } + + playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; + + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) + { + float screenWidth = ssaa != null && ssaa.isActiveAndEnabled ? ssaa.GetOutputWidth() : Screen.width; + float screenHeight = ssaa != null && ssaa.isActiveAndEnabled ? ssaa.GetOutputHeight() : Screen.height; + Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); + Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; + float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; + float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; + float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); + } + + float alpha = 1f; + float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; + float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); + alpha = Mathf.Lerp(alpha, 0, lerpValue); + namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + float healthAlphaMultiplier = FikaPlugin.HideHealthBar.Value ? 0 : 1f; + + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); } private void CreateHealthBar() From 144d9cd60d0175438b43d1bd39fe23dc21ecea3b Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 15:24:24 -0400 Subject: [PATCH 03/31] actually reverting some performance things to make sure i have a functional version to fall back to --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 131 +++++++++++++++---------- 1 file changed, 80 insertions(+), 51 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index a8c87c2a..0562a262 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -82,40 +82,109 @@ protected void Update() private void UpdateScreenSpacePosition() { + Camera camera = CameraClass.Instance.Camera; + float opacityMultiplier = 1f; if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) { - if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic && FikaPlugin.HideNamePlateInOptic.Value) + // Scope scaling and positioning + if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) { - playerPlate.ScalarObjectScreen.active = false; - return; + if (FikaPlugin.HideNamePlateInOptic.Value) { + playerPlate.ScalarObjectScreen.active = false; + return; + } } + // Opacity in ADS opacityMultiplier = FikaPlugin.OpacityInADS.Value; } - Camera camera = CameraClass.Instance.Camera; float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); if (sqrDistance > maxDistanceToShow) { + // Disable the nameplate if the player is too far away playerPlate.ScalarObjectScreen.active = false; return; } - - playerPlate.ScalarObjectScreen.active = true; + + if (playerPlate.ScalarObjectScreen.active == false) + { + playerPlate.ScalarObjectScreen.active = true; + } float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); - Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + Vector3 position; + + position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + Vector3 screenPoint = camera.WorldToScreenPoint(position); - SSAA ssaa = CameraClass.Instance.SSAA; - if (ssaa != null && ssaa.isActiveAndEnabled) + if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) { - screenScale = ssaa.GetOutputWidth() / ssaa.GetInputWidth(); + int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); + float inputWidth = CameraClass.Instance.SSAA.GetInputWidth(); + screenScale = outputWidth / inputWidth; } - if (screenPoint.z <= 0) + if (screenPoint.z > 0) + { + playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; + + // Less opaque when not looking at the player + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) + { + float screenWidth = Screen.width; + float screenHeight = Screen.height; + if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) + { + screenWidth = CameraClass.Instance.SSAA.GetOutputWidth(); + screenHeight = CameraClass.Instance.SSAA.GetOutputHeight(); + } + Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); + Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; + float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; + float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; + float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; + + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); + } + + float alpha = 1f; + float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; + float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); + alpha = Mathf.Lerp(alpha, 0, lerpValue); + namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + + // Setting the nameplate scale + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + + // Setting the overall nameplate alpha + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + + float healthAlphaMultiplier = 1f; + if (FikaPlugin.HideHealthBar.Value) + { + healthAlphaMultiplier = 0; + } + + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); + } + else { + // Hide the nameplate if the player is behind the camera UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); UpdateColorImage(playerPlate.healthBarScreen, 0); UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); @@ -123,47 +192,7 @@ private void UpdateScreenSpacePosition() UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); UpdateColorImage(playerPlate.usecPlateScreen, 0); UpdateColorImage(playerPlate.bearPlateScreen, 0); - return; - } - - playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - - float distFromCenterMultiplier = 1f; - if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) - { - float screenWidth = ssaa != null && ssaa.isActiveAndEnabled ? ssaa.GetOutputWidth() : Screen.width; - float screenHeight = ssaa != null && ssaa.isActiveAndEnabled ? ssaa.GetOutputHeight() : Screen.height; - Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); - Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; - float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; - float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; - float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; - distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); } - - float alpha = 1f; - float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; - float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); - alpha = Mathf.Lerp(alpha, 0, lerpValue); - namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); - namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); - - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; - - alpha *= opacityMultiplier; - alpha *= distFromCenterMultiplier; - alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); - - float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); - float healthAlphaMultiplier = FikaPlugin.HideHealthBar.Value ? 0 : 1f; - - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); - UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.usecPlateScreen, alpha); - UpdateColorImage(playerPlate.bearPlateScreen, alpha); } private void CreateHealthBar() From f5bb82e3b04b5fe4da6072b846f2daae72b6fea1 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sat, 4 May 2024 22:38:13 +0200 Subject: [PATCH 04/31] Fix friendly fire not working not working with grenades --- Fika.Core/Coop/Players/CoopPlayer.cs | 8 ++++++++ Fika.Core/Fika.Core.csproj | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index be5e81c2..4543c04d 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -200,6 +200,14 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy { if (IsYourPlayer) { + if (damageInfo.Player != null) + { + CoopPlayer player = (CoopPlayer)damageInfo.Player.iPlayer; + if (!player.IsObservedAI && !FikaPlugin.Instance.FriendlyFire) + { + return; + } + } if (colliderType == EBodyPartColliderType.HeadCommon) { damageInfo.Damage *= FikaPlugin.HeadDamageMultiplier.Value; diff --git a/Fika.Core/Fika.Core.csproj b/Fika.Core/Fika.Core.csproj index 7d7b9d12..07689c06 100644 --- a/Fika.Core/Fika.Core.csproj +++ b/Fika.Core/Fika.Core.csproj @@ -60,7 +60,7 @@ False - ..\References\hollowed.dll + ..\References\Assembly-CSharp.dll False From 41fd2e7a62f86a505e21301b4659060aacfaa328 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sat, 4 May 2024 22:54:15 +0200 Subject: [PATCH 05/31] Fix reference --- Fika.Core/Fika.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fika.Core/Fika.Core.csproj b/Fika.Core/Fika.Core.csproj index 07689c06..7d7b9d12 100644 --- a/Fika.Core/Fika.Core.csproj +++ b/Fika.Core/Fika.Core.csproj @@ -60,7 +60,7 @@ False - ..\References\Assembly-CSharp.dll + ..\References\hollowed.dll False From 60f130169126ac793ef9a7b63886789ada64522d Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 17:07:48 -0400 Subject: [PATCH 06/31] cleanup/refactor + performance probably --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 151 +++++++++++-------------- Fika.sln | 10 +- 2 files changed, 70 insertions(+), 91 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 0562a262..39636f91 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -2,6 +2,7 @@ using Comfort.Common; using EFT; +using EFT.Animations; using EFT.UI; using Fika.Core.Bundles; using Fika.Core.Coop.Players; @@ -43,7 +44,8 @@ protected void Update() playerPlate.gameObject.SetActive(true); } UpdateScreenSpacePosition(); - if (!FikaPlugin.HideHealthBar.Value) { + if (!FikaPlugin.HideHealthBar.Value) + { float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; if (FikaPlugin.UseHealthNumber.Value) @@ -82,117 +84,94 @@ protected void Update() private void UpdateScreenSpacePosition() { - Camera camera = CameraClass.Instance.Camera; - float opacityMultiplier = 1f; - if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) + + ProceduralWeaponAnimation proceduralWeaponAnimation = mainPlayer.ProceduralWeaponAnimation; + if (mainPlayer.HealthController.IsAlive && proceduralWeaponAnimation.IsAiming) { - // Scope scaling and positioning - if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) + if (proceduralWeaponAnimation.CurrentScope.IsOptic && FikaPlugin.HideNamePlateInOptic.Value) { - if (FikaPlugin.HideNamePlateInOptic.Value) { - playerPlate.ScalarObjectScreen.active = false; - return; - } + playerPlate.ScalarObjectScreen.active = false; + return; } - // Opacity in ADS opacityMultiplier = FikaPlugin.OpacityInADS.Value; } + CameraClass cameraInstance = CameraClass.Instance; + Camera camera = cameraInstance.Camera; float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; - float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); + float maxDistanceToShow = FikaPlugin.MaxDistanceToShow.Value * FikaPlugin.MaxDistanceToShow.Value; if (sqrDistance > maxDistanceToShow) { - // Disable the nameplate if the player is too far away playerPlate.ScalarObjectScreen.active = false; return; } - - if (playerPlate.ScalarObjectScreen.active == false) - { - playerPlate.ScalarObjectScreen.active = true; - } - float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); - Vector3 position; - - position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + playerPlate.ScalarObjectScreen.active = true; + float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); + Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); Vector3 screenPoint = camera.WorldToScreenPoint(position); - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) + if (screenPoint.z <= 0) { - int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); - float inputWidth = CameraClass.Instance.SSAA.GetInputWidth(); - screenScale = outputWidth / inputWidth; + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); + UpdateColorImage(playerPlate.healthBarScreen, 0); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); + UpdateColorImage(playerPlate.usecPlateScreen, 0); + UpdateColorImage(playerPlate.bearPlateScreen, 0); + return; } - if (screenPoint.z > 0) + SSAA ssaa = cameraInstance.SSAA; + bool isSSAAEnabled = ssaa != null && ssaa.isActiveAndEnabled; + if (isSSAAEnabled) { - playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; + int outputWidth = ssaa.GetOutputWidth(); + float inputWidth = ssaa.GetInputWidth(); + screenScale = outputWidth / inputWidth; + } - // Less opaque when not looking at the player - float distFromCenterMultiplier = 1f; - if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) - { - float screenWidth = Screen.width; - float screenHeight = Screen.height; - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) - { - screenWidth = CameraClass.Instance.SSAA.GetOutputWidth(); - screenHeight = CameraClass.Instance.SSAA.GetOutputHeight(); - } - Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); - Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; - float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; - float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; - float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; - - distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); - } + playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - float alpha = 1f; - float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; - float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); - alpha = Mathf.Lerp(alpha, 0, lerpValue); - namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); - namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) + { + float screenWidth = isSSAAEnabled ? ssaa.GetOutputWidth() : Screen.width; + float screenHeight = isSSAAEnabled ? ssaa.GetOutputHeight() : Screen.height; + Vector3 screenCenter = new(screenWidth / 2, screenHeight / 2, 0); + Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; + float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; + float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; + float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); + } - // Setting the nameplate scale - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + float alpha = 1f; + float halfMaxDistanceToShow = maxDistanceToShow / 2; + float lerpValue = Mathf.Clamp01((sqrDistance - halfMaxDistanceToShow) / (halfMaxDistanceToShow)); + alpha = Mathf.LerpUnclamped(alpha, 0, lerpValue); + float namePlateScaleMult = Mathf.LerpUnclamped(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); - // Setting the overall nameplate alpha - alpha *= opacityMultiplier; - alpha *= distFromCenterMultiplier; - alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; - float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); - float healthAlphaMultiplier = 1f; - if (FikaPlugin.HideHealthBar.Value) - { - healthAlphaMultiplier = 0; - } + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + float healthAlphaMultiplier = FikaPlugin.HideHealthBar.Value ? 0 : 1f; - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); - UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.usecPlateScreen, alpha); - UpdateColorImage(playerPlate.bearPlateScreen, alpha); - } - else - { - // Hide the nameplate if the player is behind the camera - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); - UpdateColorImage(playerPlate.healthBarScreen, 0); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); - UpdateColorImage(playerPlate.usecPlateScreen, 0); - UpdateColorImage(playerPlate.bearPlateScreen, 0); - } + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); } private void CreateHealthBar() @@ -249,7 +228,7 @@ private void UpdateHealthBarColor(float normalizedHealth) playerPlate.healthBarScreen.color = color; } - private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) + private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) { @@ -258,7 +237,7 @@ private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) screenObject.color = color; } } - + private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) diff --git a/Fika.sln b/Fika.sln index ac0a030b..51ab95f4 100644 --- a/Fika.sln +++ b/Fika.sln @@ -8,16 +8,16 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU GoldMaster|Any CPU = GoldMaster|Any CPU + Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.Build.0 = Debug|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.ActiveCfg = Release|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.Build.0 = Release|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.ActiveCfg = Release|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.Build.0 = Release|Any CPU {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.ActiveCfg = GoldMaster|Any CPU {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.Build.0 = GoldMaster|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 0ebeefdb8644c10e9c98625e85caeea8885b67be Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 17:12:32 -0400 Subject: [PATCH 07/31] Revert "cleanup/refactor + performance probably" This reverts commit 60f130169126ac793ef9a7b63886789ada64522d. --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 151 ++++++++++++++----------- Fika.sln | 10 +- 2 files changed, 91 insertions(+), 70 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 39636f91..0562a262 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -2,7 +2,6 @@ using Comfort.Common; using EFT; -using EFT.Animations; using EFT.UI; using Fika.Core.Bundles; using Fika.Core.Coop.Players; @@ -44,8 +43,7 @@ protected void Update() playerPlate.gameObject.SetActive(true); } UpdateScreenSpacePosition(); - if (!FikaPlugin.HideHealthBar.Value) - { + if (!FikaPlugin.HideHealthBar.Value) { float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; if (FikaPlugin.UseHealthNumber.Value) @@ -84,94 +82,117 @@ protected void Update() private void UpdateScreenSpacePosition() { + Camera camera = CameraClass.Instance.Camera; + float opacityMultiplier = 1f; - - ProceduralWeaponAnimation proceduralWeaponAnimation = mainPlayer.ProceduralWeaponAnimation; - if (mainPlayer.HealthController.IsAlive && proceduralWeaponAnimation.IsAiming) + if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) { - if (proceduralWeaponAnimation.CurrentScope.IsOptic && FikaPlugin.HideNamePlateInOptic.Value) + // Scope scaling and positioning + if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) { - playerPlate.ScalarObjectScreen.active = false; - return; + if (FikaPlugin.HideNamePlateInOptic.Value) { + playerPlate.ScalarObjectScreen.active = false; + return; + } } + // Opacity in ADS opacityMultiplier = FikaPlugin.OpacityInADS.Value; } - CameraClass cameraInstance = CameraClass.Instance; - Camera camera = cameraInstance.Camera; float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; - float maxDistanceToShow = FikaPlugin.MaxDistanceToShow.Value * FikaPlugin.MaxDistanceToShow.Value; + float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); if (sqrDistance > maxDistanceToShow) { + // Disable the nameplate if the player is too far away playerPlate.ScalarObjectScreen.active = false; return; } - - playerPlate.ScalarObjectScreen.active = true; + + if (playerPlate.ScalarObjectScreen.active == false) + { + playerPlate.ScalarObjectScreen.active = true; + } float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); - Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + Vector3 position; + + position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + Vector3 screenPoint = camera.WorldToScreenPoint(position); - if (screenPoint.z <= 0) + if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) { - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); - UpdateColorImage(playerPlate.healthBarScreen, 0); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); - UpdateColorImage(playerPlate.usecPlateScreen, 0); - UpdateColorImage(playerPlate.bearPlateScreen, 0); - return; + int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); + float inputWidth = CameraClass.Instance.SSAA.GetInputWidth(); + screenScale = outputWidth / inputWidth; } - SSAA ssaa = cameraInstance.SSAA; - bool isSSAAEnabled = ssaa != null && ssaa.isActiveAndEnabled; - if (isSSAAEnabled) + if (screenPoint.z > 0) { - int outputWidth = ssaa.GetOutputWidth(); - float inputWidth = ssaa.GetInputWidth(); - screenScale = outputWidth / inputWidth; - } + playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; + // Less opaque when not looking at the player + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) + { + float screenWidth = Screen.width; + float screenHeight = Screen.height; + if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) + { + screenWidth = CameraClass.Instance.SSAA.GetOutputWidth(); + screenHeight = CameraClass.Instance.SSAA.GetOutputHeight(); + } + Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); + Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; + float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; + float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; + float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; + + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); + } - float distFromCenterMultiplier = 1f; - if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) - { - float screenWidth = isSSAAEnabled ? ssaa.GetOutputWidth() : Screen.width; - float screenHeight = isSSAAEnabled ? ssaa.GetOutputHeight() : Screen.height; - Vector3 screenCenter = new(screenWidth / 2, screenHeight / 2, 0); - Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; - float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; - float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; - float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; - distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); - } + float alpha = 1f; + float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; + float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); + alpha = Mathf.Lerp(alpha, 0, lerpValue); + namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); - float alpha = 1f; - float halfMaxDistanceToShow = maxDistanceToShow / 2; - float lerpValue = Mathf.Clamp01((sqrDistance - halfMaxDistanceToShow) / (halfMaxDistanceToShow)); - alpha = Mathf.LerpUnclamped(alpha, 0, lerpValue); - float namePlateScaleMult = Mathf.LerpUnclamped(1f, 0.5f, lerpValue); - namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + // Setting the nameplate scale + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + // Setting the overall nameplate alpha + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); - alpha *= opacityMultiplier; - alpha *= distFromCenterMultiplier; - alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); - float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); - float healthAlphaMultiplier = FikaPlugin.HideHealthBar.Value ? 0 : 1f; + float healthAlphaMultiplier = 1f; + if (FikaPlugin.HideHealthBar.Value) + { + healthAlphaMultiplier = 0; + } - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); - UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.usecPlateScreen, alpha); - UpdateColorImage(playerPlate.bearPlateScreen, alpha); + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); + } + else + { + // Hide the nameplate if the player is behind the camera + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); + UpdateColorImage(playerPlate.healthBarScreen, 0); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); + UpdateColorImage(playerPlate.usecPlateScreen, 0); + UpdateColorImage(playerPlate.bearPlateScreen, 0); + } } private void CreateHealthBar() @@ -228,7 +249,7 @@ private void UpdateHealthBarColor(float normalizedHealth) playerPlate.healthBarScreen.color = color; } - private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) + private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) { @@ -237,7 +258,7 @@ private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) screenObject.color = color; } } - + private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) diff --git a/Fika.sln b/Fika.sln index 51ab95f4..ac0a030b 100644 --- a/Fika.sln +++ b/Fika.sln @@ -8,16 +8,16 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - GoldMaster|Any CPU = GoldMaster|Any CPU Release|Any CPU = Release|Any CPU + GoldMaster|Any CPU = GoldMaster|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.ActiveCfg = Release|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.Build.0 = Release|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.ActiveCfg = GoldMaster|Any CPU - {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.Build.0 = GoldMaster|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.Debug|Any CPU.Build.0 = Debug|Any CPU {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.ActiveCfg = Release|Any CPU {79F0E889-A195-42B4-8656-4F35685BBB80}.Release|Any CPU.Build.0 = Release|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.ActiveCfg = GoldMaster|Any CPU + {79F0E889-A195-42B4-8656-4F35685BBB80}.GoldMaster|Any CPU.Build.0 = GoldMaster|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 16e918e10e188ae76100f46b70726b35ce136ffa Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sat, 4 May 2024 17:13:15 -0400 Subject: [PATCH 08/31] why are you like this git --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 151 +++++++++++-------------- 1 file changed, 65 insertions(+), 86 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 0562a262..39636f91 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -2,6 +2,7 @@ using Comfort.Common; using EFT; +using EFT.Animations; using EFT.UI; using Fika.Core.Bundles; using Fika.Core.Coop.Players; @@ -43,7 +44,8 @@ protected void Update() playerPlate.gameObject.SetActive(true); } UpdateScreenSpacePosition(); - if (!FikaPlugin.HideHealthBar.Value) { + if (!FikaPlugin.HideHealthBar.Value) + { float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; if (FikaPlugin.UseHealthNumber.Value) @@ -82,117 +84,94 @@ protected void Update() private void UpdateScreenSpacePosition() { - Camera camera = CameraClass.Instance.Camera; - float opacityMultiplier = 1f; - if (mainPlayer.HealthController.IsAlive && mainPlayer.ProceduralWeaponAnimation.IsAiming) + + ProceduralWeaponAnimation proceduralWeaponAnimation = mainPlayer.ProceduralWeaponAnimation; + if (mainPlayer.HealthController.IsAlive && proceduralWeaponAnimation.IsAiming) { - // Scope scaling and positioning - if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic) + if (proceduralWeaponAnimation.CurrentScope.IsOptic && FikaPlugin.HideNamePlateInOptic.Value) { - if (FikaPlugin.HideNamePlateInOptic.Value) { - playerPlate.ScalarObjectScreen.active = false; - return; - } + playerPlate.ScalarObjectScreen.active = false; + return; } - // Opacity in ADS opacityMultiplier = FikaPlugin.OpacityInADS.Value; } + CameraClass cameraInstance = CameraClass.Instance; + Camera camera = cameraInstance.Camera; float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; - float maxDistanceToShow = Mathf.Pow(FikaPlugin.MaxDistanceToShow.Value, 2); + float maxDistanceToShow = FikaPlugin.MaxDistanceToShow.Value * FikaPlugin.MaxDistanceToShow.Value; if (sqrDistance > maxDistanceToShow) { - // Disable the nameplate if the player is too far away playerPlate.ScalarObjectScreen.active = false; return; } - - if (playerPlate.ScalarObjectScreen.active == false) - { - playerPlate.ScalarObjectScreen.active = true; - } - float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); - Vector3 position; - - position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); + playerPlate.ScalarObjectScreen.active = true; + float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); + Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); Vector3 screenPoint = camera.WorldToScreenPoint(position); - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) + if (screenPoint.z <= 0) { - int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); - float inputWidth = CameraClass.Instance.SSAA.GetInputWidth(); - screenScale = outputWidth / inputWidth; + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); + UpdateColorImage(playerPlate.healthBarScreen, 0); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); + UpdateColorImage(playerPlate.usecPlateScreen, 0); + UpdateColorImage(playerPlate.bearPlateScreen, 0); + return; } - if (screenPoint.z > 0) + SSAA ssaa = cameraInstance.SSAA; + bool isSSAAEnabled = ssaa != null && ssaa.isActiveAndEnabled; + if (isSSAAEnabled) { - playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; + int outputWidth = ssaa.GetOutputWidth(); + float inputWidth = ssaa.GetInputWidth(); + screenScale = outputWidth / inputWidth; + } - // Less opaque when not looking at the player - float distFromCenterMultiplier = 1f; - if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) - { - float screenWidth = Screen.width; - float screenHeight = Screen.height; - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) - { - screenWidth = CameraClass.Instance.SSAA.GetOutputWidth(); - screenHeight = CameraClass.Instance.SSAA.GetOutputHeight(); - } - Vector3 screenCenter = new Vector3(screenWidth / 2, screenHeight / 2, 0); - Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; - float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; - float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; - float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; - - distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); - } + playerPlate.ScalarObjectScreen.transform.position = screenScale < 1 ? screenPoint : screenPoint * screenScale; - float alpha = 1f; - float namePlateScaleMult = FikaPlugin.NamePlateScale.Value; - float lerpValue = (sqrDistance - maxDistanceToShow / 2) / (maxDistanceToShow / 2); - alpha = Mathf.Lerp(alpha, 0, lerpValue); - namePlateScaleMult = Mathf.Lerp(1f, 0.5f, lerpValue); - namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); + float distFromCenterMultiplier = 1f; + if (FikaPlugin.DecreaseOpacityNotLookingAt.Value) + { + float screenWidth = isSSAAEnabled ? ssaa.GetOutputWidth() : Screen.width; + float screenHeight = isSSAAEnabled ? ssaa.GetOutputHeight() : Screen.height; + Vector3 screenCenter = new(screenWidth / 2, screenHeight / 2, 0); + Vector3 playerPosition = playerPlate.ScalarObjectScreen.transform.position; + float sqrDistFromCenter = (screenCenter - playerPosition).sqrMagnitude; + float minScreenSizeHalf = Mathf.Min(screenWidth, screenHeight) / 2; + float maxSqrDistFromCenter = minScreenSizeHalf * minScreenSizeHalf; + distFromCenterMultiplier = Mathf.Clamp01(1 - (sqrDistFromCenter / maxSqrDistFromCenter)); + } - // Setting the nameplate scale - playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; + float alpha = 1f; + float halfMaxDistanceToShow = maxDistanceToShow / 2; + float lerpValue = Mathf.Clamp01((sqrDistance - halfMaxDistanceToShow) / (halfMaxDistanceToShow)); + alpha = Mathf.LerpUnclamped(alpha, 0, lerpValue); + float namePlateScaleMult = Mathf.LerpUnclamped(1f, 0.5f, lerpValue); + namePlateScaleMult = Mathf.Clamp(namePlateScaleMult * FikaPlugin.NamePlateScale.Value, FikaPlugin.MinimumNamePlateScale.Value * FikaPlugin.NamePlateScale.Value, FikaPlugin.NamePlateScale.Value); - // Setting the overall nameplate alpha - alpha *= opacityMultiplier; - alpha *= distFromCenterMultiplier; - alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); + playerPlate.ScalarObjectScreen.transform.localScale = (Vector3.one / processedDistance) * namePlateScaleMult; - float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + alpha *= opacityMultiplier; + alpha *= distFromCenterMultiplier; + alpha = Mathf.Max(FikaPlugin.MinimumOpacity.Value, alpha); - float healthAlphaMultiplier = 1f; - if (FikaPlugin.HideHealthBar.Value) - { - healthAlphaMultiplier = 0; - } + float backgroundOpacity = Mathf.Clamp(alpha, 0f, 0.44f); + float healthAlphaMultiplier = FikaPlugin.HideHealthBar.Value ? 0 : 1f; - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); - UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); - UpdateColorImage(playerPlate.usecPlateScreen, alpha); - UpdateColorImage(playerPlate.bearPlateScreen, alpha); - } - else - { - // Hide the nameplate if the player is behind the camera - UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); - UpdateColorImage(playerPlate.healthBarScreen, 0); - UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, 0); - UpdateColorImage(playerPlate.healthBarBackgroundScreen, 0); - UpdateColorImage(playerPlate.healthNumberBackgroundScreen, 0); - UpdateColorImage(playerPlate.usecPlateScreen, 0); - UpdateColorImage(playerPlate.bearPlateScreen, 0); - } + UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, alpha); + UpdateColorImage(playerPlate.healthBarScreen, alpha * healthAlphaMultiplier); + UpdateColorTextMeshProUGUI(playerPlate.healthNumberScreen, alpha * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthBarBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.healthNumberBackgroundScreen, backgroundOpacity * healthAlphaMultiplier); + UpdateColorImage(playerPlate.usecPlateScreen, alpha); + UpdateColorImage(playerPlate.bearPlateScreen, alpha); } private void CreateHealthBar() @@ -249,7 +228,7 @@ private void UpdateHealthBarColor(float normalizedHealth) playerPlate.healthBarScreen.color = color; } - private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) + private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) { @@ -258,7 +237,7 @@ private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) screenObject.color = color; } } - + private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) From 210b183098ea884d19e7ddb6221f698824b97028 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sun, 5 May 2024 09:52:37 +0200 Subject: [PATCH 09/31] Fix friendly fire logic --- Fika.Core/Coop/Players/CoopPlayer.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index 4543c04d..b16b3450 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -202,8 +202,7 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy { if (damageInfo.Player != null) { - CoopPlayer player = (CoopPlayer)damageInfo.Player.iPlayer; - if (!player.IsObservedAI && !FikaPlugin.Instance.FriendlyFire) + if (!FikaPlugin.Instance.FriendlyFire && damageInfo.Player.iPlayer is ObservedCoopPlayer observedCoopPlayer && !observedCoopPlayer.IsObservedAI) { return; } From 04651fae15ffe7a5050884db69aa774e25affce3 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sun, 5 May 2024 09:52:57 +0200 Subject: [PATCH 10/31] Prevent crash from RaidLeave failure --- Fika.Core/Coop/GameMode/CoopGame.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 4dda819f..00eee33b 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -1374,8 +1374,15 @@ public override void Stop(string profileId, ExitStatus exitStatus, string exitNa wavesSpawnScenario_0?.Stop(); - PlayerLeftRequest body = new PlayerLeftRequest(myPlayer.ProfileId); - FikaRequestHandler.RaidLeave(body); + try + { + PlayerLeftRequest body = new(myPlayer.ProfileId); + FikaRequestHandler.RaidLeave(body); + } + catch (Exception) + { + FikaPlugin.Instance.FikaLogger.LogError("Unable to send RaidLeave request to server."); + } if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) { From 280d2589cc94b22e62b4ad2af71491db2cc97e87 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sun, 5 May 2024 09:53:39 +0200 Subject: [PATCH 11/31] Cleanup --- Fika.Core/Coop/Components/CoopHandler.cs | 2 +- Fika.Core/Coop/GameMode/CoopGame.cs | 1 - Fika.Core/Coop/Players/CoopPlayer.cs | 4 ++-- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 2 +- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 1 - Fika.Core/UI/Patches/ItemContextPatch.cs | 1 - 6 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Fika.Core/Coop/Components/CoopHandler.cs b/Fika.Core/Coop/Components/CoopHandler.cs index cb8d9434..31947143 100644 --- a/Fika.Core/Coop/Components/CoopHandler.cs +++ b/Fika.Core/Coop/Components/CoopHandler.cs @@ -183,7 +183,7 @@ protected void OnDestroy() StopCoroutine(ProcessSpawnQueue()); if (PingRoutine != null) { - StopCoroutine(PingRoutine); + StopCoroutine(PingRoutine); } } diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 00eee33b..a31f9717 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -31,7 +31,6 @@ using HarmonyLib; using JsonType; using LiteNetLib.Utils; -using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index b16b3450..6a58cfe4 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -228,7 +228,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId); } - return null; + return null; } public override void Proceed(bool withNetwork, Callback callback, bool scheduled = true) @@ -1287,7 +1287,7 @@ public virtual void HandleDamagePacket(DamagePacket packet) if (!FikaPlugin.Instance.FriendlyFire && damageInfo.Player.iPlayer is ObservedCoopPlayer observedCoopPlayer && !observedCoopPlayer.IsObservedAI) { return; - } + } } } diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 4307efc4..e69a00c7 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -889,7 +889,7 @@ protected override async void Start() { IsObservedAI = true; } - + PacketSender = gameObject.AddComponent(); if (IsObservedAI) diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 71d1adc5..99e6a593 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -7,7 +7,6 @@ using Fika.Core.Networking.Http.Models; using Fika.Core.UI.Models; using HarmonyLib; -using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; diff --git a/Fika.Core/UI/Patches/ItemContextPatch.cs b/Fika.Core/UI/Patches/ItemContextPatch.cs index 53de2019..cc877167 100644 --- a/Fika.Core/UI/Patches/ItemContextPatch.cs +++ b/Fika.Core/UI/Patches/ItemContextPatch.cs @@ -7,7 +7,6 @@ using Fika.Core.Networking.Http; using Fika.Core.UI.Models; using HarmonyLib; -using Newtonsoft.Json; using System.Collections.Generic; using System.Linq; using System.Reflection; From ee5f6989dc8a3465d40df923da52f945cef39bde Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sun, 5 May 2024 04:03:35 -0400 Subject: [PATCH 12/31] basic nameplate occlusion using existing isVisible check --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 7 +++++++ Fika.Core/FikaPlugin.cs | 25 ++++++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 39636f91..b7b34965 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -84,6 +84,13 @@ protected void Update() private void UpdateScreenSpacePosition() { + // Check if we should occlude the name plate + if (FikaPlugin.OccludeNamePlates.Value && !currentPlayer.IsVisible) + { + playerPlate.ScalarObjectScreen.active = false; + return; + } + float opacityMultiplier = 1f; ProceduralWeaponAnimation proceduralWeaponAnimation = mainPlayer.ProceduralWeaponAnimation; diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index f4a52b5f..4cd46a20 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -108,6 +108,7 @@ public class FikaPlugin : BaseUnityPlugin // Coop | NamePlates public static ConfigEntry UseNamePlates { get; set; } + public static ConfigEntry OccludeNamePlates { get; set; } public static ConfigEntry HideHealthBar { get; set; } public static ConfigEntry UseHealthNumber { get; set; } public static ConfigEntry UsePlateFactionSide { get; set; } @@ -264,31 +265,33 @@ private void SetupConfig() FasterInventoryScrollSpeed = Config.Bind("Coop", "Faster Inventory Scroll Speed", 63, new ConfigDescription("The speed at which the inventory scrolls at. Default is 63.", new AcceptableValueRange(63, 500), new ConfigurationManagerAttributes() { Order = 2 })); ExtractKey = Config.Bind("Coop", "Extract Key", new KeyboardShortcut(KeyCode.F8), new ConfigDescription("The key used to extract from the raid.", tags: new ConfigurationManagerAttributes() { Order = 1 })); - + // Coop | NamePlates - UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); - + UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 11 })); + + OccludeNamePlates = Config.Bind("Coop | NamePlates", "Occlude Name Plates", false, new ConfigDescription("Hides name plates for players that are not visible.", tags: new ConfigurationManagerAttributes() { Order = 10 })); + HideHealthBar = Config.Bind("Coop | NamePlates", "Hide Health Bar", false, new ConfigDescription("Completely hides the health bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); - + UseHealthNumber = Config.Bind("Coop | NamePlates", "Show HP% instead of bar", false, new ConfigDescription("Shows health in % amount instead of using the bar.", tags: new ConfigurationManagerAttributes() { Order = 8 })); - + UsePlateFactionSide = Config.Bind("Coop | NamePlates", "Show Player Faction Icon", true, new ConfigDescription("Shows the player faction icon next to the HP bar.", tags: new ConfigurationManagerAttributes() { Order = 7 })); - + HideNamePlateInOptic = Config.Bind("Coop | NamePlates", "Hide Name Plate in Optic", true, new ConfigDescription("Hides the name plate when viewing through PiP scopes since it's kinda janky.", tags: new ConfigurationManagerAttributes() { Order = 6 })); - + DecreaseOpacityNotLookingAt = Config.Bind("Coop | NamePlates", "Decrease Opacity In Peripheral", true, new ConfigDescription("Decreases the opacity of the name plates when not looking at a player.", tags: new ConfigurationManagerAttributes() { Order = 5 })); NamePlateScale = Config.Bind("Coop | NamePlates", "Name Plate Scale", 0.22f, new ConfigDescription("Size of the name plates", new AcceptableValueRange(0.05f, 1f), new ConfigurationManagerAttributes() { Order = 4 })); - + OpacityInADS = Config.Bind("Coop | NamePlates", "Opacity in ADS", 0.75f, new ConfigDescription("The opacity of the name plates when aiming down sights.", new AcceptableValueRange(0.1f, 1f), new ConfigurationManagerAttributes() { Order = 3 })); - + MaxDistanceToShow = Config.Bind("Coop | NamePlates", "Max Distance to Show", 500f, new ConfigDescription("The maximum distance at which name plates will become invisible, starts to fade at half the input value.", new AcceptableValueRange(10f, 1000f), new ConfigurationManagerAttributes() { Order = 2 })); - + MinimumOpacity = Config.Bind("Coop | NamePlates", "Minimum Opacity", 0.1f, new ConfigDescription("The minimum opacity of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 1 })); MinimumNamePlateScale = Config.Bind("Coop | NamePlates", "Minimum Name Plate Scale", 0.01f, new ConfigDescription("The minimum scale of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 0 })); - + // Coop | Custom UsePingSystem = Config.Bind("Coop | Custom", "Ping System", false, new ConfigDescription("Toggle Ping System. If enabled you can receive and send pings by pressing the ping key.", tags: new ConfigurationManagerAttributes() { Order = 6 })); From 75872235c55627167f5b697c5068e14b9bd746fb Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sun, 5 May 2024 05:45:44 -0400 Subject: [PATCH 13/31] Removed occlusion setting, implemented throttled updates, all elements actively respect config --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 103 ++++++++++++++++--------- Fika.Core/FikaPlugin.cs | 5 +- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index b7b34965..7bc85660 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -22,6 +22,9 @@ public class FikaHealthBar : MonoBehaviour private CoopPlayer mainPlayer; private PlayerPlateUI playerPlate; private float screenScale = 1f; + private int layerMask; + private int frameCounter = 0; + private readonly int throttleInterval = 60; // throttle to 1 update per 60 frames protected void Awake() { @@ -34,17 +37,24 @@ protected void Update() { if (currentPlayer != null) { - if (!FikaPlugin.UseNamePlates.Value) + bool throttleUpdate = IsThrottleUpdate(); + if (throttleUpdate) { - playerPlate.gameObject.SetActive(false); - return; - } - else if (playerPlate.gameObject.active == false) - { - playerPlate.gameObject.SetActive(true); + // Handling the visibility of elements + if (!FikaPlugin.UseNamePlates.Value) + { + playerPlate.gameObject.SetActive(false); + return; + } + else if (playerPlate.gameObject.active == false) + { + playerPlate.gameObject.SetActive(true); + } + SetPlayerPlateFactionVisibility(FikaPlugin.UsePlateFactionSide.Value); + SetPlayerPlateHealthVisibility(FikaPlugin.HideHealthBar.Value); } - UpdateScreenSpacePosition(); - if (!FikaPlugin.HideHealthBar.Value) + // Updating the health bar + if (playerPlate.healthBarScreen.gameObject.activeSelf) { float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; @@ -71,6 +81,9 @@ protected void Update() UpdateHealthBarColor(normalizedHealth); } } + // Finally, update the screen space position + UpdateScreenSpacePosition(throttleUpdate); + // Destroy if this player is dead if (!currentPlayer.HealthController.IsAlive) { Destroy(this); @@ -82,17 +95,10 @@ protected void Update() } } - private void UpdateScreenSpacePosition() + private void UpdateScreenSpacePosition(bool throttleUpdate) { - // Check if we should occlude the name plate - if (FikaPlugin.OccludeNamePlates.Value && !currentPlayer.IsVisible) - { - playerPlate.ScalarObjectScreen.active = false; - return; - } - + // ADS opacity handling float opacityMultiplier = 1f; - ProceduralWeaponAnimation proceduralWeaponAnimation = mainPlayer.ProceduralWeaponAnimation; if (mainPlayer.HealthController.IsAlive && proceduralWeaponAnimation.IsAiming) { @@ -106,7 +112,9 @@ private void UpdateScreenSpacePosition() CameraClass cameraInstance = CameraClass.Instance; Camera camera = cameraInstance.Camera; - float sqrDistance = (camera.transform.position - currentPlayer.Position).sqrMagnitude; + // Distance check + Vector3 direction = camera.transform.position - currentPlayer.Position; + float sqrDistance = direction.sqrMagnitude; float maxDistanceToShow = FikaPlugin.MaxDistanceToShow.Value * FikaPlugin.MaxDistanceToShow.Value; if (sqrDistance > maxDistanceToShow) { @@ -114,6 +122,7 @@ private void UpdateScreenSpacePosition() return; } + // If we're here, we can show the name plate playerPlate.ScalarObjectScreen.active = true; float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); @@ -189,24 +198,6 @@ private void CreateHealthBar() GameObject uiGameObj = Instantiate(uiPrefab); playerPlate = uiGameObj.GetComponent(); playerPlate.SetNameText(currentPlayer.Profile.Info.MainProfileNickname); - if (FikaPlugin.UsePlateFactionSide.Value) - { - if (currentPlayer.Profile.Side == EPlayerSide.Usec) - { - playerPlate.usecPlateScreen.gameObject.SetActive(true); - } - else if (currentPlayer.Profile.Side == EPlayerSide.Bear) - { - playerPlate.bearPlateScreen.gameObject.SetActive(true); - } - } - if (FikaPlugin.HideHealthBar.Value) - { - playerPlate.healthBarScreen.gameObject.SetActive(false); - playerPlate.healthNumberScreen.gameObject.SetActive(false); - playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); - playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); - } if (FikaPlugin.DevelopersList.ContainsKey(currentPlayer.Profile.Nickname.ToLower())) { playerPlate.playerNameScreen.color = new Color(0, 0.6091f, 1, 1); @@ -225,6 +216,9 @@ private void CreateHealthBar() playerPlate.usecPlateScreen.GetComponent().sprite = specialIcons.IconsSettings[2].IconSprite; playerPlate.usecPlateScreen.transform.localPosition = new Vector3(0f, 24.9f, 0); } + // Start the plates both disabled, the visibility will be set in the update loop + playerPlate.usecPlateScreen.gameObject.SetActive(false); + playerPlate.bearPlateScreen.gameObject.SetActive(false); } } @@ -235,7 +229,7 @@ private void UpdateHealthBarColor(float normalizedHealth) playerPlate.healthBarScreen.color = color; } - private void UpdateColorImage(UnityEngine.UI.Image screenObject, float alpha) + private void UpdateColorImage(Image screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) { @@ -255,6 +249,39 @@ private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, floa } } + private void SetPlayerPlateHealthVisibility(bool hidden) + { + playerPlate.healthNumberScreen.gameObject.SetActive(!hidden && FikaPlugin.UseHealthNumber.Value); + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(!hidden && FikaPlugin.UseHealthNumber.Value); + playerPlate.healthBarScreen.gameObject.SetActive(!hidden && !FikaPlugin.UseHealthNumber.Value); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(!hidden && !FikaPlugin.UseHealthNumber.Value); + } + + + private void SetPlayerPlateFactionVisibility(bool visible) + { + if (currentPlayer.Profile.Side == EPlayerSide.Usec) + { + playerPlate.usecPlateScreen.gameObject.SetActive(visible); + } + else if (currentPlayer.Profile.Side == EPlayerSide.Bear) + { + playerPlate.bearPlateScreen.gameObject.SetActive(visible); + } + } + + private bool IsThrottleUpdate() + { + // For throttling updates to various elements + frameCounter++; + bool throttleUpdate = frameCounter >= throttleInterval; + if (throttleUpdate) + { + frameCounter = 0; + } + return throttleUpdate; + } + private void OnDestroy() { playerPlate.gameObject.SetActive(false); diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index 4cd46a20..23666ccb 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -108,7 +108,6 @@ public class FikaPlugin : BaseUnityPlugin // Coop | NamePlates public static ConfigEntry UseNamePlates { get; set; } - public static ConfigEntry OccludeNamePlates { get; set; } public static ConfigEntry HideHealthBar { get; set; } public static ConfigEntry UseHealthNumber { get; set; } public static ConfigEntry UsePlateFactionSide { get; set; } @@ -268,9 +267,7 @@ private void SetupConfig() // Coop | NamePlates - UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 11 })); - - OccludeNamePlates = Config.Bind("Coop | NamePlates", "Occlude Name Plates", false, new ConfigDescription("Hides name plates for players that are not visible.", tags: new ConfigurationManagerAttributes() { Order = 10 })); + UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); HideHealthBar = Config.Bind("Coop | NamePlates", "Hide Health Bar", false, new ConfigDescription("Completely hides the health bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); From 4d19a380e3156550f0d95a8e28c6570bea4b05b8 Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sun, 5 May 2024 05:47:26 -0400 Subject: [PATCH 14/31] remove unused int --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 7bc85660..6564eb61 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -22,7 +22,6 @@ public class FikaHealthBar : MonoBehaviour private CoopPlayer mainPlayer; private PlayerPlateUI playerPlate; private float screenScale = 1f; - private int layerMask; private int frameCounter = 0; private readonly int throttleInterval = 60; // throttle to 1 update per 60 frames From 0427165b15fa1fe2fb0541b22bf815bcb31dc455 Mon Sep 17 00:00:00 2001 From: NickMillion <37994255+NickMillion@users.noreply.github.com> Date: Sun, 5 May 2024 07:45:56 -0400 Subject: [PATCH 15/31] NamePlate -> Name Plate to make Lacy happy --- Fika.Core/FikaPlugin.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index 23666ccb..3667d613 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -106,7 +106,7 @@ public class FikaPlugin : BaseUnityPlugin public static ConfigEntry FasterInventoryScrollSpeed { get; set; } public static ConfigEntry ExtractKey { get; set; } - // Coop | NamePlates + // Coop | Name Plates public static ConfigEntry UseNamePlates { get; set; } public static ConfigEntry HideHealthBar { get; set; } public static ConfigEntry UseHealthNumber { get; set; } @@ -265,29 +265,29 @@ private void SetupConfig() ExtractKey = Config.Bind("Coop", "Extract Key", new KeyboardShortcut(KeyCode.F8), new ConfigDescription("The key used to extract from the raid.", tags: new ConfigurationManagerAttributes() { Order = 1 })); - // Coop | NamePlates + // Coop | Name Plates - UseNamePlates = Config.Bind("Coop | NamePlates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); + UseNamePlates = Config.Bind("Coop | Name Plates", "Show Player Name Plates", false, new ConfigDescription("Toggle Health-Bars & Names.", tags: new ConfigurationManagerAttributes() { Order = 10 })); - HideHealthBar = Config.Bind("Coop | NamePlates", "Hide Health Bar", false, new ConfigDescription("Completely hides the health bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); + HideHealthBar = Config.Bind("Coop | Name Plates", "Hide Health Bar", false, new ConfigDescription("Completely hides the health bar.", tags: new ConfigurationManagerAttributes() { Order = 9 })); - UseHealthNumber = Config.Bind("Coop | NamePlates", "Show HP% instead of bar", false, new ConfigDescription("Shows health in % amount instead of using the bar.", tags: new ConfigurationManagerAttributes() { Order = 8 })); + UseHealthNumber = Config.Bind("Coop | Name Plates", "Show HP% instead of bar", false, new ConfigDescription("Shows health in % amount instead of using the bar.", tags: new ConfigurationManagerAttributes() { Order = 8 })); - UsePlateFactionSide = Config.Bind("Coop | NamePlates", "Show Player Faction Icon", true, new ConfigDescription("Shows the player faction icon next to the HP bar.", tags: new ConfigurationManagerAttributes() { Order = 7 })); + UsePlateFactionSide = Config.Bind("Coop | Name Plates", "Show Player Faction Icon", true, new ConfigDescription("Shows the player faction icon next to the HP bar.", tags: new ConfigurationManagerAttributes() { Order = 7 })); - HideNamePlateInOptic = Config.Bind("Coop | NamePlates", "Hide Name Plate in Optic", true, new ConfigDescription("Hides the name plate when viewing through PiP scopes since it's kinda janky.", tags: new ConfigurationManagerAttributes() { Order = 6 })); + HideNamePlateInOptic = Config.Bind("Coop | Name Plates", "Hide Name Plate in Optic", true, new ConfigDescription("Hides the name plate when viewing through PiP scopes since it's kinda janky.", tags: new ConfigurationManagerAttributes() { Order = 6 })); - DecreaseOpacityNotLookingAt = Config.Bind("Coop | NamePlates", "Decrease Opacity In Peripheral", true, new ConfigDescription("Decreases the opacity of the name plates when not looking at a player.", tags: new ConfigurationManagerAttributes() { Order = 5 })); + DecreaseOpacityNotLookingAt = Config.Bind("Coop | Name Plates", "Decrease Opacity In Peripheral", true, new ConfigDescription("Decreases the opacity of the name plates when not looking at a player.", tags: new ConfigurationManagerAttributes() { Order = 5 })); - NamePlateScale = Config.Bind("Coop | NamePlates", "Name Plate Scale", 0.22f, new ConfigDescription("Size of the name plates", new AcceptableValueRange(0.05f, 1f), new ConfigurationManagerAttributes() { Order = 4 })); + NamePlateScale = Config.Bind("Coop | Name Plates", "Name Plate Scale", 0.22f, new ConfigDescription("Size of the name plates", new AcceptableValueRange(0.05f, 1f), new ConfigurationManagerAttributes() { Order = 4 })); - OpacityInADS = Config.Bind("Coop | NamePlates", "Opacity in ADS", 0.75f, new ConfigDescription("The opacity of the name plates when aiming down sights.", new AcceptableValueRange(0.1f, 1f), new ConfigurationManagerAttributes() { Order = 3 })); + OpacityInADS = Config.Bind("Coop | Name Plates", "Opacity in ADS", 0.75f, new ConfigDescription("The opacity of the name plates when aiming down sights.", new AcceptableValueRange(0.1f, 1f), new ConfigurationManagerAttributes() { Order = 3 })); - MaxDistanceToShow = Config.Bind("Coop | NamePlates", "Max Distance to Show", 500f, new ConfigDescription("The maximum distance at which name plates will become invisible, starts to fade at half the input value.", new AcceptableValueRange(10f, 1000f), new ConfigurationManagerAttributes() { Order = 2 })); + MaxDistanceToShow = Config.Bind("Coop | Name Plates", "Max Distance to Show", 500f, new ConfigDescription("The maximum distance at which name plates will become invisible, starts to fade at half the input value.", new AcceptableValueRange(10f, 1000f), new ConfigurationManagerAttributes() { Order = 2 })); - MinimumOpacity = Config.Bind("Coop | NamePlates", "Minimum Opacity", 0.1f, new ConfigDescription("The minimum opacity of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 1 })); + MinimumOpacity = Config.Bind("Coop | Name Plates", "Minimum Opacity", 0.1f, new ConfigDescription("The minimum opacity of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 1 })); - MinimumNamePlateScale = Config.Bind("Coop | NamePlates", "Minimum Name Plate Scale", 0.01f, new ConfigDescription("The minimum scale of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 0 })); + MinimumNamePlateScale = Config.Bind("Coop | Name Plates", "Minimum Name Plate Scale", 0.01f, new ConfigDescription("The minimum scale of the name plates.", new AcceptableValueRange(0.0f, 1f), new ConfigurationManagerAttributes() { Order = 0 })); // Coop | Custom From 79a678fdc6bb3403fb446b274b395db856027204 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Sun, 5 May 2024 21:05:01 +0200 Subject: [PATCH 16/31] WIP NetId resync --- Fika.Core/Coop/GameMode/CoopGame.cs | 108 +++++++++--------- Fika.Core/Networking/FikaClient.cs | 18 +++ .../Packets/Communication/SyncNetIdPacket.cs | 22 ++++ 3 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 Fika.Core/Networking/Packets/Communication/SyncNetIdPacket.cs diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index a31f9717..284f8da9 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -482,33 +482,31 @@ public override void vmethod_1(float timeBeforeDeploy) /// Time in seconds to count down private async void DeployScreen(float timeBeforeDeploy) { - if (MatchmakerAcceptPatches.IsServer && MatchmakerAcceptPatches.HostExpectedNumberOfPlayers <= 1) + if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) { - if (fikaStartButton != null) + if (MatchmakerAcceptPatches.IsServer && MatchmakerAcceptPatches.HostExpectedNumberOfPlayers <= 1) { - Destroy(fikaStartButton); - } + if (fikaStartButton != null) + { + Destroy(fikaStartButton); + } - if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) - { SetStatusModel status = new SetStatusModel(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME); await FikaRequestHandler.UpdateSetStatus(status); - } - Singleton.Instance.ReadyClients++; - base.vmethod_1(timeBeforeDeploy); - return; - } + Singleton.Instance.ReadyClients++; + base.vmethod_1(timeBeforeDeploy); + return; + } - forceStart = false; + NetDataWriter writer = new(); + forceStart = false; - MatchmakerAcceptPatches.GClass3163.ChangeStatus("Waiting for other players to finish loading..."); + MatchmakerAcceptPatches.GClass3163.ChangeStatus("Waiting for other players to finish loading..."); - fikaStartButton?.SetActive(true); + fikaStartButton?.SetActive(true); - if (MatchmakerAcceptPatches.IsServer) - { - if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) + if (MatchmakerAcceptPatches.IsServer) { SetStatusModel status = new SetStatusModel(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME); await FikaRequestHandler.UpdateSetStatus(status); @@ -517,52 +515,56 @@ private async void DeployScreen(float timeBeforeDeploy) { await Task.Delay(100); } while (coopHandler.HumanPlayers < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); - } - FikaServer server = Singleton.Instance; - server.ReadyClients++; - InformationPacket packet = new() - { - NumberOfPlayers = server.NetServer.ConnectedPeersCount, - ReadyPlayers = server.ReadyClients - }; - NetDataWriter writer = new(); - writer.Reset(); - server.SendDataToAll(writer, ref packet, LiteNetLib.DeliveryMethod.ReliableOrdered); + FikaServer server = Singleton.Instance; + server.ReadyClients++; + InformationPacket packet = new() + { + NumberOfPlayers = server.NetServer.ConnectedPeersCount, + ReadyPlayers = server.ReadyClients + }; + writer.Reset(); + server.SendDataToAll(writer, ref packet, LiteNetLib.DeliveryMethod.ReliableOrdered); - do - { - await Task.Delay(250); - } while (Singleton.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); - } - else if (MatchmakerAcceptPatches.IsClient) - { - if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) + do + { + await Task.Delay(250); + } while (Singleton.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); + + foreach (CoopPlayer player in coopHandler.Players.Values) + { + SyncNetIdPacket syncPacket = new(player.ProfileId, player.NetId); + + writer.Reset(); + Singleton.Instance.SendDataToAll(writer, ref syncPacket, LiteNetLib.DeliveryMethod.ReliableUnordered); + } + } + else if (MatchmakerAcceptPatches.IsClient) { do { await Task.Delay(100); } while (coopHandler.HumanPlayers < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); - } - FikaClient client = Singleton.Instance; - InformationPacket packet = new(true) - { - ReadyPlayers = 1 - }; - NetDataWriter writer = new(); - writer.Reset(); - client.SendData(writer, ref packet, LiteNetLib.DeliveryMethod.ReliableOrdered); + FikaClient client = Singleton.Instance; + InformationPacket packet = new(true) + { + ReadyPlayers = 1 + }; + writer.Reset(); + client.SendData(writer, ref packet, LiteNetLib.DeliveryMethod.ReliableOrdered); - do - { - await Task.Delay(250); - } while (Singleton.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); - } + do + { + await Task.Delay(250); + } while (Singleton.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart); + } - if (fikaStartButton != null) - { - Destroy(fikaStartButton); + if (fikaStartButton != null) + { + Destroy(fikaStartButton); + } + } base.vmethod_1(timeBeforeDeploy); diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 4899f4f2..9e4d9566 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -27,6 +27,7 @@ using System.Net; using System.Net.Sockets; using UnityEngine; +using UnityEngine.Assertions.Must; namespace Fika.Core.Networking { @@ -80,6 +81,7 @@ public void Start() packetProcessor.SubscribeNetSerializable(OnBorderZonePacketReceived); packetProcessor.SubscribeNetSerializable(OnSendCharacterPacketReceived); packetProcessor.SubscribeNetSerializable(OnAssignNetIdPacketReceived); + packetProcessor.SubscribeNetSerializable(OnSyncNetIdPacketReceived); _netClient = new NetManager(this) { @@ -114,6 +116,21 @@ public void Start() ClientReady = true; } + private void OnSyncNetIdPacketReceived(SyncNetIdPacket packet, NetPeer peer) + { + Dictionary newPlayers = Players; + if (Players[packet.NetId].ProfileId != packet.ProfileId) + { + FikaPlugin.Instance.FikaLogger.LogWarning($"{packet.ProfileId} had the wrong NetId: {Players[packet.NetId].NetId}, should be {packet.NetId}"); + for (int i = 0; i < Players.Count; i++) + { + KeyValuePair playerToReorganize = Players.Where(x => x.Value.ProfileId == packet.ProfileId).First(); + Players.Remove(playerToReorganize.Key); + Players[packet.NetId] = playerToReorganize.Value; + } + } + } + private void OnAssignNetIdPacketReceived(AssignNetIdPacket packet, NetPeer peer) { MyPlayer.NetId = packet.NetId; @@ -130,6 +147,7 @@ private void OnAssignNetIdPacketReceived(AssignNetIdPacket packet, NetPeer peer) if (i == -1) { + FikaPlugin.Instance.FikaLogger.LogError("Could not find own player among players list"); return; } diff --git a/Fika.Core/Networking/Packets/Communication/SyncNetIdPacket.cs b/Fika.Core/Networking/Packets/Communication/SyncNetIdPacket.cs new file mode 100644 index 00000000..3f9590b9 --- /dev/null +++ b/Fika.Core/Networking/Packets/Communication/SyncNetIdPacket.cs @@ -0,0 +1,22 @@ +using LiteNetLib.Utils; + +namespace Fika.Core.Networking +{ + public struct SyncNetIdPacket(string profileId, int netId) : INetSerializable + { + public string ProfileId = profileId; + public int NetId = netId; + + public void Deserialize(NetDataReader reader) + { + ProfileId = reader.GetString(); + NetId = reader.GetInt(); + } + + public void Serialize(NetDataWriter writer) + { + writer.Put(ProfileId); + writer.Put(NetId); + } + } +} From 4a4124a28139fd27552c9b5e034680700bb058eb Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 08:15:14 +0200 Subject: [PATCH 17/31] Test before setting new NetId --- Fika.Core/Networking/FikaClient.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 9e4d9566..6e992498 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -119,20 +119,28 @@ public void Start() private void OnSyncNetIdPacketReceived(SyncNetIdPacket packet, NetPeer peer) { Dictionary newPlayers = Players; - if (Players[packet.NetId].ProfileId != packet.ProfileId) + if (Players.TryGetValue(packet.NetId, out CoopPlayer player)) { - FikaPlugin.Instance.FikaLogger.LogWarning($"{packet.ProfileId} had the wrong NetId: {Players[packet.NetId].NetId}, should be {packet.NetId}"); - for (int i = 0; i < Players.Count; i++) + if (player.ProfileId != packet.ProfileId) { - KeyValuePair playerToReorganize = Players.Where(x => x.Value.ProfileId == packet.ProfileId).First(); - Players.Remove(playerToReorganize.Key); - Players[packet.NetId] = playerToReorganize.Value; - } + FikaPlugin.Instance.FikaLogger.LogWarning($"OnSyncNetIdPacketReceived: {packet.ProfileId} had the wrong NetId: {Players[packet.NetId].NetId}, should be {packet.NetId}"); + for (int i = 0; i < Players.Count; i++) + { + KeyValuePair playerToReorganize = Players.Where(x => x.Value.ProfileId == packet.ProfileId).First(); + Players.Remove(playerToReorganize.Key); + Players[packet.NetId] = playerToReorganize.Value; + } + } + } + else + { + FikaPlugin.Instance.FikaLogger.LogError($"OnSyncNetIdPacketReceived: Could not find NetId {packet.NetId} in player list!"); } } private void OnAssignNetIdPacketReceived(AssignNetIdPacket packet, NetPeer peer) { + FikaPlugin.Instance.FikaLogger.LogInfo($"OnAssignNetIdPacketReceived: Assigned NetId {packet.NetId} to my own client."); MyPlayer.NetId = packet.NetId; int i = -1; foreach (var player in Players) @@ -147,7 +155,7 @@ private void OnAssignNetIdPacketReceived(AssignNetIdPacket packet, NetPeer peer) if (i == -1) { - FikaPlugin.Instance.FikaLogger.LogError("Could not find own player among players list"); + FikaPlugin.Instance.FikaLogger.LogError("OnAssignNetIdPacketReceived: Could not find own player among players list"); return; } From 5048f81119bf27c6589545431328fcf06558a9b0 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 08:15:26 +0200 Subject: [PATCH 18/31] Log NetId assignment --- Fika.Core/Coop/Components/CoopHandler.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Fika.Core/Coop/Components/CoopHandler.cs b/Fika.Core/Coop/Components/CoopHandler.cs index 31947143..47a09c6d 100644 --- a/Fika.Core/Coop/Components/CoopHandler.cs +++ b/Fika.Core/Coop/Components/CoopHandler.cs @@ -493,8 +493,11 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p return null; ((CoopPlayer)otherPlayer).NetId = netId; + Logger.LogInfo($"SpawnObservedPlayer: {profile.Nickname} spawning with NetId {netId}"); if (!isAI) + { HumanPlayers++; + } if (!Players.ContainsKey(netId)) { @@ -507,7 +510,9 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p foreach (CoopPlayer player in Players.Values) { if (player is not ObservedCoopPlayer) + { continue; + } Collider playerCollider = otherPlayer.GetCharacterControllerCommon().GetCollider(); Collider otherCollider = player.GetCharacterControllerCommon().GetCollider(); From 2fe957d27aadf707aec0e30d85aa8e4d332ef770 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 08:15:36 +0200 Subject: [PATCH 19/31] WIP Allow pinging when moving --- Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs | 7 ++++++- Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs index ded2b67c..2c69f0dd 100644 --- a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs @@ -10,6 +10,7 @@ using LiteNetLib.Utils; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Fika.Core.Coop.PacketHandlers @@ -121,7 +122,11 @@ private void Update() Client?.SendData(Writer, ref healthSyncPacket, DeliveryMethod.ReliableOrdered); } } - if (FikaPlugin.PingButton.Value.IsPressed() && player.IsYourPlayer && player.HealthController.IsAlive && FikaPlugin.UsePingSystem.Value) + if (Input.GetKey(FikaPlugin.PingButton.Value.MainKey) + && FikaPlugin.PingButton.Value.Modifiers.All(Input.GetKey) + && player.IsYourPlayer + && player.HealthController.IsAlive + && FikaPlugin.UsePingSystem.Value) { player?.Ping(); } diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 35230e19..18680d29 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -13,6 +13,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Fika.Core.Coop.PacketHandlers @@ -128,7 +129,11 @@ private void Update() Server?.SendDataToAll(Writer, ref healthSyncPacket, DeliveryMethod.ReliableOrdered); } } - if (FikaPlugin.PingButton.Value.IsPressed() && player.IsYourPlayer && player.HealthController.IsAlive && FikaPlugin.UsePingSystem.Value) + if (Input.GetKey(FikaPlugin.PingButton.Value.MainKey) + && FikaPlugin.PingButton.Value.Modifiers.All(Input.GetKey) + && player.IsYourPlayer + && player.HealthController.IsAlive + && FikaPlugin.UsePingSystem.Value) { player?.Ping(); } From 28522fc134350e312987149a4e35f4465eaacbe4 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 10:57:02 +0200 Subject: [PATCH 20/31] Ping servers before attempting to connect This will resolve the infamous "Waiting for X players" issue --- Fika.Core/Networking/FikaClient.cs | 7 +- Fika.Core/Networking/FikaPingingClient.cs | 117 ++++++++++++++++++ Fika.Core/Networking/FikaServer.cs | 27 +++- Fika.Core/Networking/Models/GetHostRequest.cs | 4 +- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 45 ++++++- 5 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 Fika.Core/Networking/FikaPingingClient.cs diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 6e992498..f7ac4a7f 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -27,7 +27,6 @@ using System.Net; using System.Net.Sockets; using UnityEngine; -using UnityEngine.Assertions.Must; namespace Fika.Core.Networking { @@ -56,7 +55,7 @@ public NetManager NetClient private ManualLogSource clientLogger; public bool ClientReady = false; - public void Start() + protected void Start() { clientLogger = new("Fika Client"); @@ -96,7 +95,7 @@ public void Start() _netClient.Start(); - GetHostRequest body = new GetHostRequest(); + GetHostRequest body = new(CoopHandler.GetServerId()); GetHostResponse result = FikaRequestHandler.GetHost(body); IP = result.Ip; @@ -130,7 +129,7 @@ private void OnSyncNetIdPacketReceived(SyncNetIdPacket packet, NetPeer peer) Players.Remove(playerToReorganize.Key); Players[packet.NetId] = playerToReorganize.Value; } - } + } } else { diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs new file mode 100644 index 00000000..c3b9a547 --- /dev/null +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -0,0 +1,117 @@ +using BepInEx.Logging; +using EFT.UI; +using Fika.Core.Networking.Http; +using Fika.Core.Networking.Http.Models; +using LiteNetLib; +using LiteNetLib.Utils; +using System; +using System.Net; +using System.Net.Sockets; + +namespace Fika.Core.Networking +{ + internal class FikaPingingClient(string serverId) : INetEventListener + { + public NetManager NetClient; + private readonly ManualLogSource _logger = new("Pinging Client"); + private readonly string serverId = serverId; + private IPEndPoint remoteEndPoint; + public bool Received = false; + + public bool Init() + { + NetClient = new(this) + { + UnconnectedMessagesEnabled = true + }; + + GetHostRequest body = new(serverId); + GetHostResponse result = FikaRequestHandler.GetHost(body); + + string ip = result.Ip; + int port = result.Port; + + if (string.IsNullOrEmpty(ip)) + { + _logger.LogError("IP was empty when pinging!"); + return false; + } + + if (port == default) + { + _logger.LogError("Port was empty when pinging!"); + return false; + } + + remoteEndPoint = new(IPAddress.Parse(ip), port); + + NetClient.Start(); + + return true; + } + + public bool PingEndPoint() + { + if (Received) + { + return true; + } + + NetDataWriter writer = new(); + writer.Put("fika.hello"); + + bool success = NetClient.SendUnconnectedMessage(writer, remoteEndPoint); + return success; + } + + public void OnConnectionRequest(ConnectionRequest request) + { + // Do nothing + } + + public void OnNetworkError(IPEndPoint endPoint, SocketError socketError) + { + // Do nothing + } + + public void OnNetworkLatencyUpdate(NetPeer peer, int latency) + { + // Do nothing + } + + public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod) + { + // Do nothing + } + + public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) + { + _logger.LogInfo("Received response from server, parsing..."); + if (reader.TryGetString(out string result)) + { + if (result == "fika.hello") + { + Received = true; + } + else + { + _logger.LogError("Data was not as expected"); + } + } + else + { + _logger.LogError("Could not parse string"); + } + } + + public void OnPeerConnected(NetPeer peer) + { + // Do nothing + } + + public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) + { + // Do nothing + } + } +} diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index db488ecd..f6e4f5af 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -34,7 +34,7 @@ public class FikaServer : MonoBehaviour, INetEventListener, INetLogger { private NetManager _netServer; public NetPacketProcessor packetProcessor = new(); - private NetDataWriter _dataWriter = new(); + private readonly NetDataWriter _dataWriter = new(); public CoopPlayer MyPlayer => (CoopPlayer)Singleton.Instance.MainPlayer; public Dictionary Players => CoopHandler.Players; public List PlayersMissing = []; @@ -51,7 +51,7 @@ public NetManager NetServer } public DateTime timeSinceLastPeerDisconnected = DateTime.Now.AddDays(1); public bool hasHadPeer = false; - private ManualLogSource serverLogger; + private readonly ManualLogSource serverLogger = new("Fika Server"); public bool ServerReady = false; private int _currentNetId; @@ -61,7 +61,6 @@ public async void Start() _currentNetId = 1; NetDebug.Logger = this; - serverLogger = new("Fika Server"); packetProcessor.SubscribeNetSerializable(OnPlayerStatePacketReceived); packetProcessor.SubscribeNetSerializable(OnGameTimerPacketReceived); @@ -85,6 +84,7 @@ public async void Start() _netServer = new NetManager(this) { BroadcastReceiveEnabled = true, + UnconnectedMessagesEnabled = true, UpdateTime = 15, AutoRecycle = true, IPv6Enabled = false, @@ -607,6 +607,27 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead resp.Put(1); _netServer.SendUnconnectedMessage(resp, remoteEndPoint); } + else + { + if (reader.TryGetString(out string data)) + { + if (data == "fika.hello") + { + NetDataWriter resp = new(); + resp.Put(data); + _netServer.SendUnconnectedMessage(resp, remoteEndPoint); + serverLogger.LogInfo("PingingRequest: Correct ping query, sending response"); + } + else + { + serverLogger.LogError("PingingRequest: Data was not as expected"); + } + } + else + { + serverLogger.LogError("PingingRequest: Could not parse string"); + } + } } public void OnNetworkLatencyUpdate(NetPeer peer, int latency) diff --git a/Fika.Core/Networking/Models/GetHostRequest.cs b/Fika.Core/Networking/Models/GetHostRequest.cs index cd19b884..d0a104d2 100644 --- a/Fika.Core/Networking/Models/GetHostRequest.cs +++ b/Fika.Core/Networking/Models/GetHostRequest.cs @@ -9,9 +9,9 @@ public struct GetHostRequest [DataMember(Name = "serverId")] public string ServerId; - public GetHostRequest() + public GetHostRequest(string serverId) { - ServerId = CoopHandler.GetServerId(); + ServerId = serverId; } } } \ No newline at end of file diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 99e6a593..0f8f1dde 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -3,6 +3,7 @@ using EFT.UI; using Fika.Core.Bundles; using Fika.Core.Coop.Matchmaker; +using Fika.Core.Networking; using Fika.Core.Networking.Http; using Fika.Core.Networking.Http.Models; using Fika.Core.UI.Models; @@ -141,8 +142,48 @@ private void ManualRefresh() RefreshUI(); } - private void JoinMatch(string profileId, string serverId) + private IEnumerator JoinMatch(string profileId, string serverId, Button button) { + button.enabled = false; + + NotificationManagerClass.DisplayMessageNotification("Connecting to server...", iconType: EFT.Communications.ENotificationIconType.EntryPoint); + + FikaPingingClient pingingClient = new(serverId); + if (pingingClient.Init()) + { + int attempts = 0; + bool success = false; + + do + { + attempts++; + if (pingingClient.PingEndPoint()) + { + pingingClient.NetClient.PollEvents(); + success = pingingClient.Received; + } + yield return new WaitForFixedUpdate(); + } while (attempts < 5 && !success); + + if (!success) + { + Singleton.Instance.ShowCriticalErrorScreen( + "ERROR CONNECTING", + "Unable to connect to the server. Make sure that all ports are open and that all settings are configured correctly.", + ErrorScreen.EButtonType.OkButton, 10f, null, null); + + button.enabled = true; + yield break; + } + } + else + { + ConsoleScreen.Log("ERROR"); + } + + pingingClient.NetClient?.Stop(); + pingingClient = null; + if (MatchmakerAcceptPatches.JoinMatch(RaidSettings, profileId, serverId, out CreateMatch result, out string errorMessage)) { MatchmakerAcceptPatches.SetGroupId(result.ServerId); @@ -211,7 +252,7 @@ private void RefreshUI() } Singleton.Instance.PlayUISound(EUISoundType.ButtonClick); - JoinMatch(ProfileId, server.name); + StartCoroutine(JoinMatch(ProfileId, server.name, button)); }); TooltipTextGetter tooltipTextGetter; From 6bd3924e998c7011997bc6adaa465ce34b54450e Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 10:57:31 +0200 Subject: [PATCH 21/31] Cleanup --- Fika.Core/Coop/GameMode/CoopGame.cs | 4 ++-- Fika.Core/Networking/FikaPingingClient.cs | 2 -- Fika.Core/Networking/Models/GetHostRequest.cs | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 284f8da9..89dc70d0 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -563,8 +563,8 @@ private async void DeployScreen(float timeBeforeDeploy) if (fikaStartButton != null) { Destroy(fikaStartButton); - } - + } + } base.vmethod_1(timeBeforeDeploy); diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs index c3b9a547..e35a506c 100644 --- a/Fika.Core/Networking/FikaPingingClient.cs +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -1,10 +1,8 @@ using BepInEx.Logging; -using EFT.UI; using Fika.Core.Networking.Http; using Fika.Core.Networking.Http.Models; using LiteNetLib; using LiteNetLib.Utils; -using System; using System.Net; using System.Net.Sockets; diff --git a/Fika.Core/Networking/Models/GetHostRequest.cs b/Fika.Core/Networking/Models/GetHostRequest.cs index d0a104d2..8ce16366 100644 --- a/Fika.Core/Networking/Models/GetHostRequest.cs +++ b/Fika.Core/Networking/Models/GetHostRequest.cs @@ -1,4 +1,3 @@ -using Fika.Core.Coop.Components; using System.Runtime.Serialization; namespace Fika.Core.Networking.Http.Models From 5421dead254a6aceb20c6f7ec990176593d19de2 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 11:10:11 +0200 Subject: [PATCH 22/31] Update loggers --- .../AkiSupport/Airdrops/Utils/FikaItemFactoryUtil.cs | 2 +- Fika.Core/Coop/BTR/FikaBTRManager_Client.cs | 2 +- Fika.Core/Coop/BTR/FikaBTRManager_Host.cs | 2 +- Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs | 2 +- Fika.Core/Networking/FikaClient.cs | 3 +-- Fika.Core/Networking/FikaPingingClient.cs | 10 +++++++--- Fika.Core/Networking/FikaServer.cs | 2 +- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 2 +- 8 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Fika.Core/AkiSupport/Airdrops/Utils/FikaItemFactoryUtil.cs b/Fika.Core/AkiSupport/Airdrops/Utils/FikaItemFactoryUtil.cs index 38ebd293..3257c561 100644 --- a/Fika.Core/AkiSupport/Airdrops/Utils/FikaItemFactoryUtil.cs +++ b/Fika.Core/AkiSupport/Airdrops/Utils/FikaItemFactoryUtil.cs @@ -19,7 +19,7 @@ public class FikaItemFactoryUtil public FikaItemFactoryUtil() { itemFactory = Singleton.Instance; - logSource = new("ItemFactoryUtil"); + logSource = Logger.CreateLogSource("ItemFactoryUtil"); } public void BuildContainer(LootableContainer container, FikaAirdropConfigModel config, string dropType) diff --git a/Fika.Core/Coop/BTR/FikaBTRManager_Client.cs b/Fika.Core/Coop/BTR/FikaBTRManager_Client.cs index c3276968..1f06af9d 100644 --- a/Fika.Core/Coop/BTR/FikaBTRManager_Client.cs +++ b/Fika.Core/Coop/BTR/FikaBTRManager_Client.cs @@ -61,7 +61,7 @@ internal class FikaBTRManager_Client : MonoBehaviour Type btrControllerType = typeof(BTRControllerClass); _updateTaxiPriceMethod = AccessTools.GetDeclaredMethods(btrControllerType).Single(IsUpdateTaxiPriceMethod); client = Singleton.Instance; - btrLogger = new("BTR Client"); + btrLogger = BepInEx.Logging.Logger.CreateLogSource("BTR Client"); } private void Awake() diff --git a/Fika.Core/Coop/BTR/FikaBTRManager_Host.cs b/Fika.Core/Coop/BTR/FikaBTRManager_Host.cs index a96ac738..e0ef2313 100644 --- a/Fika.Core/Coop/BTR/FikaBTRManager_Host.cs +++ b/Fika.Core/Coop/BTR/FikaBTRManager_Host.cs @@ -71,7 +71,7 @@ internal class FikaBTRManager_Host : MonoBehaviour Type btrControllerType = typeof(BTRControllerClass); _updateTaxiPriceMethod = AccessTools.GetDeclaredMethods(btrControllerType).Single(IsUpdateTaxiPriceMethod); server = Singleton.Instance; - btrLogger = new("BTR Host"); + btrLogger = BepInEx.Logging.Logger.CreateLogSource("BTR Host"); } public bool CanPlayerEnter(IPlayer player) diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 18680d29..21258714 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -35,7 +35,7 @@ public class ServerPacketSender : MonoBehaviour, IPacketSender private void Awake() { - logger = new("ServerPacketSender"); + logger = BepInEx.Logging.Logger.CreateLogSource("ServerPacketSender"); player = GetComponent(); } diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index f7ac4a7f..3db2a18e 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -52,12 +52,11 @@ public NetManager NetClient public string IP { get; private set; } public int Port { get; private set; } public bool SpawnPointsReceived { get; private set; } = false; - private ManualLogSource clientLogger; + private readonly ManualLogSource clientLogger = BepInEx.Logging.Logger.CreateLogSource("Fika.Client"); public bool ClientReady = false; protected void Start() { - clientLogger = new("Fika Client"); packetProcessor.SubscribeNetSerializable(OnPlayerStatePacketReceived); packetProcessor.SubscribeNetSerializable(OnGameTimerPacketReceived); diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs index e35a506c..ef77fa1a 100644 --- a/Fika.Core/Networking/FikaPingingClient.cs +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -11,7 +11,7 @@ namespace Fika.Core.Networking internal class FikaPingingClient(string serverId) : INetEventListener { public NetManager NetClient; - private readonly ManualLogSource _logger = new("Pinging Client"); + private readonly ManualLogSource _logger = Logger.CreateLogSource("Fika.PingingClient"); private readonly string serverId = serverId; private IPEndPoint remoteEndPoint; public bool Received = false; @@ -58,8 +58,7 @@ public bool PingEndPoint() NetDataWriter writer = new(); writer.Put("fika.hello"); - bool success = NetClient.SendUnconnectedMessage(writer, remoteEndPoint); - return success; + return NetClient.SendUnconnectedMessage(writer, remoteEndPoint); } public void OnConnectionRequest(ConnectionRequest request) @@ -84,7 +83,12 @@ public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelN public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) { + if (Received) + { + return; + } _logger.LogInfo("Received response from server, parsing..."); + if (reader.TryGetString(out string result)) { if (result == "fika.hello") diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index f6e4f5af..38701df8 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -51,7 +51,7 @@ public NetManager NetServer } public DateTime timeSinceLastPeerDisconnected = DateTime.Now.AddDays(1); public bool hasHadPeer = false; - private readonly ManualLogSource serverLogger = new("Fika Server"); + private readonly ManualLogSource serverLogger = BepInEx.Logging.Logger.CreateLogSource("Fika.Server"); public bool ServerReady = false; private int _currentNetId; diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 0f8f1dde..66c1687c 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -163,7 +163,7 @@ private IEnumerator JoinMatch(string profileId, string serverId, Button button) success = pingingClient.Received; } yield return new WaitForFixedUpdate(); - } while (attempts < 5 && !success); + } while (!success && attempts < 5); if (!success) { From ace80938effda58d0dba906c4675febfa11182e0 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 13:58:25 +0200 Subject: [PATCH 23/31] More logging in CoopHandler --- Fika.Core/Coop/Components/CoopHandler.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Fika.Core/Coop/Components/CoopHandler.cs b/Fika.Core/Coop/Components/CoopHandler.cs index 47a09c6d..fd367537 100644 --- a/Fika.Core/Coop/Components/CoopHandler.cs +++ b/Fika.Core/Coop/Components/CoopHandler.cs @@ -503,6 +503,10 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p { Players.Add(netId, (CoopPlayer)otherPlayer); } + else + { + Logger.LogError($"Trying to add {otherPlayer.Profile.Nickname} to list of players but it was already there!"); + } if (!Singleton.Instance.RegisteredPlayers.Any(x => x.Profile.ProfileId == profile.ProfileId)) Singleton.Instance.RegisteredPlayers.Add(otherPlayer); From 81361c018825446db38c89c41f549e7b80064357 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 13:58:36 +0200 Subject: [PATCH 24/31] More logging in Fika Client --- Fika.Core/Networking/FikaClient.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 3db2a18e..203be287 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -133,6 +133,13 @@ private void OnSyncNetIdPacketReceived(SyncNetIdPacket packet, NetPeer peer) else { FikaPlugin.Instance.FikaLogger.LogError($"OnSyncNetIdPacketReceived: Could not find NetId {packet.NetId} in player list!"); + string allPlayers = ""; + foreach (KeyValuePair kvp in CoopHandler.Players) + { + string toAdd = $"Key: {kvp.Key}, Nickname: {kvp.Value.Profile.Nickname}, NetId: {kvp.Value.NetId}"; + allPlayers = string.Join(", ", allPlayers + toAdd); + } + FikaPlugin.Instance.FikaLogger.LogError(allPlayers); } } From df2041df34139447a16b9cbd0e2e1190fd01a9d4 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 13:58:47 +0200 Subject: [PATCH 25/31] Assign standard NetId of 1000 to clients --- Fika.Core/Coop/GameMode/CoopGame.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 89dc70d0..fa8dfbd2 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -654,7 +654,7 @@ public override async Task vmethod_2(int playerId, Vector3 position LocalPlayer myPlayer = await CoopPlayer.Create(playerId, spawnPoint.Position, spawnPoint.Rotation, "Player", "Main_", EPointOfView.FirstPerson, profile, false, UpdateQueue, Player.EUpdateMode.Auto, Player.EUpdateMode.Auto, GClass549.Config.CharacterController.ClientPlayerMode, () => Singleton.Instance.Control.Settings.MouseSensitivity, - () => Singleton.Instance.Control.Settings.MouseAimingSensitivity, new GClass1445(), 0, questController); + () => Singleton.Instance.Control.Settings.MouseAimingSensitivity, new GClass1445(), MatchmakerAcceptPatches.IsServer ? 0 : 1000, questController); profile.SetSpawnedInSession(profile.Side == EPlayerSide.Savage); From 6273364f916ff06fba9a9a0f3a7dfc9859717a6b Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 13:59:09 +0200 Subject: [PATCH 26/31] Ping for 3 seconds rather than 5 ticks --- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 66c1687c..5808c542 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -162,8 +162,8 @@ private IEnumerator JoinMatch(string profileId, string serverId, Button button) pingingClient.NetClient.PollEvents(); success = pingingClient.Received; } - yield return new WaitForFixedUpdate(); - } while (!success && attempts < 5); + yield return new WaitForSeconds(0.1f); + } while (!success && attempts < 30); if (!success) { From 537b8237870a1dc378648659061d2c9c5266658f Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 14:43:31 +0200 Subject: [PATCH 27/31] Improve health bar update --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 84 +++++++++++++++++--------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 6564eb61..87bfb0cf 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -52,34 +52,6 @@ protected void Update() SetPlayerPlateFactionVisibility(FikaPlugin.UsePlateFactionSide.Value); SetPlayerPlateHealthVisibility(FikaPlugin.HideHealthBar.Value); } - // Updating the health bar - if (playerPlate.healthBarScreen.gameObject.activeSelf) - { - float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; - float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; - if (FikaPlugin.UseHealthNumber.Value) - { - if (!playerPlate.healthNumberBackgroundScreen.gameObject.activeSelf) - { - playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(true); - playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); - } - int healthNumberPercentage = (int)Math.Round((currentHealth / maxHealth) * 100); - playerPlate.SetHealthNumberText($"{healthNumberPercentage}%"); - } - else - { - if (!playerPlate.healthBarBackgroundScreen.gameObject.active) - { - playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); - playerPlate.healthBarBackgroundScreen.gameObject.SetActive(true); - } - - float normalizedHealth = Mathf.Clamp01(currentHealth / maxHealth); - playerPlate.healthBarScreen.fillAmount = normalizedHealth; - UpdateHealthBarColor(normalizedHealth); - } - } // Finally, update the screen space position UpdateScreenSpacePosition(throttleUpdate); // Destroy if this player is dead @@ -219,6 +191,59 @@ private void CreateHealthBar() playerPlate.usecPlateScreen.gameObject.SetActive(false); playerPlate.bearPlateScreen.gameObject.SetActive(false); } + + currentPlayer.HealthController.HealthChangedEvent += HealthController_HealthChangedEvent; + currentPlayer.HealthController.BodyPartDestroyedEvent += HealthController_BodyPartDestroyedEvent; + currentPlayer.HealthController.BodyPartRestoredEvent += HealthController_BodyPartRestoredEvent; + } + + private void HealthController_BodyPartRestoredEvent(EBodyPart arg1, EFT.HealthSystem.ValueStruct arg2) + { + UpdateHealth(); + } + + private void HealthController_BodyPartDestroyedEvent(EBodyPart arg1, EDamageType arg2) + { + UpdateHealth(); + } + + private void HealthController_HealthChangedEvent(EBodyPart arg1, float arg2, DamageInfo arg3) + { + UpdateHealth(); + } + + /// + /// Updates the health on the HealthBar, this is invoked from events on the healthcontroller + /// + private void UpdateHealth() + { + if (playerPlate.healthBarScreen.gameObject.activeSelf) + { + float currentHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Current; + float maxHealth = currentPlayer.HealthController.GetBodyPartHealth(EBodyPart.Common, true).Maximum; + if (FikaPlugin.UseHealthNumber.Value) + { + if (!playerPlate.healthNumberBackgroundScreen.gameObject.activeSelf) + { + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(true); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(false); + } + int healthNumberPercentage = (int)Math.Round((currentHealth / maxHealth) * 100); + playerPlate.SetHealthNumberText($"{healthNumberPercentage}%"); + } + else + { + if (!playerPlate.healthBarBackgroundScreen.gameObject.active) + { + playerPlate.healthNumberBackgroundScreen.gameObject.SetActive(false); + playerPlate.healthBarBackgroundScreen.gameObject.SetActive(true); + } + + float normalizedHealth = Mathf.Clamp01(currentHealth / maxHealth); + playerPlate.healthBarScreen.fillAmount = normalizedHealth; + UpdateHealthBarColor(normalizedHealth); + } + } } private void UpdateHealthBarColor(float normalizedHealth) @@ -283,6 +308,9 @@ private bool IsThrottleUpdate() private void OnDestroy() { + currentPlayer.HealthController.HealthChangedEvent -= HealthController_HealthChangedEvent; + currentPlayer.HealthController.BodyPartDestroyedEvent -= HealthController_BodyPartDestroyedEvent; + currentPlayer.HealthController.BodyPartRestoredEvent -= HealthController_BodyPartRestoredEvent; playerPlate.gameObject.SetActive(false); Destroy(this); } From 248cf1b81e2e0106eb01204cf82bae96cd17261f Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 14:43:58 +0200 Subject: [PATCH 28/31] Explicit vars --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 87bfb0cf..48efef20 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -257,7 +257,7 @@ private void UpdateColorImage(Image screenObject, float alpha) { if (screenObject.gameObject.activeInHierarchy) { - var color = screenObject.color; + Color color = screenObject.color; color.a = alpha; screenObject.color = color; } @@ -267,7 +267,7 @@ private void UpdateColorTextMeshProUGUI(TMPro.TextMeshProUGUI screenObject, floa { if (screenObject.gameObject.activeInHierarchy) { - var color = screenObject.color; + Color color = screenObject.color; color.a = alpha; screenObject.color = color; } From e94466b988f9c017cd8a165eb29d594108ad4f6b Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 14:44:14 +0200 Subject: [PATCH 29/31] More explicit vars --- Fika.Core/UI/FikaUIUtils.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Fika.Core/UI/FikaUIUtils.cs b/Fika.Core/UI/FikaUIUtils.cs index 3a96d603..957870d9 100644 --- a/Fika.Core/UI/FikaUIUtils.cs +++ b/Fika.Core/UI/FikaUIUtils.cs @@ -10,22 +10,22 @@ internal class FikaUIUtils { public static TextMeshProUGUI CreateOverlayText(string overlayText) { - var obj = GameObject.Find("/Preloader UI/Preloader UI/Watermark"); - var labelObj = GameObject.Find("/Preloader UI/Preloader UI/Watermark/Label"); + GameObject obj = GameObject.Find("/Preloader UI/Preloader UI/Watermark"); + GameObject labelObj = GameObject.Find("/Preloader UI/Preloader UI/Watermark/Label"); if (labelObj != null) { Object.Destroy(labelObj); } - var watermarkText = obj.GetComponent(); + ClientWatermark watermarkText = obj.GetComponent(); if (watermarkText != null) { Object.Destroy(watermarkText); } obj.active = true; - var text = obj.AddComponent(); + TextMeshProUGUI text = obj.AddComponent(); text.horizontalAlignment = HorizontalAlignmentOptions.Center; text.verticalAlignment = VerticalAlignmentOptions.Bottom; text.margin = new Vector4(0, 0, 0, -350); From ad8406e2d6ae1aa1f5bfe7919c94809c06adf58d Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 14:44:24 +0200 Subject: [PATCH 30/31] Update extract text when using modifiers --- Fika.Core/Coop/FreeCamera/FreeCameraController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fika.Core/Coop/FreeCamera/FreeCameraController.cs b/Fika.Core/Coop/FreeCamera/FreeCameraController.cs index 29750ec4..f1cd7c20 100644 --- a/Fika.Core/Coop/FreeCamera/FreeCameraController.cs +++ b/Fika.Core/Coop/FreeCamera/FreeCameraController.cs @@ -236,7 +236,7 @@ private void ShowExtractMessage() if (FikaPlugin.ExtractKey.Value.Modifiers.Count() > 0) { string modifiers = string.Join("+", FikaPlugin.ExtractKey.Value.Modifiers); - text = modifiers + "+" + text; + text = modifiers + " + " + text; } extractText = FikaUIUtils.CreateOverlayText($"Press '{text}' to extract"); } From 00275b88a8a07857f356ab183b34dea88ef6546d Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 6 May 2024 20:07:41 +0200 Subject: [PATCH 31/31] 3s > 5s timeout --- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 5808c542..3e2205dd 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -163,7 +163,7 @@ private IEnumerator JoinMatch(string profileId, string serverId, Button button) success = pingingClient.Received; } yield return new WaitForSeconds(0.1f); - } while (!success && attempts < 30); + } while (!success && attempts < 50); if (!success) {