From 7c57b1b69d92976182ea2b1ed3bcf4164e5d8857 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 14 Feb 2022 15:15:18 -0800 Subject: [PATCH 1/5] better meteor patches --- .../Messages/FragmentInitialStateMessage.cs | 2 + .../Messages/MeteorLaunchMessage.cs | 10 +- QSB/MeteorSync/Patches/MeteorPatches.cs | 172 ++++++------------ QSB/MeteorSync/WorldObjects/QSBFragment.cs | 18 +- QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 6 +- .../WorldObjects/QSBMeteorLauncher.cs | 28 ++- 6 files changed, 104 insertions(+), 132 deletions(-) diff --git a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs index a72be153..0c474671 100644 --- a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs +++ b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs @@ -7,8 +7,10 @@ using UnityEngine; namespace QSB.MeteorSync.Messages { + /// /// called when we request a resync on client join /// pain + /// public class FragmentInitialStateMessage : QSBWorldObjectMessage { private float Integrity; diff --git a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs index 657a21ce..5ec4ff9b 100644 --- a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs +++ b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs @@ -1,6 +1,7 @@ using Mirror; using QSB.Messaging; using QSB.MeteorSync.WorldObjects; +using QSB.WorldSync; namespace QSB.MeteorSync.Messages { @@ -9,10 +10,10 @@ namespace QSB.MeteorSync.Messages private int MeteorId; private float LaunchSpeed; - public MeteorLaunchMessage(QSBMeteorLauncher qsbMeteorLauncher) + public MeteorLaunchMessage(MeteorController meteorController, float launchSpeed) { - MeteorId = qsbMeteorLauncher.MeteorId; - LaunchSpeed = qsbMeteorLauncher.LaunchSpeed; + MeteorId = meteorController.GetWorldObject().ObjectId; + LaunchSpeed = launchSpeed; } public override void Serialize(NetworkWriter writer) @@ -29,6 +30,7 @@ namespace QSB.MeteorSync.Messages LaunchSpeed = reader.Read(); } - public override void OnReceiveRemote() => WorldObject.LaunchMeteor(MeteorId, LaunchSpeed); + public override void OnReceiveRemote() => + WorldObject.LaunchMeteor(MeteorId.GetWorldObject(), LaunchSpeed); } } diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 90aa4fdd..1e33f6f1 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -1,10 +1,8 @@ using HarmonyLib; -using OWML.Common; using QSB.Messaging; using QSB.MeteorSync.Messages; using QSB.MeteorSync.WorldObjects; using QSB.Patches; -using QSB.Utility; using QSB.WorldSync; using UnityEngine; @@ -68,8 +66,7 @@ namespace QSB.MeteorSync.Patches particleSystem.Play(); } - var qsbMeteorLauncher = __instance.GetWorldObject(); - qsbMeteorLauncher.SendMessage(new MeteorPreLaunchMessage()); + __instance.GetWorldObject().SendMessage(new MeteorPreLaunchMessage()); } if (Time.time > __instance._lastLaunchTime + __instance._launchDelay + 2.3f) @@ -92,6 +89,11 @@ namespace QSB.MeteorSync.Patches [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.LaunchMeteor))] public static bool LaunchMeteor(MeteorLauncher __instance) { + if (!QSBWorldSync.AllObjectsReady) + { + return true; + } + var flag = __instance._dynamicMeteorPool != null && (__instance._meteorPool == null || Random.value < __instance._dynamicProbability); MeteorController meteorController = null; if (!flag) @@ -125,13 +127,8 @@ namespace QSB.MeteorSync.Patches if (meteorController != null) { - var qsbMeteorLauncher = __instance.GetWorldObject(); - var qsbMeteor = meteorController.GetWorldObject(); - - qsbMeteorLauncher.MeteorId = qsbMeteor.ObjectId; - qsbMeteorLauncher.LaunchSpeed = Random.Range(__instance._minLaunchSpeed, __instance._maxLaunchSpeed); - - var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + (__instance.transform.TransformDirection(__instance._launchDirection) * qsbMeteorLauncher.LaunchSpeed); + var launchSpeed = Random.Range(__instance._minLaunchSpeed, __instance._maxLaunchSpeed); + var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + __instance.transform.TransformDirection(__instance._launchDirection) * launchSpeed; var angularVelocity = __instance.transform.forward * 2f; meteorController.Launch(null, __instance.transform.position, __instance.transform.rotation, linearVelocity, angularVelocity); if (__instance._audioSector.ContainsOccupant(DynamicOccupant.Player)) @@ -140,31 +137,35 @@ namespace QSB.MeteorSync.Patches __instance._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); } - qsbMeteorLauncher.SendMessage(new MeteorLaunchMessage(qsbMeteorLauncher)); + __instance.GetWorldObject().SendMessage(new MeteorLaunchMessage(meteorController, launchSpeed)); } return false; } - [HarmonyPostfix] - [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] - public static void Impact(MeteorController __instance, - GameObject hitObject, Vector3 impactPoint, Vector3 impactVel) - { - var qsbMeteor = __instance.GetWorldObject(); - if (QSBMeteor.IsSpecialImpact(hitObject)) - { - qsbMeteor.SendMessage(new MeteorSpecialImpactMessage()); - } - } - - [HarmonyPostfix] + [HarmonyPrefix] [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] - public static void AddDamage(FragmentIntegrity __instance, + public static bool AddDamage(FragmentIntegrity __instance, float damage) { - var qsbFragment = __instance.GetWorldObject(); - qsbFragment.SendMessage(new FragmentDamageMessage(damage)); + if (__instance._integrity <= 0f) + { + return false; + } + + __instance._integrity = __instance.CanBreak() ? + Mathf.Max(0f, __instance._integrity - damage * __instance.DamageMultiplier()) : + Mathf.Max(0f, __instance._integrity - Mathf.Min(damage * __instance.DamageMultiplier(), __instance._integrity / 2f)); + + if (__instance._integrity == 0f && __instance._motherFragment != null) + { + __instance._motherFragment.ChildIsBroken(); + } + + __instance.CallOnTakeDamage(); + __instance.GetWorldObject().SendMessage(new FragmentDamageMessage(damage)); + + return false; } } @@ -177,97 +178,11 @@ namespace QSB.MeteorSync.Patches [HarmonyPrefix] [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.FixedUpdate))] - public static bool FixedUpdate(MeteorLauncher __instance) - => false; + public static bool FixedUpdate(MeteorLauncher __instance) => false; [HarmonyPrefix] - [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.LaunchMeteor))] - public static bool LaunchMeteor(MeteorLauncher __instance) - { - var qsbMeteorLauncher = __instance.GetWorldObject(); - - MeteorController meteorController = null; - QSBMeteor qsbMeteor; - - bool MeteorMatches(MeteorController x) - { - qsbMeteor = x.GetWorldObject(); - return qsbMeteor.ObjectId == qsbMeteorLauncher.MeteorId; - } - - if (__instance._meteorPool != null) - { - meteorController = __instance._meteorPool.Find(MeteorMatches); - if (meteorController != null) - { - meteorController.Initialize(__instance.transform, __instance._detectableField, __instance._detectableFluid); - } - } - else if (__instance._dynamicMeteorPool != null) - { - meteorController = __instance._dynamicMeteorPool.Find(MeteorMatches); - if (meteorController != null) - { - meteorController.Initialize(__instance.transform, null, null); - } - } - - if (meteorController != null) - { - var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + (__instance.transform.TransformDirection(__instance._launchDirection) * qsbMeteorLauncher.LaunchSpeed); - var angularVelocity = __instance.transform.forward * 2f; - meteorController.Launch(null, __instance.transform.position, __instance.transform.rotation, linearVelocity, angularVelocity); - if (__instance._audioSector.ContainsOccupant(DynamicOccupant.Player)) - { - __instance._launchSource.pitch = Random.Range(0.4f, 0.6f); - __instance._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); - } - } - else - { - DebugLog.ToConsole($"{qsbMeteorLauncher} - could not find meteor {qsbMeteorLauncher.MeteorId} in pool", MessageType.Warning); - } - - return false; - } - - [HarmonyPrefix] - [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] - public static bool Impact(MeteorController __instance, - GameObject hitObject, Vector3 impactPoint, Vector3 impactVel) - { - __instance._intactRenderer.enabled = false; - __instance._impactLight.enabled = true; - __instance._impactLight.intensity = __instance._impactLightCurve.Evaluate(0f); - var rotation = Quaternion.LookRotation(impactVel); - foreach (var particleSystem in __instance._impactParticles) - { - particleSystem.transform.rotation = rotation; - particleSystem.Play(); - } - - __instance._impactSource.PlayOneShot(AudioType.BH_MeteorImpact); - foreach (var owCollider in __instance._owColliders) - { - owCollider.SetActivation(false); - } - - __instance._owRigidbody.MakeKinematic(); - __instance.transform.SetParent(hitObject.GetAttachedOWRigidbody().transform); - FragmentSurfaceProxy.UntrackMeteor(__instance); - FragmentCollisionProxy.UntrackMeteor(__instance); - __instance._ignoringCollisions = false; - __instance._hasImpacted = true; - __instance._impactTime = Time.time; - - var qsbMeteor = __instance.GetWorldObject(); - if (QSBMeteor.IsSpecialImpact(hitObject)) - { - qsbMeteor.SendMessage(new MeteorSpecialImpactMessage()); - } - - return false; - } + [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] + public static bool AddDamage() => false; } /// @@ -277,6 +192,22 @@ namespace QSB.MeteorSync.Patches { public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; + [HarmonyPostfix] + [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] + public static void Impact(MeteorController __instance, + GameObject hitObject) + { + if (!QSBWorldSync.AllObjectsReady) + { + return; + } + + if (QSBMeteor.IsSpecialImpact(hitObject)) + { + __instance.GetWorldObject().SendMessage(new MeteorSpecialImpactMessage()); + } + } + [HarmonyPrefix] [HarmonyPatch(typeof(DetachableFragment), nameof(DetachableFragment.Detach))] public static void Detach_Prefix(DetachableFragment __instance, out FragmentIntegrity __state) => @@ -298,6 +229,11 @@ namespace QSB.MeteorSync.Patches return true; } + if (!QSBWorldSync.AllObjectsReady) + { + return true; + } + var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); if (__instance.enabled) @@ -360,4 +296,4 @@ namespace QSB.MeteorSync.Patches return false; } } -} \ No newline at end of file +} diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index 10cfd586..afcae573 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -38,6 +38,22 @@ namespace QSB.MeteorSync.WorldObjects public float LeashLength; public void AddDamage(float damage) - => AttachedObject.AddDamage(damage); + { + if (AttachedObject._integrity <= 0f) + { + return; + } + + AttachedObject._integrity = AttachedObject.CanBreak() ? + Mathf.Max(0f, AttachedObject._integrity - damage * AttachedObject.DamageMultiplier()) : + Mathf.Max(0f, AttachedObject._integrity - Mathf.Min(damage * AttachedObject.DamageMultiplier(), AttachedObject._integrity / 2f)); + + if (AttachedObject._integrity == 0f && AttachedObject._motherFragment != null) + { + AttachedObject._motherFragment.ChildIsBroken(); + } + + AttachedObject.CallOnTakeDamage(); + } } } diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index 5afd2e92..d4b2fa0e 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -10,8 +10,12 @@ namespace QSB.MeteorSync.WorldObjects // todo SendInitialState } + /// + /// for syncing impacts from remote player and probe + /// public static bool IsSpecialImpact(GameObject go) => - go == Locator.GetPlayerCollider().gameObject || (Locator.GetProbe() != null && go == Locator.GetProbe()._anchor._collider.gameObject); + go == Locator.GetPlayerCollider().gameObject || + Locator.GetProbe() && go == Locator.GetProbe()._anchor._collider.gameObject; public void SpecialImpact() { diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs index c1f42d70..64993915 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs @@ -1,4 +1,7 @@ -using QSB.WorldSync; +using OWML.Common; +using QSB.Utility; +using QSB.WorldSync; +using UnityEngine; namespace QSB.MeteorSync.WorldObjects { @@ -9,9 +12,6 @@ namespace QSB.MeteorSync.WorldObjects // todo SendInitialState } - public int MeteorId; - public float LaunchSpeed; - public void PreLaunchMeteor() { foreach (var particleSystem in AttachedObject._launchParticles) @@ -20,12 +20,24 @@ namespace QSB.MeteorSync.WorldObjects } } - public void LaunchMeteor(int meteorId, float launchSpeed) + public void LaunchMeteor(QSBMeteor qsbMeteor, float launchSpeed) { - MeteorId = meteorId; - LaunchSpeed = launchSpeed; + var meteorController = qsbMeteor.AttachedObject; + if (meteorController.hasLaunched) + { + DebugLog.DebugWrite($"{qsbMeteor} of {this} has already launched", MessageType.Warning); + return; + } + + var linearVelocity = AttachedObject._parentBody.GetPointVelocity(AttachedObject.transform.position) + AttachedObject.transform.TransformDirection(AttachedObject._launchDirection) * launchSpeed; + var angularVelocity = AttachedObject.transform.forward * 2f; + meteorController.Launch(null, AttachedObject.transform.position, AttachedObject.transform.rotation, linearVelocity, angularVelocity); + if (AttachedObject._audioSector.ContainsOccupant(DynamicOccupant.Player)) + { + AttachedObject._launchSource.pitch = Random.Range(0.4f, 0.6f); + AttachedObject._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); + } - AttachedObject.LaunchMeteor(); foreach (var particleSystem in AttachedObject._launchParticles) { particleSystem.Stop(); From befeb84f629ec139b61e9128b1bb84003cc071e9 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 14 Feb 2022 15:19:33 -0800 Subject: [PATCH 2/5] extra check --- QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index d4b2fa0e..4f88d47a 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -1,4 +1,6 @@ -using QSB.WorldSync; +using OWML.Common; +using QSB.Utility; +using QSB.WorldSync; using UnityEngine; namespace QSB.MeteorSync.WorldObjects @@ -19,6 +21,12 @@ namespace QSB.MeteorSync.WorldObjects public void SpecialImpact() { + if (AttachedObject.hasImpacted) + { + DebugLog.DebugWrite($"{this} has already impacted", MessageType.Warning); + return; + } + AttachedObject._intactRenderer.enabled = false; AttachedObject._impactLight.enabled = true; AttachedObject._impactLight.intensity = AttachedObject._impactLightCurve.Evaluate(0f); From 6ce6b7cec343fa695e9af94e15b2f77e6314598d Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 14 Feb 2022 15:25:42 -0800 Subject: [PATCH 3/5] Revert "extra check" This reverts commit befeb84f629ec139b61e9128b1bb84003cc071e9. --- QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index 4f88d47a..d4b2fa0e 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -1,6 +1,4 @@ -using OWML.Common; -using QSB.Utility; -using QSB.WorldSync; +using QSB.WorldSync; using UnityEngine; namespace QSB.MeteorSync.WorldObjects @@ -21,12 +19,6 @@ namespace QSB.MeteorSync.WorldObjects public void SpecialImpact() { - if (AttachedObject.hasImpacted) - { - DebugLog.DebugWrite($"{this} has already impacted", MessageType.Warning); - return; - } - AttachedObject._intactRenderer.enabled = false; AttachedObject._impactLight.enabled = true; AttachedObject._impactLight.intensity = AttachedObject._impactLightCurve.Evaluate(0f); From a3ab23395f4fb2338feded9fa80de0462e225b59 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 14 Feb 2022 15:25:43 -0800 Subject: [PATCH 4/5] Revert "better meteor patches" This reverts commit 7c57b1b69d92976182ea2b1ed3bcf4164e5d8857. --- .../Messages/FragmentInitialStateMessage.cs | 2 - .../Messages/MeteorLaunchMessage.cs | 10 +- QSB/MeteorSync/Patches/MeteorPatches.cs | 172 ++++++++++++------ QSB/MeteorSync/WorldObjects/QSBFragment.cs | 18 +- QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 6 +- .../WorldObjects/QSBMeteorLauncher.cs | 28 +-- 6 files changed, 132 insertions(+), 104 deletions(-) diff --git a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs index 0c474671..a72be153 100644 --- a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs +++ b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs @@ -7,10 +7,8 @@ using UnityEngine; namespace QSB.MeteorSync.Messages { - /// /// called when we request a resync on client join /// pain - /// public class FragmentInitialStateMessage : QSBWorldObjectMessage { private float Integrity; diff --git a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs index 5ec4ff9b..657a21ce 100644 --- a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs +++ b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs @@ -1,7 +1,6 @@ using Mirror; using QSB.Messaging; using QSB.MeteorSync.WorldObjects; -using QSB.WorldSync; namespace QSB.MeteorSync.Messages { @@ -10,10 +9,10 @@ namespace QSB.MeteorSync.Messages private int MeteorId; private float LaunchSpeed; - public MeteorLaunchMessage(MeteorController meteorController, float launchSpeed) + public MeteorLaunchMessage(QSBMeteorLauncher qsbMeteorLauncher) { - MeteorId = meteorController.GetWorldObject().ObjectId; - LaunchSpeed = launchSpeed; + MeteorId = qsbMeteorLauncher.MeteorId; + LaunchSpeed = qsbMeteorLauncher.LaunchSpeed; } public override void Serialize(NetworkWriter writer) @@ -30,7 +29,6 @@ namespace QSB.MeteorSync.Messages LaunchSpeed = reader.Read(); } - public override void OnReceiveRemote() => - WorldObject.LaunchMeteor(MeteorId.GetWorldObject(), LaunchSpeed); + public override void OnReceiveRemote() => WorldObject.LaunchMeteor(MeteorId, LaunchSpeed); } } diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 1e33f6f1..90aa4fdd 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -1,8 +1,10 @@ using HarmonyLib; +using OWML.Common; using QSB.Messaging; using QSB.MeteorSync.Messages; using QSB.MeteorSync.WorldObjects; using QSB.Patches; +using QSB.Utility; using QSB.WorldSync; using UnityEngine; @@ -66,7 +68,8 @@ namespace QSB.MeteorSync.Patches particleSystem.Play(); } - __instance.GetWorldObject().SendMessage(new MeteorPreLaunchMessage()); + var qsbMeteorLauncher = __instance.GetWorldObject(); + qsbMeteorLauncher.SendMessage(new MeteorPreLaunchMessage()); } if (Time.time > __instance._lastLaunchTime + __instance._launchDelay + 2.3f) @@ -89,11 +92,6 @@ namespace QSB.MeteorSync.Patches [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.LaunchMeteor))] public static bool LaunchMeteor(MeteorLauncher __instance) { - if (!QSBWorldSync.AllObjectsReady) - { - return true; - } - var flag = __instance._dynamicMeteorPool != null && (__instance._meteorPool == null || Random.value < __instance._dynamicProbability); MeteorController meteorController = null; if (!flag) @@ -127,8 +125,13 @@ namespace QSB.MeteorSync.Patches if (meteorController != null) { - var launchSpeed = Random.Range(__instance._minLaunchSpeed, __instance._maxLaunchSpeed); - var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + __instance.transform.TransformDirection(__instance._launchDirection) * launchSpeed; + var qsbMeteorLauncher = __instance.GetWorldObject(); + var qsbMeteor = meteorController.GetWorldObject(); + + qsbMeteorLauncher.MeteorId = qsbMeteor.ObjectId; + qsbMeteorLauncher.LaunchSpeed = Random.Range(__instance._minLaunchSpeed, __instance._maxLaunchSpeed); + + var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + (__instance.transform.TransformDirection(__instance._launchDirection) * qsbMeteorLauncher.LaunchSpeed); var angularVelocity = __instance.transform.forward * 2f; meteorController.Launch(null, __instance.transform.position, __instance.transform.rotation, linearVelocity, angularVelocity); if (__instance._audioSector.ContainsOccupant(DynamicOccupant.Player)) @@ -137,35 +140,31 @@ namespace QSB.MeteorSync.Patches __instance._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); } - __instance.GetWorldObject().SendMessage(new MeteorLaunchMessage(meteorController, launchSpeed)); + qsbMeteorLauncher.SendMessage(new MeteorLaunchMessage(qsbMeteorLauncher)); } return false; } - [HarmonyPrefix] + [HarmonyPostfix] + [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] + public static void Impact(MeteorController __instance, + GameObject hitObject, Vector3 impactPoint, Vector3 impactVel) + { + var qsbMeteor = __instance.GetWorldObject(); + if (QSBMeteor.IsSpecialImpact(hitObject)) + { + qsbMeteor.SendMessage(new MeteorSpecialImpactMessage()); + } + } + + [HarmonyPostfix] [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] - public static bool AddDamage(FragmentIntegrity __instance, + public static void AddDamage(FragmentIntegrity __instance, float damage) { - if (__instance._integrity <= 0f) - { - return false; - } - - __instance._integrity = __instance.CanBreak() ? - Mathf.Max(0f, __instance._integrity - damage * __instance.DamageMultiplier()) : - Mathf.Max(0f, __instance._integrity - Mathf.Min(damage * __instance.DamageMultiplier(), __instance._integrity / 2f)); - - if (__instance._integrity == 0f && __instance._motherFragment != null) - { - __instance._motherFragment.ChildIsBroken(); - } - - __instance.CallOnTakeDamage(); - __instance.GetWorldObject().SendMessage(new FragmentDamageMessage(damage)); - - return false; + var qsbFragment = __instance.GetWorldObject(); + qsbFragment.SendMessage(new FragmentDamageMessage(damage)); } } @@ -178,11 +177,97 @@ namespace QSB.MeteorSync.Patches [HarmonyPrefix] [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.FixedUpdate))] - public static bool FixedUpdate(MeteorLauncher __instance) => false; + public static bool FixedUpdate(MeteorLauncher __instance) + => false; [HarmonyPrefix] - [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] - public static bool AddDamage() => false; + [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.LaunchMeteor))] + public static bool LaunchMeteor(MeteorLauncher __instance) + { + var qsbMeteorLauncher = __instance.GetWorldObject(); + + MeteorController meteorController = null; + QSBMeteor qsbMeteor; + + bool MeteorMatches(MeteorController x) + { + qsbMeteor = x.GetWorldObject(); + return qsbMeteor.ObjectId == qsbMeteorLauncher.MeteorId; + } + + if (__instance._meteorPool != null) + { + meteorController = __instance._meteorPool.Find(MeteorMatches); + if (meteorController != null) + { + meteorController.Initialize(__instance.transform, __instance._detectableField, __instance._detectableFluid); + } + } + else if (__instance._dynamicMeteorPool != null) + { + meteorController = __instance._dynamicMeteorPool.Find(MeteorMatches); + if (meteorController != null) + { + meteorController.Initialize(__instance.transform, null, null); + } + } + + if (meteorController != null) + { + var linearVelocity = __instance._parentBody.GetPointVelocity(__instance.transform.position) + (__instance.transform.TransformDirection(__instance._launchDirection) * qsbMeteorLauncher.LaunchSpeed); + var angularVelocity = __instance.transform.forward * 2f; + meteorController.Launch(null, __instance.transform.position, __instance.transform.rotation, linearVelocity, angularVelocity); + if (__instance._audioSector.ContainsOccupant(DynamicOccupant.Player)) + { + __instance._launchSource.pitch = Random.Range(0.4f, 0.6f); + __instance._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); + } + } + else + { + DebugLog.ToConsole($"{qsbMeteorLauncher} - could not find meteor {qsbMeteorLauncher.MeteorId} in pool", MessageType.Warning); + } + + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] + public static bool Impact(MeteorController __instance, + GameObject hitObject, Vector3 impactPoint, Vector3 impactVel) + { + __instance._intactRenderer.enabled = false; + __instance._impactLight.enabled = true; + __instance._impactLight.intensity = __instance._impactLightCurve.Evaluate(0f); + var rotation = Quaternion.LookRotation(impactVel); + foreach (var particleSystem in __instance._impactParticles) + { + particleSystem.transform.rotation = rotation; + particleSystem.Play(); + } + + __instance._impactSource.PlayOneShot(AudioType.BH_MeteorImpact); + foreach (var owCollider in __instance._owColliders) + { + owCollider.SetActivation(false); + } + + __instance._owRigidbody.MakeKinematic(); + __instance.transform.SetParent(hitObject.GetAttachedOWRigidbody().transform); + FragmentSurfaceProxy.UntrackMeteor(__instance); + FragmentCollisionProxy.UntrackMeteor(__instance); + __instance._ignoringCollisions = false; + __instance._hasImpacted = true; + __instance._impactTime = Time.time; + + var qsbMeteor = __instance.GetWorldObject(); + if (QSBMeteor.IsSpecialImpact(hitObject)) + { + qsbMeteor.SendMessage(new MeteorSpecialImpactMessage()); + } + + return false; + } } /// @@ -192,22 +277,6 @@ namespace QSB.MeteorSync.Patches { public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; - [HarmonyPostfix] - [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] - public static void Impact(MeteorController __instance, - GameObject hitObject) - { - if (!QSBWorldSync.AllObjectsReady) - { - return; - } - - if (QSBMeteor.IsSpecialImpact(hitObject)) - { - __instance.GetWorldObject().SendMessage(new MeteorSpecialImpactMessage()); - } - } - [HarmonyPrefix] [HarmonyPatch(typeof(DetachableFragment), nameof(DetachableFragment.Detach))] public static void Detach_Prefix(DetachableFragment __instance, out FragmentIntegrity __state) => @@ -229,11 +298,6 @@ namespace QSB.MeteorSync.Patches return true; } - if (!QSBWorldSync.AllObjectsReady) - { - return true; - } - var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); if (__instance.enabled) @@ -296,4 +360,4 @@ namespace QSB.MeteorSync.Patches return false; } } -} +} \ No newline at end of file diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index afcae573..10cfd586 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -38,22 +38,6 @@ namespace QSB.MeteorSync.WorldObjects public float LeashLength; public void AddDamage(float damage) - { - if (AttachedObject._integrity <= 0f) - { - return; - } - - AttachedObject._integrity = AttachedObject.CanBreak() ? - Mathf.Max(0f, AttachedObject._integrity - damage * AttachedObject.DamageMultiplier()) : - Mathf.Max(0f, AttachedObject._integrity - Mathf.Min(damage * AttachedObject.DamageMultiplier(), AttachedObject._integrity / 2f)); - - if (AttachedObject._integrity == 0f && AttachedObject._motherFragment != null) - { - AttachedObject._motherFragment.ChildIsBroken(); - } - - AttachedObject.CallOnTakeDamage(); - } + => AttachedObject.AddDamage(damage); } } diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index d4b2fa0e..5afd2e92 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -10,12 +10,8 @@ namespace QSB.MeteorSync.WorldObjects // todo SendInitialState } - /// - /// for syncing impacts from remote player and probe - /// public static bool IsSpecialImpact(GameObject go) => - go == Locator.GetPlayerCollider().gameObject || - Locator.GetProbe() && go == Locator.GetProbe()._anchor._collider.gameObject; + go == Locator.GetPlayerCollider().gameObject || (Locator.GetProbe() != null && go == Locator.GetProbe()._anchor._collider.gameObject); public void SpecialImpact() { diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs index 64993915..c1f42d70 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs @@ -1,7 +1,4 @@ -using OWML.Common; -using QSB.Utility; -using QSB.WorldSync; -using UnityEngine; +using QSB.WorldSync; namespace QSB.MeteorSync.WorldObjects { @@ -12,6 +9,9 @@ namespace QSB.MeteorSync.WorldObjects // todo SendInitialState } + public int MeteorId; + public float LaunchSpeed; + public void PreLaunchMeteor() { foreach (var particleSystem in AttachedObject._launchParticles) @@ -20,24 +20,12 @@ namespace QSB.MeteorSync.WorldObjects } } - public void LaunchMeteor(QSBMeteor qsbMeteor, float launchSpeed) + public void LaunchMeteor(int meteorId, float launchSpeed) { - var meteorController = qsbMeteor.AttachedObject; - if (meteorController.hasLaunched) - { - DebugLog.DebugWrite($"{qsbMeteor} of {this} has already launched", MessageType.Warning); - return; - } - - var linearVelocity = AttachedObject._parentBody.GetPointVelocity(AttachedObject.transform.position) + AttachedObject.transform.TransformDirection(AttachedObject._launchDirection) * launchSpeed; - var angularVelocity = AttachedObject.transform.forward * 2f; - meteorController.Launch(null, AttachedObject.transform.position, AttachedObject.transform.rotation, linearVelocity, angularVelocity); - if (AttachedObject._audioSector.ContainsOccupant(DynamicOccupant.Player)) - { - AttachedObject._launchSource.pitch = Random.Range(0.4f, 0.6f); - AttachedObject._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); - } + MeteorId = meteorId; + LaunchSpeed = launchSpeed; + AttachedObject.LaunchMeteor(); foreach (var particleSystem in AttachedObject._launchParticles) { particleSystem.Stop(); From 1209151df2d50cb6606f104abad4b34b74b5dd36 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Tue, 15 Feb 2022 00:00:22 -0800 Subject: [PATCH 5/5] QSBNetworkManager: ACTUALLY dont do authority reversion if we are the host --- QSB/QSBNetworkManager.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/QSB/QSBNetworkManager.cs b/QSB/QSBNetworkManager.cs index e0240266..3bd32bce 100644 --- a/QSB/QSBNetworkManager.cs +++ b/QSB/QSBNetworkManager.cs @@ -279,7 +279,8 @@ namespace QSB { DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info); - if (!QSBCore.IsHost) + // local conn = we are host, so skip + if (conn is not LocalConnectionToClient) { // revert authority from ship if (ShipTransformSync.LocalInstance != null)