* serv3 in shared pt 1 * beginning of deepclone api * progress in implementing ideepclone & serv3 in content * adds target * its cant hurt you it cant hurt you * more changes to content.server * adds dataclasses * almost there * renamed & edited entry * finishes refactoring content to use serv3 * gasmixture runtimes, next: reagentunit * fucin hell that was an annoying one * adds flags * fixes some yaml errors * removes comment * fixes generic components for now * removes todo actually clones values my god paul fixes bug involving resolving custom data classes from other proj renames dataclass fixes spritecomp adds WithFormat.Constants support * adds deepclone to ResistanceSet * adds a bunch of deepclone implementations adds a deepclone analyzer (TODO) adds a deep clone fallback for classes & structs * fixes a bunch of runtimes * adds deepclone to entityuid * adds generator to sln * gets rid of warnings * fixes * argh * componentdata refactors * more deepclone impl * heck me i reworked all of content deepclone * renames custom dataclasstarget * misc * reworks prototypes * deepclone nuke * renamed customdataclass attribute * fixes everything * misc fixed * the killcommit * getting there * changed yamlfieldattribute namespace * adds back iselfserialize * renames everything to data(field/definition) * ouch * Fix most errors on content * Fix more errors in content * Fix some components * work on tests * fixes some customdataclasses * fuggin shit * yes * yeas * Remove data classes * Data field naming fixes * arg * Git resetti RobustToolbox * Merge fixes * General fixes * Fix startup serialization errors * Fix DamageContainerPrototype when supported classes or types are null * Implement construction graph step type serializer * Fix up construction serialization * Fix up construction serialization part 2 * Fix null list in technology database component * Fix body serialization * Fix entity storage serialization * Fix actions serialization * Fix AI serialization * Fix reaction serialization * Fix body serialization * Fix grid atmosphere serialization * Rename IServ3Manager to ISerializationManager * Convert every non generic serializer to the new format, general fixes * Serialization and body system fix * pushinheritance fix * Update all prototypes to have a parent and have consistent id/parent properties * Merge fixes * smh my head * cuddling slaps * Content commit for engine PR * stuff * more fixes * argh * yes even you are fixed * changelog fixes * fixes seeds * argh * Test fixes * Add writing for alert order prototype * Fix alert order writing * FIX * its been alot ok * Fix the rest of the visualizers * Fix server alerts component tests * Fix alert prototype tests not using the read value * Fix alert prototype tests initializing serialization multiple times * THIS IS AN AMERICAN CODEBASE GOD BLESS THE USA * Add ImplicitDataDefinitionForInheritors to IMechanismBehavior Fixes the behaviors not being found * Fix NRE in strap component Good night to the 1 buckle optimization * Fix clothing component slot flags serialization tag * Fix body component in all components test * Merge fixes * ffs * Make construction graph prototype use serialization hooks * human yaml linted * a * Do the thing for construction * stuff * a * monke see yaml linter * LINT HARDER * Remove redundant todo * yes * Add skip hook argument to readers and copiers * we gamin * test/datafield fixes * adds more verbose validation * moves linter to action * Improve construction graph step type serializer error message * Fix ammo box component NRE * gamin * some updates to the linter * yes * removes that test * misc fixes * array fix priority fix misc fixes * adds proper info the validation * adds alwaysrelevant usa * Make yaml linter take half as long to run (~50% less) * Make yaml linter 5 times faster (~80% less execution time) * based vera being based * fixes mapsaving * warning cleanup & moves surpressor * removes old msbuild targets * Revert "Make yaml linter 5 times faster (~80% less execution time)" This reverts commit 3e6091359a26252c3e98828199553de668031c63. * Add -nowarn to yaml linter run configuration * Improve yaml linter message feedback * Make dependencies an argument instead of a property on the serialization manager * yamllinting slaps * Clean up type serializers * Move yaml linter code to its own method * Fix yaml errors * Change yaml linter action name and remove -nowarn * yaml linter please shut * Git resetti robust toolbox Co-authored-by: Paul <ritter.paul1+git@googlemail.com> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
414 lines
14 KiB
C#
414 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Content.Server.GameObjects.Components.Chemistry;
|
|
using Content.Server.GameObjects.Components.Movement;
|
|
using Content.Shared.Chemistry;
|
|
using Content.Shared.GameObjects.EntitySystems;
|
|
using Content.Shared.Maps;
|
|
using Content.Shared.Physics;
|
|
using Content.Shared.Utility;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Localization;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Random;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
|
using Robust.Shared.Utility;
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
namespace Content.Server.GameObjects.Components.Fluids
|
|
{
|
|
/// <summary>
|
|
/// Puddle on a floor
|
|
/// </summary>
|
|
[RegisterComponent]
|
|
public class PuddleComponent : Component, IExamine, IMapInit
|
|
{
|
|
// Current design: Something calls the SpillHelper.Spill, that will either
|
|
// A) Add to an existing puddle at the location (normalised to tile-center) or
|
|
// B) add a new one
|
|
// From this every time a puddle is spilt on it will try and overflow to its neighbours if possible,
|
|
// and also update its appearance based on volume level (opacity) and chemistry color
|
|
// Small puddles will evaporate after a set delay
|
|
|
|
// TODO: 'leaves fluidtracks', probably in a separate component for stuff like gibb chunks?;
|
|
|
|
// based on behaviour (e.g. someone being punched vs slashed with a sword would have different blood sprite)
|
|
// to check for low volumes for evaporation or whatever
|
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
public override string Name => "Puddle";
|
|
|
|
private CancellationTokenSource _evaporationToken;
|
|
[DataField("evaporate_threshold")]
|
|
private ReagentUnit _evaporateThreshold = ReagentUnit.New(20); // How few <Solution Quantity> we can hold prior to self-destructing
|
|
public ReagentUnit EvaporateThreshold
|
|
{
|
|
get => _evaporateThreshold;
|
|
set => _evaporateThreshold = value;
|
|
}
|
|
private ReagentUnit _slipThreshold = ReagentUnit.New(3);
|
|
public ReagentUnit SlipThreshold
|
|
{
|
|
get => _slipThreshold;
|
|
set => _slipThreshold = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The time that it will take this puddle to evaporate, in seconds.
|
|
/// </summary>
|
|
[DataField("evaporate_time")]
|
|
public float EvaporateTime { get; private set; } = 5f;
|
|
|
|
[DataField("spill_sound")]
|
|
private string _spillSound = "/Audio/Effects/Fluids/splat.ogg";
|
|
|
|
/// <summary>
|
|
/// Whether or not this puddle is currently overflowing onto its neighbors
|
|
/// </summary>
|
|
private bool _overflown;
|
|
|
|
private SpriteComponent _spriteComponent;
|
|
private SnapGridComponent _snapGrid;
|
|
|
|
public ReagentUnit MaxVolume
|
|
{
|
|
get => _contents.MaxVolume;
|
|
set => _contents.MaxVolume = value;
|
|
}
|
|
|
|
[ViewVariables]
|
|
public ReagentUnit CurrentVolume => _contents.CurrentVolume;
|
|
|
|
// Volume at which the fluid will try to spill to adjacent components
|
|
// Currently a random number, potentially change
|
|
public ReagentUnit OverflowVolume => _overflowVolume;
|
|
[ViewVariables]
|
|
[DataField("overflow_volume")]
|
|
private ReagentUnit _overflowVolume = ReagentUnit.New(20);
|
|
private ReagentUnit OverflowLeft => CurrentVolume - OverflowVolume;
|
|
|
|
private SolutionContainerComponent _contents;
|
|
public bool EmptyHolder => _contents.ReagentList.Count == 0;
|
|
[DataField("variants")]
|
|
private int _spriteVariants = 1;
|
|
// Whether the underlying solution color should be used
|
|
[DataField("recolor")]
|
|
private bool _recolor = default;
|
|
|
|
private bool Slippery => Owner.TryGetComponent(out SlipperyComponent slippery) && slippery.Slippery;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
if (Owner.TryGetComponent(out SolutionContainerComponent solutionComponent))
|
|
{
|
|
_contents = solutionComponent;
|
|
}
|
|
else
|
|
{
|
|
_contents = Owner.AddComponent<SolutionContainerComponent>();
|
|
}
|
|
|
|
_snapGrid = Owner.EnsureComponent<SnapGridComponent>();
|
|
|
|
// Smaller than 1m^3 for now but realistically this shouldn't be hit
|
|
MaxVolume = ReagentUnit.New(1000);
|
|
|
|
// Random sprite state set server-side so it's consistent across all clients
|
|
_spriteComponent = Owner.EnsureComponent<SpriteComponent>();
|
|
|
|
var randomVariant = _random.Next(0, _spriteVariants - 1);
|
|
|
|
if (_spriteComponent.BaseRSIPath != null)
|
|
{
|
|
var baseName = new ResourcePath(_spriteComponent.BaseRSIPath).FilenameWithoutExtension;
|
|
|
|
_spriteComponent.LayerSetState(0, $"{baseName}-{randomVariant}"); // TODO: Remove hardcode
|
|
|
|
}
|
|
|
|
// UpdateAppearance should get called soon after this so shouldn't need to call Dirty() here
|
|
|
|
UpdateStatus();
|
|
}
|
|
|
|
void IMapInit.MapInit()
|
|
{
|
|
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
|
_spriteComponent.Rotation = Angle.FromDegrees(robustRandom.Next(0, 359));
|
|
}
|
|
|
|
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
|
|
{
|
|
if(Slippery)
|
|
{
|
|
message.AddText(Loc.GetString("It looks slippery."));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether adding this solution to this puddle would overflow.
|
|
/// </summary>
|
|
/// <param name="solution"></param>
|
|
/// <returns></returns>
|
|
public bool WouldOverflow(Solution solution)
|
|
{
|
|
return (CurrentVolume + solution.TotalVolume > _overflowVolume);
|
|
}
|
|
|
|
// Flow rate should probably be controlled globally so this is it for now
|
|
internal bool TryAddSolution(Solution solution, bool sound = true, bool checkForEvaporate = true, bool checkForOverflow = true)
|
|
{
|
|
if (solution.TotalVolume == 0)
|
|
{
|
|
return false;
|
|
}
|
|
var result = _contents.TryAddSolution(solution);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
UpdateStatus();
|
|
|
|
if (checkForOverflow)
|
|
{
|
|
CheckOverflow();
|
|
}
|
|
|
|
if (checkForEvaporate)
|
|
{
|
|
CheckEvaporate();
|
|
}
|
|
|
|
UpdateAppearance();
|
|
if (!sound)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayAtCoords(_spillSound, Owner.Transform.Coordinates);
|
|
return true;
|
|
}
|
|
|
|
internal Solution SplitSolution(ReagentUnit quantity)
|
|
{
|
|
var split = _contents.SplitSolution(quantity);
|
|
CheckEvaporate();
|
|
UpdateAppearance();
|
|
return split;
|
|
}
|
|
|
|
public void CheckEvaporate()
|
|
{
|
|
if (CurrentVolume == 0)
|
|
{
|
|
Owner.Delete();
|
|
}
|
|
}
|
|
|
|
public void Evaporate()
|
|
{
|
|
_contents.SplitSolution(ReagentUnit.Min(ReagentUnit.New(1), _contents.CurrentVolume));
|
|
if (CurrentVolume == 0)
|
|
{
|
|
Owner.Delete();
|
|
}
|
|
else
|
|
{
|
|
UpdateStatus();
|
|
}
|
|
}
|
|
|
|
public void UpdateStatus()
|
|
{
|
|
_evaporationToken?.Cancel();
|
|
if(Owner.Deleted) return;
|
|
|
|
UpdateAppearance();
|
|
UpdateSlip();
|
|
|
|
if (_evaporateThreshold == ReagentUnit.New(-1) || CurrentVolume > _evaporateThreshold)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_evaporationToken = new CancellationTokenSource();
|
|
|
|
// KYS to evaporate
|
|
Owner.SpawnTimer(TimeSpan.FromSeconds(EvaporateTime), Evaporate, _evaporationToken.Token);
|
|
}
|
|
|
|
private void UpdateSlip()
|
|
{
|
|
if ((_slipThreshold == ReagentUnit.New(-1) || CurrentVolume < _slipThreshold) &&
|
|
Owner.TryGetComponent(out SlipperyComponent oldSlippery))
|
|
{
|
|
oldSlippery.Slippery = false;
|
|
}
|
|
else if (CurrentVolume >= _slipThreshold)
|
|
{
|
|
var newSlippery = Owner.EnsureComponent<SlipperyComponent>();
|
|
newSlippery.Slippery = true;
|
|
}
|
|
}
|
|
|
|
private void UpdateAppearance()
|
|
{
|
|
if (Owner.Deleted || EmptyHolder)
|
|
{
|
|
return;
|
|
}
|
|
// Opacity based on level of fullness to overflow
|
|
// Hard-cap lower bound for visibility reasons
|
|
var volumeScale = (CurrentVolume.Float() / OverflowVolume.Float()) * 0.75f + 0.25f;
|
|
var cappedScale = Math.Min(1.0f, volumeScale);
|
|
// Color based on the underlying solutioncomponent
|
|
Color newColor;
|
|
if (_recolor)
|
|
{
|
|
newColor = _contents.Color.WithAlpha(cappedScale);
|
|
}
|
|
else
|
|
{
|
|
newColor = _spriteComponent.Color.WithAlpha(cappedScale);
|
|
}
|
|
|
|
_spriteComponent.Color = newColor;
|
|
|
|
_spriteComponent.Dirty();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Will overflow this entity to neighboring entities if required
|
|
/// </summary>
|
|
private void CheckOverflow()
|
|
{
|
|
if (CurrentVolume <= _overflowVolume || _overflown)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var nextPuddles = new List<PuddleComponent>() {this};
|
|
var overflownPuddles = new List<PuddleComponent>();
|
|
|
|
while (OverflowLeft > ReagentUnit.Zero && nextPuddles.Count > 0)
|
|
{
|
|
foreach (var next in nextPuddles.ToArray())
|
|
{
|
|
nextPuddles.Remove(next);
|
|
|
|
next._overflown = true;
|
|
overflownPuddles.Add(next);
|
|
|
|
var adjacentPuddles = next.GetAllAdjacentOverflow().ToArray();
|
|
if (OverflowLeft <= ReagentUnit.Epsilon * adjacentPuddles.Length)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (adjacentPuddles.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var numberOfAdjacent = ReagentUnit.New(adjacentPuddles.Length);
|
|
var overflowSplit = OverflowLeft / numberOfAdjacent;
|
|
foreach (var adjacent in adjacentPuddles)
|
|
{
|
|
var adjacentPuddle = adjacent();
|
|
var quantity = ReagentUnit.Min(overflowSplit, adjacentPuddle.OverflowVolume);
|
|
var spillAmount = _contents.SplitSolution(quantity);
|
|
|
|
adjacentPuddle.TryAddSolution(spillAmount, false, false, false);
|
|
nextPuddles.Add(adjacentPuddle);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var puddle in overflownPuddles)
|
|
{
|
|
puddle._overflown = false;
|
|
}
|
|
}
|
|
|
|
/// <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="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(Direction direction, out Func<PuddleComponent> puddle)
|
|
{
|
|
puddle = default;
|
|
|
|
var mapGrid = _mapManager.GetGrid(Owner.Transform.GridID);
|
|
|
|
if (!Owner.Transform.Coordinates.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;
|
|
}
|
|
|
|
foreach (var entity in _snapGrid.GetInDir(direction))
|
|
{
|
|
if (entity.TryGetComponent(out IPhysicsComponent physics) &&
|
|
(physics.CollisionLayer & (int) CollisionGroup.Impassable) != 0)
|
|
{
|
|
puddle = default;
|
|
return false;
|
|
}
|
|
|
|
if (entity.TryGetComponent(out PuddleComponent existingPuddle))
|
|
{
|
|
if (existingPuddle._overflown)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
puddle = () => existingPuddle;
|
|
}
|
|
}
|
|
|
|
if (puddle == default)
|
|
{
|
|
var grid = _snapGrid.DirectionToGrid(direction);
|
|
puddle = () => Owner.EntityManager.SpawnEntity(Owner.Prototype.ID, grid).GetComponent<PuddleComponent>();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <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()
|
|
{
|
|
foreach (var direction in SharedDirectionExtensions.RandomDirections())
|
|
{
|
|
if (TryGetAdjacentOverflow(direction, out var puddle))
|
|
{
|
|
yield return puddle;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|