using System.Diagnostics.CodeAnalysis; using System.Linq; using Content.Server.Chemistry.EntitySystems; using Content.Server.Coordinates.Helpers; using Content.Server.Fluids.EntitySystems; using Content.Shared.Chemistry.Components; using Content.Shared.Chemistry.Reagent; using Content.Shared.FixedPoint; using Robust.Server.GameObjects; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Prototypes; namespace Content.Server.Fluids.Components { // TODO: Kill these with fire 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. /// Whether to attempt to merge with existing puddles public static PuddleComponent? SpillAt(this Solution solution, EntityUid entity, string prototype, bool sound = true, bool combine = true) { return solution.SpillAt(IoCManager.Resolve().GetComponent(entity).Coordinates, prototype, sound, combine: combine); } /// /// 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. /// Whether to attempt to merge with existing puddles /// True if a puddle was created, false otherwise. public static bool TrySpillAt(this Solution solution, EntityUid entity, string prototype, [NotNullWhen(true)] out PuddleComponent? puddle, bool sound = true, bool combine = true) { puddle = solution.SpillAt(entity, prototype, sound, combine: combine); 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. /// 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 combine = true) { if (solution.TotalVolume == 0) return null; var mapManager = IoCManager.Resolve(); var entityManager = IoCManager.Resolve(); 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, combine: combine); } /// /// 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. /// 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, bool combine = true) { puddle = solution.SpillAt(coordinates, prototype, sound, combine: combine); return puddle != null; } public static bool TryGetPuddle(this TileRef tileRef, GridTileLookupSystem? gridTileLookupSystem, [NotNullWhen(true)] out PuddleComponent? puddle) { foreach (var entity in tileRef.GetEntitiesInTileFast(gridTileLookupSystem)) { if (IoCManager.Resolve().TryGetComponent(entity, out PuddleComponent? p)) { puddle = p; return true; } } puddle = null; return false; } public static PuddleComponent? SpillAt(this TileRef tileRef, Solution solution, string prototype, 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(); var gridId = tileRef.GridIndex; if (!mapManager.TryGetGrid(gridId, out var mapGrid)) return null; // Let's not spill to invalid grids. if (!noTileReact) { // First, do all tile reactions foreach (var reagent in solution.Contents.ToArray()) { var proto = prototypeManager.Index(reagent.ReagentId); proto.ReactionTile(tileRef, reagent.Quantity); } } // Tile reactions used up everything. if (solution.CurrentVolume == FixedPoint2.Zero) 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 spillGridCoords = mapGrid.GridTileToWorld(tileRef.GridIndices); var spillEntities = IoCManager.Resolve() .GetEntitiesIntersecting(mapGrid.ParentMapId, spillGridCoords.Position).ToArray(); foreach (var spillEntity in spillEntities) { if (EntitySystem.Get() .TryGetRefillableSolution(spillEntity, out var solutionContainerComponent)) { EntitySystem.Get().Refill(spillEntity, solutionContainerComponent, solution.SplitSolution(FixedPoint2.Min( solutionContainerComponent.AvailableVolume, solutionContainerComponent.MaxSpillRefill)) ); } } var puddleSystem = EntitySystem.Get(); if (combine) { foreach (var spillEntity in spillEntities) { if (!IoCManager.Resolve().TryGetComponent(spillEntity, out PuddleComponent? puddleComponent)) continue; if (!overflow && puddleSystem.WouldOverflow(puddleComponent.Owner, solution, puddleComponent)) return null; if (!puddleSystem.TryAddSolution(puddleComponent.Owner, solution, sound)) continue; return puddleComponent; } } var puddleEnt = serverEntityManager.SpawnEntity(prototype, spillGridCoords); var newPuddleComponent = IoCManager.Resolve().GetComponent(puddleEnt); puddleSystem.TryAddSolution(newPuddleComponent.Owner, solution, sound); return newPuddleComponent; } } }