diff --git a/QSB/PlayerBodySetup/Local/LocalPlayerCreation.cs b/QSB/PlayerBodySetup/Local/LocalPlayerCreation.cs index 3fde2c98..3c083447 100644 --- a/QSB/PlayerBodySetup/Local/LocalPlayerCreation.cs +++ b/QSB/PlayerBodySetup/Local/LocalPlayerCreation.cs @@ -22,7 +22,7 @@ namespace QSB.PlayerBodySetup.Local { DebugLog.DebugWrite($"CREATE PLAYER"); - sectorDetector.Init(Locator.GetPlayerSectorDetector(), TargetType.Player); + sectorDetector.Init(Locator.GetPlayerSectorDetector()); // player body var playerBody = Locator.GetPlayerTransform(); diff --git a/QSB/SectorSync/QSBSectorDetector.cs b/QSB/SectorSync/QSBSectorDetector.cs index a62788c6..4b2944d3 100644 --- a/QSB/SectorSync/QSBSectorDetector.cs +++ b/QSB/SectorSync/QSBSectorDetector.cs @@ -13,9 +13,8 @@ namespace QSB.SectorSync public readonly List SectorList = new(); private SectorDetector _sectorDetector; - private TargetType _targetType; - public void Init(SectorDetector detector, TargetType type) + public void Init(SectorDetector detector) { if (_sectorDetector) { @@ -33,8 +32,6 @@ namespace QSB.SectorSync _sectorDetector.OnExitSector += RemoveSector; _sectorDetector._sectorList.ForEach(AddSector); - - _targetType = type; } public void Uninit() @@ -106,35 +103,41 @@ namespace QSB.SectorSync /// public QSBSector GetClosestSector() { - var inASector = SectorList.Any(x => x.ShouldSyncTo(_targetType)); + var type = _sectorDetector._occupantType; - var listToCheck = inASector - ? SectorList - : QSBWorldSync.GetWorldObjects().Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed); + var validSectors = SectorList.Where(x => x.ShouldSyncTo(type)).ToList(); + var inASector = validSectors.Count > 0; - var goodSectors = listToCheck.Where(sector => sector.ShouldSyncTo(_targetType)).ToList(); + if (!inASector) + { + validSectors = QSBWorldSync.GetWorldObjects() + .Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed && x.ShouldSyncTo(type)) + .ToList(); + } - if (goodSectors.Count == 0) + if (validSectors.Count == 0) { return null; } - var closest = goodSectors + var closest = validSectors .MinBy(sector => CalculateSectorScore(sector, _sectorDetector._attachedRigidbody)); if (inASector) { var pos = _sectorDetector._attachedRigidbody.GetPosition(); - bool IsApproxCloseToClosestSector(QSBSector sectorToCheck) - => OWMath.ApproxEquals(Vector3.Distance(sectorToCheck.Position, pos), - Vector3.Distance(closest.Position, pos), - 0.01f); + bool IsSameDistanceAsClosest(QSBSector fakeSector) + => OWMath.ApproxEquals( + Vector3.Distance(fakeSector.Position, pos), + Vector3.Distance(closest.Position, pos), + 0.01f); - bool IsFakeSectorActive(QSBSector fakeSectorToCheck) - => goodSectors.Any(x => fakeSectorToCheck.FakeSector.AttachedSector == x.AttachedObject); + bool IsAttachedValid(QSBSector fakeSector) + => validSectors.Any(x => x.AttachedObject == fakeSector.FakeSector.AttachedSector); - var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors.FirstOrDefault(x => IsApproxCloseToClosestSector(x) && IsFakeSectorActive(x)); + var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors + .FirstOrDefault(x => IsSameDistanceAsClosest(x) && IsAttachedValid(x)); return fakeToSyncTo ?? closest; } @@ -174,4 +177,4 @@ namespace QSB.SectorSync return 0; } } -} \ No newline at end of file +} diff --git a/QSB/SectorSync/TargetType.cs b/QSB/SectorSync/TargetType.cs deleted file mode 100644 index 7abef4ad..00000000 --- a/QSB/SectorSync/TargetType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace QSB.SectorSync -{ - public enum TargetType - { - Player, - Probe, - Ship - } -} diff --git a/QSB/SectorSync/WorldObjects/QSBSector.cs b/QSB/SectorSync/WorldObjects/QSBSector.cs index da7a60bf..e6ac8a22 100644 --- a/QSB/SectorSync/WorldObjects/QSBSector.cs +++ b/QSB/SectorSync/WorldObjects/QSBSector.cs @@ -26,6 +26,7 @@ namespace QSB.SectorSync.WorldObjects } } public Vector3 Position => Transform.position; + public bool IsFakeSector => AttachedObject is FakeSector; public FakeSector FakeSector => (FakeSector)AttachedObject; @@ -47,7 +48,7 @@ namespace QSB.SectorSync.WorldObjects public override void SendInitialState(uint to) { } - public bool ShouldSyncTo(TargetType targetType) + public bool ShouldSyncTo(DynamicOccupant occupantType) { if (AttachedObject == null) { @@ -60,7 +61,7 @@ namespace QSB.SectorSync.WorldObjects return false; } - if (targetType == TargetType.Ship && Type == Sector.Name.Ship) + if (occupantType == DynamicOccupant.Ship && Type == Sector.Name.Ship) { return false; } diff --git a/QSB/ShipSync/TransformSync/ShipTransformSync.cs b/QSB/ShipSync/TransformSync/ShipTransformSync.cs index 05c55f1f..8f0676da 100644 --- a/QSB/ShipSync/TransformSync/ShipTransformSync.cs +++ b/QSB/ShipSync/TransformSync/ShipTransformSync.cs @@ -1,4 +1,3 @@ -using QSB.SectorSync; using QSB.Syncs.Sectored.Rigidbodies; using QSB.Utility; using UnityEngine; @@ -23,7 +22,7 @@ namespace QSB.ShipSync.TransformSync protected override OWRigidbody InitAttachedRigidbody() { - SectorDetector.Init(Locator.GetShipDetector().GetComponent(), TargetType.Ship); + SectorDetector.Init(Locator.GetShipDetector().GetComponent()); return Locator.GetShipBody(); } diff --git a/QSB/Tools/ProbeTool/TransformSync/PlayerProbeSync.cs b/QSB/Tools/ProbeTool/TransformSync/PlayerProbeSync.cs index 239ca4fb..a85acd3f 100644 --- a/QSB/Tools/ProbeTool/TransformSync/PlayerProbeSync.cs +++ b/QSB/Tools/ProbeTool/TransformSync/PlayerProbeSync.cs @@ -1,5 +1,4 @@ using OWML.Common; -using QSB.SectorSync; using QSB.Syncs.Sectored.Transforms; using QSB.Tools.ProbeLauncherTool; using QSB.Utility; @@ -26,7 +25,7 @@ namespace QSB.Tools.ProbeTool.TransformSync protected override Transform InitLocalTransform() { - SectorDetector.Init(Locator.GetProbe().GetSectorDetector(), TargetType.Probe); + SectorDetector.Init(Locator.GetProbe().GetSectorDetector()); var body = Locator.GetProbe().transform; Player.ProbeBody = body.gameObject;