private static double TryAddToInventory(MyInventory inventory, double amount, MyObjectBuilder_Base newObject, MyDefinitionId itemId) { var remaining = 0D; if (amount > 1) { for (var i = 0; i < amount; i++) { if (!inventory.CanItemsBeAdded(1, itemId) || !inventory.AddItems(1, newObject)) { remaining = amount - i; break; } } } else { if (!inventory.CanItemsBeAdded(1, itemId) || !inventory.AddItems((MyFixedPoint)amount, newObject)) { remaining = amount; } } return(remaining); }
private void TransferFromItem(MyObjectBuilder_PhysicalObject item, int count) { MyInventory targetInventory = ((MyCubeBlock)m_constructionBlock.ConstructionBlock).GetInventory(); if (targetInventory.CanItemsBeAdded(count, item.GetId())) { targetInventory.AddItems(count, item); return; } var inventoryItem = new MyPhysicalInventoryItem(count, item); MyFloatingObjects.Spawn(inventoryItem, Vector3D.Transform(m_targetBlock.Position * m_targetBlock.CubeGrid.GridSize, m_targetBlock.CubeGrid.WorldMatrix), m_targetBlock.CubeGrid.WorldMatrix.Forward, m_targetBlock.CubeGrid.WorldMatrix.Up); }
public bool AddItems(MyInventory inventory, MyObjectBuilder_PhysicalObject obj, bool overrideCheck, MyFixedPoint amount) { if (overrideCheck || !inventory.ContainItems(amount, obj)) { if (inventory.CanItemsBeAdded(amount, obj.GetId())) { inventory.AddItems(amount, obj); return(true); } else { return(false); } } else { return(false); } }
public static void AddItemsToInventory(MyInventory inventory, MyDefinitionId itemId, float amount = -1) { var itemDef = GetItemDefinition(itemId); if (itemDef == null) { return; } float freeSpace = (float)(inventory.MaxVolume - inventory.CurrentVolume); var amountToAdd = Math.Floor(freeSpace / itemDef.Volume); if (amountToAdd > amount && amount > -1) { var adjustedAmt = amountToAdd - amount; amountToAdd = adjustedAmt; } if (amountToAdd > 0 && inventory.CanItemsBeAdded((MyFixedPoint)amountToAdd, itemId) == true) { inventory.AddItems((MyFixedPoint)amountToAdd, MyObjectBuilderSerializer.CreateNewObject(itemId)); } }
private bool CheckInventoryCapacity(MyInventory inventory, MyBlueprintDefinitionBase.Item item, MyFixedPoint amountMultiplier) { return inventory.CanItemsBeAdded(item.Amount * amountMultiplier, item.Id); }
public bool AddItems(MyInventory inventory, MyObjectBuilder_PhysicalObject obj, bool overrideCheck, MyFixedPoint amount) { if (overrideCheck || !inventory.ContainItems(amount, obj)) { if (inventory.CanItemsBeAdded(amount, obj.GetId())) { inventory.AddItems(amount, obj); return true; } else { return false; } } else { return false; } }
private bool CheckInventoryCapacity(MyInventory inventory, MyBlueprintDefinitionBase.Item item, MyFixedPoint amountMultiplier) { return(inventory.CanItemsBeAdded(item.Amount * amountMultiplier, item.Id)); }
private void TransferFromTarget(NaniteMiningItem target) { // Must be invoked from game thread IMyEntity entity; if (!MyAPIGateway.Entities.TryGetEntityById(target.VoxelId, out entity)) { CancelTarget(target); return; } MyAPIGateway.Parallel.Start(() => { try { if (entity == null || !IsValidVoxelTarget(target, entity)) { MyAPIGateway.Utilities.InvokeOnGameThread(() => { CancelTarget(target); }); return; } MyAPIGateway.Utilities.InvokeOnGameThread(() => { // Invocation 0 try { var def = MyDefinitionManager.Static.GetVoxelMaterialDefinition(target.VoxelMaterial); var item = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_Ore>(def.MinedOre); var inventory = ((MyCubeBlock)m_constructionBlock.ConstructionBlock).GetInventory(); MyInventory targetInventory = ((MyCubeBlock)m_constructionBlock.ConstructionBlock).GetInventory(); if (targetInventory != null && targetInventory.CanItemsBeAdded((MyFixedPoint)(target.Amount), item.GetId())) { if (entity == null) { CancelTarget(target); return; } var ownerName = targetInventory.Owner as IMyTerminalBlock; if (ownerName != null) { Logging.Instance.WriteLine($"[Mining] Transfer - Adding {target.Amount} {item.GetId().SubtypeName} to {ownerName.CustomName}", 1); } if (!targetInventory.AddItems((MyFixedPoint)(target.Amount), item)) { Logging.Instance.WriteLine($"Error while transferring {target.Amount} {item.GetId().SubtypeName}! Aborting mining operation."); return; } IMyVoxelBase voxel = entity as IMyVoxelBase; MyVoxelBase voxelBase = voxel as MyVoxelBase; voxelBase.RequestVoxelOperationSphere(target.Position, 1f, target.VoxelMaterial, MyVoxelBase.OperationType.Cut); AddMinedPosition(target); CompleteTarget(target); return; } Logging.Instance.WriteLine("[Mining] Mined materials could not be moved. No free cargo space (probably)!", 1); CancelTarget(target); } catch (Exception e) { Logging.Instance.WriteLine($"Exception in NaniteMiningTargets.TransferFromTarget (Invocation 0):\n{e}"); } }); } catch (Exception e) { Logging.Instance.WriteLine($"Exception in NaniteMiningTargets.TransferFromTarget:\n{e}"); } }); }
public static bool FindFreeCargo(MyCubeBlock startBlock, MyObjectBuilder_Base item, int count, bool order = true) { var list = Conveyor.GetConveyorListFromEntity(startBlock); if (list == null) { Logging.Instance.WriteLine(string.Format("Conveyor list is null!")); return(false); } if (!list.Contains(startBlock.EntityId)) { list.Add(startBlock.EntityId); } List <MyInventory> inventoryList = new List <MyInventory>(); foreach (var inventoryItem in list) { IMyEntity entity; if (MyAPIGateway.Entities.TryGetEntityById(inventoryItem, out entity)) { if (!(entity is IMyCubeBlock)) { continue; } if (entity is Ingame.IMyRefinery || entity is Ingame.IMyAssembler) { continue; } MyCubeBlock block = (MyCubeBlock)entity; if (!block.HasInventory) { continue; } if (block.EntityId == startBlock.EntityId) { inventoryList.Insert(0, block.GetInventory()); } else { inventoryList.Add(block.GetInventory()); } } } MyInventory targetInventory = null; List <MyInventory> modifiedList; if (order) { modifiedList = inventoryList.OrderByDescending(x => (float)x.MaxVolume - (float)x.CurrentVolume).ToList(); } else { modifiedList = inventoryList; } foreach (var inventoryItem in modifiedList) { targetInventory = inventoryItem; if (targetInventory.CanItemsBeAdded(count, item.GetId())) { var ownerName = targetInventory.Owner as IMyTerminalBlock; if (ownerName != null) { Logging.Instance.WriteLine(string.Format("TRANSFER Adding {0} {1} to {2}", count, item.GetId().SubtypeName, ownerName.CustomName)); } targetInventory.AddItems(count, item); return(true); } } return(false); }
private void DoWork() { try { Vector3D velocity = Vector3D.Zero; var grid = (Container.Entity as IMyCubeBlock).CubeGrid; var entity = (Container.Entity as IMyFunctionalBlock); MyFixedPoint amount = 0; m_speed = 0; if (grid != null && entity != null && grid.Physics != null) { velocity = grid.Physics.LinearVelocity; var rotation = entity.WorldMatrix.GetDirectionVector(Base6Directions.Direction.Forward); var start = entity.GetPosition() + (entity.WorldAABB.Size.Z / 2 * rotation); var end = start + (100 * rotation); if ((Container.Entity as IMyCubeBlock).CubeGrid.RayCastBlocks(start, end).HasValue) { m_state = RamscoopState.Blocked; } else if (!Vector3D.IsZero(velocity)) { m_state = RamscoopState.Collecting; m_speed = velocity.Length(); var rotdot = Vector3D.Dot(velocity, rotation); var lens = velocity.Length() * rotation.Length(); var cos_theta = (rotdot / lens); cos_theta += 1.0f; // positive offset, facing reverse will be 0. m_cosTheta = cos_theta / 2.0d; m_amountAdded = amount = m_amount * (MyFixedPoint)m_cosTheta * (MyFixedPoint)m_speed * (MyFixedPoint)m_sizeFactor; } } if (Sync.IsServer && m_inventory.CanItemsBeAdded(amount, m_definitionId)) { var content = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(m_definitionId); if (content != null) { MyAPIGateway.Utilities.InvokeOnGameThread(() => { try { if (content != null) { m_inventory.AddItems(amount, content); } } catch (Exception e) { Debug.HandleException(e); } }); } } } catch (Exception e) { Debug.HandleException(e); } }