From 25a207a3259920d02fdfd8a67bd9f1974a43e78d Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 19:17:06 -0800 Subject: [PATCH 01/13] meteors: improve patches --- .../Messages/MeteorLaunchMessage.cs | 39 ++--- QSB/MeteorSync/Patches/MeteorPatches.cs | 143 ++++++------------ QSB/MeteorSync/WorldObjects/QSBFragment.cs | 2 + QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 15 +- .../WorldObjects/QSBMeteorLauncher.cs | 43 ++++-- 5 files changed, 100 insertions(+), 142 deletions(-) diff --git a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs index 1efc4331..5bc2c14d 100644 --- a/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs +++ b/QSB/MeteorSync/Messages/MeteorLaunchMessage.cs @@ -1,33 +1,18 @@ -using Mirror; -using QSB.Messaging; +using QSB.Messaging; using QSB.MeteorSync.WorldObjects; +using QSB.WorldSync; namespace QSB.MeteorSync.Messages; -public class MeteorLaunchMessage : QSBWorldObjectMessage +public class MeteorLaunchMessage : QSBWorldObjectMessage { - private int MeteorId; - private float LaunchSpeed; + public MeteorLaunchMessage(MeteorController meteor, float launchSpeed) : base(( + meteor.GetWorldObject().ObjectId, + launchSpeed + )) { } - public MeteorLaunchMessage(QSBMeteorLauncher qsbMeteorLauncher) - { - MeteorId = qsbMeteorLauncher.MeteorId; - LaunchSpeed = qsbMeteorLauncher.LaunchSpeed; - } - - public override void Serialize(NetworkWriter writer) - { - base.Serialize(writer); - writer.Write(MeteorId); - writer.Write(LaunchSpeed); - } - - public override void Deserialize(NetworkReader reader) - { - base.Deserialize(reader); - MeteorId = reader.Read(); - LaunchSpeed = reader.Read(); - } - - public override void OnReceiveRemote() => WorldObject.LaunchMeteor(MeteorId, LaunchSpeed); -} \ No newline at end of file + public override void OnReceiveRemote() => WorldObject.LaunchMeteor( + Data.MeteorId.GetWorldObject().AttachedObject, + Data.LaunchSpeed + ); +} diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 852fb16f..36e6246a 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; @@ -19,7 +17,7 @@ public class MeteorServerPatches : QSBPatch [HarmonyPrefix] [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.FixedUpdate))] - public static bool FixedUpdate(MeteorLauncher __instance) + public static bool MeteorLauncher_FixedUpdate(MeteorLauncher __instance) { if (!QSBWorldSync.AllObjectsReady) { @@ -63,13 +61,13 @@ public class MeteorServerPatches : QSBPatch if (!__instance._areParticlesPlaying) { __instance._areParticlesPlaying = true; - foreach (var particleSystem in __instance._launchParticles) + foreach (var launchParticle in __instance._launchParticles) { - particleSystem.Play(); + launchParticle.Play(); } - var qsbMeteorLauncher = __instance.GetWorldObject(); - qsbMeteorLauncher.SendMessage(new MeteorPreLaunchMessage()); + __instance.GetWorldObject() + .SendMessage(new MeteorPreLaunchMessage()); } if (Time.time > __instance._lastLaunchTime + __instance._launchDelay + 2.3f) @@ -78,9 +76,9 @@ public class MeteorServerPatches : QSBPatch __instance._lastLaunchTime = Time.time; __instance._launchDelay = Random.Range(__instance._minInterval, __instance._maxInterval); __instance._areParticlesPlaying = false; - foreach (var particleSystem in __instance._launchParticles) + foreach (var launchParticle in __instance._launchParticles) { - particleSystem.Stop(); + launchParticle.Stop(); } } } @@ -90,7 +88,7 @@ public class MeteorServerPatches : QSBPatch [HarmonyPrefix] [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.LaunchMeteor))] - public static bool LaunchMeteor(MeteorLauncher __instance) + public static bool MeteorLauncher_LaunchMeteor(MeteorLauncher __instance) { var flag = __instance._dynamicMeteorPool != null && (__instance._meteorPool == null || Random.value < __instance._dynamicProbability); MeteorController meteorController = null; @@ -125,13 +123,9 @@ public class MeteorServerPatches : QSBPatch if (meteorController != null) { - var qsbMeteorLauncher = __instance.GetWorldObject(); - var qsbMeteor = meteorController.GetWorldObject(); + var launchSpeed = Random.Range(__instance._minLaunchSpeed, __instance._maxLaunchSpeed); - 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 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 +134,25 @@ public class MeteorServerPatches : QSBPatch __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] [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] - public static void AddDamage(FragmentIntegrity __instance, + public static void FragmentIntegrity_AddDamage(FragmentIntegrity __instance, float damage) { - var qsbFragment = __instance.GetWorldObject(); - qsbFragment.SendMessage(new FragmentDamageMessage(damage)); + if (__instance._integrity <= 0f) + { + return; + } + + __instance.GetWorldObject() + .SendMessage(new FragmentDamageMessage(damage)); } } @@ -177,73 +165,22 @@ public class MeteorClientPatches : QSBPatch [HarmonyPrefix] [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.FixedUpdate))] - public static bool FixedUpdate(MeteorLauncher __instance) + public static bool MeteorLauncher_FixedUpdate() => 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, + public static bool MeteorController_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) + foreach (var impactParticle in __instance._impactParticles) { - particleSystem.transform.rotation = rotation; - particleSystem.Play(); + impactParticle.transform.rotation = rotation; + impactParticle.Play(); } __instance._impactSource.PlayOneShot(AudioType.BH_MeteorImpact); @@ -260,12 +197,6 @@ public class MeteorClientPatches : QSBPatch __instance._hasImpacted = true; __instance._impactTime = Time.time; - var qsbMeteor = __instance.GetWorldObject(); - if (QSBMeteor.IsSpecialImpact(hitObject)) - { - qsbMeteor.SendMessage(new MeteorSpecialImpactMessage()); - } - return false; } } @@ -277,20 +208,32 @@ public class MeteorPatches : QSBPatch { public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; + [HarmonyPostfix] + [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] + public static void MeteorController_Impact(MeteorController __instance, + GameObject hitObject, Vector3 impactPoint, Vector3 impactVel) + { + 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) => + public static void DetachableFragment_Detach_Prefix(DetachableFragment __instance, out FragmentIntegrity __state) => // this gets set to null in Detach, so store it here and and then restore it in postfix __state = __instance._fragmentIntegrity; [HarmonyPostfix] [HarmonyPatch(typeof(DetachableFragment), nameof(DetachableFragment.Detach))] - public static void Detach_Postfix(DetachableFragment __instance, FragmentIntegrity __state) => + public static void DetachableFragment_Detach_Postfix(DetachableFragment __instance, FragmentIntegrity __state) => __instance._fragmentIntegrity = __state; [HarmonyPrefix] [HarmonyPatch(typeof(DebrisLeash), nameof(DebrisLeash.MoveByDistance))] - public static bool MoveByDistance(DebrisLeash __instance, + public static bool DebrisLeash_MoveByDistance(DebrisLeash __instance, float distance) { if (__instance._detachableFragment == null || __instance._detachableFragment._fragmentIntegrity == null) @@ -304,7 +247,7 @@ public class MeteorPatches : QSBPatch { var vector = __instance._attachedBody.GetPosition() - __instance._anchorBody.GetPosition(); var d = Mathf.Min(distance, qsbFragment.LeashLength - vector.magnitude); - __instance._attachedBody.SetPosition(__instance._anchorBody.GetPosition() + (vector.normalized * d)); + __instance._attachedBody.SetPosition(__instance._anchorBody.GetPosition() + vector.normalized * d); } return false; @@ -312,7 +255,7 @@ public class MeteorPatches : QSBPatch [HarmonyPrefix] [HarmonyPatch(typeof(DebrisLeash), nameof(DebrisLeash.FixedUpdate))] - public static bool FixedUpdate(DebrisLeash __instance) + public static bool DebrisLeash_FixedUpdate(DebrisLeash __instance) { if (__instance._detachableFragment == null || __instance._detachableFragment._fragmentIntegrity == null) { @@ -359,4 +302,4 @@ public class MeteorPatches : QSBPatch return false; } -} \ No newline at end of file +} diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index c44eb3c9..d5867a52 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -29,7 +29,9 @@ public class QSBFragment : WorldObject public OWRigidbody RefBody => IsThruWhiteHole ? MeteorManager.WhiteHoleVolume._whiteHoleBody : Locator._brittleHollow._owRigidbody; public OWRigidbody Body => IsDetached ? AttachedObject.transform.parent.parent.GetAttachedOWRigidbody() : null; + /// /// what the leash length will be when we eventually detach and fall thru white hole + /// public float LeashLength; public void AddDamage(float damage) diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index 50369c93..68f4fce8 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -1,10 +1,21 @@ -using QSB.WorldSync; +using Cysharp.Threading.Tasks; +using QSB.WorldSync; +using System.Threading; using UnityEngine; namespace QSB.MeteorSync.WorldObjects; public class QSBMeteor : WorldObject { + private QSBMeteorLauncher _qsbMeteorLauncher; + + public override async UniTask Init(CancellationToken ct) + { + var meteorLauncher = AttachedObject._suspendRoot.GetComponent(); + await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct); + _qsbMeteorLauncher = meteorLauncher.GetWorldObject(); + } + public override void SendInitialState(uint to) { // todo SendInitialState @@ -36,4 +47,4 @@ public class QSBMeteor : WorldObject AttachedObject._hasImpacted = true; AttachedObject._impactTime = Time.time; } -} \ No newline at end of file +} diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs index a4ca09df..7be4118e 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs @@ -1,34 +1,51 @@ -using QSB.WorldSync; +using Cysharp.Threading.Tasks; +using QSB.WorldSync; +using System.Linq; +using System.Threading; +using UnityEngine; namespace QSB.MeteorSync.WorldObjects; public class QSBMeteorLauncher : WorldObject { + private QSBMeteor[] _qsbMeteors; + + public override async UniTask Init(CancellationToken ct) + { + var meteors = AttachedObject._meteorPool.Concat(AttachedObject._dynamicMeteorPool); + await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct); + _qsbMeteors = meteors.Select(x => x.GetWorldObject()).ToArray(); + } + public override void SendInitialState(uint to) { // todo SendInitialState } - public int MeteorId; - public float LaunchSpeed; - public void PreLaunchMeteor() { - foreach (var particleSystem in AttachedObject._launchParticles) + foreach (var launchParticle in AttachedObject._launchParticles) { - particleSystem.Play(); + launchParticle.Play(); } } - public void LaunchMeteor(int meteorId, float launchSpeed) + public void LaunchMeteor(MeteorController meteorController, float launchSpeed) { - MeteorId = meteorId; - LaunchSpeed = launchSpeed; + meteorController.Initialize(AttachedObject.transform, AttachedObject._detectableField, AttachedObject._detectableFluid); - AttachedObject.LaunchMeteor(); - foreach (var particleSystem in AttachedObject._launchParticles) + 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)) { - particleSystem.Stop(); + AttachedObject._launchSource.pitch = Random.Range(0.4f, 0.6f); + AttachedObject._launchSource.PlayOneShot(AudioType.BH_MeteorLaunch); + } + + foreach (var launchParticle in AttachedObject._launchParticles) + { + launchParticle.Stop(); } } -} \ No newline at end of file +} From c127e54079853183677933b560c9f463aac9ef74 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 19:34:59 -0800 Subject: [PATCH 02/13] fragments: better debris leash length sync --- QSB/MeteorSync/Patches/MeteorPatches.cs | 76 +++------------------- QSB/MeteorSync/WorldObjects/QSBFragment.cs | 2 +- 2 files changed, 11 insertions(+), 67 deletions(-) diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 36e6246a..ecca07c7 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; @@ -19,11 +21,6 @@ public class MeteorServerPatches : QSBPatch [HarmonyPatch(typeof(MeteorLauncher), nameof(MeteorLauncher.FixedUpdate))] public static bool MeteorLauncher_FixedUpdate(MeteorLauncher __instance) { - if (!QSBWorldSync.AllObjectsReady) - { - return true; - } - if (__instance._launchedMeteors != null) { for (var i = __instance._launchedMeteors.Count - 1; i >= 0; i--) @@ -231,75 +228,22 @@ public class MeteorPatches : QSBPatch public static void DetachableFragment_Detach_Postfix(DetachableFragment __instance, FragmentIntegrity __state) => __instance._fragmentIntegrity = __state; - [HarmonyPrefix] - [HarmonyPatch(typeof(DebrisLeash), nameof(DebrisLeash.MoveByDistance))] - public static bool DebrisLeash_MoveByDistance(DebrisLeash __instance, - float distance) + [HarmonyPostfix] + [HarmonyPatch(typeof(DebrisLeash), nameof(DebrisLeash.Init))] + public static void DebrisLeash_Init(DebrisLeash __instance) { if (__instance._detachableFragment == null || __instance._detachableFragment._fragmentIntegrity == null) { - return true; + return; } var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); - - if (__instance.enabled) + if (qsbFragment.LeashLength == null) { - var vector = __instance._attachedBody.GetPosition() - __instance._anchorBody.GetPosition(); - var d = Mathf.Min(distance, qsbFragment.LeashLength - vector.magnitude); - __instance._attachedBody.SetPosition(__instance._anchorBody.GetPosition() + vector.normalized * d); + DebugLog.ToConsole($"DebrisLeash.Init for {qsbFragment} before LeashLength was set", MessageType.Warning); + return; } - return false; - } - - [HarmonyPrefix] - [HarmonyPatch(typeof(DebrisLeash), nameof(DebrisLeash.FixedUpdate))] - public static bool DebrisLeash_FixedUpdate(DebrisLeash __instance) - { - if (__instance._detachableFragment == null || __instance._detachableFragment._fragmentIntegrity == null) - { - return true; - } - - if (!QSBWorldSync.AllObjectsReady) - { - return true; - } - - var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); - - if (!__instance._deccelerating) - { - var num = Vector3.Distance(__instance._attachedBody.GetPosition(), __instance._anchorBody.GetPosition()); - var num2 = Mathf.Pow(__instance._attachedBody.GetVelocity().magnitude, 2f) / (2f * __instance._deccel); - var vector = __instance._attachedBody.GetVelocity() - __instance._anchorBody.GetVelocity(); - if (num >= qsbFragment.LeashLength - num2 && vector.magnitude > 0.1f) - { - __instance._deccelerating = true; - return false; - } - } - else - { - var vector2 = __instance._attachedBody.GetVelocity() - __instance._anchorBody.GetVelocity(); - var velocityChange = -vector2.normalized * Mathf.Min(__instance._deccel * Time.deltaTime, vector2.magnitude); - if (velocityChange.magnitude < 0.01f) - { - __instance._attachedBody.SetVelocity(__instance._anchorBody.GetVelocity()); - __instance._deccelerating = false; - if (__instance._detachableFragment != null) - { - __instance._detachableFragment.ComeToRest(__instance._anchorBody); - } - - __instance.enabled = false; - return false; - } - - __instance._attachedBody.AddVelocityChange(velocityChange); - } - - return false; + __instance._leashLength = (float)qsbFragment.LeashLength; } } diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index d5867a52..8a331cb6 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -32,7 +32,7 @@ public class QSBFragment : WorldObject /// /// what the leash length will be when we eventually detach and fall thru white hole /// - public float LeashLength; + public float? LeashLength; public void AddDamage(float damage) => AttachedObject.AddDamage(damage); From ea4bc822bd3e71de1d636f5cdd0ba1cddd6867b7 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 19:38:59 -0800 Subject: [PATCH 03/13] QSBMeteor: format --- QSB/MeteorSync/WorldObjects/QSBMeteor.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs index 68f4fce8..d69e5ec8 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteor.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteor.cs @@ -22,16 +22,17 @@ public class QSBMeteor : WorldObject } 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() != null && go == Locator.GetProbe()._anchor._collider.gameObject; public void SpecialImpact() { AttachedObject._intactRenderer.enabled = false; AttachedObject._impactLight.enabled = true; AttachedObject._impactLight.intensity = AttachedObject._impactLightCurve.Evaluate(0f); - foreach (var particleSystem in AttachedObject._impactParticles) + foreach (var impactParticle in AttachedObject._impactParticles) { - particleSystem.Play(); + impactParticle.Play(); } AttachedObject._impactSource.PlayOneShot(AudioType.BH_MeteorImpact); From addb7405238316ab1666fc3889159204262020cd Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 19:47:09 -0800 Subject: [PATCH 04/13] fragment: use SetIntegrity instead of AddDamage --- .../Messages/FragmentDamageMessage.cs | 11 --------- .../Messages/FragmentIntegrityEvent.cs | 10 ++++++++ QSB/MeteorSync/Patches/MeteorPatches.cs | 2 +- QSB/MeteorSync/WorldObjects/QSBFragment.cs | 23 +++++++++++++++---- 4 files changed, 30 insertions(+), 16 deletions(-) delete mode 100644 QSB/MeteorSync/Messages/FragmentDamageMessage.cs create mode 100644 QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs diff --git a/QSB/MeteorSync/Messages/FragmentDamageMessage.cs b/QSB/MeteorSync/Messages/FragmentDamageMessage.cs deleted file mode 100644 index e30f5e5b..00000000 --- a/QSB/MeteorSync/Messages/FragmentDamageMessage.cs +++ /dev/null @@ -1,11 +0,0 @@ -using QSB.Messaging; -using QSB.MeteorSync.WorldObjects; - -namespace QSB.MeteorSync.Messages; - -public class FragmentDamageMessage : QSBWorldObjectMessage -{ - public FragmentDamageMessage(float damage) : base(damage) { } - - public override void OnReceiveRemote() => WorldObject.AddDamage(Data); -} \ No newline at end of file diff --git a/QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs b/QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs new file mode 100644 index 00000000..12020025 --- /dev/null +++ b/QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs @@ -0,0 +1,10 @@ +using QSB.Messaging; +using QSB.MeteorSync.WorldObjects; + +namespace QSB.MeteorSync.Messages; + +public class FragmentIntegrityEvent : QSBWorldObjectMessage +{ + public FragmentIntegrityEvent(float integrity) : base(integrity) { } + public override void OnReceiveRemote() => WorldObject.SetIntegrity(Data); +} diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index ecca07c7..66b3260a 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -149,7 +149,7 @@ public class MeteorServerPatches : QSBPatch } __instance.GetWorldObject() - .SendMessage(new FragmentDamageMessage(damage)); + .SendMessage(new FragmentIntegrityEvent(damage)); } } diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index 8a331cb6..e866d617 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -30,10 +30,25 @@ public class QSBFragment : WorldObject public OWRigidbody Body => IsDetached ? AttachedObject.transform.parent.parent.GetAttachedOWRigidbody() : null; /// - /// what the leash length will be when we eventually detach and fall thru white hole + /// what the leash length will be when we eventually detach and fall thru white hole. + /// + /// generated by the server and sent to clients in the initial state message. /// public float? LeashLength; - public void AddDamage(float damage) - => AttachedObject.AddDamage(damage); -} \ No newline at end of file + public void SetIntegrity(float integrity) + { + if (OWMath.ApproxEquals(AttachedObject._integrity, integrity)) + { + return; + } + + if (AttachedObject._integrity <= 0f) + { + return; + } + + AttachedObject._integrity = integrity; + AttachedObject.CallOnTakeDamage(); + } +} From ff9b1b455594db1b3a178bb92506c6fa3ba860b1 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 20:01:44 -0800 Subject: [PATCH 05/13] fragments: much simpler initial state sync --- .../Messages/FragmentInitialStateMessage.cs | 148 ++---------------- .../Messages/MeteorSpecialImpactMessage.cs | 3 + QSB/MeteorSync/Patches/MeteorPatches.cs | 4 +- QSB/MeteorSync/WorldObjects/QSBFragment.cs | 23 +-- 4 files changed, 27 insertions(+), 151 deletions(-) diff --git a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs index 26692324..6069b1f3 100644 --- a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs +++ b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs @@ -1,147 +1,29 @@ -using Mirror; -using OWML.Common; +using OWML.Common; using QSB.Messaging; using QSB.MeteorSync.WorldObjects; using QSB.Utility; -using UnityEngine; namespace QSB.MeteorSync.Messages; -/// called when we request a resync on client join -/// pain -public class FragmentInitialStateMessage : QSBWorldObjectMessage +public class FragmentInitialStateMessage : QSBWorldObjectMessage { - private float Integrity; - private float OrigIntegrity; - private float LeashLength; - private bool IsDetached; - - private bool IsThruWhiteHole; - private Vector3 RelPos; - private Quaternion RelRot; - private Vector3 RelVel; - private Vector3 RelAngVel; - - public FragmentInitialStateMessage(QSBFragment qsbFragment) - { - Integrity = qsbFragment.AttachedObject._integrity; - OrigIntegrity = qsbFragment.AttachedObject._origIntegrity; - LeashLength = qsbFragment.LeashLength; - IsDetached = qsbFragment.IsDetached; - - if (IsDetached) - { - IsThruWhiteHole = qsbFragment.IsThruWhiteHole; - - var body = qsbFragment.Body; - var refBody = qsbFragment.RefBody; - var pos = body.GetPosition(); - RelPos = refBody.transform.ToRelPos(pos); - RelRot = refBody.transform.ToRelRot(body.GetRotation()); - RelVel = refBody.ToRelVel(body.GetVelocity(), pos); - RelAngVel = refBody.ToRelAngVel(body.GetAngularVelocity()); - } - } - - public override void Serialize(NetworkWriter writer) - { - base.Serialize(writer); - writer.Write(Integrity); - writer.Write(OrigIntegrity); - writer.Write(LeashLength); - writer.Write(IsDetached); - if (IsDetached) - { - writer.Write(IsThruWhiteHole); - writer.Write(RelPos); - writer.Write(RelRot); - writer.Write(RelVel); - writer.Write(RelAngVel); - } - } - - public override void Deserialize(NetworkReader reader) - { - base.Deserialize(reader); - Integrity = reader.Read(); - OrigIntegrity = reader.Read(); - LeashLength = reader.Read(); - IsDetached = reader.Read(); - if (IsDetached) - { - IsThruWhiteHole = reader.Read(); - RelPos = reader.ReadVector3(); - RelRot = reader.ReadQuaternion(); - RelVel = reader.ReadVector3(); - RelAngVel = reader.ReadVector3(); - } - } + public FragmentInitialStateMessage(QSBFragment qsbFragment) : base(( + qsbFragment.AttachedObject._origIntegrity, + qsbFragment.AttachedObject._integrity, + (float)qsbFragment.LeashLength // will have a value at this point, so cast is okay + )) { } public override void OnReceiveRemote() { - WorldObject.AttachedObject._origIntegrity = OrigIntegrity; - WorldObject.LeashLength = LeashLength; - if (!OWMath.ApproxEquals(WorldObject.AttachedObject._integrity, Integrity)) + WorldObject.AttachedObject._origIntegrity = Data.OrigIntegrity; + WorldObject.SetIntegrity(Data.Integrity); + if (WorldObject.LeashLength != null) { - WorldObject.AttachedObject._integrity = Integrity; - WorldObject.AttachedObject.CallOnTakeDamage(); + DebugLog.ToConsole($"leash length for {WorldObject} already set", MessageType.Warning); + return; } - if (IsDetached && !WorldObject.IsDetached) - { - // the detach is delayed, so wait until that happens - Delay.RunWhen(() => WorldObject.IsDetached, () => - { - var body = WorldObject.Body; - - if (IsThruWhiteHole && !WorldObject.IsThruWhiteHole) - { - var whiteHoleVolume = MeteorManager.WhiteHoleVolume; - var attachedFluidDetector = body.GetAttachedFluidDetector(); - var attachedForceDetector = body.GetAttachedForceDetector(); - if (attachedFluidDetector is ConstantFluidDetector constantFluidDetector) - { - constantFluidDetector.SetDetectableFluid(whiteHoleVolume._fluidVolume); - } - - if (attachedForceDetector is ConstantForceDetector constantForceDetector) - { - constantForceDetector.ClearAllFields(); - } - - WorldObject.DetachableFragment.ChangeFragmentSector(whiteHoleVolume._whiteHoleSector, - whiteHoleVolume._whiteHoleProxyShadowSuperGroup); - - WorldObject.DetachableFragment.EndWarpScaling(); - body.gameObject.AddComponent().Init(whiteHoleVolume._whiteHoleBody, WorldObject.LeashLength); - whiteHoleVolume._ejectedBodyList.Add(body); - } - else if (!IsThruWhiteHole && WorldObject.IsThruWhiteHole) - { - // should only happen if client is way too far ahead and they try to connect. we fail here. - DebugLog.ToConsole($"{WorldObject} is thru white hole, but msg is not. fuck", MessageType.Error); - return; - } - - if (WorldObject.IsThruWhiteHole) - { - var debrisLeash = body.GetComponent(); - debrisLeash._deccelerating = false; - debrisLeash.enabled = true; - } - - var refBody = WorldObject.RefBody; - var pos = refBody.transform.FromRelPos(RelPos); - body.SetPosition(pos); - body.SetRotation(refBody.transform.FromRelRot(RelRot)); - body.SetVelocity(refBody.FromRelVel(RelVel, pos)); - body.SetAngularVelocity(refBody.FromRelAngVel(RelAngVel)); - }); - } - else if (!IsDetached && WorldObject.IsDetached) - { - // should only happen if client is way too far ahead and they try to connect. we fail here. - DebugLog.ToConsole($"{WorldObject} is detached, but msg is not. fuck", MessageType.Error); - } + WorldObject.LeashLength = Data.LeashLength; } -} \ No newline at end of file +} diff --git a/QSB/MeteorSync/Messages/MeteorSpecialImpactMessage.cs b/QSB/MeteorSync/Messages/MeteorSpecialImpactMessage.cs index c44ce87c..c3a0011a 100644 --- a/QSB/MeteorSync/Messages/MeteorSpecialImpactMessage.cs +++ b/QSB/MeteorSync/Messages/MeteorSpecialImpactMessage.cs @@ -3,6 +3,9 @@ using QSB.MeteorSync.WorldObjects; namespace QSB.MeteorSync.Messages; +/// +/// for syncing impact with a remote player/probe +/// public class MeteorSpecialImpactMessage : QSBWorldObjectMessage { public override void OnReceiveRemote() => WorldObject.SpecialImpact(); diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 66b3260a..73290190 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -149,7 +149,7 @@ public class MeteorServerPatches : QSBPatch } __instance.GetWorldObject() - .SendMessage(new FragmentIntegrityEvent(damage)); + .SendMessage(new FragmentIntegrityEvent(__instance._integrity)); } } @@ -240,7 +240,7 @@ public class MeteorPatches : QSBPatch var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); if (qsbFragment.LeashLength == null) { - DebugLog.ToConsole($"DebrisLeash.Init for {qsbFragment} before LeashLength was set", MessageType.Warning); + DebugLog.ToConsole($"DebrisLeash.Init called for {qsbFragment} before LeashLength was set", MessageType.Warning); return; } diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index e866d617..21b07f3c 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -11,8 +11,6 @@ public class QSBFragment : WorldObject { public override async UniTask Init(CancellationToken ct) { - DetachableFragment = AttachedObject.GetComponent(); - if (QSBCore.IsHost) { LeashLength = Random.Range(MeteorManager.WhiteHoleVolume._debrisDistMin, MeteorManager.WhiteHoleVolume._debrisDistMax); @@ -22,20 +20,6 @@ public class QSBFragment : WorldObject public override void SendInitialState(uint to) => this.SendMessage(new FragmentInitialStateMessage(this) { To = to }); - public DetachableFragment DetachableFragment; - public bool IsDetached => DetachableFragment != null && DetachableFragment._isDetached; - public bool IsThruWhiteHole => IsDetached && DetachableFragment._sector != null && - DetachableFragment._sector._parentSector == MeteorManager.WhiteHoleVolume._whiteHoleSector; - public OWRigidbody RefBody => IsThruWhiteHole ? MeteorManager.WhiteHoleVolume._whiteHoleBody : Locator._brittleHollow._owRigidbody; - public OWRigidbody Body => IsDetached ? AttachedObject.transform.parent.parent.GetAttachedOWRigidbody() : null; - - /// - /// what the leash length will be when we eventually detach and fall thru white hole. - /// - /// generated by the server and sent to clients in the initial state message. - /// - public float? LeashLength; - public void SetIntegrity(float integrity) { if (OWMath.ApproxEquals(AttachedObject._integrity, integrity)) @@ -51,4 +35,11 @@ public class QSBFragment : WorldObject AttachedObject._integrity = integrity; AttachedObject.CallOnTakeDamage(); } + + /// + /// what the leash length will be when we eventually detach and fall thru white hole. + /// + /// generated by the server and sent to clients in the initial state message. + /// + public float? LeashLength; } From 8d55453357206f12ad3b238ca99956c58ab6f110 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 20:02:50 -0800 Subject: [PATCH 06/13] fragments: always send integrity cuz whoops --- QSB/MeteorSync/Patches/MeteorPatches.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 73290190..97f418b1 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -140,17 +140,9 @@ public class MeteorServerPatches : QSBPatch [HarmonyPostfix] [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] - public static void FragmentIntegrity_AddDamage(FragmentIntegrity __instance, - float damage) - { - if (__instance._integrity <= 0f) - { - return; - } - + public static void FragmentIntegrity_AddDamage(FragmentIntegrity __instance) => __instance.GetWorldObject() .SendMessage(new FragmentIntegrityEvent(__instance._integrity)); - } } /// From 5c0d19d785008205a8ff65822556b1ec26277439 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 20:13:05 -0800 Subject: [PATCH 07/13] fragments: patch AddDamage to do nothing on non-hosts --- QSB/MeteorSync/Patches/MeteorPatches.cs | 33 +++---------------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 97f418b1..4d64de08 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -158,36 +158,9 @@ public class MeteorClientPatches : QSBPatch => false; [HarmonyPrefix] - [HarmonyPatch(typeof(MeteorController), nameof(MeteorController.Impact))] - public static bool MeteorController_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 impactParticle in __instance._impactParticles) - { - impactParticle.transform.rotation = rotation; - impactParticle.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; - - return false; - } + [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] + public static bool FragmentIntegrity_AddDamage() + => false; } /// From 276ff3d8e30665e2a13c1cb5a5bb1077b0ff5d17 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 20:20:37 -0800 Subject: [PATCH 08/13] fragments: tweak --- .../Messages/FragmentInitialStateMessage.cs | 22 +++++++++---------- ...tyEvent.cs => FragmentIntegrityMessage.cs} | 4 ++-- QSB/MeteorSync/Patches/MeteorPatches.cs | 11 +++++----- QSB/MeteorSync/WorldObjects/QSBFragment.cs | 12 +++++----- 4 files changed, 23 insertions(+), 26 deletions(-) rename QSB/MeteorSync/Messages/{FragmentIntegrityEvent.cs => FragmentIntegrityMessage.cs} (53%) diff --git a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs index 6069b1f3..8d06d8b1 100644 --- a/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs +++ b/QSB/MeteorSync/Messages/FragmentInitialStateMessage.cs @@ -5,25 +5,23 @@ using QSB.Utility; namespace QSB.MeteorSync.Messages; -public class FragmentInitialStateMessage : QSBWorldObjectMessage +/// +/// original integrity, leash length +/// +public class FragmentInitialStateMessage : QSBWorldObjectMessage { - public FragmentInitialStateMessage(QSBFragment qsbFragment) : base(( - qsbFragment.AttachedObject._origIntegrity, - qsbFragment.AttachedObject._integrity, - (float)qsbFragment.LeashLength // will have a value at this point, so cast is okay - )) { } + public FragmentInitialStateMessage(float origIntegrity, float leashLength) : base((origIntegrity, leashLength)) { } public override void OnReceiveRemote() { WorldObject.AttachedObject._origIntegrity = Data.OrigIntegrity; - WorldObject.SetIntegrity(Data.Integrity); - if (WorldObject.LeashLength != null) + if (WorldObject.LeashLength == null) + { + WorldObject.LeashLength = Data.LeashLength; + } + else { DebugLog.ToConsole($"leash length for {WorldObject} already set", MessageType.Warning); - return; } - - WorldObject.LeashLength = Data.LeashLength; } } diff --git a/QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs b/QSB/MeteorSync/Messages/FragmentIntegrityMessage.cs similarity index 53% rename from QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs rename to QSB/MeteorSync/Messages/FragmentIntegrityMessage.cs index 12020025..7caf3a35 100644 --- a/QSB/MeteorSync/Messages/FragmentIntegrityEvent.cs +++ b/QSB/MeteorSync/Messages/FragmentIntegrityMessage.cs @@ -3,8 +3,8 @@ using QSB.MeteorSync.WorldObjects; namespace QSB.MeteorSync.Messages; -public class FragmentIntegrityEvent : QSBWorldObjectMessage +public class FragmentIntegrityMessage : QSBWorldObjectMessage { - public FragmentIntegrityEvent(float integrity) : base(integrity) { } + public FragmentIntegrityMessage(float integrity) : base(integrity) { } public override void OnReceiveRemote() => WorldObject.SetIntegrity(Data); } diff --git a/QSB/MeteorSync/Patches/MeteorPatches.cs b/QSB/MeteorSync/Patches/MeteorPatches.cs index 4d64de08..eb517b40 100644 --- a/QSB/MeteorSync/Patches/MeteorPatches.cs +++ b/QSB/MeteorSync/Patches/MeteorPatches.cs @@ -142,7 +142,7 @@ public class MeteorServerPatches : QSBPatch [HarmonyPatch(typeof(FragmentIntegrity), nameof(FragmentIntegrity.AddDamage))] public static void FragmentIntegrity_AddDamage(FragmentIntegrity __instance) => __instance.GetWorldObject() - .SendMessage(new FragmentIntegrityEvent(__instance._integrity)); + .SendMessage(new FragmentIntegrityMessage(__instance._integrity)); } /// @@ -203,12 +203,13 @@ public class MeteorPatches : QSBPatch } var qsbFragment = __instance._detachableFragment._fragmentIntegrity.GetWorldObject(); - if (qsbFragment.LeashLength == null) + if (qsbFragment.LeashLength != null) + { + __instance._leashLength = (float)qsbFragment.LeashLength; + } + else { DebugLog.ToConsole($"DebrisLeash.Init called for {qsbFragment} before LeashLength was set", MessageType.Warning); - return; } - - __instance._leashLength = (float)qsbFragment.LeashLength; } } diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index 21b07f3c..b32c6c72 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -17,8 +17,11 @@ public class QSBFragment : WorldObject } } - public override void SendInitialState(uint to) => - this.SendMessage(new FragmentInitialStateMessage(this) { To = to }); + public override void SendInitialState(uint to) + { + this.SendMessage(new FragmentInitialStateMessage(AttachedObject._origIntegrity, (float)LeashLength) { To = to }); + this.SendMessage(new FragmentIntegrityMessage(AttachedObject._integrity)); + } public void SetIntegrity(float integrity) { @@ -27,11 +30,6 @@ public class QSBFragment : WorldObject return; } - if (AttachedObject._integrity <= 0f) - { - return; - } - AttachedObject._integrity = integrity; AttachedObject.CallOnTakeDamage(); } From 22856c9cd4dc24f9163caf2118fadaf1433d9c4e Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 20:32:33 -0800 Subject: [PATCH 09/13] QSBMeteorLauncher: meteor pools might be null --- QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs index 7be4118e..52040255 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs @@ -1,5 +1,6 @@ using Cysharp.Threading.Tasks; using QSB.WorldSync; +using System.Collections.Generic; using System.Linq; using System.Threading; using UnityEngine; @@ -12,7 +13,8 @@ public class QSBMeteorLauncher : WorldObject public override async UniTask Init(CancellationToken ct) { - var meteors = AttachedObject._meteorPool.Concat(AttachedObject._dynamicMeteorPool); + var meteors = (AttachedObject._meteorPool ?? new List()) + .Concat(AttachedObject._dynamicMeteorPool ?? new List()); await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct); _qsbMeteors = meteors.Select(x => x.GetWorldObject()).ToArray(); } From 310daa7d4512b418b36e131070784db3c405a35d Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 21:23:06 -0800 Subject: [PATCH 10/13] update Extensions.cs --- QSB/OrbSync/WorldObjects/QSBOrb.cs | 4 +++- QSB/Utility/Extensions.cs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/QSB/OrbSync/WorldObjects/QSBOrb.cs b/QSB/OrbSync/WorldObjects/QSBOrb.cs index ad200a4c..69eaff56 100644 --- a/QSB/OrbSync/WorldObjects/QSBOrb.cs +++ b/QSB/OrbSync/WorldObjects/QSBOrb.cs @@ -5,8 +5,10 @@ using QSB.OrbSync.Messages; using QSB.OrbSync.TransformSync; using QSB.Utility; using QSB.WorldSync; +using System; using System.Threading; using UnityEngine; +using Object = UnityEngine.Object; namespace QSB.OrbSync.WorldObjects; @@ -37,7 +39,7 @@ public class QSBOrb : WorldObject public override void SendInitialState(uint to) { this.SendMessage(new OrbDragMessage(AttachedObject._isBeingDragged) { To = to }); - var slotIndex = AttachedObject._slots.IndexOf(AttachedObject._occupiedSlot); + var slotIndex = Array.IndexOf(AttachedObject._slots, AttachedObject._occupiedSlot); this.SendMessage(new OrbSlotMessage(slotIndex, false) { To = to }); } diff --git a/QSB/Utility/Extensions.cs b/QSB/Utility/Extensions.cs index 4d63e046..b1171388 100644 --- a/QSB/Utility/Extensions.cs +++ b/QSB/Utility/Extensions.cs @@ -156,7 +156,7 @@ public static class Extensions return y; } - public static int IndexOf(this T[] array, T value) => Array.IndexOf(array, value); + public static IEnumerable EmptyIfNull(this IEnumerable source) => source ?? Enumerable.Empty(); public static bool IsInRange(this IList list, int index) => index >= 0 && index < list.Count; From 0dc2c1a3f9c04a7b71254dcb218a17e976e1bd53 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 21:27:29 -0800 Subject: [PATCH 11/13] QSBMeteor: order by index in launcher pools --- QSB/MeteorSync/MeteorManager.cs | 13 ++++++++++--- QSB/MeteorSync/WorldObjects/QSBFragment.cs | 1 + QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs | 11 +++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/QSB/MeteorSync/MeteorManager.cs b/QSB/MeteorSync/MeteorManager.cs index 57812185..589a2d57 100644 --- a/QSB/MeteorSync/MeteorManager.cs +++ b/QSB/MeteorSync/MeteorManager.cs @@ -1,5 +1,6 @@ using Cysharp.Threading.Tasks; using QSB.MeteorSync.WorldObjects; +using QSB.Utility; using QSB.WorldSync; using System.Linq; using System.Threading; @@ -18,8 +19,14 @@ public class MeteorManager : WorldObjectManager await UniTask.WaitUntil(() => LateInitializerManager.isDoneInitializing, cancellationToken: ct); WhiteHoleVolume = QSBWorldSync.GetUnityObjects().First(); - QSBWorldSync.Init(); - QSBWorldSync.Init(); QSBWorldSync.Init(); + + var meteorLaunchers = QSBWorldSync.GetUnityObjects().SortDeterministic().ToList(); + QSBWorldSync.Init(meteorLaunchers); + + // order by pool instead of using SortDeterministic + var meteors = meteorLaunchers.SelectMany(x => + x._meteorPool.EmptyIfNull().Concat(x._dynamicMeteorPool.EmptyIfNull())); + QSBWorldSync.Init(meteors); } -} \ No newline at end of file +} diff --git a/QSB/MeteorSync/WorldObjects/QSBFragment.cs b/QSB/MeteorSync/WorldObjects/QSBFragment.cs index b32c6c72..73235525 100644 --- a/QSB/MeteorSync/WorldObjects/QSBFragment.cs +++ b/QSB/MeteorSync/WorldObjects/QSBFragment.cs @@ -14,6 +14,7 @@ public class QSBFragment : WorldObject if (QSBCore.IsHost) { LeashLength = Random.Range(MeteorManager.WhiteHoleVolume._debrisDistMin, MeteorManager.WhiteHoleVolume._debrisDistMax); + // SetIntegrity(0); } } diff --git a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs index 52040255..bfcd552a 100644 --- a/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs +++ b/QSB/MeteorSync/WorldObjects/QSBMeteorLauncher.cs @@ -1,6 +1,6 @@ using Cysharp.Threading.Tasks; +using QSB.Utility; using QSB.WorldSync; -using System.Collections.Generic; using System.Linq; using System.Threading; using UnityEngine; @@ -13,8 +13,7 @@ public class QSBMeteorLauncher : WorldObject public override async UniTask Init(CancellationToken ct) { - var meteors = (AttachedObject._meteorPool ?? new List()) - .Concat(AttachedObject._dynamicMeteorPool ?? new List()); + var meteors = AttachedObject._meteorPool.EmptyIfNull().Concat(AttachedObject._dynamicMeteorPool.EmptyIfNull()); await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct); _qsbMeteors = meteors.Select(x => x.GetWorldObject()).ToArray(); } @@ -32,13 +31,13 @@ public class QSBMeteorLauncher : WorldObject } } - public void LaunchMeteor(MeteorController meteorController, float launchSpeed) + public void LaunchMeteor(MeteorController meteor, float launchSpeed) { - meteorController.Initialize(AttachedObject.transform, AttachedObject._detectableField, AttachedObject._detectableFluid); + meteor.Initialize(AttachedObject.transform, AttachedObject._detectableField, AttachedObject._detectableFluid); 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); + meteor.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); From 7c9ff9642e2296a0a72625e87a1843cb6a5c7177 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 10 Mar 2022 21:51:20 -0800 Subject: [PATCH 12/13] DropItemMessage: also use send relative normal --- QSB/ItemSync/Messages/DropItemMessage.cs | 43 +++++++----------------- QSB/ItemSync/Patches/ItemPatches.cs | 11 +++--- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/QSB/ItemSync/Messages/DropItemMessage.cs b/QSB/ItemSync/Messages/DropItemMessage.cs index 40ac0e8e..fa2268cc 100644 --- a/QSB/ItemSync/Messages/DropItemMessage.cs +++ b/QSB/ItemSync/Messages/DropItemMessage.cs @@ -1,5 +1,4 @@ -using Mirror; -using QSB.ItemSync.WorldObjects.Items; +using QSB.ItemSync.WorldObjects.Items; using QSB.Messaging; using QSB.Player; using QSB.SectorSync.WorldObjects; @@ -8,39 +7,21 @@ using UnityEngine; namespace QSB.ItemSync.Messages; -internal class DropItemMessage : QSBWorldObjectMessage +internal class DropItemMessage : QSBWorldObjectMessage { - private Vector3 Position; - private Vector3 Normal; - private int SectorId; - - public DropItemMessage(Vector3 position, Vector3 normal, Sector sector) - { - Position = position; - Normal = normal; - SectorId = sector.GetWorldObject().ObjectId; - } - - public override void Serialize(NetworkWriter writer) - { - base.Serialize(writer); - writer.Write(Position); - writer.Write(Normal); - writer.Write(SectorId); - } - - public override void Deserialize(NetworkReader reader) - { - base.Deserialize(reader); - Position = reader.ReadVector3(); - Normal = reader.ReadVector3(); - SectorId = reader.Read(); - } + public DropItemMessage(Vector3 position, Vector3 normal, Sector sector) : base(( + sector.transform.InverseTransformPoint(position), + sector.transform.InverseTransformDirection(normal), + sector.GetWorldObject().ObjectId + )) { } public override void OnReceiveRemote() { - var sector = SectorId.GetWorldObject().AttachedObject; - WorldObject.DropItem(sector.transform.TransformPoint(Position), Normal, sector); + var sector = Data.SectorId.GetWorldObject().AttachedObject; + var position = sector.transform.TransformPoint(Data.LocalPos); + var normal = sector.transform.TransformDirection(Data.LocalNorm); + WorldObject.DropItem(position, normal, sector); var player = QSBPlayerManager.GetPlayer(From); player.HeldItem = WorldObject; diff --git a/QSB/ItemSync/Patches/ItemPatches.cs b/QSB/ItemSync/Patches/ItemPatches.cs index 4c1fc19e..94d2f656 100644 --- a/QSB/ItemSync/Patches/ItemPatches.cs +++ b/QSB/ItemSync/Patches/ItemPatches.cs @@ -90,7 +90,7 @@ internal class ItemPatches : QSBPatch var parent = (customDropTarget == null) ? targetRigidbody.transform : customDropTarget.GetItemDropTargetTransform(hit.collider.gameObject); - var IQSBItem = __instance._heldItem.GetWorldObject(); + var qsbItem = __instance._heldItem.GetWorldObject(); __instance._heldItem.DropItem(hit.point, hit.normal, parent, sector, customDropTarget); __instance._heldItem = null; QSBPlayerManager.LocalPlayer.HeldItem = null; @@ -98,12 +98,13 @@ internal class ItemPatches : QSBPatch var parentSector = parent.GetComponentInChildren(); if (parentSector != null) { - var localPos = parentSector.transform.InverseTransformPoint(hit.point); - IQSBItem.SendMessage(new DropItemMessage(localPos, hit.normal, parentSector)); - return false; + qsbItem.SendMessage(new DropItemMessage(hit.point, hit.normal, parentSector)); + } + else + { + DebugLog.ToConsole($"Error - No sector found for rigidbody {targetRigidbody.name}!.", MessageType.Error); } - DebugLog.ToConsole($"Error - No sector found for rigidbody {targetRigidbody.name}!.", MessageType.Error); return false; } } \ No newline at end of file From 2ade7051fa4fdda96137a0859b28bb3d42890a87 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 11 Mar 2022 00:55:10 -0800 Subject: [PATCH 13/13] PlayerJoinMessage: made a typo --- QSB/Player/Messages/PlayerJoinMessage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QSB/Player/Messages/PlayerJoinMessage.cs b/QSB/Player/Messages/PlayerJoinMessage.cs index c2377ecb..b273ec63 100644 --- a/QSB/Player/Messages/PlayerJoinMessage.cs +++ b/QSB/Player/Messages/PlayerJoinMessage.cs @@ -74,7 +74,7 @@ public class PlayerJoinMessage : QSBMessage var player = QSBPlayerManager.GetPlayer(From); player.Name = PlayerName; - DebugLog.ToAll($"{player} joined!", MessageType.Info); + DebugLog.ToAll($"{player.Name} joined!", MessageType.Info); DebugLog.DebugWrite($"{player} joined. qsbVersion:{QSBVersion}, gameVersion:{GameVersion}, dlcInstalled:{DlcInstalled}", MessageType.Info); }