2020-12-03 22:49:00 +01:00
using System ;
using System.Collections.Generic ;
2021-06-09 22:19:39 +02:00
using Content.Server.Stack ;
2021-03-01 15:24:46 -08:00
using Robust.Shared.Containers ;
2020-12-03 22:49:00 +01:00
using Robust.Shared.GameObjects ;
2021-03-05 01:08:38 +01:00
using Robust.Shared.Serialization.Manager.Attributes ;
2020-12-03 22:49:00 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Construction.Components
2020-12-03 22:49:00 +01:00
{
[RegisterComponent]
public class MachineComponent : Component , IMapInit
{
public override string Name = > "Machine" ;
2021-03-05 01:08:38 +01:00
[DataField("board")]
2021-03-16 15:50:20 +01:00
public string? BoardPrototype { get ; private set ; }
2020-12-03 22:49:00 +01:00
2021-03-16 15:50:20 +01:00
private Container _boardContainer = default ! ;
private Container _partContainer = default ! ;
2020-12-03 22:49:00 +01:00
2021-06-19 19:41:26 -07:00
protected override void Initialize ( )
2020-12-03 22:49:00 +01:00
{
base . Initialize ( ) ;
2021-04-04 20:36:35 +02:00
_boardContainer = Owner . EnsureContainer < Container > ( MachineFrameComponent . BoardContainer ) ;
_partContainer = Owner . EnsureContainer < Container > ( MachineFrameComponent . PartContainer ) ;
2020-12-03 22:49:00 +01:00
}
public IEnumerable < MachinePartComponent > GetAllParts ( )
{
foreach ( var entity in _partContainer . ContainedEntities )
{
if ( entity . TryGetComponent < MachinePartComponent > ( out var machinePart ) )
yield return machinePart ;
}
}
public void RefreshParts ( )
{
foreach ( var refreshable in Owner . GetAllComponents < IRefreshParts > ( ) )
{
refreshable . RefreshParts ( GetAllParts ( ) ) ;
}
}
public void CreateBoardAndStockParts ( )
{
// Entity might not be initialized yet.
2021-04-04 20:36:35 +02:00
var boardContainer = Owner . EnsureContainer < Container > ( MachineFrameComponent . BoardContainer , out var existedBoard ) ;
var partContainer = Owner . EnsureContainer < Container > ( MachineFrameComponent . PartContainer , out var existedParts ) ;
2020-12-03 22:49:00 +01:00
if ( string . IsNullOrEmpty ( BoardPrototype ) )
return ;
var entityManager = Owner . EntityManager ;
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 , Owner . Transform . Coordinates ) ;
if ( ! _boardContainer . Insert ( board ) )
{
throw new Exception ( $"Couldn't insert board with prototype {BoardPrototype} to machine with prototype {Owner.Prototype?.ID ?? " N / A "}!" ) ;
}
if ( ! board . TryGetComponent < MachineBoardComponent > ( 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 ] , Owner . Transform . Coordinates ) ;
if ( ! partContainer . Insert ( p ) )
throw new Exception ( $"Couldn't insert machine part of type {part} to machine with prototype {Owner.Prototype?.ID ?? " N / A "}!" ) ;
}
}
foreach ( var ( stackType , amount ) in machineBoard . MaterialRequirements )
{
2021-06-12 11:24:34 +02:00
var stack = EntitySystem . Get < StackSystem > ( ) . Spawn ( amount , stackType , Owner . Transform . Coordinates ) ;
2021-05-26 10:20:57 +02:00
2021-11-09 15:34:40 +01:00
if ( ! partContainer . Insert ( Owner . EntityManager . GetEntity ( stack ) ) )
2020-12-03 22:49:00 +01:00
throw new Exception ( $"Couldn't insert machine material of type {stackType} to machine with prototype {Owner.Prototype?.ID ?? " N / A "}" ) ;
}
foreach ( var ( compName , info ) in machineBoard . ComponentRequirements )
{
for ( var i = 0 ; i < info . Amount ; i + + )
{
var c = entityManager . SpawnEntity ( info . DefaultPrototype , Owner . Transform . Coordinates ) ;
if ( ! partContainer . Insert ( c ) )
throw new Exception ( $"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {Owner.Prototype?.ID ?? " N / A "}" ) ;
}
}
2021-03-08 05:09:30 +01:00
foreach ( var ( tagName , info ) in machineBoard . TagRequirements )
{
for ( var i = 0 ; i < info . Amount ; i + + )
{
var c = entityManager . SpawnEntity ( info . DefaultPrototype , Owner . Transform . Coordinates ) ;
if ( ! partContainer . Insert ( c ) )
throw new Exception ( $"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {Owner.Prototype?.ID ?? " N / A "}" ) ;
}
}
2020-12-03 22:49:00 +01:00
}
public void MapInit ( )
{
CreateBoardAndStockParts ( ) ;
2021-06-19 13:25:05 +02:00
RefreshParts ( ) ;
2020-12-03 22:49:00 +01:00
}
}
}