2022-07-16 21:50:57 -05:00
using Content.Server.Administration.Logs ;
2022-01-14 03:21:30 +13:00
using Content.Server.Electrocution ;
using Content.Server.Power.Components ;
2022-01-29 22:45:57 +11:00
using Content.Server.Stack ;
2022-07-16 21:50:57 -05:00
using Content.Shared.Database ;
2023-04-03 13:13:48 +12:00
using Content.Shared.DoAfter ;
2022-01-14 03:21:30 +13:00
using Content.Shared.Interaction ;
2023-02-24 19:01:25 -05:00
using Content.Shared.Tools ;
using Content.Shared.Tools.Components ;
2022-01-29 22:45:57 +11:00
using Robust.Shared.Map ;
2023-10-24 00:20:33 +11:00
using CableCuttingFinishedEvent = Content . Shared . Tools . Systems . CableCuttingFinishedEvent ;
using SharedToolSystem = Content . Shared . Tools . Systems . SharedToolSystem ;
2022-01-14 03:21:30 +13:00
namespace Content.Server.Power.EntitySystems ;
2022-01-29 22:45:57 +11:00
public sealed partial class CableSystem : EntitySystem
2022-01-14 03:21:30 +13:00
{
2022-01-29 22:45:57 +11:00
[Dependency] private readonly IMapManager _mapManager = default ! ;
[Dependency] private readonly ITileDefinitionManager _tileManager = default ! ;
2023-02-24 19:01:25 -05:00
[Dependency] private readonly SharedToolSystem _toolSystem = default ! ;
2022-01-29 22:45:57 +11:00
[Dependency] private readonly StackSystem _stack = default ! ;
2022-01-14 03:21:30 +13:00
[Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default ! ;
2022-07-16 21:50:57 -05:00
[Dependency] private readonly IAdminLogManager _adminLogs = default ! ;
2022-01-14 03:21:30 +13:00
public override void Initialize ( )
{
base . Initialize ( ) ;
2022-01-29 22:45:57 +11:00
InitializeCablePlacer ( ) ;
2022-01-14 03:21:30 +13:00
SubscribeLocalEvent < CableComponent , InteractUsingEvent > ( OnInteractUsing ) ;
2023-04-03 13:13:48 +12:00
SubscribeLocalEvent < CableComponent , CableCuttingFinishedEvent > ( OnCableCut ) ;
2022-04-24 13:54:25 +10:00
// Shouldn't need re-anchoring.
2022-01-14 03:21:30 +13:00
SubscribeLocalEvent < CableComponent , AnchorStateChangedEvent > ( OnAnchorChanged ) ;
}
private void OnInteractUsing ( EntityUid uid , CableComponent cable , InteractUsingEvent args )
{
if ( args . Handled )
return ;
2023-04-03 13:13:48 +12:00
args . Handled = _toolSystem . UseTool ( args . Used , args . User , uid , cable . CuttingDelay , cable . CuttingQuality , new CableCuttingFinishedEvent ( ) ) ;
2022-01-14 03:21:30 +13:00
}
2023-04-03 13:13:48 +12:00
private void OnCableCut ( EntityUid uid , CableComponent cable , DoAfterEvent args )
2022-01-14 03:21:30 +13:00
{
2023-04-03 13:13:48 +12:00
if ( args . Cancelled )
return ;
2022-01-14 03:21:30 +13:00
if ( _electrocutionSystem . TryDoElectrifiedAct ( uid , args . User ) )
return ;
2022-07-16 21:50:57 -05:00
_adminLogs . Add ( LogType . CableCut , LogImpact . Medium , $"The {ToPrettyString(uid)} at {Transform(uid).Coordinates} was cut by {ToPrettyString(args.User)}." ) ;
2022-01-14 03:21:30 +13:00
Spawn ( cable . CableDroppedOnCutPrototype , Transform ( uid ) . Coordinates ) ;
2022-01-15 05:26:52 +13:00
QueueDel ( uid ) ;
2022-01-14 03:21:30 +13:00
}
private void OnAnchorChanged ( EntityUid uid , CableComponent cable , ref AnchorStateChangedEvent args )
{
if ( args . Anchored )
return ; // huh? it wasn't anchored?
2022-01-15 05:26:52 +13:00
// anchor state can change as a result of deletion (detach to null).
// We don't want to spawn an entity when deleted.
if ( ! TryLifeStage ( uid , out var life ) | | life > = EntityLifeStage . Terminating )
return ;
2022-01-14 03:21:30 +13:00
// This entity should not be un-anchorable. But this can happen if the grid-tile is deleted (RCD, explosion,
// etc). In that case: behave as if the cable had been cut.
Spawn ( cable . CableDroppedOnCutPrototype , Transform ( uid ) . Coordinates ) ;
2022-01-15 05:26:52 +13:00
QueueDel ( uid ) ;
2022-01-14 03:21:30 +13:00
}
}