2022-02-03 10:04:46 +11:00
using Content.Server.Construction.Components ;
2023-01-16 11:53:23 -05:00
using Content.Shared.Construction.Components ;
2022-02-03 10:04:46 +11:00
using Robust.Shared.Containers ;
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 )
{
2022-07-14 04:35:37 -07:00
component . BoardContainer = _container . EnsureContainer < Container > ( uid , MachineFrameComponent . BoardContainerName ) ;
component . PartContainer = _container . EnsureContainer < Container > ( uid , MachineFrameComponent . PartContainerName ) ;
2022-02-03 10:04:46 +11:00
}
private void OnMachineMapInit ( EntityUid uid , MachineComponent component , MapInitEvent args )
{
2023-04-21 20:04:20 +10:00
CreateBoardAndStockParts ( uid , component ) ;
2022-02-03 10:04:46 +11:00
}
2023-04-21 20:04:20 +10:00
private void CreateBoardAndStockParts ( EntityUid uid , MachineComponent component )
2022-02-03 10:04:46 +11:00
{
// Entity might not be initialized yet.
2023-04-21 20:04:20 +10:00
var boardContainer = _container . EnsureContainer < Container > ( uid , MachineFrameComponent . BoardContainerName ) ;
var partContainer = _container . EnsureContainer < Container > ( uid , MachineFrameComponent . PartContainerName ) ;
2022-02-03 10:04:46 +11:00
2024-06-05 16:23:23 -04:00
if ( string . IsNullOrEmpty ( component . Board ) )
2022-02-03 10:04:46 +11:00
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 ;
2024-06-05 16:23:23 -04:00
var xform = Transform ( uid ) ;
if ( ! TrySpawnInContainer ( component . Board , uid , MachineFrameComponent . BoardContainerName , out var board ) )
2022-02-03 10:04:46 +11:00
{
2024-06-05 16:23:23 -04:00
throw new Exception ( $"Couldn't insert board with prototype {component.Board} to machine with prototype {Prototype(uid)?.ID ?? " N / A "}!" ) ;
2022-02-03 10:04:46 +11:00
}
2023-09-20 10:12:48 +10:00
if ( ! TryComp < MachineBoardComponent > ( board , out var machineBoard ) )
2022-02-03 10:04:46 +11:00
{
2024-06-05 16:23:23 -04:00
throw new Exception ( $"Entity with prototype {component.Board} doesn't have a {nameof(MachineBoardComponent)}!" ) ;
2022-02-03 10:04:46 +11:00
}
2024-06-05 16:23:23 -04:00
foreach ( var ( stackType , amount ) in machineBoard . StackRequirements )
2022-02-03 10:04:46 +11:00
{
2024-06-05 16:23:23 -04:00
var stack = _stackSystem . Spawn ( amount , stackType , xform . Coordinates ) ;
2023-12-27 21:30:03 -08:00
if ( ! _container . Insert ( stack , partContainer ) )
2024-06-05 16:23:23 -04:00
throw new Exception ( $"Couldn't insert machine material of type {stackType} to machine with prototype {Prototype(uid)?.ID ?? " N / A "}" ) ;
2022-02-03 10:04:46 +11:00
}
foreach ( var ( compName , info ) in machineBoard . ComponentRequirements )
{
for ( var i = 0 ; i < info . Amount ; i + + )
{
2024-06-05 16:23:23 -04:00
if ( ! TrySpawnInContainer ( info . DefaultPrototype , uid , MachineFrameComponent . PartContainerName , out _ ) )
throw new Exception ( $"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {Prototype(uid)?.ID ?? " N / A "}" ) ;
2022-02-03 10:04:46 +11:00
}
}
foreach ( var ( tagName , info ) in machineBoard . TagRequirements )
{
for ( var i = 0 ; i < info . Amount ; i + + )
{
2024-06-05 16:23:23 -04:00
if ( ! TrySpawnInContainer ( info . DefaultPrototype , uid , MachineFrameComponent . PartContainerName , out _ ) )
throw new Exception ( $"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {Prototype(uid)?.ID ?? " N / A "}" ) ;
2022-02-03 10:04:46 +11:00
}
}
}
}