private static bool IsReorderable(MechComponentDef def) { if (!(def.ComponentType >= ComponentType.AmmunitionBox && def.ComponentType <= ComponentType.Upgrade)) { return(false); } if (MechDefBuilder.LocationCount(def.AllowedLocations) == 1) { return(false); } if (def.Is <Category>(out var category) && category.CategoryDescriptor.UniqueForLocation) { return(false); } return(true); }
public void AutoFixMechDef(MechDef mechDef, float originalTotalTonnage) { var builder = new MechDefBuilder(mechDef.Chassis, mechDef.Inventory.ToList()); // find any overused location if (!builder.HasOveruse()) { return; } // heatsinks, upgrades var itemsToBeReordered = mechDef.Inventory .Where(c => IsReorderable(c.Def)) .OrderBy(c => MechDefBuilder.LocationCount(c.Def.AllowedLocations)) .ThenByDescending(c => c.Def.InventorySize) .ThenByDescending(c => { switch (c.ComponentDefType) { case ComponentType.Upgrade: return(2); case ComponentType.AmmunitionBox: return(1); default: return(0); } }) .ToList(); // remove all items that can be reordered: heatsinks, upgrades foreach (var item in itemsToBeReordered) { builder.Remove(item); } // then add most restricting, and then largest items first (probably double head sinks) foreach (var item in itemsToBeReordered) { // couldn't add everything if (!builder.Add(item.Def)) { return; } } // if reorder does not work perfectly, ignore if (builder.HasOveruse()) { return; } // save mechDef.SetInventory(builder.Inventory.ToArray()); //Control.mod.Logger.LogDebug($"Name={mechDef.Name} ChassisID={mechDef.ChassisID}"); foreach (var item in mechDef.Inventory) { //Control.mod.Logger.LogDebug($" ComponentDefID={item.ComponentDefID} MountedLocation={item.MountedLocation}"); } }