예제 #1
0
 public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> toRemove)
 {
     this.Clear();
     foreach (KeyValuePair <MyDefinitionId, int> pair in toRemove)
     {
         int amount = 0;
         MyComponentGroupDefinition groupForComponent = MyDefinitionManager.Static.GetGroupForComponent(pair.Key, out amount);
         if (groupForComponent != null)
         {
             this.AddItem(groupForComponent.Id, amount, pair.Value);
             continue;
         }
         if ((MySessionComponentEquivalency.Static != null) && MySessionComponentEquivalency.Static.HasEquivalents(pair.Key))
         {
             HashSet <MyDefinitionId> equivalents = MySessionComponentEquivalency.Static.GetEquivalents(pair.Key);
             if (equivalents == null)
             {
                 continue;
             }
             int num2 = pair.Value;
             foreach (MyDefinitionId id in equivalents)
             {
                 if (num2 <= 0)
                 {
                     break;
                 }
                 num2 -= (int)inventory.RemoveItemsOfType(num2, id, MyItemFlags.None, false);
             }
             continue;
         }
         inventory.RemoveItemsOfType(pair.Value, pair.Key, MyItemFlags.None, false);
     }
     inventory.CountItems(m_componentCounts);
     this.Solve(m_componentCounts);
     inventory.ApplyChanges(this.m_solution);
 }
        public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> toRemove)
        {
            Clear();
            foreach (var material in toRemove) // rename material to component
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    MyComponentSubstitutionDefinition substitutionDefinition = null;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(material.Key, out substitutionDefinition))
                    {
                        int amountToRemove = material.Value;
                        foreach (var entry in substitutionDefinition.ProvidingComponents)
                        {
                            if (amountToRemove > 0)
                            {
                                var removed = inventory.RemoveItemsOfType(amountToRemove * entry.Value, entry.Key);
                                amountToRemove -= (int)removed;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (amountToRemove > 0)
                        {
                            var removed = inventory.RemoveItemsOfType(amountToRemove, material.Key);
                            amountToRemove -= (int)removed;
                        }
                    }
                    else
                    {
                        inventory.RemoveItemsOfType(material.Value, material.Key);
                        continue;
                    }
                }
                else
                {
                    AddItem(group.Id, groupAmount, material.Value);
                }
            }

            inventory.CountItems(m_componentCounts);
            bool success = Solve(m_componentCounts);

            Debug.Assert(success, "Could not combine required items!");

            inventory.ApplyChanges(m_solution);

            /*CheckUpdate();
             *
             * m_remainder.Clear();
             * foreach (var material in toRemove)
             * {
             *  m_remainder.Add(material.Key, material.Value);
             * }
             *
             * bool success = true;
             *
             * m_cuttingSolver.Clear();
             * foreach (var material in m_remainder)
             * {
             *  int groupAmount = 0;
             *  MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);
             *
             *  // The component does not belong to any component group => we are looking exactly for the given component
             *  if (group == null)
             *  {
             *      success &= RemoveItemsOfTypeInternal(material.Key, material.Value);
             *      Debug.Assert(success, "Could not find the required component although we were permitted to build!");
             *      continue;
             *  }
             *  else
             *  {
             *      m_cuttingSolver.AddItem(group.Id, groupAmount, material.Value);
             *  }
             *
             *  m_componentCounts.Clear();
             *  CollectItems(m_componentCounts);
             *  success &= m_cuttingSolver.Solve(m_componentCounts);
             *
             *  List<MyComponentCombiner.ComponentChange> changes = null;
             *  m_cuttingSolver.GetSolution(out changes);
             *  foreach (var change in changes)
             *  {
             *      if (change.IsRemoval())
             *      {
             *          success &= RemoveItemsOfTypeInternal(change.ToRemove, change.Amount);
             *          Debug.Assert(success, "Could not remove compnents, although the solver told us it should be possible!");
             *      }
             *      else if (change.IsChange())
             *      {
             *          ComponentInfo cInfo = null;
             *          m_componentInfos.TryGetValue(change.ToRemove, out cInfo);
             *          Debug.Assert(cInfo != null, "Could not find a component in MyAreaInventory!");
             *
             *          if (cInfo == null) continue;
             *
             *          for (int i = 0; i < change.Amount; ++i)
             *          {
             *              int dummy = 0;
             *              long entityId = cInfo.RemoveComponent(1, out dummy);
             *              if (entityId == 0) break;
             *
             *              var grid = TryGetComponent(entityId);
             *              if (grid == null)
             *              {
             *                  break;
             *              }
             *
             *              SpawnRemainingData spawnData = new SpawnRemainingData();
             *              PrepareSpawnRemainingMaterial(grid, ref spawnData);
             *
             *              grid.Physics.Enabled = false;
             *              grid.SyncObject.SendCloseRequest();
             *
             *              spawnData.DefId = change.ToAdd;
             *              SpawnRemainingMaterial(ref spawnData);
             *          }
             *      }
             *  }
             * }
             *
             * return success;*/
        }
