Fix spill crash (#5480)

* fix spill

* clean up mop component

* remove unecessary code
This commit is contained in:
Leon Friedrich
2021-11-24 06:14:46 +13:00
committed by GitHub
parent cc79053825
commit 1de82b4324
3 changed files with 70 additions and 82 deletions

View File

@@ -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;
}
}
}

View File

@@ -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);
/// <summary>
/// After cleaning a floor tile, leave this much reagent as a puddle. I.e., leave behind a wet floor.
/// </summary>
[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<SolutionContainerSystem>();
if (!EntitySystem.Get<SolutionContainerSystem>().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<SolutionContainerSystem>().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<DoAfterSystem>().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<PuddleSystem>();
var solutionSystem = EntitySystem.Get<SolutionContainerSystem>();
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.
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
transferAmount = FixedPoint2.Min(PickupAmount, puddleSolution.TotalVolume, CurrentVolume);
// is the puddle cleaned?
if (puddleSolution.TotalVolume - transferAmount <= 0)
{
puddleComponent.Owner.Delete();
transferAmount = FixedPoint2.Min(FixedPoint2.New(5), CurrentVolume);
puddleCleaned = true;
}
else
{
return true;
}
}
else
{
if (solutionSystem.TryGetSolution(eventArgs.Target.Uid, puddleComponent.SolutionName, out var puddleSolution))
solutionSystem.SplitSolution(eventArgs.Target.Uid, puddleSolution, transferAmount);
}
if (puddleCleaned) //After cleaning the puddle, make a new puddle with solution from the mop as a "wet floor". Then evaporate it slowly.
{
// 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<SolutionContainerSystem>().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);

View File

@@ -27,10 +27,11 @@ namespace Content.Server.Fluids.Components
/// <param name="prototype">The prototype to use.</param>
/// <param name="sound">Play the spill sound.</param>
/// <returns>The puddle if one was created, null otherwise.</returns>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
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);
}
/// <summary>
@@ -43,13 +44,14 @@ namespace Content.Server.Fluids.Components
/// <param name="prototype">The prototype to use.</param>
/// <param name="sound">Play the spill sound.</param>
/// <param name="entityManager"></param>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
/// <returns>The puddle if one was created, null otherwise.</returns>
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<IEntityManager>();
return solution.SpillAt(entityManager.GetComponent<TransformComponent>(entity).Coordinates, prototype, sound);
return solution.SpillAt(entityManager.GetComponent<TransformComponent>(entity).Coordinates, prototype, sound, combine: combine);
}
/// <summary>
@@ -62,11 +64,12 @@ namespace Content.Server.Fluids.Components
/// <param name="prototype">The prototype to use.</param>
/// <param name="puddle">The puddle if one was created, null otherwise.</param>
/// <param name="sound">Play the spill sound.</param>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
/// <returns>True if a puddle was created, false otherwise.</returns>
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
/// <param name="coordinates">The coordinates to spill the solution at.</param>
/// <param name="prototype">The prototype to use.</param>
/// <param name="sound">Whether or not to play the spill sound.</param>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
/// <returns>The puddle if one was created, null otherwise.</returns>
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);
}
/// <summary>
@@ -100,11 +104,12 @@ namespace Content.Server.Fluids.Components
/// <param name="prototype">The prototype to use.</param>
/// <param name="puddle">The puddle if one was created, null otherwise.</param>
/// <param name="sound">Play the spill sound.</param>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
/// <returns>True if a puddle was created, false otherwise.</returns>
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<IMapManager>();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
var serverEntityManager = IoCManager.Resolve<IServerEntityManager>();
// 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<IEntityLookup>()
.GetEntitiesIntersecting(mapGrid.ParentMapId, spillGridCoords.Position).ToArray();
@@ -177,6 +179,8 @@ namespace Content.Server.Fluids.Components
var puddleSystem = EntitySystem.Get<PuddleSystem>();
if (combine)
{
foreach (var spillEntity in spillEntities)
{
if (!spillEntity.TryGetComponent(out PuddleComponent? puddleComponent)) continue;
@@ -185,13 +189,9 @@ namespace Content.Server.Fluids.Components
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<PuddleComponent>();