Merge pull request #6934 from abcdefg30/survival01

Ported Survival01 to Lua
This commit is contained in:
Matthias Mailänder
2014-11-23 15:17:42 +01:00
17 changed files with 2060 additions and 12 deletions

View File

@@ -220,6 +220,68 @@ namespace OpenRA.Mods.RA.Scripting
GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, context);
}
[Desc("Call a function when this actor is killed or captured. " +
"The callback function will be called as func().")]
public void OnKilledOrCaptured(Actor a, LuaFunction func)
{
var called = false;
var copy = (LuaFunction)func.CopyReference();
Action<Actor> OnKilledOrCaptured = m =>
{
try
{
if (called)
return;
copy.Call().Dispose();
copy.Dispose();
called = true;
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
GetScriptTriggers(a).OnCapturedInternal += OnKilledOrCaptured;
GetScriptTriggers(a).OnKilledInternal += OnKilledOrCaptured;
}
[Desc("Call a function when all of the actors in a group have been killed or captured. " +
"The callback function will be called as func().")]
public void OnAllKilledOrCaptured(Actor[] actors, LuaFunction func)
{
var group = actors.ToList();
var copy = (LuaFunction)func.CopyReference();
Action<Actor> OnMemberKilledOrCaptured = m =>
{
try
{
if (!group.Contains(m))
return;
group.Remove(m);
if (!group.Any())
{
copy.Call().Dispose();
copy.Dispose();
}
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
foreach (var a in group)
{
GetScriptTriggers(a).OnCapturedInternal += OnMemberKilledOrCaptured;
GetScriptTriggers(a).OnKilledInternal += OnMemberKilledOrCaptured;
}
}
[Desc("Call a function when a ground-based actor enters this cell footprint." +
"Returns the trigger id for later removal using RemoveFootprintTrigger(int id)." +
"The callback function will be called as func(Actor a, int id).")]
@@ -278,6 +340,64 @@ namespace OpenRA.Mods.RA.Scripting
context.World.ActorMap.RemoveCellTrigger(id);
}
[Desc("Call a function when an actor enters this range." +
"Returns the trigger id for later removal using RemoveProximityTrigger(int id)." +
"The callback function will be called as func(Actor a, int id).")]
public int OnEnteredProximityTrigger(WPos pos, WRange range, LuaFunction func)
{
var triggerId = 0;
var onEntry = (LuaFunction)func.CopyReference();
Action<Actor> invokeEntry = a =>
{
try
{
using (var luaActor = a.ToLuaValue(context))
using (var id = triggerId.ToLuaValue(context))
onEntry.Call(luaActor, id).Dispose();
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
triggerId = context.World.ActorMap.AddProximityTrigger(pos, range, invokeEntry, null);
return triggerId;
}
[Desc("Call a function when an actor leaves this range." +
"Returns the trigger id for later removal using RemoveProximityTrigger(int id)." +
"The callback function will be called as func(Actor a, int id).")]
public int OnExitedProximityTrigger(WPos pos, WRange range, LuaFunction func)
{
var triggerId = 0;
var onExit = (LuaFunction)func.CopyReference();
Action<Actor> invokeExit = a =>
{
try
{
using (var luaActor = a.ToLuaValue(context))
using (var id = triggerId.ToLuaValue(context))
onExit.Call(luaActor, id).Dispose();
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
triggerId = context.World.ActorMap.AddProximityTrigger(pos, range, null, invokeExit);
return triggerId;
}
[Desc("Removes a previously created proximitry trigger.")]
public void RemoveProximityTrigger(int id)
{
context.World.ActorMap.RemoveProximityTrigger(id);
}
[Desc("Call a function when this actor is infiltrated. The callback function " +
"will be called as func(Actor self, Actor infiltrator).")]
public void OnInfiltrated(Actor a, LuaFunction func)

View File

@@ -0,0 +1,73 @@
#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 Eluant;
using OpenRA.Mods.Common.Power;
using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Scripting
{
[ScriptPropertyGroup("Power")]
public class PlayerPowerProperties : ScriptPlayerProperties, Requires<PowerManagerInfo>
{
readonly PowerManager pm;
public PlayerPowerProperties(ScriptContext context, Player player)
: base(context, player)
{
pm = player.PlayerActor.Trait<PowerManager>();
}
[Desc("Returns the total of the power the player has.")]
public int PowerProvided
{
get { return pm.PowerProvided; }
}
[Desc("Returns the power used by the player.")]
public int PowerDrained
{
get { return pm.PowerDrained; }
}
[Desc("Returns the player's power state " +
"(\"Normal\", \"Low\" or \"Critical\").")]
public string PowerState
{
get { return pm.PowerState.ToString(); }
}
[Desc("Triggers low power for the chosen amount of ticks.")]
public void TriggerPowerOutage(int ticks)
{
pm.TriggerPowerOutage(ticks);
}
}
[ScriptPropertyGroup("Power")]
public class ActorPowerProperties : ScriptActorProperties, Requires<PowerInfo>
{
readonly PowerInfo pi;
public ActorPowerProperties(ScriptContext context, Actor self)
: base(context, self)
{
pi = self.Info.Traits.GetOrDefault<PowerInfo>();
}
[Desc("Returns the power drained/provided by this actor.")]
public int Power
{
get { return pi.Amount; }
}
}
}

View File

@@ -32,6 +32,7 @@ namespace OpenRA.Mods.RA.Scripting
readonly World world;
public event Action<Actor> OnKilledInternal = _ => { };
public event Action<Actor> OnCapturedInternal = _ => { };
public event Action<Actor> OnRemovedInternal = _ => { };
public event Action<Actor, Actor> OnProducedInternal = (a, b) => { };
public event Action<Actor, Actor> OnOtherProducedInternal = (a, b) => { };
@@ -236,6 +237,9 @@ namespace OpenRA.Mods.RA.Scripting
return;
}
}
// Run any internally bound callbacks
OnCapturedInternal(self);
}
public void Infiltrated(Actor self, Actor infiltrator)