From dfa4277e472f62adf94d054472c58b29ee50d9c4 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sat, 29 Jan 2022 22:45:40 +1100 Subject: [PATCH] ECS StorageFill (#6351) --- .../Components/StorageFillComponent.cs | 67 ++----------------- .../EntitySystems/StorageSystem.Fill.cs | 46 +++++++++++++ .../Storage/EntitySystems/StorageSystem.cs | 6 +- 3 files changed, 56 insertions(+), 63 deletions(-) create mode 100644 Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs diff --git a/Content.Server/Storage/Components/StorageFillComponent.cs b/Content.Server/Storage/Components/StorageFillComponent.cs index 84c1823ca3..c78f9cceb5 100644 --- a/Content.Server/Storage/Components/StorageFillComponent.cs +++ b/Content.Server/Storage/Components/StorageFillComponent.cs @@ -1,71 +1,14 @@ -using System; using System.Collections.Generic; -using Content.Shared.Storage; +using Content.Server.Storage.EntitySystems; +using Robust.Shared.Analyzers; 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 + [RegisterComponent, ComponentProtoName("StorageFill"), Friend(typeof(StorageSystem))] + public sealed class StorageFillComponent : Component { - [Dependency] private readonly IEntityManager _entMan = default!; - - public override string Name => "StorageFill"; - - [DataField("contents")] private List _contents = new(); - - public IReadOnlyList Contents => _contents; - - void IMapInit.MapInit() - { - if (_contents.Count == 0) - { - return; - } - - if (!_entMan.TryGetComponent(Owner, out IStorageComponent? storage)) - { - Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({Owner})"); - return; - } - - var random = IoCManager.Resolve(); - - var alreadySpawnedGroups = new List(); - foreach (var storageItem in _contents) - { - if (!string.IsNullOrEmpty(storageItem.GroupId) && - alreadySpawnedGroups.Contains(storageItem.GroupId)) continue; - - if (storageItem.SpawnProbability != 1f && - !random.Prob(storageItem.SpawnProbability)) - { - continue; - } - - var entMan = _entMan; - var transform = entMan.GetComponent(Owner); - - for (var i = 0; i < storageItem.Amount; i++) - { - - var ent = entMan.SpawnEntity(storageItem.PrototypeId, transform.Coordinates); - - if (storage.Insert(ent)) continue; - - Logger.ErrorS("storage", $"Tried to StorageFill {storageItem.PrototypeId} inside {Owner} but can't."); - entMan.DeleteEntity(ent); - } - - if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId); - } - } + [DataField("contents")] public List Contents = new(); } } diff --git a/Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs b/Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs new file mode 100644 index 0000000000..b02aeb98b2 --- /dev/null +++ b/Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using Content.Server.Storage.Components; +using Robust.Shared.GameObjects; +using Robust.Shared.Log; +using Robust.Shared.Random; + +namespace Content.Server.Storage.EntitySystems; + +public sealed partial class StorageSystem +{ + private void OnStorageFillMapInit(EntityUid uid, StorageFillComponent component, MapInitEvent args) + { + if (component.Contents.Count == 0) return; + + if (!TryComp(uid, out var storage)) + { + Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({uid})"); + return; + } + + var coordinates = Transform(uid).Coordinates; + var alreadySpawnedGroups = new HashSet(); + + foreach (var entry in component.Contents) + { + // Handle "Or" groups + if (!string.IsNullOrEmpty(entry.GroupId) && alreadySpawnedGroups.Contains(entry.GroupId)) continue; + + // Check random spawn + // ReSharper disable once CompareOfFloatsByEqualityOperator + if (entry.SpawnProbability != 1f && !_random.Prob(entry.SpawnProbability)) continue; + + for (var i = 0; i < entry.Amount; i++) + { + var ent = EntityManager.SpawnEntity(entry.PrototypeId, coordinates); + + if (storage.Insert(ent)) continue; + + Logger.ErrorS("storage", $"Tried to StorageFill {entry.PrototypeId} inside {uid} but can't."); + EntityManager.DeleteEntity(ent); + } + + if (!string.IsNullOrEmpty(entry.GroupId)) alreadySpawnedGroups.Add(entry.GroupId); + } + } +} diff --git a/Content.Server/Storage/EntitySystems/StorageSystem.cs b/Content.Server/Storage/EntitySystems/StorageSystem.cs index 706431c98a..1f52ff11a1 100644 --- a/Content.Server/Storage/EntitySystems/StorageSystem.cs +++ b/Content.Server/Storage/EntitySystems/StorageSystem.cs @@ -11,14 +11,16 @@ using Robust.Shared.Containers; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Localization; +using Robust.Shared.Random; using Robust.Shared.Timing; namespace Content.Server.Storage.EntitySystems { [UsedImplicitly] - internal sealed class StorageSystem : EntitySystem + public sealed partial class StorageSystem : EntitySystem { [Dependency] private readonly IGameTiming _gameTiming = default!; + [Dependency] private readonly IRobustRandom _random = default!; private readonly List _sessionCache = new(); @@ -33,6 +35,8 @@ namespace Content.Server.Storage.EntitySystems SubscribeLocalEvent(AddToggleOpenVerb); SubscribeLocalEvent(AddOpenUiVerb); SubscribeLocalEvent(OnRelayMovement); + + SubscribeLocalEvent(OnStorageFillMapInit); } private void OnRelayMovement(EntityUid uid, EntityStorageComponent component, RelayMovementEntityEvent args)