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

@ -5,22 +5,29 @@ namespace Glamourer.Designs.Special;
public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileSystem, Configuration config) : IService
{
private readonly Random _rng = new();
private WeakReference<Design?> _lastDesign = new(null, false);
private readonly Random _rng = new();
private readonly WeakReference<Design> _lastDesign = new(null!, false);
public Design? Design(IReadOnlyList<Design> localDesigns)
{
if (localDesigns.Count == 0)
if (localDesigns.Count is 0)
return null;
int idx;
do
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]);
return localDesigns[idx];
var idx = _rng.Next(0, localDesigns.Count);
if (localDesigns.Count is 1)
{
_lastDesign.SetTarget(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()
@ -31,12 +38,12 @@ public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileS
public Design? Design(IReadOnlyList<IDesignPredicate> predicates)
{
if (predicates.Count == 0)
return Design();
if (predicates.Count == 1)
return Design(predicates[0]);
return Design(IDesignPredicate.Get(predicates, designs, fileSystem).ToList());
return predicates.Count switch
{
0 => Design(),
1 => Design(predicates[0]),
_ => Design(IDesignPredicate.Get(predicates, designs, fileSystem).ToList()),
};
}
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,
config.UseTemporarySettings,
v => config.UseTemporarySettings = v);
Checkbox("Prevent Random Design Repeats",
"When using random designs, prevent the same design from being chosen twice in a row.",
Checkbox("Prevent Random Design Repeats"u8,
"When using random designs, prevent the same design from being chosen twice in a row."u8,
config.PreventRandomRepeats, v => config.PreventRandomRepeats = v);
ImGui.NewLine();
}
@ -250,8 +250,8 @@ public class SettingsTab(
private void DrawQuickDesignBoxes()
{
var showAuto = config.EnableAutoDesigns;
var numColumns = 8 - (showAuto ? 0 : 2) - (config.UseTemporarySettings ? 0 : 1);
var showAuto = config.EnableAutoDesigns;
var numColumns = 8 - (showAuto ? 0 : 2) - (config.UseTemporarySettings ? 0 : 1);
ImGui.NewLine();
ImUtf8.Text("Show the Following Buttons in the Quick Design Bar:"u8);
ImGui.Dummy(Vector2.Zero);