MaterialComponent cleanup (#13326)

This commit is contained in:
Leon Friedrich
2023-01-08 11:36:32 +13:00
committed by GitHub
parent 89301629f5
commit 1f5bae751f
12 changed files with 70 additions and 75 deletions

View File

@@ -38,19 +38,29 @@ namespace Content.IntegrationTests.Tests.Materials
var allMaterialProtos = prototypeManager.EnumeratePrototypes<MaterialPrototype>(); var allMaterialProtos = prototypeManager.EnumeratePrototypes<MaterialPrototype>();
var coords = testMap.GridCoords; var coords = testMap.GridCoords;
foreach (var proto in allMaterialProtos) Assert.Multiple(() =>
{ {
if (proto.StackEntity == "") foreach (var proto in allMaterialProtos)
continue; {
if (proto.StackEntity == "")
continue;
var spawned = entityManager.SpawnEntity(proto.StackEntity, coords); var spawned = entityManager.SpawnEntity(proto.StackEntity, coords);
Assert.That(entityManager.HasComponent<StackComponent>(spawned), Assert.That(entityManager.TryGetComponent<StackComponent>(spawned, out var stack),
$"{proto.ID} 'stack entity' {proto.StackEntity} has the stack component"); $"{proto.ID} 'stack entity' {proto.StackEntity} does not have the stack component");
Assert.That(entityManager.HasComponent<MaterialComponent>(spawned), Assert.That(entityManager.HasComponent<MaterialComponent>(spawned),
$"{proto.ID} 'material stack' {proto.StackEntity} has the material component"); $"{proto.ID} 'material stack' {proto.StackEntity} does not have the material component");
}
StackPrototype? stackProto = null;
Assert.That(stack?.StackTypeId != null && prototypeManager.TryIndex(stack.StackTypeId, out stackProto),
$"{proto.ID} material has no stack prototype");
if (stackProto != null)
Assert.That(proto.StackEntity, Is.EqualTo(stackProto.Spawn));
}
});
mapManager.DeleteMap(testMap.MapId); mapManager.DeleteMap(testMap.MapId);
}); });

View File

@@ -163,6 +163,16 @@ public sealed class PricingSystem : EntitySystem
return price; return price;
} }
public double GetMaterialPrice(MaterialComponent component)
{
double price = 0;
foreach (var (id, quantity) in component.Materials)
{
price += _prototypeManager.Index<MaterialPrototype>(id).Price * quantity;
}
return price;
}
/// <summary> /// <summary>
/// Appraises an entity, returning it's price. /// Appraises an entity, returning it's price.
/// </summary> /// </summary>
@@ -181,10 +191,11 @@ public sealed class PricingSystem : EntitySystem
if (TryComp<MaterialComponent>(uid, out var material) && !HasComp<StackPriceComponent>(uid)) if (TryComp<MaterialComponent>(uid, out var material) && !HasComp<StackPriceComponent>(uid))
{ {
var matPrice = GetMaterialPrice(material);
if (TryComp<StackComponent>(uid, out var stack)) if (TryComp<StackComponent>(uid, out var stack))
ev.Price += stack.Count * material.Materials.Sum(x => x.Price * material._materials[x.ID]); matPrice *= stack.Count;
else
ev.Price += material.Materials.Sum(x => x.Price); ev.Price += matPrice;
} }
if (TryComp<ContainerManagerComponent>(uid, out var containers)) if (TryComp<ContainerManagerComponent>(uid, out var containers))

View File

