From 1d1b97cb6a815bce71335db71c5543c41b4a9236 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 12 Jun 2016 18:45:40 +0100 Subject: [PATCH] Add ScriptLobbyDropdown trait and lua API. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../Scripting/Global/MapGlobal.cs | 12 ++++ .../Traits/World/ScriptLobbyDropdown.cs | 66 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 OpenRA.Mods.Common/Traits/World/ScriptLobbyDropdown.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 27170c8382..372325c950 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -756,6 +756,7 @@ + diff --git a/OpenRA.Mods.Common/Scripting/Global/MapGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/MapGlobal.cs index 71384d39c3..351035e8b9 100644 --- a/OpenRA.Mods.Common/Scripting/Global/MapGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/MapGlobal.cs @@ -107,6 +107,18 @@ namespace OpenRA.Mods.Common.Scripting [Desc("Returns the difficulty selected by the player before starting the mission.")] public string Difficulty { get { return Context.World.LobbyInfo.GlobalSettings.Difficulty; } } + [Desc("Returns the value of a `ScriptLobbyDropdown` selected in the game lobby.")] + public LuaValue LobbyOption(string id) + { + var option = Context.World.WorldActor.TraitsImplementing() + .FirstOrDefault(sld => sld.Info.ID == id); + + if (option == null) + throw new YamlException("A ScriptLobbyDropdown with ID `" + id + "` was not found."); + + return option.Value; + } + [Desc("Returns a table of all the actors that were specified in the map file.")] public Actor[] NamedActors { get { return sma.Actors.Values.ToArray(); } } diff --git a/OpenRA.Mods.Common/Traits/World/ScriptLobbyDropdown.cs b/OpenRA.Mods.Common/Traits/World/ScriptLobbyDropdown.cs new file mode 100644 index 0000000000..2a15cadf9d --- /dev/null +++ b/OpenRA.Mods.Common/Traits/World/ScriptLobbyDropdown.cs @@ -0,0 +1,66 @@ +#region Copyright & License Information +/* + * Copyright 2007-2016 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, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; +using System.Linq; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Controls the map difficulty, tech level, and short game lobby options.")] + public class ScriptLobbyDropdownInfo : ITraitInfo, ILobbyOptions + { + [FieldLoader.Require] + [Desc("Internal id for this option.")] + public readonly string ID = null; + + [FieldLoader.Require] + [Desc("Display name for this option.")] + public readonly string Label = null; + + [FieldLoader.Require] + [Desc("Default option key in the `Values` list.")] + public readonly string Default = null; + + [FieldLoader.Require] + [Desc("Difficulty levels supported by the map.")] + public readonly Dictionary Values = null; + + [Desc("Prevent the option from being changed from its default value.")] + public readonly bool Locked = false; + + IEnumerable ILobbyOptions.LobbyOptions(Ruleset rules) + { + yield return new LobbyOption(ID, Label, + new ReadOnlyDictionary(Values), + Default, Locked); + } + + public object Create(ActorInitializer init) { return new ScriptLobbyDropdown(this); } + } + + public class ScriptLobbyDropdown : INotifyCreated + { + public readonly ScriptLobbyDropdownInfo Info; + + public string Value { get; private set; } + + public ScriptLobbyDropdown(ScriptLobbyDropdownInfo info) + { + Info = info; + } + + void INotifyCreated.Created(Actor self) + { + Value = self.World.LobbyInfo.GlobalSettings.OptionOrDefault(Info.ID, Info.Default); + } + } +}