From 1b19fed97c77206ace556ff5c05bee34fef7f767 Mon Sep 17 00:00:00 2001 From: ike709 Date: Tue, 11 Feb 2025 19:45:39 -0600 Subject: [PATCH] Implement `sign()` (#2207) Co-authored-by: ike709 --- .github/workflows/compiler-test.yml | 6 +++--- Content.Tests/DMProject/Tests/Builtins/sign.dm | 10 ++++++++++ DMCompiler/DMStandard/_Standard.dm | 1 + OpenDreamRuntime/Procs/Native/DreamProcNative.cs | 1 + .../Procs/Native/DreamProcNativeRoot.cs | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 Content.Tests/DMProject/Tests/Builtins/sign.dm diff --git a/.github/workflows/compiler-test.yml b/.github/workflows/compiler-test.yml index 9d44fc0634..face818642 100644 --- a/.github/workflows/compiler-test.yml +++ b/.github/workflows/compiler-test.yml @@ -75,7 +75,7 @@ jobs: ref: dev path: nebula - name: Compile Nebula Dev - run: main\bin\DMCompiler\DMCompiler.exe nebula\nebula.dme --suppress-unimplemented + run: main\bin\DMCompiler\DMCompiler.exe nebula\nebula.dme --suppress-unimplemented --version=516.1655 - name: Checkout /vg/station Master uses: actions/checkout@v2 with: @@ -83,7 +83,7 @@ jobs: ref: Bleeding-Edge path: vg - name: Compile /vg/station Master - run: main\bin\DMCompiler\DMCompiler.exe vg\vgstation13.dme --suppress-unimplemented + run: main\bin\DMCompiler\DMCompiler.exe vg\vgstation13.dme --suppress-unimplemented --version=516.1655 - name: Checkout CM Master uses: actions/checkout@v2 with: @@ -99,4 +99,4 @@ jobs: ref: master path: aurora - name: Compile Aurora Master - run: main\bin\DMCompiler\DMCompiler.exe aurora\aurorastation.dme --suppress-unimplemented + run: main\bin\DMCompiler\DMCompiler.exe aurora\aurorastation.dme --suppress-unimplemented --version=516.1655 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 5eabf6be5a..80a283ea9e 100644 --- a/DMCompiler/DMStandard/_Standard.dm +++ b/DMCompiler/DMStandard/_Standard.dm @@ -83,6 +83,7 @@ proc/roll(ndice = 1, sides) as num proc/round(A, B) as num proc/sha1(input) as text|null proc/shutdown(Addr,Natural = 0) +proc/sign(A) as num proc/sleep(Delay) proc/sorttext(T1, T2) as num proc/sorttextEx(T1, T2) as num diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNative.cs b/OpenDreamRuntime/Procs/Native/DreamProcNative.cs index 45d9737617..d2020f15f3 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNative.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNative.cs @@ -84,6 +84,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) { objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_round); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sha1); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_shutdown); + objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sign); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sleep); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sorttext); objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sorttextEx); diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs index e20346626c..75b9e96013 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs @@ -2425,6 +2425,22 @@ public static DreamValue NativeProc_shutdown(NativeProc.Bundle bundle, DreamObje return DreamValue.Null; } + [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("sleep")] [DreamProcParameter("Delay", Type = DreamValueTypeFlag.Float)] public static async Task NativeProc_sleep(AsyncNativeProc.State state) {