From b7548e0fe689a3a8865563d2641808085e72288e Mon Sep 17 00:00:00 2001 From: ike709 Date: Wed, 5 Feb 2025 10:27:59 -0600 Subject: [PATCH] Implement `sign()` --- Content.Tests/DMProject/Tests/Builtins/sign.dm | 10 ++++++++++ DMCompiler/DMStandard/_Standard.dm | 1 + OpenDreamRuntime/Procs/Native/DreamProcNative.cs | 1 + .../Procs/Native/DreamProcNativeRoot.cs | 16 ++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 Content.Tests/DMProject/Tests/Builtins/sign.dm diff --git a/Content.Tests/DMProject/Tests/Builtins/sign.dm b/Content.Tests/DMProject/Tests/Builtins/sign.dm new file mode 100644 index 0000000000..26786f754d --- /dev/null +++ b/Content.Tests/DMProject/Tests/Builtins/sign.dm @@ -0,0 +1,10 @@ + +/proc/RunTest() + ASSERT(sign(5.2) == 1) + ASSERT(sign(-5.2) == -1) + ASSERT(sign(0) == 0) + ASSERT(sign(null) == 0) + ASSERT(sign("") == 0) + ASSERT(sign("foo") == 0) + ASSERT(sign(list(1)) == 0) + \ No newline at end of file diff --git a/DMCompiler/DMStandard/_Standard.dm b/DMCompiler/DMStandard/_Standard.dm index 410b1cd950..76c6d1264d 100644 --- a/DMCompiler/DMStandard/_Standard.dm +++ b/DMCompiler/DMStandard/_Standard.dm @@ -81,6 +81,7 @@ proc/rgb(R, G, B, A, space) as text|null proc/rgb2num(color, space = COLORSPACE_RGB) as /list proc/roll(ndice = 1, sides) as num proc/round(A, B) as num +proc/sign(A) as num proc/sha1(input) as text|null proc/shutdown(Addr,Natural = 0) proc/sleep(Delay) diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNative.cs b/OpenDreamRuntime/Procs/Native/DreamProcNative.cs index acaaac09e2..8cb5fbb235 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNative.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNative.cs @@ -82,6 +82,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) { objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_rgb2num); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_roll); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_round); + objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sign); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sha1); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_shutdown); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sleep); diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs index e8c0511734..6fd58fbafe 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs @@ -2380,6 +2380,22 @@ public static DreamValue NativeProc_roll(NativeProc.Bundle bundle, DreamObject? return new DreamValue(total); } + [DreamProc("sign")] + [DreamProcParameter("A", Type = DreamValueTypeFlag.Float)] + public static DreamValue NativeProc_sign(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) { + if (bundle.Arguments.Length != 1) throw new Exception($"expected 1 argument (found {bundle.Arguments.Length})"); + DreamValue arg = bundle.GetArgument(0, "A"); + + // Any non-num returns 0 + if (!arg.TryGetValueAsFloat(out var value)) return new DreamValue(0); + + return value switch { + 0 => new DreamValue(0), + < 0 => new DreamValue(-1), + _ => new DreamValue(1) + }; + } + [DreamProc("sha1")] [DreamProcParameter("T", Type = DreamValueTypeFlag.String | DreamValueTypeFlag.DreamResource)] public static DreamValue NativeProc_sha1(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {