Port map difficulty to new options backend.

This commit is contained in:
Paul Chote
2016-06-12 20:31:55 +01:00
parent 1d1b97cb6a
commit 8ce4ab0bd1
31 changed files with 263 additions and 111 deletions

View File

@@ -105,7 +105,18 @@ namespace OpenRA.Mods.Common.Scripting
public bool IsSinglePlayer { get { return Context.World.LobbyInfo.IsSinglePlayer; } }
[Desc("Returns the difficulty selected by the player before starting the mission.")]
public string Difficulty { get { return Context.World.LobbyInfo.GlobalSettings.Difficulty; } }
public string Difficulty
{
get
{
Game.Debug("Map script is using deprecated Map.Difficulty API. This should be changed to Map.LobbyOption(\"difficulty\").");
Log.Write("lua", "Map script is using deprecated Map.Difficulty API. This should be changed to Map.LobbyOption(\"difficulty\").");
var option = Context.World.WorldActor.TraitsImplementing<ScriptLobbyDropdown>()
.FirstOrDefault(sld => sld.Info.ID == "difficulty");
return option != null ? option.Info.Values[option.Value] : null;
}
}
[Desc("Returns the value of a `ScriptLobbyDropdown` selected in the game lobby.")]
public LuaValue LobbyOption(string id)

View File

