- Completely rewrited the `ConstructionComponent` logic to be ECS, *without* looking too much at the original implementation.
- The original implementation was dirty and unmaintainable, whereas this new implementation is much cleaner, well-organized and maintainable. I've made sure to leave many comments around, explaining what everything does.
- Construction now has a framework for handling events other than `InteractUsing`.
- This means that you can now have CGL steps for things other than inserting items, using tools...
- Construction no longer uses `async` everywhere for `DoAfter`s. Instead it uses events.
- Construction event handling occurs in the `ConstructionSystem` update tick, instead of on event handlers.
- This ensures we can delete/modify entities without worrying about "collection modified while enumerating" exceptions.
- This also means the construction update tick is where all the fun happens, meaning it'll show up on our metrics and give us an idea of how expensive it is/how much tick time is spent in construction.
- `IGraphCondition` and `IGraphAction` have been refactored to take in `EntityUid`, `IEntityManager`, and to not be async.
- Removes nested steps, as they made maintainability significantly worse, and nothing used them yet.
- This fixes #4892 and fixes #4857
Please note, this leaves many things unchanged, as my idea is to split this into multiple PRs. Some unchanged things:
- Initial construction code is the same. In the future, it'll probably use dummy entities.
- Client-side guided steps are the same. In the future, the server will generate the guided steps and send them to clients as needed, caching these in both the server and client to save cycles and bandwidth.
- No new construction graph steps... Yet! 👀
110 lines
4.5 KiB
C#
110 lines
4.5 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Content.Server.Construction.Components;
|
|
using Content.Shared.Construction;
|
|
using JetBrains.Annotations;
|
|
using Robust.Shared.Containers;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.Log;
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
|
|
|
namespace Content.Server.Construction.Completions
|
|
{
|
|
[UsedImplicitly]
|
|
[DataDefinition]
|
|
public class BuildMachine : IGraphAction
|
|
{
|
|
public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
|
|
{
|
|
if (!entityManager.TryGetComponent(uid, out ContainerManagerComponent? containerManager))
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} did not have a container manager! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
if (!entityManager.TryGetComponent(uid, out MachineFrameComponent? machineFrame))
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} did not have a machine frame component! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
if (!machineFrame.IsComplete)
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} doesn't have all required parts to be built! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
if (!containerManager.TryGetContainer(MachineFrameComponent.BoardContainer, out var entBoardContainer))
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} did not have the '{MachineFrameComponent.BoardContainer}' container! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
if (!containerManager.TryGetContainer(MachineFrameComponent.PartContainer, out var entPartContainer))
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} did not have the '{MachineFrameComponent.PartContainer}' container! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
if (entBoardContainer.ContainedEntities.Count != 1)
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} did not have exactly one item in the '{MachineFrameComponent.BoardContainer}' container! Aborting build machine action.");
|
|
}
|
|
|
|
var board = entBoardContainer.ContainedEntities[0];
|
|
|
|
if (!board.TryGetComponent(out MachineBoardComponent? boardComponent))
|
|
{
|
|
Logger.Warning($"Machine frame entity {uid} had an invalid entity in container \"{MachineFrameComponent.BoardContainer}\"! Aborting build machine action.");
|
|
return;
|
|
}
|
|
|
|
entBoardContainer.Remove(board);
|
|
|
|
var transform = entityManager.GetComponent<ITransformComponent>(uid);
|
|
var machine = entityManager.SpawnEntity(boardComponent.Prototype, transform.Coordinates);
|
|
machine.Transform.LocalRotation = transform.LocalRotation;
|
|
|
|
var boardContainer = machine.EnsureContainer<Container>(MachineFrameComponent.BoardContainer, out var existed);
|
|
|
|
if (existed)
|
|
{
|
|
// Clean that up...
|
|
boardContainer.CleanContainer();
|
|
}
|
|
|
|
var partContainer = machine.EnsureContainer<Container>(MachineFrameComponent.PartContainer, out existed);
|
|
|
|
if (existed)
|
|
{
|
|
// Clean that up, too...
|
|
partContainer.CleanContainer();
|
|
}
|
|
|
|
boardContainer.Insert(board);
|
|
|
|
// Now we insert all parts.
|
|
foreach (var part in entPartContainer.ContainedEntities.ToArray())
|
|
{
|
|
entPartContainer.ForceRemove(part);
|
|
partContainer.Insert(part);
|
|
}
|
|
|
|
var constructionSystem = entityManager.EntitySysManager.GetEntitySystem<ConstructionSystem>();
|
|
if (machine.TryGetComponent(out ConstructionComponent? construction))
|
|
{
|
|
// We only add these two container. If some construction needs to take other containers into account, fix this.
|
|
constructionSystem.AddContainer(machine.Uid, MachineFrameComponent.BoardContainer, construction);
|
|
constructionSystem.AddContainer(machine.Uid, MachineFrameComponent.PartContainer, construction);
|
|
}
|
|
|
|
if (machine.TryGetComponent(out MachineComponent? machineComp))
|
|
{
|
|
machineComp.RefreshParts();
|
|
}
|
|
|
|
entityManager.DeleteEntity(uid);
|
|
}
|
|
}
|
|
}
|