Skip to content

Commit

Permalink
Implement values_dot()
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Feb 11, 2025
1 parent d2b9e20 commit 435a89a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Content.Tests/DMProject/Tests/Builtins/values_dot.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

/proc/RunTest()
ASSERT(values_dot(list(a=2.4,b=1,c=7),list(a=2,b=4,c=null)) == 8.8)
ASSERT(values_dot(list(),list(a=2,b=4)) == 0)
ASSERT(values_dot(list("a"),list(a=2,b=4)) == 0)
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/_Standard.dm
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ proc/typesof(Item1) as /list
proc/uppertext(T as text) as text
proc/url_decode(UrlText) as text
proc/url_encode(PlainText, format = 0) as text
proc/values_dot(A, B) as num
proc/values_product(Alist) as num
proc/values_sum(Alist) as num
proc/view(Dist = 5, Center = usr) as /list
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_uppertext);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_url_decode);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_url_encode);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_values_dot);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_values_product);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_values_sum);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_view);
Expand Down
29 changes: 29 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3108,6 +3108,35 @@ public static DreamValue NativeProc_url_encode(NativeProc.Bundle bundle, DreamOb
return new DreamValue(HttpUtility.UrlEncode(plainText));
}

[DreamProc("values_dot")]
[DreamProcParameter("A", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("B", Type = DreamValueTypeFlag.DreamObject)]
public static DreamValue NativeProc_values_dot(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (bundle.Arguments.Length != 2) throw new Exception("expected 2 arguments");

DreamValue argA = bundle.GetArgument(0, "A");
DreamValue argB = bundle.GetArgument(1, "B");

float sum = 0; // Default return is 0 for invalid args

if (argA.TryGetValueAsDreamList(out var listA) && listA.IsAssociative && argB.TryGetValueAsDreamList(out var listB) && listB.IsAssociative) {
var aValues = listA.GetAssociativeValues();
var bValues = listB.GetAssociativeValues();

// sum += valueA * valueB
// for each assoc value whose key exists in both lists
// and when both assoc values are floats
foreach (var (key,value) in aValues) {
if (value.TryGetValueAsFloat(out var aFloat) && bValues.TryGetValue(key, out var bVal) &&
bVal.TryGetValueAsFloat(out var bFloat)) {
sum += (aFloat * bFloat);
}
}
}

return new DreamValue(sum);
}

[DreamProc("values_product")]
[DreamProcParameter("Alist", Type = DreamValueTypeFlag.DreamObject)]
public static DreamValue NativeProc_values_product(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
Expand Down

0 comments on commit 435a89a

Please sign in to comment.