diff --git a/OpenRA.Game/WRange.cs b/OpenRA.Game/WRange.cs index ab272f825e..1e6038c405 100644 --- a/OpenRA.Game/WRange.cs +++ b/OpenRA.Game/WRange.cs @@ -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 /// /// 1d world distance - 1024 units = 1 cell. /// - public struct WRange : IComparable, IComparable, IEquatable + public struct WRange : IComparable, IComparable, IEquatable, 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(out a) || !right.TryGetClrValue(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(out a) || !right.TryGetClrValue(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(out a) || !right.TryGetClrValue(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 } } diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 85dab4f5b9..9dc726733a 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -286,6 +286,8 @@ + + diff --git a/OpenRA.Mods.RA/Scripting/Global/CoordinateGlobals.cs b/OpenRA.Mods.RA/Scripting/Global/CoordinateGlobals.cs index 8fd2ca3794..dcc1763c22 100644 --- a/OpenRA.Mods.RA/Scripting/Global/CoordinateGlobals.cs +++ b/OpenRA.Mods.RA/Scripting/Global/CoordinateGlobals.cs @@ -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); } + } } diff --git a/OpenRA.Mods.RA/Scripting/Global/MapGlobal.cs b/OpenRA.Mods.RA/Scripting/Global/MapGlobal.cs index cc9981e552..34c61859f0 100644 --- a/OpenRA.Mods.RA/Scripting/Global/MapGlobal.cs +++ b/OpenRA.Mods.RA/Scripting/Global/MapGlobal.cs @@ -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; } diff --git a/OpenRA.Mods.RA/Scripting/Properties/GeneralProperties.cs b/OpenRA.Mods.RA/Scripting/Properties/GeneralProperties.cs index 4a4025a73c..0ab242684b 100644 --- a/OpenRA.Mods.RA/Scripting/Properties/GeneralProperties.cs +++ b/OpenRA.Mods.RA/Scripting/Properties/GeneralProperties.cs @@ -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 { diff --git a/OpenRA.Mods.RA/Scripting/Properties/GuardProperties.cs b/OpenRA.Mods.RA/Scripting/Properties/GuardProperties.cs new file mode 100644 index 0000000000..30f6c8b6dd --- /dev/null +++ b/OpenRA.Mods.RA/Scripting/Properties/GuardProperties.cs @@ -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, Requires + { + Guard guard; + public GuardProperties(Actor self) + : base(self) + { + guard = self.Trait(); + } + + [ScriptActorPropertyActivity] + [Desc("Guard the target actor.")] + public void Guard(Actor targetActor) + { + if (targetActor.HasTrait()) + guard.GuardTarget(self, Target.FromActor(targetActor)); + } + } +} \ No newline at end of file diff --git a/OpenRA.Mods.RA/Scripting/Properties/PlayerProperties.cs b/OpenRA.Mods.RA/Scripting/Properties/PlayerProperties.cs new file mode 100644 index 0000000000..538b611c1f --- /dev/null +++ b/OpenRA.Mods.RA/Scripting/Properties/PlayerProperties.cs @@ -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; } } + } +}