diff --git a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs index e64fe77646..5085a319d5 100644 --- a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs @@ -406,6 +406,20 @@ namespace OpenRA.Mods.Common.Scripting GetScriptTriggers(a).RegisterCallback(Trigger.OnInfiltrated, func, Context); } + [Desc("Call a function when this actor is discovered by an enemy or a player with a Neutral stance. " + + "The callback function will be called as func(Actor discovered, Player discoverer)")] + public void OnDiscovered(Actor a, LuaFunction func) + { + GetScriptTriggers(a).RegisterCallback(Trigger.OnDiscovered, func, Context); + } + + [Desc("Call a function when this player is discovered by an enemy or neutral player. " + + "The callback function will be called as func(Player discovered, Player discoverer, Actor discoveredActor)")] + public void OnPlayerDiscovered(Player discovered, LuaFunction func) + { + GetScriptTriggers(discovered.PlayerActor).RegisterCallback(Trigger.OnPlayerDiscovered, func, Context); + } + [Desc("Removes all triggers from this actor." + "Note that the removal will only take effect at the end of a tick, " + "so you must not add new triggers at the same time that you are calling this function.")] diff --git a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs index 3c887b6028..298cb806fb 100644 --- a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs +++ b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs @@ -21,7 +21,8 @@ namespace OpenRA.Mods.Common.Scripting public enum Trigger { OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost, - OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated, OnAddedToWorld, OnRemovedFromWorld + OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated, + OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered } [Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")] @@ -31,7 +32,7 @@ namespace OpenRA.Mods.Common.Scripting } public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction, - INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable + INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable, INotifyDiscovered { readonly World world; @@ -324,6 +325,40 @@ namespace OpenRA.Mods.Common.Scripting OnOtherProducedInternal(producee, produced); } + public void OnDiscovered(Actor self, Player discoverer, bool playNotification) + { + foreach (var f in Triggers[Trigger.OnDiscovered]) + { + try + { + using (var a = self.ToLuaValue(f.Second)) + using (var b = discoverer.ToLuaValue(f.Second)) + f.First.Call(a, b).Dispose(); + } + catch (Exception ex) + { + f.Second.FatalError(ex.Message); + return; + } + } + + foreach (var f in Triggers[Trigger.OnPlayerDiscovered]) + { + try + { + using (var a = self.Owner.ToLuaValue(f.Second)) + using (var b = discoverer.ToLuaValue(f.Second)) + using (var c = self.ToLuaValue(f.Second)) + f.First.Call(a, b, c).Dispose(); + } + catch (Exception ex) + { + f.Second.FatalError(ex.Message); + return; + } + } + } + public void Clear(Trigger trigger) { world.AddFrameEndTask(w =>