2025-03-28 11:37:34 -04:00
using System.Linq ;
2023-07-08 14:08:32 +10:00
using System.Numerics ;
2024-08-09 22:12:40 -04:00
using Content.Shared.EntityTable ;
2022-10-17 05:43:33 +13:00
using Robust.Shared.Containers ;
using Robust.Shared.Map ;
2023-03-24 22:00:29 +13:00
namespace Content.Shared.Containers ;
2022-10-17 05:43:33 +13:00
public sealed class ContainerFillSystem : EntitySystem
{
[Dependency] private readonly SharedContainerSystem _containerSystem = default ! ;
2024-08-09 22:12:40 -04:00
[Dependency] private readonly EntityTableSystem _entityTable = default ! ;
[Dependency] private readonly SharedTransformSystem _transform = default ! ;
2022-10-17 05:43:33 +13:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < ContainerFillComponent , MapInitEvent > ( OnMapInit ) ;
2024-08-09 22:12:40 -04:00
SubscribeLocalEvent < EntityTableContainerFillComponent , MapInitEvent > ( OnTableMapInit ) ;
2022-10-17 05:43:33 +13:00
}
private void OnMapInit ( EntityUid uid , ContainerFillComponent component , MapInitEvent args )
{
if ( ! TryComp ( uid , out ContainerManagerComponent ? containerComp ) )
return ;
var xform = Transform ( uid ) ;
var coords = new EntityCoordinates ( uid , Vector2 . Zero ) ;
foreach ( var ( contaienrId , prototypes ) in component . Containers )
{
if ( ! _containerSystem . TryGetContainer ( uid , contaienrId , out var container , containerComp ) )
{
2023-06-27 23:56:52 +10:00
Log . Error ( $"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} is missing a container ({contaienrId})." ) ;
2022-10-17 05:43:33 +13:00
continue ;
}
foreach ( var proto in prototypes )
{
var ent = Spawn ( proto , coords ) ;
2023-12-27 21:30:03 -08:00
if ( ! _containerSystem . Insert ( ent , container , containerXform : xform ) )
2022-10-17 05:43:33 +13:00
{
2025-06-26 19:50:49 -04:00
var alreadyContained = container . ContainedEntities . Count > 0 ? string . Join ( "\n" , container . ContainedEntities . Select ( e = > $"\t - {ToPrettyString(e)}" ) ) : "< empty >" ;
2025-03-28 11:37:34 -04:00
Log . Error ( $"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} failed to insert an entity: {ToPrettyString(ent)}.\nCurrent contents:\n{alreadyContained}" ) ;
2025-02-07 03:49:22 -08:00
_transform . AttachToGridOrMap ( ent ) ;
2022-10-17 05:43:33 +13:00
break ;
}
}
}
}
2024-08-09 22:12:40 -04:00
private void OnTableMapInit ( Entity < EntityTableContainerFillComponent > ent , ref MapInitEvent args )
{
if ( ! TryComp ( ent , out ContainerManagerComponent ? containerComp ) )
return ;
if ( TerminatingOrDeleted ( ent ) | | ! Exists ( ent ) )
return ;
var xform = Transform ( ent ) ;
var coords = new EntityCoordinates ( ent , Vector2 . Zero ) ;
foreach ( var ( containerId , table ) in ent . Comp . Containers )
{
if ( ! _containerSystem . TryGetContainer ( ent , containerId , out var container , containerComp ) )
{
Log . Error ( $"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} is missing a container ({containerId})." ) ;
continue ;
}
var spawns = _entityTable . GetSpawns ( table ) ;
foreach ( var proto in spawns )
{
var spawn = Spawn ( proto , coords ) ;
if ( ! _containerSystem . Insert ( spawn , container , containerXform : xform ) )
{
2025-06-26 19:50:49 -04:00
var alreadyContained = container . ContainedEntities . Count > 0 ? string . Join ( "\n" , container . ContainedEntities . Select ( e = > $"\t - {ToPrettyString(e)}" ) ) : "< empty >" ;
2025-03-28 11:37:34 -04:00
Log . Error ( $"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} failed to insert an entity: {ToPrettyString(spawn)}.\nCurrent contents:\n{alreadyContained}" ) ;
2024-08-09 22:12:40 -04:00
_transform . AttachToGridOrMap ( spawn ) ;
break ;
}
}
}
}
2022-10-17 05:43:33 +13:00
}