2018-10-25 17:35:34 -07:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-06 14:27:03 -07:00
|
|
|
|
using Content.Server.GameObjects;
|
2020-07-17 11:03:07 +02:00
|
|
|
|
using Content.Server.GameObjects.Components.Items.Storage;
|
2020-07-06 14:27:03 -07:00
|
|
|
|
using Content.Server.GameObjects.EntitySystems.Click;
|
2019-05-05 13:09:21 +02:00
|
|
|
|
using Robust.Server.GameObjects.EntitySystemMessages;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Server.Interfaces.Player;
|
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
|
using Robust.Shared.GameObjects.Systems;
|
|
|
|
|
|
using Robust.Shared.Interfaces.GameObjects;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-07-06 14:27:03 -07:00
|
|
|
|
namespace Content.Server.Interfaces.GameObjects.Components.Interaction
|
2018-10-25 17:35:34 -07:00
|
|
|
|
{
|
|
|
|
|
|
class StorageSystem : EntitySystem
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly List<IPlayerSession> _sessionCache = new List<IPlayerSession>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
2020-02-19 17:08:59 -08:00
|
|
|
|
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
|
|
|
|
|
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
2020-02-18 19:43:54 -08:00
|
|
|
|
|
|
|
|
|
|
EntityQuery = new TypeEntityQuery(typeof(ServerStorageComponent));
|
2018-11-11 11:32:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-25 17:35:34 -07:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var entity in RelevantEntities)
|
|
|
|
|
|
{
|
2018-11-11 11:32:05 -08:00
|
|
|
|
CheckSubscribedEntities(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-02-19 14:39:00 -08:00
|
|
|
|
private static void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
2018-11-11 11:32:05 -08:00
|
|
|
|
{
|
2019-05-05 13:09:21 +02:00
|
|
|
|
var oldParentEntity = message.Container.Owner;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2019-05-05 13:09:21 +02:00
|
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
|
|
|
|
|
|
{
|
|
|
|
|
|
storageComp.HandleEntityMaybeRemoved(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-02-19 14:39:00 -08:00
|
|
|
|
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
|
2019-05-05 13:09:21 +02:00
|
|
|
|
{
|
|
|
|
|
|
var oldParentEntity = message.Container.Owner;
|
2018-11-11 11:32:05 -08:00
|
|
|
|
|
|
|
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
|
|
|
|
|
|
{
|
2019-05-05 13:09:21 +02:00
|
|
|
|
storageComp.HandleEntityMaybeInserted(message);
|
2018-11-11 11:32:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckSubscribedEntities(IEntity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
var storageComp = entity.GetComponent<ServerStorageComponent>();
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
// We have to cache the set of sessions because Unsubscribe modifies the original.
|
|
|
|
|
|
_sessionCache.Clear();
|
|
|
|
|
|
_sessionCache.AddRange(storageComp.SubscribedSessions);
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
if (_sessionCache.Count == 0)
|
|
|
|
|
|
return;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
var storagePos = entity.Transform.WorldPosition;
|
|
|
|
|
|
var storageMap = entity.Transform.MapID;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var session in _sessionCache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var attachedEntity = session.AttachedEntity;
|
|
|
|
|
|
|
|
|
|
|
|
// The component manages the set of sessions, so this invalid session should be removed soon.
|
|
|
|
|
|
if (attachedEntity == null || !attachedEntity.IsValid())
|
|
|
|
|
|
continue;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
if (storageMap != attachedEntity.Transform.MapID)
|
|
|
|
|
|
continue;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
var distanceSquared = (storagePos - attachedEntity.Transform.WorldPosition).LengthSquared;
|
2019-05-16 15:51:26 +02:00
|
|
|
|
if (distanceSquared > InteractionSystem.InteractionRangeSquared)
|
2018-11-11 11:32:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
storageComp.UnsubscribeSession(session);
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|