Merge pull request #11065 from abcdefg30/luaMapActors

Add a lua method for querying all actors that are currently InWorld
This commit is contained in:
Matthias Mailänder
2016-04-10 18:57:14 +02:00
19 changed files with 51 additions and 51 deletions

View File

@@ -21,10 +21,13 @@ namespace OpenRA.Mods.Common.Scripting
public class MapGlobal : ScriptGlobal
{
readonly SpawnMapActors sma;
readonly World world;
public MapGlobal(ScriptContext context)
: base(context)
{
sma = context.World.WorldActor.Trait<SpawnMapActors>();
world = context.World;
// Register map actors as globals (yuck!)
foreach (var kv in sma.Actors)
@@ -133,5 +136,8 @@ namespace OpenRA.Mods.Common.Scripting
{
return Context.World.ActorsHavingTrait<ScriptTags>(t => t.HasTag(tag)).ToArray();
}
[Desc("Returns a table of all the actors that are currently on the map/in the world.")]
public Actor[] ActorsInWorld { get { return world.Actors.ToArray(); } }
}
}

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections;
using System.Linq;
using Eluant;
using OpenRA.Scripting;
@@ -54,6 +55,20 @@ namespace OpenRA.Mods.Common.Scripting
return true;
}
[Desc("Returns the original collection filtered with the func.")]
public LuaTable Where(LuaValue[] collection, LuaFunction func)
{
var t = Context.CreateTable();
foreach (var c in collection)
using (var ret = func.Call(c))
using (var result = ret.FirstOrDefault())
if (result != null && result.ToBoolean())
t.Add(t.Count + 1, c);
return t;
}
[Desc("Returns the first n values from a collection.")]
public LuaValue[] Take(int n, LuaValue[] source)
{

View File

@@ -26,7 +26,7 @@ getActors = function(owner, units)
local maxUnits = 0
local actors = { }
for type, count in pairs(units) do
local globalActors = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local globalActors = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == owner and actor.Type == type and not actor.IsDead
end)
if #globalActors < count then
@@ -234,7 +234,7 @@ Tick = function()
end
checkProduction = function(player)
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == player and actor.Type == UnitToRebuild
end)
@@ -251,7 +251,7 @@ checkProduction = function(player)
end
getStartUnits = function()
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == enemy
end)
Utils.Do(Units, function(unit)

View File

@@ -180,7 +180,7 @@ getActors = function(owner, units)
local maxUnits = 0
local actors = { }
for type, count in pairs(units) do
local globalActors = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local globalActors = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == owner and actor.Type == type and not actor.IsDead
end)
if #globalActors < count then
@@ -196,7 +196,7 @@ getActors = function(owner, units)
end
checkProduction = function(player)
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == player and actor.Type == UnitToRebuild
end)
@@ -213,7 +213,7 @@ checkProduction = function(player)
end
getStartUnits = function()
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == enemy
end)
Utils.Do(Units, function(unit)

View File

@@ -275,7 +275,7 @@ CheckForSams = function(player)
end
checkProduction = function(player)
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == enemy
end)
@@ -304,7 +304,7 @@ checkProduction = function(player)
end
getStartUnits = function()
local Units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
local Units = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.Owner == enemy and ( actor.Type == 'e2' or actor.Type == 'e1' or actor.Type == 'jeep' or actor.Type == 'mtnk')
end)
Utils.Do(Units, function(unit)

View File

@@ -88,9 +88,7 @@ WorldLoaded = function()
InitObjectives()
Trigger.OnRemovedFromWorld(AtreidesConyard, function()
local refs = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
return actor.Type == "refinery"
end)
local refs = Utils.Where(Map.ActorsInWorld, function(actor) return actor.Type == "refinery" end)
if #refs == 0 then
harkonnen.MarkCompletedObjective(KillAtreides)

View File

@@ -88,9 +88,7 @@ WorldLoaded = function()
InitObjectives()
Trigger.OnRemovedFromWorld(AtreidesConyard, function()
local refs = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
return actor.Type == "refinery"
end)
local refs = Utils.Where(Map.ActorsInWorld, function(actor) return actor.Type == "refinery" end)
if #refs == 0 then
harkonnen.MarkCompletedObjective(KillAtreides)

View File

@@ -145,11 +145,12 @@ InitTriggers = function()
end)
Trigger.OnKilled(ExplosiveBarrel, function()
local bridge = Map.ActorsInBox(USSRReinforcementsCameraWaypoint.CenterPosition, USSRReinforcementsEntryWaypoint.CenterPosition,
function(self) return self.Type == "bridge1" end)
if not bridge[1].IsDead then
bridge[1].Kill()
-- We need the first bridge which is returned
local bridge = Utils.Where(Map.ActorsInWorld, function(actor) return actor.Type == "bridge1" end)[1]
if not bridge.IsDead then
bridge.Kill()
end
end)
@@ -237,7 +238,7 @@ InitTriggers = function()
end)
Trigger.AfterDelay(0, function()
local bridges = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Type == "bridge1" end)
local bridges = Utils.Where(Map.ActorsInWorld, function(actor) return actor.Type == "bridge1" end)
Trigger.OnAllKilled(bridges, function()
player.MarkCompletedObjective(KillBridges)

View File

@@ -348,7 +348,7 @@ InitTriggers = function()
end)
Trigger.AfterDelay(0, function()
local bridges = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Type == "bridge1" or self.Type == "bridge2" end)
local bridges = Utils.Where(Map.ActorsInWorld, function(actor) return actor.Type == "bridge1" or actor.Type == "bridge2" end)
ExplodingBridge = bridges[1]
Trigger.OnAllKilled(bridges, function()

View File

@@ -105,9 +105,9 @@ ProtectHarvester = function(unit)
end
InitAIUnits = function()
IdlingUnits = Map.ActorsInBox(MainBaseTopLeft.CenterPosition, Map.BottomRight, function(self) return self.Owner == ussr and self.HasProperty("Hunt") end)
IdlingUnits = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == ussr and self.HasProperty("Hunt") and self.Location.Y > MainBaseTopLeft.Location.Y end)
local buildings = Map.ActorsInBox(MainBaseTopLeft.CenterPosition, Map.BottomRight, function(self) return self.Owner == ussr and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == ussr and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building)
if building.Owner == ussr and building.Health < building.MaxHealth * 3/4 then
@@ -255,7 +255,7 @@ ProduceAircraft = function()
Trigger.OnIdle(units[1], function()
if not target or target.IsDead or (not target.IsInWorld) then
local enemies = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == greece and self.HasProperty("Health") end)
local enemies = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == greece and self.HasProperty("Health") end)
if #enemies > 0 then
target = Utils.Random(enemies)
units[1].Attack(target)

