diff --git a/Content.Server/Worldgen/Systems/Carvers/NoiseRangeCarverSystem.cs b/Content.Server/Worldgen/Systems/Carvers/NoiseRangeCarverSystem.cs index f2e051669a..1207d6f157 100644 --- a/Content.Server/Worldgen/Systems/Carvers/NoiseRangeCarverSystem.cs +++ b/Content.Server/Worldgen/Systems/Carvers/NoiseRangeCarverSystem.cs @@ -1,4 +1,4 @@ -using Content.Server.Worldgen.Components.Carvers; +using Content.Server.Worldgen.Components.Carvers; using Content.Server.Worldgen.Systems.Debris; namespace Content.Server.Worldgen.Systems.Carvers; @@ -20,7 +20,7 @@ public sealed class NoiseRangeCarverSystem : EntitySystem private void OnPrePlaceDebris(EntityUid uid, NoiseRangeCarverComponent component, ref PrePlaceDebrisFeatureEvent args) { - var coords = WorldGen.WorldToChunkCoords(args.Coords.ToMapPos(EntityManager, _transform)); + var coords = WorldGen.WorldToChunkCoords(_transform.ToMapCoordinates(args.Coords).Position); var val = _index.Evaluate(uid, component.NoiseChannel, coords); foreach (var (low, high) in component.Ranges) diff --git a/Content.Server/Worldgen/Systems/Debris/DebrisFeaturePlacerSystem.cs b/Content.Server/Worldgen/Systems/Debris/DebrisFeaturePlacerSystem.cs index 47ee6f6214..b609f7e1f7 100644 --- a/Content.Server/Worldgen/Systems/Debris/DebrisFeaturePlacerSystem.cs +++ b/Content.Server/Worldgen/Systems/Debris/DebrisFeaturePlacerSystem.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Numerics; using Content.Server.Worldgen.Components; using Content.Server.Worldgen.Components.Debris; @@ -26,6 +26,8 @@ public sealed class DebrisFeaturePlacerSystem : BaseWorldSystem private ISawmill _sawmill = default!; + private List> _mapGrids = new(); + /// public override void Initialize() { @@ -139,7 +141,14 @@ public sealed class DebrisFeaturePlacerSystem : BaseWorldSystem component.DoSpawns = false; // Don't repeat yourself if this crashes. - var chunk = Comp(args.Chunk); + if (!TryComp(args.Chunk, out var chunk)) + return; + + var chunkMap = chunk.Map; + + if (!TryComp(chunkMap, out var map)) + return; + var densityChannel = component.DensityNoiseChannel; var density = _noiseIndex.Evaluate(uid, densityChannel, chunk.Coordinates + new Vector2(0.5f, 0.5f)); if (density == 0) @@ -157,7 +166,9 @@ public sealed class DebrisFeaturePlacerSystem : BaseWorldSystem .ToList(); } - points ??= GeneratePointsInChunk(args.Chunk, density, chunk.Coordinates, chunk.Map); + points ??= GeneratePointsInChunk(args.Chunk, density, chunk.Coordinates, chunkMap); + + var mapId = map.MapId; var safetyBounds = Box2.UnitCentered.Enlarged(component.SafetyZoneRadius); var failures = 0; // Avoid severe log spam. @@ -173,11 +184,10 @@ public sealed class DebrisFeaturePlacerSystem : BaseWorldSystem if (pointDensity == 0 && component.DensityClip || _random.Prob(component.RandomCancellationChance)) continue; - var coords = new EntityCoordinates(chunk.Map, point); + if (HasCollisions(mapId, safetyBounds.Translated(point))) + continue; - if (_mapManager - .FindGridsIntersecting(Comp(chunk.Map).MapId, safetyBounds.Translated(point)).Any()) - continue; // Oops, gonna collide. + var coords = new EntityCoordinates(chunkMap, point); var preEv = new PrePlaceDebrisFeatureEvent(coords, args.Chunk); RaiseLocalEvent(uid, ref preEv); @@ -216,6 +226,19 @@ public sealed class DebrisFeaturePlacerSystem : BaseWorldSystem _sawmill.Error($"Failed to place {failures} debris at chunk {args.Chunk}"); } + /// + /// Checks to see if the potential spawn point is clear + /// + /// + /// + /// + private bool HasCollisions(MapId mapId, Box2 point) + { + _mapGrids.Clear(); + _mapManager.FindGridsIntersecting(mapId, point, ref _mapGrids); + return _mapGrids.Count > 0; + } + /// /// Generates the points to put into a chunk using a poisson disk sampler. /// diff --git a/Content.Server/Worldgen/Systems/Debris/NoiseDrivenDebrisSelectorSystem.cs b/Content.Server/Worldgen/Systems/Debris/NoiseDrivenDebrisSelectorSystem.cs index 8d8ca1b6c7..a02ff81362 100644 --- a/Content.Server/Worldgen/Systems/Debris/NoiseDrivenDebrisSelectorSystem.cs +++ b/Content.Server/Worldgen/Systems/Debris/NoiseDrivenDebrisSelectorSystem.cs @@ -1,5 +1,6 @@ -using Content.Server.Worldgen.Components.Debris; +using Content.Server.Worldgen.Components.Debris; using Robust.Server.GameObjects; +using Robust.Shared.Physics; using Robust.Shared.Random; namespace Content.Server.Worldgen.Systems.Debris; @@ -28,7 +29,7 @@ public sealed class NoiseDrivenDebrisSelectorSystem : BaseWorldSystem private void OnSelectDebrisKind(EntityUid uid, NoiseDrivenDebrisSelectorComponent component, ref TryGetPlaceableDebrisFeatureEvent args) { - var coords = WorldGen.WorldToChunkCoords(args.Coords.ToMapPos(EntityManager, _xformSys)); + var coords = WorldGen.WorldToChunkCoords(_xformSys.ToMapCoordinates(args.Coords).Position); var prob = _index.Evaluate(uid, component.NoiseChannel, coords); if (prob is < 0 or > 1)