Fix planet command being ran on existing maps (#21775)
This commit is contained in:
@@ -6,6 +6,7 @@ using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Decals;
|
||||
using Content.Server.Ghost.Roles.Components;
|
||||
using Content.Server.Shuttles.Events;
|
||||
using Content.Server.Shuttles.Systems;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Decals;
|
||||
using Content.Shared.Gravity;
|
||||
@@ -21,6 +22,9 @@ using Robust.Shared.Console;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Noise;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Physics.Systems;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
@@ -41,8 +45,12 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
[Dependency] private readonly AtmosphereSystem _atmos = default!;
|
||||
[Dependency] private readonly DecalSystem _decals = default!;
|
||||
[Dependency] private readonly SharedMapSystem _mapSystem = default!;
|
||||
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
[Dependency] private readonly ShuttleSystem _shuttles = default!;
|
||||
|
||||
private EntityQuery<BiomeComponent> _biomeQuery;
|
||||
private EntityQuery<FixturesComponent> _fixturesQuery;
|
||||
private EntityQuery<TransformComponent> _xformQuery;
|
||||
|
||||
private readonly HashSet<EntityUid> _handledEntities = new();
|
||||
@@ -69,6 +77,8 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
{
|
||||
base.Initialize();
|
||||
Log.Level = LogLevel.Debug;
|
||||
_biomeQuery = GetEntityQuery<BiomeComponent>();
|
||||
_fixturesQuery = GetEntityQuery<FixturesComponent>();
|
||||
_xformQuery = GetEntityQuery<TransformComponent>();
|
||||
SubscribeLocalEvent<BiomeComponent, MapInitEvent>(OnBiomeMapInit);
|
||||
SubscribeLocalEvent<FTLStartedEvent>(OnFTLStarted);
|
||||
@@ -92,12 +102,12 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
|
||||
var query = AllEntityQuery<BiomeComponent>();
|
||||
|
||||
while (query.MoveNext(out var biome))
|
||||
while (query.MoveNext(out var uid, out var biome))
|
||||
{
|
||||
if (biome.Template == null || !reloads.Modified.TryGetValue(biome.Template, out var proto))
|
||||
continue;
|
||||
|
||||
SetTemplate(biome, (BiomeTemplatePrototype) proto);
|
||||
SetTemplate(uid, biome, (BiomeTemplatePrototype) proto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,29 +120,64 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
|
||||
private void OnBiomeMapInit(EntityUid uid, BiomeComponent component, MapInitEvent args)
|
||||
{
|
||||
if (component.Seed != -1)
|
||||
return;
|
||||
if (component.Seed == -1)
|
||||
{
|
||||
SetSeed(uid, component, _random.Next());
|
||||
}
|
||||
|
||||
SetSeed(component, _random.Next());
|
||||
var xform = Transform(uid);
|
||||
var mapId = xform.MapID;
|
||||
|
||||
if (mapId != MapId.Nullspace && TryComp(uid, out MapGridComponent? mapGrid))
|
||||
{
|
||||
var setTiles = new List<(Vector2i Index, Tile tile)>();
|
||||
|
||||
foreach (var grid in _mapManager.GetAllMapGrids(mapId))
|
||||
{
|
||||
var gridUid = grid.Owner;
|
||||
|
||||
if (!_fixturesQuery.TryGetComponent(gridUid, out var fixtures))
|
||||
continue;
|
||||
|
||||
// Don't want shuttles flying around now do we.
|
||||
_shuttles.Disable(gridUid);
|
||||
var pTransform = _physics.GetPhysicsTransform(gridUid);
|
||||
|
||||
foreach (var fixture in fixtures.Fixtures.Values)
|
||||
{
|
||||
for (var i = 0; i < fixture.Shape.ChildCount; i++)
|
||||
{
|
||||
var aabb = fixture.Shape.ComputeAABB(pTransform, i);
|
||||
|
||||
setTiles.Clear();
|
||||
ReserveTiles(uid, aabb, setTiles);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSeed(BiomeComponent component, int seed)
|
||||
public void SetSeed(EntityUid uid, BiomeComponent component, int seed, bool dirty = true)
|
||||
{
|
||||
component.Seed = seed;
|
||||
Dirty(component);
|
||||
|
||||
if (dirty)
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
public void ClearTemplate(BiomeComponent component)
|
||||
public void ClearTemplate(EntityUid uid, BiomeComponent component, bool dirty = true)
|
||||
{
|
||||
component.Layers.Clear();
|
||||
component.Template = null;
|
||||
Dirty(component);
|
||||
|
||||
if (dirty)
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="BiomeComponent.Template"/> and refreshes layers.
|
||||
/// </summary>
|
||||
public void SetTemplate(BiomeComponent component, BiomeTemplatePrototype template)
|
||||
public void SetTemplate(EntityUid uid, BiomeComponent component, BiomeTemplatePrototype template, bool dirty = true)
|
||||
{
|
||||
component.Layers.Clear();
|
||||
component.Template = template.ID;
|
||||
@@ -142,13 +187,14 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
component.Layers.Add(layer);
|
||||
}
|
||||
|
||||
Dirty(component);
|
||||
if (dirty)
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified layer at the specified marker if it exists.
|
||||
/// </summary>
|
||||
public void AddLayer(BiomeComponent component, string id, IBiomeLayer addedLayer, int seedOffset = 0)
|
||||
public void AddLayer(EntityUid uid, BiomeComponent component, string id, IBiomeLayer addedLayer, int seedOffset = 0)
|
||||
{
|
||||
for (var i = 0; i < component.Layers.Count; i++)
|
||||
{
|
||||
@@ -162,25 +208,19 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
break;
|
||||
}
|
||||
|
||||
Dirty(component);
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
public void AddMarkerLayer(BiomeComponent component, string marker)
|
||||
public void AddMarkerLayer(EntityUid uid, BiomeComponent component, string marker)
|
||||
{
|
||||
if (!ProtoManager.HasIndex<BiomeMarkerLayerPrototype>(marker))
|
||||
{
|
||||
// TODO: Log when we get a sawmill
|
||||
return;
|
||||
}
|
||||
|
||||
component.MarkerLayers.Add(marker);
|
||||
Dirty(component);
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified template at the specified marker if it exists, withour overriding every layer.
|
||||
/// </summary>
|
||||
public void AddTemplate(BiomeComponent component, string id, BiomeTemplatePrototype template, int seedOffset = 0)
|
||||
public void AddTemplate(EntityUid uid, BiomeComponent component, string id, BiomeTemplatePrototype template, int seedOffset = 0)
|
||||
{
|
||||
for (var i = 0; i < component.Layers.Count; i++)
|
||||
{
|
||||
@@ -199,7 +239,7 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
break;
|
||||
}
|
||||
|
||||
Dirty(component);
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
private void OnFTLStarted(ref FTLStartedEvent ev)
|
||||
@@ -258,7 +298,7 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
|
||||
foreach (var layer in markers)
|
||||
{
|
||||
var proto = ProtoManager.Index<BiomeMarkerLayerPrototype>(layer);
|
||||
var proto = ProtoManager.Index(layer);
|
||||
var enumerator = new ChunkIndicesEnumerator(area, proto.Size);
|
||||
|
||||
while (enumerator.MoveNext(out var chunk))
|
||||
@@ -273,8 +313,6 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
var biomeQuery = GetEntityQuery<BiomeComponent>();
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
var biomes = AllEntityQuery<BiomeComponent>();
|
||||
|
||||
while (biomes.MoveNext(out var biome))
|
||||
@@ -286,17 +324,16 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
// Get chunks in range
|
||||
foreach (var pSession in Filter.GetAllPlayers(_playerManager))
|
||||
{
|
||||
|
||||
if (xformQuery.TryGetComponent(pSession.AttachedEntity, out var xform) &&
|
||||
if (_xformQuery.TryGetComponent(pSession.AttachedEntity, out var xform) &&
|
||||
_handledEntities.Add(pSession.AttachedEntity.Value) &&
|
||||
biomeQuery.TryGetComponent(xform.MapUid, out var biome))
|
||||
_biomeQuery.TryGetComponent(xform.MapUid, out var biome))
|
||||
{
|
||||
var worldPos = _transform.GetWorldPosition(xform, xformQuery);
|
||||
var worldPos = _transform.GetWorldPosition(xform);
|
||||
AddChunksInRange(biome, worldPos);
|
||||
|
||||
foreach (var layer in biome.MarkerLayers)
|
||||
{
|
||||
var layerProto = ProtoManager.Index<BiomeMarkerLayerPrototype>(layer);
|
||||
var layerProto = ProtoManager.Index(layer);
|
||||
AddMarkerChunksInRange(biome, worldPos, layerProto);
|
||||
}
|
||||
}
|
||||
@@ -304,18 +341,18 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
foreach (var viewer in pSession.ViewSubscriptions)
|
||||
{
|
||||
if (!_handledEntities.Add(viewer) ||
|
||||
!xformQuery.TryGetComponent(viewer, out xform) ||
|
||||
!biomeQuery.TryGetComponent(xform.MapUid, out biome))
|
||||
!_xformQuery.TryGetComponent(viewer, out xform) ||
|
||||
!_biomeQuery.TryGetComponent(xform.MapUid, out biome))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var worldPos = _transform.GetWorldPosition(xform, xformQuery);
|
||||
var worldPos = _transform.GetWorldPosition(xform);
|
||||
AddChunksInRange(biome, worldPos);
|
||||
|
||||
foreach (var layer in biome.MarkerLayers)
|
||||
{
|
||||
var layerProto = ProtoManager.Index<BiomeMarkerLayerPrototype>(layer);
|
||||
var layerProto = ProtoManager.Index(layer);
|
||||
AddMarkerChunksInRange(biome, worldPos, layerProto);
|
||||
}
|
||||
}
|
||||
@@ -326,7 +363,7 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
while (loadBiomes.MoveNext(out var gridUid, out var biome, out var grid))
|
||||
{
|
||||
// Load new chunks
|
||||
LoadChunks(biome, gridUid, grid, biome.Seed, xformQuery);
|
||||
LoadChunks(biome, gridUid, grid, biome.Seed, _xformQuery);
|
||||
// Unload old chunks
|
||||
UnloadChunks(biome, gridUid, grid, biome.Seed);
|
||||
}
|
||||
@@ -867,10 +904,12 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
if (!Resolve(mapUid, ref metadata))
|
||||
return;
|
||||
|
||||
var biome = EnsureComp<BiomeComponent>(mapUid);
|
||||
EnsureComp<MapGridComponent>(mapUid);
|
||||
var biome = (BiomeComponent) EntityManager.ComponentFactory.GetComponent(typeof(BiomeComponent));
|
||||
seed ??= _random.Next();
|
||||
SetSeed(biome, seed.Value);
|
||||
SetTemplate(biome, biomeTemplate);
|
||||
SetSeed(mapUid, biome, seed.Value, false);
|
||||
SetTemplate(mapUid, biome, biomeTemplate, false);
|
||||
AddComp(mapUid, biome, true);
|
||||
Dirty(mapUid, biome, metadata);
|
||||
|
||||
var gravity = EnsureComp<GravityComponent>(mapUid);
|
||||
@@ -902,7 +941,41 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
|
||||
};
|
||||
|
||||
_atmos.SetMapAtmosphere(mapUid, false, mixture, atmos);
|
||||
}
|
||||
|
||||
EnsureComp<MapGridComponent>(mapUid);
|
||||
/// <summary>
|
||||
/// Sets the specified tiles as relevant and marks them as modified.
|
||||
/// </summary>
|
||||
public void ReserveTiles(EntityUid mapUid, Box2 bounds, List<(Vector2i Index, Tile Tile)> tiles, BiomeComponent? biome = null, MapGridComponent? mapGrid = null)
|
||||
{
|
||||
if (!Resolve(mapUid, ref biome, ref mapGrid, false))
|
||||
return;
|
||||
|
||||
foreach (var tileSet in _mapSystem.GetLocalTilesIntersecting(mapUid, mapGrid, bounds, false))
|
||||
{
|
||||
Vector2i chunkOrigin;
|
||||
HashSet<Vector2i> modified;
|
||||
|
||||
// Existing, ignore
|
||||
if (_mapSystem.TryGetTileRef(mapUid, mapGrid, tileSet.GridIndices, out var existingRef) && !existingRef.Tile.IsEmpty)
|
||||
{
|
||||
chunkOrigin = SharedMapSystem.GetChunkIndices(tileSet.GridIndices, ChunkSize) * ChunkSize;
|
||||
modified = biome.ModifiedTiles.GetOrNew(chunkOrigin);
|
||||
modified.Add(tileSet.GridIndices);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!TryGetBiomeTile(tileSet.GridIndices, biome.Layers, biome.Seed, mapGrid, out var tile))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
chunkOrigin = SharedMapSystem.GetChunkIndices(tileSet.GridIndices, ChunkSize) * ChunkSize;
|
||||
modified = biome.ModifiedTiles.GetOrNew(chunkOrigin);
|
||||
modified.Add(tileSet.GridIndices);
|
||||
tiles.Add((tileSet.GridIndices, tile.Value));
|
||||
}
|
||||
|
||||
_mapSystem.SetTiles(mapUid, mapGrid, tiles);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user