using System.Linq;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Components;
using Content.Shared.Stacks;
using Content.Shared.Whitelist;
using JetBrains.Annotations;
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!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly SharedMaterialSiloSystem _materialSilo = default!;
///
/// Default volume for a sheet if the material's entity prototype has no material composition.
///
private const int DefaultSheetVolume = 100;
///
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnMapInit);
SubscribeLocalEvent(OnInteractUsing);
}
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);
}
///
/// 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, MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(uid, ref component))
return 0; //you have nothing
if (Resolve(uid, ref utilizer, false) && utilizer.Silo.HasValue)
return _materialSilo.GetSiloMaterialAmount(uid, material, utilizer);
return component.Storage.GetValueOrDefault(material, 0);
}
///
/// 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, MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(uid, ref component))
return 0;
if (Resolve(uid, ref utilizer, false) && utilizer.Silo.HasValue)
return _materialSilo.GetSiloTotalMaterialAmount(uid, utilizer);
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, MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(uid, ref component))
return false;
var storageLimit = component.StorageLimit;
if (Resolve(uid, ref utilizer, false) && utilizer.Silo.HasValue)
storageLimit = _materialSilo.GetSiloStorage(uid, utilizer)?.Comp.StorageLimit;
return storageLimit == null || GetTotalMaterialAmount(uid, component) + volume <= 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, MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(uid, ref component)
|| !CanTakeVolume(uid, volume, component, utilizer)
|| (!component.MaterialWhiteList?.Contains(materialId) ?? false))
return false;
var amount = GetMaterialAmount(uid, materialId, component, utilizer);
return amount + volume >= 0;
}
///
/// Checks if the specified materials can be changed by the specified volumes.
///
///
///
///
/// If the amount can be changed
public bool CanChangeMaterialAmount(Entity entity, Dictionary materials, MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(entity, ref entity.Comp))
return false;
foreach (var (material, amount) in materials)
{
if (!CanChangeMaterialAmount(entity, material, amount, entity.Comp, utilizer))
return false;
}
return true;
}
///
/// 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, MaterialSiloUtilizerComponent? utilizer = null, bool dirty = true)
{
if (!Resolve(uid, ref component))
return false;
var storage = component;
var storageUid = uid;
if (Resolve(uid, ref utilizer, false) && utilizer.Silo.HasValue)
{
var silo = _materialSilo.GetSiloStorage(uid, utilizer);
if (silo.HasValue)
{
storage = silo.Value.Comp;
storageUid = silo.Value;
}
}
if (!CanChangeMaterialAmount(uid, materialId, volume, component, utilizer))
return false;
storage.Storage.TryAdd(materialId, 0);
storage.Storage[materialId] += volume;
var ev = new MaterialAmountChangedEvent();
RaiseLocalEvent(storageUid, ref ev);
if (dirty)
Dirty(storageUid, storage);
return true;
}
///
/// Changes the amount of a specific material in the storage.
/// Still respects the filters in place.
///
///
///
/// If the amount can be changed
public bool TryChangeMaterialAmount(Entity entity, Dictionary materials)
{
if (!Resolve(entity, ref entity.Comp))
return false;
var storage = entity.Comp;
var storageUid = entity;
if (TryComp(entity, out var utilizer) && utilizer.Silo.HasValue)
{
var silo = _materialSilo.GetSiloStorage(entity, utilizer);
if (silo.HasValue)
{
storage = silo.Value.Comp;
storageUid = silo.Value.Owner;
}
}
if (!CanChangeMaterialAmount(entity, materials, utilizer))
return false;
foreach (var (material, amount) in materials)
{
if (!TryChangeMaterialAmount(entity, material, amount, entity.Comp, utilizer, false))
return false;
}
Dirty(storageUid, storage);
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.
/// The material silo utilizer component on .
/// True if it was successful (enough space etc).
public bool TrySetMaterialAmount(
EntityUid uid,
string materialId,
int volume,
MaterialStorageComponent? component = null,
MaterialSiloUtilizerComponent? utilizer = null)
{
if (!Resolve(uid, ref component))
return false;
var curAmount = GetMaterialAmount(uid, materialId, component, utilizer);
var delta = volume - curAmount;
return TryChangeMaterialAmount(uid, materialId, delta, component, utilizer);
}
public virtual bool CanInsertMaterialEntity(
EntityUid toInsert,
EntityUid receiver,
MaterialStorageComponent? storage = null,
MaterialSiloUtilizerComponent? utilizer = null,
MaterialComponent? material = null,
PhysicalCompositionComponent? composition = null
)
{
if (!Resolve(receiver, ref storage)
|| !Resolve(toInsert, ref material, ref composition, false)
|| _whitelistSystem.IsWhitelistFail(storage.Whitelist, toInsert)
|| 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, utilizer))
return false;
totalVolume += vol * multiplier;
}
return CanTakeVolume(receiver, totalVolume, storage, utilizer);
}
///
/// Tries to insert an entity into the material storage.
///
public virtual bool TryInsertMaterialEntity(EntityUid user,
EntityUid toInsert,
EntityUid receiver,
MaterialStorageComponent? storage = null,
MaterialSiloUtilizerComponent? utilizer = 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 (_whitelistSystem.IsWhitelistFail(storage.Whitelist, toInsert))
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, utilizer))
return false;
totalVolume += vol * multiplier;
}
if (!CanTakeVolume(receiver, totalVolume, storage, utilizer))
return false;
foreach (var (mat, vol) in composition.MaterialComposition)
{
TryChangeMaterialAmount(receiver, mat, vol * multiplier, storage, utilizer);
}
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(receiver, insertingComp);
var ev = new MaterialEntityInsertedEvent(user, toInsert, material, multiplier); // Lavaland Change
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(uid, 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);
}
public int GetSheetVolume(MaterialPrototype material)
{
if (material.StackEntity == null)
return DefaultSheetVolume;
var proto = _prototype.Index(material.StackEntity);
if (!proto.TryGetComponent(out var composition))
return DefaultSheetVolume;
return composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == material.ID).Value;
}
}