From 879901f4b9efa95313e19f0ce5e2f0f4c830ec58 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 20 Dec 2020 00:05:02 +0100 Subject: [PATCH] Add new block IDs, a property, 2 tests and fixes --- GamecraftModdingAPI/Block.cs | 2 +- GamecraftModdingAPI/Blocks/BlockIDs.cs | 26 +++++++++++----- GamecraftModdingAPI/Blocks/BlockTests.cs | 36 +++++++++++++++++----- GamecraftModdingAPI/Blocks/DampedSpring.cs | 11 +++++++ GamecraftModdingAPI/Tests/Assert.cs | 19 ++++++++++++ 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 4aa0a30..513ad5e 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -171,7 +171,7 @@ namespace GamecraftModdingAPI egid = new EGID(id, group.Value); if (typeToGroup.TryGetValue(type, out var gr) && gr.All(egs => egs != group.Value)) //If this subclass has a specific group, then use that - so Block should still work - throw new BlockTypeException($"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => g.ToString()).Aggregate((a, b) => a + ", " + b)} instead of {group.Value}"); + throw new BlockTypeException($"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => ((uint)g).ToString()).Aggregate((a, b) => a + ", " + b)} instead of {(uint)group.Value}"); } if (initializers.TryGetValue(type, out var func)) diff --git a/GamecraftModdingAPI/Blocks/BlockIDs.cs b/GamecraftModdingAPI/Blocks/BlockIDs.cs index 9591ae2..ff08b51 100644 --- a/GamecraftModdingAPI/Blocks/BlockIDs.cs +++ b/GamecraftModdingAPI/Blocks/BlockIDs.cs @@ -71,8 +71,9 @@ namespace GamecraftModdingAPI.Blocks GlassConeSegment, GlassCylinder, GlassSphere, - Lever, //63 - two IDs skipped - PlayerSpawn = 66, //Crashes without special handling + Lever, //63 + WoodenSlatsDoor = 65, + PlayerSpawn, //Crashes without special handling SmallSpawn, MediumSpawn, LargeSpawn, @@ -86,10 +87,17 @@ namespace GamecraftModdingAPI.Blocks DampedSpring, ServoPiston, StepperPiston, - PneumaticPiston, + PneumaticPiston, //80 PneumaticHinge, - PneumaticAxle, //82 - PilotSeat = 90, //Might crash + PneumaticAxle, + WindowedDoor, + Bench, + Chair, + Stool, + DampedHingeSpring, + PlainGlassDoor, + PlainWoodenDoor, + PilotSeat, //Might crash PassengerSeat, PilotControls, GrassCube, @@ -148,8 +156,9 @@ namespace GamecraftModdingAPI.Blocks WoodCylinder, WoodHemisphere, WoodSphere, - BrickCube, //149 - BrickSlicedCube = 151, + BrickCube, + DampedAxleSpring, //150 + BrickSlicedCube, BrickSlope, BrickCorner, ConcreteCube, @@ -355,6 +364,7 @@ namespace GamecraftModdingAPI.Blocks HexNetCylinder = 797, HexNetHemisphere, HexNetSphere, - HexNetTubeCorner //800 + HexNetTubeCorner, //800 + CenterOfMassBlock = 1346 } } \ No newline at end of file diff --git a/GamecraftModdingAPI/Blocks/BlockTests.cs b/GamecraftModdingAPI/Blocks/BlockTests.cs index 5447f6c..dbcd39e 100644 --- a/GamecraftModdingAPI/Blocks/BlockTests.cs +++ b/GamecraftModdingAPI/Blocks/BlockTests.cs @@ -1,6 +1,7 @@ using System; using Gamecraft.Wires; +using Unity.Mathematics; using GamecraftModdingAPI; using GamecraftModdingAPI.Tests; @@ -60,19 +61,31 @@ namespace GamecraftModdingAPI.Blocks Assert.Errorless(() => { b = newBlock.Specialise(); }, "Block.Specialize() raised an exception: ", "Block.Specialize() completed without issue."); if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized Piston is not null.")) return; if (!Assert.CloseTo(b.MaximumExtension, 1.01f, $"Piston.MaximumExtension {b.MaximumExtension} does not equal default value, possibly because it failed silently.", "Piston.MaximumExtension is close enough to default.")) return; - if (!Assert.CloseTo(b.MaximumForce, 750f, $"Piston.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Piston.MaximumForce is close enough to default.")) return; + if (!Assert.CloseTo(b.MaximumForce, 1.0f, $"Piston.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Piston.MaximumForce is close enough to default.")) return; } - [APITestCase(TestType.EditMode)] + [APITestCase(TestType.EditMode)] public static void TestServo() { - Block newBlock = Block.PlaceNew(BlockIDs.ServoAxle, Unity.Mathematics.float3.zero + 1); - Servo b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler - Assert.Errorless(() => { b = newBlock.Specialise(); }, "Block.Specialize() raised an exception: ", "Block.Specialize() completed without issue."); - if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized Servo is not null.")) return; - if (!Assert.CloseTo(b.MaximumAngle, 180f, $"Servo.MaximumAngle {b.MaximumAngle} does not equal default value, possibly because it failed silently.", "Servo.MaximumAngle is close enough to default.")) return; + Block newBlock = Block.PlaceNew(BlockIDs.ServoAxle, Unity.Mathematics.float3.zero + 1); + Servo b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler + Assert.Errorless(() => { b = newBlock.Specialise(); }, "Block.Specialize() raised an exception: ", "Block.Specialize() completed without issue."); + if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized Servo is not null.")) return; + if (!Assert.CloseTo(b.MaximumAngle, 180f, $"Servo.MaximumAngle {b.MaximumAngle} does not equal default value, possibly because it failed silently.", "Servo.MaximumAngle is close enough to default.")) return; if (!Assert.CloseTo(b.MinimumAngle, -180f, $"Servo.MinimumAngle {b.MinimumAngle} does not equal default value, possibly because it failed silently.", "Servo.MinimumAngle is close enough to default.")) return; - if (!Assert.CloseTo(b.MaximumForce, 750f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return; + if (!Assert.CloseTo(b.MaximumForce, 60f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return; + } + + [APITestCase(TestType.EditMode)] + public static void TestDampedSpring() + { + Block newBlock = Block.PlaceNew(BlockIDs.DampedSpring, Unity.Mathematics.float3.zero + 1); + DampedSpring b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler + Assert.Errorless(() => { b = newBlock.Specialise(); }, "Block.Specialize() raised an exception: ", "Block.Specialize() completed without issue."); + if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized DampedSpring is not null.")) return; + if (!Assert.CloseTo(b.Stiffness, 1.0f, $"DampedSpring.Stiffness {b.Stiffness} does not equal default value, possibly because it failed silently.", "DampedSpring.Stiffness is close enough to default.")) return; + if (!Assert.CloseTo(b.Damping, 0.1f, $"DampedSpring.Damping {b.Damping} does not equal default value, possibly because it failed silently.", "DampedSpring.Damping is close enough to default.")) return; + if (!Assert.CloseTo(b.MaxExtension, 0.3f, $"DampedSpring.MaxExtension {b.MaxExtension} does not equal default value, possibly because it failed silently.", "DampedSpring.MaxExtension is close enough to default.")) return; } [APITestCase(TestType.Game)] @@ -119,6 +132,13 @@ namespace GamecraftModdingAPI.Blocks if (!Assert.Errorless(() => { newWire = b.Connect(0, target, 0);})) return; if (!Assert.NotNull(newWire, "SignalingBlock.Connect(...) returned null, possible because it failed silently.", "SignalingBlock.Connect(...) returned a non-null value.")) return; } + + [APITestCase(TestType.EditMode)] + public static void TestSpecialiseError() + { + Block newBlock = Block.PlaceNew(BlockIDs.Bench, new float3(1, 1, 1)); + if (Assert.Errorful(() => newBlock.Specialise(), "Block.Specialise() was expected to error on a bench block.", "Block.Specialise() errored as expected for a bench block.")) return; + } } #endif } diff --git a/GamecraftModdingAPI/Blocks/DampedSpring.cs b/GamecraftModdingAPI/Blocks/DampedSpring.cs index 51da25e..20d28f2 100644 --- a/GamecraftModdingAPI/Blocks/DampedSpring.cs +++ b/GamecraftModdingAPI/Blocks/DampedSpring.cs @@ -44,5 +44,16 @@ namespace GamecraftModdingAPI.Blocks set => BlockEngine.SetBlockInfo(this, (ref DampedSpringReadOnlyStruct ljf, float val) => ljf.springDamping = val, value); } + + /// + /// The spring's maximum extension. + /// + public float MaxExtension + { + get => BlockEngine.GetBlockInfo(this, (DampedSpringReadOnlyStruct ljf) => ljf.maxExtent); + + set => BlockEngine.SetBlockInfo(this, + (ref DampedSpringReadOnlyStruct ljf, float val) => ljf.maxExtent = val, value); + } } } \ No newline at end of file diff --git a/GamecraftModdingAPI/Tests/Assert.cs b/GamecraftModdingAPI/Tests/Assert.cs index 78f0597..edc1392 100644 --- a/GamecraftModdingAPI/Tests/Assert.cs +++ b/GamecraftModdingAPI/Tests/Assert.cs @@ -119,6 +119,25 @@ namespace GamecraftModdingAPI.Tests return true; } + public static bool Errorful(Action tryThis, string err = null, string success = null) where T : Exception + { + if (err == null) err = $"{tryThis} was expected to error but completed without errors."; + if (success == null) success = $"{tryThis} completed with an expected error."; + try + { + tryThis(); + } + catch (T e) + { + TestRoot.TestsPassed = true; + Log(PASS + success + " " + e.GetType().Name + ": " + e.Message); + return true; + } + Log(FAIL + err); + TestRoot.TestsPassed = false; + return false; + } + public static bool CloseTo(float a, float b, string err = null, string success = null, float delta = float.Epsilon) { if (err == null) err = $"{a} is not within {delta} of {b}.";