bool filterInventories(IMyTerminalBlock block) { if (block.CubeGrid != Me.CubeGrid || !block.HasInventory) { return(false); } else { List <MyInventoryItem> items = new List <MyInventoryItem>(); int allItemsCount = 0; block.GetInventory(0).GetItems(items); allItemsCount += items.Count; if (block.InventoryCount == 2) { List <MyInventoryItem> items2 = new List <MyInventoryItem>(); block.GetInventory(1).GetItems(items2); allItemsCount += items2.Count; } if (allItemsCount == 0) { return(false); } } return(true); }
public string getFillLevelBarForBlock(IMyTerminalBlock Block, out int fillLevel0, out int fillLevel1) { fillLevel0 = 0; fillLevel1 = 0; if (!Block.HasInventory) { return(""); } bool showBorderForThisBlock = showBlockState && Block is IMyProductionBlock; char stateColor = showBlockState?getStateColor(Block):'\0'; List <string> slug = new List <string>(); string bar0 = getFillLevelBarForInventory(Block.GetInventory(0), out fillLevel0); if (showBorderForThisBlock) { bar0 = stateColor + bar0.Substring(2) + stateColor; } switch (Block.InventoryCount) { case 1: for (int i = 0; i <= 15; i++) { slug.Add(bar0); } break; case 2: string bar1 = getFillLevelBarForInventory(Block.GetInventory(1), out fillLevel1); if (showBorderForThisBlock) { bar1 = stateColor + bar1.Substring(2) + stateColor; } for (int i = 0; i <= 7; i++) { slug.Add(bar0); } slug.Add(showBorderForThisBlock?stateColor + barDivider.Substring(0, LCD_WIDTH - 1) + stateColor:barDivider); for (int i = 0; i <= 7; i++) { slug.Add(bar1); } break; } if (showBorderForThisBlock && slug.Count > 0) { slug[0] = new String(stateColor, LCD_WIDTH); slug[slug.Count - 1] = slug[0]; } return(string.Join("\n", slug.ToArray())); }
Vector3 SortInventory(IMyTerminalBlock block) { IMyInventory inv; if (block.InventoryCount == 2) { inv = block.GetInventory(1); } else { inv = block.GetInventory(0); } //Abort if inventory is missing if (inv == null) { BlocksMissingInventory.Add(block.CustomName); return(Vector3.Zero); } var items = new List <MyInventoryItem>(); inv.GetItems(items); var itemsSorted = items.Distinct().OrderBy(q => q.Type.SubtypeId).ToList(); //Sort alphabetically and remove dupes. /* * //Checkas all items in slots >= current slot, for each slot. Sorts and removes dupes. * for (int i = 0; i < itemsSorted.Count; i++) * { * for (int j = i; j < inv.ItemCount; j++) * { * var item = inv.GetItemAt(j); * * if (itemsSorted[i] == item) * { * //Item fouond * * if (i == j) continue; //Already in the right sport, continue and look for dupes. * * //Move home. * if(inv.GetItemAt(i) == items[j]) * { * //Target slot contains same item, merge * inv.TransferItemFrom(inv, j, inv.ItemCount, true); * j--; //Check same index again * } * else * { * //Target slot contains something else, don't merge * inv.TransferItemFrom(inv, j, inv.ItemCount); * } * } * } * } */ return(new Vector3((float)inv.CurrentVolume, (float)inv.MaxVolume, 0)); }
public void Scan1(clsItemName liste, Program pp) { List <MyInventoryItem> c3 = new List <MyInventoryItem>(); thisBlock.GetInventory().GetItems(c3); thisBlock.CustomData = STORAGETAG + "\n"; foreach (MyInventoryItem e1 in c3) { thisBlock.CustomData += "+ " + liste.ContainsKey(e1, pp).rd + "\n"; } }
public static void WeaponCoreReplenishment(IMyTerminalBlock block) { MES_SessionCore.Instance.WeaponCore.DisableRequiredPower(block); var ammoList = GetCompatibleAmmoTypes(block); //Fill Ammos - int totalMagazines = 0; int totalLoopRuns = 0; int maxMagazines = 100; //Until Moved To Config; int maxLoopRuns = 100; if (ammoList.Count == 0) { return; } while (totalMagazines < maxMagazines && totalLoopRuns < maxLoopRuns) { totalLoopRuns++; bool breakNow = false; bool noLoop = true; bool addedItem = false; foreach (var ammoId in ammoList) { if (ammoId.SubtypeName == "Energy") { continue; } noLoop = false; var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(ammoId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = 1, Content = content }; if (block.GetInventory().CanItemsBeAdded(1, ammoId) == true) { block.GetInventory().AddItems(1, inventoryItem.Content); totalMagazines++; addedItem = true; } } if (breakNow || noLoop || !addedItem) { break; } } }
private void PushItem(IMyTerminalBlock from, MyInventoryItem item) { /* * tries to push item to the most appropriate inventory, or returns if it can't * there's some repeated code here, but come on, i'm not writing a function for 2 calls */ HashSet <string> blockCategoryKeySet = idToCategoryKeySet[from.EntityId]; // it might be more accurate to call this 'full type' // i'm not using the actual subtype because it would cause conflicts between ingots/ores string subtype = item.Type.ToString(); string type = item.Type.TypeId; string categoryKey; if (typeToCategoryKey.TryGetValue(subtype, out categoryKey)) // to avoid exceptions with unregistered items { if (blockCategoryKeySet.Contains(categoryKey)) // already in the right place { return; } if (Categories[categoryKey].Inventories.Count > 0) // if there's a place to put it { if (TryMoveToTarget(from.GetInventory(), Categories[categoryKey], item)) // if it could actually be moved to any matching chest { return; } } } if (typeToCategoryKey.TryGetValue(type, out categoryKey)) { if (blockCategoryKeySet.Contains(categoryKey)) // already in the right place { return; } if (Categories[categoryKey].Inventories.Count > 0) // if there's a place to put it { if (TryMoveToTarget(from.GetInventory(), Categories[categoryKey], item)) // if it could actually be moved to any matching chest { return; } } } if (blockCategoryKeySet.Count > 0) // if the current inventory isn't misc. { MoveToMisc(from.GetInventory(), item); } }
public BlockInterface(IMyTerminalBlock ablock) { block = ablock; name = ablock.CustomName; input = ablock.GetInventory(0); if (ablock.InventoryCount > 1) { hasOutput = true; output = ablock.GetInventory(1); } for (int i = 0; i < (int)Labeling.NumOfLabels; i++) { labels[i] = false; } }
bool haveIce(IMyTerminalBlock block) { IMyInventory tempInv; List <MyInventoryItem> items = new List <MyInventoryItem>(); if (block.HasInventory) { tempInv = block.GetInventory(); tempInv.GetItems(items, it => (it.Type.SubtypeId == "Ice")); if (items.Count != 0) { foreach (MyInventoryItem item in items) { cargoIce += (double)item.Amount; } return(true); } } else { Echo("Error: checking inventory of blocks without inventory"); return(false); } return(false); }
bool CollectParts(IMyTerminalBlock block) { if (block.CubeGrid.EntityId != Context.Reference.CubeGrid.EntityId) { return(false); } if (block.HasInventory && block.CustomName.Contains("<M>")) { Inventories.Add(block.GetInventory(block.InventoryCount - 1)); } if (block is IMyGasTank && (block.BlockDefinition.SubtypeId == "LargeHydrogenTank" || block.BlockDefinition.SubtypeId == "SmallHydrogenTank" || block.BlockDefinition.SubtypeId == "LargeHydrogenTankSmall" || block.BlockDefinition.SubtypeId == "SmallHydrogenTankSmall")) { HydrogenTanks.Add((IMyGasTank)block); } if (block is IMyBatteryBlock) { Batteries.Add((IMyBatteryBlock)block); } if (block is IMyBeacon) { Beacon = (IMyBeacon)block; } return(false); }
void GetBlockRequestSettings(IMyTerminalBlock block) { InventoryRequests[block] = new Dictionary <MyItemType, int>(); if (iniParser.TryParse(block.CustomData) && iniParser.ContainsSection(kInventoryRequestSection)) { iniParser.GetKeys(iniKeyScratchpad); foreach (var key in iniKeyScratchpad) { if (key.Section != kInventoryRequestSection) { continue; } var count = iniParser.Get(key).ToInt32(); if (count == 0) { continue; } var type = MyItemType.Parse(key.Name); var inventory = block.GetInventory(block.InventoryCount - 1); itemTypeScratchpad.Clear(); inventory.GetAcceptedItems(itemTypeScratchpad); if (!itemTypeScratchpad.Contains(type)) { continue; } InventoryRequests[block][type] = count; } } }
private void getCounts(string itemstring) { int count = 0; List <IMyTerminalBlock> containers = new List <IMyTerminalBlock>(); GridTerminalSystem.GetBlocksOfType(containers, (IMyTerminalBlock block) => block.HasInventory); //Echo("Found " + containers.Count.ToString() + " containers"); for (int i = 0; i < containers.Count; i++) { IMyTerminalBlock cont = containers[i]; List <MyInventoryItem> items = new List <MyInventoryItem>(); IMyInventory cont_inv = cont.GetInventory() as IMyInventory; if (cont_inv.ItemCount == 0) { continue; } cont_inv.GetItems(items, (MyInventoryItem item) => item.Type.ToString() == itemstring); //Echo("Found " + items.Count.ToString() + " items in " + cont.Name); for (int j = 0; j < items.Count; j++) { var item = items[j]; count += item.Amount.ToIntSafe(); } } counts[itemstring] = count; }
public override void GetInstances(IMyTerminalBlock block, List <IMyInventory> instances) { for (int i = 0; i < block.InventoryCount; i++) { instances.Add(block.GetInventory(i)); } }
public static IEnumerable <IMyInventory> getInventories(this IMyTerminalBlock block) { for (int inventoryIdx = 0; inventoryIdx < block.InventoryCount; ++inventoryIdx) { yield return(block.GetInventory(inventoryIdx)); } }
private void StackAll(IMyTerminalBlock cargo) { var reset = true; while (reset) { var firstSlots = new Dictionary <string, int>(); var inv = cargo.GetInventory(0); var items = new List <MyInventoryItem>(); inv.GetItems(items); reset = false; for (int i = 0; i < items.Count; ++i) { var item = items[i]; var itemName = decodeItemName(item.Type.ToString(), item.Type.ToString()); if (firstSlots.ContainsKey(itemName)) { var targetIndex = firstSlots[itemName]; var amount = item.Amount; inv.TransferItemTo(inv, i, targetIndex, true, amount); reset = true; break; } else { firstSlots.Add(itemName, i); } } } }
// Get all inventories of a block. Most have just one. Refineries and Assemblers have two. static IEnumerable <IMyInventory> AllInventories(IMyTerminalBlock block) { for (int i = 0; i < block.InventoryCount; i++) { yield return(block.GetInventory(i)); } }
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); }
private bool AddCategoriesToBlock(IMyTerminalBlock block) { /* * try (and add) all relevant categories to a block, mark the category as active, and increment inventory counts * true if any category was added, false if not */ bool hasCategory = false; string[] customData = block.CustomData.Split(newLine); foreach (string tag in customData) { string categoryKey; if (tagToCategoryKey.TryGetValue(tag, out categoryKey)) { Category category = Categories[categoryKey]; ActiveCategories[categoryKey] = category; // doesn't really matter what's used for this key category.Inventories.Add(block); IMyInventory inventory = block.GetInventory(); category.CurrentVolume += inventory.CurrentVolume; category.MaxVolume += inventory.MaxVolume; idToCategoryKeySet[block.EntityId].Add(categoryKey); hasCategory = true; } } return(hasCategory); }
bool Filter(IMyTerminalBlock block, GridManager gridManager) => block.GetInventory() != null && gridManager.Manages(block.CubeGrid) && !(block is IMyUserControllableGun) && !(block is IMyCargoContainer) && !(block is IMyProductionBlock) && !(block is IMyGasGenerator) && !(block is IMyGasTank) && !(block is IMyReactor);
bool checkBlock() { if (inventoryIndex_ < block_.InventoryCount) { var inventory = block_.GetInventory(inventoryIndex_); acceptedItems_.Clear(); acceptedItemIndex_ = 0; inventory.GetAcceptedItems(null, (itemType) => { int index = dc_.itemTypeFilter_.FindIndex(x => VISItemType.compareItemTypes(x.type, itemType)); ItemType item = new ItemType(); item.type = itemType; if (index >= 0) { item.amount = dc_.itemTypeFilter_[index].amount; acceptedItems_.Add(item); } else if (dc_.itemTypeFilter_.Count == 0) { long defaultAmount = Default.AmountItems.FirstOrDefault(pair => pair.Key.Equals(item.type)).Value; item.amount = defaultAmount > 0 ? defaultAmount : dc_.defaultMaxAmountItems_; acceptedItems_.Add(item); } return(false); }); if (acceptedItems_.Count > 0) { dc_.inventories_.Add(inventory); dc_.maxVolume_ += (double)inventory.MaxVolume; addBlock = true; } Check = checkInventory; return(false); } if (addBlock) { dc_.Blocks.Add(block_); } return(true); }
override public IEnumerable <ResourceStack> GetResources() { if (_block.HasInventory) { List <MyInventoryItem> items = new List <MyInventoryItem>(); _block.GetInventory().GetItems(items); return(items.Select(item => ConvertToResource(item)).Where(resource => resource != null)); } return(new List <ResourceStack>()); }
private void doSortContainer(IMyTerminalBlock Block, Dictionary <string, List <IMyTerminalBlock> > targets) { debug(" Inventory Count: " + Block.GetInventoryCount().ToString()); for (int i_Inventory = 0; i_Inventory < Block.GetInventoryCount(); i_Inventory++) { debug(" Start Sorting Inventory " + (i_Inventory + 1).ToString() + " of " + Block.CustomName); doSortInventory(Block, Block.GetInventory(i_Inventory), targets); debug(" End Sorting Inventory " + (i_Inventory + 1).ToString() + " of " + Block.CustomName); } }
protected static IMyInventory GetInvWithName(string name, int idx) { IMyTerminalBlock blk = GridTerminalSystem.GetBlockWithName(name); if (blk == null) { return(null); } return(blk.GetInventory(idx)); }
private void ProcessBlock(IMyTerminalBlock block) { for (int i = 0; i < block.InventoryCount; i++) { items.Clear(); IMyInventory inventory = block.GetInventory(0); CurrentCapacity += (double)inventory.CurrentVolume; TotalCapacity += (double)inventory.MaxVolume; block.GetInventory(i).GetItems(items); foreach (MyInventoryItem item in items) { if (item.Type.TypeId == "MyObjectBuilder_Ore") { hasAnyOre = true; } else { hasSomethingExceptOre = true; } //string itemName = item.Type.SubtypeId; string itemName = item.Type.ToString(); double amount = (double)item.Amount; if (cargo.ContainsKey(itemName)) { CargoEntry ce = cargo[itemName]; ce.amount += amount; CargoEntry ce2 = cargo[itemName]; } else { CargoEntry ce = new CargoEntry { amount = amount, typeid = item.Type.TypeId }; cargo.Add(itemName, ce); } } } }
public void TransferItemTo(IMyTerminalBlock from, IMyTerminalBlock to, string name) { if (from == null || to == null) { return; } IMyInventory a1 = from.GetInventory(0); List <MyInventoryItem> a2 = new List <MyInventoryItem>(); a1.GetItems(a2); Echo("a2=" + a2.Count.ToString()); IMyInventory b1 = to.GetInventory(); List <MyInventoryItem> b2 = new List <MyInventoryItem>(); b1.GetItems(b2); Echo("b2=" + b2.Count.ToString()); int x = 0; for (; x < a2.Count; x++) { if (getName(a2[x]) == name) { break; } } int y = 0; for (; y < b2.Count; y++) { if (getName(b2[y]) == name) { break; } } Echo(string.Format("x={0},y={1}", x, y)); if (x >= a2.Count) { return; } if (y >= b2.Count) { y = 0; } bool z = a1.TransferItemTo(b1, x); Echo(z.ToString()); }
private void countInventory(IMyTerminalBlock b) { List <MyInventoryItem> li = new List <MyInventoryItem>(); b.GetInventory().GetItems(li); if (li.Count > 0) { foreach (MyInventoryItem item in li) { totalItems += item.Amount.RawValue; } } }
Vector3 GetFillLevel(IMyTerminalBlock block) { IMyInventory inv; if (block.InventoryCount == 2) { inv = block.GetInventory(1); } else { inv = block.GetInventory(0); } //Abort if inventory is missing if (inv == null) { BlocksMissingInventory.Add(block.CustomName); return(Vector3.Zero); } return(new Vector3((float)inv.CurrentVolume, (float)inv.MaxVolume, 0)); }
private bool empty(IMyTerminalBlock cube) { if (cube is IMyProductionBlock) { bool flag = false; flag |= emptyInventory(cube, ((IMyProductionBlock)cube).InputInventory); flag |= emptyInventory(cube, ((IMyProductionBlock)cube).OutputInventory); return(flag); } else { return(emptyInventory(cube, cube.GetInventory())); } }
public void Main() //called each cycle { if (currentSources.Count == 0) { currentSources.AddList(ourContainers); } if (tick % 5 == 0) { containersToFill.Clear(); GridTerminalSystem.GetBlocksOfType <IMyCargoContainer>(containersToFill, b => b.CubeGrid != Me.CubeGrid && shipsToOffload.Contains(b.CubeGrid.DisplayName)); } if (containersToFill.Count == 0) { Echo("No valid target inventories."); return; } Echo("Emptying " + ourContainers.Count + " containers into " + containersToFill.Count + " targets."); IMyTerminalBlock box = getRandom(currentSources); if (isEmpty(box)) { Echo(box.CustomName + " is already empty."); currentSources.Remove(box); } else if (empty(box)) { if (isEmpty(box)) { Echo("Emptied " + box.CustomName); currentSources.Remove(box); } else { Echo(box.CustomName + " still has cargo occupying " + box.GetInventory().CurrentVolume *1000 + " m3"); } } else { Echo(box.CustomName + " could not have any cargo moved from it!"); } buildCounts(); Echo("Item total is " + totalItems); if (lastTotal != totalItems) { Echo("Item count changed from " + lastTotal + "!"); } lastTotal = totalItems; tick++; }
public void Main(string argument, UpdateType updateSource) { // The main entry point of the script, invoked every time // one of the programmable block's Run actions are invoked, // or the script updates itself. The updateSource argument // describes where the update came from. Be aware that the // updateSource is a bitfield and might contain more than // one update type. // // The method itself is required, but the arguments above // can be removed if not needed. ///example, find something in one cargo container and move half of it to another container IMyTerminalBlock cargo1 = GridTerminalSystem.GetBlockWithName("cargo1"); //IMyTerminalBlock cargo2 = GridTerminalSystem.GetBlockWithName("cargo2"); VRage.MyFixedPoint fixedPoint; IMyInventory inven1; //IMyInventory inven2; List <MyInventoryItem> stacks1 = new List <MyInventoryItem>(); //List <MyInventoryItem> stacks2 = new List<MyInventoryItem>(); inven1 = cargo1.GetInventory(); Echo(cargo1.InventoryCount.ToString()); //inven2 = cargo2.GetInventory(); //inven1.GetItems(stacks1, null); /* * foreach (MyInventoryItem stack in stacks1) { * * if (stack.Type.SubtypeId == "Nickel" && stack.Amount > 0) { * * //double dbAmount = Convert.ToDouble(amount); * //double amountToMove = ((double)stack.Amount / newAmount); * //Echo(amountToMove.ToString()); * * long inputAmount = stack.Amount.ToIntSafe(); * double dbAmount = Convert.ToDouble(inputAmount); * fixedPoint = (VRage.MyFixedPoint)dbAmount; * inven1.TransferItemTo(inven2, stack, fixedPoint); * * * } * } */ }
public static bool isConnectedToCargo(IMyCargoContainer container, IMyTerminalBlock block) { if (container.InventoryCount == 0) { return(false); } IMyInventory inventory = container.GetInventory(0); if (block.InventoryCount == 0) { return(true); } return(block.GetInventory(0).CanTransferItemTo(inventory, ICE_ITEM)); }