From dfc5bcdc1284bb969f60d0ad7b16523e6270dd00 Mon Sep 17 00:00:00 2001 From: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Date: Sat, 29 Apr 2023 00:53:41 -0400 Subject: [PATCH] Convert materials to use PhysicalComposition (#15414) --- .../Materials/MaterialStorageSystem.cs | 9 +++- .../Tests/MaterialArbitrageTest.cs | 22 ++++++---- Content.Server/Cargo/Systems/PricingSystem.cs | 16 ++++--- .../Materials/MaterialStorageSystem.cs | 17 +++++--- Content.Shared/Materials/MaterialComponent.cs | 21 ++++----- .../Materials/SharedMaterialStorageSystem.cs | 43 ++++++++++--------- .../Objects/Materials/Sheets/glass.yml | 12 ++++-- .../Objects/Materials/Sheets/metal.yml | 6 ++- .../Objects/Materials/Sheets/other.yml | 9 ++-- .../Entities/Objects/Materials/ingots.yml | 6 ++- .../Entities/Objects/Materials/materials.yml | 15 ++++--- .../Entities/Objects/Materials/ore.yml | 18 +++++--- .../Entities/Objects/Misc/space_cash.yml | 3 +- 13 files changed, 117 insertions(+), 80 deletions(-) diff --git a/Content.Client/Materials/MaterialStorageSystem.cs b/Content.Client/Materials/MaterialStorageSystem.cs index b4ca1f2736..edd07391f7 100644 --- a/Content.Client/Materials/MaterialStorageSystem.cs +++ b/Content.Client/Materials/MaterialStorageSystem.cs @@ -40,9 +40,14 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem } } - public override bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null) + public override bool TryInsertMaterialEntity(EntityUid user, + EntityUid toInsert, + EntityUid receiver, + MaterialStorageComponent? storage = null, + MaterialComponent? material = null, + PhysicalCompositionComponent? composition = null) { - if (!base.TryInsertMaterialEntity(user, toInsert, receiver, component)) + if (!base.TryInsertMaterialEntity(user, toInsert, receiver, storage, material, composition)) return false; _transform.DetachParentToNull(toInsert, Transform(toInsert)); return true; diff --git a/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs b/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs index 406b5aad81..e0894debd6 100644 --- a/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs +++ b/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs @@ -107,11 +107,12 @@ public sealed class MaterialArbitrageTest var stackProto = protoManager.Index(materialStep.MaterialPrototypeId); var spawnProto = protoManager.Index(stackProto.Spawn); - if (!spawnProto.Components.TryGetValue(materialName, out var matreg)) + if (!spawnProto.Components.ContainsKey(materialName) || + !spawnProto.Components.TryGetValue(compositionName, out var compositionReg)) continue; - var mat = (MaterialComponent) matreg.Component; - foreach (var (matId, amount) in mat.Materials) + var mat = (PhysicalCompositionComponent) compositionReg.Component; + foreach (var (matId, amount) in mat.MaterialComposition) { materials[matId] = materialStep.Amount * amount + materials.GetValueOrDefault(matId); } @@ -156,11 +157,13 @@ public sealed class MaterialArbitrageTest var spawnProto = protoManager.Index(key); // get the amount of each material included in the entity - if (!spawnProto.Components.TryGetValue(materialName, out var matreg)) + + if (!spawnProto.Components.ContainsKey(materialName) || + !spawnProto.Components.TryGetValue(compositionName, out var compositionReg)) continue; - var mat = (MaterialComponent) matreg.Component; - foreach (var (matId, amount) in mat.Materials) + var mat = (PhysicalCompositionComponent) compositionReg.Component; + foreach (var (matId, amount) in mat.MaterialComposition) { spawnedMats[matId] = value.Max * amount + spawnedMats.GetValueOrDefault(matId); } @@ -235,11 +238,12 @@ public sealed class MaterialArbitrageTest var spawnProto = protoManager.Index(spawnCompletion.Prototype); - if (!spawnProto.Components.TryGetValue(materialName, out var matreg)) + if (!spawnProto.Components.ContainsKey(materialName) || + !spawnProto.Components.TryGetValue(compositionName, out var compositionReg)) continue; - var mat = (MaterialComponent) matreg.Component; - foreach (var (matId, amount) in mat.Materials) + var mat = (PhysicalCompositionComponent) compositionReg.Component; + foreach (var (matId, amount) in mat.MaterialComposition) { materials[matId] = spawnCompletion.Amount * amount + materials.GetValueOrDefault(matId); } diff --git a/Content.Server/Cargo/Systems/PricingSystem.cs b/Content.Server/Cargo/Systems/PricingSystem.cs index 3d4bd64a4c..d4493fa8ca 100644 --- a/Content.Server/Cargo/Systems/PricingSystem.cs +++ b/Content.Server/Cargo/Systems/PricingSystem.cs @@ -121,10 +121,10 @@ public sealed class PricingSystem : EntitySystem return price; } - private double GetMaterialPrice(MaterialComponent component) + private double GetMaterialPrice(PhysicalCompositionComponent component) { double price = 0; - foreach (var (id, quantity) in component.Materials) + foreach (var (id, quantity) in component.MaterialComposition) { price += _prototypeManager.Index(id).Price * quantity; } @@ -213,9 +213,10 @@ public sealed class PricingSystem : EntitySystem { double price = 0; - if (TryComp(uid, out var material)) + if (HasComp(uid) && + TryComp(uid, out var composition)) { - var matPrice = GetMaterialPrice(material); + var matPrice = GetMaterialPrice(composition); if (TryComp(uid, out var stack)) matPrice *= stack.Count; @@ -229,10 +230,11 @@ public sealed class PricingSystem : EntitySystem { double price = 0; - if (prototype.Components.TryGetValue(_factory.GetComponentName(typeof(MaterialComponent)), out var materials)) + if (prototype.Components.ContainsKey(_factory.GetComponentName(typeof(MaterialComponent))) && + prototype.Components.TryGetValue(_factory.GetComponentName(typeof(PhysicalCompositionComponent)), out var composition)) { - var materialsComp = (MaterialComponent) materials.Component; - var matPrice = GetMaterialPrice(materialsComp); + var compositionComp = (PhysicalCompositionComponent) composition.Component; + var matPrice = GetMaterialPrice(compositionComp); if (prototype.Components.TryGetValue(_factory.GetComponentName(typeof(StackComponent)), out var stackProto)) { diff --git a/Content.Server/Materials/MaterialStorageSystem.cs b/Content.Server/Materials/MaterialStorageSystem.cs index e79a47d067..63eeaf2fe5 100644 --- a/Content.Server/Materials/MaterialStorageSystem.cs +++ b/Content.Server/Materials/MaterialStorageSystem.cs @@ -40,15 +40,20 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem } } - public override bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null) + public override bool TryInsertMaterialEntity(EntityUid user, + EntityUid toInsert, + EntityUid receiver, + MaterialStorageComponent? storage = null, + MaterialComponent? material = null, + PhysicalCompositionComponent? composition = null) { - if (!Resolve(receiver, ref component)) + if (!Resolve(receiver, ref storage) || !Resolve(toInsert, ref material, ref composition, false)) return false; if (TryComp(receiver, out var power) && !power.Powered) return false; - if (!base.TryInsertMaterialEntity(user, toInsert, receiver, component)) + if (!base.TryInsertMaterialEntity(user, toInsert, receiver, storage, material, composition)) return false; - _audio.PlayPvs(component.InsertingSound, receiver); + _audio.PlayPvs(storage.InsertingSound, receiver); _popup.PopupEntity(Loc.GetString("machine-insert-item", ("user", user), ("machine", receiver), ("item", toInsert)), receiver); QueueDel(toInsert); @@ -116,10 +121,10 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem return new List(); var entProto = _prototypeManager.Index(materialProto.StackEntity); - if (!entProto.TryGetComponent(out var material)) + if (!entProto.TryGetComponent(out var composition)) return new List(); - var materialPerStack = material.Materials[materialProto.ID]; + var materialPerStack = composition.MaterialComposition[materialProto.ID]; var amountToSpawn = amount / materialPerStack; overflowMaterial = amount - amountToSpawn * materialPerStack; return _stackSystem.SpawnMultiple(materialProto.StackEntity, amountToSpawn, coordinates); diff --git a/Content.Shared/Materials/MaterialComponent.cs b/Content.Shared/Materials/MaterialComponent.cs index 25d4e68a23..3a5bba3e1f 100644 --- a/Content.Shared/Materials/MaterialComponent.cs +++ b/Content.Shared/Materials/MaterialComponent.cs @@ -1,16 +1,13 @@ using Robust.Shared.GameStates; -using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary; -namespace Content.Shared.Materials +namespace Content.Shared.Materials; +/// +/// Empty component that marks an entity as a "raw" material. +/// The material amounts themselves are in +/// +[RegisterComponent, NetworkedComponent] +public sealed class MaterialComponent : Component { - /// - /// Component to store data such as "this object is made out of steel". - /// This is not a storage system for say smelteries. - /// - [RegisterComponent, NetworkedComponent] - public sealed class MaterialComponent : Component - { - [DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer))] - public readonly Dictionary Materials = new(); - } + } + diff --git a/Content.Shared/Materials/SharedMaterialStorageSystem.cs b/Content.Shared/Materials/SharedMaterialStorageSystem.cs index 7daea6977b..d2a7af52c6 100644 --- a/Content.Shared/Materials/SharedMaterialStorageSystem.cs +++ b/Content.Shared/Materials/SharedMaterialStorageSystem.cs @@ -35,13 +35,14 @@ public abstract class SharedMaterialStorageSystem : EntitySystem public override void Update(float frameTime) { base.Update(frameTime); - foreach (var inserting in EntityQuery()) + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var inserting)) { if (_timing.CurTime < inserting.EndTime) continue; - _appearance.SetData(inserting.Owner, MaterialStorageVisuals.Inserting, false); - RemComp(inserting.Owner, inserting); + _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false); + RemComp(uid, inserting); } } @@ -184,53 +185,53 @@ public abstract class SharedMaterialStorageSystem : EntitySystem /// /// Tries to insert an entity into the material storage. /// - /// - /// - /// - /// - /// If it was successful - public virtual bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null) + public virtual bool TryInsertMaterialEntity(EntityUid user, + EntityUid toInsert, + EntityUid receiver, + MaterialStorageComponent? storage = null, + MaterialComponent? material = null, + PhysicalCompositionComponent? composition = null) { - if (!Resolve(receiver, ref component)) + if (!Resolve(receiver, ref storage)) return false; - if (!TryComp(toInsert, out var material)) + if (!Resolve(toInsert, ref material, ref composition, false)) return false; - if (component.EntityWhitelist?.IsValid(toInsert) == false) + if (storage.EntityWhitelist?.IsValid(toInsert) == false) 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 material.Materials) + foreach (var (mat, vol) in composition.MaterialComposition) { - if (!CanChangeMaterialAmount(receiver, mat, vol * multiplier, component)) + if (!CanChangeMaterialAmount(receiver, mat, vol * multiplier, storage)) return false; totalVolume += vol * multiplier; } - if (!CanTakeVolume(receiver, totalVolume, component)) + if (!CanTakeVolume(receiver, totalVolume, storage)) return false; - foreach (var (mat, vol) in material.Materials) + foreach (var (mat, vol) in composition.MaterialComposition) { - TryChangeMaterialAmount(receiver, mat, vol * multiplier, component); + TryChangeMaterialAmount(receiver, mat, vol * multiplier, storage); } var insertingComp = EnsureComp(receiver); - insertingComp.EndTime = _timing.CurTime + component.InsertionTime; - if (!component.IgnoreColor) + insertingComp.EndTime = _timing.CurTime + storage.InsertionTime; + if (!storage.IgnoreColor) { - _prototype.TryIndex(material.Materials.Keys.Last(), out var lastMat); + _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(component.Owner, ref ev); + RaiseLocalEvent(receiver, ref ev); return true; } diff --git a/Resources/Prototypes/Entities/Objects/Materials/Sheets/glass.yml b/Resources/Prototypes/Entities/Objects/Materials/Sheets/glass.yml index e49d6a3207..145df2231a 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/Sheets/glass.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/Sheets/glass.yml @@ -49,7 +49,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Glass: 100 - type: Stack stackType: Glass @@ -91,7 +92,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: ReinforcedGlass: 100 - type: Stack stackType: ReinforcedGlass @@ -133,7 +135,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: PlasmaGlass: 100 - type: Stack stackType: PlasmaGlass @@ -172,7 +175,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: ReinforcedPlasmaGlass: 100 - type: Stack stackType: ReinforcedPlasmaGlass diff --git a/Resources/Prototypes/Entities/Objects/Materials/Sheets/metal.yml b/Resources/Prototypes/Entities/Objects/Materials/Sheets/metal.yml index fd99c1b728..cf26b9523b 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/Sheets/metal.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/Sheets/metal.yml @@ -36,7 +36,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Steel: 100 - type: Stack stackType: Steel @@ -74,7 +75,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Plasteel: 100 - type: Stack stackType: Plasteel diff --git a/Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml b/Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml index 3430a3cfc4..0eed0ac32f 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml @@ -63,7 +63,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Plasma: 100 - type: Stack stackType: Plasma @@ -113,7 +114,8 @@ - Sheet - DroneUsable - type: Material - materials: + - type: PhysicalComposition + materialComposition: Plastic: 100 - type: Stack stackType: Plastic @@ -147,7 +149,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Uranium: 100 - type: Stack stackType: Uranium diff --git a/Resources/Prototypes/Entities/Objects/Materials/ingots.yml b/Resources/Prototypes/Entities/Objects/Materials/ingots.yml index 13092cdb5e..1066fcb024 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/ingots.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/ingots.yml @@ -34,7 +34,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Gold: 100 - type: Stack stackType: Gold @@ -68,7 +69,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Silver: 100 - type: Stack stackType: Silver diff --git a/Resources/Prototypes/Entities/Objects/Materials/materials.yml b/Resources/Prototypes/Entities/Objects/Materials/materials.yml index 141afd2df1..0fd9e479b6 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/materials.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/materials.yml @@ -32,7 +32,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Cardboard: 100 - type: Stack stackType: Cardboard @@ -76,7 +77,8 @@ - type: Stack stackType: Cloth - type: Material - materials: + - type: PhysicalComposition + materialComposition: Cloth: 100 - type: Extractable juiceSolution: @@ -112,7 +114,8 @@ - type: Stack stackType: Durathread - type: Material - materials: + - type: PhysicalComposition + materialComposition: Durathread: 100 - type: Sprite state: durathread_3 @@ -144,7 +147,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Wood: 100 - type: Stack stackType: WoodPlank @@ -175,7 +179,8 @@ suffix: Full components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Biomass: 1 - type: Stack stackType: Biomass diff --git a/Resources/Prototypes/Entities/Objects/Materials/ore.yml b/Resources/Prototypes/Entities/Objects/Materials/ore.yml index dc5ecf4cfc..76287c044e 100644 --- a/Resources/Prototypes/Entities/Objects/Materials/ore.yml +++ b/Resources/Prototypes/Entities/Objects/Materials/ore.yml @@ -46,7 +46,8 @@ - type: Sprite state: gold - type: Material - materials: + - type: PhysicalComposition + materialComposition: Gold: 500 - type: entity @@ -68,7 +69,8 @@ - type: Sprite state: iron - type: Material - materials: + - type: PhysicalComposition + materialComposition: Steel: 500 - type: entity @@ -90,7 +92,8 @@ - type: Sprite state: plasma - type: Material - materials: + - type: PhysicalComposition + materialComposition: Plasma: 500 - type: entity @@ -112,7 +115,8 @@ - type: Sprite state: silver - type: Material - materials: + - type: PhysicalComposition + materialComposition: Silver: 500 - type: entity @@ -134,7 +138,8 @@ - type: Sprite state: spacequartz - type: Material - materials: + - type: PhysicalComposition + materialComposition: Glass: 500 - type: entity @@ -156,7 +161,8 @@ - type: Sprite state: uranium - type: Material - materials: + - type: PhysicalComposition + materialComposition: Uranium: 500 - type: entity diff --git a/Resources/Prototypes/Entities/Objects/Misc/space_cash.yml b/Resources/Prototypes/Entities/Objects/Misc/space_cash.yml index 4b786e2dc8..d179ef77c4 100644 --- a/Resources/Prototypes/Entities/Objects/Misc/space_cash.yml +++ b/Resources/Prototypes/Entities/Objects/Misc/space_cash.yml @@ -5,7 +5,8 @@ description: You gotta have money. components: - type: Material - materials: + - type: PhysicalComposition + materialComposition: Credit: 1 - type: StaticPrice price: 0 -- 2.51.2