Merge pull request #11364 from pchote/lobby-trait-options

Unhardcode the lobby options backend.
This commit is contained in:
reaperrr
2016-06-11 13:40:09 +02:00
committed by GitHub
22 changed files with 423 additions and 546 deletions

View File

@@ -173,6 +173,17 @@ namespace OpenRA.Network
}
}
public class LobbyOptionState
{
public bool Locked;
public string Value;
public string PreferredValue;
public LobbyOptionState() { }
public bool Enabled { get { return Value == "True"; } }
}
public class Global
{
public string ServerName;
@@ -180,32 +191,52 @@ namespace OpenRA.Network
public int Timestep = 40;
public int OrderLatency = 3; // net tick frames (x 120 = ms)
public int RandomSeed = 0;
public bool AllowCheats = false;
public bool AllowSpectators = true;
public bool Dedicated;
public string Difficulty;
public bool Crates = true;
public bool Creeps = true;
public bool Shroud = true;
public bool Fog = true;
public bool AllyBuildRadius = true;
public int StartingCash = 5000;
public string TechLevel;
public string StartingUnitsClass;
public string GameSpeedType = "default";
public bool ShortGame = true;
public bool AllowVersionMismatch;
public string GameUid;
public bool DisableSingleplayer;
[FieldLoader.Ignore]
public Dictionary<string, LobbyOptionState> LobbyOptions = new Dictionary<string, LobbyOptionState>();
public static Global Deserialize(MiniYaml data)
{
return FieldLoader.Load<Global>(data);
var gs = FieldLoader.Load<Global>(data);
var optionsNode = data.Nodes.FirstOrDefault(n => n.Key == "Options");
if (optionsNode != null)
foreach (var n in optionsNode.Value.Nodes)
gs.LobbyOptions[n.Key] = FieldLoader.Load<LobbyOptionState>(n.Value);
return gs;
}
public MiniYamlNode Serialize()
{
return new MiniYamlNode("GlobalSettings", FieldSaver.Save(this));
var data = new MiniYamlNode("GlobalSettings", FieldSaver.Save(this));
var options = LobbyOptions.Select(kv => new MiniYamlNode(kv.Key, FieldSaver.Save(kv.Value))).ToList();
data.Value.Nodes.Add(new MiniYamlNode("Options", new MiniYaml(null, options)));
return data;
}
public bool OptionOrDefault(string id, bool def)
{
LobbyOptionState option;
if (LobbyOptions.TryGetValue(id, out option))
return option.Enabled;
return def;
}
public string OptionOrDefault(string id, string def)
{
LobbyOptionState option;
if (LobbyOptions.TryGetValue(id, out option))
return option.Value;
return def;
}
}

View File

@@ -149,7 +149,6 @@ namespace OpenRA.Server
RandomSeed = randomSeed,
Map = settings.Map,
ServerName = settings.Name,
Dedicated = dedicated,
DisableSingleplayer = settings.DisableSinglePlayer,
}
};

View File

