2021-11-11 16:10:57 -07:00
using Content.Server.Body.Components ;
2021-11-30 16:47:21 -07:00
using Content.Server.Body.Systems ;
2021-07-21 01:41:22 +10:00
using Content.Server.Chemistry.Components ;
2021-10-29 13:40:15 +01:00
using Content.Server.Chemistry.Components.SolutionManager ;
2022-05-12 18:59:03 +08:00
using Content.Shared.Inventory ;
2021-07-21 01:41:22 +10:00
using JetBrains.Annotations ;
using Robust.Shared.Physics.Dynamics ;
2022-09-14 17:26:26 +10:00
using Robust.Shared.Physics.Events ;
2022-05-12 18:59:03 +08:00
using Robust.Shared.Prototypes ;
2021-07-21 01:41:22 +10:00
namespace Content.Server.Chemistry.EntitySystems
{
[UsedImplicitly]
internal sealed class SolutionInjectOnCollideSystem : EntitySystem
{
2022-05-12 18:59:03 +08:00
[Dependency] private readonly IPrototypeManager _protoManager = default ! ;
2021-09-06 15:49:44 +02:00
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default ! ;
2021-11-30 16:47:21 -07:00
[Dependency] private readonly BloodstreamSystem _bloodstreamSystem = default ! ;
2022-05-12 18:59:03 +08:00
[Dependency] private readonly InventorySystem _inventorySystem = default ! ;
2021-11-30 16:47:21 -07:00
2021-07-21 01:41:22 +10:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < SolutionInjectOnCollideComponent , ComponentInit > ( HandleInit ) ;
SubscribeLocalEvent < SolutionInjectOnCollideComponent , StartCollideEvent > ( HandleInjection ) ;
}
private void HandleInit ( EntityUid uid , SolutionInjectOnCollideComponent component , ComponentInit args )
{
2021-09-06 15:49:44 +02:00
component . Owner
. EnsureComponentWarn < SolutionContainerManagerComponent > ( $"{nameof(SolutionInjectOnCollideComponent)} requires a SolutionContainerManager on {component.Owner}!" ) ;
2021-07-21 01:41:22 +10:00
}
2022-09-14 17:26:26 +10:00
private void HandleInjection ( EntityUid uid , SolutionInjectOnCollideComponent component , ref StartCollideEvent args )
2021-07-21 01:41:22 +10:00
{
2023-05-09 19:21:26 +12:00
var target = args . OtherEntity ;
2022-05-12 18:59:03 +08:00
2023-05-09 19:21:26 +12:00
if ( ! args . OtherBody . Hard | |
2022-05-12 18:59:03 +08:00
! EntityManager . TryGetComponent < BloodstreamComponent > ( target , out var bloodstream ) | |
2021-12-03 15:53:09 +01:00
! _solutionsSystem . TryGetInjectableSolution ( component . Owner , out var solution ) ) return ;
2021-07-21 01:41:22 +10:00
2022-05-12 18:59:03 +08:00
if ( component . BlockSlots ! = 0x0 & & TryComp < InventoryComponent > ( target , out var inventory ) )
{
var containerEnumerator = new InventorySystem . ContainerSlotEnumerator ( target , inventory . TemplateId , _protoManager , _inventorySystem , component . BlockSlots ) ;
while ( containerEnumerator . MoveNext ( out var container ) )
{
if ( ! container . ContainedEntity . HasValue ) continue ;
return ;
}
}
2021-07-21 01:41:22 +10:00
var solRemoved = solution . SplitSolution ( component . TransferAmount ) ;
2023-01-12 16:41:40 +13:00
var solRemovedVol = solRemoved . Volume ;
2023-05-09 19:21:26 +12:00
2021-07-21 01:41:22 +10:00
var solToInject = solRemoved . SplitSolution ( solRemovedVol * component . TransferEfficiency ) ;
2022-05-12 18:59:03 +08:00
_bloodstreamSystem . TryAddToChemicals ( target , solToInject , bloodstream ) ;
2021-07-21 01:41:22 +10:00
}
}
}