Convert materials to use PhysicalComposition (#15414)

This commit is contained in:
Nemanja
2023-04-29 00:53:41 -04:00
committed by GitHub
parent 04abec9a55
commit dfc5bcdc12
13 changed files with 118 additions and 81 deletions

View 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; return false;
_transform.DetachParentToNull(toInsert, Transform(toInsert)); _transform.DetachParentToNull(toInsert, Transform(toInsert));
return true; return true;

View File

@@ -107,11 +107,12 @@ public sealed class MaterialArbitrageTest
var stackProto = protoManager.Index<StackPrototype>(materialStep.MaterialPrototypeId); var stackProto = protoManager.Index<StackPrototype>(materialStep.MaterialPrototypeId);
var spawnProto = protoManager.Index<EntityPrototype>(stackProto.Spawn); 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; continue;
var mat = (MaterialComponent) matreg.Component; var mat = (PhysicalCompositionComponent) compositionReg.Component;
foreach (var (matId, amount) in mat.Materials) foreach (var (matId, amount) in mat.MaterialComposition)
{ {
materials[matId] = materialStep.Amount * amount + materials.GetValueOrDefault(matId); materials[matId] = materialStep.Amount * amount + materials.GetValueOrDefault(matId);
} }
@@ -156,11 +157,13 @@ public sealed class MaterialArbitrageTest
var spawnProto = protoManager.Index<EntityPrototype>(key); var spawnProto = protoManager.Index<EntityPrototype>(key);
// get the amount of each material included in the entity // get the amount of each material included in the entity
if (!spawnProto.Components.TryGetValue(materialName, out var matreg))
continue;
var mat = (MaterialComponent) matreg.Component;
foreach (var (matId, amount) in mat.Materials) if (!spawnProto.Components.ContainsKey(materialName) ||
!spawnProto.Components.TryGetValue(compositionName, out var compositionReg))
continue;
var mat = (PhysicalCompositionComponent) compositionReg.Component;
foreach (var (matId, amount) in mat.MaterialComposition)
{ {
spawnedMats[matId] = value.Max * amount + spawnedMats.GetValueOrDefault(matId); spawnedMats[matId] = value.Max * amount + spawnedMats.GetValueOrDefault(matId);
} }
@@ -235,11 +238,12 @@ public sealed class MaterialArbitrageTest
var spawnProto = protoManager.Index<EntityPrototype>(spawnCompletion.Prototype); 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; continue;
var mat = (MaterialComponent) matreg.Component; var mat = (PhysicalCompositionComponent) compositionReg.Component;
foreach (var (matId, amount) in mat.Materials) foreach (var (matId, amount) in mat.MaterialComposition)
{ {
materials[matId] = spawnCompletion.Amount * amount + materials.GetValueOrDefault(matId); materials[matId] = spawnCompletion.Amount * amount + materials.GetValueOrDefault(matId);
} }

View File

@@ -121,10 +121,10 @@ public sealed class PricingSystem : EntitySystem
return price; return price;
} }
private double GetMaterialPrice(MaterialComponent component) private double GetMaterialPrice(PhysicalCompositionComponent component)
{ {
double price = 0; double price = 0;
foreach (var (id, quantity) in component.Materials) foreach (var (id, quantity) in component.MaterialComposition)
{ {
price += _prototypeManager.Index<MaterialPrototype>(id).Price * quantity; price += _prototypeManager.Index<MaterialPrototype>(id).Price * quantity;
} }
@@ -213,9 +213,10 @@ public sealed class PricingSystem : EntitySystem
{ {
double price = 0; 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)) if (TryComp<StackComponent>(uid, out var stack))
matPrice *= stack.Count; matPrice *= stack.Count;
@@ -229,10 +230,11 @@ public sealed class PricingSystem : EntitySystem
{ {
double price = 0; 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 compositionComp = (PhysicalCompositionComponent) composition.Component;
var matPrice = GetMaterialPrice(materialsComp); var matPrice = GetMaterialPrice(compositionComp);
if (prototype.Components.TryGetValue(_factory.GetComponentName(typeof(StackComponent)), out var stackProto)) if (prototype.Components.TryGetValue(_factory.GetComponentName(typeof(StackComponent)), out var stackProto))
{ {

View 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; return false;
if (TryComp<ApcPowerReceiverComponent>(receiver, out var power) && !power.Powered) if (TryComp<ApcPowerReceiverComponent>(receiver, out var power) && !power.Powered)
return false; return false;
if (!base.TryInsertMaterialEntity(user, toInsert, receiver, component)) if (!base.TryInsertMaterialEntity(user, toInsert, receiver, storage, material, composition))
return false; return false;
_audio.PlayPvs(component.InsertingSound, receiver); _audio.PlayPvs(storage.InsertingSound, receiver);
_popup.PopupEntity(Loc.GetString("machine-insert-item", ("user", user), ("machine", receiver), _popup.PopupEntity(Loc.GetString("machine-insert-item", ("user", user), ("machine", receiver),
("item", toInsert)), receiver); ("item", toInsert)), receiver);
QueueDel(toInsert); QueueDel(toInsert);
@@ -116,10 +121,10 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
return new List<EntityUid>(); return new List<EntityUid>();
var entProto = _prototypeManager.Index<EntityPrototype>(materialProto.StackEntity); 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>(); return new List<EntityUid>();
var materialPerStack = material.Materials[materialProto.ID]; var materialPerStack = composition.MaterialComposition[materialProto.ID];
var amountToSpawn = amount / materialPerStack; var amountToSpawn = amount / materialPerStack;
overflowMaterial = amount - amountToSpawn * materialPerStack; overflowMaterial = amount - amountToSpawn * materialPerStack;
return _stackSystem.SpawnMultiple(materialProto.StackEntity, amountToSpawn, coordinates); return _stackSystem.SpawnMultiple(materialProto.StackEntity, amountToSpawn, coordinates);

View File

@@ -1,16 +1,13 @@
using Robust.Shared.GameStates; 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();
}
} }

View File

@@ -35,13 +35,14 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
base.Update(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) if (_timing.CurTime < inserting.EndTime)
continue; continue;
_appearance.SetData(inserting.Owner, MaterialStorageVisuals.Inserting, false); _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false);
RemComp(inserting.Owner, inserting); RemComp(uid, inserting);
} }
} }
@@ -184,53 +185,53 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
/// <summary> /// <summary>
/// Tries to insert an entity into the material storage. /// Tries to insert an entity into the material storage.
/// </summary> /// </summary>
/// <param name="user"></param> public virtual bool TryInsertMaterialEntity(EntityUid user,
/// <param name="toInsert"></param> EntityUid toInsert,
/// <param name="receiver"></param> EntityUid receiver,
/// <param name="component"></param> MaterialStorageComponent? storage = null,
/// <returns>If it was successful</returns> MaterialComponent? material = null,
public virtual bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null) PhysicalCompositionComponent? composition = null)
{ {
if (!Resolve(receiver, ref component)) if (!Resolve(receiver, ref storage))
return false; return false;
if (!TryComp<MaterialComponent>(toInsert, out var material)) if (!Resolve(toInsert, ref material, ref composition, false))
return false; return false;
if (component.EntityWhitelist?.IsValid(toInsert) == false) if (storage.EntityWhitelist?.IsValid(toInsert) == false)
return false; return false;
// Material Whitelist checked implicitly by CanChangeMaterialAmount(); // Material Whitelist checked implicitly by CanChangeMaterialAmount();
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 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; return false;
totalVolume += vol * multiplier; totalVolume += vol * multiplier;
} }
if (!CanTakeVolume(receiver, totalVolume, component)) if (!CanTakeVolume(receiver, totalVolume, storage))
return false; 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); var insertingComp = EnsureComp<InsertingMaterialStorageComponent>(receiver);
insertingComp.EndTime = _timing.CurTime + component.InsertionTime; insertingComp.EndTime = _timing.CurTime + storage.InsertionTime;
if (!component.IgnoreColor) 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; insertingComp.MaterialColor = lastMat?.Color;
} }
_appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true); _appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true);
Dirty(insertingComp); Dirty(insertingComp);
var ev = new MaterialEntityInsertedEvent(material); var ev = new MaterialEntityInsertedEvent(material);
RaiseLocalEvent(component.Owner, ref ev); RaiseLocalEvent(receiver, ref ev);
return true; return true;
} }

