Added more Lua functions

This commit is contained in:
Curtis Shmyr
2014-08-15 19:51:02 -06:00
parent 780fd0df29
commit cb4bd52fc0
7 changed files with 140 additions and 4 deletions

View File

@@ -10,6 +10,9 @@
using System;
using System.Linq;
using Eluant;
using Eluant.ObjectBinding;
using OpenRA.Scripting;
using OpenRA.Support;
namespace OpenRA
@@ -17,7 +20,7 @@ namespace OpenRA
/// <summary>
/// 1d world distance - 1024 units = 1 cell.
/// </summary>
public struct WRange : IComparable, IComparable<WRange>, IEquatable<WRange>
public struct WRange : IComparable, IComparable<WRange>, IEquatable<WRange>, IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
{
public readonly int Range;
@@ -94,5 +97,53 @@ namespace OpenRA
public int CompareTo(WRange other) { return Range.CompareTo(other.Range); }
public override string ToString() { return Range.ToString(); }
#region Scripting interface
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
{
WRange a;
WRange b;
if (!left.TryGetClrValue<WRange>(out a) || !right.TryGetClrValue<WRange>(out b))
throw new LuaException("Attempted to call WRange.Add(WRange, WRange) with invalid arguments.");
return new LuaCustomClrObject(a + b);
}
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
{
WRange a;
WRange b;
if (!left.TryGetClrValue<WRange>(out a) || !right.TryGetClrValue<WRange>(out b))
throw new LuaException("Attempted to call WRange.Subtract(WRange, WRange) with invalid arguments.");
return new LuaCustomClrObject(a - b);
}
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
{
WRange a;
WRange b;
if (!left.TryGetClrValue<WRange>(out a) || !right.TryGetClrValue<WRange>(out b))
throw new LuaException("Attempted to call WRange.Equals(WRange, WRange) with invalid arguments.");
return a == b;
}
public LuaValue this[LuaRuntime runtime, LuaValue key]
{
get
{
switch (key.ToString())
{
case "Range": return Range;
default: throw new LuaException("WPos does not define a member '{0}'".F(key));
}
}
set
{
throw new LuaException("WRange is read-only. Use WRange.New to create a new value");
}
}
#endregion
}
}

View File

@@ -286,6 +286,8 @@
<Compile Include="Player\ProvidesTechPrerequisite.cs" />
<Compile Include="Player\MissionObjectives.cs" />
<Compile Include="PortableChrono.cs" />
<Compile Include="Scripting\Properties\GuardProperties.cs" />
<Compile Include="Scripting\Properties\PlayerProperties.cs" />
<Compile Include="Warheads\DestroyResourceWarhead.cs" />
<Compile Include="Warheads\CreateEffectWarhead.cs" />
<Compile Include="Warheads\CreateResourceWarhead.cs" />

View File

@@ -57,4 +57,13 @@ namespace OpenRA.Scripting
[Desc("The world zero-vector.")]
public WVec Zero { get { return WVec.Zero; } }
}
[ScriptGlobal("WRange")]
public class WRangeGlobal : ScriptGlobal
{
public WRangeGlobal(ScriptContext context) : base(context) { }
[Desc("Create a new WRange.")]
public WRange New(int r) { return new WRange(r); }
}
}

View File

@@ -28,15 +28,18 @@ namespace OpenRA.Mods.RA.Scripting
}
[Desc("Returns a table of all actors within the requested region, filtered using the specified function.")]
public LuaTable ActorsInCircle(WPos location, WRange radius, LuaFunction filter)
public LuaTable ActorsInCircle(WPos location, WRange radius, LuaFunction filter = null)
{
var actors = context.World.FindActorsInCircle(location, radius)
.Select(a => a.ToLuaValue(context))
.Where(a =>
.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);
}
@@ -72,9 +75,13 @@ namespace OpenRA.Mods.RA.Scripting
public Actor NamedActor(string actorName)
{
Actor ret;
if (!sma.Actors.TryGetValue(actorName, out ret))
return null;
if (ret.Destroyed)
return null;
return ret;
}

View File

@@ -56,6 +56,9 @@ namespace OpenRA.Mods.RA.Scripting
[Desc("The player that owns the actor.")]
public Player Owner { get { return self.Owner; } }
[Desc("The type of the actor (e.g. \"e1\").")]
public string Type { get { return self.Info.Name; } }
[Desc("The direction that the actor is facing.")]
public int Facing
{

View File

@@ -0,0 +1,34 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Scripting
{
[ScriptPropertyGroup("Guard")]
public class GuardProperties : ScriptActorProperties, Requires<GuardInfo>, Requires<IMoveInfo>
{
Guard guard;
public GuardProperties(Actor self)
: base(self)
{
guard = self.Trait<Guard>();
}
[ScriptActorPropertyActivity]
[Desc("Guard the target actor.")]
public void Guard(Actor targetActor)
{
if (targetActor.HasTrait<Guardable>())
guard.GuardTarget(self, Target.FromActor(targetActor));
}
}
}

View File

@@ -0,0 +1,30 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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;
using OpenRA.Scripting;
namespace OpenRA.Mods.RA.Scripting
{
[ScriptPropertyGroup("Player")]
public class PlayerProperties : ScriptPlayerProperties
{
readonly Player p;
public PlayerProperties(Player player)
: base(player)
{
p = player;
}
[Desc("The player's name.")]
public string PlayerName { get { return p.PlayerName; } }
}
}