Implement SmartFridge functionality (#38648)
* Add SmartFridge * my nit so pick * my access so expanded and my whitelist so both * list -> hashset
This commit is contained in:
committed by
GitHub
parent
99b431cafd
commit
d2ddbcbcda
@@ -19,6 +19,7 @@ using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Power;
|
||||
using Content.Shared.Power.EntitySystems;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Content.Shared.Throwing;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Whitelist;
|
||||
@@ -90,6 +91,9 @@ public abstract class SharedDisposalUnitSystem : EntitySystem
|
||||
SubscribeLocalEvent<DisposalUnitComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
|
||||
SubscribeLocalEvent<DisposalUnitComponent, DragDropTargetEvent>(OnDragDropOn);
|
||||
SubscribeLocalEvent<DisposalUnitComponent, ContainerRelayMovementEntityEvent>(OnMovement);
|
||||
|
||||
SubscribeLocalEvent<DisposalUnitComponent, GetDumpableVerbEvent>(OnGetDumpableVerb);
|
||||
SubscribeLocalEvent<DisposalUnitComponent, DumpEvent>(OnDump);
|
||||
}
|
||||
|
||||
private void AddDisposalAltVerbs(Entity<DisposalUnitComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
|
||||
@@ -785,4 +789,23 @@ public abstract class SharedDisposalUnitSystem : EntitySystem
|
||||
// See also, medical scanner. Also maybe add verbs for entering lockers/body bags?
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
private void OnGetDumpableVerb(Entity<DisposalUnitComponent> ent, ref GetDumpableVerbEvent args)
|
||||
{
|
||||
args.Verb = Loc.GetString("dump-disposal-verb-name", ("unit", ent));
|
||||
}
|
||||
|
||||
private void OnDump(Entity<DisposalUnitComponent> ent, ref DumpEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = true;
|
||||
args.PlaySound = true;
|
||||
|
||||
foreach (var entity in args.DumpQueue)
|
||||
{
|
||||
DoInsertDisposalUnit(ent, entity, args.User);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Storage;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Shared.Placeable;
|
||||
|
||||
public sealed class PlaceableSurfaceSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
|
||||
|
||||
@@ -19,6 +21,8 @@ public sealed class PlaceableSurfaceSystem : EntitySystem
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageInteractUsingAttemptEvent>(OnStorageInteractUsingAttempt);
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterOpenEvent>(OnStorageAfterOpen);
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterCloseEvent>(OnStorageAfterClose);
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, GetDumpableVerbEvent>(OnGetDumpableVerb);
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, DumpEvent>(OnDump);
|
||||
}
|
||||
|
||||
public void SetPlaceable(EntityUid uid, bool isPlaceable, PlaceableSurfaceComponent? surface = null)
|
||||
@@ -87,4 +91,25 @@ public sealed class PlaceableSurfaceSystem : EntitySystem
|
||||
{
|
||||
SetPlaceable(ent.Owner, false, ent.Comp);
|
||||
}
|
||||
|
||||
private void OnGetDumpableVerb(Entity<PlaceableSurfaceComponent> ent, ref GetDumpableVerbEvent args)
|
||||
{
|
||||
args.Verb = Loc.GetString("dump-placeable-verb-name", ("surface", ent));
|
||||
}
|
||||
|
||||
private void OnDump(Entity<PlaceableSurfaceComponent> ent, ref DumpEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = true;
|
||||
args.PlaySound = true;
|
||||
|
||||
var (targetPos, targetRot) = _transformSystem.GetWorldPositionRotation(ent);
|
||||
|
||||
foreach (var entity in args.DumpQueue)
|
||||
{
|
||||
_transformSystem.SetWorldPositionRotation(entity, targetPos + _random.NextVector2Box() / 4, targetRot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
99
Content.Shared/SmartFridge/SmartFridgeComponent.cs
Normal file
99
Content.Shared/SmartFridge/SmartFridgeComponent.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.SmartFridge;
|
||||
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
|
||||
[Access(typeof(SmartFridgeSystem))]
|
||||
public sealed partial class SmartFridgeComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The container ID that this SmartFridge stores its inventory in
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public string Container = "smart_fridge_inventory";
|
||||
|
||||
/// <summary>
|
||||
/// Whitelist for what entities can be inserted
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public EntityWhitelist? Whitelist;
|
||||
|
||||
/// <summary>
|
||||
/// Blacklist for what entities can be inserted
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public EntityWhitelist? Blacklist;
|
||||
|
||||
/// <summary>
|
||||
/// The sound played on inserting an item into the fridge
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public SoundSpecifier? InsertSound = new SoundCollectionSpecifier("MachineInsert");
|
||||
|
||||
/// <summary>
|
||||
/// A list of entries to display in the UI
|
||||
/// </summary>
|
||||
[DataField, AutoNetworkedField]
|
||||
public List<SmartFridgeEntry> Entries = new();
|
||||
|
||||
/// <summary>
|
||||
/// A mapping of smart fridge entries to the actual contained contents
|
||||
/// </summary>
|
||||
[DataField, AutoNetworkedField]
|
||||
[Access(typeof(SmartFridgeSystem), Other = AccessPermissions.ReadExecute)]
|
||||
public Dictionary<SmartFridgeEntry, HashSet<NetEntity>> ContainedEntries = new();
|
||||
|
||||
/// <summary>
|
||||
/// The flavour text displayed at the bottom of the SmartFridge's UI
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public LocId FlavorText = "smart-fridge-request-generic";
|
||||
|
||||
/// <summary>
|
||||
/// Sound that plays when ejecting an item
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public SoundSpecifier SoundVend = new SoundCollectionSpecifier("VendingDispense")
|
||||
{
|
||||
Params = new AudioParams
|
||||
{
|
||||
Volume = -4f,
|
||||
Variation = 0.15f
|
||||
}
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Sound that plays when an item can't be ejected
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public SoundSpecifier SoundDeny = new SoundCollectionSpecifier("VendingDeny");
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable, DataRecord]
|
||||
public record struct SmartFridgeEntry
|
||||
{
|
||||
public string Name;
|
||||
|
||||
public SmartFridgeEntry(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum SmartFridgeUiKey : byte
|
||||
{
|
||||
Key,
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class SmartFridgeDispenseItemMessage(SmartFridgeEntry entry) : BoundUserInterfaceMessage
|
||||
{
|
||||
public SmartFridgeEntry Entry = entry;
|
||||
}
|
||||
157
Content.Shared/SmartFridge/SmartFridgeSystem.cs
Normal file
157
Content.Shared/SmartFridge/SmartFridgeSystem.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Content.Shared.Access.Components;
|
||||
using Content.Shared.Access.Systems;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.SmartFridge;
|
||||
|
||||
public sealed class SmartFridgeSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
|
||||
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _hands = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<SmartFridgeComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<SmartFridgeComponent, EntRemovedFromContainerMessage>(OnItemRemoved);
|
||||
|
||||
SubscribeLocalEvent<SmartFridgeComponent, GetDumpableVerbEvent>(OnGetDumpableVerb);
|
||||
SubscribeLocalEvent<SmartFridgeComponent, DumpEvent>(OnDump);
|
||||
|
||||
Subs.BuiEvents<SmartFridgeComponent>(SmartFridgeUiKey.Key,
|
||||
sub =>
|
||||
{
|
||||
sub.Event<SmartFridgeDispenseItemMessage>(OnDispenseItem);
|
||||
});
|
||||
}
|
||||
|
||||
private bool DoInsert(Entity<SmartFridgeComponent> ent, EntityUid user, IEnumerable<EntityUid> usedItems, bool playSound)
|
||||
{
|
||||
if (!_container.TryGetContainer(ent, ent.Comp.Container, out var container))
|
||||
return false;
|
||||
|
||||
if (!Allowed(ent, user))
|
||||
return true;
|
||||
|
||||
bool anyInserted = false;
|
||||
foreach (var used in usedItems)
|
||||
{
|
||||
if (!_whitelist.CheckBoth(used, ent.Comp.Blacklist, ent.Comp.Whitelist))
|
||||
continue;
|
||||
anyInserted = true;
|
||||
|
||||
_container.Insert(used, container);
|
||||
var key = new SmartFridgeEntry(Identity.Name(used, EntityManager));
|
||||
if (!ent.Comp.Entries.Contains(key))
|
||||
ent.Comp.Entries.Add(key);
|
||||
|
||||
ent.Comp.ContainedEntries.TryAdd(key, new());
|
||||
var entries = ent.Comp.ContainedEntries[key];
|
||||
if (!entries.Contains(GetNetEntity(used)))
|
||||
entries.Add(GetNetEntity(used));
|
||||
|
||||
Dirty(ent);
|
||||
}
|
||||
|
||||
if (anyInserted && playSound)
|
||||
{
|
||||
_audio.PlayPredicted(ent.Comp.InsertSound, ent, user);
|
||||
}
|
||||
|
||||
return anyInserted;
|
||||
}
|
||||
|
||||
private void OnInteractUsing(Entity<SmartFridgeComponent> ent, ref InteractUsingEvent args)
|
||||
{
|
||||
if (!_hands.CanDrop(args.User, args.Used))
|
||||
return;
|
||||
|
||||
args.Handled = DoInsert(ent, args.User, [args.Used], true);
|
||||
}
|
||||
|
||||
private void OnItemRemoved(Entity<SmartFridgeComponent> ent, ref EntRemovedFromContainerMessage args)
|
||||
{
|
||||
var key = new SmartFridgeEntry(Identity.Name(args.Entity, EntityManager));
|
||||
|
||||
if (ent.Comp.ContainedEntries.TryGetValue(key, out var contained))
|
||||
{
|
||||
contained.Remove(GetNetEntity(args.Entity));
|
||||
}
|
||||
|
||||
Dirty(ent);
|
||||
}
|
||||
|
||||
private bool Allowed(Entity<SmartFridgeComponent> machine, EntityUid user)
|
||||
{
|
||||
if (_accessReader.IsAllowed(user, machine))
|
||||
return true;
|
||||
|
||||
_popup.PopupPredicted(Loc.GetString("smart-fridge-component-try-eject-access-denied"), machine, user);
|
||||
_audio.PlayPredicted(machine.Comp.SoundDeny, machine, user);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void OnDispenseItem(Entity<SmartFridgeComponent> ent, ref SmartFridgeDispenseItemMessage args)
|
||||
{
|
||||
if (!_timing.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
if (!Allowed(ent, args.Actor))
|
||||
return;
|
||||
|
||||
if (!ent.Comp.ContainedEntries.TryGetValue(args.Entry, out var contained))
|
||||
{
|
||||
_audio.PlayPredicted(ent.Comp.SoundDeny, ent, args.Actor);
|
||||
_popup.PopupPredicted(Loc.GetString("smart-fridge-component-try-eject-unknown-entry"), ent, args.Actor);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var item in contained)
|
||||
{
|
||||
if (!_container.TryRemoveFromContainer(GetEntity(item)))
|
||||
continue;
|
||||
|
||||
_audio.PlayPredicted(ent.Comp.SoundVend, ent, args.Actor);
|
||||
contained.Remove(item);
|
||||
Dirty(ent);
|
||||
return;
|
||||
}
|
||||
|
||||
_audio.PlayPredicted(ent.Comp.SoundDeny, ent, args.Actor);
|
||||
_popup.PopupPredicted(Loc.GetString("smart-fridge-component-try-eject-out-of-stock"), ent, args.Actor);
|
||||
}
|
||||
|
||||
private void OnGetDumpableVerb(Entity<SmartFridgeComponent> ent, ref GetDumpableVerbEvent args)
|
||||
{
|
||||
if (_accessReader.IsAllowed(args.User, ent))
|
||||
{
|
||||
args.Verb = Loc.GetString("dump-smartfridge-verb-name", ("unit", ent));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDump(Entity<SmartFridgeComponent> ent, ref DumpEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = true;
|
||||
args.PlaySound = true;
|
||||
|
||||
DoInsert(ent, args.User, args.DumpQueue, false);
|
||||
}
|
||||
}
|
||||
@@ -33,3 +33,15 @@ public sealed partial class DumpableComponent : Component
|
||||
[DataField("multiplier"), AutoNetworkedField]
|
||||
public float Multiplier = 1.0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on Dumpable entities to get the verb for dumping
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct GetDumpableVerbEvent(EntityUid User, string? Verb);
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on Dumpable entities to complete the dump
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct DumpEvent(Queue<EntityUid> DumpQueue, EntityUid User, bool PlaySound, bool Handled);
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Disposal;
|
||||
using Content.Shared.Disposal.Components;
|
||||
using Content.Shared.Disposal.Unit;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Placeable;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
@@ -21,7 +17,6 @@ public sealed class DumpableSystem : EntitySystem
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedDisposalUnitSystem _disposalUnitSystem = default!;
|
||||
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
|
||||
|
||||
@@ -39,10 +34,12 @@ public sealed class DumpableSystem : EntitySystem
|
||||
|
||||
private void OnAfterInteract(EntityUid uid, DumpableComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (!args.CanReach || args.Handled)
|
||||
if (!args.CanReach || args.Handled || args.Target is not { } target)
|
||||
return;
|
||||
|
||||
if (!HasComp<DisposalUnitComponent>(args.Target) && !HasComp<PlaceableSurfaceComponent>(args.Target))
|
||||
var evt = new GetDumpableVerbEvent(args.User, null);
|
||||
RaiseLocalEvent(target, ref evt);
|
||||
if (evt.Verb is null)
|
||||
return;
|
||||
|
||||
if (!TryComp<StorageComponent>(uid, out var storage))
|
||||
@@ -51,7 +48,7 @@ public sealed class DumpableSystem : EntitySystem
|
||||
if (!storage.Container.ContainedEntities.Any())
|
||||
return;
|
||||
|
||||
StartDoAfter(uid, args.Target.Value, args.User, component);
|
||||
StartDoAfter(uid, target, args.User, component);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
@@ -83,33 +80,22 @@ public sealed class DumpableSystem : EntitySystem
|
||||
if (!TryComp<StorageComponent>(uid, out var storage) || !storage.Container.ContainedEntities.Any())
|
||||
return;
|
||||
|
||||
if (HasComp<DisposalUnitComponent>(args.Target))
|
||||
{
|
||||
UtilityVerb verb = new()
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
StartDoAfter(uid, args.Target, args.User, dumpable);
|
||||
},
|
||||
Text = Loc.GetString("dump-disposal-verb-name", ("unit", args.Target)),
|
||||
IconEntity = GetNetEntity(uid)
|
||||
};
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
var evt = new GetDumpableVerbEvent(args.User, null);
|
||||
RaiseLocalEvent(args.Target, ref evt);
|
||||
|
||||
if (HasComp<PlaceableSurfaceComponent>(args.Target))
|
||||
if (evt.Verb is not { } verbText)
|
||||
return;
|
||||
|
||||
UtilityVerb verb = new()
|
||||
{
|
||||
UtilityVerb verb = new()
|
||||
Act = () =>
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
StartDoAfter(uid, args.Target, args.User, dumpable);
|
||||
},
|
||||
Text = Loc.GetString("dump-placeable-verb-name", ("surface", args.Target)),
|
||||
IconEntity = GetNetEntity(uid)
|
||||
};
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
StartDoAfter(uid, args.Target, args.User, dumpable);
|
||||
},
|
||||
Text = verbText,
|
||||
IconEntity = GetNetEntity(uid)
|
||||
};
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
private void StartDoAfter(EntityUid storageUid, EntityUid targetUid, EntityUid userUid, DumpableComponent dumpable)
|
||||
@@ -141,34 +127,15 @@ public sealed class DumpableSystem : EntitySystem
|
||||
|
||||
private void OnDoAfter(EntityUid uid, DumpableComponent component, DumpableDoAfterEvent args)
|
||||
{
|
||||
if (args.Handled || args.Cancelled || !TryComp<StorageComponent>(uid, out var storage) || storage.Container.ContainedEntities.Count == 0)
|
||||
if (args.Handled || args.Cancelled || !TryComp<StorageComponent>(uid, out var storage) || storage.Container.ContainedEntities.Count == 0 || args.Args.Target is not { } target)
|
||||
return;
|
||||
|
||||
var dumpQueue = new Queue<EntityUid>(storage.Container.ContainedEntities);
|
||||
|
||||
var dumped = false;
|
||||
var evt = new DumpEvent(dumpQueue, args.Args.User, false, false);
|
||||
RaiseLocalEvent(target, ref evt);
|
||||
|
||||
if (HasComp<DisposalUnitComponent>(args.Args.Target))
|
||||
{
|
||||
dumped = true;
|
||||
|
||||
foreach (var entity in dumpQueue)
|
||||
{
|
||||
_disposalUnitSystem.DoInsertDisposalUnit(args.Args.Target.Value, entity, args.Args.User);
|
||||
}
|
||||
}
|
||||
else if (HasComp<PlaceableSurfaceComponent>(args.Args.Target))
|
||||
{
|
||||
dumped = true;
|
||||
|
||||
var (targetPos, targetRot) = _transformSystem.GetWorldPositionRotation(args.Args.Target.Value);
|
||||
|
||||
foreach (var entity in dumpQueue)
|
||||
{
|
||||
_transformSystem.SetWorldPositionRotation(entity, targetPos + _random.NextVector2Box() / 4, targetRot);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!evt.Handled)
|
||||
{
|
||||
var targetPos = _transformSystem.GetWorldPosition(uid);
|
||||
|
||||
@@ -177,9 +144,11 @@ public sealed class DumpableSystem : EntitySystem
|
||||
var transform = Transform(entity);
|
||||
_transformSystem.SetWorldPositionRotation(entity, targetPos + _random.NextVector2Box() / 4, _random.NextAngle(), transform);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (dumped)
|
||||
if (evt.PlaySound)
|
||||
{
|
||||
_audio.PlayPredicted(component.DumpSound, uid, args.User);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user