diff --git a/lib/mod_list.dart b/lib/mod_list.dart index 9f31e10..ce53e86 100644 --- a/lib/mod_list.dart +++ b/lib/mod_list.dart @@ -324,7 +324,8 @@ class ModList { int total = scoreInfo['total']!; if (total == 0 || bestScore == total) { - return bestOrder; // All constraints satisfied or no constraints + // All constraints satisfied or no constraints, sort by size where possible + return _sortSizeWithinConstraints(bestOrder); } // Use a limited number of improvement passes @@ -397,7 +398,77 @@ class ModList { if (!improved) break; // If no improvements in this pass, stop } - return bestOrder; + // After optimizing for soft constraints, sort by size where possible + return _sortSizeWithinConstraints(bestOrder); + } + + /// Sort mods by size within compatible groups + List _sortSizeWithinConstraints(List order) { + // Find groups of mods that can be reordered without breaking constraints + List> groups = []; + List currentGroup = []; + + for (int i = 0; i < order.length; i++) { + String modId = order[i]; + Mod mod = mods[modId]!; + + if (currentGroup.isEmpty) { + currentGroup.add(modId); + continue; + } + + // Check if this mod can join the current group + bool canJoin = true; + for (String groupModId in currentGroup) { + Mod groupMod = mods[groupModId]!; + + // Check hard dependencies + if (mod.dependencies.contains(groupModId) || + groupMod.dependencies.contains(modId)) { + canJoin = false; + break; + } + + // Check soft constraints + if (mod.loadAfter.contains(groupModId) || + groupMod.loadBefore.contains(modId) || + mod.loadBefore.contains(groupModId) || + groupMod.loadAfter.contains(modId)) { + canJoin = false; + break; + } + } + + if (canJoin) { + currentGroup.add(modId); + } else { + // Start a new group + if (currentGroup.isNotEmpty) { + groups.add(List.from(currentGroup)); + currentGroup = [modId]; + } + } + } + + // Add the last group if not empty + if (currentGroup.isNotEmpty) { + groups.add(currentGroup); + } + + // Sort each group by size + for (List group in groups) { + if (group.length > 1) { + group.sort((a, b) => mods[b]!.size.compareTo(mods[a]!.size)); + } + } + + // Reconstruct the order + List result = []; + for (List group in groups) { + result.addAll(group); + } + + return result; } List loadDependencies(