Mopping ECS (No BucketComponents allowed!) (#6718)

Co-authored-by: mirrorcult <lunarautomaton6@gmail.com>
This commit is contained in:
Willhelm53
2022-03-14 16:02:26 -05:00
committed by GitHub
parent 84225dde3d
commit 65178bccc4
10 changed files with 449 additions and 332 deletions

View File

@@ -0,0 +1,54 @@
using Content.Server.Fluids.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.Sound;
namespace Content.Server.Fluids.Components;
/// <summary>
/// For entities that can clean up puddles
/// </summary>
[RegisterComponent, Friend(typeof(MoppingSystem))]
public sealed class AbsorbentComponent : Component
{
public const string SolutionName = "absorbed";
[DataField("pickupAmount")]
public FixedPoint2 PickupAmount = FixedPoint2.New(10);
/// <summary>
/// When using this tool on an empty floor tile, leave this much reagent as a new puddle.
/// </summary>
[DataField("residueAmount")]
public FixedPoint2 ResidueAmount = FixedPoint2.New(10); // Should be higher than MopLowerLimit
/// <summary>
/// To leave behind a wet floor, this tool will be unable to take from puddles with a volume less than this amount.
/// </summary>
[DataField("mopLowerLimit")]
public FixedPoint2 MopLowerLimit = FixedPoint2.New(5);
[DataField("pickupSound")]
public SoundSpecifier PickupSound = new SoundPathSpecifier("/Audio/Effects/Fluids/slosh.ogg");
[DataField("transferSound")]
public SoundSpecifier TransferSound = new SoundPathSpecifier("/Audio/Effects/Fluids/watersplash.ogg");
/// <summary>
/// Multiplier for the do_after delay for how quickly the mopping happens.
/// </summary>
[ViewVariables]
[DataField("mopSpeed")] public float MopSpeed = 1;
/// <summary>
/// How many entities can this tool interact with at once?
/// </summary>
[DataField("maxEntities")]
public int MaxInteractingEntities = 1;
/// <summary>
/// What entities is this tool interacting with right now?
/// </summary>
[ViewVariables]
public HashSet<EntityUid> InteractingEntities = new();
}

View File

@@ -1,141 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.DoAfter;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Popups;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Fluids.Components
{
/// <summary>
/// Can a mop click on this entity and dump its fluids
/// </summary>
[RegisterComponent]
public sealed class BucketComponent : Component, IInteractUsing
{
[Dependency] private readonly IEntityManager _entMan = default!;
public const string SolutionName = "bucket";
private List<EntityUid> _currentlyUsing = new();
public FixedPoint2 MaxVolume
{
get =>
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution)
? solution.MaxVolume
: FixedPoint2.Zero;
set
{
if (EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution))
{
solution.MaxVolume = value;
}
}
}
public FixedPoint2 CurrentVolume => EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution)
? solution.CurrentVolume
: FixedPoint2.Zero;
[DataField("sound")]
private SoundSpecifier _sound = new SoundPathSpecifier("/Audio/Effects/Fluids/watersplash.ogg");
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
var solutionsSys = EntitySystem.Get<SolutionContainerSystem>();
if (!solutionsSys.TryGetSolution(Owner, SolutionName, out var contents) ||
_currentlyUsing.Contains(eventArgs.Using) ||
!_entMan.TryGetComponent(eventArgs.Using, out MopComponent? mopComponent) ||
mopComponent.Mopping)
{
return false;
}
if (CurrentVolume <= 0)
{
Owner.PopupMessage(eventArgs.User, Loc.GetString("bucket-component-bucket-is-empty-message"));
return false;
}
_currentlyUsing.Add(eventArgs.Using);
// IMO let em move while doing it.
var doAfterArgs = new DoAfterEventArgs(eventArgs.User, 1.0f, target: eventArgs.Target)
{
BreakOnStun = true,
BreakOnDamage = true,
};
var result = await EntitySystem.Get<DoAfterSystem>().WaitDoAfter(doAfterArgs);
_currentlyUsing.Remove(eventArgs.Using);
if (result == DoAfterStatus.Cancelled || _entMan.Deleted(Owner) || mopComponent.Deleted ||
CurrentVolume <= 0 || !EntitySystem.Get<SharedInteractionSystem>().InRangeUnobstructed(Owner, mopComponent.Owner))
return false;
//Checks if the mop is empty
if(mopComponent.CurrentVolume == 0)
{
// Transfers up to half the mop's available capacity to the mop
// Takes the lower of the mop's available volume and the bucket's current volume.
var transferAmount = FixedPoint2.Min(0.5*mopComponent.AvailableVolume, CurrentVolume);
if (transferAmount == 0)
{
return false;
}
var mopContents = mopComponent.MopSolution;
if (mopContents == null)
{
return false;
}
// Transfer solution from the bucket to the mop
// Owner is the bucket being interacted with. contents is the Solution contained by said bucket.
var solution = solutionsSys.SplitSolution(Owner, contents, transferAmount);
if (!solutionsSys.TryAddSolution(mopComponent.Owner, mopComponent.MopSolution, solution))
{
return false; //if the attempt fails
}
Owner.PopupMessage(eventArgs.User, Loc.GetString("bucket-component-mop-is-now-wet-message"));
}
else //if mop is not empty
{
//Transfer the mop solution to the bucket
if (mopComponent.MopSolution == null)
return false;
var solutionFromMop = solutionsSys.SplitSolution(mopComponent.Owner, mopComponent.MopSolution, mopComponent.CurrentVolume);
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution);
if (!solutionsSys.TryAddSolution(Owner, solution, solutionFromMop))
{
return false; //if the attempt fails
}
Owner.PopupMessage(eventArgs.User, Loc.GetString("bucket-component-mop-is-now-dry-message"));
}
SoundSystem.Play(Filter.Pvs(Owner), _sound.GetSound(), Owner);
return true;
}
}
}

