The ExtractEmmyLuaAPI utility command, invoked with `--emmy-lua-api`, produces a documentation file that is used by the [OpenRA Lua Language Extension](https://marketplace.visualstudio.com/items?itemName=openra.vscode-openra-lua) to provide documentation and type information is VSCode and VSCode compatible editors when editing the Lua scripts. We improve the documentation and types produced by this utility in a few ways: - Require descriptions to be provided for all items. - Fix the type definitions of the base engine types (cpos, wpos, wangle, wdist, wvec, cvec) to match with the actual bindings on the C# side. Add some extra bindings for these types to increase their utility. - Introduce ScriptEmmyTypeOverrideAttribute to allow the C# side of the bindings to provide a more specific type. The utility command now requires this to be used to avoid accidentally exporting poor type information. - Fix a handful of scripts where the new type information revealed warnings. The ability to ScriptEmmyTypeOverrideAttribute allows parameters and return types to provide a more specific type compared to the previous, weak, type definition. For example LuaValue mapped to `any`, LuaTable mapped to `table`, and LuaFunction mapped to `function`. These types are all non-specific. `any` can be anything, `table` is a table without known types for its keys or values, `function` is a function with an unknown signature. Now, we can provide specific types. , e.g. instead of `table`, ReinforcementsGlobal.ReinforceWithTransport is able to specify `{ [1]: actor, [2]: actor[] }` - a table with keys 1 and 2, whose values are an actor, and a table of actors respectively. The callback functions in MapGlobal now have signatures, e.g. instead of `function` we have `fun(a: actor):boolean`. In UtilsGlobal, we also make use of generic types. These work in a similar fashion to generics in C#. These methods operate on collections, we can introduce a generic parameter named `T` for the type of the items in those collections. Now the return type and callback parameters can also use that generic type. This means the return type or callback functions operate on the same type as whatever type is in the collection you pass in. e.g. Utils.Do accepts a collection typed as `T[]` with a callback function invoked on each item typed as `fun(item: T)`. If you pass in actors, the callback operates on an actor. If you pass in strings, the callback operates on a string, etc. Overall, these changes should result in an improved user experience for those editing OpenRA Lua scripts in a compatible IDE.
195 lines
6.6 KiB
C#
195 lines
6.6 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright (c) The OpenRA Developers and Contributors
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. For more
|
|
* information, see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using Eluant;
|
|
using Eluant.ObjectBinding;
|
|
using OpenRA.Scripting;
|
|
using OpenRA.Support;
|
|
|
|
namespace OpenRA
|
|
{
|
|
/// <summary>
|
|
/// 1d world distance - 1024 units = 1 cell.
|
|
/// </summary>
|
|
public readonly struct WDist : IComparable, IComparable<WDist>, IEquatable<WDist>, IScriptBindable,
|
|
ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaUnaryMinusBinding,
|
|
ILuaMultiplicationBinding, ILuaDivisionBinding, ILuaLessThanBinding, ILuaLessThanOrEqualToBinding,
|
|
ILuaTableBinding, ILuaToStringBinding
|
|
{
|
|
public readonly int Length;
|
|
public long LengthSquared => (long)Length * Length;
|
|
|
|
public WDist(int r) { Length = r; }
|
|
public static readonly WDist Zero = new(0);
|
|
public static readonly WDist MaxValue = new(int.MaxValue);
|
|
public static WDist FromCells(int cells) { return new WDist(1024 * cells); }
|
|
|
|
public static WDist operator +(WDist a, WDist b) { return new WDist(a.Length + b.Length); }
|
|
public static WDist operator -(WDist a, WDist b) { return new WDist(a.Length - b.Length); }
|
|
public static WDist operator -(WDist a) { return new WDist(-a.Length); }
|
|
public static WDist operator /(WDist a, int b) { return new WDist(a.Length / b); }
|
|
public static WDist operator *(WDist a, int b) { return new WDist(a.Length * b); }
|
|
public static WDist operator *(int a, WDist b) { return new WDist(a * b.Length); }
|
|
public static bool operator <(WDist a, WDist b) { return a.Length < b.Length; }
|
|
public static bool operator >(WDist a, WDist b) { return a.Length > b.Length; }
|
|
public static bool operator <=(WDist a, WDist b) { return a.Length <= b.Length; }
|
|
public static bool operator >=(WDist a, WDist b) { return a.Length >= b.Length; }
|
|
|
|
public static bool operator ==(WDist me, WDist other) { return me.Length == other.Length; }
|
|
public static bool operator !=(WDist me, WDist other) { return !(me == other); }
|
|
|
|
// Sampled a N-sample probability density function in the range [-1024..1024]
|
|
// 1 sample produces a rectangular probability
|
|
// 2 samples produces a triangular probability
|
|
// ...
|
|
// N samples approximates a true Gaussian
|
|
public static WDist FromPDF(MersenneTwister r, int samples)
|
|
{
|
|
return new WDist(Exts.MakeArray(samples, _ => r.Next(-1024, 1024))
|
|
.Sum() / samples);
|
|
}
|
|
|
|
public static bool TryParse(string s, out WDist result)
|
|
{
|
|
result = Zero;
|
|
|
|
if (string.IsNullOrEmpty(s))
|
|
return false;
|
|
|
|
s = s.ToLowerInvariant();
|
|
var components = s.Split('c');
|
|
var cell = 0;
|
|
int subcell;
|
|
|
|
switch (components.Length)
|
|
{
|
|
case 2:
|
|
if (!Exts.TryParseInt32Invariant(components[0], out cell) ||
|
|
!Exts.TryParseInt32Invariant(components[1], out subcell))
|
|
return false;
|
|
break;
|
|
case 1:
|
|
if (!Exts.TryParseInt32Invariant(components[0], out subcell))
|
|
return false;
|
|
break;
|
|
default: return false;
|
|
}
|
|
|
|
// Propagate sign to fractional part
|
|
if (cell < 0)
|
|
subcell = -subcell;
|
|
|
|
result = new WDist(1024 * cell + subcell);
|
|
return true;
|
|
}
|
|
|
|
public override int GetHashCode() { return Length.GetHashCode(); }
|
|
|
|
public bool Equals(WDist other) { return other == this; }
|
|
public override bool Equals(object obj) { return obj is WDist dist && Equals(dist); }
|
|
|
|
public int CompareTo(object obj)
|
|
{
|
|
if (obj is not WDist)
|
|
return 1;
|
|
return Length.CompareTo(((WDist)obj).Length);
|
|
}
|
|
|
|
public int CompareTo(WDist other) { return Length.CompareTo(other.Length); }
|
|
|
|
public override string ToString()
|
|
{
|
|
var absLength = Math.Abs(Length);
|
|
var absValue = (absLength / 1024).ToStringInvariant() + "c" + (absLength % 1024).ToStringInvariant();
|
|
return Length < 0 ? "-" + absValue : absValue;
|
|
}
|
|
|
|
#region Scripting interface
|
|
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out WDist b))
|
|
throw new LuaException("Attempted to call WDist.Add(WDist, WDist) with invalid arguments.");
|
|
|
|
return new LuaCustomClrObject(a + b);
|
|
}
|
|
|
|
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out WDist b))
|
|
throw new LuaException("Attempted to call WDist.Subtract(WDist, WDist) with invalid arguments.");
|
|
|
|
return new LuaCustomClrObject(a - b);
|
|
}
|
|
|
|
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out WDist b))
|
|
throw new LuaException("Attempted to call WDist.Equals(WDist, WDist) with invalid arguments.");
|
|
|
|
return a == b;
|
|
}
|
|
|
|
public LuaValue Minus(LuaRuntime runtime) => new LuaCustomClrObject(-this);
|
|
|
|
public LuaValue Multiply(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out int b))
|
|
throw new LuaException("Attempted to call WDist.Multiply(WDist, integer) with invalid arguments.");
|
|
|
|
return new LuaCustomClrObject(a * b);
|
|
}
|
|
|
|
public LuaValue Divide(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out int b))
|
|
throw new LuaException("Attempted to call WDist.Divide(WDist, integer) with invalid arguments.");
|
|
|
|
return new LuaCustomClrObject(a / b);
|
|
}
|
|
|
|
public LuaValue LessThan(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out WDist b))
|
|
throw new LuaException("Attempted to call WDist.LessThan(WDist, WDist) with invalid arguments.");
|
|
|
|
return a < b;
|
|
}
|
|
|
|
public LuaValue LessThanOrEqualTo(LuaRuntime runtime, LuaValue left, LuaValue right)
|
|
{
|
|
if (!left.TryGetClrValue(out WDist a) || !right.TryGetClrValue(out WDist b))
|
|
throw new LuaException("Attempted to call WDist.LessThanOrEqualTo(WDist, WDist) with invalid arguments.");
|
|
|
|
return a <= b;
|
|
}
|
|
|
|
public LuaValue this[LuaRuntime runtime, LuaValue key]
|
|
{
|
|
get
|
|
{
|
|
switch (key.ToString())
|
|
{
|
|
case "Length": return Length;
|
|
default: throw new LuaException($"WDist does not define a member '{key}'");
|
|
}
|
|
}
|
|
|
|
set => throw new LuaException("WDist is read-only. Use WDist.New to create a new value");
|
|
}
|
|
|
|
public LuaValue ToString(LuaRuntime runtime) => ToString();
|
|
|
|
#endregion
|
|
}
|
|
}
|