private static void AddItemToLootBag(MyEntity itemOwner, MyPhysicalInventoryItem item, ref MyEntity lootBagEntity) { MyLootBagDefinition lootBagDefinition = MyDefinitionManager.Static.GetLootBagDefinition(); if (lootBagDefinition != null) { MyDefinitionBase itemDefinition = item.GetItemDefinition(); if (itemDefinition != null) { if ((lootBagEntity == null) && (lootBagDefinition.SearchRadius > 0f)) { Vector3D position = itemOwner.PositionComp.GetPosition(); BoundingSphereD boundingSphere = new BoundingSphereD(position, (double)lootBagDefinition.SearchRadius); List <MyEntity> entitiesInSphere = MyEntities.GetEntitiesInSphere(ref boundingSphere); double maxValue = double.MaxValue; foreach (MyEntity entity in entitiesInSphere) { if (entity.MarkedForClose) { continue; } if ((entity.GetType() == typeof(MyEntity)) && ((entity.DefinitionId != null) && (entity.DefinitionId.Value == lootBagDefinition.ContainerDefinition))) { double num2 = (entity.PositionComp.GetPosition() - position).LengthSquared(); if (num2 < maxValue) { lootBagEntity = entity; maxValue = num2; } } } entitiesInSphere.Clear(); } if ((lootBagEntity == null) || (lootBagEntity.Components.Has <MyInventoryBase>() && !(lootBagEntity.Components.Get <MyInventoryBase>() as MyInventory).CanItemsBeAdded(item.Amount, itemDefinition.Id))) { MyContainerDefinition definition2; lootBagEntity = null; if (MyComponentContainerExtension.TryGetContainerDefinition(lootBagDefinition.ContainerDefinition.TypeId, lootBagDefinition.ContainerDefinition.SubtypeId, out definition2)) { lootBagEntity = SpawnBagAround(itemOwner, definition2, 3, 2, 5, 1f); } } if (lootBagEntity != null) { MyInventory inventory = lootBagEntity.Components.Get <MyInventoryBase>() as MyInventory; if (inventory != null) { if (itemDefinition is MyCubeBlockDefinition) { inventory.AddBlocks(itemDefinition as MyCubeBlockDefinition, item.Amount); } else { inventory.AddItems(item.Amount, item.Content); } } } } } }
/// <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); } } } }