Replace old Overflow algorithm (#6280)
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
@@ -1,28 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Server.Fluids.Components;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Directions;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Fluids;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Slippery;
|
||||
using Content.Shared.Verbs;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Fluids.EntitySystems
|
||||
@@ -30,8 +14,8 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
[UsedImplicitly]
|
||||
public sealed class PuddleSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly FluidSpreaderSystem _fluidSpreaderSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -45,7 +29,7 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
|
||||
private void OnInit(EntityUid uid, PuddleComponent component, ComponentInit args)
|
||||
{
|
||||
var solution = _solutionContainerSystem.EnsureSolution(uid, component.SolutionName);
|
||||
var solution = _solutionContainerSystem.EnsureSolution(uid, component.SolutionName);
|
||||
solution.MaxVolume = FixedPoint2.New(1000);
|
||||
}
|
||||
|
||||
@@ -111,21 +95,6 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
QueueDel(uid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether adding this solution to this puddle would overflow.
|
||||
/// </summary>
|
||||
/// <param name="uid">Uid of owning entity</param>
|
||||
/// <param name="puddle">Puddle to which we are adding solution</param>
|
||||
/// <param name="solution">Solution we intend to add</param>
|
||||
/// <returns></returns>
|
||||
public bool WouldOverflow(EntityUid uid, Solution solution, PuddleComponent? puddle = null)
|
||||
{
|
||||
if (!Resolve(uid, ref puddle))
|
||||
return false;
|
||||
|
||||
return puddle.CurrentVolume + solution.TotalVolume > puddle.OverflowVolume;
|
||||
}
|
||||
|
||||
public bool EmptyHolder(EntityUid uid, PuddleComponent? puddleComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref puddleComponent))
|
||||
@@ -147,24 +116,40 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
: FixedPoint2.Zero;
|
||||
}
|
||||
|
||||
public bool TryAddSolution(EntityUid uid, Solution solution,
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="puddleUid">Puddle to which we add</param>
|
||||
/// <param name="addedSolution">Solution that is added to puddleComponent</param>
|
||||
/// <param name="sound">Play sound on overflow</param>
|
||||
/// <param name="checkForOverflow">Overflow on encountered values</param>
|
||||
/// <param name="puddleComponent">Optional resolved PuddleComponent</param>
|
||||
/// <returns></returns>
|
||||
public bool TryAddSolution(EntityUid puddleUid,
|
||||
Solution addedSolution,
|
||||
bool sound = true,
|
||||
bool checkForOverflow = true,
|
||||
PuddleComponent? puddleComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref puddleComponent))
|
||||
if (!Resolve(puddleUid, ref puddleComponent))
|
||||
return false;
|
||||
|
||||
if (solution.TotalVolume == 0 ||
|
||||
if (addedSolution.TotalVolume == 0 ||
|
||||
!_solutionContainerSystem.TryGetSolution(puddleComponent.Owner, puddleComponent.SolutionName,
|
||||
out var puddleSolution))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
var result = _solutionContainerSystem
|
||||
.TryAddSolution(puddleComponent.Owner, puddleSolution, solution);
|
||||
.TryMixAndOverflow(puddleComponent.Owner, puddleSolution, addedSolution, puddleComponent.OverflowVolume,
|
||||
out var overflowSolution);
|
||||
|
||||
if (checkForOverflow && overflowSolution != null)
|
||||
{
|
||||
_fluidSpreaderSystem.AddOverflowingPuddle(puddleComponent, overflowSolution);
|
||||
}
|
||||
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
@@ -172,11 +157,6 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
|
||||
RaiseLocalEvent(puddleComponent.Owner, new SolutionChangedEvent());
|
||||
|
||||
if (checkForOverflow)
|
||||
{
|
||||
CheckOverflow(puddleComponent);
|
||||
}
|
||||
|
||||
if (!sound)
|
||||
{
|
||||
return true;
|
||||
@@ -188,139 +168,18 @@ namespace Content.Server.Fluids.EntitySystems
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will overflow this entity to neighboring entities if required
|
||||
/// Whether adding this solution to this puddle would overflow.
|
||||
/// </summary>
|
||||
private void CheckOverflow(PuddleComponent puddleComponent)
|
||||
/// <param name="uid">Uid of owning entity</param>
|
||||
/// <param name="puddle">Puddle to which we are adding solution</param>
|
||||
/// <param name="solution">Solution we intend to add</param>
|
||||
/// <returns></returns>
|
||||
public bool WouldOverflow(EntityUid uid, Solution solution, PuddleComponent? puddle = null)
|
||||
{
|
||||
if (puddleComponent.CurrentVolume <= puddleComponent.OverflowVolume
|
||||
|| puddleComponent.Overflown)
|
||||
return;
|
||||
|
||||
var nextPuddles = new List<PuddleComponent>() { puddleComponent };
|
||||
var overflownPuddles = new List<PuddleComponent>();
|
||||
|
||||
while (puddleComponent.OverflowLeft > FixedPoint2.Zero && nextPuddles.Count > 0)
|
||||
{
|
||||
foreach (var next in nextPuddles.ToArray())
|
||||
{
|
||||
nextPuddles.Remove(next);
|
||||
|
||||
next.Overflown = true;
|
||||
overflownPuddles.Add(next);
|
||||
|
||||
var adjacentPuddles = GetAllAdjacentOverflow(next).ToArray();
|
||||
if (puddleComponent.OverflowLeft <= FixedPoint2.Epsilon * adjacentPuddles.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (adjacentPuddles.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var numberOfAdjacent = FixedPoint2.New(adjacentPuddles.Length);
|
||||
var overflowSplit = puddleComponent.OverflowLeft / numberOfAdjacent;
|
||||
foreach (var adjacent in adjacentPuddles)
|
||||
{
|
||||
var adjacentPuddle = adjacent();
|
||||
var quantity = FixedPoint2.Min(overflowSplit, adjacentPuddle.OverflowVolume);
|
||||
var puddleSolution = _solutionContainerSystem.EnsureSolution(puddleComponent.Owner,
|
||||
puddleComponent.SolutionName);
|
||||
var spillAmount = _solutionContainerSystem.SplitSolution(puddleComponent.Owner,
|
||||
puddleSolution, quantity);
|
||||
|
||||
TryAddSolution(adjacentPuddle.Owner, spillAmount, false, false);
|
||||
nextPuddles.Add(adjacentPuddle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var puddle in overflownPuddles)
|
||||
{
|
||||
puddle.Overflown = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds or creates adjacent puddles in random directions from this one
|
||||
/// </summary>
|
||||
/// <returns>Enumerable of the puddles found or to be created</returns>
|
||||
private IEnumerable<Func<PuddleComponent>> GetAllAdjacentOverflow(PuddleComponent puddleComponent)
|
||||
{
|
||||
foreach (var direction in SharedDirectionExtensions.RandomDirections())
|
||||
{
|
||||
if (TryGetAdjacentOverflow(puddleComponent, direction, out var puddle))
|
||||
{
|
||||
yield return puddle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get an adjacent coordinate to overflow to, unless it is blocked by a wall on the
|
||||
/// same tile or the tile is empty
|
||||
/// </summary>
|
||||
/// <param name="puddleComponent"></param>
|
||||
/// <param name="direction">The direction to get the puddle from, respective to this one</param>
|
||||
/// <param name="puddle">The puddle that was found or is to be created, or null if there
|
||||
/// is a wall in the way</param>
|
||||
/// <returns>true if a puddle was found or created, false otherwise</returns>
|
||||
private bool TryGetAdjacentOverflow(PuddleComponent puddleComponent, Direction direction,
|
||||
[NotNullWhen(true)] out Func<PuddleComponent>? puddle)
|
||||
{
|
||||
puddle = default;
|
||||
|
||||
// We're most likely in space, do nothing.
|
||||
if (!EntityManager.GetComponent<TransformComponent>(puddleComponent.Owner).GridID.IsValid())
|
||||
if (!Resolve(uid, ref puddle))
|
||||
return false;
|
||||
|
||||
var mapGrid = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(puddleComponent.Owner).GridID);
|
||||
var coords = EntityManager.GetComponent<TransformComponent>(puddleComponent.Owner).Coordinates;
|
||||
|
||||
if (!coords.Offset(direction).TryGetTileRef(out var tile))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// If space return early, let that spill go out into the void
|
||||
if (tile.Value.Tile.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EntityManager.GetComponent<TransformComponent>(puddleComponent.Owner).Anchored)
|
||||
return false;
|
||||
|
||||
foreach (var entity in mapGrid.GetInDir(coords, direction))
|
||||
{
|
||||
if (EntityManager.TryGetComponent(entity, out IPhysBody? physics) &&
|
||||
(physics.CollisionLayer & (int)CollisionGroup.Impassable) != 0)
|
||||
{
|
||||
puddle = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (EntityManager.TryGetComponent(entity, out PuddleComponent? existingPuddle))
|
||||
{
|
||||
if (existingPuddle.Overflown)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
puddle = () => existingPuddle;
|
||||
}
|
||||
}
|
||||
|
||||
puddle ??= () =>
|
||||
{
|
||||
var id = EntityManager.SpawnEntity(
|
||||
EntityManager.GetComponent<MetaDataComponent>(puddleComponent.Owner).EntityPrototype?.ID,
|
||||
mapGrid.DirectionToGrid(coords, direction));
|
||||
return EntityManager.GetComponent<PuddleComponent>(id);
|
||||
};
|
||||
|
||||
return true;
|
||||
return puddle.CurrentVolume + solution.TotalVolume > puddle.OverflowVolume;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user