@@ -217,7 +217,7 @@ namespace Content.Server.Lathe
private void OnMaterialEntityInserted(EntityUid uid, LatheComponent component, MaterialEntityInsertedEvent args) private void OnMaterialEntityInserted(EntityUid uid, LatheComponent component, MaterialEntityInsertedEvent args)
{ {
var lastMat = args.Materials.Keys.Last(); var lastMat = args.MaterialComp.Materials.Keys.Last();
// We need the prototype to get the color // We need the prototype to get the color
_proto.TryIndex(lastMat, out MaterialPrototype? matProto); _proto.TryIndex(lastMat, out MaterialPrototype? matProto);
EnsureComp<LatheInsertingComponent>(uid).TimeRemaining = component.InsertionTime; EnsureComp<LatheInsertingComponent>(uid).TimeRemaining = component.InsertionTime;

View File

@@ -27,6 +27,9 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
private void OnDeconstructed(EntityUid uid, MaterialStorageComponent component, MachineDeconstructedEvent args) private void OnDeconstructed(EntityUid uid, MaterialStorageComponent component, MachineDeconstructedEvent args)
{ {
if (!component.DropOnDeconstruct)
return;
foreach (var (material, amount) in component.Storage) foreach (var (material, amount) in component.Storage)
{ {
_stackSystem.SpawnMultipleFromMaterial(amount, material, Transform(uid).Coordinates); _stackSystem.SpawnMultipleFromMaterial(amount, material, Transform(uid).Coordinates);

View File

@@ -110,7 +110,7 @@ namespace Content.Server.Stack
return list; return list;
int maxCountPerStack = _sharedStack.GetMaxCount(stack); int maxCountPerStack = _sharedStack.GetMaxCount(stack);
var materialPerStack = material._materials[materialProto.ID]; var materialPerStack = material.Materials[materialProto.ID];
var materialPerMaxCount = maxCountPerStack * materialPerStack; var materialPerMaxCount = maxCountPerStack * materialPerStack;

View File

@@ -1,6 +1,4 @@
using System.Linq;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Shared.Materials namespace Content.Shared.Materials
@@ -13,29 +11,6 @@ namespace Content.Shared.Materials
public sealed class MaterialComponent : Component public sealed class MaterialComponent : Component
{ {
[DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<int, MaterialPrototype>))] [DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<int, MaterialPrototype>))]
// ReSharper disable once CollectionNeverUpdated.Local public readonly Dictionary<string, int> Materials = new();
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}");
}
}
}
} }
} }

View File

@@ -26,6 +26,12 @@ public sealed class MaterialStorageComponent : Component
[DataField("whitelist")] [DataField("whitelist")]
public EntityWhitelist? EntityWhitelist; public EntityWhitelist? EntityWhitelist;
/// <summary>
/// Whether or not to drop contained materials when deconstructed.
/// </summary>
[DataField("dropOnDeconstruct")]
public bool DropOnDeconstruct = true;
/// <summary> /// <summary>
/// Whitelist generated on runtime for what specific materials can be inserted into this entity. /// Whitelist generated on runtime for what specific materials can be inserted into this entity.
/// </summary> /// </summary>
@@ -44,11 +50,11 @@ public sealed class MaterialStorageComponent : Component
/// </summary> /// </summary>
public readonly struct MaterialEntityInsertedEvent public readonly struct MaterialEntityInsertedEvent
{ {
public readonly Dictionary<string, int> Materials; public readonly MaterialComponent MaterialComp;
public MaterialEntityInsertedEvent(Dictionary<string, int> materials) public MaterialEntityInsertedEvent(MaterialComponent materials)
{ {
Materials = materials; MaterialComp = materials;
} }
} }

View File