View File

@@ -226,11 +226,6 @@ ActivatePatrols = function()
GroupPatrol(PatrolA, PatrolAPath, DateTime.Seconds(7))
GroupPatrol(PatrolB, PatrolBPath, DateTime.Seconds(6))
end)
local units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == soviets and self.HasProperty("AutoTarget") end)
Utils.Do(units, function(unit)
unit.Stance = "Defend"
end)
end
InitTriggers = function()

View File

@@ -168,9 +168,7 @@ SuperTankDomeInfiltrated = function()
end
SuperTanksDestruction = function()
local badGuys = Map.ActorsInBox(Map.TopLeft, Map.BottomRight,
function(self) return self.Owner == badguy and self.HasProperty("Health") end)
local badGuys = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == badguy and self.HasProperty("Health") end)
Utils.Do(badGuys, function(unit)
unit.Kill()
end)

View File

@@ -36,7 +36,7 @@ WorldLoaded = function()
end)
Trigger.AfterDelay(0, function()
local buildings = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == germany and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == germany and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building, attacker)
if building.Owner == germany and building.Health < building.MaxHealth * 0.8 then

View File

@@ -128,7 +128,7 @@ WorldLoaded = function()
end
end)
Trigger.AfterDelay(0, function()
local buildings = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == enemy and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == enemy and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building, attacker)
if building.Owner == enemy and building.Health < building.MaxHealth * 0.8 then

View File

@@ -581,9 +581,6 @@ Actors:
ReinfRoadPoint: waypoint
Location: 52,42
Owner: Neutral
NWIdlePoint: waypoint
Location: 38,42
Owner: Neutral
EIslandPoint: waypoint
Location: 51,58
Owner: Neutral

View File

@@ -1,7 +1,7 @@
IdleHunt = function(unit) if not unit.IsDead then Trigger.OnIdle(unit, unit.Hunt) end end
IdlingUnits = function()
local lazyUnits = Map.ActorsInBox(NWIdlePoint.CenterPosition, Map.BottomRight, function(actor)
local lazyUnits = Utils.Where(Map.ActorsInWorld, function(actor)
return actor.HasProperty("Hunt") and (actor.Owner == GoodGuy or actor.Owner == Greece) end)
Utils.Do(lazyUnits, function(unit)
@@ -12,7 +12,8 @@ IdlingUnits = function()
end)
end
BaseBuildings = {
BaseBuildings =
{
{ type = "powr", pos = CVec.New(3, -2), cost = 300 },
{ type = "tent", pos = CVec.New(0, 4), cost = 400 },
{ type = "hbox", pos = CVec.New(3, 6), cost = 600 },

View File

@@ -15,11 +15,7 @@ CheckForCYard = function()
end
CheckForSPen = function()
SPens = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(actor)
return actor.Type == "spen"
end)
return #SPens >=1
return Utils.Any(Map.ActorsInWorld, function(actor) return actor.Type == "spen" end)
end
RunInitialActivities = function()
@@ -36,7 +32,7 @@ RunInitialActivities = function()
IdlingUnits()
Media.PlaySpeechNotification(player, "ReinforcementsArrived")
local buildings = Map.ActorsInBox(NWIdlePoint.CenterPosition, Map.BottomRight, function(self) return self.Owner == Greece and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == Greece and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building)
if building.Owner == Greece and building.Health < building.MaxHealth * 3/4 then

View File

@@ -382,7 +382,7 @@ SetupSoviets = function()
end)
Trigger.AfterDelay(0, function()
local buildings = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == soviets and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == soviets and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building)
if building.Owner == soviets and building.Health < building.MaxHealth * DamageModifier then

View File

@@ -378,7 +378,7 @@ SetupSoviets = function()
end)
Trigger.AfterDelay(0, function()
local buildings = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == soviets and self.HasProperty("StartBuildingRepairs") end)
local buildings = Utils.Where(Map.ActorsInWorld, function(self) return self.Owner == soviets and self.HasProperty("StartBuildingRepairs") end)
Utils.Do(buildings, function(actor)
Trigger.OnDamaged(actor, function(building)
if building.Owner == soviets and building.Health < building.MaxHealth * 3/4 then
@@ -386,11 +386,6 @@ SetupSoviets = function()
end
end)
end)
local units = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(self) return self.Owner == soviets and self.HasProperty("AutoTarget") end)
Utils.Do(units, function(unit)
unit.Stance = "Defend"
end)
end)
end