Files
wwdpublic/Content.Server/Spawners/EntitySystems/ConditionalSpawnerSystem.cs
VMSolidus 08822e34ba Cherry-Pick Antag Refactor (#734)
This cherry-pick's Wizden's Antag Refactor, which is needed for all
future antag updates, as well as for me to cherry-pick over Cultists(Who
are going to need some editing to fit the antag refactor), Changelings,
Heretics, and Wizards. I actually selected the White-Dream-Public
version of the Antag Refactor, due to it having commits made tailored to
our repository, so it comes prepackaged with all the changes necessary
for our repo-specific content.

https://github.com/frosty-dev/ss14-wwdp/pull/10

Signed-off-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: ThereDrD <88589686+ThereDrD0@users.noreply.github.com>
Co-authored-by: Jeff <velcroboy333@hotmail.com>
Co-authored-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: Timemaster99 <elijahrobot@gmail.com>
Co-authored-by: luckywill339@gmail.com <luckywill339@gmail.com>
Co-authored-by: Danger Revolution! <142105406+DangerRevolution@users.noreply.github.com>
Co-authored-by: Azzy <azzydev@icloud.com>
2024-08-15 10:11:48 -07:00

115 lines
4.0 KiB
C#

using System.Numerics;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Components;
using Content.Server.GameTicking.Rules.Components;
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(uid, component);
}
private void OnRandSpawnMapInit(EntityUid uid, RandomSpawnerComponent component, MapInitEvent args)
{
Spawn(uid, component);
if (component.DeleteSpawnerAfterSpawn)
QueueDel(uid);
}
private void OnRuleStarted(ref GameRuleStartedEvent args)
{
var query = EntityQueryEnumerator<ConditionalSpawnerComponent>();
while (query.MoveNext(out var uid, out var spawner))
{
RuleStarted(uid, spawner, args);
}
}
public void RuleStarted(EntityUid uid, ConditionalSpawnerComponent component, GameRuleStartedEvent obj)
{
if (component.GameRules.Contains(obj.RuleId))
Spawn(uid, component);
}
private void TrySpawn(EntityUid uid, ConditionalSpawnerComponent component)
{
if (component.GameRules.Count == 0)
{
Spawn(uid, component);
return;
}
foreach (var rule in component.GameRules)
{
if (!_ticker.IsGameRuleActive(rule))
continue;
Spawn(uid, component);
return;
}
}
private void Spawn(EntityUid uid, ConditionalSpawnerComponent component)
{
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
Log.Warning($"Prototype list in ConditionalSpawnComponent is empty! Entity: {ToPrettyString(uid)}");
return;
}
if (!Deleted(uid))
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), Transform(uid).Coordinates);
}
private void Spawn(EntityUid uid, RandomSpawnerComponent component)
{
if (component.RarePrototypes.Count > 0 && (component.RareChance == 1.0f || _robustRandom.Prob(component.RareChance)))
{
EntityManager.SpawnEntity(_robustRandom.Pick(component.RarePrototypes), Transform(uid).Coordinates);
return;
}
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
Log.Warning($"Prototype list in RandomSpawnerComponent is empty! Entity: {ToPrettyString(uid)}");
return;
}
if (Deleted(uid))
return;
var offset = component.Offset;
var xOffset = _robustRandom.NextFloat(-offset, offset);
var yOffset = _robustRandom.NextFloat(-offset, offset);
var coordinates = Transform(uid).Coordinates.Offset(new Vector2(xOffset, yOffset));
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), coordinates);
}
}
}