Lathe Refactor and ECS (#11201)
* lathe and material storage refactor * materialStorage ECS it kinda sus tho * beginning the lathe shitcode dive * couple lathe visuals and lathe system * lathe changes and such * dynamic lathe databases * rewrote internal logic on to ui * da newI * material display clientside * misc ui changes * component state handling and various other things * moar * Update Content.Shared/Lathe/LatheComponent.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * first volley of sloth review * more fixes * losin' my mind * all da changes * test fix and other review Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
42
Content.Shared/Materials/MaterialComponent.cs
Normal file
42
Content.Shared/Materials/MaterialComponent.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Linq;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
|
||||
|
||||
namespace Content.Shared.Materials
|
||||
{
|
||||
/// <summary>
|
||||
/// Component to store data such as "this object is made out of steel".
|
||||
/// This is not a storage system for say smelteries.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class MaterialComponent : Component
|
||||
{
|
||||
[ViewVariables]
|
||||
[DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<int, MaterialPrototype>))]
|
||||
// ReSharper disable once CollectionNeverUpdated.Local
|
||||
public readonly Dictionary<string, int> _materials = new();
|
||||
public List<string> MaterialIds => _materials.Keys.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all materials which make up this entity.
|
||||
/// This property has an IoC resolve and is generally slow, so be sure to cache the results if needed.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public IEnumerable<MaterialPrototype> Materials
|
||||
{
|
||||
get
|
||||
{
|
||||
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
foreach (var id in MaterialIds)
|
||||
{
|
||||
if(prototypeManager.TryIndex<MaterialPrototype>(id, out var material))
|
||||
yield return material;
|
||||
else
|
||||
Logger.Error($"Material prototype {id} does not exist! Entity: {Owner}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
88
Content.Shared/Materials/MaterialStorageComponent.cs
Normal file
88
Content.Shared/Materials/MaterialStorageComponent.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
||||
|
||||
namespace Content.Shared.Materials;
|
||||
|
||||
[Access(typeof(SharedMaterialStorageSystem))]
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class MaterialStorageComponent : Component
|
||||
{
|
||||
[ViewVariables]
|
||||
public Dictionary<string, int> Storage { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// How much material the storage can store in total.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("storageLimit")]
|
||||
public int? StorageLimit;
|
||||
|
||||
/// <summary>
|
||||
/// Whitelist for specifying the kind of items that can be insert into this entity.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("whitelist")]
|
||||
public EntityWhitelist? EntityWhitelist;
|
||||
|
||||
/// <summary>
|
||||
/// Whitelist generated on runtime for what specific materials can be inserted into this entity.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("materialWhiteList", customTypeSerializer: typeof(PrototypeIdListSerializer<MaterialPrototype>))]
|
||||
public List<string>? MaterialWhiteList;
|
||||
|
||||
/// <summary>
|
||||
/// The sound that plays when inserting an item into the storage
|
||||
/// </summary>
|
||||
[DataField("insertingSound")]
|
||||
public SoundSpecifier? InsertingSound;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// event raised on the materialStorage when a material entity is inserted into it.
|
||||
/// </summary>
|
||||
public readonly struct MaterialEntityInsertedEvent
|
||||
{
|
||||
public readonly Dictionary<string, int> Materials;
|
||||
|
||||
public MaterialEntityInsertedEvent(Dictionary<string, int> materials)
|
||||
{
|
||||
Materials = materials;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a material amount is changed
|
||||
/// </summary>
|
||||
public readonly struct MaterialAmountChangedEvent
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public sealed class GetMaterialWhitelistEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid Storage;
|
||||
|
||||
public List<string> Whitelist = new();
|
||||
|
||||
public GetMaterialWhitelistEvent(EntityUid storage)
|
||||
{
|
||||
Storage = storage;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class MaterialStorageComponentState : ComponentState
|
||||
{
|
||||
public Dictionary<string, int> Storage;
|
||||
|
||||
public List<string>? MaterialWhitelist;
|
||||
|
||||
public MaterialStorageComponentState(Dictionary<string, int> storage, List<string>? materialWhitelist)
|
||||
{
|
||||
Storage = storage;
|
||||
MaterialWhitelist = materialWhitelist;
|
||||
}
|
||||
}
|
||||
217
Content.Shared/Materials/SharedMaterialStorageSystem.cs
Normal file
217
Content.Shared/Materials/SharedMaterialStorageSystem.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Stacks;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Materials;
|
||||
|
||||
/// <summary>
|
||||
/// This handles storing materials and modifying their amounts
|
||||
/// <see cref="MaterialStorageComponent"/>
|
||||
/// </summary>
|
||||
public abstract class SharedMaterialStorageSystem : EntitySystem
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<MaterialStorageComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<MaterialStorageComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<MaterialStorageComponent, ComponentHandleState>(OnHandleState);
|
||||
}
|
||||
|
||||
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<string, int>(state.Storage);
|
||||
|
||||
if (state.MaterialWhitelist != null)
|
||||
component.MaterialWhiteList = new List<string>(state.MaterialWhitelist);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the volume of a specified material contained in this storage.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="material"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>The volume of the material</returns>
|
||||
[PublicAPI]
|
||||
public int GetMaterialAmount(EntityUid uid, MaterialPrototype material, MaterialStorageComponent? component = null)
|
||||
{
|
||||
return GetMaterialAmount(uid, material.ID, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the volume of a specified material contained in this storage.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="material"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>The volume of the material</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total volume of all materials in the storage.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>The volume of all materials in the storage</returns>
|
||||
public int GetTotalMaterialAmount(EntityUid uid, MaterialStorageComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return 0;
|
||||
return component.Storage.Values.Sum();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if a specific amount of volume will fit in the storage.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="volume"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>If the specified volume will fit</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the specified material can be changed by the specified volume.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="materialId"></param>
|
||||
/// <param name="volume"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>If the amount can be changed</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the amount of a specific material in the storage.
|
||||
/// Still respects the filters in place.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="materialId"></param>
|
||||
/// <param name="volume"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>If it was successful</returns>
|
||||
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;
|
||||
|
||||
RaiseLocalEvent(uid, new MaterialAmountChangedEvent());
|
||||
Dirty(component);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to insert an entity into the material storage.
|
||||
/// </summary>
|
||||
/// <param name="toInsert"></param>
|
||||
/// <param name="receiver"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>If it was successful</returns>
|
||||
public bool TryInsertMaterialEntity(EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null)
|
||||
{
|
||||
if (!Resolve(receiver, ref component))
|
||||
return false;
|
||||
|
||||
if (!TryComp<MaterialComponent>(toInsert, out var material))
|
||||
return false;
|
||||
|
||||
if (component.EntityWhitelist?.IsValid(toInsert) == false)
|
||||
return false;
|
||||
|
||||
if (component.MaterialWhiteList != null)
|
||||
{
|
||||
var matUsed = false;
|
||||
foreach (var mat in material.Materials)
|
||||
{
|
||||
if (component.MaterialWhiteList.Contains(mat.ID))
|
||||
matUsed = true;
|
||||
}
|
||||
|
||||
if (!matUsed)
|
||||
return false;
|
||||
}
|
||||
|
||||
var multiplier = TryComp<SharedStackComponent>(toInsert, out var stackComponent) ? stackComponent.Count : 1;
|
||||
|
||||
var totalVolume = 0;
|
||||
foreach (var (mat, vol) in component.Storage)
|
||||
{
|
||||
if (!CanChangeMaterialAmount(receiver, mat, vol, component))
|
||||
return false;
|
||||
totalVolume += vol * multiplier;
|
||||
}
|
||||
|
||||
if (!CanTakeVolume(receiver, totalVolume, component))
|
||||
return false;
|
||||
|
||||
foreach (var (mat, vol) in material._materials)
|
||||
{
|
||||
TryChangeMaterialAmount(receiver, mat, vol * multiplier, component);
|
||||
}
|
||||
|
||||
OnFinishInsertMaterialEntity(toInsert, component);
|
||||
RaiseLocalEvent(component.Owner, new MaterialEntityInsertedEvent(material._materials));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcasts an event that will collect a list of which materials
|
||||
/// are allowed to be inserted into the materialStorage.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component"></param>
|
||||
public void UpdateMaterialWhitelist(EntityUid uid, MaterialStorageComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component, false))
|
||||
return;
|
||||
var ev = new GetMaterialWhitelistEvent(uid);
|
||||
RaiseLocalEvent(uid, ev);
|
||||
component.MaterialWhiteList = ev.Whitelist;
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// This is done because of popup spam and not being able
|
||||
/// to do entity deletion clientside.
|
||||
/// </remarks>
|
||||
protected abstract void OnFinishInsertMaterialEntity(EntityUid toInsert, MaterialStorageComponent component);
|
||||
|
||||
private void OnInteractUsing(EntityUid uid, MaterialStorageComponent component, InteractUsingEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
args.Handled = TryInsertMaterialEntity(args.Used, uid, component);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user