From 1de82b4324c8e28529400d7761c11f0962cf6fac Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Wed, 24 Nov 2021 06:14:46 +1300 Subject: [PATCH] Fix spill crash (#5480) * fix spill * clean up mop component * remove unecessary code --- .../SpillIfPuddlePresentTileReaction.cs | 2 +- .../Fluids/Components/MopComponent.cs | 94 ++++++++----------- .../Fluids/Components/SpillExtensions.cs | 56 +++++------ 3 files changed, 70 insertions(+), 82 deletions(-) diff --git a/Content.Server/Chemistry/TileReactions/SpillIfPuddlePresentTileReaction.cs b/Content.Server/Chemistry/TileReactions/SpillIfPuddlePresentTileReaction.cs index 5a55b264eb..19fe6ff906 100644 --- a/Content.Server/Chemistry/TileReactions/SpillIfPuddlePresentTileReaction.cs +++ b/Content.Server/Chemistry/TileReactions/SpillIfPuddlePresentTileReaction.cs @@ -17,7 +17,7 @@ namespace Content.Server.Chemistry.TileReactions { if (reactVolume < 5 || !tile.TryGetPuddle(null, out _)) return FixedPoint2.Zero; - return tile.SpillAt(new Solution(reagent.ID, reactVolume), "PuddleSmear", true, false) != null ? reactVolume : FixedPoint2.Zero; + return tile.SpillAt(new Solution(reagent.ID, reactVolume), "PuddleSmear", true, false, true) != null ? reactVolume : FixedPoint2.Zero; } } } diff --git a/Content.Server/Fluids/Components/MopComponent.cs b/Content.Server/Fluids/Components/MopComponent.cs index 36f1eedb79..508e1a42d8 100644 --- a/Content.Server/Fluids/Components/MopComponent.cs +++ b/Content.Server/Fluids/Components/MopComponent.cs @@ -61,7 +61,13 @@ namespace Content.Server.Fluids.Components // 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(5); + public FixedPoint2 PickupAmount { get; } = FixedPoint2.New(10); + + /// + /// After cleaning a floor tile, leave this much reagent as a puddle. I.e., leave behind a wet floor. + /// + [DataField("residueAmount")] + public FixedPoint2 ResidueAmount { get; } = FixedPoint2.New(5); [DataField("pickup_sound")] private SoundSpecifier _pickupSound = new SoundPathSpecifier("/Audio/Effects/Fluids/slosh.ogg"); @@ -81,54 +87,43 @@ namespace Content.Server.Fluids.Components * 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(); - if (!EntitySystem.Get().TryGetSolution(Owner.Uid, SolutionName, out var contents ) || + if (!solutionSystem.TryGetSolution(Owner.Uid, SolutionName, out var contents ) || Mopping || !eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true)) { return false; } - var currentVolume = CurrentVolume; - - if (eventArgs.Target == null) - { - if (currentVolume > 0) - { - // Drop the liquid on the mop on to the ground - EntitySystem.Get().SplitSolution(Owner.Uid, contents, CurrentVolume) - .SpillAt(eventArgs.ClickLocation, "PuddleSmear"); - return true; - } - - return false; - } - - if (!eventArgs.Target.TryGetComponent(out PuddleComponent? puddleComponent)) - { - return false; - } - - var puddleVolume = puddleComponent.CurrentVolume; - - if (currentVolume <= 0) + if (CurrentVolume <= 0) { Owner.PopupMessage(eventArgs.User, Loc.GetString("mop-component-mop-is-dry-message")); return false; } - Mopping = true; + if (eventArgs.Target == null) + { + // Drop the liquid on the mop on to the ground + solutionSystem.SplitSolution(Owner.Uid, contents, FixedPoint2.Min(ResidueAmount, CurrentVolume)) + .SpillAt(eventArgs.ClickLocation, "PuddleSmear"); + return true; + } + + if (!eventArgs.Target.TryGetComponent(out PuddleComponent? puddleComponent) || + !solutionSystem.TryGetSolution(puddleComponent.OwnerUid, puddleComponent.SolutionName, out var puddleSolution)) + return false; // So if the puddle has 20 units we mop in 2 seconds. Don't just store CurrentVolume given it can change so need to re-calc it anyway. - var doAfterArgs = new DoAfterEventArgs(eventArgs.User, _mopSpeed * puddleVolume.Float() / 10.0f, + var doAfterArgs = new DoAfterEventArgs(eventArgs.User, _mopSpeed * puddleSolution.CurrentVolume.Float() / 10.0f, target: eventArgs.Target) { BreakOnUserMove = true, BreakOnStun = true, BreakOnDamage = true, }; + Mopping = true; var result = await EntitySystem.Get().WaitDoAfter(doAfterArgs); - Mopping = false; if (result == DoAfterStatus.Cancelled || @@ -136,39 +131,32 @@ namespace Content.Server.Fluids.Components puddleComponent.Deleted) return false; - // Annihilate the puddle - var transferAmount = FixedPoint2.Min(FixedPoint2.New(5), puddleComponent.CurrentVolume, CurrentVolume); - var puddleCleaned = puddleComponent.CurrentVolume - transferAmount <= 0; - var puddleSystem = EntitySystem.Get(); - var solutionSystem = EntitySystem.Get(); - if (transferAmount == 0) - { - if (puddleSystem.EmptyHolder(puddleComponent.Owner.Uid, puddleComponent)) //The puddle doesn't actually *have* reagents, for example vomit because there's no "vomit" reagent. - { - puddleComponent.Owner.Delete(); - transferAmount = FixedPoint2.Min(FixedPoint2.New(5), CurrentVolume); - puddleCleaned = true; - } - else - { - return true; - } - } + 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, CurrentVolume); else - { - if (solutionSystem.TryGetSolution(eventArgs.Target.Uid, puddleComponent.SolutionName, out var puddleSolution)) - solutionSystem.SplitSolution(eventArgs.Target.Uid, puddleSolution, transferAmount); - } + transferAmount = FixedPoint2.Min(PickupAmount, puddleSolution.TotalVolume, CurrentVolume); - if (puddleCleaned) //After cleaning the puddle, make a new puddle with solution from the mop as a "wet floor". Then evaporate it slowly. + // is the puddle cleaned? + if (puddleSolution.TotalVolume - transferAmount <= 0) { + puddleComponent.Owner.Delete(); + + // After cleaning the puddle, make a new puddle with solution from the mop as a "wet floor". Then evaporate it slowly. + // we do this WITHOUT adding to the existing puddle. Otherwise we have might have water puddles with the vomit sprite. solutionSystem.SplitSolution(Owner.Uid, contents, transferAmount) - .SpillAt(eventArgs.ClickLocation, "PuddleSmear"); + .SplitSolution(ResidueAmount) + .SpillAt(eventArgs.ClickLocation, "PuddleSmear", combine: false); } else { - EntitySystem.Get().SplitSolution(Owner.Uid, contents, transferAmount); + // remove solution from the puddle + solutionSystem.SplitSolution(eventArgs.Target.Uid, puddleSolution, transferAmount); + + // and from the mop + solutionSystem.SplitSolution(Owner.Uid, contents, transferAmount); } SoundSystem.Play(Filter.Pvs(Owner), _pickupSound.GetSound(), Owner); diff --git a/Content.Server/Fluids/Components/SpillExtensions.cs b/Content.Server/Fluids/Components/SpillExtensions.cs index 2066ae3e2e..120b875e8d 100644 --- a/Content.Server/Fluids/Components/SpillExtensions.cs +++ b/Content.Server/Fluids/Components/SpillExtensions.cs @@ -27,10 +27,11 @@ namespace Content.Server.Fluids.Components /// The prototype to use. /// Play the spill sound. /// The puddle if one was created, null otherwise. + /// Whether to attempt to merge with existing puddles public static PuddleComponent? SpillAt(this Solution solution, IEntity entity, string prototype, - bool sound = true) + bool sound = true, bool combine = true) { - return solution.SpillAt(entity.Transform.Coordinates, prototype, sound); + return solution.SpillAt(entity.Transform.Coordinates, prototype, sound, combine: combine); } /// @@ -43,13 +44,14 @@ namespace Content.Server.Fluids.Components /// The prototype to use. /// Play the spill sound. /// + /// Whether to attempt to merge with existing puddles /// The puddle if one was created, null otherwise. public static PuddleComponent? SpillAt(this Solution solution, EntityUid entity, string prototype, - bool sound = true, IEntityManager? entityManager = null) + bool sound = true, IEntityManager? entityManager = null, bool combine = true) { entityManager ??= IoCManager.Resolve(); - return solution.SpillAt(entityManager.GetComponent(entity).Coordinates, prototype, sound); + return solution.SpillAt(entityManager.GetComponent(entity).Coordinates, prototype, sound, combine: combine); } /// @@ -62,11 +64,12 @@ namespace Content.Server.Fluids.Components /// The prototype to use. /// The puddle if one was created, null otherwise. /// Play the spill sound. + /// Whether to attempt to merge with existing puddles /// True if a puddle was created, false otherwise. public static bool TrySpillAt(this Solution solution, IEntity entity, string prototype, - [NotNullWhen(true)] out PuddleComponent? puddle, bool sound = true) + [NotNullWhen(true)] out PuddleComponent? puddle, bool sound = true, bool combine = true) { - puddle = solution.SpillAt(entity, prototype, sound); + puddle = solution.SpillAt(entity, prototype, sound, combine: combine); return puddle != null; } @@ -77,9 +80,10 @@ namespace Content.Server.Fluids.Components /// The coordinates to spill the solution at. /// The prototype to use. /// Whether or not to play the spill sound. + /// Whether to attempt to merge with existing puddles /// The puddle if one was created, null otherwise. public static PuddleComponent? SpillAt(this Solution solution, EntityCoordinates coordinates, string prototype, - bool overflow = true, bool sound = true) + bool overflow = true, bool sound = true, bool combine = true) { if (solution.TotalVolume == 0) return null; @@ -89,7 +93,7 @@ namespace Content.Server.Fluids.Components if (!mapManager.TryGetGrid(coordinates.GetGridId(entityManager), out var mapGrid)) return null; // Let's not spill to space. - return SpillAt(mapGrid.GetTileRef(coordinates), solution, prototype, overflow, sound); + return SpillAt(mapGrid.GetTileRef(coordinates), solution, prototype, overflow, sound, combine: combine); } /// @@ -100,11 +104,12 @@ namespace Content.Server.Fluids.Components /// The prototype to use. /// The puddle if one was created, null otherwise. /// Play the spill sound. + /// Whether to attempt to merge with existing puddles /// True if a puddle was created, false otherwise. public static bool TrySpillAt(this Solution solution, EntityCoordinates coordinates, string prototype, - [NotNullWhen(true)] out PuddleComponent? puddle, bool sound = true) + [NotNullWhen(true)] out PuddleComponent? puddle, bool sound = true, bool combine = true) { - puddle = solution.SpillAt(coordinates, prototype, sound); + puddle = solution.SpillAt(coordinates, prototype, sound, combine: combine); return puddle != null; } @@ -125,17 +130,17 @@ namespace Content.Server.Fluids.Components } public static PuddleComponent? SpillAt(this TileRef tileRef, Solution solution, string prototype, - bool overflow = true, bool sound = true, bool noTileReact = false) + bool overflow = true, bool sound = true, bool noTileReact = false, bool combine = true) { if (solution.TotalVolume <= 0) return null; + // If space return early, let that spill go out into the void + if (tileRef.Tile.IsEmpty) return null; + var mapManager = IoCManager.Resolve(); var prototypeManager = IoCManager.Resolve(); var serverEntityManager = IoCManager.Resolve(); - // If space return early, let that spill go out into the void - if (tileRef.Tile.IsEmpty) return null; - var gridId = tileRef.GridIndex; if (!mapManager.TryGetGrid(gridId, out var mapGrid)) return null; // Let's not spill to invalid grids. @@ -155,10 +160,7 @@ namespace Content.Server.Fluids.Components // Get normalized co-ordinate for spill location and spill it in the centre // TODO: Does SnapGrid or something else already do this? - var spillGridCoords = mapGrid.GridTileToLocal(tileRef.GridIndices); - - PuddleComponent? puddle = null; - var spilt = false; + var spillGridCoords = mapGrid.GridTileToWorld(tileRef.GridIndices); var spillEntities = IoCManager.Resolve() .GetEntitiesIntersecting(mapGrid.ParentMapId, spillGridCoords.Position).ToArray(); @@ -177,22 +179,20 @@ namespace Content.Server.Fluids.Components var puddleSystem = EntitySystem.Get(); - foreach (var spillEntity in spillEntities) + if (combine) { - if (!spillEntity.TryGetComponent(out PuddleComponent? puddleComponent)) continue; + foreach (var spillEntity in spillEntities) + { + if (!spillEntity.TryGetComponent(out PuddleComponent? puddleComponent)) continue; - if (!overflow && puddleSystem.WouldOverflow(puddleComponent.Owner.Uid, solution, puddleComponent)) return null; + if (!overflow && puddleSystem.WouldOverflow(puddleComponent.Owner.Uid, solution, puddleComponent)) return null; - if (!puddleSystem.TryAddSolution(puddleComponent.Owner.Uid, solution, sound)) continue; + if (!puddleSystem.TryAddSolution(puddleComponent.Owner.Uid, solution, sound)) continue; - puddle = puddleComponent; - spilt = true; - break; + return puddleComponent; + } } - // Did we add to an existing puddle - if (spilt) return puddle; - var puddleEnt = serverEntityManager.SpawnEntity(prototype, spillGridCoords); var newPuddleComponent = puddleEnt.GetComponent();