Refactor stacks to use prototypes (#3387)

* Refactor stacks to use prototypes

* Fix not assigned warning

* Add names to stacks

* Make machine baords and material constructions use the name as well

* Remove defaulting stacks to prototype id

* Fix tests

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
DrSmugleaf
2021-02-25 06:18:29 +01:00
committed by GitHub
parent 7c32574547
commit fdcbece63d
33 changed files with 326 additions and 186 deletions

View File

@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using Content.Server.Construction;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Stacks;
using Microsoft.Extensions.Logging;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -14,13 +18,15 @@ namespace Content.Server.GameObjects.Components.Construction
[RegisterComponent]
public class MachineBoardComponent : Component, IExamine
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override string Name => "MachineBoard";
[ViewVariables]
private Dictionary<MachinePart, int> _requirements;
[ViewVariables]
private Dictionary<StackType, int> _materialRequirements;
private Dictionary<string, int> _materialIdRequirements;
[ViewVariables]
private Dictionary<string, ComponentPartInfo> _componentRequirements;
@@ -28,7 +34,20 @@ namespace Content.Server.GameObjects.Components.Construction
[ViewVariables(VVAccess.ReadWrite)]
public string Prototype { get; private set; }
public IReadOnlyDictionary<MachinePart, int> Requirements => _requirements;
public IReadOnlyDictionary<StackType, int> MaterialRequirements => _materialRequirements;
public IReadOnlyDictionary<string, int> MaterialIdRequirements => _materialIdRequirements;
public IEnumerable<KeyValuePair<StackPrototype, int>> MaterialRequirements
{
get
{
foreach (var (materialId, amount) in MaterialIdRequirements)
{
var material = _prototypeManager.Index<StackPrototype>(materialId);
yield return new KeyValuePair<StackPrototype, int>(material, amount);
}
}
}
public IReadOnlyDictionary<string, ComponentPartInfo> ComponentRequirements => _componentRequirements;
public override void ExposeData(ObjectSerializer serializer)
@@ -36,10 +55,23 @@ namespace Content.Server.GameObjects.Components.Construction
base.ExposeData(serializer);
serializer.DataField(this, x => x.Prototype, "prototype", null);
serializer.DataField(ref _requirements, "requirements", new Dictionary<MachinePart, int>());
serializer.DataField(ref _materialRequirements, "materialRequirements", new Dictionary<StackType, int>());
serializer.DataField(ref _materialIdRequirements, "materialRequirements", new Dictionary<string, int>());
serializer.DataField(ref _componentRequirements, "componentRequirements", new Dictionary<string, ComponentPartInfo>());
}
protected override void Startup()
{
base.Startup();
foreach (var material in _materialIdRequirements.Keys)
{
if (!_prototypeManager.HasIndex<StackPrototype>(material))
{
Logger.Error($"No {nameof(StackPrototype)} found with id {material}");
}
}
}
public void Examine(FormattedMessage message, bool inDetailsRange)
{
message.AddMarkup(Loc.GetString("Requires:\n"));
@@ -50,7 +82,7 @@ namespace Content.Server.GameObjects.Components.Construction
foreach (var (material, amount) in MaterialRequirements)
{
message.AddMarkup(Loc.GetString("[color=yellow]{0}x[/color] [color=green]{1}[/color]\n", amount, Loc.GetString(material.ToString())));
message.AddMarkup(Loc.GetString("[color=yellow]{0}x[/color] [color=green]{1}[/color]\n", amount, Loc.GetString(material.Name)));
}
foreach (var (_, info) in ComponentRequirements)

View File

@@ -2,7 +2,6 @@
using System.Threading.Tasks;
using Content.Server.Construction;
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Construction;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
@@ -16,7 +15,7 @@ namespace Content.Server.GameObjects.Components.Construction
[RegisterComponent]
public class MachineFrameComponent : Component, IInteractUsing
{
[Dependency] private IComponentFactory _componentFactory = default!;
[Dependency] private readonly IComponentFactory _componentFactory = default!;
public const string PartContainer = "machine_parts";
public const string BoardContainer = "machine_board";
@@ -60,7 +59,7 @@ namespace Content.Server.GameObjects.Components.Construction
private Dictionary<MachinePart, int> _progress;
[ViewVariables]
private Dictionary<StackType, int> _materialProgress;
private Dictionary<string, int> _materialProgress;
[ViewVariables]
private Dictionary<string, int> _componentProgress;
@@ -75,13 +74,13 @@ namespace Content.Server.GameObjects.Components.Construction
public IReadOnlyDictionary<MachinePart, int> Requirements { get; private set; }
[ViewVariables]
public IReadOnlyDictionary<StackType, int> MaterialRequirements { get; private set; }
public IReadOnlyDictionary<string, int> MaterialRequirements { get; private set; }
[ViewVariables]
public IReadOnlyDictionary<string, ComponentPartInfo> ComponentRequirements { get; private set; }
public IReadOnlyDictionary<MachinePart, int> Progress => _progress;
public IReadOnlyDictionary<StackType, int> MaterialProgress => _materialProgress;
public IReadOnlyDictionary<string, int> MaterialProgress => _materialProgress;
public IReadOnlyDictionary<string, int> ComponentProgress => _componentProgress;
public override void Initialize()
@@ -108,10 +107,10 @@ namespace Content.Server.GameObjects.Components.Construction
private void ResetProgressAndRequirements(MachineBoardComponent machineBoard)
{
Requirements = machineBoard.Requirements;
MaterialRequirements = machineBoard.MaterialRequirements;
MaterialRequirements = machineBoard.MaterialIdRequirements;
ComponentRequirements = machineBoard.ComponentRequirements;
_progress = new Dictionary<MachinePart, int>();
_materialProgress = new Dictionary<StackType, int>();
_materialProgress = new Dictionary<string, int>();
_componentProgress = new Dictionary<string, int>();
foreach (var (machinePart, _) in Requirements)
@@ -179,7 +178,7 @@ namespace Content.Server.GameObjects.Components.Construction
if (part.TryGetComponent<StackComponent>(out var stack))
{
var type = (StackType) stack.StackType;
var type = stack.StackTypeId;
// Check this is part of the requirements...
if (!MaterialRequirements.ContainsKey(type))
continue;
@@ -249,7 +248,7 @@ namespace Content.Server.GameObjects.Components.Construction
if (eventArgs.Using.TryGetComponent<StackComponent>(out var stack))
{
var type = (StackType) stack.StackType;
var type = stack.StackTypeId;
if (!MaterialRequirements.ContainsKey(type))
return false;

View File

@@ -14,7 +14,6 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Stack
{
// TODO: Naming and presentation and such could use some improvement.
[RegisterComponent]
[ComponentReference(typeof(SharedStackComponent))]
@@ -85,7 +84,7 @@ namespace Content.Server.GameObjects.Components.Stack
if (!eventArgs.Using.TryGetComponent<StackComponent>(out var stack))
return false;
if (!stack.StackType.Equals(StackType))
if (!stack.StackTypeId.Equals(StackTypeId))
{
return false;
}