public void Test_CallFunction() { // Test a function that dosent return anything (void c# , nil/nan Lua) functions.LoadScript(testCode1, "testCode1"); DynValue value = functions.Call("test_func0"); Assert.AreEqual(true, value.IsNilOrNan()); }
/* ano - ugly hack to work around https://github.com/xanathar/moonsharp/issues/230 * see also * https://github.com/xanathar/moonsharp/blob/b811c97699b08eb025829272cc9a09044a81361f/src/MoonSharp.Interpreter/CoreLib/MathModule.cs#L284 * and * https://github.com/xanathar/moonsharp/blob/b811c97699b08eb025829272cc9a09044a81361f/src/MoonSharp.Interpreter/CoreLib/MathModule.cs#L26 * if those links were down, basically new Random(seed) throws an exception if seed is * equal to int.MinValue, and very large numbers in lua being passed to it were casting * to int.MinValue, which, well, was causing this whole exception problem */ internal DynValue RandomSeed(DynValue input) { int seed; if (input.IsNilOrNan()) { seed = Environment.TickCount; } else { seed = (int)input.Number; } if (seed == int.MinValue) { // we dont warn about this more than once if (!bInitializedSeed) { // should we warn about this in the parameterless case? Warn( "math.randomseed of " + input.CastToString() + " was out of bounds.\nseed must be greater than " + int.MinValue + " and less than or equal to " + int.MaxValue + ". a random seed chosen based on the current system uptime was used instead." ); } seed = Environment.TickCount; /* * as per the * https://msdn.microsoft.com/en-us/library/system.environment.tickcount(v=vs.110).aspx * Environment.TickCount documentation: * TickCount will increment from zero to Int32.MaxValue for approximately 24.9 days, * then jump to Int32.MinValue */ while (seed == int.MinValue) { // sleeping here sucks but it's supposed to happen only precisely at // int.MaxValue milliseconds of system uptime System.Threading.Thread.Sleep(1); seed = Environment.TickCount; } } Random rand = new Random(seed); script.Registry.Set("F61E3AA7247D4D1EB7A45430B0C8C9BB_MATH_RANDOM", UserData.Create(new MoonSharp.Interpreter.Interop.AnonWrapper <Random>(rand)) ); bInitializedSeed = true; return(DynValue.Nil); }
public static void SetUDMFField(MapElement element, string key, DynValue value) { if (!General.Map.FormatInterface.HasCustomFields) { ScriptContext.context.WarnFormatIncompatible("UDMF fields not supported."); return; } if (key == null) { throw new ScriptRuntimeException("key is nil, can't SetUDMFField() (not enough arguments maybe?)"); } if (element == null) { throw new ScriptRuntimeException("map element is nil, can't SetUDMFField()"); } if (element.IsDisposed) { throw new ScriptRuntimeException("map element is disposed, can't SetUDMFField()"); } if (value.IsNilOrNan() || value.IsVoid()) { throw new ScriptRuntimeException("value is nil, nan, or void. can't SetUDMFField() (not enough arguments maybe?) " + value.ToObject().GetType().ToString()); } key = UniValue.ValidateName(key); if (key == "") { return; } object v_object = value.ToObject(); if (v_object is double) { v_object = (float)((double)v_object); } try { element.SetField(key, v_object); } catch (ArgumentException) { throw new ScriptRuntimeException("error setting UDMF field " + key + ", must be int, float, string, double or bool, instead was " + v_object.GetType().ToString()); } }
public static DynValue concat(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue vlist = args.AsType(0, "concat", DataType.Table, false); DynValue vsep = args.AsType(1, "concat", DataType.String, true); DynValue vstart = args.AsType(2, "concat", DataType.Number, true); DynValue vend = args.AsType(3, "concat", DataType.Number, true); Table list = vlist.Table; string sep = vsep.IsNil() ? "" : vsep.String; int start = vstart.IsNilOrNan() ? 1 : (int)vstart.Number; int end; if (vend.IsNilOrNan()) { end = GetTableLength(executionContext, vlist); } else { end = (int)vend.Number; } if (end < start) { return(DynValue.NewString(string.Empty)); } StringBuilder sb = new StringBuilder(); for (int i = start; i <= end; i++) { DynValue v = list.Get(i); if (v.Type != DataType.Number && v.Type != DataType.String) { throw new ScriptRuntimeException("invalid value ({1}) at index {0} in table for 'concat'", i, v.Type.ToLuaTypeString()); } string s = v.ToPrintString(); if (i != start) { sb.Append(sep); } sb.Append(s); } return(DynValue.NewString(sb.ToString())); }
public override DynValue Eval(ScriptExecutionContext context) { DynValue b = m_BaseExp.Eval(context).ToScalar(); DynValue i = m_IndexExp != null?m_IndexExp.Eval(context).ToScalar() : DynValue.NewString(m_Name); if (b.Type != DataType.Table) { throw new DynamicExpressionException("Attempt to index non-table."); } else if (i.IsNilOrNan()) { throw new DynamicExpressionException("Attempt to index with nil or nan key."); } return(b.Table.Get(i) ?? DynValue.Nil); }
private void TurnRight(DynValue p) { if (this._alreadyTurned) { return; } float percent = 1f; if (!p.IsNilOrNan()) { percent = Mathf.Clamp01((float)p.Number); } this._alreadyTurned = true; Turn(this._type.turnSpeed * percent); }
private DynValue ProjectTo(DynValue x, DynValue y, DynValue a, DynValue d) { if (x.IsNilOrNan() || y.IsNilOrNan() || a.IsNilOrNan() || d.IsNilOrNan()) { return(DynValue.NewNumber(-1)); } float distance = (float)d.Number; float angle = (float)a.Number; float newX = (float)x.Number + (Mathf.Sin(angle * Mathf.Deg2Rad) * distance); float newY = (float)y.Number + (Mathf.Cos(angle * Mathf.Deg2Rad) * distance); Table pos = DynValue.NewTable(this._script).Table; pos.Set(DynValue.NewString("x"), DynValue.NewNumber(Mathf.Round(newX * 1000f) / 1000f)); pos.Set(DynValue.NewString("y"), DynValue.NewNumber(Mathf.Round(newY * 1000f) / 1000f)); return(DynValue.NewTable(pos)); }
private DynValue BearingTo(DynValue x, DynValue y) { if (x.IsNilOrNan() || y.IsNilOrNan()) { return(DynValue.NewNumber(-1)); } float dx = (float)x.Number - this._pos.x; float dy = (float)y.Number - this._pos.y; // float bearing = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg; float bearing = 0f; if (dx > 0) { bearing = 90 - (Mathf.Atan(dy / dx) * Mathf.Rad2Deg); } else if (dx < 0) { bearing = 270 - (Mathf.Atan(dy / dx) * Mathf.Rad2Deg); } else if (dy > 0) { bearing = 0f; } else if (dy < 0) { bearing = 180f; } else { // Technically there is no bearing // bots are on top of each other bearing = 0; } return(DynValue.NewNumber(Mathf.Round(bearing * 1000f) / 1000f)); }
private void DownloadFile(string url, string localFile, DynValue callback) { bool downloaded = true; try { using (WebClient client = new WebClient()) { client.DownloadFile(url, localFile); } } catch (WebException ex) { FancyConsole.WriteLine(ex.Message, ConsoleColor.Red); downloaded = false; } if (!callback.IsNilOrNan()) Program.Call(callback, downloaded); }