Beispiel #1
0
        private static void OnTransferItemsBaseMsg(ref TransferItemsBaseMsg msg, MyNetworkClient sender)
        {
            MyEntity sourceContainer      = MyEntities.GetEntityById(msg.SourceContainerId);
            MyEntity destinationContainer = MyEntities.GetEntityById(msg.DestinationContainerId);

            if (sourceContainer == null || destinationContainer == null)
            {
                Debug.Fail("Containers/Entities weren't found!");
                return;
            }

            MyInventoryBase sourceInventory      = sourceContainer.GetInventory(msg.SourceInventoryId);
            MyInventoryBase destinationInventory = destinationContainer.GetInventory(msg.DestinationInventoryId);

            if (sourceInventory == null || destinationInventory == null)
            {
                Debug.Fail("Inventories weren't found!");
                return;
            }

            var items = sourceInventory.GetItems();

            foreach (var item in items)
            {
                if (item.ItemId == msg.SourceItemId)
                {
                    MyInventoryBase.TransferItems(sourceInventory, destinationInventory, item, msg.Amount);
                    return;
                }
            }
        }
Beispiel #2
0
        private static void InventoryBaseTransferItem_Implementation(MyInventoryTransferEventContent eventParams)
        {
            if (!MyEntities.EntityExists(eventParams.DestinationOwnerId) || !MyEntities.EntityExists(eventParams.SourceOwnerId))
            {
                return;
            }

            MyEntity                sourceOwner = MyEntities.GetEntityById(eventParams.SourceOwnerId);
            MyInventoryBase         source      = sourceOwner.GetInventory(eventParams.SourceInventoryId);
            MyEntity                destOwner   = MyEntities.GetEntityById(eventParams.DestinationOwnerId);
            MyInventoryBase         dst         = destOwner.GetInventory(eventParams.DestinationInventoryId);
            var                     items       = source.GetItems();
            MyPhysicalInventoryItem?foundItem   = null;

            foreach (var item in items)
            {
                if (item.ItemId == eventParams.ItemId)
                {
                    foundItem = item;
                }
            }

            if (foundItem.HasValue)
            {
                dst.TransferItemsFrom(source, foundItem, eventParams.Amount);
            }
        }
Beispiel #3
0
        /*
         * private void BeforeContentsChanged(MyInventoryBase inventory) {
         *  Log.Trace("BeforeContentsChanged called on inventory " + inventory.Entity.ToString(), "BeforeContentsChanged");
         * }
         */

        private void OnContentsChanged(MyInventoryBase inventory)
        {
            Log.Trace("Updating inventory cache with inventory " + inventory.Entity.ToString(), "OnContentsChanged");

            ItemCountsAggregate cachedCount;

            if (!InventoryTotals.TryGetValue(inventory, out cachedCount))
            {
                Log.Error("Received an update for inventory we're not tracking.", "UpdateInventory");
                return;
            }

            ItemCountsAggregate originalCounts = cachedCount.Copy();

            Totals -= originalCounts;

            if (WatchedItems != null)
            {
                foreach (var id in WatchedItems)
                {
                    cachedCount.Set(id, inventory.GetItemAmount(id));
                }
            }
            else
            {
                foreach (var item in inventory.GetItems())
                {
                    cachedCount.Set(item.Content.GetObjectId(), item.Amount);
                }
            }

            Totals += cachedCount;

            if (SkipNextNotify)
            {
                SkipNextNotify = false;
            }
            else
            {
                NotifyContentsChanged(cachedCount - originalCounts);
            }

            //DebugPrint();
        }
Beispiel #4
0
        private static bool playerDrinkSomething(IMyEntity entity, PlayerData playerData)
        {
            MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase();
            var             items     = inventory.GetItems();

            foreach (IMyInventoryItem item in items)
            {
                float result;

                // Getting the item type

                string szItemContent = item.Content.ToString();

                //MyAPIGateway.Utilities.ShowMessage("DEBUG", "szItemContent: " + item.Content.SubtypeName);

                string szTypeName = szItemContent.Substring(szItemContent.IndexOf(OBJECT_BUILDER_PREFIX) + OBJECT_BUILDER_PREFIX.Length);

                // Type verification

                if (!szTypeName.Equals("Ingot"))
                {
                    continue;
                }

                if (mBeverageTypes.TryGetValue(item.Content.SubtypeName, out result))
                {
                    float canConsumeNum = Math.Min(((MAX_VALUE - playerData.thirst) / result), (float)item.Amount);

                    //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canDrink: " + canConsumeNum);

                    if (canConsumeNum > 0)
                    {
                        inventory.Remove(item, (MyFixedPoint)canConsumeNum);
                        playerData.thirst += result * (float)canConsumeNum;

                        return(true);
                    }
                }
            }

            return(false);
        }
        private void OnOutputChange(MyInventoryBase output)
        {
            MyPhysicalInventoryItem[] currentOutput = output.GetItems().ToArray();

            IEnumerable <MyPhysicalInventoryItem> added   = currentOutput.Except(LastOutput);
            IEnumerable <MyPhysicalInventoryItem> removed = LastOutput.Except(currentOutput);

            if (removed.Count() == 1 && added.Count() == 1)
            {
                MyPhysicalInventoryItem old = removed.First();
                MyPhysicalInventoryItem cur = added.First();

                if (old.Amount > cur.Amount)
                {
                    old.Amount -= cur.Amount;
                    UpdateAmounts(InvType.Output, InvAction.Remove, old);
                }
                else
                {
                    cur.Amount -= old.Amount;
                    UpdateAmounts(InvType.Output, InvAction.Add, cur);
                }
            }
            else if (removed.Count() >= 1)
            {
                foreach (MyPhysicalInventoryItem item in removed)
                {
                    UpdateAmounts(InvType.Output, InvAction.Remove, item);
                }
            }
            else if (added.Count() >= 1)
            {
                foreach (MyPhysicalInventoryItem item in added)
                {
                    UpdateAmounts(InvType.Output, InvAction.Add, item);
                }
            }

            Increment();
        }
