From 84edd8ea033cc730a62c79fb6cb497fa6096502d Mon Sep 17 00:00:00 2001 From: Ed Date: Wed, 22 Jan 2025 13:14:47 +0300 Subject: [PATCH 1/2] multiple workbench results --- .../CP14WorkbenchRequirementControl.xaml.cs | 3 +- .../Workbench/CP14WorkbenchWindow.xaml.cs | 3 +- .../_CP14/Workbench/CP14WorkbenchSystem.cs | 75 ++++++++++++++----- .../CP14WorkbenchRecipePrototype.cs | 3 + .../_CP14/Recipes/Workbench/anvil.yml | 1 + 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/Content.Client/_CP14/Workbench/CP14WorkbenchRequirementControl.xaml.cs b/Content.Client/_CP14/Workbench/CP14WorkbenchRequirementControl.xaml.cs index 7686375fbf..1ebd02ebbb 100644 --- a/Content.Client/_CP14/Workbench/CP14WorkbenchRequirementControl.xaml.cs +++ b/Content.Client/_CP14/Workbench/CP14WorkbenchRequirementControl.xaml.cs @@ -54,7 +54,8 @@ public sealed partial class CP14WorkbenchRequirementControl : Control private void UpdateName() { var result = _prototype.Index(_recipePrototype.Result); - Name.Text = Loc.GetString(result.Name); + var counter = _recipePrototype.ResultCount > 1 ? $" x{_recipePrototype.ResultCount}" : ""; + Name.Text = $"{Loc.GetString(result.Name)} {counter}" ; } private void UpdateView() diff --git a/Content.Client/_CP14/Workbench/CP14WorkbenchWindow.xaml.cs b/Content.Client/_CP14/Workbench/CP14WorkbenchWindow.xaml.cs index 170b37af39..44c1ba53bb 100644 --- a/Content.Client/_CP14/Workbench/CP14WorkbenchWindow.xaml.cs +++ b/Content.Client/_CP14/Workbench/CP14WorkbenchWindow.xaml.cs @@ -114,7 +114,8 @@ public sealed partial class CP14WorkbenchWindow : DefaultWindow var result = _prototype.Index(recipe.Result); ItemView.SetPrototype(recipe.Result); - ItemName.Text = result.Name; + var counter = recipe.ResultCount > 1 ? $" x{recipe.ResultCount}" : ""; + ItemName.Text = result.Name + counter; ItemDescription.Text = result.Description; ItemRequirements.RemoveAllChildren(); diff --git a/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs b/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs index 3ba7081152..2f2e27fd16 100644 --- a/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs +++ b/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs @@ -77,7 +77,9 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem if (!_proto.TryIndex(args.Recipe, out var recipe)) return; - var placedEntities = _lookup.GetEntitiesInRange(Transform(ent).Coordinates, ent.Comp.WorkbenchRadius, LookupFlags.Uncontained); + var placedEntities = _lookup.GetEntitiesInRange(Transform(ent).Coordinates, + ent.Comp.WorkbenchRadius, + LookupFlags.Uncontained); if (!CanCraftRecipe(recipe, placedEntities, args.User)) { @@ -85,23 +87,31 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem return; } - if (!_proto.TryIndex(args.Recipe, out var indexedRecipe)) - return; - if (recipe.KnowledgeRequired is not null) _knowledge.UseKnowledge(args.User, recipe.KnowledgeRequired.Value); - var resultEntity = Spawn(indexedRecipe.Result); - _stack.TryMergeToContacts(resultEntity); - - _solutionContainer.TryGetSolution(resultEntity, recipe.Solution, out var resultSolution, out _); - if (recipe.TryMergeSolutions && resultSolution is not null) + var resultEntities = new HashSet(); + for (int i = 0; i < recipe.ResultCount; i++) { - resultSolution.Value.Comp.Solution.MaxVolume = 0; - _solutionContainer.RemoveAllSolution(resultSolution.Value); //If we combine ingredient solutions, we do not use the default solution prescribed in the entity. + var resultEntity = Spawn(recipe.Result); + resultEntities.Add(resultEntity); + _transform.SetCoordinates(resultEntity, Transform(ent).Coordinates); + _stack.TryMergeToContacts(resultEntity); + + if (recipe.TryMergeSolutions) + { + _solutionContainer.TryGetSolution(resultEntity, recipe.Solution, out var resultSolution, out _); + + if (resultSolution is not null) + { + resultSolution.Value.Comp.Solution.MaxVolume = 0; + _solutionContainer.RemoveAllSolution(resultSolution + .Value); //If we combine ingredient solutions, we do not use the default solution prescribed in the entity. + } + } } - foreach (var requiredIngredient in recipe.Entities) + foreach (var requiredIngredient in recipe.Entities) { var requiredCount = requiredIngredient.Value; foreach (var placedEntity in placedEntities) @@ -114,12 +124,33 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem if (placedProto == requiredIngredient.Key && requiredCount > 0) { // Trying merge solutions - if (recipe.TryMergeSolutions - && resultSolution is not null - && _solutionContainer.TryGetSolution(placedEntity, recipe.Solution, out var ingredientSoln, out var ingredientSolution)) + if (recipe.TryMergeSolutions) { - resultSolution.Value.Comp.Solution.MaxVolume += ingredientSoln.Value.Comp.Solution.MaxVolume; - _solutionContainer.TryAddSolution(resultSolution.Value, ingredientSolution); + _solutionContainer.TryGetSolution(placedEntity, + recipe.Solution, + out var ingredientSoln, + out var ingredientSolution); + + if (ingredientSoln is null || + ingredientSolution is null) + continue; + + var splitted = _solutionContainer.SplitSolution(ingredientSoln.Value, + ingredientSolution.Volume / recipe.ResultCount); + + foreach (var resultEntity in resultEntities) + { + _solutionContainer.TryGetSolution(resultEntity, + recipe.Solution, + out var resultSolution, + out _); + if (resultSolution is not null) + { + resultSolution.Value.Comp.Solution.MaxVolume += + ingredientSoln.Value.Comp.Solution.MaxVolume / recipe.ResultCount; + _solutionContainer.TryAddSolution(resultSolution.Value, splitted); + } + } } requiredCount--; @@ -149,12 +180,14 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem requiredCount -= count; } } - _transform.SetCoordinates(resultEntity, Transform(ent).Coordinates); + UpdateUIRecipes(ent, args.User); args.Handled = true; } - private void StartCraft(Entity workbench, EntityUid user, CP14WorkbenchRecipePrototype recipe) + private void StartCraft(Entity workbench, + EntityUid user, + CP14WorkbenchRecipePrototype recipe) { var craftDoAfter = new CP14CraftDoAfterEvent { @@ -185,7 +218,7 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem //Ingredients check var indexedIngredients = IndexIngredients(entities); - foreach (var requiredIngredient in recipe.Entities) + foreach (var requiredIngredient in recipe.Entities) { if (!indexedIngredients.TryGetValue(requiredIngredient.Key, out var availableQuantity) || availableQuantity < requiredIngredient.Value) @@ -209,6 +242,7 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem if (count < value) return false; } + return true; } @@ -227,6 +261,7 @@ public sealed partial class CP14WorkbenchSystem : SharedCP14WorkbenchSystem else indexedIngredients[protoId] = 1; } + return indexedIngredients; } } diff --git a/Content.Shared/_CP14/Workbench/Prototypes/CP14WorkbenchRecipePrototype.cs b/Content.Shared/_CP14/Workbench/Prototypes/CP14WorkbenchRecipePrototype.cs index 67d17e3710..2f1c88b1b6 100644 --- a/Content.Shared/_CP14/Workbench/Prototypes/CP14WorkbenchRecipePrototype.cs +++ b/Content.Shared/_CP14/Workbench/Prototypes/CP14WorkbenchRecipePrototype.cs @@ -35,6 +35,9 @@ public sealed class CP14WorkbenchRecipePrototype : IPrototype [DataField(required: true)] public EntProtoId Result; + [DataField] + public int ResultCount = 1; + [DataField] public bool TryMergeSolutions = false; diff --git a/Resources/Prototypes/_CP14/Recipes/Workbench/anvil.yml b/Resources/Prototypes/_CP14/Recipes/Workbench/anvil.yml index 5cf002da21..920c2d4929 100644 --- a/Resources/Prototypes/_CP14/Recipes/Workbench/anvil.yml +++ b/Resources/Prototypes/_CP14/Recipes/Workbench/anvil.yml @@ -116,6 +116,7 @@ stacks: CP14CopperBar: 1 result: CP14Crossbolt + resultCount: 3 knowledgeRequired: MetallForging - type: CP14Recipe From 32e00d38a1d1c1c7d6df93d5ab7aead4f3a7384e Mon Sep 17 00:00:00 2001 From: Ed Date: Wed, 22 Jan 2025 13:22:56 +0300 Subject: [PATCH 2/2] Update workbenchs.yml --- .../_CP14/Entities/Structures/Furniture/workbenchs.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workbenchs.yml b/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workbenchs.yml index 3c6eabf7e9..0276c5a842 100644 --- a/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workbenchs.yml +++ b/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workbenchs.yml @@ -14,6 +14,8 @@ state: filler - type: ActivatableUI key: enum.CP14WorkbenchUiKey.Key + requiresComplex: true + singleUser: true - type: Climbable - type: Clickable - type: CP14Workbench