Makes construction graphs turing complete (#3516)
* Makes construction graphs turing complete * Improvements
This commit is contained in:
committed by
GitHub
parent
2648ba4e57
commit
13e95ac9a8
33
Content.Server/Construction/Completions/ConditionalAction.cs
Normal file
33
Content.Server/Construction/Completions/ConditionalAction.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class ConditionalAction : IGraphAction
|
||||
{
|
||||
[field: DataField("passUser")] public bool PassUser { get; } = false;
|
||||
|
||||
[field: DataField("condition", required:true)] public IEdgeCondition? Condition { get; } = null;
|
||||
|
||||
[field: DataField("action", required:true)] public IGraphAction? Action { get; } = null;
|
||||
|
||||
[field: DataField("else")] public IGraphAction? Else { get; } = null;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
if (Condition == null || Action == null)
|
||||
return;
|
||||
|
||||
if (await Condition.Condition(PassUser && user != null ? user : entity))
|
||||
await Action.PerformAction(entity, user);
|
||||
else if (Else != null)
|
||||
await Else.PerformAction(entity, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#nullable enable
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[DataDefinition]
|
||||
public class DeleteEntitiesInContainer : IGraphAction
|
||||
{
|
||||
[field: DataField("container")] public string Container { get; } = string.Empty;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Container)) return;
|
||||
if (!entity.TryGetComponent(out ContainerManagerComponent? containerMan)) return;
|
||||
if (!containerMan.TryGetContainer(Container, out var container)) return;
|
||||
|
||||
foreach (var contained in container.ContainedEntities.ToArray())
|
||||
{
|
||||
if(container.Remove(contained))
|
||||
contained.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Content.Server/Construction/Completions/MoveContainer.cs
Normal file
34
Content.Server/Construction/Completions/MoveContainer.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
#nullable enable
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class MoveContainer : IGraphAction
|
||||
{
|
||||
[field: DataField("from")] public string? FromContainer { get; } = null;
|
||||
[field: DataField("to")] public string? ToContainer { get; } = null;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
if (string.IsNullOrEmpty(FromContainer) || string.IsNullOrEmpty(ToContainer))
|
||||
return;
|
||||
|
||||
var from = entity.EnsureContainer<Container>(FromContainer);
|
||||
var to = entity.EnsureContainer<Container>(ToContainer);
|
||||
|
||||
foreach (var contained in from.ContainedEntities.ToArray())
|
||||
{
|
||||
if (from.Remove(contained))
|
||||
to.Insert(contained);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Content.Server/Construction/Completions/PopupEveryone.cs
Normal file
20
Content.Server/Construction/Completions/PopupEveryone.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Utility;
|
||||
using Content.Shared.Construction;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[DataDefinition]
|
||||
public class PopupEveryone : IGraphAction
|
||||
{
|
||||
[field: DataField("text")] public string Text { get; } = string.Empty;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
entity.PopupMessageEveryone(Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,8 @@ namespace Content.Server.Construction.Completions
|
||||
[DataDefinition]
|
||||
public class PopupUser : IGraphAction
|
||||
{
|
||||
[DataField("cursor")] public bool Cursor { get; private set; } = false;
|
||||
[DataField("text")] public string Text { get; private set; } = string.Empty;
|
||||
[field: DataField("cursor")] public bool Cursor { get; } = false;
|
||||
[field: DataField("text")] public string Text { get; } = string.Empty;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class SpawnPrototypeAtContainer : IGraphAction
|
||||
{
|
||||
[field: DataField("prototype")] public string Prototype { get; } = string.Empty;
|
||||
[field: DataField("container")] public string Container { get; } = string.Empty;
|
||||
[field: DataField("amount")] public int Amount { get; } = 1;
|
||||
|
||||
public async Task PerformAction(IEntity entity, IEntity? user)
|
||||
{
|
||||
if (entity.Deleted || string.IsNullOrEmpty(Container) || string.IsNullOrEmpty(Prototype))
|
||||
return;
|
||||
|
||||
var container = entity.EnsureContainer<Container>(Container);
|
||||
|
||||
for (var i = 0; i < Amount; i++)
|
||||
{
|
||||
container.Insert(entity.EntityManager.SpawnEntity(Prototype, entity.Transform.Coordinates));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Content.Server/Construction/Conditions/AllConditions.cs
Normal file
28
Content.Server/Construction/Conditions/AllConditions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Conditions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class AllConditions : IEdgeCondition
|
||||
{
|
||||
[field: DataField("conditions")]
|
||||
public IEdgeCondition[] Conditions { get; } = Array.Empty<IEdgeCondition>();
|
||||
|
||||
public async Task<bool> Condition(IEntity entity)
|
||||
{
|
||||
foreach (var condition in Conditions)
|
||||
{
|
||||
if (!await condition.Condition(entity))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Content.Server/Construction/Conditions/AnyConditions.cs
Normal file
28
Content.Server/Construction/Conditions/AnyConditions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Construction.Conditions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class AnyConditions : IEdgeCondition
|
||||
{
|
||||
[field: DataField("conditions")]
|
||||
public IEdgeCondition[] Conditions { get; } = Array.Empty<IEdgeCondition>();
|
||||
|
||||
public async Task<bool> Condition(IEntity entity)
|
||||
{
|
||||
foreach (var condition in Conditions)
|
||||
{
|
||||
if (await condition.Condition(entity))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Content.Server/Construction/Conditions/ContainerNotEmpty.cs
Normal file
40
Content.Server/Construction/Conditions/ContainerNotEmpty.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Construction.Conditions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class ContainerNotEmpty : IEdgeCondition
|
||||
{
|
||||
[DataField("container")] public string Container { get; private set; } = string.Empty;
|
||||
[DataField("text")] public string Text { get; private set; } = string.Empty;
|
||||
|
||||
public async Task<bool> Condition(IEntity entity)
|
||||
{
|
||||
if (!entity.TryGetComponent(out ContainerManagerComponent? containerManager) ||
|
||||
!containerManager.TryGetContainer(Container, out var container)) return false;
|
||||
|
||||
return container.ContainedEntities.Count != 0;
|
||||
}
|
||||
|
||||
public bool DoExamine(IEntity entity, FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
if (!entity.TryGetComponent(out ContainerManagerComponent? containerManager) ||
|
||||
!containerManager.TryGetContainer(Container, out var container)) return false;
|
||||
|
||||
if (container.ContainedEntities.Count != 0)
|
||||
return false;
|
||||
|
||||
message.AddMarkup(Text);
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -281,7 +281,7 @@ namespace Content.Server.GameObjects.Components.Construction
|
||||
else
|
||||
{
|
||||
_containers.Add(insertStep.Store);
|
||||
var container = ContainerHelpers.EnsureContainer<Container>(Owner, insertStep.Store);
|
||||
var container = Owner.EnsureContainer<Container>(insertStep.Store);
|
||||
container.Insert(entityUsing);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user