Skip to content

Commit a98e460

Browse files
pchotereaperrr
authored andcommitted
Expose WAngle to Lua API and deprecate old Facing.
1 parent 0349435 commit a98e460

File tree

6 files changed

+121
-12
lines changed

6 files changed

+121
-12
lines changed

OpenRA.Game/WAngle.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
#endregion
1111

1212
using System;
13+
using Eluant;
14+
using Eluant.ObjectBinding;
15+
using OpenRA.Scripting;
1316

1417
namespace OpenRA
1518
{
1619
/// <summary>
1720
/// 1D angle - 1024 units = 360 degrees.
1821
/// </summary>
19-
public struct WAngle : IEquatable<WAngle>
22+
public struct WAngle : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, IEquatable<WAngle>
2023
{
2124
public readonly int Angle;
2225
public int AngleSquared { get { return (int)Angle * Angle; } }
@@ -169,5 +172,58 @@ public static WAngle ArcTan(int y, int x, int stride)
169172
9233, 9781, 10396, 11094, 11891, 12810, 13882, 15148, 16667, 18524, 20843,
170173
23826, 27801, 33366, 41713, 55622, 83438, 166883, int.MaxValue
171174
};
175+
176+
#region Scripting interface
177+
178+
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
179+
{
180+
WAngle a, b;
181+
int c;
182+
183+
if (!left.TryGetClrValue(out a))
184+
throw new LuaException("Attempted to call WAngle.Add(WAngle, WAngle) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
185+
186+
if (right.TryGetClrValue(out c))
187+
{
188+
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
189+
return new LuaCustomClrObject(a + FromFacing(c));
190+
}
191+
192+
if (right.TryGetClrValue(out b))
193+
return new LuaCustomClrObject(a + b);
194+
195+
throw new LuaException("Attempted to call WAngle.Add(WAngle, WAngle) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
196+
}
197+
198+
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
199+
{
200+
WAngle a, b;
201+
int c;
202+
203+
if (!left.TryGetClrValue(out a))
204+
throw new LuaException("Attempted to call WAngle.Subtract(WAngle, WAngle) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
205+
206+
if (right.TryGetClrValue(out c))
207+
{
208+
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
209+
return new LuaCustomClrObject(a - FromFacing(c));
210+
}
211+
212+
if (right.TryGetClrValue(out b))
213+
return new LuaCustomClrObject(a - b);
214+
215+
throw new LuaException("Attempted to call WAngle.Subtract(WAngle, WAngle) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
216+
}
217+
218+
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
219+
{
220+
WAngle a, b;
221+
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
222+
return false;
223+
224+
return a == b;
225+
}
226+
227+
#endregion
172228
}
173229
}

OpenRA.Game/WVec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
150150
case "X": return X;
151151
case "Y": return Y;
152152
case "Z": return Z;
153-
case "Facing": return Yaw.Facing;
153+
case "Facing": return new LuaCustomClrObject(Yaw);
154154
default: throw new LuaException("WVec does not define a member '{0}'".F(key));
155155
}
156156
}

OpenRA.Mods.Common/Scripting/Global/ActorGlobal.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ ActorInit CreateInit(string initName, LuaValue value)
7575
return init;
7676
}
7777

78+
// HACK: Forward compatibility for future WAngle facings
79+
var facingInit = init as FacingInit;
80+
if (facingInit != null)
81+
{
82+
WAngle angle;
83+
if (value.TryGetClrValue(out angle))
84+
{
85+
facingInit.Initialize(angle.Facing);
86+
return facingInit;
87+
}
88+
89+
Game.Debug("Initializing Facing with integers is deprecated. Use Angle instead.");
90+
}
91+
7892
var initializers = initType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
7993
.Where(m => m.Name == "Initialize" && m.GetParameters().Length == 1);
8094

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using OpenRA.Scripting;
13+
14+
namespace OpenRA.Mods.Common.Scripting.Global
15+
{
16+
[ScriptGlobal("Angle")]
17+
public class AngleGlobal : ScriptGlobal
18+
{
19+
public AngleGlobal(ScriptContext context)
20+
: base(context) { }
21+
22+
public WAngle North { get { return WAngle.Zero; } }
23+
public WAngle NorthWest { get { return new WAngle(128); } }
24+
public WAngle West { get { return new WAngle(256); } }
25+
public WAngle SouthWest { get { return new WAngle(384); } }
26+
public WAngle South { get { return new WAngle(512); } }
27+
public WAngle SouthEast { get { return new WAngle(640); } }
28+
public WAngle East { get { return new WAngle(768); } }
29+
public WAngle NorthEast { get { return new WAngle(896); } }
30+
31+
[Desc("Create an arbitrary angle.")]
32+
public WAngle New(int a) { return new WAngle(a); }
33+
}
34+
}

OpenRA.Mods.Common/Scripting/Global/FacingGlobal.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ public class FacingGlobal : ScriptGlobal
1919
public FacingGlobal(ScriptContext context)
2020
: base(context) { }
2121

22-
public int North { get { return 0; } }
23-
public int NorthWest { get { return 32; } }
24-
public int West { get { return 64; } }
25-
public int SouthWest { get { return 96; } }
26-
public int South { get { return 128; } }
27-
public int SouthEast { get { return 160; } }
28-
public int East { get { return 192; } }
29-
public int NorthEast { get { return 224; } }
22+
void Deprecated()
23+
{
24+
Game.Debug("The Facing table is deprecated. Use Angle instead.");
25+
}
26+
27+
public int North { get { Deprecated(); return 0; } }
28+
public int NorthWest { get { Deprecated(); return 32; } }
29+
public int West { get { Deprecated(); return 64; } }
30+
public int SouthWest { get { Deprecated(); return 96; } }
31+
public int South { get { Deprecated(); return 128; } }
32+
public int SouthEast { get { Deprecated(); return 160; } }
33+
public int East { get { Deprecated(); return 192; } }
34+
public int NorthEast { get { Deprecated(); return 224; } }
3035
}
3136
}

OpenRA.Mods.Common/Scripting/Properties/GeneralProperties.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public GeneralProperties(ScriptContext context, Actor self)
122122
public WPos CenterPosition { get { return Self.CenterPosition; } }
123123

124124
[Desc("The direction that the actor is facing.")]
125-
public int Facing
125+
public WAngle Facing
126126
{
127127
get
128128
{
129129
if (facing == null)
130130
throw new LuaException("Actor '{0}' doesn't define a facing".F(Self));
131131

132-
return facing.Facing.Facing;
132+
return facing.Facing;
133133
}
134134
}
135135

0 commit comments

Comments
 (0)