Rewrite map generator settings UI logic.

This commit is contained in:
Paul Chote
2025-04-14 09:35:13 +01:00
committed by Gustas Kažukauskas
parent 8f46247dc9
commit 78f660124c
9 changed files with 390 additions and 548 deletions

View File

@@ -15,6 +15,7 @@ using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Text;
using OpenRA.Mods.Common.MapGenerator;
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Common.UtilityCommands
@@ -204,9 +205,9 @@ namespace OpenRA.Mods.Common.UtilityCommands
.Select(variable => config.Choices[variable].Length)
.ToImmutableArray();
var generator =
modData.DefaultRules.Actors[SystemActors.EditorWorld].TraitInfos<IMapGeneratorInfo>()
.FirstOrDefault(info => info.Type == config.MapGeneratorType);
var generator = modData.DefaultRules.Actors[SystemActors.EditorWorld]
.TraitInfos<IMapGeneratorInfo>()
.FirstOrDefault(info => info.Type == config.MapGeneratorType);
if (generator == null)
throw new ArgumentException($"No map generator with type `{config.MapGeneratorType}`");
@@ -246,7 +247,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
.Select(kv => $"{kv.Key}={kv.Value}")
.ToHashSet();
var tileset = modData.DefaultTerrainInfo[iterationChoices[Configuration.TilesetVariable]];
var terrainInfo = modData.DefaultTerrainInfo[iterationChoices[Configuration.TilesetVariable]];
iterationChoices.Remove(Configuration.TilesetVariable);
var size = iterationChoices[Configuration.SizeVariable]
@@ -263,26 +264,31 @@ namespace OpenRA.Mods.Common.UtilityCommands
if (size.Length != 2)
throw new ArgumentException($"bad map size `{iterationChoices[Configuration.SizeVariable]}`");
var map = new Map(modData, tileset, size[0], size[1]);
var map = new Map(modData, terrainInfo, size[0], size[1]);
var maxTerrainHeight = map.Grid.MaximumTerrainHeight;
var tl = new PPos(1, 1 + maxTerrainHeight);
var br = new PPos(size[0], size[1] + maxTerrainHeight);
map.SetBounds(tl, br);
var settings = generator.GetSettings(tileset);
var choices = settings.DefaultChoices();
foreach (var option in choices.Keys)
if (iterationChoices.TryGetValue(option.Id, out var choice))
var settings = generator.GetSettings();
foreach (var o in settings.Options)
{
if (iterationChoices.TryGetValue(o.Id, out var choice))
{
choices[option] = option.IsFreeform()
? choices[option].NewValue(choice)
: option.Choices.First(c => c.Id == choice);
iterationChoices.Remove(option.Id);
if (o is MapGeneratorBooleanOption bo)
bo.Value = FieldLoader.GetValue<bool>("choice", choice);
else if (o is MapGeneratorIntegerOption io)
io.Value = FieldLoader.GetValue<int>("choice", choice);
else if (o is MapGeneratorMultiIntegerChoiceOption mio)
mio.Value = FieldLoader.GetValue<int>("choice", choice);
else if (o is MapGeneratorMultiChoiceOption mo)
mo.Value = choice;
iterationChoices.Remove(o.Id);
}
else if (config.NoDefaults)
{
throw new ArgumentException($"No choices specified for option `{option.Id}`");
}
throw new ArgumentException($"No choices specified for option `{o.Id}`");
}
if (iterationChoices.Count != 0)
throw new ArgumentException($"Unknown options: {string.Join(", ", iterationChoices.Keys)}");
@@ -292,8 +298,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
tests++;
try
{
var my = settings.Compile(choices);
generator.Generate(map, my);
generator.Generate(map, settings.Compile(terrainInfo));
}
catch (Exception e) when (e is MapGenerationException || e is YamlException)
{