2021-06-18 01:49:18 -07:00
|
|
|
using System.Collections.Generic;
|
2021-09-20 19:06:48 +10:00
|
|
|
using Content.Server.Hands.Components;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Interaction;
|
|
|
|
|
using Content.Server.Storage.Components;
|
2021-09-20 19:06:48 +10:00
|
|
|
using Content.Shared.Movement;
|
2020-08-13 22:17:12 +10:00
|
|
|
using JetBrains.Annotations;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.Player;
|
2021-03-01 15:24:46 -08:00
|
|
|
using Robust.Shared.Containers;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-09-20 19:06:48 +10:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Timing;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
2021-07-25 08:41:50 -07:00
|
|
|
namespace Content.Server.Storage.EntitySystems
|
2018-10-25 17:35:34 -07:00
|
|
|
{
|
2020-08-13 22:17:12 +10:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
internal sealed class StorageSystem : EntitySystem
|
2018-10-25 17:35:34 -07:00
|
|
|
{
|
2021-09-20 19:06:48 +10:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly List<IPlayerSession> _sessionCache = new();
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
2021-04-09 16:08:12 +02:00
|
|
|
base.Initialize();
|
|
|
|
|
|
2020-02-19 17:08:59 -08:00
|
|
|
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
|
|
|
|
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
2021-09-20 19:06:48 +10:00
|
|
|
SubscribeLocalEvent<EntityStorageComponent, RelayMovementEntityEvent>(OnRelayMovement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRelayMovement(EntityUid uid, EntityStorageComponent component, RelayMovementEntityEvent args)
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.HasComponent<HandsComponent>(uid))
|
2021-09-20 19:06:48 +10:00
|
|
|
{
|
|
|
|
|
if (_gameTiming.CurTime <
|
|
|
|
|
component.LastInternalOpenAttempt + EntityStorageComponent.InternalOpenAttemptDelay)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
component.LastInternalOpenAttempt = _gameTiming.CurTime;
|
|
|
|
|
component.TryOpenStorage(args.Entity);
|
|
|
|
|
}
|
2018-11-11 11:32:05 -08:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 17:35:34 -07:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var component in EntityManager.EntityQuery<ServerStorageComponent>(true))
|
2018-10-25 17:35:34 -07:00
|
|
|
{
|
2020-08-13 22:17:12 +10:00
|
|
|
CheckSubscribedEntities(component);
|
2018-11-11 11:32:05 -08:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
2019-05-05 13:09:21 +02:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
2018-11-11 11:32:05 -08:00
|
|
|
{
|
2019-05-05 13:09:21 +02:00
|
|
|
storageComp.HandleEntityMaybeInserted(message);
|
2018-11-11 11:32:05 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 22:17:12 +10:00
|
|
|
private void CheckSubscribedEntities(ServerStorageComponent storageComp)
|
2018-11-11 11:32:05 -08:00
|
|
|
{
|
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
|
|
|
|
2020-08-13 22:17:12 +10:00
|
|
|
var storagePos = storageComp.Owner.Transform.WorldPosition;
|
|
|
|
|
var storageMap = storageComp.Owner.Transform.MapID;
|
2018-11-11 11:32:05 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|