public Program() { Runtime.UpdateFrequency = UpdateFrequency.Update100; H = new Helper(GridTerminalSystem); Echo("Lauching script...\n If you add more refineries recompile script"); // Get needed blocks in grid AllRefineries = H.Grid.GetGroupBlocks(controlledRefineries); EmptyCargoInventory = H.Grid.GetCargoContainers(emptyCargoContainer).First().GetInventory(0); ContainerInventories = H.Grid.GetBlocks().Where(t => t is IMyCargoContainer).Select(t => t.GetInventory(0)).ToList(); // Empty refineries into one cargo container before starting script (that should be handled by some inventory manager script) AllRefineries.Select(t => t.GetInventory(0)).Where(t => t.ItemCount > 0).ToList() .ForEach(t => CargoHelper.MoveAllCargo(t, EmptyCargoInventory)); }
public void Main(string argument, UpdateType updateSource) { H.UpdateTimer(); Echo("Running Ucat's automated refineries" + H.TimerChar); // Get ores in inventories var ores = ContainerInventories.SelectMany(t => CargoHelper.GetItemsInInventory(t)).Where(t => (t.IsOre && t.ItemName != CargoHelper.ICE) || (t.IsIngot && t.ItemName == CargoHelper.SCRAP)).ToList(); //Echo("Ores Found:\n" + ores.Select(t => t.ItemName + ": " + t.Quantity.ToIntSafe() + "\n").Aggregate((acc, t) => acc + t)); // Get most priority ore and quantity for each refinery //var itemName = oresPriority.FirstOrDefault(t => ores.Any(r => r.ItemName == t)); // Get ore with most quantity first Echo("Sorting ores by most quantity.\n"); var max = ores.Max(r => r.Quantity.RawValue); var itemName = ores.FirstOrDefault(t => t.Quantity.RawValue == max)?.ItemName; if (itemName == null) { Echo("\nAll done, nothing to refine"); return; } var item = ores.FirstOrDefault(t => t.ItemName == itemName); var divided = (MyFixedPoint)(item.Quantity.RawValue > 1000 ? ((decimal)item.Quantity.RawValue / AllRefineries.Count / 1000000) : 1); if (divided.RawValue < 1000000) { Echo("\nNot worth moving " + item.Quantity.ToIntSafe() + " " + itemName + ".\nWaiting for more..."); return; } var refineryOne = AllRefineries.First(); var moveOres = refineryOne.GetInventory(0).CurrentVolume.RawValue < refineryOne.GetInventory(0).MaxVolume.RawValue / 2; var emptySecondInventory = refineryOne.GetInventory(1).CurrentVolume.RawValue > refineryOne.GetInventory(1).MaxVolume.RawValue / 2; if (moveOres) { Echo("Moving " + divided.ToIntSafe() + " " + itemName + " into each refinery."); } AllRefineries.ForEach(t => { // Move target quantity into refinery if not full if (moveOres) { t.GetInventory(0).TransferItemFrom(item.Inventory, item.Item, divided); } // Empty second inventory if (emptySecondInventory) { CargoHelper.MoveAllCargo(t.GetInventory(1), EmptyCargoInventory); } // Turn off refinery when it's empty, on otherwise if (t.GetInventory(0).ItemCount == 0) { TerminalBlockHelper.TurnOff(t); } else { TerminalBlockHelper.TurnOn(t); } }); }