@@ -506,39 +506,6 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "difficulty",
s =>
{
if (server.LobbyInfo.GlobalSettings.Difficulty == s)
return true;
if (!client.IsAdmin)
{
server.SendOrderTo(conn, "Message", "Only the host can set that option.");
return true;
}
var mapOptions = server.Map.Rules.Actors["world"].TraitInfo<MapOptionsInfo>();
if (mapOptions.DifficultyLocked || !mapOptions.Difficulties.Any())
{
server.SendOrderTo(conn, "Message", "Map has disabled difficulty configuration.");
return true;
}
if (s != null && !mapOptions.Difficulties.Contains(s))
{
server.SendOrderTo(conn, "Message", "Invalid difficulty selected: {0}".F(s));
server.SendOrderTo(conn, "Message", "Supported values: {0}".F(mapOptions.Difficulties.JoinWith(", ")));
return true;
}
server.LobbyInfo.GlobalSettings.Difficulty = s;
server.SyncLobbyGlobalSettings();
server.SendMessage("{0} changed difficulty to {1}.".F(client.Name, s));
return true;
}
},
{ "gamespeed",
s =>
{

View File

@@ -30,15 +30,6 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Prevent the tech level from being changed in the lobby.")]
public readonly bool TechLevelLocked = false;
[Desc("Difficulty levels supported by the map.")]
public readonly string[] Difficulties = { };
[Desc("Default difficulty level.")]
public readonly string Difficulty = null;
[Desc("Prevent the difficulty from being changed in the lobby.")]
public readonly bool DifficultyLocked = false;
IEnumerable<LobbyOption> ILobbyOptions.LobbyOptions(Ruleset rules)
{
yield return new LobbyBooleanOption("shortgame", "Short Game", ShortGameEnabled, ShortGameLocked);

View File

@@ -94,6 +94,8 @@ namespace OpenRA.Mods.Common.UtilityCommands
internal static void UpgradeActorRules(int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth)
{
var addNodes = new List<MiniYamlNode>();
foreach (var node in nodes)
{
if (engineVersion < 20160515)
@@ -146,8 +148,48 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
// Map difficulty configuration was split to a generic trait
if (engineVersion < 20160614 && node.Key.StartsWith("MapOptions"))
{
var difficultiesNode = node.Value.Nodes.FirstOrDefault(n => n.Key == "Difficulties");
if (difficultiesNode != null)
{
var difficulties = FieldLoader.GetValue<string[]>("Difficulties", difficultiesNode.Value.Value)
.ToDictionary(d => d.Replace(" ", "").ToLowerInvariant(), d => d);
node.Value.Nodes.Remove(difficultiesNode);
var childNodes = new List<MiniYamlNode>()
{
new MiniYamlNode("ID", "difficulty"),
new MiniYamlNode("Label", "Difficulty"),
new MiniYamlNode("Values", new MiniYaml("", difficulties.Select(kv => new MiniYamlNode(kv.Key, kv.Value)).ToList()))
};
var difficultyNode = node.Value.Nodes.FirstOrDefault(n => n.Key == "Difficulty");
if (difficultyNode != null)
{
childNodes.Add(new MiniYamlNode("Default", difficultyNode.Value.Value.Replace(" ", "").ToLowerInvariant()));
node.Value.Nodes.Remove(difficultyNode);
}
else
childNodes.Add(new MiniYamlNode("Default", difficulties.Keys.First()));
var lockedNode = node.Value.Nodes.FirstOrDefault(n => n.Key == "DifficultyLocked");
if (lockedNode != null)
{
childNodes.Add(new MiniYamlNode("Locked", lockedNode.Value.Value));
node.Value.Nodes.Remove(lockedNode);
}
addNodes.Add(new MiniYamlNode("ScriptLobbyDropdown@difficulty", new MiniYaml("", childNodes)));
}
}
UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1);
}
foreach (var a in addNodes)
nodes.Add(a);
}
internal static void UpgradeWeaponRules(int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth)

View File

@@ -368,6 +368,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ "TECHLEVEL", "techlevel" },
{ "STARTINGUNITS", "startingunits" },
{ "STARTINGCASH", "startingcash" },
{ "DIFFICULTY", "difficulty" }
};
var allOptions = new CachedTransform<MapPreview, LobbyOption[]>(
@@ -423,35 +424,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
}
var difficulty = optionsBin.GetOrNull<DropDownButtonWidget>("DIFFICULTY_DROPDOWNBUTTON");
if (difficulty != null)
{
var mapOptions = new CachedTransform<MapPreview, MapOptionsInfo>(
map => map.Rules.Actors["world"].TraitInfo<MapOptionsInfo>());
difficulty.IsVisible = () => Map.RulesLoaded && mapOptions.Update(Map).Difficulties.Any();
difficulty.IsDisabled = () => configurationDisabled() || mapOptions.Update(Map).DifficultyLocked;
difficulty.GetText = () => orderManager.LobbyInfo.GlobalSettings.Difficulty;
difficulty.OnMouseDown = _ =>
{
var options = mapOptions.Update(Map).Difficulties.Select(d => new DropDownOption
{
Title = d,
IsSelected = () => orderManager.LobbyInfo.GlobalSettings.Difficulty == d,
OnClick = () => orderManager.IssueOrder(Order.Command("difficulty {0}".F(d)))
});
Func<DropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{
var item = ScrollItemWidget.Setup(template, option.IsSelected, option.OnClick);
item.Get<LabelWidget>("LABEL").GetText = () => option.Title;
return item;
};
difficulty.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", options.Count() * 30, options, setupItem);
};
optionsBin.Get<LabelWidget>("DIFFICULTY_DESC").IsVisible = difficulty.IsVisible;
}
var gameSpeed = optionsBin.GetOrNull<DropDownButtonWidget>("GAMESPEED_DROPDOWNBUTTON");
if (gameSpeed != null)
{

View File

@@ -196,7 +196,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Cache the rules on a background thread to avoid jank
var difficultyDisabled = true;
var difficulties = new string[0];
var difficulties = new Dictionary<string, string>();
var briefingVideo = "";
var briefingVideoVisible = false;
@@ -206,11 +206,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
new Thread(() =>
{
var mapOptions = preview.Rules.Actors["world"].TraitInfo<MapOptionsInfo>();
var mapDifficulty = preview.Rules.Actors["world"].TraitInfos<ScriptLobbyDropdownInfo>()
.FirstOrDefault(sld => sld.ID == "difficulty");
difficulty = mapOptions.Difficulty ?? mapOptions.Difficulties.FirstOrDefault();
difficulties = mapOptions.Difficulties;
difficultyDisabled = mapOptions.DifficultyLocked || mapOptions.Difficulties.Length <= 1;
if (mapDifficulty != null)
{
difficulty = mapDifficulty.Default;
difficulties = mapDifficulty.Values;
difficultyDisabled = mapDifficulty.Locked;
}
var missionData = preview.Rules.Actors["world"].TraitInfoOrDefault<MissionDataInfo>();
if (missionData != null)
@@ -245,15 +249,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (difficultyButton != null)
{
var difficultyName = new CachedTransform<string, string>(id => id == null || !difficulties.ContainsKey(id) ? "Normal" : difficulties[id]);
difficultyButton.IsDisabled = () => difficultyDisabled;
difficultyButton.GetText = () => difficulty ?? "Normal";
difficultyButton.GetText = () => difficultyName.Update(difficulty);
difficultyButton.OnMouseDown = _ =>
{
var options = difficulties.Select(d => new DropDownOption
var options = difficulties.Select(kv => new DropDownOption
{
Title = d,
IsSelected = () => difficulty == d,
OnClick = () => difficulty = d
Title = kv.Value,
IsSelected = () => difficulty == kv.Key,
OnClick = () => difficulty = kv.Key
});
Func<DropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
@@ -361,7 +366,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var orders = new[] {
Order.Command("gamespeed {0}".F(gameSpeed)),
Order.Command("difficulty {0}".F(difficulty)),
Order.Command("option difficulty {0}".F(difficulty)),
Order.Command("state {0}".F(Session.ClientState.Ready))
};