Files
crystall-punk-14/Content.Server/StationEvents/Events/RadiationStorm.cs
Moony 36181334b5 StationSystem/jobs/partial spawning refactor (#7580)
* Partial work on StationSystem refactor.

* WIP station jobs API.

* forgor to fire off grid events.

* Partial implementation of StationSpawningSystem

* whoops infinite loop.

* Spawners should work now.

* it compiles.

* tfw

* Vestigial code cleanup.

* fix station deletion.

* attempt to make tests go brr

* add latejoin spawnpoints to test maps.

* make sure the station still exists while destructing spawners.

* forgot an exists check.

* destruction order check.

* hopefully fix final test.

* fail-safe radstorm.

* Deep-clean job code further. This is bugged!!!!!

* Fix job bug. (init order moment)

* whooo cleanup

* New job selection algorithm that tries to distribute fairly across stations.

* small nitpicks

* Give the heads their weights to replace the head field.

* make overflow assign take a station list.

* moment

* Fixes and test #1 of many.

* please fix nullspace

* AssignJobs should no longer even consider showing up on a trace.

* add comment.

* Introduce station configs, praying i didn't miss something.

* in one small change stations are now fully serializable.

* Further doc comments.

* whoops.

* Solve bug where assignjobs didn't account for roundstart.

* Fix spawning, improve the API.
Caught an oversight in stationsystem that should've broke everything but didn't, whoops.

* Goodbye JobController.

* minor fix..

* fix test fail, remove debug logs.

* quick serialization fixes.

* fixes..

* sus

* partialing

* Update Content.Server/Station/Systems/StationJobsSystem.Roundstart.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Use dirtying to avoid rebuilding the list 2,100 times.

* add a bajillion more lines of docs (mostly in AssignJobs so i don't ever forget how it works)

* Update Content.IntegrationTests/Tests/Station/StationJobsTest.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Add the Mysteriously Missing Captain Check.

* Put maprender back the way it belongs.

* I love addressing reviews.

* Update Content.Server/Station/Systems/StationJobsSystem.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* doc cleanup.

* Fix bureaucratic error, add job slot tests.

* zero cost abstractions when

* cri

* saner error.

* Fix spawning failing certain tests due to gameticker not handling falliability correctly.
Can't fix this until I refactor the rest of spawning code.

* submodule gaming

* Packedenger.

* Documentation consistency.

Co-authored-by: Kara <lunarautomaton6@gmail.com>
2022-05-10 13:43:30 -05:00

139 lines
4.7 KiB
C#

using System.Linq;
using Content.Server.Radiation;
using Content.Server.Station.Components;
using Content.Server.Station.Systems;
using Content.Shared.Coordinates;
using Content.Shared.Sound;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Random;
namespace Content.Server.StationEvents.Events
{
[UsedImplicitly]
public sealed class RadiationStorm : StationEvent
{
// Based on Goonstation style radiation storm with some TG elements (announcer, etc.)
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
private StationSystem _stationSystem = default!;
public override string Name => "RadiationStorm";
public override string StartAnnouncement => Loc.GetString("station-event-radiation-storm-start-announcement");
protected override string EndAnnouncement => Loc.GetString("station-event-radiation-storm-end-announcement");
public override SoundSpecifier? StartAudio => new SoundPathSpecifier("/Audio/Announcements/radiation.ogg");
protected override float StartAfter => 10.0f;
// Event specific details
private float _timeUntilPulse;
private const float MinPulseDelay = 0.2f;
private const float MaxPulseDelay = 0.8f;
private EntityUid _target = EntityUid.Invalid;
private void ResetTimeUntilPulse()
{
_timeUntilPulse = _robustRandom.NextFloat() * (MaxPulseDelay - MinPulseDelay) + MinPulseDelay;
}
public override void Announce()
{
base.Announce();
EndAfter = _robustRandom.Next(30, 80) + StartAfter; // We want to be forgiving about the radstorm.
}
public override void Startup()
{
_entityManager.EntitySysManager.Resolve(ref _stationSystem);
ResetTimeUntilPulse();
if (_stationSystem.Stations.Count == 0)
{
Running = false;
return;
}
_target = _robustRandom.Pick(_stationSystem.Stations);
base.Startup();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!Started || !Running) return;
if (_target.Valid == false)
{
Running = false;
return;
}
_timeUntilPulse -= frameTime;
if (_timeUntilPulse <= 0.0f)
{
var mapManager = IoCManager.Resolve<IMapManager>();
// Account for split stations by just randomly picking a piece of it.
var possibleTargets = _entityManager.GetComponent<StationDataComponent>(_target).Grids;
if (possibleTargets.Count == 0)
{
Running = false;
return;
}
var stationEnt = _robustRandom.Pick(possibleTargets);
if (!_entityManager.TryGetComponent<IMapGridComponent>(stationEnt, out var grid))
return;
if (mapManager.IsGridPaused(grid.GridIndex))
return;
SpawnPulse(grid.Grid);
}
}
private void SpawnPulse(IMapGrid mapGrid)
{
if (!TryFindRandomGrid(mapGrid, out var coordinates))
return;
var pulse = _entityManager.SpawnEntity("RadiationPulse", coordinates);
_entityManager.GetComponent<RadiationPulseComponent>(pulse).DoPulse();
ResetTimeUntilPulse();
}
public void SpawnPulseAt(EntityCoordinates at)
{
var pulse = IoCManager.Resolve<IEntityManager>()
.SpawnEntity("RadiationPulse", at);
_entityManager.GetComponent<RadiationPulseComponent>(pulse).DoPulse();
}
private bool TryFindRandomGrid(IMapGrid mapGrid, out EntityCoordinates coordinates)
{
if (!mapGrid.Index.IsValid())
{
coordinates = default;
return false;
}
var bounds = mapGrid.LocalBounds;
var randomX = _robustRandom.Next((int) bounds.Left, (int) bounds.Right);
var randomY = _robustRandom.Next((int) bounds.Bottom, (int) bounds.Top);
coordinates = mapGrid.ToCoordinates(randomX, randomY);
// TODO: Need to get valid tiles? (maybe just move right if the tile we chose is invalid?)
if (!coordinates.IsValid(_entityManager))
{
coordinates = default;
return false;
}
return true;
}
}
}