Support weighted tile variantize (#18940)
* Support weighted tile variantize * Remove unused using * Use an array
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Content.Shared.Atmos;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
@@ -37,7 +36,7 @@ namespace Content.Shared.Maps
|
||||
public string BaseTurf { get; } = string.Empty;
|
||||
|
||||
[DataField("canCrowbar")] public bool CanCrowbar { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Whether this tile can be pried by an advanced prying tool if not pryable otherwise.
|
||||
/// </summary>
|
||||
@@ -62,7 +61,7 @@ namespace Content.Shared.Maps
|
||||
/// <summary>
|
||||
/// This controls what variants the `variantize` command is allowed to use.
|
||||
/// </summary>
|
||||
[DataField("placementVariants")] public byte[] PlacementVariants { get; set; } = new byte[1] { 0 };
|
||||
[DataField("placementVariants")] public float[] PlacementVariants { get; set; } = new [] { 1f };
|
||||
|
||||
[DataField("thermalConductivity")] public float ThermalConductivity = 0.04f;
|
||||
|
||||
|
||||
@@ -89,6 +89,30 @@ namespace Content.Shared.Maps
|
||||
return tile.Tile.IsSpace(tileDefinitionManager);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a weighted pick of a tile variant.
|
||||
/// </summary>
|
||||
public static byte PickVariant(this ContentTileDefinition tile, IRobustRandom? random = null)
|
||||
{
|
||||
IoCManager.Resolve(ref random);
|
||||
var variants = tile.PlacementVariants;
|
||||
|
||||
var sum = variants.Sum();
|
||||
var accumulated = 0f;
|
||||
var rand = random.NextFloat() * sum;
|
||||
|
||||
for (byte i = 0; i < variants.Length; ++i)
|
||||
{
|
||||
accumulated += variants[i];
|
||||
|
||||
if (accumulated >= rand)
|
||||
return i;
|
||||
}
|
||||
|
||||
// Shouldn't happen
|
||||
throw new InvalidOperationException($"Invalid weighted variantize tile pick for {tile.ID}!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper that returns all entities in a turf.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user