Files
OldThink/Content.Server/Construction/ConstructionSystem.cs

96 lines
3.7 KiB
C#
Raw Normal View History

2021-06-09 22:19:39 +02:00
using Content.Server.Construction.Components;
using Content.Server.Stack;
using Content.Shared.Construction;
using Content.Shared.DoAfter;
using JetBrains.Annotations;
2022-10-30 02:48:53 -04:00
using Robust.Server.Containers;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
2023-10-24 00:20:33 +11:00
using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Construction
{
/// <summary>
/// The server-side implementation of the construction system, which is used for constructing entities in game.
/// </summary>
[UsedImplicitly]
public sealed partial class ConstructionSystem : SharedConstructionSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
2022-10-30 02:48:53 -04:00
[Dependency] private readonly ContainerSystem _container = default!;
[Dependency] private readonly StackSystem _stackSystem = default!;
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
public override void Initialize()
{
base.Initialize();
2020-04-20 10:36:02 +01:00
2022-02-03 10:20:17 +11:00
InitializeComputer();
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! :eyes:
2021-10-26 16:38:03 +02:00
InitializeGraphs();
InitializeGuided();
InitializeInteractions();
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! :eyes:
2021-10-26 16:38:03 +02:00
InitializeInitial();
2022-02-03 10:04:46 +11:00
InitializeMachines();
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! :eyes:
2021-10-26 16:38:03 +02:00
SubscribeLocalEvent<ConstructionComponent, ComponentInit>(OnConstructionInit);
SubscribeLocalEvent<ConstructionComponent, ComponentStartup>(OnConstructionStartup);
}
private void OnConstructionInit(Entity<ConstructionComponent> ent, ref ComponentInit args)
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! :eyes:
2021-10-26 16:38:03 +02:00
{
var construction = ent.Comp;
if (GetCurrentGraph(ent, construction) is not {} graph)
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! :eyes:
2021-10-26 16:38:03 +02:00
{
Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid graph specified.");
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! :eyes:
2021-10-26 16:38:03 +02:00
return;
}
if (GetNodeFromGraph(graph, construction.Node) is not {} node)
{
Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid node specified.");
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! :eyes:
2021-10-26 16:38:03 +02:00
return;
}
ConstructionGraphEdge? edge = null;
if (construction.EdgeIndex is {} edgeIndex)
{
if (GetEdgeFromNode(node, edgeIndex) is not {} currentEdge)
{
Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid edge index specified.");
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! :eyes:
2021-10-26 16:38:03 +02:00
return;
}
edge = currentEdge;
}
if (construction.TargetNode is {} targetNodeId)
{
if (GetNodeFromGraph(graph, targetNodeId) is not { } targetNode)
{
Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid target node specified.");
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! :eyes:
2021-10-26 16:38:03 +02:00
return;
}
UpdatePathfinding(ent, graph, node, targetNode, edge, construction);
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! :eyes:
2021-10-26 16:38:03 +02:00
}
}
private void OnConstructionStartup(EntityUid uid, ConstructionComponent construction, ComponentStartup args)
{
if (GetCurrentNode(uid, construction) is not {} node)
return;
PerformActions(uid, null, node.Actions);
}
public override void Update(float frameTime)
{
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! :eyes:
2021-10-26 16:38:03 +02:00
base.Update(frameTime);
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! :eyes:
2021-10-26 16:38:03 +02:00
UpdateInteractions();
2020-06-06 10:40:53 +02:00
}
}
}