View File

@@ -49,7 +49,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Glass: 100 Glass: 100
- type: Stack - type: Stack
stackType: Glass stackType: Glass
@@ -91,7 +92,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
ReinforcedGlass: 100 ReinforcedGlass: 100
- type: Stack - type: Stack
stackType: ReinforcedGlass stackType: ReinforcedGlass
@@ -133,7 +135,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
PlasmaGlass: 100 PlasmaGlass: 100
- type: Stack - type: Stack
stackType: PlasmaGlass stackType: PlasmaGlass
@@ -172,7 +175,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
ReinforcedPlasmaGlass: 100 ReinforcedPlasmaGlass: 100
- type: Stack - type: Stack
stackType: ReinforcedPlasmaGlass stackType: ReinforcedPlasmaGlass

View File

@@ -36,7 +36,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Steel: 100 Steel: 100
- type: Stack - type: Stack
stackType: Steel stackType: Steel
@@ -74,7 +75,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Plasteel: 100 Plasteel: 100
- type: Stack - type: Stack
stackType: Plasteel stackType: Plasteel

View File

@@ -63,7 +63,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Plasma: 100 Plasma: 100
- type: Stack - type: Stack
stackType: Plasma stackType: Plasma
@@ -113,7 +114,8 @@
- Sheet - Sheet
- DroneUsable - DroneUsable
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Plastic: 100 Plastic: 100
- type: Stack - type: Stack
stackType: Plastic stackType: Plastic
@@ -147,7 +149,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Uranium: 100 Uranium: 100
- type: Stack - type: Stack
stackType: Uranium stackType: Uranium

