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

View 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))
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);
}
@@ -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);
}

View 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))
{

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;
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);

View 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>
/// Component to store data such as "this object is made out of steel".
/// This is not a storage system for say smelteries.
/// 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
{
[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)
{
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;
}

View 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
@@ -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

View 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

View File

@@ -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

View 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

View 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:
@@ -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

View 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
@@ -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

View File

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