Skip to content

Tweaks: (enhancement) Add min module count setting to Case Generator #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Tweaks/TweaksAssembly/Tweaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ void LogChildren(Transform goTransform, int depth = 0)
new Dictionary<string, object> { { "Text", "Cases" }, { "Type", "Section" } },
new Dictionary<string, object> { { "Key", "BetterCasePicker" }, { "Text", "Better Case Picker" }, { "Description", "Chooses the smallest case that fits instead of a random one." } },
new Dictionary<string, object> { { "Key", "CaseGenerator" }, { "Text", "Case Generator" }, { "Description", "Generates a case to best fit the bomb which can be one of the colors defined by CaseColors." } },
new Dictionary<string, object> { { "Key", "CaseGeneratorMinModules" }, { "Text", "Case Generator Module Minimum" }, { "Description", "Disables generation of cases that hold less\nthan this many modules." } },
new Dictionary<string, object> { { "Key", "CaseColors" }, { "Text", "Case Colors" }, { "Description", "Controls the color of the cases that are generated with Case Generator." } },

new Dictionary<string, object> { { "Text", "Tweaks" }, { "Type", "Section" } },
Expand Down Expand Up @@ -1012,6 +1013,7 @@ class TweakSettings
public Mode Mode = Mode.Normal;
public int MissionSeed = -1;
public bool CaseGenerator = true;
public int CaseGeneratorMinModules = 1;
public bool ModuleTweaks = true;
public List<string> CaseColors = new List<string>();
public Dictionary<string, object> Holdables = new Dictionary<string, object>();
Expand Down
8 changes: 6 additions & 2 deletions Tweaks/TweaksAssembly/Tweaks/BetterCasePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,18 @@ static void HandleGeneratorSetting(GeneratorSetting generatorSetting, ObjectPool
// Generate a case using Case Generator
if (Tweaks.setupSettings.CaseGenerator)
{
// Make sure a size is found even if the user threshold is enormous (above 2 * componentCount^2 - 1)
int caseSizeSearchLimit = System.Math.Max(componentCount, Tweaks.userSettings.CaseGeneratorMinModules + 1);

List<Vector2> caseSizes = new List<Vector2>();
for (int x = 1; x <= componentCount; x++)
for (int y = 1; y <= componentCount; y++)
for (int x = 1; x <= caseSizeSearchLimit; x++)
for (int y = 1; y <= caseSizeSearchLimit; y++)
Comment on lines +111 to +116
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this change is needed. Why would we need to generate cases that are bigger than the maximum needed for the bomb?

Copy link
Author

@Kodipher Kodipher May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular change prevents a potential edge case issue... sort of:
There is a hidden assumption that the case generator always generates a valid case to be used, but if for whatever reason the user sets the minimum module threshold to be ridiculously and unpractically high (which.. why would they, but they might) then all potential cases sizes would be filtered out, breaking that assumption.

Concrete example: about 340 minimum with only 12 modules in a mission and no other modded cases installed will result in no valid cases, despite case generator being enabled.

But a lot of the time componentCount will be higher than the threshold anyway, so this does not impede performance, just handles the edge case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So.. should this be marked resolved or.. should I revert this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (x >= y)
caseSizes.Add(new Vector2(x, y));

var caseSize = caseSizes
.Where(size => size.y / size.x >= 0.5f && size.x * size.y * (frontFaceOnly ? 1 : 2) >= componentCount)
.Where(size => size.x * size.y * 2 - 1 >= Tweaks.userSettings.CaseGeneratorMinModules)
.OrderBy(size => System.Math.Abs(size.x * size.y * (frontFaceOnly ? 1 : 2) - componentCount))
.ThenByDescending(size => size.y / size.x)
.FirstOrDefault();
Expand Down