Files
tbd-station-14/Content.Server/Worldgen/Systems/Debris/NoiseDrivenDebrisSelectorSystem.cs
Moony e91fc652a3 Dynamic space world generation and debris. (#15120)
* World generation (squash)

* Test fixes.

* command

* o

* Access cleanup.

* Documentation touchups.

* Use a prototype serializer for BiomeSelectionComponent

* Struct enumerator in SimpleFloorPlanPopulatorSystem

* Safety margins around PoissonDiskSampler, cookie acquisition methodologies

* Struct enumerating PoissonDiskSampler; internal side

* Struct enumerating PoissonDiskSampler: Finish it

* Update WorldgenConfigSystem.cs

awa

---------

Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
Co-authored-by: 20kdc <asdd2808@gmail.com>
2023-05-16 06:36:45 -05:00

60 lines
2.0 KiB
C#

using Content.Server.Worldgen.Components.Debris;
using Robust.Server.GameObjects;
using Robust.Shared.Random;
namespace Content.Server.Worldgen.Systems.Debris;
/// <summary>
/// This handles selecting debris with probability decided by a noise channel.
/// </summary>
public sealed class NoiseDrivenDebrisSelectorSystem : BaseWorldSystem
{
[Dependency] private readonly NoiseIndexSystem _index = default!;
[Dependency] private readonly TransformSystem _xformSys = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private ISawmill _sawmill = default!;
/// <inheritdoc />
public override void Initialize()
{
_sawmill = _logManager.GetSawmill("world.debris.noise_debris_selector");
// Event is forcibly ordered to always be handled after the simple selector.
SubscribeLocalEvent<NoiseDrivenDebrisSelectorComponent, TryGetPlaceableDebrisFeatureEvent>(OnSelectDebrisKind,
after: new[] {typeof(DebrisFeaturePlacerSystem)});
}
private void OnSelectDebrisKind(EntityUid uid, NoiseDrivenDebrisSelectorComponent component,
ref TryGetPlaceableDebrisFeatureEvent args)
{
var coords = WorldGen.WorldToChunkCoords(args.Coords.ToMapPos(EntityManager, _xformSys));
var prob = _index.Evaluate(uid, component.NoiseChannel, coords);
if (prob is < 0 or > 1)
{
_sawmill.Error(
$"Sampled a probability of {prob}, which is outside the [0, 1] range, at {coords} aka {args.Coords}.");
return;
}
if (!_random.Prob(prob))
return;
var l = new List<string?>(1);
component.CachedDebrisTable.GetSpawns(_random, ref l);
switch (l.Count)
{
case 0:
return;
case > 1:
_sawmill.Warning($"Got more than one possible debris type from {uid}. List: {string.Join(", ", l)}");
break;
}
args.DebrisProto = l[0];
}
}