@@ -1,4 +1,4 @@
using System.Linq; using System.Linq;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -152,24 +152,13 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
if (component.EntityWhitelist?.IsValid(toInsert) == false) if (component.EntityWhitelist?.IsValid(toInsert) == false)
return false; return false;
if (component.MaterialWhiteList != null) // Material Whitelist checked implicitly by CanChangeMaterialAmount();
{
var matUsed = false;
foreach (var mat in material.Materials)
{
if (component.MaterialWhiteList.Contains(mat.ID))
matUsed = true;
}
if (!matUsed)
return false;
}
var multiplier = TryComp<StackComponent>(toInsert, out var stackComponent) ? stackComponent.Count : 1; var multiplier = TryComp<StackComponent>(toInsert, out var stackComponent) ? stackComponent.Count : 1;
var totalVolume = 0; var totalVolume = 0;
foreach (var (mat, vol) in component.Storage) foreach (var (mat, vol) in material.Materials)
{ {
if (!CanChangeMaterialAmount(receiver, mat, vol, component)) if (!CanChangeMaterialAmount(receiver, mat, vol * multiplier, component))
return false; return false;
totalVolume += vol * multiplier; totalVolume += vol * multiplier;
} }
@@ -177,12 +166,12 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
if (!CanTakeVolume(receiver, totalVolume, component)) if (!CanTakeVolume(receiver, totalVolume, component))
return false; return false;
foreach (var (mat, vol) in material._materials) foreach (var (mat, vol) in material.Materials)
{ {
TryChangeMaterialAmount(receiver, mat, vol * multiplier, component); TryChangeMaterialAmount(receiver, mat, vol * multiplier, component);
} }
RaiseLocalEvent(component.Owner, new MaterialEntityInsertedEvent(material._materials)); RaiseLocalEvent(component.Owner, new MaterialEntityInsertedEvent(material));
return true; return true;
} }

View File

@@ -585,6 +585,7 @@
- type: Machine - type: Machine
board: OreProcessorMachineCircuitboard board: OreProcessorMachineCircuitboard
- type: MaterialStorage - type: MaterialStorage
dropOnDeconstruct: false #should drop ores instead of ingots/sheets
whitelist: whitelist:
tags: tags:
- Ore - Ore

View File

@@ -1,6 +1,6 @@
- type: material - type: material
id: Glass id: Glass
stackEntity: SheetGlass stackEntity: SheetGlass1
name: materials-glass name: materials-glass
icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: glass } icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: glass }
color: "#a8ccd7" color: "#a8ccd7"
@@ -8,7 +8,7 @@
- type: material - type: material
id: ReinforcedGlass id: ReinforcedGlass
stackEntity: SheetRGlass stackEntity: SheetRGlass1
name: materials-reinforced-glass name: materials-reinforced-glass
icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: rglass } icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: rglass }
color: "#549bb0" color: "#549bb0"
@@ -16,7 +16,7 @@
- type: material - type: material
id: PlasmaGlass id: PlasmaGlass
stackEntity: SheetPGlass stackEntity: SheetPGlass1
name: materials-plasma-glass name: materials-plasma-glass
icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: pglass } icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: pglass }
color: "#b35989" color: "#b35989"
@@ -24,7 +24,7 @@
- type: material - type: material
id: ReinforcedPlasmaGlass id: ReinforcedPlasmaGlass
stackEntity: SheetRPGlass stackEntity: SheetRPGlass1
name: materials-reinforced-plasma-glass name: materials-reinforced-plasma-glass
icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: rpglass } icon: { sprite: Objects/Materials/Sheets/glass.rsi, state: rpglass }
color: "#8c4069" color: "#8c4069"

View File

