mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-10 19:01:13 +03:00
* Resolve StationEvent warnings * fix misuses of ent after rename, paracusia flip * oh god more rules * comments, whitespace, unnecessary precision * comments, NotNull->IsNull oops * more consistent ent vs. ent.Owner, whitespace * Greytide: "with accress from... access groups"
55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using Content.Server.StationEvents.Components;
|
|
using Content.Server.Storage.EntitySystems;
|
|
using Content.Shared.GameTicking.Components;
|
|
using Content.Shared.Station.Components;
|
|
using Content.Shared.Storage.Components;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Random;
|
|
|
|
namespace Content.Server.StationEvents.Events;
|
|
|
|
/// <summary>
|
|
/// Handler for events that spawn an entity inside of another random entity with <see cref="EntityStorageComponent"/>.
|
|
/// </summary>
|
|
/// <seealso cref="RandomEntityStorageSpawnRuleComponent"/>
|
|
public sealed partial class RandomEntityStorageSpawnRule : StationEventSystem<RandomEntityStorageSpawnRuleComponent>
|
|
{
|
|
[Dependency] private EntityStorageSystem _entityStorage = default!;
|
|
|
|
protected override void Started(Entity<RandomEntityStorageSpawnRuleComponent, GameRuleComponent> ent, ref GameRuleStartedEvent args)
|
|
{
|
|
base.Started(ent, ref args);
|
|
|
|
if (!Station.TryGetRandomStation<StationEventEligibleComponent>(out var station))
|
|
return;
|
|
|
|
var spawnRule = ent.Comp1;
|
|
|
|
var validLockers = new List<(EntityUid, EntityStorageComponent)>();
|
|
var spawn = Spawn(spawnRule.Prototype, MapCoordinates.Nullspace);
|
|
|
|
var query = EntityQueryEnumerator<EntityStorageComponent, TransformComponent>();
|
|
while (query.MoveNext(out var storageUid, out var storage, out var xform))
|
|
{
|
|
if (Station.GetOwningStation(storageUid, xform) != station.Value.Owner)
|
|
continue;
|
|
|
|
if (!_entityStorage.CanInsert(spawn, storageUid, storage))
|
|
continue;
|
|
|
|
validLockers.Add((storageUid, storage));
|
|
}
|
|
|
|
if (validLockers.Count == 0)
|
|
{
|
|
Del(spawn);
|
|
return;
|
|
}
|
|
|
|
var (locker, storageComp) = RobustRandom.Pick(validLockers);
|
|
if (!_entityStorage.Insert(spawn, locker, storageComp))
|
|
{
|
|
Del(spawn);
|
|
}
|
|
}
|
|
}
|