Skip to content

Commit

Permalink
reduce lathe recipe copy pasta (space-wizards#31515)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
deltanedas authored Aug 29, 2024
1 parent a799d4a commit 3bdd2e3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
9 changes: 3 additions & 6 deletions Content.IntegrationTests/Tests/MaterialArbitrageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task NoMaterialArbitrage()
var pricing = entManager.System<PricingSystem>();
var stackSys = entManager.System<StackSystem>();
var mapSystem = server.System<SharedMapSystem>();
var latheSys = server.System<SharedLatheSystem>();
var compFact = server.ResolveDependency<IComponentFactory>();

Assert.That(mapSystem.IsInitialized(testMap.MapId));
Expand All @@ -53,12 +54,8 @@ public async Task NoMaterialArbitrage()
var materialName = compFact.GetComponentName(typeof(MaterialComponent));
var destructibleName = compFact.GetComponentName(typeof(DestructibleComponent));

// construct inverted lathe recipe dictionary
Dictionary<string, List<LatheRecipePrototype>> latheRecipes = new();
foreach (var proto in protoManager.EnumeratePrototypes<LatheRecipePrototype>())
{
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.
Expand Down
14 changes: 7 additions & 7 deletions Content.Shared/Lathe/SharedLatheSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class SharedLatheSystem : EntitySystem
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!;

private readonly Dictionary<string, List<LatheRecipePrototype>> _inverseRecipeDictionary = new();
public readonly Dictionary<string, List<LatheRecipePrototype>> InverseRecipes = new();

public override void Initialize()
{
Expand Down Expand Up @@ -83,20 +83,20 @@ private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)

private void BuildInverseRecipeDictionary()
{
_inverseRecipeDictionary.Clear();
InverseRecipes.Clear();
foreach (var latheRecipe in _proto.EnumeratePrototypes<LatheRecipePrototype>())
{
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<LatheRecipePrototype>? recipes)
{
recipes = new();
if (_inverseRecipeDictionary.TryGetValue(prototype, out var r))
if (InverseRecipes.TryGetValue(prototype, out var r))
recipes.AddRange(r);
return recipes.Count != 0;
}
Expand All @@ -111,7 +111,7 @@ public string GetRecipeName(LatheRecipePrototype proto)
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;
}
Expand All @@ -137,7 +137,7 @@ public string GetRecipeDescription(LatheRecipePrototype proto)
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;
}
Expand Down
12 changes: 11 additions & 1 deletion Content.Shared/Research/Prototypes/LatheRecipePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
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!;

/// <inheritdoc/>
[ParentDataField(typeof(AbstractPrototypeIdArraySerializer<LatheRecipePrototype>))]
public string[]? Parents { get; }

/// <inheritdoc />
[NeverPushInheritance]
[AbstractDataField]
public bool Abstract { get; }

/// <summary>
/// Name displayed in the lathe GUI.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions Resources/Prototypes/Entities/Structures/Machines/lathe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,6 @@
- PortableGeneratorPacmanMachineCircuitboard
- PortableGeneratorSuperPacmanMachineCircuitboard
- PortableGeneratorJrPacmanMachineCircuitboard
- WallmountGeneratorElectronics
- WallmountGeneratorAPUElectronics
- WallmountSubstationElectronics
- PowerCageRechargerCircuitboard
- EmitterCircuitboard
Expand Down
32 changes: 20 additions & 12 deletions Resources/Prototypes/Recipes/Lathes/Parts.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
# Non-stackable part that can have a use outside of machines
- type: latheRecipe
id: CapacitorStockPart
result: CapacitorStockPart
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

- 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

0 comments on commit 3bdd2e3

Please sign in to comment.