mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-06 17:58:05 +02:00
78 lines
2.3 KiB
C#
78 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Server.Physics;
|
|
using Robust.Shared;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.ContentPack;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Serialization.Manager;
|
|
|
|
namespace Robust.Server.GameObjects
|
|
{
|
|
public sealed class MapSystem : SharedMapSystem
|
|
{
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
|
|
|
private bool _deleteEmptyGrids;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<MapGridComponent, EmptyGridEvent>(HandleGridEmpty);
|
|
|
|
_cfg.OnValueChanged(CVars.GameDeleteEmptyGrids, SetGridDeletion, true);
|
|
}
|
|
|
|
protected override void OnMapAdd(EntityUid uid, MapComponent component, ComponentAdd args)
|
|
{
|
|
EnsureComp<PhysicsMapComponent>(uid);
|
|
}
|
|
|
|
private void SetGridDeletion(bool value)
|
|
{
|
|
_deleteEmptyGrids = value;
|
|
|
|
// If we have any existing empty ones then cull them on setting the cvar
|
|
if (_deleteEmptyGrids)
|
|
{
|
|
var toDelete = new List<IMapGrid>();
|
|
|
|
foreach (var grid in MapManager.GetAllGrids())
|
|
{
|
|
if (!GridEmpty(grid)) continue;
|
|
toDelete.Add(grid);
|
|
}
|
|
|
|
foreach (var grid in toDelete)
|
|
{
|
|
MapManager.DeleteGrid(grid.GridEntityId);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool GridEmpty(IMapGrid grid)
|
|
{
|
|
return !(grid.GetAllTiles().Any());
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
|
|
_cfg.UnsubValueChanged(CVars.GameDeleteEmptyGrids, SetGridDeletion);
|
|
}
|
|
|
|
private void HandleGridEmpty(EntityUid uid, MapGridComponent component, EmptyGridEvent args)
|
|
{
|
|
if (!_deleteEmptyGrids) return;
|
|
if (!EntityManager.EntityExists(uid)) return;
|
|
if (EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage >= EntityLifeStage.Terminating) return;
|
|
|
|
MapManager.DeleteGrid(args.GridId);
|
|
}
|
|
}
|
|
}
|