예제 #3
0
        public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader<MyDefinitionId, int> toRemove)
        {
            Clear();
            foreach (var material in toRemove) // rename material to component
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    MyComponentSubstitutionDefinition substitutionDefinition = null;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(material.Key, out substitutionDefinition))
                    {
                        int amountToRemove = material.Value;
                        foreach (var entry in substitutionDefinition.ProvidingComponents)
                        {
                            if (amountToRemove > 0)
                            {
                                var removed = inventory.RemoveItemsOfType(amountToRemove * entry.Value, entry.Key);
                                amountToRemove -= (int)removed;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        inventory.RemoveItemsOfType(material.Value, material.Key);
                        continue;
                    }
                }
                else
                {
                    AddItem(group.Id, groupAmount, material.Value);
                }
            }

            inventory.CountItems(m_componentCounts);
            bool success = Solve(m_componentCounts);
            Debug.Assert(success, "Could not combine required items!");

            inventory.ApplyChanges(m_solution);

            /*CheckUpdate();

            m_remainder.Clear();
            foreach (var material in toRemove)
            {
                m_remainder.Add(material.Key, material.Value);
            }

            bool success = true;

            m_cuttingSolver.Clear();
            foreach (var material in m_remainder)
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    success &= RemoveItemsOfTypeInternal(material.Key, material.Value);
                    Debug.Assert(success, "Could not find the required component although we were permitted to build!");
                    continue;
                }
                else
                {
                    m_cuttingSolver.AddItem(group.Id, groupAmount, material.Value);
                }

                m_componentCounts.Clear();
                CollectItems(m_componentCounts);
                success &= m_cuttingSolver.Solve(m_componentCounts);

                List<MyComponentCombiner.ComponentChange> changes = null;
                m_cuttingSolver.GetSolution(out changes);
                foreach (var change in changes)
                {
                    if (change.IsRemoval())
                    {
                        success &= RemoveItemsOfTypeInternal(change.ToRemove, change.Amount);
                        Debug.Assert(success, "Could not remove compnents, although the solver told us it should be possible!");
                    }
                    else if (change.IsChange())
                    {
                        ComponentInfo cInfo = null;
                        m_componentInfos.TryGetValue(change.ToRemove, out cInfo);
                        Debug.Assert(cInfo != null, "Could not find a component in MyAreaInventory!");

                        if (cInfo == null) continue;

                        for (int i = 0; i < change.Amount; ++i)
                        {
                            int dummy = 0;
                            long entityId = cInfo.RemoveComponent(1, out dummy);
                            if (entityId == 0) break;

                            var grid = TryGetComponent(entityId);
                            if (grid == null)
                            {
                                break;
                            }

                            SpawnRemainingData spawnData = new SpawnRemainingData();
                            PrepareSpawnRemainingMaterial(grid, ref spawnData);

                            grid.Physics.Enabled = false;
                            grid.SyncObject.SendCloseRequest();

                            spawnData.DefId = change.ToAdd;
                            SpawnRemainingMaterial(ref spawnData);
                        }
                    }
                }
            }

            return success;*/
        }