AutomaticAtmosSystem.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Server.Atmos.Components;
  2. using Content.Server.Shuttles.Systems;
  3. using Content.Shared.Maps;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Physics.Components;
  6. namespace Content.Server.Atmos.EntitySystems;
  7. /// <summary>
  8. /// Handles automatically adding a grid atmosphere to grids that become large enough, allowing players to build shuttles
  9. /// with a sealed atmosphere from scratch.
  10. /// </summary>
  11. public sealed class AutomaticAtmosSystem : EntitySystem
  12. {
  13. [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
  14. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<TileChangedEvent>(OnTileChanged);
  19. }
  20. private void OnTileChanged(ref TileChangedEvent ev)
  21. {
  22. // Only if a atmos-holding tile has been added or removed.
  23. // Also, these calls are surprisingly slow.
  24. // TODO: Make tiledefmanager cache the IsSpace property, and turn this lookup-through-two-interfaces into
  25. // TODO: a simple array lookup, as tile IDs are likely contiguous, and there's at most 2^16 possibilities anyway.
  26. var oldSpace = ev.OldTile.IsSpace(_tileDefinitionManager);
  27. var newSpace = ev.NewTile.IsSpace(_tileDefinitionManager);
  28. if (!(oldSpace && !newSpace ||
  29. !oldSpace && newSpace) ||
  30. _atmosphereSystem.HasAtmosphere(ev.Entity))
  31. return;
  32. if (!TryComp<PhysicsComponent>(ev.Entity, out var physics))
  33. return;
  34. // We can't actually count how many tiles there are efficiently, so instead estimate with the mass.
  35. if (physics.Mass / ShuttleSystem.TileMassMultiplier >= 7.0f)
  36. {
  37. AddComp<GridAtmosphereComponent>(ev.Entity);
  38. Log.Info($"Giving grid {ev.Entity} GridAtmosphereComponent.");
  39. }
  40. // It's not super important to remove it should the grid become too small again.
  41. // If explosions ever gain the ability to outright shatter grids, do rethink this.
  42. }
  43. }