bool moveCargo(bool frontToRear) { MyFixedPoint remaining = 200; int fromIdx = 0, toIdx = this._invs.Count - 1; while (fromIdx < toIdx) { IMyInventory from = this._invs[frontToRear ? fromIdx : this._invs.Count - 1 - fromIdx].Inv; IMyInventory to = this._invs[frontToRear ? toIdx : this._invs.Count - 1 - toIdx].Inv; bool isToFull = false; var items = new List <MyInventoryItem>(); from.GetItems(items); isToFull = true; foreach (MyInventoryItem item in items) { MyFixedPoint previousAmount = item.Amount; from.TransferItemTo(to, item, remaining); if (from.GetItemAt(fromIdx).HasValue) { MyFixedPoint amountTransfered = previousAmount - from.GetItemAt(fromIdx).Value.Amount; remaining -= amountTransfered; if (remaining > 1) { isToFull = true; break; } } else { remaining -= previousAmount; } if (remaining < 1) { break; } } if (isToFull) { --toIdx; } else if (remaining > 1) { ++fromIdx; } else { break; } } return(remaining < 1); }
/// <summary> /// Attempts to move certain item type from one inventory to another. /// </summary> /// <param name="from">Source inventory.</param> /// <param name="to">Destination inventory.</param> /// <param name="type">Item type to move.</param> /// <param name="amount">How much to move. If not specified, attempts to move every item of set type.</param> /// <returns>The amount actually moved.</returns> public static MyFixedPoint MoveItem(IMyInventory from, IMyInventory to, MyItemType type, MyFixedPoint?amount = null) { MyFixedPoint amountmoved = new MyFixedPoint(); MyInventoryItem?item; bool moved; for (int i = from.ItemCount; i >= 0; i--) { item = from.GetItemAt(i); if (item.HasValue && item.Value.Type == type) { MyFixedPoint amounttomove = amount.HasValue ? MyFixedPoint.Min(amount.Value - amountmoved, item.Value.Amount) : item.Value.Amount; moved = to.TransferItemFrom(from, item.Value, amounttomove); if (!moved) { return(amountmoved); } amountmoved += amounttomove; if (amountmoved >= amount) { return(amountmoved); } } } return(amountmoved); }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); if (!_init) { TerminalBlock = Entity as Sandbox.ModAPI.IMyTerminalBlock; TerminalBlock.AppendingCustomInfo += AppendingCustomInfo; _init = true; } if (TerminalBlock.IsFunctional) { CompostGrid gridData = CompostDataStore.Get(TerminalBlock); float playerAmount = 0; if (gridData.LastScan == null || gridData.LastScan < DateTime.Now.AddSeconds(-5)) { gridData.Refresh(); foreach (IMyIdentity identity in gridData.PlayersOnToilet) { IMyPlayer player = Utilities.IdentityToPlayer(identity); if (player == null || player.Character == null || !player.Character.HasInventory) { continue; } IMyInventory inven = player.Character.GetInventory(); for (int i = inven.ItemCount - 1; i >= 0; i--) { VRage.Game.ModAPI.Ingame.MyInventoryItem?item = inven.GetItemAt(i); if (!item.HasValue) { continue; } if (item.Value.Type.ToString().Contains("Ore/Organic")) { playerAmount += (float)item.Value.Amount; inven.RemoveItemsAt(i); } } } } float amount = playerAmount + MyUtils.GetRandomFloat(Config.Instance.OrganicPerOxyenfarmPerSecondMin * gridData.EffectiveFarms * 1.6f, Config.Instance.OrganicPerOxyenfarmPerSecondMax * gridData.EffectiveFarms * 1.6f); TerminalBlock.GetInventory().AddItems((VRage.MyFixedPoint)amount, new MyObjectBuilder_Ore() { SubtypeName = "Organic" }); TerminalBlock.RefreshCustomInfo(); } }
void ClearInventory( IMyInventory inv, IMyCubeGrid targetGrid, List <string> filter = null) { for (int j = inv.ItemCount - 1; j >= 0; --j) { MyInventoryItem item = (MyInventoryItem)inv.GetItemAt(j); if (!CheckFilterMatch(item.Type, filter)) { continue; } MoveToContainer(item, inv, j, cargoContainers[targetGrid]); } }
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; } } } } }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); IMyInventory inventory = ((Sandbox.ModAPI.IMyTerminalBlock)Entity).GetInventory(0) as IMyInventory; double targetAmount = 10000; if (TerminalalBlock.CustomData != null && TerminalalBlock.CustomData != "") { try { targetAmount = double.Parse(TerminalalBlock.CustomData); } catch (Exception e) { } } try { for (int i = 0; i < inventory.ItemCount; i++) { MyObjectBuilder_PhysicalObject builder = GetItemBuilder(inventory.GetItemAt(i).Value); if (builder == null) { continue; } double amount = targetAmount - (double)inventory.GetItemAmount(builder); if (amount < 0) { inventory.RemoveItemsOfType((MyFixedPoint)Math.Abs(amount), builder); } else if (amount > 0) { inventory.AddItems((MyFixedPoint)amount, builder); } } } catch (Exception e) { } TerminalalBlock.RefreshCustomInfo(); }
public string GetStatus() { statusbuilder.Clear(); //statusbuilder.AppendLine($"NEEDSMOREBIRDS {NeedsMoreBirds}, TIMEOUT {BirdReleaseTimeout}"); //statusbuilder.AppendLine($"ENEMIES {TopEnemies.Count}"); // //foreach (var kvp in EnemyToNumBirds) //{ // statusbuilder.AppendLine($"- {kvp.Key.DisplayName} : {kvp.Value}"); //} var inventoryItem = AmmoBox.GetItemAt(0); if (inventoryItem != null) { statusbuilder.AppendLine(inventoryItem.Value.Type.SubtypeId); } else { statusbuilder.AppendLine("NULL"); } return(statusbuilder.ToString()); }