View File

@@ -34,7 +34,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Gold: 100 Gold: 100
- type: Stack - type: Stack
stackType: Gold stackType: Gold
@@ -68,7 +69,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Silver: 100 Silver: 100
- type: Stack - type: Stack
stackType: Silver stackType: Silver

View File

@@ -32,7 +32,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Cardboard: 100 Cardboard: 100
- type: Stack - type: Stack
stackType: Cardboard stackType: Cardboard
@@ -76,7 +77,8 @@
- type: Stack - type: Stack
stackType: Cloth stackType: Cloth
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Cloth: 100 Cloth: 100
- type: Extractable - type: Extractable
juiceSolution: juiceSolution:
@@ -112,7 +114,8 @@
- type: Stack - type: Stack
stackType: Durathread stackType: Durathread
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Durathread: 100 Durathread: 100
- type: Sprite - type: Sprite
state: durathread_3 state: durathread_3
@@ -144,7 +147,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Wood: 100 Wood: 100
- type: Stack - type: Stack
stackType: WoodPlank stackType: WoodPlank
@@ -175,7 +179,8 @@
suffix: Full suffix: Full
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Biomass: 1 Biomass: 1
- type: Stack - type: Stack
stackType: Biomass stackType: Biomass

View File

@@ -46,7 +46,8 @@
- type: Sprite - type: Sprite
state: gold state: gold
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Gold: 500 Gold: 500
- type: entity - type: entity
@@ -68,7 +69,8 @@
- type: Sprite - type: Sprite
state: iron state: iron
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Steel: 500 Steel: 500
- type: entity - type: entity
@@ -90,7 +92,8 @@
- type: Sprite - type: Sprite
state: plasma state: plasma
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Plasma: 500 Plasma: 500
- type: entity - type: entity
@@ -112,7 +115,8 @@
- type: Sprite - type: Sprite
state: silver state: silver
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Silver: 500 Silver: 500
- type: entity - type: entity
@@ -134,7 +138,8 @@
- type: Sprite - type: Sprite
state: spacequartz state: spacequartz
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Glass: 500 Glass: 500
- type: entity - type: entity
@@ -156,7 +161,8 @@
- type: Sprite - type: Sprite
state: uranium state: uranium
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Uranium: 500 Uranium: 500
- type: entity - type: entity

View File

@@ -5,7 +5,8 @@
description: You gotta have money. description: You gotta have money.
components: components:
- type: Material - type: Material
materials: - type: PhysicalComposition
materialComposition:
Credit: 1 Credit: 1
- type: StaticPrice - type: StaticPrice
price: 0 price: 0