From 56d7937be15a38d750700f816f7eb9442bf25524 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 28 Jun 2026 01:27:31 +0900 Subject: [PATCH] =?UTF-8?q?AviUtl=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=97?= =?UTF-8?q?=E3=83=88=E3=81=AE=E3=83=98=E3=83=83=E3=83=80=E3=83=BB=E3=82=BB?= =?UTF-8?q?=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E8=A7=A3=E9=87=88?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=89=8D=E5=87=A6=E7=90=86=E5=B1=A4=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AviUtlRandomEquivalenceTests.cs | 71 +++++++++++++++++++ LuaScript.Tests/LuaScript.Tests.csproj | 2 + LuaScript/Compat/AviUtlRandom.cs | 30 ++++++++ LuaScript/LuaAutoCompletionStrategy.cs | 2 +- LuaScript/LuaScriptEngine.cs | 12 ++++ native/shim.lua | 20 ++++++ native/worker.lua | 4 ++ 7 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 LuaScript.Tests/AviUtlRandomEquivalenceTests.cs create mode 100644 LuaScript/Compat/AviUtlRandom.cs diff --git a/LuaScript.Tests/AviUtlRandomEquivalenceTests.cs b/LuaScript.Tests/AviUtlRandomEquivalenceTests.cs new file mode 100644 index 0000000..4e3eae9 --- /dev/null +++ b/LuaScript.Tests/AviUtlRandomEquivalenceTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using LuaScript.Compat; + +namespace LuaScript.Tests +{ + public class AviUtlRandomEquivalenceTests + { + [Fact] + public void Rand_StaysWithinRequestedRange() + { + for (int seed = 0; seed < 64; seed++) + { + double value = AviUtlRandom.Next(-3, 7, seed, seed * 3 + 1); + Assert.InRange(value, -3d, 7d); + Assert.Equal(Math.Floor(value), value); + } + } + + [Fact] + public void Rand_IsDeterministicForSameInputs() + { + Assert.Equal( + AviUtlRandom.Next(0, 100, 5, 12), + AviUtlRandom.Next(0, 100, 5, 12)); + } + + [Fact] + public void Rand_MatchesBetweenMoonSharpAndLuaJit() + { + var lane = new LuaJitLane(); + Assert.True(lane.Available, "native/luajit.exe must be present"); + + var cases = new List<(double A, double B, double Seed, double Frame)>(); + for (int seed = 0; seed < 16; seed++) + for (int frame = 0; frame < 4; frame++) + { + cases.Add((0d, 100d, seed, frame)); + cases.Add((-50d, 50d, seed + 1, frame * 7)); + cases.Add((1d, 6d, seed * 13, frame)); + cases.Add((-3.5d, 3.5d, seed, frame + 1000)); + } + + var exprs = new List(cases.Count); + foreach (var c in cases) + exprs.Add($"aviutl_rand({L(c.A)},{L(c.B)},{L(c.Seed)},{L(c.Frame)})"); + + string[] lines = lane.Eval(exprs); + Assert.Equal(exprs.Count, lines.Length); + + var failures = new List(); + for (int i = 0; i < cases.Count; i++) + { + double expected = AviUtlRandom.Next(cases[i].A, cases[i].B, cases[i].Seed, cases[i].Frame); + if (!double.TryParse(lines[i], NumberStyles.Float, CultureInfo.InvariantCulture, out var got)) + { + failures.Add($"{exprs[i]} => luajit '{lines[i]}' (not a number), expected {expected}"); + continue; + } + if (got != expected) + failures.Add($"{exprs[i]} => luajit {got}, moonsharp {expected}"); + } + + Assert.True(failures.Count == 0, + $"{failures.Count}/{cases.Count} mismatches:\n" + string.Join("\n", failures.GetRange(0, Math.Min(25, failures.Count)))); + } + + private static string L(double value) => value.ToString("R", CultureInfo.InvariantCulture); + } +} diff --git a/LuaScript.Tests/LuaScript.Tests.csproj b/LuaScript.Tests/LuaScript.Tests.csproj index 4ad1bb9..a02798b 100644 --- a/LuaScript.Tests/LuaScript.Tests.csproj +++ b/LuaScript.Tests/LuaScript.Tests.csproj @@ -32,6 +32,8 @@ + + \ No newline at end of file diff --git a/LuaScript/Compat/AviUtlRandom.cs b/LuaScript/Compat/AviUtlRandom.cs new file mode 100644 index 0000000..ccb08ab --- /dev/null +++ b/LuaScript/Compat/AviUtlRandom.cs @@ -0,0 +1,30 @@ +using System; + +namespace LuaScript.Compat +{ + internal static class AviUtlRandom + { + public static double Next(double a, double b, double seed, double frame) + { + long seedBits = BitConverter.DoubleToInt64Bits(seed); + long frameBits = BitConverter.DoubleToInt64Bits(frame); + uint s = (uint)(seedBits ^ (seedBits >> 32)); + uint f = (uint)(frameBits ^ (frameBits >> 32)); + + uint h = s * 374761393u + f * 668265263u; + h = (h ^ (h >> 13)) * 1274126177u; + h ^= h >> 16; + + double r = h / 4294967295.0; + + long lo = (long)Math.Floor(Math.Min(a, b)); + long hi = (long)Math.Floor(Math.Max(a, b)); + long span = hi - lo + 1L; + if (span <= 1L) + return lo; + + long value = lo + (long)(r * span); + return value > hi ? hi : value; + } + } +} diff --git a/LuaScript/LuaAutoCompletionStrategy.cs b/LuaScript/LuaAutoCompletionStrategy.cs index 7f9a89f..bd3dd40 100644 --- a/LuaScript/LuaAutoCompletionStrategy.cs +++ b/LuaScript/LuaAutoCompletionStrategy.cs @@ -42,7 +42,7 @@ namespace LuaScript "obj.time", "obj.totaltime", "obj.t", "obj.frame", "obj.totalframe", "obj.framerate", "obj.layer", "obj.index", "obj.num", "obj.getpixel", "obj.setpixel", - "obj.getpixeldata" + "obj.getpixeldata", "obj.rand" ]), ("math", [ "math.abs", "math.ceil", "math.cos", "math.exp", "math.floor", diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 240c9ff..035672e 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -1,3 +1,4 @@ +using LuaScript.Compat; using MoonSharp.Interpreter; using MoonSharp.Interpreter.Debugging; using MoonSharp.Interpreter.Loaders; @@ -451,6 +452,17 @@ namespace LuaScript }); obj["putpixeldata"] = DynValue.NewCallback((_, _) => DynValue.Void); + + obj["rand"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + double frameDefault = _activeContext?.Frame ?? 0d; + double a = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; + double b = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; + double seed = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; + double frame = args.Count > 3 ? args[3].CastToNumber() ?? frameDefault : frameDefault; + return DynValue.NewNumber(AviUtlRandom.Next(a, b, seed, frame)); + }); } private static DynValue BuildObjectTable(Script script, SceneObjectInfo info) diff --git a/native/shim.lua b/native/shim.lua index 4fa437f..f924bdb 100644 --- a/native/shim.lua +++ b/native/shim.lua @@ -307,6 +307,26 @@ function anim.rand(...) return min + r * (max - min) end +function aviutl_rand(a, b, seed, frame) + local ps = ffi.new("double[1]", seed) + local ws = ffi.cast("uint32_t*", ps) + local s = tou(bit.bxor(ws[0], ws[1])) + local pf = ffi.new("double[1]", frame) + local wf = ffi.cast("uint32_t*", pf) + local f = tou(bit.bxor(wf[0], wf[1])) + local h = tou(umul32(s, 374761393) + umul32(f, 668265263)) + h = umul32(tou(bit.bxor(h, bit.rshift(h, 13))), 1274126177) + h = tou(bit.bxor(h, bit.rshift(h, 16))) + local r = h / 4294967295.0 + local lo = floor(math.min(a, b)) + local hi = floor(math.max(a, b)) + local span = hi - lo + 1 + if span <= 1 then return lo end + local v = lo + floor(r * span) + if v > hi then return hi end + return v +end + local function noise_hash(x, y, z) local n = tou(umul32(tou(x), 374761393) + umul32(tou(y), 668265263) + umul32(tou(z), 1013904223)) n = umul32(tou(bit.bxor(n, bit.rshift(n, 13))), 1274126177) diff --git a/native/worker.lua b/native/worker.lua index 5409ef6..22d4465 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -146,6 +146,10 @@ end function obj.putpixeldata() end +function obj.rand(a, b, seed, frame) + return aviutl_rand(a or 0, b or 0, seed or 0, frame or obj.frame or 0) +end + local cacheTag, cacheFrame, cacheFound local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer