#nullable enable
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Chemistry;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.GameObjects.Components.Fluids
{
public static class SpillExtensions
{
///
/// Spills the specified solution at the entity's location if possible.
///
///
/// The entity to use as a location to spill the solution at.
///
/// Initial solution for the prototype.
/// The prototype to use.
/// Play the spill sound.
/// The puddle if one was created, null otherwise.
public static PuddleComponent? SpillAt(this Solution solution, IEntity entity, string prototype, bool sound = true)
{
var coordinates = entity.Transform.Coordinates;
return solution.SpillAt(coordinates, prototype, sound);
}
///
/// Spills the specified solution at the entity's location if possible.
///
///
/// The entity to use as a location to spill the solution at.
///
/// Initial solution for the prototype.
/// The prototype to use.
/// The puddle if one was created, null otherwise.
/// Play the spill sound.
/// 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)
{
puddle = solution.SpillAt(entity, prototype, sound);
return puddle != null;
}
///
/// Spills solution at the specified grid coordinates.
///
/// Initial solution for the prototype.
/// The coordinates to spill the solution at.
/// The prototype to use.
/// Whether or not to play the spill sound.
/// The puddle if one was created, null otherwise.
public static PuddleComponent? SpillAt(this Solution solution, EntityCoordinates coordinates, string prototype, bool sound = true)
{
if (solution.TotalVolume == 0)
{
return null;
}
var mapManager = IoCManager.Resolve();
var entityManager = IoCManager.Resolve();
var serverEntityManager = IoCManager.Resolve();
var gridId = coordinates.GetGridId(entityManager);
var mapGrid = mapManager.GetGrid(gridId);
// If space return early, let that spill go out into the void
var tileRef = mapGrid.GetTileRef(coordinates);
if (tileRef.Tile.IsEmpty)
{
return null;
}
// Get normalized co-ordinate for spill location and spill it in the centre
// TODO: Does SnapGrid or something else already do this?
var spillTileMapGrid = mapManager.GetGrid(gridId);
var spillTileRef = spillTileMapGrid.GetTileRef(coordinates).GridIndices;
var spillGridCoords = spillTileMapGrid.GridTileToLocal(spillTileRef);
var spilt = false;
foreach (var spillEntity in entityManager.GetEntitiesAt(spillTileMapGrid.ParentMapId, spillGridCoords.Position))
{
if (!spillEntity.TryGetComponent(out PuddleComponent? puddleComponent))
{
continue;
}
if (!puddleComponent.TryAddSolution(solution, sound))
{
continue;
}
spilt = true;
break;
}
// Did we add to an existing puddle
if (spilt)
{
return null;
}
var puddle = serverEntityManager.SpawnEntity(prototype, spillGridCoords);
var newPuddleComponent = puddle.GetComponent();
newPuddleComponent.TryAddSolution(solution, sound);
return newPuddleComponent;
}
///
/// Spills the specified solution at the entity's location if possible.
///
/// The coordinates to spill the solution at.
/// Initial solution for the prototype.
/// The prototype to use.
/// The puddle if one was created, null otherwise.
/// Play the spill sound.
/// 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)
{
puddle = solution.SpillAt(coordinates, prototype, sound);
return puddle != null;
}
}
}