2023-04-25 20:23:14 -04:00
using Content.Server.Power.Components ;
using JetBrains.Annotations ;
using Robust.Shared.Audio ;
using Robust.Shared.Player ;
using Robust.Shared.Utility ;
using System.Threading ;
using Content.Server.Power.EntitySystems ;
using Timer = Robust . Shared . Timing . Timer ;
2023-08-06 13:39:48 -05:00
using System.Linq ;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components ;
2023-08-06 13:39:48 -05:00
using Robust.Shared.Random ;
2023-04-25 20:23:14 -04:00
using Content.Server.Station.Components ;
using Content.Server.StationEvents.Components ;
namespace Content.Server.StationEvents.Events
{
[UsedImplicitly]
public sealed class PowerGridCheckRule : StationEventSystem < PowerGridCheckRuleComponent >
{
[Dependency] private readonly ApcSystem _apcSystem = default ! ;
protected override void Started ( EntityUid uid , PowerGridCheckRuleComponent component , GameRuleComponent gameRule , GameRuleStartedEvent args )
{
base . Started ( uid , component , gameRule , args ) ;
2023-05-19 15:45:09 -05:00
if ( ! TryGetRandomStation ( out var chosenStation ) )
2023-04-25 20:23:14 -04:00
return ;
2023-08-06 13:39:48 -05:00
foreach ( var ( apc , transform ) in EntityQuery < ApcComponent , TransformComponent > ( true ) )
2023-04-25 20:23:14 -04:00
{
if ( apc . MainBreakerEnabled & & CompOrNull < StationMemberComponent > ( transform . GridUid ) ? . Station = = chosenStation )
2023-08-06 13:39:48 -05:00
component . Powered . Add ( apc . Owner ) ;
2023-04-25 20:23:14 -04:00
}
RobustRandom . Shuffle ( component . Powered ) ;
component . NumberPerSecond = Math . Max ( 1 , ( int ) ( component . Powered . Count / component . SecondsUntilOff ) ) ; // Number of APCs to turn off every second. At least one.
}
protected override void Ended ( EntityUid uid , PowerGridCheckRuleComponent component , GameRuleComponent gameRule , GameRuleEndedEvent args )
{
base . Ended ( uid , component , gameRule , args ) ;
foreach ( var entity in component . Unpowered )
{
if ( Deleted ( entity ) )
continue ;
if ( TryComp ( entity , out ApcComponent ? apcComponent ) )
{
2023-08-06 13:39:48 -05:00
if ( ! apcComponent . MainBreakerEnabled )
2023-04-25 20:23:14 -04:00
_apcSystem . ApcToggleBreaker ( entity , apcComponent ) ;
}
}
// Can't use the default EndAudio
component . AnnounceCancelToken ? . Cancel ( ) ;
component . AnnounceCancelToken = new CancellationTokenSource ( ) ;
Timer . Spawn ( 3000 , ( ) = >
{
Audio . PlayGlobal ( "/Audio/Announcements/power_on.ogg" , Filter . Broadcast ( ) , true , AudioParams . Default . WithVolume ( - 4f ) ) ;
} , component . AnnounceCancelToken . Token ) ;
component . Unpowered . Clear ( ) ;
}
protected override void ActiveTick ( EntityUid uid , PowerGridCheckRuleComponent component , GameRuleComponent gameRule , float frameTime )
{
base . ActiveTick ( uid , component , gameRule , frameTime ) ;
var updates = 0 ;
component . FrameTimeAccumulator + = frameTime ;
if ( component . FrameTimeAccumulator > component . UpdateRate )
{
updates = ( int ) ( component . FrameTimeAccumulator / component . UpdateRate ) ;
component . FrameTimeAccumulator - = component . UpdateRate * updates ;
}
for ( var i = 0 ; i < updates ; i + + )
{
if ( component . Powered . Count = = 0 )
break ;
var selected = component . Powered . Pop ( ) ;
if ( Deleted ( selected ) )
continue ;
if ( TryComp < ApcComponent > ( selected , out var apcComponent ) )
{
if ( apcComponent . MainBreakerEnabled )
_apcSystem . ApcToggleBreaker ( selected , apcComponent ) ;
}
component . Unpowered . Add ( selected ) ;
}
}
}
}