View File

@@ -1,182 +0,0 @@
using System.Threading.Tasks;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.DoAfter;
using Content.Server.Fluids.EntitySystems;
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Popups;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Fluids.Components
{
/// <summary>
/// For cleaning up puddles
/// </summary>
[RegisterComponent]
public sealed class MopComponent : Component, IAfterInteract
{
[Dependency] private readonly IEntityManager _entities = default!;
public const string SolutionName = "mop";
/// <summary>
/// Used to prevent do_after spam if we're currently mopping.
/// </summary>
public bool Mopping { get; private set; }
// MopSolution Object stores whatever solution the mop has absorbed.
public Solution? MopSolution
{
get
{
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution);
return solution;
}
}
// MaxVolume is the Maximum volume the mop can absorb (however, this is defined in janitor.yml)
public FixedPoint2 MaxVolume
{
get => MopSolution?.MaxVolume ?? FixedPoint2.Zero;
set
{
var solution = MopSolution;
if (solution != null)
{
solution.MaxVolume = value;
}
}
}
// CurrentVolume is the volume the mop has absorbed.
public FixedPoint2 CurrentVolume => MopSolution?.CurrentVolume ?? FixedPoint2.Zero;
// AvailableVolume is the remaining volume capacity of the mop.
public FixedPoint2 AvailableVolume => MopSolution?.AvailableVolume ?? FixedPoint2.Zero;
// Currently there's a separate amount for pickup and dropoff so
// Picking up a puddle requires multiple clicks
// Dumping in a bucket requires 1 click
// Long-term you'd probably use a cooldown and start the pickup once we have some form of global cooldown
[DataField("pickup_amount")]
public FixedPoint2 PickupAmount { get; } = FixedPoint2.New(10);
/// <summary>
/// When using the mop on an empty floor tile, leave this much reagent as a new puddle.
/// </summary>
[DataField("residueAmount")]
public FixedPoint2 ResidueAmount { get; } = FixedPoint2.New(10); // Should be higher than MopLowerLimit
/// <summary>
/// To leave behind a wet floor, the mop will be unable to take from puddles with a volume less than this amount.
/// </summary>
[DataField("mopLowerLimit")]
public FixedPoint2 MopLowerLimit { get; } = FixedPoint2.New(5);
[DataField("pickup_sound")]
private SoundSpecifier _pickupSound = new SoundPathSpecifier("/Audio/Effects/Fluids/slosh.ogg");
/// <summary>
/// Multiplier for the do_after delay for how fast the mop works.
/// </summary>
[ViewVariables]
[DataField("speed")] private float _mopSpeed = 1;
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
/*
* Functionality:
* Essentially if we click on an empty tile spill our contents there
* Otherwise, try to mop up the puddle (if it is a puddle).
* It will try to destroy solution on the mop to do so, and if it is successful
* will spill some of the mop's solution onto the puddle which will evaporate eventually.
*/
var solutionSystem = EntitySystem.Get<SolutionContainerSystem>();
var spillableSystem = EntitySystem.Get<SpillableSystem>();
if (!eventArgs.CanReach ||
!solutionSystem.TryGetSolution(Owner, SolutionName, out var contents ) ||
Mopping)
{
return false;
}
if (eventArgs.Target is not {Valid: true} target)
{
// Drop the liquid on the mop on to the ground
var solution = solutionSystem.SplitSolution(Owner, contents, FixedPoint2.Min(ResidueAmount, CurrentVolume));
spillableSystem.SpillAt(solution, eventArgs.ClickLocation, "PuddleSmear");
return true;
}
if (!_entities.TryGetComponent(target, out PuddleComponent? puddleComponent) ||
!solutionSystem.TryGetSolution((puddleComponent).Owner, puddleComponent.SolutionName, out var puddleSolution))
return false;
// if the puddle is too small for the mop to effectively take any more solution
if (puddleSolution.TotalVolume <= MopLowerLimit)
{
// Transfers solution from the mop to the puddle
solutionSystem.TryAddSolution(target, puddleSolution, solutionSystem.SplitSolution(Owner, contents, FixedPoint2.Min(ResidueAmount,CurrentVolume)));
return true;
}
// if the mop is full
if(AvailableVolume <= 0)
{
Owner.PopupMessage(eventArgs.User, Loc.GetString("mop-component-mop-is-full-message"));
return false;
}
// Mopping duration (aka delay) should scale with PickupAmount and not puddle volume, because we are picking up a constant volume of solution with each click.
var doAfterArgs = new DoAfterEventArgs(eventArgs.User, _mopSpeed * PickupAmount.Float() / 10.0f,
target: target)
{
BreakOnUserMove = true,
BreakOnStun = true,
BreakOnDamage = true,
};
Mopping = true;
var result = await EntitySystem.Get<DoAfterSystem>().WaitDoAfter(doAfterArgs);
Mopping = false;
if (result == DoAfterStatus.Cancelled ||
_entities.Deleted(Owner) ||
puddleComponent.Deleted)
return false;
// The volume the mop will take from the puddle
FixedPoint2 transferAmount;
// does the puddle actually have reagents? it might not if its a weird cosmetic entity.
if (puddleSolution.TotalVolume == 0)
transferAmount = FixedPoint2.Min(PickupAmount, AvailableVolume);
else
{
transferAmount = FixedPoint2.Min(PickupAmount, puddleSolution.TotalVolume, AvailableVolume);
if ((puddleSolution.TotalVolume - transferAmount) < MopLowerLimit) // If the transferAmount would bring the puddle below the MopLowerLimit
transferAmount = puddleSolution.TotalVolume - MopLowerLimit; // Then the transferAmount should bring the puddle down to the MopLowerLimit exactly
}
// Transfers solution from the puddle to the mop
solutionSystem.TryAddSolution(Owner, contents, solutionSystem.SplitSolution(target, puddleSolution, transferAmount));
SoundSystem.Play(Filter.Pvs(Owner), _pickupSound.GetSound(), Owner);
// if the mop became full after that puddle, let the player know.
if(AvailableVolume <= 0)
Owner.PopupMessage(eventArgs.User, Loc.GetString("mop-component-mop-is-now-full-message"));
return true;
}
}
}