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 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 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 OnAllRemovedFromWorld(Actor[] actors, LuaFunction func) { var group = actors.ToList(); var f = (LuaFunction)func.CopyReference(); Action<Actor> onMemberRemoved = m => { try { if (!group.Remove(m)) return; if (!group.Any()) using (f) f.Call().Dispose(); } catch (Exception e) { Context.FatalError(e.Message); } }; foreach (var a in group) GetScriptTriggers(a).OnRemovedInternal += onMemberRemoved; }
public Triggerable(LuaFunction function, ScriptContext context, Actor self) { Function = (LuaFunction)function.CopyReference(); Context = context; Self = self.ToLuaValue(Context); }
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null, LuaFunction actionFunc = null, LuaFunction exitFunc = null) { var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); var cargo = transport.TraitOrDefault<Cargo>(); var passengers = new List<Actor>(); if (cargo != null && cargoTypes != null) { foreach (var cargoType in cargoTypes) { var passenger = CreateActor(owner, cargoType, false, entryPath[0]); passengers.Add(passenger); cargo.Load(transport, passenger); } } for (var i = 1; i < entryPath.Length; i++) Move(transport, entryPath[i]); if (actionFunc != null) { var af = actionFunc.CopyReference() as LuaFunction; transport.QueueActivity(new CallFunc(() => { af.Call(transport.ToLuaValue(Context), passengers.ToArray().ToLuaValue(Context)); af.Dispose(); })); } else { var aircraftInfo = transport.TraitOrDefault<Aircraft>(); if (aircraftInfo != null) { if (!aircraftInfo.IsPlane) { transport.QueueActivity(new Turn(transport, aircraftInfo.Info.InitialFacing)); transport.QueueActivity(new HeliLand(transport, true)); } else { transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, entryPath.Last()))); } transport.QueueActivity(new Wait(15)); } if (cargo != null) { transport.QueueActivity(new UnloadCargo(transport, true)); transport.QueueActivity(new WaitFor(() => cargo.IsEmpty(transport))); } transport.QueueActivity(new Wait(aircraftInfo != null ? 50 : 25)); } if (exitFunc != null) { var ef = exitFunc.CopyReference() as LuaFunction; transport.QueueActivity(new CallFunc(() => { ef.Call(transport.ToLuaValue(Context)); ef.Dispose(); })); } else if (exitPath != null) { foreach (var wpt in exitPath) Move(transport, wpt); transport.QueueActivity(new RemoveSelf()); } var ret = Context.CreateTable(); ret.Add(1, transport.ToLuaValue(Context)); ret.Add(2, passengers.ToArray().ToLuaValue(Context)); return ret; }
public void RegisterCallback(Trigger trigger, LuaFunction func, ScriptContext context) { Triggers[trigger].Add(Pair.New((LuaFunction)func.CopyReference(), context)); }
public void PlayMusic(string track = null, LuaFunction func = null) { if (!Game.Settings.Sound.MapMusic) return; var music = world.Map.Rules.InstalledMusic.Select(a => a.Value).ToArray(); if (!music.Any()) return; var musicInfo = !string.IsNullOrEmpty(track) ? world.Map.Rules.Music[track] : Game.Settings.Sound.Repeat && previousMusic != null ? previousMusic : Game.Settings.Sound.Shuffle ? music.Random(Game.CosmeticRandom) : previousMusic == null ? music.First() : music.SkipWhile(s => s != previousMusic).Skip(1).First(); if (func != null) { var f = func.CopyReference() as LuaFunction; onComplete = () => { try { using (f) f.Call().Dispose(); } catch (LuaException e) { Context.FatalError(e.Message); } }; } else onComplete = () => { }; Sound.PlayMusicThen(musicInfo, onComplete); previousMusic = Sound.CurrentMusic; }
public bool PlayMovieInRadar(string movie, LuaFunction playComplete = null) { if (playComplete != null) { var f = playComplete.CopyReference() as LuaFunction; onCompleteRadar = () => { try { using (f) f.Call().Dispose(); } catch (LuaException e) { Context.FatalError(e.Message); } }; } else onCompleteRadar = () => { }; Stream s = null; try { s = GlobalFileSystem.Open(movie); } catch (FileNotFoundException e) { Log.Write("lua", "Couldn't play movie {0}! File doesn't exist.", e.FileName); onCompleteRadar(); return false; } AsyncLoader l = new AsyncLoader(Media.LoadVqa); IAsyncResult ar = l.BeginInvoke(s, null, null); onLoadComplete = () => { Media.StopFMVInRadar(); world.AddFrameEndTask(_ => Media.PlayFMVInRadar(world, l.EndInvoke(ar), onCompleteRadar)); }; world.AddFrameEndTask(w => w.Add(new AsyncAction(ar, onLoadComplete))); return true; }
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 int OnExitedFootprint(CPos[] cells, 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.AddCellTrigger(cells, null, invokeExit); return triggerId; }
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 OnEnteredProximityTrigger(WPos pos, WDist 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; }
public CallLuaFunc(LuaFunction function, ScriptContext context) { this.function = (LuaFunction)function.CopyReference(); this.context = context; }
public CallLuaFunc(LuaFunction func) { function = (LuaFunction)func.CopyReference(); }