Files
tbd-station-14/Content.Server/Spawners/EntitySystems/ConditionalSpawnerSystem.cs
Kara b9a0894d7c Event refactor (#9589)
* Station event refactor

* Remove clientside `IStationEventManager`

we can just use prototypes

* Basic API idea

* Cruft

* first attempt at epicness

* okay yeah this shit is really clean

* sort out minor stuff

* Convert `BreakerFlip`

* `BureaucraticError` + general cleanup

* `DiseaseOutbreak`

* `FalseAlarm`

* `GasLeak`

* `KudzuGrowth`

* `MeteorSwarm`

* `MouseMigration`

* misc errors

* `PowerGridCheck`

* `RandomSentience`

* `VentClog`

* `VentCritters`

* `ZombieOutbreak`

* Rewrite basic event scheduler

* Minor fixes and logging

* ooooops

* errors + fix

* linter

* completions, `RuleStarted` property, update loop fixes

* Tweaks

* Fix #9462

* Basic scheduler update fix, and fixes #8174

* Add test

* UI cleanup

* really this was just for testing
2022-07-10 20:48:41 -05:00

108 lines
3.7 KiB
C#

using Content.Server.GameTicking;
using Content.Server.Spawners.Components;
using JetBrains.Annotations;
using Robust.Shared.Random;
namespace Content.Server.Spawners.EntitySystems
{
[UsedImplicitly]
public sealed class ConditionalSpawnerSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly GameTicker _ticker = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GameRuleStartedEvent>(OnRuleStarted);
SubscribeLocalEvent<ConditionalSpawnerComponent, MapInitEvent>(OnCondSpawnMapInit);
SubscribeLocalEvent<RandomSpawnerComponent, MapInitEvent>(OnRandSpawnMapInit);
}
private void OnCondSpawnMapInit(EntityUid uid, ConditionalSpawnerComponent component, MapInitEvent args)
{
TrySpawn(component);
}
private void OnRandSpawnMapInit(EntityUid uid, RandomSpawnerComponent component, MapInitEvent args)
{
Spawn(component);
EntityManager.QueueDeleteEntity(uid);
}
private void OnRuleStarted(GameRuleStartedEvent args)
{
foreach (var spawner in EntityManager.EntityQuery<ConditionalSpawnerComponent>())
{
RuleStarted(spawner, args);
}
}
public void RuleStarted(ConditionalSpawnerComponent component, GameRuleStartedEvent obj)
{
if(component.GameRules.Contains(obj.Rule.ID))
Spawn(component);
}
private void TrySpawn(ConditionalSpawnerComponent component)
{
if (component.GameRules.Count == 0)
{
Spawn(component);
return;
}
foreach (var rule in component.GameRules)
{
if (!_ticker.IsGameRuleStarted(rule)) continue;
Spawn(component);
return;
}
}
private void Spawn(ConditionalSpawnerComponent component)
{
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
Logger.Warning($"Prototype list in ConditionalSpawnComponent is empty! Entity: {component.Owner}");
return;
}
if (!Deleted(component.Owner))
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), Transform(component.Owner).Coordinates);
}
private void Spawn(RandomSpawnerComponent component)
{
if (component.RarePrototypes.Count > 0 && (component.RareChance == 1.0f || _robustRandom.Prob(component.RareChance)))
{
EntityManager.SpawnEntity(_robustRandom.Pick(component.RarePrototypes), Transform(component.Owner).Coordinates);
return;
}
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
Logger.Warning($"Prototype list in RandomSpawnerComponent is empty! Entity: {component.Owner}");
return;
}
if (Deleted(component.Owner)) return;
var offset = component.Offset;
var xOffset = _robustRandom.NextFloat(-offset, offset);
var yOffset = _robustRandom.NextFloat(-offset, offset);
var coordinates = Transform(component.Owner).Coordinates.Offset(new Vector2(xOffset, yOffset));
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), coordinates);
}
}
}