Files
space-station-14/Content.Server/Chemistry/EntitySystems/SolutionInjectOnEventSystem.cs
Nikovnik 0e76d4e5ed Metabolizing bloodstream (#35071)
* merged chemical into bloodstream

* changed injectable to bloodstream

* separated bleeding and direct blood removal

* removed blood gain from protein

* reduced blood gain from saline

* rejuvenating fills to reference volume

* fixed blood regulation

* red mead requires stirring to make

* reverted accidental line deletion

* cleared the skeletons from the closet

* additional routing

* field rename for xeno

* removed mention of chemstream and field rename for asteroid mobs

* minor optimizations

* Revert "reduced blood gain from saline"

This reverts commit de26fd1c0d.

* Revert "removed blood gain from protein"

This reverts commit 7a1648caf3.

* removed unused component fetch

* dead check mini refactor

* eventized blood exclusion

* quick fix

* Pain

* Commit of doom

* COMMIT

* renamed bloodMaxFactor to MaxVolumeFactor

* addressed floating point error

* returned vomiting chemicals

* blood reagent always skips the flush

* no need to mention blood reagent

* fixed passing blood flush

* adadsafasfasfassfasf

* whoops

* merge fixed injectors

* Revert "adadsafasfasfassfasf"

This reverts commit 0a5313a68d.

* simplify reagent removal

* enabled foreign blood transfusion

* Revert "COMMIT"

This reverts commit 19abd679cd.

* simplified reagent removal when modifying blood level

* removed misleading coment since the changes

* documented MetabolismExclusionEvent

* fixed negative negative modification of blood level

* fixed hypervolemia not normalizing

* constrainted blood modification

* returned bloodpack stop on fully healed

* forgot to stage this

* band aid for diona blood

* swapping GetReagent with GetPrototype

* optimize blood filtering

* multiplicative multi reagent blood level calculation

* removed unused stuff

* optimized blood calculation a tiny bit

* added per reagent blood regulation

* optimized (referenceVolume + bloodReagents) into referenceSolution

* polished coded to proper function

* forgot to stage rootable system change

* clean up, unnecessary GetBloodLevel call

* rename method name to TryAddToBloodstream instead of Chemicals

* placed overfill safety

* cleanup and final touches

* final touch

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2025-12-17 19:21:16 +00:00

159 lines
7.1 KiB
C#

using Content.Server.Body.Systems;
using Content.Server.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Body.Components;
using Content.Shared.Chemistry.Events;
using Content.Shared.Inventory;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Tag;
using Content.Shared.Weapons.Melee.Events;
using Robust.Shared.Collections;
using Robust.Shared.Prototypes;
namespace Content.Server.Chemistry.EntitySystems;
/// <summary>
/// System for handling the different inheritors of <see cref="BaseSolutionInjectOnEventComponent"/>.
/// Subscribes to relevent events and performs solution injections when they are raised.
/// </summary>
public sealed class SolutionInjectOnCollideSystem : EntitySystem
{
[Dependency] private readonly BloodstreamSystem _bloodstream = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
[Dependency] private readonly TagSystem _tag = default!;
private static readonly ProtoId<TagPrototype> HardsuitTag = "Hardsuit";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SolutionInjectOnProjectileHitComponent, ProjectileHitEvent>(HandleProjectileHit);
SubscribeLocalEvent<SolutionInjectOnEmbedComponent, EmbedEvent>(HandleEmbed);
SubscribeLocalEvent<MeleeChemicalInjectorComponent, MeleeHitEvent>(HandleMeleeHit);
SubscribeLocalEvent<SolutionInjectWhileEmbeddedComponent, InjectOverTimeEvent>(OnInjectOverTime);
}
private void HandleProjectileHit(Entity<SolutionInjectOnProjectileHitComponent> entity, ref ProjectileHitEvent args)
{
DoInjection((entity.Owner, entity.Comp), args.Target, args.Shooter);
}
private void HandleEmbed(Entity<SolutionInjectOnEmbedComponent> entity, ref EmbedEvent args)
{
DoInjection((entity.Owner, entity.Comp), args.Embedded, args.Shooter);
}
private void HandleMeleeHit(Entity<MeleeChemicalInjectorComponent> entity, ref MeleeHitEvent args)
{
// MeleeHitEvent is weird, so we have to filter to make sure we actually
// hit something and aren't just examining the weapon.
if (args.IsHit)
TryInjectTargets((entity.Owner, entity.Comp), args.HitEntities, args.User);
}
private void OnInjectOverTime(Entity<SolutionInjectWhileEmbeddedComponent> entity, ref InjectOverTimeEvent args)
{
DoInjection((entity.Owner, entity.Comp), args.EmbeddedIntoUid);
}
private void DoInjection(Entity<BaseSolutionInjectOnEventComponent> injectorEntity, EntityUid target, EntityUid? source = null)
{
TryInjectTargets(injectorEntity, [target], source);
}
/// <summary>
/// Filters <paramref name="targets"/> for valid targets and tries to inject a portion of <see cref="BaseSolutionInjectOnEventComponent.Solution"/> into
/// each valid target's bloodstream.
/// </summary>
/// <remarks>
/// Targets are invalid if any of the following are true:
/// <list type="bullet">
/// <item>The target does not have a bloodstream.</item>
/// <item><see cref="BaseSolutionInjectOnEventComponent.PierceArmor"/> is false and the target is wearing a hardsuit.</item>
/// <item><see cref="BaseSolutionInjectOnEventComponent.BlockSlots"/> is not NONE and the target has an item equipped in any of the specified slots.</item>
/// </list>
/// </remarks>
/// <returns>true if at least one target was successfully injected, otherwise false</returns>
private bool TryInjectTargets(Entity<BaseSolutionInjectOnEventComponent> injector, IReadOnlyList<EntityUid> targets, EntityUid? source = null)
{
// Make sure we have at least one target
if (targets.Count == 0)
return false;
// Get the solution to inject
if (!_solutionContainer.TryGetSolution(injector.Owner, injector.Comp.Solution, out var injectorSolution))
return false;
// Build a list of bloodstreams to inject into
var targetBloodstreams = new ValueList<Entity<BloodstreamComponent>>();
foreach (var target in targets)
{
if (Deleted(target))
continue;
// Yuck, this is way to hardcodey for my tastes
// TODO blocking injection with a hardsuit should probably done with a cancellable event or something
if (!injector.Comp.PierceArmor && _inventory.TryGetSlotEntity(target, "outerClothing", out var suit) && _tag.HasTag(suit.Value, HardsuitTag))
{
// Only show popup to attacker
if (source != null)
_popup.PopupEntity(Loc.GetString(injector.Comp.BlockedByHardsuitPopupMessage, ("weapon", injector.Owner), ("target", target)), target, source.Value, PopupType.SmallCaution);
continue;
}
// Check if the target has anything equipped in a slot that would block injection
if (injector.Comp.BlockSlots != SlotFlags.NONE)
{
var blocked = false;
var containerEnumerator = _inventory.GetSlotEnumerator(target, injector.Comp.BlockSlots);
while (containerEnumerator.MoveNext(out var container))
{
if (container.ContainedEntity != null)
{
blocked = true;
break;
}
}
if (blocked)
continue;
}
// Make sure the target has a bloodstream
if (!TryComp<BloodstreamComponent>(target, out var bloodstream))
continue;
// Checks passed; add this target's bloodstream to the list
targetBloodstreams.Add((target, bloodstream));
}
// Make sure we got at least one bloodstream
if (targetBloodstreams.Count == 0)
return false;
// Extract total needed solution from the injector
var removedSolution = _solutionContainer.SplitSolution(injectorSolution.Value, injector.Comp.TransferAmount * targetBloodstreams.Count);
// Adjust solution amount based on transfer efficiency
var solutionToInject = removedSolution.SplitSolution(removedSolution.Volume * injector.Comp.TransferEfficiency);
// Calculate how much of the adjusted solution each target will get
var volumePerBloodstream = solutionToInject.Volume * (1f / targetBloodstreams.Count);
var anySuccess = false;
foreach (var targetBloodstream in targetBloodstreams)
{
// Take our portion of the adjusted solution for this target
var individualInjection = solutionToInject.SplitSolution(volumePerBloodstream);
// Inject our portion into the target's bloodstream
if (_bloodstream.TryAddToBloodstream(targetBloodstream.AsNullable(), individualInjection))
anySuccess = true;
}
// Huzzah!
return anySuccess;
}
}