Example #1
0
		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);
		}
Example #2
0
        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;
        }
Example #3
0
		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();
		}
Example #4
0
        public void Dispose()
        {
            if (function == null)
                return;

            function.Dispose();
            function = null;
        }
Example #5
0
		public Player[] GetPlayers(LuaFunction filter)
		{
			return Context.World.Players
				.Where(p =>
				{
					using (var f = filter.Call(p.ToLuaValue(Context)))
						return f.First().ToBoolean();
				}).ToArray();
		}
Example #6
0
        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;
        }
Example #7
0
		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);
		}
Example #8
0
        protected void Dispose(bool disposing)
        {
            if (function == null)
                return;

            if (disposing)
            {
                function.Dispose();
                function = null;
            }
        }
Example #9
0
		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);
		}
Example #10
0
        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;
        }
Example #11
0
		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;
		}
Example #12
0
		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();
		}
Example #13
0
		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();
		}
Example #14
0
        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)));
        }
Example #15
0
        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);
        }
Example #16
0
		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;
		}
Example #17
0
        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;
        }
Example #18
0
 public void OnProduction(Actor a, LuaFunction func)
 {
     GetScriptTriggers(a).RegisterCallback(Trigger.OnProduction, func, Context);
 }
Example #19
0
 public void OnPlayerWon(Player player, LuaFunction func)
 {
     GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnPlayerWon, func, Context);
 }
Example #20
0
 public void OnPlayerDiscovered(Player discovered, LuaFunction func)
 {
     GetScriptTriggers(discovered.PlayerActor).RegisterCallback(Trigger.OnPlayerDiscovered, func, Context);
 }
Example #21
0
 public void OnPassengerExited(Actor a, LuaFunction func)
 {
     GetScriptTriggers(a).RegisterCallback(Trigger.OnPassengerExited, func, Context);
 }
Example #22
0
 public void OnObjectiveFailed(Player player, LuaFunction func)
 {
     GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, Context);
 }
Example #23
0
        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;
        }
Example #24
0
        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;
        }
Example #25
0
 public void OnDiscovered(Actor a, LuaFunction func)
 {
     GetScriptTriggers(a).RegisterCallback(Trigger.OnDiscovered, func, Context);
 }
Example #26
0
 public void OnAddedToWorld(Actor a, LuaFunction func)
 {
     GetScriptTriggers(a).RegisterCallback(Trigger.OnAddedToWorld, func, Context);
 }
Example #27
0
 public CallLuaFunc(LuaFunction function, ScriptContext context)
 {
     this.function = (LuaFunction)function.CopyReference();
     this.context = context;
 }
Example #28
0
        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;
        }
Example #29
0
 public void CallFunc(LuaFunction func)
 {
     Self.QueueActivity(new CallLuaFunc(func, Context));
 }
Example #30
0
 public void OnCapture(Actor a, LuaFunction func)
 {
     GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, Context);
 }