/// <summary> /// Add the given inventory item to loot bag. If loot bag does not exist then it will be created. /// </summary> private static void AddItemToLootBag(MyEntity itemOwner, MyPhysicalInventoryItem item, ref MyEntity lootBagEntity) { Debug.Assert(Sandbox.Game.Multiplayer.Sync.IsServer); var lootBagDefinition = MyDefinitionManager.Static.GetLootBagDefinition(); Debug.Assert(lootBagDefinition != null, "Loot bag not definined"); if (lootBagDefinition == null) return; // Block MyDefinitionBase itemDefinition = item.GetItemDefinition(); Debug.Assert(itemDefinition != null, "Unknown inventory item"); if (itemDefinition == null) return; // Find lootbag nearby. if (lootBagEntity == null && lootBagDefinition.SearchRadius > 0) { Vector3D itemOwnerPosition = itemOwner.PositionComp.GetPosition(); BoundingSphereD sphere = new BoundingSphereD(itemOwnerPosition, lootBagDefinition.SearchRadius); var entitiesInSphere = MyEntities.GetEntitiesInSphere(ref sphere); double minDistanceSq = double.MaxValue; foreach (var entity in entitiesInSphere) { if (!entity.MarkedForClose && (entity.GetType() == typeof(MyEntity))) { if (entity.DefinitionId != null && entity.DefinitionId.Value == lootBagDefinition.ContainerDefinition) { var distanceSq = (entity.PositionComp.GetPosition() - itemOwnerPosition).LengthSquared(); if (distanceSq < minDistanceSq) { lootBagEntity = entity; minDistanceSq = distanceSq; } } } } entitiesInSphere.Clear(); } // Create lootbag if (lootBagEntity == null || (lootBagEntity.Components.Has<MyInventoryBase>() && !(lootBagEntity.Components.Get<MyInventoryBase>() as MyInventory).CanItemsBeAdded(item.Amount, itemDefinition.Id))) { lootBagEntity = null; MyContainerDefinition lootBagDef; if (MyComponentContainerExtension.TryGetContainerDefinition(lootBagDefinition.ContainerDefinition.TypeId, lootBagDefinition.ContainerDefinition.SubtypeId, out lootBagDef)) { lootBagEntity = SpawnBagAround(itemOwner, lootBagDef); } } Debug.Assert(lootBagEntity != null, "Loot bag not created"); // Fill lootbag inventory if (lootBagEntity != null) { MyInventory inventory = lootBagEntity.Components.Get<MyInventoryBase>() as MyInventory; Debug.Assert(inventory != null); if (inventory != null) { if (itemDefinition is MyCubeBlockDefinition) inventory.AddBlocks(itemDefinition as MyCubeBlockDefinition, item.Amount); else inventory.AddItems(item.Amount, item.Content); } } }
public void AddChangedPhysicalInventoryItem(MyPhysicalInventoryItem intentoryItem, MyFixedPoint changedAmount, bool added) { Debug.Assert(changedAmount > 0 || (!added && changedAmount < 0)); var definition = intentoryItem.GetItemDefinition(); if (definition == null) return; if (changedAmount < 0) changedAmount = -changedAmount; Debug.Assert(changedAmount > 0); var item = new MyItemInfo() { DefinitionId = definition.Id, Icons = definition.Icons, TotalAmount = intentoryItem.Amount, ChangedAmount = changedAmount, Added = added }; AddItem(item); }
public void AddPhysicalInventoryItem(MyPhysicalInventoryItem intentoryItem, MyFixedPoint addedAmount) { var definition = intentoryItem.GetItemDefinition(); if (definition == null) return; var item = new MyItemInfo() { DefinitionId = definition.Id, Icon = definition.Icon, TotalAmount = intentoryItem.Amount, AddedAmount = addedAmount }; AddItem(item); }