Construction graph improvements (#17960)
This commit is contained in:
committed by
GitHub
parent
fbf1d476f2
commit
9243050e1a
@@ -1,76 +0,0 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Shared.Construction;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Containers;
|
||||
using Robust.Shared.Containers;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed class BuildComputer : IGraphAction
|
||||
{
|
||||
[DataField("container")] public string Container { get; private set; } = string.Empty;
|
||||
|
||||
// TODO use or generalize ConstructionSystem.ChangeEntity();
|
||||
// TODO pass in node/edge & graph ID for better error logs.
|
||||
public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
|
||||
{
|
||||
if (!entityManager.TryGetComponent(uid, out ContainerManagerComponent? containerManager))
|
||||
{
|
||||
Logger.Error($"Computer entity {entityManager.ToPrettyString(uid)} did not have a container manager! Aborting build computer action.");
|
||||
return;
|
||||
}
|
||||
|
||||
var containerSystem = entityManager.EntitySysManager.GetEntitySystem<ContainerSystem>();
|
||||
|
||||
if (!containerSystem.TryGetContainer(uid, Container, out var container, containerManager))
|
||||
{
|
||||
Logger.Error($"Computer entity {entityManager.ToPrettyString(uid)} did not have the specified '{Container}' container! Aborting build computer action.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (container.ContainedEntities.Count != 1)
|
||||
{
|
||||
Logger.Error($"Computer entity {entityManager.ToPrettyString(uid)} did not have exactly one item in the specified '{Container}' container! Aborting build computer action.");
|
||||
return;
|
||||
}
|
||||
|
||||
var board = container.ContainedEntities[0];
|
||||
|
||||
if (!entityManager.TryGetComponent(board, out ComputerBoardComponent? boardComponent))
|
||||
{
|
||||
Logger.Error($"Computer entity {entityManager.ToPrettyString(uid)} had an invalid entity in container \"{Container}\"! Aborting build computer action.");
|
||||
return;
|
||||
}
|
||||
|
||||
container.Remove(board);
|
||||
|
||||
var transform = entityManager.GetComponent<TransformComponent>(uid);
|
||||
var computer = entityManager.SpawnEntity(boardComponent.Prototype, transform.Coordinates);
|
||||
entityManager.GetComponent<TransformComponent>(computer).LocalRotation = transform.LocalRotation;
|
||||
|
||||
var computerContainer = containerSystem.EnsureContainer<Container>(computer, Container);
|
||||
|
||||
// In case it already existed and there are any entities inside the container, delete them.
|
||||
foreach (var ent in computerContainer.ContainedEntities.ToArray())
|
||||
{
|
||||
computerContainer.ForceRemove(ent);
|
||||
entityManager.DeleteEntity(ent);
|
||||
}
|
||||
|
||||
computerContainer.Insert(board);
|
||||
|
||||
// We only add this container. If some construction needs to take other containers into account, fix this.
|
||||
entityManager.EntitySysManager.GetEntitySystem<ConstructionSystem>().AddContainer(computer, Container);
|
||||
|
||||
var entChangeEv = new ConstructionChangeEntityEvent(computer, uid);
|
||||
entityManager.EventBus.RaiseLocalEvent(uid, entChangeEv);
|
||||
entityManager.EventBus.RaiseLocalEvent(computer, entChangeEv, broadcast: true);
|
||||
|
||||
// Delete the original entity.
|
||||
entityManager.QueueDeleteEntity(uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Shared.Construction;
|
||||
using Content.Shared.Construction.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Containers;
|
||||
using Robust.Shared.Containers;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed class BuildMachine : IGraphAction
|
||||
{
|
||||
// TODO use or generalize ConstructionSystem.ChangeEntity();
|
||||
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 (!entityManager.EntitySysManager.GetEntitySystem<MachineFrameSystem>().IsComplete(machineFrame))
|
||||
{
|
||||
Logger.Warning($"Machine frame entity {uid} doesn't have all required parts to be built! Aborting build machine action.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!containerManager.TryGetContainer(MachineFrameComponent.BoardContainerName, out var entBoardContainer))
|
||||
{
|
||||
Logger.Warning($"Machine frame entity {uid} did not have the '{MachineFrameComponent.BoardContainerName}' container! Aborting build machine action.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!containerManager.TryGetContainer(MachineFrameComponent.PartContainerName, out var entPartContainer))
|
||||
{
|
||||
Logger.Warning($"Machine frame entity {uid} did not have the '{MachineFrameComponent.PartContainerName}' 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.BoardContainerName}' container! Aborting build machine action.");
|
||||
}
|
||||
|
||||
var board = entBoardContainer.ContainedEntities[0];
|
||||
|
||||
if (!entityManager.TryGetComponent(board, out MachineBoardComponent? boardComponent))
|
||||
{
|
||||
Logger.Warning($"Machine frame entity {uid} had an invalid entity in container \"{MachineFrameComponent.BoardContainerName}\"! Aborting build machine action.");
|
||||
return;
|
||||
}
|
||||
|
||||
entBoardContainer.Remove(board);
|
||||
|
||||
var containerSys = entityManager.EntitySysManager.GetEntitySystem<ContainerSystem>();
|
||||
var transform = entityManager.GetComponent<TransformComponent>(uid);
|
||||
var machine = entityManager.SpawnEntity(boardComponent.Prototype, transform.Coordinates);
|
||||
entityManager.GetComponent<TransformComponent>(machine).LocalRotation = transform.LocalRotation;
|
||||
|
||||
var boardContainer = containerSys.EnsureContainer<Container>(machine, MachineFrameComponent.BoardContainerName, out var existed);
|
||||
|
||||
if (existed)
|
||||
{
|
||||
// Clean that up...
|
||||
containerSys.CleanContainer(boardContainer);
|
||||
}
|
||||
|
||||
var partContainer = containerSys.EnsureContainer<Container>(machine, MachineFrameComponent.PartContainerName, out existed);
|
||||
|
||||
if (existed)
|
||||
{
|
||||
// Clean that up, too...
|
||||
containerSys.CleanContainer(partContainer);
|
||||
}
|
||||
|
||||
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 (entityManager.TryGetComponent(machine, out ConstructionComponent? construction))
|
||||
{
|
||||
// We only add these two container. If some construction needs to take other containers into account, fix this.
|
||||
constructionSystem.AddContainer(machine, MachineFrameComponent.BoardContainerName, construction);
|
||||
constructionSystem.AddContainer(machine, MachineFrameComponent.PartContainerName, construction);
|
||||
}
|
||||
|
||||
if (entityManager.TryGetComponent(machine, out MachineComponent? machineComp))
|
||||
{
|
||||
constructionSystem.RefreshParts(machine, machineComp);
|
||||
}
|
||||
|
||||
var entChangeEv = new ConstructionChangeEntityEvent(machine, uid);
|
||||
entityManager.EventBus.RaiseLocalEvent(uid, entChangeEv);
|
||||
entityManager.EventBus.RaiseLocalEvent(machine, entChangeEv, broadcast: true);
|
||||
entityManager.QueueDeleteEntity(uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,7 +256,8 @@ namespace Content.Server.Construction
|
||||
$"{ToPrettyString(userUid.Value):player} changed {ToPrettyString(uid):entity}'s node from \"{oldNode}\" to \"{id}\"");
|
||||
|
||||
// ChangeEntity will handle the pathfinding update.
|
||||
if (node.Entity is {} newEntity && ChangeEntity(uid, userUid, newEntity, construction) != null)
|
||||
if (node.Entity.GetId(uid, userUid, new(EntityManager)) is {} newEntity
|
||||
&& ChangeEntity(uid, userUid, newEntity, construction) != null)
|
||||
return true;
|
||||
|
||||
if(performActions)
|
||||
@@ -323,8 +324,6 @@ namespace Content.Server.Construction
|
||||
}
|
||||
}
|
||||
|
||||
EntityManager.InitializeAndStartEntity(newUid);
|
||||
|
||||
// We set the graph and node accordingly.
|
||||
ChangeGraph(newUid, userUid, construction.Graph, construction.Node, false, newConstruction);
|
||||
|
||||
@@ -374,6 +373,13 @@ namespace Content.Server.Construction
|
||||
RaiseLocalEvent(uid, entChangeEv);
|
||||
RaiseLocalEvent(newUid, entChangeEv, broadcast: true);
|
||||
|
||||
foreach (var logic in GetCurrentNode(newUid, newConstruction)!.TransformLogic)
|
||||
{
|
||||
logic.Transform(uid, newUid, userUid, new(EntityManager));
|
||||
}
|
||||
|
||||
EntityManager.InitializeAndStartEntity(newUid);
|
||||
|
||||
QueueDel(uid);
|
||||
|
||||
return newUid;
|
||||
|
||||
@@ -252,7 +252,7 @@ namespace Content.Server.Construction
|
||||
return null;
|
||||
}
|
||||
|
||||
var newEntityProto = graph.Nodes[edge.Target].Entity;
|
||||
var newEntityProto = graph.Nodes[edge.Target].Entity.GetId(null, user, new(EntityManager));
|
||||
var newEntity = EntityManager.SpawnEntity(newEntityProto, EntityManager.GetComponent<TransformComponent>(user).Coordinates);
|
||||
|
||||
if (!TryComp(newEntity, out ConstructionComponent? construction))
|
||||
|
||||
@@ -37,7 +37,7 @@ public sealed class MachineFrameSystem : EntitySystem
|
||||
{
|
||||
RegenerateProgress(component);
|
||||
|
||||
if (TryComp<ConstructionComponent>(uid, out var construction))
|
||||
if (TryComp<ConstructionComponent>(uid, out var construction) && construction.TargetNode == null)
|
||||
{
|
||||
// Attempt to set pathfinding to the machine node...
|
||||
_construction.SetPathfindingTarget(uid, "machine", construction);
|
||||
|
||||
41
Content.Server/Construction/NodeEntities/BoardNodeEntity.cs
Normal file
41
Content.Server/Construction/NodeEntities/BoardNodeEntity.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Shared.Construction;
|
||||
using Content.Shared.Construction.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Containers;
|
||||
|
||||
namespace Content.Server.Construction.NodeEntities;
|
||||
|
||||
/// <summary>
|
||||
/// Works for both <see cref="ComputerBoardComponent"/> and <see cref="MachineBoardComponent"/>
|
||||
/// because duplicating code just for this is really stinky.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed class BoardNodeEntity : IGraphNodeEntity
|
||||
{
|
||||
[DataField("container")] public string Container { get; private set; } = string.Empty;
|
||||
|
||||
public string? GetId(EntityUid? uid, EntityUid? userUid, GraphNodeEntityArgs args)
|
||||
{
|
||||
if (uid == null)
|
||||
return null;
|
||||
|
||||
var containerSystem = args.EntityManager.EntitySysManager.GetEntitySystem<ContainerSystem>();
|
||||
|
||||
if (!containerSystem.TryGetContainer(uid.Value, Container, out var container)
|
||||
|| container.ContainedEntities.Count == 0)
|
||||
return null;
|
||||
|
||||
var board = container.ContainedEntities[0];
|
||||
|
||||
// There should not be a case where both of these components exist on the same entity...
|
||||
if (args.EntityManager.TryGetComponent(board, out MachineBoardComponent? machine))
|
||||
return machine.Prototype;
|
||||
|
||||
if(args.EntityManager.TryGetComponent(board, out ComputerBoardComponent? computer))
|
||||
return computer.Prototype;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user