Created
July 11, 2019 10:48
-
-
Save Cranc/fff1c6620b4c3b938600fe9414cb6f99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace IngameScript | |
{ | |
partial class Program : MyGridProgram | |
{ | |
private static string INGOTS = "ingots"; | |
private static string COMPONENTS = "components"; | |
private static string INFORMATION_PANEL = "storage_information"; | |
private static string TRASH_REMOVEL = "Trash"; | |
private static string typeId_Ingots = "MyObjectBuilder_Ingot"; | |
private static string typeId_Components = "MyObjectBuilder_Component"; | |
private static double description_space = 3.5; | |
private static int progressBar_slots = 40; | |
private static int numberOf_decimals = 2; | |
private static char boarder_character = '+'; | |
private static Boolean trash_removel = true; | |
private List<IMyCargoContainer> cargo_container_ingots; | |
private List<IMyCargoContainer> cargo_container_components; | |
private List<IMyTextPanel> informationPanels; | |
private IMyShipConnector trashShute; | |
private string[] storage; | |
private Dictionary<string, uint> items; | |
public Program() | |
{ | |
this.Runtime.UpdateFrequency = UpdateFrequency.Update100; | |
this.items = new Dictionary<string, uint>(); | |
FillList(); | |
List<IMyShipConnector> list = new List<IMyShipConnector>(); | |
this.GridTerminalSystem.GetBlocksOfType(list, block => block.CustomData.Contains(TRASH_REMOVEL) && block.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
if (list.Count > 0) | |
this.trashShute = list[0]; | |
else | |
this.trashShute = null; | |
this.cargo_container_components = new List<IMyCargoContainer>(); | |
this.GridTerminalSystem.GetBlocksOfType(this.cargo_container_components, container => container.CustomData.Contains(COMPONENTS) && container.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
this.cargo_container_ingots = new List<IMyCargoContainer>(); | |
this.GridTerminalSystem.GetBlocksOfType(this.cargo_container_ingots, container => container.CustomData.Contains(INGOTS) && container.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
this.informationPanels = new List<IMyTextPanel>(); | |
GridTerminalSystem.GetBlocksOfType(this.informationPanels, panel => panel.CustomData.Contains(INFORMATION_PANEL) && panel is IMyTextPanel && panel.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
if (this.cargo_container_ingots.Count <= 0) | |
this.cargo_container_ingots = null; | |
if (this.cargo_container_components.Count <= 0) | |
this.cargo_container_components = null; | |
if (this.informationPanels.Count <= 0) | |
this.informationPanels = null; | |
this.storage = Storage.Replace("\n", "").Split(';'); | |
if (this.storage.Length > 0) | |
{ | |
LoadData(); | |
} | |
else | |
{ | |
this.storage = null; | |
SaveData(); | |
} | |
} | |
public void Save() | |
{ | |
SaveData(); | |
} | |
private void LoadData() | |
{ | |
for (int i = 0; i < this.storage.Length - 1; i++) | |
{ | |
try | |
{ | |
var item = this.storage[i++]; | |
var value = UInt32.Parse(this.storage[i]); | |
this.items[item] = value; | |
} | |
catch (Exception e) | |
{ | |
this.Echo(e.ToString()); | |
} | |
} | |
} | |
private void UpdateTaskList() | |
{ | |
this.storage = this.Me.CustomData.Replace("\n", "").Split(';'); | |
this.LoadData(); | |
} | |
private void FillList() | |
{ | |
items.Add("Stone", 10000); | |
items.Add("Iron", 100000); | |
items.Add("Nickel", 30000); | |
items.Add("Cobalt", 20000); | |
items.Add("Magnesium", 10000); | |
items.Add("Silicon", 40000); | |
items.Add("Silver", 1000); | |
items.Add("Gold", 1000); | |
items.Add("Platinum", 100); | |
items.Add("Uranium", 1000); | |
} | |
private void SaveData() | |
{ | |
string builder = ""; | |
foreach (KeyValuePair<string, uint> k in this.items) | |
{ | |
builder += k.Key; | |
builder += ";"; | |
builder += k.Value.ToString(); | |
builder += ";"; | |
builder += "\n"; | |
} | |
builder.Remove(builder.LastIndexOf(';'), 1); | |
this.Me.CustomData = builder; | |
this.Storage = ""; | |
this.Storage = builder; | |
} | |
private void TransfareComponents(List<IMyInventory> list) | |
{ | |
int i = 0; | |
foreach (var inv in list) | |
{ | |
List<MyInventoryItem> items = new List<MyInventoryItem>(); | |
inv.GetItems(items, entry => entry.Type.TypeId.Equals(typeId_Components)); | |
foreach (var item in items) | |
{ | |
var destination = cargo_container_components[i].GetInventory(); | |
while (!destination.CanItemsBeAdded(item.Amount, item.Type)) | |
{ | |
if (i + 1 >= cargo_container_components.Count) | |
{ | |
return; | |
} | |
destination = cargo_container_components[++i].GetInventory(); | |
} | |
destination.TransferItemFrom(inv, item, item.Amount).ToString(); | |
} | |
} | |
} | |
private void TransfareIngots(List<IMyInventory> list) | |
{ | |
int i = 0; | |
foreach (var inv in list) | |
{ | |
List<MyInventoryItem> items = new List<MyInventoryItem>(); | |
inv.GetItems(items, entry => entry.Type.TypeId.Equals(typeId_Ingots)); | |
foreach (var item in items) | |
{ | |
var destination = cargo_container_ingots[i].GetInventory(); | |
while (!destination.CanItemsBeAdded(item.Amount, item.Type)) | |
{ | |
if (i + 1 >= cargo_container_ingots.Count) | |
{ | |
return; | |
} | |
destination = cargo_container_ingots[++i].GetInventory(); | |
} | |
destination.TransferItemFrom(inv, item, item.Amount).ToString(); | |
} | |
} | |
} | |
private void DisplayComponents(IMyTextPanel panel, Double level_comp) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, "Components:")); | |
builder.Append(BuildProgressBar(level_comp)); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayIngots(IMyTextPanel panel, Double level_ing) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, "Ingots:")); | |
builder.Append(BuildProgressBar(level_ing)); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayRefineries(IMyTextPanel panel, Double level_refinery) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, "Refinery Input:")); | |
builder.Append(BuildProgressBar(level_refinery)); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayHydrogenTank(IMyTextPanel panel, Double ratio) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, "Hydrogen Tank:")); | |
builder.Append(BuildProgressBar(ratio)); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayBatteries(IMyTextPanel panel, Double ratio) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, "Battery charge:")); | |
builder.Append(BuildProgressBar(ratio)); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayIngots(IMyTextPanel panel, Double ratio, String name) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(BuildNameAndPadding(panel, name + " ingots:")); | |
builder.Append(BuildProgressBar(ratio)); | |
panel.WriteText(builder, true); | |
} | |
private StringBuilder BuildNameAndPadding(IMyTextPanel panel, string name) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(name); | |
var strInPixel = panel.MeasureStringInPixels(builder, panel.Font, panel.FontSize); | |
var panelSize = panel.SurfaceSize; | |
var pixel_left = ((double)panelSize.X / description_space) - (double)strInPixel.X; | |
StringBuilder space = new StringBuilder(); | |
space.Append(' '); | |
var spaceInPixel = panel.MeasureStringInPixels(space, panel.Font, panel.FontSize); | |
var spaces = pixel_left / (double)spaceInPixel.X; | |
spaces = Round(spaces); | |
if (spaces < 0) | |
spaces = 0; | |
builder.Append(' ', (int)spaces); | |
return builder; | |
} | |
private StringBuilder BuildProgressBar(Double level) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
var temp = level; | |
if (temp > 1)//dont go over max progressBar Slots but show percentages | |
temp = 1; | |
var progressNmbr = Round(temp * progressBar_slots); | |
var rest = progressBar_slots - progressNmbr; | |
if (rest < 0) | |
rest = 0; | |
if (progressNmbr < 0) | |
progressNmbr = 0; | |
builder.Append("["); | |
builder.Append('|', progressNmbr); | |
builder.Append('.', rest); | |
builder.Append("]"); | |
builder.Append(" "); | |
builder.Append((level * 100).ToString("N" + numberOf_decimals)); | |
builder.Append("%"); | |
builder.Append("\n"); | |
return builder; | |
} | |
private void BuildBorder(IMyTextPanel panel, char border) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
StringBuilder text = new StringBuilder(); | |
text.Append(border); | |
var nmbr = panel.SurfaceSize.X / panel.MeasureStringInPixels(text, panel.Font, panel.FontSize).X; | |
if (nmbr < 1) | |
return; | |
builder.Append(border, (int)nmbr); | |
builder.Append('\n'); | |
panel.WriteText(builder, true); | |
} | |
private void DisplayStorageLevels() | |
{ | |
if (this.informationPanels == null) | |
return; | |
List<IMyRefinery> refineries = new List<IMyRefinery>(); | |
this.GridTerminalSystem.GetBlocksOfType(refineries, block => block is IMyRefinery && block.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
List<IMyGasTank> tankH = new List<IMyGasTank>(); | |
this.GridTerminalSystem.GetBlocksOfType(tankH, block => block is IMyGasTank && block.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>(); | |
this.GridTerminalSystem.GetBlocksOfType(batteries, block => block is IMyBatteryBlock && block.CubeGrid.IsSameConstructAs(this.Me.CubeGrid)); | |
foreach (var panel in this.informationPanels) | |
{ | |
panel.WriteText(""); | |
panel.FontSize = .7f; | |
//components | |
DisplayComponents(panel, GetLevel(this.cargo_container_components)); | |
//ingots | |
DisplayIngots(panel, GetLevel(this.cargo_container_ingots)); | |
//refinery storage | |
DisplayRefineries(panel, GetLevelInputInventory(refineries)); | |
//Hydrogen Tank | |
DisplayHydrogenTank(panel, GetLevelTank(tankH)); | |
//Batteries | |
DisplayBatteries(panel, GetLevelCharge(batteries)); | |
//Border | |
BuildBorder(panel, boarder_character); | |
//Gravel + Trash Removel | |
DisplayIngots(panel, CountIngot("Stone") / items["Stone"], "Stone"); | |
//Iron Ingots | |
DisplayIngots(panel, CountIngot("Iron") / items["Iron"], "Iron"); | |
//Nickel Ingots | |
DisplayIngots(panel, CountIngot("Nickel") / items["Nickel"], "Nickel"); | |
//Cobalt Ingots | |
DisplayIngots(panel, CountIngot("Cobalt") / items["Cobalt"], "Cobalt"); | |
//Magnesium Ingots | |
DisplayIngots(panel, CountIngot("Magnesium") / items["Magnesium"], "Magnesium"); | |
//Silicon Ingots | |
DisplayIngots(panel, CountIngot("Silicon") / items["Silicon"], "Silicon"); | |
//Silver Ingots | |
DisplayIngots(panel, CountIngot("Silver") / items["Silver"], "Silver"); | |
//Gold Ingots | |
DisplayIngots(panel, CountIngot("Gold") / items["Gold"], "Gold"); | |
//Platinum Ingots | |
DisplayIngots(panel, CountIngot("Platinum") / items["Platinum"], "Platinum"); | |
//Uranium Ingots | |
DisplayIngots(panel, CountIngot("Uranium") / items["Uranium"], "Uranium"); | |
} | |
} | |
private float CountIngot(string subTypeId) | |
{ | |
MyFixedPoint amount = new MyFixedPoint(); | |
foreach(var container in cargo_container_ingots) | |
{ | |
var inv = container.GetInventory(); | |
var item = inv.FindItem(new MyItemType("MyObjectBuilder_Ingot", subTypeId)); | |
if (item.HasValue) | |
{ | |
amount += item.Value.Amount; | |
} | |
} | |
return (float) amount; | |
} | |
private Double GetLevelCharge<T>(List<T> list) where T : IMyBatteryBlock | |
{ | |
float max_charge = 0; | |
float cur_charge = 0; | |
foreach (var entry in list) | |
{ | |
max_charge += entry.MaxStoredPower; | |
cur_charge += entry.CurrentStoredPower; | |
} | |
return cur_charge / max_charge; | |
} | |
private Double GetLevelTank<T>(List<T> list) where T : IMyGasTank | |
{ | |
Double ratio = 0; | |
foreach (var entry in list) | |
{ | |
ratio += entry.FilledRatio; | |
} | |
if (list.Count != 0) | |
ratio = ratio / list.Count; | |
return ratio; | |
} | |
private Double GetLevelInputInventory<T>(List<T> list) where T : IMyProductionBlock | |
{ | |
MyFixedPoint max = new MyFixedPoint(); | |
MyFixedPoint cur = new MyFixedPoint(); | |
foreach (var entry in list) | |
{ | |
max += entry.InputInventory.MaxVolume; | |
cur += entry.InputInventory.CurrentVolume; | |
} | |
return ((double)cur.ToIntSafe() / (double)max.ToIntSafe()); | |
} | |
private Double GetLevel<T>(List<T> list) where T : IMyEntity | |
{ | |
MyFixedPoint max = new MyFixedPoint(); | |
MyFixedPoint cur = new MyFixedPoint(); | |
foreach (var inv in list) | |
{ | |
max += inv.GetInventory().MaxVolume; | |
cur += inv.GetInventory().CurrentVolume; | |
} | |
return ((double)cur.ToIntSafe() / (double)max.ToIntSafe()); | |
} | |
private void RemoveTrash() | |
{ | |
if (trashShute == null) | |
return; | |
var temp = CountIngot("Stone") - items["Stone"]; | |
MyFixedPoint amount = new MyFixedPoint(); | |
amount.RawValue = (long)(temp * 1000000); // convert float to MyFixedPoint | |
if (amount <= 0) | |
return; | |
foreach (var con in this.cargo_container_ingots) | |
{ | |
var inv = con.GetInventory(); | |
List<MyInventoryItem> items = new List<MyInventoryItem>(); | |
inv.GetItems(items, item => item.Type.SubtypeId.Equals("Stone")); | |
foreach(var item in items) | |
{ | |
if (amount > 0) | |
{ | |
var temp2 = item.Amount; | |
this.trashShute.GetInventory().TransferItemFrom(inv, item, amount); | |
amount -= temp2; | |
} | |
} | |
} | |
} | |
private int Round(double nmbr) | |
{ | |
int i = (int)nmbr; | |
double val = nmbr - i; | |
if (val >= 0.3) | |
return i + 1; | |
else | |
return i; | |
} | |
public void Main(string argument, UpdateType updateSource) | |
{ | |
//recalc these Lists in Main to include inventorys that connect to base | |
List<IMyAssembler> assemblers = new List<IMyAssembler>(); | |
List<IMyRefinery> refineries = new List<IMyRefinery>(); | |
List<IMyShipGrinder> grinders = new List<IMyShipGrinder>(); | |
List<IMyCargoContainer> cargos = new List<IMyCargoContainer>(); | |
List<IMyShipConnector> connectors = new List<IMyShipConnector>(); | |
List<IMyInventory> inventories_ingots = new List<IMyInventory>(); | |
List<IMyInventory> inventories_components = new List<IMyInventory>(); | |
this.GridTerminalSystem.GetBlocksOfType(assemblers, entity => entity is IMyAssembler); | |
this.GridTerminalSystem.GetBlocksOfType(refineries, entity => entity is IMyRefinery); | |
this.GridTerminalSystem.GetBlocksOfType(cargos, entity => entity is IMyCargoContainer); | |
this.GridTerminalSystem.GetBlocksOfType(connectors, entity => entity is IMyShipConnector && !entity.CustomData.Contains(TRASH_REMOVEL)); | |
this.GridTerminalSystem.GetBlocksOfType(grinders, entity => entity is IMyShipGrinder); | |
UpdateTaskList(); | |
foreach (var entry in grinders) | |
{ | |
inventories_components.Add(entry.GetInventory()); | |
} | |
foreach (var entry in assemblers) | |
{ | |
if(entry.Mode == MyAssemblerMode.Assembly) | |
inventories_components.Add(entry.OutputInventory); | |
} | |
foreach(var entry in refineries) | |
{ | |
inventories_ingots.Add(entry.OutputInventory); | |
} | |
foreach(var entry in cargos) | |
{ | |
inventories_components.Add(entry.GetInventory(0)); | |
inventories_ingots.Add(entry.GetInventory(0)); | |
} | |
foreach(var entry in connectors) | |
{ | |
inventories_components.Add(entry.GetInventory(0)); | |
inventories_ingots.Add(entry.GetInventory(0)); | |
} | |
//move components | |
if (this.cargo_container_components != null) | |
TransfareComponents(inventories_components); | |
//move ingots | |
if(this.cargo_container_ingots != null) | |
TransfareIngots(inventories_ingots); | |
if (trash_removel) | |
RemoveTrash(); | |
DisplayStorageLevels(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment