public MyPhysicalInventoryItem(MyObjectBuilder_InventoryItem item) { Debug.Assert(item.Amount > 0, "Creating inventory item with zero amount!"); ItemId = 0; Amount = item.Amount; Scale = item.Scale; Content = item.PhysicalContent; }
public MyPhysicalInventoryItem(MyFixedPoint amount, MyObjectBuilder_PhysicalObject content, float scale = 1) { Debug.Assert(amount > 0, "Creating inventory item with zero amount!"); ItemId = 0; Amount = amount; Scale = scale; Content = content; }
//TODO: This should be deprecated, we shoud add IMyInventoryItems objects only , instead of items based on their objectbuilder /// <summary> /// Adds item to inventory /// </summary> /// <param name="amount"></param> /// <param name="objectBuilder"></param> /// <param name="index"></param> /// <returns>true if items were added, false if items didn't fit</returns> public abstract bool AddItems(MyFixedPoint amount, MyObjectBuilder_Base objectBuilder);
internal IndexedBlueprint(MyBlueprintDefinitionBase def, MyDefinitionId result, MyFixedPoint divider, bool reverse) { Result = result; Ingredients = new Dictionary <MyDefinitionId, MyFixedPoint>(MyDefinitionId.Comparer); ConsumptionRecipe = reverse; if (!reverse) { foreach (var req in def.Prerequisites) { Ingredients[req.Id] = (MyFixedPoint)((double)req.Amount / (double)divider); } } else { foreach (var req in def.Results) { Ingredients[req.Id] = (MyFixedPoint)((double)req.Amount / (double)divider); } } }
public MyFixedPoint batch() { return(MyFixedPoint.Min(batchSize, fromLong(toLong(amount) / 4))); }
public ItemInfo(MyInventoryItem item) { TypeId = item.Type.TypeId; SubtypeId = item.Type.SubtypeId; Amount = item.Amount; }
public static MyEntity Spawn(this MyPhysicalInventoryItem thisItem, MyFixedPoint amount, MatrixD worldMatrix, MyEntity owner = null) { if (amount < 0) { return(null); } if (thisItem.Content == null) { Debug.Fail("Can not spawn item with null content!"); return(null); } if (thisItem.Content is MyObjectBuilder_BlockItem) { Debug.Assert(MyFixedPoint.IsIntegral(amount), "Spawning fractional number of grids!"); bool isBlock = typeof(MyObjectBuilder_CubeBlock).IsAssignableFrom(thisItem.Content.GetObjectId().TypeId); Debug.Assert(isBlock, "Block item does not contain block!?!?@&*#%!"); if (!isBlock) { return(null); } var blockItem = thisItem.Content as MyObjectBuilder_BlockItem; MyCubeBlockDefinition blockDefinition; MyDefinitionManager.Static.TryGetCubeBlockDefinition(blockItem.BlockDefId, out blockDefinition); Debug.Assert(blockDefinition != null, "Block definition not found"); if (blockDefinition == null) { return(null); } var builder = MyObjectBuilderSerializer.CreateNewObject(typeof(MyObjectBuilder_CubeGrid)) as MyObjectBuilder_CubeGrid; builder.GridSizeEnum = blockDefinition.CubeSize; builder.IsStatic = false; builder.PersistentFlags |= MyPersistentEntityFlags2.InScene | MyPersistentEntityFlags2.Enabled; builder.PositionAndOrientation = new MyPositionAndOrientation(worldMatrix); var block = MyObjectBuilderSerializer.CreateNewObject(blockItem.BlockDefId) as MyObjectBuilder_CubeBlock; System.Diagnostics.Debug.Assert(block != null, "Block couldn't been created, maybe wrong definition id? DefID: " + blockItem.BlockDefId); if (block != null) { block.Min = blockDefinition.Size / 2 - blockDefinition.Size + Vector3I.One; builder.CubeBlocks.Add(block); MyCubeGrid firstGrid = null; for (int i = 0; i < amount; ++i) { builder.EntityId = MyEntityIdentifier.AllocateId(); block.EntityId = MyEntityIdentifier.AllocateId(); MyCubeGrid newGrid = MyEntities.CreateFromObjectBuilder(builder) as MyCubeGrid; firstGrid = firstGrid ?? newGrid; MyEntities.Add(newGrid); } return(firstGrid); } return(null); } else { MyPhysicalItemDefinition itemDefinition = null; if (MyDefinitionManager.Static.TryGetPhysicalItemDefinition(thisItem.Content.GetObjectId(), out itemDefinition)) { return(MyFloatingObjects.Spawn(new MyPhysicalInventoryItem(amount, thisItem.Content), worldMatrix, owner != null ? owner.Physics : null)); } return(null); } }
public static decimal ConvertFromRawQuantity(MyFixedPoint quantity) { return((decimal)quantity.RawValue / 1000000); }
public abstract bool ItemsCanBeRemoved(MyFixedPoint amount, IMyInventoryItem item);
/// <summary> /// Transfers safely given item from inventory given as parameter to this instance. /// </summary> /// <returns>true if items were succesfully transfered, otherwise, false</returns> public abstract bool TransferItemsFrom(MyInventoryBase sourceInventory, IMyInventoryItem item, MyFixedPoint amount);
/// <summary> /// Remove items of a given amount and definition /// </summary> /// <param name="amount">amount ot remove</param> /// <param name="contentId">definition id of items to be removed</param> /// <param name="spawn">Set tru to spawn object in the world, after it was removed</param> /// <returns>Returns the actually removed amount</returns> public abstract MyFixedPoint RemoveItemsOfType(MyFixedPoint amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, bool spawn = false);
private void ChangeRequirementsToResults(MyBlueprintDefinitionBase queueItem, MyFixedPoint blueprintAmount) { Debug.Assert(Sync.IsServer); if (!MySession.Static.CreativeMode) { blueprintAmount = MyFixedPoint.Min(OutputInventory.ComputeAmountThatFits(queueItem), blueprintAmount); } if (blueprintAmount == 0) { return; } foreach (var prerequisite in queueItem.Prerequisites) { var obPrerequisite = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(prerequisite.Id); var prerequisiteAmount = blueprintAmount * prerequisite.Amount; InputInventory.RemoveItemsOfType(prerequisiteAmount, obPrerequisite); } foreach (var result in queueItem.Results) { var resultId = result.Id; var obResult = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(resultId); var conversionRatio = result.Amount * m_refineryDef.MaterialEfficiency * UpgradeValues["Effectiveness"]; if (conversionRatio > (MyFixedPoint)1.0f) { conversionRatio = (MyFixedPoint)1.0f; } var resultAmount = blueprintAmount * conversionRatio; OutputInventory.AddItems(resultAmount, obResult); } RemoveFirstQueueItemAnnounce(blueprintAmount); }
private void RebuildQueue() { Debug.Assert(Sync.IsServer || !MyFakes.ENABLE_PRODUCTION_SYNC); m_queueNeedsRebuild = false; ClearQueue(false); InitializeInventoryCounts(inputInventory: true); // Find all blueprints that contain as a prerequisite any item from the input inventory and sort them by the input inventory // index of the first item found. m_tmpSortedBlueprints.Clear(); var inputItems = InputInventory.GetItems(); for (int i = 0; i < m_refineryDef.BlueprintClasses.Count; ++i) { foreach (var blueprint in m_refineryDef.BlueprintClasses[i]) { int firstRequirementIndex = 0; bool found = false; while (firstRequirementIndex < inputItems.Count) { MyDefinitionId inputItemId = new MyDefinitionId(inputItems[firstRequirementIndex].Content.TypeId, inputItems[firstRequirementIndex].Content.SubtypeId); for (int j = 0; j < blueprint.Prerequisites.Length; ++j) { if (blueprint.Prerequisites[j].Id.Equals(inputItemId)) { found = true; break; } } if (found) { m_tmpSortedBlueprints.Add(new KeyValuePair <int, MyBlueprintDefinitionBase>(firstRequirementIndex, blueprint)); break; } firstRequirementIndex++; } } } m_tmpSortedBlueprints.Sort((pair1, pair2) => pair1.Key - pair2.Key); MyFixedPoint buildAmount, remainingAmount; foreach (var pair in m_tmpSortedBlueprints) { var blueprint = pair.Value; buildAmount = MyFixedPoint.MaxValue; foreach (var requirement in blueprint.Prerequisites) { remainingAmount = 0; m_tmpInventoryCounts.TryGetValue(requirement.Id, out remainingAmount); if (remainingAmount == 0) { buildAmount = 0; break; } buildAmount = MyFixedPoint.Min((remainingAmount * (1f / (float)requirement.Amount)), buildAmount); } if (blueprint.Atomic) { buildAmount = MyFixedPoint.Floor(buildAmount); } if (buildAmount > 0 && buildAmount != MyFixedPoint.MaxValue) { InsertQueueItemRequest(-1, blueprint, buildAmount); foreach (var prerequisite in blueprint.Prerequisites) { m_tmpInventoryCounts.TryGetValue(prerequisite.Id, out remainingAmount); remainingAmount -= prerequisite.Amount * buildAmount; //Debug.Assert(remainingAmount >= 0); if (remainingAmount == 0) { m_tmpInventoryCounts.Remove(prerequisite.Id); } else { m_tmpInventoryCounts[prerequisite.Id] = remainingAmount; } } } } m_tmpSortedBlueprints.Clear(); m_tmpInventoryCounts.Clear(); }
public static MyFixedPoint ToFixedPoint(this int value) { return(MyFixedPoint.DeserializeString(value.ToString(CultureInfo.InvariantCulture))); }
private void RebuildQueue() { Debug.Assert(Sync.IsServer); m_queueNeedsRebuild = false; ClearQueue(false); //Changed by Gregory: Allow for duplicate blueprints cause it should be a supported functionality to add resources of the same type more than once //So now the index is essentially given by input items of inventory. Maybe try something more efficient? m_tmpSortedBlueprints.Clear(); var inputItems = InputInventory.GetItems(); for (int indx = 0; indx < inputItems.Count; indx++) { for (int i = 0; i < m_refineryDef.BlueprintClasses.Count; ++i) { foreach (var blueprint in m_refineryDef.BlueprintClasses[i]) { bool found = false; MyDefinitionId inputItemId = new MyDefinitionId(inputItems[indx].Content.TypeId, inputItems[indx].Content.SubtypeId); for (int j = 0; j < blueprint.Prerequisites.Length; ++j) { if (blueprint.Prerequisites[j].Id.Equals(inputItemId)) { found = true; break; } } if (found) { m_tmpSortedBlueprints.Add(new KeyValuePair <int, MyBlueprintDefinitionBase>(indx, blueprint)); break; } } } } MyFixedPoint buildAmount, remainingAmount; for (int i = 0; i < m_tmpSortedBlueprints.Count; i++) { var blueprint = m_tmpSortedBlueprints[i].Value; buildAmount = MyFixedPoint.MaxValue; foreach (var requirement in blueprint.Prerequisites) { remainingAmount = inputItems[i].Amount; if (remainingAmount == 0) { buildAmount = 0; break; } buildAmount = MyFixedPoint.Min((remainingAmount * (1f / (float)requirement.Amount)), buildAmount); } if (blueprint.Atomic) { buildAmount = MyFixedPoint.Floor(buildAmount); } if (buildAmount > 0 && buildAmount != MyFixedPoint.MaxValue) { InsertQueueItemRequest(-1, blueprint, buildAmount); } } m_tmpSortedBlueprints.Clear(); }
/// <summary> /// Transfers safely given item from inventory given as parameter to this instance. /// </summary> /// <returns>true if items were succesfully transfered, otherwise, false</returns> public abstract bool TransferItemsFrom(MyInventoryBase sourceInventory, IMyInventoryItem item, MyFixedPoint amount, bool stack);
public abstract void ConsumeItem(MyDefinitionId itemId, MyFixedPoint amount, long consumerEntityId = 0);
public abstract bool Remove(IMyInventoryItem item, MyFixedPoint amount);
public static MyEntity SpawnInWorldOrLootBag(this MyPhysicalInventoryItem thisItem, MyEntity owner, ref MyEntity lootBagEntity) { Debug.Assert(Sandbox.Game.Multiplayer.Sync.IsServer); MyDefinitionBase itemDefinition = thisItem.GetItemDefinition(); Debug.Assert(itemDefinition != null); if (itemDefinition == null) { return(null); } MyEntity spawnedItem = null; Vector3 upDir = -MyGravityProviderSystem.CalculateNaturalGravityInPoint(owner.PositionComp.WorldMatrix.Translation); if (upDir == Vector3.Zero) { upDir = Vector3.Up; } else { upDir.Normalize(); } if (itemDefinition is MyCubeBlockDefinition) { MyCubeBlockDefinition blockDef = itemDefinition as MyCubeBlockDefinition; if (MyDefinitionManager.Static.GetLootBagDefinition() != null) { // New code which tries to spawn item with "MyEntities.FindFreePlace" and if there is no such position then loot bag is spawn and item is moved into it. MyModel blockModel = MyModels.GetModelOnlyData(blockDef.Model); BoundingBox box = blockModel.BoundingBox; box.Inflate(0.15f); // Inflate with value that is higher than half size of small grid so it will eliminate problems with block center offsets. float radius = box.HalfExtents.Max(); var baseSpawnPosition = owner.PositionComp.WorldMatrix.Translation; if (owner is MyCharacter) { baseSpawnPosition += owner.PositionComp.WorldMatrix.Up + owner.PositionComp.WorldMatrix.Forward; } else { baseSpawnPosition += upDir; } for (int gridIndex = 0; gridIndex < thisItem.Amount; ++gridIndex) { Vector3D?spawnPos = null; if (lootBagEntity == null && (spawnPos = MyEntities.FindFreePlace(baseSpawnPosition, radius, maxTestCount: 50, testsPerDistance: 5, stepSize: 0.25f)) != null) { MatrixD transform = owner.PositionComp.WorldMatrix; transform.Translation = spawnPos.Value; var blockBuilder = MyObjectBuilderSerializer.CreateNewObject(blockDef.Id) as MyObjectBuilder_CubeBlock; blockBuilder.Min = blockDef.Size / 2 - blockDef.Size + Vector3I.One; blockBuilder.EntityId = MyEntityIdentifier.AllocateId(); var newGrid = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_CubeGrid>(); newGrid.PositionAndOrientation = new MyPositionAndOrientation(transform); newGrid.GridSizeEnum = blockDef.CubeSize; newGrid.PersistentFlags |= MyPersistentEntityFlags2.InScene; newGrid.EntityId = MyEntityIdentifier.AllocateId(); newGrid.CubeBlocks.Add(blockBuilder); var entity = MyEntities.CreateFromObjectBuilderAndAdd(newGrid); spawnedItem = spawnedItem ?? entity; } else { AddItemToLootBag(owner, new MyPhysicalInventoryItem(1, thisItem.Content), ref lootBagEntity); } } } else { // Old code used in SE (when no loot bag definition is defined). float spawnRadius = MyUtils.GetRandomFloat(Math.Max(0.25f, ITEM_SPAWN_RADIUS), ITEM_SPAWN_RADIUS); Vector3D randomizer = MyUtils.GetRandomVector3CircleNormalized() * spawnRadius + Vector3D.Up * 0.25f; int yOffset = 0; MyModel m = MyModels.GetModelOnlyData(blockDef.Model); float sizeY = m.BoundingBoxSize.Y + 0.05f; BoundingBox box = m.BoundingBox; for (int gridIndex = 0; gridIndex < thisItem.Amount; ++gridIndex) { var newGrid = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_CubeGrid>(); MatrixD transform = owner.PositionComp.WorldMatrix * MatrixD.CreateTranslation(randomizer + new Vector3D(0, yOffset * sizeY, 0)); if (!GetNonPenetratingTransformPosition(ref box, ref transform)) { randomizer = MyUtils.GetRandomVector3CircleNormalized() * 0.25f + Vector3D.Up * 0.25f; transform = owner.PositionComp.WorldMatrix * MatrixD.CreateTranslation(randomizer + new Vector3D(0, yOffset * sizeY, 0)); } newGrid.PositionAndOrientation = new MyPositionAndOrientation(transform); newGrid.GridSizeEnum = blockDef.CubeSize; newGrid.PersistentFlags |= MyPersistentEntityFlags2.InScene; newGrid.EntityId = MyEntityIdentifier.AllocateId(); var newBlock = MyObjectBuilderSerializer.CreateNewObject(blockDef.Id) as MyObjectBuilder_CubeBlock; newBlock.EntityId = MyEntityIdentifier.AllocateId(); newGrid.CubeBlocks.Add(newBlock); var entity = MyEntities.CreateFromObjectBuilderAndAdd(newGrid); spawnedItem = spawnedItem ?? entity; if ((gridIndex + 1) % 10 == 0) { spawnRadius = MyUtils.GetRandomFloat(Math.Max(0.25f, ITEM_SPAWN_RADIUS), ITEM_SPAWN_RADIUS); randomizer = MyUtils.GetRandomVector3CircleNormalized() * spawnRadius + Vector3D.Up * 0.25f; yOffset = 0; } else { yOffset++; } } } } else if (itemDefinition is MyPhysicalItemDefinition) { MyPhysicalItemDefinition floatingObjectDefinition = itemDefinition as MyPhysicalItemDefinition; MyFixedPoint amount = thisItem.Amount; bool canStack = thisItem.Content.CanStack(thisItem.Content); MyFixedPoint stackSize = canStack ? amount : 1; MyFixedPoint maxStackAmount = MyFixedPoint.MaxValue; MyComponentDefinition compDef = null; if (MyDefinitionManager.Static.TryGetComponentDefinition(thisItem.Content.GetId(), out compDef)) { maxStackAmount = compDef.MaxStackAmount; stackSize = MyFixedPoint.Min(stackSize, maxStackAmount); } if (MyDefinitionManager.Static.GetLootBagDefinition() != null) { // New code which tries to spawn item with "MyEntities.FindFreePlace" and if there is no such position then loot bag is spawn and item is moved into it. MyModel model = MyModels.GetModelOnlyData(floatingObjectDefinition.Model); BoundingBox box = model.BoundingBox; float radius = box.HalfExtents.Max(); var baseSpawnPosition = owner.PositionComp.WorldMatrix.Translation; if (owner is MyCharacter) { baseSpawnPosition += owner.PositionComp.WorldMatrix.Up + owner.PositionComp.WorldMatrix.Forward; } else { baseSpawnPosition += upDir; } while (amount > 0) { MyFixedPoint spawnAmount = stackSize; amount -= stackSize; if (amount < 0) { spawnAmount = amount + stackSize; } Vector3D?spawnPos = null; if (lootBagEntity == null && (spawnPos = MyEntities.FindFreePlace(baseSpawnPosition, radius, maxTestCount: 50, testsPerDistance: 5, stepSize: 0.25f)) != null) { MatrixD worldMat = owner.PositionComp.WorldMatrix; worldMat.Translation = spawnPos.Value; var entity = MyFloatingObjects.Spawn(new MyPhysicalInventoryItem(spawnAmount, thisItem.Content), worldMat); spawnedItem = spawnedItem ?? entity; } else { AddItemToLootBag(owner, new MyPhysicalInventoryItem(spawnAmount, thisItem.Content), ref lootBagEntity); } } } else { // Old code used in SE (when no loot bag definition is defined). while (amount > 0) { MyFixedPoint spawnAmount = stackSize; amount -= stackSize; if (amount < 0) { spawnAmount = amount + stackSize; } float spawnRadius = MyUtils.GetRandomFloat(Math.Max(0.25f, ITEM_SPAWN_RADIUS), ITEM_SPAWN_RADIUS); Vector3D randomizer = MyUtils.GetRandomVector3CircleNormalized() * spawnRadius + Vector3D.Up * 0.25f; var worldMat = owner.PositionComp.WorldMatrix * MatrixD.CreateTranslation(randomizer); var entity = MyFloatingObjects.Spawn(new MyPhysicalInventoryItem(spawnAmount, thisItem.Content), worldMat); spawnedItem = spawnedItem ?? entity; } } } return(spawnedItem); }
private string WriteProgressBar(char filled, char empty, MyFixedPoint current, int length, MyFixedPoint max) { string s = "["; float onePercent = max.ToIntSafe() / (float)100; for (int i = 0; i < length; i++) { if (i * onePercent < current.ToIntSafe()) { s += filled; } else { s += empty; } } return($"{s}]"); }
public override ChangeInfo Update(MyEntity owner, long playerID = 0) { ChangeInfo changed = ChangeInfo.None; if (MyCubeBuilder.Static == null) { return(changed); } var blockDefinition = MyCubeBuilder.Static.IsActivated ? MyCubeBuilder.Static.ToolbarBlockDefinition : null; var blockDef = (this.Definition as Sandbox.Definitions.MyCubeBlockDefinition); if ((MyCubeBuilder.Static.BlockCreationIsActivated || MyCubeBuilder.Static.MultiBlockCreationIsActivated) && blockDefinition != null && (!MyFakes.ENABLE_BATTLE_SYSTEM || !MySession.Static.Battle)) { if (blockDefinition.BlockPairName == blockDef.BlockPairName) { WantsToBeSelected = true; } else if (blockDef.BlockStages != null && blockDef.BlockStages.Contains(blockDefinition.Id)) { WantsToBeSelected = true; } else { WantsToBeSelected = false; } } else { WantsToBeSelected = false; } var character = MySession.Static.LocalCharacter; if (MyFakes.ENABLE_GATHERING_SMALL_BLOCK_FROM_GRID) { if (blockDef.CubeSize == MyCubeSize.Small && character != null) { var inventory = character.GetInventory(); MyFixedPoint amount = inventory != null?inventory.GetItemAmount(Definition.Id) : 0; if (m_lastAmount != amount) { m_lastAmount = amount; changed |= ChangeInfo.IconText; } if (MySession.Static.SurvivalMode) { changed |= SetEnabled(m_lastAmount > 0); } else { changed |= SetEnabled(true); // so that we correctly set icontext when changing from enabled to disabled even when the amount is the same changed |= ChangeInfo.IconText; } } } else { changed |= SetEnabled(true); } if (MyPerGameSettings.EnableResearch && MySessionComponentResearch.Static != null && (blockDef.CubeSize == MyCubeSize.Large)) { changed |= SetEnabled(MySessionComponentResearch.Static.CanUse(character, Definition.Id)); } return(changed); }
public override void CleanUp() { base.CleanUp(); Mags = 0; WeaponId = -1; }
private bool CheckInventoryCapacity(MyInventory inventory, MyBlueprintDefinitionBase.Item item, MyFixedPoint amountMultiplier) { return(inventory.CanItemsBeAdded(item.Amount * amountMultiplier, item.Id)); }
static long toLong(MyFixedPoint p) { return(p.RawValue / 1000000); }
private bool CheckInventoryCapacity(MyInventory inventory, MyBlueprintDefinitionBase.Item[] items, MyFixedPoint amountMultiplier) { if (MySession.Static.CreativeMode) { return(true); } MyFixedPoint resultVolume = 0; foreach (var item in items) { var def = MyDefinitionManager.Static.GetPhysicalItemDefinition(item.Id); resultVolume += (MyFixedPoint)def.Volume * item.Amount * amountMultiplier; } return(inventory.CurrentVolume + resultVolume <= inventory.MaxVolume); }
protected virtual void RemoveOperatingItem_Implementation(MyPhysicalInventoryItem item, MyFixedPoint amount) { }
private bool CheckInventoryContents(MyInventory inventory, MyBlueprintDefinitionBase.Item item, MyFixedPoint amountMultiplier) { return(inventory.ContainItems(item.Amount * amountMultiplier, item.Id)); }
private bool CheckInventoryContents(MyInventory inventory, MyBlueprintDefinitionBase.Item[] item, MyFixedPoint amountMultiplier) { for (int i = 0; i < item.Length; ++i) { if (!inventory.ContainItems(item[i].Amount * amountMultiplier, item[i].Id)) { return(false); } } return(true); }
protected override void RemoveFirstQueueItem(MyFixedPoint amount, float progress = 0f) { CurrentProgress = progress; base.RemoveFirstQueueItem(amount); }
protected override void InsertQueueItem(int idx, MyBlueprintDefinitionBase blueprint, MyFixedPoint amount) { if (idx == 0) { var queueItem = TryGetFirstQueueItem(); if (queueItem.HasValue && queueItem.Value.Blueprint != blueprint) { CurrentProgress = 0f; } } base.InsertQueueItem(idx, blueprint, amount); }
private void DisassembleAllInOutput() { ClearQueue(sendEvent: false); var items = OutputInventory.GetItems(); var toAdd = new List <Tuple <MyBlueprintDefinitionBase, MyFixedPoint> >(); bool add = true; foreach (var item in items) { var blueprint = MyDefinitionManager.Static.TryGetBlueprintDefinitionByResultId(item.Content.GetId()); if (blueprint != null) { var tuple = Tuple.Create(blueprint, item.Amount); toAdd.Add(tuple); } else { add = false; toAdd.Clear(); break; } } if (add) { foreach (var bp in toAdd) { InsertQueueItemRequest(-1, bp.Item1, bp.Item2); } return; } InitializeInventoryCounts(inputInventory: false); MyFixedPoint disassembleAmount, remainingAmount; for (int i = 0; i < m_assemblerDef.BlueprintClasses.Count; ++i) { foreach (var blueprint in m_assemblerDef.BlueprintClasses[i]) { disassembleAmount = MyFixedPoint.MaxValue; foreach (var result in blueprint.Results) { remainingAmount = 0; m_tmpInventoryCounts.TryGetValue(result.Id, out remainingAmount); if (remainingAmount == 0) { disassembleAmount = 0; break; } disassembleAmount = MyFixedPoint.Min((MyFixedPoint)((double)remainingAmount / (double)result.Amount), disassembleAmount); } if (blueprint.Atomic) { disassembleAmount = MyFixedPoint.Floor(disassembleAmount); } if (disassembleAmount > 0) { InsertQueueItemRequest(-1, blueprint, disassembleAmount); foreach (var result in blueprint.Results) { m_tmpInventoryCounts.TryGetValue(result.Id, out remainingAmount); remainingAmount -= result.Amount * disassembleAmount; Debug.Assert(remainingAmount >= 0); if (remainingAmount == 0) { m_tmpInventoryCounts.Remove(result.Id); } else { m_tmpInventoryCounts[result.Id] = remainingAmount; } } } } } m_tmpInventoryCounts.Clear(); }
public abstract bool Add(IMyInventoryItem item, MyFixedPoint amount, bool stack = true);
protected override void AddProducedItemToInventory(MyBlueprintDefinitionBase definition, MyFixedPoint amountMult) { System.Diagnostics.Debug.Assert(Sync.IsServer, "This method should be called only on server!"); if (!Sync.IsServer) { return; } MyInventory interactingInventory = null; MyInventory innerInventory = (Entity as MyEntity).GetInventory(); bool resultAdded = false; if (m_lastEntityInteraction != null) { interactingInventory = (m_lastEntityInteraction as MyEntity).GetInventory(); if (interactingInventory != null) { foreach (var prodItem in definition.Results) { var amountToAdd = prodItem.Amount * amountMult; var inventoryItem = CreateInventoryItem(prodItem.Id, amountToAdd); resultAdded = interactingInventory.Add(inventoryItem, inventoryItem.Amount); if (!resultAdded) { resultAdded = innerInventory.Add(inventoryItem, inventoryItem.Amount); } System.Diagnostics.Debug.Assert(resultAdded, "Result of adding is false!"); } } } else { if (innerInventory == null) { System.Diagnostics.Debug.Fail("Inventory was not found on the entity!"); return; } foreach (var prodItem in definition.Results) { var amountToAdd = prodItem.Amount * amountMult; var inventoryItem = CreateInventoryItem(prodItem.Id, amountToAdd); resultAdded = innerInventory.Add(inventoryItem, inventoryItem.Amount); System.Diagnostics.Debug.Assert(resultAdded, "Result of adding is false!"); } } m_lastEntityInteraction = null; }
//TODO: This should be deprecated, we shoud add IMyInventoryItems objects only , instead of items based on their objectbuilder /// <summary> /// Adds item to inventory /// </summary> /// <param name="amount"></param> /// <param name="objectBuilder"></param> /// <param name="index"></param> /// <returns>true if items were added, false if items didn't fit</returns> public abstract bool AddItems(MyFixedPoint amount, MyObjectBuilder_Base objectBuilder, int index = -1, bool stack = true);
public bool ContainItems(MyFixedPoint amount, MyItemType itemType) => FindItem(itemType)?.Amount >= amount;