Files
tbd-station-14/Content.Server/StationEvents/Events/PowerGridCheckRule.cs
chromiumboy 1de682e23f Power monitoring console overhaul (#20927)
* Prototyping whole station wire map

* More prototyping

* Added icons for the different power distributors and toggleable cable displays

* Power cable layouts are now only sent to the client when the power monitor is open

* UI prototyping

* Power monitors can now see the sprites of distant entities, long entity names are truncated

* Updated how network devices are added to the player's PVS

* More feature prototypes

* Added source / load symbols

* Final prototype! Time to actually code it properly...

* Start of code clean up

* Continuing code clean up

* Fixed UI appearance

* Code clean up complete

* Removed unnecessary changes

* Updated how power values are calculated, added UI warnings for power sinks and power net checks

* Updated how power values are calculated again, added support for portable generators

* Removed unnecessary files

* Map beacons start toggled off, console map now works outside the station, fixed substation icon

* Made some of Sloth's requested changes. Power distributors don't blink anymore, unless selected

* Moved a number of static variables in PowerMonitoringHelper to sensible places in the main files. Added a NavMapTrackableComponent so that you can specify how individual entities appear on the navmap

* Updated the colors/positions of HV cables and SMESes to improve contrast

* Fixed SMES color in map legend

* Partially fixed auto-scrolling on device selection, made sublists alphabetical

* Changed how auto-scroll is handled

* Changed the font color of the console warning messages

* Reduced the font size of beacon labels

* Added the station name to the console

* Organized references

* Removed unwanted changes to RobustToolbox

* Fix merge conflict

* Fix merge conflict, maybe

* Fix merge conflict

* Updated outdated reference

* Fixed portable_generator.yml

* Implemented a number of requested changes, move bit masks to a shared component

* Navigate listings via the navmap

* First attempt at improving efficiency

* Second attempt at optimization, entity grouping added for solar panels

* Finished solar panel entity joining

* Finished major revisions, code clean up needed

* Finializing optimizations

* Made requested changes

* Bug fix, removed obsolete code

* Bug fixes

* Bug fixes

* STarted revisions

* Further revisions

* More revision

* Finalizing revisions. Need to make RT PR

* Code tidying

* More code tidying

* Trying to avoid merge conflicts

* Trying to avoid merge conflicts

* Removed use of PVS

* Improving efficiency

* Addressed a bunch of outstanding issues

* Clear old data on console refresh

* UI adjustments

* Made node comparison more robust. More devices can be combined into one entry

* Added missing component 'dirty'
2023-12-24 17:07:41 +11:00

97 lines
3.8 KiB
C#

using System.Threading;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Station.Components;
using Content.Server.StationEvents.Components;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
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);
if (!TryGetRandomStation(out var chosenStation))
return;
component.AffectedStation = chosenStation.Value;
var query = AllEntityQuery<ApcComponent, TransformComponent>();
while (query.MoveNext(out var apcUid ,out var apc, out var transform))
{
if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
component.Powered.Add(apcUid);
}
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))
{
if(!apcComponent.MainBreakerEnabled)
_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);
}
}
}
}