Re-organize all projects (#4166)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
[ComponentReference(typeof(EntityStorageComponent))]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IStorageComponent))]
|
||||
[RegisterComponent]
|
||||
public class CursedEntityStorageComponent : EntityStorageComponent
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
public override string Name => "CursedEntityStorage";
|
||||
|
||||
protected override void CloseStorage()
|
||||
{
|
||||
base.CloseStorage();
|
||||
|
||||
// No contents, we do nothing
|
||||
if (Contents.ContainedEntities.Count == 0) return;
|
||||
|
||||
var lockers = Owner.EntityManager.GetEntities(new TypeEntityQuery(typeof(EntityStorageComponent))).ToList();
|
||||
|
||||
if (lockers.Contains(Owner))
|
||||
lockers.Remove(Owner);
|
||||
|
||||
var lockerEnt = _robustRandom.Pick(lockers);
|
||||
|
||||
if (lockerEnt == null) return; // No valid lockers anywhere.
|
||||
|
||||
var locker = lockerEnt.GetComponent<EntityStorageComponent>();
|
||||
|
||||
if(locker.Open)
|
||||
locker.TryCloseStorage(Owner);
|
||||
|
||||
foreach (var entity in Contents.ContainedEntities.ToArray())
|
||||
{
|
||||
Contents.ForceRemove(entity);
|
||||
locker.Insert(entity);
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Effects/teleport_departure.ogg", Owner, AudioHelpers.WithVariation(0.125f));
|
||||
SoundSystem.Play(Filter.Pvs(lockerEnt), "/Audio/Effects/teleport_arrival.ogg", lockerEnt, AudioHelpers.WithVariation(0.125f));
|
||||
}
|
||||
}
|
||||
}
|
||||
502
Content.Server/Storage/Components/EntityStorageComponent.cs
Normal file
502
Content.Server/Storage/Components/EntityStorageComponent.cs
Normal file
@@ -0,0 +1,502 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Placeable;
|
||||
using Content.Server.Tools.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Storage;
|
||||
using Content.Shared.Tool;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IStorageComponent))]
|
||||
public class EntityStorageComponent : Component, IActivate, IStorageComponent, IInteractUsing, IDestroyAct, IActionBlocker, IExAct
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override string Name => "EntityStorage";
|
||||
|
||||
private const float MaxSize = 1.0f; // maximum width or height of an entity allowed inside the storage.
|
||||
|
||||
private static readonly TimeSpan InternalOpenAttemptDelay = TimeSpan.FromSeconds(0.5);
|
||||
private TimeSpan _lastInternalOpenAttempt;
|
||||
|
||||
private const int OpenMask = (int) (
|
||||
CollisionGroup.MobImpassable |
|
||||
CollisionGroup.VaultImpassable |
|
||||
CollisionGroup.SmallImpassable);
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("Capacity")]
|
||||
private int _storageCapacityMax = 30;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("IsCollidableWhenOpen")]
|
||||
private bool _isCollidableWhenOpen;
|
||||
|
||||
[ViewVariables]
|
||||
protected IEntityQuery? EntityQuery;
|
||||
|
||||
[DataField("showContents")]
|
||||
private bool _showContents;
|
||||
|
||||
[DataField("occludesLight")]
|
||||
private bool _occludesLight = true;
|
||||
|
||||
[DataField("open")]
|
||||
private bool _open;
|
||||
|
||||
[DataField("CanWeldShut")]
|
||||
private bool _canWeldShut = true;
|
||||
|
||||
[DataField("IsWeldedShut")]
|
||||
private bool _isWeldedShut;
|
||||
|
||||
[DataField("closeSound")]
|
||||
private string _closeSound = "/Audio/Machines/closetclose.ogg";
|
||||
|
||||
[DataField("openSound")]
|
||||
private string _openSound = "/Audio/Machines/closetopen.ogg";
|
||||
|
||||
[ViewVariables]
|
||||
protected Container Contents = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the container contents should be drawn when the container is closed.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool ShowContents
|
||||
{
|
||||
get => _showContents;
|
||||
set
|
||||
{
|
||||
_showContents = value;
|
||||
Contents.ShowContents = _showContents;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool OccludesLight
|
||||
{
|
||||
get => _occludesLight;
|
||||
set
|
||||
{
|
||||
_occludesLight = value;
|
||||
Contents.OccludesLight = _occludesLight;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool Open
|
||||
{
|
||||
get => _open;
|
||||
private set => _open = value;
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool IsWeldedShut
|
||||
{
|
||||
get => _isWeldedShut;
|
||||
set
|
||||
{
|
||||
if (_isWeldedShut == value) return;
|
||||
|
||||
_isWeldedShut = value;
|
||||
UpdateAppearance();
|
||||
}
|
||||
}
|
||||
|
||||
private bool _beingWelded;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool CanWeldShut {
|
||||
get => _canWeldShut;
|
||||
set
|
||||
{
|
||||
if (_canWeldShut == value) return;
|
||||
|
||||
_canWeldShut = value;
|
||||
UpdateAppearance();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Contents = Owner.EnsureContainer<Container>(nameof(EntityStorageComponent));
|
||||
EntityQuery = new IntersectingEntityQuery(Owner);
|
||||
|
||||
Contents.ShowContents = _showContents;
|
||||
Contents.OccludesLight = _occludesLight;
|
||||
|
||||
if (Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
|
||||
{
|
||||
placeableSurfaceComponent.IsPlaceable = Open;
|
||||
}
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public virtual void Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
ToggleOpen(eventArgs.User);
|
||||
}
|
||||
|
||||
public virtual bool CanOpen(IEntity user, bool silent = false)
|
||||
{
|
||||
if (IsWeldedShut)
|
||||
{
|
||||
if(!silent) Owner.PopupMessage(user, Loc.GetString("It's welded completely shut!"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool CanClose(IEntity user, bool silent = false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ToggleOpen(IEntity user)
|
||||
{
|
||||
if (Open)
|
||||
{
|
||||
TryCloseStorage(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
TryOpenStorage(user);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void CloseStorage()
|
||||
{
|
||||
Open = false;
|
||||
EntityQuery ??= new IntersectingEntityQuery(Owner);
|
||||
var entities = Owner.EntityManager.GetEntities(EntityQuery);
|
||||
var count = 0;
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
// prevents taking items out of inventories, out of containers, and orphaning child entities
|
||||
if(!entity.Transform.IsMapTransform)
|
||||
continue;
|
||||
|
||||
// only items that can be stored in an inventory, or a mob, can be eaten by a locker
|
||||
if (!entity.HasComponent<SharedItemComponent>() &&
|
||||
!entity.HasComponent<IBody>())
|
||||
continue;
|
||||
|
||||
if (!AddToContents(entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
if (count >= _storageCapacityMax)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ModifyComponents();
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _closeSound, Owner);
|
||||
_lastInternalOpenAttempt = default;
|
||||
}
|
||||
|
||||
protected virtual void OpenStorage()
|
||||
{
|
||||
Open = true;
|
||||
EmptyContents();
|
||||
ModifyComponents();
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _openSound, Owner);
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(StorageVisuals.CanWeld, _canWeldShut);
|
||||
appearance.SetData(StorageVisuals.Welded, _isWeldedShut);
|
||||
}
|
||||
}
|
||||
|
||||
private void ModifyComponents()
|
||||
{
|
||||
if (!_isCollidableWhenOpen && Owner.TryGetComponent<IPhysBody>(out var physics))
|
||||
{
|
||||
if (Open)
|
||||
{
|
||||
foreach (var fixture in physics.Fixtures)
|
||||
{
|
||||
fixture.CollisionLayer &= ~OpenMask;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var fixture in physics.Fixtures)
|
||||
{
|
||||
fixture.CollisionLayer |= OpenMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
|
||||
{
|
||||
placeableSurfaceComponent.IsPlaceable = Open;
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(StorageVisuals.Open, Open);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual bool AddToContents(IEntity entity)
|
||||
{
|
||||
if (entity == Owner) return false;
|
||||
if (entity.TryGetComponent(out IPhysBody? entityPhysicsComponent))
|
||||
{
|
||||
if (MaxSize < entityPhysicsComponent.GetWorldAABB().Size.X
|
||||
|| MaxSize < entityPhysicsComponent.GetWorldAABB().Size.Y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return Contents.CanInsert(entity) && Insert(entity);
|
||||
}
|
||||
|
||||
public virtual Vector2 ContentsDumpPosition()
|
||||
{
|
||||
return Owner.Transform.WorldPosition;
|
||||
}
|
||||
|
||||
private void EmptyContents()
|
||||
{
|
||||
foreach (var contained in Contents.ContainedEntities.ToArray())
|
||||
{
|
||||
if (Contents.Remove(contained))
|
||||
{
|
||||
contained.Transform.WorldPosition = ContentsDumpPosition();
|
||||
if (contained.TryGetComponent<IPhysBody>(out var physics))
|
||||
{
|
||||
physics.CanCollide = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case RelayMovementEntityMessage msg:
|
||||
if (msg.Entity.HasComponent<HandsComponent>())
|
||||
{
|
||||
if (_gameTiming.CurTime <
|
||||
_lastInternalOpenAttempt + InternalOpenAttemptDelay)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
_lastInternalOpenAttempt = _gameTiming.CurTime;
|
||||
TryOpenStorage(msg.Entity);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool TryOpenStorage(IEntity user)
|
||||
{
|
||||
if (!CanOpen(user)) return false;
|
||||
OpenStorage();
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool TryCloseStorage(IEntity user)
|
||||
{
|
||||
if (!CanClose(user)) return false;
|
||||
CloseStorage();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Remove(IEntity entity)
|
||||
{
|
||||
return Contents.CanRemove(entity);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Insert(IEntity entity)
|
||||
{
|
||||
// Trying to add while open just dumps it on the ground below us.
|
||||
if (Open)
|
||||
{
|
||||
entity.Transform.WorldPosition = Owner.Transform.WorldPosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!Contents.Insert(entity)) return false;
|
||||
|
||||
entity.Transform.LocalPosition = Vector2.Zero;
|
||||
if (entity.TryGetComponent(out IPhysBody? body))
|
||||
{
|
||||
body.CanCollide = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool CanInsert(IEntity entity)
|
||||
{
|
||||
if (Open)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Contents.ContainedEntities.Count >= _storageCapacityMax)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Contents.CanInsert(entity);
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (_beingWelded)
|
||||
return false;
|
||||
|
||||
if (Open)
|
||||
{
|
||||
_beingWelded = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CanWeldShut)
|
||||
{
|
||||
_beingWelded = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Contents.Contains(eventArgs.User))
|
||||
{
|
||||
_beingWelded = false;
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("It's too Cramped!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!eventArgs.Using.TryGetComponent(out WelderComponent? tool) || !tool.WelderLit)
|
||||
{
|
||||
_beingWelded = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_beingWelded)
|
||||
return false;
|
||||
|
||||
_beingWelded = true;
|
||||
|
||||
if (!await tool.UseTool(eventArgs.User, Owner, 1f, ToolQuality.Welding, 1f))
|
||||
{
|
||||
_beingWelded = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
_beingWelded = false;
|
||||
IsWeldedShut ^= true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
Open = true;
|
||||
EmptyContents();
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class OpenToggleVerb : Verb<EntityStorageComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, EntityStorageComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
component.OpenVerbGetData(user, component, data);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Activate(IEntity user, EntityStorageComponent component)
|
||||
{
|
||||
component.ToggleOpen(user);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OpenVerbGetData(IEntity user, EntityStorageComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsWeldedShut)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Disabled;
|
||||
var verb = Loc.GetString(component.Open ? "Close" : "Open");
|
||||
data.Text = Loc.GetString("{0} (welded shut)", verb);
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString(component.Open ? "Close" : "Open");
|
||||
data.IconTexture = component.Open ? "/Textures/Interface/VerbIcons/close.svg.192dpi.png" : "/Textures/Interface/VerbIcons/open.svg.192dpi.png";
|
||||
}
|
||||
|
||||
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Severity < ExplosionSeverity.Heavy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var containedEntities = Contents.ContainedEntities.ToList();
|
||||
foreach (var entity in containedEntities)
|
||||
{
|
||||
var exActs = entity.GetAllComponents<IExAct>().ToArray();
|
||||
foreach (var exAct in exActs)
|
||||
{
|
||||
exAct.OnExplosion(eventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Content.Server/Storage/Components/IStorageComponent.cs
Normal file
11
Content.Server/Storage/Components/IStorageComponent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
public interface IStorageComponent
|
||||
{
|
||||
bool Remove(IEntity entity);
|
||||
bool Insert(IEntity entity);
|
||||
bool CanInsert(IEntity entity);
|
||||
}
|
||||
}
|
||||
118
Content.Server/Storage/Components/SecretStashComponent.cs
Normal file
118
Content.Server/Storage/Components/SecretStashComponent.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
#nullable enable
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Notification;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Logic for secret single slot stash, like plant pot or toilet cistern
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class SecretStashComponent : Component, IDestroyAct
|
||||
{
|
||||
public override string Name => "SecretStash";
|
||||
|
||||
[ViewVariables] [DataField("maxItemSize")]
|
||||
private int _maxItemSize = (int) ReferenceSizes.Pocket;
|
||||
|
||||
[ViewVariables] [DataField("secretPartName")]
|
||||
private readonly string? _secretPartNameOverride = null;
|
||||
|
||||
[ViewVariables] private ContainerSlot _itemContainer = default!;
|
||||
|
||||
public string SecretPartName => _secretPartNameOverride ?? Loc.GetString("{0:theName}", Owner);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_itemContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "stash", out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to hide item inside secret stash from hands of user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="itemToHide"></param>
|
||||
/// <returns>True if item was hidden inside stash</returns>
|
||||
public bool TryHideItem(IEntity user, IEntity itemToHide)
|
||||
{
|
||||
if (_itemContainer.ContainedEntity != null)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("There's already something in here?!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!itemToHide.TryGetComponent(out ItemComponent? item))
|
||||
return false;
|
||||
|
||||
if (item.Size > _maxItemSize)
|
||||
{
|
||||
Owner.PopupMessage(user,
|
||||
Loc.GetString("{0:TheName} is too big to fit in {1}!", itemToHide, SecretPartName));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!user.TryGetComponent(out IHandsComponent? hands))
|
||||
return false;
|
||||
|
||||
if (!hands.Drop(itemToHide, _itemContainer))
|
||||
return false;
|
||||
|
||||
Owner.PopupMessage(user, Loc.GetString("comp-secret-stash-action-hide", ("item", itemToHide), ("this", SecretPartName)));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try get item and place it in users hand
|
||||
/// If user can't take it by hands, will drop item from container
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns>True if user recieved item</returns>
|
||||
public bool TryGetItem(IEntity user)
|
||||
{
|
||||
if (_itemContainer.ContainedEntity == null)
|
||||
return false;
|
||||
|
||||
Owner.PopupMessage(user, Loc.GetString("There was something inside {0}!", SecretPartName));
|
||||
|
||||
if (user.TryGetComponent(out HandsComponent? hands))
|
||||
{
|
||||
if (!_itemContainer.ContainedEntity.TryGetComponent(out ItemComponent? item))
|
||||
return false;
|
||||
hands.PutInHandOrDrop(item);
|
||||
}
|
||||
else if (_itemContainer.Remove(_itemContainer.ContainedEntity))
|
||||
{
|
||||
_itemContainer.ContainedEntity.Transform.Coordinates = Owner.Transform.Coordinates;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is there something inside secret stash item container?
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool HasItemInside()
|
||||
{
|
||||
return _itemContainer.ContainedEntity != null;
|
||||
}
|
||||
|
||||
public void OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
// drop item inside
|
||||
if (_itemContainer.ContainedEntity != null)
|
||||
{
|
||||
_itemContainer.ContainedEntity.Transform.Coordinates = Owner.Transform.Coordinates;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using Content.Server.Access.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Storage;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(EntityStorageComponent))]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IStorageComponent))]
|
||||
public class SecureEntityStorageComponent : EntityStorageComponent
|
||||
{
|
||||
public override string Name => "SecureEntityStorage";
|
||||
[DataField("locked")]
|
||||
private bool _locked = true;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool Locked
|
||||
{
|
||||
get => _locked;
|
||||
set
|
||||
{
|
||||
_locked = value;
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(StorageVisuals.Locked, _locked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(StorageVisuals.CanLock, true);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (Locked)
|
||||
{
|
||||
DoToggleLock(eventArgs.User);
|
||||
return;
|
||||
}
|
||||
|
||||
base.Activate(eventArgs);
|
||||
}
|
||||
|
||||
public override bool CanOpen(IEntity user, bool silent = false)
|
||||
{
|
||||
if (Locked)
|
||||
{
|
||||
Owner.PopupMessage(user, "It's locked!");
|
||||
return false;
|
||||
}
|
||||
return base.CanOpen(user, silent);
|
||||
}
|
||||
|
||||
protected override void OpenVerbGetData(IEntity user, EntityStorageComponent component, VerbData data)
|
||||
{
|
||||
if (Locked)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
base.OpenVerbGetData(user, component, data);
|
||||
}
|
||||
|
||||
private void DoToggleLock(IEntity user)
|
||||
{
|
||||
if (Locked)
|
||||
{
|
||||
DoUnlock(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
DoLock(user);
|
||||
}
|
||||
}
|
||||
|
||||
private void DoUnlock(IEntity user)
|
||||
{
|
||||
if (!CheckAccess(user)) return;
|
||||
|
||||
Locked = false;
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/door_lock_off.ogg", Owner, AudioParams.Default.WithVolume(-5));
|
||||
}
|
||||
|
||||
private void DoLock(IEntity user)
|
||||
{
|
||||
if (!CheckAccess(user)) return;
|
||||
|
||||
Locked = true;
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/door_lock_on.ogg", Owner, AudioParams.Default.WithVolume(-5));
|
||||
}
|
||||
|
||||
private bool CheckAccess(IEntity user)
|
||||
{
|
||||
if (Owner.TryGetComponent(out AccessReader? reader))
|
||||
{
|
||||
if (!reader.IsAllowed(user))
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("Access denied"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class ToggleLockVerb : Verb<SecureEntityStorageComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, SecureEntityStorageComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user) || component.Open)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString(component.Locked ? "Unlock" : "Lock");
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, SecureEntityStorageComponent component)
|
||||
{
|
||||
component.DoToggleLock(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
630
Content.Server/Storage/Components/ServerStorageComponent.cs
Normal file
630
Content.Server/Storage/Components/ServerStorageComponent.cs
Normal file
@@ -0,0 +1,630 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.Placeable;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Storage;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Storage component for containing entities within this one, matches a UI on the client which shows stored entities
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IStorageComponent))]
|
||||
public class ServerStorageComponent : SharedStorageComponent, IInteractUsing, IUse, IActivate, IStorageComponent, IDestroyAct, IExAct, IAfterInteract
|
||||
{
|
||||
private const string LoggerName = "Storage";
|
||||
|
||||
private Container? _storage;
|
||||
private readonly Dictionary<IEntity, int> _sizeCache = new();
|
||||
|
||||
[DataField("occludesLight")]
|
||||
private bool _occludesLight = true;
|
||||
[DataField("quickInsert")]
|
||||
private bool _quickInsert; //Can insert storables by "attacking" them with the storage entity
|
||||
[DataField("areaInsert")]
|
||||
private bool _areaInsert; //"Attacking" with the storage entity causes it to insert all nearby storables after a delay
|
||||
private bool _storageInitialCalculated;
|
||||
private int _storageUsed;
|
||||
[DataField("capacity")]
|
||||
private int _storageCapacityMax = 10000;
|
||||
public readonly HashSet<IPlayerSession> SubscribedSessions = new();
|
||||
|
||||
[DataField("storageSoundCollection")]
|
||||
public string? StorageSoundCollection { get; set; }
|
||||
|
||||
[ViewVariables]
|
||||
public override IReadOnlyList<IEntity>? StoredEntities => _storage?.ContainedEntities;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool OccludesLight
|
||||
{
|
||||
get => _occludesLight;
|
||||
set
|
||||
{
|
||||
_occludesLight = value;
|
||||
if (_storage != null) _storage.OccludesLight = value;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureInitialCalculated()
|
||||
{
|
||||
if (_storageInitialCalculated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RecalculateStorageUsed();
|
||||
|
||||
_storageInitialCalculated = true;
|
||||
}
|
||||
|
||||
private void RecalculateStorageUsed()
|
||||
{
|
||||
_storageUsed = 0;
|
||||
|
||||
if (_storage == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in _storage.ContainedEntities)
|
||||
{
|
||||
var item = entity.GetComponent<SharedItemComponent>();
|
||||
_storageUsed += item.Size;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies if an entity can be stored and if it fits
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to check</param>
|
||||
/// <returns>true if it can be inserted, false otherwise</returns>
|
||||
public bool CanInsert(IEntity entity)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (entity.TryGetComponent(out ServerStorageComponent? storage) &&
|
||||
storage._storageCapacityMax >= _storageCapacityMax)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out SharedItemComponent? store) &&
|
||||
store.Size > _storageCapacityMax - _storageUsed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts into the storage container
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to insert</param>
|
||||
/// <returns>true if the entity was inserted, false otherwise</returns>
|
||||
public bool Insert(IEntity entity)
|
||||
{
|
||||
return CanInsert(entity) && _storage?.Insert(entity) == true;
|
||||
}
|
||||
|
||||
public override bool Remove(IEntity entity)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
return _storage?.Remove(entity) == true;
|
||||
}
|
||||
|
||||
public void HandleEntityMaybeInserted(EntInsertedIntoContainerMessage message)
|
||||
{
|
||||
if (message.Container != _storage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PlaySoundCollection(StorageSoundCollection);
|
||||
EnsureInitialCalculated();
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) had entity (UID {message.Entity.Uid}) inserted into it.");
|
||||
|
||||
var size = 0;
|
||||
if (message.Entity.TryGetComponent(out SharedItemComponent? storable))
|
||||
size = storable.Size;
|
||||
|
||||
_storageUsed += size;
|
||||
_sizeCache[message.Entity] = size;
|
||||
|
||||
UpdateClientInventories();
|
||||
}
|
||||
|
||||
public void HandleEntityMaybeRemoved(EntRemovedFromContainerMessage message)
|
||||
{
|
||||
if (message.Container != _storage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnsureInitialCalculated();
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) had entity (UID {message.Entity}) removed from it.");
|
||||
|
||||
if (!_sizeCache.TryGetValue(message.Entity, out var size))
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"Removed entity {message.Entity} without a cached size from storage {Owner} at {Owner.Transform.MapPosition}");
|
||||
|
||||
RecalculateStorageUsed();
|
||||
return;
|
||||
}
|
||||
|
||||
_storageUsed -= size;
|
||||
|
||||
UpdateClientInventories();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an entity into storage from the player's active hand
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity from</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertHeldEntity(IEntity player)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!player.TryGetComponent(out IHandsComponent? hands) ||
|
||||
hands.GetActiveHand == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var toInsert = hands.GetActiveHand;
|
||||
|
||||
if (!hands.Drop(toInsert.Owner))
|
||||
{
|
||||
Owner.PopupMessage(player, "Can't insert.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Insert(toInsert.Owner))
|
||||
{
|
||||
hands.PutInHand(toInsert);
|
||||
Owner.PopupMessage(player, "Can't insert.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an Entity (<paramref name="toInsert"/>) in the world into storage, informing <paramref name="player"/> if it fails.
|
||||
/// <paramref name="toInsert"/> is *NOT* held, see <see cref="PlayerInsertHeldEntity(IEntity)"/>.
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity with</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertEntityInWorld(IEntity player, IEntity toInsert)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!Insert(toInsert))
|
||||
{
|
||||
Owner.PopupMessage(player, "Can't insert.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the storage UI for an entity
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to open the UI for</param>
|
||||
public void OpenStorageUI(IEntity entity)
|
||||
{
|
||||
PlaySoundCollection(StorageSoundCollection);
|
||||
EnsureInitialCalculated();
|
||||
|
||||
var userSession = entity.GetComponent<ActorComponent>().PlayerSession;
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) \"used\" by player session (UID {userSession.AttachedEntityUid}).");
|
||||
|
||||
SubscribeSession(userSession);
|
||||
SendNetworkMessage(new OpenStorageUIMessage(), userSession.ConnectedClient);
|
||||
UpdateClientInventory(userSession);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the storage UI on all subscribed actors, informing them of the state of the container.
|
||||
/// </summary>
|
||||
private void UpdateClientInventories()
|
||||
{
|
||||
foreach (var session in SubscribedSessions)
|
||||
{
|
||||
UpdateClientInventory(session);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates storage UI on a client, informing them of the state of the container.
|
||||
/// </summary>
|
||||
/// <param name="session">The client to be updated</param>
|
||||
private void UpdateClientInventory(IPlayerSession session)
|
||||
{
|
||||
if (session.AttachedEntity == null)
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) detected no attached entity in player session (UID {session.AttachedEntityUid}).");
|
||||
|
||||
UnsubscribeSession(session);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_storage == null)
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"{nameof(UpdateClientInventory)} called with null {nameof(_storage)}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (StoredEntities == null)
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"{nameof(UpdateClientInventory)} called with null {nameof(StoredEntities)}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var stored = StoredEntities.Select(e => e.Uid).ToArray();
|
||||
|
||||
SendNetworkMessage(new StorageHeldItemsMessage(stored, _storageUsed, _storageCapacityMax), session.ConnectedClient);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a session to the update list.
|
||||
/// </summary>
|
||||
/// <param name="session">The session to add</param>
|
||||
private void SubscribeSession(IPlayerSession session)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!SubscribedSessions.Contains(session))
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) subscribed player session (UID {session.AttachedEntityUid}).");
|
||||
|
||||
session.PlayerStatusChanged += HandlePlayerSessionChangeEvent;
|
||||
SubscribedSessions.Add(session);
|
||||
|
||||
UpdateDoorState();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a session from the update list.
|
||||
/// </summary>
|
||||
/// <param name="session">The session to remove</param>
|
||||
public void UnsubscribeSession(IPlayerSession session)
|
||||
{
|
||||
if (SubscribedSessions.Contains(session))
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) unsubscribed player session (UID {session.AttachedEntityUid}).");
|
||||
|
||||
SubscribedSessions.Remove(session);
|
||||
SendNetworkMessage(new CloseStorageUIMessage(), session.ConnectedClient);
|
||||
|
||||
UpdateDoorState();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePlayerSessionChangeEvent(object? obj, SessionStatusEventArgs sessionStatus)
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) handled a status change in player session (UID {sessionStatus.Session.AttachedEntityUid}).");
|
||||
|
||||
if (sessionStatus.NewStatus != SessionStatus.InGame)
|
||||
{
|
||||
UnsubscribeSession(sessionStatus.Session);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateDoorState()
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(StorageVisuals.Open, SubscribedSessions.Count != 0);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
_storage = ContainerHelpers.EnsureContainer<Container>(Owner, "storagebase");
|
||||
_storage.OccludesLight = _occludesLight;
|
||||
}
|
||||
|
||||
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession? session = null)
|
||||
{
|
||||
base.HandleNetworkMessage(message, channel, session);
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
throw new ArgumentException(nameof(session));
|
||||
}
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case RemoveEntityMessage remove:
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
var player = session.AttachedEntity;
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var ownerTransform = Owner.Transform;
|
||||
var playerTransform = player.Transform;
|
||||
|
||||
if (!playerTransform.Coordinates.InRange(Owner.EntityManager, ownerTransform.Coordinates, 2) ||
|
||||
!ownerTransform.IsMapTransform &&
|
||||
!playerTransform.ContainsEntity(ownerTransform))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var entity = Owner.EntityManager.GetEntity(remove.EntityUid);
|
||||
|
||||
if (entity == null || _storage?.Contains(entity) == false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var item = entity.GetComponent<ItemComponent>();
|
||||
if (item == null ||
|
||||
!player.TryGetComponent(out HandsComponent? hands))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!hands.CanPutInHand(item))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
hands.PutInHand(item);
|
||||
|
||||
break;
|
||||
}
|
||||
case InsertEntityMessage _:
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
var player = session.AttachedEntity;
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!player.InRangeUnobstructed(Owner, popup: true))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
PlayerInsertHeldEntity(player);
|
||||
|
||||
break;
|
||||
}
|
||||
case CloseStorageUIMessage _:
|
||||
{
|
||||
if (session is not IPlayerSession playerSession)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
UnsubscribeSession(playerSession);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts storable entities into this storage container if possible, otherwise return to the hand of the user
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner.Uid}) attacked by user (UID {eventArgs.User.Uid}) with entity (UID {eventArgs.Using.Uid}).");
|
||||
|
||||
if (Owner.HasComponent<PlaceableSurfaceComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return PlayerInsertHeldEntity(eventArgs.User);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to open the storage UI
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
OpenStorageUI(eventArgs.User);
|
||||
return false;
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
((IUse) this).UseEntity(new UseEntityEventArgs(eventArgs.User));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows a user to pick up entities by clicking them, or pick up all entities in a certain radius
|
||||
/// arround a click.
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true)) return false;
|
||||
|
||||
// Pick up all entities in a radius around the clicked location.
|
||||
// The last half of the if is because carpets exist and this is terrible
|
||||
if(_areaInsert && (eventArgs.Target == null || !eventArgs.Target.HasComponent<SharedItemComponent>()))
|
||||
{
|
||||
var validStorables = new List<IEntity>();
|
||||
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(eventArgs.ClickLocation, 1))
|
||||
{
|
||||
if (!entity.Transform.IsMapTransform
|
||||
|| entity == eventArgs.User
|
||||
|| !entity.HasComponent<SharedItemComponent>())
|
||||
continue;
|
||||
validStorables.Add(entity);
|
||||
}
|
||||
|
||||
//If there's only one then let's be generous
|
||||
if (validStorables.Count > 1)
|
||||
{
|
||||
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
|
||||
var doAfterArgs = new DoAfterEventArgs(eventArgs.User, 0.2f * validStorables.Count, CancellationToken.None, Owner)
|
||||
{
|
||||
BreakOnStun = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnUserMove = true,
|
||||
NeedHand = true,
|
||||
};
|
||||
var result = await doAfterSystem.DoAfter(doAfterArgs);
|
||||
if (result != DoAfterStatus.Finished) return true;
|
||||
}
|
||||
|
||||
var successfullyInserted = new List<EntityUid>();
|
||||
var successfullyInsertedPositions = new List<EntityCoordinates>();
|
||||
foreach (var entity in validStorables)
|
||||
{
|
||||
// Check again, situation may have changed for some entities, but we'll still pick up any that are valid
|
||||
if (!entity.Transform.IsMapTransform
|
||||
|| entity == eventArgs.User
|
||||
|| !entity.HasComponent<SharedItemComponent>())
|
||||
continue;
|
||||
var coords = entity.Transform.Coordinates;
|
||||
if (PlayerInsertEntityInWorld(eventArgs.User, entity))
|
||||
{
|
||||
successfullyInserted.Add(entity.Uid);
|
||||
successfullyInsertedPositions.Add(coords);
|
||||
}
|
||||
}
|
||||
|
||||
// If we picked up atleast one thing, play a sound and do a cool animation!
|
||||
if (successfullyInserted.Count>0)
|
||||
{
|
||||
PlaySoundCollection(StorageSoundCollection);
|
||||
SendNetworkMessage(
|
||||
new AnimateInsertingEntitiesMessage(
|
||||
successfullyInserted,
|
||||
successfullyInsertedPositions
|
||||
)
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Pick up the clicked entity
|
||||
else if(_quickInsert)
|
||||
{
|
||||
if (eventArgs.Target == null
|
||||
|| !eventArgs.Target.Transform.IsMapTransform
|
||||
|| eventArgs.Target == eventArgs.User
|
||||
|| !eventArgs.Target.HasComponent<SharedItemComponent>())
|
||||
return false;
|
||||
var position = eventArgs.Target.Transform.Coordinates;
|
||||
if(PlayerInsertEntityInWorld(eventArgs.User, eventArgs.Target))
|
||||
{
|
||||
SendNetworkMessage(new AnimateInsertingEntitiesMessage(
|
||||
new List<EntityUid>() { eventArgs.Target.Uid },
|
||||
new List<EntityCoordinates>() { position }
|
||||
));
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
var storedEntities = StoredEntities?.ToList();
|
||||
|
||||
if (storedEntities == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in storedEntities)
|
||||
{
|
||||
Remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Severity < ExplosionSeverity.Heavy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var storedEntities = StoredEntities?.ToList();
|
||||
|
||||
if (storedEntities == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in storedEntities)
|
||||
{
|
||||
var exActs = entity.GetAllComponents<IExAct>().ToArray();
|
||||
foreach (var exAct in exActs)
|
||||
{
|
||||
exAct.OnExplosion(eventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void PlaySoundCollection(string? name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var file = AudioHelpers.GetRandomFileFromSoundCollection(name);
|
||||
SoundSystem.Play(Filter.Pvs(Owner), file, Owner, AudioParams.Default);
|
||||
}
|
||||
}
|
||||
}
|
||||
88
Content.Server/Storage/Components/StorageCounterComponent.cs
Normal file
88
Content.Server/Storage/Components/StorageCounterComponent.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Storage that spawns and counts a single item.
|
||||
/// Usually used for things like matchboxes, cigarette packs,
|
||||
/// cigar cases etc.
|
||||
/// </summary>
|
||||
/// <code>
|
||||
/// - type: StorageCounter
|
||||
/// amount: 6 # Note: this field can be omitted
|
||||
/// countTag: Cigarette # Note: field doesn't point to entity Id, but its tag
|
||||
/// </code>
|
||||
[RegisterComponent]
|
||||
public class StorageCounterComponent : Component, ISerializationHooks
|
||||
{
|
||||
// TODO Convert to EntityWhitelist
|
||||
[DataField("countTag")]
|
||||
private string? _countTag;
|
||||
|
||||
[DataField("amount")]
|
||||
private int? _maxAmount;
|
||||
|
||||
/// <summary>
|
||||
/// Single item storage component usually have an attached StackedVisualizer.
|
||||
/// </summary>
|
||||
[ComponentDependency] private readonly AppearanceComponent? _appearanceComponent = default;
|
||||
|
||||
public override string Name => "StorageCounter";
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
if (_countTag == null)
|
||||
{
|
||||
Logger.Warning("StorageCounterComponent without a `countTag` is useless");
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
|
||||
if (_appearanceComponent != null)
|
||||
{
|
||||
switch (message)
|
||||
{
|
||||
case ContainerContentsModifiedMessage msg:
|
||||
var actual = Count(msg.Container.ContainedEntities);
|
||||
_appearanceComponent.SetData(StackVisuals.Actual, actual);
|
||||
if (_maxAmount != null)
|
||||
{
|
||||
_appearanceComponent.SetData(StackVisuals.MaxCount, _maxAmount);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int Count(IReadOnlyList<IEntity> containerContainedEntities)
|
||||
{
|
||||
var count = 0;
|
||||
if (_countTag != null)
|
||||
{
|
||||
foreach (var entity in containerContainedEntities)
|
||||
{
|
||||
if (entity.HasTag(_countTag))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
106
Content.Server/Storage/Components/StorageFillComponent.cs
Normal file
106
Content.Server/Storage/Components/StorageFillComponent.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class StorageFillComponent : Component, IMapInit
|
||||
{
|
||||
public override string Name => "StorageFill";
|
||||
|
||||
[DataField("contents")]
|
||||
private List<StorageFillEntry> _contents = new();
|
||||
|
||||
public IReadOnlyList<StorageFillEntry> Contents => _contents;
|
||||
|
||||
void IMapInit.MapInit()
|
||||
{
|
||||
if (_contents.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Owner.TryGetComponent(out IStorageComponent? storage))
|
||||
{
|
||||
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({Owner})");
|
||||
return;
|
||||
}
|
||||
|
||||
var random = IoCManager.Resolve<IRobustRandom>();
|
||||
|
||||
var alreadySpawnedGroups = new List<string>();
|
||||
foreach (var storageItem in _contents)
|
||||
{
|
||||
if (string.IsNullOrEmpty(storageItem.PrototypeId)) continue;
|
||||
if (!string.IsNullOrEmpty(storageItem.GroupId) && alreadySpawnedGroups.Contains(storageItem.GroupId)) continue;
|
||||
|
||||
if (storageItem.SpawnProbability != 1f &&
|
||||
!random.Prob(storageItem.SpawnProbability))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var i = 0; i < storageItem.Amount; i++)
|
||||
{
|
||||
storage.Insert(Owner.EntityManager.SpawnEntity(storageItem.PrototypeId, Owner.Transform.Coordinates));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[DataDefinition]
|
||||
public struct StorageFillEntry : IPopulateDefaultValues
|
||||
{
|
||||
[DataField("id", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string? PrototypeId;
|
||||
|
||||
[DataField("prob")]
|
||||
public float SpawnProbability;
|
||||
/// <summary>
|
||||
/// The probability that an item will spawn. Takes decimal form so 0.05 is 5%, 0.50 is 50% etc.
|
||||
/// </summary>
|
||||
[DataField("orGroup")]
|
||||
public string GroupId;
|
||||
/// <summary>
|
||||
/// orGroup signifies to pick between entities designated with an ID.
|
||||
///
|
||||
/// <example>
|
||||
/// <para>To define an orGroup in a StorageFill component you
|
||||
/// need to add it to the entities you want to choose between and
|
||||
/// add a prob field. In this example there is a 50% chance the storage
|
||||
/// spawns with Y or Z.
|
||||
///
|
||||
/// </para>
|
||||
/// <code>
|
||||
/// - type: StorageFill
|
||||
/// contents:
|
||||
/// - name: X
|
||||
/// - name: Y
|
||||
/// prob: 0.50
|
||||
/// orGroup: YOrZ
|
||||
/// - name: Z
|
||||
/// orGroup: YOrZ
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
[DataField("amount")]
|
||||
public int Amount;
|
||||
|
||||
public void PopulateDefaultValues()
|
||||
{
|
||||
Amount = 1;
|
||||
SpawnProbability = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
Content.Server/Storage/StorageSystem.cs
Normal file
94
Content.Server/Storage/StorageSystem.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Interaction;
|
||||
using Content.Server.Storage.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Storage
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class StorageSystem : EntitySystem
|
||||
{
|
||||
private readonly List<IPlayerSession> _sessionCache = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
||||
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
UnsubscribeLocalEvent<EntRemovedFromContainerMessage>();
|
||||
UnsubscribeLocalEvent<EntInsertedIntoContainerMessage>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var component in ComponentManager.EntityQuery<ServerStorageComponent>(true))
|
||||
{
|
||||
CheckSubscribedEntities(component);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
|
||||
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
||||
{
|
||||
storageComp.HandleEntityMaybeRemoved(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
|
||||
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
||||
{
|
||||
storageComp.HandleEntityMaybeInserted(message);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckSubscribedEntities(ServerStorageComponent storageComp)
|
||||
{
|
||||
|
||||
// We have to cache the set of sessions because Unsubscribe modifies the original.
|
||||
_sessionCache.Clear();
|
||||
_sessionCache.AddRange(storageComp.SubscribedSessions);
|
||||
|
||||
if (_sessionCache.Count == 0)
|
||||
return;
|
||||
|
||||
var storagePos = storageComp.Owner.Transform.WorldPosition;
|
||||
var storageMap = storageComp.Owner.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;
|
||||
|
||||
if (storageMap != attachedEntity.Transform.MapID)
|
||||
continue;
|
||||
|
||||
var distanceSquared = (storagePos - attachedEntity.Transform.WorldPosition).LengthSquared;
|
||||
if (distanceSquared > InteractionSystem.InteractionRangeSquared)
|
||||
{
|
||||
storageComp.UnsubscribeSession(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user