public void TransferItemFrom(MyInventory sourceInventory, int sourceItemIndex, int? targetItemIndex = null, bool? stackIfPossible = null, MyFixedPoint? amount = null) { Debug.Assert(sourceInventory != null); if (this == sourceInventory) return; if (sourceItemIndex < 0 || sourceItemIndex >= sourceInventory.m_items.Count) return; Transfer(sourceInventory, this, sourceInventory.GetItems()[sourceItemIndex].ItemId, targetItemIndex.HasValue ? targetItemIndex.Value : -1, amount); }
bool IMyInventory.IsConnectedTo(IMyInventory dst) { MyInventory dstInventory = dst as MyInventory; if (dstInventory != null) { return(IsConnected(dstInventory)); } return(false); }
bool Sandbox.ModAPI.Interfaces.IMyInventory.IsConnectedTo(Sandbox.ModAPI.Interfaces.IMyInventory dst) { MyInventory dstInventory = dst as MyInventory; if (dstInventory != null) { return(IsConnected(dstInventory)); } return(false); }
private bool TransferItemsFrom(IMyInventory sourceInventory, int sourceItemIndex, int?targetItemIndex, bool?stackIfPossible, VRage.MyFixedPoint?amount, bool useConveyors) { MyInventory srcInventory = sourceInventory as MyInventory; if (srcInventory != null && (useConveyors == false || IsConnected(srcInventory) == true)) { TransferItemFrom(srcInventory, sourceItemIndex, targetItemIndex, stackIfPossible, amount); return(true); } return(false); }
private MyGuiControlGrid MakeInventoryGrid(MyInventory inventory) { var grid = new MyGuiControlGrid(); grid.Name = "InventoryGrid"; grid.VisualStyle = MyGuiControlGridStyleEnum.Inventory; grid.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP; grid.ColumnsCount = 7; grid.RowsCount = 1; grid.ShowTooltipWhenDisabled = true; grid.UserData = inventory; return grid; }
public static void Transfer(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null, bool spawn = false) { var itemNullable = src.GetItemByID(srcItemId); if (!itemNullable.HasValue) return; var item = itemNullable.Value; if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId())) return; var transferAmount = amount ?? item.Amount; SyncObject.TransferItems(src, transferAmount, srcItemId, dst, dstIdx, spawn); }
private bool TransferItemsTo(IMyInventory dst, int sourceItemIndex, int?targetItemIndex, VRage.MyFixedPoint?amount, bool useConveyor) { MyInventory dstInventory = dst as MyInventory; if (dstInventory != null) { if (sourceItemIndex < 0 || sourceItemIndex >= this.m_items.Count || (useConveyor == true && IsConnected(dstInventory) == false)) { return(false); } Transfer(this as MyInventory, dstInventory, this.GetItems()[sourceItemIndex].ItemId, targetItemIndex.HasValue ? targetItemIndex.Value : -1, amount); return(true); } return(false); }
private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false) { //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyInventoryItem item = src.m_items[i]; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject; if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f) { i++; continue; } // End of oxygen specific code if (item.Content.GetObjectId() != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } }
private bool IsConnected(MyInventory dstInventory) { var srcConveyor = (this.Owner as IMyConveyorEndpointBlock); if (srcConveyor != null) { reachableVertices.Clear(); MyGridConveyorSystem.Pathfinding.FindReachable(srcConveyor.ConveyorEndpoint, reachableVertices, (vertex) => vertex.CubeBlock != null); foreach (var vertex in reachableVertices) { if (dstInventory.Owner == vertex.CubeBlock) { return(true); } } } return(false); }
public static void TransferAll(MyInventory src, MyInventory dst) { Debug.Assert(Sync.IsServer, "Calling a server-only method on the client!"); if (!Sync.IsServer) return; int prevItemCount = src.m_items.Count + 1; while (src.m_items.Count != prevItemCount && src.m_items.Count != 0) { prevItemCount = src.m_items.Count; Transfer(src, dst, src.m_items[0].ItemId); } Debug.Assert(src.m_items.Count == 0, "Could not move all inventory items!"); }
//this is from client only public static void TransferByUser(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null) { // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref if (dst.Owner == null) MyLog.Default.WriteLine("dst.Owner == null"); if (src == null) { return; } var itemNullable = src.GetItemByID(srcItemId); if (!itemNullable.HasValue) return; var item = itemNullable.Value; // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref if (item.Content == null) MyLog.Default.WriteLine("item.Content == null"); if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId())) return; var transferAmount = amount ?? item.Amount; if (dst == null) { src.RemoveItems(srcItemId, amount, true, false); return; } //TransferItemsInternal(src, dst, srcItemId, false, dstIdx, transferAmount); // CH: TODO: Remove if date > 15.5.2016 :-) It's only to catch a nullref for (int i = 0; i < dst.Owner.InventoryCount; i++) { if (dst.Owner.GetInventory(i) == null) MyLog.Default.WriteLine("dst.Owner.GetInventory(i) == null"); } byte inventoryIndex = 0; for (byte i = 0; i < dst.Owner.InventoryCount; i++) { if (dst.Owner.GetInventory(i).Equals(dst)) { inventoryIndex = i; break; } } MyMultiplayer.RaiseEvent(src, x => x.InventoryTransferItem_Implementation, transferAmount, srcItemId, dst.Owner.EntityId, inventoryIndex, dstIdx); }
private static MyFixedPoint TransferOrRemove(MyInventory src, MyFixedPoint? amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false, bool onlyWhole = true) { MyFixedPoint removedAmount = 0; if (!onlyWhole) { amount = MyFixedPoint.Min(amount.Value, src.GetItemAmount(contentId, flags)); } //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (!onlyWhole || src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. // If the requested item is an oxygen container, do a preliminary loop to pull any non-full items first. if (contentId.TypeId == typeof(MyObjectBuilder_OxygenContainerObject) || contentId.TypeId == typeof(MyObjectBuilder_GasContainerObject)) { int k = 0; while (k < src.m_items.Count) { if (!transferAll && remainingAmount == 0) break; MyPhysicalInventoryItem item = src.m_items[k]; // Skip full oxygen bottles in this loop. They will not be skipped in the next one. var oxygenBottle = item.Content as MyObjectBuilder_GasContainerObject; if (oxygenBottle != null && oxygenBottle.GasLevel == 1f) { k++; continue; } if (item.Content.GetObjectId() != contentId) { k++; continue; } if (transferAll || remainingAmount >= item.Amount) { removedAmount += item.Amount; remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { removedAmount += item.Amount; Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } // End of oxygen specific code int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) break; MyPhysicalInventoryItem item = src.m_items[i]; var objectId = item.Content.GetId(); if (objectId != contentId && item.Content.TypeId == typeof(MyObjectBuilder_BlockItem)) { //objectId = MyDefinitionManager.Static.GetComponentId(item.Content.GetObjectId()); objectId = item.Content.GetObjectId(); } if (objectId != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { removedAmount += item.Amount; remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { removedAmount += remainingAmount; Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } return removedAmount; }
private static void TransferOrRemove(MyInventory src, MyFixedPoint? amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false) { //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) break; MyInventoryItem item = src.m_items[i]; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject; if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f) { i++; continue; } // End of oxygen specific code if (item.Content.GetObjectId() != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } }
private void inventory_OnContentsChanged(MyInventory obj) { RefreshInventoryContents(); if (InventoryContentsChanged != null) InventoryContentsChanged(this); }
private static void FixTransferAmount(MyInventory src, MyInventory dst, MyPhysicalInventoryItem? srcItem, bool spawn, ref MyFixedPoint remove, ref MyFixedPoint add) { Debug.Assert(Sync.IsServer); if (srcItem.Value.Amount < remove) { remove = srcItem.Value.Amount; add = remove; } if (!MySession.Static.CreativeMode && src != dst) { MyFixedPoint space = dst.ComputeAmountThatFits(srcItem.Value.Content.GetObjectId()); if (space < remove) { if (spawn) { MyEntity e = (dst.Owner as MyEntity); Matrix m = e.WorldMatrix; MyFloatingObjects.Spawn(new MyPhysicalInventoryItem(remove - space, srcItem.Value.Content), e.PositionComp.GetPosition() + m.Forward + m.Up, m.Forward, m.Up, e.Physics); } else { remove = space; } add = space; } } }
public static void Transfer(MyInventory src, MyInventory dst, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyFixedPoint?amount = null, bool spawn = false) { TransferOrRemove(src, amount, contentId, flags, dst); }
// This is not nice handling, but I didn't cleaner way.. private void OnContentsChanged(MyInventory obj) { if (ComponentContentsChanged != null) ComponentContentsChanged(this); }
public void SetInventory(MyInventory inventory, int index) { }
//this is from client only public static void TransferByUser(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null) { var itemNullable = src.GetItemByID(srcItemId); if (!itemNullable.HasValue) return; var item = itemNullable.Value; if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId())) return; var transferAmount = amount ?? item.Amount; if (dst == null) { src.RemoveItems(srcItemId, amount, true, false); return; } //TransferItemsInternal(src, dst, srcItemId, false, dstIdx, transferAmount); byte inventoryIndex = 0; for (byte i = 0; i < dst.Owner.InventoryCount; i++) { if (dst.Owner.GetInventory(i).Equals(dst)) { inventoryIndex = i; break; } } MyMultiplayer.RaiseEvent(src, x => x.InventoryTransferItem_Implementation, transferAmount, srcItemId, dst.Owner.EntityId, inventoryIndex, dstIdx); }
public static void TransferItemsInternal(MyInventory src, MyInventory dst, uint itemId, bool spawn, int destItemIndex, MyFixedPoint amount) { Debug.Assert(Sync.IsServer); MyFixedPoint remove = amount; var srcItem = src.GetItemByID(itemId); if (!srcItem.HasValue) return; FixTransferAmount(src, dst, srcItem, spawn, ref remove, ref amount); if (amount != 0) { if (dst.AddItems(amount, srcItem.Value.Content, destItemIndex)) { if (remove != 0) src.RemoveItems(itemId, remove); } } }
public static void Transfer(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint? amount = null, bool spawn = false) { var itemNullable = src.GetItemByID(srcItemId); if (!itemNullable.HasValue) return; var item = itemNullable.Value; if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId())) return; if (Sync.IsServer) { var transferAmount = amount ?? item.Amount; if (dst == null) { src.RemoveItems(srcItemId, amount, true, spawn); return; } TransferItemsInternal(src, dst, srcItemId, spawn, dstIdx, transferAmount); } }
private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false) { //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. // If the requested item is an oxygen container, do a preliminary loop to pull any non-full items first. if (contentId.TypeId == typeof(MyObjectBuilder_OxygenContainerObject)) { int k = 0; while (k < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyPhysicalInventoryItem item = src.m_items[k]; // Skip full oxygen bottles in this loop. They will not be skipped in the next one. var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject; if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f) { k++; continue; } if (item.Content.GetObjectId() != contentId) { k++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } // End of oxygen specific code int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyPhysicalInventoryItem item = src.m_items[i]; if (item.Content.GetObjectId() != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } }
private static void TransferItemsInternal(MyInventory src, MyInventory dst, uint srcItemId, bool spawn, int destItemIndex, MyFixedPoint amount) { Debug.Assert(Sync.IsServer); MyFixedPoint remove = amount; MyPhysicalInventoryItem srcItem = default(MyPhysicalInventoryItem); int srcIndex = -1; for (int i = 0; i < src.m_items.Count; ++i) { if (src.m_items[i].ItemId == srcItemId) { srcIndex = i; srcItem = src.m_items[i]; break; } } if (srcIndex == -1) return; FixTransferAmount(src, dst, srcItem, spawn, ref remove, ref amount); if (amount != 0) { if (src == dst && destItemIndex >= 0 && destItemIndex < dst.m_items.Count && !dst.m_items[destItemIndex].Content.CanStack(srcItem.Content)) { dst.SwapItems(srcIndex, destItemIndex); } else { dst.AddItemsInternal(amount, srcItem.Content, dst == src && remove == 0 ? srcItemId : (uint?)null, destItemIndex); if (remove != 0) src.RemoveItems(srcItemId, remove); } } }
public InventoryItemWrapper( MyPhysicalInventoryItem item, MyInventory inventory ) { Item = item; Inventory = inventory; }
private bool IsConnected(MyInventory dstInventory) { var srcConveyor = (this.Owner as IMyConveyorEndpointBlock); if (srcConveyor != null) { reachableVertices.Clear(); MyGridConveyorSystem.FindReachable(srcConveyor.ConveyorEndpoint, reachableVertices, (vertex) => vertex.CubeBlock != null); foreach (var vertex in reachableVertices) { if (dstInventory.Owner == vertex.CubeBlock) { return true; } } } return false; }
public static MyFixedPoint Transfer(MyInventory src, MyInventory dst, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyFixedPoint? amount = null, bool spawn = false) { return TransferOrRemove(src, amount, contentId, flags, dst); }
private MyGuiControlLabel MakeMassLabel(MyInventory inventory) { var label = MakeLabel(MySpaceTexts.ScreenTerminalInventory_Mass); label.Name = "MassLabel"; return label; }