using System.Linq; using Content.Shared.Interaction; using Content.Shared.Interaction.Components; using Content.Shared.Stacks; using JetBrains.Annotations; using Robust.Shared.GameStates; using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Shared.Materials; /// /// This handles storing materials and modifying their amounts /// /// public abstract class SharedMaterialStorageSystem : EntitySystem { [Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IPrototypeManager _prototype = default!; /// public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnInteractUsing); SubscribeLocalEvent(OnGetState); SubscribeLocalEvent(OnHandleState); SubscribeLocalEvent(OnGetInsertingState); SubscribeLocalEvent(OnHandleInsertingState); SubscribeLocalEvent(OnUnpaused); } public override void Update(float frameTime) { base.Update(frameTime); var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var inserting)) { if (_timing.CurTime < inserting.EndTime) continue; _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false); RemComp(uid, inserting); } } private void OnMapInit(EntityUid uid, MaterialStorageComponent component, MapInitEvent args) { _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false); } private void OnGetState(EntityUid uid, MaterialStorageComponent component, ref ComponentGetState args) { args.State = new MaterialStorageComponentState(component.Storage, component.MaterialWhiteList); } private void OnHandleState(EntityUid uid, MaterialStorageComponent component, ref ComponentHandleState args) { if (args.Current is not MaterialStorageComponentState state) return; component.Storage = new Dictionary(state.Storage); if (state.MaterialWhitelist != null) component.MaterialWhiteList = new List(state.MaterialWhitelist); } private void OnGetInsertingState(EntityUid uid, InsertingMaterialStorageComponent component, ref ComponentGetState args) { args.State = new InsertingMaterialStorageComponentState(component.EndTime, component.MaterialColor); } private void OnHandleInsertingState(EntityUid uid, InsertingMaterialStorageComponent component, ref ComponentHandleState args) { if (args.Current is not InsertingMaterialStorageComponentState state) return; component.EndTime = state.EndTime; component.MaterialColor = state.MaterialColor; } private void OnUnpaused(EntityUid uid, InsertingMaterialStorageComponent component, ref EntityUnpausedEvent args) { component.EndTime += args.PausedTime; } /// /// Gets the volume of a specified material contained in this storage. /// /// /// /// /// The volume of the material [PublicAPI] public int GetMaterialAmount(EntityUid uid, MaterialPrototype material, MaterialStorageComponent? component = null) { return GetMaterialAmount(uid, material.ID, component); } /// /// Gets the volume of a specified material contained in this storage. /// /// /// /// /// The volume of the material public int GetMaterialAmount(EntityUid uid, string material, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return 0; //you have nothing return !component.Storage.TryGetValue(material, out var amount) ? 0 : amount; } /// /// Gets the total volume of all materials in the storage. /// /// /// /// The volume of all materials in the storage public int GetTotalMaterialAmount(EntityUid uid, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return 0; return component.Storage.Values.Sum(); } /// /// Tests if a specific amount of volume will fit in the storage. /// /// /// /// /// If the specified volume will fit public bool CanTakeVolume(EntityUid uid, int volume, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return false; return component.StorageLimit == null || GetTotalMaterialAmount(uid, component) + volume <= component.StorageLimit; } /// /// Checks if the specified material can be changed by the specified volume. /// /// /// /// /// /// If the amount can be changed public bool CanChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return false; return CanTakeVolume(uid, volume, component) && (component.MaterialWhiteList == null || component.MaterialWhiteList.Contains(materialId)) && (!component.Storage.TryGetValue(materialId, out var amount) || amount + volume >= 0); } /// /// Changes the amount of a specific material in the storage. /// Still respects the filters in place. /// /// /// /// /// /// If it was successful public bool TryChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return false; if (!CanChangeMaterialAmount(uid, materialId, volume, component)) return false; if (!component.Storage.ContainsKey(materialId)) component.Storage.Add(materialId, 0); component.Storage[materialId] += volume; var ev = new MaterialAmountChangedEvent(); RaiseLocalEvent(uid, ref ev); Dirty(component); return true; } /// /// Tries to set the amount of material in the storage to a specific value. /// Still respects the filters in place. /// /// The entity to change the material storage on. /// The ID of the material to change. /// The stored material volume to set the storage to. /// The storage component on . Resolved automatically if not given. /// True if it was successful (enough space etc). public bool TrySetMaterialAmount( EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component)) return false; var curAmount = GetMaterialAmount(uid, materialId, component); var delta = volume - curAmount; return TryChangeMaterialAmount(uid, materialId, delta, component); } /// /// Tries to insert an entity into the material storage. /// public virtual bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? storage = null, MaterialComponent? material = null, PhysicalCompositionComponent? composition = null) { if (!Resolve(receiver, ref storage)) return false; if (!Resolve(toInsert, ref material, ref composition, false)) return false; if (storage.EntityWhitelist?.IsValid(toInsert) == false) return false; if (HasComp(toInsert)) return false; // Material Whitelist checked implicitly by CanChangeMaterialAmount(); var multiplier = TryComp(toInsert, out var stackComponent) ? stackComponent.Count : 1; var totalVolume = 0; foreach (var (mat, vol) in composition.MaterialComposition) { if (!CanChangeMaterialAmount(receiver, mat, vol * multiplier, storage)) return false; totalVolume += vol * multiplier; } if (!CanTakeVolume(receiver, totalVolume, storage)) return false; foreach (var (mat, vol) in composition.MaterialComposition) { TryChangeMaterialAmount(receiver, mat, vol * multiplier, storage); } var insertingComp = EnsureComp(receiver); insertingComp.EndTime = _timing.CurTime + storage.InsertionTime; if (!storage.IgnoreColor) { _prototype.TryIndex(composition.MaterialComposition.Keys.First(), out var lastMat); insertingComp.MaterialColor = lastMat?.Color; } _appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true); Dirty(insertingComp); var ev = new MaterialEntityInsertedEvent(material); RaiseLocalEvent(receiver, ref ev); return true; } /// /// Broadcasts an event that will collect a list of which materials /// are allowed to be inserted into the materialStorage. /// /// /// public void UpdateMaterialWhitelist(EntityUid uid, MaterialStorageComponent? component = null) { if (!Resolve(uid, ref component, false)) return; var ev = new GetMaterialWhitelistEvent(uid); RaiseLocalEvent(uid, ref ev); component.MaterialWhiteList = ev.Whitelist; Dirty(component); } private void OnInteractUsing(EntityUid uid, MaterialStorageComponent component, InteractUsingEvent args) { if (args.Handled || !component.InsertOnInteract) return; args.Handled = TryInsertMaterialEntity(args.User, args.Used, uid, component); } }