private bool empty(IMyTerminalBlock cube) { List <MyInventoryItem> li = new List <MyInventoryItem>(); IMyInventory src = cube.GetInventory(); src.GetItems(li); foreach (MyInventoryItem item in li) { //Echo("Trying to move "+item.Amount.ToIntSafe()+" of "+item.Type.SubtypeId+" from "+cube.CustomName); IMyCargoContainer box = getTargetToFill(); //Echo("Checking "+box.CustomName); IMyInventory tgt = box.GetInventory(); if (tryMove(src, tgt, item)) { Echo("Moved " + item.Amount.ToIntSafe() + " of " + item.Type.SubtypeId + " from " + cube.CustomName + " to " + box.CustomName); currentPreferredTarget = box; return(true); } else { currentPreferredTarget = null; } } return(false); }
public static VRage.MyFixedPoint ComputeAmountThatFits(this VRage.Game.ModAPI.Ingame.IMyInventory Inventory, VRage.Game.ModAPI.Ingame.IMyInventoryItem Item, VRage.MyFixedPoint?Amount = null) { var Id = Item.Content.GetId(); var ActualAmount = Amount.HasValue ? Amount.Value : Item.Amount; var FittingAmount = (Inventory as MyInventory).ComputeAmountThatFits(Id); return(FittingAmount > ActualAmount ? ActualAmount : FittingAmount); }
private bool tryMove(IMyInventory src, IMyInventory tgt, MyInventoryItem item) { long filled = src.CurrentVolume.RawValue; bool ret = src.TransferItemTo(tgt, item); ret &= src.CurrentVolume.RawValue != filled; return(ret); }
/// <summary> /// Updates cargoVolume, cargoMaxVolume, and prev_cargo with values from /// the cargo containers. /// </summary> void UpdateCargoRatio() { if (lastUpdate_cargo < updateInterval) { return; } lastUpdate_cargo = new TimeSpan(); prev_cargoRatio = cargoRatio; VRage.MyFixedPoint cargoVolume = 0, cargoMaxVolume = 0; GridTerminalSystem.GetBlocksOfType <IMyCargoContainer>(blocks, BlockOnMyGrid); for (int bi = 0; bi < blocks.Count; bi++) { int inventoryCount = blocks[bi].GetInventoryCount(); for (int ii = 0; ii < inventoryCount; ii++) { VRage.Game.ModAPI.Ingame.IMyInventory inventory = blocks[bi].GetInventory(ii); VRage.MyFixedPoint cv = inventory.CurrentVolume; VRage.MyFixedPoint mv = inventory.MaxVolume; if (cv + cargoBuffer < mv) { cargoVolume += cv; } else { cargoVolume += mv; } cargoMaxVolume += mv; } } cargoRatio = (float)cargoVolume / (float)cargoMaxVolume; Echo("Cargo: " + ToPercent(cargoRatio)); }
/// <summary> /// Removes components which are in the tool's cargo from pulling list. /// </summary> Dictionary <string, int> Reduce(Dictionary <string, int> ComponentList) { Dictionary <string, int> Reduced = ComponentList; VRage.Game.ModAPI.Ingame.IMyInventory MyInventory = ToolCargo; var InventoryList = MyInventory.GetItems(); List <string> ToRemove = new List <string>(); foreach (var Item in InventoryList) { if (ComponentList.ContainsKey(Item.Content.SubtypeName)) { Reduced[Item.Content.SubtypeName] -= (int)Item.Amount; if (Reduced[Item.Content.SubtypeName] <= 0) { ToRemove.Add(Item.Content.SubtypeName); } } } Reduced.RemoveAll(ToRemove); return(Reduced); }
/// <summary> /// Updates uraniumMass with values from the reactors. /// </summary> void UpdateReactorUranium() { if (lastUpdate_uranium < updateInterval) { return; } lastUpdate_uranium = new TimeSpan(); uraniumMass = 0; GridTerminalSystem.GetBlocksOfType <IMyReactor>(blocks, BlockOnMyGrid); for (int bi = 0; bi < blocks.Count; bi++) { int inventoryCount = blocks[bi].GetInventoryCount(); for (int ii = 0; ii < inventoryCount; ii++) { VRage.Game.ModAPI.Ingame.IMyInventory inventory = blocks[bi].GetInventory(ii); uraniumMass += inventory.CurrentMass; } } Echo("Uranium: " + uraniumMass); }
public Dictionary <string, int> PullIn(IMyTerminalBlock Puller, Dictionary <string, int> ComponentList, string TypeConstraint) { lock (InventoryLock) { // If you are wondering why I'm using Ingame interfaces in ModAPI, the Ingame inventory functions are more error-proof Dictionary <string, int> PulledList = new Dictionary <string, int>(); var AccessibleInventoryOwners = GetAccessibleInventories(Puller); VRage.Game.ModAPI.Ingame.IMyInventory PullingInventory = (Puller as Sandbox.ModAPI.Ingame.IMyTerminalBlock).GetInventory(); if (PullingInventory == null) { return(null); } foreach (var Block in AccessibleInventoryOwners) { VRage.Game.ModAPI.Ingame.IMyInventory BlockInventory; if (Block.InventoryCount > 1) { BlockInventory = (Block as Sandbox.ModAPI.Ingame.IMyTerminalBlock).GetInventory(1); } else { BlockInventory = (Block as Sandbox.ModAPI.Ingame.IMyTerminalBlock).GetInventory(0); } var InventoryList = BlockInventory.GetItems(); foreach (VRage.Game.ModAPI.Ingame.IMyInventoryItem Item in InventoryList) { if (Item.Amount <= 0 || (TypeConstraint.Contains("Component") && Item.Amount < 1)) { continue; // KSWH and their code... } if (!Item.Content.TypeId.ToString().Contains(TypeConstraint)) { continue; } if (!ComponentList.ContainsKey(Item.Content.SubtypeName)) { continue; } int NecessaryAmount = ComponentList[Item.Content.SubtypeName]; if (NecessaryAmount <= 0) { continue; //This means we've pulled everything we need } int PullableAmount = Item.Amount > NecessaryAmount ? NecessaryAmount : (int)Item.Amount; PullableAmount = (int)PullingInventory.ComputeAmountThatFits(Item, PullableAmount); if (PullableAmount == 0) { continue; } int ItemIndex = InventoryList.IndexOf(Item); int ItemAmount = (int)Item.Amount; PullingInventory.TransferItemFrom(BlockInventory, ItemIndex, null, true, PullableAmount); ComponentList[Item.Content.SubtypeName] -= PullableAmount; if (PulledList.ContainsKey(Item.Content.SubtypeName)) { PulledList[Item.Content.SubtypeName] += ItemAmount; } else { PulledList.Add(Item.Content.SubtypeName, ItemAmount); } } } return(PulledList); } }
public void Main() //called each cycle { tick++; if (tick % 4 != 0) { return; } counts.Clear(); long capacity = 0; long used = 0; long totalItems = 0; long capacityOver = 0; long usedOver = 0; long totalItemsOver = 0; List <string> nonfull = new List <string>(); foreach (IMyEntity io in containers) { IMyInventory inv = io.GetInventory(0); if (io is IMyProductionBlock) { inv = ((IMyProductionBlock)io).InputInventory; } int cap = inv.MaxVolume.ToIntSafe(); int usedVol = inv.CurrentVolume.ToIntSafe(); if (cap != usedVol) { string name = io.Name; if (io is IMyTerminalBlock) { name = ((IMyTerminalBlock)io).CustomName; } nonfull.Add(name); } capacity += cap; used += usedVol; List <MyInventoryItem> li = new List <MyInventoryItem>(); inv.GetItems(li); foreach (MyInventoryItem ii in li) { int amt = 0; string type = ii.Type.ToString(); counts.TryGetValue(type, out amt); int has = ii.Amount.ToIntSafe(); amt += has; counts[type] = amt; totalItems += has; } } if (!TREAT_OVERFLOW_AS_MAIN && used >= capacity) { foreach (IMyEntity io in overflow) { IMyInventory inv = io.GetInventory(0); //do not increment capacity, so fill fraction goes over 100% capacityOver += inv.MaxVolume.ToIntSafe(); used += inv.CurrentVolume.ToIntSafe(); usedOver += inv.CurrentVolume.ToIntSafe(); List <MyInventoryItem> li = new List <MyInventoryItem>(); inv.GetItems(li); foreach (MyInventoryItem ii in li) { int amt = 0; string type = ii.Type.ToString(); counts.TryGetValue(type, out amt); int has = ii.Amount.ToIntSafe(); amt += has; counts[type] = amt; totalItems += has; totalItemsOver += has; } } } double frac = used / (double)capacity; Color c = getDisplayColor(frac); foreach (IMyTextPanel scr in displays) { scr.WriteText(""); scr.BackgroundColor = c; scr.ContentType = ContentType.TEXT_AND_IMAGE; scr.FontColor = new VRageMath.Color(255, 255, 255, 255); scr.WriteText("Cargo is " + Math.Round(frac * 100, 1) + " % full.", true); if (!TREAT_OVERFLOW_AS_MAIN && frac > 1) { scr.WriteText("Cargo is overflowing!\n"); double frac2 = usedOver / (double)capacityOver; scr.WriteText("Overflow is " + Math.Round(frac2 * 100, 1) + " % full.\n", true); } if (nonfull.Count > 0 && frac > 0.8) { scr.WriteText("Space remains in: " + string.Join(", ", nonfull), true); } if (SHOW_ITEM_BREAKDOWN) { scr.WriteText(" Contents:\n", true); List <KeyValuePair <string, int> > entries = new List <KeyValuePair <string, int> >(); foreach (var entry in counts) { entries.Add(entry); } entries.Sort((e1, e2) => - e1.Value.CompareTo(e2.Value)); foreach (var entry in entries) { int prev = 0; countsLast.TryGetValue(entry.Key, out prev); string delta = " (No change)"; if (prev < entry.Value) { delta = " (+" + (entry.Value - prev) + " units)"; } else if (prev > entry.Value) { delta = " (-" + (prev - entry.Value) + " units)"; } if (countsLast.Count == 0) { delta = ""; } scr.WriteText(localize(entry.Key) + " x " + entry.Value + " (" + Math.Round(entry.Value * 100F / totalItems, 1) + " %)" + delta, true); scr.WriteText("\n", true); } } else { scr.WriteText("\n", true); } } if (SHOW_ITEM_BREAKDOWN) { countsLast = new Dictionary <string, int>(counts); } }
public void Main() //called each cycle { itemCounts.Clear(); fractions.Clear(); foreach (IMyEntity io in containers) { IMyInventory inv = io.GetInventory(); List <MyInventoryItem> li = new List <MyInventoryItem>(); inv.GetItems(li); foreach (MyInventoryItem ii in li) { TurretType ammo = isAmmoItem(ii); if (ammo != TurretType.UNKNOWN) { locale[ammo] = ii.Type.SubtypeId; //Echo(ii.Type.ToString()+" > "+ammo); if (currentTurrets.Contains(ammo)) { int amt = 0; itemCounts.TryGetValue(ammo, out amt); int has = ii.Amount.ToIntSafe(); amt += has; itemCounts[ammo] = amt; //Echo(ammo+" > "+amt); } } } } foreach (Display scr in ammoDisplays) { scr.prepare(); } foreach (Display scr in turretDisplays) { scr.prepare(); } foreach (TurretType type in itemCounts.Keys) { fractions[type] = Math.Min(1F, itemCounts[type] * roundValues[type] / (float)baseValues[type]); Echo(type + " > " + itemCounts[type] + " > " + fractions[type]); showStatus(type); } foreach (Display scr in ammoDisplays) { scr.write(""); } foreach (IMyLargeTurretBase tur in turrets) { IMyInventory inv = tur.GetInventory(); float f = inv.CurrentVolume == 0 ? 0 : inv.CurrentVolume.RawValue / (float)inv.MaxVolume.RawValue; if (tur is IMyLargeInteriorTurret) { f *= (float)inv.MaxVolume.RawValue / 0.01F; } if (f <= 0.01) { //Echo(tur.CustomName+" > "+inv.CurrentVolume+"/"+inv.MaxVolume); foreach (Display scr in turretDisplays) { scr.setColor(Color.Red); scr.write("Turret " + tur.CustomName + " is empty!"); } } else if (f <= 0.25) { //Echo(tur.CustomName+" > "+inv.CurrentVolume+"/"+inv.MaxVolume); foreach (Display scr in turretDisplays) { scr.setColor(Color.Yellow); scr.write("Turret " + tur.CustomName + " is low!"); } } } }