RPED can now add parts to in-progress machine frames (#16633)
* RPED construction * Requested Changes
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Robust.Shared.Audio;
|
using Robust.Shared.Audio;
|
||||||
|
|
||||||
namespace Content.Server.Construction.Components;
|
namespace Content.Server.Construction.Components;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using Content.Shared.Popups;
|
|||||||
using Robust.Shared.Containers;
|
using Robust.Shared.Containers;
|
||||||
using Robust.Shared.Utility;
|
using Robust.Shared.Utility;
|
||||||
using Content.Shared.Wires;
|
using Content.Shared.Wires;
|
||||||
|
using Robust.Shared.Collections;
|
||||||
|
|
||||||
namespace Content.Server.Construction;
|
namespace Content.Server.Construction;
|
||||||
|
|
||||||
@@ -35,35 +36,45 @@ public sealed class PartExchangerSystem : EntitySystem
|
|||||||
if (args.Cancelled || args.Handled || args.Args.Target == null)
|
if (args.Cancelled || args.Handled || args.Args.Target == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!TryComp<MachineComponent>(args.Args.Target.Value, out var machine))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!TryComp<ServerStorageComponent>(uid, out var storage) || storage.Storage == null)
|
if (!TryComp<ServerStorageComponent>(uid, out var storage) || storage.Storage == null)
|
||||||
return; //the parts are stored in here
|
return; //the parts are stored in here
|
||||||
|
|
||||||
|
var machinePartQuery = GetEntityQuery<MachinePartComponent>();
|
||||||
|
var machineParts = new List<MachinePartComponent>();
|
||||||
|
|
||||||
|
foreach (var item in storage.Storage.ContainedEntities) //get parts in RPED
|
||||||
|
{
|
||||||
|
if (machinePartQuery.TryGetComponent(item, out var part))
|
||||||
|
machineParts.Add(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
TryExchangeMachineParts(args.Args.Target.Value, storage, machineParts);
|
||||||
|
TryConstructMachineParts(args.Args.Target.Value, storage, machineParts);
|
||||||
|
|
||||||
|
args.Handled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TryExchangeMachineParts(EntityUid uid, ServerStorageComponent storage, List<MachinePartComponent> machineParts)
|
||||||
|
{
|
||||||
|
if (!TryComp<MachineComponent>(uid, out var machine) || storage.Storage == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var machinePartQuery = GetEntityQuery<MachinePartComponent>();
|
||||||
var board = machine.BoardContainer.ContainedEntities.FirstOrNull();
|
var board = machine.BoardContainer.ContainedEntities.FirstOrNull();
|
||||||
|
|
||||||
if (board == null || !TryComp<MachineBoardComponent>(board, out var macBoardComp))
|
if (board == null || !TryComp<MachineBoardComponent>(board, out var macBoardComp))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var machineParts = new List<MachinePartComponent>();
|
foreach (var item in new ValueList<EntityUid>(machine.PartContainer.ContainedEntities)) //clone so don't modify during enumeration
|
||||||
|
|
||||||
foreach (var ent in storage.Storage.ContainedEntities) //get parts in RPED
|
|
||||||
{
|
{
|
||||||
if (TryComp<MachinePartComponent>(ent, out var part))
|
if (machinePartQuery.TryGetComponent(item, out var part))
|
||||||
machineParts.Add(part);
|
|
||||||
}
|
|
||||||
foreach (var ent in new List<EntityUid>(machine.PartContainer.ContainedEntities)) //clone so don't modify during enumeration
|
|
||||||
{
|
|
||||||
if (TryComp<MachinePartComponent>(ent, out var part))
|
|
||||||
{
|
{
|
||||||
machineParts.Add(part);
|
machineParts.Add(part);
|
||||||
_container.RemoveEntity(args.Args.Target.Value, ent);
|
_container.RemoveEntity(uid, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//order by highest rating
|
machineParts.Sort((x, y) => y.Rating.CompareTo(x.Rating));
|
||||||
machineParts = machineParts.OrderByDescending(p => p.Rating).ToList();
|
|
||||||
|
|
||||||
var updatedParts = new List<MachinePartComponent>();
|
var updatedParts = new List<MachinePartComponent>();
|
||||||
foreach (var (type, amount) in macBoardComp.Requirements)
|
foreach (var (type, amount) in macBoardComp.Requirements)
|
||||||
@@ -83,9 +94,56 @@ public sealed class PartExchangerSystem : EntitySystem
|
|||||||
storage.Storage.Insert(unused.Owner);
|
storage.Storage.Insert(unused.Owner);
|
||||||
_storage.Insert(uid, unused.Owner, null, false);
|
_storage.Insert(uid, unused.Owner, null, false);
|
||||||
}
|
}
|
||||||
_construction.RefreshParts(args.Args.Target.Value, machine);
|
_construction.RefreshParts(uid, machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TryConstructMachineParts(EntityUid uid, ServerStorageComponent storage, List<MachinePartComponent> machineParts)
|
||||||
|
{
|
||||||
|
if (!TryComp<MachineFrameComponent>(uid, out var machine) || storage.Storage == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var machinePartQuery = GetEntityQuery<MachinePartComponent>();
|
||||||
|
var board = machine.BoardContainer.ContainedEntities.FirstOrNull();
|
||||||
|
|
||||||
|
if (!machine.HasBoard || !TryComp<MachineBoardComponent>(board, out var macBoardComp))
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var item in new ValueList<EntityUid>(machine.PartContainer.ContainedEntities)) //clone so don't modify during enumeration
|
||||||
|
{
|
||||||
|
if (machinePartQuery.TryGetComponent(item, out var part))
|
||||||
|
{
|
||||||
|
machineParts.Add(part);
|
||||||
|
_container.RemoveEntity(uid, item);
|
||||||
|
machine.Progress[part.PartType]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
machineParts.Sort((x, y) => y.Rating.CompareTo(x.Rating));
|
||||||
|
|
||||||
|
var updatedParts = new List<MachinePartComponent>();
|
||||||
|
foreach (var (type, amount) in macBoardComp.Requirements)
|
||||||
|
{
|
||||||
|
var target = machineParts.Where(p => p.PartType == type).Take(amount);
|
||||||
|
updatedParts.AddRange(target);
|
||||||
|
}
|
||||||
|
foreach (var part in updatedParts)
|
||||||
|
{
|
||||||
|
if (!machine.Requirements.ContainsKey(part.PartType))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
machine.PartContainer.Insert(part.Owner, EntityManager);
|
||||||
|
machine.Progress[part.PartType]++;
|
||||||
|
machineParts.Remove(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
//put the unused parts back into rped. (this also does the "swapping")
|
||||||
|
foreach (var unused in machineParts)
|
||||||
|
{
|
||||||
|
storage.Storage.Insert(unused.Owner);
|
||||||
|
_storage.Insert(uid, unused.Owner, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
args.Handled = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnAfterInteract(EntityUid uid, PartExchangerComponent component, AfterInteractEvent args)
|
private void OnAfterInteract(EntityUid uid, PartExchangerComponent component, AfterInteractEvent args)
|
||||||
@@ -96,7 +154,7 @@ public sealed class PartExchangerSystem : EntitySystem
|
|||||||
if (args.Target == null)
|
if (args.Target == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!HasComp<MachineComponent>(args.Target))
|
if (!HasComp<MachineComponent>(args.Target) && !HasComp<MachineFrameComponent>(args.Target))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (TryComp<WiresPanelComponent>(args.Target, out var panel) && !panel.Open)
|
if (TryComp<WiresPanelComponent>(args.Target, out var panel) && !panel.Open)
|
||||||
|
|||||||
Reference in New Issue
Block a user