Machine-code cleanup (#28489)
This commit is contained in:
@@ -7,7 +7,7 @@ using Content.Shared.Stacks;
|
||||
using Content.Shared.Tag;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Construction;
|
||||
|
||||
@@ -62,24 +62,7 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
// If this changes in the future, then RegenerateProgress() also needs to be updated.
|
||||
// Note that one entity is ALLOWED to satisfy more than one kind of component or tag requirements. This is
|
||||
// necessary in order to avoid weird entity-ordering shenanigans in RegenerateProgress().
|
||||
var stack = CompOrNull<StackComponent>(args.Used);
|
||||
var machinePart = CompOrNull<MachinePartComponent>(args.Used);
|
||||
if (stack != null && machinePart != null)
|
||||
{
|
||||
if (TryInsertPartStack(uid, args.Used, component, machinePart, stack))
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle parts
|
||||
if (machinePart != null)
|
||||
{
|
||||
if (TryInsertPart(uid, args.Used, component, machinePart))
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (stack != null)
|
||||
if (TryComp<StackComponent>(args.Used, out var stack))
|
||||
{
|
||||
if (TryInsertStack(uid, args.Used, component, stack))
|
||||
args.Handled = true;
|
||||
@@ -172,67 +155,6 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <returns>Whether or not the function had any effect. Does not indicate success.</returns>
|
||||
private bool TryInsertPart(EntityUid uid, EntityUid used, MachineFrameComponent component, MachinePartComponent machinePart)
|
||||
{
|
||||
DebugTools.Assert(!HasComp<StackComponent>(uid));
|
||||
if (!component.Requirements.ContainsKey(machinePart.PartType))
|
||||
return false;
|
||||
|
||||
if (component.Progress[machinePart.PartType] >= component.Requirements[machinePart.PartType])
|
||||
return false;
|
||||
|
||||
if (!_container.TryRemoveFromContainer(used))
|
||||
return false;
|
||||
|
||||
if (!_container.Insert(used, component.PartContainer))
|
||||
return true;
|
||||
|
||||
component.Progress[machinePart.PartType]++;
|
||||
if (IsComplete(component))
|
||||
_popupSystem.PopupEntity(Loc.GetString("machine-frame-component-on-complete"), uid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <returns>Whether or not the function had any effect. Does not indicate success.</returns>
|
||||
private bool TryInsertPartStack(EntityUid uid, EntityUid used, MachineFrameComponent component, MachinePartComponent machinePart, StackComponent stack)
|
||||
{
|
||||
if (!component.Requirements.ContainsKey(machinePart.PartType))
|
||||
return false;
|
||||
|
||||
var progress = component.Progress[machinePart.PartType];
|
||||
var requirement = component.Requirements[machinePart.PartType];
|
||||
|
||||
var needed = requirement - progress;
|
||||
if (needed <= 0)
|
||||
return false;
|
||||
|
||||
var count = stack.Count;
|
||||
if (count < needed)
|
||||
{
|
||||
if (!_container.Insert(used, component.PartContainer))
|
||||
return true;
|
||||
|
||||
component.Progress[machinePart.PartType] += count;
|
||||
return true;
|
||||
}
|
||||
|
||||
var splitStack = _stack.Split(used, needed, Transform(uid).Coordinates, stack);
|
||||
|
||||
if (splitStack == null)
|
||||
return false;
|
||||
|
||||
if (!_container.Insert(splitStack.Value, component.PartContainer))
|
||||
return true;
|
||||
|
||||
component.Progress[machinePart.PartType] += needed;
|
||||
if (IsComplete(component))
|
||||
_popupSystem.PopupEntity(Loc.GetString("machine-frame-component-on-complete"), uid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <returns>Whether or not the function had any effect. Does not indicate success.</returns>
|
||||
private bool TryInsertStack(EntityUid uid, EntityUid used, MachineFrameComponent component, StackComponent stack)
|
||||
{
|
||||
@@ -281,12 +203,6 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
if (!component.HasBoard)
|
||||
return false;
|
||||
|
||||
foreach (var (part, amount) in component.Requirements)
|
||||
{
|
||||
if (component.Progress[part] < amount)
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var (type, amount) in component.MaterialRequirements)
|
||||
{
|
||||
if (component.MaterialProgress[type] < amount)
|
||||
@@ -310,21 +226,14 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
|
||||
public void ResetProgressAndRequirements(MachineFrameComponent component, MachineBoardComponent machineBoard)
|
||||
{
|
||||
component.Requirements = new Dictionary<string, int>(machineBoard.Requirements);
|
||||
component.MaterialRequirements = new Dictionary<string, int>(machineBoard.MaterialIdRequirements);
|
||||
component.MaterialRequirements = new Dictionary<ProtoId<StackPrototype>, int>(machineBoard.StackRequirements);
|
||||
component.ComponentRequirements = new Dictionary<string, GenericPartInfo>(machineBoard.ComponentRequirements);
|
||||
component.TagRequirements = new Dictionary<string, GenericPartInfo>(machineBoard.TagRequirements);
|
||||
component.TagRequirements = new Dictionary<ProtoId<TagPrototype>, GenericPartInfo>(machineBoard.TagRequirements);
|
||||
|
||||
component.Progress.Clear();
|
||||
component.MaterialProgress.Clear();
|
||||
component.ComponentProgress.Clear();
|
||||
component.TagProgress.Clear();
|
||||
|
||||
foreach (var (machinePart, _) in component.Requirements)
|
||||
{
|
||||
component.Progress[machinePart] = 0;
|
||||
}
|
||||
|
||||
foreach (var (stackType, _) in component.MaterialRequirements)
|
||||
{
|
||||
component.MaterialProgress[stackType] = 0;
|
||||
@@ -349,7 +258,6 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
component.MaterialRequirements.Clear();
|
||||
component.ComponentRequirements.Clear();
|
||||
component.TagRequirements.Clear();
|
||||
component.Progress.Clear();
|
||||
component.MaterialProgress.Clear();
|
||||
component.ComponentProgress.Clear();
|
||||
component.TagProgress.Clear();
|
||||
@@ -368,19 +276,6 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
|
||||
foreach (var part in component.PartContainer.ContainedEntities)
|
||||
{
|
||||
if (TryComp<MachinePartComponent>(part, out var machinePart))
|
||||
{
|
||||
// Check this is part of the requirements...
|
||||
if (!component.Requirements.ContainsKey(machinePart.PartType))
|
||||
continue;
|
||||
|
||||
if (!component.Progress.ContainsKey(machinePart.PartType))
|
||||
component.Progress[machinePart.PartType] = 1;
|
||||
else
|
||||
component.Progress[machinePart.PartType]++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (TryComp<StackComponent>(part, out var stack))
|
||||
{
|
||||
var type = stack.StackTypeId;
|
||||
@@ -404,9 +299,7 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
if (!HasComp(part, registration.Type))
|
||||
continue;
|
||||
|
||||
if (!component.ComponentProgress.ContainsKey(compName))
|
||||
component.ComponentProgress[compName] = 1;
|
||||
else
|
||||
if (!component.ComponentProgress.TryAdd(compName, 1))
|
||||
component.ComponentProgress[compName]++;
|
||||
}
|
||||
|
||||
@@ -419,18 +312,17 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
if (!_tag.HasTag(tagComp, tagName))
|
||||
continue;
|
||||
|
||||
if (!component.TagProgress.ContainsKey(tagName))
|
||||
component.TagProgress[tagName] = 1;
|
||||
else
|
||||
if (!component.TagProgress.TryAdd(tagName, 1))
|
||||
component.TagProgress[tagName]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void OnMachineFrameExamined(EntityUid uid, MachineFrameComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange)
|
||||
if (!args.IsInDetailsRange || !component.HasBoard)
|
||||
return;
|
||||
if (component.HasBoard)
|
||||
args.PushMarkup(Loc.GetString("machine-frame-component-on-examine-label", ("board", EntityManager.GetComponent<MetaDataComponent>(component.BoardContainer.ContainedEntities[0]).EntityName)));
|
||||
|
||||
var board = component.BoardContainer.ContainedEntities[0];
|
||||
args.PushMarkup(Loc.GetString("machine-frame-component-on-examine-label", ("board", Name(board))));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user