Files
RobustToolbox/Robust.Server/GameObjects/EntitySystems/MapSystem.cs
T
metalgearslothandGitHub d72185933a Add support for grid chunk removals (#1941)
* Add support for grid chunk removals

Also allows grids to be removed when they have no more chunks remaining.

* No more crashing pog

* Slightly better

* Minor optimisations and fix bounds

* Avoid creating new chunks for anchoring

* chucky

* comment

* Tests

* Remove some logs

* Remove another log

* Review
2021-08-23 16:00:07 +10:00

68 lines
2.0 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Robust.Server.GameObjects
{
internal sealed class MapSystem : SharedMapSystem
{
private bool _deleteEmptyGrids;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MapGridComponent, EmptyGridEvent>(HandleGridEmpty);
var configManager = IoCManager.Resolve<IConfigurationManager>();
configManager.OnValueChanged(CVars.GameDeleteEmptyGrids, SetGridDeletion, true);
}
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.Index);
}
}
}
private bool GridEmpty(IMapGrid grid)
{
return !(grid.GetAllTiles().Any());
}
public override void Shutdown()
{
base.Shutdown();
var configManager = IoCManager.Resolve<IConfigurationManager>();
configManager.UnsubValueChanged(CVars.GameDeleteEmptyGrids, SetGridDeletion);
}
private void HandleGridEmpty(EntityUid uid, MapGridComponent component, EmptyGridEvent args)
{
if (!_deleteEmptyGrids ||
!EntityManager.TryGetEntity(uid, out var gridEnt) ||
gridEnt.LifeStage >= EntityLifeStage.Terminating) return;
MapManager.DeleteGrid(args.GridId);
}
}
}