Merge pull request #4530 from obrakmann/lua
Added a few Lua functions, attempt 2
This commit is contained in:
@@ -299,12 +299,12 @@ namespace OpenRA.Mods.RA.Scripting
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
public void AttackMove(Actor actor, CPos location)
|
||||
public void AttackMove(Actor actor, CPos location, double nearEnough)
|
||||
{
|
||||
if (actor.HasTrait<AttackMove>())
|
||||
actor.QueueActivity(new AttackMove.AttackMoveActivity(actor, new Move.Move(location, 0)));
|
||||
actor.QueueActivity(new AttackMove.AttackMoveActivity(actor, new Move.Move(location, (int)nearEnough)));
|
||||
else
|
||||
actor.QueueActivity(new Move.Move(location, 0));
|
||||
actor.QueueActivity(new Move.Move(location, (int)nearEnough));
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
@@ -330,5 +330,47 @@ namespace OpenRA.Mods.RA.Scripting
|
||||
{
|
||||
return mapActors[actorName];
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
public Actor[] FindActorsInBox(WPos topLeft, WPos bottomRight)
|
||||
{
|
||||
return world.FindActorsInBox(topLeft, bottomRight).ToArray();
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
public Actor[] FindActorsInCircle(WPos location, WRange radius)
|
||||
{
|
||||
return world.FindActorsInCircle(location, radius).ToArray();
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
public void BuildWithSharedQueue(Player player, string unit, double amount)
|
||||
{
|
||||
var ri = Rules.Info[unit];
|
||||
if (ri == null || !ri.Traits.Contains<BuildableInfo>())
|
||||
return;
|
||||
|
||||
var category = ri.Traits.Get<BuildableInfo>().Queue;
|
||||
|
||||
var queue = world.ActorsWithTrait<ClassicProductionQueue>()
|
||||
.Where(a => a.Actor.Owner == player && a.Trait.Info.Type == category)
|
||||
.Select(a => a.Trait).FirstOrDefault();
|
||||
|
||||
if (queue != null)
|
||||
queue.ResolveOrder(queue.self, Order.StartProduction(queue.self, unit, (int)amount));
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
public void BuildWithPerFactoryQueue(Actor factory, string unit, double amount)
|
||||
{
|
||||
if (!factory.HasTrait<ProductionQueue>())
|
||||
return;
|
||||
|
||||
var ri = Rules.Info[unit];
|
||||
if (ri == null || !ri.Traits.Contains<BuildableInfo>())
|
||||
return;
|
||||
|
||||
factory.Trait<ProductionQueue>().ResolveOrder(factory, Order.StartProduction(factory, unit, (int)amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,3 +180,4 @@ LuaScripts:
|
||||
mods/common/lua/reinforcements.lua
|
||||
mods/common/lua/supportpowers.lua
|
||||
mods/common/lua/rules.lua
|
||||
mods/common/lua/production.lua
|
||||
|
||||
@@ -47,8 +47,8 @@ Actor.Teleport = function(actor, location)
|
||||
actor:QueueActivity(OpenRA.New("SimpleTeleport", { location }))
|
||||
end
|
||||
|
||||
Actor.AttackMove = function(actor, location)
|
||||
Internal.AttackMove(actor, location)
|
||||
Actor.AttackMove = function(actor, location, nearEnough)
|
||||
Internal.AttackMove(actor, location, nearEnough or 0)
|
||||
end
|
||||
|
||||
Actor.HeliFly = function(actor, position)
|
||||
@@ -178,3 +178,25 @@ end
|
||||
Actor.Trait = function(actor, className)
|
||||
return Internal.Trait(actor, className)
|
||||
end
|
||||
|
||||
Actor.ReturnToBase = function(actor, airfield)
|
||||
actor:QueueActivity(OpenRA.New("ReturnToBase", { actor, airfield }))
|
||||
end
|
||||
|
||||
Actor.Patrol = function(actor, waypoints, wait, loop)
|
||||
Utils.Do(waypoints, function(wpt)
|
||||
Actor.AttackMove(actor, wpt.Location, 3)
|
||||
Actor.Wait(actor, wait or 0)
|
||||
end)
|
||||
if loop or loop == nil then
|
||||
Actor.CallFunc(actor, function() Actor.Patrol(actor, waypoints, wait, loop) end)
|
||||
end
|
||||
end
|
||||
|
||||
Actor.PatrolUntil = function(actor, waypoints, wait, func)
|
||||
if func == nil then error("No function specified", 2) end
|
||||
Actor.Patrol(actor, waypoints, wait, false)
|
||||
if not func(actor) then
|
||||
Actor.CallFunc(actor, function() Actor.PatrolUntil(actor, waypoints, wait, func) end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,6 +16,48 @@ Map.GetNamedActor = function(actorName)
|
||||
return Internal.GetNamedActor(actorName)
|
||||
end
|
||||
|
||||
Map.FindActorsInCircle = function(location, radius, func)
|
||||
local actors = Internal.FindActorsInCircle(location.CenterPosition, WRange.FromCells(radius))
|
||||
return Utils.EnumerableWhere(actors, func)
|
||||
end
|
||||
|
||||
Map.FindActorsInBox = function(topLeft, bottomRight, func)
|
||||
local actors = Internal.FindActorsInBox(topLeft.CenterPosition, bottomRight.CenterPosition)
|
||||
return Utils.EnumerableWhere(actors, func)
|
||||
end
|
||||
|
||||
Map.__FilterByTrait = function(a, player, trait)
|
||||
return Actor.Owner(a) == player and Actor.HasTrait(a, trait)
|
||||
end
|
||||
|
||||
Map.__FilterByTraitAndIdle = function(a, player, trait)
|
||||
return Map.__FilterByTrait(a, player, trait) and Actor.IsIdle(a)
|
||||
end
|
||||
|
||||
Map.FindUnitsInCircle = function(player, location, radius)
|
||||
return Map.FindActorsInCircle(location, radius, function(a) return Map.__FilterByTrait(a, player, "Mobile") end)
|
||||
end
|
||||
|
||||
Map.FindUnitsInBox = function(player, topLeft, bottomRight)
|
||||
return Map.FindActorsInBox(topLeft, bottomRight, function(a) return Map.__FilterByTrait(a, player, "Mobile") end)
|
||||
end
|
||||
|
||||
Map.FindStructuresInCircle = function(player, location, radius)
|
||||
return Map.FindActorsInCircle(location, radius, function(a) return Map.__FilterByTrait(a, player, "Building") end)
|
||||
end
|
||||
|
||||
Map.FindStructuresInBox = function(player, topLeft, bottomRight)
|
||||
return Map.FindActorsInBox(topLeft, bottomRight, function(a) return Map.__FilterByTrait(a, player, "Building") end)
|
||||
end
|
||||
|
||||
Map.FindIdleUnitsInCircle = function(player, location, radius)
|
||||
return Map.FindActorsInCircle(location, radius, function(a) return Map.__FilterByTraitAndIdle(a, player, "Mobile") end)
|
||||
end
|
||||
|
||||
Map.FindIdleUnitsInBox = function(player, topLeft, bottomRight)
|
||||
return Map.FindActorsInBox(topLeft, bottomRight, function(a) return Map.__FilterByTraitAndIdle(a, player, "Mobile") end)
|
||||
end
|
||||
|
||||
CPos.New = function(x, y)
|
||||
return OpenRA.New("CPos", { { x, "Int32" }, { y, "Int32" } })
|
||||
end
|
||||
|
||||
@@ -10,6 +10,7 @@ OpenRA.New = function(className, args)
|
||||
end
|
||||
|
||||
OpenRA.RunAfterDelay = function(delay, func)
|
||||
if func == nil then error("No function specified", 2) end
|
||||
Internal.RunAfterDelay(delay, func)
|
||||
end
|
||||
|
||||
@@ -42,7 +43,11 @@ OpenRA.SetWinState = function(player, winState)
|
||||
end
|
||||
|
||||
OpenRA.GetRandomInteger = function(low, high)
|
||||
return Internal.GetRandomInteger(low, high)
|
||||
if high <= low then
|
||||
return low
|
||||
else
|
||||
return Internal.GetRandomInteger(low, high)
|
||||
end
|
||||
end
|
||||
|
||||
OpenRA.TakeOre = function(player, amount)
|
||||
|
||||
17
mods/common/lua/production.lua
Normal file
17
mods/common/lua/production.lua
Normal file
@@ -0,0 +1,17 @@
|
||||
Production = { }
|
||||
|
||||
Production.BuildWithSharedQueue = function(player, unit, amount)
|
||||
Internal.BuildWithSharedQueue(player, unit, amount or 1)
|
||||
end
|
||||
|
||||
Production.BuildWithPerFactoryQueue = function(factory, unit, amount)
|
||||
Internal.BuildWithPerFactoryQueue(factory, unit, amount or 1)
|
||||
end
|
||||
|
||||
Production.SetRallyPoint = function(factory, location)
|
||||
Actor.Trait(factory, "RallyPoint").rallyPoint = location.Location
|
||||
end
|
||||
|
||||
Production.SetPrimaryBuilding = function(factory)
|
||||
Actor.Trait(factory, "PrimaryBuilding"):SetPrimaryProducer(factory, true)
|
||||
end
|
||||
@@ -12,7 +12,7 @@ Team.New = function(actors)
|
||||
end
|
||||
|
||||
Team.AddActorEventHandlers = function(team)
|
||||
for i, actor in ipairs(team.Actors) do
|
||||
Team.Do(team, function(actor)
|
||||
|
||||
Actor.OnKilled(actor, function()
|
||||
Team.InvokeHandlers(team.OnAnyKilled)
|
||||
@@ -23,13 +23,11 @@ Team.AddActorEventHandlers = function(team)
|
||||
Team.InvokeHandlers(team.OnAnyRemovedFromWorld)
|
||||
if not Team.AnyAreInWorld(team) then Team.InvokeHandlers(team.OnAllRemovedFromWorld) end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
Team.InvokeHandlers = function(event)
|
||||
for i, handler in ipairs(event) do
|
||||
handler()
|
||||
end
|
||||
Utils.Do(event, function(handler) handler() end)
|
||||
end
|
||||
|
||||
Team.AllAreDead = function(team)
|
||||
@@ -59,3 +57,11 @@ end
|
||||
Team.Do = function(team, func)
|
||||
Utils.Do(team.Actors, func)
|
||||
end
|
||||
|
||||
Team.Patrol = function(team, waypoints, wait, loop)
|
||||
Team.Do(team, function(a) Actor.Patrol(a, waypoints, wait, loop) end)
|
||||
end
|
||||
|
||||
Team.PatrolUntil = function(team, waypoints, wait, func)
|
||||
Team.Do(team, function(a) Actor.PatrolUntil(a, waypoints, wait, func) end)
|
||||
end
|
||||
|
||||
@@ -75,3 +75,12 @@ end
|
||||
Utils.TableToArray = function(luaTable)
|
||||
return Internal.TableToArray(luaTable)
|
||||
end
|
||||
|
||||
Utils.Seconds = function(seconds)
|
||||
local TicksPerSecond = 25
|
||||
return seconds * TicksPerSecond
|
||||
end
|
||||
|
||||
Utils.Minutes = function(minutes)
|
||||
return Utils.Seconds(minutes * 60)
|
||||
end
|
||||
|
||||
@@ -161,3 +161,4 @@ LuaScripts:
|
||||
mods/common/lua/reinforcements.lua
|
||||
mods/common/lua/supportpowers.lua
|
||||
mods/common/lua/rules.lua
|
||||
mods/common/lua/production.lua
|
||||
|
||||
@@ -177,3 +177,4 @@ LuaScripts:
|
||||
mods/common/lua/reinforcements.lua
|
||||
mods/common/lua/supportpowers.lua
|
||||
mods/common/lua/rules.lua
|
||||
mods/common/lua/production.lua
|
||||
|
||||
@@ -199,3 +199,4 @@ LuaScripts:
|
||||
mods/common/lua/reinforcements.lua
|
||||
mods/common/lua/supportpowers.lua
|
||||
mods/common/lua/rules.lua
|
||||
mods/common/lua/production.lua
|
||||
|
||||
Reference in New Issue
Block a user