Add CPos.New etc functions to Lua standard library and helpers to LuaScriptInterface.cs. Rename Team.Create to Team.New.

This commit is contained in:
ScottNZ
2013-12-16 19:01:54 +13:00
parent 8e9835f2fa
commit b2e9de810e
9 changed files with 117 additions and 22 deletions

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using System; using System;
using System.Collections;
using System.Linq; using System.Linq;
using NLua; using NLua;
using OpenRA.Effects; using OpenRA.Effects;
@@ -49,6 +50,7 @@ namespace OpenRA.Mods.RA.Scripting
context.Lua["WorldRenderer"] = wr; context.Lua["WorldRenderer"] = wr;
context.RegisterObject(this, "Internal", false); context.RegisterObject(this, "Internal", false);
context.RegisterType(typeof(WVec), "WVec", true); context.RegisterType(typeof(WVec), "WVec", true);
context.RegisterType(typeof(CVec), "CVec", true);
context.RegisterType(typeof(WPos), "WPos", true); context.RegisterType(typeof(WPos), "WPos", true);
context.RegisterType(typeof(CPos), "CPos", true); context.RegisterType(typeof(CPos), "CPos", true);
context.RegisterType(typeof(WRot), "WRot", true); context.RegisterType(typeof(WRot), "WRot", true);
@@ -146,6 +148,19 @@ namespace OpenRA.Mods.RA.Scripting
return ret != null; return ret != null;
} }
[LuaGlobal]
public object[] ActorsWithTrait(string className)
{
var type = Game.modData.ObjectCreator.FindType(className);
if (type == null)
throw new InvalidOperationException("Cannot locate type: {0}".F(className));
var method = typeof(World).GetMethod("ActorsWithTrait");
var genericMethod = method.MakeGenericMethod(type);
var result = ((IEnumerable)genericMethod.Invoke(world, null)).Cast<object>().ToArray();
return result;
}
[LuaGlobal] [LuaGlobal]
public object TraitInfoOrDefault(string actorType, string className) public object TraitInfoOrDefault(string actorType, string className)
{ {
@@ -273,5 +288,32 @@ namespace OpenRA.Mods.RA.Scripting
{ {
return world.ActorsWithTrait<MustBeDestroyed>().All(p => p.Actor.Owner != player); return world.ActorsWithTrait<MustBeDestroyed>().All(p => p.Actor.Owner != player);
} }
[LuaGlobal]
public void AttackMove(Actor actor, CPos location)
{
if (actor.HasTrait<AttackMove>())
actor.QueueActivity(new AttackMove.AttackMoveActivity(actor, new Move.Move(location, 0)));
else
actor.QueueActivity(new Move.Move(location, 0));
}
[LuaGlobal]
public int GetRandomInteger(double low, double high)
{
return world.SharedRandom.Next((int)low, (int)high);
}
[LuaGlobal]
public CPos GetRandomCell()
{
return world.ChooseRandomCell(world.SharedRandom);
}
[LuaGlobal]
public CPos GetRandomEdgeCell()
{
return world.ChooseRandomEdgeCell();
}
} }
} }

View File

@@ -5,7 +5,7 @@ speed = 5
Tick = function() Tick = function()
ticks = ticks + 1 ticks = ticks + 1
local t = (ticks + 45) % (360 * speed) * (math.pi / 180) / speed; local t = (ticks + 45) % (360 * speed) * (math.pi / 180) / speed;
OpenRA.SetViewportCenterPosition(WPos.op_Addition(viewportOrigin, MakeVec(-15360 * math.sin(t), 4096 * math.cos(t)))) OpenRA.SetViewportCenterPosition(WPos.op_Addition(viewportOrigin, WVec.New(-15360 * math.sin(t), 4096 * math.cos(t))))
end end
WorldLoaded = function() WorldLoaded = function()
@@ -16,7 +16,7 @@ WorldLoaded = function()
local units = { boat1, boat2, boat3, boat4, lst1, lst2, lst3} local units = { boat1, boat2, boat3, boat4, lst1, lst2, lst3}
for i, unit in ipairs(units) do for i, unit in ipairs(units) do
LoopTrack(unit, MakePos(8, unit.Location.Y), MakePos(87, unit.Location.Y)) LoopTrack(unit, CPos.New(8, unit.Location.Y), CPos.New(87, unit.Location.Y))
end end
end end
@@ -35,12 +35,3 @@ CreateUnitsInTransport = function(transport, passengerNames)
cargo:Load(transport, Actor.Create(passengerName, { AddToWorld = false, Owner = owner, Facing = { facing, "Int32" } })) cargo:Load(transport, Actor.Create(passengerName, { AddToWorld = false, Owner = owner, Facing = { facing, "Int32" } }))
end end
end end
-- TODO: The standard library should expose CPos.New() etc
MakePos = function(x, y)
return OpenRA.New("CPos", { {x, "Int32"}, {y, "Int32"} })
end
MakeVec = function(x, y)
return OpenRA.New("WVec", { {x, "Int32"}, {y, "Int32"}, {0, "Int32"} })
end

