Update old PR slightly.

This commit is contained in:
Ottermandias 2025-05-03 23:36:03 +02:00
parent 2c87077918
commit a6073e2a42
2 changed files with 28 additions and 21 deletions

View file

@ -6,23 +6,30 @@ namespace Glamourer.Designs.Special;
public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileSystem, Configuration config) : IService public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileSystem, Configuration config) : IService
{ {
private readonly Random _rng = new(); private readonly Random _rng = new();
private WeakReference<Design?> _lastDesign = new(null, false); private readonly WeakReference<Design> _lastDesign = new(null!, false);
public Design? Design(IReadOnlyList<Design> localDesigns) public Design? Design(IReadOnlyList<Design> localDesigns)
{ {
if (localDesigns.Count == 0) if (localDesigns.Count is 0)
return null; return null;
int idx; var idx = _rng.Next(0, localDesigns.Count);
do if (localDesigns.Count is 1)
idx = _rng.Next(0, localDesigns.Count); {
while (config.PreventRandomRepeats && localDesigns.Count > 1 && _lastDesign.TryGetTarget(out var lastDesign) && lastDesign == localDesigns[idx]);
Glamourer.Log.Verbose($"[Random Design] Chose design {idx + 1} out of {localDesigns.Count}: {localDesigns[idx].Incognito}.");
_lastDesign.SetTarget(localDesigns[idx]); _lastDesign.SetTarget(localDesigns[idx]);
return localDesigns[idx]; return localDesigns[idx];
} }
if (config.PreventRandomRepeats && _lastDesign.TryGetTarget(out var lastDesign))
while (lastDesign == localDesigns[idx])
idx = _rng.Next(0, localDesigns.Count);
var design = localDesigns[idx];
Glamourer.Log.Verbose($"[Random Design] Chose design {idx + 1} out of {localDesigns.Count}: {design.Incognito}.");
_lastDesign.SetTarget(design);
return design;
}
public Design? Design() public Design? Design()
=> Design(designs); => Design(designs);
@ -31,12 +38,12 @@ public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileS
public Design? Design(IReadOnlyList<IDesignPredicate> predicates) public Design? Design(IReadOnlyList<IDesignPredicate> predicates)
{ {
if (predicates.Count == 0) return predicates.Count switch
return Design(); {
if (predicates.Count == 1) 0 => Design(),
return Design(predicates[0]); 1 => Design(predicates[0]),
_ => Design(IDesignPredicate.Get(predicates, designs, fileSystem).ToList()),
return Design(IDesignPredicate.Get(predicates, designs, fileSystem).ToList()); };
} }
public Design? Design(string restrictions) public Design? Design(string restrictions)

View file

@ -102,8 +102,8 @@ public class SettingsTab(
"Apply all settings as temporary settings so they will be reset when Glamourer or the game shut down."u8, "Apply all settings as temporary settings so they will be reset when Glamourer or the game shut down."u8,
config.UseTemporarySettings, config.UseTemporarySettings,
v => config.UseTemporarySettings = v); v => config.UseTemporarySettings = v);
Checkbox("Prevent Random Design Repeats", Checkbox("Prevent Random Design Repeats"u8,
"When using random designs, prevent the same design from being chosen twice in a row.", "When using random designs, prevent the same design from being chosen twice in a row."u8,
config.PreventRandomRepeats, v => config.PreventRandomRepeats = v); config.PreventRandomRepeats, v => config.PreventRandomRepeats = v);
ImGui.NewLine(); ImGui.NewLine();
} }