public void PlayMusic(string track = null, LuaFunction func = null) { if (!playlist.IsMusicAvailable) return; var musicInfo = !string.IsNullOrEmpty(track) ? GetMusicTrack(track) : playlist.GetNextSong(); if (func != null) { var f = func.CopyReference() as LuaFunction; onComplete = () => { try { using (f) f.Call().Dispose(); } catch (LuaException e) { Context.FatalError(e.Message); } }; playlist.Play(musicInfo, onComplete); } else playlist.Play(musicInfo); }
public void OnAllKilled(LuaTable actors, LuaFunction func) { List<Actor> group = new List<Actor>(); foreach (var kv in actors) { Actor actor; if (!kv.Value.TryGetClrValue<Actor>(out actor)) throw new LuaException("OnAllKilled requires a table of int,Actor pairs. Recieved {0},{1}".F(kv.Key.GetType().Name, kv.Value.GetType().Name)); group.Add(actor); } var copy = (LuaFunction)func.CopyReference(); Action<Actor> OnMemberKilled = m => { group.Remove(m); if (!group.Any()) { copy.Call(); copy.Dispose(); } }; foreach (var a in group) GetScriptTriggers(a).OnKilledInternal += OnMemberKilled; }
public Actor[] Reinforce(Player owner, string[] actorTypes, CPos[] entryPath, int interval = 25, LuaFunction actionFunc = null) { var actors = new List<Actor>(); for (var i = 0; i < actorTypes.Length; i++) { var af = actionFunc != null ? actionFunc.CopyReference() as LuaFunction : null; var actor = CreateActor(owner, actorTypes[i], false, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); actors.Add(actor); var actionDelay = i * interval; Action actorAction = () => { Context.World.Add(actor); for (var j = 1; j < entryPath.Length; j++) Move(actor, entryPath[j]); if (af != null) { actor.QueueActivity(new CallFunc(() => { af.Call(actor.ToLuaValue(Context)); af.Dispose(); })); } }; Context.World.AddFrameEndTask(w => w.Add(new DelayedAction(actionDelay, actorAction))); } return actors.ToArray(); }
public void Dispose() { if (function == null) return; function.Dispose(); function = null; }
public Player[] GetPlayers(LuaFunction filter) { return Context.World.Players .Where(p => { using (var f = filter.Call(p.ToLuaValue(Context))) return f.First().ToBoolean(); }).ToArray(); }
public bool Any(LuaValue[] collection, LuaFunction func) { foreach (var c in collection) using (var ret = func.Call(c)) using (var result = ret.FirstOrDefault()) if (result != null && result.ToBoolean()) return true; return false; }
public LuaTable ActorsInCircle(WPos location, WRange radius, LuaFunction filter) { var actors = context.World.FindActorsInCircle(location, radius) .Select(a => a.ToLuaValue(context)) .Where(a => { using (var f = filter.Call(a)) return f.First().ToBoolean(); }); return actors.ToLuaTable(context); }
protected void Dispose(bool disposing) { if (function == null) return; if (disposing) { function.Dispose(); function = null; } }
public LuaTable GetPlayers(LuaFunction filter) { var players = context.World.Players .Select(p => p.ToLuaValue(context)) .Where(a => { using (var f = filter.Call(a)) return f.First().ToBoolean(); }); return players.ToLuaTable(context); }
public bool Any(LuaTable table, LuaFunction func) { foreach (var kv in table) { using (var ret = func.Call(kv.Value)) { var result = ret.FirstOrDefault(); if (result != null && result.ToBoolean()) return true; } } return false; }
public bool All(LuaValue[] collection, LuaFunction func) { foreach (var c in collection) { using (var ret = func.Call(c)) { var result = ret.FirstOrDefault(); if (result == null || !result.ToBoolean()) return false; } } return true; }
public Actor[] ActorsInBox(WPos topLeft, WPos bottomRight, LuaFunction filter = null) { var actors = Context.World.ActorMap.ActorsInBox(topLeft, bottomRight); if (filter != null) { actors = actors.Where(a => { using (var f = filter.Call(a.ToLuaValue(Context))) return f.First().ToBoolean(); }); } return actors.ToArray(); }
public Actor[] ActorsInCircle(WPos location, WDist radius, LuaFunction filter = null) { var actors = Context.World.FindActorsInCircle(location, radius); if (filter != null) { actors = actors.Where(a => { using (var f = filter.Call(a.ToLuaValue(Context))) return f.First().ToBoolean(); }); } return actors.ToArray(); }
public void AfterDelay(int delay, LuaFunction func) { var f = (LuaFunction)func.CopyReference(); Action doCall = () => { try { using (f) f.Call().Dispose(); } catch (Exception e) { Context.FatalError(e.Message); } }; Context.World.AddFrameEndTask(w => w.Add(new DelayedAction(delay, doCall))); }
public void PlayMovieFullscreen(string movie, LuaFunction func = null) { if (func != null) { var f = func.CopyReference() as LuaFunction; onCompleteFullscreen = () => { try { using (f) f.Call().Dispose(); } catch (LuaException e) { Context.FatalError(e.Message); } }; } else onCompleteFullscreen = () => { }; Media.PlayFMVFullscreen(world, movie, onCompleteFullscreen); }
public void OnAllKilled(Actor[] actors, LuaFunction func) { var group = actors.ToList(); var copy = (LuaFunction)func.CopyReference(); Action<Actor> onMemberKilled = m => { try { group.Remove(m); if (!group.Any()) { copy.Call(); copy.Dispose(); } } catch (Exception e) { Context.FatalError(e.Message); } }; foreach (var a in group) GetScriptTriggers(a).OnKilledInternal += onMemberKilled; }
public int OnEnteredFootprint(CPos[] cells, LuaFunction func) { // We can't easily dispose onEntry, so we'll have to rely on finalization for it. var onEntry = (LuaFunction)func.CopyReference(); var triggerId = 0; 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.AddCellTrigger(cells, invokeEntry, null); return triggerId; }
public void OnProduction(Actor a, LuaFunction func) { GetScriptTriggers(a).RegisterCallback(Trigger.OnProduction, func, Context); }
public void OnPlayerWon(Player player, LuaFunction func) { GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnPlayerWon, func, Context); }
public void OnPlayerDiscovered(Player discovered, LuaFunction func) { GetScriptTriggers(discovered.PlayerActor).RegisterCallback(Trigger.OnPlayerDiscovered, func, Context); }
public void OnPassengerExited(Actor a, LuaFunction func) { GetScriptTriggers(a).RegisterCallback(Trigger.OnPassengerExited, func, Context); }
public void OnObjectiveFailed(Player player, LuaFunction func) { GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, Context); }
public void OnKilledOrCaptured(Actor a, LuaFunction func) { var called = false; var f = (LuaFunction)func.CopyReference(); Action<Actor> onKilledOrCaptured = m => { try { if (called) return; using (f) f.Call().Dispose(); called = true; } catch (Exception e) { Context.FatalError(e.Message); } }; GetScriptTriggers(a).OnCapturedInternal += onKilledOrCaptured; GetScriptTriggers(a).OnKilledInternal += onKilledOrCaptured; }
public int OnExitedProximityTrigger(WPos pos, WDist range, LuaFunction func) { // We can't easily dispose onExit, so we'll have to rely on finalization for it. var onExit = (LuaFunction)func.CopyReference(); var triggerId = 0; 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; }
public void OnDiscovered(Actor a, LuaFunction func) { GetScriptTriggers(a).RegisterCallback(Trigger.OnDiscovered, func, Context); }
public void OnAddedToWorld(Actor a, LuaFunction func) { GetScriptTriggers(a).RegisterCallback(Trigger.OnAddedToWorld, func, Context); }
public CallLuaFunc(LuaFunction function, ScriptContext context) { this.function = (LuaFunction)function.CopyReference(); this.context = context; }
public void OnAnyKilled(Actor[] actors, LuaFunction func) { var called = false; var f = (LuaFunction)func.CopyReference(); Action<Actor> onMemberKilled = m => { try { if (called) return; using (f) using (var killed = m.ToLuaValue(Context)) f.Call(killed).Dispose(); called = true; } catch (Exception e) { Context.FatalError(e.Message); } }; foreach (var a in actors) GetScriptTriggers(a).OnKilledInternal += onMemberKilled; }
public void CallFunc(LuaFunction func) { Self.QueueActivity(new CallLuaFunc(func, Context)); }
public void OnCapture(Actor a, LuaFunction func) { GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, Context); }