2024-08-09 18:24:19 +12:00
using Content.Shared.Administration.Logs ;
2024-03-05 03:13:50 +00:00
using Content.Shared.Chemistry.Components ;
2024-08-09 18:24:19 +12:00
using Content.Shared.Database ;
2021-11-03 16:48:03 -07:00
using Content.Shared.FixedPoint ;
2023-03-24 05:00:38 +00:00
using Content.Shared.Popups ;
2024-04-20 13:51:56 +00:00
using Robust.Shared.Network ;
2023-07-09 21:24:30 +02:00
using Robust.Shared.Random ;
2021-09-06 15:49:44 +02:00
2024-03-05 03:13:50 +00:00
namespace Content.Shared.Chemistry.EntitySystems ;
2023-03-24 05:00:38 +00:00
public sealed class RehydratableSystem : EntitySystem
2021-09-06 15:49:44 +02:00
{
2024-04-20 13:51:56 +00:00
[Dependency] private readonly INetManager _net = default ! ;
2023-07-09 21:24:30 +02:00
[Dependency] private readonly IRobustRandom _random = default ! ;
2024-03-05 03:13:50 +00:00
[Dependency] private readonly SharedPopupSystem _popup = default ! ;
[Dependency] private readonly SharedSolutionContainerSystem _solutions = default ! ;
[Dependency] private readonly SharedTransformSystem _xform = default ! ;
2024-08-09 18:24:19 +12:00
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default ! ;
2023-03-24 05:00:38 +00:00
public override void Initialize ( )
2021-09-06 15:49:44 +02:00
{
2023-03-24 05:00:38 +00:00
base . Initialize ( ) ;
2021-09-06 15:49:44 +02:00
2023-12-29 04:47:43 -08:00
SubscribeLocalEvent < RehydratableComponent , SolutionContainerChangedEvent > ( OnSolutionChange ) ;
2023-03-24 05:00:38 +00:00
}
2021-09-06 15:49:44 +02:00
2024-03-05 03:13:50 +00:00
private void OnSolutionChange ( Entity < RehydratableComponent > ent , ref SolutionContainerChangedEvent args )
2023-03-24 05:00:38 +00:00
{
2024-03-05 03:13:50 +00:00
var quantity = _solutions . GetTotalPrototypeQuantity ( ent , ent . Comp . CatalystPrototype ) ;
2024-08-09 18:24:19 +12:00
_adminLogger . Add ( LogType . Action , LogImpact . Medium , $"{ToPrettyString(ent.Owner)} was hydrated, now contains a solution of: {SharedSolutionContainerSystem.ToPrettyString(args.Solution)}." ) ;
2024-03-05 03:13:50 +00:00
if ( quantity ! = FixedPoint2 . Zero & & quantity > = ent . Comp . CatalystMinimum )
2021-09-06 15:49:44 +02:00
{
2024-03-05 03:13:50 +00:00
Expand ( ent ) ;
2021-09-06 15:49:44 +02:00
}
2023-03-24 05:00:38 +00:00
}
2021-09-06 15:49:44 +02:00
2023-03-24 05:00:38 +00:00
// Try not to make this public if you can help it.
2024-03-05 03:13:50 +00:00
private void Expand ( Entity < RehydratableComponent > ent )
2023-03-24 05:00:38 +00:00
{
2024-04-20 13:51:56 +00:00
if ( _net . IsClient )
return ;
2024-03-05 03:13:50 +00:00
var ( uid , comp ) = ent ;
2023-03-24 05:00:38 +00:00
2023-07-09 21:24:30 +02:00
var randomMob = _random . Pick ( comp . PossibleSpawns ) ;
var target = Spawn ( randomMob , Transform ( uid ) . Coordinates ) ;
2024-08-09 18:24:19 +12:00
_adminLogger . Add ( LogType . Action , LogImpact . Medium , $"{ToPrettyString(ent.Owner)} has been hydrated correctly and spawned: {ToPrettyString(target)}." ) ;
2024-03-05 03:13:50 +00:00
_popup . PopupEntity ( Loc . GetString ( "rehydratable-component-expands-message" , ( "owner" , uid ) ) , target ) ;
2023-07-09 21:24:30 +02:00
2024-03-05 03:13:50 +00:00
_xform . AttachToGridOrMap ( target ) ;
2023-03-24 05:00:38 +00:00
var ev = new GotRehydratedEvent ( target ) ;
RaiseLocalEvent ( uid , ref ev ) ;
// prevent double hydration while queued
RemComp < RehydratableComponent > ( uid ) ;
QueueDel ( uid ) ;
2021-09-06 15:49:44 +02:00
}
}