Beispiel #6
0
        private static bool playerEatSomething(IMyEntity entity, PlayerData playerData)
        {
            MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase();
            var             items     = inventory.GetItems();

            foreach (IMyInventoryItem item in items)
            {
                float result;
                if (mFoodTypes.TryGetValue(item.GetDefinitionId(), out result))
                {
                    float canConsumeNum = Math.Min(((MAX_VALUE - playerData.hunger) / result), (float)item.Amount);
                    //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canEat: " + canConsumeNum);
                    if (canConsumeNum > 0)
                    {
                        inventory.Remove(item, (MyFixedPoint)canConsumeNum);
                        playerData.hunger += result * (float)canConsumeNum;
                        return(true);
                    }
                }
            }
            return(false);
        }
        private void OnInputChange(MyInventoryBase input)
        {
            MyPhysicalInventoryItem[] currentInput = input.GetItems().ToArray();

            IEnumerable <MyPhysicalInventoryItem> added   = currentInput.Except(LastInput);
            IEnumerable <MyPhysicalInventoryItem> removed = LastInput.Except(currentInput);

            if (added.Count() > 1 || removed.Count() > 1)
            {
                ActivityCollector.Log.Error("More than one component being added or removed!!");
            }

            if (removed.Count() == 1 && added.Count() == 1)
            {
                MyPhysicalInventoryItem old = removed.First();
                MyPhysicalInventoryItem cur = added.First();

                if (old.Amount > cur.Amount)
                {
                    old.Amount -= cur.Amount;
                    UpdateAmounts(InvType.Input, InvAction.Remove, old);
                }
                else
                {
                    cur.Amount -= old.Amount;
                    UpdateAmounts(InvType.Input, InvAction.Add, cur);
                }
            }
            else if (removed.Count() == 1)
            {
                UpdateAmounts(InvType.Input, InvAction.Remove, removed.First());
            }
            else if (added.Count() == 1)
            {
                UpdateAmounts(InvType.Input, InvAction.Add, added.First());
            }

            Increment();
        }
Beispiel #8
0
        public static Ammo GetLoadedAmmo(IMyCubeBlock weapon)
        {
            MyEntity entity = (MyEntity)weapon;

            if (!entity.HasInventory)
            {
                throw new InvalidOperationException("Has no inventory: " + weapon.getBestName());
            }

            MyInventoryBase inv = entity.GetInventoryBase(0);

            if (inv.GetItemsCount() == 0)
            {
                return(null);
            }

            MyDefinitionId magazineId;

            try { magazineId = inv.GetItems()[0].Content.GetId(); }
            catch (IndexOutOfRangeException)             // because of race condition
            { return(null); }

            return(GetAmmo(magazineId));
        }
Beispiel #9
0
        private static bool playerDrinkSomething(IMyEntity entity, PlayerData playerData, float maxval_cap, float crapbonus)
        {
            MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase();
            var             items     = inventory.GetItems();

            foreach (IMyInventoryItem item in items)
            {
                float result;

                // Getting the item type

                string szItemContent = item.Content.ToString();

                //MyAPIGateway.Utilities.ShowMessage("DEBUG", "szItemContent: " + item.Content.SubtypeName);

                string szTypeName = szItemContent.Substring(szItemContent.IndexOf(OBJECT_BUILDER_PREFIX) + OBJECT_BUILDER_PREFIX.Length);

                // Type verification

                if (!szTypeName.Equals("Ingot"))
                {
                    continue;
                }

                if (mBeverageTypes.TryGetValue(item.Content.SubtypeName, out result))
                {
                    float canConsumeNum = Math.Min(((maxval_cap - playerData.thirst) / result), (float)item.Amount);

                    //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canDrink: " + canConsumeNum);

                    if (canConsumeNum > 0)
                    {
                        inventory.Remove(item, (MyFixedPoint)canConsumeNum);
                        playerData.thirst += result * (float)canConsumeNum;
                        if (item.Content.SubtypeName.Contains("offee"))                                                                 // TODO parametrize this
                        {
                            playerData.fatigue = Config.MaxValue;                                                                       // TODO parametrize this
                        }
                        if (item.Content.SubtypeName.Contains("ouillon"))                                                               // TODO parametrize this
                        {
                            playerData.hunger += Math.Max(0f, Math.Min(result * (float)canConsumeNum, maxval_cap - playerData.hunger)); // TODO parametrize this
                        }
                        // waste management line
                        if (CRAP_AMOUNT > 0.0)
                        {
                            inventory.AddItems((MyFixedPoint)(canConsumeNum * CRAP_AMOUNT * crapbonus), new MyObjectBuilder_Ingot()
                            {
                                SubtypeName = "GreyWater"
                            });
                            if (CROSS_CRAP_AMOUNT > 0.0)
                            {
                                inventory.AddItems((MyFixedPoint)(canConsumeNum * (1 - CRAP_AMOUNT) * CROSS_CRAP_AMOUNT), new MyObjectBuilder_Ore()
                                {
                                    SubtypeName = "Organic"
                                });
                            }
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
 private void OnBeforeOutputChange(MyInventoryBase output)
 {
     LastOutput = output.GetItems().ToArray();
 }
 private void OnBeforeInputChange(MyInventoryBase input)
 {
     LastInput = input.GetItems().ToArray();
 }