View File

@@ -24,7 +24,7 @@ Actor.Move = function(actor, location)
end end
Actor.MoveNear = function(actor, location, nearEnough) Actor.MoveNear = function(actor, location, nearEnough)
actor:QueueActivity(OpenRA.New("Move", { location, Map.GetWRangeFromCells(nearEnough) })) actor:QueueActivity(OpenRA.New("Move", { location, WRange.FromCells(nearEnough) }))
end end
Actor.ScriptedMove = function(actor, location) Actor.ScriptedMove = function(actor, location)
@@ -35,6 +35,10 @@ Actor.Teleport = function(actor, location)
actor:QueueActivity(OpenRA.New("SimpleTeleport", { location })) actor:QueueActivity(OpenRA.New("SimpleTeleport", { location }))
end end
Actor.AttackMove = function(actor, location)
Internal.AttackMove(actor, location)
end
Actor.HeliFly = function(actor, position) Actor.HeliFly = function(actor, position)
actor:QueueActivity(OpenRA.New("HeliFly", { position })) actor:QueueActivity(OpenRA.New("HeliFly", { position }))
end end
@@ -119,6 +123,10 @@ Actor.Facing = function(actor)
return Actor.Trait(actor, "IFacing"):get_Facing() return Actor.Trait(actor, "IFacing"):get_Facing()
end end
Actor.IsIdle = function(actor)
return actor.IsIdle
end
Actor.SetStance = function(actor, stance) Actor.SetStance = function(actor, stance)
Internal.SetUnitStance(actor, stance) Internal.SetUnitStance(actor, stance)
end end
@@ -135,6 +143,14 @@ Actor.OnRemovedFromWorld = function(actor, eh)
Actor.Trait(actor, "LuaScriptEvents").OnRemovedFromWorld:Add(eh) Actor.Trait(actor, "LuaScriptEvents").OnRemovedFromWorld:Add(eh)
end end
Actor.ActorsWithTrait = function(className)
local ret = { }
for item in Utils.Enumerate(Internal.ActorsWithTrait(className)) do
table.insert(ret, item.Actor)
end
return ret
end
Actor.HasTrait = function(actor, className) Actor.HasTrait = function(actor, className)
return Internal.HasTrait(actor, className) return Internal.HasTrait(actor, className)
end end

View File

@@ -4,6 +4,44 @@ Map.GetFacing = function(vec, currentFacing)
return Internal.GetFacing(vec, currentFacing) return Internal.GetFacing(vec, currentFacing)
end end
Map.GetWRangeFromCells = function(cells) Map.GetRandomCell = function()
return Internal.GetWRangeFromCells(cells) return Internal.GetRandomCell()
end
Map.GetRandomEdgeCell = function()
return Internal.GetRandomEdgeCell()
end
CPos.New = function(x, y)
return OpenRA.New("CPos", { { x, "Int32" }, { y, "Int32" } })
end
WPos.New = function(x, y, z)
if z == nil then
z = 0
end
return OpenRA.New("WPos", { { x, "Int32" }, { y, "Int32" }, { z, "Int32" } })
end
WPos.FromCPos = function(location)
return WPos.New(location.X * 1024, location.Y * 1024, 0)
end
CVec.New = function(x, y)
return OpenRA.New("CVec", { { x, "Int32" }, { y, "Int32" } })
end
WVec.New = function(x, y, z)
if z == nil then
z = 0
end
return OpenRA.New("WVec", { { x, "Int32" }, { y, "Int32" }, { z, "Int32" } })
end
WRange.New = function(r)
return OpenRA.New("WRange", { { r, "Int32" } })
end
WRange.FromCells = function(cells)
return WRange.New(cells * 1024)
end end

