diff --git a/Yafc.Model/Data/DataUtils.cs b/Yafc.Model/Data/DataUtils.cs index 8ff39883..3eef19c8 100644 --- a/Yafc.Model/Data/DataUtils.cs +++ b/Yafc.Model/Data/DataUtils.cs @@ -109,48 +109,55 @@ public static Bits GetMilestoneOrder(FactorioId id) { /// /// The only normal item in . /// The only normal user favorite in . + /// The only item in , considering both normal and special items. + /// The only user favorite in , considering both normal and special items. /// If no previous options are applicable, . /// public static T? SelectSingle(this T[] list, out string recipeHint) where T : FactorioObject { - var userFavorites = Project.current.preferences.favorites; - bool acceptOnlyFavorites = false; - T? element = null; - if (list.Any(t => t.IsAccessible())) { - recipeHint = "Hint: Complete milestones to enable ctrl+click"; - } - else { - recipeHint = "Hint: Mark a recipe as accessible to enable ctrl+click"; - } - foreach (var elem in list) { - if (!elem.IsAccessibleWithCurrentMilestones() || elem.specialType != FactorioObjectSpecialType.Normal) { - continue; + return @internal(list, true, out recipeHint) ?? @internal(list, false, out recipeHint); + + static T? @internal(T[] list, bool excludeSpecial, out string recipeHint) { + HashSet userFavorites = Project.current.preferences.favorites; + bool acceptOnlyFavorites = false; + T? element = null; + if (list.Any(t => t.IsAccessible())) { + recipeHint = "Hint: Complete milestones to enable ctrl+click"; } - - if (userFavorites.Contains(elem)) { - if (!acceptOnlyFavorites || element == null) { - element = elem; - recipeHint = "Hint: ctrl+click to add your favorited recipe"; - acceptOnlyFavorites = true; - } - else { - recipeHint = "Hint: Cannot ctrl+click with multiple favorited recipes"; - return null; - } + else { + recipeHint = "Hint: Mark a recipe as accessible to enable ctrl+click"; } - else if (!acceptOnlyFavorites) { - if (element == null) { - element = elem; - recipeHint = "Hint: ctrl+click to add the accessible recipe"; + foreach (T elem in list) { + // Always consider normal entries. A list with two normals and one special should select nothing, rather than selecting the only special item. + if (!elem.IsAccessibleWithCurrentMilestones() || (elem.specialType != FactorioObjectSpecialType.Normal && excludeSpecial)) { + continue; } - else { - element = null; - recipeHint = "Hint: Set a favorite recipe to add it with ctrl+click"; - acceptOnlyFavorites = true; + + if (userFavorites.Contains(elem)) { + if (!acceptOnlyFavorites || element == null) { + element = elem; + recipeHint = "Hint: ctrl+click to add your favorited recipe"; + acceptOnlyFavorites = true; + } + else { + recipeHint = "Hint: Cannot ctrl+click with multiple favorited recipes"; + return null; + } + } + else if (!acceptOnlyFavorites) { + if (element == null) { + element = elem; + recipeHint = excludeSpecial ? "Hint: ctrl+click to add the accessible normal recipe" : "Hint: ctrl+click to add the accessible recipe"; + } + else { + element = null; + recipeHint = "Hint: Set a favorite recipe to add it with ctrl+click"; + acceptOnlyFavorites = true; + } } } - } - return element; + return element; + } } public static void SetupForProject(Project project) {