@@ -1,6 +1,6 @@
- type: material - type: material
id: Biomass id: Biomass
stackEntity: MaterialBiomass stackEntity: MaterialBiomass1
name: materials-biomass name: materials-biomass
icon: { sprite: /Textures/Objects/Misc/monkeycube.rsi, state: cube } icon: { sprite: /Textures/Objects/Misc/monkeycube.rsi, state: cube }
color: "#8A9A5B" color: "#8A9A5B"
@@ -8,7 +8,7 @@
- type: material - type: material
id: Cloth id: Cloth
stackEntity: MaterialCloth stackEntity: MaterialCloth1
name: materials-cloth name: materials-cloth
icon: { sprite: /Textures/Objects/Materials/materials.rsi, state: cloth } icon: { sprite: /Textures/Objects/Materials/materials.rsi, state: cloth }
color: "#e7e7de" color: "#e7e7de"
@@ -16,7 +16,7 @@
- type: material - type: material
id: Durathread id: Durathread
stackEntity: MaterialDurathread stackEntity: MaterialDurathread1
name: materials-durathread name: materials-durathread
icon: { sprite: /Textures/Objects/Materials/materials.rsi, state: durathread } icon: { sprite: /Textures/Objects/Materials/materials.rsi, state: durathread }
color: "#8291a1" color: "#8291a1"
@@ -24,7 +24,7 @@
- type: material - type: material
id: Plasma id: Plasma
stackEntity: SheetPlasma stackEntity: SheetPlasma1
name: materials-plasma name: materials-plasma
icon: { sprite: Objects/Materials/Sheets/other.rsi, state: plasma } icon: { sprite: Objects/Materials/Sheets/other.rsi, state: plasma }
color: "#7e009e" color: "#7e009e"
@@ -32,7 +32,7 @@
- type: material - type: material
id: Plastic id: Plastic
stackEntity: SheetPlastic stackEntity: SheetPlastic1
name: materials-plastic name: materials-plastic
icon: { sprite: Objects/Materials/Sheets/other.rsi, state: plastic } icon: { sprite: Objects/Materials/Sheets/other.rsi, state: plastic }
color: "#d9d9d9" color: "#d9d9d9"
@@ -40,7 +40,7 @@
- type: material - type: material
id: Wood id: Wood
stackEntity: MaterialWoodPlank stackEntity: MaterialWoodPlank1
name: materials-wood name: materials-wood
icon: { sprite: Objects/Materials/materials.rsi, state: wood } icon: { sprite: Objects/Materials/materials.rsi, state: wood }
color: "#966F33" color: "#966F33"
@@ -48,7 +48,7 @@
- type: material - type: material
id: Uranium id: Uranium
stackEntity: SheetUranium stackEntity: SheetUranium1
name: materials-uranium name: materials-uranium
icon: { sprite: Objects/Materials/Sheets/other.rsi, state: uranium } icon: { sprite: Objects/Materials/Sheets/other.rsi, state: uranium }
color: "#32a852" color: "#32a852"

View File

@@ -1,13 +1,13 @@
- type: material - type: material
id: Steel id: Steel
stackEntity: SheetSteel stackEntity: SheetSteel1
name: materials-steel name: materials-steel
icon: { sprite: Objects/Materials/Sheets/metal.rsi, state: steel } icon: { sprite: Objects/Materials/Sheets/metal.rsi, state: steel }
price: 0.05 price: 0.05
- type: material - type: material
id: Gold id: Gold
stackEntity: IngotGold stackEntity: IngotGold1
name: materials-gold name: materials-gold
icon: { sprite: Objects/Materials/ingots.rsi, state: gold } icon: { sprite: Objects/Materials/ingots.rsi, state: gold }
color: "#FFD700" color: "#FFD700"
@@ -15,7 +15,7 @@
- type: material - type: material
id: Silver id: Silver
stackEntity: IngotSilver stackEntity: IngotSilver1
name: materials-silver name: materials-silver
icon: { sprite: Objects/Materials/ingots.rsi, state: silver } icon: { sprite: Objects/Materials/ingots.rsi, state: silver }
color: "#C0C0C0" color: "#C0C0C0"
@@ -23,7 +23,7 @@
- type: material - type: material
id: Plasteel id: Plasteel
stackEntity: SheetPlasteel stackEntity: SheetPlasteel1
name: materials-plasteel name: materials-plasteel
icon: { sprite: Objects/Materials/Sheets/metal.rsi, state: plasteel } icon: { sprite: Objects/Materials/Sheets/metal.rsi, state: plasteel }
color: "#696969" #Okay, this is epic color: "#696969" #Okay, this is epic