From 3bdd2e3a872834b48595d33629046f4866016e31 Mon Sep 17 00:00:00 2001 From: deltanedas <39013340+deltanedas@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:36:29 +0000 Subject: [PATCH] reduce lathe recipe copy pasta (#31515) * add inheritance to lathe recipes and make result an override * add GetResult method to lathe system * make other parts of the code use GetResult * clean up the stock parts yml * remove unused apu boards from dynamic recipes * make inverse dictionary public so test doesnt have to copy paste * revert result override stuff --------- Co-authored-by: deltanedas <@deltanedas:kde.org> --- .../Tests/MaterialArbitrageTest.cs | 9 ++--- Content.Shared/Lathe/SharedLatheSystem.cs | 14 +++---- .../Prototypes/LatheRecipePrototype.cs | 12 +++++- .../Entities/Structures/Machines/lathe.yml | 2 - Resources/Prototypes/Recipes/Lathes/Parts.yml | 38 +++++++++++-------- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs b/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs index c48afd819b..ae09405220 100644 --- a/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs +++ b/Content.IntegrationTests/Tests/MaterialArbitrageTest.cs @@ -44,6 +44,7 @@ public sealed class MaterialArbitrageTest var pricing = entManager.System(); var stackSys = entManager.System(); var mapSystem = server.System(); + var latheSys = server.System(); var compFact = server.ResolveDependency(); Assert.That(mapSystem.IsInitialized(testMap.MapId)); @@ -53,12 +54,8 @@ public sealed class MaterialArbitrageTest var materialName = compFact.GetComponentName(typeof(MaterialComponent)); var destructibleName = compFact.GetComponentName(typeof(DestructibleComponent)); - // construct inverted lathe recipe dictionary - Dictionary> latheRecipes = new(); - foreach (var proto in protoManager.EnumeratePrototypes()) - { - latheRecipes.GetOrNew(proto.Result).Add(proto); - } + // get the inverted lathe recipe dictionary + var latheRecipes = latheSys.InverseRecipes; // Lets assume the possible lathe for resource multipliers: // TODO: each recipe can technically have its own cost multiplier associated with it, so this test needs redone to factor that in. diff --git a/Content.Shared/Lathe/SharedLatheSystem.cs b/Content.Shared/Lathe/SharedLatheSystem.cs index e240571f31..dd251ed18b 100644 --- a/Content.Shared/Lathe/SharedLatheSystem.cs +++ b/Content.Shared/Lathe/SharedLatheSystem.cs @@ -19,7 +19,7 @@ public abstract class SharedLatheSystem : EntitySystem [Dependency] private readonly IPrototypeManager _proto = default!; [Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!; - private readonly Dictionary> _inverseRecipeDictionary = new(); + public readonly Dictionary> InverseRecipes = new(); public override void Initialize() { @@ -83,20 +83,20 @@ public abstract class SharedLatheSystem : EntitySystem private void BuildInverseRecipeDictionary() { - _inverseRecipeDictionary.Clear(); + InverseRecipes.Clear(); foreach (var latheRecipe in _proto.EnumeratePrototypes()) { - if (latheRecipe.Result == null) + if (latheRecipe.Result is not {} result) continue; - _inverseRecipeDictionary.GetOrNew(latheRecipe.Result).Add(latheRecipe); + InverseRecipes.GetOrNew(result).Add(latheRecipe); } } public bool TryGetRecipesFromEntity(string prototype, [NotNullWhen(true)] out List? recipes) { recipes = new(); - if (_inverseRecipeDictionary.TryGetValue(prototype, out var r)) + if (InverseRecipes.TryGetValue(prototype, out var r)) recipes.AddRange(r); return recipes.Count != 0; } @@ -111,7 +111,7 @@ public abstract class SharedLatheSystem : EntitySystem if (!string.IsNullOrWhiteSpace(proto.Name)) return Loc.GetString(proto.Name); - if (proto.Result is { } result) + if (proto.Result is {} result) { return _proto.Index(result).Name; } @@ -137,7 +137,7 @@ public abstract class SharedLatheSystem : EntitySystem if (!string.IsNullOrWhiteSpace(proto.Description)) return Loc.GetString(proto.Description); - if (proto.Result is { } result) + if (proto.Result is {} result) { return _proto.Index(result).Description; } diff --git a/Content.Shared/Research/Prototypes/LatheRecipePrototype.cs b/Content.Shared/Research/Prototypes/LatheRecipePrototype.cs index 40c20df343..0fabbd6716 100644 --- a/Content.Shared/Research/Prototypes/LatheRecipePrototype.cs +++ b/Content.Shared/Research/Prototypes/LatheRecipePrototype.cs @@ -4,17 +4,27 @@ using Content.Shared.Lathe.Prototypes; using Content.Shared.Materials; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Array; using Robust.Shared.Utility; namespace Content.Shared.Research.Prototypes { [NetSerializable, Serializable, Prototype] - public sealed partial class LatheRecipePrototype : IPrototype + public sealed partial class LatheRecipePrototype : IPrototype, IInheritingPrototype { [ViewVariables] [IdDataField] public string ID { get; private set; } = default!; + /// + [ParentDataField(typeof(AbstractPrototypeIdArraySerializer))] + public string[]? Parents { get; } + + /// + [NeverPushInheritance] + [AbstractDataField] + public bool Abstract { get; } + /// /// Name displayed in the lathe GUI. /// diff --git a/Resources/Prototypes/Entities/Structures/Machines/lathe.yml b/Resources/Prototypes/Entities/Structures/Machines/lathe.yml index ba20a7b0cd..c606e26f8f 100644 --- a/Resources/Prototypes/Entities/Structures/Machines/lathe.yml +++ b/Resources/Prototypes/Entities/Structures/Machines/lathe.yml @@ -495,8 +495,6 @@ - PortableGeneratorPacmanMachineCircuitboard - PortableGeneratorSuperPacmanMachineCircuitboard - PortableGeneratorJrPacmanMachineCircuitboard - - WallmountGeneratorElectronics - - WallmountGeneratorAPUElectronics - WallmountSubstationElectronics - PowerCageRechargerCircuitboard - EmitterCircuitboard diff --git a/Resources/Prototypes/Recipes/Lathes/Parts.yml b/Resources/Prototypes/Recipes/Lathes/Parts.yml index 90cff2174d..0202919cb9 100644 --- a/Resources/Prototypes/Recipes/Lathes/Parts.yml +++ b/Resources/Prototypes/Recipes/Lathes/Parts.yml @@ -1,26 +1,34 @@ +# Non-stackable part that can have a use outside of machines - type: latheRecipe + abstract: true + id: BasePartRecipe + category: Parts + completetime: 2 + materials: + Steel: 300 + Plastic: 200 + +# Stackable part with no function +- type: latheRecipe + abstract: true + parent: BasePartRecipe + id: BaseStockPartRecipe + completetime: 1 + materials: + Steel: 50 + Plastic: 50 + +- type: latheRecipe + parent: BaseStockPartRecipe id: CapacitorStockPart result: CapacitorStockPart - category: Parts - completetime: 1 - materials: - Steel: 50 - Plastic: 50 - type: latheRecipe + parent: BaseStockPartRecipe id: MatterBinStockPart result: MatterBinStockPart - category: Parts - completetime: 1 - materials: - Steel: 50 - Plastic: 50 - type: latheRecipe + parent: BaseStockPartRecipe id: MicroManipulatorStockPart result: MicroManipulatorStockPart - category: Parts - completetime: 1 - materials: - Steel: 50 - Plastic: 50