Пример #1
0
        public void RefillInventory(MyInventory inventory) 
        {
            foreach (MyInventoryTemplateItem templateItem in TemplateItems) 
            {
                // test chance to refill
                if (MyMwcUtils.GetRandomFloat(0.0001f, 1f) > templateItem.ChanceToRefill) 
                {
                    continue;
                }

                // get count to refill
                int existingCount = inventory.GetInventoryItemsCount(templateItem.ObjectBuilderType, templateItem.ObjectBuilderId);
                int countToRefill = MyMwcUtils.GetRandomInt(templateItem.CountToRefillMin, templateItem.CountToRefillMax) - existingCount;
                for (int i = 0; i < countToRefill; i++) 
                {
                    if (inventory.IsFull) 
                    {
                        return;
                    }

                    // get amount ratio to refill
                    float amountRatio = MyMwcUtils.GetRandomFloat(templateItem.AmountRatioMin, templateItem.AmountRatioMax);
                    if(amountRatio > 0f)
                    {
                        MyMwcObjectBuilder_Base objectBuilder = MyMwcObjectBuilder_Base.CreateNewObject(templateItem.ObjectBuilderType, templateItem.ObjectBuilderId);
                        MyInventoryItem inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(objectBuilder);
                        inventoryItem.Amount = (float)(int)(amountRatio * inventoryItem.MaxAmount);
                        inventory.AddInventoryItem(inventoryItem);
                    }
                }
            }
        }