| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- namespace Lua.Standard;
- public static class MathematicsLibrary
- {
- public static void OpenMathLibrary(this LuaState state)
- {
- state.Environment[RandomInstanceKey] = new(new RandomUserData(new Random()));
- var math = new LuaTable(0, Functions.Length);
- foreach (var func in Functions)
- {
- math[func.Name] = func;
- }
- math["pi"] = Math.PI;
- math["huge"] = double.PositiveInfinity;
- state.Environment["math"] = math;
- state.LoadedModules["math"] = math;
- }
- public const string RandomInstanceKey = "__lua_mathematics_library_random_instance";
- static readonly LuaFunction[] Functions = [
- new("abs", Abs),
- new("acos", Acos),
- new("asin", Asin),
- new("atan2", Atan2),
- new("atan", Atan),
- new("ceil", Ceil),
- new("cos", Cos),
- new("cosh", Cosh),
- new("deg", Deg),
- new("exp", Exp),
- new("floor", Floor),
- new("fmod", Fmod),
- new("frexp", Frexp),
- new("ldexp", Ldexp),
- new("log", Log),
- new("max", Max),
- new("min", Min),
- new("modf", Modf),
- new("pow", Pow),
- new("rad", Rad),
- new("random", Random),
- new("randomseed", RandomSeed),
- new("sin", Sin),
- new("sinh", Sinh),
- new("sqrt", Sqrt),
- new("tan", Tan),
- new("tanh", Tanh),
- ];
- sealed class RandomUserData(Random random) : ILuaUserData
- {
- static LuaTable? SharedMetatable;
- public LuaTable? Metatable { get => SharedMetatable; set => SharedMetatable = value; }
- public Random Random { get; } = random;
- }
- public static ValueTask<int> Abs(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Abs(arg0);
- return new(1);
- }
- public static ValueTask<int> Acos(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Acos(arg0);
- return new(1);
- }
- public static ValueTask<int> Asin(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Asin(arg0);
- return new(1);
- }
- public static ValueTask<int> Atan2(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = Math.Atan2(arg0, arg1);
- return new(1);
- }
- public static ValueTask<int> Atan(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Atan(arg0);
- return new(1);
- }
- public static ValueTask<int> Ceil(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Ceiling(arg0);
- return new(1);
- }
- public static ValueTask<int> Cos(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Cos(arg0);
- return new(1);
- }
- public static ValueTask<int> Cosh(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Cosh(arg0);
- return new(1);
- }
- public static ValueTask<int> Deg(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = arg0 * (180.0 / Math.PI);
- return new(1);
- }
- public static ValueTask<int> Exp(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Exp(arg0);
- return new(1);
- }
- public static ValueTask<int> Floor(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Floor(arg0);
- return new(1);
- }
- public static ValueTask<int> Fmod(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = arg0 % arg1;
- return new(1);
- }
- public static ValueTask<int> Frexp(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var (m, e) = MathEx.Frexp(arg0);
- buffer.Span[0] = m;
- buffer.Span[1] = e;
- return new(2);
- }
- public static ValueTask<int> Ldexp(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = arg0 * Math.Pow(2, arg1);
- return new(1);
- }
- public static ValueTask<int> Log(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- if (context.ArgumentCount == 1)
- {
- buffer.Span[0] = Math.Log(arg0);
- }
- else
- {
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = Math.Log(arg0, arg1);
- }
- return new(1);
- }
- public static ValueTask<int> Max(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var x = context.GetArgument<double>(0);
- for (int i = 1; i < context.ArgumentCount; i++)
- {
- x = Math.Max(x, context.GetArgument<double>(i));
- }
- buffer.Span[0] = x;
- return new(1);
- }
- public static ValueTask<int> Min(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var x = context.GetArgument<double>(0);
- for (int i = 1; i < context.ArgumentCount; i++)
- {
- x = Math.Min(x, context.GetArgument<double>(i));
- }
- buffer.Span[0] = x;
- return new(1);
- }
- public static ValueTask<int> Modf(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var (i, f) = MathEx.Modf(arg0);
- buffer.Span[0] = i;
- buffer.Span[1] = f;
- return new(2);
- }
- public static ValueTask<int> Pow(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = Math.Pow(arg0, arg1);
- return new(1);
- }
- public static ValueTask<int> Rad(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = arg0 * (Math.PI / 180.0);
- return new(1);
- }
- public static ValueTask<int> Random(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var rand = context.State.Environment[RandomInstanceKey].Read<RandomUserData>().Random;
- if (context.ArgumentCount == 0)
- {
- buffer.Span[0] = rand.NextDouble();
- }
- else if (context.ArgumentCount == 1)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = rand.NextDouble() * (arg0 - 1) + 1;
- }
- else
- {
- var arg0 = context.GetArgument<double>(0);
- var arg1 = context.GetArgument<double>(1);
- buffer.Span[0] = rand.NextDouble() * (arg1 - arg0) + arg0;
- }
- return new(1);
- }
- public static ValueTask<int> RandomSeed(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- context.State.Environment[RandomInstanceKey] = new(new RandomUserData(new Random((int)BitConverter.DoubleToInt64Bits(arg0))));
- return new(0);
- }
- public static ValueTask<int> Sin(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Sin(arg0);
- return new(1);
- }
- public static ValueTask<int> Sinh(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Sinh(arg0);
- return new(1);
- }
- public static ValueTask<int> Sqrt(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Sqrt(arg0);
- return new(1);
- }
- public static ValueTask<int> Tan(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Tan(arg0);
- return new(1);
- }
- public static ValueTask<int> Tanh(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var arg0 = context.GetArgument<double>(0);
- buffer.Span[0] = Math.Tanh(arg0);
- return new(1);
- }
- }
|