Files
wylab-station-14/Content.Shared/Rootable/RootableSystem.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 de26fd1c0d99f3019fe7dd1451a50230cc90f058.

* Revert "removed blood gain from protein"

This reverts commit 7a1648caf39fe26406db73c2a5afa389b82c612f.

* 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 0a5313a68dd6484d36d28d08930c76851b72ae38.

* simplify reagent removal

* enabled foreign blood transfusion

* Revert "COMMIT"

This reverts commit 19abd679cd7761ebd47bb242bd644176a3006a42.

* 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

259 lines
9.8 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Actions.Components;
using Content.Shared.Administration.Logs;
using Content.Shared.Alert;
using Content.Shared.Body.Components;
using Content.Shared.Body.Systems;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Cloning.Events;
using Content.Shared.Coordinates;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using Content.Shared.Fluids.Components;
using Content.Shared.Gravity;
using Content.Shared.Mobs;
using Content.Shared.Movement.Systems;
using Content.Shared.Slippery;
using Content.Shared.Toggleable;
using Content.Shared.Trigger.Components.Effects;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
namespace Content.Shared.Rootable;
/// <summary>
/// Adds an action to toggle rooting to the ground, primarily for the Diona species.
/// Being rooted prevents weighlessness and slipping, but causes any floor contents to transfer its reagents to the bloodstream.
/// </summary>
public sealed class RootableSystem : EntitySystem
{
[Dependency] private readonly AlertsSystem _alerts = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ISharedAdminLogManager _logger = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!;
[Dependency] private readonly ReactiveSystem _reactive = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedBloodstreamSystem _blood = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
private EntityQuery<PuddleComponent> _puddleQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
public override void Initialize()
{
base.Initialize();
_puddleQuery = GetEntityQuery<PuddleComponent>();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
SubscribeLocalEvent<RootableComponent, MapInitEvent>(OnRootableMapInit);
SubscribeLocalEvent<RootableComponent, ComponentShutdown>(OnRootableShutdown);
SubscribeLocalEvent<RootableComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<RootableComponent, EndCollideEvent>(OnEndCollide);
SubscribeLocalEvent<RootableComponent, ToggleActionEvent>(OnRootableToggle);
SubscribeLocalEvent<RootableComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<RootableComponent, IsWeightlessEvent>(OnIsWeightless);
SubscribeLocalEvent<RootableComponent, SlipAttemptEvent>(OnSlipAttempt);
SubscribeLocalEvent<RootableComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovementSpeed);
SubscribeLocalEvent<RootableComponent, CloningEvent>(OnCloning);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<RootableComponent, BloodstreamComponent>();
var curTime = _timing.CurTime;
while (query.MoveNext(out var uid, out var rooted, out var bloodstream))
{
if (!rooted.Rooted || rooted.PuddleEntity == null || curTime < rooted.NextUpdate || !_puddleQuery.TryComp(rooted.PuddleEntity, out var puddleComp))
continue;
rooted.NextUpdate += rooted.TransferFrequency;
Dirty(uid, rooted);
PuddleReact((uid, rooted, bloodstream), (rooted.PuddleEntity.Value, puddleComp!));
}
}
/// <summary>
/// Determines if the puddle is set up properly and if so, moves on to reacting.
/// </summary>
private void PuddleReact(Entity<RootableComponent, BloodstreamComponent> ent, Entity<PuddleComponent> puddleEntity)
{
if (!_solutionContainer.ResolveSolution(puddleEntity.Owner, puddleEntity.Comp.SolutionName, ref puddleEntity.Comp.Solution, out var solution) ||
solution.Contents.Count == 0)
{
return;
}
ReactWithEntity(ent, puddleEntity, solution);
}
/// <summary>
/// Attempt to transfer an amount of the solution to the ent's bloodstream.
/// </summary>
private void ReactWithEntity(Entity<RootableComponent, BloodstreamComponent> ent, Entity<PuddleComponent> puddleEntity, Solution solution)
{
if (!_solutionContainer.ResolveSolution(ent.Owner, ent.Comp2.BloodSolutionName, ref ent.Comp2.BloodSolution, out var bloodSolution) || bloodSolution.AvailableVolume <= 0)
return;
var availableTransfer = FixedPoint2.Min(solution.Volume, ent.Comp1.TransferRate);
var transferAmount = FixedPoint2.Min(availableTransfer, bloodSolution.AvailableVolume);
var transferSolution = _solutionContainer.SplitSolution(puddleEntity.Comp.Solution!.Value, transferAmount);
_reactive.DoEntityReaction(ent, transferSolution, ReactionMethod.Ingestion);
// Log solution addition by puddle.
if (_blood.TryAddToBloodstream((ent, ent.Comp2), transferSolution))
_logger.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(ent):target} absorbed puddle {SharedSolutionContainerSystem.ToPrettyString(transferSolution)}");
}
private void OnCloning(Entity<RootableComponent> ent, ref CloningEvent args)
{
if (!args.Settings.EventComponents.Contains(Factory.GetRegistration(ent.Comp.GetType()).Name))
return;
var cloneComp = EnsureComp<RootableComponent>(args.CloneUid);
cloneComp.TransferRate = ent.Comp.TransferRate;
cloneComp.TransferFrequency = ent.Comp.TransferFrequency;
cloneComp.SpeedModifier = ent.Comp.SpeedModifier;
cloneComp.RootSound = ent.Comp.RootSound;
Dirty(args.CloneUid, cloneComp);
}
private void OnRootableMapInit(Entity<RootableComponent> ent, ref MapInitEvent args)
{
if (!TryComp(ent, out ActionsComponent? comp))
return;
ent.Comp.NextUpdate = _timing.CurTime;
Dirty(ent);
_actions.AddAction(ent, ref ent.Comp.ActionEntity, ent.Comp.Action, component: comp);
}
private void OnRootableShutdown(Entity<RootableComponent> ent, ref ComponentShutdown args)
{
if (!TryComp(ent, out ActionsComponent? comp))
return;
var actions = new Entity<ActionsComponent?>(ent, comp);
_actions.RemoveAction(actions, ent.Comp.ActionEntity);
_alerts.ClearAlert(ent.Owner, ent.Comp.RootedAlert);
}
private void OnRootableToggle(Entity<RootableComponent> ent, ref ToggleActionEvent args)
{
args.Handled = TryToggleRooting((ent, ent));
}
private void OnMobStateChanged(Entity<RootableComponent> ent, ref MobStateChangedEvent args)
{
if (ent.Comp.Rooted)
TryToggleRooting((ent, ent));
}
public bool TryToggleRooting(Entity<RootableComponent?> ent)
{
if (!Resolve(ent, ref ent.Comp))
return false;
ent.Comp.Rooted = !ent.Comp.Rooted;
_movementSpeedModifier.RefreshMovementSpeedModifiers(ent);
_gravity.RefreshWeightless(ent.Owner);
if (ent.Comp.Rooted)
{
_alerts.ShowAlert(ent.Owner, ent.Comp.RootedAlert);
var curTime = _timing.CurTime;
if (curTime > ent.Comp.NextUpdate)
ent.Comp.NextUpdate = curTime;
}
else
{
_alerts.ClearAlert(ent.Owner, ent.Comp.RootedAlert);
}
_audio.PlayPredicted(ent.Comp.RootSound, ent.Owner.ToCoordinates(), ent);
Dirty(ent);
return true;
}
private void OnIsWeightless(Entity<RootableComponent> ent, ref IsWeightlessEvent args)
{
if (args.Handled || !ent.Comp.Rooted)
return;
// Do not cancel weightlessness if the person is in off-grid.
if (!_gravity.EntityOnGravitySupportingGridOrMap(ent.Owner))
return;
args.IsWeightless = false;
args.Handled = true;
}
private void OnSlipAttempt(Entity<RootableComponent> ent, ref SlipAttemptEvent args)
{
if (!ent.Comp.Rooted)
return;
if (args.SlipCausingEntity != null && HasComp<DamageOnTriggerComponent>(args.SlipCausingEntity))
return;
args.NoSlip = true;
}
private void OnStartCollide(Entity<RootableComponent> ent, ref StartCollideEvent args)
{
if (!_puddleQuery.HasComp(args.OtherEntity))
return;
ent.Comp.PuddleEntity = args.OtherEntity;
if (ent.Comp.NextUpdate < _timing.CurTime) // To prevent constantly moving to new puddles resetting the timer.
ent.Comp.NextUpdate = _timing.CurTime;
Dirty(ent);
}
private void OnEndCollide(Entity<RootableComponent> ent, ref EndCollideEvent args)
{
if (ent.Comp.PuddleEntity != args.OtherEntity)
return;
var exists = Exists(args.OtherEntity);
if (!_physicsQuery.TryComp(ent, out var body))
return;
foreach (var entContact in _physics.GetContactingEntities(ent, body))
{
if (exists && entContact == args.OtherEntity)
continue;
if (!_puddleQuery.HasComponent(entContact))
continue;
ent.Comp.PuddleEntity = ent;
return; // New puddle found, no need to continue.
}
ent.Comp.PuddleEntity = null;
Dirty(ent);
}
private void OnRefreshMovementSpeed(Entity<RootableComponent> ent, ref RefreshMovementSpeedModifiersEvent args)
{
if (ent.Comp.Rooted)
args.ModifySpeed(ent.Comp.SpeedModifier);
}
}