View File

@@ -23,8 +23,8 @@ Mission.MissionOver = function(winners, losers, setWinStates)
end end
Mission.GetGroundAttackersOf = function(player) Mission.GetGroundAttackersOf = function(player)
return Utils.EnumerableWhere(World.Actors, function(actor) return Utils.Where(Actor.ActorsWithTrait("AttackBase"), function(actor)
return not Actor.IsDead(actor) and Actor.IsInWorld(actor) and Actor.Owner(actor) == player and Actor.HasTrait(actor, "AttackBase") and Actor.HasTrait(actor, "Mobile") return not Actor.IsDead(actor) and Actor.IsInWorld(actor) and Actor.Owner(actor) == player and Actor.HasTrait(actor, "Mobile")
end) end)
end end

View File

@@ -33,10 +33,18 @@ OpenRA.GetPlayer = function(internalName)
return Utils.EnumerableFirstOrNil(World.Players, function(p) return p.InternalName == internalName end) return Utils.EnumerableFirstOrNil(World.Players, function(p) return p.InternalName == internalName end)
end end
OpenRA.GetPlayers = function(func)
return Utils.EnumerableWhere(World.Players, func)
end
OpenRA.SetWinState = function(player, winState) OpenRA.SetWinState = function(player, winState)
Internal.SetWinState(player, winState) Internal.SetWinState(player, winState)
end end
OpenRA.GetRandomInteger = function(low, high)
return Internal.GetRandomInteger(low, high)
end
OpenRA.TakeOre = function(player, amount) OpenRA.TakeOre = function(player, amount)
Actor.Trait(player.PlayerActor, "PlayerResources"):TakeOre(amount) Actor.Trait(player.PlayerActor, "PlayerResources"):TakeOre(amount)
end end

View File

@@ -1,6 +1,6 @@
Team = { } Team = { }
Team.Create = function(actors) Team.New = function(actors)
local team = { } local team = { }
team.Actors = actors team.Actors = actors
team.OnAllKilled = { } team.OnAllKilled = { }

View File

@@ -134,10 +134,10 @@ WorldLoaded = function()
Actor.OnKilled(Lab, LabDestroyed) Actor.OnKilled(Lab, LabDestroyed)
Actor.OnKilled(OilPump, OilPumpDestroyed) Actor.OnKilled(OilPump, OilPumpDestroyed)
labGuardsTeam = Team.Create({ LabGuard1, LabGuard2, LabGuard3 }) labGuardsTeam = Team.New({ LabGuard1, LabGuard2, LabGuard3 })
Team.AddEventHandler(labGuardsTeam.OnAllKilled, LabGuardsKilled) Team.AddEventHandler(labGuardsTeam.OnAllKilled, LabGuardsKilled)
civiliansTeam = Team.Create({ Civilian1, Civilian2 }) civiliansTeam = Team.New({ Civilian1, Civilian2 })
RunInitialActivities() RunInitialActivities()

View File

@@ -54,7 +54,7 @@ SendTrucks = function()
Actor.Move(truck, TruckExitPoint.Location) Actor.Move(truck, TruckExitPoint.Location)
Actor.RemoveSelf(truck) Actor.RemoveSelf(truck)
end) end)
local trucksTeam = Team.Create(trucks) local trucksTeam = Team.New(trucks)
Team.AddEventHandler(trucksTeam.OnAllRemovedFromWorld, MissionAccomplished) Team.AddEventHandler(trucksTeam.OnAllRemovedFromWorld, MissionAccomplished)
Team.AddEventHandler(trucksTeam.OnAnyKilled, MissionFailed) Team.AddEventHandler(trucksTeam.OnAnyKilled, MissionFailed)
end) end)