@@ -9,10 +9,12 @@
*/
#endregion
using System.Collections.Generic;
namespace OpenRA.Traits
{
[Desc("Attach this to the player actor.")]
public class DeveloperModeInfo : ITraitInfo
public class DeveloperModeInfo : ITraitInfo, ILobbyOptions
{
[Desc("Default value of the developer mode checkbox in the lobby.")]
public bool Enabled = false;
@@ -56,6 +58,11 @@ namespace OpenRA.Traits
[Desc("Enable the actor tags overlay by default.")]
public bool ShowActorTags;
IEnumerable<LobbyOption> ILobbyOptions.LobbyOptions(Ruleset rules)
{
yield return new LobbyBooleanOption("cheats", "Debug Menu", Enabled, Locked);
}
public object Create(ActorInitializer init) { return new DeveloperMode(this); }
}
@@ -106,7 +113,8 @@ namespace OpenRA.Traits
void INotifyCreated.Created(Actor self)
{
Enabled = self.World.LobbyInfo.GlobalSettings.AllowCheats || self.World.LobbyInfo.IsSinglePlayer;
Enabled = self.World.LobbyInfo.IsSinglePlayer || self.World.LobbyInfo.GlobalSettings
.OptionOrDefault("cheats", info.Enabled);
}
public void ResolveOrder(Actor self, Order order)

View File

@@ -10,11 +10,12 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Traits
{
public class PlayerResourcesInfo : ITraitInfo
public class PlayerResourcesInfo : ITraitInfo, ILobbyOptions
{
[Desc("Starting cash options that are available in the lobby options.")]
public readonly int[] SelectableCash = { 2500, 5000, 10000, 20000 };
@@ -31,6 +32,14 @@ namespace OpenRA.Traits
[Desc("Delay (in ticks) during which warnings will be muted.")]
public readonly int InsufficientFundsNotificationDelay = 750;
IEnumerable<LobbyOption> ILobbyOptions.LobbyOptions(Ruleset rules)
{
var startingCash = SelectableCash.ToDictionary(c => c.ToString(), c => "$" + c.ToString());
if (startingCash.Any())
yield return new LobbyOption("startingcash", "Starting Cash", new ReadOnlyDictionary<string, string>(startingCash), DefaultCash.ToString(), DefaultCashLocked);
}
public object Create(ActorInitializer init) { return new PlayerResources(init.Self, this); }
}
@@ -46,7 +55,11 @@ namespace OpenRA.Traits
this.info = info;
owner = self.Owner;
Cash = self.World.LobbyInfo.GlobalSettings.StartingCash;
var startingCash = self.World.LobbyInfo.GlobalSettings
.OptionOrDefault("startingcash", info.DefaultCash.ToString());
if (!int.TryParse(startingCash, out Cash))
Cash = info.DefaultCash;
}
[Sync] public int Cash;

View File

@@ -412,4 +412,50 @@ namespace OpenRA.Traits
public interface IRulesetLoaded<TInfo> { void RulesetLoaded(Ruleset rules, TInfo info); }
public interface IRulesetLoaded : IRulesetLoaded<ActorInfo>, ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface ILobbyOptions : ITraitInfoInterface
{
IEnumerable<LobbyOption> LobbyOptions(Ruleset rules);
}
public class LobbyOption
{
public readonly string Id;
public readonly string Name;
public readonly IReadOnlyDictionary<string, string> Values;
public readonly string DefaultValue;
public readonly bool Locked;
public LobbyOption(string id, string name, IReadOnlyDictionary<string, string> values, string defaultValue, bool locked)
{
Id = id;
Name = name;
Values = values;
DefaultValue = defaultValue;
Locked = locked;
}
public virtual string ValueChangedMessage(string playerName, string newValue)
{
return playerName + " changed " + Name + " to " + Values[newValue] + ".";
}
}
public class LobbyBooleanOption : LobbyOption
{
static readonly Dictionary<string, string> BoolValues = new Dictionary<string, string>()
{
{ true.ToString(), "enabled" },
{ false.ToString(), "disabled" }
};
public LobbyBooleanOption(string id, string name, bool defaultValue, bool locked)
: base(id, name, new ReadOnlyDictionary<string, string>(BoolValues), defaultValue.ToString(), locked) { }
public override string ValueChangedMessage(string playerName, string newValue)
{
return playerName + " " + BoolValues[newValue] + " " + Name + ".";
}
}
}

View File

@@ -12,11 +12,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Network;
namespace OpenRA.Traits
{
[Desc("Required for shroud and fog visibility checks. Add this to the player actor.")]
public class ShroudInfo : ITraitInfo
public class ShroudInfo : ITraitInfo, ILobbyOptions
{
[Desc("Default value of the fog checkbox in the lobby.")]
public bool FogEnabled = true;
@@ -30,7 +31,13 @@ namespace OpenRA.Traits
[Desc("Prevent the explore map enabled state from being changed in the lobby.")]
public bool ExploredMapLocked = false;
public object Create(ActorInitializer init) { return new Shroud(init.Self); }
IEnumerable<LobbyOption> ILobbyOptions.LobbyOptions(Ruleset rules)
{
yield return new LobbyBooleanOption("explored", "Explored Map", ExploredMapEnabled, ExploredMapLocked);
yield return new LobbyBooleanOption("fog", "Fog of War", FogEnabled, FogLocked);
}
public object Create(ActorInitializer init) { return new Shroud(init.Self, this); }
}
public class Shroud : ISync, INotifyCreated
@@ -38,6 +45,7 @@ namespace OpenRA.Traits
public event Action<IEnumerable<PPos>> CellsChanged;
readonly Actor self;
readonly ShroudInfo info;
readonly Map map;
readonly CellLayer<short> visibleCount;
@@ -72,9 +80,10 @@ namespace OpenRA.Traits
public int Hash { get; private set; }
public Shroud(Actor self)
public Shroud(Actor self, ShroudInfo info)
{
this.self = self;
this.info = info;
map = self.World.Map;
visibleCount = new CellLayer<short>(map);
@@ -84,9 +93,11 @@ namespace OpenRA.Traits
void INotifyCreated.Created(Actor self)
{
fogEnabled = self.World.LobbyInfo.GlobalSettings.Fog;
var shroudEnabled = self.World.LobbyInfo.GlobalSettings.Shroud;
if (!shroudEnabled)
var gs = self.World.LobbyInfo.GlobalSettings;
fogEnabled = gs.OptionOrDefault("fog", info.FogEnabled);
var exploreMap = gs.OptionOrDefault("explored", info.ExploredMapEnabled);
if (exploreMap)
self.World.AddFrameEndTask(w => ExploreAll());
}