protected void InitializeInventoryCounts(bool inputInventory = true)
 {
     m_tmpInventoryCounts.Clear();
     foreach (var item in inputInventory ? InputInventory.GetItems() : OutputInventory.GetItems())
     {
         MyFixedPoint   count  = 0;
         MyDefinitionId itemId = new MyDefinitionId(item.Content.TypeId, item.Content.SubtypeId);
         m_tmpInventoryCounts.TryGetValue(itemId, out count);
         m_tmpInventoryCounts[itemId] = count + item.Amount;
     }
 }
示例#2
0
        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();
        }
示例#3
0
        public override void UpdateBeforeSimulation100()
        {
            base.UpdateBeforeSimulation100();

            if (m_inventoryOwnersDirty)
            {
                GetCoveyorInventoryOwners();
            }

            if (Sync.IsServer && IsWorking && m_useConveyorSystem)
            {
                if (DisassembleEnabled) // Dissasembling
                {
                    if (OutputInventory.VolumeFillFactor < 0.99f)
                    {
                        //MyGridConveyorSystem.PullAllRequest(this, OutputInventory, OwnerId, OutputInventory.Constraint);
                        var item = TryGetFirstQueueItem();
                        if (item != null)
                        {
                            if (!OutputInventory.ContainItems(null, item.Value.Blueprint.Results[0].Id))
                            {
                                MyGridConveyorSystem.ItemPullRequest(this, OutputInventory, OwnerId, item.Value.Blueprint.Results[0].Id, item.Value.Amount);
                            }
                        }
                    }
                    if (InputInventory.VolumeFillFactor > 0.75f)
                    {
                        Debug.Assert(InputInventory.GetItems().Count > 0);
                        MyGridConveyorSystem.PushAnyRequest(this, InputInventory, OwnerId);
                    }
                }
                else // Assembling
                {
                    //if (IsSlave && m_queue.Count < 1 && MyFakes.ENABLE_ASSEMBLER_COOPERATION && !RepeatEnabled)
                    //{
                    //    GetItemFromOtherAssemblers(TIME_IN_ADVANCE);
                    //}
                    if (InputInventory.VolumeFillFactor < 0.99f)
                    {
                        m_requiredComponents.Clear();

                        var next = false;
                        int i    = 0;
                        var time = 0f;
                        do
                        {
                            var item          = TryGetQueueItem(i);
                            var remainingTime = TIME_IN_ADVANCE - time;
                            if (item.HasValue)
                            {
                                var productivity = (((MyAssemblerDefinition)BlockDefinition).AssemblySpeed + UpgradeValues["Productivity"]);
                                var factor       = MySession.Static.AssemblerSpeedMultiplier * productivity;
                                var itemAmount   = 1;
                                if (item.Value.Blueprint.BaseProductionTimeInSeconds / factor < remainingTime)
                                {
                                    itemAmount = Math.Min((int)item.Value.Amount, Convert.ToInt32(Math.Ceiling(remainingTime / (item.Value.Blueprint.BaseProductionTimeInSeconds / factor))));
                                }
                                time += itemAmount * item.Value.Blueprint.BaseProductionTimeInSeconds / factor;
                                if (time < TIME_IN_ADVANCE)
                                {
                                    next = true;
                                }
                                var amountMult = (MyFixedPoint)(1.0f / MySession.Static.AssemblerEfficiencyMultiplier);
                                foreach (var component in item.Value.Blueprint.Prerequisites)
                                {
                                    var requiredAmount = component.Amount * itemAmount * amountMult;

                                    bool found = false;
                                    for (int j = 0; j < m_requiredComponents.Count; j++)
                                    {
                                        if (m_requiredComponents[j].Id == component.Id)
                                        {
                                            m_requiredComponents[j] = new MyBlueprintDefinitionBase.Item
                                            {
                                                Amount = m_requiredComponents[j].Amount + requiredAmount,
                                                Id     = component.Id
                                            };
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        m_requiredComponents.Add(new MyBlueprintDefinitionBase.Item
                                        {
                                            Amount = requiredAmount,
                                            Id     = component.Id
                                        });
                                    }
                                }
                            }

                            i++;
                            if (i >= m_queue.Count)
                            {
                                next = false;
                            }
                        } while (next);

                        foreach (var component in m_requiredComponents)
                        {
                            var availableAmount = InputInventory.GetItemAmount(component.Id);
                            var neededAmount    = component.Amount - availableAmount;
                            if (neededAmount <= 0)
                            {
                                continue;
                            }

                            MyGridConveyorSystem.ItemPullRequest(this, InputInventory, OwnerId, component.Id, neededAmount);
                        }

                        if (IsSlave && !RepeatEnabled)
                        {
                            var remainingTime = TIME_IN_ADVANCE - time;
                            if (remainingTime > 0)
                            {
                                GetItemFromOtherAssemblers(remainingTime);
                            }
                        }
                    }

                    if (OutputInventory.VolumeFillFactor > 0.75f)
                    {
                        Debug.Assert(OutputInventory.GetItems().Count > 0);
                        MyGridConveyorSystem.PushAnyRequest(this, OutputInventory, OwnerId);
                    }
                }
            }
        }
示例#4
0
        public override void UpdateBeforeSimulation100()
        {
            base.UpdateBeforeSimulation100();

            if (m_inventoryOwnersDirty)
            {
                GetCoveyorInventoryOwners();
            }

            if (Sync.IsServer && IsWorking && m_useConveyorSystem)
            {
                if (DisassembleEnabled) // Dissasembling
                {
                    if (OutputInventory.VolumeFillFactor < 0.99f)
                    {
                        //MyGridConveyorSystem.PullAllRequest(this, OutputInventory, OwnerId, OutputInventory.Constraint);
                        var item = TryGetFirstQueueItem();
                        if (item != null)
                        {
                            if (!OutputInventory.ContainItems(null, item.Value.Blueprint.Results[0].Id))
                            {
                                MyGridConveyorSystem.ItemPullRequest(this, OutputInventory, OwnerId, item.Value.Blueprint.Results[0].Id, item.Value.Amount);
                            }
                        }
                    }
                    if (InputInventory.VolumeFillFactor > 0.75f)
                    {
                        Debug.Assert(InputInventory.GetItems().Count > 0);
                        MyGridConveyorSystem.PushAnyRequest(this, InputInventory, OwnerId);
                    }
                }
                else // Assembling
                {
                    if (IsSlave && m_queue.Count < 1 && MyFakes.ENABLE_ASSEMBLER_COOPERATION && !RepeatEnabled)
                    {
                        GetItemFromOtherAssemblers();
                    }
                    if (InputInventory.VolumeFillFactor < 0.99f)
                    {
                        var next = false;
                        int i    = 0;
                        var time = 0f;
                        do
                        {
                            var item = TryGetQueueItem(i);
                            if (item.HasValue)
                            {
                                var factor        = MySession.Static.AssemblerSpeedMultiplier / MySession.Static.AssemblerEfficiencyMultiplier;
                                var itemAmount    = 1;
                                var remainingTime = TIME_IN_ADVANCE - time;
                                if (item.Value.Blueprint.BaseProductionTimeInSeconds < remainingTime)
                                {
                                    itemAmount = Math.Min((int)item.Value.Amount, Convert.ToInt32(Math.Floor(remainingTime / (item.Value.Blueprint.BaseProductionTimeInSeconds / factor))));
                                    time      += itemAmount * item.Value.Blueprint.BaseProductionTimeInSeconds / MySession.Static.AssemblerSpeedMultiplier;
                                    if (time < TIME_IN_ADVANCE)

                                    {
                                        next = true;
                                    }
                                }
                                foreach (var component in item.Value.Blueprint.Prerequisites)
                                {
                                    var availableAmount = InputInventory.GetItemAmount(component.Id);
                                    if (i > 0)
                                    {
                                        availableAmount = 0;
                                    }
                                    var neededAmount = component.Amount * itemAmount - availableAmount;
                                    if (neededAmount <= 0)
                                    {
                                        continue;
                                    }

                                    MyGridConveyorSystem.ItemPullRequest(this, InputInventory, OwnerId, component.Id, neededAmount);
                                }
                            }
                            if (i > 0)
                            {
                                next = false;
                            }
                            i++;
                        } while (next);
                    }
                    if (OutputInventory.VolumeFillFactor > 0.75f)
                    {
                        Debug.Assert(OutputInventory.GetItems().Count > 0);
                        MyGridConveyorSystem.PushAnyRequest(this, OutputInventory, OwnerId);
                    }
                }
            }
        }
示例#5
0
        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();
        }