CatStation/Content.Server/StationEvents/Events/RandomEntityStorageSpawnRule.cs
qwerltaz e39cd127d9
fix some station events targeting off-station grids (#45813)
* fix vent critters spawning off station

* I polish the code

* add utility to get entities ONLY on the station. use in vent critters rule.

* fix station events affecting outside grids

* better argument names for GetEntitiesWithComponentOnStation

* BreakerFlipRule

* \n

* fix doc in GetEntitiesWithComponentOnStation

* keep the comments

* remove BreakerFlipRuleComponent Blacklist

* minor cleanup

* review

* format

---------

Co-authored-by: Pok245 <113675512+Pok27@users.noreply.github.com>
2026-10-03 19:17:59 +00:00

45 lines
1.5 KiB
C#

using Content.Server.StationEvents.Components;
using Content.Server.Storage.EntitySystems;
using Content.Shared.GameTicking.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);
var spawnRule = ent.Comp1;
var validLockers = new List<Entity<EntityStorageComponent>>();
var spawn = Spawn(spawnRule.Prototype, MapCoordinates.Nullspace);
foreach (var storageEnt in Station.GetEntitiesWithComponentOnStation<EntityStorageComponent>(false))
{
if (!_entityStorage.CanInsert(spawn, storageEnt, storageEnt.Comp))
continue;
validLockers.Add(storageEnt);
}
if (validLockers.Count == 0)
{
Del(spawn);
return;
}
var locker = RobustRandom.Pick(validLockers);
if (!_entityStorage.Insert(spawn, locker, locker.Comp))
Del(spawn);
}
}