mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-20 15:41:01 +00:00
Merge branch 'dev' into orb-overhaul
# Conflicts: # QSB/SectorSync/QSBSectorManager.cs # QSB/Syncs/Sectored/BaseSectoredSync.cs
This commit is contained in:
commit
07caf6129b
@ -121,8 +121,8 @@ namespace QSB.Player
|
||||
|
||||
// Misc
|
||||
public bool IsReady { get; set; }
|
||||
public bool IsInMoon; // MOVE : move into PlayerStates?
|
||||
public bool IsInShrine; // MOVE : move into PlayerStates?
|
||||
public bool IsInMoon;
|
||||
public bool IsInShrine;
|
||||
public IQSBQuantumObject EntangledObject;
|
||||
public bool IsDead { get; set; }
|
||||
public ClientState State { get; set; }
|
||||
|
@ -117,7 +117,6 @@ namespace QSB
|
||||
gameObject.AddComponent<ConversationManager>();
|
||||
gameObject.AddComponent<QSBInputManager>();
|
||||
gameObject.AddComponent<TimeSyncUI>();
|
||||
gameObject.AddComponent<RepeatingManager>();
|
||||
gameObject.AddComponent<PlayerEntanglementWatcher>();
|
||||
gameObject.AddComponent<DebugGUI>();
|
||||
gameObject.AddComponent<MenuManager>();
|
||||
|
@ -13,12 +13,29 @@ namespace QSB.SectorSync
|
||||
{
|
||||
public static QSBSectorManager Instance { get; private set; }
|
||||
public bool IsReady { get; private set; }
|
||||
public List<QSBSector> FakeSectors = new();
|
||||
public readonly List<QSBSector> FakeSectors = new();
|
||||
|
||||
private void OnEnable() => RepeatingManager.Repeatings.Add(this);
|
||||
private void OnDisable() => RepeatingManager.Repeatings.Remove(this);
|
||||
public readonly List<BaseSectoredSync> SectoredSyncs = new();
|
||||
|
||||
public readonly List<IBaseSectoredSync> SectoredSyncs = new();
|
||||
#region repeating timer
|
||||
|
||||
private const float TimeInterval = 0.4f;
|
||||
private float _checkTimer = TimeInterval;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_checkTimer += Time.unscaledDeltaTime;
|
||||
if (_checkTimer < TimeInterval)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Invoke();
|
||||
|
||||
_checkTimer = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Invoke()
|
||||
{
|
||||
@ -76,8 +93,7 @@ namespace QSB.SectorSync
|
||||
|
||||
private void CheckTransformSyncSector(IBaseSectoredSync transformSync)
|
||||
{
|
||||
var attachedObject = transformSync.ReturnObject();
|
||||
var closestSector = transformSync.SectorSync.GetClosestSector(attachedObject.transform);
|
||||
var closestSector = transformSync.SectorSync.GetClosestSector();
|
||||
if (closestSector == default(QSBSector))
|
||||
{
|
||||
return;
|
||||
@ -91,4 +107,4 @@ namespace QSB.SectorSync
|
||||
transformSync.SetReferenceSector(closestSector);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using OWML.Common;
|
||||
using OWML.Utils;
|
||||
using QSB.SectorSync.WorldObjects;
|
||||
using QSB.Utility;
|
||||
using QSB.WorldSync;
|
||||
@ -13,9 +12,8 @@ namespace QSB.SectorSync
|
||||
public class SectorSync : MonoBehaviour
|
||||
{
|
||||
public bool IsReady { get; private set; }
|
||||
public List<QSBSector> SectorList = new();
|
||||
public readonly List<QSBSector> SectorList = new();
|
||||
|
||||
private OWRigidbody _attachedOWRigidbody;
|
||||
private SectorDetector _sectorDetector;
|
||||
private TargetType _targetType;
|
||||
|
||||
@ -48,40 +46,20 @@ namespace QSB.SectorSync
|
||||
_sectorDetector.OnEnterSector += AddSector;
|
||||
_sectorDetector.OnExitSector += RemoveSector;
|
||||
|
||||
_attachedOWRigidbody = _sectorDetector.GetValue<OWRigidbody>("_attachedRigidbody");
|
||||
if (_attachedOWRigidbody == null)
|
||||
{
|
||||
DebugLog.ToConsole($"Warning - OWRigidbody for {_sectorDetector.name} is null!", MessageType.Warning);
|
||||
}
|
||||
|
||||
PopulateSectorList();
|
||||
SectorList.Clear();
|
||||
_sectorDetector._sectorList.ForEach(AddSector);
|
||||
|
||||
_targetType = type;
|
||||
IsReady = true;
|
||||
}
|
||||
|
||||
private void PopulateSectorList()
|
||||
{
|
||||
var currentList = _sectorDetector.GetValue<List<Sector>>("_sectorList");
|
||||
|
||||
SectorList.Clear();
|
||||
foreach (var sector in currentList)
|
||||
{
|
||||
if (sector == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AddSector(sector);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddSector(Sector sector)
|
||||
{
|
||||
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector>(sector);
|
||||
if (worldObject == null)
|
||||
{
|
||||
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (SectorList.Contains(worldObject))
|
||||
@ -111,7 +89,7 @@ namespace QSB.SectorSync
|
||||
SectorList.Remove(worldObject);
|
||||
}
|
||||
|
||||
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
|
||||
public QSBSector GetClosestSector()
|
||||
{
|
||||
if (QSBSectorManager.Instance == null || !QSBSectorManager.Instance.IsReady)
|
||||
{
|
||||
@ -120,73 +98,62 @@ namespace QSB.SectorSync
|
||||
|
||||
if (!IsReady)
|
||||
{
|
||||
DebugLog.ToConsole($"Warning - Tried to use GetClosestSector() before this SectorSync is ready. Transform:{trans.name} Stacktrace:\r\n{Environment.StackTrace}", MessageType.Warning);
|
||||
DebugLog.ToConsole($"Warning - Tried to use GetClosestSector() before this SectorSync is ready. Stacktrace:\r\n{Environment.StackTrace}", MessageType.Warning);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_sectorDetector == null || _attachedOWRigidbody == null || _targetType == TargetType.None)
|
||||
if (_sectorDetector == null)
|
||||
{
|
||||
IsReady = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
bool ShouldSyncTo(QSBSector sector, TargetType type)
|
||||
{
|
||||
if (sector == null)
|
||||
{
|
||||
DebugLog.ToConsole($"Warning - Tried to check if we should sync to null sector!", MessageType.Warning);
|
||||
return false;
|
||||
}
|
||||
var inASector = SectorList.Any(x => x.ShouldSyncTo(_targetType));
|
||||
|
||||
return sector.ShouldSyncTo(type);
|
||||
}
|
||||
var listToCheck = inASector
|
||||
? SectorList
|
||||
: QSBWorldSync.GetWorldObjects<QSBSector>().Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed);
|
||||
|
||||
var numSectorsCurrentlyIn = SectorList.Count(x => ShouldSyncTo(x, _targetType));
|
||||
var goodSectors = listToCheck.Where(sector => sector.ShouldSyncTo(_targetType)).ToList();
|
||||
|
||||
var listToCheck = numSectorsCurrentlyIn == 0
|
||||
? QSBWorldSync.GetWorldObjects<QSBSector>().Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed)
|
||||
: SectorList;
|
||||
|
||||
var activeNotNullNotBlacklisted = listToCheck.Where(sector => sector.AttachedObject != null
|
||||
&& sector.ShouldSyncTo(_targetType));
|
||||
if (activeNotNullNotBlacklisted.Count() == 0)
|
||||
if (goodSectors.Count == 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var ordered = activeNotNullNotBlacklisted
|
||||
.OrderBy(sector => CalculateSectorScore(sector, trans, _attachedOWRigidbody));
|
||||
var closest = goodSectors
|
||||
.OrderBy(sector => CalculateSectorScore(sector, _sectorDetector._attachedRigidbody)).First();
|
||||
|
||||
// TODO : clean up this shit???
|
||||
if (
|
||||
numSectorsCurrentlyIn != 0 &&
|
||||
// if any fake sectors are *roughly* in the same place as other sectors - we want fake sectors to override other sectors
|
||||
QSBSectorManager.Instance.FakeSectors.Any(
|
||||
x => OWMath.ApproxEquals(Vector3.Distance(x.Position, trans.position), Vector3.Distance(ordered.FirstOrDefault().Position, trans.position), 0.01f)
|
||||
&& activeNotNullNotBlacklisted.Any(
|
||||
y => y.AttachedObject == (x.AttachedObject as FakeSector).AttachedSector)))
|
||||
if (inASector)
|
||||
{
|
||||
return QSBSectorManager.Instance.FakeSectors.First(x => OWMath.ApproxEquals(Vector3.Distance(x.Position, trans.position), Vector3.Distance(ordered.FirstOrDefault().Position, trans.position), 0.01f));
|
||||
var pos = _sectorDetector._attachedRigidbody.GetPosition();
|
||||
|
||||
bool IsApproxCloseToClosestSector(QSBSector sectorToCheck)
|
||||
=> OWMath.ApproxEquals(Vector3.Distance(sectorToCheck.Position, pos),
|
||||
Vector3.Distance(closest.Position, pos),
|
||||
0.01f);
|
||||
|
||||
bool IsFakeSectorActive(QSBSector fakeSectorToCheck)
|
||||
=> goodSectors.Any(x => fakeSectorToCheck.FakeSector.AttachedSector == x.AttachedObject);
|
||||
|
||||
var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors.FirstOrDefault(x => IsApproxCloseToClosestSector(x) && IsFakeSectorActive(x));
|
||||
return fakeToSyncTo ?? closest;
|
||||
}
|
||||
|
||||
return ordered.FirstOrDefault();
|
||||
return closest;
|
||||
}
|
||||
|
||||
public static float CalculateSectorScore(QSBSector sector, Transform trans, OWRigidbody rigidbody)
|
||||
private static float CalculateSectorScore(QSBSector sector, OWRigidbody rigidbody)
|
||||
{
|
||||
var distance = Vector3.Distance(sector.Position, trans.position); // want to be small
|
||||
var radius = GetRadius(sector); // want to be small
|
||||
var velocity = GetRelativeVelocity(sector, rigidbody); // want to be small
|
||||
var distance = (sector.Position - rigidbody.GetPosition()).sqrMagnitude;
|
||||
var radius = GetRadius(sector);
|
||||
var velocity = GetRelativeVelocity(sector, rigidbody);
|
||||
|
||||
return Mathf.Pow(distance, 2) + Mathf.Pow(radius, 2) + Mathf.Pow(velocity, 2);
|
||||
return distance + Mathf.Pow(radius, 2) + velocity;
|
||||
}
|
||||
|
||||
public static float GetRadius(QSBSector sector)
|
||||
private static float GetRadius(QSBSector sector)
|
||||
{
|
||||
if (sector == null)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
// TODO : make this work for other stuff, not just shaped triggervolumes
|
||||
var trigger = sector.AttachedObject.GetTriggerVolume();
|
||||
if (trigger != null)
|
||||
@ -200,14 +167,13 @@ namespace QSB.SectorSync
|
||||
return 0f;
|
||||
}
|
||||
|
||||
public static float GetRelativeVelocity(QSBSector sector, OWRigidbody rigidbody)
|
||||
private static float GetRelativeVelocity(QSBSector sector, OWRigidbody rigidbody)
|
||||
{
|
||||
var sectorRigidBody = sector.AttachedObject.GetOWRigidbody();
|
||||
if (sectorRigidBody != null && rigidbody != null)
|
||||
{
|
||||
var relativeVelocity = sectorRigidBody.GetRelativeVelocity(rigidbody);
|
||||
var relativeVelocityMagnitude = relativeVelocity.sqrMagnitude; // Remember this is squared for efficiency!
|
||||
return relativeVelocityMagnitude;
|
||||
return relativeVelocity.sqrMagnitude;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -2,10 +2,8 @@
|
||||
{
|
||||
public enum TargetType
|
||||
{
|
||||
None = 0,
|
||||
Player = 1,
|
||||
Probe = 2,
|
||||
Ship = 3,
|
||||
Other = 4
|
||||
Player,
|
||||
Probe,
|
||||
Ship
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using OWML.Common;
|
||||
using OWML.Utils;
|
||||
using QSB.Utility;
|
||||
using QSB.WorldSync;
|
||||
using System;
|
||||
@ -25,7 +24,8 @@ namespace QSB.SectorSync.WorldObjects
|
||||
}
|
||||
}
|
||||
public Vector3 Position => Transform.position;
|
||||
public bool IsFakeSector => AttachedObject.GetType() == typeof(FakeSector);
|
||||
public bool IsFakeSector => AttachedObject is FakeSector;
|
||||
public FakeSector FakeSector => (FakeSector)AttachedObject;
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
@ -86,7 +86,7 @@ namespace QSB.SectorSync.WorldObjects
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!shuttleController.GetValue<bool>("_isPlayerInside"))
|
||||
if (!shuttleController._isPlayerInside)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
using OWML.Common;
|
||||
using QSB.LogSync.Events;
|
||||
using QSB.Player;
|
||||
using QSB.SectorSync;
|
||||
using QSB.SectorSync.WorldObjects;
|
||||
using QSB.Utility;
|
||||
using QSB.WorldSync;
|
||||
using QuantumUNET.Transport;
|
||||
using UnityEngine;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace QSB.Syncs.Sectored
|
||||
{
|
||||
@ -71,7 +72,7 @@ namespace QSB.Syncs.Sectored
|
||||
|
||||
private void InitSector()
|
||||
{
|
||||
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
|
||||
var closestSector = SectorSync.GetClosestSector();
|
||||
if (closestSector != null)
|
||||
{
|
||||
SetReferenceSector(closestSector);
|
||||
@ -156,6 +157,12 @@ namespace QSB.Syncs.Sectored
|
||||
|
||||
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
|
||||
{
|
||||
if (HasAuthority)
|
||||
{
|
||||
reader.ReadInt32();
|
||||
return;
|
||||
}
|
||||
|
||||
int sectorId;
|
||||
if (!WorldObjectManager.AllObjectsReady)
|
||||
{
|
||||
@ -212,7 +219,7 @@ namespace QSB.Syncs.Sectored
|
||||
{
|
||||
if (SectorSync.IsReady)
|
||||
{
|
||||
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
|
||||
var closestSector = SectorSync.GetClosestSector();
|
||||
if (closestSector != null)
|
||||
{
|
||||
SetReferenceSector(closestSector);
|
||||
|
@ -1,29 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace QSB.Utility
|
||||
{
|
||||
internal class RepeatingManager : MonoBehaviour
|
||||
{
|
||||
public static List<IRepeating> Repeatings = new();
|
||||
|
||||
private const float TimeInterval = 0.4f;
|
||||
private float _checkTimer = TimeInterval;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_checkTimer += Time.unscaledDeltaTime;
|
||||
if (_checkTimer < TimeInterval)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var repeat in Repeatings)
|
||||
{
|
||||
repeat.Invoke();
|
||||
}
|
||||
|
||||
_checkTimer = 0;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user