/// <summary> /// As long as ComputeAmountThatFits is not available for modding we have to try /// </summary> public static VRage.MyFixedPoint MaxItemsAddable(this IMyInventory destInventory, VRage.MyFixedPoint maxNeeded, VRage.ObjectBuilders.SerializableDefinitionId contentId) { if (destInventory.CanItemsBeAdded(maxNeeded, contentId)) { return(maxNeeded); } int maxPossible = 0; int currentStep = Math.Max((int)maxNeeded / 2, 1); int currentTry = 0; while (currentStep > 0) { currentTry = maxPossible + currentStep; if (destInventory.CanItemsBeAdded(currentTry, contentId)) { maxPossible = currentTry; } else { if (currentStep <= 1) { break; } } if (currentStep > 1) { currentStep = currentStep / 2; } } return(maxPossible); }
/// <summary> /// As long as ComputeAmountThatFits is not available for modding we have to try /// </summary> public static VRage.MyFixedPoint MaxItemsAddable(this IMyInventory destInventory, VRage.MyFixedPoint maxNeeded, MyItemType itemType) { if (destInventory.CanItemsBeAdded(maxNeeded, itemType)) { return(maxNeeded); } int maxPossible = 0; int currentStep = Math.Max((int)maxNeeded / 2, 1); int currentTry = 0; while (currentStep > 0) { currentTry = maxPossible + currentStep; if (destInventory.CanItemsBeAdded(currentTry, itemType)) { maxPossible = currentTry; } else { if (currentStep <= 1) { break; } } if (currentStep > 1) { currentStep = currentStep / 2; } } return(maxPossible); }
/** * Locates all the Oxygen generator class blocks on the given grid, * then adds 2000 ice if there is room to do so. * * Only call on server. */ public static void FillOxygenGenerators(IMyCubeGrid targetGrid) { List <IMySlimBlock> blockList = new List <IMySlimBlock>(); targetGrid.GetBlocks(blockList, b => b.FatBlock is Sandbox.ModAPI.IMyGasGenerator); foreach (var block in blockList) { IMyGasGenerator gasGenerator = ((IMyGasGenerator)block.FatBlock); if (gasGenerator == null) { continue; } MyDefinitionId definitionId = new MyDefinitionId(typeof(MyObjectBuilder_Ore), "Ice"); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = AMOUNT_ICE_TO_ADD, Content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(definitionId) }; IMyInventory gasGeneratorInv = gasGenerator.GetInventory(0); if (!gasGeneratorInv.CanItemsBeAdded(AMOUNT_ICE_TO_ADD, definitionId)) { continue; } gasGeneratorInv.AddItems(AMOUNT_ICE_TO_ADD, inventoryItem.Content); } }
/// <summary> /// As long as ComputeAmountThatFits is not available for modding we have to try /// </summary> public static VRage.MyFixedPoint MaxFractionItemsAddable(this IMyInventory destInventory, VRage.MyFixedPoint maxNeeded, MyItemType itemType) { if (destInventory.CanItemsBeAdded(maxNeeded, itemType)) { return(maxNeeded); } VRage.MyFixedPoint maxPossible = 0; VRage.MyFixedPoint currentStep = (VRage.MyFixedPoint)((float)maxNeeded / 2); VRage.MyFixedPoint currentTry = 0; while (currentStep > VRage.MyFixedPoint.SmallestPossibleValue) { currentTry = maxPossible + currentStep; if (destInventory.CanItemsBeAdded(currentTry, itemType)) { maxPossible = currentTry; } currentStep = (VRage.MyFixedPoint)((float)currentStep / 2); } return(maxPossible); }
private void addtoParachute(IMyParachute FoundCargoCont, SerializableDefinitionId item, int amount) { IMyInventory inventory = ((Sandbox.ModAPI.Ingame.IMyTerminalBlock)FoundCargoCont).GetInventory(0) as IMyInventory; if (!inventory.CanItemsBeAdded(amount, item)) { return; } MyFixedPoint amount2 = (MyFixedPoint)Math.Min(amount, 9999); inventory.AddItems(amount2, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(item)); }
public bool CanTransferItemTo(IMyInventory otherInventory, MyItemType itemType) { if (!ContainItems(Value1, itemType)) { return(false); } if (!IsConnectedTo(otherInventory)) { return(false); } return(otherInventory.CanItemsBeAdded(Value1, itemType)); }
public static bool InventoryAdd(IMyInventory inventory, MyFixedPoint amount, MyDefinitionId definitionId) { var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(definitionId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, PhysicalContent = content }; if (inventory.CanItemsBeAdded(inventoryItem.Amount, definitionId)) { inventory.AddItems(inventoryItem.Amount, inventoryItem.PhysicalContent, -1); return(true); } // Inventory full. Could not add the item. return(false); }
public static bool FixUnfinishedBlock(IMyInventory inv, IMySlimBlock slimBlock, long owner) { bool success = true; Dictionary <string, int> missingParts = new Dictionary <string, int>(); slimBlock.GetMissingComponents(missingParts); if (missingParts.Keys.Count == 0) { return(success); } foreach (var part in missingParts.Keys.ToList()) { MyDefinitionId defId = new MyDefinitionId(typeof(MyObjectBuilder_Component), part); var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(defId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = 1, Content = content }; while (missingParts[part] > 0) { if (inv.CanItemsBeAdded(1, defId) == true) { inv.AddItems(1, inventoryItem.Content); missingParts[part]--; } else { //Logger.AddMsg("Failed To Add Repair Component To Container", true); success = false; break; } } slimBlock.MoveItemsToConstructionStockpile(inv); slimBlock.IncreaseMountLevel(10000, owner, inv); } return(success); }
private static double AddToInventory(IMyInventory inventory, MyDefinitionId itemDefinition, double amount) { var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(itemDefinition); MyObjectBuilder_InventoryItem item = new MyObjectBuilder_InventoryItem { Amount = new MyFixedPoint { RawValue = (long)(amount * _multi) }, PhysicalContent = content }; if (!inventory.CanItemsBeAdded(item.Amount, itemDefinition)) { return(0); } inventory.AddItems(item.Amount, item.PhysicalContent); return(amount); }
public static bool InventoryAdd(IMyInventory inventory, MyFixedPoint amount, MyDefinitionId definitionId) { var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(definitionId); var gasContainer = content as MyObjectBuilder_GasContainerObject; if (gasContainer != null) { gasContainer.GasLevel = 1f; } MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, Content = content }; if (inventory.CanItemsBeAdded(inventoryItem.Amount, definitionId)) { inventory.AddItems(inventoryItem.Amount, (MyObjectBuilder_PhysicalObject)inventoryItem.Content, -1); return(true); } // Inventory full. Could not add the item. return(false); }
public float TryTransferTo(IMyInventory TargetInventory, MyItemType ItemDef, float TargetAmount, IList <IMyInventory> SourceInventories) { if (TargetInventory == null) { throw new ArgumentNullException(nameof(TargetInventory)); } if (SourceInventories == null) { throw new ArgumentNullException(nameof(SourceInventories)); } if (SourceInventories.Count == 0) { return(0); } float YetToTransfer = TargetAmount; List <MyInventoryItem> Items = new List <MyInventoryItem>(40); foreach (IMyInventory SourceInventory in SourceInventories) { if (!SourceInventory.Owner.HasInventory) { continue; } if (SourceInventory.Owner is IMyTerminalBlock) { IMyTerminalBlock SourceBlock = SourceInventory.Owner as IMyTerminalBlock; if (!SourceBlock.IsFunctional) { continue; } if (!SourceBlock.HasPlayerAccess(MyKernel.Tool.OwnerId)) { continue; } } Items.Clear(); if (Items.Capacity < 40) { Items.Capacity = 40; } SourceInventory.GetItems(Items); foreach (MyInventoryItem Item in Items) { MyItemType def = Item.Type; float amount = (float)Item.Amount; if (Item.Type != ItemDef) { continue; } float pullAmount = Math.Min(amount, YetToTransfer); if (TargetInventory.CanItemsBeAdded((VRage.MyFixedPoint)pullAmount, Item.Type)) { bool pull = SourceInventory.TransferItemTo(TargetInventory, Items.IndexOf(Item), amount: (VRage.MyFixedPoint)pullAmount); if (pull) { YetToTransfer -= pullAmount; if (YetToTransfer <= 0) { return(TargetAmount); } } } else { float FittableAmount = (float)(TargetInventory as Sandbox.Game.MyInventory).ComputeAmountThatFits(Item.Type); if (FittableAmount > pullAmount) { FittableAmount = pullAmount; } bool pull = SourceInventory.TransferItemTo(TargetInventory, Items.IndexOf(Item), amount: (VRage.MyFixedPoint)FittableAmount); if (pull) { YetToTransfer -= (float)FittableAmount; return(TargetAmount - YetToTransfer); } } } } return(TargetAmount - YetToTransfer); }
internal void TakeRequiredComponents(MyEntity inventoryOwner) { if (MyAPIGateway.Session.CreativeMode) { return; } if (!inventoryOwner.HasInventory) { return; } // Ignore reactors if (inventoryOwner is Sandbox.ModAPI.Ingame.IMyReactor) { return; } if (inventoryOwner is IMyCubeBlock && ((IMyCubeBlock)inventoryOwner).BlockDefinition.SubtypeName.Contains("Nanite")) { return; } int inventoryIndex = inventoryOwner.InventoryCount - 1; //if (inventoryOwner is Sandbox.ModAPI.Ingame.IMyAssembler) // inventoryIndex = 1; IMyInventory constructionInventory = GetConstructionInventory(); IMyInventory inventory = (IMyInventory)inventoryOwner.GetInventoryBase(inventoryIndex); if (constructionInventory == null || inventory == null) { return; } if (((Sandbox.Game.MyInventory)inventory).GetItemsCount() < 1) { return; } //if (!constructionInventory.IsConnectedTo(inventory)) // return; IMyTerminalBlock terminalOwner = inventoryOwner as IMyTerminalBlock; MyRelationsBetweenPlayerAndBlock relation = ((IMyTerminalBlock)m_constructionBlock).GetUserRelationToOwner(terminalOwner.OwnerId); if (relation == MyRelationsBetweenPlayerAndBlock.Enemies) { return; } foreach (var inventoryItem in inventory.GetItems().ToList()) { foreach (var componentNeeded in ComponentsRequired.ToList()) { if (inventoryItem.Content.TypeId != typeof(MyObjectBuilder_Component)) { continue; } if (componentNeeded.Value <= 0) { continue; } if ((int)inventoryItem.Amount <= 0f) { continue; } if (inventoryItem.Content.SubtypeName == componentNeeded.Key) { if (inventoryItem.Amount >= componentNeeded.Value) { var validAmount = GetMaxComponentAmount(componentNeeded.Key, (float)constructionInventory.MaxVolume - (float)constructionInventory.CurrentVolume); var amount = Math.Min(componentNeeded.Value, validAmount); if (!constructionInventory.CanItemsBeAdded((int)amount, new SerializableDefinitionId(typeof(MyObjectBuilder_Component), componentNeeded.Key))) { continue; } inventory.RemoveItemsOfType((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); constructionInventory.AddItems((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); ComponentsRequired[componentNeeded.Key] -= (int)amount; } else { var validAmount = GetMaxComponentAmount(componentNeeded.Key, (float)constructionInventory.MaxVolume - (float)constructionInventory.CurrentVolume); var amount = Math.Min((float)inventoryItem.Amount, validAmount); if (!constructionInventory.CanItemsBeAdded((int)amount, new SerializableDefinitionId(typeof(MyObjectBuilder_Component), componentNeeded.Key))) { continue; } inventory.RemoveItemsOfType((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); constructionInventory.AddItems((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); ComponentsRequired[componentNeeded.Key] -= (int)amount; } continue; } } } }
internal void TakeRequiredComponents() { if (MyAPIGateway.Session.CreativeMode || ComponentsRequired.Count < 1) { return; } List <IMyInventory> removalList = new List <IMyInventory>(); try { foreach (IMyInventory inventory in connectedInventory) { IMyInventory inv = null; IMyInventory constructionInventory = GetConstructionInventory(); if (inventory == null || inventory.CurrentVolume == inventory.MaxVolume) { continue; } if (!GridHelper.IsValidInventoryConnection(constructionInventory, inventory, out inv)) { removalList.Add(inventory); continue; } foreach (var inventoryItem in inventory.GetItems().ToList()) { foreach (var componentNeeded in ComponentsRequired.ToList()) { if (inventoryItem.Content.TypeId != typeof(MyObjectBuilder_Component) || componentNeeded.Value <= 0 || (int)inventoryItem.Amount <= 0f || inventoryItem.Content.SubtypeName != componentNeeded.Key) { continue; } var validAmount = GetMaxComponentAmount(componentNeeded.Key, (float)constructionInventory.MaxVolume - (float)constructionInventory.CurrentVolume); float amount; if (inventoryItem.Amount >= componentNeeded.Value) { amount = Math.Min(componentNeeded.Value, validAmount); } else { amount = Math.Min((float)inventoryItem.Amount, validAmount); } if (!constructionInventory.CanItemsBeAdded((int)amount, new SerializableDefinitionId(typeof(MyObjectBuilder_Component), componentNeeded.Key))) { continue; } MyAPIGateway.Utilities.InvokeOnGameThread(() => { try { inventory.RemoveItemsOfType((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); constructionInventory.AddItems((int)amount, (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_Component), componentNeeded.Key)); if (ComponentsRequired.ContainsKey(componentNeeded.Key)) { ComponentsRequired[componentNeeded.Key] -= (int)amount; } } catch (Exception ex) { Logging.Instance.WriteLine($"Nanite Control Factory: Exception in NaniteConstructionInventory.TakeRequiredComponents:\n{ex.ToString()}"); } }); } } } } catch (InvalidOperationException ex) { Logging.Instance.WriteLine("NaniteConstructionSystem.Extensions.GridHelper.TryMoveToFreeCargo: A list was modified. Aborting.", 1); } catch (Exception ex) when(ex.ToString().Contains("IndexOutOfRangeException")) //because Keen thinks we shouldn't have access to this exception ... { Logging.Instance.WriteLine("NaniteConstructionSystem.Extensions.GridHelper.TryMoveToFreeCargo: A list was modified. Aborting.", 1); } foreach (IMyInventory inv in removalList) { MyAPIGateway.Utilities.InvokeOnGameThread(() => { connectedInventory.Remove(inv); }); } }
public static bool InventoryAdd(IMyInventory inventory, MyFixedPoint amount, MyDefinitionId definitionId) { var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(definitionId); var gasContainer = content as MyObjectBuilder_GasContainerObject; if (gasContainer != null) gasContainer.GasLevel = 1f; MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, Content = content }; if (inventory.CanItemsBeAdded(inventoryItem.Amount, definitionId)) { inventory.AddItems(inventoryItem.Amount, (MyObjectBuilder_PhysicalObject)inventoryItem.Content, -1); return true; } // Inventory full. Could not add the item. return false; }