Files
OpenRA/OpenRA.Mods.Common/Scripting/Global/UtilsGlobal.cs
2015-05-16 13:19:44 +02:00

108 lines
2.7 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* 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. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using Eluant;
using OpenRA.Scripting;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Scripting
{
[ScriptGlobal("Utils")]
public class UtilsGlobal : ScriptGlobal
{
public UtilsGlobal(ScriptContext context) : base(context) { }
[Desc("Calls a function on every element in a collection.")]
public void Do(LuaValue[] collection, LuaFunction func)
{
foreach (var c in collection)
func.Call(c).Dispose();
}
[Desc("Returns true if func returns true for any element in a collection.")]
public bool Any(LuaValue[] collection, LuaFunction func)
{
foreach (var c in collection)
{
using (var ret = func.Call(c))
{
var result = ret.FirstOrDefault();
if (result != null && result.ToBoolean())
return true;
}
}
return false;
}
[Desc("Returns true if func returns true for all elements in a collection.")]
public bool All(LuaValue[] collection, LuaFunction func)
{
foreach (var c in collection)
{
using (var ret = func.Call(c))
{
var result = ret.FirstOrDefault();
if (result == null || !result.ToBoolean())
return false;
}
}
return true;
}
[Desc("Returns the first n values from a collection.")]
public LuaValue[] Take(int n, LuaValue[] source)
{
return source.Take(n).ToArray();
}
[Desc("Skips over the first numElements members of a table and return the rest.")]
public LuaTable Skip(LuaTable table, int numElements)
{
var t = Context.CreateTable();
for (var i = numElements; i <= table.Count; i++)
t.Add(t.Count + 1, table[i]);
return t;
}
[Desc("Returns a random value from a collection.")]
public LuaValue Random(LuaValue[] collection)
{
return collection.Random(Context.World.SharedRandom);
}
[Desc("Expands the given footprint one step along the coordinate axes, and (if requested) diagonals.")]
public CPos[] ExpandFootprint(CPos[] footprint, bool allowDiagonal)
{
return Util.ExpandFootprint(footprint, allowDiagonal).ToArray();
}
[Desc("Returns a random integer x in the range low &lt;= x &lt; high.")]
public int RandomInteger(int low, int high)
{
if (high <= low)
return low;
return Context.World.SharedRandom.Next(low, high);
}
[Desc("Returns the ticks formatted to HH:MM:SS.")]
public string FormatTime(int ticks, bool leadingMinuteZero = true)
{
return WidgetUtils.FormatTime(ticks, leadingMinuteZero);
}
}
}