From 4f1b17b832ef16c3c5ba05f1da1f5dd85f70bf9d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 11 May 2025 19:14:15 +0100 Subject: [PATCH] Allow MapGeneratorSettings to be initialized from MapGenerationArgs. --- .../MapGenerator/MapGeneratorSettings.cs | 42 ++++++++++++++++++- OpenRA.Mods.Common/TraitsInterfaces.cs | 2 + 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.Common/MapGenerator/MapGeneratorSettings.cs b/OpenRA.Mods.Common/MapGenerator/MapGeneratorSettings.cs index 5b9fa0beb2..f0bb425ea9 100644 --- a/OpenRA.Mods.Common/MapGenerator/MapGeneratorSettings.cs +++ b/OpenRA.Mods.Common/MapGenerator/MapGeneratorSettings.cs @@ -208,6 +208,11 @@ namespace OpenRA.Mods.Common.MapGenerator public sealed class MapGeneratorSettings : IMapGeneratorSettings { + sealed class MapGenerationArgsWithOptions : MapGenerationArgs + { + public Dictionary Options = []; + } + readonly IMapGeneratorInfo generatorInfo; public MapGeneratorSettings(IMapGeneratorInfo generatorInfo, MiniYaml yaml) @@ -253,6 +258,26 @@ namespace OpenRA.Mods.Common.MapGenerator } } + public void Initialize(MapGenerationArgs args) + { + if (args is MapGenerationArgsWithOptions optionArgs) + { + foreach (var o in Options) + { + if (!optionArgs.Options.TryGetValue(o.Id, out var value)) + continue; + + switch (o) + { + case MapGeneratorBooleanOption bo: bo.Value = FieldLoader.GetValue(o.Id, value); break; + case MapGeneratorIntegerOption io: io.Value = FieldLoader.GetValue(o.Id, value); break; + case MapGeneratorMultiIntegerChoiceOption mio: mio.Value = FieldLoader.GetValue(o.Id, value); break; + case MapGeneratorMultiChoiceOption mo: mo.Value = value; break; + } + } + } + } + public MapGenerationArgs Compile(ITerrainInfo terrainInfo, Size size) { // Apply the choices in their canonical order. @@ -261,14 +286,27 @@ namespace OpenRA.Mods.Common.MapGenerator .OrderBy(option => option.Priority) .Select(o => o.GetSettings(terrainInfo, playerCount)); - return new MapGenerationArgs() + var options = new Dictionary(); + foreach (var o in Options) + { + switch (o) + { + case MapGeneratorBooleanOption bo: options[o.Id] = FieldSaver.FormatValue(bo.Value); break; + case MapGeneratorIntegerOption io: options[o.Id] = FieldSaver.FormatValue(io.Value); break; + case MapGeneratorMultiIntegerChoiceOption mio: options[o.Id] = FieldSaver.FormatValue(mio.Value); break; + case MapGeneratorMultiChoiceOption mo: options[o.Id] = mo.Value; break; + } + } + + return new MapGenerationArgsWithOptions() { Generator = generatorInfo.Type, Tileset = terrainInfo.Id, Size = size, Title = FluentProvider.GetMessage(generatorInfo.MapTitle), Author = FluentProvider.GetMessage(generatorInfo.Name), - Settings = new MiniYaml(null, MiniYaml.Merge(layers)) + Settings = new MiniYaml(null, MiniYaml.Merge(layers)), + Options = options }; } } diff --git a/OpenRA.Mods.Common/TraitsInterfaces.cs b/OpenRA.Mods.Common/TraitsInterfaces.cs index a81164e3aa..dc498e4e38 100644 --- a/OpenRA.Mods.Common/TraitsInterfaces.cs +++ b/OpenRA.Mods.Common/TraitsInterfaces.cs @@ -984,6 +984,8 @@ namespace OpenRA.Mods.Common.Traits void Randomize(MersenneTwister random); + void Initialize(MapGenerationArgs args); + MapGenerationArgs Compile(ITerrainInfo terrainInfo, Size size); }