Consistent Absorbent System behavior (#22723)

This commit is contained in:
LordCarve
2023-12-22 20:02:09 +01:00
committed by GitHub
parent b6bd82caa6
commit 524dbf9a78
2 changed files with 471 additions and 77 deletions

View File

@@ -8,7 +8,6 @@ using Content.Shared.Interaction;
using Content.Shared.Timing;
using Content.Shared.Weapons.Melee;
using Robust.Server.Audio;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
@@ -27,7 +26,6 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SolutionContainerSystem _solutionSystem = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
public override void Initialize()
{
@@ -81,7 +79,7 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
if (component.Progress.Equals(oldProgress))
return;
Dirty(component);
Dirty(uid, component);
}
private void OnInteractNoHand(EntityUid uid, AbsorbentComponent component, InteractNoHandEvent args)
@@ -102,29 +100,27 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
args.Handled = true;
}
private void Mop(EntityUid user, EntityUid target, EntityUid used, AbsorbentComponent component)
public void Mop(EntityUid user, EntityUid target, EntityUid used, AbsorbentComponent component)
{
if (!_solutionSystem.TryGetSolution(used, AbsorbentComponent.SolutionName, out var absorberSoln))
if (!_solutionSystem.TryGetSolution(used, AbsorbentComponent.SolutionName, out var absorbentSolution))
return;
if (_useDelay.ActiveDelay(used))
return;
// If it's a puddle try to grab from
if (!TryPuddleInteract(user, used, target, component, absorberSoln))
if (!TryPuddleInteract(user, used, target, component, absorbentSolution))
{
// Do a transfer, try to get water onto us and transfer anything else to them.
// If it's anything else transfer to
if (!TryTransferAbsorber(user, used, target, component, absorberSoln))
// If it's refillable try to transfer
if (!TryRefillableInteract(user, used, target, component, absorbentSolution))
return;
}
}
/// <summary>
/// Attempt to fill an absorber from some refillable solution.
/// Logic for an absorbing entity interacting with a refillable.
/// </summary>
private bool TryTransferAbsorber(EntityUid user, EntityUid used, EntityUid target, AbsorbentComponent component, Solution absorberSoln)
private bool TryRefillableInteract(EntityUid user, EntityUid used, EntityUid target, AbsorbentComponent component, Solution absorbentSolution)
{
if (!TryComp(target, out RefillableSolutionComponent? refillable))
return false;
@@ -134,74 +130,15 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
if (refillableSolution.Volume <= 0)
{
var msg = Loc.GetString("mopping-system-target-container-empty", ("target", target));
_popups.PopupEntity(msg, user, user);
return false;
}
// Remove the non-water reagents.
// Remove water on target
// Then do the transfer.
var nonWater = absorberSoln.SplitSolutionWithout(component.PickupAmount, PuddleSystem.EvaporationReagents);
_solutionContainerSystem.UpdateChemicals(used, absorberSoln, true);
if (nonWater.Volume == FixedPoint2.Zero && absorberSoln.AvailableVolume == FixedPoint2.Zero)
{
_popups.PopupEntity(Loc.GetString("mopping-system-puddle-space", ("used", used)), user, user);
return false;
}
var transferAmount = component.PickupAmount < absorberSoln.AvailableVolume ?
component.PickupAmount :
absorberSoln.AvailableVolume;
var water = refillableSolution.SplitSolutionWithOnly(transferAmount, PuddleSystem.EvaporationReagents);
_solutionContainerSystem.UpdateChemicals(target, refillableSolution);
if (water.Volume == FixedPoint2.Zero && nonWater.Volume == FixedPoint2.Zero)
{
_popups.PopupEntity(Loc.GetString("mopping-system-target-container-empty-water", ("target", target)), user, user);
return false;
}
if (water.Volume > 0 && !_solutionContainerSystem.TryAddSolution(used, absorberSoln, water))
{
_popups.PopupEntity(Loc.GetString("mopping-system-full", ("used", used)), used, user);
}
// Attempt to transfer the full nonWater solution to the bucket.
if (nonWater.Volume > 0)
{
bool fullTransferSuccess = _solutionContainerSystem.TryAddSolution(target, refillableSolution, nonWater);
// If full transfer was unsuccessful, try a partial transfer.
if (!fullTransferSuccess)
{
var partiallyTransferSolution = nonWater.SplitSolution(refillableSolution.AvailableVolume);
// Try to transfer the split solution to the bucket.
if (_solutionContainerSystem.TryAddSolution(target, refillableSolution, partiallyTransferSolution))
{
// The transfer was successful. nonWater now contains the amount that wasn't transferred.
// If there's any leftover nonWater solution, add it back to the mop.
if (nonWater.Volume > 0)
{
absorberSoln.AddSolution(nonWater, _prototype);
_solutionContainerSystem.UpdateChemicals(used, absorberSoln);
}
}
else
{
// If the transfer was unsuccessful, combine both solutions and return them to the mop.
nonWater.AddSolution(partiallyTransferSolution, _prototype);
absorberSoln.AddSolution(nonWater, _prototype);
_solutionContainerSystem.UpdateChemicals(used, absorberSoln);
}
}
// Target empty - only transfer absorbent contents into refillable
if (!TryTransferFromAbsorbentToRefillable(user, used, target, component, absorbentSolution, refillableSolution))
return false;
}
else
{
_popups.PopupEntity(Loc.GetString("mopping-system-full", ("used", target)), user, user);
// Target non-empty - do a two-way transfer
if (!TryTwoWayAbsorbentRefillableTransfer(user, used, target, component, absorbentSolution, refillableSolution))
return false;
}
_audio.PlayPvs(component.TransferSound, target);
@@ -209,6 +146,119 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
return true;
}
/// <summary>
/// Logic for an transferring solution from absorber to an empty refillable.
/// </summary>
private bool TryTransferFromAbsorbentToRefillable(
EntityUid user,
EntityUid used,
EntityUid target,
AbsorbentComponent component,
Solution absorbentSolution,
Solution refillableSolution)
{
if (absorbentSolution.Volume <= 0)
{
_popups.PopupEntity(Loc.GetString("mopping-system-target-container-empty", ("target", target)), user, user);
return false;
}
var transferAmount = component.PickupAmount < refillableSolution.AvailableVolume ?
component.PickupAmount :
refillableSolution.AvailableVolume;
if (transferAmount <= 0)
{
_popups.PopupEntity(Loc.GetString("mopping-system-full", ("used", used)), used, user);
return false;
}
// Prioritize transferring non-evaporatives if absorbent has any
var contaminants = absorbentSolution.SplitSolutionWithout(transferAmount, PuddleSystem.EvaporationReagents);
if (contaminants.Volume > 0)
{
_solutionSystem.UpdateChemicals(used, absorbentSolution, true);
_solutionSystem.TryAddSolution(target, refillableSolution, contaminants);
}
else
{
var evaporatives = absorbentSolution.SplitSolution(transferAmount);
_solutionSystem.UpdateChemicals(used, absorbentSolution, true);
_solutionSystem.TryAddSolution(target, refillableSolution, evaporatives);
}
return true;
}
/// <summary>
/// Logic for an transferring contaminants to a non-empty refillable & reabsorbing water if any available.
/// </summary>
private bool TryTwoWayAbsorbentRefillableTransfer(
EntityUid user,
EntityUid used,
EntityUid target,
AbsorbentComponent component,
Solution absorbentSolution,
Solution refillableSolution)
{
var contaminantsFromAbsorbent = absorbentSolution.SplitSolutionWithout(component.PickupAmount, PuddleSystem.EvaporationReagents);
_solutionSystem.UpdateChemicals(used, absorbentSolution, true);
if (contaminantsFromAbsorbent.Volume == FixedPoint2.Zero && absorbentSolution.AvailableVolume == FixedPoint2.Zero)
{
// Nothing to transfer to refillable and no room to absorb anything extra
_popups.PopupEntity(Loc.GetString("mopping-system-puddle-space", ("used", used)), user, user);
// We can return cleanly because nothing was split from absorbent solution
return false;
}
var waterPulled = component.PickupAmount < absorbentSolution.AvailableVolume ?
component.PickupAmount :
absorbentSolution.AvailableVolume;
var waterFromRefillable = refillableSolution.SplitSolutionWithOnly(waterPulled, PuddleSystem.EvaporationReagents);
_solutionSystem.UpdateChemicals(target, refillableSolution);
if (waterFromRefillable.Volume == FixedPoint2.Zero && contaminantsFromAbsorbent.Volume == FixedPoint2.Zero)
{
// Nothing to transfer in either direction
_popups.PopupEntity(Loc.GetString("mopping-system-target-container-empty-water", ("target", target)), user, user);
// We can return cleanly because nothing was split from refillable solution
return false;
}
var anyTransferOccurred = false;
if (waterFromRefillable.Volume > FixedPoint2.Zero)
{
// transfer water to absorbent
_solutionSystem.TryAddSolution(used, absorbentSolution, waterFromRefillable);
anyTransferOccurred = true;
}
if (contaminantsFromAbsorbent.Volume > 0)
{
if (refillableSolution.AvailableVolume <= 0)
{
_popups.PopupEntity(Loc.GetString("mopping-system-full", ("used", target)), user, user);
}
else
{
// transfer as much contaminants to refillable as will fit
var contaminantsForRefillable = contaminantsFromAbsorbent.SplitSolution(refillableSolution.AvailableVolume);
_solutionSystem.TryAddSolution(target, refillableSolution, contaminantsForRefillable);
anyTransferOccurred = true;
}
// absorb everything that did not fit in the refillable back by the absorbent
_solutionSystem.TryAddSolution(used, absorbentSolution, contaminantsFromAbsorbent);
}
return anyTransferOccurred;
}
/// <summary>
/// Logic for an absorbing entity interacting with a puddle.
/// </summary>