2023-10-05 20:53:53 +01:00
|
|
|
using System.Linq;
|
2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2023-02-06 00:03:53 -05:00
|
|
|
using Content.Shared.Anomaly.Components;
|
|
|
|
|
using Content.Shared.Anomaly.Effects.Components;
|
|
|
|
|
using Content.Shared.Physics;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
using Robust.Shared.Physics;
|
|
|
|
|
using Robust.Shared.Physics.Components;
|
2023-10-05 20:53:53 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
2023-02-06 00:03:53 -05:00
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Anomaly.Effects;
|
|
|
|
|
|
2023-05-06 01:08:37 +12:00
|
|
|
public sealed class EntityAnomalySystem : EntitySystem
|
2023-02-06 00:03:53 -05:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IMapManager _map = default!;
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
2023-05-06 01:08:37 +12:00
|
|
|
SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalyPulseEvent>(OnPulse);
|
|
|
|
|
SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalySupercriticalEvent>(OnSupercritical);
|
2023-11-06 04:41:42 +02:00
|
|
|
SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalyStabilityChangedEvent>(OnStabilityChanged);
|
2023-02-06 00:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-06 01:08:37 +12:00
|
|
|
private void OnPulse(EntityUid uid, EntitySpawnAnomalyComponent component, ref AnomalyPulseEvent args)
|
2023-02-06 00:03:53 -05:00
|
|
|
{
|
2023-11-06 04:41:42 +02:00
|
|
|
if (!component.SpawnOnPulse)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
var range = component.SpawnRange * args.Stability;
|
|
|
|
|
var amount = (int) (component.MaxSpawnAmount * args.Severity + 0.5f);
|
|
|
|
|
|
|
|
|
|
var xform = Transform(uid);
|
2023-11-06 04:41:42 +02:00
|
|
|
SpawnEntitesOnOpenTiles(component, xform, amount, range, component.Spawns);
|
2023-02-06 00:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-06 01:08:37 +12:00
|
|
|
private void OnSupercritical(EntityUid uid, EntitySpawnAnomalyComponent component, ref AnomalySupercriticalEvent args)
|
2023-02-06 00:03:53 -05:00
|
|
|
{
|
2023-11-06 04:41:42 +02:00
|
|
|
if (!component.SpawnOnSuperCritical)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
var xform = Transform(uid);
|
2023-11-06 04:41:42 +02:00
|
|
|
// A cluster of entities
|
|
|
|
|
SpawnEntitesOnOpenTiles(component, xform, component.MaxSpawnAmount, component.SpawnRange, component.Spawns);
|
2023-06-04 07:31:10 +12:00
|
|
|
// And so much meat (for the meat anomaly at least)
|
2023-11-06 04:41:42 +02:00
|
|
|
SpawnEntitesOnOpenTiles(component, xform, component.MaxSpawnAmount, component.SpawnRange, component.SuperCriticalSpawns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStabilityChanged(EntityUid uid, EntitySpawnAnomalyComponent component, ref AnomalyStabilityChangedEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (!component.SpawnOnStabilityChanged)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var range = component.SpawnRange * args.Stability;
|
|
|
|
|
var amount = (int) (component.MaxSpawnAmount * args.Stability + 0.5f);
|
|
|
|
|
|
|
|
|
|
var xform = Transform(uid);
|
|
|
|
|
SpawnEntitesOnOpenTiles(component, xform, amount, range, component.Spawns);
|
2023-02-06 00:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2023-11-06 04:41:42 +02:00
|
|
|
private void SpawnEntitesOnOpenTiles(EntitySpawnAnomalyComponent component, TransformComponent xform, int amount, float radius, List<EntProtoId> spawns)
|
2023-02-06 00:03:53 -05:00
|
|
|
{
|
2023-04-24 21:13:44 -04:00
|
|
|
if (!component.Spawns.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
if (!_map.TryGetGrid(xform.GridUid, out var grid))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var localpos = xform.Coordinates.Position;
|
|
|
|
|
var tilerefs = grid.GetLocalTilesIntersecting(
|
2023-07-08 14:08:32 +10:00
|
|
|
new Box2(localpos + new Vector2(-radius, -radius), localpos + new Vector2(radius, radius))).ToArray();
|
2023-04-24 21:13:44 -04:00
|
|
|
|
|
|
|
|
if (tilerefs.Length == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
_random.Shuffle(tilerefs);
|
|
|
|
|
var physQuery = GetEntityQuery<PhysicsComponent>();
|
|
|
|
|
var amountCounter = 0;
|
|
|
|
|
foreach (var tileref in tilerefs)
|
|
|
|
|
{
|
|
|
|
|
var valid = true;
|
|
|
|
|
foreach (var ent in grid.GetAnchoredEntities(tileref.GridIndices))
|
|
|
|
|
{
|
|
|
|
|
if (!physQuery.TryGetComponent(ent, out var body))
|
|
|
|
|
continue;
|
2023-10-05 20:53:53 +01:00
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
if (body.BodyType != BodyType.Static ||
|
|
|
|
|
!body.Hard ||
|
|
|
|
|
(body.CollisionLayer & (int) CollisionGroup.Impassable) == 0)
|
|
|
|
|
continue;
|
2023-10-05 20:53:53 +01:00
|
|
|
|
2023-02-06 00:03:53 -05:00
|
|
|
valid = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!valid)
|
|
|
|
|
continue;
|
|
|
|
|
amountCounter++;
|
2023-06-04 07:31:10 +12:00
|
|
|
Spawn(_random.Pick(spawns), tileref.GridIndices.ToEntityCoordinates(xform.GridUid.Value, _map));
|
2023-02-06 00:03:53 -05:00
|
|
|
if (amountCounter >= amount)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|