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

@@ -35,6 +35,93 @@ namespace Content.Server.Tools
InitializeMultipleTools();
}
/// <summary>
/// Whether a tool entity has the specified quality or not.
/// </summary>
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
}
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
/// <summary>
/// Sync version of UseTool.
/// </summary>
/// <param name="tool">The tool entity.</param>
/// <param name="user">The entity using the tool.</param>
/// <param name="target">Optionally, a target to use the tool on.</param>
/// <param name="fuel">An optional amount of fuel or energy to consume-</param>
/// <param name="doAfterDelay">A doAfter delay in seconds.</param>
/// <param name="toolQualitiesNeeded">The tool qualities needed to use the tool.</param>
/// <param name="doAfterCompleteEvent">An event to broadcast once the doAfter is completed successfully.</param>
/// <param name="doAfterCancelledEvent">An event to broadcast once the doAfter is cancelled.</param>
/// <param name="doAfterCheck">An optional check to perform for the doAfter.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Whether initially, using the tool succeeded. If there's a doAfter delay, you'll need to listen to
/// the <see cref="doAfterCompleteEvent"/> and <see cref="doAfterCancelledEvent"/> being broadcast
/// to see whether using the tool succeeded or not. If the <see cref="doAfterDelay"/> is zero,
/// this simply returns whether using the tool succeeded or not.</returns>
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, IEnumerable<string> toolQualitiesNeeded, object doAfterCompleteEvent, object doAfterCancelledEvent,
Func<bool>? doAfterCheck = null, ToolComponent? toolComponent = null)
{
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
if (!Resolve(tool, ref toolComponent, false))
return false;
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
return false;
if (doAfterDelay > 0f)
{
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, default, target)
{
ExtraCheck = doAfterCheck,
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
BroadcastFinishedEvent = doAfterCompleteEvent,
BroadcastCancelledEvent = doAfterCancelledEvent,
};
_doAfterSystem.DoAfter(doAfterArgs);
return true;
}
return ToolFinishUse(tool, user, fuel, toolComponent);
}
// This is hilariously long.
/// <inheritdoc cref="UseTool(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,System.Nullable{Robust.Shared.GameObjects.EntityUid},float,float,System.Collections.Generic.IEnumerable{string},Robust.Shared.GameObjects.EntityUid,object,object,System.Func{bool}?,Content.Server.Tools.Components.ToolComponent?)"/>
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, string toolQualityNeeded, object doAfterCompleteEvent, object doAfterCancelledEvent,
Func<bool>? doAfterCheck = null, ToolComponent? toolComponent = null)
{
return UseTool(tool, user, target, fuel, doAfterDelay, new[] { toolQualityNeeded },
doAfterCompleteEvent, doAfterCancelledEvent, doAfterCheck, toolComponent);
}
/// <summary>
/// Async version of UseTool.
/// </summary>
/// <param name="tool">The tool entity.</param>
/// <param name="user">The entity using the tool.</param>
/// <param name="target">Optionally, a target to use the tool on.</param>
/// <param name="fuel">An optional amount of fuel or energy to consume-</param>
/// <param name="doAfterDelay">A doAfter delay in seconds.</param>
/// <param name="toolQualitiesNeeded">The tool qualities needed to use the tool.</param>
/// <param name="doAfterCheck">An optional check to perform for the doAfter.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Whether using the tool succeeded or not.</returns>
public async Task<bool> UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, IEnumerable<string> toolQualitiesNeeded, Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null)
@@ -43,13 +130,7 @@ namespace Content.Server.Tools
if (!Resolve(tool, ref toolComponent, false))
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded) || !_actionBlockerSystem.CanInteract(user))
return false;
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
RaiseLocalEvent(tool, beforeAttempt, false);
if (beforeAttempt.Cancelled)
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
return false;
if (doAfterDelay > 0f)
@@ -70,6 +151,37 @@ namespace Content.Server.Tools
return false;
}
return ToolFinishUse(tool, user, fuel, toolComponent);
}
// This is hilariously long.
/// <inheritdoc cref="UseTool(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,System.Nullable{Robust.Shared.GameObjects.EntityUid},float,float,System.Collections.Generic.IEnumerable{string},Robust.Shared.GameObjects.EntityUid,object,object,System.Func{bool}?,Content.Server.Tools.Components.ToolComponent?)"/>
public Task<bool> UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, string toolQualityNeeded, Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null)
{
return UseTool(tool, user, target, fuel, doAfterDelay, new [] {toolQualityNeeded}, doAfterCheck, toolComponent);
}
private bool ToolStartUse(EntityUid tool, EntityUid user, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded) || !_actionBlockerSystem.CanInteract(user))
return false;
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
RaiseLocalEvent(tool, beforeAttempt, false);
return !beforeAttempt.Cancelled;
}
private bool ToolFinishUse(EntityUid tool, EntityUid user, float fuel, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
var afterAttempt = new ToolUseFinishAttemptEvent(fuel, user);
RaiseLocalEvent(tool, afterAttempt, false);
@@ -82,13 +194,6 @@ namespace Content.Server.Tools
return true;
}
public Task<bool> UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, string toolQualityNeeded, Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null)
{
return UseTool(tool, user, target, fuel, doAfterDelay, new [] {toolQualityNeeded}, doAfterCheck, toolComponent);
}
public void PlayToolSound(EntityUid uid, ToolComponent? tool = null)
{
if (!Resolve(uid, ref tool))