#region Copyright & License Information /* * Copyright 2007-2015 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 System.Collections.Generic; using System.Linq; using Eluant; using OpenRA.Primitives; using OpenRA.Scripting; using OpenRA.Traits; namespace OpenRA.Mods.Common.Scripting { public enum Trigger { OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost, OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated, OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered } [Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")] public class ScriptTriggersInfo : ITraitInfo { public object Create(ActorInitializer init) { return new ScriptTriggers(init.World); } } public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction, INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable, INotifyDiscovered { readonly World world; public event Action OnKilledInternal = _ => { }; public event Action OnCapturedInternal = _ => { }; public event Action OnRemovedInternal = _ => { }; public event Action OnProducedInternal = (a, b) => { }; public event Action OnOtherProducedInternal = (a, b) => { }; public Dictionary>> Triggers = new Dictionary>>(); public ScriptTriggers(World world) { this.world = world; foreach (Trigger t in Enum.GetValues(typeof(Trigger))) Triggers.Add(t, new List>()); } public void RegisterCallback(Trigger trigger, LuaFunction func, ScriptContext context) { Triggers[trigger].Add(Pair.New((LuaFunction)func.CopyReference(), context)); } public void TickIdle(Actor self) { foreach (var f in Triggers[Trigger.OnIdle]) { try { using (var a = self.ToLuaValue(f.Second)) f.First.Call(a).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void Damaged(Actor self, AttackInfo e) { foreach (var f in Triggers[Trigger.OnDamaged]) { try { using (var a = self.ToLuaValue(f.Second)) using (var b = e.Attacker.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void Killed(Actor self, AttackInfo e) { // Run Lua callbacks foreach (var f in Triggers[Trigger.OnKilled]) { try { using (var a = self.ToLuaValue(f.Second)) using (var b = e.Attacker.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } // Run any internally bound callbacks OnKilledInternal(self); } public void UnitProduced(Actor self, Actor other, CPos exit) { // Run Lua callbacks foreach (var f in Triggers[Trigger.OnProduction]) { try { using (var a = self.ToLuaValue(f.Second)) using (var b = other.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } // Run any internally bound callbacks OnProducedInternal(self, other); } public void OnPlayerWon(Player player) { foreach (var f in Triggers[Trigger.OnPlayerWon]) { try { using (var a = player.ToLuaValue(f.Second)) f.First.Call(a).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void OnPlayerLost(Player player) { foreach (var f in Triggers[Trigger.OnPlayerLost]) { try { using (var a = player.ToLuaValue(f.Second)) f.First.Call(a).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void OnObjectiveAdded(Player player, int id) { foreach (var f in Triggers[Trigger.OnObjectiveAdded]) { try { using (var a = player.ToLuaValue(f.Second)) using (var b = id.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void OnObjectiveCompleted(Player player, int id) { foreach (var f in Triggers[Trigger.OnObjectiveCompleted]) { try { using (var a = player.ToLuaValue(f.Second)) using (var b = id.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void OnObjectiveFailed(Player player, int id) { foreach (var f in Triggers[Trigger.OnObjectiveFailed]) { try { using (var a = player.ToLuaValue(f.Second)) using (var b = id.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner) { foreach (var f in Triggers[Trigger.OnCapture]) { try { using (var a = self.ToLuaValue(f.Second)) using (var b = captor.ToLuaValue(f.Second)) using (var c = oldOwner.ToLuaValue(f.Second)) using (var d = newOwner.ToLuaValue(f.Second)) f.First.Call(a, b, c, d).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } // Run any internally bound callbacks OnCapturedInternal(self); } public void Infiltrated(Actor self, Actor infiltrator) { foreach (var f in Triggers[Trigger.OnInfiltrated]) { try { using (var a = self.ToLuaValue(f.Second)) using (var b = infiltrator.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void AddedToWorld(Actor self) { foreach (var f in Triggers[Trigger.OnAddedToWorld]) { try { using (var a = self.ToLuaValue(f.Second)) f.First.Call(a).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } } public void RemovedFromWorld(Actor self) { // Run Lua callbacks foreach (var f in Triggers[Trigger.OnRemovedFromWorld]) { try { using (var a = self.ToLuaValue(f.Second)) f.First.Call(a).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } // Run any internally bound callbacks OnRemovedInternal(self); } public void UnitProducedByOther(Actor self, Actor producee, Actor produced) { // Run Lua callbacks foreach (var f in Triggers[Trigger.OnOtherProduction]) { try { using (var a = producee.ToLuaValue(f.Second)) using (var b = produced.ToLuaValue(f.Second)) f.First.Call(a, b).Dispose(); } catch (Exception ex) { f.Second.FatalError(ex.Message); return; } } // Run any internally bound callbacks 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 => { Triggers[trigger].Select(p => p.First).Do(f => f.Dispose()); Triggers[trigger].Clear(); }); } public void ClearAll() { foreach (Trigger t in Enum.GetValues(typeof(Trigger))) Clear(t); } public void Dispose() { ClearAll(); } } }