From 436d406585c96cad240baeabb39e1fd36b0bee7c Mon Sep 17 00:00:00 2001 From: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Date: Wed, 24 Feb 2021 16:26:56 +0100 Subject: [PATCH] Adds tag support to construction (#3386) --- .../Construction/ConstructionMenuPresenter.cs | 26 ++++------- .../Construction/ConstructionComponent.cs | 16 ++----- .../EntitySystems/ConstructionSystem.cs | 30 ++----------- .../ArbitraryInsertConstructionGraphStep.cs | 9 +++- .../ComponentConstructionGraphStep.cs | 1 - .../Construction/ConstructionGraphEdge.cs | 14 ++++++ .../MultipleTagsConstructionGraphStep.cs | 44 +++++++++++++++++++ .../Construction/TagConstructionGraphStep.cs | 25 +++++++++++ Resources/Locale/en-US/construction.ftl | 2 + .../Recipes/Construction/Graphs/conveyor.yml | 3 +- .../Recipes/Construction/Graphs/firelock.yml | 2 +- 11 files changed, 110 insertions(+), 62 deletions(-) create mode 100644 Content.Shared/Construction/MultipleTagsConstructionGraphStep.cs create mode 100644 Content.Shared/Construction/TagConstructionGraphStep.cs diff --git a/Content.Client/Construction/ConstructionMenuPresenter.cs b/Content.Client/Construction/ConstructionMenuPresenter.cs index 14ba7ca187..3ae7345782 100644 --- a/Content.Client/Construction/ConstructionMenuPresenter.cs +++ b/Content.Client/Construction/ConstructionMenuPresenter.cs @@ -259,12 +259,8 @@ namespace Content.Client.Construction stepList.AddItem(Loc.GetString("{0}. Use a {1}.", stepNumber++, toolStep.Tool.GetToolName()), icon); break; - case PrototypeConstructionGraphStep prototypeStep: - stepList.AddItem(Loc.GetString("{0}. Add {1}.", stepNumber++, prototypeStep.Name), icon); - break; - - case ComponentConstructionGraphStep componentStep: - stepList.AddItem(Loc.GetString("{0}. Add {1}.", stepNumber++, componentStep.Name), icon); + case ArbitraryInsertConstructionGraphStep arbitraryStep: + stepList.AddItem(Loc.GetString("{0}. Add {1}.", stepNumber++, arbitraryStep.Name), icon); break; case NestedConstructionGraphStep nestedStep: @@ -282,19 +278,16 @@ namespace Content.Client.Construction switch (subStep) { case MaterialConstructionGraphStep materialStep: - if (!(prototype.Type == ConstructionType.Item)) stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}x {4}.", stepNumber, parallelNumber, subStepNumber++, materialStep.Amount, materialStep.Material), icon); + if (prototype.Type != ConstructionType.Item) + stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}x {4}.", stepNumber, parallelNumber, subStepNumber++, materialStep.Amount, materialStep.Material), icon); break; case ToolConstructionGraphStep toolStep: stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Use a {3}.", stepNumber, parallelNumber, subStepNumber++, toolStep.Tool.GetToolName()), icon); break; - case PrototypeConstructionGraphStep prototypeStep: - stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}.", stepNumber, parallelNumber, subStepNumber++, prototypeStep.Name), icon); - break; - - case ComponentConstructionGraphStep componentStep: - stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}.", stepNumber, parallelNumber, subStepNumber++, componentStep.Name), icon); + case ArbitraryInsertConstructionGraphStep arbitraryStep: + stepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}.", stepNumber, parallelNumber, subStepNumber++, arbitraryStep.Name), icon); break; } } @@ -357,11 +350,8 @@ namespace Content.Client.Construction break; - case ComponentConstructionGraphStep componentStep: - return componentStep.Icon?.Frame0(); - - case PrototypeConstructionGraphStep prototypeStep: - return prototypeStep.Icon?.Frame0(); + case ArbitraryInsertConstructionGraphStep arbitraryStep: + return arbitraryStep.Icon?.Frame0(); case NestedConstructionGraphStep: return null; diff --git a/Content.Server/GameObjects/Components/Construction/ConstructionComponent.cs b/Content.Server/GameObjects/Components/Construction/ConstructionComponent.cs index 5d4468caef..d0f562f4b8 100644 --- a/Content.Server/GameObjects/Components/Construction/ConstructionComponent.cs +++ b/Content.Server/GameObjects/Components/Construction/ConstructionComponent.cs @@ -186,8 +186,7 @@ namespace Content.Server.GameObjects.Components.Construction { case MaterialConstructionGraphStep _: case ToolConstructionGraphStep _: - case PrototypeConstructionGraphStep _: - case ComponentConstructionGraphStep _: + case ArbitraryInsertConstructionGraphStep _: if (await HandleStep(eventArgs, edge, firstStep)) { if(edge.Steps.Count > 1) @@ -258,17 +257,8 @@ namespace Content.Server.GameObjects.Components.Construction switch (insertStep) { - case PrototypeConstructionGraphStep prototypeStep: - if (prototypeStep.EntityValid(eventArgs.Using) - && await doAfterSystem.DoAfter(doAfterArgs) == DoAfterStatus.Finished) - { - valid = true; - } - - break; - - case ComponentConstructionGraphStep componentStep: - if (componentStep.EntityValid(eventArgs.Using) + case ArbitraryInsertConstructionGraphStep arbitraryStep: + if (arbitraryStep.EntityValid(eventArgs.Using) && await doAfterSystem.DoAfter(doAfterArgs) == DoAfterStatus.Finished) { valid = true; diff --git a/Content.Server/GameObjects/EntitySystems/ConstructionSystem.cs b/Content.Server/GameObjects/EntitySystems/ConstructionSystem.cs index 2d6d8d358d..3ff13e97ab 100644 --- a/Content.Server/GameObjects/EntitySystems/ConstructionSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/ConstructionSystem.cs @@ -185,46 +185,24 @@ namespace Content.Server.GameObjects.EntitySystems break; - case ComponentConstructionGraphStep componentStep: + case ArbitraryInsertConstructionGraphStep arbitraryStep: foreach (var entity in EnumerateNearby(user)) { - if (!componentStep.EntityValid(entity)) + if (!arbitraryStep.EntityValid(entity)) continue; - if (string.IsNullOrEmpty(componentStep.Store)) + if (string.IsNullOrEmpty(arbitraryStep.Store)) { if (!container.Insert(entity)) continue; } - else if (!GetContainer(componentStep.Store).Insert(entity)) + else if (!GetContainer(arbitraryStep.Store).Insert(entity)) continue; handled = true; break; } - break; - - case PrototypeConstructionGraphStep prototypeStep: - foreach (var entity in EnumerateNearby(user)) - { - if (!prototypeStep.EntityValid(entity)) - continue; - - if (string.IsNullOrEmpty(prototypeStep.Store)) - { - if (!container.Insert(entity)) - continue; - } - else if (!GetContainer(prototypeStep.Store).Insert(entity)) - { - continue; - } - - handled = true; - break; - } - break; } diff --git a/Content.Shared/Construction/ArbitraryInsertConstructionGraphStep.cs b/Content.Shared/Construction/ArbitraryInsertConstructionGraphStep.cs index 2a0fbbe389..b02e391019 100644 --- a/Content.Shared/Construction/ArbitraryInsertConstructionGraphStep.cs +++ b/Content.Shared/Construction/ArbitraryInsertConstructionGraphStep.cs @@ -1,4 +1,5 @@ -using Robust.Shared.Serialization; +using Robust.Shared.Localization; +using Robust.Shared.Serialization; using Robust.Shared.Utility; namespace Content.Shared.Construction @@ -15,5 +16,11 @@ namespace Content.Shared.Construction serializer.DataField(this, x => x.Icon, "icon", SpriteSpecifier.Invalid); serializer.DataField(this, x => x.Name, "name", string.Empty); } + + public override void DoExamine(FormattedMessage message, bool inDetailsRange) + { + if (string.IsNullOrEmpty(Name)) return; + message.AddMarkup(Loc.GetString("construction-insert-arbitrary-entity", ("stepName", Name))); + } } } diff --git a/Content.Shared/Construction/ComponentConstructionGraphStep.cs b/Content.Shared/Construction/ComponentConstructionGraphStep.cs index 910837fb1d..ab5e554e7e 100644 --- a/Content.Shared/Construction/ComponentConstructionGraphStep.cs +++ b/Content.Shared/Construction/ComponentConstructionGraphStep.cs @@ -14,7 +14,6 @@ namespace Content.Shared.Construction base.ExposeData(serializer); serializer.DataField(this, x => x.Component, "component", string.Empty); - } public override bool EntityValid(IEntity entity) diff --git a/Content.Shared/Construction/ConstructionGraphEdge.cs b/Content.Shared/Construction/ConstructionGraphEdge.cs index f117f41106..c4fdbf28ef 100644 --- a/Content.Shared/Construction/ConstructionGraphEdge.cs +++ b/Content.Shared/Construction/ConstructionGraphEdge.cs @@ -89,6 +89,20 @@ namespace Content.Shared.Construction return component; } + if (mapping.TryGetNode("tag", out _)) + { + var tags = new TagConstructionGraphStep(); + tags.ExposeData(stepSerializer); + return tags; + } + + if (mapping.TryGetNode("allTags", out _) || mapping.TryGetNode("anyTags", out _)) + { + var tags = new MultipleTagsConstructionGraphStep(); + tags.ExposeData(stepSerializer); + return tags; + } + if(mapping.TryGetNode("steps", out _)) { var nested = new NestedConstructionGraphStep(); diff --git a/Content.Shared/Construction/MultipleTagsConstructionGraphStep.cs b/Content.Shared/Construction/MultipleTagsConstructionGraphStep.cs new file mode 100644 index 0000000000..fd4bd220c6 --- /dev/null +++ b/Content.Shared/Construction/MultipleTagsConstructionGraphStep.cs @@ -0,0 +1,44 @@ +#nullable enable +using System.Collections; +using System.Collections.Generic; +using Content.Shared.GameObjects.Components.Tag; +using Robust.Shared.GameObjects; +using Robust.Shared.Serialization; + +namespace Content.Shared.Construction +{ + public class MultipleTagsConstructionGraphStep : ArbitraryInsertConstructionGraphStep + { + private List? _allTags = null; + private List? _anyTags = null; + + public override void ExposeData(ObjectSerializer serializer) + { + base.ExposeData(serializer); + + serializer.DataField(ref _allTags, "allTags", null); + serializer.DataField(ref _anyTags, "anyTags", null); + } + + private static bool IsNullOrEmpty(ICollection? list) + { + return list == null || list.Count == 0; + } + + public override bool EntityValid(IEntity entity) + { + // This step can only happen if either list has tags. + if (IsNullOrEmpty(_allTags) && IsNullOrEmpty(_anyTags)) + return false; // Step is somehow invalid, we return. + + if (_allTags != null && !entity.HasAllTags(_allTags)) + return false; // We don't have all the tags needed. + + if (_anyTags != null && !entity.HasAnyTag(_anyTags)) + return false; // We don't have any of the tags needed. + + // This entity is valid! + return true; + } + } +} diff --git a/Content.Shared/Construction/TagConstructionGraphStep.cs b/Content.Shared/Construction/TagConstructionGraphStep.cs new file mode 100644 index 0000000000..4532598009 --- /dev/null +++ b/Content.Shared/Construction/TagConstructionGraphStep.cs @@ -0,0 +1,25 @@ +#nullable enable +using System.Collections.Generic; +using Content.Shared.GameObjects.Components.Tag; +using Robust.Shared.GameObjects; +using Robust.Shared.Serialization; + +namespace Content.Shared.Construction +{ + public class TagConstructionGraphStep : ArbitraryInsertConstructionGraphStep + { + private string? _tag = null; + + public override void ExposeData(ObjectSerializer serializer) + { + base.ExposeData(serializer); + + serializer.DataField(ref _tag, "tag", null); + } + + public override bool EntityValid(IEntity entity) + { + return !string.IsNullOrEmpty(_tag) && entity.HasTag(_tag); + } + } +} diff --git a/Resources/Locale/en-US/construction.ftl b/Resources/Locale/en-US/construction.ftl index 13cbcd2ddb..5cf12f2783 100644 --- a/Resources/Locale/en-US/construction.ftl +++ b/Resources/Locale/en-US/construction.ftl @@ -13,3 +13,5 @@ construction-insert-entity-with-component = Next, insert an entity with a {$comp # Shown when examining an in-construction object construction-insert-exact-entity = Next, insert {$entityName}. +# Shown when examining an in-construction object +construction-insert-arbitrary-entity = Next, insert {$stepName}. diff --git a/Resources/Prototypes/Recipes/Construction/Graphs/conveyor.yml b/Resources/Prototypes/Recipes/Construction/Graphs/conveyor.yml index 01b4662295..4f20a6c032 100644 --- a/Resources/Prototypes/Recipes/Construction/Graphs/conveyor.yml +++ b/Resources/Prototypes/Recipes/Construction/Graphs/conveyor.yml @@ -6,8 +6,7 @@ edges: - to: entity steps: - # Replace with tag ASAP - - prototype: ConveyorAssembly + - tag: ConveyorAssembly icon: sprite: Constructible/Power/conveyor.rsi state: conveyor_loose diff --git a/Resources/Prototypes/Recipes/Construction/Graphs/firelock.yml b/Resources/Prototypes/Recipes/Construction/Graphs/firelock.yml index a05a310213..2ced5b24c5 100644 --- a/Resources/Prototypes/Recipes/Construction/Graphs/firelock.yml +++ b/Resources/Prototypes/Recipes/Construction/Graphs/firelock.yml @@ -50,7 +50,7 @@ - !type:EntityAnchored anchored: true steps: - - prototype: FirelockElectronics + - tag: FirelockElectronics name: Firelock Electronics icon: sprite: "Constructible/Misc/module.rsi"