Add a number of small helper functions to Lua API

This commit is contained in:
Oliver Brakmann
2014-06-29 19:11:17 +02:00
parent d23707b5f7
commit 882f3f34c2
4 changed files with 68 additions and 7 deletions

View File

@@ -65,5 +65,8 @@ namespace OpenRA.Scripting
[Desc("Create a new WRange.")]
public WRange New(int r) { return new WRange(r); }
[Desc("Create a new WRange by cell distance")]
public WRange FromCells(int numCells) { return WRange.FromCells(numCells); }
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Linq;
using Eluant;
using OpenRA.Scripting;
@@ -43,6 +44,34 @@ namespace OpenRA.Mods.RA.Scripting
return actors.ToLuaTable(context);
}
[Desc("Returns a table of all actors within the requested rectangle, filtered using the specified function.")]
public LuaTable ActorsInBox(WPos topLeft, WPos bottomRight, LuaFunction filter = null)
{
var actors = context.World.ActorMap.ActorsInBox(topLeft, bottomRight)
.Select(a => a.ToLuaValue(context));
if (filter != null)
actors = actors.Where(a =>
{
using (var f = filter.Call(a))
return f.First().ToBoolean();
});
return actors.ToLuaTable(context);
}
[Desc("Returns the location of the top-left corner of the map.")]
public WPos TopLeft
{
get { return new WPos(context.World.Map.Bounds.Left * 1024, context.World.Map.Bounds.Top * 1024, 0); }
}
[Desc("Returns the location of the bottom-right corner of the map.")]
public WPos BottomRight
{
get { return new WPos(context.World.Map.Bounds.Right * 1024, context.World.Map.Bounds.Bottom * 1024, 0); }
}
[Desc("Returns a random cell inside the visible region of the map.")]
public CPos RandomCell()
{

View File

@@ -58,6 +58,17 @@ namespace OpenRA.Mods.RA.Scripting
return true;
}
[Desc("Skips over the first numElements members of the array and returns 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 table.")]
public LuaValue Random(LuaTable table)
{
@@ -94,5 +105,17 @@ namespace OpenRA.Mods.RA.Scripting
{
return context.World.Map.CenterOfCell(cell);
}
[Desc("Converts the number of seconds into game time (ticks).")]
public int Seconds(int seconds)
{
return seconds * 25;
}
[Desc("Converts the number of minutes into game time (ticks).")]
public int Minutes(int minutes)
{
return Seconds(minutes * 60);
}
}
}

View File

@@ -9,22 +9,28 @@
#endregion
using System;
using System.Linq;
using Eluant;
using OpenRA.Scripting;
using OpenRA.Mods.RA.Move;
namespace OpenRA.Mods.RA.Scripting
{
[ScriptPropertyGroup("Player")]
public class PlayerProperties : ScriptPlayerProperties
{
readonly Player p;
public PlayerProperties(ScriptContext context, Player player)
: base(context, player)
{
p = player;
}
: base(context, player) { }
[Desc("The player's name.")]
public string PlayerName { get { return p.PlayerName; } }
public string Name { get { return player.PlayerName; } }
[Desc("Returns an array of actors representing all ground attack units of this player.")]
public LuaTable GetGroundAttackers()
{
return player.World.ActorsWithTrait<AttackBase>().Select(a => a.Actor)
.Where(a => a.Owner == player && !a.IsDead() && a.IsInWorld && a.HasTrait<Mobile>())
.Select(a => a.ToLuaValue(context)).ToLuaTable(context);
}
}
}