private IEnumerator <bool> TransferItems(IMyInventory sourceInventory) { var isOreInventory = OreInventories.Contains(sourceInventory); var isComponentInventory = ComponentInventories.Contains(sourceInventory); sourceInventory.GetItems(Items); foreach (var item in Items) { if (item.Type.TypeId == "MyObjectBuilder_Ingot" || item.Type.TypeId == "MyObjectBuilder_Ore") { if (!isOreInventory) { foreach (var oreInventory in OreInventories) { if (sourceInventory.CanTransferItemTo(oreInventory, item.Type)) { if (sourceInventory.TransferItemTo(oreInventory, item)) { break; } } } } } else if (!isComponentInventory) { foreach (var componentInventory in ComponentInventories) { if (sourceInventory.CanTransferItemTo(componentInventory, item.Type)) { if (sourceInventory.TransferItemTo(componentInventory, item)) { break; } } } } yield return(true); } }
public void moveItemType(IMyInventory inInventory, List <MyInventoryItem> items) { IMyInventory outInventory; MyInventoryItem tempItem; int itemCount = items.Count; bool isFull = true; if (!(container_dict.ContainsKey(cargoType(items[0].Type.TypeId)))) { Echo("Trying to move item when there is no containers available"); return; } for (int i = 0; i < itemCount; ++i) { tempItem = items[i]; foreach (IMyCargoContainer container in container_dict[cargoType(items[i].Type.TypeId)]) { outInventory = container.GetInventory(); if (((float)outInventory.CurrentVolume / (float)outInventory.MaxVolume) < 0.9) { isFull = false; } if (inInventory.CanTransferItemTo(outInventory, items[i].Type)) { inInventory.TransferItemTo(outInventory, items[i]); } else { Echo("Can't transfer items to " + container.CustomName + " make sure the conveyor system is working"); } if (tempItem != items[i]) { itemCount--; i--; break; } } if (isFull) { addNewContainer(cargoType(tempItem.Type.TypeId)); } isFull = true; } }
/// <summary> /// Moves as many as possible from srcInventory to destinations /// </summary> private static bool TryTransferItemTo(IMyInventory srcInventory, List <IMyInventory> destinations, int srcItemIndex, MyInventoryItem srcItem, bool all, ExcludeInventory exclude) { var moved = false; if (all) { foreach (var destInventory in destinations) { if (exclude != null && exclude(destInventory, srcInventory, ref srcItem)) { continue; } if (destInventory.CanItemsBeAdded(srcItem.Amount, srcItem.Type) && srcInventory.CanTransferItemTo(destInventory, srcItem.Type)) { moved = srcInventory.TransferItemTo(destInventory, srcItemIndex, null, true, srcItem.Amount, false); if (moved) { break; } } } return(moved); } foreach (var destInventory in destinations) { if (exclude != null && exclude(destInventory, srcInventory, ref srcItem)) { continue; } if (srcInventory.CanTransferItemTo(destInventory, srcItem.Type)) { var amount = destInventory.MaxItemsAddable(srcItem.Amount, srcItem.Type); if (amount > 0) { moved = srcInventory.TransferItemTo(destInventory, srcItemIndex, null, true, amount, true) || moved; if (srcItem.Amount <= 0) { break; } } } } return(moved); }
private void MoveToMisc(IMyInventory source, MyInventoryItem item) { /* * try to move the item to an uncategorized inventory */ foreach (IMyTerminalBlock block in miscInventoryBlocks) { IMyInventory destination = block.GetInventory(); if (source.CanTransferItemTo(destination, item.Type) && !destination.IsFull) // same as above { if (source.TransferItemTo(destination, item)) // TransferItemTo returns true even if nothing was transferred { return; } } } }
void MoveItemFromStorageToInventory( MyItemType type, IMyInventory targetInv, VRage.MyFixedPoint amount ) { VRage.MyFixedPoint remainingAmount = amount; foreach (CargoContainer container in cargoContainers[Me.CubeGrid]) { IMyInventory inventory = container.container.GetInventory(); if (!inventory.IsConnectedTo(targetInv)) { continue; } for (int i = 0; i < inventory.ItemCount; ++i) { MyInventoryItem item = (MyInventoryItem)inventory.GetItemAt(i); if (item.Type != type) { continue; } if (!inventory.CanTransferItemTo(targetInv, item.Type)) { continue; } VRage.MyFixedPoint transferrableAmount = remainingAmount; if (item.Amount < transferrableAmount) { transferrableAmount = item.Amount; } if (inventory.TransferItemTo(targetInv, item, transferrableAmount)) { remainingAmount -= transferrableAmount; if (remainingAmount == 0) { return; } } } } }
private bool TryMoveToTarget(IMyInventory source, Category target, MyInventoryItem item) { /* * try to move the item from the source inventory to an inventory of the appropriate category */ foreach (IMyTerminalBlock block in target.Inventories) { IMyInventory destination = block.GetInventory(); // TransferItemTo() returns true even if nothing was moved (but the type was appropriate) if (source.CanTransferItemTo(destination, item.Type) && !destination.IsFull) // connected and room available { if (source.TransferItemTo(destination, item)) { return(true); } } } return(false); }
bool ExportItem(MyInventoryItem item, IMyInventory fromInventory, List <IMyTerminalBlock> destinationBlocks) { VRage.MyFixedPoint amount = item.Amount; foreach (var block in destinationBlocks) { var inv = block.GetInventory(0); if (fromInventory == inv) { continue; } if (!inv.IsFull && fromInventory.CanTransferItemTo(inv, item.Type)) { var info = item.Type.GetItemInfo(); double freeVolume = (double)(inv.MaxVolume - inv.CurrentVolume); if (info.Volume <= freeVolume) { //Move all fromInventory.TransferItemTo(inv, item); return(true); } else { //Calculate how much to move double part = freeVolume / info.Volume; amount *= (VRage.MyFixedPoint)part; fromInventory.TransferItemTo(inv, item, amount); if (amount == 0) { return(true); } } } } return(false); }
void FindCargoSpace( MyInventoryItem item, IMyInventory currentInv, List <CargoContainer> inputContainers, List <CargoContainer> output) { //perfect match contains item sufficient space //1 1 1 = 7 (ideal) //1 1 0 = 6 //1 0 1 = 5 //1 0 0 = 4 //0 1 1 = 3 //0 1 0 = 2 //0 0 1 = 1 //0 0 0 = 0 float itemVolume = ItemVolume(item); output.Clear(); List <CargoContainer>[] sortLists = new List <CargoContainer> [8]; for (int i = 0; i < 8; ++i) { sortLists[i] = new List <CargoContainer>(); } foreach (CargoContainer container in inputContainers) { int parsedType = (int)ParseType(item); if (0 == (parsedType & container.acceptorBits)) { continue; } IMyInventory inv = container.container.GetInventory(); if (!currentInv.CanTransferItemTo(inv, item.Type)) { continue; } float freeSpace = inv.MaxVolume.RawValue - inv.CurrentVolume.RawValue; if (freeSpace == 0f) { continue; } int sortKey = 0; if (freeSpace >= itemVolume) { sortKey ^= 1; } if (container.Contains(item.Type)) { sortKey ^= 2; } if (parsedType == container.acceptorBits) { // container ONLY accepts this type of Item (e.g. ores) sortKey ^= 4; } sortLists[sortKey].Add(container); } for (int i = 7; i >= 0; --i) { output.AddRange(sortLists[i]); } }