2022-01-28 20:49:07 -08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using OWML.Common;
|
2021-12-22 14:46:37 -08:00
|
|
|
|
using QSB.Messaging;
|
2021-01-05 15:56:14 +00:00
|
|
|
|
using QSB.Player;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.QuantumSync.Messages;
|
2021-01-03 16:09:58 +00:00
|
|
|
|
using QSB.QuantumSync.WorldObjects;
|
2021-01-31 16:14:17 +00:00
|
|
|
|
using QSB.Utility;
|
2020-12-31 12:10:55 +00:00
|
|
|
|
using QSB.WorldSync;
|
2021-02-21 18:25:25 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-18 12:33:07 +00:00
|
|
|
|
using System.Linq;
|
2022-01-28 20:49:07 -08:00
|
|
|
|
using System.Threading;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.QuantumSync;
|
|
|
|
|
|
2023-07-28 19:30:57 +01:00
|
|
|
|
public class QuantumManager : WorldObjectManager
|
2020-12-22 21:39:53 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
|
2021-12-20 18:41:12 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public static QuantumShrine Shrine { get; private set; }
|
2020-12-22 21:39:53 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void Awake() => QSBPlayerManager.OnRemovePlayer += PlayerLeave;
|
2020-12-23 22:43:05 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite("Building quantum objects...", MessageType.Info);
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumState, QuantumState>();
|
|
|
|
|
QSBWorldSync.Init<QSBSocketedQuantumObject, SocketedQuantumObject>();
|
|
|
|
|
QSBWorldSync.Init<QSBMultiStateQuantumObject, MultiStateQuantumObject>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumSocket, QuantumSocket>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumShuffleObject, QuantumShuffleObject>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumMoon, QuantumMoon>();
|
|
|
|
|
QSBWorldSync.Init<QSBEyeProxyQuantumMoon, EyeProxyQuantumMoon>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumSkeletonTower, QuantumSkeletonTower>();
|
|
|
|
|
if (scene == OWScene.SolarSystem)
|
2020-12-22 21:39:53 +00:00
|
|
|
|
{
|
2022-05-03 08:48:24 +01:00
|
|
|
|
Shrine = QSBWorldSync.GetUnityObject<QuantumShrine>();
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-18 15:50:20 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void PlayerLeave(PlayerInfo player)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
foreach (var obj in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
|
2021-02-14 12:47:40 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (obj.ControllingPlayer == player.PlayerId)
|
2021-03-23 13:18:29 +00:00
|
|
|
|
{
|
2023-05-08 11:38:24 -07:00
|
|
|
|
obj.SendMessage(new QuantumOwnershipMessage(obj.IsEnabled ? QSBPlayerManager.LocalPlayerId : 0u));
|
2021-03-23 13:18:29 +00:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void OnRenderObject()
|
|
|
|
|
{
|
|
|
|
|
if (!QSBCore.DebugSettings.DrawLines)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2021-02-14 12:47:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (Shrine != null)
|
2021-01-03 16:09:58 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
Popcron.Gizmos.Sphere(Shrine.transform.position, 10f, Color.magenta);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-18 12:33:07 +00:00
|
|
|
|
|
2022-03-06 22:36:52 -08:00
|
|
|
|
public static (bool FoundPlayers, List<PlayerInfo> PlayersWhoCanSee) IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool ignoreLocalCamera)
|
2022-03-02 19:46:33 -08:00
|
|
|
|
{
|
|
|
|
|
if (!QSBWorldSync.AllObjectsReady)
|
|
|
|
|
{
|
2022-03-06 22:36:52 -08:00
|
|
|
|
return (false, new List<PlayerInfo>());
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var playersWithCameras = QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera);
|
|
|
|
|
if (playersWithCameras.Count == 0)
|
2021-01-31 14:21:54 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
DebugLog.ToConsole($"Warning - Could not find any players with cameras!", MessageType.Warning);
|
2022-03-06 22:36:52 -08:00
|
|
|
|
return (false, new List<PlayerInfo>());
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2021-07-11 16:18:47 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (!tracker.gameObject.activeInHierarchy)
|
|
|
|
|
{
|
2022-03-06 22:36:52 -08:00
|
|
|
|
return (false, new List<PlayerInfo>());
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var playersWhoCanSee = new List<PlayerInfo>();
|
|
|
|
|
var foundPlayers = false;
|
|
|
|
|
foreach (var player in playersWithCameras)
|
|
|
|
|
{
|
|
|
|
|
if (player.Camera == null)
|
2021-03-25 20:56:26 +00:00
|
|
|
|
{
|
2022-03-06 01:01:02 -08:00
|
|
|
|
DebugLog.ToConsole($"Warning - Camera is null for id:{player}!", MessageType.Warning);
|
2022-03-02 19:46:33 -08:00
|
|
|
|
continue;
|
2021-03-25 20:56:26 +00:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var isInFrustum = tracker.IsInFrustum(player.Camera.GetFrustumPlanes());
|
|
|
|
|
if (isInFrustum)
|
2021-03-25 20:56:26 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
playersWhoCanSee.Add(player);
|
|
|
|
|
foundPlayers = true;
|
2021-03-25 20:56:26 +00:00
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2021-01-31 14:21:54 +00:00
|
|
|
|
|
2022-03-06 22:36:52 -08:00
|
|
|
|
return (foundPlayers, playersWhoCanSee);
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2022-01-02 21:26:52 -08:00
|
|
|
|
|
2022-03-06 03:27:15 -08:00
|
|
|
|
public static bool IsVisible(ShapeVisibilityTracker tracker, bool ignoreLocalCamera) =>
|
|
|
|
|
tracker.gameObject.activeInHierarchy
|
2022-03-06 22:36:52 -08:00
|
|
|
|
&& IsVisibleUsingCameraFrustum(tracker, ignoreLocalCamera).FoundPlayers
|
2022-03-06 03:27:15 -08:00
|
|
|
|
&& QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera)
|
|
|
|
|
.Any(x => VisibilityOccluder.CanYouSee(tracker, x.Camera.mainCamera.transform.position));
|
2022-01-18 15:50:20 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public static IEnumerable<PlayerInfo> GetEntangledPlayers(QuantumObject obj)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBWorldSync.AllObjectsReady)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<PlayerInfo>();
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var worldObj = obj.GetWorldObject<IQSBQuantumObject>();
|
|
|
|
|
return QSBPlayerManager.PlayerList.Where(x => x.EntangledObject == worldObj);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 17:57:27 +01:00
|
|
|
|
public static void OnTakeProbeSnapshot(PlayerInfo player, ProbeCamera.ID cameraId)
|
|
|
|
|
{
|
|
|
|
|
foreach (var quantumObject in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
|
|
|
|
|
{
|
2023-02-08 12:10:17 +00:00
|
|
|
|
if (quantumObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId || quantumObject.HostControls)
|
2022-08-27 17:57:27 +01:00
|
|
|
|
{
|
|
|
|
|
quantumObject.OnTakeProbeSnapshot(player, cameraId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 11:08:56 +01:00
|
|
|
|
public static void OnRemoveProbeSnapshot(PlayerInfo player)
|
|
|
|
|
{
|
|
|
|
|
foreach (var quantumObject in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
|
|
|
|
|
{
|
2023-02-08 12:10:17 +00:00
|
|
|
|
if (quantumObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId || quantumObject.HostControls)
|
2022-08-28 11:08:56 +01:00
|
|
|
|
{
|
|
|
|
|
quantumObject.OnRemoveProbeSnapshot(player);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-06 03:27:15 -08:00
|
|
|
|
}
|