quantum-space-buddies/QSB/SectorSync/QSBSectorDetector.cs

179 lines
4.4 KiB
C#
Raw Normal View History

2021-03-09 19:45:00 +00:00
using OWML.Common;
using QSB.SectorSync.WorldObjects;
2021-02-19 12:42:37 +00:00
using QSB.Utility;
2021-03-09 19:45:00 +00:00
using QSB.WorldSync;
using System.Collections.Generic;
2020-08-22 16:08:56 +00:00
using System.Linq;
2020-08-21 13:04:13 +00:00
using UnityEngine;
2020-02-13 19:23:26 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.SectorSync;
public class QSBSectorDetector : MonoBehaviour
2020-02-15 19:48:02 +00:00
{
2022-03-03 03:46:33 +00:00
public readonly List<QSBSector> SectorList = new();
2021-02-19 12:54:48 +00:00
2022-03-03 03:46:33 +00:00
private SectorDetector _sectorDetector;
2021-03-09 19:45:00 +00:00
2022-03-03 03:46:33 +00:00
public void Init(SectorDetector detector)
{
if (_sectorDetector)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2020-12-02 21:23:01 +00:00
}
2020-02-15 19:48:02 +00:00
2022-03-03 03:46:33 +00:00
if (!detector)
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole("Error - Trying to init QSBSectorDetector with null SectorDetector!", MessageType.Error);
return;
}
_sectorDetector = detector;
_sectorDetector.OnEnterSector += AddSector;
_sectorDetector.OnExitSector += RemoveSector;
2022-03-03 03:46:33 +00:00
_sectorDetector._sectorList.ForEach(AddSector);
}
2022-03-03 03:46:33 +00:00
public void Uninit()
{
if (!_sectorDetector)
{
return;
}
2022-03-03 03:46:33 +00:00
_sectorDetector.OnEnterSector -= AddSector;
_sectorDetector.OnExitSector -= RemoveSector;
_sectorDetector = null;
SectorList.Clear();
}
private void AddSector(Sector sector)
{
if (!sector)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
// wtf
DebugLog.ToConsole($"Warning - Trying to add {sector.name} for {gameObject.name}, but it is null", MessageType.Warning);
return;
2021-03-09 19:45:00 +00:00
}
2022-03-03 03:46:33 +00:00
var worldObject = sector.GetWorldObject<QSBSector>();
if (worldObject == null)
2021-03-09 19:45:00 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
2020-12-02 21:23:01 +00:00
}
2020-08-05 19:59:50 +00:00
2022-03-03 03:46:33 +00:00
if (SectorList.Contains(worldObject))
2021-03-09 19:45:00 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - Trying to add {sector.name} for {gameObject.name}, but is already in list", MessageType.Warning);
return;
}
2021-06-19 10:26:05 +00:00
2022-03-03 03:46:33 +00:00
SectorList.Add(worldObject);
}
2021-03-09 21:56:20 +00:00
2022-03-03 03:46:33 +00:00
private void RemoveSector(Sector sector)
{
if (!sector)
{
// wtf
DebugLog.ToConsole($"Warning - Trying to remove {sector.name} for {gameObject.name}, but it is null", MessageType.Warning);
return;
}
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
var worldObject = sector.GetWorldObject<QSBSector>();
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
if (!SectorList.Contains(worldObject))
{
DebugLog.ToConsole($"Warning - Trying to remove {sector.name} for {gameObject.name}, but is not in list!", MessageType.Warning);
return;
}
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
SectorList.Remove(worldObject);
}
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
/// <summary>
/// called only by the sector manager
/// </summary>
public QSBSector GetClosestSector()
{
var type = _sectorDetector._occupantType;
2021-12-18 20:18:41 +00:00
2022-03-03 03:46:33 +00:00
var validSectors = SectorList.Where(x => x.ShouldSyncTo(type)).ToList();
var inASector = validSectors.Count > 0;
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
if (!inASector)
{
validSectors = QSBWorldSync.GetWorldObjects<QSBSector>()
.Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed && x.ShouldSyncTo(type))
.ToList();
}
2021-03-13 10:19:02 +00:00
2022-03-03 03:46:33 +00:00
if (validSectors.Count == 0)
{
return null;
2021-03-09 21:56:20 +00:00
}
2022-03-03 03:46:33 +00:00
var closest = validSectors
.MinBy(sector => CalculateSectorScore(sector, _sectorDetector._attachedRigidbody));
if (inASector)
2021-04-21 10:02:17 +00:00
{
2022-03-03 03:46:33 +00:00
var pos = _sectorDetector._attachedRigidbody.GetPosition();
bool IsSameDistanceAsClosest(QSBSector fakeSector)
=> OWMath.ApproxEquals(
Vector3.Distance(fakeSector.Position, pos),
Vector3.Distance(closest.Position, pos),
0.01f);
2021-04-21 10:02:17 +00:00
2022-03-03 03:46:33 +00:00
bool IsAttachedValid(QSBSector fakeSector)
=> validSectors.Any(x => x.AttachedObject == fakeSector.FakeSector.AttachedSector);
var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors
.FirstOrDefault(x => IsSameDistanceAsClosest(x) && IsAttachedValid(x));
return fakeToSyncTo ?? closest;
2021-04-21 10:02:17 +00:00
}
2022-03-03 03:46:33 +00:00
return closest;
}
private static float CalculateSectorScore(QSBSector sector, OWRigidbody rigidbody)
{
var distance = (sector.Position - rigidbody.GetPosition()).sqrMagnitude;
var radius = GetRadius(sector);
var velocity = GetRelativeVelocity(sector, rigidbody);
return distance + Mathf.Pow(radius, 2) + velocity;
}
private static float GetRadius(QSBSector sector)
{
// TODO : make this work for other stuff, not just shaped triggervolumes
var trigger = sector.AttachedObject.GetTriggerVolume();
if (trigger && trigger.GetShape())
2021-03-09 21:56:20 +00:00
{
2022-03-03 03:46:33 +00:00
return trigger.GetShape().CalcWorldBounds().radius;
2021-03-09 19:45:00 +00:00
}
2021-06-19 10:26:05 +00:00
2022-03-03 03:46:33 +00:00
return 0f;
}
private static float GetRelativeVelocity(QSBSector sector, OWRigidbody rigidbody)
{
var sectorRigidBody = sector.AttachedObject.GetOWRigidbody();
if (sectorRigidBody && rigidbody)
2021-04-21 10:02:17 +00:00
{
2022-03-03 03:46:33 +00:00
var relativeVelocity = sectorRigidBody.GetRelativeVelocity(rigidbody);
return relativeVelocity.sqrMagnitude;
2021-04-21 10:02:17 +00:00
}
2022-03-03 03:46:33 +00:00
return 0;
2020-12-14 21:20:53 +00:00
}
}