ConstructionGL2 Part 1: ECSification, events and steps. (#5017)

- 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! 👀
This commit is contained in:
Vera Aguilera Puerto
2021-10-26 16:38:03 +02:00
committed by GitHub
parent d70470b99b
commit 189a5c7847
57 changed files with 1877 additions and 1265 deletions

View File

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Content.Server.Construction.Components;
using Content.Shared.Construction;
using JetBrains.Annotations;
using Robust.Server.Containers;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
@@ -16,60 +17,57 @@ namespace Content.Server.Construction.Completions
{
[DataField("container")] public string Container { get; private set; } = string.Empty;
public async Task PerformAction(IEntity entity, IEntity? user)
public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
{
if (!entity.TryGetComponent(out ContainerManagerComponent? containerManager))
if (!entityManager.TryGetComponent(uid, out ContainerManagerComponent? containerManager))
{
Logger.Warning($"Computer entity {entity} did not have a container manager! Aborting build computer action.");
Logger.Warning($"Computer entity {uid} did not have a container manager! Aborting build computer action.");
return;
}
if (!containerManager.TryGetContainer(Container, out var container))
var containerSystem = entityManager.EntitySysManager.GetEntitySystem<ContainerSystem>();
if (!containerSystem.TryGetContainer(uid, Container, out var container, containerManager))
{
Logger.Warning($"Computer entity {entity} did not have the specified '{Container}' container! Aborting build computer action.");
Logger.Warning($"Computer entity {uid} did not have the specified '{Container}' container! Aborting build computer action.");
return;
}
if (container.ContainedEntities.Count != 1)
{
Logger.Warning($"Computer entity {entity} did not have exactly one item in the specified '{Container}' container! Aborting build computer action.");
Logger.Warning($"Computer entity {uid} did not have exactly one item in the specified '{Container}' container! Aborting build computer action.");
}
var board = container.ContainedEntities[0];
if (!board.TryGetComponent(out ComputerBoardComponent? boardComponent))
{
Logger.Warning($"Computer entity {entity} had an invalid entity in container \"{Container}\"! Aborting build computer action.");
Logger.Warning($"Computer entity {uid} had an invalid entity in container \"{Container}\"! Aborting build computer action.");
return;
}
var entityManager = entity.EntityManager;
container.Remove(board);
var computer = entityManager.SpawnEntity(boardComponent.Prototype, entity.Transform.Coordinates);
computer.Transform.LocalRotation = entity.Transform.LocalRotation;
var transform = entityManager.GetComponent<ITransformComponent>(uid);
var computer = entityManager.SpawnEntity(boardComponent.Prototype, transform.Coordinates);
computer.Transform.LocalRotation = transform.LocalRotation;
var computerContainer = ContainerHelpers.EnsureContainer<Container>(computer, Container, out var existed);
var computerContainer = containerSystem.EnsureContainer<Container>(computer.Uid, Container);
if (existed)
// In case it already existed and there are any entities inside the container, delete them.
foreach (var ent in computerContainer.ContainedEntities.ToArray())
{
// In case there are any entities inside this, delete them.
foreach (var ent in computerContainer.ContainedEntities.ToArray())
{
computerContainer.ForceRemove(ent);
ent.Delete();
}
computerContainer.ForceRemove(ent);
ent.Delete();
}
computerContainer.Insert(board);
if (computer.TryGetComponent(out ConstructionComponent? construction))
{
// We only add this container. If some construction needs to take other containers into account, fix this.
construction.AddContainer(Container);
}
// We only add this container. If some construction needs to take other containers into account, fix this.
entityManager.EntitySysManager.GetEntitySystem<ConstructionSystem>().AddContainer(computer.Uid, Container);
entity.Delete();
// Delete the original entity.
entityManager.DeleteEntity(uid);
}
}
}