]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Convert materials to use PhysicalComposition (#15414)
authorNemanja <98561806+EmoGarbage404@users.noreply.github.com>
Sat, 29 Apr 2023 04:53:41 +0000 (00:53 -0400)
committerGitHub <noreply@github.com>
Sat, 29 Apr 2023 04:53:41 +0000 (14:53 +1000)
13 files changed:
Content.Client/Materials/MaterialStorageSystem.cs
Content.IntegrationTests/Tests/MaterialArbitrageTest.cs
Content.Server/Cargo/Systems/PricingSystem.cs
Content.Server/Materials/MaterialStorageSystem.cs
Content.Shared/Materials/MaterialComponent.cs
Content.Shared/Materials/SharedMaterialStorageSystem.cs
Resources/Prototypes/Entities/Objects/Materials/Sheets/glass.yml
Resources/Prototypes/Entities/Objects/Materials/Sheets/metal.yml
Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml
Resources/Prototypes/Entities/Objects/Materials/ingots.yml
Resources/Prototypes/Entities/Objects/Materials/materials.yml
Resources/Prototypes/Entities/Objects/Materials/ore.yml
Resources/Prototypes/Entities/Objects/Misc/space_cash.yml

index b4ca1f273654e2cb9854063edc9234bad0ebaac4..edd07391f7bcc86929ef172a56e9233a3e61e047 100644 (file)
@@ -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;
index 406b5aad812046c7459011c72406a19503972ebd..e0894debd6d830345c928d994d9abaec60e85d43 100644 (file)
@@ -107,11 +107,12 @@ public sealed class MaterialArbitrageTest
                     var stackProto = protoManager.Index<StackPrototype>(materialStep.MaterialPrototypeId);
                     var spawnProto = protoManager.Index<EntityPrototype>(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<EntityPrototype>(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<EntityPrototype>(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);
                     }
index 3d4bd64a4cafd81a403c996786a097d040632a30..d4493fa8ca0a1f4b6b7dc61dad9cdcf932aa8584 100644 (file)
@@ -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<MaterialPrototype>(id).Price * quantity;
         }
@@ -213,9 +213,10 @@ public sealed class PricingSystem : EntitySystem
     {
         double price = 0;
 
-        if (TryComp<MaterialComponent>(uid, out var material))
+        if (HasComp<MaterialComponent>(uid) &&
+            TryComp<PhysicalCompositionComponent>(uid, out var composition))
         {
-            var matPrice = GetMaterialPrice(material);
+            var matPrice = GetMaterialPrice(composition);
             if (TryComp<StackComponent>(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))
             {
index e79a47d0673c916d323a495d484f0db0518fff44..63eeaf2fe5e2adb8bb6fffc024b94dc97fc039fa 100644 (file)
@@ -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<ApcPowerReceiverComponent>(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<EntityUid>();
 
         var entProto = _prototypeManager.Index<EntityPrototype>(materialProto.StackEntity);
-        if (!entProto.TryGetComponent<MaterialComponent>(out var material))
+        if (!entProto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
             return new List<EntityUid>();
 
-        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);
index 25d4e68a2347eb6b1d4025ad4b0c852f90237389..3a5bba3e1ff31599a52b002526b06270861be9e2 100644 (file)
@@ -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;
+/// <summary>
+/// Empty component that marks an entity as a "raw" material.
+/// The material amounts themselves are in <see cref="PhysicalCompositionComponent"/>
+/// </summary>
+[RegisterComponent, NetworkedComponent]
+public sealed class MaterialComponent : Component
 {
-    /// <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
-    {
-        [DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<int, MaterialPrototype>))]
-        public readonly Dictionary<string, int> Materials = new();
-    }
+
 }
+
index 7daea6977b0683cccf0914157d139e0db43090e7..d2a7af52c6763da1f7762bdd5a45e1b0a44acd35 100644 (file)
@@ -35,13 +35,14 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
     public override void Update(float frameTime)
     {
         base.Update(frameTime);
-        foreach (var inserting in EntityQuery<InsertingMaterialStorageComponent>())
+        var query = EntityQueryEnumerator<InsertingMaterialStorageComponent>();
+        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
     /// <summary>
     /// Tries to insert an entity into the material storage.
     /// </summary>
-    /// <param name="user"></param>
-    /// <param name="toInsert"></param>
-    /// <param name="receiver"></param>
-    /// <param name="component"></param>
-    /// <returns>If it was successful</returns>
-    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<MaterialComponent>(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<StackComponent>(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<InsertingMaterialStorageComponent>(receiver);
-        insertingComp.EndTime = _timing.CurTime + component.InsertionTime;
-        if (!component.IgnoreColor)
+        insertingComp.EndTime = _timing.CurTime + storage.InsertionTime;
+        if (!storage.IgnoreColor)
         {
-            _prototype.TryIndex<MaterialPrototype>(material.Materials.Keys.Last(), out var lastMat);
+            _prototype.TryIndex<MaterialPrototype>(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;
     }
 
index e49d6a3207deac079fe2ab268bc37e21feb2a669..145df2231a9ae50e09b3e8840c394eb09e155556 100644 (file)
@@ -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
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       PlasmaGlass: 100
   - type: Stack
     stackType: PlasmaGlass
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       ReinforcedPlasmaGlass: 100
   - type: Stack
     stackType: ReinforcedPlasmaGlass
index fd99c1b7280b8f41c52460f8b500d0b52dc388ab..cf26b9523bc551d0f003c10ef33be154928d9a35 100644 (file)
@@ -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
index 3430a3cfc43996f6158e52f3d693d7d70d7a1939..0eed0ac32f94f37fd3688c7a7ea45cc0ce421821 100644 (file)
@@ -63,7 +63,8 @@
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Plasma: 100
   - type: Stack
     stackType: Plasma
     - Sheet
     - DroneUsable
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Plastic: 100
   - type: Stack
     stackType: Plastic
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Uranium: 100
   - type: Stack
     stackType: Uranium
index 13092cdb5eecdb77a4128aa255bd3d7810760ffb..1066fcb024597358e1fe820408c9642661b4a193 100644 (file)
@@ -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
index 141afd2df18ba9eee595c96b9e6be6378275025b..0fd9e479b66b71faa4fde9372a902c9f3fd5f1bf 100644 (file)
@@ -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:
   - type: Stack
     stackType: Durathread
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Durathread: 100
   - type: Sprite
     state: durathread_3
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Wood: 100
   - type: Stack
     stackType: WoodPlank
   suffix: Full
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Biomass: 1
   - type: Stack
     stackType: Biomass
index dc5ecf4cfcbb5e64bfebcfa4bc2e2608ff2aee63..76287c044e8ea45f427902207312c5817928db61 100644 (file)
@@ -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
   - type: Sprite
     state: silver
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Silver: 500
 
 - type: entity
   - type: Sprite
     state: spacequartz
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Glass: 500
 
 - type: entity
   - type: Sprite
     state: uranium
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Uranium: 500
 
 - type: entity
index 4b786e2dc8f556a51d84c2dfba1cbc182198eabe..d179ef77c4b21725c56d9ed925682b6d4a365691 100644 (file)
@@ -5,7 +5,8 @@
   description: You gotta have money.
   components:
   - type: Material
-    materials:
+  - type: PhysicalComposition
+    materialComposition:
       Credit: 1
   - type: StaticPrice
     price: 0