]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add validation test for `ExplosionPrototype`s (#37621)
authorTayrtahn <tayrtahn@gmail.com>
Mon, 19 May 2025 22:22:47 +0000 (18:22 -0400)
committerGitHub <noreply@github.com>
Mon, 19 May 2025 22:22:47 +0000 (18:22 -0400)
* Add validation test for ExplosionPrototype

* Remove runtime validation in prototype

Content.IntegrationTests/Tests/Explosion/ExplosionPrototypeTest.cs [new file with mode: 0644]
Content.Shared/Explosion/ExplosionPrototype.cs

diff --git a/Content.IntegrationTests/Tests/Explosion/ExplosionPrototypeTest.cs b/Content.IntegrationTests/Tests/Explosion/ExplosionPrototypeTest.cs
new file mode 100644 (file)
index 0000000..54ff61c
--- /dev/null
@@ -0,0 +1,28 @@
+using Content.Shared.Explosion;
+
+namespace Content.IntegrationTests.Tests.Explosion;
+
+public sealed class ExplosionPrototypeTest
+{
+    [Test]
+    public async Task ValidateExplosionPrototypes()
+    {
+        await using var pair = await PoolManager.GetServerClient();
+        var server = pair.Server;
+        var entMan = server.EntMan;
+        var protoMan = server.ProtoMan;
+
+        var protos = protoMan.EnumeratePrototypes<ExplosionPrototype>();
+
+        Assert.Multiple(() =>
+        {
+            foreach (var proto in protos)
+            {
+                Assert.That(proto._tileBreakChance, Is.Not.Empty, $"Empty tile break chance definitions for explosion prototype: {proto.ID}");
+                Assert.That(proto._tileBreakChance, Has.Length.EqualTo(proto._tileBreakIntensity.Length), $"Malformed tile break chance definitions for explosion prototype: {proto.ID}");
+            }
+        });
+
+        await pair.CleanReturnAsync();
+    }
+}
index 192dfee1a190ac751e4d3742db44d91f57bded8d..b20c6891a861b62cc0c1b6b47dcd5f589a0babcb 100644 (file)
@@ -36,14 +36,14 @@ public sealed partial class ExplosionPrototype : IPrototype
     ///     explosion intensity to a tile break chance via linear interpolation.
     /// </summary>
     [DataField("tileBreakChance")]
-    private float[] _tileBreakChance = { 0f, 1f };
+    public float[] _tileBreakChance = { 0f, 1f };
 
     /// <summary>
     ///     This set of points, together with <see cref="_tileBreakChance"/> define a function that maps the
     ///     explosion intensity to a tile break chance via linear interpolation.
     /// </summary>
     [DataField("tileBreakIntensity")]
-    private float[] _tileBreakIntensity = {0f, 15f };
+    public float[] _tileBreakIntensity = { 0f, 15f };
 
     /// <summary>
     ///     When a tile is broken by an explosion, the intensity is reduced by this amount and is used to try and
@@ -115,19 +115,13 @@ public sealed partial class ExplosionPrototype : IPrototype
     /// </summary>
     public float TileBreakChance(float intensity)
     {
-        if (_tileBreakChance.Length == 0 || _tileBreakChance.Length != _tileBreakIntensity.Length)
-        {
-            Logger.Error($"Malformed tile break chance definitions for explosion prototype: {ID}");
-            return 0;
-        }
-
         if (intensity >= _tileBreakIntensity[^1] || _tileBreakIntensity.Length == 1)
             return _tileBreakChance[^1];
 
         if (intensity <= _tileBreakIntensity[0])
             return _tileBreakChance[0];
 
-        int i = Array.FindIndex(_tileBreakIntensity, k => k >= intensity);
+        var i = Array.FindIndex(_tileBreakIntensity, k => k >= intensity);
 
         var slope = (_tileBreakChance[i] - _tileBreakChance[i - 1]) / (_tileBreakIntensity[i] - _tileBreakIntensity[i - 1]);
         return _tileBreakChance[i - 1] + slope * (intensity - _tileBreakIntensity[i - 1]);