ECS construction machines (#6354)

This commit is contained in:
metalgearsloth
2022-02-03 10:04:46 +11:00
committed by GitHub
parent 5091c6aa9d
commit 42102b17c1
4 changed files with 124 additions and 118 deletions

View File

@@ -1,11 +1,9 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Content.Server.Construction.Components; using Content.Server.Construction.Components;
using Content.Shared.Construction; using Content.Shared.Construction;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
@@ -101,7 +99,7 @@ namespace Content.Server.Construction.Completions
if (entityManager.TryGetComponent(machine, out MachineComponent? machineComp)) if (entityManager.TryGetComponent(machine, out MachineComponent? machineComp))
{ {
machineComp.RefreshParts(); constructionSystem.RefreshParts(machineComp);
} }
entityManager.DeleteEntity(uid); entityManager.DeleteEntity(uid);

View File

@@ -1,125 +1,18 @@
using System; using Robust.Shared.Containers;
using System.Collections.Generic;
using Content.Server.Stack;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Construction.Components namespace Content.Server.Construction.Components
{ {
[RegisterComponent] [RegisterComponent, ComponentProtoName("Machine")]
public class MachineComponent : Component, IMapInit public sealed class MachineComponent : Component
{ {
[Dependency] private readonly IEntityManager _entMan = default!; [DataField("board", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
[DataField("board")]
public string? BoardPrototype { get; private set; } public string? BoardPrototype { get; private set; }
private Container _boardContainer = default!; public Container BoardContainer = default!;
private Container _partContainer = default!; public Container PartContainer = default!;
protected override void Initialize()
{
base.Initialize();
_boardContainer = Owner.EnsureContainer<Container>(MachineFrameComponent.BoardContainer);
_partContainer = Owner.EnsureContainer<Container>(MachineFrameComponent.PartContainer);
}
public IEnumerable<MachinePartComponent> GetAllParts()
{
foreach (var entity in _partContainer.ContainedEntities)
{
if (_entMan.TryGetComponent<MachinePartComponent?>(entity, out var machinePart))
yield return machinePart;
}
}
public void RefreshParts()
{
foreach (var refreshable in _entMan.GetComponents<IRefreshParts>(Owner))
{
refreshable.RefreshParts(GetAllParts());
}
}
public void CreateBoardAndStockParts()
{
// Entity might not be initialized yet.
var boardContainer = Owner.EnsureContainer<Container>(MachineFrameComponent.BoardContainer, out var existedBoard);
var partContainer = Owner.EnsureContainer<Container>(MachineFrameComponent.PartContainer, out var existedParts);
if (string.IsNullOrEmpty(BoardPrototype))
return;
var entityManager = _entMan;
if (existedBoard || existedParts)
{
// We're done here, let's suppose all containers are correct just so we don't screw SaveLoadSave.
if (boardContainer.ContainedEntities.Count > 0)
return;
}
var board = entityManager.SpawnEntity(BoardPrototype, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if (!_boardContainer.Insert(board))
{
throw new Exception($"Couldn't insert board with prototype {BoardPrototype} to machine with prototype {_entMan.GetComponent<MetaDataComponent>(Owner).EntityPrototype?.ID ?? "N/A"}!");
}
if (!_entMan.TryGetComponent<MachineBoardComponent?>(board, out var machineBoard))
{
throw new Exception($"Entity with prototype {BoardPrototype} doesn't have a {nameof(MachineBoardComponent)}!");
}
foreach (var (part, amount) in machineBoard.Requirements)
{
for (var i = 0; i < amount; i++)
{
var p = entityManager.SpawnEntity(MachinePartComponent.Prototypes[part], _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if (!partContainer.Insert(p))
throw new Exception($"Couldn't insert machine part of type {part} to machine with prototype {_entMan.GetComponent<MetaDataComponent>(Owner).EntityPrototype?.ID ?? "N/A"}!");
}
}
foreach (var (stackType, amount) in machineBoard.MaterialRequirements)
{
var stack = EntitySystem.Get<StackSystem>().Spawn(amount, stackType, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if (!partContainer.Insert(stack))
throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {_entMan.GetComponent<MetaDataComponent>(Owner).EntityPrototype?.ID ?? "N/A"}");
}
foreach (var (compName, info) in machineBoard.ComponentRequirements)
{
for (var i = 0; i < info.Amount; i++)
{
var c = entityManager.SpawnEntity(info.DefaultPrototype, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {_entMan.GetComponent<MetaDataComponent>(Owner).EntityPrototype?.ID ?? "N/A"}");
}
}
foreach (var (tagName, info) in machineBoard.TagRequirements)
{
for (var i = 0; i < info.Amount; i++)
{
var c = entityManager.SpawnEntity(info.DefaultPrototype, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {_entMan.GetComponent<MetaDataComponent>(Owner).EntityPrototype?.ID ?? "N/A"}");
}
}
}
public void MapInit()
{
CreateBoardAndStockParts();
RefreshParts();
}
} }
} }

View File

@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using Content.Server.Construction.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
namespace Content.Server.Construction;
public sealed partial class ConstructionSystem
{
private void InitializeMachines()
{
SubscribeLocalEvent<MachineComponent, ComponentInit>(OnMachineInit);
SubscribeLocalEvent<MachineComponent, MapInitEvent>(OnMachineMapInit);
}
private void OnMachineInit(EntityUid uid, MachineComponent component, ComponentInit args)
{
component.BoardContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.BoardContainer);
component.PartContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.PartContainer);
}
private void OnMachineMapInit(EntityUid uid, MachineComponent component, MapInitEvent args)
{
CreateBoardAndStockParts(component);
RefreshParts(component);
}
public IEnumerable<MachinePartComponent> GetAllParts(MachineComponent component)
{
foreach (var entity in component.PartContainer.ContainedEntities)
{
if (TryComp<MachinePartComponent?>(entity, out var machinePart))
yield return machinePart;
}
}
public void RefreshParts(MachineComponent component)
{
foreach (var refreshable in EntityManager.GetComponents<IRefreshParts>(component.Owner))
{
refreshable.RefreshParts(GetAllParts(component));
}
}
public void CreateBoardAndStockParts(MachineComponent component)
{
// Entity might not be initialized yet.
var boardContainer = _container.EnsureContainer<Container>(component.Owner, MachineFrameComponent.BoardContainer);
var partContainer = _container.EnsureContainer<Container>(component.Owner, MachineFrameComponent.PartContainer);
if (string.IsNullOrEmpty(component.BoardPrototype))
return;
// We're done here, let's suppose all containers are correct just so we don't screw SaveLoadSave.
if (boardContainer.ContainedEntities.Count > 0)
return;
var board = EntityManager.SpawnEntity(component.BoardPrototype, Transform(component.Owner).Coordinates);
if (!component.BoardContainer.Insert(board))
{
throw new Exception($"Couldn't insert board with prototype {component.BoardPrototype} to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}!");
}
if (!TryComp<MachineBoardComponent?>(board, out var machineBoard))
{
throw new Exception($"Entity with prototype {component.BoardPrototype} doesn't have a {nameof(MachineBoardComponent)}!");
}
foreach (var (part, amount) in machineBoard.Requirements)
{
for (var i = 0; i < amount; i++)
{
var p = EntityManager.SpawnEntity(MachinePartComponent.Prototypes[part], Transform(component.Owner).Coordinates);
if (!partContainer.Insert(p))
throw new Exception($"Couldn't insert machine part of type {part} to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}!");
}
}
foreach (var (stackType, amount) in machineBoard.MaterialRequirements)
{
var stack = _stackSystem.Spawn(amount, stackType, Transform(component.Owner).Coordinates);
if (!partContainer.Insert(stack))
throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}");
}
foreach (var (compName, info) in machineBoard.ComponentRequirements)
{
for (var i = 0; i < info.Amount; i++)
{
var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(component.Owner).Coordinates);
if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}");
}
}
foreach (var (tagName, info) in machineBoard.TagRequirements)
{
for (var i = 0; i < info.Amount; i++)
{
var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(component.Owner).Coordinates);
if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}");
}
}
}
}

View File

@@ -7,6 +7,7 @@ using Content.Shared.Examine;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
@@ -28,6 +29,7 @@ namespace Content.Server.Construction
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!; [Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly StackSystem _stackSystem = default!; [Dependency] private readonly StackSystem _stackSystem = default!;
[Dependency] private readonly ToolSystem _toolSystem = default!; [Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
private const string SawmillName = "Construction"; private const string SawmillName = "Construction";
private ISawmill _sawmill = default!; private ISawmill _sawmill = default!;
@@ -42,6 +44,7 @@ namespace Content.Server.Construction
InitializeGuided(); InitializeGuided();
InitializeInteractions(); InitializeInteractions();
InitializeInitial(); InitializeInitial();
InitializeMachines();
SubscribeLocalEvent<ConstructionComponent, ComponentInit>(OnConstructionInit); SubscribeLocalEvent<ConstructionComponent, ComponentInit>(OnConstructionInit);
SubscribeLocalEvent<ConstructionComponent, ComponentStartup>(OnConstructionStartup); SubscribeLocalEvent<ConstructionComponent, ComponentStartup>(OnConstructionStartup);