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-01-14 03:21:30 +13:00
using Content.Server.Tools ;
2022-07-16 21:50:57 -05:00
using Content.Shared.Database ;
2022-01-14 03:21:30 +13:00
using Content.Shared.Interaction ;
2022-01-29 22:45:57 +11:00
using Robust.Shared.Map ;
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 ! ;
2022-01-14 03:21:30 +13:00
[Dependency] private readonly ToolSystem _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 ) ;
SubscribeLocalEvent < CableComponent , CuttingFinishedEvent > ( 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 ;
2022-01-19 11:17:46 +13:00
var ev = new CuttingFinishedEvent ( args . User ) ;
_toolSystem . UseTool ( args . Used , args . User , uid , 0 , cable . CuttingDelay , new [ ] { cable . CuttingQuality } , doAfterCompleteEvent : ev , doAfterEventTarget : uid ) ;
2022-01-14 03:21:30 +13:00
args . Handled = true ;
}
private void OnCableCut ( EntityUid uid , CableComponent cable , CuttingFinishedEvent args )
{
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
}
}
2022-02-16 00:23:23 -07:00
public sealed class CuttingFinishedEvent : EntityEventArgs
2022-01-14 03:21:30 +13:00
{
public EntityUid User ;
2022-01-19 11:17:46 +13:00
public CuttingFinishedEvent ( EntityUid user )
2022-01-14 03:21:30 +13:00
{
User = user ;
}
}