2021-12-05 04:18:30 +01:00
using Content.Server.Administration.Logs ;
2021-11-24 00:38:39 +01:00
using Content.Server.Chemistry.EntitySystems ;
using Content.Server.Fluids.Components ;
2022-08-03 17:47:14 -07:00
using Content.Server.Nutrition.Components ;
2021-12-05 04:18:30 +01:00
using Content.Shared.Chemistry.Components ;
using Content.Shared.Chemistry.Reagent ;
2022-10-23 11:30:37 +13:00
using Content.Shared.Clothing.Components ;
2021-12-05 04:18:30 +01:00
using Content.Shared.Database ;
using Content.Shared.FixedPoint ;
2022-01-13 17:22:19 +03:00
using Content.Shared.Inventory.Events ;
2021-11-24 00:38:39 +01:00
using Content.Shared.Throwing ;
using Content.Shared.Verbs ;
using JetBrains.Annotations ;
2021-12-05 04:18:30 +01:00
using Robust.Shared.Map ;
using Robust.Shared.Prototypes ;
2022-10-23 11:30:37 +13:00
using System.Diagnostics.CodeAnalysis ;
using System.Linq ;
2023-04-06 14:20:48 -07:00
using Content.Shared.Chemistry ;
using Content.Shared.Chemistry.Reaction ;
2023-02-24 19:01:25 -05:00
using Content.Shared.DoAfter ;
2023-04-06 14:20:48 -07:00
using Content.Shared.Examine ;
using Content.Shared.IdentityManagement ;
using Content.Shared.Popups ;
2023-04-03 13:13:48 +12:00
using Content.Shared.Spillable ;
2023-04-06 14:20:48 -07:00
using Content.Shared.Weapons.Melee ;
using Content.Shared.Weapons.Melee.Events ;
using Robust.Shared.Player ;
using Robust.Shared.Random ;
2021-11-24 00:38:39 +01:00
namespace Content.Server.Fluids.EntitySystems ;
[UsedImplicitly]
2022-02-15 03:22:26 +01:00
public sealed class SpillableSystem : EntitySystem
2021-11-24 00:38:39 +01:00
{
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default ! ;
2021-12-05 04:18:30 +01:00
[Dependency] private readonly PuddleSystem _puddleSystem = default ! ;
[Dependency] private readonly IMapManager _mapManager = default ! ;
[Dependency] private readonly IPrototypeManager _prototypeManager = default ! ;
2022-03-03 21:18:35 +11:00
[Dependency] private readonly EntityLookupSystem _entityLookup = default ! ;
2023-04-06 14:20:48 -07:00
[Dependency] private readonly IAdminLogManager _adminLogger = default ! ;
[Dependency] private readonly IRobustRandom _random = default ! ;
2023-04-03 13:13:48 +12:00
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default ! ;
2023-04-06 14:20:48 -07:00
[Dependency] private readonly ReactiveSystem _reactive = default ! ;
[Dependency] private readonly SharedPopupSystem _popup = default ! ;
2021-11-24 00:38:39 +01:00
2021-12-05 04:18:30 +01:00
public override void Initialize ( )
2021-11-24 00:38:39 +01:00
{
base . Initialize ( ) ;
2023-04-06 14:20:48 -07:00
SubscribeLocalEvent < SpillableComponent , ExaminedEvent > ( OnExamined ) ;
2021-11-24 00:38:39 +01:00
SubscribeLocalEvent < SpillableComponent , LandEvent > ( SpillOnLand ) ;
2023-04-06 14:20:48 -07:00
SubscribeLocalEvent < SpillableComponent , MeleeHitEvent > ( SplashOnMeleeHit ) ;
2022-02-10 15:30:59 +13:00
SubscribeLocalEvent < SpillableComponent , GetVerbsEvent < Verb > > ( AddSpillVerb ) ;
2022-01-13 17:22:19 +03:00
SubscribeLocalEvent < SpillableComponent , GotEquippedEvent > ( OnGotEquipped ) ;
2022-06-23 19:26:54 -07:00
SubscribeLocalEvent < SpillableComponent , SolutionSpikeOverflowEvent > ( OnSpikeOverflow ) ;
2023-04-03 13:13:48 +12:00
SubscribeLocalEvent < SpillableComponent , SpillDoAfterEvent > ( OnDoAfter ) ;
2022-06-23 19:26:54 -07:00
}
2023-04-06 14:20:48 -07:00
private void OnExamined ( EntityUid uid , SpillableComponent component , ExaminedEvent args )
{
args . PushMarkup ( Loc . GetString ( "spill-examine-is-spillable" ) ) ;
if ( HasComp < MeleeWeaponComponent > ( uid ) )
args . PushMarkup ( Loc . GetString ( "spill-examine-spillable-weapon" ) ) ;
}
2022-06-23 19:26:54 -07:00
private void OnSpikeOverflow ( EntityUid uid , SpillableComponent component , SolutionSpikeOverflowEvent args )
{
if ( ! args . Handled )
{
SpillAt ( args . Overflow , Transform ( uid ) . Coordinates , "PuddleSmear" ) ;
}
args . Handled = true ;
2022-01-13 17:22:19 +03:00
}
private void OnGotEquipped ( EntityUid uid , SpillableComponent component , GotEquippedEvent args )
{
if ( ! component . SpillWorn )
return ;
if ( ! TryComp ( uid , out ClothingComponent ? clothing ) )
return ;
// check if entity was actually used as clothing
// not just taken in pockets or something
2022-07-27 03:53:47 -07:00
var isCorrectSlot = clothing . Slots . HasFlag ( args . SlotFlags ) ;
2022-01-13 17:22:19 +03:00
if ( ! isCorrectSlot ) return ;
if ( ! _solutionContainerSystem . TryGetSolution ( uid , component . SolutionName , out var solution ) )
return ;
2023-01-12 16:41:40 +13:00
if ( solution . Volume = = 0 )
2022-01-13 17:22:19 +03:00
return ;
// spill all solution on the player
2023-01-12 16:41:40 +13:00
var drainedSolution = _solutionContainerSystem . Drain ( uid , solution , solution . Volume ) ;
2022-01-13 17:22:19 +03:00
SpillAt ( args . Equipee , drainedSolution , "PuddleSmear" ) ;
2021-11-24 00:38:39 +01:00
}
2021-12-05 04:18:30 +01:00
/// <summary>
/// Spills the specified solution at the entity's location if possible.
/// </summary>
/// <param name="uid">
/// The entity to use as a location to spill the solution at.
/// </param>
/// <param name="solution">Initial solution for the prototype.</param>
/// <param name="prototype">The prototype to use.</param>
/// <param name="sound">Play the spill sound.</param>
/// <param name="combine">Whether to attempt to merge with existing puddles</param>
/// <param name="transformComponent">Optional Transform component</param>
/// <returns>The puddle if one was created, null otherwise.</returns>
public PuddleComponent ? SpillAt ( EntityUid uid , Solution solution , string prototype ,
bool sound = true , bool combine = true , TransformComponent ? transformComponent = null )
{
return ! Resolve ( uid , ref transformComponent , false )
? null
: SpillAt ( solution , transformComponent . Coordinates , prototype , sound : sound , combine : combine ) ;
}
2023-01-18 05:25:32 +11:00
private void SpillOnLand ( EntityUid uid , SpillableComponent component , ref LandEvent args )
2021-12-05 04:18:30 +01:00
{
if ( ! _solutionContainerSystem . TryGetSolution ( uid , component . SolutionName , out var solution ) ) return ;
2022-08-14 07:19:45 +02:00
if ( TryComp < DrinkComponent > ( uid , out var drink ) & & ( ! drink . Opened ) )
return ;
2021-12-05 04:18:30 +01:00
if ( args . User ! = null )
{
2022-05-28 23:41:17 -07:00
_adminLogger . Add ( LogType . Landed ,
2023-04-06 14:20:48 -07:00
$"{ToPrettyString(args.User.Value):user} threw {ToPrettyString(uid):entity} which spilled a solution {SolutionContainerSystem.ToPrettyString(solution):solution} on landing" ) ;
2021-12-05 04:18:30 +01:00
}
2023-01-12 16:41:40 +13:00
var drainedSolution = _solutionContainerSystem . Drain ( uid , solution , solution . Volume ) ;
2023-04-06 14:20:48 -07:00
SplashSpillAt ( uid , drainedSolution , Transform ( uid ) . Coordinates , "PuddleSmear" ) ;
}
private void SplashOnMeleeHit ( EntityUid uid , SpillableComponent component , MeleeHitEvent args )
{
// When attacking someone reactive with a spillable entity,
// splash a little on them (touch react)
// If this also has solution transfer, then assume the transfer amount is how much we want to spill.
// Otherwise let's say they want to spill a quarter of its max volume.
if ( ! _solutionContainerSystem . TryGetDrainableSolution ( uid , out var solution ) )
return ;
if ( TryComp < DrinkComponent > ( uid , out var drink ) & & ! drink . Opened )
return ;
var hitCount = args . HitEntities . Count ;
var totalSplit = FixedPoint2 . Min ( solution . MaxVolume * 0.25 , solution . Volume ) ;
if ( TryComp < SolutionTransferComponent > ( uid , out var transfer ) )
{
totalSplit = FixedPoint2 . Min ( transfer . TransferAmount , solution . Volume ) ;
}
// a little lame, but reagent quantity is not very balanced and we don't want people
// spilling like 100u of reagent on someone at once!
totalSplit = FixedPoint2 . Min ( totalSplit , component . MaxMeleeSpillAmount ) ;
foreach ( var hit in args . HitEntities )
{
if ( ! HasComp < ReactiveComponent > ( hit ) )
{
hitCount - = 1 ; // so we don't undershoot solution calculation for actual reactive entities
continue ;
}
var splitSolution = _solutionContainerSystem . SplitSolution ( uid , solution , totalSplit / hitCount ) ;
_adminLogger . Add ( LogType . MeleeHit , $"{ToPrettyString(args.User)} splashed {SolutionContainerSystem.ToPrettyString(splitSolution):solution} from {ToPrettyString(uid):entity} onto {ToPrettyString(hit):target}" ) ;
_reactive . DoEntityReaction ( hit , splitSolution , ReactionMethod . Touch ) ;
_popup . PopupEntity (
Loc . GetString ( "spill-melee-hit-attacker" , ( "amount" , totalSplit / hitCount ) , ( "spillable" , uid ) ,
( "target" , Identity . Entity ( hit , EntityManager ) ) ) ,
hit , args . User ) ;
_popup . PopupEntity (
Loc . GetString ( "spill-melee-hit-others" , ( "attacker" , args . User ) , ( "spillable" , uid ) ,
( "target" , Identity . Entity ( hit , EntityManager ) ) ) ,
hit , Filter . PvsExcept ( args . User ) , true , PopupType . SmallCaution ) ;
}
2021-12-05 04:18:30 +01:00
}
2022-02-10 15:30:59 +13:00
private void AddSpillVerb ( EntityUid uid , SpillableComponent component , GetVerbsEvent < Verb > args )
2021-12-05 04:18:30 +01:00
{
if ( ! args . CanAccess | | ! args . CanInteract )
return ;
2021-12-07 17:48:49 +01:00
if ( ! _solutionContainerSystem . TryGetDrainableSolution ( args . Target , out var solution ) )
2021-12-05 04:18:30 +01:00
return ;
2022-08-03 17:47:14 -07:00
if ( TryComp < DrinkComponent > ( args . Target , out var drink ) & & ( ! drink . Opened ) )
return ;
2023-01-12 16:41:40 +13:00
if ( solution . Volume = = FixedPoint2 . Zero )
2021-12-05 04:18:30 +01:00
return ;
Verb verb = new ( ) ;
verb . Text = Loc . GetString ( "spill-target-verb-get-data-text" ) ;
// TODO VERB ICONS spill icon? pouring out a glass/beaker?
2023-04-03 13:13:48 +12:00
verb . Act = ( ) = >
2022-10-09 15:46:08 -04:00
{
2023-04-03 13:13:48 +12:00
_doAfterSystem . TryStartDoAfter ( new DoAfterArgs ( args . User , component . SpillDelay ? ? 0 , new SpillDoAfterEvent ( ) , uid , target : uid )
2022-10-09 15:46:08 -04:00
{
2023-04-03 13:13:48 +12:00
BreakOnTargetMove = true ,
BreakOnUserMove = true ,
BreakOnDamage = true ,
NeedHand = true ,
} ) ;
} ;
2021-12-05 04:18:30 +01:00
verb . Impact = LogImpact . Medium ; // dangerous reagent reaction are logged separately.
2022-10-26 14:15:48 +13:00
verb . DoContactInteraction = true ;
2021-12-05 04:18:30 +01:00
args . Verbs . Add ( verb ) ;
}
2023-04-06 14:20:48 -07:00
/// <summary>
/// First splashes reagent on reactive entities near the spilling entity, then spills the rest regularly to a
/// puddle. This is intended for 'destructive' spills, like when entities are destroyed or thrown.
/// </summary>
public PuddleComponent ? SplashSpillAt ( EntityUid uid , Solution solution , EntityCoordinates coordinates , string prototype ,
bool overflow = true , bool sound = true , bool combine = true , EntityUid ? user = null )
{
if ( solution . Volume = = 0 )
return null ;
// Get reactive entities nearby--if there are some, it'll spill a bit on them instead.
foreach ( var ent in _entityLookup . GetComponentsInRange < ReactiveComponent > ( coordinates , 1.0f ) )
{
// sorry! no overload for returning uid, so .owner must be used
var owner = ent . Owner ;
// between 5 and 30%
var splitAmount = solution . Volume * _random . NextFloat ( 0.05f , 0.30f ) ;
var splitSolution = solution . SplitSolution ( splitAmount ) ;
if ( user ! = null )
{
_adminLogger . Add ( LogType . Landed ,
$"{ToPrettyString(user.Value):user} threw {ToPrettyString(uid):entity} which splashed a solution {SolutionContainerSystem.ToPrettyString(solution):solution} onto {ToPrettyString(owner):target}" ) ;
}
_reactive . DoEntityReaction ( owner , splitSolution , ReactionMethod . Touch ) ;
_popup . PopupEntity ( Loc . GetString ( "spill-land-spilled-on-other" , ( "spillable" , uid ) , ( "target" , owner ) ) , owner , PopupType . SmallCaution ) ;
}
return SpillAt ( solution , coordinates , prototype , overflow , sound , combine : combine ) ;
}
2021-12-05 04:18:30 +01:00
/// <summary>
/// Spills solution at the specified grid coordinates.
/// </summary>
/// <param name="solution">Initial solution for the prototype.</param>
/// <param name="coordinates">The coordinates to spill the solution at.</param>
/// <param name="prototype">The prototype to use.</param>
/// <param name="overflow">If the puddle overflow will be calculated. Defaults to true.</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 PuddleComponent ? SpillAt ( Solution solution , EntityCoordinates coordinates , string prototype ,
bool overflow = true , bool sound = true , bool combine = true )
{
2023-01-12 16:41:40 +13:00
if ( solution . Volume = = 0 ) return null ;
2021-12-05 04:18:30 +01:00
2022-06-20 12:14:35 +12:00
if ( ! _mapManager . TryGetGrid ( coordinates . GetGridUid ( EntityManager ) , out var mapGrid ) )
2021-12-05 04:18:30 +01:00
return null ; // Let's not spill to space.
return SpillAt ( mapGrid . GetTileRef ( coordinates ) , solution , prototype , overflow , sound ,
combine : combine ) ;
}
public bool TryGetPuddle ( TileRef tileRef , [ NotNullWhen ( true ) ] out PuddleComponent ? puddle )
{
2022-01-09 23:47:01 +11:00
foreach ( var entity in _entityLookup . GetEntitiesIntersecting ( tileRef ) )
2021-11-24 00:38:39 +01:00
{
2021-12-07 17:48:49 +01:00
if ( EntityManager . TryGetComponent ( entity , out PuddleComponent ? p ) )
2021-12-05 04:18:30 +01:00
{
puddle = p ;
return true ;
}
2021-11-24 00:38:39 +01:00
}
2021-12-05 04:18:30 +01:00
puddle = null ;
return false ;
2021-11-24 00:38:39 +01:00
}
2021-12-05 04:18:30 +01:00
public PuddleComponent ? SpillAt ( TileRef tileRef , Solution solution , string prototype ,
bool overflow = true , bool sound = true , bool noTileReact = false , bool combine = true )
{
2023-01-12 16:41:40 +13:00
if ( solution . Volume < = 0 ) return null ;
2021-12-05 04:18:30 +01:00
// If space return early, let that spill go out into the void
if ( tileRef . Tile . IsEmpty ) return null ;
2022-06-11 18:54:41 -07:00
var gridId = tileRef . GridUid ;
2021-12-05 04:18:30 +01:00
if ( ! _mapManager . TryGetGrid ( gridId , out var mapGrid ) ) return null ; // Let's not spill to invalid grids.
if ( ! noTileReact )
{
// First, do all tile reactions
2022-02-15 03:22:26 +01:00
for ( var i = 0 ; i < solution . Contents . Count ; i + + )
2021-12-05 04:18:30 +01:00
{
2022-02-15 03:22:26 +01:00
var ( reagentId , quantity ) = solution . Contents [ i ] ;
2021-12-05 04:18:30 +01:00
var proto = _prototypeManager . Index < ReagentPrototype > ( reagentId ) ;
2022-02-15 03:22:26 +01:00
var removed = proto . ReactionTile ( tileRef , quantity ) ;
if ( removed < = FixedPoint2 . Zero ) continue ;
solution . RemoveReagent ( reagentId , removed ) ;
2021-12-05 04:18:30 +01:00
}
}
// Tile reactions used up everything.
2023-01-12 16:41:40 +13:00
if ( solution . Volume = = FixedPoint2 . Zero )
2021-12-05 04:18:30 +01:00
return null ;
// Get normalized co-ordinate for spill location and spill it in the centre
// TODO: Does SnapGrid or something else already do this?
2022-04-24 13:54:25 +10:00
var spillGridCoords = mapGrid . GridTileToLocal ( tileRef . GridIndices ) ;
2022-02-15 03:22:26 +01:00
var startEntity = EntityUid . Invalid ;
PuddleComponent ? puddleComponent = null ;
2021-12-05 04:18:30 +01:00
if ( combine )
{
2022-02-15 03:22:26 +01:00
var spillEntities = _entityLookup . GetEntitiesIntersecting ( tileRef ) . ToArray ( ) ;
2021-12-05 04:18:30 +01:00
foreach ( var spillEntity in spillEntities )
{
2022-02-15 03:22:26 +01:00
if ( ! EntityManager . TryGetComponent ( spillEntity , out puddleComponent ) ) continue ;
2021-12-05 04:18:30 +01:00
2021-12-07 17:48:49 +01:00
if ( ! overflow & & _puddleSystem . WouldOverflow ( puddleComponent . Owner , solution , puddleComponent ) )
2021-12-05 04:18:30 +01:00
return null ;
2022-02-15 03:22:26 +01:00
if ( ! _puddleSystem . TryAddSolution ( puddleComponent . Owner , solution , sound , overflow ) ) continue ;
2021-12-05 04:18:30 +01:00
2022-02-15 03:22:26 +01:00
startEntity = puddleComponent . Owner ;
break ;
2021-12-05 04:18:30 +01:00
}
}
2022-02-15 03:22:26 +01:00
if ( startEntity ! = EntityUid . Invalid )
return puddleComponent ;
2021-12-05 04:18:30 +01:00
2022-02-15 03:22:26 +01:00
startEntity = EntityManager . SpawnEntity ( prototype , spillGridCoords ) ;
puddleComponent = EntityManager . EnsureComponent < PuddleComponent > ( startEntity ) ;
_puddleSystem . TryAddSolution ( startEntity , solution , sound , overflow ) ;
2021-12-05 04:18:30 +01:00
2022-02-15 03:22:26 +01:00
return puddleComponent ;
2021-12-05 04:18:30 +01:00
}
2022-10-09 15:46:08 -04:00
2023-02-24 19:01:25 -05:00
private void OnDoAfter ( EntityUid uid , SpillableComponent component , DoAfterEvent args )
2022-10-09 15:46:08 -04:00
{
2023-02-24 19:01:25 -05:00
if ( args . Handled | | args . Cancelled | | args . Args . Target = = null )
return ;
2022-10-09 15:46:08 -04:00
//solution gone by other means before doafter completes
2023-02-24 19:01:25 -05:00
if ( ! _solutionContainerSystem . TryGetDrainableSolution ( uid , out var solution ) | | solution . Volume = = 0 )
2022-10-09 15:46:08 -04:00
return ;
2023-02-24 19:01:25 -05:00
var puddleSolution = _solutionContainerSystem . SplitSolution ( uid , solution , solution . Volume ) ;
2022-10-09 15:46:08 -04:00
2023-02-24 19:01:25 -05:00
SpillAt ( puddleSolution , Transform ( uid ) . Coordinates , "PuddleSmear" ) ;
2022-10-09 15:46:08 -04:00
2023-02-24 19:01:25 -05:00
args . Handled = true ;
2022-10-09 15:46:08 -04:00
}
2021-11-24 00:38:39 +01:00
}