diff --git a/Assets/Scripts/ServerShared/ColorMath.cs b/Assets/Scripts/ServerShared/ColorMath.cs index b94764bd3..26ea704a7 100644 --- a/Assets/Scripts/ServerShared/ColorMath.cs +++ b/Assets/Scripts/ServerShared/ColorMath.cs @@ -18,6 +18,12 @@ public static class ColorMath return saturate(float3(r,g,b)); } + // A hue band on the circle runs from start up to end, wrapping past 1 when end < start. + public static bool HueInBand(float hue, float start, float end) => frac(hue - start) < frac(end - start); + + // Maps u in [0,1) onto the hues outside the band, so a uniform draw stays uniform over the allowed arc. + public static float HueOutsideBand(float u, float start, float end) => frac(end + u * (1 - frac(end - start))); + public static float3 HsvToRgb(in float3 hsv) { float3 rgb = HueToRgb(hsv.x); diff --git a/Assets/Scripts/ServerShared/Settings.cs b/Assets/Scripts/ServerShared/Settings.cs index cda5e1623..af22d2840 100644 --- a/Assets/Scripts/ServerShared/Settings.cs +++ b/Assets/Scripts/ServerShared/Settings.cs @@ -142,6 +142,9 @@ public class ZoneGenerationSettings public float SunSecondaryColorDistance = .25f; public float SunLightSaturation = .5f; public float SunFogTintSaturation = .5f; + // The green band no sun colour may take, as fractions of the hue circle (75 to 165 degrees: chartreuse through spring green). + public float SunHueExcludedStart = 75f / 360f; + public float SunHueExcludedEnd = 165f / 360f; public ExponentialLerp GasGiantBandCount; public float GasGiantBandColorSeparation = .25f; diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 202e5e4e9..12a8810e9 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -159,8 +159,11 @@ public static class ZoneGenerator { if (gas is SunData sun) { - float primary = random.NextFloat(); - float secondary = frac(primary + 1 + zoneSettings.SunSecondaryColorDistance * (random.NextFloat() > .5 ? 1 : -1)); + float primary = ColorMath.HueOutsideBand(random.NextFloat(), zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd); + float step = zoneSettings.SunSecondaryColorDistance * (random.NextFloat() > .5 ? 1 : -1); + float secondary = frac(primary + 1 + step); + if (ColorMath.HueInBand(secondary, zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd)) + secondary = frac(primary + 1 - step); gas.Colors = new [] { float4(ColorMath.HsvToRgb(float3(primary, zoneSettings.SunColorSaturation, .5f)), 0), diff --git a/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs b/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs new file mode 100644 index 000000000..e94deab92 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; + +// Operator rulings suns-never-green and suns-never-green-edges: no generated sun colour (the body's two colour stops, its +// fog tint and its light colour) takes a hue where green dominates. The band is the Settings default, 75 to 165 degrees; +// yellow (60) and teal (170) stay allowed. Hues are recovered from the RGB the production generator wrote. +public sealed class SunsNeverGreenTests : IDisposable +{ + private const float Degree = 1f / 360f; + // Hue recovered from a float RGB wobbles by a few millionths of a turn; the band is judged with this much margin. + private const float Slack = 1e-3f; + + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-sunsgreen-" + Guid.NewGuid().ToString("N")); + private int _runs; + private readonly List _caches = new List(); + + public SunsNeverGreenTests() => Directory.CreateDirectory(_root); + + public void Dispose() + { + foreach (var cache in _caches) cache.Dispose(); + Directory.Delete(_root, true); + } + + [Theory] + [InlineData(76f)] + [InlineData(80f)] + [InlineData(120f)] + [InlineData(160f)] + [InlineData(164f)] + public void HuesInsideTheDefaultBandAreRefused(float degrees) + { + var settings = new ZoneGenerationSettings(); + Assert.True(ColorMath.HueInBand(degrees * Degree, settings.SunHueExcludedStart, settings.SunHueExcludedEnd)); + } + + [Theory] + [InlineData(0f)] + [InlineData(60f)] + [InlineData(74f)] + [InlineData(166f)] + [InlineData(170f)] + [InlineData(180f)] + [InlineData(300f)] + public void YellowTealAndTheRestOfTheCircleAreAllowed(float degrees) + { + var settings = new ZoneGenerationSettings(); + Assert.False(ColorMath.HueInBand(degrees * Degree, settings.SunHueExcludedStart, settings.SunHueExcludedEnd)); + } + + [Fact] + public void TheDrawMapsOntoTheWholeAllowedArcAndNothingElse() + { + var settings = new ZoneGenerationSettings(); + var start = settings.SunHueExcludedStart; + var end = settings.SunHueExcludedEnd; + var low = float.MaxValue; + var high = float.MinValue; + for (var i = 0; i < 1000; i++) + { + var hue = ColorMath.HueOutsideBand(i / 1000f, start, end); + Assert.False(ColorMath.HueInBand(hue, start, end), $"draw {i}/1000 landed in the band"); + // Unwrapped from the band's far edge, the allowed arc runs 165 up to 75 + 360. + var along = ColorMath.HueInBand(hue, 0f, end) ? hue + 1f : hue; + low = Math.Min(low, along); + high = Math.Max(high, along); + } + Assert.InRange(low, end, end + 1f * Degree); + Assert.InRange(high, 1f + start - 1f * Degree, 1f + start); + } + + [Fact] + public void GeneratedSunsKeepEveryColourOutOfTheDefaultBand() + { + var settings = new ZoneGenerationSettings(); + var suns = GenerateSuns(z => { }, out _); + Assert.True(suns.Count >= 10, $"only {suns.Count} suns generated"); + foreach (var sun in suns) + foreach (var hue in Hues(sun)) + Assert.False(ColorMath.HueInBand(hue, settings.SunHueExcludedStart + Slack, settings.SunHueExcludedEnd - Slack), + $"a sun colour fell in the green band (hue {hue / Degree:F1} degrees)"); + } + + [Fact] + public void TheBandComesFromSettingsNotFromTheGenerator() + { + // A blue band, 200 to 300 degrees: suns avoid it and now take green freely. + var suns = GenerateSuns(z => + { + z.SunHueExcludedStart = 200f * Degree; + z.SunHueExcludedEnd = 300f * Degree; + }, out _); + Assert.True(suns.Count >= 10, $"only {suns.Count} suns generated"); + var hues = suns.SelectMany(Hues).ToList(); + Assert.All(hues, hue => Assert.False(ColorMath.HueInBand(hue, 200f * Degree + Slack, 300f * Degree - Slack))); + Assert.Contains(hues, hue => ColorMath.HueInBand(hue, 75f * Degree, 165f * Degree)); + } + + private static IEnumerable Hues(SunData sun) => + sun.Colors.Select(c => c.xyz) + .Concat(new[] { sun.FogTintColor, sun.LightColor }) + .Select(rgb => ColorMath.RgbToHsv(rgb).x); + + // Production path: Galaxy, ItemManager and ZoneGenerator.GenerateZone over the shipped ZoneSettings, as in OrbitsFromFieldTests. + private List GenerateSuns(Action adjust, out int zones) + { + var suns = new List(); + zones = 0; + _runs++; + for (uint seed = 1; seed <= 8; seed++) + { + var dir = Path.Combine(_root, $"galaxy{_runs}-{seed}"); + Directory.CreateDirectory(dir); + var catalog = Path.Combine(dir, "Aetheria.cc"); + File.Copy(TestCatalog.Repo, catalog); + var cache = new CultCache(TestCatalog.Registry()); + _caches.Add(cache); + cache.AddBackingStore(new GameCult.Caching.MessagePack.SingleFileMessagePackBackingStore(catalog), AetheriaStores.CatalogTypes); + cache.AddBackingStore(new GameCult.Caching.MessagePack.SingleFileMessagePackBackingStore(Path.Combine(dir, "run.cc")), AetheriaStores.RunTypes); + var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot()); + var tutorial = authored.Read("TutorialGenerationSettings"); + var galaxy = new Galaxy(tutorial, authored.Read("TutorialBackgroundSettings"), + authored.Read("NameGeneratorSettings"), cache, new PlayerSettings(), + Directory.CreateDirectory(Path.Combine(dir, "Narrative")), _ => { }, null, seed); + var items = new ItemManager(cache, new ProvenanceLedger(), authored.Read("GameplaySettings"), _ => { }); + var zoneSettings = authored.Read("ZoneSettings"); + adjust(zoneSettings); + foreach (var zone in galaxy.Zones) + { + zones++; + var pack = ZoneGenerator.GenerateZone(items, zoneSettings, galaxy, zone, false, false); + suns.AddRange(pack.Planets.Select(p => cache.Get(p)).OfType()); + } + } + return suns; + } +}