2021-03-09 19:45:00 +00:00
|
|
|
|
using OWML.Common;
|
2020-12-31 12:10:55 +00:00
|
|
|
|
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-01-22 00:37:30 +00:00
|
|
|
|
{
|
2022-03-20 02:55:19 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - Trying to init QSBSectorDetector {name} with null SectorDetector!", MessageType.Error);
|
2022-03-03 03:46:33 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sectorDetector = detector;
|
|
|
|
|
_sectorDetector.OnEnterSector += AddSector;
|
|
|
|
|
_sectorDetector.OnExitSector += RemoveSector;
|
2022-01-22 00:37:30 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
_sectorDetector._sectorList.ForEach(AddSector);
|
|
|
|
|
}
|
2022-01-22 00:37:30 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public void Uninit()
|
|
|
|
|
{
|
|
|
|
|
if (!_sectorDetector)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2022-01-22 00:37:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2022-03-20 02:55:19 +00:00
|
|
|
|
DebugLog.ToConsole($"Warning - Trying to add null sector for QSBSectorDetector {name}", MessageType.Error);
|
2022-03-03 03:46:33 +00:00
|
|
|
|
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-20 02:55:19 +00:00
|
|
|
|
SectorList.SafeAdd(worldObject);
|
2022-03-03 03:46:33 +00:00
|
|
|
|
}
|
2021-03-09 21:56:20 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
private void RemoveSector(Sector sector)
|
|
|
|
|
{
|
|
|
|
|
if (!sector)
|
|
|
|
|
{
|
|
|
|
|
// wtf
|
2022-03-20 02:55:19 +00:00
|
|
|
|
DebugLog.ToConsole($"Warning - Trying to remove null sector for QSBSectorDetector {name}", MessageType.Error);
|
2022-03-03 03:46:33 +00:00
|
|
|
|
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-20 02:55:19 +00:00
|
|
|
|
SectorList.QuickRemove(worldObject);
|
2022-03-03 03:46:33 +00:00
|
|
|
|
}
|
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-20 07:13:40 +00:00
|
|
|
|
var validSectors = SectorList
|
|
|
|
|
.Where(x => x.ShouldSyncTo(type))
|
|
|
|
|
.ToList();
|
2021-12-18 10:33:18 +00:00
|
|
|
|
|
2022-03-20 07:13:40 +00:00
|
|
|
|
if (validSectors.Count == 0)
|
2022-03-03 03:46:33 +00:00
|
|
|
|
{
|
|
|
|
|
validSectors = QSBWorldSync.GetWorldObjects<QSBSector>()
|
2022-03-20 09:36:14 +00:00
|
|
|
|
.Where(x =>
|
|
|
|
|
// we only wanna sync to the major ones when far away
|
|
|
|
|
x.Type != Sector.Name.Unnamed &&
|
|
|
|
|
x.ShouldSyncTo(type))
|
2022-03-03 03:46:33 +00:00
|
|
|
|
.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-20 07:13:40 +00:00
|
|
|
|
return validSectors
|
2022-03-20 07:50:17 +00:00
|
|
|
|
.MinBy(x => x.GetScore(_sectorDetector._attachedRigidbody));
|
2022-03-03 03:46:33 +00:00
|
|
|
|
}
|
2022-03-20 02:55:19 +00:00
|
|
|
|
}
|